blob: 013ddc0107c7bcc96f072f0328bf1ba521062bc5 [file] [log] [blame]
Chris Lattnere5485072002-05-06 18:21:31 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propogation -----===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
3// This file implements constant propogation and merging:
4//
5// Specifically, this:
Chris Lattnere5485072002-05-06 18:21:31 +00006// * Converts instructions like "add int 1, 2" into 3
Chris Lattner2f7c9632001-06-06 20:29:01 +00007//
8// Notice that:
9// * This pass has a habit of making definitions be dead. It is a good idea
Chris Lattnere5485072002-05-06 18:21:31 +000010// to to run a DIE pass sometime after running this pass.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattner560da702002-05-07 18:10:55 +000015#include "llvm/Transforms/Utils/Local.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000016#include "llvm/ConstantHandling.h"
Chris Lattner560da702002-05-07 18:10:55 +000017#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattnere5485072002-05-06 18:21:31 +000019#include "llvm/Support/InstIterator.h"
20#include <set>
Chris Lattner2f7c9632001-06-06 20:29:01 +000021
Chris Lattner0b18c1d2002-05-10 15:38:35 +000022#include "Support/StatisticReporter.h"
23static Statistic<> NumInstKilled("constprop - Number of instructions killed");
24
Chris Lattner04805fa2002-02-26 21:46:54 +000025namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000026 struct ConstantPropogation : public FunctionPass {
Chris Lattner37104aa2002-04-29 14:57:45 +000027 const char *getPassName() const { return "Simple Constant Propogation"; }
28
Chris Lattnere5485072002-05-06 18:21:31 +000029 inline bool runOnFunction(Function *F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000030
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnere5485072002-05-06 18:21:31 +000032 AU.preservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000033 }
Chris Lattner04805fa2002-02-26 21:46:54 +000034 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000035}
Chris Lattner04805fa2002-02-26 21:46:54 +000036
37Pass *createConstantPropogationPass() {
38 return new ConstantPropogation();
39}
40
Chris Lattnere5485072002-05-06 18:21:31 +000041
42bool ConstantPropogation::runOnFunction(Function *F) {
43 // Initialize the worklist to all of the instructions ready to process...
44 std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
45 bool Changed = false;
46
47 while (!WorkList.empty()) {
48 Instruction *I = *WorkList.begin();
49 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
50
51 if (!I->use_empty()) // Don't muck with dead instructions...
52 if (Constant *C = ConstantFoldInstruction(I)) {
53 // Add all of the users of this instruction to the worklist, they might
54 // be constant propogatable now...
55 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
56 UI != UE; ++UI)
57 WorkList.insert(cast<Instruction>(*UI));
58
59 // Replace all of the uses of a variable with uses of the constant.
60 I->replaceAllUsesWith(C);
Chris Lattner0b18c1d2002-05-10 15:38:35 +000061
Chris Lattnere5485072002-05-06 18:21:31 +000062 // We made a change to the function...
63 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000064 ++NumInstKilled;
Chris Lattnere5485072002-05-06 18:21:31 +000065 }
66 }
67 return Changed;
68}