File indexing completed on 2026-05-10 08:44:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef LLVM_IR_INSTRTYPES_H
0016 #define LLVM_IR_INSTRTYPES_H
0017
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/STLExtras.h"
0020 #include "llvm/ADT/Sequence.h"
0021 #include "llvm/ADT/StringMap.h"
0022 #include "llvm/ADT/Twine.h"
0023 #include "llvm/ADT/iterator_range.h"
0024 #include "llvm/IR/Attributes.h"
0025 #include "llvm/IR/CallingConv.h"
0026 #include "llvm/IR/DerivedTypes.h"
0027 #include "llvm/IR/FMF.h"
0028 #include "llvm/IR/Function.h"
0029 #include "llvm/IR/Instruction.h"
0030 #include "llvm/IR/LLVMContext.h"
0031 #include "llvm/IR/OperandTraits.h"
0032 #include "llvm/IR/User.h"
0033 #include <algorithm>
0034 #include <cassert>
0035 #include <cstddef>
0036 #include <cstdint>
0037 #include <iterator>
0038 #include <optional>
0039 #include <string>
0040 #include <vector>
0041
0042 namespace llvm {
0043
0044 class StringRef;
0045 class Type;
0046 class Value;
0047 class ConstantRange;
0048
0049 namespace Intrinsic {
0050 typedef unsigned ID;
0051 }
0052
0053
0054
0055
0056
0057 class UnaryInstruction : public Instruction {
0058 constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
0059
0060 protected:
0061 UnaryInstruction(Type *Ty, unsigned iType, Value *V,
0062 InsertPosition InsertBefore = nullptr)
0063 : Instruction(Ty, iType, AllocMarker, InsertBefore) {
0064 Op<0>() = V;
0065 }
0066
0067 public:
0068
0069 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0070 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0071
0072
0073 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0074
0075
0076 static bool classof(const Instruction *I) {
0077 return I->isUnaryOp() || I->getOpcode() == Instruction::Alloca ||
0078 I->getOpcode() == Instruction::Load ||
0079 I->getOpcode() == Instruction::VAArg ||
0080 I->getOpcode() == Instruction::ExtractValue ||
0081 I->getOpcode() == Instruction::Freeze ||
0082 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
0083 }
0084 static bool classof(const Value *V) {
0085 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0086 }
0087 };
0088
0089 template <>
0090 struct OperandTraits<UnaryInstruction> :
0091 public FixedNumOperandTraits<UnaryInstruction, 1> {
0092 };
0093
0094 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
0095
0096
0097
0098
0099
0100 class UnaryOperator : public UnaryInstruction {
0101 void AssertOK();
0102
0103 protected:
0104 UnaryOperator(UnaryOps iType, Value *S, Type *Ty, const Twine &Name,
0105 InsertPosition InsertBefore);
0106
0107
0108 friend class Instruction;
0109
0110 UnaryOperator *cloneImpl() const;
0111
0112 public:
0113
0114
0115
0116
0117
0118 static UnaryOperator *Create(UnaryOps Op, Value *S,
0119 const Twine &Name = Twine(),
0120 InsertPosition InsertBefore = nullptr);
0121
0122
0123
0124
0125 #define HANDLE_UNARY_INST(N, OPC, CLASS) \
0126 static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") { \
0127 return Create(Instruction::OPC, V, Name); \
0128 }
0129 #include "llvm/IR/Instruction.def"
0130 #define HANDLE_UNARY_INST(N, OPC, CLASS) \
0131 static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
0132 InsertPosition InsertBefore = nullptr) { \
0133 return Create(Instruction::OPC, V, Name, InsertBefore); \
0134 }
0135 #include "llvm/IR/Instruction.def"
0136
0137 static UnaryOperator *
0138 CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO,
0139 const Twine &Name = "",
0140 InsertPosition InsertBefore = nullptr) {
0141 UnaryOperator *UO = Create(Opc, V, Name, InsertBefore);
0142 UO->copyIRFlags(CopyO);
0143 return UO;
0144 }
0145
0146 static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource,
0147 const Twine &Name = "",
0148 InsertPosition InsertBefore = nullptr) {
0149 return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name,
0150 InsertBefore);
0151 }
0152
0153 UnaryOps getOpcode() const {
0154 return static_cast<UnaryOps>(Instruction::getOpcode());
0155 }
0156
0157
0158 static bool classof(const Instruction *I) {
0159 return I->isUnaryOp();
0160 }
0161 static bool classof(const Value *V) {
0162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0163 }
0164 };
0165
0166
0167
0168
0169
0170 class BinaryOperator : public Instruction {
0171 constexpr static IntrusiveOperandsAllocMarker AllocMarker{2};
0172
0173 void AssertOK();
0174
0175 protected:
0176 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
0177 const Twine &Name, InsertPosition InsertBefore);
0178
0179
0180 friend class Instruction;
0181
0182 BinaryOperator *cloneImpl() const;
0183
0184 public:
0185
0186 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0187 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0188
0189
0190 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0191
0192
0193
0194
0195
0196
0197 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
0198 const Twine &Name = Twine(),
0199 InsertPosition InsertBefore = nullptr);
0200
0201
0202
0203
0204 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
0205 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
0206 const Twine &Name = "") { \
0207 return Create(Instruction::OPC, V1, V2, Name); \
0208 }
0209 #include "llvm/IR/Instruction.def"
0210 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
0211 static BinaryOperator *Create##OPC(Value *V1, Value *V2, const Twine &Name, \
0212 InsertPosition InsertBefore) { \
0213 return Create(Instruction::OPC, V1, V2, Name, InsertBefore); \
0214 }
0215 #include "llvm/IR/Instruction.def"
0216
0217 static BinaryOperator *
0218 CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Value *CopyO,
0219 const Twine &Name = "",
0220 InsertPosition InsertBefore = nullptr) {
0221 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0222 BO->copyIRFlags(CopyO);
0223 return BO;
0224 }
0225
0226 static BinaryOperator *CreateWithFMF(BinaryOps Opc, Value *V1, Value *V2,
0227 FastMathFlags FMF,
0228 const Twine &Name = "",
0229 InsertPosition InsertBefore = nullptr) {
0230 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0231 BO->setFastMathFlags(FMF);
0232 return BO;
0233 }
0234
0235 static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, FastMathFlags FMF,
0236 const Twine &Name = "") {
0237 return CreateWithFMF(Instruction::FAdd, V1, V2, FMF, Name);
0238 }
0239 static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, FastMathFlags FMF,
0240 const Twine &Name = "") {
0241 return CreateWithFMF(Instruction::FSub, V1, V2, FMF, Name);
0242 }
0243 static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF,
0244 const Twine &Name = "") {
0245 return CreateWithFMF(Instruction::FMul, V1, V2, FMF, Name);
0246 }
0247 static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF,
0248 const Twine &Name = "") {
0249 return CreateWithFMF(Instruction::FDiv, V1, V2, FMF, Name);
0250 }
0251
0252 static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2,
0253 Instruction *FMFSource,
0254 const Twine &Name = "") {
0255 return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name);
0256 }
0257 static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2,
0258 Instruction *FMFSource,
0259 const Twine &Name = "") {
0260 return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name);
0261 }
0262 static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2,
0263 Instruction *FMFSource,
0264 const Twine &Name = "") {
0265 return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name);
0266 }
0267 static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2,
0268 Instruction *FMFSource,
0269 const Twine &Name = "") {
0270 return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name);
0271 }
0272 static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2,
0273 Instruction *FMFSource,
0274 const Twine &Name = "") {
0275 return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name);
0276 }
0277
0278 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
0279 const Twine &Name = "") {
0280 BinaryOperator *BO = Create(Opc, V1, V2, Name);
0281 BO->setHasNoSignedWrap(true);
0282 return BO;
0283 }
0284
0285 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
0286 const Twine &Name,
0287 InsertPosition InsertBefore) {
0288 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0289 BO->setHasNoSignedWrap(true);
0290 return BO;
0291 }
0292
0293 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
0294 const Twine &Name = "") {
0295 BinaryOperator *BO = Create(Opc, V1, V2, Name);
0296 BO->setHasNoUnsignedWrap(true);
0297 return BO;
0298 }
0299
0300 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
0301 const Twine &Name,
0302 InsertPosition InsertBefore) {
0303 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0304 BO->setHasNoUnsignedWrap(true);
0305 return BO;
0306 }
0307
0308 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
0309 const Twine &Name = "") {
0310 BinaryOperator *BO = Create(Opc, V1, V2, Name);
0311 BO->setIsExact(true);
0312 return BO;
0313 }
0314
0315 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
0316 const Twine &Name,
0317 InsertPosition InsertBefore) {
0318 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0319 BO->setIsExact(true);
0320 return BO;
0321 }
0322
0323 static inline BinaryOperator *
0324 CreateDisjoint(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name = "");
0325 static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
0326 Value *V2, const Twine &Name,
0327 InsertPosition InsertBefore);
0328
0329 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
0330 static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \
0331 const Twine &Name = "") { \
0332 return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
0333 } \
0334 static BinaryOperator *Create##NUWNSWEXACT##OPC( \
0335 Value *V1, Value *V2, const Twine &Name, \
0336 InsertPosition InsertBefore = nullptr) { \
0337 return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, InsertBefore); \
0338 }
0339
0340 DEFINE_HELPERS(Add, NSW)
0341 DEFINE_HELPERS(Add, NUW)
0342 DEFINE_HELPERS(Sub, NSW)
0343 DEFINE_HELPERS(Sub, NUW)
0344 DEFINE_HELPERS(Mul, NSW)
0345 DEFINE_HELPERS(Mul, NUW)
0346 DEFINE_HELPERS(Shl, NSW)
0347 DEFINE_HELPERS(Shl, NUW)
0348
0349 DEFINE_HELPERS(SDiv, Exact)
0350 DEFINE_HELPERS(UDiv, Exact)
0351 DEFINE_HELPERS(AShr, Exact)
0352 DEFINE_HELPERS(LShr, Exact)
0353
0354 DEFINE_HELPERS(Or, Disjoint)
0355
0356 #undef DEFINE_HELPERS
0357
0358
0359
0360
0361
0362
0363 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
0364 InsertPosition InsertBefore = nullptr);
0365 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
0366 InsertPosition InsertBefore = nullptr);
0367 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
0368 InsertPosition InsertBefore = nullptr);
0369
0370 BinaryOps getOpcode() const {
0371 return static_cast<BinaryOps>(Instruction::getOpcode());
0372 }
0373
0374
0375
0376
0377
0378
0379 bool swapOperands();
0380
0381
0382 static bool classof(const Instruction *I) {
0383 return I->isBinaryOp();
0384 }
0385 static bool classof(const Value *V) {
0386 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0387 }
0388 };
0389
0390 template <>
0391 struct OperandTraits<BinaryOperator> :
0392 public FixedNumOperandTraits<BinaryOperator, 2> {
0393 };
0394
0395 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
0396
0397
0398
0399
0400 class PossiblyDisjointInst : public BinaryOperator {
0401 public:
0402 enum { IsDisjoint = (1 << 0) };
0403
0404 void setIsDisjoint(bool B) {
0405 SubclassOptionalData =
0406 (SubclassOptionalData & ~IsDisjoint) | (B * IsDisjoint);
0407 }
0408
0409 bool isDisjoint() const { return SubclassOptionalData & IsDisjoint; }
0410
0411 static bool classof(const Instruction *I) {
0412 return I->getOpcode() == Instruction::Or;
0413 }
0414
0415 static bool classof(const Value *V) {
0416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0417 }
0418 };
0419
0420 BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
0421 Value *V2, const Twine &Name) {
0422 BinaryOperator *BO = Create(Opc, V1, V2, Name);
0423 cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
0424 return BO;
0425 }
0426 BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
0427 Value *V2, const Twine &Name,
0428 InsertPosition InsertBefore) {
0429 BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore);
0430 cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
0431 return BO;
0432 }
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444 class CastInst : public UnaryInstruction {
0445 protected:
0446
0447 CastInst(Type *Ty, unsigned iType, Value *S, const Twine &NameStr = "",
0448 InsertPosition InsertBefore = nullptr)
0449 : UnaryInstruction(Ty, iType, S, InsertBefore) {
0450 setName(NameStr);
0451 }
0452
0453 public:
0454
0455
0456
0457
0458
0459
0460 static CastInst *Create(
0461 Instruction::CastOps,
0462 Value *S,
0463 Type *Ty,
0464 const Twine &Name = "",
0465 InsertPosition InsertBefore = nullptr
0466 );
0467
0468
0469 static CastInst *CreateZExtOrBitCast(
0470 Value *S,
0471 Type *Ty,
0472 const Twine &Name = "",
0473 InsertPosition InsertBefore = nullptr
0474 );
0475
0476
0477 static CastInst *CreateSExtOrBitCast(
0478 Value *S,
0479 Type *Ty,
0480 const Twine &Name = "",
0481 InsertPosition InsertBefore = nullptr
0482 );
0483
0484
0485 static CastInst *CreatePointerCast(
0486 Value *S,
0487 Type *Ty,
0488 const Twine &Name = "",
0489 InsertPosition InsertBefore = nullptr
0490 );
0491
0492
0493 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
0494 Value *S,
0495 Type *Ty,
0496 const Twine &Name = "",
0497 InsertPosition InsertBefore = nullptr
0498 );
0499
0500
0501
0502
0503
0504
0505
0506 static CastInst *CreateBitOrPointerCast(
0507 Value *S,
0508 Type *Ty,
0509 const Twine &Name = "",
0510 InsertPosition InsertBefore = nullptr
0511 );
0512
0513
0514 static CastInst *CreateIntegerCast(
0515 Value *S,
0516 Type *Ty,
0517 bool isSigned,
0518 const Twine &Name = "",
0519 InsertPosition InsertBefore = nullptr
0520 );
0521
0522
0523 static CastInst *CreateFPCast(
0524 Value *S,
0525 Type *Ty,
0526 const Twine &Name = "",
0527 InsertPosition InsertBefore = nullptr
0528 );
0529
0530
0531 static CastInst *CreateTruncOrBitCast(
0532 Value *S,
0533 Type *Ty,
0534 const Twine &Name = "",
0535 InsertPosition InsertBefore = nullptr
0536 );
0537
0538
0539 static bool isBitCastable(
0540 Type *SrcTy,
0541 Type *DestTy
0542 );
0543
0544
0545
0546
0547
0548
0549 static bool isBitOrNoopPointerCastable(
0550 Type *SrcTy,
0551 Type *DestTy,
0552 const DataLayout &DL);
0553
0554
0555
0556
0557 static Instruction::CastOps getCastOpcode(
0558 const Value *Val,
0559 bool SrcIsSigned,
0560 Type *Ty,
0561 bool DstIsSigned
0562 );
0563
0564
0565
0566
0567
0568
0569 bool isIntegerCast() const;
0570
0571
0572
0573
0574
0575
0576
0577
0578 static bool isNoopCast(
0579 Instruction::CastOps Opcode,
0580 Type *SrcTy,
0581 Type *DstTy,
0582 const DataLayout &DL
0583 );
0584
0585
0586
0587
0588 bool isNoopCast(const DataLayout &DL) const;
0589
0590
0591
0592
0593
0594
0595
0596 static unsigned isEliminableCastPair(
0597 Instruction::CastOps firstOpcode,
0598 Instruction::CastOps secondOpcode,
0599 Type *SrcTy,
0600 Type *MidTy,
0601 Type *DstTy,
0602 Type *SrcIntPtrTy,
0603 Type *MidIntPtrTy,
0604 Type *DstIntPtrTy
0605 );
0606
0607
0608 Instruction::CastOps getOpcode() const {
0609 return Instruction::CastOps(Instruction::getOpcode());
0610 }
0611
0612
0613 Type* getSrcTy() const { return getOperand(0)->getType(); }
0614
0615 Type* getDestTy() const { return getType(); }
0616
0617
0618
0619
0620
0621 static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy);
0622 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
0623 return castIsValid(op, S->getType(), DstTy);
0624 }
0625
0626
0627 static bool classof(const Instruction *I) {
0628 return I->isCast();
0629 }
0630 static bool classof(const Value *V) {
0631 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0632 }
0633 };
0634
0635
0636 class PossiblyNonNegInst : public CastInst {
0637 public:
0638 enum { NonNeg = (1 << 0) };
0639
0640 static bool classof(const Instruction *I) {
0641 switch (I->getOpcode()) {
0642 case Instruction::ZExt:
0643 case Instruction::UIToFP:
0644 return true;
0645 default:
0646 return false;
0647 }
0648 }
0649
0650 static bool classof(const Value *V) {
0651 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0652 }
0653 };
0654
0655
0656
0657
0658
0659
0660
0661 class CmpInst : public Instruction {
0662 constexpr static IntrusiveOperandsAllocMarker AllocMarker{2};
0663
0664 public:
0665
0666
0667
0668
0669
0670
0671
0672
0673 enum Predicate : unsigned {
0674
0675 FCMP_FALSE = 0,
0676 FCMP_OEQ = 1,
0677 FCMP_OGT = 2,
0678 FCMP_OGE = 3,
0679 FCMP_OLT = 4,
0680 FCMP_OLE = 5,
0681 FCMP_ONE = 6,
0682 FCMP_ORD = 7,
0683 FCMP_UNO = 8,
0684 FCMP_UEQ = 9,
0685 FCMP_UGT = 10,
0686 FCMP_UGE = 11,
0687 FCMP_ULT = 12,
0688 FCMP_ULE = 13,
0689 FCMP_UNE = 14,
0690 FCMP_TRUE = 15,
0691 FIRST_FCMP_PREDICATE = FCMP_FALSE,
0692 LAST_FCMP_PREDICATE = FCMP_TRUE,
0693 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
0694 ICMP_EQ = 32,
0695 ICMP_NE = 33,
0696 ICMP_UGT = 34,
0697 ICMP_UGE = 35,
0698 ICMP_ULT = 36,
0699 ICMP_ULE = 37,
0700 ICMP_SGT = 38,
0701 ICMP_SGE = 39,
0702 ICMP_SLT = 40,
0703 ICMP_SLE = 41,
0704 FIRST_ICMP_PREDICATE = ICMP_EQ,
0705 LAST_ICMP_PREDICATE = ICMP_SLE,
0706 BAD_ICMP_PREDICATE = ICMP_SLE + 1
0707 };
0708 using PredicateField =
0709 Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>;
0710
0711
0712 static auto FCmpPredicates() {
0713 return enum_seq_inclusive(Predicate::FIRST_FCMP_PREDICATE,
0714 Predicate::LAST_FCMP_PREDICATE,
0715 force_iteration_on_noniterable_enum);
0716 }
0717
0718
0719 static auto ICmpPredicates() {
0720 return enum_seq_inclusive(Predicate::FIRST_ICMP_PREDICATE,
0721 Predicate::LAST_ICMP_PREDICATE,
0722 force_iteration_on_noniterable_enum);
0723 }
0724
0725 protected:
0726 CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, Value *LHS,
0727 Value *RHS, const Twine &Name = "",
0728 InsertPosition InsertBefore = nullptr,
0729 Instruction *FlagsSource = nullptr);
0730
0731 public:
0732
0733 void *operator new(size_t S) { return User::operator new(S, AllocMarker); }
0734 void operator delete(void *Ptr) { User::operator delete(Ptr); }
0735
0736
0737
0738
0739
0740
0741 static CmpInst *Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2,
0742 const Twine &Name = "",
0743 InsertPosition InsertBefore = nullptr);
0744
0745
0746
0747
0748
0749
0750
0751 static CmpInst *CreateWithCopiedFlags(OtherOps Op, Predicate Pred, Value *S1,
0752 Value *S2,
0753 const Instruction *FlagsSource,
0754 const Twine &Name = "",
0755 InsertPosition InsertBefore = nullptr);
0756
0757
0758 OtherOps getOpcode() const {
0759 return static_cast<OtherOps>(Instruction::getOpcode());
0760 }
0761
0762
0763 Predicate getPredicate() const { return getSubclassData<PredicateField>(); }
0764
0765
0766 void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); }
0767
0768 static bool isFPPredicate(Predicate P) {
0769 static_assert(FIRST_FCMP_PREDICATE == 0,
0770 "FIRST_FCMP_PREDICATE is required to be 0");
0771 return P <= LAST_FCMP_PREDICATE;
0772 }
0773
0774 static bool isIntPredicate(Predicate P) {
0775 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
0776 }
0777
0778 static StringRef getPredicateName(Predicate P);
0779
0780 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
0781 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
0782
0783
0784
0785
0786
0787 Predicate getInversePredicate() const {
0788 return getInversePredicate(getPredicate());
0789 }
0790
0791
0792
0793
0794 static Predicate getOrderedPredicate(Predicate Pred) {
0795 return static_cast<Predicate>(Pred & FCMP_ORD);
0796 }
0797
0798 Predicate getOrderedPredicate() const {
0799 return getOrderedPredicate(getPredicate());
0800 }
0801
0802
0803
0804
0805 static Predicate getUnorderedPredicate(Predicate Pred) {
0806 return static_cast<Predicate>(Pred | FCMP_UNO);
0807 }
0808
0809 Predicate getUnorderedPredicate() const {
0810 return getUnorderedPredicate(getPredicate());
0811 }
0812
0813
0814
0815
0816
0817 static Predicate getInversePredicate(Predicate pred);
0818
0819
0820
0821
0822
0823
0824
0825 Predicate getSwappedPredicate() const {
0826 return getSwappedPredicate(getPredicate());
0827 }
0828
0829
0830
0831
0832 static Predicate getSwappedPredicate(Predicate pred);
0833
0834
0835
0836
0837 static bool isStrictPredicate(Predicate predicate);
0838
0839
0840
0841 bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); }
0842
0843
0844
0845
0846 static bool isNonStrictPredicate(Predicate predicate);
0847
0848
0849
0850 bool isNonStrictPredicate() const {
0851 return isNonStrictPredicate(getPredicate());
0852 }
0853
0854
0855
0856 Predicate getStrictPredicate() const {
0857 return getStrictPredicate(getPredicate());
0858 }
0859
0860
0861
0862
0863
0864
0865 static Predicate getStrictPredicate(Predicate pred);
0866
0867
0868
0869 Predicate getNonStrictPredicate() const {
0870 return getNonStrictPredicate(getPredicate());
0871 }
0872
0873
0874
0875
0876
0877
0878 static Predicate getNonStrictPredicate(Predicate pred);
0879
0880
0881
0882
0883 static Predicate getFlippedStrictnessPredicate(Predicate pred);
0884
0885
0886
0887
0888
0889
0890
0891 Predicate getFlippedStrictnessPredicate() const {
0892 return getFlippedStrictnessPredicate(getPredicate());
0893 }
0894
0895
0896 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
0897
0898
0899
0900
0901 void swapOperands();
0902
0903
0904
0905 bool isCommutative() const;
0906
0907
0908
0909
0910 static bool isEquality(Predicate pred);
0911
0912
0913 bool isEquality() const { return isEquality(getPredicate()); }
0914
0915
0916
0917
0918 bool isEquivalence(bool Invert = false) const;
0919
0920
0921 static bool isRelational(Predicate P) { return !isEquality(P); }
0922
0923
0924 bool isRelational() const { return !isEquality(); }
0925
0926
0927
0928 bool isSigned() const {
0929 return isSigned(getPredicate());
0930 }
0931
0932
0933
0934 bool isUnsigned() const {
0935 return isUnsigned(getPredicate());
0936 }
0937
0938
0939
0940 bool isTrueWhenEqual() const {
0941 return isTrueWhenEqual(getPredicate());
0942 }
0943
0944
0945
0946 bool isFalseWhenEqual() const {
0947 return isFalseWhenEqual(getPredicate());
0948 }
0949
0950
0951
0952 static bool isUnsigned(Predicate predicate);
0953
0954
0955
0956 static bool isSigned(Predicate predicate);
0957
0958
0959 static bool isOrdered(Predicate predicate);
0960
0961
0962 static bool isUnordered(Predicate predicate);
0963
0964
0965 static bool isTrueWhenEqual(Predicate predicate);
0966
0967
0968 static bool isFalseWhenEqual(Predicate predicate);
0969
0970
0971 static bool classof(const Instruction *I) {
0972 return I->getOpcode() == Instruction::ICmp ||
0973 I->getOpcode() == Instruction::FCmp;
0974 }
0975 static bool classof(const Value *V) {
0976 return isa<Instruction>(V) && classof(cast<Instruction>(V));
0977 }
0978
0979
0980 static Type* makeCmpResultType(Type* opnd_type) {
0981 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
0982 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
0983 vt->getElementCount());
0984 }
0985 return Type::getInt1Ty(opnd_type->getContext());
0986 }
0987
0988 private:
0989
0990
0991 void setValueSubclassData(unsigned short D) {
0992 Value::setValueSubclassData(D);
0993 }
0994 };
0995
0996
0997 template <>
0998 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
0999 };
1000
1001 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
1002
1003 raw_ostream &operator<<(raw_ostream &OS, CmpInst::Predicate Pred);
1004
1005
1006
1007 struct OperandBundleUse {
1008 ArrayRef<Use> Inputs;
1009
1010 OperandBundleUse() = default;
1011 explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs)
1012 : Inputs(Inputs), Tag(Tag) {}
1013
1014
1015
1016 bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const {
1017 if (isDeoptOperandBundle())
1018 if (A == Attribute::ReadOnly || A == Attribute::NoCapture)
1019 return Inputs[Idx]->getType()->isPointerTy();
1020
1021
1022 return false;
1023 }
1024
1025
1026 StringRef getTagName() const {
1027 return Tag->getKey();
1028 }
1029
1030
1031
1032
1033
1034
1035 uint32_t getTagID() const {
1036 return Tag->getValue();
1037 }
1038
1039
1040 bool isDeoptOperandBundle() const {
1041 return getTagID() == LLVMContext::OB_deopt;
1042 }
1043
1044
1045 bool isFuncletOperandBundle() const {
1046 return getTagID() == LLVMContext::OB_funclet;
1047 }
1048
1049
1050 bool isCFGuardTargetOperandBundle() const {
1051 return getTagID() == LLVMContext::OB_cfguardtarget;
1052 }
1053
1054 private:
1055
1056 StringMapEntry<uint32_t> *Tag;
1057 };
1058
1059
1060
1061
1062
1063
1064
1065 template <typename InputTy> class OperandBundleDefT {
1066 std::string Tag;
1067 std::vector<InputTy> Inputs;
1068
1069 public:
1070 explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs)
1071 : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {}
1072 explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs)
1073 : Tag(std::move(Tag)), Inputs(Inputs) {}
1074
1075 explicit OperandBundleDefT(const OperandBundleUse &OBU) {
1076 Tag = std::string(OBU.getTagName());
1077 llvm::append_range(Inputs, OBU.Inputs);
1078 }
1079
1080 ArrayRef<InputTy> inputs() const { return Inputs; }
1081
1082 using input_iterator = typename std::vector<InputTy>::const_iterator;
1083
1084 size_t input_size() const { return Inputs.size(); }
1085 input_iterator input_begin() const { return Inputs.begin(); }
1086 input_iterator input_end() const { return Inputs.end(); }
1087
1088 StringRef getTag() const { return Tag; }
1089 };
1090
1091 using OperandBundleDef = OperandBundleDefT<Value *>;
1092 using ConstOperandBundleDef = OperandBundleDefT<const Value *>;
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112 class CallBase : public Instruction {
1113 protected:
1114
1115 using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>;
1116 using CallingConvField =
1117 Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10,
1118 CallingConv::MaxID>;
1119 static_assert(
1120 Bitfield::areContiguous<CallInstReservedField, CallingConvField>(),
1121 "Bitfields must be contiguous");
1122
1123
1124 static constexpr int CalledOperandOpEndIdx = -1;
1125
1126 AttributeList Attrs;
1127 FunctionType *FTy;
1128
1129 template <class... ArgsTy>
1130 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1131 : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {}
1132
1133 using Instruction::Instruction;
1134
1135 bool hasDescriptor() const { return Value::HasDescriptor; }
1136
1137 unsigned getNumSubclassExtraOperands() const {
1138 switch (getOpcode()) {
1139 case Instruction::Call:
1140 return 0;
1141 case Instruction::Invoke:
1142 return 2;
1143 case Instruction::CallBr:
1144 return getNumSubclassExtraOperandsDynamic();
1145 }
1146 llvm_unreachable("Invalid opcode!");
1147 }
1148
1149
1150
1151 unsigned getNumSubclassExtraOperandsDynamic() const;
1152
1153 public:
1154 using Instruction::getContext;
1155
1156
1157
1158
1159
1160
1161
1162 static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles,
1163 InsertPosition InsertPt = nullptr);
1164
1165
1166
1167
1168
1169
1170 static CallBase *Create(CallBase *CB, OperandBundleDef Bundle,
1171 InsertPosition InsertPt = nullptr);
1172
1173
1174 static CallBase *addOperandBundle(CallBase *CB, uint32_t ID,
1175 OperandBundleDef OB,
1176 InsertPosition InsertPt = nullptr);
1177
1178
1179 static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID,
1180 InsertPosition InsertPt = nullptr);
1181
1182
1183 Value *getConvergenceControlToken() const {
1184 if (auto Bundle = getOperandBundle(llvm::LLVMContext::OB_convergencectrl)) {
1185 return Bundle->Inputs[0].get();
1186 }
1187 return nullptr;
1188 }
1189
1190 static bool classof(const Instruction *I) {
1191 return I->getOpcode() == Instruction::Call ||
1192 I->getOpcode() == Instruction::Invoke ||
1193 I->getOpcode() == Instruction::CallBr;
1194 }
1195 static bool classof(const Value *V) {
1196 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1197 }
1198
1199 FunctionType *getFunctionType() const { return FTy; }
1200
1201 void mutateFunctionType(FunctionType *FTy) {
1202 Value::mutateType(FTy->getReturnType());
1203 this->FTy = FTy;
1204 }
1205
1206 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1207
1208
1209
1210
1211
1212
1213 User::op_iterator data_operands_begin() { return op_begin(); }
1214 User::const_op_iterator data_operands_begin() const {
1215 return const_cast<CallBase *>(this)->data_operands_begin();
1216 }
1217 User::op_iterator data_operands_end() {
1218
1219
1220 return op_end() - getNumSubclassExtraOperands() - 1;
1221 }
1222 User::const_op_iterator data_operands_end() const {
1223 return const_cast<CallBase *>(this)->data_operands_end();
1224 }
1225 iterator_range<User::op_iterator> data_ops() {
1226 return make_range(data_operands_begin(), data_operands_end());
1227 }
1228 iterator_range<User::const_op_iterator> data_ops() const {
1229 return make_range(data_operands_begin(), data_operands_end());
1230 }
1231 bool data_operands_empty() const {
1232 return data_operands_end() == data_operands_begin();
1233 }
1234 unsigned data_operands_size() const {
1235 return std::distance(data_operands_begin(), data_operands_end());
1236 }
1237
1238 bool isDataOperand(const Use *U) const {
1239 assert(this == U->getUser() &&
1240 "Only valid to query with a use of this instruction!");
1241 return data_operands_begin() <= U && U < data_operands_end();
1242 }
1243 bool isDataOperand(Value::const_user_iterator UI) const {
1244 return isDataOperand(&UI.getUse());
1245 }
1246
1247
1248
1249 unsigned getDataOperandNo(Value::const_user_iterator UI) const {
1250 return getDataOperandNo(&UI.getUse());
1251 }
1252
1253
1254
1255 unsigned getDataOperandNo(const Use *U) const {
1256 assert(isDataOperand(U) && "Data operand # out of range!");
1257 return U - data_operands_begin();
1258 }
1259
1260
1261 User::op_iterator arg_begin() { return op_begin(); }
1262 User::const_op_iterator arg_begin() const {
1263 return const_cast<CallBase *>(this)->arg_begin();
1264 }
1265
1266
1267 User::op_iterator arg_end() {
1268
1269
1270 return data_operands_end() - getNumTotalBundleOperands();
1271 }
1272 User::const_op_iterator arg_end() const {
1273 return const_cast<CallBase *>(this)->arg_end();
1274 }
1275
1276
1277 iterator_range<User::op_iterator> args() {
1278 return make_range(arg_begin(), arg_end());
1279 }
1280 iterator_range<User::const_op_iterator> args() const {
1281 return make_range(arg_begin(), arg_end());
1282 }
1283 bool arg_empty() const { return arg_end() == arg_begin(); }
1284 unsigned arg_size() const { return arg_end() - arg_begin(); }
1285
1286 Value *getArgOperand(unsigned i) const {
1287 assert(i < arg_size() && "Out of bounds!");
1288 return getOperand(i);
1289 }
1290
1291 void setArgOperand(unsigned i, Value *v) {
1292 assert(i < arg_size() && "Out of bounds!");
1293 setOperand(i, v);
1294 }
1295
1296
1297 const Use &getArgOperandUse(unsigned i) const {
1298 assert(i < arg_size() && "Out of bounds!");
1299 return User::getOperandUse(i);
1300 }
1301 Use &getArgOperandUse(unsigned i) {
1302 assert(i < arg_size() && "Out of bounds!");
1303 return User::getOperandUse(i);
1304 }
1305
1306 bool isArgOperand(const Use *U) const {
1307 assert(this == U->getUser() &&
1308 "Only valid to query with a use of this instruction!");
1309 return arg_begin() <= U && U < arg_end();
1310 }
1311 bool isArgOperand(Value::const_user_iterator UI) const {
1312 return isArgOperand(&UI.getUse());
1313 }
1314
1315
1316
1317 unsigned getArgOperandNo(const Use *U) const {
1318 assert(isArgOperand(U) && "Arg operand # out of range!");
1319 return U - arg_begin();
1320 }
1321
1322
1323
1324 unsigned getArgOperandNo(Value::const_user_iterator UI) const {
1325 return getArgOperandNo(&UI.getUse());
1326 }
1327
1328
1329
1330 bool hasArgument(const Value *V) const {
1331 return llvm::is_contained(args(), V);
1332 }
1333
1334 Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); }
1335
1336 const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); }
1337 Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); }
1338
1339
1340
1341 Function *getCalledFunction() const {
1342 if (auto *F = dyn_cast_or_null<Function>(getCalledOperand()))
1343 if (F->getValueType() == getFunctionType())
1344 return F;
1345 return nullptr;
1346 }
1347
1348
1349 bool isIndirectCall() const;
1350
1351
1352 bool isCallee(Value::const_user_iterator UI) const {
1353 return isCallee(&UI.getUse());
1354 }
1355
1356
1357 bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; }
1358
1359
1360 Function *getCaller();
1361 const Function *getCaller() const {
1362 return const_cast<CallBase *>(this)->getCaller();
1363 }
1364
1365
1366
1367 bool isMustTailCall() const;
1368
1369
1370 bool isTailCall() const;
1371
1372
1373
1374
1375 Intrinsic::ID getIntrinsicID() const;
1376
1377 void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; }
1378
1379
1380 void setCalledFunction(Function *Fn) {
1381 setCalledFunction(Fn->getFunctionType(), Fn);
1382 }
1383
1384
1385 void setCalledFunction(FunctionCallee Fn) {
1386 setCalledFunction(Fn.getFunctionType(), Fn.getCallee());
1387 }
1388
1389
1390
1391 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1392 this->FTy = FTy;
1393
1394
1395 assert(getType() == FTy->getReturnType());
1396 setCalledOperand(Fn);
1397 }
1398
1399 CallingConv::ID getCallingConv() const {
1400 return getSubclassData<CallingConvField>();
1401 }
1402
1403 void setCallingConv(CallingConv::ID CC) {
1404 setSubclassData<CallingConvField>(CC);
1405 }
1406
1407
1408 bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); }
1409
1410
1411
1412
1413
1414
1415
1416
1417 AttributeList getAttributes() const { return Attrs; }
1418
1419
1420 void setAttributes(AttributeList A) { Attrs = A; }
1421
1422
1423 AttributeSet getRetAttributes() const {
1424 return getAttributes().getRetAttrs();
1425 }
1426
1427
1428 AttributeSet getParamAttributes(unsigned ArgNo) const {
1429 return getAttributes().getParamAttrs(ArgNo);
1430 }
1431
1432
1433
1434
1435
1436 bool tryIntersectAttributes(const CallBase *Other) {
1437 if (this == Other)
1438 return true;
1439 AttributeList AL = getAttributes();
1440 AttributeList ALOther = Other->getAttributes();
1441 auto Intersected = AL.intersectWith(getContext(), ALOther);
1442 if (!Intersected)
1443 return false;
1444 setAttributes(*Intersected);
1445 return true;
1446 }
1447
1448
1449
1450
1451 bool hasFnAttr(Attribute::AttrKind Kind) const {
1452 assert(Kind != Attribute::NoBuiltin &&
1453 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin");
1454 return hasFnAttrImpl(Kind);
1455 }
1456
1457
1458
1459
1460 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1461
1462
1463
1464 void addAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
1465 Attrs = Attrs.addAttributeAtIndex(getContext(), i, Kind);
1466 }
1467
1468
1469 void addAttributeAtIndex(unsigned i, Attribute Attr) {
1470 Attrs = Attrs.addAttributeAtIndex(getContext(), i, Attr);
1471 }
1472
1473
1474 void addFnAttr(Attribute::AttrKind Kind) {
1475 Attrs = Attrs.addFnAttribute(getContext(), Kind);
1476 }
1477
1478
1479 void addFnAttr(Attribute Attr) {
1480 Attrs = Attrs.addFnAttribute(getContext(), Attr);
1481 }
1482
1483
1484 void addRetAttr(Attribute::AttrKind Kind) {
1485 Attrs = Attrs.addRetAttribute(getContext(), Kind);
1486 }
1487
1488
1489 void addRetAttr(Attribute Attr) {
1490 Attrs = Attrs.addRetAttribute(getContext(), Attr);
1491 }
1492
1493
1494 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1495 assert(ArgNo < arg_size() && "Out of bounds");
1496 Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Kind);
1497 }
1498
1499
1500 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1501 assert(ArgNo < arg_size() && "Out of bounds");
1502 Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Attr);
1503 }
1504
1505
1506 void removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
1507 Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind);
1508 }
1509
1510
1511 void removeAttributeAtIndex(unsigned i, StringRef Kind) {
1512 Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind);
1513 }
1514
1515
1516 void removeFnAttrs(const AttributeMask &AttrsToRemove) {
1517 Attrs = Attrs.removeFnAttributes(getContext(), AttrsToRemove);
1518 }
1519
1520
1521 void removeFnAttr(Attribute::AttrKind Kind) {
1522 Attrs = Attrs.removeFnAttribute(getContext(), Kind);
1523 }
1524
1525
1526 void removeFnAttr(StringRef Kind) {
1527 Attrs = Attrs.removeFnAttribute(getContext(), Kind);
1528 }
1529
1530
1531 void removeRetAttr(Attribute::AttrKind Kind) {
1532 Attrs = Attrs.removeRetAttribute(getContext(), Kind);
1533 }
1534
1535
1536 void removeRetAttrs(const AttributeMask &AttrsToRemove) {
1537 Attrs = Attrs.removeRetAttributes(getContext(), AttrsToRemove);
1538 }
1539
1540
1541 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1542 assert(ArgNo < arg_size() && "Out of bounds");
1543 Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind);
1544 }
1545
1546
1547 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1548 assert(ArgNo < arg_size() && "Out of bounds");
1549 Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind);
1550 }
1551
1552
1553 void removeParamAttrs(unsigned ArgNo, const AttributeMask &AttrsToRemove) {
1554 Attrs = Attrs.removeParamAttributes(getContext(), ArgNo, AttrsToRemove);
1555 }
1556
1557
1558 void addDereferenceableParamAttr(unsigned i, uint64_t Bytes) {
1559 Attrs = Attrs.addDereferenceableParamAttr(getContext(), i, Bytes);
1560 }
1561
1562
1563 void addDereferenceableRetAttr(uint64_t Bytes) {
1564 Attrs = Attrs.addDereferenceableRetAttr(getContext(), Bytes);
1565 }
1566
1567
1568 void addRangeRetAttr(const ConstantRange &CR) {
1569 Attrs = Attrs.addRangeRetAttr(getContext(), CR);
1570 }
1571
1572
1573 bool hasRetAttr(Attribute::AttrKind Kind) const {
1574 return hasRetAttrImpl(Kind);
1575 }
1576
1577 bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); }
1578
1579
1580 Attribute getRetAttr(Attribute::AttrKind Kind) const {
1581 Attribute RetAttr = Attrs.getRetAttr(Kind);
1582 if (RetAttr.isValid())
1583 return RetAttr;
1584
1585
1586 if (const Function *F = getCalledFunction())
1587 return F->getRetAttribute(Kind);
1588 return Attribute();
1589 }
1590
1591
1592 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
1593
1594
1595 Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const {
1596 return getAttributes().getAttributeAtIndex(i, Kind);
1597 }
1598
1599
1600 Attribute getAttributeAtIndex(unsigned i, StringRef Kind) const {
1601 return getAttributes().getAttributeAtIndex(i, Kind);
1602 }
1603
1604
1605 Attribute getFnAttr(StringRef Kind) const {
1606 Attribute Attr = getAttributes().getFnAttr(Kind);
1607 if (Attr.isValid())
1608 return Attr;
1609 return getFnAttrOnCalledFunction(Kind);
1610 }
1611
1612
1613 Attribute getFnAttr(Attribute::AttrKind Kind) const {
1614 Attribute A = getAttributes().getFnAttr(Kind);
1615 if (A.isValid())
1616 return A;
1617 return getFnAttrOnCalledFunction(Kind);
1618 }
1619
1620
1621 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1622 assert(ArgNo < arg_size() && "Out of bounds");
1623 Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
1624 if (A.isValid())
1625 return A;
1626 return getParamAttrOnCalledFunction(ArgNo, Kind);
1627 }
1628
1629
1630 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1631 assert(ArgNo < arg_size() && "Out of bounds");
1632 Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
1633 if (A.isValid())
1634 return A;
1635 return getParamAttrOnCalledFunction(ArgNo, Kind);
1636 }
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1650
1651 assert(i < arg_size() + getNumTotalBundleOperands() &&
1652 "Data operand index out of bounds!");
1653
1654
1655
1656
1657
1658 if (i < arg_size())
1659 return paramHasAttr(i, Kind);
1660
1661 assert(hasOperandBundles() && i >= getBundleOperandsStartIndex() &&
1662 "Must be either a call argument or an operand bundle!");
1663 return bundleOperandHasAttr(i, Kind);
1664 }
1665
1666
1667
1668
1669 bool doesNotCapture(unsigned OpNo) const {
1670
1671
1672 if (OpNo < arg_size() && isByValArgument(OpNo))
1673 return true;
1674
1675 return dataOperandHasImpliedAttr(OpNo, Attribute::NoCapture);
1676 }
1677
1678
1679 bool isByValArgument(unsigned ArgNo) const {
1680 return paramHasAttr(ArgNo, Attribute::ByVal);
1681 }
1682
1683
1684 bool isInAllocaArgument(unsigned ArgNo) const {
1685 return paramHasAttr(ArgNo, Attribute::InAlloca);
1686 }
1687
1688
1689
1690 bool isPassPointeeByValueArgument(unsigned ArgNo) const {
1691 return paramHasAttr(ArgNo, Attribute::ByVal) ||
1692 paramHasAttr(ArgNo, Attribute::InAlloca) ||
1693 paramHasAttr(ArgNo, Attribute::Preallocated);
1694 }
1695
1696
1697
1698
1699 bool isPassingUndefUB(unsigned ArgNo) const {
1700 return paramHasAttr(ArgNo, Attribute::NoUndef) ||
1701
1702 paramHasAttr(ArgNo, Attribute::Dereferenceable) ||
1703
1704 paramHasAttr(ArgNo, Attribute::DereferenceableOrNull);
1705 }
1706
1707
1708
1709 bool hasInAllocaArgument() const {
1710 return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca);
1711 }
1712
1713
1714
1715 bool doesNotAccessMemory(unsigned OpNo) const {
1716 return dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
1717 }
1718
1719
1720
1721 bool onlyReadsMemory(unsigned OpNo) const {
1722
1723
1724 if (OpNo < arg_size() && isByValArgument(OpNo))
1725 return true;
1726
1727 return dataOperandHasImpliedAttr(OpNo, Attribute::ReadOnly) ||
1728 dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
1729 }
1730
1731
1732
1733 bool onlyWritesMemory(unsigned OpNo) const {
1734 return dataOperandHasImpliedAttr(OpNo, Attribute::WriteOnly) ||
1735 dataOperandHasImpliedAttr(OpNo, Attribute::ReadNone);
1736 }
1737
1738
1739 MaybeAlign getRetAlign() const {
1740 if (auto Align = Attrs.getRetAlignment())
1741 return Align;
1742 if (const Function *F = getCalledFunction())
1743 return F->getAttributes().getRetAlignment();
1744 return std::nullopt;
1745 }
1746
1747
1748 MaybeAlign getParamAlign(unsigned ArgNo) const {
1749 return Attrs.getParamAlignment(ArgNo);
1750 }
1751
1752 MaybeAlign getParamStackAlign(unsigned ArgNo) const {
1753 return Attrs.getParamStackAlignment(ArgNo);
1754 }
1755
1756
1757 Type *getParamByRefType(unsigned ArgNo) const {
1758 if (auto *Ty = Attrs.getParamByRefType(ArgNo))
1759 return Ty;
1760 if (const Function *F = getCalledFunction())
1761 return F->getAttributes().getParamByRefType(ArgNo);
1762 return nullptr;
1763 }
1764
1765
1766 Type *getParamByValType(unsigned ArgNo) const {
1767 if (auto *Ty = Attrs.getParamByValType(ArgNo))
1768 return Ty;
1769 if (const Function *F = getCalledFunction())
1770 return F->getAttributes().getParamByValType(ArgNo);
1771 return nullptr;
1772 }
1773
1774
1775 Type *getParamPreallocatedType(unsigned ArgNo) const {
1776 if (auto *Ty = Attrs.getParamPreallocatedType(ArgNo))
1777 return Ty;
1778 if (const Function *F = getCalledFunction())
1779 return F->getAttributes().getParamPreallocatedType(ArgNo);
1780 return nullptr;
1781 }
1782
1783
1784 Type *getParamInAllocaType(unsigned ArgNo) const {
1785 if (auto *Ty = Attrs.getParamInAllocaType(ArgNo))
1786 return Ty;
1787 if (const Function *F = getCalledFunction())
1788 return F->getAttributes().getParamInAllocaType(ArgNo);
1789 return nullptr;
1790 }
1791
1792
1793 Type *getParamStructRetType(unsigned ArgNo) const {
1794 if (auto *Ty = Attrs.getParamStructRetType(ArgNo))
1795 return Ty;
1796 if (const Function *F = getCalledFunction())
1797 return F->getAttributes().getParamStructRetType(ArgNo);
1798 return nullptr;
1799 }
1800
1801
1802
1803
1804 Type *getParamElementType(unsigned ArgNo) const {
1805 return Attrs.getParamElementType(ArgNo);
1806 }
1807
1808
1809
1810 uint64_t getRetDereferenceableBytes() const {
1811 uint64_t Bytes = Attrs.getRetDereferenceableBytes();
1812 if (const Function *F = getCalledFunction())
1813 Bytes = std::max(Bytes, F->getAttributes().getRetDereferenceableBytes());
1814 return Bytes;
1815 }
1816
1817
1818
1819 uint64_t getParamDereferenceableBytes(unsigned i) const {
1820 return Attrs.getParamDereferenceableBytes(i);
1821 }
1822
1823
1824
1825 uint64_t getRetDereferenceableOrNullBytes() const {
1826 uint64_t Bytes = Attrs.getRetDereferenceableOrNullBytes();
1827 if (const Function *F = getCalledFunction()) {
1828 Bytes = std::max(Bytes,
1829 F->getAttributes().getRetDereferenceableOrNullBytes());
1830 }
1831
1832 return Bytes;
1833 }
1834
1835
1836
1837 uint64_t getParamDereferenceableOrNullBytes(unsigned i) const {
1838 return Attrs.getParamDereferenceableOrNullBytes(i);
1839 }
1840
1841
1842
1843 FPClassTest getRetNoFPClass() const;
1844
1845
1846
1847 FPClassTest getParamNoFPClass(unsigned i) const;
1848
1849
1850
1851 std::optional<ConstantRange> getRange() const;
1852
1853
1854
1855
1856 bool isReturnNonNull() const;
1857
1858
1859 bool returnDoesNotAlias() const {
1860 return Attrs.hasRetAttr(Attribute::NoAlias);
1861 }
1862
1863
1864
1865 Value *getReturnedArgOperand() const {
1866 return getArgOperandWithAttribute(Attribute::Returned);
1867 }
1868
1869
1870
1871 Value *getArgOperandWithAttribute(Attribute::AttrKind Kind) const;
1872
1873
1874
1875 bool isNoBuiltin() const {
1876 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1877 !hasFnAttrImpl(Attribute::Builtin);
1878 }
1879
1880
1881 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1882
1883
1884 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1885 void setIsNoInline() { addFnAttr(Attribute::NoInline); }
1886
1887 MemoryEffects getMemoryEffects() const;
1888 void setMemoryEffects(MemoryEffects ME);
1889
1890
1891 bool doesNotAccessMemory() const;
1892 void setDoesNotAccessMemory();
1893
1894
1895 bool onlyReadsMemory() const;
1896 void setOnlyReadsMemory();
1897
1898
1899 bool onlyWritesMemory() const;
1900 void setOnlyWritesMemory();
1901
1902
1903
1904 bool onlyAccessesArgMemory() const;
1905 void setOnlyAccessesArgMemory();
1906
1907
1908
1909 bool onlyAccessesInaccessibleMemory() const;
1910 void setOnlyAccessesInaccessibleMemory();
1911
1912
1913
1914 bool onlyAccessesInaccessibleMemOrArgMem() const;
1915 void setOnlyAccessesInaccessibleMemOrArgMem();
1916
1917
1918 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1919 void setDoesNotReturn() { addFnAttr(Attribute::NoReturn); }
1920
1921
1922 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1923
1924
1925 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1926 void setDoesNotThrow() { addFnAttr(Attribute::NoUnwind); }
1927
1928
1929 bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); }
1930 void setCannotDuplicate() { addFnAttr(Attribute::NoDuplicate); }
1931
1932
1933 bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); }
1934 void setCannotMerge() { addFnAttr(Attribute::NoMerge); }
1935
1936
1937 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1938 void setConvergent() { addFnAttr(Attribute::Convergent); }
1939 void setNotConvergent() { removeFnAttr(Attribute::Convergent); }
1940
1941
1942
1943 bool hasStructRetAttr() const {
1944 if (arg_empty())
1945 return false;
1946
1947
1948 return paramHasAttr(0, Attribute::StructRet);
1949 }
1950
1951
1952 bool hasByValArgument() const {
1953 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1954 }
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966 unsigned getNumOperandBundles() const {
1967 return std::distance(bundle_op_info_begin(), bundle_op_info_end());
1968 }
1969
1970
1971 bool hasOperandBundles() const { return getNumOperandBundles() != 0; }
1972
1973
1974 unsigned getBundleOperandsStartIndex() const {
1975 assert(hasOperandBundles() && "Don't call otherwise!");
1976 return bundle_op_info_begin()->Begin;
1977 }
1978
1979
1980 unsigned getBundleOperandsEndIndex() const {
1981 assert(hasOperandBundles() && "Don't call otherwise!");
1982 return bundle_op_info_end()[-1].End;
1983 }
1984
1985
1986 bool isBundleOperand(unsigned Idx) const {
1987 return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() &&
1988 Idx < getBundleOperandsEndIndex();
1989 }
1990
1991
1992
1993 bool isOperandBundleOfType(uint32_t ID, unsigned Idx) const {
1994 return isBundleOperand(Idx) &&
1995 getOperandBundleForOperand(Idx).getTagID() == ID;
1996 }
1997
1998
1999 bool isBundleOperand(const Use *U) const {
2000 assert(this == U->getUser() &&
2001 "Only valid to query with a use of this instruction!");
2002 return hasOperandBundles() && isBundleOperand(U - op_begin());
2003 }
2004 bool isBundleOperand(Value::const_user_iterator UI) const {
2005 return isBundleOperand(&UI.getUse());
2006 }
2007
2008
2009
2010 unsigned getNumTotalBundleOperands() const {
2011 if (!hasOperandBundles())
2012 return 0;
2013
2014 unsigned Begin = getBundleOperandsStartIndex();
2015 unsigned End = getBundleOperandsEndIndex();
2016
2017 assert(Begin <= End && "Should be!");
2018 return End - Begin;
2019 }
2020
2021
2022 OperandBundleUse getOperandBundleAt(unsigned Index) const {
2023 assert(Index < getNumOperandBundles() && "Index out of bounds!");
2024 return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index));
2025 }
2026
2027
2028
2029 unsigned countOperandBundlesOfType(StringRef Name) const {
2030 unsigned Count = 0;
2031 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
2032 if (getOperandBundleAt(i).getTagName() == Name)
2033 Count++;
2034
2035 return Count;
2036 }
2037
2038
2039
2040 unsigned countOperandBundlesOfType(uint32_t ID) const {
2041 unsigned Count = 0;
2042 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
2043 if (getOperandBundleAt(i).getTagID() == ID)
2044 Count++;
2045
2046 return Count;
2047 }
2048
2049
2050
2051
2052
2053 std::optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
2054 assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!");
2055
2056 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
2057 OperandBundleUse U = getOperandBundleAt(i);
2058 if (U.getTagName() == Name)
2059 return U;
2060 }
2061
2062 return std::nullopt;
2063 }
2064
2065
2066
2067
2068
2069 std::optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
2070 assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!");
2071
2072 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
2073 OperandBundleUse U = getOperandBundleAt(i);
2074 if (U.getTagID() == ID)
2075 return U;
2076 }
2077
2078 return std::nullopt;
2079 }
2080
2081
2082
2083
2084
2085
2086
2087
2088 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const;
2089
2090
2091
2092
2093
2094 OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const {
2095 return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx));
2096 }
2097
2098
2099
2100 bool hasReadingOperandBundles() const;
2101
2102
2103
2104 bool hasClobberingOperandBundles() const;
2105
2106
2107
2108 bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const {
2109 auto &BOI = getBundleOpInfoForOperand(OpIdx);
2110 auto OBU = operandBundleFromBundleOpInfo(BOI);
2111 return OBU.operandHasAttr(OpIdx - BOI.Begin, A);
2112 }
2113
2114
2115
2116
2117 bool hasIdenticalOperandBundleSchema(const CallBase &Other) const {
2118 if (getNumOperandBundles() != Other.getNumOperandBundles())
2119 return false;
2120
2121 return std::equal(bundle_op_info_begin(), bundle_op_info_end(),
2122 Other.bundle_op_info_begin());
2123 }
2124
2125
2126
2127 bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const {
2128 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
2129 uint32_t ID = getOperandBundleAt(i).getTagID();
2130 if (!is_contained(IDs, ID))
2131 return true;
2132 }
2133 return false;
2134 }
2135
2136
2137
2138 struct BundleOpInfo {
2139
2140
2141 StringMapEntry<uint32_t> *Tag;
2142
2143
2144
2145 uint32_t Begin;
2146
2147
2148
2149 uint32_t End;
2150
2151 bool operator==(const BundleOpInfo &Other) const {
2152 return Tag == Other.Tag && Begin == Other.Begin && End == Other.End;
2153 }
2154 };
2155
2156
2157
2158 OperandBundleUse
2159 operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const {
2160 const auto *begin = op_begin();
2161 ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End);
2162 return OperandBundleUse(BOI.Tag, Inputs);
2163 }
2164
2165 using bundle_op_iterator = BundleOpInfo *;
2166 using const_bundle_op_iterator = const BundleOpInfo *;
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214 bundle_op_iterator bundle_op_info_begin() {
2215 if (!hasDescriptor())
2216 return nullptr;
2217
2218 uint8_t *BytesBegin = getDescriptor().begin();
2219 return reinterpret_cast<bundle_op_iterator>(BytesBegin);
2220 }
2221
2222
2223
2224 const_bundle_op_iterator bundle_op_info_begin() const {
2225 auto *NonConstThis = const_cast<CallBase *>(this);
2226 return NonConstThis->bundle_op_info_begin();
2227 }
2228
2229
2230
2231 bundle_op_iterator bundle_op_info_end() {
2232 if (!hasDescriptor())
2233 return nullptr;
2234
2235 uint8_t *BytesEnd = getDescriptor().end();
2236 return reinterpret_cast<bundle_op_iterator>(BytesEnd);
2237 }
2238
2239
2240
2241 const_bundle_op_iterator bundle_op_info_end() const {
2242 auto *NonConstThis = const_cast<CallBase *>(this);
2243 return NonConstThis->bundle_op_info_end();
2244 }
2245
2246
2247 iterator_range<bundle_op_iterator> bundle_op_infos() {
2248 return make_range(bundle_op_info_begin(), bundle_op_info_end());
2249 }
2250
2251
2252 iterator_range<const_bundle_op_iterator> bundle_op_infos() const {
2253 return make_range(bundle_op_info_begin(), bundle_op_info_end());
2254 }
2255
2256
2257
2258
2259
2260
2261
2262 op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
2263 const unsigned BeginIndex);
2264
2265
2266 bool hasDeoptState() const {
2267 return getOperandBundle(LLVMContext::OB_deopt).has_value();
2268 }
2269
2270 public:
2271
2272
2273
2274
2275 BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx);
2276 const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const {
2277 return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx);
2278 }
2279
2280 protected:
2281
2282 static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) {
2283 unsigned Total = 0;
2284 for (const auto &B : Bundles)
2285 Total += B.input_size();
2286 return Total;
2287 }
2288
2289
2290
2291
2292 private:
2293 bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
2294 bool hasFnAttrOnCalledFunction(StringRef Kind) const;
2295
2296 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
2297 if (Attrs.hasFnAttr(Kind))
2298 return true;
2299
2300 return hasFnAttrOnCalledFunction(Kind);
2301 }
2302 template <typename AK> Attribute getFnAttrOnCalledFunction(AK Kind) const;
2303 template <typename AK>
2304 Attribute getParamAttrOnCalledFunction(unsigned ArgNo, AK Kind) const;
2305
2306
2307
2308 template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const {
2309 if (Attrs.hasRetAttr(Kind))
2310 return true;
2311
2312
2313 if (const Function *F = getCalledFunction())
2314 return F->getAttributes().hasRetAttr(Kind);
2315 return false;
2316 }
2317 };
2318
2319 template <>
2320 struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase> {};
2321
2322 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)
2323
2324
2325
2326
2327 class FuncletPadInst : public Instruction {
2328 private:
2329 FuncletPadInst(const FuncletPadInst &CPI, AllocInfo AllocInfo);
2330
2331 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
2332 ArrayRef<Value *> Args, AllocInfo AllocInfo,
2333 const Twine &NameStr, InsertPosition InsertBefore);
2334
2335 void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr);
2336
2337 protected:
2338
2339 friend class Instruction;
2340 friend class CatchPadInst;
2341 friend class CleanupPadInst;
2342
2343 FuncletPadInst *cloneImpl() const;
2344
2345 public:
2346
2347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2348
2349
2350
2351 unsigned arg_size() const { return getNumOperands() - 1; }
2352
2353
2354
2355
2356
2357
2358
2359 Value *getParentPad() const { return Op<-1>(); }
2360 void setParentPad(Value *ParentPad) {
2361 assert(ParentPad);
2362 Op<-1>() = ParentPad;
2363 }
2364
2365
2366
2367 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2368 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2369
2370
2371 op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
2372
2373
2374 const_op_range arg_operands() const {
2375 return const_op_range(op_begin(), op_end() - 1);
2376 }
2377
2378
2379 static bool classof(const Instruction *I) { return I->isFuncletPad(); }
2380 static bool classof(const Value *V) {
2381 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2382 }
2383 };
2384
2385 template <>
2386 struct OperandTraits<FuncletPadInst>
2387 : public VariadicOperandTraits<FuncletPadInst> {};
2388
2389 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)
2390
2391 }
2392
2393 #endif