X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FIRBuilder.h;h=223fe028e209376013a4f8a502c058ec765f2c8e;hb=9e9a0d5fc26878e51a58a8b57900fcbf952c2691;hp=a4ae671e463ae7208d52a4c3b2f76256a3d6d7ad;hpb=4cc2b85861f362e49205710c19266a8ffed1a0d9;p=oota-llvm.git diff --git a/include/llvm/Support/IRBuilder.h b/include/llvm/Support/IRBuilder.h index a4ae671e463..223fe028e20 100644 --- a/include/llvm/Support/IRBuilder.h +++ b/include/llvm/Support/IRBuilder.h @@ -17,8 +17,10 @@ #include "llvm/Constants.h" #include "llvm/Instructions.h" +#include "llvm/GlobalAlias.h" #include "llvm/GlobalVariable.h" #include "llvm/Function.h" +#include "llvm/LLVMContext.h" #include "llvm/Support/ConstantFolder.h" namespace llvm { @@ -40,13 +42,33 @@ namespace llvm { template class IRBuilder{ BasicBlock *BB; BasicBlock::iterator InsertPt; + LLVMContext &Context; T Folder; public: - IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); } - explicit IRBuilder(BasicBlock *TheBB, const T& F = T()) - : Folder(F) { SetInsertPoint(TheBB); } - IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T()) - : Folder(F) { SetInsertPoint(TheBB, IP); } + IRBuilder(LLVMContext &C, const T& F) : + Context(C), Folder(F) { ClearInsertionPoint(); } + + IRBuilder(LLVMContext &C) : Context(C), Folder(C) { ClearInsertionPoint(); } + + explicit IRBuilder(BasicBlock *TheBB, const T& F) + : Context(TheBB->getContext()), Folder(F) { + SetInsertPoint(TheBB); + } + + explicit IRBuilder(BasicBlock *TheBB) + : Context(TheBB->getContext()), Folder(Context) { + SetInsertPoint(TheBB); + } + + IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F) + : Context(TheBB->getContext()), Folder(F) { + SetInsertPoint(TheBB, IP); + } + + IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) + : Context(TheBB->getContext()), Folder(Context) { + SetInsertPoint(TheBB, IP); + } /// getFolder - Get the constant folder being used. const T& getFolder() { return Folder; } @@ -67,6 +89,8 @@ public: BasicBlock *GetInsertBlock() const { return BB; } + BasicBlock::iterator GetInsertPoint() const { return InsertPt; } + /// SetInsertPoint - This specifies that created instructions should be /// appended to the end of the specified block. void SetInsertPoint(BasicBlock *TheBB) { @@ -173,18 +197,36 @@ public: return Folder.CreateAdd(LC, RC); return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name); } + Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") { + if (Constant *LC = dyn_cast(LHS)) + if (Constant *RC = dyn_cast(RHS)) + return Folder.CreateFAdd(LC, RC); + return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name); + } Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") { if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Folder.CreateSub(LC, RC); return Insert(BinaryOperator::CreateSub(LHS, RHS), Name); } + Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") { + if (Constant *LC = dyn_cast(LHS)) + if (Constant *RC = dyn_cast(RHS)) + return Folder.CreateFSub(LC, RC); + return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name); + } Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") { if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Folder.CreateMul(LC, RC); return Insert(BinaryOperator::CreateMul(LHS, RHS), Name); } + Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") { + if (Constant *LC = dyn_cast(LHS)) + if (Constant *RC = dyn_cast(RHS)) + return Folder.CreateFMul(LC, RC); + return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name); + } Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") { if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) @@ -269,12 +311,17 @@ public: Value *CreateNeg(Value *V, const char *Name = "") { if (Constant *VC = dyn_cast(V)) return Folder.CreateNeg(VC); - return Insert(BinaryOperator::CreateNeg(V), Name); + return Insert(BinaryOperator::CreateNeg(Context, V), Name); + } + Value *CreateFNeg(Value *V, const char *Name = "") { + if (Constant *VC = dyn_cast(V)) + return Folder.CreateFNeg(VC); + return Insert(BinaryOperator::CreateFNeg(Context, V), Name); } Value *CreateNot(Value *V, const char *Name = "") { if (Constant *VC = dyn_cast(V)) return Folder.CreateNot(VC); - return Insert(BinaryOperator::CreateNot(V), Name); + return Insert(BinaryOperator::CreateNot(Context, V), Name); } //===--------------------------------------------------------------------===// @@ -323,7 +370,7 @@ public: return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); } Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") { - llvm::Value *Idx = ConstantInt::get(llvm::Type::Int32Ty, Idx0); + Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0); if (Constant *PC = dyn_cast(Ptr)) return Folder.CreateGetElementPtr(PC, &Idx, 1); @@ -332,9 +379,9 @@ public: } Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, const char *Name = "") { - llvm::Value *Idxs[] = { - ConstantInt::get(llvm::Type::Int32Ty, Idx0), - ConstantInt::get(llvm::Type::Int32Ty, Idx1) + Value *Idxs[] = { + ConstantInt::get(Type::Int32Ty, Idx0), + ConstantInt::get(Type::Int32Ty, Idx1) }; if (Constant *PC = dyn_cast(Ptr)) @@ -343,7 +390,7 @@ public: return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); } Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") { - llvm::Value *Idx = ConstantInt::get(llvm::Type::Int64Ty, Idx0); + Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0); if (Constant *PC = dyn_cast(Ptr)) return Folder.CreateGetElementPtr(PC, &Idx, 1); @@ -352,9 +399,9 @@ public: } Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, const char *Name = "") { - llvm::Value *Idxs[] = { - ConstantInt::get(llvm::Type::Int64Ty, Idx0), - ConstantInt::get(llvm::Type::Int64Ty, Idx1) + Value *Idxs[] = { + ConstantInt::get(Type::Int64Ty, Idx0), + ConstantInt::get(Type::Int64Ty, Idx1) }; if (Constant *PC = dyn_cast(Ptr)) @@ -367,19 +414,21 @@ public: } Value *CreateGlobalString(const char *Str = "", const char *Name = "") { Constant *StrConstant = ConstantArray::get(Str, true); - GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(), - true, - GlobalValue::InternalLinkage, - StrConstant, - "", - BB->getParent()->getParent(), - false); + Module &M = *BB->getParent()->getParent(); + GlobalVariable *gv = new GlobalVariable(M, + StrConstant->getType(), + true, + GlobalValue::InternalLinkage, + StrConstant, + "", + 0, + false); gv->setName(Name); return gv; } Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") { Value *gv = CreateGlobalString(Str, Name); - Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); + Value *zero = ConstantInt::get(Type::Int32Ty, 0); Value *Args[] = { zero, zero }; return CreateGEP(gv, Args, Args+2, Name); } @@ -528,29 +577,14 @@ public: if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Folder.CreateICmp(P, LC, RC); - return Insert(new ICmpInst(P, LHS, RHS), Name); + return Insert(new ICmpInst(Context, P, LHS, RHS), Name); } Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const char *Name = "") { if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Folder.CreateFCmp(P, LC, RC); - return Insert(new FCmpInst(P, LHS, RHS), Name); - } - - Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, - const char *Name = "") { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Folder.CreateVICmp(P, LC, RC); - return Insert(new VICmpInst(P, LHS, RHS), Name); - } - Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, - const char *Name = "") { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Folder.CreateVFCmp(P, LC, RC); - return Insert(new VFCmpInst(P, LHS, RHS), Name); + return Insert(new FCmpInst(Context, P, LHS, RHS), Name); } //===--------------------------------------------------------------------===// @@ -607,7 +641,7 @@ public: if (Constant *VC = dyn_cast(Vec)) if (Constant *IC = dyn_cast(Idx)) return Folder.CreateExtractElement(VC, IC); - return Insert(new ExtractElementInst(Vec, Idx), Name); + return Insert(ExtractElementInst::Create(Vec, Idx), Name); } Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, @@ -671,16 +705,30 @@ public: /// CreateIsNull - Return an i1 value testing if \arg Arg is null. Value *CreateIsNull(Value *Arg, const char *Name = "") { - return CreateICmpEQ(Arg, llvm::Constant::getNullValue(Arg->getType()), + return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()), Name); } /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null. Value *CreateIsNotNull(Value *Arg, const char *Name = "") { - return CreateICmpNE(Arg, llvm::Constant::getNullValue(Arg->getType()), + return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()), Name); } + /// CreatePtrDiff - Return the i64 difference between two pointer values, + /// dividing out the size of the pointed-to objects. This is intended to + /// implement C-style pointer subtraction. + Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") { + assert(LHS->getType() == RHS->getType() && + "Pointer subtraction operand types must match!"); + const PointerType *ArgType = cast(LHS->getType()); + Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty); + Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty); + Value *Difference = CreateSub(LHS_int, RHS_int); + return CreateSDiv(Difference, + ConstantExpr::getSizeOf(ArgType->getElementType()), + Name); + } }; }