LLVM API Documentation

IRBuilder.h

Go to the documentation of this file.
00001 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- 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_SUPPORT_IRBUILDER_H
00016 #define LLVM_SUPPORT_IRBUILDER_H
00017 
00018 #include "llvm/Constants.h"
00019 #include "llvm/Instructions.h"
00020 #include "llvm/GlobalAlias.h"
00021 #include "llvm/GlobalVariable.h"
00022 #include "llvm/Function.h"
00023 #include "llvm/LLVMContext.h"
00024 #include "llvm/ADT/Twine.h"
00025 #include "llvm/Support/ConstantFolder.h"
00026 
00027 namespace llvm {
00028 
00029 /// IRBuilder - This provides a uniform API for creating instructions and
00030 /// inserting them into a basic block: either at the end of a BasicBlock, or
00031 /// at a specific iterator location in a block.
00032 ///
00033 /// Note that the builder does not expose the full generality of LLVM
00034 /// instructions.  For example, it cannot be used to create instructions with
00035 /// arbitrary names (specifically, names with nul characters in them) - It only
00036 /// supports nul-terminated C strings.  For fully generic names, use
00037 /// I->setName().  For access to extra instruction properties, use the mutators
00038 /// (e.g. setVolatile) on the instructions after they have been created.
00039 /// The first template argument handles whether or not to preserve names in the
00040 /// final instruction output. This defaults to on.  The second template argument
00041 /// specifies a class to use for creating constants.  This defaults to creating
00042 /// minimally folded constants.
00043 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
00044   BasicBlock *BB;
00045   BasicBlock::iterator InsertPt;
00046   LLVMContext &Context;
00047   T Folder;
00048 public:
00049   IRBuilder(LLVMContext &C, const T& F) :
00050     Context(C), Folder(F) { ClearInsertionPoint(); }
00051   
00052   explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
00053     ClearInsertionPoint();
00054   }
00055   
00056   explicit IRBuilder(BasicBlock *TheBB, const T& F)
00057       : Context(TheBB->getContext()), Folder(F) {
00058     SetInsertPoint(TheBB);
00059   }
00060   
00061   explicit IRBuilder(BasicBlock *TheBB)
00062       : Context(TheBB->getContext()), Folder(Context) {
00063     SetInsertPoint(TheBB);
00064   }
00065   
00066   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
00067       : Context(TheBB->getContext()), Folder(F) {
00068     SetInsertPoint(TheBB, IP);
00069   }
00070   
00071   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
00072       : Context(TheBB->getContext()), Folder(Context) {
00073     SetInsertPoint(TheBB, IP);
00074   }
00075 
00076   /// getFolder - Get the constant folder being used.
00077   const T& getFolder() { return Folder; }
00078 
00079   /// isNamePreserving - Return true if this builder is configured to actually
00080   /// add the requested names to IR created through it.
00081   bool isNamePreserving() const { return preserveNames; }
00082   
00083   //===--------------------------------------------------------------------===//
00084   // Builder configuration methods
00085   //===--------------------------------------------------------------------===//
00086 
00087   /// ClearInsertionPoint - Clear the insertion point: created instructions will
00088   /// not be inserted into a block.
00089   void ClearInsertionPoint() {
00090     BB = 0;
00091   }
00092 
00093   BasicBlock *GetInsertBlock() const { return BB; }
00094 
00095   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
00096 
00097   /// SetInsertPoint - This specifies that created instructions should be
00098   /// appended to the end of the specified block.
00099   void SetInsertPoint(BasicBlock *TheBB) {
00100     BB = TheBB;
00101     InsertPt = BB->end();
00102   }
00103 
00104   /// SetInsertPoint - This specifies that created instructions should be
00105   /// inserted at the specified point.
00106   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
00107     BB = TheBB;
00108     InsertPt = IP;
00109   }
00110 
00111   /// Insert - Insert and return the specified instruction.
00112   template<typename InstTy>
00113   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
00114     InsertHelper(I, Name);
00115     return I;
00116   }
00117 
00118   /// InsertHelper - Insert the specified instruction at the specified insertion
00119   /// point.  This is split out of Insert so that it isn't duplicated for every
00120   /// template instantiation.
00121   void InsertHelper(Instruction *I, const Twine &Name) const {
00122     if (BB) BB->getInstList().insert(InsertPt, I);
00123     if (preserveNames)
00124       I->setName(Name);
00125   }
00126 
00127   //===--------------------------------------------------------------------===//
00128   // Type creation methods
00129   //===--------------------------------------------------------------------===//
00130 
00131   /// getInt1Ty - Fetch the type representing a single bit
00132   const Type *getInt1Ty() {
00133     return Type::getInt1Ty(Context);
00134   }
00135   
00136   /// getInt8Ty - Fetch the type representing an 8-bit integer.
00137   const Type *getInt8Ty() {
00138     return Type::getInt8Ty(Context);
00139   }
00140   
00141   /// getInt16Ty - Fetch the type representing a 16-bit integer.
00142   const Type *getInt16Ty() {
00143     return Type::getInt16Ty(Context);
00144   }
00145   
00146   /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
00147   const Type *getInt32Ty() {
00148     return Type::getInt32Ty(Context);
00149   }
00150   
00151   /// getInt64Ty - Fetch the type representing a 64-bit integer.
00152   const Type *getInt64Ty() {
00153     return Type::getInt64Ty(Context);
00154   }
00155 
00156   /// getFloatTy - Fetch the type representing a 32-bit floating point value.
00157   const Type *getFloatTy() {
00158     return Type::getFloatTy(Context);
00159   }
00160   
00161   /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
00162   const Type *getDoubleTy() {
00163     return Type::getDoubleTy(Context);
00164   }
00165   
00166   /// getVoidTy - Fetch the type representing void.
00167   const Type *getVoidTy() {
00168     return Type::getVoidTy(Context);
00169   }
00170 
00171   //===--------------------------------------------------------------------===//
00172   // Instruction creation methods: Terminators
00173   //===--------------------------------------------------------------------===//
00174 
00175   /// CreateRetVoid - Create a 'ret void' instruction.
00176   ReturnInst *CreateRetVoid() {
00177     return Insert(ReturnInst::Create(Context));
00178   }
00179 
00180   /// @verbatim
00181   /// CreateRet - Create a 'ret <val>' instruction.
00182   /// @endverbatim
00183   ReturnInst *CreateRet(Value *V) {
00184     return Insert(ReturnInst::Create(Context, V));
00185   }
00186 
00187   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
00188   /// with one Value from the retVals array each, that build a aggregate
00189   /// return value one value at a time, and a ret instruction to return
00190   /// the resulting aggregate value. This is a convenience function for
00191   /// code that uses aggregate return values as a vehicle for having
00192   /// multiple return values.
00193   ///
00194   ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
00195     const Type *RetType = BB->getParent()->getReturnType();
00196     Value *V = UndefValue::get(RetType);
00197     for (unsigned i = 0; i != N; ++i)
00198       V = CreateInsertValue(V, retVals[i], i, "mrv");
00199     return Insert(ReturnInst::Create(Context, V));
00200   }
00201 
00202   /// CreateBr - Create an unconditional 'br label X' instruction.
00203   BranchInst *CreateBr(BasicBlock *Dest) {
00204     return Insert(BranchInst::Create(Dest));
00205   }
00206 
00207   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
00208   /// instruction.
00209   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
00210     return Insert(BranchInst::Create(True, False, Cond));
00211   }
00212 
00213   /// CreateSwitch - Create a switch instruction with the specified value,
00214   /// default dest, and with a hint for the number of cases that will be added
00215   /// (for efficient allocation).
00216   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
00217     return Insert(SwitchInst::Create(V, Dest, NumCases));
00218   }
00219 
00220   /// CreateInvoke - Create an invoke instruction.
00221   template<typename InputIterator>
00222   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
00223                            BasicBlock *UnwindDest, InputIterator ArgBegin,
00224                            InputIterator ArgEnd, const Twine &Name = "") {
00225     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
00226                                      ArgBegin, ArgEnd), Name);
00227   }
00228 
00229   UnwindInst *CreateUnwind() {
00230     return Insert(new UnwindInst(Context));
00231   }
00232 
00233   UnreachableInst *CreateUnreachable() {
00234     return Insert(new UnreachableInst(Context));
00235   }
00236 
00237   //===--------------------------------------------------------------------===//
00238   // Instruction creation methods: Binary Operators
00239   //===--------------------------------------------------------------------===//
00240 
00241   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
00242     if (Constant *LC = dyn_cast<Constant>(LHS))
00243       if (Constant *RC = dyn_cast<Constant>(RHS))
00244         return Folder.CreateAdd(LC, RC);
00245     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
00246   }
00247   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
00248     if (Constant *LC = dyn_cast<Constant>(LHS))
00249       if (Constant *RC = dyn_cast<Constant>(RHS))
00250         return Folder.CreateNSWAdd(LC, RC);
00251     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
00252   }
00253   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
00254     if (Constant *LC = dyn_cast<Constant>(LHS))
00255       if (Constant *RC = dyn_cast<Constant>(RHS))
00256         return Folder.CreateFAdd(LC, RC);
00257     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
00258   }
00259   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
00260     if (Constant *LC = dyn_cast<Constant>(LHS))
00261       if (Constant *RC = dyn_cast<Constant>(RHS))
00262         return Folder.CreateSub(LC, RC);
00263     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
00264   }
00265   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
00266     if (Constant *LC = dyn_cast<Constant>(LHS))
00267       if (Constant *RC = dyn_cast<Constant>(RHS))
00268         return Folder.CreateFSub(LC, RC);
00269     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
00270   }
00271   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
00272     if (Constant *LC = dyn_cast<Constant>(LHS))
00273       if (Constant *RC = dyn_cast<Constant>(RHS))
00274         return Folder.CreateMul(LC, RC);
00275     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
00276   }
00277   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
00278     if (Constant *LC = dyn_cast<Constant>(LHS))
00279       if (Constant *RC = dyn_cast<Constant>(RHS))
00280         return Folder.CreateFMul(LC, RC);
00281     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
00282   }
00283   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00284     if (Constant *LC = dyn_cast<Constant>(LHS))
00285       if (Constant *RC = dyn_cast<Constant>(RHS))
00286         return Folder.CreateUDiv(LC, RC);
00287     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
00288   }
00289   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00290     if (Constant *LC = dyn_cast<Constant>(LHS))
00291       if (Constant *RC = dyn_cast<Constant>(RHS))
00292         return Folder.CreateSDiv(LC, RC);
00293     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
00294   }
00295   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00296     if (Constant *LC = dyn_cast<Constant>(LHS))
00297       if (Constant *RC = dyn_cast<Constant>(RHS))
00298         return Folder.CreateExactSDiv(LC, RC);
00299     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
00300   }
00301   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00302     if (Constant *LC = dyn_cast<Constant>(LHS))
00303       if (Constant *RC = dyn_cast<Constant>(RHS))
00304         return Folder.CreateFDiv(LC, RC);
00305     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
00306   }
00307   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
00308     if (Constant *LC = dyn_cast<Constant>(LHS))
00309       if (Constant *RC = dyn_cast<Constant>(RHS))
00310         return Folder.CreateURem(LC, RC);
00311     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
00312   }
00313   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
00314     if (Constant *LC = dyn_cast<Constant>(LHS))
00315       if (Constant *RC = dyn_cast<Constant>(RHS))
00316         return Folder.CreateSRem(LC, RC);
00317     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
00318   }
00319   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
00320     if (Constant *LC = dyn_cast<Constant>(LHS))
00321       if (Constant *RC = dyn_cast<Constant>(RHS))
00322         return Folder.CreateFRem(LC, RC);
00323     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
00324   }
00325   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
00326     if (Constant *LC = dyn_cast<Constant>(LHS))
00327       if (Constant *RC = dyn_cast<Constant>(RHS))
00328         return Folder.CreateShl(LC, RC);
00329     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
00330   }
00331   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
00332     if (Constant *LC = dyn_cast<Constant>(LHS))
00333       if (Constant *RC = dyn_cast<Constant>(RHS))
00334         return Folder.CreateLShr(LC, RC);
00335     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
00336   }
00337   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
00338     if (Constant *LC = dyn_cast<Constant>(LHS))
00339       if (Constant *RC = dyn_cast<Constant>(RHS))
00340         return Folder.CreateAShr(LC, RC);
00341     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
00342   }
00343   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
00344     if (Constant *LC = dyn_cast<Constant>(LHS))
00345       if (Constant *RC = dyn_cast<Constant>(RHS))
00346         return Folder.CreateAnd(LC, RC);
00347     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
00348   }
00349   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
00350     if (Constant *LC = dyn_cast<Constant>(LHS))
00351       if (Constant *RC = dyn_cast<Constant>(RHS))
00352         return Folder.CreateOr(LC, RC);
00353     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
00354   }
00355   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
00356     if (Constant *LC = dyn_cast<Constant>(LHS))
00357       if (Constant *RC = dyn_cast<Constant>(RHS))
00358         return Folder.CreateXor(LC, RC);
00359     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
00360   }
00361 
00362   Value *CreateBinOp(Instruction::BinaryOps Opc,
00363                      Value *LHS, Value *RHS, const Twine &Name = "") {
00364     if (Constant *LC = dyn_cast<Constant>(LHS))
00365       if (Constant *RC = dyn_cast<Constant>(RHS))
00366         return Folder.CreateBinOp(Opc, LC, RC);
00367     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
00368   }
00369 
00370   Value *CreateNeg(Value *V, const Twine &Name = "") {
00371     if (Constant *VC = dyn_cast<Constant>(V))
00372       return Folder.CreateNeg(VC);
00373     return Insert(BinaryOperator::CreateNeg(V), Name);
00374   }
00375   Value *CreateFNeg(Value *V, const Twine &Name = "") {
00376     if (Constant *VC = dyn_cast<Constant>(V))
00377       return Folder.CreateFNeg(VC);
00378     return Insert(BinaryOperator::CreateFNeg(V), Name);
00379   }
00380   Value *CreateNot(Value *V, const Twine &Name = "") {
00381     if (Constant *VC = dyn_cast<Constant>(V))
00382       return Folder.CreateNot(VC);
00383     return Insert(BinaryOperator::CreateNot(V), Name);
00384   }
00385 
00386   //===--------------------------------------------------------------------===//
00387   // Instruction creation methods: Memory Instructions
00388   //===--------------------------------------------------------------------===//
00389 
00390   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
00391                            const Twine &Name = "") {
00392     return Insert(new MallocInst(Ty, ArraySize), Name);
00393   }
00394   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
00395                            const Twine &Name = "") {
00396     return Insert(new AllocaInst(Ty, ArraySize), Name);
00397   }
00398   FreeInst *CreateFree(Value *Ptr) {
00399     return Insert(new FreeInst(Ptr));
00400   }
00401   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
00402   // converting the string to 'bool' for the isVolatile parameter.
00403   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
00404     return Insert(new LoadInst(Ptr), Name);
00405   }
00406   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
00407     return Insert(new LoadInst(Ptr), Name);
00408   }
00409   LoadInst *CreateLoad(Value *Ptr, bool isVolatile,
00410                        const Twine &Name = "") {
00411     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
00412   }
00413   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
00414     return Insert(new StoreInst(Val, Ptr, isVolatile));
00415   }
00416   template<typename InputIterator>
00417   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
00418                    const Twine &Name = "") {
00419     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
00420       // Every index must be constant.
00421       InputIterator i;
00422       for (i = IdxBegin; i < IdxEnd; ++i)
00423         if (!isa<Constant>(*i))
00424           break;
00425       if (i == IdxEnd)
00426         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
00427     }
00428     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
00429   }
00430   template<typename InputIterator>
00431   Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
00432                            const Twine &Name = "") {
00433     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
00434       // Every index must be constant.
00435       InputIterator i;
00436       for (i = IdxBegin; i < IdxEnd; ++i)
00437         if (!isa<Constant>(*i))
00438           break;
00439       if (i == IdxEnd)
00440         return Folder.CreateInBoundsGetElementPtr(PC,
00441                                                   &IdxBegin[0],
00442                                                   IdxEnd - IdxBegin);
00443     }
00444     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
00445                   Name);
00446   }
00447   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
00448     if (Constant *PC = dyn_cast<Constant>(Ptr))
00449       if (Constant *IC = dyn_cast<Constant>(Idx))
00450         return Folder.CreateGetElementPtr(PC, &IC, 1);
00451     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
00452   }
00453   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
00454     if (Constant *PC = dyn_cast<Constant>(Ptr))
00455       if (Constant *IC = dyn_cast<Constant>(Idx))
00456         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
00457     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
00458   }
00459   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
00460     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
00461 
00462     if (Constant *PC = dyn_cast<Constant>(Ptr))
00463       return Folder.CreateGetElementPtr(PC, &Idx, 1);
00464 
00465     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
00466   }
00467   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
00468                                     const Twine &Name = "") {
00469     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
00470 
00471     if (Constant *PC = dyn_cast<Constant>(Ptr))
00472       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
00473 
00474     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
00475   }
00476   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
00477                     const Twine &Name = "") {
00478     Value *Idxs[] = {
00479       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
00480       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
00481     };
00482 
00483     if (Constant *PC = dyn_cast<Constant>(Ptr))
00484       return Folder.CreateGetElementPtr(PC, Idxs, 2);
00485 
00486     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
00487   }
00488   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
00489                                     const Twine &Name = "") {
00490     Value *Idxs[] = {
00491       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
00492       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
00493     };
00494 
00495     if (Constant *PC = dyn_cast<Constant>(Ptr))
00496       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
00497 
00498     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
00499   }
00500   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
00501     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
00502 
00503     if (Constant *PC = dyn_cast<Constant>(Ptr))
00504       return Folder.CreateGetElementPtr(PC, &Idx, 1);
00505 
00506     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
00507   }
00508   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
00509                                     const Twine &Name = "") {
00510     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
00511 
00512     if (Constant *PC = dyn_cast<Constant>(Ptr))
00513       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
00514 
00515     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
00516   }
00517   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
00518                     const Twine &Name = "") {
00519     Value *Idxs[] = {
00520       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
00521       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
00522     };
00523 
00524     if (Constant *PC = dyn_cast<Constant>(Ptr))
00525       return Folder.CreateGetElementPtr(PC, Idxs, 2);
00526 
00527     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
00528   }
00529   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
00530                                     const Twine &Name = "") {
00531     Value *Idxs[] = {
00532       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
00533       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
00534     };
00535 
00536     if (Constant *PC = dyn_cast<Constant>(Ptr))
00537       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
00538 
00539     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
00540   }
00541   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
00542     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
00543   }
00544   Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
00545     Constant *StrConstant = ConstantArray::get(Context, Str, true);
00546     Module &M = *BB->getParent()->getParent();
00547     GlobalVariable *gv = new GlobalVariable(M,
00548                                             StrConstant->getType(),
00549                                             true,
00550                                             GlobalValue::InternalLinkage,
00551                                             StrConstant,
00552                                             "",
00553                                             0,
00554                                             false);
00555     gv->setName(Name);
00556     return gv;
00557   }
00558   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
00559     Value *gv = CreateGlobalString(Str, Name);
00560     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
00561     Value *Args[] = { zero, zero };
00562     return CreateInBoundsGEP(gv, Args, Args+2, Name);
00563   }
00564   //===--------------------------------------------------------------------===//
00565   // Instruction creation methods: Cast/Conversion Operators
00566   //===--------------------------------------------------------------------===//
00567 
00568   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
00569     return CreateCast(Instruction::Trunc, V, DestTy, Name);
00570   }
00571   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
00572     return CreateCast(Instruction::ZExt, V, DestTy, Name);
00573   }
00574   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
00575     return CreateCast(Instruction::SExt, V, DestTy, Name);
00576   }
00577   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
00578     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
00579   }
00580   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
00581     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
00582   }
00583   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
00584     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
00585   }
00586   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
00587     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
00588   }
00589   Value *CreateFPTrunc(Value *V, const Type *DestTy,
00590                        const Twine &Name = "") {
00591     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
00592   }
00593   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
00594     return CreateCast(Instruction::FPExt, V, DestTy, Name);
00595   }
00596   Value *CreatePtrToInt(Value *V, const Type *DestTy,
00597                         const Twine &Name = "") {
00598     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
00599   }
00600   Value *CreateIntToPtr(Value *V, const Type *DestTy,
00601                         const Twine &Name = "") {
00602     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
00603   }
00604   Value *CreateBitCast(Value *V, const Type *DestTy,
00605                        const Twine &Name = "") {
00606     return CreateCast(Instruction::BitCast, V, DestTy, Name);
00607   }
00608   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
00609                              const Twine &Name = "") {
00610     if (V->getType() == DestTy)
00611       return V;
00612     if (Constant *VC = dyn_cast<Constant>(V))
00613       return Folder.CreateZExtOrBitCast(VC, DestTy);
00614     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
00615   }
00616   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
00617                              const Twine &Name = "") {
00618     if (V->getType() == DestTy)
00619       return V;
00620     if (Constant *VC = dyn_cast<Constant>(V))
00621       return Folder.CreateSExtOrBitCast(VC, DestTy);
00622     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
00623   }
00624   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
00625                               const Twine &Name = "") {
00626     if (V->getType() == DestTy)
00627       return V;
00628     if (Constant *VC = dyn_cast<Constant>(V))
00629       return Folder.CreateTruncOrBitCast(VC, DestTy);
00630     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
00631   }
00632   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
00633                     const Twine &Name = "") {
00634     if (V->getType() == DestTy)
00635       return V;
00636     if (Constant *VC = dyn_cast<Constant>(V))
00637       return Folder.CreateCast(Op, VC, DestTy);
00638     return Insert(CastInst::Create(Op, V, DestTy), Name);
00639   }
00640   Value *CreatePointerCast(Value *V, const Type *DestTy,
00641                            const Twine &Name = "") {
00642     if (V->getType() == DestTy)
00643       return V;
00644     if (Constant *VC = dyn_cast<Constant>(V))
00645       return Folder.CreatePointerCast(VC, DestTy);
00646     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
00647   }
00648   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
00649                        const Twine &Name = "") {
00650     if (V->getType() == DestTy)
00651       return V;
00652     if (Constant *VC = dyn_cast<Constant>(V))
00653       return Folder.CreateIntCast(VC, DestTy, isSigned);
00654     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
00655   }
00656   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
00657     if (V->getType() == DestTy)
00658       return V;
00659     if (Constant *VC = dyn_cast<Constant>(V))
00660       return Folder.CreateFPCast(VC, DestTy);
00661     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
00662   }
00663 
00664   //===--------------------------------------------------------------------===//
00665   // Instruction creation methods: Compare Instructions
00666   //===--------------------------------------------------------------------===//
00667 
00668   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
00669     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
00670   }
00671   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
00672     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
00673   }
00674   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
00675     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
00676   }
00677   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
00678     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
00679   }
00680   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
00681     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
00682   }
00683   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
00684     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
00685   }
00686   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
00687     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
00688   }
00689   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
00690     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
00691   }
00692   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
00693     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
00694   }
00695   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
00696     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
00697   }
00698 
00699   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
00700     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
00701   }
00702   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
00703     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
00704   }
00705   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
00706     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
00707   }
00708   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
00709     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
00710   }
00711   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
00712     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
00713   }
00714   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
00715     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
00716   }
00717   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
00718     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
00719   }
00720   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
00721     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
00722   }
00723   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
00724     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
00725   }
00726   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
00727     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
00728   }
00729   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
00730     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
00731   }
00732   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
00733     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
00734   }
00735   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
00736     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
00737   }
00738   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
00739     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
00740   }
00741 
00742   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00743                     const Twine &Name = "") {
00744     if (Constant *LC = dyn_cast<Constant>(LHS))
00745       if (Constant *RC = dyn_cast<Constant>(RHS))
00746         return Folder.CreateICmp(P, LC, RC);
00747     return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
00748   }
00749   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00750                     const Twine &Name = "") {
00751     if (Constant *LC = dyn_cast<Constant>(LHS))
00752       if (Constant *RC = dyn_cast<Constant>(RHS))
00753         return Folder.CreateFCmp(P, LC, RC);
00754     return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
00755   }
00756 
00757   //===--------------------------------------------------------------------===//
00758   // Instruction creation methods: Other Instructions
00759   //===--------------------------------------------------------------------===//
00760 
00761   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
00762     return Insert(PHINode::Create(Ty), Name);
00763   }
00764 
00765   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
00766     return Insert(CallInst::Create(Callee), Name);
00767   }
00768   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
00769     return Insert(CallInst::Create(Callee, Arg), Name);
00770   }
00771   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
00772                         const Twine &Name = "") {
00773     Value *Args[] = { Arg1, Arg2 };
00774     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
00775   }
00776   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
00777                         const Twine &Name = "") {
00778     Value *Args[] = { Arg1, Arg2, Arg3 };
00779     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
00780   }
00781   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
00782                         Value *Arg4, const Twine &Name = "") {
00783     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
00784     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
00785   }
00786 
00787   template<typename InputIterator>
00788   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
00789                        InputIterator ArgEnd, const Twine &Name = "") {
00790     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
00791   }
00792 
00793   Value *CreateSelect(Value *C, Value *True, Value *False,
00794                       const Twine &Name = "") {
00795     if (Constant *CC = dyn_cast<Constant>(C))
00796       if (Constant *TC = dyn_cast<Constant>(True))
00797         if (Constant *FC = dyn_cast<Constant>(False))
00798           return Folder.CreateSelect(CC, TC, FC);
00799     return Insert(SelectInst::Create(C, True, False), Name);
00800   }
00801 
00802   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
00803     return Insert(new VAArgInst(List, Ty), Name);
00804   }
00805 
00806   Value *CreateExtractElement(Value *Vec, Value *Idx,
00807                               const Twine &Name = "") {
00808     if (Constant *VC = dyn_cast<Constant>(Vec))
00809       if (Constant *IC = dyn_cast<Constant>(Idx))
00810         return Folder.CreateExtractElement(VC, IC);
00811     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
00812   }
00813 
00814   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
00815                              const Twine &Name = "") {
00816     if (Constant *VC = dyn_cast<Constant>(Vec))
00817       if (Constant *NC = dyn_cast<Constant>(NewElt))
00818         if (Constant *IC = dyn_cast<Constant>(Idx))
00819           return Folder.CreateInsertElement(VC, NC, IC);
00820     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
00821   }
00822 
00823   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
00824                              const Twine &Name = "") {
00825     if (Constant *V1C = dyn_cast<Constant>(V1))
00826       if (Constant *V2C = dyn_cast<Constant>(V2))
00827         if (Constant *MC = dyn_cast<Constant>(Mask))
00828           return Folder.CreateShuffleVector(V1C, V2C, MC);
00829     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
00830   }
00831 
00832   Value *CreateExtractValue(Value *Agg, unsigned Idx,
00833                             const Twine &Name = "") {
00834     if (Constant *AggC = dyn_cast<Constant>(Agg))
00835       return Folder.CreateExtractValue(AggC, &Idx, 1);
00836     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
00837   }
00838 
00839   template<typename InputIterator>
00840   Value *CreateExtractValue(Value *Agg,
00841                             InputIterator IdxBegin,
00842                             InputIterator IdxEnd,
00843                             const Twine &Name = "") {
00844     if (Constant *AggC = dyn_cast<Constant>(Agg))
00845       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
00846     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
00847   }
00848 
00849   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
00850                            const Twine &Name = "") {
00851     if (Constant *AggC = dyn_cast<Constant>(Agg))
00852       if (Constant *ValC = dyn_cast<Constant>(Val))
00853         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
00854     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
00855   }
00856 
00857   template<typename InputIterator>
00858   Value *CreateInsertValue(Value *Agg, Value *Val,
00859                            InputIterator IdxBegin,
00860                            InputIterator IdxEnd,
00861                            const Twine &Name = "") {
00862     if (Constant *AggC = dyn_cast<Constant>(Agg))
00863       if (Constant *ValC = dyn_cast<Constant>(Val))
00864         return Folder.CreateInsertValue(AggC, ValC,
00865                                             IdxBegin, IdxEnd - IdxBegin);
00866     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
00867   }
00868 
00869   //===--------------------------------------------------------------------===//
00870   // Utility creation methods
00871   //===--------------------------------------------------------------------===//
00872 
00873   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
00874   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
00875     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
00876                         Name);
00877   }
00878 
00879   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
00880   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
00881     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
00882                         Name);
00883   }
00884 
00885   /// CreatePtrDiff - Return the i64 difference between two pointer values,
00886   /// dividing out the size of the pointed-to objects.  This is intended to
00887   /// implement C-style pointer subtraction. As such, the pointers must be
00888   /// appropriately aligned for their element types and pointing into the
00889   /// same object.
00890   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
00891     assert(LHS->getType() == RHS->getType() &&
00892            "Pointer subtraction operand types must match!");
00893     const PointerType *ArgType = cast<PointerType>(LHS->getType());
00894     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
00895     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
00896     Value *Difference = CreateSub(LHS_int, RHS_int);
00897     return CreateExactSDiv(Difference,
00898                            ConstantExpr::getSizeOf(ArgType->getElementType()),
00899                            Name);
00900   }
00901 };
00902 
00903 }
00904 
00905 #endif