#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 {
template <bool preserveNames=true, typename T = ConstantFolder> 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; }
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) {
///
ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
const Type *RetType = BB->getParent()->getReturnType();
- Value *V = UndefValue::get(RetType);
+ Value *V = Context.getUndef(RetType);
for (unsigned i = 0; i != N; ++i)
V = CreateInsertValue(V, retVals[i], i, "mrv");
return Insert(ReturnInst::Create(V));
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<Constant>(LHS))
+ if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
+ if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
+ if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
Value *CreateNeg(Value *V, const char *Name = "") {
if (Constant *VC = dyn_cast<Constant>(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<Constant>(V))
+ return Folder.CreateFNeg(VC);
+ return Insert(BinaryOperator::CreateFNeg(Context, V), Name);
}
Value *CreateNot(Value *V, const char *Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))
return Folder.CreateNot(VC);
- return Insert(BinaryOperator::CreateNot(V), Name);
+ return Insert(BinaryOperator::CreateNot(Context, V), Name);
}
//===--------------------------------------------------------------------===//
return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
}
Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
- Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
+ Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0);
if (Constant *PC = dyn_cast<Constant>(Ptr))
return Folder.CreateGetElementPtr(PC, &Idx, 1);
Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
const char *Name = "") {
Value *Idxs[] = {
- ConstantInt::get(Type::Int32Ty, Idx0),
- ConstantInt::get(Type::Int32Ty, Idx1)
+ Context.getConstantInt(Type::Int32Ty, Idx0),
+ Context.getConstantInt(Type::Int32Ty, Idx1)
};
if (Constant *PC = dyn_cast<Constant>(Ptr))
return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
}
Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
- Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
+ Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0);
if (Constant *PC = dyn_cast<Constant>(Ptr))
return Folder.CreateGetElementPtr(PC, &Idx, 1);
Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
const char *Name = "") {
Value *Idxs[] = {
- ConstantInt::get(Type::Int64Ty, Idx0),
- ConstantInt::get(Type::Int64Ty, Idx1)
+ Context.getConstantInt(Type::Int64Ty, Idx0),
+ Context.getConstantInt(Type::Int64Ty, Idx1)
};
if (Constant *PC = dyn_cast<Constant>(Ptr))
return CreateConstGEP2_32(Ptr, 0, Idx, Name);
}
Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
- Constant *StrConstant = ConstantArray::get(Str, true);
- GlobalVariable *gv = new GlobalVariable(StrConstant->getType(),
+ Constant *StrConstant = Context.getConstantArray(Str, true);
+ Module &M = *BB->getParent()->getParent();
+ GlobalVariable *gv = new GlobalVariable(M,
+ StrConstant->getType(),
true,
GlobalValue::InternalLinkage,
StrConstant,
"",
- BB->getParent()->getParent(),
+ 0,
false);
gv->setName(Name);
return gv;
}
Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
Value *gv = CreateGlobalString(Str, Name);
- Value *zero = ConstantInt::get(Type::Int32Ty, 0);
+ Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
Value *Args[] = { zero, zero };
return CreateGEP(gv, Args, Args+2, Name);
}
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
- if (Constant *RC = dyn_cast<Constant>(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<Constant>(LHS))
- if (Constant *RC = dyn_cast<Constant>(RHS))
- return Folder.CreateVFCmp(P, LC, RC);
- return Insert(new VFCmpInst(P, LHS, RHS), Name);
+ return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
}
//===--------------------------------------------------------------------===//
/// CreateIsNull - Return an i1 value testing if \arg Arg is null.
Value *CreateIsNull(Value *Arg, const char *Name = "") {
- return CreateICmpEQ(Arg, 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, Constant::getNullValue(Arg->getType()),
+ return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
Name);
}
Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
Value *Difference = CreateSub(LHS_int, RHS_int);
return CreateSDiv(Difference,
- ConstantExpr::getSizeOf(ArgType->getElementType()),
+ Context.getConstantExprSizeOf(ArgType->getElementType()),
Name);
}
};