7478 Commits

Author SHA1 Message Date
Sanjay Patel
0eedc9e567 [InstCombine] bitrev (zext i1 X) --> select X, SMinC, 0
https://alive2.llvm.org/ce/z/ZXCtgi

This breaks the infinite combine loop for issue #59897,
but we may still need more changes to avoid those loops.
2023-01-09 12:27:37 -05:00
Sanjay Patel
b1e6947618 [InstCombine] add tests for bitreverse of i1; NFC 2023-01-09 12:27:37 -05:00
Sanjay Patel
2dcbd740ee [InstCombine] reduce smul.ov with i1 types to 'and'
https://alive2.llvm.org/ce/z/5tLkW6

There's still a miscompile bug as shown in issue #59876 / D141214 .
2023-01-09 10:27:15 -05:00
Sanjay Patel
fc9d54a4a1 [InstCombine] add tests for smul/umul with overflow with i1 types; NFC
More coverage for D141214 / issue #59876
2023-01-09 10:27:15 -05:00
Nikita Popov
59f91ddf90 [InstCombine] Preserve alignment in atomicrmw -> store fold
Preserve the alignment of the original atomicrmw, rather than using
the ABI alignment.

The same problem exists for loads, but that code is being removed
in D141277 anyway.
2023-01-09 15:37:24 +01:00
Nikita Popov
22cafc7381 [InstCombine] Test alignment in atomicrmw -> store transform (NFC)
And regenerate test checks. The current alignment is incorrect.
2023-01-09 15:26:01 +01:00
Jamie Hill-Daniel
6b9317f52a [InstCombine] Fold zero check followed by decrement to usub.sat
Fold (a == 0) : 0 ? a - 1 into usub.sat(a, 1).

Differential Revision: https://reviews.llvm.org/D140798
2023-01-09 14:22:25 +01:00
Jamie Hill-Daniel
8f4795ef13 [InstCombine] Add tests for saturating subtract by one (NFC)
Tests for D140798.
2023-01-09 14:10:28 +01:00
Noah Goldstein
6d839621da [InstCombine] Canonicalize (A & B_Pow2) eq/ne B_Pow2 patterns
1. A & B_Pow2 != B_Pow2 -> A & B_Pow2 == 0
   https://alive2.llvm.org/ce/z/KVUej4

2. A & B_Pow2 == B_Pow2 -> A & B_Pow2 != 0
   https://alive2.llvm.org/ce/z/PVv9FR

This allows the patterns to more easily be analyzed elsewhere.

Differential Revision: https://reviews.llvm.org/D141090
2023-01-09 12:48:28 +01:00
chenglin.bi
33794cffcf [InstCombine] Fold logic-and/logic-or by distributive laws part2
Follow up https://reviews.llvm.org/D139408, support `and/or+select` patterns
X && Z || Y && Z --> (X || Y) && Z
https://alive2.llvm.org/ce/z/EMCkBG
https://alive2.llvm.org/ce/z/Q-YRvr
https://alive2.llvm.org/ce/z/SFkVQc
https://alive2.llvm.org/ce/z/S9MCuJ
https://alive2.llvm.org/ce/z/KZ7zzz

(X || Z) && (Y || Z) --> (X && Y) || Z
https://alive2.llvm.org/ce/z/Ggpa8-
https://alive2.llvm.org/ce/z/nhQRLY
https://alive2.llvm.org/ce/z/zpmEnq
https://alive2.llvm.org/ce/z/7omsrf
https://alive2.llvm.org/ce/z/CWBzBp

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D139630
2023-01-09 10:21:17 +08:00
Sanjay Patel
21d3871b7c [InstCombine] fold not-shift of signbit to icmp+zext, part 2
Follow-up to:
6c39a3aae1dc

That converted a pattern with ashr directly to icmp+zext, and
this updates the pattern that we used to convert to.

This canonicalizes to icmp for better analysis in the minimum case
and shortens patterns where the source type is not the same as dest type:
https://alive2.llvm.org/ce/z/tpXJ64
https://alive2.llvm.org/ce/z/dQ405O

This requires an adjustment to an icmp transform to avoid infinite looping.
2023-01-08 12:04:09 -05:00
luxufan
eda8e999dd [InstCombine] Combine (zext a) mul (zext b) to llvm.umul.with.overflow only if mul has NUW flag
Fixes: https://github.com/llvm/llvm-project/issues/59836

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D141031
2023-01-08 14:41:59 +08:00
OCHyams
7ea47f9e41 [DebugInfo] Replace UndefValue with PoisonValue in setKillLocation
This helps towards the effort to remove UndefValue from LLVM.

Related to https://discourse.llvm.org/t/auto-undef-debug-uses-of-a-deleted-value

Reviewed By: nlopes

