xref: /freebsd/contrib/llvm-project/llvm/include/llvm/SandboxIR/Constant.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- Constant.h -----------------------------------------------*- 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 #ifndef LLVM_SANDBOXIR_CONSTANT_H
10 #define LLVM_SANDBOXIR_CONSTANT_H
11 
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/Constant.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/GlobalAlias.h"
16 #include "llvm/IR/GlobalIFunc.h"
17 #include "llvm/IR/GlobalObject.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/SandboxIR/Argument.h"
21 #include "llvm/SandboxIR/BasicBlock.h"
22 #include "llvm/SandboxIR/Context.h"
23 #include "llvm/SandboxIR/Type.h"
24 #include "llvm/SandboxIR/User.h"
25 #include "llvm/Support/Compiler.h"
26 
27 namespace llvm::sandboxir {
28 
29 class BasicBlock;
30 class Function;
31 
32 class Constant : public sandboxir::User {
33 protected:
Constant(llvm::Constant * C,sandboxir::Context & SBCtx)34   Constant(llvm::Constant *C, sandboxir::Context &SBCtx)
35       : sandboxir::User(ClassID::Constant, C, SBCtx) {}
Constant(ClassID ID,llvm::Constant * C,sandboxir::Context & SBCtx)36   Constant(ClassID ID, llvm::Constant *C, sandboxir::Context &SBCtx)
37       : sandboxir::User(ID, C, SBCtx) {}
38   friend class ConstantInt; // For constructor.
39   friend class Function;    // For constructor
40   friend class Context;     // For constructor.
getOperandUseInternal(unsigned OpIdx,bool Verify)41   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
42     return getOperandUseDefault(OpIdx, Verify);
43   }
44 
45 public:
46   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)47   static bool classof(const sandboxir::Value *From) {
48     switch (From->getSubclassID()) {
49 #define DEF_CONST(ID, CLASS) case ClassID::ID:
50 #include "llvm/SandboxIR/Values.def"
51       return true;
52     default:
53       return false;
54     }
55   }
getParent()56   sandboxir::Context &getParent() const { return getContext(); }
getUseOperandNo(const Use & Use)57   unsigned getUseOperandNo(const Use &Use) const override {
58     return getUseOperandNoDefault(Use);
59   }
60 #ifndef NDEBUG
verify()61   void verify() const override {
62     assert(isa<llvm::Constant>(Val) && "Expected Constant!");
63   }
64   void dumpOS(raw_ostream &OS) const override;
65 #endif
66 };
67 
68 // TODO: This should inherit from ConstantData.
69 class ConstantInt : public Constant {
ConstantInt(llvm::ConstantInt * C,Context & Ctx)70   ConstantInt(llvm::ConstantInt *C, Context &Ctx)
71       : Constant(ClassID::ConstantInt, C, Ctx) {}
72   friend class Context; // For constructor.
73 
getOperandUseInternal(unsigned OpIdx,bool Verify)74   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
75     llvm_unreachable("ConstantInt has no operands!");
76   }
77 
78 public:
79   LLVM_ABI static ConstantInt *getTrue(Context &Ctx);
80   LLVM_ABI static ConstantInt *getFalse(Context &Ctx);
81   LLVM_ABI static ConstantInt *getBool(Context &Ctx, bool V);
82   LLVM_ABI static Constant *getTrue(Type *Ty);
83   LLVM_ABI static Constant *getFalse(Type *Ty);
84   LLVM_ABI static Constant *getBool(Type *Ty, bool V);
85 
86   /// If Ty is a vector type, return a Constant with a splat of the given
87   /// value. Otherwise return a ConstantInt for the given value.
88   LLVM_ABI static ConstantInt *get(Type *Ty, uint64_t V, bool IsSigned = false);
89 
90   /// Return a ConstantInt with the specified integer value for the specified
91   /// type. If the type is wider than 64 bits, the value will be zero-extended
92   /// to fit the type, unless IsSigned is true, in which case the value will
93   /// be interpreted as a 64-bit signed integer and sign-extended to fit
94   /// the type.
95   /// Get a ConstantInt for a specific value.
96   LLVM_ABI static ConstantInt *get(IntegerType *Ty, uint64_t V,
97                                    bool IsSigned = false);
98 
99   /// Return a ConstantInt with the specified value for the specified type. The
100   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
101   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
102   /// signed value for the type Ty.
103   /// Get a ConstantInt for a specific signed value.
104   LLVM_ABI static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
105   LLVM_ABI static Constant *getSigned(Type *Ty, int64_t V);
106 
107   /// Return a ConstantInt with the specified value and an implied Type. The
108   /// type is the integer type that corresponds to the bit width of the value.
109   LLVM_ABI static ConstantInt *get(Context &Ctx, const APInt &V);
110 
111   /// Return a ConstantInt constructed from the string strStart with the given
112   /// radix.
113   LLVM_ABI static ConstantInt *get(IntegerType *Ty, StringRef Str,
114                                    uint8_t Radix);
115 
116   /// If Ty is a vector type, return a Constant with a splat of the given
117   /// value. Otherwise return a ConstantInt for the given value.
118   LLVM_ABI static Constant *get(Type *Ty, const APInt &V);
119 
120   /// Return the constant as an APInt value reference. This allows clients to
121   /// obtain a full-precision copy of the value.
122   /// Return the constant's value.
getValue()123   inline const APInt &getValue() const {
124     return cast<llvm::ConstantInt>(Val)->getValue();
125   }
126 
127   /// getBitWidth - Return the scalar bitwidth of this constant.
getBitWidth()128   unsigned getBitWidth() const {
129     return cast<llvm::ConstantInt>(Val)->getBitWidth();
130   }
131   /// Return the constant as a 64-bit unsigned integer value after it
132   /// has been zero extended as appropriate for the type of this constant. Note
133   /// that this method can assert if the value does not fit in 64 bits.
134   /// Return the zero extended value.
getZExtValue()135   inline uint64_t getZExtValue() const {
136     return cast<llvm::ConstantInt>(Val)->getZExtValue();
137   }
138 
139   /// Return the constant as a 64-bit integer value after it has been sign
140   /// extended as appropriate for the type of this constant. Note that
141   /// this method can assert if the value does not fit in 64 bits.
142   /// Return the sign extended value.
getSExtValue()143   inline int64_t getSExtValue() const {
144     return cast<llvm::ConstantInt>(Val)->getSExtValue();
145   }
146 
147   /// Return the constant as an llvm::MaybeAlign.
148   /// Note that this method can assert if the value does not fit in 64 bits or
149   /// is not a power of two.
getMaybeAlignValue()150   inline MaybeAlign getMaybeAlignValue() const {
151     return cast<llvm::ConstantInt>(Val)->getMaybeAlignValue();
152   }
153 
154   /// Return the constant as an llvm::Align, interpreting `0` as `Align(1)`.
155   /// Note that this method can assert if the value does not fit in 64 bits or
156   /// is not a power of two.
getAlignValue()157   inline Align getAlignValue() const {
158     return cast<llvm::ConstantInt>(Val)->getAlignValue();
159   }
160 
161   /// A helper method that can be used to determine if the constant contained
162   /// within is equal to a constant.  This only works for very small values,
163   /// because this is all that can be represented with all types.
164   /// Determine if this constant's value is same as an unsigned char.
equalsInt(uint64_t V)165   bool equalsInt(uint64_t V) const {
166     return cast<llvm::ConstantInt>(Val)->equalsInt(V);
167   }
168 
169   /// Variant of the getType() method to always return an IntegerType, which
170   /// reduces the amount of casting needed in parts of the compiler.
171   LLVM_ABI IntegerType *getIntegerType() const;
172 
173   /// This static method returns true if the type Ty is big enough to
174   /// represent the value V. This can be used to avoid having the get method
175   /// assert when V is larger than Ty can represent. Note that there are two
176   /// versions of this method, one for unsigned and one for signed integers.
177   /// Although ConstantInt canonicalizes everything to an unsigned integer,
178   /// the signed version avoids callers having to convert a signed quantity
179   /// to the appropriate unsigned type before calling the method.
180   /// @returns true if V is a valid value for type Ty
181   /// Determine if the value is in range for the given type.
182   LLVM_ABI static bool isValueValidForType(Type *Ty, uint64_t V);
183   LLVM_ABI static bool isValueValidForType(Type *Ty, int64_t V);
184 
isNegative()185   bool isNegative() const { return cast<llvm::ConstantInt>(Val)->isNegative(); }
186 
187   /// This is just a convenience method to make client code smaller for a
188   /// common code. It also correctly performs the comparison without the
189   /// potential for an assertion from getZExtValue().
isZero()190   bool isZero() const { return cast<llvm::ConstantInt>(Val)->isZero(); }
191 
192   /// This is just a convenience method to make client code smaller for a
193   /// common case. It also correctly performs the comparison without the
194   /// potential for an assertion from getZExtValue().
195   /// Determine if the value is one.
isOne()196   bool isOne() const { return cast<llvm::ConstantInt>(Val)->isOne(); }
197 
198   /// This function will return true iff every bit in this constant is set
199   /// to true.
200   /// @returns true iff this constant's bits are all set to true.
201   /// Determine if the value is all ones.
isMinusOne()202   bool isMinusOne() const { return cast<llvm::ConstantInt>(Val)->isMinusOne(); }
203 
204   /// This function will return true iff this constant represents the largest
205   /// value that may be represented by the constant's type.
206   /// @returns true iff this is the largest value that may be represented
207   /// by this type.
208   /// Determine if the value is maximal.
isMaxValue(bool IsSigned)209   bool isMaxValue(bool IsSigned) const {
210     return cast<llvm::ConstantInt>(Val)->isMaxValue(IsSigned);
211   }
212 
213   /// This function will return true iff this constant represents the smallest
214   /// value that may be represented by this constant's type.
215   /// @returns true if this is the smallest value that may be represented by
216   /// this type.
217   /// Determine if the value is minimal.
isMinValue(bool IsSigned)218   bool isMinValue(bool IsSigned) const {
219     return cast<llvm::ConstantInt>(Val)->isMinValue(IsSigned);
220   }
221 
222   /// This function will return true iff this constant represents a value with
223   /// active bits bigger than 64 bits or a value greater than the given uint64_t
224   /// value.
225   /// @returns true iff this constant is greater or equal to the given number.
226   /// Determine if the value is greater or equal to the given number.
uge(uint64_t Num)227   bool uge(uint64_t Num) const {
228     return cast<llvm::ConstantInt>(Val)->uge(Num);
229   }
230 
231   /// getLimitedValue - If the value is smaller than the specified limit,
232   /// return it, otherwise return the limit value.  This causes the value
233   /// to saturate to the limit.
234   /// @returns the min of the value of the constant and the specified value
235   /// Get the constant's value with a saturation limit
236   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
237     return cast<llvm::ConstantInt>(Val)->getLimitedValue(Limit);
238   }
239 
240   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)241   static bool classof(const sandboxir::Value *From) {
242     return From->getSubclassID() == ClassID::ConstantInt;
243   }
getUseOperandNo(const Use & Use)244   unsigned getUseOperandNo(const Use &Use) const override {
245     llvm_unreachable("ConstantInt has no operands!");
246   }
247 #ifndef NDEBUG
verify()248   void verify() const override {
249     assert(isa<llvm::ConstantInt>(Val) && "Expected a ConstantInst!");
250   }
dumpOS(raw_ostream & OS)251   void dumpOS(raw_ostream &OS) const override {
252     dumpCommonPrefix(OS);
253     dumpCommonSuffix(OS);
254   }
255 #endif
256 };
257 
258 // TODO: This should inherit from ConstantData.
259 class ConstantFP final : public Constant {
ConstantFP(llvm::ConstantFP * C,Context & Ctx)260   ConstantFP(llvm::ConstantFP *C, Context &Ctx)
261       : Constant(ClassID::ConstantFP, C, Ctx) {}
262   friend class Context; // For constructor.
263 
264 public:
265   /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
266   /// for the specified value in the specified type. This should only be used
267   /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
268   /// host double and as the target format.
269   LLVM_ABI static Constant *get(Type *Ty, double V);
270 
271   /// If Ty is a vector type, return a Constant with a splat of the given
272   /// value. Otherwise return a ConstantFP for the given value.
273   LLVM_ABI static Constant *get(Type *Ty, const APFloat &V);
274 
275   LLVM_ABI static Constant *get(Type *Ty, StringRef Str);
276 
277   LLVM_ABI static ConstantFP *get(const APFloat &V, Context &Ctx);
278 
279   LLVM_ABI static Constant *getNaN(Type *Ty, bool Negative = false,
280                                    uint64_t Payload = 0);
281   LLVM_ABI static Constant *getQNaN(Type *Ty, bool Negative = false,
282                                     APInt *Payload = nullptr);
283   LLVM_ABI static Constant *getSNaN(Type *Ty, bool Negative = false,
284                                     APInt *Payload = nullptr);
285   LLVM_ABI static Constant *getZero(Type *Ty, bool Negative = false);
286 
287   LLVM_ABI static Constant *getNegativeZero(Type *Ty);
288   LLVM_ABI static Constant *getInfinity(Type *Ty, bool Negative = false);
289 
290   /// Return true if Ty is big enough to represent V.
291   LLVM_ABI static bool isValueValidForType(Type *Ty, const APFloat &V);
292 
getValueAPF()293   inline const APFloat &getValueAPF() const {
294     return cast<llvm::ConstantFP>(Val)->getValueAPF();
295   }
getValue()296   inline const APFloat &getValue() const {
297     return cast<llvm::ConstantFP>(Val)->getValue();
298   }
299 
300   /// Return true if the value is positive or negative zero.
isZero()301   bool isZero() const { return cast<llvm::ConstantFP>(Val)->isZero(); }
302 
303   /// Return true if the sign bit is set.
isNegative()304   bool isNegative() const { return cast<llvm::ConstantFP>(Val)->isNegative(); }
305 
306   /// Return true if the value is infinity
isInfinity()307   bool isInfinity() const { return cast<llvm::ConstantFP>(Val)->isInfinity(); }
308 
309   /// Return true if the value is a NaN.
isNaN()310   bool isNaN() const { return cast<llvm::ConstantFP>(Val)->isNaN(); }
311 
312   /// We don't rely on operator== working on double values, as it returns true
313   /// for things that are clearly not equal, like -0.0 and 0.0.
314   /// As such, this method can be used to do an exact bit-for-bit comparison of
315   /// two floating point values.  The version with a double operand is retained
316   /// because it's so convenient to write isExactlyValue(2.0), but please use
317   /// it only for simple constants.
isExactlyValue(const APFloat & V)318   bool isExactlyValue(const APFloat &V) const {
319     return cast<llvm::ConstantFP>(Val)->isExactlyValue(V);
320   }
321 
isExactlyValue(double V)322   bool isExactlyValue(double V) const {
323     return cast<llvm::ConstantFP>(Val)->isExactlyValue(V);
324   }
325 
326   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)327   static bool classof(const sandboxir::Value *From) {
328     return From->getSubclassID() == ClassID::ConstantFP;
329   }
330 
331   // TODO: Better name: getOperandNo(const Use&). Should be private.
getUseOperandNo(const Use & Use)332   unsigned getUseOperandNo(const Use &Use) const final {
333     llvm_unreachable("ConstantFP has no operands!");
334   }
335 #ifndef NDEBUG
verify()336   void verify() const override {
337     assert(isa<llvm::ConstantFP>(Val) && "Expected a ConstantFP!");
338   }
dumpOS(raw_ostream & OS)339   void dumpOS(raw_ostream &OS) const override {
340     dumpCommonPrefix(OS);
341     dumpCommonSuffix(OS);
342   }
343 #endif
344 };
345 
346 /// Base class for aggregate constants (with operands).
347 class ConstantAggregate : public Constant {
348 protected:
ConstantAggregate(ClassID ID,llvm::Constant * C,Context & Ctx)349   ConstantAggregate(ClassID ID, llvm::Constant *C, Context &Ctx)
350       : Constant(ID, C, Ctx) {}
351 
352 public:
353   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)354   static bool classof(const sandboxir::Value *From) {
355     auto ID = From->getSubclassID();
356     return ID == ClassID::ConstantVector || ID == ClassID::ConstantStruct ||
357            ID == ClassID::ConstantArray;
358   }
359 };
360 
361 class ConstantArray final : public ConstantAggregate {
ConstantArray(llvm::ConstantArray * C,Context & Ctx)362   ConstantArray(llvm::ConstantArray *C, Context &Ctx)
363       : ConstantAggregate(ClassID::ConstantArray, C, Ctx) {}
364   friend class Context; // For constructor.
365 
366 public:
367   LLVM_ABI static Constant *get(ArrayType *T, ArrayRef<Constant *> V);
368   LLVM_ABI ArrayType *getType() const;
369 
370   // TODO: Missing functions: getType(), getTypeForElements(), getAnon(), get().
371 
372   /// For isa/dyn_cast.
classof(const Value * From)373   static bool classof(const Value *From) {
374     return From->getSubclassID() == ClassID::ConstantArray;
375   }
376 };
377 
378 class ConstantStruct final : public ConstantAggregate {
ConstantStruct(llvm::ConstantStruct * C,Context & Ctx)379   ConstantStruct(llvm::ConstantStruct *C, Context &Ctx)
380       : ConstantAggregate(ClassID::ConstantStruct, C, Ctx) {}
381   friend class Context; // For constructor.
382 
383 public:
384   LLVM_ABI static Constant *get(StructType *T, ArrayRef<Constant *> V);
385 
386   template <typename... Csts>
387   static std::enable_if_t<are_base_of<Constant, Csts...>::value, Constant *>
get(StructType * T,Csts * ...Vs)388   get(StructType *T, Csts *...Vs) {
389     return get(T, ArrayRef<Constant *>({Vs...}));
390   }
391   /// Return an anonymous struct that has the specified elements.
392   /// If the struct is possibly empty, then you must specify a context.
393   static Constant *getAnon(ArrayRef<Constant *> V, bool Packed = false) {
394     return get(getTypeForElements(V, Packed), V);
395   }
396   static Constant *getAnon(Context &Ctx, ArrayRef<Constant *> V,
397                            bool Packed = false) {
398     return get(getTypeForElements(Ctx, V, Packed), V);
399   }
400   /// This version of the method allows an empty list.
401   LLVM_ABI static StructType *
402   getTypeForElements(Context &Ctx, ArrayRef<Constant *> V, bool Packed = false);
403   /// Return an anonymous struct type to use for a constant with the specified
404   /// set of elements. The list must not be empty.
405   static StructType *getTypeForElements(ArrayRef<Constant *> V,
406                                         bool Packed = false) {
407     assert(!V.empty() &&
408            "ConstantStruct::getTypeForElements cannot be called on empty list");
409     return getTypeForElements(V[0]->getContext(), V, Packed);
410   }
411 
412   /// Specialization - reduce amount of casting.
getType()413   inline StructType *getType() const {
414     return cast<StructType>(Value::getType());
415   }
416 
417   /// For isa/dyn_cast.
classof(const Value * From)418   static bool classof(const Value *From) {
419     return From->getSubclassID() == ClassID::ConstantStruct;
420   }
421 };
422 
423 class ConstantVector final : public ConstantAggregate {
ConstantVector(llvm::ConstantVector * C,Context & Ctx)424   ConstantVector(llvm::ConstantVector *C, Context &Ctx)
425       : ConstantAggregate(ClassID::ConstantVector, C, Ctx) {}
426   friend class Context; // For constructor.
427 
428 public:
429   LLVM_ABI static Constant *get(ArrayRef<Constant *> V);
430   /// Return a ConstantVector with the specified constant in each element.
431   /// Note that this might not return an instance of ConstantVector
432   LLVM_ABI static Constant *getSplat(ElementCount EC, Constant *Elt);
433   /// Specialize the getType() method to always return a FixedVectorType,
434   /// which reduces the amount of casting needed in parts of the compiler.
getType()435   inline FixedVectorType *getType() const {
436     return cast<FixedVectorType>(Value::getType());
437   }
438   /// If all elements of the vector constant have the same value, return that
439   /// value. Otherwise, return nullptr. Ignore poison elements by setting
440   /// AllowPoison to true.
441   LLVM_ABI Constant *getSplatValue(bool AllowPoison = false) const;
442 
443   /// For isa/dyn_cast.
classof(const Value * From)444   static bool classof(const Value *From) {
445     return From->getSubclassID() == ClassID::ConstantVector;
446   }
447 };
448 
449 // TODO: Inherit from ConstantData.
450 class ConstantAggregateZero final : public Constant {
ConstantAggregateZero(llvm::ConstantAggregateZero * C,Context & Ctx)451   ConstantAggregateZero(llvm::ConstantAggregateZero *C, Context &Ctx)
452       : Constant(ClassID::ConstantAggregateZero, C, Ctx) {}
453   friend class Context; // For constructor.
454 
455 public:
456   LLVM_ABI static ConstantAggregateZero *get(Type *Ty);
457   /// If this CAZ has array or vector type, return a zero with the right element
458   /// type.
459   LLVM_ABI Constant *getSequentialElement() const;
460   /// If this CAZ has struct type, return a zero with the right element type for
461   /// the specified element.
462   LLVM_ABI Constant *getStructElement(unsigned Elt) const;
463   /// Return a zero of the right value for the specified GEP index if we can,
464   /// otherwise return null (e.g. if C is a ConstantExpr).
465   LLVM_ABI Constant *getElementValue(Constant *C) const;
466   /// Return a zero of the right value for the specified GEP index.
467   LLVM_ABI Constant *getElementValue(unsigned Idx) const;
468   /// Return the number of elements in the array, vector, or struct.
getElementCount()469   ElementCount getElementCount() const {
470     return cast<llvm::ConstantAggregateZero>(Val)->getElementCount();
471   }
472 
473   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)474   static bool classof(const sandboxir::Value *From) {
475     return From->getSubclassID() == ClassID::ConstantAggregateZero;
476   }
getUseOperandNo(const Use & Use)477   unsigned getUseOperandNo(const Use &Use) const final {
478     llvm_unreachable("ConstantAggregateZero has no operands!");
479   }
480 #ifndef NDEBUG
verify()481   void verify() const override {
482     assert(isa<llvm::ConstantAggregateZero>(Val) && "Expected a CAZ!");
483   }
dumpOS(raw_ostream & OS)484   void dumpOS(raw_ostream &OS) const override {
485     dumpCommonPrefix(OS);
486     dumpCommonSuffix(OS);
487   }
488 #endif
489 };
490 
491 /// ConstantDataSequential - A vector or array constant whose element type is a
492 /// simple 1/2/4/8-byte integer or half/bfloat/float/double, and whose elements
493 /// are just simple data values (i.e. ConstantInt/ConstantFP).  This Constant
494 /// node has no operands because it stores all of the elements of the constant
495 /// as densely packed data, instead of as Value*'s.
496 ///
497 /// This is the common base class of ConstantDataArray and ConstantDataVector.
498 class ConstantDataSequential : public Constant {
499 protected:
ConstantDataSequential(ClassID ID,llvm::ConstantDataSequential * C,Context & Ctx)500   ConstantDataSequential(ClassID ID, llvm::ConstantDataSequential *C,
501                          Context &Ctx)
502       : Constant(ID, C, Ctx) {}
503 
504 public:
505   /// Return true if a ConstantDataSequential can be formed with a vector or
506   /// array of the specified element type.
507   /// ConstantDataArray only works with normal float and int types that are
508   /// stored densely in memory, not with things like i42 or x86_f80.
isElementTypeCompatible(Type * Ty)509   static bool isElementTypeCompatible(Type *Ty) {
510     return llvm::ConstantDataSequential::isElementTypeCompatible(Ty->LLVMTy);
511   }
512   /// If this is a sequential container of integers (of any size), return the
513   /// specified element in the low bits of a uint64_t.
getElementAsInteger(unsigned ElmIdx)514   uint64_t getElementAsInteger(unsigned ElmIdx) const {
515     return cast<llvm::ConstantDataSequential>(Val)->getElementAsInteger(ElmIdx);
516   }
517   /// If this is a sequential container of integers (of any size), return the
518   /// specified element as an APInt.
getElementAsAPInt(unsigned ElmIdx)519   APInt getElementAsAPInt(unsigned ElmIdx) const {
520     return cast<llvm::ConstantDataSequential>(Val)->getElementAsAPInt(ElmIdx);
521   }
522   /// If this is a sequential container of floating point type, return the
523   /// specified element as an APFloat.
getElementAsAPFloat(unsigned ElmIdx)524   APFloat getElementAsAPFloat(unsigned ElmIdx) const {
525     return cast<llvm::ConstantDataSequential>(Val)->getElementAsAPFloat(ElmIdx);
526   }
527   /// If this is an sequential container of floats, return the specified element
528   /// as a float.
getElementAsFloat(unsigned ElmIdx)529   float getElementAsFloat(unsigned ElmIdx) const {
530     return cast<llvm::ConstantDataSequential>(Val)->getElementAsFloat(ElmIdx);
531   }
532   /// If this is an sequential container of doubles, return the specified
533   /// element as a double.
getElementAsDouble(unsigned ElmIdx)534   double getElementAsDouble(unsigned ElmIdx) const {
535     return cast<llvm::ConstantDataSequential>(Val)->getElementAsDouble(ElmIdx);
536   }
537   /// Return a Constant for a specified index's element.
538   /// Note that this has to compute a new constant to return, so it isn't as
539   /// efficient as getElementAsInteger/Float/Double.
getElementAsConstant(unsigned ElmIdx)540   Constant *getElementAsConstant(unsigned ElmIdx) const {
541     return Ctx.getOrCreateConstant(
542         cast<llvm::ConstantDataSequential>(Val)->getElementAsConstant(ElmIdx));
543   }
544   /// Return the element type of the array/vector.
getElementType()545   Type *getElementType() const {
546     return Ctx.getType(
547         cast<llvm::ConstantDataSequential>(Val)->getElementType());
548   }
549   /// Return the number of elements in the array or vector.
getNumElements()550   unsigned getNumElements() const {
551     return cast<llvm::ConstantDataSequential>(Val)->getNumElements();
552   }
553   /// Return the size (in bytes) of each element in the array/vector.
554   /// The size of the elements is known to be a multiple of one byte.
getElementByteSize()555   uint64_t getElementByteSize() const {
556     return cast<llvm::ConstantDataSequential>(Val)->getElementByteSize();
557   }
558   /// This method returns true if this is an array of \p CharSize integers.
559   bool isString(unsigned CharSize = 8) const {
560     return cast<llvm::ConstantDataSequential>(Val)->isString(CharSize);
561   }
562   /// This method returns true if the array "isString", ends with a null byte,
563   /// and does not contains any other null bytes.
isCString()564   bool isCString() const {
565     return cast<llvm::ConstantDataSequential>(Val)->isCString();
566   }
567   /// If this array is isString(), then this method returns the array as a
568   /// StringRef. Otherwise, it asserts out.
getAsString()569   StringRef getAsString() const {
570     return cast<llvm::ConstantDataSequential>(Val)->getAsString();
571   }
572   /// If this array is isCString(), then this method returns the array (without
573   /// the trailing null byte) as a StringRef. Otherwise, it asserts out.
getAsCString()574   StringRef getAsCString() const {
575     return cast<llvm::ConstantDataSequential>(Val)->getAsCString();
576   }
577   /// Return the raw, underlying, bytes of this data. Note that this is an
578   /// extremely tricky thing to work with, as it exposes the host endianness of
579   /// the data elements.
getRawDataValues()580   StringRef getRawDataValues() const {
581     return cast<llvm::ConstantDataSequential>(Val)->getRawDataValues();
582   }
583 
classof(const Value * From)584   static bool classof(const Value *From) {
585     return From->getSubclassID() == ClassID::ConstantDataArray ||
586            From->getSubclassID() == ClassID::ConstantDataVector;
587   }
588 };
589 
590 class ConstantDataArray final : public ConstantDataSequential {
ConstantDataArray(llvm::ConstantDataArray * C,Context & Ctx)591   ConstantDataArray(llvm::ConstantDataArray *C, Context &Ctx)
592       : ConstantDataSequential(ClassID::ConstantDataArray, C, Ctx) {}
593   friend class Context;
594 
595 public:
classof(const Value * From)596   static bool classof(const Value *From) {
597     return From->getSubclassID() == ClassID::ConstantDataArray;
598   }
599   /// get() constructor - Return a constant with array type with an element
600   /// count and element type matching the ArrayRef passed in.  Note that this
601   /// can return a ConstantAggregateZero object.
602   template <typename ElementTy>
get(Context & Ctx,ArrayRef<ElementTy> Elts)603   static Constant *get(Context &Ctx, ArrayRef<ElementTy> Elts) {
604     auto *NewLLVMC = llvm::ConstantDataArray::get(Ctx.LLVMCtx, Elts);
605     return Ctx.getOrCreateConstant(NewLLVMC);
606   }
607 
608   /// get() constructor - ArrayTy needs to be compatible with
609   /// ArrayRef<ElementTy>.
610   template <typename ArrayTy>
get(Context & Ctx,ArrayTy & Elts)611   static Constant *get(Context &Ctx, ArrayTy &Elts) {
612     return ConstantDataArray::get(Ctx, ArrayRef(Elts));
613   }
614 
615   /// getRaw() constructor - Return a constant with array type with an element
616   /// count and element type matching the NumElements and ElementTy parameters
617   /// passed in. Note that this can return a ConstantAggregateZero object.
618   /// ElementTy must be one of i8/i16/i32/i64/half/bfloat/float/double. Data is
619   /// the buffer containing the elements. Be careful to make sure Data uses the
620   /// right endianness, the buffer will be used as-is.
getRaw(StringRef Data,uint64_t NumElements,Type * ElementTy)621   static Constant *getRaw(StringRef Data, uint64_t NumElements,
622                           Type *ElementTy) {
623     auto *LLVMC =
624         llvm::ConstantDataArray::getRaw(Data, NumElements, ElementTy->LLVMTy);
625     return ElementTy->getContext().getOrCreateConstant(LLVMC);
626   }
627   /// getFP() constructors - Return a constant of array type with a float
628   /// element type taken from argument `ElementType', and count taken from
629   /// argument `Elts'.  The amount of bits of the contained type must match the
630   /// number of bits of the type contained in the passed in ArrayRef.
631   /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
632   /// that this can return a ConstantAggregateZero object.
getFP(Type * ElementType,ArrayRef<uint16_t> Elts)633   static Constant *getFP(Type *ElementType, ArrayRef<uint16_t> Elts) {
634     auto *LLVMC = llvm::ConstantDataArray::getFP(ElementType->LLVMTy, Elts);
635     return ElementType->getContext().getOrCreateConstant(LLVMC);
636   }
getFP(Type * ElementType,ArrayRef<uint32_t> Elts)637   static Constant *getFP(Type *ElementType, ArrayRef<uint32_t> Elts) {
638     auto *LLVMC = llvm::ConstantDataArray::getFP(ElementType->LLVMTy, Elts);
639     return ElementType->getContext().getOrCreateConstant(LLVMC);
640   }
getFP(Type * ElementType,ArrayRef<uint64_t> Elts)641   static Constant *getFP(Type *ElementType, ArrayRef<uint64_t> Elts) {
642     auto *LLVMC = llvm::ConstantDataArray::getFP(ElementType->LLVMTy, Elts);
643     return ElementType->getContext().getOrCreateConstant(LLVMC);
644   }
645   /// This method constructs a CDS and initializes it with a text string.
646   /// The default behavior (AddNull==true) causes a null terminator to
647   /// be placed at the end of the array (increasing the length of the string by
648   /// one more than the StringRef would normally indicate.  Pass AddNull=false
649   /// to disable this behavior.
650   static Constant *getString(Context &Ctx, StringRef Initializer,
651                              bool AddNull = true) {
652     auto *LLVMC =
653         llvm::ConstantDataArray::getString(Ctx.LLVMCtx, Initializer, AddNull);
654     return Ctx.getOrCreateConstant(LLVMC);
655   }
656 
657   /// Specialize the getType() method to always return an ArrayType,
658   /// which reduces the amount of casting needed in parts of the compiler.
getType()659   inline ArrayType *getType() const {
660     return cast<ArrayType>(Value::getType());
661   }
662 };
663 
664 /// A vector constant whose element type is a simple 1/2/4/8-byte integer or
665 /// float/double, and whose elements are just simple data values
666 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
667 /// stores all of the elements of the constant as densely packed data, instead
668 /// of as Value*'s.
669 class ConstantDataVector final : public ConstantDataSequential {
ConstantDataVector(llvm::ConstantDataVector * C,Context & Ctx)670   ConstantDataVector(llvm::ConstantDataVector *C, Context &Ctx)
671       : ConstantDataSequential(ClassID::ConstantDataVector, C, Ctx) {}
672   friend class Context;
673 
674 public:
675   /// Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const Value * From)676   static bool classof(const Value *From) {
677     return From->getSubclassID() == ClassID::ConstantDataVector;
678   }
679   /// get() constructors - Return a constant with vector type with an element
680   /// count and element type matching the ArrayRef passed in.  Note that this
681   /// can return a ConstantAggregateZero object.
get(Context & Ctx,ArrayRef<uint8_t> Elts)682   static Constant *get(Context &Ctx, ArrayRef<uint8_t> Elts) {
683     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
684     return Ctx.getOrCreateConstant(NewLLVMC);
685   }
get(Context & Ctx,ArrayRef<uint16_t> Elts)686   static Constant *get(Context &Ctx, ArrayRef<uint16_t> Elts) {
687     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
688     return Ctx.getOrCreateConstant(NewLLVMC);
689   }
get(Context & Ctx,ArrayRef<uint32_t> Elts)690   static Constant *get(Context &Ctx, ArrayRef<uint32_t> Elts) {
691     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
692     return Ctx.getOrCreateConstant(NewLLVMC);
693   }
get(Context & Ctx,ArrayRef<uint64_t> Elts)694   static Constant *get(Context &Ctx, ArrayRef<uint64_t> Elts) {
695     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
696     return Ctx.getOrCreateConstant(NewLLVMC);
697   }
get(Context & Ctx,ArrayRef<float> Elts)698   static Constant *get(Context &Ctx, ArrayRef<float> Elts) {
699     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
700     return Ctx.getOrCreateConstant(NewLLVMC);
701   }
get(Context & Ctx,ArrayRef<double> Elts)702   static Constant *get(Context &Ctx, ArrayRef<double> Elts) {
703     auto *NewLLVMC = llvm::ConstantDataVector::get(Ctx.LLVMCtx, Elts);
704     return Ctx.getOrCreateConstant(NewLLVMC);
705   }
706 
707   /// getRaw() constructor - Return a constant with vector type with an element
708   /// count and element type matching the NumElements and ElementTy parameters
709   /// passed in. Note that this can return a ConstantAggregateZero object.
710   /// ElementTy must be one of i8/i16/i32/i64/half/bfloat/float/double. Data is
711   /// the buffer containing the elements. Be careful to make sure Data uses the
712   /// right endianness, the buffer will be used as-is.
getRaw(StringRef Data,uint64_t NumElements,Type * ElementTy)713   static Constant *getRaw(StringRef Data, uint64_t NumElements,
714                           Type *ElementTy) {
715     auto *NewLLVMC =
716         llvm::ConstantDataVector::getRaw(Data, NumElements, ElementTy->LLVMTy);
717     return ElementTy->getContext().getOrCreateConstant(NewLLVMC);
718   }
719   /// getFP() constructors - Return a constant of vector type with a float
720   /// element type taken from argument `ElementType', and count taken from
721   /// argument `Elts'.  The amount of bits of the contained type must match the
722   /// number of bits of the type contained in the passed in ArrayRef.
723   /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
724   /// that this can return a ConstantAggregateZero object.
getFP(Type * ElementType,ArrayRef<uint16_t> Elts)725   static Constant *getFP(Type *ElementType, ArrayRef<uint16_t> Elts) {
726     auto *NewLLVMC = llvm::ConstantDataVector::getFP(ElementType->LLVMTy, Elts);
727     return ElementType->getContext().getOrCreateConstant(NewLLVMC);
728   }
getFP(Type * ElementType,ArrayRef<uint32_t> Elts)729   static Constant *getFP(Type *ElementType, ArrayRef<uint32_t> Elts) {
730     auto *NewLLVMC = llvm::ConstantDataVector::getFP(ElementType->LLVMTy, Elts);
731     return ElementType->getContext().getOrCreateConstant(NewLLVMC);
732   }
getFP(Type * ElementType,ArrayRef<uint64_t> Elts)733   static Constant *getFP(Type *ElementType, ArrayRef<uint64_t> Elts) {
734     auto *NewLLVMC = llvm::ConstantDataVector::getFP(ElementType->LLVMTy, Elts);
735     return ElementType->getContext().getOrCreateConstant(NewLLVMC);
736   }
737 
738   /// Return a ConstantVector with the specified constant in each element.
739   /// The specified constant has to be a of a compatible type (i8/i16/
740   /// i32/i64/half/bfloat/float/double) and must be a ConstantFP or ConstantInt.
getSplat(unsigned NumElts,Constant * Elt)741   static Constant *getSplat(unsigned NumElts, Constant *Elt) {
742     auto *NewLLVMC = llvm::ConstantDataVector::getSplat(
743         NumElts, cast<llvm::Constant>(Elt->Val));
744     return Elt->getContext().getOrCreateConstant(NewLLVMC);
745   }
746 
747   /// Returns true if this is a splat constant, meaning that all elements have
748   /// the same value.
isSplat()749   bool isSplat() const {
750     return cast<llvm::ConstantDataVector>(Val)->isSplat();
751   }
752 
753   /// If this is a splat constant, meaning that all of the elements have the
754   /// same value, return that value. Otherwise return NULL.
getSplatValue()755   Constant *getSplatValue() const {
756     return Ctx.getOrCreateConstant(
757         cast<llvm::ConstantDataVector>(Val)->getSplatValue());
758   }
759 
760   /// Specialize the getType() method to always return a FixedVectorType,
761   /// which reduces the amount of casting needed in parts of the compiler.
getType()762   inline FixedVectorType *getType() const {
763     return cast<FixedVectorType>(Value::getType());
764   }
765 };
766 
767 // TODO: Inherit from ConstantData.
768 class ConstantPointerNull final : public Constant {
ConstantPointerNull(llvm::ConstantPointerNull * C,Context & Ctx)769   ConstantPointerNull(llvm::ConstantPointerNull *C, Context &Ctx)
770       : Constant(ClassID::ConstantPointerNull, C, Ctx) {}
771   friend class Context; // For constructor.
772 
773 public:
774   LLVM_ABI static ConstantPointerNull *get(PointerType *Ty);
775 
776   LLVM_ABI PointerType *getType() const;
777 
778   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)779   static bool classof(const sandboxir::Value *From) {
780     return From->getSubclassID() == ClassID::ConstantPointerNull;
781   }
getUseOperandNo(const Use & Use)782   unsigned getUseOperandNo(const Use &Use) const final {
783     llvm_unreachable("ConstantPointerNull has no operands!");
784   }
785 #ifndef NDEBUG
verify()786   void verify() const override {
787     assert(isa<llvm::ConstantPointerNull>(Val) && "Expected a CPNull!");
788   }
dumpOS(raw_ostream & OS)789   void dumpOS(raw_ostream &OS) const override {
790     dumpCommonPrefix(OS);
791     dumpCommonSuffix(OS);
792   }
793 #endif
794 };
795 
796 // TODO: Inherit from ConstantData.
797 class UndefValue : public Constant {
798 protected:
UndefValue(llvm::UndefValue * C,Context & Ctx)799   UndefValue(llvm::UndefValue *C, Context &Ctx)
800       : Constant(ClassID::UndefValue, C, Ctx) {}
UndefValue(ClassID ID,llvm::Constant * C,Context & Ctx)801   UndefValue(ClassID ID, llvm::Constant *C, Context &Ctx)
802       : Constant(ID, C, Ctx) {}
803   friend class Context; // For constructor.
804 
805 public:
806   /// Static factory methods - Return an 'undef' object of the specified type.
807   LLVM_ABI static UndefValue *get(Type *T);
808 
809   /// If this Undef has array or vector type, return a undef with the right
810   /// element type.
811   LLVM_ABI UndefValue *getSequentialElement() const;
812 
813   /// If this undef has struct type, return a undef with the right element type
814   /// for the specified element.
815   LLVM_ABI UndefValue *getStructElement(unsigned Elt) const;
816 
817   /// Return an undef of the right value for the specified GEP index if we can,
818   /// otherwise return null (e.g. if C is a ConstantExpr).
819   LLVM_ABI UndefValue *getElementValue(Constant *C) const;
820 
821   /// Return an undef of the right value for the specified GEP index.
822   LLVM_ABI UndefValue *getElementValue(unsigned Idx) const;
823 
824   /// Return the number of elements in the array, vector, or struct.
getNumElements()825   unsigned getNumElements() const {
826     return cast<llvm::UndefValue>(Val)->getNumElements();
827   }
828 
829   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)830   static bool classof(const sandboxir::Value *From) {
831     return From->getSubclassID() == ClassID::UndefValue ||
832            From->getSubclassID() == ClassID::PoisonValue;
833   }
getUseOperandNo(const Use & Use)834   unsigned getUseOperandNo(const Use &Use) const final {
835     llvm_unreachable("UndefValue has no operands!");
836   }
837 #ifndef NDEBUG
verify()838   void verify() const override {
839     assert(isa<llvm::UndefValue>(Val) && "Expected an UndefValue!");
840   }
dumpOS(raw_ostream & OS)841   void dumpOS(raw_ostream &OS) const override {
842     dumpCommonPrefix(OS);
843     dumpCommonSuffix(OS);
844   }
845 #endif
846 };
847 
848 class PoisonValue final : public UndefValue {
PoisonValue(llvm::PoisonValue * C,Context & Ctx)849   PoisonValue(llvm::PoisonValue *C, Context &Ctx)
850       : UndefValue(ClassID::PoisonValue, C, Ctx) {}
851   friend class Context; // For constructor.
852 
853 public:
854   /// Static factory methods - Return an 'poison' object of the specified type.
855   LLVM_ABI static PoisonValue *get(Type *T);
856 
857   /// If this poison has array or vector type, return a poison with the right
858   /// element type.
859   LLVM_ABI PoisonValue *getSequentialElement() const;
860 
861   /// If this poison has struct type, return a poison with the right element
862   /// type for the specified element.
863   LLVM_ABI PoisonValue *getStructElement(unsigned Elt) const;
864 
865   /// Return an poison of the right value for the specified GEP index if we can,
866   /// otherwise return null (e.g. if C is a ConstantExpr).
867   LLVM_ABI PoisonValue *getElementValue(Constant *C) const;
868 
869   /// Return an poison of the right value for the specified GEP index.
870   LLVM_ABI PoisonValue *getElementValue(unsigned Idx) const;
871 
872   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)873   static bool classof(const sandboxir::Value *From) {
874     return From->getSubclassID() == ClassID::PoisonValue;
875   }
876 #ifndef NDEBUG
verify()877   void verify() const override {
878     assert(isa<llvm::PoisonValue>(Val) && "Expected a PoisonValue!");
879   }
dumpOS(raw_ostream & OS)880   void dumpOS(raw_ostream &OS) const override {
881     dumpCommonPrefix(OS);
882     dumpCommonSuffix(OS);
883   }
884 #endif
885 };
886 
887 class GlobalValue : public Constant {
888 protected:
GlobalValue(ClassID ID,llvm::GlobalValue * C,Context & Ctx)889   GlobalValue(ClassID ID, llvm::GlobalValue *C, Context &Ctx)
890       : Constant(ID, C, Ctx) {}
891   friend class Context; // For constructor.
892 
893 public:
894   using LinkageTypes = llvm::GlobalValue::LinkageTypes;
895   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)896   static bool classof(const sandboxir::Value *From) {
897     switch (From->getSubclassID()) {
898     case ClassID::Function:
899     case ClassID::GlobalVariable:
900     case ClassID::GlobalAlias:
901     case ClassID::GlobalIFunc:
902       return true;
903     default:
904       return false;
905     }
906   }
907 
getAddressSpace()908   unsigned getAddressSpace() const {
909     return cast<llvm::GlobalValue>(Val)->getAddressSpace();
910   }
hasGlobalUnnamedAddr()911   bool hasGlobalUnnamedAddr() const {
912     return cast<llvm::GlobalValue>(Val)->hasGlobalUnnamedAddr();
913   }
914 
915   /// Returns true if this value's address is not significant in this module.
916   /// This attribute is intended to be used only by the code generator and LTO
917   /// to allow the linker to decide whether the global needs to be in the symbol
918   /// table. It should probably not be used in optimizations, as the value may
919   /// have uses outside the module; use hasGlobalUnnamedAddr() instead.
hasAtLeastLocalUnnamedAddr()920   bool hasAtLeastLocalUnnamedAddr() const {
921     return cast<llvm::GlobalValue>(Val)->hasAtLeastLocalUnnamedAddr();
922   }
923 
924   using UnnamedAddr = llvm::GlobalValue::UnnamedAddr;
925 
getUnnamedAddr()926   UnnamedAddr getUnnamedAddr() const {
927     return cast<llvm::GlobalValue>(Val)->getUnnamedAddr();
928   }
929   LLVM_ABI void setUnnamedAddr(UnnamedAddr V);
930 
getMinUnnamedAddr(UnnamedAddr A,UnnamedAddr B)931   static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) {
932     return llvm::GlobalValue::getMinUnnamedAddr(A, B);
933   }
934 
hasComdat()935   bool hasComdat() const { return cast<llvm::GlobalValue>(Val)->hasComdat(); }
936 
937   // TODO: We need a SandboxIR Comdat if we want to implement getComdat().
938   using VisibilityTypes = llvm::GlobalValue::VisibilityTypes;
getVisibility()939   VisibilityTypes getVisibility() const {
940     return cast<llvm::GlobalValue>(Val)->getVisibility();
941   }
hasDefaultVisibility()942   bool hasDefaultVisibility() const {
943     return cast<llvm::GlobalValue>(Val)->hasDefaultVisibility();
944   }
hasHiddenVisibility()945   bool hasHiddenVisibility() const {
946     return cast<llvm::GlobalValue>(Val)->hasHiddenVisibility();
947   }
hasProtectedVisibility()948   bool hasProtectedVisibility() const {
949     return cast<llvm::GlobalValue>(Val)->hasProtectedVisibility();
950   }
951   LLVM_ABI void setVisibility(VisibilityTypes V);
952 
953   // TODO: Add missing functions.
954 };
955 
956 class GlobalObject : public GlobalValue {
957 protected:
GlobalObject(ClassID ID,llvm::GlobalObject * C,Context & Ctx)958   GlobalObject(ClassID ID, llvm::GlobalObject *C, Context &Ctx)
959       : GlobalValue(ID, C, Ctx) {}
960   friend class Context; // For constructor.
getOperandUseInternal(unsigned OpIdx,bool Verify)961   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
962     return getOperandUseDefault(OpIdx, Verify);
963   }
964 
965 public:
getUseOperandNo(const Use & Use)966   unsigned getUseOperandNo(const Use &Use) const final {
967     return getUseOperandNoDefault(Use);
968   }
969   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)970   static bool classof(const sandboxir::Value *From) {
971     switch (From->getSubclassID()) {
972     case ClassID::Function:
973     case ClassID::GlobalVariable:
974     case ClassID::GlobalIFunc:
975       return true;
976     default:
977       return false;
978     }
979   }
980 
981   /// Check if this global has a custom object file section.
982   ///
983   /// This is more efficient than calling getSection() and checking for an empty
984   /// string.
hasSection()985   bool hasSection() const {
986     return cast<llvm::GlobalObject>(Val)->hasSection();
987   }
988 
989   /// Get the custom section of this global if it has one.
990   ///
991   /// If this global does not have a custom section, this will be empty and the
992   /// default object file section (.text, .data, etc) will be used.
getSection()993   StringRef getSection() const {
994     return cast<llvm::GlobalObject>(Val)->getSection();
995   }
996 
997   /// Change the section for this global.
998   ///
999   /// Setting the section to the empty string tells LLVM to choose an
1000   /// appropriate default object file section.
1001   LLVM_ABI void setSection(StringRef S);
1002 
hasComdat()1003   bool hasComdat() const { return cast<llvm::GlobalObject>(Val)->hasComdat(); }
1004 
1005   // TODO: implement get/setComdat(), etc. once we have a sandboxir::Comdat.
1006 
1007   // TODO: We currently don't support Metadata in sandboxir so all
1008   // Metadata-related functions are missing.
1009 
1010   using VCallVisibility = llvm::GlobalObject::VCallVisibility;
1011 
getVCallVisibility()1012   VCallVisibility getVCallVisibility() const {
1013     return cast<llvm::GlobalObject>(Val)->getVCallVisibility();
1014   }
1015 
1016   /// Returns true if the alignment of the value can be unilaterally
1017   /// increased.
1018   ///
1019   /// Note that for functions this is the alignment of the code, not the
1020   /// alignment of a function pointer.
canIncreaseAlignment()1021   bool canIncreaseAlignment() const {
1022     return cast<llvm::GlobalObject>(Val)->canIncreaseAlignment();
1023   }
1024 };
1025 
1026 /// Provides API functions, like getIterator() and getReverseIterator() to
1027 /// GlobalIFunc, Function, GlobalVariable and GlobalAlias. In LLVM IR these are
1028 /// provided by ilist_node.
1029 template <typename GlobalT, typename LLVMGlobalT, typename ParentT,
1030           typename LLVMParentT>
1031 class GlobalWithNodeAPI : public ParentT {
1032   /// Helper for mapped_iterator.
1033   struct LLVMGVToGV {
1034     Context &Ctx;
LLVMGVToGVLLVMGVToGV1035     LLVMGVToGV(Context &Ctx) : Ctx(Ctx) {}
1036     LLVM_ABI GlobalT &operator()(LLVMGlobalT &LLVMGV) const;
1037   };
1038 
1039 public:
GlobalWithNodeAPI(Value::ClassID ID,LLVMParentT * C,Context & Ctx)1040   GlobalWithNodeAPI(Value::ClassID ID, LLVMParentT *C, Context &Ctx)
1041       : ParentT(ID, C, Ctx) {}
1042 
getParent()1043   Module *getParent() const {
1044     llvm::Module *LLVMM = cast<LLVMGlobalT>(this->Val)->getParent();
1045     return this->Ctx.getModule(LLVMM);
1046   }
1047 
1048   using iterator = mapped_iterator<
1049       decltype(static_cast<LLVMGlobalT *>(nullptr)->getIterator()), LLVMGVToGV>;
1050   using reverse_iterator = mapped_iterator<
1051       decltype(static_cast<LLVMGlobalT *>(nullptr)->getReverseIterator()),
1052       LLVMGVToGV>;
getIterator()1053   iterator getIterator() const {
1054     auto *LLVMGV = cast<LLVMGlobalT>(this->Val);
1055     LLVMGVToGV ToGV(this->Ctx);
1056     return map_iterator(LLVMGV->getIterator(), ToGV);
1057   }
getReverseIterator()1058   reverse_iterator getReverseIterator() const {
1059     auto *LLVMGV = cast<LLVMGlobalT>(this->Val);
1060     LLVMGVToGV ToGV(this->Ctx);
1061     return map_iterator(LLVMGV->getReverseIterator(), ToGV);
1062   }
1063 };
1064 
1065 // Explicit instantiations.
1066 extern template class LLVM_TEMPLATE_ABI GlobalWithNodeAPI<
1067     GlobalIFunc, llvm::GlobalIFunc, GlobalObject, llvm::GlobalObject>;
1068 extern template class LLVM_TEMPLATE_ABI GlobalWithNodeAPI<
1069     Function, llvm::Function, GlobalObject, llvm::GlobalObject>;
1070 extern template class LLVM_TEMPLATE_ABI GlobalWithNodeAPI<
1071     GlobalVariable, llvm::GlobalVariable, GlobalObject, llvm::GlobalObject>;
1072 extern template class LLVM_TEMPLATE_ABI GlobalWithNodeAPI<
1073     GlobalAlias, llvm::GlobalAlias, GlobalValue, llvm::GlobalValue>;
1074 
1075 class GlobalIFunc final
1076     : public GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject,
1077                                llvm::GlobalObject> {
GlobalIFunc(llvm::GlobalObject * C,Context & Ctx)1078   GlobalIFunc(llvm::GlobalObject *C, Context &Ctx)
1079       : GlobalWithNodeAPI(ClassID::GlobalIFunc, C, Ctx) {}
1080   friend class Context; // For constructor.
1081 
1082 public:
1083   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1084   static bool classof(const sandboxir::Value *From) {
1085     return From->getSubclassID() == ClassID::GlobalIFunc;
1086   }
1087 
1088   // TODO: Missing create() because we don't have a sandboxir::Module yet.
1089 
1090   // TODO: Missing functions: copyAttributesFrom(), removeFromParent(),
1091   // eraseFromParent()
1092 
1093   LLVM_ABI void setResolver(Constant *Resolver);
1094 
1095   LLVM_ABI Constant *getResolver() const;
1096 
1097   // Return the resolver function after peeling off potential ConstantExpr
1098   // indirection.
1099   LLVM_ABI Function *getResolverFunction();
getResolverFunction()1100   const Function *getResolverFunction() const {
1101     return const_cast<GlobalIFunc *>(this)->getResolverFunction();
1102   }
1103 
isValidLinkage(LinkageTypes L)1104   static bool isValidLinkage(LinkageTypes L) {
1105     return llvm::GlobalIFunc::isValidLinkage(L);
1106   }
1107 
1108   // TODO: Missing applyAlongResolverPath().
1109 
1110 #ifndef NDEBUG
verify()1111   void verify() const override {
1112     assert(isa<llvm::GlobalIFunc>(Val) && "Expected a GlobalIFunc!");
1113   }
dumpOS(raw_ostream & OS)1114   void dumpOS(raw_ostream &OS) const override {
1115     dumpCommonPrefix(OS);
1116     dumpCommonSuffix(OS);
1117   }
1118 #endif
1119 };
1120 
1121 class GlobalVariable final
1122     : public GlobalWithNodeAPI<GlobalVariable, llvm::GlobalVariable,
1123                                GlobalObject, llvm::GlobalObject> {
GlobalVariable(llvm::GlobalObject * C,Context & Ctx)1124   GlobalVariable(llvm::GlobalObject *C, Context &Ctx)
1125       : GlobalWithNodeAPI(ClassID::GlobalVariable, C, Ctx) {}
1126   friend class Context; // For constructor.
1127 
1128   /// Helper for mapped_iterator.
1129   struct LLVMGVToGV {
1130     Context &Ctx;
LLVMGVToGVLLVMGVToGV1131     LLVMGVToGV(Context &Ctx) : Ctx(Ctx) {}
1132     LLVM_ABI GlobalVariable &operator()(llvm::GlobalVariable &LLVMGV) const;
1133   };
1134 
1135 public:
1136   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1137   static bool classof(const sandboxir::Value *From) {
1138     return From->getSubclassID() == ClassID::GlobalVariable;
1139   }
1140 
1141   /// Definitions have initializers, declarations don't.
1142   ///
hasInitializer()1143   inline bool hasInitializer() const {
1144     return cast<llvm::GlobalVariable>(Val)->hasInitializer();
1145   }
1146 
1147   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
1148   /// and any other instances of the global (this can happen due to weak
1149   /// linkage) are guaranteed to have the same initializer.
1150   ///
1151   /// Note that if you want to transform a global, you must use
1152   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
1153   ///
1154   /// Example:
1155   ///
1156   /// @a = global SomeType* null - Initializer is both definitive and unique.
1157   ///
1158   /// @b = global weak SomeType* null - Initializer is neither definitive nor
1159   /// unique.
1160   ///
1161   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
1162   /// unique.
hasDefinitiveInitializer()1163   inline bool hasDefinitiveInitializer() const {
1164     return cast<llvm::GlobalVariable>(Val)->hasDefinitiveInitializer();
1165   }
1166 
1167   /// hasUniqueInitializer - Whether the global variable has an initializer, and
1168   /// any changes made to the initializer will turn up in the final executable.
hasUniqueInitializer()1169   inline bool hasUniqueInitializer() const {
1170     return cast<llvm::GlobalVariable>(Val)->hasUniqueInitializer();
1171   }
1172 
1173   /// getInitializer - Return the initializer for this global variable.  It is
1174   /// illegal to call this method if the global is external, because we cannot
1175   /// tell what the value is initialized to!
1176   ///
1177   LLVM_ABI Constant *getInitializer() const;
1178   /// setInitializer - Sets the initializer for this global variable, removing
1179   /// any existing initializer if InitVal==NULL. The initializer must have the
1180   /// type getValueType().
1181   LLVM_ABI void setInitializer(Constant *InitVal);
1182 
1183   // TODO: Add missing replaceInitializer(). Requires special tracker
1184 
1185   /// If the value is a global constant, its value is immutable throughout the
1186   /// runtime execution of the program.  Assigning a value into the constant
1187   /// leads to undefined behavior.
1188   ///
isConstant()1189   bool isConstant() const {
1190     return cast<llvm::GlobalVariable>(Val)->isConstant();
1191   }
1192   LLVM_ABI void setConstant(bool V);
1193 
isExternallyInitialized()1194   bool isExternallyInitialized() const {
1195     return cast<llvm::GlobalVariable>(Val)->isExternallyInitialized();
1196   }
1197   LLVM_ABI void setExternallyInitialized(bool Val);
1198 
1199   // TODO: Missing copyAttributesFrom()
1200 
1201   // TODO: Missing removeFromParent(), eraseFromParent(), dropAllReferences()
1202 
1203   // TODO: Missing addDebugInfo(), getDebugInfo()
1204 
1205   // TODO: Missing attribute setter functions: addAttribute(), setAttributes().
1206   //       There seems to be no removeAttribute() so we can't undo them.
1207 
1208   /// Return true if the attribute exists.
hasAttribute(Attribute::AttrKind Kind)1209   bool hasAttribute(Attribute::AttrKind Kind) const {
1210     return cast<llvm::GlobalVariable>(Val)->hasAttribute(Kind);
1211   }
1212 
1213   /// Return true if the attribute exists.
hasAttribute(StringRef Kind)1214   bool hasAttribute(StringRef Kind) const {
1215     return cast<llvm::GlobalVariable>(Val)->hasAttribute(Kind);
1216   }
1217 
1218   /// Return true if any attributes exist.
hasAttributes()1219   bool hasAttributes() const {
1220     return cast<llvm::GlobalVariable>(Val)->hasAttributes();
1221   }
1222 
1223   /// Return the attribute object.
getAttribute(Attribute::AttrKind Kind)1224   Attribute getAttribute(Attribute::AttrKind Kind) const {
1225     return cast<llvm::GlobalVariable>(Val)->getAttribute(Kind);
1226   }
1227 
1228   /// Return the attribute object.
getAttribute(StringRef Kind)1229   Attribute getAttribute(StringRef Kind) const {
1230     return cast<llvm::GlobalVariable>(Val)->getAttribute(Kind);
1231   }
1232 
1233   /// Return the attribute set for this global
getAttributes()1234   AttributeSet getAttributes() const {
1235     return cast<llvm::GlobalVariable>(Val)->getAttributes();
1236   }
1237 
1238   /// Return attribute set as list with index.
1239   /// FIXME: This may not be required once ValueEnumerators
1240   /// in bitcode-writer can enumerate attribute-set.
getAttributesAsList(unsigned Index)1241   AttributeList getAttributesAsList(unsigned Index) const {
1242     return cast<llvm::GlobalVariable>(Val)->getAttributesAsList(Index);
1243   }
1244 
1245   /// Check if section name is present
hasImplicitSection()1246   bool hasImplicitSection() const {
1247     return cast<llvm::GlobalVariable>(Val)->hasImplicitSection();
1248   }
1249 
1250   /// Get the custom code model raw value of this global.
1251   ///
getCodeModelRaw()1252   unsigned getCodeModelRaw() const {
1253     return cast<llvm::GlobalVariable>(Val)->getCodeModelRaw();
1254   }
1255 
1256   /// Get the custom code model of this global if it has one.
1257   ///
1258   /// If this global does not have a custom code model, the empty instance
1259   /// will be returned.
getCodeModel()1260   std::optional<CodeModel::Model> getCodeModel() const {
1261     return cast<llvm::GlobalVariable>(Val)->getCodeModel();
1262   }
1263 
1264   /// Returns the alignment of the given variable.
getAlign()1265   MaybeAlign getAlign() const {
1266     return cast<llvm::GlobalVariable>(Val)->getAlign();
1267   }
1268 
1269   // TODO: Add missing: setAligment(Align)
1270 
1271   /// Sets the alignment attribute of the GlobalVariable.
1272   /// This method will be deprecated as the alignment property should always be
1273   /// defined.
1274   LLVM_ABI void setAlignment(MaybeAlign Align);
1275 
1276   // TODO: Missing setCodeModel(). Requires custom tracker.
1277 
1278 #ifndef NDEBUG
verify()1279   void verify() const override {
1280     assert(isa<llvm::GlobalVariable>(Val) && "Expected a GlobalVariable!");
1281   }
dumpOS(raw_ostream & OS)1282   void dumpOS(raw_ostream &OS) const override {
1283     dumpCommonPrefix(OS);
1284     dumpCommonSuffix(OS);
1285   }
1286 #endif
1287 };
1288 
1289 class GlobalAlias final
1290     : public GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue,
1291                                llvm::GlobalValue> {
GlobalAlias(llvm::GlobalAlias * C,Context & Ctx)1292   GlobalAlias(llvm::GlobalAlias *C, Context &Ctx)
1293       : GlobalWithNodeAPI(ClassID::GlobalAlias, C, Ctx) {}
1294   friend class Context; // For constructor.
1295 
1296 public:
1297   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1298   static bool classof(const sandboxir::Value *From) {
1299     return From->getSubclassID() == ClassID::GlobalAlias;
1300   }
1301 
1302   // TODO: Missing create() due to unimplemented sandboxir::Module.
1303 
1304   // TODO: Missing copyAttributresFrom().
1305   // TODO: Missing removeFromParent(), eraseFromParent().
1306 
1307   LLVM_ABI void setAliasee(Constant *Aliasee);
1308   LLVM_ABI Constant *getAliasee() const;
1309 
1310   LLVM_ABI const GlobalObject *getAliaseeObject() const;
getAliaseeObject()1311   GlobalObject *getAliaseeObject() {
1312     return const_cast<GlobalObject *>(
1313         static_cast<const GlobalAlias *>(this)->getAliaseeObject());
1314   }
1315 
isValidLinkage(LinkageTypes L)1316   static bool isValidLinkage(LinkageTypes L) {
1317     return llvm::GlobalAlias::isValidLinkage(L);
1318   }
1319 };
1320 
1321 class NoCFIValue final : public Constant {
NoCFIValue(llvm::NoCFIValue * C,Context & Ctx)1322   NoCFIValue(llvm::NoCFIValue *C, Context &Ctx)
1323       : Constant(ClassID::NoCFIValue, C, Ctx) {}
1324   friend class Context; // For constructor.
1325 
getOperandUseInternal(unsigned OpIdx,bool Verify)1326   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
1327     return getOperandUseDefault(OpIdx, Verify);
1328   }
1329 
1330 public:
1331   /// Return a NoCFIValue for the specified function.
1332   LLVM_ABI static NoCFIValue *get(GlobalValue *GV);
1333 
1334   LLVM_ABI GlobalValue *getGlobalValue() const;
1335 
1336   /// NoCFIValue is always a pointer.
1337   LLVM_ABI PointerType *getType() const;
1338   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1339   static bool classof(const sandboxir::Value *From) {
1340     return From->getSubclassID() == ClassID::NoCFIValue;
1341   }
1342 
getUseOperandNo(const Use & Use)1343   unsigned getUseOperandNo(const Use &Use) const final {
1344     return getUseOperandNoDefault(Use);
1345   }
1346 
1347 #ifndef NDEBUG
verify()1348   void verify() const override {
1349     assert(isa<llvm::NoCFIValue>(Val) && "Expected a NoCFIValue!");
1350   }
dumpOS(raw_ostream & OS)1351   void dumpOS(raw_ostream &OS) const override {
1352     dumpCommonPrefix(OS);
1353     dumpCommonSuffix(OS);
1354   }
1355 #endif
1356 };
1357 
1358 class ConstantPtrAuth final : public Constant {
ConstantPtrAuth(llvm::ConstantPtrAuth * C,Context & Ctx)1359   ConstantPtrAuth(llvm::ConstantPtrAuth *C, Context &Ctx)
1360       : Constant(ClassID::ConstantPtrAuth, C, Ctx) {}
1361   friend class Context; // For constructor.
1362 
1363 public:
1364   /// Return a pointer signed with the specified parameters.
1365   LLVM_ABI static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1366                                        ConstantInt *Disc, Constant *AddrDisc);
1367   /// The pointer that is signed in this ptrauth signed pointer.
1368   LLVM_ABI Constant *getPointer() const;
1369 
1370   /// The Key ID, an i32 constant.
1371   LLVM_ABI ConstantInt *getKey() const;
1372 
1373   /// The integer discriminator, an i64 constant, or 0.
1374   LLVM_ABI ConstantInt *getDiscriminator() const;
1375 
1376   /// The address discriminator if any, or the null constant.
1377   /// If present, this must be a value equivalent to the storage location of
1378   /// the only global-initializer user of the ptrauth signed pointer.
1379   LLVM_ABI Constant *getAddrDiscriminator() const;
1380 
1381   /// Whether there is any non-null address discriminator.
hasAddressDiscriminator()1382   bool hasAddressDiscriminator() const {
1383     return cast<llvm::ConstantPtrAuth>(Val)->hasAddressDiscriminator();
1384   }
1385 
1386   /// Whether the address uses a special address discriminator.
1387   /// These discriminators can't be used in real pointer-auth values; they
1388   /// can only be used in "prototype" values that indicate how some real
1389   /// schema is supposed to be produced.
hasSpecialAddressDiscriminator(uint64_t Value)1390   bool hasSpecialAddressDiscriminator(uint64_t Value) const {
1391     return cast<llvm::ConstantPtrAuth>(Val)->hasSpecialAddressDiscriminator(
1392         Value);
1393   }
1394 
1395   /// Check whether an authentication operation with key \p Key and (possibly
1396   /// blended) discriminator \p Discriminator is known to be compatible with
1397   /// this ptrauth signed pointer.
isKnownCompatibleWith(const Value * Key,const Value * Discriminator,const DataLayout & DL)1398   bool isKnownCompatibleWith(const Value *Key, const Value *Discriminator,
1399                              const DataLayout &DL) const {
1400     return cast<llvm::ConstantPtrAuth>(Val)->isKnownCompatibleWith(
1401         Key->Val, Discriminator->Val, DL);
1402   }
1403 
1404   /// Produce a new ptrauth expression signing the given value using
1405   /// the same schema as is stored in one.
1406   LLVM_ABI ConstantPtrAuth *getWithSameSchema(Constant *Pointer) const;
1407 
1408   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1409   static bool classof(const sandboxir::Value *From) {
1410     return From->getSubclassID() == ClassID::ConstantPtrAuth;
1411   }
1412 };
1413 
1414 class ConstantExpr : public Constant {
ConstantExpr(llvm::ConstantExpr * C,Context & Ctx)1415   ConstantExpr(llvm::ConstantExpr *C, Context &Ctx)
1416       : Constant(ClassID::ConstantExpr, C, Ctx) {}
1417   friend class Context; // For constructor.
1418 
1419 public:
1420   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1421   static bool classof(const sandboxir::Value *From) {
1422     return From->getSubclassID() == ClassID::ConstantExpr;
1423   }
1424   // TODO: Missing functions.
1425 };
1426 
1427 class BlockAddress final : public Constant {
BlockAddress(llvm::BlockAddress * C,Context & Ctx)1428   BlockAddress(llvm::BlockAddress *C, Context &Ctx)
1429       : Constant(ClassID::BlockAddress, C, Ctx) {}
1430   friend class Context; // For constructor.
1431 
1432 public:
1433   /// Return a BlockAddress for the specified function and basic block.
1434   LLVM_ABI static BlockAddress *get(Function *F, BasicBlock *BB);
1435 
1436   /// Return a BlockAddress for the specified basic block.  The basic
1437   /// block must be embedded into a function.
1438   LLVM_ABI static BlockAddress *get(BasicBlock *BB);
1439 
1440   /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
1441   ///
1442   /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
1443   LLVM_ABI static BlockAddress *lookup(const BasicBlock *BB);
1444 
1445   LLVM_ABI Function *getFunction() const;
1446   LLVM_ABI BasicBlock *getBasicBlock() const;
1447 
1448   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1449   static bool classof(const sandboxir::Value *From) {
1450     return From->getSubclassID() == ClassID::BlockAddress;
1451   }
1452 };
1453 
1454 class DSOLocalEquivalent final : public Constant {
DSOLocalEquivalent(llvm::DSOLocalEquivalent * C,Context & Ctx)1455   DSOLocalEquivalent(llvm::DSOLocalEquivalent *C, Context &Ctx)
1456       : Constant(ClassID::DSOLocalEquivalent, C, Ctx) {}
1457   friend class Context; // For constructor.
1458 
1459 public:
1460   /// Return a DSOLocalEquivalent for the specified global value.
1461   LLVM_ABI static DSOLocalEquivalent *get(GlobalValue *GV);
1462 
1463   LLVM_ABI GlobalValue *getGlobalValue() const;
1464 
1465   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1466   static bool classof(const sandboxir::Value *From) {
1467     return From->getSubclassID() == ClassID::DSOLocalEquivalent;
1468   }
1469 
getUseOperandNo(const Use & Use)1470   unsigned getUseOperandNo(const Use &Use) const final {
1471     llvm_unreachable("DSOLocalEquivalent has no operands!");
1472   }
1473 
1474 #ifndef NDEBUG
verify()1475   void verify() const override {
1476     assert(isa<llvm::DSOLocalEquivalent>(Val) &&
1477            "Expected a DSOLocalEquivalent!");
1478   }
dumpOS(raw_ostream & OS)1479   void dumpOS(raw_ostream &OS) const override {
1480     dumpCommonPrefix(OS);
1481     dumpCommonSuffix(OS);
1482   }
1483 #endif
1484 };
1485 
1486 // TODO: This should inherit from ConstantData.
1487 class ConstantTokenNone final : public Constant {
ConstantTokenNone(llvm::ConstantTokenNone * C,Context & Ctx)1488   ConstantTokenNone(llvm::ConstantTokenNone *C, Context &Ctx)
1489       : Constant(ClassID::ConstantTokenNone, C, Ctx) {}
1490   friend class Context; // For constructor.
1491 
1492 public:
1493   /// Return the ConstantTokenNone.
1494   LLVM_ABI static ConstantTokenNone *get(Context &Ctx);
1495 
1496   /// For isa/dyn_cast.
classof(const sandboxir::Value * From)1497   static bool classof(const sandboxir::Value *From) {
1498     return From->getSubclassID() == ClassID::ConstantTokenNone;
1499   }
1500 
getUseOperandNo(const Use & Use)1501   unsigned getUseOperandNo(const Use &Use) const final {
1502     llvm_unreachable("ConstantTokenNone has no operands!");
1503   }
1504 
1505 #ifndef NDEBUG
verify()1506   void verify() const override {
1507     assert(isa<llvm::ConstantTokenNone>(Val) &&
1508            "Expected a ConstantTokenNone!");
1509   }
dumpOS(raw_ostream & OS)1510   void dumpOS(raw_ostream &OS) const override {
1511     dumpCommonPrefix(OS);
1512     dumpCommonSuffix(OS);
1513   }
1514 #endif
1515 };
1516 
1517 } // namespace llvm::sandboxir
1518 
1519 #endif // LLVM_SANDBOXIR_CONSTANT_H
1520