Skip to content

Commit a2d717d

Browse files
authored
[RISCV] Prevent copying dummy_reg_pair_with_x0 in RISCVMakeCompressible. (#141261)
dummy_reg_pair_with_x0 is the odd subregister of X0_Pair, but it isn't a real register. We need to copy X0 instead since X0_Pair reads as 0.
1 parent 7b4d2a0 commit a2d717d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,21 @@ bool RISCVMakeCompressibleOpt::runOnMachineFunction(MachineFunction &Fn) {
452452
.addReg(RegImm.Reg);
453453
} else if (RISCV::GPRPairRegClass.contains(RegImm.Reg)) {
454454
assert(RegImm.Imm == 0);
455+
MCRegister EvenReg = TRI.getSubReg(RegImm.Reg, RISCV::sub_gpr_even);
456+
MCRegister OddReg;
457+
// We need to special case odd reg for X0_PAIR.
458+
if (RegImm.Reg == RISCV::X0_Pair)
459+
OddReg = RISCV::X0;
460+
else
461+
OddReg = TRI.getSubReg(RegImm.Reg, RISCV::sub_gpr_odd);
462+
assert(NewReg != RISCV::X0_Pair && "Cannot write to X0_Pair");
455463
BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::ADDI),
456464
TRI.getSubReg(NewReg, RISCV::sub_gpr_even))
457-
.addReg(TRI.getSubReg(RegImm.Reg, RISCV::sub_gpr_even))
465+
.addReg(EvenReg)
458466
.addImm(0);
459467
BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::ADDI),
460468
TRI.getSubReg(NewReg, RISCV::sub_gpr_odd))
461-
.addReg(TRI.getSubReg(RegImm.Reg, RISCV::sub_gpr_odd))
469+
.addReg(OddReg)
462470
.addImm(0);
463471
} else {
464472
assert((RISCV::FPR32RegClass.contains(RegImm.Reg) ||

llvm/test/CodeGen/RISCV/make-compressible-zilsd.mir

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
ret void
1111
}
1212

13+
define void @store_common_value_double_zero(ptr %a, ptr %b, ptr %c) #0 {
14+
entry:
15+
store double 0.0, ptr %a, align 8
16+
store double 0.0, ptr %b, align 8
17+
store double 0.0, ptr %c, align 8
18+
ret void
19+
}
20+
1321
define void @store_common_ptr_double(double %a, double %b, double %d, ptr %p) #0 {
1422
entry:
1523
store volatile double %a, ptr %p, align 8
@@ -117,6 +125,28 @@ body: |
117125
SD_RV32 killed renamable $x16_x17, killed renamable $x12, 0 :: (store (s64) into %ir.c)
118126
PseudoRET
119127
128+
...
129+
---
130+
name: store_common_value_double_zero
131+
tracksRegLiveness: true
132+
body: |
133+
bb.0.entry:
134+
liveins: $x10, $x11, $x12
135+
136+
; RV32-LABEL: name: store_common_value_double_zero
137+
; RV32: liveins: $x10, $x11, $x12
138+
; RV32-NEXT: {{ $}}
139+
; RV32-NEXT: $x14 = ADDI $x0, 0
140+
; RV32-NEXT: $x15 = ADDI $x0, 0
141+
; RV32-NEXT: SD_RV32 $x14_x15, killed renamable $x10, 0 :: (store (s64) into %ir.a)
142+
; RV32-NEXT: SD_RV32 $x14_x15, killed renamable $x11, 0 :: (store (s64) into %ir.b)
143+
; RV32-NEXT: SD_RV32 $x14_x15, killed renamable $x12, 0 :: (store (s64) into %ir.c)
144+
; RV32-NEXT: PseudoRET
145+
SD_RV32 $x0_pair, killed renamable $x10, 0 :: (store (s64) into %ir.a)
146+
SD_RV32 $x0_pair, killed renamable $x11, 0 :: (store (s64) into %ir.b)
147+
SD_RV32 $x0_pair, killed renamable $x12, 0 :: (store (s64) into %ir.c)
148+
PseudoRET
149+
120150
...
121151
---
122152
name: store_common_ptr_double

0 commit comments

Comments
 (0)