1220 Commits

Author SHA1 Message Date
Florian Hahn
cd2fc73b49
Revert "[ValueTracking][InstCombine] Add a new API to allow to ignore poison generating flags or metadatas when implying poison"
This reverts commit 754f3ae65518331b7175d7a9b4a124523ebe6eac.

Unfortunately the change can cause regressions due to dropping flags
from instructions (like nuw,nsw,inbounds), prevent further optimizations
depending on those flags.

A simple example is the IR below, where `inbounds` is dropped with the
patch and the phase-ordering test added in 7c91d82ab912fae8b.

    define i1 @test(ptr %base, i64 noundef %len, ptr %p2) {
    bb:
      %gep = getelementptr inbounds i32, ptr %base, i64 %len
      %c.1 = icmp uge ptr %p2, %base
      %c.2 = icmp ult ptr %p2, %gep
      %select = select i1 %c.1, i1 %c.2, i1 false
      ret i1 %select
    }

For more discussion, see D149404.
2023-05-29 15:44:37 +01:00
Nikita Popov
d2502eb091 [KnownBits] Add support for nuw/nsw on shifts
Implement precise nuw/nsw support in the KnownBits implementation,
replacing the rather crude handling in ValueTracking.

Differential Revision: https://reviews.llvm.org/D151208
2023-05-25 10:17:10 +02:00
Nikita Popov
92dc4fff87 [ValueTracking] Check for known bits conflict for shl nsw (PR62908)
I removed the conflict check from computeKnownBitsFromShiftOperator()
in D150648 assuming that this is now handled on the KnownBits side.
However, the nsw handling is still inside ValueTracking, so we
still need to handle conflicts there. Restore the check closer to
where it is relevant.

Fixes https://github.com/llvm/llvm-project/issues/62908.
2023-05-24 10:54:10 +02:00
Matt Arsenault
7738db3afa ValueTracking: Handle constrained_sqrt in computeKnownFPClass
With this, the body of CannotBeNegativeZero can be dropped.
2023-05-24 08:49:04 +01:00
Noah Goldstein
2622b2f409 [ValueTracking] Use select condition to help determine if select is non-zero
In `select c, x, y` the condition `c` dominates the resulting `x` or
`y` chosen by the `select`. This adds logic to `isKnownNonZero` to try
and use the `icmp` for the `c` condition to see if it implies the
select `x` or `y` are known non-zero.

For example in:
    ```
    %c = icmp ugt i8 %x, %C
    %r = select i1 %c, i8 %x, i8 %y
    ```
    The true arm of select `%x` is non-zero (when "returned" by the
    `select`) because `%c` being true implies `%x` is non-zero.

Alive2 Links (with `x {pred} C`):
    - EQ  iff `C != 0`:
        - https://alive2.llvm.org/ce/z/umLabn
    - NE  iff `C == 0`:
        - https://alive2.llvm.org/ce/z/DQvy8Y
    - UGT [always]:
        - https://alive2.llvm.org/ce/z/HBkjgQ
    - UGE iff `C != 0`:
        - https://alive2.llvm.org/ce/z/LDNifB
    - SGT iff `C s>= 0`:
        - https://alive2.llvm.org/ce/z/QzWDj3
    - SGE iff `C s> 0`:
        - https://alive2.llvm.org/ce/z/rR4g3D
    - SLT iff `C s<= 0`:
        - https://alive2.llvm.org/ce/z/uysayx
    - SLE iff `C s< 0`:
        - https://alive2.llvm.org/ce/z/2jYc7e

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D147900
2023-05-23 13:52:40 -05:00
Noah Goldstein
8a60814ed5 [ValueTracking] Use KnownBits functions for computeKnownBits of saturating add/sub functions
The knownbits implementation covers all the cases previously handled
by `uadd.sat`/`usub.sat` as well some additional ones. We previously
were not handling the `ssub.sat`/`sadd.sat` cases at all.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D150103
2023-05-23 13:52:40 -05:00
Nikita Popov
f7d1baa414 [KnownBits] Return zero instead of unknown for always poison shifts
For always poison shifts, any KnownBits return value is valid.
Currently we return unknown, but returning zero is generally more
profitable. We had some code in ValueTracking that tried to do this,
but was actually dead code.

Differential Revision: https://reviews.llvm.org/D150648
2023-05-23 14:41:22 +02:00
Matt Arsenault
81ca787618 Reapply "ValueTracking: Delete body of isKnownNeverInfinity"
This reverts commit d1dc3e13a791fe1b99a341406b5dafec64750cb1.

