1142 Commits

Author SHA1 Message Date
annamthomas
98d8b688bd
[InstSimplify] Check call for FMF instead of CtxI (#71585)
This code was incorrectly checking that the CtxI has required FMF, but
the context instruction need not always be the instrinsic call.

Check that the intrinsic call has the required FMF.

Fixes PR71548.
2023-11-08 10:25:11 -05:00
Anna Thomas
f0cdf4b468 [InstCombine] Check FPMathOperator for Ctx before FMF check
We need to check FPMathOperator for Ctx instruction before checking fast
math flag on this Ctx.

Ctx is not always an FPMathOperator, so explicitly check for it.

Fixes #71548.
2023-11-07 10:50:19 -05:00
Nikita Popov
0c6a77baa6 [InstSimplify] Remove redundant simplifyAndOrOfICmpsWithZero() fold (NFCI)
This has been subsumed by simplifyAndOrWithICmpEq().
2023-11-07 14:53:32 +01:00
Nikita Popov
fb01f683af [InstSimplify] Remove redundant simplifyAndOrOfICmpsWithLimitConst() fold (NFCI)
This fold has been subsumed by simplifyAndOrWithICmpEq().
2023-11-07 14:35:03 +01:00
Nikita Popov
060de415af Reapply [InstCombine] Simplify and/or of icmp eq with op replacement (#70335)
Relative to the first attempt, this contains two changes:

First, we only handle the case where one side simplifies to true or
false, instead of calling simplification recursively. The previous
approach would return poison if one operand simplified to poison
(under the equality assumption), which is incorrect.

Second, we do not fold llvm.is.constant in simplifyWithOpReplaced().
We may be assuming that a value is constant, if the equality holds,
but it may not actually be constant. This is nominally just a QoI
issue, but the std::list implementation in libstdc++ relies on the
precise behavior in a way that causes miscompiles.

-----

and/or in logical (select) form benefit from generic simplifications via
simplifyWithOpReplaced(). However, the corresponding fold for plain
and/or currently does not exist.

Similar to selects, there are two general cases for this fold
(illustrated with `and`, but there are `or` conjugates).

The basic case is something like `(a == b) & c`, where the replacement
of a with b or b with a inside c allows it to fold to true or false.
Then the whole operation will fold to either false or `a == b`.

The second case is something like `(a != b) & c`, where the replacement
inside c allows it to fold to false. In that case, the operand can be
replaced with c, because in the case where a == b (and thus the icmp is
false), c itself will already be false.

As the test diffs show, this catches quite a lot of patterns in existing
test coverage. This also obsoletes quite a few existing special-case
and/or of icmp folds we have (e.g. simplifyAndOrOfICmpsWithLimitConst),
but I haven't removed anything as part of this patch in the interest of
risk mitigation.

Fixes #69050.
Fixes #69091.
2023-11-03 10:16:15 +01:00
Noah Goldstein
8c2fcf5b77 [InstSimplify] Add some basic simplifications for llvm.ptrmask
Mostly the same as `and`. We also have a check for a useless
`llvm.ptrmask` if the ptr is already known aligned.

Differential Revision: https://reviews.llvm.org/D156633
2023-11-01 23:50:35 -05:00
Nikita Popov
e91812792a [InstSimplify] Avoid ConstantExpr::getIntegerCast() (NFCI)
This always works on a constant integer or integer splat, so the
constant fold here should always succeed.
2023-11-01 11:15:18 +01:00
Nikita Popov
e46dd6fbc0 Revert "[InstCombine] Simplify and/or of icmp eq with op replacement (#70335)"
This reverts commit 1770a2e325192f1665018e21200596da1904a330.

Stage 2 llvm-tblgen crashes when generating X86GenAsmWriter.inc and
other files.
2023-10-30 18:33:03 +01:00
Nikita Popov
1770a2e325
[InstCombine] Simplify and/or of icmp eq with op replacement (#70335)
and/or in logical (select) form benefit from generic simplifications via
simplifyWithOpReplaced(). However, the corresponding fold for plain
and/or currently does not exist.

Similar to selects, there are two general cases for this fold
(illustrated with `and`, but there are `or` conjugates).

The basic case is something like `(a == b) & c`, where the replacement
of a with b or b with a inside c allows it to fold to true or false.
Then the whole operation will fold to either false or `a == b`.

The second case is something like `(a != b) & c`, where the replacement
inside c allows it to fold to false. In that case, the operand can be
replaced with c, because in the case where a == b (and thus the icmp is
false), c itself will already be false.

As the test diffs show, this catches quite a lot of patterns in existing
test coverage. This also obsoletes quite a few existing special-case
and/or of icmp folds we have (e.g. simplifyAndOrOfICmpsWithLimitConst),
but I haven't removed anything as part of this patch in the interest of
risk mitigation.

Fixes #69050.
Fixes #69091.
2023-10-30 10:05:39 +01:00
Pierre van Houtryve
4fc1e7db27
[InstSimplify] Fold (a != 0) ? abs(a) : 0 (#70305)
Solves #70204
2023-10-27 14:52:09 +02:00
Nikita Popov
4638c29c3d [InstSimplify] Remove redundant pointer icmp fold (NFCI)
This fold is already performed as part of simplifyICmpWithZero().
2023-10-26 14:30:33 +02:00
Craig Topper
a5686c2b55
[InstSimplify] Avoid use of ConstantExpr::getICmp. NFC (#67873) 2023-09-30 13:01:29 -07:00
Craig Topper
abcaebfe3a [InstSimplify] Use cast instead of dyn_cast+assert. NFC 2023-09-29 22:08:53 -07:00
Nikita Popov
b35f2940e9 [InstSimplify] Avoid use of ConstantExpr::getCast()
Use the constant folding API instead.

One of these uses actually improves results, because the bitcast
expression gets folded away.
2023-09-29 10:23:40 +02:00
Nikita Popov
a09e32e5fe [InstSimplify] Respect UseInstrInfo in more folds
Some folds using m_NUW, m_NSW style matchers were missed, make
sure they respect UseInstrInfo.

This is part of #53218, but not a complete fix for the issue.
2023-09-26 13:54:03 +02:00
Yingwei Zheng
0d821b22e0
[InstSimplify] Generalize fold for icmp ugt/ule (pow2 << X), signmask
Alive2: https://alive2.llvm.org/ce/z/wZ41t7
2023-09-25 00:07:20 +08:00
Nikita Popov
c41b4b6397 [InstCombine] Make flag drop during select equiv fold more generic
Instead of unsetting flags on the instruction, attempting the
fold, and the resetting the flags if it failed, add support to
simplifyWithOpReplaced() to ignore poison-generating flags/metadata
and collect all instructions where they may need to be dropped.

This allows us to perform the fold a) with poison-generating
metadata, which was previously not handled and b) poison-generating
flags/metadata that are not on the root instruction.

Proof for the ctpop case: https://alive2.llvm.org/ce/z/3H3HFs

Fixes https://github.com/llvm/llvm-project/issues/62450.
2023-09-19 14:54:25 +02:00
Yingwei Zheng
be2723da5c
[InstSimplify] Fold icmp of X and/or C1 and X and/or C2 into constant (#65905)
This patch simplifies the pattern `icmp X and/or C1, X and/or C2` when
one constant mask is the subset of the other.
If `C1 & C2 == C1`, `A = X and/or C1`, `B = X and/or C2`, we can do the
following folds:
`icmp ule A, B -> true`
`icmp ugt A, B -> false`
We can apply similar folds for signed predicates when `C1` and `C2` are
the same sign:
`icmp sle A, B -> true`
`icmp sgt A, B -> false`

Alive2: https://alive2.llvm.org/ce/z/Q4ekP5
Fixes #65833.
2023-09-18 21:32:48 +08:00
Paul Walker
c7d65e4466 [IR] Enable load/store/alloca for arrays of scalable vectors.
Differential Revision: https://reviews.llvm.org/D158517
2023-09-14 13:49:01 +00:00
Matt Arsenault
00061843bd InstSimplify: Simplifications for ldexp
Ported from old amdgcn intrinsic which will soon be deleted.

https://reviews.llvm.org/D149587
2023-09-13 08:38:48 +03:00
Matt Arsenault
6f2e943de6 InstSimplify: Handle folding fcmp with literal nans without a context instruction
Fixes reported assert after ddb3f12c428bc4bd5a98913d74dfd7f2402bdfd8
2023-09-02 10:22:09 -04:00
Matt Arsenault
5dcd6669ff InstSimplify: Handle exp10(log10(x)) -> x
Copy from exp/exp2 case.

https://reviews.llvm.org/D157894
2023-09-02 09:21:47 -04:00
Matt Arsenault
da077a52c4 InstSimplify: Handle log10(exp10(x))
Copied from the exp/exp2 cases

https://reviews.llvm.org/D157894
2023-09-02 08:57:54 -04:00
Philip Reames
294ad08ecc Revert "Revert "InstSimplify: Use correct interested FP classes when simplifying fcmp""
This reverts commit 89f0314ee14a4d7f5a92fd63574ba545863df016.  Change does not build.
2023-09-01 12:17:36 -07:00
Zequan Wu
89f0314ee1 Revert "InstSimplify: Use correct interested FP classes when simplifying fcmp"
Revert "InstSimplify: Add baseline tests for reported regression"
Revert "InstSimplify: Start cleaning up simplifyFCmpInst"

This reverts commit 0637b00041c7d6a191d51d9966c4f5f41fb97ab5.
This reverts commit 239fb206de35935416e652b89725d5f3193f78f5.
This reverts commit ddb3f12c428bc4bd5a98913d74dfd7f2402bdfd8.

These commits causes crashes when compiling chromium code, attached reduced ir at: https://reviews.llvm.org/D151887#4634914
2023-09-01 14:54:27 -04:00
Matt Arsenault
0637b00041 InstSimplify: Use correct interested FP classes when simplifying fcmp
We are interested in the cases that we don't want, so this was
backwards.

Fixes regression reported after ddb3f12c428bc4bd5a98913d74dfd7f2402bdfd8
2023-08-31 10:11:52 -04:00
Matt Arsenault
ddb3f12c42 InstSimplify: Start cleaning up simplifyFCmpInst
Also picks up a few improvements (Some of the fcmp.ll
test names imply they aren't quite testing what was intended.
Checking the sign bit can't be performed with a compare to a 0).

Much of the logic in here is the same as the class detection
logic of fcmpToClassTest. We could unify more with a weaker
version of fcmpToClassTest which returns implied classes rather
than exact class-like compares. Also could unify more with detection
of possible classes in non-splat vectors.

One problem here is we now only perform folds that used
to always work now require a context instruction. This is
because fcmpToClassTest requires the parent function.
Either fcmpToClassTest could tolerate a missing context
function, or we could require passing in one to simplifyFCmpInst.
Without this it's possible to hit the !isNan assert (which feels like
an unnecessary assert). In any case, these cases don't appear in
any tests.

https://reviews.llvm.org/D151887
2023-08-30 11:53:05 -04:00
Nikita Popov
98f16dfa13 [InstSimplify] Simplify simplifyRelativeLoad (NFCI)
Drop an unnecessary bitcast and make use of the
ConstantFoldLoadFromConstPtr API that accepts an Offset, instead
of going through a GEP expression.
2023-08-09 16:18:04 +02:00
Zhongyunde
68ea002a63 [InstSimplify] Check the NonZero for power of two value
Fixes https://github.com/llvm/llvm-project/issues/64339

proofs: https://alive2.llvm.org/ce/z/yZ_I2a

Reviewed By: goldstein.w.n
Differential Revision: https://reviews.llvm.org/D156881
2023-08-04 09:14:45 +08:00
Bjorn Pettersson
e6e9a87534 Drop some typed pointer handling
Differential Revision: https://reviews.llvm.org/D156739
2023-08-02 12:08:37 +02:00
Zhongyunde
497966f7f2 Reland [InstSimplify] Remove the remainder loop if we know the mask is always true
We check the loop trip count is known a power of 2 to determine
whether the tail loop can be eliminated in D146199.
However, the remainder loop of mask scalable loop can also be removed
If we know the mask is always going to be true for every vector iteration.
Depend on the assume of power-of-two vscale on D155350

proofs: https://alive2.llvm.org/ce/z/bT62Wa

Fix https://github.com/llvm/llvm-project/issues/63616.

Reviewed By: goldstein.w.n, nikic, david-arm, paulwalker-arm
Differential Revision: https://reviews.llvm.org/D154953
2023-08-01 22:20:22 +08:00
Nikita Popov
eb9fce092a Revert "[InstSimplify] Remove the remainder loop if we know the mask is always true"
This reverts commit 3e386b227886e2fb77b0c1e9182026c4e049f346.

Next to the original fold, this also implements an unnecessary and
inappropriate simplifyICmpWithDominatingAssume() based fold.
2023-08-01 09:03:20 +02:00
Zhongyunde
3e386b2278 [InstSimplify] Remove the remainder loop if we know the mask is always true
We check the loop trip count is known a power of 2 to determine
whether the tail loop can be eliminated in D146199.
However, the remainder loop of mask scalable loop can also be removed
If we know the mask is always going to be true for every vector iteration.
Depend on the assume of power-of-two vscale on D155350

proofs: https://alive2.llvm.org/ce/z/FkTMoy

Fix https://github.com/llvm/llvm-project/issues/63616.

Reviewed By: goldstein.w.n, nikic, david-arm, paulwalker-arm
Differential Revision: https://reviews.llvm.org/D154953
2023-08-01 11:20:20 +08:00
Zhongyunde
05aae0839f Reland [AArch64][NFC] Call the API getVScaleRange directly
Use the maximum 64 for BitWidth of getVScaleRange to avoid returning an empty range.

the previous changes bring in a Buildbot failure because MinSVEVectorSize = MinSVEVectorSize.
    error: explicitly assigning value of variable of type 'unsigned int' to itself [-Werror,-Wself-assign]

Reviewed By: sdesmalen, nikic, dmgreen
Differential Revision: https://reviews.llvm.org/D155708
2023-07-26 18:55:31 +08:00
Zhongyunde
ebaac2b2d6 Revert "[AArch64][NFC] Call the API getVScaleRange directly"
This reverts commit 67005c8e6fa9464f8bc436305a422071013ae499.
2023-07-26 16:44:14 +08:00
Zhongyunde
67005c8e6f [AArch64][NFC] Call the API getVScaleRange directly
Use the maximum 64 for BitWidth of getVScaleRange to
avoid returning an empty range.

Reviewed By: sdesmalen, nikic, dmgreen
Differential Revision: https://reviews.llvm.org/D155708
2023-07-26 15:54:04 +08:00
Nikita Popov
0db5d8e123 Reapply [InstSimplify] Make simplifyWithOpReplaced() recursive (PR63104)
A similar assumption as for the x^x case also existed for the absorber
case, which lead to a stage2 miscompile. That assumption is not fixed.

-----

Support replacement of operands not only in the immediate
instruction, but also instructions it uses.

To the most part, this extension is straightforward, but there are
two bits worth highlighting:

First, we can now no longer assume that if the Op is a vector, the
instruction also returns a vector. If Op is a vector and the
instruction returns a scalar, we should consider it as a cross-lane
operation.

Second, for the x ^ x special case and the absorber special case, we
can no longer assume that one of the operands is RepOp, as we might
have a replacement higher up the instruction chain.

There is one optimization regression, but it is in a fuzzer-generated
test case.

Fixes https://github.com/llvm/llvm-project/issues/63104.
2023-07-18 10:36:39 +02:00
Matt Arsenault
29d2a9bf9d InstSimplify: Handle basic folds for frexp
Handle constant folding and idempotent folding. Not sure
this is an appropriate use of undef for the inf/nan case. The
C version says the second result is "unspecified". The AMDGPU
instruction returns 0.
2023-07-17 17:28:01 -04:00
Nikita Popov
2bc7d02312 Revert "[InstSimplify] Make simplifyWithOpReplaced() recursive (PR63104)"
This is very likely the cause of a stage 2 failure in
Transforms/LoopVectorize/check-prof-info.ll. Revert until I can
investigate this.

This reverts commit 3d199d086e076f0b9b90d4c59f2226a417a639b5.
2023-07-14 18:33:39 +02:00
Nikita Popov
3d199d086e [InstSimplify] Make simplifyWithOpReplaced() recursive (PR63104)
Support replacement of operands not only in the immediate
instruction, but also instructions it uses.

To the most part, this extension is straightforward, but there are
two bits worth highlighting:

First, we can now no longer assume that if the Op is a vector, the
instruction also returns a vector. If Op is a vector and the
instruction returns a scalar, we should consider it as a cross-lane
operation.

Second, for the x ^ x special case, we can no longer assume that
the operand is RepOp, as we might have a replacement higher up the
instruction chain.

There is one optimization regression, but it is in a fuzzer-generated
test case.

Fixes https://github.com/llvm/llvm-project/issues/63104.
2023-07-14 16:33:40 +02:00
Nikita Popov
e21ccdcd98 [InstSimplify] Revert unintentional change
This change was made as part of 91b84811ab11ea169cb8ac3d6ccf8a4b8740ec98,
which was only supposed to add tests.
2023-07-14 16:27:49 +02:00
Nikita Popov
547544112b [InstSimplify] Allow gep inbounds x, 0 -> x in non-refining op replacement
After the semantics change from https://reviews.llvm.org/D154051,
gep inbounds x, 0 can no longer produce poison. As such, we can
also perform this fold during non-refining operand replacement
and avoid unnecessary drops of the inbounds flag.

The online alive2 version has not been update to the new
semantics yet, but we can use the following proof locally:

    define ptr @src(ptr %base, i64 %offset) {
      %cmp = icmp eq i64 %offset, 0
      %gep = getelementptr inbounds i8, ptr %base, i64 %offset
      %sel = select i1 %cmp, ptr %base, ptr %gep
      ret ptr %sel
    }

    define ptr @tgt(ptr %base, i64 %offset) {
      %gep = getelementptr inbounds i8, ptr %base, i64 %offset
      ret ptr %gep
    }
2023-07-14 16:14:50 +02:00
Nikita Popov
91b84811ab [InstSimplify] Add tests for recursive simplify with op replaced (NFC) 2023-07-14 16:06:34 +02:00
Nikita Popov
61e0822efa [llvm][clang] Remove uses of isOpaquePointerTy() (NFC)
This now always returns true (for pointer types).
2023-07-14 10:27:58 +02: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
db58a9c03f InstSimplify: Update another cannotBeOrderedLessThanZero use
Pass all the optional arguments to enable assumes.
2023-07-07 08:15:09 -04:00
Matt Arsenault
39f2fce0fa ValueTracking: Update another cannotBeOrderedLessThanZero use 2023-07-07 07:34:33 -04:00
Matt Arsenault
708fa7d926 ValueTracking: Update a use of cannotBeOrderedLessThanZero
Makes assumes work.
2023-07-07 07:33:48 -04:00
Nikita Popov
6c7fd723c4 [InstSimplify] Fold gep inbounds undef to undef instead of poison
With the semantics change from D154051, it is no longer valid to
fold gep inbounds undef to poison (unless we know the index is
non-zero). Fold it to undef instead.

Differential Revision: https://reviews.llvm.org/D154215
2023-07-06 14:59:22 +02:00
Hanbum Park
46a5745017 [InstSimplify] Fold icmp of allocas based on offset difference
Strengthen the fold for icmps of non-overlapping storage, by
working on the difference of offsets, rather than considering
both offsets independently. In particular, this allows handling
comparisons of pointers to the end of equal-sized allocations.

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

Differential Revision: https://reviews.llvm.org/D153752
2023-06-29 09:16:26 +02:00