Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 1 | //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 10 | // This file implements Loop Index Splitting Pass. This pass handles three |
| 11 | // kinds of loops. |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 12 | // |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 13 | // [1] A loop may be eliminated if the body is executed exactly once. |
| 14 | // For example, |
| 15 | // |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 16 | // for (i = 0; i < N; ++i) { |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 17 | // if (i == X) { |
| 18 | // body; |
| 19 | // } |
| 20 | // } |
| 21 | // |
| 22 | // is transformed to |
| 23 | // |
| 24 | // i = X; |
| 25 | // body; |
| 26 | // |
| 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, |
| 29 | // |
| 30 | // for (i = 0; i < N; ++i) { |
| 31 | // if (i > A && i < B) { |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 32 | // ... |
| 33 | // } |
| 34 | // } |
| 35 | // |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 36 | // is transformed to iterators from A to B, if A > 0 and B < N. |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 37 | // |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 38 | // [3] A loop may be split if the loop body is dominated by a branch. |
| 39 | // For example, |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 40 | // |
| 41 | // for (i = LB; i < UB; ++i) { if (i < SV) A; else B; } |
| 42 | // |
| 43 | // is transformed into |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 44 | // |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 45 | // AEV = BSV = SV |
| 46 | // for (i = LB; i < min(UB, AEV); ++i) |
| 47 | // A; |
| 48 | // for (i = max(LB, BSV); i < UB; ++i); |
| 49 | // B; |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 50 | // |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | #define DEBUG_TYPE "loop-index-split" |
| 54 | |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Scalar.h" |
Devang Patel | d96c60d | 2009-02-06 06:19:06 +0000 | [diff] [blame] | 56 | #include "llvm/IntrinsicInst.h" |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 57 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 97b6e2c | 2009-02-17 20:50:11 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/ScalarEvolution.h" |
Devang Patel | 787a713 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/Dominators.h" |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 60 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 61 | #include "llvm/Transforms/Utils/Cloning.h" |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 62 | #include "llvm/Transforms/Utils/Local.h" |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Compiler.h" |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 64 | #include "llvm/ADT/DepthFirstIterator.h" |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 65 | #include "llvm/ADT/Statistic.h" |
| 66 | |
| 67 | using namespace llvm; |
| 68 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 69 | STATISTIC(NumIndexSplit, "Number of loop index split"); |
| 70 | STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split"); |
| 71 | STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted"); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 72 | |
| 73 | namespace { |
| 74 | |
| 75 | class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass { |
| 76 | |
| 77 | public: |
| 78 | static char ID; // Pass ID, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 79 | LoopIndexSplit() : LoopPass(&ID) {} |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 80 | |
| 81 | // Index split Loop L. Return true if loop is split. |
| 82 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 83 | |
| 84 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 85 | AU.addPreserved<ScalarEvolution>(); |
| 86 | AU.addRequiredID(LCSSAID); |
| 87 | AU.addPreservedID(LCSSAID); |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 88 | AU.addRequired<LoopInfo>(); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 89 | AU.addPreserved<LoopInfo>(); |
| 90 | AU.addRequiredID(LoopSimplifyID); |
| 91 | AU.addPreservedID(LoopSimplifyID); |
Devang Patel | 9704fcf | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 92 | AU.addRequired<DominatorTree>(); |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 93 | AU.addRequired<DominanceFrontier>(); |
Devang Patel | 787a713 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 94 | AU.addPreserved<DominatorTree>(); |
| 95 | AU.addPreserved<DominanceFrontier>(); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private: |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 99 | /// processOneIterationLoop -- Eliminate loop if loop body is executed |
| 100 | /// only once. For example, |
| 101 | /// for (i = 0; i < N; ++i) { |
| 102 | /// if ( i == X) { |
| 103 | /// ... |
| 104 | /// } |
| 105 | /// } |
| 106 | /// |
| 107 | bool processOneIterationLoop(); |
Devang Patel | 71554b8 | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 108 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 109 | // -- Routines used by updateLoopIterationSpace(); |
Devang Patel | c9d123d | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 110 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 111 | /// updateLoopIterationSpace -- Update loop's iteration space if loop |
| 112 | /// body is executed for certain IV range only. For example, |
| 113 | /// |
| 114 | /// for (i = 0; i < N; ++i) { |
| 115 | /// if ( i > A && i < B) { |
| 116 | /// ... |
| 117 | /// } |
| 118 | /// } |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 119 | /// is transformed to iterators from A to B, if A > 0 and B < N. |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 120 | /// |
| 121 | bool updateLoopIterationSpace(); |
Devang Patel | 71554b8 | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 122 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 123 | /// restrictLoopBound - Op dominates loop body. Op compares an IV based value |
| 124 | /// with a loop invariant value. Update loop's lower and upper bound based on |
| 125 | /// the loop invariant value. |
| 126 | bool restrictLoopBound(ICmpInst &Op); |
Devang Patel | 4a69da9 | 2007-08-25 00:56:38 +0000 | [diff] [blame] | 127 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 128 | // --- Routines used by splitLoop(). --- / |
Devang Patel | 4a69da9 | 2007-08-25 00:56:38 +0000 | [diff] [blame] | 129 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 130 | bool splitLoop(); |
Devang Patel | 4a69da9 | 2007-08-25 00:56:38 +0000 | [diff] [blame] | 131 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 132 | /// removeBlocks - Remove basic block DeadBB and all blocks dominated by |
| 133 | /// DeadBB. This routine is used to remove split condition's dead branch, |
| 134 | /// dominated by DeadBB. LiveBB dominates split conidition's other branch. |
Devang Patel | a6a8663 | 2007-08-14 18:35:57 +0000 | [diff] [blame] | 135 | void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 136 | |
| 137 | /// moveExitCondition - Move exit condition EC into split condition block. |
| 138 | void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, |
| 139 | BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC, |
| 140 | PHINode *IV, Instruction *IVAdd, Loop *LP, |
| 141 | unsigned); |
| 142 | |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 143 | /// updatePHINodes - CFG has been changed. |
| 144 | /// Before |
| 145 | /// - ExitBB's single predecessor was Latch |
| 146 | /// - Latch's second successor was Header |
| 147 | /// Now |
| 148 | /// - ExitBB's single predecessor was Header |
| 149 | /// - Latch's one and only successor was Header |
| 150 | /// |
| 151 | /// Update ExitBB PHINodes' to reflect this change. |
| 152 | void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, |
| 153 | BasicBlock *Header, |
Devang Patel | ea06906 | 2008-02-13 22:23:07 +0000 | [diff] [blame] | 154 | PHINode *IV, Instruction *IVIncrement, Loop *LP); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 155 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 156 | // --- Utility routines --- / |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 157 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 158 | /// cleanBlock - A block is considered clean if all non terminal |
| 159 | /// instructions are either PHINodes or IV based values. |
| 160 | bool cleanBlock(BasicBlock *BB); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 161 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 162 | /// IVisLT - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 163 | /// IV based value is less than the loop invariant then return the loop |
| 164 | /// invariant. Otherwise return NULL. |
| 165 | Value * IVisLT(ICmpInst &Op); |
| 166 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 167 | /// IVisLE - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 168 | /// IV based value is less than or equal to the loop invariant then |
| 169 | /// return the loop invariant. Otherwise return NULL. |
| 170 | Value * IVisLE(ICmpInst &Op); |
| 171 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 172 | /// IVisGT - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 173 | /// IV based value is greater than the loop invariant then return the loop |
| 174 | /// invariant. Otherwise return NULL. |
| 175 | Value * IVisGT(ICmpInst &Op); |
| 176 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 177 | /// IVisGE - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 178 | /// IV based value is greater than or equal to the loop invariant then |
| 179 | /// return the loop invariant. Otherwise return NULL. |
| 180 | Value * IVisGE(ICmpInst &Op); |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 181 | |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 182 | private: |
| 183 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 184 | // Current Loop information. |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 185 | Loop *L; |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 186 | LPPassManager *LPM; |
| 187 | LoopInfo *LI; |
Devang Patel | 9704fcf | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 188 | DominatorTree *DT; |
Devang Patel | fc4c5f8 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 189 | DominanceFrontier *DF; |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 190 | |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 191 | PHINode *IndVar; |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 192 | ICmpInst *ExitCondition; |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 193 | ICmpInst *SplitCondition; |
| 194 | Value *IVStartValue; |
| 195 | Value *IVExitValue; |
| 196 | Instruction *IVIncrement; |
| 197 | SmallPtrSet<Value *, 4> IVBasedValues; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 198 | }; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 201 | char LoopIndexSplit::ID = 0; |
| 202 | static RegisterPass<LoopIndexSplit> |
| 203 | X("loop-index-split", "Index Split Loops"); |
| 204 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 205 | Pass *llvm::createLoopIndexSplitPass() { |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 206 | return new LoopIndexSplit(); |
| 207 | } |
| 208 | |
| 209 | // Index split Loop L. Return true if loop is split. |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 210 | bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) { |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 211 | L = IncomingLoop; |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 212 | LPM = &LPM_Ref; |
Devang Patel | 71554b8 | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 213 | |
Devang Patel | 3fe4f21 | 2007-08-15 02:14:55 +0000 | [diff] [blame] | 214 | // FIXME - Nested loops make dominator info updates tricky. |
Devang Patel | 4e8061c | 2007-08-14 23:53:57 +0000 | [diff] [blame] | 215 | if (!L->getSubLoops().empty()) |
| 216 | return false; |
| 217 | |
Devang Patel | 9704fcf | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 218 | DT = &getAnalysis<DominatorTree>(); |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 219 | LI = &getAnalysis<LoopInfo>(); |
Devang Patel | 7375bb9 | 2007-08-15 03:34:53 +0000 | [diff] [blame] | 220 | DF = &getAnalysis<DominanceFrontier>(); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 221 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 222 | // Initialize loop data. |
| 223 | IndVar = L->getCanonicalInductionVariable(); |
| 224 | if (!IndVar) return false; |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 225 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 226 | bool P1InLoop = L->contains(IndVar->getIncomingBlock(1)); |
| 227 | IVStartValue = IndVar->getIncomingValue(!P1InLoop); |
| 228 | IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop)); |
| 229 | if (!IVIncrement) return false; |
Devang Patel | 71554b8 | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 230 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 231 | IVBasedValues.clear(); |
| 232 | IVBasedValues.insert(IndVar); |
| 233 | IVBasedValues.insert(IVIncrement); |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 234 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 235 | I != E; ++I) |
| 236 | for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); |
| 237 | BI != BE; ++BI) { |
| 238 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI)) |
| 239 | if (BO != IVIncrement |
| 240 | && (BO->getOpcode() == Instruction::Add |
| 241 | || BO->getOpcode() == Instruction::Sub)) |
| 242 | if (IVBasedValues.count(BO->getOperand(0)) |
| 243 | && L->isLoopInvariant(BO->getOperand(1))) |
| 244 | IVBasedValues.insert(BO); |
| 245 | } |
Devang Patel | bacf519 | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 246 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 247 | // Reject loop if loop exit condition is not suitable. |
Dan Gohman | c833246 | 2009-02-12 18:08:24 +0000 | [diff] [blame] | 248 | BasicBlock *ExitingBlock = L->getExitingBlock(); |
| 249 | if (!ExitingBlock) |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 250 | return false; |
Dan Gohman | c833246 | 2009-02-12 18:08:24 +0000 | [diff] [blame] | 251 | BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 252 | if (!EBR) return false; |
| 253 | ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition()); |
| 254 | if (!ExitCondition) return false; |
Dan Gohman | c833246 | 2009-02-12 18:08:24 +0000 | [diff] [blame] | 255 | if (ExitingBlock != L->getLoopLatch()) return false; |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 256 | IVExitValue = ExitCondition->getOperand(1); |
| 257 | if (!L->isLoopInvariant(IVExitValue)) |
| 258 | IVExitValue = ExitCondition->getOperand(0); |
| 259 | if (!L->isLoopInvariant(IVExitValue)) |
| 260 | return false; |
Devang Patel | a5e27f8 | 2008-07-09 00:12:01 +0000 | [diff] [blame] | 261 | |
| 262 | // If start value is more then exit value where induction variable |
| 263 | // increments by 1 then we are potentially dealing with an infinite loop. |
| 264 | // Do not index split this loop. |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 265 | if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue)) |
| 266 | if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue)) |
| 267 | if (SV->getSExtValue() > EV->getSExtValue()) |
| 268 | return false; |
Devang Patel | c9d123d | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 269 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 270 | if (processOneIterationLoop()) |
| 271 | return true; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 272 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 273 | if (updateLoopIterationSpace()) |
| 274 | return true; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 275 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 276 | if (splitLoop()) |
| 277 | return true; |
Devang Patel | d35ed2c | 2007-09-11 00:42:56 +0000 | [diff] [blame] | 278 | |
| 279 | return false; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 282 | // --- Helper routines --- |
| 283 | // isUsedOutsideLoop - Returns true iff V is used outside the loop L. |
| 284 | static bool isUsedOutsideLoop(Value *V, Loop *L) { |
| 285 | for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 286 | if (!L->contains(cast<Instruction>(*UI)->getParent())) |
| 287 | return true; |
| 288 | return false; |
| 289 | } |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 290 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 291 | // Return V+1 |
| 292 | static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt) { |
| 293 | ConstantInt *One = ConstantInt::get(V->getType(), 1, Sign); |
| 294 | return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt); |
| 295 | } |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 296 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 297 | // Return V-1 |
| 298 | static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt) { |
| 299 | ConstantInt *One = ConstantInt::get(V->getType(), 1, Sign); |
| 300 | return BinaryOperator::CreateSub(V, One, "lsp", InsertPt); |
| 301 | } |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 302 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 303 | // Return min(V1, V1) |
| 304 | static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { |
| 305 | |
| 306 | Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 307 | V1, V2, "lsp", InsertPt); |
| 308 | return SelectInst::Create(C, V1, V2, "lsp", InsertPt); |
| 309 | } |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 310 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 311 | // Return max(V1, V2) |
| 312 | static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { |
| 313 | |
| 314 | Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 315 | V1, V2, "lsp", InsertPt); |
| 316 | return SelectInst::Create(C, V2, V1, "lsp", InsertPt); |
| 317 | } |
Devang Patel | 968eee2 | 2007-09-19 00:15:16 +0000 | [diff] [blame] | 318 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 319 | /// processOneIterationLoop -- Eliminate loop if loop body is executed |
| 320 | /// only once. For example, |
| 321 | /// for (i = 0; i < N; ++i) { |
| 322 | /// if ( i == X) { |
| 323 | /// ... |
| 324 | /// } |
| 325 | /// } |
| 326 | /// |
| 327 | bool LoopIndexSplit::processOneIterationLoop() { |
| 328 | SplitCondition = NULL; |
Devang Patel | 84ef08b | 2007-09-19 00:11:01 +0000 | [diff] [blame] | 329 | BasicBlock *Latch = L->getLoopLatch(); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 330 | BasicBlock *Header = L->getHeader(); |
| 331 | BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator()); |
| 332 | if (!BR) return false; |
| 333 | if (!isa<BranchInst>(Latch->getTerminator())) return false; |
| 334 | if (BR->isUnconditional()) return false; |
| 335 | SplitCondition = dyn_cast<ICmpInst>(BR->getCondition()); |
| 336 | if (!SplitCondition) return false; |
| 337 | if (SplitCondition == ExitCondition) return false; |
| 338 | if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false; |
| 339 | if (BR->getOperand(1) != Latch) return false; |
| 340 | if (!IVBasedValues.count(SplitCondition->getOperand(0)) |
| 341 | && !IVBasedValues.count(SplitCondition->getOperand(1))) |
Devang Patel | 84ef08b | 2007-09-19 00:11:01 +0000 | [diff] [blame] | 342 | return false; |
| 343 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 344 | // If IV is used outside the loop then this loop traversal is required. |
| 345 | // FIXME: Calculate and use last IV value. |
| 346 | if (isUsedOutsideLoop(IVIncrement, L)) |
| 347 | return false; |
| 348 | |
| 349 | // If BR operands are not IV or not loop invariants then skip this loop. |
| 350 | Value *OPV = SplitCondition->getOperand(0); |
| 351 | Value *SplitValue = SplitCondition->getOperand(1); |
| 352 | if (!L->isLoopInvariant(SplitValue)) { |
| 353 | Value *T = SplitValue; |
| 354 | SplitValue = OPV; |
| 355 | OPV = T; |
| 356 | } |
| 357 | if (!L->isLoopInvariant(SplitValue)) |
| 358 | return false; |
| 359 | Instruction *OPI = dyn_cast<Instruction>(OPV); |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 360 | if (!OPI) |
| 361 | return false; |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 362 | if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L)) |
| 363 | return false; |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 364 | Value *StartValue = IVStartValue; |
| 365 | Value *ExitValue = IVExitValue;; |
| 366 | |
| 367 | if (OPV != IndVar) { |
| 368 | // If BR operand is IV based then use this operand to calculate |
| 369 | // effective conditions for loop body. |
| 370 | BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV); |
| 371 | if (!BOPV) |
| 372 | return false; |
| 373 | if (BOPV->getOpcode() != Instruction::Add) |
| 374 | return false; |
| 375 | StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR); |
| 376 | ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR); |
| 377 | } |
| 378 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 379 | if (!cleanBlock(Header)) |
| 380 | return false; |
| 381 | |
| 382 | if (!cleanBlock(Latch)) |
| 383 | return false; |
| 384 | |
| 385 | // If the merge point for BR is not loop latch then skip this loop. |
| 386 | if (BR->getSuccessor(0) != Latch) { |
| 387 | DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); |
| 388 | assert (DF0 != DF->end() && "Unable to find dominance frontier"); |
| 389 | if (!DF0->second.count(Latch)) |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | if (BR->getSuccessor(1) != Latch) { |
| 394 | DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); |
| 395 | assert (DF1 != DF->end() && "Unable to find dominance frontier"); |
| 396 | if (!DF1->second.count(Latch)) |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | // Now, Current loop L contains compare instruction |
| 401 | // that compares induction variable, IndVar, against loop invariant. And |
| 402 | // entire (i.e. meaningful) loop body is dominated by this compare |
| 403 | // instruction. In such case eliminate |
| 404 | // loop structure surrounding this loop body. For example, |
| 405 | // for (int i = start; i < end; ++i) { |
| 406 | // if ( i == somevalue) { |
| 407 | // loop_body |
| 408 | // } |
| 409 | // } |
| 410 | // can be transformed into |
| 411 | // if (somevalue >= start && somevalue < end) { |
| 412 | // i = somevalue; |
| 413 | // loop_body |
| 414 | // } |
Devang Patel | 6a2bfda | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 415 | |
Devang Patel | ebc5fea | 2007-08-20 20:49:01 +0000 | [diff] [blame] | 416 | // Replace index variable with split value in loop body. Loop body is executed |
| 417 | // only when index variable is equal to split value. |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 418 | IndVar->replaceAllUsesWith(SplitValue); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 419 | |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 420 | // Replace split condition in header. |
| 421 | // Transform |
| 422 | // SplitCondition : icmp eq i32 IndVar, SplitValue |
| 423 | // into |
| 424 | // c1 = icmp uge i32 SplitValue, StartValue |
Devang Patel | ba32a5f | 2007-09-10 23:57:58 +0000 | [diff] [blame] | 425 | // c2 = icmp ult i32 SplitValue, ExitValue |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 426 | // and i32 c1, c2 |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 427 | Instruction *C1 = new ICmpInst(ExitCondition->isSignedPredicate() ? |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 428 | ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 429 | SplitValue, StartValue, "lisplit", BR); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 430 | |
| 431 | CmpInst::Predicate C2P = ExitCondition->getPredicate(); |
| 432 | BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator()); |
| 433 | if (LatchBR->getOperand(0) != Header) |
| 434 | C2P = CmpInst::getInversePredicate(C2P); |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 435 | Instruction *C2 = new ICmpInst(C2P, SplitValue, ExitValue, "lisplit", BR); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 436 | Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR); |
| 437 | |
| 438 | SplitCondition->replaceAllUsesWith(NSplitCond); |
| 439 | SplitCondition->eraseFromParent(); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 440 | |
Devang Patel | fc19fbd | 2008-10-10 22:02:57 +0000 | [diff] [blame] | 441 | // Remove Latch to Header edge. |
| 442 | BasicBlock *LatchSucc = NULL; |
| 443 | Header->removePredecessor(Latch); |
| 444 | for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); |
| 445 | SI != E; ++SI) { |
| 446 | if (Header != *SI) |
| 447 | LatchSucc = *SI; |
| 448 | } |
Devang Patel | fc19fbd | 2008-10-10 22:02:57 +0000 | [diff] [blame] | 449 | |
Devang Patel | b23c232 | 2009-03-30 22:24:10 +0000 | [diff] [blame] | 450 | // Clean up latch block. |
| 451 | Value *LatchBRCond = LatchBR->getCondition(); |
| 452 | LatchBR->setUnconditionalDest(LatchSucc); |
| 453 | RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 454 | |
Devang Patel | 423c8b2 | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 455 | LPM->deleteLoopFromQueue(L); |
Devang Patel | 787a713 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 456 | |
| 457 | // Update Dominator Info. |
| 458 | // Only CFG change done is to remove Latch to Header edge. This |
| 459 | // does not change dominator tree because Latch did not dominate |
| 460 | // Header. |
Devang Patel | fc4c5f8 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 461 | if (DF) { |
Devang Patel | 787a713 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 462 | DominanceFrontier::iterator HeaderDF = DF->find(Header); |
| 463 | if (HeaderDF != DF->end()) |
| 464 | DF->removeFromFrontier(HeaderDF, Header); |
| 465 | |
| 466 | DominanceFrontier::iterator LatchDF = DF->find(Latch); |
| 467 | if (LatchDF != DF->end()) |
| 468 | DF->removeFromFrontier(LatchDF, Header); |
| 469 | } |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 470 | |
| 471 | ++NumIndexSplitRemoved; |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 475 | /// restrictLoopBound - Op dominates loop body. Op compares an IV based value |
| 476 | /// with a loop invariant value. Update loop's lower and upper bound based on |
| 477 | /// the loop invariant value. |
| 478 | bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) { |
| 479 | bool Sign = Op.isSignedPredicate(); |
| 480 | Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 481 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 482 | if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { |
| 483 | BranchInst *EBR = |
| 484 | cast<BranchInst>(ExitCondition->getParent()->getTerminator()); |
| 485 | ExitCondition->setPredicate(ExitCondition->getInversePredicate()); |
| 486 | BasicBlock *T = EBR->getSuccessor(0); |
| 487 | EBR->setSuccessor(0, EBR->getSuccessor(1)); |
| 488 | EBR->setSuccessor(1, T); |
Devang Patel | fee76bd | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 491 | // New upper and lower bounds. |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 492 | Value *NLB = NULL; |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 493 | Value *NUB = NULL; |
| 494 | if (Value *V = IVisLT(Op)) { |
| 495 | // Restrict upper bound. |
| 496 | if (IVisLE(*ExitCondition)) |
| 497 | V = getMinusOne(V, Sign, PHTerm); |
| 498 | NUB = getMin(V, IVExitValue, Sign, PHTerm); |
| 499 | } else if (Value *V = IVisLE(Op)) { |
| 500 | // Restrict upper bound. |
| 501 | if (IVisLT(*ExitCondition)) |
| 502 | V = getPlusOne(V, Sign, PHTerm); |
| 503 | NUB = getMin(V, IVExitValue, Sign, PHTerm); |
| 504 | } else if (Value *V = IVisGT(Op)) { |
| 505 | // Restrict lower bound. |
| 506 | V = getPlusOne(V, Sign, PHTerm); |
| 507 | NLB = getMax(V, IVStartValue, Sign, PHTerm); |
| 508 | } else if (Value *V = IVisGE(Op)) |
| 509 | // Restrict lower bound. |
| 510 | NLB = getMax(V, IVStartValue, Sign, PHTerm); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 511 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 512 | if (!NLB && !NUB) |
| 513 | return false; |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 514 | |
| 515 | if (NLB) { |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 516 | unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader()); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 517 | IndVar->setIncomingValue(i, NLB); |
| 518 | } |
| 519 | |
| 520 | if (NUB) { |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 521 | unsigned i = (ExitCondition->getOperand(0) != IVExitValue); |
| 522 | ExitCondition->setOperand(i, NUB); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 523 | } |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 524 | return true; |
Devang Patel | 5279d06 | 2007-09-17 20:39:48 +0000 | [diff] [blame] | 525 | } |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 526 | |
| 527 | /// updateLoopIterationSpace -- Update loop's iteration space if loop |
| 528 | /// body is executed for certain IV range only. For example, |
| 529 | /// |
| 530 | /// for (i = 0; i < N; ++i) { |
| 531 | /// if ( i > A && i < B) { |
| 532 | /// ... |
| 533 | /// } |
| 534 | /// } |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 535 | /// is transformed to iterators from A to B, if A > 0 and B < N. |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 536 | /// |
| 537 | bool LoopIndexSplit::updateLoopIterationSpace() { |
| 538 | SplitCondition = NULL; |
| 539 | if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE |
| 540 | || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) |
| 541 | return false; |
| 542 | BasicBlock *Latch = L->getLoopLatch(); |
Devang Patel | 5279d06 | 2007-09-17 20:39:48 +0000 | [diff] [blame] | 543 | BasicBlock *Header = L->getHeader(); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 544 | BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator()); |
| 545 | if (!BR) return false; |
| 546 | if (!isa<BranchInst>(Latch->getTerminator())) return false; |
| 547 | if (BR->isUnconditional()) return false; |
| 548 | BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition()); |
| 549 | if (!AND) return false; |
| 550 | if (AND->getOpcode() != Instruction::And) return false; |
| 551 | ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0)); |
| 552 | ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1)); |
| 553 | if (!Op0 || !Op1) |
| 554 | return false; |
| 555 | IVBasedValues.insert(AND); |
| 556 | IVBasedValues.insert(Op0); |
| 557 | IVBasedValues.insert(Op1); |
| 558 | if (!cleanBlock(Header)) return false; |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 559 | BasicBlock *ExitingBlock = ExitCondition->getParent(); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 560 | if (!cleanBlock(ExitingBlock)) return false; |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 561 | |
Devang Patel | cf42ee4 | 2009-03-02 23:39:14 +0000 | [diff] [blame] | 562 | // If the merge point for BR is not loop latch then skip this loop. |
| 563 | if (BR->getSuccessor(0) != Latch) { |
| 564 | DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); |
| 565 | assert (DF0 != DF->end() && "Unable to find dominance frontier"); |
| 566 | if (!DF0->second.count(Latch)) |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (BR->getSuccessor(1) != Latch) { |
| 571 | DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); |
| 572 | assert (DF1 != DF->end() && "Unable to find dominance frontier"); |
| 573 | if (!DF1->second.count(Latch)) |
| 574 | return false; |
| 575 | } |
| 576 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 577 | // Verify that loop exiting block has only two predecessor, where one pred |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 578 | // is split condition block. The other predecessor will become exiting block's |
| 579 | // dominator after CFG is updated. TODO : Handle CFG's where exiting block has |
| 580 | // more then two predecessors. This requires extra work in updating dominator |
| 581 | // information. |
| 582 | BasicBlock *ExitingBBPred = NULL; |
| 583 | for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock); |
| 584 | PI != PE; ++PI) { |
| 585 | BasicBlock *BB = *PI; |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 586 | if (Header == BB) |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 587 | continue; |
| 588 | if (ExitingBBPred) |
| 589 | return false; |
| 590 | else |
| 591 | ExitingBBPred = BB; |
| 592 | } |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 593 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 594 | if (!restrictLoopBound(*Op0)) |
| 595 | return false; |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 596 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 597 | if (!restrictLoopBound(*Op1)) |
| 598 | return false; |
| 599 | |
| 600 | // Update CFG. |
| 601 | if (BR->getSuccessor(0) == ExitingBlock) |
| 602 | BR->setUnconditionalDest(BR->getSuccessor(1)); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 603 | else |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 604 | BR->setUnconditionalDest(BR->getSuccessor(0)); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 605 | |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 606 | AND->eraseFromParent(); |
Dan Gohman | a8c763b | 2008-08-14 18:13:49 +0000 | [diff] [blame] | 607 | if (Op0->use_empty()) |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 608 | Op0->eraseFromParent(); |
Dan Gohman | a8c763b | 2008-08-14 18:13:49 +0000 | [diff] [blame] | 609 | if (Op1->use_empty()) |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 610 | Op1->eraseFromParent(); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 611 | |
| 612 | // Update domiantor info. Now, ExitingBlock has only one predecessor, |
| 613 | // ExitingBBPred, and it is ExitingBlock's immediate domiantor. |
| 614 | DT->changeImmediateDominator(ExitingBlock, ExitingBBPred); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 615 | |
| 616 | BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1); |
| 617 | if (L->contains(ExitBlock)) |
| 618 | ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0); |
| 619 | |
| 620 | // If ExitingBlock is a member of the loop basic blocks' DF list then |
| 621 | // replace ExitingBlock with header and exit block in the DF list |
| 622 | DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 623 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 624 | I != E; ++I) { |
| 625 | BasicBlock *BB = *I; |
| 626 | if (BB == Header || BB == ExitingBlock) |
| 627 | continue; |
| 628 | DominanceFrontier::iterator BBDF = DF->find(BB); |
| 629 | DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); |
| 630 | DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); |
| 631 | while (DomSetI != DomSetE) { |
| 632 | DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; |
| 633 | ++DomSetI; |
| 634 | BasicBlock *DFBB = *CurrentItr; |
| 635 | if (DFBB == ExitingBlock) { |
| 636 | BBDF->second.erase(DFBB); |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 637 | for (DominanceFrontier::DomSetType::iterator |
| 638 | EBI = ExitingBlockDF->second.begin(), |
| 639 | EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI) |
| 640 | BBDF->second.insert(*EBI); |
Devang Patel | 453a844 | 2007-09-25 17:31:19 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 644 | NumRestrictBounds++; |
Devang Patel | 1c01350 | 2007-09-25 17:43:08 +0000 | [diff] [blame] | 645 | return true; |
Devang Patel | 5279d06 | 2007-09-17 20:39:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Devang Patel | a6a8663 | 2007-08-14 18:35:57 +0000 | [diff] [blame] | 648 | /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB. |
| 649 | /// This routine is used to remove split condition's dead branch, dominated by |
| 650 | /// DeadBB. LiveBB dominates split conidition's other branch. |
| 651 | void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP, |
| 652 | BasicBlock *LiveBB) { |
Devang Patel | 98147a3 | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 653 | |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 654 | // First update DeadBB's dominance frontier. |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 655 | SmallVector<BasicBlock *, 8> FrontierBBs; |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 656 | DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB); |
| 657 | if (DeadBBDF != DF->end()) { |
| 658 | SmallVector<BasicBlock *, 8> PredBlocks; |
| 659 | |
| 660 | DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second; |
| 661 | for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(), |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 662 | DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) |
| 663 | { |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 664 | BasicBlock *FrontierBB = *DeadBBSetI; |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 665 | FrontierBBs.push_back(FrontierBB); |
| 666 | |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 667 | // Rremove any PHI incoming edge from blocks dominated by DeadBB. |
| 668 | PredBlocks.clear(); |
| 669 | for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB); |
| 670 | PI != PE; ++PI) { |
| 671 | BasicBlock *P = *PI; |
| 672 | if (P == DeadBB || DT->dominates(DeadBB, P)) |
| 673 | PredBlocks.push_back(P); |
Devang Patel | fc4c5f8 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 674 | } |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 675 | |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 676 | for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end(); |
| 677 | FBI != FBE; ++FBI) { |
| 678 | if (PHINode *PN = dyn_cast<PHINode>(FBI)) { |
| 679 | for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(), |
| 680 | PE = PredBlocks.end(); PI != PE; ++PI) { |
| 681 | BasicBlock *P = *PI; |
| 682 | PN->removeIncomingValue(P); |
| 683 | } |
| 684 | } |
| 685 | else |
| 686 | break; |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 687 | } |
Devang Patel | 98147a3 | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 688 | } |
Devang Patel | 98147a3 | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 689 | } |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 690 | |
| 691 | // Now remove DeadBB and all nodes dominated by DeadBB in df order. |
| 692 | SmallVector<BasicBlock *, 32> WorkList; |
| 693 | DomTreeNode *DN = DT->getNode(DeadBB); |
| 694 | for (df_iterator<DomTreeNode*> DI = df_begin(DN), |
| 695 | E = df_end(DN); DI != E; ++DI) { |
| 696 | BasicBlock *BB = DI->getBlock(); |
| 697 | WorkList.push_back(BB); |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 698 | BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy)); |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | while (!WorkList.empty()) { |
| 702 | BasicBlock *BB = WorkList.back(); WorkList.pop_back(); |
Devang Patel | 575ec80 | 2009-03-25 23:57:48 +0000 | [diff] [blame] | 703 | LPM->deleteSimpleAnalysisValue(BB, LP); |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 704 | for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end(); |
Devang Patel | d15dd8c | 2007-09-20 23:01:50 +0000 | [diff] [blame] | 705 | BBI != BBE; ) { |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 706 | Instruction *I = BBI; |
Devang Patel | d15dd8c | 2007-09-20 23:01:50 +0000 | [diff] [blame] | 707 | ++BBI; |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 708 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Owen Anderson | 0b2a153 | 2009-04-14 01:04:19 +0000 | [diff] [blame] | 709 | LPM->deleteSimpleAnalysisValue(I, LP); |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 710 | I->eraseFromParent(); |
| 711 | } |
Devang Patel | 5b8ec61 | 2007-08-15 03:31:47 +0000 | [diff] [blame] | 712 | DT->eraseNode(BB); |
| 713 | DF->removeBlock(BB); |
| 714 | LI->removeBlock(BB); |
| 715 | BB->eraseFromParent(); |
| 716 | } |
Devang Patel | 96bf524 | 2007-08-17 21:59:16 +0000 | [diff] [blame] | 717 | |
| 718 | // Update Frontier BBs' dominator info. |
| 719 | while (!FrontierBBs.empty()) { |
| 720 | BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back(); |
| 721 | BasicBlock *NewDominator = FBB->getSinglePredecessor(); |
| 722 | if (!NewDominator) { |
| 723 | pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB); |
| 724 | NewDominator = *PI; |
| 725 | ++PI; |
| 726 | if (NewDominator != LiveBB) { |
| 727 | for(; PI != PE; ++PI) { |
| 728 | BasicBlock *P = *PI; |
| 729 | if (P == LiveBB) { |
| 730 | NewDominator = LiveBB; |
| 731 | break; |
| 732 | } |
| 733 | NewDominator = DT->findNearestCommonDominator(NewDominator, P); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | assert (NewDominator && "Unable to fix dominator info."); |
| 738 | DT->changeImmediateDominator(FBB, NewDominator); |
| 739 | DF->changeImmediateDominator(FBB, NewDominator, DT); |
| 740 | } |
| 741 | |
Devang Patel | 98147a3 | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 744 | // moveExitCondition - Move exit condition EC into split condition block CondBB. |
| 745 | void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 746 | BasicBlock *ExitBB, ICmpInst *EC, |
| 747 | ICmpInst *SC, PHINode *IV, |
| 748 | Instruction *IVAdd, Loop *LP, |
| 749 | unsigned ExitValueNum) { |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 750 | |
| 751 | BasicBlock *ExitingBB = EC->getParent(); |
| 752 | Instruction *CurrentBR = CondBB->getTerminator(); |
| 753 | |
| 754 | // Move exit condition into split condition block. |
| 755 | EC->moveBefore(CurrentBR); |
| 756 | EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV); |
| 757 | |
| 758 | // Move exiting block's branch into split condition block. Update its branch |
| 759 | // destination. |
| 760 | BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator()); |
| 761 | ExitingBR->moveBefore(CurrentBR); |
Devang Patel | 23067df | 2008-02-13 22:06:36 +0000 | [diff] [blame] | 762 | BasicBlock *OrigDestBB = NULL; |
| 763 | if (ExitingBR->getSuccessor(0) == ExitBB) { |
| 764 | OrigDestBB = ExitingBR->getSuccessor(1); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 765 | ExitingBR->setSuccessor(1, ActiveBB); |
Devang Patel | 23067df | 2008-02-13 22:06:36 +0000 | [diff] [blame] | 766 | } |
| 767 | else { |
| 768 | OrigDestBB = ExitingBR->getSuccessor(0); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 769 | ExitingBR->setSuccessor(0, ActiveBB); |
Devang Patel | 23067df | 2008-02-13 22:06:36 +0000 | [diff] [blame] | 770 | } |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 771 | |
| 772 | // Remove split condition and current split condition branch. |
| 773 | SC->eraseFromParent(); |
| 774 | CurrentBR->eraseFromParent(); |
| 775 | |
Devang Patel | 23067df | 2008-02-13 22:06:36 +0000 | [diff] [blame] | 776 | // Connect exiting block to original destination. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 777 | BranchInst::Create(OrigDestBB, ExitingBB); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 778 | |
| 779 | // Update PHINodes |
Devang Patel | ea06906 | 2008-02-13 22:23:07 +0000 | [diff] [blame] | 780 | updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 781 | |
| 782 | // Fix dominator info. |
| 783 | // ExitBB is now dominated by CondBB |
| 784 | DT->changeImmediateDominator(ExitBB, CondBB); |
| 785 | DF->changeImmediateDominator(ExitBB, CondBB, DT); |
| 786 | |
| 787 | // Basicblocks dominated by ActiveBB may have ExitingBB or |
| 788 | // a basic block outside the loop in their DF list. If so, |
| 789 | // replace it with CondBB. |
| 790 | DomTreeNode *Node = DT->getNode(ActiveBB); |
| 791 | for (df_iterator<DomTreeNode *> DI = df_begin(Node), DE = df_end(Node); |
| 792 | DI != DE; ++DI) { |
| 793 | BasicBlock *BB = DI->getBlock(); |
| 794 | DominanceFrontier::iterator BBDF = DF->find(BB); |
| 795 | DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); |
| 796 | DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); |
| 797 | while (DomSetI != DomSetE) { |
| 798 | DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; |
| 799 | ++DomSetI; |
| 800 | BasicBlock *DFBB = *CurrentItr; |
| 801 | if (DFBB == ExitingBB || !L->contains(DFBB)) { |
| 802 | BBDF->second.erase(DFBB); |
| 803 | BBDF->second.insert(CondBB); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /// updatePHINodes - CFG has been changed. |
| 810 | /// Before |
| 811 | /// - ExitBB's single predecessor was Latch |
| 812 | /// - Latch's second successor was Header |
| 813 | /// Now |
Devang Patel | 82ada54 | 2008-02-08 22:49:13 +0000 | [diff] [blame] | 814 | /// - ExitBB's single predecessor is Header |
| 815 | /// - Latch's one and only successor is Header |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 816 | /// |
| 817 | /// Update ExitBB PHINodes' to reflect this change. |
| 818 | void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, |
| 819 | BasicBlock *Header, |
Devang Patel | ea06906 | 2008-02-13 22:23:07 +0000 | [diff] [blame] | 820 | PHINode *IV, Instruction *IVIncrement, |
| 821 | Loop *LP) { |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 822 | |
| 823 | for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end(); |
Devang Patel | 4a3c0ac | 2008-03-27 17:32:46 +0000 | [diff] [blame] | 824 | BI != BE; ) { |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 825 | PHINode *PN = dyn_cast<PHINode>(BI); |
Devang Patel | 4a3c0ac | 2008-03-27 17:32:46 +0000 | [diff] [blame] | 826 | ++BI; |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 827 | if (!PN) |
| 828 | break; |
| 829 | |
| 830 | Value *V = PN->getIncomingValueForBlock(Latch); |
| 831 | if (PHINode *PHV = dyn_cast<PHINode>(V)) { |
Devang Patel | 82ada54 | 2008-02-08 22:49:13 +0000 | [diff] [blame] | 832 | // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use |
| 833 | // in Header which is new incoming value for PN. |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 834 | Value *NewV = NULL; |
| 835 | for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end(); |
Devang Patel | 82ada54 | 2008-02-08 22:49:13 +0000 | [diff] [blame] | 836 | UI != E; ++UI) |
| 837 | if (PHINode *U = dyn_cast<PHINode>(*UI)) |
Devang Patel | ea06906 | 2008-02-13 22:23:07 +0000 | [diff] [blame] | 838 | if (LP->contains(U->getParent())) { |
Devang Patel | 82ada54 | 2008-02-08 22:49:13 +0000 | [diff] [blame] | 839 | NewV = U; |
| 840 | break; |
| 841 | } |
| 842 | |
Devang Patel | 60a1290 | 2008-03-24 20:16:14 +0000 | [diff] [blame] | 843 | // Add incoming value from header only if PN has any use inside the loop. |
| 844 | if (NewV) |
| 845 | PN->addIncoming(NewV, Header); |
Devang Patel | d79faee | 2007-08-25 02:39:24 +0000 | [diff] [blame] | 846 | |
| 847 | } else if (Instruction *PHI = dyn_cast<Instruction>(V)) { |
| 848 | // If this instruction is IVIncrement then IV is new incoming value |
| 849 | // from header otherwise this instruction must be incoming value from |
| 850 | // header because loop is in LCSSA form. |
| 851 | if (PHI == IVIncrement) |
| 852 | PN->addIncoming(IV, Header); |
| 853 | else |
| 854 | PN->addIncoming(V, Header); |
| 855 | } else |
| 856 | // Otherwise this is an incoming value from header because loop is in |
| 857 | // LCSSA form. |
| 858 | PN->addIncoming(V, Header); |
| 859 | |
| 860 | // Remove incoming value from Latch. |
| 861 | PN->removeIncomingValue(Latch); |
| 862 | } |
| 863 | } |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 864 | |
| 865 | bool LoopIndexSplit::splitLoop() { |
| 866 | SplitCondition = NULL; |
| 867 | if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE |
| 868 | || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) |
| 869 | return false; |
| 870 | BasicBlock *Header = L->getHeader(); |
| 871 | BasicBlock *Latch = L->getLoopLatch(); |
| 872 | BranchInst *SBR = NULL; // Split Condition Branch |
| 873 | BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator()); |
| 874 | // If Exiting block includes loop variant instructions then this |
| 875 | // loop may not be split safely. |
| 876 | BasicBlock *ExitingBlock = ExitCondition->getParent(); |
| 877 | if (!cleanBlock(ExitingBlock)) return false; |
| 878 | |
| 879 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 880 | I != E; ++I) { |
| 881 | BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator()); |
| 882 | if (!BR || BR->isUnconditional()) continue; |
| 883 | ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition()); |
| 884 | if (!CI || CI == ExitCondition |
| 885 | || CI->getPredicate() == ICmpInst::ICMP_NE |
| 886 | || CI->getPredicate() == ICmpInst::ICMP_EQ) |
| 887 | continue; |
| 888 | |
| 889 | // Unable to handle triangle loops at the moment. |
| 890 | // In triangle loop, split condition is in header and one of the |
| 891 | // the split destination is loop latch. If split condition is EQ |
| 892 | // then such loops are already handle in processOneIterationLoop(). |
| 893 | if (Header == (*I) |
| 894 | && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1))) |
| 895 | continue; |
| 896 | |
| 897 | // If the block does not dominate the latch then this is not a diamond. |
| 898 | // Such loop may not benefit from index split. |
| 899 | if (!DT->dominates((*I), Latch)) |
| 900 | continue; |
| 901 | |
| 902 | // If split condition branches heads do not have single predecessor, |
| 903 | // SplitCondBlock, then is not possible to remove inactive branch. |
| 904 | if (!BR->getSuccessor(0)->getSinglePredecessor() |
| 905 | || !BR->getSuccessor(1)->getSinglePredecessor()) |
| 906 | return false; |
| 907 | |
| 908 | // If the merge point for BR is not loop latch then skip this condition. |
| 909 | if (BR->getSuccessor(0) != Latch) { |
| 910 | DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); |
| 911 | assert (DF0 != DF->end() && "Unable to find dominance frontier"); |
| 912 | if (!DF0->second.count(Latch)) |
| 913 | continue; |
| 914 | } |
| 915 | |
| 916 | if (BR->getSuccessor(1) != Latch) { |
| 917 | DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); |
| 918 | assert (DF1 != DF->end() && "Unable to find dominance frontier"); |
| 919 | if (!DF1->second.count(Latch)) |
| 920 | continue; |
| 921 | } |
| 922 | SplitCondition = CI; |
| 923 | SBR = BR; |
| 924 | break; |
| 925 | } |
| 926 | |
| 927 | if (!SplitCondition) |
| 928 | return false; |
| 929 | |
| 930 | // If the predicate sign does not match then skip. |
| 931 | if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate()) |
| 932 | return false; |
| 933 | |
| 934 | unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue); |
| 935 | unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0)); |
| 936 | Value *SplitValue = SplitCondition->getOperand(SVOpNum); |
| 937 | if (!L->isLoopInvariant(SplitValue)) |
| 938 | return false; |
| 939 | if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum))) |
| 940 | return false; |
| 941 | |
| 942 | // Normalize loop conditions so that it is easier to calculate new loop |
| 943 | // bounds. |
| 944 | if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { |
| 945 | ExitCondition->setPredicate(ExitCondition->getInversePredicate()); |
| 946 | BasicBlock *T = EBR->getSuccessor(0); |
| 947 | EBR->setSuccessor(0, EBR->getSuccessor(1)); |
| 948 | EBR->setSuccessor(1, T); |
| 949 | } |
| 950 | |
| 951 | if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) { |
| 952 | SplitCondition->setPredicate(SplitCondition->getInversePredicate()); |
| 953 | BasicBlock *T = SBR->getSuccessor(0); |
| 954 | SBR->setSuccessor(0, SBR->getSuccessor(1)); |
| 955 | SBR->setSuccessor(1, T); |
| 956 | } |
| 957 | |
| 958 | //[*] Calculate new loop bounds. |
| 959 | Value *AEV = SplitValue; |
| 960 | Value *BSV = SplitValue; |
| 961 | bool Sign = SplitCondition->isSignedPredicate(); |
| 962 | Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); |
| 963 | |
| 964 | if (IVisLT(*ExitCondition)) { |
| 965 | if (IVisLT(*SplitCondition)) { |
| 966 | /* Do nothing */ |
| 967 | } |
| 968 | else if (IVisLE(*SplitCondition)) { |
| 969 | AEV = getPlusOne(SplitValue, Sign, PHTerm); |
| 970 | BSV = getPlusOne(SplitValue, Sign, PHTerm); |
| 971 | } else { |
| 972 | assert (0 && "Unexpected split condition!"); |
| 973 | } |
| 974 | } |
| 975 | else if (IVisLE(*ExitCondition)) { |
| 976 | if (IVisLT(*SplitCondition)) { |
| 977 | AEV = getMinusOne(SplitValue, Sign, PHTerm); |
| 978 | } |
| 979 | else if (IVisLE(*SplitCondition)) { |
| 980 | BSV = getPlusOne(SplitValue, Sign, PHTerm); |
| 981 | } else { |
| 982 | assert (0 && "Unexpected split condition!"); |
| 983 | } |
| 984 | } else { |
| 985 | assert (0 && "Unexpected exit condition!"); |
| 986 | } |
| 987 | AEV = getMin(AEV, IVExitValue, Sign, PHTerm); |
| 988 | BSV = getMax(BSV, IVStartValue, Sign, PHTerm); |
| 989 | |
| 990 | // [*] Clone Loop |
| 991 | DenseMap<const Value *, Value *> ValueMap; |
| 992 | Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this); |
| 993 | Loop *ALoop = L; |
| 994 | |
| 995 | // [*] ALoop's exiting edge enters BLoop's header. |
| 996 | // ALoop's original exit block becomes BLoop's exit block. |
| 997 | PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]); |
| 998 | BasicBlock *A_ExitingBlock = ExitCondition->getParent(); |
| 999 | BranchInst *A_ExitInsn = |
| 1000 | dyn_cast<BranchInst>(A_ExitingBlock->getTerminator()); |
| 1001 | assert (A_ExitInsn && "Unable to find suitable loop exit branch"); |
| 1002 | BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1); |
| 1003 | BasicBlock *B_Header = BLoop->getHeader(); |
| 1004 | if (ALoop->contains(B_ExitBlock)) { |
| 1005 | B_ExitBlock = A_ExitInsn->getSuccessor(0); |
| 1006 | A_ExitInsn->setSuccessor(0, B_Header); |
| 1007 | } else |
| 1008 | A_ExitInsn->setSuccessor(1, B_Header); |
| 1009 | |
| 1010 | // [*] Update ALoop's exit value using new exit value. |
| 1011 | ExitCondition->setOperand(EVOpNum, AEV); |
| 1012 | |
| 1013 | // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from |
| 1014 | // original loop's preheader. Add incoming PHINode values from |
| 1015 | // ALoop's exiting block. Update BLoop header's domiantor info. |
| 1016 | |
| 1017 | // Collect inverse map of Header PHINodes. |
| 1018 | DenseMap<Value *, Value *> InverseMap; |
| 1019 | for (BasicBlock::iterator BI = ALoop->getHeader()->begin(), |
| 1020 | BE = ALoop->getHeader()->end(); BI != BE; ++BI) { |
| 1021 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 1022 | PHINode *PNClone = cast<PHINode>(ValueMap[PN]); |
| 1023 | InverseMap[PNClone] = PN; |
| 1024 | } else |
| 1025 | break; |
| 1026 | } |
| 1027 | |
| 1028 | BasicBlock *A_Preheader = ALoop->getLoopPreheader(); |
| 1029 | for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end(); |
| 1030 | BI != BE; ++BI) { |
| 1031 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 1032 | // Remove incoming value from original preheader. |
| 1033 | PN->removeIncomingValue(A_Preheader); |
| 1034 | |
| 1035 | // Add incoming value from A_ExitingBlock. |
| 1036 | if (PN == B_IndVar) |
| 1037 | PN->addIncoming(BSV, A_ExitingBlock); |
| 1038 | else { |
| 1039 | PHINode *OrigPN = cast<PHINode>(InverseMap[PN]); |
| 1040 | Value *V2 = NULL; |
| 1041 | // If loop header is also loop exiting block then |
| 1042 | // OrigPN is incoming value for B loop header. |
| 1043 | if (A_ExitingBlock == ALoop->getHeader()) |
| 1044 | V2 = OrigPN; |
| 1045 | else |
| 1046 | V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock); |
| 1047 | PN->addIncoming(V2, A_ExitingBlock); |
| 1048 | } |
| 1049 | } else |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | DT->changeImmediateDominator(B_Header, A_ExitingBlock); |
| 1054 | DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT); |
| 1055 | |
| 1056 | // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit |
| 1057 | // block. Remove incoming PHINode values from ALoop's exiting block. |
| 1058 | // Add new incoming values from BLoop's incoming exiting value. |
| 1059 | // Update BLoop exit block's dominator info.. |
| 1060 | BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]); |
| 1061 | for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end(); |
| 1062 | BI != BE; ++BI) { |
| 1063 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 1064 | PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)], |
| 1065 | B_ExitingBlock); |
| 1066 | PN->removeIncomingValue(A_ExitingBlock); |
| 1067 | } else |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock); |
| 1072 | DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT); |
| 1073 | |
Dan Gohman | f159ccd | 2009-04-29 22:01:05 +0000 | [diff] [blame^] | 1074 | //[*] Split ALoop's exit edge. This creates a new block which |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1075 | // serves two purposes. First one is to hold PHINode defnitions |
| 1076 | // to ensure that ALoop's LCSSA form. Second use it to act |
| 1077 | // as a preheader for BLoop. |
| 1078 | BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this); |
| 1079 | |
| 1080 | //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes |
| 1081 | // in A_ExitBlock to redefine outgoing PHI definitions from ALoop. |
| 1082 | for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end(); |
| 1083 | BI != BE; ++BI) { |
| 1084 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 1085 | Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock); |
| 1086 | PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName()); |
| 1087 | newPHI->addIncoming(V1, A_ExitingBlock); |
| 1088 | A_ExitBlock->getInstList().push_front(newPHI); |
| 1089 | PN->removeIncomingValue(A_ExitBlock); |
| 1090 | PN->addIncoming(newPHI, A_ExitBlock); |
| 1091 | } else |
| 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | //[*] Eliminate split condition's inactive branch from ALoop. |
| 1096 | BasicBlock *A_SplitCondBlock = SplitCondition->getParent(); |
| 1097 | BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator()); |
| 1098 | BasicBlock *A_InactiveBranch = NULL; |
| 1099 | BasicBlock *A_ActiveBranch = NULL; |
| 1100 | A_ActiveBranch = A_BR->getSuccessor(0); |
| 1101 | A_InactiveBranch = A_BR->getSuccessor(1); |
| 1102 | A_BR->setUnconditionalDest(A_ActiveBranch); |
| 1103 | removeBlocks(A_InactiveBranch, L, A_ActiveBranch); |
| 1104 | |
| 1105 | //[*] Eliminate split condition's inactive branch in from BLoop. |
| 1106 | BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]); |
| 1107 | BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator()); |
| 1108 | BasicBlock *B_InactiveBranch = NULL; |
| 1109 | BasicBlock *B_ActiveBranch = NULL; |
| 1110 | B_ActiveBranch = B_BR->getSuccessor(1); |
| 1111 | B_InactiveBranch = B_BR->getSuccessor(0); |
| 1112 | B_BR->setUnconditionalDest(B_ActiveBranch); |
| 1113 | removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch); |
| 1114 | |
| 1115 | BasicBlock *A_Header = ALoop->getHeader(); |
| 1116 | if (A_ExitingBlock == A_Header) |
| 1117 | return true; |
| 1118 | |
| 1119 | //[*] Move exit condition into split condition block to avoid |
| 1120 | // executing dead loop iteration. |
| 1121 | ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]); |
| 1122 | Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]); |
| 1123 | ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]); |
| 1124 | |
| 1125 | moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition, |
| 1126 | cast<ICmpInst>(SplitCondition), IndVar, IVIncrement, |
| 1127 | ALoop, EVOpNum); |
| 1128 | |
| 1129 | moveExitCondition(B_SplitCondBlock, B_ActiveBranch, |
| 1130 | B_ExitBlock, B_ExitCondition, |
| 1131 | B_SplitCondition, B_IndVar, B_IndVarIncrement, |
| 1132 | BLoop, EVOpNum); |
| 1133 | |
| 1134 | NumIndexSplit++; |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
| 1138 | /// cleanBlock - A block is considered clean if all non terminal instructions |
| 1139 | /// are either, PHINodes, IV based. |
| 1140 | bool LoopIndexSplit::cleanBlock(BasicBlock *BB) { |
| 1141 | Instruction *Terminator = BB->getTerminator(); |
| 1142 | for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); |
| 1143 | BI != BE; ++BI) { |
| 1144 | Instruction *I = BI; |
| 1145 | |
| 1146 | if (isa<PHINode>(I) || I == Terminator || I == ExitCondition |
Devang Patel | d96c60d | 2009-02-06 06:19:06 +0000 | [diff] [blame] | 1147 | || I == SplitCondition || IVBasedValues.count(I) |
| 1148 | || isa<DbgInfoIntrinsic>(I)) |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1149 | continue; |
| 1150 | |
| 1151 | if (I->mayWriteToMemory()) |
| 1152 | return false; |
| 1153 | |
| 1154 | // I is used only inside this block then it is OK. |
| 1155 | bool usedOutsideBB = false; |
| 1156 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 1157 | UI != UE; ++UI) { |
| 1158 | Instruction *U = cast<Instruction>(UI); |
| 1159 | if (U->getParent() != BB) |
| 1160 | usedOutsideBB = true; |
| 1161 | } |
| 1162 | if (!usedOutsideBB) |
| 1163 | continue; |
| 1164 | |
| 1165 | // Otherwise we have a instruction that may not allow loop spliting. |
| 1166 | return false; |
| 1167 | } |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 1171 | /// IVisLT - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1172 | /// IV based value is less than the loop invariant then return the loop |
| 1173 | /// invariant. Otherwise return NULL. |
| 1174 | Value * LoopIndexSplit::IVisLT(ICmpInst &Op) { |
| 1175 | ICmpInst::Predicate P = Op.getPredicate(); |
| 1176 | if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) |
| 1177 | && IVBasedValues.count(Op.getOperand(0)) |
| 1178 | && L->isLoopInvariant(Op.getOperand(1))) |
| 1179 | return Op.getOperand(1); |
| 1180 | |
| 1181 | if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) |
| 1182 | && IVBasedValues.count(Op.getOperand(1)) |
| 1183 | && L->isLoopInvariant(Op.getOperand(0))) |
| 1184 | return Op.getOperand(0); |
| 1185 | |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 1189 | /// IVisLE - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1190 | /// IV based value is less than or equal to the loop invariant then |
| 1191 | /// return the loop invariant. Otherwise return NULL. |
| 1192 | Value * LoopIndexSplit::IVisLE(ICmpInst &Op) { |
| 1193 | ICmpInst::Predicate P = Op.getPredicate(); |
| 1194 | if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) |
| 1195 | && IVBasedValues.count(Op.getOperand(0)) |
| 1196 | && L->isLoopInvariant(Op.getOperand(1))) |
| 1197 | return Op.getOperand(1); |
| 1198 | |
| 1199 | if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) |
| 1200 | && IVBasedValues.count(Op.getOperand(1)) |
| 1201 | && L->isLoopInvariant(Op.getOperand(0))) |
| 1202 | return Op.getOperand(0); |
| 1203 | |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 1207 | /// IVisGT - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1208 | /// IV based value is greater than the loop invariant then return the loop |
| 1209 | /// invariant. Otherwise return NULL. |
| 1210 | Value * LoopIndexSplit::IVisGT(ICmpInst &Op) { |
| 1211 | ICmpInst::Predicate P = Op.getPredicate(); |
| 1212 | if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) |
| 1213 | && IVBasedValues.count(Op.getOperand(0)) |
| 1214 | && L->isLoopInvariant(Op.getOperand(1))) |
| 1215 | return Op.getOperand(1); |
| 1216 | |
| 1217 | if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) |
| 1218 | && IVBasedValues.count(Op.getOperand(1)) |
| 1219 | && L->isLoopInvariant(Op.getOperand(0))) |
| 1220 | return Op.getOperand(0); |
| 1221 | |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | |
Devang Patel | 042b877 | 2008-12-08 17:07:24 +0000 | [diff] [blame] | 1225 | /// IVisGE - If Op is comparing IV based value with an loop invariant and |
Devang Patel | 3831005 | 2008-12-04 21:38:42 +0000 | [diff] [blame] | 1226 | /// IV based value is greater than or equal to the loop invariant then |
| 1227 | /// return the loop invariant. Otherwise return NULL. |
| 1228 | Value * LoopIndexSplit::IVisGE(ICmpInst &Op) { |
| 1229 | ICmpInst::Predicate P = Op.getPredicate(); |
| 1230 | if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) |
| 1231 | && IVBasedValues.count(Op.getOperand(0)) |
| 1232 | && L->isLoopInvariant(Op.getOperand(1))) |
| 1233 | return Op.getOperand(1); |
| 1234 | |
| 1235 | if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) |
| 1236 | && IVBasedValues.count(Op.getOperand(1)) |
| 1237 | && L->isLoopInvariant(Op.getOperand(0))) |
| 1238 | return Op.getOperand(0); |
| 1239 | |
| 1240 | return NULL; |
| 1241 | } |
| 1242 | |