More LLVMContext-ification.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74807 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-07-05 22:41:43 +00:00
parent db882950f3
commit 0a205a4598
26 changed files with 162 additions and 98 deletions

View File

@ -22,6 +22,7 @@
namespace llvm {
class TerminatorInst;
class LLVMContext;
template<> struct ilist_traits<Instruction>
: public SymbolTableListTraits<Instruction, BasicBlock> {
@ -85,6 +86,10 @@ private:
explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
BasicBlock *InsertBefore = 0);
public:
/// getContext - Get the context in which this basic block lives,
/// or null if it is not currently attached to a function.
LLVMContext* getContext() const;
/// Instruction iterators...
typedef InstListType::iterator iterator;
typedef InstListType::const_iterator const_iterator;

View File

@ -129,7 +129,7 @@ public:
/// getContext - Return a pointer to the LLVMContext associated with this
/// function, or NULL if this function is not bound to a context yet.
LLVMContext* getContext();
LLVMContext* getContext() const;
/// isVarArg - Return true if this function takes a variable number of
/// arguments.

View File

@ -166,6 +166,7 @@ public:
Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
const unsigned* IdxList,
unsigned NumIdx);
Constant* getConstantExprSizeOf(const Type* Ty);
Constant* getZeroValueForNegation(const Type* Ty);
// ConstantFP accessors
@ -188,6 +189,7 @@ public:
MDString* getMDString(const std::string &Str);
// FunctionType accessors
FunctionType* getFunctionType(const Type* Result, bool isVarArg);
FunctionType* getFunctionType(const Type* Result,
const std::vector<const Type*>& Params,
bool isVarArg);

View File

@ -23,6 +23,7 @@ class AllocaInst;
class DominatorTree;
class DominanceFrontier;
class AliasSetTracker;
class LLVMContext;
/// isAllocaPromotable - Return true if this alloca is legal for promotion.
/// This is true if there are only loads and stores to the alloca...
@ -39,6 +40,7 @@ bool isAllocaPromotable(const AllocaInst *AI);
///
void PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
DominatorTree &DT, DominanceFrontier &DF,
LLVMContext* Context,
AliasSetTracker *AST = 0);
} // End llvm namespace

View File

@ -20,9 +20,10 @@
namespace llvm {
class Value;
class Instruction;
class LLVMContext;
typedef DenseMap<const Value *, Value *> ValueMapTy;
Value *MapValue(const Value *V, ValueMapTy &VM);
Value *MapValue(const Value *V, ValueMapTy &VM, LLVMContext* Context);
void RemapInstruction(Instruction *I, ValueMapTy &VM);
} // End llvm namespace

View File

@ -314,7 +314,7 @@ bool DwarfEHPrepare::PromoteStackTemporaries() {
if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) {
// Turn the exception temporary into registers and phi nodes if possible.
std::vector<AllocaInst*> Allocas(1, ExceptionValueVar);
PromoteMemToReg(Allocas, *DT, *DF);
PromoteMemToReg(Allocas, *DT, *DF, Context);
return true;
}
return false;

View File

@ -593,7 +593,7 @@ void LICM::sink(Instruction &I) {
if (AI) {
std::vector<AllocaInst*> Allocas;
Allocas.push_back(AI);
PromoteMemToReg(Allocas, *DT, *DF, CurAST);
PromoteMemToReg(Allocas, *DT, *DF, Context, CurAST);
}
}
}
@ -773,7 +773,7 @@ void LICM::PromoteValuesInLoop() {
PromotedAllocas.reserve(PromotedValues.size());
for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i)
PromotedAllocas.push_back(PromotedValues[i].first);
PromoteMemToReg(PromotedAllocas, *DT, *DF, CurAST);
PromoteMemToReg(PromotedAllocas, *DT, *DF, Context, CurAST);
}
/// FindPromotableValuesInLoop - Check the current loop for stores to definite

View File

@ -186,7 +186,7 @@ bool SROA::performPromotion(Function &F) {
if (Allocas.empty()) break;
PromoteMemToReg(Allocas, DT, DF);
PromoteMemToReg(Allocas, DT, DF, Context);
NumPromoted += Allocas.size();
Changed = true;
}