Differential Revision: https://reviews.llvm.org/D140905
2023-01-06 10:51:02 +00:00
Sanjay Patel
ad14cef1d5 [InstCombine] add tests for cmp of pow2 mask; NFC 2023-01-05 12:28:08 -05:00
luxufan
5006d82329 [InstCombine][NFC] Regenerate test cases by update_test_checks.py 2023-01-05 14:38:08 +08:00
luxufan
90cf22b91b [InstCombine][NFC] Regenerate test cases by update_test_checks.py 2023-01-05 14:22:07 +08:00
chenglin.bi
87b2c760d0 [Instcombine] fold logic ops to select
(C & X) | ~(C | Y) -> C ? X : ~Y

https://alive2.llvm.org/ce/z/4yLh_i

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D139080
2023-01-05 12:04:35 +08:00
Sanjay Patel
c43a7874a3 [InstCombine] don't let 'exact' inhibit demanded bits folds for udiv
We shouldn't penalize instructions that have extra flags.

Drop the poison-generating flags if needed instead of bailing out.
This makes canonicalization/optimization more uniform.

There is a chance that dropping flags will cause some
other transform to not fire, but we added a preliminary
patch to avoid that with:
f0faea571403

See D140665 for more details.
2023-01-04 13:13:02 -05:00
chenglin.bi
ec48682ce9 [InstCombine] add more tests into select-factorize; NFC 2023-01-04 17:49:47 +08:00
chenglin.bi
50a1c9b107 [Instcombine] Regenerate tests for logical-select; NFC 2023-01-04 16:31:56 +08:00
Amaury Séchet
86616443bf [NFC] Autogenerate test/Transforms/InstCombine/fls.ll 2023-01-04 02:05:30 +00:00
Owen Anderson
ec40c8f6fe [ValueTracking] Improve ComputeNumSignBits to handle Trunc
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D140796
2023-01-03 15:26:21 -07:00
Nikita Popov
82cf72a83b [InstCombine] Convert some tests to opaque pointers (NFC)
These involved regenerating test checks. There are two significant
differences here:

1. With typed pointers we sometimes swapped gep and addrspacecast,
   as a side-effect of other transforms. However, the current
   direction is likely undesirable, and we should canonicalize in
   the reverse direction instead (gep of ac, instead of ac of gep).
   This should be done after typed pointers are removed, to avoid
   conflicting transforms.

2. The "descaling" optimization isn't really compatible with
   opaque pointers. This will be addressed longer-term by moving
   away from type-based GEP, at which point the form with explicit
   multiplications will be the canonical one.
2023-01-03 17:20:43 +01:00
Nikita Popov
f3a113ff48 [TLI] Fix signature for fprintf (PR59757)
fprintf() requires two pointer arguments.

Fixes https://github.com/llvm/llvm-project/issues/59757.
2023-01-03 15:28:47 +01:00
Sanjay Patel
cb9569049c [InstCombine] fold mask with not-of-sext-bool to select
~sext(A) & Op1 --> A ? 0 : Op1

With no extra uses, this pattern is already reduced,
but we would miss it in examples such as issue #59773.

https://alive2.llvm.org/ce/z/WGLcSR
2023-01-02 13:33:28 -05:00
Sanjay Patel
871c415f2d [InstCombine] add tests for not-sext-bool+mask; NFC 2023-01-02 13:33:28 -05:00
Roman Lebedev
cf58063a40
[InstCombine] Canonicalize math-y conditional negation into a select
https://alive2.llvm.org/ce/z/vPs-gZ

This is a larger pattern than would seem necessary, with minimal being:
* `and` https://alive2.llvm.org/ce/z/q9-MqK
* `or`  https://alive2.llvm.org/ce/z/AUUEMZ
* `xor` https://alive2.llvm.org/ce/z/dm3Ume
.. so for all others, we canonicalize away from math to `select`,
but there we canonicalize in the opposite direction.

Fixes https://github.com/llvm/llvm-project/issues/59791
2023-01-02 21:26:37 +03:00
Roman Lebedev
1fb07108d4
[NFC][InstCombine] Add tests for conditional negation via math
https://github.com/llvm/llvm-project/issues/59791
2023-01-02 21:26:37 +03:00
Nikita Popov
81ac46445b [InstCombine] Support vectors in icmp of GEP fold
EmitGEPOffset() supports vector GEPs nowadays, so we don't need
any further code changes.

compare_gep_with_base_vector1 shows a weakness in folding the
resulting comparison if an index splat has to be performed.
2023-01-02 15:29:13 +01:00
Nikita Popov
a21458c7b0 [InstCombine] Add tests for icmp of vector GEP (NFC)
These show up as regressions with opaque pointers.
2023-01-02 15:18:19 +01:00
Nikita Popov
e44b11d9b6 [ValueTracking] Treat branch on undef as UB as well
We were already treating branch on poison as UB, but branch on
undef is also UB. Move the checks into the correct function.

From LangRef for br:

> If ‘cond’ is poison or undef, this instruction has undefined behavior.

From LangRef for switch:

> If ‘value’ is poison or undef, this instruction has undefined behavior.

