llvm-project/llvm/lib/Analysis
Nikita Popov 649e125451 [BDCE][DemandedBits] Detect dead uses of undead instructions
This (mostly) fixes https://bugs.llvm.org/show_bug.cgi?id=39771.

BDCE currently detects instructions that don't have any demanded bits
and replaces their uses with zero. However, if an instruction has
multiple uses, then some of the uses may be dead (have no demanded bits)
even though the instruction itself is still live. This patch extends
DemandedBits/BDCE to detect such uses and replace them with zero.
While this will not immediately render any instructions dead, it may
lead to simplifications (in the motivating case, by converting a rotate
into a simple shift), break dependencies, etc.

The implementation tries to strike a balance between analysis power and
complexity/memory usage. Originally I wanted to track demanded bits on
a per-use level, but ultimately we're only really interested in whether
a use is entirely dead or not. I'm using an extra set to track which uses
are dead. However, as initially all uses are dead, I'm not storing uses
those user is also dead. This case is checked separately instead.

The test case has a couple of cases that are not simplified yet. In
particular, we're only looking at uses of instructions right now. I think
it would make sense to also extend this to arguments. Furthermore
DemandedBits doesn't yet know some of the tricks that InstCombine does
for the demanded bits or bitwise or/and/xor in combination with known
bits information.

Differential Revision: https://reviews.llvm.org/D55563

