3476 Commits

Author SHA1 Message Date
Amaury Séchet
7e5681cf29 [DAG] Peek through ZEXT/TRUNC in foldAddSubMasked1
Fix a regression in D141883

Depends on D141883

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D141884
2023-01-19 13:23:42 +00:00
Amaury Séchet
2826869d7b [DAG] Do not combine any_ext when we combine and into zext.
This transofrm loses information that can be useful for other transforms.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D141883
2023-01-19 12:37:05 +00:00
Roman Lebedev
7460842fb2
[DAGCombiner] combineShuffleOfSplatVal(): don't assert that shuffle is non-undef
As per the test case from Steven Johnson in https://reviews.llvm.org/rGf8d9097168b7#1165311
we can indeed encounter such shuffles, that produce all-undef after folding,
before something else manages to optimize them away.
2023-01-18 18:45:08 +03:00
Haojian Wu
9936064d66 Remove an unused variable, NFC 2023-01-18 13:08:21 +01:00
Simon Pilgrim
73cdbbea02 [DAG] combineInsertEltToShuffle - split off mergeInsertEltWithShuffle fold. NFC.
combineInsertEltToShuffle was performing 2 very different folds in the same call, merging "(insert_vector_elt (vector_shuffle X, Y), (extract_vector_elt X, N), IdxC) --> (vector_shuffle X, Y)" and "(insert_vector_elt V, (bitcast X from vector type), IdxC) --> bitcast(shuffle (bitcast V), (extended X), Mask)"

The folds are currently still attempted in the same order as before (just as 2 seperate calls) so there should be no change in behaviour.

First step towards some adjustments to mergeInsertEltWithShuffle for D127115.
2023-01-18 11:56:36 +00:00
David Green
21df504399 [DAG][ARM][AArch64] Transform max(a,b) - min(a,b) -> abd(a,b)
This adds both signed and unsigned transforms for
max(a, b) - min(a, b) -> abd(a, b).

unsigned: https://alive2.llvm.org/ce/z/RF4jGQ
signed: https://alive2.llvm.org/ce/z/Cjr2zE

Fixes: #59894

Differential Revision: https://reviews.llvm.org/D141706
2023-01-18 11:44:26 +00:00
David Green
e26ec330c4 [DAG][AArch64][ARM] Combine abd(sub(x, y)) to abd if the sub is nsw
This implements the fold (abs (sub nsw x, y)) -> abds(x, y). Providing
the sub is nsw this appears to be valid without the extensions that are
usually used for abds. https://alive2.llvm.org/ce/z/XHVaB3. The
equivalent abdu combine seems to not be valid.

Differential Revision: https://reviews.llvm.org/D141665
2023-01-18 10:10:52 +00:00
Amaury Séchet
396ad408fd [DAG] Recombine (binop (shift x y))
This helps address regressions in D127115 .

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D141809
2023-01-16 02:20:29 +00:00
Roman Lebedev
f8d9097168
[DAGCombiner] combineShuffleOfSplatVal(): try to canonicalize to a splat shuffle
As noted in https://reviews.llvm.org/D141778#inline-1369900,
we fail to produce splat shuffles from certain sequences
of shuffles, that may have non-shuffles in the middle of seq.

There is a big pitfail to avoid here: just because `isSplatValue()`
says that all demanded elements are splat, we can't pick any random
one of them, because some of them could be undef! We must ignore those!
2023-01-15 21:11:33 +03:00
HanSheng Zhang
95414345d5
[DAGCombiner] visitFREEZE(): gracefully handle node invalidation
When we freeze operands of an operation that we are trying to freeze,
doing so may invalidate the original SDValue. We should just re-fetch
it from the ISD::FREEZE node, because if we bail, we'd hopefully just
revisit the node and do that again.

Fixes https://github.com/llvm/llvm-project/issues/59891

Differential Revision: https://reviews.llvm.org/D141256
2023-01-13 21:53:29 +03:00
Roman Lebedev
5ffa8c4019
[NFC][DAGCombiner] Fix typo in visitFREEZE() 2023-01-13 21:52:47 +03:00
Craig Topper
c8bd5343df [DAGCombiner][RISCV] Pre-promote (zext (abs X)) to (abs (sext X)) when X has an illegal type.
Type legalization will insert a sign extend anyway. By doing it
early we can remove the zext. ComputeNumSignBits can't spot it
after type legalization because type legalization may expand
the abs to sra+xor+sub.

