LLVM  16.0.0git
LoopVersioningLICM.cpp
Go to the documentation of this file.
1 //===- LoopVersioningLICM.cpp - LICM Loop Versioning ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // When alias analysis is uncertain about the aliasing between any two accesses,
10 // it will return MayAlias. This uncertainty from alias analysis restricts LICM
11 // from proceeding further. In cases where alias analysis is uncertain we might
12 // use loop versioning as an alternative.
13 //
14 // Loop Versioning will create a version of the loop with aggressive aliasing
15 // assumptions in addition to the original with conservative (default) aliasing
16 // assumptions. The version of the loop making aggressive aliasing assumptions
17 // will have all the memory accesses marked as no-alias. These two versions of
18 // loop will be preceded by a memory runtime check. This runtime check consists
19 // of bound checks for all unique memory accessed in loop, and it ensures the
20 // lack of memory aliasing. The result of the runtime check determines which of
21 // the loop versions is executed: If the runtime check detects any memory
22 // aliasing, then the original loop is executed. Otherwise, the version with
23 // aggressive aliasing assumptions is used.
24 //
25 // Following are the top level steps:
26 //
27 // a) Perform LoopVersioningLICM's feasibility check.
28 // b) If loop is a candidate for versioning then create a memory bound check,
29 // by considering all the memory accesses in loop body.
30 // c) Clone original loop and set all memory accesses as no-alias in new loop.
31 // d) Set original loop & versioned loop as a branch target of the runtime check
32 // result.
33 //
34 // It transforms loop as shown below:
35 //
36 // +----------------+
37 // |Runtime Memcheck|
38 // +----------------+
39 // |
40 // +----------+----------------+----------+
41 // | |
42 // +---------+----------+ +-----------+----------+
43 // |Orig Loop Preheader | |Cloned Loop Preheader |
44 // +--------------------+ +----------------------+
45 // | |
46 // +--------------------+ +----------------------+
47 // |Orig Loop Body | |Cloned Loop Body |
48 // +--------------------+ +----------------------+
49 // | |
50 // +--------------------+ +----------------------+
51 // |Orig Loop Exit Block| |Cloned Loop Exit Block|
52 // +--------------------+ +-----------+----------+
53 // | |
54 // +----------+--------------+-----------+
55 // |
56 // +-----+----+
57 // |Join Block|
58 // +----------+
59 //
60 //===----------------------------------------------------------------------===//
61 
63 #include "llvm/ADT/SmallVector.h"
64 #include "llvm/ADT/StringRef.h"
69 #include "llvm/Analysis/LoopInfo.h"
70 #include "llvm/Analysis/LoopPass.h"
73 #include "llvm/IR/Dominators.h"
74 #include "llvm/IR/Instruction.h"
75 #include "llvm/IR/Instructions.h"
76 #include "llvm/IR/LLVMContext.h"
77 #include "llvm/IR/MDBuilder.h"
78 #include "llvm/IR/Metadata.h"
79 #include "llvm/IR/Value.h"
80 #include "llvm/InitializePasses.h"
81 #include "llvm/Pass.h"
82 #include "llvm/Support/Casting.h"
84 #include "llvm/Support/Debug.h"
86 #include "llvm/Transforms/Scalar.h"
87 #include "llvm/Transforms/Utils.h"
90 #include <cassert>
91 #include <memory>
92 
93 using namespace llvm;
94 
95 #define DEBUG_TYPE "loop-versioning-licm"
96 
97 static const char *LICMVersioningMetaData = "llvm.loop.licm_versioning.disable";
98 
99 /// Threshold minimum allowed percentage for possible
100 /// invariant instructions in a loop.
101 static cl::opt<float>
102  LVInvarThreshold("licm-versioning-invariant-threshold",
103  cl::desc("LoopVersioningLICM's minimum allowed percentage"
104  "of possible invariant instructions per loop"),
105  cl::init(25), cl::Hidden);
106 
107 /// Threshold for maximum allowed loop nest/depth
109  "licm-versioning-max-depth-threshold",
110  cl::desc(
111  "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"),
112  cl::init(2), cl::Hidden);
113 
114 namespace {
115 
116 struct LoopVersioningLICMLegacyPass : public LoopPass {
117  static char ID;
118 
119  LoopVersioningLICMLegacyPass() : LoopPass(ID) {
122  }
123 
124  bool runOnLoop(Loop *L, LPPassManager &LPM) override;
125 
126  StringRef getPassName() const override { return "Loop Versioning for LICM"; }
127 
128  void getAnalysisUsage(AnalysisUsage &AU) const override {
129  AU.setPreservesCFG();
140  }
141 };
142 
143 struct LoopVersioningLICM {
144  // We don't explicitly pass in LoopAccessInfo to the constructor since the
145  // loop versioning might return early due to instructions that are not safe
146  // for versioning. By passing the proxy instead the construction of
147  // LoopAccessInfo will take place only when it's necessary.
148  LoopVersioningLICM(AliasAnalysis *AA, ScalarEvolution *SE,
150  LoopAccessInfoManager &LAIs, LoopInfo &LI,
151  Loop *CurLoop)
152  : AA(AA), SE(SE), LAIs(LAIs), LI(LI), CurLoop(CurLoop),
153  LoopDepthThreshold(LVLoopDepthThreshold),
154  InvariantThreshold(LVInvarThreshold), ORE(ORE) {}
155 
156  bool run(DominatorTree *DT);
157 
158 private:
159  // Current AliasAnalysis information
160  AliasAnalysis *AA;
161 
162  // Current ScalarEvolution
163  ScalarEvolution *SE;
164 
165  // Current Loop's LoopAccessInfo
166  const LoopAccessInfo *LAI = nullptr;
167 
168  // Proxy for retrieving LoopAccessInfo.
169  LoopAccessInfoManager &LAIs;
170 
171  LoopInfo &LI;
172 
173  // The current loop we are working on.
174  Loop *CurLoop;
175 
176  // Maximum loop nest threshold
177  unsigned LoopDepthThreshold;
178 
179  // Minimum invariant threshold
180  float InvariantThreshold;
181 
182  // Counter to track num of load & store
183  unsigned LoadAndStoreCounter = 0;
184 
185  // Counter to track num of invariant
186  unsigned InvariantCounter = 0;
187 
188  // Read only loop marker.
189  bool IsReadOnlyLoop = true;
190 
191  // OptimizationRemarkEmitter
193 
194  bool isLegalForVersioning();
195  bool legalLoopStructure();
196  bool legalLoopInstructions();
197  bool legalLoopMemoryAccesses();
198  bool isLoopAlreadyVisited();
199  void setNoAliasToLoop(Loop *VerLoop);
200  bool instructionSafeForVersioning(Instruction *I);
201 };
202 
203 } // end anonymous namespace
204 
205 /// Check loop structure and confirms it's good for LoopVersioningLICM.
206 bool LoopVersioningLICM::legalLoopStructure() {
207  // Loop must be in loop simplify form.
208  if (!CurLoop->isLoopSimplifyForm()) {
209  LLVM_DEBUG(dbgs() << " loop is not in loop-simplify form.\n");
210  return false;
211  }
212  // Loop should be innermost loop, if not return false.
213  if (!CurLoop->getSubLoops().empty()) {
214  LLVM_DEBUG(dbgs() << " loop is not innermost\n");
215  return false;
216  }
217  // Loop should have a single backedge, if not return false.
218  if (CurLoop->getNumBackEdges() != 1) {
219  LLVM_DEBUG(dbgs() << " loop has multiple backedges\n");
220  return false;
221  }
222  // Loop must have a single exiting block, if not return false.
223  if (!CurLoop->getExitingBlock()) {
224  LLVM_DEBUG(dbgs() << " loop has multiple exiting block\n");
225  return false;
226  }
227  // We only handle bottom-tested loop, i.e. loop in which the condition is
228  // checked at the end of each iteration. With that we can assume that all
229  // instructions in the loop are executed the same number of times.
230  if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) {
231  LLVM_DEBUG(dbgs() << " loop is not bottom tested\n");
232  return false;
233  }
234  // Parallel loops must not have aliasing loop-invariant memory accesses.
235  // Hence we don't need to version anything in this case.
236  if (CurLoop->isAnnotatedParallel()) {
237  LLVM_DEBUG(dbgs() << " Parallel loop is not worth versioning\n");
238  return false;
239  }
240  // Loop depth more then LoopDepthThreshold are not allowed
241  if (CurLoop->getLoopDepth() > LoopDepthThreshold) {
242  LLVM_DEBUG(dbgs() << " loop depth is more then threshold\n");
243  return false;
244  }
245  // We need to be able to compute the loop trip count in order
246  // to generate the bound checks.
247  const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop);
248  if (isa<SCEVCouldNotCompute>(ExitCount)) {
249  LLVM_DEBUG(dbgs() << " loop does not has trip count\n");
250  return false;
251  }
252  return true;
253 }
254 
255 /// Check memory accesses in loop and confirms it's good for
256 /// LoopVersioningLICM.
257 bool LoopVersioningLICM::legalLoopMemoryAccesses() {
258  // Loop over the body of this loop, construct AST.
259  AliasSetTracker AST(*AA);
260  for (auto *Block : CurLoop->getBlocks()) {
261  // Ignore blocks in subloops.
262  if (LI.getLoopFor(Block) == CurLoop)
263  AST.add(*Block);
264  }
265 
266  // Memory check:
267  // Transform phase will generate a versioned loop and also a runtime check to
268  // ensure the pointers are independent and they don’t alias.
269  // In version variant of loop, alias meta data asserts that all access are
270  // mutually independent.
271  //
272  // Pointers aliasing in alias domain are avoided because with multiple
273  // aliasing domains we may not be able to hoist potential loop invariant
274  // access out of the loop.
275  //
276  // Iterate over alias tracker sets, and confirm AliasSets doesn't have any
277  // must alias set.
278  bool HasMayAlias = false;
279  bool TypeSafety = false;
280  bool HasMod = false;
281  for (const auto &I : AST) {
282  const AliasSet &AS = I;
283  // Skip Forward Alias Sets, as this should be ignored as part of
284  // the AliasSetTracker object.
285  if (AS.isForwardingAliasSet())
286  continue;
287  // With MustAlias its not worth adding runtime bound check.
288  if (AS.isMustAlias())
289  return false;
290  Value *SomePtr = AS.begin()->getValue();
291  bool TypeCheck = true;
292  // Check for Mod & MayAlias
293  HasMayAlias |= AS.isMayAlias();
294  HasMod |= AS.isMod();
295  for (const auto &A : AS) {
296  Value *Ptr = A.getValue();
297  // Alias tracker should have pointers of same data type.
298  TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType()));
299  }
300  // At least one alias tracker should have pointers of same data type.
301  TypeSafety |= TypeCheck;
302  }
303  // Ensure types should be of same type.
304  if (!TypeSafety) {
305  LLVM_DEBUG(dbgs() << " Alias tracker type safety failed!\n");
306  return false;
307  }
308  // Ensure loop body shouldn't be read only.
309  if (!HasMod) {
310  LLVM_DEBUG(dbgs() << " No memory modified in loop body\n");
311  return false;
312  }
313  // Make sure alias set has may alias case.
314  // If there no alias memory ambiguity, return false.
315  if (!HasMayAlias) {
316  LLVM_DEBUG(dbgs() << " No ambiguity in memory access.\n");
317  return false;
318  }
319  return true;
320 }
321 
322 /// Check loop instructions safe for Loop versioning.
323 /// It returns true if it's safe else returns false.
324 /// Consider following:
325 /// 1) Check all load store in loop body are non atomic & non volatile.
326 /// 2) Check function call safety, by ensuring its not accessing memory.
327 /// 3) Loop body shouldn't have any may throw instruction.
328 /// 4) Loop body shouldn't have any convergent or noduplicate instructions.
329 bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) {
330  assert(I != nullptr && "Null instruction found!");
331  // Check function call safety
332  if (auto *Call = dyn_cast<CallBase>(I)) {
333  if (Call->isConvergent() || Call->cannotDuplicate()) {
334  LLVM_DEBUG(dbgs() << " Convergent call site found.\n");
335  return false;
336  }
337 
338  if (!AA->doesNotAccessMemory(Call)) {
339  LLVM_DEBUG(dbgs() << " Unsafe call site found.\n");
340  return false;
341  }
342  }
343 
344  // Avoid loops with possiblity of throw
345  if (I->mayThrow()) {
346  LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n");
347  return false;
348  }
349  // If current instruction is load instructions
350  // make sure it's a simple load (non atomic & non volatile)
351  if (I->mayReadFromMemory()) {
352  LoadInst *Ld = dyn_cast<LoadInst>(I);
353  if (!Ld || !Ld->isSimple()) {
354  LLVM_DEBUG(dbgs() << " Found a non-simple load.\n");
355  return false;
356  }
357  LoadAndStoreCounter++;
358  Value *Ptr = Ld->getPointerOperand();
359  // Check loop invariant.
360  if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
361  InvariantCounter++;
362  }
363  // If current instruction is store instruction
364  // make sure it's a simple store (non atomic & non volatile)
365  else if (I->mayWriteToMemory()) {
366  StoreInst *St = dyn_cast<StoreInst>(I);
367  if (!St || !St->isSimple()) {
368  LLVM_DEBUG(dbgs() << " Found a non-simple store.\n");
369  return false;
370  }
371  LoadAndStoreCounter++;
372  Value *Ptr = St->getPointerOperand();
373  // Check loop invariant.
374  if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
375  InvariantCounter++;
376 
377  IsReadOnlyLoop = false;
378  }
379  return true;
380 }
381 
382 /// Check loop instructions and confirms it's good for
383 /// LoopVersioningLICM.
384 bool LoopVersioningLICM::legalLoopInstructions() {
385  // Resetting counters.
386  LoadAndStoreCounter = 0;
387  InvariantCounter = 0;
388  IsReadOnlyLoop = true;
389  using namespace ore;
390  // Iterate over loop blocks and instructions of each block and check
391  // instruction safety.
392  for (auto *Block : CurLoop->getBlocks())
393  for (auto &Inst : *Block) {
394  // If instruction is unsafe just return false.
395  if (!instructionSafeForVersioning(&Inst)) {
396  ORE->emit([&]() {
397  return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopInst", &Inst)
398  << " Unsafe Loop Instruction";
399  });
400  return false;
401  }
402  }
403  // Get LoopAccessInfo from current loop via the proxy.
404  LAI = &LAIs.getInfo(*CurLoop);
405  // Check LoopAccessInfo for need of runtime check.
406  if (LAI->getRuntimePointerChecking()->getChecks().empty()) {
407  LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
408  return false;
409  }
410  // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold
411  if (LAI->getNumRuntimePointerChecks() >
413  LLVM_DEBUG(
414  dbgs() << " LAA: Runtime checks are more than threshold !!\n");
415  ORE->emit([&]() {
416  return OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeCheck",
417  CurLoop->getStartLoc(),
418  CurLoop->getHeader())
419  << "Number of runtime checks "
420  << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks())
421  << " exceeds threshold "
423  });
424  return false;
425  }
426  // Loop should have at least one invariant load or store instruction.
427  if (!InvariantCounter) {
428  LLVM_DEBUG(dbgs() << " Invariant not found !!\n");
429  return false;
430  }
431  // Read only loop not allowed.
432  if (IsReadOnlyLoop) {
433  LLVM_DEBUG(dbgs() << " Found a read-only loop!\n");
434  return false;
435  }
436  // Profitablity check:
437  // Check invariant threshold, should be in limit.
438  if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) {
439  LLVM_DEBUG(
440  dbgs()
441  << " Invariant load & store are less then defined threshold\n");
442  LLVM_DEBUG(dbgs() << " Invariant loads & stores: "
443  << ((InvariantCounter * 100) / LoadAndStoreCounter)
444  << "%\n");
445  LLVM_DEBUG(dbgs() << " Invariant loads & store threshold: "
446  << InvariantThreshold << "%\n");
447  ORE->emit([&]() {
448  return OptimizationRemarkMissed(DEBUG_TYPE, "InvariantThreshold",
449  CurLoop->getStartLoc(),
450  CurLoop->getHeader())
451  << "Invariant load & store "
452  << NV("LoadAndStoreCounter",
453  ((InvariantCounter * 100) / LoadAndStoreCounter))
454  << " are less then defined threshold "
455  << NV("Threshold", InvariantThreshold);
456  });
457  return false;
458  }
459  return true;
460 }
461 
462 /// It checks loop is already visited or not.
463 /// check loop meta data, if loop revisited return true
464 /// else false.
465 bool LoopVersioningLICM::isLoopAlreadyVisited() {
466  // Check LoopVersioningLICM metadata into loop
468  return true;
469  }
470  return false;
471 }
472 
473 /// Checks legality for LoopVersioningLICM by considering following:
474 /// a) loop structure legality b) loop instruction legality
475 /// c) loop memory access legality.
476 /// Return true if legal else returns false.
477 bool LoopVersioningLICM::isLegalForVersioning() {
478  using namespace ore;
479  LLVM_DEBUG(dbgs() << "Loop: " << *CurLoop);
480  // Make sure not re-visiting same loop again.
481  if (isLoopAlreadyVisited()) {
482  LLVM_DEBUG(
483  dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n");
484  return false;
485  }
486  // Check loop structure leagality.
487  if (!legalLoopStructure()) {
488  LLVM_DEBUG(
489  dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n");
490  ORE->emit([&]() {
491  return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopStruct",
492  CurLoop->getStartLoc(),
493  CurLoop->getHeader())
494  << " Unsafe Loop structure";
495  });
496  return false;
497  }
498  // Check loop instruction leagality.
499  if (!legalLoopInstructions()) {
500  LLVM_DEBUG(
501  dbgs()
502  << " Loop instructions not suitable for LoopVersioningLICM\n\n");
503  return false;
504  }
505  // Check loop memory access leagality.
506  if (!legalLoopMemoryAccesses()) {
507  LLVM_DEBUG(
508  dbgs()
509  << " Loop memory access not suitable for LoopVersioningLICM\n\n");
510  ORE->emit([&]() {
511  return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopMemoryAccess",
512  CurLoop->getStartLoc(),
513  CurLoop->getHeader())
514  << " Unsafe Loop memory access";
515  });
516  return false;
517  }
518  // Loop versioning is feasible, return true.
519  LLVM_DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n");
520  ORE->emit([&]() {
521  return OptimizationRemark(DEBUG_TYPE, "IsLegalForVersioning",
522  CurLoop->getStartLoc(), CurLoop->getHeader())
523  << " Versioned loop for LICM."
524  << " Number of runtime checks we had to insert "
525  << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks());
526  });
527  return true;
528 }
529 
530 /// Update loop with aggressive aliasing assumptions.
531 /// It marks no-alias to any pairs of memory operations by assuming
532 /// loop should not have any must-alias memory accesses pairs.
533 /// During LoopVersioningLICM legality we ignore loops having must
534 /// aliasing memory accesses.
535 void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) {
536  // Get latch terminator instruction.
537  Instruction *I = VerLoop->getLoopLatch()->getTerminator();
538  // Create alias scope domain.
539  MDBuilder MDB(I->getContext());
540  MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain");
541  StringRef Name = "LVAliasScope";
542  MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
543  SmallVector<Metadata *, 4> Scopes{NewScope}, NoAliases{NewScope};
544  // Iterate over each instruction of loop.
545  // set no-alias for all load & store instructions.
546  for (auto *Block : CurLoop->getBlocks()) {
547  for (auto &Inst : *Block) {
548  // Only interested in instruction that may modify or read memory.
549  if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory())
550  continue;
551  // Set no-alias for current instruction.
552  Inst.setMetadata(
553  LLVMContext::MD_noalias,
554  MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias),
555  MDNode::get(Inst.getContext(), NoAliases)));
556  // set alias-scope for current instruction.
557  Inst.setMetadata(
558  LLVMContext::MD_alias_scope,
559  MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope),
560  MDNode::get(Inst.getContext(), Scopes)));
561  }
562  }
563 }
564 
565 bool LoopVersioningLICMLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
566  if (skipLoop(L))
567  return false;
568 
569  AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
570  ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
572  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
573  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
574  DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
575  auto &LAIs = getAnalysis<LoopAccessLegacyAnalysis>().getLAIs();
576 
577  return LoopVersioningLICM(AA, SE, ORE, LAIs, LI, L).run(DT);
578 }
579 
581  // Do not do the transformation if disabled by metadata.
583  return false;
584 
585  bool Changed = false;
586 
587  // Check feasiblity of LoopVersioningLICM.
588  // If versioning found to be feasible and beneficial then proceed
589  // else simply return, by cleaning up memory.
590  if (isLegalForVersioning()) {
591  // Do loop versioning.
592  // Create memcheck for memory accessed inside loop.
593  // Clone original loop, and set blocks properly.
594  LoopVersioning LVer(*LAI, LAI->getRuntimePointerChecking()->getChecks(),
595  CurLoop, &LI, DT, SE);
596  LVer.versionLoop();
597  // Set Loop Versioning metaData for original loop.
598  addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData);
599  // Set Loop Versioning metaData for version loop.
600  addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData);
601  // Set "llvm.mem.parallel_loop_access" metaData to versioned loop.
602  // FIXME: "llvm.mem.parallel_loop_access" annotates memory access
603  // instructions, not loops.
604  addStringMetadataToLoop(LVer.getVersionedLoop(),
605  "llvm.mem.parallel_loop_access");
606  // Update version loop with aggressive aliasing assumption.
607  setNoAliasToLoop(LVer.getVersionedLoop());
608  Changed = true;
609  }
610  return Changed;
611 }
612 
614 
615 INITIALIZE_PASS_BEGIN(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
616  "Loop Versioning For LICM", false, false)
620 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
623 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
626 INITIALIZE_PASS_END(LoopVersioningLICMLegacyPass, "loop-versioning-licm",
627  "Loop Versioning For LICM", false, false)
628 
630  return new LoopVersioningLICMLegacyPass();
631 }
632 
633 namespace llvm {
634 
637  LPMUpdater &U) {
638  AliasAnalysis *AA = &LAR.AA;
639  ScalarEvolution *SE = &LAR.SE;
640  DominatorTree *DT = &LAR.DT;
641  const Function *F = L.getHeader()->getParent();
643 
644  LoopAccessInfoManager LAIs(*SE, *AA, *DT, LAR.LI, nullptr);
645  if (!LoopVersioningLICM(AA, SE, &ORE, LAIs, LAR.LI, &L).run(DT))
646  return PreservedAnalyses::all();
648 }
649 } // namespace llvm
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::OptimizationRemarkMissed
Diagnostic information for missed-optimization remarks.
Definition: DiagnosticInfo.h:734
llvm::LoopAccessLegacyAnalysis
This analysis provides dependence information for the memory accesses of a loop.
Definition: LoopAccessAnalysis.h:796
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(LoopVersioningLICMLegacyPass, "loop-versioning-licm", "Loop Versioning For LICM", false, false) INITIALIZE_PASS_END(LoopVersioningLICMLegacyPass
llvm::LoopAccessInfoManager
Definition: LoopAccessAnalysis.h:768
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
Metadata.h
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:104
Scalar.h
llvm::Function
Definition: Function.h:60
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
StringRef.h
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::AliasSet::isMod
bool isMod() const
Definition: AliasSetTracker.h:197
LoopAccessAnalysis.h
llvm::ScalarEvolution
The main scalar evolution driver.
Definition: ScalarEvolution.h:449
OptimizationRemarkEmitter.h
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
GlobalsModRef.h
llvm::AliasSetTracker
Definition: AliasSetTracker.h:305
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
ScalarEvolution.h
llvm::LoopInfoWrapperPass
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:1293
llvm::LoopStandardAnalysisResults
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Definition: LoopAnalysisManager.h:51
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition: OptimizationRemarkEmitter.h:136
llvm::LoopVersioningLICMPass::run
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &LAR, LPMUpdater &U)
Definition: LoopVersioningLICM.cpp:635
llvm::LoadInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:261
llvm::LoopStandardAnalysisResults::DT
DominatorTree & DT
Definition: LoopAnalysisManager.h:54
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1400
F
#define F(x, y, z)
Definition: MD5.cpp:55
loop
Analysis the ScalarEvolution expression for r is< loop > Outside the loop
Definition: README.txt:8
AliasAnalysis.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Instruction.h
CommandLine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::AAResults
Definition: AliasAnalysis.h:294
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
llvm::createLoopVersioningLICMPass
Pass * createLoopVersioningLICMPass()
Definition: LoopVersioningLICM.cpp:629
LVLoopDepthThreshold
static cl::opt< unsigned > LVLoopDepthThreshold("licm-versioning-max-depth-threshold", cl::desc("LoopVersioningLICM's threshold for maximum allowed loop nest/depth"), cl::init(2), cl::Hidden)
Threshold for maximum allowed loop nest/depth.
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::AliasSet
Definition: AliasSetTracker.h:49
llvm::StoreInst::isSimple
bool isSimple() const
Definition: Instructions.h:379
false
Definition: StackSlotColoring.cpp:141
llvm::AliasSet::isForwardingAliasSet
bool isForwardingAliasSet() const
Return true if this alias set should be ignored as part of the AliasSetTracker object.
Definition: AliasSetTracker.h:203
llvm::Instruction
Definition: Instruction.h:42
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:306
MDBuilder.h
llvm::initializeLoopVersioningLICMLegacyPassPass
void initializeLoopVersioningLICMLegacyPassPass(PassRegistry &)
LoopUtils.h
llvm::ScalarEvolutionWrapperPass
Definition: ScalarEvolution.h:2187
llvm::LPPassManager
Definition: LoopPass.h:76
llvm::hasLICMVersioningTransformation
TransformationMode hasLICMVersioningTransformation(const Loop *L)
Definition: LoopUtils.cpp:438
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
Utils.h
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition: PointerTypeAnalysis.cpp:189
LoopInfo.h
llvm::getLoopPassPreservedAnalyses
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
Definition: LoopAnalysisManager.cpp:138
llvm::findStringMetadataForLoop
Optional< const MDOperand * > findStringMetadataForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for loop.
Definition: LoopInfo.cpp:1052
llvm::cl::opt
Definition: CommandLine.h:1412
llvm::SCEV
This class represents an analyzed expression in the program.
Definition: ScalarEvolution.h:75
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:298
llvm::logicalview::LVCompareKind::Scopes
@ Scopes
LICM
loop versioning Loop Versioning For LICM
Definition: LoopVersioningLICM.cpp:627
llvm::LoopPass
Definition: LoopPass.h:28
llvm::LPMUpdater
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Definition: LoopPassManager.h:262
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::LoopVersioning
This class emits a version of the loop where run-time checks ensure that may-alias pointers can't ove...
Definition: LoopVersioning.h:41
llvm::addStringMetadataToLoop
void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
Definition: LoopUtils.cpp:215
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::LoopAccessInfo
Drive the analysis of memory accesses in the loop.
Definition: LoopAccessAnalysis.h:562
llvm::AliasSet::isMayAlias
bool isMayAlias() const
Definition: AliasSetTracker.h:199
llvm::LoopBase::getLoopLatch
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:232
llvm::TM_Disable
@ TM_Disable
The transformation should not be applied.
Definition: LoopUtils.h:277
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::VectorizerParams::RuntimeMemoryCheckThreshold
static unsigned RuntimeMemoryCheckThreshold
\When performing memory disambiguation checks at runtime do not make more than this number of compari...
Definition: LoopAccessAnalysis.h:50
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
LICMVersioningMetaData
static const char * LICMVersioningMetaData
Definition: LoopVersioningLICM.cpp:97
llvm::LoopInfo
Definition: LoopInfo.h:1108
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::LoadInst::isSimple
bool isSimple() const
Definition: Instructions.h:253
DEBUG_TYPE
#define DEBUG_TYPE
Definition: LoopVersioningLICM.cpp:95
LoopPass.h
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::MDNode::concatenate
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
Definition: Metadata.cpp:1006
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::LoopStandardAnalysisResults::LI
LoopInfo & LI
Definition: LoopAnalysisManager.h:55
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
LoopVersioningLICM.h
llvm::LCSSAID
char & LCSSAID
Definition: LCSSA.cpp:492
llvm::OptimizationRemarkEmitterWrapperPass
OptimizationRemarkEmitter legacy analysis pass.
Definition: OptimizationRemarkEmitter.h:146
llvm::LoopSimplifyID
char & LoopSimplifyID
Definition: LoopSimplify.cpp:789
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
Casting.h
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition: LoopInfo.h:105
LoopVersioning.h
llvm::LoopStandardAnalysisResults::SE
ScalarEvolution & SE
Definition: LoopAnalysisManager.h:56
llvm::LoopStandardAnalysisResults::AA
AAResults & AA
Definition: LoopAnalysisManager.h:52
llvm::MDBuilder
Definition: MDBuilder.h:36
licm
loop versioning licm
Definition: LoopVersioningLICM.cpp:626
llvm::AliasSet::begin
iterator begin() const
Definition: AliasSetTracker.h:211
llvm::OptimizationRemark
Diagnostic information for applied optimization remarks.
Definition: DiagnosticInfo.h:689
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
Instructions.h
SmallVector.h
llvm::StoreInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:390
Dominators.h
NoAliases
static cl::opt< bool > NoAliases("csky-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
llvm::AliasSet::isMustAlias
bool isMustAlias() const
Definition: AliasSetTracker.h:198
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:924
llvm::GlobalsAAWrapperPass
Legacy wrapper pass to provide the GlobalsAAResult object.
Definition: GlobalsModRef.h:142
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:119
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
AliasSetTracker.h
llvm::AnalysisUsage::addRequiredID
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:279
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
LLVMContext.h
llvm::cl::desc
Definition: CommandLine.h:413
raw_ostream.h
Value.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
LVInvarThreshold
static cl::opt< float > LVInvarThreshold("licm-versioning-invariant-threshold", cl::desc("LoopVersioningLICM's minimum allowed percentage" "of possible invariant instructions per loop"), cl::init(25), cl::Hidden)
Threshold minimum allowed percentage for possible invariant instructions in a loop.