xref: /llvm-project/llvm/test/DebugInfo/MIR/InstrRef/dbg-phis-with-loops.mir (revision d53425e2a33a0ff7336c86d3f668b1855c47a6de)
1# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
2# RUN:    -experimental-debug-variable-locations -run-pass=livedebugvalues \
3# RUN:    | FileCheck %s --check-prefix=CHECK
4#
5# Copy of dbg-phis-merging-in-ldv.mir, where I've added a loop in between the
6# DBG_PHI "definitions" of values, and the DBG_INSTR_REFs where they're used.
7# We should be able to traverse this obstacle.
8--- |
9  ; ModuleID = 'before.mir'
10  source_filename = "test.c"
11  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
12  target triple = "x86_64-unknown-linux-gnu"
13
14  define dso_local i32 @foo(i64 %bar, i64 %baz) !dbg !7 {
15  entry:
16    call void @llvm.dbg.value(metadata i64 %bar, metadata !12, metadata !DIExpression()), !dbg !13
17    call void @llvm.dbg.value(metadata i64 %baz, metadata !14, metadata !DIExpression()), !dbg !13
18    call void @ext(i64 %bar), !dbg !15
19    %add = add nsw i64 %bar, 12, !dbg !16
20    call void @llvm.dbg.value(metadata i64 %add, metadata !12, metadata !DIExpression()), !dbg !13
21    call void @ext(i64 %add), !dbg !17
22    %add1 = add nsw i64 %baz, 1, !dbg !18
23    call void @llvm.dbg.value(metadata i64 %add1, metadata !14, metadata !DIExpression()), !dbg !13
24    %call = call i64 @getlong(), !dbg !19
25    %tobool = icmp ne i64 %call, 0, !dbg !19
26    br i1 %tobool, label %if.then, label %if.else, !dbg !21
27
28  if.then:                                          ; preds = %entry
29    %add2 = add nsw i64 %add, 1, !dbg !22
30    call void @llvm.dbg.value(metadata i64 %add2, metadata !12, metadata !DIExpression()), !dbg !13
31    br label %if.end, !dbg !24
32
33  if.else:                                          ; preds = %entry
34    %add3 = add nsw i64 %add, 2, !dbg !25
35    call void @llvm.dbg.value(metadata i64 %add3, metadata !12, metadata !DIExpression()), !dbg !13
36    br label %if.end
37
38  if.end:                                           ; preds = %if.else, %if.then
39    %bar.addr.0 = phi i64 [ %add2, %if.then ], [ %add3, %if.else ], !dbg !27
40    call void @llvm.dbg.value(metadata i64 %bar.addr.0, metadata !12, metadata !DIExpression()), !dbg !13
41    %add4 = add nsw i64 %bar.addr.0, %add1, !dbg !28
42    call void @llvm.dbg.value(metadata i64 %add4, metadata !12, metadata !DIExpression()), !dbg !13
43    call void @ext(i64 %add4), !dbg !29
44    %conv = trunc i64 %add4 to i32, !dbg !30
45    ret i32 %conv, !dbg !31
46  }
47
48  ; Function Attrs: nounwind readnone speculatable willreturn
49  declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
50
51  declare dso_local void @ext(i64)
52
53  declare dso_local i64 @getlong()
54
55  ; Function Attrs: nounwind readnone speculatable willreturn
56  declare void @llvm.dbg.value(metadata, metadata, metadata) #0
57
58  attributes #0 = { nounwind readnone speculatable willreturn }
59
60  !llvm.dbg.cu = !{!0}
61  !llvm.module.flags = !{!3, !4, !5}
62  !llvm.ident = !{!6}
63
64  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
65  !1 = !DIFile(filename: "test.c", directory: ".")
66  !2 = !{}
67  !3 = !{i32 7, !"Dwarf Version", i32 4}
68  !4 = !{i32 2, !"Debug Info Version", i32 3}
69  !5 = !{i32 1, !"wchar_size", i32 4}
70  !6 = !{!"clang"}
71  !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
72  !8 = !DISubroutineType(types: !9)
73  !9 = !{!10, !11, !11}
74  !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
75  !11 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
76  !12 = !DILocalVariable(name: "bar", arg: 1, scope: !7, file: !1, line: 3, type: !11)
77  !13 = !DILocation(line: 0, scope: !7)
78  !14 = !DILocalVariable(name: "baz", arg: 2, scope: !7, file: !1, line: 3, type: !11)
79  !15 = !DILocation(line: 4, column: 3, scope: !7)
80  !16 = !DILocation(line: 5, column: 7, scope: !7)
81  !17 = !DILocation(line: 6, column: 3, scope: !7)
82  !18 = !DILocation(line: 7, column: 7, scope: !7)
83  !19 = !DILocation(line: 9, column: 7, scope: !20)
84  !20 = distinct !DILexicalBlock(scope: !7, file: !1, line: 9, column: 7)
85  !21 = !DILocation(line: 9, column: 7, scope: !7)
86  !22 = !DILocation(line: 10, column: 9, scope: !23)
87  !23 = distinct !DILexicalBlock(scope: !20, file: !1, line: 9, column: 18)
88  !24 = !DILocation(line: 11, column: 3, scope: !23)
89  !25 = !DILocation(line: 12, column: 9, scope: !26)
90  !26 = distinct !DILexicalBlock(scope: !20, file: !1, line: 11, column: 10)
91  !27 = !DILocation(line: 0, scope: !20)
92  !28 = !DILocation(line: 15, column: 7, scope: !7)
93  !29 = !DILocation(line: 16, column: 3, scope: !7)
94  !30 = !DILocation(line: 17, column: 10, scope: !7)
95  !31 = !DILocation(line: 17, column: 3, scope: !7)
96
97...
98---
99name:            foo
100alignment:       16
101tracksRegLiveness: true
102debugInstrRef: true
103liveins:
104  - { reg: '$rdi' }
105  - { reg: '$rsi' }
106frameInfo:
107  stackSize:       24
108  offsetAdjustment: -24
109  maxAlignment:    1
110  adjustsStack:    true
111  hasCalls:        true
112  maxCallFrameSize: 0
113  cvBytesOfCalleeSavedRegisters: 16
114fixedStack:
115  - { id: 0, type: spill-slot, offset: -24, size: 8, alignment: 8, callee-saved-register: '$rbx' }
116  - { id: 1, type: spill-slot, offset: -16, size: 8, alignment: 16, callee-saved-register: '$r14' }
117machineFunctionInfo: {}
118body:             |
119  bb.0.entry:
120    successors: %bb.2, %bb.1
121    liveins: $rdi, $rsi, $r14, $rbx
122
123    frame-setup PUSH64r killed $r14, implicit-def $rsp, implicit $rsp
124    CFI_INSTRUCTION def_cfa_offset 16
125    frame-setup PUSH64r killed $rbx, implicit-def $rsp, implicit $rsp
126    CFI_INSTRUCTION def_cfa_offset 24
127    frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
128    CFI_INSTRUCTION def_cfa_offset 32
129    CFI_INSTRUCTION offset $rbx, -24
130    CFI_INSTRUCTION offset $r14, -16
131    $r14 = MOV64rr $rsi
132    $rbx = MOV64rr $rdi
133    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !15
134    renamable $rbx = ADD64ri32 killed renamable $rbx, 12, implicit-def $eflags, debug-location !16
135    $rdi = MOV64rr $rbx, debug-location !17
136    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !17
137    renamable $r14 = ADD64ri32 killed renamable $r14, 1, implicit-def $eflags, debug-location !18
138    CALL64pcrel32 @getlong, csr_64, implicit $rsp, implicit $ssp, implicit-def $rax, debug-location !19
139    CMP64ri8 killed renamable $rax, 0, implicit-def $eflags, debug-location !19
140    JCC_1 %bb.1, 5, implicit $eflags, debug-location !21
141
142  bb.2.if.else:
143    liveins: $rbx, $r14, $rax
144
145    renamable $rbx = ADD64ri32 killed renamable $rbx, 2, implicit-def $eflags, debug-location !25
146    DBG_PHI $r14, 1
147    DBG_PHI $rbx, 2
148    DBG_PHI $rax, 3
149    $rax = MOV64ri 0
150    JMP_1 %bb.3
151
152  bb.1.if.then:
153    liveins: $rbx, $r14, $rax
154
155    renamable $rbx = ADD64ri32 killed renamable $rbx, 1, implicit-def $eflags, debug-location !22
156    DBG_PHI $r14, 1
157    DBG_PHI $rbx, 2
158    DBG_PHI $rax, 3
159
160  bb.3:
161    $r15 = MOV64ri 0
162    CMP64ri8 $r15, 0, implicit-def $eflags, debug-location !19
163    JCC_1 %bb.3, 5, implicit $eflags, debug-location !21
164
165  bb.4:
166    liveins: $rbx, $r14
167
168    DBG_INSTR_REF !14, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0), debug-location !13
169    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(2, 0), debug-location !13
170    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(3, 0), debug-location !13
171
172    ; Value number 1 is live-through the above control flow from the two
173    ; DBG_PHIs:
174    ; CHECK:       DBG_INSTR_REF {{.+}}, dbg-instr-ref(1, 0)
175    ; CHECK-NEXT:  DBG_VALUE_LIST {{.+}} $r14
176    ;
177    ; While value number 2 has different defs that merge on entry to bb.3.
178    ; These are both in $rbx though, and we should find its location:
179    ; CHECK:       DBG_INSTR_REF {{.+}}, dbg-instr-ref(2, 0)
180    ; CHECK-NEXT:  DBG_VALUE_LIST {{.+}} $rbx
181    ;
182    ; Value number 3 cannot be resolved because $rax is clobbered in bb.2,
183    ; meaning the merged value in bb.3 is incorrect. It should produce a
184    ; DBG_VALUE $noreg.
185    ; CHECK:       DBG_INSTR_REF {{.+}}, dbg-instr-ref(3, 0)
186    ; CHECK-NEXT:  DBG_VALUE_LIST {{.+}} $noreg
187
188    renamable $rbx = ADD64rr killed renamable $rbx, killed renamable $r14, implicit-def $eflags, debug-location !28
189    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(2, 0), debug-location !13
190
191    ; After clobbering rbx, the variable location should not be available.
192    ; CHECK:       DBG_INSTR_REF {{.+}}, dbg-instr-ref(2, 0)
193    ; CHECK-NEXT:  DBG_VALUE_LIST {{.+}} $noreg
194
195    $rdi = MOV64rr $rbx, debug-location !29
196    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !29
197    $eax = MOV32rr $ebx, implicit killed $rbx, debug-location !31
198    $rsp = frame-destroy ADD64ri8 $rsp, 8, implicit-def dead $eflags, debug-location !31
199    CFI_INSTRUCTION def_cfa_offset 24, debug-location !31
200    $rbx = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !31
201    CFI_INSTRUCTION def_cfa_offset 16, debug-location !31
202    $r14 = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !31
203    CFI_INSTRUCTION def_cfa_offset 8, debug-location !31
204    RET64 implicit $eax, debug-location !31
205
206...
207