2003-05-20 21:01:22 +00:00
|
|
|
//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2003-05-20 21:01:22 +00:00
|
|
|
// This file implements constant propagation and merging:
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// 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-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-05-07 18:10:55 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-01-12 19:15:20 +00:00
|
|
|
#include "llvm/Constant.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"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-06 18:21:31 +00:00
|
|
|
#include <set>
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<> NumInstKilled("constprop", "Number of instructions killed");
|
|
|
|
|
2003-05-20 21:01:22 +00:00
|
|
|
struct ConstantPropagation : public FunctionPass {
|
2002-06-25 16:13:24 +00:00
|
|
|
bool runOnFunction(Function &F);
|
2002-04-28 21:27:06 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-21 20:00:28 +00:00
|
|
|
AU.setPreservesCFG();
|
2002-04-28 21:27:06 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
2002-07-23 18:06:35 +00:00
|
|
|
|
2003-05-20 21:01:22 +00:00
|
|
|
RegisterOpt<ConstantPropagation> X("constprop","Simple constant propagation");
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-09 06:02:20 +00:00
|
|
|
Pass *llvm::createConstantPropagationPass() {
|
2003-05-20 21:01:22 +00:00
|
|
|
return new ConstantPropagation();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-05-06 18:21:31 +00:00
|
|
|
|
2003-05-20 21:01:22 +00:00
|
|
|
bool ConstantPropagation::runOnFunction(Function &F) {
|
2002-05-06 18:21:31 +00:00
|
|
|
// Initialize the worklist to all of the instructions ready to process...
|
2004-04-27 15:13:33 +00:00
|
|
|
std::set<Instruction*> WorkList;
|
|
|
|
for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
|
|
|
|
WorkList.insert(&*i);
|
|
|
|
}
|
2002-05-06 18:21:31 +00:00
|
|
|
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
|
2003-05-20 21:01:22 +00:00
|
|
|
// be constant propagatable now...
|
2002-05-06 18:21:31 +00:00
|
|
|
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);
|
2002-05-10 15:38:35 +00:00
|
|
|
|
2004-04-13 19:28:20 +00:00
|
|
|
// Remove the dead instruction.
|
|
|
|
WorkList.erase(I);
|
|
|
|
I->getParent()->getInstList().erase(I);
|
|
|
|
|
2002-05-06 18:21:31 +00:00
|
|
|
// We made a change to the function...
|
|
|
|
Changed = true;
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumInstKilled;
|
2002-05-06 18:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|