1242 Commits

Author SHA1 Message Date
Matt Arsenault
02f647f892 ValueTracking: Handle sign bit of constrained sitofp/uitofp
This is for parity with CannotBeNegativeZero which is close to
droppable.
2023-04-19 20:11:33 -04:00
Matt Arsenault
f6d79ad9eb ValueTracking: Implement computeKnownFPClass for fdiv for nan handling 2023-04-19 20:11:33 -04:00
Matt Arsenault
e7bcfea622 ValueTracking: Fix backwards handling of fpclass assumes
This was a bit confused because nofpclass expresses the opposite
from what an assume of class expresses. We need to assume
the intersection of assumed classes, which also needs to be inverted
to convert to nofpclass.
2023-04-19 20:11:32 -04:00
Matt Arsenault
dea4f37b7d ValueTracking: Handle shufflevector in computeKnownFPClass 2023-04-19 08:18:37 -04:00
Matt Arsenault
8e70ed6efd ValueTracking: Handle insertelement in computeKnownFPClass 2023-04-19 08:18:37 -04:00
Matt Arsenault
0d448783c3 ValueTracking: sitofp cannot return -0 2023-04-19 08:18:37 -04:00
OCHyams
ca10e73b53 [NFC] Rename isPointerOffset to getPointerOffsetFrom and move to Value.h
Linking LLVMCore failed when building D148536 with shared libs enabled:
https://lab.llvm.org/buildbot/#/builders/121/builds/29766

Make isPointerOffset a Value method and rename it to getPointerOffsetFrom.

Reviewed By: jmorse

Differential Revision: https://reviews.llvm.org/D148698
2023-04-19 12:22:58 +01:00
Noah Goldstein
e846ec57cb Recommit "[ValueTracking] Apply the isKnownNonZero techniques in ashr/lshl to shl and vice-versa" (2nd Try)
Wasn't related to the bug it was original thought to be causing.
2023-04-18 17:17:57 -05:00
Nikita Popov
294831688f [ValueTracking] Use SmallPtrSetImpl (NFC)
Don't hardcode set size in function signature.
2023-04-18 12:37:15 +02:00
Noah Goldstein
3c4d9cc273 Revert "[ValueTracking] Apply the isKnownNonZero techniques in ashr/lshl to shl and vice-versa"
May be related to PR62175

This reverts commit 57590d1dd47bbe9aa4b79a0f93cc3ec62cc5d060.
2023-04-18 01:23:08 -05:00
Noah Goldstein
57590d1dd4 [ValueTracking] Apply the isKnownNonZero techniques in ashr/lshl to shl and vice-versa
For all shifts we can apply the same two optimizations.

    1) `ShiftOp(KnownVal.One, Max(KnownCnt)) != 0`
        -> result is non-zero
    2) If already known `Val != 0` and we only shift out zeros (based
       on `Max(KnownCnt)`)
        -> result is non-zero

The former exists for `shl` and the latter (for constant `Cnt`) exists
for `ashr`/`lshr`.

This patch improves the latter to use `Max(KnownCnt)` instead of
relying on a constant shift `Cnt` and applies both techniques for all
shift ops.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D148404
2023-04-17 22:39:06 -05:00
Nikita Popov
fd63a7d5c8 Revert "ValueTracking: Handle freeze in computeKnownFPClass"
This reverts commit 2c8d0048f03d054f13909a26f959ef95b2a0a4de.

This is incorrect: computeKnownFPClass() is only known up to
poison, and freeze poison may have any FP class.
2023-04-17 12:59:23 +02:00
Noah Goldstein
f688d215e5 [ValueTracking] Add shl nsw %val, %cnt != 0 if %val != 0.
Alive2 Link: https://alive2.llvm.org/ce/z/mxZLJn

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D147898
2023-04-14 18:23:47 -05:00
Noah Goldstein
684963b86d [ValueTracking] Use maximum shift count in shl when determining if shl can be zero.
Previously only return `shl` non-zero if the shift value was `1`. We
can expand this if we have some bounds on the shift count.

For example:
    ```
    %cnt = and %c, 16 ; Max cnt == 16
    %val = or %v, 4 ; val[2] is known one
    %shl = shl %val, %cnt ; (val.known.one << cnt.maxval) != 0
    ```

