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