aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Analysis/LoopCacheAnalysis/compute-cost.ll
blob: 205cd851fce0defbb22e6887812f5b3e4ddd0cfd (plain)
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
; RUN: opt < %s  -cache-line-size=32 -passes='print<loop-cache-cost>' -disable-output 2>&1 | FileCheck -check-prefix=SMALLER-CACHELINE %s
; RUN: opt < %s  -cache-line-size=256 -passes='print<loop-cache-cost>' -disable-output 2>&1 | FileCheck -check-prefix=LARGER-CACHELINE %s

;; This test is similar to test/Analysis/LoopCacheAnalysis/PowerPC/compute-cost.ll,
;; with differences that it tests the scenarios where an option for cache line size is
;; specified with different values.

; Check IndexedReference::computeRefCost can handle type differences between
; Stride and TripCount

; Round costs up to the nearest whole number i.e. in 'for.cond5' cost is calculated 12.5 and
; it makes more sense to say 13 cache lines are used rather than 12 cache lines.

; SMALLER-CACHELINE: Loop 'for.cond' has cost = 256
; LARGER-CACHELINE: Loop 'for.cond' has cost = 32
%struct._Handleitem = type { ptr }

define void @handle_to_ptr(ptr %blocks) {
; Preheader:
entry:
  br label %for.cond

; Loop:
for.cond:                                         ; preds = %for.body, %entry
  %i.0 = phi i32 [ 1, %entry ], [ %inc, %for.body ]
  %cmp = icmp ult i32 %i.0, 1024
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  %idxprom = zext i32 %i.0 to i64
  %arrayidx = getelementptr inbounds ptr, ptr %blocks, i64 %idxprom
  store ptr null, ptr %arrayidx, align 8
  %inc = add nuw nsw i32 %i.0, 1
  br label %for.cond

; Exit blocks
for.end:                                          ; preds = %for.cond
  ret void
}

; Check IndexedReference::computeRefCost can handle type differences between
; Coeff and ElemSize.

; SMALLER-CACHELINE: Loop 'for.cond' has cost = 100000000
; SMALLER-CACHELINE: Loop 'for.cond1' has cost = 1000000
; SMALLER-CACHELINE: Loop 'for.cond5' has cost = 130000
; LARGER-CACHELINE: Loop 'for.cond' has cost = 100000000
; LARGER-CACHELINE: Loop 'for.cond1' has cost = 1000000
; LARGER-CACHELINE: Loop 'for.cond5' has cost = 20000
@data = external dso_local global [2 x [4 x [18 x i32]]], align 1

define dso_local void @handle_to_ptr_2(i1 %b0, i1 %b1, i1 %b2) {
entry:
  br label %for.cond

for.cond:
  %i.0 = phi i16 [ 0, %entry ], [ %inc18, %for.inc17 ]
  %idxprom = zext i16 %i.0 to i32
  br i1 %b2, label %for.end19, label %for.cond1

for.cond1:
  %j.0 = phi i16 [ %inc15, %for.inc14 ], [ 0, %for.cond ]
  br i1 %b1, label %for.inc17, label %for.cond5.preheader

for.cond5.preheader:
  %idxprom10 = zext i16 %j.0 to i32
  br label %for.cond5

for.cond5:
  %k.0 = phi i16 [ %inc, %for.inc ], [ 0, %for.cond5.preheader ]
  br i1 %b0, label %for.inc14, label %for.inc

for.inc:
  %idxprom12 = zext i16 %k.0 to i32
  %arrayidx13 = getelementptr inbounds [2 x [4 x [18 x i32]]], ptr @data, i32 0, i32 %idxprom, i32 %idxprom10, i32 %idxprom12
  store i32 7, ptr %arrayidx13, align 1
  %inc = add nuw nsw i16 %k.0, 1
  br label %for.cond5

for.inc14:
  %inc15 = add nuw nsw i16 %j.0, 1
  br label %for.cond1

for.inc17:
  %inc18 = add nuw nsw i16 %i.0, 1
  br label %for.cond

for.end19:
  ret void
}

; Check IndexedReference::computeRefCost can handle negative stride

