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