Skip to content

Commit e884324

Browse files
committed
[RISCV] Generalize select (and (x , 0x1) == 0), y, (z ^ y) ) and select (and (x , 0x1) == 0), y, (z | y) ) transforms by removing and-clause
These transforms were recently added (by me) in D134881. Looking at the code again, I realized we don't need the (and x, 0x1) portion of the pattern, we just need to know that the result of that sub-tree is either 0 or 1. Checking for this directly allows us to match slightly more broadly. The test changes are zext i1 arguments, but this could also kick in for e.g. shifts of high bits, or any other source of known bits. Differential Revision: https://reviews.llvm.org/D135081
1 parent dcd02a5 commit e884324

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9022,12 +9022,13 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
90229022
if (TrueV == FalseV)
90239023
return TrueV;
90249024

9025-
// (select (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ y
9026-
// (select (and (x , 0x1) != 0), (z ^ y) ), y -> (-(and (x , 0x1)) & z ) ^ y
9027-
// (select (and (x , 0x1) == 0), y, (z | y) ) -> (-(and (x , 0x1)) & z ) | y
9028-
// (select (and (x , 0x1) != 0), (z | y) ), y -> (-(and (x , 0x1)) & z ) | y
9025+
// (select (x in [0,1] == 0), y, (z ^ y) ) -> (-x & z ) ^ y
9026+
// (select (x in [0,1] != 0), (z ^ y) ), y -> (-x & z ) ^ y
9027+
// (select (x in [0,1] == 0), y, (z | y) ) -> (-x & z ) | y
9028+
// (select (x in [0,1] != 0), (z | y) ), y -> (-x & z ) | y
9029+
APInt Mask = APInt::getBitsSetFrom(LHS.getValueSizeInBits(), 1);
90299030
if (isNullConstant(RHS) && ISD::isIntEqualitySetCC(CCVal) &&
9030-
LHS.getOpcode() == ISD::AND && isOneConstant(LHS.getOperand(1))) {
9031+
DAG.MaskedValueIsZero(LHS, Mask)) {
90319032
unsigned Opcode;
90329033
SDValue Src1, Src2;
90339034
// true if FalseV is XOR or OR operator and one of its operands

llvm/test/CodeGen/RISCV/select-binop-identity.ll

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,16 @@ define i64 @and_select_all_ones_i64(i1 zeroext %c, i64 %x, i64 %y) {
6161
define signext i32 @or_select_all_zeros_i32(i1 zeroext %c, i32 signext %x, i32 signext %y) {
6262
; RV32I-LABEL: or_select_all_zeros_i32:
6363
; RV32I: # %bb.0:
64-
; RV32I-NEXT: beqz a0, .LBB2_2
65-
; RV32I-NEXT: # %bb.1:
66-
; RV32I-NEXT: or a2, a2, a1
67-
; RV32I-NEXT: .LBB2_2:
68-
; RV32I-NEXT: mv a0, a2
64+
; RV32I-NEXT: neg a0, a0
65+
; RV32I-NEXT: and a0, a0, a1
66+
; RV32I-NEXT: or a0, a0, a2
6967
; RV32I-NEXT: ret
7068
;
7169
; RV64I-LABEL: or_select_all_zeros_i32:
7270
; RV64I: # %bb.0:
73-
; RV64I-NEXT: beqz a0, .LBB2_2
74-
; RV64I-NEXT: # %bb.1:
75-
; RV64I-NEXT: or a2, a2, a1
76-
; RV64I-NEXT: .LBB2_2:
77-
; RV64I-NEXT: mv a0, a2
71+
; RV64I-NEXT: neg a0, a0
72+
; RV64I-NEXT: and a0, a0, a1
73+
; RV64I-NEXT: or a0, a0, a2
7874
; RV64I-NEXT: ret
7975
%a = select i1 %c, i32 %x, i32 0
8076
%b = or i32 %y, %a
@@ -132,22 +128,18 @@ define signext i32 @xor_select_all_zeros_i32(i1 zeroext %c, i32 signext %x, i32
132128
define i64 @xor_select_all_zeros_i64(i1 zeroext %c, i64 %x, i64 %y) {
133129
; RV32I-LABEL: xor_select_all_zeros_i64:
134130
; RV32I: # %bb.0:
135-
; RV32I-NEXT: beqz a0, .LBB5_2
136-
; RV32I-NEXT: # %bb.1:
137-
; RV32I-NEXT: xor a3, a3, a1
138-
; RV32I-NEXT: xor a4, a4, a2
139-
; RV32I-NEXT: .LBB5_2:
140-
; RV32I-NEXT: mv a0, a3
141-
; RV32I-NEXT: mv a1, a4
131+
; RV32I-NEXT: neg a5, a0
132+
; RV32I-NEXT: and a0, a5, a1
133+
; RV32I-NEXT: xor a0, a0, a3
134+
; RV32I-NEXT: and a1, a5, a2
135+
; RV32I-NEXT: xor a1, a1, a4
142136
; RV32I-NEXT: ret
143137
;
144138
; RV64I-LABEL: xor_select_all_zeros_i64:
145139
; RV64I: # %bb.0:
146-
; RV64I-NEXT: beqz a0, .LBB5_2
147-
; RV64I-NEXT: # %bb.1:
148-
; RV64I-NEXT: xor a2, a2, a1
149-
; RV64I-NEXT: .LBB5_2:
150-
; RV64I-NEXT: mv a0, a2
140+
; RV64I-NEXT: neg a0, a0
141+
; RV64I-NEXT: and a0, a0, a1
142+
; RV64I-NEXT: xor a0, a0, a2
151143
; RV64I-NEXT: ret
152144
%a = select i1 %c, i64 %x, i64 0
153145
%b = xor i64 %a, %y

0 commit comments

Comments
 (0)