llvm.org GIT mirror llvm / fee76bd
Begin loop index split pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40883 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 12 years ago
3 changed file(s) with 391 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
8282 (void) llvm::createLoopUnrollPass();
8383 (void) llvm::createLoopUnswitchPass();
8484 (void) llvm::createLoopRotatePass();
85 (void) llvm::createLoopIndexSplitPass();
8586 (void) llvm::createLowerAllocationsPass();
8687 (void) llvm::createLowerGCPass();
8788 (void) llvm::createLowerInvokePass();
140140 //
141141 LoopPass *createLoopRotatePass();
142142
143 //===----------------------------------------------------------------------===//
144 //
145 // LoopIndexSplit - This pass splits loop
146 //
147 LoopPass *createLoopIndexSplitPass();
148
143149
144150 //===----------------------------------------------------------------------===//
145151 //
0 //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Devang Patel and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements Loop Index Splitting Pass.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "loop-index-split"
14
15 #include "llvm/Function.h"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Analysis/LoopPass.h"
18 #include "llvm/Analysis/ScalarEvolutionExpander.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/ADT/Statistic.h"
21
22 using namespace llvm;
23
24 STATISTIC(NumIndexSplit, "Number of loops index split");
25
26 namespace {
27
28 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
29
30 public:
31 static char ID; // Pass ID, replacement for typeid
32 LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
33
34 // Index split Loop L. Return true if loop is split.
35 bool runOnLoop(Loop *L, LPPassManager &LPM);
36
37 void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.addRequired();
39 AU.addPreserved();
40 AU.addRequiredID(LCSSAID);
41 AU.addPreservedID(LCSSAID);
42 AU.addPreserved();
43 AU.addRequiredID(LoopSimplifyID);
44 AU.addPreservedID(LoopSimplifyID);
45 }
46
47 private:
48 /// Find condition inside a loop that is suitable candidate for index split.
49 void findSplitCondition();
50
51 /// processOneIterationLoop - Current loop L contains compare instruction
52 /// that compares induction variable, IndVar, agains loop invariant. If
53 /// entire (i.e. meaningful) loop body is dominated by this compare
54 /// instruction then loop body is executed only for one iteration. In
55 /// such case eliminate loop structure surrounding this loop body. For
56 bool processOneIterationLoop(LPPassManager &LPM);
57
58 // If loop header includes loop variant instruction operands then
59 // this loop may not be eliminated.
60 bool safeHeader(BasicBlock *BB);
61
62 // If Exit block includes loop variant instructions then this
63 // loop may not be eliminated.
64 bool safeExitBlock(BasicBlock *BB);
65
66 bool splitLoop();
67
68 private:
69
70 // Current Loop.
71 Loop *L;
72 ScalarEvolution *SE;
73
74 // Induction variable whose range is being split by this transformation.
75 PHINode *IndVar;
76
77 // Induction variable's range is split at this value.
78 Value *SplitValue;
79
80 // Induction variable's final loop exit value.
81 Value *ExitValue;
82
83 // This compare instruction compares IndVar against SplitValue.
84 ICmpInst *SplitCondition;
85 };
86
87 char LoopIndexSplit::ID = 0;
88 RegisterPass X ("loop-index-split", "Index Split Loops");
89 }
90
91 LoopPass *llvm::createLoopIndexSplitPass() {
92 return new LoopIndexSplit();
93 }
94
95 // Index split Loop L. Return true if loop is split.
96 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM) {
97 bool Changed = false;
98 L = IncomingLoop;
99 SplitCondition = NULL;
100 SE = &getAnalysis();
101
102 findSplitCondition();
103
104 if (!SplitCondition)
105 return false;
106
107 if (SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
108 // If it is possible to eliminate loop then do so.
109 Changed = processOneIterationLoop(LPM);
110 else
111 Changed = splitLoop();
112
113 if (Changed)
114 ++NumIndexSplit;
115
116 return Changed;
117 }
118
119 /// Find condition inside a loop that is suitable candidate for index split.
120 void LoopIndexSplit::findSplitCondition() {
121
122 BasicBlock *Header = L->getHeader();
123
124 for (BasicBlock::iterator I = Header->begin(); isa(I); ++I) {
125 PHINode *PN = cast(I);
126
127 if (!PN->getType()->isInteger())
128 continue;
129
130 SCEVHandle SCEV = SE->getSCEV(PN);
131 if (!isa(SCEV))
132 continue;
133
134 // If this phi node is used in a compare instruction then it is a
135 // split condition candidate.
136 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
137 UI != E; ++UI) {
138 if (ICmpInst *CI = dyn_cast(*UI)) {
139 SplitCondition = CI;
140 break;
141 }
142 }
143
144 // Valid SplitCondition's one operand is phi node and the other operand
145 // is loop invariant.
146 if (SplitCondition) {
147 if (SplitCondition->getOperand(0) != PN)
148 SplitValue = SplitCondition->getOperand(0);
149 else
150 SplitValue = SplitCondition->getOperand(1);
151 SCEVHandle ValueSCEV = SE->getSCEV(SplitValue);
152
153 // If SplitValue is not invariant then SplitCondition is not appropriate.
154 if (!ValueSCEV->isLoopInvariant(L))
155 SplitCondition = NULL;
156 }
157
158 // We are looking for only one split condition.
159 if (SplitCondition) {
160 IndVar = PN;
161 break;
162 }
163 }
164 }
165
166 /// processOneIterationLoop - Current loop L contains compare instruction
167 /// that compares induction variable, IndVar, against loop invariant. If
168 /// entire (i.e. meaningful) loop body is dominated by this compare
169 /// instruction then loop body is executed only once. In such case eliminate
170 /// loop structure surrounding this loop body. For example,
171 /// for (int i = start; i < end; ++i) {
172 /// if ( i == somevalue) {
173 /// loop_body
174 /// }
175 /// }
176 /// can be transformed into
177 /// if (somevalue >= start && somevalue < end) {
178 /// i = somevalue;
179 /// loop_body
180 /// }
181 bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
182
183 BasicBlock *Header = L->getHeader();
184
185 // First of all, check if SplitCondition dominates entire loop body
186 // or not.
187
188 // If SplitCondition is not in loop header then this loop is not suitable
189 // for this transformation.
190 if (SplitCondition->getParent() != Header)
191 return false;
192
193 // If one of the Header block's successor is not an exit block then this
194 // loop is not a suitable candidate.
195 BasicBlock *ExitBlock = NULL;
196 for (succ_iterator SI = succ_begin(Header), E = succ_end(Header); SI != E; ++SI) {
197 if (L->isLoopExit(*SI)) {
198 ExitBlock = *SI;
199 break;
200 }
201 }
202
203 if (!ExitBlock)
204 return false;
205
206 // If loop header includes loop variant instruction operands then
207 // this loop may not be eliminated.
208 if (!safeHeader(Header))
209 return false;
210
211 // If Exit block includes loop variant instructions then this
212 // loop may not be eliminated.
213 if (!safeExitBlock(ExitBlock))
214 return false;
215
216 BasicBlock *Latch = L->getLoopLatch();
217 BasicBlock *Preheader = L->getLoopPreheader();
218 Instruction *Terminator = Header->getTerminator();
219 Value *StartValue = IndVar->getIncomingValueForBlock(Preheader);
220
221 // Update CFG.
222
223 // Replace split condition in header.
224 // Transform
225 // SplitCondition : icmp eq i32 IndVar, SplitValue
226 // into
227 // c1 = icmp uge i32 SplitValue, StartValue
228 // c2 = icmp ult i32 vSplitValue, ExitValue
229 // and i32 c1, c2
230 bool SignedPredicate = SplitCondition->isSignedPredicate();
231 Instruction *C1 = new ICmpInst(SignedPredicate ?
232 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
233 SplitValue, StartValue, "lisplit", Terminator);
234 Instruction *C2 = new ICmpInst(SignedPredicate ?
235 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
236 SplitValue, ExitValue, "lisplit", Terminator);
237 Instruction *NSplitCond = BinaryOperator::create(Instruction::And,
238 C1, C2, "lisplit", Terminator);
239 SplitCondition->replaceAllUsesWith(NSplitCond);
240 SplitCondition->removeFromParent();
241 delete SplitCondition;
242
243 // As a first step to break this loop, remove Latch to Header edge.
244 BasicBlock *LatchSucc = NULL;
245 Header->removePredecessor(Latch);
246 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
247 SI != E; ++SI) {
248 if (Header != *SI)
249 LatchSucc = *SI;
250 }
251 BranchInst *BR = dyn_cast(Latch->getTerminator());
252 BR->setUnconditionalDest(LatchSucc);
253
254 // Now, clear latch block. Remove instructions that are responsible
255 // to increment induction variable.
256 Instruction *LTerminator = Latch->getTerminator();
257 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
258 LB != LE; ) {
259 Instruction *I = LB;
260 ++LB;
261 if (isa(I) || I == LTerminator)
262 continue;
263
264 I->replaceAllUsesWith(UndefValue::get(I->getType()));
265 I->removeFromParent();
266 delete I;
267 }
268
269 LPM.deleteLoopFromQueue(L);
270 return true;
271 }
272
273 // If loop header includes loop variant instruction operands then
274 // this loop can not be eliminated. This is used by processOneIterationLoop().
275 bool LoopIndexSplit::safeHeader(BasicBlock *Header) {
276
277 Instruction *Terminator = Header->getTerminator();
278 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
279 BI != BE; ++BI) {
280 Instruction *I = BI;
281
282 // PHI Nodes are OK.
283 if (isa(I))
284 continue;
285
286 // SplitCondition itself is OK.
287 if (ICmpInst *CI = dyn_cast(I)) {
288 if (CI == SplitCondition)
289 continue;
290 }
291
292 // Terminator is also harmless.
293 if (I == Terminator)
294 continue;
295
296 // Otherwise we have a instruction that may not be safe.
297 return false;
298 }
299
300 return true;
301 }
302
303 // If Exit block includes loop variant instructions then this
304 // loop may not be eliminated. This is used by processOneIterationLoop().
305 bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) {
306
307 Instruction *ExitCondition = NULL;
308 Instruction *IndVarIncrement = NULL;
309
310 for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
311 BI != BE; ++BI) {
312 Instruction *I = BI;
313
314 // PHI Nodes are OK.
315 if (isa(I))
316 continue;
317
318 // Check if I is induction variable increment instruction.
319 if (BinaryOperator *BOp = dyn_cast(I)) {
320 if (BOp->getOpcode() != Instruction::Add)
321 return false;
322
323 Value *Op0 = BOp->getOperand(0);
324 Value *Op1 = BOp->getOperand(1);
325 PHINode *PN = NULL;
326 ConstantInt *CI = NULL;
327
328 if ((PN = dyn_cast(Op0))) {
329 if ((CI = dyn_cast(Op1)))
330 IndVarIncrement = I;
331 } else
332 if ((PN = dyn_cast(Op1))) {
333 if ((CI = dyn_cast(Op0)))
334 IndVarIncrement = I;
335 }
336
337 if (IndVarIncrement && PN == IndVar && CI->isOne())
338 continue;
339 }
340 // I is an Exit condition if next instruction is block terminator.
341 // Exit condition is OK if it compares loop invariant exit value,
342 // which is checked below.
343 else if (isa(I)) {
344 ++BI;
345 Instruction *N = BI;
346 if (N == ExitBlock->getTerminator()) {
347 ExitCondition = I;
348 break;
349 }
350 }
351
352 // Otherwise we have instruction that may not be safe.
353 return false;
354 }
355
356 // Check if Exit condition is comparing induction variable against
357 // loop invariant value. If one operand is induction variable and
358 // the other operand is loop invaraint then Exit condition is safe.
359 if (ExitCondition) {
360 Value *Op0 = ExitCondition->getOperand(0);
361 Value *Op1 = ExitCondition->getOperand(1);
362
363 Instruction *Insn0 = dyn_cast(Op0);
364 Instruction *Insn1 = dyn_cast(Op1);
365
366 if (Insn0 && Insn0 == IndVarIncrement)
367 ExitValue = Op1;
368 else if (Insn1 && Insn1 == IndVarIncrement)
369 ExitValue = Op0;
370
371 SCEVHandle ValueSCEV = SE->getSCEV(ExitValue);
372 if (!ValueSCEV->isLoopInvariant(L))
373 return false;
374 }
375
376 // We could not find any reason to consider ExitBlock unsafe.
377 return true;
378 }
379
380 bool LoopIndexSplit::splitLoop() {
381 // FIXME :)
382 return false;
383 }