Index: lib/CodeGen/MachineSink.cpp =================================================================== --- lib/CodeGen/MachineSink.cpp +++ lib/CodeGen/MachineSink.cpp @@ -753,6 +753,36 @@ MBP.LHS.getReg() == BaseReg; } +/// Sink an instruction and its associated debug instructions. +static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo, + MachineBasicBlock::iterator InsertPos) { + // Collect matching debug values. + SmallVector DbgValuesToSink; + collectDebugValues(MI, DbgValuesToSink); + + // Merge or erase debug location to ensure consistent stepping in profilers + // and debuggers. + if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end()) + MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(), + InsertPos->getDebugLoc())); + else + MI.setDebugLoc(DebugLoc()); + + // Move the instruction. + MachineBasicBlock *ParentBlock = MI.getParent(); + SuccToSinkTo.splice(InsertPos, ParentBlock, MI, + ++MachineBasicBlock::iterator(MI)); + + // Move previously adjacent debug value instructions to the insert position. + for (SmallVectorImpl::iterator DBI = DbgValuesToSink.begin(), + DBE = DbgValuesToSink.end(); + DBI != DBE; ++DBI) { + MachineInstr *DbgMI = *DBI; + SuccToSinkTo.splice(InsertPos, ParentBlock, DbgMI, + ++MachineBasicBlock::iterator(DbgMI)); + } +} + /// SinkInstruction - Determine whether it is safe to sink the specified machine /// instruction out of its current block into a successor. bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore, @@ -866,30 +896,7 @@ while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) ++InsertPos; - // collect matching debug values. - SmallVector DbgValuesToSink; - collectDebugValues(MI, DbgValuesToSink); - - // Merge or erase debug location to ensure consistent stepping in profilers - // and debuggers. - if (!SuccToSinkTo->empty() && InsertPos != SuccToSinkTo->end()) - MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(), - InsertPos->getDebugLoc())); - else - MI.setDebugLoc(DebugLoc()); - - - // Move the instruction. - SuccToSinkTo->splice(InsertPos, ParentBlock, MI, - ++MachineBasicBlock::iterator(MI)); - - // Move previously adjacent debug value instructions to the insert position. - for (SmallVectorImpl::iterator DBI = DbgValuesToSink.begin(), - DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) { - MachineInstr *DbgMI = *DBI; - SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI, - ++MachineBasicBlock::iterator(DbgMI)); - } + performSink(MI, *SuccToSinkTo, InsertPos); // Conservatively, clear any kill flags, since it's possible that they are no // longer correct. @@ -1118,6 +1125,9 @@ MachineInstr *MI = &*I; ++I; + if (MI->isDebugInstr()) + continue; + // Do not move any instruction across function call. if (MI->isCall()) return false; @@ -1158,7 +1168,7 @@ // block. clearKillFlags(MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI); MachineBasicBlock::iterator InsertPos = SuccBB->getFirstNonPHI(); - SuccBB->splice(InsertPos, &CurBB, MI); + performSink(*MI, *SuccBB, InsertPos); updateLiveIn(MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy); Changed = true; Index: test/CodeGen/X86/postra-ignore-dbg-instrs.mir =================================================================== --- test/CodeGen/X86/postra-ignore-dbg-instrs.mir +++ test/CodeGen/X86/postra-ignore-dbg-instrs.mir @@ -0,0 +1,88 @@ +# RUN: llc -mtriple=x86_64-none-linux-gnu -run-pass=postra-machine-sink -verify-machineinstrs -o - %s | FileCheck %s +# +# This test was originally generated from the following sample: +# +# int x0; +# extern void x3(int, int); +# void x1(int x2) { +# if (x0) +# x3(0, x2); +# } +# +# The code generates a COPY instruction which the PostRA Machine Sink pass will +# try to sink. Earlier versions were not performing the sink due to a +# DBG_VALUE instruction confusing the sinking algorithm. + +--- | + @x0 = common dso_local global i32 0, align 4, !dbg !0 + + define dso_local void @x1(i32) !dbg !11 { + %2 = alloca i32, align 4 + store i32 %0, i32* %2, align 4 + call void @llvm.dbg.declare(metadata i32* %2, metadata !14, metadata !DIExpression()), !dbg !16 + %3 = load i32, i32* @x0, align 4, !dbg !16 + %4 = icmp ne i32 %3, 0, !dbg !16 + br i1 %4, label %5, label %7, !dbg !16 + + ;