1 //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Loop Index Splitting Pass. This pass handles three
13 // [1] A loop may be eliminated if the body is executed exactly once.
16 // for (i = 0; i < N; ++i) {
27 // [2] A loop's iteration space may be shrunk if the loop body is executed
28 // for a proper sub-range of the loop's iteration space. For example,
30 // for (i = 0; i < N; ++i) {
31 // if (i > A && i < B) {
36 // is transformed to iterators from A to B, if A > 0 and B < N.
38 // [3] A loop may be split if the loop body is dominated by a branch.
41 // for (i = LB; i < UB; ++i) { if (i < SV) A; else B; }
43 // is transformed into
46 // for (i = LB; i < min(UB, AEV); ++i)
48 // for (i = max(LB, BSV); i < UB; ++i);
51 //===----------------------------------------------------------------------===//
53 #define DEBUG_TYPE "loop-index-split"
54 #include "llvm/Transforms/Scalar.h"
55 #include "llvm/IntrinsicInst.h"
56 #include "llvm/LLVMContext.h"
57 #include "llvm/Analysis/LoopPass.h"
58 #include "llvm/Analysis/ScalarEvolution.h"
59 #include "llvm/Analysis/Dominators.h"
60 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
61 #include "llvm/Transforms/Utils/Cloning.h"
62 #include "llvm/Transforms/Utils/Local.h"
63 #include "llvm/ADT/DepthFirstIterator.h"
64 #include "llvm/ADT/Statistic.h"
68 STATISTIC(NumIndexSplit, "Number of loop index split");
69 STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split");
70 STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted");
74 class LoopIndexSplit : public LoopPass {
76 static char ID; // Pass ID, replacement for typeid
77 LoopIndexSplit() : LoopPass(&ID) {}
79 // Index split Loop L. Return true if loop is split.
80 bool runOnLoop(Loop *L, LPPassManager &LPM);
82 void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.addPreserved<ScalarEvolution>();
84 AU.addRequiredID(LCSSAID);
85 AU.addPreservedID(LCSSAID);
86 AU.addRequired<LoopInfo>();
87 AU.addPreserved<LoopInfo>();
88 AU.addRequiredID(LoopSimplifyID);
89 AU.addPreservedID(LoopSimplifyID);
90 AU.addRequired<DominatorTree>();
91 AU.addRequired<DominanceFrontier>();
92 AU.addPreserved<DominatorTree>();
93 AU.addPreserved<DominanceFrontier>();
97 /// processOneIterationLoop -- Eliminate loop if loop body is executed
98 /// only once. For example,
99 /// for (i = 0; i < N; ++i) {
105 bool processOneIterationLoop();
107 // -- Routines used by updateLoopIterationSpace();
109 /// updateLoopIterationSpace -- Update loop's iteration space if loop
110 /// body is executed for certain IV range only. For example,
112 /// for (i = 0; i < N; ++i) {
113 /// if ( i > A && i < B) {
117 /// is transformed to iterators from A to B, if A > 0 and B < N.
119 bool updateLoopIterationSpace();
121 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value
122 /// with a loop invariant value. Update loop's lower and upper bound based on
123 /// the loop invariant value.
124 bool restrictLoopBound(ICmpInst &Op);
126 // --- Routines used by splitLoop(). --- /
130 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by
131 /// DeadBB. This routine is used to remove split condition's dead branch,
132 /// dominated by DeadBB. LiveBB dominates split conidition's other branch.
133 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
135 /// moveExitCondition - Move exit condition EC into split condition block.
136 void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
137 BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC,
138 PHINode *IV, Instruction *IVAdd, Loop *LP,
141 /// updatePHINodes - CFG has been changed.
143 /// - ExitBB's single predecessor was Latch
144 /// - Latch's second successor was Header
146 /// - ExitBB's single predecessor was Header
147 /// - Latch's one and only successor was Header
149 /// Update ExitBB PHINodes' to reflect this change.
150 void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
152 PHINode *IV, Instruction *IVIncrement, Loop *LP);
154 // --- Utility routines --- /
156 /// cleanBlock - A block is considered clean if all non terminal
157 /// instructions are either PHINodes or IV based values.
158 bool cleanBlock(BasicBlock *BB);
160 /// IVisLT - If Op is comparing IV based value with an loop invariant and
161 /// IV based value is less than the loop invariant then return the loop
162 /// invariant. Otherwise return NULL.
163 Value * IVisLT(ICmpInst &Op);
165 /// IVisLE - If Op is comparing IV based value with an loop invariant and
166 /// IV based value is less than or equal to the loop invariant then
167 /// return the loop invariant. Otherwise return NULL.
168 Value * IVisLE(ICmpInst &Op);
170 /// IVisGT - If Op is comparing IV based value with an loop invariant and
171 /// IV based value is greater than the loop invariant then return the loop
172 /// invariant. Otherwise return NULL.
173 Value * IVisGT(ICmpInst &Op);
175 /// IVisGE - If Op is comparing IV based value with an loop invariant and
176 /// IV based value is greater than or equal to the loop invariant then
177 /// return the loop invariant. Otherwise return NULL.
178 Value * IVisGE(ICmpInst &Op);
182 // Current Loop information.
187 DominanceFrontier *DF;
190 ICmpInst *ExitCondition;
191 ICmpInst *SplitCondition;
194 Instruction *IVIncrement;
195 SmallPtrSet<Value *, 4> IVBasedValues;
199 char LoopIndexSplit::ID = 0;
200 static RegisterPass<LoopIndexSplit>
201 X("loop-index-split", "Index Split Loops");
203 Pass *llvm::createLoopIndexSplitPass() {
204 return new LoopIndexSplit();
207 // Index split Loop L. Return true if loop is split.
208 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
212 // If LoopSimplify form is not available, stay out of trouble.
213 if (!L->isLoopSimplifyForm())
216 // FIXME - Nested loops make dominator info updates tricky.
217 if (!L->getSubLoops().empty())
220 DT = &getAnalysis<DominatorTree>();
221 LI = &getAnalysis<LoopInfo>();
222 DF = &getAnalysis<DominanceFrontier>();
224 // Initialize loop data.
225 IndVar = L->getCanonicalInductionVariable();
226 if (!IndVar) return false;
228 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1));
229 IVStartValue = IndVar->getIncomingValue(!P1InLoop);
230 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop));
231 if (!IVIncrement) return false;
233 IVBasedValues.clear();
234 IVBasedValues.insert(IndVar);
235 IVBasedValues.insert(IVIncrement);
236 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
238 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
240 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI))
241 if (BO != IVIncrement
242 && (BO->getOpcode() == Instruction::Add
243 || BO->getOpcode() == Instruction::Sub))
244 if (IVBasedValues.count(BO->getOperand(0))
245 && L->isLoopInvariant(BO->getOperand(1)))
246 IVBasedValues.insert(BO);
249 // Reject loop if loop exit condition is not suitable.
250 BasicBlock *ExitingBlock = L->getExitingBlock();
253 BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
254 if (!EBR) return false;
255 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition());
256 if (!ExitCondition) return false;
257 if (ExitingBlock != L->getLoopLatch()) return false;
258 IVExitValue = ExitCondition->getOperand(1);
259 if (!L->isLoopInvariant(IVExitValue))
260 IVExitValue = ExitCondition->getOperand(0);
261 if (!L->isLoopInvariant(IVExitValue))
263 if (!IVBasedValues.count(
264 ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0))))
267 // If start value is more then exit value where induction variable
268 // increments by 1 then we are potentially dealing with an infinite loop.
269 // Do not index split this loop.
270 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue))
271 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue))
272 if (SV->getSExtValue() > EV->getSExtValue())
275 if (processOneIterationLoop())
278 if (updateLoopIterationSpace())
287 // --- Helper routines ---
288 // isUsedOutsideLoop - Returns true iff V is used outside the loop L.
289 static bool isUsedOutsideLoop(Value *V, Loop *L) {
290 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
291 if (!L->contains(cast<Instruction>(*UI)))
297 static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
298 LLVMContext &Context) {
299 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
300 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
304 static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
305 LLVMContext &Context) {
306 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
307 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
310 // Return min(V1, V1)
311 static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
313 Value *C = new ICmpInst(InsertPt,
314 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
316 return SelectInst::Create(C, V1, V2, "lsp", InsertPt);
319 // Return max(V1, V2)
320 static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
322 Value *C = new ICmpInst(InsertPt,
323 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
325 return SelectInst::Create(C, V2, V1, "lsp", InsertPt);
328 /// processOneIterationLoop -- Eliminate loop if loop body is executed
329 /// only once. For example,
330 /// for (i = 0; i < N; ++i) {
336 bool LoopIndexSplit::processOneIterationLoop() {
337 SplitCondition = NULL;
338 BasicBlock *Latch = L->getLoopLatch();
339 BasicBlock *Header = L->getHeader();
340 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
341 if (!BR) return false;
342 if (!isa<BranchInst>(Latch->getTerminator())) return false;
343 if (BR->isUnconditional()) return false;
344 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition());
345 if (!SplitCondition) return false;
346 if (SplitCondition == ExitCondition) return false;
347 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false;
348 if (BR->getOperand(1) != Latch) return false;
349 if (!IVBasedValues.count(SplitCondition->getOperand(0))
350 && !IVBasedValues.count(SplitCondition->getOperand(1)))
353 // If IV is used outside the loop then this loop traversal is required.
354 // FIXME: Calculate and use last IV value.
355 if (isUsedOutsideLoop(IVIncrement, L))
358 // If BR operands are not IV or not loop invariants then skip this loop.
359 Value *OPV = SplitCondition->getOperand(0);
360 Value *SplitValue = SplitCondition->getOperand(1);
361 if (!L->isLoopInvariant(SplitValue))
362 std::swap(OPV, SplitValue);
363 if (!L->isLoopInvariant(SplitValue))
365 Instruction *OPI = dyn_cast<Instruction>(OPV);
368 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
370 Value *StartValue = IVStartValue;
371 Value *ExitValue = IVExitValue;;
374 // If BR operand is IV based then use this operand to calculate
375 // effective conditions for loop body.
376 BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
379 if (BOPV->getOpcode() != Instruction::Add)
381 StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
382 ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
385 if (!cleanBlock(Header))
388 if (!cleanBlock(Latch))
391 // If the merge point for BR is not loop latch then skip this loop.
392 if (BR->getSuccessor(0) != Latch) {
393 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
394 assert (DF0 != DF->end() && "Unable to find dominance frontier");
395 if (!DF0->second.count(Latch))
399 if (BR->getSuccessor(1) != Latch) {
400 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
401 assert (DF1 != DF->end() && "Unable to find dominance frontier");
402 if (!DF1->second.count(Latch))
406 // Now, Current loop L contains compare instruction
407 // that compares induction variable, IndVar, against loop invariant. And
408 // entire (i.e. meaningful) loop body is dominated by this compare
409 // instruction. In such case eliminate
410 // loop structure surrounding this loop body. For example,
411 // for (int i = start; i < end; ++i) {
412 // if ( i == somevalue) {
416 // can be transformed into
417 // if (somevalue >= start && somevalue < end) {
422 // Replace index variable with split value in loop body. Loop body is executed
423 // only when index variable is equal to split value.
424 IndVar->replaceAllUsesWith(SplitValue);
426 // Replace split condition in header.
428 // SplitCondition : icmp eq i32 IndVar, SplitValue
430 // c1 = icmp uge i32 SplitValue, StartValue
431 // c2 = icmp ult i32 SplitValue, ExitValue
433 Instruction *C1 = new ICmpInst(BR, ExitCondition->isSigned() ?
434 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
435 SplitValue, StartValue, "lisplit");
437 CmpInst::Predicate C2P = ExitCondition->getPredicate();
438 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
439 if (LatchBR->getOperand(1) != Header)
440 C2P = CmpInst::getInversePredicate(C2P);
441 Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit");
442 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
444 SplitCondition->replaceAllUsesWith(NSplitCond);
445 SplitCondition->eraseFromParent();
447 // Remove Latch to Header edge.
448 BasicBlock *LatchSucc = NULL;
449 Header->removePredecessor(Latch);
450 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
456 // Clean up latch block.
457 Value *LatchBRCond = LatchBR->getCondition();
458 LatchBR->setUnconditionalDest(LatchSucc);
459 RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
461 LPM->deleteLoopFromQueue(L);
463 // Update Dominator Info.
464 // Only CFG change done is to remove Latch to Header edge. This
465 // does not change dominator tree because Latch did not dominate
468 DominanceFrontier::iterator HeaderDF = DF->find(Header);
469 if (HeaderDF != DF->end())
470 DF->removeFromFrontier(HeaderDF, Header);
472 DominanceFrontier::iterator LatchDF = DF->find(Latch);
473 if (LatchDF != DF->end())
474 DF->removeFromFrontier(LatchDF, Header);
477 ++NumIndexSplitRemoved;
481 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value
482 /// with a loop invariant value. Update loop's lower and upper bound based on
483 /// the loop invariant value.
484 bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) {
485 bool Sign = Op.isSigned();
486 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
488 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
490 cast<BranchInst>(ExitCondition->getParent()->getTerminator());
491 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
492 BasicBlock *T = EBR->getSuccessor(0);
493 EBR->setSuccessor(0, EBR->getSuccessor(1));
494 EBR->setSuccessor(1, T);
497 LLVMContext &Context = Op.getContext();
499 // New upper and lower bounds.
502 if (Value *V = IVisLT(Op)) {
503 // Restrict upper bound.
504 if (IVisLE(*ExitCondition))
505 V = getMinusOne(V, Sign, PHTerm, Context);
506 NUB = getMin(V, IVExitValue, Sign, PHTerm);
507 } else if (Value *V = IVisLE(Op)) {
508 // Restrict upper bound.
509 if (IVisLT(*ExitCondition))
510 V = getPlusOne(V, Sign, PHTerm, Context);
511 NUB = getMin(V, IVExitValue, Sign, PHTerm);
512 } else if (Value *V = IVisGT(Op)) {
513 // Restrict lower bound.
514 V = getPlusOne(V, Sign, PHTerm, Context);
515 NLB = getMax(V, IVStartValue, Sign, PHTerm);
516 } else if (Value *V = IVisGE(Op))
517 // Restrict lower bound.
518 NLB = getMax(V, IVStartValue, Sign, PHTerm);
524 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader());
525 IndVar->setIncomingValue(i, NLB);
529 unsigned i = (ExitCondition->getOperand(0) != IVExitValue);
530 ExitCondition->setOperand(i, NUB);
535 /// updateLoopIterationSpace -- Update loop's iteration space if loop
536 /// body is executed for certain IV range only. For example,
538 /// for (i = 0; i < N; ++i) {
539 /// if ( i > A && i < B) {
543 /// is transformed to iterators from A to B, if A > 0 and B < N.
545 bool LoopIndexSplit::updateLoopIterationSpace() {
546 SplitCondition = NULL;
547 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
548 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
550 BasicBlock *Latch = L->getLoopLatch();
551 BasicBlock *Header = L->getHeader();
552 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
553 if (!BR) return false;
554 if (!isa<BranchInst>(Latch->getTerminator())) return false;
555 if (BR->isUnconditional()) return false;
556 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition());
557 if (!AND) return false;
558 if (AND->getOpcode() != Instruction::And) return false;
559 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0));
560 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1));
563 IVBasedValues.insert(AND);
564 IVBasedValues.insert(Op0);
565 IVBasedValues.insert(Op1);
566 if (!cleanBlock(Header)) return false;
567 BasicBlock *ExitingBlock = ExitCondition->getParent();
568 if (!cleanBlock(ExitingBlock)) return false;
570 // If the merge point for BR is not loop latch then skip this loop.
571 if (BR->getSuccessor(0) != Latch) {
572 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
573 assert (DF0 != DF->end() && "Unable to find dominance frontier");
574 if (!DF0->second.count(Latch))
578 if (BR->getSuccessor(1) != Latch) {
579 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
580 assert (DF1 != DF->end() && "Unable to find dominance frontier");
581 if (!DF1->second.count(Latch))
585 // Verify that loop exiting block has only two predecessor, where one pred
586 // is split condition block. The other predecessor will become exiting block's
587 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has
588 // more then two predecessors. This requires extra work in updating dominator
590 BasicBlock *ExitingBBPred = NULL;
591 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock);
593 BasicBlock *BB = *PI;
602 if (!restrictLoopBound(*Op0))
605 if (!restrictLoopBound(*Op1))
609 if (BR->getSuccessor(0) == ExitingBlock)
610 BR->setUnconditionalDest(BR->getSuccessor(1));
612 BR->setUnconditionalDest(BR->getSuccessor(0));
614 AND->eraseFromParent();
615 if (Op0->use_empty())
616 Op0->eraseFromParent();
617 if (Op1->use_empty())
618 Op1->eraseFromParent();
620 // Update domiantor info. Now, ExitingBlock has only one predecessor,
621 // ExitingBBPred, and it is ExitingBlock's immediate domiantor.
622 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred);
624 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1);
625 if (L->contains(ExitBlock))
626 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0);
628 // If ExitingBlock is a member of the loop basic blocks' DF list then
629 // replace ExitingBlock with header and exit block in the DF list
630 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock);
631 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
634 if (BB == Header || BB == ExitingBlock)
636 DominanceFrontier::iterator BBDF = DF->find(BB);
637 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
638 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
639 while (DomSetI != DomSetE) {
640 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
642 BasicBlock *DFBB = *CurrentItr;
643 if (DFBB == ExitingBlock) {
644 BBDF->second.erase(DFBB);
645 for (DominanceFrontier::DomSetType::iterator
646 EBI = ExitingBlockDF->second.begin(),
647 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI)
648 BBDF->second.insert(*EBI);
656 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
657 /// This routine is used to remove split condition's dead branch, dominated by
658 /// DeadBB. LiveBB dominates split conidition's other branch.
659 void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
660 BasicBlock *LiveBB) {
662 // First update DeadBB's dominance frontier.
663 SmallVector<BasicBlock *, 8> FrontierBBs;
664 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
665 if (DeadBBDF != DF->end()) {
666 SmallVector<BasicBlock *, 8> PredBlocks;
668 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
669 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
670 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI)
672 BasicBlock *FrontierBB = *DeadBBSetI;
673 FrontierBBs.push_back(FrontierBB);
675 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
677 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
680 if (P == DeadBB || DT->dominates(DeadBB, P))
681 PredBlocks.push_back(P);
684 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
686 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
687 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
688 PE = PredBlocks.end(); PI != PE; ++PI) {
690 PN->removeIncomingValue(P);
699 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
700 SmallVector<BasicBlock *, 32> WorkList;
701 DomTreeNode *DN = DT->getNode(DeadBB);
702 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
703 E = df_end(DN); DI != E; ++DI) {
704 BasicBlock *BB = DI->getBlock();
705 WorkList.push_back(BB);
706 BB->replaceAllUsesWith(UndefValue::get(
707 Type::getLabelTy(DeadBB->getContext())));
710 while (!WorkList.empty()) {
711 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
712 LPM->deleteSimpleAnalysisValue(BB, LP);
713 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
715 Instruction *I = BBI;
717 I->replaceAllUsesWith(UndefValue::get(I->getType()));
718 LPM->deleteSimpleAnalysisValue(I, LP);
719 I->eraseFromParent();
724 BB->eraseFromParent();
727 // Update Frontier BBs' dominator info.
728 while (!FrontierBBs.empty()) {
729 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
730 BasicBlock *NewDominator = FBB->getSinglePredecessor();
732 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
735 if (NewDominator != LiveBB) {
736 for(; PI != PE; ++PI) {
739 NewDominator = LiveBB;
742 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
746 assert (NewDominator && "Unable to fix dominator info.");
747 DT->changeImmediateDominator(FBB, NewDominator);
748 DF->changeImmediateDominator(FBB, NewDominator, DT);
753 // moveExitCondition - Move exit condition EC into split condition block CondBB.
754 void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
755 BasicBlock *ExitBB, ICmpInst *EC,
756 ICmpInst *SC, PHINode *IV,
757 Instruction *IVAdd, Loop *LP,
758 unsigned ExitValueNum) {
760 BasicBlock *ExitingBB = EC->getParent();
761 Instruction *CurrentBR = CondBB->getTerminator();
763 // Move exit condition into split condition block.
764 EC->moveBefore(CurrentBR);
765 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV);
767 // Move exiting block's branch into split condition block. Update its branch
769 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator());
770 ExitingBR->moveBefore(CurrentBR);
771 BasicBlock *OrigDestBB = NULL;
772 if (ExitingBR->getSuccessor(0) == ExitBB) {
773 OrigDestBB = ExitingBR->getSuccessor(1);
774 ExitingBR->setSuccessor(1, ActiveBB);
777 OrigDestBB = ExitingBR->getSuccessor(0);
778 ExitingBR->setSuccessor(0, ActiveBB);
781 // Remove split condition and current split condition branch.
782 SC->eraseFromParent();
783 CurrentBR->eraseFromParent();
785 // Connect exiting block to original destination.
786 BranchInst::Create(OrigDestBB, ExitingBB);
789 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
791 // Fix dominator info.
792 // ExitBB is now dominated by CondBB
793 DT->changeImmediateDominator(ExitBB, CondBB);
794 DF->changeImmediateDominator(ExitBB, CondBB, DT);
796 // Blocks outside the loop may have been in the dominance frontier of blocks
797 // inside the condition; this is now impossible because the blocks inside the
798 // condition no loger dominate the exit. Remove the relevant blocks from
799 // the dominance frontiers.
800 for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end();
802 if (*I == CondBB || !DT->dominates(CondBB, *I)) continue;
803 DominanceFrontier::iterator BBDF = DF->find(*I);
804 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
805 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
806 while (DomSetI != DomSetE) {
807 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
809 BasicBlock *DFBB = *CurrentItr;
810 if (!LP->contains(DFBB))
811 BBDF->second.erase(DFBB);
816 /// updatePHINodes - CFG has been changed.
818 /// - ExitBB's single predecessor was Latch
819 /// - Latch's second successor was Header
821 /// - ExitBB's single predecessor is Header
822 /// - Latch's one and only successor is Header
824 /// Update ExitBB PHINodes' to reflect this change.
825 void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
827 PHINode *IV, Instruction *IVIncrement,
830 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end();
832 PHINode *PN = dyn_cast<PHINode>(BI);
837 Value *V = PN->getIncomingValueForBlock(Latch);
838 if (PHINode *PHV = dyn_cast<PHINode>(V)) {
839 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
840 // in Header which is new incoming value for PN.
842 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end();
844 if (PHINode *U = dyn_cast<PHINode>(*UI))
845 if (LP->contains(U)) {
850 // Add incoming value from header only if PN has any use inside the loop.
852 PN->addIncoming(NewV, Header);
854 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
855 // If this instruction is IVIncrement then IV is new incoming value
856 // from header otherwise this instruction must be incoming value from
857 // header because loop is in LCSSA form.
858 if (PHI == IVIncrement)
859 PN->addIncoming(IV, Header);
861 PN->addIncoming(V, Header);
863 // Otherwise this is an incoming value from header because loop is in
865 PN->addIncoming(V, Header);
867 // Remove incoming value from Latch.
868 PN->removeIncomingValue(Latch);
872 bool LoopIndexSplit::splitLoop() {
873 SplitCondition = NULL;
874 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
875 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
877 BasicBlock *Header = L->getHeader();
878 BasicBlock *Latch = L->getLoopLatch();
879 BranchInst *SBR = NULL; // Split Condition Branch
880 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator());
881 // If Exiting block includes loop variant instructions then this
882 // loop may not be split safely.
883 BasicBlock *ExitingBlock = ExitCondition->getParent();
884 if (!cleanBlock(ExitingBlock)) return false;
886 LLVMContext &Context = Header->getContext();
888 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
890 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator());
891 if (!BR || BR->isUnconditional()) continue;
892 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
893 if (!CI || CI == ExitCondition
894 || CI->getPredicate() == ICmpInst::ICMP_NE
895 || CI->getPredicate() == ICmpInst::ICMP_EQ)
898 // Unable to handle triangle loops at the moment.
899 // In triangle loop, split condition is in header and one of the
900 // the split destination is loop latch. If split condition is EQ
901 // then such loops are already handle in processOneIterationLoop().
903 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1)))
906 // If the block does not dominate the latch then this is not a diamond.
907 // Such loop may not benefit from index split.
908 if (!DT->dominates((*I), Latch))
911 // If split condition branches heads do not have single predecessor,
912 // SplitCondBlock, then is not possible to remove inactive branch.
913 if (!BR->getSuccessor(0)->getSinglePredecessor()
914 || !BR->getSuccessor(1)->getSinglePredecessor())
917 // If the merge point for BR is not loop latch then skip this condition.
918 if (BR->getSuccessor(0) != Latch) {
919 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
920 assert (DF0 != DF->end() && "Unable to find dominance frontier");
921 if (!DF0->second.count(Latch))
925 if (BR->getSuccessor(1) != Latch) {
926 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
927 assert (DF1 != DF->end() && "Unable to find dominance frontier");
928 if (!DF1->second.count(Latch))
939 // If the predicate sign does not match then skip.
940 if (ExitCondition->isSigned() != SplitCondition->isSigned())
943 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue);
944 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0));
945 Value *SplitValue = SplitCondition->getOperand(SVOpNum);
946 if (!L->isLoopInvariant(SplitValue))
948 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum)))
951 // Normalize loop conditions so that it is easier to calculate new loop
953 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
954 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
955 BasicBlock *T = EBR->getSuccessor(0);
956 EBR->setSuccessor(0, EBR->getSuccessor(1));
957 EBR->setSuccessor(1, T);
960 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) {
961 SplitCondition->setPredicate(SplitCondition->getInversePredicate());
962 BasicBlock *T = SBR->getSuccessor(0);
963 SBR->setSuccessor(0, SBR->getSuccessor(1));
964 SBR->setSuccessor(1, T);
967 //[*] Calculate new loop bounds.
968 Value *AEV = SplitValue;
969 Value *BSV = SplitValue;
970 bool Sign = SplitCondition->isSigned();
971 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
973 if (IVisLT(*ExitCondition)) {
974 if (IVisLT(*SplitCondition)) {
977 else if (IVisLE(*SplitCondition)) {
978 AEV = getPlusOne(SplitValue, Sign, PHTerm, Context);
979 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
981 assert (0 && "Unexpected split condition!");
984 else if (IVisLE(*ExitCondition)) {
985 if (IVisLT(*SplitCondition)) {
986 AEV = getMinusOne(SplitValue, Sign, PHTerm, Context);
988 else if (IVisLE(*SplitCondition)) {
989 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
991 assert (0 && "Unexpected split condition!");
994 assert (0 && "Unexpected exit condition!");
996 AEV = getMin(AEV, IVExitValue, Sign, PHTerm);
997 BSV = getMax(BSV, IVStartValue, Sign, PHTerm);
1000 DenseMap<const Value *, Value *> ValueMap;
1001 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
1004 // [*] ALoop's exiting edge enters BLoop's header.
1005 // ALoop's original exit block becomes BLoop's exit block.
1006 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
1007 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
1008 BranchInst *A_ExitInsn =
1009 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
1010 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
1011 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
1012 BasicBlock *B_Header = BLoop->getHeader();
1013 if (ALoop->contains(B_ExitBlock)) {
1014 B_ExitBlock = A_ExitInsn->getSuccessor(0);
1015 A_ExitInsn->setSuccessor(0, B_Header);
1017 A_ExitInsn->setSuccessor(1, B_Header);
1019 // [*] Update ALoop's exit value using new exit value.
1020 ExitCondition->setOperand(EVOpNum, AEV);
1022 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
1023 // original loop's preheader. Add incoming PHINode values from
1024 // ALoop's exiting block. Update BLoop header's domiantor info.
1026 // Collect inverse map of Header PHINodes.
1027 DenseMap<Value *, Value *> InverseMap;
1028 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(),
1029 BE = ALoop->getHeader()->end(); BI != BE; ++BI) {
1030 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1031 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
1032 InverseMap[PNClone] = PN;
1037 BasicBlock *A_Preheader = ALoop->getLoopPreheader();
1038 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1040 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1041 // Remove incoming value from original preheader.
1042 PN->removeIncomingValue(A_Preheader);
1044 // Add incoming value from A_ExitingBlock.
1046 PN->addIncoming(BSV, A_ExitingBlock);
1048 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
1050 // If loop header is also loop exiting block then
1051 // OrigPN is incoming value for B loop header.
1052 if (A_ExitingBlock == ALoop->getHeader())
1055 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
1056 PN->addIncoming(V2, A_ExitingBlock);
1062 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
1063 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
1065 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
1066 // block. Remove incoming PHINode values from ALoop's exiting block.
1067 // Add new incoming values from BLoop's incoming exiting value.
1068 // Update BLoop exit block's dominator info..
1069 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
1070 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
1072 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1073 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
1075 PN->removeIncomingValue(A_ExitingBlock);
1080 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
1081 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
1083 //[*] Split ALoop's exit edge. This creates a new block which
1084 // serves two purposes. First one is to hold PHINode defnitions
1085 // to ensure that ALoop's LCSSA form. Second use it to act
1086 // as a preheader for BLoop.
1087 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
1089 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
1090 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
1091 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1093 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1094 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
1095 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
1096 newPHI->addIncoming(V1, A_ExitingBlock);
1097 A_ExitBlock->getInstList().push_front(newPHI);
1098 PN->removeIncomingValue(A_ExitBlock);
1099 PN->addIncoming(newPHI, A_ExitBlock);
1104 //[*] Eliminate split condition's inactive branch from ALoop.
1105 BasicBlock *A_SplitCondBlock = SplitCondition->getParent();
1106 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
1107 BasicBlock *A_InactiveBranch = NULL;
1108 BasicBlock *A_ActiveBranch = NULL;
1109 A_ActiveBranch = A_BR->getSuccessor(0);
1110 A_InactiveBranch = A_BR->getSuccessor(1);
1111 A_BR->setUnconditionalDest(A_ActiveBranch);
1112 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
1114 //[*] Eliminate split condition's inactive branch in from BLoop.
1115 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
1116 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
1117 BasicBlock *B_InactiveBranch = NULL;
1118 BasicBlock *B_ActiveBranch = NULL;
1119 B_ActiveBranch = B_BR->getSuccessor(1);
1120 B_InactiveBranch = B_BR->getSuccessor(0);
1121 B_BR->setUnconditionalDest(B_ActiveBranch);
1122 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
1124 BasicBlock *A_Header = ALoop->getHeader();
1125 if (A_ExitingBlock == A_Header)
1128 //[*] Move exit condition into split condition block to avoid
1129 // executing dead loop iteration.
1130 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]);
1131 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]);
1132 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]);
1134 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition,
1135 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement,
1138 moveExitCondition(B_SplitCondBlock, B_ActiveBranch,
1139 B_ExitBlock, B_ExitCondition,
1140 B_SplitCondition, B_IndVar, B_IndVarIncrement,
1147 /// cleanBlock - A block is considered clean if all non terminal instructions
1148 /// are either, PHINodes, IV based.
1149 bool LoopIndexSplit::cleanBlock(BasicBlock *BB) {
1150 Instruction *Terminator = BB->getTerminator();
1151 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1153 Instruction *I = BI;
1155 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition
1156 || I == SplitCondition || IVBasedValues.count(I)
1157 || isa<DbgInfoIntrinsic>(I))
1160 if (I->mayHaveSideEffects())
1163 // I is used only inside this block then it is OK.
1164 bool usedOutsideBB = false;
1165 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1167 Instruction *U = cast<Instruction>(UI);
1168 if (U->getParent() != BB)
1169 usedOutsideBB = true;
1174 // Otherwise we have a instruction that may not allow loop spliting.
1180 /// IVisLT - If Op is comparing IV based value with an loop invariant and
1181 /// IV based value is less than the loop invariant then return the loop
1182 /// invariant. Otherwise return NULL.
1183 Value * LoopIndexSplit::IVisLT(ICmpInst &Op) {
1184 ICmpInst::Predicate P = Op.getPredicate();
1185 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1186 && IVBasedValues.count(Op.getOperand(0))
1187 && L->isLoopInvariant(Op.getOperand(1)))
1188 return Op.getOperand(1);
1190 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1191 && IVBasedValues.count(Op.getOperand(1))
1192 && L->isLoopInvariant(Op.getOperand(0)))
1193 return Op.getOperand(0);
1198 /// IVisLE - If Op is comparing IV based value with an loop invariant and
1199 /// IV based value is less than or equal to the loop invariant then
1200 /// return the loop invariant. Otherwise return NULL.
1201 Value * LoopIndexSplit::IVisLE(ICmpInst &Op) {
1202 ICmpInst::Predicate P = Op.getPredicate();
1203 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1204 && IVBasedValues.count(Op.getOperand(0))
1205 && L->isLoopInvariant(Op.getOperand(1)))
1206 return Op.getOperand(1);
1208 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1209 && IVBasedValues.count(Op.getOperand(1))
1210 && L->isLoopInvariant(Op.getOperand(0)))
1211 return Op.getOperand(0);
1216 /// IVisGT - If Op is comparing IV based value with an loop invariant and
1217 /// IV based value is greater than the loop invariant then return the loop
1218 /// invariant. Otherwise return NULL.
1219 Value * LoopIndexSplit::IVisGT(ICmpInst &Op) {
1220 ICmpInst::Predicate P = Op.getPredicate();
1221 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1222 && IVBasedValues.count(Op.getOperand(0))
1223 && L->isLoopInvariant(Op.getOperand(1)))
1224 return Op.getOperand(1);
1226 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1227 && IVBasedValues.count(Op.getOperand(1))
1228 && L->isLoopInvariant(Op.getOperand(0)))
1229 return Op.getOperand(0);
1234 /// IVisGE - If Op is comparing IV based value with an loop invariant and
1235 /// IV based value is greater than or equal to the loop invariant then
1236 /// return the loop invariant. Otherwise return NULL.
1237 Value * LoopIndexSplit::IVisGE(ICmpInst &Op) {
1238 ICmpInst::Predicate P = Op.getPredicate();
1239 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1240 && IVBasedValues.count(Op.getOperand(0))
1241 && L->isLoopInvariant(Op.getOperand(1)))
1242 return Op.getOperand(1);
1244 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1245 && IVBasedValues.count(Op.getOperand(1))
1246 && L->isLoopInvariant(Op.getOperand(0)))
1247 return Op.getOperand(0);