Differential Revision: https://reviews.llvm.org/D147897
2023-04-14 18:23:45 -05:00
Matt Arsenault
2c8d0048f0 ValueTracking: Handle freeze in computeKnownFPClass 2023-04-14 17:53:41 -04:00
Matt Arsenault
49b931bdc5 ValueTracking: Implement computeKnownFPClass for arithmetic.fence 2023-04-14 17:41:27 -04:00
Matt Arsenault
3dabcdc78b ValueTracking: Implement computeKnownFPClass for llvm.trunc 2023-04-14 17:41:26 -04:00
Matt Arsenault
656b52a6c6 ValueTracking: Handle non-splat vectors in computeKnownFPClass
Avoids some regressions when the implementation of isKnownNeverNaN is
replaced with computeKnownFPClass.
2023-04-14 17:41:26 -04:00
Matt Arsenault
e2d68c2fa4 ValueTracking: Implement computeKnownFPClass for canonicalize 2023-04-14 16:17:55 -04:00
Matt Arsenault
cb022084f0 ValueTracking: Handle fptrunc in computeKnownFPClass
Handle nan.
2023-04-14 14:36:56 -04:00
Matt Arsenault
409ef45000 ValueTracking: Handle extractelement and extractvalue in computeKnownFPClass 2023-04-14 14:36:56 -04:00
Matt Arsenault
c603fd2f39 ValueTracking: Implement computeKnownFPClass for sin/cos 2023-04-14 14:36:55 -04:00
Matt Arsenault
054cac104f ValueTracking: Address todo for nan fmul handling in computeKnownFPClass
If both operands can't be zero or nan, the result can't be nan.
2023-04-13 14:44:34 -04:00
Matt Arsenault
4d044bfb33 ValueTracking: Handle no-nan check for computeKnownFPClass for fmul
Copy the logic from isKnownNeverNaN for fadd/fsub. Leave the
extension to handle the zero case for a future change.
2023-04-13 14:44:34 -04:00
Matt Arsenault
6aca400986 ValueTracking: Handle no-nan check for computeKnownFPClass for fadd/fsub
Copy the logic from isKnownNeverNaN for fadd/fsub.
2023-04-12 06:48:58 -04:00
Matt Arsenault
eb8e43a2a1 ValueTracking: Remove outdated todo 2023-04-12 06:48:58 -04:00
Nikita Popov
3f53a58597 [ValueTracking] Fix incorrect computeConstantRange() arguments
The second argument is ForSigned, not UseInstrInfo.
2023-03-31 16:56:56 +02:00
Philip Reames
54539fa8b3 [LSR/LFTR] Move two utilities to common code for reuse [nfc]
We're working on a transform in LSR which is essentiall an inverse of LFTR (in certain sub-cases).  Move utilties so that they can be reused.
2023-03-20 09:05:38 -07:00
Matt Arsenault
d2404ea6ce Attributor: Assume handling for nofpclass 2023-03-17 07:39:40 -04:00
Nikita Popov
a5242483e4 [SCEV] Recognize vscale intrinsics
Now that SCEV has a dedicated vscale node type, we should also map
vscale intrinsics to it. To make sure this does not regress ranges
(which were KnownBits based previously), add support for vscale to
getRangeRef() as well.

Differential Revision: https://reviews.llvm.org/D146226
2023-03-17 10:07:39 +01:00
Nikita Popov
402dfa389e [ValueTracking] Support vscale in computeConstantRange()
Add support for vscale in computeConstantRange(), based on
vscale_range attributes. This allows simplifying based on the
precise range, rather than a KnownBits approximation (which will
be off by a factor of two for the usual case of a power of two
upper bound).

Differential Revision: https://reviews.llvm.org/D146217
2023-03-17 10:03:24 +01:00
Matt Arsenault
8a37512924 ValueTracking: Extract fcmpToClassTest out of InstCombine
Also update unsigned to FPClassTest
2023-03-16 23:14:40 -04:00
Matt Arsenault
b39deda3e1 ValueTracking: Handle nofpclass in computeKnownFPClass 2023-03-16 23:14:40 -04:00
Matt Arsenault
931d4098a2 ValueTracking: Add start of computeKnownFPClass API
Add a new compute-known-bits like function to compute all
the interesting floating point properties at once.

