aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ValueTracking.cpp
AgeCommit message (Collapse)AuthorFilesLines
2022-11-17ValueTracking: Look through copysign in isKnownNeverInfinityMatt Arsenault1-0/+1
2022-11-17ValueTracking: Look through fneg in isKnownNeverNaNMatt Arsenault1-0/+1
2022-11-17ValueTracking: Look through fabs and fneg in isKnownNeverInfinityMatt Arsenault1-0/+2
2022-11-17ValueTracking: Look through canonicalize in isKnownNeverInfinityMatt Arsenault1-0/+9
2022-11-01Address post commit review feedback from D137046Philip Reames1-4/+2
It was pointed out the verifier rejects inttoptr and ptrtoint casts with inputs and outputs whose scalability doesn't match. As such, checking the input type separately from the type of the cast itself is redundant.
2022-11-01Allow scalable vectors in ComputeNumSignBits and isKnownNonNullPhilip Reames1-22/+6
This is a follow up to D136470 which extends the same scheme used there to ComputeNumSignBits and isKnownNonNull. As a reminder, for scalable vectors we track a single bit which is implicitly broadcast to all lanes. We do not know how many lanes there are statically, and thus have to be conservative along paths which require exact sizes. Differential Revision: https://reviews.llvm.org/D137046
2022-10-31Address post commit style comment from 087bb0fPhilip Reames1-2/+3
2022-10-31[ValueTracking] Improve performance of programUndefinedIfUndefOrPoison (NFC)Geza Lore1-8/+10
programUndefinedIfUndefOrPoison used to eagerly propagate the fact that a value is poison to the users of the value. The problem is that if the value has a lot of uses (orders of magnitude more than the scanning limit we use in this function), then we spend the bulk of our time in eagerly propagating the poison property, which we will mostly never use later anyway due to the scanning limit. I have a test case (of ~50k lines of machine generated C++), where this results in ~60% of 35s compilation time being spent doing just this eager propagation. This patch changes programUndefinedIfUndefOrPoison to only propagate to instructions actually visited, looking back to see if their operands are poison. This should be equivalent and no functional change is intended, but we regain virtually all of the 60% compilation time spent in this function in my test case (i.e.: a 2.5x total compilation speedup). Differential Revision: https://reviews.llvm.org/D137027
2022-10-30[ValueTracking] Assert known bits sanity in isKnownNonZeroPhilip Reames1-0/+14
These are the same asserts we have in other query routines; cover this interface too.
2022-10-30[VectorUtils] Add getShuffleDemandedElts helperSimon Pilgrim1-26/+3
We have similar code to translate a demanded elements mask for a shuffle's operands in multiple places - this patch adds a helper function to VectorUtils and updates a number of locations to use it directly. Differential Revision: https://reviews.llvm.org/D136832
2022-10-30Allow scalable vectors in computeKnownBitsPhilip Reames1-16/+18
This extends the computeKnownBits analysis to support scalable vectors. The critical detail is in deciding how to represent the demanded elements of a vector whose length is unknown at compile time. For this patch, I adopt the convention that we track one bit which corresponds to all lanes. That is, that bit is implicitly broadcast to all lanes of the scalable vector resulting in all lanes being demanded. This is the same convention we use in getSplatValue in SelectionDAG. Note that this convention doesn't actually impact much. Most of the code is agnostic to the interpretation of the demanded elements, and the few cases which actually care need case by case handling anyways. In this patch, I just bail out of those cases. A prior patch (D128159) proposed using a different convention in SDAG. I don't see any strong reason to prefer one scheme over the other, so I propose we go with this one as it's conceptually the simplest. Getting known and demanded bit optimizations unblocked at all is a significant win. I've locally implemented this scheme in reasonable large parts of ValueTracking.cpp and SelectionDAG equivalents, and have not hit any blockers. If this is approved, I plan to post a series of patches plumbing this through all the relevant parts. In the discussion on that patch, a preference was expressed for introducing some form of abstraction around the demanded elements. I'll note that I've played with several variations on that idea locally, and have yet to find anything which results in more readable code. If anyone has concrete ideas in this area, I'm happy to explore in follow up patches. I'd strongly prefer to be making API changes in NFC manner with tests in place. Differential Revision: https://reviews.llvm.org/D136470
2022-10-20[NFC] Fix a few whitespace inconsistencies.Paul Walker1-1/+1
2022-10-07[ValueTracking][SimplifyLibCalls] Fix bug in getConstantDataArrayInfo for ↵Bjorn Pettersson1-3/+11
wchar_t When SimplifyLibCalls is dealing with wchar_t (e.g. optimizing wcslen) it uses ValueTracking helpers with a CharSize/ElementSize that isn't 8, but rather 16 or 32 (to match with the size in bits of a wchar_t). Problem I've seen is that llvm::getConstantDataArrayInfo is taking both an "ElementSize" argument (basically indicating size of a char/element in bits) and an "Offset" which afaict is an offset in the unit "number of elements". Then it also use stripAndAccumulateConstantOffsets to get a "StartIdx" which afaict is calculated in bytes. The returned Slice.Length is based on arithmetics that add/subtract variables that are having different units (bytes vs elements). Most notably I think the "StartIdx" must be scaled using the "ElementSize" to get correct results. The symptom of the above problem was seen in the wcslen-1.ll test case which miscompiled. This patch is supposed to resolve the bug by converting between bytes and elements when needed. Differential Revision: https://reviews.llvm.org/D135263
2022-10-07[ValueTracking] Remove unused Offset argument in getConstantStringInfo() (NFC)Nikita Popov1-2/+2
2022-10-04[ValueTracking] Handle constant exprs in isKnownNonZero()Nikita Popov1-13/+7
Handle constant expressions by falling through to the general operator-based code. In particular, this adds support for bitcast and GEP expressions.
2022-10-04[ValueTracking] Avoid known bits fallthrough for freeze (NFCI)Nikita Popov1-4/+3
The known bits logic should never produce a better result than the direct recursive non-zero query here, so skip the fallthrough.
2022-10-04[ValueTracking] Switch isKnownNonZero() to switch over opcodes (NFCI)Nikita Popov1-91/+99
The change in the assume-queries-counter.ll test is because we skip and unnecessary known bits query for arguments.
2022-10-02[ValueTracking] peek through fpext in isKnownNeverInfinity()Sanjay Patel1-0/+4
https://alive2.llvm.org/ce/z/BkNoRW
2022-09-29[ValueTracking] Fix CannotBeOrderedLessThanZero() for fdiv (PR58046)Nikita Popov1-2/+11
When checking the RHS of fdiv, we should set the SignBitOnly flag, because a negative zero can become -Inf, which is ordered less than zero. Fixes https://github.com/llvm/llvm-project/issues/58046. Differential Revision: https://reviews.llvm.org/D134876
2022-09-19Analysis: Add AssumptionCache to isSafeToSpeculativelyExecuteMatt Arsenault1-6/+7
Does not update any of the uses.
2022-09-19Analysis: Add AssumptionCache argument to isDereferenceableAndAlignedPointerMatt Arsenault1-2/+3
This does not try to pass it through from the end users.
2022-08-25[ValueTracking][InstCombine] restrict FP min/max matching to avoid miscompileSanjay Patel1-3/+12
This is a long-standing FIXME with a non-FMF test that exposes the bug as shown in issue #57357. It's possible that there's still a way to miscompile by mis-identifying/mis-folding FP min/max patterns, but this patch only exposes a couple of seemingly minor regressions while preventing the broken transform.
2022-08-17[instcombine] Optimise for zero initialisation of product given fast flags ↵Zain Jaffal1-1/+2
are enabled Currently, clang ignores the 0 initialisation in finite math For example: ``` double f_prod = 0; double arr[1000]; for (size_t i = 0; i < 1000; i++) { f_prod *= arr[i]; } ``` Clang will ignore that `f_prod` is set to zero and it will generate assembly to iterate over the loop. Reviewed By: fhahn, spatel Differential Revision: https://reviews.llvm.org/D131672
2022-08-16[ValueTracking] computeKnownBits - attempt to use a branch condition feeding ↵Simon Pilgrim1-0/+36
a phi to improve known bits range (PR38280) If computeKnownBits encounters a phi node, and we fail to determine any known bits through direct analysis, see if the incoming value is part of a branch condition feeding the phi. Handle cases where icmp(IncomingValue PRED Constant) is driving a branch instruction feeding that phi node - at the moment this only handles EQ/ULT/ULE predicate cases as they are the most straightforward to handle and most likely for branch-loop 'max upper bound' cases - we can extend this if/when necessary. I investigated a more general icmp(LHS PRED RHS) KnownBits system, but the hard limits we put on value tracking depth through phi nodes meant that we were mainly catching constants anyhow. Fixes the pointless vectorization in PR38280 / Issue #37628 (excessive unrolling still needs handling though) Differential Revision: https://reviews.llvm.org/D131838
2022-08-08[llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFCFangrui Song1-5/+5
With C++17 there is no Clang pedantic warning or MSVC C5051.
2022-08-04[ValueTracking] improve readability in isImpliedCond helper functions; NFCSanjay Patel1-41/+40
This matches the caller code naming scheme and avoids the potentially confusing transition from left/right to A/B.
2022-08-04[ValueTracking] reduce code in isImpliedCondICmps; NFCSanjay Patel1-8/+3
This copies the implementation of the subsequent match with constants.
2022-07-26[InstCombine] Fold strtoul and strtoull and avoid PR #56293Martin Sebor1-3/+4
Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D129224
2022-07-25[ValueTracking] Fix unused variable warning in release builds. NFCBenjamin Kramer1-2/+2
2022-07-24[ValueTracking] allow vector types in isImpliedCondition()Sanjay Patel1-23/+9
The matching of constants assumed integers, but we can handle splat vector constants seamlessly with m_APInt.
2022-07-16[Analysis] Qualify auto variables in for loops (NFC)Kazu Hirata1-6/+6
2022-07-06[ValueTracking] Accept Instruction in isSafeToSpeculativelyExecute() (NFC)Nikita Popov1-12/+7
As constant expressions can no longer trap, it only makes sense to call isSafeToSpeculativelyExecute on Instructions, so limit the API to accept only them, rather than general Operators or Values.
2022-07-06[IR] Remove Constant::canTrap() (NFC)Nikita Popov1-5/+0
As integer div/rem constant expressions are no longer supported, constants can no longer trap and are always safe to speculate. Remove the Constant::canTrap() method and its usages.
2022-06-28[InstCombine] Look through more casts when folding memchr and memcmpMartin Sebor1-39/+34
Enhance getConstantDataArrayInfo to let the memchr and memcmp library call folders look through arbitrarily long sequences of bitcast and GEP instructions. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D128364
2022-06-23[ValueTracking] Teach isKnownNonZero that a vscale is never 0.David Green1-0/+3
A llvm.vscale will always be at least 1, never zero. Teaching that to isKnownNonZero can help fold away some statically known compares. Differential Revision: https://reviews.llvm.org/D128217
2022-06-21[InstCombine] handle subobjects of constant aggregatesMartin Sebor1-34/+64
Remove the known limitation of the library function call folders to only work with top-level arrays of characters (as per the TODO comment in the code) and allows them to also fold calls involving subobjects of constant aggregates such as member arrays.
2022-06-20[llvm] Don't use Optional::getValue (NFC)Kazu Hirata1-2/+1
2022-06-20[llvm] Don't use Optional::hasValue (NFC)Kazu Hirata1-1/+1
2022-06-19[ValueTracking] recognize sub X, (X -nuw Y) as not overflowingSanjay Patel1-3/+10
This extends a similar pattern from D125500 and D127754. If we know that operand 1 (RHS) of a subtract is itself a non-overflowing subtract from operand 0 (LHS), then the final/outer subtract is also non-overflowing: https://alive2.llvm.org/ce/z/Bqan8v InstCombine uses this analysis to trigger a narrowing optimization, so that is what the first changed test shows. The last test models a motivating case from issue #48013. In that example, we determine 'nuw' on the first sub from the urem, then we determine that the 2nd sub can be narrowed, and that leads to eliminating both subtracts. here are still several missing subtract narrowing optimizations demonstrated in the tests above the diffs shown here - those should be handled in InstCombine with another set of patches.
2022-06-14[ValueTracking] recognize sub X, (X -nsw Y) as not overflowingSanjay Patel1-3/+9
This extends a similar pattern from D125500. If we know that operand 1 (RHS) of a subtract is itself a non-overflowing subtract from operand 0 (LHS), then the final/outer subtract is also non-overflowing: https://alive2.llvm.org/ce/z/Bqan8v InstCombine uses this analysis to trigger a narrowing optimization, so that is what the first changed test shows. The last test models the motivating case from issue #48013. In that example, we determine 'nsw' on the first sub from the srem, then we determine that the 2nd sub can be narrowed, and that leads to eliminating both subtracts. This works for unsigned sub too, but I left that out to keep the patch minimal. If this looks ok, I will follow up with that change. There are also several missing subtract narrowing optimizations demonstrated in the tests above the diffs shown here - those should be handled in InstCombine with another set of patches. Differential Revision: https://reviews.llvm.org/D127754
2022-06-07[ValueTracking] Add support to deduce a PHI node being a power of 2 if each ↵William Huang1-1/+63
incoming value is a power of 2. Reviewed By: davidxl Differential Revision: https://reviews.llvm.org/D124889
2022-06-01[ValueTracking] Enable -branch-on-poison-as-ub by defaultNikita Popov1-6/+5
Now that SimpleLoopUnswitch and other transforms no longer introduce branch on poison, enable the -branch-on-poison-as-ub option by default. The practical impact of this is mostly better flag preservation in SCEV, and some freeze instructions no longer being necessary. Differential Revision: https://reviews.llvm.org/D125299
2022-05-30Fix warning for unused variable in the non-assert build (NFC)Mehdi Amini1-0/+2
2022-05-30Re-land "[VP] vp intrinsics are not speculatable" with test fixSimon Moll1-3/+32
Update the llvmir-intrinsics.mlir test to account for the modified attribute sets. This reverts commit 2e2a8a2d9082250e4aad312c6008a526f2b007c7.
2022-05-30Revert "[VP] vp intrinsics are not speculatable"Mehdi Amini1-32/+3
This reverts commit 78a18d2b54e7e8e0e2c1d1cb33d015d7f69b8cc7. Break MLIR bot: https://lab.llvm.org/buildbot/#/builders/61/builds/27127
2022-05-30[VP] vp intrinsics are not speculatableSimon Moll1-3/+32
VP intrinsics show UB if the %evl parameter is out of bounds - they must not carry the speculatable attribute. The out-of-bounds UB disappears when the %evl parameter is expanded into the mask or expansion replaces the entire VP intrinsic with non-VP code. This patch - Removes the speculatable attribute on all VP intrinsics. - Generalizes the isSafeToSpeculativelyExecute function to let VP expansion know whether the VP intrinsic replacement will be speculatable. VP expansion may only discard %evl where this is the case. Reviewed By: frasercrmck Differential Revision: https://reviews.llvm.org/D125296
2022-05-26[ValueTracking] Added support to deduce PHI Nodes values being a power of 2William Huang1-0/+19
Add Value Tracking support to deduce induction variable being a power of 2, allowing urem optimizations Reviewed By: davidxl Differential Revision: https://reviews.llvm.org/D126018
2022-05-25[ValueTracking] Loads with !dereferenceable metadata cannot be undef/poisonNikita Popov1-1/+3
A load with !dereferenceable or !dereferenceable_or_null metadata must return a well-defined (non-undef/poison) value. Effectively they imply !noundef. This is the same as we do for the dereferenceable(N) attribute. This should fix https://github.com/llvm/llvm-project/issues/55672, or at least the specific case discussed there. Differential Revision: https://reviews.llvm.org/D126296
2022-05-19Revert "[ValueTracking] Added support to deduce PHI Nodes values being a ↵Nico Weber1-19/+0
power of 2" This reverts commit d5c130f17e503e128b8a413c2ce0e522987d2a16. Breaks tests, see https://reviews.llvm.org/D125332#3525819
2022-05-19[ValueTracking] Added support to deduce PHI Nodes values being a power of 2William Huang1-0/+19
Add Value Tracking support to deduce induction variable being a power of 2, allowing urem optimizations Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D125332