1 //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This transformation analyzes and transforms the induction variables (and 11 // computations derived from them) into simpler forms suitable for subsequent 12 // analysis and transformation. 13 // 14 // This transformation makes the following changes to each loop with an 15 // identifiable induction variable: 16 // 1. All loops are transformed to have a SINGLE canonical induction variable 17 // which starts at zero and steps by one. 18 // 2. The canonical induction variable is guaranteed to be the first PHI node 19 // in the loop header block. 20 // 3. The canonical induction variable is guaranteed to be in a wide enough 21 // type so that IV expressions need not be (directly) zero-extended or 22 // sign-extended. 23 // 4. Any pointer arithmetic recurrences are raised to use array subscripts. 24 // 25 // If the trip count of a loop is computable, this pass also makes the following 26 // changes: 27 // 1. The exit condition for the loop is canonicalized to compare the 28 // induction value against the exit value. This turns loops like: 29 // 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)' 30 // 2. Any use outside of the loop of an expression derived from the indvar 31 // is changed to compute the derived value outside of the loop, eliminating 32 // the dependence on the exit value of the induction variable. If the only 33 // purpose of the loop is to compute the exit value of some derived 34 // expression, this transformation will make the loop dead. 35 // 36 // This transformation should be followed by strength reduction after all of the 37 // desired loop transformations have been performed. 38 // 39 //===----------------------------------------------------------------------===// 40 41 #define DEBUG_TYPE "indvars" 42 #include "llvm/Transforms/Scalar.h" 43 #include "llvm/BasicBlock.h" 44 #include "llvm/Constants.h" 45 #include "llvm/Instructions.h" 46 #include "llvm/LLVMContext.h" 47 #include "llvm/Type.h" 48 #include "llvm/Analysis/Dominators.h" 49 #include "llvm/Analysis/IVUsers.h" 50 #include "llvm/Analysis/ScalarEvolutionExpander.h" 51 #include "llvm/Analysis/LoopInfo.h" 52 #include "llvm/Analysis/LoopPass.h" 53 #include "llvm/Support/CFG.h" 54 #include "llvm/Support/CommandLine.h" 55 #include "llvm/Support/Debug.h" 56 #include "llvm/Support/raw_ostream.h" 57 #include "llvm/Transforms/Utils/Local.h" 58 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 59 #include "llvm/ADT/SmallVector.h" 60 #include "llvm/ADT/Statistic.h" 61 #include "llvm/ADT/STLExtras.h" 62 using namespace llvm; 63 64 STATISTIC(NumRemoved , "Number of aux indvars removed"); 65 STATISTIC(NumInserted, "Number of canonical indvars added"); 66 STATISTIC(NumReplaced, "Number of exit values replaced"); 67 STATISTIC(NumLFTR , "Number of loop exit tests replaced"); 68 69 namespace { 70 class IndVarSimplify : public LoopPass { 71 IVUsers *IU; 72 LoopInfo *LI; 73 ScalarEvolution *SE; 74 DominatorTree *DT; 75 bool Changed; 76 public: 77 78 static char ID; // Pass identification, replacement for typeid 79 IndVarSimplify() : LoopPass(&ID) {} 80 81 virtual bool runOnLoop(Loop *L, LPPassManager &LPM); 82 83 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 84 AU.addRequired<DominatorTree>(); 85 AU.addRequired<LoopInfo>(); 86 AU.addRequired<ScalarEvolution>(); 87 AU.addRequiredID(LoopSimplifyID); 88 AU.addRequiredID(LCSSAID); 89 AU.addRequired<IVUsers>(); 90 AU.addPreserved<ScalarEvolution>(); 91 AU.addPreservedID(LoopSimplifyID); 92 AU.addPreservedID(LCSSAID); 93 AU.addPreserved<IVUsers>(); 94 AU.setPreservesCFG(); 95 } 96 97 private: 98 99 void RewriteNonIntegerIVs(Loop *L); 100 101 ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, 102 Value *IndVar, 103 BasicBlock *ExitingBlock, 104 BranchInst *BI, 105 SCEVExpander &Rewriter); 106 void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount, 107 SCEVExpander &Rewriter); 108 109 void RewriteIVExpressions(Loop *L, const Type *LargestType, 110 SCEVExpander &Rewriter); 111 112 void SinkUnusedInvariants(Loop *L); 113 114 void HandleFloatingPointIV(Loop *L, PHINode *PH); 115 }; 116 } 117 118 char IndVarSimplify::ID = 0; 119 static RegisterPass<IndVarSimplify> 120 X("indvars", "Canonicalize Induction Variables"); 121 122 Pass *llvm::createIndVarSimplifyPass() { 123 return new IndVarSimplify(); 124 } 125 126 /// LinearFunctionTestReplace - This method rewrites the exit condition of the 127 /// loop to be a canonical != comparison against the incremented loop induction 128 /// variable. This pass is able to rewrite the exit tests of any loop where the 129 /// SCEV analysis can determine a loop-invariant trip count of the loop, which 130 /// is actually a much broader range than just linear tests. 131 ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L, 132 const SCEV *BackedgeTakenCount, 133 Value *IndVar, 134 BasicBlock *ExitingBlock, 135 BranchInst *BI, 136 SCEVExpander &Rewriter) { 137 // If the exiting block is not the same as the backedge block, we must compare 138 // against the preincremented value, otherwise we prefer to compare against 139 // the post-incremented value. 140 Value *CmpIndVar; 141 const SCEV *RHS = BackedgeTakenCount; 142 if (ExitingBlock == L->getLoopLatch()) { 143 // Add one to the "backedge-taken" count to get the trip count. 144 // If this addition may overflow, we have to be more pessimistic and 145 // cast the induction variable before doing the add. 146 const SCEV *Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType()); 147 const SCEV *N = 148 SE->getAddExpr(BackedgeTakenCount, 149 SE->getIntegerSCEV(1, BackedgeTakenCount->getType())); 150 if ((isa<SCEVConstant>(N) && !N->isZero()) || 151 SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { 152 // No overflow. Cast the sum. 153 RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType()); 154 } else { 155 // Potential overflow. Cast before doing the add. 156 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, 157 IndVar->getType()); 158 RHS = SE->getAddExpr(RHS, 159 SE->getIntegerSCEV(1, IndVar->getType())); 160 } 161 162 // The BackedgeTaken expression contains the number of times that the 163 // backedge branches to the loop header. This is one less than the 164 // number of times the loop executes, so use the incremented indvar. 165 CmpIndVar = L->getCanonicalInductionVariableIncrement(); 166 } else { 167 // We have to use the preincremented value... 168 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, 169 IndVar->getType()); 170 CmpIndVar = IndVar; 171 } 172 173 // Expand the code for the iteration count. 174 assert(RHS->isLoopInvariant(L) && 175 "Computed iteration count is not loop invariant!"); 176 Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI); 177 178 // Insert a new icmp_ne or icmp_eq instruction before the branch. 179 ICmpInst::Predicate Opcode; 180 if (L->contains(BI->getSuccessor(0))) 181 Opcode = ICmpInst::ICMP_NE; 182 else 183 Opcode = ICmpInst::ICMP_EQ; 184 185 DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n" 186 << " LHS:" << *CmpIndVar << '\n' 187 << " op:\t" 188 << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n" 189 << " RHS:\t" << *RHS << "\n"); 190 191 ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond"); 192 193 Instruction *OrigCond = cast<Instruction>(BI->getCondition()); 194 // It's tempting to use replaceAllUsesWith here to fully replace the old 195 // comparison, but that's not immediately safe, since users of the old 196 // comparison may not be dominated by the new comparison. Instead, just 197 // update the branch to use the new comparison; in the common case this 198 // will make old comparison dead. 199 BI->setCondition(Cond); 200 RecursivelyDeleteTriviallyDeadInstructions(OrigCond); 201 202 ++NumLFTR; 203 Changed = true; 204 return Cond; 205 } 206 207 /// RewriteLoopExitValues - Check to see if this loop has a computable 208 /// loop-invariant execution count. If so, this means that we can compute the 209 /// final value of any expressions that are recurrent in the loop, and 210 /// substitute the exit values from the loop into any instructions outside of 211 /// the loop that use the final values of the current expressions. 212 /// 213 /// This is mostly redundant with the regular IndVarSimplify activities that 214 /// happen later, except that it's more powerful in some cases, because it's 215 /// able to brute-force evaluate arbitrary instructions as long as they have 216 /// constant operands at the beginning of the loop. 217 void IndVarSimplify::RewriteLoopExitValues(Loop *L, 218 const SCEV *BackedgeTakenCount, 219 SCEVExpander &Rewriter) { 220 // Verify the input to the pass in already in LCSSA form. 221 assert(L->isLCSSAForm()); 222 223 SmallVector<BasicBlock*, 8> ExitBlocks; 224 L->getUniqueExitBlocks(ExitBlocks); 225 226 // Find all values that are computed inside the loop, but used outside of it. 227 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan 228 // the exit blocks of the loop to find them. 229 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { 230 BasicBlock *ExitBB = ExitBlocks[i]; 231 232 // If there are no PHI nodes in this exit block, then no values defined 233 // inside the loop are used on this path, skip it. 234 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); 235 if (!PN) continue; 236 237 unsigned NumPreds = PN->getNumIncomingValues(); 238 239 // Iterate over all of the PHI nodes. 240 BasicBlock::iterator BBI = ExitBB->begin(); 241 while ((PN = dyn_cast<PHINode>(BBI++))) { 242 if (PN->use_empty()) 243 continue; // dead use, don't replace it 244 // Iterate over all of the values in all the PHI nodes. 245 for (unsigned i = 0; i != NumPreds; ++i) { 246 // If the value being merged in is not integer or is not defined 247 // in the loop, skip it. 248 Value *InVal = PN->getIncomingValue(i); 249 if (!isa<Instruction>(InVal) || 250 // SCEV only supports integer expressions for now. 251 (!isa<IntegerType>(InVal->getType()) && 252 !isa<PointerType>(InVal->getType()))) 253 continue; 254 255 // If this pred is for a subloop, not L itself, skip it. 256 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) 257 continue; // The Block is in a subloop, skip it. 258 259 // Check that InVal is defined in the loop. 260 Instruction *Inst = cast<Instruction>(InVal); 261 if (!L->contains(Inst)) 262 continue; 263 264 // Okay, this instruction has a user outside of the current loop 265 // and varies predictably *inside* the loop. Evaluate the value it 266 // contains when the loop exits, if possible. 267 const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); 268 if (!ExitValue->isLoopInvariant(L)) 269 continue; 270 271 Changed = true; 272 ++NumReplaced; 273 274 Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst); 275 276 DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n' 277 << " LoopVal = " << *Inst << "\n"); 278 279 PN->setIncomingValue(i, ExitVal); 280 281 // If this instruction is dead now, delete it. 282 RecursivelyDeleteTriviallyDeadInstructions(Inst); 283 284 if (NumPreds == 1) { 285 // Completely replace a single-pred PHI. This is safe, because the 286 // NewVal won't be variant in the loop, so we don't need an LCSSA phi 287 // node anymore. 288 PN->replaceAllUsesWith(ExitVal); 289 RecursivelyDeleteTriviallyDeadInstructions(PN); 290 } 291 } 292 if (NumPreds != 1) { 293 // Clone the PHI and delete the original one. This lets IVUsers and 294 // any other maps purge the original user from their records. 295 PHINode *NewPN = cast<PHINode>(PN->clone()); 296 NewPN->takeName(PN); 297 NewPN->insertBefore(PN); 298 PN->replaceAllUsesWith(NewPN); 299 PN->eraseFromParent(); 300 } 301 } 302 } 303 } 304 305 void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { 306 // First step. Check to see if there are any floating-point recurrences. 307 // If there are, change them into integer recurrences, permitting analysis by 308 // the SCEV routines. 309 // 310 BasicBlock *Header = L->getHeader(); 311 312 SmallVector<WeakVH, 8> PHIs; 313 for (BasicBlock::iterator I = Header->begin(); 314 PHINode *PN = dyn_cast<PHINode>(I); ++I) 315 PHIs.push_back(PN); 316 317 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) 318 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i])) 319 HandleFloatingPointIV(L, PN); 320 321 // If the loop previously had floating-point IV, ScalarEvolution 322 // may not have been able to compute a trip count. Now that we've done some 323 // re-writing, the trip count may be computable. 324 if (Changed) 325 SE->forgetLoop(L); 326 } 327 328 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { 329 IU = &getAnalysis<IVUsers>(); 330 LI = &getAnalysis<LoopInfo>(); 331 SE = &getAnalysis<ScalarEvolution>(); 332 DT = &getAnalysis<DominatorTree>(); 333 Changed = false; 334 335 // If there are any floating-point recurrences, attempt to 336 // transform them to use integer recurrences. 337 RewriteNonIntegerIVs(L); 338 339 BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null 340 const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L); 341 342 // Create a rewriter object which we'll use to transform the code with. 343 SCEVExpander Rewriter(*SE); 344 345 // Check to see if this loop has a computable loop-invariant execution count. 346 // If so, this means that we can compute the final value of any expressions 347 // that are recurrent in the loop, and substitute the exit values from the 348 // loop into any instructions outside of the loop that use the final values of 349 // the current expressions. 350 // 351 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) 352 RewriteLoopExitValues(L, BackedgeTakenCount, Rewriter); 353 354 // Compute the type of the largest recurrence expression, and decide whether 355 // a canonical induction variable should be inserted. 356 const Type *LargestType = 0; 357 bool NeedCannIV = false; 358 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { 359 LargestType = BackedgeTakenCount->getType(); 360 LargestType = SE->getEffectiveSCEVType(LargestType); 361 // If we have a known trip count and a single exit block, we'll be 362 // rewriting the loop exit test condition below, which requires a 363 // canonical induction variable. 364 if (ExitingBlock) 365 NeedCannIV = true; 366 } 367 for (IVUsers::const_iterator I = IU->begin(), E = IU->end(); I != E; ++I) { 368 const Type *Ty = 369 SE->getEffectiveSCEVType(I->getOperandValToReplace()->getType()); 370 if (!LargestType || 371 SE->getTypeSizeInBits(Ty) > 372 SE->getTypeSizeInBits(LargestType)) 373 LargestType = Ty; 374 NeedCannIV = true; 375 } 376 377 // Now that we know the largest of the induction variable expressions 378 // in this loop, insert a canonical induction variable of the largest size. 379 Value *IndVar = 0; 380 if (NeedCannIV) { 381 // Check to see if the loop already has a canonical-looking induction 382 // variable. If one is present and it's wider than the planned canonical 383 // induction variable, temporarily remove it, so that the Rewriter 384 // doesn't attempt to reuse it. 385 PHINode *OldCannIV = L->getCanonicalInductionVariable(); 386 if (OldCannIV) { 387 if (SE->getTypeSizeInBits(OldCannIV->getType()) > 388 SE->getTypeSizeInBits(LargestType)) 389 OldCannIV->removeFromParent(); 390 else 391 OldCannIV = 0; 392 } 393 394 IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType); 395 396 ++NumInserted; 397 Changed = true; 398 DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n'); 399 400 // Now that the official induction variable is established, reinsert 401 // the old canonical-looking variable after it so that the IR remains 402 // consistent. It will be deleted as part of the dead-PHI deletion at 403 // the end of the pass. 404 if (OldCannIV) 405 OldCannIV->insertAfter(cast<Instruction>(IndVar)); 406 } 407 408 // If we have a trip count expression, rewrite the loop's exit condition 409 // using it. We can currently only handle loops with a single exit. 410 ICmpInst *NewICmp = 0; 411 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) { 412 assert(NeedCannIV && 413 "LinearFunctionTestReplace requires a canonical induction variable"); 414 // Can't rewrite non-branch yet. 415 if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) 416 NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, 417 ExitingBlock, BI, Rewriter); 418 } 419 420 // Rewrite IV-derived expressions. Clears the rewriter cache. 421 RewriteIVExpressions(L, LargestType, Rewriter); 422 423 // The Rewriter may not be used from this point on. 424 425 // Loop-invariant instructions in the preheader that aren't used in the 426 // loop may be sunk below the loop to reduce register pressure. 427 SinkUnusedInvariants(L); 428 429 // For completeness, inform IVUsers of the IV use in the newly-created 430 // loop exit test instruction. 431 if (NewICmp) 432 IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0))); 433 434 // Clean up dead instructions. 435 Changed |= DeleteDeadPHIs(L->getHeader()); 436 // Check a post-condition. 437 assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!"); 438 return Changed; 439 } 440 441 void IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType, 442 SCEVExpander &Rewriter) { 443 SmallVector<WeakVH, 16> DeadInsts; 444 445 // Rewrite all induction variable expressions in terms of the canonical 446 // induction variable. 447 // 448 // If there were induction variables of other sizes or offsets, manually 449 // add the offsets to the primary induction variable and cast, avoiding 450 // the need for the code evaluation methods to insert induction variables 451 // of different sizes. 452 for (IVUsers::iterator UI = IU->begin(), E = IU->end(); UI != E; ++UI) { 453 const SCEV *Stride = UI->getStride(); 454 Value *Op = UI->getOperandValToReplace(); 455 const Type *UseTy = Op->getType(); 456 Instruction *User = UI->getUser(); 457 458 // Compute the final addrec to expand into code. 459 const SCEV *AR = IU->getReplacementExpr(*UI); 460 461 // Evaluate the expression out of the loop, if possible. 462 if (!L->contains(UI->getUser())) { 463 const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop()); 464 if (ExitVal->isLoopInvariant(L)) 465 AR = ExitVal; 466 } 467 468 // FIXME: It is an extremely bad idea to indvar substitute anything more 469 // complex than affine induction variables. Doing so will put expensive 470 // polynomial evaluations inside of the loop, and the str reduction pass 471 // currently can only reduce affine polynomials. For now just disable 472 // indvar subst on anything more complex than an affine addrec, unless 473 // it can be expanded to a trivial value. 474 if (!AR->isLoopInvariant(L) && !Stride->isLoopInvariant(L)) 475 continue; 476 477 // Determine the insertion point for this user. By default, insert 478 // immediately before the user. The SCEVExpander class will automatically 479 // hoist loop invariants out of the loop. For PHI nodes, there may be 480 // multiple uses, so compute the nearest common dominator for the 481 // incoming blocks. 482 Instruction *InsertPt = User; 483 if (PHINode *PHI = dyn_cast<PHINode>(InsertPt)) 484 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) 485 if (PHI->getIncomingValue(i) == Op) { 486 if (InsertPt == User) 487 InsertPt = PHI->getIncomingBlock(i)->getTerminator(); 488 else 489 InsertPt = 490 DT->findNearestCommonDominator(InsertPt->getParent(), 491 PHI->getIncomingBlock(i)) 492 ->getTerminator(); 493 } 494 495 // Now expand it into actual Instructions and patch it into place. 496 Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt); 497 498 // Patch the new value into place. 499 if (Op->hasName()) 500 NewVal->takeName(Op); 501 User->replaceUsesOfWith(Op, NewVal); 502 UI->setOperandValToReplace(NewVal); 503 DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n' 504 << " into = " << *NewVal << "\n"); 505 ++NumRemoved; 506 Changed = true; 507 508 // The old value may be dead now. 509 DeadInsts.push_back(Op); 510 } 511 512 // Clear the rewriter cache, because values that are in the rewriter's cache 513 // can be deleted in the loop below, causing the AssertingVH in the cache to 514 // trigger. 515 Rewriter.clear(); 516 // Now that we're done iterating through lists, clean up any instructions 517 // which are now dead. 518 while (!DeadInsts.empty()) 519 if (Instruction *Inst = 520 dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val())) 521 RecursivelyDeleteTriviallyDeadInstructions(Inst); 522 } 523 524 /// If there's a single exit block, sink any loop-invariant values that 525 /// were defined in the preheader but not used inside the loop into the 526 /// exit block to reduce register pressure in the loop. 527 void IndVarSimplify::SinkUnusedInvariants(Loop *L) { 528 BasicBlock *ExitBlock = L->getExitBlock(); 529 if (!ExitBlock) return; 530 531 BasicBlock *Preheader = L->getLoopPreheader(); 532 if (!Preheader) return; 533 534 Instruction *InsertPt = ExitBlock->getFirstNonPHI(); 535 BasicBlock::iterator I = Preheader->getTerminator(); 536 while (I != Preheader->begin()) { 537 --I; 538 // New instructions were inserted at the end of the preheader. 539 if (isa<PHINode>(I)) 540 break; 541 // Don't move instructions which might have side effects, since the side 542 // effects need to complete before instructions inside the loop. Also 543 // don't move instructions which might read memory, since the loop may 544 // modify memory. Note that it's okay if the instruction might have 545 // undefined behavior: LoopSimplify guarantees that the preheader 546 // dominates the exit block. 547 if (I->mayHaveSideEffects() || I->mayReadFromMemory()) 548 continue; 549 // Don't sink static AllocaInsts out of the entry block, which would 550 // turn them into dynamic allocas! 551 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) 552 if (AI->isStaticAlloca()) 553 continue; 554 // Determine if there is a use in or before the loop (direct or 555 // otherwise). 556 bool UsedInLoop = false; 557 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); 558 UI != UE; ++UI) { 559 BasicBlock *UseBB = cast<Instruction>(UI)->getParent(); 560 if (PHINode *P = dyn_cast<PHINode>(UI)) { 561 unsigned i = 562 PHINode::getIncomingValueNumForOperand(UI.getOperandNo()); 563 UseBB = P->getIncomingBlock(i); 564 } 565 if (UseBB == Preheader || L->contains(UseBB)) { 566 UsedInLoop = true; 567 break; 568 } 569 } 570 // If there is, the def must remain in the preheader. 571 if (UsedInLoop) 572 continue; 573 // Otherwise, sink it to the exit block. 574 Instruction *ToMove = I; 575 bool Done = false; 576 if (I != Preheader->begin()) 577 --I; 578 else 579 Done = true; 580 ToMove->moveBefore(InsertPt); 581 if (Done) 582 break; 583 InsertPt = ToMove; 584 } 585 } 586 587 /// Return true if it is OK to use SIToFPInst for an inducation variable 588 /// with given inital and exit values. 589 static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV, 590 uint64_t intIV, uint64_t intEV) { 591 592 if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative()) 593 return true; 594 595 // If the iteration range can be handled by SIToFPInst then use it. 596 APInt Max = APInt::getSignedMaxValue(32); 597 if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV))) 598 return true; 599 600 return false; 601 } 602 603 /// convertToInt - Convert APF to an integer, if possible. 604 static bool convertToInt(const APFloat &APF, uint64_t *intVal) { 605 606 bool isExact = false; 607 if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) 608 return false; 609 if (APF.convertToInteger(intVal, 32, APF.isNegative(), 610 APFloat::rmTowardZero, &isExact) 611 != APFloat::opOK) 612 return false; 613 if (!isExact) 614 return false; 615 return true; 616 617 } 618 619 /// HandleFloatingPointIV - If the loop has floating induction variable 620 /// then insert corresponding integer induction variable if possible. 621 /// For example, 622 /// for(double i = 0; i < 10000; ++i) 623 /// bar(i) 624 /// is converted into 625 /// for(int i = 0; i < 10000; ++i) 626 /// bar((double)i); 627 /// 628 void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { 629 630 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); 631 unsigned BackEdge = IncomingEdge^1; 632 633 // Check incoming value. 634 ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge)); 635 if (!InitValue) return; 636 uint64_t newInitValue = 637 Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); 638 if (!convertToInt(InitValue->getValueAPF(), &newInitValue)) 639 return; 640 641 // Check IV increment. Reject this PH if increement operation is not 642 // an add or increment value can not be represented by an integer. 643 BinaryOperator *Incr = 644 dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge)); 645 if (!Incr) return; 646 if (Incr->getOpcode() != Instruction::FAdd) return; 647 ConstantFP *IncrValue = NULL; 648 unsigned IncrVIndex = 1; 649 if (Incr->getOperand(1) == PH) 650 IncrVIndex = 0; 651 IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex)); 652 if (!IncrValue) return; 653 uint64_t newIncrValue = 654 Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); 655 if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue)) 656 return; 657 658 // Check Incr uses. One user is PH and the other users is exit condition used 659 // by the conditional terminator. 660 Value::use_iterator IncrUse = Incr->use_begin(); 661 Instruction *U1 = cast<Instruction>(IncrUse++); 662 if (IncrUse == Incr->use_end()) return; 663 Instruction *U2 = cast<Instruction>(IncrUse++); 664 if (IncrUse != Incr->use_end()) return; 665 666 // Find exit condition. 667 FCmpInst *EC = dyn_cast<FCmpInst>(U1); 668 if (!EC) 669 EC = dyn_cast<FCmpInst>(U2); 670 if (!EC) return; 671 672 if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) { 673 if (!BI->isConditional()) return; 674 if (BI->getCondition() != EC) return; 675 } 676 677 // Find exit value. If exit value can not be represented as an interger then 678 // do not handle this floating point PH. 679 ConstantFP *EV = NULL; 680 unsigned EVIndex = 1; 681 if (EC->getOperand(1) == Incr) 682 EVIndex = 0; 683 EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex)); 684 if (!EV) return; 685 uint64_t intEV = Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); 686 if (!convertToInt(EV->getValueAPF(), &intEV)) 687 return; 688 689 // Find new predicate for integer comparison. 690 CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; 691 switch (EC->getPredicate()) { 692 case CmpInst::FCMP_OEQ: 693 case CmpInst::FCMP_UEQ: 694 NewPred = CmpInst::ICMP_EQ; 695 break; 696 case CmpInst::FCMP_OGT: 697 case CmpInst::FCMP_UGT: 698 NewPred = CmpInst::ICMP_UGT; 699 break; 700 case CmpInst::FCMP_OGE: 701 case CmpInst::FCMP_UGE: 702 NewPred = CmpInst::ICMP_UGE; 703 break; 704 case CmpInst::FCMP_OLT: 705 case CmpInst::FCMP_ULT: 706 NewPred = CmpInst::ICMP_ULT; 707 break; 708 case CmpInst::FCMP_OLE: 709 case CmpInst::FCMP_ULE: 710 NewPred = CmpInst::ICMP_ULE; 711 break; 712 default: 713 break; 714 } 715 if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; 716 717 // Insert new integer induction variable. 718 PHINode *NewPHI = PHINode::Create(Type::getInt32Ty(PH->getContext()), 719 PH->getName()+".int", PH); 720 NewPHI->addIncoming(ConstantInt::get(Type::getInt32Ty(PH->getContext()), 721 newInitValue), 722 PH->getIncomingBlock(IncomingEdge)); 723 724 Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, 725 ConstantInt::get(Type::getInt32Ty(PH->getContext()), 726 newIncrValue), 727 Incr->getName()+".int", Incr); 728 NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); 729 730 // The back edge is edge 1 of newPHI, whatever it may have been in the 731 // original PHI. 732 ConstantInt *NewEV = ConstantInt::get(Type::getInt32Ty(PH->getContext()), 733 intEV); 734 Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV); 735 Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1)); 736 ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(), 737 NewPred, LHS, RHS, EC->getName()); 738 739 // In the following deltions, PH may become dead and may be deleted. 740 // Use a WeakVH to observe whether this happens. 741 WeakVH WeakPH = PH; 742 743 // Delete old, floating point, exit comparision instruction. 744 NewEC->takeName(EC); 745 EC->replaceAllUsesWith(NewEC); 746 RecursivelyDeleteTriviallyDeadInstructions(EC); 747 748 // Delete old, floating point, increment instruction. 749 Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); 750 RecursivelyDeleteTriviallyDeadInstructions(Incr); 751 752 // Replace floating induction variable, if it isn't already deleted. 753 // Give SIToFPInst preference over UIToFPInst because it is faster on 754 // platforms that are widely used. 755 if (WeakPH && !PH->use_empty()) { 756 if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) { 757 SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv", 758 PH->getParent()->getFirstNonPHI()); 759 PH->replaceAllUsesWith(Conv); 760 } else { 761 UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv", 762 PH->getParent()->getFirstNonPHI()); 763 PH->replaceAllUsesWith(Conv); 764 } 765 RecursivelyDeleteTriviallyDeadInstructions(PH); 766 } 767 768 // Add a new IVUsers entry for the newly-created integer PHI. 769 IU->AddUsersIfInteresting(NewPHI); 770 } 771