2508 Commits

Author SHA1 Message Date
Alexey Bataev
85f529dda1 Revert "[SLP]Fix codegen of compares with consts, being trunced"
This reverts commit 16e0cc8308379857ecd69e6fe1aaf71e15b94910 to add
a new test case for the miscompile
2026-03-23 06:27:08 -07:00
Alexey Bataev
16e0cc8308 [SLP]Fix codegen of compares with consts, being trunced
If the const values have more active bits, than requested by the another
operand of the compare, such constants should not be trunced to avoid
miscompilation
2026-03-23 06:04:08 -07:00
Alexey Bataev
b2ba79578b [SLP]Fix patterns for compile time blow up with ordered reductions
Excluded patterns, leading to compile time blow up for integer ordered
reductions.
2026-03-22 13:42:54 -07:00
Alexey Bataev
88f830aed8 [SLP]Do not try to reduced instruction, marked for deletion in previous attempts
Need to skip instructions, which were vectorized and marked for deletion
to prevent a compiler crash
2026-03-22 10:10:48 -07:00
Alexey Bataev
616240369e [SLP]Do not consider copyable node with SplitVectorize parent
If the copyables are schedulable and the parent node is plit vectorize,
need to skip the scheduling analysis for such nodes to avoid a compiler
crash
2026-03-21 06:56:59 -07:00
Alexey Bataev
db143fb2b9 [SLP][NFC]Use block number instead of pointer for stable sorting, NFC 2026-03-21 04:30:32 -07:00
Alexey Bataev
b260861b38 [SLP]Update values after ordered vectorization
Need to update matching between the original reduced values and their
vectorized matches after ordered reduction vectorization to avoid
a compiler crash
2026-03-20 13:33:40 -07:00
Alexey Bataev
94e366ef20
[SLP] Initial support for ordered reductions
Patch models ordered reductions as a series of extractelements for the
cases which cannot be modeled as unordered reductions.

