blob: b467784b5e1d1640931ef08a9258fa24b701d2bb [file] [log] [blame]
Chris Lattnerd80e9732002-04-28 00:47:11 +00001//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
2//
3// This pass is designed to be a very quick global transformation that
4// eliminates global common subexpressions from a function. It does this by
Chris Lattner2964f362002-08-30 22:53:30 +00005// using an existing value numbering implementation to identify the common
6// subexpressions, eliminating them when possible.
Chris Lattnerd80e9732002-04-28 00:47:11 +00007//
Chris Lattnerd80e9732002-04-28 00:47:11 +00008//===----------------------------------------------------------------------===//
9
Chris Lattner022103b2002-05-07 20:03:00 +000010#include "llvm/Transforms/Scalar.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000011#include "llvm/iMemory.h"
Chris Lattner2964f362002-08-30 22:53:30 +000012#include "llvm/Type.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000013#include "llvm/Analysis/Dominators.h"
Chris Lattner14987f12002-08-30 20:22:29 +000014#include "llvm/Analysis/ValueNumbering.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000015#include "llvm/Support/InstIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000017#include <algorithm>
Chris Lattnerd80e9732002-04-28 00:47:11 +000018
19namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000020 Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
21 Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
22 Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
Chris Lattner14987f12002-08-30 20:22:29 +000023 "to non-instruction values");
24
25 class GCSE : public FunctionPass {
Chris Lattner2964f362002-08-30 22:53:30 +000026 std::set<Instruction*> WorkList;
Chris Lattner18fb2a62002-05-14 05:02:40 +000027 DominatorSet *DomSetInfo;
Chris Lattner14987f12002-08-30 20:22:29 +000028 ValueNumbering *VN;
Chris Lattnerd80e9732002-04-28 00:47:11 +000029 public:
Chris Lattner7e708292002-06-25 16:13:24 +000030 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000031
Chris Lattnerd80e9732002-04-28 00:47:11 +000032 private:
Chris Lattner14987f12002-08-30 20:22:29 +000033 bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues);
34 Instruction *EliminateCSE(Instruction *I, Instruction *Other);
Chris Lattnerd80e9732002-04-28 00:47:11 +000035 void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
Chris Lattner18fb2a62002-05-14 05:02:40 +000036
Chris Lattnerd80e9732002-04-28 00:47:11 +000037 // This transformation requires dominator and immediate dominator info
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000039 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000040 AU.addRequired<DominatorSet>();
41 AU.addRequired<ImmediateDominators>();
Chris Lattner14987f12002-08-30 20:22:29 +000042 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000043 }
44 };
Chris Lattnerf6293092002-07-23 18:06:35 +000045
Chris Lattnera6275cc2002-07-26 21:12:46 +000046 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattnerd80e9732002-04-28 00:47:11 +000047}
48
49// createGCSEPass - The public interface to this file...
50Pass *createGCSEPass() { return new GCSE(); }
51
52
53// GCSE::runOnFunction - This is the main transformation entry point for a
54// function.
55//
Chris Lattner7e708292002-06-25 16:13:24 +000056bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000057 bool Changed = false;
58
Chris Lattnerd456ec92002-08-22 18:24:48 +000059 // Get pointers to the analysis results that we will be using...
Chris Lattnerd80e9732002-04-28 00:47:11 +000060 DomSetInfo = &getAnalysis<DominatorSet>();
Chris Lattner14987f12002-08-30 20:22:29 +000061 VN = &getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000062
63 // Step #1: Add all instructions in the function to the worklist for
64 // processing. All of the instructions are considered to be our
65 // subexpressions to eliminate if possible.
66 //
67 WorkList.insert(inst_begin(F), inst_end(F));
68
69 // Step #2: WorkList processing. Iterate through all of the instructions,
70 // checking to see if there are any additionally defined subexpressions in the
71 // program. If so, eliminate them!
72 //
73 while (!WorkList.empty()) {
Chris Lattner7e708292002-06-25 16:13:24 +000074 Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
Chris Lattnerd80e9732002-04-28 00:47:11 +000075 WorkList.erase(WorkList.begin());
76
Chris Lattner14987f12002-08-30 20:22:29 +000077 // If this instruction computes a value, try to fold together common
78 // instructions that compute it.
Chris Lattnerd80e9732002-04-28 00:47:11 +000079 //
Chris Lattner14987f12002-08-30 20:22:29 +000080 if (I.getType() != Type::VoidTy) {
81 std::vector<Value*> EqualValues;
82 VN->getEqualNumberNodes(&I, EqualValues);
83
84 if (!EqualValues.empty())
85 Changed |= EliminateRedundancies(&I, EqualValues);
86 }
Chris Lattnerd80e9732002-04-28 00:47:11 +000087 }
Chris Lattner18fb2a62002-05-14 05:02:40 +000088
Chris Lattnerd80e9732002-04-28 00:47:11 +000089 // When the worklist is empty, return whether or not we changed anything...
90 return Changed;
91}
92
Chris Lattner14987f12002-08-30 20:22:29 +000093bool GCSE::EliminateRedundancies(Instruction *I,
94 std::vector<Value*> &EqualValues) {
95 // If the EqualValues set contains any non-instruction values, then we know
96 // that all of the instructions can be replaced with the non-instruction value
97 // because it is guaranteed to dominate all of the instructions in the
98 // function. We only have to do hard work if all we have are instructions.
99 //
100 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
101 if (!isa<Instruction>(EqualValues[i])) {
102 // Found a non-instruction. Replace all instructions with the
103 // non-instruction.
104 //
105 Value *Replacement = EqualValues[i];
106
107 // Make sure we get I as well...
108 EqualValues[i] = I;
109
110 // Replace all instructions with the Replacement value.
111 for (i = 0; i != e; ++i)
112 if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) {
113 // Change all users of I to use Replacement.
114 I->replaceAllUsesWith(Replacement);
115
116 if (isa<LoadInst>(I))
117 ++NumLoadRemoved; // Keep track of loads eliminated
118 ++NumInstRemoved; // Keep track of number of instructions eliminated
119 ++NumNonInsts; // Keep track of number of insts repl with values
120
121 // Erase the instruction from the program.
122 I->getParent()->getInstList().erase(I);
123 }
124
125 return true;
126 }
127
128 // Remove duplicate entries from EqualValues...
129 std::sort(EqualValues.begin(), EqualValues.end());
130 EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()),
131 EqualValues.end());
132
133 // From this point on, EqualValues is logically a vector of instructions.
134 //
135 bool Changed = false;
136 EqualValues.push_back(I); // Make sure I is included...
137 while (EqualValues.size() > 1) {
138 // FIXME, this could be done better than simple iteration!
139 Instruction *Test = cast<Instruction>(EqualValues.back());
140 EqualValues.pop_back();
141
142 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
143 if (Instruction *Ret = EliminateCSE(Test,
144 cast<Instruction>(EqualValues[i]))) {
145 if (Ret == Test) // Eliminated EqualValues[i]
146 EqualValues[i] = Test; // Make sure that we reprocess I at some point
147 Changed = true;
148 break;
149 }
150 }
151 return Changed;
152}
153
Chris Lattnerd80e9732002-04-28 00:47:11 +0000154
155// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
156// uses of the instruction use First now instead.
157//
158void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000159 Instruction &Second = *SI;
Chris Lattner8b054c02002-04-29 16:20:25 +0000160
161 //cerr << "DEL " << (void*)Second << Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000162
163 // Add the first instruction back to the worklist
164 WorkList.insert(First);
165
166 // Add all uses of the second instruction to the worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000167 for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
Chris Lattnerd80e9732002-04-28 00:47:11 +0000168 UI != UE; ++UI)
169 WorkList.insert(cast<Instruction>(*UI));
170
171 // Make all users of 'Second' now use 'First'
Chris Lattner7e708292002-06-25 16:13:24 +0000172 Second.replaceAllUsesWith(First);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000173
174 // Erase the second instruction from the program
Chris Lattner7e708292002-06-25 16:13:24 +0000175 Second.getParent()->getInstList().erase(SI);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000176}
177
Chris Lattner14987f12002-08-30 20:22:29 +0000178// EliminateCSE - The two instruction I & Other have been found to be common
179// subexpressions. This function is responsible for eliminating one of them,
180// and for fixing the worklist to be correct. The instruction that is preserved
181// is returned from the function if the other is eliminated, otherwise null is
182// returned.
Chris Lattnerd80e9732002-04-28 00:47:11 +0000183//
Chris Lattner14987f12002-08-30 20:22:29 +0000184Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) {
Chris Lattner18fb2a62002-05-14 05:02:40 +0000185 assert(I != Other);
Chris Lattner8b054c02002-04-29 16:20:25 +0000186
Chris Lattner18fb2a62002-05-14 05:02:40 +0000187 WorkList.erase(I);
Chris Lattner8b054c02002-04-29 16:20:25 +0000188 WorkList.erase(Other); // Other may not actually be on the worklist anymore...
Chris Lattnerd80e9732002-04-28 00:47:11 +0000189
Chris Lattnerd80e9732002-04-28 00:47:11 +0000190 // Handle the easy case, where both instructions are in the same basic block
191 BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
Chris Lattner14987f12002-08-30 20:22:29 +0000192 Instruction *Ret = 0;
193
Chris Lattnerd80e9732002-04-28 00:47:11 +0000194 if (BB1 == BB2) {
195 // Eliminate the second occuring instruction. Add all uses of the second
196 // instruction to the worklist.
197 //
198 // Scan the basic block looking for the "first" instruction
199 BasicBlock::iterator BI = BB1->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000200 while (&*BI != I && &*BI != Other) {
Chris Lattnerd80e9732002-04-28 00:47:11 +0000201 ++BI;
202 assert(BI != BB1->end() && "Instructions not found in parent BB!");
203 }
204
205 // Keep track of which instructions occurred first & second
Chris Lattner7e708292002-06-25 16:13:24 +0000206 Instruction *First = BI;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000207 Instruction *Second = I != First ? I : Other; // Get iterator to second inst
Chris Lattner7e708292002-06-25 16:13:24 +0000208 BI = Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000209
210 // Destroy Second, using First instead.
Chris Lattner14987f12002-08-30 20:22:29 +0000211 ReplaceInstWithInst(First, BI);
212 Ret = First;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000213
214 // Otherwise, the two instructions are in different basic blocks. If one
215 // dominates the other instruction, we can simply use it
216 //
217 } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
Chris Lattner7e708292002-06-25 16:13:24 +0000218 ReplaceInstWithInst(I, Other);
Chris Lattner14987f12002-08-30 20:22:29 +0000219 Ret = I;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000220 } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
Chris Lattner7e708292002-06-25 16:13:24 +0000221 ReplaceInstWithInst(Other, I);
Chris Lattner14987f12002-08-30 20:22:29 +0000222 Ret = Other;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000223 } else {
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000224 // This code is disabled because it has several problems:
225 // One, the actual assumption is wrong, as shown by this code:
226 // int "test"(int %X, int %Y) {
227 // %Z = add int %X, %Y
228 // ret int %Z
229 // Unreachable:
230 // %Q = add int %X, %Y
231 // ret int %Q
232 // }
233 //
234 // Here there are no shared dominators. Additionally, this had the habit of
235 // moving computations where they were not always computed. For example, in
Chris Lattnerbac74582003-02-01 04:50:59 +0000236 // a case like this:
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000237 // if (c) {
238 // if (d) ...
239 // else ... X+Y ...
240 // } else {
241 // ... X+Y ...
242 // }
243 //
244 // In thiscase, the expression would be hoisted to outside the 'if' stmt,
245 // causing the expression to be evaluated, even for the if (d) path, which
246 // could cause problems, if, for example, it caused a divide by zero. In
247 // general the problem this case is trying to solve is better addressed with
248 // PRE than GCSE.
249 //
Chris Lattner14987f12002-08-30 20:22:29 +0000250 return 0;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000251 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000252
Chris Lattner14987f12002-08-30 20:22:29 +0000253 if (isa<LoadInst>(Ret))
254 ++NumLoadRemoved; // Keep track of loads eliminated
255 ++NumInstRemoved; // Keep track of number of instructions eliminated
Chris Lattnerd80e9732002-04-28 00:47:11 +0000256
Chris Lattner14987f12002-08-30 20:22:29 +0000257 // Add all users of Ret to the worklist...
258 for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I)
259 if (Instruction *Inst = dyn_cast<Instruction>(*I))
260 WorkList.insert(Inst);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000261
Chris Lattner14987f12002-08-30 20:22:29 +0000262 return Ret;
Chris Lattner18fb2a62002-05-14 05:02:40 +0000263}