1 ; RUN: opt < %s -passes=sink -S | FileCheck %s
2 ; RUN: opt < %s -aa-pipeline='basic-aa' -passes='sink' -S | FileCheck %s
4 @A = external global i32
5 @B = external global i32
7 ; Sink should sink the load past the store (which doesn't overlap) into
8 ; the block that uses it.
12 ; CHECK-NEXT: %l = load i32, ptr @A
13 ; CHECK-NEXT: ret i32 %l
15 define i32 @foo(i1 %z) {
18 br i1 %z, label %true, label %false
25 ; But don't sink load volatiles...
28 ; CHECK: load volatile
29 ; CHECK-NEXT: store i32
31 define i32 @foo2(i1 %z) {
32 %l = load volatile i32, ptr @A
34 br i1 %z, label %true, label %false
41 ; Sink to the nearest post-dominator
43 ; CHECK-LABEL: @diamond(
49 define i32 @diamond(i32 %a, i32 %b, i32 %c) {
50 %1 = mul nsw i32 %c, %b
51 %2 = icmp sgt i32 %a, 0
52 br i1 %2, label %B0, label %B1
61 %.01 = phi i32 [ %c, %B0 ], [ %a, %B1 ]
66 ; We shouldn't sink constant sized allocas from the entry block, since CodeGen
67 ; interprets allocas outside the entry block as dynamically sized stack objects.
69 ; CHECK-LABEL: @alloca_nosink
72 define i32 @alloca_nosink(i32 %a, i32 %b) {
75 %1 = icmp ne i32 %a, 0
76 br i1 %1, label %if, label %endif
79 %2 = getelementptr i32, ptr %0, i32 1
82 %3 = getelementptr i32, ptr %0, i32 %b
90 ; Make sure we sink dynamic sized allocas
92 ; CHECK-LABEL: @alloca_sink_dynamic
97 define i32 @alloca_sink_dynamic(i32 %a, i32 %b, i32 %size) {
99 %0 = alloca i32, i32 %size
100 %1 = icmp ne i32 %a, 0
101 br i1 %1, label %if, label %endif
104 %2 = getelementptr i32, ptr %0, i32 1
107 %3 = getelementptr i32, ptr %0, i32 %b
108 %4 = load i32, ptr %3
115 ; We also want to sink allocas that are not in the entry block. These
116 ; will already be considered as dynamically sized stack objects, so sinking
117 ; them does no further damage.
119 ; CHECK-LABEL: @alloca_sink_nonentry
124 define i32 @alloca_sink_nonentry(i32 %a, i32 %b, i32 %c) {
126 %cmp = icmp ne i32 %c, 0
127 br i1 %cmp, label %endif, label %if0
131 %1 = icmp ne i32 %a, 0
132 br i1 %1, label %if, label %endif
135 %2 = getelementptr i32, ptr %0, i32 1
138 %3 = getelementptr i32, ptr %0, i32 %b
139 %4 = load i32, ptr %3