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