2002-05-06 18:21:31 +00:00
|
|
|
//===- ConstantProp.cpp - Code to perform Simple Constant Propogation -----===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file implements constant propogation and merging:
|
|
|
|
//
|
|
|
|
// Specifically, this:
|
2002-05-06 18:21:31 +00:00
|
|
|
// * Converts instructions like "add int 1, 2" into 3
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// Notice that:
|
|
|
|
// * This pass has a habit of making definitions be dead. It is a good idea
|
2002-05-06 18:21:31 +00:00
|
|
|
// to to run a DIE pass sometime after running this pass.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/Scalar/ConstantProp.h"
|
2002-05-07 18:10:55 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2002-04-08 20:18:09 +00:00
|
|
|
#include "llvm/ConstantHandling.h"
|
2002-05-07 18:10:55 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-05-06 18:21:31 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
|
|
|
#include <set>
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct ConstantPropogation : public FunctionPass {
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Simple Constant Propogation"; }
|
|
|
|
|
2002-05-06 18:21:31 +00:00
|
|
|
inline bool runOnFunction(Function *F);
|
2002-04-28 21:27:06 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-05-06 18:21:31 +00:00
|
|
|
AU.preservesCFG();
|
2002-04-28 21:27:06 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
Pass *createConstantPropogationPass() {
|
|
|
|
return new ConstantPropogation();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-05-06 18:21:31 +00:00
|
|
|
|
|
|
|
bool ConstantPropogation::runOnFunction(Function *F) {
|
|
|
|
// Initialize the worklist to all of the instructions ready to process...
|
|
|
|
std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
Instruction *I = *WorkList.begin();
|
|
|
|
WorkList.erase(WorkList.begin()); // Get an element from the worklist...
|
|
|
|
|
|
|
|
if (!I->use_empty()) // Don't muck with dead instructions...
|
|
|
|
if (Constant *C = ConstantFoldInstruction(I)) {
|
|
|
|
// Add all of the users of this instruction to the worklist, they might
|
|
|
|
// be constant propogatable now...
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
WorkList.insert(cast<Instruction>(*UI));
|
|
|
|
|
|
|
|
// Replace all of the uses of a variable with uses of the constant.
|
|
|
|
I->replaceAllUsesWith(C);
|
|
|
|
|
|
|
|
// We made a change to the function...
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|