github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/gc/popt.go (about)

     1  // Derived from Inferno utils/6c/gc.h
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6c/gc.h
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  // "Portable" optimizations.
    32  
    33  package gc
    34  
    35  import (
    36  	"cmd/internal/obj"
    37  	"fmt"
    38  	"sort"
    39  	"strings"
    40  )
    41  
    42  type OptStats struct {
    43  	Ncvtreg int32
    44  	Nspill  int32
    45  	Nreload int32
    46  	Ndelmov int32
    47  	Nvar    int32
    48  	Naddr   int32
    49  }
    50  
    51  var Ostats OptStats
    52  
    53  var noreturn_symlist [10]*Sym
    54  
    55  // p is a call instruction. Does the call fail to return?
    56  func Noreturn(p *obj.Prog) bool {
    57  	if noreturn_symlist[0] == nil {
    58  		noreturn_symlist[0] = Pkglookup("panicindex", Runtimepkg)
    59  		noreturn_symlist[1] = Pkglookup("panicslice", Runtimepkg)
    60  		noreturn_symlist[2] = Pkglookup("throwinit", Runtimepkg)
    61  		noreturn_symlist[3] = Pkglookup("gopanic", Runtimepkg)
    62  		noreturn_symlist[4] = Pkglookup("panicwrap", Runtimepkg)
    63  		noreturn_symlist[5] = Pkglookup("throwreturn", Runtimepkg)
    64  		noreturn_symlist[6] = Pkglookup("selectgo", Runtimepkg)
    65  		noreturn_symlist[7] = Pkglookup("block", Runtimepkg)
    66  	}
    67  
    68  	if p.To.Node == nil {
    69  		return false
    70  	}
    71  	s := ((p.To.Node).(*Node)).Sym
    72  	if s == nil {
    73  		return false
    74  	}
    75  	for i := 0; noreturn_symlist[i] != nil; i++ {
    76  		if s == noreturn_symlist[i] {
    77  			return true
    78  		}
    79  	}
    80  	return false
    81  }
    82  
    83  // JMP chasing and removal.
    84  //
    85  // The code generator depends on being able to write out jump
    86  // instructions that it can jump to now but fill in later.
    87  // the linker will resolve them nicely, but they make the code
    88  // longer and more difficult to follow during debugging.
    89  // Remove them.
    90  
    91  // what instruction does a JMP to p eventually land on?
    92  func chasejmp(p *obj.Prog, jmploop *int) *obj.Prog {
    93  	n := 0
    94  	for p != nil && p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH {
    95  		n++
    96  		if n > 10 {
    97  			*jmploop = 1
    98  			break
    99  		}
   100  
   101  		p = p.To.Val.(*obj.Prog)
   102  	}
   103  
   104  	return p
   105  }
   106  
   107  // reuse reg pointer for mark/sweep state.
   108  // leave reg==nil at end because alive==nil.
   109  var alive interface{} = nil
   110  var dead interface{} = 1
   111  
   112  // mark all code reachable from firstp as alive
   113  func mark(firstp *obj.Prog) {
   114  	for p := firstp; p != nil; p = p.Link {
   115  		if p.Opt != dead {
   116  			break
   117  		}
   118  		p.Opt = alive
   119  		if p.As != obj.ACALL && p.To.Type == obj.TYPE_BRANCH && p.To.Val.(*obj.Prog) != nil {
   120  			mark(p.To.Val.(*obj.Prog))
   121  		}
   122  		if p.As == obj.AJMP || p.As == obj.ARET || p.As == obj.AUNDEF {
   123  			break
   124  		}
   125  	}
   126  }
   127  
   128  func fixjmp(firstp *obj.Prog) {
   129  	if Debug['R'] != 0 && Debug['v'] != 0 {
   130  		fmt.Printf("\nfixjmp\n")
   131  	}
   132  
   133  	// pass 1: resolve jump to jump, mark all code as dead.
   134  	jmploop := 0
   135  
   136  	for p := firstp; p != nil; p = p.Link {
   137  		if Debug['R'] != 0 && Debug['v'] != 0 {
   138  			fmt.Printf("%v\n", p)
   139  		}
   140  		if p.As != obj.ACALL && p.To.Type == obj.TYPE_BRANCH && p.To.Val.(*obj.Prog) != nil && p.To.Val.(*obj.Prog).As == obj.AJMP {
   141  			if Debug['N'] == 0 {
   142  				p.To.Val = chasejmp(p.To.Val.(*obj.Prog), &jmploop)
   143  				if Debug['R'] != 0 && Debug['v'] != 0 {
   144  					fmt.Printf("->%v\n", p)
   145  				}
   146  			}
   147  		}
   148  
   149  		p.Opt = dead
   150  	}
   151  	if Debug['R'] != 0 && Debug['v'] != 0 {
   152  		fmt.Printf("\n")
   153  	}
   154  
   155  	// pass 2: mark all reachable code alive
   156  	mark(firstp)
   157  
   158  	// pass 3: delete dead code (mostly JMPs).
   159  	var last *obj.Prog
   160  
   161  	for p := firstp; p != nil; p = p.Link {
   162  		if p.Opt == dead {
   163  			if p.Link == nil && p.As == obj.ARET && last != nil && last.As != obj.ARET {
   164  				// This is the final ARET, and the code so far doesn't have one.
   165  				// Let it stay. The register allocator assumes that all live code in
   166  				// the function can be traversed by starting at all the RET instructions
   167  				// and following predecessor links. If we remove the final RET,
   168  				// this assumption will not hold in the case of an infinite loop
   169  				// at the end of a function.
   170  				// Keep the RET but mark it dead for the liveness analysis.
   171  				p.Mode = 1
   172  			} else {
   173  				if Debug['R'] != 0 && Debug['v'] != 0 {
   174  					fmt.Printf("del %v\n", p)
   175  				}
   176  				continue
   177  			}
   178  		}
   179  
   180  		if last != nil {
   181  			last.Link = p
   182  		}
   183  		last = p
   184  	}
   185  
   186  	last.Link = nil
   187  
   188  	// pass 4: elide JMP to next instruction.
   189  	// only safe if there are no jumps to JMPs anymore.
   190  	if jmploop == 0 && Debug['N'] == 0 {
   191  		var last *obj.Prog
   192  		for p := firstp; p != nil; p = p.Link {
   193  			if p.As == obj.AJMP && p.To.Type == obj.TYPE_BRANCH && p.To.Val == p.Link {
   194  				if Debug['R'] != 0 && Debug['v'] != 0 {
   195  					fmt.Printf("del %v\n", p)
   196  				}
   197  				continue
   198  			}
   199  
   200  			if last != nil {
   201  				last.Link = p
   202  			}
   203  			last = p
   204  		}
   205  
   206  		last.Link = nil
   207  	}
   208  
   209  	if Debug['R'] != 0 && Debug['v'] != 0 {
   210  		fmt.Printf("\n")
   211  		for p := firstp; p != nil; p = p.Link {
   212  			fmt.Printf("%v\n", p)
   213  		}
   214  		fmt.Printf("\n")
   215  	}
   216  }
   217  
   218  // Control flow analysis. The Flow structures hold predecessor and successor
   219  // information as well as basic loop analysis.
   220  //
   221  //	graph = Flowstart(firstp, nil)
   222  //	... use flow graph ...
   223  //	Flowend(graph) // free graph
   224  //
   225  // Typical uses of the flow graph are to iterate over all the flow-relevant instructions:
   226  //
   227  //	for f := graph.Start; f != nil; f = f.Link {}
   228  //
   229  // or, given an instruction f, to iterate over all the predecessors, which is
   230  // f.P1 and this list:
   231  //
   232  //	for f2 := f.P2; f2 != nil; f2 = f2.P2link {}
   233  //
   234  // The second argument (newData) to Flowstart specifies a func to create object
   235  // for every f.Data field, for use by the client.
   236  // If newData is nil, f.Data will be nil.
   237  
   238  var flowmark int
   239  
   240  // MaxFlowProg is the maximum size program (counted in instructions)
   241  // for which the flow code will build a graph. Functions larger than this limit
   242  // will not have flow graphs and consequently will not be optimized.
   243  const MaxFlowProg = 50000
   244  
   245  var ffcache []Flow // reusable []Flow, to reduce allocation
   246  
   247  func growffcache(n int) {
   248  	if n > cap(ffcache) {
   249  		n = (n * 5) / 4
   250  		if n > MaxFlowProg {
   251  			n = MaxFlowProg
   252  		}
   253  		ffcache = make([]Flow, n)
   254  	}
   255  	ffcache = ffcache[:n]
   256  }
   257  
   258  func Flowstart(firstp *obj.Prog, newData func() interface{}) *Graph {
   259  	// Count and mark instructions to annotate.
   260  	nf := 0
   261  
   262  	for p := firstp; p != nil; p = p.Link {
   263  		p.Opt = nil // should be already, but just in case
   264  		Thearch.Proginfo(p)
   265  		if p.Info.Flags&Skip != 0 {
   266  			continue
   267  		}
   268  		p.Opt = &flowmark
   269  		nf++
   270  	}
   271  
   272  	if nf == 0 {
   273  		return nil
   274  	}
   275  
   276  	if nf >= MaxFlowProg {
   277  		if Debug['v'] != 0 {
   278  			Warn("%v is too big (%d instructions)", Curfn.Func.Nname.Sym, nf)
   279  		}
   280  		return nil
   281  	}
   282  
   283  	// Allocate annotations and assign to instructions.
   284  	graph := new(Graph)
   285  
   286  	growffcache(nf)
   287  	ff := ffcache
   288  	start := &ff[0]
   289  	id := 0
   290  	var last *Flow
   291  	for p := firstp; p != nil; p = p.Link {
   292  		if p.Opt == nil {
   293  			continue
   294  		}
   295  		f := &ff[0]
   296  		ff = ff[1:]
   297  		p.Opt = f
   298  		f.Prog = p
   299  		if last != nil {
   300  			last.Link = f
   301  		}
   302  		last = f
   303  		if newData != nil {
   304  			f.Data = newData()
   305  		}
   306  		f.Id = int32(id)
   307  		id++
   308  	}
   309  
   310  	// Fill in pred/succ information.
   311  	var f1 *Flow
   312  	var p *obj.Prog
   313  	for f := start; f != nil; f = f.Link {
   314  		p = f.Prog
   315  		if p.Info.Flags&Break == 0 {
   316  			f1 = f.Link
   317  			f.S1 = f1
   318  			f1.P1 = f
   319  		}
   320  
   321  		if p.To.Type == obj.TYPE_BRANCH {
   322  			if p.To.Val == nil {
   323  				Fatalf("pnil %v", p)
   324  			}
   325  			f1 = p.To.Val.(*obj.Prog).Opt.(*Flow)
   326  			if f1 == nil {
   327  				Fatalf("fnil %v / %v", p, p.To.Val.(*obj.Prog))
   328  			}
   329  			if f1 == f {
   330  				//fatal("self loop %v", p);
   331  				continue
   332  			}
   333  
   334  			f.S2 = f1
   335  			f.P2link = f1.P2
   336  			f1.P2 = f
   337  		}
   338  	}
   339  
   340  	graph.Start = start
   341  	graph.Num = nf
   342  	return graph
   343  }
   344  
   345  func Flowend(graph *Graph) {
   346  	for f := graph.Start; f != nil; f = f.Link {
   347  		f.Prog.Info.Flags = 0 // drop cached proginfo
   348  		f.Prog.Opt = nil
   349  	}
   350  	clear := ffcache[:graph.Num]
   351  	for i := range clear {
   352  		clear[i] = Flow{}
   353  	}
   354  }
   355  
   356  // find looping structure
   357  //
   358  // 1) find reverse postordering
   359  // 2) find approximate dominators,
   360  //	the actual dominators if the flow graph is reducible
   361  //	otherwise, dominators plus some other non-dominators.
   362  //	See Matthew S. Hecht and Jeffrey D. Ullman,
   363  //	"Analysis of a Simple Algorithm for Global Data Flow Problems",
   364  //	Conf.  Record of ACM Symp. on Principles of Prog. Langs, Boston, Massachusetts,
   365  //	Oct. 1-3, 1973, pp.  207-217.
   366  // 3) find all nodes with a predecessor dominated by the current node.
   367  //	such a node is a loop head.
   368  //	recursively, all preds with a greater rpo number are in the loop
   369  func postorder(r *Flow, rpo2r []*Flow, n int32) int32 {
   370  	r.Rpo = 1
   371  	r1 := r.S1
   372  	if r1 != nil && r1.Rpo == 0 {
   373  		n = postorder(r1, rpo2r, n)
   374  	}
   375  	r1 = r.S2
   376  	if r1 != nil && r1.Rpo == 0 {
   377  		n = postorder(r1, rpo2r, n)
   378  	}
   379  	rpo2r[n] = r
   380  	n++
   381  	return n
   382  }
   383  
   384  func rpolca(idom []int32, rpo1 int32, rpo2 int32) int32 {
   385  	if rpo1 == -1 {
   386  		return rpo2
   387  	}
   388  	var t int32
   389  	for rpo1 != rpo2 {
   390  		if rpo1 > rpo2 {
   391  			t = rpo2
   392  			rpo2 = rpo1
   393  			rpo1 = t
   394  		}
   395  
   396  		for rpo1 < rpo2 {
   397  			t = idom[rpo2]
   398  			if t >= rpo2 {
   399  				Fatalf("bad idom")
   400  			}
   401  			rpo2 = t
   402  		}
   403  	}
   404  
   405  	return rpo1
   406  }
   407  
   408  func doms(idom []int32, r int32, s int32) bool {
   409  	for s > r {
   410  		s = idom[s]
   411  	}
   412  	return s == r
   413  }
   414  
   415  func loophead(idom []int32, r *Flow) bool {
   416  	src := r.Rpo
   417  	if r.P1 != nil && doms(idom, src, r.P1.Rpo) {
   418  		return true
   419  	}
   420  	for r = r.P2; r != nil; r = r.P2link {
   421  		if doms(idom, src, r.Rpo) {
   422  			return true
   423  		}
   424  	}
   425  	return false
   426  }
   427  
   428  func loopmark(rpo2r **Flow, head int32, r *Flow) {
   429  	if r.Rpo < head || r.Active == head {
   430  		return
   431  	}
   432  	r.Active = head
   433  	r.Loop += LOOP
   434  	if r.P1 != nil {
   435  		loopmark(rpo2r, head, r.P1)
   436  	}
   437  	for r = r.P2; r != nil; r = r.P2link {
   438  		loopmark(rpo2r, head, r)
   439  	}
   440  }
   441  
   442  func flowrpo(g *Graph) {
   443  	g.Rpo = make([]*Flow, g.Num)
   444  	idom := make([]int32, g.Num)
   445  
   446  	for r1 := g.Start; r1 != nil; r1 = r1.Link {
   447  		r1.Active = 0
   448  	}
   449  
   450  	rpo2r := g.Rpo
   451  	d := postorder(g.Start, rpo2r, 0)
   452  	nr := int32(g.Num)
   453  	if d > nr {
   454  		Fatalf("too many reg nodes %d %d", d, nr)
   455  	}
   456  	nr = d
   457  	var r1 *Flow
   458  	for i := int32(0); i < nr/2; i++ {
   459  		r1 = rpo2r[i]
   460  		rpo2r[i] = rpo2r[nr-1-i]
   461  		rpo2r[nr-1-i] = r1
   462  	}
   463  
   464  	for i := int32(0); i < nr; i++ {
   465  		rpo2r[i].Rpo = i
   466  	}
   467  
   468  	idom[0] = 0
   469  	var me int32
   470  	for i := int32(0); i < nr; i++ {
   471  		r1 = rpo2r[i]
   472  		me = r1.Rpo
   473  		d = -1
   474  
   475  		// rpo2r[r.Rpo] == r protects against considering dead code,
   476  		// which has r.Rpo == 0.
   477  		if r1.P1 != nil && rpo2r[r1.P1.Rpo] == r1.P1 && r1.P1.Rpo < me {
   478  			d = r1.P1.Rpo
   479  		}
   480  		for r1 = r1.P2; r1 != nil; r1 = r1.P2link {
   481  			if rpo2r[r1.Rpo] == r1 && r1.Rpo < me {
   482  				d = rpolca(idom, d, r1.Rpo)
   483  			}
   484  		}
   485  		idom[i] = d
   486  	}
   487  
   488  	for i := int32(0); i < nr; i++ {
   489  		r1 = rpo2r[i]
   490  		r1.Loop++
   491  		if r1.P2 != nil && loophead(idom, r1) {
   492  			loopmark(&rpo2r[0], i, r1)
   493  		}
   494  	}
   495  
   496  	for r1 := g.Start; r1 != nil; r1 = r1.Link {
   497  		r1.Active = 0
   498  	}
   499  }
   500  
   501  func Uniqp(r *Flow) *Flow {
   502  	r1 := r.P1
   503  	if r1 == nil {
   504  		r1 = r.P2
   505  		if r1 == nil || r1.P2link != nil {
   506  			return nil
   507  		}
   508  	} else if r.P2 != nil {
   509  		return nil
   510  	}
   511  	return r1
   512  }
   513  
   514  func Uniqs(r *Flow) *Flow {
   515  	r1 := r.S1
   516  	if r1 == nil {
   517  		r1 = r.S2
   518  		if r1 == nil {
   519  			return nil
   520  		}
   521  	} else if r.S2 != nil {
   522  		return nil
   523  	}
   524  	return r1
   525  }
   526  
   527  // The compilers assume they can generate temporary variables
   528  // as needed to preserve the right semantics or simplify code
   529  // generation and the back end will still generate good code.
   530  // This results in a large number of ephemeral temporary variables.
   531  // Merge temps with non-overlapping lifetimes and equal types using the
   532  // greedy algorithm in Poletto and Sarkar, "Linear Scan Register Allocation",
   533  // ACM TOPLAS 1999.
   534  
   535  type TempVar struct {
   536  	node    *Node
   537  	def     *Flow    // definition of temp var
   538  	use     *Flow    // use list, chained through Flow.data
   539  	merge   *TempVar // merge var with this one
   540  	start   int64    // smallest Prog.pc in live range
   541  	end     int64    // largest Prog.pc in live range
   542  	addr    bool     // address taken - no accurate end
   543  	removed bool     // removed from program
   544  }
   545  
   546  // startcmp sorts TempVars by start, then id, then symbol name.
   547  type startcmp []*TempVar
   548  
   549  func (x startcmp) Len() int      { return len(x) }
   550  func (x startcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   551  func (x startcmp) Less(i, j int) bool {
   552  	a := x[i]
   553  	b := x[j]
   554  
   555  	if a.start < b.start {
   556  		return true
   557  	}
   558  	if a.start > b.start {
   559  		return false
   560  	}
   561  
   562  	// Order what's left by id or symbol name,
   563  	// just so that sort is forced into a specific ordering,
   564  	// so that the result of the sort does not depend on
   565  	// the sort implementation.
   566  	if a.def != b.def {
   567  		return int(a.def.Id-b.def.Id) < 0
   568  	}
   569  	if a.node != b.node {
   570  		return a.node.Sym.Name < b.node.Sym.Name
   571  	}
   572  	return false
   573  }
   574  
   575  // Is n available for merging?
   576  func canmerge(n *Node) bool {
   577  	return n.Class == PAUTO && strings.HasPrefix(n.Sym.Name, "autotmp")
   578  }
   579  
   580  func mergetemp(firstp *obj.Prog) {
   581  	const (
   582  		debugmerge = 0
   583  	)
   584  
   585  	g := Flowstart(firstp, nil)
   586  	if g == nil {
   587  		return
   588  	}
   589  
   590  	// Build list of all mergeable variables.
   591  	var vars []*TempVar
   592  	for _, n := range Curfn.Func.Dcl {
   593  		if canmerge(n) {
   594  			v := &TempVar{}
   595  			vars = append(vars, v)
   596  			n.SetOpt(v)
   597  			v.node = n
   598  		}
   599  	}
   600  
   601  	// Build list of uses.
   602  	// We assume that the earliest reference to a temporary is its definition.
   603  	// This is not true of variables in general but our temporaries are all
   604  	// single-use (that's why we have so many!).
   605  	for f := g.Start; f != nil; f = f.Link {
   606  		p := f.Prog
   607  		if p.From.Node != nil && ((p.From.Node).(*Node)).Opt() != nil && p.To.Node != nil && ((p.To.Node).(*Node)).Opt() != nil {
   608  			Fatalf("double node %v", p)
   609  		}
   610  		var v *TempVar
   611  		n, _ := p.From.Node.(*Node)
   612  		if n != nil {
   613  			v, _ = n.Opt().(*TempVar)
   614  		}
   615  		if v == nil {
   616  			n, _ = p.To.Node.(*Node)
   617  			if n != nil {
   618  				v, _ = n.Opt().(*TempVar)
   619  			}
   620  		}
   621  		if v != nil {
   622  			if v.def == nil {
   623  				v.def = f
   624  			}
   625  			f.Data = v.use
   626  			v.use = f
   627  			if n == p.From.Node && (p.Info.Flags&LeftAddr != 0) {
   628  				v.addr = true
   629  			}
   630  		}
   631  	}
   632  
   633  	if debugmerge > 1 && Debug['v'] != 0 {
   634  		Dumpit("before", g.Start, 0)
   635  	}
   636  
   637  	nkill := 0
   638  
   639  	// Special case.
   640  	for _, v := range vars {
   641  		if v.addr {
   642  			continue
   643  		}
   644  
   645  		// Used in only one instruction, which had better be a write.
   646  		f := v.use
   647  		if f != nil && f.Data.(*Flow) == nil {
   648  			p := f.Prog
   649  			if p.To.Node == v.node && (p.Info.Flags&RightWrite != 0) && p.Info.Flags&RightRead == 0 {
   650  				p.As = obj.ANOP
   651  				p.To = obj.Addr{}
   652  				v.removed = true
   653  				if debugmerge > 0 && Debug['v'] != 0 {
   654  					fmt.Printf("drop write-only %v\n", v.node.Sym)
   655  				}
   656  			} else {
   657  				Fatalf("temp used and not set: %v", p)
   658  			}
   659  			nkill++
   660  			continue
   661  		}
   662  
   663  		// Written in one instruction, read in the next, otherwise unused,
   664  		// no jumps to the next instruction. Happens mainly in 386 compiler.
   665  		f = v.use
   666  		if f != nil && f.Link == f.Data.(*Flow) && (f.Data.(*Flow)).Data.(*Flow) == nil && Uniqp(f.Link) == f {
   667  			p := f.Prog
   668  			p1 := f.Link.Prog
   669  			const (
   670  				SizeAny = SizeB | SizeW | SizeL | SizeQ | SizeF | SizeD
   671  			)
   672  			if p.From.Node == v.node && p1.To.Node == v.node && (p.Info.Flags&Move != 0) && (p.Info.Flags|p1.Info.Flags)&(LeftAddr|RightAddr) == 0 && p.Info.Flags&SizeAny == p1.Info.Flags&SizeAny {
   673  				p1.From = p.From
   674  				Thearch.Excise(f)
   675  				v.removed = true
   676  				if debugmerge > 0 && Debug['v'] != 0 {
   677  					fmt.Printf("drop immediate-use %v\n", v.node.Sym)
   678  				}
   679  			}
   680  
   681  			nkill++
   682  			continue
   683  		}
   684  	}
   685  
   686  	// Traverse live range of each variable to set start, end.
   687  	// Each flood uses a new value of gen so that we don't have
   688  	// to clear all the r.Active words after each variable.
   689  	gen := uint32(0)
   690  
   691  	for _, v := range vars {
   692  		gen++
   693  		for f := v.use; f != nil; f = f.Data.(*Flow) {
   694  			mergewalk(v, f, gen)
   695  		}
   696  		if v.addr {
   697  			gen++
   698  			for f := v.use; f != nil; f = f.Data.(*Flow) {
   699  				varkillwalk(v, f, gen)
   700  			}
   701  		}
   702  	}
   703  
   704  	// Sort variables by start.
   705  	bystart := make([]*TempVar, len(vars))
   706  	copy(bystart, vars)
   707  	sort.Sort(startcmp(bystart))
   708  
   709  	// List of in-use variables, sorted by end, so that the ones that
   710  	// will last the longest are the earliest ones in the array.
   711  	// The tail inuse[nfree:] holds no-longer-used variables.
   712  	// In theory we should use a sorted tree so that insertions are
   713  	// guaranteed O(log n) and then the loop is guaranteed O(n log n).
   714  	// In practice, it doesn't really matter.
   715  	inuse := make([]*TempVar, len(bystart))
   716  
   717  	ninuse := 0
   718  	nfree := len(bystart)
   719  	for _, v := range bystart {
   720  		if debugmerge > 0 && Debug['v'] != 0 {
   721  			fmt.Printf("consider %v: removed=%t\n", Nconv(v.node, FmtSharp), v.removed)
   722  		}
   723  
   724  		if v.removed {
   725  			continue
   726  		}
   727  
   728  		// Expire no longer in use.
   729  		for ninuse > 0 && inuse[ninuse-1].end < v.start {
   730  			ninuse--
   731  			nfree--
   732  			inuse[nfree] = inuse[ninuse]
   733  		}
   734  
   735  		if debugmerge > 0 && Debug['v'] != 0 {
   736  			fmt.Printf("consider %v: removed=%t nfree=%d nvar=%d\n", Nconv(v.node, FmtSharp), v.removed, nfree, len(bystart))
   737  		}
   738  
   739  		// Find old temp to reuse if possible.
   740  		t := v.node.Type
   741  
   742  		for j := nfree; j < len(inuse); j++ {
   743  			v1 := inuse[j]
   744  			if debugmerge > 0 && Debug['v'] != 0 {
   745  				fmt.Printf("consider %v: maybe %v: type=%v,%v addrtaken=%v,%v\n", Nconv(v.node, FmtSharp), Nconv(v1.node, FmtSharp), t, v1.node.Type, v.node.Addrtaken, v1.node.Addrtaken)
   746  			}
   747  
   748  			// Require the types to match but also require the addrtaken bits to match.
   749  			// If a variable's address is taken, that disables registerization for the individual
   750  			// words of the variable (for example, the base,len,cap of a slice).
   751  			// We don't want to merge a non-addressed var with an addressed one and
   752  			// inhibit registerization of the former.
   753  			if Eqtype(t, v1.node.Type) && v.node.Addrtaken == v1.node.Addrtaken {
   754  				inuse[j] = inuse[nfree]
   755  				nfree++
   756  				if v1.merge != nil {
   757  					v.merge = v1.merge
   758  				} else {
   759  					v.merge = v1
   760  				}
   761  				nkill++
   762  				break
   763  			}
   764  		}
   765  
   766  		// Sort v into inuse.
   767  		j := ninuse
   768  		ninuse++
   769  
   770  		for j > 0 && inuse[j-1].end < v.end {
   771  			inuse[j] = inuse[j-1]
   772  			j--
   773  		}
   774  
   775  		inuse[j] = v
   776  	}
   777  
   778  	if debugmerge > 0 && Debug['v'] != 0 {
   779  		fmt.Printf("%v [%d - %d]\n", Curfn.Func.Nname.Sym, len(vars), nkill)
   780  		for _, v := range vars {
   781  			fmt.Printf("var %v %v %d-%d", Nconv(v.node, FmtSharp), v.node.Type, v.start, v.end)
   782  			if v.addr {
   783  				fmt.Printf(" addr=true")
   784  			}
   785  			if v.removed {
   786  				fmt.Printf(" removed=true")
   787  			}
   788  			if v.merge != nil {
   789  				fmt.Printf(" merge %v", Nconv(v.merge.node, FmtSharp))
   790  			}
   791  			if v.start == v.end && v.def != nil {
   792  				fmt.Printf(" %v", v.def.Prog)
   793  			}
   794  			fmt.Printf("\n")
   795  		}
   796  
   797  		if debugmerge > 1 && Debug['v'] != 0 {
   798  			Dumpit("after", g.Start, 0)
   799  		}
   800  	}
   801  
   802  	// Update node references to use merged temporaries.
   803  	for f := g.Start; f != nil; f = f.Link {
   804  		p := f.Prog
   805  		n, _ := p.From.Node.(*Node)
   806  		if n != nil {
   807  			v, _ := n.Opt().(*TempVar)
   808  			if v != nil && v.merge != nil {
   809  				p.From.Node = v.merge.node
   810  			}
   811  		}
   812  		n, _ = p.To.Node.(*Node)
   813  		if n != nil {
   814  			v, _ := n.Opt().(*TempVar)
   815  			if v != nil && v.merge != nil {
   816  				p.To.Node = v.merge.node
   817  			}
   818  		}
   819  	}
   820  
   821  	// Delete merged nodes from declaration list.
   822  	dcl := make([]*Node, 0, len(Curfn.Func.Dcl)-nkill)
   823  	for _, n := range Curfn.Func.Dcl {
   824  		v, _ := n.Opt().(*TempVar)
   825  		if v != nil && (v.merge != nil || v.removed) {
   826  			continue
   827  		}
   828  		dcl = append(dcl, n)
   829  	}
   830  	Curfn.Func.Dcl = dcl
   831  
   832  	// Clear aux structures.
   833  	for _, v := range vars {
   834  		v.node.SetOpt(nil)
   835  	}
   836  
   837  	Flowend(g)
   838  }
   839  
   840  func mergewalk(v *TempVar, f0 *Flow, gen uint32) {
   841  	var p *obj.Prog
   842  	var f1 *Flow
   843  
   844  	for f1 = f0; f1 != nil; f1 = f1.P1 {
   845  		if uint32(f1.Active) == gen {
   846  			break
   847  		}
   848  		f1.Active = int32(gen)
   849  		p = f1.Prog
   850  		if v.end < p.Pc {
   851  			v.end = p.Pc
   852  		}
   853  		if f1 == v.def {
   854  			v.start = p.Pc
   855  			break
   856  		}
   857  	}
   858  
   859  	var f2 *Flow
   860  	for f := f0; f != f1; f = f.P1 {
   861  		for f2 = f.P2; f2 != nil; f2 = f2.P2link {
   862  			mergewalk(v, f2, gen)
   863  		}
   864  	}
   865  }
   866  
   867  func varkillwalk(v *TempVar, f0 *Flow, gen uint32) {
   868  	var p *obj.Prog
   869  	var f1 *Flow
   870  
   871  	for f1 = f0; f1 != nil; f1 = f1.S1 {
   872  		if uint32(f1.Active) == gen {
   873  			break
   874  		}
   875  		f1.Active = int32(gen)
   876  		p = f1.Prog
   877  		if v.end < p.Pc {
   878  			v.end = p.Pc
   879  		}
   880  		if v.start > p.Pc {
   881  			v.start = p.Pc
   882  		}
   883  		if p.As == obj.ARET || (p.As == obj.AVARKILL && p.To.Node == v.node) {
   884  			break
   885  		}
   886  	}
   887  
   888  	for f := f0; f != f1; f = f.S1 {
   889  		varkillwalk(v, f.S2, gen)
   890  	}
   891  }
   892  
   893  // Eliminate redundant nil pointer checks.
   894  //
   895  // The code generation pass emits a CHECKNIL for every possibly nil pointer.
   896  // This pass removes a CHECKNIL if every predecessor path has already
   897  // checked this value for nil.
   898  //
   899  // Simple backwards flood from check to definition.
   900  // Run prog loop backward from end of program to beginning to avoid quadratic
   901  // behavior removing a run of checks.
   902  //
   903  // Assume that stack variables with address not taken can be loaded multiple times
   904  // from memory without being rechecked. Other variables need to be checked on
   905  // each load.
   906  
   907  var killed int // f.Data is either nil or &killed
   908  
   909  func nilopt(firstp *obj.Prog) {
   910  	g := Flowstart(firstp, nil)
   911  	if g == nil {
   912  		return
   913  	}
   914  
   915  	if Debug_checknil > 1 { // || strcmp(curfn->nname->sym->name, "f1") == 0
   916  		Dumpit("nilopt", g.Start, 0)
   917  	}
   918  
   919  	ncheck := 0
   920  	nkill := 0
   921  	var p *obj.Prog
   922  	for f := g.Start; f != nil; f = f.Link {
   923  		p = f.Prog
   924  		if p.As != obj.ACHECKNIL || !Thearch.Regtyp(&p.From) {
   925  			continue
   926  		}
   927  		ncheck++
   928  		if Thearch.Stackaddr(&p.From) {
   929  			if Debug_checknil != 0 && p.Lineno > 1 {
   930  				Warnl(p.Lineno, "removed nil check of SP address")
   931  			}
   932  			f.Data = &killed
   933  			continue
   934  		}
   935  
   936  		nilwalkfwd(f)
   937  		if f.Data != nil {
   938  			if Debug_checknil != 0 && p.Lineno > 1 {
   939  				Warnl(p.Lineno, "removed nil check before indirect")
   940  			}
   941  			continue
   942  		}
   943  
   944  		nilwalkback(f)
   945  		if f.Data != nil {
   946  			if Debug_checknil != 0 && p.Lineno > 1 {
   947  				Warnl(p.Lineno, "removed repeated nil check")
   948  			}
   949  			continue
   950  		}
   951  	}
   952  
   953  	for f := g.Start; f != nil; f = f.Link {
   954  		if f.Data != nil {
   955  			nkill++
   956  			Thearch.Excise(f)
   957  		}
   958  	}
   959  
   960  	Flowend(g)
   961  
   962  	if Debug_checknil > 1 {
   963  		fmt.Printf("%v: removed %d of %d nil checks\n", Curfn.Func.Nname.Sym, nkill, ncheck)
   964  	}
   965  }
   966  
   967  func nilwalkback(fcheck *Flow) {
   968  	for f := fcheck; f != nil; f = Uniqp(f) {
   969  		p := f.Prog
   970  		if (p.Info.Flags&RightWrite != 0) && Thearch.Sameaddr(&p.To, &fcheck.Prog.From) {
   971  			// Found initialization of value we're checking for nil.
   972  			// without first finding the check, so this one is unchecked.
   973  			return
   974  		}
   975  
   976  		if f != fcheck && p.As == obj.ACHECKNIL && Thearch.Sameaddr(&p.From, &fcheck.Prog.From) {
   977  			fcheck.Data = &killed
   978  			return
   979  		}
   980  	}
   981  }
   982  
   983  // Here is a more complex version that scans backward across branches.
   984  // It assumes fcheck->kill = 1 has been set on entry, and its job is to find a reason
   985  // to keep the check (setting fcheck->kill = 0).
   986  // It doesn't handle copying of aggregates as well as I would like,
   987  // nor variables with their address taken,
   988  // and it's too subtle to turn on this late in Go 1.2. Perhaps for Go 1.3.
   989  /*
   990  for(f1 = f0; f1 != nil; f1 = f1->p1) {
   991  	if(f1->active == gen)
   992  		break;
   993  	f1->active = gen;
   994  	p = f1->prog;
   995  
   996  	// If same check, stop this loop but still check
   997  	// alternate predecessors up to this point.
   998  	if(f1 != fcheck && p->as == ACHECKNIL && thearch.sameaddr(&p->from, &fcheck->prog->from))
   999  		break;
  1000  
  1001  	if((p.Info.flags & RightWrite) && thearch.sameaddr(&p->to, &fcheck->prog->from)) {
  1002  		// Found initialization of value we're checking for nil.
  1003  		// without first finding the check, so this one is unchecked.
  1004  		fcheck->kill = 0;
  1005  		return;
  1006  	}
  1007  
  1008  	if(f1->p1 == nil && f1->p2 == nil) {
  1009  		print("lost pred for %v\n", fcheck->prog);
  1010  		for(f1=f0; f1!=nil; f1=f1->p1) {
  1011  			thearch.proginfo(&info, f1->prog);
  1012  			print("\t%v %d %d %D %D\n", r1->prog, info.flags&RightWrite, thearch.sameaddr(&f1->prog->to, &fcheck->prog->from), &f1->prog->to, &fcheck->prog->from);
  1013  		}
  1014  		fatal("lost pred trail");
  1015  	}
  1016  }
  1017  
  1018  for(f = f0; f != f1; f = f->p1)
  1019  	for(f2 = f->p2; f2 != nil; f2 = f2->p2link)
  1020  		nilwalkback(fcheck, f2, gen);
  1021  */
  1022  
  1023  func nilwalkfwd(fcheck *Flow) {
  1024  	// If the path down from rcheck dereferences the address
  1025  	// (possibly with a small offset) before writing to memory
  1026  	// and before any subsequent checks, it's okay to wait for
  1027  	// that implicit check. Only consider this basic block to
  1028  	// avoid problems like:
  1029  	//	_ = *x // should panic
  1030  	//	for {} // no writes but infinite loop may be considered visible
  1031  
  1032  	var last *Flow
  1033  	for f := Uniqs(fcheck); f != nil; f = Uniqs(f) {
  1034  		p := f.Prog
  1035  		if (p.Info.Flags&LeftRead != 0) && Thearch.Smallindir(&p.From, &fcheck.Prog.From) {
  1036  			fcheck.Data = &killed
  1037  			return
  1038  		}
  1039  
  1040  		if (p.Info.Flags&(RightRead|RightWrite) != 0) && Thearch.Smallindir(&p.To, &fcheck.Prog.From) {
  1041  			fcheck.Data = &killed
  1042  			return
  1043  		}
  1044  
  1045  		// Stop if another nil check happens.
  1046  		if p.As == obj.ACHECKNIL {
  1047  			return
  1048  		}
  1049  
  1050  		// Stop if value is lost.
  1051  		if (p.Info.Flags&RightWrite != 0) && Thearch.Sameaddr(&p.To, &fcheck.Prog.From) {
  1052  			return
  1053  		}
  1054  
  1055  		// Stop if memory write.
  1056  		if (p.Info.Flags&RightWrite != 0) && !Thearch.Regtyp(&p.To) {
  1057  			return
  1058  		}
  1059  
  1060  		// Stop if we jump backward.
  1061  		if last != nil && f.Id <= last.Id {
  1062  			return
  1063  		}
  1064  		last = f
  1065  	}
  1066  }