1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00
llvm-mirror/lib/Analysis
Philip Reames 8ae2dfce83 [SCEV] Compute exit counts for unsigned IVs using mustprogress semantics
The motivation here is simple loops with unsigned induction variables w/non-one steps. A toy example would be:
for (unsigned i = 0; i < N; i += 2) { body; }

Given C/C++ semantics, we do not get the nuw flag on the induction variable. Given that lack, we currently can't compute a bound for this loop. We can do better for many cases, depending on the contents of "body".

The basic intuition behind this patch is as follows:
* A step which evenly divides the iteration space must wrap through the same numbers repeatedly. And thus, we can ignore potential cornercases where we exit after the n-th wrap through uint32_max.
* Per C++ rules, infinite loops without side effects are UB. We already have code in SCEV which relies on this.  In LLVM, this is tied to the mustprogress attribute.

Together, these let us conclude that the trip count of this loop must come before unsigned overflow unless the body would form a well defined infinite loop.

A couple notes for those reading along:
* I reused the loop properties code which is overly conservative for this case. I may follow up in another patch to generalize it for the actual UB rules.
* We could cache the n(s/u)w facts. I left that out because doing a pre-patch which cached existing inference showed a lot of diffs I had trouble fully explaining. I plan to get back to this, but I don't want it on the critical path.

