1# These tests check that DBG_VALUE intervals are correctly coalesced (or not) in
2# LiveDebugVariables.
3# NOTE: We do not currently handle partially overlapping fragments in LDV.
4#
5# The IR in this file has been modified by hand, generated from this source:
6# void escape(int *);
7# extern int global;
8# void f(int x) {
9#   escape(&x);
10#   x = 1;
11#   global = x;
12#   x = 2;
13#   escape(&x);
14# }
15
16# RUN: llc %s -start-before=machine-scheduler -stop-after=virtregrewriter -o - \
17# RUN:     | FileCheck %s --implicit-check-not=DBG_VALUE
18
19# Verify that DBG_VALUEs with same { Variable, Fragment } but different DIExpressions
20# are not coalesced.
21#
22# DV1 %0 "x" (deref) --+-- Do not coalesce these because DV2 refers to the same bits.
23# DV2 %0 "x" ()        |
24# DV3 %0 "x" (deref) --+
25#
26# CHECK: name: f1
27# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR:[0-9]+]], !DIExpression(DW_OP_deref)
28# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]],        !DIExpression()
29# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]],        !DIExpression(DW_OP_deref)
30
31# Verify that DBG_VALUEs with same { Variable, Fragment } and { Location, DIExpression } are
32# coalesced if there are no intervening DBG_VALUEs with the same { Variable, Fragment }.
33#
34# DV1 %0 "x" (fragment 00 16) --+-- Coalesce these because DV2 refers to different bits.
35# DV2 %0 "x" (fragment 16 16)   |
36# DV3 %0 "x" (fragment 00 16) --+
37#
38# CHECK: name: f2
39# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16)
40# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]],        !DIExpression(DW_OP_LLVM_fragment, 16, 16)
41
42--- |
43  target triple = "x86_64-unknown-linux-gnu"
44
45  @global = external global i32, align 4
46  declare void @llvm.dbg.value(metadata, metadata, metadata)
47  declare void @escape(i32*)
48
49  define void @f1(i32 %x) !dbg !6 {
50  entry:
51    %x.addr = alloca i32, align 4
52    store i32 %x, i32* %x.addr, align 4
53    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression(DW_OP_deref)), !dbg !12
54    call void @escape(i32* %x.addr), !dbg !12
55    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression()), !dbg !12
56    store i32 1, i32* @global, align 4, !dbg !12
57    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression(DW_OP_deref)), !dbg !12
58    store i32 2, i32* %x.addr, align 4, !dbg !12
59    call void @escape(i32* %x.addr), !dbg !12
60    ret void, !dbg !12
61  }
62
63  define void @f2(i32 %x) !dbg !13 {
64  entry:
65    %x.addr = alloca i32, align 4
66    store i32 %x, i32* %x.addr, align 4
67    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16)), !dbg !15
68    call void @escape(i32* %x.addr)
69    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_LLVM_fragment, 16, 16)), !dbg !15
70    store i32 1, i32* @global, align 4
71    call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16)), !dbg !15
72    store i32 2, i32* %x.addr, align 4
73    call void @escape(i32* %x.addr)
74    ret void
75  }
76
77  !llvm.dbg.cu = !{!0}
78  !llvm.module.flags = !{!3, !4}
79  !llvm.ident = !{!5}
80
81  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
82  !1 = !DIFile(filename: "test.c", directory: "")
83  !2 = !{}
84  !3 = !{i32 2, !"Dwarf Version", i32 4}
85  !4 = !{i32 2, !"Debug Info Version", i32 3}
86  !5 = !{!"clang version 11.0.0 "}
87  !6 = distinct !DISubprogram(name: "f1", scope: !1, file: !1, line: 3, type: !7, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !10)
88  !7 = !DISubroutineType(types: !8)
89  !8 = !{null, !9}
90  !9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
91  !10 = !{!11}
92  !11 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !1, line: 3, type: !9)
93  !12 = !DILocation(line: 3, column: 12, scope: !6)
94  !13 = distinct !DISubprogram(name: "f2", scope: !1, file: !1, line: 20, type: !7, scopeLine: 20, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !10)
95  !14 = !DILocalVariable(name: "x", arg: 1, scope: !13, file: !1, line: 21, type: !9)
96  !15 = !DILocation(line: 23, column: 12, scope: !13)
97
98...
99---
100name:            f1
101tracksRegLiveness: true
102stack:
103  - { id: 0, name: x.addr, type: default, offset: 0, size: 4, alignment: 4,
104      stack-id: default, callee-saved-register: '', callee-saved-restored: true,
105      debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
106body:             |
107  bb.0.entry:
108    liveins: $edi
109    %0:gr32 = COPY $edi
110    MOV32mr %stack.0.x.addr, 1, $noreg, 0, $noreg, %0 :: (store 4 into %ir.x.addr)
111    DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(DW_OP_deref), debug-location !12
112    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12
113    %1:gr64 = LEA64r %stack.0.x.addr, 1, $noreg, 0, $noreg
114    $rdi = COPY %1, debug-location !12
115    CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, debug-location !12
116    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12
117    DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(), debug-location !12
118    MOV32mi $rip, 1, $noreg, @global, $noreg, 1, debug-location !12 :: (store 4 into @global)
119    DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(DW_OP_deref), debug-location !12
120    MOV32mi %stack.0.x.addr, 1, $noreg, 0, $noreg, 2, debug-location !12 :: (store 4 into %ir.x.addr)
121    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12
122    $rdi = COPY %1, debug-location !12
123    CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, debug-location !12
124    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12
125    RET 0, debug-location !12
126...
127---
128name:            f2
129tracksRegLiveness: true
130stack:
131  - { id: 0, name: x.addr, type: default, offset: 0, size: 4, alignment: 4,
132      stack-id: default, callee-saved-register: '', callee-saved-restored: true,
133      debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
134body:             |
135  bb.0.entry:
136    liveins: $edi
137    %0:gr32 = COPY $edi
138    MOV32mr %stack.0.x.addr, 1, $noreg, 0, $noreg, %0 :: (store 4 into %ir.x.addr)
139    DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16), debug-location !15
140    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
141    %1:gr64 = LEA64r %stack.0.x.addr, 1, $noreg, 0, $noreg
142    $rdi = COPY %1
143    CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp
144    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
145    DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_LLVM_fragment, 16, 16), debug-location !15
146    MOV32mi $rip, 1, $noreg, @global, $noreg, 1 :: (store 4 into @global)
147    DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16), debug-location !15
148    MOV32mi %stack.0.x.addr, 1, $noreg, 0, $noreg, 2 :: (store 4 into %ir.x.addr)
149    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
150    $rdi = COPY %1
151    CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp
152    ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp
153    RET 0
154...
155