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