Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9280,11 +9280,21 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
}
}

const int TrueValCost = RISCVMatInt::getIntMatCost(
TrueVal, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
const int FalseValCost = RISCVMatInt::getIntMatCost(
FalseVal, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
bool IsCZERO_NEZ = TrueValCost <= FalseValCost;
auto getCost = [&](APInt Delta, APInt Addend) {
const int DeltaCost = RISCVMatInt::getIntMatCost(
Delta, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
// Dos the addend folds into an ADDI
if (Addend.isSignedIntN(12))
return DeltaCost;
const int AddendCost = RISCVMatInt::getIntMatCost(
Addend, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
// Panalize the ADD slightly so that we prefer to end with an ADDI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Panalize the ADD slightly so that we prefer to end with an ADDI
// Penalize the ADD slightly so that we prefer to end with an ADDI

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this bit to a separate PR to reduce the conceptual moving pieces.

// if costs are otherwise equal. This helps to expose the immediate
// for possible folding into a dependent memory instruction.
return AddendCost + DeltaCost + 1;
};
bool IsCZERO_NEZ = getCost(FalseVal - TrueVal, TrueVal) <=
getCost(TrueVal - FalseVal, FalseVal);
SDValue LHSVal = DAG.getConstant(
IsCZERO_NEZ ? FalseVal - TrueVal : TrueVal - FalseVal, DL, VT);
SDValue RHSVal =
Expand Down
14 changes: 6 additions & 8 deletions llvm/test/CodeGen/RISCV/select-const.ll
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,10 @@ define i32 @select_nonnegative_lui_addi(i32 signext %x) {
; RV32ZICOND-LABEL: select_nonnegative_lui_addi:
; RV32ZICOND: # %bb.0:
; RV32ZICOND-NEXT: srli a0, a0, 31
; RV32ZICOND-NEXT: lui a1, 1048572
; RV32ZICOND-NEXT: addi a1, a1, 25
; RV32ZICOND-NEXT: czero.eqz a0, a1, a0
; RV32ZICOND-NEXT: lui a1, 4
; RV32ZICOND-NEXT: add a0, a0, a1
; RV32ZICOND-NEXT: addi a1, a1, -25
; RV32ZICOND-NEXT: czero.nez a0, a1, a0
; RV32ZICOND-NEXT: addi a0, a0, 25
; RV32ZICOND-NEXT: ret
;
; RV64I-LABEL: select_nonnegative_lui_addi:
Expand All @@ -536,11 +535,10 @@ define i32 @select_nonnegative_lui_addi(i32 signext %x) {
; RV64ZICOND-LABEL: select_nonnegative_lui_addi:
; RV64ZICOND: # %bb.0:
; RV64ZICOND-NEXT: srli a0, a0, 63
; RV64ZICOND-NEXT: lui a1, 1048572
; RV64ZICOND-NEXT: addi a1, a1, 25
; RV64ZICOND-NEXT: czero.eqz a0, a1, a0
; RV64ZICOND-NEXT: lui a1, 4
; RV64ZICOND-NEXT: add a0, a0, a1
; RV64ZICOND-NEXT: addi a1, a1, -25
; RV64ZICOND-NEXT: czero.nez a0, a1, a0
; RV64ZICOND-NEXT: addi a0, a0, 25
; RV64ZICOND-NEXT: ret
%cmp = icmp sgt i32 %x, -1
%cond = select i1 %cmp, i32 16384, i32 25
Expand Down
Loading