1 //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===// 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 file implements induction variable simplification. It does 11 // not define any actual pass or policy, but provides a single function to 12 // simplify a loop's induction variables based on ScalarEvolution. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Transforms/Utils/SimplifyIndVar.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/IVUsers.h" 21 #include "llvm/Analysis/LoopInfo.h" 22 #include "llvm/Analysis/LoopPass.h" 23 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/Dominators.h" 26 #include "llvm/IR/IRBuilder.h" 27 #include "llvm/IR/Instructions.h" 28 #include "llvm/IR/IntrinsicInst.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/raw_ostream.h" 32 33 using namespace llvm; 34 35 #define DEBUG_TYPE "indvars" 36 37 STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); 38 STATISTIC(NumElimOperand, "Number of IV operands folded into a use"); 39 STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); 40 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); 41 42 namespace { 43 /// This is a utility for simplifying induction variables 44 /// based on ScalarEvolution. It is the primary instrument of the 45 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after 46 /// other loop passes that preserve SCEV. 47 class SimplifyIndvar { 48 Loop *L; 49 LoopInfo *LI; 50 ScalarEvolution *SE; 51 52 SmallVectorImpl<WeakVH> &DeadInsts; 53 54 bool Changed; 55 56 public: 57 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, LoopInfo *LI, 58 SmallVectorImpl<WeakVH> &Dead, IVUsers *IVU = nullptr) 59 : L(Loop), LI(LI), SE(SE), DeadInsts(Dead), Changed(false) { 60 assert(LI && "IV simplification requires LoopInfo"); 61 } 62 63 bool hasChanged() const { return Changed; } 64 65 /// Iteratively perform simplification on a worklist of users of the 66 /// specified induction variable. This is the top-level driver that applies 67 /// all simplicitions to users of an IV. 68 void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr); 69 70 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); 71 72 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); 73 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); 74 void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand, 75 bool IsSigned); 76 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand); 77 78 Instruction *splitOverflowIntrinsic(Instruction *IVUser, 79 const DominatorTree *DT); 80 }; 81 } 82 83 /// Fold an IV operand into its use. This removes increments of an 84 /// aligned IV when used by a instruction that ignores the low bits. 85 /// 86 /// IVOperand is guaranteed SCEVable, but UseInst may not be. 87 /// 88 /// Return the operand of IVOperand for this induction variable if IVOperand can 89 /// be folded (in case more folding opportunities have been exposed). 90 /// Otherwise return null. 91 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) { 92 Value *IVSrc = nullptr; 93 unsigned OperIdx = 0; 94 const SCEV *FoldedExpr = nullptr; 95 switch (UseInst->getOpcode()) { 96 default: 97 return nullptr; 98 case Instruction::UDiv: 99 case Instruction::LShr: 100 // We're only interested in the case where we know something about 101 // the numerator and have a constant denominator. 102 if (IVOperand != UseInst->getOperand(OperIdx) || 103 !isa<ConstantInt>(UseInst->getOperand(1))) 104 return nullptr; 105 106 // Attempt to fold a binary operator with constant operand. 107 // e.g. ((I + 1) >> 2) => I >> 2 108 if (!isa<BinaryOperator>(IVOperand) 109 || !isa<ConstantInt>(IVOperand->getOperand(1))) 110 return nullptr; 111 112 IVSrc = IVOperand->getOperand(0); 113 // IVSrc must be the (SCEVable) IV, since the other operand is const. 114 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand"); 115 116 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1)); 117 if (UseInst->getOpcode() == Instruction::LShr) { 118 // Get a constant for the divisor. See createSCEV. 119 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth(); 120 if (D->getValue().uge(BitWidth)) 121 return nullptr; 122 123 D = ConstantInt::get(UseInst->getContext(), 124 APInt::getOneBitSet(BitWidth, D->getZExtValue())); 125 } 126 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D)); 127 } 128 // We have something that might fold it's operand. Compare SCEVs. 129 if (!SE->isSCEVable(UseInst->getType())) 130 return nullptr; 131 132 // Bypass the operand if SCEV can prove it has no effect. 133 if (SE->getSCEV(UseInst) != FoldedExpr) 134 return nullptr; 135 136 DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand 137 << " -> " << *UseInst << '\n'); 138 139 UseInst->setOperand(OperIdx, IVSrc); 140 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper"); 141 142 ++NumElimOperand; 143 Changed = true; 144 if (IVOperand->use_empty()) 145 DeadInsts.push_back(IVOperand); 146 return IVSrc; 147 } 148 149 /// SimplifyIVUsers helper for eliminating useless 150 /// comparisons against an induction variable. 151 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { 152 unsigned IVOperIdx = 0; 153 ICmpInst::Predicate Pred = ICmp->getPredicate(); 154 if (IVOperand != ICmp->getOperand(0)) { 155 // Swapped 156 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); 157 IVOperIdx = 1; 158 Pred = ICmpInst::getSwappedPredicate(Pred); 159 } 160 161 // Get the SCEVs for the ICmp operands. 162 const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx)); 163 const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx)); 164 165 // Simplify unnecessary loops away. 166 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); 167 S = SE->getSCEVAtScope(S, ICmpLoop); 168 X = SE->getSCEVAtScope(X, ICmpLoop); 169 170 // If the condition is always true or always false, replace it with 171 // a constant value. 172 if (SE->isKnownPredicate(Pred, S, X)) 173 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); 174 else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) 175 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); 176 else 177 return; 178 179 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); 180 ++NumElimCmp; 181 Changed = true; 182 DeadInsts.push_back(ICmp); 183 } 184 185 /// SimplifyIVUsers helper for eliminating useless 186 /// remainder operations operating on an induction variable. 187 void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem, 188 Value *IVOperand, 189 bool IsSigned) { 190 // We're only interested in the case where we know something about 191 // the numerator. 192 if (IVOperand != Rem->getOperand(0)) 193 return; 194 195 // Get the SCEVs for the ICmp operands. 196 const SCEV *S = SE->getSCEV(Rem->getOperand(0)); 197 const SCEV *X = SE->getSCEV(Rem->getOperand(1)); 198 199 // Simplify unnecessary loops away. 200 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); 201 S = SE->getSCEVAtScope(S, ICmpLoop); 202 X = SE->getSCEVAtScope(X, ICmpLoop); 203 204 // i % n --> i if i is in [0,n). 205 if ((!IsSigned || SE->isKnownNonNegative(S)) && 206 SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 207 S, X)) 208 Rem->replaceAllUsesWith(Rem->getOperand(0)); 209 else { 210 // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). 211 const SCEV *LessOne = 212 SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1)); 213 if (IsSigned && !SE->isKnownNonNegative(LessOne)) 214 return; 215 216 if (!SE->isKnownPredicate(IsSigned ? 217 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 218 LessOne, X)) 219 return; 220 221 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, 222 Rem->getOperand(0), Rem->getOperand(1)); 223 SelectInst *Sel = 224 SelectInst::Create(ICmp, 225 ConstantInt::get(Rem->getType(), 0), 226 Rem->getOperand(0), "tmp", Rem); 227 Rem->replaceAllUsesWith(Sel); 228 } 229 230 DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); 231 ++NumElimRem; 232 Changed = true; 233 DeadInsts.push_back(Rem); 234 } 235 236 /// Eliminate an operation that consumes a simple IV and has 237 /// no observable side-effect given the range of IV values. 238 /// IVOperand is guaranteed SCEVable, but UseInst may not be. 239 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, 240 Instruction *IVOperand) { 241 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { 242 eliminateIVComparison(ICmp, IVOperand); 243 return true; 244 } 245 if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) { 246 bool IsSigned = Rem->getOpcode() == Instruction::SRem; 247 if (IsSigned || Rem->getOpcode() == Instruction::URem) { 248 eliminateIVRemainder(Rem, IVOperand, IsSigned); 249 return true; 250 } 251 } 252 253 // Eliminate any operation that SCEV can prove is an identity function. 254 if (!SE->isSCEVable(UseInst->getType()) || 255 (UseInst->getType() != IVOperand->getType()) || 256 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) 257 return false; 258 259 DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); 260 261 UseInst->replaceAllUsesWith(IVOperand); 262 ++NumElimIdentity; 263 Changed = true; 264 DeadInsts.push_back(UseInst); 265 return true; 266 } 267 268 /// Annotate BO with nsw / nuw if it provably does not signed-overflow / 269 /// unsigned-overflow. Returns true if anything changed, false otherwise. 270 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, 271 Value *IVOperand) { 272 273 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`. 274 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap()) 275 return false; 276 277 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *, 278 SCEV::NoWrapFlags); 279 280 switch (BO->getOpcode()) { 281 default: 282 return false; 283 284 case Instruction::Add: 285 GetExprForBO = &ScalarEvolution::getAddExpr; 286 break; 287 288 case Instruction::Sub: 289 GetExprForBO = &ScalarEvolution::getMinusSCEV; 290 break; 291 292 case Instruction::Mul: 293 GetExprForBO = &ScalarEvolution::getMulExpr; 294 break; 295 } 296 297 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth(); 298 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2); 299 const SCEV *LHS = SE->getSCEV(BO->getOperand(0)); 300 const SCEV *RHS = SE->getSCEV(BO->getOperand(1)); 301 302 bool Changed = false; 303 304 if (!BO->hasNoUnsignedWrap()) { 305 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy); 306 const SCEV *OpAfterExtend = (SE->*GetExprForBO)( 307 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy), 308 SCEV::FlagAnyWrap); 309 if (ExtendAfterOp == OpAfterExtend) { 310 BO->setHasNoUnsignedWrap(); 311 SE->forgetValue(BO); 312 Changed = true; 313 } 314 } 315 316 if (!BO->hasNoSignedWrap()) { 317 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy); 318 const SCEV *OpAfterExtend = (SE->*GetExprForBO)( 319 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy), 320 SCEV::FlagAnyWrap); 321 if (ExtendAfterOp == OpAfterExtend) { 322 BO->setHasNoSignedWrap(); 323 SE->forgetValue(BO); 324 Changed = true; 325 } 326 } 327 328 return Changed; 329 } 330 331 /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow 332 /// analysis and optimization. 333 /// 334 /// \return A new value representing the non-overflowing add if possible, 335 /// otherwise return the original value. 336 Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser, 337 const DominatorTree *DT) { 338 IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser); 339 if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow) 340 return IVUser; 341 342 // Find a branch guarded by the overflow check. 343 BranchInst *Branch = nullptr; 344 Instruction *AddVal = nullptr; 345 for (User *U : II->users()) { 346 if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) { 347 if (ExtractInst->getNumIndices() != 1) 348 continue; 349 if (ExtractInst->getIndices()[0] == 0) 350 AddVal = ExtractInst; 351 else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse()) 352 Branch = dyn_cast<BranchInst>(ExtractInst->user_back()); 353 } 354 } 355 if (!AddVal || !Branch) 356 return IVUser; 357 358 BasicBlock *ContinueBB = Branch->getSuccessor(1); 359 if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB)) 360 return IVUser; 361 362 // Check if all users of the add are provably NSW. 363 bool AllNSW = true; 364 for (Use &U : AddVal->uses()) { 365 if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) { 366 BasicBlock *UseBB = UseInst->getParent(); 367 if (PHINode *PHI = dyn_cast<PHINode>(UseInst)) 368 UseBB = PHI->getIncomingBlock(U); 369 if (!DT->dominates(ContinueBB, UseBB)) { 370 AllNSW = false; 371 break; 372 } 373 } 374 } 375 if (!AllNSW) 376 return IVUser; 377 378 // Go for it... 379 IRBuilder<> Builder(IVUser); 380 Instruction *AddInst = dyn_cast<Instruction>( 381 Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1))); 382 383 // The caller expects the new add to have the same form as the intrinsic. The 384 // IV operand position must be the same. 385 assert((AddInst->getOpcode() == Instruction::Add && 386 AddInst->getOperand(0) == II->getOperand(0)) && 387 "Bad add instruction created from overflow intrinsic."); 388 389 AddVal->replaceAllUsesWith(AddInst); 390 DeadInsts.push_back(AddVal); 391 return AddInst; 392 } 393 394 /// Add all uses of Def to the current IV's worklist. 395 static void pushIVUsers( 396 Instruction *Def, 397 SmallPtrSet<Instruction*,16> &Simplified, 398 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) { 399 400 for (User *U : Def->users()) { 401 Instruction *UI = cast<Instruction>(U); 402 403 // Avoid infinite or exponential worklist processing. 404 // Also ensure unique worklist users. 405 // If Def is a LoopPhi, it may not be in the Simplified set, so check for 406 // self edges first. 407 if (UI != Def && Simplified.insert(UI).second) 408 SimpleIVUsers.push_back(std::make_pair(UI, Def)); 409 } 410 } 411 412 /// Return true if this instruction generates a simple SCEV 413 /// expression in terms of that IV. 414 /// 415 /// This is similar to IVUsers' isInteresting() but processes each instruction 416 /// non-recursively when the operand is already known to be a simpleIVUser. 417 /// 418 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) { 419 if (!SE->isSCEVable(I->getType())) 420 return false; 421 422 // Get the symbolic expression for this instruction. 423 const SCEV *S = SE->getSCEV(I); 424 425 // Only consider affine recurrences. 426 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); 427 if (AR && AR->getLoop() == L) 428 return true; 429 430 return false; 431 } 432 433 /// Iteratively perform simplification on a worklist of users 434 /// of the specified induction variable. Each successive simplification may push 435 /// more users which may themselves be candidates for simplification. 436 /// 437 /// This algorithm does not require IVUsers analysis. Instead, it simplifies 438 /// instructions in-place during analysis. Rather than rewriting induction 439 /// variables bottom-up from their users, it transforms a chain of IVUsers 440 /// top-down, updating the IR only when it encouters a clear optimization 441 /// opportunitiy. 442 /// 443 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. 444 /// 445 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { 446 if (!SE->isSCEVable(CurrIV->getType())) 447 return; 448 449 // Instructions processed by SimplifyIndvar for CurrIV. 450 SmallPtrSet<Instruction*,16> Simplified; 451 452 // Use-def pairs if IV users waiting to be processed for CurrIV. 453 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; 454 455 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be 456 // called multiple times for the same LoopPhi. This is the proper thing to 457 // do for loop header phis that use each other. 458 pushIVUsers(CurrIV, Simplified, SimpleIVUsers); 459 460 while (!SimpleIVUsers.empty()) { 461 std::pair<Instruction*, Instruction*> UseOper = 462 SimpleIVUsers.pop_back_val(); 463 Instruction *UseInst = UseOper.first; 464 465 // Bypass back edges to avoid extra work. 466 if (UseInst == CurrIV) continue; 467 468 if (V && V->shouldSplitOverflowInstrinsics()) { 469 UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree()); 470 if (!UseInst) 471 continue; 472 } 473 474 Instruction *IVOperand = UseOper.second; 475 for (unsigned N = 0; IVOperand; ++N) { 476 assert(N <= Simplified.size() && "runaway iteration"); 477 478 Value *NewOper = foldIVUser(UseOper.first, IVOperand); 479 if (!NewOper) 480 break; // done folding 481 IVOperand = dyn_cast<Instruction>(NewOper); 482 } 483 if (!IVOperand) 484 continue; 485 486 if (eliminateIVUser(UseOper.first, IVOperand)) { 487 pushIVUsers(IVOperand, Simplified, SimpleIVUsers); 488 continue; 489 } 490 491 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) { 492 if (isa<OverflowingBinaryOperator>(BO) && 493 strengthenOverflowingOperation(BO, IVOperand)) { 494 // re-queue uses of the now modified binary operator and fall 495 // through to the checks that remain. 496 pushIVUsers(IVOperand, Simplified, SimpleIVUsers); 497 } 498 } 499 500 CastInst *Cast = dyn_cast<CastInst>(UseOper.first); 501 if (V && Cast) { 502 V->visitCast(Cast); 503 continue; 504 } 505 if (isSimpleIVUser(UseOper.first, L, SE)) { 506 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers); 507 } 508 } 509 } 510 511 namespace llvm { 512 513 void IVVisitor::anchor() { } 514 515 /// Simplify instructions that use this induction variable 516 /// by using ScalarEvolution to analyze the IV's recurrence. 517 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM, 518 SmallVectorImpl<WeakVH> &Dead, IVVisitor *V) 519 { 520 LoopInfo *LI = &LPM->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 521 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, LI, Dead); 522 SIV.simplifyUsers(CurrIV, V); 523 return SIV.hasChanged(); 524 } 525 526 /// Simplify users of induction variables within this 527 /// loop. This does not actually change or add IVs. 528 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM, 529 SmallVectorImpl<WeakVH> &Dead) { 530 bool Changed = false; 531 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { 532 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, LPM, Dead); 533 } 534 return Changed; 535 } 536 537 } // namespace llvm 538