blob: e949525d80db314aeb88a93f62e37f4b20ec2419 [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 Lattner46234fd2004-03-15 05:46:59 +000019#include "llvm/iOther.h"
Chris Lattner2964f362002-08-30 22:53:30 +000020#include "llvm/Type.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000021#include "llvm/Analysis/Dominators.h"
Chris Lattner14987f12002-08-30 20:22:29 +000022#include "llvm/Analysis/ValueNumbering.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000023#include "llvm/Support/InstIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000024#include "Support/Statistic.h"
Chris Lattner79fc8652004-02-05 22:33:19 +000025#include "Support/Debug.h"
Chris Lattnerd80e9732002-04-28 00:47:11 +000026#include <algorithm>
Chris Lattnerd7456022004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnerd80e9732002-04-28 00:47:11 +000029namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000030 Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
31 Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
Chris Lattner46234fd2004-03-15 05:46:59 +000032 Statistic<> NumCallRemoved("gcse", "Number of calls removed");
Chris Lattnera92f6962002-10-01 22:38:41 +000033 Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
Chris Lattner14987f12002-08-30 20:22:29 +000034 "to non-instruction values");
35
36 class GCSE : public FunctionPass {
Chris Lattner2964f362002-08-30 22:53:30 +000037 std::set<Instruction*> WorkList;
Chris Lattner18fb2a62002-05-14 05:02:40 +000038 DominatorSet *DomSetInfo;
Chris Lattner14987f12002-08-30 20:22:29 +000039 ValueNumbering *VN;
Chris Lattnerd80e9732002-04-28 00:47:11 +000040 public:
Chris Lattner7e708292002-06-25 16:13:24 +000041 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000042
Chris Lattnerd80e9732002-04-28 00:47:11 +000043 private:
Chris Lattner14987f12002-08-30 20:22:29 +000044 bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues);
45 Instruction *EliminateCSE(Instruction *I, Instruction *Other);
Chris Lattnerd80e9732002-04-28 00:47:11 +000046 void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
Chris Lattner18fb2a62002-05-14 05:02:40 +000047
Chris Lattnerd80e9732002-04-28 00:47:11 +000048 // This transformation requires dominator and immediate dominator info
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000050 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000051 AU.addRequired<DominatorSet>();
52 AU.addRequired<ImmediateDominators>();
Chris Lattner14987f12002-08-30 20:22:29 +000053 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000054 }
55 };
Chris Lattnerf6293092002-07-23 18:06:35 +000056
Chris Lattnera6275cc2002-07-26 21:12:46 +000057 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattnerd80e9732002-04-28 00:47:11 +000058}
59
60// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000061FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000062
Chris Lattnerd80e9732002-04-28 00:47:11 +000063// GCSE::runOnFunction - This is the main transformation entry point for a
64// function.
65//
Chris Lattner7e708292002-06-25 16:13:24 +000066bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000067 bool Changed = false;
68
Chris Lattnerd456ec92002-08-22 18:24:48 +000069 // Get pointers to the analysis results that we will be using...
Chris Lattnerd80e9732002-04-28 00:47:11 +000070 DomSetInfo = &getAnalysis<DominatorSet>();
Chris Lattner14987f12002-08-30 20:22:29 +000071 VN = &getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000072
73 // Step #1: Add all instructions in the function to the worklist for
74 // processing. All of the instructions are considered to be our
75 // subexpressions to eliminate if possible.
76 //
77 WorkList.insert(inst_begin(F), inst_end(F));
78
79 // Step #2: WorkList processing. Iterate through all of the instructions,
80 // checking to see if there are any additionally defined subexpressions in the
81 // program. If so, eliminate them!
82 //
83 while (!WorkList.empty()) {
Chris Lattner7e708292002-06-25 16:13:24 +000084 Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
Chris Lattnerd80e9732002-04-28 00:47:11 +000085 WorkList.erase(WorkList.begin());
86
Chris Lattner14987f12002-08-30 20:22:29 +000087 // If this instruction computes a value, try to fold together common
88 // instructions that compute it.
Chris Lattnerd80e9732002-04-28 00:47:11 +000089 //
Chris Lattner14987f12002-08-30 20:22:29 +000090 if (I.getType() != Type::VoidTy) {
91 std::vector<Value*> EqualValues;
92 VN->getEqualNumberNodes(&I, EqualValues);
93
94 if (!EqualValues.empty())
95 Changed |= EliminateRedundancies(&I, EqualValues);
96 }
Chris Lattnerd80e9732002-04-28 00:47:11 +000097 }
Chris Lattner18fb2a62002-05-14 05:02:40 +000098
Chris Lattnerd80e9732002-04-28 00:47:11 +000099 // When the worklist is empty, return whether or not we changed anything...
100 return Changed;
101}
102
Chris Lattner14987f12002-08-30 20:22:29 +0000103bool GCSE::EliminateRedundancies(Instruction *I,
104 std::vector<Value*> &EqualValues) {
105 // If the EqualValues set contains any non-instruction values, then we know
106 // that all of the instructions can be replaced with the non-instruction value
107 // because it is guaranteed to dominate all of the instructions in the
108 // function. We only have to do hard work if all we have are instructions.
109 //
110 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
111 if (!isa<Instruction>(EqualValues[i])) {
112 // Found a non-instruction. Replace all instructions with the
113 // non-instruction.
114 //
115 Value *Replacement = EqualValues[i];
116
117 // Make sure we get I as well...
118 EqualValues[i] = I;
119
120 // Replace all instructions with the Replacement value.
121 for (i = 0; i != e; ++i)
122 if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) {
123 // Change all users of I to use Replacement.
124 I->replaceAllUsesWith(Replacement);
125
126 if (isa<LoadInst>(I))
127 ++NumLoadRemoved; // Keep track of loads eliminated
Chris Lattner46234fd2004-03-15 05:46:59 +0000128 if (isa<CallInst>(I))
129 ++NumCallRemoved; // Keep track of calls eliminated
Chris Lattner14987f12002-08-30 20:22:29 +0000130 ++NumInstRemoved; // Keep track of number of instructions eliminated
131 ++NumNonInsts; // Keep track of number of insts repl with values
132
133 // Erase the instruction from the program.
134 I->getParent()->getInstList().erase(I);
Chris Lattnerbea68b32003-06-17 03:57:18 +0000135 WorkList.erase(I);
Chris Lattner14987f12002-08-30 20:22:29 +0000136 }
137
138 return true;
139 }
140
141 // Remove duplicate entries from EqualValues...
142 std::sort(EqualValues.begin(), EqualValues.end());
143 EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()),
144 EqualValues.end());
145
146 // From this point on, EqualValues is logically a vector of instructions.
147 //
148 bool Changed = false;
149 EqualValues.push_back(I); // Make sure I is included...
150 while (EqualValues.size() > 1) {
151 // FIXME, this could be done better than simple iteration!
152 Instruction *Test = cast<Instruction>(EqualValues.back());
153 EqualValues.pop_back();
154
155 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
156 if (Instruction *Ret = EliminateCSE(Test,
157 cast<Instruction>(EqualValues[i]))) {
158 if (Ret == Test) // Eliminated EqualValues[i]
159 EqualValues[i] = Test; // Make sure that we reprocess I at some point
160 Changed = true;
161 break;
162 }
163 }
164 return Changed;
165}
166
Chris Lattnerd80e9732002-04-28 00:47:11 +0000167
168// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
169// uses of the instruction use First now instead.
170//
171void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000172 Instruction &Second = *SI;
Chris Lattner79fc8652004-02-05 22:33:19 +0000173
174 DEBUG(std::cerr << "GCSE: Substituting %" << First->getName() << " for: "
175 << Second);
Chris Lattner8b054c02002-04-29 16:20:25 +0000176
177 //cerr << "DEL " << (void*)Second << Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000178
179 // Add the first instruction back to the worklist
180 WorkList.insert(First);
181
182 // Add all uses of the second instruction to the worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000183 for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
Chris Lattnerd80e9732002-04-28 00:47:11 +0000184 UI != UE; ++UI)
185 WorkList.insert(cast<Instruction>(*UI));
186
187 // Make all users of 'Second' now use 'First'
Chris Lattner7e708292002-06-25 16:13:24 +0000188 Second.replaceAllUsesWith(First);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000189
190 // Erase the second instruction from the program
Chris Lattner7e708292002-06-25 16:13:24 +0000191 Second.getParent()->getInstList().erase(SI);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000192}
193
Chris Lattner14987f12002-08-30 20:22:29 +0000194// EliminateCSE - The two instruction I & Other have been found to be common
195// subexpressions. This function is responsible for eliminating one of them,
196// and for fixing the worklist to be correct. The instruction that is preserved
197// is returned from the function if the other is eliminated, otherwise null is
198// returned.
Chris Lattnerd80e9732002-04-28 00:47:11 +0000199//
Chris Lattner14987f12002-08-30 20:22:29 +0000200Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) {
Chris Lattner18fb2a62002-05-14 05:02:40 +0000201 assert(I != Other);
Chris Lattner8b054c02002-04-29 16:20:25 +0000202
Chris Lattner18fb2a62002-05-14 05:02:40 +0000203 WorkList.erase(I);
Chris Lattner8b054c02002-04-29 16:20:25 +0000204 WorkList.erase(Other); // Other may not actually be on the worklist anymore...
Chris Lattnerd80e9732002-04-28 00:47:11 +0000205
Chris Lattnerd80e9732002-04-28 00:47:11 +0000206 // Handle the easy case, where both instructions are in the same basic block
207 BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
Chris Lattner14987f12002-08-30 20:22:29 +0000208 Instruction *Ret = 0;
209
Chris Lattnerd80e9732002-04-28 00:47:11 +0000210 if (BB1 == BB2) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000211 // Eliminate the second occurring instruction. Add all uses of the second
Chris Lattnerd80e9732002-04-28 00:47:11 +0000212 // instruction to the worklist.
213 //
214 // Scan the basic block looking for the "first" instruction
215 BasicBlock::iterator BI = BB1->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000216 while (&*BI != I && &*BI != Other) {
Chris Lattnerd80e9732002-04-28 00:47:11 +0000217 ++BI;
218 assert(BI != BB1->end() && "Instructions not found in parent BB!");
219 }
220
221 // Keep track of which instructions occurred first & second
Chris Lattner7e708292002-06-25 16:13:24 +0000222 Instruction *First = BI;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000223 Instruction *Second = I != First ? I : Other; // Get iterator to second inst
Chris Lattner7e708292002-06-25 16:13:24 +0000224 BI = Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000225
226 // Destroy Second, using First instead.
Chris Lattner14987f12002-08-30 20:22:29 +0000227 ReplaceInstWithInst(First, BI);
228 Ret = First;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000229
230 // Otherwise, the two instructions are in different basic blocks. If one
231 // dominates the other instruction, we can simply use it
232 //
233 } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
Chris Lattner7e708292002-06-25 16:13:24 +0000234 ReplaceInstWithInst(I, Other);
Chris Lattner14987f12002-08-30 20:22:29 +0000235 Ret = I;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000236 } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
Chris Lattner7e708292002-06-25 16:13:24 +0000237 ReplaceInstWithInst(Other, I);
Chris Lattner14987f12002-08-30 20:22:29 +0000238 Ret = Other;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000239 } else {
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000240 // This code is disabled because it has several problems:
241 // One, the actual assumption is wrong, as shown by this code:
242 // int "test"(int %X, int %Y) {
243 // %Z = add int %X, %Y
244 // ret int %Z
245 // Unreachable:
246 // %Q = add int %X, %Y
247 // ret int %Q
248 // }
249 //
250 // Here there are no shared dominators. Additionally, this had the habit of
251 // moving computations where they were not always computed. For example, in
Chris Lattnerbac74582003-02-01 04:50:59 +0000252 // a case like this:
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000253 // if (c) {
254 // if (d) ...
255 // else ... X+Y ...
256 // } else {
257 // ... X+Y ...
258 // }
259 //
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000260 // In this case, the expression would be hoisted to outside the 'if' stmt,
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000261 // causing the expression to be evaluated, even for the if (d) path, which
262 // could cause problems, if, for example, it caused a divide by zero. In
263 // general the problem this case is trying to solve is better addressed with
264 // PRE than GCSE.
265 //
Chris Lattner14987f12002-08-30 20:22:29 +0000266 return 0;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000267 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000268
Chris Lattner14987f12002-08-30 20:22:29 +0000269 if (isa<LoadInst>(Ret))
270 ++NumLoadRemoved; // Keep track of loads eliminated
Chris Lattner46234fd2004-03-15 05:46:59 +0000271 if (isa<CallInst>(Ret))
272 ++NumCallRemoved; // Keep track of calls eliminated
Chris Lattner14987f12002-08-30 20:22:29 +0000273 ++NumInstRemoved; // Keep track of number of instructions eliminated
Chris Lattnerd80e9732002-04-28 00:47:11 +0000274
Chris Lattner14987f12002-08-30 20:22:29 +0000275 // Add all users of Ret to the worklist...
276 for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I)
277 if (Instruction *Inst = dyn_cast<Instruction>(*I))
278 WorkList.insert(Inst);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000279
Chris Lattner14987f12002-08-30 20:22:29 +0000280 return Ret;
Chris Lattner18fb2a62002-05-14 05:02:40 +0000281}