1266 Commits

Author SHA1 Message Date
Noah Goldstein
dff3d8a279 [ValueTracking] Add support for mul in isKnownToBeAPowerOfTwo
pow2 * pow2 is a power of 2 or zero.

Proof: https://alive2.llvm.org/ce/z/FNiiXd

Differential Revision: https://reviews.llvm.org/D157308
2023-08-09 14:42:55 -05:00
Noah Goldstein
4f818daca6 [ValueTracking] Add support for fshl/fshr in isKnownToBeAPowerOfTwo
If the funnel shifts are rotates (op0 == op1) then the number of 1s/0s
don't change so we can just look through op0/op1.

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

Differential Revision: https://reviews.llvm.org/D157307
2023-08-09 14:42:55 -05:00
Noah Goldstein
6f4d660d7f [ValueTracking] Add support for bswap and bitreverse in isKnownToBeAPowerOfTwo
Both of these intrinsics don't change the number of 1s/0s so we can
look directly through them.

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

Differential Revision: https://reviews.llvm.org/D157306
2023-08-09 14:42:55 -05:00
Noah Goldstein
03b529a2a6 [ValueTracking] Use switch for Intrinsics in isKnownToBePowerOfTwo; NFC
Differential Revision: https://reviews.llvm.org/D157305
2023-08-09 14:42:54 -05:00
Noah Goldstein
bab8058d3b [ValueTracking] If OrZero is set, look through trunc in isKnownToBePowerOfTwo
Just move coverage.

Proof: https://alive2.llvm.org/ce/z/H37tVX

