blob: 3d700cb4da4fd10c87492d1b58c7e9ee130d8b57 [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 Lattnerd80e9732002-04-28 00:47:11 +000024#include <algorithm>
Chris Lattnerd7456022004-01-09 06:02:20 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnerd80e9732002-04-28 00:47:11 +000027namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000028 Statistic<> NumInstRemoved("gcse", "Number of instructions removed");
29 Statistic<> NumLoadRemoved("gcse", "Number of loads removed");
30 Statistic<> NumNonInsts ("gcse", "Number of instructions removed due "
Chris Lattner14987f12002-08-30 20:22:29 +000031 "to non-instruction values");
32
33 class GCSE : public FunctionPass {
Chris Lattner2964f362002-08-30 22:53:30 +000034 std::set<Instruction*> WorkList;
Chris Lattner18fb2a62002-05-14 05:02:40 +000035 DominatorSet *DomSetInfo;
Chris Lattner14987f12002-08-30 20:22:29 +000036 ValueNumbering *VN;
Chris Lattnerd80e9732002-04-28 00:47:11 +000037 public:
Chris Lattner7e708292002-06-25 16:13:24 +000038 virtual bool runOnFunction(Function &F);
Chris Lattnerd80e9732002-04-28 00:47:11 +000039
Chris Lattnerd80e9732002-04-28 00:47:11 +000040 private:
Chris Lattner14987f12002-08-30 20:22:29 +000041 bool EliminateRedundancies(Instruction *I,std::vector<Value*> &EqualValues);
42 Instruction *EliminateCSE(Instruction *I, Instruction *Other);
Chris Lattnerd80e9732002-04-28 00:47:11 +000043 void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
Chris Lattner18fb2a62002-05-14 05:02:40 +000044
Chris Lattnerd80e9732002-04-28 00:47:11 +000045 // This transformation requires dominator and immediate dominator info
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000047 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000048 AU.addRequired<DominatorSet>();
49 AU.addRequired<ImmediateDominators>();
Chris Lattner14987f12002-08-30 20:22:29 +000050 AU.addRequired<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000051 }
52 };
Chris Lattnerf6293092002-07-23 18:06:35 +000053
Chris Lattnera6275cc2002-07-26 21:12:46 +000054 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattnerd80e9732002-04-28 00:47:11 +000055}
56
57// createGCSEPass - The public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000058FunctionPass *llvm::createGCSEPass() { return new GCSE(); }
Chris Lattnerd80e9732002-04-28 00:47:11 +000059
Chris Lattnerd80e9732002-04-28 00:47:11 +000060// GCSE::runOnFunction - This is the main transformation entry point for a
61// function.
62//
Chris Lattner7e708292002-06-25 16:13:24 +000063bool GCSE::runOnFunction(Function &F) {
Chris Lattnerd80e9732002-04-28 00:47:11 +000064 bool Changed = false;
65
Chris Lattnerd456ec92002-08-22 18:24:48 +000066 // Get pointers to the analysis results that we will be using...
Chris Lattnerd80e9732002-04-28 00:47:11 +000067 DomSetInfo = &getAnalysis<DominatorSet>();
Chris Lattner14987f12002-08-30 20:22:29 +000068 VN = &getAnalysis<ValueNumbering>();
Chris Lattnerd80e9732002-04-28 00:47:11 +000069
70 // Step #1: Add all instructions in the function to the worklist for
71 // processing. All of the instructions are considered to be our
72 // subexpressions to eliminate if possible.
73 //
74 WorkList.insert(inst_begin(F), inst_end(F));
75
76 // Step #2: WorkList processing. Iterate through all of the instructions,
77 // checking to see if there are any additionally defined subexpressions in the
78 // program. If so, eliminate them!
79 //
80 while (!WorkList.empty()) {
Chris Lattner7e708292002-06-25 16:13:24 +000081 Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
Chris Lattnerd80e9732002-04-28 00:47:11 +000082 WorkList.erase(WorkList.begin());
83
Chris Lattner14987f12002-08-30 20:22:29 +000084 // If this instruction computes a value, try to fold together common
85 // instructions that compute it.
Chris Lattnerd80e9732002-04-28 00:47:11 +000086 //
Chris Lattner14987f12002-08-30 20:22:29 +000087 if (I.getType() != Type::VoidTy) {
88 std::vector<Value*> EqualValues;
89 VN->getEqualNumberNodes(&I, EqualValues);
90
91 if (!EqualValues.empty())
92 Changed |= EliminateRedundancies(&I, EqualValues);
93 }
Chris Lattnerd80e9732002-04-28 00:47:11 +000094 }
Chris Lattner18fb2a62002-05-14 05:02:40 +000095
Chris Lattnerd80e9732002-04-28 00:47:11 +000096 // When the worklist is empty, return whether or not we changed anything...
97 return Changed;
98}
99
Chris Lattner14987f12002-08-30 20:22:29 +0000100bool GCSE::EliminateRedundancies(Instruction *I,
101 std::vector<Value*> &EqualValues) {
102 // If the EqualValues set contains any non-instruction values, then we know
103 // that all of the instructions can be replaced with the non-instruction value
104 // because it is guaranteed to dominate all of the instructions in the
105 // function. We only have to do hard work if all we have are instructions.
106 //
107 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
108 if (!isa<Instruction>(EqualValues[i])) {
109 // Found a non-instruction. Replace all instructions with the
110 // non-instruction.
111 //
112 Value *Replacement = EqualValues[i];
113
114 // Make sure we get I as well...
115 EqualValues[i] = I;
116
117 // Replace all instructions with the Replacement value.
118 for (i = 0; i != e; ++i)
119 if (Instruction *I = dyn_cast<Instruction>(EqualValues[i])) {
120 // Change all users of I to use Replacement.
121 I->replaceAllUsesWith(Replacement);
122
123 if (isa<LoadInst>(I))
124 ++NumLoadRemoved; // Keep track of loads eliminated
125 ++NumInstRemoved; // Keep track of number of instructions eliminated
126 ++NumNonInsts; // Keep track of number of insts repl with values
127
128 // Erase the instruction from the program.
129 I->getParent()->getInstList().erase(I);
Chris Lattnerbea68b32003-06-17 03:57:18 +0000130 WorkList.erase(I);
Chris Lattner14987f12002-08-30 20:22:29 +0000131 }
132
133 return true;
134 }
135
136 // Remove duplicate entries from EqualValues...
137 std::sort(EqualValues.begin(), EqualValues.end());
138 EqualValues.erase(std::unique(EqualValues.begin(), EqualValues.end()),
139 EqualValues.end());
140
141 // From this point on, EqualValues is logically a vector of instructions.
142 //
143 bool Changed = false;
144 EqualValues.push_back(I); // Make sure I is included...
145 while (EqualValues.size() > 1) {
146 // FIXME, this could be done better than simple iteration!
147 Instruction *Test = cast<Instruction>(EqualValues.back());
148 EqualValues.pop_back();
149
150 for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
151 if (Instruction *Ret = EliminateCSE(Test,
152 cast<Instruction>(EqualValues[i]))) {
153 if (Ret == Test) // Eliminated EqualValues[i]
154 EqualValues[i] = Test; // Make sure that we reprocess I at some point
155 Changed = true;
156 break;
157 }
158 }
159 return Changed;
160}
161
Chris Lattnerd80e9732002-04-28 00:47:11 +0000162
163// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
164// uses of the instruction use First now instead.
165//
166void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000167 Instruction &Second = *SI;
Chris Lattner8b054c02002-04-29 16:20:25 +0000168
169 //cerr << "DEL " << (void*)Second << Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000170
171 // Add the first instruction back to the worklist
172 WorkList.insert(First);
173
174 // Add all uses of the second instruction to the worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000175 for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
Chris Lattnerd80e9732002-04-28 00:47:11 +0000176 UI != UE; ++UI)
177 WorkList.insert(cast<Instruction>(*UI));
178
179 // Make all users of 'Second' now use 'First'
Chris Lattner7e708292002-06-25 16:13:24 +0000180 Second.replaceAllUsesWith(First);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000181
182 // Erase the second instruction from the program
Chris Lattner7e708292002-06-25 16:13:24 +0000183 Second.getParent()->getInstList().erase(SI);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000184}
185
Chris Lattner14987f12002-08-30 20:22:29 +0000186// EliminateCSE - The two instruction I & Other have been found to be common
187// subexpressions. This function is responsible for eliminating one of them,
188// and for fixing the worklist to be correct. The instruction that is preserved
189// is returned from the function if the other is eliminated, otherwise null is
190// returned.
Chris Lattnerd80e9732002-04-28 00:47:11 +0000191//
Chris Lattner14987f12002-08-30 20:22:29 +0000192Instruction *GCSE::EliminateCSE(Instruction *I, Instruction *Other) {
Chris Lattner18fb2a62002-05-14 05:02:40 +0000193 assert(I != Other);
Chris Lattner8b054c02002-04-29 16:20:25 +0000194
Chris Lattner18fb2a62002-05-14 05:02:40 +0000195 WorkList.erase(I);
Chris Lattner8b054c02002-04-29 16:20:25 +0000196 WorkList.erase(Other); // Other may not actually be on the worklist anymore...
Chris Lattnerd80e9732002-04-28 00:47:11 +0000197
Chris Lattnerd80e9732002-04-28 00:47:11 +0000198 // Handle the easy case, where both instructions are in the same basic block
199 BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
Chris Lattner14987f12002-08-30 20:22:29 +0000200 Instruction *Ret = 0;
201
Chris Lattnerd80e9732002-04-28 00:47:11 +0000202 if (BB1 == BB2) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000203 // Eliminate the second occurring instruction. Add all uses of the second
Chris Lattnerd80e9732002-04-28 00:47:11 +0000204 // instruction to the worklist.
205 //
206 // Scan the basic block looking for the "first" instruction
207 BasicBlock::iterator BI = BB1->begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000208 while (&*BI != I && &*BI != Other) {
Chris Lattnerd80e9732002-04-28 00:47:11 +0000209 ++BI;
210 assert(BI != BB1->end() && "Instructions not found in parent BB!");
211 }
212
213 // Keep track of which instructions occurred first & second
Chris Lattner7e708292002-06-25 16:13:24 +0000214 Instruction *First = BI;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000215 Instruction *Second = I != First ? I : Other; // Get iterator to second inst
Chris Lattner7e708292002-06-25 16:13:24 +0000216 BI = Second;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000217
218 // Destroy Second, using First instead.
Chris Lattner14987f12002-08-30 20:22:29 +0000219 ReplaceInstWithInst(First, BI);
220 Ret = First;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000221
222 // Otherwise, the two instructions are in different basic blocks. If one
223 // dominates the other instruction, we can simply use it
224 //
225 } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
Chris Lattner7e708292002-06-25 16:13:24 +0000226 ReplaceInstWithInst(I, Other);
Chris Lattner14987f12002-08-30 20:22:29 +0000227 Ret = I;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000228 } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
Chris Lattner7e708292002-06-25 16:13:24 +0000229 ReplaceInstWithInst(Other, I);
Chris Lattner14987f12002-08-30 20:22:29 +0000230 Ret = Other;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000231 } else {
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000232 // This code is disabled because it has several problems:
233 // One, the actual assumption is wrong, as shown by this code:
234 // int "test"(int %X, int %Y) {
235 // %Z = add int %X, %Y
236 // ret int %Z
237 // Unreachable:
238 // %Q = add int %X, %Y
239 // ret int %Q
240 // }
241 //
242 // Here there are no shared dominators. Additionally, this had the habit of
243 // moving computations where they were not always computed. For example, in
Chris Lattnerbac74582003-02-01 04:50:59 +0000244 // a case like this:
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000245 // if (c) {
246 // if (d) ...
247 // else ... X+Y ...
248 // } else {
249 // ... X+Y ...
250 // }
251 //
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000252 // In this case, the expression would be hoisted to outside the 'if' stmt,
Chris Lattnerbe1ecf62002-08-02 18:06:01 +0000253 // causing the expression to be evaluated, even for the if (d) path, which
254 // could cause problems, if, for example, it caused a divide by zero. In
255 // general the problem this case is trying to solve is better addressed with
256 // PRE than GCSE.
257 //
Chris Lattner14987f12002-08-30 20:22:29 +0000258 return 0;
Chris Lattnerd80e9732002-04-28 00:47:11 +0000259 }
Chris Lattnerd80e9732002-04-28 00:47:11 +0000260
Chris Lattner14987f12002-08-30 20:22:29 +0000261 if (isa<LoadInst>(Ret))
262 ++NumLoadRemoved; // Keep track of loads eliminated
263 ++NumInstRemoved; // Keep track of number of instructions eliminated
Chris Lattnerd80e9732002-04-28 00:47:11 +0000264
Chris Lattner14987f12002-08-30 20:22:29 +0000265 // Add all users of Ret to the worklist...
266 for (Value::use_iterator I = Ret->use_begin(), E = Ret->use_end(); I != E;++I)
267 if (Instruction *Inst = dyn_cast<Instruction>(*I))
268 WorkList.insert(Inst);
Chris Lattnerd80e9732002-04-28 00:47:11 +0000269
Chris Lattner14987f12002-08-30 20:22:29 +0000270 return Ret;
Chris Lattner18fb2a62002-05-14 05:02:40 +0000271}