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