xref: /llvm-project/llvm/test/DebugInfo/MIR/InstrRef/phi-coalescing.mir (revision 7564566779eb07e9daf41a351b09cf7607871845)
1# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
2# RUN:    -experimental-debug-variable-locations \
3# RUN:    -run-pass=phi-node-elimination,register-coalescer \
4# RUN:    | FileCheck %s --check-prefix=DOESCOALESCE
5# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
6# RUN:    -experimental-debug-variable-locations \
7# RUN:    -run-pass=phi-node-elimination,register-coalescer,livedebugvars,greedy,virtregrewriter \
8# RUN:    | FileCheck %s --check-prefix=CHECK
9#
10# Test that a PHI with a debug instruction number attached survives register
11# coalescing, when its vreg gets coalesced. This test comes in two parts, first
12# that coalescing actually happens, second that a DBG_PHI is still produced
13# afterwards.
14#
15# Original C code, the PHI is of the value of 'bar' after the control flow.
16# Compiled at -O0, applied -mem2reg, llc -O0, then manually added the PHI
17# instruction label. Additional variable locations removed.
18#
19#    void ext(long);
20#    long getlong(void);
21#    int foo(long bar, long baz) {
22#      ext(bar);
23#      bar += 12;
24#      ext(bar);
25#
26#      if (getlong())
27#        bar += 1;
28#
29#      bar += baz;
30#      ext(bar);
31#      return bar;
32#    }
33#
34--- |
35  ; ModuleID = 'promoted.ll'
36  source_filename = "test.c"
37  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
38  target triple = "x86_64-unknown-linux-gnu"
39
40  define dso_local i32 @foo(i64 %bar, i64 %baz) !dbg !7 {
41  entry:
42    call void @llvm.dbg.value(metadata i64 %bar, metadata !12, metadata !DIExpression()), !dbg !13
43    call void @llvm.dbg.value(metadata i64 %baz, metadata !14, metadata !DIExpression()), !dbg !13
44    call void @ext(i64 %bar), !dbg !13
45    %add = add nsw i64 %bar, 12, !dbg !13
46    call void @llvm.dbg.value(metadata i64 %add, metadata !12, metadata !DIExpression()), !dbg !13
47    call void @ext(i64 %add), !dbg !13
48    %call = call i64 @getlong(), !dbg !13
49    %tobool = icmp ne i64 %call, 0, !dbg !13
50    br i1 %tobool, label %if.then, label %if.end, !dbg !13
51
52  if.then:                                          ; preds = %entry
53    %add1 = add nsw i64 %add, 1, !dbg !13
54    call void @llvm.dbg.value(metadata i64 %add1, metadata !12, metadata !DIExpression()), !dbg !13
55    br label %if.end, !dbg !13
56
57  if.end:                                           ; preds = %if.then, %entry
58    %bar.addr.0 = phi i64 [ %add1, %if.then ], [ %add, %entry ], !dbg !13
59    call void @llvm.dbg.value(metadata i64 %bar.addr.0, metadata !12, metadata !DIExpression()), !dbg !13
60    %add2 = add nsw i64 %bar.addr.0, %baz, !dbg !13
61    call void @llvm.dbg.value(metadata i64 %add2, metadata !12, metadata !DIExpression()), !dbg !13
62    call void @ext(i64 %add2), !dbg !13
63    %conv = trunc i64 %add2 to i32, !dbg !13
64    ret i32 %conv, !dbg !13
65  }
66
67  ; Function Attrs: nounwind readnone speculatable willreturn
68  declare void @llvm.dbg.declare(metadata, metadata, metadata)
69
70  declare dso_local void @ext(i64)
71
72  declare dso_local i64 @getlong()
73
74  ; Function Attrs: nounwind readnone speculatable willreturn
75  declare void @llvm.dbg.value(metadata, metadata, metadata)
76
77  !llvm.dbg.cu = !{!0}
78  !llvm.module.flags = !{!3, !4, !5}
79  !llvm.ident = !{!6}
80
81  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
82  !1 = !DIFile(filename: "test.c", directory: "/tmp/out.c")
83  !2 = !{}
84  !3 = !{i32 7, !"Dwarf Version", i32 4}
85  !4 = !{i32 2, !"Debug Info Version", i32 3}
86  !5 = !{i32 1, !"wchar_size", i32 4}
87  !6 = !{!""}
88  !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
89  !8 = !DISubroutineType(types: !9)
90  !9 = !{!10, !11, !11}
91  !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
92  !11 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
93  !12 = !DILocalVariable(name: "bar", arg: 1, scope: !7, file: !1, line: 3, type: !11)
94  !13 = !DILocation(line: 0, scope: !7)
95  !14 = !DILocalVariable(name: "baz", arg: 2, scope: !7, file: !1, line: 3, type: !11)
96  !15 = distinct !DILexicalBlock(scope: !7, file: !1, line: 8, column: 7)
97
98...
99---
100name:            foo
101alignment:       16
102tracksRegLiveness: true
103debugInstrRef: true
104liveins:
105  - { reg: '$rdi', virtual-reg: '%3' }
106  - { reg: '$rsi', virtual-reg: '%5' }
107frameInfo:
108  maxAlignment:    1
109  adjustsStack:    true
110  hasCalls:        true
111machineFunctionInfo: {}
112body:             |
113  bb.0.entry:
114    liveins: $rdi, $rsi
115
116    %5:gr64 = COPY $rsi
117    %3:gr64 = COPY $rdi
118    %4:gr64 = COPY killed %3
119    %6:gr64 = COPY killed %5
120    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
121    $rdi = COPY %4, debug-location !13
122    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !13
123    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
124    %9:gr64 = ADD64ri32 %4, 12, implicit-def $eflags, debug-location !13
125    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
126    $rdi = COPY %9, debug-location !13
127    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !13
128    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
129    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
130    CALL64pcrel32 @getlong, csr_64, implicit $rsp, implicit $ssp, implicit-def $rax, debug-location !13
131    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
132    %8:gr64 = COPY $rax, debug-location !13
133    CMP64ri8 %8, 0, implicit-def $eflags, debug-location !13
134    JCC_1 %bb.2, 4, implicit $eflags, debug-location !13
135
136  ; DOESCOALESCE-LABEL: bb.1.if.then:
137  ; CHECK-LABEL:        bb.1.if.then:
138  bb.1.if.then:
139    %10:gr64 = ADD64ri32 %9, 1, implicit-def $eflags, debug-location !13
140
141  ; Verify that the vreg is different immediately after register coalescing.
142  ; DOESCOALESCE-NOT:   %10:gr64 ADD64ri32
143  ; DOESCOALESCE:       %{{[0-9]+}}:gr64_with_sub_8bit = ADD64ri32
144  ; Verify that all these registers land in r14.
145  ; CHECK:              renamable $r14 = ADD64ri32 killed renamable $r14
146
147  ; DOESCOALESCE-LABEL: bb.2.if.end:
148  ; CHECK-LABEL:        bb.2.if.end:
149  bb.2.if.end:
150    %2:gr64 = PHI %9, %bb.0, %10, %bb.1, debug-instr-number 1, debug-location !13
151  ; CHECK:              DBG_PHI $r14, 1
152    DBG_INSTR_REF !12, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0), debug-location !13
153    %2:gr64 = ADD64rr killed %2, %6, implicit-def $eflags, debug-location !13
154    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
155    $rdi = COPY %2, debug-location !13
156    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, debug-location !13
157    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
158    %13:gr32 = COPY %2.sub_32bit, debug-location !13
159    $eax = COPY %13, debug-location !13
160    RET64 implicit $eax, debug-location !13
161
162...
163