1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
//===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Devang Patel and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements Loop Index Splitting Pass.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "loop-index-split"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Function.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Support/Compiler.h"
#include "llvm/ADT/Statistic.h"
using namespace llvm;
STATISTIC(NumIndexSplit, "Number of loops index split");
namespace {
class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
public:
static char ID; // Pass ID, replacement for typeid
LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
// Index split Loop L. Return true if loop is split.
bool runOnLoop(Loop *L, LPPassManager &LPM);
void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<ScalarEvolution>();
AU.addPreserved<ScalarEvolution>();
AU.addRequiredID(LCSSAID);
AU.addPreservedID(LCSSAID);
AU.addRequired<LoopInfo>();
AU.addPreserved<LoopInfo>();
AU.addRequiredID(LoopSimplifyID);
AU.addPreservedID(LoopSimplifyID);
AU.addRequired<DominatorTree>();
AU.addPreserved<DominatorTree>();
AU.addPreserved<DominanceFrontier>();
}
private:
class SplitInfo {
public:
SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {}
// Induction variable's range is split at this value.
Value *SplitValue;
// This compare instruction compares IndVar against SplitValue.
ICmpInst *SplitCondition;
// Clear split info.
void clear() {
SplitValue = NULL;
SplitCondition = NULL;
}
};
private:
/// Find condition inside a loop that is suitable candidate for index split.
void findSplitCondition();
/// Find loop's exit condition.
void findLoopConditionals();
/// Return induction variable associated with value V.
void findIndVar(Value *V, Loop *L);
/// processOneIterationLoop - Current loop L contains compare instruction
/// that compares induction variable, IndVar, agains loop invariant. If
/// entire (i.e. meaningful) loop body is dominated by this compare
/// instruction then loop body is executed only for one iteration. In
/// such case eliminate loop structure surrounding this loop body. For
bool processOneIterationLoop(SplitInfo &SD);
/// If loop header includes loop variant instruction operands then
/// this loop may not be eliminated.
bool safeHeader(SplitInfo &SD, BasicBlock *BB);
/// If Exit block includes loop variant instructions then this
/// loop may not be eliminated.
bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
/// removeBlocks - Remove basic block BB and all blocks dominated by BB.
void removeBlocks(BasicBlock *InBB, Loop *LP);
/// Find cost of spliting loop L.
unsigned findSplitCost(Loop *L, SplitInfo &SD);
bool splitLoop(SplitInfo &SD);
void initialize() {
IndVar = NULL;
IndVarIncrement = NULL;
ExitCondition = NULL;
StartValue = NULL;
ExitValueNum = 0;
SplitData.clear();
}
private:
// Current Loop.
Loop *L;
LPPassManager *LPM;
LoopInfo *LI;
ScalarEvolution *SE;
DominatorTree *DT;
DominanceFrontier *DF;
SmallVector<SplitInfo, 4> SplitData;
// Induction variable whose range is being split by this transformation.
PHINode *IndVar;
Instruction *IndVarIncrement;
// Loop exit condition.
ICmpInst *ExitCondition;
// Induction variable's initial value.
Value *StartValue;
// Induction variable's final loop exit value operand number in exit condition..
unsigned ExitValueNum;
};
char LoopIndexSplit::ID = 0;
RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
}
LoopPass *llvm::createLoopIndexSplitPass() {
return new LoopIndexSplit();
}
// Index split Loop L. Return true if loop is split.
bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
bool Changed = false;
L = IncomingLoop;
LPM = &LPM_Ref;
SE = &getAnalysis<ScalarEvolution>();
DT = &getAnalysis<DominatorTree>();
LI = &getAnalysis<LoopInfo>();
DF = getAnalysisToUpdate<DominanceFrontier>();
initialize();
findLoopConditionals();
if (!ExitCondition)
return false;
findSplitCondition();
if (SplitData.empty())
return false;
// First see if it is possible to eliminate loop itself or not.
for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
E = SplitData.end(); SI != E; ++SI) {
SplitInfo &SD = *SI;
if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
Changed = processOneIterationLoop(SD);
if (Changed) {
++NumIndexSplit;
// If is loop is eliminated then nothing else to do here.
return Changed;
}
}
}
unsigned MaxCost = 99;
unsigned Index = 0;
unsigned MostProfitableSDIndex = 0;
for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
E = SplitData.end(); SI != E; ++SI, ++Index) {
SplitInfo SD = *SI;
// ICM_EQs are already handled above.
if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
continue;
unsigned Cost = findSplitCost(L, SD);
if (Cost < MaxCost)
MostProfitableSDIndex = Index;
}
// Split most profitiable condition.
Changed = splitLoop(SplitData[MostProfitableSDIndex]);
if (Changed)
++NumIndexSplit;
return Changed;
}
/// Return true if V is a induction variable or induction variable's
/// increment for loop L.
void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
Instruction *I = dyn_cast<Instruction>(V);
if (!I)
return;
// Check if I is a phi node from loop header or not.
if (PHINode *PN = dyn_cast<PHINode>(V)) {
if (PN->getParent() == L->getHeader()) {
IndVar = PN;
return;
}
}
// Check if I is a add instruction whose one operand is
// phi node from loop header and second operand is constant.
if (I->getOpcode() != Instruction::Add)
return;
Value *Op0 = I->getOperand(0);
Value *Op1 = I->getOperand(1);
if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
if (PN->getParent() == L->getHeader()
&& isa<ConstantInt>(Op1)) {
IndVar = PN;
IndVarIncrement = I;
return;
}
}
if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
if (PN->getParent() == L->getHeader()
&& isa<ConstantInt>(Op0)) {
IndVar = PN;
IndVarIncrement = I;
return;
}
}
return;
}
// Find loop's exit condition and associated induction variable.
void LoopIndexSplit::findLoopConditionals() {
BasicBlock *ExitBlock = NULL;
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
I != E; ++I) {
BasicBlock *BB = *I;
if (!L->isLoopExit(BB))
continue;
if (ExitBlock)
return;
ExitBlock = BB;
}
if (!ExitBlock)
return;
// If exit block's terminator is conditional branch inst then we have found
// exit condition.
BranchInst *BR = dyn_cast<BranchInst>(ExitBlock->getTerminator());
if (!BR || BR->isUnconditional())
return;
ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
if (!CI)
return;
ExitCondition = CI;
// Exit condition's one operand is loop invariant exit value and second
// operand is SCEVAddRecExpr based on induction variable.
Value *V0 = CI->getOperand(0);
Value *V1 = CI->getOperand(1);
SCEVHandle SH0 = SE->getSCEV(V0);
SCEVHandle SH1 = SE->getSCEV(V1);
if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
ExitValueNum = 0;
findIndVar(V1, L);
}
else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
ExitValueNum = 1;
findIndVar(V0, L);
}
if (!IndVar)
ExitCondition = NULL;
else if (IndVar) {
BasicBlock *Preheader = L->getLoopPreheader();
StartValue = IndVar->getIncomingValueForBlock(Preheader);
}
}
/// Find condition inside a loop that is suitable candidate for index split.
void LoopIndexSplit::findSplitCondition() {
SplitInfo SD;
// Check all basic block's terminators.
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
I != E; ++I) {
BasicBlock *BB = *I;
// If this basic block does not terminate in a conditional branch
// then terminator is not a suitable split condition.
BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
if (!BR)
continue;
if (BR->isUnconditional())
continue;
ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
if (!CI || CI == ExitCondition)
return;
// If one operand is loop invariant and second operand is SCEVAddRecExpr
// based on induction variable then CI is a candidate split condition.
Value *V0 = CI->getOperand(0);
Value *V1 = CI->getOperand(1);
SCEVHandle SH0 = SE->getSCEV(V0);
SCEVHandle SH1 = SE->getSCEV(V1);
if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
SD.SplitValue = V0;
SD.SplitCondition = CI;
if (PHINode *PN = dyn_cast<PHINode>(V1)) {
if (PN == IndVar)
SplitData.push_back(SD);
}
else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
if (IndVarIncrement && IndVarIncrement == Insn)
SplitData.push_back(SD);
}
}
else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
SD.SplitValue = V1;
SD.SplitCondition = CI;
if (PHINode *PN = dyn_cast<PHINode>(V0)) {
if (PN == IndVar)
SplitData.push_back(SD);
}
else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
if (IndVarIncrement && IndVarIncrement == Insn)
SplitData.push_back(SD);
}
}
}
}
/// processOneIterationLoop - Current loop L contains compare instruction
/// that compares induction variable, IndVar, against loop invariant. If
/// entire (i.e. meaningful) loop body is dominated by this compare
/// instruction then loop body is executed only once. In such case eliminate
/// loop structure surrounding this loop body. For example,
/// for (int i = start; i < end; ++i) {
/// if ( i == somevalue) {
/// loop_body
/// }
/// }
/// can be transformed into
/// if (somevalue >= start && somevalue < end) {
/// i = somevalue;
/// loop_body
/// }
bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
BasicBlock *Header = L->getHeader();
// First of all, check if SplitCondition dominates entire loop body
// or not.
// If SplitCondition is not in loop header then this loop is not suitable
// for this transformation.
if (SD.SplitCondition->getParent() != Header)
return false;
// If loop header includes loop variant instruction operands then
// this loop may not be eliminated.
if (!safeHeader(SD, Header))
return false;
// If Exit block includes loop variant instructions then this
// loop may not be eliminated.
if (!safeExitBlock(SD, ExitCondition->getParent()))
return false;
// Update CFG.
// As a first step to break this loop, remove Latch to Header edge.
BasicBlock *Latch = L->getLoopLatch();
BasicBlock *LatchSucc = NULL;
BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
if (!BR)
return false;
Header->removePredecessor(Latch);
for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
SI != E; ++SI) {
if (Header != *SI)
LatchSucc = *SI;
}
BR->setUnconditionalDest(LatchSucc);
Instruction *Terminator = Header->getTerminator();
Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
// Replace split condition in header.
// Transform
// SplitCondition : icmp eq i32 IndVar, SplitValue
// into
// c1 = icmp uge i32 SplitValue, StartValue
// c2 = icmp ult i32 vSplitValue, ExitValue
// and i32 c1, c2
bool SignedPredicate = ExitCondition->isSignedPredicate();
Instruction *C1 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
SD.SplitValue, StartValue, "lisplit",
Terminator);
Instruction *C2 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
SD.SplitValue, ExitValue, "lisplit",
Terminator);
Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
Terminator);
SD.SplitCondition->replaceAllUsesWith(NSplitCond);
SD.SplitCondition->eraseFromParent();
// Now, clear latch block. Remove instructions that are responsible
// to increment induction variable.
Instruction *LTerminator = Latch->getTerminator();
for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
LB != LE; ) {
Instruction *I = LB;
++LB;
if (isa<PHINode>(I) || I == LTerminator)
continue;
if (I == IndVarIncrement)
I->replaceAllUsesWith(ExitValue);
else
I->replaceAllUsesWith(UndefValue::get(I->getType()));
I->eraseFromParent();
}
LPM->deleteLoopFromQueue(L);
// Update Dominator Info.
// Only CFG change done is to remove Latch to Header edge. This
// does not change dominator tree because Latch did not dominate
// Header.
if (DF) {
DominanceFrontier::iterator HeaderDF = DF->find(Header);
if (HeaderDF != DF->end())
DF->removeFromFrontier(HeaderDF, Header);
DominanceFrontier::iterator LatchDF = DF->find(Latch);
if (LatchDF != DF->end())
DF->removeFromFrontier(LatchDF, Header);
}
return true;
}
// If loop header includes loop variant instruction operands then
// this loop can not be eliminated. This is used by processOneIterationLoop().
bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Instruction *Terminator = Header->getTerminator();
for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
BI != BE; ++BI) {
Instruction *I = BI;
// PHI Nodes are OK.
if (isa<PHINode>(I))
continue;
// SplitCondition itself is OK.
if (I == SD.SplitCondition)
continue;
// Induction variable is OK.
if (I == IndVar)
continue;
// Induction variable increment is OK.
if (I == IndVarIncrement)
continue;
// Terminator is also harmless.
if (I == Terminator)
continue;
// Otherwise we have a instruction that may not be safe.
return false;
}
return true;
}
// If Exit block includes loop variant instructions then this
// loop may not be eliminated. This is used by processOneIterationLoop().
bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
BI != BE; ++BI) {
Instruction *I = BI;
// PHI Nodes are OK.
if (isa<PHINode>(I))
continue;
// Induction variable increment is OK.
if (IndVarIncrement && IndVarIncrement == I)
continue;
// Check if I is induction variable increment instruction.
if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
Value *Op0 = I->getOperand(0);
Value *Op1 = I->getOperand(1);
PHINode *PN = NULL;
ConstantInt *CI = NULL;
if ((PN = dyn_cast<PHINode>(Op0))) {
if ((CI = dyn_cast<ConstantInt>(Op1)))
IndVarIncrement = I;
} else
if ((PN = dyn_cast<PHINode>(Op1))) {
if ((CI = dyn_cast<ConstantInt>(Op0)))
IndVarIncrement = I;
}
if (IndVarIncrement && PN == IndVar && CI->isOne())
continue;
}
// I is an Exit condition if next instruction is block terminator.
// Exit condition is OK if it compares loop invariant exit value,
// which is checked below.
else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
if (EC == ExitCondition)
continue;
}
if (I == ExitBlock->getTerminator())
continue;
// Otherwise we have instruction that may not be safe.
return false;
}
// We could not find any reason to consider ExitBlock unsafe.
return true;
}
/// Find cost of spliting loop L. Cost is measured in terms of size growth.
/// Size is growth is calculated based on amount of code duplicated in second
/// loop.
unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
unsigned Cost = 0;
BasicBlock *SDBlock = SD.SplitCondition->getParent();
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
I != E; ++I) {
BasicBlock *BB = *I;
// If a block is not dominated by split condition block then
// it must be duplicated in both loops.
if (!DT->dominates(SDBlock, BB))
Cost += BB->size();
}
return Cost;
}
/// removeBlocks - Remove basic block BB and all blocks dominated by BB.
void LoopIndexSplit::removeBlocks(BasicBlock *InBB, Loop *LP) {
SmallVector<std::pair<BasicBlock *, succ_iterator>, 8> WorkList;
WorkList.push_back(std::make_pair(InBB, succ_begin(InBB)));
while (!WorkList.empty()) {
BasicBlock *BB = WorkList.back(). first;
succ_iterator SIter =WorkList.back().second;
// If all successor's are processed then remove this block.
if (SIter == succ_end(BB)) {
WorkList.pop_back();
for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
BBI != BBE; ++BBI) {
Instruction *I = BBI;
I->replaceAllUsesWith(UndefValue::get(I->getType()));
I->eraseFromParent();
}
LPM->deleteSimpleAnalysisValue(BB, LP);
DT->eraseNode(BB);
DF->removeBlock(BB);
LI->removeBlock(BB);
BB->eraseFromParent();
} else {
BasicBlock *SuccBB = *SIter;
++WorkList.back().second;
if (DT->dominates(BB, SuccBB)) {
WorkList.push_back(std::make_pair(SuccBB, succ_begin(SuccBB)));
continue;
} else {
// If SuccBB is not dominated by BB then it is not removed, however remove
// any PHI incoming edge from BB.
for(BasicBlock::iterator SBI = SuccBB->begin(), SBE = SuccBB->end();
SBI != SBE; ++SBI) {
if (PHINode *PN = dyn_cast<PHINode>(SBI))
PN->removeIncomingValue(BB);
else
break;
}
// If BB is not dominating SuccBB then SuccBB is in BB's dominance
// frontiner.
DominanceFrontier::iterator BBDF = DF->find(BB);
DF->removeFromFrontier(BBDF, SuccBB);
}
}
}
}
bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
BasicBlock *Preheader = L->getLoopPreheader();
// True loop is original loop. False loop is cloned loop.
bool SignedPredicate = ExitCondition->isSignedPredicate();
//[*] Calculate True loop's new Exit Value in loop preheader.
// TLExitValue = min(SplitValue, ExitValue)
//[*] Calculate False loop's new Start Value in loop preheader.
// FLStartValue = min(SplitValue, TrueLoop.StartValue)
Value *TLExitValue = NULL;
Value *FLStartValue = NULL;
if (isa<ConstantInt>(SD.SplitValue)) {
TLExitValue = SD.SplitValue;
FLStartValue = SD.SplitValue;
}
else {
Value *C1 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
SD.SplitValue,
ExitCondition->getOperand(ExitValueNum),
"lsplit.ev",
Preheader->getTerminator());
TLExitValue = new SelectInst(C1, SD.SplitValue,
ExitCondition->getOperand(ExitValueNum),
"lsplit.ev", Preheader->getTerminator());
Value *C2 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
SD.SplitValue, StartValue, "lsplit.sv",
Preheader->getTerminator());
FLStartValue = new SelectInst(C2, SD.SplitValue, StartValue,
"lsplit.sv", Preheader->getTerminator());
}
//[*] Clone loop. Avoid true destination of split condition and
// the blocks dominated by true destination.
DenseMap<const Value *, Value *> ValueMap;
Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this);
BasicBlock *FalseHeader = FalseLoop->getHeader();
//[*] True loop's exit edge enters False loop.
PHINode *IndVarClone = cast<PHINode>(ValueMap[IndVar]);
BasicBlock *ExitBlock = ExitCondition->getParent();
BranchInst *ExitInsn = dyn_cast<BranchInst>(ExitBlock->getTerminator());
assert (ExitInsn && "Unable to find suitable loop exit branch");
BasicBlock *ExitDest = ExitInsn->getSuccessor(1);
if (L->contains(ExitDest)) {
ExitDest = ExitInsn->getSuccessor(0);
ExitInsn->setSuccessor(0, FalseHeader);
} else
ExitInsn->setSuccessor(1, FalseHeader);
// Collect inverse map of Header PHINodes.
DenseMap<Value *, Value *> InverseMap;
for (BasicBlock::iterator BI = L->getHeader()->begin(),
BE = L->getHeader()->end(); BI != BE; ++BI) {
if (PHINode *PN = dyn_cast<PHINode>(BI)) {
PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
InverseMap[PNClone] = PN;
} else
break;
}
// Update False loop's header
for (BasicBlock::iterator BI = FalseHeader->begin(), BE = FalseHeader->end();
BI != BE; ++BI) {
if (PHINode *PN = dyn_cast<PHINode>(BI)) {
PN->removeIncomingValue(Preheader);
if (PN == IndVarClone)
PN->addIncoming(FLStartValue, ExitBlock);
else {
PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
Value *V2 = OrigPN->getIncomingValueForBlock(ExitBlock);
PN->addIncoming(V2, ExitBlock);
}
} else
break;
}
// Update ExitDest. Now it's predecessor is False loop's exit block.
BasicBlock *ExitBlockClone = cast<BasicBlock>(ValueMap[ExitBlock]);
for (BasicBlock::iterator BI = ExitDest->begin(), BE = ExitDest->end();
BI != BE; ++BI) {
if (PHINode *PN = dyn_cast<PHINode>(BI)) {
PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(ExitBlock)], ExitBlockClone);
PN->removeIncomingValue(ExitBlock);
} else
break;
}
if (DT) {
DT->changeImmediateDominator(FalseHeader, ExitBlock);
DT->changeImmediateDominator(ExitDest, cast<BasicBlock>(ValueMap[ExitBlock]));
}
assert (!L->contains(ExitDest) && " Unable to find exit edge destination");
//[*] Split Exit Edge.
SplitEdge(ExitBlock, FalseHeader, this);
//[*] Eliminate split condition's false branch from True loop.
BasicBlock *SplitBlock = SD.SplitCondition->getParent();
BranchInst *BR = cast<BranchInst>(SplitBlock->getTerminator());
BasicBlock *FBB = BR->getSuccessor(1);
BR->setUnconditionalDest(BR->getSuccessor(0));
removeBlocks(FBB, L);
//[*] Update True loop's exit value using new exit value.
ExitCondition->setOperand(ExitValueNum, TLExitValue);
//[*] Eliminate split condition's true branch in False loop CFG.
BasicBlock *FSplitBlock = cast<BasicBlock>(ValueMap[SplitBlock]);
BranchInst *FBR = cast<BranchInst>(FSplitBlock->getTerminator());
BasicBlock *TBB = FBR->getSuccessor(0);
FBR->setUnconditionalDest(FBR->getSuccessor(1));
removeBlocks(TBB, FalseLoop);
return true;
}
|