If the zext result type is larger than the type to be promoted to,
we'll promote to a legal type and then zext the rest of the way.
If the legal type is larger than the destination type we can promote
and then truncate.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D140509
2023-01-13 10:40:25 -08:00
Guillaume Chatelet
8fd5558b29 [NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:49:38 +00:00
Guillaume Chatelet
48f5d77eee [NFC] Use TypeSize::getKnownMinValue() instead of TypeSize::getKnownMinSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:36:39 +00:00
Amaury Séchet
c0d7f0ca00 [DAGCombine] fold (sext (sext_inreg x)) -> (sext (trunc x))
This fixes a regression introduced by D127115 in test/CodeGen/PowerPC/store-forward-be64.ll

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D140993
2023-01-10 20:33:02 +00:00
chenglin.bi
be7d0d6db5 [DAGCombiner] Fix issue with rot chain pattern
faa35fc87370 fix the case of negative input shift. But when `c1`, `c2` is not the same side, it will also cause negative shift amount.
And that negative shift amount can't normalize by urem. So add one more bit size to normalize the last shift amount.

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

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D141363
2023-01-11 01:29:09 +08:00
serge-sans-paille
38818b60c5
Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ part
Use deduction guides instead of helper functions.

The only non-automatic changes have been:

1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*))
2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase.
3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated.
4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that).

Per reviewers' comment, some useless makeArrayRef have been removed in the process.

This is a follow-up to https://reviews.llvm.org/D140896 that introduced
the deduction guides.

Differential Revision: https://reviews.llvm.org/D140955
2023-01-05 14:11:08 +01:00
Roman Lebedev
2a43a4478c
[NFCI][DAGCombiner] foldExtendVectorInregToExtendOfSubvector(): just build new VT
Changing element type seems to not play well with non-simple types,
even though we are operating on EVT's here.
2023-01-05 01:33:24 +03:00
Roman Lebedev
41005b7ab2
[DAGCombiner] Do try to combine ISD::ANY_EXTEND_VECTOR_INREG nodes
These weren't previously getting combined at all here,
only in target-specific combines.
2023-01-05 01:12:31 +03:00
Roman Lebedev
317a1adfe4
[DAGCombiner] Fold *_EXTEND_INREG of one of CONCAT_VECTORS operands into *_EXTEND of operand
This appears to be the root problematic pattern
for AArch64 regression in D140677.

We already do this, and many more, as target-specific X86 combines,
so this isn't causing much of an impact.
2023-01-05 01:12:31 +03:00
Roman Lebedev
846d06c707
[DAG] tryToFoldExtendOfConstant(): sext undef is not undef
https://alive2.llvm.org/ce/z/cLGpWV, but https://alive2.llvm.org/ce/z/TGNH4P
2023-01-04 22:42:43 +03:00
Roman Lebedev
4fc417ec37
[DAGCombiner] convertBuildVecZextToBuildVecWithZeros(): rework split factor calculation
The original computation was both making assumptions that do not hold
in practice, and being overly pessimistic. We should just check
every possible split factor, and pick the best one.

Fixes https://github.com/llvm/llvm-project/issues/59781
2023-01-02 18:34:35 +03:00
Roman Lebedev
1337821f11
[DAGCombiner][X86] Fold a CONCAT_VECTORS of SHUFFLE_VECTOR and it's operand into wider SHUFFLE_VECTOR
This was showing as a source of *many* regressions
with more aggressive ZERO_EXTEND_VECTOR_INREG recognition.
2023-01-01 23:18:42 +03:00
Roman Lebedev
e4d25a9c23
[DAG] BUILD_VECTOR: absorb ZERO_EXTEND of a single first operand if all other ops are zeros
This kind of pattern seems to come up as regressions
with better ZERO_EXTEND_VECTOR_INREG recognition.

