Skip to content

Commit e297006

Browse files
committed
[ScheduleDAG] Move DBG_VALUEs after first term forward.
MBBs are not allowed to have non-terminator instructions after the first terminator. Currently in some cases (see the modified test), EmitSchedule can add DBG_VALUEs after the last terminator, for example when referring a debug value that gets folded into a TCRETURN instruction on ARM. This patch updates EmitSchedule to move inserted DBG_VALUEs just before the first terminator. I am not sure if there are terminators produce values that can in turn be used by a DBG_VALUE. In that case, moving the DBG_VALUE might result in referencing an undefined register. But in any case, it seems like currently there is no way to insert a proper DBG_VALUEs for such registers anyways. Alternatively it might make sense to just remove those extra DBG_VALUES. I am not too familiar with the details of debug info in the backend and would appreciate any suggestions on how to address the issue in the best possible way. Reviewers: vsk, aprantl, jpaquette, efriedma, paquette Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D83561
1 parent 650baf2 commit e297006

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ class MachineInstr
280280
const MachineBasicBlock* getParent() const { return Parent; }
281281
MachineBasicBlock* getParent() { return Parent; }
282282

283+
/// Move the instruction before \p MovePos.
284+
void moveBefore(MachineInstr *MovePos);
285+
283286
/// Return the function that contains the basic block that this instruction
284287
/// belongs to.
285288
///

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
147147
setFlags(MI.Flags);
148148
}
149149

150+
void MachineInstr::moveBefore(MachineInstr *MovePos) {
151+
MovePos->getParent()->splice(MovePos, getParent(), getIterator());
152+
}
153+
150154
/// getRegInfo - If this instruction is embedded into a MachineFunction,
151155
/// return the MachineRegisterInfo object for the current function, otherwise
152156
/// return null.

llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,29 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
10341034
}
10351035

10361036
InsertPos = Emitter.getInsertPos();
1037-
return Emitter.getBlock();
1037+
// In some cases, DBG_VALUEs might be inserted after the first terminator,
1038+
// which results in an invalid MBB. If that happens, move the DBG_VALUEs
1039+
// before the first terminator.
1040+
MachineBasicBlock *InsertBB = Emitter.getBlock();
1041+
auto FirstTerm = InsertBB->getFirstTerminator();
1042+
if (FirstTerm != InsertBB->end()) {
1043+
assert(!FirstTerm->isDebugValue() &&
1044+
"first terminator cannot be a debug value");
1045+
for (MachineInstr &MI : make_early_inc_range(
1046+
make_range(std::next(FirstTerm), InsertBB->end()))) {
1047+
if (!MI.isDebugValue())
1048+
continue;
1049+
1050+
if (&MI == InsertPos)
1051+
InsertPos = std::prev(InsertPos->getIterator());
1052+
1053+
// The DBG_VALUE was referencing a value produced by a terminator. By
1054+
// moving the DBG_VALUE, the referenced value also needs invalidating.
1055+
MI.getOperand(0).ChangeToRegister(0, false);
1056+
MI.moveBefore(&*FirstTerm);
1057+
}
1058+
}
1059+
return InsertBB;
10381060
}
10391061

10401062
/// Return the basic block label.

llvm/test/CodeGen/ARM/dbg-tcreturn.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; XFAIL: *
2-
; RUN: llc %s -o - -stop-after=finalize-isel -verify-machineinstr | FileCheck %s
1+
; RUN: llc %s -o - -stop-after=finalize-isel -verify-machineinstrs | FileCheck %s
32

43
target datalayout = "e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
54
target triple = "thumbv7-apple-ios7.0.0"
@@ -12,8 +11,8 @@ target triple = "thumbv7-apple-ios7.0.0"
1211
; CHECK-NEXT: %0:gpr = COPY $r0
1312
; CHECK-NEXT: $r0 = COPY %0
1413
; CHECK-NEXT: $r1 = COPY %1
15-
; CHECK-NEXT: TCRETURNdi &__divsi3, implicit $sp, implicit $r0, implicit $r1
1614
; CHECK-NEXT: DBG_VALUE $noreg, $noreg, !13, !DIExpression(), debug-location !16
15+
; CHECK-NEXT: TCRETURNdi &__divsi3, implicit $sp, implicit $r0, implicit $r1
1716

1817
define i32 @test(i32 %a1, i32 %a2) !dbg !5 {
1918
entry:

0 commit comments

Comments
 (0)