xref: /llvm-project/llvm/test/CodeGen/X86/tail-dup-merge-loop-headers.ll (revision 51f48295cbe8fa3a44db263b528dd9f7bae7bf9a)
1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
3
4; Function Attrs: nounwind uwtable
5define void @tail_dup_merge_loops(i32 %a, i8* %b, i8* %c) local_unnamed_addr #0 {
6; CHECK-LABEL: tail_dup_merge_loops:
7; CHECK:       # %bb.0: # %entry
8; CHECK-NEXT:    testl %edi, %edi
9; CHECK-NEXT:    jne .LBB0_2
10; CHECK-NEXT:    jmp .LBB0_5
11; CHECK-NEXT:  .LBB0_3: # %inner_loop_exit
12; CHECK-NEXT:    # in Loop: Header=BB0_2 Depth=1
13; CHECK-NEXT:    incq %rsi
14; CHECK-NEXT:    testl %edi, %edi
15; CHECK-NEXT:    jne .LBB0_2
16; CHECK-NEXT:    jmp .LBB0_5
17; CHECK-NEXT:    .p2align 4, 0x90
18; CHECK-NEXT:  .LBB0_4: # %inner_loop_latch
19; CHECK-NEXT:    # in Loop: Header=BB0_2 Depth=1
20; CHECK-NEXT:    addq $2, %rsi
21; CHECK-NEXT:  .LBB0_2: # %inner_loop_top
22; CHECK-NEXT:    # =>This Inner Loop Header: Depth=1
23; CHECK-NEXT:    cmpb $0, (%rsi)
24; CHECK-NEXT:    jns .LBB0_4
25; CHECK-NEXT:    jmp .LBB0_3
26; CHECK-NEXT:  .LBB0_5: # %exit
27; CHECK-NEXT:    retq
28entry:
29  %notlhs674.i = icmp eq i32 %a, 0
30  br label %outer_loop_top
31
32outer_loop_top:                         ; preds = %inner_loop_exit, %entry
33  %dst.0.ph.i = phi i8* [ %b, %entry ], [ %scevgep679.i, %inner_loop_exit ]
34  br i1 %notlhs674.i, label %exit, label %inner_loop_preheader
35
36inner_loop_preheader:                           ; preds = %outer_loop_top
37  br label %inner_loop_top
38
39inner_loop_top:                                     ; preds = %inner_loop_latch, %inner_loop_preheader
40  %dst.0.i = phi i8* [ %inc, %inner_loop_latch ], [ %dst.0.ph.i, %inner_loop_preheader ]
41  %var = load i8, i8* %dst.0.i
42  %tobool1.i = icmp slt i8 %var, 0
43  br label %inner_loop_test
44
45inner_loop_test:                                       ; preds = %inner_loop_top
46  br i1 %tobool1.i, label %inner_loop_exit, label %inner_loop_latch
47
48inner_loop_exit:                       ; preds = %inner_loop_test
49  %scevgep.i = getelementptr i8, i8* %dst.0.i, i64 1
50  %scevgep679.i = getelementptr i8, i8* %scevgep.i, i64 0
51  br label %outer_loop_top
52
53inner_loop_latch:                                ; preds = %inner_loop_test
54  %cmp75.i = icmp ult i8* %dst.0.i, %c
55  %inc = getelementptr i8, i8* %dst.0.i, i64 2
56  br label %inner_loop_top
57
58exit:                              ; preds = %outer_loop_top
59  ret void
60}
61
62@.str.6 = external unnamed_addr constant [23 x i8], align 1
63
64; There is an erroneus check in LoopBase::addBasicBlockToLoop(), where it
65; assumes that the header block for a loop is unique.
66; For most of compilation this assumption is true, but during layout we allow
67; this assumption to be violated. The following code will trigger the bug:
68
69; The loops in question is eventually headed by the block shared_loop_header
70;
71; During layout The block labeled outer_loop_header gets tail-duplicated into
72; outer_loop_latch, and into shared_preheader, and then removed. This leaves
73; shared_loop_header as the header of both loops. The end result
74; is that there are 2 valid loops, and that they share a header. If we re-ran
75; the loop analysis, it would classify this as a single loop.
76; So far this is fine as far as layout is concerned.
77; After layout we tail merge blocks merge_other and merge_predecessor_split.
78; We do this even though they share only a single instruction, because
79; merge_predecessor_split falls through to their shared successor:
80; outer_loop_latch.
81; The rest of the blocks in the function are noise unfortunately. Bugpoint
82; couldn't shrink the test any further.
83
84define i32 @loop_shared_header(i8* %exe, i32 %exesz, i32 %headsize, i32 %min, i32 %wwprva, i32 %e_lfanew, i8* readonly %wwp, i32 %wwpsz, i16 zeroext %sects) local_unnamed_addr #0 {
85; CHECK-LABEL: loop_shared_header:
86; CHECK:       # %bb.0: # %entry
87; CHECK-NEXT:    pushq %rbp
88; CHECK-NEXT:    pushq %r15
89; CHECK-NEXT:    pushq %r14
90; CHECK-NEXT:    pushq %r13
91; CHECK-NEXT:    pushq %r12
92; CHECK-NEXT:    pushq %rbx
93; CHECK-NEXT:    pushq %rax
94; CHECK-NEXT:    movl $1, %ebx
95; CHECK-NEXT:    xorl %eax, %eax
96; CHECK-NEXT:    testb %al, %al
97; CHECK-NEXT:    jne .LBB1_26
98; CHECK-NEXT:  # %bb.1: # %if.end19
99; CHECK-NEXT:    movl %esi, %r13d
100; CHECK-NEXT:    movq %rdi, %r12
101; CHECK-NEXT:    movl (%rax), %ebp
102; CHECK-NEXT:    leal (,%rbp,4), %r14d
103; CHECK-NEXT:    movl %r14d, %r15d
104; CHECK-NEXT:    movl $1, %esi
105; CHECK-NEXT:    movq %r15, %rdi
106; CHECK-NEXT:    callq cli_calloc
107; CHECK-NEXT:    testl %r13d, %r13d
108; CHECK-NEXT:    je .LBB1_25
109; CHECK-NEXT:  # %bb.2: # %if.end19
110; CHECK-NEXT:    testl %ebp, %ebp
111; CHECK-NEXT:    je .LBB1_25
112; CHECK-NEXT:  # %bb.3: # %if.end19
113; CHECK-NEXT:    movq %rax, %rbx
114; CHECK-NEXT:    xorl %eax, %eax
115; CHECK-NEXT:    testb %al, %al
116; CHECK-NEXT:    jne .LBB1_25
117; CHECK-NEXT:  # %bb.4: # %if.end19
118; CHECK-NEXT:    cmpq %r12, %rbx
119; CHECK-NEXT:    jb .LBB1_25
120; CHECK-NEXT:  # %bb.5: # %if.end50
121; CHECK-NEXT:    movq %rbx, %rdi
122; CHECK-NEXT:    movq %r15, %rdx
123; CHECK-NEXT:    callq memcpy
124; CHECK-NEXT:    cmpl $4, %r14d
125; CHECK-NEXT:    jb .LBB1_28
126; CHECK-NEXT:  # %bb.6: # %shared_preheader
127; CHECK-NEXT:    movb $32, %dl
128; CHECK-NEXT:    xorl %eax, %eax
129; CHECK-NEXT:    # implicit-def: $rcx
130; CHECK-NEXT:    testl %ebp, %ebp
131; CHECK-NEXT:    je .LBB1_18
132; CHECK-NEXT:    .p2align 4, 0x90
133; CHECK-NEXT:  .LBB1_8: # %shared_loop_header
134; CHECK-NEXT:    # =>This Inner Loop Header: Depth=1
135; CHECK-NEXT:    testq %rbx, %rbx
136; CHECK-NEXT:    jne .LBB1_27
137; CHECK-NEXT:  # %bb.9: # %inner_loop_body
138; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
139; CHECK-NEXT:    testl %eax, %eax
140; CHECK-NEXT:    jns .LBB1_8
141; CHECK-NEXT:  # %bb.10: # %if.end96.i
142; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
143; CHECK-NEXT:    cmpl $3, %ebp
144; CHECK-NEXT:    jae .LBB1_22
145; CHECK-NEXT:  # %bb.11: # %if.end287.i
146; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
147; CHECK-NEXT:    xorl %esi, %esi
148; CHECK-NEXT:    cmpl $1, %ebp
149; CHECK-NEXT:    setne %dl
150; CHECK-NEXT:    testb %al, %al
151; CHECK-NEXT:    jne .LBB1_15
152; CHECK-NEXT:  # %bb.12: # %if.end308.i
153; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
154; CHECK-NEXT:    testb %al, %al
155; CHECK-NEXT:    je .LBB1_17
156; CHECK-NEXT:  # %bb.13: # %if.end335.i
157; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
158; CHECK-NEXT:    xorl %edx, %edx
159; CHECK-NEXT:    testb %dl, %dl
160; CHECK-NEXT:    movl $0, %esi
161; CHECK-NEXT:    jne .LBB1_7
162; CHECK-NEXT:  # %bb.14: # %merge_other
163; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
164; CHECK-NEXT:    xorl %esi, %esi
165; CHECK-NEXT:    jmp .LBB1_16
166; CHECK-NEXT:  .LBB1_15: # in Loop: Header=BB1_8 Depth=1
167; CHECK-NEXT:    movb %dl, %sil
168; CHECK-NEXT:    addl $3, %esi
169; CHECK-NEXT:  .LBB1_16: # %outer_loop_latch
170; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
171; CHECK-NEXT:    # implicit-def: $dl
172; CHECK-NEXT:    jmp .LBB1_7
173; CHECK-NEXT:  .LBB1_17: # %merge_predecessor_split
174; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
175; CHECK-NEXT:    movb $32, %dl
176; CHECK-NEXT:    xorl %esi, %esi
177; CHECK-NEXT:  .LBB1_7: # %outer_loop_latch
178; CHECK-NEXT:    # in Loop: Header=BB1_8 Depth=1
179; CHECK-NEXT:    movzwl %si, %esi
180; CHECK-NEXT:    decl %esi
181; CHECK-NEXT:    movzwl %si, %esi
182; CHECK-NEXT:    leaq 1(%rcx,%rsi), %rcx
183; CHECK-NEXT:    testl %ebp, %ebp
184; CHECK-NEXT:    jne .LBB1_8
185; CHECK-NEXT:  .LBB1_18: # %while.cond.us1412.i
186; CHECK-NEXT:    xorl %eax, %eax
187; CHECK-NEXT:    testb %al, %al
188; CHECK-NEXT:    movl $1, %ebx
189; CHECK-NEXT:    jne .LBB1_20
190; CHECK-NEXT:  # %bb.19: # %while.cond.us1412.i
191; CHECK-NEXT:    decb %dl
192; CHECK-NEXT:    jne .LBB1_26
193; CHECK-NEXT:  .LBB1_20: # %if.end41.us1436.i
194; CHECK-NEXT:  .LBB1_25:
195; CHECK-NEXT:    movl $1, %ebx
196; CHECK-NEXT:    jmp .LBB1_26
197; CHECK-NEXT:  .LBB1_22: # %if.then99.i
198; CHECK-NEXT:    xorl %ebx, %ebx
199; CHECK-NEXT:    movl $.str.6, %edi
200; CHECK-NEXT:    xorl %eax, %eax
201; CHECK-NEXT:    callq cli_dbgmsg
202; CHECK-NEXT:  .LBB1_26: # %cleanup
203; CHECK-NEXT:    movl %ebx, %eax
204; CHECK-NEXT:    addq $8, %rsp
205; CHECK-NEXT:    popq %rbx
206; CHECK-NEXT:    popq %r12
207; CHECK-NEXT:    popq %r13
208; CHECK-NEXT:    popq %r14
209; CHECK-NEXT:    popq %r15
210; CHECK-NEXT:    popq %rbp
211; CHECK-NEXT:    retq
212; CHECK-NEXT:  .LBB1_27: # %wunpsect.exit.thread.loopexit389
213; CHECK-NEXT:  .LBB1_28: # %wunpsect.exit.thread.loopexit391
214entry:
215  %0 = load i32, i32* undef, align 4
216  %mul = shl nsw i32 %0, 2
217  br i1 undef, label %if.end19, label %cleanup
218
219if.end19:                                         ; preds = %entry
220  %conv = zext i32 %mul to i64
221  %call = tail call i8* @cli_calloc(i64 %conv, i64 1)
222  %1 = icmp eq i32 %exesz, 0
223  %notrhs = icmp eq i32 %0, 0
224  %or.cond117.not = or i1 %1, %notrhs
225  %or.cond202 = or i1 %or.cond117.not, undef
226  %cmp35 = icmp ult i8* %call, %exe
227  %or.cond203 = or i1 %or.cond202, %cmp35
228  br i1 %or.cond203, label %cleanup, label %if.end50
229
230if.end50:                                         ; preds = %if.end19
231  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull %call, i8* undef, i64 %conv, i1 false)
232  %cmp1.i.i = icmp ugt i32 %mul, 3
233  br i1 %cmp1.i.i, label %shared_preheader, label %wunpsect.exit.thread.loopexit391
234
235shared_preheader:                                 ; preds = %if.end50
236  br label %outer_loop_header
237
238outer_loop_header:                                ; preds = %outer_loop_latch, %shared_preheader
239  %bits.1.i = phi i8 [ 32, %shared_preheader ], [ %bits.43.i, %outer_loop_latch ]
240  %dst.0.ph.i = phi i8* [ undef, %shared_preheader ], [ %scevgep679.i, %outer_loop_latch ]
241  %2 = icmp eq i32 %0, 0
242  br i1 %2, label %while.cond.us1412.i, label %shared_loop_header
243
244while.cond.us1412.i:                              ; preds = %outer_loop_header
245  %.pre.i = add i8 %bits.1.i, -1
246  %tobool2.us1420.i = icmp eq i8 %.pre.i, 0
247  %or.cond.us1421.i = or i1 undef, %tobool2.us1420.i
248  br i1 %or.cond.us1421.i, label %if.end41.us1436.i, label %cleanup
249
250if.end41.us1436.i:                                ; preds = %while.cond.us1412.i
251  unreachable
252
253shared_loop_header:                               ; preds = %dup_early2, %dup_early1
254  %dst.0.i = phi i8* [ undef, %inner_loop_body ], [ %dst.0.ph.i, %outer_loop_header ], [ undef, %dead_block ]
255  %cmp3.i1172.i = icmp ult i8* null, %call
256  br i1 %cmp3.i1172.i, label %wunpsect.exit.thread.loopexit389, label %inner_loop_body
257
258inner_loop_body:                                  ; preds = %shared_loop_header
259  %3 = icmp slt i32 undef, 0
260  br i1 %3, label %if.end96.i, label %shared_loop_header
261
262dead_block:                                       ; preds = %inner_loop_body
263  %cmp75.i = icmp ult i8* %dst.0.i, null
264  br label %shared_loop_header
265
266if.end96.i:                                       ; preds = %inner_loop_body
267  %cmp97.i = icmp ugt i32 %0, 2
268  br i1 %cmp97.i, label %if.then99.i, label %if.end287.i
269
270if.then99.i:                                      ; preds = %if.end96.i
271  tail call void (i8*, ...) @cli_dbgmsg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 undef)
272  br label %cleanup
273
274if.end287.i:                                      ; preds = %if.end96.i
275  %cmp291.i = icmp ne i32 %0, 1
276  %conv294.i = select i1 %cmp291.i, i16 4, i16 3
277  br i1 undef, label %if.end308.i, label %outer_loop_latch
278
279if.end308.i:                                      ; preds = %if.end287.i
280  br i1 undef, label %if.end335.i, label %merge_predecessor_split
281
282merge_predecessor_split:                          ; preds = %if.end308.i
283  %4 = bitcast i8* undef to i32*
284  br label %outer_loop_latch
285
286if.end335.i:                                      ; preds = %if.end308.i
287  br i1 undef, label %outer_loop_latch, label %merge_other
288
289merge_other:                                      ; preds = %if.end335.i
290  br label %outer_loop_latch
291
292outer_loop_latch:                                 ; preds = %merge_other, %if.end335.i, %merge_predecessor_split, %if.end287.i
293  %bits.43.i = phi i8 [ undef, %if.end287.i ], [ undef, %merge_other ], [ 32, %merge_predecessor_split ], [ 0, %if.end335.i ]
294  %backsize.0.i = phi i16 [ %conv294.i, %if.end287.i ], [ 0, %merge_other ], [ 0, %merge_predecessor_split ], [ 0, %if.end335.i ]
295  %5 = add i16 %backsize.0.i, -1
296  %6 = zext i16 %5 to i64
297  %scevgep.i = getelementptr i8, i8* %dst.0.ph.i, i64 1
298  %scevgep679.i = getelementptr i8, i8* %scevgep.i, i64 %6
299  br label %outer_loop_header
300
301wunpsect.exit.thread.loopexit389:                 ; preds = %shared_loop_header
302  unreachable
303
304wunpsect.exit.thread.loopexit391:                 ; preds = %if.end50
305  unreachable
306
307cleanup:                                          ; preds = %if.then99.i, %while.cond.us1412.i, %if.end19, %entry
308  %retval.0 = phi i32 [ 0, %if.then99.i ], [ 1, %entry ], [ 1, %if.end19 ], [ 1, %while.cond.us1412.i ]
309  ret i32 %retval.0
310}
311
312; Function Attrs: nounwind
313declare void @cli_dbgmsg(i8*, ...) local_unnamed_addr #0
314
315; Function Attrs: nounwind
316declare i8* @cli_calloc(i64, i64) local_unnamed_addr #0
317
318; Function Attrs: argmemonly nounwind
319declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1) #1
320attributes #0 = { nounwind }
321attributes #1 = { argmemonly nounwind }
322