200bdd9e869e2982f54923b05e54c117fd33f5d9 should have fixed
the reported regression.
2023-05-23 08:48:25 +01:00
Matt Arsenault
c21f1feed3 ValueTracking: Drop rounding mode check for constrained_sqrt in CannotBeNegativeZero
The only value that can produce -0 is exactly -0, no rounding is involved. If the
denormal mode has flushed denormal inputs, a negative value could produce -0.
The constrained intrinsics do not track the denormal mode, and this is just
generally broken in the current set of FP predicates. The move to computeKnownFPClass
will address some of these issues.
2023-05-22 10:49:10 +01:00
luxufan
754f3ae655 [ValueTracking][InstCombine] Add a new API to allow to ignore poison generating flags or metadatas when implying poison
This patch add a new API `impliesPoisonIgnoreFlagsOrMetadatas` which is
the same as `impliesPoison` but ignoring poison generating flags or
metadatas in the process of implying poison and recording these ignored
instructions.

In InstCombineSelect, replacing `impliesPoison` with
`impliesPoisonIgnoreFlagsOrMetadatas` to allow more patterns like
`select i1 %a, i1 %b, i1 false` to be optimized to and/or instructions
by droping the poison generating flags or metadatas.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149404
2023-05-19 14:50:32 +08:00
Alina Sbirlea
d1dc3e13a7 Revert "ValueTracking: Delete body of isKnownNeverInfinity"
This reverts commit 73925ef8b0eacc6792f0e3ea21a3e6d51f5ee8b0.
Introduces test failures, mismatch inf/nan
2023-05-18 23:31:51 -07:00
Matt Arsenault
4130ccc8be ValueTracking: Check context instruction is in a function 2023-05-18 14:40:13 +01:00
Matt Arsenault
5b18d84380 Reapply "ValueTracking: Handle phi in computeKnownFPClass"
This reverts commit e13f88d1ff5234946af6349a9a7cf56fcb6c040e.

Fix off by one recursion limit check.
2023-05-18 12:58:09 +01:00
Matt Arsenault
f42136d4d6 ValueTracking: Check instruction is in a parent in computeKnownFPClass
For some reason the inliner calls simplifyInstruction with disembodied
instructions. I consider this to be an API defect. Either the instruction
should always be inserted prior to simplification, or we at least
should pass in the new function for the context.
2023-05-18 12:21:47 +01:00
Matt Arsenault
e13f88d1ff Revert "ValueTracking: Handle phi in computeKnownFPClass"
This reverts commit cac9e427eb1ff3dabda8ac08968b998c3bc5ab47.

Causing crashes in lencod
2023-05-18 09:43:46 +01:00
Matt Arsenault
cac9e427eb ValueTracking: Handle phi in computeKnownFPClass
Doesn't try the all the tricks computeKnownBits does.
2023-05-18 08:04:38 +01:00
Matt Arsenault
e47b76a82a ValueTracking: Delete body of isKnownNeverNaN
This should now be redundant with the nan handling in computeKnownFPClass.
2023-05-18 08:04:37 +01:00
Matt Arsenault
73925ef8b0 ValueTracking: Delete body of isKnownNeverInfinity
computeKnownFPClass should now handle infinity checks equally as
well as what this could do before so delete the redundant code.
2023-05-18 08:04:37 +01:00
Matt Arsenault
dd61b63b5c ValueTracking: Handle sign bit for fptrunc in computeKnownFPClass 2023-05-17 23:40:06 +01:00
Matt Arsenault
6666969a08 ValueTracking: Implement computeKnownFPClass for various rounding intrinsics 2023-05-17 23:40:05 +01:00
Nikita Popov
9b7616856c [ValueTracking] Fix i1 abs range (PR62760)
For i1 operations, we may end up returning an empty range instead
of a full one. Make sure to use the getNonEmpty constructor.

Fixes https://github.com/llvm/llvm-project/issues/62760.
2023-05-17 12:31:13 +02:00
Noah Goldstein
124547eae8 [ValueTracking] Use KnownBits::sdiv for sdiv opcode in computeKnownBits
We now of an implementation of `KnownBits::sdiv` so we can implement
this case.

Differential Revision: https://reviews.llvm.org/D150096
2023-05-16 18:58:12 -05:00
Noah Goldstein
99795afb28 [ValueTracking] Pass exact flag to KnownBits::udiv in computeKnownBits
This information was previously missing but we can use it for
determining the low-bits.

