1244 Commits

Author SHA1 Message Date
Paul Walker
62e46f2621 [LLVM] Remove support for constant scalable vector GEPs.
This work has fallen out from D134648 as a requirement to loosen
the "constness" of vscale.

Differential Revision: https://reviews.llvm.org/D145404
2023-03-14 16:48:33 +00:00
Nikita Popov
be687aff05 [InstSimplify] Adjust context instruction when threading phi (PR61312)
When threading operations over phis, we need to adjust the context
instruction to the terminator of the incoming block. This was
handled when threading icmps, but not when threading binops.

Fixes https://github.com/llvm/llvm-project/issues/61312.
2023-03-10 10:34:27 +01:00
Nikita Popov
8bcdc503eb [InstSimplify] Add test for PR61312 (NFC) 2023-03-10 10:29:43 +01:00
Nikita Popov
475f30dfc8 [ConstantFolding] Preserve inbounds when casting GEP indices
This canonicalization just makes the implicit sext/trunc explicit,
and does not affect the inbounds-ness of the GEP.
2023-03-09 13:08:19 +01:00
Nikita Popov
809af78d95 [InstSimplify] Fix newlines in test (NFC)
This file used CRLF newlines.
2023-03-09 13:02:22 +01:00
Noah Goldstein
46864f3b68 [InstSimplify] Simplify (shl nsw nuw X, BitWidth - 1) -> 0
https://alive2.llvm.org/ce/z/uFy5zT

Reviewed By: nikic, spatel

Differential Revision: https://reviews.llvm.org/D145327
2023-03-06 20:29:53 -06:00
Noah Goldstein
a1771ad1d5 [InstSimplify] Add tests for (shl nuw nsw X, BitWidth - 1) -> 0; NFC
Differential Revision: https://reviews.llvm.org/D145335
2023-03-06 20:29:44 -06:00
Zhongyunde
15d5c59280 [InstCombine] Improvement the analytics through the dominating condition
Address the dominating condition, the urem fold is benefit from the analytics improvements.
Fix https://github.com/llvm/llvm-project/issues/60546

NOTE: delete the calls in simplifyBinaryIntrinsic and foldICmpWithDominatingICmp
is used to reduce compile time.

Reviewed By: nikic, arsenm, erikdesjardins
Differential Revision: https://reviews.llvm.org/D144248
2023-03-01 17:03:34 +08: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
khei4
0eb45cd081 (NFC)[ConstantFold][InstSimplify] add tests for folding load for patterned arrays and structs 2023-02-23 23:16:59 +09:00
khei4
a6a0cf7bca Revert "(NFC)[ConstantFold][InstSimplify] add tests for folding load for patterned arrays and structs"
This reverts commit 9636bcd3a0091b0bb77250182ac0c549112f9196.
2023-02-23 19:36:11 +09:00
khei4
9636bcd3a0 (NFC)[ConstantFold][InstSimplify] add tests for folding load for patterned arrays and structs 2023-02-23 17:09:54 +09:00
Sanjay Patel
ae2322a0dc [InstSimplify] enhance simplifyWithOpReplaced() to allow more 'select' removal
This is a generalization of a suggestion from issue #60799
that allows removing a redundant guard of an input value
via icmp+select. It should also solve issue #60801.

This only comes into play for a select with an equality
condition where we are trying to substitute a constant into
the false arm of a select. (A 'true' select arm substitution
allows "refinement", so it is not on this code path.)

The constant must be the same in the compare and the select,
and it must be a "binop absorber" (X op C = C). That query
currently includes 'or', 'and', and 'mul', so there are tests
for all of those opcodes.

We then use "impliesPoison" on the false arm binop and the
original "Op" to be replaced to ensure that the select is not
actually blocking poison from leaking. That could be
potentially expensive as we recursively test each operand, but
it is currently limited to a depth of 2. That's enough to catch
our motivating cases, but probably nothing more complicated
(although that seems unlikely).

I don't know how to generalize a proof for Alive2 for this, but
here's a positive and negative test example to help illustrate
the subtle logic differences of poison/undef propagation:
https://alive2.llvm.org/ce/z/Sz5K-c

Differential Revision: https://reviews.llvm.org/D144493
2023-02-21 17:03:40 -05:00
Sanjay Patel
dbc00b88e9 [InstSimplify] add tests for simplifyWithOpReplaced(); NFC
issue #60799
issue #60801
2023-02-21 12:49:34 -05:00
Matt Devereau
da94a2b62a [InstSimplify] Correct icmp_lshr test to use ult instead of slt 2023-02-20 09:51:11 +00:00
Matt Devereau
8299c764bd [InstSimplify] Simplify icmp between Shl instructions of the same value
define i1 @compare_vscales() {
  %vscale = call i64 @llvm.vscale.i64()
  %vscalex2 = shl nuw nsw i64 %vscale, 1
  %vscalex4 = shl nuw nsw i64 %vscale, 2
  %cmp = icmp ult i64 %vscalex2, %vscalex4
  ret i1 %cmp
}

