Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 1 | //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Devang Patel and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Loop Index Splitting Pass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "loop-index-split" |
| 15 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar.h" |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
| 19 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Devang Patel | 95fd717 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Dominators.h" |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 22 | #include "llvm/Transforms/Utils/Cloning.h" |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumIndexSplit, "Number of loops index split"); |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass { |
| 33 | |
| 34 | public: |
| 35 | static char ID; // Pass ID, replacement for typeid |
| 36 | LoopIndexSplit() : LoopPass((intptr_t)&ID) {} |
| 37 | |
| 38 | // Index split Loop L. Return true if loop is split. |
| 39 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 40 | |
| 41 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 42 | AU.addRequired<ScalarEvolution>(); |
| 43 | AU.addPreserved<ScalarEvolution>(); |
| 44 | AU.addRequiredID(LCSSAID); |
| 45 | AU.addPreservedID(LCSSAID); |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 46 | AU.addRequired<LoopInfo>(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 47 | AU.addPreserved<LoopInfo>(); |
| 48 | AU.addRequiredID(LoopSimplifyID); |
| 49 | AU.addPreservedID(LoopSimplifyID); |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 50 | AU.addRequired<DominatorTree>(); |
Devang Patel | 95fd717 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 51 | AU.addPreserved<DominatorTree>(); |
| 52 | AU.addPreserved<DominanceFrontier>(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private: |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 56 | |
| 57 | class SplitInfo { |
| 58 | public: |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 59 | SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {} |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 60 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 61 | // Induction variable's range is split at this value. |
| 62 | Value *SplitValue; |
| 63 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 64 | // This compare instruction compares IndVar against SplitValue. |
| 65 | ICmpInst *SplitCondition; |
| 66 | |
Devang Patel | 3169633 | 2007-08-08 21:18:27 +0000 | [diff] [blame] | 67 | // Clear split info. |
| 68 | void clear() { |
Devang Patel | 3169633 | 2007-08-08 21:18:27 +0000 | [diff] [blame] | 69 | SplitValue = NULL; |
Devang Patel | 3169633 | 2007-08-08 21:18:27 +0000 | [diff] [blame] | 70 | SplitCondition = NULL; |
Devang Patel | 3169633 | 2007-08-08 21:18:27 +0000 | [diff] [blame] | 71 | } |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 72 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 73 | }; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 74 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 75 | private: |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 76 | /// Find condition inside a loop that is suitable candidate for index split. |
| 77 | void findSplitCondition(); |
| 78 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 79 | /// Find loop's exit condition. |
| 80 | void findLoopConditionals(); |
| 81 | |
| 82 | /// Return induction variable associated with value V. |
| 83 | void findIndVar(Value *V, Loop *L); |
| 84 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 85 | /// processOneIterationLoop - Current loop L contains compare instruction |
| 86 | /// that compares induction variable, IndVar, agains loop invariant. If |
| 87 | /// entire (i.e. meaningful) loop body is dominated by this compare |
| 88 | /// instruction then loop body is executed only for one iteration. In |
| 89 | /// such case eliminate loop structure surrounding this loop body. For |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 90 | bool processOneIterationLoop(SplitInfo &SD); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 91 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 92 | /// If loop header includes loop variant instruction operands then |
| 93 | /// this loop may not be eliminated. |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 94 | bool safeHeader(SplitInfo &SD, BasicBlock *BB); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 95 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 96 | /// If Exit block includes loop variant instructions then this |
| 97 | /// loop may not be eliminated. |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 98 | bool safeExitBlock(SplitInfo &SD, BasicBlock *BB); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 99 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 100 | /// removeBlocks - Remove basic block BB and all blocks dominated by BB. |
Devang Patel | d79bfdd | 2007-08-13 22:22:13 +0000 | [diff] [blame] | 101 | void removeBlocks(BasicBlock *InBB, Loop *LP); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 102 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 103 | /// Find cost of spliting loop L. |
| 104 | unsigned findSplitCost(Loop *L, SplitInfo &SD); |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 105 | bool splitLoop(SplitInfo &SD); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 106 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 107 | void initialize() { |
| 108 | IndVar = NULL; |
| 109 | IndVarIncrement = NULL; |
| 110 | ExitCondition = NULL; |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 111 | StartValue = NULL; |
| 112 | ExitValueNum = 0; |
| 113 | SplitData.clear(); |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 116 | private: |
| 117 | |
| 118 | // Current Loop. |
| 119 | Loop *L; |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 120 | LPPassManager *LPM; |
| 121 | LoopInfo *LI; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 122 | ScalarEvolution *SE; |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 123 | DominatorTree *DT; |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 124 | DominanceFrontier *DF; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 125 | SmallVector<SplitInfo, 4> SplitData; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 126 | |
| 127 | // Induction variable whose range is being split by this transformation. |
| 128 | PHINode *IndVar; |
| 129 | Instruction *IndVarIncrement; |
| 130 | |
| 131 | // Loop exit condition. |
| 132 | ICmpInst *ExitCondition; |
| 133 | |
| 134 | // Induction variable's initial value. |
| 135 | Value *StartValue; |
| 136 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 137 | // Induction variable's final loop exit value operand number in exit condition.. |
| 138 | unsigned ExitValueNum; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | char LoopIndexSplit::ID = 0; |
| 142 | RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops"); |
| 143 | } |
| 144 | |
| 145 | LoopPass *llvm::createLoopIndexSplitPass() { |
| 146 | return new LoopIndexSplit(); |
| 147 | } |
| 148 | |
| 149 | // Index split Loop L. Return true if loop is split. |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 150 | bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) { |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 151 | bool Changed = false; |
| 152 | L = IncomingLoop; |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 153 | LPM = &LPM_Ref; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 154 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 155 | SE = &getAnalysis<ScalarEvolution>(); |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 156 | DT = &getAnalysis<DominatorTree>(); |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 157 | LI = &getAnalysis<LoopInfo>(); |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 158 | DF = getAnalysisToUpdate<DominanceFrontier>(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 159 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 160 | initialize(); |
| 161 | |
| 162 | findLoopConditionals(); |
| 163 | |
| 164 | if (!ExitCondition) |
| 165 | return false; |
| 166 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 167 | findSplitCondition(); |
| 168 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 169 | if (SplitData.empty()) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 170 | return false; |
| 171 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 172 | // First see if it is possible to eliminate loop itself or not. |
| 173 | for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(), |
| 174 | E = SplitData.end(); SI != E; ++SI) { |
| 175 | SplitInfo &SD = *SI; |
| 176 | if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) { |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 177 | Changed = processOneIterationLoop(SD); |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 178 | if (Changed) { |
| 179 | ++NumIndexSplit; |
| 180 | // If is loop is eliminated then nothing else to do here. |
| 181 | return Changed; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 186 | unsigned MaxCost = 99; |
| 187 | unsigned Index = 0; |
| 188 | unsigned MostProfitableSDIndex = 0; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 189 | for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(), |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 190 | E = SplitData.end(); SI != E; ++SI, ++Index) { |
| 191 | SplitInfo SD = *SI; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 192 | |
| 193 | // ICM_EQs are already handled above. |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 194 | if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 195 | continue; |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 196 | |
| 197 | unsigned Cost = findSplitCost(L, SD); |
| 198 | if (Cost < MaxCost) |
| 199 | MostProfitableSDIndex = Index; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 200 | } |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 201 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 202 | // Split most profitiable condition. |
| 203 | Changed = splitLoop(SplitData[MostProfitableSDIndex]); |
| 204 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 205 | if (Changed) |
| 206 | ++NumIndexSplit; |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 207 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 208 | return Changed; |
| 209 | } |
| 210 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 211 | /// Return true if V is a induction variable or induction variable's |
| 212 | /// increment for loop L. |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 213 | void LoopIndexSplit::findIndVar(Value *V, Loop *L) { |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 214 | |
| 215 | Instruction *I = dyn_cast<Instruction>(V); |
| 216 | if (!I) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 217 | return; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 218 | |
| 219 | // Check if I is a phi node from loop header or not. |
| 220 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 221 | if (PN->getParent() == L->getHeader()) { |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 222 | IndVar = PN; |
| 223 | return; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | // Check if I is a add instruction whose one operand is |
| 228 | // phi node from loop header and second operand is constant. |
| 229 | if (I->getOpcode() != Instruction::Add) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 230 | return; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 231 | |
| 232 | Value *Op0 = I->getOperand(0); |
| 233 | Value *Op1 = I->getOperand(1); |
| 234 | |
| 235 | if (PHINode *PN = dyn_cast<PHINode>(Op0)) { |
| 236 | if (PN->getParent() == L->getHeader() |
| 237 | && isa<ConstantInt>(Op1)) { |
| 238 | IndVar = PN; |
| 239 | IndVarIncrement = I; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 240 | return; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | if (PHINode *PN = dyn_cast<PHINode>(Op1)) { |
| 245 | if (PN->getParent() == L->getHeader() |
| 246 | && isa<ConstantInt>(Op0)) { |
| 247 | IndVar = PN; |
| 248 | IndVarIncrement = I; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 249 | return; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Find loop's exit condition and associated induction variable. |
| 257 | void LoopIndexSplit::findLoopConditionals() { |
| 258 | |
| 259 | BasicBlock *ExitBlock = NULL; |
| 260 | |
| 261 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 262 | I != E; ++I) { |
| 263 | BasicBlock *BB = *I; |
| 264 | if (!L->isLoopExit(BB)) |
| 265 | continue; |
| 266 | if (ExitBlock) |
| 267 | return; |
| 268 | ExitBlock = BB; |
| 269 | } |
| 270 | |
| 271 | if (!ExitBlock) |
| 272 | return; |
| 273 | |
| 274 | // If exit block's terminator is conditional branch inst then we have found |
| 275 | // exit condition. |
| 276 | BranchInst *BR = dyn_cast<BranchInst>(ExitBlock->getTerminator()); |
| 277 | if (!BR || BR->isUnconditional()) |
| 278 | return; |
| 279 | |
| 280 | ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition()); |
| 281 | if (!CI) |
| 282 | return; |
| 283 | |
| 284 | ExitCondition = CI; |
| 285 | |
| 286 | // Exit condition's one operand is loop invariant exit value and second |
| 287 | // operand is SCEVAddRecExpr based on induction variable. |
| 288 | Value *V0 = CI->getOperand(0); |
| 289 | Value *V1 = CI->getOperand(1); |
| 290 | |
| 291 | SCEVHandle SH0 = SE->getSCEV(V0); |
| 292 | SCEVHandle SH1 = SE->getSCEV(V1); |
| 293 | |
| 294 | if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) { |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 295 | ExitValueNum = 0; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 296 | findIndVar(V1, L); |
| 297 | } |
| 298 | else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) { |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 299 | ExitValueNum = 1; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 300 | findIndVar(V0, L); |
| 301 | } |
| 302 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 303 | if (!IndVar) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 304 | ExitCondition = NULL; |
| 305 | else if (IndVar) { |
| 306 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 307 | StartValue = IndVar->getIncomingValueForBlock(Preheader); |
| 308 | } |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 311 | /// Find condition inside a loop that is suitable candidate for index split. |
| 312 | void LoopIndexSplit::findSplitCondition() { |
| 313 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 314 | SplitInfo SD; |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 315 | // Check all basic block's terminators. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 316 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 317 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 318 | I != E; ++I) { |
| 319 | BasicBlock *BB = *I; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 320 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 321 | // If this basic block does not terminate in a conditional branch |
| 322 | // then terminator is not a suitable split condition. |
| 323 | BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator()); |
| 324 | if (!BR) |
| 325 | continue; |
| 326 | |
| 327 | if (BR->isUnconditional()) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 328 | continue; |
| 329 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 330 | ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition()); |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 331 | if (!CI || CI == ExitCondition) |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 332 | return; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 333 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 334 | // If one operand is loop invariant and second operand is SCEVAddRecExpr |
| 335 | // based on induction variable then CI is a candidate split condition. |
| 336 | Value *V0 = CI->getOperand(0); |
| 337 | Value *V1 = CI->getOperand(1); |
| 338 | |
| 339 | SCEVHandle SH0 = SE->getSCEV(V0); |
| 340 | SCEVHandle SH1 = SE->getSCEV(V1); |
| 341 | |
| 342 | if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) { |
| 343 | SD.SplitValue = V0; |
| 344 | SD.SplitCondition = CI; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 345 | if (PHINode *PN = dyn_cast<PHINode>(V1)) { |
| 346 | if (PN == IndVar) |
| 347 | SplitData.push_back(SD); |
| 348 | } |
| 349 | else if (Instruction *Insn = dyn_cast<Instruction>(V1)) { |
| 350 | if (IndVarIncrement && IndVarIncrement == Insn) |
| 351 | SplitData.push_back(SD); |
| 352 | } |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 353 | } |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 354 | else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) { |
| 355 | SD.SplitValue = V1; |
| 356 | SD.SplitCondition = CI; |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 357 | if (PHINode *PN = dyn_cast<PHINode>(V0)) { |
| 358 | if (PN == IndVar) |
| 359 | SplitData.push_back(SD); |
| 360 | } |
| 361 | else if (Instruction *Insn = dyn_cast<Instruction>(V0)) { |
| 362 | if (IndVarIncrement && IndVarIncrement == Insn) |
| 363 | SplitData.push_back(SD); |
| 364 | } |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /// processOneIterationLoop - Current loop L contains compare instruction |
| 370 | /// that compares induction variable, IndVar, against loop invariant. If |
| 371 | /// entire (i.e. meaningful) loop body is dominated by this compare |
| 372 | /// instruction then loop body is executed only once. In such case eliminate |
| 373 | /// loop structure surrounding this loop body. For example, |
| 374 | /// for (int i = start; i < end; ++i) { |
| 375 | /// if ( i == somevalue) { |
| 376 | /// loop_body |
| 377 | /// } |
| 378 | /// } |
| 379 | /// can be transformed into |
| 380 | /// if (somevalue >= start && somevalue < end) { |
| 381 | /// i = somevalue; |
| 382 | /// loop_body |
| 383 | /// } |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 384 | bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) { |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 385 | |
| 386 | BasicBlock *Header = L->getHeader(); |
| 387 | |
| 388 | // First of all, check if SplitCondition dominates entire loop body |
| 389 | // or not. |
| 390 | |
| 391 | // If SplitCondition is not in loop header then this loop is not suitable |
| 392 | // for this transformation. |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 393 | if (SD.SplitCondition->getParent() != Header) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 394 | return false; |
| 395 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 396 | // If loop header includes loop variant instruction operands then |
| 397 | // this loop may not be eliminated. |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 398 | if (!safeHeader(SD, Header)) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 399 | return false; |
| 400 | |
| 401 | // If Exit block includes loop variant instructions then this |
| 402 | // loop may not be eliminated. |
Devang Patel | bfa5eba | 2007-08-10 00:59:03 +0000 | [diff] [blame] | 403 | if (!safeExitBlock(SD, ExitCondition->getParent())) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 404 | return false; |
| 405 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 406 | // Update CFG. |
| 407 | |
| 408 | // As a first step to break this loop, remove Latch to Header edge. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 409 | BasicBlock *Latch = L->getLoopLatch(); |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 410 | BasicBlock *LatchSucc = NULL; |
| 411 | BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator()); |
| 412 | if (!BR) |
| 413 | return false; |
| 414 | Header->removePredecessor(Latch); |
| 415 | for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); |
| 416 | SI != E; ++SI) { |
| 417 | if (Header != *SI) |
| 418 | LatchSucc = *SI; |
| 419 | } |
| 420 | BR->setUnconditionalDest(LatchSucc); |
| 421 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 422 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 423 | Instruction *Terminator = Header->getTerminator(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 424 | |
Devang Patel | bc5fe63 | 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 |
| 430 | // c2 = icmp ult i32 vSplitValue, ExitValue |
| 431 | // and i32 c1, c2 |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 432 | bool SignedPredicate = ExitCondition->isSignedPredicate(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 433 | Instruction *C1 = new ICmpInst(SignedPredicate ? |
| 434 | ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 435 | SD.SplitValue, StartValue, "lisplit", |
| 436 | Terminator); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 437 | Instruction *C2 = new ICmpInst(SignedPredicate ? |
| 438 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 439 | SD.SplitValue, |
| 440 | ExitCondition->getOperand(ExitValueNum), "lisplit", |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 441 | Terminator); |
| 442 | Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit", |
| 443 | Terminator); |
| 444 | SD.SplitCondition->replaceAllUsesWith(NSplitCond); |
| 445 | SD.SplitCondition->eraseFromParent(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 446 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 447 | // Now, clear latch block. Remove instructions that are responsible |
| 448 | // to increment induction variable. |
| 449 | Instruction *LTerminator = Latch->getTerminator(); |
| 450 | for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end(); |
| 451 | LB != LE; ) { |
| 452 | Instruction *I = LB; |
| 453 | ++LB; |
| 454 | if (isa<PHINode>(I) || I == LTerminator) |
| 455 | continue; |
| 456 | |
| 457 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Devang Patel | 0d75c29 | 2007-08-07 17:45:35 +0000 | [diff] [blame] | 458 | I->eraseFromParent(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 461 | LPM->deleteLoopFromQueue(L); |
Devang Patel | 95fd717 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 462 | |
| 463 | // Update Dominator Info. |
| 464 | // Only CFG change done is to remove Latch to Header edge. This |
| 465 | // does not change dominator tree because Latch did not dominate |
| 466 | // Header. |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 467 | if (DF) { |
Devang Patel | 95fd717 | 2007-08-08 21:39:47 +0000 | [diff] [blame] | 468 | DominanceFrontier::iterator HeaderDF = DF->find(Header); |
| 469 | if (HeaderDF != DF->end()) |
| 470 | DF->removeFromFrontier(HeaderDF, Header); |
| 471 | |
| 472 | DominanceFrontier::iterator LatchDF = DF->find(Latch); |
| 473 | if (LatchDF != DF->end()) |
| 474 | DF->removeFromFrontier(LatchDF, Header); |
| 475 | } |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | |
| 479 | // If loop header includes loop variant instruction operands then |
| 480 | // this loop can not be eliminated. This is used by processOneIterationLoop(). |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 481 | bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) { |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 482 | |
| 483 | Instruction *Terminator = Header->getTerminator(); |
| 484 | for(BasicBlock::iterator BI = Header->begin(), BE = Header->end(); |
| 485 | BI != BE; ++BI) { |
| 486 | Instruction *I = BI; |
| 487 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 488 | // PHI Nodes are OK. FIXME : Handle last value assignments. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 489 | if (isa<PHINode>(I)) |
| 490 | continue; |
| 491 | |
| 492 | // SplitCondition itself is OK. |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 493 | if (I == SD.SplitCondition) |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 494 | continue; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 495 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 496 | // Induction variable is OK. |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 497 | if (I == IndVar) |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 498 | continue; |
| 499 | |
| 500 | // Induction variable increment is OK. |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 501 | if (I == IndVarIncrement) |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 502 | continue; |
| 503 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 504 | // Terminator is also harmless. |
| 505 | if (I == Terminator) |
| 506 | continue; |
| 507 | |
| 508 | // Otherwise we have a instruction that may not be safe. |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | // If Exit block includes loop variant instructions then this |
| 516 | // loop may not be eliminated. This is used by processOneIterationLoop(). |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 517 | bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) { |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 518 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 519 | for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end(); |
| 520 | BI != BE; ++BI) { |
| 521 | Instruction *I = BI; |
| 522 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 523 | // PHI Nodes are OK. FIXME : Handle last value assignments. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 524 | if (isa<PHINode>(I)) |
| 525 | continue; |
| 526 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 527 | // Induction variable increment is OK. |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 528 | if (IndVarIncrement && IndVarIncrement == I) |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 529 | continue; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 530 | |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 531 | // Check if I is induction variable increment instruction. |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 532 | if (!IndVarIncrement && I->getOpcode() == Instruction::Add) { |
Devang Patel | 2545f7b | 2007-08-09 01:39:01 +0000 | [diff] [blame] | 533 | |
| 534 | Value *Op0 = I->getOperand(0); |
| 535 | Value *Op1 = I->getOperand(1); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 536 | PHINode *PN = NULL; |
| 537 | ConstantInt *CI = NULL; |
| 538 | |
| 539 | if ((PN = dyn_cast<PHINode>(Op0))) { |
| 540 | if ((CI = dyn_cast<ConstantInt>(Op1))) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 541 | IndVarIncrement = I; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 542 | } else |
| 543 | if ((PN = dyn_cast<PHINode>(Op1))) { |
| 544 | if ((CI = dyn_cast<ConstantInt>(Op0))) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 545 | IndVarIncrement = I; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 548 | if (IndVarIncrement && PN == IndVar && CI->isOne()) |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 549 | continue; |
| 550 | } |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 551 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 552 | // I is an Exit condition if next instruction is block terminator. |
| 553 | // Exit condition is OK if it compares loop invariant exit value, |
| 554 | // which is checked below. |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 555 | else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) { |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 556 | if (EC == ExitCondition) |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame] | 557 | continue; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 560 | if (I == ExitBlock->getTerminator()) |
| 561 | continue; |
| 562 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 563 | // Otherwise we have instruction that may not be safe. |
| 564 | return false; |
| 565 | } |
| 566 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 567 | // We could not find any reason to consider ExitBlock unsafe. |
| 568 | return true; |
| 569 | } |
| 570 | |
Devang Patel | 0aaeb17 | 2007-08-08 22:25:28 +0000 | [diff] [blame] | 571 | /// Find cost of spliting loop L. Cost is measured in terms of size growth. |
| 572 | /// Size is growth is calculated based on amount of code duplicated in second |
| 573 | /// loop. |
| 574 | unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) { |
| 575 | |
| 576 | unsigned Cost = 0; |
| 577 | BasicBlock *SDBlock = SD.SplitCondition->getParent(); |
| 578 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 579 | I != E; ++I) { |
| 580 | BasicBlock *BB = *I; |
| 581 | // If a block is not dominated by split condition block then |
| 582 | // it must be duplicated in both loops. |
| 583 | if (!DT->dominates(SDBlock, BB)) |
| 584 | Cost += BB->size(); |
| 585 | } |
| 586 | |
| 587 | return Cost; |
| 588 | } |
| 589 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 590 | /// removeBlocks - Remove basic block BB and all blocks dominated by BB. |
Devang Patel | d79bfdd | 2007-08-13 22:22:13 +0000 | [diff] [blame] | 591 | void LoopIndexSplit::removeBlocks(BasicBlock *InBB, Loop *LP) { |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 592 | |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 593 | SmallVector<std::pair<BasicBlock *, succ_iterator>, 8> WorkList; |
| 594 | WorkList.push_back(std::make_pair(InBB, succ_begin(InBB))); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 595 | while (!WorkList.empty()) { |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 596 | BasicBlock *BB = WorkList.back(). first; |
| 597 | succ_iterator SIter =WorkList.back().second; |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 598 | |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 599 | // If all successor's are processed then remove this block. |
| 600 | if (SIter == succ_end(BB)) { |
| 601 | WorkList.pop_back(); |
| 602 | for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end(); |
| 603 | BBI != BBE; ++BBI) { |
| 604 | Instruction *I = BBI; |
| 605 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 606 | I->eraseFromParent(); |
| 607 | } |
Devang Patel | d79bfdd | 2007-08-13 22:22:13 +0000 | [diff] [blame] | 608 | LPM->deleteSimpleAnalysisValue(BB, LP); |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 609 | DT->eraseNode(BB); |
| 610 | DF->removeBlock(BB); |
| 611 | LI->removeBlock(BB); |
| 612 | BB->eraseFromParent(); |
| 613 | } else { |
| 614 | BasicBlock *SuccBB = *SIter; |
| 615 | ++WorkList.back().second; |
| 616 | |
| 617 | if (DT->dominates(BB, SuccBB)) { |
| 618 | WorkList.push_back(std::make_pair(SuccBB, succ_begin(SuccBB))); |
| 619 | continue; |
| 620 | } else { |
| 621 | // If SuccBB is not dominated by BB then it is not removed, however remove |
| 622 | // any PHI incoming edge from BB. |
| 623 | for(BasicBlock::iterator SBI = SuccBB->begin(), SBE = SuccBB->end(); |
| 624 | SBI != SBE; ++SBI) { |
| 625 | if (PHINode *PN = dyn_cast<PHINode>(SBI)) |
| 626 | PN->removeIncomingValue(BB); |
| 627 | else |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | // If BB is not dominating SuccBB then SuccBB is in BB's dominance |
| 632 | // frontiner. |
| 633 | DominanceFrontier::iterator BBDF = DF->find(BB); |
| 634 | DF->removeFromFrontier(BBDF, SuccBB); |
| 635 | } |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 636 | } |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Devang Patel | c8dadbf | 2007-08-08 21:02:17 +0000 | [diff] [blame] | 640 | bool LoopIndexSplit::splitLoop(SplitInfo &SD) { |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 641 | |
| 642 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 643 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 644 | // True loop is original loop. False loop is cloned loop. |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 645 | |
| 646 | bool SignedPredicate = ExitCondition->isSignedPredicate(); |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 647 | //[*] Calculate True loop's new Exit Value in loop preheader. |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 648 | // TLExitValue = min(SplitValue, ExitValue) |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 649 | //[*] Calculate False loop's new Start Value in loop preheader. |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 650 | // FLStartValue = min(SplitValue, TrueLoop.StartValue) |
| 651 | Value *TLExitValue = NULL; |
| 652 | Value *FLStartValue = NULL; |
| 653 | if (isa<ConstantInt>(SD.SplitValue)) { |
| 654 | TLExitValue = SD.SplitValue; |
| 655 | FLStartValue = SD.SplitValue; |
| 656 | } |
| 657 | else { |
| 658 | Value *C1 = new ICmpInst(SignedPredicate ? |
| 659 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 660 | SD.SplitValue, |
| 661 | ExitCondition->getOperand(ExitValueNum), |
| 662 | "lsplit.ev", |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 663 | Preheader->getTerminator()); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 664 | TLExitValue = new SelectInst(C1, SD.SplitValue, |
| 665 | ExitCondition->getOperand(ExitValueNum), |
Devang Patel | f824fb4 | 2007-08-10 00:53:35 +0000 | [diff] [blame] | 666 | "lsplit.ev", Preheader->getTerminator()); |
| 667 | |
| 668 | Value *C2 = new ICmpInst(SignedPredicate ? |
| 669 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 670 | SD.SplitValue, StartValue, "lsplit.sv", |
| 671 | Preheader->getTerminator()); |
| 672 | FLStartValue = new SelectInst(C2, SD.SplitValue, StartValue, |
| 673 | "lsplit.sv", Preheader->getTerminator()); |
| 674 | } |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 675 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 676 | //[*] Clone loop. Avoid true destination of split condition and |
| 677 | // the blocks dominated by true destination. |
| 678 | DenseMap<const Value *, Value *> ValueMap; |
| 679 | Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this); |
| 680 | BasicBlock *FalseHeader = FalseLoop->getHeader(); |
| 681 | |
| 682 | //[*] True loop's exit edge enters False loop. |
| 683 | PHINode *IndVarClone = cast<PHINode>(ValueMap[IndVar]); |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 684 | BasicBlock *ExitBlock = ExitCondition->getParent(); |
| 685 | BranchInst *ExitInsn = dyn_cast<BranchInst>(ExitBlock->getTerminator()); |
| 686 | assert (ExitInsn && "Unable to find suitable loop exit branch"); |
| 687 | BasicBlock *ExitDest = ExitInsn->getSuccessor(1); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 688 | |
| 689 | for (BasicBlock::iterator BI = FalseHeader->begin(), BE = FalseHeader->end(); |
| 690 | BI != BE; ++BI) { |
| 691 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 692 | PN->removeIncomingValue(Preheader); |
| 693 | if (PN == IndVarClone) |
| 694 | PN->addIncoming(FLStartValue, ExitBlock); |
| 695 | // else { FIXME : Handl last value assignments.} |
| 696 | } |
| 697 | else |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | if (L->contains(ExitDest)) { |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 702 | ExitDest = ExitInsn->getSuccessor(0); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 703 | ExitInsn->setSuccessor(0, FalseHeader); |
| 704 | } else |
| 705 | ExitInsn->setSuccessor(1, FalseHeader); |
| 706 | |
Devang Patel | b763961 | 2007-08-13 22:13:24 +0000 | [diff] [blame] | 707 | if (DT) { |
| 708 | DT->changeImmediateDominator(FalseHeader, ExitBlock); |
| 709 | DT->changeImmediateDominator(ExitDest, cast<BasicBlock>(ValueMap[ExitBlock])); |
| 710 | } |
| 711 | |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 712 | assert (!L->contains(ExitDest) && " Unable to find exit edge destination"); |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 713 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 714 | //[*] Split Exit Edge. |
| 715 | SplitEdge(ExitBlock, FalseHeader, this); |
Devang Patel | 901f67e | 2007-08-10 18:07:13 +0000 | [diff] [blame] | 716 | |
Devang Patel | 61571ca | 2007-08-10 00:33:50 +0000 | [diff] [blame] | 717 | //[*] Eliminate split condition's false branch from True loop. |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 718 | BasicBlock *SplitBlock = SD.SplitCondition->getParent(); |
| 719 | BranchInst *BR = cast<BranchInst>(SplitBlock->getTerminator()); |
| 720 | BasicBlock *FBB = BR->getSuccessor(1); |
| 721 | BR->setUnconditionalDest(BR->getSuccessor(0)); |
Devang Patel | d79bfdd | 2007-08-13 22:22:13 +0000 | [diff] [blame] | 722 | removeBlocks(FBB, L); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 723 | |
| 724 | //[*] Update True loop's exit value using new exit value. |
| 725 | ExitCondition->setOperand(ExitValueNum, TLExitValue); |
| 726 | |
| 727 | //[*] Eliminate split condition's true branch in False loop CFG. |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 728 | BasicBlock *FSplitBlock = cast<BasicBlock>(ValueMap[SplitBlock]); |
| 729 | BranchInst *FBR = cast<BranchInst>(FSplitBlock->getTerminator()); |
| 730 | BasicBlock *TBB = FBR->getSuccessor(0); |
| 731 | FBR->setUnconditionalDest(FBR->getSuccessor(1)); |
Devang Patel | d79bfdd | 2007-08-13 22:22:13 +0000 | [diff] [blame] | 732 | removeBlocks(TBB, FalseLoop); |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 733 | |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 734 | return true; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 735 | } |
Devang Patel | 6a2d6ef | 2007-08-12 07:02:51 +0000 | [diff] [blame] | 736 | |