1 //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements Loop Index Splitting Pass.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "loop-index-split"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Analysis/LoopPass.h"
18 #include "llvm/Analysis/ScalarEvolutionExpander.h"
19 #include "llvm/Analysis/Dominators.h"
20 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21 #include "llvm/Transforms/Utils/Cloning.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/ADT/DepthFirstIterator.h"
24 #include "llvm/ADT/Statistic.h"
28 STATISTIC(NumIndexSplit, "Number of loops index split");
32 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
35 static char ID; // Pass ID, replacement for typeid
36 LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
38 // Index split Loop L. Return true if loop is split.
39 bool runOnLoop(Loop *L, LPPassManager &LPM);
41 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<ScalarEvolution>();
43 AU.addPreserved<ScalarEvolution>();
44 AU.addRequiredID(LCSSAID);
45 AU.addPreservedID(LCSSAID);
46 AU.addRequired<LoopInfo>();
47 AU.addPreserved<LoopInfo>();
48 AU.addRequiredID(LoopSimplifyID);
49 AU.addPreservedID(LoopSimplifyID);
50 AU.addRequired<DominatorTree>();
51 AU.addRequired<DominanceFrontier>();
52 AU.addPreserved<DominatorTree>();
53 AU.addPreserved<DominanceFrontier>();
60 SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {}
62 // Induction variable's range is split at this value.
65 // This compare instruction compares IndVar against SplitValue.
66 ICmpInst *SplitCondition;
71 SplitCondition = NULL;
77 /// Find condition inside a loop that is suitable candidate for index split.
78 void findSplitCondition();
80 /// Find loop's exit condition.
81 void findLoopConditionals();
83 /// Return induction variable associated with value V.
84 void findIndVar(Value *V, Loop *L);
86 /// 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
91 bool processOneIterationLoop(SplitInfo &SD);
93 /// If loop header includes loop variant instruction operands then
94 /// this loop may not be eliminated.
95 bool safeHeader(SplitInfo &SD, BasicBlock *BB);
97 /// If Exit block includes loop variant instructions then this
98 /// loop may not be eliminated.
99 bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
101 /// 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);
106 /// Find cost of spliting loop L.
107 unsigned findSplitCost(Loop *L, SplitInfo &SD);
108 bool splitLoop(SplitInfo &SD);
112 IndVarIncrement = NULL;
113 ExitCondition = NULL;
127 DominanceFrontier *DF;
128 SmallVector<SplitInfo, 4> SplitData;
130 // Induction variable whose range is being split by this transformation.
132 Instruction *IndVarIncrement;
134 // Loop exit condition.
135 ICmpInst *ExitCondition;
137 // Induction variable's initial value.
140 // Induction variable's final loop exit value operand number in exit condition..
141 unsigned ExitValueNum;
144 char LoopIndexSplit::ID = 0;
145 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
148 LoopPass *llvm::createLoopIndexSplitPass() {
149 return new LoopIndexSplit();
152 // Index split Loop L. Return true if loop is split.
153 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
154 bool Changed = false;
158 // FIXME - Nested loops make dominator info updates tricky.
159 if (!L->getSubLoops().empty())
162 SE = &getAnalysis<ScalarEvolution>();
163 DT = &getAnalysis<DominatorTree>();
164 LI = &getAnalysis<LoopInfo>();
165 DF = &getAnalysis<DominanceFrontier>();
169 findLoopConditionals();
174 findSplitCondition();
176 if (SplitData.empty())
179 // First see if it is possible to eliminate loop itself or not.
180 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
181 E = SplitData.end(); SI != E; ++SI) {
183 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
184 Changed = processOneIterationLoop(SD);
187 // If is loop is eliminated then nothing else to do here.
193 unsigned MaxCost = 99;
195 unsigned MostProfitableSDIndex = 0;
196 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
197 E = SplitData.end(); SI != E; ++SI, ++Index) {
200 // ICM_EQs are already handled above.
201 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
204 unsigned Cost = findSplitCost(L, SD);
206 MostProfitableSDIndex = Index;
209 // Split most profitiable condition.
210 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
218 /// Return true if V is a induction variable or induction variable's
219 /// increment for loop L.
220 void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
222 Instruction *I = dyn_cast<Instruction>(V);
226 // Check if I is a phi node from loop header or not.
227 if (PHINode *PN = dyn_cast<PHINode>(V)) {
228 if (PN->getParent() == L->getHeader()) {
234 // Check if I is a add instruction whose one operand is
235 // phi node from loop header and second operand is constant.
236 if (I->getOpcode() != Instruction::Add)
239 Value *Op0 = I->getOperand(0);
240 Value *Op1 = I->getOperand(1);
242 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
243 if (PN->getParent() == L->getHeader()
244 && isa<ConstantInt>(Op1)) {
251 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
252 if (PN->getParent() == L->getHeader()
253 && isa<ConstantInt>(Op0)) {
263 // Find loop's exit condition and associated induction variable.
264 void LoopIndexSplit::findLoopConditionals() {
266 BasicBlock *ExitBlock = NULL;
268 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
271 if (!L->isLoopExit(BB))
281 // If exit block's terminator is conditional branch inst then we have found
283 BranchInst *BR = dyn_cast<BranchInst>(ExitBlock->getTerminator());
284 if (!BR || BR->isUnconditional())
287 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
293 // Exit condition's one operand is loop invariant exit value and second
294 // operand is SCEVAddRecExpr based on induction variable.
295 Value *V0 = CI->getOperand(0);
296 Value *V1 = CI->getOperand(1);
298 SCEVHandle SH0 = SE->getSCEV(V0);
299 SCEVHandle SH1 = SE->getSCEV(V1);
301 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
305 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
311 ExitCondition = NULL;
313 BasicBlock *Preheader = L->getLoopPreheader();
314 StartValue = IndVar->getIncomingValueForBlock(Preheader);
318 /// Find condition inside a loop that is suitable candidate for index split.
319 void LoopIndexSplit::findSplitCondition() {
322 // Check all basic block's terminators.
324 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
328 // If this basic block does not terminate in a conditional branch
329 // then terminator is not a suitable split condition.
330 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
334 if (BR->isUnconditional())
337 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
338 if (!CI || CI == ExitCondition)
341 // If one operand is loop invariant and second operand is SCEVAddRecExpr
342 // based on induction variable then CI is a candidate split condition.
343 Value *V0 = CI->getOperand(0);
344 Value *V1 = CI->getOperand(1);
346 SCEVHandle SH0 = SE->getSCEV(V0);
347 SCEVHandle SH1 = SE->getSCEV(V1);
349 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
351 SD.SplitCondition = CI;
352 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
354 SplitData.push_back(SD);
356 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
357 if (IndVarIncrement && IndVarIncrement == Insn)
358 SplitData.push_back(SD);
361 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
363 SD.SplitCondition = CI;
364 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
366 SplitData.push_back(SD);
368 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
369 if (IndVarIncrement && IndVarIncrement == Insn)
370 SplitData.push_back(SD);
376 /// processOneIterationLoop - Current loop L contains compare instruction
377 /// that compares induction variable, IndVar, against loop invariant. If
378 /// entire (i.e. meaningful) loop body is dominated by this compare
379 /// instruction then loop body is executed only once. In such case eliminate
380 /// loop structure surrounding this loop body. For example,
381 /// for (int i = start; i < end; ++i) {
382 /// if ( i == somevalue) {
386 /// can be transformed into
387 /// if (somevalue >= start && somevalue < end) {
391 bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
393 BasicBlock *Header = L->getHeader();
395 // First of all, check if SplitCondition dominates entire loop body
398 // If SplitCondition is not in loop header then this loop is not suitable
399 // for this transformation.
400 if (SD.SplitCondition->getParent() != Header)
403 // If loop header includes loop variant instruction operands then
404 // this loop may not be eliminated.
405 if (!safeHeader(SD, Header))
408 // If Exit block includes loop variant instructions then this
409 // loop may not be eliminated.
410 if (!safeExitBlock(SD, ExitCondition->getParent()))
415 // As a first step to break this loop, remove Latch to Header edge.
416 BasicBlock *Latch = L->getLoopLatch();
417 BasicBlock *LatchSucc = NULL;
418 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
421 Header->removePredecessor(Latch);
422 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
427 BR->setUnconditionalDest(LatchSucc);
429 Instruction *Terminator = Header->getTerminator();
430 Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
432 // Replace split condition in header.
434 // SplitCondition : icmp eq i32 IndVar, SplitValue
436 // c1 = icmp uge i32 SplitValue, StartValue
437 // c2 = icmp ult i32 vSplitValue, ExitValue
439 bool SignedPredicate = ExitCondition->isSignedPredicate();
440 Instruction *C1 = new ICmpInst(SignedPredicate ?
441 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
442 SD.SplitValue, StartValue, "lisplit",
444 Instruction *C2 = new ICmpInst(SignedPredicate ?
445 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
446 SD.SplitValue, ExitValue, "lisplit",
448 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
450 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
451 SD.SplitCondition->eraseFromParent();
453 // Now, clear latch block. Remove instructions that are responsible
454 // to increment induction variable.
455 Instruction *LTerminator = Latch->getTerminator();
456 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
460 if (isa<PHINode>(I) || I == LTerminator)
463 if (I == IndVarIncrement)
464 I->replaceAllUsesWith(ExitValue);
466 I->replaceAllUsesWith(UndefValue::get(I->getType()));
467 I->eraseFromParent();
470 LPM->deleteLoopFromQueue(L);
472 // Update Dominator Info.
473 // Only CFG change done is to remove Latch to Header edge. This
474 // does not change dominator tree because Latch did not dominate
477 DominanceFrontier::iterator HeaderDF = DF->find(Header);
478 if (HeaderDF != DF->end())
479 DF->removeFromFrontier(HeaderDF, Header);
481 DominanceFrontier::iterator LatchDF = DF->find(Latch);
482 if (LatchDF != DF->end())
483 DF->removeFromFrontier(LatchDF, Header);
488 // If loop header includes loop variant instruction operands then
489 // this loop can not be eliminated. This is used by processOneIterationLoop().
490 bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
492 Instruction *Terminator = Header->getTerminator();
493 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
501 // SplitCondition itself is OK.
502 if (I == SD.SplitCondition)
505 // Induction variable is OK.
509 // Induction variable increment is OK.
510 if (I == IndVarIncrement)
513 // Terminator is also harmless.
517 // Otherwise we have a instruction that may not be safe.
524 // If Exit block includes loop variant instructions then this
525 // loop may not be eliminated. This is used by processOneIterationLoop().
526 bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
528 for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
536 // Induction variable increment is OK.
537 if (IndVarIncrement && IndVarIncrement == I)
540 // Check if I is induction variable increment instruction.
541 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
543 Value *Op0 = I->getOperand(0);
544 Value *Op1 = I->getOperand(1);
546 ConstantInt *CI = NULL;
548 if ((PN = dyn_cast<PHINode>(Op0))) {
549 if ((CI = dyn_cast<ConstantInt>(Op1)))
552 if ((PN = dyn_cast<PHINode>(Op1))) {
553 if ((CI = dyn_cast<ConstantInt>(Op0)))
557 if (IndVarIncrement && PN == IndVar && CI->isOne())
561 // 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.
564 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
565 if (EC == ExitCondition)
569 if (I == ExitBlock->getTerminator())
572 // Otherwise we have instruction that may not be safe.
576 // We could not find any reason to consider ExitBlock unsafe.
580 /// Find cost of spliting loop L. Cost is measured in terms of size growth.
581 /// Size is growth is calculated based on amount of code duplicated in second
583 unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
586 BasicBlock *SDBlock = SD.SplitCondition->getParent();
587 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
590 // If a block is not dominated by split condition block then
591 // it must be duplicated in both loops.
592 if (!DT->dominates(SDBlock, BB))
599 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
600 /// This routine is used to remove split condition's dead branch, dominated by
601 /// DeadBB. LiveBB dominates split conidition's other branch.
602 void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
603 BasicBlock *LiveBB) {
605 // First update DeadBB's dominance frontier.
606 SmallVector<BasicBlock *, 8> FrontierBBs;
607 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
608 if (DeadBBDF != DF->end()) {
609 SmallVector<BasicBlock *, 8> PredBlocks;
611 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
612 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
613 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) {
614 BasicBlock *FrontierBB = *DeadBBSetI;
615 FrontierBBs.push_back(FrontierBB);
617 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
619 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
622 if (P == DeadBB || DT->dominates(DeadBB, P))
623 PredBlocks.push_back(P);
626 BasicBlock *NewDominator = NULL;
627 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
629 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
630 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
631 PE = PredBlocks.end(); PI != PE; ++PI) {
633 PN->removeIncomingValue(P);
635 // If we have not identified new dominator then see if we can identify
636 // one based on remaining incoming PHINode values.
637 if (NewDominator == NULL && PN->getNumIncomingValues() == 1)
638 NewDominator = PN->getIncomingBlock(0);
646 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
647 SmallVector<BasicBlock *, 32> WorkList;
648 DomTreeNode *DN = DT->getNode(DeadBB);
649 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
650 E = df_end(DN); DI != E; ++DI) {
651 BasicBlock *BB = DI->getBlock();
652 WorkList.push_back(BB);
653 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
656 while (!WorkList.empty()) {
657 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
658 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
660 Instruction *I = BBI;
661 I->replaceAllUsesWith(UndefValue::get(I->getType()));
662 I->eraseFromParent();
664 LPM->deleteSimpleAnalysisValue(BB, LP);
668 BB->eraseFromParent();
671 // Update Frontier BBs' dominator info.
672 while (!FrontierBBs.empty()) {
673 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
674 BasicBlock *NewDominator = FBB->getSinglePredecessor();
676 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
679 if (NewDominator != LiveBB) {
680 for(; PI != PE; ++PI) {
683 NewDominator = LiveBB;
686 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
690 assert (NewDominator && "Unable to fix dominator info.");
691 DT->changeImmediateDominator(FBB, NewDominator);
692 DF->changeImmediateDominator(FBB, NewDominator, DT);
697 bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
699 BasicBlock *Preheader = L->getLoopPreheader();
700 BasicBlock *SplitBlock = SD.SplitCondition->getParent();
701 BasicBlock *Latch = L->getLoopLatch();
702 BasicBlock *Header = L->getHeader();
703 BranchInst *SplitTerminator = cast<BranchInst>(SplitBlock->getTerminator());
705 // FIXME - Unable to handle triange loops at the moment.
706 // In triangle loop, split condition is in header and one of the
707 // the split destination is loop latch. If split condition is EQ
708 // then such loops are already handle in processOneIterationLoop().
709 if (Header == SplitBlock
710 && (Latch == SplitTerminator->getSuccessor(0)
711 || Latch == SplitTerminator->getSuccessor(1)))
715 BasicBlock *Succ0 = SplitTerminator->getSuccessor(0);
716 BasicBlock *Succ1 = SplitTerminator->getSuccessor(1);
717 if (DT->dominates(Succ0, Latch) || DT->dominates(Succ1, Latch))
720 // True loop is original loop. False loop is cloned loop.
722 bool SignedPredicate = ExitCondition->isSignedPredicate();
723 //[*] Calculate True loop's new Exit Value in loop preheader.
724 // TLExitValue = min(SplitValue, ExitValue)
725 //[*] Calculate False loop's new Start Value in loop preheader.
726 // FLStartValue = min(SplitValue, TrueLoop.StartValue)
727 Value *TLExitValue = NULL;
728 Value *FLStartValue = NULL;
729 if (isa<ConstantInt>(SD.SplitValue)) {
730 TLExitValue = SD.SplitValue;
731 FLStartValue = SD.SplitValue;
734 Value *C1 = new ICmpInst(SignedPredicate ?
735 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
737 ExitCondition->getOperand(ExitValueNum),
739 Preheader->getTerminator());
740 TLExitValue = new SelectInst(C1, SD.SplitValue,
741 ExitCondition->getOperand(ExitValueNum),
742 "lsplit.ev", Preheader->getTerminator());
744 Value *C2 = new ICmpInst(SignedPredicate ?
745 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
746 SD.SplitValue, StartValue, "lsplit.sv",
747 Preheader->getTerminator());
748 FLStartValue = new SelectInst(C2, SD.SplitValue, StartValue,
749 "lsplit.sv", Preheader->getTerminator());
752 //[*] Clone loop. Avoid true destination of split condition and
753 // the blocks dominated by true destination.
754 DenseMap<const Value *, Value *> ValueMap;
755 Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this);
756 BasicBlock *FalseHeader = FalseLoop->getHeader();
758 //[*] True loop's exit edge enters False loop.
759 PHINode *IndVarClone = cast<PHINode>(ValueMap[IndVar]);
760 BasicBlock *ExitBlock = ExitCondition->getParent();
761 BranchInst *ExitInsn = dyn_cast<BranchInst>(ExitBlock->getTerminator());
762 assert (ExitInsn && "Unable to find suitable loop exit branch");
763 BasicBlock *ExitDest = ExitInsn->getSuccessor(1);
765 if (L->contains(ExitDest)) {
766 ExitDest = ExitInsn->getSuccessor(0);
767 ExitInsn->setSuccessor(0, FalseHeader);
769 ExitInsn->setSuccessor(1, FalseHeader);
771 // Collect inverse map of Header PHINodes.
772 DenseMap<Value *, Value *> InverseMap;
773 for (BasicBlock::iterator BI = L->getHeader()->begin(),
774 BE = L->getHeader()->end(); BI != BE; ++BI) {
775 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
776 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
777 InverseMap[PNClone] = PN;
782 // Update False loop's header
783 for (BasicBlock::iterator BI = FalseHeader->begin(), BE = FalseHeader->end();
785 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
786 PN->removeIncomingValue(Preheader);
787 if (PN == IndVarClone)
788 PN->addIncoming(FLStartValue, ExitBlock);
790 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
791 Value *V2 = OrigPN->getIncomingValueForBlock(ExitBlock);
792 PN->addIncoming(V2, ExitBlock);
798 // Update ExitDest. Now it's predecessor is False loop's exit block.
799 BasicBlock *ExitBlockClone = cast<BasicBlock>(ValueMap[ExitBlock]);
800 for (BasicBlock::iterator BI = ExitDest->begin(), BE = ExitDest->end();
802 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
803 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(ExitBlock)], ExitBlockClone);
804 PN->removeIncomingValue(ExitBlock);
810 DT->changeImmediateDominator(FalseHeader, ExitBlock);
811 DT->changeImmediateDominator(ExitDest, cast<BasicBlock>(ValueMap[ExitBlock]));
814 assert (!L->contains(ExitDest) && " Unable to find exit edge destination");
816 //[*] Split Exit Edge.
817 SplitEdge(ExitBlock, FalseHeader, this);
819 //[*] Eliminate split condition's false branch from True loop.
820 BranchInst *BR = cast<BranchInst>(SplitBlock->getTerminator());
821 BasicBlock *FBB = BR->getSuccessor(1);
822 BR->setUnconditionalDest(BR->getSuccessor(0));
823 removeBlocks(FBB, L, BR->getSuccessor(0));
825 //[*] Update True loop's exit value using new exit value.
826 ExitCondition->setOperand(ExitValueNum, TLExitValue);
828 //[*] Eliminate split condition's true branch in False loop CFG.
829 BasicBlock *FSplitBlock = cast<BasicBlock>(ValueMap[SplitBlock]);
830 BranchInst *FBR = cast<BranchInst>(FSplitBlock->getTerminator());
831 BasicBlock *TBB = FBR->getSuccessor(0);
832 FBR->setUnconditionalDest(FBR->getSuccessor(1));
833 removeBlocks(TBB, FalseLoop, cast<BasicBlock>(FBR->getSuccessor(0)));