[llvm] [DAG]: Created method `m_SelectLike` to match `ISD::Select` and `ISD::VSelect` (PR #164069)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 18 07:26:31 PDT 2025
https://github.com/kper updated https://github.com/llvm/llvm-project/pull/164069
>From 18977756ddc9fb55e8cbd65a0410419ea9fde3b0 Mon Sep 17 00:00:00 2001
From: Kevin Per <kevin.per at protonmail.com>
Date: Sat, 18 Oct 2025 09:46:32 +0000
Subject: [PATCH 1/4] [DAG]: Created method `m_SelectLike` to match
`ISD::Select` and `ISD::VSelect`
---
llvm/include/llvm/CodeGen/SDPatternMatch.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 201dc68de8b76..707c529d2b97c 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -558,6 +558,12 @@ m_VSelect(const T0_P &Cond, const T1_P &T, const T2_P &F) {
return TernaryOpc_match<T0_P, T1_P, T2_P>(ISD::VSELECT, Cond, T, F);
}
+template <typename T0_P, typename T1_P, typename T2_P>
+inline TernaryOpc_match<T0_P, T1_P, T2_P>
+m_SelectLike(const T0_P &Cond, const T1_P &T, const T2_P &F) {
+ return m_Select(Cond, T, F) || m_VSelect(Cond, T, F);
+}
+
template <typename T0_P, typename T1_P, typename T2_P>
inline Result_match<0, TernaryOpc_match<T0_P, T1_P, T2_P>>
m_Load(const T0_P &Ch, const T1_P &Ptr, const T2_P &Offset) {
>From 7ffcd4d2732d83b5477b613df2ff49dc4629e532 Mon Sep 17 00:00:00 2001
From: Kevin Per <kevin.per at protonmail.com>
Date: Sat, 18 Oct 2025 14:06:02 +0000
Subject: [PATCH 2/4] [DAG]: Refactored foldSelectWithIdentityConstant
---
llvm/include/llvm/CodeGen/SDPatternMatch.h | 5 ++---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 17 +++++++++--------
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 707c529d2b97c..0dcf400962393 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -559,9 +559,8 @@ m_VSelect(const T0_P &Cond, const T1_P &T, const T2_P &F) {
}
template <typename T0_P, typename T1_P, typename T2_P>
-inline TernaryOpc_match<T0_P, T1_P, T2_P>
-m_SelectLike(const T0_P &Cond, const T1_P &T, const T2_P &F) {
- return m_Select(Cond, T, F) || m_VSelect(Cond, T, F);
+inline auto m_SelectLike(const T0_P &Cond, const T1_P &T, const T2_P &F) {
+ return m_AnyOf(m_Select(Cond, T, F), m_VSelect(Cond, T, F));
}
template <typename T0_P, typename T1_P, typename T2_P>
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index c97300d64d455..d788a76ae65bf 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -2476,28 +2476,29 @@ static bool canFoldInAddressingMode(SDNode *N, SDNode *Use, SelectionDAG &DAG,
/// masked vector operation if the target supports it.
static SDValue foldSelectWithIdentityConstant(SDNode *N, SelectionDAG &DAG,
bool ShouldCommuteOperands) {
- // Match a select as operand 1. The identity constant that we are looking for
- // is only valid as operand 1 of a non-commutative binop.
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);
+
+ // Match a select as operand 1. The identity constant that we are looking for
+ // is only valid as operand 1 of a non-commutative binop.
if (ShouldCommuteOperands)
std::swap(N0, N1);
- unsigned SelOpcode = N1.getOpcode();
- if ((SelOpcode != ISD::VSELECT && SelOpcode != ISD::SELECT) ||
- !N1.hasOneUse())
+ SDValue Cond, TVal, FVal;
+ if (!sd_match(N1,
+ m_SelectLike(m_Value(Cond), m_Value(TVal), m_Value(FVal))) ||
+ !N1->hasOneUse()) {
return SDValue();
+ }
// We can't hoist all instructions because of immediate UB (not speculatable).
// For example div/rem by zero.
if (!DAG.isSafeToSpeculativelyExecuteNode(N))
return SDValue();
+ unsigned SelOpcode = N1.getOpcode();
unsigned Opcode = N->getOpcode();
EVT VT = N->getValueType(0);
- SDValue Cond = N1.getOperand(0);
- SDValue TVal = N1.getOperand(1);
- SDValue FVal = N1.getOperand(2);
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
// This transform increases uses of N0, so freeze it to be safe.
>From 86f6692d0ff560c0753024a34cc078907698f26c Mon Sep 17 00:00:00 2001
From: Kevin Per <kevin.per at protonmail.com>
Date: Sat, 18 Oct 2025 14:14:00 +0000
Subject: [PATCH 3/4] [DAG]: Refactored tryToFoldExtendSelectLoad
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index d788a76ae65bf..80a91ff46977f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -13857,12 +13857,13 @@ static SDValue tryToFoldExtendSelectLoad(SDNode *N, const TargetLowering &TLI,
Opcode == ISD::ANY_EXTEND) &&
"Expected EXTEND dag node in input!");
- if (!(N0->getOpcode() == ISD::SELECT || N0->getOpcode() == ISD::VSELECT) ||
- !N0.hasOneUse())
+ SDValue Cond, Op1, Op2;
+ if (!sd_match(N0,
+ m_SelectLike(m_Value(Cond), m_Value(Op1), m_Value(Op2))) ||
+ !N0->hasOneUse()) {
return SDValue();
+ }
- SDValue Op1 = N0->getOperand(1);
- SDValue Op2 = N0->getOperand(2);
if (!isCompatibleLoad(Op1, Opcode) || !isCompatibleLoad(Op2, Opcode))
return SDValue();
>From ad68cbdc9a2201f8c19e3a067a6dd06b6bf7341b Mon Sep 17 00:00:00 2001
From: Kevin Per <kevin.per at protonmail.com>
Date: Sat, 18 Oct 2025 14:16:51 +0000
Subject: [PATCH 4/4] [DAG]: Refactored takeInexpensiveLog2
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 80a91ff46977f..c445a40ed3665 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -29619,12 +29619,13 @@ static SDValue takeInexpensiveLog2(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
}
// c ? X : Y -> c ? Log2(X) : Log2(Y)
- if ((Op.getOpcode() == ISD::SELECT || Op.getOpcode() == ISD::VSELECT) &&
- Op.hasOneUse()) {
- if (SDValue LogX = takeInexpensiveLog2(DAG, DL, VT, Op.getOperand(1),
- Depth + 1, AssumeNonZero))
- if (SDValue LogY = takeInexpensiveLog2(DAG, DL, VT, Op.getOperand(2),
- Depth + 1, AssumeNonZero))
+ SDValue Cond, TVal, FVal;
+ if (sd_match(Op, m_SelectLike(m_Value(Cond), m_Value(TVal), m_Value(FVal))) &&
+ Op->hasOneUse()) {
+ if (SDValue LogX =
+ takeInexpensiveLog2(DAG, DL, VT, TVal, Depth + 1, AssumeNonZero))
+ if (SDValue LogY =
+ takeInexpensiveLog2(DAG, DL, VT, FVal, Depth + 1, AssumeNonZero))
return DAG.getSelect(DL, VT, Op.getOperand(0), LogX, LogY);
}
More information about the llvm-commits
mailing list