View File

@ -16,6 +16,7 @@
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Constant.h"
#include "llvm/Type.h"
#include "llvm/Analysis/AliasAnalysis.h"
@ -49,7 +50,7 @@ void llvm::DeleteDeadBlock(BasicBlock *BB) {
// contained within it must dominate their uses, that all uses will
// eventually be removed (they are themselves dead).
if (!I.use_empty())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
I.replaceAllUsesWith(BB->getContext()->getUndef(I.getType()));
BB->getInstList().pop_back();
}
@ -69,7 +70,7 @@ void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) {
if (PN->getIncomingValue(0) != PN)
PN->replaceAllUsesWith(PN->getIncomingValue(0));
else
PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
PN->replaceAllUsesWith(BB->getContext()->getUndef(PN->getType()));
PN->eraseFromParent();
}
}
@ -250,7 +251,8 @@ void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
// Create a value to return... if the function doesn't return null...
if (BB->getParent()->getReturnType() != Type::VoidTy)
RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
RetVal = TI->getParent()->getContext()->getNullValue(
BB->getParent()->getReturnType());
// Create the return...
NewTI = ReturnInst::Create(RetVal);
@ -385,7 +387,8 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
if (NumPreds == 0) {
// Insert dummy values as the incoming value.
for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
cast<PHINode>(I)->addIncoming(BB->getContext()->getUndef(I->getType()),
NewBB);
return NewBB;
}

View File

@ -20,6 +20,7 @@
#include "llvm/IntrinsicInst.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Function.h"
#include "llvm/LLVMContext.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
@ -150,7 +151,8 @@ Function *llvm::CloneFunction(const Function *F,
ArgTypes.push_back(I->getType());
// Create a new function type...
FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
FunctionType *FTy =
F->getContext()->getFunctionType(F->getFunctionType()->getReturnType(),
ArgTypes, F->getFunctionType()->isVarArg());
// Create the new function...
@ -323,10 +325,13 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
/// mapping its operands through ValueMap if they are available.
Constant *PruningFunctionCloner::
ConstantFoldMappedInstruction(const Instruction *I) {
LLVMContext* Context = I->getParent()->getContext();
SmallVector<Constant*, 8> Ops;
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
ValueMap)))
ValueMap,
Context)))
Ops.push_back(Op);
else
return 0; // All operands not constant!
@ -361,6 +366,7 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
ClonedCodeInfo *CodeInfo,
const TargetData *TD) {
assert(NameSuffix && "NameSuffix cannot be null!");
LLVMContext* Context = OldFunc->getContext();
#ifndef NDEBUG
for (Function::const_arg_iterator II = OldFunc->arg_begin(),
@ -430,7 +436,8 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
if (BasicBlock *MappedBlock =
cast_or_null<BasicBlock>(ValueMap[PN->getIncomingBlock(pred)])) {
Value *InVal = MapValue(PN->getIncomingValue(pred), ValueMap);
Value *InVal = MapValue(PN->getIncomingValue(pred),
ValueMap, Context);
assert(InVal && "Unknown input value?");
PN->setIncomingValue(pred, InVal);
PN->setIncomingBlock(pred, MappedBlock);
@ -482,7 +489,7 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
BasicBlock::iterator I = NewBB->begin();
BasicBlock::const_iterator OldI = OldBB->begin();
while ((PN = dyn_cast<PHINode>(I++))) {
Value *NV = UndefValue::get(PN->getType());
Value *NV = OldFunc->getContext()->getUndef(PN->getType());
PN->replaceAllUsesWith(NV);
assert(ValueMap[OldI] == PN && "ValueMap mismatch");
ValueMap[OldI] = NV;

View File

@ -88,7 +88,8 @@ Module *llvm::CloneModule(const Module *M,
GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
if (I->hasInitializer())
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
ValueMap)));
ValueMap,
&M->getContext())));
GV->setLinkage(I->getLinkage());
GV->setThreadLocal(I->isThreadLocal());
GV->setConstant(I->isConstant());
@ -119,7 +120,7 @@ Module *llvm::CloneModule(const Module *M,
GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
GA->setLinkage(I->getLinkage());
if (const Constant* C = I->getAliasee())
GA->setAliasee(cast<Constant>(MapValue(C, ValueMap)));
GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, &M->getContext())));
}
return New;

