#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; }
///
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));
InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
BasicBlock *UnwindDest, InputIterator ArgBegin,
InputIterator ArgEnd, const char *Name = "") {
- return Insert(TransferAttributes(InvokeInst::Create(Callee,
- NormalDest, UnwindDest,
- ArgBegin, ArgEnd),
- Callee), Name);
+ return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
+ ArgBegin, ArgEnd), Name);
}
UnwindInst *CreateUnwind() {
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(V), Name);
+ 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);
}
//===--------------------------------------------------------------------===//
return Insert(PHINode::Create(Ty), Name);
}
- CallInst *TransferAttributes(CallInst *CI, const Value* Callee) const {
- if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Callee))
- Callee = GA->getAliasedGlobal();
-
- if (const Function *F = dyn_cast<Function>(Callee)) {
- CI->setCallingConv(F->getCallingConv());
- CI->setAttributes(F->getAttributes());
- }
-
- return CI;
- }
-
CallInst *CreateCall(Value *Callee, const char *Name = "") {
- return Insert(TransferAttributes(CallInst::Create(Callee), Callee), Name);
+ return Insert(CallInst::Create(Callee), Name);
}
CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
- return Insert(TransferAttributes(CallInst::Create(Callee, Arg),
- Callee), Name);
+ return Insert(CallInst::Create(Callee, Arg), Name);
}
CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
const char *Name = "") {
Value *Args[] = { Arg1, Arg2 };
- return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+2),
- Callee), Name);
+ return Insert(CallInst::Create(Callee, Args, Args+2), Name);
}
CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
const char *Name = "") {
Value *Args[] = { Arg1, Arg2, Arg3 };
- return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+3),
- Callee), Name);
+ return Insert(CallInst::Create(Callee, Args, Args+3), Name);
}
CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
Value *Arg4, const char *Name = "") {
Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
- return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+4),
- Callee), Name);
+ return Insert(CallInst::Create(Callee, Args, Args+4), Name);
}
template<typename InputIterator>
CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
InputIterator ArgEnd, const char *Name = "") {
- return Insert(TransferAttributes(CallInst::Create(Callee, ArgBegin, ArgEnd),
- Callee), Name);
+ return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
}
Value *CreateSelect(Value *C, Value *True, Value *False,
/// 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);
}
};