; SMALLER-CACHELINE: Loop 'for.neg.cond' has cost = 256
; LARGER-CACHELINE: Loop 'for.neg.cond' has cost = 32
define void @handle_to_ptr_neg_stride(ptr %blocks) {
; Preheader:
entry:
  br label %for.neg.cond

; Loop:
for.neg.cond:                                         ; preds = %for.neg.body, %entry
  %i.0 = phi i32 [ 1023, %entry ], [ %dec, %for.neg.body ]
  %cmp = icmp sgt i32 %i.0, 0
  br i1 %cmp, label %for.neg.body, label %for.neg.end

for.neg.body:                                         ; preds = %for.neg.cond
  %idxprom = zext i32 %i.0 to i64
  %arrayidx = getelementptr inbounds ptr, ptr %blocks, i64 %idxprom
  store ptr null, ptr %arrayidx, align 8
  %dec = add nsw i32 %i.0, -1
  br label %for.neg.cond

; Exit blocks
for.neg.end:                                          ; preds = %for.neg.cond
  ret void
}



;   for (int i = 40960; i > 0; i--)
;     B[i] = B[40960 - i];

; FIXME: Currently negative access functions are treated the same as positive
; access functions. When this is fixed this testcase should have a cost
; approximately 2x higher.

; SMALLER-CACHELINE: Loop 'for.cond2' has cost = 10241
; LARGER-CACHELINE: Loop 'for.cond2' has cost = 1281
define void @Test2(ptr %B) {
entry:
  br label %for.cond2

for.cond2:                                         ; preds = %for.body, %entry
  %i.0 = phi i32 [ 40960, %entry ], [ %dec, %for.body ]
  %cmp = icmp sgt i32 %i.0, 0
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  %sub = sub nsw i32 40960, %i.0
  %idxprom = sext i32 %sub to i64
  %arrayidx = getelementptr inbounds double, ptr %B, i64 %idxprom
  %0 = load double, ptr %arrayidx, align 8
  %idxprom1 = sext i32 %i.0 to i64
  %arrayidx2 = getelementptr inbounds double, ptr %B, i64 %idxprom1
  store double %0, ptr %arrayidx2, align 8
  %dec = add nsw i32 %i.0, -1
  br label %for.cond2

for.end:                                          ; preds = %for.cond
  ret void
}



;   for (i = 40960; i > 0; i--)
;     C[i] = C[i];

; SMALLER-CACHELINE: Loop 'for.cond3' has cost = 10241
; LARGER-CACHELINE: Loop 'for.cond3' has cost = 1281
define void @Test3(ptr %C) {
entry:
  br label %for.cond3

for.cond3:                                         ; preds = %for.body, %entry
  %i.0 = phi i32 [ 40960, %entry ], [ %dec, %for.body ]
  %cmp = icmp sgt i32 %i.0, 0
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  %idxprom = sext i32 %i.0 to i64
  %arrayidx = getelementptr inbounds ptr, ptr %C, i64 %idxprom
  %0 = load ptr, ptr %arrayidx, align 8
  %idxprom1 = sext i32 %i.0 to i64
  %arrayidx2 = getelementptr inbounds ptr, ptr %C, i64 %idxprom1
  store ptr %0, ptr %arrayidx2, align 8
  %dec = add nsw i32 %i.0, -1
  br label %for.cond3

for.end:                                          ; preds = %for.cond
  ret void
}



;  for (i = 0; i < 40960; i++)
;     D[i] = D[i];

; SMALLER-CACHELINE: Loop 'for.cond4' has cost = 10241
; LARGER-CACHELINE: Loop 'for.cond4' has cost = 1281
define void @Test4(ptr %D) {
entry:
  br label %for.cond4

for.cond4:                                         ; preds = %for.body, %entry
  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  %cmp = icmp slt i32 %i.0, 40960
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  %idxprom = sext i32 %i.0 to i64
  %arrayidx = getelementptr inbounds ptr, ptr %D, i64 %idxprom
  %0 = load ptr, ptr %arrayidx, align 8
  %idxprom1 = sext i32 %i.0 to i64
  %arrayidx2 = getelementptr inbounds ptr, ptr %D, i64 %idxprom1
  store ptr %0, ptr %arrayidx2, align 8
  %inc = add nsw i32 %i.0, 1
  br label %for.cond4

for.end:                                          ; preds = %for.cond
  ret void
}