xref: /llvm-project/llvm/test/CodeGen/MIR/X86/branch-folder-with-label.mir (revision ff9af4c43ad71eeba2cabe99609cfaa0fd54c1d0)
1# Generated with
2#
3# clang -g -O1 -S -emit-llvm test.c
4# llc -stop-before=branch-folder test.ll
5#
6# typedef struct bar {
7#   int data;
8# } bar;
9#
10# int foo(int a)
11# {
12#   return a;
13# }
14#
15# int baz(int *out)
16# {
17#   int ret;
18#
19#   if ((ret = foo(*out)) < 0)
20#     return ret;
21#   if (out)
22#     *out = 1;
23#
24#   return 0;
25# }
26#
27# int test(bar *s)
28# {
29#   int idx, ret;
30#
31# retry:
32#   do {
33#     ret = baz(&idx);
34#     if (ret < 0)
35#       return ret;
36#   } while (idx < 0 || !s->data);
37#
38#   goto retry;
39# }
40#
41# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=branch-folder | FileCheck %s
42--- |
43  ; ModuleID = 'test.ll'
44  source_filename = "test.ll"
45
46  %struct.bar = type { i32 }
47
48  define i32 @foo(i32 returned %a) local_unnamed_addr {
49  entry:
50    ret i32 %a
51  }
52
53  define i32 @baz(ptr %out) local_unnamed_addr !dbg !4 {
54  entry:
55    %0 = load i32, ptr %out, align 4
56    %call = tail call i32 @foo(i32 %0), !dbg !9
57    %cmp = icmp slt i32 %call, 0
58    br i1 %cmp, label %cleanup, label %if.then1
59
60  if.then1:                                         ; preds = %entry
61    store i32 1, ptr %out, align 4
62    br label %cleanup
63
64  cleanup:                                          ; preds = %if.then1, %entry
65    %retval.0 = phi i32 [ %call, %entry ], [ 0, %if.then1 ]
66    ret i32 %retval.0
67  }
68
69  define i32 @test(ptr nocapture readonly %s) local_unnamed_addr !dbg !11 {
70  entry:
71    %idx = alloca i32, align 4
72    call void @llvm.dbg.label(metadata !20), !dbg !21
73    %call58 = call i32 @baz(ptr nonnull %idx), !dbg !22
74    %cmp69 = icmp slt i32 %call58, 0
75    br i1 %cmp69, label %if.then, label %do.cond.lr.ph.lr.ph
76
77  do.cond.lr.ph.lr.ph:                              ; preds = %entry
78    br label %do.cond
79
80  retry.loopexit:                                   ; preds = %lor.rhs
81    call void @llvm.dbg.label(metadata !20), !dbg !21
82    %call5 = call i32 @baz(ptr nonnull %idx), !dbg !22
83    %cmp6 = icmp slt i32 %call5, 0
84    br i1 %cmp6, label %if.then, label %do.cond
85
86  if.then:                                          ; preds = %do.body.backedge, %retry.loopexit, %entry
87    %call.lcssa = phi i32 [ %call58, %entry ], [ %call, %do.body.backedge ], [ %call5, %retry.loopexit ]
88    ret i32 %call.lcssa
89
90  do.cond:                                          ; preds = %retry.loopexit, %do.body.backedge, %do.cond.lr.ph.lr.ph
91    %0 = load i32, ptr %idx, align 4
92    %cmp1 = icmp slt i32 %0, 0
93    br i1 %cmp1, label %do.body.backedge, label %lor.rhs
94
95  lor.rhs:                                          ; preds = %do.cond
96    %1 = bitcast ptr %s to ptr
97    %2 = load i32, ptr %1, align 4
98    %tobool = icmp eq i32 %2, 0
99    br i1 %tobool, label %do.body.backedge, label %retry.loopexit
100
101  do.body.backedge:                                 ; preds = %lor.rhs, %do.cond
102    %call = call i32 @baz(ptr nonnull %idx), !dbg !22
103    %cmp = icmp slt i32 %call, 0
104    br i1 %cmp, label %if.then, label %do.cond
105  }
106
107  ; Function Attrs: nounwind readnone speculatable
108  declare void @llvm.dbg.label(metadata) #0
109
110  ; Function Attrs: nounwind
111  declare void @llvm.stackprotector(ptr, ptr) #1
112
113  attributes #0 = { nounwind readnone speculatable }
114  attributes #1 = { nounwind }
115
116  !llvm.dbg.cu = !{!0}
117  !llvm.module.flags = !{!3}
118
119  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: GNU)
120  !1 = !DIFile(filename: "test.c", directory: "/home/users")
121  !2 = !{}
122  !3 = !{i32 2, !"Debug Info Version", i32 3}
123  !4 = distinct !DISubprogram(name: "baz", scope: !1, file: !1, line: 10, type: !5, isLocal: false, isDefinition: true, scopeLine: 11, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2)
124  !5 = !DISubroutineType(types: !6)
125  !6 = !{!7, !8}
126  !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
127  !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
128  !9 = !DILocation(line: 14, column: 14, scope: !10)
129  !10 = distinct !DILexicalBlock(scope: !4, file: !1, line: 14, column: 7)
130  !11 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 22, type: !12, isLocal: false, isDefinition: true, scopeLine: 23, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !19)
131  !12 = !DISubroutineType(types: !13)
132  !13 = !{!7, !14}
133  !14 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !15, size: 64)
134  !15 = !DIDerivedType(tag: DW_TAG_typedef, name: "bar", file: !1, line: 3, baseType: !16)
135  !16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "bar", file: !1, line: 1, size: 32, elements: !17)
136  !17 = !{!18}
137  !18 = !DIDerivedType(tag: DW_TAG_member, name: "data", scope: !16, file: !1, line: 2, baseType: !7, size: 32)
138  !19 = !{!20}
139  !20 = !DILabel(scope: !11, name: "retry", file: !1, line: 26)
140  !21 = !DILocation(line: 26, column: 1, scope: !11)
141  !22 = !DILocation(line: 28, column: 11, scope: !23)
142  !23 = distinct !DILexicalBlock(scope: !11, file: !1, line: 27, column: 6)
143
144...
145---
146name:            foo
147alignment:       16
148exposesReturnsTwice: false
149legalized:       false
150regBankSelected: false
151selected:        false
152failedISel:      false
153tracksRegLiveness: true
154hasWinCFI:       false
155registers:
156liveins:
157  - { reg: '$edi', virtual-reg: '' }
158frameInfo:
159  isFrameAddressTaken: false
160  isReturnAddressTaken: false
161  hasStackMap:     false
162  hasPatchPoint:   false
163  stackSize:       0
164  offsetAdjustment: 0
165  maxAlignment:    0
166  adjustsStack:    false
167  hasCalls:        false
168  stackProtector:  ''
169  maxCallFrameSize: 0
170  cvBytesOfCalleeSavedRegisters: 0
171  hasOpaqueSPAdjustment: false
172  hasVAStart:      false
173  hasMustTailInVarArgFunc: false
174  localFrameSize:  0
175  savePoint:       ''
176  restorePoint:    ''
177fixedStack:
178stack:
179constants:
180body:             |
181  bb.0.entry:
182    liveins: $edi
183
184    renamable $eax = COPY $edi
185    RET 0, $eax
186
187...
188---
189name:            baz
190alignment:       16
191exposesReturnsTwice: false
192legalized:       false
193regBankSelected: false
194selected:        false
195failedISel:      false
196tracksRegLiveness: true
197hasWinCFI:       false
198registers:
199liveins:
200  - { reg: '$rdi', virtual-reg: '' }
201frameInfo:
202  isFrameAddressTaken: false
203  isReturnAddressTaken: false
204  hasStackMap:     false
205  hasPatchPoint:   false
206  stackSize:       8
207  offsetAdjustment: 0
208  maxAlignment:    0
209  adjustsStack:    true
210  hasCalls:        true
211  stackProtector:  ''
212  maxCallFrameSize: 0
213  cvBytesOfCalleeSavedRegisters: 8
214  hasOpaqueSPAdjustment: false
215  hasVAStart:      false
216  hasMustTailInVarArgFunc: false
217  localFrameSize:  0
218  savePoint:       ''
219  restorePoint:    ''
220fixedStack:
221  - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: default,
222      callee-saved-register: '$rbx', callee-saved-restored: true, debug-info-variable: '',
223      debug-info-expression: '', debug-info-location: '' }
224stack:
225constants:
226body:             |
227  bb.0.entry:
228    successors: %bb.1(0x30000000), %bb.2(0x50000000)
229    liveins: $rdi, $rbx
230
231    frame-setup PUSH64r killed $rbx, implicit-def $rsp, implicit $rsp
232    CFI_INSTRUCTION def_cfa_offset 16
233    CFI_INSTRUCTION offset $rbx, -16
234    renamable $rbx = COPY $rdi
235    renamable $edi = MOV32rm $rdi, 1, $noreg, 0, $noreg :: (load (s32) from %ir.out)
236    CALL64pcrel32 @foo, csr_64, implicit $rsp, implicit $ssp, implicit $edi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax, debug-location !9
237    TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
238    JCC_1 %bb.2, 9, implicit killed $eflags
239  ; CHECK: JCC_1 %bb.2, 8, implicit $eflags
240
241  bb.1:
242    successors: %bb.3(0x80000000)
243    liveins: $eax
244
245    JMP_1 %bb.3
246
247  bb.2.if.then1:
248    successors: %bb.3(0x80000000)
249    liveins: $rbx
250
251    MOV32mi killed renamable $rbx, 1, $noreg, 0, $noreg, 1 :: (store (s32) into %ir.out)
252    renamable $eax = MOV32r0 implicit-def dead $eflags
253
254  bb.3.cleanup:
255    liveins: $eax
256
257    $rbx = frame-destroy POP64r implicit-def $rsp, implicit $rsp
258    CFI_INSTRUCTION def_cfa_offset 8
259    RET 0, $eax
260
261...
262---
263name:            test
264alignment:       16
265exposesReturnsTwice: false
266legalized:       false
267regBankSelected: false
268selected:        false
269failedISel:      false
270tracksRegLiveness: true
271hasWinCFI:       false
272registers:
273liveins:
274  - { reg: '$rdi', virtual-reg: '' }
275frameInfo:
276  isFrameAddressTaken: false
277  isReturnAddressTaken: false
278  hasStackMap:     false
279  hasPatchPoint:   false
280  stackSize:       24
281  offsetAdjustment: 0
282  maxAlignment:    4
283  adjustsStack:    true
284  hasCalls:        true
285  stackProtector:  ''
286  maxCallFrameSize: 0
287  cvBytesOfCalleeSavedRegisters: 16
288  hasOpaqueSPAdjustment: false
289  hasVAStart:      false
290  hasMustTailInVarArgFunc: false
291  localFrameSize:  0
292  savePoint:       ''
293  restorePoint:    ''
294fixedStack:
295  - { id: 0, type: spill-slot, offset: -24, size: 8, alignment: 8, stack-id: default,
296      callee-saved-register: '$rbx', callee-saved-restored: true, debug-info-variable: '',
297      debug-info-expression: '', debug-info-location: '' }
298  - { id: 1, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: default,
299      callee-saved-register: '$r14', callee-saved-restored: true, debug-info-variable: '',
300      debug-info-expression: '', debug-info-location: '' }
301stack:
302  - { id: 0, name: idx, type: default, offset: -28, size: 4, alignment: 4,
303      stack-id: default, callee-saved-register: '', callee-saved-restored: true,
304      debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
305constants:
306body:             |
307  bb.0.entry:
308    successors: %bb.1(0x30000000), %bb.2(0x50000000)
309    liveins: $rdi, $r14, $rbx
310
311    frame-setup PUSH64r killed $r14, implicit-def $rsp, implicit $rsp
312    CFI_INSTRUCTION def_cfa_offset 16
313    frame-setup PUSH64r killed $rbx, implicit-def $rsp, implicit $rsp
314    CFI_INSTRUCTION def_cfa_offset 24
315    frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
316    CFI_INSTRUCTION def_cfa_offset 32
317    CFI_INSTRUCTION offset $rbx, -24
318    CFI_INSTRUCTION offset $r14, -16
319    renamable $rbx = COPY $rdi
320    DBG_LABEL !20, debug-location !21
321    renamable $rdi = LEA64r $rsp, 1, $noreg, 4, $noreg
322    CALL64pcrel32 @baz, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax, debug-location !22
323    TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
324    JCC_1 %bb.2, 9, implicit killed $eflags
325  ; CHECK: JCC_1 %bb.5, 8, implicit $eflags
326
327  bb.1:
328    successors: %bb.5(0x80000000)
329    liveins: $eax
330
331    JMP_1 %bb.5
332
333  bb.2:
334    successors: %bb.6(0x80000000)
335    liveins: $rbx
336
337    renamable $r14 = LEA64r $rsp, 1, $noreg, 4, $noreg
338    JMP_1 %bb.6
339
340  bb.3.retry.loopexit:
341    successors: %bb.4(0x04000000), %bb.6(0x7c000000)
342    liveins: $rbx, $r14
343
344    DBG_LABEL !20, debug-location !21
345    $rdi = COPY renamable $r14, debug-location !22
346    CALL64pcrel32 @baz, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax, debug-location !22
347    TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
348    JCC_1 %bb.6, 9, implicit killed $eflags
349
350  bb.4:
351    successors: %bb.5(0x80000000)
352    liveins: $eax
353
354
355  bb.5.if.then:
356    liveins: $eax
357
358    $rsp = frame-destroy ADD64ri32 $rsp, 8, implicit-def dead $eflags
359    CFI_INSTRUCTION def_cfa_offset 24
360    $rbx = frame-destroy POP64r implicit-def $rsp, implicit $rsp
361    CFI_INSTRUCTION def_cfa_offset 16
362    $r14 = frame-destroy POP64r implicit-def $rsp, implicit $rsp
363    CFI_INSTRUCTION def_cfa_offset 8
364    RET 0, $eax
365
366  bb.6.do.cond:
367    successors: %bb.8(0x30000000), %bb.7(0x50000000)
368    liveins: $rbx, $r14
369
370    CMP32mi $rsp, 1, $noreg, 4, $noreg, 0, implicit-def $eflags :: (dereferenceable load (s32) from %ir.idx)
371    JCC_1 %bb.8, 8, implicit killed $eflags
372    JMP_1 %bb.7
373
374  bb.7.lor.rhs:
375    successors: %bb.8(0x30000000), %bb.3(0x50000000)
376    liveins: $rbx, $r14
377
378    CMP32mi renamable $rbx, 1, $noreg, 0, $noreg, 0, implicit-def $eflags :: (load (s32) from %ir.1)
379    JCC_1 %bb.3, 5, implicit killed $eflags
380    JMP_1 %bb.8
381
382  bb.8.do.body.backedge:
383    successors: %bb.9(0x04000000), %bb.6(0x7c000000)
384    liveins: $rbx, $r14
385
386    $rdi = COPY renamable $r14, debug-location !22
387    CALL64pcrel32 @baz, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, implicit-def $eax, debug-location !22
388    TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
389    JCC_1 %bb.6, 9, implicit killed $eflags
390
391  bb.9:
392    successors: %bb.5(0x80000000)
393    liveins: $eax
394
395    JMP_1 %bb.5
396
397...
398