Commit Graph

1390 Commits

Author SHA1 Message Date
Yingwei Zheng
1fea712cd1 [ValueTracking] Infer X u<= X +nuw Y for any Y (#75524)
Alive2: https://alive2.llvm.org/ce/z/kiGxCf
Fixes #70374.
2023-12-15 16:33:39 +08:00
Nikita Popov
7686d49517 [ValueTracking] Handle returned attribute with mismatched type
The returned attribute can be used when it is possible to
"losslessly bitcast" between the argument and return type,
including between two vector types.

computeKnownBits() would crash in this case, isKnownNonZero()
would potentially produce a miscompile.

Fixes https://github.com/llvm/llvm-project/issues/74722.
2023-12-08 17:05:13 +01:00
Mikhail Goncharov
0d0c229855 Revert "Reapply "ValueTracking: Identify implied fp classes by general fcmp (#66505)""
This reverts commit d55692d60d.

See discussion in #66505: assertion fires in OSS build of TensorFlow.
2023-12-05 11:10:24 +01:00
Nikita Popov
460faa0c87 [InstSimplify] Check common operand with constant earlier
If both icmps have the same operands and the RHS is constant, we
would currently go into the isImpliedCondMatchingOperands() code
path, instead of the isImpliedCondCommonOperandWithConstants()
path. Both are correct, but the latter can produce more accurate
results if the implication is dependent on the sign.
2023-12-01 12:18:59 +01:00
Nikita Popov
89b0044ca9 [InstSimplify] Add test for implied cond with equal ops and constant (NFC) 2023-12-01 12:18:27 +01:00
Nikita Popov
cd31cf5989 [InstSimplify] Fix or disjoint miscompile with op replacement
Make sure %x does not get folded to "or disjoint %x, %x" without
dropping the flag, as this would be a derefinement.
2023-12-01 11:45:09 +01:00
Nikita Popov
5a1020bb00 [InstSimplify] Add test for disjoint or miscompile (NFC)
The absorption case is already handled correctly, but the
idempentence case is not.
2023-12-01 11:45:09 +01:00
Matt Arsenault
d55692d60d Reapply "ValueTracking: Identify implied fp classes by general fcmp (#66505)"
This reverts commit 96a0d714d5.

Avoid assert with dynamic denormal-fp-math We don't recognize compares
with 0 as an exact class test if we don't know the denormal mode. We could
try to do better here, but it's probably not worth it.

Fixes asserts reported after 1adce7d8e47e2438f99f91607760b825e5e3cc37
2023-12-01 17:51:46 +09:00
Nikita Popov
ea602cb806 [IR] Support or disjoint in hasPoisonGeneratingFlags()
This fixed incorrect removal of freeze instructions.
2023-11-30 17:26:23 +01:00
Nikita Popov
ca5a01d8e4 [InstSimplify] Add test for incorrect freeze of or disjoint (NFC) 2023-11-30 17:26:23 +01:00
Nikita Popov
07c18a05e2 [InstSimplify] Fix select bit test miscompile with disjoint
The select condition ensures the disjointness here. The transform
is not valid without dropping the flag, which InstSimplify can't
do.
2023-11-30 16:55:32 +01:00
Nikita Popov
c89553ae82 [InstSimplify] Add test for or disjoint miscompile (NFC) 2023-11-30 16:55:32 +01:00
Graham Hunter
4028dd2e93 [InstSimplify] Fold converted urem to 0 if there's no overlapping bits (#71528)
When folding urem instructions we can end up not recognizing that
the output will always be 0 due to Value*s being different, despite
generating the same data (in this case, 2 different calls to vscale).

This patch recognizes the (x << N) & (add (x << M), -1) pattern that
instcombine replaces urem with after the two vscale calls have been
reduced to one via CSE, then replaces with 0 when x is a power of 2
and N >= M.
2023-11-20 10:27:16 +00:00
Nikita Popov
56c1d30183 [IR] Remove support for lshr/ashr constant expressions (#71955)
Remove support for the lshr and ashr constant expressions. All places
creating them have been removed beforehand, so this just removes the
APIs and uses of these constant expressions in tests.

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
2023-11-14 09:25:14 +01:00
Matt Arsenault
0e1a52f556 ValueTracking: Handle compare gt to -inf in class identification (#72086)
This apparently shows up somewhere in chromium. We also are missing a
canonicalization to an equality compare with inf.
2023-11-14 10:05:38 +09:00
Matt Arsenault
d4912e8050 ValueTracking: Add some tests to cover asserts in fcmpImpliesClass
Catch asserts hit after 1adce7d8e47e2438f99f91607760b825e5e3cc37
2023-11-13 15:05:52 +09:00
Hans Wennborg
96a0d714d5 Revert "ValueTracking: Identify implied fp classes by general fcmp (#66505)"
This causes asserts to fire:

  llvm/lib/Analysis/ValueTracking.cpp:4262:
  std::tuple<Value *, FPClassTest, FPClassTest> llvm::fcmpImpliesClass(CmpInst::Predicate, const Function &, Value *, const APFloat *, bool):
  Assertion `(RHSClass == fcPosNormal || RHSClass == fcNegNormal || RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal) && "should have been recognized as an exact class test"' failed.

See comments on the PR.

> Previously we could recognize exact class tests performed by
> an fcmp with special values (0s, infs and smallest normal).
> Expand this to recognize the implied classes by a compare with a general
> constant. e.g. fcmp ogt x, 1 implies positive and non-0.
>
> The API should be better merged with fcmpToClassTest but that
> made the diff way bigger, will try to do that in a future
> patch.

This reverts commit dc3faf0ed0.
2023-11-10 14:45:52 +01:00
Matt Arsenault
dc3faf0ed0 ValueTracking: Identify implied fp classes by general fcmp (#66505)
Previously we could recognize exact class tests performed by
an fcmp with special values (0s, infs and smallest normal).
Expand this to recognize the implied classes by a compare with a general
constant. e.g. fcmp ogt x, 1 implies positive and non-0.
    
The API should be better merged with fcmpToClassTest but that
made the diff way bigger, will try to do that in a future
patch.
2023-11-10 11:39:19 +09:00
Graham Hunter
34f83e86b4 [InstSimplify] Precommit extra tests for PR71528 2023-11-08 17:02:10 +00:00
Graham Hunter
e397a465a8 [InstSimplify] Precommit test for PR71528 2023-11-08 12:17:09 +00:00
Nikita Popov
17764d2c87 [IR] Remove FP cast constant expressions (#71408)
Remove support for the fptrunc, fpext, fptoui, fptosi, uitofp and sitofp
constant expressions. All places creating them have been removed
beforehand, so this just removes the APIs and uses of these constant
expressions in tests.

With this, the only remaining FP operation that still has constant
expression support is fcmp.

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
2023-11-07 09:34:16 +01:00
Nikita Popov
05a4770647 [ConstantFold] Fix incorrect type assumptions
If a pointer isn't a constant expression, global or block address,
it's not guaranteed to be a null pointer. It can also be a no_cfi
or dso_local_equivalent constant.
2023-11-03 13:12:39 +01:00
Nikita Popov
e4a4122eb6 [IR] Remove zext and sext constant expressions (#71040)
Remove support for zext and sext constant expressions. All places
creating them have been removed beforehand, so this just removes the
APIs and uses of these constant expressions in tests.

There is some additional cleanup that can be done on top of this, e.g.
we can remove the ZExtInst vs ZExtOperator footgun.

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
2023-11-03 10:46:07 +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
Nikita Popov
2a1821766c [InstSimplify] Add additional test for #70335 (NFC)
Test the interaction with llvm.is.constant.
2023-11-02 16:15:23 +01:00
Nikita Popov
e331ca5076 [InstSimplify] Add extra tests for #70335 (NFC)
These were miscompiled with the initial version of the patch.
2023-11-02 11:41:47 +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
Noah Goldstein
a37861d707 [InstSimplify] Add tests for simplify llvm.ptrmask; NFC
Differential Revision: https://reviews.llvm.org/D156632
2023-11-01 23:50:35 -05:00
Nikita Popov
e46dd6fbc0 Revert "[InstCombine] Simplify and/or of icmp eq with op replacement (#70335)"
This reverts commit 1770a2e325.

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
ea1909f82c [InstSimplify] Add tests for #69050 and #69091 (NFC) 2023-10-27 09:50:35 +02:00
Alex Richardson
e39f6c1844 [opt] Infer DataLayout from triple if not specified
There are many tests that specify a target triple/CPU flags but no
DataLayout which can lead to IR being generated that has unusual
behaviour. This commit attempts to use the default DataLayout based
on the relevant flags if there is no explicit override on the command
line or in the IR file.

One thing that is not currently possible to differentiate from a missing
datalayout `target datalayout = ""` in the IR file since the current
APIs don't allow detecting this case. If it is considered useful to
support this case (instead of passing "-data-layout=" on the command
line), I can change IR parsers to track whether they have seen such a
directive and change the callback type.

Differential Revision: https://reviews.llvm.org/D141060
2023-10-26 12:07:37 -07:00
Noah Goldstein
444383e0d0 [ValueTracking] Do more thorough non-zero check in isKnownToBePowerOfTwo when OrZero is no set.
We can cover more cases by directly checking if the result is
known-nonzero for common patterns when they are missing `OrZero`.

This patch add `isKnownNonZero` checks for `shl`, `lshr`, `and`, and `mul`.

Differential Revision: https://reviews.llvm.org/D157309
2023-10-12 16:05:19 -05: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
Yingwei Zheng
9f2fc88b23 [ValueTracking] Simplify uaddo pattern (#65910)
This patch simplifies the overflow check of unsigned addition.

`a + b <u a` implies `a + b <u b`
`a + b >=u a` implies `a + b >=u b`

Alive2: https://alive2.llvm.org/ce/z/H8oK8n
Fixes #65863.
2023-09-29 01:59:17 +08:00
Yingwei Zheng
4ea883cbbb [InstCombine] Add pre-commit tests for PR66606. NFC. 2023-09-19 11:13:16 +08:00
Yingwei Zheng
2a38d83918 [InstSimplify] Add pre-commits for PR#66606. NFC. 2023-09-19 01:00:34 +08: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
Yingwei Zheng
a054d89cd4 [InstSimplify] Add signed version of pre-commit tests for PR65905. NFC. 2023-09-17 22:36:01 +08:00
Yingwei Zheng
2f45b56728 [InstSimplify] Add pre-commit tests for PR65905. NFC. 2023-09-17 20:40:19 +08:00
Yingwei Zheng
bce6c53464 [InstSimplify] Add pre-commit tests for PR65910. NFC. 2023-09-16 20:07:04 +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
1b235b087b ValueTracking: Add baseline tests for fcmp with non-0/inf constants
Currently assumes only do anything for fcmps that perform an exact
class test.
2023-09-11 20:03:03 +03: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
ee8d1d26e9 ConstantFolding: Handle exp10 intrinsic
https://reviews.llvm.org/D157892
2023-09-02 09:20:51 -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
Matt Arsenault
2e2b3678e5 InstSimplify: Baseline tests for exp10 handling 2023-09-01 20:18:52 -04:00
Philip Reames
294ad08ecc Revert "Revert "InstSimplify: Use correct interested FP classes when simplifying fcmp""
This reverts commit 89f0314ee1.  Change does not build.
2023-09-01 12:17:36 -07:00