move_latch_to_loop_top.ll
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
; RUN: llc -mcpu=corei7 -mtriple=x86_64-linux < %s | FileCheck %s
; The block latch should be moved before header.
;CHECK-LABEL: test1:
;CHECK: %latch
;CHECK: %header
;CHECK: %false
define i32 @test1(i32* %p) {
entry:
br label %header
header:
%x1 = phi i64 [0, %entry], [%x2, %latch]
%count1 = phi i32 [0, %entry], [%count4, %latch]
%0 = ptrtoint i32* %p to i64
%1 = add i64 %0, %x1
%2 = inttoptr i64 %1 to i32*
%data = load i32, i32* %2
%3 = icmp eq i32 %data, 0
br i1 %3, label %latch, label %false
false:
%count2 = add i32 %count1, 1
br label %latch
latch:
%count4 = phi i32 [%count2, %false], [%count1, %header]
%x2 = add i64 %x1, 1
%4 = icmp eq i64 %x2, 100
br i1 %4, label %exit, label %header
exit:
ret i32 %count4
}
; The block latch and one of false/true should be moved before header.
;CHECK-LABEL: test2:
;CHECK: %true
;CHECK: %latch
;CHECK: %header
;CHECK: %false
define i32 @test2(i32* %p) {
entry:
br label %header
header:
%x1 = phi i64 [0, %entry], [%x2, %latch]
%count1 = phi i32 [0, %entry], [%count4, %latch]
%0 = ptrtoint i32* %p to i64
%1 = add i64 %0, %x1
%2 = inttoptr i64 %1 to i32*
%data = load i32, i32* %2
%3 = icmp eq i32 %data, 0
br i1 %3, label %true, label %false
false:
%count2 = add i32 %count1, 1
br label %latch
true:
%count3 = add i32 %count1, 2
br label %latch
latch:
%count4 = phi i32 [%count2, %false], [%count3, %true]
%x2 = add i64 %x1, 1
%4 = icmp eq i64 %x2, 100
br i1 %4, label %exit, label %header
exit:
ret i32 %count4
}
; More blocks can be moved before header.
; header <------------
; /\ |
; / \ |
; / \ |
; / \ |
; / \ |
; true false |
; /\ /\ |
; / \ / \ |
; / \ / \ |
; true3 false3 / \ |
; \ / true2 false2 |
; \ / \ / |
; \/ \ / |
; endif3 \ / |
; \ \/ |
; \ endif2 |
; \ / |
; \ / |
; \ / |
; \ / |
; \/ |
; latch-------------
; |
; |
; exit
;
; Blocks true3,endif3,latch should be moved before header.
;
;CHECK-LABEL: test3:
;CHECK: %true3
;CHECK: %endif3
;CHECK: %latch
;CHECK: %header
;CHECK: %false
define i32 @test3(i32* %p) {
entry:
br label %header
header:
%x1 = phi i64 [0, %entry], [%x2, %latch]
%count1 = phi i32 [0, %entry], [%count12, %latch]
%0 = ptrtoint i32* %p to i64
%1 = add i64 %0, %x1
%2 = inttoptr i64 %1 to i32*
%data = load i32, i32* %2
%3 = icmp eq i32 %data, 0
br i1 %3, label %true, label %false, !prof !3
false:
%count2 = add i32 %count1, 1
%cond = icmp sgt i32 %count2, 10
br i1 %cond, label %true2, label %false2
false2:
%count3 = and i32 %count2, 7
br label %endif2
true2:
%count4 = mul i32 %count2, 3
br label %endif2
endif2:
%count5 = phi i32 [%count3, %false2], [%count4, %true2]
%count6 = sub i32 %count5, 5
br label %latch
true:
%count7 = add i32 %count1, 2
%cond2 = icmp slt i32 %count7, 20
br i1 %cond2, label %true3, label %false3
false3:
%count8 = or i32 %count7, 3
br label %endif3
true3:
%count9 = xor i32 %count7, 55
br label %endif3
endif3:
%count10 = phi i32 [%count8, %false3], [%count9, %true3]
%count11 = add i32 %count10, 3
br label %latch
latch:
%count12 = phi i32 [%count6, %endif2], [%count11, %endif3]
%x2 = add i64 %x1, 1
%4 = icmp eq i64 %x2, 100
br i1 %4, label %exit, label %header
exit:
ret i32 %count12
}
; The exit block has higher frequency than false block, so latch block
; should not moved before header.
;CHECK-LABEL: test4:
;CHECK: %header
;CHECK: %true
;CHECK: %latch
;CHECK: %false
;CHECK: %exit
define i32 @test4(i32 %t, i32* %p) {
entry:
br label %header
header:
%x1 = phi i64 [0, %entry], [%x2, %latch]
%count1 = phi i32 [0, %entry], [%count4, %latch]
%0 = ptrtoint i32* %p to i64
%1 = add i64 %0, %x1
%2 = inttoptr i64 %1 to i32*
%data = load i32, i32* %2
%3 = icmp eq i32 %data, 0
br i1 %3, label %true, label %false, !prof !1
false:
%count2 = add i32 %count1, 1
br label %latch
true:
%count3 = add i32 %count1, 2
br label %latch
latch:
%count4 = phi i32 [%count2, %false], [%count3, %true]
%x2 = add i64 %x1, 1
%4 = icmp eq i64 %x2, 100
br i1 %4, label %exit, label %header, !prof !2
exit:
ret i32 %count4
}
!1 = !{!"branch_weights", i32 100, i32 1}
!2 = !{!"branch_weights", i32 16, i32 16}
!3 = !{!"branch_weights", i32 51, i32 49}
; If move latch to loop top doesn't reduce taken branch, don't do it.
;CHECK-LABEL: test5:
;CHECK: %entry
;CHECK: %header
;CHECK: %latch
define void @test5(i32* %p) {
entry:
br label %header
header:
%x1 = phi i64 [0, %entry], [%x1, %header], [%x2, %latch]
%0 = ptrtoint i32* %p to i64
%1 = add i64 %0, %x1
%2 = inttoptr i64 %1 to i32*
%data = load i32, i32* %2
%3 = icmp eq i32 %data, 0
br i1 %3, label %latch, label %header
latch:
%x2 = add i64 %x1, 1
br label %header
exit:
ret void
}