From 8898cd8dcf71e03349bc0c7f9d50a0bf6af573bc Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Mon, 25 Sep 2017 16:14:53 +0000 Subject: [DebugInfo] Sort the SDDbgValue list before assuming it is in IR order Summary: This code iterates the 'Orders' vector in parallel with the DbgValue list, emitting all DBG_VALUEs that occurred between the last IR order insertion point and the next insertion point. This assumes the SDDbgValue list is sorted in IR order, which it usually is. However, it is not sorted when a node with a debug value is replaced with another one. When this happens, TransferDbgValues is called, and the new value is added to the end of the list. The problem can be solved by stably sorting the list by IR order. Reviewers: aprantl, Ka-Ka Reviewed By: aprantl Subscribers: MatzeB, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D38197 llvm-svn: 314114 --- .../CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp') diff --git a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp index 9fdb3c85602..6eebba19e5c 100644 --- a/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGSDNodes.cpp @@ -857,8 +857,13 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) { MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI(); // Sort the source order instructions and use the order to insert debug - // values. - std::sort(Orders.begin(), Orders.end(), less_first()); + // values. Use stable_sort so that DBG_VALUEs are inserted in the same order + // regardless of the host's implementation fo std::sort. + std::stable_sort(Orders.begin(), Orders.end(), less_first()); + std::stable_sort(DAG->DbgBegin(), DAG->DbgEnd(), + [](const SDDbgValue *LHS, const SDDbgValue *RHS) { + return LHS->getOrder() < RHS->getOrder(); + }); SDDbgInfo::DbgIterator DI = DAG->DbgBegin(); SDDbgInfo::DbgIterator DE = DAG->DbgEnd(); @@ -870,10 +875,12 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) { // Insert all SDDbgValue's whose order(s) are before "Order". if (!MI) continue; - for (; DI != DE && - (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) { + for (; DI != DE; ++DI) { + if ((*DI)->getOrder() < LastOrder || (*DI)->getOrder() >= Order) + break; if ((*DI)->isInvalidated()) continue; + MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap); if (DbgMI) { if (!LastOrder) @@ -892,11 +899,13 @@ EmitSchedule(MachineBasicBlock::iterator &InsertPos) { // Add trailing DbgValue's before the terminator. FIXME: May want to add // some of them before one or more conditional branches? SmallVector DbgMIs; - while (DI != DE) { - if (!(*DI)->isInvalidated()) - if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap)) - DbgMIs.push_back(DbgMI); - ++DI; + for (; DI != DE; ++DI) { + if ((*DI)->isInvalidated()) + continue; + assert((*DI)->getOrder() >= LastOrder && + "emitting DBG_VALUE out of order"); + if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap)) + DbgMIs.push_back(DbgMI); } MachineBasicBlock *InsertBB = Emitter.getBlock(); -- cgit v1.2.3