1
2
3
4
5 package ssa
6
7 import (
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/types"
10 "fmt"
11 )
12
13 type indVarFlags uint8
14
15 const (
16 indVarMinExc indVarFlags = 1 << iota
17 indVarMaxInc
18 indVarCountDown
19 )
20
21 type indVar struct {
22 ind *Value
23 nxt *Value
24 min *Value
25 max *Value
26 entry *Block
27 flags indVarFlags
28
29
30
31
32
33 }
34
35
36
37
38
39
40
41
42
43
44
45 func parseIndVar(ind *Value) (min, inc, nxt *Value, loopReturn Edge) {
46 if ind.Op != OpPhi {
47 return
48 }
49
50 if n := ind.Args[0]; (n.Op == OpAdd64 || n.Op == OpAdd32 || n.Op == OpAdd16 || n.Op == OpAdd8) && (n.Args[0] == ind || n.Args[1] == ind) {
51 min, nxt, loopReturn = ind.Args[1], n, ind.Block.Preds[0]
52 } else if n := ind.Args[1]; (n.Op == OpAdd64 || n.Op == OpAdd32 || n.Op == OpAdd16 || n.Op == OpAdd8) && (n.Args[0] == ind || n.Args[1] == ind) {
53 min, nxt, loopReturn = ind.Args[0], n, ind.Block.Preds[1]
54 } else {
55
56 return
57 }
58
59 if nxt.Args[0] == ind {
60 inc = nxt.Args[1]
61 } else if nxt.Args[1] == ind {
62 inc = nxt.Args[0]
63 } else {
64 panic("unreachable")
65 }
66
67 return
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 func findIndVar(f *Func) []indVar {
87 var iv []indVar
88 sdom := f.Sdom()
89
90 for _, b := range f.Blocks {
91 if b.Kind != BlockIf || len(b.Preds) != 2 {
92 continue
93 }
94
95 var ind *Value
96 var init *Value
97 var limit *Value
98
99
100
101 c := b.Controls[0]
102 inclusive := false
103 switch c.Op {
104 case OpLeq64, OpLeq32, OpLeq16, OpLeq8:
105 inclusive = true
106 fallthrough
107 case OpLess64, OpLess32, OpLess16, OpLess8:
108 ind, limit = c.Args[0], c.Args[1]
109 default:
110 continue
111 }
112
113
114 less := true
115 init, inc, nxt, loopReturn := parseIndVar(ind)
116 if init == nil {
117
118
119
120
121 init, inc, nxt, loopReturn = parseIndVar(limit)
122 if init == nil {
123
124 continue
125 }
126
127
128
129 ind, limit = limit, ind
130 less = false
131 }
132
133 if ind.Block != b {
134
135
136
137 continue
138 }
139
140
141 if !inc.isGenericIntConst() {
142 continue
143 }
144 step := inc.AuxInt
145 if step == 0 {
146 continue
147 }
148
149
150 var startBody Edge
151 switch {
152 case sdom.IsAncestorEq(b.Succs[0].b, loopReturn.b):
153 startBody = b.Succs[0]
154 case sdom.IsAncestorEq(b.Succs[1].b, loopReturn.b):
155
156 startBody = b.Succs[1]
157 less = !less
158 inclusive = !inclusive
159 default:
160 continue
161 }
162
163
164
165
166
167 if step > 0 && !less {
168 continue
169 }
170 if step < 0 && less {
171 continue
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 if len(startBody.b.Preds) != 1 {
191
192 continue
193 }
194
195
196
197 if !sdom.IsAncestorEq(startBody.b, nxt.Block) {
198
199 continue
200 }
201
202
203
204
205
206 ok := func() bool {
207 if step > 0 {
208 if limit.isGenericIntConst() {
209
210 v := limit.AuxInt
211 if !inclusive {
212 if v == minSignedValue(limit.Type) {
213 return false
214 }
215 v--
216 }
217 if init.isGenericIntConst() {
218
219 if init.AuxInt > v {
220 return false
221 }
222
223
224 v = addU(init.AuxInt, diff(v, init.AuxInt)/uint64(step)*uint64(step))
225 }
226 if addWillOverflow(v, step, maxSignedValue(ind.Type)) {
227 return false
228 }
229 if inclusive && v != limit.AuxInt || !inclusive && v+1 != limit.AuxInt {
230
231 limit = f.constVal(limit.Op, limit.Type, v, true)
232 inclusive = true
233 }
234 return true
235 }
236 if step == 1 && !inclusive {
237
238 return true
239 }
240
241
242 knn, k := findKNN(limit)
243 if knn == nil || k < 0 {
244 return false
245 }
246
247
248 if inclusive {
249
250 return step <= k
251 }
252
253 return step <= k+1 && k != maxSignedValue(limit.Type)
254 } else {
255 if limit.isGenericIntConst() {
256
257 v := limit.AuxInt
258 if !inclusive {
259 if v == maxSignedValue(limit.Type) {
260 return false
261 }
262 v++
263 }
264 if init.isGenericIntConst() {
265
266 if init.AuxInt < v {
267 return false
268 }
269
270
271 v = subU(init.AuxInt, diff(init.AuxInt, v)/uint64(-step)*uint64(-step))
272 }
273 if subWillUnderflow(v, -step, minSignedValue(ind.Type)) {
274 return false
275 }
276 if inclusive && v != limit.AuxInt || !inclusive && v-1 != limit.AuxInt {
277
278 limit = f.constVal(limit.Op, limit.Type, v, true)
279 inclusive = true
280 }
281 return true
282 }
283 if step == -1 && !inclusive {
284
285 return true
286 }
287 }
288 return false
289
290 }
291
292 if ok() {
293 flags := indVarFlags(0)
294 var min, max *Value
295 if step > 0 {
296 min = init
297 max = limit
298 if inclusive {
299 flags |= indVarMaxInc
300 }
301 } else {
302 min = limit
303 max = init
304 flags |= indVarMaxInc
305 if !inclusive {
306 flags |= indVarMinExc
307 }
308 flags |= indVarCountDown
309 step = -step
310 }
311 if f.pass.debug >= 1 {
312 printIndVar(b, ind, min, max, step, flags)
313 }
314
315 iv = append(iv, indVar{
316 ind: ind,
317 nxt: nxt,
318 min: min,
319 max: max,
320 entry: startBody.b,
321 flags: flags,
322 })
323 b.Logf("found induction variable %v (inc = %v, min = %v, max = %v)\n", ind, inc, min, max)
324 }
325
326
327
328
329
330 }
331
332 return iv
333 }
334
335
336
337 func subWillUnderflow(x, y int64, min int64) bool {
338 if y < 0 {
339 base.Fatalf("expecting positive value")
340 }
341 return x < min+y
342 }
343
344
345
346 func addWillOverflow(x, y int64, max int64) bool {
347 if y < 0 {
348 base.Fatalf("expecting positive value")
349 }
350 return x > max-y
351 }
352
353
354 func diff(x, y int64) uint64 {
355 if x < y {
356 base.Fatalf("diff %d - %d underflowed", x, y)
357 }
358 return uint64(x - y)
359 }
360
361
362 func addU(x int64, y uint64) int64 {
363 if y >= 1<<63 {
364 if x >= 0 {
365 base.Fatalf("addU overflowed %d + %d", x, y)
366 }
367 x += 1<<63 - 1
368 x += 1
369 y -= 1 << 63
370 }
371
372 if addWillOverflow(x, int64(y), maxSignedValue(types.Types[types.TINT64])) {
373 base.Fatalf("addU overflowed %d + %d", x, y)
374 }
375 return x + int64(y)
376 }
377
378
379 func subU(x int64, y uint64) int64 {
380 if y >= 1<<63 {
381 if x < 0 {
382 base.Fatalf("subU underflowed %d - %d", x, y)
383 }
384 x -= 1<<63 - 1
385 x -= 1
386 y -= 1 << 63
387 }
388
389 if subWillUnderflow(x, int64(y), minSignedValue(types.Types[types.TINT64])) {
390 base.Fatalf("subU underflowed %d - %d", x, y)
391 }
392 return x - int64(y)
393 }
394
395
396
397 func findKNN(v *Value) (*Value, int64) {
398 var x, y *Value
399 x = v
400 switch v.Op {
401 case OpSub64, OpSub32, OpSub16, OpSub8:
402 x = v.Args[0]
403 y = v.Args[1]
404
405 case OpAdd64, OpAdd32, OpAdd16, OpAdd8:
406 x = v.Args[0]
407 y = v.Args[1]
408 if x.isGenericIntConst() {
409 x, y = y, x
410 }
411 }
412 switch x.Op {
413 case OpSliceLen, OpStringLen, OpSliceCap:
414 default:
415 return nil, 0
416 }
417 if y == nil {
418 return x, 0
419 }
420 if !y.isGenericIntConst() {
421 return nil, 0
422 }
423 if v.Op == OpAdd64 || v.Op == OpAdd32 || v.Op == OpAdd16 || v.Op == OpAdd8 {
424 return x, -y.AuxInt
425 }
426 return x, y.AuxInt
427 }
428
429 func printIndVar(b *Block, i, min, max *Value, inc int64, flags indVarFlags) {
430 mb1, mb2 := "[", "]"
431 if flags&indVarMinExc != 0 {
432 mb1 = "("
433 }
434 if flags&indVarMaxInc == 0 {
435 mb2 = ")"
436 }
437
438 mlim1, mlim2 := fmt.Sprint(min.AuxInt), fmt.Sprint(max.AuxInt)
439 if !min.isGenericIntConst() {
440 if b.Func.pass.debug >= 2 {
441 mlim1 = fmt.Sprint(min)
442 } else {
443 mlim1 = "?"
444 }
445 }
446 if !max.isGenericIntConst() {
447 if b.Func.pass.debug >= 2 {
448 mlim2 = fmt.Sprint(max)
449 } else {
450 mlim2 = "?"
451 }
452 }
453 extra := ""
454 if b.Func.pass.debug >= 2 {
455 extra = fmt.Sprintf(" (%s)", i)
456 }
457 b.Func.Warnl(b.Pos, "Induction variable: limits %v%v,%v%v, increment %d%s", mb1, mlim1, mlim2, mb2, inc, extra)
458 }
459
460 func minSignedValue(t *types.Type) int64 {
461 return -1 << (t.Size()*8 - 1)
462 }
463
464 func maxSignedValue(t *types.Type) int64 {
465 return 1<<((t.Size()*8)-1) - 1
466 }
467
View as plain text