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/ADT/ArrayRef.h" 19 #include "llvm/ADT/None.h" 20 #include "llvm/ADT/SmallVector.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/Operator.h" 38 #include "llvm/IR/Type.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/IR/ValueHandle.h" 41 #include "llvm/Support/AtomicOrdering.h" 42 #include "llvm/Support/CBindingWrapping.h" 43 #include "llvm/Support/Casting.h" 44 #include "llvm-c/Types.h" 45 #include <cassert> 46 #include <cstddef> 47 #include <cstdint> 48 49 namespace llvm { 50 51 class APInt; 52 class MDNode; 53 class Module; 54 class Use; 55 56 /// \brief This provides the default implementation of the IRBuilder 57 /// 'InsertHelper' method that is called whenever an instruction is created by 58 /// IRBuilder and needs to be inserted. 59 /// 60 /// By default, this inserts the instruction at the insertion point. 61 class IRBuilderDefaultInserter { 62 protected: InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)63 void InsertHelper(Instruction *I, const Twine &Name, 64 BasicBlock *BB, BasicBlock::iterator InsertPt) const { 65 if (BB) BB->getInstList().insert(InsertPt, I); 66 I->setName(Name); 67 } 68 }; 69 70 /// \brief Common base class shared among various IRBuilders. 71 class IRBuilderBase { 72 DebugLoc CurDbgLocation; 73 74 protected: 75 BasicBlock *BB; 76 BasicBlock::iterator InsertPt; 77 LLVMContext &Context; 78 79 MDNode *DefaultFPMathTag; 80 FastMathFlags FMF; 81 82 ArrayRef<OperandBundleDef> DefaultOperandBundles; 83 84 public: 85 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr, 86 ArrayRef<OperandBundleDef> OpBundles = None) Context(context)87 : Context(context), DefaultFPMathTag(FPMathTag), FMF(), 88 DefaultOperandBundles(OpBundles) { 89 ClearInsertionPoint(); 90 } 91 92 //===--------------------------------------------------------------------===// 93 // Builder configuration methods 94 //===--------------------------------------------------------------------===// 95 96 /// \brief Clear the insertion point: created instructions will not be 97 /// inserted into a block. ClearInsertionPoint()98 void ClearInsertionPoint() { 99 BB = nullptr; 100 InsertPt.reset(nullptr); 101 } 102 GetInsertBlock()103 BasicBlock *GetInsertBlock() const { return BB; } GetInsertPoint()104 BasicBlock::iterator GetInsertPoint() const { return InsertPt; } getContext()105 LLVMContext &getContext() const { return Context; } 106 107 /// \brief This specifies that created instructions should be appended to the 108 /// end of the specified block. SetInsertPoint(BasicBlock * TheBB)109 void SetInsertPoint(BasicBlock *TheBB) { 110 BB = TheBB; 111 InsertPt = BB->end(); 112 } 113 114 /// \brief This specifies that created instructions should be inserted before 115 /// the specified instruction. SetInsertPoint(Instruction * I)116 void SetInsertPoint(Instruction *I) { 117 BB = I->getParent(); 118 InsertPt = I->getIterator(); 119 assert(InsertPt != BB->end() && "Can't read debug loc from end()"); 120 SetCurrentDebugLocation(I->getDebugLoc()); 121 } 122 123 /// \brief This specifies that created instructions should be inserted at the 124 /// specified point. SetInsertPoint(BasicBlock * TheBB,BasicBlock::iterator IP)125 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { 126 BB = TheBB; 127 InsertPt = IP; 128 if (IP != TheBB->end()) 129 SetCurrentDebugLocation(IP->getDebugLoc()); 130 } 131 132 /// \brief Set location information used by debugging information. SetCurrentDebugLocation(DebugLoc L)133 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); } 134 135 /// \brief Get location information used by debugging information. getCurrentDebugLocation()136 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; } 137 138 /// \brief If this builder has a current debug location, set it on the 139 /// specified instruction. SetInstDebugLocation(Instruction * I)140 void SetInstDebugLocation(Instruction *I) const { 141 if (CurDbgLocation) 142 I->setDebugLoc(CurDbgLocation); 143 } 144 145 /// \brief Get the return type of the current function that we're emitting 146 /// into. 147 Type *getCurrentFunctionReturnType() const; 148 149 /// InsertPoint - A saved insertion point. 150 class InsertPoint { 151 BasicBlock *Block; 152 BasicBlock::iterator Point; 153 154 public: 155 /// \brief Creates a new insertion point which doesn't point to anything. InsertPoint()156 InsertPoint() : Block(nullptr) {} 157 158 /// \brief Creates a new insertion point at the given location. InsertPoint(BasicBlock * InsertBlock,BasicBlock::iterator InsertPoint)159 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) 160 : Block(InsertBlock), Point(InsertPoint) {} 161 162 /// \brief Returns true if this insert point is set. isSet()163 bool isSet() const { return (Block != nullptr); } 164 getBlock()165 llvm::BasicBlock *getBlock() const { return Block; } getPoint()166 llvm::BasicBlock::iterator getPoint() const { return Point; } 167 }; 168 169 /// \brief Returns the current insert point. saveIP()170 InsertPoint saveIP() const { 171 return InsertPoint(GetInsertBlock(), GetInsertPoint()); 172 } 173 174 /// \brief Returns the current insert point, clearing it in the process. saveAndClearIP()175 InsertPoint saveAndClearIP() { 176 InsertPoint IP(GetInsertBlock(), GetInsertPoint()); 177 ClearInsertionPoint(); 178 return IP; 179 } 180 181 /// \brief Sets the current insert point to a previously-saved location. restoreIP(InsertPoint IP)182 void restoreIP(InsertPoint IP) { 183 if (IP.isSet()) 184 SetInsertPoint(IP.getBlock(), IP.getPoint()); 185 else 186 ClearInsertionPoint(); 187 } 188 189 /// \brief Get the floating point math metadata being used. getDefaultFPMathTag()190 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } 191 192 /// \brief Get the flags to be applied to created floating point ops getFastMathFlags()193 FastMathFlags getFastMathFlags() const { return FMF; } 194 195 /// \brief Clear the fast-math flags. clearFastMathFlags()196 void clearFastMathFlags() { FMF.clear(); } 197 198 /// \brief Set the floating point math metadata to be used. setDefaultFPMathTag(MDNode * FPMathTag)199 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } 200 201 /// \brief Set the fast-math flags to be used with generated fp-math operators setFastMathFlags(FastMathFlags NewFMF)202 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } 203 204 //===--------------------------------------------------------------------===// 205 // RAII helpers. 206 //===--------------------------------------------------------------------===// 207 208 // \brief RAII object that stores the current insertion point and restores it 209 // when the object is destroyed. This includes the debug location. 210 class InsertPointGuard { 211 IRBuilderBase &Builder; 212 AssertingVH<BasicBlock> Block; 213 BasicBlock::iterator Point; 214 DebugLoc DbgLoc; 215 216 InsertPointGuard(const InsertPointGuard &) = delete; 217 InsertPointGuard &operator=(const InsertPointGuard &) = delete; 218 219 public: InsertPointGuard(IRBuilderBase & B)220 InsertPointGuard(IRBuilderBase &B) 221 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), 222 DbgLoc(B.getCurrentDebugLocation()) {} 223 ~InsertPointGuard()224 ~InsertPointGuard() { 225 Builder.restoreIP(InsertPoint(Block, Point)); 226 Builder.SetCurrentDebugLocation(DbgLoc); 227 } 228 }; 229 230 // \brief RAII object that stores the current fast math settings and restores 231 // them when the object is destroyed. 232 class FastMathFlagGuard { 233 IRBuilderBase &Builder; 234 FastMathFlags FMF; 235 MDNode *FPMathTag; 236 237 FastMathFlagGuard(const FastMathFlagGuard &) = delete; 238 FastMathFlagGuard &operator=( 239 const FastMathFlagGuard &) = delete; 240 241 public: FastMathFlagGuard(IRBuilderBase & B)242 FastMathFlagGuard(IRBuilderBase &B) 243 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {} 244 ~FastMathFlagGuard()245 ~FastMathFlagGuard() { 246 Builder.FMF = FMF; 247 Builder.DefaultFPMathTag = FPMathTag; 248 } 249 }; 250 251 //===--------------------------------------------------------------------===// 252 // Miscellaneous creation methods. 253 //===--------------------------------------------------------------------===// 254 255 /// \brief Make a new global variable with initializer type i8* 256 /// 257 /// Make a new global variable with an initializer that has array of i8 type 258 /// filled in with the null terminated string value specified. The new global 259 /// variable will be marked mergable with any others of the same contents. If 260 /// Name is specified, it is the name of the global variable created. 261 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", 262 unsigned AddressSpace = 0); 263 264 /// \brief Get a constant value representing either true or false. getInt1(bool V)265 ConstantInt *getInt1(bool V) { 266 return ConstantInt::get(getInt1Ty(), V); 267 } 268 269 /// \brief Get the constant value for i1 true. getTrue()270 ConstantInt *getTrue() { 271 return ConstantInt::getTrue(Context); 272 } 273 274 /// \brief Get the constant value for i1 false. getFalse()275 ConstantInt *getFalse() { 276 return ConstantInt::getFalse(Context); 277 } 278 279 /// \brief Get a constant 8-bit value. getInt8(uint8_t C)280 ConstantInt *getInt8(uint8_t C) { 281 return ConstantInt::get(getInt8Ty(), C); 282 } 283 284 /// \brief Get a constant 16-bit value. getInt16(uint16_t C)285 ConstantInt *getInt16(uint16_t C) { 286 return ConstantInt::get(getInt16Ty(), C); 287 } 288 289 /// \brief Get a constant 32-bit value. getInt32(uint32_t C)290 ConstantInt *getInt32(uint32_t C) { 291 return ConstantInt::get(getInt32Ty(), C); 292 } 293 294 /// \brief Get a constant 64-bit value. getInt64(uint64_t C)295 ConstantInt *getInt64(uint64_t C) { 296 return ConstantInt::get(getInt64Ty(), C); 297 } 298 299 /// \brief Get a constant N-bit value, zero extended or truncated from 300 /// a 64-bit value. getIntN(unsigned N,uint64_t C)301 ConstantInt *getIntN(unsigned N, uint64_t C) { 302 return ConstantInt::get(getIntNTy(N), C); 303 } 304 305 /// \brief Get a constant integer value. getInt(const APInt & AI)306 ConstantInt *getInt(const APInt &AI) { 307 return ConstantInt::get(Context, AI); 308 } 309 310 //===--------------------------------------------------------------------===// 311 // Type creation methods 312 //===--------------------------------------------------------------------===// 313 314 /// \brief Fetch the type representing a single bit getInt1Ty()315 IntegerType *getInt1Ty() { 316 return Type::getInt1Ty(Context); 317 } 318 319 /// \brief Fetch the type representing an 8-bit integer. getInt8Ty()320 IntegerType *getInt8Ty() { 321 return Type::getInt8Ty(Context); 322 } 323 324 /// \brief Fetch the type representing a 16-bit integer. getInt16Ty()325 IntegerType *getInt16Ty() { 326 return Type::getInt16Ty(Context); 327 } 328 329 /// \brief Fetch the type representing a 32-bit integer. getInt32Ty()330 IntegerType *getInt32Ty() { 331 return Type::getInt32Ty(Context); 332 } 333 334 /// \brief Fetch the type representing a 64-bit integer. getInt64Ty()335 IntegerType *getInt64Ty() { 336 return Type::getInt64Ty(Context); 337 } 338 339 /// \brief Fetch the type representing a 128-bit integer. getInt128Ty()340 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); } 341 342 /// \brief Fetch the type representing an N-bit integer. getIntNTy(unsigned N)343 IntegerType *getIntNTy(unsigned N) { 344 return Type::getIntNTy(Context, N); 345 } 346 347 /// \brief Fetch the type representing a 16-bit floating point value. getHalfTy()348 Type *getHalfTy() { 349 return Type::getHalfTy(Context); 350 } 351 352 /// \brief Fetch the type representing a 32-bit floating point value. getFloatTy()353 Type *getFloatTy() { 354 return Type::getFloatTy(Context); 355 } 356 357 /// \brief Fetch the type representing a 64-bit floating point value. getDoubleTy()358 Type *getDoubleTy() { 359 return Type::getDoubleTy(Context); 360 } 361 362 /// \brief Fetch the type representing void. getVoidTy()363 Type *getVoidTy() { 364 return Type::getVoidTy(Context); 365 } 366 367 /// \brief Fetch the type representing a pointer to an 8-bit integer value. 368 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { 369 return Type::getInt8PtrTy(Context, AddrSpace); 370 } 371 372 /// \brief Fetch the type representing a pointer to an integer value. 373 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { 374 return DL.getIntPtrType(Context, AddrSpace); 375 } 376 377 //===--------------------------------------------------------------------===// 378 // Intrinsic creation methods 379 //===--------------------------------------------------------------------===// 380 381 /// \brief Create and insert a memset to the specified pointer and the 382 /// specified value. 383 /// 384 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is 385 /// specified, it will be added to the instruction. Likewise with alias.scope 386 /// and noalias tags. 387 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, 388 bool isVolatile = false, MDNode *TBAATag = nullptr, 389 MDNode *ScopeTag = nullptr, 390 MDNode *NoAliasTag = nullptr) { 391 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, 392 TBAATag, ScopeTag, NoAliasTag); 393 } 394 395 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, 396 bool isVolatile = false, MDNode *TBAATag = nullptr, 397 MDNode *ScopeTag = nullptr, 398 MDNode *NoAliasTag = nullptr); 399 400 /// \brief Create and insert a memcpy between the specified pointers. 401 /// 402 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 403 /// specified, it will be added to the instruction. Likewise with alias.scope 404 /// and noalias tags. 405 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align, 406 bool isVolatile = false, MDNode *TBAATag = nullptr, 407 MDNode *TBAAStructTag = nullptr, 408 MDNode *ScopeTag = nullptr, 409 MDNode *NoAliasTag = nullptr) { 410 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag, 411 TBAAStructTag, ScopeTag, NoAliasTag); 412 } 413 414 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, 415 bool isVolatile = false, MDNode *TBAATag = nullptr, 416 MDNode *TBAAStructTag = nullptr, 417 MDNode *ScopeTag = nullptr, 418 MDNode *NoAliasTag = nullptr); 419 420 /// \brief Create and insert a memmove between the specified 421 /// pointers. 422 /// 423 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 424 /// specified, it will be added to the instruction. Likewise with alias.scope 425 /// and noalias tags. 426 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align, 427 bool isVolatile = false, MDNode *TBAATag = nullptr, 428 MDNode *ScopeTag = nullptr, 429 MDNode *NoAliasTag = nullptr) { 430 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, 431 TBAATag, ScopeTag, NoAliasTag); 432 } 433 434 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align, 435 bool isVolatile = false, MDNode *TBAATag = nullptr, 436 MDNode *ScopeTag = nullptr, 437 MDNode *NoAliasTag = nullptr); 438 439 /// \brief Create a lifetime.start intrinsic. 440 /// 441 /// If the pointer isn't i8* it will be converted. 442 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr); 443 444 /// \brief Create a lifetime.end intrinsic. 445 /// 446 /// If the pointer isn't i8* it will be converted. 447 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr); 448 449 /// \brief Create a call to Masked Load intrinsic 450 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask, 451 Value *PassThru = nullptr, const Twine &Name = ""); 452 453 /// \brief Create a call to Masked Store intrinsic 454 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align, 455 Value *Mask); 456 457 /// \brief Create a call to Masked Gather intrinsic 458 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align, 459 Value *Mask = nullptr, 460 Value *PassThru = nullptr, 461 const Twine& Name = ""); 462 463 /// \brief Create a call to Masked Scatter intrinsic 464 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align, 465 Value *Mask = nullptr); 466 467 /// \brief Create an assume intrinsic call that allows the optimizer to 468 /// assume that the provided condition will be true. 469 CallInst *CreateAssumption(Value *Cond); 470 471 /// \brief Create a call to the experimental.gc.statepoint intrinsic to 472 /// start a new statepoint sequence. 473 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 474 Value *ActualCallee, 475 ArrayRef<Value *> CallArgs, 476 ArrayRef<Value *> DeoptArgs, 477 ArrayRef<Value *> GCArgs, 478 const Twine &Name = ""); 479 480 /// \brief Create a call to the experimental.gc.statepoint intrinsic to 481 /// start a new statepoint sequence. 482 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 483 Value *ActualCallee, uint32_t Flags, 484 ArrayRef<Use> CallArgs, 485 ArrayRef<Use> TransitionArgs, 486 ArrayRef<Use> DeoptArgs, 487 ArrayRef<Value *> GCArgs, 488 const Twine &Name = ""); 489 490 // \brief Conveninence function for the common case when CallArgs are filled 491 // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be 492 // .get()'ed to get the Value pointer. 493 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, 494 Value *ActualCallee, ArrayRef<Use> CallArgs, 495 ArrayRef<Value *> DeoptArgs, 496 ArrayRef<Value *> GCArgs, 497 const Twine &Name = ""); 498 499 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to 500 /// start a new statepoint sequence. 501 InvokeInst * 502 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, 503 Value *ActualInvokee, BasicBlock *NormalDest, 504 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs, 505 ArrayRef<Value *> DeoptArgs, 506 ArrayRef<Value *> GCArgs, const Twine &Name = ""); 507 508 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to 509 /// start a new statepoint sequence. 510 InvokeInst *CreateGCStatepointInvoke( 511 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, 512 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, 513 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs, 514 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs, 515 const Twine &Name = ""); 516 517 // Conveninence function for the common case when CallArgs are filled in using 518 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to 519 // get the Value *. 520 InvokeInst * 521 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, 522 Value *ActualInvokee, BasicBlock *NormalDest, 523 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, 524 ArrayRef<Value *> DeoptArgs, 525 ArrayRef<Value *> GCArgs, const Twine &Name = ""); 526 527 /// \brief Create a call to the experimental.gc.result intrinsic to extract 528 /// the result from a call wrapped in a statepoint. 529 CallInst *CreateGCResult(Instruction *Statepoint, 530 Type *ResultType, 531 const Twine &Name = ""); 532 533 /// \brief Create a call to the experimental.gc.relocate intrinsics to 534 /// project the relocated value of one pointer from the statepoint. 535 CallInst *CreateGCRelocate(Instruction *Statepoint, 536 int BaseOffset, 537 int DerivedOffset, 538 Type *ResultType, 539 const Twine &Name = ""); 540 541 private: 542 /// \brief Create a call to a masked intrinsic with given Id. 543 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops, 544 ArrayRef<Type *> OverloadedTypes, 545 const Twine &Name = ""); 546 547 Value *getCastedInt8PtrValue(Value *Ptr); 548 }; 549 550 /// \brief This provides a uniform API for creating instructions and inserting 551 /// them into a basic block: either at the end of a BasicBlock, or at a specific 552 /// iterator location in a block. 553 /// 554 /// Note that the builder does not expose the full generality of LLVM 555 /// instructions. For access to extra instruction properties, use the mutators 556 /// (e.g. setVolatile) on the instructions after they have been 557 /// created. Convenience state exists to specify fast-math flags and fp-math 558 /// tags. 559 /// 560 /// The first template argument specifies a class to use for creating constants. 561 /// This defaults to creating minimally folded constants. The second template 562 /// argument allows clients to specify custom insertion hooks that are called on 563 /// every newly created insertion. 564 template <typename T = ConstantFolder, 565 typename Inserter = IRBuilderDefaultInserter> 566 class IRBuilder : public IRBuilderBase, public Inserter { 567 T Folder; 568 569 public: 570 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(), 571 MDNode *FPMathTag = nullptr, 572 ArrayRef<OperandBundleDef> OpBundles = None) IRBuilderBase(C,FPMathTag,OpBundles)573 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)), 574 Folder(F) {} 575 576 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr, 577 ArrayRef<OperandBundleDef> OpBundles = None) IRBuilderBase(C,FPMathTag,OpBundles)578 : IRBuilderBase(C, FPMathTag, OpBundles), Folder() {} 579 580 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr, 581 ArrayRef<OperandBundleDef> OpBundles = None) 582 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) { 583 SetInsertPoint(TheBB); 584 } 585 586 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr, 587 ArrayRef<OperandBundleDef> OpBundles = None) 588 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() { 589 SetInsertPoint(TheBB); 590 } 591 592 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr, 593 ArrayRef<OperandBundleDef> OpBundles = None) 594 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles), Folder() { 595 SetInsertPoint(IP); 596 } 597 598 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F, 599 MDNode *FPMathTag = nullptr, 600 ArrayRef<OperandBundleDef> OpBundles = None) 601 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) { 602 SetInsertPoint(TheBB, IP); 603 } 604 605 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, 606 MDNode *FPMathTag = nullptr, 607 ArrayRef<OperandBundleDef> OpBundles = None) 608 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() { 609 SetInsertPoint(TheBB, IP); 610 } 611 612 /// \brief Get the constant folder being used. getFolder()613 const T &getFolder() { return Folder; } 614 615 /// \brief Insert and return the specified instruction. 616 template<typename InstTy> 617 InstTy *Insert(InstTy *I, const Twine &Name = "") const { 618 this->InsertHelper(I, Name, BB, InsertPt); 619 this->SetInstDebugLocation(I); 620 return I; 621 } 622 623 /// \brief No-op overload to handle constants. 624 Constant *Insert(Constant *C, const Twine& = "") const { 625 return C; 626 } 627 628 //===--------------------------------------------------------------------===// 629 // Instruction creation methods: Terminators 630 //===--------------------------------------------------------------------===// 631 632 private: 633 /// \brief Helper to add branch weight and unpredictable metadata onto an 634 /// instruction. 635 /// \returns The annotated instruction. 636 template <typename InstTy> addBranchMetadata(InstTy * I,MDNode * Weights,MDNode * Unpredictable)637 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) { 638 if (Weights) 639 I->setMetadata(LLVMContext::MD_prof, Weights); 640 if (Unpredictable) 641 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable); 642 return I; 643 } 644 645 public: 646 /// \brief Create a 'ret void' instruction. CreateRetVoid()647 ReturnInst *CreateRetVoid() { 648 return Insert(ReturnInst::Create(Context)); 649 } 650 651 /// \brief Create a 'ret <val>' instruction. CreateRet(Value * V)652 ReturnInst *CreateRet(Value *V) { 653 return Insert(ReturnInst::Create(Context, V)); 654 } 655 656 /// \brief Create a sequence of N insertvalue instructions, 657 /// with one Value from the retVals array each, that build a aggregate 658 /// return value one value at a time, and a ret instruction to return 659 /// the resulting aggregate value. 660 /// 661 /// This is a convenience function for code that uses aggregate return values 662 /// as a vehicle for having multiple return values. CreateAggregateRet(Value * const * retVals,unsigned N)663 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { 664 Value *V = UndefValue::get(getCurrentFunctionReturnType()); 665 for (unsigned i = 0; i != N; ++i) 666 V = CreateInsertValue(V, retVals[i], i, "mrv"); 667 return Insert(ReturnInst::Create(Context, V)); 668 } 669 670 /// \brief Create an unconditional 'br label X' instruction. CreateBr(BasicBlock * Dest)671 BranchInst *CreateBr(BasicBlock *Dest) { 672 return Insert(BranchInst::Create(Dest)); 673 } 674 675 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest' 676 /// instruction. 677 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, 678 MDNode *BranchWeights = nullptr, 679 MDNode *Unpredictable = nullptr) { 680 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond), 681 BranchWeights, Unpredictable)); 682 } 683 684 /// \brief Create a switch instruction with the specified value, default dest, 685 /// and with a hint for the number of cases that will be added (for efficient 686 /// allocation). 687 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, 688 MDNode *BranchWeights = nullptr, 689 MDNode *Unpredictable = nullptr) { 690 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases), 691 BranchWeights, Unpredictable)); 692 } 693 694 /// \brief Create an indirect branch instruction with the specified address 695 /// operand, with an optional hint for the number of destinations that will be 696 /// added (for efficient allocation). 697 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { 698 return Insert(IndirectBrInst::Create(Addr, NumDests)); 699 } 700 701 /// \brief Create an invoke instruction. 702 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 703 BasicBlock *UnwindDest, 704 ArrayRef<Value *> Args = None, 705 const Twine &Name = "") { 706 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args), 707 Name); 708 } 709 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 710 BasicBlock *UnwindDest, ArrayRef<Value *> Args, 711 ArrayRef<OperandBundleDef> OpBundles, 712 const Twine &Name = "") { 713 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args, 714 OpBundles), Name); 715 } 716 CreateResume(Value * Exn)717 ResumeInst *CreateResume(Value *Exn) { 718 return Insert(ResumeInst::Create(Exn)); 719 } 720 721 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad, 722 BasicBlock *UnwindBB = nullptr) { 723 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB)); 724 } 725 726 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, 727 unsigned NumHandlers, 728 const Twine &Name = "") { 729 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers), 730 Name); 731 } 732 733 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args, 734 const Twine &Name = "") { 735 return Insert(CatchPadInst::Create(ParentPad, Args), Name); 736 } 737 738 CleanupPadInst *CreateCleanupPad(Value *ParentPad, 739 ArrayRef<Value *> Args = None, 740 const Twine &Name = "") { 741 return Insert(CleanupPadInst::Create(ParentPad, Args), Name); 742 } 743 CreateCatchRet(CatchPadInst * CatchPad,BasicBlock * BB)744 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) { 745 return Insert(CatchReturnInst::Create(CatchPad, BB)); 746 } 747 CreateUnreachable()748 UnreachableInst *CreateUnreachable() { 749 return Insert(new UnreachableInst(Context)); 750 } 751 752 //===--------------------------------------------------------------------===// 753 // Instruction creation methods: Binary Operators 754 //===--------------------------------------------------------------------===// 755 private: CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,Value * LHS,Value * RHS,const Twine & Name,bool HasNUW,bool HasNSW)756 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, 757 Value *LHS, Value *RHS, 758 const Twine &Name, 759 bool HasNUW, bool HasNSW) { 760 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); 761 if (HasNUW) BO->setHasNoUnsignedWrap(); 762 if (HasNSW) BO->setHasNoSignedWrap(); 763 return BO; 764 } 765 AddFPMathAttributes(Instruction * I,MDNode * FPMathTag,FastMathFlags FMF)766 Instruction *AddFPMathAttributes(Instruction *I, 767 MDNode *FPMathTag, 768 FastMathFlags FMF) const { 769 if (!FPMathTag) 770 FPMathTag = DefaultFPMathTag; 771 if (FPMathTag) 772 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag); 773 I->setFastMathFlags(FMF); 774 return I; 775 } 776 777 public: 778 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", 779 bool HasNUW = false, bool HasNSW = false) { 780 if (Constant *LC = dyn_cast<Constant>(LHS)) 781 if (Constant *RC = dyn_cast<Constant>(RHS)) 782 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name); 783 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, 784 HasNUW, HasNSW); 785 } 786 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 787 return CreateAdd(LHS, RHS, Name, false, true); 788 } 789 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 790 return CreateAdd(LHS, RHS, Name, true, false); 791 } 792 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "", 793 MDNode *FPMathTag = nullptr) { 794 if (Constant *LC = dyn_cast<Constant>(LHS)) 795 if (Constant *RC = dyn_cast<Constant>(RHS)) 796 return Insert(Folder.CreateFAdd(LC, RC), Name); 797 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS), 798 FPMathTag, FMF), Name); 799 } 800 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", 801 bool HasNUW = false, bool HasNSW = false) { 802 if (Constant *LC = dyn_cast<Constant>(LHS)) 803 if (Constant *RC = dyn_cast<Constant>(RHS)) 804 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name); 805 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, 806 HasNUW, HasNSW); 807 } 808 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 809 return CreateSub(LHS, RHS, Name, false, true); 810 } 811 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 812 return CreateSub(LHS, RHS, Name, true, false); 813 } 814 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "", 815 MDNode *FPMathTag = nullptr) { 816 if (Constant *LC = dyn_cast<Constant>(LHS)) 817 if (Constant *RC = dyn_cast<Constant>(RHS)) 818 return Insert(Folder.CreateFSub(LC, RC), Name); 819 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS), 820 FPMathTag, FMF), Name); 821 } 822 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", 823 bool HasNUW = false, bool HasNSW = false) { 824 if (Constant *LC = dyn_cast<Constant>(LHS)) 825 if (Constant *RC = dyn_cast<Constant>(RHS)) 826 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name); 827 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, 828 HasNUW, HasNSW); 829 } 830 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 831 return CreateMul(LHS, RHS, Name, false, true); 832 } 833 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 834 return CreateMul(LHS, RHS, Name, true, false); 835 } 836 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "", 837 MDNode *FPMathTag = nullptr) { 838 if (Constant *LC = dyn_cast<Constant>(LHS)) 839 if (Constant *RC = dyn_cast<Constant>(RHS)) 840 return Insert(Folder.CreateFMul(LC, RC), Name); 841 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS), 842 FPMathTag, FMF), Name); 843 } 844 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", 845 bool isExact = false) { 846 if (Constant *LC = dyn_cast<Constant>(LHS)) 847 if (Constant *RC = dyn_cast<Constant>(RHS)) 848 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name); 849 if (!isExact) 850 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); 851 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); 852 } 853 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 854 return CreateUDiv(LHS, RHS, Name, true); 855 } 856 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", 857 bool isExact = false) { 858 if (Constant *LC = dyn_cast<Constant>(LHS)) 859 if (Constant *RC = dyn_cast<Constant>(RHS)) 860 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name); 861 if (!isExact) 862 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); 863 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); 864 } 865 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 866 return CreateSDiv(LHS, RHS, Name, true); 867 } 868 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "", 869 MDNode *FPMathTag = nullptr) { 870 if (Constant *LC = dyn_cast<Constant>(LHS)) 871 if (Constant *RC = dyn_cast<Constant>(RHS)) 872 return Insert(Folder.CreateFDiv(LC, RC), Name); 873 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS), 874 FPMathTag, FMF), Name); 875 } 876 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { 877 if (Constant *LC = dyn_cast<Constant>(LHS)) 878 if (Constant *RC = dyn_cast<Constant>(RHS)) 879 return Insert(Folder.CreateURem(LC, RC), Name); 880 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); 881 } 882 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { 883 if (Constant *LC = dyn_cast<Constant>(LHS)) 884 if (Constant *RC = dyn_cast<Constant>(RHS)) 885 return Insert(Folder.CreateSRem(LC, RC), Name); 886 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); 887 } 888 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "", 889 MDNode *FPMathTag = nullptr) { 890 if (Constant *LC = dyn_cast<Constant>(LHS)) 891 if (Constant *RC = dyn_cast<Constant>(RHS)) 892 return Insert(Folder.CreateFRem(LC, RC), Name); 893 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS), 894 FPMathTag, FMF), Name); 895 } 896 897 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", 898 bool HasNUW = false, bool HasNSW = false) { 899 if (Constant *LC = dyn_cast<Constant>(LHS)) 900 if (Constant *RC = dyn_cast<Constant>(RHS)) 901 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name); 902 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, 903 HasNUW, HasNSW); 904 } 905 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", 906 bool HasNUW = false, bool HasNSW = false) { 907 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 908 HasNUW, HasNSW); 909 } 910 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", 911 bool HasNUW = false, bool HasNSW = false) { 912 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 913 HasNUW, HasNSW); 914 } 915 916 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", 917 bool isExact = false) { 918 if (Constant *LC = dyn_cast<Constant>(LHS)) 919 if (Constant *RC = dyn_cast<Constant>(RHS)) 920 return Insert(Folder.CreateLShr(LC, RC, isExact), Name); 921 if (!isExact) 922 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); 923 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); 924 } 925 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 926 bool isExact = false) { 927 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 928 } 929 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 930 bool isExact = false) { 931 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 932 } 933 934 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", 935 bool isExact = false) { 936 if (Constant *LC = dyn_cast<Constant>(LHS)) 937 if (Constant *RC = dyn_cast<Constant>(RHS)) 938 return Insert(Folder.CreateAShr(LC, RC, isExact), Name); 939 if (!isExact) 940 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); 941 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); 942 } 943 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 944 bool isExact = false) { 945 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 946 } 947 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 948 bool isExact = false) { 949 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 950 } 951 952 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { 953 if (Constant *RC = dyn_cast<Constant>(RHS)) { 954 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue()) 955 return LHS; // LHS & -1 -> LHS 956 if (Constant *LC = dyn_cast<Constant>(LHS)) 957 return Insert(Folder.CreateAnd(LC, RC), Name); 958 } 959 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); 960 } 961 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { 962 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 963 } 964 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { 965 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 966 } 967 968 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { 969 if (Constant *RC = dyn_cast<Constant>(RHS)) { 970 if (RC->isNullValue()) 971 return LHS; // LHS | 0 -> LHS 972 if (Constant *LC = dyn_cast<Constant>(LHS)) 973 return Insert(Folder.CreateOr(LC, RC), Name); 974 } 975 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); 976 } 977 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { 978 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 979 } 980 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { 981 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 982 } 983 984 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { 985 if (Constant *LC = dyn_cast<Constant>(LHS)) 986 if (Constant *RC = dyn_cast<Constant>(RHS)) 987 return Insert(Folder.CreateXor(LC, RC), Name); 988 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); 989 } 990 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { 991 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 992 } 993 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { 994 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 995 } 996 997 Value *CreateBinOp(Instruction::BinaryOps Opc, 998 Value *LHS, Value *RHS, const Twine &Name = "", 999 MDNode *FPMathTag = nullptr) { 1000 if (Constant *LC = dyn_cast<Constant>(LHS)) 1001 if (Constant *RC = dyn_cast<Constant>(RHS)) 1002 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name); 1003 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); 1004 if (isa<FPMathOperator>(BinOp)) 1005 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF); 1006 return Insert(BinOp, Name); 1007 } 1008 1009 Value *CreateNeg(Value *V, const Twine &Name = "", 1010 bool HasNUW = false, bool HasNSW = false) { 1011 if (Constant *VC = dyn_cast<Constant>(V)) 1012 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name); 1013 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name); 1014 if (HasNUW) BO->setHasNoUnsignedWrap(); 1015 if (HasNSW) BO->setHasNoSignedWrap(); 1016 return BO; 1017 } 1018 Value *CreateNSWNeg(Value *V, const Twine &Name = "") { 1019 return CreateNeg(V, Name, false, true); 1020 } 1021 Value *CreateNUWNeg(Value *V, const Twine &Name = "") { 1022 return CreateNeg(V, Name, true, false); 1023 } 1024 Value *CreateFNeg(Value *V, const Twine &Name = "", 1025 MDNode *FPMathTag = nullptr) { 1026 if (Constant *VC = dyn_cast<Constant>(V)) 1027 return Insert(Folder.CreateFNeg(VC), Name); 1028 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V), 1029 FPMathTag, FMF), Name); 1030 } 1031 Value *CreateNot(Value *V, const Twine &Name = "") { 1032 if (Constant *VC = dyn_cast<Constant>(V)) 1033 return Insert(Folder.CreateNot(VC), Name); 1034 return Insert(BinaryOperator::CreateNot(V), Name); 1035 } 1036 1037 //===--------------------------------------------------------------------===// 1038 // Instruction creation methods: Memory Instructions 1039 //===--------------------------------------------------------------------===// 1040 1041 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, 1042 const Twine &Name = "") { 1043 return Insert(new AllocaInst(Ty, ArraySize), Name); 1044 } 1045 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of 1046 // converting the string to 'bool' for the isVolatile parameter. CreateLoad(Value * Ptr,const char * Name)1047 LoadInst *CreateLoad(Value *Ptr, const char *Name) { 1048 return Insert(new LoadInst(Ptr), Name); 1049 } 1050 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") { 1051 return Insert(new LoadInst(Ptr), Name); 1052 } 1053 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") { 1054 return Insert(new LoadInst(Ty, Ptr), Name); 1055 } 1056 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") { 1057 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name); 1058 } 1059 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { 1060 return Insert(new StoreInst(Val, Ptr, isVolatile)); 1061 } 1062 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' 1063 // correctly, instead of converting the string to 'bool' for the isVolatile 1064 // parameter. CreateAlignedLoad(Value * Ptr,unsigned Align,const char * Name)1065 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) { 1066 LoadInst *LI = CreateLoad(Ptr, Name); 1067 LI->setAlignment(Align); 1068 return LI; 1069 } 1070 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, 1071 const Twine &Name = "") { 1072 LoadInst *LI = CreateLoad(Ptr, Name); 1073 LI->setAlignment(Align); 1074 return LI; 1075 } 1076 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile, 1077 const Twine &Name = "") { 1078 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name); 1079 LI->setAlignment(Align); 1080 return LI; 1081 } 1082 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, 1083 bool isVolatile = false) { 1084 StoreInst *SI = CreateStore(Val, Ptr, isVolatile); 1085 SI->setAlignment(Align); 1086 return SI; 1087 } 1088 FenceInst *CreateFence(AtomicOrdering Ordering, 1089 SynchronizationScope SynchScope = CrossThread, 1090 const Twine &Name = "") { 1091 return Insert(new FenceInst(Context, Ordering, SynchScope), Name); 1092 } 1093 AtomicCmpXchgInst * 1094 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, 1095 AtomicOrdering SuccessOrdering, 1096 AtomicOrdering FailureOrdering, 1097 SynchronizationScope SynchScope = CrossThread) { 1098 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, 1099 FailureOrdering, SynchScope)); 1100 } 1101 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, 1102 AtomicOrdering Ordering, 1103 SynchronizationScope SynchScope = CrossThread) { 1104 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope)); 1105 } 1106 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList, 1107 const Twine &Name = "") { 1108 return CreateGEP(nullptr, Ptr, IdxList, Name); 1109 } 1110 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, 1111 const Twine &Name = "") { 1112 if (Constant *PC = dyn_cast<Constant>(Ptr)) { 1113 // Every index must be constant. 1114 size_t i, e; 1115 for (i = 0, e = IdxList.size(); i != e; ++i) 1116 if (!isa<Constant>(IdxList[i])) 1117 break; 1118 if (i == e) 1119 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name); 1120 } 1121 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name); 1122 } 1123 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, 1124 const Twine &Name = "") { 1125 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name); 1126 } 1127 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, 1128 const Twine &Name = "") { 1129 if (Constant *PC = dyn_cast<Constant>(Ptr)) { 1130 // Every index must be constant. 1131 size_t i, e; 1132 for (i = 0, e = IdxList.size(); i != e; ++i) 1133 if (!isa<Constant>(IdxList[i])) 1134 break; 1135 if (i == e) 1136 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList), 1137 Name); 1138 } 1139 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name); 1140 } 1141 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { 1142 return CreateGEP(nullptr, Ptr, Idx, Name); 1143 } 1144 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") { 1145 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1146 if (Constant *IC = dyn_cast<Constant>(Idx)) 1147 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name); 1148 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); 1149 } 1150 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx, 1151 const Twine &Name = "") { 1152 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1153 if (Constant *IC = dyn_cast<Constant>(Idx)) 1154 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name); 1155 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); 1156 } 1157 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") { 1158 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name); 1159 } 1160 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, 1161 const Twine &Name = "") { 1162 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 1163 1164 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1165 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name); 1166 1167 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); 1168 } 1169 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, 1170 const Twine &Name = "") { 1171 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 1172 1173 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1174 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name); 1175 1176 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); 1177 } 1178 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, 1179 const Twine &Name = "") { 1180 Value *Idxs[] = { 1181 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 1182 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 1183 }; 1184 1185 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1186 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name); 1187 1188 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); 1189 } 1190 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, 1191 unsigned Idx1, const Twine &Name = "") { 1192 Value *Idxs[] = { 1193 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 1194 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 1195 }; 1196 1197 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1198 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name); 1199 1200 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); 1201 } 1202 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") { 1203 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 1204 1205 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1206 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name); 1207 1208 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name); 1209 } 1210 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0, 1211 const Twine &Name = "") { 1212 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 1213 1214 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1215 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name); 1216 1217 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name); 1218 } 1219 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, 1220 const Twine &Name = "") { 1221 Value *Idxs[] = { 1222 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 1223 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 1224 }; 1225 1226 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1227 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name); 1228 1229 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name); 1230 } 1231 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, 1232 const Twine &Name = "") { 1233 Value *Idxs[] = { 1234 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 1235 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 1236 }; 1237 1238 if (Constant *PC = dyn_cast<Constant>(Ptr)) 1239 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs), 1240 Name); 1241 1242 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name); 1243 } 1244 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, 1245 const Twine &Name = "") { 1246 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name); 1247 } 1248 1249 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type 1250 /// instead of a pointer to array of i8. 1251 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", 1252 unsigned AddressSpace = 0) { 1253 GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace); 1254 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0); 1255 Value *Args[] = { zero, zero }; 1256 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name); 1257 } 1258 1259 //===--------------------------------------------------------------------===// 1260 // Instruction creation methods: Cast/Conversion Operators 1261 //===--------------------------------------------------------------------===// 1262 1263 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { 1264 return CreateCast(Instruction::Trunc, V, DestTy, Name); 1265 } 1266 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { 1267 return CreateCast(Instruction::ZExt, V, DestTy, Name); 1268 } 1269 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { 1270 return CreateCast(Instruction::SExt, V, DestTy, Name); 1271 } 1272 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return 1273 /// the value untouched if the type of V is already DestTy. 1274 Value *CreateZExtOrTrunc(Value *V, Type *DestTy, 1275 const Twine &Name = "") { 1276 assert(V->getType()->isIntOrIntVectorTy() && 1277 DestTy->isIntOrIntVectorTy() && 1278 "Can only zero extend/truncate integers!"); 1279 Type *VTy = V->getType(); 1280 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 1281 return CreateZExt(V, DestTy, Name); 1282 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 1283 return CreateTrunc(V, DestTy, Name); 1284 return V; 1285 } 1286 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return 1287 /// the value untouched if the type of V is already DestTy. 1288 Value *CreateSExtOrTrunc(Value *V, Type *DestTy, 1289 const Twine &Name = "") { 1290 assert(V->getType()->isIntOrIntVectorTy() && 1291 DestTy->isIntOrIntVectorTy() && 1292 "Can only sign extend/truncate integers!"); 1293 Type *VTy = V->getType(); 1294 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 1295 return CreateSExt(V, DestTy, Name); 1296 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 1297 return CreateTrunc(V, DestTy, Name); 1298 return V; 1299 } 1300 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){ 1301 return CreateCast(Instruction::FPToUI, V, DestTy, Name); 1302 } 1303 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){ 1304 return CreateCast(Instruction::FPToSI, V, DestTy, Name); 1305 } 1306 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 1307 return CreateCast(Instruction::UIToFP, V, DestTy, Name); 1308 } 1309 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 1310 return CreateCast(Instruction::SIToFP, V, DestTy, Name); 1311 } 1312 Value *CreateFPTrunc(Value *V, Type *DestTy, 1313 const Twine &Name = "") { 1314 return CreateCast(Instruction::FPTrunc, V, DestTy, Name); 1315 } 1316 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { 1317 return CreateCast(Instruction::FPExt, V, DestTy, Name); 1318 } 1319 Value *CreatePtrToInt(Value *V, Type *DestTy, 1320 const Twine &Name = "") { 1321 return CreateCast(Instruction::PtrToInt, V, DestTy, Name); 1322 } 1323 Value *CreateIntToPtr(Value *V, Type *DestTy, 1324 const Twine &Name = "") { 1325 return CreateCast(Instruction::IntToPtr, V, DestTy, Name); 1326 } 1327 Value *CreateBitCast(Value *V, Type *DestTy, 1328 const Twine &Name = "") { 1329 return CreateCast(Instruction::BitCast, V, DestTy, Name); 1330 } 1331 Value *CreateAddrSpaceCast(Value *V, Type *DestTy, 1332 const Twine &Name = "") { 1333 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name); 1334 } 1335 Value *CreateZExtOrBitCast(Value *V, Type *DestTy, 1336 const Twine &Name = "") { 1337 if (V->getType() == DestTy) 1338 return V; 1339 if (Constant *VC = dyn_cast<Constant>(V)) 1340 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); 1341 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); 1342 } 1343 Value *CreateSExtOrBitCast(Value *V, Type *DestTy, 1344 const Twine &Name = "") { 1345 if (V->getType() == DestTy) 1346 return V; 1347 if (Constant *VC = dyn_cast<Constant>(V)) 1348 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); 1349 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); 1350 } 1351 Value *CreateTruncOrBitCast(Value *V, Type *DestTy, 1352 const Twine &Name = "") { 1353 if (V->getType() == DestTy) 1354 return V; 1355 if (Constant *VC = dyn_cast<Constant>(V)) 1356 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); 1357 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); 1358 } 1359 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, 1360 const Twine &Name = "") { 1361 if (V->getType() == DestTy) 1362 return V; 1363 if (Constant *VC = dyn_cast<Constant>(V)) 1364 return Insert(Folder.CreateCast(Op, VC, DestTy), Name); 1365 return Insert(CastInst::Create(Op, V, DestTy), Name); 1366 } 1367 Value *CreatePointerCast(Value *V, Type *DestTy, 1368 const Twine &Name = "") { 1369 if (V->getType() == DestTy) 1370 return V; 1371 if (Constant *VC = dyn_cast<Constant>(V)) 1372 return Insert(Folder.CreatePointerCast(VC, DestTy), Name); 1373 return Insert(CastInst::CreatePointerCast(V, DestTy), Name); 1374 } 1375 1376 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, 1377 const Twine &Name = "") { 1378 if (V->getType() == DestTy) 1379 return V; 1380 1381 if (Constant *VC = dyn_cast<Constant>(V)) { 1382 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy), 1383 Name); 1384 } 1385 1386 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy), 1387 Name); 1388 } 1389 1390 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, 1391 const Twine &Name = "") { 1392 if (V->getType() == DestTy) 1393 return V; 1394 if (Constant *VC = dyn_cast<Constant>(V)) 1395 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); 1396 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); 1397 } 1398 1399 Value *CreateBitOrPointerCast(Value *V, Type *DestTy, 1400 const Twine &Name = "") { 1401 if (V->getType() == DestTy) 1402 return V; 1403 if (V->getType()->getScalarType()->isPointerTy() && 1404 DestTy->getScalarType()->isIntegerTy()) 1405 return CreatePtrToInt(V, DestTy, Name); 1406 if (V->getType()->getScalarType()->isIntegerTy() && 1407 DestTy->getScalarType()->isPointerTy()) 1408 return CreateIntToPtr(V, DestTy, Name); 1409 1410 return CreateBitCast(V, DestTy, Name); 1411 } 1412 1413 private: 1414 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a 1415 // compile time error, instead of converting the string to bool for the 1416 // isSigned parameter. 1417 Value *CreateIntCast(Value *, Type *, const char *) = delete; 1418 1419 public: 1420 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { 1421 if (V->getType() == DestTy) 1422 return V; 1423 if (Constant *VC = dyn_cast<Constant>(V)) 1424 return Insert(Folder.CreateFPCast(VC, DestTy), Name); 1425 return Insert(CastInst::CreateFPCast(V, DestTy), Name); 1426 } 1427 1428 //===--------------------------------------------------------------------===// 1429 // Instruction creation methods: Compare Instructions 1430 //===--------------------------------------------------------------------===// 1431 1432 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { 1433 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); 1434 } 1435 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { 1436 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); 1437 } 1438 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { 1439 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); 1440 } 1441 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { 1442 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); 1443 } 1444 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { 1445 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); 1446 } 1447 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { 1448 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); 1449 } 1450 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { 1451 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); 1452 } 1453 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { 1454 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); 1455 } 1456 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { 1457 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); 1458 } 1459 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { 1460 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); 1461 } 1462 1463 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "", 1464 MDNode *FPMathTag = nullptr) { 1465 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag); 1466 } 1467 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "", 1468 MDNode *FPMathTag = nullptr) { 1469 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag); 1470 } 1471 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "", 1472 MDNode *FPMathTag = nullptr) { 1473 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag); 1474 } 1475 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "", 1476 MDNode *FPMathTag = nullptr) { 1477 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag); 1478 } 1479 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "", 1480 MDNode *FPMathTag = nullptr) { 1481 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag); 1482 } 1483 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "", 1484 MDNode *FPMathTag = nullptr) { 1485 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag); 1486 } 1487 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "", 1488 MDNode *FPMathTag = nullptr) { 1489 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag); 1490 } 1491 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "", 1492 MDNode *FPMathTag = nullptr) { 1493 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag); 1494 } 1495 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "", 1496 MDNode *FPMathTag = nullptr) { 1497 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag); 1498 } 1499 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "", 1500 MDNode *FPMathTag = nullptr) { 1501 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag); 1502 } 1503 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "", 1504 MDNode *FPMathTag = nullptr) { 1505 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag); 1506 } 1507 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "", 1508 MDNode *FPMathTag = nullptr) { 1509 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag); 1510 } 1511 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "", 1512 MDNode *FPMathTag = nullptr) { 1513 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag); 1514 } 1515 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "", 1516 MDNode *FPMathTag = nullptr) { 1517 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag); 1518 } 1519 1520 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 1521 const Twine &Name = "") { 1522 if (Constant *LC = dyn_cast<Constant>(LHS)) 1523 if (Constant *RC = dyn_cast<Constant>(RHS)) 1524 return Insert(Folder.CreateICmp(P, LC, RC), Name); 1525 return Insert(new ICmpInst(P, LHS, RHS), Name); 1526 } 1527 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 1528 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 1529 if (Constant *LC = dyn_cast<Constant>(LHS)) 1530 if (Constant *RC = dyn_cast<Constant>(RHS)) 1531 return Insert(Folder.CreateFCmp(P, LC, RC), Name); 1532 return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS), 1533 FPMathTag, FMF), Name); 1534 } 1535 1536 //===--------------------------------------------------------------------===// 1537 // Instruction creation methods: Other Instructions 1538 //===--------------------------------------------------------------------===// 1539 1540 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, 1541 const Twine &Name = "") { 1542 return Insert(PHINode::Create(Ty, NumReservedValues), Name); 1543 } 1544 1545 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None, 1546 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 1547 PointerType *PTy = cast<PointerType>(Callee->getType()); 1548 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1549 return CreateCall(FTy, Callee, Args, Name, FPMathTag); 1550 } 1551 1552 CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee, 1553 ArrayRef<Value *> Args, const Twine &Name = "", 1554 MDNode *FPMathTag = nullptr) { 1555 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); 1556 if (isa<FPMathOperator>(CI)) 1557 CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF)); 1558 return Insert(CI, Name); 1559 } 1560 1561 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args, 1562 ArrayRef<OperandBundleDef> OpBundles, 1563 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 1564 CallInst *CI = CallInst::Create(Callee, Args, OpBundles); 1565 if (isa<FPMathOperator>(CI)) 1566 CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF)); 1567 return Insert(CI, Name); 1568 } 1569 1570 CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args, 1571 const Twine &Name = "", MDNode *FPMathTag = nullptr) { 1572 return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag); 1573 } 1574 1575 Value *CreateSelect(Value *C, Value *True, Value *False, 1576 const Twine &Name = "", Instruction *MDFrom = nullptr) { 1577 if (Constant *CC = dyn_cast<Constant>(C)) 1578 if (Constant *TC = dyn_cast<Constant>(True)) 1579 if (Constant *FC = dyn_cast<Constant>(False)) 1580 return Insert(Folder.CreateSelect(CC, TC, FC), Name); 1581 1582 SelectInst *Sel = SelectInst::Create(C, True, False); 1583 if (MDFrom) { 1584 MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof); 1585 MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable); 1586 Sel = addBranchMetadata(Sel, Prof, Unpred); 1587 } 1588 return Insert(Sel, Name); 1589 } 1590 1591 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { 1592 return Insert(new VAArgInst(List, Ty), Name); 1593 } 1594 1595 Value *CreateExtractElement(Value *Vec, Value *Idx, 1596 const Twine &Name = "") { 1597 if (Constant *VC = dyn_cast<Constant>(Vec)) 1598 if (Constant *IC = dyn_cast<Constant>(Idx)) 1599 return Insert(Folder.CreateExtractElement(VC, IC), Name); 1600 return Insert(ExtractElementInst::Create(Vec, Idx), Name); 1601 } 1602 1603 Value *CreateExtractElement(Value *Vec, uint64_t Idx, 1604 const Twine &Name = "") { 1605 return CreateExtractElement(Vec, getInt64(Idx), Name); 1606 } 1607 1608 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, 1609 const Twine &Name = "") { 1610 if (Constant *VC = dyn_cast<Constant>(Vec)) 1611 if (Constant *NC = dyn_cast<Constant>(NewElt)) 1612 if (Constant *IC = dyn_cast<Constant>(Idx)) 1613 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name); 1614 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); 1615 } 1616 1617 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, 1618 const Twine &Name = "") { 1619 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name); 1620 } 1621 1622 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, 1623 const Twine &Name = "") { 1624 if (Constant *V1C = dyn_cast<Constant>(V1)) 1625 if (Constant *V2C = dyn_cast<Constant>(V2)) 1626 if (Constant *MC = dyn_cast<Constant>(Mask)) 1627 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name); 1628 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); 1629 } 1630 1631 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask, 1632 const Twine &Name = "") { 1633 Value *Mask = ConstantDataVector::get(Context, IntMask); 1634 return CreateShuffleVector(V1, V2, Mask, Name); 1635 } 1636 1637 Value *CreateExtractValue(Value *Agg, 1638 ArrayRef<unsigned> Idxs, 1639 const Twine &Name = "") { 1640 if (Constant *AggC = dyn_cast<Constant>(Agg)) 1641 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name); 1642 return Insert(ExtractValueInst::Create(Agg, Idxs), Name); 1643 } 1644 1645 Value *CreateInsertValue(Value *Agg, Value *Val, 1646 ArrayRef<unsigned> Idxs, 1647 const Twine &Name = "") { 1648 if (Constant *AggC = dyn_cast<Constant>(Agg)) 1649 if (Constant *ValC = dyn_cast<Constant>(Val)) 1650 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name); 1651 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); 1652 } 1653 1654 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses, 1655 const Twine &Name = "") { 1656 return Insert(LandingPadInst::Create(Ty, NumClauses), Name); 1657 } 1658 1659 //===--------------------------------------------------------------------===// 1660 // Utility creation methods 1661 //===--------------------------------------------------------------------===// 1662 1663 /// \brief Return an i1 value testing if \p Arg is null. 1664 Value *CreateIsNull(Value *Arg, const Twine &Name = "") { 1665 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), 1666 Name); 1667 } 1668 1669 /// \brief Return an i1 value testing if \p Arg is not null. 1670 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { 1671 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), 1672 Name); 1673 } 1674 1675 /// \brief Return the i64 difference between two pointer values, dividing out 1676 /// the size of the pointed-to objects. 1677 /// 1678 /// This is intended to implement C-style pointer subtraction. As such, the 1679 /// pointers must be appropriately aligned for their element types and 1680 /// pointing into the same object. 1681 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") { 1682 assert(LHS->getType() == RHS->getType() && 1683 "Pointer subtraction operand types must match!"); 1684 PointerType *ArgType = cast<PointerType>(LHS->getType()); 1685 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context)); 1686 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context)); 1687 Value *Difference = CreateSub(LHS_int, RHS_int); 1688 return CreateExactSDiv(Difference, 1689 ConstantExpr::getSizeOf(ArgType->getElementType()), 1690 Name); 1691 } 1692 1693 /// \brief Create an invariant.group.barrier intrinsic call, that stops 1694 /// optimizer to propagate equality using invariant.group metadata. 1695 /// If Ptr type is different from i8*, it's casted to i8* before call 1696 /// and casted back to Ptr type after call. CreateInvariantGroupBarrier(Value * Ptr)1697 Value *CreateInvariantGroupBarrier(Value *Ptr) { 1698 Module *M = BB->getParent()->getParent(); 1699 Function *FnInvariantGroupBarrier = Intrinsic::getDeclaration(M, 1700 Intrinsic::invariant_group_barrier); 1701 1702 Type *ArgumentAndReturnType = FnInvariantGroupBarrier->getReturnType(); 1703 assert(ArgumentAndReturnType == 1704 FnInvariantGroupBarrier->getFunctionType()->getParamType(0) && 1705 "InvariantGroupBarrier should take and return the same type"); 1706 Type *PtrType = Ptr->getType(); 1707 1708 bool PtrTypeConversionNeeded = PtrType != ArgumentAndReturnType; 1709 if (PtrTypeConversionNeeded) 1710 Ptr = CreateBitCast(Ptr, ArgumentAndReturnType); 1711 1712 CallInst *Fn = CreateCall(FnInvariantGroupBarrier, {Ptr}); 1713 1714 if (PtrTypeConversionNeeded) 1715 return CreateBitCast(Fn, PtrType); 1716 return Fn; 1717 } 1718 1719 /// \brief Return a vector value that contains \arg V broadcasted to \p 1720 /// NumElts elements. 1721 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") { 1722 assert(NumElts > 0 && "Cannot splat to an empty vector!"); 1723 1724 // First insert it into an undef vector so we can shuffle it. 1725 Type *I32Ty = getInt32Ty(); 1726 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts)); 1727 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0), 1728 Name + ".splatinsert"); 1729 1730 // Shuffle the value across the desired number of elements. 1731 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts)); 1732 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat"); 1733 } 1734 1735 /// \brief 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)1736 Value *CreateExtractInteger(const DataLayout &DL, Value *From, 1737 IntegerType *ExtractedTy, uint64_t Offset, 1738 const Twine &Name) { 1739 IntegerType *IntTy = cast<IntegerType>(From->getType()); 1740 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <= 1741 DL.getTypeStoreSize(IntTy) && 1742 "Element extends past full value"); 1743 uint64_t ShAmt = 8 * Offset; 1744 Value *V = From; 1745 if (DL.isBigEndian()) 1746 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - 1747 DL.getTypeStoreSize(ExtractedTy) - Offset); 1748 if (ShAmt) { 1749 V = CreateLShr(V, ShAmt, Name + ".shift"); 1750 } 1751 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() && 1752 "Cannot extract to a larger integer!"); 1753 if (ExtractedTy != IntTy) { 1754 V = CreateTrunc(V, ExtractedTy, Name + ".trunc"); 1755 } 1756 return V; 1757 } 1758 1759 /// \brief Create an assume intrinsic call that represents an alignment 1760 /// assumption on the provided pointer. 1761 /// 1762 /// An optional offset can be provided, and if it is provided, the offset 1763 /// must be subtracted from the provided pointer to get the pointer with the 1764 /// specified alignment. 1765 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, 1766 unsigned Alignment, 1767 Value *OffsetValue = nullptr) { 1768 assert(isa<PointerType>(PtrValue->getType()) && 1769 "trying to create an alignment assumption on a non-pointer?"); 1770 1771 PointerType *PtrTy = cast<PointerType>(PtrValue->getType()); 1772 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace()); 1773 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint"); 1774 1775 Value *Mask = ConstantInt::get(IntPtrTy, 1776 Alignment > 0 ? Alignment - 1 : 0); 1777 if (OffsetValue) { 1778 bool IsOffsetZero = false; 1779 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue)) 1780 IsOffsetZero = CI->isZero(); 1781 1782 if (!IsOffsetZero) { 1783 if (OffsetValue->getType() != IntPtrTy) 1784 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true, 1785 "offsetcast"); 1786 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr"); 1787 } 1788 } 1789 1790 Value *Zero = ConstantInt::get(IntPtrTy, 0); 1791 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr"); 1792 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond"); 1793 1794 return CreateAssumption(InvCond); 1795 } 1796 }; 1797 1798 // Create wrappers for C Binding types (see CBindingWrapping.h). 1799 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef) 1800 1801 } // end namespace llvm 1802 1803 #endif // LLVM_IR_IRBUILDER_H 1804