View File

@ -18,6 +18,7 @@
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/Dominators.h"
@ -237,6 +238,8 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
DOUT << "inputs: " << inputs.size() << "\n";
DOUT << "outputs: " << outputs.size() << "\n";
LLVMContext* Context = header->getContext();
// This function returns unsigned, outputs will go back by reference.
switch (NumExitBlocks) {
case 0:
@ -262,7 +265,8 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
if (AggregateArgs)
paramTy.push_back((*I)->getType());
else
paramTy.push_back(PointerType::getUnqual((*I)->getType()));
paramTy.push_back(
header->getContext()->getPointerTypeUnqual((*I)->getType()));
}
DOUT << "Function type: " << *RetTy << " f(";
@ -272,11 +276,13 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
DOUT << ")\n";
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
PointerType *StructPtr = PointerType::getUnqual(StructType::get(paramTy));
PointerType *StructPtr =
Context->getPointerTypeUnqual(Context->getStructType(paramTy));
paramTy.clear();
paramTy.push_back(StructPtr);
}
const FunctionType *funcType = FunctionType::get(RetTy, paramTy, false);
const FunctionType *funcType =
Context->getFunctionType(RetTy, paramTy, false);
// Create the new function
Function *newFunction = Function::Create(funcType,
@ -298,8 +304,8 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
Value *RewriteVal;
if (AggregateArgs) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = ConstantInt::get(Type::Int32Ty, i);
Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = Context->getConstantInt(Type::Int32Ty, i);
std::string GEPname = "gep_" + inputs[i]->getName();
TerminatorInst *TI = newFunction->begin()->getTerminator();
GetElementPtrInst *GEP = GetElementPtrInst::Create(AI, Idx, Idx+2,
@ -346,6 +352,8 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
void CodeExtractor::
emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
Values &inputs, Values &outputs) {
LLVMContext* Context = codeReplacer->getContext();
// Emit a call to the new function, passing in: *pointer to struct (if
// aggregating parameters), or plan inputs and allocated memory for outputs
std::vector<Value*> params, StructValues, ReloadOutputs;
@ -378,7 +386,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
ArgTypes.push_back((*v)->getType());
// Allocate a struct at the beginning of this function
Type *StructArgTy = StructType::get(ArgTypes);
Type *StructArgTy = Context->getStructType(ArgTypes);
Struct =
new AllocaInst(StructArgTy, 0, "structArg",
codeReplacer->getParent()->begin()->begin());
@ -386,8 +394,8 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = ConstantInt::get(Type::Int32Ty, i);
Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = Context->getConstantInt(Type::Int32Ty, i);
GetElementPtrInst *GEP =
GetElementPtrInst::Create(Struct, Idx, Idx + 2,
"gep_" + StructValues[i]->getName());
@ -412,8 +420,8 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
Value *Output = 0;
if (AggregateArgs) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = ConstantInt::get(Type::Int32Ty, FirstOut + i);
Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = Context->getConstantInt(Type::Int32Ty, FirstOut + i);
GetElementPtrInst *GEP
= GetElementPtrInst::Create(Struct, Idx, Idx + 2,
"gep_reload_" + outputs[i]->getName());
@ -434,7 +442,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
// Now we can emit a switch statement using the call as a value.
SwitchInst *TheSwitch =
SwitchInst::Create(ConstantInt::getNullValue(Type::Int16Ty),
SwitchInst::Create(Context->getNullValue(Type::Int16Ty),
codeReplacer, 0, codeReplacer);
// Since there may be multiple exits from the original region, make the new
@ -465,17 +473,17 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
case 0:
case 1: break; // No value needed.
case 2: // Conditional branch, return a bool
brVal = ConstantInt::get(Type::Int1Ty, !SuccNum);
brVal = Context->getConstantInt(Type::Int1Ty, !SuccNum);
break;
default:
brVal = ConstantInt::get(Type::Int16Ty, SuccNum);
brVal = Context->getConstantInt(Type::Int16Ty, SuccNum);
break;
}
ReturnInst *NTRet = ReturnInst::Create(brVal, NewTarget);
// Update the switch instruction.
TheSwitch->addCase(ConstantInt::get(Type::Int16Ty, SuccNum),
TheSwitch->addCase(Context->getConstantInt(Type::Int16Ty, SuccNum),
OldTarget);
// Restore values just before we exit
@ -513,8 +521,8 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
if (DominatesDef) {
if (AggregateArgs) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = ConstantInt::get(Type::Int32Ty,FirstOut+out);
Idx[0] = Context->getNullValue(Type::Int32Ty);
Idx[1] = Context->getConstantInt(Type::Int32Ty,FirstOut+out);
GetElementPtrInst *GEP =
GetElementPtrInst::Create(OAI, Idx, Idx + 2,
"gep_" + outputs[out]->getName(),
@ -551,7 +559,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
} else {
// Otherwise we must have code extracted an unwind or something, just
// return whatever we want.
ReturnInst::Create(Constant::getNullValue(OldFnRetTy), TheSwitch);
ReturnInst::Create(Context->getNullValue(OldFnRetTy), TheSwitch);
}
TheSwitch->eraseFromParent();

View File

@ -15,6 +15,7 @@
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
@ -238,6 +239,7 @@ static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) {
//
bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
Instruction *TheCall = CS.getInstruction();
LLVMContext *Context = TheCall->getParent()->getContext();
assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
"Instruction not in function!");
@ -302,7 +304,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
!CalledFunc->onlyReadsMemory()) {
const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty);
// Create the alloca. If we have TargetData, use nice alignment.
unsigned Align = 1;
@ -319,15 +321,16 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
Value *Size;
if (TD == 0)
Size = ConstantExpr::getSizeOf(AggTy);
Size = Context->getConstantExprSizeOf(AggTy);
else
Size = ConstantInt::get(Type::Int64Ty, TD->getTypeStoreSize(AggTy));
Size = Context->getConstantInt(Type::Int64Ty,
TD->getTypeStoreSize(AggTy));
// Always generate a memcpy of alignment 1 here because we don't know
// the alignment of the src pointer. Other optimizations can infer
// better alignment.
Value *CallArgs[] = {
DestCast, SrcCast, Size, ConstantInt::get(Type::Int32Ty, 1)
DestCast, SrcCast, Size, Context->getConstantInt(Type::Int32Ty, 1)
};
CallInst *TheMemCpy =
CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
@ -517,7 +520,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
if (!TheCall->use_empty()) {
ReturnInst *R = Returns[0];
if (TheCall == R->getReturnValue())
TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
else
TheCall->replaceAllUsesWith(R->getReturnValue());
}
@ -610,7 +613,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
// using the return value of the call with the computed value.
if (!TheCall->use_empty()) {
if (TheCall == Returns[0]->getReturnValue())
TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
else
TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
}
@ -630,7 +633,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
} else if (!TheCall->use_empty()) {
// No returns, but something is using the return value of the call. Just
// nuke the result.
TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
}
// Since we are now done with the Call/Invoke, we can delete it.

View File

@ -33,6 +33,7 @@
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/Dominators.h"
@ -242,7 +243,7 @@ Value *LCSSA::GetValueForBlock(DomTreeNode *BB, Instruction *OrigInst,
DenseMap<DomTreeNode*, Value*> &Phis) {
// If there is no dominator info for this BB, it is unreachable.
if (BB == 0)
return UndefValue::get(OrigInst->getType());
return Context->getUndef(OrigInst->getType());
// If we have already computed this value, return the previously computed val.
if (Phis.count(BB)) return Phis[BB];

View File

@ -20,6 +20,7 @@
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/DebugInfo.h"
@ -262,7 +263,8 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) {
/// too, recursively.
void
llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
LLVMContext* Context = PN->getParent()->getContext();
// We can remove a PHI if it is on a cycle in the def-use graph
// where each node in the cycle has degree one, i.e. only one use,
// and is an instruction with no side effects.
@ -279,7 +281,7 @@ llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
if (PHINode *JP = dyn_cast<PHINode>(J))
if (!PHIs.insert(cast<PHINode>(JP))) {
// Break the cycle and delete the PHI and its operands.
JP->replaceAllUsesWith(UndefValue::get(JP->getType()));
JP->replaceAllUsesWith(Context->getUndef(JP->getType()));
RecursivelyDeleteTriviallyDeadInstructions(JP);
break;
}
@ -299,7 +301,7 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB) {
while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
Value *NewVal = PN->getIncomingValue(0);
// Replace self referencing PHI with undef, it must be dead.
if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
if (NewVal == PN) NewVal = DestBB->getContext()->getUndef(PN->getType());
PN->replaceAllUsesWith(NewVal);
PN->eraseFromParent();
}

View File

@ -37,6 +37,7 @@
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Function.h"
#include "llvm/LLVMContext.h"
#include "llvm/Type.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Dominators.h"
@ -165,7 +166,7 @@ bool LoopSimplify::runOnFunction(Function &F) {
// Delete the dead terminator.
if (AA) AA->deleteValue(TI);
if (!TI->use_empty())
TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
TI->replaceAllUsesWith(Context->getUndef(TI->getType()));
TI->eraseFromParent();
Changed |= true;
}

View File

@ -19,6 +19,7 @@
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Constants.h"
#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Target/TargetData.h"
@ -86,10 +87,10 @@ Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
// This function is always successful.
//
bool LowerAllocations::doInitialization(Module &M) {
const Type *BPTy = PointerType::getUnqual(Type::Int8Ty);
const Type *BPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
// Prototype malloc as "char* malloc(...)", because we don't know in
// doInitialization whether size_t is int or long.
FunctionType *FT = FunctionType::get(BPTy, true);
FunctionType *FT = Context->getFunctionType(BPTy, true);
MallocFunc = M.getOrInsertFunction("malloc", FT);
FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0);
return true;
@ -115,11 +116,12 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
// malloc(type) becomes i8 *malloc(size)
Value *MallocArg;
if (LowerMallocArgToInteger)
MallocArg = ConstantInt::get(Type::Int64Ty,
MallocArg = Context->getConstantInt(Type::Int64Ty,
TD.getTypeAllocSize(AllocTy));
else
MallocArg = ConstantExpr::getSizeOf(AllocTy);
MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
MallocArg = Context->getConstantExprSizeOf(AllocTy);
MallocArg =
Context->getConstantExprTruncOrBitCast(cast<Constant>(MallocArg),
IntPtrTy);
if (MI->isArrayAllocation()) {
@ -127,8 +129,10 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
cast<ConstantInt>(MallocArg)->isOne()) {
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
} else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
CO =
Context->getConstantExprIntegerCast(CO, IntPtrTy, false /*ZExt*/);
MallocArg = Context->getConstantExprMul(CO,
cast<Constant>(MallocArg));
} else {
Value *Scale = MI->getOperand(0);
if (Scale->getType() != IntPtrTy)
@ -150,7 +154,7 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
if (MCall->getType() != Type::VoidTy)
MCast = new BitCastInst(MCall, MI->getType(), "", I);
else
MCast = Constant::getNullValue(MI->getType());
MCast = Context->getNullValue(MI->getType());
// Replace all uses of the old malloc inst with the cast inst
MI->replaceAllUsesWith(MCast);
@ -160,7 +164,7 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
} else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Value *PtrCast =
new BitCastInst(FI->getOperand(0),
PointerType::getUnqual(Type::Int8Ty), "", I);
Context->getPointerTypeUnqual(Type::Int8Ty), "", I);
// Insert a call to the free function...
CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();