Fixes #50590

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/182644
2026-03-20 13:45:14 -04:00
Alexey Bataev
2bb0fa46a8
[SLP]Prefer copyable over alternate
If the instructions state is alternate and/or contains non-directly
matching instructions, need to check if it is better to represent such
operations as non-alternate with copyables.
To do this, we need to compare operands between the instructions in their
different representations and choose the best one for optimal
vectorization.

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/183777
2026-03-20 11:59:59 -04:00
Alexey Bataev
7d76a3122d
[SLP]Improve analysis for the shl-based reduced values with copyables (#185485)
shl-based reduced values in many cases serve as a bitcast/bswap-based
transfromation root, but need to improve analysis for better matching.
This patch merges reduction candidates into a single reduced value
array, if there are only 2 different candidate arrays, one of them has
only single element, the second is a list of shl instructions. Also,
sorts these shl instructions by their shift amount and merges with the
single candidate, if it is profitable to have a copyable reduction.
2026-03-19 14:16:53 -04:00
Alexey Bataev
9050794e06
[SLP]Improve reductions for copyables/split nodes
The original support for copyables leads to a regression in x264 in
RISCV, this patch improves detection of the copyable candidates by more
precise checking of the profitability and adds and extra check for
splitnode reduction, if it is profitable.

Fixes #184313

Reviewers: hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/185697
2026-03-19 12:03:05 -04:00
Alexey Bataev
582fa78753
[SLP]Do not match buildvector node, if current node is part of its combined nodes
If current buildvector node is part of the combined nodes of the
matching candidate node, this matching candidate must be considered as
non-matching to prevent wrong def-use chain

Reviewers: 

Pull Request: https://github.com/llvm/llvm-project/pull/187491
2026-03-19 08:15:32 -04:00
Alexey Bataev
abdcde9bbc
[SLP] Loop aware cost model/tree building
Currently, SLP vectorizer do not care about loops and their trip count.
It may lead to inefficient vectorization in some cases. Patch adds loop
nest-aware tree building and cost estimation.
When it comes to tree building, it now checks that tree do not span
across different loop nests. The nodes from other loop nests are
immediate buildvector nodes.
The cost model adds the knowledge about loop trip count. If it is
unknown, the default value is used, controlled by the
-slp-cost-loop-min-trip-count=<value> option. The cost of the vector
nodes in the loop is multiplied by the number of iteration (trip count),
because each vector node will be executed the trip count number of
times. This allows better cost estimation.

Original Reviewers:
jdenny-ornl, vporpo, hiraditya, RKSimon

Original PR: https://github.com/llvm/llvm-project/pull/150450

Recommit after revert in c7bd3062f1dac975cf9b706f457b3c55b4bf57ff and in 4e500bd0015042b0cd4b7c87b81caeea06072d24

Reviewers: 

Pull Request: https://github.com/llvm/llvm-project/pull/187391
2026-03-18 17:54:01 -04:00
Alexey Bataev
4e500bd001 Revert "[SLP] Loop aware cost model/tree building"
This reverts commit 6261cb4487f153c599a040d7a77524561b520240 to try to
fix compile time regressions
2026-03-18 09:46:39 -07:00
Alexey Bataev
6261cb4487 [SLP] Loop aware cost model/tree building
Currently, SLP vectorizer do not care about loops and their trip count.
It may lead to inefficient vectorization in some cases. Patch adds loop
nest-aware tree building and cost estimation.
When it comes to tree building, it now checks that tree do not span
across different loop nests. The nodes from other loop nests are
immediate buildvector nodes.
The cost model adds the knowledge about loop trip count. If it is
unknown, the default value is used, controlled by the
-slp-cost-loop-min-trip-count=<value> option. The cost of the vector
nodes in the loop is multiplied by the number of iteration (trip count),
because each vector node will be executed the trip count number of
times. This allows better cost estimation.

Reviewers: jdenny-ornl, vporpo, hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/150450

Recommit after revert in c7bd3062f1dac975cf9b706f457b3c55b4bf57ff
2026-03-18 07:33:07 -07:00
Ryan Buchner
af67e30a63
[SLP][NFC] Refactor BinOpSameOpcodeHelper BIT enum (#187067)
More readable syntax and increase type width to avoid silent errors if
we reach 17 members.
2026-03-17 12:38:14 -07:00
Alexis Engelke
43ec60eee5
Reland "[DomTree] Assert non-null block for pre-dom tree" (#187005)
Reland #186790 with fix for SCEV. A loop can have more than one latch,
in which case getLoopLatch returns null.
2026-03-17 14:10:04 +00:00
Alexey Bataev
d117f98ff6 [SLP]Fix legality checks for bswap-based transformations
Fix the checks for the non-power-of-2 base bswaps by checking the
power-of-2 of the source type, not the target scalar type. Plus, add
cost estimation for zext, if the source type does not match the scalar type and fixes final bitcasting for the reduced values.

Fixes https://github.com/llvm/llvm-project/pull/184018#issuecomment-4053477562
2026-03-16 11:56:24 -07:00
Alexis Engelke
e30aa40aa6
Revert "[DomTree] Assert non-null block for pre-dom tree" (#186831)
Reverts llvm/llvm-project#186790

Breaks buildbots, there are more SLPVectorizer problems.

https://lab.llvm.org/buildbot/#/builders/52/builds/15810
2026-03-16 17:29:35 +01:00
Alexey Bataev
61a9e30045 Revert "[SLP]Fix legality checks for bswap-based transformations"
This reverts commit 2d4daea3b66469420fc164e76c15558b34e44c75 to fix
a buildbot https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flab.llvm.org%2Fbuildbot%2F%23%2Fbuilders%2F164%2Fbuilds%2F19737&data=05%7C02%7C%7C672461616e0d4b66614208de8374a0ff%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C639092734113272365%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=%2B23yMlvZzYt7bB2gM6MmcEwPkIKQogXjcKYIZ%2Bz79zQ%3D&reserved=0
2026-03-16 09:01:49 -07:00
Alexey Bataev
2d4daea3b6 [SLP]Fix legality checks for bswap-based transformations
Fix the checks for the non-power-of-2 base bswaps by checking the
power-of-2 of the source type, not the target scalar type. Plus, add
cost estimation for zext, if the source type does not match the scalar type.

Fixes https://github.com/llvm/llvm-project/pull/184018#issuecomment-4053477562
2026-03-16 08:40:44 -07:00
Alexis Engelke
d4c22859db
[DomTree] Assert non-null block for pre-dom tree (#186790)
In a pre-dominator tree, blocks should never be null.
2026-03-16 16:07:49 +01:00
Alexey Bataev
50822d6b25 [SLP]Do not request the last instruction for first buildvector nodes with no state
If looking for the match of the gather/buildvector node and its root is
a first node, which also a buildvector/gather, and has no state, we
should skip the analysis for such nodes to prevent a compiler crash

Fixes #185851
2026-03-11 10:11:09 -07:00
Alexey Bataev
aa90add989 [SLP]Track vectorized values in reductions for correct handling between vectorization
Need to use WeakTrackingVH handler instead of the Value * to correctly
track modified/replaced vectorized instructions

Fixes https://github.com/llvm/llvm-project/pull/182760#issuecomment-4036706233
2026-03-11 06:05:08 -07:00
Alexey Bataev
c7bd3062f1 Revert "[SLP] Loop aware cost model/tree building"
This reverts commit 8963edb534e28d548d8381675bb18af1770c3041 to fix
miscompilations/compile time regressions, reported in https://github.com/llvm/llvm-project/pull/150450#issuecomment-4037224288, https://github.com/llvm/llvm-project/pull/150450#issuecomment-4037481719 and https://github.com/llvm/llvm-project/pull/150450#issuecomment-4038134121
2026-03-11 04:37:54 -07:00
Alexey Bataev
8963edb534
[SLP] Loop aware cost model/tree building
Currently, SLP vectorizer do not care about loops and their trip count.
It may lead to inefficient vectorization in some cases. Patch adds loop
nest-aware tree building and cost estimation.
When it comes to tree building, it now checks that tree do not span
across different loop nests. The nodes from other loop nests are
immediate buildvector nodes.
The cost model adds the knowledge about loop trip count. If it is
unknown, the default value is used, controlled by the
-slp-cost-loop-min-trip-count=<value> option. The cost of the vector
nodes in the loop is multiplied by the number of iteration (trip count),
because each vector node will be executed the trip count number of
times. This allows better cost estimation.

Reviewers: jdenny-ornl, vporpo, hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/150450
2026-03-10 16:14:57 -04:00
tudinhh
c192e8c9e3
[SLP] Fix misvectorization in commutative to non-commutative conversion (#185230)
**Summary**
Fixes a miscompilation where commutative operations (e.g., or, and, mul)
with a left-hand side constant were incorrectly transformed into
non-commutative operations (e.g., shl, sub).

**The Problem**
In `BinOpSameOpcodeHelper::getOperand`, when a constant is at `Pos ==
0`, the helper was failing to swap operand order for new non-commutative
target opcodes. This resulted in inverted logic, such as transforming
`or 0, %x` into `shl 0, %x` (resulting in 0) instead of the correct `%x
<< 0`.

**The Fix**
The existing logic only protected the Sub opcode. This patch generalizes
the fix to all non-commutative instructions by using
`!Instruction::isCommutative(ToOpcode)`. This ensures that for any
directional operation, the variable is correctly placed on the LHS and
the constant on the RHS.

**Changes**
SLPVectorizer.cpp: Replaced the specific Sub check with a general
isCommutative check.

Regression Test: Added lhs-constant-non-cummutative.ll to cover shl,
sub, and ashr targets.

Fixes #185186
2026-03-09 16:17:39 -04:00
Alexey Bataev
95919ecd57
[SLP]Allow bitcast/bswap based reductions for types, larger than the total strided size
Added support for zero extending the bitcasted/bswapped type to the
original type, if it is larger than the original scalar type

Reviewers: hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/184018
2026-03-08 10:37:09 -04:00
Alexey Bataev
e0e5000ea7
[SLP]Remove Alternate early profitability checks in favor of throttling
Removes early check, which may prevent some further optimizations, in
favor of tree throttling.

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/182760
2026-03-08 09:37:51 -04:00
Alexey Bataev
d8b718a3fa [SLP]Match the mask size, when copying mask for full match
Need to be careful, when filling the mask for fully matched nodes, the
masks may differ in sizes

Fixes a crash reported in test/Transforms/SLPVectorizer/X86/mask-size-less-common-mask.ll
2026-03-08 05:33:30 -07:00
Alexey Bataev
a96a0ded25 [SLP]Fix the matching of the nodes with the same scalars, but reused
If the scalars are reused and the ReuseShuffleIndices is set, we may
miss matching for the buildvector/gather nodes and add an extra cost
2026-03-07 10:29:34 -08:00
Alexey Bataev
2714583317 [SLP]Do not consider split vectorize nodes as vector phi nodes
Split vectorize nodes should not be considered as vector PHI nodes, when
trying to find the insertion point for the postpotned nodes.

Fixes #184585
2026-03-04 14:03:20 -08:00
Alexey Bataev
789bf51f0c [SLP]Do not consider condition with multiple uses and negate predicate as a candidate for inversed select
If the select/zext comparison has negate predicate and is used in
several places, it should not be considered as a candidate for inversed
zext/select pattern, it will be replaced by a negate vector predicate,
leading to an incorrect codegen for other uses
2026-03-01 12:01:19 -08:00
Alexey Bataev
9730d31284 [SLP]Fix types for reductions in revec
Need to consider vector inputs, when building casts for the reduced
values

Fixes #170828
2026-03-01 07:54:13 -08:00
Alexey Bataev
a6e7c38ea6 [SLP]Do not vectorize select nodes with scalar and vector conditions
If the select nodes contains selects with mixed scalar/vector
conditions, such nodes should not be revectorized.

Fixes #170836
2026-03-01 07:01:04 -08:00
Alexey Bataev
e317f42455 [SLP]Recalculate dependencies for the buildvector schedule node, if they have copyable node
Need to recalculate the deps for all buildvector nodes with copyable
deps to prevent a compiler crash during scheduling of instructions
2026-02-28 12:29:47 -08:00
Alexey Bataev
12e1075b64 [SLP]Fix operand reordering when estimating profitability of operands
Need to swap operand for a single instruction, not for the the same lane
of the first and second instruction in the list
2026-02-27 16:16:22 -08:00
Akash Dutta
cf28f23f10
[SLP] Reject duplicate shift amounts in matchesShlZExt reorder path (#183627)
In the reordered RHS path of matchesShlZExt, the code never checked that
each shift amount (0, Stride, 2×Stride, …) appears at most once. When
the same shift appeared in multiple lanes, it still filled Order,
producing a non-permutation (e.g. Order = [0,0,0,1]). That led to bad
shuffle masks and miscompilation (e.g. shuffles with poison).

The patch adds an explicit duplicate check: before setting Order[Idx] =
Pos, it ensures Pos has not been seen before, using a SmallBitVector
SeenPositions(VF). If a position is seen twice, the function returns
false and the optimization is not applied.
2026-02-27 13:00:58 -06:00
Alexey Bataev
c08079d8e7 [SLP]Add single-use check for the bitcasted reduction
If the reduced value, to be bitcasted, is used multiple times, it will
require emission of the extractelement instruction. Such nodes should
not be bitcasted, should be vectorized as vector instructions.

Fixes https://github.com/llvm/llvm-project/pull/181940#issuecomment-3950734168
2026-02-24 05:27:38 -08:00
Alexey Bataev
95a960daa0 [SLP]Do not convert inversed cmp nodes, if they reordered/reused
If the cmp node with inversed compares must be reordered/shuffled with
the reuses, disable transformation for such nodes for now, they require
some special processing.

Fixes https://github.com/llvm/llvm-project/pull/181580#issuecomment-3933026221
2026-02-20 06:04:51 -08:00
Alexey Bataev
29d4fea59b [SLP]Handle mixed select-to-bicasts and general reductions
If the reduction tree represents mixed select-to-bitcasts and general
reductions, need to handle them correctly to avoid a compiler crash

Fixes https://github.com/llvm/llvm-project/pull/181940#issuecomment-3929220929
2026-02-19 13:38:34 -08:00
Alexey Bataev
38d804725f [SLP]Do not mark for transforming to buildvector inversed compares
Inversed compares must remain vector nodes, they should be converted to
gathers to generate correct code.

Fixes issue reported in https://github.com/llvm/llvm-project/pull/181580#issuecomment-3926951332
2026-02-19 09:37:50 -08:00
Alexey Bataev
c6425aa9ae
[SLP]Support reduced or selects of bitmask as cmp bitcast
Converts reduced or(select %cmp, bitmask, 0) to zext(bitcast %vector_cmp to
i<num_reduced_values>) to in

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/181940
2026-02-18 18:01:42 -05:00
Alexey Bataev
a7c25ba33c [SLP][NFC]Fix reorered -> reordered 2026-02-18 13:54:39 -08:00
Ryan Buchner
a6e8de7407
[SLP][NFC] Fix MainOp/AltOp assertion to check the correct value (#182093)
Previously both assertions were checking MainOp.

Initial assertion added incorrectly in d41e517748e2d.
2026-02-18 13:08:10 -08:00
Alexey Bataev
a5aaa9dc63
[SLP]Convert compares from zexts, promoted to selects, to inversed op, if improves codegen
Some of the zext i1 (cmp) + select sequences can be transformed by
inverting compare predicates to remove extra shuffles, like
zext 1 (cmp ne) + select (cmp eq), 0, 2 can be modeled as select <2
x > (cmp ne), <1, 2>, zeroinitializer

Reviewers: RKSimon, hiraditya

Pull Request: https://github.com/llvm/llvm-project/pull/181580
2026-02-17 13:26:06 -05:00
Alexey Bataev
26f944bb50 [SLP]Fix an ArrayRef out-of-bounds access in slice
If the revec is enabled, may have the number of parts (registers) for
the combined node, not a single element node, so need to check for
potential out-of-bounds access

Fixes #181798
2026-02-17 10:00:13 -08:00
Alexey Bataev
ef52df4365
[SLP]Do not increase depth for type-changing nodes and NotProfitableForVectorization removal
The patch changes the maximum tree size analysis. 1. Do not increase
depth for type changing nodes (like casts and compares), allowing more
deeper trees to be built. 2. Removes NotProfitableForVectorization
workaround, not needed anymore after throttling enabled

Reviewers: hiraditya, RKSimon

Pull Request: https://github.com/llvm/llvm-project/pull/180950
2026-02-17 09:52:23 -05:00
Alexey Bataev
2ff4ec172a [SLP]Fix revec in split nodes
Initially split nodes do not support vector entries in revec mode, patch
fixes the issue by adding analysis for the scale factor

Fixes #181546
2026-02-16 14:09:28 -08:00
Alexey Bataev
7ec7907b80 [SLP] Fix a very long loads offset, being stored in DenseMap
Added a check for a very long offset to avoid a crash in the compiler

Fixes #181682
2026-02-16 11:07:22 -08:00