blob: 60b915a1b5e43baf2478999389f25059b552cf72 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements constant propagation and merging:
11//
12// Specifically, this:
13// * Converts instructions like "add int 1, 2" into 3
14//
15// Notice that:
16// * This pass has a habit of making definitions be dead. It is a good idea
17// to to run a DIE pass sometime after running this pass.
18//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "constprop"
22#include "llvm/Transforms/Scalar.h"
23#include "llvm/Analysis/ConstantFolding.h"
24#include "llvm/Constant.h"
25#include "llvm/Instruction.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/InstIterator.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
31using namespace llvm;
32
33STATISTIC(NumInstKilled, "Number of instructions killed");
34
35namespace {
36 struct VISIBILITY_HIDDEN ConstantPropagation : public FunctionPass {
37 static char ID; // Pass identification, replacement for typeid
38 ConstantPropagation() : FunctionPass((intptr_t)&ID) {}
39
40 bool runOnFunction(Function &F);
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesCFG();
44 }
45 };
46
47 char ConstantPropagation::ID = 0;
48 RegisterPass<ConstantPropagation> X("constprop",
49 "Simple constant propagation");
50}
51
52FunctionPass *llvm::createConstantPropagationPass() {
53 return new ConstantPropagation();
54}
55
56
57bool ConstantPropagation::runOnFunction(Function &F) {
58 // Initialize the worklist to all of the instructions ready to process...
59 std::set<Instruction*> WorkList;
60 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
61 WorkList.insert(&*i);
62 }
63 bool Changed = false;
64
65 while (!WorkList.empty()) {
66 Instruction *I = *WorkList.begin();
67 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
68
69 if (!I->use_empty()) // Don't muck with dead instructions...
70 if (Constant *C = ConstantFoldInstruction(I)) {
71 // Add all of the users of this instruction to the worklist, they might
72 // be constant propagatable now...
73 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
74 UI != UE; ++UI)
75 WorkList.insert(cast<Instruction>(*UI));
76
77 // Replace all of the uses of a variable with uses of the constant.
78 I->replaceAllUsesWith(C);
79
80 // Remove the dead instruction.
81 WorkList.erase(I);
82 I->getParent()->getInstList().erase(I);
83
84 // We made a change to the function...
85 Changed = true;
86 ++NumInstKilled;
87 }
88 }
89 return Changed;
90}