[llvm] 8905b1c - [RISCV] Efficiently lower (select %cond, andn (f, x), f) using zicond (#147369)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 9 06:32:58 PDT 2025
Author: Ryan Buchner
Date: 2025-07-09T09:32:54-04:00
New Revision: 8905b1c38f15d7f7b31e741222ae1de0a11222d8
URL: https://github.com/llvm/llvm-project/commit/8905b1c38f15d7f7b31e741222ae1de0a11222d8
DIFF: https://github.com/llvm/llvm-project/commit/8905b1c38f15d7f7b31e741222ae1de0a11222d8.diff
LOG: [RISCV] Efficiently lower (select %cond, andn (f, x), f) using zicond (#147369)
The following case is now optimized:
(select c, (and f, ~x), f) -> (andn f, (czero_eqz x, c))
Added:
Modified:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/zicond-opts.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index dcb4f690ba35c..1519e83e230e2 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -9075,18 +9075,51 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
if (isNullConstant(TrueV))
return DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, FalseV, CondV);
+ // Check to see if a given operation is a 'NOT', if so return the negated
+ // operand
+ auto getNotOperand = [](const SDValue &Op) -> std::optional<const SDValue> {
+ using namespace llvm::SDPatternMatch;
+ SDValue Xor;
+ if (sd_match(Op, m_OneUse(m_Not(m_Value(Xor))))) {
+ return Xor;
+ }
+ return std::nullopt;
+ };
// (select c, (and f, x), f) -> (or (and f, x), (czero_nez f, c))
+ // (select c, (and f, ~x), f) -> (andn f, (czero_eqz x, c))
if (TrueV.getOpcode() == ISD::AND &&
- (TrueV.getOperand(0) == FalseV || TrueV.getOperand(1) == FalseV))
+ (TrueV.getOperand(0) == FalseV || TrueV.getOperand(1) == FalseV)) {
+ auto NotOperand = (TrueV.getOperand(0) == FalseV)
+ ? getNotOperand(TrueV.getOperand(1))
+ : getNotOperand(TrueV.getOperand(0));
+ if (NotOperand) {
+ SDValue CMOV =
+ DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, *NotOperand, CondV);
+ SDValue NOT = DAG.getNOT(DL, CMOV, VT);
+ return DAG.getNode(ISD::AND, DL, VT, FalseV, NOT);
+ }
return DAG.getNode(
ISD::OR, DL, VT, TrueV,
DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, FalseV, CondV));
+ }
+
// (select c, t, (and t, x)) -> (or (czero_eqz t, c), (and t, x))
+ // (select c, t, (and t, ~x)) -> (andn t, (czero_nez x, c))
if (FalseV.getOpcode() == ISD::AND &&
- (FalseV.getOperand(0) == TrueV || FalseV.getOperand(1) == TrueV))
+ (FalseV.getOperand(0) == TrueV || FalseV.getOperand(1) == TrueV)) {
+ auto NotOperand = (FalseV.getOperand(0) == TrueV)
+ ? getNotOperand(FalseV.getOperand(1))
+ : getNotOperand(FalseV.getOperand(0));
+ if (NotOperand) {
+ SDValue CMOV =
+ DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, *NotOperand, CondV);
+ SDValue NOT = DAG.getNOT(DL, CMOV, VT);
+ return DAG.getNode(ISD::AND, DL, VT, TrueV, NOT);
+ }
return DAG.getNode(
ISD::OR, DL, VT, FalseV,
DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, TrueV, CondV));
+ }
// Try some other optimizations before falling back to generic lowering.
if (SDValue V = combineSelectToBinOp(Op.getNode(), DAG, Subtarget))
diff --git a/llvm/test/CodeGen/RISCV/zicond-opts.ll b/llvm/test/CodeGen/RISCV/zicond-opts.ll
index 35b06c4f4fb41..a16145d15db81 100644
--- a/llvm/test/CodeGen/RISCV/zicond-opts.ll
+++ b/llvm/test/CodeGen/RISCV/zicond-opts.ll
@@ -233,9 +233,8 @@ define i64 @test_inv_and_nez(i64 %f, i64 %x, i1 %cond) {
; RV64ZICOND-LABEL: test_inv_and_nez:
; RV64ZICOND: # %bb.0:
; RV64ZICOND-NEXT: andi a2, a2, 1
-; RV64ZICOND-NEXT: andn a1, a0, a1
-; RV64ZICOND-NEXT: czero.nez a0, a0, a2
-; RV64ZICOND-NEXT: or a0, a1, a0
+; RV64ZICOND-NEXT: czero.eqz a1, a1, a2
+; RV64ZICOND-NEXT: andn a0, a0, a1
; RV64ZICOND-NEXT: ret
%5 = xor i64 %x, -1
%6 = select i1 %cond, i64 %5, i64 -1
@@ -258,9 +257,8 @@ define i64 @test_inv_and_eqz(i64 %f, i64 %x, i1 %cond) {
; RV64ZICOND-LABEL: test_inv_and_eqz:
; RV64ZICOND: # %bb.0:
; RV64ZICOND-NEXT: andi a2, a2, 1
-; RV64ZICOND-NEXT: andn a1, a0, a1
-; RV64ZICOND-NEXT: czero.eqz a0, a0, a2
-; RV64ZICOND-NEXT: or a0, a1, a0
+; RV64ZICOND-NEXT: czero.nez a1, a1, a2
+; RV64ZICOND-NEXT: andn a0, a0, a1
; RV64ZICOND-NEXT: ret
%5 = xor i64 %x, -1
%6 = select i1 %cond, i64 -1, i64 %5
More information about the llvm-commits
mailing list