LLVM API Documentation
00001 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the IRBuilder class, which is used as a convenient way 00011 // to create LLVM instructions with a consistent and simplified interface. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_IR_IRBUILDER_H 00016 #define LLVM_IR_IRBUILDER_H 00017 00018 #include "llvm/ADT/ArrayRef.h" 00019 #include "llvm/ADT/StringRef.h" 00020 #include "llvm/ADT/Twine.h" 00021 #include "llvm/IR/BasicBlock.h" 00022 #include "llvm/IR/DataLayout.h" 00023 #include "llvm/IR/Instructions.h" 00024 #include "llvm/IR/LLVMContext.h" 00025 #include "llvm/IR/Operator.h" 00026 #include "llvm/Support/CBindingWrapping.h" 00027 #include "llvm/Support/ConstantFolder.h" 00028 00029 namespace llvm { 00030 class MDNode; 00031 00032 /// \brief This provides the default implementation of the IRBuilder 00033 /// 'InsertHelper' method that is called whenever an instruction is created by 00034 /// IRBuilder and needs to be inserted. 00035 /// 00036 /// By default, this inserts the instruction at the insertion point. 00037 template <bool preserveNames = true> 00038 class IRBuilderDefaultInserter { 00039 protected: 00040 void InsertHelper(Instruction *I, const Twine &Name, 00041 BasicBlock *BB, BasicBlock::iterator InsertPt) const { 00042 if (BB) BB->getInstList().insert(InsertPt, I); 00043 if (preserveNames) 00044 I->setName(Name); 00045 } 00046 }; 00047 00048 /// \brief Common base class shared among various IRBuilders. 00049 class IRBuilderBase { 00050 DebugLoc CurDbgLocation; 00051 protected: 00052 /// Save the current debug location here while we are suppressing 00053 /// line table entries. 00054 llvm::DebugLoc SavedDbgLocation; 00055 00056 BasicBlock *BB; 00057 BasicBlock::iterator InsertPt; 00058 LLVMContext &Context; 00059 public: 00060 00061 IRBuilderBase(LLVMContext &context) 00062 : Context(context) { 00063 ClearInsertionPoint(); 00064 } 00065 00066 //===--------------------------------------------------------------------===// 00067 // Builder configuration methods 00068 //===--------------------------------------------------------------------===// 00069 00070 /// \brief Clear the insertion point: created instructions will not be 00071 /// inserted into a block. 00072 void ClearInsertionPoint() { 00073 BB = 0; 00074 } 00075 00076 BasicBlock *GetInsertBlock() const { return BB; } 00077 BasicBlock::iterator GetInsertPoint() const { return InsertPt; } 00078 LLVMContext &getContext() const { return Context; } 00079 00080 /// \brief This specifies that created instructions should be appended to the 00081 /// end of the specified block. 00082 void SetInsertPoint(BasicBlock *TheBB) { 00083 BB = TheBB; 00084 InsertPt = BB->end(); 00085 } 00086 00087 /// \brief This specifies that created instructions should be inserted before 00088 /// the specified instruction. 00089 void SetInsertPoint(Instruction *I) { 00090 BB = I->getParent(); 00091 InsertPt = I; 00092 SetCurrentDebugLocation(I->getDebugLoc()); 00093 } 00094 00095 /// \brief This specifies that created instructions should be inserted at the 00096 /// specified point. 00097 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { 00098 BB = TheBB; 00099 InsertPt = IP; 00100 } 00101 00102 /// \brief Find the nearest point that dominates this use, and specify that 00103 /// created instructions should be inserted at this point. 00104 void SetInsertPoint(Use &U) { 00105 Instruction *UseInst = cast<Instruction>(U.getUser()); 00106 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) { 00107 BasicBlock *PredBB = Phi->getIncomingBlock(U); 00108 assert(U != PredBB->getTerminator() && "critical edge not split"); 00109 SetInsertPoint(PredBB, PredBB->getTerminator()); 00110 return; 00111 } 00112 SetInsertPoint(UseInst); 00113 } 00114 00115 /// \brief Set location information used by debugging information. 00116 void SetCurrentDebugLocation(const DebugLoc &L) { 00117 CurDbgLocation = L; 00118 } 00119 00120 /// \brief Temporarily suppress DebugLocations from being attached 00121 /// to emitted instructions, until the next call to 00122 /// SetCurrentDebugLocation() or EnableDebugLocations(). Use this 00123 /// if you want an instruction to be counted towards the prologue or 00124 /// if there is no useful source location. 00125 void DisableDebugLocations() { 00126 llvm::DebugLoc Empty; 00127 SavedDbgLocation = getCurrentDebugLocation(); 00128 SetCurrentDebugLocation(Empty); 00129 } 00130 00131 /// \brief Restore the previously saved DebugLocation. 00132 void EnableDebugLocations() { 00133 assert(CurDbgLocation.isUnknown()); 00134 SetCurrentDebugLocation(SavedDbgLocation); 00135 } 00136 00137 /// \brief Get location information used by debugging information. 00138 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; } 00139 00140 /// \brief If this builder has a current debug location, set it on the 00141 /// specified instruction. 00142 void SetInstDebugLocation(Instruction *I) const { 00143 if (!CurDbgLocation.isUnknown()) 00144 I->setDebugLoc(CurDbgLocation); 00145 } 00146 00147 /// \brief Get the return type of the current function that we're emitting 00148 /// into. 00149 Type *getCurrentFunctionReturnType() const; 00150 00151 /// InsertPoint - A saved insertion point. 00152 class InsertPoint { 00153 BasicBlock *Block; 00154 BasicBlock::iterator Point; 00155 00156 public: 00157 /// \brief Creates a new insertion point which doesn't point to anything. 00158 InsertPoint() : Block(0) {} 00159 00160 /// \brief Creates a new insertion point at the given location. 00161 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) 00162 : Block(InsertBlock), Point(InsertPoint) {} 00163 00164 /// \brief Returns true if this insert point is set. 00165 bool isSet() const { return (Block != 0); } 00166 00167 llvm::BasicBlock *getBlock() const { return Block; } 00168 llvm::BasicBlock::iterator getPoint() const { return Point; } 00169 }; 00170 00171 /// \brief Returns the current insert point. 00172 InsertPoint saveIP() const { 00173 return InsertPoint(GetInsertBlock(), GetInsertPoint()); 00174 } 00175 00176 /// \brief Returns the current insert point, clearing it in the process. 00177 InsertPoint saveAndClearIP() { 00178 InsertPoint IP(GetInsertBlock(), GetInsertPoint()); 00179 ClearInsertionPoint(); 00180 return IP; 00181 } 00182 00183 /// \brief Sets the current insert point to a previously-saved location. 00184 void restoreIP(InsertPoint IP) { 00185 if (IP.isSet()) 00186 SetInsertPoint(IP.getBlock(), IP.getPoint()); 00187 else 00188 ClearInsertionPoint(); 00189 } 00190 00191 //===--------------------------------------------------------------------===// 00192 // Miscellaneous creation methods. 00193 //===--------------------------------------------------------------------===// 00194 00195 /// \brief Make a new global variable with initializer type i8* 00196 /// 00197 /// Make a new global variable with an initializer that has array of i8 type 00198 /// filled in with the null terminated string value specified. The new global 00199 /// variable will be marked mergable with any others of the same contents. If 00200 /// Name is specified, it is the name of the global variable created. 00201 Value *CreateGlobalString(StringRef Str, const Twine &Name = ""); 00202 00203 /// \brief Get a constant value representing either true or false. 00204 ConstantInt *getInt1(bool V) { 00205 return ConstantInt::get(getInt1Ty(), V); 00206 } 00207 00208 /// \brief Get the constant value for i1 true. 00209 ConstantInt *getTrue() { 00210 return ConstantInt::getTrue(Context); 00211 } 00212 00213 /// \brief Get the constant value for i1 false. 00214 ConstantInt *getFalse() { 00215 return ConstantInt::getFalse(Context); 00216 } 00217 00218 /// \brief Get a constant 8-bit value. 00219 ConstantInt *getInt8(uint8_t C) { 00220 return ConstantInt::get(getInt8Ty(), C); 00221 } 00222 00223 /// \brief Get a constant 16-bit value. 00224 ConstantInt *getInt16(uint16_t C) { 00225 return ConstantInt::get(getInt16Ty(), C); 00226 } 00227 00228 /// \brief Get a constant 32-bit value. 00229 ConstantInt *getInt32(uint32_t C) { 00230 return ConstantInt::get(getInt32Ty(), C); 00231 } 00232 00233 /// \brief Get a constant 64-bit value. 00234 ConstantInt *getInt64(uint64_t C) { 00235 return ConstantInt::get(getInt64Ty(), C); 00236 } 00237 00238 /// \brief Get a constant integer value. 00239 ConstantInt *getInt(const APInt &AI) { 00240 return ConstantInt::get(Context, AI); 00241 } 00242 00243 //===--------------------------------------------------------------------===// 00244 // Type creation methods 00245 //===--------------------------------------------------------------------===// 00246 00247 /// \brief Fetch the type representing a single bit 00248 IntegerType *getInt1Ty() { 00249 return Type::getInt1Ty(Context); 00250 } 00251 00252 /// \brief Fetch the type representing an 8-bit integer. 00253 IntegerType *getInt8Ty() { 00254 return Type::getInt8Ty(Context); 00255 } 00256 00257 /// \brief Fetch the type representing a 16-bit integer. 00258 IntegerType *getInt16Ty() { 00259 return Type::getInt16Ty(Context); 00260 } 00261 00262 /// \brief Fetch the type representing a 32-bit integer. 00263 IntegerType *getInt32Ty() { 00264 return Type::getInt32Ty(Context); 00265 } 00266 00267 /// \brief Fetch the type representing a 64-bit integer. 00268 IntegerType *getInt64Ty() { 00269 return Type::getInt64Ty(Context); 00270 } 00271 00272 /// \brief Fetch the type representing a 32-bit floating point value. 00273 Type *getFloatTy() { 00274 return Type::getFloatTy(Context); 00275 } 00276 00277 /// \brief Fetch the type representing a 64-bit floating point value. 00278 Type *getDoubleTy() { 00279 return Type::getDoubleTy(Context); 00280 } 00281 00282 /// \brief Fetch the type representing void. 00283 Type *getVoidTy() { 00284 return Type::getVoidTy(Context); 00285 } 00286 00287 /// \brief Fetch the type representing a pointer to an 8-bit integer value. 00288 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { 00289 return Type::getInt8PtrTy(Context, AddrSpace); 00290 } 00291 00292 /// \brief Fetch the type representing a pointer to an integer value. 00293 IntegerType* getIntPtrTy(DataLayout *DL, unsigned AddrSpace = 0) { 00294 return DL->getIntPtrType(Context, AddrSpace); 00295 } 00296 00297 //===--------------------------------------------------------------------===// 00298 // Intrinsic creation methods 00299 //===--------------------------------------------------------------------===// 00300 00301 /// \brief Create and insert a memset to the specified pointer and the 00302 /// specified value. 00303 /// 00304 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is 00305 /// specified, it will be added to the instruction. 00306 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, 00307 bool isVolatile = false, MDNode *TBAATag = 0) { 00308 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag); 00309 } 00310 00311 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, 00312 bool isVolatile = false, MDNode *TBAATag = 0); 00313 00314 /// \brief Create and insert a memcpy between the specified pointers. 00315 /// 00316 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 00317 /// specified, it will be added to the instruction. 00318 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align, 00319 bool isVolatile = false, MDNode *TBAATag = 0, 00320 MDNode *TBAAStructTag = 0) { 00321 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag, 00322 TBAAStructTag); 00323 } 00324 00325 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, 00326 bool isVolatile = false, MDNode *TBAATag = 0, 00327 MDNode *TBAAStructTag = 0); 00328 00329 /// \brief Create and insert a memmove between the specified 00330 /// pointers. 00331 /// 00332 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is 00333 /// specified, it will be added to the instruction. 00334 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align, 00335 bool isVolatile = false, MDNode *TBAATag = 0) { 00336 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag); 00337 } 00338 00339 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align, 00340 bool isVolatile = false, MDNode *TBAATag = 0); 00341 00342 /// \brief Create a lifetime.start intrinsic. 00343 /// 00344 /// If the pointer isn't i8* it will be converted. 00345 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0); 00346 00347 /// \brief Create a lifetime.end intrinsic. 00348 /// 00349 /// If the pointer isn't i8* it will be converted. 00350 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0); 00351 00352 private: 00353 Value *getCastedInt8PtrValue(Value *Ptr); 00354 }; 00355 00356 /// \brief This provides a uniform API for creating instructions and inserting 00357 /// them into a basic block: either at the end of a BasicBlock, or at a specific 00358 /// iterator location in a block. 00359 /// 00360 /// Note that the builder does not expose the full generality of LLVM 00361 /// instructions. For access to extra instruction properties, use the mutators 00362 /// (e.g. setVolatile) on the instructions after they have been 00363 /// created. Convenience state exists to specify fast-math flags and fp-math 00364 /// tags. 00365 /// 00366 /// The first template argument handles whether or not to preserve names in the 00367 /// final instruction output. This defaults to on. The second template argument 00368 /// specifies a class to use for creating constants. This defaults to creating 00369 /// minimally folded constants. The fourth template argument allows clients to 00370 /// specify custom insertion hooks that are called on every newly created 00371 /// insertion. 00372 template<bool preserveNames = true, typename T = ConstantFolder, 00373 typename Inserter = IRBuilderDefaultInserter<preserveNames> > 00374 class IRBuilder : public IRBuilderBase, public Inserter { 00375 T Folder; 00376 MDNode *DefaultFPMathTag; 00377 FastMathFlags FMF; 00378 public: 00379 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(), 00380 MDNode *FPMathTag = 0) 00381 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag), 00382 FMF() { 00383 } 00384 00385 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) 00386 : IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() { 00387 } 00388 00389 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0) 00390 : IRBuilderBase(TheBB->getContext()), Folder(F), 00391 DefaultFPMathTag(FPMathTag), FMF() { 00392 SetInsertPoint(TheBB); 00393 } 00394 00395 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0) 00396 : IRBuilderBase(TheBB->getContext()), Folder(), 00397 DefaultFPMathTag(FPMathTag), FMF() { 00398 SetInsertPoint(TheBB); 00399 } 00400 00401 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0) 00402 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag), 00403 FMF() { 00404 SetInsertPoint(IP); 00405 SetCurrentDebugLocation(IP->getDebugLoc()); 00406 } 00407 00408 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0) 00409 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag), 00410 FMF() { 00411 SetInsertPoint(U); 00412 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc()); 00413 } 00414 00415 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F, 00416 MDNode *FPMathTag = 0) 00417 : IRBuilderBase(TheBB->getContext()), Folder(F), 00418 DefaultFPMathTag(FPMathTag), FMF() { 00419 SetInsertPoint(TheBB, IP); 00420 } 00421 00422 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0) 00423 : IRBuilderBase(TheBB->getContext()), Folder(), 00424 DefaultFPMathTag(FPMathTag), FMF() { 00425 SetInsertPoint(TheBB, IP); 00426 } 00427 00428 /// \brief Get the constant folder being used. 00429 const T &getFolder() { return Folder; } 00430 00431 /// \brief Get the floating point math metadata being used. 00432 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } 00433 00434 /// \brief Get the flags to be applied to created floating point ops 00435 FastMathFlags getFastMathFlags() const { return FMF; } 00436 00437 /// \brief Clear the fast-math flags. 00438 void clearFastMathFlags() { FMF.clear(); } 00439 00440 /// \brief SetDefaultFPMathTag - Set the floating point math metadata to be used. 00441 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } 00442 00443 /// \brief Set the fast-math flags to be used with generated fp-math operators 00444 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } 00445 00446 /// \brief Return true if this builder is configured to actually add the 00447 /// requested names to IR created through it. 00448 bool isNamePreserving() const { return preserveNames; } 00449 00450 /// \brief Insert and return the specified instruction. 00451 template<typename InstTy> 00452 InstTy *Insert(InstTy *I, const Twine &Name = "") const { 00453 this->InsertHelper(I, Name, BB, InsertPt); 00454 this->SetInstDebugLocation(I); 00455 return I; 00456 } 00457 00458 /// \brief No-op overload to handle constants. 00459 Constant *Insert(Constant *C, const Twine& = "") const { 00460 return C; 00461 } 00462 00463 //===--------------------------------------------------------------------===// 00464 // Instruction creation methods: Terminators 00465 //===--------------------------------------------------------------------===// 00466 00467 private: 00468 /// \brief Helper to add branch weight metadata onto an instruction. 00469 /// \returns The annotated instruction. 00470 template <typename InstTy> 00471 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) { 00472 if (Weights) 00473 I->setMetadata(LLVMContext::MD_prof, Weights); 00474 return I; 00475 } 00476 00477 public: 00478 /// \brief Create a 'ret void' instruction. 00479 ReturnInst *CreateRetVoid() { 00480 return Insert(ReturnInst::Create(Context)); 00481 } 00482 00483 /// \brief Create a 'ret <val>' instruction. 00484 ReturnInst *CreateRet(Value *V) { 00485 return Insert(ReturnInst::Create(Context, V)); 00486 } 00487 00488 /// \brief Create a sequence of N insertvalue instructions, 00489 /// with one Value from the retVals array each, that build a aggregate 00490 /// return value one value at a time, and a ret instruction to return 00491 /// the resulting aggregate value. 00492 /// 00493 /// This is a convenience function for code that uses aggregate return values 00494 /// as a vehicle for having multiple return values. 00495 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { 00496 Value *V = UndefValue::get(getCurrentFunctionReturnType()); 00497 for (unsigned i = 0; i != N; ++i) 00498 V = CreateInsertValue(V, retVals[i], i, "mrv"); 00499 return Insert(ReturnInst::Create(Context, V)); 00500 } 00501 00502 /// \brief Create an unconditional 'br label X' instruction. 00503 BranchInst *CreateBr(BasicBlock *Dest) { 00504 return Insert(BranchInst::Create(Dest)); 00505 } 00506 00507 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest' 00508 /// instruction. 00509 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, 00510 MDNode *BranchWeights = 0) { 00511 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond), 00512 BranchWeights)); 00513 } 00514 00515 /// \brief Create a switch instruction with the specified value, default dest, 00516 /// and with a hint for the number of cases that will be added (for efficient 00517 /// allocation). 00518 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, 00519 MDNode *BranchWeights = 0) { 00520 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases), 00521 BranchWeights)); 00522 } 00523 00524 /// \brief Create an indirect branch instruction with the specified address 00525 /// operand, with an optional hint for the number of destinations that will be 00526 /// added (for efficient allocation). 00527 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { 00528 return Insert(IndirectBrInst::Create(Addr, NumDests)); 00529 } 00530 00531 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 00532 BasicBlock *UnwindDest, const Twine &Name = "") { 00533 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, 00534 ArrayRef<Value *>()), 00535 Name); 00536 } 00537 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 00538 BasicBlock *UnwindDest, Value *Arg1, 00539 const Twine &Name = "") { 00540 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1), 00541 Name); 00542 } 00543 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest, 00544 BasicBlock *UnwindDest, Value *Arg1, 00545 Value *Arg2, Value *Arg3, 00546 const Twine &Name = "") { 00547 Value *Args[] = { Arg1, Arg2, Arg3 }; 00548 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args), 00549 Name); 00550 } 00551 /// \brief Create an invoke instruction. 00552 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 00553 BasicBlock *UnwindDest, ArrayRef<Value *> Args, 00554 const Twine &Name = "") { 00555 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args), 00556 Name); 00557 } 00558 00559 ResumeInst *CreateResume(Value *Exn) { 00560 return Insert(ResumeInst::Create(Exn)); 00561 } 00562 00563 UnreachableInst *CreateUnreachable() { 00564 return Insert(new UnreachableInst(Context)); 00565 } 00566 00567 //===--------------------------------------------------------------------===// 00568 // Instruction creation methods: Binary Operators 00569 //===--------------------------------------------------------------------===// 00570 private: 00571 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, 00572 Value *LHS, Value *RHS, 00573 const Twine &Name, 00574 bool HasNUW, bool HasNSW) { 00575 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); 00576 if (HasNUW) BO->setHasNoUnsignedWrap(); 00577 if (HasNSW) BO->setHasNoSignedWrap(); 00578 return BO; 00579 } 00580 00581 Instruction *AddFPMathAttributes(Instruction *I, 00582 MDNode *FPMathTag, 00583 FastMathFlags FMF) const { 00584 if (!FPMathTag) 00585 FPMathTag = DefaultFPMathTag; 00586 if (FPMathTag) 00587 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag); 00588 I->setFastMathFlags(FMF); 00589 return I; 00590 } 00591 public: 00592 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", 00593 bool HasNUW = false, bool HasNSW = false) { 00594 if (Constant *LC = dyn_cast<Constant>(LHS)) 00595 if (Constant *RC = dyn_cast<Constant>(RHS)) 00596 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name); 00597 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, 00598 HasNUW, HasNSW); 00599 } 00600 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 00601 return CreateAdd(LHS, RHS, Name, false, true); 00602 } 00603 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { 00604 return CreateAdd(LHS, RHS, Name, true, false); 00605 } 00606 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "", 00607 MDNode *FPMathTag = 0) { 00608 if (Constant *LC = dyn_cast<Constant>(LHS)) 00609 if (Constant *RC = dyn_cast<Constant>(RHS)) 00610 return Insert(Folder.CreateFAdd(LC, RC), Name); 00611 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS), 00612 FPMathTag, FMF), Name); 00613 } 00614 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", 00615 bool HasNUW = false, bool HasNSW = false) { 00616 if (Constant *LC = dyn_cast<Constant>(LHS)) 00617 if (Constant *RC = dyn_cast<Constant>(RHS)) 00618 return Insert(Folder.CreateSub(LC, RC), Name); 00619 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, 00620 HasNUW, HasNSW); 00621 } 00622 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 00623 return CreateSub(LHS, RHS, Name, false, true); 00624 } 00625 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { 00626 return CreateSub(LHS, RHS, Name, true, false); 00627 } 00628 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "", 00629 MDNode *FPMathTag = 0) { 00630 if (Constant *LC = dyn_cast<Constant>(LHS)) 00631 if (Constant *RC = dyn_cast<Constant>(RHS)) 00632 return Insert(Folder.CreateFSub(LC, RC), Name); 00633 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS), 00634 FPMathTag, FMF), Name); 00635 } 00636 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", 00637 bool HasNUW = false, bool HasNSW = false) { 00638 if (Constant *LC = dyn_cast<Constant>(LHS)) 00639 if (Constant *RC = dyn_cast<Constant>(RHS)) 00640 return Insert(Folder.CreateMul(LC, RC), Name); 00641 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, 00642 HasNUW, HasNSW); 00643 } 00644 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 00645 return CreateMul(LHS, RHS, Name, false, true); 00646 } 00647 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { 00648 return CreateMul(LHS, RHS, Name, true, false); 00649 } 00650 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "", 00651 MDNode *FPMathTag = 0) { 00652 if (Constant *LC = dyn_cast<Constant>(LHS)) 00653 if (Constant *RC = dyn_cast<Constant>(RHS)) 00654 return Insert(Folder.CreateFMul(LC, RC), Name); 00655 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS), 00656 FPMathTag, FMF), Name); 00657 } 00658 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", 00659 bool isExact = false) { 00660 if (Constant *LC = dyn_cast<Constant>(LHS)) 00661 if (Constant *RC = dyn_cast<Constant>(RHS)) 00662 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name); 00663 if (!isExact) 00664 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); 00665 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); 00666 } 00667 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 00668 return CreateUDiv(LHS, RHS, Name, true); 00669 } 00670 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", 00671 bool isExact = false) { 00672 if (Constant *LC = dyn_cast<Constant>(LHS)) 00673 if (Constant *RC = dyn_cast<Constant>(RHS)) 00674 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name); 00675 if (!isExact) 00676 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); 00677 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); 00678 } 00679 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { 00680 return CreateSDiv(LHS, RHS, Name, true); 00681 } 00682 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "", 00683 MDNode *FPMathTag = 0) { 00684 if (Constant *LC = dyn_cast<Constant>(LHS)) 00685 if (Constant *RC = dyn_cast<Constant>(RHS)) 00686 return Insert(Folder.CreateFDiv(LC, RC), Name); 00687 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS), 00688 FPMathTag, FMF), Name); 00689 } 00690 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { 00691 if (Constant *LC = dyn_cast<Constant>(LHS)) 00692 if (Constant *RC = dyn_cast<Constant>(RHS)) 00693 return Insert(Folder.CreateURem(LC, RC), Name); 00694 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); 00695 } 00696 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { 00697 if (Constant *LC = dyn_cast<Constant>(LHS)) 00698 if (Constant *RC = dyn_cast<Constant>(RHS)) 00699 return Insert(Folder.CreateSRem(LC, RC), Name); 00700 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); 00701 } 00702 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "", 00703 MDNode *FPMathTag = 0) { 00704 if (Constant *LC = dyn_cast<Constant>(LHS)) 00705 if (Constant *RC = dyn_cast<Constant>(RHS)) 00706 return Insert(Folder.CreateFRem(LC, RC), Name); 00707 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS), 00708 FPMathTag, FMF), Name); 00709 } 00710 00711 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", 00712 bool HasNUW = false, bool HasNSW = false) { 00713 if (Constant *LC = dyn_cast<Constant>(LHS)) 00714 if (Constant *RC = dyn_cast<Constant>(RHS)) 00715 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name); 00716 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, 00717 HasNUW, HasNSW); 00718 } 00719 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", 00720 bool HasNUW = false, bool HasNSW = false) { 00721 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 00722 HasNUW, HasNSW); 00723 } 00724 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", 00725 bool HasNUW = false, bool HasNSW = false) { 00726 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, 00727 HasNUW, HasNSW); 00728 } 00729 00730 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", 00731 bool isExact = false) { 00732 if (Constant *LC = dyn_cast<Constant>(LHS)) 00733 if (Constant *RC = dyn_cast<Constant>(RHS)) 00734 return Insert(Folder.CreateLShr(LC, RC, isExact), Name); 00735 if (!isExact) 00736 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); 00737 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); 00738 } 00739 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 00740 bool isExact = false) { 00741 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 00742 } 00743 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 00744 bool isExact = false) { 00745 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 00746 } 00747 00748 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", 00749 bool isExact = false) { 00750 if (Constant *LC = dyn_cast<Constant>(LHS)) 00751 if (Constant *RC = dyn_cast<Constant>(RHS)) 00752 return Insert(Folder.CreateAShr(LC, RC, isExact), Name); 00753 if (!isExact) 00754 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); 00755 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); 00756 } 00757 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", 00758 bool isExact = false) { 00759 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 00760 } 00761 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", 00762 bool isExact = false) { 00763 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); 00764 } 00765 00766 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { 00767 if (Constant *RC = dyn_cast<Constant>(RHS)) { 00768 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue()) 00769 return LHS; // LHS & -1 -> LHS 00770 if (Constant *LC = dyn_cast<Constant>(LHS)) 00771 return Insert(Folder.CreateAnd(LC, RC), Name); 00772 } 00773 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); 00774 } 00775 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { 00776 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00777 } 00778 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { 00779 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00780 } 00781 00782 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { 00783 if (Constant *RC = dyn_cast<Constant>(RHS)) { 00784 if (RC->isNullValue()) 00785 return LHS; // LHS | 0 -> LHS 00786 if (Constant *LC = dyn_cast<Constant>(LHS)) 00787 return Insert(Folder.CreateOr(LC, RC), Name); 00788 } 00789 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); 00790 } 00791 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { 00792 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00793 } 00794 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { 00795 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00796 } 00797 00798 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { 00799 if (Constant *LC = dyn_cast<Constant>(LHS)) 00800 if (Constant *RC = dyn_cast<Constant>(RHS)) 00801 return Insert(Folder.CreateXor(LC, RC), Name); 00802 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); 00803 } 00804 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { 00805 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00806 } 00807 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { 00808 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); 00809 } 00810 00811 Value *CreateBinOp(Instruction::BinaryOps Opc, 00812 Value *LHS, Value *RHS, const Twine &Name = "") { 00813 if (Constant *LC = dyn_cast<Constant>(LHS)) 00814 if (Constant *RC = dyn_cast<Constant>(RHS)) 00815 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name); 00816 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); 00817 } 00818 00819 Value *CreateNeg(Value *V, const Twine &Name = "", 00820 bool HasNUW = false, bool HasNSW = false) { 00821 if (Constant *VC = dyn_cast<Constant>(V)) 00822 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name); 00823 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name); 00824 if (HasNUW) BO->setHasNoUnsignedWrap(); 00825 if (HasNSW) BO->setHasNoSignedWrap(); 00826 return BO; 00827 } 00828 Value *CreateNSWNeg(Value *V, const Twine &Name = "") { 00829 return CreateNeg(V, Name, false, true); 00830 } 00831 Value *CreateNUWNeg(Value *V, const Twine &Name = "") { 00832 return CreateNeg(V, Name, true, false); 00833 } 00834 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) { 00835 if (Constant *VC = dyn_cast<Constant>(V)) 00836 return Insert(Folder.CreateFNeg(VC), Name); 00837 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V), 00838 FPMathTag, FMF), Name); 00839 } 00840 Value *CreateNot(Value *V, const Twine &Name = "") { 00841 if (Constant *VC = dyn_cast<Constant>(V)) 00842 return Insert(Folder.CreateNot(VC), Name); 00843 return Insert(BinaryOperator::CreateNot(V), Name); 00844 } 00845 00846 //===--------------------------------------------------------------------===// 00847 // Instruction creation methods: Memory Instructions 00848 //===--------------------------------------------------------------------===// 00849 00850 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0, 00851 const Twine &Name = "") { 00852 return Insert(new AllocaInst(Ty, ArraySize), Name); 00853 } 00854 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of 00855 // converting the string to 'bool' for the isVolatile parameter. 00856 LoadInst *CreateLoad(Value *Ptr, const char *Name) { 00857 return Insert(new LoadInst(Ptr), Name); 00858 } 00859 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") { 00860 return Insert(new LoadInst(Ptr), Name); 00861 } 00862 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") { 00863 return Insert(new LoadInst(Ptr, 0, isVolatile), Name); 00864 } 00865 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { 00866 return Insert(new StoreInst(Val, Ptr, isVolatile)); 00867 } 00868 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' 00869 // correctly, instead of converting the string to 'bool' for the isVolatile 00870 // parameter. 00871 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) { 00872 LoadInst *LI = CreateLoad(Ptr, Name); 00873 LI->setAlignment(Align); 00874 return LI; 00875 } 00876 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, 00877 const Twine &Name = "") { 00878 LoadInst *LI = CreateLoad(Ptr, Name); 00879 LI->setAlignment(Align); 00880 return LI; 00881 } 00882 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile, 00883 const Twine &Name = "") { 00884 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name); 00885 LI->setAlignment(Align); 00886 return LI; 00887 } 00888 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, 00889 bool isVolatile = false) { 00890 StoreInst *SI = CreateStore(Val, Ptr, isVolatile); 00891 SI->setAlignment(Align); 00892 return SI; 00893 } 00894 FenceInst *CreateFence(AtomicOrdering Ordering, 00895 SynchronizationScope SynchScope = CrossThread) { 00896 return Insert(new FenceInst(Context, Ordering, SynchScope)); 00897 } 00898 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, 00899 AtomicOrdering Ordering, 00900 SynchronizationScope SynchScope = CrossThread) { 00901 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope)); 00902 } 00903 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, 00904 AtomicOrdering Ordering, 00905 SynchronizationScope SynchScope = CrossThread) { 00906 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope)); 00907 } 00908 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList, 00909 const Twine &Name = "") { 00910 if (Constant *PC = dyn_cast<Constant>(Ptr)) { 00911 // Every index must be constant. 00912 size_t i, e; 00913 for (i = 0, e = IdxList.size(); i != e; ++i) 00914 if (!isa<Constant>(IdxList[i])) 00915 break; 00916 if (i == e) 00917 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name); 00918 } 00919 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name); 00920 } 00921 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, 00922 const Twine &Name = "") { 00923 if (Constant *PC = dyn_cast<Constant>(Ptr)) { 00924 // Every index must be constant. 00925 size_t i, e; 00926 for (i = 0, e = IdxList.size(); i != e; ++i) 00927 if (!isa<Constant>(IdxList[i])) 00928 break; 00929 if (i == e) 00930 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name); 00931 } 00932 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name); 00933 } 00934 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { 00935 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00936 if (Constant *IC = dyn_cast<Constant>(Idx)) 00937 return Insert(Folder.CreateGetElementPtr(PC, IC), Name); 00938 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); 00939 } 00940 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { 00941 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00942 if (Constant *IC = dyn_cast<Constant>(Idx)) 00943 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name); 00944 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name); 00945 } 00946 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") { 00947 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 00948 00949 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00950 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name); 00951 00952 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); 00953 } 00954 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0, 00955 const Twine &Name = "") { 00956 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); 00957 00958 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00959 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name); 00960 00961 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name); 00962 } 00963 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 00964 const Twine &Name = "") { 00965 Value *Idxs[] = { 00966 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 00967 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 00968 }; 00969 00970 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00971 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name); 00972 00973 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name); 00974 } 00975 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 00976 const Twine &Name = "") { 00977 Value *Idxs[] = { 00978 ConstantInt::get(Type::getInt32Ty(Context), Idx0), 00979 ConstantInt::get(Type::getInt32Ty(Context), Idx1) 00980 }; 00981 00982 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00983 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name); 00984 00985 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name); 00986 } 00987 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") { 00988 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 00989 00990 if (Constant *PC = dyn_cast<Constant>(Ptr)) 00991 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name); 00992 00993 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); 00994 } 00995 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0, 00996 const Twine &Name = "") { 00997 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); 00998 00999 if (Constant *PC = dyn_cast<Constant>(Ptr)) 01000 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name); 01001 01002 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name); 01003 } 01004 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, 01005 const Twine &Name = "") { 01006 Value *Idxs[] = { 01007 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 01008 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 01009 }; 01010 01011 if (Constant *PC = dyn_cast<Constant>(Ptr)) 01012 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name); 01013 01014 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name); 01015 } 01016 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, 01017 const Twine &Name = "") { 01018 Value *Idxs[] = { 01019 ConstantInt::get(Type::getInt64Ty(Context), Idx0), 01020 ConstantInt::get(Type::getInt64Ty(Context), Idx1) 01021 }; 01022 01023 if (Constant *PC = dyn_cast<Constant>(Ptr)) 01024 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name); 01025 01026 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name); 01027 } 01028 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") { 01029 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name); 01030 } 01031 01032 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type 01033 /// instead of a pointer to array of i8. 01034 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") { 01035 Value *gv = CreateGlobalString(Str, Name); 01036 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0); 01037 Value *Args[] = { zero, zero }; 01038 return CreateInBoundsGEP(gv, Args, Name); 01039 } 01040 01041 //===--------------------------------------------------------------------===// 01042 // Instruction creation methods: Cast/Conversion Operators 01043 //===--------------------------------------------------------------------===// 01044 01045 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { 01046 return CreateCast(Instruction::Trunc, V, DestTy, Name); 01047 } 01048 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { 01049 return CreateCast(Instruction::ZExt, V, DestTy, Name); 01050 } 01051 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { 01052 return CreateCast(Instruction::SExt, V, DestTy, Name); 01053 } 01054 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return 01055 /// the value untouched if the type of V is already DestTy. 01056 Value *CreateZExtOrTrunc(Value *V, Type *DestTy, 01057 const Twine &Name = "") { 01058 assert(V->getType()->isIntOrIntVectorTy() && 01059 DestTy->isIntOrIntVectorTy() && 01060 "Can only zero extend/truncate integers!"); 01061 Type *VTy = V->getType(); 01062 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 01063 return CreateZExt(V, DestTy, Name); 01064 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 01065 return CreateTrunc(V, DestTy, Name); 01066 return V; 01067 } 01068 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return 01069 /// the value untouched if the type of V is already DestTy. 01070 Value *CreateSExtOrTrunc(Value *V, Type *DestTy, 01071 const Twine &Name = "") { 01072 assert(V->getType()->isIntOrIntVectorTy() && 01073 DestTy->isIntOrIntVectorTy() && 01074 "Can only sign extend/truncate integers!"); 01075 Type *VTy = V->getType(); 01076 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) 01077 return CreateSExt(V, DestTy, Name); 01078 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) 01079 return CreateTrunc(V, DestTy, Name); 01080 return V; 01081 } 01082 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){ 01083 return CreateCast(Instruction::FPToUI, V, DestTy, Name); 01084 } 01085 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){ 01086 return CreateCast(Instruction::FPToSI, V, DestTy, Name); 01087 } 01088 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 01089 return CreateCast(Instruction::UIToFP, V, DestTy, Name); 01090 } 01091 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ 01092 return CreateCast(Instruction::SIToFP, V, DestTy, Name); 01093 } 01094 Value *CreateFPTrunc(Value *V, Type *DestTy, 01095 const Twine &Name = "") { 01096 return CreateCast(Instruction::FPTrunc, V, DestTy, Name); 01097 } 01098 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { 01099 return CreateCast(Instruction::FPExt, V, DestTy, Name); 01100 } 01101 Value *CreatePtrToInt(Value *V, Type *DestTy, 01102 const Twine &Name = "") { 01103 return CreateCast(Instruction::PtrToInt, V, DestTy, Name); 01104 } 01105 Value *CreateIntToPtr(Value *V, Type *DestTy, 01106 const Twine &Name = "") { 01107 return CreateCast(Instruction::IntToPtr, V, DestTy, Name); 01108 } 01109 Value *CreateBitCast(Value *V, Type *DestTy, 01110 const Twine &Name = "") { 01111 return CreateCast(Instruction::BitCast, V, DestTy, Name); 01112 } 01113 Value *CreateZExtOrBitCast(Value *V, Type *DestTy, 01114 const Twine &Name = "") { 01115 if (V->getType() == DestTy) 01116 return V; 01117 if (Constant *VC = dyn_cast<Constant>(V)) 01118 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); 01119 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); 01120 } 01121 Value *CreateSExtOrBitCast(Value *V, Type *DestTy, 01122 const Twine &Name = "") { 01123 if (V->getType() == DestTy) 01124 return V; 01125 if (Constant *VC = dyn_cast<Constant>(V)) 01126 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); 01127 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); 01128 } 01129 Value *CreateTruncOrBitCast(Value *V, Type *DestTy, 01130 const Twine &Name = "") { 01131 if (V->getType() == DestTy) 01132 return V; 01133 if (Constant *VC = dyn_cast<Constant>(V)) 01134 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); 01135 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); 01136 } 01137 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, 01138 const Twine &Name = "") { 01139 if (V->getType() == DestTy) 01140 return V; 01141 if (Constant *VC = dyn_cast<Constant>(V)) 01142 return Insert(Folder.CreateCast(Op, VC, DestTy), Name); 01143 return Insert(CastInst::Create(Op, V, DestTy), Name); 01144 } 01145 Value *CreatePointerCast(Value *V, Type *DestTy, 01146 const Twine &Name = "") { 01147 if (V->getType() == DestTy) 01148 return V; 01149 if (Constant *VC = dyn_cast<Constant>(V)) 01150 return Insert(Folder.CreatePointerCast(VC, DestTy), Name); 01151 return Insert(CastInst::CreatePointerCast(V, DestTy), Name); 01152 } 01153 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, 01154 const Twine &Name = "") { 01155 if (V->getType() == DestTy) 01156 return V; 01157 if (Constant *VC = dyn_cast<Constant>(V)) 01158 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); 01159 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); 01160 } 01161 private: 01162 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a 01163 // compile time error, instead of converting the string to bool for the 01164 // isSigned parameter. 01165 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION; 01166 public: 01167 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { 01168 if (V->getType() == DestTy) 01169 return V; 01170 if (Constant *VC = dyn_cast<Constant>(V)) 01171 return Insert(Folder.CreateFPCast(VC, DestTy), Name); 01172 return Insert(CastInst::CreateFPCast(V, DestTy), Name); 01173 } 01174 01175 //===--------------------------------------------------------------------===// 01176 // Instruction creation methods: Compare Instructions 01177 //===--------------------------------------------------------------------===// 01178 01179 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { 01180 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); 01181 } 01182 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { 01183 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); 01184 } 01185 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { 01186 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); 01187 } 01188 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { 01189 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); 01190 } 01191 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { 01192 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); 01193 } 01194 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { 01195 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); 01196 } 01197 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { 01198 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); 01199 } 01200 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { 01201 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); 01202 } 01203 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { 01204 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); 01205 } 01206 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { 01207 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); 01208 } 01209 01210 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") { 01211 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name); 01212 } 01213 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") { 01214 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name); 01215 } 01216 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") { 01217 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name); 01218 } 01219 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") { 01220 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name); 01221 } 01222 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") { 01223 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name); 01224 } 01225 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") { 01226 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name); 01227 } 01228 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") { 01229 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name); 01230 } 01231 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") { 01232 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name); 01233 } 01234 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") { 01235 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name); 01236 } 01237 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { 01238 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name); 01239 } 01240 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { 01241 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name); 01242 } 01243 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { 01244 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name); 01245 } 01246 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { 01247 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name); 01248 } 01249 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") { 01250 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name); 01251 } 01252 01253 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 01254 const Twine &Name = "") { 01255 if (Constant *LC = dyn_cast<Constant>(LHS)) 01256 if (Constant *RC = dyn_cast<Constant>(RHS)) 01257 return Insert(Folder.CreateICmp(P, LC, RC), Name); 01258 return Insert(new ICmpInst(P, LHS, RHS), Name); 01259 } 01260 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 01261 const Twine &Name = "") { 01262 if (Constant *LC = dyn_cast<Constant>(LHS)) 01263 if (Constant *RC = dyn_cast<Constant>(RHS)) 01264 return Insert(Folder.CreateFCmp(P, LC, RC), Name); 01265 return Insert(new FCmpInst(P, LHS, RHS), Name); 01266 } 01267 01268 //===--------------------------------------------------------------------===// 01269 // Instruction creation methods: Other Instructions 01270 //===--------------------------------------------------------------------===// 01271 01272 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, 01273 const Twine &Name = "") { 01274 return Insert(PHINode::Create(Ty, NumReservedValues), Name); 01275 } 01276 01277 CallInst *CreateCall(Value *Callee, const Twine &Name = "") { 01278 return Insert(CallInst::Create(Callee), Name); 01279 } 01280 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") { 01281 return Insert(CallInst::Create(Callee, Arg), Name); 01282 } 01283 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2, 01284 const Twine &Name = "") { 01285 Value *Args[] = { Arg1, Arg2 }; 01286 return Insert(CallInst::Create(Callee, Args), Name); 01287 } 01288 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3, 01289 const Twine &Name = "") { 01290 Value *Args[] = { Arg1, Arg2, Arg3 }; 01291 return Insert(CallInst::Create(Callee, Args), Name); 01292 } 01293 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3, 01294 Value *Arg4, const Twine &Name = "") { 01295 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 }; 01296 return Insert(CallInst::Create(Callee, Args), Name); 01297 } 01298 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3, 01299 Value *Arg4, Value *Arg5, const Twine &Name = "") { 01300 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 }; 01301 return Insert(CallInst::Create(Callee, Args), Name); 01302 } 01303 01304 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args, 01305 const Twine &Name = "") { 01306 return Insert(CallInst::Create(Callee, Args), Name); 01307 } 01308 01309 Value *CreateSelect(Value *C, Value *True, Value *False, 01310 const Twine &Name = "") { 01311 if (Constant *CC = dyn_cast<Constant>(C)) 01312 if (Constant *TC = dyn_cast<Constant>(True)) 01313 if (Constant *FC = dyn_cast<Constant>(False)) 01314 return Insert(Folder.CreateSelect(CC, TC, FC), Name); 01315 return Insert(SelectInst::Create(C, True, False), Name); 01316 } 01317 01318 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { 01319 return Insert(new VAArgInst(List, Ty), Name); 01320 } 01321 01322 Value *CreateExtractElement(Value *Vec, Value *Idx, 01323 const Twine &Name = "") { 01324 if (Constant *VC = dyn_cast<Constant>(Vec)) 01325 if (Constant *IC = dyn_cast<Constant>(Idx)) 01326 return Insert(Folder.CreateExtractElement(VC, IC), Name); 01327 return Insert(ExtractElementInst::Create(Vec, Idx), Name); 01328 } 01329 01330 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, 01331 const Twine &Name = "") { 01332 if (Constant *VC = dyn_cast<Constant>(Vec)) 01333 if (Constant *NC = dyn_cast<Constant>(NewElt)) 01334 if (Constant *IC = dyn_cast<Constant>(Idx)) 01335 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name); 01336 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); 01337 } 01338 01339 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, 01340 const Twine &Name = "") { 01341 if (Constant *V1C = dyn_cast<Constant>(V1)) 01342 if (Constant *V2C = dyn_cast<Constant>(V2)) 01343 if (Constant *MC = dyn_cast<Constant>(Mask)) 01344 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name); 01345 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); 01346 } 01347 01348 Value *CreateExtractValue(Value *Agg, 01349 ArrayRef<unsigned> Idxs, 01350 const Twine &Name = "") { 01351 if (Constant *AggC = dyn_cast<Constant>(Agg)) 01352 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name); 01353 return Insert(ExtractValueInst::Create(Agg, Idxs), Name); 01354 } 01355 01356 Value *CreateInsertValue(Value *Agg, Value *Val, 01357 ArrayRef<unsigned> Idxs, 01358 const Twine &Name = "") { 01359 if (Constant *AggC = dyn_cast<Constant>(Agg)) 01360 if (Constant *ValC = dyn_cast<Constant>(Val)) 01361 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name); 01362 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); 01363 } 01364 01365 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses, 01366 const Twine &Name = "") { 01367 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name); 01368 } 01369 01370 //===--------------------------------------------------------------------===// 01371 // Utility creation methods 01372 //===--------------------------------------------------------------------===// 01373 01374 /// \brief Return an i1 value testing if \p Arg is null. 01375 Value *CreateIsNull(Value *Arg, const Twine &Name = "") { 01376 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), 01377 Name); 01378 } 01379 01380 /// \brief Return an i1 value testing if \p Arg is not null. 01381 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { 01382 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), 01383 Name); 01384 } 01385 01386 /// \brief Return the i64 difference between two pointer values, dividing out 01387 /// the size of the pointed-to objects. 01388 /// 01389 /// This is intended to implement C-style pointer subtraction. As such, the 01390 /// pointers must be appropriately aligned for their element types and 01391 /// pointing into the same object. 01392 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") { 01393 assert(LHS->getType() == RHS->getType() && 01394 "Pointer subtraction operand types must match!"); 01395 PointerType *ArgType = cast<PointerType>(LHS->getType()); 01396 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context)); 01397 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context)); 01398 Value *Difference = CreateSub(LHS_int, RHS_int); 01399 return CreateExactSDiv(Difference, 01400 ConstantExpr::getSizeOf(ArgType->getElementType()), 01401 Name); 01402 } 01403 01404 /// \brief Return a vector value that contains \arg V broadcasted to \p 01405 /// NumElts elements. 01406 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") { 01407 assert(NumElts > 0 && "Cannot splat to an empty vector!"); 01408 01409 // First insert it into an undef vector so we can shuffle it. 01410 Type *I32Ty = getInt32Ty(); 01411 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts)); 01412 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0), 01413 Name + ".splatinsert"); 01414 01415 // Shuffle the value across the desired number of elements. 01416 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts)); 01417 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat"); 01418 } 01419 }; 01420 01421 // Create wrappers for C Binding types (see CBindingWrapping.h). 01422 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef) 01423 01424 } 01425 01426 #endif