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" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | STATISTIC(NumIndexSplit, "Number of loops index split"); |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass { |
| 30 | |
| 31 | public: |
| 32 | static char ID; // Pass ID, replacement for typeid |
| 33 | LoopIndexSplit() : LoopPass((intptr_t)&ID) {} |
| 34 | |
| 35 | // Index split Loop L. Return true if loop is split. |
| 36 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 37 | |
| 38 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 39 | AU.addRequired<ScalarEvolution>(); |
| 40 | AU.addPreserved<ScalarEvolution>(); |
| 41 | AU.addRequiredID(LCSSAID); |
| 42 | AU.addPreservedID(LCSSAID); |
| 43 | AU.addPreserved<LoopInfo>(); |
| 44 | AU.addRequiredID(LoopSimplifyID); |
| 45 | AU.addPreservedID(LoopSimplifyID); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | /// Find condition inside a loop that is suitable candidate for index split. |
| 50 | void findSplitCondition(); |
| 51 | |
| 52 | /// processOneIterationLoop - Current loop L contains compare instruction |
| 53 | /// that compares induction variable, IndVar, agains loop invariant. If |
| 54 | /// entire (i.e. meaningful) loop body is dominated by this compare |
| 55 | /// instruction then loop body is executed only for one iteration. In |
| 56 | /// such case eliminate loop structure surrounding this loop body. For |
| 57 | bool processOneIterationLoop(LPPassManager &LPM); |
| 58 | |
| 59 | // If loop header includes loop variant instruction operands then |
| 60 | // this loop may not be eliminated. |
| 61 | bool safeHeader(BasicBlock *BB); |
| 62 | |
| 63 | // If Exit block includes loop variant instructions then this |
| 64 | // loop may not be eliminated. |
| 65 | bool safeExitBlock(BasicBlock *BB); |
| 66 | |
| 67 | bool splitLoop(); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | // Current Loop. |
| 72 | Loop *L; |
| 73 | ScalarEvolution *SE; |
| 74 | |
| 75 | // Induction variable whose range is being split by this transformation. |
| 76 | PHINode *IndVar; |
| 77 | |
| 78 | // Induction variable's range is split at this value. |
| 79 | Value *SplitValue; |
| 80 | |
| 81 | // Induction variable's final loop exit value. |
| 82 | Value *ExitValue; |
| 83 | |
| 84 | // This compare instruction compares IndVar against SplitValue. |
| 85 | ICmpInst *SplitCondition; |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 86 | |
| 87 | // Loop exit condition. |
| 88 | ICmpInst *ExitCondition; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | char LoopIndexSplit::ID = 0; |
| 92 | RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops"); |
| 93 | } |
| 94 | |
| 95 | LoopPass *llvm::createLoopIndexSplitPass() { |
| 96 | return new LoopIndexSplit(); |
| 97 | } |
| 98 | |
| 99 | // Index split Loop L. Return true if loop is split. |
| 100 | bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM) { |
| 101 | bool Changed = false; |
| 102 | L = IncomingLoop; |
| 103 | SplitCondition = NULL; |
| 104 | SE = &getAnalysis<ScalarEvolution>(); |
| 105 | |
| 106 | findSplitCondition(); |
| 107 | |
| 108 | if (!SplitCondition) |
| 109 | return false; |
| 110 | |
| 111 | if (SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) |
| 112 | // If it is possible to eliminate loop then do so. |
| 113 | Changed = processOneIterationLoop(LPM); |
| 114 | else |
| 115 | Changed = splitLoop(); |
| 116 | |
| 117 | if (Changed) |
| 118 | ++NumIndexSplit; |
| 119 | |
| 120 | return Changed; |
| 121 | } |
| 122 | |
| 123 | /// Find condition inside a loop that is suitable candidate for index split. |
| 124 | void LoopIndexSplit::findSplitCondition() { |
| 125 | |
| 126 | BasicBlock *Header = L->getHeader(); |
| 127 | |
| 128 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 129 | PHINode *PN = cast<PHINode>(I); |
| 130 | |
| 131 | if (!PN->getType()->isInteger()) |
| 132 | continue; |
| 133 | |
| 134 | SCEVHandle SCEV = SE->getSCEV(PN); |
| 135 | if (!isa<SCEVAddRecExpr>(SCEV)) |
| 136 | continue; |
| 137 | |
| 138 | // If this phi node is used in a compare instruction then it is a |
| 139 | // split condition candidate. |
| 140 | for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 141 | UI != E; ++UI) { |
| 142 | if (ICmpInst *CI = dyn_cast<ICmpInst>(*UI)) { |
| 143 | SplitCondition = CI; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Valid SplitCondition's one operand is phi node and the other operand |
| 149 | // is loop invariant. |
| 150 | if (SplitCondition) { |
| 151 | if (SplitCondition->getOperand(0) != PN) |
| 152 | SplitValue = SplitCondition->getOperand(0); |
| 153 | else |
| 154 | SplitValue = SplitCondition->getOperand(1); |
| 155 | SCEVHandle ValueSCEV = SE->getSCEV(SplitValue); |
| 156 | |
| 157 | // If SplitValue is not invariant then SplitCondition is not appropriate. |
| 158 | if (!ValueSCEV->isLoopInvariant(L)) |
| 159 | SplitCondition = NULL; |
| 160 | } |
| 161 | |
| 162 | // We are looking for only one split condition. |
| 163 | if (SplitCondition) { |
| 164 | IndVar = PN; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// processOneIterationLoop - Current loop L contains compare instruction |
| 171 | /// that compares induction variable, IndVar, against loop invariant. If |
| 172 | /// entire (i.e. meaningful) loop body is dominated by this compare |
| 173 | /// instruction then loop body is executed only once. In such case eliminate |
| 174 | /// loop structure surrounding this loop body. For example, |
| 175 | /// for (int i = start; i < end; ++i) { |
| 176 | /// if ( i == somevalue) { |
| 177 | /// loop_body |
| 178 | /// } |
| 179 | /// } |
| 180 | /// can be transformed into |
| 181 | /// if (somevalue >= start && somevalue < end) { |
| 182 | /// i = somevalue; |
| 183 | /// loop_body |
| 184 | /// } |
| 185 | bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) { |
| 186 | |
| 187 | BasicBlock *Header = L->getHeader(); |
| 188 | |
| 189 | // First of all, check if SplitCondition dominates entire loop body |
| 190 | // or not. |
| 191 | |
| 192 | // If SplitCondition is not in loop header then this loop is not suitable |
| 193 | // for this transformation. |
| 194 | if (SplitCondition->getParent() != Header) |
| 195 | return false; |
| 196 | |
| 197 | // If one of the Header block's successor is not an exit block then this |
| 198 | // loop is not a suitable candidate. |
| 199 | BasicBlock *ExitBlock = NULL; |
| 200 | for (succ_iterator SI = succ_begin(Header), E = succ_end(Header); SI != E; ++SI) { |
| 201 | if (L->isLoopExit(*SI)) { |
| 202 | ExitBlock = *SI; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (!ExitBlock) |
| 208 | return false; |
| 209 | |
| 210 | // If loop header includes loop variant instruction operands then |
| 211 | // this loop may not be eliminated. |
| 212 | if (!safeHeader(Header)) |
| 213 | return false; |
| 214 | |
| 215 | // If Exit block includes loop variant instructions then this |
| 216 | // loop may not be eliminated. |
| 217 | if (!safeExitBlock(ExitBlock)) |
| 218 | return false; |
| 219 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 220 | // Update CFG. |
| 221 | |
| 222 | // 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] | 223 | BasicBlock *Latch = L->getLoopLatch(); |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 224 | BasicBlock *LatchSucc = NULL; |
| 225 | BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator()); |
| 226 | if (!BR) |
| 227 | return false; |
| 228 | Header->removePredecessor(Latch); |
| 229 | for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); |
| 230 | SI != E; ++SI) { |
| 231 | if (Header != *SI) |
| 232 | LatchSucc = *SI; |
| 233 | } |
| 234 | BR->setUnconditionalDest(LatchSucc); |
| 235 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 236 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 237 | Instruction *Terminator = Header->getTerminator(); |
| 238 | Value *StartValue = IndVar->getIncomingValueForBlock(Preheader); |
| 239 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 240 | // Replace split condition in header. |
| 241 | // Transform |
| 242 | // SplitCondition : icmp eq i32 IndVar, SplitValue |
| 243 | // into |
| 244 | // c1 = icmp uge i32 SplitValue, StartValue |
| 245 | // c2 = icmp ult i32 vSplitValue, ExitValue |
| 246 | // and i32 c1, c2 |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 247 | bool SignedPredicate = ExitCondition->isSignedPredicate(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 248 | Instruction *C1 = new ICmpInst(SignedPredicate ? |
| 249 | ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, |
| 250 | SplitValue, StartValue, "lisplit", Terminator); |
| 251 | Instruction *C2 = new ICmpInst(SignedPredicate ? |
| 252 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 253 | SplitValue, ExitValue, "lisplit", Terminator); |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 254 | Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit", Terminator); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 255 | SplitCondition->replaceAllUsesWith(NSplitCond); |
Devang Patel | 0d75c29 | 2007-08-07 17:45:35 +0000 | [diff] [blame] | 256 | SplitCondition->eraseFromParent(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 257 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 258 | // Now, clear latch block. Remove instructions that are responsible |
| 259 | // to increment induction variable. |
| 260 | Instruction *LTerminator = Latch->getTerminator(); |
| 261 | for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end(); |
| 262 | LB != LE; ) { |
| 263 | Instruction *I = LB; |
| 264 | ++LB; |
| 265 | if (isa<PHINode>(I) || I == LTerminator) |
| 266 | continue; |
| 267 | |
| 268 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
Devang Patel | 0d75c29 | 2007-08-07 17:45:35 +0000 | [diff] [blame] | 269 | I->eraseFromParent(); |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | LPM.deleteLoopFromQueue(L); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | // If loop header includes loop variant instruction operands then |
| 277 | // this loop can not be eliminated. This is used by processOneIterationLoop(). |
| 278 | bool LoopIndexSplit::safeHeader(BasicBlock *Header) { |
| 279 | |
| 280 | Instruction *Terminator = Header->getTerminator(); |
| 281 | for(BasicBlock::iterator BI = Header->begin(), BE = Header->end(); |
| 282 | BI != BE; ++BI) { |
| 283 | Instruction *I = BI; |
| 284 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 285 | // PHI Nodes are OK. FIXME : Handle last value assignments. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 286 | if (isa<PHINode>(I)) |
| 287 | continue; |
| 288 | |
| 289 | // SplitCondition itself is OK. |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 290 | if (I == SplitCondition) |
| 291 | continue; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 292 | |
| 293 | // Terminator is also harmless. |
| 294 | if (I == Terminator) |
| 295 | continue; |
| 296 | |
| 297 | // Otherwise we have a instruction that may not be safe. |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | // If Exit block includes loop variant instructions then this |
| 305 | // loop may not be eliminated. This is used by processOneIterationLoop(). |
| 306 | bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) { |
| 307 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 308 | Instruction *IndVarIncrement = NULL; |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 309 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 310 | for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end(); |
| 311 | BI != BE; ++BI) { |
| 312 | Instruction *I = BI; |
| 313 | |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 314 | // PHI Nodes are OK. FIXME : Handle last value assignments. |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 315 | if (isa<PHINode>(I)) |
| 316 | continue; |
| 317 | |
| 318 | // Check if I is induction variable increment instruction. |
| 319 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(I)) { |
| 320 | if (BOp->getOpcode() != Instruction::Add) |
| 321 | return false; |
| 322 | |
| 323 | Value *Op0 = BOp->getOperand(0); |
| 324 | Value *Op1 = BOp->getOperand(1); |
| 325 | PHINode *PN = NULL; |
| 326 | ConstantInt *CI = NULL; |
| 327 | |
| 328 | if ((PN = dyn_cast<PHINode>(Op0))) { |
| 329 | if ((CI = dyn_cast<ConstantInt>(Op1))) |
| 330 | IndVarIncrement = I; |
| 331 | } else |
| 332 | if ((PN = dyn_cast<PHINode>(Op1))) { |
| 333 | if ((CI = dyn_cast<ConstantInt>(Op0))) |
| 334 | IndVarIncrement = I; |
| 335 | } |
| 336 | |
| 337 | if (IndVarIncrement && PN == IndVar && CI->isOne()) |
| 338 | continue; |
| 339 | } |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 340 | |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 341 | // I is an Exit condition if next instruction is block terminator. |
| 342 | // Exit condition is OK if it compares loop invariant exit value, |
| 343 | // which is checked below. |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 344 | else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) { |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 345 | ++BI; |
| 346 | Instruction *N = BI; |
| 347 | if (N == ExitBlock->getTerminator()) { |
Devang Patel | 3719d4f | 2007-08-07 23:17:52 +0000 | [diff] [blame] | 348 | ExitCondition = EC; |
Devang Patel | 2bcb501 | 2007-08-08 01:51:27 +0000 | [diff] [blame^] | 349 | continue; |
Devang Patel | bc5fe63 | 2007-08-07 00:25:56 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | // Otherwise we have instruction that may not be safe. |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | // Check if Exit condition is comparing induction variable against |
| 358 | // loop invariant value. If one operand is induction variable and |
| 359 | // the other operand is loop invaraint then Exit condition is safe. |
| 360 | if (ExitCondition) { |
| 361 | Value *Op0 = ExitCondition->getOperand(0); |
| 362 | Value *Op1 = ExitCondition->getOperand(1); |
| 363 | |
| 364 | Instruction *Insn0 = dyn_cast<Instruction>(Op0); |
| 365 | Instruction *Insn1 = dyn_cast<Instruction>(Op1); |
| 366 | |
| 367 | if (Insn0 && Insn0 == IndVarIncrement) |
| 368 | ExitValue = Op1; |
| 369 | else if (Insn1 && Insn1 == IndVarIncrement) |
| 370 | ExitValue = Op0; |
| 371 | |
| 372 | SCEVHandle ValueSCEV = SE->getSCEV(ExitValue); |
| 373 | if (!ValueSCEV->isLoopInvariant(L)) |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | // We could not find any reason to consider ExitBlock unsafe. |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool LoopIndexSplit::splitLoop() { |
| 382 | // FIXME :) |
| 383 | return false; |
| 384 | } |