Move the ConstantInt uniquing table into LLVMContextImpl. This exposed a number of issues in

our current context-passing stuff, which is also fixed here


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76089 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-07-16 18:04:31 +00:00
parent f96db468fc
commit 001dbfebcb
34 changed files with 174 additions and 92 deletions

View File

@ -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 "

View File

@ -37,6 +37,7 @@ public:
// Initialization and finalization hooks.
virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
Context = L->getHeader()->getContext();
return false;
}

View File

@ -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;
}

View File

@ -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.
///

View File

@ -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

View File

@ -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<ConstantExpr>(V1))
if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
@ -530,6 +532,8 @@ BasicAliasAnalysis::CheckGEPInstructions(
const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
Context = &GEPPointerTy->getContext();
// Find the (possibly empty) initial sequence of equal values... which are not
// necessarily constants.
unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops;

View File

@ -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<const SCEV *> &Ops) {
++Idx;
while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(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<const SCEV *> &Ops) {
assert(Idx < Ops.size());
while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(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<const SCEV *> &Ops) {
assert(Idx < Ops.size());
while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(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<ConstantInt>(U->getOperand(1))) {
uint32_t BitWidth = cast<IntegerType>(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<ConstantInt>(U->getOperand(1))) {
uint32_t BitWidth = cast<IntegerType>(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));
}

View File

@ -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);
}

View File

@ -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.

View File

@ -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) {

View File

@ -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<Constant*, 8> MaskElts;
cast<Constant>(I.getOperand(2))->getVectorElements(*Context, MaskElts);
cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
MaskElts);
unsigned MaskNumElts = MaskElts.size();
for (unsigned i = 0; i != MaskNumElts; ++i) {
if (isa<UndefValue>(MaskElts[i]))

View File

@ -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

View File

@ -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<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();

View File

@ -44,6 +44,8 @@ namespace {
return false; // Nothing to extract
}
Context = &M.getContext();
if (deleteStuff)
return deleteGV();
M.setModuleInlineAsm("");

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -44,6 +44,8 @@ static RegisterPass<IndMemRemPass>
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

View File

@ -101,6 +101,8 @@ void InternalizePass::LoadFile(const char *Filename) {
bool InternalizePass::runOnModule(Module &M) {
CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
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

View File

@ -134,6 +134,8 @@ static RegisterPass<LowerSetJmp> 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");

View File

@ -615,6 +615,8 @@ static bool fold(std::vector<Function *> &FnVec, unsigned i, unsigned j) {
bool MergeFunctions::runOnModule(Module &M) {
bool Changed = false;
Context = &M.getContext();
std::map<unsigned long, std::vector<Function *> > FnMap;
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {

View File

@ -141,6 +141,8 @@ Function* PartialInliner::unswitchFunction(Function* F) {
}
bool PartialInliner::runOnModule(Module& M) {
Context = &M.getContext();
std::vector<Function*> worklist;
worklist.reserve(M.size());
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)

View File

@ -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;

View File

@ -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");

View File

@ -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; ) {

View File

@ -374,6 +374,7 @@ bool StripDebugInfo(Module &M) {
}
bool StripSymbols::runOnModule(Module &M) {
Context = &M.getContext();
bool Changed = false;
Changed |= StripDebugInfo(M);
if (!OnlyDebugInfo)

View File

@ -905,6 +905,7 @@ namespace {
class VISIBILITY_HIDDEN ValueRanges {
ValueNumbering &VN;
TargetData *TD;
LLVMContext *Context;
class VISIBILITY_HIDDEN ScopedRange {
typedef std::vector<std::pair<DomTreeDFS::Node *, ConstantRange> >
@ -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<PointerType>(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<ConstantInt>(Canonical)) {
if (ConstantInt *Arg = dyn_cast<ConstantInt>(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<IntegerType>(SI.getSrcTy())->getBitWidth();
uint32_t DstBitWidth = cast<IntegerType>(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<IntegerType>(ZI.getSrcTy())->getBitWidth();
uint32_t DstBitWidth = cast<IntegerType>(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<ConstantInt>(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;
}

View File

@ -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.

View File

@ -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;

View File

@ -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) {

View File

@ -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<void*>::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<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
DenseMapAPIntKeyInfo> IntMapTy;
static ManagedStatic<IntMapTy> 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<true> Writer(*ConstantsLock);
ConstantInt *&NewSlot = (*IntConstants)[Key];
if (!Slot) {
NewSlot = new ConstantInt(ITy, V);
}
return NewSlot;
} else {
return Slot;
}
}
//===----------------------------------------------------------------------===//
// ConstantFP
//===----------------------------------------------------------------------===//

View File

@ -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) {

View File

@ -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<true> Writer(ConstantsLock);
ConstantInt *&NewSlot = IntConstants[Key];
if (!Slot) {
NewSlot = new ConstantInt(ITy, V);
}
return NewSlot;
} else {
return Slot;
}
}

View File

@ -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<void*>::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<true> ConstantsLock;
typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
DenseMapAPIntKeyInfo> 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);
};
}