1 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the IRBuilder class, which is used as a convenient way 10 // to create LLVM instructions with a consistent and simplified interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_IR_IRBUILDER_H 15 #define LLVM_IR_IRBUILDER_H 16 17 #include "llvm-c/Types.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/IR/BasicBlock.h" 23 #include "llvm/IR/Constant.h" 24 #include "llvm/IR/ConstantFolder.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/DebugLoc.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/FPEnv.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/GlobalVariable.h" 32 #include "llvm/IR/InstrTypes.h" 33 #include "llvm/IR/Instruction.h" 34 #include "llvm/IR/Instructions.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/LLVMContext.h" 37 #include "llvm/IR/Module.h" 38 #include "llvm/IR/Operator.h" 39 #include "llvm/IR/Type.h" 40 #include "llvm/IR/Value.h" 41 #include "llvm/IR/ValueHandle.h" 42 #include "llvm/Support/AtomicOrdering.h" 43 #include "llvm/Support/CBindingWrapping.h" 44 #include "llvm/Support/Casting.h" 45 #include <cassert> 46 #include <cstdint> 47 #include <functional> 48 #include <optional> 49 #include <utility> 50 51 namespace llvm { 52 53 class APInt; 54 class Use; 55 56 /// This provides the default implementation of the IRBuilder 57 /// 'InsertHelper' method that is called whenever an instruction is created by 58 /// IRBuilder and needs to be inserted. 59 /// 60 /// By default, this inserts the instruction at the insertion point. 61 class IRBuilderDefaultInserter { 62 public: 63 virtual ~IRBuilderDefaultInserter(); 64 InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)65 virtual void InsertHelper(Instruction *I, const Twine &Name, 66 BasicBlock *BB, 67 BasicBlock::iterator InsertPt) const { 68 if (BB) 69 I->insertInto(BB, InsertPt); 70 I->setName(Name); 71 } 72 }; 73 74 /// Provides an 'InsertHelper' that calls a user-provided callback after 75 /// performing the default insertion. 76 class IRBuilderCallbackInserter : public IRBuilderDefaultInserter { 77 std::function<void(Instruction *)> Callback; 78 79 public: 80 ~IRBuilderCallbackInserter() override; 81 IRBuilderCallbackInserter(std::function<void (Instruction *)> Callback)82 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback) 83 : Callback(std::move(Callback)) {} 84 InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)85 void InsertHelper(Instruction *I, const Twine &Name, 86 BasicBlock *BB, 87 BasicBlock::iterator InsertPt) const override { 88 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); 89 Callback(I); 90 } 91 }; 92 93 /// Common base class shared among various IRBuilders. 94 class IRBuilderBase { 95 /// Pairs of (metadata kind, MDNode *) that should be added to all newly 96 /// created instructions, like !dbg metadata. 97 SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy; 98 99 /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not 100 /// null. If \p MD is null, remove the entry with \p Kind. AddOrRemoveMetadataToCopy(unsigned Kind,MDNode * MD)101 void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) { 102 if (!MD) { 103 erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) { 104 return KV.first == Kind; 105 }); 106 return; 107 } 108 109 for (auto &KV : MetadataToCopy) 110 if (KV.first == Kind) { 111 KV.second = MD; 112 return; 113 } 114 115 MetadataToCopy.emplace_back(Kind, MD); 116 } 117 118 protected: 119 BasicBlock *BB; 120 BasicBlock::iterator InsertPt; 121 LLVMContext &Context; 122 const IRBuilderFolder &Folder; 123 const IRBuilderDefaultInserter &Inserter; 124 125 MDNode *DefaultFPMathTag; 126 FastMathFlags FMF; 127 128 bool IsFPConstrained = false; 129 fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict; 130 RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic; 131 132 ArrayRef<OperandBundleDef> DefaultOperandBundles; 133 134 public: IRBuilderBase(LLVMContext & context,const IRBuilderFolder & Folder,const IRBuilderDefaultInserter & Inserter,MDNode * FPMathTag,ArrayRef<OperandBundleDef> OpBundles)135 IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder, 136 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag, 137 ArrayRef<OperandBundleDef> OpBundles) 138 : Context(context), Folder(Folder), Inserter(Inserter), 139 DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) { 140 ClearInsertionPoint(); 141 } 142 143 /// Insert and return the specified instruction. 144 template<typename InstTy> 145 InstTy *Insert(InstTy *I, const Twine &Name = "") const { 146 Inserter.InsertHelper(I, Name, BB, InsertPt); 147 AddMetadataToInst(I); 148 return I; 149 } 150 151 /// No-op overload to handle constants. 152 Constant *Insert(Constant *C, const Twine& = "") const { 153 return C; 154 } 155 156 Value *Insert(Value *V, const Twine &Name = "") const { 157 if (Instruction *I = dyn_cast<Instruction>(V)) 158 return Insert(I, Name); 159 assert(isa<Constant>(V)); 160 return V; 161 } 162 163 //===--------------------------------------------------------------------===// 164 // Builder configuration methods 165 //===--------------------------------------------------------------------===// 166 167 /// Clear the insertion point: created instructions will not be 168 /// inserted into a block. ClearInsertionPoint()169 void ClearInsertionPoint() { 170 BB = nullptr; 171 InsertPt = BasicBlock::iterator(); 172 } 173 GetInsertBlock()174 BasicBlock *GetInsertBlock() const { return BB; } GetInsertPoint()175 BasicBlock::iterator GetInsertPoint() const { return InsertPt; } getContext()176 LLVMContext &getContext() const { return Context; } 177 178 /// This specifies that created instructions should be appended to the 179 /// end of the specified block. SetInsertPoint(BasicBlock * TheBB)180 void SetInsertPoint(BasicBlock *TheBB) { 181 BB = TheBB; 182 InsertPt = BB->end(); 183 } 184 185 /// This specifies that created instructions should be inserted before 186 /// the specified instruction. SetInsertPoint(Instruction * I)187 void SetInsertPoint(Instruction *I) { 188 BB = I->getParent(); 189 InsertPt = I->getIterator(); 190 assert(InsertPt != BB->end() && "Can't read debug loc from end()"); 191 SetCurrentDebugLocation(I->getDebugLoc()); 192 } 193 194 /// This specifies that created instructions should be inserted at the 195 /// specified point. SetInsertPoint(BasicBlock * TheBB,BasicBlock::iterator IP)196 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { 197 BB = TheBB; 198 InsertPt = IP; 199 if (IP != TheBB->end()) 200 SetCurrentDebugLocation(IP->getDebugLoc()); 201 } 202 203 /// This specifies that created instructions should inserted at the beginning 204 /// end of the specified function, but after already existing static alloca 205 /// instructions that are at the start. SetInsertPointPastAllocas(Function * F)206 void SetInsertPointPastAllocas(Function *F) { 207 BB = &F->getEntryBlock(); 208 InsertPt = BB->getFirstNonPHIOrDbgOrAlloca(); 209 } 210 211 /// Set location information used by debugging information. SetCurrentDebugLocation(DebugLoc L)212 void SetCurrentDebugLocation(DebugLoc L) { 213 AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode()); 214 } 215 216 /// Collect metadata with IDs \p MetadataKinds from \p Src which should be 217 /// added to all created instructions. Entries present in MedataDataToCopy but 218 /// not on \p Src will be dropped from MetadataToCopy. CollectMetadataToCopy(Instruction * Src,ArrayRef<unsigned> MetadataKinds)219 void CollectMetadataToCopy(Instruction *Src, 220 ArrayRef<unsigned> MetadataKinds) { 221 for (unsigned K : MetadataKinds) 222 AddOrRemoveMetadataToCopy(K, Src->getMetadata(K)); 223 } 224 225 /// Get location information used by debugging information. 226 DebugLoc getCurrentDebugLocation() const; 227 228 /// If this builder has a current debug location, set it on the 229 /// specified instruction. 230 void SetInstDebugLocation(Instruction *I) const; 231 232 /// Add all entries in MetadataToCopy to \p I. AddMetadataToInst(Instruction * I)233 void AddMetadataToInst(Instruction *I) const { 234 for (const auto &KV : MetadataToCopy) 235 I->setMetadata(KV.first, KV.second); 236 } 237 238 /// Get the return type of the current function that we're emitting 239 /// into. 240 Type *getCurrentFunctionReturnType() const; 241 242 /// InsertPoint - A saved insertion point. 243 class InsertPoint { 244 BasicBlock *Block = nullptr; 245 BasicBlock::iterator Point; 246 247 public: 248 /// Creates a new insertion point which doesn't point to anything. 249 InsertPoint() = default; 250 251 /// Creates a new insertion point at the given location. InsertPoint(BasicBlock * InsertBlock,BasicBlock::iterator InsertPoint)252 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) 253 : Block(InsertBlock), Point(InsertPoint) {} 254 255 /// Returns true if this insert point is set. isSet()256 bool isSet() const { return (Block != nullptr); } 257 getBlock()258 BasicBlock *getBlock() const { return Block; } getPoint()259 BasicBlock::iterator getPoint() const { return Point; } 260 }; 261 262 /// Returns the current insert point. saveIP()263 InsertPoint saveIP() const { 264 return InsertPoint(GetInsertBlock(), GetInsertPoint()); 265 } 266 267 /// Returns the current insert point, clearing it in the process. saveAndClearIP()268 InsertPoint saveAndClearIP() { 269 InsertPoint IP(GetInsertBlock(), GetInsertPoint()); 270 ClearInsertionPoint(); 271 return IP; 272 } 273 274 /// Sets the current insert point to a previously-saved location. restoreIP(InsertPoint IP)275 void restoreIP(InsertPoint IP) { 276 if (IP.isSet()) 277 SetInsertPoint(IP.getBlock(), IP.getPoint()); 278 else 279 ClearInsertionPoint(); 280 } 281 282 /// Get the floating point math metadata being used. getDefaultFPMathTag()283 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } 284 285 /// Get the flags to be applied to created floating point ops getFastMathFlags()286 FastMathFlags getFastMathFlags() const { return FMF; } 287 getFastMathFlags()288 FastMathFlags &getFastMathFlags() { return FMF; } 289 290 /// Clear the fast-math flags. clearFastMathFlags()291 void clearFastMathFlags() { FMF.clear(); } 292 293 /// Set the floating point math metadata to be used. setDefaultFPMathTag(MDNode * FPMathTag)294 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } 295 296 /// Set the fast-math flags to be used with generated fp-math operators setFastMathFlags(FastMathFlags NewFMF)297 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } 298 299 /// Enable/Disable use of constrained floating point math. When 300 /// enabled the CreateF<op>() calls instead create constrained 301 /// floating point intrinsic calls. Fast math flags are unaffected 302 /// by this setting. setIsFPConstrained(bool IsCon)303 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; } 304 305 /// Query for the use of constrained floating point math getIsFPConstrained()306 bool getIsFPConstrained() { return IsFPConstrained; } 307 308 /// Set the exception handling to be used with constrained floating point setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept)309 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) { 310 #ifndef NDEBUG 311 std::optional<StringRef> ExceptStr = 312 convertExceptionBehaviorToStr(NewExcept); 313 assert(ExceptStr && "Garbage strict exception behavior!"); 314 #endif 315 DefaultConstrainedExcept = NewExcept; 316 } 317 318 /// Set the rounding mode handling to be used with constrained floating point setDefaultConstrainedRounding(RoundingMode NewRounding)319 void setDefaultConstrainedRounding(RoundingMode NewRounding) { 320 #ifndef NDEBUG 321 std::optional<StringRef> RoundingStr = 322 convertRoundingModeToStr(NewRounding); 323 assert(RoundingStr && "Garbage strict rounding mode!"); 324 #endif 325 DefaultConstrainedRounding = NewRounding; 326 } 327 328 /// Get the exception handling used with constrained floating point getDefaultConstrainedExcept()329 fp::ExceptionBehavior getDefaultConstrainedExcept() { 330 return DefaultConstrainedExcept; 331 } 332 333 /// Get the rounding mode handling used with constrained floating point getDefaultConstrainedRounding()334 RoundingMode getDefaultConstrainedRounding() { 335 return DefaultConstrainedRounding; 336 } 337 setConstrainedFPFunctionAttr()338 void setConstrainedFPFunctionAttr() { 339 assert(BB && "Must have a basic block to set any function attributes!"); 340 341 Function *F = BB->getParent(); 342 if (!F->hasFnAttribute(Attribute::StrictFP)) { 343 F->addFnAttr(Attribute::StrictFP); 344 } 345 } 346 setConstrainedFPCallAttr(CallBase * I)347 void setConstrainedFPCallAttr(CallBase *I) { 348 I->addFnAttr(Attribute::StrictFP); 349 } 350 setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles)351 void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) { 352 DefaultOperandBundles = OpBundles; 353 } 354 355 //===--------------------------------------------------------------------===// 356 // RAII helpers. 357 //===--------------------------------------------------------------------===// 358 359 // RAII object that stores the current insertion point and restores it 360 // when the object is destroyed. This includes the debug location. 361 class InsertPointGuard { 362 IRBuilderBase &Builder; 363 AssertingVH<BasicBlock> Block; 364 BasicBlock::iterator Point; 365 DebugLoc DbgLoc; 366 367 public: InsertPointGuard(IRBuilderBase & B)368 InsertPointGuard(IRBuilderBase &B) 369 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), 370 DbgLoc(B.getCurrentDebugLocation()) {} 371 372 InsertPointGuard(const InsertPointGuard &) = delete; 373 InsertPointGuard &operator=(const InsertPointGuard &) = delete; 374 ~InsertPointGuard()375 ~InsertPointGuard() { 376 Builder.restoreIP(InsertPoint(Block, Point)); 377 Builder.SetCurrentDebugLocation(DbgLoc); 378 } 379 }; 380 381 // RAII object that stores the current fast math settings and restores 382 // them when the object is destroyed. 383 class FastMathFlagGuard { 384 IRBuilderBase &Builder; 385 FastMathFlags FMF; 386 MDNode *FPMathTag; 387 bool IsFPConstrained; 388 fp::ExceptionBehavior DefaultConstrainedExcept; 389 RoundingMode DefaultConstrainedRounding; 390 391 public: FastMathFlagGuard(IRBuilderBase & B)392 FastMathFlagGuard(IRBuilderBase &B) 393 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag), 394 IsFPConstrained(B.IsFPConstrained), 395 DefaultConstrainedExcept(B.DefaultConstrainedExcept), 396 DefaultConstrainedRounding(B.DefaultConstrainedRounding) {} 397 398 FastMathFlagGuard(const FastMathFlagGuard &) = delete; 399 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete; 400 ~FastMathFlagGuard()401 ~FastMathFlagGuard() { 402 Builder.FMF = FMF; 403 Builder.DefaultFPMathTag = FPMathTag; 404 Builder.IsFPConstrained = IsFPConstrained; 405 Builder.DefaultConstrainedExcept = DefaultConstrainedExcept; 406 Builder.DefaultConstrainedRounding = DefaultConstrainedRounding; 407 } 408 }; 409 410 // RAII object that stores the current default operand bundles and restores 411 // them when the object is destroyed. 412 class OperandBundlesGuard { 413 IRBuilderBase &Builder; 414 ArrayRef<OperandBundleDef> DefaultOperandBundles; 415 416 public: OperandBundlesGuard(IRBuilderBase & B)417 OperandBundlesGuard(IRBuilderBase &B) 418 : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {} 419 420 OperandBundlesGuard(const OperandBundlesGuard &) = delete; 421 OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete; 422 ~OperandBundlesGuard()423 ~OperandBundlesGuard() { 424 Builder.DefaultOperandBundles = DefaultOperandBundles; 425 } 426 }; 427 428 429 //===--------------------------------------------------------------------===// 430 // Miscellaneous creation methods. 431 //===--------------------------------------------------------------------===// 432 433 /// Make a new global variable with initializer type i8* 434 /// 435 /// Make a new global variable with an initializer that has array of i8 type 436 /// filled in with the null terminated string value specified. The new global 437 /// variable will be marked mergable with any others of the same contents. If 438 /// Name is specified, it is the name of the global variable created. 439 /// 440 /// If no module is given via \p M, it is take from the insertion point basic 441 /// block. 442 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", 443 unsigned AddressSpace = 0, 444 Module *M = nullptr); 445 446 /// Get a constant value representing either true or false. getInt1(bool V)447 ConstantInt *getInt1(bool V) { 448 return ConstantInt::get(getInt1Ty(), V); 449 } 450 451 /// Get the constant value for i1 true. getTrue()452 ConstantInt *getTrue() { 453 return ConstantInt::getTrue(Context); 454 } 455 456 /// Get the constant value for i1 false. getFalse()457 ConstantInt *getFalse() { 458 return ConstantInt::getFalse(Context); 459 } 460 461 /// Get a constant 8-bit value. getInt8(uint8_t C)462 ConstantInt *getInt8(uint8_t C) { 463 return ConstantInt::get(getInt8Ty(), C); 464 } 465 466 /// Get a constant 16-bit value. getInt16(uint16_t C)467 ConstantInt *getInt16(uint16_t C) { 468 return ConstantInt::get(getInt16Ty(), C); 469 } 470 471 /// Get a constant 32-bit value. getInt32(uint32_t C)472 ConstantInt *getInt32(uint32_t C) { 473 return ConstantInt::get(getInt32Ty(), C); 474 } 475 476 /// Get a constant 64-bit value. getInt64(uint64_t C)477 ConstantInt *getInt64(uint64_t C) { 478 return ConstantInt::get(getInt64Ty(), C); 479 } 480 481 /// Get a constant N-bit value, zero extended or truncated from 482 /// a 64-bit value. getIntN(unsigned N,uint64_t C)483 ConstantInt *getIntN(unsigned N, uint64_t C) { 484 return ConstantInt::get(getIntNTy(N), C); 485 } 486 487 /// Get a constant integer value. getInt(const APInt & AI)488 ConstantInt *getInt(const APInt &AI) { 489 return ConstantInt::get(Context, AI); 490 } 491 492 //===--------------------------------------------------------------------===// 493 // Type creation methods 494 //===--------------------------------------------------------------------===// 495 496 /// Fetch the type representing a single bit getInt1Ty()497 IntegerType *getInt1Ty() { 498 return Type::getInt1Ty(Context); 499 } 500 501 /// Fetch the type representing an 8-bit integer. getInt8Ty()502 IntegerType *getInt8Ty() { 503 return Type::getInt8Ty(Context); 504 } 505 506 /// Fetch the type representing a 16-bit integer. getInt16Ty()507 IntegerType *getInt16Ty() { 508 return Type::getInt16Ty(Context); 509 } 510 511 /// Fetch the type representing a 32-bit integer. getInt32Ty()512 IntegerType *getInt32Ty() { 513 return Type::getInt32Ty(Context); 514 } 515 516 /// Fetch the type representing a 64-bit integer. getInt64Ty()517 IntegerType *getInt64Ty() { 518 return Type::getInt64Ty(Context); 519 } 520 521 /// Fetch the type representing a 128-bit integer. getInt128Ty()522 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); } 523 524 /// Fetch the type representing an N-bit integer. getIntNTy(unsigned N)525 IntegerType *getIntNTy(unsigned N) { 526 return Type::getIntNTy(Context, N); 527 } 528 529 /// Fetch the type representing a 16-bit floating point value. getHalfTy()530 Type *getHalfTy() { 531 return Type::getHalfTy(Context); 532 } 533 534 /// Fetch the type representing a 16-bit brain floating point value. getBFloatTy()535 Type *getBFloatTy() { 536 return Type::getBFloatTy(Context); 537 } 538 539 /// Fetch the type representing a 32-bit floating point value. getFloatTy()540 Type *getFloatTy() { 541 return Type::getFloatTy(Context); 542 } 543 544 /// Fetch the type representing a 64-bit floating point value. getDoubleTy()545 Type *getDoubleTy() { 546 return Type::getDoubleTy(Context); 547 } 548 549 /// Fetch the type representing void. getVoidTy()550 Type *getVoidTy() { 551 return Type::getVoidTy(Context); 552 } 553 554 /// Fetch the type representing a pointer. 555 PointerType *getPtrTy(unsigned AddrSpace = 0) { 556 return PointerType::get(Context, AddrSpace); 557 } 558 559 /// Fetch the type representing a pointer to an 8-bit integer value. 560 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { 561 return Type::getInt8PtrTy(Context, AddrSpace); 562 } 563 564 /// Fetch the type of an integer with size at least as big as that of a 565 /// pointer in the given address space. 566 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { 567 return DL.getIntPtrType(Context, AddrSpace); 568 } 569 570 //===--------------------------------------------------------------------===// 571 // Intrinsic creation methods 572 //===--------------------------------------------------------------------===// 573 574 /// Create and insert a memset to the specified pointer and the 575 /// specified value. 576 /// 577 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is 578 /// specified, it will be added to the instruction. Likewise with alias.scope 579 /// and noalias tags. 580 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, 581 MaybeAlign Align, bool isVolatile = false, 582 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, 583 MDNode *NoAliasTag = nullptr) { 584 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, 585 TBAATag, ScopeTag, NoAliasTag); 586 } 587 588 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align, 589 bool isVolatile = false, MDNode *TBAATag = nullptr, 590 MDNode *ScopeTag = nullptr, 591 MDNode *NoAliasTag = nullptr); 592 593 CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val, 594 Value *Size, bool IsVolatile = false, 595 MDNode *TBAATag = nullptr, 596 MDNode *ScopeTag = nullptr, 597 MDNode *NoAliasTag = nullptr); 598 599 /// Create and insert an element unordered-atomic memset of the region of 600 /// memory starting at the given pointer to the given value. 601 /// 602 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is 603 /// specified, it will be added to the instruction. Likewise with alias.scope 604 /// and noalias tags. 605 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, 606 uint64_t Size, Align Alignment, 607 uint32_t ElementSize, 608 MDNode *TBAATag = nullptr, 609 MDNode *ScopeTag = nullptr, 610 MDNode *NoAliasTag = nullptr) { 611 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), 612 Align(Alignment), ElementSize, 613 TBAATag, ScopeTag, NoAliasTag); 614 } 615 616 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, 617 Value *Size, Align Alignment, 618 uint32_t ElementSize, 619 MDNode *TBAATag = nullptr, 620 MDNode *ScopeTag = nullptr, 621 MDNode *NoAliasTag = nullptr); 622 623 /// Create and insert a memcpy between the specified pointers. 624 /// 625 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 626 /// specified, it will be added to the instruction. Likewise with alias.scope 627 /// and noalias tags. 628 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, 629 MaybeAlign SrcAlign, uint64_t Size, 630 bool isVolatile = false, MDNode *TBAATag = nullptr, 631 MDNode *TBAAStructTag = nullptr, 632 MDNode *ScopeTag = nullptr, 633 MDNode *NoAliasTag = nullptr) { 634 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size), 635 isVolatile, TBAATag, TBAAStructTag, ScopeTag, 636 NoAliasTag); 637 } 638 639 CallInst *CreateMemTransferInst( 640 Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src, 641 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false, 642 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, 643 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr); 644 645 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, 646 MaybeAlign SrcAlign, Value *Size, 647 bool isVolatile = false, MDNode *TBAATag = nullptr, 648 MDNode *TBAAStructTag = nullptr, 649 MDNode *ScopeTag = nullptr, 650 MDNode *NoAliasTag = nullptr) { 651 return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src, 652 SrcAlign, Size, isVolatile, TBAATag, 653 TBAAStructTag, ScopeTag, NoAliasTag); 654 } 655 656 CallInst * 657 CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src, 658 MaybeAlign SrcAlign, Value *Size, bool IsVolatile = false, 659 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, 660 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr); 661 662 /// Create and insert an element unordered-atomic memcpy between the 663 /// specified pointers. 664 /// 665 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively. 666 /// 667 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 668 /// specified, it will be added to the instruction. Likewise with alias.scope 669 /// and noalias tags. 670 CallInst *CreateElementUnorderedAtomicMemCpy( 671 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, 672 uint32_t ElementSize, MDNode *TBAATag = nullptr, 673 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, 674 MDNode *NoAliasTag = nullptr); 675 676 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, 677 MaybeAlign SrcAlign, uint64_t Size, 678 bool isVolatile = false, MDNode *TBAATag = nullptr, 679 MDNode *ScopeTag = nullptr, 680 MDNode *NoAliasTag = nullptr) { 681 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), 682 isVolatile, TBAATag, ScopeTag, NoAliasTag); 683 } 684 685 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, 686 MaybeAlign SrcAlign, Value *Size, 687 bool isVolatile = false, MDNode *TBAATag = nullptr, 688 MDNode *ScopeTag = nullptr, 689 MDNode *NoAliasTag = nullptr); 690 691 /// \brief Create and insert an element unordered-atomic memmove between the 692 /// specified pointers. 693 /// 694 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, 695 /// respectively. 696 /// 697 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 698 /// specified, it will be added to the instruction. Likewise with alias.scope 699 /// and noalias tags. 700 CallInst *CreateElementUnorderedAtomicMemMove( 701 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, 702 uint32_t ElementSize, MDNode *TBAATag = nullptr, 703 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, 704 MDNode *NoAliasTag = nullptr); 705 706 private: 707 CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src); 708 709 public: 710 /// Create a sequential vector fadd reduction intrinsic of the source vector. 711 /// The first parameter is a scalar accumulator value. An unordered reduction 712 /// can be created by adding the reassoc fast-math flag to the resulting 713 /// sequential reduction. 714 CallInst *CreateFAddReduce(Value *Acc, Value *Src); 715 716 /// Create a sequential vector fmul reduction intrinsic of the source vector. 717 /// The first parameter is a scalar accumulator value. An unordered reduction 718 /// can be created by adding the reassoc fast-math flag to the resulting 719 /// sequential reduction. 720 CallInst *CreateFMulReduce(Value *Acc, Value *Src); 721 722 /// Create a vector int add reduction intrinsic of the source vector. 723 CallInst *CreateAddReduce(Value *Src); 724 725 /// Create a vector int mul reduction intrinsic of the source vector. 726 CallInst *CreateMulReduce(Value *Src); 727 728 /// Create a vector int AND reduction intrinsic of the source vector. 729 CallInst *CreateAndReduce(Value *Src); 730 731 /// Create a vector int OR reduction intrinsic of the source vector. 732 CallInst *CreateOrReduce(Value *Src); 733 734 /// Create a vector int XOR reduction intrinsic of the source vector. 735 CallInst *CreateXorReduce(Value *Src); 736 737 /// Create a vector integer max reduction intrinsic of the source 738 /// vector. 739 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false); 740 741 /// Create a vector integer min reduction intrinsic of the source 742 /// vector. 743 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false); 744 745 /// Create a vector float max reduction intrinsic of the source 746 /// vector. 747 CallInst *CreateFPMaxReduce(Value *Src); 748 749 /// Create a vector float min reduction intrinsic of the source 750 /// vector. 751 CallInst *CreateFPMinReduce(Value *Src); 752 753 /// Create a lifetime.start intrinsic. 754 /// 755 /// If the pointer isn't i8* it will be converted. 756 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr); 757 758 /// Create a lifetime.end intrinsic. 759 /// 760 /// If the pointer isn't i8* it will be converted. 761 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr); 762 763 /// Create a call to invariant.start intrinsic. 764 /// 765 /// If the pointer isn't i8* it will be converted. 766 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr); 767 768 /// Create a call to llvm.threadlocal.address intrinsic. 769 CallInst *CreateThreadLocalAddress(Value *Ptr); 770 771 /// Create a call to Masked Load intrinsic 772 CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask, 773 Value *PassThru = nullptr, const Twine &Name = ""); 774 775 /// Create a call to Masked Store intrinsic 776 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, 777 Value *Mask); 778 779 /// Create a call to Masked Gather intrinsic 780 CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment, 781 Value *Mask = nullptr, Value *PassThru = nullptr, 782 const Twine &Name = ""); 783 784 /// Create a call to Masked Scatter intrinsic 785 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, 786 Value *Mask = nullptr); 787 788 /// Create a call to Masked Expand Load intrinsic 789 CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, Value *Mask = nullptr, 790 Value *PassThru = nullptr, 791 const Twine &Name = ""); 792 793 /// Create a call to Masked Compress Store intrinsic 794 CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr, 795 Value *Mask = nullptr); 796 797 /// Create an assume intrinsic call that allows the optimizer to 798 /// assume that the provided condition will be true. 799 /// 800 /// The optional argument \p OpBundles specifies operand bundles that are 801 /// added to the call instruction. 802 CallInst * 803 CreateAssumption(Value *Cond, 804 ArrayRef<OperandBundleDef> OpBundles = std::nullopt); 805 806 /// Create a llvm.experimental.noalias.scope.decl intrinsic call. 807 Instruction *CreateNoAliasScopeDeclaration(Value *Scope); CreateNoAliasScopeDeclaration(MDNode * ScopeTag)808 Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) { 809 return CreateNoAliasScopeDeclaration( 810 MetadataAsValue::get(Context, ScopeTag)); 811 } 812 813 /// Create a call to the experimental.gc.statepoint intrinsic to 814 /// start a new statepoint sequence. 815 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 816 FunctionCallee ActualCallee, 817 ArrayRef<Value *> CallArgs, 818 std::optional<ArrayRef<Value *>> DeoptArgs, 819 ArrayRef<Value *> GCArgs, 820 const Twine &Name = ""); 821 822 /// Create a call to the experimental.gc.statepoint intrinsic to 823 /// start a new statepoint sequence. 824 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 825 FunctionCallee ActualCallee, uint32_t Flags, 826 ArrayRef<Value *> CallArgs, 827 std::optional<ArrayRef<Use>> TransitionArgs, 828 std::optional<ArrayRef<Use>> DeoptArgs, 829 ArrayRef<Value *> GCArgs, 830 const Twine &Name = ""); 831 832 /// Conveninence function for the common case when CallArgs are filled 833 /// in using ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be 834 /// .get()'ed to get the Value pointer. 835 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 836 FunctionCallee ActualCallee, 837 ArrayRef<Use> CallArgs, 838 std::optional<ArrayRef<Value *>> DeoptArgs, 839 ArrayRef<Value *> GCArgs, 840 const Twine &Name = ""); 841 842 /// Create an invoke to the experimental.gc.statepoint intrinsic to 843 /// start a new statepoint sequence. 844 InvokeInst * 845 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, 846 FunctionCallee ActualInvokee, BasicBlock *NormalDest, 847 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs, 848 std::optional<ArrayRef<Value *>> DeoptArgs, 849 ArrayRef<Value *> GCArgs, const Twine &Name = ""); 850 851 /// Create an invoke to the experimental.gc.statepoint intrinsic to 852 /// start a new statepoint sequence. 853 InvokeInst *CreateGCStatepointInvoke( 854 uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee, 855 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, 856 ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs, 857 std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs, 858 const Twine &Name = ""); 859 860 // Convenience function for the common case when CallArgs are filled in using 861 // ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to 862 // get the Value *. 863 InvokeInst * 864 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, 865 FunctionCallee ActualInvokee, BasicBlock *NormalDest, 866 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, 867 std::optional<ArrayRef<Value *>> DeoptArgs, 868 ArrayRef<Value *> GCArgs, const Twine &Name = ""); 869 870 /// Create a call to the experimental.gc.result intrinsic to extract 871 /// the result from a call wrapped in a statepoint. 872 CallInst *CreateGCResult(Instruction *Statepoint, 873 Type *ResultType, 874 const Twine &Name = ""); 875 876 /// Create a call to the experimental.gc.relocate intrinsics to 877 /// project the relocated value of one pointer from the statepoint. 878 CallInst *CreateGCRelocate(Instruction *Statepoint, 879 int BaseOffset, 880 int DerivedOffset, 881 Type *ResultType, 882 const Twine &Name = ""); 883 884 /// Create a call to the experimental.gc.pointer.base intrinsic to get the 885 /// base pointer for the specified derived pointer. 886 CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = ""); 887 888 /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get 889 /// the offset of the specified derived pointer from its base. 890 CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = ""); 891 892 /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale 893 /// will be the same type as that of \p Scaling. 894 Value *CreateVScale(Constant *Scaling, const Twine &Name = ""); 895 896 /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...> 897 Value *CreateStepVector(Type *DstType, const Twine &Name = ""); 898 899 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its 900 /// type. 901 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, 902 Instruction *FMFSource = nullptr, 903 const Twine &Name = ""); 904 905 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the 906 /// first type. 907 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, 908 Instruction *FMFSource = nullptr, 909 const Twine &Name = ""); 910 911 /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If 912 /// \p FMFSource is provided, copy fast-math-flags from that instruction to 913 /// the intrinsic. 914 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types, 915 ArrayRef<Value *> Args, 916 Instruction *FMFSource = nullptr, 917 const Twine &Name = ""); 918 919 /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If 920 /// \p FMFSource is provided, copy fast-math-flags from that instruction to 921 /// the intrinsic. 922 CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID, 923 ArrayRef<Value *> Args, 924 Instruction *FMFSource = nullptr, 925 const Twine &Name = ""); 926 927 /// Create call to the minnum intrinsic. 928 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") { 929 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name); 930 } 931 932 /// Create call to the maxnum intrinsic. 933 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") { 934 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name); 935 } 936 937 /// Create call to the minimum intrinsic. 938 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") { 939 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name); 940 } 941 942 /// Create call to the maximum intrinsic. 943 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") { 944 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name); 945 } 946 947 /// Create call to the copysign intrinsic. 948 CallInst *CreateCopySign(Value *LHS, Value *RHS, 949 Instruction *FMFSource = nullptr, 950 const Twine &Name = "") { 951 return CreateBinaryIntrinsic(Intrinsic::copysign, LHS, RHS, FMFSource, 952 Name); 953 } 954 955 /// Create a call to the arithmetic_fence intrinsic. 956 CallInst *CreateArithmeticFence(Value *Val, Type *DstType, 957 const Twine &Name = "") { 958 return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr, 959 Name); 960 } 961 962 /// Create a call to the vector.extract intrinsic. 963 CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx, 964 const Twine &Name = "") { 965 return CreateIntrinsic(Intrinsic::vector_extract, 966 {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr, 967 Name); 968 } 969 970 /// Create a call to the vector.insert intrinsic. 971 CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec, 972 Value *Idx, const Twine &Name = "") { 973 return CreateIntrinsic(Intrinsic::vector_insert, 974 {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx}, 975 nullptr, Name); 976 } 977 978 private: 979 /// Create a call to a masked intrinsic with given Id. 980 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops, 981 ArrayRef<Type *> OverloadedTypes, 982 const Twine &Name = ""); 983 984 Value *getCastedInt8PtrValue(Value *Ptr); 985 986 //===--------------------------------------------------------------------===// 987 // Instruction creation methods: Terminators 988 //===--------------------------------------------------------------------===// 989 990 private: 991 /// Helper to add branch weight and unpredictable metadata onto an 992 /// instruction. 993 /// \returns The annotated instruction. 994 template <typename InstTy> addBranchMetadata(InstTy * I,MDNode * Weights,MDNode * Unpredictable)995 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) { 996 if (Weights) 997 I->setMetadata(LLVMContext::MD_prof, Weights); 998 if (Unpredictable) 999 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable); 1000 return I; 1001 } 1002 1003 public: 1004 /// Create a 'ret void' instruction. CreateRetVoid()1005 ReturnInst *CreateRetVoid() { 1006 return Insert(ReturnInst::Create(Context)); 1007 } 1008 1009 /// Create a 'ret <val>' instruction. CreateRet(Value * V)1010 ReturnInst *CreateRet(Value *V) { 1011 return Insert(ReturnInst::Create(Context, V)); 1012 } 1013 1014 /// Create a sequence of N insertvalue instructions, 1015 /// with one Value from the retVals array each, that build a aggregate 1016 /// return value one value at a time, and a ret instruction to return 1017 /// the resulting aggregate value. 1018 /// 1019 /// This is a convenience function for code that uses aggregate return values 1020 /// as a vehicle for having multiple return values. CreateAggregateRet(Value * const * retVals,unsigned N)1021 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { 1022 Value *V = PoisonValue::get(getCurrentFunctionReturnType()); 1023 for (unsigned i = 0; i != N; ++i) 1024 V = CreateInsertValue(V, retVals[i], i, "mrv"); 1025 return Insert(ReturnInst::Create(Context, V)); 1026 } 1027 1028 /// Create an unconditional 'br label X' instruction. CreateBr(BasicBlock * Dest)1029 BranchInst *CreateBr(BasicBlock *Dest) { 1030 return Insert(BranchInst::Create(Dest)); 1031 } 1032 1033 /// Create a conditional 'br Cond, TrueDest, FalseDest' 1034 /// instruction. 1035 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, 1036 MDNode *BranchWeights = nullptr, 1037 MDNode *Unpredictable = nullptr) { 1038 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond), 1039 BranchWeights, Unpredictable)); 1040 } 1041 1042 /// Create a conditional 'br Cond, TrueDest, FalseDest' 1043 /// instruction. Copy branch meta data if available. CreateCondBr(Value * Cond,BasicBlock * True,BasicBlock * False,Instruction * MDSrc)1044 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, 1045 Instruction *MDSrc) { 1046 BranchInst *Br = BranchInst::Create(True, False, Cond); 1047 if (MDSrc) { 1048 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable, 1049 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg}; 1050 Br->copyMetadata(*MDSrc, WL); 1051 } 1052 return Insert(Br); 1053 } 1054 1055 /// Create a switch instruction with the specified value, default dest, 1056 /// and with a hint for the number of cases that will be added (for efficient 1057 /// allocation). 1058 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, 1059 MDNode *BranchWeights = nullptr, 1060 MDNode *Unpredictable = nullptr) { 1061 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases), 1062 BranchWeights, Unpredictable)); 1063 } 1064 1065 /// Create an indirect branch instruction with the specified address 1066 /// operand, with an optional hint for the number of destinations that will be 1067 /// added (for efficient allocation). 1068 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { 1069 return Insert(IndirectBrInst::Create(Addr, NumDests)); 1070 } 1071 1072 /// Create an invoke instruction. 1073 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, 1074 BasicBlock *NormalDest, BasicBlock *UnwindDest, 1075 ArrayRef<Value *> Args, 1076 ArrayRef<OperandBundleDef> OpBundles, 1077 const Twine &Name = "") { 1078 InvokeInst *II = 1079 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles); 1080 if (IsFPConstrained) 1081 setConstrainedFPCallAttr(II); 1082 return Insert(II, Name); 1083 } 1084 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, 1085 BasicBlock *NormalDest, BasicBlock *UnwindDest, 1086 ArrayRef<Value *> Args = std::nullopt, 1087 const Twine &Name = "") { 1088 InvokeInst *II = 1089 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args); 1090 if (IsFPConstrained) 1091 setConstrainedFPCallAttr(II); 1092 return Insert(II, Name); 1093 } 1094 1095 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, 1096 BasicBlock *UnwindDest, ArrayRef<Value *> Args, 1097 ArrayRef<OperandBundleDef> OpBundles, 1098 const Twine &Name = "") { 1099 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), 1100 NormalDest, UnwindDest, Args, OpBundles, Name); 1101 } 1102 1103 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, 1104 BasicBlock *UnwindDest, 1105 ArrayRef<Value *> Args = std::nullopt, 1106 const Twine &Name = "") { 1107 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), 1108 NormalDest, UnwindDest, Args, Name); 1109 } 1110 1111 /// \brief Create a callbr instruction. 1112 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, 1113 BasicBlock *DefaultDest, 1114 ArrayRef<BasicBlock *> IndirectDests, 1115 ArrayRef<Value *> Args = std::nullopt, 1116 const Twine &Name = "") { 1117 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, 1118 Args), Name); 1119 } 1120 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, 1121 BasicBlock *DefaultDest, 1122 ArrayRef<BasicBlock *> IndirectDests, 1123 ArrayRef<Value *> Args, 1124 ArrayRef<OperandBundleDef> OpBundles, 1125 const Twine &Name = "") { 1126 return Insert( 1127 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args, 1128 OpBundles), Name); 1129 } 1130 1131 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, 1132 ArrayRef<BasicBlock *> IndirectDests, 1133 ArrayRef<Value *> Args = std::nullopt, 1134 const Twine &Name = "") { 1135 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), 1136 DefaultDest, IndirectDests, Args, Name); 1137 } 1138 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, 1139 ArrayRef<BasicBlock *> IndirectDests, 1140 ArrayRef<Value *> Args, 1141 ArrayRef<OperandBundleDef> OpBundles, 1142 const Twine &Name = "") { 1143 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), 1144 DefaultDest, IndirectDests, Args, Name); 1145 } 1146 CreateResume(Value * Exn)1147 ResumeInst *CreateResume(Value *Exn) { 1148 return Insert(ResumeInst::Create(Exn)); 1149 } 1150 1151 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad, 1152 BasicBlock *UnwindBB = nullptr) { 1153 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB)); 1154 } 1155 1156 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, 1157 unsigned NumHandlers, 1158 const Twine &Name = "") { 1159 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers), 1160 Name); 1161 } 1162 1163 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args, 1164 const Twine &Name = "") { 1165 return Insert(CatchPadInst::Create(ParentPad, Args), Name); 1166 } 1167 1168 CleanupPadInst *CreateCleanupPad(Value *ParentPad, 1169 ArrayRef<Value *> Args = std::nullopt, 1170 const Twine &Name = "") { 1171 return Insert(CleanupPadInst::Create(ParentPad, Args), Name); 1172 } 1173 CreateCatchRet(CatchPadInst * CatchPad,BasicBlock * BB)1174 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) { 1175 return Insert(CatchReturnInst::Create(CatchPad, BB)); 1176 } 1177 CreateUnreachable()1178 UnreachableInst *CreateUnreachable() { 1179 return Insert(new UnreachableInst(Context)); 1180 } 1181 1182 //===--------------------------------------------------------------------===// 1183 // Instruction creation methods: Binary Operators 1184 //===--------------------------------------------------------------------===// 1185 private: CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,Value * LHS,Value * RHS,const Twine & Name,bool HasNUW,bool HasNSW)1186 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, 1187 Value *LHS, Value *RHS, 1188 const Twine &Name, 1189 bool HasNUW, bool HasNSW) { 1190 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); 1191 if (HasNUW) BO->setHasNoUnsignedWrap(); 1192 if (HasNSW) BO->setHasNoSignedWrap(); 1193 return BO; 1194 } 1195 setFPAttrs(Instruction * I,MDNode * FPMD,FastMathFlags FMF)1196 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD, 1197 FastMathFlags FMF) const { 1198 if (!FPMD) 1199 FPMD = DefaultFPMathTag; 1200 if (FPMD) 1201 I->setMetadata(LLVMContext::MD_fpmath, FPMD); 1202 I->setFastMathFlags(FMF); 1203 return I; 1204 } 1205 getConstrainedFPRounding(std::optional<RoundingMode> Rounding)1206 Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) { 1207 RoundingMode UseRounding = DefaultConstrainedRounding; 1208 1209 if (Rounding) 1210 UseRounding = *Rounding; 1211 1212 std::optional<StringRef> RoundingStr = 1213 convertRoundingModeToStr(UseRounding); 1214 assert(RoundingStr && "Garbage strict rounding mode!"); 1215 auto *RoundingMDS = MDString::get(Context, *RoundingStr); 1216 1217 return MetadataAsValue::get(Context, RoundingMDS); 1218 } 1219 getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except)1220 Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) { 1221 std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr( 1222 Except.value_or(DefaultConstrainedExcept)); 1223 assert(ExceptStr && "Garbage strict exception behavior!"); 1224 auto *ExceptMDS = MDString::get(Context, *ExceptStr); 1225 1226 return MetadataAsValue::get(Context, ExceptMDS); 1227 } 1228 getConstrainedFPPredicate(CmpInst::Predicate Predicate)1229 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) { 1230 assert(CmpInst::isFPPredicate(Predicate) && 1231 Predicate != CmpInst::FCMP_FALSE && 1232 Predicate != CmpInst::FCMP_TRUE && 1233 "Invalid constrained FP comparison predicate!"); 1234 1235 StringRef PredicateStr = CmpInst::getPredicateName(Predicate); 1236 auto *PredicateMDS = MDString::get(Context, PredicateStr); 1237 1238 return MetadataAsValue::get(Context, PredicateMDS); 1239 } 1240 1241 public: 1242 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", 1243 bool HasNUW = false, bool HasNSW = false) { 1244 if (Value *V = 1245 Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW)) 1246 return V; 1247 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW, 1248 HasNSW); 1249 } 1250 1251 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 1252 return CreateAdd(LHS, RHS, Name, false, true); 1253 } 1254 1255 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 1256 return CreateAdd(LHS, RHS, Name, true, false); 1257 } 1258 1259 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", 1260 bool HasNUW = false, bool HasNSW = false) { 1261 if (Value *V = 1262 Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW)) 1263 return V; 1264 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW, 1265 HasNSW); 1266 } 1267 1268 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 1269 return CreateSub(LHS, RHS, Name, false, true); 1270 } 1271 1272 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 1273 return CreateSub(LHS, RHS, Name, true, false); 1274 } 1275 1276 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", 1277 bool HasNUW = false, bool HasNSW = false) { 1278 if (Value *V = 1279 Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW)) 1280 return V; 1281 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW, 1282 HasNSW); 1283 } 1284 1285 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 1286 return CreateMul(LHS, RHS, Name, false, true); 1287 } 1288 1289 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 1290 return CreateMul(LHS, RHS, Name, true, false); 1291 } 1292 1293 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", 1294 bool isExact = false) { 1295 if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact)) 1296 return V; 1297 if (!isExact) 1298 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); 1299 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); 1300 } 1301 1302 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 1303 return CreateUDiv(LHS, RHS, Name, true); 1304 } 1305 1306 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", 1307 bool isExact = false) { 1308 if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact)) 1309 return V; 1310 if (!isExact) 1311 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); 1312 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); 1313 } 1314 1315 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 1316 return CreateSDiv(LHS, RHS, Name, true); 1317 } 1318 1319 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { 1320 if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS)) 1321 return V; 1322 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); 1323 } 1324 1325 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { 1326 if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS)) 1327 return V; 1328 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); 1329 } 1330 1331 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", 1332 bool HasNUW = false, bool HasNSW = false) { 1333 if (Value *V = 1334 Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW)) 1335 return V; 1336 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, 1337 HasNUW, HasNSW); 1338 } 1339 1340 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", 1341 bool HasNUW = false, bool HasNSW = false) { 1342 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 1343 HasNUW, HasNSW); 1344 } 1345 1346 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", 1347 bool HasNUW = false, bool HasNSW = false) { 1348 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 1349 HasNUW, HasNSW); 1350 } 1351 1352 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", 1353 bool isExact = false) { 1354 if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact)) 1355 return V; 1356 if (!isExact) 1357 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); 1358 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); 1359 } 1360 1361 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 1362 bool isExact = false) { 1363 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 1364 } 1365 1366 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 1367 bool isExact = false) { 1368 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 1369 } 1370 1371 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", 1372 bool isExact = false) { 1373 if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact)) 1374 return V; 1375 if (!isExact) 1376 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); 1377 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); 1378 } 1379 1380 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 1381 bool isExact = false) { 1382 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 1383 } 1384 1385 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 1386 bool isExact = false) { 1387 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 1388 } 1389 1390 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { 1391 if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS)) 1392 return V; 1393 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); 1394 } 1395 1396 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { 1397 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1398 } 1399 1400 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { 1401 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1402 } 1403 CreateAnd(ArrayRef<Value * > Ops)1404 Value *CreateAnd(ArrayRef<Value*> Ops) { 1405 assert(!Ops.empty()); 1406 Value *Accum = Ops[0]; 1407 for (unsigned i = 1; i < Ops.size(); i++) 1408 Accum = CreateAnd(Accum, Ops[i]); 1409 return Accum; 1410 } 1411 1412 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { 1413 if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS)) 1414 return V; 1415 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); 1416 } 1417 1418 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { 1419 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1420 } 1421 1422 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { 1423 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1424 } 1425 CreateOr(ArrayRef<Value * > Ops)1426 Value *CreateOr(ArrayRef<Value*> Ops) { 1427 assert(!Ops.empty()); 1428 Value *Accum = Ops[0]; 1429 for (unsigned i = 1; i < Ops.size(); i++) 1430 Accum = CreateOr(Accum, Ops[i]); 1431 return Accum; 1432 } 1433 1434 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { 1435 if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS)) 1436 return V; 1437 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); 1438 } 1439 1440 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { 1441 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1442 } 1443 1444 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { 1445 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 1446 } 1447 1448 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "", 1449 MDNode *FPMD = nullptr) { 1450 if (IsFPConstrained) 1451 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, 1452 L, R, nullptr, Name, FPMD); 1453 1454 if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF)) 1455 return V; 1456 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF); 1457 return Insert(I, Name); 1458 } 1459 1460 /// Copy fast-math-flags from an instruction rather than using the builder's 1461 /// default FMF. 1462 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, 1463 const Twine &Name = "") { 1464 if (IsFPConstrained) 1465 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, 1466 L, R, FMFSource, Name); 1467 1468 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1469 if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF)) 1470 return V; 1471 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, FMF); 1472 return Insert(I, Name); 1473 } 1474 1475 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "", 1476 MDNode *FPMD = nullptr) { 1477 if (IsFPConstrained) 1478 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, 1479 L, R, nullptr, Name, FPMD); 1480 1481 if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF)) 1482 return V; 1483 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF); 1484 return Insert(I, Name); 1485 } 1486 1487 /// Copy fast-math-flags from an instruction rather than using the builder's 1488 /// default FMF. 1489 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, 1490 const Twine &Name = "") { 1491 if (IsFPConstrained) 1492 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, 1493 L, R, FMFSource, Name); 1494 1495 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1496 if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF)) 1497 return V; 1498 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, FMF); 1499 return Insert(I, Name); 1500 } 1501 1502 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "", 1503 MDNode *FPMD = nullptr) { 1504 if (IsFPConstrained) 1505 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, 1506 L, R, nullptr, Name, FPMD); 1507 1508 if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF)) 1509 return V; 1510 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF); 1511 return Insert(I, Name); 1512 } 1513 1514 /// Copy fast-math-flags from an instruction rather than using the builder's 1515 /// default FMF. 1516 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, 1517 const Twine &Name = "") { 1518 if (IsFPConstrained) 1519 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, 1520 L, R, FMFSource, Name); 1521 1522 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1523 if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF)) 1524 return V; 1525 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, FMF); 1526 return Insert(I, Name); 1527 } 1528 1529 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "", 1530 MDNode *FPMD = nullptr) { 1531 if (IsFPConstrained) 1532 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, 1533 L, R, nullptr, Name, FPMD); 1534 1535 if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF)) 1536 return V; 1537 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF); 1538 return Insert(I, Name); 1539 } 1540 1541 /// Copy fast-math-flags from an instruction rather than using the builder's 1542 /// default FMF. 1543 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, 1544 const Twine &Name = "") { 1545 if (IsFPConstrained) 1546 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, 1547 L, R, FMFSource, Name); 1548 1549 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1550 if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF)) 1551 return V; 1552 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, FMF); 1553 return Insert(I, Name); 1554 } 1555 1556 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "", 1557 MDNode *FPMD = nullptr) { 1558 if (IsFPConstrained) 1559 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, 1560 L, R, nullptr, Name, FPMD); 1561 1562 if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V; 1563 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF); 1564 return Insert(I, Name); 1565 } 1566 1567 /// Copy fast-math-flags from an instruction rather than using the builder's 1568 /// default FMF. 1569 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, 1570 const Twine &Name = "") { 1571 if (IsFPConstrained) 1572 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, 1573 L, R, FMFSource, Name); 1574 1575 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1576 if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V; 1577 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, FMF); 1578 return Insert(I, Name); 1579 } 1580 1581 Value *CreateBinOp(Instruction::BinaryOps Opc, 1582 Value *LHS, Value *RHS, const Twine &Name = "", 1583 MDNode *FPMathTag = nullptr) { 1584 if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS)) return V; 1585 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); 1586 if (isa<FPMathOperator>(BinOp)) 1587 setFPAttrs(BinOp, FPMathTag, FMF); 1588 return Insert(BinOp, Name); 1589 } 1590 1591 Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") { 1592 assert(Cond2->getType()->isIntOrIntVectorTy(1)); 1593 return CreateSelect(Cond1, Cond2, 1594 ConstantInt::getNullValue(Cond2->getType()), Name); 1595 } 1596 1597 Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") { 1598 assert(Cond2->getType()->isIntOrIntVectorTy(1)); 1599 return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()), 1600 Cond2, Name); 1601 } 1602 1603 Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2, 1604 const Twine &Name = "") { 1605 switch (Opc) { 1606 case Instruction::And: 1607 return CreateLogicalAnd(Cond1, Cond2, Name); 1608 case Instruction::Or: 1609 return CreateLogicalOr(Cond1, Cond2, Name); 1610 default: 1611 break; 1612 } 1613 llvm_unreachable("Not a logical operation."); 1614 } 1615 1616 // NOTE: this is sequential, non-commutative, ordered reduction! CreateLogicalOr(ArrayRef<Value * > Ops)1617 Value *CreateLogicalOr(ArrayRef<Value *> Ops) { 1618 assert(!Ops.empty()); 1619 Value *Accum = Ops[0]; 1620 for (unsigned i = 1; i < Ops.size(); i++) 1621 Accum = CreateLogicalOr(Accum, Ops[i]); 1622 return Accum; 1623 } 1624 1625 CallInst *CreateConstrainedFPBinOp( 1626 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr, 1627 const Twine &Name = "", MDNode *FPMathTag = nullptr, 1628 std::optional<RoundingMode> Rounding = std::nullopt, 1629 std::optional<fp::ExceptionBehavior> Except = std::nullopt); 1630 1631 Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNUW = false, 1632 bool HasNSW = false) { 1633 return CreateSub(Constant::getNullValue(V->getType()), V, Name, HasNUW, 1634 HasNSW); 1635 } 1636 1637 Value *CreateNSWNeg(Value *V, const Twine &Name = "") { 1638 return CreateNeg(V, Name, false, true); 1639 } 1640 1641 Value *CreateNUWNeg(Value *V, const Twine &Name = "") { 1642 return CreateNeg(V, Name, true, false); 1643 } 1644 1645 Value *CreateFNeg(Value *V, const Twine &Name = "", 1646 MDNode *FPMathTag = nullptr) { 1647 if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF)) 1648 return Res; 1649 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF), 1650 Name); 1651 } 1652 1653 /// Copy fast-math-flags from an instruction rather than using the builder's 1654 /// default FMF. 1655 Value *CreateFNegFMF(Value *V, Instruction *FMFSource, 1656 const Twine &Name = "") { 1657 FastMathFlags FMF = FMFSource->getFastMathFlags(); 1658 if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF)) 1659 return Res; 1660 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, FMF), 1661 Name); 1662 } 1663 1664 Value *CreateNot(Value *V, const Twine &Name = "") { 1665 return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name); 1666 } 1667 1668 Value *CreateUnOp(Instruction::UnaryOps Opc, 1669 Value *V, const Twine &Name = "", 1670 MDNode *FPMathTag = nullptr) { 1671 if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF)) 1672 return Res; 1673 Instruction *UnOp = UnaryOperator::Create(Opc, V); 1674 if (isa<FPMathOperator>(UnOp)) 1675 setFPAttrs(UnOp, FPMathTag, FMF); 1676 return Insert(UnOp, Name); 1677 } 1678 1679 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc. 1680 /// Correct number of operands must be passed accordingly. 1681 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops, 1682 const Twine &Name = "", MDNode *FPMathTag = nullptr); 1683 1684 //===--------------------------------------------------------------------===// 1685 // Instruction creation methods: Memory Instructions 1686 //===--------------------------------------------------------------------===// 1687 1688 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace, 1689 Value *ArraySize = nullptr, const Twine &Name = "") { 1690 const DataLayout &DL = BB->getModule()->getDataLayout(); 1691 Align AllocaAlign = DL.getPrefTypeAlign(Ty); 1692 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); 1693 } 1694 1695 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, 1696 const Twine &Name = "") { 1697 const DataLayout &DL = BB->getModule()->getDataLayout(); 1698 Align AllocaAlign = DL.getPrefTypeAlign(Ty); 1699 unsigned AddrSpace = DL.getAllocaAddrSpace(); 1700 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); 1701 } 1702 1703 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of 1704 /// converting the string to 'bool' for the isVolatile parameter. CreateLoad(Type * Ty,Value * Ptr,const char * Name)1705 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) { 1706 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); 1707 } 1708 1709 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") { 1710 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); 1711 } 1712 1713 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile, 1714 const Twine &Name = "") { 1715 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name); 1716 } 1717 1718 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { 1719 return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile); 1720 } 1721 CreateAlignedLoad(Type * Ty,Value * Ptr,MaybeAlign Align,const char * Name)1722 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, 1723 const char *Name) { 1724 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); 1725 } 1726 1727 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, 1728 const Twine &Name = "") { 1729 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); 1730 } 1731 1732 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, 1733 bool isVolatile, const Twine &Name = "") { 1734 if (!Align) { 1735 const DataLayout &DL = BB->getModule()->getDataLayout(); 1736 Align = DL.getABITypeAlign(Ty); 1737 } 1738 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name); 1739 } 1740 1741 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, 1742 bool isVolatile = false) { 1743 if (!Align) { 1744 const DataLayout &DL = BB->getModule()->getDataLayout(); 1745 Align = DL.getABITypeAlign(Val->getType()); 1746 } 1747 return Insert(new StoreInst(Val, Ptr, isVolatile, *Align)); 1748 } 1749 FenceInst *CreateFence(AtomicOrdering Ordering, 1750 SyncScope::ID SSID = SyncScope::System, 1751 const Twine &Name = "") { 1752 return Insert(new FenceInst(Context, Ordering, SSID), Name); 1753 } 1754 1755 AtomicCmpXchgInst * 1756 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, 1757 AtomicOrdering SuccessOrdering, 1758 AtomicOrdering FailureOrdering, 1759 SyncScope::ID SSID = SyncScope::System) { 1760 if (!Align) { 1761 const DataLayout &DL = BB->getModule()->getDataLayout(); 1762 Align = llvm::Align(DL.getTypeStoreSize(New->getType())); 1763 } 1764 1765 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering, 1766 FailureOrdering, SSID)); 1767 } 1768 1769 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, 1770 Value *Val, MaybeAlign Align, 1771 AtomicOrdering Ordering, 1772 SyncScope::ID SSID = SyncScope::System) { 1773 if (!Align) { 1774 const DataLayout &DL = BB->getModule()->getDataLayout(); 1775 Align = llvm::Align(DL.getTypeStoreSize(Val->getType())); 1776 } 1777 1778 return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID)); 1779 } 1780 1781 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, 1782 const Twine &Name = "", bool IsInBounds = false) { 1783 if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, IsInBounds)) 1784 return V; 1785 return Insert(IsInBounds 1786 ? GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList) 1787 : GetElementPtrInst::Create(Ty, Ptr, IdxList), 1788 Name); 1789 } 1790 1791 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, 1792 const Twine &Name = "") { 1793 return CreateGEP(Ty, Ptr, IdxList, Name, /* IsInBounds */ true); 1794 } 1795 1796 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, 1797 const Twine &Name = "") { 1798 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 1799 1800 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false)) 1801 return V; 1802 1803 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); 1804 } 1805 1806 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, 1807 const Twine &Name = "") { 1808 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 1809 1810 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true)) 1811 return V; 1812 1813 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); 1814 } 1815 1816 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, 1817 const Twine &Name = "") { 1818 Value *Idxs[] = { 1819 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 1820 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 1821 }; 1822 1823 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false)) 1824 return V; 1825 1826 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); 1827 } 1828 1829 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, 1830 unsigned Idx1, const Twine &Name = "") { 1831 Value *Idxs[] = { 1832 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 1833 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 1834 }; 1835 1836 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true)) 1837 return V; 1838 1839 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); 1840 } 1841 1842 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, 1843 const Twine &Name = "") { 1844 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 1845 1846 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false)) 1847 return V; 1848 1849 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); 1850 } 1851 1852 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, 1853 const Twine &Name = "") { 1854 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 1855 1856 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true)) 1857 return V; 1858 1859 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); 1860 } 1861 1862 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, 1863 const Twine &Name = "") { 1864 Value *Idxs[] = { 1865 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 1866 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 1867 }; 1868 1869 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false)) 1870 return V; 1871 1872 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); 1873 } 1874 1875 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, 1876 uint64_t Idx1, const Twine &Name = "") { 1877 Value *Idxs[] = { 1878 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 1879 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 1880 }; 1881 1882 if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true)) 1883 return V; 1884 1885 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); 1886 } 1887 1888 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, 1889 const Twine &Name = "") { 1890 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name); 1891 } 1892 1893 /// Same as CreateGlobalString, but return a pointer with "i8*" type 1894 /// instead of a pointer to array of i8. 1895 /// 1896 /// If no module is given via \p M, it is take from the insertion point basic 1897 /// block. 1898 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", 1899 unsigned AddressSpace = 0, 1900 Module *M = nullptr) { 1901 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M); 1902 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); 1903 Constant *Indices[] = {Zero, Zero}; 1904 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV, 1905 Indices); 1906 } 1907 1908 //===--------------------------------------------------------------------===// 1909 // Instruction creation methods: Cast/Conversion Operators 1910 //===--------------------------------------------------------------------===// 1911 1912 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { 1913 return CreateCast(Instruction::Trunc, V, DestTy, Name); 1914 } 1915 1916 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { 1917 return CreateCast(Instruction::ZExt, V, DestTy, Name); 1918 } 1919 1920 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { 1921 return CreateCast(Instruction::SExt, V, DestTy, Name); 1922 } 1923 1924 /// Create a ZExt or Trunc from the integer value V to DestTy. Return 1925 /// the value untouched if the type of V is already DestTy. 1926 Value *CreateZExtOrTrunc(Value *V, Type *DestTy, 1927 const Twine &Name = "") { 1928 assert(V->getType()->isIntOrIntVectorTy() && 1929 DestTy->isIntOrIntVectorTy() && 1930 "Can only zero extend/truncate integers!"); 1931 Type *VTy = V->getType(); 1932 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 1933 return CreateZExt(V, DestTy, Name); 1934 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 1935 return CreateTrunc(V, DestTy, Name); 1936 return V; 1937 } 1938 1939 /// Create a SExt or Trunc from the integer value V to DestTy. Return 1940 /// the value untouched if the type of V is already DestTy. 1941 Value *CreateSExtOrTrunc(Value *V, Type *DestTy, 1942 const Twine &Name = "") { 1943 assert(V->getType()->isIntOrIntVectorTy() && 1944 DestTy->isIntOrIntVectorTy() && 1945 "Can only sign extend/truncate integers!"); 1946 Type *VTy = V->getType(); 1947 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 1948 return CreateSExt(V, DestTy, Name); 1949 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 1950 return CreateTrunc(V, DestTy, Name); 1951 return V; 1952 } 1953 1954 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") { 1955 if (IsFPConstrained) 1956 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui, 1957 V, DestTy, nullptr, Name); 1958 return CreateCast(Instruction::FPToUI, V, DestTy, Name); 1959 } 1960 1961 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") { 1962 if (IsFPConstrained) 1963 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi, 1964 V, DestTy, nullptr, Name); 1965 return CreateCast(Instruction::FPToSI, V, DestTy, Name); 1966 } 1967 1968 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 1969 if (IsFPConstrained) 1970 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp, 1971 V, DestTy, nullptr, Name); 1972 return CreateCast(Instruction::UIToFP, V, DestTy, Name); 1973 } 1974 1975 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 1976 if (IsFPConstrained) 1977 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp, 1978 V, DestTy, nullptr, Name); 1979 return CreateCast(Instruction::SIToFP, V, DestTy, Name); 1980 } 1981 1982 Value *CreateFPTrunc(Value *V, Type *DestTy, 1983 const Twine &Name = "") { 1984 if (IsFPConstrained) 1985 return CreateConstrainedFPCast( 1986 Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr, 1987 Name); 1988 return CreateCast(Instruction::FPTrunc, V, DestTy, Name); 1989 } 1990 1991 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { 1992 if (IsFPConstrained) 1993 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext, 1994 V, DestTy, nullptr, Name); 1995 return CreateCast(Instruction::FPExt, V, DestTy, Name); 1996 } 1997 1998 Value *CreatePtrToInt(Value *V, Type *DestTy, 1999 const Twine &Name = "") { 2000 return CreateCast(Instruction::PtrToInt, V, DestTy, Name); 2001 } 2002 2003 Value *CreateIntToPtr(Value *V, Type *DestTy, 2004 const Twine &Name = "") { 2005 return CreateCast(Instruction::IntToPtr, V, DestTy, Name); 2006 } 2007 2008 Value *CreateBitCast(Value *V, Type *DestTy, 2009 const Twine &Name = "") { 2010 return CreateCast(Instruction::BitCast, V, DestTy, Name); 2011 } 2012 2013 Value *CreateAddrSpaceCast(Value *V, Type *DestTy, 2014 const Twine &Name = "") { 2015 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name); 2016 } 2017 2018 Value *CreateZExtOrBitCast(Value *V, Type *DestTy, 2019 const Twine &Name = "") { 2020 if (V->getType() == DestTy) 2021 return V; 2022 if (auto *VC = dyn_cast<Constant>(V)) 2023 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); 2024 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); 2025 } 2026 2027 Value *CreateSExtOrBitCast(Value *V, Type *DestTy, 2028 const Twine &Name = "") { 2029 if (V->getType() == DestTy) 2030 return V; 2031 if (auto *VC = dyn_cast<Constant>(V)) 2032 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); 2033 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); 2034 } 2035 2036 Value *CreateTruncOrBitCast(Value *V, Type *DestTy, 2037 const Twine &Name = "") { 2038 if (V->getType() == DestTy) 2039 return V; 2040 if (auto *VC = dyn_cast<Constant>(V)) 2041 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); 2042 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); 2043 } 2044 2045 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, 2046 const Twine &Name = "") { 2047 if (V->getType() == DestTy) 2048 return V; 2049 if (auto *VC = dyn_cast<Constant>(V)) 2050 return Insert(Folder.CreateCast(Op, VC, DestTy), Name); 2051 return Insert(CastInst::Create(Op, V, DestTy), Name); 2052 } 2053 2054 Value *CreatePointerCast(Value *V, Type *DestTy, 2055 const Twine &Name = "") { 2056 if (V->getType() == DestTy) 2057 return V; 2058 if (auto *VC = dyn_cast<Constant>(V)) 2059 return Insert(Folder.CreatePointerCast(VC, DestTy), Name); 2060 return Insert(CastInst::CreatePointerCast(V, DestTy), Name); 2061 } 2062 2063 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, 2064 const Twine &Name = "") { 2065 if (V->getType() == DestTy) 2066 return V; 2067 2068 if (auto *VC = dyn_cast<Constant>(V)) { 2069 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy), 2070 Name); 2071 } 2072 2073 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy), 2074 Name); 2075 } 2076 2077 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, 2078 const Twine &Name = "") { 2079 if (V->getType() == DestTy) 2080 return V; 2081 if (auto *VC = dyn_cast<Constant>(V)) 2082 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); 2083 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); 2084 } 2085 2086 Value *CreateBitOrPointerCast(Value *V, Type *DestTy, 2087 const Twine &Name = "") { 2088 if (V->getType() == DestTy) 2089 return V; 2090 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy()) 2091 return CreatePtrToInt(V, DestTy, Name); 2092 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy()) 2093 return CreateIntToPtr(V, DestTy, Name); 2094 2095 return CreateBitCast(V, DestTy, Name); 2096 } 2097 2098 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { 2099 if (V->getType() == DestTy) 2100 return V; 2101 if (auto *VC = dyn_cast<Constant>(V)) 2102 return Insert(Folder.CreateFPCast(VC, DestTy), Name); 2103 return Insert(CastInst::CreateFPCast(V, DestTy), Name); 2104 } 2105 2106 CallInst *CreateConstrainedFPCast( 2107 Intrinsic::ID ID, Value *V, Type *DestTy, 2108 Instruction *FMFSource = nullptr, const Twine &Name = "", 2109 MDNode *FPMathTag = nullptr, 2110 std::optional<RoundingMode> Rounding = std::nullopt, 2111 std::optional<fp::ExceptionBehavior> Except = std::nullopt); 2112 2113 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a 2114 // compile time error, instead of converting the string to bool for the 2115 // isSigned parameter. 2116 Value *CreateIntCast(Value *, Type *, const char *) = delete; 2117 2118 //===--------------------------------------------------------------------===// 2119 // Instruction creation methods: Compare Instructions 2120 //===--------------------------------------------------------------------===// 2121 2122 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { 2123 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); 2124 } 2125 2126 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { 2127 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); 2128 } 2129 2130 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { 2131 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); 2132 } 2133 2134 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { 2135 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); 2136 } 2137 2138 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { 2139 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); 2140 } 2141 2142 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { 2143 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); 2144 } 2145 2146 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { 2147 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); 2148 } 2149 2150 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { 2151 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); 2152 } 2153 2154 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { 2155 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); 2156 } 2157 2158 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { 2159 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); 2160 } 2161 2162 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "", 2163 MDNode *FPMathTag = nullptr) { 2164 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag); 2165 } 2166 2167 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "", 2168 MDNode *FPMathTag = nullptr) { 2169 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag); 2170 } 2171 2172 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "", 2173 MDNode *FPMathTag = nullptr) { 2174 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag); 2175 } 2176 2177 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "", 2178 MDNode *FPMathTag = nullptr) { 2179 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag); 2180 } 2181 2182 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "", 2183 MDNode *FPMathTag = nullptr) { 2184 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag); 2185 } 2186 2187 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "", 2188 MDNode *FPMathTag = nullptr) { 2189 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag); 2190 } 2191 2192 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "", 2193 MDNode *FPMathTag = nullptr) { 2194 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag); 2195 } 2196 2197 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "", 2198 MDNode *FPMathTag = nullptr) { 2199 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag); 2200 } 2201 2202 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "", 2203 MDNode *FPMathTag = nullptr) { 2204 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag); 2205 } 2206 2207 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "", 2208 MDNode *FPMathTag = nullptr) { 2209 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag); 2210 } 2211 2212 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "", 2213 MDNode *FPMathTag = nullptr) { 2214 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag); 2215 } 2216 2217 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "", 2218 MDNode *FPMathTag = nullptr) { 2219 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag); 2220 } 2221 2222 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "", 2223 MDNode *FPMathTag = nullptr) { 2224 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag); 2225 } 2226 2227 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "", 2228 MDNode *FPMathTag = nullptr) { 2229 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag); 2230 } 2231 2232 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 2233 const Twine &Name = "") { 2234 if (auto *V = Folder.FoldICmp(P, LHS, RHS)) 2235 return V; 2236 return Insert(new ICmpInst(P, LHS, RHS), Name); 2237 } 2238 2239 // Create a quiet floating-point comparison (i.e. one that raises an FP 2240 // exception only in the case where an input is a signaling NaN). 2241 // Note that this differs from CreateFCmpS only if IsFPConstrained is true. 2242 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 2243 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2244 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false); 2245 } 2246 2247 Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, 2248 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2249 return CmpInst::isFPPredicate(Pred) 2250 ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag) 2251 : CreateICmp(Pred, LHS, RHS, Name); 2252 } 2253 2254 // Create a signaling floating-point comparison (i.e. one that raises an FP 2255 // exception whenever an input is any NaN, signaling or quiet). 2256 // Note that this differs from CreateFCmp only if IsFPConstrained is true. 2257 Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS, 2258 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2259 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true); 2260 } 2261 2262 private: 2263 // Helper routine to create either a signaling or a quiet FP comparison. 2264 Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS, 2265 const Twine &Name, MDNode *FPMathTag, 2266 bool IsSignaling); 2267 2268 public: 2269 CallInst *CreateConstrainedFPCmp( 2270 Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R, 2271 const Twine &Name = "", 2272 std::optional<fp::ExceptionBehavior> Except = std::nullopt); 2273 2274 //===--------------------------------------------------------------------===// 2275 // Instruction creation methods: Other Instructions 2276 //===--------------------------------------------------------------------===// 2277 2278 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, 2279 const Twine &Name = "") { 2280 PHINode *Phi = PHINode::Create(Ty, NumReservedValues); 2281 if (isa<FPMathOperator>(Phi)) 2282 setFPAttrs(Phi, nullptr /* MDNode* */, FMF); 2283 return Insert(Phi, Name); 2284 } 2285 2286 private: 2287 CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops, 2288 const Twine &Name = "", 2289 Instruction *FMFSource = nullptr, 2290 ArrayRef<OperandBundleDef> OpBundles = {}); 2291 2292 public: 2293 CallInst *CreateCall(FunctionType *FTy, Value *Callee, 2294 ArrayRef<Value *> Args = std::nullopt, 2295 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2296 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); 2297 if (IsFPConstrained) 2298 setConstrainedFPCallAttr(CI); 2299 if (isa<FPMathOperator>(CI)) 2300 setFPAttrs(CI, FPMathTag, FMF); 2301 return Insert(CI, Name); 2302 } 2303 2304 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args, 2305 ArrayRef<OperandBundleDef> OpBundles, 2306 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2307 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles); 2308 if (IsFPConstrained) 2309 setConstrainedFPCallAttr(CI); 2310 if (isa<FPMathOperator>(CI)) 2311 setFPAttrs(CI, FPMathTag, FMF); 2312 return Insert(CI, Name); 2313 } 2314 2315 CallInst *CreateCall(FunctionCallee Callee, 2316 ArrayRef<Value *> Args = std::nullopt, 2317 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2318 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name, 2319 FPMathTag); 2320 } 2321 2322 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args, 2323 ArrayRef<OperandBundleDef> OpBundles, 2324 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 2325 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, 2326 OpBundles, Name, FPMathTag); 2327 } 2328 2329 CallInst *CreateConstrainedFPCall( 2330 Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "", 2331 std::optional<RoundingMode> Rounding = std::nullopt, 2332 std::optional<fp::ExceptionBehavior> Except = std::nullopt); 2333 2334 Value *CreateSelect(Value *C, Value *True, Value *False, 2335 const Twine &Name = "", Instruction *MDFrom = nullptr); 2336 2337 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { 2338 return Insert(new VAArgInst(List, Ty), Name); 2339 } 2340 2341 Value *CreateExtractElement(Value *Vec, Value *Idx, 2342 const Twine &Name = "") { 2343 if (Value *V = Folder.FoldExtractElement(Vec, Idx)) 2344 return V; 2345 return Insert(ExtractElementInst::Create(Vec, Idx), Name); 2346 } 2347 2348 Value *CreateExtractElement(Value *Vec, uint64_t Idx, 2349 const Twine &Name = "") { 2350 return CreateExtractElement(Vec, getInt64(Idx), Name); 2351 } 2352 2353 Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, 2354 const Twine &Name = "") { 2355 return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name); 2356 } 2357 2358 Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx, 2359 const Twine &Name = "") { 2360 return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name); 2361 } 2362 2363 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, 2364 const Twine &Name = "") { 2365 if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx)) 2366 return V; 2367 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); 2368 } 2369 2370 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, 2371 const Twine &Name = "") { 2372 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name); 2373 } 2374 2375 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, 2376 const Twine &Name = "") { 2377 SmallVector<int, 16> IntMask; 2378 ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask); 2379 return CreateShuffleVector(V1, V2, IntMask, Name); 2380 } 2381 2382 /// See class ShuffleVectorInst for a description of the mask representation. 2383 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask, 2384 const Twine &Name = "") { 2385 if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask)) 2386 return V; 2387 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); 2388 } 2389 2390 /// Create a unary shuffle. The second vector operand of the IR instruction 2391 /// is poison. 2392 Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask, 2393 const Twine &Name = "") { 2394 return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name); 2395 } 2396 2397 Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs, 2398 const Twine &Name = "") { 2399 if (auto *V = Folder.FoldExtractValue(Agg, Idxs)) 2400 return V; 2401 return Insert(ExtractValueInst::Create(Agg, Idxs), Name); 2402 } 2403 2404 Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 2405 const Twine &Name = "") { 2406 if (auto *V = Folder.FoldInsertValue(Agg, Val, Idxs)) 2407 return V; 2408 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); 2409 } 2410 2411 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses, 2412 const Twine &Name = "") { 2413 return Insert(LandingPadInst::Create(Ty, NumClauses), Name); 2414 } 2415 2416 Value *CreateFreeze(Value *V, const Twine &Name = "") { 2417 return Insert(new FreezeInst(V), Name); 2418 } 2419 2420 //===--------------------------------------------------------------------===// 2421 // Utility creation methods 2422 //===--------------------------------------------------------------------===// 2423 2424 /// Return a boolean value testing if \p Arg == 0. 2425 Value *CreateIsNull(Value *Arg, const Twine &Name = "") { 2426 return CreateICmpEQ(Arg, ConstantInt::getNullValue(Arg->getType()), Name); 2427 } 2428 2429 /// Return a boolean value testing if \p Arg != 0. 2430 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { 2431 return CreateICmpNE(Arg, ConstantInt::getNullValue(Arg->getType()), Name); 2432 } 2433 2434 /// Return a boolean value testing if \p Arg < 0. 2435 Value *CreateIsNeg(Value *Arg, const Twine &Name = "") { 2436 return CreateICmpSLT(Arg, ConstantInt::getNullValue(Arg->getType()), Name); 2437 } 2438 2439 /// Return a boolean value testing if \p Arg > -1. 2440 Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") { 2441 return CreateICmpSGT(Arg, ConstantInt::getAllOnesValue(Arg->getType()), 2442 Name); 2443 } 2444 2445 /// Return the i64 difference between two pointer values, dividing out 2446 /// the size of the pointed-to objects. 2447 /// 2448 /// This is intended to implement C-style pointer subtraction. As such, the 2449 /// pointers must be appropriately aligned for their element types and 2450 /// pointing into the same object. 2451 Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS, 2452 const Twine &Name = ""); 2453 2454 /// Create a launder.invariant.group intrinsic call. If Ptr type is 2455 /// different from pointer to i8, it's casted to pointer to i8 in the same 2456 /// address space before call and casted back to Ptr type after call. 2457 Value *CreateLaunderInvariantGroup(Value *Ptr); 2458 2459 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is 2460 /// different from pointer to i8, it's casted to pointer to i8 in the same 2461 /// address space before call and casted back to Ptr type after call. 2462 Value *CreateStripInvariantGroup(Value *Ptr); 2463 2464 /// Return a vector value that contains the vector V reversed 2465 Value *CreateVectorReverse(Value *V, const Twine &Name = ""); 2466 2467 /// Return a vector splice intrinsic if using scalable vectors, otherwise 2468 /// return a shufflevector. If the immediate is positive, a vector is 2469 /// extracted from concat(V1, V2), starting at Imm. If the immediate 2470 /// is negative, we extract -Imm elements from V1 and the remaining 2471 /// elements from V2. Imm is a signed integer in the range 2472 /// -VL <= Imm < VL (where VL is the runtime vector length of the 2473 /// source/result vector) 2474 Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm, 2475 const Twine &Name = ""); 2476 2477 /// Return a vector value that contains \arg V broadcasted to \p 2478 /// NumElts elements. 2479 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = ""); 2480 2481 /// Return a vector value that contains \arg V broadcasted to \p 2482 /// EC elements. 2483 Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = ""); 2484 2485 /// Return a value that has been extracted from a larger integer type. 2486 Value *CreateExtractInteger(const DataLayout &DL, Value *From, 2487 IntegerType *ExtractedTy, uint64_t Offset, 2488 const Twine &Name); 2489 2490 Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base, 2491 unsigned Dimension, unsigned LastIndex, 2492 MDNode *DbgInfo); 2493 2494 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex, 2495 MDNode *DbgInfo); 2496 2497 Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base, 2498 unsigned Index, unsigned FieldIndex, 2499 MDNode *DbgInfo); 2500 2501 private: 2502 /// Helper function that creates an assume intrinsic call that 2503 /// represents an alignment assumption on the provided pointer \p PtrValue 2504 /// with offset \p OffsetValue and alignment value \p AlignValue. 2505 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL, 2506 Value *PtrValue, Value *AlignValue, 2507 Value *OffsetValue); 2508 2509 public: 2510 /// Create an assume intrinsic call that represents an alignment 2511 /// assumption on the provided pointer. 2512 /// 2513 /// An optional offset can be provided, and if it is provided, the offset 2514 /// must be subtracted from the provided pointer to get the pointer with the 2515 /// specified alignment. 2516 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, 2517 unsigned Alignment, 2518 Value *OffsetValue = nullptr); 2519 2520 /// Create an assume intrinsic call that represents an alignment 2521 /// assumption on the provided pointer. 2522 /// 2523 /// An optional offset can be provided, and if it is provided, the offset 2524 /// must be subtracted from the provided pointer to get the pointer with the 2525 /// specified alignment. 2526 /// 2527 /// This overload handles the condition where the Alignment is dependent 2528 /// on an existing value rather than a static value. 2529 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, 2530 Value *Alignment, 2531 Value *OffsetValue = nullptr); 2532 }; 2533 2534 /// This provides a uniform API for creating instructions and inserting 2535 /// them into a basic block: either at the end of a BasicBlock, or at a specific 2536 /// iterator location in a block. 2537 /// 2538 /// Note that the builder does not expose the full generality of LLVM 2539 /// instructions. For access to extra instruction properties, use the mutators 2540 /// (e.g. setVolatile) on the instructions after they have been 2541 /// created. Convenience state exists to specify fast-math flags and fp-math 2542 /// tags. 2543 /// 2544 /// The first template argument specifies a class to use for creating constants. 2545 /// This defaults to creating minimally folded constants. The second template 2546 /// argument allows clients to specify custom insertion hooks that are called on 2547 /// every newly created insertion. 2548 template <typename FolderTy = ConstantFolder, 2549 typename InserterTy = IRBuilderDefaultInserter> 2550 class IRBuilder : public IRBuilderBase { 2551 private: 2552 FolderTy Folder; 2553 InserterTy Inserter; 2554 2555 public: 2556 IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(), 2557 MDNode *FPMathTag = nullptr, 2558 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2559 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles), 2560 Folder(Folder), Inserter(Inserter) {} 2561 2562 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr, 2563 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2564 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {} 2565 2566 explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder, 2567 MDNode *FPMathTag = nullptr, 2568 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2569 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, 2570 FPMathTag, OpBundles), 2571 Folder(Folder) { 2572 SetInsertPoint(TheBB); 2573 } 2574 2575 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr, 2576 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2577 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, 2578 FPMathTag, OpBundles) { 2579 SetInsertPoint(TheBB); 2580 } 2581 2582 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr, 2583 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2584 : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag, 2585 OpBundles) { 2586 SetInsertPoint(IP); 2587 } 2588 2589 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder, 2590 MDNode *FPMathTag = nullptr, 2591 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2592 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, 2593 FPMathTag, OpBundles), 2594 Folder(Folder) { 2595 SetInsertPoint(TheBB, IP); 2596 } 2597 2598 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, 2599 MDNode *FPMathTag = nullptr, 2600 ArrayRef<OperandBundleDef> OpBundles = std::nullopt) 2601 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, 2602 FPMathTag, OpBundles) { 2603 SetInsertPoint(TheBB, IP); 2604 } 2605 2606 /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard 2607 /// or FastMathFlagGuard instead. 2608 IRBuilder(const IRBuilder &) = delete; 2609 getInserter()2610 InserterTy &getInserter() { return Inserter; } 2611 }; 2612 2613 template <typename FolderTy, typename InserterTy> 2614 IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, 2615 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>; 2616 IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>; 2617 template <typename FolderTy> 2618 IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>) 2619 -> IRBuilder<FolderTy>; 2620 IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>; 2621 IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>; 2622 template <typename FolderTy> 2623 IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *, 2624 ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>; 2625 IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *, 2626 ArrayRef<OperandBundleDef>) -> IRBuilder<>; 2627 2628 2629 // Create wrappers for C Binding types (see CBindingWrapping.h). 2630 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef) 2631 2632 } // end namespace llvm 2633 2634 #endif // LLVM_IR_IRBUILDER_H 2635