Eventually this should absorb all the various floating point
queries we already have.
2023-03-16 23:14:40 -04:00
Nikita Popov
531e06668b [ValueTracking] Return ConstantRange for intrinsic ranges (NFC)
Instead of setting Lower and Upper, return a ConstantRange.
Should do this for the others as well.
2023-03-16 14:25:28 +01:00
Matt Arsenault
5da674492a IR: Add nofpclass parameter attribute
This carries a bitmask indicating forbidden floating-point value kinds
in the argument or return value. This will enable interprocedural
-ffinite-math-only optimizations. This is primarily to cover the
no-nans and no-infinities cases, but also covers the other floating
point classes for free. Textually, this provides a number of names
corresponding to bits in FPClassTest, e.g.

  call nofpclass(nan inf) @must_be_finite()
  call nofpclass(snan) @cannot_be_snan()

This is more expressive than the existing nnan and ninf fast math
flags. As an added bonus, you can represent fun things like nanf:

  declare nofpclass(inf zero sub norm) float @only_nans()

Compared to nnan/ninf:
  - Can be applied to individual call operands as well as the return value
  - Can distinguish signaling and quiet nans
  - Distinguishes the sign of infinities
  - Can be safely propagated since it doesn't imply anything about
    other operands.
  - Does not apply to FP instructions; it's not a flag

This is one step closer to being able to retire "no-nans-fp-math" and
"no-infs-fp-math". The one remaining situation where we have no way to
represent no-nans/infs is for loads (if we wanted to solve this we
could introduce !nofpclass metadata, following along with
noundef/!noundef).

This is to help simplify the GPU builtin math library
distribution. Currently the library code has explicit finite math only
checks, read from global constants the compiler driver needs to set
based on the compiler flags during linking. We end up having to
internalize the library into each translation unit in case different
linked modules have different math flags. By propagating known-not-nan
and known-not-infinity information, we can automatically prune the
edge case handling in most functions if the function is only reached
from fast math uses.
2023-02-24 07:41:29 -04:00
Noah Goldstein
196d3e3965 Add logic for tracking lowbit of (and/xor/or X, (add/sub X, Odd))
Any case of logicop + add/sub(Odd) we can prove the low bit is either
zero/non-zero.

Alive2 Links:
    xor:
        sub x, C: https://alive2.llvm.org/ce/z/aaABdS
        sub C, x: https://alive2.llvm.org/ce/z/2W-ZJ7
        add C, x: https://alive2.llvm.org/ce/z/pzDkte
    or:
        sub x, C: https://alive2.llvm.org/ce/z/xd-bcP
        sub C, x: https://alive2.llvm.org/ce/z/p8hXJF
        add C, x: https://alive2.llvm.org/ce/z/osmkB6
    and:
        sub x, C: https://alive2.llvm.org/ce/z/D_NNxR
        sub C, x: https://alive2.llvm.org/ce/z/N_5C62
        add C, x: https://alive2.llvm.org/ce/z/4cy7a4

Differential Revision: https://reviews.llvm.org/D142427
2023-02-23 19:52:17 -06:00
Noah Goldstein
6ad6f9c579 Add helper for handling computeKnownBits for and/xor/or; NFC
This change just factors out the existing logic for and/xor/or and
puts them in a publicly available helper.  functionality is the same.

Differential Revision: https://reviews.llvm.org/D142849
2023-02-23 19:52:16 -06:00
Max Kazantsev
0cbb8ec030 Revert "[AssumptionCache] caches @llvm.experimental.guard's"
This reverts commit f9599bbc7a3f831e1793a549d8a7a19265f3e504.

For some reason it caused us a huge compile time regression in downstream
workloads. Not sure whether the source of it is in upstream code ir not.
Temporarily reverting until investigated.

