blob: a01798b48f5a0b46b91ff79577b230734c042b76 [file] [log] [blame]
Devang Patelbc5fe632007-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 Patelbc5fe632007-08-07 00:25:56 +000016#include "llvm/Transforms/Scalar.h"
17#include "llvm/Analysis/LoopPass.h"
18#include "llvm/Analysis/ScalarEvolutionExpander.h"
Devang Patel95fd7172007-08-08 21:39:47 +000019#include "llvm/Analysis/Dominators.h"
Devang Patel901f67e2007-08-10 18:07:13 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21#include "llvm/Transforms/Utils/Cloning.h"
Devang Patelbc5fe632007-08-07 00:25:56 +000022#include "llvm/Support/Compiler.h"
Devang Patelf4277122007-08-15 03:31:47 +000023#include "llvm/ADT/DepthFirstIterator.h"
Devang Patelbc5fe632007-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 Patel901f67e2007-08-10 18:07:13 +000046 AU.addRequired<LoopInfo>();
Devang Patelbc5fe632007-08-07 00:25:56 +000047 AU.addPreserved<LoopInfo>();
48 AU.addRequiredID(LoopSimplifyID);
49 AU.addPreservedID(LoopSimplifyID);
Devang Patel0aaeb172007-08-08 22:25:28 +000050 AU.addRequired<DominatorTree>();
Devang Patelf4277122007-08-15 03:31:47 +000051 AU.addRequired<DominanceFrontier>();
Devang Patel95fd7172007-08-08 21:39:47 +000052 AU.addPreserved<DominatorTree>();
53 AU.addPreserved<DominanceFrontier>();
Devang Patelbc5fe632007-08-07 00:25:56 +000054 }
55
56 private:
Devang Patelc8dadbf2007-08-08 21:02:17 +000057
58 class SplitInfo {
59 public:
Devang Patel61571ca2007-08-10 00:33:50 +000060 SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {}
Devang Patel2545f7b2007-08-09 01:39:01 +000061
Devang Patelc8dadbf2007-08-08 21:02:17 +000062 // Induction variable's range is split at this value.
63 Value *SplitValue;
64
Devang Patelc8dadbf2007-08-08 21:02:17 +000065 // This compare instruction compares IndVar against SplitValue.
66 ICmpInst *SplitCondition;
67
Devang Patel31696332007-08-08 21:18:27 +000068 // Clear split info.
69 void clear() {
Devang Patel31696332007-08-08 21:18:27 +000070 SplitValue = NULL;
Devang Patel31696332007-08-08 21:18:27 +000071 SplitCondition = NULL;
Devang Patel31696332007-08-08 21:18:27 +000072 }
Devang Patel2545f7b2007-08-09 01:39:01 +000073
Devang Patelc8dadbf2007-08-08 21:02:17 +000074 };
Devang Patel61571ca2007-08-10 00:33:50 +000075
Devang Patelc8dadbf2007-08-08 21:02:17 +000076 private:
Devang Patelbc5fe632007-08-07 00:25:56 +000077 /// Find condition inside a loop that is suitable candidate for index split.
78 void findSplitCondition();
79
Devang Patel61571ca2007-08-10 00:33:50 +000080 /// Find loop's exit condition.
81 void findLoopConditionals();
82
83 /// Return induction variable associated with value V.
84 void findIndVar(Value *V, Loop *L);
85
Devang Patelbc5fe632007-08-07 00:25:56 +000086 /// processOneIterationLoop - Current loop L contains compare instruction
87 /// that compares induction variable, IndVar, agains loop invariant. If
88 /// entire (i.e. meaningful) loop body is dominated by this compare
89 /// instruction then loop body is executed only for one iteration. In
90 /// such case eliminate loop structure surrounding this loop body. For
Devang Patel901f67e2007-08-10 18:07:13 +000091 bool processOneIterationLoop(SplitInfo &SD);
Devang Patelbc5fe632007-08-07 00:25:56 +000092
Devang Patel0aaeb172007-08-08 22:25:28 +000093 /// If loop header includes loop variant instruction operands then
94 /// this loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +000095 bool safeHeader(SplitInfo &SD, BasicBlock *BB);
Devang Patelbc5fe632007-08-07 00:25:56 +000096
Devang Patel9263fc32007-08-20 23:51:18 +000097 /// If Exiting block includes loop variant instructions then this
Devang Patel0aaeb172007-08-08 22:25:28 +000098 /// loop may not be eliminated.
Devang Patel9263fc32007-08-20 23:51:18 +000099 bool safeExitingBlock(SplitInfo &SD, BasicBlock *BB);
Devang Patelbc5fe632007-08-07 00:25:56 +0000100
Devang Patel60a94c72007-08-14 18:35:57 +0000101 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
102 /// This routine is used to remove split condition's dead branch, dominated by
103 /// DeadBB. LiveBB dominates split conidition's other branch.
104 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000105
Devang Patel0aaeb172007-08-08 22:25:28 +0000106 /// Find cost of spliting loop L.
107 unsigned findSplitCost(Loop *L, SplitInfo &SD);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000108 bool splitLoop(SplitInfo &SD);
Devang Patelbc5fe632007-08-07 00:25:56 +0000109
Devang Patel61571ca2007-08-10 00:33:50 +0000110 void initialize() {
111 IndVar = NULL;
112 IndVarIncrement = NULL;
113 ExitCondition = NULL;
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000114 StartValue = NULL;
115 ExitValueNum = 0;
116 SplitData.clear();
Devang Patel61571ca2007-08-10 00:33:50 +0000117 }
118
Devang Patelbc5fe632007-08-07 00:25:56 +0000119 private:
120
121 // Current Loop.
122 Loop *L;
Devang Patel901f67e2007-08-10 18:07:13 +0000123 LPPassManager *LPM;
124 LoopInfo *LI;
Devang Patelbc5fe632007-08-07 00:25:56 +0000125 ScalarEvolution *SE;
Devang Patel0aaeb172007-08-08 22:25:28 +0000126 DominatorTree *DT;
Devang Patelb7639612007-08-13 22:13:24 +0000127 DominanceFrontier *DF;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000128 SmallVector<SplitInfo, 4> SplitData;
Devang Patel61571ca2007-08-10 00:33:50 +0000129
130 // Induction variable whose range is being split by this transformation.
131 PHINode *IndVar;
132 Instruction *IndVarIncrement;
133
134 // Loop exit condition.
135 ICmpInst *ExitCondition;
136
137 // Induction variable's initial value.
138 Value *StartValue;
139
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000140 // Induction variable's final loop exit value operand number in exit condition..
141 unsigned ExitValueNum;
Devang Patelbc5fe632007-08-07 00:25:56 +0000142 };
143
144 char LoopIndexSplit::ID = 0;
145 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
146}
147
148LoopPass *llvm::createLoopIndexSplitPass() {
149 return new LoopIndexSplit();
150}
151
152// Index split Loop L. Return true if loop is split.
Devang Patel901f67e2007-08-10 18:07:13 +0000153bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000154 bool Changed = false;
155 L = IncomingLoop;
Devang Patel901f67e2007-08-10 18:07:13 +0000156 LPM = &LPM_Ref;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000157
Devang Patel81fcdfb2007-08-15 02:14:55 +0000158 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel79276b32007-08-14 23:53:57 +0000159 if (!L->getSubLoops().empty())
160 return false;
161
Devang Patelbc5fe632007-08-07 00:25:56 +0000162 SE = &getAnalysis<ScalarEvolution>();
Devang Patel0aaeb172007-08-08 22:25:28 +0000163 DT = &getAnalysis<DominatorTree>();
Devang Patel901f67e2007-08-10 18:07:13 +0000164 LI = &getAnalysis<LoopInfo>();
Devang Patel2190f172007-08-15 03:34:53 +0000165 DF = &getAnalysis<DominanceFrontier>();
Devang Patelbc5fe632007-08-07 00:25:56 +0000166
Devang Patel61571ca2007-08-10 00:33:50 +0000167 initialize();
168
169 findLoopConditionals();
170
171 if (!ExitCondition)
172 return false;
173
Devang Patelbc5fe632007-08-07 00:25:56 +0000174 findSplitCondition();
175
Devang Patelc8dadbf2007-08-08 21:02:17 +0000176 if (SplitData.empty())
Devang Patelbc5fe632007-08-07 00:25:56 +0000177 return false;
178
Devang Patelc8dadbf2007-08-08 21:02:17 +0000179 // First see if it is possible to eliminate loop itself or not.
180 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Devang Patel49fbf5a2007-08-20 20:24:15 +0000181 E = SplitData.end(); SI != E;) {
Devang Patelc8dadbf2007-08-08 21:02:17 +0000182 SplitInfo &SD = *SI;
183 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
Devang Patel901f67e2007-08-10 18:07:13 +0000184 Changed = processOneIterationLoop(SD);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000185 if (Changed) {
186 ++NumIndexSplit;
187 // If is loop is eliminated then nothing else to do here.
188 return Changed;
Devang Patel49fbf5a2007-08-20 20:24:15 +0000189 } else {
190 SmallVector<SplitInfo, 4>::iterator Delete_SI = SI;
191 ++SI;
192 SplitData.erase(Delete_SI);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000193 }
Devang Patel49fbf5a2007-08-20 20:24:15 +0000194 } else
195 ++SI;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000196 }
197
Devang Patel0aaeb172007-08-08 22:25:28 +0000198 unsigned MaxCost = 99;
199 unsigned Index = 0;
200 unsigned MostProfitableSDIndex = 0;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000201 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Devang Patel0aaeb172007-08-08 22:25:28 +0000202 E = SplitData.end(); SI != E; ++SI, ++Index) {
203 SplitInfo SD = *SI;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000204
205 // ICM_EQs are already handled above.
Devang Patel49fbf5a2007-08-20 20:24:15 +0000206 assert (SD.SplitCondition->getPredicate() != ICmpInst::ICMP_EQ &&
207 "Unexpected split condition predicate");
Devang Patel0aaeb172007-08-08 22:25:28 +0000208
209 unsigned Cost = findSplitCost(L, SD);
210 if (Cost < MaxCost)
211 MostProfitableSDIndex = Index;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000212 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000213
Devang Patel0aaeb172007-08-08 22:25:28 +0000214 // Split most profitiable condition.
Devang Patel49fbf5a2007-08-20 20:24:15 +0000215 if (!SplitData.empty())
216 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
Devang Patel0aaeb172007-08-08 22:25:28 +0000217
Devang Patelbc5fe632007-08-07 00:25:56 +0000218 if (Changed)
219 ++NumIndexSplit;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000220
Devang Patelbc5fe632007-08-07 00:25:56 +0000221 return Changed;
222}
223
Devang Patel2545f7b2007-08-09 01:39:01 +0000224/// Return true if V is a induction variable or induction variable's
225/// increment for loop L.
Devang Patel61571ca2007-08-10 00:33:50 +0000226void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000227
228 Instruction *I = dyn_cast<Instruction>(V);
229 if (!I)
Devang Patel61571ca2007-08-10 00:33:50 +0000230 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000231
232 // Check if I is a phi node from loop header or not.
233 if (PHINode *PN = dyn_cast<PHINode>(V)) {
234 if (PN->getParent() == L->getHeader()) {
Devang Patel61571ca2007-08-10 00:33:50 +0000235 IndVar = PN;
236 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000237 }
238 }
239
240 // Check if I is a add instruction whose one operand is
241 // phi node from loop header and second operand is constant.
242 if (I->getOpcode() != Instruction::Add)
Devang Patel61571ca2007-08-10 00:33:50 +0000243 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000244
245 Value *Op0 = I->getOperand(0);
246 Value *Op1 = I->getOperand(1);
247
248 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
249 if (PN->getParent() == L->getHeader()
250 && isa<ConstantInt>(Op1)) {
251 IndVar = PN;
252 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000253 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000254 }
255 }
256
257 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
258 if (PN->getParent() == L->getHeader()
259 && isa<ConstantInt>(Op0)) {
260 IndVar = PN;
261 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000262 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000263 }
264 }
265
Devang Patel61571ca2007-08-10 00:33:50 +0000266 return;
267}
268
269// Find loop's exit condition and associated induction variable.
270void LoopIndexSplit::findLoopConditionals() {
271
Devang Patel9263fc32007-08-20 23:51:18 +0000272 BasicBlock *ExitingBlock = NULL;
Devang Patel61571ca2007-08-10 00:33:50 +0000273
274 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
275 I != E; ++I) {
276 BasicBlock *BB = *I;
277 if (!L->isLoopExit(BB))
278 continue;
Devang Patel9263fc32007-08-20 23:51:18 +0000279 if (ExitingBlock)
Devang Patel61571ca2007-08-10 00:33:50 +0000280 return;
Devang Patel9263fc32007-08-20 23:51:18 +0000281 ExitingBlock = BB;
Devang Patel61571ca2007-08-10 00:33:50 +0000282 }
283
Devang Patel9263fc32007-08-20 23:51:18 +0000284 if (!ExitingBlock)
Devang Patel61571ca2007-08-10 00:33:50 +0000285 return;
286
287 // If exit block's terminator is conditional branch inst then we have found
288 // exit condition.
Devang Patel9263fc32007-08-20 23:51:18 +0000289 BranchInst *BR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patel61571ca2007-08-10 00:33:50 +0000290 if (!BR || BR->isUnconditional())
291 return;
292
293 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
294 if (!CI)
295 return;
296
297 ExitCondition = CI;
298
299 // Exit condition's one operand is loop invariant exit value and second
300 // operand is SCEVAddRecExpr based on induction variable.
301 Value *V0 = CI->getOperand(0);
302 Value *V1 = CI->getOperand(1);
303
304 SCEVHandle SH0 = SE->getSCEV(V0);
305 SCEVHandle SH1 = SE->getSCEV(V1);
306
307 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000308 ExitValueNum = 0;
Devang Patel61571ca2007-08-10 00:33:50 +0000309 findIndVar(V1, L);
310 }
311 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000312 ExitValueNum = 1;
Devang Patel61571ca2007-08-10 00:33:50 +0000313 findIndVar(V0, L);
314 }
315
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000316 if (!IndVar)
Devang Patel61571ca2007-08-10 00:33:50 +0000317 ExitCondition = NULL;
318 else if (IndVar) {
319 BasicBlock *Preheader = L->getLoopPreheader();
320 StartValue = IndVar->getIncomingValueForBlock(Preheader);
321 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000322}
323
Devang Patelbc5fe632007-08-07 00:25:56 +0000324/// Find condition inside a loop that is suitable candidate for index split.
325void LoopIndexSplit::findSplitCondition() {
326
Devang Patelc8dadbf2007-08-08 21:02:17 +0000327 SplitInfo SD;
Devang Patel2545f7b2007-08-09 01:39:01 +0000328 // Check all basic block's terminators.
Devang Patelbc5fe632007-08-07 00:25:56 +0000329
Devang Patel2545f7b2007-08-09 01:39:01 +0000330 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
331 I != E; ++I) {
332 BasicBlock *BB = *I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000333
Devang Patel2545f7b2007-08-09 01:39:01 +0000334 // If this basic block does not terminate in a conditional branch
335 // then terminator is not a suitable split condition.
336 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
337 if (!BR)
338 continue;
339
340 if (BR->isUnconditional())
Devang Patelbc5fe632007-08-07 00:25:56 +0000341 continue;
342
Devang Patel2545f7b2007-08-09 01:39:01 +0000343 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
Devang Patel61571ca2007-08-10 00:33:50 +0000344 if (!CI || CI == ExitCondition)
Devang Patel2545f7b2007-08-09 01:39:01 +0000345 return;
Devang Patelbc5fe632007-08-07 00:25:56 +0000346
Devang Patel2545f7b2007-08-09 01:39:01 +0000347 // If one operand is loop invariant and second operand is SCEVAddRecExpr
348 // based on induction variable then CI is a candidate split condition.
349 Value *V0 = CI->getOperand(0);
350 Value *V1 = CI->getOperand(1);
351
352 SCEVHandle SH0 = SE->getSCEV(V0);
353 SCEVHandle SH1 = SE->getSCEV(V1);
354
355 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
356 SD.SplitValue = V0;
357 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000358 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
359 if (PN == IndVar)
360 SplitData.push_back(SD);
361 }
362 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
363 if (IndVarIncrement && IndVarIncrement == Insn)
364 SplitData.push_back(SD);
365 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000366 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000367 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
368 SD.SplitValue = V1;
369 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000370 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
371 if (PN == IndVar)
372 SplitData.push_back(SD);
373 }
374 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
375 if (IndVarIncrement && IndVarIncrement == Insn)
376 SplitData.push_back(SD);
377 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000378 }
379 }
380}
381
382/// processOneIterationLoop - Current loop L contains compare instruction
383/// that compares induction variable, IndVar, against loop invariant. If
384/// entire (i.e. meaningful) loop body is dominated by this compare
385/// instruction then loop body is executed only once. In such case eliminate
386/// loop structure surrounding this loop body. For example,
387/// for (int i = start; i < end; ++i) {
388/// if ( i == somevalue) {
389/// loop_body
390/// }
391/// }
392/// can be transformed into
393/// if (somevalue >= start && somevalue < end) {
394/// i = somevalue;
395/// loop_body
396/// }
Devang Patel901f67e2007-08-10 18:07:13 +0000397bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000398
399 BasicBlock *Header = L->getHeader();
400
401 // First of all, check if SplitCondition dominates entire loop body
402 // or not.
403
404 // If SplitCondition is not in loop header then this loop is not suitable
405 // for this transformation.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000406 if (SD.SplitCondition->getParent() != Header)
Devang Patelbc5fe632007-08-07 00:25:56 +0000407 return false;
408
Devang Patelbc5fe632007-08-07 00:25:56 +0000409 // If loop header includes loop variant instruction operands then
410 // this loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000411 if (!safeHeader(SD, Header))
Devang Patelbc5fe632007-08-07 00:25:56 +0000412 return false;
413
Devang Patel9263fc32007-08-20 23:51:18 +0000414 // If Exiting block includes loop variant instructions then this
Devang Patelbc5fe632007-08-07 00:25:56 +0000415 // loop may not be eliminated.
Devang Patel9263fc32007-08-20 23:51:18 +0000416 if (!safeExitingBlock(SD, ExitCondition->getParent()))
Devang Patelbc5fe632007-08-07 00:25:56 +0000417 return false;
418
Devang Patel2bcb5012007-08-08 01:51:27 +0000419 // Update CFG.
420
Devang Patelc166b952007-08-20 20:49:01 +0000421 // Replace index variable with split value in loop body. Loop body is executed
422 // only when index variable is equal to split value.
423 IndVar->replaceAllUsesWith(SD.SplitValue);
424
425 // Remove Latch to Header edge.
Devang Patelbc5fe632007-08-07 00:25:56 +0000426 BasicBlock *Latch = L->getLoopLatch();
Devang Patel2bcb5012007-08-08 01:51:27 +0000427 BasicBlock *LatchSucc = NULL;
428 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
429 if (!BR)
430 return false;
431 Header->removePredecessor(Latch);
432 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
433 SI != E; ++SI) {
434 if (Header != *SI)
435 LatchSucc = *SI;
436 }
437 BR->setUnconditionalDest(LatchSucc);
438
Devang Patelbc5fe632007-08-07 00:25:56 +0000439 Instruction *Terminator = Header->getTerminator();
Devang Patel59e0c062007-08-14 01:30:57 +0000440 Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
Devang Patelbc5fe632007-08-07 00:25:56 +0000441
Devang Patelbc5fe632007-08-07 00:25:56 +0000442 // Replace split condition in header.
443 // Transform
444 // SplitCondition : icmp eq i32 IndVar, SplitValue
445 // into
446 // c1 = icmp uge i32 SplitValue, StartValue
447 // c2 = icmp ult i32 vSplitValue, ExitValue
448 // and i32 c1, c2
Devang Patel61571ca2007-08-10 00:33:50 +0000449 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patelbc5fe632007-08-07 00:25:56 +0000450 Instruction *C1 = new ICmpInst(SignedPredicate ?
451 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Devang Patelc8dadbf2007-08-08 21:02:17 +0000452 SD.SplitValue, StartValue, "lisplit",
453 Terminator);
Devang Patelbc5fe632007-08-07 00:25:56 +0000454 Instruction *C2 = new ICmpInst(SignedPredicate ?
455 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel59e0c062007-08-14 01:30:57 +0000456 SD.SplitValue, ExitValue, "lisplit",
Devang Patelc8dadbf2007-08-08 21:02:17 +0000457 Terminator);
458 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
459 Terminator);
460 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
461 SD.SplitCondition->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000462
Devang Patelbc5fe632007-08-07 00:25:56 +0000463 // Now, clear latch block. Remove instructions that are responsible
464 // to increment induction variable.
465 Instruction *LTerminator = Latch->getTerminator();
466 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
467 LB != LE; ) {
468 Instruction *I = LB;
469 ++LB;
470 if (isa<PHINode>(I) || I == LTerminator)
471 continue;
472
Devang Patel59e0c062007-08-14 01:30:57 +0000473 if (I == IndVarIncrement)
474 I->replaceAllUsesWith(ExitValue);
475 else
476 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Devang Patel0d75c292007-08-07 17:45:35 +0000477 I->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000478 }
479
Devang Patel901f67e2007-08-10 18:07:13 +0000480 LPM->deleteLoopFromQueue(L);
Devang Patel95fd7172007-08-08 21:39:47 +0000481
482 // Update Dominator Info.
483 // Only CFG change done is to remove Latch to Header edge. This
484 // does not change dominator tree because Latch did not dominate
485 // Header.
Devang Patelb7639612007-08-13 22:13:24 +0000486 if (DF) {
Devang Patel95fd7172007-08-08 21:39:47 +0000487 DominanceFrontier::iterator HeaderDF = DF->find(Header);
488 if (HeaderDF != DF->end())
489 DF->removeFromFrontier(HeaderDF, Header);
490
491 DominanceFrontier::iterator LatchDF = DF->find(Latch);
492 if (LatchDF != DF->end())
493 DF->removeFromFrontier(LatchDF, Header);
494 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000495 return true;
496}
497
498// If loop header includes loop variant instruction operands then
499// this loop can not be eliminated. This is used by processOneIterationLoop().
Devang Patelc8dadbf2007-08-08 21:02:17 +0000500bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000501
502 Instruction *Terminator = Header->getTerminator();
503 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
504 BI != BE; ++BI) {
505 Instruction *I = BI;
506
Devang Patel59e0c062007-08-14 01:30:57 +0000507 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000508 if (isa<PHINode>(I))
509 continue;
510
511 // SplitCondition itself is OK.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000512 if (I == SD.SplitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000513 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000514
Devang Patel2545f7b2007-08-09 01:39:01 +0000515 // Induction variable is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000516 if (I == IndVar)
Devang Patel2545f7b2007-08-09 01:39:01 +0000517 continue;
518
519 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000520 if (I == IndVarIncrement)
Devang Patel2545f7b2007-08-09 01:39:01 +0000521 continue;
522
Devang Patelbc5fe632007-08-07 00:25:56 +0000523 // Terminator is also harmless.
524 if (I == Terminator)
525 continue;
526
527 // Otherwise we have a instruction that may not be safe.
528 return false;
529 }
530
531 return true;
532}
533
Devang Patel9263fc32007-08-20 23:51:18 +0000534// If Exiting block includes loop variant instructions then this
Devang Patelbc5fe632007-08-07 00:25:56 +0000535// loop may not be eliminated. This is used by processOneIterationLoop().
Devang Patel9263fc32007-08-20 23:51:18 +0000536bool LoopIndexSplit::safeExitingBlock(SplitInfo &SD,
537 BasicBlock *ExitingBlock) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000538
Devang Patel9263fc32007-08-20 23:51:18 +0000539 for (BasicBlock::iterator BI = ExitingBlock->begin(),
540 BE = ExitingBlock->end(); BI != BE; ++BI) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000541 Instruction *I = BI;
542
Devang Patel59e0c062007-08-14 01:30:57 +0000543 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000544 if (isa<PHINode>(I))
545 continue;
546
Devang Patel2545f7b2007-08-09 01:39:01 +0000547 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000548 if (IndVarIncrement && IndVarIncrement == I)
Devang Patel2545f7b2007-08-09 01:39:01 +0000549 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000550
Devang Patel2545f7b2007-08-09 01:39:01 +0000551 // Check if I is induction variable increment instruction.
Devang Patel61571ca2007-08-10 00:33:50 +0000552 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000553
554 Value *Op0 = I->getOperand(0);
555 Value *Op1 = I->getOperand(1);
Devang Patelbc5fe632007-08-07 00:25:56 +0000556 PHINode *PN = NULL;
557 ConstantInt *CI = NULL;
558
559 if ((PN = dyn_cast<PHINode>(Op0))) {
560 if ((CI = dyn_cast<ConstantInt>(Op1)))
Devang Patel61571ca2007-08-10 00:33:50 +0000561 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000562 } else
563 if ((PN = dyn_cast<PHINode>(Op1))) {
564 if ((CI = dyn_cast<ConstantInt>(Op0)))
Devang Patel61571ca2007-08-10 00:33:50 +0000565 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000566 }
567
Devang Patel61571ca2007-08-10 00:33:50 +0000568 if (IndVarIncrement && PN == IndVar && CI->isOne())
Devang Patelbc5fe632007-08-07 00:25:56 +0000569 continue;
570 }
Devang Patel2bcb5012007-08-08 01:51:27 +0000571
Devang Patelbc5fe632007-08-07 00:25:56 +0000572 // I is an Exit condition if next instruction is block terminator.
573 // Exit condition is OK if it compares loop invariant exit value,
574 // which is checked below.
Devang Patel3719d4f2007-08-07 23:17:52 +0000575 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
Devang Patel61571ca2007-08-10 00:33:50 +0000576 if (EC == ExitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000577 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000578 }
579
Devang Patel9263fc32007-08-20 23:51:18 +0000580 if (I == ExitingBlock->getTerminator())
Devang Patel61571ca2007-08-10 00:33:50 +0000581 continue;
582
Devang Patelbc5fe632007-08-07 00:25:56 +0000583 // Otherwise we have instruction that may not be safe.
584 return false;
585 }
586
Devang Patel9263fc32007-08-20 23:51:18 +0000587 // We could not find any reason to consider ExitingBlock unsafe.
Devang Patelbc5fe632007-08-07 00:25:56 +0000588 return true;
589}
590
Devang Patel0aaeb172007-08-08 22:25:28 +0000591/// Find cost of spliting loop L. Cost is measured in terms of size growth.
592/// Size is growth is calculated based on amount of code duplicated in second
593/// loop.
594unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
595
596 unsigned Cost = 0;
597 BasicBlock *SDBlock = SD.SplitCondition->getParent();
598 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
599 I != E; ++I) {
600 BasicBlock *BB = *I;
601 // If a block is not dominated by split condition block then
602 // it must be duplicated in both loops.
603 if (!DT->dominates(SDBlock, BB))
604 Cost += BB->size();
605 }
606
607 return Cost;
608}
609
Devang Patel60a94c72007-08-14 18:35:57 +0000610/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
611/// This routine is used to remove split condition's dead branch, dominated by
612/// DeadBB. LiveBB dominates split conidition's other branch.
613void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
614 BasicBlock *LiveBB) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000615
Devang Patelf4277122007-08-15 03:31:47 +0000616 // First update DeadBB's dominance frontier.
Devang Patel9cee7a02007-08-17 21:59:16 +0000617 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patelf4277122007-08-15 03:31:47 +0000618 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
619 if (DeadBBDF != DF->end()) {
620 SmallVector<BasicBlock *, 8> PredBlocks;
621
622 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
623 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
624 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) {
625 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel9cee7a02007-08-17 21:59:16 +0000626 FrontierBBs.push_back(FrontierBB);
627
Devang Patelf4277122007-08-15 03:31:47 +0000628 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
629 PredBlocks.clear();
630 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
631 PI != PE; ++PI) {
632 BasicBlock *P = *PI;
633 if (P == DeadBB || DT->dominates(DeadBB, P))
634 PredBlocks.push_back(P);
Devang Patelb7639612007-08-13 22:13:24 +0000635 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000636
637 BasicBlock *NewDominator = NULL;
Devang Patelf4277122007-08-15 03:31:47 +0000638 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
639 FBI != FBE; ++FBI) {
640 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
641 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
642 PE = PredBlocks.end(); PI != PE; ++PI) {
643 BasicBlock *P = *PI;
644 PN->removeIncomingValue(P);
645 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000646 // If we have not identified new dominator then see if we can identify
647 // one based on remaining incoming PHINode values.
648 if (NewDominator == NULL && PN->getNumIncomingValues() == 1)
649 NewDominator = PN->getIncomingBlock(0);
Devang Patelf4277122007-08-15 03:31:47 +0000650 }
651 else
652 break;
Devang Patel9cee7a02007-08-17 21:59:16 +0000653 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000654 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000655 }
Devang Patelf4277122007-08-15 03:31:47 +0000656
657 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
658 SmallVector<BasicBlock *, 32> WorkList;
659 DomTreeNode *DN = DT->getNode(DeadBB);
660 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
661 E = df_end(DN); DI != E; ++DI) {
662 BasicBlock *BB = DI->getBlock();
663 WorkList.push_back(BB);
Devang Patel9cee7a02007-08-17 21:59:16 +0000664 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
Devang Patelf4277122007-08-15 03:31:47 +0000665 }
666
667 while (!WorkList.empty()) {
668 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
669 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
670 BBI != BBE; ++BBI) {
671 Instruction *I = BBI;
672 I->replaceAllUsesWith(UndefValue::get(I->getType()));
673 I->eraseFromParent();
674 }
675 LPM->deleteSimpleAnalysisValue(BB, LP);
676 DT->eraseNode(BB);
677 DF->removeBlock(BB);
678 LI->removeBlock(BB);
679 BB->eraseFromParent();
680 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000681
682 // Update Frontier BBs' dominator info.
683 while (!FrontierBBs.empty()) {
684 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
685 BasicBlock *NewDominator = FBB->getSinglePredecessor();
686 if (!NewDominator) {
687 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
688 NewDominator = *PI;
689 ++PI;
690 if (NewDominator != LiveBB) {
691 for(; PI != PE; ++PI) {
692 BasicBlock *P = *PI;
693 if (P == LiveBB) {
694 NewDominator = LiveBB;
695 break;
696 }
697 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
698 }
699 }
700 }
701 assert (NewDominator && "Unable to fix dominator info.");
702 DT->changeImmediateDominator(FBB, NewDominator);
703 DF->changeImmediateDominator(FBB, NewDominator, DT);
704 }
705
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000706}
707
Devang Patel2a24ff32007-08-21 21:12:02 +0000708/// splitLoop - Split current loop L in two loops using split information
709/// SD. Update dominator information. Maintain LCSSA form.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000710bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
Devang Patelf824fb42007-08-10 00:53:35 +0000711
Devang Patel2a24ff32007-08-21 21:12:02 +0000712 // True loop is original loop. False loop is cloned loop.
Devang Patel81fcdfb2007-08-15 02:14:55 +0000713
Devang Patel2a24ff32007-08-21 21:12:02 +0000714 BasicBlock *TL_Preheader = L->getLoopPreheader();
715 BasicBlock *TL_SplitCondBlock = SD.SplitCondition->getParent();
716 BasicBlock *TL_Latch = L->getLoopLatch();
717 BasicBlock *TL_Header = L->getHeader();
718 BranchInst *TL_SplitTerminator =
719 cast<BranchInst>(TL_SplitCondBlock->getTerminator());
720
Devang Patel81fcdfb2007-08-15 02:14:55 +0000721 // FIXME - Unable to handle triange loops at the moment.
722 // In triangle loop, split condition is in header and one of the
723 // the split destination is loop latch. If split condition is EQ
724 // then such loops are already handle in processOneIterationLoop().
Devang Patel2a24ff32007-08-21 21:12:02 +0000725 BasicBlock *Succ0 = TL_SplitTerminator->getSuccessor(0);
726 BasicBlock *Succ1 = TL_SplitTerminator->getSuccessor(1);
727 if (TL_Header == TL_SplitCondBlock
728 && (TL_Latch == Succ0 || TL_Latch == Succ1))
Devang Patel81fcdfb2007-08-15 02:14:55 +0000729 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000730
Devang Patel9cba64e2007-08-18 00:00:32 +0000731 // If one of the split condition branch is post dominating other then loop
732 // index split is not appropriate.
Devang Patel2a24ff32007-08-21 21:12:02 +0000733 if (DT->dominates(Succ0, TL_Latch) || DT->dominates(Succ1, TL_Latch))
Devang Patel9cee7a02007-08-17 21:59:16 +0000734 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000735
Devang Patel9cba64e2007-08-18 00:00:32 +0000736 // If one of the split condition branch is a predecessor of the other
737 // split condition branch head then do not split loop on this condition.
Devang Patel2a24ff32007-08-21 21:12:02 +0000738 for(pred_iterator PI = pred_begin(Succ0), PE = pred_end(Succ0);
739 PI != PE; ++PI)
Devang Patel9cba64e2007-08-18 00:00:32 +0000740 if (Succ1 == *PI)
741 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000742 for(pred_iterator PI = pred_begin(Succ1), PE = pred_end(Succ1);
743 PI != PE; ++PI)
Devang Patel9cba64e2007-08-18 00:00:32 +0000744 if (Succ0 == *PI)
745 return false;
746
Devang Patelf824fb42007-08-10 00:53:35 +0000747 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patel61571ca2007-08-10 00:33:50 +0000748 //[*] Calculate True loop's new Exit Value in loop preheader.
Devang Patel2a24ff32007-08-21 21:12:02 +0000749 // TL_ExitValue = min(SplitValue, ExitValue)
Devang Patel61571ca2007-08-10 00:33:50 +0000750 //[*] Calculate False loop's new Start Value in loop preheader.
Devang Patel5671efc2007-08-22 18:07:47 +0000751 // FL_StartValue = max(SplitValue, TrueLoop.StartValue)
Devang Patel2a24ff32007-08-21 21:12:02 +0000752 Value *TL_ExitValue = NULL;
753 Value *FL_StartValue = NULL;
Devang Patelf824fb42007-08-10 00:53:35 +0000754 if (isa<ConstantInt>(SD.SplitValue)) {
Devang Patel2a24ff32007-08-21 21:12:02 +0000755 TL_ExitValue = SD.SplitValue;
756 FL_StartValue = SD.SplitValue;
Devang Patelf824fb42007-08-10 00:53:35 +0000757 }
758 else {
Devang Patel2a24ff32007-08-21 21:12:02 +0000759 Instruction *TL_PHTerminator = TL_Preheader->getTerminator();
Devang Patelf824fb42007-08-10 00:53:35 +0000760 Value *C1 = new ICmpInst(SignedPredicate ?
761 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000762 SD.SplitValue,
763 ExitCondition->getOperand(ExitValueNum),
Devang Patel2a24ff32007-08-21 21:12:02 +0000764 "lsplit.ev", TL_PHTerminator);
765 TL_ExitValue = new SelectInst(C1, SD.SplitValue,
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000766 ExitCondition->getOperand(ExitValueNum),
Devang Patel2a24ff32007-08-21 21:12:02 +0000767 "lsplit.ev", TL_PHTerminator);
Devang Patelf824fb42007-08-10 00:53:35 +0000768
769 Value *C2 = new ICmpInst(SignedPredicate ?
770 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
771 SD.SplitValue, StartValue, "lsplit.sv",
Devang Patel2a24ff32007-08-21 21:12:02 +0000772 TL_PHTerminator);
Devang Patel5671efc2007-08-22 18:07:47 +0000773 FL_StartValue = new SelectInst(C2, StartValue, SD.SplitValue,
774 "lsplit.sv", TL_PHTerminator);
Devang Patelf824fb42007-08-10 00:53:35 +0000775 }
Devang Patel901f67e2007-08-10 18:07:13 +0000776
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000777 //[*] Clone loop. Avoid true destination of split condition and
778 // the blocks dominated by true destination.
779 DenseMap<const Value *, Value *> ValueMap;
780 Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this);
Devang Patel2a24ff32007-08-21 21:12:02 +0000781 BasicBlock *FL_Header = FalseLoop->getHeader();
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000782
783 //[*] True loop's exit edge enters False loop.
Devang Patel2a24ff32007-08-21 21:12:02 +0000784 PHINode *FL_IndVar = cast<PHINode>(ValueMap[IndVar]);
785 BasicBlock *TL_ExitingBlock = ExitCondition->getParent();
786 BranchInst *TL_ExitInsn =
787 dyn_cast<BranchInst>(TL_ExitingBlock->getTerminator());
788 assert (TL_ExitInsn && "Unable to find suitable loop exit branch");
789 BasicBlock *TL_ExitDest = TL_ExitInsn->getSuccessor(1);
790
791 if (L->contains(TL_ExitDest)) {
792 TL_ExitDest = TL_ExitInsn->getSuccessor(0);
793 TL_ExitInsn->setSuccessor(0, FL_Header);
Devang Patel59e0c062007-08-14 01:30:57 +0000794 } else
Devang Patel2a24ff32007-08-21 21:12:02 +0000795 TL_ExitInsn->setSuccessor(1, FL_Header);
796
Devang Patel59e0c062007-08-14 01:30:57 +0000797 // Collect inverse map of Header PHINodes.
798 DenseMap<Value *, Value *> InverseMap;
799 for (BasicBlock::iterator BI = L->getHeader()->begin(),
800 BE = L->getHeader()->end(); BI != BE; ++BI) {
801 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
802 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
803 InverseMap[PNClone] = PN;
804 } else
805 break;
806 }
Devang Patel2a24ff32007-08-21 21:12:02 +0000807
Devang Patel59e0c062007-08-14 01:30:57 +0000808 // Update False loop's header
Devang Patel2a24ff32007-08-21 21:12:02 +0000809 for (BasicBlock::iterator BI = FL_Header->begin(), BE = FL_Header->end();
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000810 BI != BE; ++BI) {
811 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patel2a24ff32007-08-21 21:12:02 +0000812 PN->removeIncomingValue(TL_Preheader);
813 if (PN == FL_IndVar)
814 PN->addIncoming(FL_StartValue, TL_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000815 else {
816 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
Devang Patel2a24ff32007-08-21 21:12:02 +0000817 Value *V2 = OrigPN->getIncomingValueForBlock(TL_ExitingBlock);
818 PN->addIncoming(V2, TL_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000819 }
820 } else
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000821 break;
822 }
Devang Patel2a24ff32007-08-21 21:12:02 +0000823
824 // Update TL_ExitDest. Now it's predecessor is False loop's exit block.
825 BasicBlock *FL_ExitingBlock = cast<BasicBlock>(ValueMap[TL_ExitingBlock]);
826 for (BasicBlock::iterator BI = TL_ExitDest->begin(), BE = TL_ExitDest->end();
Devang Patel59e0c062007-08-14 01:30:57 +0000827 BI != BE; ++BI) {
828 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patel2a24ff32007-08-21 21:12:02 +0000829 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(TL_ExitingBlock)],
830 FL_ExitingBlock);
831 PN->removeIncomingValue(TL_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000832 } else
833 break;
834 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000835
Devang Patelb7639612007-08-13 22:13:24 +0000836 if (DT) {
Devang Patel2a24ff32007-08-21 21:12:02 +0000837 DT->changeImmediateDominator(FL_Header, TL_ExitingBlock);
838 DT->changeImmediateDominator(TL_ExitDest,
839 cast<BasicBlock>(ValueMap[TL_ExitingBlock]));
Devang Patelb7639612007-08-13 22:13:24 +0000840 }
841
Devang Patel2a24ff32007-08-21 21:12:02 +0000842 assert (!L->contains(TL_ExitDest) && " Unable to find exit edge destination");
Devang Patel901f67e2007-08-10 18:07:13 +0000843
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000844 //[*] Split Exit Edge.
Devang Patel2a24ff32007-08-21 21:12:02 +0000845 BasicBlock *TL_ExitBlock = SplitEdge(TL_ExitingBlock, FL_Header, this);
Devang Patel901f67e2007-08-10 18:07:13 +0000846
Devang Patel61571ca2007-08-10 00:33:50 +0000847 //[*] Eliminate split condition's false branch from True loop.
Devang Patel2a24ff32007-08-21 21:12:02 +0000848 BranchInst *TL_BR = cast<BranchInst>(TL_SplitCondBlock->getTerminator());
849 BasicBlock *TL_FalseBlock = TL_BR->getSuccessor(1);
850 TL_BR->setUnconditionalDest(TL_BR->getSuccessor(0));
851 removeBlocks(TL_FalseBlock, L, TL_BR->getSuccessor(0));
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000852
853 //[*] Update True loop's exit value using new exit value.
Devang Patel2a24ff32007-08-21 21:12:02 +0000854 ExitCondition->setOperand(ExitValueNum, TL_ExitValue);
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000855
856 //[*] Eliminate split condition's true branch in False loop CFG.
Devang Patel2a24ff32007-08-21 21:12:02 +0000857 BasicBlock *FL_SplitCondBlock = cast<BasicBlock>(ValueMap[TL_SplitCondBlock]);
858 BranchInst *FL_BR = cast<BranchInst>(FL_SplitCondBlock->getTerminator());
859 BasicBlock *FL_TrueBlock = FL_BR->getSuccessor(0);
860 FL_BR->setUnconditionalDest(FL_BR->getSuccessor(1));
861 removeBlocks(FL_TrueBlock, FalseLoop,
862 cast<BasicBlock>(FL_BR->getSuccessor(0)));
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000863
Devang Patel7ef89b82007-08-21 19:47:46 +0000864 //[*] Preserve LCSSA
Devang Patel2a24ff32007-08-21 21:12:02 +0000865 for(BasicBlock::iterator BI = FL_Header->begin(), BE = FL_Header->end();
Devang Patel7ef89b82007-08-21 19:47:46 +0000866 BI != BE; ++BI) {
867 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
868 Value *V1 = PN->getIncomingValueForBlock(TL_ExitBlock);
869 PHINode *newPHI = new PHINode(PN->getType(), PN->getName());
Devang Patel2a24ff32007-08-21 21:12:02 +0000870 newPHI->addIncoming(V1, TL_ExitingBlock);
Devang Patel7ef89b82007-08-21 19:47:46 +0000871 TL_ExitBlock->getInstList().push_front(newPHI);
872 PN->removeIncomingValue(TL_ExitBlock);
873 PN->addIncoming(newPHI, TL_ExitBlock);
874 } else
875 break;
876 }
877
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000878 return true;
Devang Patelbc5fe632007-08-07 00:25:56 +0000879}