1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
//
// This pass is designed to be a very quick global transformation that
// eliminates global common subexpressions from a function. It does this by
// examining the SSA value graph of the function, instead of doing slow, dense,
// bit-vector computations.
//
// This pass works best if it is proceeded with a simple constant propogation
// pass and an instruction combination pass because this pass does not do any
// value numbering (in order to be speedy).
//
// This pass does not attempt to CSE load instructions, because it does not use
// pointer analysis to determine when it is safe.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar/GCSE.h"
#include "llvm/Pass.h"
#include "llvm/InstrTypes.h"
#include "llvm/iMemory.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/InstIterator.h"
#include <set>
#include <algorithm>
namespace {
class GCSE : public FunctionPass, public InstVisitor<GCSE, bool> {
set<Instruction*> WorkList;
DominatorSet *DomSetInfo;
ImmediateDominators *ImmDominator;
public:
const char *getPassName() const {
return "Global Common Subexpression Elimination";
}
virtual bool runOnFunction(Function *F);
// Visitation methods, these are invoked depending on the type of
// instruction being checked. They should return true if a common
// subexpression was folded.
//
bool visitUnaryOperator(Instruction *I);
bool visitBinaryOperator(Instruction *I);
bool visitGetElementPtrInst(GetElementPtrInst *I);
bool visitCastInst(CastInst *I){return visitUnaryOperator((Instruction*)I);}
bool visitShiftInst(ShiftInst *I) {
return visitBinaryOperator((Instruction*)I);
}
bool visitInstruction(Instruction *) { return false; }
private:
void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
void CommonSubExpressionFound(Instruction *I, Instruction *Other);
// This transformation requires dominator and immediate dominator info
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.preservesCFG();
AU.addRequired(DominatorSet::ID);
AU.addRequired(ImmediateDominators::ID);
}
};
}
// createGCSEPass - The public interface to this file...
Pass *createGCSEPass() { return new GCSE(); }
// GCSE::runOnFunction - This is the main transformation entry point for a
// function.
//
bool GCSE::runOnFunction(Function *F) {
bool Changed = false;
DomSetInfo = &getAnalysis<DominatorSet>();
ImmDominator = &getAnalysis<ImmediateDominators>();
// Step #1: Add all instructions in the function to the worklist for
// processing. All of the instructions are considered to be our
// subexpressions to eliminate if possible.
//
WorkList.insert(inst_begin(F), inst_end(F));
// Step #2: WorkList processing. Iterate through all of the instructions,
// checking to see if there are any additionally defined subexpressions in the
// program. If so, eliminate them!
//
while (!WorkList.empty()) {
Instruction *I = *WorkList.begin(); // Get an instruction from the worklist
WorkList.erase(WorkList.begin());
// Visit the instruction, dispatching to the correct visit function based on
// the instruction type. This does the checking.
//
Changed |= visit(I);
}
// When the worklist is empty, return whether or not we changed anything...
return Changed;
}
// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
// uses of the instruction use First now instead.
//
void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Instruction *Second = *SI;
//cerr << "DEL " << (void*)Second << Second;
// Add the first instruction back to the worklist
WorkList.insert(First);
// Add all uses of the second instruction to the worklist
for (Value::use_iterator UI = Second->use_begin(), UE = Second->use_end();
UI != UE; ++UI)
WorkList.insert(cast<Instruction>(*UI));
// Make all users of 'Second' now use 'First'
Second->replaceAllUsesWith(First);
// Erase the second instruction from the program
delete Second->getParent()->getInstList().remove(SI);
}
// CommonSubExpressionFound - The two instruction I & Other have been found to
// be common subexpressions. This function is responsible for eliminating one
// of them, and for fixing the worklist to be correct.
//
void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
// I has already been removed from the worklist, Other needs to be.
assert(I != Other && WorkList.count(I) == 0 && "I shouldn't be on worklist!");
WorkList.erase(Other); // Other may not actually be on the worklist anymore...
// Handle the easy case, where both instructions are in the same basic block
BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
if (BB1 == BB2) {
// Eliminate the second occuring instruction. Add all uses of the second
// instruction to the worklist.
//
// Scan the basic block looking for the "first" instruction
BasicBlock::iterator BI = BB1->begin();
while (*BI != I && *BI != Other) {
++BI;
assert(BI != BB1->end() && "Instructions not found in parent BB!");
}
// Keep track of which instructions occurred first & second
Instruction *First = *BI;
Instruction *Second = I != First ? I : Other; // Get iterator to second inst
BI = find(BI, BB1->end(), Second);
assert(BI != BB1->end() && "Second instruction not found in parent block!");
// Destroy Second, using First instead.
ReplaceInstWithInst(First, BI);
// Otherwise, the two instructions are in different basic blocks. If one
// dominates the other instruction, we can simply use it
//
} else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
BasicBlock::iterator BI = find(BB2->begin(), BB2->end(), Other);
assert(BI != BB2->end() && "Other not in parent basic block!");
ReplaceInstWithInst(I, BI);
} else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
BasicBlock::iterator BI = find(BB1->begin(), BB1->end(), I);
assert(BI != BB1->end() && "I not in parent basic block!");
ReplaceInstWithInst(Other, BI);
} else {
// Handle the most general case now. In this case, neither I dom Other nor
// Other dom I. Because we are in SSA form, we are guaranteed that the
// operands of the two instructions both dominate the uses, so we _know_
// that there must exist a block that dominates both instructions (if the
// operands of the instructions are globals or constants, worst case we
// would get the entry node of the function). Search for this block now.
//
// Search up the immediate dominator chain of BB1 for the shared dominator
BasicBlock *SharedDom = (*ImmDominator)[BB1];
while (!DomSetInfo->dominates(SharedDom, BB2))
SharedDom = (*ImmDominator)[SharedDom];
// At this point, shared dom must dominate BOTH BB1 and BB2...
assert(SharedDom && DomSetInfo->dominates(SharedDom, BB1) &&
DomSetInfo->dominates(SharedDom, BB2) && "Dominators broken!");
// Rip 'I' out of BB1, and move it to the end of SharedDom.
BB1->getInstList().remove(I);
SharedDom->getInstList().insert(SharedDom->end()-1, I);
// Eliminate 'Other' now.
BasicBlock::iterator BI = find(BB2->begin(), BB2->end(), Other);
assert(BI != BB2->end() && "I not in parent basic block!");
ReplaceInstWithInst(I, BI);
}
}
//===----------------------------------------------------------------------===//
//
// Visitation methods, these are invoked depending on the type of instruction
// being checked. They should return true if a common subexpression was folded.
//
//===----------------------------------------------------------------------===//
bool GCSE::visitUnaryOperator(Instruction *I) {
Value *Op = I->getOperand(0);
Function *F = I->getParent()->getParent();
for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
UI != UE; ++UI)
if (Instruction *Other = dyn_cast<Instruction>(*UI))
// Check to see if this new binary operator is not I, but same operand...
if (Other != I && Other->getOpcode() == I->getOpcode() &&
Other->getOperand(0) == Op && // Is the operand the same?
// Is it embeded in the same function? (This could be false if LHS
// is a constant or global!)
Other->getParent()->getParent() == F &&
// Check that the types are the same, since this code handles casts...
Other->getType() == I->getType()) {
// These instructions are identical. Handle the situation.
CommonSubExpressionFound(I, Other);
return true; // One instruction eliminated!
}
return false;
}
bool GCSE::visitBinaryOperator(Instruction *I) {
Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
Function *F = I->getParent()->getParent();
for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
UI != UE; ++UI)
if (Instruction *Other = dyn_cast<Instruction>(*UI))
// Check to see if this new binary operator is not I, but same operand...
if (Other != I && Other->getOpcode() == I->getOpcode() &&
// Are the LHS and RHS the same?
Other->getOperand(0) == LHS && Other->getOperand(1) == RHS &&
// Is it embeded in the same function? (This could be false if LHS
// is a constant or global!)
Other->getParent()->getParent() == F) {
// These instructions are identical. Handle the situation.
CommonSubExpressionFound(I, Other);
return true; // One instruction eliminated!
}
return false;
}
bool GCSE::visitGetElementPtrInst(GetElementPtrInst *I) {
Value *Op = I->getOperand(0);
Function *F = I->getParent()->getParent();
for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
UI != UE; ++UI)
if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
// Check to see if this new binary operator is not I, but same operand...
if (Other != I && Other->getParent()->getParent() == F &&
Other->getType() == I->getType()) {
// Check to see that all operators past the 0th are the same...
unsigned i = 1, e = I->getNumOperands();
for (; i != e; ++i)
if (I->getOperand(i) != Other->getOperand(i)) break;
if (i == e) {
// These instructions are identical. Handle the situation.
CommonSubExpressionFound(I, Other);
return true; // One instruction eliminated!
}
}
return false;
}
|