blob: 8ed8238806d53695ee6c69a2b51655c6ee3e1c65 [file] [log] [blame]
Chris Lattner1467f642002-04-28 00:47:11 +00001//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1467f642002-04-28 00:47:11 +00009//
10// This pass is designed to be a very quick global transformation that
11// eliminates global common subexpressions from a function. It does this by
Chris Lattner1b09a9a2002-08-30 22:53:30 +000012// using an existing value numbering implementation to identify the common
13// subexpressions, eliminating them when possible.
Chris Lattner1467f642002-04-28 00:47:11 +000014//
Chris Lattner1467f642002-04-28 00:47:11 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner69c49002004-04-10 21:11:11 +000018#include "llvm/BasicBlock.h"
19#include "llvm/Constant.h"
20#include "llvm/Instructions.h"
Chris Lattner1b09a9a2002-08-30 22:53:30 +000021#include "llvm/Type.h"
Chris Lattner1467f642002-04-28 00:47:11 +000022#include "llvm/Analysis/Dominators.h"
Chris Lattnerb2a31092002-08-30 20:22:29 +000023#include "llvm/Analysis/ValueNumbering.h"
Chris Lattner69c49002004-04-10 21:11:11 +000024#include "llvm/Transforms/Utils/Local.h"
25#include "Support/DepthFirstIterator.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000026#include "Support/Statistic.h"
Chris Lattner1467f642002-04-28 00:47:11 +000027#include <algorithm>
Chris Lattner49525f82004-01-09 06:02:20 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner1467f642002-04-28 00:47:11 +000030namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000031 Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
32 Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
Chris Lattnercd832822004-03-15 05:46:59 +000033 Statistic<> NumCallRemoved("gcse", "Number of calls removed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000034 Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
Chris Lattnerb2a31092002-08-30 20:22:29 +000035 "to non-instruction values");
36
Chris Lattner69c49002004-04-10 21:11:11 +000037 struct GCSE : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000038 virtual bool runOnFunction(Function &F);
Chris Lattner1467f642002-04-28 00:47:11 +000039
Chris Lattner1467f642002-04-28 00:47:11 +000040 private:
Chris Lattner69c49002004-04-10 21:11:11 +000041 void ReplaceInstructionWith(Instruction *I, Value *V);
Chris Lattnerd38ddb12002-05-14 05:02:40 +000042
Chris Lattner1467f642002-04-28 00:47:11 +000043 // This transformation requires dominator and immediate dominator info
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000045 AU.setPreservesCFG();
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000046 AU.addRequired<DominatorSet>();
Chris Lattner69c49002004-04-10 21:11:11 +000047 AU.addRequired<DominatorTree>();
Chris Lattnerb2a31092002-08-30 20:22:29 +000048 AU.addRequired<ValueNumbering>();
Chris Lattner1467f642002-04-28 00:47:11 +000049 }
50 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000051
Chris Lattnerc8b70922002-07-26 21:12:46 +000052 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattner1467f642002-04-28 00:47:11 +000053}
54
55// createGCSEPass - The public interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000056FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattner1467f642002-04-28 00:47:11 +000057
Chris Lattner1467f642002-04-28 00:47:11 +000058// GCSE::runOnFunction - This is the main transformation entry point for a
59// function.
60//
Chris Lattner113f4f42002-06-25 16:13:24 +000061bool GCSE::runOnFunction(Function &F) {
Chris Lattner1467f642002-04-28 00:47:11 +000062 bool Changed = false;
63
Chris Lattner879cb972002-08-22 18:24:48 +000064 // Get pointers to the analysis results that we will be using...
Chris Lattner69c49002004-04-10 21:11:11 +000065 DominatorSet &DS = getAnalysis<DominatorSet>();
66 ValueNumbering &VN = getAnalysis<ValueNumbering>();
67 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner1467f642002-04-28 00:47:11 +000068
Chris Lattner69c49002004-04-10 21:11:11 +000069 std::vector<Value*> EqualValues;
Chris Lattner1467f642002-04-28 00:47:11 +000070
Chris Lattner69c49002004-04-10 21:11:11 +000071 // Traverse the CFG of the function in dominator order, so that we see each
72 // instruction after we see its operands.
73 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
74 E = df_end(DT.getRootNode()); DI != E; ++DI) {
75 BasicBlock *BB = DI->getBlock();
Chris Lattner1467f642002-04-28 00:47:11 +000076
Chris Lattner69c49002004-04-10 21:11:11 +000077 // Remember which instructions we've seen in this basic block as we scan.
78 std::set<Instruction*> BlockInsts;
Chris Lattnerb2a31092002-08-30 20:22:29 +000079
Chris Lattner69c49002004-04-10 21:11:11 +000080 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
81 Instruction *Inst = I++;
82
83 // If this instruction computes a value, try to fold together common
84 // instructions that compute it.
85 //
86 if (Inst->getType() != Type::VoidTy) {
87 VN.getEqualNumberNodes(Inst, EqualValues);
88
89 // If this instruction computes a value that is already computed
90 // elsewhere, try to recycle the old value.
91 if (!EqualValues.empty()) {
92 if (Inst == &*BB->begin())
93 I = BB->end();
94 else {
95 I = Inst; --I;
96 }
97
98 // First check to see if we were able to value number this instruction
99 // to a non-instruction value. If so, prefer that value over other
100 // instructions which may compute the same thing.
101 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
102 if (!isa<Instruction>(EqualValues[i])) {
103 ++NumNonInsts; // Keep track of # of insts repl with values
104
105 // Change all users of Inst to use the replacement and remove it
106 // from the program.
107 ReplaceInstructionWith(Inst, EqualValues[i]);
108 Inst = 0;
109 EqualValues.clear(); // don't enter the next loop
110 break;
111 }
112
113 // If there were no non-instruction values that this instruction
114 // produces, find a dominating instruction that produces the same
115 // value. If we find one, use it's value instead of ours.
116 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i) {
117 Instruction *OtherI = cast<Instruction>(EqualValues[i]);
118 bool Dominates = false;
119 if (OtherI->getParent() == BB)
120 Dominates = BlockInsts.count(OtherI);
121 else
122 Dominates = DS.dominates(OtherI->getParent(), BB);
123
124 if (Dominates) {
125 // Okay, we found an instruction with the same value as this one
126 // and that dominates this one. Replace this instruction with the
127 // specified one.
128 ReplaceInstructionWith(Inst, OtherI);
129 Inst = 0;
130 break;
131 }
132 }
133
134 EqualValues.clear();
135
136 if (Inst) {
137 I = Inst; ++I; // Deleted no instructions
138 } else if (I == BB->end()) { // Deleted first instruction
139 I = BB->begin();
140 } else { // Deleted inst in middle of block.
141 ++I;
142 }
143 }
144
145 if (Inst)
146 BlockInsts.insert(Inst);
147 }
Chris Lattnerb2a31092002-08-30 20:22:29 +0000148 }
Chris Lattner1467f642002-04-28 00:47:11 +0000149 }
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000150
Chris Lattner1467f642002-04-28 00:47:11 +0000151 // When the worklist is empty, return whether or not we changed anything...
152 return Changed;
153}
154
Chris Lattnerb2a31092002-08-30 20:22:29 +0000155
Chris Lattner69c49002004-04-10 21:11:11 +0000156void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
157 if (isa<LoadInst>(I))
158 ++NumLoadRemoved; // Keep track of loads eliminated
159 if (isa<CallInst>(I))
160 ++NumCallRemoved; // Keep track of calls eliminated
161 ++NumInstRemoved; // Keep track of number of insts eliminated
Chris Lattnerb2a31092002-08-30 20:22:29 +0000162
Chris Lattner69c49002004-04-10 21:11:11 +0000163 // If we are not replacing the instruction with a constant, we cannot do
164 // anything special.
165 if (!isa<Constant>(V)) {
166 I->replaceAllUsesWith(V);
Chris Lattnerb2a31092002-08-30 20:22:29 +0000167
Chris Lattner69c49002004-04-10 21:11:11 +0000168 // Erase the instruction from the program.
169 I->getParent()->getInstList().erase(I);
170 return;
171 }
Chris Lattnerb2a31092002-08-30 20:22:29 +0000172
Chris Lattner69c49002004-04-10 21:11:11 +0000173 Constant *C = cast<Constant>(V);
174 std::vector<User*> Users(I->use_begin(), I->use_end());
175
176 // Perform the replacement.
177 I->replaceAllUsesWith(C);
178
179 // Erase the instruction from the program.
180 I->getParent()->getInstList().erase(I);
Chris Lattnerb2a31092002-08-30 20:22:29 +0000181
Chris Lattner69c49002004-04-10 21:11:11 +0000182 // Check each user to see if we can constant fold it.
183 while (!Users.empty()) {
184 Instruction *U = cast<Instruction>(Users.back());
185 Users.pop_back();
Chris Lattnerb2a31092002-08-30 20:22:29 +0000186
Chris Lattner69c49002004-04-10 21:11:11 +0000187 if (Constant *C = ConstantFoldInstruction(U)) {
188 ReplaceInstructionWith(U, C);
189
190 // If the instruction used I more than once, it could be on the user list
191 // multiple times. Make sure we don't reprocess it.
192 std::vector<User*>::iterator It = std::find(Users.begin(), Users.end(),U);
193 while (It != Users.end()) {
194 Users.erase(It);
195 It = std::find(Users.begin(), Users.end(), U);
Chris Lattnerb2a31092002-08-30 20:22:29 +0000196 }
Chris Lattner1467f642002-04-28 00:47:11 +0000197 }
Chris Lattner1467f642002-04-28 00:47:11 +0000198 }
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000199}