Differential Revision: https://reviews.llvm.org/D150095
2023-05-16 18:58:12 -05:00
Noah Goldstein
774ecc20e1 [ValueTracking] deduce X * Y != 0 if LowestKnownBit(X) * LowestKnownBit(Y) != 0
For `X * Y`, if there exists a subset of `X` and subset of `Y` s.t `sX * sY != 0`,
then `X * Y != 0`.
    - See first proof: https://alive2.llvm.org/ce/z/28C9CG
    - NB: This is why the previous Odd case works.

In knownbits we could exhaustively hunt for such a subset, but
`LSB(X)` and `LSB(Y)` actually works. If `LSB(X) * LSB(Y) != 0`, then
`X * Y != 0`
    - See proof: https://alive2.llvm.org/ce/z/p5wWid

In `isKnownNonZero` we can use this as if the `LowestKnownOne(X) *
LowestKnownOne(Y) != 0`, then `X * Y != 0`, and we don't need to try
and other subsets.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D150425
2023-05-16 18:58:12 -05:00
Matt Arsenault
86d0b524f3 ValueTracking: Expand signature of isKnownNeverInfinity/NaN
This is in preparation for replacing the implementation
with a wrapper around computeKnownFPClass.
2023-05-16 20:42:58 +01:00
Matt Arsenault
0ee2245ab2 ValueTracking: Restore ordered negative handling for frem
In D148674, the negative condition was weakened to only
checking isKnownNever(fcNegative), instead of cannotBeOrderedLessThanZero().

This avoids a regression when CannotBeOrderedLessThanZero is
replaced with computeKnownFPClass.
2023-05-16 14:38:39 +01:00
Matt Arsenault
c9f88e5bf0 ValueTracking: fadd/fsub +0 cannot return -0
Copied from CannotBeNegativeZero and extended to cover fsub.
2023-05-16 14:33:34 +01:00
Matt Arsenault
fe5786d416 ValueTracking: Implement computeKnownFPClass for sqrt
Could be slightly smarter in cases that are probably uninteresting.
2023-05-16 12:41:43 +01:00
Jay Foad
d8229e2f14 [KnownBits] Define and use intersectWith and unionWith
Define intersectWith and unionWith as two complementary ways of
combining KnownBits. The names are chosen for consistency with
ConstantRange.

Deprecate commonBits as a synonym for intersectWith.

Differential Revision: https://reviews.llvm.org/D150443
2023-05-16 09:23:51 +01:00
Jay Foad
71ac47f391 [KnownBits] Make use of KnownBits.isUnknown. NFC. 2023-05-16 09:19:55 +01:00
Piotr Sobczak
7322d35476 [ValueTracking] Fix computeKnownFPClass with canonicalize
Update code that assumes llvm.canonicalize only handles scalars,
by adding a call to getScalarType().
This is fine, as the intrinsic is trivially vectorizable.

Introduced in D147870, and uncovered by D148065.

Differential Revision: https://reviews.llvm.org/D150556
2023-05-15 14:50:35 +02:00
luxufan
f470922a29 Revert "Revert "[ValutTracking] Use isGuaranteedNotToBePoison in impliesPoison""
This reverts commit 706e8110573c83f140a63b40803d6370c86c1414.
2023-05-10 14:35:55 +08:00
Noah Goldstein
2647547ee4 Re-revert "[ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate"
Seems to be causing a bug in CorrelatedValuePropegation. Reverting
while the issue is investigated.

This reverts commit 6c667abf3294d61e4fbe1238e1755c79f7547f1b.
2023-05-09 19:11:21 -05:00
Hans Wennborg
706e811057 Revert "[ValutTracking] Use isGuaranteedNotToBePoison in impliesPoison"
This broke two lit tests:

  LLVM :: Transforms/LoopSimplify/merge-exits.ll
  LLVM :: Transforms/PhaseOrdering/X86/vector-reductions.ll

see comment on the code review.

> Differential Revision: https://reviews.llvm.org/D149934

This reverts commit 2ba4cfd56769ab50c9c6f432f93265d7793bd1f2.
2023-05-09 09:15:18 +02:00
luxufan
2ba4cfd567 [ValutTracking] Use isGuaranteedNotToBePoison in impliesPoison
Differential Revision: https://reviews.llvm.org/D149934
2023-05-09 12:39:58 +08:00
Kazu Hirata
061b5ba9aa [Analysis] Fix a warning
This patch fixes:

  llvm/lib/Analysis/ValueTracking.cpp:895:12: error: unused variable
  'BitWidth' [-Werror,-Wunused-variable]
