clang  16.0.0git
Integral.h
Go to the documentation of this file.
1 //===--- Integral.h - Wrapper for numeric types for the VM ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the VM types and helpers operating on types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_INTERP_INTEGRAL_H
14 #define LLVM_CLANG_AST_INTERP_INTEGRAL_H
15 
17 #include "clang/AST/APValue.h"
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <cstddef>
22 #include <cstdint>
23 
24 namespace clang {
25 namespace interp {
26 
29 
30 /// Helper to compare two comparable types.
31 template <typename T>
32 ComparisonCategoryResult Compare(const T &X, const T &Y) {
33  if (X < Y)
35  if (X > Y)
38 }
39 
40 // Helper structure to select the representation.
41 template <unsigned Bits, bool Signed> struct Repr;
42 template <> struct Repr<8, false> { using Type = uint8_t; };
43 template <> struct Repr<16, false> { using Type = uint16_t; };
44 template <> struct Repr<32, false> { using Type = uint32_t; };
45 template <> struct Repr<64, false> { using Type = uint64_t; };
46 template <> struct Repr<8, true> { using Type = int8_t; };
47 template <> struct Repr<16, true> { using Type = int16_t; };
48 template <> struct Repr<32, true> { using Type = int32_t; };
49 template <> struct Repr<64, true> { using Type = int64_t; };
50 
51 /// Wrapper around numeric types.
52 ///
53 /// These wrappers are required to shared an interface between APSint and
54 /// builtin primitive numeral types, while optimising for storage and
55 /// allowing methods operating on primitive type to compile to fast code.
56 template <unsigned Bits, bool Signed> class Integral final {
57 private:
58  template <unsigned OtherBits, bool OtherSigned> friend class Integral;
59 
60  // The primitive representing the integral.
61  using ReprT = typename Repr<Bits, Signed>::Type;
62  ReprT V;
63 
64  /// Primitive representing limits.
65  static const auto Min = std::numeric_limits<ReprT>::min();
66  static const auto Max = std::numeric_limits<ReprT>::max();
67 
68  /// Construct an integral from anything that is convertible to storage.
69  template <typename T> explicit Integral(T V) : V(V) {}
70 
71 public:
72  /// Zero-initializes an integral.
73  Integral() : V(0) {}
74 
75  /// Constructs an integral from another integral.
76  template <unsigned SrcBits, bool SrcSign>
78 
79  /// Construct an integral from a value based on signedness.
80  explicit Integral(const APSInt &V)
81  : V(V.isSigned() ? V.getSExtValue() : V.getZExtValue()) {}
82 
83  bool operator<(Integral RHS) const { return V < RHS.V; }
84  bool operator>(Integral RHS) const { return V > RHS.V; }
85  bool operator<=(Integral RHS) const { return V <= RHS.V; }
86  bool operator>=(Integral RHS) const { return V >= RHS.V; }
87  bool operator==(Integral RHS) const { return V == RHS.V; }
88  bool operator!=(Integral RHS) const { return V != RHS.V; }
89 
90  bool operator>(unsigned RHS) const {
91  return V >= 0 && static_cast<unsigned>(V) > RHS;
92  }
93 
94  Integral operator-() const { return Integral(-V); }
95  Integral operator~() const { return Integral(~V); }
96 
97  template <unsigned DstBits, bool DstSign>
98  explicit operator Integral<DstBits, DstSign>() const {
100  }
101 
102  explicit operator unsigned() const { return V; }
103  explicit operator int64_t() const { return V; }
104  explicit operator uint64_t() const { return V; }
105 
106  APSInt toAPSInt() const {
107  return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);
108  }
109  APSInt toAPSInt(unsigned NumBits) const {
110  if constexpr (Signed)
111  return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed);
112  else
113  return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed);
114  }
115  APValue toAPValue() const { return APValue(toAPSInt()); }
116 
118  return Integral<Bits, false>(*this);
119  }
120 
121  constexpr static unsigned bitWidth() { return Bits; }
122 
123  bool isZero() const { return !V; }
124 
125  bool isMin() const { return *this == min(bitWidth()); }
126 
127  bool isMinusOne() const { return Signed && V == ReprT(-1); }
128 
129  constexpr static bool isSigned() { return Signed; }
130 
131  bool isNegative() const { return V < ReprT(0); }
132  bool isPositive() const { return !isNegative(); }
133 
135  return Compare(V, RHS.V);
136  }
137 
138  unsigned countLeadingZeros() const {
139  return llvm::countLeadingZeros<ReprT>(V);
140  }
141 
142  Integral truncate(unsigned TruncBits) const {
143  if (TruncBits >= Bits)
144  return *this;
145  const ReprT BitMask = (ReprT(1) << ReprT(TruncBits)) - 1;
146  const ReprT SignBit = ReprT(1) << (TruncBits - 1);
147  const ReprT ExtMask = ~BitMask;
148  return Integral((V & BitMask) | (Signed && (V & SignBit) ? ExtMask : 0));
149  }
150 
151  void print(llvm::raw_ostream &OS) const { OS << V; }
152 
153  static Integral min(unsigned NumBits) {
154  return Integral(Min);
155  }
156  static Integral max(unsigned NumBits) {
157  return Integral(Max);
158  }
159 
160  template <typename ValT> static Integral from(ValT Value) {
161  if constexpr (std::is_integral<ValT>::value)
162  return Integral(Value);
163  else
164  return Integral::from(static_cast<Integral::ReprT>(Value));
165  }
166 
167  template <unsigned SrcBits, bool SrcSign>
168  static std::enable_if_t<SrcBits != 0, Integral>
170  return Integral(Value.V);
171  }
172 
173  template <bool SrcSign> static Integral from(Integral<0, SrcSign> Value) {
174  if constexpr (SrcSign)
175  return Integral(Value.V.getSExtValue());
176  else
177  return Integral(Value.V.getZExtValue());
178  }
179 
180  static Integral zero() { return from(0); }
181 
182  template <typename T> static Integral from(T Value, unsigned NumBits) {
183  return Integral(Value);
184  }
185 
186  static bool inRange(int64_t Value, unsigned NumBits) {
187  return CheckRange<ReprT, Min, Max>(Value);
188  }
189 
190  static bool increment(Integral A, Integral *R) {
191  return add(A, Integral(ReprT(1)), A.bitWidth(), R);
192  }
193 
194  static bool decrement(Integral A, Integral *R) {
195  return sub(A, Integral(ReprT(1)), A.bitWidth(), R);
196  }
197 
198  static bool add(Integral A, Integral B, unsigned OpBits, Integral *R) {
199  return CheckAddUB(A.V, B.V, R->V);
200  }
201 
202  static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R) {
203  return CheckSubUB(A.V, B.V, R->V);
204  }
205 
206  static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R) {
207  return CheckMulUB(A.V, B.V, R->V);
208  }
209 
210  static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R) {
211  *R = Integral(A.V % B.V);
212  return false;
213  }
214 
215  static bool div(Integral A, Integral B, unsigned OpBits, Integral *R) {
216  *R = Integral(A.V / B.V);
217  return false;
218  }
219 
220  static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R) {
221  *R = Integral(A.V & B.V);
222  return false;
223  }
224 
225  static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R) {
226  *R = Integral(A.V | B.V);
227  return false;
228  }
229 
230  static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R) {
231  *R = Integral(A.V ^ B.V);
232  return false;
233  }
234 
235  static bool neg(Integral A, Integral *R) {
236  *R = -A;
237  return false;
238  }
239 
240  static bool comp(Integral A, Integral *R) {
241  *R = Integral(~A.V);
242  return false;
243  }
244 
245 private:
246  template <typename T> static bool CheckAddUB(T A, T B, T &R) {
247  if constexpr (std::is_signed_v<T>) {
248  return llvm::AddOverflow<T>(A, B, R);
249  } else {
250  R = A + B;
251  return false;
252  }
253  }
254 
255  template <typename T> static bool CheckSubUB(T A, T B, T &R) {
256  if constexpr (std::is_signed_v<T>) {
257  return llvm::SubOverflow<T>(A, B, R);
258  } else {
259  R = A - B;
260  return false;
261  }
262  }
263 
264  template <typename T> static bool CheckMulUB(T A, T B, T &R) {
265  if constexpr (std::is_signed_v<T>) {
266  return llvm::MulOverflow<T>(A, B, R);
267  } else {
268  R = A * B;
269  return false;
270  }
271  }
272  template <typename T, T Min, T Max> static bool CheckRange(int64_t V) {
273  if constexpr (std::is_signed_v<T>) {
274  return Min <= V && V <= Max;
275  } else {
276  return V >= 0 && static_cast<uint64_t>(V) <= Max;
277  }
278  }
279 };
280 
281 template <unsigned Bits, bool Signed>
282 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Integral<Bits, Signed> I) {
283  I.print(OS);
284  return OS;
285 }
286 
287 } // namespace interp
288 } // namespace clang
289 
290 #endif
clang::interp::Integral
Wrapper around numeric types.
Definition: Integral.h:56
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::interp::Integral::operator-
Integral operator-() const
Definition: Integral.h:94
clang::interp::Integral::toUnsigned
Integral< Bits, false > toUnsigned() const
Definition: Integral.h:117
clang::interp::Integral::bitWidth
constexpr static unsigned bitWidth()
Definition: Integral.h:121
clang::interp::APInt
llvm::APInt APInt
Definition: Integral.h:27
clang::interp::Integral::bitXor
static bool bitXor(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:230
clang::interp::Integral::operator!=
bool operator!=(Integral RHS) const
Definition: Integral.h:88
clang::interp::Integral::toAPSInt
APSInt toAPSInt() const
Definition: Integral.h:106
clang::interp::Integral::from
static Integral from(Integral< 0, SrcSign > Value)
Definition: Integral.h:173
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::interp::Integral::compare
ComparisonCategoryResult compare(const Integral &RHS) const
Definition: Integral.h:134
clang::interp::Integral::operator>
bool operator>(unsigned RHS) const
Definition: Integral.h:90
clang::interp::Integral::operator~
Integral operator~() const
Definition: Integral.h:95
clang::interp::Integral::from
static Integral from(ValT Value)
Definition: Integral.h:160
clang::interp::Integral::mul
static bool mul(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:206
clang::interp::Integral::Integral
Integral(Integral< SrcBits, SrcSign > V)
Constructs an integral from another integral.
Definition: Integral.h:77
clang::interp::Integral::print
void print(llvm::raw_ostream &OS) const
Definition: Integral.h:151
clang::interp::Repr< 32, false >::Type
uint32_t Type
Definition: Integral.h:44
V
#define V(N, I)
Definition: ASTContext.h:3208
clang::interp::Compare
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Integral.h:32
clang::interp::Repr< 32, true >::Type
int32_t Type
Definition: Integral.h:48
min
__DEVICE__ int min(int __a, int __b)
Definition: __clang_cuda_math.h:197
clang::interp::Integral::isMin
bool isMin() const
Definition: Integral.h:125
clang::interp::Repr< 16, true >::Type
int16_t Type
Definition: Integral.h:47
clang::interp::Integral::operator>
bool operator>(Integral RHS) const
Definition: Integral.h:84
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::TypeSpecifierSign::Signed
@ Signed
clang::interp::Integral::rem
static bool rem(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:210
clang::interp::Repr< 8, true >::Type
int8_t Type
Definition: Integral.h:46
clang::interp::Integral::truncate
Integral truncate(unsigned TruncBits) const
Definition: Integral.h:142
clang::interp::Integral::neg
static bool neg(Integral A, Integral *R)
Definition: Integral.h:235
clang::interp::Integral::isPositive
bool isPositive() const
Definition: Integral.h:132
clang::interp::Integral::inRange
static bool inRange(int64_t Value, unsigned NumBits)
Definition: Integral.h:186
APValue.h
clang::interp::Repr< 64, false >::Type
uint64_t Type
Definition: Integral.h:45
clang::interp::Integral::isMinusOne
bool isMinusOne() const
Definition: Integral.h:127
clang::ComparisonCategoryResult::Less
@ Less
clang::interp::Integral::toAPSInt
APSInt toAPSInt(unsigned NumBits) const
Definition: Integral.h:109
clang::interp::Integral::Integral
Integral()
Zero-initializes an integral.
Definition: Integral.h:73
clang::interp::Integral::zero
static Integral zero()
Definition: Integral.h:180
clang::interp::CheckRange
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition: Interp.cpp:241
clang::interp::Integral::div
static bool div(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:215
clang::interp::Repr< 16, false >::Type
uint16_t Type
Definition: Integral.h:43
clang::interp::Integral::Integral
Integral(const APSInt &V)
Construct an integral from a value based on signedness.
Definition: Integral.h:80
clang::interp::Integral::bitOr
static bool bitOr(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:225
clang::interp::Integral::Integral
friend class Integral
Definition: Integral.h:58
false
#define false
Definition: stdbool.h:22
clang::interp::Integral::min
static Integral min(unsigned NumBits)
Definition: Integral.h:153
clang::interp::Repr< 64, true >::Type
int64_t Type
Definition: Integral.h:49
clang::interp::Integral::operator>=
bool operator>=(Integral RHS) const
Definition: Integral.h:86
clang::interp::Integral::increment
static bool increment(Integral A, Integral *R)
Definition: Integral.h:190
Value
Value
Definition: UninitializedValues.cpp:103
clang::interp::Integral::max
static Integral max(unsigned NumBits)
Definition: Integral.h:156
clang::interp::Integral::operator<=
bool operator<=(Integral RHS) const
Definition: Integral.h:85
clang::interp::Integral::isZero
bool isZero() const
Definition: Integral.h:123
clang::ComparisonCategoryResult::Equal
@ Equal
clang::interp::Integral::countLeadingZeros
unsigned countLeadingZeros() const
Definition: Integral.h:138
clang::interp::Integral::isNegative
bool isNegative() const
Definition: Integral.h:131
clang::interp::Repr
Definition: Integral.h:41
ComparisonCategories.h
clang
Definition: CalledOnceCheck.h:17
clang::interp::Integral::from
static std::enable_if_t< SrcBits !=0, Integral > from(Integral< SrcBits, SrcSign > Value)
Definition: Integral.h:169
hlsl::int64_t
long int64_t
Definition: hlsl_basic_types.h:26
clang::interp::Integral::isSigned
constexpr static bool isSigned()
Definition: Integral.h:129
clang::interp::Integral::decrement
static bool decrement(Integral A, Integral *R)
Definition: Integral.h:194
clang::ComparisonCategoryResult
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
Definition: ComparisonCategories.h:66
clang::interp::Repr< 8, false >::Type
uint8_t Type
Definition: Integral.h:42
clang::interp::Integral::toAPValue
APValue toAPValue() const
Definition: Integral.h:115
clang::interp::Integral::bitAnd
static bool bitAnd(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:220
unsigned
clang::interp::Integral::operator<
bool operator<(Integral RHS) const
Definition: Integral.h:83
clang::interp::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:156
clang::interp::Integral::from
static Integral from(T Value, unsigned NumBits)
Definition: Integral.h:182
clang::interp::APSInt
llvm::APSInt APSInt
Definition: Integral.h:28
clang::interp::Integral::comp
static bool comp(Integral A, Integral *R)
Definition: Integral.h:240
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
true
#define true
Definition: stdbool.h:21
clang::interp::Integral::add
static bool add(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:198
clang::ComparisonCategoryResult::Greater
@ Greater
clang::interp::Integral::sub
static bool sub(Integral A, Integral B, unsigned OpBits, Integral *R)
Definition: Integral.h:202
clang::interp::Integral::operator==
bool operator==(Integral RHS) const
Definition: Integral.h:87