1 //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass performs loop invariant code motion, attempting to remove as much 10 // code from the body of a loop as possible. It does this by either hoisting 11 // code into the preheader block, or by sinking code to the exit blocks if it is 12 // safe. This pass also promotes must-aliased memory locations in the loop to 13 // live in registers, thus hoisting and sinking "invariant" loads and stores. 14 // 15 // This pass uses alias analysis for two purposes: 16 // 17 // 1. Moving loop invariant loads and calls out of loops. If we can determine 18 // that a load or call inside of a loop never aliases anything stored to, 19 // we can hoist it or sink it like any other instruction. 20 // 2. Scalar Promotion of Memory - If there is a store instruction inside of 21 // the loop, we try to move the store to happen AFTER the loop instead of 22 // inside of the loop. This can only happen if a few conditions are true: 23 // A. The pointer stored through is loop invariant 24 // B. There are no stores or loads in the loop which _may_ alias the 25 // pointer. There are no calls in the loop which mod/ref the pointer. 26 // If these conditions are true, we can promote the loads and stores in the 27 // loop of the pointer to use a temporary alloca'd variable. We then use 28 // the SSAUpdater to construct the appropriate SSA form for the value. 29 // 30 //===----------------------------------------------------------------------===// 31 32 #include "llvm/Transforms/Scalar/LICM.h" 33 #include "llvm/ADT/SetOperations.h" 34 #include "llvm/ADT/Statistic.h" 35 #include "llvm/Analysis/AliasAnalysis.h" 36 #include "llvm/Analysis/AliasSetTracker.h" 37 #include "llvm/Analysis/BasicAliasAnalysis.h" 38 #include "llvm/Analysis/CaptureTracking.h" 39 #include "llvm/Analysis/ConstantFolding.h" 40 #include "llvm/Analysis/GlobalsModRef.h" 41 #include "llvm/Analysis/GuardUtils.h" 42 #include "llvm/Analysis/Loads.h" 43 #include "llvm/Analysis/LoopInfo.h" 44 #include "llvm/Analysis/LoopIterator.h" 45 #include "llvm/Analysis/LoopPass.h" 46 #include "llvm/Analysis/MemoryBuiltins.h" 47 #include "llvm/Analysis/MemorySSA.h" 48 #include "llvm/Analysis/MemorySSAUpdater.h" 49 #include "llvm/Analysis/MustExecute.h" 50 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 51 #include "llvm/Analysis/ScalarEvolution.h" 52 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 53 #include "llvm/Analysis/TargetLibraryInfo.h" 54 #include "llvm/Analysis/ValueTracking.h" 55 #include "llvm/IR/CFG.h" 56 #include "llvm/IR/Constants.h" 57 #include "llvm/IR/DataLayout.h" 58 #include "llvm/IR/DebugInfoMetadata.h" 59 #include "llvm/IR/DerivedTypes.h" 60 #include "llvm/IR/Dominators.h" 61 #include "llvm/IR/Instructions.h" 62 #include "llvm/IR/IntrinsicInst.h" 63 #include "llvm/IR/LLVMContext.h" 64 #include "llvm/IR/Metadata.h" 65 #include "llvm/IR/PatternMatch.h" 66 #include "llvm/IR/PredIteratorCache.h" 67 #include "llvm/InitializePasses.h" 68 #include "llvm/Support/CommandLine.h" 69 #include "llvm/Support/Debug.h" 70 #include "llvm/Support/raw_ostream.h" 71 #include "llvm/Transforms/Scalar.h" 72 #include "llvm/Transforms/Scalar/LoopPassManager.h" 73 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 74 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 75 #include "llvm/Transforms/Utils/Local.h" 76 #include "llvm/Transforms/Utils/LoopUtils.h" 77 #include "llvm/Transforms/Utils/SSAUpdater.h" 78 #include <algorithm> 79 #include <utility> 80 using namespace llvm; 81 82 #define DEBUG_TYPE "licm" 83 84 STATISTIC(NumCreatedBlocks, "Number of blocks created"); 85 STATISTIC(NumClonedBranches, "Number of branches cloned"); 86 STATISTIC(NumSunk, "Number of instructions sunk out of loop"); 87 STATISTIC(NumHoisted, "Number of instructions hoisted out of loop"); 88 STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk"); 89 STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk"); 90 STATISTIC(NumPromoted, "Number of memory locations promoted to registers"); 91 92 /// Memory promotion is enabled by default. 93 static cl::opt<bool> 94 DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false), 95 cl::desc("Disable memory promotion in LICM pass")); 96 97 static cl::opt<bool> ControlFlowHoisting( 98 "licm-control-flow-hoisting", cl::Hidden, cl::init(false), 99 cl::desc("Enable control flow (and PHI) hoisting in LICM")); 100 101 static cl::opt<uint32_t> MaxNumUsesTraversed( 102 "licm-max-num-uses-traversed", cl::Hidden, cl::init(8), 103 cl::desc("Max num uses visited for identifying load " 104 "invariance in loop using invariant start (default = 8)")); 105 106 // Default value of zero implies we use the regular alias set tracker mechanism 107 // instead of the cross product using AA to identify aliasing of the memory 108 // location we are interested in. 109 static cl::opt<int> 110 LICMN2Theshold("licm-n2-threshold", cl::Hidden, cl::init(0), 111 cl::desc("How many instruction to cross product using AA")); 112 113 // Experimental option to allow imprecision in LICM in pathological cases, in 114 // exchange for faster compile. This is to be removed if MemorySSA starts to 115 // address the same issue. This flag applies only when LICM uses MemorySSA 116 // instead on AliasSetTracker. LICM calls MemorySSAWalker's 117 // getClobberingMemoryAccess, up to the value of the Cap, getting perfect 118 // accuracy. Afterwards, LICM will call into MemorySSA's getDefiningAccess, 119 // which may not be precise, since optimizeUses is capped. The result is 120 // correct, but we may not get as "far up" as possible to get which access is 121 // clobbering the one queried. 122 cl::opt<unsigned> llvm::SetLicmMssaOptCap( 123 "licm-mssa-optimization-cap", cl::init(100), cl::Hidden, 124 cl::desc("Enable imprecision in LICM in pathological cases, in exchange " 125 "for faster compile. Caps the MemorySSA clobbering calls.")); 126 127 // Experimentally, memory promotion carries less importance than sinking and 128 // hoisting. Limit when we do promotion when using MemorySSA, in order to save 129 // compile time. 130 cl::opt<unsigned> llvm::SetLicmMssaNoAccForPromotionCap( 131 "licm-mssa-max-acc-promotion", cl::init(250), cl::Hidden, 132 cl::desc("[LICM & MemorySSA] When MSSA in LICM is disabled, this has no " 133 "effect. When MSSA in LICM is enabled, then this is the maximum " 134 "number of accesses allowed to be present in a loop in order to " 135 "enable memory promotion.")); 136 137 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); 138 static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, 139 const LoopSafetyInfo *SafetyInfo, 140 TargetTransformInfo *TTI, bool &FreeInLoop); 141 static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, 142 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, 143 MemorySSAUpdater *MSSAU, ScalarEvolution *SE, 144 OptimizationRemarkEmitter *ORE); 145 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, 146 const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, 147 MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE); 148 static bool isSafeToExecuteUnconditionally(Instruction &Inst, 149 const DominatorTree *DT, 150 const Loop *CurLoop, 151 const LoopSafetyInfo *SafetyInfo, 152 OptimizationRemarkEmitter *ORE, 153 const Instruction *CtxI = nullptr); 154 static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, 155 AliasSetTracker *CurAST, Loop *CurLoop, 156 AliasAnalysis *AA); 157 static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, 158 Loop *CurLoop, 159 SinkAndHoistLICMFlags &Flags); 160 static Instruction *cloneInstructionInExitBlock( 161 Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, 162 const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU); 163 164 static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, 165 AliasSetTracker *AST, MemorySSAUpdater *MSSAU); 166 167 static void moveInstructionBefore(Instruction &I, Instruction &Dest, 168 ICFLoopSafetyInfo &SafetyInfo, 169 MemorySSAUpdater *MSSAU, ScalarEvolution *SE); 170 171 namespace { 172 struct LoopInvariantCodeMotion { 173 bool runOnLoop(Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT, 174 TargetLibraryInfo *TLI, TargetTransformInfo *TTI, 175 ScalarEvolution *SE, MemorySSA *MSSA, 176 OptimizationRemarkEmitter *ORE); 177 178 LoopInvariantCodeMotion(unsigned LicmMssaOptCap, 179 unsigned LicmMssaNoAccForPromotionCap) 180 : LicmMssaOptCap(LicmMssaOptCap), 181 LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap) {} 182 183 private: 184 unsigned LicmMssaOptCap; 185 unsigned LicmMssaNoAccForPromotionCap; 186 187 std::unique_ptr<AliasSetTracker> 188 collectAliasInfoForLoop(Loop *L, LoopInfo *LI, AliasAnalysis *AA); 189 std::unique_ptr<AliasSetTracker> 190 collectAliasInfoForLoopWithMSSA(Loop *L, AliasAnalysis *AA, 191 MemorySSAUpdater *MSSAU); 192 }; 193 194 struct LegacyLICMPass : public LoopPass { 195 static char ID; // Pass identification, replacement for typeid 196 LegacyLICMPass( 197 unsigned LicmMssaOptCap = SetLicmMssaOptCap, 198 unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap) 199 : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap) { 200 initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry()); 201 } 202 203 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 204 if (skipLoop(L)) 205 return false; 206 207 auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); 208 MemorySSA *MSSA = EnableMSSALoopDependency 209 ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA()) 210 : nullptr; 211 // For the old PM, we can't use OptimizationRemarkEmitter as an analysis 212 // pass. Function analyses need to be preserved across loop transformations 213 // but ORE cannot be preserved (see comment before the pass definition). 214 OptimizationRemarkEmitter ORE(L->getHeader()->getParent()); 215 return LICM.runOnLoop(L, 216 &getAnalysis<AAResultsWrapperPass>().getAAResults(), 217 &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), 218 &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), 219 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( 220 *L->getHeader()->getParent()), 221 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI( 222 *L->getHeader()->getParent()), 223 SE ? &SE->getSE() : nullptr, MSSA, &ORE); 224 } 225 226 /// This transformation requires natural loop information & requires that 227 /// loop preheaders be inserted into the CFG... 228 /// 229 void getAnalysisUsage(AnalysisUsage &AU) const override { 230 AU.addPreserved<DominatorTreeWrapperPass>(); 231 AU.addPreserved<LoopInfoWrapperPass>(); 232 AU.addRequired<TargetLibraryInfoWrapperPass>(); 233 if (EnableMSSALoopDependency) { 234 AU.addRequired<MemorySSAWrapperPass>(); 235 AU.addPreserved<MemorySSAWrapperPass>(); 236 } 237 AU.addRequired<TargetTransformInfoWrapperPass>(); 238 getLoopAnalysisUsage(AU); 239 } 240 241 private: 242 LoopInvariantCodeMotion LICM; 243 }; 244 } // namespace 245 246 PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM, 247 LoopStandardAnalysisResults &AR, LPMUpdater &) { 248 // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis 249 // pass. Function analyses need to be preserved across loop transformations 250 // but ORE cannot be preserved (see comment before the pass definition). 251 OptimizationRemarkEmitter ORE(L.getHeader()->getParent()); 252 253 LoopInvariantCodeMotion LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); 254 if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, &AR.TLI, &AR.TTI, &AR.SE, 255 AR.MSSA, &ORE)) 256 return PreservedAnalyses::all(); 257 258 auto PA = getLoopPassPreservedAnalyses(); 259 260 PA.preserve<DominatorTreeAnalysis>(); 261 PA.preserve<LoopAnalysis>(); 262 if (AR.MSSA) 263 PA.preserve<MemorySSAAnalysis>(); 264 265 return PA; 266 } 267 268 char LegacyLICMPass::ID = 0; 269 INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion", 270 false, false) 271 INITIALIZE_PASS_DEPENDENCY(LoopPass) 272 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 273 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 274 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 275 INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false, 276 false) 277 278 Pass *llvm::createLICMPass() { return new LegacyLICMPass(); } 279 Pass *llvm::createLICMPass(unsigned LicmMssaOptCap, 280 unsigned LicmMssaNoAccForPromotionCap) { 281 return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap); 282 } 283 284 /// Hoist expressions out of the specified loop. Note, alias info for inner 285 /// loop is not preserved so it is not a good idea to run LICM multiple 286 /// times on one loop. 287 bool LoopInvariantCodeMotion::runOnLoop( 288 Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT, 289 TargetLibraryInfo *TLI, TargetTransformInfo *TTI, ScalarEvolution *SE, 290 MemorySSA *MSSA, OptimizationRemarkEmitter *ORE) { 291 bool Changed = false; 292 293 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form."); 294 295 // If this loop has metadata indicating that LICM is not to be performed then 296 // just exit. 297 if (hasDisableLICMTransformsHint(L)) { 298 return false; 299 } 300 301 std::unique_ptr<AliasSetTracker> CurAST; 302 std::unique_ptr<MemorySSAUpdater> MSSAU; 303 bool NoOfMemAccTooLarge = false; 304 unsigned LicmMssaOptCounter = 0; 305 306 if (!MSSA) { 307 LLVM_DEBUG(dbgs() << "LICM: Using Alias Set Tracker.\n"); 308 CurAST = collectAliasInfoForLoop(L, LI, AA); 309 } else { 310 LLVM_DEBUG(dbgs() << "LICM: Using MemorySSA.\n"); 311 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA); 312 313 unsigned AccessCapCount = 0; 314 for (auto *BB : L->getBlocks()) { 315 if (auto *Accesses = MSSA->getBlockAccesses(BB)) { 316 for (const auto &MA : *Accesses) { 317 (void)MA; 318 AccessCapCount++; 319 if (AccessCapCount > LicmMssaNoAccForPromotionCap) { 320 NoOfMemAccTooLarge = true; 321 break; 322 } 323 } 324 } 325 if (NoOfMemAccTooLarge) 326 break; 327 } 328 } 329 330 // Get the preheader block to move instructions into... 331 BasicBlock *Preheader = L->getLoopPreheader(); 332 333 // Compute loop safety information. 334 ICFLoopSafetyInfo SafetyInfo; 335 SafetyInfo.computeLoopSafetyInfo(L); 336 337 // We want to visit all of the instructions in this loop... that are not parts 338 // of our subloops (they have already had their invariants hoisted out of 339 // their loop, into this loop, so there is no need to process the BODIES of 340 // the subloops). 341 // 342 // Traverse the body of the loop in depth first order on the dominator tree so 343 // that we are guaranteed to see definitions before we see uses. This allows 344 // us to sink instructions in one pass, without iteration. After sinking 345 // instructions, we perform another pass to hoist them out of the loop. 346 SinkAndHoistLICMFlags Flags = {NoOfMemAccTooLarge, LicmMssaOptCounter, 347 LicmMssaOptCap, LicmMssaNoAccForPromotionCap, 348 /*IsSink=*/true}; 349 if (L->hasDedicatedExits()) 350 Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, TTI, L, 351 CurAST.get(), MSSAU.get(), &SafetyInfo, Flags, ORE); 352 Flags.IsSink = false; 353 if (Preheader) 354 Changed |= 355 hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, L, 356 CurAST.get(), MSSAU.get(), SE, &SafetyInfo, Flags, ORE); 357 358 // Now that all loop invariants have been removed from the loop, promote any 359 // memory references to scalars that we can. 360 // Don't sink stores from loops without dedicated block exits. Exits 361 // containing indirect branches are not transformed by loop simplify, 362 // make sure we catch that. An additional load may be generated in the 363 // preheader for SSA updater, so also avoid sinking when no preheader 364 // is available. 365 if (!DisablePromotion && Preheader && L->hasDedicatedExits() && 366 !NoOfMemAccTooLarge) { 367 // Figure out the loop exits and their insertion points 368 SmallVector<BasicBlock *, 8> ExitBlocks; 369 L->getUniqueExitBlocks(ExitBlocks); 370 371 // We can't insert into a catchswitch. 372 bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) { 373 return isa<CatchSwitchInst>(Exit->getTerminator()); 374 }); 375 376 if (!HasCatchSwitch) { 377 SmallVector<Instruction *, 8> InsertPts; 378 SmallVector<MemoryAccess *, 8> MSSAInsertPts; 379 InsertPts.reserve(ExitBlocks.size()); 380 if (MSSAU) 381 MSSAInsertPts.reserve(ExitBlocks.size()); 382 for (BasicBlock *ExitBlock : ExitBlocks) { 383 InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); 384 if (MSSAU) 385 MSSAInsertPts.push_back(nullptr); 386 } 387 388 PredIteratorCache PIC; 389 390 bool Promoted = false; 391 392 // Build an AST using MSSA. 393 if (!CurAST.get()) 394 CurAST = collectAliasInfoForLoopWithMSSA(L, AA, MSSAU.get()); 395 396 // Loop over all of the alias sets in the tracker object. 397 for (AliasSet &AS : *CurAST) { 398 // We can promote this alias set if it has a store, if it is a "Must" 399 // alias set, if the pointer is loop invariant, and if we are not 400 // eliminating any volatile loads or stores. 401 if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || 402 !L->isLoopInvariant(AS.begin()->getValue())) 403 continue; 404 405 assert( 406 !AS.empty() && 407 "Must alias set should have at least one pointer element in it!"); 408 409 SmallSetVector<Value *, 8> PointerMustAliases; 410 for (const auto &ASI : AS) 411 PointerMustAliases.insert(ASI.getValue()); 412 413 Promoted |= promoteLoopAccessesToScalars( 414 PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI, 415 DT, TLI, L, CurAST.get(), MSSAU.get(), &SafetyInfo, ORE); 416 } 417 418 // Once we have promoted values across the loop body we have to 419 // recursively reform LCSSA as any nested loop may now have values defined 420 // within the loop used in the outer loop. 421 // FIXME: This is really heavy handed. It would be a bit better to use an 422 // SSAUpdater strategy during promotion that was LCSSA aware and reformed 423 // it as it went. 424 if (Promoted) 425 formLCSSARecursively(*L, *DT, LI, SE); 426 427 Changed |= Promoted; 428 } 429 } 430 431 // Check that neither this loop nor its parent have had LCSSA broken. LICM is 432 // specifically moving instructions across the loop boundary and so it is 433 // especially in need of sanity checking here. 434 assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!"); 435 assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) && 436 "Parent loop not left in LCSSA form after LICM!"); 437 438 if (MSSAU.get() && VerifyMemorySSA) 439 MSSAU->getMemorySSA()->verifyMemorySSA(); 440 441 if (Changed && SE) 442 SE->forgetLoopDispositions(L); 443 return Changed; 444 } 445 446 /// Walk the specified region of the CFG (defined by all blocks dominated by 447 /// the specified block, and that are in the current loop) in reverse depth 448 /// first order w.r.t the DominatorTree. This allows us to visit uses before 449 /// definitions, allowing us to sink a loop body in one pass without iteration. 450 /// 451 bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI, 452 DominatorTree *DT, TargetLibraryInfo *TLI, 453 TargetTransformInfo *TTI, Loop *CurLoop, 454 AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, 455 ICFLoopSafetyInfo *SafetyInfo, 456 SinkAndHoistLICMFlags &Flags, 457 OptimizationRemarkEmitter *ORE) { 458 459 // Verify inputs. 460 assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && 461 CurLoop != nullptr && SafetyInfo != nullptr && 462 "Unexpected input to sinkRegion."); 463 assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) && 464 "Either AliasSetTracker or MemorySSA should be initialized."); 465 466 // We want to visit children before parents. We will enque all the parents 467 // before their children in the worklist and process the worklist in reverse 468 // order. 469 SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop); 470 471 bool Changed = false; 472 for (DomTreeNode *DTN : reverse(Worklist)) { 473 BasicBlock *BB = DTN->getBlock(); 474 // Only need to process the contents of this block if it is not part of a 475 // subloop (which would already have been processed). 476 if (inSubLoop(BB, CurLoop, LI)) 477 continue; 478 479 for (BasicBlock::iterator II = BB->end(); II != BB->begin();) { 480 Instruction &I = *--II; 481 482 // If the instruction is dead, we would try to sink it because it isn't 483 // used in the loop, instead, just delete it. 484 if (isInstructionTriviallyDead(&I, TLI)) { 485 LLVM_DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n'); 486 salvageKnowledge(&I); 487 salvageDebugInfo(I); 488 ++II; 489 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); 490 Changed = true; 491 continue; 492 } 493 494 // Check to see if we can sink this instruction to the exit blocks 495 // of the loop. We can do this if the all users of the instruction are 496 // outside of the loop. In this case, it doesn't even matter if the 497 // operands of the instruction are loop invariant. 498 // 499 bool FreeInLoop = false; 500 if (!I.mayHaveSideEffects() && 501 isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) && 502 canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, 503 ORE)) { 504 if (sink(I, LI, DT, CurLoop, SafetyInfo, MSSAU, ORE)) { 505 if (!FreeInLoop) { 506 ++II; 507 salvageDebugInfoOrMarkUndef(I); 508 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); 509 } 510 Changed = true; 511 } 512 } 513 } 514 } 515 if (MSSAU && VerifyMemorySSA) 516 MSSAU->getMemorySSA()->verifyMemorySSA(); 517 return Changed; 518 } 519 520 namespace { 521 // This is a helper class for hoistRegion to make it able to hoist control flow 522 // in order to be able to hoist phis. The way this works is that we initially 523 // start hoisting to the loop preheader, and when we see a loop invariant branch 524 // we make note of this. When we then come to hoist an instruction that's 525 // conditional on such a branch we duplicate the branch and the relevant control 526 // flow, then hoist the instruction into the block corresponding to its original 527 // block in the duplicated control flow. 528 class ControlFlowHoister { 529 private: 530 // Information about the loop we are hoisting from 531 LoopInfo *LI; 532 DominatorTree *DT; 533 Loop *CurLoop; 534 MemorySSAUpdater *MSSAU; 535 536 // A map of blocks in the loop to the block their instructions will be hoisted 537 // to. 538 DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap; 539 540 // The branches that we can hoist, mapped to the block that marks a 541 // convergence point of their control flow. 542 DenseMap<BranchInst *, BasicBlock *> HoistableBranches; 543 544 public: 545 ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop, 546 MemorySSAUpdater *MSSAU) 547 : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {} 548 549 void registerPossiblyHoistableBranch(BranchInst *BI) { 550 // We can only hoist conditional branches with loop invariant operands. 551 if (!ControlFlowHoisting || !BI->isConditional() || 552 !CurLoop->hasLoopInvariantOperands(BI)) 553 return; 554 555 // The branch destinations need to be in the loop, and we don't gain 556 // anything by duplicating conditional branches with duplicate successors, 557 // as it's essentially the same as an unconditional branch. 558 BasicBlock *TrueDest = BI->getSuccessor(0); 559 BasicBlock *FalseDest = BI->getSuccessor(1); 560 if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) || 561 TrueDest == FalseDest) 562 return; 563 564 // We can hoist BI if one branch destination is the successor of the other, 565 // or both have common successor which we check by seeing if the 566 // intersection of their successors is non-empty. 567 // TODO: This could be expanded to allowing branches where both ends 568 // eventually converge to a single block. 569 SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc; 570 TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest)); 571 FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest)); 572 BasicBlock *CommonSucc = nullptr; 573 if (TrueDestSucc.count(FalseDest)) { 574 CommonSucc = FalseDest; 575 } else if (FalseDestSucc.count(TrueDest)) { 576 CommonSucc = TrueDest; 577 } else { 578 set_intersect(TrueDestSucc, FalseDestSucc); 579 // If there's one common successor use that. 580 if (TrueDestSucc.size() == 1) 581 CommonSucc = *TrueDestSucc.begin(); 582 // If there's more than one pick whichever appears first in the block list 583 // (we can't use the value returned by TrueDestSucc.begin() as it's 584 // unpredicatable which element gets returned). 585 else if (!TrueDestSucc.empty()) { 586 Function *F = TrueDest->getParent(); 587 auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); }; 588 auto It = std::find_if(F->begin(), F->end(), IsSucc); 589 assert(It != F->end() && "Could not find successor in function"); 590 CommonSucc = &*It; 591 } 592 } 593 // The common successor has to be dominated by the branch, as otherwise 594 // there will be some other path to the successor that will not be 595 // controlled by this branch so any phi we hoist would be controlled by the 596 // wrong condition. This also takes care of avoiding hoisting of loop back 597 // edges. 598 // TODO: In some cases this could be relaxed if the successor is dominated 599 // by another block that's been hoisted and we can guarantee that the 600 // control flow has been replicated exactly. 601 if (CommonSucc && DT->dominates(BI, CommonSucc)) 602 HoistableBranches[BI] = CommonSucc; 603 } 604 605 bool canHoistPHI(PHINode *PN) { 606 // The phi must have loop invariant operands. 607 if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN)) 608 return false; 609 // We can hoist phis if the block they are in is the target of hoistable 610 // branches which cover all of the predecessors of the block. 611 SmallPtrSet<BasicBlock *, 8> PredecessorBlocks; 612 BasicBlock *BB = PN->getParent(); 613 for (BasicBlock *PredBB : predecessors(BB)) 614 PredecessorBlocks.insert(PredBB); 615 // If we have less predecessor blocks than predecessors then the phi will 616 // have more than one incoming value for the same block which we can't 617 // handle. 618 // TODO: This could be handled be erasing some of the duplicate incoming 619 // values. 620 if (PredecessorBlocks.size() != pred_size(BB)) 621 return false; 622 for (auto &Pair : HoistableBranches) { 623 if (Pair.second == BB) { 624 // Which blocks are predecessors via this branch depends on if the 625 // branch is triangle-like or diamond-like. 626 if (Pair.first->getSuccessor(0) == BB) { 627 PredecessorBlocks.erase(Pair.first->getParent()); 628 PredecessorBlocks.erase(Pair.first->getSuccessor(1)); 629 } else if (Pair.first->getSuccessor(1) == BB) { 630 PredecessorBlocks.erase(Pair.first->getParent()); 631 PredecessorBlocks.erase(Pair.first->getSuccessor(0)); 632 } else { 633 PredecessorBlocks.erase(Pair.first->getSuccessor(0)); 634 PredecessorBlocks.erase(Pair.first->getSuccessor(1)); 635 } 636 } 637 } 638 // PredecessorBlocks will now be empty if for every predecessor of BB we 639 // found a hoistable branch source. 640 return PredecessorBlocks.empty(); 641 } 642 643 BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) { 644 if (!ControlFlowHoisting) 645 return CurLoop->getLoopPreheader(); 646 // If BB has already been hoisted, return that 647 if (HoistDestinationMap.count(BB)) 648 return HoistDestinationMap[BB]; 649 650 // Check if this block is conditional based on a pending branch 651 auto HasBBAsSuccessor = 652 [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) { 653 return BB != Pair.second && (Pair.first->getSuccessor(0) == BB || 654 Pair.first->getSuccessor(1) == BB); 655 }; 656 auto It = std::find_if(HoistableBranches.begin(), HoistableBranches.end(), 657 HasBBAsSuccessor); 658 659 // If not involved in a pending branch, hoist to preheader 660 BasicBlock *InitialPreheader = CurLoop->getLoopPreheader(); 661 if (It == HoistableBranches.end()) { 662 LLVM_DEBUG(dbgs() << "LICM using " << InitialPreheader->getName() 663 << " as hoist destination for " << BB->getName() 664 << "\n"); 665 HoistDestinationMap[BB] = InitialPreheader; 666 return InitialPreheader; 667 } 668 BranchInst *BI = It->first; 669 assert(std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == 670 HoistableBranches.end() && 671 "BB is expected to be the target of at most one branch"); 672 673 LLVMContext &C = BB->getContext(); 674 BasicBlock *TrueDest = BI->getSuccessor(0); 675 BasicBlock *FalseDest = BI->getSuccessor(1); 676 BasicBlock *CommonSucc = HoistableBranches[BI]; 677 BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent()); 678 679 // Create hoisted versions of blocks that currently don't have them 680 auto CreateHoistedBlock = [&](BasicBlock *Orig) { 681 if (HoistDestinationMap.count(Orig)) 682 return HoistDestinationMap[Orig]; 683 BasicBlock *New = 684 BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent()); 685 HoistDestinationMap[Orig] = New; 686 DT->addNewBlock(New, HoistTarget); 687 if (CurLoop->getParentLoop()) 688 CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI); 689 ++NumCreatedBlocks; 690 LLVM_DEBUG(dbgs() << "LICM created " << New->getName() 691 << " as hoist destination for " << Orig->getName() 692 << "\n"); 693 return New; 694 }; 695 BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest); 696 BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest); 697 BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc); 698 699 // Link up these blocks with branches. 700 if (!HoistCommonSucc->getTerminator()) { 701 // The new common successor we've generated will branch to whatever that 702 // hoist target branched to. 703 BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor(); 704 assert(TargetSucc && "Expected hoist target to have a single successor"); 705 HoistCommonSucc->moveBefore(TargetSucc); 706 BranchInst::Create(TargetSucc, HoistCommonSucc); 707 } 708 if (!HoistTrueDest->getTerminator()) { 709 HoistTrueDest->moveBefore(HoistCommonSucc); 710 BranchInst::Create(HoistCommonSucc, HoistTrueDest); 711 } 712 if (!HoistFalseDest->getTerminator()) { 713 HoistFalseDest->moveBefore(HoistCommonSucc); 714 BranchInst::Create(HoistCommonSucc, HoistFalseDest); 715 } 716 717 // If BI is being cloned to what was originally the preheader then 718 // HoistCommonSucc will now be the new preheader. 719 if (HoistTarget == InitialPreheader) { 720 // Phis in the loop header now need to use the new preheader. 721 InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc); 722 if (MSSAU) 723 MSSAU->wireOldPredecessorsToNewImmediatePredecessor( 724 HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget}); 725 // The new preheader dominates the loop header. 726 DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc); 727 DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader()); 728 DT->changeImmediateDominator(HeaderNode, PreheaderNode); 729 // The preheader hoist destination is now the new preheader, with the 730 // exception of the hoist destination of this branch. 731 for (auto &Pair : HoistDestinationMap) 732 if (Pair.second == InitialPreheader && Pair.first != BI->getParent()) 733 Pair.second = HoistCommonSucc; 734 } 735 736 // Now finally clone BI. 737 ReplaceInstWithInst( 738 HoistTarget->getTerminator(), 739 BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition())); 740 ++NumClonedBranches; 741 742 assert(CurLoop->getLoopPreheader() && 743 "Hoisting blocks should not have destroyed preheader"); 744 return HoistDestinationMap[BB]; 745 } 746 }; 747 } // namespace 748 749 /// Walk the specified region of the CFG (defined by all blocks dominated by 750 /// the specified block, and that are in the current loop) in depth first 751 /// order w.r.t the DominatorTree. This allows us to visit definitions before 752 /// uses, allowing us to hoist a loop body in one pass without iteration. 753 /// 754 bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI, 755 DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop, 756 AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, 757 ScalarEvolution *SE, ICFLoopSafetyInfo *SafetyInfo, 758 SinkAndHoistLICMFlags &Flags, 759 OptimizationRemarkEmitter *ORE) { 760 // Verify inputs. 761 assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && 762 CurLoop != nullptr && SafetyInfo != nullptr && 763 "Unexpected input to hoistRegion."); 764 assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) && 765 "Either AliasSetTracker or MemorySSA should be initialized."); 766 767 ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU); 768 769 // Keep track of instructions that have been hoisted, as they may need to be 770 // re-hoisted if they end up not dominating all of their uses. 771 SmallVector<Instruction *, 16> HoistedInstructions; 772 773 // For PHI hoisting to work we need to hoist blocks before their successors. 774 // We can do this by iterating through the blocks in the loop in reverse 775 // post-order. 776 LoopBlocksRPO Worklist(CurLoop); 777 Worklist.perform(LI); 778 bool Changed = false; 779 for (BasicBlock *BB : Worklist) { 780 // Only need to process the contents of this block if it is not part of a 781 // subloop (which would already have been processed). 782 if (inSubLoop(BB, CurLoop, LI)) 783 continue; 784 785 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) { 786 Instruction &I = *II++; 787 // Try constant folding this instruction. If all the operands are 788 // constants, it is technically hoistable, but it would be better to 789 // just fold it. 790 if (Constant *C = ConstantFoldInstruction( 791 &I, I.getModule()->getDataLayout(), TLI)) { 792 LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C 793 << '\n'); 794 if (CurAST) 795 CurAST->copyValue(&I, C); 796 // FIXME MSSA: Such replacements may make accesses unoptimized (D51960). 797 I.replaceAllUsesWith(C); 798 if (isInstructionTriviallyDead(&I, TLI)) 799 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); 800 Changed = true; 801 continue; 802 } 803 804 // Try hoisting the instruction out to the preheader. We can only do 805 // this if all of the operands of the instruction are loop invariant and 806 // if it is safe to hoist the instruction. 807 // TODO: It may be safe to hoist if we are hoisting to a conditional block 808 // and we have accurately duplicated the control flow from the loop header 809 // to that block. 810 if (CurLoop->hasLoopInvariantOperands(&I) && 811 canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags, 812 ORE) && 813 isSafeToExecuteUnconditionally( 814 I, DT, CurLoop, SafetyInfo, ORE, 815 CurLoop->getLoopPreheader()->getTerminator())) { 816 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, 817 MSSAU, SE, ORE); 818 HoistedInstructions.push_back(&I); 819 Changed = true; 820 continue; 821 } 822 823 // Attempt to remove floating point division out of the loop by 824 // converting it to a reciprocal multiplication. 825 if (I.getOpcode() == Instruction::FDiv && I.hasAllowReciprocal() && 826 CurLoop->isLoopInvariant(I.getOperand(1))) { 827 auto Divisor = I.getOperand(1); 828 auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0); 829 auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor); 830 ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags()); 831 SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent()); 832 ReciprocalDivisor->insertBefore(&I); 833 834 auto Product = 835 BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor); 836 Product->setFastMathFlags(I.getFastMathFlags()); 837 SafetyInfo->insertInstructionTo(Product, I.getParent()); 838 Product->insertAfter(&I); 839 I.replaceAllUsesWith(Product); 840 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU); 841 842 hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), 843 SafetyInfo, MSSAU, SE, ORE); 844 HoistedInstructions.push_back(ReciprocalDivisor); 845 Changed = true; 846 continue; 847 } 848 849 auto IsInvariantStart = [&](Instruction &I) { 850 using namespace PatternMatch; 851 return I.use_empty() && 852 match(&I, m_Intrinsic<Intrinsic::invariant_start>()); 853 }; 854 auto MustExecuteWithoutWritesBefore = [&](Instruction &I) { 855 return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) && 856 SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop); 857 }; 858 if ((IsInvariantStart(I) || isGuard(&I)) && 859 CurLoop->hasLoopInvariantOperands(&I) && 860 MustExecuteWithoutWritesBefore(I)) { 861 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, 862 MSSAU, SE, ORE); 863 HoistedInstructions.push_back(&I); 864 Changed = true; 865 continue; 866 } 867 868 if (PHINode *PN = dyn_cast<PHINode>(&I)) { 869 if (CFH.canHoistPHI(PN)) { 870 // Redirect incoming blocks first to ensure that we create hoisted 871 // versions of those blocks before we hoist the phi. 872 for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i) 873 PN->setIncomingBlock( 874 i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i))); 875 hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo, 876 MSSAU, SE, ORE); 877 assert(DT->dominates(PN, BB) && "Conditional PHIs not expected"); 878 Changed = true; 879 continue; 880 } 881 } 882 883 // Remember possibly hoistable branches so we can actually hoist them 884 // later if needed. 885 if (BranchInst *BI = dyn_cast<BranchInst>(&I)) 886 CFH.registerPossiblyHoistableBranch(BI); 887 } 888 } 889 890 // If we hoisted instructions to a conditional block they may not dominate 891 // their uses that weren't hoisted (such as phis where some operands are not 892 // loop invariant). If so make them unconditional by moving them to their 893 // immediate dominator. We iterate through the instructions in reverse order 894 // which ensures that when we rehoist an instruction we rehoist its operands, 895 // and also keep track of where in the block we are rehoisting to to make sure 896 // that we rehoist instructions before the instructions that use them. 897 Instruction *HoistPoint = nullptr; 898 if (ControlFlowHoisting) { 899 for (Instruction *I : reverse(HoistedInstructions)) { 900 if (!llvm::all_of(I->uses(), 901 [&](Use &U) { return DT->dominates(I, U); })) { 902 BasicBlock *Dominator = 903 DT->getNode(I->getParent())->getIDom()->getBlock(); 904 if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) { 905 if (HoistPoint) 906 assert(DT->dominates(Dominator, HoistPoint->getParent()) && 907 "New hoist point expected to dominate old hoist point"); 908 HoistPoint = Dominator->getTerminator(); 909 } 910 LLVM_DEBUG(dbgs() << "LICM rehoisting to " 911 << HoistPoint->getParent()->getName() 912 << ": " << *I << "\n"); 913 moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU, SE); 914 HoistPoint = I; 915 Changed = true; 916 } 917 } 918 } 919 if (MSSAU && VerifyMemorySSA) 920 MSSAU->getMemorySSA()->verifyMemorySSA(); 921 922 // Now that we've finished hoisting make sure that LI and DT are still 923 // valid. 924 #ifdef EXPENSIVE_CHECKS 925 if (Changed) { 926 assert(DT->verify(DominatorTree::VerificationLevel::Fast) && 927 "Dominator tree verification failed"); 928 LI->verify(*DT); 929 } 930 #endif 931 932 return Changed; 933 } 934 935 // Return true if LI is invariant within scope of the loop. LI is invariant if 936 // CurLoop is dominated by an invariant.start representing the same memory 937 // location and size as the memory location LI loads from, and also the 938 // invariant.start has no uses. 939 static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT, 940 Loop *CurLoop) { 941 Value *Addr = LI->getOperand(0); 942 const DataLayout &DL = LI->getModule()->getDataLayout(); 943 const uint32_t LocSizeInBits = DL.getTypeSizeInBits(LI->getType()); 944 945 // if the type is i8 addrspace(x)*, we know this is the type of 946 // llvm.invariant.start operand 947 auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()), 948 LI->getPointerAddressSpace()); 949 unsigned BitcastsVisited = 0; 950 // Look through bitcasts until we reach the i8* type (this is invariant.start 951 // operand type). 952 while (Addr->getType() != PtrInt8Ty) { 953 auto *BC = dyn_cast<BitCastInst>(Addr); 954 // Avoid traversing high number of bitcast uses. 955 if (++BitcastsVisited > MaxNumUsesTraversed || !BC) 956 return false; 957 Addr = BC->getOperand(0); 958 } 959 960 unsigned UsesVisited = 0; 961 // Traverse all uses of the load operand value, to see if invariant.start is 962 // one of the uses, and whether it dominates the load instruction. 963 for (auto *U : Addr->users()) { 964 // Avoid traversing for Load operand with high number of users. 965 if (++UsesVisited > MaxNumUsesTraversed) 966 return false; 967 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); 968 // If there are escaping uses of invariant.start instruction, the load maybe 969 // non-invariant. 970 if (!II || II->getIntrinsicID() != Intrinsic::invariant_start || 971 !II->use_empty()) 972 continue; 973 unsigned InvariantSizeInBits = 974 cast<ConstantInt>(II->getArgOperand(0))->getSExtValue() * 8; 975 // Confirm the invariant.start location size contains the load operand size 976 // in bits. Also, the invariant.start should dominate the load, and we 977 // should not hoist the load out of a loop that contains this dominating 978 // invariant.start. 979 if (LocSizeInBits <= InvariantSizeInBits && 980 DT->properlyDominates(II->getParent(), CurLoop->getHeader())) 981 return true; 982 } 983 984 return false; 985 } 986 987 namespace { 988 /// Return true if-and-only-if we know how to (mechanically) both hoist and 989 /// sink a given instruction out of a loop. Does not address legality 990 /// concerns such as aliasing or speculation safety. 991 bool isHoistableAndSinkableInst(Instruction &I) { 992 // Only these instructions are hoistable/sinkable. 993 return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || 994 isa<FenceInst>(I) || isa<CastInst>(I) || isa<UnaryOperator>(I) || 995 isa<BinaryOperator>(I) || isa<SelectInst>(I) || 996 isa<GetElementPtrInst>(I) || isa<CmpInst>(I) || 997 isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) || 998 isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) || 999 isa<InsertValueInst>(I) || isa<FreezeInst>(I)); 1000 } 1001 /// Return true if all of the alias sets within this AST are known not to 1002 /// contain a Mod, or if MSSA knows thare are no MemoryDefs in the loop. 1003 bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU, 1004 const Loop *L) { 1005 if (CurAST) { 1006 for (AliasSet &AS : *CurAST) { 1007 if (!AS.isForwardingAliasSet() && AS.isMod()) { 1008 return false; 1009 } 1010 } 1011 return true; 1012 } else { /*MSSAU*/ 1013 for (auto *BB : L->getBlocks()) 1014 if (MSSAU->getMemorySSA()->getBlockDefs(BB)) 1015 return false; 1016 return true; 1017 } 1018 } 1019 1020 /// Return true if I is the only Instruction with a MemoryAccess in L. 1021 bool isOnlyMemoryAccess(const Instruction *I, const Loop *L, 1022 const MemorySSAUpdater *MSSAU) { 1023 for (auto *BB : L->getBlocks()) 1024 if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) { 1025 int NotAPhi = 0; 1026 for (const auto &Acc : *Accs) { 1027 if (isa<MemoryPhi>(&Acc)) 1028 continue; 1029 const auto *MUD = cast<MemoryUseOrDef>(&Acc); 1030 if (MUD->getMemoryInst() != I || NotAPhi++ == 1) 1031 return false; 1032 } 1033 } 1034 return true; 1035 } 1036 } 1037 1038 bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, 1039 Loop *CurLoop, AliasSetTracker *CurAST, 1040 MemorySSAUpdater *MSSAU, 1041 bool TargetExecutesOncePerLoop, 1042 SinkAndHoistLICMFlags *Flags, 1043 OptimizationRemarkEmitter *ORE) { 1044 // If we don't understand the instruction, bail early. 1045 if (!isHoistableAndSinkableInst(I)) 1046 return false; 1047 1048 MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr; 1049 if (MSSA) 1050 assert(Flags != nullptr && "Flags cannot be null."); 1051 1052 // Loads have extra constraints we have to verify before we can hoist them. 1053 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 1054 if (!LI->isUnordered()) 1055 return false; // Don't sink/hoist volatile or ordered atomic loads! 1056 1057 // Loads from constant memory are always safe to move, even if they end up 1058 // in the same alias set as something that ends up being modified. 1059 if (AA->pointsToConstantMemory(LI->getOperand(0))) 1060 return true; 1061 if (LI->hasMetadata(LLVMContext::MD_invariant_load)) 1062 return true; 1063 1064 if (LI->isAtomic() && !TargetExecutesOncePerLoop) 1065 return false; // Don't risk duplicating unordered loads 1066 1067 // This checks for an invariant.start dominating the load. 1068 if (isLoadInvariantInLoop(LI, DT, CurLoop)) 1069 return true; 1070 1071 bool Invalidated; 1072 if (CurAST) 1073 Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST, 1074 CurLoop, AA); 1075 else 1076 Invalidated = pointerInvalidatedByLoopWithMSSA( 1077 MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, *Flags); 1078 // Check loop-invariant address because this may also be a sinkable load 1079 // whose address is not necessarily loop-invariant. 1080 if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) 1081 ORE->emit([&]() { 1082 return OptimizationRemarkMissed( 1083 DEBUG_TYPE, "LoadWithLoopInvariantAddressInvalidated", LI) 1084 << "failed to move load with loop-invariant address " 1085 "because the loop may invalidate its value"; 1086 }); 1087 1088 return !Invalidated; 1089 } else if (CallInst *CI = dyn_cast<CallInst>(&I)) { 1090 // Don't sink or hoist dbg info; it's legal, but not useful. 1091 if (isa<DbgInfoIntrinsic>(I)) 1092 return false; 1093 1094 // Don't sink calls which can throw. 1095 if (CI->mayThrow()) 1096 return false; 1097 1098 using namespace PatternMatch; 1099 if (match(CI, m_Intrinsic<Intrinsic::assume>())) 1100 // Assumes don't actually alias anything or throw 1101 return true; 1102 1103 if (match(CI, m_Intrinsic<Intrinsic::experimental_widenable_condition>())) 1104 // Widenable conditions don't actually alias anything or throw 1105 return true; 1106 1107 // Handle simple cases by querying alias analysis. 1108 FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI); 1109 if (Behavior == FMRB_DoesNotAccessMemory) 1110 return true; 1111 if (AliasAnalysis::onlyReadsMemory(Behavior)) { 1112 // A readonly argmemonly function only reads from memory pointed to by 1113 // it's arguments with arbitrary offsets. If we can prove there are no 1114 // writes to this memory in the loop, we can hoist or sink. 1115 if (AliasAnalysis::onlyAccessesArgPointees(Behavior)) { 1116 // TODO: expand to writeable arguments 1117 for (Value *Op : CI->arg_operands()) 1118 if (Op->getType()->isPointerTy()) { 1119 bool Invalidated; 1120 if (CurAST) 1121 Invalidated = pointerInvalidatedByLoop( 1122 MemoryLocation(Op, LocationSize::unknown(), AAMDNodes()), 1123 CurAST, CurLoop, AA); 1124 else 1125 Invalidated = pointerInvalidatedByLoopWithMSSA( 1126 MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, 1127 *Flags); 1128 if (Invalidated) 1129 return false; 1130 } 1131 return true; 1132 } 1133 1134 // If this call only reads from memory and there are no writes to memory 1135 // in the loop, we can hoist or sink the call as appropriate. 1136 if (isReadOnly(CurAST, MSSAU, CurLoop)) 1137 return true; 1138 } 1139 1140 // FIXME: This should use mod/ref information to see if we can hoist or 1141 // sink the call. 1142 1143 return false; 1144 } else if (auto *FI = dyn_cast<FenceInst>(&I)) { 1145 // Fences alias (most) everything to provide ordering. For the moment, 1146 // just give up if there are any other memory operations in the loop. 1147 if (CurAST) { 1148 auto Begin = CurAST->begin(); 1149 assert(Begin != CurAST->end() && "must contain FI"); 1150 if (std::next(Begin) != CurAST->end()) 1151 // constant memory for instance, TODO: handle better 1152 return false; 1153 auto *UniqueI = Begin->getUniqueInstruction(); 1154 if (!UniqueI) 1155 // other memory op, give up 1156 return false; 1157 (void)FI; // suppress unused variable warning 1158 assert(UniqueI == FI && "AS must contain FI"); 1159 return true; 1160 } else // MSSAU 1161 return isOnlyMemoryAccess(FI, CurLoop, MSSAU); 1162 } else if (auto *SI = dyn_cast<StoreInst>(&I)) { 1163 if (!SI->isUnordered()) 1164 return false; // Don't sink/hoist volatile or ordered atomic store! 1165 1166 // We can only hoist a store that we can prove writes a value which is not 1167 // read or overwritten within the loop. For those cases, we fallback to 1168 // load store promotion instead. TODO: We can extend this to cases where 1169 // there is exactly one write to the location and that write dominates an 1170 // arbitrary number of reads in the loop. 1171 if (CurAST) { 1172 auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI)); 1173 1174 if (AS.isRef() || !AS.isMustAlias()) 1175 // Quick exit test, handled by the full path below as well. 1176 return false; 1177 auto *UniqueI = AS.getUniqueInstruction(); 1178 if (!UniqueI) 1179 // other memory op, give up 1180 return false; 1181 assert(UniqueI == SI && "AS must contain SI"); 1182 return true; 1183 } else { // MSSAU 1184 if (isOnlyMemoryAccess(SI, CurLoop, MSSAU)) 1185 return true; 1186 // If there are more accesses than the Promotion cap, give up, we're not 1187 // walking a list that long. 1188 if (Flags->NoOfMemAccTooLarge) 1189 return false; 1190 // Check store only if there's still "quota" to check clobber. 1191 if (Flags->LicmMssaOptCounter >= Flags->LicmMssaOptCap) 1192 return false; 1193 // If there are interfering Uses (i.e. their defining access is in the 1194 // loop), or ordered loads (stored as Defs!), don't move this store. 1195 // Could do better here, but this is conservatively correct. 1196 // TODO: Cache set of Uses on the first walk in runOnLoop, update when 1197 // moving accesses. Can also extend to dominating uses. 1198 auto *SIMD = MSSA->getMemoryAccess(SI); 1199 for (auto *BB : CurLoop->getBlocks()) 1200 if (auto *Accesses = MSSA->getBlockAccesses(BB)) { 1201 for (const auto &MA : *Accesses) 1202 if (const auto *MU = dyn_cast<MemoryUse>(&MA)) { 1203 auto *MD = MU->getDefiningAccess(); 1204 if (!MSSA->isLiveOnEntryDef(MD) && 1205 CurLoop->contains(MD->getBlock())) 1206 return false; 1207 // Disable hoisting past potentially interfering loads. Optimized 1208 // Uses may point to an access outside the loop, as getClobbering 1209 // checks the previous iteration when walking the backedge. 1210 // FIXME: More precise: no Uses that alias SI. 1211 if (!Flags->IsSink && !MSSA->dominates(SIMD, MU)) 1212 return false; 1213 } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) { 1214 if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) { 1215 (void)LI; // Silence warning. 1216 assert(!LI->isUnordered() && "Expected unordered load"); 1217 return false; 1218 } 1219 // Any call, while it may not be clobbering SI, it may be a use. 1220 if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) { 1221 // Check if the call may read from the memory locattion written 1222 // to by SI. Check CI's attributes and arguments; the number of 1223 // such checks performed is limited above by NoOfMemAccTooLarge. 1224 ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI)); 1225 if (isModOrRefSet(MRI)) 1226 return false; 1227 } 1228 } 1229 } 1230 1231 auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI); 1232 Flags->LicmMssaOptCounter++; 1233 // If there are no clobbering Defs in the loop, store is safe to hoist. 1234 return MSSA->isLiveOnEntryDef(Source) || 1235 !CurLoop->contains(Source->getBlock()); 1236 } 1237 } 1238 1239 assert(!I.mayReadOrWriteMemory() && "unhandled aliasing"); 1240 1241 // We've established mechanical ability and aliasing, it's up to the caller 1242 // to check fault safety 1243 return true; 1244 } 1245 1246 /// Returns true if a PHINode is a trivially replaceable with an 1247 /// Instruction. 1248 /// This is true when all incoming values are that instruction. 1249 /// This pattern occurs most often with LCSSA PHI nodes. 1250 /// 1251 static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) { 1252 for (const Value *IncValue : PN.incoming_values()) 1253 if (IncValue != &I) 1254 return false; 1255 1256 return true; 1257 } 1258 1259 /// Return true if the instruction is free in the loop. 1260 static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop, 1261 const TargetTransformInfo *TTI) { 1262 1263 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { 1264 if (TTI->getUserCost(GEP) != TargetTransformInfo::TCC_Free) 1265 return false; 1266 // For a GEP, we cannot simply use getUserCost because currently it 1267 // optimistically assume that a GEP will fold into addressing mode 1268 // regardless of its users. 1269 const BasicBlock *BB = GEP->getParent(); 1270 for (const User *U : GEP->users()) { 1271 const Instruction *UI = cast<Instruction>(U); 1272 if (CurLoop->contains(UI) && 1273 (BB != UI->getParent() || 1274 (!isa<StoreInst>(UI) && !isa<LoadInst>(UI)))) 1275 return false; 1276 } 1277 return true; 1278 } else 1279 return TTI->getUserCost(&I) == TargetTransformInfo::TCC_Free; 1280 } 1281 1282 /// Return true if the only users of this instruction are outside of 1283 /// the loop. If this is true, we can sink the instruction to the exit 1284 /// blocks of the loop. 1285 /// 1286 /// We also return true if the instruction could be folded away in lowering. 1287 /// (e.g., a GEP can be folded into a load as an addressing mode in the loop). 1288 static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, 1289 const LoopSafetyInfo *SafetyInfo, 1290 TargetTransformInfo *TTI, bool &FreeInLoop) { 1291 const auto &BlockColors = SafetyInfo->getBlockColors(); 1292 bool IsFree = isFreeInLoop(I, CurLoop, TTI); 1293 for (const User *U : I.users()) { 1294 const Instruction *UI = cast<Instruction>(U); 1295 if (const PHINode *PN = dyn_cast<PHINode>(UI)) { 1296 const BasicBlock *BB = PN->getParent(); 1297 // We cannot sink uses in catchswitches. 1298 if (isa<CatchSwitchInst>(BB->getTerminator())) 1299 return false; 1300 1301 // We need to sink a callsite to a unique funclet. Avoid sinking if the 1302 // phi use is too muddled. 1303 if (isa<CallInst>(I)) 1304 if (!BlockColors.empty() && 1305 BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1) 1306 return false; 1307 } 1308 1309 if (CurLoop->contains(UI)) { 1310 if (IsFree) { 1311 FreeInLoop = true; 1312 continue; 1313 } 1314 return false; 1315 } 1316 } 1317 return true; 1318 } 1319 1320 static Instruction *cloneInstructionInExitBlock( 1321 Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, 1322 const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) { 1323 Instruction *New; 1324 if (auto *CI = dyn_cast<CallInst>(&I)) { 1325 const auto &BlockColors = SafetyInfo->getBlockColors(); 1326 1327 // Sinking call-sites need to be handled differently from other 1328 // instructions. The cloned call-site needs a funclet bundle operand 1329 // appropriate for its location in the CFG. 1330 SmallVector<OperandBundleDef, 1> OpBundles; 1331 for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles(); 1332 BundleIdx != BundleEnd; ++BundleIdx) { 1333 OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx); 1334 if (Bundle.getTagID() == LLVMContext::OB_funclet) 1335 continue; 1336 1337 OpBundles.emplace_back(Bundle); 1338 } 1339 1340 if (!BlockColors.empty()) { 1341 const ColorVector &CV = BlockColors.find(&ExitBlock)->second; 1342 assert(CV.size() == 1 && "non-unique color for exit block!"); 1343 BasicBlock *BBColor = CV.front(); 1344 Instruction *EHPad = BBColor->getFirstNonPHI(); 1345 if (EHPad->isEHPad()) 1346 OpBundles.emplace_back("funclet", EHPad); 1347 } 1348 1349 New = CallInst::Create(CI, OpBundles); 1350 } else { 1351 New = I.clone(); 1352 } 1353 1354 ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New); 1355 if (!I.getName().empty()) 1356 New->setName(I.getName() + ".le"); 1357 1358 if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) { 1359 // Create a new MemoryAccess and let MemorySSA set its defining access. 1360 MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB( 1361 New, nullptr, New->getParent(), MemorySSA::Beginning); 1362 if (NewMemAcc) { 1363 if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc)) 1364 MSSAU->insertDef(MemDef, /*RenameUses=*/true); 1365 else { 1366 auto *MemUse = cast<MemoryUse>(NewMemAcc); 1367 MSSAU->insertUse(MemUse, /*RenameUses=*/true); 1368 } 1369 } 1370 } 1371 1372 // Build LCSSA PHI nodes for any in-loop operands. Note that this is 1373 // particularly cheap because we can rip off the PHI node that we're 1374 // replacing for the number and blocks of the predecessors. 1375 // OPT: If this shows up in a profile, we can instead finish sinking all 1376 // invariant instructions, and then walk their operands to re-establish 1377 // LCSSA. That will eliminate creating PHI nodes just to nuke them when 1378 // sinking bottom-up. 1379 for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE; 1380 ++OI) 1381 if (Instruction *OInst = dyn_cast<Instruction>(*OI)) 1382 if (Loop *OLoop = LI->getLoopFor(OInst->getParent())) 1383 if (!OLoop->contains(&PN)) { 1384 PHINode *OpPN = 1385 PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), 1386 OInst->getName() + ".lcssa", &ExitBlock.front()); 1387 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) 1388 OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); 1389 *OI = OpPN; 1390 } 1391 return New; 1392 } 1393 1394 static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, 1395 AliasSetTracker *AST, MemorySSAUpdater *MSSAU) { 1396 if (AST) 1397 AST->deleteValue(&I); 1398 if (MSSAU) 1399 MSSAU->removeMemoryAccess(&I); 1400 SafetyInfo.removeInstruction(&I); 1401 I.eraseFromParent(); 1402 } 1403 1404 static void moveInstructionBefore(Instruction &I, Instruction &Dest, 1405 ICFLoopSafetyInfo &SafetyInfo, 1406 MemorySSAUpdater *MSSAU, 1407 ScalarEvolution *SE) { 1408 SafetyInfo.removeInstruction(&I); 1409 SafetyInfo.insertInstructionTo(&I, Dest.getParent()); 1410 I.moveBefore(&Dest); 1411 if (MSSAU) 1412 if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>( 1413 MSSAU->getMemorySSA()->getMemoryAccess(&I))) 1414 MSSAU->moveToPlace(OldMemAcc, Dest.getParent(), 1415 MemorySSA::BeforeTerminator); 1416 if (SE) 1417 SE->forgetValue(&I); 1418 } 1419 1420 static Instruction *sinkThroughTriviallyReplaceablePHI( 1421 PHINode *TPN, Instruction *I, LoopInfo *LI, 1422 SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies, 1423 const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, 1424 MemorySSAUpdater *MSSAU) { 1425 assert(isTriviallyReplaceablePHI(*TPN, *I) && 1426 "Expect only trivially replaceable PHI"); 1427 BasicBlock *ExitBlock = TPN->getParent(); 1428 Instruction *New; 1429 auto It = SunkCopies.find(ExitBlock); 1430 if (It != SunkCopies.end()) 1431 New = It->second; 1432 else 1433 New = SunkCopies[ExitBlock] = cloneInstructionInExitBlock( 1434 *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU); 1435 return New; 1436 } 1437 1438 static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) { 1439 BasicBlock *BB = PN->getParent(); 1440 if (!BB->canSplitPredecessors()) 1441 return false; 1442 // It's not impossible to split EHPad blocks, but if BlockColors already exist 1443 // it require updating BlockColors for all offspring blocks accordingly. By 1444 // skipping such corner case, we can make updating BlockColors after splitting 1445 // predecessor fairly simple. 1446 if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad()) 1447 return false; 1448 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 1449 BasicBlock *BBPred = *PI; 1450 if (isa<IndirectBrInst>(BBPred->getTerminator()) || 1451 isa<CallBrInst>(BBPred->getTerminator())) 1452 return false; 1453 } 1454 return true; 1455 } 1456 1457 static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT, 1458 LoopInfo *LI, const Loop *CurLoop, 1459 LoopSafetyInfo *SafetyInfo, 1460 MemorySSAUpdater *MSSAU) { 1461 #ifndef NDEBUG 1462 SmallVector<BasicBlock *, 32> ExitBlocks; 1463 CurLoop->getUniqueExitBlocks(ExitBlocks); 1464 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), 1465 ExitBlocks.end()); 1466 #endif 1467 BasicBlock *ExitBB = PN->getParent(); 1468 assert(ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block."); 1469 1470 // Split predecessors of the loop exit to make instructions in the loop are 1471 // exposed to exit blocks through trivially replaceable PHIs while keeping the 1472 // loop in the canonical form where each predecessor of each exit block should 1473 // be contained within the loop. For example, this will convert the loop below 1474 // from 1475 // 1476 // LB1: 1477 // %v1 = 1478 // br %LE, %LB2 1479 // LB2: 1480 // %v2 = 1481 // br %LE, %LB1 1482 // LE: 1483 // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable 1484 // 1485 // to 1486 // 1487 // LB1: 1488 // %v1 = 1489 // br %LE.split, %LB2 1490 // LB2: 1491 // %v2 = 1492 // br %LE.split2, %LB1 1493 // LE.split: 1494 // %p1 = phi [%v1, %LB1] <-- trivially replaceable 1495 // br %LE 1496 // LE.split2: 1497 // %p2 = phi [%v2, %LB2] <-- trivially replaceable 1498 // br %LE 1499 // LE: 1500 // %p = phi [%p1, %LE.split], [%p2, %LE.split2] 1501 // 1502 const auto &BlockColors = SafetyInfo->getBlockColors(); 1503 SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB)); 1504 while (!PredBBs.empty()) { 1505 BasicBlock *PredBB = *PredBBs.begin(); 1506 assert(CurLoop->contains(PredBB) && 1507 "Expect all predecessors are in the loop"); 1508 if (PN->getBasicBlockIndex(PredBB) >= 0) { 1509 BasicBlock *NewPred = SplitBlockPredecessors( 1510 ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true); 1511 // Since we do not allow splitting EH-block with BlockColors in 1512 // canSplitPredecessors(), we can simply assign predecessor's color to 1513 // the new block. 1514 if (!BlockColors.empty()) 1515 // Grab a reference to the ColorVector to be inserted before getting the 1516 // reference to the vector we are copying because inserting the new 1517 // element in BlockColors might cause the map to be reallocated. 1518 SafetyInfo->copyColors(NewPred, PredBB); 1519 } 1520 PredBBs.remove(PredBB); 1521 } 1522 } 1523 1524 /// When an instruction is found to only be used outside of the loop, this 1525 /// function moves it to the exit blocks and patches up SSA form as needed. 1526 /// This method is guaranteed to remove the original instruction from its 1527 /// position, and may either delete it or move it to outside of the loop. 1528 /// 1529 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT, 1530 const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, 1531 MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE) { 1532 LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n"); 1533 ORE->emit([&]() { 1534 return OptimizationRemark(DEBUG_TYPE, "InstSunk", &I) 1535 << "sinking " << ore::NV("Inst", &I); 1536 }); 1537 bool Changed = false; 1538 if (isa<LoadInst>(I)) 1539 ++NumMovedLoads; 1540 else if (isa<CallInst>(I)) 1541 ++NumMovedCalls; 1542 ++NumSunk; 1543 1544 // Iterate over users to be ready for actual sinking. Replace users via 1545 // unreachable blocks with undef and make all user PHIs trivially replaceable. 1546 SmallPtrSet<Instruction *, 8> VisitedUsers; 1547 for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) { 1548 auto *User = cast<Instruction>(*UI); 1549 Use &U = UI.getUse(); 1550 ++UI; 1551 1552 if (VisitedUsers.count(User) || CurLoop->contains(User)) 1553 continue; 1554 1555 if (!DT->isReachableFromEntry(User->getParent())) { 1556 U = UndefValue::get(I.getType()); 1557 Changed = true; 1558 continue; 1559 } 1560 1561 // The user must be a PHI node. 1562 PHINode *PN = cast<PHINode>(User); 1563 1564 // Surprisingly, instructions can be used outside of loops without any 1565 // exits. This can only happen in PHI nodes if the incoming block is 1566 // unreachable. 1567 BasicBlock *BB = PN->getIncomingBlock(U); 1568 if (!DT->isReachableFromEntry(BB)) { 1569 U = UndefValue::get(I.getType()); 1570 Changed = true; 1571 continue; 1572 } 1573 1574 VisitedUsers.insert(PN); 1575 if (isTriviallyReplaceablePHI(*PN, I)) 1576 continue; 1577 1578 if (!canSplitPredecessors(PN, SafetyInfo)) 1579 return Changed; 1580 1581 // Split predecessors of the PHI so that we can make users trivially 1582 // replaceable. 1583 splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU); 1584 1585 // Should rebuild the iterators, as they may be invalidated by 1586 // splitPredecessorsOfLoopExit(). 1587 UI = I.user_begin(); 1588 UE = I.user_end(); 1589 } 1590 1591 if (VisitedUsers.empty()) 1592 return Changed; 1593 1594 #ifndef NDEBUG 1595 SmallVector<BasicBlock *, 32> ExitBlocks; 1596 CurLoop->getUniqueExitBlocks(ExitBlocks); 1597 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), 1598 ExitBlocks.end()); 1599 #endif 1600 1601 // Clones of this instruction. Don't create more than one per exit block! 1602 SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies; 1603 1604 // If this instruction is only used outside of the loop, then all users are 1605 // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of 1606 // the instruction. 1607 SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end()); 1608 for (auto *UI : Users) { 1609 auto *User = cast<Instruction>(UI); 1610 1611 if (CurLoop->contains(User)) 1612 continue; 1613 1614 PHINode *PN = cast<PHINode>(User); 1615 assert(ExitBlockSet.count(PN->getParent()) && 1616 "The LCSSA PHI is not in an exit block!"); 1617 // The PHI must be trivially replaceable. 1618 Instruction *New = sinkThroughTriviallyReplaceablePHI( 1619 PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU); 1620 PN->replaceAllUsesWith(New); 1621 eraseInstruction(*PN, *SafetyInfo, nullptr, nullptr); 1622 Changed = true; 1623 } 1624 return Changed; 1625 } 1626 1627 /// When an instruction is found to only use loop invariant operands that 1628 /// is safe to hoist, this instruction is called to do the dirty work. 1629 /// 1630 static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, 1631 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, 1632 MemorySSAUpdater *MSSAU, ScalarEvolution *SE, 1633 OptimizationRemarkEmitter *ORE) { 1634 LLVM_DEBUG(dbgs() << "LICM hoisting to " << Dest->getName() << ": " << I 1635 << "\n"); 1636 ORE->emit([&]() { 1637 return OptimizationRemark(DEBUG_TYPE, "Hoisted", &I) << "hoisting " 1638 << ore::NV("Inst", &I); 1639 }); 1640 1641 // Metadata can be dependent on conditions we are hoisting above. 1642 // Conservatively strip all metadata on the instruction unless we were 1643 // guaranteed to execute I if we entered the loop, in which case the metadata 1644 // is valid in the loop preheader. 1645 if (I.hasMetadataOtherThanDebugLoc() && 1646 // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning 1647 // time in isGuaranteedToExecute if we don't actually have anything to 1648 // drop. It is a compile time optimization, not required for correctness. 1649 !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) 1650 I.dropUnknownNonDebugMetadata(); 1651 1652 if (isa<PHINode>(I)) 1653 // Move the new node to the end of the phi list in the destination block. 1654 moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU, SE); 1655 else 1656 // Move the new node to the destination block, before its terminator. 1657 moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE); 1658 1659 // Apply line 0 debug locations when we are moving instructions to different 1660 // basic blocks because we want to avoid jumpy line tables. 1661 if (const DebugLoc &DL = I.getDebugLoc()) 1662 I.setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt())); 1663 1664 if (isa<LoadInst>(I)) 1665 ++NumMovedLoads; 1666 else if (isa<CallInst>(I)) 1667 ++NumMovedCalls; 1668 ++NumHoisted; 1669 } 1670 1671 /// Only sink or hoist an instruction if it is not a trapping instruction, 1672 /// or if the instruction is known not to trap when moved to the preheader. 1673 /// or if it is a trapping instruction and is guaranteed to execute. 1674 static bool isSafeToExecuteUnconditionally(Instruction &Inst, 1675 const DominatorTree *DT, 1676 const Loop *CurLoop, 1677 const LoopSafetyInfo *SafetyInfo, 1678 OptimizationRemarkEmitter *ORE, 1679 const Instruction *CtxI) { 1680 if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT)) 1681 return true; 1682 1683 bool GuaranteedToExecute = 1684 SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop); 1685 1686 if (!GuaranteedToExecute) { 1687 auto *LI = dyn_cast<LoadInst>(&Inst); 1688 if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand())) 1689 ORE->emit([&]() { 1690 return OptimizationRemarkMissed( 1691 DEBUG_TYPE, "LoadWithLoopInvariantAddressCondExecuted", LI) 1692 << "failed to hoist load with loop-invariant address " 1693 "because load is conditionally executed"; 1694 }); 1695 } 1696 1697 return GuaranteedToExecute; 1698 } 1699 1700 namespace { 1701 class LoopPromoter : public LoadAndStorePromoter { 1702 Value *SomePtr; // Designated pointer to store to. 1703 const SmallSetVector<Value *, 8> &PointerMustAliases; 1704 SmallVectorImpl<BasicBlock *> &LoopExitBlocks; 1705 SmallVectorImpl<Instruction *> &LoopInsertPts; 1706 SmallVectorImpl<MemoryAccess *> &MSSAInsertPts; 1707 PredIteratorCache &PredCache; 1708 AliasSetTracker &AST; 1709 MemorySSAUpdater *MSSAU; 1710 LoopInfo &LI; 1711 DebugLoc DL; 1712 int Alignment; 1713 bool UnorderedAtomic; 1714 AAMDNodes AATags; 1715 ICFLoopSafetyInfo &SafetyInfo; 1716 1717 Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const { 1718 if (Instruction *I = dyn_cast<Instruction>(V)) 1719 if (Loop *L = LI.getLoopFor(I->getParent())) 1720 if (!L->contains(BB)) { 1721 // We need to create an LCSSA PHI node for the incoming value and 1722 // store that. 1723 PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), 1724 I->getName() + ".lcssa", &BB->front()); 1725 for (BasicBlock *Pred : PredCache.get(BB)) 1726 PN->addIncoming(I, Pred); 1727 return PN; 1728 } 1729 return V; 1730 } 1731 1732 public: 1733 LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S, 1734 const SmallSetVector<Value *, 8> &PMA, 1735 SmallVectorImpl<BasicBlock *> &LEB, 1736 SmallVectorImpl<Instruction *> &LIP, 1737 SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC, 1738 AliasSetTracker &ast, MemorySSAUpdater *MSSAU, LoopInfo &li, 1739 DebugLoc dl, int alignment, bool UnorderedAtomic, 1740 const AAMDNodes &AATags, ICFLoopSafetyInfo &SafetyInfo) 1741 : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA), 1742 LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP), 1743 PredCache(PIC), AST(ast), MSSAU(MSSAU), LI(li), DL(std::move(dl)), 1744 Alignment(alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags), 1745 SafetyInfo(SafetyInfo) {} 1746 1747 bool isInstInList(Instruction *I, 1748 const SmallVectorImpl<Instruction *> &) const override { 1749 Value *Ptr; 1750 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 1751 Ptr = LI->getOperand(0); 1752 else 1753 Ptr = cast<StoreInst>(I)->getPointerOperand(); 1754 return PointerMustAliases.count(Ptr); 1755 } 1756 1757 void doExtraRewritesBeforeFinalDeletion() override { 1758 // Insert stores after in the loop exit blocks. Each exit block gets a 1759 // store of the live-out values that feed them. Since we've already told 1760 // the SSA updater about the defs in the loop and the preheader 1761 // definition, it is all set and we can start using it. 1762 for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) { 1763 BasicBlock *ExitBlock = LoopExitBlocks[i]; 1764 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); 1765 LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock); 1766 Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock); 1767 Instruction *InsertPos = LoopInsertPts[i]; 1768 StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos); 1769 if (UnorderedAtomic) 1770 NewSI->setOrdering(AtomicOrdering::Unordered); 1771 NewSI->setAlignment(Align(Alignment)); 1772 NewSI->setDebugLoc(DL); 1773 if (AATags) 1774 NewSI->setAAMetadata(AATags); 1775 1776 if (MSSAU) { 1777 MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i]; 1778 MemoryAccess *NewMemAcc; 1779 if (!MSSAInsertPoint) { 1780 NewMemAcc = MSSAU->createMemoryAccessInBB( 1781 NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning); 1782 } else { 1783 NewMemAcc = 1784 MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint); 1785 } 1786 MSSAInsertPts[i] = NewMemAcc; 1787 MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true); 1788 // FIXME: true for safety, false may still be correct. 1789 } 1790 } 1791 } 1792 1793 void replaceLoadWithValue(LoadInst *LI, Value *V) const override { 1794 // Update alias analysis. 1795 AST.copyValue(LI, V); 1796 } 1797 void instructionDeleted(Instruction *I) const override { 1798 SafetyInfo.removeInstruction(I); 1799 AST.deleteValue(I); 1800 if (MSSAU) 1801 MSSAU->removeMemoryAccess(I); 1802 } 1803 }; 1804 1805 1806 /// Return true iff we can prove that a caller of this function can not inspect 1807 /// the contents of the provided object in a well defined program. 1808 bool isKnownNonEscaping(Value *Object, const TargetLibraryInfo *TLI) { 1809 if (isa<AllocaInst>(Object)) 1810 // Since the alloca goes out of scope, we know the caller can't retain a 1811 // reference to it and be well defined. Thus, we don't need to check for 1812 // capture. 1813 return true; 1814 1815 // For all other objects we need to know that the caller can't possibly 1816 // have gotten a reference to the object. There are two components of 1817 // that: 1818 // 1) Object can't be escaped by this function. This is what 1819 // PointerMayBeCaptured checks. 1820 // 2) Object can't have been captured at definition site. For this, we 1821 // need to know the return value is noalias. At the moment, we use a 1822 // weaker condition and handle only AllocLikeFunctions (which are 1823 // known to be noalias). TODO 1824 return isAllocLikeFn(Object, TLI) && 1825 !PointerMayBeCaptured(Object, true, true); 1826 } 1827 1828 } // namespace 1829 1830 /// Try to promote memory values to scalars by sinking stores out of the 1831 /// loop and moving loads to before the loop. We do this by looping over 1832 /// the stores in the loop, looking for stores to Must pointers which are 1833 /// loop invariant. 1834 /// 1835 bool llvm::promoteLoopAccessesToScalars( 1836 const SmallSetVector<Value *, 8> &PointerMustAliases, 1837 SmallVectorImpl<BasicBlock *> &ExitBlocks, 1838 SmallVectorImpl<Instruction *> &InsertPts, 1839 SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC, 1840 LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, 1841 Loop *CurLoop, AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU, 1842 ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) { 1843 // Verify inputs. 1844 assert(LI != nullptr && DT != nullptr && CurLoop != nullptr && 1845 CurAST != nullptr && SafetyInfo != nullptr && 1846 "Unexpected Input to promoteLoopAccessesToScalars"); 1847 1848 Value *SomePtr = *PointerMustAliases.begin(); 1849 BasicBlock *Preheader = CurLoop->getLoopPreheader(); 1850 1851 // It is not safe to promote a load/store from the loop if the load/store is 1852 // conditional. For example, turning: 1853 // 1854 // for () { if (c) *P += 1; } 1855 // 1856 // into: 1857 // 1858 // tmp = *P; for () { if (c) tmp +=1; } *P = tmp; 1859 // 1860 // is not safe, because *P may only be valid to access if 'c' is true. 1861 // 1862 // The safety property divides into two parts: 1863 // p1) The memory may not be dereferenceable on entry to the loop. In this 1864 // case, we can't insert the required load in the preheader. 1865 // p2) The memory model does not allow us to insert a store along any dynamic 1866 // path which did not originally have one. 1867 // 1868 // If at least one store is guaranteed to execute, both properties are 1869 // satisfied, and promotion is legal. 1870 // 1871 // This, however, is not a necessary condition. Even if no store/load is 1872 // guaranteed to execute, we can still establish these properties. 1873 // We can establish (p1) by proving that hoisting the load into the preheader 1874 // is safe (i.e. proving dereferenceability on all paths through the loop). We 1875 // can use any access within the alias set to prove dereferenceability, 1876 // since they're all must alias. 1877 // 1878 // There are two ways establish (p2): 1879 // a) Prove the location is thread-local. In this case the memory model 1880 // requirement does not apply, and stores are safe to insert. 1881 // b) Prove a store dominates every exit block. In this case, if an exit 1882 // blocks is reached, the original dynamic path would have taken us through 1883 // the store, so inserting a store into the exit block is safe. Note that this 1884 // is different from the store being guaranteed to execute. For instance, 1885 // if an exception is thrown on the first iteration of the loop, the original 1886 // store is never executed, but the exit blocks are not executed either. 1887 1888 bool DereferenceableInPH = false; 1889 bool SafeToInsertStore = false; 1890 1891 SmallVector<Instruction *, 64> LoopUses; 1892 1893 // We start with an alignment of one and try to find instructions that allow 1894 // us to prove better alignment. 1895 unsigned Alignment = 1; 1896 // Keep track of which types of access we see 1897 bool SawUnorderedAtomic = false; 1898 bool SawNotAtomic = false; 1899 AAMDNodes AATags; 1900 1901 const DataLayout &MDL = Preheader->getModule()->getDataLayout(); 1902 1903 bool IsKnownThreadLocalObject = false; 1904 if (SafetyInfo->anyBlockMayThrow()) { 1905 // If a loop can throw, we have to insert a store along each unwind edge. 1906 // That said, we can't actually make the unwind edge explicit. Therefore, 1907 // we have to prove that the store is dead along the unwind edge. We do 1908 // this by proving that the caller can't have a reference to the object 1909 // after return and thus can't possibly load from the object. 1910 Value *Object = GetUnderlyingObject(SomePtr, MDL); 1911 if (!isKnownNonEscaping(Object, TLI)) 1912 return false; 1913 // Subtlety: Alloca's aren't visible to callers, but *are* potentially 1914 // visible to other threads if captured and used during their lifetimes. 1915 IsKnownThreadLocalObject = !isa<AllocaInst>(Object); 1916 } 1917 1918 // Check that all of the pointers in the alias set have the same type. We 1919 // cannot (yet) promote a memory location that is loaded and stored in 1920 // different sizes. While we are at it, collect alignment and AA info. 1921 for (Value *ASIV : PointerMustAliases) { 1922 // Check that all of the pointers in the alias set have the same type. We 1923 // cannot (yet) promote a memory location that is loaded and stored in 1924 // different sizes. 1925 if (SomePtr->getType() != ASIV->getType()) 1926 return false; 1927 1928 for (User *U : ASIV->users()) { 1929 // Ignore instructions that are outside the loop. 1930 Instruction *UI = dyn_cast<Instruction>(U); 1931 if (!UI || !CurLoop->contains(UI)) 1932 continue; 1933 1934 // If there is an non-load/store instruction in the loop, we can't promote 1935 // it. 1936 if (LoadInst *Load = dyn_cast<LoadInst>(UI)) { 1937 if (!Load->isUnordered()) 1938 return false; 1939 1940 SawUnorderedAtomic |= Load->isAtomic(); 1941 SawNotAtomic |= !Load->isAtomic(); 1942 1943 unsigned InstAlignment = Load->getAlignment(); 1944 if (!InstAlignment) 1945 InstAlignment = 1946 MDL.getABITypeAlignment(Load->getType()); 1947 1948 // Note that proving a load safe to speculate requires proving 1949 // sufficient alignment at the target location. Proving it guaranteed 1950 // to execute does as well. Thus we can increase our guaranteed 1951 // alignment as well. 1952 if (!DereferenceableInPH || (InstAlignment > Alignment)) 1953 if (isSafeToExecuteUnconditionally(*Load, DT, CurLoop, SafetyInfo, 1954 ORE, Preheader->getTerminator())) { 1955 DereferenceableInPH = true; 1956 Alignment = std::max(Alignment, InstAlignment); 1957 } 1958 } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) { 1959 // Stores *of* the pointer are not interesting, only stores *to* the 1960 // pointer. 1961 if (UI->getOperand(1) != ASIV) 1962 continue; 1963 if (!Store->isUnordered()) 1964 return false; 1965 1966 SawUnorderedAtomic |= Store->isAtomic(); 1967 SawNotAtomic |= !Store->isAtomic(); 1968 1969 // If the store is guaranteed to execute, both properties are satisfied. 1970 // We may want to check if a store is guaranteed to execute even if we 1971 // already know that promotion is safe, since it may have higher 1972 // alignment than any other guaranteed stores, in which case we can 1973 // raise the alignment on the promoted store. 1974 unsigned InstAlignment = Store->getAlignment(); 1975 if (!InstAlignment) 1976 InstAlignment = 1977 MDL.getABITypeAlignment(Store->getValueOperand()->getType()); 1978 1979 if (!DereferenceableInPH || !SafeToInsertStore || 1980 (InstAlignment > Alignment)) { 1981 if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) { 1982 DereferenceableInPH = true; 1983 SafeToInsertStore = true; 1984 Alignment = std::max(Alignment, InstAlignment); 1985 } 1986 } 1987 1988 // If a store dominates all exit blocks, it is safe to sink. 1989 // As explained above, if an exit block was executed, a dominating 1990 // store must have been executed at least once, so we are not 1991 // introducing stores on paths that did not have them. 1992 // Note that this only looks at explicit exit blocks. If we ever 1993 // start sinking stores into unwind edges (see above), this will break. 1994 if (!SafeToInsertStore) 1995 SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) { 1996 return DT->dominates(Store->getParent(), Exit); 1997 }); 1998 1999 // If the store is not guaranteed to execute, we may still get 2000 // deref info through it. 2001 if (!DereferenceableInPH) { 2002 DereferenceableInPH = isDereferenceableAndAlignedPointer( 2003 Store->getPointerOperand(), Store->getValueOperand()->getType(), 2004 Store->getAlign(), MDL, Preheader->getTerminator(), DT); 2005 } 2006 } else 2007 return false; // Not a load or store. 2008 2009 // Merge the AA tags. 2010 if (LoopUses.empty()) { 2011 // On the first load/store, just take its AA tags. 2012 UI->getAAMetadata(AATags); 2013 } else if (AATags) { 2014 UI->getAAMetadata(AATags, /* Merge = */ true); 2015 } 2016 2017 LoopUses.push_back(UI); 2018 } 2019 } 2020 2021 // If we found both an unordered atomic instruction and a non-atomic memory 2022 // access, bail. We can't blindly promote non-atomic to atomic since we 2023 // might not be able to lower the result. We can't downgrade since that 2024 // would violate memory model. Also, align 0 is an error for atomics. 2025 if (SawUnorderedAtomic && SawNotAtomic) 2026 return false; 2027 2028 // If we're inserting an atomic load in the preheader, we must be able to 2029 // lower it. We're only guaranteed to be able to lower naturally aligned 2030 // atomics. 2031 auto *SomePtrElemType = SomePtr->getType()->getPointerElementType(); 2032 if (SawUnorderedAtomic && 2033 Alignment < MDL.getTypeStoreSize(SomePtrElemType)) 2034 return false; 2035 2036 // If we couldn't prove we can hoist the load, bail. 2037 if (!DereferenceableInPH) 2038 return false; 2039 2040 // We know we can hoist the load, but don't have a guaranteed store. 2041 // Check whether the location is thread-local. If it is, then we can insert 2042 // stores along paths which originally didn't have them without violating the 2043 // memory model. 2044 if (!SafeToInsertStore) { 2045 if (IsKnownThreadLocalObject) 2046 SafeToInsertStore = true; 2047 else { 2048 Value *Object = GetUnderlyingObject(SomePtr, MDL); 2049 SafeToInsertStore = 2050 (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) && 2051 !PointerMayBeCaptured(Object, true, true); 2052 } 2053 } 2054 2055 // If we've still failed to prove we can sink the store, give up. 2056 if (!SafeToInsertStore) 2057 return false; 2058 2059 // Otherwise, this is safe to promote, lets do it! 2060 LLVM_DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr 2061 << '\n'); 2062 ORE->emit([&]() { 2063 return OptimizationRemark(DEBUG_TYPE, "PromoteLoopAccessesToScalar", 2064 LoopUses[0]) 2065 << "Moving accesses to memory location out of the loop"; 2066 }); 2067 ++NumPromoted; 2068 2069 // Look at all the loop uses, and try to merge their locations. 2070 std::vector<const DILocation *> LoopUsesLocs; 2071 for (auto U : LoopUses) 2072 LoopUsesLocs.push_back(U->getDebugLoc().get()); 2073 auto DL = DebugLoc(DILocation::getMergedLocations(LoopUsesLocs)); 2074 2075 // We use the SSAUpdater interface to insert phi nodes as required. 2076 SmallVector<PHINode *, 16> NewPHIs; 2077 SSAUpdater SSA(&NewPHIs); 2078 LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks, 2079 InsertPts, MSSAInsertPts, PIC, *CurAST, MSSAU, *LI, DL, 2080 Alignment, SawUnorderedAtomic, AATags, *SafetyInfo); 2081 2082 // Set up the preheader to have a definition of the value. It is the live-out 2083 // value from the preheader that uses in the loop will use. 2084 LoadInst *PreheaderLoad = new LoadInst( 2085 SomePtr->getType()->getPointerElementType(), SomePtr, 2086 SomePtr->getName() + ".promoted", Preheader->getTerminator()); 2087 if (SawUnorderedAtomic) 2088 PreheaderLoad->setOrdering(AtomicOrdering::Unordered); 2089 PreheaderLoad->setAlignment(Align(Alignment)); 2090 PreheaderLoad->setDebugLoc(DL); 2091 if (AATags) 2092 PreheaderLoad->setAAMetadata(AATags); 2093 SSA.AddAvailableValue(Preheader, PreheaderLoad); 2094 2095 if (MSSAU) { 2096 MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( 2097 PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); 2098 MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess); 2099 MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); 2100 } 2101 2102 if (MSSAU && VerifyMemorySSA) 2103 MSSAU->getMemorySSA()->verifyMemorySSA(); 2104 // Rewrite all the loads in the loop and remember all the definitions from 2105 // stores in the loop. 2106 Promoter.run(LoopUses); 2107 2108 if (MSSAU && VerifyMemorySSA) 2109 MSSAU->getMemorySSA()->verifyMemorySSA(); 2110 // If the SSAUpdater didn't use the load in the preheader, just zap it now. 2111 if (PreheaderLoad->use_empty()) 2112 eraseInstruction(*PreheaderLoad, *SafetyInfo, CurAST, MSSAU); 2113 2114 return true; 2115 } 2116 2117 /// Returns an owning pointer to an alias set which incorporates aliasing info 2118 /// from L and all subloops of L. 2119 std::unique_ptr<AliasSetTracker> 2120 LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI, 2121 AliasAnalysis *AA) { 2122 auto CurAST = std::make_unique<AliasSetTracker>(*AA); 2123 2124 // Add everything from all the sub loops. 2125 for (Loop *InnerL : L->getSubLoops()) 2126 for (BasicBlock *BB : InnerL->blocks()) 2127 CurAST->add(*BB); 2128 2129 // And merge in this loop (without anything from inner loops). 2130 for (BasicBlock *BB : L->blocks()) 2131 if (LI->getLoopFor(BB) == L) 2132 CurAST->add(*BB); 2133 2134 return CurAST; 2135 } 2136 2137 std::unique_ptr<AliasSetTracker> 2138 LoopInvariantCodeMotion::collectAliasInfoForLoopWithMSSA( 2139 Loop *L, AliasAnalysis *AA, MemorySSAUpdater *MSSAU) { 2140 auto *MSSA = MSSAU->getMemorySSA(); 2141 auto CurAST = std::make_unique<AliasSetTracker>(*AA, MSSA, L); 2142 CurAST->addAllInstructionsInLoopUsingMSSA(); 2143 return CurAST; 2144 } 2145 2146 static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, 2147 AliasSetTracker *CurAST, Loop *CurLoop, 2148 AliasAnalysis *AA) { 2149 // First check to see if any of the basic blocks in CurLoop invalidate *V. 2150 bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod(); 2151 2152 if (!isInvalidatedAccordingToAST || !LICMN2Theshold) 2153 return isInvalidatedAccordingToAST; 2154 2155 // Check with a diagnostic analysis if we can refine the information above. 2156 // This is to identify the limitations of using the AST. 2157 // The alias set mechanism used by LICM has a major weakness in that it 2158 // combines all things which may alias into a single set *before* asking 2159 // modref questions. As a result, a single readonly call within a loop will 2160 // collapse all loads and stores into a single alias set and report 2161 // invalidation if the loop contains any store. For example, readonly calls 2162 // with deopt states have this form and create a general alias set with all 2163 // loads and stores. In order to get any LICM in loops containing possible 2164 // deopt states we need a more precise invalidation of checking the mod ref 2165 // info of each instruction within the loop and LI. This has a complexity of 2166 // O(N^2), so currently, it is used only as a diagnostic tool since the 2167 // default value of LICMN2Threshold is zero. 2168 2169 // Don't look at nested loops. 2170 if (CurLoop->begin() != CurLoop->end()) 2171 return true; 2172 2173 int N = 0; 2174 for (BasicBlock *BB : CurLoop->getBlocks()) 2175 for (Instruction &I : *BB) { 2176 if (N >= LICMN2Theshold) { 2177 LLVM_DEBUG(dbgs() << "Alasing N2 threshold exhausted for " 2178 << *(MemLoc.Ptr) << "\n"); 2179 return true; 2180 } 2181 N++; 2182 auto Res = AA->getModRefInfo(&I, MemLoc); 2183 if (isModSet(Res)) { 2184 LLVM_DEBUG(dbgs() << "Aliasing failed on " << I << " for " 2185 << *(MemLoc.Ptr) << "\n"); 2186 return true; 2187 } 2188 } 2189 LLVM_DEBUG(dbgs() << "Aliasing okay for " << *(MemLoc.Ptr) << "\n"); 2190 return false; 2191 } 2192 2193 static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU, 2194 Loop *CurLoop, 2195 SinkAndHoistLICMFlags &Flags) { 2196 // For hoisting, use the walker to determine safety 2197 if (!Flags.IsSink) { 2198 MemoryAccess *Source; 2199 // See declaration of SetLicmMssaOptCap for usage details. 2200 if (Flags.LicmMssaOptCounter >= Flags.LicmMssaOptCap) 2201 Source = MU->getDefiningAccess(); 2202 else { 2203 Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU); 2204 Flags.LicmMssaOptCounter++; 2205 } 2206 return !MSSA->isLiveOnEntryDef(Source) && 2207 CurLoop->contains(Source->getBlock()); 2208 } 2209 2210 // For sinking, we'd need to check all Defs below this use. The getClobbering 2211 // call will look on the backedge of the loop, but will check aliasing with 2212 // the instructions on the previous iteration. 2213 // For example: 2214 // for (i ... ) 2215 // load a[i] ( Use (LoE) 2216 // store a[i] ( 1 = Def (2), with 2 = Phi for the loop. 2217 // i++; 2218 // The load sees no clobbering inside the loop, as the backedge alias check 2219 // does phi translation, and will check aliasing against store a[i-1]. 2220 // However sinking the load outside the loop, below the store is incorrect. 2221 2222 // For now, only sink if there are no Defs in the loop, and the existing ones 2223 // precede the use and are in the same block. 2224 // FIXME: Increase precision: Safe to sink if Use post dominates the Def; 2225 // needs PostDominatorTreeAnalysis. 2226 // FIXME: More precise: no Defs that alias this Use. 2227 if (Flags.NoOfMemAccTooLarge) 2228 return true; 2229 for (auto *BB : CurLoop->getBlocks()) 2230 if (auto *Accesses = MSSA->getBlockDefs(BB)) 2231 for (const auto &MA : *Accesses) 2232 if (const auto *MD = dyn_cast<MemoryDef>(&MA)) 2233 if (MU->getBlock() != MD->getBlock() || 2234 !MSSA->locallyDominates(MD, MU)) 2235 return true; 2236 return false; 2237 } 2238 2239 /// Little predicate that returns true if the specified basic block is in 2240 /// a subloop of the current one, not the current one itself. 2241 /// 2242 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) { 2243 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); 2244 return LI->getLoopFor(BB) != CurLoop; 2245 } 2246