This IR is currently emitted by LLVM. This icmp is redundant as this snippet
can be simplified to true or false as both operands originate from the same
@llvm.vscale.i64() call.

Differential Revision: https://reviews.llvm.org/D142542
2023-02-20 09:25:34 +00:00
Kohei Asano
a4d6c7dd99 [InstSimplify] Fold LoadInst for uniform constant global variables
Fold LoadInst for uniformly initialized constants, even if there
are non-constant GEP indices.

Goal proof: https://alive2.llvm.org/ce/z/oZtVby

Motivated by https://github.com/rust-lang/rust/issues/107208

Differential Revision: https://reviews.llvm.org/D144184
2023-02-20 09:43:52 +01:00
Kohei Asano
0e3089b244 [InstSimplify] Add additional load folding tests (NFC)
For D144184.
2023-02-20 09:24:09 +01: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
Nikita Popov
9ca2c309ab [InstSimplify] Fix poison safety in insertvalue fold
We can only fold insertvalue undef, (extractvalue x, n) to x
if x is not poison, otherwise we might be replacing undef with
poison (https://alive2.llvm.org/ce/z/fnw3c8). The insertvalue
poison case is always fine.

I didn't go to particularly large effort to preserve cases where
folding with undef is still legal (mainly when there is a chain of
multiple inserts that end up covering the whole aggregate),
because this shouldn't really occur in practice: We should always
be generating the insertvalue poison form when constructing
aggregates nowadays.

Differential Revision: https://reviews.llvm.org/D144106
2023-02-16 09:39:44 +01:00
Nikita Popov
22882c39df [InstSimplify] Add additional insertvalue into undef tests (NFC) 2023-02-15 16:34:55 +01:00
Matt Devereau
d64d5772b1 Add InstSimplify tests for comparisons between known constants 2023-02-15 09:56:02 +00:00
Sanjay Patel
74a2dd1356 [InstSimplify] fix/improve folding with an SNaN vector element operand
Follow-up to the equivalent change for scalars:
D143505 / 83ba349ae0a8
2023-02-14 19:10:56 -05:00
Sanjay Patel
ac2f13f673 [InstSimplify] add tests for vectors with SNaN constants; NFC
Coverage for extension of functionality from D143505.
2023-02-14 19:10:56 -05:00
Sanjay Patel
83ba349ae0 [InstSimplify] fix/improve folding with an SNaN operand
There are 2 issues here:

1. In the default LLVM FP environment (regular FP math instructions),
   SNaN is some flavor of "don't care" which we will nail down in
   D143074, so this is just a quality-of-implementation improvement
   for default FP.
2. In the constrained FP environment (constrained intrinsics), SNaN
   must not propagate through a math operation; it has to be quieted
   according to IEEE-754 spec. That is independent of exception
   handling mode, so the current behavior is a miscompile.

Differential Revision: https://reviews.llvm.org/D143505
2023-02-14 17:51:06 -05:00
Sanjay Patel
ed8dae9a43 [InstSimplify] add tests for strict fadd with SNaN operand; NFC 2023-02-08 08:21:18 -05:00
Nikita Popov
5f01a626dd [ConstantFold] Fix inbounds inference on mismatching source element type
When inferring that a GEP of a global variable is inbounds because
there is no notional overindexing, we need to check that the
global value type and the GEP source element type match.

This was not necessary with typed pointers (because we would have
a bitcast in between), but is necessary with opaque pointers.

We should be able to recover some of the safe cases by performing
an offset based inbounds inference in DL-aware ConstantFolding.
2023-01-31 11:33:00 +01:00
Sanjay Patel
20167e8483 [InstSimplify] !(X && Y) || X --> true (for poison-safe logical ops)
https://alive2.llvm.org/ce/z/xuvL46

This is the similar to the existing folds added with:
D138853 / f2973327496fc966c4e89597
7dbeb127eaf6
...but with the and/or swapped.
2023-01-29 10:24:52 -05:00
Sanjay Patel
081a4e6712 [InstSimplify] add tests for poison-safe logical nand+or; NFC 2023-01-29 09:43:53 -05:00
Sanjay Patel
7dbeb127ea [InstSimplify] X && !(X || Y) --> false
https://alive2.llvm.org/ce/z/7J8Exr

This is a commuted variant that was not included in:
D138853 / f2973327496fc966c4e89597
2023-01-26 13:38:43 -05:00
Sanjay Patel
a56a02bc7e [InstSimplify] add commuted variants of logical and/or pattern; NFC
Existing tests were added with D138853, but that patch failed
to handle all of the commutes. The poison-safety behavior is
symmetric, so I'm not duplicating all of the tests that were
added with that patch.
2023-01-26 13:38:43 -05:00
Sanjay Patel
d178c15cac [InstSimplify] (X || Y) && Y --> Y (for poison-safe logical ops)
https://alive2.llvm.org/ce/z/oT_tEh

This is the conjugate/sibling pattern suggested in post-commit
feedback for:
9444252a674df5952bb5af2b76348ae4b45

issue #60167
2023-01-22 10:44:45 -05:00
Sanjay Patel
b9a6b4e3eb [InstSimplify] add tests for poison-safe variants of (X || Y) && Y; NFC 2023-01-22 10:44:44 -05:00
Sanjay Patel
9444252a67 [InstSimplify] with poison-safe logical ops: (X && Y) || X --> X
https://alive2.llvm.org/ce/z/ptZcJH

issue #60167
2023-01-20 17:35:24 -05:00
Sanjay Patel
5542bbf250 [InstSimplify] add tests for logical-and/or reduction; NFC
issue #60167
2023-01-20 17:35:24 -05:00
Noah Goldstein
78f29acae6 Add transform ctpop(X) -> 1 iff X is non-zero power of 2
Definitionally a non-zero power of 2 will only have 1 bit set so this
is a freebee.

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D141990
2023-01-19 11:26:24 -08:00
Noah Goldstein
eac59fa04e Add tests for ctpop(X) where X is a power of 2; NFC
Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D141989
2023-01-19 11:26:24 -08:00
Sanjay Patel
1ce06176ec [InstSimplify] reduce "mul nsw i1" to "false"
https://alive2.llvm.org/ce/z/XYGvYg

Follow-up for:
68c197f07eeae71
2023-01-18 13:51:40 -05:00
Sanjay Patel
1378e7d8b8 [InstSimplify] add no-wrap parameters to simplifyMul and add more tests; NFC
This gives mul the same capabilities as add/sub.
A potential improvement with nsw was noted in:
1720ec6da040729f17
2023-01-18 13:29:30 -05:00
Sanjay Patel
eb42b67e8c [InstCombine][InstSimplify] add tests for i1/i2 mul with no-wrap; NFC
A bug was introduced with 68c197f07eeae71 as noted in the
post-commit review comments, and there are potentially
missed smaller transforms/simplifications because no-wrap
multiply with only 1 or 2 bits eliminates some potential
results.
2023-01-18 10:17:06 -05:00
Matt Arsenault
c663b8c429 ValueTracking: Teach CannotBeOrderedLessThanZero about rounding intrinsics
These should obviously preserve the sign although the variety of these
always confuses me.
2023-01-15 09:44:45 -05:00
Paul Walker
eae26b6640 [IRBuilder] Use canonical i64 type for insertelement index used by vector splats.
Instcombine prefers this canonical form (see getPreferredVectorIndex),
as does IRBuilder when passing the index as an integer so we may as
well use the prefered form from creation.

NOTE: All test changes are mechanical with nothing else expected
beyond a change of index type from i32 to i64.

Differential Revision: https://reviews.llvm.org/D140983
2023-01-11 14:08:06 +00:00
Alex Richardson
968f2c77a8 Re-gernerate a test in preparation for D141060 2023-01-06 17:38:55 +00:00
Sanjay Patel
6c232db2ae [InstSimplify] fold selects where true/false arm is the same as condition
We managed to fold related patterns in issue #59704,
but we were missing these more basic folds:
https://alive2.llvm.org/ce/z/y6d7SN
2022-12-30 08:54:09 -05:00
Sanjay Patel
94944f800c [InstSimplify] add tests for select-of-bool; NFC 2022-12-30 08:54:08 -05:00
Sanjay Patel
f0faea5714 [InstSimplify] fold exact divide to poison if it is known to not divide evenly
This is related to the discussion in D140665. I was looking over the demanded
bits implementation in IR and noticed that we just bail out of a potential
fold if a udiv is exact:
82be8a1d2b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (L799)

Also, see tests added with 7f0c11509e8f.

Then, I saw that we could lose a fold to poison if we zap the exact with that
transform, so this patch tries to catch that as a preliminary step.

Alive2 proofs:
https://alive2.llvm.org/ce/z/zCjKM7
https://alive2.llvm.org/ce/z/-tz_RK (trailing zeros must be "less-than")
https://alive2.llvm.org/ce/z/c9CMsJ (general proof and specific example)

Differential Revision: https://reviews.llvm.org/D140733
2022-12-29 10:26:50 -05:00
Sanjay Patel
935a6525e4 [InstSimplify] add tests for div exact; NFC 2022-12-28 14:08:47 -05:00
Matt Arsenault
8f111ecd75 InstSimplify: Split isKnownNeverInfinity tests into separate file
This fixes an annoying assymmetry in the test organization. We have
known-never-nan.ll for dedicated isKnownNeverNaN handling tests, but
the isKnownNeverInfinity were in floating-point-compare.ll. Move the
more targeted tests into a separate file to match.
2022-12-22 09:38:14 -05:00
Matt Arsenault
876f3d6c91 ValueTracking: Add test for isKnownNeverInfinity for fptrunc 2022-12-22 09:38:14 -05:00
Matt Arsenault
94a703ecdc ValueTracking: Add test for fneg isKnownNeverNaN handling
This didn't have a negative test.
2022-12-22 09:38:14 -05:00