blob: eb74b7c0a4484c15376f52a5f5bf246f38f7c3b1 [file] [log] [blame]
Chris Lattnerd80e9732002-04-28 00:47:11 +00001//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
John Criswellb576c942003-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 Lattnerd80e9732002-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 Lattner2964f362002-08-30 22:53:30 +000012// using an existing value numbering implementation to identify the common
13// subexpressions, eliminating them when possible.
Chris Lattnerd80e9732002-04-28 00:47:11 +000014//
Chris Lattnerd80e9732002-04-28 00:47:11 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner022103b2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000018#include "llvm/iMemory.h"
Chris Lattner2964f362002-08-30 22:53:30 +000019#include "llvm/Type.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000020#include "llvm/Analysis/Dominators.h"
Chris Lattner14987f12002-08-30 20:22:29 +000021#include "llvm/Analysis/ValueNumbering.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000022#include "llvm/Support/InstIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000023#include "Support/Statistic.h"
Chris Lattner79fc8652004-02-05 22:33:19 +000024#include "Support/Debug.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000025#include <algorithm>
Chris Lattnerd7456022004-01-09 06:02:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerd80e9732002-04-28 00:47:11 +000028namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000029 Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
30 Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
31 Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
Chris Lattner14987f12002-08-30 20:22:29 +000032 "to non-instruction values");
33
34 class GCSE : public FunctionPass {
Chris Lattner2964f362002-08-30 22:53:30 +000035 std::set<Instruction*> WorkList;
Chris Lattner18fb2a62002-05-14 05:02:40 +000036 DominatorSet *DomSetInfo;
Chris Lattner14987f12002-08-30 20:22:29 +000037 ValueNumbering *VN;
Chris Lattnerd80e9732002-04-28 00:47:11 +000038 public:
Chris Lattner7e708292002-06-25 16:13:24 +000039 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000040
Chris Lattnerd80e9732002-04-28 00:47:11 +000041 private:
Chris Lattner14987f12002-08-30 20:22:29 +000042 bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues);
43 Instruction *EliminateCSE(Instruction *I, Instruction *Other);
Chris Lattnerd80e9732002-04-28 00:47:11 +000044 void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
Chris Lattner18fb2a62002-05-14 05:02:40 +000045
Chris Lattnerd80e9732002-04-28 00:47:11 +000046 // This transformation requires dominator and immediate dominator info
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000048 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000049 AU.addRequired<DominatorSet>();
50 AU.addRequired<ImmediateDominators>();
Chris Lattner14987f12002-08-30 20:22:29 +000051 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000052 }
53 };
Chris Lattnerf6293092002-07-23 18:06:35 +000054
Chris Lattnera6275cc2002-07-26 21:12:46 +000055 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattnerd80e9732002-04-28 00:47:11 +000056}
57
58// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000059FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000060
Chris Lattnerd80e9732002-04-28 00:47:11 +000061// GCSE::runOnFunction - This is the main transformation entry point for a
62// function.
63//
Chris Lattner7e708292002-06-25 16:13:24 +000064bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000065 bool Changed = false;
66
Chris Lattnerd456ec92002-08-22 18:24:48 +000067 // Get pointers to the analysis results that we will be using...
Chris Lattnerd80e9732002-04-28 00:47:11 +000068 DomSetInfo = &getAnalysis<DominatorSet>();
Chris Lattner14987f12002-08-30 20:22:29 +000069 VN = &getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000070
71 // Step #1: Add all instructions in the function to the worklist for
72 // processing. All of the instructions are considered to be our
73 // subexpressions to eliminate if possible.
74 //
75 WorkList.insert(inst_begin(F), inst_end(F));
76
77 // Step #2: WorkList processing. Iterate through all of the instructions,
78 // checking to see if there are any additionally defined subexpressions in the
79 // program. If so, eliminate them!
80 //
81 while (!WorkList.empty()) {
Chris Lattner7e708292002-06-25 16:13:24 +000082 Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
Chris Lattnerd80e9732002-04-28 00:47:11 +000083 WorkList.erase(WorkList.begin());
84
Chris Lattner14987f12002-08-30 20:22:29 +000085 // If this instruction computes a value, try to fold together common
86 // instructions that compute it.
Chris Lattnerd80e9732002-04-28 00:47:11 +000087 //
Chris Lattner14987f12002-08-30 20:22:29 +000088 if (I.getType() != Type::VoidTy) {
89 std::vector<Value*> EqualValues;
90 VN->getEqualNumberNodes(&I, EqualValues);
91
92 if (!EqualValues.empty())
93 Changed |= EliminateRedundancies(&I, EqualValues);
94 }
Chris Lattnerd80e9732002-04-28 00:47:11 +000095 }
Chris Lattner18fb2a62002-05-14 05:02:40 +000096
Chris Lattnerd80e9732002-04-28 00:47:11 +000097 // When the worklist is empty, return whether or not we changed anything...
98 return Changed;
99}
100
Chris Lattner14987f12002-08-30 20:22:29 +0000101bool GCSE::EliminateRedundancies(Instruction *I,
102 std::vector<Value*> &EqualValues) {
103 // If the EqualValues set contains any non-instruction values, then we know
104 // that all of the instructions can be replaced with the non-instruction value
105 // because it is guaranteed to dominate all of the instructions in the
106 // function. We only have to do hard work if all we have are instructions.
107 //
108 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
109 if (!isa<Instruction>(EqualValues[i])) {
110 // Found a non-instruction. Replace all instructions with the
111 // non-instruction.
112 //
113 Value *Replacement = EqualValues[i];
114
115 // Make sure we get I as well...
116 EqualValues[i] = I;
117
118 // Replace all instructions with the Replacement value.
119 for (i = 0; i != e; ++i)
120 if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) {
121 // Change all users of I to use Replacement.
122 I->replaceAllUsesWith(Replacement);
123
124 if (isa<LoadInst>(I))
125 ++NumLoadRemoved; // Keep track of loads eliminated
126 ++NumInstRemoved; // Keep track of number of instructions eliminated
127 ++NumNonInsts; // Keep track of number of insts repl with values
128
129 // Erase the instruction from the program.
130 I->getParent()->getInstList().erase(I);
Chris Lattnerbea68b32003-06-17 03:57:18 +0000131 WorkList.erase(I);
Chris Lattner14987f12002-08-30 20:22:29 +0000132 }
133
134 return true;
135 }
136
137 // Remove duplicate entries from EqualValues...
138 std::sort(EqualValues.begin(), EqualValues.end());
139 EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()),
140 EqualValues.end());
141
142 // From this point on, EqualValues is logically a vector of instructions.
143 //
144 bool Changed = false;
145 EqualValues.push_back(I); // Make sure I is included...
146 while (EqualValues.size() > 1) {
147 // FIXME, this could be done better than simple iteration!
148 Instruction *Test = cast<Instruction>(EqualValues.back());
149 EqualValues.pop_back();
150
151 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
152 if (Instruction *Ret = EliminateCSE(Test,
153 cast<Instruction>(EqualValues[i]))) {
154 if (Ret == Test) // Eliminated EqualValues[i]
155 EqualValues[i] = Test; // Make sure that we reprocess I at some point
156 Changed = true;
157 break;
158 }
159 }
160 return Changed;
161}
162
Chris Lattnerd80e9732002-04-28 00:47:11 +0000163
164// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
165// uses of the instruction use First now instead.
166//
167void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000168 Instruction &Second = *SI;
Chris Lattner79fc8652004-02-05 22:33:19 +0000169
170 DEBUG(std::cerr << "GCSE: Substituting %" << First->getName() << " for: "
171 << Second);
Chris Lattner8b054c02002-04-29 16:20:25 +0000172
173 //cerr << "DEL " << (void*)Second << Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000174
175 // Add the first instruction back to the worklist
176 WorkList.insert(First);
177
178 // Add all uses of the second instruction to the worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000179 for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
Chris Lattnerd80e9732002-04-28 00:47:11 +0000180 UI != UE; ++UI)
181 WorkList.insert(cast<Instruction>(*UI));
182
183 // Make all users of 'Second' now use 'First'
Chris Lattner7e708292002-06-25 16:13:24 +0000184 Second.replaceAllUsesWith(First);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000185
186 // Erase the second instruction from the program
Chris Lattner7e708292002-06-25 16:13:24 +0000187 Second.getParent()->getInstList().erase(SI);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000188}
189
Chris Lattner14987f12002-08-30 20:22:29 +0000190// EliminateCSE - The two instruction I & Other have been found to be common
191// subexpressions. This function is responsible for eliminating one of them,
192// and for fixing the worklist to be correct. The instruction that is preserved
193// is returned from the function if the other is eliminated, otherwise null is
194// returned.
Chris Lattnerd80e9732002-04-28 00:47:11 +0000195//
Chris Lattner14987f12002-08-30 20:22:29 +0000196Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) {
Chris Lattner18fb2a62002-05-14 05:02:40 +0000197 assert(I != Other);
Chris Lattner8b054c02002-04-29 16:20:25 +0000198
Chris Lattner18fb2a62002-05-14 05:02:40 +0000199 WorkList.erase(I);
Chris Lattner8b054c02002-04-29 16:20:25 +0000200 WorkList.erase(Other); // Other may not actually be on the worklist anymore...
Chris Lattnerd80e9732002-04-28 00:47:11 +0000201
Chris Lattnerd80e9732002-04-28 00:47:11 +0000202 // Handle the easy case, where both instructions are in the same basic block
203 BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
Chris Lattner14987f12002-08-30 20:22:29 +0000204 Instruction *Ret = 0;
205
Chris Lattnerd80e9732002-04-28 00:47:11 +0000206 if (BB1 == BB2) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000207 // Eliminate the second occurring instruction. Add all uses of the second
Chris Lattnerd80e9732002-04-28 00:47:11 +0000208 // instruction to the worklist.
209 //
210 // Scan the basic block looking for the "first" instruction
211 BasicBlock::iterator BI = BB1->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000212 while (&*BI != I && &*BI != Other) {
Chris Lattnerd80e9732002-04-28 00:47:11 +0000213 ++BI;
214 assert(BI != BB1->end() && "Instructions not found in parent BB!");
215 }
216
217 // Keep track of which instructions occurred first & second
Chris Lattner7e708292002-06-25 16:13:24 +0000218 Instruction *First = BI;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000219 Instruction *Second = I != First ? I : Other; // Get iterator to second inst
Chris Lattner7e708292002-06-25 16:13:24 +0000220 BI = Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000221
222 // Destroy Second, using First instead.
Chris Lattner14987f12002-08-30 20:22:29 +0000223 ReplaceInstWithInst(First, BI);
224 Ret = First;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000225
226 // Otherwise, the two instructions are in different basic blocks. If one
227 // dominates the other instruction, we can simply use it
228 //
229 } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
Chris Lattner7e708292002-06-25 16:13:24 +0000230 ReplaceInstWithInst(I, Other);
Chris Lattner14987f12002-08-30 20:22:29 +0000231 Ret = I;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000232 } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
Chris Lattner7e708292002-06-25 16:13:24 +0000233 ReplaceInstWithInst(Other, I);
Chris Lattner14987f12002-08-30 20:22:29 +0000234 Ret = Other;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000235 } else {
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000236 // This code is disabled because it has several problems:
237 // One, the actual assumption is wrong, as shown by this code:
238 // int "test"(int %X, int %Y) {
239 // %Z = add int %X, %Y
240 // ret int %Z
241 // Unreachable:
242 // %Q = add int %X, %Y
243 // ret int %Q
244 // }
245 //
246 // Here there are no shared dominators. Additionally, this had the habit of
247 // moving computations where they were not always computed. For example, in
Chris Lattnerbac74582003-02-01 04:50:59 +0000248 // a case like this:
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000249 // if (c) {
250 // if (d) ...
251 // else ... X+Y ...
252 // } else {
253 // ... X+Y ...
254 // }
255 //
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000256 // In this case, the expression would be hoisted to outside the 'if' stmt,
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000257 // causing the expression to be evaluated, even for the if (d) path, which
258 // could cause problems, if, for example, it caused a divide by zero. In
259 // general the problem this case is trying to solve is better addressed with
260 // PRE than GCSE.
261 //
Chris Lattner14987f12002-08-30 20:22:29 +0000262 return 0;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000263 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000264
Chris Lattner14987f12002-08-30 20:22:29 +0000265 if (isa<LoadInst>(Ret))
266 ++NumLoadRemoved; // Keep track of loads eliminated
267 ++NumInstRemoved; // Keep track of number of instructions eliminated
Chris Lattnerd80e9732002-04-28 00:47:11 +0000268
Chris Lattner14987f12002-08-30 20:22:29 +0000269 // Add all users of Ret to the worklist...
270 for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I)
271 if (Instruction *Inst = dyn_cast<Instruction>(*I))
272 WorkList.insert(Inst);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000273
Chris Lattner14987f12002-08-30 20:22:29 +0000274 return Ret;
Chris Lattner18fb2a62002-05-14 05:02:40 +0000275}