2856 Commits

Author SHA1 Message Date
Iasonaskrpr
b44434474e
Improved ISD::SRL handling in isKnownToBeAPowerOfTwo (#182562)
Fixes #181651

Added DemandedElts argument to isConstOrConstSplat and to
isKnowTobePowerOfTwo calls and OrZero || isKnownNeverZero(Val, Depth) is
checked before isKnowTobePowerOfTwo. Also added unit tests.
2026-03-14 18:49:08 +00:00
paperchalice
a266f60ddf
[SelectionDAG] Remove NoNaNsFPMath uses (#183448)
This pr removes the rest uses in LLVMCodeGen.
2026-03-10 14:06:42 +08:00
Arjun Parmar
2bf2115471
[DAG] isKnownToBeAPowerOfTwo - add ISD::VECTOR_SHUFFLE handling (#185203)
closes #183350
2026-03-08 12:43:16 +00:00
Ayush Kumar Gaur
c56aad6b4f
[DAG] isKnownNeverZero: Add DemandedElts handling for ROTL/ROTR/BITREVERSE/BSWAP/CTPOP/ABS (#184033)
Add DemandedElts handling for ROTL/ROTR/BITREVERSE/BSWAP/CTPOP/ABS in SelectionDAG::isKnownNeverZero

Fixes #183040
2026-03-07 12:21:30 +00:00
Joeljm1
ec9e16444e
[DAG] isKnownNeverZero - add ISD::SHL DemandedElts handling and tests (#183772)
Added DemandedElts to ISD::SHL case in isKnowNeverZero and required tests

Fixes #183044
2026-03-06 17:46:45 +00:00
Weiwen He
cd62289831
[DAG] isKnownNeverZero - add ISD::ADD/SUB DemandedElts handling and tests (#183958)
Closes #183043
2026-03-04 23:03:14 +00:00
Manuel Dun
5c08616df7
[DAG] isKnownToBeAPowerOfTwo - Power of 2 value is known to be power of 2 after BSWAP/BITREVERSE (#182207)
This is my first pr specifically for llvm
Fixes #181657
Allows the isKnownToBeAPowerOfTwo function to handle BSWAP and
BITREVERSE dag operations.
2026-03-04 20:20:53 +00:00
Mir Immad
b0b583475a
[DAG] Improved handling of ISD::ROTL and ISD::ROTR in isKnownToBeAPowerOfTwo (#182744)
Fixes #181642
2026-03-04 16:43:22 +00:00
zhijian lin
c5039c1848
[NFC] Refactor the SelectionDAG::getMemcmp etc with a existing helper function getRuntimeCallSDValueHelper (#184200)
Refactor the SelectionDAG::getMemcmp,
SelectionDAG::getStrcpy,SelectionDAG::getStrlen with a existing helper
function SelectionDAG::getRuntimeCallSDValueHelper.
2026-03-03 13:58:57 -05:00
Lewis Crawford
fa6eef8378
Revert "Avoid maxnum(sNaN, x) optimizations / folds (#170181)" (#184125)
This reverts commit ea3fdc5972db7f2d459e543307af05c357f2be26.

Re-enable const-folding for maxnum/minnum in the middle-end, GlobalISel,
and SelectionDAG.

Re-enable optimizations that depend on maxnum/minnum sNaN semantics in
InstCombine and DAGCombiner.

Now that maxnum(x, sNaN) is specified to non-deterministically produce
either NaN or x, these constant-foldings and optimizations are now valid
again according to the newly clarified semantics in #172012 .
2026-03-03 12:45:26 +00:00
Shekhar
703649554d
[DAG] isKnownNeverZero - add ISD::OR DemandedElts handling (#183228)
This patch updates `SelectionDAG::isKnownNeverZero` to support `ISD::OR`
by forwarding the `DemandedElts` mask to its operands.

Previously, `ISD::OR` dropped the mask, causing the compiler to be
overly conservative if any lane in the vector was zero, even if that
lane wasn't demanded. This change allows the compiler to prove a vector
result is non-zero even if ignored lanes are zero.

Fixes #183037

**Tests:**

- Moved tests from the C++ file to the IR assembly file
(`known-never-zero.ll`) as requested.
- Confirmed the code now correctly tracks which parts of a vector are
actually needed for `ISD::OR`.
- This allows the compiler to prove a result is "never zero" even if
some unused lanes contain zeros.
2026-03-03 12:24:58 +00:00
Shekhar
03a9ebc897
[DAG] isKnownNeverZero - add ISD::UADDSAT/UMAX/UMIN DemandedElts handling and tests (#183992)
Fixes #183038

Adds `isKnownNeverZero` support for `UADDSAT`, `UMAX`, and `UMIN`. This
allows the compiler to prove a vector result is _non-zero_ by analyzing
only the demanded lanes of its operands.
2026-03-03 10:36:02 +00:00
tudinhh
24873cb955
[SelectionDAG] Pass DemandedElts to isKnownNeverZero for extend nodes (#183624)
Fixes #183042

This patch updates `SelectionDAG::isKnownNeverZero` to forward the
`DemandedElts` mask when analyzing `ISD::ZERO_EXTEND` and
`ISD::SIGN_EXTEND` nodes.
2026-03-02 19:11:49 +00:00
Pranshu Goyal
fb6038d937
[DAG] isKnownNeverZero - add ISD::SRA/SRL DemandedElts handling and tests (#183577)
This patch updates `SelectionDAG::isKnownNeverZero` to support
`ISD::SRA`/`ISD::SRL` by forwarding the `DemandedElts` mask to its
operands.

Fixes #183046
2026-03-02 18:50:30 +00:00
Aaron Smull
730587d3be
[DAG] isKnownNeverZero - add DemandedElts for ISD::SMIN/SMAX (#184054)
Resolves #183039
2026-03-02 10:32:12 +00:00
Simon Pilgrim
48209b6777
[DAG] isKnownToBeAPowerOfTwo - add ISD::EXTRACT_VECTOR_ELT handling (#183924)
Initialize DemandedElts mask when the index is constant and inbounds, otherwise check all elements.
2026-03-01 18:21:36 +00:00
Simon Pilgrim
b72d8ac98c
[DAG] isKnownNeverZero - add ISD::EXTRACT_VECTOR_ELT handling (#183961)
Initialize DemandedElts mask when the index is constant and inbounds, otherwise check all elements.
2026-02-28 22:52:04 +00:00
zhijian lin
da851db4bb
[PowerPC] using milicode call for memccpy instead of lib call (#182563)
AIX has "millicode" routines, which are functions loaded at boot time
into fixed addresses in kernel memory. This allows them to be customized
for the processor. The __memccpy routine is a millicode implementation;
we use millicode for the memccpy function instead of a library call to
improve performance

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2026-02-26 13:09:22 -05:00
Abhijeet
327f060535
[DAG] Fix OrZero in isKnownToBeAPowerOfTwo ISD::AND (#182934)
Fixes #181653
2026-02-26 13:21:40 +00:00
Aadarsh Keshri
6d7ec4b7c3
[DAG] Improved ISD::SHL handling in isKnownToBeAPowerOfTwo (#181882)
Fixes  #181650
2026-02-26 10:10:56 +00:00
Paul Walker
ab360b1e7e
[LLVM][TTI] Remove the isVScaleKnownToBeAPowerOfTwo hook. (#183292)
After https://github.com/llvm/llvm-project/pull/183080 this is no longer
a configurable property.

NOTE: No test changes expected beyond
llvm/test/Transforms/LoopVectorize/scalable-predication.ll which has
been removed because it only existed to verfiy the now unsupported
functionality.
2026-02-25 14:09:52 +00:00
Harrishan Raveendran
9a91c50a4c
[DAG] isKnownNeverZero - add DemandedElts argument (#182679)
Following changes were made for isKnownNeverZero :
- Added BUILDVECTOR and SPLATVECTOR cases.
- Added support for DemandedElts arguments for SELECT/VSELECT cases.  
- Added tests for constants and SELECT/VSELECT.

Closes #181656
2026-02-23 18:21:29 +00:00
Fedor Nikolaev
900778e40d
[DAG] isKnownToBeAPowerOfTwo - add DemandedElts + OrZero handling to ISD::SELECT/VSELECT cases (#181753)
Pass DemandedElts and OrZero arguments through SELECT/VSELECT nodes when
checking if values are power-of-two.

Add unit tests for scalar SELECT (OrZero) and vector VSELECT
(DemandedElts + OrZero).

Resolves #181645
2026-02-17 13:05:28 +00:00
Jie Fu
7806a0b699 [CodeGen] Silence a warning (NFC)
llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:4665:12:
 error: unused variable 'NumElts' [-Werror,-Wunused-variable]
  unsigned NumElts = DemandedElts.getBitWidth();
           ^
1 error generated.
2026-02-17 11:36:21 +08:00
Simon Pilgrim
20aff2085c
[DAG] isKnownToBeAPowerOfTwo - use switch() to match against each opcode. NFC. (#181637)
isKnownToBeAPowerOfTwo has gotten to a size now that we should use a general switch like other value tracking helpers.
2026-02-16 12:01:17 +00:00
Simon Pilgrim
afd4df07ab
[DAG] isKnownToBeAPowerOfTwo - add DemandedElts, OrZero arguments and support for zero constants (#181485)
Update the signature for isKnownToBeAPowerOfTwo methods to take
DemandedElts and OrZero arguments.

So far all I've done is add OrZero support for constants
(scalars/buildvector/splat), with the intention to incrementally extend
support in the future (or spread the work as beginner GFI patches).

Similarly the DemandedElts argument is currently only used for constant
build vector tests but support can be extended in future patches.

Fixes #178938
2026-02-15 11:45:22 +00:00
Björn Pettersson
6420099bcc
[SelectionDAG] Make sure demanded lanes for AND/MUL-by-zero are frozen (#180727)
DAGCombiner can fold a chain of INSERT_VECTOR_ELT into a vector AND/OR
operation. This patch adds protection to avoid that we end up making the
vector more poisonous by freezing the source vector when the elements
that should be set to 0/-1 may be poison in the source vector.

The patch also fixes a bug in SimplifyDemandedVectorElts for
MUL/MULHU/MULHS/AND that could result in making the vector more
poisonous. Problem was that we skipped demanding elements from Op0 that
were known to be zero in Op1. But that could result in elements being
simplified into poison when simplifying Op0, and then the result would
be poison and not zero after the MUL/MULHU/MULHS/AND. The solution is to
defensively make sure that we demand all the elements originally
demanded also when simplifying Op0.

This bugs were found when analysing the miscompiles in
https://github.com/llvm/llvm-project/issues/179448

Main culprit in #179448 seems to have been the bug in DAGCombiner. The
bug in SimplifyDemandedVectorElts surfaced when fixing the DAGCombiner,
as that fix typically introduce the (AND (FREEZE x), y) pattern that
wasn't handled correctly in SimplifyDemandedVectorElts.

Also fixes #180409.
Also fixes #176682.
2026-02-12 10:58:29 +01:00
paperchalice
c53acf0443
[SelectionDAGBuilder] Remove NoNaNsFPMath uses (#169904)
Replaced by checking fast-math flags or value tracking results.
2026-02-09 09:48:07 +08:00
Nicolai Hähnle
af836ff60c
[CodeGen] Add getTgtMemIntrinsic overload for multiple memory operands (NFC) (#175843)
There are target intrinsics that logically require two MMOs, such as
llvm.amdgcn.global.load.lds, which is a copy from global memory to LDS,
so there's both a load and a store to different addresses.

Add an overload of getTgtMemIntrinsic that produces intrinsic info in a
vector, and implement it in terms of the existing (now protected)
overload.

GlobalISel and SelectionDAG paths are updated to support multiple MMOs.
The main part of this change is supporting multiple MMOs in
MemIntrinsicNodes.

Converting the backends to using the new overload is a fairly mechanical step
that is done in a separate change in the hope that that allows reducing merging
pains during review and for downstreams. A later change will then enable
using multiple MMOs in AMDGPU.
2026-02-02 21:58:42 +00:00
Philip Ginsbach-Chen
5d5b4aaa0e
[SelectionDAG][NFC] Rename isConstantSequence to isArithmeticSequence (#179108)
The previous name was misleading: the method checks for an arithmetic
progression `(start, start+stride, start+2*stride, ...)`, not just any
constant sequence. The new name uses precise mathematical terminology.

https://github.com/llvm/llvm-project/pull/176671#discussion_r2735571479
2026-02-02 17:19:57 +00:00
zhijian lin
dc520ea4af
[PowerPC] using milicode call for strcmp instead of lib call (#177009)
1. AIX has "millicode" routines, which are functions loaded at boot time
into fixed addresses in kernel memory. This allows them to be customized
for the processor. The __strcmp routine is a millicode implementation;
we use millicode for the strcmp function instead of a library call to
improve performance.
2026-02-02 09:34:53 -05:00
Simon Pilgrim
a372152cb5
[DAG] visitVECTOR_SHUFFLE - ensure correct resno when folding shuffle(bop(shuffle(x,y),shuffle(z,w)) (#179124)
TLI.isBinOp recognises some opcodes that have multiple results,
including UADDO etc.

In most cases we currently just bail if a binop has multiple results,
but shuffle combining was missing the check and its pretty trivial to
add handling in this case.

I've added add/sub-overflow opcodes to verifyNode to help catch these
cases in the future - IIRC there was a plan to autogen these, but there
isn't anything at the moment.

Fixes #179112
2026-02-02 09:22:48 +00:00
Benjamin Maxwell
1818b23a99
[SDAG] Check for nsz in DAG.canIgnoreSignBitOfZero() (#178905)
Follow up to #174423
2026-02-01 15:58:38 +00:00
Philip Ginsbach-Chen
e345976e04
[SelectionDAG] Handle undef at any position in isConstantSequence (#176671)
This patch extends `BuildVectorSDNode::isConstantSequence` to recognize
constant sequences that contain undef elements at any position.

The new implementation finds the first two non-undef constant elements,
computes the stride from their difference, then verifies all other
defined elements match the sequence. This enables SVE's INDEX
instruction to be used in more cases.

This change particularly benefits ZIP1/ZIP2 patterns where one operand
is a constant sequence. When a smaller constant vector like `<0, 1, 2,
3>` is used in a ZIP1 shuffle producing a wider result, it gets expanded
with trailing undefs. Similarly, for ZIP2 patterns, the DAG combiner
transforms the constant to have leading undefs since ZIP2 only uses the
upper half of its operands.

In particular, these patterns arise naturally from `VectorCombine`'s
`compactShuffleOperands` optimization (see #176074) that I am suggesting
as a fix for #137447.
2026-01-30 19:57:11 +00:00
Osama Abdelkader
aad7259ff6
[AArch64] Optimize memset to use NEON DUP instruction for more sizes (#166030)
This change improves memset code generation for non-zero values on
AArch64 by using NEON's DUP instruction instead of
the less efficient multiplication with 0x01010101 pattern.

For small sizes, the value is extracted from a larger DUP. For
non-power-of-two sizes, overlapping stores are used in some cases.

TargetLowering::findOptimalMemOpLowering is modified to allow explicitly
specifying the size of the constant in cases where the constant is
larger than the store operations.

Fixes #165949
2026-01-29 13:03:38 -08:00
Craig Topper
53ec484ebf
[SelectionDAG] Add CTLS to FoldConstantArithmetic and optimize i1 CTLS to 0. (#178552)
Since we don't have a CTLS intrinsic, it likely gets constant folded
while it is still a CTLZ pattern so I'm using a unittest to test it.
2026-01-29 08:00:10 -08:00
serge-sans-paille
adbbe856d7
[perf] Replace copy-assign by move-assign in llvm/lib/CodeGen/* (#178172) 2026-01-28 06:57:50 +00:00
Sander de Smalen
0e84f659b8
Support EXTRACT_SUBVECTOR in computeKnownBits for scalable vectors (#177163)
Rather than not supporting this case it would just be more conservative
as it will need to prove known bits for all elements.

Follows on from #176883
2026-01-27 12:53:00 +00:00
Craig Topper
896a667473
[KnownBits][SelectionDAG] Add KnownBits::clmul. Support trailing bits. NFC (#177517)
Borrow the known trailing bits logic from KnownBits::mul, but using
APIntOps::clmul.
2026-01-23 11:11:38 -08:00
Craig Topper
53b0a64e98
[SelectionDAG] Add very basic computeKnownBits support for ISD::CLMUL. (#177445)
This implements leading zero count support so we can remove some
unnecessary ANDs.
2026-01-22 14:49:34 -08:00
Cheng Lingfei
711e8e5694
[AArch64] Optimize memcpy for non-power of two sizes (#168890)
The previous getMemcpyLoadsAndStores implementation would chain
load/store instructions from "NumLdStInMemcpy - GlueIter -
GluedLdStLimit" to "NumLdStInMemcpy - GlueIter". This approach caused
issues when copying non-power-of-two sizes, as it would chain leading
load/stores with subsequent instructions at non-power-of-two aligned
offsets.

This chaining pattern prevented optimal optimizations in
aarch64-ldst-opt pass for these load/store instructions.

This commit modifies the chaining range to be from GlueIter to GlueIter
+ GluedLdStLimit, enabling proper optimization of load/store
instructions in aarch64-ldst-opt.


Closes https://github.com/llvm/llvm-project/issues/165947
2026-01-22 15:47:50 +00:00
Sander de Smalen
e807c6f89d
[AArch64] Fold sext-in-reg for predicate -> fixed-length conversions. (#176883) 2026-01-21 13:15:28 +00:00
Matt Arsenault
aca2783840
DAG: Get libcall info from LibcallLowering in more places (#176836)
Avoid using TargetLowering functions
2026-01-20 12:47:22 +01:00
Sander de Smalen
3eed0511c0 [SelectionDAG] NFC: Remove redundant assert in ComputeNumSignBits.
This assert should not have existed, because just below it the code
bails out for that same condition. The case of the vector being a
scalable vector also shouldn't cause the compiler to crash with an
assertion failure, and instead it should just avoid analysing the
expression.
2026-01-20 09:09:15 +00:00
Jerry Dang
d2c5892c22
[SelectionDAG] Add TRUNCATE_SSAT_S/U and TRUNCATE_USAT_U to canCreateUndefOrPoison and computeKnownBits (#152143) (#168809)
1. Implement `SelectionDAG::computeKnownBits` for TRUNCATE_SSAT_S/U and
TRUNCATE_USAT_U
2. Saturating truncation operations are well-defined for all inputs and
cannot create poison or undef values. This allows the optimizer to
eliminate unnecessary freeze instructions after these operations.

Fixes #152143
2026-01-19 10:25:08 +00:00
fbrv
dd29183f33
[DAG] Allow MIN/MAX signedness flip when operands are known-negative (#174469)
Extend the existing DAGCombine logic in visitIMINMAX so that signed and
unsigned MIN/MAX can be flipped not only when both operands are known
non-negative but also when both operands are known negative. This
replaces the old SignBitIsZero checks with computeKnownBits and explicit
tests for non-negative or negative operands while keeping all existing
legality and saturation gating in place. Add regression tests to cover
both the known-negative case and the known-non-negative case.

Fixes #174325
2026-01-16 18:48:54 +00:00
Matt Arsenault
01e6245af4
DAG: Avoid querying libcall info from TargetLowering (#176268)
Libcall lowering decisions should come from the LibcallLoweringInfo
analysis. Query this through the DAG, so eventually the source
can be the analysis. For the moment this is just a wrapper around
the TargetLowering information.
2026-01-16 09:02:49 +00:00
zhijian lin
7b90f426a6
[PowerPC] using milicode call for strstr instead of lib call (#176002)
AIX has "millicode" routines, which are functions loaded at boot time
into fixed addresses in kernel memory. This allows them to be customized
for the processor. The __strstr routine is a millicode implementation;
we use millicode for the strstr function instead of a library call to
improve performance.

I add a helper function `getRuntimeCallSDValueHelper` in the patch. I
will refactor the function `SelectionDAG::getStrlen`
`SelectionDAG::getStrcpy` etc later in another patch.
2026-01-15 14:58:17 -05:00
Manasij Mukherjee
2fa1ba62ac
[SelectionDAG] Fix zext assertion check for scalable vectors (#176064)
Use element type comparisons in getZeroExtendInReg to avoid comparing
scalable and fixed types.

Fixes #176037
2026-01-14 22:00:26 -08:00
Gergo Stomfai
5f31b9c381
[DAG] computeKnownBits - add CTLS handling (#174824)
Add handling for CTLS using the same method as in
https://github.com/llvm/llvm-project/pull/174636.

Added tests to AArch64 and RISCV, but it seems that ARM is actually
resolving `llvm.arm.cls` to `clz`, so not tests added there.
2026-01-14 15:04:40 +00:00