github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/plive.go (about)

     1  // Copyright 2013 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  // Garbage collector liveness bitmap generation.
     6  
     7  // The command line flag -live causes this code to print debug information.
     8  // The levels are:
     9  //
    10  //	-live (aka -live=1): print liveness lists as code warnings at safe points
    11  //	-live=2: print an assembly listing with liveness annotations
    12  //
    13  // Each level includes the earlier output as well.
    14  
    15  package gc
    16  
    17  import (
    18  	"github.com/gagliardetto/golang-go/cmd/compile/internal/ssa"
    19  	"github.com/gagliardetto/golang-go/cmd/compile/internal/types"
    20  	"github.com/gagliardetto/golang-go/cmd/internal/obj"
    21  	"github.com/gagliardetto/golang-go/cmd/internal/objabi"
    22  	"crypto/md5"
    23  	"fmt"
    24  	"strings"
    25  )
    26  
    27  // OpVarDef is an annotation for the liveness analysis, marking a place
    28  // where a complete initialization (definition) of a variable begins.
    29  // Since the liveness analysis can see initialization of single-word
    30  // variables quite easy, OpVarDef is only needed for multi-word
    31  // variables satisfying isfat(n.Type). For simplicity though, buildssa
    32  // emits OpVarDef regardless of variable width.
    33  //
    34  // An 'OpVarDef x' annotation in the instruction stream tells the liveness
    35  // analysis to behave as though the variable x is being initialized at that
    36  // point in the instruction stream. The OpVarDef must appear before the
    37  // actual (multi-instruction) initialization, and it must also appear after
    38  // any uses of the previous value, if any. For example, if compiling:
    39  //
    40  //	x = x[1:]
    41  //
    42  // it is important to generate code like:
    43  //
    44  //	base, len, cap = pieces of x[1:]
    45  //	OpVarDef x
    46  //	x = {base, len, cap}
    47  //
    48  // If instead the generated code looked like:
    49  //
    50  //	OpVarDef x
    51  //	base, len, cap = pieces of x[1:]
    52  //	x = {base, len, cap}
    53  //
    54  // then the liveness analysis would decide the previous value of x was
    55  // unnecessary even though it is about to be used by the x[1:] computation.
    56  // Similarly, if the generated code looked like:
    57  //
    58  //	base, len, cap = pieces of x[1:]
    59  //	x = {base, len, cap}
    60  //	OpVarDef x
    61  //
    62  // then the liveness analysis will not preserve the new value of x, because
    63  // the OpVarDef appears to have "overwritten" it.
    64  //
    65  // OpVarDef is a bit of a kludge to work around the fact that the instruction
    66  // stream is working on single-word values but the liveness analysis
    67  // wants to work on individual variables, which might be multi-word
    68  // aggregates. It might make sense at some point to look into letting
    69  // the liveness analysis work on single-word values as well, although
    70  // there are complications around interface values, slices, and strings,
    71  // all of which cannot be treated as individual words.
    72  //
    73  // OpVarKill is the opposite of OpVarDef: it marks a value as no longer needed,
    74  // even if its address has been taken. That is, an OpVarKill annotation asserts
    75  // that its argument is certainly dead, for use when the liveness analysis
    76  // would not otherwise be able to deduce that fact.
    77  
    78  // TODO: get rid of OpVarKill here. It's useful for stack frame allocation
    79  // so the compiler can allocate two temps to the same location. Here it's now
    80  // useless, since the implementation of stack objects.
    81  
    82  // BlockEffects summarizes the liveness effects on an SSA block.
    83  type BlockEffects struct {
    84  	// Computed during Liveness.prologue using only the content of
    85  	// individual blocks:
    86  	//
    87  	//	uevar: upward exposed variables (used before set in block)
    88  	//	varkill: killed variables (set in block)
    89  	uevar   varRegVec
    90  	varkill varRegVec
    91  
    92  	// Computed during Liveness.solve using control flow information:
    93  	//
    94  	//	livein: variables live at block entry
    95  	//	liveout: variables live at block exit
    96  	livein  varRegVec
    97  	liveout varRegVec
    98  }
    99  
   100  // A collection of global state used by liveness analysis.
   101  type Liveness struct {
   102  	fn         *Node
   103  	f          *ssa.Func
   104  	vars       []*Node
   105  	idx        map[*Node]int32
   106  	stkptrsize int64
   107  
   108  	be []BlockEffects
   109  
   110  	// unsafePoints bit i is set if Value ID i is not a safe point.
   111  	unsafePoints bvec
   112  
   113  	// An array with a bit vector for each safe point in the
   114  	// current Block during Liveness.epilogue. Indexed in Value
   115  	// order for that block. Additionally, for the entry block
   116  	// livevars[0] is the entry bitmap. Liveness.compact moves
   117  	// these to stackMaps and regMaps.
   118  	livevars []varRegVec
   119  
   120  	// livenessMap maps from safe points (i.e., CALLs) to their
   121  	// liveness map indexes.
   122  	livenessMap LivenessMap
   123  	stackMapSet bvecSet
   124  	stackMaps   []bvec
   125  	regMapSet   map[liveRegMask]int
   126  	regMaps     []liveRegMask
   127  
   128  	cache progeffectscache
   129  
   130  	// These are only populated if open-coded defers are being used.
   131  	// List of vars/stack slots storing defer args
   132  	openDeferVars []openDeferVarInfo
   133  	// Map from defer arg OpVarDef to the block where the OpVarDef occurs.
   134  	openDeferVardefToBlockMap map[*Node]*ssa.Block
   135  	// Map of blocks that cannot reach a return or exit (panic)
   136  	nonReturnBlocks map[*ssa.Block]bool
   137  }
   138  
   139  type openDeferVarInfo struct {
   140  	n         *Node // Var/stack slot storing a defer arg
   141  	varsIndex int   // Index of variable in lv.vars
   142  }
   143  
   144  // LivenessMap maps from *ssa.Value to LivenessIndex.
   145  type LivenessMap struct {
   146  	m []LivenessIndex
   147  }
   148  
   149  func (m *LivenessMap) reset(ids int) {
   150  	m2 := m.m
   151  	if ids > cap(m2) {
   152  		m2 = make([]LivenessIndex, ids)
   153  	} else {
   154  		m2 = m2[:ids]
   155  	}
   156  	none := LivenessInvalid
   157  	for i := range m2 {
   158  		m2[i] = none
   159  	}
   160  	m.m = m2
   161  }
   162  
   163  func (m *LivenessMap) set(v *ssa.Value, i LivenessIndex) {
   164  	m.m[v.ID] = i
   165  }
   166  
   167  func (m LivenessMap) Get(v *ssa.Value) LivenessIndex {
   168  	if int(v.ID) < len(m.m) {
   169  		return m.m[int(v.ID)]
   170  	}
   171  	// Not a safe point.
   172  	return LivenessInvalid
   173  }
   174  
   175  // LivenessIndex stores the liveness map index for a safe-point.
   176  type LivenessIndex struct {
   177  	stackMapIndex int
   178  	regMapIndex   int
   179  }
   180  
   181  // LivenessInvalid indicates an unsafe point.
   182  //
   183  // We use index -2 because PCDATA tables conventionally start at -1,
   184  // so -1 is used to mean the entry liveness map (which is actually at
   185  // index 0; sigh). TODO(austin): Maybe we should use PCDATA+1 as the
   186  // index into the liveness map so -1 uniquely refers to the entry
   187  // liveness map.
   188  var LivenessInvalid = LivenessIndex{-2, -2}
   189  
   190  func (idx LivenessIndex) Valid() bool {
   191  	return idx.stackMapIndex >= 0
   192  }
   193  
   194  type progeffectscache struct {
   195  	retuevar    []int32
   196  	tailuevar   []int32
   197  	initialized bool
   198  }
   199  
   200  // varRegVec contains liveness bitmaps for variables and registers.
   201  type varRegVec struct {
   202  	vars bvec
   203  	regs liveRegMask
   204  }
   205  
   206  func (v *varRegVec) Eq(v2 varRegVec) bool {
   207  	return v.vars.Eq(v2.vars) && v.regs == v2.regs
   208  }
   209  
   210  func (v *varRegVec) Copy(v2 varRegVec) {
   211  	v.vars.Copy(v2.vars)
   212  	v.regs = v2.regs
   213  }
   214  
   215  func (v *varRegVec) Clear() {
   216  	v.vars.Clear()
   217  	v.regs = 0
   218  }
   219  
   220  func (v *varRegVec) Or(v1, v2 varRegVec) {
   221  	v.vars.Or(v1.vars, v2.vars)
   222  	v.regs = v1.regs | v2.regs
   223  }
   224  
   225  func (v *varRegVec) AndNot(v1, v2 varRegVec) {
   226  	v.vars.AndNot(v1.vars, v2.vars)
   227  	v.regs = v1.regs &^ v2.regs
   228  }
   229  
   230  // livenessShouldTrack reports whether the liveness analysis
   231  // should track the variable n.
   232  // We don't care about variables that have no pointers,
   233  // nor do we care about non-local variables,
   234  // nor do we care about empty structs (handled by the pointer check),
   235  // nor do we care about the fake PAUTOHEAP variables.
   236  func livenessShouldTrack(n *Node) bool {
   237  	return n.Op == ONAME && (n.Class() == PAUTO || n.Class() == PPARAM || n.Class() == PPARAMOUT) && types.Haspointers(n.Type)
   238  }
   239  
   240  // getvariables returns the list of on-stack variables that we need to track
   241  // and a map for looking up indices by *Node.
   242  func getvariables(fn *Node) ([]*Node, map[*Node]int32) {
   243  	var vars []*Node
   244  	for _, n := range fn.Func.Dcl {
   245  		if livenessShouldTrack(n) {
   246  			vars = append(vars, n)
   247  		}
   248  	}
   249  	idx := make(map[*Node]int32, len(vars))
   250  	for i, n := range vars {
   251  		idx[n] = int32(i)
   252  	}
   253  	return vars, idx
   254  }
   255  
   256  func (lv *Liveness) initcache() {
   257  	if lv.cache.initialized {
   258  		Fatalf("liveness cache initialized twice")
   259  		return
   260  	}
   261  	lv.cache.initialized = true
   262  
   263  	for i, node := range lv.vars {
   264  		switch node.Class() {
   265  		case PPARAM:
   266  			// A return instruction with a p.to is a tail return, which brings
   267  			// the stack pointer back up (if it ever went down) and then jumps
   268  			// to a new function entirely. That form of instruction must read
   269  			// all the parameters for correctness, and similarly it must not
   270  			// read the out arguments - they won't be set until the new
   271  			// function runs.
   272  			lv.cache.tailuevar = append(lv.cache.tailuevar, int32(i))
   273  
   274  		case PPARAMOUT:
   275  			// All results are live at every return point.
   276  			// Note that this point is after escaping return values
   277  			// are copied back to the stack using their PAUTOHEAP references.
   278  			lv.cache.retuevar = append(lv.cache.retuevar, int32(i))
   279  		}
   280  	}
   281  }
   282  
   283  // A liveEffect is a set of flags that describe an instruction's
   284  // liveness effects on a variable.
   285  //
   286  // The possible flags are:
   287  //	uevar - used by the instruction
   288  //	varkill - killed by the instruction (set)
   289  // A kill happens after the use (for an instruction that updates a value, for example).
   290  type liveEffect int
   291  
   292  const (
   293  	uevar liveEffect = 1 << iota
   294  	varkill
   295  )
   296  
   297  // valueEffects returns the index of a variable in lv.vars and the
   298  // liveness effects v has on that variable.
   299  // If v does not affect any tracked variables, it returns -1, 0.
   300  func (lv *Liveness) valueEffects(v *ssa.Value) (int32, liveEffect) {
   301  	n, e := affectedNode(v)
   302  	if e == 0 || n == nil || n.Op != ONAME { // cheapest checks first
   303  		return -1, 0
   304  	}
   305  
   306  	// AllocFrame has dropped unused variables from
   307  	// lv.fn.Func.Dcl, but they might still be referenced by
   308  	// OpVarFoo pseudo-ops. Ignore them to prevent "lost track of
   309  	// variable" ICEs (issue 19632).
   310  	switch v.Op {
   311  	case ssa.OpVarDef, ssa.OpVarKill, ssa.OpVarLive, ssa.OpKeepAlive:
   312  		if !n.Name.Used() {
   313  			return -1, 0
   314  		}
   315  	}
   316  
   317  	var effect liveEffect
   318  	// Read is a read, obviously.
   319  	//
   320  	// Addr is a read also, as any subsequent holder of the pointer must be able
   321  	// to see all the values (including initialization) written so far.
   322  	// This also prevents a variable from "coming back from the dead" and presenting
   323  	// stale pointers to the garbage collector. See issue 28445.
   324  	if e&(ssa.SymRead|ssa.SymAddr) != 0 {
   325  		effect |= uevar
   326  	}
   327  	if e&ssa.SymWrite != 0 && (!isfat(n.Type) || v.Op == ssa.OpVarDef) {
   328  		effect |= varkill
   329  	}
   330  
   331  	if effect == 0 {
   332  		return -1, 0
   333  	}
   334  
   335  	if pos, ok := lv.idx[n]; ok {
   336  		return pos, effect
   337  	}
   338  	return -1, 0
   339  }
   340  
   341  // affectedNode returns the *Node affected by v
   342  func affectedNode(v *ssa.Value) (*Node, ssa.SymEffect) {
   343  	// Special cases.
   344  	switch v.Op {
   345  	case ssa.OpLoadReg:
   346  		n, _ := AutoVar(v.Args[0])
   347  		return n, ssa.SymRead
   348  	case ssa.OpStoreReg:
   349  		n, _ := AutoVar(v)
   350  		return n, ssa.SymWrite
   351  
   352  	case ssa.OpVarLive:
   353  		return v.Aux.(*Node), ssa.SymRead
   354  	case ssa.OpVarDef, ssa.OpVarKill:
   355  		return v.Aux.(*Node), ssa.SymWrite
   356  	case ssa.OpKeepAlive:
   357  		n, _ := AutoVar(v.Args[0])
   358  		return n, ssa.SymRead
   359  	}
   360  
   361  	e := v.Op.SymEffect()
   362  	if e == 0 {
   363  		return nil, 0
   364  	}
   365  
   366  	switch a := v.Aux.(type) {
   367  	case nil, *obj.LSym:
   368  		// ok, but no node
   369  		return nil, e
   370  	case *Node:
   371  		return a, e
   372  	default:
   373  		Fatalf("weird aux: %s", v.LongString())
   374  		return nil, e
   375  	}
   376  }
   377  
   378  // regEffects returns the registers affected by v.
   379  func (lv *Liveness) regEffects(v *ssa.Value) (uevar, kill liveRegMask) {
   380  	if v.Op == ssa.OpPhi {
   381  		// All phi node arguments must come from the same
   382  		// register and the result must also go to that
   383  		// register, so there's no overall effect.
   384  		return 0, 0
   385  	}
   386  	addLocs := func(mask liveRegMask, v *ssa.Value, ptrOnly bool) liveRegMask {
   387  		if int(v.ID) >= len(lv.f.RegAlloc) {
   388  			// v has no allocated registers.
   389  			return mask
   390  		}
   391  		loc := lv.f.RegAlloc[v.ID]
   392  		if loc == nil {
   393  			// v has no allocated registers.
   394  			return mask
   395  		}
   396  		if v.Op == ssa.OpGetG {
   397  			// GetG represents the G register, which is a
   398  			// pointer, but not a valid GC register. The
   399  			// current G is always reachable, so it's okay
   400  			// to ignore this register.
   401  			return mask
   402  		}
   403  
   404  		// Collect registers and types from v's location.
   405  		var regs [2]*ssa.Register
   406  		nreg := 0
   407  		switch loc := loc.(type) {
   408  		case ssa.LocalSlot:
   409  			return mask
   410  		case *ssa.Register:
   411  			if ptrOnly && !v.Type.HasHeapPointer() {
   412  				return mask
   413  			}
   414  			regs[0] = loc
   415  			nreg = 1
   416  		case ssa.LocPair:
   417  			// The value will have TTUPLE type, and the
   418  			// children are nil or *ssa.Register.
   419  			if v.Type.Etype != types.TTUPLE {
   420  				v.Fatalf("location pair %s has non-tuple type %v", loc, v.Type)
   421  			}
   422  			for i, loc1 := range loc {
   423  				if loc1 == nil {
   424  					continue
   425  				}
   426  				if ptrOnly && !v.Type.FieldType(i).HasHeapPointer() {
   427  					continue
   428  				}
   429  				regs[nreg] = loc1.(*ssa.Register)
   430  				nreg++
   431  			}
   432  		default:
   433  			v.Fatalf("weird RegAlloc location: %s (%T)", loc, loc)
   434  		}
   435  
   436  		// Add register locations to vars.
   437  		for _, reg := range regs[:nreg] {
   438  			if reg.GCNum() == -1 {
   439  				if ptrOnly {
   440  					v.Fatalf("pointer in non-pointer register %v", reg)
   441  				} else {
   442  					continue
   443  				}
   444  			}
   445  			mask |= 1 << uint(reg.GCNum())
   446  		}
   447  		return mask
   448  	}
   449  
   450  	// v clobbers all registers it writes to (whether or not the
   451  	// write is pointer-typed).
   452  	kill = addLocs(0, v, false)
   453  	for _, arg := range v.Args {
   454  		// v uses all registers is reads from, but we only
   455  		// care about marking those containing pointers.
   456  		uevar = addLocs(uevar, arg, true)
   457  	}
   458  	return uevar, kill
   459  }
   460  
   461  type liveRegMask uint32
   462  
   463  func (m liveRegMask) niceString(config *ssa.Config) string {
   464  	if m == 0 {
   465  		return "<none>"
   466  	}
   467  	str := ""
   468  	for i, reg := range config.GCRegMap {
   469  		if m&(1<<uint(i)) != 0 {
   470  			if str != "" {
   471  				str += ","
   472  			}
   473  			str += reg.String()
   474  		}
   475  	}
   476  	return str
   477  }
   478  
   479  type livenessFuncCache struct {
   480  	be          []BlockEffects
   481  	livenessMap LivenessMap
   482  }
   483  
   484  // Constructs a new liveness structure used to hold the global state of the
   485  // liveness computation. The cfg argument is a slice of *BasicBlocks and the
   486  // vars argument is a slice of *Nodes.
   487  func newliveness(fn *Node, f *ssa.Func, vars []*Node, idx map[*Node]int32, stkptrsize int64) *Liveness {
   488  	lv := &Liveness{
   489  		fn:         fn,
   490  		f:          f,
   491  		vars:       vars,
   492  		idx:        idx,
   493  		stkptrsize: stkptrsize,
   494  
   495  		regMapSet: make(map[liveRegMask]int),
   496  	}
   497  
   498  	// Significant sources of allocation are kept in the ssa.Cache
   499  	// and reused. Surprisingly, the bit vectors themselves aren't
   500  	// a major source of allocation, but the slices are.
   501  	if lc, _ := f.Cache.Liveness.(*livenessFuncCache); lc == nil {
   502  		// Prep the cache so liveness can fill it later.
   503  		f.Cache.Liveness = new(livenessFuncCache)
   504  	} else {
   505  		if cap(lc.be) >= f.NumBlocks() {
   506  			lv.be = lc.be[:f.NumBlocks()]
   507  		}
   508  		lv.livenessMap = LivenessMap{lc.livenessMap.m[:0]}
   509  	}
   510  	if lv.be == nil {
   511  		lv.be = make([]BlockEffects, f.NumBlocks())
   512  	}
   513  
   514  	nblocks := int32(len(f.Blocks))
   515  	nvars := int32(len(vars))
   516  	bulk := bvbulkalloc(nvars, nblocks*7)
   517  	for _, b := range f.Blocks {
   518  		be := lv.blockEffects(b)
   519  
   520  		be.uevar = varRegVec{vars: bulk.next()}
   521  		be.varkill = varRegVec{vars: bulk.next()}
   522  		be.livein = varRegVec{vars: bulk.next()}
   523  		be.liveout = varRegVec{vars: bulk.next()}
   524  	}
   525  	lv.livenessMap.reset(lv.f.NumValues())
   526  
   527  	lv.markUnsafePoints()
   528  	return lv
   529  }
   530  
   531  func (lv *Liveness) blockEffects(b *ssa.Block) *BlockEffects {
   532  	return &lv.be[b.ID]
   533  }
   534  
   535  // NOTE: The bitmap for a specific type t could be cached in t after
   536  // the first run and then simply copied into bv at the correct offset
   537  // on future calls with the same type t.
   538  func onebitwalktype1(t *types.Type, off int64, bv bvec) {
   539  	if t.Align > 0 && off&int64(t.Align-1) != 0 {
   540  		Fatalf("onebitwalktype1: invalid initial alignment: type %v has alignment %d, but offset is %v", t, t.Align, off)
   541  	}
   542  
   543  	switch t.Etype {
   544  	case TINT8, TUINT8, TINT16, TUINT16,
   545  		TINT32, TUINT32, TINT64, TUINT64,
   546  		TINT, TUINT, TUINTPTR, TBOOL,
   547  		TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128:
   548  
   549  	case TPTR, TUNSAFEPTR, TFUNC, TCHAN, TMAP:
   550  		if off&int64(Widthptr-1) != 0 {
   551  			Fatalf("onebitwalktype1: invalid alignment, %v", t)
   552  		}
   553  		bv.Set(int32(off / int64(Widthptr))) // pointer
   554  
   555  	case TSTRING:
   556  		// struct { byte *str; intgo len; }
   557  		if off&int64(Widthptr-1) != 0 {
   558  			Fatalf("onebitwalktype1: invalid alignment, %v", t)
   559  		}
   560  		bv.Set(int32(off / int64(Widthptr))) //pointer in first slot
   561  
   562  	case TINTER:
   563  		// struct { Itab *tab;	void *data; }
   564  		// or, when isnilinter(t)==true:
   565  		// struct { Type *type; void *data; }
   566  		if off&int64(Widthptr-1) != 0 {
   567  			Fatalf("onebitwalktype1: invalid alignment, %v", t)
   568  		}
   569  		// The first word of an interface is a pointer, but we don't
   570  		// treat it as such.
   571  		// 1. If it is a non-empty interface, the pointer points to an itab
   572  		//    which is always in persistentalloc space.
   573  		// 2. If it is an empty interface, the pointer points to a _type.
   574  		//   a. If it is a compile-time-allocated type, it points into
   575  		//      the read-only data section.
   576  		//   b. If it is a reflect-allocated type, it points into the Go heap.
   577  		//      Reflect is responsible for keeping a reference to
   578  		//      the underlying type so it won't be GCd.
   579  		// If we ever have a moving GC, we need to change this for 2b (as
   580  		// well as scan itabs to update their itab._type fields).
   581  		bv.Set(int32(off/int64(Widthptr) + 1)) // pointer in second slot
   582  
   583  	case TSLICE:
   584  		// struct { byte *array; uintgo len; uintgo cap; }
   585  		if off&int64(Widthptr-1) != 0 {
   586  			Fatalf("onebitwalktype1: invalid TARRAY alignment, %v", t)
   587  		}
   588  		bv.Set(int32(off / int64(Widthptr))) // pointer in first slot (BitsPointer)
   589  
   590  	case TARRAY:
   591  		elt := t.Elem()
   592  		if elt.Width == 0 {
   593  			// Short-circuit for #20739.
   594  			break
   595  		}
   596  		for i := int64(0); i < t.NumElem(); i++ {
   597  			onebitwalktype1(elt, off, bv)
   598  			off += elt.Width
   599  		}
   600  
   601  	case TSTRUCT:
   602  		for _, f := range t.Fields().Slice() {
   603  			onebitwalktype1(f.Type, off+f.Offset, bv)
   604  		}
   605  
   606  	default:
   607  		Fatalf("onebitwalktype1: unexpected type, %v", t)
   608  	}
   609  }
   610  
   611  // usedRegs returns the maximum width of the live register map.
   612  func (lv *Liveness) usedRegs() int32 {
   613  	var any liveRegMask
   614  	for _, live := range lv.regMaps {
   615  		any |= live
   616  	}
   617  	i := int32(0)
   618  	for any != 0 {
   619  		any >>= 1
   620  		i++
   621  	}
   622  	return i
   623  }
   624  
   625  // Generates live pointer value maps for arguments and local variables. The
   626  // this argument and the in arguments are always assumed live. The vars
   627  // argument is a slice of *Nodes.
   628  func (lv *Liveness) pointerMap(liveout bvec, vars []*Node, args, locals bvec) {
   629  	for i := int32(0); ; i++ {
   630  		i = liveout.Next(i)
   631  		if i < 0 {
   632  			break
   633  		}
   634  		node := vars[i]
   635  		switch node.Class() {
   636  		case PAUTO:
   637  			onebitwalktype1(node.Type, node.Xoffset+lv.stkptrsize, locals)
   638  
   639  		case PPARAM, PPARAMOUT:
   640  			onebitwalktype1(node.Type, node.Xoffset, args)
   641  		}
   642  	}
   643  }
   644  
   645  // markUnsafePoints finds unsafe points and computes lv.unsafePoints.
   646  func (lv *Liveness) markUnsafePoints() {
   647  	if compiling_runtime || lv.f.NoSplit {
   648  		// No complex analysis necessary. Do this on the fly
   649  		// in issafepoint.
   650  		return
   651  	}
   652  
   653  	lv.unsafePoints = bvalloc(int32(lv.f.NumValues()))
   654  
   655  	// Mark architecture-specific unsafe points.
   656  	for _, b := range lv.f.Blocks {
   657  		for _, v := range b.Values {
   658  			if v.Op.UnsafePoint() {
   659  				lv.unsafePoints.Set(int32(v.ID))
   660  			}
   661  		}
   662  	}
   663  
   664  	// Mark write barrier unsafe points.
   665  	for _, wbBlock := range lv.f.WBLoads {
   666  		if wbBlock.Kind == ssa.BlockPlain && len(wbBlock.Values) == 0 {
   667  			// The write barrier block was optimized away
   668  			// but we haven't done dead block elimination.
   669  			// (This can happen in -N mode.)
   670  			continue
   671  		}
   672  		// Check that we have the expected diamond shape.
   673  		if len(wbBlock.Succs) != 2 {
   674  			lv.f.Fatalf("expected branch at write barrier block %v", wbBlock)
   675  		}
   676  		s0, s1 := wbBlock.Succs[0].Block(), wbBlock.Succs[1].Block()
   677  		if s0 == s1 {
   678  			// There's no difference between write barrier on and off.
   679  			// Thus there's no unsafe locations. See issue 26024.
   680  			continue
   681  		}
   682  		if s0.Kind != ssa.BlockPlain || s1.Kind != ssa.BlockPlain {
   683  			lv.f.Fatalf("expected successors of write barrier block %v to be plain", wbBlock)
   684  		}
   685  		if s0.Succs[0].Block() != s1.Succs[0].Block() {
   686  			lv.f.Fatalf("expected successors of write barrier block %v to converge", wbBlock)
   687  		}
   688  
   689  		// Flow backwards from the control value to find the
   690  		// flag load. We don't know what lowered ops we're
   691  		// looking for, but all current arches produce a
   692  		// single op that does the memory load from the flag
   693  		// address, so we look for that.
   694  		var load *ssa.Value
   695  		v := wbBlock.Controls[0]
   696  		for {
   697  			if sym, ok := v.Aux.(*obj.LSym); ok && sym == writeBarrier {
   698  				load = v
   699  				break
   700  			}
   701  			switch v.Op {
   702  			case ssa.Op386TESTL:
   703  				// 386 lowers Neq32 to (TESTL cond cond),
   704  				if v.Args[0] == v.Args[1] {
   705  					v = v.Args[0]
   706  					continue
   707  				}
   708  			case ssa.OpRISCV64SUB:
   709  				// RISCV64 lowers Neq32 to include a SUB with multiple arguments.
   710  				// TODO(jsing): it would be preferable not to use Neq32 for
   711  				// writeBuffer.enabled checks on this platform.
   712  				v = v.Args[0]
   713  				continue
   714  			case ssa.Op386MOVLload, ssa.OpARM64MOVWUload, ssa.OpPPC64MOVWZload, ssa.OpWasmI64Load32U:
   715  				// Args[0] is the address of the write
   716  				// barrier control. Ignore Args[1],
   717  				// which is the mem operand.
   718  				// TODO: Just ignore mem operands?
   719  				v = v.Args[0]
   720  				continue
   721  			}
   722  			// Common case: just flow backwards.
   723  			if len(v.Args) != 1 {
   724  				v.Fatalf("write barrier control value has more than one argument: %s", v.LongString())
   725  			}
   726  			v = v.Args[0]
   727  		}
   728  
   729  		// Mark everything after the load unsafe.
   730  		found := false
   731  		for _, v := range wbBlock.Values {
   732  			found = found || v == load
   733  			if found {
   734  				lv.unsafePoints.Set(int32(v.ID))
   735  			}
   736  		}
   737  
   738  		// Mark the two successor blocks unsafe. These come
   739  		// back together immediately after the direct write in
   740  		// one successor and the last write barrier call in
   741  		// the other, so there's no need to be more precise.
   742  		for _, succ := range wbBlock.Succs {
   743  			for _, v := range succ.Block().Values {
   744  				lv.unsafePoints.Set(int32(v.ID))
   745  			}
   746  		}
   747  	}
   748  
   749  	// Find uintptr -> unsafe.Pointer conversions and flood
   750  	// unsafeness back to a call (which is always a safe point).
   751  	//
   752  	// Looking for the uintptr -> unsafe.Pointer conversion has a
   753  	// few advantages over looking for unsafe.Pointer -> uintptr
   754  	// conversions:
   755  	//
   756  	// 1. We avoid needlessly blocking safe-points for
   757  	// unsafe.Pointer -> uintptr conversions that never go back to
   758  	// a Pointer.
   759  	//
   760  	// 2. We don't have to detect calls to reflect.Value.Pointer,
   761  	// reflect.Value.UnsafeAddr, and reflect.Value.InterfaceData,
   762  	// which are implicit unsafe.Pointer -> uintptr conversions.
   763  	// We can't even reliably detect this if there's an indirect
   764  	// call to one of these methods.
   765  	//
   766  	// TODO: For trivial unsafe.Pointer arithmetic, it would be
   767  	// nice to only flood as far as the unsafe.Pointer -> uintptr
   768  	// conversion, but it's hard to know which argument of an Add
   769  	// or Sub to follow.
   770  	var flooded bvec
   771  	var flood func(b *ssa.Block, vi int)
   772  	flood = func(b *ssa.Block, vi int) {
   773  		if flooded.n == 0 {
   774  			flooded = bvalloc(int32(lv.f.NumBlocks()))
   775  		}
   776  		if flooded.Get(int32(b.ID)) {
   777  			return
   778  		}
   779  		for i := vi - 1; i >= 0; i-- {
   780  			v := b.Values[i]
   781  			if v.Op.IsCall() {
   782  				// Uintptrs must not contain live
   783  				// pointers across calls, so stop
   784  				// flooding.
   785  				return
   786  			}
   787  			lv.unsafePoints.Set(int32(v.ID))
   788  		}
   789  		if vi == len(b.Values) {
   790  			// We marked all values in this block, so no
   791  			// need to flood this block again.
   792  			flooded.Set(int32(b.ID))
   793  		}
   794  		for _, pred := range b.Preds {
   795  			flood(pred.Block(), len(pred.Block().Values))
   796  		}
   797  	}
   798  	for _, b := range lv.f.Blocks {
   799  		for i, v := range b.Values {
   800  			if !(v.Op == ssa.OpConvert && v.Type.IsPtrShaped()) {
   801  				continue
   802  			}
   803  			// Flood the unsafe-ness of this backwards
   804  			// until we hit a call.
   805  			flood(b, i+1)
   806  		}
   807  	}
   808  }
   809  
   810  // Returns true for instructions that are safe points that must be annotated
   811  // with liveness information.
   812  func (lv *Liveness) issafepoint(v *ssa.Value) bool {
   813  	// The runtime was written with the assumption that
   814  	// safe-points only appear at call sites (because that's how
   815  	// it used to be). We could and should improve that, but for
   816  	// now keep the old safe-point rules in the runtime.
   817  	//
   818  	// go:nosplit functions are similar. Since safe points used to
   819  	// be coupled with stack checks, go:nosplit often actually
   820  	// means "no safe points in this function".
   821  	if compiling_runtime || lv.f.NoSplit {
   822  		return v.Op.IsCall()
   823  	}
   824  	switch v.Op {
   825  	case ssa.OpInitMem, ssa.OpArg, ssa.OpSP, ssa.OpSB,
   826  		ssa.OpSelect0, ssa.OpSelect1, ssa.OpGetG,
   827  		ssa.OpVarDef, ssa.OpVarLive, ssa.OpKeepAlive,
   828  		ssa.OpPhi:
   829  		// These don't produce code (see genssa).
   830  		return false
   831  	}
   832  	return !lv.unsafePoints.Get(int32(v.ID))
   833  }
   834  
   835  // Initializes the sets for solving the live variables. Visits all the
   836  // instructions in each basic block to summarizes the information at each basic
   837  // block
   838  func (lv *Liveness) prologue() {
   839  	lv.initcache()
   840  
   841  	if lv.fn.Func.HasDefer() && !lv.fn.Func.OpenCodedDeferDisallowed() {
   842  		lv.openDeferVardefToBlockMap = make(map[*Node]*ssa.Block)
   843  		for i, n := range lv.vars {
   844  			if n.Name.OpenDeferSlot() {
   845  				lv.openDeferVars = append(lv.openDeferVars, openDeferVarInfo{n: n, varsIndex: i})
   846  			}
   847  		}
   848  
   849  		// Find any blocks that cannot reach a return or a BlockExit
   850  		// (panic) -- these must be because of an infinite loop.
   851  		reachesRet := make(map[ssa.ID]bool)
   852  		blockList := make([]*ssa.Block, 0, 256)
   853  
   854  		for _, b := range lv.f.Blocks {
   855  			if b.Kind == ssa.BlockRet || b.Kind == ssa.BlockRetJmp || b.Kind == ssa.BlockExit {
   856  				blockList = append(blockList, b)
   857  			}
   858  		}
   859  
   860  		for len(blockList) > 0 {
   861  			b := blockList[0]
   862  			blockList = blockList[1:]
   863  			if reachesRet[b.ID] {
   864  				continue
   865  			}
   866  			reachesRet[b.ID] = true
   867  			for _, e := range b.Preds {
   868  				blockList = append(blockList, e.Block())
   869  			}
   870  		}
   871  
   872  		lv.nonReturnBlocks = make(map[*ssa.Block]bool)
   873  		for _, b := range lv.f.Blocks {
   874  			if !reachesRet[b.ID] {
   875  				lv.nonReturnBlocks[b] = true
   876  				//fmt.Println("No reach ret", lv.f.Name, b.ID, b.Kind)
   877  			}
   878  		}
   879  	}
   880  
   881  	for _, b := range lv.f.Blocks {
   882  		be := lv.blockEffects(b)
   883  
   884  		// Walk the block instructions backward and update the block
   885  		// effects with the each prog effects.
   886  		for j := len(b.Values) - 1; j >= 0; j-- {
   887  			if b.Values[j].Op == ssa.OpVarDef {
   888  				n := b.Values[j].Aux.(*Node)
   889  				if n.Name.OpenDeferSlot() {
   890  					lv.openDeferVardefToBlockMap[n] = b
   891  				}
   892  			}
   893  			pos, e := lv.valueEffects(b.Values[j])
   894  			regUevar, regKill := lv.regEffects(b.Values[j])
   895  			if e&varkill != 0 {
   896  				be.varkill.vars.Set(pos)
   897  				be.uevar.vars.Unset(pos)
   898  			}
   899  			be.varkill.regs |= regKill
   900  			be.uevar.regs &^= regKill
   901  			if e&uevar != 0 {
   902  				be.uevar.vars.Set(pos)
   903  			}
   904  			be.uevar.regs |= regUevar
   905  		}
   906  	}
   907  }
   908  
   909  // markDeferVarsLive marks each variable storing an open-coded defer arg as
   910  // specially live in block b if the variable definition dominates block b.
   911  func (lv *Liveness) markDeferVarsLive(b *ssa.Block, newliveout *varRegVec) {
   912  	// Only force computation of dominators if we have a block where we need
   913  	// to specially mark defer args live.
   914  	sdom := lv.f.Sdom()
   915  	for _, info := range lv.openDeferVars {
   916  		defB := lv.openDeferVardefToBlockMap[info.n]
   917  		if sdom.IsAncestorEq(defB, b) {
   918  			newliveout.vars.Set(int32(info.varsIndex))
   919  		}
   920  	}
   921  }
   922  
   923  // Solve the liveness dataflow equations.
   924  func (lv *Liveness) solve() {
   925  	// These temporary bitvectors exist to avoid successive allocations and
   926  	// frees within the loop.
   927  	nvars := int32(len(lv.vars))
   928  	newlivein := varRegVec{vars: bvalloc(nvars)}
   929  	newliveout := varRegVec{vars: bvalloc(nvars)}
   930  
   931  	// Walk blocks in postorder ordering. This improves convergence.
   932  	po := lv.f.Postorder()
   933  
   934  	// Iterate through the blocks in reverse round-robin fashion. A work
   935  	// queue might be slightly faster. As is, the number of iterations is
   936  	// so low that it hardly seems to be worth the complexity.
   937  
   938  	for change := true; change; {
   939  		change = false
   940  		for _, b := range po {
   941  			be := lv.blockEffects(b)
   942  
   943  			newliveout.Clear()
   944  			switch b.Kind {
   945  			case ssa.BlockRet:
   946  				for _, pos := range lv.cache.retuevar {
   947  					newliveout.vars.Set(pos)
   948  				}
   949  			case ssa.BlockRetJmp:
   950  				for _, pos := range lv.cache.tailuevar {
   951  					newliveout.vars.Set(pos)
   952  				}
   953  			case ssa.BlockExit:
   954  				// panic exit - nothing to do
   955  			default:
   956  				// A variable is live on output from this block
   957  				// if it is live on input to some successor.
   958  				//
   959  				// out[b] = \bigcup_{s \in succ[b]} in[s]
   960  				newliveout.Copy(lv.blockEffects(b.Succs[0].Block()).livein)
   961  				for _, succ := range b.Succs[1:] {
   962  					newliveout.Or(newliveout, lv.blockEffects(succ.Block()).livein)
   963  				}
   964  			}
   965  
   966  			if lv.fn.Func.HasDefer() && !lv.fn.Func.OpenCodedDeferDisallowed() &&
   967  				(b.Kind == ssa.BlockExit || lv.nonReturnBlocks[b]) {
   968  				// Open-coded defer args slots must be live
   969  				// everywhere in a function, since a panic can
   970  				// occur (almost) anywhere. Force all appropriate
   971  				// defer arg slots to be live in BlockExit (panic)
   972  				// blocks and in blocks that do not reach a return
   973  				// (because of infinite loop).
   974  				//
   975  				// We are assuming that the defer exit code at
   976  				// BlockReturn/BlockReturnJmp accesses all of the
   977  				// defer args (with pointers), and so keeps them
   978  				// live. This analysis may have to be adjusted if
   979  				// that changes (because of optimizations).
   980  				lv.markDeferVarsLive(b, &newliveout)
   981  			}
   982  
   983  			if !be.liveout.Eq(newliveout) {
   984  				change = true
   985  				be.liveout.Copy(newliveout)
   986  			}
   987  
   988  			// A variable is live on input to this block
   989  			// if it is used by this block, or live on output from this block and
   990  			// not set by the code in this block.
   991  			//
   992  			// in[b] = uevar[b] \cup (out[b] \setminus varkill[b])
   993  			newlivein.AndNot(be.liveout, be.varkill)
   994  			be.livein.Or(newlivein, be.uevar)
   995  		}
   996  	}
   997  }
   998  
   999  // Visits all instructions in a basic block and computes a bit vector of live
  1000  // variables at each safe point locations.
  1001  func (lv *Liveness) epilogue() {
  1002  	nvars := int32(len(lv.vars))
  1003  	liveout := varRegVec{vars: bvalloc(nvars)}
  1004  	livedefer := bvalloc(nvars) // always-live variables
  1005  
  1006  	// If there is a defer (that could recover), then all output
  1007  	// parameters are live all the time.  In addition, any locals
  1008  	// that are pointers to heap-allocated output parameters are
  1009  	// also always live (post-deferreturn code needs these
  1010  	// pointers to copy values back to the stack).
  1011  	// TODO: if the output parameter is heap-allocated, then we
  1012  	// don't need to keep the stack copy live?
  1013  	if lv.fn.Func.HasDefer() {
  1014  		for i, n := range lv.vars {
  1015  			if n.Class() == PPARAMOUT {
  1016  				if n.Name.IsOutputParamHeapAddr() {
  1017  					// Just to be paranoid.  Heap addresses are PAUTOs.
  1018  					Fatalf("variable %v both output param and heap output param", n)
  1019  				}
  1020  				if n.Name.Param.Heapaddr != nil {
  1021  					// If this variable moved to the heap, then
  1022  					// its stack copy is not live.
  1023  					continue
  1024  				}
  1025  				// Note: zeroing is handled by zeroResults in walk.go.
  1026  				livedefer.Set(int32(i))
  1027  			}
  1028  			if n.Name.IsOutputParamHeapAddr() {
  1029  				// This variable will be overwritten early in the function
  1030  				// prologue (from the result of a mallocgc) but we need to
  1031  				// zero it in case that malloc causes a stack scan.
  1032  				n.Name.SetNeedzero(true)
  1033  				livedefer.Set(int32(i))
  1034  			}
  1035  		}
  1036  	}
  1037  
  1038  	// We must analyze the entry block first. The runtime assumes
  1039  	// the function entry map is index 0. Conveniently, layout
  1040  	// already ensured that the entry block is first.
  1041  	if lv.f.Entry != lv.f.Blocks[0] {
  1042  		lv.f.Fatalf("entry block must be first")
  1043  	}
  1044  
  1045  	{
  1046  		// Reserve an entry for function entry.
  1047  		live := bvalloc(nvars)
  1048  		lv.livevars = append(lv.livevars, varRegVec{vars: live})
  1049  	}
  1050  
  1051  	for _, b := range lv.f.Blocks {
  1052  		be := lv.blockEffects(b)
  1053  		firstBitmapIndex := len(lv.livevars)
  1054  
  1055  		// Walk forward through the basic block instructions and
  1056  		// allocate liveness maps for those instructions that need them.
  1057  		for _, v := range b.Values {
  1058  			if !lv.issafepoint(v) {
  1059  				continue
  1060  			}
  1061  
  1062  			live := bvalloc(nvars)
  1063  			lv.livevars = append(lv.livevars, varRegVec{vars: live})
  1064  		}
  1065  
  1066  		// walk backward, construct maps at each safe point
  1067  		index := int32(len(lv.livevars) - 1)
  1068  
  1069  		liveout.Copy(be.liveout)
  1070  		for i := len(b.Values) - 1; i >= 0; i-- {
  1071  			v := b.Values[i]
  1072  
  1073  			if lv.issafepoint(v) {
  1074  				// Found an interesting instruction, record the
  1075  				// corresponding liveness information.
  1076  
  1077  				live := &lv.livevars[index]
  1078  				live.Or(*live, liveout)
  1079  				live.vars.Or(live.vars, livedefer) // only for non-entry safe points
  1080  				index--
  1081  			}
  1082  
  1083  			// Update liveness information.
  1084  			pos, e := lv.valueEffects(v)
  1085  			regUevar, regKill := lv.regEffects(v)
  1086  			if e&varkill != 0 {
  1087  				liveout.vars.Unset(pos)
  1088  			}
  1089  			liveout.regs &^= regKill
  1090  			if e&uevar != 0 {
  1091  				liveout.vars.Set(pos)
  1092  			}
  1093  			liveout.regs |= regUevar
  1094  		}
  1095  
  1096  		if b == lv.f.Entry {
  1097  			if index != 0 {
  1098  				Fatalf("bad index for entry point: %v", index)
  1099  			}
  1100  
  1101  			// Check to make sure only input variables are live.
  1102  			for i, n := range lv.vars {
  1103  				if !liveout.vars.Get(int32(i)) {
  1104  					continue
  1105  				}
  1106  				if n.Class() == PPARAM {
  1107  					continue // ok
  1108  				}
  1109  				Fatalf("bad live variable at entry of %v: %L", lv.fn.Func.Nname, n)
  1110  			}
  1111  
  1112  			// Record live variables.
  1113  			live := &lv.livevars[index]
  1114  			live.Or(*live, liveout)
  1115  		}
  1116  
  1117  		// Check that no registers are live across calls.
  1118  		// For closure calls, the CALLclosure is the last use
  1119  		// of the context register, so it's dead after the call.
  1120  		index = int32(firstBitmapIndex)
  1121  		for _, v := range b.Values {
  1122  			if lv.issafepoint(v) {
  1123  				live := lv.livevars[index]
  1124  				if v.Op.IsCall() && live.regs != 0 {
  1125  					lv.printDebug()
  1126  					v.Fatalf("%v register %s recorded as live at call", lv.fn.Func.Nname, live.regs.niceString(lv.f.Config))
  1127  				}
  1128  				index++
  1129  			}
  1130  		}
  1131  
  1132  		// The liveness maps for this block are now complete. Compact them.
  1133  		lv.compact(b)
  1134  	}
  1135  
  1136  	// Done compacting. Throw out the stack map set.
  1137  	lv.stackMaps = lv.stackMapSet.extractUniqe()
  1138  	lv.stackMapSet = bvecSet{}
  1139  
  1140  	// Useful sanity check: on entry to the function,
  1141  	// the only things that can possibly be live are the
  1142  	// input parameters.
  1143  	for j, n := range lv.vars {
  1144  		if n.Class() != PPARAM && lv.stackMaps[0].Get(int32(j)) {
  1145  			lv.f.Fatalf("%v %L recorded as live on entry", lv.fn.Func.Nname, n)
  1146  		}
  1147  	}
  1148  	// Check that no registers are live at function entry.
  1149  	// The context register, if any, comes from a
  1150  	// LoweredGetClosurePtr operation first thing in the function,
  1151  	// so it doesn't appear live at entry.
  1152  	if regs := lv.regMaps[0]; regs != 0 {
  1153  		lv.printDebug()
  1154  		lv.f.Fatalf("%v register %s recorded as live on entry", lv.fn.Func.Nname, regs.niceString(lv.f.Config))
  1155  	}
  1156  }
  1157  
  1158  // Compact coalesces identical bitmaps from lv.livevars into the sets
  1159  // lv.stackMapSet and lv.regMaps.
  1160  //
  1161  // Compact clears lv.livevars.
  1162  //
  1163  // There are actually two lists of bitmaps, one list for the local variables and one
  1164  // list for the function arguments. Both lists are indexed by the same PCDATA
  1165  // index, so the corresponding pairs must be considered together when
  1166  // merging duplicates. The argument bitmaps change much less often during
  1167  // function execution than the local variable bitmaps, so it is possible that
  1168  // we could introduce a separate PCDATA index for arguments vs locals and
  1169  // then compact the set of argument bitmaps separately from the set of
  1170  // local variable bitmaps. As of 2014-04-02, doing this to the godoc binary
  1171  // is actually a net loss: we save about 50k of argument bitmaps but the new
  1172  // PCDATA tables cost about 100k. So for now we keep using a single index for
  1173  // both bitmap lists.
  1174  func (lv *Liveness) compact(b *ssa.Block) {
  1175  	add := func(live varRegVec) LivenessIndex {
  1176  		// Deduplicate the stack map.
  1177  		stackIndex := lv.stackMapSet.add(live.vars)
  1178  		// Deduplicate the register map.
  1179  		regIndex, ok := lv.regMapSet[live.regs]
  1180  		if !ok {
  1181  			regIndex = len(lv.regMapSet)
  1182  			lv.regMapSet[live.regs] = regIndex
  1183  			lv.regMaps = append(lv.regMaps, live.regs)
  1184  		}
  1185  		return LivenessIndex{stackIndex, regIndex}
  1186  	}
  1187  	pos := 0
  1188  	if b == lv.f.Entry {
  1189  		// Handle entry stack map.
  1190  		add(lv.livevars[0])
  1191  		pos++
  1192  	}
  1193  	for _, v := range b.Values {
  1194  		if lv.issafepoint(v) {
  1195  			lv.livenessMap.set(v, add(lv.livevars[pos]))
  1196  			pos++
  1197  		}
  1198  	}
  1199  
  1200  	// Reset livevars.
  1201  	lv.livevars = lv.livevars[:0]
  1202  }
  1203  
  1204  func (lv *Liveness) showlive(v *ssa.Value, live bvec) {
  1205  	if debuglive == 0 || lv.fn.funcname() == "init" || strings.HasPrefix(lv.fn.funcname(), ".") {
  1206  		return
  1207  	}
  1208  	if !(v == nil || v.Op.IsCall()) {
  1209  		// Historically we only printed this information at
  1210  		// calls. Keep doing so.
  1211  		return
  1212  	}
  1213  	if live.IsEmpty() {
  1214  		return
  1215  	}
  1216  
  1217  	pos := lv.fn.Func.Nname.Pos
  1218  	if v != nil {
  1219  		pos = v.Pos
  1220  	}
  1221  
  1222  	s := "live at "
  1223  	if v == nil {
  1224  		s += fmt.Sprintf("entry to %s:", lv.fn.funcname())
  1225  	} else if sym, ok := v.Aux.(*obj.LSym); ok {
  1226  		fn := sym.Name
  1227  		if pos := strings.Index(fn, "."); pos >= 0 {
  1228  			fn = fn[pos+1:]
  1229  		}
  1230  		s += fmt.Sprintf("call to %s:", fn)
  1231  	} else {
  1232  		s += "indirect call:"
  1233  	}
  1234  
  1235  	for j, n := range lv.vars {
  1236  		if live.Get(int32(j)) {
  1237  			s += fmt.Sprintf(" %v", n)
  1238  		}
  1239  	}
  1240  
  1241  	Warnl(pos, s)
  1242  }
  1243  
  1244  func (lv *Liveness) printbvec(printed bool, name string, live varRegVec) bool {
  1245  	if live.vars.IsEmpty() && live.regs == 0 {
  1246  		return printed
  1247  	}
  1248  
  1249  	if !printed {
  1250  		fmt.Printf("\t")
  1251  	} else {
  1252  		fmt.Printf(" ")
  1253  	}
  1254  	fmt.Printf("%s=", name)
  1255  
  1256  	comma := ""
  1257  	for i, n := range lv.vars {
  1258  		if !live.vars.Get(int32(i)) {
  1259  			continue
  1260  		}
  1261  		fmt.Printf("%s%s", comma, n.Sym.Name)
  1262  		comma = ","
  1263  	}
  1264  	fmt.Printf("%s%s", comma, live.regs.niceString(lv.f.Config))
  1265  	return true
  1266  }
  1267  
  1268  // printeffect is like printbvec, but for valueEffects and regEffects.
  1269  func (lv *Liveness) printeffect(printed bool, name string, pos int32, x bool, regMask liveRegMask) bool {
  1270  	if !x && regMask == 0 {
  1271  		return printed
  1272  	}
  1273  	if !printed {
  1274  		fmt.Printf("\t")
  1275  	} else {
  1276  		fmt.Printf(" ")
  1277  	}
  1278  	fmt.Printf("%s=", name)
  1279  	if x {
  1280  		fmt.Printf("%s", lv.vars[pos].Sym.Name)
  1281  	}
  1282  	for j, reg := range lv.f.Config.GCRegMap {
  1283  		if regMask&(1<<uint(j)) != 0 {
  1284  			if x {
  1285  				fmt.Printf(",")
  1286  			}
  1287  			x = true
  1288  			fmt.Printf("%v", reg)
  1289  		}
  1290  	}
  1291  	return true
  1292  }
  1293  
  1294  // Prints the computed liveness information and inputs, for debugging.
  1295  // This format synthesizes the information used during the multiple passes
  1296  // into a single presentation.
  1297  func (lv *Liveness) printDebug() {
  1298  	fmt.Printf("liveness: %s\n", lv.fn.funcname())
  1299  
  1300  	pcdata := 0
  1301  	for i, b := range lv.f.Blocks {
  1302  		if i > 0 {
  1303  			fmt.Printf("\n")
  1304  		}
  1305  
  1306  		// bb#0 pred=1,2 succ=3,4
  1307  		fmt.Printf("bb#%d pred=", b.ID)
  1308  		for j, pred := range b.Preds {
  1309  			if j > 0 {
  1310  				fmt.Printf(",")
  1311  			}
  1312  			fmt.Printf("%d", pred.Block().ID)
  1313  		}
  1314  		fmt.Printf(" succ=")
  1315  		for j, succ := range b.Succs {
  1316  			if j > 0 {
  1317  				fmt.Printf(",")
  1318  			}
  1319  			fmt.Printf("%d", succ.Block().ID)
  1320  		}
  1321  		fmt.Printf("\n")
  1322  
  1323  		be := lv.blockEffects(b)
  1324  
  1325  		// initial settings
  1326  		printed := false
  1327  		printed = lv.printbvec(printed, "uevar", be.uevar)
  1328  		printed = lv.printbvec(printed, "livein", be.livein)
  1329  		if printed {
  1330  			fmt.Printf("\n")
  1331  		}
  1332  
  1333  		// program listing, with individual effects listed
  1334  
  1335  		if b == lv.f.Entry {
  1336  			live := lv.stackMaps[pcdata]
  1337  			fmt.Printf("(%s) function entry\n", linestr(lv.fn.Func.Nname.Pos))
  1338  			fmt.Printf("\tlive=")
  1339  			printed = false
  1340  			for j, n := range lv.vars {
  1341  				if !live.Get(int32(j)) {
  1342  					continue
  1343  				}
  1344  				if printed {
  1345  					fmt.Printf(",")
  1346  				}
  1347  				fmt.Printf("%v", n)
  1348  				printed = true
  1349  			}
  1350  			fmt.Printf("\n")
  1351  		}
  1352  
  1353  		for _, v := range b.Values {
  1354  			fmt.Printf("(%s) %v\n", linestr(v.Pos), v.LongString())
  1355  
  1356  			if pos := lv.livenessMap.Get(v); pos.Valid() {
  1357  				pcdata = pos.stackMapIndex
  1358  			}
  1359  
  1360  			pos, effect := lv.valueEffects(v)
  1361  			regUevar, regKill := lv.regEffects(v)
  1362  			printed = false
  1363  			printed = lv.printeffect(printed, "uevar", pos, effect&uevar != 0, regUevar)
  1364  			printed = lv.printeffect(printed, "varkill", pos, effect&varkill != 0, regKill)
  1365  			if printed {
  1366  				fmt.Printf("\n")
  1367  			}
  1368  
  1369  			if !lv.issafepoint(v) {
  1370  				continue
  1371  			}
  1372  
  1373  			live := lv.stackMaps[pcdata]
  1374  			fmt.Printf("\tlive=")
  1375  			printed = false
  1376  			for j, n := range lv.vars {
  1377  				if !live.Get(int32(j)) {
  1378  					continue
  1379  				}
  1380  				if printed {
  1381  					fmt.Printf(",")
  1382  				}
  1383  				fmt.Printf("%v", n)
  1384  				printed = true
  1385  			}
  1386  			regLive := lv.regMaps[lv.livenessMap.Get(v).regMapIndex]
  1387  			if regLive != 0 {
  1388  				if printed {
  1389  					fmt.Printf(",")
  1390  				}
  1391  				fmt.Printf("%s", regLive.niceString(lv.f.Config))
  1392  			}
  1393  			fmt.Printf("\n")
  1394  		}
  1395  
  1396  		// bb bitsets
  1397  		fmt.Printf("end\n")
  1398  		printed = false
  1399  		printed = lv.printbvec(printed, "varkill", be.varkill)
  1400  		printed = lv.printbvec(printed, "liveout", be.liveout)
  1401  		if printed {
  1402  			fmt.Printf("\n")
  1403  		}
  1404  	}
  1405  
  1406  	fmt.Printf("\n")
  1407  }
  1408  
  1409  // Dumps a slice of bitmaps to a symbol as a sequence of uint32 values. The
  1410  // first word dumped is the total number of bitmaps. The second word is the
  1411  // length of the bitmaps. All bitmaps are assumed to be of equal length. The
  1412  // remaining bytes are the raw bitmaps.
  1413  func (lv *Liveness) emit() (argsSym, liveSym, regsSym *obj.LSym) {
  1414  	// Size args bitmaps to be just large enough to hold the largest pointer.
  1415  	// First, find the largest Xoffset node we care about.
  1416  	// (Nodes without pointers aren't in lv.vars; see livenessShouldTrack.)
  1417  	var maxArgNode *Node
  1418  	for _, n := range lv.vars {
  1419  		switch n.Class() {
  1420  		case PPARAM, PPARAMOUT:
  1421  			if maxArgNode == nil || n.Xoffset > maxArgNode.Xoffset {
  1422  				maxArgNode = n
  1423  			}
  1424  		}
  1425  	}
  1426  	// Next, find the offset of the largest pointer in the largest node.
  1427  	var maxArgs int64
  1428  	if maxArgNode != nil {
  1429  		maxArgs = maxArgNode.Xoffset + typeptrdata(maxArgNode.Type)
  1430  	}
  1431  
  1432  	// Size locals bitmaps to be stkptrsize sized.
  1433  	// We cannot shrink them to only hold the largest pointer,
  1434  	// because their size is used to calculate the beginning
  1435  	// of the local variables frame.
  1436  	// Further discussion in https://golang.org/cl/104175.
  1437  	// TODO: consider trimming leading zeros.
  1438  	// This would require shifting all bitmaps.
  1439  	maxLocals := lv.stkptrsize
  1440  
  1441  	// Temporary symbols for encoding bitmaps.
  1442  	var argsSymTmp, liveSymTmp, regsSymTmp obj.LSym
  1443  
  1444  	args := bvalloc(int32(maxArgs / int64(Widthptr)))
  1445  	aoff := duint32(&argsSymTmp, 0, uint32(len(lv.stackMaps))) // number of bitmaps
  1446  	aoff = duint32(&argsSymTmp, aoff, uint32(args.n))          // number of bits in each bitmap
  1447  
  1448  	locals := bvalloc(int32(maxLocals / int64(Widthptr)))
  1449  	loff := duint32(&liveSymTmp, 0, uint32(len(lv.stackMaps))) // number of bitmaps
  1450  	loff = duint32(&liveSymTmp, loff, uint32(locals.n))        // number of bits in each bitmap
  1451  
  1452  	for _, live := range lv.stackMaps {
  1453  		args.Clear()
  1454  		locals.Clear()
  1455  
  1456  		lv.pointerMap(live, lv.vars, args, locals)
  1457  
  1458  		aoff = dbvec(&argsSymTmp, aoff, args)
  1459  		loff = dbvec(&liveSymTmp, loff, locals)
  1460  	}
  1461  
  1462  	regs := bvalloc(lv.usedRegs())
  1463  	roff := duint32(&regsSymTmp, 0, uint32(len(lv.regMaps))) // number of bitmaps
  1464  	roff = duint32(&regsSymTmp, roff, uint32(regs.n))        // number of bits in each bitmap
  1465  	if regs.n > 32 {
  1466  		// Our uint32 conversion below won't work.
  1467  		Fatalf("GP registers overflow uint32")
  1468  	}
  1469  
  1470  	if regs.n > 0 {
  1471  		for _, live := range lv.regMaps {
  1472  			regs.Clear()
  1473  			regs.b[0] = uint32(live)
  1474  			roff = dbvec(&regsSymTmp, roff, regs)
  1475  		}
  1476  	}
  1477  
  1478  	// Give these LSyms content-addressable names,
  1479  	// so that they can be de-duplicated.
  1480  	// This provides significant binary size savings.
  1481  	//
  1482  	// These symbols will be added to Ctxt.Data by addGCLocals
  1483  	// after parallel compilation is done.
  1484  	makeSym := func(tmpSym *obj.LSym) *obj.LSym {
  1485  		return Ctxt.LookupInit(fmt.Sprintf("gclocals·%x", md5.Sum(tmpSym.P)), func(lsym *obj.LSym) {
  1486  			lsym.P = tmpSym.P
  1487  		})
  1488  	}
  1489  	return makeSym(&argsSymTmp), makeSym(&liveSymTmp), makeSym(&regsSymTmp)
  1490  }
  1491  
  1492  // Entry pointer for liveness analysis. Solves for the liveness of
  1493  // pointer variables in the function and emits a runtime data
  1494  // structure read by the garbage collector.
  1495  // Returns a map from GC safe points to their corresponding stack map index.
  1496  func liveness(e *ssafn, f *ssa.Func, pp *Progs) LivenessMap {
  1497  	// Construct the global liveness state.
  1498  	vars, idx := getvariables(e.curfn)
  1499  	lv := newliveness(e.curfn, f, vars, idx, e.stkptrsize)
  1500  
  1501  	// Run the dataflow framework.
  1502  	lv.prologue()
  1503  	lv.solve()
  1504  	lv.epilogue()
  1505  	if debuglive > 0 {
  1506  		lv.showlive(nil, lv.stackMaps[0])
  1507  		for _, b := range f.Blocks {
  1508  			for _, val := range b.Values {
  1509  				if idx := lv.livenessMap.Get(val); idx.Valid() {
  1510  					lv.showlive(val, lv.stackMaps[idx.stackMapIndex])
  1511  				}
  1512  			}
  1513  		}
  1514  	}
  1515  	if debuglive >= 2 {
  1516  		lv.printDebug()
  1517  	}
  1518  
  1519  	// Update the function cache.
  1520  	{
  1521  		cache := f.Cache.Liveness.(*livenessFuncCache)
  1522  		if cap(lv.be) < 2000 { // Threshold from ssa.Cache slices.
  1523  			for i := range lv.be {
  1524  				lv.be[i] = BlockEffects{}
  1525  			}
  1526  			cache.be = lv.be
  1527  		}
  1528  		if cap(lv.livenessMap.m) < 2000 {
  1529  			cache.livenessMap = lv.livenessMap
  1530  		}
  1531  	}
  1532  
  1533  	// Emit the live pointer map data structures
  1534  	ls := e.curfn.Func.lsym
  1535  	ls.Func.GCArgs, ls.Func.GCLocals, ls.Func.GCRegs = lv.emit()
  1536  
  1537  	p := pp.Prog(obj.AFUNCDATA)
  1538  	Addrconst(&p.From, objabi.FUNCDATA_ArgsPointerMaps)
  1539  	p.To.Type = obj.TYPE_MEM
  1540  	p.To.Name = obj.NAME_EXTERN
  1541  	p.To.Sym = ls.Func.GCArgs
  1542  
  1543  	p = pp.Prog(obj.AFUNCDATA)
  1544  	Addrconst(&p.From, objabi.FUNCDATA_LocalsPointerMaps)
  1545  	p.To.Type = obj.TYPE_MEM
  1546  	p.To.Name = obj.NAME_EXTERN
  1547  	p.To.Sym = ls.Func.GCLocals
  1548  
  1549  	p = pp.Prog(obj.AFUNCDATA)
  1550  	Addrconst(&p.From, objabi.FUNCDATA_RegPointerMaps)
  1551  	p.To.Type = obj.TYPE_MEM
  1552  	p.To.Name = obj.NAME_EXTERN
  1553  	p.To.Sym = ls.Func.GCRegs
  1554  
  1555  	return lv.livenessMap
  1556  }
  1557  
  1558  // isfat reports whether a variable of type t needs multiple assignments to initialize.
  1559  // For example:
  1560  //
  1561  // 	type T struct { x, y int }
  1562  // 	x := T{x: 0, y: 1}
  1563  //
  1564  // Then we need:
  1565  //
  1566  // 	var t T
  1567  // 	t.x = 0
  1568  // 	t.y = 1
  1569  //
  1570  // to fully initialize t.
  1571  func isfat(t *types.Type) bool {
  1572  	if t != nil {
  1573  		switch t.Etype {
  1574  		case TSLICE, TSTRING,
  1575  			TINTER: // maybe remove later
  1576  			return true
  1577  		case TARRAY:
  1578  			// Array of 1 element, check if element is fat
  1579  			if t.NumElem() == 1 {
  1580  				return isfat(t.Elem())
  1581  			}
  1582  			return true
  1583  		case TSTRUCT:
  1584  			// Struct with 1 field, check if field is fat
  1585  			if t.NumFields() == 1 {
  1586  				return isfat(t.Field(0).Type)
  1587  			}
  1588  			return true
  1589  		}
  1590  	}
  1591  
  1592  	return false
  1593  }