1337 Commits

Author SHA1 Message Date
Yingwei Zheng
1fea712cd1
[ValueTracking] Infer X u<= X +nuw Y for any Y (#75524)
Alive2: https://alive2.llvm.org/ce/z/kiGxCf
Fixes #70374.
2023-12-15 16:33:39 +08:00
Nikita Popov
7686d49517 [ValueTracking] Handle returned attribute with mismatched type
The returned attribute can be used when it is possible to
"losslessly bitcast" between the argument and return type,
including between two vector types.

computeKnownBits() would crash in this case, isKnownNonZero()
would potentially produce a miscompile.

Fixes https://github.com/llvm/llvm-project/issues/74722.
2023-12-08 17:05:13 +01:00
Nikita Popov
292256673c [ValueTracking] Remove unused argument (NFC) 2023-12-08 10:25:04 +01:00
Craig Topper
32ec5fbfed
[ValueTracking] Use BinaryOperator instead of Operator in matchSimpleRecurrence. (#74678)
Operator allows the phi operand to be a ConstantExpr. A ConstantExpr is
a valid operand to a phi, but is never going to be a recurrence.
We can only match a BinaryOperator so use that instead.
2023-12-07 10:27:57 -08:00
Nikita Popov
6a1badfed2 [ValueTracking] Add missing check when computing known bits from pointer icmp
I'm not sure whether it's possible to cause a miscompile due to
the missing check right now, as the affected values mechanism
effectively protects us against this. This becomes a problem for
an upcoming patch though.
2023-12-07 14:10:59 +01:00
Nikita Popov
d77067d08a
[ValueTracking] Add dominating condition support in computeKnownBits() (#73662)
This adds support for using dominating conditions in computeKnownBits()
when called from InstCombine. The implementation uses a
DomConditionCache, which stores which branches may provide information
that is relevant for a given value.

DomConditionCache is similar to AssumptionCache, but does not try to do
any kind of automatic tracking. Relevant branches have to be explicitly
registered and invalidated values explicitly removed. The necessary
tracking is done inside InstCombine.

The reason why this doesn't just do exactly the same thing as
AssumptionCache is that a lot more transforms touch branches and branch
conditions than assumptions. AssumptionCache is an immutable analysis
and mostly gets away with this because only a handful of places have to
register additional assumptions (mostly as a result of cloning). This is
very much not the case for branches.

This change regresses compile-time by about ~0.2%. It also improves
stage2-O0-g builds by about ~0.2%, which indicates that this change results
in additional optimizations inside clang itself.

Fixes https://github.com/llvm/llvm-project/issues/74242.
2023-12-06 14:17:18 +01:00
Craig Topper
5c3496ff33
[InstCombine] Check isGuaranteedNotToBeUndef in haveNoCommonBitsSetSpecialCases. (#74390)
It's not safe for InstCombine to add disjoint metadata when converting
Add to Or otherwise.

I've added noundef attribute to preserve existing test behavior.
2023-12-05 10:33:44 -08:00
Mikhail Goncharov
0d0c229855 Revert "Reapply "ValueTracking: Identify implied fp classes by general fcmp (#66505)""
This reverts commit d55692d60d218f402ce107520daabed15f2d9ef6.

See discussion in #66505: assertion fires in OSS build of TensorFlow.
2023-12-05 11:10:24 +01:00
Craig Topper
b73d79fda8 [RISCV] Fix typo in comment. NFC
This should say "Assume that VL output is <= 65536".
2023-12-04 14:15:49 -08:00
Nikita Popov
4275da2278 [ValueTracking] Add isGuaranteedNotToBeUndef() variant (NFC)
We have a bunch of places where we have to guard against undef
to avoid multi-use issues, but would be fine with poison. Use a
different function for these to make it clear, and to indicate that
this check can be removed once we no longer support undef. I've
replaced some of the obvious cases, but there's probably more.

For now, the implementation is the same as UndefOrPoison, it just
has a more precise name.
2023-12-04 12:04:41 +01:00
Nikita Popov
da86d4a8c9 [ValueTracking] Reduce duplication in haveNoCommonBitsSet() (NFC)
Extract a function and call it with both operand orders, so that
we don't have to explicitly commute every single pattern.
2023-12-01 14:26:15 +01:00
Nikita Popov
460faa0c87 [InstSimplify] Check common operand with constant earlier
If both icmps have the same operands and the RHS is constant, we
would currently go into the isImpliedCondMatchingOperands() code
path, instead of the isImpliedCondCommonOperandWithConstants()
path. Both are correct, but the latter can produce more accurate
results if the implication is dependent on the sign.
2023-12-01 12:18:59 +01:00
Matt Arsenault
d55692d60d Reapply "ValueTracking: Identify implied fp classes by general fcmp (#66505)"
This reverts commit 96a0d714d58e48c363ee6abbbcdfd7a6ce646ac1.

Avoid assert with dynamic denormal-fp-math We don't recognize compares
with 0 as an exact class test if we don't know the denormal mode. We could
try to do better here, but it's probably not worth it.

Fixes asserts reported after 1adce7d8e47e2438f99f91607760b825e5e3cc37
2023-12-01 17:51:46 +09:00
Allen
ab3fdbdfbe
[ValueTracking] Support srem/urem for isKnownNonNullFromDominatingCondition (#74021)
Similar to div, the rem should also proof its second operand is
non-zero, otherwise it is a UB.

Fix https://github.com/llvm/llvm-project/issues/71782
2023-12-01 16:20:38 +08:00
Nikita Popov
1566380e6b [ValueTracking] Switch analyzeKnownBitsFromAndXorOr() to use SimplifyQuery (NFC)
It already used it internally, make the public API use it as well.
2023-11-30 14:06:05 +01:00
Nikita Popov
d9e8ae7d2f [ValueTracking] Convert MaskedValueIsZero() to use SimplifyQuery (NFC) 2023-11-29 11:18:42 +01:00
Nikita Popov
42b6c8ed3a [ValueTracking] Convert isKnownNegative() to use SimplifyQuery (NFC)
It looks like this function is actually unused.
2023-11-29 11:11:12 +01:00
Nikita Popov
d6e8f3b9a2 [ValueTracking] Convert isKnownPositive() to use SimplifyQuery (NFC) 2023-11-29 11:08:39 +01:00
Nikita Popov
4b3ea337ad [ValueTracking] Convert isKnownNonNegative() to use SimplifyQuery (NFC) 2023-11-29 10:52:52 +01:00
Nikita Popov
1116e4f5dc [ValueTracking] Rename computeKnownBitsFrom{Assume -> Context} (NFC)
In preparation for handling non-assume context-sensitive facts.
2023-11-28 14:57:24 +01:00
Nikita Popov
3e6207a775 [InstCombine] Use SimplifyQuery for computeKnownBits() (NFC)
Call computeKnownBits() with SimplifyQuery to make sure it gets
all available analyses, even if more are added in the future.

As this code is performance-critical, I'm exporting the variant
with by-ref KnownBits and SimplifyQuery, as the variant returning
KnownBits is measurably slower in this context.
2023-11-28 12:59:33 +01:00
Nikita Popov
17041276d4 [ValueTracking] Remove unused computeKnownBits() overload (NFC)
If you need this, use one of the variants returning KnownBits
instead.
2023-11-28 12:51:11 +01:00
Nikita Popov
c644486740 [ValueTracking] Pass unpacked arguments to computeKnownBitsFromCmp() (NFC)
This will alllow using it with an inverted predicate in the future.
2023-11-27 17:35:53 +01:00
Nikita Popov
28a5e6b069
[InstCombine] Remove over-generalization from computeKnownBitsFromCmp() (#72637)
For most practical purposes, the only KnownBits patterns we care about are
those involving a constant comparison RHS and constant mask. However,
the actual implementation is written in a very general way -- and of
course, with basically no test coverage of those generalizations.

This patch reduces the implementation to only handle cases with constant
operands. The test changes are all in "make sure we don't crash" tests.

The motivation for this change is an upcoming patch to handling dominating
conditions in computeKnownBits(). Handling non-constant RHS would add
significant additional compile-time overhead in that case, without any
significant impact on optimization quality.
2023-11-27 16:50:05 +01:00
Nikita Popov
56f569049c [ValueTracking] Handle operand bundle assumes in same loop (NFCI)
We are already iterating over all assumes in AC, so handle
operand bundle based assumes in the same loop, instead of querying
them separately.

To keep the debug counter working, make it work per-bundle rather
than per-value.
2023-11-21 14:34:02 +01:00
Yingwei Zheng
be32e39892
[ValueTracking] Ignore poison values in computeKnownBits (#72683)
This patch handles `poison` elements of non-splat vectors in
`computeKnownBits`. It addresses test changes after I delete the
duplicate logic in https://github.com/llvm/llvm-project/pull/72535.

See also @nikic's comment:
https://github.com/llvm/llvm-project/pull/72535#pullrequestreview-1736991557
2023-11-18 03:36:09 +08:00
Nikita Popov
8775232c49 [ValueTracking] Remove handling of KnownBits assumptions with invert
For all practical purposes, we only care about comparisons with
constant RHS in this code. In that case, an invert will be
canonicalized into the constant and it will be handled by other cases.

Given the complete lack of test coverage, I'm removing this code.
2023-11-17 10:42:18 +01:00
Yingwei Zheng
2060bfcdc7
[ValueTracking] Add support for non-splat vecs in computeConstantRange (#72365)
Related patch: https://github.com/llvm/llvm-project/pull/68331
This missed optimization is discovered with the help of
https://github.com/AliveToolkit/alive2/pull/962.
2023-11-15 21:27:48 +08:00
Matt Arsenault
0e1a52f556
ValueTracking: Handle compare gt to -inf in class identification (#72086)
This apparently shows up somewhere in chromium. We also are missing a
canonicalization to an equality compare with inf.
2023-11-14 10:05:38 +09:00
Hans Wennborg
96a0d714d5 Revert "ValueTracking: Identify implied fp classes by general fcmp (#66505)"
This causes asserts to fire:

  llvm/lib/Analysis/ValueTracking.cpp:4262:
  std::tuple<Value *, FPClassTest, FPClassTest> llvm::fcmpImpliesClass(CmpInst::Predicate, const Function &, Value *, const APFloat *, bool):
  Assertion `(RHSClass == fcPosNormal || RHSClass == fcNegNormal || RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal) && "should have been recognized as an exact class test"' failed.

See comments on the PR.

> Previously we could recognize exact class tests performed by
> an fcmp with special values (0s, infs and smallest normal).
> Expand this to recognize the implied classes by a compare with a general
> constant. e.g. fcmp ogt x, 1 implies positive and non-0.
>
> The API should be better merged with fcmpToClassTest but that
> made the diff way bigger, will try to do that in a future
> patch.

This reverts commit dc3faf0ed0e3f1ea9e435a006167d9649f865da1.
2023-11-10 14:45:52 +01:00
Matt Arsenault
dc3faf0ed0
ValueTracking: Identify implied fp classes by general fcmp (#66505)
Previously we could recognize exact class tests performed by
an fcmp with special values (0s, infs and smallest normal).
Expand this to recognize the implied classes by a compare with a general
constant. e.g. fcmp ogt x, 1 implies positive and non-0.
    
The API should be better merged with fcmpToClassTest but that
made the diff way bigger, will try to do that in a future
patch.
2023-11-10 11:39:19 +09:00
Philip Reames
23099ac239
Add known and demanded bits support for zext nneg (#70858)
zext nneg was recently added to the IR in #67982.   This patch teaches
demanded bits and known bits about the semantics of the instruction, and
adds a couple of test cases to illustrate basic functionality.
2023-11-06 18:47:56 -08:00
Nikita Popov
edea974349 [ValueTracking] Avoid FP cast constant expressions
Use the constant folding API instead.
2023-11-06 14:19:32 +01:00
Nikita Popov
d47e2ff8e1 [ValueTracking] Avoid ConstantExpr::getIntegerCast()
Use ConstantFoldIntegerCast() instead, to remove the reliance on
constant expressions.
2023-11-01 11:19:12 +01:00
Mikhail Gudim
9abf3df111
[ValueTracking] Analyze Select in isKnownNonEqual. (#68427)
Basic way to recursively analyze `select` in `isKnownNonEqual`: `select
%c, %t, %f` is non-equal to `%x` if `%t` is non-equal to `%x` and `%f`
is non-equal to `%x`.
2023-10-25 01:08:40 -04:00
Nikita Popov
eb86de63d9
[IR] Require that ptrmask mask matches pointer index size (#69343)
Currently, we specify that the ptrmask intrinsic allows the mask to have
any size, which will be zero-extended or truncated to the pointer size.

However, what semantics of the specified GEP expansion actually imply is
that the mask is only meaningful up to the pointer type *index* size --
any higher bits of the pointer will always be preserved. In other words,
the mask gets 1-extended from the index size to the pointer size. This
is also the behavior we want for CHERI architectures.

This PR makes two changes:
* It spells out the interaction with the pointer type index size more
explicitly.
* It requires that the mask matches the pointer type index size. The
intention here is to make handling of this intrinsic more robust, to
avoid accidental mix-ups of pointer size and index size in code
generating this intrinsic. If a zero-extend or truncate of the mask is
desired, it should just be done explicitly in IR. This also cuts down on
the amount of testing we have to do, and things transforms needs to
check for.

As far as I can tell, we don't actually support pointers with different
index type size at the SDAG level, so I'm just asserting the sizes match
there for now. Out-of-tree targets using different index sizes may need
to adjust that code.
2023-10-24 09:54:29 +02:00
Dhruv Chawla
076581fd95
[ValueTracking] Implement sdiv/udiv support for isKnownNonNullFromDominatingCondition (#67282)
The second operand of a sdiv/udiv has to be non-null, as division by
zero is UB.

Proofs: https://alive2.llvm.org/ce/z/WttZbb

Fixes https://github.com/llvm/llvm-project/issues/64240.
2023-10-20 09:24:33 +05:30
Dhruv Chawla
be57381a4a
[InstCombine] Create a class to lazily track computed known bits (#66611)
This patch adds a new class "WithCache" which stores a pointer to
any type passable to computeKnownBits along with KnownBits
information which is computed on-demand when getKnownBits()
is called. This allows reusing the known bits information when it is
passed as an argument to multiple functions.

It also changes a few functions to accept WithCache(s) so that
known bits information computed in some callees can be propagated to
others from the top level visitAddSub caller.

This gives a speedup of 0.14%:
https://llvm-compile-time-tracker.com/compare.php?from=499d41cef2e7bbb65804f6a815b9fa8b27efce0f&to=fbea87f1f1e6d5552e2bc309f8e201a3af6d28ec&stat=instructions:u
2023-10-17 21:40:18 +05:30
Nikita Popov
d4300154b6 Revert "[ValueTracking] Remove by-ref computeKnownBits() overloads (NFC)"
This reverts commit b5743d4798b250506965e07ebab806a3c2d767cc.

This causes some minor compile-time impact. Revert for now, better
to do the change more gradually.
2023-10-16 14:04:09 +02:00
Nikita Popov
b5743d4798 [ValueTracking] Remove by-ref computeKnownBits() overloads (NFC)
Remove the old overloads that accept KnownBits by reference, in
favor of those that return it by value.
2023-10-16 13:00:31 +02:00
Noah Goldstein
444383e0d0 [ValueTracking] Do more thorough non-zero check in isKnownToBePowerOfTwo when OrZero is no set.
We can cover more cases by directly checking if the result is
known-nonzero for common patterns when they are missing `OrZero`.

This patch add `isKnownNonZero` checks for `shl`, `lshr`, `and`, and `mul`.

Differential Revision: https://reviews.llvm.org/D157309
2023-10-12 16:05:19 -05:00
Noah Goldstein
dfda65c892 [ValueTracking] Add support for non-splat vecs in cmpExcludesZero
Just a small QOL change.
2023-10-12 16:05:19 -05:00
Noah Goldstein
50ece4cba9 [ValueTracking] Add better support for ConstantRange(And)
The fairly common power of two pattern `X & -X` can be capped at the
highest power of 2 (signbit set).
2023-10-12 14:12:26 -05:00
Noah Goldstein
0f8b40a82e [ValueTracking] Add better support for ConstantRange(Shl)
1) If LHS is constant:
    - The low bits of the LHS is set, the lower bound is non-zero
    - The upper bound can be capped at popcount(LHS) high bits
2) If RHS is constant:
    - The upper bound can be capped at (Width - RHS) high bits
2023-10-12 14:12:26 -05:00
Nikita Popov
80fa5a6377 [ValueTracking] Use SimplifyQuery in haveNoCommonBitsSet() (NFC)
Pass SimplifyQuery instead of unpacked list of arguments.
2023-10-10 11:39:59 +02:00
Nikita Popov
1b3cc4e715 [ValueTracking] Use SimplifyQuery for the overflow APIs (NFC)
Accept a SimplifyQuery instead of an unpacked list of arguments.
2023-10-10 10:57:49 +02:00
Matt Arsenault
7a46baa465
ValueTracking: Use fcAllFlags for unknown value (#66393)
In the failure case we return null, which callers are checking. We were
also returning an fcNone which was unused. It's more consistent to
return fcAllFlags as any possible value, such that the value is always
directly usable without checking the returned value.
2023-10-05 13:10:28 -07:00
Mikhail Gudim
4a2a6a4111
[ValueTracking] Try to infer range of select from true and false values. (#68256)
When computing range of `select` instruction, first compute the union of
ranges of "True" and "False" operands of the `select` instruction.
2023-10-05 13:23:05 -04:00
Nikita Popov
ba149f6e09 [ValueTracking] Add SimplifyQuery ctor without TLI (NFC)
While we pretty much always want to pass DT, AC and CxtI, most
places don't care about TLI. Add an overload where this is not
one of the first parameters.
2023-10-05 09:55:00 +02:00
Nikita Popov
941c75a530 [ValueTracking] Return ConstantRange instead of setting limits (NFC)
Same as previously done for intrinsics.
2023-10-05 09:24:20 +02:00