Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===- Constant.h -----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SANDBOXIR_CONSTANT_H
0010 #define LLVM_SANDBOXIR_CONSTANT_H
0011 
0012 #include "llvm/IR/BasicBlock.h"
0013 #include "llvm/IR/Constant.h"
0014 #include "llvm/IR/Constants.h"
0015 #include "llvm/IR/GlobalAlias.h"
0016 #include "llvm/IR/GlobalIFunc.h"
0017 #include "llvm/IR/GlobalObject.h"
0018 #include "llvm/IR/GlobalValue.h"
0019 #include "llvm/IR/GlobalVariable.h"
0020 #include "llvm/SandboxIR/Argument.h"
0021 #include "llvm/SandboxIR/BasicBlock.h"
0022 #include "llvm/SandboxIR/Context.h"
0023 #include "llvm/SandboxIR/Type.h"
0024 #include "llvm/SandboxIR/User.h"
0025 #include "llvm/Support/Compiler.h"
0026 
0027 namespace llvm::sandboxir {
0028 
0029 class BasicBlock;
0030 class Function;
0031 
0032 class Constant : public sandboxir::User {
0033 protected:
0034   Constant(llvm::Constant *C, sandboxir::Context &SBCtx)
0035       : sandboxir::User(ClassID::Constant, C, SBCtx) {}
0036   Constant(ClassID ID, llvm::Constant *C, sandboxir::Context &SBCtx)
0037       : sandboxir::User(ID, C, SBCtx) {}
0038   friend class ConstantInt; // For constructor.
0039   friend class Function;    // For constructor
0040   friend class Context;     // For constructor.
0041   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
0042     return getOperandUseDefault(OpIdx, Verify);
0043   }
0044 
0045 public:
0046   /// For isa/dyn_cast.
0047   static bool classof(const sandboxir::Value *From) {
0048     switch (From->getSubclassID()) {
0049 #define DEF_CONST(ID, CLASS) case ClassID::ID:
0050 #include "llvm/SandboxIR/Values.def"
0051       return true;
0052     default:
0053       return false;
0054     }
0055   }
0056   sandboxir::Context &getParent() const { return getContext(); }
0057   unsigned getUseOperandNo(const Use &Use) const override {
0058     return getUseOperandNoDefault(Use);
0059   }
0060 #ifndef NDEBUG
0061   void verify() const override {
0062     assert(isa<llvm::Constant>(Val) && "Expected Constant!");
0063   }
0064   void dumpOS(raw_ostream &OS) const override;
0065 #endif
0066 };
0067 
0068 // TODO: This should inherit from ConstantData.
0069 class ConstantInt : public Constant {
0070   ConstantInt(llvm::ConstantInt *C, Context &Ctx)
0071       : Constant(ClassID::ConstantInt, C, Ctx) {}
0072   friend class Context; // For constructor.
0073 
0074   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
0075     llvm_unreachable("ConstantInt has no operands!");
0076   }
0077 
0078 public:
0079   static ConstantInt *getTrue(Context &Ctx);
0080   static ConstantInt *getFalse(Context &Ctx);
0081   static ConstantInt *getBool(Context &Ctx, bool V);
0082   static Constant *getTrue(Type *Ty);
0083   static Constant *getFalse(Type *Ty);
0084   static Constant *getBool(Type *Ty, bool V);
0085 
0086   /// If Ty is a vector type, return a Constant with a splat of the given
0087   /// value. Otherwise return a ConstantInt for the given value.
0088   static ConstantInt *get(Type *Ty, uint64_t V, bool IsSigned = false);
0089 
0090   /// Return a ConstantInt with the specified integer value for the specified
0091   /// type. If the type is wider than 64 bits, the value will be zero-extended
0092   /// to fit the type, unless IsSigned is true, in which case the value will
0093   /// be interpreted as a 64-bit signed integer and sign-extended to fit
0094   /// the type.
0095   /// Get a ConstantInt for a specific value.
0096   static ConstantInt *get(IntegerType *Ty, uint64_t V, bool IsSigned = false);
0097 
0098   /// Return a ConstantInt with the specified value for the specified type. The
0099   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
0100   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
0101   /// signed value for the type Ty.
0102   /// Get a ConstantInt for a specific signed value.
0103   static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
0104   static Constant *getSigned(Type *Ty, int64_t V);
0105 
0106   /// Return a ConstantInt with the specified value and an implied Type. The
0107   /// type is the integer type that corresponds to the bit width of the value.
0108   static ConstantInt *get(Context &Ctx, const APInt &V);
0109 
0110   /// Return a ConstantInt constructed from the string strStart with the given
0111   /// radix.
0112   static ConstantInt *get(IntegerType *Ty, StringRef Str, uint8_t Radix);
0113 
0114   /// If Ty is a vector type, return a Constant with a splat of the given
0115   /// value. Otherwise return a ConstantInt for the given value.
0116   static Constant *get(Type *Ty, const APInt &V);
0117 
0118   /// Return the constant as an APInt value reference. This allows clients to
0119   /// obtain a full-precision copy of the value.
0120   /// Return the constant's value.
0121   inline const APInt &getValue() const {
0122     return cast<llvm::ConstantInt>(Val)->getValue();
0123   }
0124 
0125   /// getBitWidth - Return the scalar bitwidth of this constant.
0126   unsigned getBitWidth() const {
0127     return cast<llvm::ConstantInt>(Val)->getBitWidth();
0128   }
0129   /// Return the constant as a 64-bit unsigned integer value after it
0130   /// has been zero extended as appropriate for the type of this constant. Note
0131   /// that this method can assert if the value does not fit in 64 bits.
0132   /// Return the zero extended value.
0133   inline uint64_t getZExtValue() const {
0134     return cast<llvm::ConstantInt>(Val)->getZExtValue();
0135   }
0136 
0137   /// Return the constant as a 64-bit integer value after it has been sign
0138   /// extended as appropriate for the type of this constant. Note that
0139   /// this method can assert if the value does not fit in 64 bits.
0140   /// Return the sign extended value.
0141   inline int64_t getSExtValue() const {
0142     return cast<llvm::ConstantInt>(Val)->getSExtValue();
0143   }
0144 
0145   /// Return the constant as an llvm::MaybeAlign.
0146   /// Note that this method can assert if the value does not fit in 64 bits or
0147   /// is not a power of two.
0148   inline MaybeAlign getMaybeAlignValue() const {
0149     return cast<llvm::ConstantInt>(Val)->getMaybeAlignValue();
0150   }
0151 
0152   /// Return the constant as an llvm::Align, interpreting `0` as `Align(1)`.
0153   /// Note that this method can assert if the value does not fit in 64 bits or
0154   /// is not a power of two.
0155   inline Align getAlignValue() const {
0156     return cast<llvm::ConstantInt>(Val)->getAlignValue();
0157   }
0158 
0159   /// A helper method that can be used to determine if the constant contained
0160   /// within is equal to a constant.  This only works for very small values,
0161   /// because this is all that can be represented with all types.
0162   /// Determine if this constant's value is same as an unsigned char.
0163   bool equalsInt(uint64_t V) const {
0164     return cast<llvm::ConstantInt>(Val)->equalsInt(V);
0165   }
0166 
0167   /// Variant of the getType() method to always return an IntegerType, which
0168   /// reduces the amount of casting needed in parts of the compiler.
0169   IntegerType *getIntegerType() const;
0170 
0171   /// This static method returns true if the type Ty is big enough to
0172   /// represent the value V. This can be used to avoid having the get method
0173   /// assert when V is larger than Ty can represent. Note that there are two
0174   /// versions of this method, one for unsigned and one for signed integers.
0175   /// Although ConstantInt canonicalizes everything to an unsigned integer,
0176   /// the signed version avoids callers having to convert a signed quantity
0177   /// to the appropriate unsigned type before calling the method.
0178   /// @returns true if V is a valid value for type Ty
0179   /// Determine if the value is in range for the given type.
0180   static bool isValueValidForType(Type *Ty, uint64_t V);
0181   static bool isValueValidForType(Type *Ty, int64_t V);
0182 
0183   bool isNegative() const { return cast<llvm::ConstantInt>(Val)->isNegative(); }
0184 
0185   /// This is just a convenience method to make client code smaller for a
0186   /// common code. It also correctly performs the comparison without the
0187   /// potential for an assertion from getZExtValue().
0188   bool isZero() const { return cast<llvm::ConstantInt>(Val)->isZero(); }
0189 
0190   /// This is just a convenience method to make client code smaller for a
0191   /// common case. It also correctly performs the comparison without the
0192   /// potential for an assertion from getZExtValue().
0193   /// Determine if the value is one.
0194   bool isOne() const { return cast<llvm::ConstantInt>(Val)->isOne(); }
0195 
0196   /// This function will return true iff every bit in this constant is set
0197   /// to true.
0198   /// @returns true iff this constant's bits are all set to true.
0199   /// Determine if the value is all ones.
0200   bool isMinusOne() const { return cast<llvm::ConstantInt>(Val)->isMinusOne(); }
0201 
0202   /// This function will return true iff this constant represents the largest
0203   /// value that may be represented by the constant's type.
0204   /// @returns true iff this is the largest value that may be represented
0205   /// by this type.
0206   /// Determine if the value is maximal.
0207   bool isMaxValue(bool IsSigned) const {
0208     return cast<llvm::ConstantInt>(Val)->isMaxValue(IsSigned);
0209   }
0210 
0211   /// This function will return true iff this constant represents the smallest
0212   /// value that may be represented by this constant's type.
0213   /// @returns true if this is the smallest value that may be represented by
0214   /// this type.
0215   /// Determine if the value is minimal.
0216   bool isMinValue(bool IsSigned) const {
0217     return cast<llvm::ConstantInt>(Val)->isMinValue(IsSigned);
0218   }
0219 
0220   /// This function will return true iff this constant represents a value with
0221   /// active bits bigger than 64 bits or a value greater than the given uint64_t
0222   /// value.
0223   /// @returns true iff this constant is greater or equal to the given number.
0224   /// Determine if the value is greater or equal to the given number.
0225   bool uge(uint64_t Num) const {
0226     return cast<llvm::ConstantInt>(Val)->uge(Num);
0227   }
0228 
0229   /// getLimitedValue - If the value is smaller than the specified limit,
0230   /// return it, otherwise return the limit value.  This causes the value
0231   /// to saturate to the limit.
0232   /// @returns the min of the value of the constant and the specified value
0233   /// Get the constant's value with a saturation limit
0234   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
0235     return cast<llvm::ConstantInt>(Val)->getLimitedValue(Limit);
0236   }
0237 
0238   /// For isa/dyn_cast.
0239   static bool classof(const sandboxir::Value *From) {
0240     return From->getSubclassID() == ClassID::ConstantInt;
0241   }
0242   unsigned getUseOperandNo(const Use &Use) const override {
0243     llvm_unreachable("ConstantInt has no operands!");
0244   }
0245 #ifndef NDEBUG
0246   void verify() const override {
0247     assert(isa<llvm::ConstantInt>(Val) && "Expected a ConstantInst!");
0248   }
0249   void dumpOS(raw_ostream &OS) const override {
0250     dumpCommonPrefix(OS);
0251     dumpCommonSuffix(OS);
0252   }
0253 #endif
0254 };
0255 
0256 // TODO: This should inherit from ConstantData.
0257 class ConstantFP final : public Constant {
0258   ConstantFP(llvm::ConstantFP *C, Context &Ctx)
0259       : Constant(ClassID::ConstantFP, C, Ctx) {}
0260   friend class Context; // For constructor.
0261 
0262 public:
0263   /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
0264   /// for the specified value in the specified type. This should only be used
0265   /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
0266   /// host double and as the target format.
0267   static Constant *get(Type *Ty, double V);
0268 
0269   /// If Ty is a vector type, return a Constant with a splat of the given
0270   /// value. Otherwise return a ConstantFP for the given value.
0271   static Constant *get(Type *Ty, const APFloat &V);
0272 
0273   static Constant *get(Type *Ty, StringRef Str);
0274 
0275   static ConstantFP *get(const APFloat &V, Context &Ctx);
0276 
0277   static Constant *getNaN(Type *Ty, bool Negative = false,
0278                           uint64_t Payload = 0);
0279   static Constant *getQNaN(Type *Ty, bool Negative = false,
0280                            APInt *Payload = nullptr);
0281   static Constant *getSNaN(Type *Ty, bool Negative = false,
0282                            APInt *Payload = nullptr);
0283   static Constant *getZero(Type *Ty, bool Negative = false);
0284 
0285   static Constant *getNegativeZero(Type *Ty);
0286   static Constant *getInfinity(Type *Ty, bool Negative = false);
0287 
0288   /// Return true if Ty is big enough to represent V.
0289   static bool isValueValidForType(Type *Ty, const APFloat &V);
0290 
0291   inline const APFloat &getValueAPF() const {
0292     return cast<llvm::ConstantFP>(Val)->getValueAPF();
0293   }
0294   inline const APFloat &getValue() const {
0295     return cast<llvm::ConstantFP>(Val)->getValue();
0296   }
0297 
0298   /// Return true if the value is positive or negative zero.
0299   bool isZero() const { return cast<llvm::ConstantFP>(Val)->isZero(); }
0300 
0301   /// Return true if the sign bit is set.
0302   bool isNegative() const { return cast<llvm::ConstantFP>(Val)->isNegative(); }
0303 
0304   /// Return true if the value is infinity
0305   bool isInfinity() const { return cast<llvm::ConstantFP>(Val)->isInfinity(); }
0306 
0307   /// Return true if the value is a NaN.
0308   bool isNaN() const { return cast<llvm::ConstantFP>(Val)->isNaN(); }
0309 
0310   /// We don't rely on operator== working on double values, as it returns true
0311   /// for things that are clearly not equal, like -0.0 and 0.0.
0312   /// As such, this method can be used to do an exact bit-for-bit comparison of
0313   /// two floating point values.  The version with a double operand is retained
0314   /// because it's so convenient to write isExactlyValue(2.0), but please use
0315   /// it only for simple constants.
0316   bool isExactlyValue(const APFloat &V) const {
0317     return cast<llvm::ConstantFP>(Val)->isExactlyValue(V);
0318   }
0319 
0320   bool isExactlyValue(double V) const {
0321     return cast<llvm::ConstantFP>(Val)->isExactlyValue(V);
0322   }
0323 
0324   /// For isa/dyn_cast.
0325   static bool classof(const sandboxir::Value *From) {
0326     return From->getSubclassID() == ClassID::ConstantFP;
0327   }
0328 
0329   // TODO: Better name: getOperandNo(const Use&). Should be private.
0330   unsigned getUseOperandNo(const Use &Use) const final {
0331     llvm_unreachable("ConstantFP has no operands!");
0332   }
0333 #ifndef NDEBUG
0334   void verify() const override {
0335     assert(isa<llvm::ConstantFP>(Val) && "Expected a ConstantFP!");
0336   }
0337   void dumpOS(raw_ostream &OS) const override {
0338     dumpCommonPrefix(OS);
0339     dumpCommonSuffix(OS);
0340   }
0341 #endif
0342 };
0343 
0344 /// Base class for aggregate constants (with operands).
0345 class ConstantAggregate : public Constant {
0346 protected:
0347   ConstantAggregate(ClassID ID, llvm::Constant *C, Context &Ctx)
0348       : Constant(ID, C, Ctx) {}
0349 
0350 public:
0351   /// For isa/dyn_cast.
0352   static bool classof(const sandboxir::Value *From) {
0353     auto ID = From->getSubclassID();
0354     return ID == ClassID::ConstantVector || ID == ClassID::ConstantStruct ||
0355            ID == ClassID::ConstantArray;
0356   }
0357 };
0358 
0359 class ConstantArray final : public ConstantAggregate {
0360   ConstantArray(llvm::ConstantArray *C, Context &Ctx)
0361       : ConstantAggregate(ClassID::ConstantArray, C, Ctx) {}
0362   friend class Context; // For constructor.
0363 
0364 public:
0365   static Constant *get(ArrayType *T, ArrayRef<Constant *> V);
0366   ArrayType *getType() const;
0367 
0368   // TODO: Missing functions: getType(), getTypeForElements(), getAnon(), get().
0369 
0370   /// For isa/dyn_cast.
0371   static bool classof(const Value *From) {
0372     return From->getSubclassID() == ClassID::ConstantArray;
0373   }
0374 };
0375 
0376 class ConstantStruct final : public ConstantAggregate {
0377   ConstantStruct(llvm::ConstantStruct *C, Context &Ctx)
0378       : ConstantAggregate(ClassID::ConstantStruct, C, Ctx) {}
0379   friend class Context; // For constructor.
0380 
0381 public:
0382   static Constant *get(StructType *T, ArrayRef<Constant *> V);
0383 
0384   template <typename... Csts>
0385   static std::enable_if_t<are_base_of<Constant, Csts...>::value, Constant *>
0386   get(StructType *T, Csts *...Vs) {
0387     return get(T, ArrayRef<Constant *>({Vs...}));
0388   }
0389   /// Return an anonymous struct that has the specified elements.
0390   /// If the struct is possibly empty, then you must specify a context.
0391   static Constant *getAnon(ArrayRef<Constant *> V, bool Packed = false) {
0392     return get(getTypeForElements(V, Packed), V);
0393   }
0394   static Constant *getAnon(Context &Ctx, ArrayRef<Constant *> V,
0395                            bool Packed = false) {
0396     return get(getTypeForElements(Ctx, V, Packed), V);
0397   }
0398   /// This version of the method allows an empty list.
0399   static StructType *getTypeForElements(Context &Ctx, ArrayRef<Constant *> V,
0400                                         bool Packed = false);
0401   /// Return an anonymous struct type to use for a constant with the specified
0402   /// set of elements. The list must not be empty.
0403   static StructType *getTypeForElements(ArrayRef<Constant *> V,
0404                                         bool Packed = false) {
0405     assert(!V.empty() &&
0406            "ConstantStruct::getTypeForElements cannot be called on empty list");
0407     return getTypeForElements(V[0]->getContext(), V, Packed);
0408   }
0409 
0410   /// Specialization - reduce amount of casting.
0411   inline StructType *getType() const {
0412     return cast<StructType>(Value::getType());
0413   }
0414 
0415   /// For isa/dyn_cast.
0416   static bool classof(const Value *From) {
0417     return From->getSubclassID() == ClassID::ConstantStruct;
0418   }
0419 };
0420 
0421 class ConstantVector final : public ConstantAggregate {
0422   ConstantVector(llvm::ConstantVector *C, Context &Ctx)
0423       : ConstantAggregate(ClassID::ConstantVector, C, Ctx) {}
0424   friend class Context; // For constructor.
0425 
0426 public:
0427   // TODO: Missing functions: getSplat(), getType(), getSplatValue(), get().
0428 
0429   /// For isa/dyn_cast.
0430   static bool classof(const Value *From) {
0431     return From->getSubclassID() == ClassID::ConstantVector;
0432   }
0433 };
0434 
0435 // TODO: Inherit from ConstantData.
0436 class ConstantAggregateZero final : public Constant {
0437   ConstantAggregateZero(llvm::ConstantAggregateZero *C, Context &Ctx)
0438       : Constant(ClassID::ConstantAggregateZero, C, Ctx) {}
0439   friend class Context; // For constructor.
0440 
0441 public:
0442   static ConstantAggregateZero *get(Type *Ty);
0443   /// If this CAZ has array or vector type, return a zero with the right element
0444   /// type.
0445   Constant *getSequentialElement() const;
0446   /// If this CAZ has struct type, return a zero with the right element type for
0447   /// the specified element.
0448   Constant *getStructElement(unsigned Elt) const;
0449   /// Return a zero of the right value for the specified GEP index if we can,
0450   /// otherwise return null (e.g. if C is a ConstantExpr).
0451   Constant *getElementValue(Constant *C) const;
0452   /// Return a zero of the right value for the specified GEP index.
0453   Constant *getElementValue(unsigned Idx) const;
0454   /// Return the number of elements in the array, vector, or struct.
0455   ElementCount getElementCount() const {
0456     return cast<llvm::ConstantAggregateZero>(Val)->getElementCount();
0457   }
0458 
0459   /// For isa/dyn_cast.
0460   static bool classof(const sandboxir::Value *From) {
0461     return From->getSubclassID() == ClassID::ConstantAggregateZero;
0462   }
0463   unsigned getUseOperandNo(const Use &Use) const final {
0464     llvm_unreachable("ConstantAggregateZero has no operands!");
0465   }
0466 #ifndef NDEBUG
0467   void verify() const override {
0468     assert(isa<llvm::ConstantAggregateZero>(Val) && "Expected a CAZ!");
0469   }
0470   void dumpOS(raw_ostream &OS) const override {
0471     dumpCommonPrefix(OS);
0472     dumpCommonSuffix(OS);
0473   }
0474 #endif
0475 };
0476 
0477 // TODO: Inherit from ConstantData.
0478 class ConstantPointerNull final : public Constant {
0479   ConstantPointerNull(llvm::ConstantPointerNull *C, Context &Ctx)
0480       : Constant(ClassID::ConstantPointerNull, C, Ctx) {}
0481   friend class Context; // For constructor.
0482 
0483 public:
0484   static ConstantPointerNull *get(PointerType *Ty);
0485 
0486   PointerType *getType() const;
0487 
0488   /// For isa/dyn_cast.
0489   static bool classof(const sandboxir::Value *From) {
0490     return From->getSubclassID() == ClassID::ConstantPointerNull;
0491   }
0492   unsigned getUseOperandNo(const Use &Use) const final {
0493     llvm_unreachable("ConstantPointerNull has no operands!");
0494   }
0495 #ifndef NDEBUG
0496   void verify() const override {
0497     assert(isa<llvm::ConstantPointerNull>(Val) && "Expected a CPNull!");
0498   }
0499   void dumpOS(raw_ostream &OS) const override {
0500     dumpCommonPrefix(OS);
0501     dumpCommonSuffix(OS);
0502   }
0503 #endif
0504 };
0505 
0506 // TODO: Inherit from ConstantData.
0507 class UndefValue : public Constant {
0508 protected:
0509   UndefValue(llvm::UndefValue *C, Context &Ctx)
0510       : Constant(ClassID::UndefValue, C, Ctx) {}
0511   UndefValue(ClassID ID, llvm::Constant *C, Context &Ctx)
0512       : Constant(ID, C, Ctx) {}
0513   friend class Context; // For constructor.
0514 
0515 public:
0516   /// Static factory methods - Return an 'undef' object of the specified type.
0517   static UndefValue *get(Type *T);
0518 
0519   /// If this Undef has array or vector type, return a undef with the right
0520   /// element type.
0521   UndefValue *getSequentialElement() const;
0522 
0523   /// If this undef has struct type, return a undef with the right element type
0524   /// for the specified element.
0525   UndefValue *getStructElement(unsigned Elt) const;
0526 
0527   /// Return an undef of the right value for the specified GEP index if we can,
0528   /// otherwise return null (e.g. if C is a ConstantExpr).
0529   UndefValue *getElementValue(Constant *C) const;
0530 
0531   /// Return an undef of the right value for the specified GEP index.
0532   UndefValue *getElementValue(unsigned Idx) const;
0533 
0534   /// Return the number of elements in the array, vector, or struct.
0535   unsigned getNumElements() const {
0536     return cast<llvm::UndefValue>(Val)->getNumElements();
0537   }
0538 
0539   /// For isa/dyn_cast.
0540   static bool classof(const sandboxir::Value *From) {
0541     return From->getSubclassID() == ClassID::UndefValue ||
0542            From->getSubclassID() == ClassID::PoisonValue;
0543   }
0544   unsigned getUseOperandNo(const Use &Use) const final {
0545     llvm_unreachable("UndefValue has no operands!");
0546   }
0547 #ifndef NDEBUG
0548   void verify() const override {
0549     assert(isa<llvm::UndefValue>(Val) && "Expected an UndefValue!");
0550   }
0551   void dumpOS(raw_ostream &OS) const override {
0552     dumpCommonPrefix(OS);
0553     dumpCommonSuffix(OS);
0554   }
0555 #endif
0556 };
0557 
0558 class PoisonValue final : public UndefValue {
0559   PoisonValue(llvm::PoisonValue *C, Context &Ctx)
0560       : UndefValue(ClassID::PoisonValue, C, Ctx) {}
0561   friend class Context; // For constructor.
0562 
0563 public:
0564   /// Static factory methods - Return an 'poison' object of the specified type.
0565   static PoisonValue *get(Type *T);
0566 
0567   /// If this poison has array or vector type, return a poison with the right
0568   /// element type.
0569   PoisonValue *getSequentialElement() const;
0570 
0571   /// If this poison has struct type, return a poison with the right element
0572   /// type for the specified element.
0573   PoisonValue *getStructElement(unsigned Elt) const;
0574 
0575   /// Return an poison of the right value for the specified GEP index if we can,
0576   /// otherwise return null (e.g. if C is a ConstantExpr).
0577   PoisonValue *getElementValue(Constant *C) const;
0578 
0579   /// Return an poison of the right value for the specified GEP index.
0580   PoisonValue *getElementValue(unsigned Idx) const;
0581 
0582   /// For isa/dyn_cast.
0583   static bool classof(const sandboxir::Value *From) {
0584     return From->getSubclassID() == ClassID::PoisonValue;
0585   }
0586 #ifndef NDEBUG
0587   void verify() const override {
0588     assert(isa<llvm::PoisonValue>(Val) && "Expected a PoisonValue!");
0589   }
0590   void dumpOS(raw_ostream &OS) const override {
0591     dumpCommonPrefix(OS);
0592     dumpCommonSuffix(OS);
0593   }
0594 #endif
0595 };
0596 
0597 class GlobalValue : public Constant {
0598 protected:
0599   GlobalValue(ClassID ID, llvm::GlobalValue *C, Context &Ctx)
0600       : Constant(ID, C, Ctx) {}
0601   friend class Context; // For constructor.
0602 
0603 public:
0604   using LinkageTypes = llvm::GlobalValue::LinkageTypes;
0605   /// For isa/dyn_cast.
0606   static bool classof(const sandboxir::Value *From) {
0607     switch (From->getSubclassID()) {
0608     case ClassID::Function:
0609     case ClassID::GlobalVariable:
0610     case ClassID::GlobalAlias:
0611     case ClassID::GlobalIFunc:
0612       return true;
0613     default:
0614       return false;
0615     }
0616   }
0617 
0618   unsigned getAddressSpace() const {
0619     return cast<llvm::GlobalValue>(Val)->getAddressSpace();
0620   }
0621   bool hasGlobalUnnamedAddr() const {
0622     return cast<llvm::GlobalValue>(Val)->hasGlobalUnnamedAddr();
0623   }
0624 
0625   /// Returns true if this value's address is not significant in this module.
0626   /// This attribute is intended to be used only by the code generator and LTO
0627   /// to allow the linker to decide whether the global needs to be in the symbol
0628   /// table. It should probably not be used in optimizations, as the value may
0629   /// have uses outside the module; use hasGlobalUnnamedAddr() instead.
0630   bool hasAtLeastLocalUnnamedAddr() const {
0631     return cast<llvm::GlobalValue>(Val)->hasAtLeastLocalUnnamedAddr();
0632   }
0633 
0634   using UnnamedAddr = llvm::GlobalValue::UnnamedAddr;
0635 
0636   UnnamedAddr getUnnamedAddr() const {
0637     return cast<llvm::GlobalValue>(Val)->getUnnamedAddr();
0638   }
0639   void setUnnamedAddr(UnnamedAddr V);
0640 
0641   static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) {
0642     return llvm::GlobalValue::getMinUnnamedAddr(A, B);
0643   }
0644 
0645   bool hasComdat() const { return cast<llvm::GlobalValue>(Val)->hasComdat(); }
0646 
0647   // TODO: We need a SandboxIR Comdat if we want to implement getComdat().
0648   using VisibilityTypes = llvm::GlobalValue::VisibilityTypes;
0649   VisibilityTypes getVisibility() const {
0650     return cast<llvm::GlobalValue>(Val)->getVisibility();
0651   }
0652   bool hasDefaultVisibility() const {
0653     return cast<llvm::GlobalValue>(Val)->hasDefaultVisibility();
0654   }
0655   bool hasHiddenVisibility() const {
0656     return cast<llvm::GlobalValue>(Val)->hasHiddenVisibility();
0657   }
0658   bool hasProtectedVisibility() const {
0659     return cast<llvm::GlobalValue>(Val)->hasProtectedVisibility();
0660   }
0661   void setVisibility(VisibilityTypes V);
0662 
0663   // TODO: Add missing functions.
0664 };
0665 
0666 class GlobalObject : public GlobalValue {
0667 protected:
0668   GlobalObject(ClassID ID, llvm::GlobalObject *C, Context &Ctx)
0669       : GlobalValue(ID, C, Ctx) {}
0670   friend class Context; // For constructor.
0671   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
0672     return getOperandUseDefault(OpIdx, Verify);
0673   }
0674 
0675 public:
0676   unsigned getUseOperandNo(const Use &Use) const final {
0677     return getUseOperandNoDefault(Use);
0678   }
0679   /// For isa/dyn_cast.
0680   static bool classof(const sandboxir::Value *From) {
0681     switch (From->getSubclassID()) {
0682     case ClassID::Function:
0683     case ClassID::GlobalVariable:
0684     case ClassID::GlobalIFunc:
0685       return true;
0686     default:
0687       return false;
0688     }
0689   }
0690 
0691   /// FIXME: Remove this function once transition to Align is over.
0692   uint64_t getAlignment() const {
0693     return cast<llvm::GlobalObject>(Val)->getAlignment();
0694   }
0695 
0696   /// Returns the alignment of the given variable or function.
0697   ///
0698   /// Note that for functions this is the alignment of the code, not the
0699   /// alignment of a function pointer.
0700   MaybeAlign getAlign() const {
0701     return cast<llvm::GlobalObject>(Val)->getAlign();
0702   }
0703 
0704   // TODO: Add missing: setAlignment(Align)
0705 
0706   /// Sets the alignment attribute of the GlobalObject.
0707   /// This method will be deprecated as the alignment property should always be
0708   /// defined.
0709   void setAlignment(MaybeAlign Align);
0710 
0711   unsigned getGlobalObjectSubClassData() const {
0712     return cast<llvm::GlobalObject>(Val)->getGlobalObjectSubClassData();
0713   }
0714 
0715   void setGlobalObjectSubClassData(unsigned V);
0716 
0717   /// Check if this global has a custom object file section.
0718   ///
0719   /// This is more efficient than calling getSection() and checking for an empty
0720   /// string.
0721   bool hasSection() const {
0722     return cast<llvm::GlobalObject>(Val)->hasSection();
0723   }
0724 
0725   /// Get the custom section of this global if it has one.
0726   ///
0727   /// If this global does not have a custom section, this will be empty and the
0728   /// default object file section (.text, .data, etc) will be used.
0729   StringRef getSection() const {
0730     return cast<llvm::GlobalObject>(Val)->getSection();
0731   }
0732 
0733   /// Change the section for this global.
0734   ///
0735   /// Setting the section to the empty string tells LLVM to choose an
0736   /// appropriate default object file section.
0737   void setSection(StringRef S);
0738 
0739   bool hasComdat() const { return cast<llvm::GlobalObject>(Val)->hasComdat(); }
0740 
0741   // TODO: implement get/setComdat(), etc. once we have a sandboxir::Comdat.
0742 
0743   // TODO: We currently don't support Metadata in sandboxir so all
0744   // Metadata-related functions are missing.
0745 
0746   using VCallVisibility = llvm::GlobalObject::VCallVisibility;
0747 
0748   VCallVisibility getVCallVisibility() const {
0749     return cast<llvm::GlobalObject>(Val)->getVCallVisibility();
0750   }
0751 
0752   /// Returns true if the alignment of the value can be unilaterally
0753   /// increased.
0754   ///
0755   /// Note that for functions this is the alignment of the code, not the
0756   /// alignment of a function pointer.
0757   bool canIncreaseAlignment() const {
0758     return cast<llvm::GlobalObject>(Val)->canIncreaseAlignment();
0759   }
0760 };
0761 
0762 /// Provides API functions, like getIterator() and getReverseIterator() to
0763 /// GlobalIFunc, Function, GlobalVariable and GlobalAlias. In LLVM IR these are
0764 /// provided by ilist_node.
0765 template <typename GlobalT, typename LLVMGlobalT, typename ParentT,
0766           typename LLVMParentT>
0767 class GlobalWithNodeAPI : public ParentT {
0768   /// Helper for mapped_iterator.
0769   struct LLVMGVToGV {
0770     Context &Ctx;
0771     LLVMGVToGV(Context &Ctx) : Ctx(Ctx) {}
0772     GlobalT &operator()(LLVMGlobalT &LLVMGV) const;
0773   };
0774 
0775 public:
0776   GlobalWithNodeAPI(Value::ClassID ID, LLVMParentT *C, Context &Ctx)
0777       : ParentT(ID, C, Ctx) {}
0778 
0779   Module *getParent() const {
0780     llvm::Module *LLVMM = cast<LLVMGlobalT>(this->Val)->getParent();
0781     return this->Ctx.getModule(LLVMM);
0782   }
0783 
0784   using iterator = mapped_iterator<
0785       decltype(static_cast<LLVMGlobalT *>(nullptr)->getIterator()), LLVMGVToGV>;
0786   using reverse_iterator = mapped_iterator<
0787       decltype(static_cast<LLVMGlobalT *>(nullptr)->getReverseIterator()),
0788       LLVMGVToGV>;
0789   iterator getIterator() const {
0790     auto *LLVMGV = cast<LLVMGlobalT>(this->Val);
0791     LLVMGVToGV ToGV(this->Ctx);
0792     return map_iterator(LLVMGV->getIterator(), ToGV);
0793   }
0794   reverse_iterator getReverseIterator() const {
0795     auto *LLVMGV = cast<LLVMGlobalT>(this->Val);
0796     LLVMGVToGV ToGV(this->Ctx);
0797     return map_iterator(LLVMGV->getReverseIterator(), ToGV);
0798   }
0799 };
0800 
0801 // These are needed for SandboxIRTest when building with LLVM_BUILD_LLVM_DYLIB
0802 extern template LLVM_TEMPLATE_ABI GlobalIFunc &
0803 GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject,
0804                   llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalIFunc
0805                                                                   &LLVMGV)
0806     const;
0807 extern template LLVM_TEMPLATE_ABI Function &
0808 GlobalWithNodeAPI<Function, llvm::Function, GlobalObject, llvm::GlobalObject>::
0809     LLVMGVToGV::operator()(llvm::Function &LLVMGV) const;
0810 
0811 extern template LLVM_TEMPLATE_ABI GlobalVariable &GlobalWithNodeAPI<
0812     GlobalVariable, llvm::GlobalVariable, GlobalObject,
0813     llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalVariable &LLVMGV)
0814     const;
0815 extern template LLVM_TEMPLATE_ABI GlobalAlias &
0816 GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue,
0817                   llvm::GlobalValue>::LLVMGVToGV::operator()(llvm::GlobalAlias
0818                                                                  &LLVMGV) const;
0819 
0820 class GlobalIFunc final
0821     : public GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject,
0822                                llvm::GlobalObject> {
0823   GlobalIFunc(llvm::GlobalObject *C, Context &Ctx)
0824       : GlobalWithNodeAPI(ClassID::GlobalIFunc, C, Ctx) {}
0825   friend class Context; // For constructor.
0826 
0827 public:
0828   /// For isa/dyn_cast.
0829   static bool classof(const sandboxir::Value *From) {
0830     return From->getSubclassID() == ClassID::GlobalIFunc;
0831   }
0832 
0833   // TODO: Missing create() because we don't have a sandboxir::Module yet.
0834 
0835   // TODO: Missing functions: copyAttributesFrom(), removeFromParent(),
0836   // eraseFromParent()
0837 
0838   void setResolver(Constant *Resolver);
0839 
0840   Constant *getResolver() const;
0841 
0842   // Return the resolver function after peeling off potential ConstantExpr
0843   // indirection.
0844   Function *getResolverFunction();
0845   const Function *getResolverFunction() const {
0846     return const_cast<GlobalIFunc *>(this)->getResolverFunction();
0847   }
0848 
0849   static bool isValidLinkage(LinkageTypes L) {
0850     return llvm::GlobalIFunc::isValidLinkage(L);
0851   }
0852 
0853   // TODO: Missing applyAlongResolverPath().
0854 
0855 #ifndef NDEBUG
0856   void verify() const override {
0857     assert(isa<llvm::GlobalIFunc>(Val) && "Expected a GlobalIFunc!");
0858   }
0859   void dumpOS(raw_ostream &OS) const override {
0860     dumpCommonPrefix(OS);
0861     dumpCommonSuffix(OS);
0862   }
0863 #endif
0864 };
0865 
0866 class GlobalVariable final
0867     : public GlobalWithNodeAPI<GlobalVariable, llvm::GlobalVariable,
0868                                GlobalObject, llvm::GlobalObject> {
0869   GlobalVariable(llvm::GlobalObject *C, Context &Ctx)
0870       : GlobalWithNodeAPI(ClassID::GlobalVariable, C, Ctx) {}
0871   friend class Context; // For constructor.
0872 
0873   /// Helper for mapped_iterator.
0874   struct LLVMGVToGV {
0875     Context &Ctx;
0876     LLVMGVToGV(Context &Ctx) : Ctx(Ctx) {}
0877     GlobalVariable &operator()(llvm::GlobalVariable &LLVMGV) const;
0878   };
0879 
0880 public:
0881   /// For isa/dyn_cast.
0882   static bool classof(const sandboxir::Value *From) {
0883     return From->getSubclassID() == ClassID::GlobalVariable;
0884   }
0885 
0886   /// Definitions have initializers, declarations don't.
0887   ///
0888   inline bool hasInitializer() const {
0889     return cast<llvm::GlobalVariable>(Val)->hasInitializer();
0890   }
0891 
0892   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
0893   /// and any other instances of the global (this can happen due to weak
0894   /// linkage) are guaranteed to have the same initializer.
0895   ///
0896   /// Note that if you want to transform a global, you must use
0897   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
0898   ///
0899   /// Example:
0900   ///
0901   /// @a = global SomeType* null - Initializer is both definitive and unique.
0902   ///
0903   /// @b = global weak SomeType* null - Initializer is neither definitive nor
0904   /// unique.
0905   ///
0906   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
0907   /// unique.
0908   inline bool hasDefinitiveInitializer() const {
0909     return cast<llvm::GlobalVariable>(Val)->hasDefinitiveInitializer();
0910   }
0911 
0912   /// hasUniqueInitializer - Whether the global variable has an initializer, and
0913   /// any changes made to the initializer will turn up in the final executable.
0914   inline bool hasUniqueInitializer() const {
0915     return cast<llvm::GlobalVariable>(Val)->hasUniqueInitializer();
0916   }
0917 
0918   /// getInitializer - Return the initializer for this global variable.  It is
0919   /// illegal to call this method if the global is external, because we cannot
0920   /// tell what the value is initialized to!
0921   ///
0922   Constant *getInitializer() const;
0923   /// setInitializer - Sets the initializer for this global variable, removing
0924   /// any existing initializer if InitVal==NULL. The initializer must have the
0925   /// type getValueType().
0926   void setInitializer(Constant *InitVal);
0927 
0928   // TODO: Add missing replaceInitializer(). Requires special tracker
0929 
0930   /// If the value is a global constant, its value is immutable throughout the
0931   /// runtime execution of the program.  Assigning a value into the constant
0932   /// leads to undefined behavior.
0933   ///
0934   bool isConstant() const {
0935     return cast<llvm::GlobalVariable>(Val)->isConstant();
0936   }
0937   void setConstant(bool V);
0938 
0939   bool isExternallyInitialized() const {
0940     return cast<llvm::GlobalVariable>(Val)->isExternallyInitialized();
0941   }
0942   void setExternallyInitialized(bool Val);
0943 
0944   // TODO: Missing copyAttributesFrom()
0945 
0946   // TODO: Missing removeFromParent(), eraseFromParent(), dropAllReferences()
0947 
0948   // TODO: Missing addDebugInfo(), getDebugInfo()
0949 
0950   // TODO: Missing attribute setter functions: addAttribute(), setAttributes().
0951   //       There seems to be no removeAttribute() so we can't undo them.
0952 
0953   /// Return true if the attribute exists.
0954   bool hasAttribute(Attribute::AttrKind Kind) const {
0955     return cast<llvm::GlobalVariable>(Val)->hasAttribute(Kind);
0956   }
0957 
0958   /// Return true if the attribute exists.
0959   bool hasAttribute(StringRef Kind) const {
0960     return cast<llvm::GlobalVariable>(Val)->hasAttribute(Kind);
0961   }
0962 
0963   /// Return true if any attributes exist.
0964   bool hasAttributes() const {
0965     return cast<llvm::GlobalVariable>(Val)->hasAttributes();
0966   }
0967 
0968   /// Return the attribute object.
0969   Attribute getAttribute(Attribute::AttrKind Kind) const {
0970     return cast<llvm::GlobalVariable>(Val)->getAttribute(Kind);
0971   }
0972 
0973   /// Return the attribute object.
0974   Attribute getAttribute(StringRef Kind) const {
0975     return cast<llvm::GlobalVariable>(Val)->getAttribute(Kind);
0976   }
0977 
0978   /// Return the attribute set for this global
0979   AttributeSet getAttributes() const {
0980     return cast<llvm::GlobalVariable>(Val)->getAttributes();
0981   }
0982 
0983   /// Return attribute set as list with index.
0984   /// FIXME: This may not be required once ValueEnumerators
0985   /// in bitcode-writer can enumerate attribute-set.
0986   AttributeList getAttributesAsList(unsigned Index) const {
0987     return cast<llvm::GlobalVariable>(Val)->getAttributesAsList(Index);
0988   }
0989 
0990   /// Check if section name is present
0991   bool hasImplicitSection() const {
0992     return cast<llvm::GlobalVariable>(Val)->hasImplicitSection();
0993   }
0994 
0995   /// Get the custom code model raw value of this global.
0996   ///
0997   unsigned getCodeModelRaw() const {
0998     return cast<llvm::GlobalVariable>(Val)->getCodeModelRaw();
0999   }
1000 
1001   /// Get the custom code model of this global if it has one.
1002   ///
1003   /// If this global does not have a custom code model, the empty instance
1004   /// will be returned.
1005   std::optional<CodeModel::Model> getCodeModel() const {
1006     return cast<llvm::GlobalVariable>(Val)->getCodeModel();
1007   }
1008 
1009   // TODO: Missing setCodeModel(). Requires custom tracker.
1010 
1011 #ifndef NDEBUG
1012   void verify() const override {
1013     assert(isa<llvm::GlobalVariable>(Val) && "Expected a GlobalVariable!");
1014   }
1015   void dumpOS(raw_ostream &OS) const override {
1016     dumpCommonPrefix(OS);
1017     dumpCommonSuffix(OS);
1018   }
1019 #endif
1020 };
1021 
1022 class GlobalAlias final
1023     : public GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue,
1024                                llvm::GlobalValue> {
1025   GlobalAlias(llvm::GlobalAlias *C, Context &Ctx)
1026       : GlobalWithNodeAPI(ClassID::GlobalAlias, C, Ctx) {}
1027   friend class Context; // For constructor.
1028 
1029 public:
1030   /// For isa/dyn_cast.
1031   static bool classof(const sandboxir::Value *From) {
1032     return From->getSubclassID() == ClassID::GlobalAlias;
1033   }
1034 
1035   // TODO: Missing create() due to unimplemented sandboxir::Module.
1036 
1037   // TODO: Missing copyAttributresFrom().
1038   // TODO: Missing removeFromParent(), eraseFromParent().
1039 
1040   void setAliasee(Constant *Aliasee);
1041   Constant *getAliasee() const;
1042 
1043   const GlobalObject *getAliaseeObject() const;
1044   GlobalObject *getAliaseeObject() {
1045     return const_cast<GlobalObject *>(
1046         static_cast<const GlobalAlias *>(this)->getAliaseeObject());
1047   }
1048 
1049   static bool isValidLinkage(LinkageTypes L) {
1050     return llvm::GlobalAlias::isValidLinkage(L);
1051   }
1052 };
1053 
1054 class NoCFIValue final : public Constant {
1055   NoCFIValue(llvm::NoCFIValue *C, Context &Ctx)
1056       : Constant(ClassID::NoCFIValue, C, Ctx) {}
1057   friend class Context; // For constructor.
1058 
1059   Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
1060     return getOperandUseDefault(OpIdx, Verify);
1061   }
1062 
1063 public:
1064   /// Return a NoCFIValue for the specified function.
1065   static NoCFIValue *get(GlobalValue *GV);
1066 
1067   GlobalValue *getGlobalValue() const;
1068 
1069   /// NoCFIValue is always a pointer.
1070   PointerType *getType() const;
1071   /// For isa/dyn_cast.
1072   static bool classof(const sandboxir::Value *From) {
1073     return From->getSubclassID() == ClassID::NoCFIValue;
1074   }
1075 
1076   unsigned getUseOperandNo(const Use &Use) const final {
1077     return getUseOperandNoDefault(Use);
1078   }
1079 
1080 #ifndef NDEBUG
1081   void verify() const override {
1082     assert(isa<llvm::NoCFIValue>(Val) && "Expected a NoCFIValue!");
1083   }
1084   void dumpOS(raw_ostream &OS) const override {
1085     dumpCommonPrefix(OS);
1086     dumpCommonSuffix(OS);
1087   }
1088 #endif
1089 };
1090 
1091 class ConstantPtrAuth final : public Constant {
1092   ConstantPtrAuth(llvm::ConstantPtrAuth *C, Context &Ctx)
1093       : Constant(ClassID::ConstantPtrAuth, C, Ctx) {}
1094   friend class Context; // For constructor.
1095 
1096 public:
1097   /// Return a pointer signed with the specified parameters.
1098   static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1099                               ConstantInt *Disc, Constant *AddrDisc);
1100   /// The pointer that is signed in this ptrauth signed pointer.
1101   Constant *getPointer() const;
1102 
1103   /// The Key ID, an i32 constant.
1104   ConstantInt *getKey() const;
1105 
1106   /// The integer discriminator, an i64 constant, or 0.
1107   ConstantInt *getDiscriminator() const;
1108 
1109   /// The address discriminator if any, or the null constant.
1110   /// If present, this must be a value equivalent to the storage location of
1111   /// the only global-initializer user of the ptrauth signed pointer.
1112   Constant *getAddrDiscriminator() const;
1113 
1114   /// Whether there is any non-null address discriminator.
1115   bool hasAddressDiscriminator() const {
1116     return cast<llvm::ConstantPtrAuth>(Val)->hasAddressDiscriminator();
1117   }
1118 
1119   /// Whether the address uses a special address discriminator.
1120   /// These discriminators can't be used in real pointer-auth values; they
1121   /// can only be used in "prototype" values that indicate how some real
1122   /// schema is supposed to be produced.
1123   bool hasSpecialAddressDiscriminator(uint64_t Value) const {
1124     return cast<llvm::ConstantPtrAuth>(Val)->hasSpecialAddressDiscriminator(
1125         Value);
1126   }
1127 
1128   /// Check whether an authentication operation with key \p Key and (possibly
1129   /// blended) discriminator \p Discriminator is known to be compatible with
1130   /// this ptrauth signed pointer.
1131   bool isKnownCompatibleWith(const Value *Key, const Value *Discriminator,
1132                              const DataLayout &DL) const {
1133     return cast<llvm::ConstantPtrAuth>(Val)->isKnownCompatibleWith(
1134         Key->Val, Discriminator->Val, DL);
1135   }
1136 
1137   /// Produce a new ptrauth expression signing the given value using
1138   /// the same schema as is stored in one.
1139   ConstantPtrAuth *getWithSameSchema(Constant *Pointer) const;
1140 
1141   /// For isa/dyn_cast.
1142   static bool classof(const sandboxir::Value *From) {
1143     return From->getSubclassID() == ClassID::ConstantPtrAuth;
1144   }
1145 };
1146 
1147 class ConstantExpr : public Constant {
1148   ConstantExpr(llvm::ConstantExpr *C, Context &Ctx)
1149       : Constant(ClassID::ConstantExpr, C, Ctx) {}
1150   friend class Context; // For constructor.
1151 
1152 public:
1153   /// For isa/dyn_cast.
1154   static bool classof(const sandboxir::Value *From) {
1155     return From->getSubclassID() == ClassID::ConstantExpr;
1156   }
1157   // TODO: Missing functions.
1158 };
1159 
1160 class BlockAddress final : public Constant {
1161   BlockAddress(llvm::BlockAddress *C, Context &Ctx)
1162       : Constant(ClassID::BlockAddress, C, Ctx) {}
1163   friend class Context; // For constructor.
1164 
1165 public:
1166   /// Return a BlockAddress for the specified function and basic block.
1167   static BlockAddress *get(Function *F, BasicBlock *BB);
1168 
1169   /// Return a BlockAddress for the specified basic block.  The basic
1170   /// block must be embedded into a function.
1171   static BlockAddress *get(BasicBlock *BB);
1172 
1173   /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
1174   ///
1175   /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
1176   static BlockAddress *lookup(const BasicBlock *BB);
1177 
1178   Function *getFunction() const;
1179   BasicBlock *getBasicBlock() const;
1180 
1181   /// For isa/dyn_cast.
1182   static bool classof(const sandboxir::Value *From) {
1183     return From->getSubclassID() == ClassID::BlockAddress;
1184   }
1185 };
1186 
1187 class DSOLocalEquivalent final : public Constant {
1188   DSOLocalEquivalent(llvm::DSOLocalEquivalent *C, Context &Ctx)
1189       : Constant(ClassID::DSOLocalEquivalent, C, Ctx) {}
1190   friend class Context; // For constructor.
1191 
1192 public:
1193   /// Return a DSOLocalEquivalent for the specified global value.
1194   static DSOLocalEquivalent *get(GlobalValue *GV);
1195 
1196   GlobalValue *getGlobalValue() const;
1197 
1198   /// For isa/dyn_cast.
1199   static bool classof(const sandboxir::Value *From) {
1200     return From->getSubclassID() == ClassID::DSOLocalEquivalent;
1201   }
1202 
1203   unsigned getUseOperandNo(const Use &Use) const final {
1204     llvm_unreachable("DSOLocalEquivalent has no operands!");
1205   }
1206 
1207 #ifndef NDEBUG
1208   void verify() const override {
1209     assert(isa<llvm::DSOLocalEquivalent>(Val) &&
1210            "Expected a DSOLocalEquivalent!");
1211   }
1212   void dumpOS(raw_ostream &OS) const override {
1213     dumpCommonPrefix(OS);
1214     dumpCommonSuffix(OS);
1215   }
1216 #endif
1217 };
1218 
1219 // TODO: This should inherit from ConstantData.
1220 class ConstantTokenNone final : public Constant {
1221   ConstantTokenNone(llvm::ConstantTokenNone *C, Context &Ctx)
1222       : Constant(ClassID::ConstantTokenNone, C, Ctx) {}
1223   friend class Context; // For constructor.
1224 
1225 public:
1226   /// Return the ConstantTokenNone.
1227   static ConstantTokenNone *get(Context &Ctx);
1228 
1229   /// For isa/dyn_cast.
1230   static bool classof(const sandboxir::Value *From) {
1231     return From->getSubclassID() == ClassID::ConstantTokenNone;
1232   }
1233 
1234   unsigned getUseOperandNo(const Use &Use) const final {
1235     llvm_unreachable("ConstantTokenNone has no operands!");
1236   }
1237 
1238 #ifndef NDEBUG
1239   void verify() const override {
1240     assert(isa<llvm::ConstantTokenNone>(Val) &&
1241            "Expected a ConstantTokenNone!");
1242   }
1243   void dumpOS(raw_ostream &OS) const override {
1244     dumpCommonPrefix(OS);
1245     dumpCommonSuffix(OS);
1246   }
1247 #endif
1248 };
1249 
1250 } // namespace llvm::sandboxir
1251 
1252 #endif // LLVM_SANDBOXIR_CONSTANT_H