Differential Revision: https://reviews.llvm.org/D103118
2021-06-07 11:24:00 -07:00
..
models/inliner
AliasAnalysis.cpp [AA] Support callCapturesBefore() on BatchAA (NFCI) 2021-05-14 21:48:08 +02:00
AliasAnalysisEvaluator.cpp
AliasAnalysisSummary.cpp
AliasAnalysisSummary.h
AliasSetTracker.cpp
Analysis.cpp
AssumeBundleQueries.cpp AssumeBundleQueries.cpp - don't dereference a dyn_cast<> result. NFCI. 2021-06-06 15:25:03 +01:00
AssumptionCache.cpp
BasicAliasAnalysis.cpp BasicAA: Recognize inttoptr as isEscapeSource 2021-05-07 07:48:50 -07:00
BlockFrequencyInfo.cpp Internalize some cl::opt global variables or move them under namespace llvm 2021-05-07 11:15:43 -07:00
BlockFrequencyInfoImpl.cpp Internalize some cl::opt global variables or move them under namespace llvm 2021-05-07 11:15:43 -07:00
BranchProbabilityInfo.cpp Fix some -Wunused-but-set-variable in -DLLVM_ENABLE_ASSERTIONS=off build 2021-06-04 23:34:43 -07:00
CallGraph.cpp
CallGraphSCCPass.cpp Internalize some cl::opt global variables or move them under namespace llvm 2021-05-07 11:15:43 -07:00
CallPrinter.cpp
CaptureTracking.cpp [CaptureTracking] Simplify reachability check (NFCI) 2021-05-16 16:04:10 +02:00
CFG.cpp [CFG] Move reachable from entry checks into basic block variant 2021-05-15 15:42:02 +02:00
CFGPrinter.cpp NFC. Refactor DOTGraphTraits::isNodeHidden 2021-06-03 11:27:06 -07:00
CFLAndersAliasAnalysis.cpp
CFLGraph.h
CFLSteensAliasAnalysis.cpp
CGSCCPassManager.cpp Revert "[NewPM] Only invalidate modified functions' analyses in CGSCC passes" 2021-05-21 16:38:03 -07:00
CMakeLists.txt [InstCombine] Fold overflow bit of [u|s]mul.with.overflow in a poison-safe way 2021-05-02 11:54:12 +09:00
CmpInstAnalysis.cpp
CodeMetrics.cpp
ConstantFolding.cpp [OpaquePtr] Create API to make a copy of a PointerType with some address space 2021-06-01 16:52:32 -07:00
ConstraintSystem.cpp
CostModel.cpp
DDG.cpp
DDGPrinter.cpp
Delinearization.cpp
DemandedBits.cpp Add getDemandedBits for uses. 2021-06-02 10:07:40 -04:00
DependenceAnalysis.cpp [Dependence Analysis] Enable delinearization of fixed sized arrays 2021-05-10 10:30:15 -07:00
DependenceGraphBuilder.cpp
DevelopmentModeInlineAdvisor.cpp
DivergenceAnalysis.cpp
DominanceFrontier.cpp
DomPrinter.cpp
DomTreeUpdater.cpp
EHPersonalities.cpp
FunctionPropertiesAnalysis.cpp
GlobalsModRef.cpp
GuardUtils.cpp
HeatUtils.cpp
ImportedFunctionsInliningStatistics.cpp
IndirectCallPromotionAnalysis.cpp
InlineAdvisor.cpp
InlineCost.cpp [NFC] Use getParamByValType instead of pointee type 2021-05-01 21:22:41 -07:00
InlineSizeEstimatorAnalysis.cpp
InstCount.cpp
InstructionPrecedenceTracking.cpp
InstructionSimplify.cpp [InstSimplify] Treat invariant group insts as bitcasts for load operands 2021-06-01 16:33:06 -07:00
Interval.cpp
IntervalPartition.cpp
IRSimilarityIdentifier.cpp
IVDescriptors.cpp [LV] Try to sink users recursively for first-order recurrences. 2021-05-31 19:55:33 +01:00
IVUsers.cpp
LazyBlockFrequencyInfo.cpp Make dependency between certain analysis passes transitive (reapply) 2021-05-05 15:17:55 +02:00
LazyBranchProbabilityInfo.cpp Make dependency between certain analysis passes transitive (reapply) 2021-05-05 15:17:55 +02:00
LazyCallGraph.cpp
LazyValueInfo.cpp [IR] Add BasicBlock::isEntryBlock() (NFC) 2021-05-15 12:41:58 +02:00
LegacyDivergenceAnalysis.cpp
Lint.cpp
Loads.cpp
LoopAccessAnalysis.cpp Revert "[LAA] Support pointer phis in loop by analyzing each incoming pointer." 2021-05-28 10:33:52 +01:00
LoopAnalysisManager.cpp [NewPM] Don't mark AA analyses as preserved 2021-05-18 13:49:03 -07:00
LoopCacheAnalysis.cpp [SCEV] Add a utility for converting from "exit count" to "trip count" 2021-05-26 10:41:49 -07:00
LoopInfo.cpp [LoopNest] Consider loop nest with inner loop guard using outer loop 2021-05-07 16:04:18 +00:00
LoopNestAnalysis.cpp [LoopNest] Consider loop nest with inner loop guard using outer loop 2021-05-07 16:04:18 +00:00
LoopPass.cpp
LoopUnrollAnalyzer.cpp [unroll] Use value domain for symbolic execution based cost model 2021-05-26 08:41:25 -07:00
MemDepPrinter.cpp
MemDerefPrinter.cpp
MemoryBuiltins.cpp
MemoryDependenceAnalysis.cpp [NFC] MemoryDependenceAnalysis cleanup. 2021-05-31 18:07:55 +03:00
MemoryLocation.cpp
MemorySSA.cpp [IR] Add BasicBlock::isEntryBlock() (NFC) 2021-05-15 12:41:58 +02:00
MemorySSAUpdater.cpp
MLInlineAdvisor.cpp
ModuleDebugInfoPrinter.cpp
ModuleSummaryAnalysis.cpp
MustExecute.cpp
ObjCARCAliasAnalysis.cpp
ObjCARCAnalysisUtils.cpp
ObjCARCInstKind.cpp
OptimizationRemarkEmitter.cpp
OverflowInstAnalysis.cpp Fix MSan crash after 1977c53b 2021-05-02 13:44:43 +09:00
PHITransAddr.cpp
PhiValues.cpp
PostDominators.cpp
ProfileSummaryInfo.cpp
PtrUseVisitor.cpp
README.txt
RegionInfo.cpp
RegionPass.cpp
RegionPrinter.cpp
ReleaseModeModelRunner.cpp
ReplayInlineAdvisor.cpp
ScalarEvolution.cpp [SCEV] Compute exit counts for unsigned IVs using mustprogress semantics 2021-06-07 11:24:00 -07:00
ScalarEvolutionAliasAnalysis.cpp [NewPM] Don't mark AA analyses as preserved 2021-05-18 13:49:03 -07:00
ScalarEvolutionDivision.cpp
ScalarEvolutionNormalization.cpp
ScopedNoAliasAA.cpp
StackLifetime.cpp
StackSafetyAnalysis.cpp
StratifiedSets.h
SyncDependenceAnalysis.cpp
SyntheticCountsUtils.cpp
TargetLibraryInfo.cpp [SimplifyLibCalls] Take size of int into consideration when emitting ldexp/ldexpf 2021-06-02 11:40:34 +02:00
TargetTransformInfo.cpp [TTI] NFC: Change getIntImmCodeSizeCost to return InstructionCost. 2021-06-02 16:04:11 +03:00
TFUtils.cpp
Trace.cpp
TypeBasedAliasAnalysis.cpp
TypeMetadataUtils.cpp
ValueLattice.cpp
ValueLatticeUtils.cpp
ValueTracking.cpp Recommit "Generalize getInvertibleOperand recurrence handling slightly" 2021-05-03 16:40:56 -07:00
VectorUtils.cpp [NFC][OpaquePtr] Avoid using PointerType::getElementType() in VectorUtils.cpp 2021-05-17 18:35:44 -07:00
VFABIDemangling.cpp

Analysis Opportunities:

//===---------------------------------------------------------------------===//

In test/Transforms/LoopStrengthReduce/quadradic-exit-value.ll, the
ScalarEvolution expression for %r is this:

  {1,+,3,+,2}<loop>

Outside the loop, this could be evaluated simply as (%n * %n), however
ScalarEvolution currently evaluates it as

  (-2 + (2 * (trunc i65 (((zext i64 (-2 + %n) to i65) * (zext i64 (-1 + %n) to i65)) /u 2) to i64)) + (3 * %n))

In addition to being much more complicated, it involves i65 arithmetic,
which is very inefficient when expanded into code.

//===---------------------------------------------------------------------===//

In formatValue in test/CodeGen/X86/lsr-delayed-fold.ll,

ScalarEvolution is forming this expression:

((trunc i64 (-1 * %arg5) to i32) + (trunc i64 %arg5 to i32) + (-1 * (trunc i64 undef to i32)))

This could be folded to

(-1 * (trunc i64 undef to i32))

//===---------------------------------------------------------------------===//