Source file src/cmd/compile/internal/ssa/func.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import (
     8  	"cmd/compile/internal/abi"
     9  	"cmd/compile/internal/base"
    10  	"cmd/compile/internal/ir"
    11  	"cmd/compile/internal/typecheck"
    12  	"cmd/compile/internal/types"
    13  	"cmd/internal/obj"
    14  	"cmd/internal/src"
    15  	"fmt"
    16  	"math"
    17  	"strings"
    18  )
    19  
    20  // A Func represents a Go func declaration (or function literal) and its body.
    21  // This package compiles each Func independently.
    22  // Funcs are single-use; a new Func must be created for every compiled function.
    23  type Func struct {
    24  	Config *Config     // architecture information
    25  	Cache  *Cache      // re-usable cache
    26  	fe     Frontend    // frontend state associated with this Func, callbacks into compiler frontend
    27  	pass   *pass       // current pass information (name, options, etc.)
    28  	Name   string      // e.g. NewFunc or (*Func).NumBlocks (no package prefix)
    29  	Type   *types.Type // type signature of the function.
    30  	Blocks []*Block    // unordered set of all basic blocks (note: not indexable by ID)
    31  	Entry  *Block      // the entry basic block
    32  
    33  	bid idAlloc // block ID allocator
    34  	vid idAlloc // value ID allocator
    35  
    36  	HTMLWriter     *HTMLWriter    // html writer, for debugging
    37  	PrintOrHtmlSSA bool           // true if GOSSAFUNC matches, true even if fe.Log() (spew phase results to stdout) is false.  There's an odd dependence on this in debug.go for method logf.
    38  	ruleMatches    map[string]int // number of times countRule was called during compilation for any given string
    39  	ABI0           *abi.ABIConfig // A copy, for no-sync access
    40  	ABI1           *abi.ABIConfig // A copy, for no-sync access
    41  	ABISelf        *abi.ABIConfig // ABI for function being compiled
    42  	ABIDefault     *abi.ABIConfig // ABI for rtcall and other no-parsed-signature/pragma functions.
    43  
    44  	scheduled   bool  // Values in Blocks are in final order
    45  	laidout     bool  // Blocks are ordered
    46  	NoSplit     bool  // true if function is marked as nosplit.  Used by schedule check pass.
    47  	dumpFileSeq uint8 // the sequence numbers of dump file. (%s_%02d__%s.dump", funcname, dumpFileSeq, phaseName)
    48  	IsPgoHot    bool
    49  
    50  	// when register allocation is done, maps value ids to locations
    51  	RegAlloc []Location
    52  
    53  	// temporary registers allocated to rare instructions
    54  	tempRegs map[ID]*Register
    55  
    56  	// map from LocalSlot to set of Values that we want to store in that slot.
    57  	NamedValues map[LocalSlot][]*Value
    58  	// Names is a copy of NamedValues.Keys. We keep a separate list
    59  	// of keys to make iteration order deterministic.
    60  	Names []*LocalSlot
    61  	// Canonicalize root/top-level local slots, and canonicalize their pieces.
    62  	// Because LocalSlot pieces refer to their parents with a pointer, this ensures that equivalent slots really are equal.
    63  	CanonicalLocalSlots  map[LocalSlot]*LocalSlot
    64  	CanonicalLocalSplits map[LocalSlotSplitKey]*LocalSlot
    65  
    66  	// RegArgs is a slice of register-memory pairs that must be spilled and unspilled in the uncommon path of function entry.
    67  	RegArgs []Spill
    68  	// OwnAux describes parameters and results for this function.
    69  	OwnAux *AuxCall
    70  
    71  	freeValues *Value // free Values linked by argstorage[0].  All other fields except ID are 0/nil.
    72  	freeBlocks *Block // free Blocks linked by succstorage[0].b.  All other fields except ID are 0/nil.
    73  
    74  	cachedPostorder  []*Block   // cached postorder traversal
    75  	cachedIdom       []*Block   // cached immediate dominators
    76  	cachedSdom       SparseTree // cached dominator tree
    77  	cachedLoopnest   *loopnest  // cached loop nest information
    78  	cachedLineStarts *xposmap   // cached map/set of xpos to integers
    79  
    80  	auxmap    auxmap             // map from aux values to opaque ids used by CSE
    81  	constants map[int64][]*Value // constants cache, keyed by constant value; users must check value's Op and Type
    82  }
    83  
    84  type LocalSlotSplitKey struct {
    85  	parent *LocalSlot
    86  	Off    int64       // offset of slot in N
    87  	Type   *types.Type // type of slot
    88  }
    89  
    90  // NewFunc returns a new, empty function object.
    91  // Caller must reset cache before calling NewFunc.
    92  func (c *Config) NewFunc(fe Frontend, cache *Cache) *Func {
    93  	return &Func{
    94  		fe:     fe,
    95  		Config: c,
    96  		Cache:  cache,
    97  
    98  		NamedValues:          make(map[LocalSlot][]*Value),
    99  		CanonicalLocalSlots:  make(map[LocalSlot]*LocalSlot),
   100  		CanonicalLocalSplits: make(map[LocalSlotSplitKey]*LocalSlot),
   101  	}
   102  }
   103  
   104  // NumBlocks returns an integer larger than the id of any Block in the Func.
   105  func (f *Func) NumBlocks() int {
   106  	return f.bid.num()
   107  }
   108  
   109  // NumValues returns an integer larger than the id of any Value in the Func.
   110  func (f *Func) NumValues() int {
   111  	return f.vid.num()
   112  }
   113  
   114  // NameABI returns the function name followed by comma and the ABI number.
   115  // This is intended for use with GOSSAFUNC and HTML dumps, and differs from
   116  // the linker's "<1>" convention because "<" and ">" require shell quoting
   117  // and are not legal file names (for use with GOSSADIR) on Windows.
   118  func (f *Func) NameABI() string {
   119  	return FuncNameABI(f.Name, f.ABISelf.Which())
   120  }
   121  
   122  // FuncNameABI returns n followed by a comma and the value of a.
   123  // This is a separate function to allow a single point encoding
   124  // of the format, which is used in places where there's not a Func yet.
   125  func FuncNameABI(n string, a obj.ABI) string {
   126  	return fmt.Sprintf("%s,%d", n, a)
   127  }
   128  
   129  // newSparseSet returns a sparse set that can store at least up to n integers.
   130  func (f *Func) newSparseSet(n int) *sparseSet {
   131  	return f.Cache.allocSparseSet(n)
   132  }
   133  
   134  // retSparseSet returns a sparse set to the config's cache of sparse
   135  // sets to be reused by f.newSparseSet.
   136  func (f *Func) retSparseSet(ss *sparseSet) {
   137  	f.Cache.freeSparseSet(ss)
   138  }
   139  
   140  // newSparseMap returns a sparse map that can store at least up to n integers.
   141  func (f *Func) newSparseMap(n int) *sparseMap {
   142  	return f.Cache.allocSparseMap(n)
   143  }
   144  
   145  // retSparseMap returns a sparse map to the config's cache of sparse
   146  // sets to be reused by f.newSparseMap.
   147  func (f *Func) retSparseMap(ss *sparseMap) {
   148  	f.Cache.freeSparseMap(ss)
   149  }
   150  
   151  // newSparseMapPos returns a sparse map that can store at least up to n integers.
   152  func (f *Func) newSparseMapPos(n int) *sparseMapPos {
   153  	return f.Cache.allocSparseMapPos(n)
   154  }
   155  
   156  // retSparseMapPos returns a sparse map to the config's cache of sparse
   157  // sets to be reused by f.newSparseMapPos.
   158  func (f *Func) retSparseMapPos(ss *sparseMapPos) {
   159  	f.Cache.freeSparseMapPos(ss)
   160  }
   161  
   162  // newPoset returns a new poset from the internal cache
   163  func (f *Func) newPoset() *poset {
   164  	if len(f.Cache.scrPoset) > 0 {
   165  		po := f.Cache.scrPoset[len(f.Cache.scrPoset)-1]
   166  		f.Cache.scrPoset = f.Cache.scrPoset[:len(f.Cache.scrPoset)-1]
   167  		return po
   168  	}
   169  	return newPoset()
   170  }
   171  
   172  // retPoset returns a poset to the internal cache
   173  func (f *Func) retPoset(po *poset) {
   174  	f.Cache.scrPoset = append(f.Cache.scrPoset, po)
   175  }
   176  
   177  func (f *Func) localSlotAddr(slot LocalSlot) *LocalSlot {
   178  	a, ok := f.CanonicalLocalSlots[slot]
   179  	if !ok {
   180  		a = new(LocalSlot)
   181  		*a = slot // don't escape slot
   182  		f.CanonicalLocalSlots[slot] = a
   183  	}
   184  	return a
   185  }
   186  
   187  func (f *Func) SplitString(name *LocalSlot) (*LocalSlot, *LocalSlot) {
   188  	ptrType := types.NewPtr(types.Types[types.TUINT8])
   189  	lenType := types.Types[types.TINT]
   190  	// Split this string up into two separate variables.
   191  	p := f.SplitSlot(name, ".ptr", 0, ptrType)
   192  	l := f.SplitSlot(name, ".len", ptrType.Size(), lenType)
   193  	return p, l
   194  }
   195  
   196  func (f *Func) SplitInterface(name *LocalSlot) (*LocalSlot, *LocalSlot) {
   197  	n := name.N
   198  	u := types.Types[types.TUINTPTR]
   199  	t := types.NewPtr(types.Types[types.TUINT8])
   200  	// Split this interface up into two separate variables.
   201  	sfx := ".itab"
   202  	if n.Type().IsEmptyInterface() {
   203  		sfx = ".type"
   204  	}
   205  	c := f.SplitSlot(name, sfx, 0, u) // see comment in typebits.Set
   206  	d := f.SplitSlot(name, ".data", u.Size(), t)
   207  	return c, d
   208  }
   209  
   210  func (f *Func) SplitSlice(name *LocalSlot) (*LocalSlot, *LocalSlot, *LocalSlot) {
   211  	ptrType := types.NewPtr(name.Type.Elem())
   212  	lenType := types.Types[types.TINT]
   213  	p := f.SplitSlot(name, ".ptr", 0, ptrType)
   214  	l := f.SplitSlot(name, ".len", ptrType.Size(), lenType)
   215  	c := f.SplitSlot(name, ".cap", ptrType.Size()+lenType.Size(), lenType)
   216  	return p, l, c
   217  }
   218  
   219  func (f *Func) SplitComplex(name *LocalSlot) (*LocalSlot, *LocalSlot) {
   220  	s := name.Type.Size() / 2
   221  	var t *types.Type
   222  	if s == 8 {
   223  		t = types.Types[types.TFLOAT64]
   224  	} else {
   225  		t = types.Types[types.TFLOAT32]
   226  	}
   227  	r := f.SplitSlot(name, ".real", 0, t)
   228  	i := f.SplitSlot(name, ".imag", t.Size(), t)
   229  	return r, i
   230  }
   231  
   232  func (f *Func) SplitInt64(name *LocalSlot) (*LocalSlot, *LocalSlot) {
   233  	var t *types.Type
   234  	if name.Type.IsSigned() {
   235  		t = types.Types[types.TINT32]
   236  	} else {
   237  		t = types.Types[types.TUINT32]
   238  	}
   239  	if f.Config.BigEndian {
   240  		return f.SplitSlot(name, ".hi", 0, t), f.SplitSlot(name, ".lo", t.Size(), types.Types[types.TUINT32])
   241  	}
   242  	return f.SplitSlot(name, ".hi", t.Size(), t), f.SplitSlot(name, ".lo", 0, types.Types[types.TUINT32])
   243  }
   244  
   245  func (f *Func) SplitStruct(name *LocalSlot, i int) *LocalSlot {
   246  	st := name.Type
   247  	return f.SplitSlot(name, st.FieldName(i), st.FieldOff(i), st.FieldType(i))
   248  }
   249  func (f *Func) SplitArray(name *LocalSlot) *LocalSlot {
   250  	n := name.N
   251  	at := name.Type
   252  	if at.NumElem() != 1 {
   253  		base.FatalfAt(n.Pos(), "bad array size")
   254  	}
   255  	et := at.Elem()
   256  	return f.SplitSlot(name, "[0]", 0, et)
   257  }
   258  
   259  func (f *Func) SplitSlot(name *LocalSlot, sfx string, offset int64, t *types.Type) *LocalSlot {
   260  	lssk := LocalSlotSplitKey{name, offset, t}
   261  	if als, ok := f.CanonicalLocalSplits[lssk]; ok {
   262  		return als
   263  	}
   264  	// Note: the _ field may appear several times.  But
   265  	// have no fear, identically-named but distinct Autos are
   266  	// ok, albeit maybe confusing for a debugger.
   267  	ls := f.fe.SplitSlot(name, sfx, offset, t)
   268  	f.CanonicalLocalSplits[lssk] = &ls
   269  	return &ls
   270  }
   271  
   272  // newValue allocates a new Value with the given fields and places it at the end of b.Values.
   273  func (f *Func) newValue(op Op, t *types.Type, b *Block, pos src.XPos) *Value {
   274  	var v *Value
   275  	if f.freeValues != nil {
   276  		v = f.freeValues
   277  		f.freeValues = v.argstorage[0]
   278  		v.argstorage[0] = nil
   279  	} else {
   280  		ID := f.vid.get()
   281  		if int(ID) < len(f.Cache.values) {
   282  			v = &f.Cache.values[ID]
   283  			v.ID = ID
   284  		} else {
   285  			v = &Value{ID: ID}
   286  		}
   287  	}
   288  	v.Op = op
   289  	v.Type = t
   290  	v.Block = b
   291  	if notStmtBoundary(op) {
   292  		pos = pos.WithNotStmt()
   293  	}
   294  	v.Pos = pos
   295  	b.Values = append(b.Values, v)
   296  	return v
   297  }
   298  
   299  // newValueNoBlock allocates a new Value with the given fields.
   300  // The returned value is not placed in any block.  Once the caller
   301  // decides on a block b, it must set b.Block and append
   302  // the returned value to b.Values.
   303  func (f *Func) newValueNoBlock(op Op, t *types.Type, pos src.XPos) *Value {
   304  	var v *Value
   305  	if f.freeValues != nil {
   306  		v = f.freeValues
   307  		f.freeValues = v.argstorage[0]
   308  		v.argstorage[0] = nil
   309  	} else {
   310  		ID := f.vid.get()
   311  		if int(ID) < len(f.Cache.values) {
   312  			v = &f.Cache.values[ID]
   313  			v.ID = ID
   314  		} else {
   315  			v = &Value{ID: ID}
   316  		}
   317  	}
   318  	v.Op = op
   319  	v.Type = t
   320  	v.Block = nil // caller must fix this.
   321  	if notStmtBoundary(op) {
   322  		pos = pos.WithNotStmt()
   323  	}
   324  	v.Pos = pos
   325  	return v
   326  }
   327  
   328  // LogStat writes a string key and int value as a warning in a
   329  // tab-separated format easily handled by spreadsheets or awk.
   330  // file names, lines, and function names are included to provide enough (?)
   331  // context to allow item-by-item comparisons across runs.
   332  // For example:
   333  // awk 'BEGIN {FS="\t"} $3~/TIME/{sum+=$4} END{print "t(ns)=",sum}' t.log
   334  func (f *Func) LogStat(key string, args ...interface{}) {
   335  	value := ""
   336  	for _, a := range args {
   337  		value += fmt.Sprintf("\t%v", a)
   338  	}
   339  	n := "missing_pass"
   340  	if f.pass != nil {
   341  		n = strings.Replace(f.pass.name, " ", "_", -1)
   342  	}
   343  	f.Warnl(f.Entry.Pos, "\t%s\t%s%s\t%s", n, key, value, f.Name)
   344  }
   345  
   346  // unCacheLine removes v from f's constant cache "line" for aux,
   347  // resets v.InCache when it is found (and removed),
   348  // and returns whether v was found in that line.
   349  func (f *Func) unCacheLine(v *Value, aux int64) bool {
   350  	vv := f.constants[aux]
   351  	for i, cv := range vv {
   352  		if v == cv {
   353  			vv[i] = vv[len(vv)-1]
   354  			vv[len(vv)-1] = nil
   355  			f.constants[aux] = vv[0 : len(vv)-1]
   356  			v.InCache = false
   357  			return true
   358  		}
   359  	}
   360  	return false
   361  }
   362  
   363  // unCache removes v from f's constant cache.
   364  func (f *Func) unCache(v *Value) {
   365  	if v.InCache {
   366  		aux := v.AuxInt
   367  		if f.unCacheLine(v, aux) {
   368  			return
   369  		}
   370  		if aux == 0 {
   371  			switch v.Op {
   372  			case OpConstNil:
   373  				aux = constNilMagic
   374  			case OpConstSlice:
   375  				aux = constSliceMagic
   376  			case OpConstString:
   377  				aux = constEmptyStringMagic
   378  			case OpConstInterface:
   379  				aux = constInterfaceMagic
   380  			}
   381  			if aux != 0 && f.unCacheLine(v, aux) {
   382  				return
   383  			}
   384  		}
   385  		f.Fatalf("unCached value %s not found in cache, auxInt=0x%x, adjusted aux=0x%x", v.LongString(), v.AuxInt, aux)
   386  	}
   387  }
   388  
   389  // freeValue frees a value. It must no longer be referenced or have any args.
   390  func (f *Func) freeValue(v *Value) {
   391  	if v.Block == nil {
   392  		f.Fatalf("trying to free an already freed value")
   393  	}
   394  	if v.Uses != 0 {
   395  		f.Fatalf("value %s still has %d uses", v, v.Uses)
   396  	}
   397  	if len(v.Args) != 0 {
   398  		f.Fatalf("value %s still has %d args", v, len(v.Args))
   399  	}
   400  	// Clear everything but ID (which we reuse).
   401  	id := v.ID
   402  	if v.InCache {
   403  		f.unCache(v)
   404  	}
   405  	*v = Value{}
   406  	v.ID = id
   407  	v.argstorage[0] = f.freeValues
   408  	f.freeValues = v
   409  }
   410  
   411  // NewBlock allocates a new Block of the given kind and places it at the end of f.Blocks.
   412  func (f *Func) NewBlock(kind BlockKind) *Block {
   413  	var b *Block
   414  	if f.freeBlocks != nil {
   415  		b = f.freeBlocks
   416  		f.freeBlocks = b.succstorage[0].b
   417  		b.succstorage[0].b = nil
   418  	} else {
   419  		ID := f.bid.get()
   420  		if int(ID) < len(f.Cache.blocks) {
   421  			b = &f.Cache.blocks[ID]
   422  			b.ID = ID
   423  		} else {
   424  			b = &Block{ID: ID}
   425  		}
   426  	}
   427  	b.Kind = kind
   428  	b.Func = f
   429  	b.Preds = b.predstorage[:0]
   430  	b.Succs = b.succstorage[:0]
   431  	b.Values = b.valstorage[:0]
   432  	f.Blocks = append(f.Blocks, b)
   433  	f.invalidateCFG()
   434  	return b
   435  }
   436  
   437  func (f *Func) freeBlock(b *Block) {
   438  	if b.Func == nil {
   439  		f.Fatalf("trying to free an already freed block")
   440  	}
   441  	// Clear everything but ID (which we reuse).
   442  	id := b.ID
   443  	*b = Block{}
   444  	b.ID = id
   445  	b.succstorage[0].b = f.freeBlocks
   446  	f.freeBlocks = b
   447  }
   448  
   449  // NewValue0 returns a new value in the block with no arguments and zero aux values.
   450  func (b *Block) NewValue0(pos src.XPos, op Op, t *types.Type) *Value {
   451  	v := b.Func.newValue(op, t, b, pos)
   452  	v.AuxInt = 0
   453  	v.Args = v.argstorage[:0]
   454  	return v
   455  }
   456  
   457  // NewValue0I returns a new value in the block with no arguments and an auxint value.
   458  func (b *Block) NewValue0I(pos src.XPos, op Op, t *types.Type, auxint int64) *Value {
   459  	v := b.Func.newValue(op, t, b, pos)
   460  	v.AuxInt = auxint
   461  	v.Args = v.argstorage[:0]
   462  	return v
   463  }
   464  
   465  // NewValue0A returns a new value in the block with no arguments and an aux value.
   466  func (b *Block) NewValue0A(pos src.XPos, op Op, t *types.Type, aux Aux) *Value {
   467  	v := b.Func.newValue(op, t, b, pos)
   468  	v.AuxInt = 0
   469  	v.Aux = aux
   470  	v.Args = v.argstorage[:0]
   471  	return v
   472  }
   473  
   474  // NewValue0IA returns a new value in the block with no arguments and both an auxint and aux values.
   475  func (b *Block) NewValue0IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux Aux) *Value {
   476  	v := b.Func.newValue(op, t, b, pos)
   477  	v.AuxInt = auxint
   478  	v.Aux = aux
   479  	v.Args = v.argstorage[:0]
   480  	return v
   481  }
   482  
   483  // NewValue1 returns a new value in the block with one argument and zero aux values.
   484  func (b *Block) NewValue1(pos src.XPos, op Op, t *types.Type, arg *Value) *Value {
   485  	v := b.Func.newValue(op, t, b, pos)
   486  	v.AuxInt = 0
   487  	v.Args = v.argstorage[:1]
   488  	v.argstorage[0] = arg
   489  	arg.Uses++
   490  	return v
   491  }
   492  
   493  // NewValue1I returns a new value in the block with one argument and an auxint value.
   494  func (b *Block) NewValue1I(pos src.XPos, op Op, t *types.Type, auxint int64, arg *Value) *Value {
   495  	v := b.Func.newValue(op, t, b, pos)
   496  	v.AuxInt = auxint
   497  	v.Args = v.argstorage[:1]
   498  	v.argstorage[0] = arg
   499  	arg.Uses++
   500  	return v
   501  }
   502  
   503  // NewValue1A returns a new value in the block with one argument and an aux value.
   504  func (b *Block) NewValue1A(pos src.XPos, op Op, t *types.Type, aux Aux, arg *Value) *Value {
   505  	v := b.Func.newValue(op, t, b, pos)
   506  	v.AuxInt = 0
   507  	v.Aux = aux
   508  	v.Args = v.argstorage[:1]
   509  	v.argstorage[0] = arg
   510  	arg.Uses++
   511  	return v
   512  }
   513  
   514  // NewValue1IA returns a new value in the block with one argument and both an auxint and aux values.
   515  func (b *Block) NewValue1IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux Aux, arg *Value) *Value {
   516  	v := b.Func.newValue(op, t, b, pos)
   517  	v.AuxInt = auxint
   518  	v.Aux = aux
   519  	v.Args = v.argstorage[:1]
   520  	v.argstorage[0] = arg
   521  	arg.Uses++
   522  	return v
   523  }
   524  
   525  // NewValue2 returns a new value in the block with two arguments and zero aux values.
   526  func (b *Block) NewValue2(pos src.XPos, op Op, t *types.Type, arg0, arg1 *Value) *Value {
   527  	v := b.Func.newValue(op, t, b, pos)
   528  	v.AuxInt = 0
   529  	v.Args = v.argstorage[:2]
   530  	v.argstorage[0] = arg0
   531  	v.argstorage[1] = arg1
   532  	arg0.Uses++
   533  	arg1.Uses++
   534  	return v
   535  }
   536  
   537  // NewValue2A returns a new value in the block with two arguments and one aux values.
   538  func (b *Block) NewValue2A(pos src.XPos, op Op, t *types.Type, aux Aux, arg0, arg1 *Value) *Value {
   539  	v := b.Func.newValue(op, t, b, pos)
   540  	v.AuxInt = 0
   541  	v.Aux = aux
   542  	v.Args = v.argstorage[:2]
   543  	v.argstorage[0] = arg0
   544  	v.argstorage[1] = arg1
   545  	arg0.Uses++
   546  	arg1.Uses++
   547  	return v
   548  }
   549  
   550  // NewValue2I returns a new value in the block with two arguments and an auxint value.
   551  func (b *Block) NewValue2I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1 *Value) *Value {
   552  	v := b.Func.newValue(op, t, b, pos)
   553  	v.AuxInt = auxint
   554  	v.Args = v.argstorage[:2]
   555  	v.argstorage[0] = arg0
   556  	v.argstorage[1] = arg1
   557  	arg0.Uses++
   558  	arg1.Uses++
   559  	return v
   560  }
   561  
   562  // NewValue2IA returns a new value in the block with two arguments and both an auxint and aux values.
   563  func (b *Block) NewValue2IA(pos src.XPos, op Op, t *types.Type, auxint int64, aux Aux, arg0, arg1 *Value) *Value {
   564  	v := b.Func.newValue(op, t, b, pos)
   565  	v.AuxInt = auxint
   566  	v.Aux = aux
   567  	v.Args = v.argstorage[:2]
   568  	v.argstorage[0] = arg0
   569  	v.argstorage[1] = arg1
   570  	arg0.Uses++
   571  	arg1.Uses++
   572  	return v
   573  }
   574  
   575  // NewValue3 returns a new value in the block with three arguments and zero aux values.
   576  func (b *Block) NewValue3(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2 *Value) *Value {
   577  	v := b.Func.newValue(op, t, b, pos)
   578  	v.AuxInt = 0
   579  	v.Args = v.argstorage[:3]
   580  	v.argstorage[0] = arg0
   581  	v.argstorage[1] = arg1
   582  	v.argstorage[2] = arg2
   583  	arg0.Uses++
   584  	arg1.Uses++
   585  	arg2.Uses++
   586  	return v
   587  }
   588  
   589  // NewValue3I returns a new value in the block with three arguments and an auxint value.
   590  func (b *Block) NewValue3I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1, arg2 *Value) *Value {
   591  	v := b.Func.newValue(op, t, b, pos)
   592  	v.AuxInt = auxint
   593  	v.Args = v.argstorage[:3]
   594  	v.argstorage[0] = arg0
   595  	v.argstorage[1] = arg1
   596  	v.argstorage[2] = arg2
   597  	arg0.Uses++
   598  	arg1.Uses++
   599  	arg2.Uses++
   600  	return v
   601  }
   602  
   603  // NewValue3A returns a new value in the block with three argument and an aux value.
   604  func (b *Block) NewValue3A(pos src.XPos, op Op, t *types.Type, aux Aux, arg0, arg1, arg2 *Value) *Value {
   605  	v := b.Func.newValue(op, t, b, pos)
   606  	v.AuxInt = 0
   607  	v.Aux = aux
   608  	v.Args = v.argstorage[:3]
   609  	v.argstorage[0] = arg0
   610  	v.argstorage[1] = arg1
   611  	v.argstorage[2] = arg2
   612  	arg0.Uses++
   613  	arg1.Uses++
   614  	arg2.Uses++
   615  	return v
   616  }
   617  
   618  // NewValue4 returns a new value in the block with four arguments and zero aux values.
   619  func (b *Block) NewValue4(pos src.XPos, op Op, t *types.Type, arg0, arg1, arg2, arg3 *Value) *Value {
   620  	v := b.Func.newValue(op, t, b, pos)
   621  	v.AuxInt = 0
   622  	v.Args = []*Value{arg0, arg1, arg2, arg3}
   623  	arg0.Uses++
   624  	arg1.Uses++
   625  	arg2.Uses++
   626  	arg3.Uses++
   627  	return v
   628  }
   629  
   630  // NewValue4I returns a new value in the block with four arguments and auxint value.
   631  func (b *Block) NewValue4I(pos src.XPos, op Op, t *types.Type, auxint int64, arg0, arg1, arg2, arg3 *Value) *Value {
   632  	v := b.Func.newValue(op, t, b, pos)
   633  	v.AuxInt = auxint
   634  	v.Args = []*Value{arg0, arg1, arg2, arg3}
   635  	arg0.Uses++
   636  	arg1.Uses++
   637  	arg2.Uses++
   638  	arg3.Uses++
   639  	return v
   640  }
   641  
   642  // constVal returns a constant value for c.
   643  func (f *Func) constVal(op Op, t *types.Type, c int64, setAuxInt bool) *Value {
   644  	if f.constants == nil {
   645  		f.constants = make(map[int64][]*Value)
   646  	}
   647  	vv := f.constants[c]
   648  	for _, v := range vv {
   649  		if v.Op == op && v.Type.Compare(t) == types.CMPeq {
   650  			if setAuxInt && v.AuxInt != c {
   651  				panic(fmt.Sprintf("cached const %s should have AuxInt of %d", v.LongString(), c))
   652  			}
   653  			return v
   654  		}
   655  	}
   656  	var v *Value
   657  	if setAuxInt {
   658  		v = f.Entry.NewValue0I(src.NoXPos, op, t, c)
   659  	} else {
   660  		v = f.Entry.NewValue0(src.NoXPos, op, t)
   661  	}
   662  	f.constants[c] = append(vv, v)
   663  	v.InCache = true
   664  	return v
   665  }
   666  
   667  // These magic auxint values let us easily cache non-numeric constants
   668  // using the same constants map while making collisions unlikely.
   669  // These values are unlikely to occur in regular code and
   670  // are easy to grep for in case of bugs.
   671  const (
   672  	constSliceMagic       = 1122334455
   673  	constInterfaceMagic   = 2233445566
   674  	constNilMagic         = 3344556677
   675  	constEmptyStringMagic = 4455667788
   676  )
   677  
   678  // ConstBool returns an int constant representing its argument.
   679  func (f *Func) ConstBool(t *types.Type, c bool) *Value {
   680  	i := int64(0)
   681  	if c {
   682  		i = 1
   683  	}
   684  	return f.constVal(OpConstBool, t, i, true)
   685  }
   686  func (f *Func) ConstInt8(t *types.Type, c int8) *Value {
   687  	return f.constVal(OpConst8, t, int64(c), true)
   688  }
   689  func (f *Func) ConstInt16(t *types.Type, c int16) *Value {
   690  	return f.constVal(OpConst16, t, int64(c), true)
   691  }
   692  func (f *Func) ConstInt32(t *types.Type, c int32) *Value {
   693  	return f.constVal(OpConst32, t, int64(c), true)
   694  }
   695  func (f *Func) ConstInt64(t *types.Type, c int64) *Value {
   696  	return f.constVal(OpConst64, t, c, true)
   697  }
   698  func (f *Func) ConstFloat32(t *types.Type, c float64) *Value {
   699  	return f.constVal(OpConst32F, t, int64(math.Float64bits(float64(float32(c)))), true)
   700  }
   701  func (f *Func) ConstFloat64(t *types.Type, c float64) *Value {
   702  	return f.constVal(OpConst64F, t, int64(math.Float64bits(c)), true)
   703  }
   704  
   705  func (f *Func) ConstSlice(t *types.Type) *Value {
   706  	return f.constVal(OpConstSlice, t, constSliceMagic, false)
   707  }
   708  func (f *Func) ConstInterface(t *types.Type) *Value {
   709  	return f.constVal(OpConstInterface, t, constInterfaceMagic, false)
   710  }
   711  func (f *Func) ConstNil(t *types.Type) *Value {
   712  	return f.constVal(OpConstNil, t, constNilMagic, false)
   713  }
   714  func (f *Func) ConstEmptyString(t *types.Type) *Value {
   715  	v := f.constVal(OpConstString, t, constEmptyStringMagic, false)
   716  	v.Aux = StringToAux("")
   717  	return v
   718  }
   719  func (f *Func) ConstOffPtrSP(t *types.Type, c int64, sp *Value) *Value {
   720  	v := f.constVal(OpOffPtr, t, c, true)
   721  	if len(v.Args) == 0 {
   722  		v.AddArg(sp)
   723  	}
   724  	return v
   725  }
   726  
   727  func (f *Func) Frontend() Frontend                                  { return f.fe }
   728  func (f *Func) Warnl(pos src.XPos, msg string, args ...interface{}) { f.fe.Warnl(pos, msg, args...) }
   729  func (f *Func) Logf(msg string, args ...interface{})                { f.fe.Logf(msg, args...) }
   730  func (f *Func) Log() bool                                           { return f.fe.Log() }
   731  
   732  func (f *Func) Fatalf(msg string, args ...interface{}) {
   733  	stats := "crashed"
   734  	if f.Log() {
   735  		f.Logf("  pass %s end %s\n", f.pass.name, stats)
   736  		printFunc(f)
   737  	}
   738  	if f.HTMLWriter != nil {
   739  		f.HTMLWriter.WritePhase(f.pass.name, fmt.Sprintf("%s <span class=\"stats\">%s</span>", f.pass.name, stats))
   740  		f.HTMLWriter.flushPhases()
   741  	}
   742  	f.fe.Fatalf(f.Entry.Pos, msg, args...)
   743  }
   744  
   745  // postorder returns the reachable blocks in f in a postorder traversal.
   746  func (f *Func) postorder() []*Block {
   747  	if f.cachedPostorder == nil {
   748  		f.cachedPostorder = postorder(f)
   749  	}
   750  	return f.cachedPostorder
   751  }
   752  
   753  func (f *Func) Postorder() []*Block {
   754  	return f.postorder()
   755  }
   756  
   757  // Idom returns a map from block ID to the immediate dominator of that block.
   758  // f.Entry.ID maps to nil. Unreachable blocks map to nil as well.
   759  func (f *Func) Idom() []*Block {
   760  	if f.cachedIdom == nil {
   761  		f.cachedIdom = dominators(f)
   762  	}
   763  	return f.cachedIdom
   764  }
   765  
   766  // Sdom returns a sparse tree representing the dominator relationships
   767  // among the blocks of f.
   768  func (f *Func) Sdom() SparseTree {
   769  	if f.cachedSdom == nil {
   770  		f.cachedSdom = newSparseTree(f, f.Idom())
   771  	}
   772  	return f.cachedSdom
   773  }
   774  
   775  // loopnest returns the loop nest information for f.
   776  func (f *Func) loopnest() *loopnest {
   777  	if f.cachedLoopnest == nil {
   778  		f.cachedLoopnest = loopnestfor(f)
   779  	}
   780  	return f.cachedLoopnest
   781  }
   782  
   783  // invalidateCFG tells f that its CFG has changed.
   784  func (f *Func) invalidateCFG() {
   785  	f.cachedPostorder = nil
   786  	f.cachedIdom = nil
   787  	f.cachedSdom = nil
   788  	f.cachedLoopnest = nil
   789  }
   790  
   791  // DebugHashMatch returns
   792  //
   793  //	base.DebugHashMatch(this function's package.name)
   794  //
   795  // for use in bug isolation.  The return value is true unless
   796  // environment variable GOSSAHASH is set, in which case "it depends".
   797  // See [base.DebugHashMatch] for more information.
   798  func (f *Func) DebugHashMatch() bool {
   799  	if !base.HasDebugHash() {
   800  		return true
   801  	}
   802  	sym := f.fe.Func().Sym()
   803  	return base.DebugHashMatchPkgFunc(sym.Pkg.Path, sym.Name)
   804  }
   805  
   806  func (f *Func) spSb() (sp, sb *Value) {
   807  	initpos := src.NoXPos // These are originally created with no position in ssa.go; if they are optimized out then recreated, should be the same.
   808  	for _, v := range f.Entry.Values {
   809  		if v.Op == OpSB {
   810  			sb = v
   811  		}
   812  		if v.Op == OpSP {
   813  			sp = v
   814  		}
   815  		if sb != nil && sp != nil {
   816  			return
   817  		}
   818  	}
   819  	if sb == nil {
   820  		sb = f.Entry.NewValue0(initpos.WithNotStmt(), OpSB, f.Config.Types.Uintptr)
   821  	}
   822  	if sp == nil {
   823  		sp = f.Entry.NewValue0(initpos.WithNotStmt(), OpSP, f.Config.Types.Uintptr)
   824  	}
   825  	return
   826  }
   827  
   828  // useFMA allows targeted debugging w/ GOFMAHASH
   829  // If you have an architecture-dependent FP glitch, this will help you find it.
   830  func (f *Func) useFMA(v *Value) bool {
   831  	if !f.Config.UseFMA {
   832  		return false
   833  	}
   834  	if base.FmaHash == nil {
   835  		return true
   836  	}
   837  	return base.FmaHash.MatchPos(v.Pos, nil)
   838  }
   839  
   840  // NewLocal returns a new anonymous local variable of the given type.
   841  func (f *Func) NewLocal(pos src.XPos, typ *types.Type) *ir.Name {
   842  	nn := typecheck.TempAt(pos, f.fe.Func(), typ) // Note: adds new auto to fn.Dcl list
   843  	nn.SetNonMergeable(true)
   844  	return nn
   845  }
   846  
   847  // IsMergeCandidate returns true if variable n could participate in
   848  // stack slot merging. For now we're restricting the set to things to
   849  // items larger than what CanSSA would allow (approximateky, we disallow things
   850  // marked as open defer slots so as to avoid complicating liveness
   851  // analysis.
   852  func IsMergeCandidate(n *ir.Name) bool {
   853  	if base.Debug.MergeLocals == 0 ||
   854  		base.Flag.N != 0 ||
   855  		n.Class != ir.PAUTO ||
   856  		n.Type().Size() <= int64(3*types.PtrSize) ||
   857  		n.Addrtaken() ||
   858  		n.NonMergeable() ||
   859  		n.OpenDeferSlot() {
   860  		return false
   861  	}
   862  	return true
   863  }
   864  

View as plain text