1 //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass performs loop invariant code motion, attempting to remove as much
11 // code from the body of a loop as possible.  It does this by either hoisting
12 // code into the preheader block, or by sinking code to the exit blocks if it is
13 // safe.  This pass also promotes must-aliased memory locations in the loop to
14 // live in registers, thus hoisting and sinking "invariant" loads and stores.
15 //
16 // This pass uses alias analysis for two purposes:
17 //
18 //  1. Moving loop invariant loads and calls out of loops.  If we can determine
19 //     that a load or call inside of a loop never aliases anything stored to,
20 //     we can hoist it or sink it like any other instruction.
21 //  2. Scalar Promotion of Memory - If there is a store instruction inside of
22 //     the loop, we try to move the store to happen AFTER the loop instead of
23 //     inside of the loop.  This can only happen if a few conditions are true:
24 //       A. The pointer stored through is loop invariant
25 //       B. There are no stores or loads in the loop which _may_ alias the
26 //          pointer.  There are no calls in the loop which mod/ref the pointer.
27 //     If these conditions are true, we can promote the loads and stores in the
28 //     loop of the pointer to use a temporary alloca'd variable.  We then use
29 //     the SSAUpdater to construct the appropriate SSA form for the value.
30 //
31 //===----------------------------------------------------------------------===//
32 
33 #include "llvm/Transforms/Scalar/LICM.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Analysis/AliasAnalysis.h"
36 #include "llvm/Analysis/AliasSetTracker.h"
37 #include "llvm/Analysis/BasicAliasAnalysis.h"
38 #include "llvm/Analysis/CaptureTracking.h"
39 #include "llvm/Analysis/ConstantFolding.h"
40 #include "llvm/Analysis/GlobalsModRef.h"
41 #include "llvm/Analysis/Loads.h"
42 #include "llvm/Analysis/LoopInfo.h"
43 #include "llvm/Analysis/LoopPass.h"
44 #include "llvm/Analysis/MemoryBuiltins.h"
45 #include "llvm/Analysis/MemorySSA.h"
46 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
47 #include "llvm/Analysis/ScalarEvolution.h"
48 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
49 #include "llvm/Analysis/TargetLibraryInfo.h"
50 #include "llvm/Analysis/ValueTracking.h"
51 #include "llvm/IR/CFG.h"
52 #include "llvm/IR/Constants.h"
53 #include "llvm/IR/DataLayout.h"
54 #include "llvm/IR/DerivedTypes.h"
55 #include "llvm/IR/Dominators.h"
56 #include "llvm/IR/Instructions.h"
57 #include "llvm/IR/IntrinsicInst.h"
58 #include "llvm/IR/LLVMContext.h"
59 #include "llvm/IR/Metadata.h"
60 #include "llvm/IR/PredIteratorCache.h"
61 #include "llvm/Support/CommandLine.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/Support/raw_ostream.h"
64 #include "llvm/Transforms/Scalar.h"
65 #include "llvm/Transforms/Scalar/LoopPassManager.h"
66 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
67 #include "llvm/Transforms/Utils/Local.h"
68 #include "llvm/Transforms/Utils/LoopUtils.h"
69 #include "llvm/Transforms/Utils/SSAUpdater.h"
70 #include <algorithm>
71 #include <utility>
72 using namespace llvm;
73 
74 #define DEBUG_TYPE "licm"
75 
76 STATISTIC(NumSunk, "Number of instructions sunk out of loop");
77 STATISTIC(NumHoisted, "Number of instructions hoisted out of loop");
78 STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
79 STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
80 STATISTIC(NumPromoted, "Number of memory locations promoted to registers");
81 
82 /// Memory promotion is enabled by default.
83 static cl::opt<bool>
84     DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false),
85                      cl::desc("Disable memory promotion in LICM pass"));
86 
87 static cl::opt<uint32_t> MaxNumUsesTraversed(
88     "licm-max-num-uses-traversed", cl::Hidden, cl::init(8),
89     cl::desc("Max num uses visited for identifying load "
90              "invariance in loop using invariant start (default = 8)"));
91 
92 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
93 static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
94                                   const LoopSafetyInfo *SafetyInfo,
95                                   TargetTransformInfo *TTI, bool &FreeInLoop);
96 static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
97                   const LoopSafetyInfo *SafetyInfo,
98                   OptimizationRemarkEmitter *ORE);
99 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
100                  const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
101                  OptimizationRemarkEmitter *ORE, bool FreeInLoop);
102 static bool isSafeToExecuteUnconditionally(Instruction &Inst,
103                                            const DominatorTree *DT,
104                                            const Loop *CurLoop,
105                                            const LoopSafetyInfo *SafetyInfo,
106                                            OptimizationRemarkEmitter *ORE,
107                                            const Instruction *CtxI = nullptr);
108 static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
109                                      const AAMDNodes &AAInfo,
110                                      AliasSetTracker *CurAST);
111 static Instruction *
112 CloneInstructionInExitBlock(Instruction &I, BasicBlock &ExitBlock, PHINode &PN,
113                             const LoopInfo *LI,
114                             const LoopSafetyInfo *SafetyInfo);
115 
116 namespace {
117 struct LoopInvariantCodeMotion {
118   bool runOnLoop(Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT,
119                  TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
120                  ScalarEvolution *SE, MemorySSA *MSSA,
121                  OptimizationRemarkEmitter *ORE, bool DeleteAST);
122 
123   DenseMap<Loop *, AliasSetTracker *> &getLoopToAliasSetMap() {
124     return LoopToAliasSetMap;
125   }
126 
127 private:
128   DenseMap<Loop *, AliasSetTracker *> LoopToAliasSetMap;
129 
130   AliasSetTracker *collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
131                                            AliasAnalysis *AA);
132 };
133 
134 struct LegacyLICMPass : public LoopPass {
135   static char ID; // Pass identification, replacement for typeid
136   LegacyLICMPass() : LoopPass(ID) {
137     initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry());
138   }
139 
140   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
141     if (skipLoop(L)) {
142       // If we have run LICM on a previous loop but now we are skipping
143       // (because we've hit the opt-bisect limit), we need to clear the
144       // loop alias information.
145       for (auto &LTAS : LICM.getLoopToAliasSetMap())
146         delete LTAS.second;
147       LICM.getLoopToAliasSetMap().clear();
148       return false;
149     }
150 
151     auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
152     MemorySSA *MSSA = EnableMSSALoopDependency
153                           ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA())
154                           : nullptr;
155     // For the old PM, we can't use OptimizationRemarkEmitter as an analysis
156     // pass.  Function analyses need to be preserved across loop transformations
157     // but ORE cannot be preserved (see comment before the pass definition).
158     OptimizationRemarkEmitter ORE(L->getHeader()->getParent());
159     return LICM.runOnLoop(L,
160                           &getAnalysis<AAResultsWrapperPass>().getAAResults(),
161                           &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
162                           &getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
163                           &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
164                           &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
165                               *L->getHeader()->getParent()),
166                           SE ? &SE->getSE() : nullptr, MSSA, &ORE, false);
167   }
168 
169   /// This transformation requires natural loop information & requires that
170   /// loop preheaders be inserted into the CFG...
171   ///
172   void getAnalysisUsage(AnalysisUsage &AU) const override {
173     AU.setPreservesCFG();
174     AU.addRequired<TargetLibraryInfoWrapperPass>();
175     if (EnableMSSALoopDependency)
176       AU.addRequired<MemorySSAWrapperPass>();
177     AU.addRequired<TargetTransformInfoWrapperPass>();
178     getLoopAnalysisUsage(AU);
179   }
180 
181   using llvm::Pass::doFinalization;
182 
183   bool doFinalization() override {
184     assert(LICM.getLoopToAliasSetMap().empty() &&
185            "Didn't free loop alias sets");
186     return false;
187   }
188 
189 private:
190   LoopInvariantCodeMotion LICM;
191 
192   /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
193   void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
194                                Loop *L) override;
195 
196   /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
197   /// set.
198   void deleteAnalysisValue(Value *V, Loop *L) override;
199 
200   /// Simple Analysis hook. Delete loop L from alias set map.
201   void deleteAnalysisLoop(Loop *L) override;
202 };
203 } // namespace
204 
205 PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM,
206                                 LoopStandardAnalysisResults &AR, LPMUpdater &) {
207   const auto &FAM =
208       AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager();
209   Function *F = L.getHeader()->getParent();
210 
211   auto *ORE = FAM.getCachedResult<OptimizationRemarkEmitterAnalysis>(*F);
212   // FIXME: This should probably be optional rather than required.
213   if (!ORE)
214     report_fatal_error("LICM: OptimizationRemarkEmitterAnalysis not "
215                        "cached at a higher level");
216 
217   LoopInvariantCodeMotion LICM;
218   if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, &AR.TLI, &AR.TTI, &AR.SE,
219                       AR.MSSA, ORE, true))
220     return PreservedAnalyses::all();
221 
222   auto PA = getLoopPassPreservedAnalyses();
223   PA.preserveSet<CFGAnalyses>();
224   return PA;
225 }
226 
227 char LegacyLICMPass::ID = 0;
228 INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",
229                       false, false)
230 INITIALIZE_PASS_DEPENDENCY(LoopPass)
231 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
232 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
233 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
234 INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false,
235                     false)
236 
237 Pass *llvm::createLICMPass() { return new LegacyLICMPass(); }
238 
239 /// Hoist expressions out of the specified loop. Note, alias info for inner
240 /// loop is not preserved so it is not a good idea to run LICM multiple
241 /// times on one loop.
242 /// We should delete AST for inner loops in the new pass manager to avoid
243 /// memory leak.
244 ///
245 bool LoopInvariantCodeMotion::runOnLoop(
246     Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT,
247     TargetLibraryInfo *TLI, TargetTransformInfo *TTI, ScalarEvolution *SE,
248     MemorySSA *MSSA, OptimizationRemarkEmitter *ORE, bool DeleteAST) {
249   bool Changed = false;
250 
251   assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
252 
253   AliasSetTracker *CurAST = collectAliasInfoForLoop(L, LI, AA);
254 
255   // Get the preheader block to move instructions into...
256   BasicBlock *Preheader = L->getLoopPreheader();
257 
258   // Compute loop safety information.
259   LoopSafetyInfo SafetyInfo;
260   computeLoopSafetyInfo(&SafetyInfo, L);
261 
262   // We want to visit all of the instructions in this loop... that are not parts
263   // of our subloops (they have already had their invariants hoisted out of
264   // their loop, into this loop, so there is no need to process the BODIES of
265   // the subloops).
266   //
267   // Traverse the body of the loop in depth first order on the dominator tree so
268   // that we are guaranteed to see definitions before we see uses.  This allows
269   // us to sink instructions in one pass, without iteration.  After sinking
270   // instructions, we perform another pass to hoist them out of the loop.
271   //
272   if (L->hasDedicatedExits())
273     Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, TTI, L,
274                           CurAST, &SafetyInfo, ORE);
275   if (Preheader)
276     Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, L,
277                            CurAST, &SafetyInfo, ORE);
278 
279   // Now that all loop invariants have been removed from the loop, promote any
280   // memory references to scalars that we can.
281   // Don't sink stores from loops without dedicated block exits. Exits
282   // containing indirect branches are not transformed by loop simplify,
283   // make sure we catch that. An additional load may be generated in the
284   // preheader for SSA updater, so also avoid sinking when no preheader
285   // is available.
286   if (!DisablePromotion && Preheader && L->hasDedicatedExits()) {
287     // Figure out the loop exits and their insertion points
288     SmallVector<BasicBlock *, 8> ExitBlocks;
289     L->getUniqueExitBlocks(ExitBlocks);
290 
291     // We can't insert into a catchswitch.
292     bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) {
293       return isa<CatchSwitchInst>(Exit->getTerminator());
294     });
295 
296     if (!HasCatchSwitch) {
297       SmallVector<Instruction *, 8> InsertPts;
298       InsertPts.reserve(ExitBlocks.size());
299       for (BasicBlock *ExitBlock : ExitBlocks)
300         InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
301 
302       PredIteratorCache PIC;
303 
304       bool Promoted = false;
305 
306       // Loop over all of the alias sets in the tracker object.
307       for (AliasSet &AS : *CurAST) {
308         // We can promote this alias set if it has a store, if it is a "Must"
309         // alias set, if the pointer is loop invariant, and if we are not
310         // eliminating any volatile loads or stores.
311         if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
312             AS.isVolatile() || !L->isLoopInvariant(AS.begin()->getValue()))
313           continue;
314 
315         assert(
316             !AS.empty() &&
317             "Must alias set should have at least one pointer element in it!");
318 
319         SmallSetVector<Value *, 8> PointerMustAliases;
320         for (const auto &ASI : AS)
321           PointerMustAliases.insert(ASI.getValue());
322 
323         Promoted |= promoteLoopAccessesToScalars(PointerMustAliases, ExitBlocks,
324                                                  InsertPts, PIC, LI, DT, TLI, L,
325                                                  CurAST, &SafetyInfo, ORE);
326       }
327 
328       // Once we have promoted values across the loop body we have to
329       // recursively reform LCSSA as any nested loop may now have values defined
330       // within the loop used in the outer loop.
331       // FIXME: This is really heavy handed. It would be a bit better to use an
332       // SSAUpdater strategy during promotion that was LCSSA aware and reformed
333       // it as it went.
334       if (Promoted)
335         formLCSSARecursively(*L, *DT, LI, SE);
336 
337       Changed |= Promoted;
338     }
339   }
340 
341   // Check that neither this loop nor its parent have had LCSSA broken. LICM is
342   // specifically moving instructions across the loop boundary and so it is
343   // especially in need of sanity checking here.
344   assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!");
345   assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&
346          "Parent loop not left in LCSSA form after LICM!");
347 
348   // If this loop is nested inside of another one, save the alias information
349   // for when we process the outer loop.
350   if (L->getParentLoop() && !DeleteAST)
351     LoopToAliasSetMap[L] = CurAST;
352   else
353     delete CurAST;
354 
355   if (Changed && SE)
356     SE->forgetLoopDispositions(L);
357   return Changed;
358 }
359 
360 /// Walk the specified region of the CFG (defined by all blocks dominated by
361 /// the specified block, and that are in the current loop) in reverse depth
362 /// first order w.r.t the DominatorTree.  This allows us to visit uses before
363 /// definitions, allowing us to sink a loop body in one pass without iteration.
364 ///
365 bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
366                       DominatorTree *DT, TargetLibraryInfo *TLI,
367                       TargetTransformInfo *TTI, Loop *CurLoop,
368                       AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
369                       OptimizationRemarkEmitter *ORE) {
370 
371   // Verify inputs.
372   assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
373          CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr &&
374          "Unexpected input to sinkRegion");
375 
376   // We want to visit children before parents. We will enque all the parents
377   // before their children in the worklist and process the worklist in reverse
378   // order.
379   SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop);
380 
381   bool Changed = false;
382   for (DomTreeNode *DTN : reverse(Worklist)) {
383     BasicBlock *BB = DTN->getBlock();
384     // Only need to process the contents of this block if it is not part of a
385     // subloop (which would already have been processed).
386     if (inSubLoop(BB, CurLoop, LI))
387       continue;
388 
389     for (BasicBlock::iterator II = BB->end(); II != BB->begin();) {
390       Instruction &I = *--II;
391 
392       // If the instruction is dead, we would try to sink it because it isn't
393       // used in the loop, instead, just delete it.
394       if (isInstructionTriviallyDead(&I, TLI)) {
395         DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n');
396         ++II;
397         CurAST->deleteValue(&I);
398         I.eraseFromParent();
399         Changed = true;
400         continue;
401       }
402 
403       // Check to see if we can sink this instruction to the exit blocks
404       // of the loop.  We can do this if the all users of the instruction are
405       // outside of the loop.  In this case, it doesn't even matter if the
406       // operands of the instruction are loop invariant.
407       //
408       bool FreeInLoop = false;
409       if (isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) &&
410           canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, SafetyInfo, ORE)) {
411         if (sink(I, LI, DT, CurLoop, SafetyInfo, ORE, FreeInLoop)) {
412           if (!FreeInLoop) {
413             ++II;
414             CurAST->deleteValue(&I);
415             I.eraseFromParent();
416           }
417           Changed = true;
418         }
419       }
420     }
421   }
422   return Changed;
423 }
424 
425 /// Walk the specified region of the CFG (defined by all blocks dominated by
426 /// the specified block, and that are in the current loop) in depth first
427 /// order w.r.t the DominatorTree.  This allows us to visit definitions before
428 /// uses, allowing us to hoist a loop body in one pass without iteration.
429 ///
430 bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
431                        DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
432                        AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
433                        OptimizationRemarkEmitter *ORE) {
434   // Verify inputs.
435   assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
436          CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr &&
437          "Unexpected input to hoistRegion");
438 
439   // We want to visit parents before children. We will enque all the parents
440   // before their children in the worklist and process the worklist in order.
441   SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop);
442 
443   bool Changed = false;
444   for (DomTreeNode *DTN : Worklist) {
445     BasicBlock *BB = DTN->getBlock();
446     // Only need to process the contents of this block if it is not part of a
447     // subloop (which would already have been processed).
448     if (!inSubLoop(BB, CurLoop, LI))
449       for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
450         Instruction &I = *II++;
451         // Try constant folding this instruction.  If all the operands are
452         // constants, it is technically hoistable, but it would be better to
453         // just fold it.
454         if (Constant *C = ConstantFoldInstruction(
455                 &I, I.getModule()->getDataLayout(), TLI)) {
456           DEBUG(dbgs() << "LICM folding inst: " << I << "  --> " << *C << '\n');
457           CurAST->copyValue(&I, C);
458           I.replaceAllUsesWith(C);
459           if (isInstructionTriviallyDead(&I, TLI)) {
460             CurAST->deleteValue(&I);
461             I.eraseFromParent();
462           }
463           Changed = true;
464           continue;
465         }
466 
467         // Attempt to remove floating point division out of the loop by
468         // converting it to a reciprocal multiplication.
469         if (I.getOpcode() == Instruction::FDiv &&
470             CurLoop->isLoopInvariant(I.getOperand(1)) &&
471             I.hasAllowReciprocal()) {
472           auto Divisor = I.getOperand(1);
473           auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0);
474           auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor);
475           ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags());
476           ReciprocalDivisor->insertBefore(&I);
477 
478           auto Product =
479               BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor);
480           Product->setFastMathFlags(I.getFastMathFlags());
481           Product->insertAfter(&I);
482           I.replaceAllUsesWith(Product);
483           I.eraseFromParent();
484 
485           hoist(*ReciprocalDivisor, DT, CurLoop, SafetyInfo, ORE);
486           Changed = true;
487           continue;
488         }
489 
490         // Try hoisting the instruction out to the preheader.  We can only do
491         // this if all of the operands of the instruction are loop invariant and
492         // if it is safe to hoist the instruction.
493         //
494         if (CurLoop->hasLoopInvariantOperands(&I) &&
495             canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, SafetyInfo, ORE) &&
496             isSafeToExecuteUnconditionally(
497                 I, DT, CurLoop, SafetyInfo, ORE,
498                 CurLoop->getLoopPreheader()->getTerminator()))
499           Changed |= hoist(I, DT, CurLoop, SafetyInfo, ORE);
500       }
501   }
502 
503   return Changed;
504 }
505 
506 /// Computes loop safety information, checks loop body & header
507 /// for the possibility of may throw exception.
508 ///
509 void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
510   assert(CurLoop != nullptr && "CurLoop cant be null");
511   BasicBlock *Header = CurLoop->getHeader();
512   // Setting default safety values.
513   SafetyInfo->MayThrow = false;
514   SafetyInfo->HeaderMayThrow = false;
515   // Iterate over header and compute safety info.
516   for (BasicBlock::iterator I = Header->begin(), E = Header->end();
517        (I != E) && !SafetyInfo->HeaderMayThrow; ++I)
518     SafetyInfo->HeaderMayThrow |=
519         !isGuaranteedToTransferExecutionToSuccessor(&*I);
520 
521   SafetyInfo->MayThrow = SafetyInfo->HeaderMayThrow;
522   // Iterate over loop instructions and compute safety info.
523   // Skip header as it has been computed and stored in HeaderMayThrow.
524   // The first block in loopinfo.Blocks is guaranteed to be the header.
525   assert(Header == *CurLoop->getBlocks().begin() &&
526          "First block must be header");
527   for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
528                             BBE = CurLoop->block_end();
529        (BB != BBE) && !SafetyInfo->MayThrow; ++BB)
530     for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
531          (I != E) && !SafetyInfo->MayThrow; ++I)
532       SafetyInfo->MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(&*I);
533 
534   // Compute funclet colors if we might sink/hoist in a function with a funclet
535   // personality routine.
536   Function *Fn = CurLoop->getHeader()->getParent();
537   if (Fn->hasPersonalityFn())
538     if (Constant *PersonalityFn = Fn->getPersonalityFn())
539       if (isFuncletEHPersonality(classifyEHPersonality(PersonalityFn)))
540         SafetyInfo->BlockColors = colorEHFunclets(*Fn);
541 }
542 
543 // Return true if LI is invariant within scope of the loop. LI is invariant if
544 // CurLoop is dominated by an invariant.start representing the same memory
545 // location and size as the memory location LI loads from, and also the
546 // invariant.start has no uses.
547 static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT,
548                                   Loop *CurLoop) {
549   Value *Addr = LI->getOperand(0);
550   const DataLayout &DL = LI->getModule()->getDataLayout();
551   const uint32_t LocSizeInBits = DL.getTypeSizeInBits(
552       cast<PointerType>(Addr->getType())->getElementType());
553 
554   // if the type is i8 addrspace(x)*, we know this is the type of
555   // llvm.invariant.start operand
556   auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()),
557                                      LI->getPointerAddressSpace());
558   unsigned BitcastsVisited = 0;
559   // Look through bitcasts until we reach the i8* type (this is invariant.start
560   // operand type).
561   while (Addr->getType() != PtrInt8Ty) {
562     auto *BC = dyn_cast<BitCastInst>(Addr);
563     // Avoid traversing high number of bitcast uses.
564     if (++BitcastsVisited > MaxNumUsesTraversed || !BC)
565       return false;
566     Addr = BC->getOperand(0);
567   }
568 
569   unsigned UsesVisited = 0;
570   // Traverse all uses of the load operand value, to see if invariant.start is
571   // one of the uses, and whether it dominates the load instruction.
572   for (auto *U : Addr->users()) {
573     // Avoid traversing for Load operand with high number of users.
574     if (++UsesVisited > MaxNumUsesTraversed)
575       return false;
576     IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
577     // If there are escaping uses of invariant.start instruction, the load maybe
578     // non-invariant.
579     if (!II || II->getIntrinsicID() != Intrinsic::invariant_start ||
580         !II->use_empty())
581       continue;
582     unsigned InvariantSizeInBits =
583         cast<ConstantInt>(II->getArgOperand(0))->getSExtValue() * 8;
584     // Confirm the invariant.start location size contains the load operand size
585     // in bits. Also, the invariant.start should dominate the load, and we
586     // should not hoist the load out of a loop that contains this dominating
587     // invariant.start.
588     if (LocSizeInBits <= InvariantSizeInBits &&
589         DT->properlyDominates(II->getParent(), CurLoop->getHeader()))
590       return true;
591   }
592 
593   return false;
594 }
595 
596 bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
597                               Loop *CurLoop, AliasSetTracker *CurAST,
598                               LoopSafetyInfo *SafetyInfo,
599                               OptimizationRemarkEmitter *ORE) {
600   // SafetyInfo is nullptr if we are checking for sinking from preheader to
601   // loop body.
602   const bool SinkingToLoopBody = !SafetyInfo;
603   // Loads have extra constraints we have to verify before we can hoist them.
604   if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
605     if (!LI->isUnordered())
606       return false; // Don't sink/hoist volatile or ordered atomic loads!
607 
608     // Loads from constant memory are always safe to move, even if they end up
609     // in the same alias set as something that ends up being modified.
610     if (AA->pointsToConstantMemory(LI->getOperand(0)))
611       return true;
612     if (LI->getMetadata(LLVMContext::MD_invariant_load))
613       return true;
614 
615     if (LI->isAtomic() && SinkingToLoopBody)
616       return false; // Don't sink unordered atomic loads to loop body.
617 
618     // This checks for an invariant.start dominating the load.
619     if (isLoadInvariantInLoop(LI, DT, CurLoop))
620       return true;
621 
622     // Don't hoist loads which have may-aliased stores in loop.
623     uint64_t Size = 0;
624     if (LI->getType()->isSized())
625       Size = I.getModule()->getDataLayout().getTypeStoreSize(LI->getType());
626 
627     AAMDNodes AAInfo;
628     LI->getAAMetadata(AAInfo);
629 
630     bool Invalidated =
631         pointerInvalidatedByLoop(LI->getOperand(0), Size, AAInfo, CurAST);
632     // Check loop-invariant address because this may also be a sinkable load
633     // whose address is not necessarily loop-invariant.
634     if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand()))
635       ORE->emit([&]() {
636         return OptimizationRemarkMissed(
637                    DEBUG_TYPE, "LoadWithLoopInvariantAddressInvalidated", LI)
638                << "failed to move load with loop-invariant address "
639                   "because the loop may invalidate its value";
640       });
641 
642     return !Invalidated;
643   } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
644     // Don't sink or hoist dbg info; it's legal, but not useful.
645     if (isa<DbgInfoIntrinsic>(I))
646       return false;
647 
648     // Don't sink calls which can throw.
649     if (CI->mayThrow())
650       return false;
651 
652     // Handle simple cases by querying alias analysis.
653     FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI);
654     if (Behavior == FMRB_DoesNotAccessMemory)
655       return true;
656     if (AliasAnalysis::onlyReadsMemory(Behavior)) {
657       // A readonly argmemonly function only reads from memory pointed to by
658       // it's arguments with arbitrary offsets.  If we can prove there are no
659       // writes to this memory in the loop, we can hoist or sink.
660       if (AliasAnalysis::onlyAccessesArgPointees(Behavior)) {
661         for (Value *Op : CI->arg_operands())
662           if (Op->getType()->isPointerTy() &&
663               pointerInvalidatedByLoop(Op, MemoryLocation::UnknownSize,
664                                        AAMDNodes(), CurAST))
665             return false;
666         return true;
667       }
668       // If this call only reads from memory and there are no writes to memory
669       // in the loop, we can hoist or sink the call as appropriate.
670       bool FoundMod = false;
671       for (AliasSet &AS : *CurAST) {
672         if (!AS.isForwardingAliasSet() && AS.isMod()) {
673           FoundMod = true;
674           break;
675         }
676       }
677       if (!FoundMod)
678         return true;
679     }
680 
681     // FIXME: This should use mod/ref information to see if we can hoist or
682     // sink the call.
683 
684     return false;
685   }
686 
687   // Only these instructions are hoistable/sinkable.
688   if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) &&
689       !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) &&
690       !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) &&
691       !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) &&
692       !isa<InsertValueInst>(I))
693     return false;
694 
695   // If we are checking for sinking from preheader to loop body it will be
696   // always safe as there is no speculative execution.
697   if (SinkingToLoopBody)
698     return true;
699 
700   // TODO: Plumb the context instruction through to make hoisting and sinking
701   // more powerful. Hoisting of loads already works due to the special casing
702   // above.
703   return isSafeToExecuteUnconditionally(I, DT, CurLoop, SafetyInfo, nullptr);
704 }
705 
706 /// Returns true if a PHINode is a trivially replaceable with an
707 /// Instruction.
708 /// This is true when all incoming values are that instruction.
709 /// This pattern occurs most often with LCSSA PHI nodes.
710 ///
711 static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I) {
712   for (const Value *IncValue : PN.incoming_values())
713     if (IncValue != &I)
714       return false;
715 
716   return true;
717 }
718 
719 /// Return true if the instruction is free in the loop.
720 static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop,
721                          const TargetTransformInfo *TTI) {
722 
723   if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
724     if (TTI->getUserCost(GEP) != TargetTransformInfo::TCC_Free)
725       return false;
726     // For a GEP, we cannot simply use getUserCost because currently it
727     // optimistically assume that a GEP will fold into addressing mode
728     // regardless of its users.
729     const BasicBlock *BB = GEP->getParent();
730     for (const User *U : GEP->users()) {
731       const Instruction *UI = cast<Instruction>(U);
732       if (CurLoop->contains(UI) &&
733           (BB != UI->getParent() ||
734            (!isa<StoreInst>(UI) && !isa<LoadInst>(UI))))
735         return false;
736     }
737     return true;
738   } else
739     return TTI->getUserCost(&I) == TargetTransformInfo::TCC_Free;
740 }
741 
742 /// Return true if the only users of this instruction are outside of
743 /// the loop. If this is true, we can sink the instruction to the exit
744 /// blocks of the loop.
745 ///
746 /// We also return true if the instruction could be folded away in lowering.
747 /// (e.g.,  a GEP can be folded into a load as an addressing mode in the loop).
748 static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
749                                   const LoopSafetyInfo *SafetyInfo,
750                                   TargetTransformInfo *TTI, bool &FreeInLoop) {
751   const auto &BlockColors = SafetyInfo->BlockColors;
752   bool IsFree = isFreeInLoop(I, CurLoop, TTI);
753   for (const User *U : I.users()) {
754     const Instruction *UI = cast<Instruction>(U);
755     if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
756       const BasicBlock *BB = PN->getParent();
757       // We cannot sink uses in catchswitches.
758       if (isa<CatchSwitchInst>(BB->getTerminator()))
759         return false;
760 
761       // We need to sink a callsite to a unique funclet.  Avoid sinking if the
762       // phi use is too muddled.
763       if (isa<CallInst>(I))
764         if (!BlockColors.empty() &&
765             BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1)
766           return false;
767     }
768 
769     if (CurLoop->contains(UI)) {
770       if (IsFree) {
771         FreeInLoop = true;
772         continue;
773       }
774       return false;
775     }
776   }
777   return true;
778 }
779 
780 static Instruction *
781 CloneInstructionInExitBlock(Instruction &I, BasicBlock &ExitBlock, PHINode &PN,
782                             const LoopInfo *LI,
783                             const LoopSafetyInfo *SafetyInfo) {
784   Instruction *New;
785   if (auto *CI = dyn_cast<CallInst>(&I)) {
786     const auto &BlockColors = SafetyInfo->BlockColors;
787 
788     // Sinking call-sites need to be handled differently from other
789     // instructions.  The cloned call-site needs a funclet bundle operand
790     // appropriate for it's location in the CFG.
791     SmallVector<OperandBundleDef, 1> OpBundles;
792     for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles();
793          BundleIdx != BundleEnd; ++BundleIdx) {
794       OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx);
795       if (Bundle.getTagID() == LLVMContext::OB_funclet)
796         continue;
797 
798       OpBundles.emplace_back(Bundle);
799     }
800 
801     if (!BlockColors.empty()) {
802       const ColorVector &CV = BlockColors.find(&ExitBlock)->second;
803       assert(CV.size() == 1 && "non-unique color for exit block!");
804       BasicBlock *BBColor = CV.front();
805       Instruction *EHPad = BBColor->getFirstNonPHI();
806       if (EHPad->isEHPad())
807         OpBundles.emplace_back("funclet", EHPad);
808     }
809 
810     New = CallInst::Create(CI, OpBundles);
811   } else {
812     New = I.clone();
813   }
814 
815   ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
816   if (!I.getName().empty())
817     New->setName(I.getName() + ".le");
818 
819   // Build LCSSA PHI nodes for any in-loop operands. Note that this is
820   // particularly cheap because we can rip off the PHI node that we're
821   // replacing for the number and blocks of the predecessors.
822   // OPT: If this shows up in a profile, we can instead finish sinking all
823   // invariant instructions, and then walk their operands to re-establish
824   // LCSSA. That will eliminate creating PHI nodes just to nuke them when
825   // sinking bottom-up.
826   for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE;
827        ++OI)
828     if (Instruction *OInst = dyn_cast<Instruction>(*OI))
829       if (Loop *OLoop = LI->getLoopFor(OInst->getParent()))
830         if (!OLoop->contains(&PN)) {
831           PHINode *OpPN =
832               PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
833                               OInst->getName() + ".lcssa", &ExitBlock.front());
834           for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
835             OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
836           *OI = OpPN;
837         }
838   return New;
839 }
840 
841 static Instruction *sinkThroughTriviallyReplacablePHI(
842     PHINode *TPN, Instruction *I, LoopInfo *LI,
843     SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies,
844     const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop) {
845   assert(isTriviallyReplacablePHI(*TPN, *I) &&
846          "Expect only trivially replacalbe PHI");
847   BasicBlock *ExitBlock = TPN->getParent();
848   Instruction *New;
849   auto It = SunkCopies.find(ExitBlock);
850   if (It != SunkCopies.end())
851     New = It->second;
852   else
853     New = SunkCopies[ExitBlock] =
854         CloneInstructionInExitBlock(*I, *ExitBlock, *TPN, LI, SafetyInfo);
855   return New;
856 }
857 
858 static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) {
859   BasicBlock *BB = PN->getParent();
860   if (!BB->canSplitPredecessors())
861     return false;
862   // It's not impossible to split EHPad blocks, but if BlockColors already exist
863   // it require updating BlockColors for all offspring blocks accordingly. By
864   // skipping such corner case, we can make updating BlockColors after splitting
865   // predecessor fairly simple.
866   if (!SafetyInfo->BlockColors.empty() && BB->getFirstNonPHI()->isEHPad())
867     return false;
868   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
869     BasicBlock *BBPred = *PI;
870     if (isa<IndirectBrInst>(BBPred->getTerminator()))
871       return false;
872   }
873   return true;
874 }
875 
876 static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
877                                         LoopInfo *LI, const Loop *CurLoop,
878                                         LoopSafetyInfo *SafetyInfo) {
879 #ifndef NDEBUG
880   SmallVector<BasicBlock *, 32> ExitBlocks;
881   CurLoop->getUniqueExitBlocks(ExitBlocks);
882   SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
883                                              ExitBlocks.end());
884 #endif
885   BasicBlock *ExitBB = PN->getParent();
886   assert(ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block.");
887 
888   // Split predecessors of the loop exit to make instructions in the loop are
889   // exposed to exit blocks through trivially replacable PHIs while keeping the
890   // loop in the canonical form where each predecessor of each exit block should
891   // be contained within the loop. For example, this will convert the loop below
892   // from
893   //
894   // LB1:
895   //   %v1 =
896   //   br %LE, %LB2
897   // LB2:
898   //   %v2 =
899   //   br %LE, %LB1
900   // LE:
901   //   %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replacable
902   //
903   // to
904   //
905   // LB1:
906   //   %v1 =
907   //   br %LE.split, %LB2
908   // LB2:
909   //   %v2 =
910   //   br %LE.split2, %LB1
911   // LE.split:
912   //   %p1 = phi [%v1, %LB1]  <-- trivially replacable
913   //   br %LE
914   // LE.split2:
915   //   %p2 = phi [%v2, %LB2]  <-- trivially replacable
916   //   br %LE
917   // LE:
918   //   %p = phi [%p1, %LE.split], [%p2, %LE.split2]
919   //
920   auto &BlockColors = SafetyInfo->BlockColors;
921   SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB));
922   while (!PredBBs.empty()) {
923     BasicBlock *PredBB = *PredBBs.begin();
924     assert(CurLoop->contains(PredBB) &&
925            "Expect all predecessors are in the loop");
926     if (PN->getBasicBlockIndex(PredBB) >= 0) {
927       BasicBlock *NewPred = SplitBlockPredecessors(
928           ExitBB, PredBB, ".split.loop.exit", DT, LI, true);
929       // Since we do not allow splitting EH-block with BlockColors in
930       // canSplitPredecessors(), we can simply assign predecessor's color to
931       // the new block.
932       if (!BlockColors.empty())
933         BlockColors[NewPred] = BlockColors[PredBB];
934     }
935     PredBBs.remove(PredBB);
936   }
937 }
938 
939 /// When an instruction is found to only be used outside of the loop, this
940 /// function moves it to the exit blocks and patches up SSA form as needed.
941 /// This method is guaranteed to remove the original instruction from its
942 /// position, and may either delete it or move it to outside of the loop.
943 ///
944 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
945                  const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
946                  OptimizationRemarkEmitter *ORE, bool FreeInLoop) {
947   DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
948   ORE->emit([&]() {
949     return OptimizationRemark(DEBUG_TYPE, "InstSunk", &I)
950            << "sinking " << ore::NV("Inst", &I);
951   });
952   bool Changed = false;
953   if (isa<LoadInst>(I))
954     ++NumMovedLoads;
955   else if (isa<CallInst>(I))
956     ++NumMovedCalls;
957   ++NumSunk;
958 
959   // Iterate over users to be ready for actual sinking. Replace users via
960   // unrechable blocks with undef and make all user PHIs trivially replcable.
961   SmallPtrSet<Instruction *, 8> VisitedUsers;
962   for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) {
963     auto *User = cast<Instruction>(*UI);
964     Use &U = UI.getUse();
965     ++UI;
966 
967     if (VisitedUsers.count(User) || CurLoop->contains(User))
968       continue;
969 
970     if (!DT->isReachableFromEntry(User->getParent())) {
971       U = UndefValue::get(I.getType());
972       Changed = true;
973       continue;
974     }
975 
976     // The user must be a PHI node.
977     PHINode *PN = cast<PHINode>(User);
978 
979     // Surprisingly, instructions can be used outside of loops without any
980     // exits.  This can only happen in PHI nodes if the incoming block is
981     // unreachable.
982     BasicBlock *BB = PN->getIncomingBlock(U);
983     if (!DT->isReachableFromEntry(BB)) {
984       U = UndefValue::get(I.getType());
985       Changed = true;
986       continue;
987     }
988 
989     VisitedUsers.insert(PN);
990     if (isTriviallyReplacablePHI(*PN, I))
991       continue;
992 
993     if (!canSplitPredecessors(PN, SafetyInfo))
994       return Changed;
995 
996     // Split predecessors of the PHI so that we can make users trivially
997     // replacable.
998     splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo);
999 
1000     // Should rebuild the iterators, as they may be invalidated by
1001     // splitPredecessorsOfLoopExit().
1002     UI = I.user_begin();
1003     UE = I.user_end();
1004   }
1005 
1006   if (VisitedUsers.empty())
1007     return Changed;
1008 
1009 #ifndef NDEBUG
1010   SmallVector<BasicBlock *, 32> ExitBlocks;
1011   CurLoop->getUniqueExitBlocks(ExitBlocks);
1012   SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
1013                                              ExitBlocks.end());
1014 #endif
1015 
1016   // Clones of this instruction. Don't create more than one per exit block!
1017   SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies;
1018 
1019   // If this instruction is only used outside of the loop, then all users are
1020   // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
1021   // the instruction.
1022   SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end());
1023   for (auto *UI : Users) {
1024     auto *User = cast<Instruction>(UI);
1025 
1026     if (CurLoop->contains(User))
1027       continue;
1028 
1029     PHINode *PN = cast<PHINode>(User);
1030     assert(ExitBlockSet.count(PN->getParent()) &&
1031            "The LCSSA PHI is not in an exit block!");
1032     // The PHI must be trivially replacable.
1033     Instruction *New = sinkThroughTriviallyReplacablePHI(PN, &I, LI, SunkCopies,
1034                                                          SafetyInfo, CurLoop);
1035     PN->replaceAllUsesWith(New);
1036     PN->eraseFromParent();
1037     Changed = true;
1038   }
1039   return Changed;
1040 }
1041 
1042 /// When an instruction is found to only use loop invariant operands that
1043 /// is safe to hoist, this instruction is called to do the dirty work.
1044 ///
1045 static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
1046                   const LoopSafetyInfo *SafetyInfo,
1047                   OptimizationRemarkEmitter *ORE) {
1048   auto *Preheader = CurLoop->getLoopPreheader();
1049   DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " << I
1050                << "\n");
1051   ORE->emit([&]() {
1052     return OptimizationRemark(DEBUG_TYPE, "Hoisted", &I) << "hoisting "
1053                                                          << ore::NV("Inst", &I);
1054   });
1055 
1056   // Metadata can be dependent on conditions we are hoisting above.
1057   // Conservatively strip all metadata on the instruction unless we were
1058   // guaranteed to execute I if we entered the loop, in which case the metadata
1059   // is valid in the loop preheader.
1060   if (I.hasMetadataOtherThanDebugLoc() &&
1061       // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning
1062       // time in isGuaranteedToExecute if we don't actually have anything to
1063       // drop.  It is a compile time optimization, not required for correctness.
1064       !isGuaranteedToExecute(I, DT, CurLoop, SafetyInfo))
1065     I.dropUnknownNonDebugMetadata();
1066 
1067   // Move the new node to the Preheader, before its terminator.
1068   I.moveBefore(Preheader->getTerminator());
1069 
1070   // Do not retain debug locations when we are moving instructions to different
1071   // basic blocks, because we want to avoid jumpy line tables. Calls, however,
1072   // need to retain their debug locs because they may be inlined.
1073   // FIXME: How do we retain source locations without causing poor debugging
1074   // behavior?
1075   if (!isa<CallInst>(I))
1076     I.setDebugLoc(DebugLoc());
1077 
1078   if (isa<LoadInst>(I))
1079     ++NumMovedLoads;
1080   else if (isa<CallInst>(I))
1081     ++NumMovedCalls;
1082   ++NumHoisted;
1083   return true;
1084 }
1085 
1086 /// Only sink or hoist an instruction if it is not a trapping instruction,
1087 /// or if the instruction is known not to trap when moved to the preheader.
1088 /// or if it is a trapping instruction and is guaranteed to execute.
1089 static bool isSafeToExecuteUnconditionally(Instruction &Inst,
1090                                            const DominatorTree *DT,
1091                                            const Loop *CurLoop,
1092                                            const LoopSafetyInfo *SafetyInfo,
1093                                            OptimizationRemarkEmitter *ORE,
1094                                            const Instruction *CtxI) {
1095   if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT))
1096     return true;
1097 
1098   bool GuaranteedToExecute =
1099       isGuaranteedToExecute(Inst, DT, CurLoop, SafetyInfo);
1100 
1101   if (!GuaranteedToExecute) {
1102     auto *LI = dyn_cast<LoadInst>(&Inst);
1103     if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand()))
1104       ORE->emit([&]() {
1105         return OptimizationRemarkMissed(
1106                    DEBUG_TYPE, "LoadWithLoopInvariantAddressCondExecuted", LI)
1107                << "failed to hoist load with loop-invariant address "
1108                   "because load is conditionally executed";
1109       });
1110   }
1111 
1112   return GuaranteedToExecute;
1113 }
1114 
1115 namespace {
1116 class LoopPromoter : public LoadAndStorePromoter {
1117   Value *SomePtr; // Designated pointer to store to.
1118   const SmallSetVector<Value *, 8> &PointerMustAliases;
1119   SmallVectorImpl<BasicBlock *> &LoopExitBlocks;
1120   SmallVectorImpl<Instruction *> &LoopInsertPts;
1121   PredIteratorCache &PredCache;
1122   AliasSetTracker &AST;
1123   LoopInfo &LI;
1124   DebugLoc DL;
1125   int Alignment;
1126   bool UnorderedAtomic;
1127   AAMDNodes AATags;
1128 
1129   Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
1130     if (Instruction *I = dyn_cast<Instruction>(V))
1131       if (Loop *L = LI.getLoopFor(I->getParent()))
1132         if (!L->contains(BB)) {
1133           // We need to create an LCSSA PHI node for the incoming value and
1134           // store that.
1135           PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB),
1136                                         I->getName() + ".lcssa", &BB->front());
1137           for (BasicBlock *Pred : PredCache.get(BB))
1138             PN->addIncoming(I, Pred);
1139           return PN;
1140         }
1141     return V;
1142   }
1143 
1144 public:
1145   LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S,
1146                const SmallSetVector<Value *, 8> &PMA,
1147                SmallVectorImpl<BasicBlock *> &LEB,
1148                SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC,
1149                AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
1150                bool UnorderedAtomic, const AAMDNodes &AATags)
1151       : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
1152         LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
1153         LI(li), DL(std::move(dl)), Alignment(alignment),
1154         UnorderedAtomic(UnorderedAtomic), AATags(AATags) {}
1155 
1156   bool isInstInList(Instruction *I,
1157                     const SmallVectorImpl<Instruction *> &) const override {
1158     Value *Ptr;
1159     if (LoadInst *LI = dyn_cast<LoadInst>(I))
1160       Ptr = LI->getOperand(0);
1161     else
1162       Ptr = cast<StoreInst>(I)->getPointerOperand();
1163     return PointerMustAliases.count(Ptr);
1164   }
1165 
1166   void doExtraRewritesBeforeFinalDeletion() const override {
1167     // Insert stores after in the loop exit blocks.  Each exit block gets a
1168     // store of the live-out values that feed them.  Since we've already told
1169     // the SSA updater about the defs in the loop and the preheader
1170     // definition, it is all set and we can start using it.
1171     for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
1172       BasicBlock *ExitBlock = LoopExitBlocks[i];
1173       Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
1174       LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
1175       Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
1176       Instruction *InsertPos = LoopInsertPts[i];
1177       StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
1178       if (UnorderedAtomic)
1179         NewSI->setOrdering(AtomicOrdering::Unordered);
1180       NewSI->setAlignment(Alignment);
1181       NewSI->setDebugLoc(DL);
1182       if (AATags)
1183         NewSI->setAAMetadata(AATags);
1184     }
1185   }
1186 
1187   void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
1188     // Update alias analysis.
1189     AST.copyValue(LI, V);
1190   }
1191   void instructionDeleted(Instruction *I) const override { AST.deleteValue(I); }
1192 };
1193 
1194 
1195 /// Return true iff we can prove that a caller of this function can not inspect
1196 /// the contents of the provided object in a well defined program.
1197 bool isKnownNonEscaping(Value *Object, const TargetLibraryInfo *TLI) {
1198   if (isa<AllocaInst>(Object))
1199     // Since the alloca goes out of scope, we know the caller can't retain a
1200     // reference to it and be well defined.  Thus, we don't need to check for
1201     // capture.
1202     return true;
1203 
1204   // For all other objects we need to know that the caller can't possibly
1205   // have gotten a reference to the object.  There are two components of
1206   // that:
1207   //   1) Object can't be escaped by this function.  This is what
1208   //      PointerMayBeCaptured checks.
1209   //   2) Object can't have been captured at definition site.  For this, we
1210   //      need to know the return value is noalias.  At the moment, we use a
1211   //      weaker condition and handle only AllocLikeFunctions (which are
1212   //      known to be noalias).  TODO
1213   return isAllocLikeFn(Object, TLI) &&
1214     !PointerMayBeCaptured(Object, true, true);
1215 }
1216 
1217 } // namespace
1218 
1219 /// Try to promote memory values to scalars by sinking stores out of the
1220 /// loop and moving loads to before the loop.  We do this by looping over
1221 /// the stores in the loop, looking for stores to Must pointers which are
1222 /// loop invariant.
1223 ///
1224 bool llvm::promoteLoopAccessesToScalars(
1225     const SmallSetVector<Value *, 8> &PointerMustAliases,
1226     SmallVectorImpl<BasicBlock *> &ExitBlocks,
1227     SmallVectorImpl<Instruction *> &InsertPts, PredIteratorCache &PIC,
1228     LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI,
1229     Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
1230     OptimizationRemarkEmitter *ORE) {
1231   // Verify inputs.
1232   assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&
1233          CurAST != nullptr && SafetyInfo != nullptr &&
1234          "Unexpected Input to promoteLoopAccessesToScalars");
1235 
1236   Value *SomePtr = *PointerMustAliases.begin();
1237   BasicBlock *Preheader = CurLoop->getLoopPreheader();
1238 
1239   // It isn't safe to promote a load/store from the loop if the load/store is
1240   // conditional.  For example, turning:
1241   //
1242   //    for () { if (c) *P += 1; }
1243   //
1244   // into:
1245   //
1246   //    tmp = *P;  for () { if (c) tmp +=1; } *P = tmp;
1247   //
1248   // is not safe, because *P may only be valid to access if 'c' is true.
1249   //
1250   // The safety property divides into two parts:
1251   // p1) The memory may not be dereferenceable on entry to the loop.  In this
1252   //    case, we can't insert the required load in the preheader.
1253   // p2) The memory model does not allow us to insert a store along any dynamic
1254   //    path which did not originally have one.
1255   //
1256   // If at least one store is guaranteed to execute, both properties are
1257   // satisfied, and promotion is legal.
1258   //
1259   // This, however, is not a necessary condition. Even if no store/load is
1260   // guaranteed to execute, we can still establish these properties.
1261   // We can establish (p1) by proving that hoisting the load into the preheader
1262   // is safe (i.e. proving dereferenceability on all paths through the loop). We
1263   // can use any access within the alias set to prove dereferenceability,
1264   // since they're all must alias.
1265   //
1266   // There are two ways establish (p2):
1267   // a) Prove the location is thread-local. In this case the memory model
1268   // requirement does not apply, and stores are safe to insert.
1269   // b) Prove a store dominates every exit block. In this case, if an exit
1270   // blocks is reached, the original dynamic path would have taken us through
1271   // the store, so inserting a store into the exit block is safe. Note that this
1272   // is different from the store being guaranteed to execute. For instance,
1273   // if an exception is thrown on the first iteration of the loop, the original
1274   // store is never executed, but the exit blocks are not executed either.
1275 
1276   bool DereferenceableInPH = false;
1277   bool SafeToInsertStore = false;
1278 
1279   SmallVector<Instruction *, 64> LoopUses;
1280 
1281   // We start with an alignment of one and try to find instructions that allow
1282   // us to prove better alignment.
1283   unsigned Alignment = 1;
1284   // Keep track of which types of access we see
1285   bool SawUnorderedAtomic = false;
1286   bool SawNotAtomic = false;
1287   AAMDNodes AATags;
1288 
1289   const DataLayout &MDL = Preheader->getModule()->getDataLayout();
1290 
1291   bool IsKnownThreadLocalObject = false;
1292   if (SafetyInfo->MayThrow) {
1293     // If a loop can throw, we have to insert a store along each unwind edge.
1294     // That said, we can't actually make the unwind edge explicit. Therefore,
1295     // we have to prove that the store is dead along the unwind edge.  We do
1296     // this by proving that the caller can't have a reference to the object
1297     // after return and thus can't possibly load from the object.
1298     Value *Object = GetUnderlyingObject(SomePtr, MDL);
1299     if (!isKnownNonEscaping(Object, TLI))
1300       return false;
1301     // Subtlety: Alloca's aren't visible to callers, but *are* potentially
1302     // visible to other threads if captured and used during their lifetimes.
1303     IsKnownThreadLocalObject = !isa<AllocaInst>(Object);
1304   }
1305 
1306   // Check that all of the pointers in the alias set have the same type.  We
1307   // cannot (yet) promote a memory location that is loaded and stored in
1308   // different sizes.  While we are at it, collect alignment and AA info.
1309   for (Value *ASIV : PointerMustAliases) {
1310     // Check that all of the pointers in the alias set have the same type.  We
1311     // cannot (yet) promote a memory location that is loaded and stored in
1312     // different sizes.
1313     if (SomePtr->getType() != ASIV->getType())
1314       return false;
1315 
1316     for (User *U : ASIV->users()) {
1317       // Ignore instructions that are outside the loop.
1318       Instruction *UI = dyn_cast<Instruction>(U);
1319       if (!UI || !CurLoop->contains(UI))
1320         continue;
1321 
1322       // If there is an non-load/store instruction in the loop, we can't promote
1323       // it.
1324       if (LoadInst *Load = dyn_cast<LoadInst>(UI)) {
1325         assert(!Load->isVolatile() && "AST broken");
1326         if (!Load->isUnordered())
1327           return false;
1328 
1329         SawUnorderedAtomic |= Load->isAtomic();
1330         SawNotAtomic |= !Load->isAtomic();
1331 
1332         if (!DereferenceableInPH)
1333           DereferenceableInPH = isSafeToExecuteUnconditionally(
1334               *Load, DT, CurLoop, SafetyInfo, ORE, Preheader->getTerminator());
1335       } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) {
1336         // Stores *of* the pointer are not interesting, only stores *to* the
1337         // pointer.
1338         if (UI->getOperand(1) != ASIV)
1339           continue;
1340         assert(!Store->isVolatile() && "AST broken");
1341         if (!Store->isUnordered())
1342           return false;
1343 
1344         SawUnorderedAtomic |= Store->isAtomic();
1345         SawNotAtomic |= !Store->isAtomic();
1346 
1347         // If the store is guaranteed to execute, both properties are satisfied.
1348         // We may want to check if a store is guaranteed to execute even if we
1349         // already know that promotion is safe, since it may have higher
1350         // alignment than any other guaranteed stores, in which case we can
1351         // raise the alignment on the promoted store.
1352         unsigned InstAlignment = Store->getAlignment();
1353         if (!InstAlignment)
1354           InstAlignment =
1355               MDL.getABITypeAlignment(Store->getValueOperand()->getType());
1356 
1357         if (!DereferenceableInPH || !SafeToInsertStore ||
1358             (InstAlignment > Alignment)) {
1359           if (isGuaranteedToExecute(*UI, DT, CurLoop, SafetyInfo)) {
1360             DereferenceableInPH = true;
1361             SafeToInsertStore = true;
1362             Alignment = std::max(Alignment, InstAlignment);
1363           }
1364         }
1365 
1366         // If a store dominates all exit blocks, it is safe to sink.
1367         // As explained above, if an exit block was executed, a dominating
1368         // store must have been been executed at least once, so we are not
1369         // introducing stores on paths that did not have them.
1370         // Note that this only looks at explicit exit blocks. If we ever
1371         // start sinking stores into unwind edges (see above), this will break.
1372         if (!SafeToInsertStore)
1373           SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) {
1374             return DT->dominates(Store->getParent(), Exit);
1375           });
1376 
1377         // If the store is not guaranteed to execute, we may still get
1378         // deref info through it.
1379         if (!DereferenceableInPH) {
1380           DereferenceableInPH = isDereferenceableAndAlignedPointer(
1381               Store->getPointerOperand(), Store->getAlignment(), MDL,
1382               Preheader->getTerminator(), DT);
1383         }
1384       } else
1385         return false; // Not a load or store.
1386 
1387       // Merge the AA tags.
1388       if (LoopUses.empty()) {
1389         // On the first load/store, just take its AA tags.
1390         UI->getAAMetadata(AATags);
1391       } else if (AATags) {
1392         UI->getAAMetadata(AATags, /* Merge = */ true);
1393       }
1394 
1395       LoopUses.push_back(UI);
1396     }
1397   }
1398 
1399   // If we found both an unordered atomic instruction and a non-atomic memory
1400   // access, bail.  We can't blindly promote non-atomic to atomic since we
1401   // might not be able to lower the result.  We can't downgrade since that
1402   // would violate memory model.  Also, align 0 is an error for atomics.
1403   if (SawUnorderedAtomic && SawNotAtomic)
1404     return false;
1405 
1406   // If we couldn't prove we can hoist the load, bail.
1407   if (!DereferenceableInPH)
1408     return false;
1409 
1410   // We know we can hoist the load, but don't have a guaranteed store.
1411   // Check whether the location is thread-local. If it is, then we can insert
1412   // stores along paths which originally didn't have them without violating the
1413   // memory model.
1414   if (!SafeToInsertStore) {
1415     if (IsKnownThreadLocalObject)
1416       SafeToInsertStore = true;
1417     else {
1418       Value *Object = GetUnderlyingObject(SomePtr, MDL);
1419       SafeToInsertStore =
1420           (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) &&
1421           !PointerMayBeCaptured(Object, true, true);
1422     }
1423   }
1424 
1425   // If we've still failed to prove we can sink the store, give up.
1426   if (!SafeToInsertStore)
1427     return false;
1428 
1429   // Otherwise, this is safe to promote, lets do it!
1430   DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr
1431                << '\n');
1432   ORE->emit([&]() {
1433     return OptimizationRemark(DEBUG_TYPE, "PromoteLoopAccessesToScalar",
1434                               LoopUses[0])
1435            << "Moving accesses to memory location out of the loop";
1436   });
1437   ++NumPromoted;
1438 
1439   // Grab a debug location for the inserted loads/stores; given that the
1440   // inserted loads/stores have little relation to the original loads/stores,
1441   // this code just arbitrarily picks a location from one, since any debug
1442   // location is better than none.
1443   DebugLoc DL = LoopUses[0]->getDebugLoc();
1444 
1445   // We use the SSAUpdater interface to insert phi nodes as required.
1446   SmallVector<PHINode *, 16> NewPHIs;
1447   SSAUpdater SSA(&NewPHIs);
1448   LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks,
1449                         InsertPts, PIC, *CurAST, *LI, DL, Alignment,
1450                         SawUnorderedAtomic, AATags);
1451 
1452   // Set up the preheader to have a definition of the value.  It is the live-out
1453   // value from the preheader that uses in the loop will use.
1454   LoadInst *PreheaderLoad = new LoadInst(
1455       SomePtr, SomePtr->getName() + ".promoted", Preheader->getTerminator());
1456   if (SawUnorderedAtomic)
1457     PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
1458   PreheaderLoad->setAlignment(Alignment);
1459   PreheaderLoad->setDebugLoc(DL);
1460   if (AATags)
1461     PreheaderLoad->setAAMetadata(AATags);
1462   SSA.AddAvailableValue(Preheader, PreheaderLoad);
1463 
1464   // Rewrite all the loads in the loop and remember all the definitions from
1465   // stores in the loop.
1466   Promoter.run(LoopUses);
1467 
1468   // If the SSAUpdater didn't use the load in the preheader, just zap it now.
1469   if (PreheaderLoad->use_empty())
1470     PreheaderLoad->eraseFromParent();
1471 
1472   return true;
1473 }
1474 
1475 /// Returns an owning pointer to an alias set which incorporates aliasing info
1476 /// from L and all subloops of L.
1477 /// FIXME: In new pass manager, there is no helper function to handle loop
1478 /// analysis such as cloneBasicBlockAnalysis, so the AST needs to be recomputed
1479 /// from scratch for every loop. Hook up with the helper functions when
1480 /// available in the new pass manager to avoid redundant computation.
1481 AliasSetTracker *
1482 LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
1483                                                  AliasAnalysis *AA) {
1484   AliasSetTracker *CurAST = nullptr;
1485   SmallVector<Loop *, 4> RecomputeLoops;
1486   for (Loop *InnerL : L->getSubLoops()) {
1487     auto MapI = LoopToAliasSetMap.find(InnerL);
1488     // If the AST for this inner loop is missing it may have been merged into
1489     // some other loop's AST and then that loop unrolled, and so we need to
1490     // recompute it.
1491     if (MapI == LoopToAliasSetMap.end()) {
1492       RecomputeLoops.push_back(InnerL);
1493       continue;
1494     }
1495     AliasSetTracker *InnerAST = MapI->second;
1496 
1497     if (CurAST != nullptr) {
1498       // What if InnerLoop was modified by other passes ?
1499       CurAST->add(*InnerAST);
1500 
1501       // Once we've incorporated the inner loop's AST into ours, we don't need
1502       // the subloop's anymore.
1503       delete InnerAST;
1504     } else {
1505       CurAST = InnerAST;
1506     }
1507     LoopToAliasSetMap.erase(MapI);
1508   }
1509   if (CurAST == nullptr)
1510     CurAST = new AliasSetTracker(*AA);
1511 
1512   auto mergeLoop = [&](Loop *L) {
1513     // Loop over the body of this loop, looking for calls, invokes, and stores.
1514     for (BasicBlock *BB : L->blocks())
1515       CurAST->add(*BB); // Incorporate the specified basic block
1516   };
1517 
1518   // Add everything from the sub loops that are no longer directly available.
1519   for (Loop *InnerL : RecomputeLoops)
1520     mergeLoop(InnerL);
1521 
1522   // And merge in this loop.
1523   mergeLoop(L);
1524 
1525   return CurAST;
1526 }
1527 
1528 /// Simple analysis hook. Clone alias set info.
1529 ///
1530 void LegacyLICMPass::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
1531                                              Loop *L) {
1532   AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1533   if (!AST)
1534     return;
1535 
1536   AST->copyValue(From, To);
1537 }
1538 
1539 /// Simple Analysis hook. Delete value V from alias set
1540 ///
1541 void LegacyLICMPass::deleteAnalysisValue(Value *V, Loop *L) {
1542   AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1543   if (!AST)
1544     return;
1545 
1546   AST->deleteValue(V);
1547 }
1548 
1549 /// Simple Analysis hook. Delete value L from alias set map.
1550 ///
1551 void LegacyLICMPass::deleteAnalysisLoop(Loop *L) {
1552   AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1553   if (!AST)
1554     return;
1555 
1556   delete AST;
1557   LICM.getLoopToAliasSetMap().erase(L);
1558 }
1559 
1560 /// Return true if the body of this loop may store into the memory
1561 /// location pointed to by V.
1562 ///
1563 static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
1564                                      const AAMDNodes &AAInfo,
1565                                      AliasSetTracker *CurAST) {
1566   // Check to see if any of the basic blocks in CurLoop invalidate *V.
1567   return CurAST->getAliasSetForPointer(V, Size, AAInfo).isMod();
1568 }
1569 
1570 /// Little predicate that returns true if the specified basic block is in
1571 /// a subloop of the current one, not the current one itself.
1572 ///
1573 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) {
1574   assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
1575   return LI->getLoopFor(BB) != CurLoop;
1576 }
1577