xref: /llvm-project/llvm/test/Transforms/MergeFunc/mergefunc-preserve-debug-info.ll (revision 094572701dce4aaf36f4521d6cf750420d39f206)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2; RUN: opt -passes='default<O0>,mergefunc' -S -mergefunc-preserve-debug-info < %s | FileCheck %s --check-prefix=OPTIMIZATION_LEVEL_0
3; RUN: opt -passes='default<O2>,mergefunc' -S -mergefunc-preserve-debug-info < %s | FileCheck %s --check-prefix=OPTIMIZATION_LEVEL_2
4
5;; Does this continue to work under non-intrinsic debug-info?
6; RUN: opt -passes='default<O0>,mergefunc' -S -mergefunc-preserve-debug-info < %s --try-experimental-debuginfo-iterators | FileCheck %s --check-prefix=OPTIMIZATION_LEVEL_0
7; RUN: opt -passes='default<O2>,mergefunc' -S -mergefunc-preserve-debug-info < %s --try-experimental-debuginfo-iterators | FileCheck %s --check-prefix=OPTIMIZATION_LEVEL_2
8
9; Preserve debug info in thunks under -passes=mergefunc -mergefunc-preserve-debug-info
10;
11; We test that:
12; At -O0 we have preserved the generated @llvm.dbg.declare debug intrinsics.
13; At -O2 we have preserved the generated @llvm.dbg.value debug intrinsics.
14; At -O0, stores from the incoming parameters to locations on the stack-frame
15;         and allocas that create these locations on the stack-frame are preserved.
16; Debug info got generated for the call made by the thunk and for its return value.
17; The foregoing is the only content of a thunk's entry block.
18; A thunk makes a tail call to the shared implementation.
19; A thunk's call site is preserved to point to the thunk (with only -passes=mergefunc the
20;   call site is modified to point to the shared implementation) when both occur
21;   within the same translation unit.
22
23; The source code that was used to test and generate this LLVM IR is:
24;
25; int maxA(int x, int y) {
26;   int i, m, j;
27;   if (x > y)
28;     m = x;
29;   else
30;     m = y;
31;   return m;
32; }
33;
34; int maxB(int x, int y) {
35;   int i, m, j;
36;   if (x > y)
37;     m = x;
38;   else
39;     m = y;
40;   return m;
41; }
42;
43; void f(void) {
44;
45;   maxA(3, 4);
46;   maxB(1, 9);
47; }
48
49; Function Attrs: nounwind uwtable
50define i32 @maxA(i32 %x, i32 %y) !dbg !6 {
51; OPTIMIZATION_LEVEL_0-LABEL: define i32 @maxA
52; OPTIMIZATION_LEVEL_0-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]]) !dbg [[DBG6:![0-9]+]] {
53; OPTIMIZATION_LEVEL_0-NEXT:  entry:
54; OPTIMIZATION_LEVEL_0-NEXT:    [[X_ADDR:%.*]] = alloca i32, align 4
55; OPTIMIZATION_LEVEL_0-NEXT:    [[Y_ADDR:%.*]] = alloca i32, align 4
56; OPTIMIZATION_LEVEL_0-NEXT:    [[I:%.*]] = alloca i32, align 4
57; OPTIMIZATION_LEVEL_0-NEXT:    [[M:%.*]] = alloca i32, align 4
58; OPTIMIZATION_LEVEL_0-NEXT:    [[J:%.*]] = alloca i32, align 4
59; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[X]], ptr [[X_ADDR]], align 4
60; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[X_ADDR]], [[META11:![0-9]+]], !DIExpression(), [[META12:![0-9]+]])
61; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[Y]], ptr [[Y_ADDR]], align 4
62; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[Y_ADDR]], [[META13:![0-9]+]], !DIExpression(), [[META14:![0-9]+]])
63; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[I]], [[META15:![0-9]+]], !DIExpression(), [[META16:![0-9]+]])
64; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[M]], [[META17:![0-9]+]], !DIExpression(), [[META18:![0-9]+]])
65; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[J]], [[META19:![0-9]+]], !DIExpression(), [[META20:![0-9]+]])
66; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP0:%.*]] = load i32, ptr [[X_ADDR]], align 4, !dbg [[DBG21:![0-9]+]]
67; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP1:%.*]] = load i32, ptr [[Y_ADDR]], align 4, !dbg [[DBG23:![0-9]+]]
68; OPTIMIZATION_LEVEL_0-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[TMP0]], [[TMP1]], !dbg [[DBG24:![0-9]+]]
69; OPTIMIZATION_LEVEL_0-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]], !dbg [[DBG25:![0-9]+]]
70; OPTIMIZATION_LEVEL_0:       if.then:
71; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP2:%.*]] = load i32, ptr [[X_ADDR]], align 4, !dbg [[DBG26:![0-9]+]]
72; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[TMP2]], ptr [[M]], align 4, !dbg [[DBG27:![0-9]+]]
73; OPTIMIZATION_LEVEL_0-NEXT:    br label [[IF_END:%.*]], !dbg [[DBG28:![0-9]+]]
74; OPTIMIZATION_LEVEL_0:       if.else:
75; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP3:%.*]] = load i32, ptr [[Y_ADDR]], align 4, !dbg [[DBG29:![0-9]+]]
76; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[TMP3]], ptr [[M]], align 4, !dbg [[DBG30:![0-9]+]]
77; OPTIMIZATION_LEVEL_0-NEXT:    br label [[IF_END]]
78; OPTIMIZATION_LEVEL_0:       if.end:
79; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP4:%.*]] = load i32, ptr [[M]], align 4, !dbg [[DBG31:![0-9]+]]
80; OPTIMIZATION_LEVEL_0-NEXT:    ret i32 [[TMP4]], !dbg [[DBG32:![0-9]+]]
81;
82; OPTIMIZATION_LEVEL_2-LABEL: define i32 @maxA
83; OPTIMIZATION_LEVEL_2-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] !dbg [[DBG6:![0-9]+]] {
84; OPTIMIZATION_LEVEL_2-NEXT:  entry:
85; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_value(i32 [[X]], [[META11:![0-9]+]], !DIExpression(), [[META12:![0-9]+]])
86; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_value(i32 [[Y]], [[META13:![0-9]+]], !DIExpression(), [[META12]])
87; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_declare(ptr undef, [[META14:![0-9]+]], !DIExpression(), [[META15:![0-9]+]])
88; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_declare(ptr undef, [[META16:![0-9]+]], !DIExpression(), [[META17:![0-9]+]])
89; OPTIMIZATION_LEVEL_2-NEXT:    [[X_Y:%.*]] = tail call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
90; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_value(i32 [[X_Y]], [[META18:![0-9]+]], !DIExpression(), [[META12]])
91; OPTIMIZATION_LEVEL_2-NEXT:    ret i32 [[X_Y]], !dbg [[DBG19:![0-9]+]]
92;
93entry:
94  %x.addr = alloca i32, align 4
95  %y.addr = alloca i32, align 4
96  %i = alloca i32, align 4
97  %m = alloca i32, align 4
98  %j = alloca i32, align 4
99  store i32 %x, ptr %x.addr, align 4
100  call void @llvm.dbg.declare(metadata ptr %x.addr, metadata !11, metadata !12), !dbg !13
101  store i32 %y, ptr %y.addr, align 4
102  call void @llvm.dbg.declare(metadata ptr %y.addr, metadata !14, metadata !12), !dbg !15
103  call void @llvm.dbg.declare(metadata ptr %i, metadata !16, metadata !12), !dbg !17
104  call void @llvm.dbg.declare(metadata ptr %m, metadata !18, metadata !12), !dbg !19
105  call void @llvm.dbg.declare(metadata ptr %j, metadata !20, metadata !12), !dbg !21
106  %0 = load i32, ptr %x.addr, align 4, !dbg !22
107  %1 = load i32, ptr %y.addr, align 4, !dbg !24
108  %cmp = icmp sgt i32 %0, %1, !dbg !25
109  br i1 %cmp, label %if.then, label %if.else, !dbg !26
110
111if.then:                                          ; preds = %entry
112  %2 = load i32, ptr %x.addr, align 4, !dbg !27
113  store i32 %2, ptr %m, align 4, !dbg !28
114  br label %if.end, !dbg !29
115
116if.else:                                          ; preds = %entry
117  %3 = load i32, ptr %y.addr, align 4, !dbg !30
118  store i32 %3, ptr %m, align 4, !dbg !31
119  br label %if.end
120
121if.end:                                           ; preds = %if.else, %if.then
122  %4 = load i32, ptr %m, align 4, !dbg !32
123  ret i32 %4, !dbg !33
124}
125
126; Function Attrs: nounwind readnone
127declare void @llvm.dbg.declare(metadata, metadata, metadata)
128
129; Function Attrs: nounwind uwtable
130define i32 @maxB(i32 %x, i32 %y) !dbg !34 {
131; OPTIMIZATION_LEVEL_0-LABEL: define i32 @maxB
132; OPTIMIZATION_LEVEL_0-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]]) !dbg [[DBG33:![0-9]+]] {
133; OPTIMIZATION_LEVEL_0-NEXT:  entry:
134; OPTIMIZATION_LEVEL_0-NEXT:    [[X_ADDR:%.*]] = alloca i32, align 4
135; OPTIMIZATION_LEVEL_0-NEXT:    [[Y_ADDR:%.*]] = alloca i32, align 4
136; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[X]], ptr [[X_ADDR]], align 4
137; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[X_ADDR]], [[META34:![0-9]+]], !DIExpression(), [[META35:![0-9]+]])
138; OPTIMIZATION_LEVEL_0-NEXT:    store i32 [[Y]], ptr [[Y_ADDR]], align 4
139; OPTIMIZATION_LEVEL_0-NEXT:      #dbg_declare(ptr [[Y_ADDR]], [[META36:![0-9]+]], !DIExpression(), [[META37:![0-9]+]])
140; OPTIMIZATION_LEVEL_0-NEXT:    [[TMP0:%.*]] = tail call i32 @maxA(i32 [[X]], i32 [[Y]]), !dbg [[DBG38:![0-9]+]]
141; OPTIMIZATION_LEVEL_0-NEXT:    ret i32 [[TMP0]], !dbg [[DBG38]]
142;
143; OPTIMIZATION_LEVEL_2-LABEL: define i32 @maxB
144; OPTIMIZATION_LEVEL_2-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] !dbg [[DBG20:![0-9]+]] {
145; OPTIMIZATION_LEVEL_2-NEXT:  entry:
146; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_value(i32 [[X]], [[META21:![0-9]+]], !DIExpression(), [[META22:![0-9]+]])
147; OPTIMIZATION_LEVEL_2-NEXT:      #dbg_value(i32 [[Y]], [[META23:![0-9]+]], !DIExpression(), [[META22]])
148; OPTIMIZATION_LEVEL_2-NEXT:    [[TMP0:%.*]] = tail call i32 @maxA(i32 [[X]], i32 [[Y]]) #[[ATTR0]], !dbg [[DBG24:![0-9]+]]
149; OPTIMIZATION_LEVEL_2-NEXT:    ret i32 [[TMP0]], !dbg [[DBG24]]
150;
151
152
153entry:
154  %x.addr = alloca i32, align 4
155  %y.addr = alloca i32, align 4
156  %i = alloca i32, align 4
157  %m = alloca i32, align 4
158  %j = alloca i32, align 4
159  store i32 %x, ptr %x.addr, align 4
160  call void @llvm.dbg.declare(metadata ptr %x.addr, metadata !35, metadata !12), !dbg !36
161  store i32 %y, ptr %y.addr, align 4
162  call void @llvm.dbg.declare(metadata ptr %y.addr, metadata !37, metadata !12), !dbg !38
163  call void @llvm.dbg.declare(metadata ptr %i, metadata !39, metadata !12), !dbg !40
164  call void @llvm.dbg.declare(metadata ptr %m, metadata !41, metadata !12), !dbg !42
165  call void @llvm.dbg.declare(metadata ptr %j, metadata !43, metadata !12), !dbg !44
166  %0 = load i32, ptr %x.addr, align 4, !dbg !45
167  %1 = load i32, ptr %y.addr, align 4, !dbg !47
168  %cmp = icmp sgt i32 %0, %1, !dbg !48
169  br i1 %cmp, label %if.then, label %if.else, !dbg !49
170
171if.then:                                          ; preds = %entry
172  %2 = load i32, ptr %x.addr, align 4, !dbg !50
173  store i32 %2, ptr %m, align 4, !dbg !51
174  br label %if.end, !dbg !52
175
176if.else:                                          ; preds = %entry
177  %3 = load i32, ptr %y.addr, align 4, !dbg !53
178  store i32 %3, ptr %m, align 4, !dbg !54
179  br label %if.end
180
181if.end:                                           ; preds = %if.else, %if.then
182  %4 = load i32, ptr %m, align 4, !dbg !55
183  ret i32 %4, !dbg !56
184}
185
186; Function Attrs: nounwind uwtable
187define void @f() !dbg !57 {
188; OPTIMIZATION_LEVEL_0-LABEL: define void @f
189; OPTIMIZATION_LEVEL_0-SAME: () !dbg [[DBG39:![0-9]+]] {
190; OPTIMIZATION_LEVEL_0-NEXT:  entry:
191; OPTIMIZATION_LEVEL_0-NEXT:    [[CALL:%.*]] = call i32 @maxA(i32 3, i32 4), !dbg [[DBG42:![0-9]+]]
192; OPTIMIZATION_LEVEL_0-NEXT:    [[CALL1:%.*]] = call i32 @maxB(i32 1, i32 9), !dbg [[DBG43:![0-9]+]]
193; OPTIMIZATION_LEVEL_0-NEXT:    ret void, !dbg [[DBG44:![0-9]+]]
194;
195; OPTIMIZATION_LEVEL_2-LABEL: define void @f
196; OPTIMIZATION_LEVEL_2-SAME: () local_unnamed_addr #[[ATTR0]] !dbg [[DBG25:![0-9]+]] {
197; OPTIMIZATION_LEVEL_2-NEXT:  entry:
198; OPTIMIZATION_LEVEL_2-NEXT:    ret void, !dbg [[DBG28:![0-9]+]]
199;
200entry:
201  %call = call i32 @maxA(i32 3, i32 4), !dbg !60
202  %call1 = call i32 @maxB(i32 1, i32 9), !dbg !61
203  ret void, !dbg !62
204}
205
206!llvm.dbg.cu = !{!0}
207!llvm.module.flags = !{!3, !4}
208!llvm.ident = !{!5}
209
210!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
211!1 = !DIFile(filename: "mergefunc-preserve-debug-info.c", directory: "")
212!2 = !{}
213!3 = !{i32 2, !"Dwarf Version", i32 4}
214!4 = !{i32 2, !"Debug Info Version", i32 3}
215!5 = !{!""}
216!6 = distinct !DISubprogram(name: "maxA", scope: !7, file: !7, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
217!7 = !DIFile(filename: "./mergefunc-preserve-debug-info.c", directory: "")
218!8 = !DISubroutineType(types: !9)
219!9 = !{!10, !10, !10}
220!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
221!11 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !7, line: 1, type: !10)
222!12 = !DIExpression()
223!13 = !DILocation(line: 1, column: 14, scope: !6)
224!14 = !DILocalVariable(name: "y", arg: 2, scope: !6, file: !7, line: 1, type: !10)
225!15 = !DILocation(line: 1, column: 21, scope: !6)
226!16 = !DILocalVariable(name: "i", scope: !6, file: !7, line: 2, type: !10)
227!17 = !DILocation(line: 2, column: 7, scope: !6)
228!18 = !DILocalVariable(name: "m", scope: !6, file: !7, line: 2, type: !10)
229!19 = !DILocation(line: 2, column: 10, scope: !6)
230!20 = !DILocalVariable(name: "j", scope: !6, file: !7, line: 2, type: !10)
231!21 = !DILocation(line: 2, column: 13, scope: !6)
232!22 = !DILocation(line: 3, column: 7, scope: !23)
233!23 = distinct !DILexicalBlock(scope: !6, file: !7, line: 3, column: 7)
234!24 = !DILocation(line: 3, column: 11, scope: !23)
235!25 = !DILocation(line: 3, column: 9, scope: !23)
236!26 = !DILocation(line: 3, column: 7, scope: !6)
237!27 = !DILocation(line: 4, column: 9, scope: !23)
238!28 = !DILocation(line: 4, column: 7, scope: !23)
239!29 = !DILocation(line: 4, column: 5, scope: !23)
240!30 = !DILocation(line: 6, column: 9, scope: !23)
241!31 = !DILocation(line: 6, column: 7, scope: !23)
242!32 = !DILocation(line: 7, column: 10, scope: !6)
243!33 = !DILocation(line: 7, column: 3, scope: !6)
244!34 = distinct !DISubprogram(name: "maxB", scope: !7, file: !7, line: 10, type: !8, isLocal: false, isDefinition: true, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
245!35 = !DILocalVariable(name: "x", arg: 1, scope: !34, file: !7, line: 10, type: !10)
246!36 = !DILocation(line: 10, column: 14, scope: !34)
247!37 = !DILocalVariable(name: "y", arg: 2, scope: !34, file: !7, line: 10, type: !10)
248!38 = !DILocation(line: 10, column: 21, scope: !34)
249!39 = !DILocalVariable(name: "i", scope: !34, file: !7, line: 11, type: !10)
250!40 = !DILocation(line: 11, column: 7, scope: !34)
251!41 = !DILocalVariable(name: "m", scope: !34, file: !7, line: 11, type: !10)
252!42 = !DILocation(line: 11, column: 10, scope: !34)
253!43 = !DILocalVariable(name: "j", scope: !34, file: !7, line: 11, type: !10)
254!44 = !DILocation(line: 11, column: 13, scope: !34)
255!45 = !DILocation(line: 12, column: 7, scope: !46)
256!46 = distinct !DILexicalBlock(scope: !34, file: !7, line: 12, column: 7)
257!47 = !DILocation(line: 12, column: 11, scope: !46)
258!48 = !DILocation(line: 12, column: 9, scope: !46)
259!49 = !DILocation(line: 12, column: 7, scope: !34)
260!50 = !DILocation(line: 13, column: 9, scope: !46)
261!51 = !DILocation(line: 13, column: 7, scope: !46)
262!52 = !DILocation(line: 13, column: 5, scope: !46)
263!53 = !DILocation(line: 15, column: 9, scope: !46)
264!54 = !DILocation(line: 15, column: 7, scope: !46)
265!55 = !DILocation(line: 16, column: 10, scope: !34)
266!56 = !DILocation(line: 16, column: 3, scope: !34)
267!57 = distinct !DISubprogram(name: "f", scope: !7, file: !7, line: 19, type: !58, isLocal: false, isDefinition: true, scopeLine: 19, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
268!58 = !DISubroutineType(types: !59)
269!59 = !{null}
270!60 = !DILocation(line: 21, column: 3, scope: !57)
271!61 = !DILocation(line: 22, column: 3, scope: !57)
272!62 = !DILocation(line: 23, column: 1, scope: !57)
273