diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -9527,6 +9527,19 @@ static bool combine_CC(SDValue &LHS, SDValue &RHS, SDValue &CC, const SDLoc &DL, SelectionDAG &DAG, const RISCVSubtarget &Subtarget) { ISD::CondCode CCVal = cast(CC)->get(); + + // As far as arithmetic right shift always saves the sign, + // shift can be omitted. + // Fold setlt (sra X, N), 0 -> setlt X, 0 and + // setge (sra X, N), 0 -> setge X, 0 + if (auto *RHSConst = dyn_cast(RHS.getNode())) { + if ((CCVal == ISD::SETGE || CCVal == ISD::SETLT) && + LHS.getOpcode() == ISD::SRA && RHSConst->isZero()) { + LHS = LHS.getOperand(0); + return true; + } + } + if (!ISD::isIntEqualitySetCC(CCVal)) return false; diff --git a/llvm/test/CodeGen/RISCV/branch_zero.ll b/llvm/test/CodeGen/RISCV/branch_zero.ll --- a/llvm/test/CodeGen/RISCV/branch_zero.ll +++ b/llvm/test/CodeGen/RISCV/branch_zero.ll @@ -8,7 +8,6 @@ ; CHECK-NEXT: .LBB0_1: # %for.body ; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 ; CHECK-NEXT: slli a0, a0, 48 -; CHECK-NEXT: srai a0, a0, 48 ; CHECK-NEXT: bltz a0, .LBB0_4 ; CHECK-NEXT: # %bb.2: # %while.cond.preheader.i ; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1 @@ -50,7 +49,6 @@ ; CHECK-NEXT: .LBB1_1: # %for.body ; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 ; CHECK-NEXT: slli a0, a0, 48 -; CHECK-NEXT: srai a0, a0, 48 ; CHECK-NEXT: bgez a0, .LBB1_4 ; CHECK-NEXT: # %bb.2: # %while.cond.preheader.i ; CHECK-NEXT: # in Loop: Header=BB1_1 Depth=1