Skip to content

Commit 38c5b35

Browse files
topperctstellar
authored andcommitted
[RISCV] Make sure ADDI replacement in optimizeCondBranch has a virtual reg destination. (llvm#81938)
If it isn't virtual, we may extend the live range of the physical register past were it is valid. For example, across a call. Found while trying to enable -riscv-enable-sink-fold which enables some copy propagation in machine sink that led to ADDIs with physical register destinations. (cherry picked from commit feee627)
1 parent 023925b commit 38c5b35

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,8 @@ bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const {
12291229
MachineBasicBlock::reverse_iterator II(&MI), E = MBB->rend();
12301230
auto DefC1 = std::find_if(++II, E, [&](const MachineInstr &I) -> bool {
12311231
int64_t Imm;
1232-
return isLoadImm(&I, Imm) && Imm == C1;
1232+
return isLoadImm(&I, Imm) && Imm == C1 &&
1233+
I.getOperand(0).getReg().isVirtual();
12331234
});
12341235
if (DefC1 != E)
12351236
return DefC1->getOperand(0).getReg();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc %s -mtriple=riscv64 -run-pass=peephole-opt -o - | FileCheck %s
3+
4+
# Make sure we shouldn't replace the %2 ADDI with the $x10 ADDI since it has a
5+
# physical register destination.
6+
7+
--- |
8+
define void @foo(i32 signext %0) {
9+
tail call void @bar(i32 1)
10+
%2 = icmp ugt i32 %0, 1
11+
br i1 %2, label %3, label %4
12+
13+
3: ; preds = %1
14+
tail call void @bar(i32 3)
15+
ret void
16+
17+
4: ; preds = %1
18+
ret void
19+
}
20+
21+
declare void @bar(...)
22+
23+
...
24+
---
25+
name: foo
26+
tracksRegLiveness: true
27+
body: |
28+
; CHECK-LABEL: name: foo
29+
; CHECK: bb.0 (%ir-block.1):
30+
; CHECK-NEXT: successors: %bb.1(0x40000000), %bb.2(0x40000000)
31+
; CHECK-NEXT: liveins: $x10
32+
; CHECK-NEXT: {{ $}}
33+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
34+
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
35+
; CHECK-NEXT: $x10 = ADDI $x0, 1
36+
; CHECK-NEXT: PseudoCALL target-flags(riscv-call) @bar, csr_ilp32_lp64, implicit-def dead $x1, implicit $x10, implicit-def $x2
37+
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
38+
; CHECK-NEXT: [[ADDI:%[0-9]+]]:gpr = ADDI $x0, 2
39+
; CHECK-NEXT: BLTU [[COPY]], killed [[ADDI]], %bb.2
40+
; CHECK-NEXT: PseudoBR %bb.1
41+
; CHECK-NEXT: {{ $}}
42+
; CHECK-NEXT: bb.1 (%ir-block.3):
43+
; CHECK-NEXT: $x10 = ADDI $x0, 3
44+
; CHECK-NEXT: PseudoTAIL target-flags(riscv-call) @bar, implicit $x2, implicit $x10
45+
; CHECK-NEXT: {{ $}}
46+
; CHECK-NEXT: bb.2 (%ir-block.4):
47+
; CHECK-NEXT: PseudoRET
48+
bb.0 (%ir-block.1):
49+
successors: %bb.1, %bb.2
50+
liveins: $x10
51+
52+
%0:gpr = COPY $x10
53+
ADJCALLSTACKDOWN 0, 0, implicit-def dead $x2, implicit $x2
54+
$x10 = ADDI $x0, 1
55+
PseudoCALL target-flags(riscv-call) @bar, csr_ilp32_lp64, implicit-def dead $x1, implicit $x10, implicit-def $x2
56+
ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
57+
%2:gpr = ADDI $x0, 2
58+
BLTU %0, killed %2, %bb.2
59+
PseudoBR %bb.1
60+
61+
bb.1 (%ir-block.3):
62+
$x10 = ADDI $x0, 3
63+
PseudoTAIL target-flags(riscv-call) @bar, implicit $x2, implicit $x10
64+
65+
bb.2 (%ir-block.4):
66+
PseudoRET
67+
68+
...

0 commit comments

Comments
 (0)