Skip to content

Commit 93bfcbb

Browse files
committed
[RISCV] Account for ADDI immediate range in select of two constants with zicond
When choosing to materialize a select of two constants using zicond, we have a choice of which direction to compute the delta. The prior cost was looking only at the cost of the values without accounting for the fact it's actually the delta which is the highest cost and that sometimes the addend can fold into an addi. Note that the addi is also preferrable since it might be folded into a dependent memory instruction if e.g. we're doing a load from a select of two constant addresses.
1 parent 8cf2d27 commit 93bfcbb

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9280,11 +9280,21 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
92809280
}
92819281
}
92829282

9283-
const int TrueValCost = RISCVMatInt::getIntMatCost(
9284-
TrueVal, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
9285-
const int FalseValCost = RISCVMatInt::getIntMatCost(
9286-
FalseVal, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
9287-
bool IsCZERO_NEZ = TrueValCost <= FalseValCost;
9283+
auto getCost = [&](APInt Delta, APInt Addend) {
9284+
const int DeltaCost = RISCVMatInt::getIntMatCost(
9285+
Delta, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
9286+
// Dos the addend folds into an ADDI
9287+
if (Addend.isSignedIntN(12))
9288+
return DeltaCost;
9289+
const int AddendCost = RISCVMatInt::getIntMatCost(
9290+
Addend, Subtarget.getXLen(), Subtarget, /*CompressionCost=*/true);
9291+
// Panalize the ADD slightly so that we prefer to end with an ADDI
9292+
// if costs are otherwise equal. This helps to expose the immediate
9293+
// for possible folding into a dependent memory instruction.
9294+
return AddendCost + DeltaCost + 1;
9295+
};
9296+
bool IsCZERO_NEZ = getCost(FalseVal - TrueVal, TrueVal) <=
9297+
getCost(TrueVal - FalseVal, FalseVal);
92889298
SDValue LHSVal = DAG.getConstant(
92899299
IsCZERO_NEZ ? FalseVal - TrueVal : TrueVal - FalseVal, DL, VT);
92909300
SDValue RHSVal =

llvm/test/CodeGen/RISCV/select-const.ll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,10 @@ define i32 @select_nonnegative_lui_addi(i32 signext %x) {
506506
; RV32ZICOND-LABEL: select_nonnegative_lui_addi:
507507
; RV32ZICOND: # %bb.0:
508508
; RV32ZICOND-NEXT: srli a0, a0, 31
509-
; RV32ZICOND-NEXT: lui a1, 1048572
510-
; RV32ZICOND-NEXT: addi a1, a1, 25
511-
; RV32ZICOND-NEXT: czero.eqz a0, a1, a0
512509
; RV32ZICOND-NEXT: lui a1, 4
513-
; RV32ZICOND-NEXT: add a0, a0, a1
510+
; RV32ZICOND-NEXT: addi a1, a1, -25
511+
; RV32ZICOND-NEXT: czero.nez a0, a1, a0
512+
; RV32ZICOND-NEXT: addi a0, a0, 25
514513
; RV32ZICOND-NEXT: ret
515514
;
516515
; RV64I-LABEL: select_nonnegative_lui_addi:
@@ -536,11 +535,10 @@ define i32 @select_nonnegative_lui_addi(i32 signext %x) {
536535
; RV64ZICOND-LABEL: select_nonnegative_lui_addi:
537536
; RV64ZICOND: # %bb.0:
538537
; RV64ZICOND-NEXT: srli a0, a0, 63
539-
; RV64ZICOND-NEXT: lui a1, 1048572
540-
; RV64ZICOND-NEXT: addi a1, a1, 25
541-
; RV64ZICOND-NEXT: czero.eqz a0, a1, a0
542538
; RV64ZICOND-NEXT: lui a1, 4
543-
; RV64ZICOND-NEXT: add a0, a0, a1
539+
; RV64ZICOND-NEXT: addi a1, a1, -25
540+
; RV64ZICOND-NEXT: czero.nez a0, a1, a0
541+
; RV64ZICOND-NEXT: addi a0, a0, 25
544542
; RV64ZICOND-NEXT: ret
545543
%cmp = icmp sgt i32 %x, -1
546544
%cond = select i1 %cmp, i32 16384, i32 25

0 commit comments

Comments
 (0)