1 //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// 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 pass transforms loops that contain branches on loop-invariant conditions 11 // to have multiple loops. For example, it turns the left into the right code: 12 // 13 // for (...) if (lic) 14 // A for (...) 15 // if (lic) A; B; C 16 // B else 17 // C for (...) 18 // A; C 19 // 20 // This can increase the size of the code exponentially (doubling it every time 21 // a loop is unswitched) so we only unswitch if the resultant code will be 22 // smaller than a threshold. 23 // 24 // This pass expects LICM to be run before it to hoist invariant conditions out 25 // of the loop, to make the unswitching opportunity obvious. 26 // 27 //===----------------------------------------------------------------------===// 28 29 #include "llvm/Transforms/Scalar.h" 30 #include "llvm/ADT/STLExtras.h" 31 #include "llvm/ADT/SmallPtrSet.h" 32 #include "llvm/ADT/Statistic.h" 33 #include "llvm/Analysis/GlobalsModRef.h" 34 #include "llvm/Analysis/AssumptionCache.h" 35 #include "llvm/Analysis/CodeMetrics.h" 36 #include "llvm/Analysis/InstructionSimplify.h" 37 #include "llvm/Analysis/LoopInfo.h" 38 #include "llvm/Analysis/LoopPass.h" 39 #include "llvm/Analysis/ScalarEvolution.h" 40 #include "llvm/Analysis/TargetTransformInfo.h" 41 #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 42 #include "llvm/Analysis/BlockFrequencyInfo.h" 43 #include "llvm/Analysis/BranchProbabilityInfo.h" 44 #include "llvm/Support/BranchProbability.h" 45 #include "llvm/IR/Constants.h" 46 #include "llvm/IR/DerivedTypes.h" 47 #include "llvm/IR/Dominators.h" 48 #include "llvm/IR/Function.h" 49 #include "llvm/IR/Instructions.h" 50 #include "llvm/IR/Module.h" 51 #include "llvm/IR/MDBuilder.h" 52 #include "llvm/Support/CommandLine.h" 53 #include "llvm/Support/Debug.h" 54 #include "llvm/Support/raw_ostream.h" 55 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 56 #include "llvm/Transforms/Utils/Cloning.h" 57 #include "llvm/Transforms/Utils/Local.h" 58 #include "llvm/Transforms/Utils/LoopUtils.h" 59 #include <algorithm> 60 #include <map> 61 #include <set> 62 using namespace llvm; 63 64 #define DEBUG_TYPE "loop-unswitch" 65 66 STATISTIC(NumBranches, "Number of branches unswitched"); 67 STATISTIC(NumSwitches, "Number of switches unswitched"); 68 STATISTIC(NumGuards, "Number of guards unswitched"); 69 STATISTIC(NumSelects , "Number of selects unswitched"); 70 STATISTIC(NumTrivial , "Number of unswitches that are trivial"); 71 STATISTIC(NumSimplify, "Number of simplifications of unswitched code"); 72 STATISTIC(TotalInsts, "Total number of instructions analyzed"); 73 74 // The specific value of 100 here was chosen based only on intuition and a 75 // few specific examples. 76 static cl::opt<unsigned> 77 Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), 78 cl::init(100), cl::Hidden); 79 80 static cl::opt<bool> 81 LoopUnswitchWithBlockFrequency("loop-unswitch-with-block-frequency", 82 cl::init(false), cl::Hidden, 83 cl::desc("Enable the use of the block frequency analysis to access PGO " 84 "heuristics to minimize code growth in cold regions.")); 85 86 static cl::opt<unsigned> 87 ColdnessThreshold("loop-unswitch-coldness-threshold", cl::init(1), cl::Hidden, 88 cl::desc("Coldness threshold in percentage. The loop header frequency " 89 "(relative to the entry frequency) is compared with this " 90 "threshold to determine if non-trivial unswitching should be " 91 "enabled.")); 92 93 namespace { 94 95 class LUAnalysisCache { 96 97 typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> > 98 UnswitchedValsMap; 99 100 typedef UnswitchedValsMap::iterator UnswitchedValsIt; 101 102 struct LoopProperties { 103 unsigned CanBeUnswitchedCount; 104 unsigned WasUnswitchedCount; 105 unsigned SizeEstimation; 106 UnswitchedValsMap UnswitchedVals; 107 }; 108 109 // Here we use std::map instead of DenseMap, since we need to keep valid 110 // LoopProperties pointer for current loop for better performance. 111 typedef std::map<const Loop*, LoopProperties> LoopPropsMap; 112 typedef LoopPropsMap::iterator LoopPropsMapIt; 113 114 LoopPropsMap LoopsProperties; 115 UnswitchedValsMap *CurLoopInstructions; 116 LoopProperties *CurrentLoopProperties; 117 118 // A loop unswitching with an estimated cost above this threshold 119 // is not performed. MaxSize is turned into unswitching quota for 120 // the current loop, and reduced correspondingly, though note that 121 // the quota is returned by releaseMemory() when the loop has been 122 // processed, so that MaxSize will return to its previous 123 // value. So in most cases MaxSize will equal the Threshold flag 124 // when a new loop is processed. An exception to that is that 125 // MaxSize will have a smaller value while processing nested loops 126 // that were introduced due to loop unswitching of an outer loop. 127 // 128 // FIXME: The way that MaxSize works is subtle and depends on the 129 // pass manager processing loops and calling releaseMemory() in a 130 // specific order. It would be good to find a more straightforward 131 // way of doing what MaxSize does. 132 unsigned MaxSize; 133 134 public: 135 LUAnalysisCache() 136 : CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr), 137 MaxSize(Threshold) {} 138 139 // Analyze loop. Check its size, calculate is it possible to unswitch 140 // it. Returns true if we can unswitch this loop. 141 bool countLoop(const Loop *L, const TargetTransformInfo &TTI, 142 AssumptionCache *AC); 143 144 // Clean all data related to given loop. 145 void forgetLoop(const Loop *L); 146 147 // Mark case value as unswitched. 148 // Since SI instruction can be partly unswitched, in order to avoid 149 // extra unswitching in cloned loops keep track all unswitched values. 150 void setUnswitched(const SwitchInst *SI, const Value *V); 151 152 // Check was this case value unswitched before or not. 153 bool isUnswitched(const SwitchInst *SI, const Value *V); 154 155 // Returns true if another unswitching could be done within the cost 156 // threshold. 157 bool CostAllowsUnswitching(); 158 159 // Clone all loop-unswitch related loop properties. 160 // Redistribute unswitching quotas. 161 // Note, that new loop data is stored inside the VMap. 162 void cloneData(const Loop *NewLoop, const Loop *OldLoop, 163 const ValueToValueMapTy &VMap); 164 }; 165 166 class LoopUnswitch : public LoopPass { 167 LoopInfo *LI; // Loop information 168 LPPassManager *LPM; 169 AssumptionCache *AC; 170 171 // Used to check if second loop needs processing after 172 // RewriteLoopBodyWithConditionConstant rewrites first loop. 173 std::vector<Loop*> LoopProcessWorklist; 174 175 LUAnalysisCache BranchesInfo; 176 177 bool EnabledPGO; 178 179 // BFI and ColdEntryFreq are only used when PGO and 180 // LoopUnswitchWithBlockFrequency are enabled. 181 BlockFrequencyInfo BFI; 182 BlockFrequency ColdEntryFreq; 183 184 bool OptimizeForSize; 185 bool redoLoop; 186 187 Loop *currentLoop; 188 DominatorTree *DT; 189 BasicBlock *loopHeader; 190 BasicBlock *loopPreheader; 191 192 bool SanitizeMemory; 193 LoopSafetyInfo SafetyInfo; 194 195 // LoopBlocks contains all of the basic blocks of the loop, including the 196 // preheader of the loop, the body of the loop, and the exit blocks of the 197 // loop, in that order. 198 std::vector<BasicBlock*> LoopBlocks; 199 // NewBlocks contained cloned copy of basic blocks from LoopBlocks. 200 std::vector<BasicBlock*> NewBlocks; 201 202 public: 203 static char ID; // Pass ID, replacement for typeid 204 explicit LoopUnswitch(bool Os = false) : 205 LoopPass(ID), OptimizeForSize(Os), redoLoop(false), 206 currentLoop(nullptr), DT(nullptr), loopHeader(nullptr), 207 loopPreheader(nullptr) { 208 initializeLoopUnswitchPass(*PassRegistry::getPassRegistry()); 209 } 210 211 bool runOnLoop(Loop *L, LPPassManager &LPM) override; 212 bool processCurrentLoop(); 213 bool isUnreachableDueToPreviousUnswitching(BasicBlock *); 214 /// This transformation requires natural loop information & requires that 215 /// loop preheaders be inserted into the CFG. 216 /// 217 void getAnalysisUsage(AnalysisUsage &AU) const override { 218 AU.addRequired<AssumptionCacheTracker>(); 219 AU.addRequired<TargetTransformInfoWrapperPass>(); 220 getLoopAnalysisUsage(AU); 221 } 222 223 private: 224 225 void releaseMemory() override { 226 BranchesInfo.forgetLoop(currentLoop); 227 } 228 229 void initLoopData() { 230 loopHeader = currentLoop->getHeader(); 231 loopPreheader = currentLoop->getLoopPreheader(); 232 } 233 234 /// Split all of the edges from inside the loop to their exit blocks. 235 /// Update the appropriate Phi nodes as we do so. 236 void SplitExitEdges(Loop *L, 237 const SmallVectorImpl<BasicBlock *> &ExitBlocks); 238 239 bool TryTrivialLoopUnswitch(bool &Changed); 240 241 bool UnswitchIfProfitable(Value *LoopCond, Constant *Val, 242 TerminatorInst *TI = nullptr); 243 void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, 244 BasicBlock *ExitBlock, TerminatorInst *TI); 245 void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L, 246 TerminatorInst *TI); 247 248 void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, 249 Constant *Val, bool isEqual); 250 251 void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, 252 BasicBlock *TrueDest, 253 BasicBlock *FalseDest, 254 Instruction *InsertPt, 255 TerminatorInst *TI); 256 257 void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L); 258 }; 259 } 260 261 // Analyze loop. Check its size, calculate is it possible to unswitch 262 // it. Returns true if we can unswitch this loop. 263 bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI, 264 AssumptionCache *AC) { 265 266 LoopPropsMapIt PropsIt; 267 bool Inserted; 268 std::tie(PropsIt, Inserted) = 269 LoopsProperties.insert(std::make_pair(L, LoopProperties())); 270 271 LoopProperties &Props = PropsIt->second; 272 273 if (Inserted) { 274 // New loop. 275 276 // Limit the number of instructions to avoid causing significant code 277 // expansion, and the number of basic blocks, to avoid loops with 278 // large numbers of branches which cause loop unswitching to go crazy. 279 // This is a very ad-hoc heuristic. 280 281 SmallPtrSet<const Value *, 32> EphValues; 282 CodeMetrics::collectEphemeralValues(L, AC, EphValues); 283 284 // FIXME: This is overly conservative because it does not take into 285 // consideration code simplification opportunities and code that can 286 // be shared by the resultant unswitched loops. 287 CodeMetrics Metrics; 288 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E; 289 ++I) 290 Metrics.analyzeBasicBlock(*I, TTI, EphValues); 291 292 Props.SizeEstimation = Metrics.NumInsts; 293 Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation); 294 Props.WasUnswitchedCount = 0; 295 MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount; 296 297 if (Metrics.notDuplicatable) { 298 DEBUG(dbgs() << "NOT unswitching loop %" 299 << L->getHeader()->getName() << ", contents cannot be " 300 << "duplicated!\n"); 301 return false; 302 } 303 } 304 305 // Be careful. This links are good only before new loop addition. 306 CurrentLoopProperties = &Props; 307 CurLoopInstructions = &Props.UnswitchedVals; 308 309 return true; 310 } 311 312 // Clean all data related to given loop. 313 void LUAnalysisCache::forgetLoop(const Loop *L) { 314 315 LoopPropsMapIt LIt = LoopsProperties.find(L); 316 317 if (LIt != LoopsProperties.end()) { 318 LoopProperties &Props = LIt->second; 319 MaxSize += (Props.CanBeUnswitchedCount + Props.WasUnswitchedCount) * 320 Props.SizeEstimation; 321 LoopsProperties.erase(LIt); 322 } 323 324 CurrentLoopProperties = nullptr; 325 CurLoopInstructions = nullptr; 326 } 327 328 // Mark case value as unswitched. 329 // Since SI instruction can be partly unswitched, in order to avoid 330 // extra unswitching in cloned loops keep track all unswitched values. 331 void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) { 332 (*CurLoopInstructions)[SI].insert(V); 333 } 334 335 // Check was this case value unswitched before or not. 336 bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) { 337 return (*CurLoopInstructions)[SI].count(V); 338 } 339 340 bool LUAnalysisCache::CostAllowsUnswitching() { 341 return CurrentLoopProperties->CanBeUnswitchedCount > 0; 342 } 343 344 // Clone all loop-unswitch related loop properties. 345 // Redistribute unswitching quotas. 346 // Note, that new loop data is stored inside the VMap. 347 void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop, 348 const ValueToValueMapTy &VMap) { 349 350 LoopProperties &NewLoopProps = LoopsProperties[NewLoop]; 351 LoopProperties &OldLoopProps = *CurrentLoopProperties; 352 UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals; 353 354 // Reallocate "can-be-unswitched quota" 355 356 --OldLoopProps.CanBeUnswitchedCount; 357 ++OldLoopProps.WasUnswitchedCount; 358 NewLoopProps.WasUnswitchedCount = 0; 359 unsigned Quota = OldLoopProps.CanBeUnswitchedCount; 360 NewLoopProps.CanBeUnswitchedCount = Quota / 2; 361 OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2; 362 363 NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation; 364 365 // Clone unswitched values info: 366 // for new loop switches we clone info about values that was 367 // already unswitched and has redundant successors. 368 for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) { 369 const SwitchInst *OldInst = I->first; 370 Value *NewI = VMap.lookup(OldInst); 371 const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI); 372 assert(NewInst && "All instructions that are in SrcBB must be in VMap."); 373 374 NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst]; 375 } 376 } 377 378 char LoopUnswitch::ID = 0; 379 INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops", 380 false, false) 381 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 382 INITIALIZE_PASS_DEPENDENCY(LoopPass) 383 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 384 INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops", 385 false, false) 386 387 Pass *llvm::createLoopUnswitchPass(bool Os) { 388 return new LoopUnswitch(Os); 389 } 390 391 /// Cond is a condition that occurs in L. If it is invariant in the loop, or has 392 /// an invariant piece, return the invariant. Otherwise, return null. 393 static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed, 394 DenseMap<Value *, Value *> &Cache) { 395 auto CacheIt = Cache.find(Cond); 396 if (CacheIt != Cache.end()) 397 return CacheIt->second; 398 399 // We started analyze new instruction, increment scanned instructions counter. 400 ++TotalInsts; 401 402 // We can never unswitch on vector conditions. 403 if (Cond->getType()->isVectorTy()) 404 return nullptr; 405 406 // Constants should be folded, not unswitched on! 407 if (isa<Constant>(Cond)) return nullptr; 408 409 // TODO: Handle: br (VARIANT|INVARIANT). 410 411 // Hoist simple values out. 412 if (L->makeLoopInvariant(Cond, Changed)) { 413 Cache[Cond] = Cond; 414 return Cond; 415 } 416 417 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond)) 418 if (BO->getOpcode() == Instruction::And || 419 BO->getOpcode() == Instruction::Or) { 420 // If either the left or right side is invariant, we can unswitch on this, 421 // which will cause the branch to go away in one loop and the condition to 422 // simplify in the other one. 423 if (Value *LHS = 424 FindLIVLoopCondition(BO->getOperand(0), L, Changed, Cache)) { 425 Cache[Cond] = LHS; 426 return LHS; 427 } 428 if (Value *RHS = 429 FindLIVLoopCondition(BO->getOperand(1), L, Changed, Cache)) { 430 Cache[Cond] = RHS; 431 return RHS; 432 } 433 } 434 435 Cache[Cond] = nullptr; 436 return nullptr; 437 } 438 439 static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) { 440 DenseMap<Value *, Value *> Cache; 441 return FindLIVLoopCondition(Cond, L, Changed, Cache); 442 } 443 444 bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) { 445 if (skipLoop(L)) 446 return false; 447 448 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache( 449 *L->getHeader()->getParent()); 450 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 451 LPM = &LPM_Ref; 452 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 453 currentLoop = L; 454 Function *F = currentLoop->getHeader()->getParent(); 455 456 SanitizeMemory = F->hasFnAttribute(Attribute::SanitizeMemory); 457 if (SanitizeMemory) 458 computeLoopSafetyInfo(&SafetyInfo, L); 459 460 EnabledPGO = F->getEntryCount().hasValue(); 461 462 if (LoopUnswitchWithBlockFrequency && EnabledPGO) { 463 BranchProbabilityInfo BPI(*F, *LI); 464 BFI.calculate(*L->getHeader()->getParent(), BPI, *LI); 465 466 // Use BranchProbability to compute a minimum frequency based on 467 // function entry baseline frequency. Loops with headers below this 468 // frequency are considered as cold. 469 const BranchProbability ColdProb(ColdnessThreshold, 100); 470 ColdEntryFreq = BlockFrequency(BFI.getEntryFreq()) * ColdProb; 471 } 472 473 bool Changed = false; 474 do { 475 assert(currentLoop->isLCSSAForm(*DT)); 476 redoLoop = false; 477 Changed |= processCurrentLoop(); 478 } while(redoLoop); 479 480 // FIXME: Reconstruct dom info, because it is not preserved properly. 481 if (Changed) 482 DT->recalculate(*F); 483 return Changed; 484 } 485 486 // Return true if the BasicBlock BB is unreachable from the loop header. 487 // Return false, otherwise. 488 bool LoopUnswitch::isUnreachableDueToPreviousUnswitching(BasicBlock *BB) { 489 auto *Node = DT->getNode(BB)->getIDom(); 490 BasicBlock *DomBB = Node->getBlock(); 491 while (currentLoop->contains(DomBB)) { 492 BranchInst *BInst = dyn_cast<BranchInst>(DomBB->getTerminator()); 493 494 Node = DT->getNode(DomBB)->getIDom(); 495 DomBB = Node->getBlock(); 496 497 if (!BInst || !BInst->isConditional()) 498 continue; 499 500 Value *Cond = BInst->getCondition(); 501 if (!isa<ConstantInt>(Cond)) 502 continue; 503 504 BasicBlock *UnreachableSucc = 505 Cond == ConstantInt::getTrue(Cond->getContext()) 506 ? BInst->getSuccessor(1) 507 : BInst->getSuccessor(0); 508 509 if (DT->dominates(UnreachableSucc, BB)) 510 return true; 511 } 512 return false; 513 } 514 515 /// Do actual work and unswitch loop if possible and profitable. 516 bool LoopUnswitch::processCurrentLoop() { 517 bool Changed = false; 518 519 initLoopData(); 520 521 // If LoopSimplify was unable to form a preheader, don't do any unswitching. 522 if (!loopPreheader) 523 return false; 524 525 // Loops with indirectbr cannot be cloned. 526 if (!currentLoop->isSafeToClone()) 527 return false; 528 529 // Without dedicated exits, splitting the exit edge may fail. 530 if (!currentLoop->hasDedicatedExits()) 531 return false; 532 533 LLVMContext &Context = loopHeader->getContext(); 534 535 // Analyze loop cost, and stop unswitching if loop content can not be duplicated. 536 if (!BranchesInfo.countLoop( 537 currentLoop, getAnalysis<TargetTransformInfoWrapperPass>().getTTI( 538 *currentLoop->getHeader()->getParent()), 539 AC)) 540 return false; 541 542 // Try trivial unswitch first before loop over other basic blocks in the loop. 543 if (TryTrivialLoopUnswitch(Changed)) { 544 return true; 545 } 546 547 // Run through the instructions in the loop, keeping track of three things: 548 // 549 // - That we do not unswitch loops containing convergent operations, as we 550 // might be making them control dependent on the unswitch value when they 551 // were not before. 552 // FIXME: This could be refined to only bail if the convergent operation is 553 // not already control-dependent on the unswitch value. 554 // 555 // - That basic blocks in the loop contain invokes whose predecessor edges we 556 // cannot split. 557 // 558 // - The set of guard intrinsics encountered (these are non terminator 559 // instructions that are also profitable to be unswitched). 560 561 SmallVector<IntrinsicInst *, 4> Guards; 562 563 for (const auto BB : currentLoop->blocks()) { 564 for (auto &I : *BB) { 565 auto CS = CallSite(&I); 566 if (!CS) continue; 567 if (CS.hasFnAttr(Attribute::Convergent)) 568 return false; 569 if (auto *II = dyn_cast<InvokeInst>(&I)) 570 if (!II->getUnwindDest()->canSplitPredecessors()) 571 return false; 572 if (auto *II = dyn_cast<IntrinsicInst>(&I)) 573 if (II->getIntrinsicID() == Intrinsic::experimental_guard) 574 Guards.push_back(II); 575 } 576 } 577 578 // Do not do non-trivial unswitch while optimizing for size. 579 // FIXME: Use Function::optForSize(). 580 if (OptimizeForSize || 581 loopHeader->getParent()->hasFnAttribute(Attribute::OptimizeForSize)) 582 return false; 583 584 if (LoopUnswitchWithBlockFrequency && EnabledPGO) { 585 // Compute the weighted frequency of the hottest block in the 586 // loop (loopHeader in this case since inner loops should be 587 // processed before outer loop). If it is less than ColdFrequency, 588 // we should not unswitch. 589 BlockFrequency LoopEntryFreq = BFI.getBlockFreq(loopHeader); 590 if (LoopEntryFreq < ColdEntryFreq) 591 return false; 592 } 593 594 for (IntrinsicInst *Guard : Guards) { 595 Value *LoopCond = 596 FindLIVLoopCondition(Guard->getOperand(0), currentLoop, Changed); 597 if (LoopCond && 598 UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context))) { 599 // NB! Unswitching (if successful) could have erased some of the 600 // instructions in Guards leaving dangling pointers there. This is fine 601 // because we're returning now, and won't look at Guards again. 602 ++NumGuards; 603 return true; 604 } 605 } 606 607 // Loop over all of the basic blocks in the loop. If we find an interior 608 // block that is branching on a loop-invariant condition, we can unswitch this 609 // loop. 610 for (Loop::block_iterator I = currentLoop->block_begin(), 611 E = currentLoop->block_end(); I != E; ++I) { 612 TerminatorInst *TI = (*I)->getTerminator(); 613 614 // Unswitching on a potentially uninitialized predicate is not 615 // MSan-friendly. Limit this to the cases when the original predicate is 616 // guaranteed to execute, to avoid creating a use-of-uninitialized-value 617 // in the code that did not have one. 618 // This is a workaround for the discrepancy between LLVM IR and MSan 619 // semantics. See PR28054 for more details. 620 if (SanitizeMemory && 621 !isGuaranteedToExecute(*TI, DT, currentLoop, &SafetyInfo)) 622 continue; 623 624 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 625 // Some branches may be rendered unreachable because of previous 626 // unswitching. 627 // Unswitch only those branches that are reachable. 628 if (isUnreachableDueToPreviousUnswitching(*I)) 629 continue; 630 631 // If this isn't branching on an invariant condition, we can't unswitch 632 // it. 633 if (BI->isConditional()) { 634 // See if this, or some part of it, is loop invariant. If so, we can 635 // unswitch on it if we desire. 636 Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), 637 currentLoop, Changed); 638 if (LoopCond && 639 UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context), TI)) { 640 ++NumBranches; 641 return true; 642 } 643 } 644 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 645 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), 646 currentLoop, Changed); 647 unsigned NumCases = SI->getNumCases(); 648 if (LoopCond && NumCases) { 649 // Find a value to unswitch on: 650 // FIXME: this should chose the most expensive case! 651 // FIXME: scan for a case with a non-critical edge? 652 Constant *UnswitchVal = nullptr; 653 654 // Do not process same value again and again. 655 // At this point we have some cases already unswitched and 656 // some not yet unswitched. Let's find the first not yet unswitched one. 657 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 658 i != e; ++i) { 659 Constant *UnswitchValCandidate = i.getCaseValue(); 660 if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) { 661 UnswitchVal = UnswitchValCandidate; 662 break; 663 } 664 } 665 666 if (!UnswitchVal) 667 continue; 668 669 if (UnswitchIfProfitable(LoopCond, UnswitchVal)) { 670 ++NumSwitches; 671 return true; 672 } 673 } 674 } 675 676 // Scan the instructions to check for unswitchable values. 677 for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end(); 678 BBI != E; ++BBI) 679 if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) { 680 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), 681 currentLoop, Changed); 682 if (LoopCond && UnswitchIfProfitable(LoopCond, 683 ConstantInt::getTrue(Context))) { 684 ++NumSelects; 685 return true; 686 } 687 } 688 } 689 return Changed; 690 } 691 692 /// Check to see if all paths from BB exit the loop with no side effects 693 /// (including infinite loops). 694 /// 695 /// If true, we return true and set ExitBB to the block we 696 /// exit through. 697 /// 698 static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, 699 BasicBlock *&ExitBB, 700 std::set<BasicBlock*> &Visited) { 701 if (!Visited.insert(BB).second) { 702 // Already visited. Without more analysis, this could indicate an infinite 703 // loop. 704 return false; 705 } 706 if (!L->contains(BB)) { 707 // Otherwise, this is a loop exit, this is fine so long as this is the 708 // first exit. 709 if (ExitBB) return false; 710 ExitBB = BB; 711 return true; 712 } 713 714 // Otherwise, this is an unvisited intra-loop node. Check all successors. 715 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) { 716 // Check to see if the successor is a trivial loop exit. 717 if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited)) 718 return false; 719 } 720 721 // Okay, everything after this looks good, check to make sure that this block 722 // doesn't include any side effects. 723 for (Instruction &I : *BB) 724 if (I.mayHaveSideEffects()) 725 return false; 726 727 return true; 728 } 729 730 /// Return true if the specified block unconditionally leads to an exit from 731 /// the specified loop, and has no side-effects in the process. If so, return 732 /// the block that is exited to, otherwise return null. 733 static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) { 734 std::set<BasicBlock*> Visited; 735 Visited.insert(L->getHeader()); // Branches to header make infinite loops. 736 BasicBlock *ExitBB = nullptr; 737 if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited)) 738 return ExitBB; 739 return nullptr; 740 } 741 742 /// We have found that we can unswitch currentLoop when LoopCond == Val to 743 /// simplify the loop. If we decide that this is profitable, 744 /// unswitch the loop, reprocess the pieces, then return true. 745 bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val, 746 TerminatorInst *TI) { 747 // Check to see if it would be profitable to unswitch current loop. 748 if (!BranchesInfo.CostAllowsUnswitching()) { 749 DEBUG(dbgs() << "NOT unswitching loop %" 750 << currentLoop->getHeader()->getName() 751 << " at non-trivial condition '" << *Val 752 << "' == " << *LoopCond << "\n" 753 << ". Cost too high.\n"); 754 return false; 755 } 756 757 UnswitchNontrivialCondition(LoopCond, Val, currentLoop, TI); 758 return true; 759 } 760 761 /// Recursively clone the specified loop and all of its children, 762 /// mapping the blocks with the specified map. 763 static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, 764 LoopInfo *LI, LPPassManager *LPM) { 765 Loop &New = LPM->addLoop(PL); 766 767 // Add all of the blocks in L to the new loop. 768 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 769 I != E; ++I) 770 if (LI->getLoopFor(*I) == L) 771 New.addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI); 772 773 // Add all of the subloops to the new loop. 774 for (Loop *I : *L) 775 CloneLoop(I, &New, VM, LI, LPM); 776 777 return &New; 778 } 779 780 /// Emit a conditional branch on two values if LIC == Val, branch to TrueDst, 781 /// otherwise branch to FalseDest. Insert the code immediately before InsertPt. 782 void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, 783 BasicBlock *TrueDest, 784 BasicBlock *FalseDest, 785 Instruction *InsertPt, 786 TerminatorInst *TI) { 787 // Insert a conditional branch on LIC to the two preheaders. The original 788 // code is the true version and the new code is the false version. 789 Value *BranchVal = LIC; 790 bool Swapped = false; 791 if (!isa<ConstantInt>(Val) || 792 Val->getType() != Type::getInt1Ty(LIC->getContext())) 793 BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val); 794 else if (Val != ConstantInt::getTrue(Val->getContext())) { 795 // We want to enter the new loop when the condition is true. 796 std::swap(TrueDest, FalseDest); 797 Swapped = true; 798 } 799 800 // Insert the new branch. 801 BranchInst *BI = 802 IRBuilder<>(InsertPt).CreateCondBr(BranchVal, TrueDest, FalseDest, TI); 803 if (Swapped) 804 BI->swapProfMetadata(); 805 806 // If either edge is critical, split it. This helps preserve LoopSimplify 807 // form for enclosing loops. 808 auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA(); 809 SplitCriticalEdge(BI, 0, Options); 810 SplitCriticalEdge(BI, 1, Options); 811 } 812 813 /// Given a loop that has a trivial unswitchable condition in it (a cond branch 814 /// from its header block to its latch block, where the path through the loop 815 /// that doesn't execute its body has no side-effects), unswitch it. This 816 /// doesn't involve any code duplication, just moving the conditional branch 817 /// outside of the loop and updating loop info. 818 void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, 819 BasicBlock *ExitBlock, 820 TerminatorInst *TI) { 821 DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %" 822 << loopHeader->getName() << " [" << L->getBlocks().size() 823 << " blocks] in Function " 824 << L->getHeader()->getParent()->getName() << " on cond: " << *Val 825 << " == " << *Cond << "\n"); 826 827 // First step, split the preheader, so that we know that there is a safe place 828 // to insert the conditional branch. We will change loopPreheader to have a 829 // conditional branch on Cond. 830 BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, DT, LI); 831 832 // Now that we have a place to insert the conditional branch, create a place 833 // to branch to: this is the exit block out of the loop that we should 834 // short-circuit to. 835 836 // Split this block now, so that the loop maintains its exit block, and so 837 // that the jump from the preheader can execute the contents of the exit block 838 // without actually branching to it (the exit block should be dominated by the 839 // loop header, not the preheader). 840 assert(!L->contains(ExitBlock) && "Exit block is in the loop?"); 841 BasicBlock *NewExit = SplitBlock(ExitBlock, &ExitBlock->front(), DT, LI); 842 843 // Okay, now we have a position to branch from and a position to branch to, 844 // insert the new conditional branch. 845 EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH, 846 loopPreheader->getTerminator(), TI); 847 LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L); 848 loopPreheader->getTerminator()->eraseFromParent(); 849 850 // We need to reprocess this loop, it could be unswitched again. 851 redoLoop = true; 852 853 // Now that we know that the loop is never entered when this condition is a 854 // particular value, rewrite the loop with this info. We know that this will 855 // at least eliminate the old branch. 856 RewriteLoopBodyWithConditionConstant(L, Cond, Val, false); 857 ++NumTrivial; 858 } 859 860 /// Check if the first non-constant condition starting from the loop header is 861 /// a trivial unswitch condition: that is, a condition controls whether or not 862 /// the loop does anything at all. If it is a trivial condition, unswitching 863 /// produces no code duplications (equivalently, it produces a simpler loop and 864 /// a new empty loop, which gets deleted). Therefore always unswitch trivial 865 /// condition. 866 bool LoopUnswitch::TryTrivialLoopUnswitch(bool &Changed) { 867 BasicBlock *CurrentBB = currentLoop->getHeader(); 868 TerminatorInst *CurrentTerm = CurrentBB->getTerminator(); 869 LLVMContext &Context = CurrentBB->getContext(); 870 871 // If loop header has only one reachable successor (currently via an 872 // unconditional branch or constant foldable conditional branch, but 873 // should also consider adding constant foldable switch instruction in 874 // future), we should keep looking for trivial condition candidates in 875 // the successor as well. An alternative is to constant fold conditions 876 // and merge successors into loop header (then we only need to check header's 877 // terminator). The reason for not doing this in LoopUnswitch pass is that 878 // it could potentially break LoopPassManager's invariants. Folding dead 879 // branches could either eliminate the current loop or make other loops 880 // unreachable. LCSSA form might also not be preserved after deleting 881 // branches. The following code keeps traversing loop header's successors 882 // until it finds the trivial condition candidate (condition that is not a 883 // constant). Since unswitching generates branches with constant conditions, 884 // this scenario could be very common in practice. 885 SmallSet<BasicBlock*, 8> Visited; 886 887 while (true) { 888 // If we exit loop or reach a previous visited block, then 889 // we can not reach any trivial condition candidates (unfoldable 890 // branch instructions or switch instructions) and no unswitch 891 // can happen. Exit and return false. 892 if (!currentLoop->contains(CurrentBB) || !Visited.insert(CurrentBB).second) 893 return false; 894 895 // Check if this loop will execute any side-effecting instructions (e.g. 896 // stores, calls, volatile loads) in the part of the loop that the code 897 // *would* execute. Check the header first. 898 for (Instruction &I : *CurrentBB) 899 if (I.mayHaveSideEffects()) 900 return false; 901 902 // FIXME: add check for constant foldable switch instructions. 903 if (BranchInst *BI = dyn_cast<BranchInst>(CurrentTerm)) { 904 if (BI->isUnconditional()) { 905 CurrentBB = BI->getSuccessor(0); 906 } else if (BI->getCondition() == ConstantInt::getTrue(Context)) { 907 CurrentBB = BI->getSuccessor(0); 908 } else if (BI->getCondition() == ConstantInt::getFalse(Context)) { 909 CurrentBB = BI->getSuccessor(1); 910 } else { 911 // Found a trivial condition candidate: non-foldable conditional branch. 912 break; 913 } 914 } else { 915 break; 916 } 917 918 CurrentTerm = CurrentBB->getTerminator(); 919 } 920 921 // CondVal is the condition that controls the trivial condition. 922 // LoopExitBB is the BasicBlock that loop exits when meets trivial condition. 923 Constant *CondVal = nullptr; 924 BasicBlock *LoopExitBB = nullptr; 925 926 if (BranchInst *BI = dyn_cast<BranchInst>(CurrentTerm)) { 927 // If this isn't branching on an invariant condition, we can't unswitch it. 928 if (!BI->isConditional()) 929 return false; 930 931 Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), 932 currentLoop, Changed); 933 934 // Unswitch only if the trivial condition itself is an LIV (not 935 // partial LIV which could occur in and/or) 936 if (!LoopCond || LoopCond != BI->getCondition()) 937 return false; 938 939 // Check to see if a successor of the branch is guaranteed to 940 // exit through a unique exit block without having any 941 // side-effects. If so, determine the value of Cond that causes 942 // it to do this. 943 if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, 944 BI->getSuccessor(0)))) { 945 CondVal = ConstantInt::getTrue(Context); 946 } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, 947 BI->getSuccessor(1)))) { 948 CondVal = ConstantInt::getFalse(Context); 949 } 950 951 // If we didn't find a single unique LoopExit block, or if the loop exit 952 // block contains phi nodes, this isn't trivial. 953 if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin())) 954 return false; // Can't handle this. 955 956 UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, LoopExitBB, 957 CurrentTerm); 958 ++NumBranches; 959 return true; 960 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurrentTerm)) { 961 // If this isn't switching on an invariant condition, we can't unswitch it. 962 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), 963 currentLoop, Changed); 964 965 // Unswitch only if the trivial condition itself is an LIV (not 966 // partial LIV which could occur in and/or) 967 if (!LoopCond || LoopCond != SI->getCondition()) 968 return false; 969 970 // Check to see if a successor of the switch is guaranteed to go to the 971 // latch block or exit through a one exit block without having any 972 // side-effects. If so, determine the value of Cond that causes it to do 973 // this. 974 // Note that we can't trivially unswitch on the default case or 975 // on already unswitched cases. 976 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 977 i != e; ++i) { 978 BasicBlock *LoopExitCandidate; 979 if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop, 980 i.getCaseSuccessor()))) { 981 // Okay, we found a trivial case, remember the value that is trivial. 982 ConstantInt *CaseVal = i.getCaseValue(); 983 984 // Check that it was not unswitched before, since already unswitched 985 // trivial vals are looks trivial too. 986 if (BranchesInfo.isUnswitched(SI, CaseVal)) 987 continue; 988 LoopExitBB = LoopExitCandidate; 989 CondVal = CaseVal; 990 break; 991 } 992 } 993 994 // If we didn't find a single unique LoopExit block, or if the loop exit 995 // block contains phi nodes, this isn't trivial. 996 if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin())) 997 return false; // Can't handle this. 998 999 UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, LoopExitBB, 1000 nullptr); 1001 ++NumSwitches; 1002 return true; 1003 } 1004 return false; 1005 } 1006 1007 /// Split all of the edges from inside the loop to their exit blocks. 1008 /// Update the appropriate Phi nodes as we do so. 1009 void LoopUnswitch::SplitExitEdges(Loop *L, 1010 const SmallVectorImpl<BasicBlock *> &ExitBlocks){ 1011 1012 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { 1013 BasicBlock *ExitBlock = ExitBlocks[i]; 1014 SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock), 1015 pred_end(ExitBlock)); 1016 1017 // Although SplitBlockPredecessors doesn't preserve loop-simplify in 1018 // general, if we call it on all predecessors of all exits then it does. 1019 SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa", DT, LI, 1020 /*PreserveLCSSA*/ true); 1021 } 1022 } 1023 1024 /// We determined that the loop is profitable to unswitch when LIC equal Val. 1025 /// Split it into loop versions and test the condition outside of either loop. 1026 /// Return the loops created as Out1/Out2. 1027 void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, 1028 Loop *L, TerminatorInst *TI) { 1029 Function *F = loopHeader->getParent(); 1030 DEBUG(dbgs() << "loop-unswitch: Unswitching loop %" 1031 << loopHeader->getName() << " [" << L->getBlocks().size() 1032 << " blocks] in Function " << F->getName() 1033 << " when '" << *Val << "' == " << *LIC << "\n"); 1034 1035 if (auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>()) 1036 SEWP->getSE().forgetLoop(L); 1037 1038 LoopBlocks.clear(); 1039 NewBlocks.clear(); 1040 1041 // First step, split the preheader and exit blocks, and add these blocks to 1042 // the LoopBlocks list. 1043 BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, DT, LI); 1044 LoopBlocks.push_back(NewPreheader); 1045 1046 // We want the loop to come after the preheader, but before the exit blocks. 1047 LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); 1048 1049 SmallVector<BasicBlock*, 8> ExitBlocks; 1050 L->getUniqueExitBlocks(ExitBlocks); 1051 1052 // Split all of the edges from inside the loop to their exit blocks. Update 1053 // the appropriate Phi nodes as we do so. 1054 SplitExitEdges(L, ExitBlocks); 1055 1056 // The exit blocks may have been changed due to edge splitting, recompute. 1057 ExitBlocks.clear(); 1058 L->getUniqueExitBlocks(ExitBlocks); 1059 1060 // Add exit blocks to the loop blocks. 1061 LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end()); 1062 1063 // Next step, clone all of the basic blocks that make up the loop (including 1064 // the loop preheader and exit blocks), keeping track of the mapping between 1065 // the instructions and blocks. 1066 NewBlocks.reserve(LoopBlocks.size()); 1067 ValueToValueMapTy VMap; 1068 for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { 1069 BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F); 1070 1071 NewBlocks.push_back(NewBB); 1072 VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping. 1073 LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L); 1074 } 1075 1076 // Splice the newly inserted blocks into the function right before the 1077 // original preheader. 1078 F->getBasicBlockList().splice(NewPreheader->getIterator(), 1079 F->getBasicBlockList(), 1080 NewBlocks[0]->getIterator(), F->end()); 1081 1082 // Now we create the new Loop object for the versioned loop. 1083 Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM); 1084 1085 // Recalculate unswitching quota, inherit simplified switches info for NewBB, 1086 // Probably clone more loop-unswitch related loop properties. 1087 BranchesInfo.cloneData(NewLoop, L, VMap); 1088 1089 Loop *ParentLoop = L->getParentLoop(); 1090 if (ParentLoop) { 1091 // Make sure to add the cloned preheader and exit blocks to the parent loop 1092 // as well. 1093 ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI); 1094 } 1095 1096 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { 1097 BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]); 1098 // The new exit block should be in the same loop as the old one. 1099 if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i])) 1100 ExitBBLoop->addBasicBlockToLoop(NewExit, *LI); 1101 1102 assert(NewExit->getTerminator()->getNumSuccessors() == 1 && 1103 "Exit block should have been split to have one successor!"); 1104 BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0); 1105 1106 // If the successor of the exit block had PHI nodes, add an entry for 1107 // NewExit. 1108 for (BasicBlock::iterator I = ExitSucc->begin(); 1109 PHINode *PN = dyn_cast<PHINode>(I); ++I) { 1110 Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]); 1111 ValueToValueMapTy::iterator It = VMap.find(V); 1112 if (It != VMap.end()) V = It->second; 1113 PN->addIncoming(V, NewExit); 1114 } 1115 1116 if (LandingPadInst *LPad = NewExit->getLandingPadInst()) { 1117 PHINode *PN = PHINode::Create(LPad->getType(), 0, "", 1118 &*ExitSucc->getFirstInsertionPt()); 1119 1120 for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc); 1121 I != E; ++I) { 1122 BasicBlock *BB = *I; 1123 LandingPadInst *LPI = BB->getLandingPadInst(); 1124 LPI->replaceAllUsesWith(PN); 1125 PN->addIncoming(LPI, BB); 1126 } 1127 } 1128 } 1129 1130 // Rewrite the code to refer to itself. 1131 for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) { 1132 for (Instruction &I : *NewBlocks[i]) { 1133 RemapInstruction(&I, VMap, 1134 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); 1135 if (auto *II = dyn_cast<IntrinsicInst>(&I)) 1136 if (II->getIntrinsicID() == Intrinsic::assume) 1137 AC->registerAssumption(II); 1138 } 1139 } 1140 1141 // Rewrite the original preheader to select between versions of the loop. 1142 BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator()); 1143 assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] && 1144 "Preheader splitting did not work correctly!"); 1145 1146 // Emit the new branch that selects between the two versions of this loop. 1147 EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR, 1148 TI); 1149 LPM->deleteSimpleAnalysisValue(OldBR, L); 1150 OldBR->eraseFromParent(); 1151 1152 LoopProcessWorklist.push_back(NewLoop); 1153 redoLoop = true; 1154 1155 // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody 1156 // deletes the instruction (for example by simplifying a PHI that feeds into 1157 // the condition that we're unswitching on), we don't rewrite the second 1158 // iteration. 1159 WeakVH LICHandle(LIC); 1160 1161 // Now we rewrite the original code to know that the condition is true and the 1162 // new code to know that the condition is false. 1163 RewriteLoopBodyWithConditionConstant(L, LIC, Val, false); 1164 1165 // It's possible that simplifying one loop could cause the other to be 1166 // changed to another value or a constant. If its a constant, don't simplify 1167 // it. 1168 if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop && 1169 LICHandle && !isa<Constant>(LICHandle)) 1170 RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true); 1171 } 1172 1173 /// Remove all instances of I from the worklist vector specified. 1174 static void RemoveFromWorklist(Instruction *I, 1175 std::vector<Instruction*> &Worklist) { 1176 1177 Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I), 1178 Worklist.end()); 1179 } 1180 1181 /// When we find that I really equals V, remove I from the 1182 /// program, replacing all uses with V and update the worklist. 1183 static void ReplaceUsesOfWith(Instruction *I, Value *V, 1184 std::vector<Instruction*> &Worklist, 1185 Loop *L, LPPassManager *LPM) { 1186 DEBUG(dbgs() << "Replace with '" << *V << "': " << *I); 1187 1188 // Add uses to the worklist, which may be dead now. 1189 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 1190 if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) 1191 Worklist.push_back(Use); 1192 1193 // Add users to the worklist which may be simplified now. 1194 for (User *U : I->users()) 1195 Worklist.push_back(cast<Instruction>(U)); 1196 LPM->deleteSimpleAnalysisValue(I, L); 1197 RemoveFromWorklist(I, Worklist); 1198 I->replaceAllUsesWith(V); 1199 I->eraseFromParent(); 1200 ++NumSimplify; 1201 } 1202 1203 /// We know either that the value LIC has the value specified by Val in the 1204 /// specified loop, or we know it does NOT have that value. 1205 /// Rewrite any uses of LIC or of properties correlated to it. 1206 void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, 1207 Constant *Val, 1208 bool IsEqual) { 1209 assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); 1210 1211 // FIXME: Support correlated properties, like: 1212 // for (...) 1213 // if (li1 < li2) 1214 // ... 1215 // if (li1 > li2) 1216 // ... 1217 1218 // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches, 1219 // selects, switches. 1220 std::vector<Instruction*> Worklist; 1221 LLVMContext &Context = Val->getContext(); 1222 1223 // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC 1224 // in the loop with the appropriate one directly. 1225 if (IsEqual || (isa<ConstantInt>(Val) && 1226 Val->getType()->isIntegerTy(1))) { 1227 Value *Replacement; 1228 if (IsEqual) 1229 Replacement = Val; 1230 else 1231 Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()), 1232 !cast<ConstantInt>(Val)->getZExtValue()); 1233 1234 for (User *U : LIC->users()) { 1235 Instruction *UI = dyn_cast<Instruction>(U); 1236 if (!UI || !L->contains(UI)) 1237 continue; 1238 Worklist.push_back(UI); 1239 } 1240 1241 for (Instruction *UI : Worklist) 1242 UI->replaceUsesOfWith(LIC, Replacement); 1243 1244 SimplifyCode(Worklist, L); 1245 return; 1246 } 1247 1248 // Otherwise, we don't know the precise value of LIC, but we do know that it 1249 // is certainly NOT "Val". As such, simplify any uses in the loop that we 1250 // can. This case occurs when we unswitch switch statements. 1251 for (User *U : LIC->users()) { 1252 Instruction *UI = dyn_cast<Instruction>(U); 1253 if (!UI || !L->contains(UI)) 1254 continue; 1255 1256 Worklist.push_back(UI); 1257 1258 // TODO: We could do other simplifications, for example, turning 1259 // 'icmp eq LIC, Val' -> false. 1260 1261 // If we know that LIC is not Val, use this info to simplify code. 1262 SwitchInst *SI = dyn_cast<SwitchInst>(UI); 1263 if (!SI || !isa<ConstantInt>(Val)) continue; 1264 1265 SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val)); 1266 // Default case is live for multiple values. 1267 if (DeadCase == SI->case_default()) continue; 1268 1269 // Found a dead case value. Don't remove PHI nodes in the 1270 // successor if they become single-entry, those PHI nodes may 1271 // be in the Users list. 1272 1273 BasicBlock *Switch = SI->getParent(); 1274 BasicBlock *SISucc = DeadCase.getCaseSuccessor(); 1275 BasicBlock *Latch = L->getLoopLatch(); 1276 1277 BranchesInfo.setUnswitched(SI, Val); 1278 1279 if (!SI->findCaseDest(SISucc)) continue; // Edge is critical. 1280 // If the DeadCase successor dominates the loop latch, then the 1281 // transformation isn't safe since it will delete the sole predecessor edge 1282 // to the latch. 1283 if (Latch && DT->dominates(SISucc, Latch)) 1284 continue; 1285 1286 // FIXME: This is a hack. We need to keep the successor around 1287 // and hooked up so as to preserve the loop structure, because 1288 // trying to update it is complicated. So instead we preserve the 1289 // loop structure and put the block on a dead code path. 1290 SplitEdge(Switch, SISucc, DT, LI); 1291 // Compute the successors instead of relying on the return value 1292 // of SplitEdge, since it may have split the switch successor 1293 // after PHI nodes. 1294 BasicBlock *NewSISucc = DeadCase.getCaseSuccessor(); 1295 BasicBlock *OldSISucc = *succ_begin(NewSISucc); 1296 // Create an "unreachable" destination. 1297 BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable", 1298 Switch->getParent(), 1299 OldSISucc); 1300 new UnreachableInst(Context, Abort); 1301 // Force the new case destination to branch to the "unreachable" 1302 // block while maintaining a (dead) CFG edge to the old block. 1303 NewSISucc->getTerminator()->eraseFromParent(); 1304 BranchInst::Create(Abort, OldSISucc, 1305 ConstantInt::getTrue(Context), NewSISucc); 1306 // Release the PHI operands for this edge. 1307 for (BasicBlock::iterator II = NewSISucc->begin(); 1308 PHINode *PN = dyn_cast<PHINode>(II); ++II) 1309 PN->setIncomingValue(PN->getBasicBlockIndex(Switch), 1310 UndefValue::get(PN->getType())); 1311 // Tell the domtree about the new block. We don't fully update the 1312 // domtree here -- instead we force it to do a full recomputation 1313 // after the pass is complete -- but we do need to inform it of 1314 // new blocks. 1315 DT->addNewBlock(Abort, NewSISucc); 1316 } 1317 1318 SimplifyCode(Worklist, L); 1319 } 1320 1321 /// Now that we have simplified some instructions in the loop, walk over it and 1322 /// constant prop, dce, and fold control flow where possible. Note that this is 1323 /// effectively a very simple loop-structure-aware optimizer. During processing 1324 /// of this loop, L could very well be deleted, so it must not be used. 1325 /// 1326 /// FIXME: When the loop optimizer is more mature, separate this out to a new 1327 /// pass. 1328 /// 1329 void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) { 1330 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); 1331 while (!Worklist.empty()) { 1332 Instruction *I = Worklist.back(); 1333 Worklist.pop_back(); 1334 1335 // Simple DCE. 1336 if (isInstructionTriviallyDead(I)) { 1337 DEBUG(dbgs() << "Remove dead instruction '" << *I); 1338 1339 // Add uses to the worklist, which may be dead now. 1340 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 1341 if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) 1342 Worklist.push_back(Use); 1343 LPM->deleteSimpleAnalysisValue(I, L); 1344 RemoveFromWorklist(I, Worklist); 1345 I->eraseFromParent(); 1346 ++NumSimplify; 1347 continue; 1348 } 1349 1350 // See if instruction simplification can hack this up. This is common for 1351 // things like "select false, X, Y" after unswitching made the condition be 1352 // 'false'. TODO: update the domtree properly so we can pass it here. 1353 if (Value *V = SimplifyInstruction(I, DL)) 1354 if (LI->replacementPreservesLCSSAForm(I, V)) { 1355 ReplaceUsesOfWith(I, V, Worklist, L, LPM); 1356 continue; 1357 } 1358 1359 // Special case hacks that appear commonly in unswitched code. 1360 if (BranchInst *BI = dyn_cast<BranchInst>(I)) { 1361 if (BI->isUnconditional()) { 1362 // If BI's parent is the only pred of the successor, fold the two blocks 1363 // together. 1364 BasicBlock *Pred = BI->getParent(); 1365 BasicBlock *Succ = BI->getSuccessor(0); 1366 BasicBlock *SinglePred = Succ->getSinglePredecessor(); 1367 if (!SinglePred) continue; // Nothing to do. 1368 assert(SinglePred == Pred && "CFG broken"); 1369 1370 DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- " 1371 << Succ->getName() << "\n"); 1372 1373 // Resolve any single entry PHI nodes in Succ. 1374 while (PHINode *PN = dyn_cast<PHINode>(Succ->begin())) 1375 ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM); 1376 1377 // If Succ has any successors with PHI nodes, update them to have 1378 // entries coming from Pred instead of Succ. 1379 Succ->replaceAllUsesWith(Pred); 1380 1381 // Move all of the successor contents from Succ to Pred. 1382 Pred->getInstList().splice(BI->getIterator(), Succ->getInstList(), 1383 Succ->begin(), Succ->end()); 1384 LPM->deleteSimpleAnalysisValue(BI, L); 1385 RemoveFromWorklist(BI, Worklist); 1386 BI->eraseFromParent(); 1387 1388 // Remove Succ from the loop tree. 1389 LI->removeBlock(Succ); 1390 LPM->deleteSimpleAnalysisValue(Succ, L); 1391 Succ->eraseFromParent(); 1392 ++NumSimplify; 1393 continue; 1394 } 1395 1396 continue; 1397 } 1398 } 1399 } 1400