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