[PowerPC] Recommit r314244 with refactoring and off by default
[llvm-core.git] / test / CodeGen / X86 / x86-shrink-wrapping.ll
blob519f0d0924e37e3e96e9923afd061aed06fe85cc
1 ; RUN: llc %s -o - -enable-shrink-wrap=true | FileCheck %s --check-prefix=CHECK --check-prefix=ENABLE
2 ; RUN: llc %s -o - -enable-shrink-wrap=false | FileCheck %s --check-prefix=CHECK --check-prefix=DISABLE
4 ; Note: Lots of tests use inline asm instead of regular calls.
5 ; This allows to have a better control on what the allocation will do.
6 ; Otherwise, we may have spill right in the entry block, defeating
7 ; shrink-wrapping. Moreover, some of the inline asm statement (nop)
8 ; are here to ensure that the related paths do not end up as critical
9 ; edges.
10 target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
11 target triple = "x86_64-apple-macosx"
14 ; Initial motivating example: Simple diamond with a call just on one side.
15 ; CHECK-LABEL: foo:
17 ; Compare the arguments and jump to exit.
18 ; No prologue needed.
19 ; ENABLE: movl %edi, [[ARG0CPY:%e[a-z]+]]
20 ; ENABLE-NEXT: cmpl %esi, [[ARG0CPY]]
21 ; ENABLE-NEXT: jge [[EXIT_LABEL:LBB[0-9_]+]]
23 ; Prologue code.
24 ; (What we push does not matter. It should be some random sratch register.)
25 ; CHECK: pushq
27 ; Compare the arguments and jump to exit.
28 ; After the prologue is set.
29 ; DISABLE: movl %edi, [[ARG0CPY:%e[a-z]+]]
30 ; DISABLE-NEXT: cmpl %esi, [[ARG0CPY]]
31 ; DISABLE-NEXT: jge [[EXIT_LABEL:LBB[0-9_]+]]
33 ; Store %a in the alloca.
34 ; CHECK: movl [[ARG0CPY]], 4(%rsp)
35 ; Set the alloca address in the second argument.
36 ; CHECK-NEXT: leaq 4(%rsp), %rsi
37 ; Set the first argument to zero.
38 ; CHECK-NEXT: xorl %edi, %edi
39 ; CHECK-NEXT: callq _doSomething
41 ; With shrink-wrapping, epilogue is just after the call.
42 ; ENABLE-NEXT: addq $8, %rsp
44 ; CHECK: [[EXIT_LABEL]]:
46 ; Without shrink-wrapping, epilogue is in the exit block.
47 ; Epilogue code. (What we pop does not matter.)
48 ; DISABLE-NEXT: popq
50 ; CHECK-NEXT: retq
51 define i32 @foo(i32 %a, i32 %b) {
52   %tmp = alloca i32, align 4
53   %tmp2 = icmp slt i32 %a, %b
54   br i1 %tmp2, label %true, label %false
56 true:
57   store i32 %a, i32* %tmp, align 4
58   %tmp4 = call i32 @doSomething(i32 0, i32* %tmp)
59   br label %false
61 false:
62   %tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ]
63   ret i32 %tmp.0
66 ; Function Attrs: optsize
67 declare i32 @doSomething(i32, i32*)
70 ; Check that we do not perform the restore inside the loop whereas the save
71 ; is outside.
72 ; CHECK-LABEL: freqSaveAndRestoreOutsideLoop:
74 ; Shrink-wrapping allows to skip the prologue in the else case.
75 ; ENABLE: testl %edi, %edi  
76 ; ENABLE: je [[ELSE_LABEL:LBB[0-9_]+]]
78 ; Prologue code.
79 ; Make sure we save the CSR used in the inline asm: rbx.
80 ; CHECK: pushq %rbx
82 ; DISABLE: testl %edi, %edi
83 ; DISABLE: je [[ELSE_LABEL:LBB[0-9_]+]]
85 ; SUM is in %esi because it is coalesced with the second
86 ; argument on the else path.
87 ; CHECK: xorl [[SUM:%esi]], [[SUM]]
88 ; CHECK-NEXT: movl $10, [[IV:%e[a-z]+]]
90 ; Next BB.
91 ; CHECK: [[LOOP:LBB[0-9_]+]]: ## %for.body
92 ; CHECK: movl $1, [[TMP:%e[a-z]+]]
93 ; CHECK: addl [[TMP]], [[SUM]]
94 ; CHECK-NEXT: decl [[IV]]
95 ; CHECK-NEXT: jne [[LOOP]]
97 ; Next BB.
98 ; SUM << 3.
99 ; CHECK: shll $3, [[SUM]]
101 ; Jump to epilogue.
102 ; DISABLE: jmp [[EPILOG_BB:LBB[0-9_]+]]
104 ; DISABLE: [[ELSE_LABEL]]: ## %if.else
105 ; Shift second argument by one and store into returned register.
106 ; DISABLE: addl %esi, %esi
107 ; DISABLE: [[EPILOG_BB]]: ## %if.end
109 ; Epilogue code.
110 ; CHECK-DAG: popq %rbx
111 ; CHECK-DAG: movl %esi, %eax
112 ; CHECK: retq
114 ; ENABLE: [[ELSE_LABEL]]: ## %if.else
115 ; Shift second argument by one and store into returned register.
116 ; ENABLE: addl %esi, %esi
117 ; ENABLE-NEXT: movl %esi, %eax
118 ; ENABLE-NEXT: retq
119 define i32 @freqSaveAndRestoreOutsideLoop(i32 %cond, i32 %N) {
120 entry:
121   %tobool = icmp eq i32 %cond, 0
122   br i1 %tobool, label %if.else, label %for.preheader
124 for.preheader:
125   tail call void asm "nop", ""()
126   br label %for.body
128 for.body:                                         ; preds = %entry, %for.body
129   %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.preheader ]
130   %sum.04 = phi i32 [ %add, %for.body ], [ 0, %for.preheader ]
131   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
132   %add = add nsw i32 %call, %sum.04
133   %inc = add nuw nsw i32 %i.05, 1
134   %exitcond = icmp eq i32 %inc, 10
135   br i1 %exitcond, label %for.end, label %for.body
137 for.end:                                          ; preds = %for.body
138   %shl = shl i32 %add, 3
139   br label %if.end
141 if.else:                                          ; preds = %entry
142   %mul = shl nsw i32 %N, 1
143   br label %if.end
145 if.end:                                           ; preds = %if.else, %for.end
146   %sum.1 = phi i32 [ %shl, %for.end ], [ %mul, %if.else ]
147   ret i32 %sum.1
150 declare i32 @something(...)
152 ; Check that we do not perform the shrink-wrapping inside the loop even
153 ; though that would be legal. The cost model must prevent that.
154 ; CHECK-LABEL: freqSaveAndRestoreOutsideLoop2:
155 ; Prologue code.
156 ; Make sure we save the CSR used in the inline asm: rbx.
157 ; CHECK: pushq %rbx
158 ; CHECK: nop
159 ; CHECK: xorl [[SUM:%e[a-z]+]], [[SUM]]
160 ; CHECK-NEXT: movl $10, [[IV:%e[a-z]+]]
161 ; Next BB.
162 ; CHECK: [[LOOP_LABEL:LBB[0-9_]+]]: ## %for.body
163 ; CHECK: movl $1, [[TMP:%e[a-z]+]]
164 ; CHECK: addl [[TMP]], [[SUM]]
165 ; CHECK-NEXT: decl [[IV]]
166 ; CHECK-NEXT: jne [[LOOP_LABEL]]
167 ; Next BB.
168 ; CHECK: ## %for.exit
169 ; CHECK: nop
170 ; CHECK: popq %rbx
171 ; CHECK-NEXT: retq
172 define i32 @freqSaveAndRestoreOutsideLoop2(i32 %cond) {
173 entry:
174   br label %for.preheader
176 for.preheader:
177   tail call void asm "nop", ""()
178   br label %for.body
180 for.body:                                         ; preds = %for.body, %entry
181   %i.04 = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]
182   %sum.03 = phi i32 [ 0, %for.preheader ], [ %add, %for.body ]
183   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
184   %add = add nsw i32 %call, %sum.03
185   %inc = add nuw nsw i32 %i.04, 1
186   %exitcond = icmp eq i32 %inc, 10
187   br i1 %exitcond, label %for.exit, label %for.body
189 for.exit:
190   tail call void asm "nop", ""()
191   br label %for.end
193 for.end:                                          ; preds = %for.body
194   ret i32 %add
197 ; Check with a more complex case that we do not have save within the loop and
198 ; restore outside.
199 ; CHECK-LABEL: loopInfoSaveOutsideLoop:
201 ; ENABLE: testl %edi, %edi
202 ; ENABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
204 ; Prologue code.
205 ; Make sure we save the CSR used in the inline asm: rbx.
206 ; CHECK: pushq %rbx
208 ; DISABLE: testl %edi, %edi
209 ; DISABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
211 ; CHECK: nop
212 ; CHECK: xorl [[SUM:%esi]], [[SUM]]
213 ; CHECK-NEXT: movl $10, [[IV:%e[a-z]+]]
215 ; CHECK: [[LOOP_LABEL:LBB[0-9_]+]]: ## %for.body
216 ; CHECK: movl $1, [[TMP:%e[a-z]+]]
217 ; CHECK: addl [[TMP]], [[SUM]]
218 ; CHECK-NEXT: decl [[IV]]
219 ; CHECK-NEXT: jne [[LOOP_LABEL]]
220 ; Next BB.
221 ; CHECK: nop
222 ; CHECK: shll $3, [[SUM]]
224 ; DISABLE: jmp [[EPILOG_BB:LBB[0-9_]+]]
226 ; DISABLE: [[ELSE_LABEL]]: ## %if.else
227 ; Shift second argument by one and store into returned register.
228 ; DISABLE: addl %esi, %esi
229 ; DISABLE: [[EPILOG_BB]]: ## %if.end
231 ; Epilogue code.
232 ; CHECK-DAG: popq %rbx
233 ; CHECK-DAG: movl %esi, %eax
234 ; CHECK: retq
236 ; ENABLE: [[ELSE_LABEL]]: ## %if.else
237 ; Shift second argument by one and store into returned register.
238 ; ENABLE: addl %esi, %esi
239 ; ENABLE-NEXT: movl %esi, %eax
240 ; ENABLE-NEXT: retq
241 define i32 @loopInfoSaveOutsideLoop(i32 %cond, i32 %N) {
242 entry:
243   %tobool = icmp eq i32 %cond, 0
244   br i1 %tobool, label %if.else, label %for.preheader
246 for.preheader:
247   tail call void asm "nop", ""()
248   br label %for.body
250 for.body:                                         ; preds = %entry, %for.body
251   %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.preheader ]
252   %sum.04 = phi i32 [ %add, %for.body ], [ 0, %for.preheader ]
253   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
254   %add = add nsw i32 %call, %sum.04
255   %inc = add nuw nsw i32 %i.05, 1
256   %exitcond = icmp eq i32 %inc, 10
257   br i1 %exitcond, label %for.end, label %for.body
259 for.end:                                          ; preds = %for.body
260   tail call void asm "nop", "~{ebx}"()
261   %shl = shl i32 %add, 3
262   br label %if.end
264 if.else:                                          ; preds = %entry
265   %mul = shl nsw i32 %N, 1
266   br label %if.end
268 if.end:                                           ; preds = %if.else, %for.end
269   %sum.1 = phi i32 [ %shl, %for.end ], [ %mul, %if.else ]
270   ret i32 %sum.1
273 ; Check with a more complex case that we do not have restore within the loop and
274 ; save outside.
275 ; CHECK-LABEL: loopInfoRestoreOutsideLoop:
277 ; ENABLE: testl %edi, %edi
278 ; ENABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
280 ; Prologue code.
281 ; Make sure we save the CSR used in the inline asm: rbx.
282 ; CHECK: pushq %rbx
284 ; DISABLE: testl %edi, %edi
285 ; DISABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
287 ; CHECK: nop
288 ; CHECK: xorl [[SUM:%esi]], [[SUM]]
289 ; CHECK-NEXT: movl $10, [[IV:%e[a-z]+]]
291 ; CHECK: [[LOOP_LABEL:LBB[0-9_]+]]: ## %for.body
292 ; CHECK: movl $1, [[TMP:%e[a-z]+]]
293 ; CHECK: addl [[TMP]], [[SUM]]
294 ; CHECK-NEXT: decl [[IV]]
295 ; CHECK-NEXT: jne [[LOOP_LABEL]]
296 ; Next BB.
297 ; CHECK: shll $3, [[SUM]]
299 ; DISABLE: jmp [[EPILOG_BB:LBB[0-9_]+]]
301 ; DISABLE: [[ELSE_LABEL]]: ## %if.else
303 ; Shift second argument by one and store into returned register.
304 ; DISABLE: addl %esi, %esi
305 ; DISABLE: [[EPILOG_BB]]: ## %if.end
307 ; Epilogue code.
308 ; CHECK-DAG: popq %rbx
309 ; CHECK-DAG: movl %esi, %eax
310 ; CHECK: retq
312 ; ENABLE: [[ELSE_LABEL]]: ## %if.else
313 ; Shift second argument by one and store into returned register.
314 ; ENABLE: addl %esi, %esi
315 ; ENABLE-NEXT: movl %esi, %eax
316 ; ENABLE-NEXT: retq
317 define i32 @loopInfoRestoreOutsideLoop(i32 %cond, i32 %N) #0 {
318 entry:
319   %tobool = icmp eq i32 %cond, 0
320   br i1 %tobool, label %if.else, label %if.then
322 if.then:                                          ; preds = %entry
323   tail call void asm "nop", "~{ebx}"()
324   br label %for.body
326 for.body:                                         ; preds = %for.body, %if.then
327   %i.05 = phi i32 [ 0, %if.then ], [ %inc, %for.body ]
328   %sum.04 = phi i32 [ 0, %if.then ], [ %add, %for.body ]
329   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
330   %add = add nsw i32 %call, %sum.04
331   %inc = add nuw nsw i32 %i.05, 1
332   %exitcond = icmp eq i32 %inc, 10
333   br i1 %exitcond, label %for.end, label %for.body
335 for.end:                                          ; preds = %for.body
336   %shl = shl i32 %add, 3
337   br label %if.end
339 if.else:                                          ; preds = %entry
340   %mul = shl nsw i32 %N, 1
341   br label %if.end
343 if.end:                                           ; preds = %if.else, %for.end
344   %sum.1 = phi i32 [ %shl, %for.end ], [ %mul, %if.else ]
345   ret i32 %sum.1
348 ; Check that we handle function with no frame information correctly.
349 ; CHECK-LABEL: emptyFrame:
350 ; CHECK: ## %entry
351 ; CHECK-NEXT: xorl %eax, %eax
352 ; CHECK-NEXT: retq
353 define i32 @emptyFrame() {
354 entry:
355   ret i32 0
358 ; Check that we handle inline asm correctly.
359 ; CHECK-LABEL: inlineAsm:
361 ; ENABLE: testl %edi, %edi
362 ; ENABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
364 ; Prologue code.
365 ; Make sure we save the CSR used in the inline asm: rbx.
366 ; CHECK: pushq %rbx
368 ; DISABLE: testl %edi, %edi
369 ; DISABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
371 ; CHECK: nop
372 ; CHECK: movl $10, [[IV:%e[a-z]+]]
374 ; CHECK: [[LOOP_LABEL:LBB[0-9_]+]]: ## %for.body
375 ; Inline asm statement.
376 ; CHECK: addl $1, %ebx
377 ; CHECK: decl [[IV]]
378 ; CHECK-NEXT: jne [[LOOP_LABEL]]
379 ; Next BB.
380 ; CHECK: nop
381 ; CHECK: xorl %esi, %esi
383 ; DISABLE: jmp [[EPILOG_BB:LBB[0-9_]+]]
385 ; DISABLE: [[ELSE_LABEL]]: ## %if.else
386 ; Shift second argument by one and store into returned register.
387 ; DISABLE: addl %esi, %esi
388 ; DISABLE: [[EPILOG_BB]]: ## %if.end
390 ; Epilogue code.
391 ; CHECK-DAG: popq %rbx
392 ; CHECK-DAG: movl %esi, %eax
393 ; CHECK: retq
395 ; ENABLE: [[ELSE_LABEL]]: ## %if.else
396 ; Shift second argument by one and store into returned register.
397 ; ENABLE: addl %esi, %esi
398 ; ENABLE-NEXT: movl %esi, %eax
399 ; ENABLE-NEXT: retq
400 define i32 @inlineAsm(i32 %cond, i32 %N) {
401 entry:
402   %tobool = icmp eq i32 %cond, 0
403   br i1 %tobool, label %if.else, label %for.preheader
405 for.preheader:
406   tail call void asm "nop", ""()
407   br label %for.body
409 for.body:                                         ; preds = %entry, %for.body
410   %i.03 = phi i32 [ %inc, %for.body ], [ 0, %for.preheader ]
411   tail call void asm "addl $$1, %ebx", "~{ebx}"()
412   %inc = add nuw nsw i32 %i.03, 1
413   %exitcond = icmp eq i32 %inc, 10
414   br i1 %exitcond, label %for.exit, label %for.body
416 for.exit:
417   tail call void asm "nop", ""()
418   br label %if.end
420 if.else:                                          ; preds = %entry
421   %mul = shl nsw i32 %N, 1
422   br label %if.end
424 if.end:                                           ; preds = %for.body, %if.else
425   %sum.0 = phi i32 [ %mul, %if.else ], [ 0, %for.exit ]
426   ret i32 %sum.0
429 ; Check that we handle calls to variadic functions correctly.
430 ; CHECK-LABEL: callVariadicFunc:
432 ; ENABLE: testl %edi, %edi
433 ; ENABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
435 ; Prologue code.
436 ; CHECK: pushq
438 ; DISABLE: testl %edi, %edi
439 ; DISABLE-NEXT: je [[ELSE_LABEL:LBB[0-9_]+]]
441 ; Setup of the varags.
442 ; CHECK: movl %esi, (%rsp)
443 ; CHECK-NEXT: xorl %eax, %eax
444 ; CHECK-NEXT: %esi, %edi
445 ; CHECK-NEXT: %esi, %edx
446 ; CHECK-NEXT: %esi, %ecx
447 ; CHECK-NEXT: %esi, %r8d
448 ; CHECK-NEXT: %esi, %r9d
449 ; CHECK-NEXT: callq _someVariadicFunc
450 ; CHECK-NEXT: movl %eax, %esi
451 ; CHECK-NEXT: shll $3, %esi
453 ; ENABLE-NEXT: addq $8, %rsp
454 ; ENABLE-NEXT: movl %esi, %eax
455 ; ENABLE-NEXT: retq
457 ; DISABLE: jmp [[IFEND_LABEL:LBB[0-9_]+]]
459 ; CHECK: [[ELSE_LABEL]]: ## %if.else
460 ; Shift second argument by one and store into returned register.
461 ; CHECK: addl %esi, %esi
463 ; DISABLE: [[IFEND_LABEL]]: ## %if.end
465 ; Epilogue code.
466 ; CHECK-NEXT: movl %esi, %eax
467 ; DISABLE-NEXT: popq
468 ; CHECK-NEXT: retq
469 define i32 @callVariadicFunc(i32 %cond, i32 %N) {
470 entry:
471   %tobool = icmp eq i32 %cond, 0
472   br i1 %tobool, label %if.else, label %if.then
474 if.then:                                          ; preds = %entry
475   %call = tail call i32 (i32, ...) @someVariadicFunc(i32 %N, i32 %N, i32 %N, i32 %N, i32 %N, i32 %N, i32 %N)
476   %shl = shl i32 %call, 3
477   br label %if.end
479 if.else:                                          ; preds = %entry
480   %mul = shl nsw i32 %N, 1
481   br label %if.end
483 if.end:                                           ; preds = %if.else, %if.then
484   %sum.0 = phi i32 [ %shl, %if.then ], [ %mul, %if.else ]
485   ret i32 %sum.0
488 declare i32 @someVariadicFunc(i32, ...)
490 ; Check that we use LEA not to clobber EFLAGS.
491 %struct.temp_slot = type { %struct.temp_slot*, %struct.rtx_def*, %struct.rtx_def*, i32, i64, %union.tree_node*, %union.tree_node*, i8, i8, i32, i32, i64, i64 }
492 %union.tree_node = type { %struct.tree_decl }
493 %struct.tree_decl = type { %struct.tree_common, i8*, i32, i32, %union.tree_node*, i48, %union.anon, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %union.tree_node*, %struct.rtx_def*, %struct.rtx_def*, %union.anon.1, %union.tree_node*, %union.tree_node*, %union.tree_node*, i64, %struct.lang_decl* }
494 %struct.tree_common = type { %union.tree_node*, %union.tree_node*, i32 }
495 %union.anon = type { i64 }
496 %union.anon.1 = type { %struct.function* }
497 %struct.function = type { %struct.eh_status*, %struct.stmt_status*, %struct.expr_status*, %struct.emit_status*, %struct.varasm_status*, i8*, %union.tree_node*, %struct.function*, i32, i32, i32, i32, %struct.rtx_def*, %struct.ix86_args, %struct.rtx_def*, %struct.rtx_def*, i8*, %struct.initial_value_struct*, i32, %union.tree_node*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %union.tree_node*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, i64, %union.tree_node*, %union.tree_node*, %struct.rtx_def*, %struct.rtx_def*, i32, %struct.rtx_def**, %struct.temp_slot*, i32, i32, i32, %struct.var_refs_queue*, i32, i32, i8*, %union.tree_node*, %struct.rtx_def*, i32, i32, %struct.machine_function*, i32, i32, %struct.language_function*, %struct.rtx_def*, i24 }
498 %struct.eh_status = type opaque
499 %struct.stmt_status = type opaque
500 %struct.expr_status = type { i32, i32, i32, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def*, %struct.rtx_def* }
501 %struct.emit_status = type { i32, i32, %struct.rtx_def*, %struct.rtx_def*, %union.tree_node*, %struct.sequence_stack*, i32, i32, i8*, i32, i8*, %union.tree_node**, %struct.rtx_def** }
502 %struct.sequence_stack = type { %struct.rtx_def*, %struct.rtx_def*, %union.tree_node*, %struct.sequence_stack* }
503 %struct.varasm_status = type opaque
504 %struct.ix86_args = type { i32, i32, i32, i32, i32, i32, i32 }
505 %struct.initial_value_struct = type opaque
506 %struct.var_refs_queue = type { %struct.rtx_def*, i32, i32, %struct.var_refs_queue* }
507 %struct.machine_function = type opaque
508 %struct.language_function = type opaque
509 %struct.lang_decl = type opaque
510 %struct.rtx_def = type { i32, [1 x %union.rtunion_def] }
511 %union.rtunion_def = type { i64 }
513 declare hidden fastcc %struct.temp_slot* @find_temp_slot_from_address(%struct.rtx_def* readonly)
515 ; CHECK-LABEL: useLEA:
516 ; DISABLE: pushq 
518 ; CHECK: testq   %rdi, %rdi
519 ; CHECK-NEXT: je      [[CLEANUP:LBB[0-9_]+]]
521 ; CHECK: movzwl  (%rdi), [[BF_LOAD:%e[a-z]+]]
522 ; CHECK-NEXT: cmpl $66, [[BF_LOAD]]
523 ; CHECK-NEXT: jne [[CLEANUP]]
525 ; CHECK: movq 8(%rdi), %rdi
526 ; CHECK-NEXT: movzwl (%rdi), %e[[BF_LOAD2:[a-z]+]]
527 ; CHECK-NEXT: leal -54(%r[[BF_LOAD2]]), [[TMP:%e[a-z]+]]
528 ; CHECK-NEXT: cmpl $14, [[TMP]]
529 ; CHECK-NEXT: ja [[LOR_LHS_FALSE:LBB[0-9_]+]]
531 ; CHECK: movl $24599, [[TMP2:%e[a-z]+]]
532 ; CHECK-NEXT: btl [[TMP]], [[TMP2]]
533 ; CHECK-NEXT: jae [[LOR_LHS_FALSE:LBB[0-9_]+]]
535 ; CHECK: [[CLEANUP]]: ## %cleanup
536 ; DISABLE: popq
537 ; CHECK-NEXT: retq
539 ; CHECK: [[LOR_LHS_FALSE]]: ## %lor.lhs.false
540 ; CHECK: cmpl $134, %e[[BF_LOAD2]]
541 ; CHECK-NEXT: je [[CLEANUP]]
543 ; CHECK: cmpl $140, %e[[BF_LOAD2]]
544 ; CHECK-NEXT: je [[CLEANUP]]
546 ; ENABLE: pushq
547 ; CHECK: callq _find_temp_slot_from_address
548 ; CHECK-NEXT: testq   %rax, %rax
550 ; The adjustment must use LEA here (or be moved above the test).
551 ; ENABLE-NEXT: leaq 8(%rsp), %rsp
553 ; CHECK-NEXT: je [[CLEANUP]]
555 ; CHECK: movb $1, 57(%rax)
556 define void @useLEA(%struct.rtx_def* readonly %x) {
557 entry:
558   %cmp = icmp eq %struct.rtx_def* %x, null
559   br i1 %cmp, label %cleanup, label %if.end
561 if.end:                                           ; preds = %entry
562   %tmp = getelementptr inbounds %struct.rtx_def, %struct.rtx_def* %x, i64 0, i32 0
563   %bf.load = load i32, i32* %tmp, align 8
564   %bf.clear = and i32 %bf.load, 65535
565   %cmp1 = icmp eq i32 %bf.clear, 66
566   br i1 %cmp1, label %lor.lhs.false, label %cleanup
568 lor.lhs.false:                                    ; preds = %if.end
569   %arrayidx = getelementptr inbounds %struct.rtx_def, %struct.rtx_def* %x, i64 0, i32 1, i64 0
570   %rtx = bitcast %union.rtunion_def* %arrayidx to %struct.rtx_def**
571   %tmp1 = load %struct.rtx_def*, %struct.rtx_def** %rtx, align 8
572   %tmp2 = getelementptr inbounds %struct.rtx_def, %struct.rtx_def* %tmp1, i64 0, i32 0
573   %bf.load2 = load i32, i32* %tmp2, align 8
574   %bf.clear3 = and i32 %bf.load2, 65535
575   switch i32 %bf.clear3, label %if.end.55 [
576     i32 67, label %cleanup
577     i32 68, label %cleanup
578     i32 54, label %cleanup
579     i32 55, label %cleanup
580     i32 58, label %cleanup
581     i32 134, label %cleanup
582     i32 56, label %cleanup
583     i32 140, label %cleanup
584   ]
586 if.end.55:                                        ; preds = %lor.lhs.false
587   %call = tail call fastcc %struct.temp_slot* @find_temp_slot_from_address(%struct.rtx_def* %tmp1) #2
588   %cmp59 = icmp eq %struct.temp_slot* %call, null
589   br i1 %cmp59, label %cleanup, label %if.then.60
591 if.then.60:                                       ; preds = %if.end.55
592   %addr_taken = getelementptr inbounds %struct.temp_slot, %struct.temp_slot* %call, i64 0, i32 8
593   store i8 1, i8* %addr_taken, align 1
594   br label %cleanup
596 cleanup:                                          ; preds = %if.then.60, %if.end.55, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %lor.lhs.false, %if.end, %entry
597   ret void
600 ; Make sure we do not insert unreachable code after noreturn function.
601 ; Although this is not incorrect to insert such code, it is useless
602 ; and it hurts the binary size.
604 ; CHECK-LABEL: noreturn:
605 ; DISABLE: pushq
607 ; CHECK: testb   %dil, %dil
608 ; CHECK-NEXT: jne      [[ABORT:LBB[0-9_]+]]
610 ; CHECK: movl $42, %eax
612 ; DISABLE-NEXT: popq
614 ; CHECK-NEXT: retq
616 ; CHECK: [[ABORT]]: ## %if.abort
618 ; ENABLE: pushq
620 ; CHECK: callq _abort
621 ; ENABLE-NOT: popq
622 define i32 @noreturn(i8 signext %bad_thing) {
623 entry:
624   %tobool = icmp eq i8 %bad_thing, 0
625   br i1 %tobool, label %if.end, label %if.abort
627 if.abort:
628   tail call void @abort() #0
629   unreachable
631 if.end:
632   ret i32 42
635 declare void @abort() #0
637 attributes #0 = { noreturn nounwind }
640 ; Make sure that we handle infinite loops properly When checking that the Save
641 ; and Restore blocks are control flow equivalent, the loop searches for the
642 ; immediate (post) dominator for the (restore) save blocks. When either the Save
643 ; or Restore block is located in an infinite loop the only immediate (post)
644 ; dominator is itself. In this case, we cannot perform shrink wrapping, but we
645 ; should return gracefully and continue compilation.
646 ; The only condition for this test is the compilation finishes correctly.
648 ; CHECK-LABEL: infiniteloop
649 ; CHECK: retq
650 define void @infiniteloop() {
651 entry:
652   br i1 undef, label %if.then, label %if.end
654 if.then:
655   %ptr = alloca i32, i32 4
656   br label %for.body
658 for.body:                                         ; preds = %for.body, %entry
659   %sum.03 = phi i32 [ 0, %if.then ], [ %add, %for.body ]
660   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
661   %add = add nsw i32 %call, %sum.03
662   store i32 %add, i32* %ptr
663   br label %for.body
665 if.end:
666   ret void
669 ; Another infinite loop test this time with a body bigger than just one block.
670 ; CHECK-LABEL: infiniteloop2
671 ; CHECK: retq
672 define void @infiniteloop2() {
673 entry:
674   br i1 undef, label %if.then, label %if.end
676 if.then:
677   %ptr = alloca i32, i32 4
678   br label %for.body
680 for.body:                                         ; preds = %for.body, %entry
681   %sum.03 = phi i32 [ 0, %if.then ], [ %add, %body1 ], [ 1, %body2]
682   %call = tail call i32 asm "movl $$1, $0", "=r,~{ebx}"()
683   %add = add nsw i32 %call, %sum.03
684   store i32 %add, i32* %ptr
685   br i1 undef, label %body1, label %body2
687 body1:
688   tail call void asm sideeffect "nop", "~{ebx}"()
689   br label %for.body
691 body2:
692   tail call void asm sideeffect "nop", "~{ebx}"()
693   br label %for.body
695 if.end:
696   ret void
699 ; Another infinite loop test this time with two nested infinite loop.
700 ; CHECK-LABEL: infiniteloop3
701 ; CHECK: retq
702 define void @infiniteloop3() {
703 entry:
704   br i1 undef, label %loop2a, label %body
706 body:                                             ; preds = %entry
707   br i1 undef, label %loop2a, label %end
709 loop1:                                            ; preds = %loop2a, %loop2b
710   %var.phi = phi i32* [ %next.phi, %loop2b ], [ %var, %loop2a ]
711   %next.phi = phi i32* [ %next.load, %loop2b ], [ %next.var, %loop2a ]
712   %0 = icmp eq i32* %var, null
713   %next.load = load i32*, i32** undef
714   br i1 %0, label %loop2a, label %loop2b
716 loop2a:                                           ; preds = %loop1, %body, %entry
717   %var = phi i32* [ null, %body ], [ null, %entry ], [ %next.phi, %loop1 ]
718   %next.var = phi i32* [ undef, %body ], [ null, %entry ], [ %next.load, %loop1 ]
719   br label %loop1
721 loop2b:                                           ; preds = %loop1
722   %gep1 = bitcast i32* %var.phi to i32*
723   %next.ptr = bitcast i32* %gep1 to i32**
724   store i32* %next.phi, i32** %next.ptr
725   br label %loop1
727 end:
728   ret void
731 ; Check that we just don't bail out on RegMask.
732 ; In this case, the RegMask does not touch a CSR so we are good to go!
733 ; CHECK-LABEL: regmask:
735 ; Compare the arguments and jump to exit.
736 ; No prologue needed.
737 ; ENABLE: cmpl %esi, %edi
738 ; ENABLE-NEXT: jge [[EXIT_LABEL:LBB[0-9_]+]]
740 ; Prologue code.
741 ; (What we push does not matter. It should be some random sratch register.)
742 ; CHECK: pushq
744 ; Compare the arguments and jump to exit.
745 ; After the prologue is set.
746 ; DISABLE: cmpl %esi, %edi
747 ; DISABLE-NEXT: jge [[EXIT_LABEL:LBB[0-9_]+]]
749 ; CHECK: nop
750 ; Set the first argument to zero.
751 ; CHECK: xorl %edi, %edi
752 ; Set the second argument to addr.
753 ; CHECK-NEXT: movq %rdx, %rsi
754 ; CHECK-NEXT: callq _doSomething
755 ; CHECK-NEXT: popq
756 ; CHECK-NEXT: retq
758 ; CHECK: [[EXIT_LABEL]]:
759 ; Set the first argument to 6.
760 ; CHECK-NEXT: movl $6, %edi
761 ; Set the second argument to addr.
762 ; CHECK-NEXT: movq %rdx, %rsi
764 ; Without shrink-wrapping, we need to restore the stack before
765 ; making the tail call.
766 ; Epilogue code.
767 ; DISABLE-NEXT: popq
769 ; CHECK-NEXT: jmp _doSomething
770 define i32 @regmask(i32 %a, i32 %b, i32* %addr) {
771   %tmp2 = icmp slt i32 %a, %b
772   br i1 %tmp2, label %true, label %false
774 true:
775   ; Clobber a CSR so that we check something on the regmask
776   ; of the tail call.
777   tail call void asm sideeffect "nop", "~{ebx}"()
778   %tmp4 = call i32 @doSomething(i32 0, i32* %addr)
779   br label %end
781 false:
782   %tmp5 = tail call i32 @doSomething(i32 6, i32* %addr)
783   br label %end
785 end:
786   %tmp.0 = phi i32 [ %tmp4, %true ], [ %tmp5, %false ]
787   ret i32 %tmp.0
790 @b = internal unnamed_addr global i1 false
791 @c = internal unnamed_addr global i8 0, align 1
792 @a = common global i32 0, align 4
794 ; Make sure the prologue does not clobber the EFLAGS when
795 ; it is live accross.
796 ; PR25629.
797 ; Note: The registers may change in the following patterns, but
798 ; because they imply register hierarchy (e.g., eax, al) this is
799 ; tricky to write robust patterns.
801 ; CHECK-LABEL: useLEAForPrologue:
803 ; Prologue is at the beginning of the function when shrink-wrapping
804 ; is disabled.
805 ; DISABLE: pushq
806 ; The stack adjustment can use SUB instr because we do not need to
807 ; preserve the EFLAGS at this point.
808 ; DISABLE-NEXT: subq $16, %rsp
810 ; Load the value of b.
811 ; CHECK: movb _b(%rip), [[BOOL:%cl]]
812 ; Create the zero value for the select assignment.
813 ; CHECK-NEXT: xorl [[CMOVE_VAL:%eax]], [[CMOVE_VAL]]
814 ; CHECK-NEXT: testb [[BOOL]], [[BOOL]]
815 ; CHECK-NEXT: jne [[STOREC_LABEL:LBB[0-9_]+]]
817 ; CHECK: movb $48, [[CMOVE_VAL:%al]]
819 ; CHECK: [[STOREC_LABEL]]:
821 ; ENABLE-NEXT: pushq
822 ; For the stack adjustment, we need to preserve the EFLAGS.
823 ; ENABLE-NEXT: leaq -16(%rsp), %rsp
825 ; Technically, we should use CMOVE_VAL here or its subregister.
826 ; CHECK-NEXT: movb %al, _c(%rip)
827 ; testb set the EFLAGS read here.
828 ; CHECK-NEXT: je [[VARFUNC_CALL:LBB[0-9_]+]]
830 ; The code of the loop is not interesting.
831 ; [...]
833 ; CHECK: [[VARFUNC_CALL]]:
834 ; Set the null parameter.
835 ; CHECK-NEXT: xorl %edi, %edi
836 ; CHECK-NEXT: callq _varfunc
838 ; Set the return value.
839 ; CHECK-NEXT: xorl %eax, %eax
841 ; Epilogue code.
842 ; CHECK-NEXT: addq $16, %rsp
843 ; CHECK-NEXT: popq
844 ; CHECK-NEXT: retq
845 define i32 @useLEAForPrologue(i32 %d, i32 %a, i8 %c) #3 {
846 entry:
847   %tmp = alloca i3
848   %.b = load i1, i1* @b, align 1
849   %bool = select i1 %.b, i8 0, i8 48
850   store i8 %bool, i8* @c, align 1
851   br i1 %.b, label %for.body.lr.ph, label %for.end
853 for.body.lr.ph:                                   ; preds = %entry
854   tail call void asm sideeffect "nop", "~{ebx}"()
855   br label %for.body
857 for.body:                                         ; preds = %for.body.lr.ph, %for.body
858   %inc6 = phi i8 [ %c, %for.body.lr.ph ], [ %inc, %for.body ]
859   %cond5 = phi i32 [ %a, %for.body.lr.ph ], [ %conv3, %for.body ]
860   %cmp2 = icmp slt i32 %d, %cond5
861   %conv3 = zext i1 %cmp2 to i32
862   %inc = add i8 %inc6, 1
863   %cmp = icmp slt i8 %inc, 45
864   br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
866 for.cond.for.end_crit_edge:                       ; preds = %for.body
867   store i32 %conv3, i32* @a, align 4
868   br label %for.end
870 for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
871   %call = tail call i32 (i8*) @varfunc(i8* null)
872   ret i32 0
875 declare i32 @varfunc(i8* nocapture readonly)
877 @sum1 = external hidden thread_local global i32, align 4
880 ; Function Attrs: nounwind
881 ; Make sure the TLS call used to access @sum1 happens after the prologue
882 ; and before the epilogue.
883 ; TLS calls used to be wrongly model and shrink-wrapping would have inserted
884 ; the prologue and epilogue just around the call to doSomething.
885 ; PR25820.
887 ; CHECK-LABEL: tlsCall:
888 ; CHECK: pushq
889 ; CHECK: testb $1, %dil
890 ; CHECK: je [[ELSE_LABEL:LBB[0-9_]+]]
892 ; master bb
893 ; CHECK: movq _sum1@TLVP(%rip), %rdi
894 ; CHECK-NEXT: callq *(%rdi)
895 ; CHECK: jmp [[EXIT_LABEL:LBB[0-9_]+]]
897 ; [[ELSE_LABEL]]:
898 ; CHECK: callq _doSomething
900 ; [[EXIT_LABEL]]:
901 ; CHECK: popq
902 ; CHECK-NEXT: retq
903 define i32 @tlsCall(i1 %bool1, i32 %arg, i32* readonly dereferenceable(4) %sum1) #3 {
904 entry:
905   br i1 %bool1, label %master, label %else
907 master:
908   %tmp1 = load i32, i32* %sum1, align 4
909   store i32 %tmp1, i32* @sum1, align 4
910   br label %exit
912 else:
913   %call = call i32 @doSomething(i32 0, i32* null)
914   br label %exit
916 exit:
917   %res = phi i32 [ %arg, %master], [ %call, %else ]
918   ret i32 %res
921 attributes #3 = { nounwind }
923 @irreducibleCFGa = common global i32 0, align 4
924 @irreducibleCFGf = common global i8 0, align 1
925 @irreducibleCFGb = common global i32 0, align 4
927 ; Check that we do not run shrink-wrapping on irreducible CFGs until
928 ; it is actually supported.
929 ; At the moment, on those CFGs the loop information may be incorrect
930 ; and since we use that information to do the placement, we may end up
931 ; inserting the prologue/epilogue at incorrect places.
932 ; PR25988.
934 ; CHECK-LABEL: irreducibleCFG:
935 ; CHECK: %entry
936 ; Make sure the prologue happens in the entry block.
937 ; CHECK-NEXT: pushq
938 ; ...
939 ; Make sure the epilogue happens in the exit block.
940 ; CHECK-NOT: popq
941 ; CHECK: popq
942 ; CHECK-NEXT: popq
943 ; CHECK-NEXT: retq
944 define i32 @irreducibleCFG() #4 {
945 entry:
946   %i0 = load i32, i32* @irreducibleCFGa, align 4
947   %.pr = load i8, i8* @irreducibleCFGf, align 1
948   %bool = icmp eq i8 %.pr, 0
949   br i1 %bool, label %split, label %preheader
951 preheader:
952   br label %preheader
954 split:
955   %i1 = load i32, i32* @irreducibleCFGb, align 4
956   %tobool1.i = icmp ne i32 %i1, 0
957   br i1 %tobool1.i, label %for.body4.i, label %for.cond8.i.preheader
959 for.body4.i:
960   %call.i = tail call i32 (...) @something(i32 %i0)
961   br label %for.cond8
963 for.cond8:
964   %p1 = phi i32 [ %inc18.i, %for.inc ], [ 0, %for.body4.i ]
965   %.pr1.pr = load i32, i32* @irreducibleCFGb, align 4
966   br label %for.cond8.i.preheader
968 for.cond8.i.preheader:
969   %.pr1 = phi i32 [ %.pr1.pr, %for.cond8 ], [ %i1, %split ]
970   %p13 = phi i32 [ %p1, %for.cond8 ], [ 0, %split ]
971   br label %for.inc
973 fn1.exit:
974   ret i32 0
976 for.inc:
977   %inc18.i = add nuw nsw i32 %p13, 1
978   %cmp = icmp slt i32 %inc18.i, 7
979   br i1 %cmp, label %for.cond8, label %fn1.exit
982 attributes #4 = { "no-frame-pointer-elim"="true" }
984 @x = external global i32, align 4
985 @y = external global i32, align 4
987 ; The post-dominator tree does not include the branch containing the infinite
988 ; loop, which can occur into a misplacement of the restore block, if we're
989 ; looking for the nearest common post-dominator of an "unreachable" block.
991 ; CHECK-LABEL: infiniteLoopNoSuccessor:
992 ; CHECK: ## BB#0:
993 ; Make sure the prologue happens in the entry block.
994 ; CHECK-NEXT: pushq %rbp
995 ; ...
996 ; Make sure we don't shrink-wrap.
997 ; CHECK: ## BB#1
998 ; CHECK-NOT: pushq %rbp
999 ; ...
1000 ; Make sure the epilogue happens in the exit block.
1001 ; CHECK: ## BB#5
1002 ; CHECK: popq %rbp
1003 ; CHECK-NEXT: retq
1004 define void @infiniteLoopNoSuccessor() #5 {
1005   %1 = load i32, i32* @x, align 4
1006   %2 = icmp ne i32 %1, 0
1007   br i1 %2, label %3, label %4
1009 ; <label>:3:
1010   store i32 0, i32* @x, align 4
1011   br label %4
1013 ; <label>:4:
1014   call void (...) @somethingElse()
1015   %5 = load i32, i32* @y, align 4
1016   %6 = icmp ne i32 %5, 0
1017   br i1 %6, label %10, label %7
1019 ; <label>:7:
1020   %8 = call i32 (...) @something()
1021   br label %9
1023 ; <label>:9:
1024   call void (...) @somethingElse()
1025   br label %9
1027 ; <label>:10:
1028   ret void
1031 declare void @somethingElse(...)
1033 attributes #5 = { nounwind  "no-frame-pointer-elim-non-leaf" }