blob: ea208135739d51ffc1d07d3bfa6aa38961d6e685 [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
Duncan Sands64fae2c2008-08-01 12:23:49 +000017// to run a DIE pass sometime after running this pass.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018//
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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/InstIterator.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
30using namespace llvm;
31
32STATISTIC(NumInstKilled, "Number of instructions killed");
33
34namespace {
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000035 struct ConstantPropagation : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000037 ConstantPropagation() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
39 bool runOnFunction(Function &F);
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.setPreservesCFG();
43 }
44 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045}
46
Dan Gohman089efff2008-05-13 00:00:25 +000047char ConstantPropagation::ID = 0;
48static RegisterPass<ConstantPropagation>
49X("constprop", "Simple constant propagation");
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051FunctionPass *llvm::createConstantPropagationPass() {
52 return new ConstantPropagation();
53}
54
55
56bool ConstantPropagation::runOnFunction(Function &F) {
57 // Initialize the worklist to all of the instructions ready to process...
58 std::set<Instruction*> WorkList;
59 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
60 WorkList.insert(&*i);
61 }
62 bool Changed = false;
63
64 while (!WorkList.empty()) {
65 Instruction *I = *WorkList.begin();
66 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
67
68 if (!I->use_empty()) // Don't muck with dead instructions...
Chris Lattner6070c012009-11-06 04:27:31 +000069 if (Constant *C = ConstantFoldInstruction(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 // Add all of the users of this instruction to the worklist, they might
71 // be constant propagatable now...
72 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
73 UI != UE; ++UI)
74 WorkList.insert(cast<Instruction>(*UI));
75
76 // Replace all of the uses of a variable with uses of the constant.
77 I->replaceAllUsesWith(C);
78
79 // Remove the dead instruction.
80 WorkList.erase(I);
Dan Gohmande087372008-06-21 22:08:46 +000081 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 // We made a change to the function...
84 Changed = true;
85 ++NumInstKilled;
86 }
87 }
88 return Changed;
89}