Source file src/cmd/compile/internal/ssa/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 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 // 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)
    19  )
    20  
    21  type indVar struct {
    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.
    27  	flags indVarFlags
    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]
    33  }
    34  
    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)
    40  //   - the header's edge returning from the body
    41  //
    42  // Currently, we detect induction variables that match (Phi min nxt),
    43  // with nxt being (Add inc ind).
    44  // If it can't parse the induction variable correctly, it returns (nil, nil, nil).
    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  		// Not a recognized induction variable.
    56  		return
    57  	}
    58  
    59  	if nxt.Args[0] == ind { // nxt = ind + inc
    60  		inc = nxt.Args[1]
    61  	} else if nxt.Args[1] == ind { // nxt = inc + ind
    62  		inc = nxt.Args[0]
    63  	} else {
    64  		panic("unreachable") // one of the cases must be true from the above.
    65  	}
    66  
    67  	return
    68  }
    69  
    70  // findIndVar finds induction variables in a function.
    71  //
    72  // Look for variables and blocks that satisfy the following
    73  //
    74  //	 loop:
    75  //	   ind = (Phi min nxt),
    76  //	   if ind < max
    77  //	     then goto enter_loop
    78  //	     else goto exit_loop
    79  //
    80  //	   enter_loop:
    81  //		do something
    82  //	      nxt = inc + ind
    83  //		goto loop
    84  //
    85  //	 exit_loop:
    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   // induction variable
    96  		var init *Value  // starting value
    97  		var limit *Value // ending value
    98  
    99  		// Check that the control if it either ind </<= limit or limit </<= ind.
   100  		// TODO: Handle unsigned comparisons?
   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  		// See if this is really an induction variable
   114  		less := true
   115  		init, inc, nxt, loopReturn := parseIndVar(ind)
   116  		if init == nil {
   117  			// We failed to parse the induction variable. Before punting, we want to check
   118  			// whether the control op was written with the induction variable on the RHS
   119  			// instead of the LHS. This happens for the downwards case, like:
   120  			//     for i := len(n)-1; i >= 0; i--
   121  			init, inc, nxt, loopReturn = parseIndVar(limit)
   122  			if init == nil {
   123  				// No recognized induction variable on either operand
   124  				continue
   125  			}
   126  
   127  			// Ok, the arguments were reversed. Swap them, and remember that we're
   128  			// looking at an ind >/>= loop (so the induction must be decrementing).
   129  			ind, limit = limit, ind
   130  			less = false
   131  		}
   132  
   133  		if ind.Block != b {
   134  			// TODO: Could be extended to include disjointed loop headers.
   135  			// I don't think this is causing missed optimizations in real world code often.
   136  			// See https://go.dev/issue/63955
   137  			continue
   138  		}
   139  
   140  		// Expect the increment to be a nonzero constant.
   141  		if !inc.isGenericIntConst() {
   142  			continue
   143  		}
   144  		step := inc.AuxInt
   145  		if step == 0 {
   146  			continue
   147  		}
   148  
   149  		// startBody is the edge that eventually returns to the loop header.
   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  			// if x { goto exit } else { goto entry } is identical to if !x { goto entry } else { goto exit }
   156  			startBody = b.Succs[1]
   157  			less = !less
   158  			inclusive = !inclusive
   159  		default:
   160  			continue
   161  		}
   162  
   163  		// Increment sign must match comparison direction.
   164  		// When incrementing, the termination comparison must be ind </<= limit.
   165  		// When decrementing, the termination comparison must be ind >/>= limit.
   166  		// See issue 26116.
   167  		if step > 0 && !less {
   168  			continue
   169  		}
   170  		if step < 0 && less {
   171  			continue
   172  		}
   173  
   174  		// Up to now we extracted the induction variable (ind),
   175  		// the increment delta (inc), the temporary sum (nxt),
   176  		// the initial value (init) and the limiting value (limit).
   177  		//
   178  		// We also know that ind has the form (Phi init nxt) where
   179  		// nxt is (Add inc nxt) which means: 1) inc dominates nxt
   180  		// and 2) there is a loop starting at inc and containing nxt.
   181  		//
   182  		// We need to prove that the induction variable is incremented
   183  		// only when it's smaller than the limiting value.
   184  		// Two conditions must happen listed below to accept ind
   185  		// as an induction variable.
   186  
   187  		// First condition: loop entry has a single predecessor, which
   188  		// is the header block.  This implies that b.Succs[0] is
   189  		// reached iff ind < limit.
   190  		if len(startBody.b.Preds) != 1 {
   191  			// the other successor must exit the loop.
   192  			continue
   193  		}
   194  
   195  		// Second condition: startBody.b dominates nxt so that
   196  		// nxt is computed when inc < limit.
   197  		if !sdom.IsAncestorEq(startBody.b, nxt.Block) {
   198  			// inc+ind can only be reached through the branch that enters the loop.
   199  			continue
   200  		}
   201  
   202  		// Check for overflow/underflow. We need to make sure that inc never causes
   203  		// the induction variable to wrap around.
   204  		// We use a function wrapper here for easy return true / return false / keep going logic.
   205  		// This function returns true if the increment will never overflow/underflow.
   206  		ok := func() bool {
   207  			if step > 0 {
   208  				if limit.isGenericIntConst() {
   209  					// Figure out the actual largest value.
   210  					v := limit.AuxInt
   211  					if !inclusive {
   212  						if v == minSignedValue(limit.Type) {
   213  							return false // < minint is never satisfiable.
   214  						}
   215  						v--
   216  					}
   217  					if init.isGenericIntConst() {
   218  						// Use stride to compute a better lower limit.
   219  						if init.AuxInt > v {
   220  							return false
   221  						}
   222  						v = addU(init.AuxInt, diff(v, init.AuxInt)/uint64(step)*uint64(step))
   223  					}
   224  					if addWillOverflow(v, step) {
   225  						return false
   226  					}
   227  					if inclusive && v != limit.AuxInt || !inclusive && v+1 != limit.AuxInt {
   228  						// We know a better limit than the programmer did. Use our limit instead.
   229  						limit = f.constVal(limit.Op, limit.Type, v, true)
   230  						inclusive = true
   231  					}
   232  					return true
   233  				}
   234  				if step == 1 && !inclusive {
   235  					// Can't overflow because maxint is never a possible value.
   236  					return true
   237  				}
   238  				// If the limit is not a constant, check to see if it is a
   239  				// negative offset from a known non-negative value.
   240  				knn, k := findKNN(limit)
   241  				if knn == nil || k < 0 {
   242  					return false
   243  				}
   244  				// limit == (something nonnegative) - k. That subtraction can't underflow, so
   245  				// we can trust it.
   246  				if inclusive {
   247  					// ind <= knn - k cannot overflow if step is at most k
   248  					return step <= k
   249  				}
   250  				// ind < knn - k cannot overflow if step is at most k+1
   251  				return step <= k+1 && k != maxSignedValue(limit.Type)
   252  			} else { // step < 0
   253  				if limit.Op == OpConst64 {
   254  					// Figure out the actual smallest value.
   255  					v := limit.AuxInt
   256  					if !inclusive {
   257  						if v == maxSignedValue(limit.Type) {
   258  							return false // > maxint is never satisfiable.
   259  						}
   260  						v++
   261  					}
   262  					if init.isGenericIntConst() {
   263  						// Use stride to compute a better lower limit.
   264  						if init.AuxInt < v {
   265  							return false
   266  						}
   267  						v = subU(init.AuxInt, diff(init.AuxInt, v)/uint64(-step)*uint64(-step))
   268  					}
   269  					if subWillUnderflow(v, -step) {
   270  						return false
   271  					}
   272  					if inclusive && v != limit.AuxInt || !inclusive && v-1 != limit.AuxInt {
   273  						// We know a better limit than the programmer did. Use our limit instead.
   274  						limit = f.constVal(limit.Op, limit.Type, v, true)
   275  						inclusive = true
   276  					}
   277  					return true
   278  				}
   279  				if step == -1 && !inclusive {
   280  					// Can't underflow because minint is never a possible value.
   281  					return true
   282  				}
   283  			}
   284  			return false
   285  
   286  		}
   287  
   288  		if ok() {
   289  			flags := indVarFlags(0)
   290  			var min, max *Value
   291  			if step > 0 {
   292  				min = init
   293  				max = limit
   294  				if inclusive {
   295  					flags |= indVarMaxInc
   296  				}
   297  			} else {
   298  				min = limit
   299  				max = init
   300  				flags |= indVarMaxInc
   301  				if !inclusive {
   302  					flags |= indVarMinExc
   303  				}
   304  				flags |= indVarCountDown
   305  				step = -step
   306  			}
   307  			if f.pass.debug >= 1 {
   308  				printIndVar(b, ind, min, max, step, flags)
   309  			}
   310  
   311  			iv = append(iv, indVar{
   312  				ind:   ind,
   313  				nxt:   nxt,
   314  				min:   min,
   315  				max:   max,
   316  				entry: startBody.b,
   317  				flags: flags,
   318  			})
   319  			b.Logf("found induction variable %v (inc = %v, min = %v, max = %v)\n", ind, inc, min, max)
   320  		}
   321  
   322  		// TODO: other unrolling idioms
   323  		// for i := 0; i < KNN - KNN % k ; i += k
   324  		// for i := 0; i < KNN&^(k-1) ; i += k // k a power of 2
   325  		// for i := 0; i < KNN&(-k) ; i += k // k a power of 2
   326  	}
   327  
   328  	return iv
   329  }
   330  
   331  // addWillOverflow reports whether x+y would result in a value more than maxint.
   332  func addWillOverflow(x, y int64) bool {
   333  	return x+y < x
   334  }
   335  
   336  // subWillUnderflow reports whether x-y would result in a value less than minint.
   337  func subWillUnderflow(x, y int64) bool {
   338  	return x-y > x
   339  }
   340  
   341  // diff returns x-y as a uint64. Requires x>=y.
   342  func diff(x, y int64) uint64 {
   343  	if x < y {
   344  		base.Fatalf("diff %d - %d underflowed", x, y)
   345  	}
   346  	return uint64(x - y)
   347  }
   348  
   349  // addU returns x+y. Requires that x+y does not overflow an int64.
   350  func addU(x int64, y uint64) int64 {
   351  	if y >= 1<<63 {
   352  		if x >= 0 {
   353  			base.Fatalf("addU overflowed %d + %d", x, y)
   354  		}
   355  		x += 1<<63 - 1
   356  		x += 1
   357  		y -= 1 << 63
   358  	}
   359  	if addWillOverflow(x, int64(y)) {
   360  		base.Fatalf("addU overflowed %d + %d", x, y)
   361  	}
   362  	return x + int64(y)
   363  }
   364  
   365  // subU returns x-y. Requires that x-y does not underflow an int64.
   366  func subU(x int64, y uint64) int64 {
   367  	if y >= 1<<63 {
   368  		if x < 0 {
   369  			base.Fatalf("subU underflowed %d - %d", x, y)
   370  		}
   371  		x -= 1<<63 - 1
   372  		x -= 1
   373  		y -= 1 << 63
   374  	}
   375  	if subWillUnderflow(x, int64(y)) {
   376  		base.Fatalf("subU underflowed %d - %d", x, y)
   377  	}
   378  	return x - int64(y)
   379  }
   380  
   381  // if v is known to be x - c, where x is known to be nonnegative and c is a
   382  // constant, return x, c. Otherwise return nil, 0.
   383  func findKNN(v *Value) (*Value, int64) {
   384  	var x, y *Value
   385  	x = v
   386  	switch v.Op {
   387  	case OpSub64, OpSub32, OpSub16, OpSub8:
   388  		x = v.Args[0]
   389  		y = v.Args[1]
   390  
   391  	case OpAdd64, OpAdd32, OpAdd16, OpAdd8:
   392  		x = v.Args[0]
   393  		y = v.Args[1]
   394  		if x.isGenericIntConst() {
   395  			x, y = y, x
   396  		}
   397  	}
   398  	switch x.Op {
   399  	case OpSliceLen, OpStringLen, OpSliceCap:
   400  	default:
   401  		return nil, 0
   402  	}
   403  	if y == nil {
   404  		return x, 0
   405  	}
   406  	if !y.isGenericIntConst() {
   407  		return nil, 0
   408  	}
   409  	if v.Op == OpAdd64 || v.Op == OpAdd32 || v.Op == OpAdd16 || v.Op == OpAdd8 {
   410  		return x, -y.AuxInt
   411  	}
   412  	return x, y.AuxInt
   413  }
   414  
   415  func printIndVar(b *Block, i, min, max *Value, inc int64, flags indVarFlags) {
   416  	mb1, mb2 := "[", "]"
   417  	if flags&indVarMinExc != 0 {
   418  		mb1 = "("
   419  	}
   420  	if flags&indVarMaxInc == 0 {
   421  		mb2 = ")"
   422  	}
   423  
   424  	mlim1, mlim2 := fmt.Sprint(min.AuxInt), fmt.Sprint(max.AuxInt)
   425  	if !min.isGenericIntConst() {
   426  		if b.Func.pass.debug >= 2 {
   427  			mlim1 = fmt.Sprint(min)
   428  		} else {
   429  			mlim1 = "?"
   430  		}
   431  	}
   432  	if !max.isGenericIntConst() {
   433  		if b.Func.pass.debug >= 2 {
   434  			mlim2 = fmt.Sprint(max)
   435  		} else {
   436  			mlim2 = "?"
   437  		}
   438  	}
   439  	extra := ""
   440  	if b.Func.pass.debug >= 2 {
   441  		extra = fmt.Sprintf(" (%s)", i)
   442  	}
   443  	b.Func.Warnl(b.Pos, "Induction variable: limits %v%v,%v%v, increment %d%s", mb1, mlim1, mlim2, mb2, inc, extra)
   444  }
   445  
   446  func minSignedValue(t *types.Type) int64 {
   447  	return -1 << (t.Size()*8 - 1)
   448  }
   449  
   450  func maxSignedValue(t *types.Type) int64 {
   451  	return 1<<((t.Size()*8)-1) - 1
   452  }
   453  

View as plain text