[llvm-commits] CVS: llvm/lib/Transforms/IPO/GlobalOpt.cpp
Chris Lattner
lattner at cs.uiuc.edu
Mon Sep 26 21:27:12 PDT 2005
Changes in directory llvm/lib/Transforms/IPO:
GlobalOpt.cpp updated: 1.53 -> 1.54
---
Log message:
Refactor this code a bit, no functionality changes.
---
Diffs of the changes: (+40 -22)
GlobalOpt.cpp | 62 +++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 40 insertions(+), 22 deletions(-)
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
diff -u llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.53 llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.54
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.53 Mon Sep 26 15:38:20 2005
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp Mon Sep 26 23:27:01 2005
@@ -1363,33 +1363,26 @@
return 0; // don't know how to evaluate.
}
-/// EvaluateStaticConstructor - Evaluate static constructors in the function, if
-/// we can. Return true if we can, false otherwise.
-static bool EvaluateStaticConstructor(Function *F) {
+/// EvaluateFunction - Evaluate a call to function F, returning true if
+/// successful, false if we can't evaluate it. ActualArgs contains the formal
+/// arguments for the function.
+static bool EvaluateFunction(Function *F,
+ const std::vector<Constant*> &ActualArgs,
+ std::vector<Function*> &CallStack,
+ std::map<Constant*, Constant*> &MutatedMemory,
+ std::vector<GlobalVariable*> &AllocaTmps) {
/// Values - As we compute SSA register values, we store their contents here.
std::map<Value*, Constant*> Values;
-
- /// MutatedMemory - For each store we execute, we update this map. Loads
- /// check this to get the most up-to-date value. If evaluation is successful,
- /// this state is committed to the process.
- std::map<Constant*, Constant*> MutatedMemory;
-
+
/// ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
/// we can only evaluate any one basic block at most once. This set keeps
/// track of what we have executed so we can detect recursive cases etc.
std::set<BasicBlock*> ExecutedBlocks;
- /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
- /// to represent its body. This allows us to delete the temporary globals
- /// when we are done.
- std::vector<GlobalVariable*> AllocaTmps;
-
// CurInst - The current instruction we're evaluating.
BasicBlock::iterator CurInst = F->begin()->begin();
ExecutedBlocks.insert(F->begin());
- bool EvaluationSuccessful = false;
-
// This is the main evaluation loop.
while (1) {
Constant *InstResult = 0;
@@ -1454,8 +1447,7 @@
NewBB = SI->getSuccessor(SI->findCaseValue(Val));
} else if (ReturnInst *RI = dyn_cast<ReturnInst>(CurInst)) {
assert(RI->getNumOperands() == 0);
- EvaluationSuccessful = true;
- break; // We succeeded at evaluating this ctor!
+ return true; // We succeeded at evaluating this ctor!
} else {
// unwind, unreachable.
break; // Cannot handle this terminator.
@@ -1492,10 +1484,34 @@
++CurInst;
}
- if (EvaluationSuccessful) {
+ return false;
+}
+
+/// EvaluateStaticConstructor - Evaluate static constructors in the function, if
+/// we can. Return true if we can, false otherwise.
+static bool EvaluateStaticConstructor(Function *F) {
+ /// MutatedMemory - For each store we execute, we update this map. Loads
+ /// check this to get the most up-to-date value. If evaluation is successful,
+ /// this state is committed to the process.
+ std::map<Constant*, Constant*> MutatedMemory;
+
+ /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
+ /// to represent its body. This vector is needed so we can delete the
+ /// temporary globals when we are done.
+ std::vector<GlobalVariable*> AllocaTmps;
+
+ /// CallStack - This is used to detect recursion. In pathological situations
+ /// we could hit exponential behavior, but at least there is nothing
+ /// unbounded.
+ std::vector<Function*> CallStack;
+
+ // Call the function.
+ bool EvalSuccess = EvaluateFunction(F, std::vector<Constant*>(), CallStack,
+ MutatedMemory, AllocaTmps);
+ if (EvalSuccess) {
// We succeeded at evaluation: commit the result.
DEBUG(std::cerr << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" <<
- F->getName() << "'\n");
+ F->getName() << "' to " << MutatedMemory.size() << " stores.\n");
for (std::map<Constant*, Constant*>::iterator I = MutatedMemory.begin(),
E = MutatedMemory.end(); I != E; ++I)
CommitValueTo(I->second, I->first);
@@ -1506,7 +1522,7 @@
while (!AllocaTmps.empty()) {
GlobalVariable *Tmp = AllocaTmps.back();
AllocaTmps.pop_back();
-
+
// 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
// later. Since this is undefined, we'll just make it be null.
@@ -1515,10 +1531,12 @@
delete Tmp;
}
- return EvaluationSuccessful;
+ return EvalSuccess;
}
+
+
/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
/// Return true if anything changed.
bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
More information about the llvm-commits
mailing list