View File

@ -40,6 +40,7 @@
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@ -114,33 +115,33 @@ FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
// doInitialization - Make sure that there is a prototype for abort in the
// current module.
bool LowerInvoke::doInitialization(Module &M) {
const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty);
AbortMessage = 0;
if (ExpensiveEHSupport) {
// Insert a type for the linked list of jump buffers.
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
JBSize = JBSize ? JBSize : 200;
const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
const Type *JmpBufTy = Context->getArrayType(VoidPtrTy, JBSize);
{ // The type is recursive, so use a type holder.
std::vector<const Type*> Elements;
Elements.push_back(JmpBufTy);
OpaqueType *OT = OpaqueType::get();
Elements.push_back(PointerType::getUnqual(OT));
PATypeHolder JBLType(StructType::get(Elements));
OpaqueType *OT = Context->getOpaqueType();
Elements.push_back(Context->getPointerTypeUnqual(OT));
PATypeHolder JBLType(Context->getStructType(Elements));
OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
JBLinkTy = JBLType.get();
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
}
const Type *PtrJBList = PointerType::getUnqual(JBLinkTy);
const Type *PtrJBList = Context->getPointerTypeUnqual(JBLinkTy);
// Now that we've done that, insert the jmpbuf list head global, unless it
// already exists.
if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
JBListHead = new GlobalVariable(PtrJBList, false,
GlobalValue::LinkOnceAnyLinkage,
Constant::getNullValue(PtrJBList),
Context->getNullValue(PtrJBList),
"llvm.sjljeh.jblist", &M);
}
@ -178,26 +179,26 @@ void LowerInvoke::createAbortMessage(Module *M) {
// The abort message for expensive EH support tells the user that the
// program 'unwound' without an 'invoke' instruction.
Constant *Msg =
ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
Context->getConstantArray("ERROR: Exception thrown, but not caught!\n");
AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
GlobalValue::InternalLinkage,
Msg, "abortmsg", M);
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
std::vector<Constant*> GEPIdx(2, Context->getNullValue(Type::Int32Ty));
AbortMessage = Context->getConstantExprGetElementPtr(MsgGV, &GEPIdx[0], 2);
} else {
// The abort message for cheap EH support tells the user that EH is not
// enabled.
Constant *Msg =
ConstantArray::get("Exception handler needed, but not enabled. Recompile"
" program with -enable-correct-eh-support.\n");
Context->getConstantArray("Exception handler needed, but not enabled."
"Recompile program with -enable-correct-eh-support.\n");
AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
GlobalValue::InternalLinkage,
Msg, "abortmsg", M);
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
std::vector<Constant*> GEPIdx(2, Context->getNullValue(Type::Int32Ty));
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
}
}