There is a minor regression in dont-distribute-phi.ll, apparently
we handle that pattern in logical but not bitwise form.
2023-01-02 12:34:23 +01:00
Matt Arsenault
7e720b010a ValueTracking: Fix canCreateUndefOrPoison for saturating shifts
These need to consider the shift amount.
2022-12-30 11:28:28 -05:00
Sanjay Patel
30af2e3191 [InstCombine] avoid miscompile in sinkNotIntoLogicalOp()
Fixes #59704
2022-12-29 14:33:41 -05:00
Sanjay Patel
e5a7309800 [InstCombine] add test for miscompile from sinkNotIntoLogicalOp(); NFC
issue #59704
2022-12-29 14:33:41 -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
Chenbing Zheng
1f84e72b7b [InstCombine] Fold (X << Z) / (X * Y) -> (1 << Z) / Y
Alive2: https://alive2.llvm.org/ce/z/CBJLeP
2022-12-29 17:30:49 +08:00
Sanjay Patel
7f0c11509e [InstCombine] add tests for udiv-by-constant demanded bits; NFC 2022-12-28 14:08:47 -05:00
Sanjay Patel
862e35e25a [InstCombine] preserve signbit semantics of NAN with fold to fabs
As discussed in issue #59279, we want fneg/fabs to conform to the
IEEE-754 spec for signbit operations - quoting from section 5.5.1
of IEEE-754-2008:
"negate(x) copies a floating-point operand x to a destination in
the same format, reversing the sign bit"
"abs(x) copies a floating-point operand x to a destination in the
same format, setting the sign bit to 0 (positive)"
"The operations treat floating-point numbers and NaNs alike."

So we gate this transform with "nnan" in addition to "nsz":
(X > 0.0) ? X : -X --> fabs(X)

Without that restriction, we could have for example:
(+NaN > 0.0) ? +NaN : -NaN --> -NaN
(because an ordered compare with NaN is always false)
That would be different than fabs(+NaN) --> +NaN.

More fabs/fneg patterns demonstrated here:
https://godbolt.org/z/h8ecc659d
(without any FMF, these are correct independently of this patch -
no fabs should be created)

The code change is a one-liner, but we have lots of tests diffs
because there are many variations of the basic pattern.

Differential Revision: https://reviews.llvm.org/D139785
2022-12-28 10:28:23 -05:00
Nikita Popov
51f2f595d1 [InstCombine] Convert test to opaque pointers (NFC)
Slightly adjust the test so it uses non-zero GEP indices, otherwise
these would get folded away with opaque pointers.
2022-12-27 16:34:04 +01:00
Nikita Popov
c93e7dec7e [InstCombine] Convert some tests to opaque pointers (NFC)
Check lines for these were regenerated, but without any
significant changes (mostly different GEP source element types).
2022-12-27 16:29:52 +01:00
Nikita Popov
4b04c30340 [InstCombine] Regenerate test checks (NFC) 2022-12-27 16:21:31 +01:00
Nikita Popov
cd9e2ba0b3 Revert "[InstCombine] Convert some tests to opaque pointers (NFC)"
This reverts commit 66cea84681e16f3d4ebdc69031824b114a0d5681.

I did not intend to commit all the changes in here, but only the
ones with no significant differences.
2022-12-27 16:19:13 +01:00
Nikita Popov
66cea84681 [InstCombine] Convert some tests to opaque pointers (NFC) 2022-12-27 16:12:59 +01:00
Sanjay Patel
a0c8017286 [InstCombine] do not add "nuw" to 1<<X if the "1" has undefined elements
This was noted as a potential miscompile in the post-commit feedback
for the patch that added this fold:
d4493dd1ed58ac3f1eab0
2022-12-26 13:16:03 -05:00
Sanjay Patel
00c7840115 [InstCombine] replace undef in vector tests with poison; NFC
I left a few of the existing undef tests in place for extra
coverage and because one of those was noted as a miscompile in
the post-commit feedback for d4493dd1ed58ac3f1eab0, but we are
transitioning to poison, so it is more valuable to test the
expected IR going forward.
2022-12-26 13:16:03 -05:00
Matt Arsenault
de8e0a4397 ValueTracking: Teach canCreateUndefOrPoison about saturating intrinsics 2022-12-23 09:42:33 -05:00
Matt Arsenault
c59ae47055 InstCombine: Add baseline tests for saturating poison handling 2022-12-23 09:42:33 -05:00
Chenbing Zheng
bff1f8c79b [InstCombine] complete (X << Z) / (Y << Z) --> X / Y
Add one more situations for this fold.
For unsigned div, 'nsw' on both shifts + 'nuw' on the dividend.

Alive2: https://alive2.llvm.org/ce/z/sELF76

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D139997
2022-12-23 11:56:52 +08:00
Nikita Popov
3f46fcac27 [InstCombine] Regenerate test checks (NFC) 2022-12-22 16:13:56 +01:00
Matt Devereau
fa641693c8 [AArch64][SVE] Remove dso_local and local_unnamed_addr from muladdsub.ll 2022-12-22 10:08:37 +00:00