For initial implementation, this is quite restricted
to the minimal viable transform, otherwise there are
too many regressions to be dealt with.
2022-12-31 00:58:11 +03:00
Dmitry Borisenkov
0ec51a460a DAG: Prevent store value forwarding to distinct addrspace load
DAGCombiner replaces (load const_addr1) directly chained with (store
(val, const_addr2)) with val if address space stripped const_addr1 ==
const_addr2. The patch fixes the issue by checking address spaces as
well.  However, it might makes sense to not to chain together side
effects that belong to different address spaces in the first place and
make SelectionDAG::root address space aware.
2022-12-29 18:19:55 -05:00
Roman Lebedev
248567a327
[DAGCombiner] Try to partition ISD::EXTRACT_VECTOR_ELT to accomodate it's ISD::BUILD_VECTOR users
This mainly cleans up a few patterns that are legalized by scalarization
from a wide-element vector, but then are further split apart to build
a more narrow-sized-element vector. In particular this happens in some
cases for illegal ISD::ZERO_EXTEND_VECTOR_INREG.

Given a ISD::EXTRACT_VECTOR_ELT, which is a glorified bit sequence extract,
recursively analyse all of it's users. and try to model themselves as
bit sequence extractions. If all of them agree on the new, narrower element
type, and all of them can be modelled as ISD::EXTRACT_VECTOR_ELT's of that
new element type, do that, but only if unmodelled users are ISD::BUILD_VECTOR.
2022-12-30 01:15:53 +03:00
Roman Lebedev
c4f815d705
[DAGCombine] combineShuffleToZeroExtendVectorInReg(): widen shuffle elements before trying to match
We might have sunk a bitcast into shuffle, and now it might be operating
on more fine-grained elements than what we'd match, so we must not be
dependent on whatever the granularity the shuffle happened to be in,
but transform it into the one canonical for us - with widest elements.
2022-12-27 00:47:45 +03:00
Roman Lebedev
e26e7ed69a
[DAG] combineShuffleToZeroExtendVectorInReg(): try to match w/ commuted operands
We don't have any reason to expect that the operand we will match
is on any particular hand of the shuffle, so we should try both.
2022-12-26 22:54:03 +03:00
Roman Lebedev
62fc5f1640
[DAGCombiner] Add a most basic combineShuffleToZeroExtendVectorInReg()
Sometimes we end up with a shuffles in DAG that would be
better represented as a `ISD::ZERO_EXTEND_VECTOR_INREG`,
and a failure to do so causes suboptimal codegen in a number of cases,
especially when we will then cast vector to scalar.

I acknowledge, the test changes here are rather underwhelming,
but as with all of codegen, it's always a yak shawing,
and this is the most stripped down version of the patch
that shows *some* effect without having insurmountable amount
of fallout to deal with. The next change resolves this regression.

The transformation will be extended in follow-ups.
2022-12-26 22:54:03 +03:00
Roman Lebedev
2f6aef52f2
[NFC][DAGCombiner] canCombineShuffleToAnyExtendVectorInreg(): take matcher as callback 2022-12-26 03:56:58 +03:00
Roman Lebedev
84ea72679e
[NFC][DAG] canCombineShuffleToAnyExtendVectorInreg(): check for legal op before matching
Likewise as with legal types check, might as well not match if won't use.
2022-12-26 01:43:49 +03:00
Roman Lebedev
2999c45050
[NFC][DAGCombiner] Extract canCombineShuffleToAnyVectorExtendInReg() helper
Adding zero-ext support isn't as straight-forward, and it's easier
to to so in a new function, but this helper is useful there.
This does not change any existing behaviour.
2022-12-26 01:04:47 +03:00
Roman Lebedev
6aa7359387
[NFC][DAG] combineShuffleToVectorExtend(): check that the type is legal first
There is no point in doing any of the potentially-costly matching
if we will inevitably give up anyway.
2022-12-26 01:03:59 +03:00
Roman Lebedev
03e848293e
[DAGCombiner] visitFREEZE(): fix cycle breaking
Depending on the particular DAG, we might either create a `freeze`,
or not. And only in the former case, the cycle would be formed.
It would be nicer to have `ReplaceAllUsesOfValueWithIf()`,
like we have in IR, but we don't have that.

Fixes https://github.com/llvm/llvm-project/issues/59677
2022-12-23 18:16:22 +03:00
Roman Lebedev
d8f541efe7
[DAGCombiner] visitFREEZE(): fix handling of no maybe-poison ops
The original code was confusing. It was stripping poison-generating flags,
but the comments were saying that doing so was a TODO.

If the poison-generating flags are present, then even if all operands
are guaranteed not to be undef or poison, the whole operation may still
produce undef or poison. We can still deal with that case,
and we already do deal with it in fact, by also dropping those flags.

