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