Use unique_ptr to handle GlobalOpt's Evaluator members

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206790 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie
2014-04-21 20:49:36 +00:00
parent 0df9abbd63
commit cdb2808ef8

View File

@@ -2271,22 +2271,16 @@ class Evaluator {
public: public:
Evaluator(const DataLayout *DL, const TargetLibraryInfo *TLI) Evaluator(const DataLayout *DL, const TargetLibraryInfo *TLI)
: DL(DL), TLI(TLI) { : DL(DL), TLI(TLI) {
ValueStack.push_back(new DenseMap<Value*, Constant*>); ValueStack.push_back(make_unique<DenseMap<Value*, Constant*>>());
} }
~Evaluator() { ~Evaluator() {
DeleteContainerPointers(ValueStack); for (auto &Tmp : AllocaTmps)
while (!AllocaTmps.empty()) {
GlobalVariable *Tmp = AllocaTmps.back();
AllocaTmps.pop_back();
// If there are still users of the alloca, the program is doing something // If there are still users of the alloca, the program is doing something
// silly, e.g. storing the address of the alloca somewhere and using it // silly, e.g. storing the address of the alloca somewhere and using it
// later. Since this is undefined, we'll just make it be null. // later. Since this is undefined, we'll just make it be null.
if (!Tmp->use_empty()) if (!Tmp->use_empty())
Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType())); Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
delete Tmp;
}
} }
/// EvaluateFunction - Evaluate a call to function F, returning true if /// EvaluateFunction - Evaluate a call to function F, returning true if
@@ -2325,7 +2319,7 @@ private:
/// ValueStack - As we compute SSA register values, we store their contents /// ValueStack - As we compute SSA register values, we store their contents
/// here. The back of the vector contains the current function and the stack /// here. The back of the vector contains the current function and the stack
/// contains the values in the calling frames. /// contains the values in the calling frames.
SmallVector<DenseMap<Value*, Constant*>*, 4> ValueStack; SmallVector<std::unique_ptr<DenseMap<Value*, Constant*>>, 4> ValueStack;
/// CallStack - This is used to detect recursion. In pathological situations /// CallStack - This is used to detect recursion. In pathological situations
/// we could hit exponential behavior, but at least there is nothing /// we could hit exponential behavior, but at least there is nothing
@@ -2340,7 +2334,7 @@ private:
/// AllocaTmps - To 'execute' an alloca, we create a temporary global variable /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
/// to represent its body. This vector is needed so we can delete the /// to represent its body. This vector is needed so we can delete the
/// temporary globals when we are done. /// temporary globals when we are done.
SmallVector<GlobalVariable*, 32> AllocaTmps; SmallVector<std::unique_ptr<GlobalVariable>, 32> AllocaTmps;
/// Invariants - These global variables have been marked invariant by the /// Invariants - These global variables have been marked invariant by the
/// static constructor. /// static constructor.
@@ -2530,11 +2524,10 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
return false; // Cannot handle array allocs. return false; // Cannot handle array allocs.
} }
Type *Ty = AI->getType()->getElementType(); Type *Ty = AI->getType()->getElementType();
AllocaTmps.push_back(new GlobalVariable(Ty, false, AllocaTmps.push_back(
GlobalValue::InternalLinkage, make_unique<GlobalVariable>(Ty, false, GlobalValue::InternalLinkage,
UndefValue::get(Ty), UndefValue::get(Ty), AI->getName()));
AI->getName())); InstResult = AllocaTmps.back().get();
InstResult = AllocaTmps.back();
DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n"); DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
} else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) { } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
CallSite CS(CurInst); CallSite CS(CurInst);
@@ -2638,12 +2631,12 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
Constant *RetVal = 0; Constant *RetVal = 0;
// Execute the call, if successful, use the return value. // Execute the call, if successful, use the return value.
ValueStack.push_back(new DenseMap<Value*, Constant*>); ValueStack.push_back(make_unique<DenseMap<Value *, Constant *>>());
if (!EvaluateFunction(Callee, RetVal, Formals)) { if (!EvaluateFunction(Callee, RetVal, Formals)) {
DEBUG(dbgs() << "Failed to evaluate function.\n"); DEBUG(dbgs() << "Failed to evaluate function.\n");
return false; return false;
} }
delete ValueStack.pop_back_val(); ValueStack.pop_back();
InstResult = RetVal; InstResult = RetVal;
if (InstResult != NULL) { if (InstResult != NULL) {