llvm-svn: 349674
2018-12-19 19:56:21 +00:00
..
AliasAnalysis.cpp Allow subclassing ExternalAA 2018-11-07 20:26:42 +00:00
AliasAnalysisEvaluator.cpp [MSSA] Print more optimization information 2018-06-14 19:55:53 +00:00
AliasAnalysisSummary.cpp
AliasAnalysisSummary.h Revert r332657: "[AA] cfl-anders-aa with field sensitivity" 2018-05-17 21:56:39 +00:00
AliasSetTracker.cpp [AliasSetTracker] Misc cleanup (NFCI) 2018-11-01 23:37:51 +00:00
Analysis.cpp [stack-safety] Empty local passes for Stack Safety Global Analysis 2018-11-26 23:05:48 +00:00
AssumptionCache.cpp
BasicAliasAnalysis.cpp Replace most users of UnknownSize with LocationSize::unknown(); NFC 2018-10-10 21:28:44 +00:00
BlockFrequencyInfo.cpp Revert r320104: infinite loop profiling bug fix 2017-12-08 19:38:07 +00:00
BlockFrequencyInfoImpl.cpp llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...) 2018-09-27 02:13:45 +00:00
BranchProbabilityInfo.cpp [TI removal] Make variables declared as `TerminatorInst` and initialized 2018-10-15 10:04:59 +00:00
CFG.cpp [TI removal] Make variables declared as `TerminatorInst` and initialized 2018-10-15 10:04:59 +00:00
CFGPrinter.cpp [CFG Printer] Add support for writing the dot files with a custom 2018-10-09 04:30:23 +00:00
CFLAndersAliasAnalysis.cpp Replace most users of UnknownSize with LocationSize::unknown(); NFC 2018-10-10 21:28:44 +00:00
CFLGraph.h [IR] Replace `isa<TerminatorInst>` with `isTerminator()`. 2018-08-26 09:51:22 +00:00
CFLSteensAliasAnalysis.cpp Rename DEBUG macro to LLVM_DEBUG. 2018-05-14 12:53:11 +00:00
CGSCCPassManager.cpp [NewPM] fixing asserts on deleted loop in -print-after-all 2018-12-11 19:05:35 +00:00
CMakeLists.txt [stack-safety] Empty local passes for Stack Safety Local Analysis 2018-11-26 21:57:47 +00:00
CallGraph.cpp llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...) 2018-09-27 02:13:45 +00:00
CallGraphSCCPass.cpp Fixing -print-module-scope for legacy SCC passes 2018-12-03 14:48:15 +00:00
CallPrinter.cpp Revert "Extend CFGPrinter and CallPrinter with Heat Colors" 2018-06-29 17:48:58 +00:00
CaptureTracking.cpp [CaptureTracking] Pass MaxUsesToExplore from wrappers to the actual implementation 2018-12-18 03:32:33 +00:00
CmpInstAnalysis.cpp [CmpInstAnalysis] fix function signature for ICmp code to predicate; NFC 2018-12-04 18:53:27 +00:00
CodeMetrics.cpp Rename DEBUG macro to LLVM_DEBUG. 2018-05-14 12:53:11 +00:00
ConstantFolding.cpp [ConstantFolding] Handle leading zero-size elements in load folding 2018-12-11 20:29:16 +00:00
CostModel.cpp Remove redundant includes from lib/Analysis. 2017-12-13 21:30:41 +00:00
Delinearization.cpp [NFC] Consolidate six getPointerOperand() utility functions into one place 2018-03-09 21:05:58 +00:00
DemandedBits.cpp [BDCE][DemandedBits] Detect dead uses of undead instructions 2018-12-19 19:56:21 +00:00
DependenceAnalysis.cpp Replace most users of UnknownSize with LocationSize::unknown(); NFC 2018-10-10 21:28:44 +00:00
DivergenceAnalysis.cpp [DA] GPUDivergenceAnalysis for unstructured GPU kernels 2018-11-30 22:55:20 +00:00
DomPrinter.cpp Revert "Extend CFGPrinter and CallPrinter with Heat Colors" 2018-06-29 17:48:58 +00:00
DominanceFrontier.cpp IWYU for llvm-config.h in llvm, additions. 2018-04-30 14:59:11 +00:00
EHPersonalities.cpp [TI removal] Make variables declared as `TerminatorInst` and initialized 2018-10-15 10:04:59 +00:00
GlobalsModRef.cpp Remove trailing space 2018-07-30 19:41:25 +00:00
GuardUtils.cpp Re-enable "[NFC] Unify guards detection" 2018-08-30 03:39:16 +00:00
IVDescriptors.cpp Fix parenthesis warning in IVDescriptors 2018-11-30 13:54:36 +00:00
IVUsers.cpp Rename DEBUG macro to LLVM_DEBUG. 2018-05-14 12:53:11 +00:00
IndirectCallPromotionAnalysis.cpp Rename DEBUG macro to LLVM_DEBUG. 2018-05-14 12:53:11 +00:00
InlineCost.cpp [Inliner] Penalise inlining of calls with loops at Oz 2018-11-05 14:54:34 +00:00
InstCount.cpp
InstructionPrecedenceTracking.cpp [LICM] Hoist guards from non-header blocks 2018-11-12 09:29:58 +00:00
InstructionSimplify.cpp [InstSimplify] Simplify saturating add/sub + icmp 2018-12-17 17:45:18 +00:00
Interval.cpp
IntervalPartition.cpp
IteratedDominanceFrontier.cpp [IDF] Teach Iterated Dominance Frontier to use a snapshot CFG based on a GraphDiff. 2018-08-17 17:39:15 +00:00
LLVMBuild.txt
LazyBlockFrequencyInfo.cpp Require DominatorTree when requiring/preserving LoopInfo in the old pass manager 2018-05-17 09:05:40 +00:00
LazyBranchProbabilityInfo.cpp Require DominatorTree when requiring/preserving LoopInfo in the old pass manager 2018-05-17 09:05:40 +00:00
LazyCallGraph.cpp ADT/STLExtras: Introduce llvm::empty; NFC 2018-10-31 00:23:23 +00:00
LazyValueInfo.cpp [LVI] run transfer function for binary operator even when the RHS isn't a constant 2018-11-21 05:24:12 +00:00
LegacyDivergenceAnalysis.cpp LegacyDivergenceAnalysis: fix uninitialized value 2018-11-30 23:07:49 +00:00
Lint.cpp Remove \brief commands from doxygen comments. 2018-05-01 15:54:18 +00:00
Loads.cpp Fix aliasing of launder.invariant.group 2018-05-23 09:16:44 +00:00
LoopAccessAnalysis.cpp [LAA] Introduce enum for vectorization safety status (NFC). 2018-12-18 22:25:11 +00:00
LoopAnalysisManager.cpp [LoopPassManager] MemorySSA should be preserved when enabled. 2018-09-06 20:54:24 +00:00
LoopInfo.cpp [Unroll/UnrollAndJam/Vectorizer/Distribute] Add followup loop attributes. 2018-12-12 17:32:52 +00:00
LoopPass.cpp [LoopPass] fixing 'Modification' messages in -debug-pass=Executions for loop passes 2018-11-19 15:10:59 +00:00
LoopUnrollAnalyzer.cpp Remove \brief commands from doxygen comments. 2018-05-01 15:54:18 +00:00
MemDepPrinter.cpp Remove trailing space 2018-07-30 19:41:25 +00:00
MemDerefPrinter.cpp Remove redundant includes from lib/Analysis. 2017-12-13 21:30:41 +00:00
MemoryBuiltins.cpp Reverting r340807. 2018-08-30 18:37:18 +00:00
MemoryDependenceAnalysis.cpp Revert r348645 - "[MemCpyOpt] memset->memcpy forwarding with undef tail" 2018-12-13 03:15:11 +00:00
MemoryLocation.cpp Replace most users of UnknownSize with LocationSize::unknown(); NFC 2018-10-10 21:28:44 +00:00
MemorySSA.cpp [MemorySSA] Create query after checking if instruction is a fence. 2018-11-13 21:12:49 +00:00
MemorySSAUpdater.cpp [IR] Add hasNPredecessors, hasNPredecessorsOrMore to BasicBlock 2018-11-19 19:54:27 +00:00
ModuleDebugInfoPrinter.cpp Remove redundant includes from lib/Analysis. 2017-12-13 21:30:41 +00:00
ModuleSummaryAnalysis.cpp [ThinLTO] Compute synthetic function entry count 2018-12-13 19:54:27 +00:00
MustExecute.cpp [LICM] Hoist guards from non-header blocks 2018-11-12 09:29:58 +00:00
ObjCARCAliasAnalysis.cpp [ModRefInfo] Make enum ModRefInfo an enum class [NFC]. 2017-12-07 22:41:34 +00:00
ObjCARCAnalysisUtils.cpp Remove \brief commands from doxygen comments. 2018-05-01 15:54:18 +00:00
ObjCARCInstKind.cpp Change the objc ARC optimizer to use the new objc.* intrinsics 2018-12-18 20:32:49 +00:00
OptimizationRemarkEmitter.cpp Remove redundant includes from lib/Analysis. 2017-12-13 21:30:41 +00:00
OrderedBasicBlock.cpp [NFC] Sanitizing asserts for OrderedBasicBlock 2018-09-11 08:46:19 +00:00
OrderedInstructions.cpp [NFC] Move OrderedInstructions and InstructionPrecedenceTracking to Analysis 2018-08-30 04:49:03 +00:00
PHITransAddr.cpp IWYU for llvm-config.h in llvm, additions. 2018-04-30 14:59:11 +00:00
PhiValues.cpp [PhiValues] Use callback value handles to invalidate deleted values 2018-08-24 15:48:30 +00:00
PostDominators.cpp [Dominators] Add PDT constructor from Function 2018-05-23 17:29:21 +00:00
ProfileSummaryInfo.cpp [SampleFDO] handle ProfileSampleAccurate when initializing function entry count 2018-12-13 21:51:42 +00:00
PtrUseVisitor.cpp
README.txt
RegionInfo.cpp Test commit, fix a minor typo. 2018-07-22 20:04:42 +00:00
RegionPass.cpp [NFC][PassTiming] factor out generic PassTimingInfo 2018-08-28 21:06:51 +00:00
RegionPrinter.cpp Revert "Extend CFGPrinter and CallPrinter with Heat Colors" 2018-06-29 17:48:58 +00:00
ScalarEvolution.cpp [SCEV][NFC] Verify IR in isLoop[Entry,Backedge]GuardedByCond 2018-11-08 05:07:58 +00:00
ScalarEvolutionAliasAnalysis.cpp Make LocationSize a proper Optional type; NFC 2018-10-09 03:18:56 +00:00
ScalarEvolutionExpander.cpp Revert r347934 "[SCEV] Guard movement of insertion point for loop-invariants" 2018-12-05 23:13:50 +00:00
ScalarEvolutionNormalization.cpp
ScopedNoAliasAA.cpp [ModRefInfo] Make enum ModRefInfo an enum class [NFC]. 2017-12-07 22:41:34 +00:00
StackSafetyAnalysis.cpp [stack-safety] Update comment 2018-11-27 01:56:44 +00:00
StratifiedSets.h Remove \brief commands from doxygen comments. 2018-05-01 15:54:18 +00:00
SyncDependenceAnalysis.cpp [TI removal] Switch some newly added code over to use `Instruction` 2018-10-19 00:22:10 +00:00
SyntheticCountsUtils.cpp [ThinLTO] Compute synthetic function entry count 2018-12-13 19:54:27 +00:00
TargetLibraryInfo.cpp Revert unapproved commit 2018-11-24 07:26:55 +00:00
TargetTransformInfo.cpp [TTI] getOperandInfo - a broadcast shuffle means the result is OK_UniformValue 2018-11-14 15:04:08 +00:00
Trace.cpp IWYU for llvm-config.h in llvm, additions. 2018-04-30 14:59:11 +00:00
TypeBasedAliasAnalysis.cpp Fix more spelling mistakes in comments of LLVM Analysis passes 2018-03-02 18:57:02 +00:00
TypeMetadataUtils.cpp [WPD] Fix incorrect devirtualization after indirect call promotion 2018-09-27 14:55:32 +00:00
ValueLattice.cpp [LVI] Move LVILatticeVal class to separate header file (NFC). 2017-09-28 11:09:22 +00:00
ValueLatticeUtils.cpp [IPSCCP] Move common functions to ValueLatticeUtils (NFC) 2017-10-13 17:53:44 +00:00
ValueTracking.cpp [ValueTracking] remove unused parameters from helper functions; NFC 2018-12-19 16:49:18 +00:00
VectorUtils.cpp [VectorUtils] Use namespace for InterleaveGroup template specialization. 2018-11-13 16:26:34 +00:00

README.txt

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))

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