2023-05-05 09:09:33 -07:00
Max Kazantsev
7fbf72a101 [ValueTracking][NFC] Factor out computeKnownBitsFromCmp
Separate the part which is specific for assume intrinsic from the part which only requires an icmp,
so that the latter could be reused for other purposes (e.g. in dominating conditions).

Differential Revision: https://reviews.llvm.org/D149940
Reviewed By: nikic
2023-05-05 17:04:11 +07:00
Max Kazantsev
1d424eead9 [NFC][ValueTracking] Hoist isValidAssumeForContext out of switch
There is a lot of copy-paste-ish checks while this can be done once.

Differential Revision: https://reviews.llvm.org/D149939
Reviewed By: nikic
2023-05-05 16:48:40 +07:00
Siyuan Zhu
edcdc81e2b [ValueTracking] add UGT/UGE and SGT/SGE in isImpliedCondOperands
Partially `fix` https://github.com/llvm/llvm-project/issues/62441.

Extend isImpliedCondOperands() to handle ugt/uge and sgt/sge predicates.

alive2 proof: https://alive2.llvm.org/ce/z/jLFDAv and
https://alive2.llvm.org/ce/z/Z8idUd

Differential Revision: https://reviews.llvm.org/D149510
2023-05-04 16:59:07 +02:00
Matt Arsenault
d3c6c3f63c ValueTracking: Sign handling for minnum/maxnum
If we know one operand is positive for maxnum, or one is negative
for minnum, the result will have the same sign.
2023-05-02 19:10:37 -04:00
Matt Arsenault
9feb7cbe51 ValueTracking: Handle minimum/maximum in computeKnownFPClass 2023-05-02 16:49:46 -04:00
Matt Arsenault
58295cac92 ValueTracking: Implement computeKnownFPClass for minnum/maxnum 2023-05-02 14:23:54 -04:00
Noah Goldstein
6c667abf32 Recommit "[ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate" (2nd Try)
Add `poison` checks that where missing.

Reviewed By: nikic
2023-05-01 17:23:58 -05:00
Noah Goldstein
358cdb4489 Revert "[ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate"
Appears to be causing out-of-tree test failures. Reverting while the
issue is investigated.

This reverts commit fbc7fcf5ae26fad7db3e7c861fc6447e02b39cf0.
2023-05-01 13:20:09 -05:00
Noah Goldstein
fbc7fcf5ae [ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate
This just replaces the exact constant requirements with known-bits
which can prove better results.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149423
2023-04-30 10:40:46 -05:00
Noah Goldstein
424b10d6cc [ValueTracking] Slight refactor to avoid unnecessary work; NFC
Two changes:
    1) Make some cases that conditionally returned unconditional.
    2) In cases of `Op0 != 0 || Op1 != 0` its better check `Op1` first
       as its more likely to be a constant due to canonicalization (so
       faster to get knownbits of).

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149419
2023-04-30 10:06:46 -05:00
Noah Goldstein
5eedfff695 [ValueTracking] Add additional cases for isKnownNonZero(mul X, Y)
If either `X` or `Y` is odd and the other is non-zero, the result is
non-zero.

Alive2 Link:
    https://alive2.llvm.org/ce/z/9V7-es

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149418
2023-04-30 10:06:46 -05:00
Noah Goldstein
d840391401 [ValueTracking] Add logic for isKnownNonZero(smin/smax X, Y)
For `smin` if either `X` or `Y` is negative, the result is non-zero.
For `smax` if either `X` or `Y` is strictly positive, the result is
non-zero.

For both if `X != 0` and `Y != 0` the result is non-zero.

Alive2 Link:
    https://alive2.llvm.org/ce/z/7yvbgN
    https://alive2.llvm.org/ce/z/zizbvq

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149417
2023-04-30 10:06:46 -05:00
Noah Goldstein
e78c30a10f [ValueTracking] Add logic for isKnownNonZero(umin X, Y)
`(umin X, Y) != 0` -> `X != 0 && Y != 0`

Alive2 Link:
    https://alive2.llvm.org/ce/z/AQh67i

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149416
2023-04-30 10:06:46 -05:00
Noah Goldstein
883daa7ac4 [ValueTracking] Add logic for isKnownNonZero(umax X, Y)
`(umax X, Y) != 0` -> `X != 0 || Y != 0`

Alive2 Link:
    https://alive2.llvm.org/ce/z/_Z9AUT

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149415
2023-04-30 10:06:46 -05:00