diff --git a/examples/BrainF/BrainFDriver.cpp b/examples/BrainF/BrainFDriver.cpp index 4eaa4940e70..021b9510ce4 100644 --- a/examples/BrainF/BrainFDriver.cpp +++ b/examples/BrainF/BrainFDriver.cpp @@ -80,13 +80,13 @@ void addMainFunction(Module *mod) { } //ret i32 0 - ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb); + ReturnInst::Create(getGlobalContext().getConstantInt(APInt(32, 0)), bb); } int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n"); - LLVMContext Context; + LLVMContext &Context = getGlobalContext(); if (InputFilename == "") { std::cerr<<"Error: You must specify the filename of the program to " diff --git a/include/llvm/Analysis/LoopPass.h b/include/llvm/Analysis/LoopPass.h index 7659b5bf458..a6a23c6b61c 100644 --- a/include/llvm/Analysis/LoopPass.h +++ b/include/llvm/Analysis/LoopPass.h @@ -37,6 +37,7 @@ public: // Initialization and finalization hooks. virtual bool doInitialization(Loop *L, LPPassManager &LPM) { + Context = L->getHeader()->getContext(); return false; } diff --git a/include/llvm/CallGraphSCCPass.h b/include/llvm/CallGraphSCCPass.h index d5ff17cf247..8d5d7c3edfb 100644 --- a/include/llvm/CallGraphSCCPass.h +++ b/include/llvm/CallGraphSCCPass.h @@ -22,6 +22,7 @@ #define LLVM_CALL_GRAPH_SCC_PASS_H #include "llvm/Pass.h" +#include "llvm/Analysis/CallGraph.h" namespace llvm { @@ -37,6 +38,7 @@ struct CallGraphSCCPass : public Pass { /// doInitialization - This method is called before the SCC's of the program /// has been processed, allowing the pass to do initialization as necessary. virtual bool doInitialization(CallGraph &CG) { + Context = &CG.getModule().getContext(); return false; } diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index e06ce1f1b9e..df086416a8e 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -50,6 +50,7 @@ class ConstantInt : public Constant { ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT ConstantInt(const IntegerType *Ty, const APInt& V); APInt Val; + friend class LLVMContextImpl; protected: // allocate space for exactly zero operands void *operator new(size_t s) { @@ -102,10 +103,6 @@ public: return CreateTrueFalseVals(false); } - /// Return a ConstantInt with the specified value and an implied Type. The - /// type is the integer type that corresponds to the bit width of the value. - static ConstantInt *get(const APInt &V); - /// getType - Specialize the getType() method to always return an IntegerType, /// which reduces the amount of casting needed in parts of the compiler. /// diff --git a/include/llvm/LLVMContext.h b/include/llvm/LLVMContext.h index 552e7992c6d..4aa2c20ec28 100644 --- a/include/llvm/LLVMContext.h +++ b/include/llvm/LLVMContext.h @@ -93,6 +93,8 @@ public: ConstantInt* getConstantIntSigned(const IntegerType* Ty, int64_t V); Constant *getConstantIntSigned(const Type *Ty, int64_t V); + /// Return a ConstantInt with the specified value and an implied Type. The + /// type is the integer type that corresponds to the bit width of the value. ConstantInt* getConstantInt(const APInt& V); /// If Ty is a vector type, return a Constant with a splat of the given diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp index 59839e8c8bb..c10c6f3f279 100644 --- a/lib/Analysis/BasicAliasAnalysis.cpp +++ b/lib/Analysis/BasicAliasAnalysis.cpp @@ -312,6 +312,8 @@ BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { AliasAnalysis::AliasResult BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size, const Value *V2, unsigned V2Size) { + Context = &V1->getType()->getContext(); + // Strip off any constant expression casts if they exist if (const ConstantExpr *CE = dyn_cast(V1)) if (CE->isCast() && isa(CE->getOperand(0)->getType())) @@ -530,6 +532,8 @@ BasicAliasAnalysis::CheckGEPInstructions( const PointerType *GEPPointerTy = cast(BasePtr1Ty); + Context = &GEPPointerTy->getContext(); + // Find the (possibly empty) initial sequence of equal values... which are not // necessarily constants. unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops; diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index dcd6558d34b..1b0b37ba010 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -191,7 +191,7 @@ const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { } const SCEV *ScalarEvolution::getConstant(const APInt& Val) { - return getConstant(ConstantInt::get(Val)); + return getConstant(Context->getConstantInt(Val)); } const SCEV * @@ -1517,7 +1517,7 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl &Ops) { ++Idx; while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { // We found two constants, fold them together! - ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * + ConstantInt *Fold = Context->getConstantInt(LHSC->getValue()->getValue() * RHSC->getValue()->getValue()); Ops[0] = getConstant(Fold); Ops.erase(Ops.begin()+1); // Erase the folded element @@ -1868,7 +1868,7 @@ ScalarEvolution::getSMaxExpr(SmallVectorImpl &Ops) { assert(Idx < Ops.size()); while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { // We found two constants, fold them together! - ConstantInt *Fold = ConstantInt::get( + ConstantInt *Fold = Context->getConstantInt( APIntOps::smax(LHSC->getValue()->getValue(), RHSC->getValue()->getValue())); Ops[0] = getConstant(Fold); @@ -1965,7 +1965,7 @@ ScalarEvolution::getUMaxExpr(SmallVectorImpl &Ops) { assert(Idx < Ops.size()); while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { // We found two constants, fold them together! - ConstantInt *Fold = ConstantInt::get( + ConstantInt *Fold = Context->getConstantInt( APIntOps::umax(LHSC->getValue()->getValue(), RHSC->getValue()->getValue())); Ops[0] = getConstant(Fold); @@ -2887,7 +2887,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { // Turn shift left of a constant amount into a multiply. if (ConstantInt *SA = dyn_cast(U->getOperand(1))) { uint32_t BitWidth = cast(V->getType())->getBitWidth(); - Constant *X = ConstantInt::get( + Constant *X = Context->getConstantInt( APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); } @@ -2897,7 +2897,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { // Turn logical shift right of a constant into a unsigned divide. if (ConstantInt *SA = dyn_cast(U->getOperand(1))) { uint32_t BitWidth = cast(V->getType())->getBitWidth(); - Constant *X = ConstantInt::get( + Constant *X = Context->getConstantInt( APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); } diff --git a/lib/CodeGen/SelectionDAG/FastISel.cpp b/lib/CodeGen/SelectionDAG/FastISel.cpp index adb4d729e6e..d523e7fce34 100644 --- a/lib/CodeGen/SelectionDAG/FastISel.cpp +++ b/lib/CodeGen/SelectionDAG/FastISel.cpp @@ -108,7 +108,7 @@ unsigned FastISel::getRegForValue(Value *V) { if (isExact) { APInt IntVal(IntBitWidth, 2, x); - unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal)); + unsigned IntegerReg = getRegForValue(Context->getConstantInt(IntVal)); if (IntegerReg != 0) Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg); } diff --git a/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp b/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp index 600185be973..1412ad9f110 100644 --- a/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp +++ b/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp @@ -2305,7 +2305,8 @@ SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) { ISD::SETLT); // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits. - SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)), + SDValue FudgePtr = DAG.getConstantPool( + DAG.getContext()->getConstantInt(FF.zext(64)), TLI.getPointerTy()); // Get a pointer to FF if the sign bit was set, or to 0 otherwise. diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index caa3ce1857b..4491eb262b9 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -874,7 +874,7 @@ SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) { } SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) { - return getConstant(*ConstantInt::get(Val), VT, isT); + return getConstant(*Context->getConstantInt(Val), VT, isT); } SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) { diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp index b1589d4c096..4f90bb31fcf 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp @@ -2406,7 +2406,8 @@ void SelectionDAGLowering::visitShuffleVector(User &I) { // Convert the ConstantVector mask operand into an array of ints, with -1 // representing undef values. SmallVector MaskElts; - cast(I.getOperand(2))->getVectorElements(*Context, MaskElts); + cast(I.getOperand(2))->getVectorElements(*DAG.getContext(), + MaskElts); unsigned MaskNumElts = MaskElts.size(); for (unsigned i = 0; i != MaskNumElts; ++i) { if (isa(MaskElts[i])) diff --git a/lib/Transforms/IPO/DeadArgumentElimination.cpp b/lib/Transforms/IPO/DeadArgumentElimination.cpp index 2e913914d6b..e86f619dcb0 100644 --- a/lib/Transforms/IPO/DeadArgumentElimination.cpp +++ b/lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -908,6 +908,7 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) { bool DAE::runOnModule(Module &M) { bool Changed = false; + Context = &M.getContext(); // First pass: Do a simple check to see if any functions can have their "..." // removed. We can do this if they never call va_start. This loop cannot be diff --git a/lib/Transforms/IPO/DeadTypeElimination.cpp b/lib/Transforms/IPO/DeadTypeElimination.cpp index 85aed2b7915..f202403d804 100644 --- a/lib/Transforms/IPO/DeadTypeElimination.cpp +++ b/lib/Transforms/IPO/DeadTypeElimination.cpp @@ -77,6 +77,7 @@ static inline bool ShouldNukeSymtabEntry(const Type *Ty){ // bool DTE::runOnModule(Module &M) { bool Changed = false; + Context = &M.getContext(); TypeSymbolTable &ST = M.getTypeSymbolTable(); std::set UsedTypes = getAnalysis().getTypes(); diff --git a/lib/Transforms/IPO/ExtractGV.cpp b/lib/Transforms/IPO/ExtractGV.cpp index b7c5ca3e6e0..fa3a055b20d 100644 --- a/lib/Transforms/IPO/ExtractGV.cpp +++ b/lib/Transforms/IPO/ExtractGV.cpp @@ -44,6 +44,8 @@ namespace { return false; // Nothing to extract } + Context = &M.getContext(); + if (deleteStuff) return deleteGV(); M.setModuleInlineAsm(""); diff --git a/lib/Transforms/IPO/GlobalDCE.cpp b/lib/Transforms/IPO/GlobalDCE.cpp index 9c652b996ae..9ecc494d9b5 100644 --- a/lib/Transforms/IPO/GlobalDCE.cpp +++ b/lib/Transforms/IPO/GlobalDCE.cpp @@ -58,6 +58,8 @@ ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); } bool GlobalDCE::runOnModule(Module &M) { bool Changed = false; + Context = &M.getContext(); + // Loop over the module, adding globals which are obviously necessary. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { Changed |= RemoveUnusedGlobalValue(*I); diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 398f78a0984..8b3cf48ceed 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -2476,6 +2476,7 @@ bool GlobalOpt::OptimizeGlobalAliases(Module &M) { bool GlobalOpt::runOnModule(Module &M) { bool Changed = false; + Context = &M.getContext(); // Try to find the llvm.globalctors list. GlobalVariable *GlobalCtors = FindGlobalCtors(M); diff --git a/lib/Transforms/IPO/IPConstantPropagation.cpp b/lib/Transforms/IPO/IPConstantPropagation.cpp index e27792a098e..30834e2d672 100644 --- a/lib/Transforms/IPO/IPConstantPropagation.cpp +++ b/lib/Transforms/IPO/IPConstantPropagation.cpp @@ -56,6 +56,8 @@ bool IPCP::runOnModule(Module &M) { bool Changed = false; bool LocalChange = true; + Context = &M.getContext(); + // FIXME: instead of using smart algorithms, we just iterate until we stop // making changes. while (LocalChange) { diff --git a/lib/Transforms/IPO/IndMemRemoval.cpp b/lib/Transforms/IPO/IndMemRemoval.cpp index 2086a166833..7b9b0ccb8b2 100644 --- a/lib/Transforms/IPO/IndMemRemoval.cpp +++ b/lib/Transforms/IPO/IndMemRemoval.cpp @@ -44,6 +44,8 @@ static RegisterPass X("indmemrem","Indirect Malloc and Free Removal"); bool IndMemRemPass::runOnModule(Module &M) { + Context = &M.getContext(); + // In theory, all direct calls of malloc and free should be promoted // to intrinsics. Therefore, this goes through and finds where the // address of free or malloc are taken and replaces those with bounce diff --git a/lib/Transforms/IPO/Internalize.cpp b/lib/Transforms/IPO/Internalize.cpp index 5093ae90b5b..9bc91725170 100644 --- a/lib/Transforms/IPO/Internalize.cpp +++ b/lib/Transforms/IPO/Internalize.cpp @@ -101,6 +101,8 @@ void InternalizePass::LoadFile(const char *Filename) { bool InternalizePass::runOnModule(Module &M) { CallGraph *CG = getAnalysisIfAvailable(); CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; + + Context = &M.getContext(); if (ExternalNames.empty()) { // Return if we're not in 'all but main' mode and have no external api diff --git a/lib/Transforms/IPO/LowerSetJmp.cpp b/lib/Transforms/IPO/LowerSetJmp.cpp index dfdf5b6f2d4..bfa2bd9bc24 100644 --- a/lib/Transforms/IPO/LowerSetJmp.cpp +++ b/lib/Transforms/IPO/LowerSetJmp.cpp @@ -134,6 +134,8 @@ static RegisterPass X("lowersetjmp", "Lower Set Jump"); bool LowerSetJmp::runOnModule(Module& M) { bool Changed = false; + Context = &M.getContext(); + // These are what the functions are called. Function* SetJmp = M.getFunction("llvm.setjmp"); Function* LongJmp = M.getFunction("llvm.longjmp"); diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp index 9cc4daa9d26..e93a08784f0 100644 --- a/lib/Transforms/IPO/MergeFunctions.cpp +++ b/lib/Transforms/IPO/MergeFunctions.cpp @@ -615,6 +615,8 @@ static bool fold(std::vector &FnVec, unsigned i, unsigned j) { bool MergeFunctions::runOnModule(Module &M) { bool Changed = false; + Context = &M.getContext(); + std::map > FnMap; for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { diff --git a/lib/Transforms/IPO/PartialInlining.cpp b/lib/Transforms/IPO/PartialInlining.cpp index 73ec9c10763..a6e090b3170 100644 --- a/lib/Transforms/IPO/PartialInlining.cpp +++ b/lib/Transforms/IPO/PartialInlining.cpp @@ -141,6 +141,8 @@ Function* PartialInliner::unswitchFunction(Function* F) { } bool PartialInliner::runOnModule(Module& M) { + Context = &M.getContext(); + std::vector worklist; worklist.reserve(M.size()); for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) diff --git a/lib/Transforms/IPO/PartialSpecialization.cpp b/lib/Transforms/IPO/PartialSpecialization.cpp index 0e1fdb9915a..3b2ce148603 100644 --- a/lib/Transforms/IPO/PartialSpecialization.cpp +++ b/lib/Transforms/IPO/PartialSpecialization.cpp @@ -108,6 +108,8 @@ SpecializeFunction(Function* F, bool PartSpec::runOnModule(Module &M) { + Context = &M.getContext(); + bool Changed = false; for (Module::iterator I = M.begin(); I != M.end(); ++I) { Function &F = *I; diff --git a/lib/Transforms/IPO/RaiseAllocations.cpp b/lib/Transforms/IPO/RaiseAllocations.cpp index 6c320509097..8d2a9cbdd5a 100644 --- a/lib/Transforms/IPO/RaiseAllocations.cpp +++ b/lib/Transforms/IPO/RaiseAllocations.cpp @@ -70,6 +70,7 @@ ModulePass *llvm::createRaiseAllocationsPass() { // function into the appropriate instruction. // void RaiseAllocations::doInitialization(Module &M) { + Context = &M.getContext(); // Get Malloc and free prototypes if they exist! MallocFunc = M.getFunction("malloc"); diff --git a/lib/Transforms/IPO/StripDeadPrototypes.cpp b/lib/Transforms/IPO/StripDeadPrototypes.cpp index a94d78e276c..2a1779107a5 100644 --- a/lib/Transforms/IPO/StripDeadPrototypes.cpp +++ b/lib/Transforms/IPO/StripDeadPrototypes.cpp @@ -42,6 +42,7 @@ X("strip-dead-prototypes", "Strip Unused Function Prototypes"); bool StripDeadPrototypesPass::runOnModule(Module &M) { bool MadeChange = false; + Context = &M.getContext(); // Erase dead function prototypes. for (Module::iterator I = M.begin(), E = M.end(); I != E; ) { diff --git a/lib/Transforms/IPO/StripSymbols.cpp b/lib/Transforms/IPO/StripSymbols.cpp index 323d1800a9b..82a0952aa20 100644 --- a/lib/Transforms/IPO/StripSymbols.cpp +++ b/lib/Transforms/IPO/StripSymbols.cpp @@ -374,6 +374,7 @@ bool StripDebugInfo(Module &M) { } bool StripSymbols::runOnModule(Module &M) { + Context = &M.getContext(); bool Changed = false; Changed |= StripDebugInfo(M); if (!OnlyDebugInfo) diff --git a/lib/Transforms/Scalar/PredicateSimplifier.cpp b/lib/Transforms/Scalar/PredicateSimplifier.cpp index 50d606311a1..51de4f7c050 100644 --- a/lib/Transforms/Scalar/PredicateSimplifier.cpp +++ b/lib/Transforms/Scalar/PredicateSimplifier.cpp @@ -905,6 +905,7 @@ namespace { class VISIBILITY_HIDDEN ValueRanges { ValueNumbering &VN; TargetData *TD; + LLVMContext *Context; class VISIBILITY_HIDDEN ScopedRange { typedef std::vector > @@ -1025,7 +1026,8 @@ namespace { public: - ValueRanges(ValueNumbering &VN, TargetData *TD) : VN(VN), TD(TD) {} + ValueRanges(ValueNumbering &VN, TargetData *TD, LLVMContext *C) : + VN(VN), TD(TD), Context(C) {} #ifndef NDEBUG virtual ~ValueRanges() {} @@ -1167,7 +1169,7 @@ namespace { Value *V = VN.value(n); // XXX: redesign worklist. const Type *Ty = V->getType(); if (Ty->isInteger()) { - addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP); + addToWorklist(V, Context->getConstantInt(*I), ICmpInst::ICMP_EQ, VRP); return; } else if (const PointerType *PTy = dyn_cast(Ty)) { assert(*I == 0 && "Pointer is null but not zero?"); @@ -1678,7 +1680,8 @@ namespace { Top(DTDFS->getNodeForBlock(TopInst->getParent())), TopBB(TopInst->getParent()), TopInst(TopInst), - modified(modified) + modified(modified), + Context(TopInst->getParent()->getContext()) { assert(Top && "VRPSolver created for unreachable basic block."); assert(Top->getBlock() == TopInst->getParent() && "Context mismatch."); @@ -1779,7 +1782,8 @@ namespace { if (ConstantInt *CI = dyn_cast(Canonical)) { if (ConstantInt *Arg = dyn_cast(LHS)) { - add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()), + add(RHS, + Context->getConstantInt(CI->getValue() ^ Arg->getValue()), ICmpInst::ICMP_EQ, NewContext); } } @@ -2404,7 +2408,7 @@ namespace { DomTreeDFS::Node *Root = DTDFS->getRootNode(); VN = new ValueNumbering(DTDFS); IG = new InequalityGraph(*VN, Root); - VR = new ValueRanges(*VN, TD); + VR = new ValueRanges(*VN, TD, Context); WorkList.push_back(Root); do { @@ -2526,21 +2530,23 @@ namespace { void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) { VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI); + LLVMContext *Context = SI.getParent()->getContext(); uint32_t SrcBitWidth = cast(SI.getSrcTy())->getBitWidth(); uint32_t DstBitWidth = cast(SI.getDestTy())->getBitWidth(); APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1)); APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1)); - VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE); - VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE); + VRP.add(Context->getConstantInt(Min), &SI, ICmpInst::ICMP_SLE); + VRP.add(Context->getConstantInt(Max), &SI, ICmpInst::ICMP_SGE); VRP.solve(); } void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) { VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &ZI); + LLVMContext *Context = ZI.getParent()->getContext(); uint32_t SrcBitWidth = cast(ZI.getSrcTy())->getBitWidth(); uint32_t DstBitWidth = cast(ZI.getDestTy())->getBitWidth(); APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth)); - VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE); + VRP.add(Context->getConstantInt(Max), &ZI, ICmpInst::ICMP_UGE); VRP.solve(); } @@ -2629,6 +2635,8 @@ namespace { Pred = IC.getPredicate(); + LLVMContext *Context = IC.getParent()->getContext(); + if (ConstantInt *Op1 = dyn_cast(IC.getOperand(1))) { ConstantInt *NextVal = 0; switch (Pred) { @@ -2636,12 +2644,12 @@ namespace { case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_ULT: if (Op1->getValue() != 0) - NextVal = ConstantInt::get(Op1->getValue()-1); + NextVal = Context->getConstantInt(Op1->getValue()-1); break; case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_UGT: if (!Op1->getValue().isAllOnesValue()) - NextVal = ConstantInt::get(Op1->getValue()+1); + NextVal = Context->getConstantInt(Op1->getValue()+1); break; } diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp index 6ada2882e31..7fa1a08f8ad 100644 --- a/lib/Transforms/Scalar/SCCP.cpp +++ b/lib/Transforms/Scalar/SCCP.cpp @@ -1662,7 +1662,10 @@ static bool AddressIsTaken(GlobalValue *GV) { } bool IPSCCP::runOnModule(Module &M) { + Context = &M.getContext(); + SCCPSolver Solver; + Solver.setContext(Context); // Loop over all functions, marking arguments to those with their addresses // taken or that are external as overdefined. diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index 8a28ca23b1b..f285406b190 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -1730,6 +1730,8 @@ void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) { /// doInitialization - Add attributes to well-known functions. /// bool SimplifyLibCalls::doInitialization(Module &M) { + Context = &M.getContext(); + Modified = false; for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { Function &F = *I; diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp index fae3edf0ada..1417f10e645 100644 --- a/lib/Transforms/Utils/LowerInvoke.cpp +++ b/lib/Transforms/Utils/LowerInvoke.cpp @@ -115,6 +115,8 @@ 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) { + Context = &M.getContext(); + const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty); AbortMessage = 0; if (ExpensiveEHSupport) { diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 49dd6a06d8a..680aed59025 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -190,67 +190,6 @@ ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { return WhichOne ? TheTrueVal : TheFalseVal; } - -namespace { - struct DenseMapAPIntKeyInfo { - struct KeyTy { - APInt val; - const Type* type; - KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} - KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} - bool operator==(const KeyTy& that) const { - return type == that.type && this->val == that.val; - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - }; - static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } - static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } - static unsigned getHashValue(const KeyTy &Key) { - return DenseMapInfo::getHashValue(Key.type) ^ - Key.val.getHashValue(); - } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; - } - static bool isPod() { return false; } - }; -} - - -typedef DenseMap IntMapTy; -static ManagedStatic IntConstants; - -// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap -// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the -// operator== and operator!= to ensure that the DenseMap doesn't attempt to -// compare APInt's of different widths, which would violate an APInt class -// invariant which generates an assertion. -ConstantInt *ConstantInt::get(const APInt& V) { - // Get the corresponding integer type for the bit width of the value. - const IntegerType *ITy = IntegerType::get(V.getBitWidth()); - // get an existing value or the insertion position - DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); - - ConstantsLock->reader_acquire(); - ConstantInt *&Slot = (*IntConstants)[Key]; - ConstantsLock->reader_release(); - - if (!Slot) { - sys::SmartScopedWriter Writer(*ConstantsLock); - ConstantInt *&NewSlot = (*IntConstants)[Key]; - if (!Slot) { - NewSlot = new ConstantInt(ITy, V); - } - - return NewSlot; - } else { - return Slot; - } -} - //===----------------------------------------------------------------------===// // ConstantFP //===----------------------------------------------------------------------===// diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index 73c93a2d6e5..0372f31e674 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -29,7 +29,7 @@ LLVMContext& llvm::getGlobalContext() { return *GlobalContext; } -LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { } +LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } LLVMContext::~LLVMContext() { delete pImpl; } // Constant accessors @@ -117,7 +117,7 @@ Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) { } ConstantInt* LLVMContext::getConstantInt(const APInt& V) { - return ConstantInt::get(V); + return pImpl->getConstantInt(V); } Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) { diff --git a/lib/VMCore/LLVMContextImpl.cpp b/lib/VMCore/LLVMContextImpl.cpp new file mode 100644 index 00000000000..a92c19fe04a --- /dev/null +++ b/lib/VMCore/LLVMContextImpl.cpp @@ -0,0 +1,48 @@ +//===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements LLVMContextImpl, the opaque implementation +// of LLVMContext. +// +//===----------------------------------------------------------------------===// + +#include "LLVMContextImpl.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/LLVMContext.h" +using namespace llvm; + +// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap +// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the +// operator== and operator!= to ensure that the DenseMap doesn't attempt to +// compare APInt's of different widths, which would violate an APInt class +// invariant which generates an assertion. +ConstantInt *LLVMContextImpl::getConstantInt(const APInt& V) { + // Get the corresponding integer type for the bit width of the value. + const IntegerType *ITy = Context.getIntegerType(V.getBitWidth()); + // get an existing value or the insertion position + DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); + + ConstantsLock.reader_acquire(); + ConstantInt *&Slot = IntConstants[Key]; + ConstantsLock.reader_release(); + + if (!Slot) { + sys::SmartScopedWriter Writer(ConstantsLock); + ConstantInt *&NewSlot = IntConstants[Key]; + if (!Slot) { + NewSlot = new ConstantInt(ITy, V); + } + + return NewSlot; + } else { + return Slot; + } +} + diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 4e089fb661c..fbf29fdfc49 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -1,4 +1,4 @@ -//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===// +//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===// // // The LLVM Compiler Infrastructure // @@ -15,9 +15,57 @@ #ifndef LLVM_LLVMCONTEXT_IMPL_H #define LLVM_LLVMCONTEXT_IMPL_H -namespace llvm { -class LLVMContextImpl { +#include "llvm/System/RWMutex.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/DenseMap.h" +namespace llvm { + +class ConstantInt; +class LLVMContext; +class Type; + +struct DenseMapAPIntKeyInfo { + struct KeyTy { + APInt val; + const Type* type; + KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} + KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} + bool operator==(const KeyTy& that) const { + return type == that.type && this->val == that.val; + } + bool operator!=(const KeyTy& that) const { + return !this->operator==(that); + } + }; + static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } + static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } + static unsigned getHashValue(const KeyTy &Key) { + return DenseMapInfo::getHashValue(Key.type) ^ + Key.val.getHashValue(); + } + static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { + return LHS == RHS; + } + static bool isPod() { return false; } +}; + +class LLVMContextImpl { + sys::SmartRWMutex ConstantsLock; + + typedef DenseMap IntMapTy; + IntMapTy IntConstants; + + LLVMContext &Context; + LLVMContextImpl(); + LLVMContextImpl(const LLVMContextImpl&); +public: + LLVMContextImpl(LLVMContext &C) : Context(C) { } + + /// Return a ConstantInt with the specified value and an implied Type. The + /// type is the integer type that corresponds to the bit width of the value. + ConstantInt* getConstantInt(const APInt &V); }; }