blob: 854b172422b810139072f2b1f36c33f9a2b3b189 [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 Pateld662ace2007-08-22 18:27:01 +0000106 /// safeSplitCondition - Return true if it is possible to
107 /// split loop using given split condition.
108 bool safeSplitCondition(SplitInfo &SD);
109
110 /// splitLoop - Split current loop L in two loops using split information
111 /// SD. Update dominator information. Maintain LCSSA form.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000112 bool splitLoop(SplitInfo &SD);
Devang Patelbc5fe632007-08-07 00:25:56 +0000113
Devang Patel61571ca2007-08-10 00:33:50 +0000114 void initialize() {
115 IndVar = NULL;
116 IndVarIncrement = NULL;
117 ExitCondition = NULL;
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000118 StartValue = NULL;
119 ExitValueNum = 0;
120 SplitData.clear();
Devang Patel61571ca2007-08-10 00:33:50 +0000121 }
122
Devang Patelbc5fe632007-08-07 00:25:56 +0000123 private:
124
125 // Current Loop.
126 Loop *L;
Devang Patel901f67e2007-08-10 18:07:13 +0000127 LPPassManager *LPM;
128 LoopInfo *LI;
Devang Patelbc5fe632007-08-07 00:25:56 +0000129 ScalarEvolution *SE;
Devang Patel0aaeb172007-08-08 22:25:28 +0000130 DominatorTree *DT;
Devang Patelb7639612007-08-13 22:13:24 +0000131 DominanceFrontier *DF;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000132 SmallVector<SplitInfo, 4> SplitData;
Devang Patel61571ca2007-08-10 00:33:50 +0000133
134 // Induction variable whose range is being split by this transformation.
135 PHINode *IndVar;
136 Instruction *IndVarIncrement;
137
138 // Loop exit condition.
139 ICmpInst *ExitCondition;
140
141 // Induction variable's initial value.
142 Value *StartValue;
143
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000144 // Induction variable's final loop exit value operand number in exit condition..
145 unsigned ExitValueNum;
Devang Patelbc5fe632007-08-07 00:25:56 +0000146 };
147
148 char LoopIndexSplit::ID = 0;
149 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
150}
151
152LoopPass *llvm::createLoopIndexSplitPass() {
153 return new LoopIndexSplit();
154}
155
156// Index split Loop L. Return true if loop is split.
Devang Patel901f67e2007-08-10 18:07:13 +0000157bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000158 bool Changed = false;
159 L = IncomingLoop;
Devang Patel901f67e2007-08-10 18:07:13 +0000160 LPM = &LPM_Ref;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000161
Devang Patel81fcdfb2007-08-15 02:14:55 +0000162 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel79276b32007-08-14 23:53:57 +0000163 if (!L->getSubLoops().empty())
164 return false;
165
Devang Patelbc5fe632007-08-07 00:25:56 +0000166 SE = &getAnalysis<ScalarEvolution>();
Devang Patel0aaeb172007-08-08 22:25:28 +0000167 DT = &getAnalysis<DominatorTree>();
Devang Patel901f67e2007-08-10 18:07:13 +0000168 LI = &getAnalysis<LoopInfo>();
Devang Patel2190f172007-08-15 03:34:53 +0000169 DF = &getAnalysis<DominanceFrontier>();
Devang Patelbc5fe632007-08-07 00:25:56 +0000170
Devang Patel61571ca2007-08-10 00:33:50 +0000171 initialize();
172
173 findLoopConditionals();
174
175 if (!ExitCondition)
176 return false;
177
Devang Patelbc5fe632007-08-07 00:25:56 +0000178 findSplitCondition();
179
Devang Patelc8dadbf2007-08-08 21:02:17 +0000180 if (SplitData.empty())
Devang Patelbc5fe632007-08-07 00:25:56 +0000181 return false;
182
Devang Patelc8dadbf2007-08-08 21:02:17 +0000183 // First see if it is possible to eliminate loop itself or not.
184 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Devang Patel49fbf5a2007-08-20 20:24:15 +0000185 E = SplitData.end(); SI != E;) {
Devang Patelc8dadbf2007-08-08 21:02:17 +0000186 SplitInfo &SD = *SI;
187 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
Devang Patel901f67e2007-08-10 18:07:13 +0000188 Changed = processOneIterationLoop(SD);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000189 if (Changed) {
190 ++NumIndexSplit;
191 // If is loop is eliminated then nothing else to do here.
192 return Changed;
Devang Patel49fbf5a2007-08-20 20:24:15 +0000193 } else {
194 SmallVector<SplitInfo, 4>::iterator Delete_SI = SI;
195 ++SI;
196 SplitData.erase(Delete_SI);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000197 }
Devang Patel49fbf5a2007-08-20 20:24:15 +0000198 } else
199 ++SI;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000200 }
201
Devang Patel0aaeb172007-08-08 22:25:28 +0000202 // Split most profitiable condition.
Devang Patel33085702007-08-24 05:21:13 +0000203 // FIXME : Implement cost analysis.
204 unsigned MostProfitableSDIndex = 0;
205 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
Devang Patel0aaeb172007-08-08 22:25:28 +0000206
Devang Patelbc5fe632007-08-07 00:25:56 +0000207 if (Changed)
208 ++NumIndexSplit;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000209
Devang Patelbc5fe632007-08-07 00:25:56 +0000210 return Changed;
211}
212
Devang Patel2545f7b2007-08-09 01:39:01 +0000213/// Return true if V is a induction variable or induction variable's
214/// increment for loop L.
Devang Patel61571ca2007-08-10 00:33:50 +0000215void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000216
217 Instruction *I = dyn_cast<Instruction>(V);
218 if (!I)
Devang Patel61571ca2007-08-10 00:33:50 +0000219 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000220
221 // Check if I is a phi node from loop header or not.
222 if (PHINode *PN = dyn_cast<PHINode>(V)) {
223 if (PN->getParent() == L->getHeader()) {
Devang Patel61571ca2007-08-10 00:33:50 +0000224 IndVar = PN;
225 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000226 }
227 }
228
229 // Check if I is a add instruction whose one operand is
230 // phi node from loop header and second operand is constant.
231 if (I->getOpcode() != Instruction::Add)
Devang Patel61571ca2007-08-10 00:33:50 +0000232 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000233
234 Value *Op0 = I->getOperand(0);
235 Value *Op1 = I->getOperand(1);
236
237 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
238 if (PN->getParent() == L->getHeader()
239 && isa<ConstantInt>(Op1)) {
240 IndVar = PN;
241 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000242 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000243 }
244 }
245
246 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
247 if (PN->getParent() == L->getHeader()
248 && isa<ConstantInt>(Op0)) {
249 IndVar = PN;
250 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000251 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000252 }
253 }
254
Devang Patel61571ca2007-08-10 00:33:50 +0000255 return;
256}
257
258// Find loop's exit condition and associated induction variable.
259void LoopIndexSplit::findLoopConditionals() {
260
Devang Patel9263fc32007-08-20 23:51:18 +0000261 BasicBlock *ExitingBlock = NULL;
Devang Patel61571ca2007-08-10 00:33:50 +0000262
263 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
264 I != E; ++I) {
265 BasicBlock *BB = *I;
266 if (!L->isLoopExit(BB))
267 continue;
Devang Patel9263fc32007-08-20 23:51:18 +0000268 if (ExitingBlock)
Devang Patel61571ca2007-08-10 00:33:50 +0000269 return;
Devang Patel9263fc32007-08-20 23:51:18 +0000270 ExitingBlock = BB;
Devang Patel61571ca2007-08-10 00:33:50 +0000271 }
272
Devang Patel9263fc32007-08-20 23:51:18 +0000273 if (!ExitingBlock)
Devang Patel61571ca2007-08-10 00:33:50 +0000274 return;
275
276 // If exit block's terminator is conditional branch inst then we have found
277 // exit condition.
Devang Patel9263fc32007-08-20 23:51:18 +0000278 BranchInst *BR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patel61571ca2007-08-10 00:33:50 +0000279 if (!BR || BR->isUnconditional())
280 return;
281
282 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
283 if (!CI)
284 return;
285
286 ExitCondition = CI;
287
288 // Exit condition's one operand is loop invariant exit value and second
289 // operand is SCEVAddRecExpr based on induction variable.
290 Value *V0 = CI->getOperand(0);
291 Value *V1 = CI->getOperand(1);
292
293 SCEVHandle SH0 = SE->getSCEV(V0);
294 SCEVHandle SH1 = SE->getSCEV(V1);
295
296 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000297 ExitValueNum = 0;
Devang Patel61571ca2007-08-10 00:33:50 +0000298 findIndVar(V1, L);
299 }
300 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000301 ExitValueNum = 1;
Devang Patel61571ca2007-08-10 00:33:50 +0000302 findIndVar(V0, L);
303 }
304
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000305 if (!IndVar)
Devang Patel61571ca2007-08-10 00:33:50 +0000306 ExitCondition = NULL;
307 else if (IndVar) {
308 BasicBlock *Preheader = L->getLoopPreheader();
309 StartValue = IndVar->getIncomingValueForBlock(Preheader);
310 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000311}
312
Devang Patelbc5fe632007-08-07 00:25:56 +0000313/// Find condition inside a loop that is suitable candidate for index split.
314void LoopIndexSplit::findSplitCondition() {
315
Devang Patelc8dadbf2007-08-08 21:02:17 +0000316 SplitInfo SD;
Devang Patel2545f7b2007-08-09 01:39:01 +0000317 // Check all basic block's terminators.
Devang Patelbc5fe632007-08-07 00:25:56 +0000318
Devang Patel2545f7b2007-08-09 01:39:01 +0000319 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
320 I != E; ++I) {
321 BasicBlock *BB = *I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000322
Devang Patel2545f7b2007-08-09 01:39:01 +0000323 // If this basic block does not terminate in a conditional branch
324 // then terminator is not a suitable split condition.
325 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
326 if (!BR)
327 continue;
328
329 if (BR->isUnconditional())
Devang Patelbc5fe632007-08-07 00:25:56 +0000330 continue;
331
Devang Patel2545f7b2007-08-09 01:39:01 +0000332 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
Devang Patel61571ca2007-08-10 00:33:50 +0000333 if (!CI || CI == ExitCondition)
Devang Patel2545f7b2007-08-09 01:39:01 +0000334 return;
Devang Patelbc5fe632007-08-07 00:25:56 +0000335
Devang Patel2545f7b2007-08-09 01:39:01 +0000336 // If one operand is loop invariant and second operand is SCEVAddRecExpr
337 // based on induction variable then CI is a candidate split condition.
338 Value *V0 = CI->getOperand(0);
339 Value *V1 = CI->getOperand(1);
340
341 SCEVHandle SH0 = SE->getSCEV(V0);
342 SCEVHandle SH1 = SE->getSCEV(V1);
343
344 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
345 SD.SplitValue = V0;
346 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000347 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
348 if (PN == IndVar)
349 SplitData.push_back(SD);
350 }
351 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
352 if (IndVarIncrement && IndVarIncrement == Insn)
353 SplitData.push_back(SD);
354 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000355 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000356 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
357 SD.SplitValue = V1;
358 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000359 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
360 if (PN == IndVar)
361 SplitData.push_back(SD);
362 }
363 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
364 if (IndVarIncrement && IndVarIncrement == Insn)
365 SplitData.push_back(SD);
366 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000367 }
368 }
369}
370
371/// processOneIterationLoop - Current loop L contains compare instruction
372/// that compares induction variable, IndVar, against loop invariant. If
373/// entire (i.e. meaningful) loop body is dominated by this compare
374/// instruction then loop body is executed only once. In such case eliminate
375/// loop structure surrounding this loop body. For example,
376/// for (int i = start; i < end; ++i) {
377/// if ( i == somevalue) {
378/// loop_body
379/// }
380/// }
381/// can be transformed into
382/// if (somevalue >= start && somevalue < end) {
383/// i = somevalue;
384/// loop_body
385/// }
Devang Patel901f67e2007-08-10 18:07:13 +0000386bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000387
388 BasicBlock *Header = L->getHeader();
389
390 // First of all, check if SplitCondition dominates entire loop body
391 // or not.
392
393 // If SplitCondition is not in loop header then this loop is not suitable
394 // for this transformation.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000395 if (SD.SplitCondition->getParent() != Header)
Devang Patelbc5fe632007-08-07 00:25:56 +0000396 return false;
397
Devang Patelbc5fe632007-08-07 00:25:56 +0000398 // If loop header includes loop variant instruction operands then
399 // this loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000400 if (!safeHeader(SD, Header))
Devang Patelbc5fe632007-08-07 00:25:56 +0000401 return false;
402
Devang Patel9263fc32007-08-20 23:51:18 +0000403 // If Exiting block includes loop variant instructions then this
Devang Patelbc5fe632007-08-07 00:25:56 +0000404 // loop may not be eliminated.
Devang Patel9263fc32007-08-20 23:51:18 +0000405 if (!safeExitingBlock(SD, ExitCondition->getParent()))
Devang Patelbc5fe632007-08-07 00:25:56 +0000406 return false;
407
Devang Patel2bcb5012007-08-08 01:51:27 +0000408 // Update CFG.
409
Devang Patelc166b952007-08-20 20:49:01 +0000410 // Replace index variable with split value in loop body. Loop body is executed
411 // only when index variable is equal to split value.
412 IndVar->replaceAllUsesWith(SD.SplitValue);
413
414 // Remove Latch to Header edge.
Devang Patelbc5fe632007-08-07 00:25:56 +0000415 BasicBlock *Latch = L->getLoopLatch();
Devang Patel2bcb5012007-08-08 01:51:27 +0000416 BasicBlock *LatchSucc = NULL;
417 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
418 if (!BR)
419 return false;
420 Header->removePredecessor(Latch);
421 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
422 SI != E; ++SI) {
423 if (Header != *SI)
424 LatchSucc = *SI;
425 }
426 BR->setUnconditionalDest(LatchSucc);
427
Devang Patelbc5fe632007-08-07 00:25:56 +0000428 Instruction *Terminator = Header->getTerminator();
Devang Patel59e0c062007-08-14 01:30:57 +0000429 Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
Devang Patelbc5fe632007-08-07 00:25:56 +0000430
Devang Patelbc5fe632007-08-07 00:25:56 +0000431 // Replace split condition in header.
432 // Transform
433 // SplitCondition : icmp eq i32 IndVar, SplitValue
434 // into
435 // c1 = icmp uge i32 SplitValue, StartValue
436 // c2 = icmp ult i32 vSplitValue, ExitValue
437 // and i32 c1, c2
Devang Patel61571ca2007-08-10 00:33:50 +0000438 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patelbc5fe632007-08-07 00:25:56 +0000439 Instruction *C1 = new ICmpInst(SignedPredicate ?
440 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Devang Patelc8dadbf2007-08-08 21:02:17 +0000441 SD.SplitValue, StartValue, "lisplit",
442 Terminator);
Devang Patelbc5fe632007-08-07 00:25:56 +0000443 Instruction *C2 = new ICmpInst(SignedPredicate ?
444 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel59e0c062007-08-14 01:30:57 +0000445 SD.SplitValue, ExitValue, "lisplit",
Devang Patelc8dadbf2007-08-08 21:02:17 +0000446 Terminator);
447 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
448 Terminator);
449 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
450 SD.SplitCondition->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000451
Devang Patelbc5fe632007-08-07 00:25:56 +0000452 // Now, clear latch block. Remove instructions that are responsible
453 // to increment induction variable.
454 Instruction *LTerminator = Latch->getTerminator();
455 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
456 LB != LE; ) {
457 Instruction *I = LB;
458 ++LB;
459 if (isa<PHINode>(I) || I == LTerminator)
460 continue;
461
Devang Patel59e0c062007-08-14 01:30:57 +0000462 if (I == IndVarIncrement)
463 I->replaceAllUsesWith(ExitValue);
464 else
465 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Devang Patel0d75c292007-08-07 17:45:35 +0000466 I->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000467 }
468
Devang Patel901f67e2007-08-10 18:07:13 +0000469 LPM->deleteLoopFromQueue(L);
Devang Patel95fd7172007-08-08 21:39:47 +0000470
471 // Update Dominator Info.
472 // Only CFG change done is to remove Latch to Header edge. This
473 // does not change dominator tree because Latch did not dominate
474 // Header.
Devang Patelb7639612007-08-13 22:13:24 +0000475 if (DF) {
Devang Patel95fd7172007-08-08 21:39:47 +0000476 DominanceFrontier::iterator HeaderDF = DF->find(Header);
477 if (HeaderDF != DF->end())
478 DF->removeFromFrontier(HeaderDF, Header);
479
480 DominanceFrontier::iterator LatchDF = DF->find(Latch);
481 if (LatchDF != DF->end())
482 DF->removeFromFrontier(LatchDF, Header);
483 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000484 return true;
485}
486
487// If loop header includes loop variant instruction operands then
488// this loop can not be eliminated. This is used by processOneIterationLoop().
Devang Patelc8dadbf2007-08-08 21:02:17 +0000489bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000490
491 Instruction *Terminator = Header->getTerminator();
492 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
493 BI != BE; ++BI) {
494 Instruction *I = BI;
495
Devang Patel59e0c062007-08-14 01:30:57 +0000496 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000497 if (isa<PHINode>(I))
498 continue;
499
500 // SplitCondition itself is OK.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000501 if (I == SD.SplitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000502 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000503
Devang Patel2545f7b2007-08-09 01:39:01 +0000504 // Induction variable is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000505 if (I == IndVar)
Devang Patel2545f7b2007-08-09 01:39:01 +0000506 continue;
507
508 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000509 if (I == IndVarIncrement)
Devang Patel2545f7b2007-08-09 01:39:01 +0000510 continue;
511
Devang Patelbc5fe632007-08-07 00:25:56 +0000512 // Terminator is also harmless.
513 if (I == Terminator)
514 continue;
515
516 // Otherwise we have a instruction that may not be safe.
517 return false;
518 }
519
520 return true;
521}
522
Devang Patel9263fc32007-08-20 23:51:18 +0000523// If Exiting block includes loop variant instructions then this
Devang Patelbc5fe632007-08-07 00:25:56 +0000524// loop may not be eliminated. This is used by processOneIterationLoop().
Devang Patel9263fc32007-08-20 23:51:18 +0000525bool LoopIndexSplit::safeExitingBlock(SplitInfo &SD,
526 BasicBlock *ExitingBlock) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000527
Devang Patel9263fc32007-08-20 23:51:18 +0000528 for (BasicBlock::iterator BI = ExitingBlock->begin(),
529 BE = ExitingBlock->end(); BI != BE; ++BI) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000530 Instruction *I = BI;
531
Devang Patel59e0c062007-08-14 01:30:57 +0000532 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000533 if (isa<PHINode>(I))
534 continue;
535
Devang Patel2545f7b2007-08-09 01:39:01 +0000536 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000537 if (IndVarIncrement && IndVarIncrement == I)
Devang Patel2545f7b2007-08-09 01:39:01 +0000538 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000539
Devang Patel2545f7b2007-08-09 01:39:01 +0000540 // Check if I is induction variable increment instruction.
Devang Patel61571ca2007-08-10 00:33:50 +0000541 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000542
543 Value *Op0 = I->getOperand(0);
544 Value *Op1 = I->getOperand(1);
Devang Patelbc5fe632007-08-07 00:25:56 +0000545 PHINode *PN = NULL;
546 ConstantInt *CI = NULL;
547
548 if ((PN = dyn_cast<PHINode>(Op0))) {
549 if ((CI = dyn_cast<ConstantInt>(Op1)))
Devang Patel61571ca2007-08-10 00:33:50 +0000550 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000551 } else
552 if ((PN = dyn_cast<PHINode>(Op1))) {
553 if ((CI = dyn_cast<ConstantInt>(Op0)))
Devang Patel61571ca2007-08-10 00:33:50 +0000554 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000555 }
556
Devang Patel61571ca2007-08-10 00:33:50 +0000557 if (IndVarIncrement && PN == IndVar && CI->isOne())
Devang Patelbc5fe632007-08-07 00:25:56 +0000558 continue;
559 }
Devang Patel2bcb5012007-08-08 01:51:27 +0000560
Devang Patelbc5fe632007-08-07 00:25:56 +0000561 // I is an Exit condition if next instruction is block terminator.
562 // Exit condition is OK if it compares loop invariant exit value,
563 // which is checked below.
Devang Patel3719d4f2007-08-07 23:17:52 +0000564 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
Devang Patel61571ca2007-08-10 00:33:50 +0000565 if (EC == ExitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000566 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000567 }
568
Devang Patel9263fc32007-08-20 23:51:18 +0000569 if (I == ExitingBlock->getTerminator())
Devang Patel61571ca2007-08-10 00:33:50 +0000570 continue;
571
Devang Patelbc5fe632007-08-07 00:25:56 +0000572 // Otherwise we have instruction that may not be safe.
573 return false;
574 }
575
Devang Patel9263fc32007-08-20 23:51:18 +0000576 // We could not find any reason to consider ExitingBlock unsafe.
Devang Patelbc5fe632007-08-07 00:25:56 +0000577 return true;
578}
579
Devang Patel60a94c72007-08-14 18:35:57 +0000580/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
581/// This routine is used to remove split condition's dead branch, dominated by
582/// DeadBB. LiveBB dominates split conidition's other branch.
583void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
584 BasicBlock *LiveBB) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000585
Devang Patelf4277122007-08-15 03:31:47 +0000586 // First update DeadBB's dominance frontier.
Devang Patel9cee7a02007-08-17 21:59:16 +0000587 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patelf4277122007-08-15 03:31:47 +0000588 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
589 if (DeadBBDF != DF->end()) {
590 SmallVector<BasicBlock *, 8> PredBlocks;
591
592 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
593 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
594 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) {
595 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel9cee7a02007-08-17 21:59:16 +0000596 FrontierBBs.push_back(FrontierBB);
597
Devang Patelf4277122007-08-15 03:31:47 +0000598 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
599 PredBlocks.clear();
600 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
601 PI != PE; ++PI) {
602 BasicBlock *P = *PI;
603 if (P == DeadBB || DT->dominates(DeadBB, P))
604 PredBlocks.push_back(P);
Devang Patelb7639612007-08-13 22:13:24 +0000605 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000606
Devang Patelf4277122007-08-15 03:31:47 +0000607 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
608 FBI != FBE; ++FBI) {
609 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
610 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
611 PE = PredBlocks.end(); PI != PE; ++PI) {
612 BasicBlock *P = *PI;
613 PN->removeIncomingValue(P);
614 }
615 }
616 else
617 break;
Devang Patel9cee7a02007-08-17 21:59:16 +0000618 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000619 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000620 }
Devang Patelf4277122007-08-15 03:31:47 +0000621
622 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
623 SmallVector<BasicBlock *, 32> WorkList;
624 DomTreeNode *DN = DT->getNode(DeadBB);
625 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
626 E = df_end(DN); DI != E; ++DI) {
627 BasicBlock *BB = DI->getBlock();
628 WorkList.push_back(BB);
Devang Patel9cee7a02007-08-17 21:59:16 +0000629 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
Devang Patelf4277122007-08-15 03:31:47 +0000630 }
631
632 while (!WorkList.empty()) {
633 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
634 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
635 BBI != BBE; ++BBI) {
636 Instruction *I = BBI;
637 I->replaceAllUsesWith(UndefValue::get(I->getType()));
638 I->eraseFromParent();
639 }
640 LPM->deleteSimpleAnalysisValue(BB, LP);
641 DT->eraseNode(BB);
642 DF->removeBlock(BB);
643 LI->removeBlock(BB);
644 BB->eraseFromParent();
645 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000646
647 // Update Frontier BBs' dominator info.
648 while (!FrontierBBs.empty()) {
649 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
650 BasicBlock *NewDominator = FBB->getSinglePredecessor();
651 if (!NewDominator) {
652 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
653 NewDominator = *PI;
654 ++PI;
655 if (NewDominator != LiveBB) {
656 for(; PI != PE; ++PI) {
657 BasicBlock *P = *PI;
658 if (P == LiveBB) {
659 NewDominator = LiveBB;
660 break;
661 }
662 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
663 }
664 }
665 }
666 assert (NewDominator && "Unable to fix dominator info.");
667 DT->changeImmediateDominator(FBB, NewDominator);
668 DF->changeImmediateDominator(FBB, NewDominator, DT);
669 }
670
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000671}
672
Devang Pateld662ace2007-08-22 18:27:01 +0000673/// safeSplitCondition - Return true if it is possible to
674/// split loop using given split condition.
675bool LoopIndexSplit::safeSplitCondition(SplitInfo &SD) {
Devang Patelf824fb42007-08-10 00:53:35 +0000676
Devang Pateld662ace2007-08-22 18:27:01 +0000677 BasicBlock *SplitCondBlock = SD.SplitCondition->getParent();
Devang Patel2a24ff32007-08-21 21:12:02 +0000678
Devang Pateld662ace2007-08-22 18:27:01 +0000679 // Unable to handle triange loops at the moment.
Devang Patel81fcdfb2007-08-15 02:14:55 +0000680 // In triangle loop, split condition is in header and one of the
681 // the split destination is loop latch. If split condition is EQ
682 // then such loops are already handle in processOneIterationLoop().
Devang Pateld662ace2007-08-22 18:27:01 +0000683 BasicBlock *Latch = L->getLoopLatch();
684 BranchInst *SplitTerminator =
685 cast<BranchInst>(SplitCondBlock->getTerminator());
686 BasicBlock *Succ0 = SplitTerminator->getSuccessor(0);
687 BasicBlock *Succ1 = SplitTerminator->getSuccessor(1);
688 if (L->getHeader() == SplitCondBlock
689 && (Latch == Succ0 || Latch == Succ1))
Devang Patel81fcdfb2007-08-15 02:14:55 +0000690 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000691
Devang Patel9cba64e2007-08-18 00:00:32 +0000692 // If one of the split condition branch is post dominating other then loop
693 // index split is not appropriate.
Devang Pateld662ace2007-08-22 18:27:01 +0000694 if (DT->dominates(Succ0, Latch) || DT->dominates(Succ1, Latch))
Devang Patel9cee7a02007-08-17 21:59:16 +0000695 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000696
Devang Patel9cba64e2007-08-18 00:00:32 +0000697 // If one of the split condition branch is a predecessor of the other
698 // split condition branch head then do not split loop on this condition.
Devang Patel2a24ff32007-08-21 21:12:02 +0000699 for(pred_iterator PI = pred_begin(Succ0), PE = pred_end(Succ0);
700 PI != PE; ++PI)
Devang Patel9cba64e2007-08-18 00:00:32 +0000701 if (Succ1 == *PI)
702 return false;
Devang Patel2a24ff32007-08-21 21:12:02 +0000703 for(pred_iterator PI = pred_begin(Succ1), PE = pred_end(Succ1);
704 PI != PE; ++PI)
Devang Patel9cba64e2007-08-18 00:00:32 +0000705 if (Succ0 == *PI)
706 return false;
707
Devang Pateld662ace2007-08-22 18:27:01 +0000708 return true;
709}
710
711/// splitLoop - Split current loop L in two loops using split information
712/// SD. Update dominator information. Maintain LCSSA form.
713bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
714
715 if (!safeSplitCondition(SD))
716 return false;
717
Devang Patela0ac7262007-08-22 19:33:29 +0000718 // After loop is cloned there are two loops.
719 //
720 // First loop, referred as ALoop, executes first part of loop's iteration
721 // space split. Second loop, referred as BLoop, executes remaining
722 // part of loop's iteration space.
723 //
724 // ALoop's exit edge enters BLoop's header through a forwarding block which
725 // acts as a BLoop's preheader.
Devang Pateld662ace2007-08-22 18:27:01 +0000726
Devang Patela0ac7262007-08-22 19:33:29 +0000727 //[*] Calculate ALoop induction variable's new exiting value and
728 // BLoop induction variable's new starting value. Calculuate these
729 // values in original loop's preheader.
730 // A_ExitValue = min(SplitValue, OrignalLoopExitValue)
731 // B_StartValue = max(SplitValue, OriginalLoopStartValue)
732 Value *A_ExitValue = NULL;
733 Value *B_StartValue = NULL;
Devang Patelf824fb42007-08-10 00:53:35 +0000734 if (isa<ConstantInt>(SD.SplitValue)) {
Devang Patela0ac7262007-08-22 19:33:29 +0000735 A_ExitValue = SD.SplitValue;
736 B_StartValue = SD.SplitValue;
Devang Patelf824fb42007-08-10 00:53:35 +0000737 }
738 else {
Devang Patela0ac7262007-08-22 19:33:29 +0000739 BasicBlock *Preheader = L->getLoopPreheader();
740 Instruction *PHTerminator = Preheader->getTerminator();
741 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patelf824fb42007-08-10 00:53:35 +0000742 Value *C1 = new ICmpInst(SignedPredicate ?
743 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000744 SD.SplitValue,
745 ExitCondition->getOperand(ExitValueNum),
Devang Patela0ac7262007-08-22 19:33:29 +0000746 "lsplit.ev", PHTerminator);
747 A_ExitValue = new SelectInst(C1, SD.SplitValue,
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000748 ExitCondition->getOperand(ExitValueNum),
Devang Patela0ac7262007-08-22 19:33:29 +0000749 "lsplit.ev", PHTerminator);
Devang Patelf824fb42007-08-10 00:53:35 +0000750
751 Value *C2 = new ICmpInst(SignedPredicate ?
752 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
753 SD.SplitValue, StartValue, "lsplit.sv",
Devang Patela0ac7262007-08-22 19:33:29 +0000754 PHTerminator);
755 B_StartValue = new SelectInst(C2, StartValue, SD.SplitValue,
756 "lsplit.sv", PHTerminator);
Devang Patelf824fb42007-08-10 00:53:35 +0000757 }
Devang Patel901f67e2007-08-10 18:07:13 +0000758
Devang Patela0ac7262007-08-22 19:33:29 +0000759 //[*] Clone loop.
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000760 DenseMap<const Value *, Value *> ValueMap;
Devang Patela0ac7262007-08-22 19:33:29 +0000761 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
762 BasicBlock *B_Header = BLoop->getHeader();
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000763
Devang Patela0ac7262007-08-22 19:33:29 +0000764 //[*] ALoop's exiting edge BLoop's header.
765 // ALoop's original exit block becomes BLoop's exit block.
766 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
767 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
768 BranchInst *A_ExitInsn =
769 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
770 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
771 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
772 if (L->contains(B_ExitBlock)) {
773 B_ExitBlock = A_ExitInsn->getSuccessor(0);
774 A_ExitInsn->setSuccessor(0, B_Header);
Devang Patel59e0c062007-08-14 01:30:57 +0000775 } else
Devang Patela0ac7262007-08-22 19:33:29 +0000776 A_ExitInsn->setSuccessor(1, B_Header);
777
778 //[*] Update ALoop's exit value using new exit value.
779 ExitCondition->setOperand(ExitValueNum, A_ExitValue);
Devang Patel2a24ff32007-08-21 21:12:02 +0000780
Devang Patela0ac7262007-08-22 19:33:29 +0000781 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
782 // original loop's preheader. Add incoming PHINode values from
783 // ALoop's exiting block. Update BLoop header's domiantor info.
784
Devang Patel59e0c062007-08-14 01:30:57 +0000785 // Collect inverse map of Header PHINodes.
786 DenseMap<Value *, Value *> InverseMap;
787 for (BasicBlock::iterator BI = L->getHeader()->begin(),
788 BE = L->getHeader()->end(); BI != BE; ++BI) {
789 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
790 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
791 InverseMap[PNClone] = PN;
792 } else
793 break;
794 }
Devang Patela0ac7262007-08-22 19:33:29 +0000795 BasicBlock *Preheader = L->getLoopPreheader();
796 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000797 BI != BE; ++BI) {
798 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela0ac7262007-08-22 19:33:29 +0000799 // Remove incoming value from original preheader.
800 PN->removeIncomingValue(Preheader);
801
802 // Add incoming value from A_ExitingBlock.
803 if (PN == B_IndVar)
804 PN->addIncoming(B_StartValue, A_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000805 else {
806 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
Devang Patela0ac7262007-08-22 19:33:29 +0000807 Value *V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
808 PN->addIncoming(V2, A_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000809 }
810 } else
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000811 break;
812 }
Devang Patela0ac7262007-08-22 19:33:29 +0000813 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
814 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
Devang Patel2a24ff32007-08-21 21:12:02 +0000815
Devang Patela0ac7262007-08-22 19:33:29 +0000816 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
817 // block. Remove incoming PHINode values from ALoop's exiting block.
818 // Add new incoming values from BLoop's incoming exiting value.
819 // Update BLoop exit block's dominator info..
820 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
821 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
Devang Patel59e0c062007-08-14 01:30:57 +0000822 BI != BE; ++BI) {
823 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela0ac7262007-08-22 19:33:29 +0000824 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
825 B_ExitingBlock);
826 PN->removeIncomingValue(A_ExitingBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000827 } else
828 break;
829 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000830
Devang Patela0ac7262007-08-22 19:33:29 +0000831 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
832 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
Devang Patelb7639612007-08-13 22:13:24 +0000833
Devang Patela0ac7262007-08-22 19:33:29 +0000834 //[*] Split ALoop's exit edge. This creates a new block which
835 // serves two purposes. First one is to hold PHINode defnitions
836 // to ensure that ALoop's LCSSA form. Second use it to act
837 // as a preheader for BLoop.
838 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
Devang Patel901f67e2007-08-10 18:07:13 +0000839
Devang Patela0ac7262007-08-22 19:33:29 +0000840 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
841 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
842 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
Devang Patel7ef89b82007-08-21 19:47:46 +0000843 BI != BE; ++BI) {
844 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
Devang Patela0ac7262007-08-22 19:33:29 +0000845 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
Devang Patel7ef89b82007-08-21 19:47:46 +0000846 PHINode *newPHI = new PHINode(PN->getType(), PN->getName());
Devang Patela0ac7262007-08-22 19:33:29 +0000847 newPHI->addIncoming(V1, A_ExitingBlock);
848 A_ExitBlock->getInstList().push_front(newPHI);
849 PN->removeIncomingValue(A_ExitBlock);
850 PN->addIncoming(newPHI, A_ExitBlock);
Devang Patel7ef89b82007-08-21 19:47:46 +0000851 } else
852 break;
853 }
854
Devang Patela0ac7262007-08-22 19:33:29 +0000855 //[*] Eliminate split condition's inactive branch from ALoop.
856 BasicBlock *A_SplitCondBlock = SD.SplitCondition->getParent();
857 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
858 BasicBlock *A_InactiveBranch = A_BR->getSuccessor(1);
Devang Patel45db4bc2007-08-22 20:55:18 +0000859 BasicBlock *A_ActiveBranch = A_BR->getSuccessor(0);
Devang Patela0ac7262007-08-22 19:33:29 +0000860 A_BR->setUnconditionalDest(A_BR->getSuccessor(0));
861 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
862
863 //[*] Eliminate split condition's inactive branch in from BLoop.
864 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
865 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
866 BasicBlock *B_InactiveBranch = B_BR->getSuccessor(0);
867 BasicBlock *B_ActiveBranch = B_BR->getSuccessor(1);
868 B_BR->setUnconditionalDest(B_BR->getSuccessor(1));
869 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
870
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000871 return true;
Devang Patelbc5fe632007-08-07 00:25:56 +0000872}