Skip to content

Commit 2c18570

Browse files
authored
[RISCV] Remove setJumpIsExpensive(). (#74647)
Middle end up optimizations can speculate away the short circuit behavior of C/C++ && and ||. Using i1 and/or or logical select instructions and a single branch. SelectionDAGBuilder can turn i1 and/or/select back into multiple branches, but this is disabled when jump is expensive. RISC-V can use slt(u)(i) to evaluate a condition into any GPR which makes us better than other targets that use a flag register. RISC-V also has single instruction compare and branch. So its not clear from a code size perspective that using compare+and/or is better. If the full condition is dependent on multiple loads, using a logic delays the branch resolution until all the loads are resolved even if there is a cheap condition that makes the loads unnecessary. PowerPC and Lanai are the only CPU targets that use setJumpIsExpensive. NVPTX and AMDGPU also use it but they are GPU targets. PowerPC appears to have a MachineIR pass that turns AND/OR of CR bits into multiple branches. I don't know anything about Lanai and their reason for using setJumpIsExpensive. I think the decision to use logic vs branches is much more nuanced than this big hammer. So I propose to make RISC-V match other CPU targets. Anyone who wants the old behavior can still pass -mllvm -jump-is-expensive=true.
1 parent 5540d81 commit 2c18570

File tree

6 files changed

+316
-412
lines changed

6 files changed

+316
-412
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,6 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
13631363
setPrefFunctionAlignment(Subtarget.getPrefFunctionAlignment());
13641364
setPrefLoopAlignment(Subtarget.getPrefLoopAlignment());
13651365

1366-
// Jumps are expensive, compared to logic
1367-
setJumpIsExpensive();
1368-
13691366
setTargetDAGCombine({ISD::INTRINSIC_VOID, ISD::INTRINSIC_W_CHAIN,
13701367
ISD::INTRINSIC_WO_CHAIN, ISD::ADD, ISD::SUB, ISD::AND,
13711368
ISD::OR, ISD::XOR, ISD::SETCC, ISD::SELECT});

llvm/test/CodeGen/RISCV/double-previous-failure.ll

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ define i32 @main() nounwind {
3131
; RV32IFD-NEXT: fld fa5, 0(sp)
3232
; RV32IFD-NEXT: lui a0, %hi(.LCPI1_0)
3333
; RV32IFD-NEXT: fld fa4, %lo(.LCPI1_0)(a0)
34-
; RV32IFD-NEXT: lui a0, %hi(.LCPI1_1)
35-
; RV32IFD-NEXT: fld fa3, %lo(.LCPI1_1)(a0)
3634
; RV32IFD-NEXT: flt.d a0, fa5, fa4
37-
; RV32IFD-NEXT: flt.d a1, fa3, fa5
38-
; RV32IFD-NEXT: or a0, a0, a1
39-
; RV32IFD-NEXT: beqz a0, .LBB1_2
40-
; RV32IFD-NEXT: # %bb.1: # %if.then
41-
; RV32IFD-NEXT: call abort@plt
42-
; RV32IFD-NEXT: .LBB1_2: # %if.end
35+
; RV32IFD-NEXT: bnez a0, .LBB1_3
36+
; RV32IFD-NEXT: # %bb.1: # %entry
37+
; RV32IFD-NEXT: lui a0, %hi(.LCPI1_1)
38+
; RV32IFD-NEXT: fld fa4, %lo(.LCPI1_1)(a0)
39+
; RV32IFD-NEXT: flt.d a0, fa4, fa5
40+
; RV32IFD-NEXT: bnez a0, .LBB1_3
41+
; RV32IFD-NEXT: # %bb.2: # %if.end
4342
; RV32IFD-NEXT: call exit@plt
43+
; RV32IFD-NEXT: .LBB1_3: # %if.then
44+
; RV32IFD-NEXT: call abort@plt
4445
;
4546
; RV32IZFINXZDINX-LABEL: main:
4647
; RV32IZFINXZDINX: # %bb.0: # %entry
@@ -56,17 +57,18 @@ define i32 @main() nounwind {
5657
; RV32IZFINXZDINX-NEXT: lui a2, %hi(.LCPI1_0)
5758
; RV32IZFINXZDINX-NEXT: lw a3, %lo(.LCPI1_0+4)(a2)
5859
; RV32IZFINXZDINX-NEXT: lw a2, %lo(.LCPI1_0)(a2)
59-
; RV32IZFINXZDINX-NEXT: lui a4, %hi(.LCPI1_1)
60-
; RV32IZFINXZDINX-NEXT: lw a5, %lo(.LCPI1_1+4)(a4)
61-
; RV32IZFINXZDINX-NEXT: lw a4, %lo(.LCPI1_1)(a4)
6260
; RV32IZFINXZDINX-NEXT: flt.d a2, a0, a2
63-
; RV32IZFINXZDINX-NEXT: flt.d a0, a4, a0
64-
; RV32IZFINXZDINX-NEXT: or a0, a2, a0
65-
; RV32IZFINXZDINX-NEXT: beqz a0, .LBB1_2
66-
; RV32IZFINXZDINX-NEXT: # %bb.1: # %if.then
67-
; RV32IZFINXZDINX-NEXT: call abort@plt
68-
; RV32IZFINXZDINX-NEXT: .LBB1_2: # %if.end
61+
; RV32IZFINXZDINX-NEXT: bnez a2, .LBB1_3
62+
; RV32IZFINXZDINX-NEXT: # %bb.1: # %entry
63+
; RV32IZFINXZDINX-NEXT: lui a2, %hi(.LCPI1_1)
64+
; RV32IZFINXZDINX-NEXT: lw a3, %lo(.LCPI1_1+4)(a2)
65+
; RV32IZFINXZDINX-NEXT: lw a2, %lo(.LCPI1_1)(a2)
66+
; RV32IZFINXZDINX-NEXT: flt.d a0, a2, a0
67+
; RV32IZFINXZDINX-NEXT: bnez a0, .LBB1_3
68+
; RV32IZFINXZDINX-NEXT: # %bb.2: # %if.end
6969
; RV32IZFINXZDINX-NEXT: call exit@plt
70+
; RV32IZFINXZDINX-NEXT: .LBB1_3: # %if.then
71+
; RV32IZFINXZDINX-NEXT: call abort@plt
7072
entry:
7173
%call = call double @test(double 2.000000e+00)
7274
%cmp = fcmp olt double %call, 2.400000e-01

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ define signext i32 @if_of_and(i1 zeroext %a, i1 zeroext %b) nounwind {
4040
; RV32I: # %bb.0:
4141
; RV32I-NEXT: addi sp, sp, -16
4242
; RV32I-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
43-
; RV32I-NEXT: and a0, a0, a1
44-
; RV32I-NEXT: beqz a0, .LBB1_2
45-
; RV32I-NEXT: # %bb.1: # %if.then
43+
; RV32I-NEXT: beqz a0, .LBB1_3
44+
; RV32I-NEXT: # %bb.1:
45+
; RV32I-NEXT: beqz a1, .LBB1_3
46+
; RV32I-NEXT: # %bb.2: # %if.then
4647
; RV32I-NEXT: call both@plt
47-
; RV32I-NEXT: j .LBB1_3
48-
; RV32I-NEXT: .LBB1_2: # %if.else
48+
; RV32I-NEXT: j .LBB1_4
49+
; RV32I-NEXT: .LBB1_3: # %if.else
4950
; RV32I-NEXT: call neither@plt
50-
; RV32I-NEXT: .LBB1_3: # %if.end
51+
; RV32I-NEXT: .LBB1_4: # %if.end
5152
; RV32I-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
5253
; RV32I-NEXT: addi sp, sp, 16
5354
; RV32I-NEXT: ret
@@ -56,14 +57,15 @@ define signext i32 @if_of_and(i1 zeroext %a, i1 zeroext %b) nounwind {
5657
; RV64I: # %bb.0:
5758
; RV64I-NEXT: addi sp, sp, -16
5859
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
59-
; RV64I-NEXT: and a0, a0, a1
60-
; RV64I-NEXT: beqz a0, .LBB1_2
61-
; RV64I-NEXT: # %bb.1: # %if.then
60+
; RV64I-NEXT: beqz a0, .LBB1_3
61+
; RV64I-NEXT: # %bb.1:
62+
; RV64I-NEXT: beqz a1, .LBB1_3
63+
; RV64I-NEXT: # %bb.2: # %if.then
6264
; RV64I-NEXT: call both@plt
63-
; RV64I-NEXT: j .LBB1_3
64-
; RV64I-NEXT: .LBB1_2: # %if.else
65+
; RV64I-NEXT: j .LBB1_4
66+
; RV64I-NEXT: .LBB1_3: # %if.else
6567
; RV64I-NEXT: call neither@plt
66-
; RV64I-NEXT: .LBB1_3: # %if.end
68+
; RV64I-NEXT: .LBB1_4: # %if.end
6769
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
6870
; RV64I-NEXT: addi sp, sp, 16
6971
; RV64I-NEXT: ret

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ define signext i32 @if_of_or(i1 zeroext %a, i1 zeroext %b) nounwind {
4040
; RV32I: # %bb.0:
4141
; RV32I-NEXT: addi sp, sp, -16
4242
; RV32I-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
43-
; RV32I-NEXT: or a0, a0, a1
44-
; RV32I-NEXT: beqz a0, .LBB1_2
45-
; RV32I-NEXT: # %bb.1: # %if.then
46-
; RV32I-NEXT: call either@plt
47-
; RV32I-NEXT: j .LBB1_3
48-
; RV32I-NEXT: .LBB1_2: # %if.else
43+
; RV32I-NEXT: bnez a0, .LBB1_3
44+
; RV32I-NEXT: # %bb.1:
45+
; RV32I-NEXT: bnez a1, .LBB1_3
46+
; RV32I-NEXT: # %bb.2: # %if.else
4947
; RV32I-NEXT: call neither@plt
50-
; RV32I-NEXT: .LBB1_3: # %if.end
48+
; RV32I-NEXT: j .LBB1_4
49+
; RV32I-NEXT: .LBB1_3: # %if.then
50+
; RV32I-NEXT: call either@plt
51+
; RV32I-NEXT: .LBB1_4: # %if.end
5152
; RV32I-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
5253
; RV32I-NEXT: addi sp, sp, 16
5354
; RV32I-NEXT: ret
@@ -56,14 +57,15 @@ define signext i32 @if_of_or(i1 zeroext %a, i1 zeroext %b) nounwind {
5657
; RV64I: # %bb.0:
5758
; RV64I-NEXT: addi sp, sp, -16
5859
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
59-
; RV64I-NEXT: or a0, a0, a1
60-
; RV64I-NEXT: beqz a0, .LBB1_2
61-
; RV64I-NEXT: # %bb.1: # %if.then
62-
; RV64I-NEXT: call either@plt
63-
; RV64I-NEXT: j .LBB1_3
64-
; RV64I-NEXT: .LBB1_2: # %if.else
60+
; RV64I-NEXT: bnez a0, .LBB1_3
61+
; RV64I-NEXT: # %bb.1:
62+
; RV64I-NEXT: bnez a1, .LBB1_3
63+
; RV64I-NEXT: # %bb.2: # %if.else
6564
; RV64I-NEXT: call neither@plt
66-
; RV64I-NEXT: .LBB1_3: # %if.end
65+
; RV64I-NEXT: j .LBB1_4
66+
; RV64I-NEXT: .LBB1_3: # %if.then
67+
; RV64I-NEXT: call either@plt
68+
; RV64I-NEXT: .LBB1_4: # %if.end
6769
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
6870
; RV64I-NEXT: addi sp, sp, 16
6971
; RV64I-NEXT: ret

0 commit comments

Comments
 (0)