1226 Commits

Author SHA1 Message Date
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
Matt Arsenault
2c52c811ee ValueTracking: Document some difficult isKnownNeverInfinity cases
Add a comment and some negative tests. I'd like to have test coverage
and explicit handling of all the math operations for clarity.
2022-12-20 13:22:22 -05:00
Matt Arsenault
407b18820c ValueTracking: Add tests for isKnownNeverInfinity for llvm.log* 2022-12-20 13:17:03 -05:00
Matt Arsenault
2bf17cc048 ValueTracking: Teach isKnownNeverInfinity about llvm.sin/llvm.cos 2022-12-20 13:17:03 -05:00
Matt Arsenault
6d125fee8c ValueTracking: Add isKnownNeverInfinity tests for sin/cos 2022-12-20 13:17:03 -05:00
Matt Arsenault
9a21475651 ValueTracking: Teach isKnownNeverInfinity about sqrt 2022-12-20 13:03:07 -05:00
Matt Arsenault
b5963ea088 ValueTracking: Add base test for isKnownNeverInfinity of sqrt 2022-12-20 13:03:07 -05:00
Matt Arsenault
41dd02e857 ValueTracking: Teach isKnownNeverInfinity about min/max functions 2022-12-20 12:52:59 -05:00
Matt Arsenault
ef550ce361 ValueTracking: Add tests for isKnownNeverInfinity for min/max intrinsics 2022-12-20 12:52:59 -05:00
Matt Arsenault
4e37d00b9d ValueTracking: Teach isKnownNeverInfinity about rounding intrinsics 2022-12-20 12:45:07 -05:00
Matt Arsenault
2bfe62ed6a ValueTracking: Add tests for isKnownNeverInfinity for rounding intrinsics 2022-12-20 12:45:07 -05:00
Matt Arsenault
6acf6661dd ConstantFolding: Ignore output denorm mode for canonicalize
Alive2 opt plugin is now happy with the test. Fixes issue 59245
2022-12-13 08:44:21 -05:00
Matt Arsenault
4fa54f8b81 ConstantFolding: Fix handling of canonicalize for ppc_fp128 0s 2022-12-13 08:44:20 -05:00
Sanjay Patel
9055661b95 [InstSimplify] try harder to propagate existing NaN values through FP folds
Any undef element in a vector would trigger the whole constant
to be replaced with a canonical NaN. This propagates each
element when possible.

issue #59122
2022-12-12 17:52:14 -05:00
Sanjay Patel
44c3c93eae [InstSimplify] add tests for vectors with NaN + partial undef; NFC
issue #59122
2022-12-12 17:52:14 -05:00
Bjorn Pettersson
3528e63d89 [test] Remove duplicate RUN lines in Transform tests 2022-12-08 11:47:16 +01:00
Nikita Popov
8a09875dd1 [InstSimplify] Do not remove insertvalue of undef into poison
We cannot remove an insertvalue of undef if it inserts into a
potentially poison value. The new implementation matches that of
insertelement.

See https://alive2.llvm.org/ce/z/pCcFsW for the previously
incorrect transform.
2022-12-08 11:35:59 +01:00
Nikita Popov
8992fce0e0 [InstSimplify] Fix check lines (NFC)
Accidentally used the wrong binary in the previous commit.
2022-12-08 11:22:49 +01:00
Nikita Popov
e161454d2e [InstSimplify] Add test for insertvalue of undef (NFC) 2022-12-08 11:16:46 +01:00