// InstVal class - This class represents the different lattice values that an
// instruction may occupy. It is a simple class with value semantics.
//
+namespace {
class InstVal {
enum {
undefined, // This instruction has no known value
inline Constant *getConstant() const { return ConstantVal; }
};
+} // end anonymous namespace
//===----------------------------------------------------------------------===//
// SCCP Class
//
// This class does all of the work of Sparse Conditional Constant Propogation.
-// It's public interface consists of a constructor and a doSCCP() function.
//
-class SCCP : public InstVisitor<SCCP> {
- Function *M; // The function that we are working on
-
+namespace {
+class SCCP : public FunctionPass, public InstVisitor<SCCP> {
std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
std::map<Value*, InstVal> ValueState; // The state each value is in...
std::vector<Instruction*> InstWorkList;// The instruction work list
std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
+public:
- //===--------------------------------------------------------------------===//
- // The public interface for this class
+ const char *getPassName() const {
+ return "Sparse Conditional Constant Propogation";
+ }
+
+ // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
+ // and return true if the function was modified.
//
-public:
+ bool runOnFunction(Function *F);
- // SCCP Ctor - Save the function to operate on...
- inline SCCP(Function *f) : M(f) {}
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ // FIXME: SCCP does not preserve the CFG because it folds terminators!
+ //AU.preservesCFG();
+ }
- // doSCCP() - Run the Sparse Conditional Constant Propogation algorithm, and
- // return true if the function was modified.
- bool doSCCP();
//===--------------------------------------------------------------------===//
// The implementation of this class
//
void OperandChangedState(User *U);
};
+} // end anonymous namespace
+
+
+// createSCCPPass - This is the public interface to this file...
+//
+Pass *createSCCPPass() {
+ return new SCCP();
+}
+
//===----------------------------------------------------------------------===//
// SCCP Class Implementation
-// doSCCP() - Run the Sparse Conditional Constant Propogation algorithm, and
-// return true if the function was modified.
+// runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
+// and return true if the function was modified.
//
-bool SCCP::doSCCP() {
+bool SCCP::runOnFunction(Function *F) {
// Mark the first block of the function as being executable...
- markExecutable(M->front());
+ markExecutable(F->front());
// Process the work lists until their are empty!
while (!BBWorkList.empty() || !InstWorkList.empty()) {
}
#if 0
- for (Function::iterator BBI = M->begin(), BBEnd = M->end();
+ for (Function::iterator BBI = F->begin(), BBEnd = F->end();
BBI != BBEnd; ++BBI)
if (!BBExecutable.count(*BBI))
cerr << "BasicBlock Dead:" << *BBI;
// constants if we have found them to be of constant values.
//
bool MadeChanges = false;
- for (Function::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
- BasicBlock *BB = *MI;
+ for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
+ BasicBlock *BB = *FI;
for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Instruction *Inst = *BI;
InstVal &IV = ValueState[Inst];
// The new constant inherits the old name of the operator...
if (Inst->hasName() && !Const->hasName())
- Const->setName(Inst->getName(), M->getSymbolTableSure());
+ Const->setName(Inst->getName(), F->getSymbolTableSure());
// Delete the operator now...
delete Inst;
}
}
+ // Reset state so that the next invokation will have empty data structures
+ BBExecutable.clear();
+ ValueState.clear();
+
// Merge identical constants last: this is important because we may have just
// introduced constants that already exist, and we don't want to pollute later
// stages with extraneous constants.
visit(I);
}
-
-namespace {
- // SCCPPass - Use Sparse Conditional Constant Propogation
- // to prove whether a value is constant and whether blocks are used.
- //
- struct SCCPPass : public FunctionPass {
- const char *getPassName() const {
- return "Sparse Conditional Constant Propogation";
- }
-
- inline bool runOnFunction(Function *F) {
- SCCP S(F);
- return S.doSCCP();
- }
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- // FIXME: SCCP does not preserve the CFG because it folds terminators!
- //AU.preservesCFG();
- }
- };
-}
-
-Pass *createSCCPPass() {
- return new SCCPPass();
-}