66 Commits

Author SHA1 Message Date
Simon Pilgrim
6832709dc0
[DAG] SDPatternMatch - rename m_Opc -> m_SpecificOpc (#190215)
Match naming convention for other m_Specific* matchers, and frees up the
m_Opc() matcher for future use in #84940 to allow us to capture the
opcode of a unknown binop

Moving to m_SpecificOpc does mess up the formatting in a few places,
I've tried to refactor to use the m_Value(SDValue, ....) matcher where I
can to retrieve some whitespace
2026-04-03 18:03:00 +00:00
Craig Topper
1e676e7edb
[SelectionDAG] Use virtual registers instead of arbitrary physical registers in unit tests. NFC (#183205)
These tests use constants in the physical register space. These will
correspond to different registers on different targets and aren't
stable.

Using virtual registers makes more sense here. This will print in a
coherent way if you anyone were to dump the DAG created by these tests.
2026-02-25 08:52:45 -08:00
zGoldthorpe
61d40e22c4
[SDPatternMatch] Add m_ConstInt overloads with uint64_t/int64_t operands (#182615)
Adds overloads
```cpp
auto m_ConstInt(uint64_t &);
auto m_ConstInt(int64_t &);
```
which behave analogously to `m_ConstInt(APInt &)`, but only match if the
captured integer fits within 64 bits.
2026-02-24 10:07:06 -07:00
Min-Yih Hsu
e022ea2ced
[SDPatternMatch] Support conditionally binding the value matching a sub-pattern (#182091)
I've seen more and more occurrences of what is essentially
```
m_AllOf(<sub pattern>, m_Value(BindVal))
```
That is, bind to the same SDValue that matches the sub-pattern. Most
people won't even bother write this `m_AllOf` construct because it's too
long, and instead just write another `sd_match` against the bound value.

This patch adds
```
m_Value(BindVal, <sub pattern>)
```
To replace the `m_AllOf` construct above and conditionally bind the
SDValue that matches the sub-pattern.

---------

Co-authored-by: Stefan Weigl-Bosker <stefan@s00.xyz>
2026-02-18 15:21:59 -08:00
Simon Pilgrim
9aec188b77
[DAG] SDPatternMatch - allow m_BinOp / m_c_BinOp to take an optional SDNodeFlags required for matching (#178435)
BinaryOpc_match is already wired up for this - but allow us to use
m_BinOp/m_c_BinOp with the required flags directly

Updated the foldShiftToAvg folds to make use of this
2026-01-28 18:50:42 +00:00
Prajwal
6fe246e639
[DAG] SDPatternMatch - Add matchers for reassociatable additions with NSW/NUW flags (#177973)
Fixes #176809
2026-01-28 14:03:08 +00:00
Stefan Weigl-Bosker
2370bf206d
[DAG] Extend MinMax matchers to detect flippable sign (#177504)
Fixes #174328
2026-01-25 15:35:57 +00:00
Aryan Kadole
af4f5ee93b
[DAG] SDPatternMatch - add m_Negative/m_StrictlyPositive/m_NonNegative/m_NonPositive/m_NonZero matchers (#175191)
Resolves #174327
2026-01-22 14:13:59 +00:00
Icaro
18b004d39b
[DAG] SDPatternMatch - Implement m_IntrinsicWOChain matchers (#175626)
Resolve #174436

Implemented matchers for intrinsics.

The usage looks something like:

```cpp
sd_match(N , m_IntrinsicWOChain<Intrinsic::${INTRINSIC_ID}>(/* match possible operands of the intrinsic */));
```
2026-01-14 11:24:10 +00:00
Abhiram Jampani
9973e38b5a
[SDPatternMatch] Add m_FAbs matcher (#174975)
Adds a pattern matcher for floating-point absolute value (ISD::FABS),
following the same pattern as m_Abs for integer absolute value.

Fixes #174751
2026-01-08 15:04:17 +00:00
Muhammad Abdul
92da91f93e
[DAG] SDPatternMatch - add a m_SpecificNeg() matcher (#173807)
Resolves #173770 

Adds m_SpecificNeg(SDValue V) to match either m_Neg(m_Specific(V)) or
constant/build-vector where elements are the negation of V's constants

Has unit test coverage on SelectionDAGPatternMatchTest.cpp
2026-01-06 13:42:33 +00:00
Artur Bermond Torres
755a693299
[DAG] SDPatternMatch - Replace runtime data structures with lengths known at compile time (#172064)
Following the suggestions in #170061, I replaced `SmallVector<SDValue>`
with `std::array<SDValue, NumPatterns>` and `SmallBitVector` with
`Bitset<NumPatterns>`.

I had to make some changes to the `collectLeaves` and
`reassociatableMatchHelper` functions. In `collectLeaves` specifically,
I changed the return type so I could propagate a failure in case the
number of found leaves is greater than the number of expected patterns.
I also added a new unit test that, together with the one already present
in the previous line, checks if the matching fails in the cases where
the number of patterns is less or more than the number of leaves.

I don't think this is going to completely address the increased compile
time reported in #169644, but hopefully it leads to an improvement.
2025-12-14 10:48:08 +00:00
Artur Bermond Torres
3a81e03088
[DAG] SDPatternMatch - Fix m_Reassociatable mismatching (#170061)
Fixes #169645

The issue was caused by a for-loop improperly overwriting SDValue binds
when m_Reassociatable is given two or more patterns that (1) call
m_Value with an SDValue parameter and (2) differ by that parameter. This
fix comes with added unit tests relevant to SDValue bindings inside
m_Reassociatable patterns.

Essentially, the original implementation first tried to match every
combination of leaf node and pattern possible and stored that in a
matrix-like structure, and then did a recursive search on that matrix to
check if it's possible to pair every leaf with a pattern. The problem is
that m_Value has a side effect where it changes an SDValue, and the
creation of this matrix was corrupting these values. Below is an example
of this, following the order of execution in the original implementation
and using the case brought by issue #169645, where this behavior was
found. The example tries to match ((a >> 1) + (b >> 1) + (a & b & 1)),
using uppercase letters for the SDValue variables themselves and
lowercase for their values. The result is that the pattern matches the
same value for A and B, which was the behavior observed in the issue:

| Line | Leaf | Pattern | Match? | Effect |
|--------|--------|--------|--------|--------|
| 1 | a >> 1 | m_Srl(m_Value(A), m_One()) | Yes | A <- a |
| 2 | a >> 1 | m_Srl(m_Value(B), m_One()) | Yes | B <- a |
| 3 | a >> 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One())
| No | -- |
| 4 | b >> 1 | m_Srl(m_Value(A), m_One()) | Yes | A <- b |
| 5 | b >> 1 | m_Srl(m_Value(B), m_One()) | Yes | B <- b |
| 6 | b >> 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B), m_One())
| No | -- |
| 7 | a & b & 1 | m_Srl(m_Value(A), m_One()) | No | -- |
| 8 | a & b & 1 | m_Srl(m_Value(B), m_One()) | No | -- |
| 9 | a & b & 1 | m_ReassociableAnd(m_Deferred(A), m_Deferred(B),
m_One()) | a == b | -- |

To fix this, the function now matches the patterns during the recursive
search itself, instead of preparing the matrix beforehand. Although this
does fix the issue, it does mean that we're performing a best case of n
and worst case of n! matching attempts, instead of the fixed nˆ2 in the
original, where n is the number of patterns provided. Going back to the
table above, using this fix the lines 2, 3, 4, 6, 7, and 8 do not
happen, and so the only effects happening are A <- a and B <- b, which
then will result in line 9 matching correctly.
2025-12-09 09:26:51 +00:00
Ravil Dorozhinskii
bc4143b27a
[DAG] SDPatternMatch - add m_SpecificFP matcher (#167438)
This patch introduces SpecificFP matcher for SelectionDAG nodes.

This includes:

Adding SpecificFP_match() in SDPatternMatch.h.
Adding test coverage in SelectionDAGPatternMatchTest.cpp.

Closes #165566
2025-11-25 11:49:36 +01:00
Matt Arsenault
9fa7627d89
DAG: Handle poison in m_Undef (#168288) 2025-11-21 18:29:15 -05:00
陈子昂
9b513ad505
[DAG] Add generic m_TernaryOp() / m_c_TernaryOp() matchers (#165520)
Similar to the m_BinOp/m_c_BinOp matchers, this patch introduces generic matchers for SelectionDAG nodes with three operands.

This includes:
- Adding m_TernaryOp() and m_c_TernaryOp() templates in SDPatternMatch.h.
- Adding comprehensive test coverage in SelectionDAGPatternMatchTest.cpp.

Fixes #165378
2025-10-29 15:17:38 +00:00
kper
e83eee335c
[DAG] Create SDPatternMatch method m_SelectLike to match ISD::Select and ISD::VSelect (#164069)
Fixes #150019
2025-10-22 09:49:34 +00:00
黃國庭
f04ea2ef1c
Add m_SelectCCLike matcher to match SELECT_CC or SELECT with SETCC (#149646)
Fix #147282 and  Follow-up to #148834

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-08-01 10:12:05 +01:00
Paul Walker
13f38c97d5
[LLVM][SelectionDAG] Align poison/undef binop folds with IR. (#149334)
The "at construction" binop folds in SelectionDAG::getNode() has
different behaviour when compared to the equivalent LLVM IR. This PR
makes the behaviour consistent while also extending the coverage to
include signed/unsigned max/min operations.
2025-07-30 11:20:30 +01:00
alex-t
9293b65a61
[AMDGPU] SelectionDAG divergence tracking should take into account Target divergency. (#147560)
This is the next attempt to upstream this:
https://github.com/llvm/llvm-project/pull/144947
The las one caused build errors in AArch64.
Issue was resolved.
2025-07-09 00:06:58 +02:00
Simon Pilgrim
d3270adcb5
[DAG] SDPatternMatch - add matching for SELECT_CC patterns to min/max like matchers (#147071)
Fixes #147083
2025-07-08 21:38:44 +01:00
woruyu
ae43002cd0
[NFC] Remove redundant namespace qualifier in SelectionDAGPatternMatchTest (#147443)
### Summary
This PR remove the extra llvm::SDPatternMatch prefix in
https://github.com/llvm/llvm-project/pull/147044
2025-07-07 19:09:12 -07:00
woruyu
c80fa2364b
[DAG] SDPatternMatch m_Zero/m_One/m_AllOnes have inconsistent undef h… (#147044)
### Summary
This PR resolves https://github.com/llvm/llvm-project/issues/146871 
This PR resolves https://github.com/llvm/llvm-project/issues/140745

Refactor m_Zero/m_One/m_AllOnes all use struct template function to
match and AllowUndefs=false as default.
2025-07-07 15:04:54 +01:00
Florian Hahn
bfd457588a
Revert "[AMDGPU] SelectionDAG divergence tracking should take into account Target divergency. (#144947)"
This reverts commit 8ac7210b7f0ad49ae7809bf6a9faf2f7433384b0.

This breaks the building the AArch64 backend, e.g. see
https://github.com/llvm/llvm-project/pull/144947

Revert to unbreak the build.

Also reverts follow-up commits 1e76f012db3ccfaa05e238812e572b5b6d12c17e.
2025-07-03 19:25:01 +01:00
alex-t
8ac7210b7f
[AMDGPU] SelectionDAG divergence tracking should take into account Target divergency. (#144947)
If a kernel is known to be executing only a single lane, IR
UniformityAnalysis will take note of that (via
GCNTTIImpl::hasBranchDivergence) and report that all values are uniform.
SelectionDAG's built-in divergence tracking should do the same.
2025-07-03 18:37:37 +02:00
zhaohui
eb1a80bfd3
[DAG] Implement SDPatternMatch m_SpecificScalarVT and m_SpecificVectorElementVT matchers (#144996)
Resolves https://github.com/llvm/llvm-project/issues/144477

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-06-30 13:26:00 +01:00
Abhishek Kaushik
abf8e25ac7
[DAG] Add SDPatternMatch::m_Load (#145481)
Add SDPatternMatch matcher and unit test coverage for `ISD::LOAD`
opcode.
This only matches the loaded value i.e. ResNo 0 and not the output
chain.
e.g.
```
m_Load(m_Value(), m_Value(), m_Value())
```

The first value is the input chain, the second is the base pointer, and
the last value is the offset.
2025-06-26 23:03:31 +05:30
Min-Yih Hsu
f0c1a9a85d
[DAG] Add SDPatternMatch::m_Result to match a specific SDValue result (#145775)
`m_Result<N>` matches a SDValue that is the N-th result of the defining
SDNode. This is useful for creating a more fine-grained matching on
SDNode with multiple results.

-----
Inspired by #145481
2025-06-26 22:08:22 +05:30
Ming Yan
bb51c5d4b8
[SDPatternMatch] Add m_Poison matcher (#144860)
Add SDPatternMatch matcher and unit test coverage for ISD::POISON opcode

e.g.
```
m_InsertElt(m_Poison(), m_Value(), m_Zero())
```
2025-06-20 10:58:02 +08:00
Rajveer Singh Bharadwaj
e07b1b26c3
[DAG] Implement SDPatternMatch m_Abs() matcher (#144512) 2025-06-18 12:59:27 +05:30
Kazu Hirata
03f616eb3a
[llvm] Compare std::optional<T> to values directly (NFC) (#143340)
This patch transforms:

  X && *X == Y

to:

  X == Y

where X is of std::optional<T>, and Y is of T or similar.
2025-06-08 22:37:59 -07:00
Simon Pilgrim
bde39d7251
[DAG] Add SDPatternMatch::m_BitwiseLogic common matcher for AND/OR/XOR nodes (#138301) 2025-05-06 12:50:50 +01:00
Ethan Kaji
6b00ae6359
[DAG] SDPatternMatch - add matchers for reassociatable binops (#119985)
fixes https://github.com/llvm/llvm-project/issues/118847

implements matchers for reassociatable opcodes as well as helpers for
commonly used reassociatable binary matchers.

---------

Co-authored-by: Min-Yih Hsu <min@myhsu.dev>
2025-03-21 13:05:06 -07:00
Nikita Popov
f137c3d592
[TargetRegistry] Accept Triple in createTargetMachine() (NFC) (#130940)
This avoids doing a Triple -> std::string -> Triple round trip in lots
of places, now that the Module stores a Triple.
2025-03-12 17:35:09 +01:00
Simon Pilgrim
bacfdcd7e0
[DAG] Add SDPatternMatch::m_BitCast matcher (#123327)
Simplifies a future patch
2025-01-17 12:22:07 +00:00
Amr Hesham
1d58699f5c
[SDPatternMatch] Add Matcher m_Undef (#122521)
Add Matcher `m_Undef`

Fixes: #122439
2025-01-11 13:23:37 +01:00
Thor Preimesberger
c1c50c7a3e
[SDPatternMatch] Add matchers m_ExtractSubvector and m_InsertSubvector (#120212)
Fixes #118846
2025-01-09 15:20:48 -08:00
Aidan Goldfarb
f3bc8c34c9
Add SD matchers and unit test coverage for ISD::VECTOR_SHUFFLE (#119592)
This PR resolves #118845. I aimed to mirror the implementation
`m_Shuffle()` in
[PatternMatch.h](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/PatternMatch.h).

Updated
[SDPatternMatch.h](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/SDPatternMatch.h)
- Added `struct m_Mask` to match masks (`ArrayRef<int>`)
- Added two `m_Shuffle` functions. One to match independently of mask,
and one to match considering mask.
- Added `struct SDShuffle_match` to match `ISD::VECTOR_SHUFFLE`
considering mask

Updated
[SDPatternMatchTest.cpp](https://github.com/llvm/llvm-project/blob/main/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp)
- Added `matchVecShuffle` test, which tests the behavior of both
`m_Shuffle()` functions

- - -

I am not sure if my test coverage is complete. I am not sure how to test
a `false` match, simply test against a different instruction? [Other
tests
](https://github.com/llvm/llvm-project/blob/main/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp#L175),
such as for `VSelect`, test against `Select`. I am not sure if there is
an analogous instruction to compare against for `VECTOR_SHUFFLE`. I
would appreciate some pointers in this area. In general, please
liberally critique this PR!

---------

Co-authored-by: Aidan <aidan.goldfarb@mail.mcgill.ca>
2025-01-06 13:57:19 -05:00
Chris White
ecdf0dac56
[DAG] SDPatternMatch - Add m_ExtractElt and m_InsertElt matchers (#119430)
Resolves #118844
2024-12-13 15:36:53 -08:00
Matin Raayai
bb3f5e1fed
Overhaul the TargetMachine and LLVMTargetMachine Classes (#111234)
Following discussions in #110443, and the following earlier discussions
in https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html,
https://reviews.llvm.org/D38482, https://reviews.llvm.org/D38489, this
PR attempts to overhaul the `TargetMachine` and `LLVMTargetMachine`
interface classes. More specifically:
1. Makes `TargetMachine` the only class implemented under
`TargetMachine.h` in the `Target` library.
2. `TargetMachine` contains target-specific interface functions that
relate to IR/CodeGen/MC constructs, whereas before (at least on paper)
it was supposed to have only IR/MC constructs. Any Target that doesn't
want to use the independent code generator simply does not implement
them, and returns either `false` or `nullptr`.
3. Renames `LLVMTargetMachine` to `CodeGenCommonTMImpl`. This renaming
aims to make the purpose of `LLVMTargetMachine` clearer. Its interface
was moved under the CodeGen library, to further emphasis its usage in
Targets that use CodeGen directly.
4. Makes `TargetMachine` the only interface used across LLVM and its
projects. With these changes, `CodeGenCommonTMImpl` is simply a set of
shared function implementations of `TargetMachine`, and CodeGen users
don't need to static cast to `LLVMTargetMachine` every time they need a
CodeGen-specific feature of the `TargetMachine`.
5. More importantly, does not change any requirements regarding library
linking.

cc @arsenm @aeubanks
2024-11-14 13:30:05 -08:00
Yingwei Zheng
cf9d1c1486
[SDAG] Simplify SDNodeFlags with bitwise logic (#114061)
This patch allows using enumeration values directly and simplifies the
implementation with bitwise logic. It addresses the comment in
https://github.com/llvm/llvm-project/pull/113808#discussion_r1819923625.
2024-10-31 08:10:07 +08:00
Simon Pilgrim
49fa91edf7 [DAG] SDPatternMatch - add missing ROTL/ROTR matchers 2024-10-16 11:57:18 +01:00
Simon Pilgrim
d3d2d72549 [DAG] SDPatternMatch - add missing BSWAP/CTPOP/CTTZ matchers 2024-10-16 11:52:58 +01:00
c8ef
854ded9b24
Reapply "[DAG] Enhance SDPatternMatch to match integer minimum and maximum patterns in addition to the existing ISD nodes." (#112203)
This patch adds icmp+select patterns for integer min/max matchers in
SDPatternMatch, similar to those in IR PatternMatch.

Reapply #111774.

Closes #108218.
2024-10-15 21:07:06 +08:00
c8ef
a3b0c31ebc
Revert "[DAG] Enhance SDPatternMatch to match integer minimum and maximum patterns in addition to the existing ISD nodes." (#112200)
Reverts llvm/llvm-project#111774

This appears to be causing some tests to fail.
2024-10-14 21:43:49 +08:00
c8ef
11f625cb87
[DAG] Enhance SDPatternMatch to match integer minimum and maximum patterns in addition to the existing ISD nodes. (#111774)
Closes #108218.

This patch adds icmp+select patterns for integer min/max matchers in
SDPatternMatch, similar to those in IR PatternMatch.
2024-10-14 21:19:34 +08:00
Robert Dazi
8837898b8d
[DAGCombine] Count leading ones: refine post DAG/Type Legalisation if promotion (#102877)
This PR is related to #99591. In this PR, instead of modifying how the
legalisation occurs depending on surrounding instructions, we refine
after legalisation.

This PR has two parts:

* `SDPatternMatch/MatchContext`: Modify a little bit the code to match
Operands (used by `m_Node(...)`) and Unary/Binary/Ternary Patterns to
make it compatible with `VPMatchContext`, instead of only `m_Opc`
supported. Some tests were added to ensure no regressions.
* `DAGCombiner`: Add a `foldSubCtlzNot` which detect and rewrite the
patterns using matching context.

Remaining Tasks:

- [ ] GlobalISel
- [ ] Currently the pattern matching will occur even before
legalisation. Should I restrict it to specific stages instead ?
- [ ] Style: Add a visitVP_SUB ?? Move `foldSubCtlzNot` in another
location for style consistency purpose ?

@topperc

---------

Co-authored-by: v01dxyz <v01dxyz@v01d.xyz>
2024-09-15 15:48:36 +04:00
Noah Goldstein
70f3863b5f [DAG][PatternMatch] Add support for matchers with flags; NFC
Add support for matching with `SDNodeFlags` i.e `add` with `nuw`.

This patch adds helpers for `or disjoint` or `zext nneg` with the same
names as we have in IR/PatternMatch api.

Closes #103060
2024-08-18 15:37:56 -07:00
v01dXYZ
fc1b019638
[DAG] SD Pattern Match: Operands patterns with VP Context (#103308)
Currently, when using a VP match context with `sd_context_match`, only Opcode matching is possible (`m_Opc(Opcode)`).

This PR suggest a way to make patterns with Operands (eg `m_Node`, `m_Add`, ...) works with a VP context.

This PR blocks another PR https://github.com/llvm/llvm-project/pull/102877.

Co-authored-by: v01dxyz <v01dxyz@v01d.xyz>
2024-08-16 09:46:20 +01:00
Jorge Botto
05dfac23f1
[DAG] Adding m_FPToUI and m_FPToSI to SDPatternMatch.h (#104044)
Adds m_FPToUI/m_FPToSI matchers for ISD::FP_TO_UINT/ISD::FP_TO_SINT in SDPatternMatch.h with suitable test coverage.

Fixes https://github.com/llvm/llvm-project/issues/103872
2024-08-15 09:49:40 +01:00