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/GlobalVariable.h"
00021 #include "llvm/Function.h"
00022 #include "llvm/Support/ConstantFolder.h"
00023 
00024 namespace llvm {
00025 
00026 /// IRBuilder - This provides a uniform API for creating instructions and
00027 /// inserting them into a basic block: either at the end of a BasicBlock, or
00028 /// at a specific iterator location in a block.
00029 ///
00030 /// Note that the builder does not expose the full generality of LLVM
00031 /// instructions.  For example, it cannot be used to create instructions with
00032 /// arbitrary names (specifically, names with nul characters in them) - It only
00033 /// supports nul-terminated C strings.  For fully generic names, use
00034 /// I->setName().  For access to extra instruction properties, use the mutators
00035 /// (e.g. setVolatile) on the instructions after they have been created.
00036 /// The first template argument handles whether or not to preserve names in the
00037 /// final instruction output. This defaults to on.  The second template argument
00038 /// specifies a class to use for creating constants.  This defaults to creating
00039 /// minimally folded constants.
00040 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
00041   BasicBlock *BB;
00042   BasicBlock::iterator InsertPt;
00043   T Folder;
00044 public:
00045   IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); }
00046   explicit IRBuilder(BasicBlock *TheBB, const T& F = T())
00047     : Folder(F) { SetInsertPoint(TheBB); }
00048   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T())
00049     : Folder(F) { SetInsertPoint(TheBB, IP); }
00050 
00051   /// getFolder - Get the constant folder being used.
00052   const T& getFolder() { return Folder; }
00053 
00054   //===--------------------------------------------------------------------===//
00055   // Builder configuration methods
00056   //===--------------------------------------------------------------------===//
00057 
00058   /// ClearInsertionPoint - Clear the insertion point: created instructions will
00059   /// not be inserted into a block.
00060   void ClearInsertionPoint() {
00061     BB = 0;
00062   }
00063 
00064   BasicBlock *GetInsertBlock() const { return BB; }
00065 
00066   /// SetInsertPoint - This specifies that created instructions should be
00067   /// appended to the end of the specified block.
00068   void SetInsertPoint(BasicBlock *TheBB) {
00069     BB = TheBB;
00070     InsertPt = BB->end();
00071   }
00072 
00073   /// SetInsertPoint - This specifies that created instructions should be
00074   /// inserted at the specified point.
00075   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
00076     BB = TheBB;
00077     InsertPt = IP;
00078   }
00079 
00080   /// Insert - Insert and return the specified instruction.
00081   template<typename InstTy>
00082   InstTy *Insert(InstTy *I, const char *Name = "") const {
00083     InsertHelper(I, Name);
00084     return I;
00085   }
00086 
00087   /// InsertHelper - Insert the specified instruction at the specified insertion
00088   /// point.  This is split out of Insert so that it isn't duplicated for every
00089   /// template instantiation.
00090   void InsertHelper(Instruction *I, const char *Name) const {
00091     if (BB) BB->getInstList().insert(InsertPt, I);
00092     if (preserveNames && Name[0])
00093       I->setName(Name);
00094   }
00095 
00096   //===--------------------------------------------------------------------===//
00097   // Instruction creation methods: Terminators
00098   //===--------------------------------------------------------------------===//
00099 
00100   /// CreateRetVoid - Create a 'ret void' instruction.
00101   ReturnInst *CreateRetVoid() {
00102     return Insert(ReturnInst::Create());
00103   }
00104 
00105   /// @verbatim
00106   /// CreateRet - Create a 'ret <val>' instruction.
00107   /// @endverbatim
00108   ReturnInst *CreateRet(Value *V) {
00109     return Insert(ReturnInst::Create(V));
00110   }
00111 
00112   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
00113   /// with one Value from the retVals array each, that build a aggregate
00114   /// return value one value at a time, and a ret instruction to return
00115   /// the resulting aggregate value. This is a convenience function for
00116   /// code that uses aggregate return values as a vehicle for having
00117   /// multiple return values.
00118   ///
00119   ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
00120     const Type *RetType = BB->getParent()->getReturnType();
00121     Value *V = UndefValue::get(RetType);
00122     for (unsigned i = 0; i != N; ++i)
00123       V = CreateInsertValue(V, retVals[i], i, "mrv");
00124     return Insert(ReturnInst::Create(V));
00125   }
00126 
00127   /// CreateBr - Create an unconditional 'br label X' instruction.
00128   BranchInst *CreateBr(BasicBlock *Dest) {
00129     return Insert(BranchInst::Create(Dest));
00130   }
00131 
00132   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
00133   /// instruction.
00134   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
00135     return Insert(BranchInst::Create(True, False, Cond));
00136   }
00137 
00138   /// CreateSwitch - Create a switch instruction with the specified value,
00139   /// default dest, and with a hint for the number of cases that will be added
00140   /// (for efficient allocation).
00141   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
00142     return Insert(SwitchInst::Create(V, Dest, NumCases));
00143   }
00144 
00145   /// CreateInvoke - Create an invoke instruction.
00146   template<typename InputIterator>
00147   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
00148                            BasicBlock *UnwindDest, InputIterator ArgBegin,
00149                            InputIterator ArgEnd, const char *Name = "") {
00150     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
00151                                      ArgBegin, ArgEnd), Name);
00152   }
00153 
00154   UnwindInst *CreateUnwind() {
00155     return Insert(new UnwindInst());
00156   }
00157 
00158   UnreachableInst *CreateUnreachable() {
00159     return Insert(new UnreachableInst());
00160   }
00161 
00162   //===--------------------------------------------------------------------===//
00163   // Instruction creation methods: Binary Operators
00164   //===--------------------------------------------------------------------===//
00165 
00166   Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
00167     if (Constant *LC = dyn_cast<Constant>(LHS))
00168       if (Constant *RC = dyn_cast<Constant>(RHS))
00169         return Folder.CreateAdd(LC, RC);
00170     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
00171   }
00172   Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
00173     if (Constant *LC = dyn_cast<Constant>(LHS))
00174       if (Constant *RC = dyn_cast<Constant>(RHS))
00175         return Folder.CreateSub(LC, RC);
00176     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
00177   }
00178   Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
00179     if (Constant *LC = dyn_cast<Constant>(LHS))
00180       if (Constant *RC = dyn_cast<Constant>(RHS))
00181         return Folder.CreateMul(LC, RC);
00182     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
00183   }
00184   Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
00185     if (Constant *LC = dyn_cast<Constant>(LHS))
00186       if (Constant *RC = dyn_cast<Constant>(RHS))
00187         return Folder.CreateUDiv(LC, RC);
00188     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
00189   }
00190   Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
00191     if (Constant *LC = dyn_cast<Constant>(LHS))
00192       if (Constant *RC = dyn_cast<Constant>(RHS))
00193         return Folder.CreateSDiv(LC, RC);
00194     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
00195   }
00196   Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
00197     if (Constant *LC = dyn_cast<Constant>(LHS))
00198       if (Constant *RC = dyn_cast<Constant>(RHS))
00199         return Folder.CreateFDiv(LC, RC);
00200     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
00201   }
00202   Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
00203     if (Constant *LC = dyn_cast<Constant>(LHS))
00204       if (Constant *RC = dyn_cast<Constant>(RHS))
00205         return Folder.CreateURem(LC, RC);
00206     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
00207   }
00208   Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
00209     if (Constant *LC = dyn_cast<Constant>(LHS))
00210       if (Constant *RC = dyn_cast<Constant>(RHS))
00211         return Folder.CreateSRem(LC, RC);
00212     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
00213   }
00214   Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
00215     if (Constant *LC = dyn_cast<Constant>(LHS))
00216       if (Constant *RC = dyn_cast<Constant>(RHS))
00217         return Folder.CreateFRem(LC, RC);
00218     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
00219   }
00220   Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
00221     if (Constant *LC = dyn_cast<Constant>(LHS))
00222       if (Constant *RC = dyn_cast<Constant>(RHS))
00223         return Folder.CreateShl(LC, RC);
00224     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
00225   }
00226   Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
00227     if (Constant *LC = dyn_cast<Constant>(LHS))
00228       if (Constant *RC = dyn_cast<Constant>(RHS))
00229         return Folder.CreateLShr(LC, RC);
00230     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
00231   }
00232   Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
00233     if (Constant *LC = dyn_cast<Constant>(LHS))
00234       if (Constant *RC = dyn_cast<Constant>(RHS))
00235         return Folder.CreateAShr(LC, RC);
00236     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
00237   }
00238   Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
00239     if (Constant *LC = dyn_cast<Constant>(LHS))
00240       if (Constant *RC = dyn_cast<Constant>(RHS))
00241         return Folder.CreateAnd(LC, RC);
00242     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
00243   }
00244   Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
00245     if (Constant *LC = dyn_cast<Constant>(LHS))
00246       if (Constant *RC = dyn_cast<Constant>(RHS))
00247         return Folder.CreateOr(LC, RC);
00248     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
00249   }
00250   Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
00251     if (Constant *LC = dyn_cast<Constant>(LHS))
00252       if (Constant *RC = dyn_cast<Constant>(RHS))
00253         return Folder.CreateXor(LC, RC);
00254     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
00255   }
00256 
00257   Value *CreateBinOp(Instruction::BinaryOps Opc,
00258                      Value *LHS, Value *RHS, const char *Name = "") {
00259     if (Constant *LC = dyn_cast<Constant>(LHS))
00260       if (Constant *RC = dyn_cast<Constant>(RHS))
00261         return Folder.CreateBinOp(Opc, LC, RC);
00262     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
00263   }
00264 
00265   Value *CreateNeg(Value *V, const char *Name = "") {
00266     if (Constant *VC = dyn_cast<Constant>(V))
00267       return Folder.CreateNeg(VC);
00268     return Insert(BinaryOperator::CreateNeg(V), Name);
00269   }
00270   Value *CreateNot(Value *V, const char *Name = "") {
00271     if (Constant *VC = dyn_cast<Constant>(V))
00272       return Folder.CreateNot(VC);
00273     return Insert(BinaryOperator::CreateNot(V), Name);
00274   }
00275 
00276   //===--------------------------------------------------------------------===//
00277   // Instruction creation methods: Memory Instructions
00278   //===--------------------------------------------------------------------===//
00279 
00280   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
00281                            const char *Name = "") {
00282     return Insert(new MallocInst(Ty, ArraySize), Name);
00283   }
00284   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
00285                            const char *Name = "") {
00286     return Insert(new AllocaInst(Ty, ArraySize), Name);
00287   }
00288   FreeInst *CreateFree(Value *Ptr) {
00289     return Insert(new FreeInst(Ptr));
00290   }
00291   LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
00292     return Insert(new LoadInst(Ptr), Name);
00293   }
00294   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
00295     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
00296   }
00297   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
00298     return Insert(new StoreInst(Val, Ptr, isVolatile));
00299   }
00300   template<typename InputIterator>
00301   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
00302                    const char *Name = "") {
00303     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
00304       // Every index must be constant.
00305       InputIterator i;
00306       for (i = IdxBegin; i < IdxEnd; ++i) {
00307         if (!dyn_cast<Constant>(*i))
00308           break;
00309       }
00310       if (i == IdxEnd)
00311         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
00312     }
00313     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
00314   }
00315   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
00316     if (Constant *PC = dyn_cast<Constant>(Ptr))
00317       if (Constant *IC = dyn_cast<Constant>(Idx))
00318         return Folder.CreateGetElementPtr(PC, &IC, 1);
00319     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
00320   }
00321   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
00322     llvm::Value *Idxs[] = {
00323       ConstantInt::get(llvm::Type::Int32Ty, 0),
00324       ConstantInt::get(llvm::Type::Int32Ty, Idx)
00325     };
00326 
00327     if (Constant *PC = dyn_cast<Constant>(Ptr))
00328       return Folder.CreateGetElementPtr(PC, Idxs, 2);
00329 
00330     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
00331   }
00332   Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
00333     Constant *StrConstant = ConstantArray::get(Str, true);
00334     GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
00335                                                   true,
00336                                                   GlobalValue::InternalLinkage,
00337                                                   StrConstant,
00338                                                   "",
00339                                                   BB->getParent()->getParent(),
00340                                                   false);
00341     gv->setName(Name);
00342     return gv;
00343   }
00344   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
00345     Value *gv = CreateGlobalString(Str, Name);
00346     Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
00347     Value *Args[] = { zero, zero };
00348     return CreateGEP(gv, Args, Args+2, Name);
00349   }
00350   //===--------------------------------------------------------------------===//
00351   // Instruction creation methods: Cast/Conversion Operators
00352   //===--------------------------------------------------------------------===//
00353 
00354   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
00355     return CreateCast(Instruction::Trunc, V, DestTy, Name);
00356   }
00357   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
00358     return CreateCast(Instruction::ZExt, V, DestTy, Name);
00359   }
00360   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
00361     return CreateCast(Instruction::SExt, V, DestTy, Name);
00362   }
00363   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
00364     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
00365   }
00366   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
00367     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
00368   }
00369   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
00370     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
00371   }
00372   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
00373     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
00374   }
00375   Value *CreateFPTrunc(Value *V, const Type *DestTy,
00376                        const char *Name = "") {
00377     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
00378   }
00379   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
00380     return CreateCast(Instruction::FPExt, V, DestTy, Name);
00381   }
00382   Value *CreatePtrToInt(Value *V, const Type *DestTy,
00383                         const char *Name = "") {
00384     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
00385   }
00386   Value *CreateIntToPtr(Value *V, const Type *DestTy,
00387                         const char *Name = "") {
00388     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
00389   }
00390   Value *CreateBitCast(Value *V, const Type *DestTy,
00391                        const char *Name = "") {
00392     return CreateCast(Instruction::BitCast, V, DestTy, Name);
00393   }
00394 
00395   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
00396                     const char *Name = "") {
00397     if (V->getType() == DestTy)
00398       return V;
00399     if (Constant *VC = dyn_cast<Constant>(V))
00400       return Folder.CreateCast(Op, VC, DestTy);
00401     return Insert(CastInst::Create(Op, V, DestTy), Name);
00402   }
00403   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
00404                        const char *Name = "") {
00405     if (V->getType() == DestTy)
00406       return V;
00407     if (Constant *VC = dyn_cast<Constant>(V))
00408       return Folder.CreateIntCast(VC, DestTy, isSigned);
00409     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
00410   }
00411 
00412   //===--------------------------------------------------------------------===//
00413   // Instruction creation methods: Compare Instructions
00414   //===--------------------------------------------------------------------===//
00415 
00416   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
00417     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
00418   }
00419   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
00420     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
00421   }
00422   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
00423     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
00424   }
00425   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
00426     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
00427   }
00428   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
00429     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
00430   }
00431   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
00432     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
00433   }
00434   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
00435     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
00436   }
00437   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
00438     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
00439   }
00440   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
00441     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
00442   }
00443   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
00444     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
00445   }
00446 
00447   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
00448     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
00449   }
00450   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
00451     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
00452   }
00453   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
00454     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
00455   }
00456   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
00457     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
00458   }
00459   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
00460     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
00461   }
00462   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
00463     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
00464   }
00465   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
00466     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
00467   }
00468   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
00469     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
00470   }
00471   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
00472     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
00473   }
00474   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
00475     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
00476   }
00477   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
00478     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
00479   }
00480   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
00481     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
00482   }
00483   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
00484     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
00485   }
00486   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
00487     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
00488   }
00489 
00490   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00491                     const char *Name = "") {
00492     if (Constant *LC = dyn_cast<Constant>(LHS))
00493       if (Constant *RC = dyn_cast<Constant>(RHS))
00494         return Folder.CreateICmp(P, LC, RC);
00495     return Insert(new ICmpInst(P, LHS, RHS), Name);
00496   }
00497   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00498                     const char *Name = "") {
00499     if (Constant *LC = dyn_cast<Constant>(LHS))
00500       if (Constant *RC = dyn_cast<Constant>(RHS))
00501         return Folder.CreateFCmp(P, LC, RC);
00502     return Insert(new FCmpInst(P, LHS, RHS), Name);
00503   }
00504 
00505   Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00506                      const char *Name = "") {
00507     if (Constant *LC = dyn_cast<Constant>(LHS))
00508       if (Constant *RC = dyn_cast<Constant>(RHS))
00509         return Folder.CreateVICmp(P, LC, RC);
00510     return Insert(new VICmpInst(P, LHS, RHS), Name);
00511   }
00512   Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
00513                      const char *Name = "") {
00514     if (Constant *LC = dyn_cast<Constant>(LHS))
00515       if (Constant *RC = dyn_cast<Constant>(RHS))
00516         return Folder.CreateVFCmp(P, LC, RC);
00517     return Insert(new VFCmpInst(P, LHS, RHS), Name);
00518   }
00519 
00520   //===--------------------------------------------------------------------===//
00521   // Instruction creation methods: Other Instructions
00522   //===--------------------------------------------------------------------===//
00523 
00524   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
00525     return Insert(PHINode::Create(Ty), Name);
00526   }
00527 
00528   CallInst *CreateCall(Value *Callee, const char *Name = "") {
00529     return Insert(CallInst::Create(Callee), Name);
00530   }
00531   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
00532     return Insert(CallInst::Create(Callee, Arg), Name);
00533   }
00534   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
00535                         const char *Name = "") {
00536     Value *Args[] = { Arg1, Arg2 };
00537     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
00538   }
00539   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
00540                         const char *Name = "") {
00541     Value *Args[] = { Arg1, Arg2, Arg3 };
00542     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
00543   }
00544   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
00545                         Value *Arg4, const char *Name = "") {
00546     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
00547     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
00548   }
00549 
00550   template<typename InputIterator>
00551   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
00552                        InputIterator ArgEnd, const char *Name = "") {
00553     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
00554   }
00555 
00556   Value *CreateSelect(Value *C, Value *True, Value *False,
00557                       const char *Name = "") {
00558     if (Constant *CC = dyn_cast<Constant>(C))
00559       if (Constant *TC = dyn_cast<Constant>(True))
00560         if (Constant *FC = dyn_cast<Constant>(False))
00561           return Folder.CreateSelect(CC, TC, FC);
00562     return Insert(SelectInst::Create(C, True, False), Name);
00563   }
00564 
00565   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
00566     return Insert(new VAArgInst(List, Ty), Name);
00567   }
00568 
00569   Value *CreateExtractElement(Value *Vec, Value *Idx,
00570                               const char *Name = "") {
00571     if (Constant *VC = dyn_cast<Constant>(Vec))
00572       if (Constant *IC = dyn_cast<Constant>(Idx))
00573         return Folder.CreateExtractElement(VC, IC);
00574     return Insert(new ExtractElementInst(Vec, Idx), Name);
00575   }
00576 
00577   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
00578                              const char *Name = "") {
00579     if (Constant *VC = dyn_cast<Constant>(Vec))
00580       if (Constant *NC = dyn_cast<Constant>(NewElt))
00581         if (Constant *IC = dyn_cast<Constant>(Idx))
00582           return Folder.CreateInsertElement(VC, NC, IC);
00583     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
00584   }
00585 
00586   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
00587                              const char *Name = "") {
00588     if (Constant *V1C = dyn_cast<Constant>(V1))
00589       if (Constant *V2C = dyn_cast<Constant>(V2))
00590         if (Constant *MC = dyn_cast<Constant>(Mask))
00591           return Folder.CreateShuffleVector(V1C, V2C, MC);
00592     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
00593   }
00594 
00595   Value *CreateExtractValue(Value *Agg, unsigned Idx,
00596                             const char *Name = "") {
00597     if (Constant *AggC = dyn_cast<Constant>(Agg))
00598       return Folder.CreateExtractValue(AggC, &Idx, 1);
00599     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
00600   }
00601 
00602   template<typename InputIterator>
00603   Value *CreateExtractValue(Value *Agg,
00604                             InputIterator IdxBegin,
00605                             InputIterator IdxEnd,
00606                             const char *Name = "") {
00607     if (Constant *AggC = dyn_cast<Constant>(Agg))
00608       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
00609     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
00610   }
00611 
00612   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
00613                            const char *Name = "") {
00614     if (Constant *AggC = dyn_cast<Constant>(Agg))
00615       if (Constant *ValC = dyn_cast<Constant>(Val))
00616         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
00617     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
00618   }
00619 
00620   template<typename InputIterator>
00621   Value *CreateInsertValue(Value *Agg, Value *Val,
00622                            InputIterator IdxBegin,
00623                            InputIterator IdxEnd,
00624                            const char *Name = "") {
00625     if (Constant *AggC = dyn_cast<Constant>(Agg))
00626       if (Constant *ValC = dyn_cast<Constant>(Val))
00627         return Folder.CreateInsertValue(AggC, ValC,
00628                                             IdxBegin, IdxEnd - IdxBegin);
00629     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
00630   }
00631 
00632   //===--------------------------------------------------------------------===//
00633   // Utility creation methods
00634   //===--------------------------------------------------------------------===//
00635 
00636   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
00637   Value *CreateIsNull(Value *Arg, const char *Name = "") {
00638     return CreateICmpEQ(Arg, llvm::Constant::getNullValue(Arg->getType()), 
00639                         Name);
00640   }
00641 
00642   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
00643   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
00644     return CreateICmpNE(Arg, llvm::Constant::getNullValue(Arg->getType()), 
00645                         Name);
00646   }
00647   
00648 };
00649 
00650 }
00651 
00652 #endif