File indexing completed on 2026-05-10 08:43:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef LLVM_IR_CONSTANTS_H
0021 #define LLVM_IR_CONSTANTS_H
0022
0023 #include "llvm/ADT/APFloat.h"
0024 #include "llvm/ADT/APInt.h"
0025 #include "llvm/ADT/ArrayRef.h"
0026 #include "llvm/ADT/STLExtras.h"
0027 #include "llvm/ADT/StringRef.h"
0028 #include "llvm/IR/Constant.h"
0029 #include "llvm/IR/ConstantRange.h"
0030 #include "llvm/IR/DerivedTypes.h"
0031 #include "llvm/IR/GEPNoWrapFlags.h"
0032 #include "llvm/IR/Intrinsics.h"
0033 #include "llvm/IR/OperandTraits.h"
0034 #include "llvm/IR/User.h"
0035 #include "llvm/IR/Value.h"
0036 #include "llvm/Support/Casting.h"
0037 #include "llvm/Support/Compiler.h"
0038 #include "llvm/Support/ErrorHandling.h"
0039 #include <cassert>
0040 #include <cstddef>
0041 #include <cstdint>
0042 #include <optional>
0043
0044 namespace llvm {
0045
0046 template <class ConstantClass> struct ConstantAggrKeyType;
0047
0048
0049
0050
0051
0052
0053 class ConstantData : public Constant {
0054 constexpr static IntrusiveOperandsAllocMarker AllocMarker{0};
0055
0056 friend class Constant;
0057
0058 Value *handleOperandChangeImpl(Value *From, Value *To) {
0059 llvm_unreachable("Constant data does not have operands!");
0060 }
0061
0062 protected:
0063 explicit ConstantData(Type *Ty, ValueTy VT) : Constant(Ty, VT, AllocMarker) {}
0064
0065 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0066
0067 public:
0068 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0069
0070 ConstantData(const ConstantData &) = delete;
0071
0072
0073 static bool classof(const Value *V) {
0074 return V->getValueID() >= ConstantDataFirstVal &&
0075 V->getValueID() <= ConstantDataLastVal;
0076 }
0077 };
0078
0079
0080
0081
0082
0083 class ConstantInt final : public ConstantData {
0084 friend class Constant;
0085 friend class ConstantVector;
0086
0087 APInt Val;
0088
0089 ConstantInt(Type *Ty, const APInt &V);
0090
0091 void destroyConstantImpl();
0092
0093
0094
0095
0096 static ConstantInt *get(LLVMContext &Context, ElementCount EC,
0097 const APInt &V);
0098
0099 public:
0100 ConstantInt(const ConstantInt &) = delete;
0101
0102 static ConstantInt *getTrue(LLVMContext &Context);
0103 static ConstantInt *getFalse(LLVMContext &Context);
0104 static ConstantInt *getBool(LLVMContext &Context, bool V);
0105 static Constant *getTrue(Type *Ty);
0106 static Constant *getFalse(Type *Ty);
0107 static Constant *getBool(Type *Ty, bool V);
0108
0109
0110
0111 static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false);
0112
0113
0114
0115
0116
0117
0118
0119 static ConstantInt *get(IntegerType *Ty, uint64_t V, bool IsSigned = false);
0120
0121
0122
0123
0124
0125
0126 static ConstantInt *getSigned(IntegerType *Ty, int64_t V) {
0127 return get(Ty, V, true);
0128 }
0129 static Constant *getSigned(Type *Ty, int64_t V) {
0130 return get(Ty, V, true);
0131 }
0132
0133
0134
0135 static ConstantInt *get(LLVMContext &Context, const APInt &V);
0136
0137
0138
0139 static ConstantInt *get(IntegerType *Ty, StringRef Str, uint8_t Radix);
0140
0141
0142
0143 static Constant *get(Type *Ty, const APInt &V);
0144
0145
0146
0147
0148 inline const APInt &getValue() const { return Val; }
0149
0150
0151 unsigned getBitWidth() const { return Val.getBitWidth(); }
0152
0153
0154
0155
0156
0157 inline uint64_t getZExtValue() const { return Val.getZExtValue(); }
0158
0159
0160
0161
0162
0163 inline int64_t getSExtValue() const { return Val.getSExtValue(); }
0164
0165
0166
0167
0168 inline MaybeAlign getMaybeAlignValue() const {
0169 return MaybeAlign(getZExtValue());
0170 }
0171
0172
0173
0174
0175 inline Align getAlignValue() const {
0176 return getMaybeAlignValue().valueOrOne();
0177 }
0178
0179
0180
0181
0182
0183 bool equalsInt(uint64_t V) const { return Val == V; }
0184
0185
0186
0187 inline IntegerType *getIntegerType() const {
0188 return cast<IntegerType>(Value::getType());
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 static bool isValueValidForType(Type *Ty, uint64_t V);
0201 static bool isValueValidForType(Type *Ty, int64_t V);
0202
0203 bool isNegative() const { return Val.isNegative(); }
0204
0205
0206
0207
0208 bool isZero() const { return Val.isZero(); }
0209
0210
0211
0212
0213
0214 bool isOne() const { return Val.isOne(); }
0215
0216
0217
0218
0219
0220 bool isMinusOne() const { return Val.isAllOnes(); }
0221
0222
0223
0224
0225
0226
0227 bool isMaxValue(bool IsSigned) const {
0228 if (IsSigned)
0229 return Val.isMaxSignedValue();
0230 else
0231 return Val.isMaxValue();
0232 }
0233
0234
0235
0236
0237
0238
0239 bool isMinValue(bool IsSigned) const {
0240 if (IsSigned)
0241 return Val.isMinSignedValue();
0242 else
0243 return Val.isMinValue();
0244 }
0245
0246
0247
0248
0249
0250
0251 bool uge(uint64_t Num) const { return Val.uge(Num); }
0252
0253
0254
0255
0256
0257
0258 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
0259 return Val.getLimitedValue(Limit);
0260 }
0261
0262
0263 static bool classof(const Value *V) {
0264 return V->getValueID() == ConstantIntVal;
0265 }
0266 };
0267
0268
0269
0270
0271 class ConstantFP final : public ConstantData {
0272 friend class Constant;
0273 friend class ConstantVector;
0274
0275 APFloat Val;
0276
0277 ConstantFP(Type *Ty, const APFloat &V);
0278
0279 void destroyConstantImpl();
0280
0281
0282
0283
0284 static ConstantFP *get(LLVMContext &Context, ElementCount EC,
0285 const APFloat &V);
0286
0287 public:
0288 ConstantFP(const ConstantFP &) = delete;
0289
0290
0291
0292
0293
0294 static Constant *get(Type *Ty, double V);
0295
0296
0297
0298 static Constant *get(Type *Ty, const APFloat &V);
0299
0300 static Constant *get(Type *Ty, StringRef Str);
0301 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
0302 static Constant *getNaN(Type *Ty, bool Negative = false,
0303 uint64_t Payload = 0);
0304 static Constant *getQNaN(Type *Ty, bool Negative = false,
0305 APInt *Payload = nullptr);
0306 static Constant *getSNaN(Type *Ty, bool Negative = false,
0307 APInt *Payload = nullptr);
0308 static Constant *getZero(Type *Ty, bool Negative = false);
0309 static Constant *getNegativeZero(Type *Ty) { return getZero(Ty, true); }
0310 static Constant *getInfinity(Type *Ty, bool Negative = false);
0311
0312
0313 static bool isValueValidForType(Type *Ty, const APFloat &V);
0314 inline const APFloat &getValueAPF() const { return Val; }
0315 inline const APFloat &getValue() const { return Val; }
0316
0317
0318 bool isZero() const { return Val.isZero(); }
0319
0320
0321 bool isNegative() const { return Val.isNegative(); }
0322
0323
0324 bool isInfinity() const { return Val.isInfinity(); }
0325
0326
0327 bool isNaN() const { return Val.isNaN(); }
0328
0329
0330
0331
0332
0333
0334
0335 bool isExactlyValue(const APFloat &V) const;
0336
0337 bool isExactlyValue(double V) const {
0338 bool ignored;
0339 APFloat FV(V);
0340 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
0341 return isExactlyValue(FV);
0342 }
0343
0344
0345 static bool classof(const Value *V) {
0346 return V->getValueID() == ConstantFPVal;
0347 }
0348 };
0349
0350
0351
0352
0353 class ConstantAggregateZero final : public ConstantData {
0354 friend class Constant;
0355
0356 explicit ConstantAggregateZero(Type *Ty)
0357 : ConstantData(Ty, ConstantAggregateZeroVal) {}
0358
0359 void destroyConstantImpl();
0360
0361 public:
0362 ConstantAggregateZero(const ConstantAggregateZero &) = delete;
0363
0364 static ConstantAggregateZero *get(Type *Ty);
0365
0366
0367
0368 Constant *getSequentialElement() const;
0369
0370
0371
0372 Constant *getStructElement(unsigned Elt) const;
0373
0374
0375
0376 Constant *getElementValue(Constant *C) const;
0377
0378
0379 Constant *getElementValue(unsigned Idx) const;
0380
0381
0382 ElementCount getElementCount() const;
0383
0384
0385
0386 static bool classof(const Value *V) {
0387 return V->getValueID() == ConstantAggregateZeroVal;
0388 }
0389 };
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402 class ConstantAggregate : public Constant {
0403 protected:
0404 ConstantAggregate(Type *T, ValueTy VT, ArrayRef<Constant *> V,
0405 AllocInfo AllocInfo);
0406
0407 public:
0408
0409 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
0410
0411
0412 static bool classof(const Value *V) {
0413 return V->getValueID() >= ConstantAggregateFirstVal &&
0414 V->getValueID() <= ConstantAggregateLastVal;
0415 }
0416 };
0417
0418 template <>
0419 struct OperandTraits<ConstantAggregate>
0420 : public VariadicOperandTraits<ConstantAggregate> {};
0421
0422 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantAggregate, Constant)
0423
0424
0425
0426
0427 class ConstantArray final : public ConstantAggregate {
0428 friend struct ConstantAggrKeyType<ConstantArray>;
0429 friend class Constant;
0430
0431 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val, AllocInfo AllocInfo);
0432
0433 void destroyConstantImpl();
0434 Value *handleOperandChangeImpl(Value *From, Value *To);
0435
0436 public:
0437
0438 static Constant *get(ArrayType *T, ArrayRef<Constant *> V);
0439
0440 private:
0441 static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
0442
0443 public:
0444
0445
0446 inline ArrayType *getType() const {
0447 return cast<ArrayType>(Value::getType());
0448 }
0449
0450
0451 static bool classof(const Value *V) {
0452 return V->getValueID() == ConstantArrayVal;
0453 }
0454 };
0455
0456
0457
0458
0459 class ConstantStruct final : public ConstantAggregate {
0460 friend struct ConstantAggrKeyType<ConstantStruct>;
0461 friend class Constant;
0462
0463 ConstantStruct(StructType *T, ArrayRef<Constant *> Val, AllocInfo AllocInfo);
0464
0465 void destroyConstantImpl();
0466 Value *handleOperandChangeImpl(Value *From, Value *To);
0467
0468 public:
0469
0470 static Constant *get(StructType *T, ArrayRef<Constant *> V);
0471
0472 template <typename... Csts>
0473 static std::enable_if_t<are_base_of<Constant, Csts...>::value, Constant *>
0474 get(StructType *T, Csts *...Vs) {
0475 return get(T, ArrayRef<Constant *>({Vs...}));
0476 }
0477
0478
0479
0480 static Constant *getAnon(ArrayRef<Constant *> V, bool Packed = false) {
0481 return get(getTypeForElements(V, Packed), V);
0482 }
0483 static Constant *getAnon(LLVMContext &Ctx, ArrayRef<Constant *> V,
0484 bool Packed = false) {
0485 return get(getTypeForElements(Ctx, V, Packed), V);
0486 }
0487
0488
0489
0490 static StructType *getTypeForElements(ArrayRef<Constant *> V,
0491 bool Packed = false);
0492
0493 static StructType *getTypeForElements(LLVMContext &Ctx,
0494 ArrayRef<Constant *> V,
0495 bool Packed = false);
0496
0497
0498 inline StructType *getType() const {
0499 return cast<StructType>(Value::getType());
0500 }
0501
0502
0503 static bool classof(const Value *V) {
0504 return V->getValueID() == ConstantStructVal;
0505 }
0506 };
0507
0508
0509
0510
0511 class ConstantVector final : public ConstantAggregate {
0512 friend struct ConstantAggrKeyType<ConstantVector>;
0513 friend class Constant;
0514
0515 ConstantVector(VectorType *T, ArrayRef<Constant *> Val, AllocInfo AllocInfo);
0516
0517 void destroyConstantImpl();
0518 Value *handleOperandChangeImpl(Value *From, Value *To);
0519
0520 public:
0521
0522 static Constant *get(ArrayRef<Constant *> V);
0523
0524 private:
0525 static Constant *getImpl(ArrayRef<Constant *> V);
0526
0527 public:
0528
0529
0530 static Constant *getSplat(ElementCount EC, Constant *Elt);
0531
0532
0533
0534 inline FixedVectorType *getType() const {
0535 return cast<FixedVectorType>(Value::getType());
0536 }
0537
0538
0539
0540
0541 Constant *getSplatValue(bool AllowPoison = false) const;
0542
0543
0544 static bool classof(const Value *V) {
0545 return V->getValueID() == ConstantVectorVal;
0546 }
0547 };
0548
0549
0550
0551
0552 class ConstantPointerNull final : public ConstantData {
0553 friend class Constant;
0554
0555 explicit ConstantPointerNull(PointerType *T)
0556 : ConstantData(T, Value::ConstantPointerNullVal) {}
0557
0558 void destroyConstantImpl();
0559
0560 public:
0561 ConstantPointerNull(const ConstantPointerNull &) = delete;
0562
0563
0564 static ConstantPointerNull *get(PointerType *T);
0565
0566
0567
0568 inline PointerType *getType() const {
0569 return cast<PointerType>(Value::getType());
0570 }
0571
0572
0573 static bool classof(const Value *V) {
0574 return V->getValueID() == ConstantPointerNullVal;
0575 }
0576 };
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 class ConstantDataSequential : public ConstantData {
0588 friend class LLVMContextImpl;
0589 friend class Constant;
0590
0591
0592
0593 const char *DataElements;
0594
0595
0596
0597
0598
0599 std::unique_ptr<ConstantDataSequential> Next;
0600
0601 void destroyConstantImpl();
0602
0603 protected:
0604 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
0605 : ConstantData(ty, VT), DataElements(Data) {}
0606
0607 static Constant *getImpl(StringRef Bytes, Type *Ty);
0608
0609 public:
0610 ConstantDataSequential(const ConstantDataSequential &) = delete;
0611
0612
0613
0614
0615
0616 static bool isElementTypeCompatible(Type *Ty);
0617
0618
0619
0620 uint64_t getElementAsInteger(unsigned i) const;
0621
0622
0623
0624 APInt getElementAsAPInt(unsigned i) const;
0625
0626
0627
0628 APFloat getElementAsAPFloat(unsigned i) const;
0629
0630
0631
0632 float getElementAsFloat(unsigned i) const;
0633
0634
0635
0636 double getElementAsDouble(unsigned i) const;
0637
0638
0639
0640
0641 Constant *getElementAsConstant(unsigned i) const;
0642
0643
0644 Type *getElementType() const;
0645
0646
0647 unsigned getNumElements() const;
0648
0649
0650
0651 uint64_t getElementByteSize() const;
0652
0653
0654 bool isString(unsigned CharSize = 8) const;
0655
0656
0657
0658 bool isCString() const;
0659
0660
0661
0662 StringRef getAsString() const {
0663 assert(isString() && "Not a string");
0664 return getRawDataValues();
0665 }
0666
0667
0668
0669 StringRef getAsCString() const {
0670 assert(isCString() && "Isn't a C string");
0671 StringRef Str = getAsString();
0672 return Str.substr(0, Str.size() - 1);
0673 }
0674
0675
0676
0677
0678 StringRef getRawDataValues() const;
0679
0680
0681 static bool classof(const Value *V) {
0682 return V->getValueID() == ConstantDataArrayVal ||
0683 V->getValueID() == ConstantDataVectorVal;
0684 }
0685
0686 private:
0687 const char *getElementPointer(unsigned Elt) const;
0688 };
0689
0690
0691
0692
0693
0694
0695
0696 class ConstantDataArray final : public ConstantDataSequential {
0697 friend class ConstantDataSequential;
0698
0699 explicit ConstantDataArray(Type *ty, const char *Data)
0700 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
0701
0702 public:
0703 ConstantDataArray(const ConstantDataArray &) = delete;
0704
0705
0706
0707
0708 template <typename ElementTy>
0709 static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
0710 const char *Data = reinterpret_cast<const char *>(Elts.data());
0711 return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
0712 Type::getScalarTy<ElementTy>(Context));
0713 }
0714
0715
0716
0717 template <typename ArrayTy>
0718 static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
0719 return ConstantDataArray::get(Context, ArrayRef(Elts));
0720 }
0721
0722
0723
0724
0725
0726
0727
0728 static Constant *getRaw(StringRef Data, uint64_t NumElements,
0729 Type *ElementTy) {
0730 Type *Ty = ArrayType::get(ElementTy, NumElements);
0731 return getImpl(Data, Ty);
0732 }
0733
0734
0735
0736
0737
0738
0739
0740 static Constant *getFP(Type *ElementType, ArrayRef<uint16_t> Elts);
0741 static Constant *getFP(Type *ElementType, ArrayRef<uint32_t> Elts);
0742 static Constant *getFP(Type *ElementType, ArrayRef<uint64_t> Elts);
0743
0744
0745
0746
0747
0748
0749 static Constant *getString(LLVMContext &Context, StringRef Initializer,
0750 bool AddNull = true);
0751
0752
0753
0754 inline ArrayType *getType() const {
0755 return cast<ArrayType>(Value::getType());
0756 }
0757
0758
0759 static bool classof(const Value *V) {
0760 return V->getValueID() == ConstantDataArrayVal;
0761 }
0762 };
0763
0764
0765
0766
0767
0768
0769
0770 class ConstantDataVector final : public ConstantDataSequential {
0771 friend class ConstantDataSequential;
0772
0773 explicit ConstantDataVector(Type *ty, const char *Data)
0774 : ConstantDataSequential(ty, ConstantDataVectorVal, Data),
0775 IsSplatSet(false) {}
0776
0777 mutable bool IsSplatSet : 1;
0778 mutable bool IsSplat : 1;
0779 bool isSplatData() const;
0780
0781 public:
0782 ConstantDataVector(const ConstantDataVector &) = delete;
0783
0784
0785
0786
0787 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
0788 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
0789 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
0790 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
0791 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
0792 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
0793
0794
0795
0796
0797
0798
0799
0800 static Constant *getRaw(StringRef Data, uint64_t NumElements,
0801 Type *ElementTy) {
0802 Type *Ty = VectorType::get(ElementTy, ElementCount::getFixed(NumElements));
0803 return getImpl(Data, Ty);
0804 }
0805
0806
0807
0808
0809
0810
0811
0812 static Constant *getFP(Type *ElementType, ArrayRef<uint16_t> Elts);
0813 static Constant *getFP(Type *ElementType, ArrayRef<uint32_t> Elts);
0814 static Constant *getFP(Type *ElementType, ArrayRef<uint64_t> Elts);
0815
0816
0817
0818
0819 static Constant *getSplat(unsigned NumElts, Constant *Elt);
0820
0821
0822
0823 bool isSplat() const;
0824
0825
0826
0827 Constant *getSplatValue() const;
0828
0829
0830
0831 inline FixedVectorType *getType() const {
0832 return cast<FixedVectorType>(Value::getType());
0833 }
0834
0835
0836 static bool classof(const Value *V) {
0837 return V->getValueID() == ConstantDataVectorVal;
0838 }
0839 };
0840
0841
0842
0843
0844 class ConstantTokenNone final : public ConstantData {
0845 friend class Constant;
0846
0847 explicit ConstantTokenNone(LLVMContext &Context)
0848 : ConstantData(Type::getTokenTy(Context), ConstantTokenNoneVal) {}
0849
0850 void destroyConstantImpl();
0851
0852 public:
0853 ConstantTokenNone(const ConstantTokenNone &) = delete;
0854
0855
0856 static ConstantTokenNone *get(LLVMContext &Context);
0857
0858
0859 static bool classof(const Value *V) {
0860 return V->getValueID() == ConstantTokenNoneVal;
0861 }
0862 };
0863
0864
0865 class ConstantTargetNone final : public ConstantData {
0866 friend class Constant;
0867
0868 explicit ConstantTargetNone(TargetExtType *T)
0869 : ConstantData(T, Value::ConstantTargetNoneVal) {}
0870
0871 void destroyConstantImpl();
0872
0873 public:
0874 ConstantTargetNone(const ConstantTargetNone &) = delete;
0875
0876
0877 static ConstantTargetNone *get(TargetExtType *T);
0878
0879
0880
0881 inline TargetExtType *getType() const {
0882 return cast<TargetExtType>(Value::getType());
0883 }
0884
0885
0886 static bool classof(const Value *V) {
0887 return V->getValueID() == ConstantTargetNoneVal;
0888 }
0889 };
0890
0891
0892
0893 class BlockAddress final : public Constant {
0894 friend class Constant;
0895
0896 constexpr static IntrusiveOperandsAllocMarker AllocMarker{2};
0897
0898 BlockAddress(Function *F, BasicBlock *BB);
0899
0900 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0901
0902 void destroyConstantImpl();
0903 Value *handleOperandChangeImpl(Value *From, Value *To);
0904
0905 public:
0906 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0907
0908
0909 static BlockAddress *get(Function *F, BasicBlock *BB);
0910
0911
0912
0913 static BlockAddress *get(BasicBlock *BB);
0914
0915
0916
0917
0918 static BlockAddress *lookup(const BasicBlock *BB);
0919
0920
0921 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0922
0923 Function *getFunction() const { return (Function *)Op<0>().get(); }
0924 BasicBlock *getBasicBlock() const { return (BasicBlock *)Op<1>().get(); }
0925
0926
0927 static bool classof(const Value *V) {
0928 return V->getValueID() == BlockAddressVal;
0929 }
0930 };
0931
0932 template <>
0933 struct OperandTraits<BlockAddress>
0934 : public FixedNumOperandTraits<BlockAddress, 2> {};
0935
0936 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
0937
0938
0939
0940
0941 class DSOLocalEquivalent final : public Constant {
0942 friend class Constant;
0943
0944 constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
0945
0946 DSOLocalEquivalent(GlobalValue *GV);
0947
0948 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0949
0950 void destroyConstantImpl();
0951 Value *handleOperandChangeImpl(Value *From, Value *To);
0952
0953 public:
0954 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0955
0956
0957 static DSOLocalEquivalent *get(GlobalValue *GV);
0958
0959
0960 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0961
0962 GlobalValue *getGlobalValue() const {
0963 return cast<GlobalValue>(Op<0>().get());
0964 }
0965
0966
0967 static bool classof(const Value *V) {
0968 return V->getValueID() == DSOLocalEquivalentVal;
0969 }
0970 };
0971
0972 template <>
0973 struct OperandTraits<DSOLocalEquivalent>
0974 : public FixedNumOperandTraits<DSOLocalEquivalent, 1> {};
0975
0976 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(DSOLocalEquivalent, Value)
0977
0978
0979
0980 class NoCFIValue final : public Constant {
0981 friend class Constant;
0982
0983 constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
0984
0985 NoCFIValue(GlobalValue *GV);
0986
0987 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0988
0989 void destroyConstantImpl();
0990 Value *handleOperandChangeImpl(Value *From, Value *To);
0991
0992 public:
0993
0994 static NoCFIValue *get(GlobalValue *GV);
0995
0996
0997 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0998
0999 GlobalValue *getGlobalValue() const {
1000 return cast<GlobalValue>(Op<0>().get());
1001 }
1002
1003
1004 PointerType *getType() const {
1005 return cast<PointerType>(Value::getType());
1006 }
1007
1008
1009 static bool classof(const Value *V) {
1010 return V->getValueID() == NoCFIValueVal;
1011 }
1012 };
1013
1014 template <>
1015 struct OperandTraits<NoCFIValue> : public FixedNumOperandTraits<NoCFIValue, 1> {
1016 };
1017
1018 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(NoCFIValue, Value)
1019
1020
1021 class ConstantPtrAuth final : public Constant {
1022 friend struct ConstantPtrAuthKeyType;
1023 friend class Constant;
1024
1025 constexpr static IntrusiveOperandsAllocMarker AllocMarker{4};
1026
1027 ConstantPtrAuth(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc,
1028 Constant *AddrDisc);
1029
1030 void *operator new(size_t s) { return User::operator new(s, AllocMarker); }
1031
1032 void destroyConstantImpl();
1033 Value *handleOperandChangeImpl(Value *From, Value *To);
1034
1035 public:
1036
1037 static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1038 ConstantInt *Disc, Constant *AddrDisc);
1039
1040
1041
1042 ConstantPtrAuth *getWithSameSchema(Constant *Pointer) const;
1043
1044
1045 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
1046
1047
1048 Constant *getPointer() const { return cast<Constant>(Op<0>().get()); }
1049
1050
1051 ConstantInt *getKey() const { return cast<ConstantInt>(Op<1>().get()); }
1052
1053
1054 ConstantInt *getDiscriminator() const {
1055 return cast<ConstantInt>(Op<2>().get());
1056 }
1057
1058
1059
1060
1061 Constant *getAddrDiscriminator() const {
1062 return cast<Constant>(Op<3>().get());
1063 }
1064
1065
1066 bool hasAddressDiscriminator() const {
1067 return !getAddrDiscriminator()->isNullValue();
1068 }
1069
1070
1071
1072
1073
1074
1075 enum { AddrDiscriminator_CtorsDtors = 1 };
1076
1077
1078
1079
1080
1081 bool hasSpecialAddressDiscriminator(uint64_t Value) const;
1082
1083
1084
1085
1086 bool isKnownCompatibleWith(const Value *Key, const Value *Discriminator,
1087 const DataLayout &DL) const;
1088
1089
1090 static bool classof(const Value *V) {
1091 return V->getValueID() == ConstantPtrAuthVal;
1092 }
1093 };
1094
1095 template <>
1096 struct OperandTraits<ConstantPtrAuth>
1097 : public FixedNumOperandTraits<ConstantPtrAuth, 4> {};
1098
1099 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPtrAuth, Constant)
1100
1101
1102
1103
1104
1105
1106
1107
1108 class ConstantExpr : public Constant {
1109 friend struct ConstantExprKeyType;
1110 friend class Constant;
1111
1112 void destroyConstantImpl();
1113 Value *handleOperandChangeImpl(Value *From, Value *To);
1114
1115 protected:
1116 ConstantExpr(Type *ty, unsigned Opcode, AllocInfo AllocInfo)
1117 : Constant(ty, ConstantExprVal, AllocInfo) {
1118
1119 setValueSubclassData(Opcode);
1120 }
1121
1122 ~ConstantExpr() = default;
1123
1124 public:
1125
1126
1127
1128
1129
1130
1131
1132 static Constant *getAlignOf(Type *Ty);
1133
1134
1135
1136
1137
1138 static Constant *getSizeOf(Type *Ty);
1139
1140 static Constant *getNeg(Constant *C, bool HasNSW = false);
1141 static Constant *getNot(Constant *C);
1142 static Constant *getAdd(Constant *C1, Constant *C2, bool HasNUW = false,
1143 bool HasNSW = false);
1144 static Constant *getSub(Constant *C1, Constant *C2, bool HasNUW = false,
1145 bool HasNSW = false);
1146 static Constant *getMul(Constant *C1, Constant *C2, bool HasNUW = false,
1147 bool HasNSW = false);
1148 static Constant *getXor(Constant *C1, Constant *C2);
1149 static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
1150 static Constant *getPtrToInt(Constant *C, Type *Ty,
1151 bool OnlyIfReduced = false);
1152 static Constant *getIntToPtr(Constant *C, Type *Ty,
1153 bool OnlyIfReduced = false);
1154 static Constant *getBitCast(Constant *C, Type *Ty,
1155 bool OnlyIfReduced = false);
1156 static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
1157 bool OnlyIfReduced = false);
1158
1159 static Constant *getNSWNeg(Constant *C) { return getNeg(C, true); }
1160
1161 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
1162 return getAdd(C1, C2, false, true);
1163 }
1164
1165 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
1166 return getAdd(C1, C2, true, false);
1167 }
1168
1169 static Constant *getNSWSub(Constant *C1, Constant *C2) {
1170 return getSub(C1, C2, false, true);
1171 }
1172
1173 static Constant *getNUWSub(Constant *C1, Constant *C2) {
1174 return getSub(C1, C2, true, false);
1175 }
1176
1177 static Constant *getNSWMul(Constant *C1, Constant *C2) {
1178 return getMul(C1, C2, false, true);
1179 }
1180
1181 static Constant *getNUWMul(Constant *C1, Constant *C2) {
1182 return getMul(C1, C2, true, false);
1183 }
1184
1185
1186
1187
1188
1189 static Constant *getExactLogBase2(Constant *C);
1190
1191
1192
1193
1194
1195
1196
1197
1198 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty,
1199 bool AllowRHSConstant = false,
1200 bool NSZ = false);
1201
1202 static Constant *getIntrinsicIdentity(Intrinsic::ID, Type *Ty);
1203
1204
1205
1206
1207 static Constant *getIdentity(Instruction *I, Type *Ty,
1208 bool AllowRHSConstant = false, bool NSZ = false);
1209
1210
1211
1212
1213
1214
1215
1216 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty,
1217 bool AllowLHSConstant = false);
1218
1219
1220 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
1221
1222
1223
1224
1225
1226
1227
1228 static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
1229 bool OnlyIfReduced = false);
1230
1231
1232 static Constant *
1233 getTruncOrBitCast(Constant *C,
1234 Type *Ty
1235 );
1236
1237
1238
1239 static Constant *
1240 getPointerCast(Constant *C,
1241 Type *Ty
1242 );
1243
1244
1245
1246 static Constant *getPointerBitCastOrAddrSpaceCast(
1247 Constant *C,
1248 Type *Ty
1249 );
1250
1251
1252 bool isCast() const;
1253
1254
1255
1256
1257
1258 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1259 unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1260
1261
1262
1263
1264
1265
1266 static Constant *
1267 getGetElementPtr(Type *Ty, Constant *C, ArrayRef<Constant *> IdxList,
1268 GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
1269 std::optional<ConstantRange> InRange = std::nullopt,
1270 Type *OnlyIfReducedTy = nullptr) {
1271 return getGetElementPtr(
1272 Ty, C, ArrayRef((Value *const *)IdxList.data(), IdxList.size()), NW,
1273 InRange, OnlyIfReducedTy);
1274 }
1275 static Constant *
1276 getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
1277 GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
1278 std::optional<ConstantRange> InRange = std::nullopt,
1279 Type *OnlyIfReducedTy = nullptr) {
1280
1281
1282
1283 return getGetElementPtr(Ty, C, cast<Value>(Idx), NW, InRange,
1284 OnlyIfReducedTy);
1285 }
1286 static Constant *
1287 getGetElementPtr(Type *Ty, Constant *C, ArrayRef<Value *> IdxList,
1288 GEPNoWrapFlags NW = GEPNoWrapFlags::none(),
1289 std::optional<ConstantRange> InRange = std::nullopt,
1290 Type *OnlyIfReducedTy = nullptr);
1291
1292
1293
1294 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1295 ArrayRef<Constant *> IdxList) {
1296 return getGetElementPtr(Ty, C, IdxList, GEPNoWrapFlags::inBounds());
1297 }
1298 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1299 Constant *Idx) {
1300
1301
1302
1303 return getGetElementPtr(Ty, C, Idx, GEPNoWrapFlags::inBounds());
1304 }
1305 static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1306 ArrayRef<Value *> IdxList) {
1307 return getGetElementPtr(Ty, C, IdxList, GEPNoWrapFlags::inBounds());
1308 }
1309
1310 static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1311 Type *OnlyIfReducedTy = nullptr);
1312 static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1313 Type *OnlyIfReducedTy = nullptr);
1314 static Constant *getShuffleVector(Constant *V1, Constant *V2,
1315 ArrayRef<int> Mask,
1316 Type *OnlyIfReducedTy = nullptr);
1317
1318
1319 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1320
1321
1322
1323 ArrayRef<int> getShuffleMask() const;
1324
1325
1326
1327
1328
1329 Constant *getShuffleMaskForBitcode() const;
1330
1331
1332 const char *getOpcodeName() const;
1333
1334
1335
1336
1337 Constant *getWithOperands(ArrayRef<Constant *> Ops) const {
1338 return getWithOperands(Ops, getType());
1339 }
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350 Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1351 bool OnlyIfReduced = false,
1352 Type *SrcTy = nullptr) const;
1353
1354
1355
1356
1357
1358
1359
1360
1361 Instruction *getAsInstruction() const;
1362
1363
1364
1365 static bool isDesirableBinOp(unsigned Opcode);
1366
1367
1368
1369 static bool isSupportedBinOp(unsigned Opcode);
1370
1371
1372 static bool isDesirableCastOp(unsigned Opcode);
1373
1374
1375 static bool isSupportedCastOp(unsigned Opcode);
1376
1377
1378
1379 static bool isSupportedGetElementPtr(const Type *SrcElemTy) {
1380 return !SrcElemTy->isScalableTy();
1381 }
1382
1383
1384 static bool classof(const Value *V) {
1385 return V->getValueID() == ConstantExprVal;
1386 }
1387
1388 private:
1389
1390
1391 void setValueSubclassData(unsigned short D) {
1392 Value::setValueSubclassData(D);
1393 }
1394 };
1395
1396 template <>
1397 struct OperandTraits<ConstantExpr>
1398 : public VariadicOperandTraits<ConstantExpr> {};
1399
1400 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412 class UndefValue : public ConstantData {
1413 friend class Constant;
1414
1415 explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {}
1416
1417 void destroyConstantImpl();
1418
1419 protected:
1420 explicit UndefValue(Type *T, ValueTy vty) : ConstantData(T, vty) {}
1421
1422 public:
1423 UndefValue(const UndefValue &) = delete;
1424
1425
1426 static UndefValue *get(Type *T);
1427
1428
1429
1430 UndefValue *getSequentialElement() const;
1431
1432
1433
1434 UndefValue *getStructElement(unsigned Elt) const;
1435
1436
1437
1438 UndefValue *getElementValue(Constant *C) const;
1439
1440
1441 UndefValue *getElementValue(unsigned Idx) const;
1442
1443
1444 unsigned getNumElements() const;
1445
1446
1447 static bool classof(const Value *V) {
1448 return V->getValueID() == UndefValueVal ||
1449 V->getValueID() == PoisonValueVal;
1450 }
1451 };
1452
1453
1454
1455
1456
1457
1458
1459
1460 class PoisonValue final : public UndefValue {
1461 friend class Constant;
1462
1463 explicit PoisonValue(Type *T) : UndefValue(T, PoisonValueVal) {}
1464
1465 void destroyConstantImpl();
1466
1467 public:
1468 PoisonValue(const PoisonValue &) = delete;
1469
1470
1471 static PoisonValue *get(Type *T);
1472
1473
1474
1475 PoisonValue *getSequentialElement() const;
1476
1477
1478
1479 PoisonValue *getStructElement(unsigned Elt) const;
1480
1481
1482
1483 PoisonValue *getElementValue(Constant *C) const;
1484
1485
1486 PoisonValue *getElementValue(unsigned Idx) const;
1487
1488
1489 static bool classof(const Value *V) {
1490 return V->getValueID() == PoisonValueVal;
1491 }
1492 };
1493
1494 }
1495
1496 #endif