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