Differential Revision: https://reviews.llvm.org/D157304
2023-08-09 14:42:54 -05:00
Alexander Kornienko
0b779b0daa Revert "[AggressiveInstCombine] Fold strcmp for short string literals"
This reverts commit 5dde755188e34c0ba5304365612904476c8adfda,
cbfcf90152de5392a36d0a0241eef25f5e159eef and
8981520b19f2d2fe3d2bc80cf26318ee6b5b7473 due to a miscompile introduced in
8981520b19f2d2fe3d2bc80cf26318ee6b5b7473 (see
https://reviews.llvm.org/D154725#4568845 for details)

Differential Revision: https://reviews.llvm.org/D157430
2023-08-08 22:53:45 +02:00
Matt Arsenault
729b55ef5e ValueTracking: Reduce indentation in computeKnownFPClass 2023-08-07 17:32:03 -04:00
Noah Goldstein
f6c726472d Revert "[ValueTracking] Improve the coverage of isKnownToBeAPowerOfTwo for vscale"
Logic is incorrect. Shift can make non-zero pow2 zero.

This reverts commit 9c837b7d0e2e2dffae804f3df49c4aeefe4743c0.
2023-08-07 10:26:05 -05:00
Nikita Popov
5de89b4d99 [ValueTracking] Support non-zero pow2 for shl with nowrap flags
If the shl has either nuw or nsw flags, then we know that bits
cannot be shifted out, so a power of two cannot become zero.

Proofs: https://alive2.llvm.org/ce/z/4QfebE
2023-08-07 17:04:55 +02:00
Nikita Popov
8aeb84c1b6 [ValueTracking] Switch over opcode in isKnownToBeAPowerOfTwo() (NFC)
Similar to the other ValueTracking function, switch over the
instruction opcode instead of doing a long sequence of match()es.
2023-08-07 16:34:15 +02:00
Matt Arsenault
1d9f77f54f ValueTracking: Really remove CannotBeOrderedLessThanZero
6640df94f9abd4f9fef0263afbf7978ac55832b8 did not actually remove it,
just its final user. cannotBeOrderedLessThanZeroImpl still has a user
which needs to be updated before it can be removed.

The users of SignBitMustBeZero currently have broken expectations for
nan handling, so requires more work to replace.
2023-08-07 08:14:41 -04:00
luxufan
4e84ec8184 [ValueTracking] Dereferenceable ret attributes implys noundef
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D156510
2023-08-07 15:11:03 +08:00
Zhongyunde
9c837b7d0e [ValueTracking] Improve the coverage of isKnownToBeAPowerOfTwo for vscale
this PR tries to match the following pattern, seperate from D156881
     ```
     %vscale = call i64 @llvm.vscale.i64()
     %shift = shl nuw nsw i64 %vscale, 11
     ```
Now, we only check the shl recursively when the OrZero is true.

Reviewed By: goldstein.w.n
Differential Revision: https://reviews.llvm.org/D157062
2023-08-04 09:06:34 +08:00
Dhruv Chawla
d899dc5296
[NFC][ValueTracking]: Move some code from isKnownNonZero to isKnownNonZeroFromOperator
There is some pointer simplification code originally from isKnownNonNull
that is now better suited to be in isKnownNonZeroFromOperator.

Differential Revision: https://reviews.llvm.org/D156141
2023-07-26 13:50:38 +05:30
Dhruv Chawla
de7a7aa110
[NFC][ValueTracking]: Remove redundant computeKnownBits call for LoadInst in isKnownNonZero
For load instructions, computeKnownBits only checks the range metadata.
This check is already present in isKnownNonZero, so there is no need to
fall through to computeKnownBits.

This change gives a speed improvement of 0.12-0.18%:
https://llvm-compile-time-tracker.com/compare.php?from=3c6ed559e5274307995586c1499a2c8e4e0276a0&to=78b462d8c4ae079638b728c6446da5999c4ee9f8&stat=instructions:u

Differential Revision: https://reviews.llvm.org/D155958
2023-07-24 15:29:46 +05:30
Matt Arsenault
d873a14e93 ValueTracking: Implement computeKnownFPClass for frexp
Work around the lack of proper multiple return values by looking
at the extractvalue.

https://reviews.llvm.org/D150982
2023-07-21 16:04:13 -04:00
Nikita Popov
4f7e03463f [ValueTracking] Check non-zero operator before dominating condition (NFC)
Prefer checking for non-zero operator before non-zero via
dominating conditions. This is to make sure we don't have
compile-time regressions when special cases that are currently
part of isKnownNonZero() get moved into isKnownNonZeroFromOperator().
2023-07-21 15:56:41 +02:00
Nikita Popov
98b10727b1 [ValueTracking] Extract isKnownNonZeroFromOperator() (NFC)
Split off the primary part of the isKnownNonZero() implementation,
in the same way it is done for computeKnownBits(). This makes it
easier to reorder different parts of isKnownNonZero().
2023-07-21 15:51:29 +02:00
Maksim Kita
8981520b19 [AggressiveInstCombine] Fold strcmp for short string literals
Fold strcmp() against 1-char string literals.

This designates AggressiveInstCombine as the pass for libcalls
simplifications that may need to change the control flow graph.

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

Differential Revision: https://reviews.llvm.org/D154725
2023-07-19 17:12:27 +02:00
Matt Arsenault
f868c22bee ValueTracking: Fix computeKnownFPClass canonicalize handling
This mostly manifested as broken constant folding. This was
mishandling the dynamic denormal mode. It was also mishandling literal
signaling nans, such that they would also be treated as poison.

https://reviews.llvm.org/D155437
2023-07-18 06:37:15 -04:00
Matt Arsenault
82c65cc8f8 ValueTracking: Fix computeKnownFPClass for vector-with-scalar powi
Fixes regression reported after
0f4eb557e87912afa412b1b0299d10eb7443410b
2023-07-17 17:27:48 -04:00
zhongyunde
4d2723bd00 [ValueTracking] Support vscale assumes for isKnownToBeAPowerOfTwo
This patch is separated from D154953 to see what tests are affected by this
change alone according comment.
Depend on the related updating of LangRef on D155193.

Reviewed By: paulwalker-arm, nikic, david-arm
Differential Revision: https://reviews.llvm.org/D155350
2023-07-15 19:42:58 +08:00
Matt Arsenault
6ed48ebf2e ValueTracking: Recognize fpclass clamping select patterns
Improve computeKnownFPClass select handling to cover the case where
the condition performs a class test. This allows us to recognize
no-nans in cases like:

  %not.nan = fcmp ord float %x, 0.0
  %select = select i1 %not.nan, float %x, float 0.0

Math library code has similar edge case filtering on the inputs and
final results.

https://reviews.llvm.org/D153089
2023-07-12 13:14:05 -04:00
Matt Arsenault
0f4eb557e8 ValueTracking: Replace CannotBeNegativeZero
This is now just a wrapper around computeKnownFPClass.
2023-07-12 13:14:05 -04:00
Matt Arsenault
c457619c5b ValueTracking: ldexp cannot return denormals based on range of exponent
The implementations of a number of math functions on amdgpu involve
pre and post-scaling the inputs out of the denormal range. If these
are chained together we can possibly fold them out.

computeConstantRange seems weaker than computeKnownBits, so this
regresses some of the older vector tests.
2023-07-11 14:53:03 -04:00
Matt Arsenault
e1ac984a10 ValueTracking: Implement computeKnownFPClass for ldexp
https://reviews.llvm.org/D149590
2023-07-11 09:26:41 -04:00
Nikita Popov
2e0af16c93 [ValueTracking] Support add+icmp assumes for KnownBits
Support the canonical range check pattern for KnownBits assumptions.
This is the same as the generic ConstantRange handling, just shifted
by an offset.
2023-07-05 16:15:47 +02:00
Nikita Popov
c6a4fc8ddf [ValueTracking] Determine assume KnownBits using ConstantRange
For non-equality icmps, we don't do any KnownBits-specific
reasoning, and just use the known bits as a constraint on the range.
We can generalize this for all predicates by round-tripping through
ConstantRange and using makeAllowedICmpRegion().

The minor improvement in zext-or-icmp is because we assume that
a value is ult [0,1], which means it must be zero.
2023-07-05 14:00:03 +02:00
Nikita Popov
8d3856836c [ValueTracking] Don't handle ptrtoint with mismatches sizes
When processing assumes, we also handle assumes on ptrtoint of the
value. In canonical IR, these will have the same size as the value.
However, in non-canonical IR there may be an implicit zext or
trunc, which results in a bit width mismatch. We currently handle
this by adjusting bitwidth everywhere, but this is fragile and I'm
pretty sure that the way we do this is incorrect for some predicates,
because we effectively end up commuting an ext/trunc and an icmp.

Instead, add an m_PtrToIntSameSize() matcher that will only handle
bitwidth preserving cases. For the bitwidth-changing cases, wait
until they have been canonicalized.

The original handling for this was added purely to prevent crashes
in an earlier implementation which failed to account for this
entirely.
2023-07-05 12:18:00 +02:00
luxufan
621fafdaad [ValueTracking] Guaranteed well-defined if parameter has a dereferecable_or_null attribute
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D153945
2023-06-29 11:45:04 +08:00
Matt Arsenault
19a35041c0 ValueTracking: Handle !absolute_symbol in computeKnownBits
Use a unit test since I don't see any existing uses try to make use of
the high bits of a pointer.

This will also assert if the metadata type doesn't match the pointer
width, but I consider that a defect in the verifier and shouldn't be
handled.

AMDGPU allocates LDS globals by assigning !absolute_symbol with the
final fixed address. Tracking the high bits are 0 may help with
addressing mode matching.
2023-06-28 07:33:27 -04:00
Matt Arsenault
f415563b48 ValueTracking: Handle ptrmask in computeKnownBits 2023-06-28 07:25:48 -04:00
Simon Pilgrim
ab066bccf7 Fix "the the" duplicate typo in comment. NFC. 2023-06-27 11:43:08 +01:00
Matt Arsenault
5af973d5e9 ValueTracking: Handle cannotBeOrderedLessThanZero for fadd
Move cannotBeOrderedLessThanZero logic into computeKnownFPClass.
2023-06-23 18:34:28 -04:00
Nikita Popov
a68c968755 [ValueTracking] Correctly check addrspace of alloca
The DataLayout alloca address space is the address space that should
be used when creating new allocas. However, not all allocas are
required to be in this address space. The isKnownNonZero() check
should work on the actual address space of the alloca, not the
default alloca address space.
2023-06-23 13:11:28 +02:00
Matt Arsenault
06f942016b ValueTracking: Ignore -0 for nsz sqrt with UseInstrInfo in computeKnownFPClass
This avoids a regression when SignBitMustBeZero is moved to computeKnownFPClass.
2023-06-20 21:30:09 -04:00
Matt Arsenault
cb9b01f910 ValueTracking: Handle compare to nan and -inf constants in fcmpToClassTest
This will help enable a cleanup of simplifyFCmpInst
2023-06-19 11:31:58 -04:00
Arthur Eubanks
f4f826bcd4 Revert "Revert "ValueTracking: Fix nan result handling for fmul""
This reverts commit 464dcab8a6c823c9cb462bf4107797b8173de088.

Going to fix forward size regression instead due to more dependent patches needing to be reverted otherwise.
2023-06-16 13:53:32 -07:00
Arthur Eubanks
464dcab8a6 Revert "ValueTracking: Fix nan result handling for fmul"
This reverts commit a632ca4b00279baf18e72a171ec0ce526e9d80aa.

Dependent commit to be reverted
2023-06-16 11:24:28 -07:00
Matt Arsenault
a632ca4b00 ValueTracking: Fix nan result handling for fmul
This was mishandling maybe 0 * inf.

Fixes issue #63316
2023-06-15 09:35:12 -04:00
Joshua Cao
cb9f1aadda [ValueTracking] Implied conditions for lshr
`V1 >> V2 u<= V1` for any V1, V2

This works for lshr and any div's that are changed to lshr's

This fixes issues in clang and rustc:
https://github.com/llvm/llvm-project/issues/62441
https://github.com/rust-lang/rust/issues/110971

Reviewed By: goldstein.w.n

Differential Revision: https://reviews.llvm.org/D151541
2023-06-06 21:06:22 -07:00
Krzysztof Drewniak
23098bd454 [AMDGPU] Add intrinsic for converting global pointers to resources
Define the function @llvm.amdgcn.make.buffer.rsrc, which take a 64-bit
pointer, the 16-bit stride/swizzling constant that replace the high 16
bits of an address in a buffer resource, the 32-bit extent/number of
elements, and the 32-bit flags (the latter two being the 3rd and 4th
wards of the resource), and combines them into a ptr addrspace(8).

This intrinsic is lowered during the early phases of the backend.

This intrinsic is needed so that alias analysis can correctly infer
that a certain buffer resource points to the same memory as some
global pointer. Previous methods of constructing buffer resources,
which relied on ptrtoint, would not allow for such an inference.

Depends on D148184

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D148957
2023-06-05 17:07:59 +00:00
Nikita Popov
fa45fb7f0c [InstCombine] Handle assumes in multi-use demanded bits simplification
This fixes the largest remaining discrepancy between results of
computeKnownBits() and SimplifyDemandedBits(). We only care about
the multi-use case here, because the assume necessarily introduces
an extra use.
2023-06-02 14:24:24 +02:00
Nikita Popov
371835e82c [ValueTracking] Replace Query with SimplifyQuery (NFC)
These implement essentially the same thing, so normalize
ValueTracking to use SimplifyQuery. In the future we can directly
expose the SimplifyQuery-based APIs.
2023-06-02 11:17:46 +02:00
Nikita Popov
97b5cc214a [ValueTracking] Remove ORE argument (NFC-ish)
The ORE argument threaded through ValueTracking is used only in a
single, untested place. It is also essentially never passed: The
only places that do so have been added very recently as part of the
KnownFPClass migration, which is vanishingly unlikely to hit this
code path. Remove this effectively dead argument.

Differential Revision: https://reviews.llvm.org/D151562
2023-06-02 09:11:53 +02:00
Nikita Popov
dfb369399d [ValueTracking] Directly use KnownBits shift functions
Make ValueTracking directly call the KnownBits shift helpers, which
provides more precise results.

Unfortunately, ValueTracking has a special case where sometimes we
determine non-zero shift amounts using isKnownNonZero(). I have my
doubts about the usefulness of that special-case (it is only tested
in a single unit test), but I've reproduced the special-case via an
extra parameter to the KnownBits methods.

Differential Revision: https://reviews.llvm.org/D151816
2023-06-01 09:46:16 +02:00
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