Index: llvm/lib/CodeGen/MachineVerifier.cpp =================================================================== --- llvm/lib/CodeGen/MachineVerifier.cpp +++ llvm/lib/CodeGen/MachineVerifier.cpp @@ -263,6 +263,7 @@ void verifyStackFrame(); void verifySlotIndexes() const; + void verifyDebugLocations(const MachineFunction &MF); void verifyProperties(const MachineFunction &MF); }; @@ -322,6 +323,40 @@ } } +void MachineVerifier::verifyDebugLocations(const MachineFunction &MF) { + // We're only guaranteed monotonicity at -O0. Optimizations + // may shuffle instructions around. + if (MF.getTarget().getOptLevel() == CodeGenOpt::None && + MF.getTarget().Options.EnableGlobalISel) + return; + for (const MachineBasicBlock &MBB : MF) { + for (const MachineInstr &MI : MBB.instrs()) { + // Reset the line every time we enter a new MBB. + unsigned LastLine = 0; + + // Debug instructions don't generate code, so we + // can skip them for verification's sake. + if (MI.isDebugInstr()) + continue; + + // Also skip instructions without a debug location + // associated. + auto DL = MI.getDebugLoc(); + if (!DL) + continue; + unsigned CurLine = DL.getLine(); + + // If the line associated with this instruction isn't + // line zero, and it's smaller than the last one we visited, + // trigger an error. + if (CurLine != 0 && CurLine < LastLine) + report("Non-monotonically increasing debug location within " + "a single MachineBasicBlock", &MI); + LastLine = CurLine; + } + } +} + void MachineVerifier::verifyProperties(const MachineFunction &MF) { // If a pass has introduced virtual registers without clearing the // NoVRegs property (or set it without allocating the vregs) @@ -372,6 +407,8 @@ verifyProperties(MF); + verifyDebugLocations(MF); + visitMachineFunctionBefore(); for (const MachineBasicBlock &MBB : MF) { visitMachineBasicBlockBefore(&MBB);