View File

@ -18,6 +18,7 @@
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
@ -200,11 +201,11 @@ BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
"SwitchLeaf", NewLeaf);
} else {
// Emit V-Lo <=u Hi-Lo
Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
Constant* NegLo = Context->getConstantExprNeg(Leaf.Low);
Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
Val->getName()+".off",
NewLeaf);
Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
Constant *UpperBound = Context->getConstantExprAdd(NegLo, Leaf.High);
Comp = new ICmpInst(ICmpInst::ICMP_ULE, Add, UpperBound,
"SwitchLeaf", NewLeaf);
}

View File

@ -75,7 +75,7 @@ bool PromotePass::runOnFunction(Function &F) {
if (Allocas.empty()) break;
PromoteMemToReg(Allocas, DT, DF);
PromoteMemToReg(Allocas, DT, DF, Context);
NumPromoted += Allocas.size();
Changed = true;
}

View File

@ -23,6 +23,7 @@
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/ADT/DenseMap.h"
@ -181,6 +182,8 @@ namespace {
/// AST - An AliasSetTracker object to update. If null, don't update it.
///
AliasSetTracker *AST;
LLVMContext* Context;
/// AllocaLookup - Reverse mapping of Allocas.
///
@ -212,8 +215,9 @@ namespace {
DenseMap<const BasicBlock*, unsigned> BBNumPreds;
public:
PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt,
DominanceFrontier &df, AliasSetTracker *ast)
: Allocas(A), DT(dt), DF(df), AST(ast) {}
DominanceFrontier &df, AliasSetTracker *ast,
LLVMContext* C)
: Allocas(A), DT(dt), DF(df), AST(ast), Context(C) {}
void run();
@ -445,7 +449,7 @@ void PromoteMem2Reg::run() {
//
RenamePassData::ValVector Values(Allocas.size());
for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
Values[i] = Context->getUndef(Allocas[i]->getAllocatedType());
// Walks all basic blocks in the function performing the SSA rename algorithm
// and inserting the phi nodes we marked as necessary
@ -472,7 +476,7 @@ void PromoteMem2Reg::run() {
// Just delete the users now.
//
if (!A->use_empty())
A->replaceAllUsesWith(UndefValue::get(A->getType()));
A->replaceAllUsesWith(Context->getUndef(A->getType()));
if (AST) AST->deleteValue(A);
A->eraseFromParent();
}
@ -558,7 +562,7 @@ void PromoteMem2Reg::run() {
BasicBlock::iterator BBI = BB->begin();
while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
SomePHI->getNumIncomingValues() == NumBadPreds) {
Value *UndefVal = UndefValue::get(SomePHI->getType());
Value *UndefVal = Context->getUndef(SomePHI->getType());
for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
SomePHI->addIncoming(UndefVal, Preds[pred]);
}
@ -804,7 +808,7 @@ void PromoteMem2Reg::PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
if (StoresByIndex.empty()) {
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
LI->replaceAllUsesWith(Context->getUndef(LI->getType()));
if (AST && isa<PointerType>(LI->getType()))
AST->deleteValue(LI);
LBI.deleteValue(LI);
@ -995,9 +999,9 @@ NextIteration:
///
void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
DominatorTree &DT, DominanceFrontier &DF,
AliasSetTracker *AST) {
LLVMContext* Context, AliasSetTracker *AST) {
// If there is nothing to do, bail out...
if (Allocas.empty()) return;
PromoteMem2Reg(Allocas, DT, DF, AST).run();
PromoteMem2Reg(Allocas, DT, DF, AST, Context).run();
}

View File

@ -16,6 +16,7 @@
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Type.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h"
@ -1274,6 +1275,8 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
/// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
/// PHI node, see if we can eliminate it.
static bool FoldTwoEntryPHINode(PHINode *PN) {
LLVMContext* Context = PN->getParent()->getContext();
// Ok, this is a two entry PHI node. Check to see if this is a simple "if
// statement", which has a very simple dominance structure. Basically, we
// are trying to find the condition that is being branched on, which
@ -1311,7 +1314,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN) {
if (PN->getIncomingValue(0) != PN)
PN->replaceAllUsesWith(PN->getIncomingValue(0));
else
PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
PN->replaceAllUsesWith(Context->getUndef(PN->getType()));
} else if (!DominatesMergePoint(PN->getIncomingValue(0), BB,
&AggressiveInsts) ||
!DominatesMergePoint(PN->getIncomingValue(1), BB,
@ -1605,6 +1608,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
assert(PBI->isConditional() && BI->isConditional());
BasicBlock *BB = BI->getParent();
LLVMContext* Context = BB->getContext();
// If this block ends with a branch instruction, and if there is a
// predecessor that ends on a branch of the same condition, make
@ -1616,7 +1620,7 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
if (BB->getSinglePredecessor()) {
// Turn this into a branch on constant.
bool CondIsTrue = PBI->getSuccessor(0) == BB;
BI->setCondition(ConstantInt::get(Type::Int1Ty, CondIsTrue));
BI->setCondition(Context->getConstantInt(Type::Int1Ty, CondIsTrue));
return true; // Nuke the branch on constant.
}
@ -1636,7 +1640,7 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
PBI->getCondition() == BI->getCondition() &&
PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
bool CondIsTrue = PBI->getSuccessor(0) == BB;
NewPN->addIncoming(ConstantInt::get(Type::Int1Ty,
NewPN->addIncoming(Context->getConstantInt(Type::Int1Ty,
CondIsTrue), *PI);
} else {
NewPN->addIncoming(BI->getCondition(), *PI);

View File

@ -13,14 +13,16 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/ValueMapper.h"
#include "llvm/BasicBlock.h"
#include "llvm/Constants.h"
#include "llvm/GlobalValue.h"
#include "llvm/Instruction.h"
#include "llvm/LLVMContext.h"
#include "llvm/MDNode.h"
#include "llvm/ADT/SmallVector.h"
using namespace llvm;
Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext* Context) {
Value *&VMSlot = VM[V];
if (VMSlot) return VMSlot; // Does it exist in the map yet?
@ -40,7 +42,7 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
i != e; ++i) {
Value *MV = MapValue(*i, VM);
Value *MV = MapValue(*i, VM, Context);
if (MV != *i) {
// This array must contain a reference to a global, make a new array
// and return it.
@ -51,8 +53,8 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Values.push_back(cast<Constant>(*j));
Values.push_back(cast<Constant>(MV));
for (++i; i != e; ++i)
Values.push_back(cast<Constant>(MapValue(*i, VM)));
return VM[V] = ConstantArray::get(CA->getType(), Values);
Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
return VM[V] = Context->getConstantArray(CA->getType(), Values);
}
}
return VM[V] = C;
@ -60,7 +62,7 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
} else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
i != e; ++i) {
Value *MV = MapValue(*i, VM);
Value *MV = MapValue(*i, VM, Context);
if (MV != *i) {
// This struct must contain a reference to a global, make a new struct
// and return it.
@ -71,8 +73,8 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Values.push_back(cast<Constant>(*j));
Values.push_back(cast<Constant>(MV));
for (++i; i != e; ++i)
Values.push_back(cast<Constant>(MapValue(*i, VM)));
return VM[V] = ConstantStruct::get(CS->getType(), Values);
Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
return VM[V] = Context->getConstantStruct(CS->getType(), Values);
}
}
return VM[V] = C;
@ -80,12 +82,12 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
std::vector<Constant*> Ops;
for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Ops.push_back(cast<Constant>(MapValue(*i, VM)));
Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
return VM[V] = CE->getWithOperands(Ops);
} else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
i != e; ++i) {
Value *MV = MapValue(*i, VM);
Value *MV = MapValue(*i, VM, Context);
if (MV != *i) {
// This vector value must contain a reference to a global, make a new
// vector constant and return it.
@ -96,8 +98,8 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Values.push_back(cast<Constant>(*j));
Values.push_back(cast<Constant>(MV));
for (++i; i != e; ++i)
Values.push_back(cast<Constant>(MapValue(*i, VM)));
return VM[V] = ConstantVector::get(Values);
Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
return VM[V] = Context->getConstantVector(Values);
}
}
return VM[V] = C;
@ -107,7 +109,7 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
e = N->elem_end(); i != e; ++i) {
if (!*i) continue;
Value *MV = MapValue(*i, VM);
Value *MV = MapValue(*i, VM, Context);
if (MV != *i) {
// This MDNode must contain a reference to a global, make a new MDNode
// and return it.
@ -117,8 +119,8 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Values.push_back(*j);
Values.push_back(MV);
for (++i; i != e; ++i)
Values.push_back(MapValue(*i, VM));
return VM[V] = MDNode::get(Values.data(), Values.size());
Values.push_back(MapValue(*i, VM, Context));
return VM[V] = Context->getMDNode(Values.data(), Values.size());
}
}
return VM[V] = C;
@ -136,7 +138,7 @@ Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
///
void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Value *V = MapValue(*op, ValueMap);
Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
assert(V && "Referenced value not in value map!");
*op = V;
}

View File

@ -29,6 +29,10 @@ ValueSymbolTable *BasicBlock::getValueSymbolTable() {
return 0;
}
LLVMContext* BasicBlock::getContext() const {
return Parent ? Parent->getContext() : 0;
}
// Explicit instantiation of SymbolTableListTraits since some of the methods
// are not in the public header file...
template class SymbolTableListTraits<Instruction, BasicBlock>;

View File

@ -114,8 +114,8 @@ void Argument::removeAttr(Attributes attr) {
// Helper Methods in Function
//===----------------------------------------------------------------------===//
LLVMContext* Function::getContext() {
Module* M = getParent();
LLVMContext* Function::getContext() const {
const Module* M = getParent();
if (M) return &M->getContext();
return 0;
}

View File

@ -374,6 +374,10 @@ Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
}
Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
return ConstantExpr::getSizeOf(Ty);
}
Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
return ConstantExpr::getZeroValueForNegationExpr(Ty);
}
@ -428,6 +432,10 @@ MDString* LLVMContext::getMDString(const std::string &Str) {
}
// FunctionType accessors
FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
return FunctionType::get(Result, isVarArg);
}
FunctionType* LLVMContext::getFunctionType(const Type* Result,
const std::vector<const Type*>& Params,
bool isVarArg) {