diff options
Diffstat (limited to 'llvm/lib/Transforms/Scalar')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/LICM.cpp | 83 |
1 files changed, 69 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp index f576661363d..ee187c1d601 100644 --- a/llvm/lib/Transforms/Scalar/LICM.cpp +++ b/llvm/lib/Transforms/Scalar/LICM.cpp @@ -89,6 +89,13 @@ static cl::opt<uint32_t> MaxNumUsesTraversed( cl::desc("Max num uses visited for identifying load " "invariance in loop using invariant start (default = 8)")); +// Default value of zero implies we use the regular alias set tracker mechanism +// instead of the cross product using AA to identify aliasing of the memory +// location we are interested in. +static cl::opt<int> +LICMN2Theshold("licm-n2-threshold", cl::Hidden, cl::init(0), + cl::desc("How many instruction to cross product using AA")); + static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI); static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, @@ -105,8 +112,10 @@ static bool isSafeToExecuteUnconditionally(Instruction &Inst, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE, const Instruction *CtxI = nullptr); -static bool isInvalidatedByLoop(const MemoryLocation &MemLoc, - AliasSetTracker *CurAST); +static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, + AliasSetTracker *CurAST, Loop *CurLoop, + AliasAnalysis *AA); + static Instruction * CloneInstructionInExitBlock(Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, @@ -628,7 +637,16 @@ bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, if (isLoadInvariantInLoop(LI, DT, CurLoop)) return true; - bool Invalidated = isInvalidatedByLoop(MemoryLocation::get(LI), CurAST); + // Don't hoist loads which have may-aliased stores in loop. + uint64_t Size = 0; + if (LI->getType()->isSized()) + Size = I.getModule()->getDataLayout().getTypeStoreSize(LI->getType()); + + AAMDNodes AAInfo; + LI->getAAMetadata(AAInfo); + + bool Invalidated = pointerInvalidatedByLoop( + MemoryLocation(LI->getOperand(0), Size, AAInfo), CurAST, CurLoop, AA); // Check loop-invariant address because this may also be a sinkable load // whose address is not necessarily loop-invariant. if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand())) @@ -669,10 +687,9 @@ bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, if (AliasAnalysis::onlyAccessesArgPointees(Behavior)) { for (Value *Op : CI->arg_operands()) if (Op->getType()->isPointerTy() && - isInvalidatedByLoop(MemoryLocation(Op, - MemoryLocation::UnknownSize, - AAMDNodes()), - CurAST)) + pointerInvalidatedByLoop( + MemoryLocation(Op, MemoryLocation::UnknownSize, AAMDNodes()), + CurAST, CurLoop, AA)) return false; return true; } @@ -1569,13 +1586,51 @@ void LegacyLICMPass::deleteAnalysisLoop(Loop *L) { LICM.getLoopToAliasSetMap().erase(L); } -/// Return true if the body of this loop may store into the memory -/// location pointed to by V. -/// -static bool isInvalidatedByLoop(const MemoryLocation &MemLoc, - AliasSetTracker *CurAST) { - // Check to see if any of the basic blocks in CurLoop invalidate *V. - return CurAST->getAliasSetFor(MemLoc).isMod(); +static bool pointerInvalidatedByLoop(MemoryLocation MemLoc, + AliasSetTracker *CurAST, Loop *CurLoop, + AliasAnalysis *AA) { + // First check to see if any of the basic blocks in CurLoop invalidate *V. + bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod(); + + if (!isInvalidatedAccordingToAST || !LICMN2Theshold) + return isInvalidatedAccordingToAST; + + // Check with a diagnostic analysis if we can refine the information above. + // This is to identify the limitations of using the AST. + // The alias set mechanism used by LICM has a major weakness in that it + // combines all things which may alias into a single set *before* asking + // modref questions. As a result, a single readonly call within a loop will + // collapse all loads and stores into a single alias set and report + // invalidation if the loop contains any store. For example, readonly calls + // with deopt states have this form and create a general alias set with all + // loads and stores. In order to get any LICM in loops containing possible + // deopt states we need a more precise invalidation of checking the mod ref + // info of each instruction within the loop and LI. This has a complexity of + // O(N^2), so currently, it is used only as a diagnostic tool since the + // default value of LICMN2Threshold is zero. + + // Don't look at nested loops. + if (CurLoop->begin() != CurLoop->end()) + return true; + + int N = 0; + for (BasicBlock *BB : CurLoop->getBlocks()) + for (Instruction &I : *BB) { + if (N >= LICMN2Theshold) { + LLVM_DEBUG(dbgs() << "Alasing N2 threshold exhausted for " + << *(MemLoc.Ptr) << "\n"); + return true; + } + N++; + auto Res = AA->getModRefInfo(&I, MemLoc); + if (isModSet(Res)) { + LLVM_DEBUG(dbgs() << "Aliasing failed on " << I << " for " + << *(MemLoc.Ptr) << "\n"); + return true; + } + } + LLVM_DEBUG(dbgs() << "Aliasing okay for " << *(MemLoc.Ptr) << "\n"); + return false; } /// Little predicate that returns true if the specified basic block is in |

