blob: 1eeb9be6a132049b698944618b9fc025ebbc2a71 [file] [log] [blame]
Devang Patelfee76bd2007-08-07 00:25:56 +00001//===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Devang Patel and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Loop Index Splitting Pass.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "loop-index-split"
15
Devang Patelfee76bd2007-08-07 00:25:56 +000016#include "llvm/Transforms/Scalar.h"
17#include "llvm/Analysis/LoopPass.h"
18#include "llvm/Analysis/ScalarEvolutionExpander.h"
Devang Patel787a7132007-08-08 21:39:47 +000019#include "llvm/Analysis/Dominators.h"
Devang Patel423c8b22007-08-10 18:07:13 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21#include "llvm/Transforms/Utils/Cloning.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000022#include "llvm/Support/Compiler.h"
Devang Patel5b8ec612007-08-15 03:31:47 +000023#include "llvm/ADT/DepthFirstIterator.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000024#include "llvm/ADT/Statistic.h"
25
26using namespace llvm;
27
28STATISTIC(NumIndexSplit, "Number of loops index split");
29
30namespace {
31
32 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
33
34 public:
35 static char ID; // Pass ID, replacement for typeid
36 LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
37
38 // Index split Loop L. Return true if loop is split.
39 bool runOnLoop(Loop *L, LPPassManager &LPM);
40
41 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<ScalarEvolution>();
43 AU.addPreserved<ScalarEvolution>();
44 AU.addRequiredID(LCSSAID);
45 AU.addPreservedID(LCSSAID);
Devang Patel423c8b22007-08-10 18:07:13 +000046 AU.addRequired<LoopInfo>();
Devang Patelfee76bd2007-08-07 00:25:56 +000047 AU.addPreserved<LoopInfo>();
48 AU.addRequiredID(LoopSimplifyID);
49 AU.addPreservedID(LoopSimplifyID);
Devang Patel9704fcf2007-08-08 22:25:28 +000050 AU.addRequired<DominatorTree>();
Devang Patel5b8ec612007-08-15 03:31:47 +000051 AU.addRequired<DominanceFrontier>();
Devang Patel787a7132007-08-08 21:39:47 +000052 AU.addPreserved<DominatorTree>();
53 AU.addPreserved<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +000054 }
55
56 private:
Devang Patel71554b82007-08-08 21:02:17 +000057
58 class SplitInfo {
59 public:
Devang Patel4259fe32007-08-24 06:17:19 +000060 SplitInfo() : SplitValue(NULL), SplitCondition(NULL),
61 UseTrueBranchFirst(true) {}
Devang Patelc9d123d2007-08-09 01:39:01 +000062
Devang Patel71554b82007-08-08 21:02:17 +000063 // Induction variable's range is split at this value.
64 Value *SplitValue;
65
Devang Patel71554b82007-08-08 21:02:17 +000066 // This compare instruction compares IndVar against SplitValue.
67 ICmpInst *SplitCondition;
68
Devang Patel4259fe32007-08-24 06:17:19 +000069 // True if after loop index split, first loop will execute split condition's
70 // true branch.
71 bool UseTrueBranchFirst;
Devang Patel9021c702007-08-08 21:18:27 +000072 // Clear split info.
73 void clear() {
Devang Patel9021c702007-08-08 21:18:27 +000074 SplitValue = NULL;
Devang Patel9021c702007-08-08 21:18:27 +000075 SplitCondition = NULL;
Devang Patel4259fe32007-08-24 06:17:19 +000076 UseTrueBranchFirst = true;
Devang Patel9021c702007-08-08 21:18:27 +000077 }
Devang Patelc9d123d2007-08-09 01:39:01 +000078
Devang Patel71554b82007-08-08 21:02:17 +000079 };
Devang Patelbacf5192007-08-10 00:33:50 +000080
Devang Patel71554b82007-08-08 21:02:17 +000081 private:
Devang Patelfee76bd2007-08-07 00:25:56 +000082 /// Find condition inside a loop that is suitable candidate for index split.
83 void findSplitCondition();
84
Devang Patelbacf5192007-08-10 00:33:50 +000085 /// Find loop's exit condition.
86 void findLoopConditionals();
87
88 /// Return induction variable associated with value V.
89 void findIndVar(Value *V, Loop *L);
90
Devang Patelfee76bd2007-08-07 00:25:56 +000091 /// processOneIterationLoop - Current loop L contains compare instruction
92 /// that compares induction variable, IndVar, agains loop invariant. If
93 /// entire (i.e. meaningful) loop body is dominated by this compare
94 /// instruction then loop body is executed only for one iteration. In
95 /// such case eliminate loop structure surrounding this loop body. For
Devang Patel423c8b22007-08-10 18:07:13 +000096 bool processOneIterationLoop(SplitInfo &SD);
Devang Patelfee76bd2007-08-07 00:25:56 +000097
Devang Patel9704fcf2007-08-08 22:25:28 +000098 /// If loop header includes loop variant instruction operands then
99 /// this loop may not be eliminated.
Devang Patel71554b82007-08-08 21:02:17 +0000100 bool safeHeader(SplitInfo &SD, BasicBlock *BB);
Devang Patelfee76bd2007-08-07 00:25:56 +0000101
Devang Patel1cc2ec82007-08-20 23:51:18 +0000102 /// If Exiting block includes loop variant instructions then this
Devang Patel9704fcf2007-08-08 22:25:28 +0000103 /// loop may not be eliminated.
Devang Patel1cc2ec82007-08-20 23:51:18 +0000104 bool safeExitingBlock(SplitInfo &SD, BasicBlock *BB);
Devang Patelfee76bd2007-08-07 00:25:56 +0000105
Devang Patela6a86632007-08-14 18:35:57 +0000106 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
107 /// This routine is used to remove split condition's dead branch, dominated by
108 /// DeadBB. LiveBB dominates split conidition's other branch.
109 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
Devang Patel98147a32007-08-12 07:02:51 +0000110
Devang Pateldc523952007-08-22 18:27:01 +0000111 /// safeSplitCondition - Return true if it is possible to
112 /// split loop using given split condition.
113 bool safeSplitCondition(SplitInfo &SD);
114
115 /// splitLoop - Split current loop L in two loops using split information
116 /// SD. Update dominator information. Maintain LCSSA form.
Devang Patel71554b82007-08-08 21:02:17 +0000117 bool splitLoop(SplitInfo &SD);
Devang Patelfee76bd2007-08-07 00:25:56 +0000118
Devang Patelbacf5192007-08-10 00:33:50 +0000119 void initialize() {
120 IndVar = NULL;
121 IndVarIncrement = NULL;
122 ExitCondition = NULL;
Devang Patel98147a32007-08-12 07:02:51 +0000123 StartValue = NULL;
124 ExitValueNum = 0;
125 SplitData.clear();
Devang Patelbacf5192007-08-10 00:33:50 +0000126 }
127
Devang Patelfee76bd2007-08-07 00:25:56 +0000128 private:
129
130 // Current Loop.
131 Loop *L;
Devang Patel423c8b22007-08-10 18:07:13 +0000132 LPPassManager *LPM;
133 LoopInfo *LI;
Devang Patelfee76bd2007-08-07 00:25:56 +0000134 ScalarEvolution *SE;
Devang Patel9704fcf2007-08-08 22:25:28 +0000135 DominatorTree *DT;
Devang Patelfc4c5f82007-08-13 22:13:24 +0000136 DominanceFrontier *DF;
Devang Patel71554b82007-08-08 21:02:17 +0000137 SmallVector<SplitInfo, 4> SplitData;
Devang Patelbacf5192007-08-10 00:33:50 +0000138
139 // Induction variable whose range is being split by this transformation.
140 PHINode *IndVar;
141 Instruction *IndVarIncrement;
142
143 // Loop exit condition.
144 ICmpInst *ExitCondition;
145
146 // Induction variable's initial value.
147 Value *StartValue;
148
Devang Patel98147a32007-08-12 07:02:51 +0000149 // Induction variable's final loop exit value operand number in exit condition..
150 unsigned ExitValueNum;
Devang Patelfee76bd2007-08-07 00:25:56 +0000151 };
152
153 char LoopIndexSplit::ID = 0;
154 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
155}
156
157LoopPass *llvm::createLoopIndexSplitPass() {
158 return new LoopIndexSplit();
159}
160
161// Index split Loop L. Return true if loop is split.
Devang Patel423c8b22007-08-10 18:07:13 +0000162bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000163 bool Changed = false;
164 L = IncomingLoop;
Devang Patel423c8b22007-08-10 18:07:13 +0000165 LPM = &LPM_Ref;
Devang Patel71554b82007-08-08 21:02:17 +0000166
Devang Patel3fe4f212007-08-15 02:14:55 +0000167 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel4e8061c2007-08-14 23:53:57 +0000168 if (!L->getSubLoops().empty())
169 return false;
170
Devang Patelfee76bd2007-08-07 00:25:56 +0000171 SE = &getAnalysis<ScalarEvolution>();
Devang Patel9704fcf2007-08-08 22:25:28 +0000172 DT = &getAnalysis<DominatorTree>();
Devang Patel423c8b22007-08-10 18:07:13 +0000173 LI = &getAnalysis<LoopInfo>();
Devang Patel7375bb92007-08-15 03:34:53 +0000174 DF = &getAnalysis<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +0000175
Devang Patelbacf5192007-08-10 00:33:50 +0000176 initialize();
177
178 findLoopConditionals();
179
180 if (!ExitCondition)
181 return false;
182
Devang Patelfee76bd2007-08-07 00:25:56 +0000183 findSplitCondition();
184
Devang Patel71554b82007-08-08 21:02:17 +0000185 if (SplitData.empty())
Devang Patelfee76bd2007-08-07 00:25:56 +0000186 return false;
187
Devang Patel71554b82007-08-08 21:02:17 +0000188 // First see if it is possible to eliminate loop itself or not.
189 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Devang Pateld651f652007-08-20 20:24:15 +0000190 E = SplitData.end(); SI != E;) {
Devang Patel71554b82007-08-08 21:02:17 +0000191 SplitInfo &SD = *SI;
192 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
Devang Patel423c8b22007-08-10 18:07:13 +0000193 Changed = processOneIterationLoop(SD);
Devang Patel71554b82007-08-08 21:02:17 +0000194 if (Changed) {
195 ++NumIndexSplit;
196 // If is loop is eliminated then nothing else to do here.
197 return Changed;
Devang Pateld651f652007-08-20 20:24:15 +0000198 } else {
199 SmallVector<SplitInfo, 4>::iterator Delete_SI = SI;
200 ++SI;
201 SplitData.erase(Delete_SI);
Devang Patel71554b82007-08-08 21:02:17 +0000202 }
Devang Pateld651f652007-08-20 20:24:15 +0000203 } else
204 ++SI;
Devang Patel71554b82007-08-08 21:02:17 +0000205 }
206
Devang Patel4259fe32007-08-24 06:17:19 +0000207 if (SplitData.empty())
208 return false;
209
Devang Patel9704fcf2007-08-08 22:25:28 +0000210 // Split most profitiable condition.
Devang Patel7237d112007-08-24 05:21:13 +0000211 // FIXME : Implement cost analysis.
212 unsigned MostProfitableSDIndex = 0;
213 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
Devang Patel9704fcf2007-08-08 22:25:28 +0000214
Devang Patelfee76bd2007-08-07 00:25:56 +0000215 if (Changed)
216 ++NumIndexSplit;
Devang Patel71554b82007-08-08 21:02:17 +0000217
Devang Patelfee76bd2007-08-07 00:25:56 +0000218 return Changed;
219}
220
Devang Patelc9d123d2007-08-09 01:39:01 +0000221/// Return true if V is a induction variable or induction variable's
222/// increment for loop L.
Devang Patelbacf5192007-08-10 00:33:50 +0000223void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
Devang Patelc9d123d2007-08-09 01:39:01 +0000224
225 Instruction *I = dyn_cast<Instruction>(V);
226 if (!I)
Devang Patelbacf5192007-08-10 00:33:50 +0000227 return;
Devang Patelc9d123d2007-08-09 01:39:01 +0000228
229 // Check if I is a phi node from loop header or not.
230 if (PHINode *PN = dyn_cast<PHINode>(V)) {
231 if (PN->getParent() == L->getHeader()) {
Devang Patelbacf5192007-08-10 00:33:50 +0000232 IndVar = PN;
233 return;
Devang Patelc9d123d2007-08-09 01:39:01 +0000234 }
235 }
236
237 // Check if I is a add instruction whose one operand is
238 // phi node from loop header and second operand is constant.
239 if (I->getOpcode() != Instruction::Add)
Devang Patelbacf5192007-08-10 00:33:50 +0000240 return;
Devang Patelc9d123d2007-08-09 01:39:01 +0000241
242 Value *Op0 = I->getOperand(0);
243 Value *Op1 = I->getOperand(1);
244
245 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
246 if (PN->getParent() == L->getHeader()
247 && isa<ConstantInt>(Op1)) {
248 IndVar = PN;
249 IndVarIncrement = I;
Devang Patelbacf5192007-08-10 00:33:50 +0000250 return;
Devang Patelc9d123d2007-08-09 01:39:01 +0000251 }
252 }
253
254 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
255 if (PN->getParent() == L->getHeader()
256 && isa<ConstantInt>(Op0)) {
257 IndVar = PN;
258 IndVarIncrement = I;
Devang Patelbacf5192007-08-10 00:33:50 +0000259 return;
Devang Patelc9d123d2007-08-09 01:39:01 +0000260 }
261 }
262
Devang Patelbacf5192007-08-10 00:33:50 +0000263 return;
264}
265
266// Find loop's exit condition and associated induction variable.
267void LoopIndexSplit::findLoopConditionals() {
268
Devang Patel1cc2ec82007-08-20 23:51:18 +0000269 BasicBlock *ExitingBlock = NULL;
Devang Patelbacf5192007-08-10 00:33:50 +0000270
271 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
272 I != E; ++I) {
273 BasicBlock *BB = *I;
274 if (!L->isLoopExit(BB))
275 continue;
Devang Patel1cc2ec82007-08-20 23:51:18 +0000276 if (ExitingBlock)
Devang Patelbacf5192007-08-10 00:33:50 +0000277 return;
Devang Patel1cc2ec82007-08-20 23:51:18 +0000278 ExitingBlock = BB;
Devang Patelbacf5192007-08-10 00:33:50 +0000279 }
280
Devang Patel1cc2ec82007-08-20 23:51:18 +0000281 if (!ExitingBlock)
Devang Patelbacf5192007-08-10 00:33:50 +0000282 return;
Devang Patelb88e4202007-08-24 05:36:56 +0000283
284 // If exiting block is neither loop header nor loop latch then this loop is
285 // not suitable.
286 if (ExitingBlock != L->getHeader() && ExitingBlock != L->getLoopLatch())
287 return;
288
Devang Patelbacf5192007-08-10 00:33:50 +0000289 // If exit block's terminator is conditional branch inst then we have found
290 // exit condition.
Devang Patel1cc2ec82007-08-20 23:51:18 +0000291 BranchInst *BR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patelbacf5192007-08-10 00:33:50 +0000292 if (!BR || BR->isUnconditional())
293 return;
294
295 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
296 if (!CI)
297 return;
298
299 ExitCondition = CI;
300
301 // Exit condition's one operand is loop invariant exit value and second
302 // operand is SCEVAddRecExpr based on induction variable.
303 Value *V0 = CI->getOperand(0);
304 Value *V1 = CI->getOperand(1);
305
306 SCEVHandle SH0 = SE->getSCEV(V0);
307 SCEVHandle SH1 = SE->getSCEV(V1);
308
309 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
Devang Patel98147a32007-08-12 07:02:51 +0000310 ExitValueNum = 0;
Devang Patelbacf5192007-08-10 00:33:50 +0000311 findIndVar(V1, L);
312 }
313 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
Devang Patel98147a32007-08-12 07:02:51 +0000314 ExitValueNum = 1;
Devang Patelbacf5192007-08-10 00:33:50 +0000315 findIndVar(V0, L);
316 }
317
Devang Patel98147a32007-08-12 07:02:51 +0000318 if (!IndVar)
Devang Patelbacf5192007-08-10 00:33:50 +0000319 ExitCondition = NULL;
320 else if (IndVar) {
321 BasicBlock *Preheader = L->getLoopPreheader();
322 StartValue = IndVar->getIncomingValueForBlock(Preheader);
323 }
Devang Patelc9d123d2007-08-09 01:39:01 +0000324}
325
Devang Patelfee76bd2007-08-07 00:25:56 +0000326/// Find condition inside a loop that is suitable candidate for index split.
327void LoopIndexSplit::findSplitCondition() {
328
Devang Patel71554b82007-08-08 21:02:17 +0000329 SplitInfo SD;
Devang Patelc9d123d2007-08-09 01:39:01 +0000330 // Check all basic block's terminators.
Devang Patelfee76bd2007-08-07 00:25:56 +0000331
Devang Patelc9d123d2007-08-09 01:39:01 +0000332 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
333 I != E; ++I) {
334 BasicBlock *BB = *I;
Devang Patelfee76bd2007-08-07 00:25:56 +0000335
Devang Patelc9d123d2007-08-09 01:39:01 +0000336 // If this basic block does not terminate in a conditional branch
337 // then terminator is not a suitable split condition.
338 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
339 if (!BR)
340 continue;
341
342 if (BR->isUnconditional())
Devang Patelfee76bd2007-08-07 00:25:56 +0000343 continue;
344
Devang Patelc9d123d2007-08-09 01:39:01 +0000345 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
Devang Patelbacf5192007-08-10 00:33:50 +0000346 if (!CI || CI == ExitCondition)
Devang Patelc9d123d2007-08-09 01:39:01 +0000347 return;
Devang Patelfee76bd2007-08-07 00:25:56 +0000348
Devang Patelc830aee2007-08-24 06:02:25 +0000349 if (CI->getPredicate() == ICmpInst::ICMP_NE)
350 return;
351
Devang Patel4259fe32007-08-24 06:17:19 +0000352 // If split condition predicate is GT or GE then first execute
353 // false branch of split condition.
354 if (CI->getPredicate() != ICmpInst::ICMP_ULT
355 && CI->getPredicate() != ICmpInst::ICMP_SLT
356 && CI->getPredicate() != ICmpInst::ICMP_ULE
357 && CI->getPredicate() != ICmpInst::ICMP_SLE)
358 SD.UseTrueBranchFirst = false;
359
Devang Patelc9d123d2007-08-09 01:39:01 +0000360 // If one operand is loop invariant and second operand is SCEVAddRecExpr
361 // based on induction variable then CI is a candidate split condition.
362 Value *V0 = CI->getOperand(0);
363 Value *V1 = CI->getOperand(1);
364
365 SCEVHandle SH0 = SE->getSCEV(V0);
366 SCEVHandle SH1 = SE->getSCEV(V1);
367
368 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
369 SD.SplitValue = V0;
370 SD.SplitCondition = CI;
Devang Patelbacf5192007-08-10 00:33:50 +0000371 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
372 if (PN == IndVar)
373 SplitData.push_back(SD);
374 }
375 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
376 if (IndVarIncrement && IndVarIncrement == Insn)
377 SplitData.push_back(SD);
378 }
Devang Patelfee76bd2007-08-07 00:25:56 +0000379 }
Devang Patelc9d123d2007-08-09 01:39:01 +0000380 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
381 SD.SplitValue = V1;
382 SD.SplitCondition = CI;
Devang Patelbacf5192007-08-10 00:33:50 +0000383 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
384 if (PN == IndVar)
385 SplitData.push_back(SD);
386 }
387 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
388 if (IndVarIncrement && IndVarIncrement == Insn)
389 SplitData.push_back(SD);
390 }
Devang Patelfee76bd2007-08-07 00:25:56 +0000391 }
392 }
393}
394
395/// processOneIterationLoop - Current loop L contains compare instruction
396/// that compares induction variable, IndVar, against loop invariant. If
397/// entire (i.e. meaningful) loop body is dominated by this compare
398/// instruction then loop body is executed only once. In such case eliminate
399/// loop structure surrounding this loop body. For example,
400/// for (int i = start; i < end; ++i) {
401/// if ( i == somevalue) {
402/// loop_body
403/// }
404/// }
405/// can be transformed into
406/// if (somevalue >= start && somevalue < end) {
407/// i = somevalue;
408/// loop_body
409/// }
Devang Patel423c8b22007-08-10 18:07:13 +0000410bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000411
412 BasicBlock *Header = L->getHeader();
413
414 // First of all, check if SplitCondition dominates entire loop body
415 // or not.
416
417 // If SplitCondition is not in loop header then this loop is not suitable
418 // for this transformation.
Devang Patel71554b82007-08-08 21:02:17 +0000419 if (SD.SplitCondition->getParent() != Header)
Devang Patelfee76bd2007-08-07 00:25:56 +0000420 return false;
421
Devang Patelfee76bd2007-08-07 00:25:56 +0000422 // If loop header includes loop variant instruction operands then
423 // this loop may not be eliminated.
Devang Patel71554b82007-08-08 21:02:17 +0000424 if (!safeHeader(SD, Header))
Devang Patelfee76bd2007-08-07 00:25:56 +0000425 return false;
426
Devang Patel1cc2ec82007-08-20 23:51:18 +0000427 // If Exiting block includes loop variant instructions then this
Devang Patelfee76bd2007-08-07 00:25:56 +0000428 // loop may not be eliminated.
Devang Patel1cc2ec82007-08-20 23:51:18 +0000429 if (!safeExitingBlock(SD, ExitCondition->getParent()))
Devang Patelfee76bd2007-08-07 00:25:56 +0000430 return false;
431
Devang Patel6a2bfda2007-08-08 01:51:27 +0000432 // Update CFG.
433
Devang Patelebc5fea2007-08-20 20:49:01 +0000434 // Replace index variable with split value in loop body. Loop body is executed
435 // only when index variable is equal to split value.
436 IndVar->replaceAllUsesWith(SD.SplitValue);
437
438 // Remove Latch to Header edge.
Devang Patelfee76bd2007-08-07 00:25:56 +0000439 BasicBlock *Latch = L->getLoopLatch();
Devang Patel6a2bfda2007-08-08 01:51:27 +0000440 BasicBlock *LatchSucc = NULL;
441 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
442 if (!BR)
443 return false;
444 Header->removePredecessor(Latch);
445 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
446 SI != E; ++SI) {
447 if (Header != *SI)
448 LatchSucc = *SI;
449 }
450 BR->setUnconditionalDest(LatchSucc);
451
Devang Patelfee76bd2007-08-07 00:25:56 +0000452 Instruction *Terminator = Header->getTerminator();
Devang Patelada054a2007-08-14 01:30:57 +0000453 Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
Devang Patelfee76bd2007-08-07 00:25:56 +0000454
Devang Patelfee76bd2007-08-07 00:25:56 +0000455 // Replace split condition in header.
456 // Transform
457 // SplitCondition : icmp eq i32 IndVar, SplitValue
458 // into
459 // c1 = icmp uge i32 SplitValue, StartValue
460 // c2 = icmp ult i32 vSplitValue, ExitValue
461 // and i32 c1, c2
Devang Patelbacf5192007-08-10 00:33:50 +0000462 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patelfee76bd2007-08-07 00:25:56 +0000463 Instruction *C1 = new ICmpInst(SignedPredicate ?
464 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Devang Patel71554b82007-08-08 21:02:17 +0000465 SD.SplitValue, StartValue, "lisplit",
466 Terminator);
Devang Patelfee76bd2007-08-07 00:25:56 +0000467 Instruction *C2 = new ICmpInst(SignedPredicate ?
468 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patelada054a2007-08-14 01:30:57 +0000469 SD.SplitValue, ExitValue, "lisplit",
Devang Patel71554b82007-08-08 21:02:17 +0000470 Terminator);
471 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
472 Terminator);
473 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
474 SD.SplitCondition->eraseFromParent();
Devang Patelfee76bd2007-08-07 00:25:56 +0000475
Devang Patelfee76bd2007-08-07 00:25:56 +0000476 // Now, clear latch block. Remove instructions that are responsible
477 // to increment induction variable.
478 Instruction *LTerminator = Latch->getTerminator();
479 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
480 LB != LE; ) {
481 Instruction *I = LB;
482 ++LB;
483 if (isa<PHINode>(I) || I == LTerminator)
484 continue;
485
Devang Patelada054a2007-08-14 01:30:57 +0000486 if (I == IndVarIncrement)
487 I->replaceAllUsesWith(ExitValue);
488 else
489 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Devang Patel8431a1c2007-08-07 17:45:35 +0000490 I->eraseFromParent();
Devang Patelfee76bd2007-08-07 00:25:56 +0000491 }
492
Devang Patel423c8b22007-08-10 18:07:13 +0000493 LPM->deleteLoopFromQueue(L);
Devang Patel787a7132007-08-08 21:39:47 +0000494
495 // Update Dominator Info.
496 // Only CFG change done is to remove Latch to Header edge. This
497 // does not change dominator tree because Latch did not dominate
498 // Header.
Devang Patelfc4c5f82007-08-13 22:13:24 +0000499 if (DF) {
Devang Patel787a7132007-08-08 21:39:47 +0000500 DominanceFrontier::iterator HeaderDF = DF->find(Header);
501 if (HeaderDF != DF->end())
502 DF->removeFromFrontier(HeaderDF, Header);
503
504 DominanceFrontier::iterator LatchDF = DF->find(Latch);
505 if (LatchDF != DF->end())
506 DF->removeFromFrontier(LatchDF, Header);
507 }
Devang Patelfee76bd2007-08-07 00:25:56 +0000508 return true;
509}
510
511// If loop header includes loop variant instruction operands then
512// this loop can not be eliminated. This is used by processOneIterationLoop().
Devang Patel71554b82007-08-08 21:02:17 +0000513bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000514
515 Instruction *Terminator = Header->getTerminator();
516 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
517 BI != BE; ++BI) {
518 Instruction *I = BI;
519
Devang Patelada054a2007-08-14 01:30:57 +0000520 // PHI Nodes are OK.
Devang Patelfee76bd2007-08-07 00:25:56 +0000521 if (isa<PHINode>(I))
522 continue;
523
524 // SplitCondition itself is OK.
Devang Patel71554b82007-08-08 21:02:17 +0000525 if (I == SD.SplitCondition)
Devang Patel6a2bfda2007-08-08 01:51:27 +0000526 continue;
Devang Patelfee76bd2007-08-07 00:25:56 +0000527
Devang Patelc9d123d2007-08-09 01:39:01 +0000528 // Induction variable is OK.
Devang Patelbacf5192007-08-10 00:33:50 +0000529 if (I == IndVar)
Devang Patelc9d123d2007-08-09 01:39:01 +0000530 continue;
531
532 // Induction variable increment is OK.
Devang Patelbacf5192007-08-10 00:33:50 +0000533 if (I == IndVarIncrement)
Devang Patelc9d123d2007-08-09 01:39:01 +0000534 continue;
535
Devang Patelfee76bd2007-08-07 00:25:56 +0000536 // Terminator is also harmless.
537 if (I == Terminator)
538 continue;
539
540 // Otherwise we have a instruction that may not be safe.
541 return false;
542 }
543
544 return true;
545}
546
Devang Patel1cc2ec82007-08-20 23:51:18 +0000547// If Exiting block includes loop variant instructions then this
Devang Patelfee76bd2007-08-07 00:25:56 +0000548// loop may not be eliminated. This is used by processOneIterationLoop().
Devang Patel1cc2ec82007-08-20 23:51:18 +0000549bool LoopIndexSplit::safeExitingBlock(SplitInfo &SD,
550 BasicBlock *ExitingBlock) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000551
Devang Patel1cc2ec82007-08-20 23:51:18 +0000552 for (BasicBlock::iterator BI = ExitingBlock->begin(),
553 BE = ExitingBlock->end(); BI != BE; ++BI) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000554 Instruction *I = BI;
555
Devang Patelada054a2007-08-14 01:30:57 +0000556 // PHI Nodes are OK.
Devang Patelfee76bd2007-08-07 00:25:56 +0000557 if (isa<PHINode>(I))
558 continue;
559
Devang Patelc9d123d2007-08-09 01:39:01 +0000560 // Induction variable increment is OK.
Devang Patelbacf5192007-08-10 00:33:50 +0000561 if (IndVarIncrement && IndVarIncrement == I)
Devang Patelc9d123d2007-08-09 01:39:01 +0000562 continue;
Devang Patelfee76bd2007-08-07 00:25:56 +0000563
Devang Patelc9d123d2007-08-09 01:39:01 +0000564 // Check if I is induction variable increment instruction.
Devang Patelbacf5192007-08-10 00:33:50 +0000565 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
Devang Patelc9d123d2007-08-09 01:39:01 +0000566
567 Value *Op0 = I->getOperand(0);
568 Value *Op1 = I->getOperand(1);
Devang Patelfee76bd2007-08-07 00:25:56 +0000569 PHINode *PN = NULL;
570 ConstantInt *CI = NULL;
571
572 if ((PN = dyn_cast<PHINode>(Op0))) {
573 if ((CI = dyn_cast<ConstantInt>(Op1)))
Devang Patelbacf5192007-08-10 00:33:50 +0000574 IndVarIncrement = I;
Devang Patelfee76bd2007-08-07 00:25:56 +0000575 } else
576 if ((PN = dyn_cast<PHINode>(Op1))) {
577 if ((CI = dyn_cast<ConstantInt>(Op0)))
Devang Patelbacf5192007-08-10 00:33:50 +0000578 IndVarIncrement = I;
Devang Patelfee76bd2007-08-07 00:25:56 +0000579 }
580
Devang Patelbacf5192007-08-10 00:33:50 +0000581 if (IndVarIncrement && PN == IndVar && CI->isOne())
Devang Patelfee76bd2007-08-07 00:25:56 +0000582 continue;
583 }
Devang Patel6a2bfda2007-08-08 01:51:27 +0000584
Devang Patelfee76bd2007-08-07 00:25:56 +0000585 // I is an Exit condition if next instruction is block terminator.
586 // Exit condition is OK if it compares loop invariant exit value,
587 // which is checked below.
Devang Patel002fe252007-08-07 23:17:52 +0000588 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
Devang Patelbacf5192007-08-10 00:33:50 +0000589 if (EC == ExitCondition)
Devang Patel6a2bfda2007-08-08 01:51:27 +0000590 continue;
Devang Patelfee76bd2007-08-07 00:25:56 +0000591 }
592
Devang Patel1cc2ec82007-08-20 23:51:18 +0000593 if (I == ExitingBlock->getTerminator())
Devang Patelbacf5192007-08-10 00:33:50 +0000594 continue;
595
Devang Patelfee76bd2007-08-07 00:25:56 +0000596 // Otherwise we have instruction that may not be safe.
597 return false;
598 }
599
Devang Patel1cc2ec82007-08-20 23:51:18 +0000600 // We could not find any reason to consider ExitingBlock unsafe.
Devang Patelfee76bd2007-08-07 00:25:56 +0000601 return true;
602}
603
Devang Patela6a86632007-08-14 18:35:57 +0000604/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
605/// This routine is used to remove split condition's dead branch, dominated by
606/// DeadBB. LiveBB dominates split conidition's other branch.
607void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
608 BasicBlock *LiveBB) {
Devang Patel98147a32007-08-12 07:02:51 +0000609
Devang Patel5b8ec612007-08-15 03:31:47 +0000610 // First update DeadBB's dominance frontier.
Devang Patel96bf5242007-08-17 21:59:16 +0000611 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patel5b8ec612007-08-15 03:31:47 +0000612 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
613 if (DeadBBDF != DF->end()) {
614 SmallVector<BasicBlock *, 8> PredBlocks;
615
616 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
617 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
618 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) {
619 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel96bf5242007-08-17 21:59:16 +0000620 FrontierBBs.push_back(FrontierBB);
621
Devang Patel5b8ec612007-08-15 03:31:47 +0000622 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
623 PredBlocks.clear();
624 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
625 PI != PE; ++PI) {
626 BasicBlock *P = *PI;
627 if (P == DeadBB || DT->dominates(DeadBB, P))
628 PredBlocks.push_back(P);
Devang Patelfc4c5f82007-08-13 22:13:24 +0000629 }
Devang Patel96bf5242007-08-17 21:59:16 +0000630
Devang Patel5b8ec612007-08-15 03:31:47 +0000631 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
632 FBI != FBE; ++FBI) {
633 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
634 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
635 PE = PredBlocks.end(); PI != PE; ++PI) {
636 BasicBlock *P = *PI;
637 PN->removeIncomingValue(P);
638 }
639 }
640 else
641 break;
Devang Patel96bf5242007-08-17 21:59:16 +0000642 }
Devang Patel98147a32007-08-12 07:02:51 +0000643 }
Devang Patel98147a32007-08-12 07:02:51 +0000644 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000645
646 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
647 SmallVector<BasicBlock *, 32> WorkList;
648 DomTreeNode *DN = DT->getNode(DeadBB);
649 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
650 E = df_end(DN); DI != E; ++DI) {
651 BasicBlock *BB = DI->getBlock();
652 WorkList.push_back(BB);
Devang Patel96bf5242007-08-17 21:59:16 +0000653 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
Devang Patel5b8ec612007-08-15 03:31:47 +0000654 }
655
656 while (!WorkList.empty()) {
657 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
658 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
659 BBI != BBE; ++BBI) {
660 Instruction *I = BBI;
661 I->replaceAllUsesWith(UndefValue::get(I->getType()));
662 I->eraseFromParent();
663 }
664 LPM->deleteSimpleAnalysisValue(BB, LP);
665 DT->eraseNode(BB);
666 DF->removeBlock(BB);
667 LI->removeBlock(BB);
668 BB->eraseFromParent();
669 }
Devang Patel96bf5242007-08-17 21:59:16 +0000670
671 // Update Frontier BBs' dominator info.
672 while (!FrontierBBs.empty()) {
673 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
674 BasicBlock *NewDominator = FBB->getSinglePredecessor();
675 if (!NewDominator) {
676 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
677 NewDominator = *PI;
678 ++PI;
679 if (NewDominator != LiveBB) {
680 for(; PI != PE; ++PI) {
681 BasicBlock *P = *PI;
682 if (P == LiveBB) {
683 NewDominator = LiveBB;
684 break;
685 }
686 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
687 }
688 }
689 }
690 assert (NewDominator && "Unable to fix dominator info.");
691 DT->changeImmediateDominator(FBB, NewDominator);
692 DF->changeImmediateDominator(FBB, NewDominator, DT);
693 }
694
Devang Patel98147a32007-08-12 07:02:51 +0000695}
696
Devang Pateldc523952007-08-22 18:27:01 +0000697/// safeSplitCondition - Return true if it is possible to
698/// split loop using given split condition.
699bool LoopIndexSplit::safeSplitCondition(SplitInfo &SD) {
Devang Patel23a19f82007-08-10 00:53:35 +0000700
Devang Pateldc523952007-08-22 18:27:01 +0000701 BasicBlock *SplitCondBlock = SD.SplitCondition->getParent();
Devang Patel0b8e02b2007-08-21 21:12:02 +0000702
Devang Pateldc523952007-08-22 18:27:01 +0000703 // Unable to handle triange loops at the moment.
Devang Patel3fe4f212007-08-15 02:14:55 +0000704 // In triangle loop, split condition is in header and one of the
705 // the split destination is loop latch. If split condition is EQ
706 // then such loops are already handle in processOneIterationLoop().
Devang Pateldc523952007-08-22 18:27:01 +0000707 BasicBlock *Latch = L->getLoopLatch();
708 BranchInst *SplitTerminator =
709 cast<BranchInst>(SplitCondBlock->getTerminator());
710 BasicBlock *Succ0 = SplitTerminator->getSuccessor(0);
711 BasicBlock *Succ1 = SplitTerminator->getSuccessor(1);
712 if (L->getHeader() == SplitCondBlock
713 && (Latch == Succ0 || Latch == Succ1))
Devang Patel3fe4f212007-08-15 02:14:55 +0000714 return false;
Devang Patel0b8e02b2007-08-21 21:12:02 +0000715
Devang Patel20d260a2007-08-18 00:00:32 +0000716 // If one of the split condition branch is post dominating other then loop
717 // index split is not appropriate.
Devang Pateldc523952007-08-22 18:27:01 +0000718 if (DT->dominates(Succ0, Latch) || DT->dominates(Succ1, Latch))
Devang Patel96bf5242007-08-17 21:59:16 +0000719 return false;
Devang Patel0b8e02b2007-08-21 21:12:02 +0000720
Devang Patel20d260a2007-08-18 00:00:32 +0000721 // If one of the split condition branch is a predecessor of the other
722 // split condition branch head then do not split loop on this condition.
Devang Patel0b8e02b2007-08-21 21:12:02 +0000723 for(pred_iterator PI = pred_begin(Succ0), PE = pred_end(Succ0);
724 PI != PE; ++PI)
Devang Patel20d260a2007-08-18 00:00:32 +0000725 if (Succ1 == *PI)
726 return false;
Devang Patel0b8e02b2007-08-21 21:12:02 +0000727 for(pred_iterator PI = pred_begin(Succ1), PE = pred_end(Succ1);
728 PI != PE; ++PI)
Devang Patel20d260a2007-08-18 00:00:32 +0000729 if (Succ0 == *PI)
730 return false;
731
Devang Patelb88e4202007-08-24 05:36:56 +0000732 // Finally this split condition is safe only if merge point for
733 // split condition branch is loop latch. This check along with previous
734 // check, to ensure that exit condition is in either loop latch or header,
735 // filters all loops with non-empty loop body between merge point
736 // and exit condition.
737 DominanceFrontier::iterator Succ0DF = DF->find(Succ0);
738 assert (Succ0DF != DF->end() && "Unable to find Succ0 dominance frontier");
739 if (Succ0DF->second.count(Latch))
740 return true;
741
742 DominanceFrontier::iterator Succ1DF = DF->find(Succ1);
743 assert (Succ1DF != DF->end() && "Unable to find Succ1 dominance frontier");
744 if (Succ1DF->second.count(Latch))
745 return true;
746
747 return false;
Devang Pateldc523952007-08-22 18:27:01 +0000748}
749
750/// splitLoop - Split current loop L in two loops using split information
751/// SD. Update dominator information. Maintain LCSSA form.
752bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
753
754 if (!safeSplitCondition(SD))
755 return false;
756
Devang Patela8644e32007-08-22 19:33:29 +0000757 // After loop is cloned there are two loops.
758 //
759 // First loop, referred as ALoop, executes first part of loop's iteration
760 // space split. Second loop, referred as BLoop, executes remaining
761 // part of loop's iteration space.
762 //
763 // ALoop's exit edge enters BLoop's header through a forwarding block which
764 // acts as a BLoop's preheader.
Devang Pateldc523952007-08-22 18:27:01 +0000765
Devang Patela8644e32007-08-22 19:33:29 +0000766 //[*] Calculate ALoop induction variable's new exiting value and
767 // BLoop induction variable's new starting value. Calculuate these
768 // values in original loop's preheader.
769 // A_ExitValue = min(SplitValue, OrignalLoopExitValue)
770 // B_StartValue = max(SplitValue, OriginalLoopStartValue)
771 Value *A_ExitValue = NULL;
772 Value *B_StartValue = NULL;
Devang Patel23a19f82007-08-10 00:53:35 +0000773 if (isa<ConstantInt>(SD.SplitValue)) {
Devang Patela8644e32007-08-22 19:33:29 +0000774 A_ExitValue = SD.SplitValue;
775 B_StartValue = SD.SplitValue;
Devang Patel23a19f82007-08-10 00:53:35 +0000776 }
777 else {
Devang Patela8644e32007-08-22 19:33:29 +0000778 BasicBlock *Preheader = L->getLoopPreheader();
779 Instruction *PHTerminator = Preheader->getTerminator();
780 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patel23a19f82007-08-10 00:53:35 +0000781 Value *C1 = new ICmpInst(SignedPredicate ?
782 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel98147a32007-08-12 07:02:51 +0000783 SD.SplitValue,
784 ExitCondition->getOperand(ExitValueNum),
Devang Patela8644e32007-08-22 19:33:29 +0000785 "lsplit.ev", PHTerminator);
786 A_ExitValue = new SelectInst(C1, SD.SplitValue,
Devang Patel98147a32007-08-12 07:02:51 +0000787 ExitCondition->getOperand(ExitValueNum),
Devang Patela8644e32007-08-22 19:33:29 +0000788 "lsplit.ev", PHTerminator);
Devang Patel23a19f82007-08-10 00:53:35 +0000789
790 Value *C2 = new ICmpInst(SignedPredicate ?
791 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
792 SD.SplitValue, StartValue, "lsplit.sv",
Devang Patela8644e32007-08-22 19:33:29 +0000793 PHTerminator);
794 B_StartValue = new SelectInst(C2, StartValue, SD.SplitValue,
795 "lsplit.sv", PHTerminator);
Devang Patel23a19f82007-08-10 00:53:35 +0000796 }
Devang Patel423c8b22007-08-10 18:07:13 +0000797
Devang Patela8644e32007-08-22 19:33:29 +0000798 //[*] Clone loop.
Devang Patel98147a32007-08-12 07:02:51 +0000799 DenseMap<const Value *, Value *> ValueMap;
Devang Patela8644e32007-08-22 19:33:29 +0000800 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
801 BasicBlock *B_Header = BLoop->getHeader();
Devang Patel98147a32007-08-12 07:02:51 +0000802
Devang Patela8644e32007-08-22 19:33:29 +0000803 //[*] ALoop's exiting edge BLoop's header.
804 // ALoop's original exit block becomes BLoop's exit block.
805 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
806 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
807 BranchInst *A_ExitInsn =
808 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
809 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
810 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
811 if (L->contains(B_ExitBlock)) {
812 B_ExitBlock = A_ExitInsn->getSuccessor(0);
813 A_ExitInsn->setSuccessor(0, B_Header);
Devang Patelada054a2007-08-14 01:30:57 +0000814 } else
Devang Patela8644e32007-08-22 19:33:29 +0000815 A_ExitInsn->setSuccessor(1, B_Header);
816
817 //[*] Update ALoop's exit value using new exit value.
818 ExitCondition->setOperand(ExitValueNum, A_ExitValue);
Devang Patel0b8e02b2007-08-21 21:12:02 +0000819
Devang Patela8644e32007-08-22 19:33:29 +0000820 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
821 // original loop's preheader. Add incoming PHINode values from
822 // ALoop's exiting block. Update BLoop header's domiantor info.
823
Devang Patelada054a2007-08-14 01:30:57 +0000824 // Collect inverse map of Header PHINodes.
825 DenseMap<Value *, Value *> InverseMap;
826 for (BasicBlock::iterator BI = L->getHeader()->begin(),
827 BE = L->getHeader()->end(); BI != BE; ++BI) {
828 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
829 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
830 InverseMap[PNClone] = PN;
831 } else
832 break;
833 }
Devang Patela8644e32007-08-22 19:33:29 +0000834 BasicBlock *Preheader = L->getLoopPreheader();
835 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
Devang Patel98147a32007-08-12 07:02:51 +0000836 BI != BE; ++BI) {
837 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela8644e32007-08-22 19:33:29 +0000838 // Remove incoming value from original preheader.
839 PN->removeIncomingValue(Preheader);
840
841 // Add incoming value from A_ExitingBlock.
842 if (PN == B_IndVar)
843 PN->addIncoming(B_StartValue, A_ExitingBlock);
Devang Patelada054a2007-08-14 01:30:57 +0000844 else {
845 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
Devang Patela8644e32007-08-22 19:33:29 +0000846 Value *V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
847 PN->addIncoming(V2, A_ExitingBlock);
Devang Patelada054a2007-08-14 01:30:57 +0000848 }
849 } else
Devang Patel98147a32007-08-12 07:02:51 +0000850 break;
851 }
Devang Patela8644e32007-08-22 19:33:29 +0000852 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
853 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
Devang Patel0b8e02b2007-08-21 21:12:02 +0000854
Devang Patela8644e32007-08-22 19:33:29 +0000855 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
856 // block. Remove incoming PHINode values from ALoop's exiting block.
857 // Add new incoming values from BLoop's incoming exiting value.
858 // Update BLoop exit block's dominator info..
859 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
860 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
Devang Patelada054a2007-08-14 01:30:57 +0000861 BI != BE; ++BI) {
862 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela8644e32007-08-22 19:33:29 +0000863 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
864 B_ExitingBlock);
865 PN->removeIncomingValue(A_ExitingBlock);
Devang Patelada054a2007-08-14 01:30:57 +0000866 } else
867 break;
868 }
Devang Patel98147a32007-08-12 07:02:51 +0000869
Devang Patela8644e32007-08-22 19:33:29 +0000870 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
871 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
Devang Patelfc4c5f82007-08-13 22:13:24 +0000872
Devang Patela8644e32007-08-22 19:33:29 +0000873 //[*] Split ALoop's exit edge. This creates a new block which
874 // serves two purposes. First one is to hold PHINode defnitions
875 // to ensure that ALoop's LCSSA form. Second use it to act
876 // as a preheader for BLoop.
877 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
Devang Patel423c8b22007-08-10 18:07:13 +0000878
Devang Patela8644e32007-08-22 19:33:29 +0000879 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
880 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
881 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
Devang Patel60cbab42007-08-21 19:47:46 +0000882 BI != BE; ++BI) {
883 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela8644e32007-08-22 19:33:29 +0000884 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
Devang Patel60cbab42007-08-21 19:47:46 +0000885 PHINode *newPHI = new PHINode(PN->getType(), PN->getName());
Devang Patela8644e32007-08-22 19:33:29 +0000886 newPHI->addIncoming(V1, A_ExitingBlock);
887 A_ExitBlock->getInstList().push_front(newPHI);
888 PN->removeIncomingValue(A_ExitBlock);
889 PN->addIncoming(newPHI, A_ExitBlock);
Devang Patel60cbab42007-08-21 19:47:46 +0000890 } else
891 break;
892 }
893
Devang Patela8644e32007-08-22 19:33:29 +0000894 //[*] Eliminate split condition's inactive branch from ALoop.
895 BasicBlock *A_SplitCondBlock = SD.SplitCondition->getParent();
896 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
Devang Patel4259fe32007-08-24 06:17:19 +0000897 BasicBlock *A_InactiveBranch = NULL;
898 BasicBlock *A_ActiveBranch = NULL;
899 if (SD.UseTrueBranchFirst) {
900 A_ActiveBranch = A_BR->getSuccessor(0);
901 A_InactiveBranch = A_BR->getSuccessor(1);
902 } else {
903 A_ActiveBranch = A_BR->getSuccessor(1);
904 A_InactiveBranch = A_BR->getSuccessor(0);
905 }
Devang Patel7097e9a2007-08-24 19:32:26 +0000906 A_BR->setUnconditionalDest(A_ActiveBranch);
Devang Patela8644e32007-08-22 19:33:29 +0000907 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
908
909 //[*] Eliminate split condition's inactive branch in from BLoop.
910 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
911 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
Devang Patel4259fe32007-08-24 06:17:19 +0000912 BasicBlock *B_InactiveBranch = NULL;
913 BasicBlock *B_ActiveBranch = NULL;
914 if (SD.UseTrueBranchFirst) {
915 B_ActiveBranch = B_BR->getSuccessor(1);
916 B_InactiveBranch = B_BR->getSuccessor(0);
917 } else {
918 B_ActiveBranch = B_BR->getSuccessor(0);
919 B_InactiveBranch = B_BR->getSuccessor(1);
920 }
Devang Patel7097e9a2007-08-24 19:32:26 +0000921 B_BR->setUnconditionalDest(B_ActiveBranch);
Devang Patela8644e32007-08-22 19:33:29 +0000922 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
923
Devang Patel98147a32007-08-12 07:02:51 +0000924 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000925}