Skip to content

[DAGCombine][RISCV] fold select_cc seteq (and x, 1) 0, 0, -1 -> neg(and(x, 1)) #152062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28963,6 +28963,16 @@ SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1,
if (SDValue V = foldSelectCCToShiftAnd(DL, N0, N1, N2, N3, CC))
return V;

// fold select_cc seteq (and x, 1), 0, 0, -1 -> neg(and(x, 1))
if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
N0->getValueType(0) == VT && isNullConstant(N1) &&
isAllOnesConstant(N2) && isOneConstant(N0->getOperand(1))) {
SDValue AndLHS = N0->getOperand(0);
SDValue Neg =
DAG.getNode(ISD::AND, DL, VT, AndLHS, DAG.getConstant(1, DL, VT));
return DAG.getNegative(Neg, DL, VT);
}

// fold (select_cc seteq (and x, y), 0, 0, A) -> (and (sra (shl x)) A)
// where y is has a single bit set.
// A plaintext description would be, we can turn the SELECT_CC into an AND
Expand Down
16 changes: 7 additions & 9 deletions llvm/test/CodeGen/RISCV/pr148084.ll
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ define fastcc void @search_tx_type() #0 {
; CHECK: # %bb.0: # %._crit_edge.i
; CHECK-NEXT: # %bb.1: # %bb
; CHECK-NEXT: lbu a1, 0(zero)
; CHECK-NEXT: lw a0, 0(zero)
; CHECK-NEXT: lh a2, 0(zero)
; CHECK-NEXT: lw a0, 0(zero)
; CHECK-NEXT: seqz a1, a1
; CHECK-NEXT: srai a3, a0, 63
; CHECK-NEXT: addi a1, a1, -1
; CHECK-NEXT: and a1, a1, a2
; CHECK-NEXT: andi a2, a1, 1
; CHECK-NEXT: addi a2, a2, -1
; CHECK-NEXT: or a3, a3, a0
; CHECK-NEXT: slli a2, a1, 63
; CHECK-NEXT: srai a2, a2, 63
; CHECK-NEXT: srai a3, a0, 63
; CHECK-NEXT: or a2, a2, a3
; CHECK-NEXT: bgez a2, .LBB0_3
; CHECK-NEXT: # %bb.2:
Expand All @@ -36,14 +35,13 @@ define fastcc void @search_tx_type() #0 {
; CHECK-NEXT: # %bb.6: # %bb
; CHECK-NEXT: mv a3, a2
; CHECK-NEXT: .LBB0_7: # %bb
; CHECK-NEXT: andi a5, a1, 8
; CHECK-NEXT: sext.w a4, a3
; CHECK-NEXT: andi a4, a1, 8
; CHECK-NEXT: mv a2, a3
; CHECK-NEXT: beqz a5, .LBB0_9
; CHECK-NEXT: beqz a4, .LBB0_9
; CHECK-NEXT: # %bb.8: # %bb
; CHECK-NEXT: mv a2, a0
; CHECK-NEXT: .LBB0_9: # %bb
; CHECK-NEXT: blt a4, a0, .LBB0_11
; CHECK-NEXT: blt a3, a0, .LBB0_11
; CHECK-NEXT: # %bb.10: # %bb
; CHECK-NEXT: mv a2, a3
; CHECK-NEXT: .LBB0_11: # %bb
Expand Down