Differential Revision: https://reviews.llvm.org/D142330
2023-02-20 18:38:07 +07:00
Kazu Hirata
f8f3db2756 Use APInt::count{l,r}_{zero,one} (NFC) 2023-02-19 22:04:47 -08:00
Noah Goldstein
3bd38f6639 [ValueTracking] Add cases for additional ops in isKnownNonZero
Add cases for the following ops:
    - 0-X            -- https://alive2.llvm.org/ce/z/6C75Li
    - bitreverse(X)  -- https://alive2.llvm.org/ce/z/SGG1q9
    - bswap(X)       -- https://alive2.llvm.org/ce/z/p7pzwh
    - ctpop(X)       -- https://alive2.llvm.org/ce/z/c5y3BC
    - abs(X)         -- https://alive2.llvm.org/ce/z/yxXGz_
                        https://alive2.llvm.org/ce/z/rSRg4K
    - uadd_sat(X, Y) -- https://alive2.llvm.org/ce/z/Zw-y4W
                        https://alive2.llvm.org/ce/z/2NRqRz
                        https://alive2.llvm.org/ce/z/M1OpF8

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142828
2023-02-18 13:45:15 -06:00
Noah Goldstein
9a8f517f57 [ValueTracking] Add KnownBits patterns xor(x, x - 1) and and(x, -x) for knowing upper bits to be zero
These two BMI pattern will clear the upper bits of result past the
first set bit. So if we know a single bit in `x` is set, we know that
`results[bitwidth - 1, log2(x) + 1] = 0`.

Alive2:
blsmsk: https://alive2.llvm.org/ce/z/a397BS
blsi: https://alive2.llvm.org/ce/z/tsbQhC

Differential Revision: https://reviews.llvm.org/D142271
2023-02-18 13:31:17 -06:00
Craig Topper
68c906811b [ValueTracking] Replace an always false condition with an assert. NFC
The one caller of this function already checked that V isn't a
Constant.

Alternatively, we could remove the check from the caller if reviewers
prefer.

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D143677
2023-02-09 15:19:28 -08:00
Craig Topper
2919ec041f [RISCV] Remove side effects from vsetvli intrinsics.
Delete the opt intrinsics since they are now identical.

I left the side effects due to user expectations about how these
interact with things like inline assembly or function calls. Or
that they wouldn't be hoisted. I think we should look at other
ways to address thoughs.

If I could, I'd rename them these somehow to distance them from
the vsetvli instruction. In some sense they only query the VL for
a particular SEW and LMUL. They don't guarantee a vsetvli
instruction will be emitted.

Fixes https://github.com/llvm/llvm-project/issues/59359

Reviewed By: rogfer01, kito-cheng

Differential Revision: https://reviews.llvm.org/D143220
2023-02-03 13:03:56 -08:00
Kazu Hirata
55e2cd1609 Use llvm::count{lr}_{zero,one} (NFC) 2023-01-28 12:41:20 -08:00
Craig Topper
4ea6d42b63 [RISCV] Teach computeKnownBits that vsetvli returns <= 65536.
Resolves a FIXME. We could do even better taking into account SEW/LMUL.
2023-01-27 16:39:54 -08:00
Craig Topper
1ce3afd977 [ValueTracking] Teach computeKnownBits about riscv.vsetvli.opt and riscv.vsetvlimax.opt intrinsics.
These are like the intrinsic without opt, but don't have side effects.

Add missing test cases for riscv.vsetvlimax.
2023-01-27 16:26:38 -08:00
Stefan Gränitz
3b387d1070 Lift EHPersonalities from Analysis to IR (NFC)
Computing EH-related information was only relevant for analysis passes so far. Lifting it to IR will allow the IR Verifier to calculate EH funclet coloring and validate funclet operand bundles in a follow-up step.

Reviewed By: rnk, compnerd

Differential Revision: https://reviews.llvm.org/D138122
2023-01-27 18:05:13 +01:00
Joshua Cao
f9599bbc7a [AssumptionCache] caches @llvm.experimental.guard's
As discussed in https://github.com/llvm/llvm-project/issues/59901

This change is not NFC. There is one SCEV and EarlyCSE test that have an
improved analysis/optimization case. Rest of the tests are not failing.

I've mostly only added cleanup to SCEV since that is where this issue
started. As a follow up, I believe there is more cleanup opportunity in
SCEV and other affected passes.

There could be cases where there are missed registerAssumption of
guards, but this case is not so bad because there will be no
miscompilation. AssumptionCacheTracker should take care of deleted
guards.

Differential Revision: https://reviews.llvm.org/D142330
2023-01-24 20:16:46 -08:00
Kazu Hirata
188ec33726 [llvm] Use llvm::bit_width (NFC) 2023-01-21 14:48:32 -08:00