llvm/llvm/test/DebugInfo/MIR/InstrRef/memory-operand-load-folding.mir

# RUN: llc %s -o - -experimental-debug-variable-locations \
# RUN:     -start-before=phi-node-elimination -stop-after=virtregrewriter \
# RUN:    -mtriple x86_64-unknown-unknown \
# RUN:  | FileCheck %s
#
# Test that when a load gets folded into an instruction (the CVTTSS2SIrr below)
# that the debug-instr-number is preserved, with a substitution.
#
# CHECK:      debugValueSubstitutions:
# CHECK-NEXT:  - { srcinst: 1, srcop: 0, dstinst: 2, dstop: 0, subreg: 0 }
# CHECK-LABEL: bb.1.sw.bb:
# CHECK:      renamable $eax = nofpexcept CVTTSS2SIrm %stack.0,
# CHECK-SAME:               debug-instr-number 2 :: (load (s32) from %stack.0)
--- |
  ; ModuleID = 'lol.ll'
  source_filename = "reduced.ll"
  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
  target triple = "x86_64-unknown-linux-gnu"

  define i32 @foo(i32 %i, float %f) local_unnamed_addr {
  if.then:
    %call = tail call i32 (i32, ...) undef(i32 %i)
    %cond = icmp eq i32 %call, 1
    br i1 %cond, label %sw.bb, label %sw.epilog

  sw.bb:                                            ; preds = %if.then
    %conv = fptosi float %f to i8
    call void @llvm.dbg.value(metadata i8 %conv, metadata !6, metadata !DIExpression()), !dbg !17
    %tobool.not = icmp eq i8 %conv, 0
    br i1 %tobool.not, label %if.end, label %sw.epilog

  if.end:                                           ; preds = %sw.bb
    tail call void (...) undef()
    br label %sw.epilog

  sw.epilog:                                        ; preds = %if.then, %if.end, %sw.bb
    ret i32 undef
  }

  ; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
  declare void @llvm.dbg.value(metadata, metadata, metadata) #0

  attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!2, !3, !4, !5}

  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
  !1 = !DIFile(filename: "/tmp/test.c", directory: ".")
  !2 = !{i32 7, !"Dwarf Version", i32 4}
  !3 = !{i32 2, !"Debug Info Version", i32 3}
  !4 = !{i32 1, !"wchar_size", i32 4}
  !5 = !{i32 7, !"uwtable", i32 1}
  !6 = !DILocalVariable(name: "b", scope: !7, file: !8, line: 15, type: !16)
  !7 = distinct !DISubprogram(name: "foo", scope: !8, file: !8, line: 8, type: !9, scopeLine: 9, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !15)
  !8 = !DIFile(filename: "/tmp/test.c", directory: "")
  !9 = !DISubroutineType(types: !10)
  !10 = !{!11, !11, !12, !13}
  !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !12 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
  !13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
  !14 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed)
  !15 = !{}
  !16 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
  !17 = !DILocation(line: 0, scope: !7)

...
---
name:            foo
tracksRegLiveness: true
debugInstrRef: true
registers:
  - { id: 0, class: gr32, preferred-register: '' }
  - { id: 1, class: fr32, preferred-register: '' }
  - { id: 2, class: gr32, preferred-register: '' }
  - { id: 3, class: gr8, preferred-register: '' }
  - { id: 4, class: gr64, preferred-register: '' }
  - { id: 5, class: gr32, preferred-register: '' }
  - { id: 6, class: gr32, preferred-register: '' }
  - { id: 7, class: gr32, preferred-register: '' }
  - { id: 8, class: gr8, preferred-register: '' }
  - { id: 9, class: gr32, preferred-register: '' }
  - { id: 10, class: gr8, preferred-register: '' }
  - { id: 11, class: gr64, preferred-register: '' }
  - { id: 12, class: gr32, preferred-register: '' }
liveins:
  - { reg: '$edi', virtual-reg: '%0' }
  - { reg: '$xmm0', virtual-reg: '%1' }
frameInfo:
  adjustsStack:    true
  hasCalls:        true
body:             |
  bb.0.if.then:
    successors: %bb.1(0x40000000), %bb.3(0x40000000)
    liveins: $edi, $xmm0

    %1:fr32 = COPY killed $xmm0
    %0:gr32 = COPY killed $edi
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
    %2:gr32 = MOV32r0 implicit-def dead $eflags
    %3:gr8 = COPY killed %2.sub_8bit
    $edi = COPY killed %0
    $al = COPY killed %3
    CALL64r undef %4:gr64, csr_64, implicit $rsp, implicit $ssp, implicit killed $edi, implicit killed $al, implicit-def $rsp, implicit-def $ssp, implicit-def $eax
    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
    %5:gr32 = COPY killed $eax
    CMP32ri8 killed %5, 1, implicit-def $eflags
    JCC_1 %bb.3, 5, implicit killed $eflags
    JMP_1 %bb.1

  bb.1.sw.bb:
    successors: %bb.2(0x30000000), %bb.3(0x50000000)

    %7:gr32 = nofpexcept CVTTSS2SIrr killed %1, implicit $mxcsr, debug-instr-number 1
    %8:gr8 = COPY killed %7.sub_8bit
    DBG_INSTR_REF !6, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(2, 0), debug-location !17
    TEST8rr killed %8, %8, implicit-def $eflags
    JCC_1 %bb.3, 5, implicit killed $eflags
    JMP_1 %bb.2

  bb.2.if.end:
    successors: %bb.3(0x80000000)

    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
    %9:gr32 = MOV32r0 implicit-def dead $eflags
    %10:gr8 = COPY killed %9.sub_8bit
    $al = COPY killed %10
    CALL64r undef %11:gr64, csr_64, implicit $rsp, implicit $ssp, implicit killed $al, implicit-def $rsp, implicit-def $ssp
    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp

  bb.3.sw.epilog:
    RET 0, undef $eax

...