Skip to content

Commit e90c6f5

Browse files
Alexandru Octavian ButiuKai Luo
authored andcommitted
[MachineCopyPropagation] Fix differences in code gen when compiling with -g
Fixes bugs [[ https://bugs.llvm.org/show_bug.cgi?id=50580 | 50580 ]] and [[ https://bugs.llvm.org/show_bug.cgi?id=49446 | 49446 ]] When compiling with -g "DBG_VALUE <reg>" instructions are added in the MIR, if such a instruction is inserted between instructions that use <reg> then MachineCopyPropagation invalidates <reg> , this causes some copies to not be propagated and causes differences in code generation (ex bugs 50580 and 49446 ). DBG_VALUE instructions should be ignored since they don't actually modify the register. Reviewed By: lkail Differential Revision: https://reviews.llvm.org/D104394
1 parent c142c06 commit e90c6f5

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

llvm/lib/CodeGen/MachineCopyPropagation.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,32 @@ void MachineCopyPropagation::BackwardCopyPropagateBlock(
870870
if (MO.isDef())
871871
Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
872872

873-
if (MO.readsReg())
874-
Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
873+
if (MO.readsReg()) {
874+
if (MO.isDebug()) {
875+
// Check if the register in the debug instruction is utilized
876+
// in a copy instruction, so we can update the debug info if the
877+
// register is changed.
878+
for (MCRegUnitIterator RUI(MO.getReg().asMCReg(), TRI); RUI.isValid();
879+
++RUI) {
880+
if (auto *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
881+
CopyDbgUsers[Copy].insert(MI);
882+
}
883+
}
884+
} else {
885+
Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
886+
}
887+
}
875888
}
876889
}
877890

878891
for (auto *Copy : MaybeDeadCopies) {
892+
893+
Register Src = Copy->getOperand(1).getReg();
894+
Register Def = Copy->getOperand(0).getReg();
895+
SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
896+
CopyDbgUsers[Copy].end());
897+
898+
MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
879899
Copy->eraseFromParent();
880900
++NumDeletes;
881901
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# RUN: llc -mtriple=i686-- -run-pass machine-cp -verify-machineinstrs -o - %s | FileCheck %s
2+
3+
4+
---
5+
# Test that machine copy propagation ignores DBG_VALUE and DBL_VALUE_LIST and updates it.
6+
# CHECK-LABEL: name: foo
7+
# CHECK: bb.0:
8+
# CHECK-NEXT: $rax = MOV64ri 31
9+
# CHECK-NEXT: DBG_VALUE $rax
10+
# CHECK-NEXT: DBG_VALUE_LIST 0, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 4, DW_OP_mul, DW_OP_plus, DW_OP_stack_value), $rax, 0, 0
11+
# CHECK-NEXT: RETQ implicit killed $rax
12+
name: foo
13+
body: |
14+
bb.0:
15+
renamable $rcx = MOV64ri 31
16+
DBG_VALUE $rcx, 0, 0, 0, 0
17+
DBG_VALUE_LIST 0, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_constu, 4, DW_OP_mul, DW_OP_plus, DW_OP_stack_value), $rcx, 0, 0
18+
$rax = COPY killed renamable $rcx
19+
RETQ implicit killed $rax
20+
...

0 commit comments

Comments
 (0)