Skip to content

Commit

Permalink
[RISCV] Fix wrong register rename for store value during make-compres…
Browse files Browse the repository at this point in the history
…sible optimization

Current implementation will rename both register in store instructions if
we store base address into memory with same base register, it's OK if
the offset is 0, however that is wrong transform if offset isn't 0, give
a smalle example here:

sd      a0, 808(a0)

We should not transform into:

addi    a2, a0, 768
sd      a2, 40(a2)

That should just rename base address like this:

addi    a2, a0, 768
sd      a0, 40(a2)

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D128876
  • Loading branch information
kito-cheng committed Jul 8, 2022
1 parent de3b5d7 commit 5c45ae4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion llvm/lib/Target/RISCV/RISCVMakeCompressible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,16 @@ static void updateOperands(MachineInstr &MI, RegImmPair OldRegImm,
assert((isCompressibleLoad(MI) || isCompressibleStore(MI)) &&
"Unsupported instruction for this optimization.");

int SkipN = 0;

// Skip the first (value) operand to a store instruction (except if the store
// offset is zero) in order to avoid an incorrect transformation.
// e.g. sd a0, 808(a0) to addi a2, a0, 768; sd a2, 40(a2)
if (isCompressibleStore(MI) && OldRegImm.Imm != 0)
SkipN = 1;

// Update registers
for (MachineOperand &MO : MI.operands())
for (MachineOperand &MO : drop_begin(MI.operands(), SkipN))
if (MO.isReg() && MO.getReg() == OldRegImm.Reg) {
// Do not update operands that define the old register.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ body: |
; CHECK-NEXT: renamable $x11 = ADDI $x0, 1
; CHECK-NEXT: $x12 = ADDI $x10, 768
; CHECK-NEXT: SD killed renamable $x11, $x12, 32 :: (store (s64) into %ir.1)
; CHECK-NEXT: SD $x12, $x12, 40 :: (store (s64) into %ir.2)
; CHECK-NEXT: SD renamable $x10, $x12, 40 :: (store (s64) into %ir.2)
; CHECK-NEXT: renamable $x11 = ADDI $x0, 2
; CHECK-NEXT: SD killed renamable $x11, killed $x12, 48 :: (store (s64) into %ir.3)
; CHECK-NEXT: PseudoRET
Expand Down

0 comments on commit 5c45ae4

Please sign in to comment.