1*9880d681SAndroid Build Coastguard Worker //===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements constant propagation and merging:
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // Specifically, this:
13*9880d681SAndroid Build Coastguard Worker // * Converts instructions like "add int 1, 2" into 3
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // Notice that:
16*9880d681SAndroid Build Coastguard Worker // * This pass has a habit of making definitions be dead. It is a good idea
17*9880d681SAndroid Build Coastguard Worker // to run a DIE pass sometime after running this pass.
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ConstantFolding.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constant.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include <set>
30*9880d681SAndroid Build Coastguard Worker using namespace llvm;
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "constprop"
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker STATISTIC(NumInstKilled, "Number of instructions killed");
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker namespace {
37*9880d681SAndroid Build Coastguard Worker struct ConstantPropagation : public FunctionPass {
38*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
ConstantPropagation__anone46665560111::ConstantPropagation39*9880d681SAndroid Build Coastguard Worker ConstantPropagation() : FunctionPass(ID) {
40*9880d681SAndroid Build Coastguard Worker initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override;
44*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anone46665560111::ConstantPropagation45*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
46*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
47*9880d681SAndroid Build Coastguard Worker AU.addRequired<TargetLibraryInfoWrapperPass>();
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker };
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker char ConstantPropagation::ID = 0;
53*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54*9880d681SAndroid Build Coastguard Worker "Simple constant propagation", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)55*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
56*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(ConstantPropagation, "constprop",
57*9880d681SAndroid Build Coastguard Worker "Simple constant propagation", false, false)
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createConstantPropagationPass() {
60*9880d681SAndroid Build Coastguard Worker return new ConstantPropagation();
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)63*9880d681SAndroid Build Coastguard Worker bool ConstantPropagation::runOnFunction(Function &F) {
64*9880d681SAndroid Build Coastguard Worker if (skipFunction(F))
65*9880d681SAndroid Build Coastguard Worker return false;
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker // Initialize the worklist to all of the instructions ready to process...
68*9880d681SAndroid Build Coastguard Worker std::set<Instruction*> WorkList;
69*9880d681SAndroid Build Coastguard Worker for (Instruction &I: instructions(&F))
70*9880d681SAndroid Build Coastguard Worker WorkList.insert(&I);
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker bool Changed = false;
73*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = F.getParent()->getDataLayout();
74*9880d681SAndroid Build Coastguard Worker TargetLibraryInfo *TLI =
75*9880d681SAndroid Build Coastguard Worker &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker while (!WorkList.empty()) {
78*9880d681SAndroid Build Coastguard Worker Instruction *I = *WorkList.begin();
79*9880d681SAndroid Build Coastguard Worker WorkList.erase(WorkList.begin()); // Get an element from the worklist...
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker if (!I->use_empty()) // Don't muck with dead instructions...
82*9880d681SAndroid Build Coastguard Worker if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
83*9880d681SAndroid Build Coastguard Worker // Add all of the users of this instruction to the worklist, they might
84*9880d681SAndroid Build Coastguard Worker // be constant propagatable now...
85*9880d681SAndroid Build Coastguard Worker for (User *U : I->users())
86*9880d681SAndroid Build Coastguard Worker WorkList.insert(cast<Instruction>(U));
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker // Replace all of the uses of a variable with uses of the constant.
89*9880d681SAndroid Build Coastguard Worker I->replaceAllUsesWith(C);
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker // Remove the dead instruction.
92*9880d681SAndroid Build Coastguard Worker WorkList.erase(I);
93*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker // We made a change to the function...
96*9880d681SAndroid Build Coastguard Worker Changed = true;
97*9880d681SAndroid Build Coastguard Worker ++NumInstKilled;
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker return Changed;
101*9880d681SAndroid Build Coastguard Worker }
102