X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FScalar%2FLoopIndexSplit.cpp;h=5f9d3703da99d610a73d568b5267b79c35e3d830;hb=923327267949b537d7a2fdad5b7a919bd90ce085;hp=a817ec5b1a4841186d130ebba2b8a99c5988a284;hpb=be97c986e18d188e5b56406fa0676243e8835063;p=oota-llvm.git diff --git a/lib/Transforms/Scalar/LoopIndexSplit.cpp b/lib/Transforms/Scalar/LoopIndexSplit.cpp index a817ec5b1a4..5f9d3703da9 100644 --- a/lib/Transforms/Scalar/LoopIndexSplit.cpp +++ b/lib/Transforms/Scalar/LoopIndexSplit.cpp @@ -2,44 +2,84 @@ // // 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 is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // -// This file implements Loop Index Splitting Pass. +// This file implements Loop Index Splitting Pass. This pass handles three +// kinds of loops. +// +// [1] A loop may be eliminated if the body is executed exactly once. +// For example, +// +// for (i = 0; i < N; ++i) { +// if (i == X) { +// body; +// } +// } +// +// is transformed to +// +// i = X; +// body; +// +// [2] A loop's iteration space may be shrunk if the loop body is executed +// for a proper sub-range of the loop's iteration space. For example, +// +// for (i = 0; i < N; ++i) { +// if (i > A && i < B) { +// ... +// } +// } +// +// is transformed to iterators from A to B, if A > 0 and B < N. +// +// [3] A loop may be split if the loop body is dominated by a branch. +// For example, +// +// for (i = LB; i < UB; ++i) { if (i < SV) A; else B; } +// +// is transformed into +// +// AEV = BSV = SV +// for (i = LB; i < min(UB, AEV); ++i) +// A; +// for (i = max(LB, BSV); i < UB; ++i); +// B; // //===----------------------------------------------------------------------===// #define DEBUG_TYPE "loop-index-split" - #include "llvm/Transforms/Scalar.h" +#include "llvm/IntrinsicInst.h" +#include "llvm/LLVMContext.h" #include "llvm/Analysis/LoopPass.h" -#include "llvm/Analysis/ScalarEvolutionExpander.h" +#include "llvm/Analysis/ScalarEvolution.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/Transforms/Utils/Local.h" #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/Statistic.h" using namespace llvm; -STATISTIC(NumIndexSplit, "Number of loops index split"); +STATISTIC(NumIndexSplit, "Number of loop index split"); +STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split"); +STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted"); namespace { - class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass { - + class LoopIndexSplit : public LoopPass { public: static char ID; // Pass ID, replacement for typeid - LoopIndexSplit() : LoopPass((intptr_t)&ID) {} + LoopIndexSplit() : LoopPass(&ID) {} // Index split Loop L. Return true if loop is split. bool runOnLoop(Loop *L, LPPassManager &LPM); void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); AU.addPreserved(); AU.addRequiredID(LCSSAID); AU.addPreservedID(LCSSAID); @@ -54,127 +94,118 @@ namespace { } private: - - class SplitInfo { - public: - SplitInfo() : SplitValue(NULL), SplitCondition(NULL), - UseTrueBranchFirst(true), A_ExitValue(NULL), - B_StartValue(NULL) {} - - // Induction variable's range is split at this value. - Value *SplitValue; - - // This compare instruction compares IndVar against SplitValue. - ICmpInst *SplitCondition; - - // True if after loop index split, first loop will execute split condition's - // true branch. - bool UseTrueBranchFirst; - - // Exit value for first loop after loop split. - Value *A_ExitValue; - - // Start value for second loop after loop split. - Value *B_StartValue; - - // Clear split info. - void clear() { - SplitValue = NULL; - SplitCondition = NULL; - UseTrueBranchFirst = true; - A_ExitValue = NULL; - B_StartValue = NULL; - } - - }; + /// processOneIterationLoop -- Eliminate loop if loop body is executed + /// only once. For example, + /// for (i = 0; i < N; ++i) { + /// if ( i == X) { + /// ... + /// } + /// } + /// + bool processOneIterationLoop(); + + // -- Routines used by updateLoopIterationSpace(); + + /// updateLoopIterationSpace -- Update loop's iteration space if loop + /// body is executed for certain IV range only. For example, + /// + /// for (i = 0; i < N; ++i) { + /// if ( i > A && i < B) { + /// ... + /// } + /// } + /// is transformed to iterators from A to B, if A > 0 and B < N. + /// + bool updateLoopIterationSpace(); + + /// restrictLoopBound - Op dominates loop body. Op compares an IV based value + /// with a loop invariant value. Update loop's lower and upper bound based on + /// the loop invariant value. + bool restrictLoopBound(ICmpInst &Op); + + // --- Routines used by splitLoop(). --- / + + bool splitLoop(); + + /// removeBlocks - Remove basic block DeadBB and all blocks dominated by + /// DeadBB. This routine is used to remove split condition's dead branch, + /// dominated by DeadBB. LiveBB dominates split conidition's other branch. + void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB); - 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); + /// moveExitCondition - Move exit condition EC into split condition block. + void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, + BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC, + PHINode *IV, Instruction *IVAdd, Loop *LP, + unsigned); - /// If loop header includes loop variant instruction operands then - /// this loop may not be eliminated. - bool safeHeader(SplitInfo &SD, BasicBlock *BB); - - /// If Exiting block includes loop variant instructions then this - /// loop may not be eliminated. - bool safeExitingBlock(SplitInfo &SD, BasicBlock *BB); - - /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB. - /// This routine is used to remove split condition's dead branch, dominated by - /// DeadBB. LiveBB dominates split conidition's other branch. - void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB); - - /// safeSplitCondition - Return true if it is possible to - /// split loop using given split condition. - bool safeSplitCondition(SplitInfo &SD); - - /// calculateLoopBounds - ALoop exit value and BLoop start values are calculated - /// based on split value. - void calculateLoopBounds(SplitInfo &SD); - - /// splitLoop - Split current loop L in two loops using split information - /// SD. Update dominator information. Maintain LCSSA form. - bool splitLoop(SplitInfo &SD); - - void initialize() { - IndVar = NULL; - IndVarIncrement = NULL; - ExitCondition = NULL; - StartValue = NULL; - ExitValueNum = 0; - SplitData.clear(); - } + /// updatePHINodes - CFG has been changed. + /// Before + /// - ExitBB's single predecessor was Latch + /// - Latch's second successor was Header + /// Now + /// - ExitBB's single predecessor was Header + /// - Latch's one and only successor was Header + /// + /// Update ExitBB PHINodes' to reflect this change. + void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, + BasicBlock *Header, + PHINode *IV, Instruction *IVIncrement, Loop *LP); + + // --- Utility routines --- / + + /// cleanBlock - A block is considered clean if all non terminal + /// instructions are either PHINodes or IV based values. + bool cleanBlock(BasicBlock *BB); + + /// IVisLT - If Op is comparing IV based value with an loop invariant and + /// IV based value is less than the loop invariant then return the loop + /// invariant. Otherwise return NULL. + Value * IVisLT(ICmpInst &Op); + + /// IVisLE - If Op is comparing IV based value with an loop invariant and + /// IV based value is less than or equal to the loop invariant then + /// return the loop invariant. Otherwise return NULL. + Value * IVisLE(ICmpInst &Op); + + /// IVisGT - If Op is comparing IV based value with an loop invariant and + /// IV based value is greater than the loop invariant then return the loop + /// invariant. Otherwise return NULL. + Value * IVisGT(ICmpInst &Op); + + /// IVisGE - If Op is comparing IV based value with an loop invariant and + /// IV based value is greater than or equal to the loop invariant then + /// return the loop invariant. Otherwise return NULL. + Value * IVisGE(ICmpInst &Op); private: - // Current Loop. + // Current Loop information. Loop *L; LPPassManager *LPM; LoopInfo *LI; - ScalarEvolution *SE; DominatorTree *DT; DominanceFrontier *DF; - SmallVector 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; + ICmpInst *SplitCondition; + Value *IVStartValue; + Value *IVExitValue; + Instruction *IVIncrement; + SmallPtrSet IVBasedValues; }; - - char LoopIndexSplit::ID = 0; - RegisterPass X ("loop-index-split", "Index Split Loops"); } -LoopPass *llvm::createLoopIndexSplitPass() { +char LoopIndexSplit::ID = 0; +static RegisterPass +X("loop-index-split", "Index Split Loops"); + +Pass *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; @@ -182,335 +213,247 @@ bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) { if (!L->getSubLoops().empty()) return false; - SE = &getAnalysis(); DT = &getAnalysis(); LI = &getAnalysis(); DF = &getAnalysis(); - initialize(); + // Initialize loop data. + IndVar = L->getCanonicalInductionVariable(); + if (!IndVar) return false; - findLoopConditionals(); + bool P1InLoop = L->contains(IndVar->getIncomingBlock(1)); + IVStartValue = IndVar->getIncomingValue(!P1InLoop); + IVIncrement = dyn_cast(IndVar->getIncomingValue(P1InLoop)); + if (!IVIncrement) return false; + + IVBasedValues.clear(); + IVBasedValues.insert(IndVar); + IVBasedValues.insert(IVIncrement); + for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); + I != E; ++I) + for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); + BI != BE; ++BI) { + if (BinaryOperator *BO = dyn_cast(BI)) + if (BO != IVIncrement + && (BO->getOpcode() == Instruction::Add + || BO->getOpcode() == Instruction::Sub)) + if (IVBasedValues.count(BO->getOperand(0)) + && L->isLoopInvariant(BO->getOperand(1))) + IVBasedValues.insert(BO); + } - if (!ExitCondition) + // Reject loop if loop exit condition is not suitable. + BasicBlock *ExitingBlock = L->getExitingBlock(); + if (!ExitingBlock) return false; - - findSplitCondition(); - - if (SplitData.empty()) + BranchInst *EBR = dyn_cast(ExitingBlock->getTerminator()); + if (!EBR) return false; + ExitCondition = dyn_cast(EBR->getCondition()); + if (!ExitCondition) return false; + if (ExitingBlock != L->getLoopLatch()) return false; + IVExitValue = ExitCondition->getOperand(1); + if (!L->isLoopInvariant(IVExitValue)) + IVExitValue = ExitCondition->getOperand(0); + if (!L->isLoopInvariant(IVExitValue)) return false; - - // First see if it is possible to eliminate loop itself or not. - for (SmallVector::iterator SI = SplitData.begin(), - E = SplitData.end(); SI != E;) { - 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; - } else { - SmallVector::iterator Delete_SI = SI; - ++SI; - SplitData.erase(Delete_SI); - } - } else - ++SI; - } - - if (SplitData.empty()) + if (!IVBasedValues.count( + ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0)))) return false; - // Split most profitiable condition. - // FIXME : Implement cost analysis. - unsigned MostProfitableSDIndex = 0; - Changed = splitLoop(SplitData[MostProfitableSDIndex]); - - if (Changed) - ++NumIndexSplit; - - return Changed; -} + // If start value is more then exit value where induction variable + // increments by 1 then we are potentially dealing with an infinite loop. + // Do not index split this loop. + if (ConstantInt *SV = dyn_cast(IVStartValue)) + if (ConstantInt *EV = dyn_cast(IVExitValue)) + if (SV->getSExtValue() > EV->getSExtValue()) + return false; -/// 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(V); - if (!I) - return; - - // Check if I is a phi node from loop header or not. - if (PHINode *PN = dyn_cast(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(Op0)) { - if (PN->getParent() == L->getHeader() - && isa(Op1)) { - IndVar = PN; - IndVarIncrement = I; - return; - } - } - - if (PHINode *PN = dyn_cast(Op1)) { - if (PN->getParent() == L->getHeader() - && isa(Op0)) { - IndVar = PN; - IndVarIncrement = I; - return; - } - } - - return; -} - -// Find loop's exit condition and associated induction variable. -void LoopIndexSplit::findLoopConditionals() { - - BasicBlock *ExitingBlock = NULL; + if (processOneIterationLoop()) + return true; - for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); - I != E; ++I) { - BasicBlock *BB = *I; - if (!L->isLoopExit(BB)) - continue; - if (ExitingBlock) - return; - ExitingBlock = BB; - } + if (updateLoopIterationSpace()) + return true; - if (!ExitingBlock) - return; - - // If exiting block is neither loop header nor loop latch then this loop is - // not suitable. - if (ExitingBlock != L->getHeader() && ExitingBlock != L->getLoopLatch()) - return; - - // If exit block's terminator is conditional branch inst then we have found - // exit condition. - BranchInst *BR = dyn_cast(ExitingBlock->getTerminator()); - if (!BR || BR->isUnconditional()) - return; - - ICmpInst *CI = dyn_cast(BR->getCondition()); - if (!CI) - return; - - // FIXME - if (CI->getPredicate() == ICmpInst::ICMP_SGT - || CI->getPredicate() == ICmpInst::ICMP_UGT - || CI->getPredicate() == ICmpInst::ICMP_SGE - || CI->getPredicate() == ICmpInst::ICMP_UGE) - 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(SH1)) { - ExitValueNum = 0; - findIndVar(V1, L); - } - else if (SH1->isLoopInvariant(L) && isa(SH0)) { - ExitValueNum = 1; - findIndVar(V0, L); - } + if (splitLoop()) + return true; - if (!IndVar) - ExitCondition = NULL; - else if (IndVar) { - BasicBlock *Preheader = L->getLoopPreheader(); - StartValue = IndVar->getIncomingValueForBlock(Preheader); - } + return false; } -/// Find condition inside a loop that is suitable candidate for index split. -void LoopIndexSplit::findSplitCondition() { - - SplitInfo SD; - // Check all basic block's terminators. +// --- Helper routines --- +// isUsedOutsideLoop - Returns true iff V is used outside the loop L. +static bool isUsedOutsideLoop(Value *V, Loop *L) { + for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) + if (!L->contains(cast(*UI)->getParent())) + return true; + return false; +} - for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); - I != E; ++I) { - BasicBlock *BB = *I; +// Return V+1 +static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt, + LLVMContext &Context) { + Constant *One = ConstantInt::get(V->getType(), 1, Sign); + return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt); +} - // If this basic block does not terminate in a conditional branch - // then terminator is not a suitable split condition. - BranchInst *BR = dyn_cast(BB->getTerminator()); - if (!BR) - continue; - - if (BR->isUnconditional()) - continue; +// Return V-1 +static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt, + LLVMContext &Context) { + Constant *One = ConstantInt::get(V->getType(), 1, Sign); + return BinaryOperator::CreateSub(V, One, "lsp", InsertPt); +} - ICmpInst *CI = dyn_cast(BR->getCondition()); - if (!CI || CI == ExitCondition) - return; - - if (CI->getPredicate() == ICmpInst::ICMP_NE) - return; - - // If split condition predicate is GT or GE then first execute - // false branch of split condition. - if (CI->getPredicate() != ICmpInst::ICMP_ULT - && CI->getPredicate() != ICmpInst::ICMP_SLT - && CI->getPredicate() != ICmpInst::ICMP_ULE - && CI->getPredicate() != ICmpInst::ICMP_SLE) - SD.UseTrueBranchFirst = false; - - // 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(SH1)) { - SD.SplitValue = V0; - SD.SplitCondition = CI; - if (PHINode *PN = dyn_cast(V1)) { - if (PN == IndVar) - SplitData.push_back(SD); - } - else if (Instruction *Insn = dyn_cast(V1)) { - if (IndVarIncrement && IndVarIncrement == Insn) - SplitData.push_back(SD); - } - } - else if (SH1->isLoopInvariant(L) && isa(SH0)) { - SD.SplitValue = V1; - SD.SplitCondition = CI; - if (PHINode *PN = dyn_cast(V0)) { - if (PN == IndVar) - SplitData.push_back(SD); - } - else if (Instruction *Insn = dyn_cast(V0)) { - if (IndVarIncrement && IndVarIncrement == Insn) - SplitData.push_back(SD); - } - } - } +// Return min(V1, V1) +static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { + + Value *C = new ICmpInst(InsertPt, + Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, + V1, V2, "lsp"); + return SelectInst::Create(C, V1, V2, "lsp", InsertPt); } -/// 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) { +// Return max(V1, V2) +static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { + + Value *C = new ICmpInst(InsertPt, + Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, + V1, V2, "lsp"); + return SelectInst::Create(C, V2, V1, "lsp", InsertPt); +} +/// processOneIterationLoop -- Eliminate loop if loop body is executed +/// only once. For example, +/// for (i = 0; i < N; ++i) { +/// if ( i == X) { +/// ... +/// } +/// } +/// +bool LoopIndexSplit::processOneIterationLoop() { + SplitCondition = NULL; + BasicBlock *Latch = L->getLoopLatch(); 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)) + BranchInst *BR = dyn_cast(Header->getTerminator()); + if (!BR) return false; + if (!isa(Latch->getTerminator())) return false; + if (BR->isUnconditional()) return false; + SplitCondition = dyn_cast(BR->getCondition()); + if (!SplitCondition) return false; + if (SplitCondition == ExitCondition) return false; + if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false; + if (BR->getOperand(1) != Latch) return false; + if (!IVBasedValues.count(SplitCondition->getOperand(0)) + && !IVBasedValues.count(SplitCondition->getOperand(1))) return false; - // If Exiting block includes loop variant instructions then this - // loop may not be eliminated. - if (!safeExitingBlock(SD, ExitCondition->getParent())) + // If IV is used outside the loop then this loop traversal is required. + // FIXME: Calculate and use last IV value. + if (isUsedOutsideLoop(IVIncrement, L)) return false; - // Update CFG. + // If BR operands are not IV or not loop invariants then skip this loop. + Value *OPV = SplitCondition->getOperand(0); + Value *SplitValue = SplitCondition->getOperand(1); + if (!L->isLoopInvariant(SplitValue)) + std::swap(OPV, SplitValue); + if (!L->isLoopInvariant(SplitValue)) + return false; + Instruction *OPI = dyn_cast(OPV); + if (!OPI) + return false; + if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L)) + return false; + Value *StartValue = IVStartValue; + Value *ExitValue = IVExitValue;; + + if (OPV != IndVar) { + // If BR operand is IV based then use this operand to calculate + // effective conditions for loop body. + BinaryOperator *BOPV = dyn_cast(OPV); + if (!BOPV) + return false; + if (BOPV->getOpcode() != Instruction::Add) + return false; + StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR); + ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR); + } - // Replace index variable with split value in loop body. Loop body is executed - // only when index variable is equal to split value. - IndVar->replaceAllUsesWith(SD.SplitValue); + if (!cleanBlock(Header)) + return false; - // Remove Latch to Header edge. - BasicBlock *Latch = L->getLoopLatch(); - BasicBlock *LatchSucc = NULL; - BranchInst *BR = dyn_cast(Latch->getTerminator()); - if (!BR) + if (!cleanBlock(Latch)) return false; - Header->removePredecessor(Latch); - for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); - SI != E; ++SI) { - if (Header != *SI) - LatchSucc = *SI; + + // If the merge point for BR is not loop latch then skip this loop. + if (BR->getSuccessor(0) != Latch) { + DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); + assert (DF0 != DF->end() && "Unable to find dominance frontier"); + if (!DF0->second.count(Latch)) + return false; } - BR->setUnconditionalDest(LatchSucc); + + if (BR->getSuccessor(1) != Latch) { + DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); + assert (DF1 != DF->end() && "Unable to find dominance frontier"); + if (!DF1->second.count(Latch)) + return false; + } + + // Now, Current loop L contains compare instruction + // that compares induction variable, IndVar, against loop invariant. And + // entire (i.e. meaningful) loop body is dominated by this compare + // instruction. 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 + // } - Instruction *Terminator = Header->getTerminator(); - Value *ExitValue = ExitCondition->getOperand(ExitValueNum); + // Replace index variable with split value in loop body. Loop body is executed + // only when index variable is equal to split value. + IndVar->replaceAllUsesWith(SplitValue); // 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 + // c2 = icmp ult i32 SplitValue, ExitValue // and i32 c1, c2 - bool SignedPredicate = ExitCondition->isSignedPredicate(); - Instruction *C1 = new ICmpInst(SignedPredicate ? + Instruction *C1 = new ICmpInst(BR, ExitCondition->isSignedPredicate() ? 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(I) || I == LTerminator) - continue; + SplitValue, StartValue, "lisplit"); - if (I == IndVarIncrement) - I->replaceAllUsesWith(ExitValue); - else - I->replaceAllUsesWith(UndefValue::get(I->getType())); - I->eraseFromParent(); + CmpInst::Predicate C2P = ExitCondition->getPredicate(); + BranchInst *LatchBR = cast(Latch->getTerminator()); + if (LatchBR->getOperand(1) != Header) + C2P = CmpInst::getInversePredicate(C2P); + Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit"); + Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR); + + SplitCondition->replaceAllUsesWith(NSplitCond); + SplitCondition->eraseFromParent(); + + // Remove Latch to Header edge. + BasicBlock *LatchSucc = NULL; + Header->removePredecessor(Latch); + for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); + SI != E; ++SI) { + if (Header != *SI) + LatchSucc = *SI; } + // Clean up latch block. + Value *LatchBRCond = LatchBR->getCondition(); + LatchBR->setUnconditionalDest(LatchSucc); + RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond); + LPM->deleteLoopFromQueue(L); // Update Dominator Info. @@ -526,99 +469,183 @@ bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) { if (LatchDF != DF->end()) DF->removeFromFrontier(LatchDF, Header); } + + ++NumIndexSplitRemoved; 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(I)) - continue; - - // SplitCondition itself is OK. - if (I == SD.SplitCondition) - continue; - - // Induction variable is OK. - if (I == IndVar) - continue; +/// restrictLoopBound - Op dominates loop body. Op compares an IV based value +/// with a loop invariant value. Update loop's lower and upper bound based on +/// the loop invariant value. +bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) { + bool Sign = Op.isSignedPredicate(); + Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); + + if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { + BranchInst *EBR = + cast(ExitCondition->getParent()->getTerminator()); + ExitCondition->setPredicate(ExitCondition->getInversePredicate()); + BasicBlock *T = EBR->getSuccessor(0); + EBR->setSuccessor(0, EBR->getSuccessor(1)); + EBR->setSuccessor(1, T); + } - // Induction variable increment is OK. - if (I == IndVarIncrement) - continue; + LLVMContext &Context = Op.getContext(); + + // New upper and lower bounds. + Value *NLB = NULL; + Value *NUB = NULL; + if (Value *V = IVisLT(Op)) { + // Restrict upper bound. + if (IVisLE(*ExitCondition)) + V = getMinusOne(V, Sign, PHTerm, Context); + NUB = getMin(V, IVExitValue, Sign, PHTerm); + } else if (Value *V = IVisLE(Op)) { + // Restrict upper bound. + if (IVisLT(*ExitCondition)) + V = getPlusOne(V, Sign, PHTerm, Context); + NUB = getMin(V, IVExitValue, Sign, PHTerm); + } else if (Value *V = IVisGT(Op)) { + // Restrict lower bound. + V = getPlusOne(V, Sign, PHTerm, Context); + NLB = getMax(V, IVStartValue, Sign, PHTerm); + } else if (Value *V = IVisGE(Op)) + // Restrict lower bound. + NLB = getMax(V, IVStartValue, Sign, PHTerm); + + if (!NLB && !NUB) + return false; - // Terminator is also harmless. - if (I == Terminator) - continue; + if (NLB) { + unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader()); + IndVar->setIncomingValue(i, NLB); + } - // Otherwise we have a instruction that may not be safe. - return false; + if (NUB) { + unsigned i = (ExitCondition->getOperand(0) != IVExitValue); + ExitCondition->setOperand(i, NUB); } - return true; } -// If Exiting block includes loop variant instructions then this -// loop may not be eliminated. This is used by processOneIterationLoop(). -bool LoopIndexSplit::safeExitingBlock(SplitInfo &SD, - BasicBlock *ExitingBlock) { +/// updateLoopIterationSpace -- Update loop's iteration space if loop +/// body is executed for certain IV range only. For example, +/// +/// for (i = 0; i < N; ++i) { +/// if ( i > A && i < B) { +/// ... +/// } +/// } +/// is transformed to iterators from A to B, if A > 0 and B < N. +/// +bool LoopIndexSplit::updateLoopIterationSpace() { + SplitCondition = NULL; + if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE + || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) + return false; + BasicBlock *Latch = L->getLoopLatch(); + BasicBlock *Header = L->getHeader(); + BranchInst *BR = dyn_cast(Header->getTerminator()); + if (!BR) return false; + if (!isa(Latch->getTerminator())) return false; + if (BR->isUnconditional()) return false; + BinaryOperator *AND = dyn_cast(BR->getCondition()); + if (!AND) return false; + if (AND->getOpcode() != Instruction::And) return false; + ICmpInst *Op0 = dyn_cast(AND->getOperand(0)); + ICmpInst *Op1 = dyn_cast(AND->getOperand(1)); + if (!Op0 || !Op1) + return false; + IVBasedValues.insert(AND); + IVBasedValues.insert(Op0); + IVBasedValues.insert(Op1); + if (!cleanBlock(Header)) return false; + BasicBlock *ExitingBlock = ExitCondition->getParent(); + if (!cleanBlock(ExitingBlock)) return false; + + // If the merge point for BR is not loop latch then skip this loop. + if (BR->getSuccessor(0) != Latch) { + DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); + assert (DF0 != DF->end() && "Unable to find dominance frontier"); + if (!DF0->second.count(Latch)) + return false; + } + + if (BR->getSuccessor(1) != Latch) { + DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); + assert (DF1 != DF->end() && "Unable to find dominance frontier"); + if (!DF1->second.count(Latch)) + return false; + } + + // Verify that loop exiting block has only two predecessor, where one pred + // is split condition block. The other predecessor will become exiting block's + // dominator after CFG is updated. TODO : Handle CFG's where exiting block has + // more then two predecessors. This requires extra work in updating dominator + // information. + BasicBlock *ExitingBBPred = NULL; + for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock); + PI != PE; ++PI) { + BasicBlock *BB = *PI; + if (Header == BB) + continue; + if (ExitingBBPred) + return false; + else + ExitingBBPred = BB; + } - for (BasicBlock::iterator BI = ExitingBlock->begin(), - BE = ExitingBlock->end(); BI != BE; ++BI) { - Instruction *I = BI; + if (!restrictLoopBound(*Op0)) + return false; - // PHI Nodes are OK. - if (isa(I)) - continue; + if (!restrictLoopBound(*Op1)) + return false; - // Induction variable increment is OK. - if (IndVarIncrement && IndVarIncrement == I) + // Update CFG. + if (BR->getSuccessor(0) == ExitingBlock) + BR->setUnconditionalDest(BR->getSuccessor(1)); + else + BR->setUnconditionalDest(BR->getSuccessor(0)); + + AND->eraseFromParent(); + if (Op0->use_empty()) + Op0->eraseFromParent(); + if (Op1->use_empty()) + Op1->eraseFromParent(); + + // Update domiantor info. Now, ExitingBlock has only one predecessor, + // ExitingBBPred, and it is ExitingBlock's immediate domiantor. + DT->changeImmediateDominator(ExitingBlock, ExitingBBPred); + + BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1); + if (L->contains(ExitBlock)) + ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0); + + // If ExitingBlock is a member of the loop basic blocks' DF list then + // replace ExitingBlock with header and exit block in the DF list + DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock); + for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); + I != E; ++I) { + BasicBlock *BB = *I; + if (BB == Header || BB == ExitingBlock) 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(Op0))) { - if ((CI = dyn_cast(Op1))) - IndVarIncrement = I; - } else - if ((PN = dyn_cast(Op1))) { - if ((CI = dyn_cast(Op0))) - IndVarIncrement = I; + DominanceFrontier::iterator BBDF = DF->find(BB); + DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); + DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); + while (DomSetI != DomSetE) { + DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; + ++DomSetI; + BasicBlock *DFBB = *CurrentItr; + if (DFBB == ExitingBlock) { + BBDF->second.erase(DFBB); + for (DominanceFrontier::DomSetType::iterator + EBI = ExitingBlockDF->second.begin(), + EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI) + BBDF->second.insert(*EBI); } - - 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(I)) { - if (EC == ExitCondition) - continue; - } - - if (I == ExitingBlock->getTerminator()) - continue; - - // Otherwise we have instruction that may not be safe. - return false; } - - // We could not find any reason to consider ExitingBlock unsafe. + NumRestrictBounds++; return true; } @@ -636,7 +663,8 @@ void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP, DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second; for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(), - DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) { + DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) + { BasicBlock *FrontierBB = *DeadBBSetI; FrontierBBs.push_back(FrontierBB); @@ -671,18 +699,21 @@ void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP, E = df_end(DN); DI != E; ++DI) { BasicBlock *BB = DI->getBlock(); WorkList.push_back(BB); - BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy)); + BB->replaceAllUsesWith(UndefValue::get( + Type::getLabelTy(DeadBB->getContext()))); } while (!WorkList.empty()) { BasicBlock *BB = WorkList.back(); WorkList.pop_back(); + LPM->deleteSimpleAnalysisValue(BB, LP); for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end(); - BBI != BBE; ++BBI) { + BBI != BBE; ) { Instruction *I = BBI; + ++BBI; I->replaceAllUsesWith(UndefValue::get(I->getType())); + LPM->deleteSimpleAnalysisValue(I, LP); I->eraseFromParent(); } - LPM->deleteSimpleAnalysisValue(BB, LP); DT->eraseNode(BB); DF->removeBlock(BB); LI->removeBlock(BB); @@ -715,281 +746,258 @@ void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP, } -/// safeSplitCondition - Return true if it is possible to -/// split loop using given split condition. -bool LoopIndexSplit::safeSplitCondition(SplitInfo &SD) { +// moveExitCondition - Move exit condition EC into split condition block CondBB. +void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, + BasicBlock *ExitBB, ICmpInst *EC, + ICmpInst *SC, PHINode *IV, + Instruction *IVAdd, Loop *LP, + unsigned ExitValueNum) { + + BasicBlock *ExitingBB = EC->getParent(); + Instruction *CurrentBR = CondBB->getTerminator(); + + // Move exit condition into split condition block. + EC->moveBefore(CurrentBR); + EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV); + + // Move exiting block's branch into split condition block. Update its branch + // destination. + BranchInst *ExitingBR = cast(ExitingBB->getTerminator()); + ExitingBR->moveBefore(CurrentBR); + BasicBlock *OrigDestBB = NULL; + if (ExitingBR->getSuccessor(0) == ExitBB) { + OrigDestBB = ExitingBR->getSuccessor(1); + ExitingBR->setSuccessor(1, ActiveBB); + } + else { + OrigDestBB = ExitingBR->getSuccessor(0); + ExitingBR->setSuccessor(0, ActiveBB); + } + + // Remove split condition and current split condition branch. + SC->eraseFromParent(); + CurrentBR->eraseFromParent(); + + // Connect exiting block to original destination. + BranchInst::Create(OrigDestBB, ExitingBB); + + // Update PHINodes + updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP); + + // Fix dominator info. + // ExitBB is now dominated by CondBB + DT->changeImmediateDominator(ExitBB, CondBB); + DF->changeImmediateDominator(ExitBB, CondBB, DT); + + // Blocks outside the loop may have been in the dominance frontier of blocks + // inside the condition; this is now impossible because the blocks inside the + // condition no loger dominate the exit. Remove the relevant blocks from + // the dominance frontiers. + for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end(); + I != E; ++I) { + if (*I == CondBB || !DT->dominates(CondBB, *I)) continue; + DominanceFrontier::iterator BBDF = DF->find(*I); + DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); + DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); + while (DomSetI != DomSetE) { + DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; + ++DomSetI; + BasicBlock *DFBB = *CurrentItr; + if (!LP->contains(DFBB)) + BBDF->second.erase(DFBB); + } + } +} - BasicBlock *SplitCondBlock = SD.SplitCondition->getParent(); - - // Unable to handle triange loops at the moment. - // In triangle loop, split condition is in header and one of the - // the split destination is loop latch. If split condition is EQ - // then such loops are already handle in processOneIterationLoop(). - BasicBlock *Latch = L->getLoopLatch(); - BranchInst *SplitTerminator = - cast(SplitCondBlock->getTerminator()); - BasicBlock *Succ0 = SplitTerminator->getSuccessor(0); - BasicBlock *Succ1 = SplitTerminator->getSuccessor(1); - if (L->getHeader() == SplitCondBlock - && (Latch == Succ0 || Latch == Succ1)) - return false; - - // If one of the split condition branch is post dominating other then loop - // index split is not appropriate. - if (DT->dominates(Succ0, Latch) || DT->dominates(Succ1, Latch)) - return false; - - // If one of the split condition branch is a predecessor of the other - // split condition branch head then do not split loop on this condition. - for(pred_iterator PI = pred_begin(Succ0), PE = pred_end(Succ0); - PI != PE; ++PI) - if (Succ1 == *PI) - return false; - for(pred_iterator PI = pred_begin(Succ1), PE = pred_end(Succ1); - PI != PE; ++PI) - if (Succ0 == *PI) - return false; +/// updatePHINodes - CFG has been changed. +/// Before +/// - ExitBB's single predecessor was Latch +/// - Latch's second successor was Header +/// Now +/// - ExitBB's single predecessor is Header +/// - Latch's one and only successor is Header +/// +/// Update ExitBB PHINodes' to reflect this change. +void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, + BasicBlock *Header, + PHINode *IV, Instruction *IVIncrement, + Loop *LP) { + + for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end(); + BI != BE; ) { + PHINode *PN = dyn_cast(BI); + ++BI; + if (!PN) + break; - // Finally this split condition is safe only if merge point for - // split condition branch is loop latch. This check along with previous - // check, to ensure that exit condition is in either loop latch or header, - // filters all loops with non-empty loop body between merge point - // and exit condition. - DominanceFrontier::iterator Succ0DF = DF->find(Succ0); - assert (Succ0DF != DF->end() && "Unable to find Succ0 dominance frontier"); - if (Succ0DF->second.count(Latch)) - return true; + Value *V = PN->getIncomingValueForBlock(Latch); + if (PHINode *PHV = dyn_cast(V)) { + // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use + // in Header which is new incoming value for PN. + Value *NewV = NULL; + for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end(); + UI != E; ++UI) + if (PHINode *U = dyn_cast(*UI)) + if (LP->contains(U->getParent())) { + NewV = U; + break; + } - DominanceFrontier::iterator Succ1DF = DF->find(Succ1); - assert (Succ1DF != DF->end() && "Unable to find Succ1 dominance frontier"); - if (Succ1DF->second.count(Latch)) - return true; - - return false; + // Add incoming value from header only if PN has any use inside the loop. + if (NewV) + PN->addIncoming(NewV, Header); + + } else if (Instruction *PHI = dyn_cast(V)) { + // If this instruction is IVIncrement then IV is new incoming value + // from header otherwise this instruction must be incoming value from + // header because loop is in LCSSA form. + if (PHI == IVIncrement) + PN->addIncoming(IV, Header); + else + PN->addIncoming(V, Header); + } else + // Otherwise this is an incoming value from header because loop is in + // LCSSA form. + PN->addIncoming(V, Header); + + // Remove incoming value from Latch. + PN->removeIncomingValue(Latch); + } } -/// calculateLoopBounds - ALoop exit value and BLoop start values are calculated -/// based on split value. -void LoopIndexSplit::calculateLoopBounds(SplitInfo &SD) { - - ICmpInst::Predicate SP = SD.SplitCondition->getPredicate(); - const Type *Ty = SD.SplitValue->getType(); - bool Sign = ExitCondition->isSignedPredicate(); - BasicBlock *Preheader = L->getLoopPreheader(); - Instruction *PHTerminator = Preheader->getTerminator(); - - // Initially use split value as upper loop bound for first loop and lower loop - // bound for second loop. - Value *AEV = SD.SplitValue; - Value *BSV = SD.SplitValue; - - switch (ExitCondition->getPredicate()) { - case ICmpInst::ICMP_SGT: - case ICmpInst::ICMP_UGT: - case ICmpInst::ICMP_SGE: - case ICmpInst::ICMP_UGE: - default: - assert (0 && "Unexpected exit condition predicate"); - - case ICmpInst::ICMP_SLT: - case ICmpInst::ICMP_ULT: - { - switch (SP) { - case ICmpInst::ICMP_SLT: - case ICmpInst::ICMP_ULT: - // - // for (i = LB; i < UB; ++i) { if (i < SV) A; else B; } - // - // is transformed into - // AEV = BSV = SV - // for (i = LB; i < min(UB, AEV); ++i) - // A; - // for (i = max(LB, BSV); i < UB; ++i); - // B; - break; - case ICmpInst::ICMP_SLE: - case ICmpInst::ICMP_ULE: - { - // - // for (i = LB; i < UB; ++i) { if (i <= SV) A; else B; } - // - // is transformed into - // - // AEV = SV + 1 - // BSV = SV + 1 - // for (i = LB; i < min(UB, AEV); ++i) - // A; - // for (i = max(LB, BSV); i < UB; ++i) - // B; - BSV = BinaryOperator::createAdd(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.add", PHTerminator); - AEV = BSV; - } - break; - case ICmpInst::ICMP_SGE: - case ICmpInst::ICMP_UGE: - // - // for (i = LB; i < UB; ++i) { if (i >= SV) A; else B; } - // - // is transformed into - // AEV = BSV = SV - // for (i = LB; i < min(UB, AEV); ++i) - // B; - // for (i = max(BSV, LB); i < UB; ++i) - // A; - break; - case ICmpInst::ICMP_SGT: - case ICmpInst::ICMP_UGT: - { - // - // for (i = LB; i < UB; ++i) { if (i > SV) A; else B; } - // - // is transformed into - // - // BSV = AEV = SV + 1 - // for (i = LB; i < min(UB, AEV); ++i) - // B; - // for (i = max(LB, BSV); i < UB; ++i) - // A; - BSV = BinaryOperator::createAdd(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.add", PHTerminator); - AEV = BSV; - } - break; - default: - assert (0 && "Unexpected split condition predicate"); - break; - } // end switch (SP) +bool LoopIndexSplit::splitLoop() { + SplitCondition = NULL; + if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE + || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) + return false; + BasicBlock *Header = L->getHeader(); + BasicBlock *Latch = L->getLoopLatch(); + BranchInst *SBR = NULL; // Split Condition Branch + BranchInst *EBR = cast(ExitCondition->getParent()->getTerminator()); + // If Exiting block includes loop variant instructions then this + // loop may not be split safely. + BasicBlock *ExitingBlock = ExitCondition->getParent(); + if (!cleanBlock(ExitingBlock)) return false; + + LLVMContext &Context = Header->getContext(); + + for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); + I != E; ++I) { + BranchInst *BR = dyn_cast((*I)->getTerminator()); + if (!BR || BR->isUnconditional()) continue; + ICmpInst *CI = dyn_cast(BR->getCondition()); + if (!CI || CI == ExitCondition + || CI->getPredicate() == ICmpInst::ICMP_NE + || CI->getPredicate() == ICmpInst::ICMP_EQ) + continue; + + // Unable to handle triangle loops at the moment. + // In triangle loop, split condition is in header and one of the + // the split destination is loop latch. If split condition is EQ + // then such loops are already handle in processOneIterationLoop(). + if (Header == (*I) + && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1))) + continue; + + // If the block does not dominate the latch then this is not a diamond. + // Such loop may not benefit from index split. + if (!DT->dominates((*I), Latch)) + continue; + + // If split condition branches heads do not have single predecessor, + // SplitCondBlock, then is not possible to remove inactive branch. + if (!BR->getSuccessor(0)->getSinglePredecessor() + || !BR->getSuccessor(1)->getSinglePredecessor()) + return false; + + // If the merge point for BR is not loop latch then skip this condition. + if (BR->getSuccessor(0) != Latch) { + DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); + assert (DF0 != DF->end() && "Unable to find dominance frontier"); + if (!DF0->second.count(Latch)) + continue; } - break; - case ICmpInst::ICMP_SLE: - case ICmpInst::ICMP_ULE: - { - switch (SP) { - case ICmpInst::ICMP_SLT: - case ICmpInst::ICMP_ULT: - // - // for (i = LB; i <= UB; ++i) { if (i < SV) A; else B; } - // - // is transformed into - // AEV = SV - 1; - // BSV = SV; - // for (i = LB; i <= min(UB, AEV); ++i) - // A; - // for (i = max(LB, BSV); i <= UB; ++i) - // B; - AEV = BinaryOperator::createSub(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.sub", PHTerminator); - break; - case ICmpInst::ICMP_SLE: - case ICmpInst::ICMP_ULE: - // - // for (i = LB; i <= UB; ++i) { if (i <= SV) A; else B; } - // - // is transformed into - // AEV = SV; - // BSV = SV + 1; - // for (i = LB; i <= min(UB, AEV); ++i) - // A; - // for (i = max(LB, BSV); i <= UB; ++i) - // B; - BSV = BinaryOperator::createAdd(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.add", PHTerminator); - break; - case ICmpInst::ICMP_SGT: - case ICmpInst::ICMP_UGT: - // - // for (i = LB; i <= UB; ++i) { if (i > SV) A; else B; } - // - // is transformed into - // AEV = SV; - // BSV = SV + 1; - // for (i = LB; i <= min(AEV, UB); ++i) - // B; - // for (i = max(LB, BSV); i <= UB; ++i) - // A; - BSV = BinaryOperator::createAdd(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.add", PHTerminator); - break; - case ICmpInst::ICMP_SGE: - case ICmpInst::ICMP_UGE: - // ** TODO ** - // - // for (i = LB; i <= UB; ++i) { if (i >= SV) A; else B; } - // - // is transformed into - // AEV = SV - 1; - // BSV = SV; - // for (i = LB; i <= min(AEV, UB); ++i) - // B; - // for (i = max(LB, BSV); i <= UB; ++i) - // A; - AEV = BinaryOperator::createSub(SD.SplitValue, - ConstantInt::get(Ty, 1, Sign), - "lsplit.sub", PHTerminator); - break; - default: - assert (0 && "Unexpected split condition predicate"); - break; - } // end switch (SP) + + if (BR->getSuccessor(1) != Latch) { + DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); + assert (DF1 != DF->end() && "Unable to find dominance frontier"); + if (!DF1->second.count(Latch)) + continue; } + SplitCondition = CI; + SBR = BR; break; } + + if (!SplitCondition) + return false; - // Calculate ALoop induction variable's new exiting value and - // BLoop induction variable's new starting value. Calculuate these - // values in original loop's preheader. - // A_ExitValue = min(SplitValue, OrignalLoopExitValue) - // B_StartValue = max(SplitValue, OriginalLoopStartValue) - Value *C1 = new ICmpInst(Sign ? - ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, - AEV, - ExitCondition->getOperand(ExitValueNum), - "lsplit.ev", PHTerminator); - SD.A_ExitValue = new SelectInst(C1, AEV, - ExitCondition->getOperand(ExitValueNum), - "lsplit.ev", PHTerminator); - - Value *C2 = new ICmpInst(Sign ? - ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, - BSV, StartValue, "lsplit.sv", - PHTerminator); - SD.B_StartValue = new SelectInst(C2, StartValue, BSV, - "lsplit.sv", PHTerminator); -} - -/// splitLoop - Split current loop L in two loops using split information -/// SD. Update dominator information. Maintain LCSSA form. -bool LoopIndexSplit::splitLoop(SplitInfo &SD) { + // If the predicate sign does not match then skip. + if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate()) + return false; - if (!safeSplitCondition(SD)) + unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue); + unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0)); + Value *SplitValue = SplitCondition->getOperand(SVOpNum); + if (!L->isLoopInvariant(SplitValue)) + return false; + if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum))) return false; - // After loop is cloned there are two loops. - // - // First loop, referred as ALoop, executes first part of loop's iteration - // space split. Second loop, referred as BLoop, executes remaining - // part of loop's iteration space. - // - // ALoop's exit edge enters BLoop's header through a forwarding block which - // acts as a BLoop's preheader. - BasicBlock *Preheader = L->getLoopPreheader(); - - // Calculate ALoop induction variable's new exiting value and - // BLoop induction variable's new starting value. - calculateLoopBounds(SD); - - //[*] Clone loop. + // Normalize loop conditions so that it is easier to calculate new loop + // bounds. + if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { + ExitCondition->setPredicate(ExitCondition->getInversePredicate()); + BasicBlock *T = EBR->getSuccessor(0); + EBR->setSuccessor(0, EBR->getSuccessor(1)); + EBR->setSuccessor(1, T); + } + + if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) { + SplitCondition->setPredicate(SplitCondition->getInversePredicate()); + BasicBlock *T = SBR->getSuccessor(0); + SBR->setSuccessor(0, SBR->getSuccessor(1)); + SBR->setSuccessor(1, T); + } + + //[*] Calculate new loop bounds. + Value *AEV = SplitValue; + Value *BSV = SplitValue; + bool Sign = SplitCondition->isSignedPredicate(); + Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); + + if (IVisLT(*ExitCondition)) { + if (IVisLT(*SplitCondition)) { + /* Do nothing */ + } + else if (IVisLE(*SplitCondition)) { + AEV = getPlusOne(SplitValue, Sign, PHTerm, Context); + BSV = getPlusOne(SplitValue, Sign, PHTerm, Context); + } else { + assert (0 && "Unexpected split condition!"); + } + } + else if (IVisLE(*ExitCondition)) { + if (IVisLT(*SplitCondition)) { + AEV = getMinusOne(SplitValue, Sign, PHTerm, Context); + } + else if (IVisLE(*SplitCondition)) { + BSV = getPlusOne(SplitValue, Sign, PHTerm, Context); + } else { + assert (0 && "Unexpected split condition!"); + } + } else { + assert (0 && "Unexpected exit condition!"); + } + AEV = getMin(AEV, IVExitValue, Sign, PHTerm); + BSV = getMax(BSV, IVStartValue, Sign, PHTerm); + + // [*] Clone Loop DenseMap ValueMap; Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this); - BasicBlock *B_Header = BLoop->getHeader(); + Loop *ALoop = L; - //[*] ALoop's exiting edge BLoop's header. + // [*] ALoop's exiting edge enters BLoop's header. // ALoop's original exit block becomes BLoop's exit block. PHINode *B_IndVar = cast(ValueMap[IndVar]); BasicBlock *A_ExitingBlock = ExitCondition->getParent(); @@ -997,23 +1005,24 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { dyn_cast(A_ExitingBlock->getTerminator()); assert (A_ExitInsn && "Unable to find suitable loop exit branch"); BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1); - if (L->contains(B_ExitBlock)) { + BasicBlock *B_Header = BLoop->getHeader(); + if (ALoop->contains(B_ExitBlock)) { B_ExitBlock = A_ExitInsn->getSuccessor(0); A_ExitInsn->setSuccessor(0, B_Header); } else A_ExitInsn->setSuccessor(1, B_Header); - //[*] Update ALoop's exit value using new exit value. - ExitCondition->setOperand(ExitValueNum, SD.A_ExitValue); - + // [*] Update ALoop's exit value using new exit value. + ExitCondition->setOperand(EVOpNum, AEV); + // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from // original loop's preheader. Add incoming PHINode values from // ALoop's exiting block. Update BLoop header's domiantor info. // Collect inverse map of Header PHINodes. DenseMap InverseMap; - for (BasicBlock::iterator BI = L->getHeader()->begin(), - BE = L->getHeader()->end(); BI != BE; ++BI) { + for (BasicBlock::iterator BI = ALoop->getHeader()->begin(), + BE = ALoop->getHeader()->end(); BI != BE; ++BI) { if (PHINode *PN = dyn_cast(BI)) { PHINode *PNClone = cast(ValueMap[PN]); InverseMap[PNClone] = PN; @@ -1021,23 +1030,31 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { break; } + BasicBlock *A_Preheader = ALoop->getLoopPreheader(); for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end(); BI != BE; ++BI) { if (PHINode *PN = dyn_cast(BI)) { // Remove incoming value from original preheader. - PN->removeIncomingValue(Preheader); + PN->removeIncomingValue(A_Preheader); // Add incoming value from A_ExitingBlock. if (PN == B_IndVar) - PN->addIncoming(SD.B_StartValue, A_ExitingBlock); + PN->addIncoming(BSV, A_ExitingBlock); else { PHINode *OrigPN = cast(InverseMap[PN]); - Value *V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock); + Value *V2 = NULL; + // If loop header is also loop exiting block then + // OrigPN is incoming value for B loop header. + if (A_ExitingBlock == ALoop->getHeader()) + V2 = OrigPN; + else + V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock); PN->addIncoming(V2, A_ExitingBlock); } } else break; } + DT->changeImmediateDominator(B_Header, A_ExitingBlock); DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT); @@ -1071,7 +1088,7 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { BI != BE; ++BI) { if (PHINode *PN = dyn_cast(BI)) { Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock); - PHINode *newPHI = new PHINode(PN->getType(), PN->getName()); + PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName()); newPHI->addIncoming(V1, A_ExitingBlock); A_ExitBlock->getInstList().push_front(newPHI); PN->removeIncomingValue(A_ExitBlock); @@ -1081,17 +1098,12 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { } //[*] Eliminate split condition's inactive branch from ALoop. - BasicBlock *A_SplitCondBlock = SD.SplitCondition->getParent(); + BasicBlock *A_SplitCondBlock = SplitCondition->getParent(); BranchInst *A_BR = cast(A_SplitCondBlock->getTerminator()); BasicBlock *A_InactiveBranch = NULL; BasicBlock *A_ActiveBranch = NULL; - if (SD.UseTrueBranchFirst) { - A_ActiveBranch = A_BR->getSuccessor(0); - A_InactiveBranch = A_BR->getSuccessor(1); - } else { - A_ActiveBranch = A_BR->getSuccessor(1); - A_InactiveBranch = A_BR->getSuccessor(0); - } + A_ActiveBranch = A_BR->getSuccessor(0); + A_InactiveBranch = A_BR->getSuccessor(1); A_BR->setUnconditionalDest(A_ActiveBranch); removeBlocks(A_InactiveBranch, L, A_ActiveBranch); @@ -1100,15 +1112,136 @@ bool LoopIndexSplit::splitLoop(SplitInfo &SD) { BranchInst *B_BR = cast(B_SplitCondBlock->getTerminator()); BasicBlock *B_InactiveBranch = NULL; BasicBlock *B_ActiveBranch = NULL; - if (SD.UseTrueBranchFirst) { - B_ActiveBranch = B_BR->getSuccessor(1); - B_InactiveBranch = B_BR->getSuccessor(0); - } else { - B_ActiveBranch = B_BR->getSuccessor(0); - B_InactiveBranch = B_BR->getSuccessor(1); - } + B_ActiveBranch = B_BR->getSuccessor(1); + B_InactiveBranch = B_BR->getSuccessor(0); B_BR->setUnconditionalDest(B_ActiveBranch); removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch); + BasicBlock *A_Header = ALoop->getHeader(); + if (A_ExitingBlock == A_Header) + return true; + + //[*] Move exit condition into split condition block to avoid + // executing dead loop iteration. + ICmpInst *B_ExitCondition = cast(ValueMap[ExitCondition]); + Instruction *B_IndVarIncrement = cast(ValueMap[IVIncrement]); + ICmpInst *B_SplitCondition = cast(ValueMap[SplitCondition]); + + moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition, + cast(SplitCondition), IndVar, IVIncrement, + ALoop, EVOpNum); + + moveExitCondition(B_SplitCondBlock, B_ActiveBranch, + B_ExitBlock, B_ExitCondition, + B_SplitCondition, B_IndVar, B_IndVarIncrement, + BLoop, EVOpNum); + + NumIndexSplit++; return true; } + +/// cleanBlock - A block is considered clean if all non terminal instructions +/// are either, PHINodes, IV based. +bool LoopIndexSplit::cleanBlock(BasicBlock *BB) { + Instruction *Terminator = BB->getTerminator(); + for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); + BI != BE; ++BI) { + Instruction *I = BI; + + if (isa(I) || I == Terminator || I == ExitCondition + || I == SplitCondition || IVBasedValues.count(I) + || isa(I)) + continue; + + if (I->mayHaveSideEffects()) + return false; + + // I is used only inside this block then it is OK. + bool usedOutsideBB = false; + for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); + UI != UE; ++UI) { + Instruction *U = cast(UI); + if (U->getParent() != BB) + usedOutsideBB = true; + } + if (!usedOutsideBB) + continue; + + // Otherwise we have a instruction that may not allow loop spliting. + return false; + } + return true; +} + +/// IVisLT - If Op is comparing IV based value with an loop invariant and +/// IV based value is less than the loop invariant then return the loop +/// invariant. Otherwise return NULL. +Value * LoopIndexSplit::IVisLT(ICmpInst &Op) { + ICmpInst::Predicate P = Op.getPredicate(); + if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) + && IVBasedValues.count(Op.getOperand(0)) + && L->isLoopInvariant(Op.getOperand(1))) + return Op.getOperand(1); + + if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) + && IVBasedValues.count(Op.getOperand(1)) + && L->isLoopInvariant(Op.getOperand(0))) + return Op.getOperand(0); + + return NULL; +} + +/// IVisLE - If Op is comparing IV based value with an loop invariant and +/// IV based value is less than or equal to the loop invariant then +/// return the loop invariant. Otherwise return NULL. +Value * LoopIndexSplit::IVisLE(ICmpInst &Op) { + ICmpInst::Predicate P = Op.getPredicate(); + if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) + && IVBasedValues.count(Op.getOperand(0)) + && L->isLoopInvariant(Op.getOperand(1))) + return Op.getOperand(1); + + if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) + && IVBasedValues.count(Op.getOperand(1)) + && L->isLoopInvariant(Op.getOperand(0))) + return Op.getOperand(0); + + return NULL; +} + +/// IVisGT - If Op is comparing IV based value with an loop invariant and +/// IV based value is greater than the loop invariant then return the loop +/// invariant. Otherwise return NULL. +Value * LoopIndexSplit::IVisGT(ICmpInst &Op) { + ICmpInst::Predicate P = Op.getPredicate(); + if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) + && IVBasedValues.count(Op.getOperand(0)) + && L->isLoopInvariant(Op.getOperand(1))) + return Op.getOperand(1); + + if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) + && IVBasedValues.count(Op.getOperand(1)) + && L->isLoopInvariant(Op.getOperand(0))) + return Op.getOperand(0); + + return NULL; +} + +/// IVisGE - If Op is comparing IV based value with an loop invariant and +/// IV based value is greater than or equal to the loop invariant then +/// return the loop invariant. Otherwise return NULL. +Value * LoopIndexSplit::IVisGE(ICmpInst &Op) { + ICmpInst::Predicate P = Op.getPredicate(); + if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) + && IVBasedValues.count(Op.getOperand(0)) + && L->isLoopInvariant(Op.getOperand(1))) + return Op.getOperand(1); + + if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) + && IVBasedValues.count(Op.getOperand(1)) + && L->isLoopInvariant(Op.getOperand(0))) + return Op.getOperand(0); + + return NULL; +} +