Refs. https://github.com/llvm/llvm-project/issues/59676
2022-12-23 17:26:05 +03:00
Roman Lebedev
d7a63a0421
[DAGCombiner] visitFREEZE(): restore previous behaviour on no maybe-poison operands
Lack of such operands implies that the op might be poison-producing due to
it's flags. We seem to drop them already, but the comments are confusing.

Fixes https://github.com/llvm/llvm-project/issues/59676
2022-12-23 17:26:05 +03:00
Roman Lebedev
6fea27662d
[DAGCombiner] visitFREEZE(): be less greedy with replacing other uses of undef 2022-12-23 02:26:36 +03:00
Roman Lebedev
f738ab9075
[DAGCombiner] visitFREEZE(): allow multiple maybe-poison operands for BUILD_VECTOR 2022-12-23 02:26:36 +03:00
Roman Lebedev
114cc45a09
[NFC][DAGCombiner] visitFREEZE(): use early return 2022-12-23 02:26:36 +03:00
Roman Lebedev
f5700e7b69
[DAGCombine][X86] Pull one-use freeze out of extract_vector_elt vector operand
This may allow us to further simplify the vector,
and freezing the extracted result is still fine:
```
----------------------------------------
define i8 @src(<2 x i8> %src, i64 %idx) {
%0:
  %i1 = freeze <2 x i8> %src
  %i2 = extractelement <2 x i8> %i1, i64 %idx
  ret i8 %i2
}
=>
define i8 @tgt(<2 x i8> %src, i64 %idx) {
%0:
  %i1 = extractelement <2 x i8> %src, i64 %idx
  %i2 = freeze i8 %i1
  ret i8 %i2
}
Transformation seems to be correct!
```

BUT, there must not be other uses of that freeze,
see `@freeze_extractelement_extra_use`.

Also, looks like we are missing some ISEL-level handling for freeze.
2022-12-23 00:03:26 +03:00
Roman Lebedev
23bc8f730d
[DAGCombiner] visitFREEZE(): allow, and update, other uses of maybe-poison operand 2022-12-22 23:23:19 +03:00
Craig Topper
3f811b26ef [DAGCombine] Fix mistake in a comment. NFC 2022-12-21 00:28:07 -08:00
Fangrui Song
036e092282 [CodeGen] std::optional::value => operator*/operator->
value() has undesired exception checking semantics and calls
__throw_bad_optional_access in libc++. Moreover, the API is unavailable without
_LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see
_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).

This fixes LLVMMIRParser, LLVMGlobalISel, LLVMAsmPrinter, LLVMSelectionDAG.
2022-12-16 23:41:36 +00:00
Juan Manuel MARTINEZ CAAMAÑO
4d852374b1 [DAGCombine] Fix always true condition in combineShiftToMULH
Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D139550
2022-12-15 13:04:42 +01:00
Kazu Hirata
6eb0b0a045 Don't include Optional.h
These files no longer use llvm::Optional.
2022-12-14 21:16:22 -08:00
Fangrui Song
67819a72c6 [CodeGen] llvm::Optional => std::optional 2022-12-13 09:06:36 +00:00
David Green
fd716925ec [DAGCombine] Fold Splat(bitcast(buildvector(x,..))) to splat(x)
This adds a fold which teaches the backend to fold
splat(bitcast(buildvector(x,..))) or
splat(bitcast(scalar_to_vector(x))) to a single splat.

This only handles lane 0 splats, which are only valid under LE, and
needs to be a little careful with the types it creates for the new
buildvector.

Differential Revision: https://reviews.llvm.org/D139611
2022-12-12 08:35:43 +00:00
jacquesguan
c2f199fa48 [DAGCombiner] Scalarize extend/truncate for splat vector.
This revision scalarizes extend/truncate for splat vector.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D122875
2022-12-12 14:53:10 +08:00
Xiang1 Zhang
7557d94bd8 [NFC] Update comment for TRUNC followed by a masked store 2022-12-12 11:24:57 +08:00
Xiang1 Zhang
9c88ccf9a9 [DAG] Stop combine for masked compressstore
Reviewed By: WangPengfei

Differential Revision: https://reviews.llvm.org/D139682
2022-12-12 10:40:20 +08:00