LLVM  4.0.0
LICM.cpp
Go to the documentation of this file.
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 
34 #include "llvm/ADT/Statistic.h"
41 #include "llvm/Analysis/Loads.h"
42 #include "llvm/Analysis/LoopInfo.h"
43 #include "llvm/Analysis/LoopPass.h"
50 #include "llvm/IR/CFG.h"
51 #include "llvm/IR/Constants.h"
52 #include "llvm/IR/DataLayout.h"
53 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/Dominators.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/IntrinsicInst.h"
57 #include "llvm/IR/LLVMContext.h"
58 #include "llvm/IR/Metadata.h"
61 #include "llvm/Support/Debug.h"
63 #include "llvm/Transforms/Scalar.h"
68 #include <algorithm>
69 #include <utility>
70 using namespace llvm;
71 
72 #define DEBUG_TYPE "licm"
73 
74 STATISTIC(NumSunk, "Number of instructions sunk out of loop");
75 STATISTIC(NumHoisted, "Number of instructions hoisted out of loop");
76 STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
77 STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
78 STATISTIC(NumPromoted, "Number of memory locations promoted to registers");
79 
80 static cl::opt<bool>
81  DisablePromotion("disable-licm-promotion", cl::Hidden,
82  cl::desc("Disable memory promotion in LICM pass"));
83 
84 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
85 static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop,
86  const LoopSafetyInfo *SafetyInfo);
87 static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
88  const LoopSafetyInfo *SafetyInfo,
90 static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
91  const Loop *CurLoop, AliasSetTracker *CurAST,
92  const LoopSafetyInfo *SafetyInfo,
95  const DominatorTree *DT,
96  const Loop *CurLoop,
97  const LoopSafetyInfo *SafetyInfo,
99  const Instruction *CtxI = nullptr);
100 static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
101  const AAMDNodes &AAInfo,
102  AliasSetTracker *CurAST);
103 static Instruction *
105  const LoopInfo *LI,
106  const LoopSafetyInfo *SafetyInfo);
107 
108 namespace {
109 struct LoopInvariantCodeMotion {
110  bool runOnLoop(Loop *L, AliasAnalysis *AA, LoopInfo *LI, DominatorTree *DT,
112  OptimizationRemarkEmitter *ORE, bool DeleteAST);
113 
114  DenseMap<Loop *, AliasSetTracker *> &getLoopToAliasSetMap() {
115  return LoopToAliasSetMap;
116  }
117 
118 private:
119  DenseMap<Loop *, AliasSetTracker *> LoopToAliasSetMap;
120 
121  AliasSetTracker *collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
122  AliasAnalysis *AA);
123 };
124 
125 struct LegacyLICMPass : public LoopPass {
126  static char ID; // Pass identification, replacement for typeid
127  LegacyLICMPass() : LoopPass(ID) {
129  }
130 
131  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
132  if (skipLoop(L)) {
133  // If we have run LICM on a previous loop but now we are skipping
134  // (because we've hit the opt-bisect limit), we need to clear the
135  // loop alias information.
136  for (auto &LTAS : LICM.getLoopToAliasSetMap())
137  delete LTAS.second;
138  LICM.getLoopToAliasSetMap().clear();
139  return false;
140  }
141 
142  auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
143  // For the old PM, we can't use OptimizationRemarkEmitter as an analysis
144  // pass. Function analyses need to be preserved across loop transformations
145  // but ORE cannot be preserved (see comment before the pass definition).
146  OptimizationRemarkEmitter ORE(L->getHeader()->getParent());
147  return LICM.runOnLoop(L,
148  &getAnalysis<AAResultsWrapperPass>().getAAResults(),
149  &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
150  &getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
151  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
152  SE ? &SE->getSE() : nullptr, &ORE, false);
153  }
154 
155  /// This transformation requires natural loop information & requires that
156  /// loop preheaders be inserted into the CFG...
157  ///
158  void getAnalysisUsage(AnalysisUsage &AU) const override {
159  AU.setPreservesCFG();
162  }
163 
165 
166  bool doFinalization() override {
167  assert(LICM.getLoopToAliasSetMap().empty() &&
168  "Didn't free loop alias sets");
169  return false;
170  }
171 
172 private:
173  LoopInvariantCodeMotion LICM;
174 
175  /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
176  void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
177  Loop *L) override;
178 
179  /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
180  /// set.
181  void deleteAnalysisValue(Value *V, Loop *L) override;
182 
183  /// Simple Analysis hook. Delete loop L from alias set map.
184  void deleteAnalysisLoop(Loop *L) override;
185 };
186 }
187 
190  const auto &FAM =
191  AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager();
192  Function *F = L.getHeader()->getParent();
193 
194  auto *ORE = FAM.getCachedResult<OptimizationRemarkEmitterAnalysis>(*F);
195  // FIXME: This should probably be optional rather than required.
196  if (!ORE)
197  report_fatal_error("LICM: OptimizationRemarkEmitterAnalysis not "
198  "cached at a higher level");
199 
200  LoopInvariantCodeMotion LICM;
201  if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, &AR.TLI, &AR.SE, ORE, true))
202  return PreservedAnalyses::all();
203 
204  // FIXME: There is no setPreservesCFG in the new PM. When that becomes
205  // available, it should be used here.
207 }
208 
209 char LegacyLICMPass::ID = 0;
210 INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",
211  false, false)
214 INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false,
215  false)
216 
217 Pass *llvm::createLICMPass() { return new LegacyLICMPass(); }
218 
219 /// Hoist expressions out of the specified loop. Note, alias info for inner
220 /// loop is not preserved so it is not a good idea to run LICM multiple
221 /// times on one loop.
222 /// We should delete AST for inner loops in the new pass manager to avoid
223 /// memory leak.
224 ///
225 bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AliasAnalysis *AA,
226  LoopInfo *LI, DominatorTree *DT,
227  TargetLibraryInfo *TLI,
228  ScalarEvolution *SE,
230  bool DeleteAST) {
231  bool Changed = false;
232 
233  assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
234 
235  AliasSetTracker *CurAST = collectAliasInfoForLoop(L, LI, AA);
236 
237  // Get the preheader block to move instructions into...
238  BasicBlock *Preheader = L->getLoopPreheader();
239 
240  // Compute loop safety information.
241  LoopSafetyInfo SafetyInfo;
242  computeLoopSafetyInfo(&SafetyInfo, L);
243 
244  // We want to visit all of the instructions in this loop... that are not parts
245  // of our subloops (they have already had their invariants hoisted out of
246  // their loop, into this loop, so there is no need to process the BODIES of
247  // the subloops).
248  //
249  // Traverse the body of the loop in depth first order on the dominator tree so
250  // that we are guaranteed to see definitions before we see uses. This allows
251  // us to sink instructions in one pass, without iteration. After sinking
252  // instructions, we perform another pass to hoist them out of the loop.
253  //
254  if (L->hasDedicatedExits())
255  Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, L,
256  CurAST, &SafetyInfo, ORE);
257  if (Preheader)
258  Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, L,
259  CurAST, &SafetyInfo, ORE);
260 
261  // Now that all loop invariants have been removed from the loop, promote any
262  // memory references to scalars that we can.
263  // Don't sink stores from loops without dedicated block exits. Exits
264  // containing indirect branches are not transformed by loop simplify,
265  // make sure we catch that. An additional load may be generated in the
266  // preheader for SSA updater, so also avoid sinking when no preheader
267  // is available.
268  if (!DisablePromotion && Preheader && L->hasDedicatedExits()) {
269  // Figure out the loop exits and their insertion points
270  SmallVector<BasicBlock *, 8> ExitBlocks;
271  L->getUniqueExitBlocks(ExitBlocks);
272 
273  // We can't insert into a catchswitch.
274  bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) {
275  return isa<CatchSwitchInst>(Exit->getTerminator());
276  });
277 
278  if (!HasCatchSwitch) {
280  InsertPts.reserve(ExitBlocks.size());
281  for (BasicBlock *ExitBlock : ExitBlocks)
282  InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
283 
284  PredIteratorCache PIC;
285 
286  bool Promoted = false;
287 
288  // Loop over all of the alias sets in the tracker object.
289  for (AliasSet &AS : *CurAST)
290  Promoted |=
291  promoteLoopAccessesToScalars(AS, ExitBlocks, InsertPts, PIC, LI, DT,
292  TLI, L, CurAST, &SafetyInfo, ORE);
293 
294  // Once we have promoted values across the loop body we have to
295  // recursively reform LCSSA as any nested loop may now have values defined
296  // within the loop used in the outer loop.
297  // FIXME: This is really heavy handed. It would be a bit better to use an
298  // SSAUpdater strategy during promotion that was LCSSA aware and reformed
299  // it as it went.
300  if (Promoted)
301  formLCSSARecursively(*L, *DT, LI, SE);
302 
303  Changed |= Promoted;
304  }
305  }
306 
307  // Check that neither this loop nor its parent have had LCSSA broken. LICM is
308  // specifically moving instructions across the loop boundary and so it is
309  // especially in need of sanity checking here.
310  assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!");
311  assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&
312  "Parent loop not left in LCSSA form after LICM!");
313 
314  // If this loop is nested inside of another one, save the alias information
315  // for when we process the outer loop.
316  if (L->getParentLoop() && !DeleteAST)
317  LoopToAliasSetMap[L] = CurAST;
318  else
319  delete CurAST;
320 
321  if (Changed && SE)
322  SE->forgetLoopDispositions(L);
323  return Changed;
324 }
325 
326 /// Walk the specified region of the CFG (defined by all blocks dominated by
327 /// the specified block, and that are in the current loop) in reverse depth
328 /// first order w.r.t the DominatorTree. This allows us to visit uses before
329 /// definitions, allowing us to sink a loop body in one pass without iteration.
330 ///
332  DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
333  AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
335 
336  // Verify inputs.
337  assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
338  CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr &&
339  "Unexpected input to sinkRegion");
340 
341  BasicBlock *BB = N->getBlock();
342  // If this subregion is not in the top level loop at all, exit.
343  if (!CurLoop->contains(BB))
344  return false;
345 
346  // We are processing blocks in reverse dfo, so process children first.
347  bool Changed = false;
348  const std::vector<DomTreeNode *> &Children = N->getChildren();
349  for (DomTreeNode *Child : Children)
350  Changed |=
351  sinkRegion(Child, AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo, ORE);
352 
353  // Only need to process the contents of this block if it is not part of a
354  // subloop (which would already have been processed).
355  if (inSubLoop(BB, CurLoop, LI))
356  return Changed;
357 
358  for (BasicBlock::iterator II = BB->end(); II != BB->begin();) {
359  Instruction &I = *--II;
360 
361  // If the instruction is dead, we would try to sink it because it isn't used
362  // in the loop, instead, just delete it.
363  if (isInstructionTriviallyDead(&I, TLI)) {
364  DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n');
365  ++II;
366  CurAST->deleteValue(&I);
367  I.eraseFromParent();
368  Changed = true;
369  continue;
370  }
371 
372  // Check to see if we can sink this instruction to the exit blocks
373  // of the loop. We can do this if the all users of the instruction are
374  // outside of the loop. In this case, it doesn't even matter if the
375  // operands of the instruction are loop invariant.
376  //
377  if (isNotUsedInLoop(I, CurLoop, SafetyInfo) &&
378  canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, SafetyInfo, ORE)) {
379  ++II;
380  Changed |= sink(I, LI, DT, CurLoop, CurAST, SafetyInfo, ORE);
381  }
382  }
383  return Changed;
384 }
385 
386 /// Walk the specified region of the CFG (defined by all blocks dominated by
387 /// the specified block, and that are in the current loop) in depth first
388 /// order w.r.t the DominatorTree. This allows us to visit definitions before
389 /// uses, allowing us to hoist a loop body in one pass without iteration.
390 ///
392  DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
393  AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
395  // Verify inputs.
396  assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
397  CurLoop != nullptr && CurAST != nullptr && SafetyInfo != nullptr &&
398  "Unexpected input to hoistRegion");
399 
400  BasicBlock *BB = N->getBlock();
401 
402  // If this subregion is not in the top level loop at all, exit.
403  if (!CurLoop->contains(BB))
404  return false;
405 
406  // Only need to process the contents of this block if it is not part of a
407  // subloop (which would already have been processed).
408  bool Changed = false;
409  if (!inSubLoop(BB, CurLoop, LI))
410  for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
411  Instruction &I = *II++;
412  // Try constant folding this instruction. If all the operands are
413  // constants, it is technically hoistable, but it would be better to just
414  // fold it.
416  &I, I.getModule()->getDataLayout(), TLI)) {
417  DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n');
418  CurAST->copyValue(&I, C);
420  if (isInstructionTriviallyDead(&I, TLI)) {
421  CurAST->deleteValue(&I);
422  I.eraseFromParent();
423  }
424  Changed = true;
425  continue;
426  }
427 
428  // Try hoisting the instruction out to the preheader. We can only do this
429  // if all of the operands of the instruction are loop invariant and if it
430  // is safe to hoist the instruction.
431  //
432  if (CurLoop->hasLoopInvariantOperands(&I) &&
433  canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, SafetyInfo, ORE) &&
435  I, DT, CurLoop, SafetyInfo, ORE,
436  CurLoop->getLoopPreheader()->getTerminator()))
437  Changed |= hoist(I, DT, CurLoop, SafetyInfo, ORE);
438  }
439 
440  const std::vector<DomTreeNode *> &Children = N->getChildren();
441  for (DomTreeNode *Child : Children)
442  Changed |=
443  hoistRegion(Child, AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo, ORE);
444  return Changed;
445 }
446 
447 /// Computes loop safety information, checks loop body & header
448 /// for the possibility of may throw exception.
449 ///
450 void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
451  assert(CurLoop != nullptr && "CurLoop cant be null");
452  BasicBlock *Header = CurLoop->getHeader();
453  // Setting default safety values.
454  SafetyInfo->MayThrow = false;
455  SafetyInfo->HeaderMayThrow = false;
456  // Iterate over header and compute safety info.
457  for (BasicBlock::iterator I = Header->begin(), E = Header->end();
458  (I != E) && !SafetyInfo->HeaderMayThrow; ++I)
459  SafetyInfo->HeaderMayThrow |=
461 
462  SafetyInfo->MayThrow = SafetyInfo->HeaderMayThrow;
463  // Iterate over loop instructions and compute safety info.
464  for (Loop::block_iterator BB = CurLoop->block_begin(),
465  BBE = CurLoop->block_end();
466  (BB != BBE) && !SafetyInfo->MayThrow; ++BB)
467  for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
468  (I != E) && !SafetyInfo->MayThrow; ++I)
470 
471  // Compute funclet colors if we might sink/hoist in a function with a funclet
472  // personality routine.
473  Function *Fn = CurLoop->getHeader()->getParent();
474  if (Fn->hasPersonalityFn())
475  if (Constant *PersonalityFn = Fn->getPersonalityFn())
476  if (isFuncletEHPersonality(classifyEHPersonality(PersonalityFn)))
477  SafetyInfo->BlockColors = colorEHFunclets(*Fn);
478 }
479 
481  Loop *CurLoop, AliasSetTracker *CurAST,
482  LoopSafetyInfo *SafetyInfo,
484  // Loads have extra constraints we have to verify before we can hoist them.
485  if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
486  if (!LI->isUnordered())
487  return false; // Don't hoist volatile/atomic loads!
488 
489  // Loads from constant memory are always safe to move, even if they end up
490  // in the same alias set as something that ends up being modified.
491  if (AA->pointsToConstantMemory(LI->getOperand(0)))
492  return true;
493  if (LI->getMetadata(LLVMContext::MD_invariant_load))
494  return true;
495 
496  // Don't hoist loads which have may-aliased stores in loop.
497  uint64_t Size = 0;
498  if (LI->getType()->isSized())
499  Size = I.getModule()->getDataLayout().getTypeStoreSize(LI->getType());
500 
501  AAMDNodes AAInfo;
502  LI->getAAMetadata(AAInfo);
503 
504  bool Invalidated =
505  pointerInvalidatedByLoop(LI->getOperand(0), Size, AAInfo, CurAST);
506  // Check loop-invariant address because this may also be a sinkable load
507  // whose address is not necessarily loop-invariant.
508  if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand()))
510  DEBUG_TYPE, "LoadWithLoopInvariantAddressInvalidated", LI)
511  << "failed to move load with loop-invariant address "
512  "because the loop may invalidate its value");
513 
514  return !Invalidated;
515  } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
516  // Don't sink or hoist dbg info; it's legal, but not useful.
517  if (isa<DbgInfoIntrinsic>(I))
518  return false;
519 
520  // Don't sink calls which can throw.
521  if (CI->mayThrow())
522  return false;
523 
524  // Handle simple cases by querying alias analysis.
525  FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI);
526  if (Behavior == FMRB_DoesNotAccessMemory)
527  return true;
528  if (AliasAnalysis::onlyReadsMemory(Behavior)) {
529  // A readonly argmemonly function only reads from memory pointed to by
530  // it's arguments with arbitrary offsets. If we can prove there are no
531  // writes to this memory in the loop, we can hoist or sink.
533  for (Value *Op : CI->arg_operands())
534  if (Op->getType()->isPointerTy() &&
536  AAMDNodes(), CurAST))
537  return false;
538  return true;
539  }
540  // If this call only reads from memory and there are no writes to memory
541  // in the loop, we can hoist or sink the call as appropriate.
542  bool FoundMod = false;
543  for (AliasSet &AS : *CurAST) {
544  if (!AS.isForwardingAliasSet() && AS.isMod()) {
545  FoundMod = true;
546  break;
547  }
548  }
549  if (!FoundMod)
550  return true;
551  }
552 
553  // FIXME: This should use mod/ref information to see if we can hoist or
554  // sink the call.
555 
556  return false;
557  }
558 
559  // Only these instructions are hoistable/sinkable.
560  if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) &&
561  !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) &&
562  !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) &&
563  !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) &&
564  !isa<InsertValueInst>(I))
565  return false;
566 
567  // SafetyInfo is nullptr if we are checking for sinking from preheader to
568  // loop body. It will be always safe as there is no speculative execution.
569  if (!SafetyInfo)
570  return true;
571 
572  // TODO: Plumb the context instruction through to make hoisting and sinking
573  // more powerful. Hoisting of loads already works due to the special casing
574  // above.
575  return isSafeToExecuteUnconditionally(I, DT, CurLoop, SafetyInfo, nullptr);
576 }
577 
578 /// Returns true if a PHINode is a trivially replaceable with an
579 /// Instruction.
580 /// This is true when all incoming values are that instruction.
581 /// This pattern occurs most often with LCSSA PHI nodes.
582 ///
583 static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I) {
584  for (const Value *IncValue : PN.incoming_values())
585  if (IncValue != &I)
586  return false;
587 
588  return true;
589 }
590 
591 /// Return true if the only users of this instruction are outside of
592 /// the loop. If this is true, we can sink the instruction to the exit
593 /// blocks of the loop.
594 ///
595 static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop,
596  const LoopSafetyInfo *SafetyInfo) {
597  const auto &BlockColors = SafetyInfo->BlockColors;
598  for (const User *U : I.users()) {
599  const Instruction *UI = cast<Instruction>(U);
600  if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
601  const BasicBlock *BB = PN->getParent();
602  // We cannot sink uses in catchswitches.
603  if (isa<CatchSwitchInst>(BB->getTerminator()))
604  return false;
605 
606  // We need to sink a callsite to a unique funclet. Avoid sinking if the
607  // phi use is too muddled.
608  if (isa<CallInst>(I))
609  if (!BlockColors.empty() &&
610  BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1)
611  return false;
612 
613  // A PHI node where all of the incoming values are this instruction are
614  // special -- they can just be RAUW'ed with the instruction and thus
615  // don't require a use in the predecessor. This is a particular important
616  // special case because it is the pattern found in LCSSA form.
617  if (isTriviallyReplacablePHI(*PN, I)) {
618  if (CurLoop->contains(PN))
619  return false;
620  else
621  continue;
622  }
623 
624  // Otherwise, PHI node uses occur in predecessor blocks if the incoming
625  // values. Check for such a use being inside the loop.
626  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
627  if (PN->getIncomingValue(i) == &I)
628  if (CurLoop->contains(PN->getIncomingBlock(i)))
629  return false;
630 
631  continue;
632  }
633 
634  if (CurLoop->contains(UI))
635  return false;
636  }
637  return true;
638 }
639 
640 static Instruction *
642  const LoopInfo *LI,
643  const LoopSafetyInfo *SafetyInfo) {
644  Instruction *New;
645  if (auto *CI = dyn_cast<CallInst>(&I)) {
646  const auto &BlockColors = SafetyInfo->BlockColors;
647 
648  // Sinking call-sites need to be handled differently from other
649  // instructions. The cloned call-site needs a funclet bundle operand
650  // appropriate for it's location in the CFG.
652  for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles();
653  BundleIdx != BundleEnd; ++BundleIdx) {
654  OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx);
655  if (Bundle.getTagID() == LLVMContext::OB_funclet)
656  continue;
657 
658  OpBundles.emplace_back(Bundle);
659  }
660 
661  if (!BlockColors.empty()) {
662  const ColorVector &CV = BlockColors.find(&ExitBlock)->second;
663  assert(CV.size() == 1 && "non-unique color for exit block!");
664  BasicBlock *BBColor = CV.front();
665  Instruction *EHPad = BBColor->getFirstNonPHI();
666  if (EHPad->isEHPad())
667  OpBundles.emplace_back("funclet", EHPad);
668  }
669 
670  New = CallInst::Create(CI, OpBundles);
671  } else {
672  New = I.clone();
673  }
674 
675  ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
676  if (!I.getName().empty())
677  New->setName(I.getName() + ".le");
678 
679  // Build LCSSA PHI nodes for any in-loop operands. Note that this is
680  // particularly cheap because we can rip off the PHI node that we're
681  // replacing for the number and blocks of the predecessors.
682  // OPT: If this shows up in a profile, we can instead finish sinking all
683  // invariant instructions, and then walk their operands to re-establish
684  // LCSSA. That will eliminate creating PHI nodes just to nuke them when
685  // sinking bottom-up.
686  for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE;
687  ++OI)
688  if (Instruction *OInst = dyn_cast<Instruction>(*OI))
689  if (Loop *OLoop = LI->getLoopFor(OInst->getParent()))
690  if (!OLoop->contains(&PN)) {
691  PHINode *OpPN =
692  PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
693  OInst->getName() + ".lcssa", &ExitBlock.front());
694  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
695  OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
696  *OI = OpPN;
697  }
698  return New;
699 }
700 
701 /// When an instruction is found to only be used outside of the loop, this
702 /// function moves it to the exit blocks and patches up SSA form as needed.
703 /// This method is guaranteed to remove the original instruction from its
704 /// position, and may either delete it or move it to outside of the loop.
705 ///
706 static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
707  const Loop *CurLoop, AliasSetTracker *CurAST,
708  const LoopSafetyInfo *SafetyInfo,
710  DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
711  ORE->emit(OptimizationRemark(DEBUG_TYPE, "InstSunk", &I)
712  << "sinking " << ore::NV("Inst", &I));
713  bool Changed = false;
714  if (isa<LoadInst>(I))
715  ++NumMovedLoads;
716  else if (isa<CallInst>(I))
717  ++NumMovedCalls;
718  ++NumSunk;
719  Changed = true;
720 
721 #ifndef NDEBUG
723  CurLoop->getUniqueExitBlocks(ExitBlocks);
724  SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
725  ExitBlocks.end());
726 #endif
727 
728  // Clones of this instruction. Don't create more than one per exit block!
730 
731  // If this instruction is only used outside of the loop, then all users are
732  // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
733  // the instruction.
734  while (!I.use_empty()) {
736  auto *User = cast<Instruction>(*UI);
737  if (!DT->isReachableFromEntry(User->getParent())) {
739  continue;
740  }
741  // The user must be a PHI node.
742  PHINode *PN = cast<PHINode>(User);
743 
744  // Surprisingly, instructions can be used outside of loops without any
745  // exits. This can only happen in PHI nodes if the incoming block is
746  // unreachable.
747  Use &U = UI.getUse();
748  BasicBlock *BB = PN->getIncomingBlock(U);
749  if (!DT->isReachableFromEntry(BB)) {
750  U = UndefValue::get(I.getType());
751  continue;
752  }
753 
754  BasicBlock *ExitBlock = PN->getParent();
755  assert(ExitBlockSet.count(ExitBlock) &&
756  "The LCSSA PHI is not in an exit block!");
757 
758  Instruction *New;
759  auto It = SunkCopies.find(ExitBlock);
760  if (It != SunkCopies.end())
761  New = It->second;
762  else
763  New = SunkCopies[ExitBlock] =
764  CloneInstructionInExitBlock(I, *ExitBlock, *PN, LI, SafetyInfo);
765 
766  PN->replaceAllUsesWith(New);
767  PN->eraseFromParent();
768  }
769 
770  CurAST->deleteValue(&I);
771  I.eraseFromParent();
772  return Changed;
773 }
774 
775 /// When an instruction is found to only use loop invariant operands that
776 /// is safe to hoist, this instruction is called to do the dirty work.
777 ///
778 static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
779  const LoopSafetyInfo *SafetyInfo,
781  auto *Preheader = CurLoop->getLoopPreheader();
782  DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": " << I
783  << "\n");
784  ORE->emit(OptimizationRemark(DEBUG_TYPE, "Hoisted", &I)
785  << "hosting " << ore::NV("Inst", &I));
786 
787  // Metadata can be dependent on conditions we are hoisting above.
788  // Conservatively strip all metadata on the instruction unless we were
789  // guaranteed to execute I if we entered the loop, in which case the metadata
790  // is valid in the loop preheader.
792  // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning
793  // time in isGuaranteedToExecute if we don't actually have anything to
794  // drop. It is a compile time optimization, not required for correctness.
795  !isGuaranteedToExecute(I, DT, CurLoop, SafetyInfo))
797 
798  // Move the new node to the Preheader, before its terminator.
799  I.moveBefore(Preheader->getTerminator());
800 
801  // Do not retain debug locations when we are moving instructions to different
802  // basic blocks, because we want to avoid jumpy line tables. Calls, however,
803  // need to retain their debug locs because they may be inlined.
804  // FIXME: How do we retain source locations without causing poor debugging
805  // behavior?
806  if (!isa<CallInst>(I))
807  I.setDebugLoc(DebugLoc());
808 
809  if (isa<LoadInst>(I))
810  ++NumMovedLoads;
811  else if (isa<CallInst>(I))
812  ++NumMovedCalls;
813  ++NumHoisted;
814  return true;
815 }
816 
817 /// Only sink or hoist an instruction if it is not a trapping instruction,
818 /// or if the instruction is known not to trap when moved to the preheader.
819 /// or if it is a trapping instruction and is guaranteed to execute.
821  const DominatorTree *DT,
822  const Loop *CurLoop,
823  const LoopSafetyInfo *SafetyInfo,
825  const Instruction *CtxI) {
826  if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT))
827  return true;
828 
829  bool GuaranteedToExecute =
830  isGuaranteedToExecute(Inst, DT, CurLoop, SafetyInfo);
831 
832  if (!GuaranteedToExecute) {
833  auto *LI = dyn_cast<LoadInst>(&Inst);
834  if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand()))
836  DEBUG_TYPE, "LoadWithLoopInvariantAddressCondExecuted", LI)
837  << "failed to hoist load with loop-invariant address "
838  "because load is conditionally executed");
839  }
840 
841  return GuaranteedToExecute;
842 }
843 
844 namespace {
845 class LoopPromoter : public LoadAndStorePromoter {
846  Value *SomePtr; // Designated pointer to store to.
847  SmallPtrSetImpl<Value *> &PointerMustAliases;
848  SmallVectorImpl<BasicBlock *> &LoopExitBlocks;
849  SmallVectorImpl<Instruction *> &LoopInsertPts;
850  PredIteratorCache &PredCache;
851  AliasSetTracker &AST;
852  LoopInfo &LI;
853  DebugLoc DL;
854  int Alignment;
855  AAMDNodes AATags;
856 
857  Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
858  if (Instruction *I = dyn_cast<Instruction>(V))
859  if (Loop *L = LI.getLoopFor(I->getParent()))
860  if (!L->contains(BB)) {
861  // We need to create an LCSSA PHI node for the incoming value and
862  // store that.
863  PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB),
864  I->getName() + ".lcssa", &BB->front());
865  for (BasicBlock *Pred : PredCache.get(BB))
866  PN->addIncoming(I, Pred);
867  return PN;
868  }
869  return V;
870  }
871 
872 public:
873  LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S,
877  AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
878  const AAMDNodes &AATags)
879  : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
880  LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
881  LI(li), DL(std::move(dl)), Alignment(alignment), AATags(AATags) {}
882 
883  bool isInstInList(Instruction *I,
884  const SmallVectorImpl<Instruction *> &) const override {
885  Value *Ptr;
886  if (LoadInst *LI = dyn_cast<LoadInst>(I))
887  Ptr = LI->getOperand(0);
888  else
889  Ptr = cast<StoreInst>(I)->getPointerOperand();
890  return PointerMustAliases.count(Ptr);
891  }
892 
893  void doExtraRewritesBeforeFinalDeletion() const override {
894  // Insert stores after in the loop exit blocks. Each exit block gets a
895  // store of the live-out values that feed them. Since we've already told
896  // the SSA updater about the defs in the loop and the preheader
897  // definition, it is all set and we can start using it.
898  for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
899  BasicBlock *ExitBlock = LoopExitBlocks[i];
900  Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
901  LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
902  Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
903  Instruction *InsertPos = LoopInsertPts[i];
904  StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
905  NewSI->setAlignment(Alignment);
906  NewSI->setDebugLoc(DL);
907  if (AATags)
908  NewSI->setAAMetadata(AATags);
909  }
910  }
911 
912  void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
913  // Update alias analysis.
914  AST.copyValue(LI, V);
915  }
916  void instructionDeleted(Instruction *I) const override { AST.deleteValue(I); }
917 };
918 } // end anon namespace
919 
920 /// Try to promote memory values to scalars by sinking stores out of the
921 /// loop and moving loads to before the loop. We do this by looping over
922 /// the stores in the loop, looking for stores to Must pointers which are
923 /// loop invariant.
924 ///
926  AliasSet &AS, SmallVectorImpl<BasicBlock *> &ExitBlocks,
928  LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI,
929  Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
931  // Verify inputs.
932  assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&
933  CurAST != nullptr && SafetyInfo != nullptr &&
934  "Unexpected Input to promoteLoopAccessesToScalars");
935 
936  // We can promote this alias set if it has a store, if it is a "Must" alias
937  // set, if the pointer is loop invariant, and if we are not eliminating any
938  // volatile loads or stores.
939  if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
940  AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue()))
941  return false;
942 
943  assert(!AS.empty() &&
944  "Must alias set should have at least one pointer element in it!");
945 
946  Value *SomePtr = AS.begin()->getValue();
947  BasicBlock *Preheader = CurLoop->getLoopPreheader();
948 
949  // It isn't safe to promote a load/store from the loop if the load/store is
950  // conditional. For example, turning:
951  //
952  // for () { if (c) *P += 1; }
953  //
954  // into:
955  //
956  // tmp = *P; for () { if (c) tmp +=1; } *P = tmp;
957  //
958  // is not safe, because *P may only be valid to access if 'c' is true.
959  //
960  // The safety property divides into two parts:
961  // p1) The memory may not be dereferenceable on entry to the loop. In this
962  // case, we can't insert the required load in the preheader.
963  // p2) The memory model does not allow us to insert a store along any dynamic
964  // path which did not originally have one.
965  //
966  // If at least one store is guaranteed to execute, both properties are
967  // satisfied, and promotion is legal.
968  //
969  // This, however, is not a necessary condition. Even if no store/load is
970  // guaranteed to execute, we can still establish these properties.
971  // We can establish (p1) by proving that hoisting the load into the preheader
972  // is safe (i.e. proving dereferenceability on all paths through the loop). We
973  // can use any access within the alias set to prove dereferenceability,
974  // since they're all must alias.
975  //
976  // There are two ways establish (p2):
977  // a) Prove the location is thread-local. In this case the memory model
978  // requirement does not apply, and stores are safe to insert.
979  // b) Prove a store dominates every exit block. In this case, if an exit
980  // blocks is reached, the original dynamic path would have taken us through
981  // the store, so inserting a store into the exit block is safe. Note that this
982  // is different from the store being guaranteed to execute. For instance,
983  // if an exception is thrown on the first iteration of the loop, the original
984  // store is never executed, but the exit blocks are not executed either.
985 
986  bool DereferenceableInPH = false;
987  bool SafeToInsertStore = false;
988 
990  SmallPtrSet<Value *, 4> PointerMustAliases;
991 
992  // We start with an alignment of one and try to find instructions that allow
993  // us to prove better alignment.
994  unsigned Alignment = 1;
995  AAMDNodes AATags;
996 
997  const DataLayout &MDL = Preheader->getModule()->getDataLayout();
998 
999  if (SafetyInfo->MayThrow) {
1000  // If a loop can throw, we have to insert a store along each unwind edge.
1001  // That said, we can't actually make the unwind edge explicit. Therefore,
1002  // we have to prove that the store is dead along the unwind edge.
1003  //
1004  // Currently, this code just special-cases alloca instructions.
1005  if (!isa<AllocaInst>(GetUnderlyingObject(SomePtr, MDL)))
1006  return false;
1007  }
1008 
1009  // Check that all of the pointers in the alias set have the same type. We
1010  // cannot (yet) promote a memory location that is loaded and stored in
1011  // different sizes. While we are at it, collect alignment and AA info.
1012  for (const auto &ASI : AS) {
1013  Value *ASIV = ASI.getValue();
1014  PointerMustAliases.insert(ASIV);
1015 
1016  // Check that all of the pointers in the alias set have the same type. We
1017  // cannot (yet) promote a memory location that is loaded and stored in
1018  // different sizes.
1019  if (SomePtr->getType() != ASIV->getType())
1020  return false;
1021 
1022  for (User *U : ASIV->users()) {
1023  // Ignore instructions that are outside the loop.
1024  Instruction *UI = dyn_cast<Instruction>(U);
1025  if (!UI || !CurLoop->contains(UI))
1026  continue;
1027 
1028  // If there is an non-load/store instruction in the loop, we can't promote
1029  // it.
1030  if (LoadInst *Load = dyn_cast<LoadInst>(UI)) {
1031  assert(!Load->isVolatile() && "AST broken");
1032  if (!Load->isSimple())
1033  return false;
1034 
1035  if (!DereferenceableInPH)
1036  DereferenceableInPH = isSafeToExecuteUnconditionally(
1037  *Load, DT, CurLoop, SafetyInfo, ORE, Preheader->getTerminator());
1038  } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) {
1039  // Stores *of* the pointer are not interesting, only stores *to* the
1040  // pointer.
1041  if (UI->getOperand(1) != ASIV)
1042  continue;
1043  assert(!Store->isVolatile() && "AST broken");
1044  if (!Store->isSimple())
1045  return false;
1046 
1047  // If the store is guaranteed to execute, both properties are satisfied.
1048  // We may want to check if a store is guaranteed to execute even if we
1049  // already know that promotion is safe, since it may have higher
1050  // alignment than any other guaranteed stores, in which case we can
1051  // raise the alignment on the promoted store.
1052  unsigned InstAlignment = Store->getAlignment();
1053  if (!InstAlignment)
1054  InstAlignment =
1055  MDL.getABITypeAlignment(Store->getValueOperand()->getType());
1056 
1057  if (!DereferenceableInPH || !SafeToInsertStore ||
1058  (InstAlignment > Alignment)) {
1059  if (isGuaranteedToExecute(*UI, DT, CurLoop, SafetyInfo)) {
1060  DereferenceableInPH = true;
1061  SafeToInsertStore = true;
1062  Alignment = std::max(Alignment, InstAlignment);
1063  }
1064  }
1065 
1066  // If a store dominates all exit blocks, it is safe to sink.
1067  // As explained above, if an exit block was executed, a dominating
1068  // store must have been been executed at least once, so we are not
1069  // introducing stores on paths that did not have them.
1070  // Note that this only looks at explicit exit blocks. If we ever
1071  // start sinking stores into unwind edges (see above), this will break.
1072  if (!SafeToInsertStore)
1073  SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) {
1074  return DT->dominates(Store->getParent(), Exit);
1075  });
1076 
1077  // If the store is not guaranteed to execute, we may still get
1078  // deref info through it.
1079  if (!DereferenceableInPH) {
1080  DereferenceableInPH = isDereferenceableAndAlignedPointer(
1081  Store->getPointerOperand(), Store->getAlignment(), MDL,
1082  Preheader->getTerminator(), DT);
1083  }
1084  } else
1085  return false; // Not a load or store.
1086 
1087  // Merge the AA tags.
1088  if (LoopUses.empty()) {
1089  // On the first load/store, just take its AA tags.
1090  UI->getAAMetadata(AATags);
1091  } else if (AATags) {
1092  UI->getAAMetadata(AATags, /* Merge = */ true);
1093  }
1094 
1095  LoopUses.push_back(UI);
1096  }
1097  }
1098 
1099 
1100  // If we couldn't prove we can hoist the load, bail.
1101  if (!DereferenceableInPH)
1102  return false;
1103 
1104  // We know we can hoist the load, but don't have a guaranteed store.
1105  // Check whether the location is thread-local. If it is, then we can insert
1106  // stores along paths which originally didn't have them without violating the
1107  // memory model.
1108  if (!SafeToInsertStore) {
1109  Value *Object = GetUnderlyingObject(SomePtr, MDL);
1110  SafeToInsertStore =
1111  (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) &&
1112  !PointerMayBeCaptured(Object, true, true);
1113  }
1114 
1115  // If we've still failed to prove we can sink the store, give up.
1116  if (!SafeToInsertStore)
1117  return false;
1118 
1119  // Otherwise, this is safe to promote, lets do it!
1120  DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtr
1121  << '\n');
1122  ORE->emit(
1123  OptimizationRemark(DEBUG_TYPE, "PromoteLoopAccessesToScalar", LoopUses[0])
1124  << "Moving accesses to memory location out of the loop");
1125  ++NumPromoted;
1126 
1127  // Grab a debug location for the inserted loads/stores; given that the
1128  // inserted loads/stores have little relation to the original loads/stores,
1129  // this code just arbitrarily picks a location from one, since any debug
1130  // location is better than none.
1131  DebugLoc DL = LoopUses[0]->getDebugLoc();
1132 
1133  // We use the SSAUpdater interface to insert phi nodes as required.
1135  SSAUpdater SSA(&NewPHIs);
1136  LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks,
1137  InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
1138 
1139  // Set up the preheader to have a definition of the value. It is the live-out
1140  // value from the preheader that uses in the loop will use.
1141  LoadInst *PreheaderLoad = new LoadInst(
1142  SomePtr, SomePtr->getName() + ".promoted", Preheader->getTerminator());
1143  PreheaderLoad->setAlignment(Alignment);
1144  PreheaderLoad->setDebugLoc(DL);
1145  if (AATags)
1146  PreheaderLoad->setAAMetadata(AATags);
1147  SSA.AddAvailableValue(Preheader, PreheaderLoad);
1148 
1149  // Rewrite all the loads in the loop and remember all the definitions from
1150  // stores in the loop.
1151  Promoter.run(LoopUses);
1152 
1153  // If the SSAUpdater didn't use the load in the preheader, just zap it now.
1154  if (PreheaderLoad->use_empty())
1155  PreheaderLoad->eraseFromParent();
1156 
1157  return true;
1158 }
1159 
1160 /// Returns an owning pointer to an alias set which incorporates aliasing info
1161 /// from L and all subloops of L.
1162 /// FIXME: In new pass manager, there is no helper function to handle loop
1163 /// analysis such as cloneBasicBlockAnalysis, so the AST needs to be recomputed
1164 /// from scratch for every loop. Hook up with the helper functions when
1165 /// available in the new pass manager to avoid redundant computation.
1167 LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
1168  AliasAnalysis *AA) {
1169  AliasSetTracker *CurAST = nullptr;
1170  SmallVector<Loop *, 4> RecomputeLoops;
1171  for (Loop *InnerL : L->getSubLoops()) {
1172  auto MapI = LoopToAliasSetMap.find(InnerL);
1173  // If the AST for this inner loop is missing it may have been merged into
1174  // some other loop's AST and then that loop unrolled, and so we need to
1175  // recompute it.
1176  if (MapI == LoopToAliasSetMap.end()) {
1177  RecomputeLoops.push_back(InnerL);
1178  continue;
1179  }
1180  AliasSetTracker *InnerAST = MapI->second;
1181 
1182  if (CurAST != nullptr) {
1183  // What if InnerLoop was modified by other passes ?
1184  CurAST->add(*InnerAST);
1185 
1186  // Once we've incorporated the inner loop's AST into ours, we don't need
1187  // the subloop's anymore.
1188  delete InnerAST;
1189  } else {
1190  CurAST = InnerAST;
1191  }
1192  LoopToAliasSetMap.erase(MapI);
1193  }
1194  if (CurAST == nullptr)
1195  CurAST = new AliasSetTracker(*AA);
1196 
1197  auto mergeLoop = [&](Loop *L) {
1198  // Loop over the body of this loop, looking for calls, invokes, and stores.
1199  for (BasicBlock *BB : L->blocks())
1200  CurAST->add(*BB); // Incorporate the specified basic block
1201  };
1202 
1203  // Add everything from the sub loops that are no longer directly available.
1204  for (Loop *InnerL : RecomputeLoops)
1205  mergeLoop(InnerL);
1206 
1207  // And merge in this loop.
1208  mergeLoop(L);
1209 
1210  return CurAST;
1211 }
1212 
1213 /// Simple analysis hook. Clone alias set info.
1214 ///
1215 void LegacyLICMPass::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
1216  Loop *L) {
1217  AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1218  if (!AST)
1219  return;
1220 
1221  AST->copyValue(From, To);
1222 }
1223 
1224 /// Simple Analysis hook. Delete value V from alias set
1225 ///
1226 void LegacyLICMPass::deleteAnalysisValue(Value *V, Loop *L) {
1227  AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1228  if (!AST)
1229  return;
1230 
1231  AST->deleteValue(V);
1232 }
1233 
1234 /// Simple Analysis hook. Delete value L from alias set map.
1235 ///
1236 void LegacyLICMPass::deleteAnalysisLoop(Loop *L) {
1237  AliasSetTracker *AST = LICM.getLoopToAliasSetMap().lookup(L);
1238  if (!AST)
1239  return;
1240 
1241  delete AST;
1242  LICM.getLoopToAliasSetMap().erase(L);
1243 }
1244 
1245 /// Return true if the body of this loop may store into the memory
1246 /// location pointed to by V.
1247 ///
1248 static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
1249  const AAMDNodes &AAInfo,
1250  AliasSetTracker *CurAST) {
1251  // Check to see if any of the basic blocks in CurLoop invalidate *V.
1252  return CurAST->getAliasSetForPointer(V, Size, AAInfo).isMod();
1253 }
1254 
1255 /// Little predicate that returns true if the specified basic block is in
1256 /// a subloop of the current one, not the current one itself.
1257 ///
1258 static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) {
1259  assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
1260  return LI->getLoopFor(BB) != CurLoop;
1261 }
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:76
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:38
Diagnostic information for missed-optimization remarks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isForwardingAliasSet() const
Return true if this alias set should be ignored as part of the AliasSetTracker object.
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
const std::vector< DomTreeNodeBase< NodeT > * > & getChildren() const
STATISTIC(NumFunctions,"Total number of functions")
size_t i
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
This is the interface for a simple mod/ref and alias analysis over globals.
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1156
iterator begin() const
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:58
The main scalar evolution driver.
This class represents a function call, abstracting a target machine's calling convention.
This file contains the declarations for metadata subclasses.
bool onlyReadsMemory(ImmutableCallSite CS)
Checks if the specified call is known to only read from non-volatile memory (or not access memory at ...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:736
static cl::opt< bool > DisablePromotion("disable-licm-promotion", cl::Hidden, cl::desc("Disable memory promotion in LICM pass"))
LoopT * getParentLoop() const
Definition: LoopInfo.h:103
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
A debug info location.
Definition: DebugLoc.h:34
const Instruction & front() const
Definition: BasicBlock.h:240
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition: LoopInfo.cpp:61
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
An instruction for reading from memory.
Definition: Instructions.h:164
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:575
void reserve(size_type N)
Definition: SmallVector.h:377
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:31
op_iterator op_begin()
Definition: User.h:205
BlockT * getHeader() const
Definition: LoopInfo.h:102
static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I)
Returns true if a PHINode is a trivially replaceable with an Instruction.
Definition: LICM.cpp:583
DenseMap< BasicBlock *, ColorVector > BlockColors
Definition: LoopUtils.h:47
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:228
Loop Invariant Code Motion
Definition: LICM.cpp:214
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:299
bool isMustAlias() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
This is the interface for a SCEV-based alias analysis.
static Value * getPointerOperand(Instruction &Inst)
static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE)
When an instruction is found to only use loop invariant operands that is safe to hoist, this instruction is called to do the dirty work.
Definition: LICM.cpp:778
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:115
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1218
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:257
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:55
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
void computeLoopSafetyInfo(LoopSafetyInfo *, Loop *)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Definition: LICM.cpp:450
user_iterator_impl< User > user_iterator
Definition: Value.h:340
#define F(x, y, z)
Definition: MD5.cpp:51
static bool pointerInvalidatedByLoop(Value *V, uint64_t Size, const AAMDNodes &AAInfo, AliasSetTracker *CurAST)
Return true if the body of this loop may store into the memory location pointed to by V...
Definition: LICM.cpp:1248
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries...
FunctionModRefBehavior
Summary of how a function affects memory in the program.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:269
Memory SSA
Definition: MemorySSA.cpp:55
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
An instruction for storing to memory.
Definition: Instructions.h:300
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
bool isMod() const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
NodeT * getBlock() const
void forgetLoopDispositions(const Loop *L)
Called when the client has changed the disposition of values in this loop.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:143
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:256
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:109
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
void setAAMetadata(const AAMDNodes &N)
Sets the metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1222
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
This is an important base class in LLVM.
Definition: Constant.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
bool isVolatile() const
Return true if this alias set contains volatile loads or stores.
bool empty() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:368
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Diagnostic information for applied optimization remarks.
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:581
unsigned size() const
Pass * createLICMPass()
Definition: LICM.cpp:217
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS)
Return the behavior of the given call site.
Represent the analysis usage information of a pass.
op_iterator op_end()
Definition: User.h:207
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:109
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:743
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:249
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Definition: LICM.cpp:188
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
Value * getOperand(unsigned i) const
Definition: User.h:145
iterator find(PtrType Ptr) const
Definition: SmallPtrSet.h:383
static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT, const Loop *CurLoop, AliasSetTracker *CurAST, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE)
When an instruction is found to only be used outside of the loop, this function moves it to the exit ...
Definition: LICM.cpp:706
uint32_t getTagID() const
Return the tag of this operand bundle as an integer.
Definition: InstrTypes.h:1234
void setAlignment(unsigned Align)
Used in the streaming interface as the general argument type.
bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl< BasicBlock * > &, SmallVectorImpl< Instruction * > &, PredIteratorCache &, LoopInfo *, DominatorTree *, const TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *)
Try to promote memory values to scalars by sinking stores out of the loop and moving loads to before ...
Definition: LICM.cpp:925
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfo.cpp:344
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1337
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
Return all unique successor blocks of this loop.
Definition: LoopInfo.cpp:358
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
EltTy front() const
This function does not perform any non-local loads or stores to memory.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:218
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:689
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
void emit(DiagnosticInfoOptimizationBase &OptDiag)
The new interface to emit remarks.
#define DEBUG_TYPE
Definition: LICM.cpp:72
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
iterator end()
Definition: BasicBlock.h:230
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1017
Helper class for promoting a collection of loads and stores into SSA Form using the SSAUpdater...
Definition: SSAUpdater.h:134
void copyValue(Value *From, Value *To)
This method should be used whenever a preexisting value in the program is copied or cloned...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:58
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
Definition: LoopInfo.cpp:174
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
Provides information about what library functions are available for the current target.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:625
static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo)
Return true if the only users of this instruction are outside of the loop.
Definition: LICM.cpp:595
bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *ORE)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
Definition: LICM.cpp:391
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
void add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo)
These methods are used to add different types of instructions to the alias sets.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LoopSafetyInfo *, OptimizationRemarkEmitter *ORE)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
Definition: LICM.cpp:331
A lightweight accessor for an operand bundle meant to be passed around by value.
Definition: InstrTypes.h:1206
iterator_range< user_iterator > users()
Definition: Value.h:370
static bool isSafeToExecuteUnconditionally(Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE, const Instruction *CtxI=nullptr)
Only sink or hoist an instruction if it is not a trapping instruction, or if the instruction is known...
Definition: LICM.cpp:820
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:140
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
block_iterator block_end() const
Definition: LoopInfo.h:142
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:453
iterator end() const
Definition: SmallPtrSet.h:405
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
static bool onlyAccessesArgPointees(FunctionModRefBehavior MRB)
Checks if functions with the specified behavior are known to read and write at most from objects poin...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
Captures loop safety information.
Definition: LoopUtils.h:42
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:635
AliasSet & getAliasSetForPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo)
Return the alias set that the specified pointer lives in.
static Instruction * CloneInstructionInExitBlock(Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, const LoopSafetyInfo *SafetyInfo)
Definition: LICM.cpp:641
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:938
licm
Definition: LICM.cpp:214
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
Loop Invariant Code false
Definition: LICM.cpp:214
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:391
bool hasMetadataOtherThanDebugLoc() const
Return true if this instruction has metadata attached to it other than a debug location.
Definition: Instruction.h:169
bool use_empty() const
Definition: Value.h:299
static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI)
Little predicate that returns true if the specified basic block is in a subloop of the current one...
Definition: LICM.cpp:1258
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:346
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:288
LLVM Value Representation.
Definition: Value.h:71
void setAlignment(unsigned Align)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:95
void initializeLegacyLICMPassPass(PassRegistry &)
bool empty() const
Definition: LoopInfo.h:136
#define DEBUG(X)
Definition: Debug.h:100
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
Definition: LoopInfo.h:127
block_iterator block_begin() const
Definition: LoopInfo.h:141
This is the interface for LLVM's primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested...
Definition: Loads.cpp:119
bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE=nullptr)
Returns true if the hoister and sinker can handle this instruction.
Definition: LICM.cpp:480
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:209
int * Ptr
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
op_range incoming_values()
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo)
Returns true if the instruction in a loop is guaranteed to execute at least once. ...
Definition: LoopUtils.cpp:1036
The optimization diagnostic interface.
const BasicBlock * getParent() const
Definition: Instruction.h:62
void deleteValue(Value *PtrVal)
This method is used to remove a pointer value from the AliasSetTracker entirely.
loop versioning Loop Versioning For LICM
INITIALIZE_PASS_BEGIN(LegacyLICMPass,"licm","Loop Invariant Code Motion", false, false) INITIALIZE_PASS_END(LegacyLICMPass