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/Function.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Analysis/ScalarEvolutionExpander.h"
20 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
23 #include "llvm/Support/Compiler.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.addPreserved<DominatorTree>();
52 AU.addPreserved<DominanceFrontier>();
59 SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {}
61 // Induction variable's range is split at this value.
64 // This compare instruction compares IndVar against SplitValue.
65 ICmpInst *SplitCondition;
70 SplitCondition = NULL;
76 /// Find condition inside a loop that is suitable candidate for index split.
77 void findSplitCondition();
79 /// Find loop's exit condition.
80 void findLoopConditionals();
82 /// Return induction variable associated with value V.
83 void findIndVar(Value *V, Loop *L);
85 /// processOneIterationLoop - Current loop L contains compare instruction
86 /// that compares induction variable, IndVar, agains loop invariant. If
87 /// entire (i.e. meaningful) loop body is dominated by this compare
88 /// instruction then loop body is executed only for one iteration. In
89 /// such case eliminate loop structure surrounding this loop body. For
90 bool processOneIterationLoop(SplitInfo &SD);
92 /// If loop header includes loop variant instruction operands then
93 /// this loop may not be eliminated.
94 bool safeHeader(SplitInfo &SD, BasicBlock *BB);
96 /// If Exit block includes loop variant instructions then this
97 /// loop may not be eliminated.
98 bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
100 /// removeBlocks - Remove basic block BB and all blocks dominated by BB.
101 void removeBlocks(BasicBlock *InBB);
103 /// Find cost of spliting loop L.
104 unsigned findSplitCost(Loop *L, SplitInfo &SD);
105 bool splitLoop(SplitInfo &SD);
109 IndVarIncrement = NULL;
110 ExitCondition = NULL;
124 SmallVector<SplitInfo, 4> SplitData;
126 // Induction variable whose range is being split by this transformation.
128 Instruction *IndVarIncrement;
130 // Loop exit condition.
131 ICmpInst *ExitCondition;
133 // Induction variable's initial value.
136 // Induction variable's final loop exit value operand number in exit condition..
137 unsigned ExitValueNum;
140 char LoopIndexSplit::ID = 0;
141 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
144 LoopPass *llvm::createLoopIndexSplitPass() {
145 return new LoopIndexSplit();
148 // Index split Loop L. Return true if loop is split.
149 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
150 bool Changed = false;
154 SE = &getAnalysis<ScalarEvolution>();
155 DT = &getAnalysis<DominatorTree>();
156 LI = &getAnalysis<LoopInfo>();
160 findLoopConditionals();
165 findSplitCondition();
167 if (SplitData.empty())
170 // First see if it is possible to eliminate loop itself or not.
171 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
172 E = SplitData.end(); SI != E; ++SI) {
174 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
175 Changed = processOneIterationLoop(SD);
178 // If is loop is eliminated then nothing else to do here.
184 unsigned MaxCost = 99;
186 unsigned MostProfitableSDIndex = 0;
187 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
188 E = SplitData.end(); SI != E; ++SI, ++Index) {
191 // ICM_EQs are already handled above.
192 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
195 unsigned Cost = findSplitCost(L, SD);
197 MostProfitableSDIndex = Index;
200 // Split most profitiable condition.
201 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
209 /// Return true if V is a induction variable or induction variable's
210 /// increment for loop L.
211 void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
213 Instruction *I = dyn_cast<Instruction>(V);
217 // Check if I is a phi node from loop header or not.
218 if (PHINode *PN = dyn_cast<PHINode>(V)) {
219 if (PN->getParent() == L->getHeader()) {
225 // Check if I is a add instruction whose one operand is
226 // phi node from loop header and second operand is constant.
227 if (I->getOpcode() != Instruction::Add)
230 Value *Op0 = I->getOperand(0);
231 Value *Op1 = I->getOperand(1);
233 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
234 if (PN->getParent() == L->getHeader()
235 && isa<ConstantInt>(Op1)) {
242 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
243 if (PN->getParent() == L->getHeader()
244 && isa<ConstantInt>(Op0)) {
254 // Find loop's exit condition and associated induction variable.
255 void LoopIndexSplit::findLoopConditionals() {
257 BasicBlock *ExitBlock = NULL;
259 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
262 if (!L->isLoopExit(BB))
272 // If exit block's terminator is conditional branch inst then we have found
274 BranchInst *BR = dyn_cast<BranchInst>(ExitBlock->getTerminator());
275 if (!BR || BR->isUnconditional())
278 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
284 // Exit condition's one operand is loop invariant exit value and second
285 // operand is SCEVAddRecExpr based on induction variable.
286 Value *V0 = CI->getOperand(0);
287 Value *V1 = CI->getOperand(1);
289 SCEVHandle SH0 = SE->getSCEV(V0);
290 SCEVHandle SH1 = SE->getSCEV(V1);
292 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
296 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
302 ExitCondition = NULL;
304 BasicBlock *Preheader = L->getLoopPreheader();
305 StartValue = IndVar->getIncomingValueForBlock(Preheader);
309 /// Find condition inside a loop that is suitable candidate for index split.
310 void LoopIndexSplit::findSplitCondition() {
313 // Check all basic block's terminators.
315 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
319 // If this basic block does not terminate in a conditional branch
320 // then terminator is not a suitable split condition.
321 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
325 if (BR->isUnconditional())
328 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
329 if (!CI || CI == ExitCondition)
332 // If one operand is loop invariant and second operand is SCEVAddRecExpr
333 // based on induction variable then CI is a candidate split condition.
334 Value *V0 = CI->getOperand(0);
335 Value *V1 = CI->getOperand(1);
337 SCEVHandle SH0 = SE->getSCEV(V0);
338 SCEVHandle SH1 = SE->getSCEV(V1);
340 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
342 SD.SplitCondition = CI;
343 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
345 SplitData.push_back(SD);
347 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
348 if (IndVarIncrement && IndVarIncrement == Insn)
349 SplitData.push_back(SD);
352 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
354 SD.SplitCondition = CI;
355 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
357 SplitData.push_back(SD);
359 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
360 if (IndVarIncrement && IndVarIncrement == Insn)
361 SplitData.push_back(SD);
367 /// processOneIterationLoop - Current loop L contains compare instruction
368 /// that compares induction variable, IndVar, against loop invariant. If
369 /// entire (i.e. meaningful) loop body is dominated by this compare
370 /// instruction then loop body is executed only once. In such case eliminate
371 /// loop structure surrounding this loop body. For example,
372 /// for (int i = start; i < end; ++i) {
373 /// if ( i == somevalue) {
377 /// can be transformed into
378 /// if (somevalue >= start && somevalue < end) {
382 bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
384 BasicBlock *Header = L->getHeader();
386 // First of all, check if SplitCondition dominates entire loop body
389 // If SplitCondition is not in loop header then this loop is not suitable
390 // for this transformation.
391 if (SD.SplitCondition->getParent() != Header)
394 // If loop header includes loop variant instruction operands then
395 // this loop may not be eliminated.
396 if (!safeHeader(SD, Header))
399 // If Exit block includes loop variant instructions then this
400 // loop may not be eliminated.
401 if (!safeExitBlock(SD, ExitCondition->getParent()))
406 // As a first step to break this loop, remove Latch to Header edge.
407 BasicBlock *Latch = L->getLoopLatch();
408 BasicBlock *LatchSucc = NULL;
409 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
412 Header->removePredecessor(Latch);
413 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
418 BR->setUnconditionalDest(LatchSucc);
420 BasicBlock *Preheader = L->getLoopPreheader();
421 Instruction *Terminator = Header->getTerminator();
422 StartValue = IndVar->getIncomingValueForBlock(Preheader);
424 // Replace split condition in header.
426 // SplitCondition : icmp eq i32 IndVar, SplitValue
428 // c1 = icmp uge i32 SplitValue, StartValue
429 // c2 = icmp ult i32 vSplitValue, ExitValue
431 bool SignedPredicate = ExitCondition->isSignedPredicate();
432 Instruction *C1 = new ICmpInst(SignedPredicate ?
433 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
434 SD.SplitValue, StartValue, "lisplit",
436 Instruction *C2 = new ICmpInst(SignedPredicate ?
437 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
439 ExitCondition->getOperand(ExitValueNum), "lisplit",
441 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
443 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
444 SD.SplitCondition->eraseFromParent();
446 // Now, clear latch block. Remove instructions that are responsible
447 // to increment induction variable.
448 Instruction *LTerminator = Latch->getTerminator();
449 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
453 if (isa<PHINode>(I) || I == LTerminator)
456 I->replaceAllUsesWith(UndefValue::get(I->getType()));
457 I->eraseFromParent();
460 LPM->deleteLoopFromQueue(L);
462 // Update Dominator Info.
463 // Only CFG change done is to remove Latch to Header edge. This
464 // does not change dominator tree because Latch did not dominate
466 if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) {
467 DominanceFrontier::iterator HeaderDF = DF->find(Header);
468 if (HeaderDF != DF->end())
469 DF->removeFromFrontier(HeaderDF, Header);
471 DominanceFrontier::iterator LatchDF = DF->find(Latch);
472 if (LatchDF != DF->end())
473 DF->removeFromFrontier(LatchDF, Header);
478 // If loop header includes loop variant instruction operands then
479 // this loop can not be eliminated. This is used by processOneIterationLoop().
480 bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
482 Instruction *Terminator = Header->getTerminator();
483 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
487 // PHI Nodes are OK. FIXME : Handle last value assignments.
491 // SplitCondition itself is OK.
492 if (I == SD.SplitCondition)
495 // Induction variable is OK.
499 // Induction variable increment is OK.
500 if (I == IndVarIncrement)
503 // Terminator is also harmless.
507 // Otherwise we have a instruction that may not be safe.
514 // If Exit block includes loop variant instructions then this
515 // loop may not be eliminated. This is used by processOneIterationLoop().
516 bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
518 for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
522 // PHI Nodes are OK. FIXME : Handle last value assignments.
526 // Induction variable increment is OK.
527 if (IndVarIncrement && IndVarIncrement == I)
530 // Check if I is induction variable increment instruction.
531 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
533 Value *Op0 = I->getOperand(0);
534 Value *Op1 = I->getOperand(1);
536 ConstantInt *CI = NULL;
538 if ((PN = dyn_cast<PHINode>(Op0))) {
539 if ((CI = dyn_cast<ConstantInt>(Op1)))
542 if ((PN = dyn_cast<PHINode>(Op1))) {
543 if ((CI = dyn_cast<ConstantInt>(Op0)))
547 if (IndVarIncrement && PN == IndVar && CI->isOne())
551 // I is an Exit condition if next instruction is block terminator.
552 // Exit condition is OK if it compares loop invariant exit value,
553 // which is checked below.
554 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
555 if (EC == ExitCondition)
559 if (I == ExitBlock->getTerminator())
562 // Otherwise we have instruction that may not be safe.
566 // We could not find any reason to consider ExitBlock unsafe.
570 /// Find cost of spliting loop L. Cost is measured in terms of size growth.
571 /// Size is growth is calculated based on amount of code duplicated in second
573 unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
576 BasicBlock *SDBlock = SD.SplitCondition->getParent();
577 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
580 // If a block is not dominated by split condition block then
581 // it must be duplicated in both loops.
582 if (!DT->dominates(SDBlock, BB))
589 /// removeBlocks - Remove basic block BB and all blocks dominated by BB.
590 void LoopIndexSplit::removeBlocks(BasicBlock *InBB) {
592 SmallVector<BasicBlock *, 8> WorkList;
593 WorkList.push_back(InBB);
594 while (!WorkList.empty()) {
595 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
597 // First process all successor
598 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
599 BasicBlock *SuccBB = *SI;
600 if (DT->dominates(BB, SuccBB)) {
601 WorkList.push_back(SuccBB);
605 // If SuccBB is not dominated by BB then it is not removed, however remove
606 // any PHI incoming edge from BB.
607 for(BasicBlock::iterator SBI = SuccBB->begin(), SBE = SuccBB->end();
609 if (PHINode *PN = dyn_cast<PHINode>(SBI))
610 PN->removeIncomingValue(BB);
617 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
619 Instruction *I = BBI;
620 I->replaceAllUsesWith(UndefValue::get(I->getType()));
621 I->eraseFromParent();
624 BB->eraseFromParent();
628 bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
630 BasicBlock *Preheader = L->getLoopPreheader();
632 // True loop is original loop. False loop is cloned loop.
634 bool SignedPredicate = ExitCondition->isSignedPredicate();
635 //[*] Calculate True loop's new Exit Value in loop preheader.
636 // TLExitValue = min(SplitValue, ExitValue)
637 //[*] Calculate False loop's new Start Value in loop preheader.
638 // FLStartValue = min(SplitValue, TrueLoop.StartValue)
639 Value *TLExitValue = NULL;
640 Value *FLStartValue = NULL;
641 if (isa<ConstantInt>(SD.SplitValue)) {
642 TLExitValue = SD.SplitValue;
643 FLStartValue = SD.SplitValue;
646 Value *C1 = new ICmpInst(SignedPredicate ?
647 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
649 ExitCondition->getOperand(ExitValueNum),
651 Preheader->getTerminator());
652 TLExitValue = new SelectInst(C1, SD.SplitValue,
653 ExitCondition->getOperand(ExitValueNum),
654 "lsplit.ev", Preheader->getTerminator());
656 Value *C2 = new ICmpInst(SignedPredicate ?
657 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
658 SD.SplitValue, StartValue, "lsplit.sv",
659 Preheader->getTerminator());
660 FLStartValue = new SelectInst(C2, SD.SplitValue, StartValue,
661 "lsplit.sv", Preheader->getTerminator());
664 //[*] Clone loop. Avoid true destination of split condition and
665 // the blocks dominated by true destination.
666 DenseMap<const Value *, Value *> ValueMap;
667 Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this);
668 BasicBlock *FalseHeader = FalseLoop->getHeader();
670 //[*] True loop's exit edge enters False loop.
671 PHINode *IndVarClone = cast<PHINode>(ValueMap[IndVar]);
672 BasicBlock *ExitBlock = ExitCondition->getParent();
673 BranchInst *ExitInsn = dyn_cast<BranchInst>(ExitBlock->getTerminator());
674 assert (ExitInsn && "Unable to find suitable loop exit branch");
675 BasicBlock *ExitDest = ExitInsn->getSuccessor(1);
677 for (BasicBlock::iterator BI = FalseHeader->begin(), BE = FalseHeader->end();
679 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
680 PN->removeIncomingValue(Preheader);
681 if (PN == IndVarClone)
682 PN->addIncoming(FLStartValue, ExitBlock);
683 // else { FIXME : Handl last value assignments.}
689 if (L->contains(ExitDest)) {
690 ExitDest = ExitInsn->getSuccessor(0);
691 ExitInsn->setSuccessor(0, FalseHeader);
693 ExitInsn->setSuccessor(1, FalseHeader);
695 assert (!L->contains(ExitDest) && " Unable to find exit edge destination");
697 //[*] Split Exit Edge.
698 SplitEdge(ExitBlock, FalseHeader, this);
700 //[*] Eliminate split condition's false branch from True loop.
701 // Update true loop dom info (FIXME).
702 BasicBlock *SplitBlock = SD.SplitCondition->getParent();
703 BranchInst *BR = cast<BranchInst>(SplitBlock->getTerminator());
704 BasicBlock *FBB = BR->getSuccessor(1);
705 BR->setUnconditionalDest(BR->getSuccessor(0));
708 //[*] Update True loop's exit value using new exit value.
709 ExitCondition->setOperand(ExitValueNum, TLExitValue);
711 //[*] Eliminate split condition's true branch in False loop CFG.
712 // Update false loop dom info (FXME).
713 BasicBlock *FSplitBlock = cast<BasicBlock>(ValueMap[SplitBlock]);
714 BranchInst *FBR = cast<BranchInst>(FSplitBlock->getTerminator());
715 BasicBlock *TBB = FBR->getSuccessor(0);
716 FBR->setUnconditionalDest(FBR->getSuccessor(1));
719 //[*] Update dom info in general (FIXME).