diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp --- a/llvm/lib/CodeGen/MachineSink.cpp +++ b/llvm/lib/CodeGen/MachineSink.cpp @@ -383,7 +383,13 @@ void MachineSinking::FindCycleSinkCandidates( MachineCycle *Cycle, MachineBasicBlock *BB, SmallVectorImpl &Candidates) { - for (auto &MI : *BB) { + + for (auto II = BB->begin(), EI = BB->end(); II != EI; ++II) { + II = skipDebugInstructionsForward(II, EI); + if (II == EI) + break; + MachineInstr &MI = *II; + LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI); if (!TII->shouldSink(MI)) { LLVM_DEBUG(dbgs() << "CycleSink: Instruction not a candidate for this " diff --git a/llvm/test/CodeGen/AArch64/sink-insts-to-avoid-spills-debug-crash.mir b/llvm/test/CodeGen/AArch64/sink-insts-to-avoid-spills-debug-crash.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/sink-insts-to-avoid-spills-debug-crash.mir @@ -0,0 +1,51 @@ +# RUN: llc -run-pass=machine-sink -sink-insts-to-avoid-spills -o - %s | FileCheck %s +# REQUIRES: aarch64-registered-target + +# Test used to crash with the following assert: +# Assertion `MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!"' failed. +# in function isCycleInvariant (MachineCycleAnalysis.cpp) + +--- | + target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" + target triple = "aarch64-unknown-linux-gnu" + + define i32 @a() { + entry: + %call = tail call i32 @c() + call void @llvm.dbg.value(metadata i32 %call, metadata !3, metadata !DIExpression()), !dbg !6 + br label %for.cond + + for.cond: ; preds = %for.cond, %entry + br label %for.cond + } + + declare i32 @c() + + declare void @llvm.dbg.value(metadata, metadata, metadata) + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!2} + + !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug) + !1 = !DIFile(filename: "bcm.c", directory: "/") + !2 = !{i32 2, !"Debug Info Version", i32 3} + !3 = !DILocalVariable(name: "b", scope: !4, file: !1, line: 8, type: !5) + !4 = distinct !DISubprogram(name: "a", scope: !1, spFlags: DISPFlagDefinition, unit: !0) + !5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !6 = !DILocation(line: 0, scope: !4) + +... +--- +# CHECK-LABEL: a +name: a +body: | + bb.0.entry: + ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp + BL @c, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp, implicit-def $w0 + ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp + DBG_VALUE %0:gpr32all, $noreg, !3, !DIExpression(), debug-location !6 + + bb.1.for.cond: + B %bb.1 + +...