1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/types"
13 type indVarFlags uint8
16 indVarMinExc indVarFlags = 1 << iota // minimum value is exclusive (default: inclusive)
17 indVarMaxInc // maximum value is inclusive (default: exclusive)
18 indVarCountDown // if set the iteration starts at max and count towards min (default: min towards max)
22 ind *Value // induction variable
23 nxt *Value // the incremented variable
24 min *Value // minimum value, inclusive/exclusive depends on flags
25 max *Value // maximum value, inclusive/exclusive depends on flags
26 entry *Block // entry block in the loop.
28 // Invariant: for all blocks strictly dominated by entry:
29 // min <= ind < max [if flags == 0]
30 // min < ind < max [if flags == indVarMinExc]
31 // min <= ind <= max [if flags == indVarMaxInc]
32 // min < ind <= max [if flags == indVarMinExc|indVarMaxInc]
35 // parseIndVar checks whether the SSA value passed as argument is a valid induction
36 // variable, and, if so, extracts:
37 // - the minimum bound
38 // - the increment value
39 // - the "next" value (SSA value that is Phi'd into the induction variable every loop)
41 // Currently, we detect induction variables that match (Phi min nxt),
42 // with nxt being (Add inc ind).
43 // If it can't parse the induction variable correctly, it returns (nil, nil, nil).
44 func parseIndVar(ind *Value) (min, inc, nxt *Value) {
49 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) {
50 min, nxt = ind.Args[1], n
51 } 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) {
52 min, nxt = ind.Args[0], n
54 // Not a recognized induction variable.
58 if nxt.Args[0] == ind { // nxt = ind + inc
60 } else if nxt.Args[1] == ind { // nxt = inc + ind
63 panic("unreachable") // one of the cases must be true from the above.
69 // findIndVar finds induction variables in a function.
71 // Look for variables and blocks that satisfy the following
74 // ind = (Phi min nxt),
76 // then goto enter_loop
77 // else goto exit_loop
85 func findIndVar(f *Func) []indVar {
89 for _, b := range f.Blocks {
90 if b.Kind != BlockIf || len(b.Preds) != 2 {
94 var ind *Value // induction variable
95 var init *Value // starting value
96 var limit *Value // ending value
98 // Check that the control if it either ind </<= limit or limit </<= ind.
99 // TODO: Handle unsigned comparisons?
103 case OpLeq64, OpLeq32, OpLeq16, OpLeq8:
106 case OpLess64, OpLess32, OpLess16, OpLess8:
107 ind, limit = c.Args[0], c.Args[1]
112 // See if this is really an induction variable
114 init, inc, nxt := parseIndVar(ind)
116 // We failed to parse the induction variable. Before punting, we want to check
117 // whether the control op was written with the induction variable on the RHS
118 // instead of the LHS. This happens for the downwards case, like:
119 // for i := len(n)-1; i >= 0; i--
120 init, inc, nxt = parseIndVar(limit)
122 // No recognized induction variable on either operand
126 // Ok, the arguments were reversed. Swap them, and remember that we're
127 // looking at an ind >/>= loop (so the induction must be decrementing).
128 ind, limit = limit, ind
133 // TODO: Could be extended to include disjointed loop headers.
134 // I don't think this is causing missed optimizations in real world code often.
135 // See https://go.dev/issue/63955
139 // Expect the increment to be a nonzero constant.
140 if !inc.isGenericIntConst() {
148 // Increment sign must match comparison direction.
149 // When incrementing, the termination comparison must be ind </<= limit.
150 // When decrementing, the termination comparison must be ind >/>= limit.
152 if step > 0 && !less {
155 if step < 0 && less {
159 // Up to now we extracted the induction variable (ind),
160 // the increment delta (inc), the temporary sum (nxt),
161 // the initial value (init) and the limiting value (limit).
163 // We also know that ind has the form (Phi init nxt) where
164 // nxt is (Add inc nxt) which means: 1) inc dominates nxt
165 // and 2) there is a loop starting at inc and containing nxt.
167 // We need to prove that the induction variable is incremented
168 // only when it's smaller than the limiting value.
169 // Two conditions must happen listed below to accept ind
170 // as an induction variable.
172 // First condition: loop entry has a single predecessor, which
173 // is the header block. This implies that b.Succs[0] is
174 // reached iff ind < limit.
175 if len(b.Succs[0].b.Preds) != 1 {
176 // b.Succs[1] must exit the loop.
180 // Second condition: b.Succs[0] dominates nxt so that
181 // nxt is computed when inc < limit.
182 if !sdom.IsAncestorEq(b.Succs[0].b, nxt.Block) {
183 // inc+ind can only be reached through the branch that enters the loop.
187 // Check for overflow/underflow. We need to make sure that inc never causes
188 // the induction variable to wrap around.
189 // We use a function wrapper here for easy return true / return false / keep going logic.
190 // This function returns true if the increment will never overflow/underflow.
193 if limit.isGenericIntConst() {
194 // Figure out the actual largest value.
197 if v == minSignedValue(limit.Type) {
198 return false // < minint is never satisfiable.
202 if init.isGenericIntConst() {
203 // Use stride to compute a better lower limit.
207 v = addU(init.AuxInt, diff(v, init.AuxInt)/uint64(step)*uint64(step))
209 if addWillOverflow(v, step) {
212 if inclusive && v != limit.AuxInt || !inclusive && v+1 != limit.AuxInt {
213 // We know a better limit than the programmer did. Use our limit instead.
214 limit = f.constVal(limit.Op, limit.Type, v, true)
219 if step == 1 && !inclusive {
220 // Can't overflow because maxint is never a possible value.
223 // If the limit is not a constant, check to see if it is a
224 // negative offset from a known non-negative value.
225 knn, k := findKNN(limit)
226 if knn == nil || k < 0 {
229 // limit == (something nonnegative) - k. That subtraction can't underflow, so
232 // ind <= knn - k cannot overflow if step is at most k
235 // ind < knn - k cannot overflow if step is at most k+1
236 return step <= k+1 && k != maxSignedValue(limit.Type)
238 if limit.Op == OpConst64 {
239 // Figure out the actual smallest value.
242 if v == maxSignedValue(limit.Type) {
243 return false // > maxint is never satisfiable.
247 if init.isGenericIntConst() {
248 // Use stride to compute a better lower limit.
252 v = subU(init.AuxInt, diff(init.AuxInt, v)/uint64(-step)*uint64(-step))
254 if subWillUnderflow(v, -step) {
257 if inclusive && v != limit.AuxInt || !inclusive && v-1 != limit.AuxInt {
258 // We know a better limit than the programmer did. Use our limit instead.
259 limit = f.constVal(limit.Op, limit.Type, v, true)
264 if step == -1 && !inclusive {
265 // Can't underflow because minint is never a possible value.
274 flags := indVarFlags(0)
280 flags |= indVarMaxInc
285 flags |= indVarMaxInc
287 flags |= indVarMinExc
289 flags |= indVarCountDown
292 if f.pass.debug >= 1 {
293 printIndVar(b, ind, min, max, step, flags)
296 iv = append(iv, indVar{
304 b.Logf("found induction variable %v (inc = %v, min = %v, max = %v)\n", ind, inc, min, max)
307 // TODO: other unrolling idioms
308 // for i := 0; i < KNN - KNN % k ; i += k
309 // for i := 0; i < KNN&^(k-1) ; i += k // k a power of 2
310 // for i := 0; i < KNN&(-k) ; i += k // k a power of 2
316 // addWillOverflow reports whether x+y would result in a value more than maxint.
317 func addWillOverflow(x, y int64) bool {
321 // subWillUnderflow reports whether x-y would result in a value less than minint.
322 func subWillUnderflow(x, y int64) bool {
326 // diff returns x-y as a uint64. Requires x>=y.
327 func diff(x, y int64) uint64 {
329 base.Fatalf("diff %d - %d underflowed", x, y)
334 // addU returns x+y. Requires that x+y does not overflow an int64.
335 func addU(x int64, y uint64) int64 {
338 base.Fatalf("addU overflowed %d + %d", x, y)
344 if addWillOverflow(x, int64(y)) {
345 base.Fatalf("addU overflowed %d + %d", x, y)
350 // subU returns x-y. Requires that x-y does not underflow an int64.
351 func subU(x int64, y uint64) int64 {
354 base.Fatalf("subU underflowed %d - %d", x, y)
360 if subWillUnderflow(x, int64(y)) {
361 base.Fatalf("subU underflowed %d - %d", x, y)
366 // if v is known to be x - c, where x is known to be nonnegative and c is a
367 // constant, return x, c. Otherwise return nil, 0.
368 func findKNN(v *Value) (*Value, int64) {
372 case OpSub64, OpSub32, OpSub16, OpSub8:
376 case OpAdd64, OpAdd32, OpAdd16, OpAdd8:
379 if x.isGenericIntConst() {
384 case OpSliceLen, OpStringLen, OpSliceCap:
391 if !y.isGenericIntConst() {
394 if v.Op == OpAdd64 || v.Op == OpAdd32 || v.Op == OpAdd16 || v.Op == OpAdd8 {
400 func printIndVar(b *Block, i, min, max *Value, inc int64, flags indVarFlags) {
402 if flags&indVarMinExc != 0 {
405 if flags&indVarMaxInc == 0 {
409 mlim1, mlim2 := fmt.Sprint(min.AuxInt), fmt.Sprint(max.AuxInt)
410 if !min.isGenericIntConst() {
411 if b.Func.pass.debug >= 2 {
412 mlim1 = fmt.Sprint(min)
417 if !max.isGenericIntConst() {
418 if b.Func.pass.debug >= 2 {
419 mlim2 = fmt.Sprint(max)
425 if b.Func.pass.debug >= 2 {
426 extra = fmt.Sprintf(" (%s)", i)
428 b.Func.Warnl(b.Pos, "Induction variable: limits %v%v,%v%v, increment %d%s", mb1, mlim1, mlim2, mb2, inc, extra)
431 func minSignedValue(t *types.Type) int64 {
432 return -1 << (t.Size()*8 - 1)
435 func maxSignedValue(t *types.Type) int64 {
436 return 1<<((t.Size()*8)-1) - 1