File indexing completed on 2026-05-10 08:44:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_IR_IRBUILDER_H
0015 #define LLVM_IR_IRBUILDER_H
0016
0017 #include "llvm-c/Types.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/STLExtras.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/ADT/Twine.h"
0022 #include "llvm/IR/BasicBlock.h"
0023 #include "llvm/IR/Constant.h"
0024 #include "llvm/IR/ConstantFolder.h"
0025 #include "llvm/IR/Constants.h"
0026 #include "llvm/IR/DataLayout.h"
0027 #include "llvm/IR/DebugLoc.h"
0028 #include "llvm/IR/DerivedTypes.h"
0029 #include "llvm/IR/FPEnv.h"
0030 #include "llvm/IR/Function.h"
0031 #include "llvm/IR/GlobalVariable.h"
0032 #include "llvm/IR/InstrTypes.h"
0033 #include "llvm/IR/Instruction.h"
0034 #include "llvm/IR/Instructions.h"
0035 #include "llvm/IR/Intrinsics.h"
0036 #include "llvm/IR/LLVMContext.h"
0037 #include "llvm/IR/Operator.h"
0038 #include "llvm/IR/Type.h"
0039 #include "llvm/IR/Value.h"
0040 #include "llvm/IR/ValueHandle.h"
0041 #include "llvm/Support/AtomicOrdering.h"
0042 #include "llvm/Support/CBindingWrapping.h"
0043 #include "llvm/Support/Casting.h"
0044 #include <cassert>
0045 #include <cstdint>
0046 #include <functional>
0047 #include <optional>
0048 #include <utility>
0049
0050 namespace llvm {
0051
0052 class APInt;
0053 class Use;
0054
0055
0056
0057
0058
0059
0060 class IRBuilderDefaultInserter {
0061 public:
0062 virtual ~IRBuilderDefaultInserter();
0063
0064 virtual void InsertHelper(Instruction *I, const Twine &Name,
0065 BasicBlock::iterator InsertPt) const {
0066 if (InsertPt.isValid())
0067 I->insertInto(InsertPt.getNodeParent(), InsertPt);
0068 I->setName(Name);
0069 }
0070 };
0071
0072
0073
0074 class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
0075 std::function<void(Instruction *)> Callback;
0076
0077 public:
0078 ~IRBuilderCallbackInserter() override;
0079
0080 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
0081 : Callback(std::move(Callback)) {}
0082
0083 void InsertHelper(Instruction *I, const Twine &Name,
0084 BasicBlock::iterator InsertPt) const override {
0085 IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
0086 Callback(I);
0087 }
0088 };
0089
0090
0091
0092 class FMFSource {
0093 std::optional<FastMathFlags> FMF;
0094
0095 public:
0096 FMFSource() = default;
0097 FMFSource(Instruction *Source) {
0098 if (Source)
0099 FMF = Source->getFastMathFlags();
0100 }
0101 FMFSource(FastMathFlags FMF) : FMF(FMF) {}
0102 FastMathFlags get(FastMathFlags Default) const {
0103 return FMF.value_or(Default);
0104 }
0105
0106 static FMFSource intersect(Value *A, Value *B) {
0107 return FMFSource(cast<FPMathOperator>(A)->getFastMathFlags() &
0108 cast<FPMathOperator>(B)->getFastMathFlags());
0109 }
0110 };
0111
0112
0113 class IRBuilderBase {
0114
0115
0116 SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
0117
0118
0119
0120 void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
0121 if (!MD) {
0122 erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
0123 return KV.first == Kind;
0124 });
0125 return;
0126 }
0127
0128 for (auto &KV : MetadataToCopy)
0129 if (KV.first == Kind) {
0130 KV.second = MD;
0131 return;
0132 }
0133
0134 MetadataToCopy.emplace_back(Kind, MD);
0135 }
0136
0137 protected:
0138 BasicBlock *BB;
0139 BasicBlock::iterator InsertPt;
0140 LLVMContext &Context;
0141 const IRBuilderFolder &Folder;
0142 const IRBuilderDefaultInserter &Inserter;
0143
0144 MDNode *DefaultFPMathTag;
0145 FastMathFlags FMF;
0146
0147 bool IsFPConstrained = false;
0148 fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
0149 RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
0150
0151 ArrayRef<OperandBundleDef> DefaultOperandBundles;
0152
0153 public:
0154 IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
0155 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
0156 ArrayRef<OperandBundleDef> OpBundles)
0157 : Context(context), Folder(Folder), Inserter(Inserter),
0158 DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
0159 ClearInsertionPoint();
0160 }
0161
0162
0163 template<typename InstTy>
0164 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
0165 Inserter.InsertHelper(I, Name, InsertPt);
0166 AddMetadataToInst(I);
0167 return I;
0168 }
0169
0170
0171 Constant *Insert(Constant *C, const Twine& = "") const {
0172 return C;
0173 }
0174
0175 Value *Insert(Value *V, const Twine &Name = "") const {
0176 if (Instruction *I = dyn_cast<Instruction>(V))
0177 return Insert(I, Name);
0178 assert(isa<Constant>(V));
0179 return V;
0180 }
0181
0182
0183
0184
0185
0186
0187
0188 void ClearInsertionPoint() {
0189 BB = nullptr;
0190 InsertPt = BasicBlock::iterator();
0191 }
0192
0193 BasicBlock *GetInsertBlock() const { return BB; }
0194 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
0195 LLVMContext &getContext() const { return Context; }
0196
0197
0198
0199 void SetInsertPoint(BasicBlock *TheBB) {
0200 BB = TheBB;
0201 InsertPt = BB->end();
0202 }
0203
0204
0205
0206 void SetInsertPoint(Instruction *I) {
0207 BB = I->getParent();
0208 InsertPt = I->getIterator();
0209 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
0210 SetCurrentDebugLocation(I->getStableDebugLoc());
0211 }
0212
0213
0214
0215 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
0216 BB = TheBB;
0217 InsertPt = IP;
0218 if (IP != TheBB->end())
0219 SetCurrentDebugLocation(IP->getStableDebugLoc());
0220 }
0221
0222
0223
0224 void SetInsertPoint(BasicBlock::iterator IP) {
0225 BB = IP->getParent();
0226 InsertPt = IP;
0227 SetCurrentDebugLocation(IP->getStableDebugLoc());
0228 }
0229
0230
0231
0232
0233 void SetInsertPointPastAllocas(Function *F) {
0234 BB = &F->getEntryBlock();
0235 InsertPt = BB->getFirstNonPHIOrDbgOrAlloca();
0236 }
0237
0238
0239 void SetCurrentDebugLocation(DebugLoc L) {
0240 AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
0241 }
0242
0243
0244 void SetNoSanitizeMetadata() {
0245 AddOrRemoveMetadataToCopy(llvm::LLVMContext::MD_nosanitize,
0246 llvm::MDNode::get(getContext(), {}));
0247 }
0248
0249
0250
0251
0252 void CollectMetadataToCopy(Instruction *Src,
0253 ArrayRef<unsigned> MetadataKinds) {
0254 for (unsigned K : MetadataKinds)
0255 AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
0256 }
0257
0258
0259 DebugLoc getCurrentDebugLocation() const;
0260
0261
0262
0263 void SetInstDebugLocation(Instruction *I) const;
0264
0265
0266 void AddMetadataToInst(Instruction *I) const {
0267 for (const auto &KV : MetadataToCopy)
0268 I->setMetadata(KV.first, KV.second);
0269 }
0270
0271
0272
0273 Type *getCurrentFunctionReturnType() const;
0274
0275
0276 class InsertPoint {
0277 BasicBlock *Block = nullptr;
0278 BasicBlock::iterator Point;
0279
0280 public:
0281
0282 InsertPoint() = default;
0283
0284
0285 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
0286 : Block(InsertBlock), Point(InsertPoint) {}
0287
0288
0289 bool isSet() const { return (Block != nullptr); }
0290
0291 BasicBlock *getBlock() const { return Block; }
0292 BasicBlock::iterator getPoint() const { return Point; }
0293 };
0294
0295
0296 InsertPoint saveIP() const {
0297 return InsertPoint(GetInsertBlock(), GetInsertPoint());
0298 }
0299
0300
0301 InsertPoint saveAndClearIP() {
0302 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
0303 ClearInsertionPoint();
0304 return IP;
0305 }
0306
0307
0308 void restoreIP(InsertPoint IP) {
0309 if (IP.isSet())
0310 SetInsertPoint(IP.getBlock(), IP.getPoint());
0311 else
0312 ClearInsertionPoint();
0313 }
0314
0315
0316 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
0317
0318
0319 FastMathFlags getFastMathFlags() const { return FMF; }
0320
0321 FastMathFlags &getFastMathFlags() { return FMF; }
0322
0323
0324 void clearFastMathFlags() { FMF.clear(); }
0325
0326
0327 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
0328
0329
0330 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
0331
0332
0333
0334
0335
0336 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
0337
0338
0339 bool getIsFPConstrained() { return IsFPConstrained; }
0340
0341
0342 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
0343 #ifndef NDEBUG
0344 std::optional<StringRef> ExceptStr =
0345 convertExceptionBehaviorToStr(NewExcept);
0346 assert(ExceptStr && "Garbage strict exception behavior!");
0347 #endif
0348 DefaultConstrainedExcept = NewExcept;
0349 }
0350
0351
0352 void setDefaultConstrainedRounding(RoundingMode NewRounding) {
0353 #ifndef NDEBUG
0354 std::optional<StringRef> RoundingStr =
0355 convertRoundingModeToStr(NewRounding);
0356 assert(RoundingStr && "Garbage strict rounding mode!");
0357 #endif
0358 DefaultConstrainedRounding = NewRounding;
0359 }
0360
0361
0362 fp::ExceptionBehavior getDefaultConstrainedExcept() {
0363 return DefaultConstrainedExcept;
0364 }
0365
0366
0367 RoundingMode getDefaultConstrainedRounding() {
0368 return DefaultConstrainedRounding;
0369 }
0370
0371 void setConstrainedFPFunctionAttr() {
0372 assert(BB && "Must have a basic block to set any function attributes!");
0373
0374 Function *F = BB->getParent();
0375 if (!F->hasFnAttribute(Attribute::StrictFP)) {
0376 F->addFnAttr(Attribute::StrictFP);
0377 }
0378 }
0379
0380 void setConstrainedFPCallAttr(CallBase *I) {
0381 I->addFnAttr(Attribute::StrictFP);
0382 }
0383
0384 void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
0385 DefaultOperandBundles = OpBundles;
0386 }
0387
0388
0389
0390
0391
0392
0393
0394 class InsertPointGuard {
0395 IRBuilderBase &Builder;
0396 AssertingVH<BasicBlock> Block;
0397 BasicBlock::iterator Point;
0398 DebugLoc DbgLoc;
0399
0400 public:
0401 InsertPointGuard(IRBuilderBase &B)
0402 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
0403 DbgLoc(B.getCurrentDebugLocation()) {}
0404
0405 InsertPointGuard(const InsertPointGuard &) = delete;
0406 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
0407
0408 ~InsertPointGuard() {
0409 Builder.restoreIP(InsertPoint(Block, Point));
0410 Builder.SetCurrentDebugLocation(DbgLoc);
0411 }
0412 };
0413
0414
0415
0416 class FastMathFlagGuard {
0417 IRBuilderBase &Builder;
0418 FastMathFlags FMF;
0419 MDNode *FPMathTag;
0420 bool IsFPConstrained;
0421 fp::ExceptionBehavior DefaultConstrainedExcept;
0422 RoundingMode DefaultConstrainedRounding;
0423
0424 public:
0425 FastMathFlagGuard(IRBuilderBase &B)
0426 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
0427 IsFPConstrained(B.IsFPConstrained),
0428 DefaultConstrainedExcept(B.DefaultConstrainedExcept),
0429 DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
0430
0431 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
0432 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
0433
0434 ~FastMathFlagGuard() {
0435 Builder.FMF = FMF;
0436 Builder.DefaultFPMathTag = FPMathTag;
0437 Builder.IsFPConstrained = IsFPConstrained;
0438 Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
0439 Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
0440 }
0441 };
0442
0443
0444
0445 class OperandBundlesGuard {
0446 IRBuilderBase &Builder;
0447 ArrayRef<OperandBundleDef> DefaultOperandBundles;
0448
0449 public:
0450 OperandBundlesGuard(IRBuilderBase &B)
0451 : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
0452
0453 OperandBundlesGuard(const OperandBundlesGuard &) = delete;
0454 OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
0455
0456 ~OperandBundlesGuard() {
0457 Builder.DefaultOperandBundles = DefaultOperandBundles;
0458 }
0459 };
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
0476 unsigned AddressSpace = 0,
0477 Module *M = nullptr, bool AddNull = true);
0478
0479
0480 ConstantInt *getInt1(bool V) {
0481 return ConstantInt::get(getInt1Ty(), V);
0482 }
0483
0484
0485 ConstantInt *getTrue() {
0486 return ConstantInt::getTrue(Context);
0487 }
0488
0489
0490 ConstantInt *getFalse() {
0491 return ConstantInt::getFalse(Context);
0492 }
0493
0494
0495 ConstantInt *getInt8(uint8_t C) {
0496 return ConstantInt::get(getInt8Ty(), C);
0497 }
0498
0499
0500 ConstantInt *getInt16(uint16_t C) {
0501 return ConstantInt::get(getInt16Ty(), C);
0502 }
0503
0504
0505 ConstantInt *getInt32(uint32_t C) {
0506 return ConstantInt::get(getInt32Ty(), C);
0507 }
0508
0509
0510 ConstantInt *getInt64(uint64_t C) {
0511 return ConstantInt::get(getInt64Ty(), C);
0512 }
0513
0514
0515
0516 ConstantInt *getIntN(unsigned N, uint64_t C) {
0517 return ConstantInt::get(getIntNTy(N), C);
0518 }
0519
0520
0521 ConstantInt *getInt(const APInt &AI) {
0522 return ConstantInt::get(Context, AI);
0523 }
0524
0525
0526
0527
0528
0529
0530 IntegerType *getInt1Ty() {
0531 return Type::getInt1Ty(Context);
0532 }
0533
0534
0535 IntegerType *getInt8Ty() {
0536 return Type::getInt8Ty(Context);
0537 }
0538
0539
0540 IntegerType *getInt16Ty() {
0541 return Type::getInt16Ty(Context);
0542 }
0543
0544
0545 IntegerType *getInt32Ty() {
0546 return Type::getInt32Ty(Context);
0547 }
0548
0549
0550 IntegerType *getInt64Ty() {
0551 return Type::getInt64Ty(Context);
0552 }
0553
0554
0555 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
0556
0557
0558 IntegerType *getIntNTy(unsigned N) {
0559 return Type::getIntNTy(Context, N);
0560 }
0561
0562
0563 Type *getHalfTy() {
0564 return Type::getHalfTy(Context);
0565 }
0566
0567
0568 Type *getBFloatTy() {
0569 return Type::getBFloatTy(Context);
0570 }
0571
0572
0573 Type *getFloatTy() {
0574 return Type::getFloatTy(Context);
0575 }
0576
0577
0578 Type *getDoubleTy() {
0579 return Type::getDoubleTy(Context);
0580 }
0581
0582
0583 Type *getVoidTy() {
0584 return Type::getVoidTy(Context);
0585 }
0586
0587
0588 PointerType *getPtrTy(unsigned AddrSpace = 0) {
0589 return PointerType::get(Context, AddrSpace);
0590 }
0591
0592
0593
0594 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
0595 return DL.getIntPtrType(Context, AddrSpace);
0596 }
0597
0598
0599
0600 IntegerType *getIndexTy(const DataLayout &DL, unsigned AddrSpace) {
0601 return DL.getIndexType(Context, AddrSpace);
0602 }
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
0615 MaybeAlign Align, bool isVolatile = false,
0616 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
0617 MDNode *NoAliasTag = nullptr) {
0618 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
0619 TBAATag, ScopeTag, NoAliasTag);
0620 }
0621
0622 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
0623 bool isVolatile = false, MDNode *TBAATag = nullptr,
0624 MDNode *ScopeTag = nullptr,
0625 MDNode *NoAliasTag = nullptr);
0626
0627 CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
0628 Value *Size, bool IsVolatile = false,
0629 MDNode *TBAATag = nullptr,
0630 MDNode *ScopeTag = nullptr,
0631 MDNode *NoAliasTag = nullptr);
0632
0633
0634
0635
0636
0637
0638
0639 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
0640 uint64_t Size, Align Alignment,
0641 uint32_t ElementSize,
0642 MDNode *TBAATag = nullptr,
0643 MDNode *ScopeTag = nullptr,
0644 MDNode *NoAliasTag = nullptr) {
0645 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
0646 Align(Alignment), ElementSize,
0647 TBAATag, ScopeTag, NoAliasTag);
0648 }
0649
0650 CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
0651 Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
0652 Function *MallocF = nullptr, const Twine &Name = "");
0653
0654
0655
0656
0657
0658
0659 CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
0660 Value *ArraySize, Function *MallocF = nullptr,
0661 const Twine &Name = "");
0662
0663 CallInst *CreateFree(Value *Source, ArrayRef<OperandBundleDef> Bundles = {});
0664
0665 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
0666 Value *Size, Align Alignment,
0667 uint32_t ElementSize,
0668 MDNode *TBAATag = nullptr,
0669 MDNode *ScopeTag = nullptr,
0670 MDNode *NoAliasTag = nullptr);
0671
0672
0673
0674
0675
0676
0677 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
0678 MaybeAlign SrcAlign, uint64_t Size,
0679 bool isVolatile = false, MDNode *TBAATag = nullptr,
0680 MDNode *TBAAStructTag = nullptr,
0681 MDNode *ScopeTag = nullptr,
0682 MDNode *NoAliasTag = nullptr) {
0683 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
0684 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
0685 NoAliasTag);
0686 }
0687
0688 CallInst *CreateMemTransferInst(
0689 Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
0690 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
0691 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
0692 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
0693
0694 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
0695 MaybeAlign SrcAlign, Value *Size,
0696 bool isVolatile = false, MDNode *TBAATag = nullptr,
0697 MDNode *TBAAStructTag = nullptr,
0698 MDNode *ScopeTag = nullptr,
0699 MDNode *NoAliasTag = nullptr) {
0700 return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
0701 SrcAlign, Size, isVolatile, TBAATag,
0702 TBAAStructTag, ScopeTag, NoAliasTag);
0703 }
0704
0705 CallInst *
0706 CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
0707 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
0708 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
0709 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
0710 return CreateMemTransferInst(Intrinsic::memcpy_inline, Dst, DstAlign, Src,
0711 SrcAlign, Size, isVolatile, TBAATag,
0712 TBAAStructTag, ScopeTag, NoAliasTag);
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723 CallInst *CreateElementUnorderedAtomicMemCpy(
0724 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
0725 uint32_t ElementSize, MDNode *TBAATag = nullptr,
0726 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
0727 MDNode *NoAliasTag = nullptr);
0728
0729 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
0730 MaybeAlign SrcAlign, uint64_t Size,
0731 bool isVolatile = false, MDNode *TBAATag = nullptr,
0732 MDNode *ScopeTag = nullptr,
0733 MDNode *NoAliasTag = nullptr) {
0734 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
0735 isVolatile, TBAATag, ScopeTag, NoAliasTag);
0736 }
0737
0738 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
0739 MaybeAlign SrcAlign, Value *Size,
0740 bool isVolatile = false, MDNode *TBAATag = nullptr,
0741 MDNode *ScopeTag = nullptr,
0742 MDNode *NoAliasTag = nullptr) {
0743 return CreateMemTransferInst(Intrinsic::memmove, Dst, DstAlign, Src,
0744 SrcAlign, Size, isVolatile, TBAATag,
0745 nullptr, ScopeTag,
0746 NoAliasTag);
0747 }
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758 CallInst *CreateElementUnorderedAtomicMemMove(
0759 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
0760 uint32_t ElementSize, MDNode *TBAATag = nullptr,
0761 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
0762 MDNode *NoAliasTag = nullptr);
0763
0764 private:
0765 CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
0766
0767 public:
0768
0769
0770
0771
0772 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
0773
0774
0775
0776
0777
0778 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
0779
0780
0781 CallInst *CreateAddReduce(Value *Src);
0782
0783
0784 CallInst *CreateMulReduce(Value *Src);
0785
0786
0787 CallInst *CreateAndReduce(Value *Src);
0788
0789
0790 CallInst *CreateOrReduce(Value *Src);
0791
0792
0793 CallInst *CreateXorReduce(Value *Src);
0794
0795
0796
0797 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
0798
0799
0800
0801 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
0802
0803
0804
0805 CallInst *CreateFPMaxReduce(Value *Src);
0806
0807
0808
0809 CallInst *CreateFPMinReduce(Value *Src);
0810
0811
0812
0813
0814 CallInst *CreateFPMaximumReduce(Value *Src);
0815
0816
0817
0818
0819 CallInst *CreateFPMinimumReduce(Value *Src);
0820
0821
0822
0823
0824 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
0825
0826
0827
0828
0829 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
0830
0831
0832
0833
0834 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
0835
0836
0837 CallInst *CreateThreadLocalAddress(Value *Ptr);
0838
0839
0840 CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
0841 Value *PassThru = nullptr, const Twine &Name = "");
0842
0843
0844 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
0845 Value *Mask);
0846
0847
0848 CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
0849 Value *Mask = nullptr, Value *PassThru = nullptr,
0850 const Twine &Name = "");
0851
0852
0853 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
0854 Value *Mask = nullptr);
0855
0856
0857 CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
0858 Value *Mask = nullptr,
0859 Value *PassThru = nullptr,
0860 const Twine &Name = "");
0861
0862
0863 CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr, MaybeAlign Align,
0864 Value *Mask = nullptr);
0865
0866
0867 Value *getAllOnesMask(ElementCount NumElts) {
0868 VectorType *VTy = VectorType::get(Type::getInt1Ty(Context), NumElts);
0869 return Constant::getAllOnesValue(VTy);
0870 }
0871
0872
0873
0874
0875
0876
0877 CallInst *CreateAssumption(Value *Cond,
0878 ArrayRef<OperandBundleDef> OpBundles = {});
0879
0880
0881 Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
0882 Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
0883 return CreateNoAliasScopeDeclaration(
0884 MetadataAsValue::get(Context, ScopeTag));
0885 }
0886
0887
0888
0889 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0890 FunctionCallee ActualCallee,
0891 ArrayRef<Value *> CallArgs,
0892 std::optional<ArrayRef<Value *>> DeoptArgs,
0893 ArrayRef<Value *> GCArgs,
0894 const Twine &Name = "");
0895
0896
0897
0898 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0899 FunctionCallee ActualCallee, uint32_t Flags,
0900 ArrayRef<Value *> CallArgs,
0901 std::optional<ArrayRef<Use>> TransitionArgs,
0902 std::optional<ArrayRef<Use>> DeoptArgs,
0903 ArrayRef<Value *> GCArgs,
0904 const Twine &Name = "");
0905
0906
0907
0908
0909 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0910 FunctionCallee ActualCallee,
0911 ArrayRef<Use> CallArgs,
0912 std::optional<ArrayRef<Value *>> DeoptArgs,
0913 ArrayRef<Value *> GCArgs,
0914 const Twine &Name = "");
0915
0916
0917
0918 InvokeInst *
0919 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
0920 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
0921 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
0922 std::optional<ArrayRef<Value *>> DeoptArgs,
0923 ArrayRef<Value *> GCArgs, const Twine &Name = "");
0924
0925
0926
0927 InvokeInst *CreateGCStatepointInvoke(
0928 uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
0929 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
0930 ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs,
0931 std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
0932 const Twine &Name = "");
0933
0934
0935
0936
0937 InvokeInst *
0938 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
0939 FunctionCallee ActualInvokee, BasicBlock *NormalDest,
0940 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
0941 std::optional<ArrayRef<Value *>> DeoptArgs,
0942 ArrayRef<Value *> GCArgs, const Twine &Name = "");
0943
0944
0945
0946 CallInst *CreateGCResult(Instruction *Statepoint,
0947 Type *ResultType,
0948 const Twine &Name = "");
0949
0950
0951
0952 CallInst *CreateGCRelocate(Instruction *Statepoint,
0953 int BaseOffset,
0954 int DerivedOffset,
0955 Type *ResultType,
0956 const Twine &Name = "");
0957
0958
0959
0960 CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
0961
0962
0963
0964 CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
0965
0966
0967
0968 Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
0969
0970
0971
0972 Value *CreateElementCount(Type *DstType, ElementCount EC);
0973
0974
0975
0976 Value *CreateTypeSize(Type *DstType, TypeSize Size);
0977
0978
0979 Value *CreateStepVector(Type *DstType, const Twine &Name = "");
0980
0981
0982
0983 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
0984 FMFSource FMFSource = {},
0985 const Twine &Name = "");
0986
0987
0988
0989 Value *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
0990 FMFSource FMFSource = {},
0991 const Twine &Name = "");
0992
0993
0994
0995
0996 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
0997 ArrayRef<Value *> Args, FMFSource FMFSource = {},
0998 const Twine &Name = "");
0999
1000
1001
1002
1003 CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
1004 ArrayRef<Value *> Args, FMFSource FMFSource = {},
1005 const Twine &Name = "");
1006
1007
1008 Value *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1009 if (IsFPConstrained) {
1010 return CreateConstrainedFPUnroundedBinOp(
1011 Intrinsic::experimental_constrained_minnum, LHS, RHS, nullptr, Name);
1012 }
1013
1014 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
1015 }
1016
1017
1018 Value *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1019 if (IsFPConstrained) {
1020 return CreateConstrainedFPUnroundedBinOp(
1021 Intrinsic::experimental_constrained_maxnum, LHS, RHS, nullptr, Name);
1022 }
1023
1024 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
1025 }
1026
1027
1028 Value *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
1029 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
1030 }
1031
1032
1033 Value *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
1034 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
1035 }
1036
1037
1038 Value *CreateMinimumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1039 return CreateBinaryIntrinsic(Intrinsic::minimumnum, LHS, RHS, nullptr,
1040 Name);
1041 }
1042
1043
1044 Value *CreateMaximumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1045 return CreateBinaryIntrinsic(Intrinsic::maximumnum, LHS, RHS, nullptr,
1046 Name);
1047 }
1048
1049
1050 Value *CreateCopySign(Value *LHS, Value *RHS, FMFSource FMFSource = {},
1051 const Twine &Name = "") {
1052 return CreateBinaryIntrinsic(Intrinsic::copysign, LHS, RHS, FMFSource,
1053 Name);
1054 }
1055
1056
1057 Value *CreateLdexp(Value *Src, Value *Exp, FMFSource FMFSource = {},
1058 const Twine &Name = "") {
1059 assert(!IsFPConstrained && "TODO: Support strictfp");
1060 return CreateIntrinsic(Intrinsic::ldexp, {Src->getType(), Exp->getType()},
1061 {Src, Exp}, FMFSource, Name);
1062 }
1063
1064
1065 CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
1066 const Twine &Name = "") {
1067 return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
1068 Name);
1069 }
1070
1071
1072 CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
1073 const Twine &Name = "") {
1074 return CreateIntrinsic(Intrinsic::vector_extract,
1075 {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
1076 Name);
1077 }
1078
1079
1080 CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1081 Value *Idx, const Twine &Name = "") {
1082 return CreateIntrinsic(Intrinsic::vector_insert,
1083 {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
1084 nullptr, Name);
1085 }
1086
1087
1088 CallInst *CreateStackSave(const Twine &Name = "") {
1089 const DataLayout &DL = BB->getDataLayout();
1090 return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
1091 {}, nullptr, Name);
1092 }
1093
1094
1095 CallInst *CreateStackRestore(Value *Ptr, const Twine &Name = "") {
1096 return CreateIntrinsic(Intrinsic::stackrestore, {Ptr->getType()}, {Ptr},
1097 nullptr, Name);
1098 }
1099
1100
1101 Value *CreateCountTrailingZeroElems(Type *ResTy, Value *Mask,
1102 bool ZeroIsPoison = true,
1103 const Twine &Name = "") {
1104 return CreateIntrinsic(Intrinsic::experimental_cttz_elts,
1105 {ResTy, Mask->getType()},
1106 {Mask, getInt1(ZeroIsPoison)}, nullptr, Name);
1107 }
1108
1109 private:
1110
1111 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
1112 ArrayRef<Type *> OverloadedTypes,
1113 const Twine &Name = "");
1114
1115
1116
1117
1118
1119 private:
1120
1121
1122
1123 template <typename InstTy>
1124 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
1125 if (Weights)
1126 I->setMetadata(LLVMContext::MD_prof, Weights);
1127 if (Unpredictable)
1128 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
1129 return I;
1130 }
1131
1132 public:
1133
1134 ReturnInst *CreateRetVoid() {
1135 return Insert(ReturnInst::Create(Context));
1136 }
1137
1138
1139 ReturnInst *CreateRet(Value *V) {
1140 return Insert(ReturnInst::Create(Context, V));
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
1151 Value *V = PoisonValue::get(getCurrentFunctionReturnType());
1152 for (unsigned i = 0; i != N; ++i)
1153 V = CreateInsertValue(V, retVals[i], i, "mrv");
1154 return Insert(ReturnInst::Create(Context, V));
1155 }
1156
1157
1158 BranchInst *CreateBr(BasicBlock *Dest) {
1159 return Insert(BranchInst::Create(Dest));
1160 }
1161
1162
1163
1164 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1165 MDNode *BranchWeights = nullptr,
1166 MDNode *Unpredictable = nullptr) {
1167 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
1168 BranchWeights, Unpredictable));
1169 }
1170
1171
1172
1173 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1174 Instruction *MDSrc) {
1175 BranchInst *Br = BranchInst::Create(True, False, Cond);
1176 if (MDSrc) {
1177 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1178 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1179 Br->copyMetadata(*MDSrc, WL);
1180 }
1181 return Insert(Br);
1182 }
1183
1184
1185
1186
1187 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1188 MDNode *BranchWeights = nullptr,
1189 MDNode *Unpredictable = nullptr) {
1190 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1191 BranchWeights, Unpredictable));
1192 }
1193
1194
1195
1196
1197 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1198 return Insert(IndirectBrInst::Create(Addr, NumDests));
1199 }
1200
1201
1202 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1203 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1204 ArrayRef<Value *> Args,
1205 ArrayRef<OperandBundleDef> OpBundles,
1206 const Twine &Name = "") {
1207 InvokeInst *II =
1208 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
1209 if (IsFPConstrained)
1210 setConstrainedFPCallAttr(II);
1211 return Insert(II, Name);
1212 }
1213 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1214 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1215 ArrayRef<Value *> Args = {},
1216 const Twine &Name = "") {
1217 InvokeInst *II =
1218 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
1219 if (IsFPConstrained)
1220 setConstrainedFPCallAttr(II);
1221 return Insert(II, Name);
1222 }
1223
1224 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1225 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1226 ArrayRef<OperandBundleDef> OpBundles,
1227 const Twine &Name = "") {
1228 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1229 NormalDest, UnwindDest, Args, OpBundles, Name);
1230 }
1231
1232 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1233 BasicBlock *UnwindDest, ArrayRef<Value *> Args = {},
1234 const Twine &Name = "") {
1235 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1236 NormalDest, UnwindDest, Args, Name);
1237 }
1238
1239
1240 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1241 BasicBlock *DefaultDest,
1242 ArrayRef<BasicBlock *> IndirectDests,
1243 ArrayRef<Value *> Args = {},
1244 const Twine &Name = "") {
1245 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1246 Args), Name);
1247 }
1248 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1249 BasicBlock *DefaultDest,
1250 ArrayRef<BasicBlock *> IndirectDests,
1251 ArrayRef<Value *> Args,
1252 ArrayRef<OperandBundleDef> OpBundles,
1253 const Twine &Name = "") {
1254 return Insert(
1255 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1256 OpBundles), Name);
1257 }
1258
1259 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1260 ArrayRef<BasicBlock *> IndirectDests,
1261 ArrayRef<Value *> Args = {},
1262 const Twine &Name = "") {
1263 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1264 DefaultDest, IndirectDests, Args, Name);
1265 }
1266 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1267 ArrayRef<BasicBlock *> IndirectDests,
1268 ArrayRef<Value *> Args,
1269 ArrayRef<OperandBundleDef> OpBundles,
1270 const Twine &Name = "") {
1271 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1272 DefaultDest, IndirectDests, Args, Name);
1273 }
1274
1275 ResumeInst *CreateResume(Value *Exn) {
1276 return Insert(ResumeInst::Create(Exn));
1277 }
1278
1279 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1280 BasicBlock *UnwindBB = nullptr) {
1281 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1282 }
1283
1284 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1285 unsigned NumHandlers,
1286 const Twine &Name = "") {
1287 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1288 Name);
1289 }
1290
1291 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1292 const Twine &Name = "") {
1293 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1294 }
1295
1296 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1297 ArrayRef<Value *> Args = {},
1298 const Twine &Name = "") {
1299 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1300 }
1301
1302 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1303 return Insert(CatchReturnInst::Create(CatchPad, BB));
1304 }
1305
1306 UnreachableInst *CreateUnreachable() {
1307 return Insert(new UnreachableInst(Context));
1308 }
1309
1310
1311
1312
1313 private:
1314 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1315 Value *LHS, Value *RHS,
1316 const Twine &Name,
1317 bool HasNUW, bool HasNSW) {
1318 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1319 if (HasNUW) BO->setHasNoUnsignedWrap();
1320 if (HasNSW) BO->setHasNoSignedWrap();
1321 return BO;
1322 }
1323
1324 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1325 FastMathFlags FMF) const {
1326 if (!FPMD)
1327 FPMD = DefaultFPMathTag;
1328 if (FPMD)
1329 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1330 I->setFastMathFlags(FMF);
1331 return I;
1332 }
1333
1334 Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) {
1335 RoundingMode UseRounding = DefaultConstrainedRounding;
1336
1337 if (Rounding)
1338 UseRounding = *Rounding;
1339
1340 std::optional<StringRef> RoundingStr =
1341 convertRoundingModeToStr(UseRounding);
1342 assert(RoundingStr && "Garbage strict rounding mode!");
1343 auto *RoundingMDS = MDString::get(Context, *RoundingStr);
1344
1345 return MetadataAsValue::get(Context, RoundingMDS);
1346 }
1347
1348 Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) {
1349 std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(
1350 Except.value_or(DefaultConstrainedExcept));
1351 assert(ExceptStr && "Garbage strict exception behavior!");
1352 auto *ExceptMDS = MDString::get(Context, *ExceptStr);
1353
1354 return MetadataAsValue::get(Context, ExceptMDS);
1355 }
1356
1357 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1358 assert(CmpInst::isFPPredicate(Predicate) &&
1359 Predicate != CmpInst::FCMP_FALSE &&
1360 Predicate != CmpInst::FCMP_TRUE &&
1361 "Invalid constrained FP comparison predicate!");
1362
1363 StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1364 auto *PredicateMDS = MDString::get(Context, PredicateStr);
1365
1366 return MetadataAsValue::get(Context, PredicateMDS);
1367 }
1368
1369 public:
1370 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1371 bool HasNUW = false, bool HasNSW = false) {
1372 if (Value *V =
1373 Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1374 return V;
1375 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW,
1376 HasNSW);
1377 }
1378
1379 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1380 return CreateAdd(LHS, RHS, Name, false, true);
1381 }
1382
1383 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1384 return CreateAdd(LHS, RHS, Name, true, false);
1385 }
1386
1387 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1388 bool HasNUW = false, bool HasNSW = false) {
1389 if (Value *V =
1390 Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1391 return V;
1392 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW,
1393 HasNSW);
1394 }
1395
1396 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1397 return CreateSub(LHS, RHS, Name, false, true);
1398 }
1399
1400 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1401 return CreateSub(LHS, RHS, Name, true, false);
1402 }
1403
1404 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1405 bool HasNUW = false, bool HasNSW = false) {
1406 if (Value *V =
1407 Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1408 return V;
1409 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW,
1410 HasNSW);
1411 }
1412
1413 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1414 return CreateMul(LHS, RHS, Name, false, true);
1415 }
1416
1417 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1418 return CreateMul(LHS, RHS, Name, true, false);
1419 }
1420
1421 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1422 bool isExact = false) {
1423 if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact))
1424 return V;
1425 if (!isExact)
1426 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1427 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1428 }
1429
1430 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1431 return CreateUDiv(LHS, RHS, Name, true);
1432 }
1433
1434 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1435 bool isExact = false) {
1436 if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact))
1437 return V;
1438 if (!isExact)
1439 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1440 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1441 }
1442
1443 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1444 return CreateSDiv(LHS, RHS, Name, true);
1445 }
1446
1447 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1448 if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS))
1449 return V;
1450 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1451 }
1452
1453 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1454 if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS))
1455 return V;
1456 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1457 }
1458
1459 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1460 bool HasNUW = false, bool HasNSW = false) {
1461 if (Value *V =
1462 Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1463 return V;
1464 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1465 HasNUW, HasNSW);
1466 }
1467
1468 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1469 bool HasNUW = false, bool HasNSW = false) {
1470 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1471 HasNUW, HasNSW);
1472 }
1473
1474 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1475 bool HasNUW = false, bool HasNSW = false) {
1476 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1477 HasNUW, HasNSW);
1478 }
1479
1480 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1481 bool isExact = false) {
1482 if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact))
1483 return V;
1484 if (!isExact)
1485 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1486 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1487 }
1488
1489 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1490 bool isExact = false) {
1491 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1492 }
1493
1494 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1495 bool isExact = false) {
1496 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1497 }
1498
1499 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1500 bool isExact = false) {
1501 if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact))
1502 return V;
1503 if (!isExact)
1504 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1505 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1506 }
1507
1508 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1509 bool isExact = false) {
1510 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1511 }
1512
1513 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1514 bool isExact = false) {
1515 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1516 }
1517
1518 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1519 if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS))
1520 return V;
1521 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1522 }
1523
1524 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1525 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1526 }
1527
1528 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1529 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1530 }
1531
1532 Value *CreateAnd(ArrayRef<Value*> Ops) {
1533 assert(!Ops.empty());
1534 Value *Accum = Ops[0];
1535 for (unsigned i = 1; i < Ops.size(); i++)
1536 Accum = CreateAnd(Accum, Ops[i]);
1537 return Accum;
1538 }
1539
1540 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1541 if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS))
1542 return V;
1543 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1544 }
1545
1546 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1547 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1548 }
1549
1550 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1551 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1552 }
1553
1554 Value *CreateOr(ArrayRef<Value*> Ops) {
1555 assert(!Ops.empty());
1556 Value *Accum = Ops[0];
1557 for (unsigned i = 1; i < Ops.size(); i++)
1558 Accum = CreateOr(Accum, Ops[i]);
1559 return Accum;
1560 }
1561
1562 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1563 if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS))
1564 return V;
1565 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1566 }
1567
1568 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1569 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1570 }
1571
1572 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1573 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1574 }
1575
1576 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1577 MDNode *FPMD = nullptr) {
1578 return CreateFAddFMF(L, R, {}, Name, FPMD);
1579 }
1580
1581 Value *CreateFAddFMF(Value *L, Value *R, FMFSource FMFSource,
1582 const Twine &Name = "", MDNode *FPMD = nullptr) {
1583 if (IsFPConstrained)
1584 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1585 L, R, FMFSource, Name, FPMD);
1586
1587 if (Value *V =
1588 Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMFSource.get(FMF)))
1589 return V;
1590 Instruction *I =
1591 setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMFSource.get(FMF));
1592 return Insert(I, Name);
1593 }
1594
1595 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1596 MDNode *FPMD = nullptr) {
1597 return CreateFSubFMF(L, R, {}, Name, FPMD);
1598 }
1599
1600 Value *CreateFSubFMF(Value *L, Value *R, FMFSource FMFSource,
1601 const Twine &Name = "", MDNode *FPMD = nullptr) {
1602 if (IsFPConstrained)
1603 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1604 L, R, FMFSource, Name, FPMD);
1605
1606 if (Value *V =
1607 Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMFSource.get(FMF)))
1608 return V;
1609 Instruction *I =
1610 setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMFSource.get(FMF));
1611 return Insert(I, Name);
1612 }
1613
1614 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1615 MDNode *FPMD = nullptr) {
1616 return CreateFMulFMF(L, R, {}, Name, FPMD);
1617 }
1618
1619 Value *CreateFMulFMF(Value *L, Value *R, FMFSource FMFSource,
1620 const Twine &Name = "", MDNode *FPMD = nullptr) {
1621 if (IsFPConstrained)
1622 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1623 L, R, FMFSource, Name, FPMD);
1624
1625 if (Value *V =
1626 Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMFSource.get(FMF)))
1627 return V;
1628 Instruction *I =
1629 setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMFSource.get(FMF));
1630 return Insert(I, Name);
1631 }
1632
1633 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1634 MDNode *FPMD = nullptr) {
1635 return CreateFDivFMF(L, R, {}, Name, FPMD);
1636 }
1637
1638 Value *CreateFDivFMF(Value *L, Value *R, FMFSource FMFSource,
1639 const Twine &Name = "", MDNode *FPMD = nullptr) {
1640 if (IsFPConstrained)
1641 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1642 L, R, FMFSource, Name, FPMD);
1643
1644 if (Value *V =
1645 Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMFSource.get(FMF)))
1646 return V;
1647 Instruction *I =
1648 setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMFSource.get(FMF));
1649 return Insert(I, Name);
1650 }
1651
1652 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1653 MDNode *FPMD = nullptr) {
1654 return CreateFRemFMF(L, R, {}, Name, FPMD);
1655 }
1656
1657 Value *CreateFRemFMF(Value *L, Value *R, FMFSource FMFSource,
1658 const Twine &Name = "", MDNode *FPMD = nullptr) {
1659 if (IsFPConstrained)
1660 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1661 L, R, FMFSource, Name, FPMD);
1662
1663 if (Value *V =
1664 Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMFSource.get(FMF)))
1665 return V;
1666 Instruction *I =
1667 setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMFSource.get(FMF));
1668 return Insert(I, Name);
1669 }
1670
1671 Value *CreateBinOp(Instruction::BinaryOps Opc,
1672 Value *LHS, Value *RHS, const Twine &Name = "",
1673 MDNode *FPMathTag = nullptr) {
1674 return CreateBinOpFMF(Opc, LHS, RHS, {}, Name, FPMathTag);
1675 }
1676
1677 Value *CreateBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
1678 FMFSource FMFSource, const Twine &Name = "",
1679 MDNode *FPMathTag = nullptr) {
1680 if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS))
1681 return V;
1682 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1683 if (isa<FPMathOperator>(BinOp))
1684 setFPAttrs(BinOp, FPMathTag, FMFSource.get(FMF));
1685 return Insert(BinOp, Name);
1686 }
1687
1688 Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1689 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1690 return CreateSelect(Cond1, Cond2,
1691 ConstantInt::getNullValue(Cond2->getType()), Name);
1692 }
1693
1694 Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1695 assert(Cond2->getType()->isIntOrIntVectorTy(1));
1696 return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
1697 Cond2, Name);
1698 }
1699
1700 Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
1701 const Twine &Name = "") {
1702 switch (Opc) {
1703 case Instruction::And:
1704 return CreateLogicalAnd(Cond1, Cond2, Name);
1705 case Instruction::Or:
1706 return CreateLogicalOr(Cond1, Cond2, Name);
1707 default:
1708 break;
1709 }
1710 llvm_unreachable("Not a logical operation.");
1711 }
1712
1713
1714 Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1715 assert(!Ops.empty());
1716 Value *Accum = Ops[0];
1717 for (unsigned i = 1; i < Ops.size(); i++)
1718 Accum = CreateLogicalOr(Accum, Ops[i]);
1719 return Accum;
1720 }
1721
1722 CallInst *CreateConstrainedFPBinOp(
1723 Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1724 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1725 std::optional<RoundingMode> Rounding = std::nullopt,
1726 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1727
1728 CallInst *CreateConstrainedFPUnroundedBinOp(
1729 Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1730 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1731 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1732
1733 Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNSW = false) {
1734 return CreateSub(Constant::getNullValue(V->getType()), V, Name,
1735 0, HasNSW);
1736 }
1737
1738 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1739 return CreateNeg(V, Name, true);
1740 }
1741
1742 Value *CreateFNeg(Value *V, const Twine &Name = "",
1743 MDNode *FPMathTag = nullptr) {
1744 return CreateFNegFMF(V, {}, Name, FPMathTag);
1745 }
1746
1747 Value *CreateFNegFMF(Value *V, FMFSource FMFSource, const Twine &Name = "",
1748 MDNode *FPMathTag = nullptr) {
1749 if (Value *Res =
1750 Folder.FoldUnOpFMF(Instruction::FNeg, V, FMFSource.get(FMF)))
1751 return Res;
1752 return Insert(
1753 setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMFSource.get(FMF)),
1754 Name);
1755 }
1756
1757 Value *CreateNot(Value *V, const Twine &Name = "") {
1758 return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name);
1759 }
1760
1761 Value *CreateUnOp(Instruction::UnaryOps Opc,
1762 Value *V, const Twine &Name = "",
1763 MDNode *FPMathTag = nullptr) {
1764 if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1765 return Res;
1766 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1767 if (isa<FPMathOperator>(UnOp))
1768 setFPAttrs(UnOp, FPMathTag, FMF);
1769 return Insert(UnOp, Name);
1770 }
1771
1772
1773
1774 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1775 const Twine &Name = "", MDNode *FPMathTag = nullptr);
1776
1777
1778
1779
1780
1781 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1782 Value *ArraySize = nullptr, const Twine &Name = "") {
1783 const DataLayout &DL = BB->getDataLayout();
1784 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1785 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1786 }
1787
1788 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1789 const Twine &Name = "") {
1790 const DataLayout &DL = BB->getDataLayout();
1791 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1792 unsigned AddrSpace = DL.getAllocaAddrSpace();
1793 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1794 }
1795
1796
1797
1798 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1799 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1800 }
1801
1802 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1803 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1804 }
1805
1806 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1807 const Twine &Name = "") {
1808 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1809 }
1810
1811 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1812 return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1813 }
1814
1815 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1816 const char *Name) {
1817 return CreateAlignedLoad(Ty, Ptr, Align, false, Name);
1818 }
1819
1820 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1821 const Twine &Name = "") {
1822 return CreateAlignedLoad(Ty, Ptr, Align, false, Name);
1823 }
1824
1825 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1826 bool isVolatile, const Twine &Name = "") {
1827 if (!Align) {
1828 const DataLayout &DL = BB->getDataLayout();
1829 Align = DL.getABITypeAlign(Ty);
1830 }
1831 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1832 }
1833
1834 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1835 bool isVolatile = false) {
1836 if (!Align) {
1837 const DataLayout &DL = BB->getDataLayout();
1838 Align = DL.getABITypeAlign(Val->getType());
1839 }
1840 return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1841 }
1842 FenceInst *CreateFence(AtomicOrdering Ordering,
1843 SyncScope::ID SSID = SyncScope::System,
1844 const Twine &Name = "") {
1845 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1846 }
1847
1848 AtomicCmpXchgInst *
1849 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1850 AtomicOrdering SuccessOrdering,
1851 AtomicOrdering FailureOrdering,
1852 SyncScope::ID SSID = SyncScope::System) {
1853 if (!Align) {
1854 const DataLayout &DL = BB->getDataLayout();
1855 Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
1856 }
1857
1858 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1859 FailureOrdering, SSID));
1860 }
1861
1862 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1863 Value *Val, MaybeAlign Align,
1864 AtomicOrdering Ordering,
1865 SyncScope::ID SSID = SyncScope::System) {
1866 if (!Align) {
1867 const DataLayout &DL = BB->getDataLayout();
1868 Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
1869 }
1870
1871 return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1872 }
1873
1874 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1875 const Twine &Name = "",
1876 GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1877 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, NW))
1878 return V;
1879 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList, NW), Name);
1880 }
1881
1882 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1883 const Twine &Name = "") {
1884 return CreateGEP(Ty, Ptr, IdxList, Name, GEPNoWrapFlags::inBounds());
1885 }
1886
1887 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1888 const Twine &Name = "") {
1889 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1890
1891 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1892 return V;
1893
1894 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1895 }
1896
1897 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1898 const Twine &Name = "") {
1899 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1900
1901 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1902 return V;
1903
1904 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1905 }
1906
1907 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1908 const Twine &Name = "",
1909 GEPNoWrapFlags NWFlags = GEPNoWrapFlags::none()) {
1910 Value *Idxs[] = {
1911 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1912 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1913 };
1914
1915 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, NWFlags))
1916 return V;
1917
1918 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs, NWFlags), Name);
1919 }
1920
1921 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1922 unsigned Idx1, const Twine &Name = "") {
1923 Value *Idxs[] = {
1924 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1925 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1926 };
1927
1928 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1929 return V;
1930
1931 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1932 }
1933
1934 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1935 const Twine &Name = "") {
1936 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1937
1938 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1939 return V;
1940
1941 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1942 }
1943
1944 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1945 const Twine &Name = "") {
1946 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1947
1948 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1949 return V;
1950
1951 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1952 }
1953
1954 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1955 const Twine &Name = "") {
1956 Value *Idxs[] = {
1957 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1958 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1959 };
1960
1961 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::none()))
1962 return V;
1963
1964 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1965 }
1966
1967 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1968 uint64_t Idx1, const Twine &Name = "") {
1969 Value *Idxs[] = {
1970 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1971 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1972 };
1973
1974 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1975 return V;
1976
1977 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1978 }
1979
1980 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1981 const Twine &Name = "") {
1982 GEPNoWrapFlags NWFlags =
1983 GEPNoWrapFlags::inBounds() | GEPNoWrapFlags::noUnsignedWrap();
1984 return CreateConstGEP2_32(Ty, Ptr, 0, Idx, Name, NWFlags);
1985 }
1986
1987 Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
1988 GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1989 return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
1990 }
1991
1992 Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
1993 const Twine &Name = "") {
1994 return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
1995 GEPNoWrapFlags::inBounds());
1996 }
1997
1998
1999
2000
2001
2002
2003 LLVM_DEPRECATED("Use CreateGlobalString instead", "CreateGlobalString")
2004 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
2005 unsigned AddressSpace = 0,
2006 Module *M = nullptr, bool AddNull = true) {
2007 GlobalVariable *GV =
2008 CreateGlobalString(Str, Name, AddressSpace, M, AddNull);
2009 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2010 Constant *Indices[] = {Zero, Zero};
2011 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
2012 Indices);
2013 }
2014
2015
2016
2017
2018
2019 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2020 bool IsNUW = false, bool IsNSW = false) {
2021 if (V->getType() == DestTy)
2022 return V;
2023 if (Value *Folded = Folder.FoldCast(Instruction::Trunc, V, DestTy))
2024 return Folded;
2025 Instruction *I = CastInst::Create(Instruction::Trunc, V, DestTy);
2026 if (IsNUW)
2027 I->setHasNoUnsignedWrap();
2028 if (IsNSW)
2029 I->setHasNoSignedWrap();
2030 return Insert(I, Name);
2031 }
2032
2033 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",
2034 bool IsNonNeg = false) {
2035 if (V->getType() == DestTy)
2036 return V;
2037 if (Value *Folded = Folder.FoldCast(Instruction::ZExt, V, DestTy))
2038 return Folded;
2039 Instruction *I = Insert(new ZExtInst(V, DestTy), Name);
2040 if (IsNonNeg)
2041 I->setNonNeg();
2042 return I;
2043 }
2044
2045 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
2046 return CreateCast(Instruction::SExt, V, DestTy, Name);
2047 }
2048
2049
2050
2051 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
2052 const Twine &Name = "") {
2053 assert(V->getType()->isIntOrIntVectorTy() &&
2054 DestTy->isIntOrIntVectorTy() &&
2055 "Can only zero extend/truncate integers!");
2056 Type *VTy = V->getType();
2057 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2058 return CreateZExt(V, DestTy, Name);
2059 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2060 return CreateTrunc(V, DestTy, Name);
2061 return V;
2062 }
2063
2064
2065
2066 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2067 const Twine &Name = "") {
2068 assert(V->getType()->isIntOrIntVectorTy() &&
2069 DestTy->isIntOrIntVectorTy() &&
2070 "Can only sign extend/truncate integers!");
2071 Type *VTy = V->getType();
2072 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2073 return CreateSExt(V, DestTy, Name);
2074 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2075 return CreateTrunc(V, DestTy, Name);
2076 return V;
2077 }
2078
2079 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2080 if (IsFPConstrained)
2081 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
2082 V, DestTy, nullptr, Name);
2083 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
2084 }
2085
2086 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2087 if (IsFPConstrained)
2088 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
2089 V, DestTy, nullptr, Name);
2090 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
2091 }
2092
2093 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = "",
2094 bool IsNonNeg = false) {
2095 if (IsFPConstrained)
2096 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
2097 V, DestTy, nullptr, Name);
2098 if (Value *Folded = Folder.FoldCast(Instruction::UIToFP, V, DestTy))
2099 return Folded;
2100 Instruction *I = Insert(new UIToFPInst(V, DestTy), Name);
2101 if (IsNonNeg)
2102 I->setNonNeg();
2103 return I;
2104 }
2105
2106 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2107 if (IsFPConstrained)
2108 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
2109 V, DestTy, nullptr, Name);
2110 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
2111 }
2112
2113 Value *CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2114 MDNode *FPMathTag = nullptr) {
2115 return CreateFPTruncFMF(V, DestTy, {}, Name, FPMathTag);
2116 }
2117
2118 Value *CreateFPTruncFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2119 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2120 if (IsFPConstrained)
2121 return CreateConstrainedFPCast(
2122 Intrinsic::experimental_constrained_fptrunc, V, DestTy, FMFSource,
2123 Name, FPMathTag);
2124 return CreateCast(Instruction::FPTrunc, V, DestTy, Name, FPMathTag,
2125 FMFSource);
2126 }
2127
2128 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "",
2129 MDNode *FPMathTag = nullptr) {
2130 return CreateFPExtFMF(V, DestTy, {}, Name, FPMathTag);
2131 }
2132
2133 Value *CreateFPExtFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2134 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2135 if (IsFPConstrained)
2136 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
2137 V, DestTy, FMFSource, Name, FPMathTag);
2138 return CreateCast(Instruction::FPExt, V, DestTy, Name, FPMathTag,
2139 FMFSource);
2140 }
2141
2142 Value *CreatePtrToInt(Value *V, Type *DestTy,
2143 const Twine &Name = "") {
2144 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
2145 }
2146
2147 Value *CreateIntToPtr(Value *V, Type *DestTy,
2148 const Twine &Name = "") {
2149 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
2150 }
2151
2152 Value *CreateBitCast(Value *V, Type *DestTy,
2153 const Twine &Name = "") {
2154 return CreateCast(Instruction::BitCast, V, DestTy, Name);
2155 }
2156
2157 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2158 const Twine &Name = "") {
2159 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
2160 }
2161
2162 Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2163 Instruction::CastOps CastOp =
2164 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2165 ? Instruction::BitCast
2166 : Instruction::ZExt;
2167 return CreateCast(CastOp, V, DestTy, Name);
2168 }
2169
2170 Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2171 Instruction::CastOps CastOp =
2172 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2173 ? Instruction::BitCast
2174 : Instruction::SExt;
2175 return CreateCast(CastOp, V, DestTy, Name);
2176 }
2177
2178 Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2179 Instruction::CastOps CastOp =
2180 V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2181 ? Instruction::BitCast
2182 : Instruction::Trunc;
2183 return CreateCast(CastOp, V, DestTy, Name);
2184 }
2185
2186 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2187 const Twine &Name = "", MDNode *FPMathTag = nullptr,
2188 FMFSource FMFSource = {}) {
2189 if (V->getType() == DestTy)
2190 return V;
2191 if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2192 return Folded;
2193 Instruction *Cast = CastInst::Create(Op, V, DestTy);
2194 if (isa<FPMathOperator>(Cast))
2195 setFPAttrs(Cast, FPMathTag, FMFSource.get(FMF));
2196 return Insert(Cast, Name);
2197 }
2198
2199 Value *CreatePointerCast(Value *V, Type *DestTy,
2200 const Twine &Name = "") {
2201 if (V->getType() == DestTy)
2202 return V;
2203 if (auto *VC = dyn_cast<Constant>(V))
2204 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2205 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2206 }
2207
2208
2209
2210
2211 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2212 const Twine &Name = "") {
2213 if (V->getType() == DestTy)
2214 return V;
2215
2216 if (auto *VC = dyn_cast<Constant>(V)) {
2217 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2218 Name);
2219 }
2220
2221 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2222 Name);
2223 }
2224
2225 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2226 const Twine &Name = "") {
2227 Instruction::CastOps CastOp =
2228 V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2229 ? Instruction::Trunc
2230 : (isSigned ? Instruction::SExt : Instruction::ZExt);
2231 return CreateCast(CastOp, V, DestTy, Name);
2232 }
2233
2234 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2235 const Twine &Name = "") {
2236 if (V->getType() == DestTy)
2237 return V;
2238 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2239 return CreatePtrToInt(V, DestTy, Name);
2240 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2241 return CreateIntToPtr(V, DestTy, Name);
2242
2243 return CreateBitCast(V, DestTy, Name);
2244 }
2245
2246 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "",
2247 MDNode *FPMathTag = nullptr) {
2248 Instruction::CastOps CastOp =
2249 V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2250 ? Instruction::FPTrunc
2251 : Instruction::FPExt;
2252 return CreateCast(CastOp, V, DestTy, Name, FPMathTag);
2253 }
2254
2255 CallInst *CreateConstrainedFPCast(
2256 Intrinsic::ID ID, Value *V, Type *DestTy, FMFSource FMFSource = {},
2257 const Twine &Name = "", MDNode *FPMathTag = nullptr,
2258 std::optional<RoundingMode> Rounding = std::nullopt,
2259 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2260
2261
2262
2263
2264 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2265
2266
2267
2268
2269
2270 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2271 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2272 }
2273
2274 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2275 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2276 }
2277
2278 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2279 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2280 }
2281
2282 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2283 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2284 }
2285
2286 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2287 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2288 }
2289
2290 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2291 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2292 }
2293
2294 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2295 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2296 }
2297
2298 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2299 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2300 }
2301
2302 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2303 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2304 }
2305
2306 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2307 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2308 }
2309
2310 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2311 MDNode *FPMathTag = nullptr) {
2312 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2313 }
2314
2315 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2316 MDNode *FPMathTag = nullptr) {
2317 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2318 }
2319
2320 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2321 MDNode *FPMathTag = nullptr) {
2322 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2323 }
2324
2325 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2326 MDNode *FPMathTag = nullptr) {
2327 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2328 }
2329
2330 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2331 MDNode *FPMathTag = nullptr) {
2332 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2333 }
2334
2335 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2336 MDNode *FPMathTag = nullptr) {
2337 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2338 }
2339
2340 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2341 MDNode *FPMathTag = nullptr) {
2342 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2343 }
2344
2345 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2346 MDNode *FPMathTag = nullptr) {
2347 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2348 }
2349
2350 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2351 MDNode *FPMathTag = nullptr) {
2352 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2353 }
2354
2355 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2356 MDNode *FPMathTag = nullptr) {
2357 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2358 }
2359
2360 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2361 MDNode *FPMathTag = nullptr) {
2362 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2363 }
2364
2365 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2366 MDNode *FPMathTag = nullptr) {
2367 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2368 }
2369
2370 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2371 MDNode *FPMathTag = nullptr) {
2372 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2373 }
2374
2375 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2376 MDNode *FPMathTag = nullptr) {
2377 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2378 }
2379
2380 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2381 const Twine &Name = "") {
2382 if (auto *V = Folder.FoldCmp(P, LHS, RHS))
2383 return V;
2384 return Insert(new ICmpInst(P, LHS, RHS), Name);
2385 }
2386
2387
2388
2389
2390 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2391 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2392 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, false);
2393 }
2394
2395
2396
2397
2398 Value *CreateFCmpFMF(CmpInst::Predicate P, Value *LHS, Value *RHS,
2399 FMFSource FMFSource, const Twine &Name = "",
2400 MDNode *FPMathTag = nullptr) {
2401 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, FMFSource, false);
2402 }
2403
2404 Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2405 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2406 return CmpInst::isFPPredicate(Pred)
2407 ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
2408 : CreateICmp(Pred, LHS, RHS, Name);
2409 }
2410
2411
2412
2413
2414 Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2415 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2416 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, true);
2417 }
2418
2419 private:
2420
2421 Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2422 const Twine &Name, MDNode *FPMathTag,
2423 FMFSource FMFSource, bool IsSignaling);
2424
2425 public:
2426 CallInst *CreateConstrainedFPCmp(
2427 Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2428 const Twine &Name = "",
2429 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2430
2431
2432
2433
2434
2435 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2436 const Twine &Name = "") {
2437 PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2438 if (isa<FPMathOperator>(Phi))
2439 setFPAttrs(Phi, nullptr , FMF);
2440 return Insert(Phi, Name);
2441 }
2442
2443 private:
2444 CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
2445 const Twine &Name = "", FMFSource FMFSource = {},
2446 ArrayRef<OperandBundleDef> OpBundles = {});
2447
2448 public:
2449 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2450 ArrayRef<Value *> Args = {}, const Twine &Name = "",
2451 MDNode *FPMathTag = nullptr) {
2452 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2453 if (IsFPConstrained)
2454 setConstrainedFPCallAttr(CI);
2455 if (isa<FPMathOperator>(CI))
2456 setFPAttrs(CI, FPMathTag, FMF);
2457 return Insert(CI, Name);
2458 }
2459
2460 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2461 ArrayRef<OperandBundleDef> OpBundles,
2462 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2463 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2464 if (IsFPConstrained)
2465 setConstrainedFPCallAttr(CI);
2466 if (isa<FPMathOperator>(CI))
2467 setFPAttrs(CI, FPMathTag, FMF);
2468 return Insert(CI, Name);
2469 }
2470
2471 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = {},
2472 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2473 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2474 FPMathTag);
2475 }
2476
2477 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2478 ArrayRef<OperandBundleDef> OpBundles,
2479 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2480 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2481 OpBundles, Name, FPMathTag);
2482 }
2483
2484 CallInst *CreateConstrainedFPCall(
2485 Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2486 std::optional<RoundingMode> Rounding = std::nullopt,
2487 std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2488
2489 Value *CreateSelect(Value *C, Value *True, Value *False,
2490 const Twine &Name = "", Instruction *MDFrom = nullptr);
2491 Value *CreateSelectFMF(Value *C, Value *True, Value *False,
2492 FMFSource FMFSource, const Twine &Name = "",
2493 Instruction *MDFrom = nullptr);
2494
2495 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2496 return Insert(new VAArgInst(List, Ty), Name);
2497 }
2498
2499 Value *CreateExtractElement(Value *Vec, Value *Idx,
2500 const Twine &Name = "") {
2501 if (Value *V = Folder.FoldExtractElement(Vec, Idx))
2502 return V;
2503 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2504 }
2505
2506 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2507 const Twine &Name = "") {
2508 return CreateExtractElement(Vec, getInt64(Idx), Name);
2509 }
2510
2511 Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2512 const Twine &Name = "") {
2513 return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2514 }
2515
2516 Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2517 const Twine &Name = "") {
2518 return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2519 }
2520
2521 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2522 const Twine &Name = "") {
2523 if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx))
2524 return V;
2525 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2526 }
2527
2528 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2529 const Twine &Name = "") {
2530 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2531 }
2532
2533 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2534 const Twine &Name = "") {
2535 SmallVector<int, 16> IntMask;
2536 ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
2537 return CreateShuffleVector(V1, V2, IntMask, Name);
2538 }
2539
2540
2541 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2542 const Twine &Name = "") {
2543 if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask))
2544 return V;
2545 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2546 }
2547
2548
2549
2550 Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2551 const Twine &Name = "") {
2552 return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name);
2553 }
2554
2555 Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs,
2556 const Twine &Name = "") {
2557 if (auto *V = Folder.FoldExtractValue(Agg, Idxs))
2558 return V;
2559 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2560 }
2561
2562 Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2563 const Twine &Name = "") {
2564 if (auto *V = Folder.FoldInsertValue(Agg, Val, Idxs))
2565 return V;
2566 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2567 }
2568
2569 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2570 const Twine &Name = "") {
2571 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2572 }
2573
2574 Value *CreateFreeze(Value *V, const Twine &Name = "") {
2575 return Insert(new FreezeInst(V), Name);
2576 }
2577
2578
2579
2580
2581
2582
2583 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2584 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), Name);
2585 }
2586
2587
2588 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2589 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), Name);
2590 }
2591
2592
2593 Value *CreateIsNeg(Value *Arg, const Twine &Name = "") {
2594 return CreateICmpSLT(Arg, ConstantInt::getNullValue(Arg->getType()), Name);
2595 }
2596
2597
2598 Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") {
2599 return CreateICmpSGT(Arg, ConstantInt::getAllOnesValue(Arg->getType()),
2600 Name);
2601 }
2602
2603
2604
2605
2606
2607
2608
2609 Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS,
2610 const Twine &Name = "");
2611
2612
2613
2614
2615 Value *CreateLaunderInvariantGroup(Value *Ptr);
2616
2617
2618
2619
2620 Value *CreateStripInvariantGroup(Value *Ptr);
2621
2622
2623 Value *CreateVectorReverse(Value *V, const Twine &Name = "");
2624
2625
2626
2627
2628
2629
2630
2631
2632 Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
2633 const Twine &Name = "");
2634
2635
2636
2637 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2638
2639
2640
2641 Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2642
2643 Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2644 unsigned Dimension, unsigned LastIndex,
2645 MDNode *DbgInfo);
2646
2647 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2648 MDNode *DbgInfo);
2649
2650 Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2651 unsigned Index, unsigned FieldIndex,
2652 MDNode *DbgInfo);
2653
2654 Value *createIsFPClass(Value *FPNum, unsigned Test);
2655
2656 private:
2657
2658
2659
2660 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2661 Value *PtrValue, Value *AlignValue,
2662 Value *OffsetValue);
2663
2664 public:
2665
2666
2667
2668
2669
2670
2671 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2672 unsigned Alignment,
2673 Value *OffsetValue = nullptr);
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2685 Value *Alignment,
2686 Value *OffsetValue = nullptr);
2687 };
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703 template <typename FolderTy = ConstantFolder,
2704 typename InserterTy = IRBuilderDefaultInserter>
2705 class IRBuilder : public IRBuilderBase {
2706 private:
2707 FolderTy Folder;
2708 InserterTy Inserter;
2709
2710 public:
2711 IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2712 MDNode *FPMathTag = nullptr,
2713 ArrayRef<OperandBundleDef> OpBundles = {})
2714 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2715 Folder(Folder), Inserter(Inserter) {}
2716
2717 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2718 ArrayRef<OperandBundleDef> OpBundles = {})
2719 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2720
2721 explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2722 MDNode *FPMathTag = nullptr,
2723 ArrayRef<OperandBundleDef> OpBundles = {})
2724 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2725 FPMathTag, OpBundles),
2726 Folder(Folder) {
2727 SetInsertPoint(TheBB);
2728 }
2729
2730 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2731 ArrayRef<OperandBundleDef> OpBundles = {})
2732 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2733 FPMathTag, OpBundles) {
2734 SetInsertPoint(TheBB);
2735 }
2736
2737 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2738 ArrayRef<OperandBundleDef> OpBundles = {})
2739 : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2740 OpBundles) {
2741 SetInsertPoint(IP);
2742 }
2743
2744 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2745 MDNode *FPMathTag = nullptr,
2746 ArrayRef<OperandBundleDef> OpBundles = {})
2747 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2748 FPMathTag, OpBundles),
2749 Folder(Folder) {
2750 SetInsertPoint(TheBB, IP);
2751 }
2752
2753 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2754 MDNode *FPMathTag = nullptr,
2755 ArrayRef<OperandBundleDef> OpBundles = {})
2756 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2757 FPMathTag, OpBundles) {
2758 SetInsertPoint(TheBB, IP);
2759 }
2760
2761
2762
2763 IRBuilder(const IRBuilder &) = delete;
2764
2765 InserterTy &getInserter() { return Inserter; }
2766 const InserterTy &getInserter() const { return Inserter; }
2767 };
2768
2769 template <typename FolderTy, typename InserterTy>
2770 IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *,
2771 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>;
2772 IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2773 template <typename FolderTy>
2774 IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>)
2775 -> IRBuilder<FolderTy>;
2776 IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2777 IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2778 template <typename FolderTy>
2779 IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *,
2780 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>;
2781 IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *,
2782 ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2783
2784
2785
2786 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2787
2788 }
2789
2790 #endif