Source file src/cmd/compile/internal/ssacompile/loopbce.go

     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.
     4  
     5  package ssacompile
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"cmd/compile/internal/base"
    11  	"cmd/compile/internal/ssa"
    12  	"cmd/compile/internal/ssa/block"
    13  	"cmd/compile/internal/ssa/ssaop"
    14  	"cmd/compile/internal/types"
    15  )
    16  
    17  type indVarFlags uint8
    18  
    19  const (
    20  	indVarMinExc indVarFlags = 1 << iota // minimum value is exclusive (default: inclusive)
    21  	indVarMaxInc                         // maximum value is inclusive (default: exclusive)
    22  )
    23  
    24  type indVar struct {
    25  	ind   *ssa.Value // induction variable
    26  	nxt   *ssa.Value // the incremented variable
    27  	min   *ssa.Value // minimum value, inclusive/exclusive depends on flags
    28  	max   *ssa.Value // maximum value, inclusive/exclusive depends on flags
    29  	entry *ssa.Block // the block where the edge from the succeeded comparison of the induction variable goes to, means when the bound check has passed.
    30  	step  int64      // it will always be positive.
    31  	flags indVarFlags
    32  	// Invariant: for all blocks dominated by entry:
    33  	//	min <= ind <  max    [if flags == 0]
    34  	//	min <  ind <  max    [if flags == indVarMinExc]
    35  	//	min <= ind <= max    [if flags == indVarMaxInc]
    36  	//	min <  ind <= max    [if flags == indVarMinExc|indVarMaxInc]
    37  }
    38  
    39  // parseIndVar checks whether the SSA value passed as argument is a valid induction
    40  // variable, and, if so, extracts:
    41  //   - the minimum bound
    42  //   - the increment value
    43  //   - the "next" value (SSA value that is Phi'd into the induction variable every loop)
    44  //   - the header's edge returning from the body
    45  //
    46  // Currently, we detect induction variables that match (Phi min nxt),
    47  // with nxt being (Add inc ind).
    48  // If it can't parse the induction variable correctly, it returns (nil, nil, nil).
    49  func parseIndVar(ind *ssa.Value) (min, inc, nxt *ssa.Value, loopReturn ssa.Edge) {
    50  	if ind.Op != ssaop.OpPhi {
    51  		return
    52  	}
    53  
    54  	if n := ind.Args[0]; (n.Op == ssaop.OpAdd64 || n.Op == ssaop.OpAdd32 || n.Op == ssaop.OpAdd16 || n.Op == ssaop.OpAdd8) && (n.Args[0] == ind || n.Args[1] == ind) {
    55  		min, nxt, loopReturn = ind.Args[1], n, ind.Block.Preds[0]
    56  	} else if n := ind.Args[1]; (n.Op == ssaop.OpAdd64 || n.Op == ssaop.OpAdd32 || n.Op == ssaop.OpAdd16 || n.Op == ssaop.OpAdd8) && (n.Args[0] == ind || n.Args[1] == ind) {
    57  		min, nxt, loopReturn = ind.Args[0], n, ind.Block.Preds[1]
    58  	} else {
    59  		// Not a recognized induction variable.
    60  		return
    61  	}
    62  
    63  	if nxt.Args[0] == ind { // nxt = ind + inc
    64  		inc = nxt.Args[1]
    65  	} else if nxt.Args[1] == ind { // nxt = inc + ind
    66  		inc = nxt.Args[0]
    67  	} else {
    68  		panic("unreachable") // one of the cases must be true from the above.
    69  	}
    70  
    71  	return
    72  }
    73  
    74  // findIndVar finds induction variables in a function.
    75  //
    76  // Look for variables and blocks that satisfy the following
    77  //
    78  //	 loop:
    79  //	   ind = (Phi min nxt),
    80  //	   if ind < max
    81  //	     then goto enter_loop
    82  //	     else goto exit_loop
    83  //
    84  //	   enter_loop:
    85  //		do something
    86  //	      nxt = inc + ind
    87  //		goto loop
    88  //
    89  //	 exit_loop:
    90  //
    91  // We may have more than one induction variables, the loop in the go
    92  // source code may looks like this:
    93  //
    94  //	for i >= 0 && j >= 0 {
    95  //		// use i and j
    96  //		i--
    97  //		j--
    98  //	}
    99  //
   100  // So, also look for variables and blocks that satisfy the following
   101  //
   102  //	loop:
   103  //	  i = (Phi maxi nxti)
   104  //	  j = (Phi maxj nxtj)
   105  //	  if i >= mini
   106  //	    then goto check_j
   107  //	    else goto exit_loop
   108  //
   109  //	check_j:
   110  //	  if j >= minj
   111  //	    then goto enter_loop
   112  //	    else goto exit_loop
   113  //
   114  //	enter_loop:
   115  //	  do something
   116  //	  nxti = i - di
   117  //	  nxtj = j - dj
   118  //	  goto loop
   119  //
   120  //	exit_loop:
   121  func findIndVar(f *ssa.Func) []indVar {
   122  	var iv []indVar
   123  	sdom := f.Sdom()
   124  
   125  nextblock:
   126  	for _, b := range f.Blocks {
   127  		if b.Kind != block.BlockIf {
   128  			continue
   129  		}
   130  		c := b.Controls[0]
   131  		for idx := range 2 {
   132  			// Check that the control if it either ind </<= limit or limit </<= ind.
   133  			// TODO: Handle unsigned comparisons?
   134  			inclusive := false
   135  			switch c.Op {
   136  			case ssaop.OpLeq64, ssaop.OpLeq32, ssaop.OpLeq16, ssaop.OpLeq8:
   137  				inclusive = true
   138  			case ssaop.OpLess64, ssaop.OpLess32, ssaop.OpLess16, ssaop.OpLess8:
   139  			default:
   140  				continue nextblock
   141  			}
   142  
   143  			less := idx == 0
   144  			// induction variable, ending value
   145  			ind, limit := c.Args[idx], c.Args[1-idx]
   146  			// starting value, increment value, next value, loop return edge
   147  			init, inc, nxt, loopReturn := parseIndVar(ind)
   148  			if init == nil {
   149  				continue // this is not an induction variable
   150  			}
   151  
   152  			// This is ind.Block.Preds, not b.Preds. That's a restriction on the loop header,
   153  			// not the comparison block.
   154  			if len(ind.Block.Preds) != 2 {
   155  				continue
   156  			}
   157  
   158  			// Expect the increment to be a nonzero constant.
   159  			if !inc.IsGenericIntConst() {
   160  				continue
   161  			}
   162  			step := inc.AuxInt
   163  			if step == 0 {
   164  				continue
   165  			}
   166  			// step == minInt64 cannot be safely negated below, because -step
   167  			// overflows back to minInt64. The later underflow checks need a
   168  			// positive magnitude, so reject this case here.
   169  			if step == minSignedValue(ind.Type) {
   170  				continue
   171  			}
   172  
   173  			// startBody is the edge that eventually returns to the loop header.
   174  			var startBody ssa.Edge
   175  			switch {
   176  			case sdom.IsAncestorEq(b.Succs[0].B, loopReturn.B):
   177  				startBody = b.Succs[0]
   178  			case sdom.IsAncestorEq(b.Succs[1].B, loopReturn.B):
   179  				// if x { goto exit } else { goto entry } is identical to if !x { goto entry } else { goto exit }
   180  				startBody = b.Succs[1]
   181  				less = !less
   182  				inclusive = !inclusive
   183  			default:
   184  				continue
   185  			}
   186  
   187  			// Increment sign must match comparison direction.
   188  			// When incrementing, the termination comparison must be ind </<= limit.
   189  			// When decrementing, the termination comparison must be ind >/>= limit.
   190  			// See issue 26116.
   191  			if step > 0 && !less {
   192  				continue
   193  			}
   194  			if step < 0 && less {
   195  				continue
   196  			}
   197  
   198  			// Up to now we extracted the induction variable (ind),
   199  			// the increment delta (inc), the temporary sum (nxt),
   200  			// the initial value (init) and the limiting value (limit).
   201  			//
   202  			// We also know that ind has the form (Phi init nxt) where
   203  			// nxt is (Add inc nxt) which means: 1) inc dominates nxt
   204  			// and 2) there is a loop starting at inc and containing nxt.
   205  			//
   206  			// We need to prove that the induction variable is incremented
   207  			// only when it's smaller than the limiting value.
   208  			// Two conditions must happen listed below to accept ind
   209  			// as an induction variable.
   210  
   211  			// First condition: the entry block has a single predecessor.
   212  			// The entry now means the in-loop edge where the induction variable
   213  			// comparison succeeded. Its predecessor is not necessarily the header
   214  			// block. This implies that b.Succs[0] is reached iff ind < limit.
   215  			if len(startBody.B.Preds) != 1 {
   216  				// the other successor must exit the loop.
   217  				continue
   218  			}
   219  
   220  			// Second condition: startBody.b dominates nxt so that
   221  			// nxt is computed when inc < limit.
   222  			if !sdom.IsAncestorEq(startBody.B, nxt.Block) {
   223  				// inc+ind can only be reached through the branch that confirmed the
   224  				// induction variable is in bounds.
   225  				continue
   226  			}
   227  
   228  			// Check for overflow/underflow. We need to make sure that inc never causes
   229  			// the induction variable to wrap around.
   230  			// We use a function wrapper here for easy return true / return false / keep going logic.
   231  			// This function returns true if the increment will never overflow/underflow.
   232  			ok := func() bool {
   233  				if step > 0 {
   234  					if limit.IsGenericIntConst() {
   235  						// Figure out the actual largest value.
   236  						v := limit.AuxInt
   237  						if !inclusive {
   238  							if v == minSignedValue(limit.Type) {
   239  								return false // < minint is never satisfiable.
   240  							}
   241  							v--
   242  						}
   243  						if init.IsGenericIntConst() {
   244  							// Use stride to compute a better lower limit.
   245  							if init.AuxInt > v {
   246  								return false
   247  							}
   248  							// TODO(1.27): investigate passing a smaller-magnitude overflow limit to addU
   249  							// for addWillOverflow.
   250  							v = addU(init.AuxInt, diff(v, init.AuxInt)/uint64(step)*uint64(step))
   251  						}
   252  						if addWillOverflow(v, step, maxSignedValue(ind.Type)) {
   253  							return false
   254  						}
   255  						if inclusive && v != limit.AuxInt || !inclusive && v+1 != limit.AuxInt {
   256  							// We know a better limit than the programmer did. Use our limit instead.
   257  							limit = f.ConstVal(limit.Op, limit.Type, v, true)
   258  							inclusive = true
   259  						}
   260  						return true
   261  					}
   262  					if step == 1 && !inclusive {
   263  						// Can't overflow because maxint is never a possible value.
   264  						return true
   265  					}
   266  					// If the limit is not a constant, check to see if it is a
   267  					// negative offset from a known non-negative value.
   268  					knn, k := findKNN(limit)
   269  					if knn == nil || k < 0 {
   270  						return false
   271  					}
   272  					// limit == (something nonnegative) - k. That subtraction can't underflow, so
   273  					// we can trust it.
   274  					if inclusive {
   275  						// ind <= knn - k cannot overflow if step is at most k
   276  						return step <= k
   277  					}
   278  					// ind < knn - k cannot overflow if step is at most k+1
   279  					return step <= k+1 && k != maxSignedValue(limit.Type)
   280  
   281  					// TODO: other unrolling idioms
   282  					// for i := 0; i < KNN - KNN % k ; i += k
   283  					// for i := 0; i < KNN&^(k-1) ; i += k // k a power of 2
   284  					// for i := 0; i < KNN&(-k) ; i += k // k a power of 2
   285  				} else { // step < 0
   286  					if limit.IsGenericIntConst() {
   287  						// Figure out the actual smallest value.
   288  						v := limit.AuxInt
   289  						if !inclusive {
   290  							if v == maxSignedValue(limit.Type) {
   291  								return false // > maxint is never satisfiable.
   292  							}
   293  							v++
   294  						}
   295  						if init.IsGenericIntConst() {
   296  							// Use stride to compute a better lower limit.
   297  							if init.AuxInt < v {
   298  								return false
   299  							}
   300  							// TODO(1.27): investigate passing a smaller-magnitude underflow limit to subU
   301  							// for subWillUnderflow.
   302  							v = subU(init.AuxInt, diff(init.AuxInt, v)/uint64(-step)*uint64(-step))
   303  						}
   304  						if subWillUnderflow(v, -step, minSignedValue(ind.Type)) {
   305  							return false
   306  						}
   307  						if inclusive && v != limit.AuxInt || !inclusive && v-1 != limit.AuxInt {
   308  							// We know a better limit than the programmer did. Use our limit instead.
   309  							limit = f.ConstVal(limit.Op, limit.Type, v, true)
   310  							inclusive = true
   311  						}
   312  						return true
   313  					}
   314  					if step == -1 && !inclusive {
   315  						// Can't underflow because minint is never a possible value.
   316  						return true
   317  					}
   318  				}
   319  				return false
   320  			}
   321  
   322  			if ok() {
   323  				flags := indVarFlags(0)
   324  				var min, max *ssa.Value
   325  				if step > 0 {
   326  					min = init
   327  					max = limit
   328  					if inclusive {
   329  						flags |= indVarMaxInc
   330  					}
   331  				} else {
   332  					min = limit
   333  					max = init
   334  					flags |= indVarMaxInc
   335  					if !inclusive {
   336  						flags |= indVarMinExc
   337  					}
   338  					step = -step
   339  				}
   340  				if f.Pass.Debug >= 1 {
   341  					printIndVar(b, ind, min, max, step, flags)
   342  				}
   343  
   344  				iv = append(iv, indVar{
   345  					ind: ind,
   346  					nxt: nxt,
   347  					min: min,
   348  					max: max,
   349  					// This is startBody.b, where startBody is the edge from the comparison for the
   350  					// induction variable, not necessarily the in-loop edge from the loop header.
   351  					// Induction variable bounds are not valid in the loop before this edge.
   352  					entry: startBody.B,
   353  					step:  step,
   354  					flags: flags,
   355  				})
   356  				b.Logf("found induction variable %v (inc = %v, min = %v, max = %v)\n", ind, inc, min, max)
   357  			}
   358  		}
   359  	}
   360  
   361  	return iv
   362  }
   363  
   364  // subWillUnderflow checks if x - y underflows the min value.
   365  // y must be positive.
   366  func subWillUnderflow(x, y int64, min int64) bool {
   367  	if y < 0 {
   368  		base.Fatalf("expecting positive value")
   369  	}
   370  	return x < min+y
   371  }
   372  
   373  // addWillOverflow checks if x + y overflows the max value.
   374  // y must be positive.
   375  func addWillOverflow(x, y int64, max int64) bool {
   376  	if y < 0 {
   377  		base.Fatalf("expecting positive value")
   378  	}
   379  	return x > max-y
   380  }
   381  
   382  // diff returns x-y as a uint64. Requires x>=y.
   383  func diff(x, y int64) uint64 {
   384  	if x < y {
   385  		base.Fatalf("diff %d - %d underflowed", x, y)
   386  	}
   387  	return uint64(x - y)
   388  }
   389  
   390  // addU returns x+y. Requires that x+y does not overflow an int64.
   391  func addU(x int64, y uint64) int64 {
   392  	if y >= 1<<63 {
   393  		if x >= 0 {
   394  			base.Fatalf("addU overflowed %d + %d", x, y)
   395  		}
   396  		x += 1<<63 - 1
   397  		x += 1
   398  		y -= 1 << 63
   399  	}
   400  	// TODO(1.27): investigate passing a smaller-magnitude overflow limit in here.
   401  	if addWillOverflow(x, int64(y), maxSignedValue(types.Types[types.TINT64])) {
   402  		base.Fatalf("addU overflowed %d + %d", x, y)
   403  	}
   404  	return x + int64(y)
   405  }
   406  
   407  // subU returns x-y. Requires that x-y does not underflow an int64.
   408  func subU(x int64, y uint64) int64 {
   409  	if y >= 1<<63 {
   410  		if x < 0 {
   411  			base.Fatalf("subU underflowed %d - %d", x, y)
   412  		}
   413  		x -= 1<<63 - 1
   414  		x -= 1
   415  		y -= 1 << 63
   416  	}
   417  	// TODO(1.27): investigate passing a smaller-magnitude underflow limit in here.
   418  	if subWillUnderflow(x, int64(y), minSignedValue(types.Types[types.TINT64])) {
   419  		base.Fatalf("subU underflowed %d - %d", x, y)
   420  	}
   421  	return x - int64(y)
   422  }
   423  
   424  // if v is known to be x - c, where x is known to be nonnegative and c is a
   425  // constant, return x, c. Otherwise return nil, 0.
   426  func findKNN(v *ssa.Value) (*ssa.Value, int64) {
   427  	var x, y *ssa.Value
   428  	x = v
   429  	switch v.Op {
   430  	case ssaop.OpSub64, ssaop.OpSub32, ssaop.OpSub16, ssaop.OpSub8:
   431  		x = v.Args[0]
   432  		y = v.Args[1]
   433  
   434  	case ssaop.OpAdd64, ssaop.OpAdd32, ssaop.OpAdd16, ssaop.OpAdd8:
   435  		x = v.Args[0]
   436  		y = v.Args[1]
   437  		if x.IsGenericIntConst() {
   438  			x, y = y, x
   439  		}
   440  	}
   441  	switch x.Op {
   442  	case ssaop.OpSliceLen, ssaop.OpStringLen, ssaop.OpSliceCap:
   443  	default:
   444  		return nil, 0
   445  	}
   446  	if y == nil {
   447  		return x, 0
   448  	}
   449  	if !y.IsGenericIntConst() {
   450  		return nil, 0
   451  	}
   452  	if v.Op == ssaop.OpAdd64 || v.Op == ssaop.OpAdd32 || v.Op == ssaop.OpAdd16 || v.Op == ssaop.OpAdd8 {
   453  		return x, -y.AuxInt
   454  	}
   455  	return x, y.AuxInt
   456  }
   457  
   458  func printIndVar(b *ssa.Block, i, min, max *ssa.Value, inc int64, flags indVarFlags) {
   459  	mb1, mb2 := "[", "]"
   460  	if flags&indVarMinExc != 0 {
   461  		mb1 = "("
   462  	}
   463  	if flags&indVarMaxInc == 0 {
   464  		mb2 = ")"
   465  	}
   466  
   467  	mlim1, mlim2 := fmt.Sprint(min.AuxInt), fmt.Sprint(max.AuxInt)
   468  	if !min.IsGenericIntConst() {
   469  		if b.Func.Pass.Debug >= 2 {
   470  			mlim1 = fmt.Sprint(min)
   471  		} else {
   472  			mlim1 = "?"
   473  		}
   474  	}
   475  	if !max.IsGenericIntConst() {
   476  		if b.Func.Pass.Debug >= 2 {
   477  			mlim2 = fmt.Sprint(max)
   478  		} else {
   479  			mlim2 = "?"
   480  		}
   481  	}
   482  	extra := ""
   483  	if b.Func.Pass.Debug >= 2 {
   484  		extra = fmt.Sprintf(" (%s)", i)
   485  	}
   486  	b.Func.Warnl(b.Pos, "Induction variable: limits %v%v,%v%v, increment %d%s", mb1, mlim1, mlim2, mb2, inc, extra)
   487  }
   488  
   489  func minSignedValue(t *types.Type) int64 {
   490  	return -1 << (t.Size()*8 - 1)
   491  }
   492  
   493  func maxSignedValue(t *types.Type) int64 {
   494  	return 1<<((t.Size()*8)-1) - 1
   495  }
   496  

View as plain text