}
//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 "
// Initialization and finalization hooks.
virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
+ Context = L->getHeader()->getContext();
return false;
}
#define LLVM_CALL_GRAPH_SCC_PASS_H
#include "llvm/Pass.h"
+#include "llvm/Analysis/CallGraph.h"
namespace llvm {
/// 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;
}
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) {
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.
///
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
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()))
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;
}
const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
- return getConstant(ConstantInt::get(Val));
+ return getConstant(Context->getConstantInt(Val));
}
const SCEV *
++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
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);
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);
// 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));
}
// 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));
}
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);
}
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.
}
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) {
// 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]))
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
//
bool DTE::runOnModule(Module &M) {
bool Changed = false;
+ Context = &M.getContext();
TypeSymbolTable &ST = M.getTypeSymbolTable();
std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
return false; // Nothing to extract
}
+ Context = &M.getContext();
+
if (deleteStuff)
return deleteGV();
M.setModuleInlineAsm("");
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);
bool GlobalOpt::runOnModule(Module &M) {
bool Changed = false;
+ Context = &M.getContext();
// Try to find the llvm.globalctors list.
GlobalVariable *GlobalCtors = FindGlobalCtors(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) {
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
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
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");
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) {
}
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)
bool PartSpec::runOnModule(Module &M) {
+ Context = &M.getContext();
+
bool Changed = false;
for (Module::iterator I = M.begin(); I != M.end(); ++I) {
Function &F = *I;
// 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");
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; ) {
}
bool StripSymbols::runOnModule(Module &M) {
+ Context = &M.getContext();
bool Changed = false;
Changed |= StripDebugInfo(M);
if (!OnlyDebugInfo)
class VISIBILITY_HIDDEN ValueRanges {
ValueNumbering &VN;
TargetData *TD;
+ LLVMContext *Context;
class VISIBILITY_HIDDEN ScopedRange {
typedef std::vector<std::pair<DomTreeDFS::Node *, ConstantRange> >
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() {}
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?");
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.");
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);
}
}
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 {
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();
}
Pred = IC.getPredicate();
+ LLVMContext *Context = IC.getParent()->getContext();
+
if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) {
ConstantInt *NextVal = 0;
switch (Pred) {
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;
}
}
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.
/// 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;
// 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) {
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
//===----------------------------------------------------------------------===//
return *GlobalContext;
}
-LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
+LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
LLVMContext::~LLVMContext() { delete pImpl; }
// Constant accessors
}
ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
- return ConstantInt::get(V);
+ return pImpl->getConstantInt(V);
}
Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
--- /dev/null
+//===--------------- 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;
+ }
+}
+
-//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
+//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
#ifndef LLVM_LLVMCONTEXT_IMPL_H
#define LLVM_LLVMCONTEXT_IMPL_H
+#include "llvm/System/RWMutex.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/DenseMap.h"
+
namespace llvm {
-class LLVMContextImpl {
+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);
};
}