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