~ubuntu-branches/ubuntu/quantal/llvm-3.1/quantal

« back to all changes in this revision

Viewing changes to lib/Transforms/Scalar/ConstantProp.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-03-29 19:09:51 UTC
  • Revision ID: package-import@ubuntu.com-20120329190951-aq83ivog4cg8bxun
Tags: upstream-3.1~svn153643
ImportĀ upstreamĀ versionĀ 3.1~svn153643

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
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 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/Target/TargetData.h"
 
28
#include "llvm/Target/TargetLibraryInfo.h"
 
29
#include "llvm/Support/InstIterator.h"
 
30
#include "llvm/ADT/Statistic.h"
 
31
#include <set>
 
32
using namespace llvm;
 
33
 
 
34
STATISTIC(NumInstKilled, "Number of instructions killed");
 
35
 
 
36
namespace {
 
37
  struct ConstantPropagation : public FunctionPass {
 
38
    static char ID; // Pass identification, replacement for typeid
 
39
    ConstantPropagation() : FunctionPass(ID) {
 
40
      initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
 
41
    }
 
42
 
 
43
    bool runOnFunction(Function &F);
 
44
 
 
45
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
 
46
      AU.setPreservesCFG();
 
47
      AU.addRequired<TargetLibraryInfo>();
 
48
    }
 
49
  };
 
50
}
 
51
 
 
52
char ConstantPropagation::ID = 0;
 
53
INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
 
54
                "Simple constant propagation", false, false)
 
55
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
 
56
INITIALIZE_PASS_END(ConstantPropagation, "constprop",
 
57
                "Simple constant propagation", false, false)
 
58
 
 
59
FunctionPass *llvm::createConstantPropagationPass() {
 
60
  return new ConstantPropagation();
 
61
}
 
62
 
 
63
bool ConstantPropagation::runOnFunction(Function &F) {
 
64
  // Initialize the worklist to all of the instructions ready to process...
 
65
  std::set<Instruction*> WorkList;
 
66
  for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
 
67
      WorkList.insert(&*i);
 
68
  }
 
69
  bool Changed = false;
 
70
  TargetData *TD = getAnalysisIfAvailable<TargetData>();
 
71
  TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
 
72
 
 
73
  while (!WorkList.empty()) {
 
74
    Instruction *I = *WorkList.begin();
 
75
    WorkList.erase(WorkList.begin());    // Get an element from the worklist...
 
76
 
 
77
    if (!I->use_empty())                 // Don't muck with dead instructions...
 
78
      if (Constant *C = ConstantFoldInstruction(I, TD, TLI)) {
 
79
        // Add all of the users of this instruction to the worklist, they might
 
80
        // be constant propagatable now...
 
81
        for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
 
82
             UI != UE; ++UI)
 
83
          WorkList.insert(cast<Instruction>(*UI));
 
84
 
 
85
        // Replace all of the uses of a variable with uses of the constant.
 
86
        I->replaceAllUsesWith(C);
 
87
 
 
88
        // Remove the dead instruction.
 
89
        WorkList.erase(I);
 
90
        I->eraseFromParent();
 
91
 
 
92
        // We made a change to the function...
 
93
        Changed = true;
 
94
        ++NumInstKilled;
 
95
      }
 
96
  }
 
97
  return Changed;
 
98
}