1 ; RUN: llc -mcpu=corei7 -mtriple=x86_64-linux < %s | FileCheck %s
3 ; The block latch should be moved before header.
8 define i32 @test1(ptr %p) {
13 %x1 = phi i64 [0, %entry], [%x2, %latch]
14 %count1 = phi i32 [0, %entry], [%count4, %latch]
15 %0 = ptrtoint ptr %p to i64
17 %2 = inttoptr i64 %1 to ptr
18 %data = load i32, ptr %2
19 %3 = icmp eq i32 %data, 0
20 br i1 %3, label %latch, label %false
23 %count2 = add i32 %count1, 1
27 %count4 = phi i32 [%count2, %false], [%count1, %header]
29 %4 = icmp eq i64 %x2, 100
30 br i1 %4, label %exit, label %header
36 ; The block latch and one of false/true should be moved before header.
42 define i32 @test2(ptr %p) {
47 %x1 = phi i64 [0, %entry], [%x2, %latch]
48 %count1 = phi i32 [0, %entry], [%count4, %latch]
49 %0 = ptrtoint ptr %p to i64
51 %2 = inttoptr i64 %1 to ptr
52 %data = load i32, ptr %2
53 %3 = icmp eq i32 %data, 0
54 br i1 %3, label %true, label %false
57 %count2 = add i32 %count1, 1
61 %count3 = add i32 %count1, 2
65 %count4 = phi i32 [%count2, %false], [%count3, %true]
67 %4 = icmp eq i64 %x2, 100
68 br i1 %4, label %exit, label %header
74 ; More blocks can be moved before header.
75 ; header <------------
102 ; Blocks true3,endif3,latch should be moved before header.
110 define i32 @test3(ptr %p) {
115 %x1 = phi i64 [0, %entry], [%x2, %latch]
116 %count1 = phi i32 [0, %entry], [%count12, %latch]
117 %0 = ptrtoint ptr %p to i64
119 %2 = inttoptr i64 %1 to ptr
120 %data = load i32, ptr %2
121 %3 = icmp eq i32 %data, 0
122 br i1 %3, label %true, label %false, !prof !3
125 %count2 = add i32 %count1, 1
126 %cond = icmp sgt i32 %count2, 10
127 br i1 %cond, label %true2, label %false2
130 %count3 = and i32 %count2, 7
134 %count4 = mul i32 %count2, 3
138 %count5 = phi i32 [%count3, %false2], [%count4, %true2]
139 %count6 = sub i32 %count5, 5
143 %count7 = add i32 %count1, 2
144 %cond2 = icmp slt i32 %count7, 20
145 br i1 %cond2, label %true3, label %false3
148 %count8 = or i32 %count7, 3
152 %count9 = xor i32 %count7, 55
156 %count10 = phi i32 [%count8, %false3], [%count9, %true3]
157 %count11 = add i32 %count10, 3
161 %count12 = phi i32 [%count6, %endif2], [%count11, %endif3]
163 %4 = icmp eq i64 %x2, 100
164 br i1 %4, label %exit, label %header
170 ; The exit block has higher frequency than false block, so latch block
171 ; should not moved before header.
178 define i32 @test4(i32 %t, ptr %p) {
183 %x1 = phi i64 [0, %entry], [%x2, %latch]
184 %count1 = phi i32 [0, %entry], [%count4, %latch]
185 %0 = ptrtoint ptr %p to i64
187 %2 = inttoptr i64 %1 to ptr
188 %data = load i32, ptr %2
189 %3 = icmp eq i32 %data, 0
190 br i1 %3, label %true, label %false, !prof !1
193 %count2 = add i32 %count1, 1
197 %count3 = add i32 %count1, 2
201 %count4 = phi i32 [%count2, %false], [%count3, %true]
203 %4 = icmp eq i64 %x2, 100
204 br i1 %4, label %exit, label %header, !prof !2
210 !1 = !{!"branch_weights", i32 100, i32 1}
211 !2 = !{!"branch_weights", i32 16, i32 16}
212 !3 = !{!"branch_weights", i32 51, i32 49}
214 ; If move latch to loop top doesn't reduce taken branch, don't do it.
219 define void @test5(ptr %p) {
224 %x1 = phi i64 [0, %entry], [%x1, %header], [%x2, %latch]
225 %0 = ptrtoint ptr %p to i64
227 %2 = inttoptr i64 %1 to ptr
228 %data = load i32, ptr %2
229 %3 = icmp eq i32 %data, 0
230 br i1 %3, label %latch, label %header