90#define DEBUG_TYPE "loop-versioning-licm" 
   98                     cl::desc(
"LoopVersioningLICM's minimum allowed percentage " 
   99                              "of possible invariant instructions per loop"),
 
  104    "licm-versioning-max-depth-threshold",
 
  106        "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"),
 
  111struct LoopVersioningLICM {
 
  120      : 
AA(
AA), SE(SE), LAIs(LAIs), LI(LI), CurLoop(CurLoop),
 
  124  bool run(DominatorTree *DT);
 
  134  const LoopAccessInfo *LAI = 
nullptr;
 
  137  LoopAccessInfoManager &LAIs;
 
  145  unsigned LoopDepthThreshold;
 
  148  float InvariantThreshold;
 
  151  unsigned LoadAndStoreCounter = 0;
 
  154  unsigned InvariantCounter = 0;
 
  157  bool IsReadOnlyLoop = 
true;
 
  160  OptimizationRemarkEmitter *ORE;
 
  162  bool isLegalForVersioning();
 
  163  bool legalLoopStructure();
 
  164  bool legalLoopInstructions();
 
  165  bool legalLoopMemoryAccesses();
 
  166  bool isLoopAlreadyVisited();
 
  167  bool instructionSafeForVersioning(Instruction *
I);
 
  173bool LoopVersioningLICM::legalLoopStructure() {
 
  224bool LoopVersioningLICM::legalLoopMemoryAccesses() {
 
  226  BatchAAResults BAA(*AA);
 
  227  AliasSetTracker AST(BAA);
 
  246  bool HasMayAlias = 
false;
 
  247  bool TypeSafety = 
false;
 
  249  for (
const auto &
I : AST) {
 
  250    const AliasSet &AS = 
I;
 
  259    bool TypeCheck = 
true;
 
  262    HasMod |= AS.
isMod();
 
  263    for (
const auto &MemLoc : AS) {
 
  271      TypeCheck = (TypeCheck && (SomePtr->
getType() == 
Ptr->getType()));
 
  274    TypeSafety |= TypeCheck;
 
  302bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *
I) {
 
  303  assert(
I != 
nullptr && 
"Null instruction found!");
 
  319    LLVM_DEBUG(
dbgs() << 
"    May throw instruction found in loop body\n");
 
  324  if (
I->mayReadFromMemory()) {
 
  330    LoadAndStoreCounter++;
 
  338  else if (
I->mayWriteToMemory()) {
 
  344    LoadAndStoreCounter++;
 
  349    if (!
any_of(Pointers, [&](
auto &
P) { 
return P.PointerValue == 
Ptr; })) {
 
  350      LLVM_DEBUG(
dbgs() << 
"    Found a store without a runtime check.\n");
 
  357    IsReadOnlyLoop = 
false;
 
  364bool LoopVersioningLICM::legalLoopInstructions() {
 
  366  LoadAndStoreCounter = 0;
 
  367  InvariantCounter = 0;
 
  368  IsReadOnlyLoop = 
true;
 
  371  LAI = &LAIs.
getInfo(*CurLoop, 
true);
 
  380    for (
auto &Inst : *
Block) {
 
  382      if (!instructionSafeForVersioning(&Inst)) {
 
  384          return OptimizationRemarkMissed(
DEBUG_TYPE, 
"IllegalLoopInst", &Inst)
 
  385                 << 
" Unsafe Loop Instruction";
 
  394        dbgs() << 
"    LAA: Runtime checks are more than threshold !!\n");
 
  396      return OptimizationRemarkMissed(
DEBUG_TYPE, 
"RuntimeCheck",
 
  399             << 
"Number of runtime checks " 
  401             << 
" exceeds threshold " 
  407  if (!InvariantCounter) {
 
  412  if (IsReadOnlyLoop) {
 
  418  if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) {
 
  421        << 
"    Invariant load & store are less then defined threshold\n");
 
  423                      << ((InvariantCounter * 100) / LoadAndStoreCounter)
 
  426                      << InvariantThreshold << 
"%\n");
 
  428      return OptimizationRemarkMissed(
DEBUG_TYPE, 
"InvariantThreshold",
 
  431             << 
"Invariant load & store " 
  432             << 
NV(
"LoadAndStoreCounter",
 
  433                   ((InvariantCounter * 100) / LoadAndStoreCounter))
 
  434             << 
" are less then defined threshold " 
  435             << 
NV(
"Threshold", InvariantThreshold);
 
  445bool LoopVersioningLICM::isLoopAlreadyVisited() {
 
  457bool LoopVersioningLICM::isLegalForVersioning() {
 
  461  if (isLoopAlreadyVisited()) {
 
  463        dbgs() << 
"    Revisiting loop in LoopVersioningLICM not allowed.\n\n");
 
  467  if (!legalLoopStructure()) {
 
  469        dbgs() << 
"    Loop structure not suitable for LoopVersioningLICM\n\n");
 
  471      return OptimizationRemarkMissed(
DEBUG_TYPE, 
"IllegalLoopStruct",
 
  474             << 
" Unsafe Loop structure";
 
  479  if (!legalLoopInstructions()) {
 
  482        << 
"    Loop instructions not suitable for LoopVersioningLICM\n\n");
 
  486  if (!legalLoopMemoryAccesses()) {
 
  489        << 
"    Loop memory access not suitable for LoopVersioningLICM\n\n");
 
  491      return OptimizationRemarkMissed(
DEBUG_TYPE, 
"IllegalLoopMemoryAccess",
 
  494             << 
" Unsafe Loop memory access";
 
  499  LLVM_DEBUG(
dbgs() << 
"    Loop Versioning found to be beneficial\n\n");
 
  501    return OptimizationRemark(
DEBUG_TYPE, 
"IsLegalForVersioning",
 
  503           << 
" Versioned loop for LICM." 
  504           << 
" Number of runtime checks we had to insert " 
  510bool LoopVersioningLICM::run(DominatorTree *DT) {
 
  520  if (isLegalForVersioning()) {
 
  525                        CurLoop, &LI, DT, SE);
 
  535                            "llvm.mem.parallel_loop_access");
 
  537    LVer.annotateLoopWithNoAlias();
 
  549  const Function *
F = L.getHeader()->getParent();
 
  553  if (!LoopVersioningLICM(
AA, SE, &ORE, LAIs, LAR.
LI, &L).run(DT))
 
 
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
 
This is the interface for a simple mod/ref and alias analysis over globals.
 
static const char * LICMVersioningMetaData
 
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.
 
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.
 
This file defines the SmallVector class.
 
bool doesNotAccessMemory(const CallBase *Call)
Checks if the specified call is known to never read or write memory.
 
bool isForwardingAliasSet() const
Return true if this alias set should be ignored as part of the AliasSetTracker object.
 
bool cannotDuplicate() const
Determine if the invoke cannot be duplicated.
 
bool isConvergent() const
Determine if the invoke is convergent.
 
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
 
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
 
Value * getPointerOperand()
 
LLVM_ABI const LoopAccessInfo & getInfo(Loop &L, bool AllowPartial=false)
 
const RuntimePointerChecking * getRuntimePointerChecking() const
 
unsigned getNumRuntimePointerChecks() const
Number of memchecks required to prove independence of otherwise may-alias pointers.
 
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
 
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
 
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
 
BlockT * getHeader() const
 
unsigned getLoopDepth() const
Return the nesting level of this loop.
 
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
 
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
 
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
 
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &LAR, LPMUpdater &U)
 
Represents a single loop in the control flow graph.
 
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
 
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
 
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
 
A set of analyses that are preserved following a run of a transformation pass.
 
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
 
SmallVector< PointerInfo, 2 > Pointers
Information about the pointers that may require checking.
 
const SmallVectorImpl< RuntimePointerCheck > & getChecks() const
Returns the checks that generateChecks created.
 
The main scalar evolution driver.
 
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
 
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
 
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
 
Value * getPointerOperand()
 
Type * getType() const
All values are typed, get the type of this value.
 
Abstract Attribute helper functions.
 
initializer< Ty > init(const Ty &Val)
 
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
 
DiagnosticInfoOptimizationBase::Argument NV
 
This is an optimization pass for GlobalISel generic memory operations.
 
FunctionAddr VTableAddr Value
 
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
 
LLVM_ABI std::optional< const MDOperand * > findStringMetadataForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for loop.
 
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, unsigned V=0)
Set input string into loop metadata by keeping other values intact.
 
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
 
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
 
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
 
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
 
LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L)
 
@ TM_Disable
The transformation should not be applied.
 
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
 
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
 
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
 
static LLVM_ABI unsigned RuntimeMemoryCheckThreshold
\When performing memory disambiguation checks at runtime do not make more than this number of compari...