github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/compile/internal/gc/ssa.go (about)

     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 gc
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"encoding/binary"
    11  	"fmt"
    12  	"html"
    13  	"os"
    14  	"sort"
    15  
    16  	"cmd/compile/internal/ssa"
    17  	"cmd/compile/internal/types"
    18  	"cmd/internal/obj"
    19  	"cmd/internal/objabi"
    20  	"cmd/internal/src"
    21  	"cmd/internal/sys"
    22  )
    23  
    24  var ssaConfig *ssa.Config
    25  var ssaCaches []ssa.Cache
    26  
    27  var ssaDump string     // early copy of $GOSSAFUNC; the func name to dump output for
    28  var ssaDumpStdout bool // whether to dump to stdout
    29  var ssaDumpCFG string  // generate CFGs for these phases
    30  const ssaDumpFile = "ssa.html"
    31  
    32  // ssaDumpInlined holds all inlined functions when ssaDump contains a function name.
    33  var ssaDumpInlined []*Node
    34  
    35  func initssaconfig() {
    36  	types_ := ssa.NewTypes()
    37  
    38  	if thearch.SoftFloat {
    39  		softfloatInit()
    40  	}
    41  
    42  	// Generate a few pointer types that are uncommon in the frontend but common in the backend.
    43  	// Caching is disabled in the backend, so generating these here avoids allocations.
    44  	_ = types.NewPtr(types.Types[TINTER])                             // *interface{}
    45  	_ = types.NewPtr(types.NewPtr(types.Types[TSTRING]))              // **string
    46  	_ = types.NewPtr(types.NewPtr(types.Idealstring))                 // **string
    47  	_ = types.NewPtr(types.NewSlice(types.Types[TINTER]))             // *[]interface{}
    48  	_ = types.NewPtr(types.NewPtr(types.Bytetype))                    // **byte
    49  	_ = types.NewPtr(types.NewSlice(types.Bytetype))                  // *[]byte
    50  	_ = types.NewPtr(types.NewSlice(types.Types[TSTRING]))            // *[]string
    51  	_ = types.NewPtr(types.NewSlice(types.Idealstring))               // *[]string
    52  	_ = types.NewPtr(types.NewPtr(types.NewPtr(types.Types[TUINT8]))) // ***uint8
    53  	_ = types.NewPtr(types.Types[TINT16])                             // *int16
    54  	_ = types.NewPtr(types.Types[TINT64])                             // *int64
    55  	_ = types.NewPtr(types.Errortype)                                 // *error
    56  	types.NewPtrCacheEnabled = false
    57  	ssaConfig = ssa.NewConfig(thearch.LinkArch.Name, *types_, Ctxt, Debug['N'] == 0)
    58  	if thearch.LinkArch.Name == "386" {
    59  		ssaConfig.Set387(thearch.Use387)
    60  	}
    61  	ssaConfig.SoftFloat = thearch.SoftFloat
    62  	ssaConfig.Race = flag_race
    63  	ssaCaches = make([]ssa.Cache, nBackendWorkers)
    64  
    65  	// Set up some runtime functions we'll need to call.
    66  	assertE2I = sysfunc("assertE2I")
    67  	assertE2I2 = sysfunc("assertE2I2")
    68  	assertI2I = sysfunc("assertI2I")
    69  	assertI2I2 = sysfunc("assertI2I2")
    70  	deferproc = sysfunc("deferproc")
    71  	Deferreturn = sysfunc("deferreturn")
    72  	Duffcopy = sysvar("duffcopy")             // asm func with special ABI
    73  	Duffzero = sysvar("duffzero")             // asm func with special ABI
    74  	gcWriteBarrier = sysvar("gcWriteBarrier") // asm func with special ABI
    75  	goschedguarded = sysfunc("goschedguarded")
    76  	growslice = sysfunc("growslice")
    77  	msanread = sysfunc("msanread")
    78  	msanwrite = sysfunc("msanwrite")
    79  	newproc = sysfunc("newproc")
    80  	panicdivide = sysfunc("panicdivide")
    81  	panicdottypeE = sysfunc("panicdottypeE")
    82  	panicdottypeI = sysfunc("panicdottypeI")
    83  	panicindex = sysfunc("panicindex")
    84  	panicnildottype = sysfunc("panicnildottype")
    85  	panicoverflow = sysfunc("panicoverflow")
    86  	panicslice = sysfunc("panicslice")
    87  	raceread = sysfunc("raceread")
    88  	racereadrange = sysfunc("racereadrange")
    89  	racewrite = sysfunc("racewrite")
    90  	racewriterange = sysfunc("racewriterange")
    91  	x86HasPOPCNT = sysvar("x86HasPOPCNT")       // bool
    92  	x86HasSSE41 = sysvar("x86HasSSE41")         // bool
    93  	arm64HasATOMICS = sysvar("arm64HasATOMICS") // bool
    94  	typedmemclr = sysfunc("typedmemclr")
    95  	typedmemmove = sysfunc("typedmemmove")
    96  	Udiv = sysvar("udiv")                 // asm func with special ABI
    97  	writeBarrier = sysvar("writeBarrier") // struct { bool; ... }
    98  
    99  	// GO386=387 runtime definitions
   100  	ControlWord64trunc = sysvar("controlWord64trunc") // uint16
   101  	ControlWord32 = sysvar("controlWord32")           // uint16
   102  
   103  	// Wasm (all asm funcs with special ABIs)
   104  	WasmMove = sysvar("wasmMove")
   105  	WasmZero = sysvar("wasmZero")
   106  	WasmDiv = sysvar("wasmDiv")
   107  	WasmTruncS = sysvar("wasmTruncS")
   108  	WasmTruncU = sysvar("wasmTruncU")
   109  	SigPanic = sysfunc("sigpanic")
   110  }
   111  
   112  // buildssa builds an SSA function for fn.
   113  // worker indicates which of the backend workers is doing the processing.
   114  func buildssa(fn *Node, worker int) *ssa.Func {
   115  	name := fn.funcname()
   116  	printssa := name == ssaDump
   117  	var astBuf *bytes.Buffer
   118  	if printssa {
   119  		astBuf = &bytes.Buffer{}
   120  		fdumplist(astBuf, "buildssa-enter", fn.Func.Enter)
   121  		fdumplist(astBuf, "buildssa-body", fn.Nbody)
   122  		fdumplist(astBuf, "buildssa-exit", fn.Func.Exit)
   123  		if ssaDumpStdout {
   124  			fmt.Println("generating SSA for", name)
   125  			fmt.Print(astBuf.String())
   126  		}
   127  	}
   128  
   129  	var s state
   130  	s.pushLine(fn.Pos)
   131  	defer s.popLine()
   132  
   133  	s.hasdefer = fn.Func.HasDefer()
   134  	if fn.Func.Pragma&CgoUnsafeArgs != 0 {
   135  		s.cgoUnsafeArgs = true
   136  	}
   137  
   138  	fe := ssafn{
   139  		curfn: fn,
   140  		log:   printssa && ssaDumpStdout,
   141  	}
   142  	s.curfn = fn
   143  
   144  	s.f = ssa.NewFunc(&fe)
   145  	s.config = ssaConfig
   146  	s.f.Type = fn.Type
   147  	s.f.Config = ssaConfig
   148  	s.f.Cache = &ssaCaches[worker]
   149  	s.f.Cache.Reset()
   150  	s.f.DebugTest = s.f.DebugHashMatch("GOSSAHASH", name)
   151  	s.f.Name = name
   152  	s.f.PrintOrHtmlSSA = printssa
   153  	if fn.Func.Pragma&Nosplit != 0 {
   154  		s.f.NoSplit = true
   155  	}
   156  	s.panics = map[funcLine]*ssa.Block{}
   157  	s.softFloat = s.config.SoftFloat
   158  
   159  	if printssa {
   160  		s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f.Frontend(), name, ssaDumpCFG)
   161  		// TODO: generate and print a mapping from nodes to values and blocks
   162  		dumpSourcesColumn(s.f.HTMLWriter, fn)
   163  		s.f.HTMLWriter.WriteAST("AST", astBuf)
   164  	}
   165  
   166  	// Allocate starting block
   167  	s.f.Entry = s.f.NewBlock(ssa.BlockPlain)
   168  
   169  	// Allocate starting values
   170  	s.labels = map[string]*ssaLabel{}
   171  	s.labeledNodes = map[*Node]*ssaLabel{}
   172  	s.fwdVars = map[*Node]*ssa.Value{}
   173  	s.startmem = s.entryNewValue0(ssa.OpInitMem, types.TypeMem)
   174  	s.sp = s.entryNewValue0(ssa.OpSP, types.Types[TUINTPTR]) // TODO: use generic pointer type (unsafe.Pointer?) instead
   175  	s.sb = s.entryNewValue0(ssa.OpSB, types.Types[TUINTPTR])
   176  
   177  	s.startBlock(s.f.Entry)
   178  	s.vars[&memVar] = s.startmem
   179  
   180  	// Generate addresses of local declarations
   181  	s.decladdrs = map[*Node]*ssa.Value{}
   182  	for _, n := range fn.Func.Dcl {
   183  		switch n.Class() {
   184  		case PPARAM, PPARAMOUT:
   185  			s.decladdrs[n] = s.entryNewValue2A(ssa.OpLocalAddr, types.NewPtr(n.Type), n, s.sp, s.startmem)
   186  			if n.Class() == PPARAMOUT && s.canSSA(n) {
   187  				// Save ssa-able PPARAMOUT variables so we can
   188  				// store them back to the stack at the end of
   189  				// the function.
   190  				s.returns = append(s.returns, n)
   191  			}
   192  		case PAUTO:
   193  			// processed at each use, to prevent Addr coming
   194  			// before the decl.
   195  		case PAUTOHEAP:
   196  			// moved to heap - already handled by frontend
   197  		case PFUNC:
   198  			// local function - already handled by frontend
   199  		default:
   200  			s.Fatalf("local variable with class %v unimplemented", n.Class())
   201  		}
   202  	}
   203  
   204  	// Populate SSAable arguments.
   205  	for _, n := range fn.Func.Dcl {
   206  		if n.Class() == PPARAM && s.canSSA(n) {
   207  			v := s.newValue0A(ssa.OpArg, n.Type, n)
   208  			s.vars[n] = v
   209  			s.addNamedValue(n, v) // This helps with debugging information, not needed for compilation itself.
   210  		}
   211  	}
   212  
   213  	// Convert the AST-based IR to the SSA-based IR
   214  	s.stmtList(fn.Func.Enter)
   215  	s.stmtList(fn.Nbody)
   216  
   217  	// fallthrough to exit
   218  	if s.curBlock != nil {
   219  		s.pushLine(fn.Func.Endlineno)
   220  		s.exit()
   221  		s.popLine()
   222  	}
   223  
   224  	for _, b := range s.f.Blocks {
   225  		if b.Pos != src.NoXPos {
   226  			s.updateUnsetPredPos(b)
   227  		}
   228  	}
   229  
   230  	s.insertPhis()
   231  
   232  	// Main call to ssa package to compile function
   233  	ssa.Compile(s.f)
   234  	return s.f
   235  }
   236  
   237  func dumpSourcesColumn(writer *ssa.HTMLWriter, fn *Node) {
   238  	// Read sources of target function fn.
   239  	fname := Ctxt.PosTable.Pos(fn.Pos).Filename()
   240  	targetFn, err := readFuncLines(fname, fn.Pos.Line(), fn.Func.Endlineno.Line())
   241  	if err != nil {
   242  		writer.Logger.Logf("cannot read sources for function %v: %v", fn, err)
   243  	}
   244  
   245  	// Read sources of inlined functions.
   246  	var inlFns []*ssa.FuncLines
   247  	for _, fi := range ssaDumpInlined {
   248  		var elno src.XPos
   249  		if fi.Name.Defn == nil {
   250  			// Endlineno is filled from exported data.
   251  			elno = fi.Func.Endlineno
   252  		} else {
   253  			elno = fi.Name.Defn.Func.Endlineno
   254  		}
   255  		fname := Ctxt.PosTable.Pos(fi.Pos).Filename()
   256  		fnLines, err := readFuncLines(fname, fi.Pos.Line(), elno.Line())
   257  		if err != nil {
   258  			writer.Logger.Logf("cannot read sources for function %v: %v", fi, err)
   259  			continue
   260  		}
   261  		inlFns = append(inlFns, fnLines)
   262  	}
   263  
   264  	sort.Sort(ssa.ByTopo(inlFns))
   265  	if targetFn != nil {
   266  		inlFns = append([]*ssa.FuncLines{targetFn}, inlFns...)
   267  	}
   268  
   269  	writer.WriteSources("sources", inlFns)
   270  }
   271  
   272  func readFuncLines(file string, start, end uint) (*ssa.FuncLines, error) {
   273  	f, err := os.Open(os.ExpandEnv(file))
   274  	if err != nil {
   275  		return nil, err
   276  	}
   277  	defer f.Close()
   278  	var lines []string
   279  	ln := uint(1)
   280  	scanner := bufio.NewScanner(f)
   281  	for scanner.Scan() && ln <= end {
   282  		if ln >= start {
   283  			lines = append(lines, scanner.Text())
   284  		}
   285  		ln++
   286  	}
   287  	return &ssa.FuncLines{Filename: file, StartLineno: start, Lines: lines}, nil
   288  }
   289  
   290  // updateUnsetPredPos propagates the earliest-value position information for b
   291  // towards all of b's predecessors that need a position, and recurs on that
   292  // predecessor if its position is updated. B should have a non-empty position.
   293  func (s *state) updateUnsetPredPos(b *ssa.Block) {
   294  	if b.Pos == src.NoXPos {
   295  		s.Fatalf("Block %s should have a position", b)
   296  	}
   297  	bestPos := src.NoXPos
   298  	for _, e := range b.Preds {
   299  		p := e.Block()
   300  		if !p.LackingPos() {
   301  			continue
   302  		}
   303  		if bestPos == src.NoXPos {
   304  			bestPos = b.Pos
   305  			for _, v := range b.Values {
   306  				if v.LackingPos() {
   307  					continue
   308  				}
   309  				if v.Pos != src.NoXPos {
   310  					// Assume values are still in roughly textual order;
   311  					// TODO: could also seek minimum position?
   312  					bestPos = v.Pos
   313  					break
   314  				}
   315  			}
   316  		}
   317  		p.Pos = bestPos
   318  		s.updateUnsetPredPos(p) // We do not expect long chains of these, thus recursion is okay.
   319  	}
   320  }
   321  
   322  type state struct {
   323  	// configuration (arch) information
   324  	config *ssa.Config
   325  
   326  	// function we're building
   327  	f *ssa.Func
   328  
   329  	// Node for function
   330  	curfn *Node
   331  
   332  	// labels and labeled control flow nodes (OFOR, OFORUNTIL, OSWITCH, OSELECT) in f
   333  	labels       map[string]*ssaLabel
   334  	labeledNodes map[*Node]*ssaLabel
   335  
   336  	// unlabeled break and continue statement tracking
   337  	breakTo    *ssa.Block // current target for plain break statement
   338  	continueTo *ssa.Block // current target for plain continue statement
   339  
   340  	// current location where we're interpreting the AST
   341  	curBlock *ssa.Block
   342  
   343  	// variable assignments in the current block (map from variable symbol to ssa value)
   344  	// *Node is the unique identifier (an ONAME Node) for the variable.
   345  	// TODO: keep a single varnum map, then make all of these maps slices instead?
   346  	vars map[*Node]*ssa.Value
   347  
   348  	// fwdVars are variables that are used before they are defined in the current block.
   349  	// This map exists just to coalesce multiple references into a single FwdRef op.
   350  	// *Node is the unique identifier (an ONAME Node) for the variable.
   351  	fwdVars map[*Node]*ssa.Value
   352  
   353  	// all defined variables at the end of each block. Indexed by block ID.
   354  	defvars []map[*Node]*ssa.Value
   355  
   356  	// addresses of PPARAM and PPARAMOUT variables.
   357  	decladdrs map[*Node]*ssa.Value
   358  
   359  	// starting values. Memory, stack pointer, and globals pointer
   360  	startmem *ssa.Value
   361  	sp       *ssa.Value
   362  	sb       *ssa.Value
   363  
   364  	// line number stack. The current line number is top of stack
   365  	line []src.XPos
   366  	// the last line number processed; it may have been popped
   367  	lastPos src.XPos
   368  
   369  	// list of panic calls by function name and line number.
   370  	// Used to deduplicate panic calls.
   371  	panics map[funcLine]*ssa.Block
   372  
   373  	// list of PPARAMOUT (return) variables.
   374  	returns []*Node
   375  
   376  	cgoUnsafeArgs bool
   377  	hasdefer      bool // whether the function contains a defer statement
   378  	softFloat     bool
   379  }
   380  
   381  type funcLine struct {
   382  	f    *obj.LSym
   383  	base *src.PosBase
   384  	line uint
   385  }
   386  
   387  type ssaLabel struct {
   388  	target         *ssa.Block // block identified by this label
   389  	breakTarget    *ssa.Block // block to break to in control flow node identified by this label
   390  	continueTarget *ssa.Block // block to continue to in control flow node identified by this label
   391  }
   392  
   393  // label returns the label associated with sym, creating it if necessary.
   394  func (s *state) label(sym *types.Sym) *ssaLabel {
   395  	lab := s.labels[sym.Name]
   396  	if lab == nil {
   397  		lab = new(ssaLabel)
   398  		s.labels[sym.Name] = lab
   399  	}
   400  	return lab
   401  }
   402  
   403  func (s *state) Logf(msg string, args ...interface{}) { s.f.Logf(msg, args...) }
   404  func (s *state) Log() bool                            { return s.f.Log() }
   405  func (s *state) Fatalf(msg string, args ...interface{}) {
   406  	s.f.Frontend().Fatalf(s.peekPos(), msg, args...)
   407  }
   408  func (s *state) Warnl(pos src.XPos, msg string, args ...interface{}) { s.f.Warnl(pos, msg, args...) }
   409  func (s *state) Debug_checknil() bool                                { return s.f.Frontend().Debug_checknil() }
   410  
   411  var (
   412  	// dummy node for the memory variable
   413  	memVar = Node{Op: ONAME, Sym: &types.Sym{Name: "mem"}}
   414  
   415  	// dummy nodes for temporary variables
   416  	ptrVar    = Node{Op: ONAME, Sym: &types.Sym{Name: "ptr"}}
   417  	lenVar    = Node{Op: ONAME, Sym: &types.Sym{Name: "len"}}
   418  	newlenVar = Node{Op: ONAME, Sym: &types.Sym{Name: "newlen"}}
   419  	capVar    = Node{Op: ONAME, Sym: &types.Sym{Name: "cap"}}
   420  	typVar    = Node{Op: ONAME, Sym: &types.Sym{Name: "typ"}}
   421  	okVar     = Node{Op: ONAME, Sym: &types.Sym{Name: "ok"}}
   422  )
   423  
   424  // startBlock sets the current block we're generating code in to b.
   425  func (s *state) startBlock(b *ssa.Block) {
   426  	if s.curBlock != nil {
   427  		s.Fatalf("starting block %v when block %v has not ended", b, s.curBlock)
   428  	}
   429  	s.curBlock = b
   430  	s.vars = map[*Node]*ssa.Value{}
   431  	for n := range s.fwdVars {
   432  		delete(s.fwdVars, n)
   433  	}
   434  }
   435  
   436  // endBlock marks the end of generating code for the current block.
   437  // Returns the (former) current block. Returns nil if there is no current
   438  // block, i.e. if no code flows to the current execution point.
   439  func (s *state) endBlock() *ssa.Block {
   440  	b := s.curBlock
   441  	if b == nil {
   442  		return nil
   443  	}
   444  	for len(s.defvars) <= int(b.ID) {
   445  		s.defvars = append(s.defvars, nil)
   446  	}
   447  	s.defvars[b.ID] = s.vars
   448  	s.curBlock = nil
   449  	s.vars = nil
   450  	if b.LackingPos() {
   451  		// Empty plain blocks get the line of their successor (handled after all blocks created),
   452  		// except for increment blocks in For statements (handled in ssa conversion of OFOR),
   453  		// and for blocks ending in GOTO/BREAK/CONTINUE.
   454  		b.Pos = src.NoXPos
   455  	} else {
   456  		b.Pos = s.lastPos
   457  	}
   458  	return b
   459  }
   460  
   461  // pushLine pushes a line number on the line number stack.
   462  func (s *state) pushLine(line src.XPos) {
   463  	if !line.IsKnown() {
   464  		// the frontend may emit node with line number missing,
   465  		// use the parent line number in this case.
   466  		line = s.peekPos()
   467  		if Debug['K'] != 0 {
   468  			Warn("buildssa: unknown position (line 0)")
   469  		}
   470  	} else {
   471  		s.lastPos = line
   472  	}
   473  
   474  	s.line = append(s.line, line)
   475  }
   476  
   477  // popLine pops the top of the line number stack.
   478  func (s *state) popLine() {
   479  	s.line = s.line[:len(s.line)-1]
   480  }
   481  
   482  // peekPos peeks the top of the line number stack.
   483  func (s *state) peekPos() src.XPos {
   484  	return s.line[len(s.line)-1]
   485  }
   486  
   487  // newValue0 adds a new value with no arguments to the current block.
   488  func (s *state) newValue0(op ssa.Op, t *types.Type) *ssa.Value {
   489  	return s.curBlock.NewValue0(s.peekPos(), op, t)
   490  }
   491  
   492  // newValue0A adds a new value with no arguments and an aux value to the current block.
   493  func (s *state) newValue0A(op ssa.Op, t *types.Type, aux interface{}) *ssa.Value {
   494  	return s.curBlock.NewValue0A(s.peekPos(), op, t, aux)
   495  }
   496  
   497  // newValue0I adds a new value with no arguments and an auxint value to the current block.
   498  func (s *state) newValue0I(op ssa.Op, t *types.Type, auxint int64) *ssa.Value {
   499  	return s.curBlock.NewValue0I(s.peekPos(), op, t, auxint)
   500  }
   501  
   502  // newValue1 adds a new value with one argument to the current block.
   503  func (s *state) newValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
   504  	return s.curBlock.NewValue1(s.peekPos(), op, t, arg)
   505  }
   506  
   507  // newValue1A adds a new value with one argument and an aux value to the current block.
   508  func (s *state) newValue1A(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value) *ssa.Value {
   509  	return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg)
   510  }
   511  
   512  // newValue1Apos adds a new value with one argument and an aux value to the current block.
   513  // isStmt determines whether the created values may be a statement or not
   514  // (i.e., false means never, yes means maybe).
   515  func (s *state) newValue1Apos(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value, isStmt bool) *ssa.Value {
   516  	if isStmt {
   517  		return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg)
   518  	}
   519  	return s.curBlock.NewValue1A(s.peekPos().WithNotStmt(), op, t, aux, arg)
   520  }
   521  
   522  // newValue1I adds a new value with one argument and an auxint value to the current block.
   523  func (s *state) newValue1I(op ssa.Op, t *types.Type, aux int64, arg *ssa.Value) *ssa.Value {
   524  	return s.curBlock.NewValue1I(s.peekPos(), op, t, aux, arg)
   525  }
   526  
   527  // newValue2 adds a new value with two arguments to the current block.
   528  func (s *state) newValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
   529  	return s.curBlock.NewValue2(s.peekPos(), op, t, arg0, arg1)
   530  }
   531  
   532  // newValue2Apos adds a new value with two arguments and an aux value to the current block.
   533  // isStmt determines whether the created values may be a statement or not
   534  // (i.e., false means never, yes means maybe).
   535  func (s *state) newValue2Apos(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1 *ssa.Value, isStmt bool) *ssa.Value {
   536  	if isStmt {
   537  		return s.curBlock.NewValue2A(s.peekPos(), op, t, aux, arg0, arg1)
   538  	}
   539  	return s.curBlock.NewValue2A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1)
   540  }
   541  
   542  // newValue2I adds a new value with two arguments and an auxint value to the current block.
   543  func (s *state) newValue2I(op ssa.Op, t *types.Type, aux int64, arg0, arg1 *ssa.Value) *ssa.Value {
   544  	return s.curBlock.NewValue2I(s.peekPos(), op, t, aux, arg0, arg1)
   545  }
   546  
   547  // newValue3 adds a new value with three arguments to the current block.
   548  func (s *state) newValue3(op ssa.Op, t *types.Type, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
   549  	return s.curBlock.NewValue3(s.peekPos(), op, t, arg0, arg1, arg2)
   550  }
   551  
   552  // newValue3I adds a new value with three arguments and an auxint value to the current block.
   553  func (s *state) newValue3I(op ssa.Op, t *types.Type, aux int64, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
   554  	return s.curBlock.NewValue3I(s.peekPos(), op, t, aux, arg0, arg1, arg2)
   555  }
   556  
   557  // newValue3A adds a new value with three arguments and an aux value to the current block.
   558  func (s *state) newValue3A(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
   559  	return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2)
   560  }
   561  
   562  // newValue3Apos adds a new value with three arguments and an aux value to the current block.
   563  // isStmt determines whether the created values may be a statement or not
   564  // (i.e., false means never, yes means maybe).
   565  func (s *state) newValue3Apos(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *ssa.Value, isStmt bool) *ssa.Value {
   566  	if isStmt {
   567  		return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2)
   568  	}
   569  	return s.curBlock.NewValue3A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1, arg2)
   570  }
   571  
   572  // newValue4 adds a new value with four arguments to the current block.
   573  func (s *state) newValue4(op ssa.Op, t *types.Type, arg0, arg1, arg2, arg3 *ssa.Value) *ssa.Value {
   574  	return s.curBlock.NewValue4(s.peekPos(), op, t, arg0, arg1, arg2, arg3)
   575  }
   576  
   577  // entryNewValue0 adds a new value with no arguments to the entry block.
   578  func (s *state) entryNewValue0(op ssa.Op, t *types.Type) *ssa.Value {
   579  	return s.f.Entry.NewValue0(src.NoXPos, op, t)
   580  }
   581  
   582  // entryNewValue0A adds a new value with no arguments and an aux value to the entry block.
   583  func (s *state) entryNewValue0A(op ssa.Op, t *types.Type, aux interface{}) *ssa.Value {
   584  	return s.f.Entry.NewValue0A(src.NoXPos, op, t, aux)
   585  }
   586  
   587  // entryNewValue1 adds a new value with one argument to the entry block.
   588  func (s *state) entryNewValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
   589  	return s.f.Entry.NewValue1(src.NoXPos, op, t, arg)
   590  }
   591  
   592  // entryNewValue1 adds a new value with one argument and an auxint value to the entry block.
   593  func (s *state) entryNewValue1I(op ssa.Op, t *types.Type, auxint int64, arg *ssa.Value) *ssa.Value {
   594  	return s.f.Entry.NewValue1I(src.NoXPos, op, t, auxint, arg)
   595  }
   596  
   597  // entryNewValue1A adds a new value with one argument and an aux value to the entry block.
   598  func (s *state) entryNewValue1A(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value) *ssa.Value {
   599  	return s.f.Entry.NewValue1A(src.NoXPos, op, t, aux, arg)
   600  }
   601  
   602  // entryNewValue2 adds a new value with two arguments to the entry block.
   603  func (s *state) entryNewValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
   604  	return s.f.Entry.NewValue2(src.NoXPos, op, t, arg0, arg1)
   605  }
   606  
   607  // entryNewValue2A adds a new value with two arguments and an aux value to the entry block.
   608  func (s *state) entryNewValue2A(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1 *ssa.Value) *ssa.Value {
   609  	return s.f.Entry.NewValue2A(src.NoXPos, op, t, aux, arg0, arg1)
   610  }
   611  
   612  // const* routines add a new const value to the entry block.
   613  func (s *state) constSlice(t *types.Type) *ssa.Value {
   614  	return s.f.ConstSlice(t)
   615  }
   616  func (s *state) constInterface(t *types.Type) *ssa.Value {
   617  	return s.f.ConstInterface(t)
   618  }
   619  func (s *state) constNil(t *types.Type) *ssa.Value { return s.f.ConstNil(t) }
   620  func (s *state) constEmptyString(t *types.Type) *ssa.Value {
   621  	return s.f.ConstEmptyString(t)
   622  }
   623  func (s *state) constBool(c bool) *ssa.Value {
   624  	return s.f.ConstBool(types.Types[TBOOL], c)
   625  }
   626  func (s *state) constInt8(t *types.Type, c int8) *ssa.Value {
   627  	return s.f.ConstInt8(t, c)
   628  }
   629  func (s *state) constInt16(t *types.Type, c int16) *ssa.Value {
   630  	return s.f.ConstInt16(t, c)
   631  }
   632  func (s *state) constInt32(t *types.Type, c int32) *ssa.Value {
   633  	return s.f.ConstInt32(t, c)
   634  }
   635  func (s *state) constInt64(t *types.Type, c int64) *ssa.Value {
   636  	return s.f.ConstInt64(t, c)
   637  }
   638  func (s *state) constFloat32(t *types.Type, c float64) *ssa.Value {
   639  	return s.f.ConstFloat32(t, c)
   640  }
   641  func (s *state) constFloat64(t *types.Type, c float64) *ssa.Value {
   642  	return s.f.ConstFloat64(t, c)
   643  }
   644  func (s *state) constInt(t *types.Type, c int64) *ssa.Value {
   645  	if s.config.PtrSize == 8 {
   646  		return s.constInt64(t, c)
   647  	}
   648  	if int64(int32(c)) != c {
   649  		s.Fatalf("integer constant too big %d", c)
   650  	}
   651  	return s.constInt32(t, int32(c))
   652  }
   653  func (s *state) constOffPtrSP(t *types.Type, c int64) *ssa.Value {
   654  	return s.f.ConstOffPtrSP(t, c, s.sp)
   655  }
   656  
   657  // newValueOrSfCall* are wrappers around newValue*, which may create a call to a
   658  // soft-float runtime function instead (when emitting soft-float code).
   659  func (s *state) newValueOrSfCall1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
   660  	if s.softFloat {
   661  		if c, ok := s.sfcall(op, arg); ok {
   662  			return c
   663  		}
   664  	}
   665  	return s.newValue1(op, t, arg)
   666  }
   667  func (s *state) newValueOrSfCall2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
   668  	if s.softFloat {
   669  		if c, ok := s.sfcall(op, arg0, arg1); ok {
   670  			return c
   671  		}
   672  	}
   673  	return s.newValue2(op, t, arg0, arg1)
   674  }
   675  
   676  func (s *state) instrument(t *types.Type, addr *ssa.Value, wr bool) {
   677  	if !s.curfn.Func.InstrumentBody() {
   678  		return
   679  	}
   680  
   681  	w := t.Size()
   682  	if w == 0 {
   683  		return // can't race on zero-sized things
   684  	}
   685  
   686  	if ssa.IsSanitizerSafeAddr(addr) {
   687  		return
   688  	}
   689  
   690  	var fn *obj.LSym
   691  	needWidth := false
   692  
   693  	if flag_msan {
   694  		fn = msanread
   695  		if wr {
   696  			fn = msanwrite
   697  		}
   698  		needWidth = true
   699  	} else if flag_race && t.NumComponents(types.CountBlankFields) > 1 {
   700  		// for composite objects we have to write every address
   701  		// because a write might happen to any subobject.
   702  		// composites with only one element don't have subobjects, though.
   703  		fn = racereadrange
   704  		if wr {
   705  			fn = racewriterange
   706  		}
   707  		needWidth = true
   708  	} else if flag_race {
   709  		// for non-composite objects we can write just the start
   710  		// address, as any write must write the first byte.
   711  		fn = raceread
   712  		if wr {
   713  			fn = racewrite
   714  		}
   715  	} else {
   716  		panic("unreachable")
   717  	}
   718  
   719  	args := []*ssa.Value{addr}
   720  	if needWidth {
   721  		args = append(args, s.constInt(types.Types[TUINTPTR], w))
   722  	}
   723  	s.rtcall(fn, true, nil, args...)
   724  }
   725  
   726  func (s *state) load(t *types.Type, src *ssa.Value) *ssa.Value {
   727  	s.instrument(t, src, false)
   728  	return s.rawLoad(t, src)
   729  }
   730  
   731  func (s *state) rawLoad(t *types.Type, src *ssa.Value) *ssa.Value {
   732  	return s.newValue2(ssa.OpLoad, t, src, s.mem())
   733  }
   734  
   735  func (s *state) store(t *types.Type, dst, val *ssa.Value) {
   736  	s.vars[&memVar] = s.newValue3A(ssa.OpStore, types.TypeMem, t, dst, val, s.mem())
   737  }
   738  
   739  func (s *state) zero(t *types.Type, dst *ssa.Value) {
   740  	s.instrument(t, dst, true)
   741  	store := s.newValue2I(ssa.OpZero, types.TypeMem, t.Size(), dst, s.mem())
   742  	store.Aux = t
   743  	s.vars[&memVar] = store
   744  }
   745  
   746  func (s *state) move(t *types.Type, dst, src *ssa.Value) {
   747  	s.instrument(t, src, false)
   748  	s.instrument(t, dst, true)
   749  	store := s.newValue3I(ssa.OpMove, types.TypeMem, t.Size(), dst, src, s.mem())
   750  	store.Aux = t
   751  	s.vars[&memVar] = store
   752  }
   753  
   754  // stmtList converts the statement list n to SSA and adds it to s.
   755  func (s *state) stmtList(l Nodes) {
   756  	for _, n := range l.Slice() {
   757  		s.stmt(n)
   758  	}
   759  }
   760  
   761  // stmt converts the statement n to SSA and adds it to s.
   762  func (s *state) stmt(n *Node) {
   763  	if !(n.Op == OVARKILL || n.Op == OVARLIVE || n.Op == OVARDEF) {
   764  		// OVARKILL, OVARLIVE, and OVARDEF are invisible to the programmer, so we don't use their line numbers to avoid confusion in debugging.
   765  		s.pushLine(n.Pos)
   766  		defer s.popLine()
   767  	}
   768  
   769  	// If s.curBlock is nil, and n isn't a label (which might have an associated goto somewhere),
   770  	// then this code is dead. Stop here.
   771  	if s.curBlock == nil && n.Op != OLABEL {
   772  		return
   773  	}
   774  
   775  	s.stmtList(n.Ninit)
   776  	switch n.Op {
   777  
   778  	case OBLOCK:
   779  		s.stmtList(n.List)
   780  
   781  	// No-ops
   782  	case OEMPTY, ODCLCONST, ODCLTYPE, OFALL:
   783  
   784  	// Expression statements
   785  	case OCALLFUNC:
   786  		if isIntrinsicCall(n) {
   787  			s.intrinsicCall(n)
   788  			return
   789  		}
   790  		fallthrough
   791  
   792  	case OCALLMETH, OCALLINTER:
   793  		s.call(n, callNormal)
   794  		if n.Op == OCALLFUNC && n.Left.Op == ONAME && n.Left.Class() == PFUNC {
   795  			if fn := n.Left.Sym.Name; compiling_runtime && fn == "throw" ||
   796  				n.Left.Sym.Pkg == Runtimepkg && (fn == "throwinit" || fn == "gopanic" || fn == "panicwrap" || fn == "block" || fn == "panicmakeslicelen" || fn == "panicmakeslicecap") {
   797  				m := s.mem()
   798  				b := s.endBlock()
   799  				b.Kind = ssa.BlockExit
   800  				b.SetControl(m)
   801  				// TODO: never rewrite OPANIC to OCALLFUNC in the
   802  				// first place. Need to wait until all backends
   803  				// go through SSA.
   804  			}
   805  		}
   806  	case ODEFER:
   807  		s.call(n.Left, callDefer)
   808  	case OGO:
   809  		s.call(n.Left, callGo)
   810  
   811  	case OAS2DOTTYPE:
   812  		res, resok := s.dottype(n.Rlist.First(), true)
   813  		deref := false
   814  		if !canSSAType(n.Rlist.First().Type) {
   815  			if res.Op != ssa.OpLoad {
   816  				s.Fatalf("dottype of non-load")
   817  			}
   818  			mem := s.mem()
   819  			if mem.Op == ssa.OpVarKill {
   820  				mem = mem.Args[0]
   821  			}
   822  			if res.Args[1] != mem {
   823  				s.Fatalf("memory no longer live from 2-result dottype load")
   824  			}
   825  			deref = true
   826  			res = res.Args[0]
   827  		}
   828  		s.assign(n.List.First(), res, deref, 0)
   829  		s.assign(n.List.Second(), resok, false, 0)
   830  		return
   831  
   832  	case OAS2FUNC:
   833  		// We come here only when it is an intrinsic call returning two values.
   834  		if !isIntrinsicCall(n.Rlist.First()) {
   835  			s.Fatalf("non-intrinsic AS2FUNC not expanded %v", n.Rlist.First())
   836  		}
   837  		v := s.intrinsicCall(n.Rlist.First())
   838  		v1 := s.newValue1(ssa.OpSelect0, n.List.First().Type, v)
   839  		v2 := s.newValue1(ssa.OpSelect1, n.List.Second().Type, v)
   840  		s.assign(n.List.First(), v1, false, 0)
   841  		s.assign(n.List.Second(), v2, false, 0)
   842  		return
   843  
   844  	case ODCL:
   845  		if n.Left.Class() == PAUTOHEAP {
   846  			Fatalf("DCL %v", n)
   847  		}
   848  
   849  	case OLABEL:
   850  		sym := n.Sym
   851  		lab := s.label(sym)
   852  
   853  		// Associate label with its control flow node, if any
   854  		if ctl := n.labeledControl(); ctl != nil {
   855  			s.labeledNodes[ctl] = lab
   856  		}
   857  
   858  		// The label might already have a target block via a goto.
   859  		if lab.target == nil {
   860  			lab.target = s.f.NewBlock(ssa.BlockPlain)
   861  		}
   862  
   863  		// Go to that label.
   864  		// (We pretend "label:" is preceded by "goto label", unless the predecessor is unreachable.)
   865  		if s.curBlock != nil {
   866  			b := s.endBlock()
   867  			b.AddEdgeTo(lab.target)
   868  		}
   869  		s.startBlock(lab.target)
   870  
   871  	case OGOTO:
   872  		sym := n.Sym
   873  
   874  		lab := s.label(sym)
   875  		if lab.target == nil {
   876  			lab.target = s.f.NewBlock(ssa.BlockPlain)
   877  		}
   878  
   879  		b := s.endBlock()
   880  		b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block.
   881  		b.AddEdgeTo(lab.target)
   882  
   883  	case OAS:
   884  		if n.Left == n.Right && n.Left.Op == ONAME {
   885  			// An x=x assignment. No point in doing anything
   886  			// here. In addition, skipping this assignment
   887  			// prevents generating:
   888  			//   VARDEF x
   889  			//   COPY x -> x
   890  			// which is bad because x is incorrectly considered
   891  			// dead before the vardef. See issue #14904.
   892  			return
   893  		}
   894  
   895  		// Evaluate RHS.
   896  		rhs := n.Right
   897  		if rhs != nil {
   898  			switch rhs.Op {
   899  			case OSTRUCTLIT, OARRAYLIT, OSLICELIT:
   900  				// All literals with nonzero fields have already been
   901  				// rewritten during walk. Any that remain are just T{}
   902  				// or equivalents. Use the zero value.
   903  				if !isZero(rhs) {
   904  					Fatalf("literal with nonzero value in SSA: %v", rhs)
   905  				}
   906  				rhs = nil
   907  			case OAPPEND:
   908  				// Check whether we're writing the result of an append back to the same slice.
   909  				// If so, we handle it specially to avoid write barriers on the fast
   910  				// (non-growth) path.
   911  				if !samesafeexpr(n.Left, rhs.List.First()) || Debug['N'] != 0 {
   912  					break
   913  				}
   914  				// If the slice can be SSA'd, it'll be on the stack,
   915  				// so there will be no write barriers,
   916  				// so there's no need to attempt to prevent them.
   917  				if s.canSSA(n.Left) {
   918  					if Debug_append > 0 { // replicating old diagnostic message
   919  						Warnl(n.Pos, "append: len-only update (in local slice)")
   920  					}
   921  					break
   922  				}
   923  				if Debug_append > 0 {
   924  					Warnl(n.Pos, "append: len-only update")
   925  				}
   926  				s.append(rhs, true)
   927  				return
   928  			}
   929  		}
   930  
   931  		if n.Left.isBlank() {
   932  			// _ = rhs
   933  			// Just evaluate rhs for side-effects.
   934  			if rhs != nil {
   935  				s.expr(rhs)
   936  			}
   937  			return
   938  		}
   939  
   940  		var t *types.Type
   941  		if n.Right != nil {
   942  			t = n.Right.Type
   943  		} else {
   944  			t = n.Left.Type
   945  		}
   946  
   947  		var r *ssa.Value
   948  		deref := !canSSAType(t)
   949  		if deref {
   950  			if rhs == nil {
   951  				r = nil // Signal assign to use OpZero.
   952  			} else {
   953  				r = s.addr(rhs, false)
   954  			}
   955  		} else {
   956  			if rhs == nil {
   957  				r = s.zeroVal(t)
   958  			} else {
   959  				r = s.expr(rhs)
   960  			}
   961  		}
   962  
   963  		var skip skipMask
   964  		if rhs != nil && (rhs.Op == OSLICE || rhs.Op == OSLICE3 || rhs.Op == OSLICESTR) && samesafeexpr(rhs.Left, n.Left) {
   965  			// We're assigning a slicing operation back to its source.
   966  			// Don't write back fields we aren't changing. See issue #14855.
   967  			i, j, k := rhs.SliceBounds()
   968  			if i != nil && (i.Op == OLITERAL && i.Val().Ctype() == CTINT && i.Int64() == 0) {
   969  				// [0:...] is the same as [:...]
   970  				i = nil
   971  			}
   972  			// TODO: detect defaults for len/cap also.
   973  			// Currently doesn't really work because (*p)[:len(*p)] appears here as:
   974  			//    tmp = len(*p)
   975  			//    (*p)[:tmp]
   976  			//if j != nil && (j.Op == OLEN && samesafeexpr(j.Left, n.Left)) {
   977  			//      j = nil
   978  			//}
   979  			//if k != nil && (k.Op == OCAP && samesafeexpr(k.Left, n.Left)) {
   980  			//      k = nil
   981  			//}
   982  			if i == nil {
   983  				skip |= skipPtr
   984  				if j == nil {
   985  					skip |= skipLen
   986  				}
   987  				if k == nil {
   988  					skip |= skipCap
   989  				}
   990  			}
   991  		}
   992  
   993  		s.assign(n.Left, r, deref, skip)
   994  
   995  	case OIF:
   996  		bEnd := s.f.NewBlock(ssa.BlockPlain)
   997  		var likely int8
   998  		if n.Likely() {
   999  			likely = 1
  1000  		}
  1001  		var bThen *ssa.Block
  1002  		if n.Nbody.Len() != 0 {
  1003  			bThen = s.f.NewBlock(ssa.BlockPlain)
  1004  		} else {
  1005  			bThen = bEnd
  1006  		}
  1007  		var bElse *ssa.Block
  1008  		if n.Rlist.Len() != 0 {
  1009  			bElse = s.f.NewBlock(ssa.BlockPlain)
  1010  		} else {
  1011  			bElse = bEnd
  1012  		}
  1013  		s.condBranch(n.Left, bThen, bElse, likely)
  1014  
  1015  		if n.Nbody.Len() != 0 {
  1016  			s.startBlock(bThen)
  1017  			s.stmtList(n.Nbody)
  1018  			if b := s.endBlock(); b != nil {
  1019  				b.AddEdgeTo(bEnd)
  1020  			}
  1021  		}
  1022  		if n.Rlist.Len() != 0 {
  1023  			s.startBlock(bElse)
  1024  			s.stmtList(n.Rlist)
  1025  			if b := s.endBlock(); b != nil {
  1026  				b.AddEdgeTo(bEnd)
  1027  			}
  1028  		}
  1029  		s.startBlock(bEnd)
  1030  
  1031  	case ORETURN:
  1032  		s.stmtList(n.List)
  1033  		b := s.exit()
  1034  		b.Pos = s.lastPos.WithIsStmt()
  1035  
  1036  	case ORETJMP:
  1037  		s.stmtList(n.List)
  1038  		b := s.exit()
  1039  		b.Kind = ssa.BlockRetJmp // override BlockRet
  1040  		b.Aux = n.Sym.Linksym()
  1041  
  1042  	case OCONTINUE, OBREAK:
  1043  		var to *ssa.Block
  1044  		if n.Sym == nil {
  1045  			// plain break/continue
  1046  			switch n.Op {
  1047  			case OCONTINUE:
  1048  				to = s.continueTo
  1049  			case OBREAK:
  1050  				to = s.breakTo
  1051  			}
  1052  		} else {
  1053  			// labeled break/continue; look up the target
  1054  			sym := n.Sym
  1055  			lab := s.label(sym)
  1056  			switch n.Op {
  1057  			case OCONTINUE:
  1058  				to = lab.continueTarget
  1059  			case OBREAK:
  1060  				to = lab.breakTarget
  1061  			}
  1062  		}
  1063  
  1064  		b := s.endBlock()
  1065  		b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block.
  1066  		b.AddEdgeTo(to)
  1067  
  1068  	case OFOR, OFORUNTIL:
  1069  		// OFOR: for Ninit; Left; Right { Nbody }
  1070  		// cond (Left); body (Nbody); incr (Right)
  1071  		//
  1072  		// OFORUNTIL: for Ninit; Left; Right; List { Nbody }
  1073  		// => body: { Nbody }; incr: Right; if Left { lateincr: List; goto body }; end:
  1074  		bCond := s.f.NewBlock(ssa.BlockPlain)
  1075  		bBody := s.f.NewBlock(ssa.BlockPlain)
  1076  		bIncr := s.f.NewBlock(ssa.BlockPlain)
  1077  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  1078  
  1079  		// first, jump to condition test (OFOR) or body (OFORUNTIL)
  1080  		b := s.endBlock()
  1081  		if n.Op == OFOR {
  1082  			b.AddEdgeTo(bCond)
  1083  			// generate code to test condition
  1084  			s.startBlock(bCond)
  1085  			if n.Left != nil {
  1086  				s.condBranch(n.Left, bBody, bEnd, 1)
  1087  			} else {
  1088  				b := s.endBlock()
  1089  				b.Kind = ssa.BlockPlain
  1090  				b.AddEdgeTo(bBody)
  1091  			}
  1092  
  1093  		} else {
  1094  			b.AddEdgeTo(bBody)
  1095  		}
  1096  
  1097  		// set up for continue/break in body
  1098  		prevContinue := s.continueTo
  1099  		prevBreak := s.breakTo
  1100  		s.continueTo = bIncr
  1101  		s.breakTo = bEnd
  1102  		lab := s.labeledNodes[n]
  1103  		if lab != nil {
  1104  			// labeled for loop
  1105  			lab.continueTarget = bIncr
  1106  			lab.breakTarget = bEnd
  1107  		}
  1108  
  1109  		// generate body
  1110  		s.startBlock(bBody)
  1111  		s.stmtList(n.Nbody)
  1112  
  1113  		// tear down continue/break
  1114  		s.continueTo = prevContinue
  1115  		s.breakTo = prevBreak
  1116  		if lab != nil {
  1117  			lab.continueTarget = nil
  1118  			lab.breakTarget = nil
  1119  		}
  1120  
  1121  		// done with body, goto incr
  1122  		if b := s.endBlock(); b != nil {
  1123  			b.AddEdgeTo(bIncr)
  1124  		}
  1125  
  1126  		// generate incr (and, for OFORUNTIL, condition)
  1127  		s.startBlock(bIncr)
  1128  		if n.Right != nil {
  1129  			s.stmt(n.Right)
  1130  		}
  1131  		if n.Op == OFOR {
  1132  			if b := s.endBlock(); b != nil {
  1133  				b.AddEdgeTo(bCond)
  1134  				// It can happen that bIncr ends in a block containing only VARKILL,
  1135  				// and that muddles the debugging experience.
  1136  				if n.Op != OFORUNTIL && b.Pos == src.NoXPos {
  1137  					b.Pos = bCond.Pos
  1138  				}
  1139  			}
  1140  		} else {
  1141  			// bCond is unused in OFORUNTIL, so repurpose it.
  1142  			bLateIncr := bCond
  1143  			// test condition
  1144  			s.condBranch(n.Left, bLateIncr, bEnd, 1)
  1145  			// generate late increment
  1146  			s.startBlock(bLateIncr)
  1147  			s.stmtList(n.List)
  1148  			s.endBlock().AddEdgeTo(bBody)
  1149  		}
  1150  
  1151  		s.startBlock(bEnd)
  1152  
  1153  	case OSWITCH, OSELECT:
  1154  		// These have been mostly rewritten by the front end into their Nbody fields.
  1155  		// Our main task is to correctly hook up any break statements.
  1156  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  1157  
  1158  		prevBreak := s.breakTo
  1159  		s.breakTo = bEnd
  1160  		lab := s.labeledNodes[n]
  1161  		if lab != nil {
  1162  			// labeled
  1163  			lab.breakTarget = bEnd
  1164  		}
  1165  
  1166  		// generate body code
  1167  		s.stmtList(n.Nbody)
  1168  
  1169  		s.breakTo = prevBreak
  1170  		if lab != nil {
  1171  			lab.breakTarget = nil
  1172  		}
  1173  
  1174  		// walk adds explicit OBREAK nodes to the end of all reachable code paths.
  1175  		// If we still have a current block here, then mark it unreachable.
  1176  		if s.curBlock != nil {
  1177  			m := s.mem()
  1178  			b := s.endBlock()
  1179  			b.Kind = ssa.BlockExit
  1180  			b.SetControl(m)
  1181  		}
  1182  		s.startBlock(bEnd)
  1183  
  1184  	case OVARDEF:
  1185  		if !s.canSSA(n.Left) {
  1186  			s.vars[&memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, n.Left, s.mem(), false)
  1187  		}
  1188  	case OVARKILL:
  1189  		// Insert a varkill op to record that a variable is no longer live.
  1190  		// We only care about liveness info at call sites, so putting the
  1191  		// varkill in the store chain is enough to keep it correctly ordered
  1192  		// with respect to call ops.
  1193  		if !s.canSSA(n.Left) {
  1194  			s.vars[&memVar] = s.newValue1Apos(ssa.OpVarKill, types.TypeMem, n.Left, s.mem(), false)
  1195  		}
  1196  
  1197  	case OVARLIVE:
  1198  		// Insert a varlive op to record that a variable is still live.
  1199  		if !n.Left.Addrtaken() {
  1200  			s.Fatalf("VARLIVE variable %v must have Addrtaken set", n.Left)
  1201  		}
  1202  		switch n.Left.Class() {
  1203  		case PAUTO, PPARAM, PPARAMOUT:
  1204  		default:
  1205  			s.Fatalf("VARLIVE variable %v must be Auto or Arg", n.Left)
  1206  		}
  1207  		s.vars[&memVar] = s.newValue1A(ssa.OpVarLive, types.TypeMem, n.Left, s.mem())
  1208  
  1209  	case OCHECKNIL:
  1210  		p := s.expr(n.Left)
  1211  		s.nilCheck(p)
  1212  
  1213  	case OINLMARK:
  1214  		s.newValue1I(ssa.OpInlMark, types.TypeVoid, n.Xoffset, s.mem())
  1215  
  1216  	default:
  1217  		s.Fatalf("unhandled stmt %v", n.Op)
  1218  	}
  1219  }
  1220  
  1221  // exit processes any code that needs to be generated just before returning.
  1222  // It returns a BlockRet block that ends the control flow. Its control value
  1223  // will be set to the final memory state.
  1224  func (s *state) exit() *ssa.Block {
  1225  	if s.hasdefer {
  1226  		s.rtcall(Deferreturn, true, nil)
  1227  	}
  1228  
  1229  	// Run exit code. Typically, this code copies heap-allocated PPARAMOUT
  1230  	// variables back to the stack.
  1231  	s.stmtList(s.curfn.Func.Exit)
  1232  
  1233  	// Store SSAable PPARAMOUT variables back to stack locations.
  1234  	for _, n := range s.returns {
  1235  		addr := s.decladdrs[n]
  1236  		val := s.variable(n, n.Type)
  1237  		s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, n, s.mem())
  1238  		s.store(n.Type, addr, val)
  1239  		// TODO: if val is ever spilled, we'd like to use the
  1240  		// PPARAMOUT slot for spilling it. That won't happen
  1241  		// currently.
  1242  	}
  1243  
  1244  	// Do actual return.
  1245  	m := s.mem()
  1246  	b := s.endBlock()
  1247  	b.Kind = ssa.BlockRet
  1248  	b.SetControl(m)
  1249  	return b
  1250  }
  1251  
  1252  type opAndType struct {
  1253  	op    Op
  1254  	etype types.EType
  1255  }
  1256  
  1257  var opToSSA = map[opAndType]ssa.Op{
  1258  	opAndType{OADD, TINT8}:    ssa.OpAdd8,
  1259  	opAndType{OADD, TUINT8}:   ssa.OpAdd8,
  1260  	opAndType{OADD, TINT16}:   ssa.OpAdd16,
  1261  	opAndType{OADD, TUINT16}:  ssa.OpAdd16,
  1262  	opAndType{OADD, TINT32}:   ssa.OpAdd32,
  1263  	opAndType{OADD, TUINT32}:  ssa.OpAdd32,
  1264  	opAndType{OADD, TINT64}:   ssa.OpAdd64,
  1265  	opAndType{OADD, TUINT64}:  ssa.OpAdd64,
  1266  	opAndType{OADD, TFLOAT32}: ssa.OpAdd32F,
  1267  	opAndType{OADD, TFLOAT64}: ssa.OpAdd64F,
  1268  
  1269  	opAndType{OSUB, TINT8}:    ssa.OpSub8,
  1270  	opAndType{OSUB, TUINT8}:   ssa.OpSub8,
  1271  	opAndType{OSUB, TINT16}:   ssa.OpSub16,
  1272  	opAndType{OSUB, TUINT16}:  ssa.OpSub16,
  1273  	opAndType{OSUB, TINT32}:   ssa.OpSub32,
  1274  	opAndType{OSUB, TUINT32}:  ssa.OpSub32,
  1275  	opAndType{OSUB, TINT64}:   ssa.OpSub64,
  1276  	opAndType{OSUB, TUINT64}:  ssa.OpSub64,
  1277  	opAndType{OSUB, TFLOAT32}: ssa.OpSub32F,
  1278  	opAndType{OSUB, TFLOAT64}: ssa.OpSub64F,
  1279  
  1280  	opAndType{ONOT, TBOOL}: ssa.OpNot,
  1281  
  1282  	opAndType{ONEG, TINT8}:    ssa.OpNeg8,
  1283  	opAndType{ONEG, TUINT8}:   ssa.OpNeg8,
  1284  	opAndType{ONEG, TINT16}:   ssa.OpNeg16,
  1285  	opAndType{ONEG, TUINT16}:  ssa.OpNeg16,
  1286  	opAndType{ONEG, TINT32}:   ssa.OpNeg32,
  1287  	opAndType{ONEG, TUINT32}:  ssa.OpNeg32,
  1288  	opAndType{ONEG, TINT64}:   ssa.OpNeg64,
  1289  	opAndType{ONEG, TUINT64}:  ssa.OpNeg64,
  1290  	opAndType{ONEG, TFLOAT32}: ssa.OpNeg32F,
  1291  	opAndType{ONEG, TFLOAT64}: ssa.OpNeg64F,
  1292  
  1293  	opAndType{OBITNOT, TINT8}:   ssa.OpCom8,
  1294  	opAndType{OBITNOT, TUINT8}:  ssa.OpCom8,
  1295  	opAndType{OBITNOT, TINT16}:  ssa.OpCom16,
  1296  	opAndType{OBITNOT, TUINT16}: ssa.OpCom16,
  1297  	opAndType{OBITNOT, TINT32}:  ssa.OpCom32,
  1298  	opAndType{OBITNOT, TUINT32}: ssa.OpCom32,
  1299  	opAndType{OBITNOT, TINT64}:  ssa.OpCom64,
  1300  	opAndType{OBITNOT, TUINT64}: ssa.OpCom64,
  1301  
  1302  	opAndType{OIMAG, TCOMPLEX64}:  ssa.OpComplexImag,
  1303  	opAndType{OIMAG, TCOMPLEX128}: ssa.OpComplexImag,
  1304  	opAndType{OREAL, TCOMPLEX64}:  ssa.OpComplexReal,
  1305  	opAndType{OREAL, TCOMPLEX128}: ssa.OpComplexReal,
  1306  
  1307  	opAndType{OMUL, TINT8}:    ssa.OpMul8,
  1308  	opAndType{OMUL, TUINT8}:   ssa.OpMul8,
  1309  	opAndType{OMUL, TINT16}:   ssa.OpMul16,
  1310  	opAndType{OMUL, TUINT16}:  ssa.OpMul16,
  1311  	opAndType{OMUL, TINT32}:   ssa.OpMul32,
  1312  	opAndType{OMUL, TUINT32}:  ssa.OpMul32,
  1313  	opAndType{OMUL, TINT64}:   ssa.OpMul64,
  1314  	opAndType{OMUL, TUINT64}:  ssa.OpMul64,
  1315  	opAndType{OMUL, TFLOAT32}: ssa.OpMul32F,
  1316  	opAndType{OMUL, TFLOAT64}: ssa.OpMul64F,
  1317  
  1318  	opAndType{ODIV, TFLOAT32}: ssa.OpDiv32F,
  1319  	opAndType{ODIV, TFLOAT64}: ssa.OpDiv64F,
  1320  
  1321  	opAndType{ODIV, TINT8}:   ssa.OpDiv8,
  1322  	opAndType{ODIV, TUINT8}:  ssa.OpDiv8u,
  1323  	opAndType{ODIV, TINT16}:  ssa.OpDiv16,
  1324  	opAndType{ODIV, TUINT16}: ssa.OpDiv16u,
  1325  	opAndType{ODIV, TINT32}:  ssa.OpDiv32,
  1326  	opAndType{ODIV, TUINT32}: ssa.OpDiv32u,
  1327  	opAndType{ODIV, TINT64}:  ssa.OpDiv64,
  1328  	opAndType{ODIV, TUINT64}: ssa.OpDiv64u,
  1329  
  1330  	opAndType{OMOD, TINT8}:   ssa.OpMod8,
  1331  	opAndType{OMOD, TUINT8}:  ssa.OpMod8u,
  1332  	opAndType{OMOD, TINT16}:  ssa.OpMod16,
  1333  	opAndType{OMOD, TUINT16}: ssa.OpMod16u,
  1334  	opAndType{OMOD, TINT32}:  ssa.OpMod32,
  1335  	opAndType{OMOD, TUINT32}: ssa.OpMod32u,
  1336  	opAndType{OMOD, TINT64}:  ssa.OpMod64,
  1337  	opAndType{OMOD, TUINT64}: ssa.OpMod64u,
  1338  
  1339  	opAndType{OAND, TINT8}:   ssa.OpAnd8,
  1340  	opAndType{OAND, TUINT8}:  ssa.OpAnd8,
  1341  	opAndType{OAND, TINT16}:  ssa.OpAnd16,
  1342  	opAndType{OAND, TUINT16}: ssa.OpAnd16,
  1343  	opAndType{OAND, TINT32}:  ssa.OpAnd32,
  1344  	opAndType{OAND, TUINT32}: ssa.OpAnd32,
  1345  	opAndType{OAND, TINT64}:  ssa.OpAnd64,
  1346  	opAndType{OAND, TUINT64}: ssa.OpAnd64,
  1347  
  1348  	opAndType{OOR, TINT8}:   ssa.OpOr8,
  1349  	opAndType{OOR, TUINT8}:  ssa.OpOr8,
  1350  	opAndType{OOR, TINT16}:  ssa.OpOr16,
  1351  	opAndType{OOR, TUINT16}: ssa.OpOr16,
  1352  	opAndType{OOR, TINT32}:  ssa.OpOr32,
  1353  	opAndType{OOR, TUINT32}: ssa.OpOr32,
  1354  	opAndType{OOR, TINT64}:  ssa.OpOr64,
  1355  	opAndType{OOR, TUINT64}: ssa.OpOr64,
  1356  
  1357  	opAndType{OXOR, TINT8}:   ssa.OpXor8,
  1358  	opAndType{OXOR, TUINT8}:  ssa.OpXor8,
  1359  	opAndType{OXOR, TINT16}:  ssa.OpXor16,
  1360  	opAndType{OXOR, TUINT16}: ssa.OpXor16,
  1361  	opAndType{OXOR, TINT32}:  ssa.OpXor32,
  1362  	opAndType{OXOR, TUINT32}: ssa.OpXor32,
  1363  	opAndType{OXOR, TINT64}:  ssa.OpXor64,
  1364  	opAndType{OXOR, TUINT64}: ssa.OpXor64,
  1365  
  1366  	opAndType{OEQ, TBOOL}:      ssa.OpEqB,
  1367  	opAndType{OEQ, TINT8}:      ssa.OpEq8,
  1368  	opAndType{OEQ, TUINT8}:     ssa.OpEq8,
  1369  	opAndType{OEQ, TINT16}:     ssa.OpEq16,
  1370  	opAndType{OEQ, TUINT16}:    ssa.OpEq16,
  1371  	opAndType{OEQ, TINT32}:     ssa.OpEq32,
  1372  	opAndType{OEQ, TUINT32}:    ssa.OpEq32,
  1373  	opAndType{OEQ, TINT64}:     ssa.OpEq64,
  1374  	opAndType{OEQ, TUINT64}:    ssa.OpEq64,
  1375  	opAndType{OEQ, TINTER}:     ssa.OpEqInter,
  1376  	opAndType{OEQ, TSLICE}:     ssa.OpEqSlice,
  1377  	opAndType{OEQ, TFUNC}:      ssa.OpEqPtr,
  1378  	opAndType{OEQ, TMAP}:       ssa.OpEqPtr,
  1379  	opAndType{OEQ, TCHAN}:      ssa.OpEqPtr,
  1380  	opAndType{OEQ, TPTR}:       ssa.OpEqPtr,
  1381  	opAndType{OEQ, TUINTPTR}:   ssa.OpEqPtr,
  1382  	opAndType{OEQ, TUNSAFEPTR}: ssa.OpEqPtr,
  1383  	opAndType{OEQ, TFLOAT64}:   ssa.OpEq64F,
  1384  	opAndType{OEQ, TFLOAT32}:   ssa.OpEq32F,
  1385  
  1386  	opAndType{ONE, TBOOL}:      ssa.OpNeqB,
  1387  	opAndType{ONE, TINT8}:      ssa.OpNeq8,
  1388  	opAndType{ONE, TUINT8}:     ssa.OpNeq8,
  1389  	opAndType{ONE, TINT16}:     ssa.OpNeq16,
  1390  	opAndType{ONE, TUINT16}:    ssa.OpNeq16,
  1391  	opAndType{ONE, TINT32}:     ssa.OpNeq32,
  1392  	opAndType{ONE, TUINT32}:    ssa.OpNeq32,
  1393  	opAndType{ONE, TINT64}:     ssa.OpNeq64,
  1394  	opAndType{ONE, TUINT64}:    ssa.OpNeq64,
  1395  	opAndType{ONE, TINTER}:     ssa.OpNeqInter,
  1396  	opAndType{ONE, TSLICE}:     ssa.OpNeqSlice,
  1397  	opAndType{ONE, TFUNC}:      ssa.OpNeqPtr,
  1398  	opAndType{ONE, TMAP}:       ssa.OpNeqPtr,
  1399  	opAndType{ONE, TCHAN}:      ssa.OpNeqPtr,
  1400  	opAndType{ONE, TPTR}:       ssa.OpNeqPtr,
  1401  	opAndType{ONE, TUINTPTR}:   ssa.OpNeqPtr,
  1402  	opAndType{ONE, TUNSAFEPTR}: ssa.OpNeqPtr,
  1403  	opAndType{ONE, TFLOAT64}:   ssa.OpNeq64F,
  1404  	opAndType{ONE, TFLOAT32}:   ssa.OpNeq32F,
  1405  
  1406  	opAndType{OLT, TINT8}:    ssa.OpLess8,
  1407  	opAndType{OLT, TUINT8}:   ssa.OpLess8U,
  1408  	opAndType{OLT, TINT16}:   ssa.OpLess16,
  1409  	opAndType{OLT, TUINT16}:  ssa.OpLess16U,
  1410  	opAndType{OLT, TINT32}:   ssa.OpLess32,
  1411  	opAndType{OLT, TUINT32}:  ssa.OpLess32U,
  1412  	opAndType{OLT, TINT64}:   ssa.OpLess64,
  1413  	opAndType{OLT, TUINT64}:  ssa.OpLess64U,
  1414  	opAndType{OLT, TFLOAT64}: ssa.OpLess64F,
  1415  	opAndType{OLT, TFLOAT32}: ssa.OpLess32F,
  1416  
  1417  	opAndType{OGT, TINT8}:    ssa.OpGreater8,
  1418  	opAndType{OGT, TUINT8}:   ssa.OpGreater8U,
  1419  	opAndType{OGT, TINT16}:   ssa.OpGreater16,
  1420  	opAndType{OGT, TUINT16}:  ssa.OpGreater16U,
  1421  	opAndType{OGT, TINT32}:   ssa.OpGreater32,
  1422  	opAndType{OGT, TUINT32}:  ssa.OpGreater32U,
  1423  	opAndType{OGT, TINT64}:   ssa.OpGreater64,
  1424  	opAndType{OGT, TUINT64}:  ssa.OpGreater64U,
  1425  	opAndType{OGT, TFLOAT64}: ssa.OpGreater64F,
  1426  	opAndType{OGT, TFLOAT32}: ssa.OpGreater32F,
  1427  
  1428  	opAndType{OLE, TINT8}:    ssa.OpLeq8,
  1429  	opAndType{OLE, TUINT8}:   ssa.OpLeq8U,
  1430  	opAndType{OLE, TINT16}:   ssa.OpLeq16,
  1431  	opAndType{OLE, TUINT16}:  ssa.OpLeq16U,
  1432  	opAndType{OLE, TINT32}:   ssa.OpLeq32,
  1433  	opAndType{OLE, TUINT32}:  ssa.OpLeq32U,
  1434  	opAndType{OLE, TINT64}:   ssa.OpLeq64,
  1435  	opAndType{OLE, TUINT64}:  ssa.OpLeq64U,
  1436  	opAndType{OLE, TFLOAT64}: ssa.OpLeq64F,
  1437  	opAndType{OLE, TFLOAT32}: ssa.OpLeq32F,
  1438  
  1439  	opAndType{OGE, TINT8}:    ssa.OpGeq8,
  1440  	opAndType{OGE, TUINT8}:   ssa.OpGeq8U,
  1441  	opAndType{OGE, TINT16}:   ssa.OpGeq16,
  1442  	opAndType{OGE, TUINT16}:  ssa.OpGeq16U,
  1443  	opAndType{OGE, TINT32}:   ssa.OpGeq32,
  1444  	opAndType{OGE, TUINT32}:  ssa.OpGeq32U,
  1445  	opAndType{OGE, TINT64}:   ssa.OpGeq64,
  1446  	opAndType{OGE, TUINT64}:  ssa.OpGeq64U,
  1447  	opAndType{OGE, TFLOAT64}: ssa.OpGeq64F,
  1448  	opAndType{OGE, TFLOAT32}: ssa.OpGeq32F,
  1449  }
  1450  
  1451  func (s *state) concreteEtype(t *types.Type) types.EType {
  1452  	e := t.Etype
  1453  	switch e {
  1454  	default:
  1455  		return e
  1456  	case TINT:
  1457  		if s.config.PtrSize == 8 {
  1458  			return TINT64
  1459  		}
  1460  		return TINT32
  1461  	case TUINT:
  1462  		if s.config.PtrSize == 8 {
  1463  			return TUINT64
  1464  		}
  1465  		return TUINT32
  1466  	case TUINTPTR:
  1467  		if s.config.PtrSize == 8 {
  1468  			return TUINT64
  1469  		}
  1470  		return TUINT32
  1471  	}
  1472  }
  1473  
  1474  func (s *state) ssaOp(op Op, t *types.Type) ssa.Op {
  1475  	etype := s.concreteEtype(t)
  1476  	x, ok := opToSSA[opAndType{op, etype}]
  1477  	if !ok {
  1478  		s.Fatalf("unhandled binary op %v %s", op, etype)
  1479  	}
  1480  	return x
  1481  }
  1482  
  1483  func floatForComplex(t *types.Type) *types.Type {
  1484  	if t.Size() == 8 {
  1485  		return types.Types[TFLOAT32]
  1486  	} else {
  1487  		return types.Types[TFLOAT64]
  1488  	}
  1489  }
  1490  
  1491  type opAndTwoTypes struct {
  1492  	op     Op
  1493  	etype1 types.EType
  1494  	etype2 types.EType
  1495  }
  1496  
  1497  type twoTypes struct {
  1498  	etype1 types.EType
  1499  	etype2 types.EType
  1500  }
  1501  
  1502  type twoOpsAndType struct {
  1503  	op1              ssa.Op
  1504  	op2              ssa.Op
  1505  	intermediateType types.EType
  1506  }
  1507  
  1508  var fpConvOpToSSA = map[twoTypes]twoOpsAndType{
  1509  
  1510  	twoTypes{TINT8, TFLOAT32}:  twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to32F, TINT32},
  1511  	twoTypes{TINT16, TFLOAT32}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to32F, TINT32},
  1512  	twoTypes{TINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to32F, TINT32},
  1513  	twoTypes{TINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to32F, TINT64},
  1514  
  1515  	twoTypes{TINT8, TFLOAT64}:  twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to64F, TINT32},
  1516  	twoTypes{TINT16, TFLOAT64}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to64F, TINT32},
  1517  	twoTypes{TINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to64F, TINT32},
  1518  	twoTypes{TINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to64F, TINT64},
  1519  
  1520  	twoTypes{TFLOAT32, TINT8}:  twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32},
  1521  	twoTypes{TFLOAT32, TINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32},
  1522  	twoTypes{TFLOAT32, TINT32}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpCopy, TINT32},
  1523  	twoTypes{TFLOAT32, TINT64}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpCopy, TINT64},
  1524  
  1525  	twoTypes{TFLOAT64, TINT8}:  twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32},
  1526  	twoTypes{TFLOAT64, TINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32},
  1527  	twoTypes{TFLOAT64, TINT32}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpCopy, TINT32},
  1528  	twoTypes{TFLOAT64, TINT64}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpCopy, TINT64},
  1529  	// unsigned
  1530  	twoTypes{TUINT8, TFLOAT32}:  twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to32F, TINT32},
  1531  	twoTypes{TUINT16, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to32F, TINT32},
  1532  	twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to32F, TINT64}, // go wide to dodge unsigned
  1533  	twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64},            // Cvt64Uto32F, branchy code expansion instead
  1534  
  1535  	twoTypes{TUINT8, TFLOAT64}:  twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to64F, TINT32},
  1536  	twoTypes{TUINT16, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to64F, TINT32},
  1537  	twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to64F, TINT64}, // go wide to dodge unsigned
  1538  	twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64},            // Cvt64Uto64F, branchy code expansion instead
  1539  
  1540  	twoTypes{TFLOAT32, TUINT8}:  twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32},
  1541  	twoTypes{TFLOAT32, TUINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32},
  1542  	twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned
  1543  	twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64},          // Cvt32Fto64U, branchy code expansion instead
  1544  
  1545  	twoTypes{TFLOAT64, TUINT8}:  twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32},
  1546  	twoTypes{TFLOAT64, TUINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32},
  1547  	twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned
  1548  	twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64},          // Cvt64Fto64U, branchy code expansion instead
  1549  
  1550  	// float
  1551  	twoTypes{TFLOAT64, TFLOAT32}: twoOpsAndType{ssa.OpCvt64Fto32F, ssa.OpCopy, TFLOAT32},
  1552  	twoTypes{TFLOAT64, TFLOAT64}: twoOpsAndType{ssa.OpRound64F, ssa.OpCopy, TFLOAT64},
  1553  	twoTypes{TFLOAT32, TFLOAT32}: twoOpsAndType{ssa.OpRound32F, ssa.OpCopy, TFLOAT32},
  1554  	twoTypes{TFLOAT32, TFLOAT64}: twoOpsAndType{ssa.OpCvt32Fto64F, ssa.OpCopy, TFLOAT64},
  1555  }
  1556  
  1557  // this map is used only for 32-bit arch, and only includes the difference
  1558  // on 32-bit arch, don't use int64<->float conversion for uint32
  1559  var fpConvOpToSSA32 = map[twoTypes]twoOpsAndType{
  1560  	twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32Uto32F, TUINT32},
  1561  	twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32Uto64F, TUINT32},
  1562  	twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto32U, ssa.OpCopy, TUINT32},
  1563  	twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto32U, ssa.OpCopy, TUINT32},
  1564  }
  1565  
  1566  // uint64<->float conversions, only on machines that have intructions for that
  1567  var uint64fpConvOpToSSA = map[twoTypes]twoOpsAndType{
  1568  	twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64Uto32F, TUINT64},
  1569  	twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64Uto64F, TUINT64},
  1570  	twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpCvt32Fto64U, ssa.OpCopy, TUINT64},
  1571  	twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpCvt64Fto64U, ssa.OpCopy, TUINT64},
  1572  }
  1573  
  1574  var shiftOpToSSA = map[opAndTwoTypes]ssa.Op{
  1575  	opAndTwoTypes{OLSH, TINT8, TUINT8}:   ssa.OpLsh8x8,
  1576  	opAndTwoTypes{OLSH, TUINT8, TUINT8}:  ssa.OpLsh8x8,
  1577  	opAndTwoTypes{OLSH, TINT8, TUINT16}:  ssa.OpLsh8x16,
  1578  	opAndTwoTypes{OLSH, TUINT8, TUINT16}: ssa.OpLsh8x16,
  1579  	opAndTwoTypes{OLSH, TINT8, TUINT32}:  ssa.OpLsh8x32,
  1580  	opAndTwoTypes{OLSH, TUINT8, TUINT32}: ssa.OpLsh8x32,
  1581  	opAndTwoTypes{OLSH, TINT8, TUINT64}:  ssa.OpLsh8x64,
  1582  	opAndTwoTypes{OLSH, TUINT8, TUINT64}: ssa.OpLsh8x64,
  1583  
  1584  	opAndTwoTypes{OLSH, TINT16, TUINT8}:   ssa.OpLsh16x8,
  1585  	opAndTwoTypes{OLSH, TUINT16, TUINT8}:  ssa.OpLsh16x8,
  1586  	opAndTwoTypes{OLSH, TINT16, TUINT16}:  ssa.OpLsh16x16,
  1587  	opAndTwoTypes{OLSH, TUINT16, TUINT16}: ssa.OpLsh16x16,
  1588  	opAndTwoTypes{OLSH, TINT16, TUINT32}:  ssa.OpLsh16x32,
  1589  	opAndTwoTypes{OLSH, TUINT16, TUINT32}: ssa.OpLsh16x32,
  1590  	opAndTwoTypes{OLSH, TINT16, TUINT64}:  ssa.OpLsh16x64,
  1591  	opAndTwoTypes{OLSH, TUINT16, TUINT64}: ssa.OpLsh16x64,
  1592  
  1593  	opAndTwoTypes{OLSH, TINT32, TUINT8}:   ssa.OpLsh32x8,
  1594  	opAndTwoTypes{OLSH, TUINT32, TUINT8}:  ssa.OpLsh32x8,
  1595  	opAndTwoTypes{OLSH, TINT32, TUINT16}:  ssa.OpLsh32x16,
  1596  	opAndTwoTypes{OLSH, TUINT32, TUINT16}: ssa.OpLsh32x16,
  1597  	opAndTwoTypes{OLSH, TINT32, TUINT32}:  ssa.OpLsh32x32,
  1598  	opAndTwoTypes{OLSH, TUINT32, TUINT32}: ssa.OpLsh32x32,
  1599  	opAndTwoTypes{OLSH, TINT32, TUINT64}:  ssa.OpLsh32x64,
  1600  	opAndTwoTypes{OLSH, TUINT32, TUINT64}: ssa.OpLsh32x64,
  1601  
  1602  	opAndTwoTypes{OLSH, TINT64, TUINT8}:   ssa.OpLsh64x8,
  1603  	opAndTwoTypes{OLSH, TUINT64, TUINT8}:  ssa.OpLsh64x8,
  1604  	opAndTwoTypes{OLSH, TINT64, TUINT16}:  ssa.OpLsh64x16,
  1605  	opAndTwoTypes{OLSH, TUINT64, TUINT16}: ssa.OpLsh64x16,
  1606  	opAndTwoTypes{OLSH, TINT64, TUINT32}:  ssa.OpLsh64x32,
  1607  	opAndTwoTypes{OLSH, TUINT64, TUINT32}: ssa.OpLsh64x32,
  1608  	opAndTwoTypes{OLSH, TINT64, TUINT64}:  ssa.OpLsh64x64,
  1609  	opAndTwoTypes{OLSH, TUINT64, TUINT64}: ssa.OpLsh64x64,
  1610  
  1611  	opAndTwoTypes{ORSH, TINT8, TUINT8}:   ssa.OpRsh8x8,
  1612  	opAndTwoTypes{ORSH, TUINT8, TUINT8}:  ssa.OpRsh8Ux8,
  1613  	opAndTwoTypes{ORSH, TINT8, TUINT16}:  ssa.OpRsh8x16,
  1614  	opAndTwoTypes{ORSH, TUINT8, TUINT16}: ssa.OpRsh8Ux16,
  1615  	opAndTwoTypes{ORSH, TINT8, TUINT32}:  ssa.OpRsh8x32,
  1616  	opAndTwoTypes{ORSH, TUINT8, TUINT32}: ssa.OpRsh8Ux32,
  1617  	opAndTwoTypes{ORSH, TINT8, TUINT64}:  ssa.OpRsh8x64,
  1618  	opAndTwoTypes{ORSH, TUINT8, TUINT64}: ssa.OpRsh8Ux64,
  1619  
  1620  	opAndTwoTypes{ORSH, TINT16, TUINT8}:   ssa.OpRsh16x8,
  1621  	opAndTwoTypes{ORSH, TUINT16, TUINT8}:  ssa.OpRsh16Ux8,
  1622  	opAndTwoTypes{ORSH, TINT16, TUINT16}:  ssa.OpRsh16x16,
  1623  	opAndTwoTypes{ORSH, TUINT16, TUINT16}: ssa.OpRsh16Ux16,
  1624  	opAndTwoTypes{ORSH, TINT16, TUINT32}:  ssa.OpRsh16x32,
  1625  	opAndTwoTypes{ORSH, TUINT16, TUINT32}: ssa.OpRsh16Ux32,
  1626  	opAndTwoTypes{ORSH, TINT16, TUINT64}:  ssa.OpRsh16x64,
  1627  	opAndTwoTypes{ORSH, TUINT16, TUINT64}: ssa.OpRsh16Ux64,
  1628  
  1629  	opAndTwoTypes{ORSH, TINT32, TUINT8}:   ssa.OpRsh32x8,
  1630  	opAndTwoTypes{ORSH, TUINT32, TUINT8}:  ssa.OpRsh32Ux8,
  1631  	opAndTwoTypes{ORSH, TINT32, TUINT16}:  ssa.OpRsh32x16,
  1632  	opAndTwoTypes{ORSH, TUINT32, TUINT16}: ssa.OpRsh32Ux16,
  1633  	opAndTwoTypes{ORSH, TINT32, TUINT32}:  ssa.OpRsh32x32,
  1634  	opAndTwoTypes{ORSH, TUINT32, TUINT32}: ssa.OpRsh32Ux32,
  1635  	opAndTwoTypes{ORSH, TINT32, TUINT64}:  ssa.OpRsh32x64,
  1636  	opAndTwoTypes{ORSH, TUINT32, TUINT64}: ssa.OpRsh32Ux64,
  1637  
  1638  	opAndTwoTypes{ORSH, TINT64, TUINT8}:   ssa.OpRsh64x8,
  1639  	opAndTwoTypes{ORSH, TUINT64, TUINT8}:  ssa.OpRsh64Ux8,
  1640  	opAndTwoTypes{ORSH, TINT64, TUINT16}:  ssa.OpRsh64x16,
  1641  	opAndTwoTypes{ORSH, TUINT64, TUINT16}: ssa.OpRsh64Ux16,
  1642  	opAndTwoTypes{ORSH, TINT64, TUINT32}:  ssa.OpRsh64x32,
  1643  	opAndTwoTypes{ORSH, TUINT64, TUINT32}: ssa.OpRsh64Ux32,
  1644  	opAndTwoTypes{ORSH, TINT64, TUINT64}:  ssa.OpRsh64x64,
  1645  	opAndTwoTypes{ORSH, TUINT64, TUINT64}: ssa.OpRsh64Ux64,
  1646  }
  1647  
  1648  func (s *state) ssaShiftOp(op Op, t *types.Type, u *types.Type) ssa.Op {
  1649  	etype1 := s.concreteEtype(t)
  1650  	etype2 := s.concreteEtype(u)
  1651  	x, ok := shiftOpToSSA[opAndTwoTypes{op, etype1, etype2}]
  1652  	if !ok {
  1653  		s.Fatalf("unhandled shift op %v etype=%s/%s", op, etype1, etype2)
  1654  	}
  1655  	return x
  1656  }
  1657  
  1658  // expr converts the expression n to ssa, adds it to s and returns the ssa result.
  1659  func (s *state) expr(n *Node) *ssa.Value {
  1660  	if !(n.Op == ONAME || n.Op == OLITERAL && n.Sym != nil) {
  1661  		// ONAMEs and named OLITERALs have the line number
  1662  		// of the decl, not the use. See issue 14742.
  1663  		s.pushLine(n.Pos)
  1664  		defer s.popLine()
  1665  	}
  1666  
  1667  	s.stmtList(n.Ninit)
  1668  	switch n.Op {
  1669  	case OBYTES2STRTMP:
  1670  		slice := s.expr(n.Left)
  1671  		ptr := s.newValue1(ssa.OpSlicePtr, s.f.Config.Types.BytePtr, slice)
  1672  		len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice)
  1673  		return s.newValue2(ssa.OpStringMake, n.Type, ptr, len)
  1674  	case OSTR2BYTESTMP:
  1675  		str := s.expr(n.Left)
  1676  		ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, str)
  1677  		len := s.newValue1(ssa.OpStringLen, types.Types[TINT], str)
  1678  		return s.newValue3(ssa.OpSliceMake, n.Type, ptr, len, len)
  1679  	case OCFUNC:
  1680  		aux := n.Left.Sym.Linksym()
  1681  		return s.entryNewValue1A(ssa.OpAddr, n.Type, aux, s.sb)
  1682  	case ONAME:
  1683  		if n.Class() == PFUNC {
  1684  			// "value" of a function is the address of the function's closure
  1685  			sym := funcsym(n.Sym).Linksym()
  1686  			return s.entryNewValue1A(ssa.OpAddr, types.NewPtr(n.Type), sym, s.sb)
  1687  		}
  1688  		if s.canSSA(n) {
  1689  			return s.variable(n, n.Type)
  1690  		}
  1691  		addr := s.addr(n, false)
  1692  		return s.load(n.Type, addr)
  1693  	case OCLOSUREVAR:
  1694  		addr := s.addr(n, false)
  1695  		return s.load(n.Type, addr)
  1696  	case OLITERAL:
  1697  		switch u := n.Val().U.(type) {
  1698  		case *Mpint:
  1699  			i := u.Int64()
  1700  			switch n.Type.Size() {
  1701  			case 1:
  1702  				return s.constInt8(n.Type, int8(i))
  1703  			case 2:
  1704  				return s.constInt16(n.Type, int16(i))
  1705  			case 4:
  1706  				return s.constInt32(n.Type, int32(i))
  1707  			case 8:
  1708  				return s.constInt64(n.Type, i)
  1709  			default:
  1710  				s.Fatalf("bad integer size %d", n.Type.Size())
  1711  				return nil
  1712  			}
  1713  		case string:
  1714  			if u == "" {
  1715  				return s.constEmptyString(n.Type)
  1716  			}
  1717  			return s.entryNewValue0A(ssa.OpConstString, n.Type, u)
  1718  		case bool:
  1719  			return s.constBool(u)
  1720  		case *NilVal:
  1721  			t := n.Type
  1722  			switch {
  1723  			case t.IsSlice():
  1724  				return s.constSlice(t)
  1725  			case t.IsInterface():
  1726  				return s.constInterface(t)
  1727  			default:
  1728  				return s.constNil(t)
  1729  			}
  1730  		case *Mpflt:
  1731  			switch n.Type.Size() {
  1732  			case 4:
  1733  				return s.constFloat32(n.Type, u.Float32())
  1734  			case 8:
  1735  				return s.constFloat64(n.Type, u.Float64())
  1736  			default:
  1737  				s.Fatalf("bad float size %d", n.Type.Size())
  1738  				return nil
  1739  			}
  1740  		case *Mpcplx:
  1741  			r := &u.Real
  1742  			i := &u.Imag
  1743  			switch n.Type.Size() {
  1744  			case 8:
  1745  				pt := types.Types[TFLOAT32]
  1746  				return s.newValue2(ssa.OpComplexMake, n.Type,
  1747  					s.constFloat32(pt, r.Float32()),
  1748  					s.constFloat32(pt, i.Float32()))
  1749  			case 16:
  1750  				pt := types.Types[TFLOAT64]
  1751  				return s.newValue2(ssa.OpComplexMake, n.Type,
  1752  					s.constFloat64(pt, r.Float64()),
  1753  					s.constFloat64(pt, i.Float64()))
  1754  			default:
  1755  				s.Fatalf("bad float size %d", n.Type.Size())
  1756  				return nil
  1757  			}
  1758  
  1759  		default:
  1760  			s.Fatalf("unhandled OLITERAL %v", n.Val().Ctype())
  1761  			return nil
  1762  		}
  1763  	case OCONVNOP:
  1764  		to := n.Type
  1765  		from := n.Left.Type
  1766  
  1767  		// Assume everything will work out, so set up our return value.
  1768  		// Anything interesting that happens from here is a fatal.
  1769  		x := s.expr(n.Left)
  1770  
  1771  		// Special case for not confusing GC and liveness.
  1772  		// We don't want pointers accidentally classified
  1773  		// as not-pointers or vice-versa because of copy
  1774  		// elision.
  1775  		if to.IsPtrShaped() != from.IsPtrShaped() {
  1776  			return s.newValue2(ssa.OpConvert, to, x, s.mem())
  1777  		}
  1778  
  1779  		v := s.newValue1(ssa.OpCopy, to, x) // ensure that v has the right type
  1780  
  1781  		// CONVNOP closure
  1782  		if to.Etype == TFUNC && from.IsPtrShaped() {
  1783  			return v
  1784  		}
  1785  
  1786  		// named <--> unnamed type or typed <--> untyped const
  1787  		if from.Etype == to.Etype {
  1788  			return v
  1789  		}
  1790  
  1791  		// unsafe.Pointer <--> *T
  1792  		if to.Etype == TUNSAFEPTR && from.IsPtrShaped() || from.Etype == TUNSAFEPTR && to.IsPtrShaped() {
  1793  			return v
  1794  		}
  1795  
  1796  		// map <--> *hmap
  1797  		if to.Etype == TMAP && from.IsPtr() &&
  1798  			to.MapType().Hmap == from.Elem() {
  1799  			return v
  1800  		}
  1801  
  1802  		dowidth(from)
  1803  		dowidth(to)
  1804  		if from.Width != to.Width {
  1805  			s.Fatalf("CONVNOP width mismatch %v (%d) -> %v (%d)\n", from, from.Width, to, to.Width)
  1806  			return nil
  1807  		}
  1808  		if etypesign(from.Etype) != etypesign(to.Etype) {
  1809  			s.Fatalf("CONVNOP sign mismatch %v (%s) -> %v (%s)\n", from, from.Etype, to, to.Etype)
  1810  			return nil
  1811  		}
  1812  
  1813  		if instrumenting {
  1814  			// These appear to be fine, but they fail the
  1815  			// integer constraint below, so okay them here.
  1816  			// Sample non-integer conversion: map[string]string -> *uint8
  1817  			return v
  1818  		}
  1819  
  1820  		if etypesign(from.Etype) == 0 {
  1821  			s.Fatalf("CONVNOP unrecognized non-integer %v -> %v\n", from, to)
  1822  			return nil
  1823  		}
  1824  
  1825  		// integer, same width, same sign
  1826  		return v
  1827  
  1828  	case OCONV:
  1829  		x := s.expr(n.Left)
  1830  		ft := n.Left.Type // from type
  1831  		tt := n.Type      // to type
  1832  		if ft.IsBoolean() && tt.IsKind(TUINT8) {
  1833  			// Bool -> uint8 is generated internally when indexing into runtime.staticbyte.
  1834  			return s.newValue1(ssa.OpCopy, n.Type, x)
  1835  		}
  1836  		if ft.IsInteger() && tt.IsInteger() {
  1837  			var op ssa.Op
  1838  			if tt.Size() == ft.Size() {
  1839  				op = ssa.OpCopy
  1840  			} else if tt.Size() < ft.Size() {
  1841  				// truncation
  1842  				switch 10*ft.Size() + tt.Size() {
  1843  				case 21:
  1844  					op = ssa.OpTrunc16to8
  1845  				case 41:
  1846  					op = ssa.OpTrunc32to8
  1847  				case 42:
  1848  					op = ssa.OpTrunc32to16
  1849  				case 81:
  1850  					op = ssa.OpTrunc64to8
  1851  				case 82:
  1852  					op = ssa.OpTrunc64to16
  1853  				case 84:
  1854  					op = ssa.OpTrunc64to32
  1855  				default:
  1856  					s.Fatalf("weird integer truncation %v -> %v", ft, tt)
  1857  				}
  1858  			} else if ft.IsSigned() {
  1859  				// sign extension
  1860  				switch 10*ft.Size() + tt.Size() {
  1861  				case 12:
  1862  					op = ssa.OpSignExt8to16
  1863  				case 14:
  1864  					op = ssa.OpSignExt8to32
  1865  				case 18:
  1866  					op = ssa.OpSignExt8to64
  1867  				case 24:
  1868  					op = ssa.OpSignExt16to32
  1869  				case 28:
  1870  					op = ssa.OpSignExt16to64
  1871  				case 48:
  1872  					op = ssa.OpSignExt32to64
  1873  				default:
  1874  					s.Fatalf("bad integer sign extension %v -> %v", ft, tt)
  1875  				}
  1876  			} else {
  1877  				// zero extension
  1878  				switch 10*ft.Size() + tt.Size() {
  1879  				case 12:
  1880  					op = ssa.OpZeroExt8to16
  1881  				case 14:
  1882  					op = ssa.OpZeroExt8to32
  1883  				case 18:
  1884  					op = ssa.OpZeroExt8to64
  1885  				case 24:
  1886  					op = ssa.OpZeroExt16to32
  1887  				case 28:
  1888  					op = ssa.OpZeroExt16to64
  1889  				case 48:
  1890  					op = ssa.OpZeroExt32to64
  1891  				default:
  1892  					s.Fatalf("weird integer sign extension %v -> %v", ft, tt)
  1893  				}
  1894  			}
  1895  			return s.newValue1(op, n.Type, x)
  1896  		}
  1897  
  1898  		if ft.IsFloat() || tt.IsFloat() {
  1899  			conv, ok := fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]
  1900  			if s.config.RegSize == 4 && thearch.LinkArch.Family != sys.MIPS && !s.softFloat {
  1901  				if conv1, ok1 := fpConvOpToSSA32[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 {
  1902  					conv = conv1
  1903  				}
  1904  			}
  1905  			if thearch.LinkArch.Family == sys.ARM64 || thearch.LinkArch.Family == sys.Wasm || s.softFloat {
  1906  				if conv1, ok1 := uint64fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 {
  1907  					conv = conv1
  1908  				}
  1909  			}
  1910  
  1911  			if thearch.LinkArch.Family == sys.MIPS && !s.softFloat {
  1912  				if ft.Size() == 4 && ft.IsInteger() && !ft.IsSigned() {
  1913  					// tt is float32 or float64, and ft is also unsigned
  1914  					if tt.Size() == 4 {
  1915  						return s.uint32Tofloat32(n, x, ft, tt)
  1916  					}
  1917  					if tt.Size() == 8 {
  1918  						return s.uint32Tofloat64(n, x, ft, tt)
  1919  					}
  1920  				} else if tt.Size() == 4 && tt.IsInteger() && !tt.IsSigned() {
  1921  					// ft is float32 or float64, and tt is unsigned integer
  1922  					if ft.Size() == 4 {
  1923  						return s.float32ToUint32(n, x, ft, tt)
  1924  					}
  1925  					if ft.Size() == 8 {
  1926  						return s.float64ToUint32(n, x, ft, tt)
  1927  					}
  1928  				}
  1929  			}
  1930  
  1931  			if !ok {
  1932  				s.Fatalf("weird float conversion %v -> %v", ft, tt)
  1933  			}
  1934  			op1, op2, it := conv.op1, conv.op2, conv.intermediateType
  1935  
  1936  			if op1 != ssa.OpInvalid && op2 != ssa.OpInvalid {
  1937  				// normal case, not tripping over unsigned 64
  1938  				if op1 == ssa.OpCopy {
  1939  					if op2 == ssa.OpCopy {
  1940  						return x
  1941  					}
  1942  					return s.newValueOrSfCall1(op2, n.Type, x)
  1943  				}
  1944  				if op2 == ssa.OpCopy {
  1945  					return s.newValueOrSfCall1(op1, n.Type, x)
  1946  				}
  1947  				return s.newValueOrSfCall1(op2, n.Type, s.newValueOrSfCall1(op1, types.Types[it], x))
  1948  			}
  1949  			// Tricky 64-bit unsigned cases.
  1950  			if ft.IsInteger() {
  1951  				// tt is float32 or float64, and ft is also unsigned
  1952  				if tt.Size() == 4 {
  1953  					return s.uint64Tofloat32(n, x, ft, tt)
  1954  				}
  1955  				if tt.Size() == 8 {
  1956  					return s.uint64Tofloat64(n, x, ft, tt)
  1957  				}
  1958  				s.Fatalf("weird unsigned integer to float conversion %v -> %v", ft, tt)
  1959  			}
  1960  			// ft is float32 or float64, and tt is unsigned integer
  1961  			if ft.Size() == 4 {
  1962  				return s.float32ToUint64(n, x, ft, tt)
  1963  			}
  1964  			if ft.Size() == 8 {
  1965  				return s.float64ToUint64(n, x, ft, tt)
  1966  			}
  1967  			s.Fatalf("weird float to unsigned integer conversion %v -> %v", ft, tt)
  1968  			return nil
  1969  		}
  1970  
  1971  		if ft.IsComplex() && tt.IsComplex() {
  1972  			var op ssa.Op
  1973  			if ft.Size() == tt.Size() {
  1974  				switch ft.Size() {
  1975  				case 8:
  1976  					op = ssa.OpRound32F
  1977  				case 16:
  1978  					op = ssa.OpRound64F
  1979  				default:
  1980  					s.Fatalf("weird complex conversion %v -> %v", ft, tt)
  1981  				}
  1982  			} else if ft.Size() == 8 && tt.Size() == 16 {
  1983  				op = ssa.OpCvt32Fto64F
  1984  			} else if ft.Size() == 16 && tt.Size() == 8 {
  1985  				op = ssa.OpCvt64Fto32F
  1986  			} else {
  1987  				s.Fatalf("weird complex conversion %v -> %v", ft, tt)
  1988  			}
  1989  			ftp := floatForComplex(ft)
  1990  			ttp := floatForComplex(tt)
  1991  			return s.newValue2(ssa.OpComplexMake, tt,
  1992  				s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexReal, ftp, x)),
  1993  				s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexImag, ftp, x)))
  1994  		}
  1995  
  1996  		s.Fatalf("unhandled OCONV %s -> %s", n.Left.Type.Etype, n.Type.Etype)
  1997  		return nil
  1998  
  1999  	case ODOTTYPE:
  2000  		res, _ := s.dottype(n, false)
  2001  		return res
  2002  
  2003  	// binary ops
  2004  	case OLT, OEQ, ONE, OLE, OGE, OGT:
  2005  		a := s.expr(n.Left)
  2006  		b := s.expr(n.Right)
  2007  		if n.Left.Type.IsComplex() {
  2008  			pt := floatForComplex(n.Left.Type)
  2009  			op := s.ssaOp(OEQ, pt)
  2010  			r := s.newValueOrSfCall2(op, types.Types[TBOOL], s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b))
  2011  			i := s.newValueOrSfCall2(op, types.Types[TBOOL], s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))
  2012  			c := s.newValue2(ssa.OpAndB, types.Types[TBOOL], r, i)
  2013  			switch n.Op {
  2014  			case OEQ:
  2015  				return c
  2016  			case ONE:
  2017  				return s.newValue1(ssa.OpNot, types.Types[TBOOL], c)
  2018  			default:
  2019  				s.Fatalf("ordered complex compare %v", n.Op)
  2020  			}
  2021  		}
  2022  		if n.Left.Type.IsFloat() {
  2023  			return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Left.Type), types.Types[TBOOL], a, b)
  2024  		}
  2025  		return s.newValue2(s.ssaOp(n.Op, n.Left.Type), types.Types[TBOOL], a, b)
  2026  	case OMUL:
  2027  		a := s.expr(n.Left)
  2028  		b := s.expr(n.Right)
  2029  		if n.Type.IsComplex() {
  2030  			mulop := ssa.OpMul64F
  2031  			addop := ssa.OpAdd64F
  2032  			subop := ssa.OpSub64F
  2033  			pt := floatForComplex(n.Type) // Could be Float32 or Float64
  2034  			wt := types.Types[TFLOAT64]   // Compute in Float64 to minimize cancelation error
  2035  
  2036  			areal := s.newValue1(ssa.OpComplexReal, pt, a)
  2037  			breal := s.newValue1(ssa.OpComplexReal, pt, b)
  2038  			aimag := s.newValue1(ssa.OpComplexImag, pt, a)
  2039  			bimag := s.newValue1(ssa.OpComplexImag, pt, b)
  2040  
  2041  			if pt != wt { // Widen for calculation
  2042  				areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal)
  2043  				breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal)
  2044  				aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag)
  2045  				bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag)
  2046  			}
  2047  
  2048  			xreal := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag))
  2049  			ximag := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, bimag), s.newValueOrSfCall2(mulop, wt, aimag, breal))
  2050  
  2051  			if pt != wt { // Narrow to store back
  2052  				xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal)
  2053  				ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag)
  2054  			}
  2055  
  2056  			return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
  2057  		}
  2058  
  2059  		if n.Type.IsFloat() {
  2060  			return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2061  		}
  2062  
  2063  		return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2064  
  2065  	case ODIV:
  2066  		a := s.expr(n.Left)
  2067  		b := s.expr(n.Right)
  2068  		if n.Type.IsComplex() {
  2069  			// TODO this is not executed because the front-end substitutes a runtime call.
  2070  			// That probably ought to change; with modest optimization the widen/narrow
  2071  			// conversions could all be elided in larger expression trees.
  2072  			mulop := ssa.OpMul64F
  2073  			addop := ssa.OpAdd64F
  2074  			subop := ssa.OpSub64F
  2075  			divop := ssa.OpDiv64F
  2076  			pt := floatForComplex(n.Type) // Could be Float32 or Float64
  2077  			wt := types.Types[TFLOAT64]   // Compute in Float64 to minimize cancelation error
  2078  
  2079  			areal := s.newValue1(ssa.OpComplexReal, pt, a)
  2080  			breal := s.newValue1(ssa.OpComplexReal, pt, b)
  2081  			aimag := s.newValue1(ssa.OpComplexImag, pt, a)
  2082  			bimag := s.newValue1(ssa.OpComplexImag, pt, b)
  2083  
  2084  			if pt != wt { // Widen for calculation
  2085  				areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal)
  2086  				breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal)
  2087  				aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag)
  2088  				bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag)
  2089  			}
  2090  
  2091  			denom := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, breal, breal), s.newValueOrSfCall2(mulop, wt, bimag, bimag))
  2092  			xreal := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag))
  2093  			ximag := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, aimag, breal), s.newValueOrSfCall2(mulop, wt, areal, bimag))
  2094  
  2095  			// TODO not sure if this is best done in wide precision or narrow
  2096  			// Double-rounding might be an issue.
  2097  			// Note that the pre-SSA implementation does the entire calculation
  2098  			// in wide format, so wide is compatible.
  2099  			xreal = s.newValueOrSfCall2(divop, wt, xreal, denom)
  2100  			ximag = s.newValueOrSfCall2(divop, wt, ximag, denom)
  2101  
  2102  			if pt != wt { // Narrow to store back
  2103  				xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal)
  2104  				ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag)
  2105  			}
  2106  			return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
  2107  		}
  2108  		if n.Type.IsFloat() {
  2109  			return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2110  		}
  2111  		return s.intDivide(n, a, b)
  2112  	case OMOD:
  2113  		a := s.expr(n.Left)
  2114  		b := s.expr(n.Right)
  2115  		return s.intDivide(n, a, b)
  2116  	case OADD, OSUB:
  2117  		a := s.expr(n.Left)
  2118  		b := s.expr(n.Right)
  2119  		if n.Type.IsComplex() {
  2120  			pt := floatForComplex(n.Type)
  2121  			op := s.ssaOp(n.Op, pt)
  2122  			return s.newValue2(ssa.OpComplexMake, n.Type,
  2123  				s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)),
  2124  				s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)))
  2125  		}
  2126  		if n.Type.IsFloat() {
  2127  			return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2128  		}
  2129  		return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2130  	case OAND, OOR, OXOR:
  2131  		a := s.expr(n.Left)
  2132  		b := s.expr(n.Right)
  2133  		return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  2134  	case OLSH, ORSH:
  2135  		a := s.expr(n.Left)
  2136  		b := s.expr(n.Right)
  2137  		return s.newValue2(s.ssaShiftOp(n.Op, n.Type, n.Right.Type), a.Type, a, b)
  2138  	case OANDAND, OOROR:
  2139  		// To implement OANDAND (and OOROR), we introduce a
  2140  		// new temporary variable to hold the result. The
  2141  		// variable is associated with the OANDAND node in the
  2142  		// s.vars table (normally variables are only
  2143  		// associated with ONAME nodes). We convert
  2144  		//     A && B
  2145  		// to
  2146  		//     var = A
  2147  		//     if var {
  2148  		//         var = B
  2149  		//     }
  2150  		// Using var in the subsequent block introduces the
  2151  		// necessary phi variable.
  2152  		el := s.expr(n.Left)
  2153  		s.vars[n] = el
  2154  
  2155  		b := s.endBlock()
  2156  		b.Kind = ssa.BlockIf
  2157  		b.SetControl(el)
  2158  		// In theory, we should set b.Likely here based on context.
  2159  		// However, gc only gives us likeliness hints
  2160  		// in a single place, for plain OIF statements,
  2161  		// and passing around context is finnicky, so don't bother for now.
  2162  
  2163  		bRight := s.f.NewBlock(ssa.BlockPlain)
  2164  		bResult := s.f.NewBlock(ssa.BlockPlain)
  2165  		if n.Op == OANDAND {
  2166  			b.AddEdgeTo(bRight)
  2167  			b.AddEdgeTo(bResult)
  2168  		} else if n.Op == OOROR {
  2169  			b.AddEdgeTo(bResult)
  2170  			b.AddEdgeTo(bRight)
  2171  		}
  2172  
  2173  		s.startBlock(bRight)
  2174  		er := s.expr(n.Right)
  2175  		s.vars[n] = er
  2176  
  2177  		b = s.endBlock()
  2178  		b.AddEdgeTo(bResult)
  2179  
  2180  		s.startBlock(bResult)
  2181  		return s.variable(n, types.Types[TBOOL])
  2182  	case OCOMPLEX:
  2183  		r := s.expr(n.Left)
  2184  		i := s.expr(n.Right)
  2185  		return s.newValue2(ssa.OpComplexMake, n.Type, r, i)
  2186  
  2187  	// unary ops
  2188  	case ONEG:
  2189  		a := s.expr(n.Left)
  2190  		if n.Type.IsComplex() {
  2191  			tp := floatForComplex(n.Type)
  2192  			negop := s.ssaOp(n.Op, tp)
  2193  			return s.newValue2(ssa.OpComplexMake, n.Type,
  2194  				s.newValue1(negop, tp, s.newValue1(ssa.OpComplexReal, tp, a)),
  2195  				s.newValue1(negop, tp, s.newValue1(ssa.OpComplexImag, tp, a)))
  2196  		}
  2197  		return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a)
  2198  	case ONOT, OBITNOT:
  2199  		a := s.expr(n.Left)
  2200  		return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a)
  2201  	case OIMAG, OREAL:
  2202  		a := s.expr(n.Left)
  2203  		return s.newValue1(s.ssaOp(n.Op, n.Left.Type), n.Type, a)
  2204  	case OPLUS:
  2205  		return s.expr(n.Left)
  2206  
  2207  	case OADDR:
  2208  		return s.addr(n.Left, n.Bounded())
  2209  
  2210  	case OINDREGSP:
  2211  		addr := s.constOffPtrSP(types.NewPtr(n.Type), n.Xoffset)
  2212  		return s.load(n.Type, addr)
  2213  
  2214  	case ODEREF:
  2215  		p := s.exprPtr(n.Left, false, n.Pos)
  2216  		return s.load(n.Type, p)
  2217  
  2218  	case ODOT:
  2219  		if n.Left.Op == OSTRUCTLIT {
  2220  			// All literals with nonzero fields have already been
  2221  			// rewritten during walk. Any that remain are just T{}
  2222  			// or equivalents. Use the zero value.
  2223  			if !isZero(n.Left) {
  2224  				Fatalf("literal with nonzero value in SSA: %v", n.Left)
  2225  			}
  2226  			return s.zeroVal(n.Type)
  2227  		}
  2228  		// If n is addressable and can't be represented in
  2229  		// SSA, then load just the selected field. This
  2230  		// prevents false memory dependencies in race/msan
  2231  		// instrumentation.
  2232  		if islvalue(n) && !s.canSSA(n) {
  2233  			p := s.addr(n, false)
  2234  			return s.load(n.Type, p)
  2235  		}
  2236  		v := s.expr(n.Left)
  2237  		return s.newValue1I(ssa.OpStructSelect, n.Type, int64(fieldIdx(n)), v)
  2238  
  2239  	case ODOTPTR:
  2240  		p := s.exprPtr(n.Left, false, n.Pos)
  2241  		p = s.newValue1I(ssa.OpOffPtr, types.NewPtr(n.Type), n.Xoffset, p)
  2242  		return s.load(n.Type, p)
  2243  
  2244  	case OINDEX:
  2245  		switch {
  2246  		case n.Left.Type.IsString():
  2247  			if n.Bounded() && Isconst(n.Left, CTSTR) && Isconst(n.Right, CTINT) {
  2248  				// Replace "abc"[1] with 'b'.
  2249  				// Delayed until now because "abc"[1] is not an ideal constant.
  2250  				// See test/fixedbugs/issue11370.go.
  2251  				return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(n.Left.Val().U.(string)[n.Right.Int64()])))
  2252  			}
  2253  			a := s.expr(n.Left)
  2254  			i := s.expr(n.Right)
  2255  			i = s.extendIndex(i, panicindex)
  2256  			if !n.Bounded() {
  2257  				len := s.newValue1(ssa.OpStringLen, types.Types[TINT], a)
  2258  				s.boundsCheck(i, len)
  2259  			}
  2260  			ptrtyp := s.f.Config.Types.BytePtr
  2261  			ptr := s.newValue1(ssa.OpStringPtr, ptrtyp, a)
  2262  			if Isconst(n.Right, CTINT) {
  2263  				ptr = s.newValue1I(ssa.OpOffPtr, ptrtyp, n.Right.Int64(), ptr)
  2264  			} else {
  2265  				ptr = s.newValue2(ssa.OpAddPtr, ptrtyp, ptr, i)
  2266  			}
  2267  			return s.load(types.Types[TUINT8], ptr)
  2268  		case n.Left.Type.IsSlice():
  2269  			p := s.addr(n, false)
  2270  			return s.load(n.Left.Type.Elem(), p)
  2271  		case n.Left.Type.IsArray():
  2272  			if canSSAType(n.Left.Type) {
  2273  				// SSA can handle arrays of length at most 1.
  2274  				bound := n.Left.Type.NumElem()
  2275  				a := s.expr(n.Left)
  2276  				i := s.expr(n.Right)
  2277  				if bound == 0 {
  2278  					// Bounds check will never succeed.  Might as well
  2279  					// use constants for the bounds check.
  2280  					z := s.constInt(types.Types[TINT], 0)
  2281  					s.boundsCheck(z, z)
  2282  					// The return value won't be live, return junk.
  2283  					return s.newValue0(ssa.OpUnknown, n.Type)
  2284  				}
  2285  				i = s.extendIndex(i, panicindex)
  2286  				if !n.Bounded() {
  2287  					s.boundsCheck(i, s.constInt(types.Types[TINT], bound))
  2288  				}
  2289  				return s.newValue1I(ssa.OpArraySelect, n.Type, 0, a)
  2290  			}
  2291  			p := s.addr(n, false)
  2292  			return s.load(n.Left.Type.Elem(), p)
  2293  		default:
  2294  			s.Fatalf("bad type for index %v", n.Left.Type)
  2295  			return nil
  2296  		}
  2297  
  2298  	case OLEN, OCAP:
  2299  		switch {
  2300  		case n.Left.Type.IsSlice():
  2301  			op := ssa.OpSliceLen
  2302  			if n.Op == OCAP {
  2303  				op = ssa.OpSliceCap
  2304  			}
  2305  			return s.newValue1(op, types.Types[TINT], s.expr(n.Left))
  2306  		case n.Left.Type.IsString(): // string; not reachable for OCAP
  2307  			return s.newValue1(ssa.OpStringLen, types.Types[TINT], s.expr(n.Left))
  2308  		case n.Left.Type.IsMap(), n.Left.Type.IsChan():
  2309  			return s.referenceTypeBuiltin(n, s.expr(n.Left))
  2310  		default: // array
  2311  			return s.constInt(types.Types[TINT], n.Left.Type.NumElem())
  2312  		}
  2313  
  2314  	case OSPTR:
  2315  		a := s.expr(n.Left)
  2316  		if n.Left.Type.IsSlice() {
  2317  			return s.newValue1(ssa.OpSlicePtr, n.Type, a)
  2318  		} else {
  2319  			return s.newValue1(ssa.OpStringPtr, n.Type, a)
  2320  		}
  2321  
  2322  	case OITAB:
  2323  		a := s.expr(n.Left)
  2324  		return s.newValue1(ssa.OpITab, n.Type, a)
  2325  
  2326  	case OIDATA:
  2327  		a := s.expr(n.Left)
  2328  		return s.newValue1(ssa.OpIData, n.Type, a)
  2329  
  2330  	case OEFACE:
  2331  		tab := s.expr(n.Left)
  2332  		data := s.expr(n.Right)
  2333  		return s.newValue2(ssa.OpIMake, n.Type, tab, data)
  2334  
  2335  	case OSLICEHEADER:
  2336  		p := s.expr(n.Left)
  2337  		l := s.expr(n.List.First())
  2338  		c := s.expr(n.List.Second())
  2339  		return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c)
  2340  
  2341  	case OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR:
  2342  		v := s.expr(n.Left)
  2343  		var i, j, k *ssa.Value
  2344  		low, high, max := n.SliceBounds()
  2345  		if low != nil {
  2346  			i = s.extendIndex(s.expr(low), panicslice)
  2347  		}
  2348  		if high != nil {
  2349  			j = s.extendIndex(s.expr(high), panicslice)
  2350  		}
  2351  		if max != nil {
  2352  			k = s.extendIndex(s.expr(max), panicslice)
  2353  		}
  2354  		p, l, c := s.slice(n.Left.Type, v, i, j, k, n.Bounded())
  2355  		return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c)
  2356  
  2357  	case OSLICESTR:
  2358  		v := s.expr(n.Left)
  2359  		var i, j *ssa.Value
  2360  		low, high, _ := n.SliceBounds()
  2361  		if low != nil {
  2362  			i = s.extendIndex(s.expr(low), panicslice)
  2363  		}
  2364  		if high != nil {
  2365  			j = s.extendIndex(s.expr(high), panicslice)
  2366  		}
  2367  		p, l, _ := s.slice(n.Left.Type, v, i, j, nil, n.Bounded())
  2368  		return s.newValue2(ssa.OpStringMake, n.Type, p, l)
  2369  
  2370  	case OCALLFUNC:
  2371  		if isIntrinsicCall(n) {
  2372  			return s.intrinsicCall(n)
  2373  		}
  2374  		fallthrough
  2375  
  2376  	case OCALLINTER, OCALLMETH:
  2377  		a := s.call(n, callNormal)
  2378  		return s.load(n.Type, a)
  2379  
  2380  	case OGETG:
  2381  		return s.newValue1(ssa.OpGetG, n.Type, s.mem())
  2382  
  2383  	case OAPPEND:
  2384  		return s.append(n, false)
  2385  
  2386  	case OSTRUCTLIT, OARRAYLIT:
  2387  		// All literals with nonzero fields have already been
  2388  		// rewritten during walk. Any that remain are just T{}
  2389  		// or equivalents. Use the zero value.
  2390  		if !isZero(n) {
  2391  			Fatalf("literal with nonzero value in SSA: %v", n)
  2392  		}
  2393  		return s.zeroVal(n.Type)
  2394  
  2395  	default:
  2396  		s.Fatalf("unhandled expr %v", n.Op)
  2397  		return nil
  2398  	}
  2399  }
  2400  
  2401  // append converts an OAPPEND node to SSA.
  2402  // If inplace is false, it converts the OAPPEND expression n to an ssa.Value,
  2403  // adds it to s, and returns the Value.
  2404  // If inplace is true, it writes the result of the OAPPEND expression n
  2405  // back to the slice being appended to, and returns nil.
  2406  // inplace MUST be set to false if the slice can be SSA'd.
  2407  func (s *state) append(n *Node, inplace bool) *ssa.Value {
  2408  	// If inplace is false, process as expression "append(s, e1, e2, e3)":
  2409  	//
  2410  	// ptr, len, cap := s
  2411  	// newlen := len + 3
  2412  	// if newlen > cap {
  2413  	//     ptr, len, cap = growslice(s, newlen)
  2414  	//     newlen = len + 3 // recalculate to avoid a spill
  2415  	// }
  2416  	// // with write barriers, if needed:
  2417  	// *(ptr+len) = e1
  2418  	// *(ptr+len+1) = e2
  2419  	// *(ptr+len+2) = e3
  2420  	// return makeslice(ptr, newlen, cap)
  2421  	//
  2422  	//
  2423  	// If inplace is true, process as statement "s = append(s, e1, e2, e3)":
  2424  	//
  2425  	// a := &s
  2426  	// ptr, len, cap := s
  2427  	// newlen := len + 3
  2428  	// if uint(newlen) > uint(cap) {
  2429  	//    newptr, len, newcap = growslice(ptr, len, cap, newlen)
  2430  	//    vardef(a)       // if necessary, advise liveness we are writing a new a
  2431  	//    *a.cap = newcap // write before ptr to avoid a spill
  2432  	//    *a.ptr = newptr // with write barrier
  2433  	// }
  2434  	// newlen = len + 3 // recalculate to avoid a spill
  2435  	// *a.len = newlen
  2436  	// // with write barriers, if needed:
  2437  	// *(ptr+len) = e1
  2438  	// *(ptr+len+1) = e2
  2439  	// *(ptr+len+2) = e3
  2440  
  2441  	et := n.Type.Elem()
  2442  	pt := types.NewPtr(et)
  2443  
  2444  	// Evaluate slice
  2445  	sn := n.List.First() // the slice node is the first in the list
  2446  
  2447  	var slice, addr *ssa.Value
  2448  	if inplace {
  2449  		addr = s.addr(sn, false)
  2450  		slice = s.load(n.Type, addr)
  2451  	} else {
  2452  		slice = s.expr(sn)
  2453  	}
  2454  
  2455  	// Allocate new blocks
  2456  	grow := s.f.NewBlock(ssa.BlockPlain)
  2457  	assign := s.f.NewBlock(ssa.BlockPlain)
  2458  
  2459  	// Decide if we need to grow
  2460  	nargs := int64(n.List.Len() - 1)
  2461  	p := s.newValue1(ssa.OpSlicePtr, pt, slice)
  2462  	l := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice)
  2463  	c := s.newValue1(ssa.OpSliceCap, types.Types[TINT], slice)
  2464  	nl := s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], l, s.constInt(types.Types[TINT], nargs))
  2465  
  2466  	cmp := s.newValue2(s.ssaOp(OGT, types.Types[TUINT]), types.Types[TBOOL], nl, c)
  2467  	s.vars[&ptrVar] = p
  2468  
  2469  	if !inplace {
  2470  		s.vars[&newlenVar] = nl
  2471  		s.vars[&capVar] = c
  2472  	} else {
  2473  		s.vars[&lenVar] = l
  2474  	}
  2475  
  2476  	b := s.endBlock()
  2477  	b.Kind = ssa.BlockIf
  2478  	b.Likely = ssa.BranchUnlikely
  2479  	b.SetControl(cmp)
  2480  	b.AddEdgeTo(grow)
  2481  	b.AddEdgeTo(assign)
  2482  
  2483  	// Call growslice
  2484  	s.startBlock(grow)
  2485  	taddr := s.expr(n.Left)
  2486  	r := s.rtcall(growslice, true, []*types.Type{pt, types.Types[TINT], types.Types[TINT]}, taddr, p, l, c, nl)
  2487  
  2488  	if inplace {
  2489  		if sn.Op == ONAME && sn.Class() != PEXTERN {
  2490  			// Tell liveness we're about to build a new slice
  2491  			s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, sn, s.mem())
  2492  		}
  2493  		capaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(array_cap), addr)
  2494  		s.store(types.Types[TINT], capaddr, r[2])
  2495  		s.store(pt, addr, r[0])
  2496  		// load the value we just stored to avoid having to spill it
  2497  		s.vars[&ptrVar] = s.load(pt, addr)
  2498  		s.vars[&lenVar] = r[1] // avoid a spill in the fast path
  2499  	} else {
  2500  		s.vars[&ptrVar] = r[0]
  2501  		s.vars[&newlenVar] = s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], r[1], s.constInt(types.Types[TINT], nargs))
  2502  		s.vars[&capVar] = r[2]
  2503  	}
  2504  
  2505  	b = s.endBlock()
  2506  	b.AddEdgeTo(assign)
  2507  
  2508  	// assign new elements to slots
  2509  	s.startBlock(assign)
  2510  
  2511  	if inplace {
  2512  		l = s.variable(&lenVar, types.Types[TINT]) // generates phi for len
  2513  		nl = s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], l, s.constInt(types.Types[TINT], nargs))
  2514  		lenaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(array_nel), addr)
  2515  		s.store(types.Types[TINT], lenaddr, nl)
  2516  	}
  2517  
  2518  	// Evaluate args
  2519  	type argRec struct {
  2520  		// if store is true, we're appending the value v.  If false, we're appending the
  2521  		// value at *v.
  2522  		v     *ssa.Value
  2523  		store bool
  2524  	}
  2525  	args := make([]argRec, 0, nargs)
  2526  	for _, n := range n.List.Slice()[1:] {
  2527  		if canSSAType(n.Type) {
  2528  			args = append(args, argRec{v: s.expr(n), store: true})
  2529  		} else {
  2530  			v := s.addr(n, false)
  2531  			args = append(args, argRec{v: v})
  2532  		}
  2533  	}
  2534  
  2535  	p = s.variable(&ptrVar, pt) // generates phi for ptr
  2536  	if !inplace {
  2537  		nl = s.variable(&newlenVar, types.Types[TINT]) // generates phi for nl
  2538  		c = s.variable(&capVar, types.Types[TINT])     // generates phi for cap
  2539  	}
  2540  	p2 := s.newValue2(ssa.OpPtrIndex, pt, p, l)
  2541  	for i, arg := range args {
  2542  		addr := s.newValue2(ssa.OpPtrIndex, pt, p2, s.constInt(types.Types[TINT], int64(i)))
  2543  		if arg.store {
  2544  			s.storeType(et, addr, arg.v, 0, true)
  2545  		} else {
  2546  			s.move(et, addr, arg.v)
  2547  		}
  2548  	}
  2549  
  2550  	delete(s.vars, &ptrVar)
  2551  	if inplace {
  2552  		delete(s.vars, &lenVar)
  2553  		return nil
  2554  	}
  2555  	delete(s.vars, &newlenVar)
  2556  	delete(s.vars, &capVar)
  2557  	// make result
  2558  	return s.newValue3(ssa.OpSliceMake, n.Type, p, nl, c)
  2559  }
  2560  
  2561  // condBranch evaluates the boolean expression cond and branches to yes
  2562  // if cond is true and no if cond is false.
  2563  // This function is intended to handle && and || better than just calling
  2564  // s.expr(cond) and branching on the result.
  2565  func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) {
  2566  	switch cond.Op {
  2567  	case OANDAND:
  2568  		mid := s.f.NewBlock(ssa.BlockPlain)
  2569  		s.stmtList(cond.Ninit)
  2570  		s.condBranch(cond.Left, mid, no, max8(likely, 0))
  2571  		s.startBlock(mid)
  2572  		s.condBranch(cond.Right, yes, no, likely)
  2573  		return
  2574  		// Note: if likely==1, then both recursive calls pass 1.
  2575  		// If likely==-1, then we don't have enough information to decide
  2576  		// whether the first branch is likely or not. So we pass 0 for
  2577  		// the likeliness of the first branch.
  2578  		// TODO: have the frontend give us branch prediction hints for
  2579  		// OANDAND and OOROR nodes (if it ever has such info).
  2580  	case OOROR:
  2581  		mid := s.f.NewBlock(ssa.BlockPlain)
  2582  		s.stmtList(cond.Ninit)
  2583  		s.condBranch(cond.Left, yes, mid, min8(likely, 0))
  2584  		s.startBlock(mid)
  2585  		s.condBranch(cond.Right, yes, no, likely)
  2586  		return
  2587  		// Note: if likely==-1, then both recursive calls pass -1.
  2588  		// If likely==1, then we don't have enough info to decide
  2589  		// the likelihood of the first branch.
  2590  	case ONOT:
  2591  		s.stmtList(cond.Ninit)
  2592  		s.condBranch(cond.Left, no, yes, -likely)
  2593  		return
  2594  	}
  2595  	c := s.expr(cond)
  2596  	b := s.endBlock()
  2597  	b.Kind = ssa.BlockIf
  2598  	b.SetControl(c)
  2599  	b.Likely = ssa.BranchPrediction(likely) // gc and ssa both use -1/0/+1 for likeliness
  2600  	b.AddEdgeTo(yes)
  2601  	b.AddEdgeTo(no)
  2602  }
  2603  
  2604  type skipMask uint8
  2605  
  2606  const (
  2607  	skipPtr skipMask = 1 << iota
  2608  	skipLen
  2609  	skipCap
  2610  )
  2611  
  2612  // assign does left = right.
  2613  // Right has already been evaluated to ssa, left has not.
  2614  // If deref is true, then we do left = *right instead (and right has already been nil-checked).
  2615  // If deref is true and right == nil, just do left = 0.
  2616  // skip indicates assignments (at the top level) that can be avoided.
  2617  func (s *state) assign(left *Node, right *ssa.Value, deref bool, skip skipMask) {
  2618  	if left.Op == ONAME && left.isBlank() {
  2619  		return
  2620  	}
  2621  	t := left.Type
  2622  	dowidth(t)
  2623  	if s.canSSA(left) {
  2624  		if deref {
  2625  			s.Fatalf("can SSA LHS %v but not RHS %s", left, right)
  2626  		}
  2627  		if left.Op == ODOT {
  2628  			// We're assigning to a field of an ssa-able value.
  2629  			// We need to build a new structure with the new value for the
  2630  			// field we're assigning and the old values for the other fields.
  2631  			// For instance:
  2632  			//   type T struct {a, b, c int}
  2633  			//   var T x
  2634  			//   x.b = 5
  2635  			// For the x.b = 5 assignment we want to generate x = T{x.a, 5, x.c}
  2636  
  2637  			// Grab information about the structure type.
  2638  			t := left.Left.Type
  2639  			nf := t.NumFields()
  2640  			idx := fieldIdx(left)
  2641  
  2642  			// Grab old value of structure.
  2643  			old := s.expr(left.Left)
  2644  
  2645  			// Make new structure.
  2646  			new := s.newValue0(ssa.StructMakeOp(t.NumFields()), t)
  2647  
  2648  			// Add fields as args.
  2649  			for i := 0; i < nf; i++ {
  2650  				if i == idx {
  2651  					new.AddArg(right)
  2652  				} else {
  2653  					new.AddArg(s.newValue1I(ssa.OpStructSelect, t.FieldType(i), int64(i), old))
  2654  				}
  2655  			}
  2656  
  2657  			// Recursively assign the new value we've made to the base of the dot op.
  2658  			s.assign(left.Left, new, false, 0)
  2659  			// TODO: do we need to update named values here?
  2660  			return
  2661  		}
  2662  		if left.Op == OINDEX && left.Left.Type.IsArray() {
  2663  			// We're assigning to an element of an ssa-able array.
  2664  			// a[i] = v
  2665  			t := left.Left.Type
  2666  			n := t.NumElem()
  2667  
  2668  			i := s.expr(left.Right) // index
  2669  			if n == 0 {
  2670  				// The bounds check must fail.  Might as well
  2671  				// ignore the actual index and just use zeros.
  2672  				z := s.constInt(types.Types[TINT], 0)
  2673  				s.boundsCheck(z, z)
  2674  				return
  2675  			}
  2676  			if n != 1 {
  2677  				s.Fatalf("assigning to non-1-length array")
  2678  			}
  2679  			// Rewrite to a = [1]{v}
  2680  			i = s.extendIndex(i, panicindex)
  2681  			s.boundsCheck(i, s.constInt(types.Types[TINT], 1))
  2682  			v := s.newValue1(ssa.OpArrayMake1, t, right)
  2683  			s.assign(left.Left, v, false, 0)
  2684  			return
  2685  		}
  2686  		// Update variable assignment.
  2687  		s.vars[left] = right
  2688  		s.addNamedValue(left, right)
  2689  		return
  2690  	}
  2691  	// Left is not ssa-able. Compute its address.
  2692  	if left.Op == ONAME && left.Class() != PEXTERN && skip == 0 {
  2693  		s.vars[&memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, left, s.mem(), !left.IsAutoTmp())
  2694  	}
  2695  	addr := s.addr(left, false)
  2696  	if isReflectHeaderDataField(left) {
  2697  		// Package unsafe's documentation says storing pointers into
  2698  		// reflect.SliceHeader and reflect.StringHeader's Data fields
  2699  		// is valid, even though they have type uintptr (#19168).
  2700  		// Mark it pointer type to signal the writebarrier pass to
  2701  		// insert a write barrier.
  2702  		t = types.Types[TUNSAFEPTR]
  2703  	}
  2704  	if deref {
  2705  		// Treat as a mem->mem move.
  2706  		if right == nil {
  2707  			s.zero(t, addr)
  2708  		} else {
  2709  			s.move(t, addr, right)
  2710  		}
  2711  		return
  2712  	}
  2713  	// Treat as a store.
  2714  	s.storeType(t, addr, right, skip, !left.IsAutoTmp())
  2715  }
  2716  
  2717  // zeroVal returns the zero value for type t.
  2718  func (s *state) zeroVal(t *types.Type) *ssa.Value {
  2719  	switch {
  2720  	case t.IsInteger():
  2721  		switch t.Size() {
  2722  		case 1:
  2723  			return s.constInt8(t, 0)
  2724  		case 2:
  2725  			return s.constInt16(t, 0)
  2726  		case 4:
  2727  			return s.constInt32(t, 0)
  2728  		case 8:
  2729  			return s.constInt64(t, 0)
  2730  		default:
  2731  			s.Fatalf("bad sized integer type %v", t)
  2732  		}
  2733  	case t.IsFloat():
  2734  		switch t.Size() {
  2735  		case 4:
  2736  			return s.constFloat32(t, 0)
  2737  		case 8:
  2738  			return s.constFloat64(t, 0)
  2739  		default:
  2740  			s.Fatalf("bad sized float type %v", t)
  2741  		}
  2742  	case t.IsComplex():
  2743  		switch t.Size() {
  2744  		case 8:
  2745  			z := s.constFloat32(types.Types[TFLOAT32], 0)
  2746  			return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
  2747  		case 16:
  2748  			z := s.constFloat64(types.Types[TFLOAT64], 0)
  2749  			return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
  2750  		default:
  2751  			s.Fatalf("bad sized complex type %v", t)
  2752  		}
  2753  
  2754  	case t.IsString():
  2755  		return s.constEmptyString(t)
  2756  	case t.IsPtrShaped():
  2757  		return s.constNil(t)
  2758  	case t.IsBoolean():
  2759  		return s.constBool(false)
  2760  	case t.IsInterface():
  2761  		return s.constInterface(t)
  2762  	case t.IsSlice():
  2763  		return s.constSlice(t)
  2764  	case t.IsStruct():
  2765  		n := t.NumFields()
  2766  		v := s.entryNewValue0(ssa.StructMakeOp(t.NumFields()), t)
  2767  		for i := 0; i < n; i++ {
  2768  			v.AddArg(s.zeroVal(t.FieldType(i)))
  2769  		}
  2770  		return v
  2771  	case t.IsArray():
  2772  		switch t.NumElem() {
  2773  		case 0:
  2774  			return s.entryNewValue0(ssa.OpArrayMake0, t)
  2775  		case 1:
  2776  			return s.entryNewValue1(ssa.OpArrayMake1, t, s.zeroVal(t.Elem()))
  2777  		}
  2778  	}
  2779  	s.Fatalf("zero for type %v not implemented", t)
  2780  	return nil
  2781  }
  2782  
  2783  type callKind int8
  2784  
  2785  const (
  2786  	callNormal callKind = iota
  2787  	callDefer
  2788  	callGo
  2789  )
  2790  
  2791  type sfRtCallDef struct {
  2792  	rtfn  *obj.LSym
  2793  	rtype types.EType
  2794  }
  2795  
  2796  var softFloatOps map[ssa.Op]sfRtCallDef
  2797  
  2798  func softfloatInit() {
  2799  	// Some of these operations get transformed by sfcall.
  2800  	softFloatOps = map[ssa.Op]sfRtCallDef{
  2801  		ssa.OpAdd32F: sfRtCallDef{sysfunc("fadd32"), TFLOAT32},
  2802  		ssa.OpAdd64F: sfRtCallDef{sysfunc("fadd64"), TFLOAT64},
  2803  		ssa.OpSub32F: sfRtCallDef{sysfunc("fadd32"), TFLOAT32},
  2804  		ssa.OpSub64F: sfRtCallDef{sysfunc("fadd64"), TFLOAT64},
  2805  		ssa.OpMul32F: sfRtCallDef{sysfunc("fmul32"), TFLOAT32},
  2806  		ssa.OpMul64F: sfRtCallDef{sysfunc("fmul64"), TFLOAT64},
  2807  		ssa.OpDiv32F: sfRtCallDef{sysfunc("fdiv32"), TFLOAT32},
  2808  		ssa.OpDiv64F: sfRtCallDef{sysfunc("fdiv64"), TFLOAT64},
  2809  
  2810  		ssa.OpEq64F:      sfRtCallDef{sysfunc("feq64"), TBOOL},
  2811  		ssa.OpEq32F:      sfRtCallDef{sysfunc("feq32"), TBOOL},
  2812  		ssa.OpNeq64F:     sfRtCallDef{sysfunc("feq64"), TBOOL},
  2813  		ssa.OpNeq32F:     sfRtCallDef{sysfunc("feq32"), TBOOL},
  2814  		ssa.OpLess64F:    sfRtCallDef{sysfunc("fgt64"), TBOOL},
  2815  		ssa.OpLess32F:    sfRtCallDef{sysfunc("fgt32"), TBOOL},
  2816  		ssa.OpGreater64F: sfRtCallDef{sysfunc("fgt64"), TBOOL},
  2817  		ssa.OpGreater32F: sfRtCallDef{sysfunc("fgt32"), TBOOL},
  2818  		ssa.OpLeq64F:     sfRtCallDef{sysfunc("fge64"), TBOOL},
  2819  		ssa.OpLeq32F:     sfRtCallDef{sysfunc("fge32"), TBOOL},
  2820  		ssa.OpGeq64F:     sfRtCallDef{sysfunc("fge64"), TBOOL},
  2821  		ssa.OpGeq32F:     sfRtCallDef{sysfunc("fge32"), TBOOL},
  2822  
  2823  		ssa.OpCvt32to32F:  sfRtCallDef{sysfunc("fint32to32"), TFLOAT32},
  2824  		ssa.OpCvt32Fto32:  sfRtCallDef{sysfunc("f32toint32"), TINT32},
  2825  		ssa.OpCvt64to32F:  sfRtCallDef{sysfunc("fint64to32"), TFLOAT32},
  2826  		ssa.OpCvt32Fto64:  sfRtCallDef{sysfunc("f32toint64"), TINT64},
  2827  		ssa.OpCvt64Uto32F: sfRtCallDef{sysfunc("fuint64to32"), TFLOAT32},
  2828  		ssa.OpCvt32Fto64U: sfRtCallDef{sysfunc("f32touint64"), TUINT64},
  2829  		ssa.OpCvt32to64F:  sfRtCallDef{sysfunc("fint32to64"), TFLOAT64},
  2830  		ssa.OpCvt64Fto32:  sfRtCallDef{sysfunc("f64toint32"), TINT32},
  2831  		ssa.OpCvt64to64F:  sfRtCallDef{sysfunc("fint64to64"), TFLOAT64},
  2832  		ssa.OpCvt64Fto64:  sfRtCallDef{sysfunc("f64toint64"), TINT64},
  2833  		ssa.OpCvt64Uto64F: sfRtCallDef{sysfunc("fuint64to64"), TFLOAT64},
  2834  		ssa.OpCvt64Fto64U: sfRtCallDef{sysfunc("f64touint64"), TUINT64},
  2835  		ssa.OpCvt32Fto64F: sfRtCallDef{sysfunc("f32to64"), TFLOAT64},
  2836  		ssa.OpCvt64Fto32F: sfRtCallDef{sysfunc("f64to32"), TFLOAT32},
  2837  	}
  2838  }
  2839  
  2840  // TODO: do not emit sfcall if operation can be optimized to constant in later
  2841  // opt phase
  2842  func (s *state) sfcall(op ssa.Op, args ...*ssa.Value) (*ssa.Value, bool) {
  2843  	if callDef, ok := softFloatOps[op]; ok {
  2844  		switch op {
  2845  		case ssa.OpLess32F,
  2846  			ssa.OpLess64F,
  2847  			ssa.OpLeq32F,
  2848  			ssa.OpLeq64F:
  2849  			args[0], args[1] = args[1], args[0]
  2850  		case ssa.OpSub32F,
  2851  			ssa.OpSub64F:
  2852  			args[1] = s.newValue1(s.ssaOp(ONEG, types.Types[callDef.rtype]), args[1].Type, args[1])
  2853  		}
  2854  
  2855  		result := s.rtcall(callDef.rtfn, true, []*types.Type{types.Types[callDef.rtype]}, args...)[0]
  2856  		if op == ssa.OpNeq32F || op == ssa.OpNeq64F {
  2857  			result = s.newValue1(ssa.OpNot, result.Type, result)
  2858  		}
  2859  		return result, true
  2860  	}
  2861  	return nil, false
  2862  }
  2863  
  2864  var intrinsics map[intrinsicKey]intrinsicBuilder
  2865  
  2866  // An intrinsicBuilder converts a call node n into an ssa value that
  2867  // implements that call as an intrinsic. args is a list of arguments to the func.
  2868  type intrinsicBuilder func(s *state, n *Node, args []*ssa.Value) *ssa.Value
  2869  
  2870  type intrinsicKey struct {
  2871  	arch *sys.Arch
  2872  	pkg  string
  2873  	fn   string
  2874  }
  2875  
  2876  func init() {
  2877  	intrinsics = map[intrinsicKey]intrinsicBuilder{}
  2878  
  2879  	var all []*sys.Arch
  2880  	var p4 []*sys.Arch
  2881  	var p8 []*sys.Arch
  2882  	var lwatomics []*sys.Arch
  2883  	for _, a := range sys.Archs {
  2884  		all = append(all, a)
  2885  		if a.PtrSize == 4 {
  2886  			p4 = append(p4, a)
  2887  		} else {
  2888  			p8 = append(p8, a)
  2889  		}
  2890  		if a.Family != sys.PPC64 {
  2891  			lwatomics = append(lwatomics, a)
  2892  		}
  2893  	}
  2894  
  2895  	// add adds the intrinsic b for pkg.fn for the given list of architectures.
  2896  	add := func(pkg, fn string, b intrinsicBuilder, archs ...*sys.Arch) {
  2897  		for _, a := range archs {
  2898  			intrinsics[intrinsicKey{a, pkg, fn}] = b
  2899  		}
  2900  	}
  2901  	// addF does the same as add but operates on architecture families.
  2902  	addF := func(pkg, fn string, b intrinsicBuilder, archFamilies ...sys.ArchFamily) {
  2903  		m := 0
  2904  		for _, f := range archFamilies {
  2905  			if f >= 32 {
  2906  				panic("too many architecture families")
  2907  			}
  2908  			m |= 1 << uint(f)
  2909  		}
  2910  		for _, a := range all {
  2911  			if m>>uint(a.Family)&1 != 0 {
  2912  				intrinsics[intrinsicKey{a, pkg, fn}] = b
  2913  			}
  2914  		}
  2915  	}
  2916  	// alias defines pkg.fn = pkg2.fn2 for all architectures in archs for which pkg2.fn2 exists.
  2917  	alias := func(pkg, fn, pkg2, fn2 string, archs ...*sys.Arch) {
  2918  		for _, a := range archs {
  2919  			if b, ok := intrinsics[intrinsicKey{a, pkg2, fn2}]; ok {
  2920  				intrinsics[intrinsicKey{a, pkg, fn}] = b
  2921  			}
  2922  		}
  2923  	}
  2924  
  2925  	/******** runtime ********/
  2926  	if !instrumenting {
  2927  		add("runtime", "slicebytetostringtmp",
  2928  			func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2929  				// Compiler frontend optimizations emit OBYTES2STRTMP nodes
  2930  				// for the backend instead of slicebytetostringtmp calls
  2931  				// when not instrumenting.
  2932  				slice := args[0]
  2933  				ptr := s.newValue1(ssa.OpSlicePtr, s.f.Config.Types.BytePtr, slice)
  2934  				len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice)
  2935  				return s.newValue2(ssa.OpStringMake, n.Type, ptr, len)
  2936  			},
  2937  			all...)
  2938  	}
  2939  	addF("runtime/internal/math", "MulUintptr",
  2940  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2941  			if s.config.PtrSize == 4 {
  2942  				return s.newValue2(ssa.OpMul32uover, types.NewTuple(types.Types[TUINT], types.Types[TUINT]), args[0], args[1])
  2943  			}
  2944  			return s.newValue2(ssa.OpMul64uover, types.NewTuple(types.Types[TUINT], types.Types[TUINT]), args[0], args[1])
  2945  		},
  2946  		sys.AMD64, sys.I386)
  2947  	add("runtime", "KeepAlive",
  2948  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2949  			data := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, args[0])
  2950  			s.vars[&memVar] = s.newValue2(ssa.OpKeepAlive, types.TypeMem, data, s.mem())
  2951  			return nil
  2952  		},
  2953  		all...)
  2954  	add("runtime", "getclosureptr",
  2955  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2956  			return s.newValue0(ssa.OpGetClosurePtr, s.f.Config.Types.Uintptr)
  2957  		},
  2958  		all...)
  2959  
  2960  	add("runtime", "getcallerpc",
  2961  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2962  			return s.newValue0(ssa.OpGetCallerPC, s.f.Config.Types.Uintptr)
  2963  		},
  2964  		all...)
  2965  
  2966  	add("runtime", "getcallersp",
  2967  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2968  			return s.newValue0(ssa.OpGetCallerSP, s.f.Config.Types.Uintptr)
  2969  		},
  2970  		all...)
  2971  
  2972  	/******** runtime/internal/sys ********/
  2973  	addF("runtime/internal/sys", "Ctz32",
  2974  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2975  			return s.newValue1(ssa.OpCtz32, types.Types[TINT], args[0])
  2976  		},
  2977  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  2978  	addF("runtime/internal/sys", "Ctz64",
  2979  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2980  			return s.newValue1(ssa.OpCtz64, types.Types[TINT], args[0])
  2981  		},
  2982  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  2983  	addF("runtime/internal/sys", "Bswap32",
  2984  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2985  			return s.newValue1(ssa.OpBswap32, types.Types[TUINT32], args[0])
  2986  		},
  2987  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X)
  2988  	addF("runtime/internal/sys", "Bswap64",
  2989  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2990  			return s.newValue1(ssa.OpBswap64, types.Types[TUINT64], args[0])
  2991  		},
  2992  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X)
  2993  
  2994  	/******** runtime/internal/atomic ********/
  2995  	addF("runtime/internal/atomic", "Load",
  2996  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  2997  			v := s.newValue2(ssa.OpAtomicLoad32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], s.mem())
  2998  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  2999  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v)
  3000  		},
  3001  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3002  	addF("runtime/internal/atomic", "Load64",
  3003  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3004  			v := s.newValue2(ssa.OpAtomicLoad64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], s.mem())
  3005  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3006  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v)
  3007  		},
  3008  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64)
  3009  	addF("runtime/internal/atomic", "LoadAcq",
  3010  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3011  			v := s.newValue2(ssa.OpAtomicLoadAcq32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], s.mem())
  3012  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3013  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v)
  3014  		},
  3015  		sys.PPC64)
  3016  	addF("runtime/internal/atomic", "Loadp",
  3017  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3018  			v := s.newValue2(ssa.OpAtomicLoadPtr, types.NewTuple(s.f.Config.Types.BytePtr, types.TypeMem), args[0], s.mem())
  3019  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3020  			return s.newValue1(ssa.OpSelect0, s.f.Config.Types.BytePtr, v)
  3021  		},
  3022  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3023  
  3024  	addF("runtime/internal/atomic", "Store",
  3025  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3026  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicStore32, types.TypeMem, args[0], args[1], s.mem())
  3027  			return nil
  3028  		},
  3029  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3030  	addF("runtime/internal/atomic", "Store64",
  3031  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3032  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicStore64, types.TypeMem, args[0], args[1], s.mem())
  3033  			return nil
  3034  		},
  3035  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64)
  3036  	addF("runtime/internal/atomic", "StorepNoWB",
  3037  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3038  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicStorePtrNoWB, types.TypeMem, args[0], args[1], s.mem())
  3039  			return nil
  3040  		},
  3041  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64)
  3042  	addF("runtime/internal/atomic", "StoreRel",
  3043  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3044  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicStoreRel32, types.TypeMem, args[0], args[1], s.mem())
  3045  			return nil
  3046  		},
  3047  		sys.PPC64)
  3048  
  3049  	addF("runtime/internal/atomic", "Xchg",
  3050  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3051  			v := s.newValue3(ssa.OpAtomicExchange32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], args[1], s.mem())
  3052  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3053  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v)
  3054  		},
  3055  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3056  	addF("runtime/internal/atomic", "Xchg64",
  3057  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3058  			v := s.newValue3(ssa.OpAtomicExchange64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], args[1], s.mem())
  3059  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3060  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v)
  3061  		},
  3062  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64)
  3063  
  3064  	addF("runtime/internal/atomic", "Xadd",
  3065  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3066  			v := s.newValue3(ssa.OpAtomicAdd32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], args[1], s.mem())
  3067  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3068  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v)
  3069  		},
  3070  		sys.AMD64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3071  	addF("runtime/internal/atomic", "Xadd64",
  3072  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3073  			v := s.newValue3(ssa.OpAtomicAdd64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], args[1], s.mem())
  3074  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3075  			return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v)
  3076  		},
  3077  		sys.AMD64, sys.S390X, sys.MIPS64, sys.PPC64)
  3078  
  3079  	makeXaddARM64 := func(op0 ssa.Op, op1 ssa.Op, ty types.EType) func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3080  		return func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3081  			// Target Atomic feature is identified by dynamic detection
  3082  			addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), arm64HasATOMICS, s.sb)
  3083  			v := s.load(types.Types[TBOOL], addr)
  3084  			b := s.endBlock()
  3085  			b.Kind = ssa.BlockIf
  3086  			b.SetControl(v)
  3087  			bTrue := s.f.NewBlock(ssa.BlockPlain)
  3088  			bFalse := s.f.NewBlock(ssa.BlockPlain)
  3089  			bEnd := s.f.NewBlock(ssa.BlockPlain)
  3090  			b.AddEdgeTo(bTrue)
  3091  			b.AddEdgeTo(bFalse)
  3092  			b.Likely = ssa.BranchUnlikely // most machines don't have Atomics nowadays
  3093  
  3094  			// We have atomic instructions - use it directly.
  3095  			s.startBlock(bTrue)
  3096  			v0 := s.newValue3(op1, types.NewTuple(types.Types[ty], types.TypeMem), args[0], args[1], s.mem())
  3097  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v0)
  3098  			s.vars[n] = s.newValue1(ssa.OpSelect0, types.Types[ty], v0)
  3099  			s.endBlock().AddEdgeTo(bEnd)
  3100  
  3101  			// Use original instruction sequence.
  3102  			s.startBlock(bFalse)
  3103  			v1 := s.newValue3(op0, types.NewTuple(types.Types[ty], types.TypeMem), args[0], args[1], s.mem())
  3104  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v1)
  3105  			s.vars[n] = s.newValue1(ssa.OpSelect0, types.Types[ty], v1)
  3106  			s.endBlock().AddEdgeTo(bEnd)
  3107  
  3108  			// Merge results.
  3109  			s.startBlock(bEnd)
  3110  			return s.variable(n, types.Types[ty])
  3111  		}
  3112  	}
  3113  
  3114  	addF("runtime/internal/atomic", "Xadd",
  3115  		makeXaddARM64(ssa.OpAtomicAdd32, ssa.OpAtomicAdd32Variant, TUINT32),
  3116  		sys.ARM64)
  3117  	addF("runtime/internal/atomic", "Xadd64",
  3118  		makeXaddARM64(ssa.OpAtomicAdd64, ssa.OpAtomicAdd64Variant, TUINT64),
  3119  		sys.ARM64)
  3120  
  3121  	addF("runtime/internal/atomic", "Cas",
  3122  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3123  			v := s.newValue4(ssa.OpAtomicCompareAndSwap32, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem())
  3124  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3125  			return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v)
  3126  		},
  3127  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64)
  3128  	addF("runtime/internal/atomic", "Cas64",
  3129  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3130  			v := s.newValue4(ssa.OpAtomicCompareAndSwap64, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem())
  3131  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3132  			return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v)
  3133  		},
  3134  		sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64)
  3135  	addF("runtime/internal/atomic", "CasRel",
  3136  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3137  			v := s.newValue4(ssa.OpAtomicCompareAndSwap32, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem())
  3138  			s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v)
  3139  			return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v)
  3140  		},
  3141  		sys.PPC64)
  3142  
  3143  	addF("runtime/internal/atomic", "And8",
  3144  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3145  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicAnd8, types.TypeMem, args[0], args[1], s.mem())
  3146  			return nil
  3147  		},
  3148  		sys.AMD64, sys.ARM64, sys.MIPS, sys.PPC64)
  3149  	addF("runtime/internal/atomic", "Or8",
  3150  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3151  			s.vars[&memVar] = s.newValue3(ssa.OpAtomicOr8, types.TypeMem, args[0], args[1], s.mem())
  3152  			return nil
  3153  		},
  3154  		sys.AMD64, sys.ARM64, sys.MIPS, sys.PPC64)
  3155  
  3156  	alias("runtime/internal/atomic", "Loadint64", "runtime/internal/atomic", "Load64", all...)
  3157  	alias("runtime/internal/atomic", "Xaddint64", "runtime/internal/atomic", "Xadd64", all...)
  3158  	alias("runtime/internal/atomic", "Loaduint", "runtime/internal/atomic", "Load", p4...)
  3159  	alias("runtime/internal/atomic", "Loaduint", "runtime/internal/atomic", "Load64", p8...)
  3160  	alias("runtime/internal/atomic", "Loaduintptr", "runtime/internal/atomic", "Load", p4...)
  3161  	alias("runtime/internal/atomic", "Loaduintptr", "runtime/internal/atomic", "Load64", p8...)
  3162  	alias("runtime/internal/atomic", "LoadAcq", "runtime/internal/atomic", "Load", lwatomics...)
  3163  	alias("runtime/internal/atomic", "Storeuintptr", "runtime/internal/atomic", "Store", p4...)
  3164  	alias("runtime/internal/atomic", "Storeuintptr", "runtime/internal/atomic", "Store64", p8...)
  3165  	alias("runtime/internal/atomic", "StoreRel", "runtime/internal/atomic", "Store", lwatomics...)
  3166  	alias("runtime/internal/atomic", "Xchguintptr", "runtime/internal/atomic", "Xchg", p4...)
  3167  	alias("runtime/internal/atomic", "Xchguintptr", "runtime/internal/atomic", "Xchg64", p8...)
  3168  	alias("runtime/internal/atomic", "Xadduintptr", "runtime/internal/atomic", "Xadd", p4...)
  3169  	alias("runtime/internal/atomic", "Xadduintptr", "runtime/internal/atomic", "Xadd64", p8...)
  3170  	alias("runtime/internal/atomic", "Casuintptr", "runtime/internal/atomic", "Cas", p4...)
  3171  	alias("runtime/internal/atomic", "Casuintptr", "runtime/internal/atomic", "Cas64", p8...)
  3172  	alias("runtime/internal/atomic", "Casp1", "runtime/internal/atomic", "Cas", p4...)
  3173  	alias("runtime/internal/atomic", "Casp1", "runtime/internal/atomic", "Cas64", p8...)
  3174  	alias("runtime/internal/atomic", "CasRel", "runtime/internal/atomic", "Cas", lwatomics...)
  3175  
  3176  	alias("runtime/internal/sys", "Ctz8", "math/bits", "TrailingZeros8", all...)
  3177  
  3178  	/******** math ********/
  3179  	addF("math", "Sqrt",
  3180  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3181  			return s.newValue1(ssa.OpSqrt, types.Types[TFLOAT64], args[0])
  3182  		},
  3183  		sys.I386, sys.AMD64, sys.ARM, sys.ARM64, sys.MIPS, sys.MIPS64, sys.PPC64, sys.S390X)
  3184  	addF("math", "Trunc",
  3185  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3186  			return s.newValue1(ssa.OpTrunc, types.Types[TFLOAT64], args[0])
  3187  		},
  3188  		sys.ARM64, sys.PPC64, sys.S390X)
  3189  	addF("math", "Ceil",
  3190  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3191  			return s.newValue1(ssa.OpCeil, types.Types[TFLOAT64], args[0])
  3192  		},
  3193  		sys.ARM64, sys.PPC64, sys.S390X)
  3194  	addF("math", "Floor",
  3195  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3196  			return s.newValue1(ssa.OpFloor, types.Types[TFLOAT64], args[0])
  3197  		},
  3198  		sys.ARM64, sys.PPC64, sys.S390X)
  3199  	addF("math", "Round",
  3200  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3201  			return s.newValue1(ssa.OpRound, types.Types[TFLOAT64], args[0])
  3202  		},
  3203  		sys.ARM64, sys.PPC64, sys.S390X)
  3204  	addF("math", "RoundToEven",
  3205  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3206  			return s.newValue1(ssa.OpRoundToEven, types.Types[TFLOAT64], args[0])
  3207  		},
  3208  		sys.ARM64, sys.S390X)
  3209  	addF("math", "Abs",
  3210  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3211  			return s.newValue1(ssa.OpAbs, types.Types[TFLOAT64], args[0])
  3212  		},
  3213  		sys.ARM64, sys.PPC64)
  3214  	addF("math", "Copysign",
  3215  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3216  			return s.newValue2(ssa.OpCopysign, types.Types[TFLOAT64], args[0], args[1])
  3217  		},
  3218  		sys.PPC64)
  3219  
  3220  	makeRoundAMD64 := func(op ssa.Op) func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3221  		return func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3222  			addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), x86HasSSE41, s.sb)
  3223  			v := s.load(types.Types[TBOOL], addr)
  3224  			b := s.endBlock()
  3225  			b.Kind = ssa.BlockIf
  3226  			b.SetControl(v)
  3227  			bTrue := s.f.NewBlock(ssa.BlockPlain)
  3228  			bFalse := s.f.NewBlock(ssa.BlockPlain)
  3229  			bEnd := s.f.NewBlock(ssa.BlockPlain)
  3230  			b.AddEdgeTo(bTrue)
  3231  			b.AddEdgeTo(bFalse)
  3232  			b.Likely = ssa.BranchLikely // most machines have sse4.1 nowadays
  3233  
  3234  			// We have the intrinsic - use it directly.
  3235  			s.startBlock(bTrue)
  3236  			s.vars[n] = s.newValue1(op, types.Types[TFLOAT64], args[0])
  3237  			s.endBlock().AddEdgeTo(bEnd)
  3238  
  3239  			// Call the pure Go version.
  3240  			s.startBlock(bFalse)
  3241  			a := s.call(n, callNormal)
  3242  			s.vars[n] = s.load(types.Types[TFLOAT64], a)
  3243  			s.endBlock().AddEdgeTo(bEnd)
  3244  
  3245  			// Merge results.
  3246  			s.startBlock(bEnd)
  3247  			return s.variable(n, types.Types[TFLOAT64])
  3248  		}
  3249  	}
  3250  	addF("math", "RoundToEven",
  3251  		makeRoundAMD64(ssa.OpRoundToEven),
  3252  		sys.AMD64)
  3253  	addF("math", "Floor",
  3254  		makeRoundAMD64(ssa.OpFloor),
  3255  		sys.AMD64)
  3256  	addF("math", "Ceil",
  3257  		makeRoundAMD64(ssa.OpCeil),
  3258  		sys.AMD64)
  3259  	addF("math", "Trunc",
  3260  		makeRoundAMD64(ssa.OpTrunc),
  3261  		sys.AMD64)
  3262  
  3263  	/******** math/bits ********/
  3264  	addF("math/bits", "TrailingZeros64",
  3265  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3266  			return s.newValue1(ssa.OpCtz64, types.Types[TINT], args[0])
  3267  		},
  3268  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3269  	addF("math/bits", "TrailingZeros32",
  3270  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3271  			return s.newValue1(ssa.OpCtz32, types.Types[TINT], args[0])
  3272  		},
  3273  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3274  	addF("math/bits", "TrailingZeros16",
  3275  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3276  			x := s.newValue1(ssa.OpZeroExt16to32, types.Types[TUINT32], args[0])
  3277  			c := s.constInt32(types.Types[TUINT32], 1<<16)
  3278  			y := s.newValue2(ssa.OpOr32, types.Types[TUINT32], x, c)
  3279  			return s.newValue1(ssa.OpCtz32, types.Types[TINT], y)
  3280  		},
  3281  		sys.ARM, sys.MIPS)
  3282  	addF("math/bits", "TrailingZeros16",
  3283  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3284  			return s.newValue1(ssa.OpCtz16, types.Types[TINT], args[0])
  3285  		},
  3286  		sys.AMD64)
  3287  	addF("math/bits", "TrailingZeros16",
  3288  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3289  			x := s.newValue1(ssa.OpZeroExt16to64, types.Types[TUINT64], args[0])
  3290  			c := s.constInt64(types.Types[TUINT64], 1<<16)
  3291  			y := s.newValue2(ssa.OpOr64, types.Types[TUINT64], x, c)
  3292  			return s.newValue1(ssa.OpCtz64, types.Types[TINT], y)
  3293  		},
  3294  		sys.ARM64, sys.S390X, sys.PPC64)
  3295  	addF("math/bits", "TrailingZeros8",
  3296  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3297  			x := s.newValue1(ssa.OpZeroExt8to32, types.Types[TUINT32], args[0])
  3298  			c := s.constInt32(types.Types[TUINT32], 1<<8)
  3299  			y := s.newValue2(ssa.OpOr32, types.Types[TUINT32], x, c)
  3300  			return s.newValue1(ssa.OpCtz32, types.Types[TINT], y)
  3301  		},
  3302  		sys.ARM, sys.MIPS)
  3303  	addF("math/bits", "TrailingZeros8",
  3304  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3305  			return s.newValue1(ssa.OpCtz8, types.Types[TINT], args[0])
  3306  		},
  3307  		sys.AMD64)
  3308  	addF("math/bits", "TrailingZeros8",
  3309  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3310  			x := s.newValue1(ssa.OpZeroExt8to64, types.Types[TUINT64], args[0])
  3311  			c := s.constInt64(types.Types[TUINT64], 1<<8)
  3312  			y := s.newValue2(ssa.OpOr64, types.Types[TUINT64], x, c)
  3313  			return s.newValue1(ssa.OpCtz64, types.Types[TINT], y)
  3314  		},
  3315  		sys.ARM64, sys.S390X)
  3316  	alias("math/bits", "ReverseBytes64", "runtime/internal/sys", "Bswap64", all...)
  3317  	alias("math/bits", "ReverseBytes32", "runtime/internal/sys", "Bswap32", all...)
  3318  	// ReverseBytes inlines correctly, no need to intrinsify it.
  3319  	// ReverseBytes16 lowers to a rotate, no need for anything special here.
  3320  	addF("math/bits", "Len64",
  3321  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3322  			return s.newValue1(ssa.OpBitLen64, types.Types[TINT], args[0])
  3323  		},
  3324  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3325  	addF("math/bits", "Len32",
  3326  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3327  			return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0])
  3328  		},
  3329  		sys.AMD64)
  3330  	addF("math/bits", "Len32",
  3331  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3332  			if s.config.PtrSize == 4 {
  3333  				return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0])
  3334  			}
  3335  			x := s.newValue1(ssa.OpZeroExt32to64, types.Types[TUINT64], args[0])
  3336  			return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x)
  3337  		},
  3338  		sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3339  	addF("math/bits", "Len16",
  3340  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3341  			if s.config.PtrSize == 4 {
  3342  				x := s.newValue1(ssa.OpZeroExt16to32, types.Types[TUINT32], args[0])
  3343  				return s.newValue1(ssa.OpBitLen32, types.Types[TINT], x)
  3344  			}
  3345  			x := s.newValue1(ssa.OpZeroExt16to64, types.Types[TUINT64], args[0])
  3346  			return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x)
  3347  		},
  3348  		sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3349  	addF("math/bits", "Len16",
  3350  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3351  			return s.newValue1(ssa.OpBitLen16, types.Types[TINT], args[0])
  3352  		},
  3353  		sys.AMD64)
  3354  	addF("math/bits", "Len8",
  3355  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3356  			if s.config.PtrSize == 4 {
  3357  				x := s.newValue1(ssa.OpZeroExt8to32, types.Types[TUINT32], args[0])
  3358  				return s.newValue1(ssa.OpBitLen32, types.Types[TINT], x)
  3359  			}
  3360  			x := s.newValue1(ssa.OpZeroExt8to64, types.Types[TUINT64], args[0])
  3361  			return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x)
  3362  		},
  3363  		sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3364  	addF("math/bits", "Len8",
  3365  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3366  			return s.newValue1(ssa.OpBitLen8, types.Types[TINT], args[0])
  3367  		},
  3368  		sys.AMD64)
  3369  	addF("math/bits", "Len",
  3370  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3371  			if s.config.PtrSize == 4 {
  3372  				return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0])
  3373  			}
  3374  			return s.newValue1(ssa.OpBitLen64, types.Types[TINT], args[0])
  3375  		},
  3376  		sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64)
  3377  	// LeadingZeros is handled because it trivially calls Len.
  3378  	addF("math/bits", "Reverse64",
  3379  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3380  			return s.newValue1(ssa.OpBitRev64, types.Types[TINT], args[0])
  3381  		},
  3382  		sys.ARM64)
  3383  	addF("math/bits", "Reverse32",
  3384  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3385  			return s.newValue1(ssa.OpBitRev32, types.Types[TINT], args[0])
  3386  		},
  3387  		sys.ARM64)
  3388  	addF("math/bits", "Reverse16",
  3389  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3390  			return s.newValue1(ssa.OpBitRev16, types.Types[TINT], args[0])
  3391  		},
  3392  		sys.ARM64)
  3393  	addF("math/bits", "Reverse8",
  3394  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3395  			return s.newValue1(ssa.OpBitRev8, types.Types[TINT], args[0])
  3396  		},
  3397  		sys.ARM64)
  3398  	addF("math/bits", "Reverse",
  3399  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3400  			if s.config.PtrSize == 4 {
  3401  				return s.newValue1(ssa.OpBitRev32, types.Types[TINT], args[0])
  3402  			}
  3403  			return s.newValue1(ssa.OpBitRev64, types.Types[TINT], args[0])
  3404  		},
  3405  		sys.ARM64)
  3406  	addF("math/bits", "RotateLeft8",
  3407  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3408  			return s.newValue2(ssa.OpRotateLeft8, types.Types[TUINT8], args[0], args[1])
  3409  		},
  3410  		sys.AMD64)
  3411  	addF("math/bits", "RotateLeft16",
  3412  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3413  			return s.newValue2(ssa.OpRotateLeft16, types.Types[TUINT16], args[0], args[1])
  3414  		},
  3415  		sys.AMD64)
  3416  	addF("math/bits", "RotateLeft32",
  3417  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3418  			return s.newValue2(ssa.OpRotateLeft32, types.Types[TUINT32], args[0], args[1])
  3419  		},
  3420  		sys.AMD64, sys.ARM64, sys.S390X)
  3421  	addF("math/bits", "RotateLeft64",
  3422  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3423  			return s.newValue2(ssa.OpRotateLeft64, types.Types[TUINT64], args[0], args[1])
  3424  		},
  3425  		sys.AMD64, sys.ARM64, sys.S390X)
  3426  	alias("math/bits", "RotateLeft", "math/bits", "RotateLeft64", p8...)
  3427  
  3428  	makeOnesCountAMD64 := func(op64 ssa.Op, op32 ssa.Op) func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3429  		return func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3430  			addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), x86HasPOPCNT, s.sb)
  3431  			v := s.load(types.Types[TBOOL], addr)
  3432  			b := s.endBlock()
  3433  			b.Kind = ssa.BlockIf
  3434  			b.SetControl(v)
  3435  			bTrue := s.f.NewBlock(ssa.BlockPlain)
  3436  			bFalse := s.f.NewBlock(ssa.BlockPlain)
  3437  			bEnd := s.f.NewBlock(ssa.BlockPlain)
  3438  			b.AddEdgeTo(bTrue)
  3439  			b.AddEdgeTo(bFalse)
  3440  			b.Likely = ssa.BranchLikely // most machines have popcnt nowadays
  3441  
  3442  			// We have the intrinsic - use it directly.
  3443  			s.startBlock(bTrue)
  3444  			op := op64
  3445  			if s.config.PtrSize == 4 {
  3446  				op = op32
  3447  			}
  3448  			s.vars[n] = s.newValue1(op, types.Types[TINT], args[0])
  3449  			s.endBlock().AddEdgeTo(bEnd)
  3450  
  3451  			// Call the pure Go version.
  3452  			s.startBlock(bFalse)
  3453  			a := s.call(n, callNormal)
  3454  			s.vars[n] = s.load(types.Types[TINT], a)
  3455  			s.endBlock().AddEdgeTo(bEnd)
  3456  
  3457  			// Merge results.
  3458  			s.startBlock(bEnd)
  3459  			return s.variable(n, types.Types[TINT])
  3460  		}
  3461  	}
  3462  	addF("math/bits", "OnesCount64",
  3463  		makeOnesCountAMD64(ssa.OpPopCount64, ssa.OpPopCount64),
  3464  		sys.AMD64)
  3465  	addF("math/bits", "OnesCount64",
  3466  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3467  			return s.newValue1(ssa.OpPopCount64, types.Types[TINT], args[0])
  3468  		},
  3469  		sys.PPC64, sys.ARM64, sys.S390X)
  3470  	addF("math/bits", "OnesCount32",
  3471  		makeOnesCountAMD64(ssa.OpPopCount32, ssa.OpPopCount32),
  3472  		sys.AMD64)
  3473  	addF("math/bits", "OnesCount32",
  3474  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3475  			return s.newValue1(ssa.OpPopCount32, types.Types[TINT], args[0])
  3476  		},
  3477  		sys.PPC64, sys.ARM64, sys.S390X)
  3478  	addF("math/bits", "OnesCount16",
  3479  		makeOnesCountAMD64(ssa.OpPopCount16, ssa.OpPopCount16),
  3480  		sys.AMD64)
  3481  	addF("math/bits", "OnesCount16",
  3482  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3483  			return s.newValue1(ssa.OpPopCount16, types.Types[TINT], args[0])
  3484  		},
  3485  		sys.ARM64, sys.S390X, sys.PPC64)
  3486  	addF("math/bits", "OnesCount8",
  3487  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3488  			return s.newValue1(ssa.OpPopCount8, types.Types[TINT], args[0])
  3489  		},
  3490  		sys.S390X, sys.PPC64)
  3491  	addF("math/bits", "OnesCount",
  3492  		makeOnesCountAMD64(ssa.OpPopCount64, ssa.OpPopCount32),
  3493  		sys.AMD64)
  3494  	addF("math/bits", "Mul64",
  3495  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3496  			return s.newValue2(ssa.OpMul64uhilo, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1])
  3497  		},
  3498  		sys.AMD64, sys.ARM64, sys.PPC64)
  3499  	alias("math/bits", "Mul", "math/bits", "Mul64", sys.ArchAMD64, sys.ArchARM64, sys.ArchPPC64)
  3500  	addF("math/bits", "Add64",
  3501  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3502  			return s.newValue3(ssa.OpAdd64carry, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2])
  3503  		},
  3504  		sys.AMD64)
  3505  	alias("math/bits", "Add", "math/bits", "Add64", sys.ArchAMD64)
  3506  	addF("math/bits", "Sub64",
  3507  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3508  			return s.newValue3(ssa.OpSub64borrow, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2])
  3509  		},
  3510  		sys.AMD64)
  3511  	alias("math/bits", "Sub", "math/bits", "Sub64", sys.ArchAMD64)
  3512  	addF("math/bits", "Div64",
  3513  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3514  			// check for divide-by-zero/overflow and panic with appropriate message
  3515  			cmpZero := s.newValue2(s.ssaOp(ONE, types.Types[TUINT64]), types.Types[TBOOL], args[2], s.zeroVal(types.Types[TUINT64]))
  3516  			s.check(cmpZero, panicdivide)
  3517  			cmpOverflow := s.newValue2(s.ssaOp(OLT, types.Types[TUINT64]), types.Types[TBOOL], args[0], args[2])
  3518  			s.check(cmpOverflow, panicoverflow)
  3519  			return s.newValue3(ssa.OpDiv128u, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2])
  3520  		},
  3521  		sys.AMD64)
  3522  	alias("math/bits", "Div", "math/bits", "Div64", sys.ArchAMD64)
  3523  
  3524  	/******** sync/atomic ********/
  3525  
  3526  	// Note: these are disabled by flag_race in findIntrinsic below.
  3527  	alias("sync/atomic", "LoadInt32", "runtime/internal/atomic", "Load", all...)
  3528  	alias("sync/atomic", "LoadInt64", "runtime/internal/atomic", "Load64", all...)
  3529  	alias("sync/atomic", "LoadPointer", "runtime/internal/atomic", "Loadp", all...)
  3530  	alias("sync/atomic", "LoadUint32", "runtime/internal/atomic", "Load", all...)
  3531  	alias("sync/atomic", "LoadUint64", "runtime/internal/atomic", "Load64", all...)
  3532  	alias("sync/atomic", "LoadUintptr", "runtime/internal/atomic", "Load", p4...)
  3533  	alias("sync/atomic", "LoadUintptr", "runtime/internal/atomic", "Load64", p8...)
  3534  
  3535  	alias("sync/atomic", "StoreInt32", "runtime/internal/atomic", "Store", all...)
  3536  	alias("sync/atomic", "StoreInt64", "runtime/internal/atomic", "Store64", all...)
  3537  	// Note: not StorePointer, that needs a write barrier.  Same below for {CompareAnd}Swap.
  3538  	alias("sync/atomic", "StoreUint32", "runtime/internal/atomic", "Store", all...)
  3539  	alias("sync/atomic", "StoreUint64", "runtime/internal/atomic", "Store64", all...)
  3540  	alias("sync/atomic", "StoreUintptr", "runtime/internal/atomic", "Store", p4...)
  3541  	alias("sync/atomic", "StoreUintptr", "runtime/internal/atomic", "Store64", p8...)
  3542  
  3543  	alias("sync/atomic", "SwapInt32", "runtime/internal/atomic", "Xchg", all...)
  3544  	alias("sync/atomic", "SwapInt64", "runtime/internal/atomic", "Xchg64", all...)
  3545  	alias("sync/atomic", "SwapUint32", "runtime/internal/atomic", "Xchg", all...)
  3546  	alias("sync/atomic", "SwapUint64", "runtime/internal/atomic", "Xchg64", all...)
  3547  	alias("sync/atomic", "SwapUintptr", "runtime/internal/atomic", "Xchg", p4...)
  3548  	alias("sync/atomic", "SwapUintptr", "runtime/internal/atomic", "Xchg64", p8...)
  3549  
  3550  	alias("sync/atomic", "CompareAndSwapInt32", "runtime/internal/atomic", "Cas", all...)
  3551  	alias("sync/atomic", "CompareAndSwapInt64", "runtime/internal/atomic", "Cas64", all...)
  3552  	alias("sync/atomic", "CompareAndSwapUint32", "runtime/internal/atomic", "Cas", all...)
  3553  	alias("sync/atomic", "CompareAndSwapUint64", "runtime/internal/atomic", "Cas64", all...)
  3554  	alias("sync/atomic", "CompareAndSwapUintptr", "runtime/internal/atomic", "Cas", p4...)
  3555  	alias("sync/atomic", "CompareAndSwapUintptr", "runtime/internal/atomic", "Cas64", p8...)
  3556  
  3557  	alias("sync/atomic", "AddInt32", "runtime/internal/atomic", "Xadd", all...)
  3558  	alias("sync/atomic", "AddInt64", "runtime/internal/atomic", "Xadd64", all...)
  3559  	alias("sync/atomic", "AddUint32", "runtime/internal/atomic", "Xadd", all...)
  3560  	alias("sync/atomic", "AddUint64", "runtime/internal/atomic", "Xadd64", all...)
  3561  	alias("sync/atomic", "AddUintptr", "runtime/internal/atomic", "Xadd", p4...)
  3562  	alias("sync/atomic", "AddUintptr", "runtime/internal/atomic", "Xadd64", p8...)
  3563  
  3564  	/******** math/big ********/
  3565  	add("math/big", "mulWW",
  3566  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3567  			return s.newValue2(ssa.OpMul64uhilo, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1])
  3568  		},
  3569  		sys.ArchAMD64, sys.ArchARM64, sys.ArchPPC64LE, sys.ArchPPC64)
  3570  	add("math/big", "divWW",
  3571  		func(s *state, n *Node, args []*ssa.Value) *ssa.Value {
  3572  			return s.newValue3(ssa.OpDiv128u, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2])
  3573  		},
  3574  		sys.ArchAMD64)
  3575  }
  3576  
  3577  // findIntrinsic returns a function which builds the SSA equivalent of the
  3578  // function identified by the symbol sym.  If sym is not an intrinsic call, returns nil.
  3579  func findIntrinsic(sym *types.Sym) intrinsicBuilder {
  3580  	if ssa.IntrinsicsDisable {
  3581  		return nil
  3582  	}
  3583  	if sym == nil || sym.Pkg == nil {
  3584  		return nil
  3585  	}
  3586  	pkg := sym.Pkg.Path
  3587  	if sym.Pkg == localpkg {
  3588  		pkg = myimportpath
  3589  	}
  3590  	if flag_race && pkg == "sync/atomic" {
  3591  		// The race detector needs to be able to intercept these calls.
  3592  		// We can't intrinsify them.
  3593  		return nil
  3594  	}
  3595  	// Skip intrinsifying math functions (which may contain hard-float
  3596  	// instructions) when soft-float
  3597  	if thearch.SoftFloat && pkg == "math" {
  3598  		return nil
  3599  	}
  3600  
  3601  	fn := sym.Name
  3602  	return intrinsics[intrinsicKey{thearch.LinkArch.Arch, pkg, fn}]
  3603  }
  3604  
  3605  func isIntrinsicCall(n *Node) bool {
  3606  	if n == nil || n.Left == nil {
  3607  		return false
  3608  	}
  3609  	return findIntrinsic(n.Left.Sym) != nil
  3610  }
  3611  
  3612  // intrinsicCall converts a call to a recognized intrinsic function into the intrinsic SSA operation.
  3613  func (s *state) intrinsicCall(n *Node) *ssa.Value {
  3614  	v := findIntrinsic(n.Left.Sym)(s, n, s.intrinsicArgs(n))
  3615  	if ssa.IntrinsicsDebug > 0 {
  3616  		x := v
  3617  		if x == nil {
  3618  			x = s.mem()
  3619  		}
  3620  		if x.Op == ssa.OpSelect0 || x.Op == ssa.OpSelect1 {
  3621  			x = x.Args[0]
  3622  		}
  3623  		Warnl(n.Pos, "intrinsic substitution for %v with %s", n.Left.Sym.Name, x.LongString())
  3624  	}
  3625  	return v
  3626  }
  3627  
  3628  // intrinsicArgs extracts args from n, evaluates them to SSA values, and returns them.
  3629  func (s *state) intrinsicArgs(n *Node) []*ssa.Value {
  3630  	// Construct map of temps; see comments in s.call about the structure of n.
  3631  	temps := map[*Node]*ssa.Value{}
  3632  	for _, a := range n.List.Slice() {
  3633  		if a.Op != OAS {
  3634  			s.Fatalf("non-assignment as a temp function argument %v", a.Op)
  3635  		}
  3636  		l, r := a.Left, a.Right
  3637  		if l.Op != ONAME {
  3638  			s.Fatalf("non-ONAME temp function argument %v", a.Op)
  3639  		}
  3640  		// Evaluate and store to "temporary".
  3641  		// Walk ensures these temporaries are dead outside of n.
  3642  		temps[l] = s.expr(r)
  3643  	}
  3644  	args := make([]*ssa.Value, n.Rlist.Len())
  3645  	for i, n := range n.Rlist.Slice() {
  3646  		// Store a value to an argument slot.
  3647  		if x, ok := temps[n]; ok {
  3648  			// This is a previously computed temporary.
  3649  			args[i] = x
  3650  			continue
  3651  		}
  3652  		// This is an explicit value; evaluate it.
  3653  		args[i] = s.expr(n)
  3654  	}
  3655  	return args
  3656  }
  3657  
  3658  // Calls the function n using the specified call type.
  3659  // Returns the address of the return value (or nil if none).
  3660  func (s *state) call(n *Node, k callKind) *ssa.Value {
  3661  	var sym *types.Sym     // target symbol (if static)
  3662  	var closure *ssa.Value // ptr to closure to run (if dynamic)
  3663  	var codeptr *ssa.Value // ptr to target code (if dynamic)
  3664  	var rcvr *ssa.Value    // receiver to set
  3665  	fn := n.Left
  3666  	switch n.Op {
  3667  	case OCALLFUNC:
  3668  		if k == callNormal && fn.Op == ONAME && fn.Class() == PFUNC {
  3669  			sym = fn.Sym
  3670  			break
  3671  		}
  3672  		closure = s.expr(fn)
  3673  		if thearch.LinkArch.Family == sys.Wasm || objabi.GOOS == "aix" && k != callGo {
  3674  			// On AIX, the closure needs to be verified as fn can be nil, except if it's a call go. This needs to be handled by the runtime to have the "go of nil func value" error.
  3675  			// TODO(neelance): On other architectures this should be eliminated by the optimization steps
  3676  			s.nilCheck(closure)
  3677  		}
  3678  	case OCALLMETH:
  3679  		if fn.Op != ODOTMETH {
  3680  			Fatalf("OCALLMETH: n.Left not an ODOTMETH: %v", fn)
  3681  		}
  3682  		if k == callNormal {
  3683  			sym = fn.Sym
  3684  			break
  3685  		}
  3686  		// Make a name n2 for the function.
  3687  		// fn.Sym might be sync.(*Mutex).Unlock.
  3688  		// Make a PFUNC node out of that, then evaluate it.
  3689  		// We get back an SSA value representing &sync.(*Mutex).Unlock·f.
  3690  		// We can then pass that to defer or go.
  3691  		n2 := newnamel(fn.Pos, fn.Sym)
  3692  		n2.Name.Curfn = s.curfn
  3693  		n2.SetClass(PFUNC)
  3694  		// n2.Sym already existed, so it's already marked as a function.
  3695  		n2.Pos = fn.Pos
  3696  		n2.Type = types.Types[TUINT8] // dummy type for a static closure. Could use runtime.funcval if we had it.
  3697  		closure = s.expr(n2)
  3698  		// Note: receiver is already present in n.Rlist, so we don't
  3699  		// want to set it here.
  3700  	case OCALLINTER:
  3701  		if fn.Op != ODOTINTER {
  3702  			Fatalf("OCALLINTER: n.Left not an ODOTINTER: %v", fn.Op)
  3703  		}
  3704  		i := s.expr(fn.Left)
  3705  		itab := s.newValue1(ssa.OpITab, types.Types[TUINTPTR], i)
  3706  		s.nilCheck(itab)
  3707  		itabidx := fn.Xoffset + 2*int64(Widthptr) + 8 // offset of fun field in runtime.itab
  3708  		itab = s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.UintptrPtr, itabidx, itab)
  3709  		if k == callNormal {
  3710  			codeptr = s.load(types.Types[TUINTPTR], itab)
  3711  		} else {
  3712  			closure = itab
  3713  		}
  3714  		rcvr = s.newValue1(ssa.OpIData, types.Types[TUINTPTR], i)
  3715  	}
  3716  	dowidth(fn.Type)
  3717  	stksize := fn.Type.ArgWidth() // includes receiver
  3718  
  3719  	// Run all assignments of temps.
  3720  	// The temps are introduced to avoid overwriting argument
  3721  	// slots when arguments themselves require function calls.
  3722  	s.stmtList(n.List)
  3723  
  3724  	// Store arguments to stack, including defer/go arguments and receiver for method calls.
  3725  	// These are written in SP-offset order.
  3726  	argStart := Ctxt.FixedFrameSize()
  3727  	// Defer/go args.
  3728  	if k != callNormal {
  3729  		// Write argsize and closure (args to newproc/deferproc).
  3730  		argsize := s.constInt32(types.Types[TUINT32], int32(stksize))
  3731  		addr := s.constOffPtrSP(s.f.Config.Types.UInt32Ptr, argStart)
  3732  		s.store(types.Types[TUINT32], addr, argsize)
  3733  		addr = s.constOffPtrSP(s.f.Config.Types.UintptrPtr, argStart+int64(Widthptr))
  3734  		s.store(types.Types[TUINTPTR], addr, closure)
  3735  		stksize += 2 * int64(Widthptr)
  3736  		argStart += 2 * int64(Widthptr)
  3737  	}
  3738  
  3739  	// Set receiver (for interface calls).
  3740  	if rcvr != nil {
  3741  		addr := s.constOffPtrSP(s.f.Config.Types.UintptrPtr, argStart)
  3742  		s.store(types.Types[TUINTPTR], addr, rcvr)
  3743  	}
  3744  
  3745  	// Write args.
  3746  	t := n.Left.Type
  3747  	args := n.Rlist.Slice()
  3748  	if n.Op == OCALLMETH {
  3749  		f := t.Recv()
  3750  		s.storeArg(args[0], f.Type, argStart+f.Offset)
  3751  		args = args[1:]
  3752  	}
  3753  	for i, n := range args {
  3754  		f := t.Params().Field(i)
  3755  		s.storeArg(n, f.Type, argStart+f.Offset)
  3756  	}
  3757  
  3758  	// call target
  3759  	var call *ssa.Value
  3760  	switch {
  3761  	case k == callDefer:
  3762  		call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, deferproc, s.mem())
  3763  	case k == callGo:
  3764  		call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, newproc, s.mem())
  3765  	case closure != nil:
  3766  		// rawLoad because loading the code pointer from a
  3767  		// closure is always safe, but IsSanitizerSafeAddr
  3768  		// can't always figure that out currently, and it's
  3769  		// critical that we not clobber any arguments already
  3770  		// stored onto the stack.
  3771  		codeptr = s.rawLoad(types.Types[TUINTPTR], closure)
  3772  		call = s.newValue3(ssa.OpClosureCall, types.TypeMem, codeptr, closure, s.mem())
  3773  	case codeptr != nil:
  3774  		call = s.newValue2(ssa.OpInterCall, types.TypeMem, codeptr, s.mem())
  3775  	case sym != nil:
  3776  		call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, sym.Linksym(), s.mem())
  3777  	default:
  3778  		Fatalf("bad call type %v %v", n.Op, n)
  3779  	}
  3780  	call.AuxInt = stksize // Call operations carry the argsize of the callee along with them
  3781  	s.vars[&memVar] = call
  3782  
  3783  	// Finish block for defers
  3784  	if k == callDefer {
  3785  		b := s.endBlock()
  3786  		b.Kind = ssa.BlockDefer
  3787  		b.SetControl(call)
  3788  		bNext := s.f.NewBlock(ssa.BlockPlain)
  3789  		b.AddEdgeTo(bNext)
  3790  		// Add recover edge to exit code.
  3791  		r := s.f.NewBlock(ssa.BlockPlain)
  3792  		s.startBlock(r)
  3793  		s.exit()
  3794  		b.AddEdgeTo(r)
  3795  		b.Likely = ssa.BranchLikely
  3796  		s.startBlock(bNext)
  3797  	}
  3798  
  3799  	res := n.Left.Type.Results()
  3800  	if res.NumFields() == 0 || k != callNormal {
  3801  		// call has no return value. Continue with the next statement.
  3802  		return nil
  3803  	}
  3804  	fp := res.Field(0)
  3805  	return s.constOffPtrSP(types.NewPtr(fp.Type), fp.Offset+Ctxt.FixedFrameSize())
  3806  }
  3807  
  3808  // etypesign returns the signed-ness of e, for integer/pointer etypes.
  3809  // -1 means signed, +1 means unsigned, 0 means non-integer/non-pointer.
  3810  func etypesign(e types.EType) int8 {
  3811  	switch e {
  3812  	case TINT8, TINT16, TINT32, TINT64, TINT:
  3813  		return -1
  3814  	case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR, TUNSAFEPTR:
  3815  		return +1
  3816  	}
  3817  	return 0
  3818  }
  3819  
  3820  // addr converts the address of the expression n to SSA, adds it to s and returns the SSA result.
  3821  // The value that the returned Value represents is guaranteed to be non-nil.
  3822  // If bounded is true then this address does not require a nil check for its operand
  3823  // even if that would otherwise be implied.
  3824  func (s *state) addr(n *Node, bounded bool) *ssa.Value {
  3825  	t := types.NewPtr(n.Type)
  3826  	switch n.Op {
  3827  	case ONAME:
  3828  		switch n.Class() {
  3829  		case PEXTERN:
  3830  			// global variable
  3831  			v := s.entryNewValue1A(ssa.OpAddr, t, n.Sym.Linksym(), s.sb)
  3832  			// TODO: Make OpAddr use AuxInt as well as Aux.
  3833  			if n.Xoffset != 0 {
  3834  				v = s.entryNewValue1I(ssa.OpOffPtr, v.Type, n.Xoffset, v)
  3835  			}
  3836  			return v
  3837  		case PPARAM:
  3838  			// parameter slot
  3839  			v := s.decladdrs[n]
  3840  			if v != nil {
  3841  				return v
  3842  			}
  3843  			if n == nodfp {
  3844  				// Special arg that points to the frame pointer (Used by ORECOVER).
  3845  				return s.entryNewValue2A(ssa.OpLocalAddr, t, n, s.sp, s.startmem)
  3846  			}
  3847  			s.Fatalf("addr of undeclared ONAME %v. declared: %v", n, s.decladdrs)
  3848  			return nil
  3849  		case PAUTO:
  3850  			return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), !n.IsAutoTmp())
  3851  
  3852  		case PPARAMOUT: // Same as PAUTO -- cannot generate LEA early.
  3853  			// ensure that we reuse symbols for out parameters so
  3854  			// that cse works on their addresses
  3855  			return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), true)
  3856  		default:
  3857  			s.Fatalf("variable address class %v not implemented", n.Class())
  3858  			return nil
  3859  		}
  3860  	case OINDREGSP:
  3861  		// indirect off REGSP
  3862  		// used for storing/loading arguments/returns to/from callees
  3863  		return s.constOffPtrSP(t, n.Xoffset)
  3864  	case OINDEX:
  3865  		if n.Left.Type.IsSlice() {
  3866  			a := s.expr(n.Left)
  3867  			i := s.expr(n.Right)
  3868  			i = s.extendIndex(i, panicindex)
  3869  			len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], a)
  3870  			if !n.Bounded() {
  3871  				s.boundsCheck(i, len)
  3872  			}
  3873  			p := s.newValue1(ssa.OpSlicePtr, t, a)
  3874  			return s.newValue2(ssa.OpPtrIndex, t, p, i)
  3875  		} else { // array
  3876  			a := s.addr(n.Left, bounded)
  3877  			i := s.expr(n.Right)
  3878  			i = s.extendIndex(i, panicindex)
  3879  			len := s.constInt(types.Types[TINT], n.Left.Type.NumElem())
  3880  			if !n.Bounded() {
  3881  				s.boundsCheck(i, len)
  3882  			}
  3883  			return s.newValue2(ssa.OpPtrIndex, types.NewPtr(n.Left.Type.Elem()), a, i)
  3884  		}
  3885  	case ODEREF:
  3886  		return s.exprPtr(n.Left, bounded, n.Pos)
  3887  	case ODOT:
  3888  		p := s.addr(n.Left, bounded)
  3889  		return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p)
  3890  	case ODOTPTR:
  3891  		p := s.exprPtr(n.Left, bounded, n.Pos)
  3892  		return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p)
  3893  	case OCLOSUREVAR:
  3894  		return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset,
  3895  			s.entryNewValue0(ssa.OpGetClosurePtr, s.f.Config.Types.BytePtr))
  3896  	case OCONVNOP:
  3897  		addr := s.addr(n.Left, bounded)
  3898  		return s.newValue1(ssa.OpCopy, t, addr) // ensure that addr has the right type
  3899  	case OCALLFUNC, OCALLINTER, OCALLMETH:
  3900  		return s.call(n, callNormal)
  3901  	case ODOTTYPE:
  3902  		v, _ := s.dottype(n, false)
  3903  		if v.Op != ssa.OpLoad {
  3904  			s.Fatalf("dottype of non-load")
  3905  		}
  3906  		if v.Args[1] != s.mem() {
  3907  			s.Fatalf("memory no longer live from dottype load")
  3908  		}
  3909  		return v.Args[0]
  3910  	default:
  3911  		s.Fatalf("unhandled addr %v", n.Op)
  3912  		return nil
  3913  	}
  3914  }
  3915  
  3916  // canSSA reports whether n is SSA-able.
  3917  // n must be an ONAME (or an ODOT sequence with an ONAME base).
  3918  func (s *state) canSSA(n *Node) bool {
  3919  	if Debug['N'] != 0 {
  3920  		return false
  3921  	}
  3922  	for n.Op == ODOT || (n.Op == OINDEX && n.Left.Type.IsArray()) {
  3923  		n = n.Left
  3924  	}
  3925  	if n.Op != ONAME {
  3926  		return false
  3927  	}
  3928  	if n.Addrtaken() {
  3929  		return false
  3930  	}
  3931  	if n.isParamHeapCopy() {
  3932  		return false
  3933  	}
  3934  	if n.Class() == PAUTOHEAP {
  3935  		Fatalf("canSSA of PAUTOHEAP %v", n)
  3936  	}
  3937  	switch n.Class() {
  3938  	case PEXTERN:
  3939  		return false
  3940  	case PPARAMOUT:
  3941  		if s.hasdefer {
  3942  			// TODO: handle this case? Named return values must be
  3943  			// in memory so that the deferred function can see them.
  3944  			// Maybe do: if !strings.HasPrefix(n.String(), "~") { return false }
  3945  			// Or maybe not, see issue 18860.  Even unnamed return values
  3946  			// must be written back so if a defer recovers, the caller can see them.
  3947  			return false
  3948  		}
  3949  		if s.cgoUnsafeArgs {
  3950  			// Cgo effectively takes the address of all result args,
  3951  			// but the compiler can't see that.
  3952  			return false
  3953  		}
  3954  	}
  3955  	if n.Class() == PPARAM && n.Sym != nil && n.Sym.Name == ".this" {
  3956  		// wrappers generated by genwrapper need to update
  3957  		// the .this pointer in place.
  3958  		// TODO: treat as a PPARMOUT?
  3959  		return false
  3960  	}
  3961  	return canSSAType(n.Type)
  3962  	// TODO: try to make more variables SSAable?
  3963  }
  3964  
  3965  // canSSA reports whether variables of type t are SSA-able.
  3966  func canSSAType(t *types.Type) bool {
  3967  	dowidth(t)
  3968  	if t.Width > int64(4*Widthptr) {
  3969  		// 4*Widthptr is an arbitrary constant. We want it
  3970  		// to be at least 3*Widthptr so slices can be registerized.
  3971  		// Too big and we'll introduce too much register pressure.
  3972  		return false
  3973  	}
  3974  	switch t.Etype {
  3975  	case TARRAY:
  3976  		// We can't do larger arrays because dynamic indexing is
  3977  		// not supported on SSA variables.
  3978  		// TODO: allow if all indexes are constant.
  3979  		if t.NumElem() <= 1 {
  3980  			return canSSAType(t.Elem())
  3981  		}
  3982  		return false
  3983  	case TSTRUCT:
  3984  		if t.NumFields() > ssa.MaxStruct {
  3985  			return false
  3986  		}
  3987  		for _, t1 := range t.Fields().Slice() {
  3988  			if !canSSAType(t1.Type) {
  3989  				return false
  3990  			}
  3991  		}
  3992  		return true
  3993  	default:
  3994  		return true
  3995  	}
  3996  }
  3997  
  3998  // exprPtr evaluates n to a pointer and nil-checks it.
  3999  func (s *state) exprPtr(n *Node, bounded bool, lineno src.XPos) *ssa.Value {
  4000  	p := s.expr(n)
  4001  	if bounded || n.NonNil() {
  4002  		if s.f.Frontend().Debug_checknil() && lineno.Line() > 1 {
  4003  			s.f.Warnl(lineno, "removed nil check")
  4004  		}
  4005  		return p
  4006  	}
  4007  	s.nilCheck(p)
  4008  	return p
  4009  }
  4010  
  4011  // nilCheck generates nil pointer checking code.
  4012  // Used only for automatically inserted nil checks,
  4013  // not for user code like 'x != nil'.
  4014  func (s *state) nilCheck(ptr *ssa.Value) {
  4015  	if disable_checknil != 0 || s.curfn.Func.NilCheckDisabled() {
  4016  		return
  4017  	}
  4018  	s.newValue2(ssa.OpNilCheck, types.TypeVoid, ptr, s.mem())
  4019  }
  4020  
  4021  // boundsCheck generates bounds checking code. Checks if 0 <= idx < len, branches to exit if not.
  4022  // Starts a new block on return.
  4023  // idx is already converted to full int width.
  4024  func (s *state) boundsCheck(idx, len *ssa.Value) {
  4025  	if Debug['B'] != 0 {
  4026  		return
  4027  	}
  4028  
  4029  	// bounds check
  4030  	cmp := s.newValue2(ssa.OpIsInBounds, types.Types[TBOOL], idx, len)
  4031  	s.check(cmp, panicindex)
  4032  }
  4033  
  4034  func couldBeNegative(v *ssa.Value) bool {
  4035  	switch v.Op {
  4036  	case ssa.OpSliceLen, ssa.OpSliceCap, ssa.OpStringLen:
  4037  		return false
  4038  	case ssa.OpConst64:
  4039  		return v.AuxInt < 0
  4040  	case ssa.OpConst32:
  4041  		return int32(v.AuxInt) < 0
  4042  	}
  4043  	return true
  4044  }
  4045  
  4046  // sliceBoundsCheck generates slice bounds checking code. Checks if 0 <= idx <= len, branches to exit if not.
  4047  // Starts a new block on return.
  4048  // idx and len are already converted to full int width.
  4049  func (s *state) sliceBoundsCheck(idx, len *ssa.Value) {
  4050  	if Debug['B'] != 0 {
  4051  		return
  4052  	}
  4053  	if couldBeNegative(len) {
  4054  		// OpIsSliceInBounds requires second arg not negative; if it's not obviously true, must check.
  4055  		cmpop := ssa.OpGeq64
  4056  		if len.Type.Size() == 4 {
  4057  			cmpop = ssa.OpGeq32
  4058  		}
  4059  		cmp := s.newValue2(cmpop, types.Types[TBOOL], len, s.zeroVal(len.Type))
  4060  		s.check(cmp, panicslice)
  4061  	}
  4062  
  4063  	// bounds check
  4064  	cmp := s.newValue2(ssa.OpIsSliceInBounds, types.Types[TBOOL], idx, len)
  4065  	s.check(cmp, panicslice)
  4066  }
  4067  
  4068  // If cmp (a bool) is false, panic using the given function.
  4069  func (s *state) check(cmp *ssa.Value, fn *obj.LSym) {
  4070  	b := s.endBlock()
  4071  	b.Kind = ssa.BlockIf
  4072  	b.SetControl(cmp)
  4073  	b.Likely = ssa.BranchLikely
  4074  	bNext := s.f.NewBlock(ssa.BlockPlain)
  4075  	line := s.peekPos()
  4076  	pos := Ctxt.PosTable.Pos(line)
  4077  	fl := funcLine{f: fn, base: pos.Base(), line: pos.Line()}
  4078  	bPanic := s.panics[fl]
  4079  	if bPanic == nil {
  4080  		bPanic = s.f.NewBlock(ssa.BlockPlain)
  4081  		s.panics[fl] = bPanic
  4082  		s.startBlock(bPanic)
  4083  		// The panic call takes/returns memory to ensure that the right
  4084  		// memory state is observed if the panic happens.
  4085  		s.rtcall(fn, false, nil)
  4086  	}
  4087  	b.AddEdgeTo(bNext)
  4088  	b.AddEdgeTo(bPanic)
  4089  	s.startBlock(bNext)
  4090  }
  4091  
  4092  func (s *state) intDivide(n *Node, a, b *ssa.Value) *ssa.Value {
  4093  	needcheck := true
  4094  	switch b.Op {
  4095  	case ssa.OpConst8, ssa.OpConst16, ssa.OpConst32, ssa.OpConst64:
  4096  		if b.AuxInt != 0 {
  4097  			needcheck = false
  4098  		}
  4099  	}
  4100  	if needcheck {
  4101  		// do a size-appropriate check for zero
  4102  		cmp := s.newValue2(s.ssaOp(ONE, n.Type), types.Types[TBOOL], b, s.zeroVal(n.Type))
  4103  		s.check(cmp, panicdivide)
  4104  	}
  4105  	return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
  4106  }
  4107  
  4108  // rtcall issues a call to the given runtime function fn with the listed args.
  4109  // Returns a slice of results of the given result types.
  4110  // The call is added to the end of the current block.
  4111  // If returns is false, the block is marked as an exit block.
  4112  func (s *state) rtcall(fn *obj.LSym, returns bool, results []*types.Type, args ...*ssa.Value) []*ssa.Value {
  4113  	// Write args to the stack
  4114  	off := Ctxt.FixedFrameSize()
  4115  	for _, arg := range args {
  4116  		t := arg.Type
  4117  		off = Rnd(off, t.Alignment())
  4118  		ptr := s.constOffPtrSP(t.PtrTo(), off)
  4119  		size := t.Size()
  4120  		s.store(t, ptr, arg)
  4121  		off += size
  4122  	}
  4123  	off = Rnd(off, int64(Widthreg))
  4124  
  4125  	// Issue call
  4126  	call := s.newValue1A(ssa.OpStaticCall, types.TypeMem, fn, s.mem())
  4127  	s.vars[&memVar] = call
  4128  
  4129  	if !returns {
  4130  		// Finish block
  4131  		b := s.endBlock()
  4132  		b.Kind = ssa.BlockExit
  4133  		b.SetControl(call)
  4134  		call.AuxInt = off - Ctxt.FixedFrameSize()
  4135  		if len(results) > 0 {
  4136  			Fatalf("panic call can't have results")
  4137  		}
  4138  		return nil
  4139  	}
  4140  
  4141  	// Load results
  4142  	res := make([]*ssa.Value, len(results))
  4143  	for i, t := range results {
  4144  		off = Rnd(off, t.Alignment())
  4145  		ptr := s.constOffPtrSP(types.NewPtr(t), off)
  4146  		res[i] = s.load(t, ptr)
  4147  		off += t.Size()
  4148  	}
  4149  	off = Rnd(off, int64(Widthptr))
  4150  
  4151  	// Remember how much callee stack space we needed.
  4152  	call.AuxInt = off
  4153  
  4154  	return res
  4155  }
  4156  
  4157  // do *left = right for type t.
  4158  func (s *state) storeType(t *types.Type, left, right *ssa.Value, skip skipMask, leftIsStmt bool) {
  4159  	s.instrument(t, left, true)
  4160  
  4161  	if skip == 0 && (!types.Haspointers(t) || ssa.IsStackAddr(left)) {
  4162  		// Known to not have write barrier. Store the whole type.
  4163  		s.vars[&memVar] = s.newValue3Apos(ssa.OpStore, types.TypeMem, t, left, right, s.mem(), leftIsStmt)
  4164  		return
  4165  	}
  4166  
  4167  	// store scalar fields first, so write barrier stores for
  4168  	// pointer fields can be grouped together, and scalar values
  4169  	// don't need to be live across the write barrier call.
  4170  	// TODO: if the writebarrier pass knows how to reorder stores,
  4171  	// we can do a single store here as long as skip==0.
  4172  	s.storeTypeScalars(t, left, right, skip)
  4173  	if skip&skipPtr == 0 && types.Haspointers(t) {
  4174  		s.storeTypePtrs(t, left, right)
  4175  	}
  4176  }
  4177  
  4178  // do *left = right for all scalar (non-pointer) parts of t.
  4179  func (s *state) storeTypeScalars(t *types.Type, left, right *ssa.Value, skip skipMask) {
  4180  	switch {
  4181  	case t.IsBoolean() || t.IsInteger() || t.IsFloat() || t.IsComplex():
  4182  		s.store(t, left, right)
  4183  	case t.IsPtrShaped():
  4184  		// no scalar fields.
  4185  	case t.IsString():
  4186  		if skip&skipLen != 0 {
  4187  			return
  4188  		}
  4189  		len := s.newValue1(ssa.OpStringLen, types.Types[TINT], right)
  4190  		lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left)
  4191  		s.store(types.Types[TINT], lenAddr, len)
  4192  	case t.IsSlice():
  4193  		if skip&skipLen == 0 {
  4194  			len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], right)
  4195  			lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left)
  4196  			s.store(types.Types[TINT], lenAddr, len)
  4197  		}
  4198  		if skip&skipCap == 0 {
  4199  			cap := s.newValue1(ssa.OpSliceCap, types.Types[TINT], right)
  4200  			capAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, 2*s.config.PtrSize, left)
  4201  			s.store(types.Types[TINT], capAddr, cap)
  4202  		}
  4203  	case t.IsInterface():
  4204  		// itab field doesn't need a write barrier (even though it is a pointer).
  4205  		itab := s.newValue1(ssa.OpITab, s.f.Config.Types.BytePtr, right)
  4206  		s.store(types.Types[TUINTPTR], left, itab)
  4207  	case t.IsStruct():
  4208  		n := t.NumFields()
  4209  		for i := 0; i < n; i++ {
  4210  			ft := t.FieldType(i)
  4211  			addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
  4212  			val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
  4213  			s.storeTypeScalars(ft, addr, val, 0)
  4214  		}
  4215  	case t.IsArray() && t.NumElem() == 0:
  4216  		// nothing
  4217  	case t.IsArray() && t.NumElem() == 1:
  4218  		s.storeTypeScalars(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right), 0)
  4219  	default:
  4220  		s.Fatalf("bad write barrier type %v", t)
  4221  	}
  4222  }
  4223  
  4224  // do *left = right for all pointer parts of t.
  4225  func (s *state) storeTypePtrs(t *types.Type, left, right *ssa.Value) {
  4226  	switch {
  4227  	case t.IsPtrShaped():
  4228  		s.store(t, left, right)
  4229  	case t.IsString():
  4230  		ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, right)
  4231  		s.store(s.f.Config.Types.BytePtr, left, ptr)
  4232  	case t.IsSlice():
  4233  		elType := types.NewPtr(t.Elem())
  4234  		ptr := s.newValue1(ssa.OpSlicePtr, elType, right)
  4235  		s.store(elType, left, ptr)
  4236  	case t.IsInterface():
  4237  		// itab field is treated as a scalar.
  4238  		idata := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, right)
  4239  		idataAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.BytePtrPtr, s.config.PtrSize, left)
  4240  		s.store(s.f.Config.Types.BytePtr, idataAddr, idata)
  4241  	case t.IsStruct():
  4242  		n := t.NumFields()
  4243  		for i := 0; i < n; i++ {
  4244  			ft := t.FieldType(i)
  4245  			if !types.Haspointers(ft) {
  4246  				continue
  4247  			}
  4248  			addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
  4249  			val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
  4250  			s.storeTypePtrs(ft, addr, val)
  4251  		}
  4252  	case t.IsArray() && t.NumElem() == 0:
  4253  		// nothing
  4254  	case t.IsArray() && t.NumElem() == 1:
  4255  		s.storeTypePtrs(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right))
  4256  	default:
  4257  		s.Fatalf("bad write barrier type %v", t)
  4258  	}
  4259  }
  4260  
  4261  func (s *state) storeArg(n *Node, t *types.Type, off int64) {
  4262  	pt := types.NewPtr(t)
  4263  	sp := s.constOffPtrSP(pt, off)
  4264  
  4265  	if !canSSAType(t) {
  4266  		a := s.addr(n, false)
  4267  		s.move(t, sp, a)
  4268  		return
  4269  	}
  4270  
  4271  	a := s.expr(n)
  4272  	s.storeType(t, sp, a, 0, false)
  4273  }
  4274  
  4275  // slice computes the slice v[i:j:k] and returns ptr, len, and cap of result.
  4276  // i,j,k may be nil, in which case they are set to their default value.
  4277  // t is a slice, ptr to array, or string type.
  4278  func (s *state) slice(t *types.Type, v, i, j, k *ssa.Value, bounded bool) (p, l, c *ssa.Value) {
  4279  	var elemtype *types.Type
  4280  	var ptrtype *types.Type
  4281  	var ptr *ssa.Value
  4282  	var len *ssa.Value
  4283  	var cap *ssa.Value
  4284  	zero := s.constInt(types.Types[TINT], 0)
  4285  	switch {
  4286  	case t.IsSlice():
  4287  		elemtype = t.Elem()
  4288  		ptrtype = types.NewPtr(elemtype)
  4289  		ptr = s.newValue1(ssa.OpSlicePtr, ptrtype, v)
  4290  		len = s.newValue1(ssa.OpSliceLen, types.Types[TINT], v)
  4291  		cap = s.newValue1(ssa.OpSliceCap, types.Types[TINT], v)
  4292  	case t.IsString():
  4293  		elemtype = types.Types[TUINT8]
  4294  		ptrtype = types.NewPtr(elemtype)
  4295  		ptr = s.newValue1(ssa.OpStringPtr, ptrtype, v)
  4296  		len = s.newValue1(ssa.OpStringLen, types.Types[TINT], v)
  4297  		cap = len
  4298  	case t.IsPtr():
  4299  		if !t.Elem().IsArray() {
  4300  			s.Fatalf("bad ptr to array in slice %v\n", t)
  4301  		}
  4302  		elemtype = t.Elem().Elem()
  4303  		ptrtype = types.NewPtr(elemtype)
  4304  		s.nilCheck(v)
  4305  		ptr = v
  4306  		len = s.constInt(types.Types[TINT], t.Elem().NumElem())
  4307  		cap = len
  4308  	default:
  4309  		s.Fatalf("bad type in slice %v\n", t)
  4310  	}
  4311  
  4312  	// Set default values
  4313  	if i == nil {
  4314  		i = zero
  4315  	}
  4316  	if j == nil {
  4317  		j = len
  4318  	}
  4319  	if k == nil {
  4320  		k = cap
  4321  	}
  4322  
  4323  	if !bounded {
  4324  		// Panic if slice indices are not in bounds.
  4325  		s.sliceBoundsCheck(i, j)
  4326  		if j != k {
  4327  			s.sliceBoundsCheck(j, k)
  4328  		}
  4329  		if k != cap {
  4330  			s.sliceBoundsCheck(k, cap)
  4331  		}
  4332  	}
  4333  
  4334  	// Generate the following code assuming that indexes are in bounds.
  4335  	// The masking is to make sure that we don't generate a slice
  4336  	// that points to the next object in memory.
  4337  	// rlen = j - i
  4338  	// rcap = k - i
  4339  	// delta = i * elemsize
  4340  	// rptr = p + delta&mask(rcap)
  4341  	// result = (SliceMake rptr rlen rcap)
  4342  	// where mask(x) is 0 if x==0 and -1 if x>0.
  4343  	subOp := s.ssaOp(OSUB, types.Types[TINT])
  4344  	mulOp := s.ssaOp(OMUL, types.Types[TINT])
  4345  	andOp := s.ssaOp(OAND, types.Types[TINT])
  4346  	rlen := s.newValue2(subOp, types.Types[TINT], j, i)
  4347  	var rcap *ssa.Value
  4348  	switch {
  4349  	case t.IsString():
  4350  		// Capacity of the result is unimportant. However, we use
  4351  		// rcap to test if we've generated a zero-length slice.
  4352  		// Use length of strings for that.
  4353  		rcap = rlen
  4354  	case j == k:
  4355  		rcap = rlen
  4356  	default:
  4357  		rcap = s.newValue2(subOp, types.Types[TINT], k, i)
  4358  	}
  4359  
  4360  	var rptr *ssa.Value
  4361  	if (i.Op == ssa.OpConst64 || i.Op == ssa.OpConst32) && i.AuxInt == 0 {
  4362  		// No pointer arithmetic necessary.
  4363  		rptr = ptr
  4364  	} else {
  4365  		// delta = # of bytes to offset pointer by.
  4366  		delta := s.newValue2(mulOp, types.Types[TINT], i, s.constInt(types.Types[TINT], elemtype.Width))
  4367  		// If we're slicing to the point where the capacity is zero,
  4368  		// zero out the delta.
  4369  		mask := s.newValue1(ssa.OpSlicemask, types.Types[TINT], rcap)
  4370  		delta = s.newValue2(andOp, types.Types[TINT], delta, mask)
  4371  		// Compute rptr = ptr + delta
  4372  		rptr = s.newValue2(ssa.OpAddPtr, ptrtype, ptr, delta)
  4373  	}
  4374  
  4375  	return rptr, rlen, rcap
  4376  }
  4377  
  4378  type u642fcvtTab struct {
  4379  	geq, cvt2F, and, rsh, or, add ssa.Op
  4380  	one                           func(*state, *types.Type, int64) *ssa.Value
  4381  }
  4382  
  4383  var u64_f64 = u642fcvtTab{
  4384  	geq:   ssa.OpGeq64,
  4385  	cvt2F: ssa.OpCvt64to64F,
  4386  	and:   ssa.OpAnd64,
  4387  	rsh:   ssa.OpRsh64Ux64,
  4388  	or:    ssa.OpOr64,
  4389  	add:   ssa.OpAdd64F,
  4390  	one:   (*state).constInt64,
  4391  }
  4392  
  4393  var u64_f32 = u642fcvtTab{
  4394  	geq:   ssa.OpGeq64,
  4395  	cvt2F: ssa.OpCvt64to32F,
  4396  	and:   ssa.OpAnd64,
  4397  	rsh:   ssa.OpRsh64Ux64,
  4398  	or:    ssa.OpOr64,
  4399  	add:   ssa.OpAdd32F,
  4400  	one:   (*state).constInt64,
  4401  }
  4402  
  4403  func (s *state) uint64Tofloat64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4404  	return s.uint64Tofloat(&u64_f64, n, x, ft, tt)
  4405  }
  4406  
  4407  func (s *state) uint64Tofloat32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4408  	return s.uint64Tofloat(&u64_f32, n, x, ft, tt)
  4409  }
  4410  
  4411  func (s *state) uint64Tofloat(cvttab *u642fcvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4412  	// if x >= 0 {
  4413  	//    result = (floatY) x
  4414  	// } else {
  4415  	// 	  y = uintX(x) ; y = x & 1
  4416  	// 	  z = uintX(x) ; z = z >> 1
  4417  	// 	  z = z >> 1
  4418  	// 	  z = z | y
  4419  	// 	  result = floatY(z)
  4420  	// 	  result = result + result
  4421  	// }
  4422  	//
  4423  	// Code borrowed from old code generator.
  4424  	// What's going on: large 64-bit "unsigned" looks like
  4425  	// negative number to hardware's integer-to-float
  4426  	// conversion. However, because the mantissa is only
  4427  	// 63 bits, we don't need the LSB, so instead we do an
  4428  	// unsigned right shift (divide by two), convert, and
  4429  	// double. However, before we do that, we need to be
  4430  	// sure that we do not lose a "1" if that made the
  4431  	// difference in the resulting rounding. Therefore, we
  4432  	// preserve it, and OR (not ADD) it back in. The case
  4433  	// that matters is when the eleven discarded bits are
  4434  	// equal to 10000000001; that rounds up, and the 1 cannot
  4435  	// be lost else it would round down if the LSB of the
  4436  	// candidate mantissa is 0.
  4437  	cmp := s.newValue2(cvttab.geq, types.Types[TBOOL], x, s.zeroVal(ft))
  4438  	b := s.endBlock()
  4439  	b.Kind = ssa.BlockIf
  4440  	b.SetControl(cmp)
  4441  	b.Likely = ssa.BranchLikely
  4442  
  4443  	bThen := s.f.NewBlock(ssa.BlockPlain)
  4444  	bElse := s.f.NewBlock(ssa.BlockPlain)
  4445  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  4446  
  4447  	b.AddEdgeTo(bThen)
  4448  	s.startBlock(bThen)
  4449  	a0 := s.newValue1(cvttab.cvt2F, tt, x)
  4450  	s.vars[n] = a0
  4451  	s.endBlock()
  4452  	bThen.AddEdgeTo(bAfter)
  4453  
  4454  	b.AddEdgeTo(bElse)
  4455  	s.startBlock(bElse)
  4456  	one := cvttab.one(s, ft, 1)
  4457  	y := s.newValue2(cvttab.and, ft, x, one)
  4458  	z := s.newValue2(cvttab.rsh, ft, x, one)
  4459  	z = s.newValue2(cvttab.or, ft, z, y)
  4460  	a := s.newValue1(cvttab.cvt2F, tt, z)
  4461  	a1 := s.newValue2(cvttab.add, tt, a, a)
  4462  	s.vars[n] = a1
  4463  	s.endBlock()
  4464  	bElse.AddEdgeTo(bAfter)
  4465  
  4466  	s.startBlock(bAfter)
  4467  	return s.variable(n, n.Type)
  4468  }
  4469  
  4470  type u322fcvtTab struct {
  4471  	cvtI2F, cvtF2F ssa.Op
  4472  }
  4473  
  4474  var u32_f64 = u322fcvtTab{
  4475  	cvtI2F: ssa.OpCvt32to64F,
  4476  	cvtF2F: ssa.OpCopy,
  4477  }
  4478  
  4479  var u32_f32 = u322fcvtTab{
  4480  	cvtI2F: ssa.OpCvt32to32F,
  4481  	cvtF2F: ssa.OpCvt64Fto32F,
  4482  }
  4483  
  4484  func (s *state) uint32Tofloat64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4485  	return s.uint32Tofloat(&u32_f64, n, x, ft, tt)
  4486  }
  4487  
  4488  func (s *state) uint32Tofloat32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4489  	return s.uint32Tofloat(&u32_f32, n, x, ft, tt)
  4490  }
  4491  
  4492  func (s *state) uint32Tofloat(cvttab *u322fcvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4493  	// if x >= 0 {
  4494  	// 	result = floatY(x)
  4495  	// } else {
  4496  	// 	result = floatY(float64(x) + (1<<32))
  4497  	// }
  4498  	cmp := s.newValue2(ssa.OpGeq32, types.Types[TBOOL], x, s.zeroVal(ft))
  4499  	b := s.endBlock()
  4500  	b.Kind = ssa.BlockIf
  4501  	b.SetControl(cmp)
  4502  	b.Likely = ssa.BranchLikely
  4503  
  4504  	bThen := s.f.NewBlock(ssa.BlockPlain)
  4505  	bElse := s.f.NewBlock(ssa.BlockPlain)
  4506  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  4507  
  4508  	b.AddEdgeTo(bThen)
  4509  	s.startBlock(bThen)
  4510  	a0 := s.newValue1(cvttab.cvtI2F, tt, x)
  4511  	s.vars[n] = a0
  4512  	s.endBlock()
  4513  	bThen.AddEdgeTo(bAfter)
  4514  
  4515  	b.AddEdgeTo(bElse)
  4516  	s.startBlock(bElse)
  4517  	a1 := s.newValue1(ssa.OpCvt32to64F, types.Types[TFLOAT64], x)
  4518  	twoToThe32 := s.constFloat64(types.Types[TFLOAT64], float64(1<<32))
  4519  	a2 := s.newValue2(ssa.OpAdd64F, types.Types[TFLOAT64], a1, twoToThe32)
  4520  	a3 := s.newValue1(cvttab.cvtF2F, tt, a2)
  4521  
  4522  	s.vars[n] = a3
  4523  	s.endBlock()
  4524  	bElse.AddEdgeTo(bAfter)
  4525  
  4526  	s.startBlock(bAfter)
  4527  	return s.variable(n, n.Type)
  4528  }
  4529  
  4530  // referenceTypeBuiltin generates code for the len/cap builtins for maps and channels.
  4531  func (s *state) referenceTypeBuiltin(n *Node, x *ssa.Value) *ssa.Value {
  4532  	if !n.Left.Type.IsMap() && !n.Left.Type.IsChan() {
  4533  		s.Fatalf("node must be a map or a channel")
  4534  	}
  4535  	// if n == nil {
  4536  	//   return 0
  4537  	// } else {
  4538  	//   // len
  4539  	//   return *((*int)n)
  4540  	//   // cap
  4541  	//   return *(((*int)n)+1)
  4542  	// }
  4543  	lenType := n.Type
  4544  	nilValue := s.constNil(types.Types[TUINTPTR])
  4545  	cmp := s.newValue2(ssa.OpEqPtr, types.Types[TBOOL], x, nilValue)
  4546  	b := s.endBlock()
  4547  	b.Kind = ssa.BlockIf
  4548  	b.SetControl(cmp)
  4549  	b.Likely = ssa.BranchUnlikely
  4550  
  4551  	bThen := s.f.NewBlock(ssa.BlockPlain)
  4552  	bElse := s.f.NewBlock(ssa.BlockPlain)
  4553  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  4554  
  4555  	// length/capacity of a nil map/chan is zero
  4556  	b.AddEdgeTo(bThen)
  4557  	s.startBlock(bThen)
  4558  	s.vars[n] = s.zeroVal(lenType)
  4559  	s.endBlock()
  4560  	bThen.AddEdgeTo(bAfter)
  4561  
  4562  	b.AddEdgeTo(bElse)
  4563  	s.startBlock(bElse)
  4564  	switch n.Op {
  4565  	case OLEN:
  4566  		// length is stored in the first word for map/chan
  4567  		s.vars[n] = s.load(lenType, x)
  4568  	case OCAP:
  4569  		// capacity is stored in the second word for chan
  4570  		sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Width, x)
  4571  		s.vars[n] = s.load(lenType, sw)
  4572  	default:
  4573  		s.Fatalf("op must be OLEN or OCAP")
  4574  	}
  4575  	s.endBlock()
  4576  	bElse.AddEdgeTo(bAfter)
  4577  
  4578  	s.startBlock(bAfter)
  4579  	return s.variable(n, lenType)
  4580  }
  4581  
  4582  type f2uCvtTab struct {
  4583  	ltf, cvt2U, subf, or ssa.Op
  4584  	floatValue           func(*state, *types.Type, float64) *ssa.Value
  4585  	intValue             func(*state, *types.Type, int64) *ssa.Value
  4586  	cutoff               uint64
  4587  }
  4588  
  4589  var f32_u64 = f2uCvtTab{
  4590  	ltf:        ssa.OpLess32F,
  4591  	cvt2U:      ssa.OpCvt32Fto64,
  4592  	subf:       ssa.OpSub32F,
  4593  	or:         ssa.OpOr64,
  4594  	floatValue: (*state).constFloat32,
  4595  	intValue:   (*state).constInt64,
  4596  	cutoff:     9223372036854775808,
  4597  }
  4598  
  4599  var f64_u64 = f2uCvtTab{
  4600  	ltf:        ssa.OpLess64F,
  4601  	cvt2U:      ssa.OpCvt64Fto64,
  4602  	subf:       ssa.OpSub64F,
  4603  	or:         ssa.OpOr64,
  4604  	floatValue: (*state).constFloat64,
  4605  	intValue:   (*state).constInt64,
  4606  	cutoff:     9223372036854775808,
  4607  }
  4608  
  4609  var f32_u32 = f2uCvtTab{
  4610  	ltf:        ssa.OpLess32F,
  4611  	cvt2U:      ssa.OpCvt32Fto32,
  4612  	subf:       ssa.OpSub32F,
  4613  	or:         ssa.OpOr32,
  4614  	floatValue: (*state).constFloat32,
  4615  	intValue:   func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) },
  4616  	cutoff:     2147483648,
  4617  }
  4618  
  4619  var f64_u32 = f2uCvtTab{
  4620  	ltf:        ssa.OpLess64F,
  4621  	cvt2U:      ssa.OpCvt64Fto32,
  4622  	subf:       ssa.OpSub64F,
  4623  	or:         ssa.OpOr32,
  4624  	floatValue: (*state).constFloat64,
  4625  	intValue:   func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) },
  4626  	cutoff:     2147483648,
  4627  }
  4628  
  4629  func (s *state) float32ToUint64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4630  	return s.floatToUint(&f32_u64, n, x, ft, tt)
  4631  }
  4632  func (s *state) float64ToUint64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4633  	return s.floatToUint(&f64_u64, n, x, ft, tt)
  4634  }
  4635  
  4636  func (s *state) float32ToUint32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4637  	return s.floatToUint(&f32_u32, n, x, ft, tt)
  4638  }
  4639  
  4640  func (s *state) float64ToUint32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4641  	return s.floatToUint(&f64_u32, n, x, ft, tt)
  4642  }
  4643  
  4644  func (s *state) floatToUint(cvttab *f2uCvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  4645  	// cutoff:=1<<(intY_Size-1)
  4646  	// if x < floatX(cutoff) {
  4647  	// 	result = uintY(x)
  4648  	// } else {
  4649  	// 	y = x - floatX(cutoff)
  4650  	// 	z = uintY(y)
  4651  	// 	result = z | -(cutoff)
  4652  	// }
  4653  	cutoff := cvttab.floatValue(s, ft, float64(cvttab.cutoff))
  4654  	cmp := s.newValue2(cvttab.ltf, types.Types[TBOOL], x, cutoff)
  4655  	b := s.endBlock()
  4656  	b.Kind = ssa.BlockIf
  4657  	b.SetControl(cmp)
  4658  	b.Likely = ssa.BranchLikely
  4659  
  4660  	bThen := s.f.NewBlock(ssa.BlockPlain)
  4661  	bElse := s.f.NewBlock(ssa.BlockPlain)
  4662  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  4663  
  4664  	b.AddEdgeTo(bThen)
  4665  	s.startBlock(bThen)
  4666  	a0 := s.newValue1(cvttab.cvt2U, tt, x)
  4667  	s.vars[n] = a0
  4668  	s.endBlock()
  4669  	bThen.AddEdgeTo(bAfter)
  4670  
  4671  	b.AddEdgeTo(bElse)
  4672  	s.startBlock(bElse)
  4673  	y := s.newValue2(cvttab.subf, ft, x, cutoff)
  4674  	y = s.newValue1(cvttab.cvt2U, tt, y)
  4675  	z := cvttab.intValue(s, tt, int64(-cvttab.cutoff))
  4676  	a1 := s.newValue2(cvttab.or, tt, y, z)
  4677  	s.vars[n] = a1
  4678  	s.endBlock()
  4679  	bElse.AddEdgeTo(bAfter)
  4680  
  4681  	s.startBlock(bAfter)
  4682  	return s.variable(n, n.Type)
  4683  }
  4684  
  4685  // dottype generates SSA for a type assertion node.
  4686  // commaok indicates whether to panic or return a bool.
  4687  // If commaok is false, resok will be nil.
  4688  func (s *state) dottype(n *Node, commaok bool) (res, resok *ssa.Value) {
  4689  	iface := s.expr(n.Left)   // input interface
  4690  	target := s.expr(n.Right) // target type
  4691  	byteptr := s.f.Config.Types.BytePtr
  4692  
  4693  	if n.Type.IsInterface() {
  4694  		if n.Type.IsEmptyInterface() {
  4695  			// Converting to an empty interface.
  4696  			// Input could be an empty or nonempty interface.
  4697  			if Debug_typeassert > 0 {
  4698  				Warnl(n.Pos, "type assertion inlined")
  4699  			}
  4700  
  4701  			// Get itab/type field from input.
  4702  			itab := s.newValue1(ssa.OpITab, byteptr, iface)
  4703  			// Conversion succeeds iff that field is not nil.
  4704  			cond := s.newValue2(ssa.OpNeqPtr, types.Types[TBOOL], itab, s.constNil(byteptr))
  4705  
  4706  			if n.Left.Type.IsEmptyInterface() && commaok {
  4707  				// Converting empty interface to empty interface with ,ok is just a nil check.
  4708  				return iface, cond
  4709  			}
  4710  
  4711  			// Branch on nilness.
  4712  			b := s.endBlock()
  4713  			b.Kind = ssa.BlockIf
  4714  			b.SetControl(cond)
  4715  			b.Likely = ssa.BranchLikely
  4716  			bOk := s.f.NewBlock(ssa.BlockPlain)
  4717  			bFail := s.f.NewBlock(ssa.BlockPlain)
  4718  			b.AddEdgeTo(bOk)
  4719  			b.AddEdgeTo(bFail)
  4720  
  4721  			if !commaok {
  4722  				// On failure, panic by calling panicnildottype.
  4723  				s.startBlock(bFail)
  4724  				s.rtcall(panicnildottype, false, nil, target)
  4725  
  4726  				// On success, return (perhaps modified) input interface.
  4727  				s.startBlock(bOk)
  4728  				if n.Left.Type.IsEmptyInterface() {
  4729  					res = iface // Use input interface unchanged.
  4730  					return
  4731  				}
  4732  				// Load type out of itab, build interface with existing idata.
  4733  				off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), itab)
  4734  				typ := s.load(byteptr, off)
  4735  				idata := s.newValue1(ssa.OpIData, n.Type, iface)
  4736  				res = s.newValue2(ssa.OpIMake, n.Type, typ, idata)
  4737  				return
  4738  			}
  4739  
  4740  			s.startBlock(bOk)
  4741  			// nonempty -> empty
  4742  			// Need to load type from itab
  4743  			off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), itab)
  4744  			s.vars[&typVar] = s.load(byteptr, off)
  4745  			s.endBlock()
  4746  
  4747  			// itab is nil, might as well use that as the nil result.
  4748  			s.startBlock(bFail)
  4749  			s.vars[&typVar] = itab
  4750  			s.endBlock()
  4751  
  4752  			// Merge point.
  4753  			bEnd := s.f.NewBlock(ssa.BlockPlain)
  4754  			bOk.AddEdgeTo(bEnd)
  4755  			bFail.AddEdgeTo(bEnd)
  4756  			s.startBlock(bEnd)
  4757  			idata := s.newValue1(ssa.OpIData, n.Type, iface)
  4758  			res = s.newValue2(ssa.OpIMake, n.Type, s.variable(&typVar, byteptr), idata)
  4759  			resok = cond
  4760  			delete(s.vars, &typVar)
  4761  			return
  4762  		}
  4763  		// converting to a nonempty interface needs a runtime call.
  4764  		if Debug_typeassert > 0 {
  4765  			Warnl(n.Pos, "type assertion not inlined")
  4766  		}
  4767  		if n.Left.Type.IsEmptyInterface() {
  4768  			if commaok {
  4769  				call := s.rtcall(assertE2I2, true, []*types.Type{n.Type, types.Types[TBOOL]}, target, iface)
  4770  				return call[0], call[1]
  4771  			}
  4772  			return s.rtcall(assertE2I, true, []*types.Type{n.Type}, target, iface)[0], nil
  4773  		}
  4774  		if commaok {
  4775  			call := s.rtcall(assertI2I2, true, []*types.Type{n.Type, types.Types[TBOOL]}, target, iface)
  4776  			return call[0], call[1]
  4777  		}
  4778  		return s.rtcall(assertI2I, true, []*types.Type{n.Type}, target, iface)[0], nil
  4779  	}
  4780  
  4781  	if Debug_typeassert > 0 {
  4782  		Warnl(n.Pos, "type assertion inlined")
  4783  	}
  4784  
  4785  	// Converting to a concrete type.
  4786  	direct := isdirectiface(n.Type)
  4787  	itab := s.newValue1(ssa.OpITab, byteptr, iface) // type word of interface
  4788  	if Debug_typeassert > 0 {
  4789  		Warnl(n.Pos, "type assertion inlined")
  4790  	}
  4791  	var targetITab *ssa.Value
  4792  	if n.Left.Type.IsEmptyInterface() {
  4793  		// Looking for pointer to target type.
  4794  		targetITab = target
  4795  	} else {
  4796  		// Looking for pointer to itab for target type and source interface.
  4797  		targetITab = s.expr(n.List.First())
  4798  	}
  4799  
  4800  	var tmp *Node       // temporary for use with large types
  4801  	var addr *ssa.Value // address of tmp
  4802  	if commaok && !canSSAType(n.Type) {
  4803  		// unSSAable type, use temporary.
  4804  		// TODO: get rid of some of these temporaries.
  4805  		tmp = tempAt(n.Pos, s.curfn, n.Type)
  4806  		s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, tmp, s.mem())
  4807  		addr = s.addr(tmp, false)
  4808  	}
  4809  
  4810  	cond := s.newValue2(ssa.OpEqPtr, types.Types[TBOOL], itab, targetITab)
  4811  	b := s.endBlock()
  4812  	b.Kind = ssa.BlockIf
  4813  	b.SetControl(cond)
  4814  	b.Likely = ssa.BranchLikely
  4815  
  4816  	bOk := s.f.NewBlock(ssa.BlockPlain)
  4817  	bFail := s.f.NewBlock(ssa.BlockPlain)
  4818  	b.AddEdgeTo(bOk)
  4819  	b.AddEdgeTo(bFail)
  4820  
  4821  	if !commaok {
  4822  		// on failure, panic by calling panicdottype
  4823  		s.startBlock(bFail)
  4824  		taddr := s.expr(n.Right.Right)
  4825  		if n.Left.Type.IsEmptyInterface() {
  4826  			s.rtcall(panicdottypeE, false, nil, itab, target, taddr)
  4827  		} else {
  4828  			s.rtcall(panicdottypeI, false, nil, itab, target, taddr)
  4829  		}
  4830  
  4831  		// on success, return data from interface
  4832  		s.startBlock(bOk)
  4833  		if direct {
  4834  			return s.newValue1(ssa.OpIData, n.Type, iface), nil
  4835  		}
  4836  		p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface)
  4837  		return s.load(n.Type, p), nil
  4838  	}
  4839  
  4840  	// commaok is the more complicated case because we have
  4841  	// a control flow merge point.
  4842  	bEnd := s.f.NewBlock(ssa.BlockPlain)
  4843  	// Note that we need a new valVar each time (unlike okVar where we can
  4844  	// reuse the variable) because it might have a different type every time.
  4845  	valVar := &Node{Op: ONAME, Sym: &types.Sym{Name: "val"}}
  4846  
  4847  	// type assertion succeeded
  4848  	s.startBlock(bOk)
  4849  	if tmp == nil {
  4850  		if direct {
  4851  			s.vars[valVar] = s.newValue1(ssa.OpIData, n.Type, iface)
  4852  		} else {
  4853  			p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface)
  4854  			s.vars[valVar] = s.load(n.Type, p)
  4855  		}
  4856  	} else {
  4857  		p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface)
  4858  		s.move(n.Type, addr, p)
  4859  	}
  4860  	s.vars[&okVar] = s.constBool(true)
  4861  	s.endBlock()
  4862  	bOk.AddEdgeTo(bEnd)
  4863  
  4864  	// type assertion failed
  4865  	s.startBlock(bFail)
  4866  	if tmp == nil {
  4867  		s.vars[valVar] = s.zeroVal(n.Type)
  4868  	} else {
  4869  		s.zero(n.Type, addr)
  4870  	}
  4871  	s.vars[&okVar] = s.constBool(false)
  4872  	s.endBlock()
  4873  	bFail.AddEdgeTo(bEnd)
  4874  
  4875  	// merge point
  4876  	s.startBlock(bEnd)
  4877  	if tmp == nil {
  4878  		res = s.variable(valVar, n.Type)
  4879  		delete(s.vars, valVar)
  4880  	} else {
  4881  		res = s.load(n.Type, addr)
  4882  		s.vars[&memVar] = s.newValue1A(ssa.OpVarKill, types.TypeMem, tmp, s.mem())
  4883  	}
  4884  	resok = s.variable(&okVar, types.Types[TBOOL])
  4885  	delete(s.vars, &okVar)
  4886  	return res, resok
  4887  }
  4888  
  4889  // variable returns the value of a variable at the current location.
  4890  func (s *state) variable(name *Node, t *types.Type) *ssa.Value {
  4891  	v := s.vars[name]
  4892  	if v != nil {
  4893  		return v
  4894  	}
  4895  	v = s.fwdVars[name]
  4896  	if v != nil {
  4897  		return v
  4898  	}
  4899  
  4900  	if s.curBlock == s.f.Entry {
  4901  		// No variable should be live at entry.
  4902  		s.Fatalf("Value live at entry. It shouldn't be. func %s, node %v, value %v", s.f.Name, name, v)
  4903  	}
  4904  	// Make a FwdRef, which records a value that's live on block input.
  4905  	// We'll find the matching definition as part of insertPhis.
  4906  	v = s.newValue0A(ssa.OpFwdRef, t, name)
  4907  	s.fwdVars[name] = v
  4908  	s.addNamedValue(name, v)
  4909  	return v
  4910  }
  4911  
  4912  func (s *state) mem() *ssa.Value {
  4913  	return s.variable(&memVar, types.TypeMem)
  4914  }
  4915  
  4916  func (s *state) addNamedValue(n *Node, v *ssa.Value) {
  4917  	if n.Class() == Pxxx {
  4918  		// Don't track our dummy nodes (&memVar etc.).
  4919  		return
  4920  	}
  4921  	if n.IsAutoTmp() {
  4922  		// Don't track temporary variables.
  4923  		return
  4924  	}
  4925  	if n.Class() == PPARAMOUT {
  4926  		// Don't track named output values.  This prevents return values
  4927  		// from being assigned too early. See #14591 and #14762. TODO: allow this.
  4928  		return
  4929  	}
  4930  	if n.Class() == PAUTO && n.Xoffset != 0 {
  4931  		s.Fatalf("AUTO var with offset %v %d", n, n.Xoffset)
  4932  	}
  4933  	loc := ssa.LocalSlot{N: n, Type: n.Type, Off: 0}
  4934  	values, ok := s.f.NamedValues[loc]
  4935  	if !ok {
  4936  		s.f.Names = append(s.f.Names, loc)
  4937  	}
  4938  	s.f.NamedValues[loc] = append(values, v)
  4939  }
  4940  
  4941  // Branch is an unresolved branch.
  4942  type Branch struct {
  4943  	P *obj.Prog  // branch instruction
  4944  	B *ssa.Block // target
  4945  }
  4946  
  4947  // SSAGenState contains state needed during Prog generation.
  4948  type SSAGenState struct {
  4949  	pp *Progs
  4950  
  4951  	// Branches remembers all the branch instructions we've seen
  4952  	// and where they would like to go.
  4953  	Branches []Branch
  4954  
  4955  	// bstart remembers where each block starts (indexed by block ID)
  4956  	bstart []*obj.Prog
  4957  
  4958  	// 387 port: maps from SSE registers (REG_X?) to 387 registers (REG_F?)
  4959  	SSEto387 map[int16]int16
  4960  	// Some architectures require a 64-bit temporary for FP-related register shuffling. Examples include x86-387, PPC, and Sparc V8.
  4961  	ScratchFpMem *Node
  4962  
  4963  	maxarg int64 // largest frame size for arguments to calls made by the function
  4964  
  4965  	// Map from GC safe points to liveness index, generated by
  4966  	// liveness analysis.
  4967  	livenessMap LivenessMap
  4968  
  4969  	// lineRunStart records the beginning of the current run of instructions
  4970  	// within a single block sharing the same line number
  4971  	// Used to move statement marks to the beginning of such runs.
  4972  	lineRunStart *obj.Prog
  4973  
  4974  	// wasm: The number of values on the WebAssembly stack. This is only used as a safeguard.
  4975  	OnWasmStackSkipped int
  4976  }
  4977  
  4978  // Prog appends a new Prog.
  4979  func (s *SSAGenState) Prog(as obj.As) *obj.Prog {
  4980  	p := s.pp.Prog(as)
  4981  	if ssa.LosesStmtMark(as) {
  4982  		return p
  4983  	}
  4984  	// Float a statement start to the beginning of any same-line run.
  4985  	// lineRunStart is reset at block boundaries, which appears to work well.
  4986  	if s.lineRunStart == nil || s.lineRunStart.Pos.Line() != p.Pos.Line() {
  4987  		s.lineRunStart = p
  4988  	} else if p.Pos.IsStmt() == src.PosIsStmt {
  4989  		s.lineRunStart.Pos = s.lineRunStart.Pos.WithIsStmt()
  4990  		p.Pos = p.Pos.WithNotStmt()
  4991  	}
  4992  	return p
  4993  }
  4994  
  4995  // Pc returns the current Prog.
  4996  func (s *SSAGenState) Pc() *obj.Prog {
  4997  	return s.pp.next
  4998  }
  4999  
  5000  // SetPos sets the current source position.
  5001  func (s *SSAGenState) SetPos(pos src.XPos) {
  5002  	s.pp.pos = pos
  5003  }
  5004  
  5005  // Br emits a single branch instruction and returns the instruction.
  5006  // Not all architectures need the returned instruction, but otherwise
  5007  // the boilerplate is common to all.
  5008  func (s *SSAGenState) Br(op obj.As, target *ssa.Block) *obj.Prog {
  5009  	p := s.Prog(op)
  5010  	p.To.Type = obj.TYPE_BRANCH
  5011  	s.Branches = append(s.Branches, Branch{P: p, B: target})
  5012  	return p
  5013  }
  5014  
  5015  // DebugFriendlySetPos adjusts Pos.IsStmt subject to heuristics
  5016  // that reduce "jumpy" line number churn when debugging.
  5017  // Spill/fill/copy instructions from the register allocator,
  5018  // phi functions, and instructions with a no-pos position
  5019  // are examples of instructions that can cause churn.
  5020  func (s *SSAGenState) DebugFriendlySetPosFrom(v *ssa.Value) {
  5021  	switch v.Op {
  5022  	case ssa.OpPhi, ssa.OpCopy, ssa.OpLoadReg, ssa.OpStoreReg:
  5023  		// These are not statements
  5024  		s.SetPos(v.Pos.WithNotStmt())
  5025  	default:
  5026  		p := v.Pos
  5027  		if p != src.NoXPos {
  5028  			// If the position is defined, update the position.
  5029  			// Also convert default IsStmt to NotStmt; only
  5030  			// explicit statement boundaries should appear
  5031  			// in the generated code.
  5032  			if p.IsStmt() != src.PosIsStmt {
  5033  				p = p.WithNotStmt()
  5034  			}
  5035  			s.SetPos(p)
  5036  		}
  5037  	}
  5038  }
  5039  
  5040  // byXoffset implements sort.Interface for []*Node using Xoffset as the ordering.
  5041  type byXoffset []*Node
  5042  
  5043  func (s byXoffset) Len() int           { return len(s) }
  5044  func (s byXoffset) Less(i, j int) bool { return s[i].Xoffset < s[j].Xoffset }
  5045  func (s byXoffset) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
  5046  
  5047  func emitStackObjects(e *ssafn, pp *Progs) {
  5048  	var vars []*Node
  5049  	for _, n := range e.curfn.Func.Dcl {
  5050  		if livenessShouldTrack(n) && n.Addrtaken() {
  5051  			vars = append(vars, n)
  5052  		}
  5053  	}
  5054  	if len(vars) == 0 {
  5055  		return
  5056  	}
  5057  
  5058  	// Sort variables from lowest to highest address.
  5059  	sort.Sort(byXoffset(vars))
  5060  
  5061  	// Populate the stack object data.
  5062  	// Format must match runtime/stack.go:stackObjectRecord.
  5063  	x := e.curfn.Func.lsym.Func.StackObjects
  5064  	off := 0
  5065  	off = duintptr(x, off, uint64(len(vars)))
  5066  	for _, v := range vars {
  5067  		// Note: arguments and return values have non-negative Xoffset,
  5068  		// in which case the offset is relative to argp.
  5069  		// Locals have a negative Xoffset, in which case the offset is relative to varp.
  5070  		off = duintptr(x, off, uint64(v.Xoffset))
  5071  		if !typesym(v.Type).Siggen() {
  5072  			Fatalf("stack object's type symbol not generated for type %s", v.Type)
  5073  		}
  5074  		off = dsymptr(x, off, dtypesym(v.Type), 0)
  5075  	}
  5076  
  5077  	// Emit a funcdata pointing at the stack object data.
  5078  	p := pp.Prog(obj.AFUNCDATA)
  5079  	Addrconst(&p.From, objabi.FUNCDATA_StackObjects)
  5080  	p.To.Type = obj.TYPE_MEM
  5081  	p.To.Name = obj.NAME_EXTERN
  5082  	p.To.Sym = x
  5083  
  5084  	if debuglive != 0 {
  5085  		for _, v := range vars {
  5086  			Warnl(v.Pos, "stack object %v %s", v, v.Type.String())
  5087  		}
  5088  	}
  5089  }
  5090  
  5091  // genssa appends entries to pp for each instruction in f.
  5092  func genssa(f *ssa.Func, pp *Progs) {
  5093  	var s SSAGenState
  5094  
  5095  	e := f.Frontend().(*ssafn)
  5096  
  5097  	s.livenessMap = liveness(e, f, pp)
  5098  	emitStackObjects(e, pp)
  5099  
  5100  	// Remember where each block starts.
  5101  	s.bstart = make([]*obj.Prog, f.NumBlocks())
  5102  	s.pp = pp
  5103  	var progToValue map[*obj.Prog]*ssa.Value
  5104  	var progToBlock map[*obj.Prog]*ssa.Block
  5105  	var valueToProgAfter []*obj.Prog // The first Prog following computation of a value v; v is visible at this point.
  5106  	if f.PrintOrHtmlSSA {
  5107  		progToValue = make(map[*obj.Prog]*ssa.Value, f.NumValues())
  5108  		progToBlock = make(map[*obj.Prog]*ssa.Block, f.NumBlocks())
  5109  		f.Logf("genssa %s\n", f.Name)
  5110  		progToBlock[s.pp.next] = f.Blocks[0]
  5111  	}
  5112  
  5113  	if thearch.Use387 {
  5114  		s.SSEto387 = map[int16]int16{}
  5115  	}
  5116  
  5117  	s.ScratchFpMem = e.scratchFpMem
  5118  
  5119  	if Ctxt.Flag_locationlists {
  5120  		if cap(f.Cache.ValueToProgAfter) < f.NumValues() {
  5121  			f.Cache.ValueToProgAfter = make([]*obj.Prog, f.NumValues())
  5122  		}
  5123  		valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()]
  5124  		for i := range valueToProgAfter {
  5125  			valueToProgAfter[i] = nil
  5126  		}
  5127  	}
  5128  
  5129  	// If the very first instruction is not tagged as a statement,
  5130  	// debuggers may attribute it to previous function in program.
  5131  	firstPos := src.NoXPos
  5132  	for _, v := range f.Entry.Values {
  5133  		if v.Pos.IsStmt() == src.PosIsStmt {
  5134  			firstPos = v.Pos
  5135  			v.Pos = firstPos.WithDefaultStmt()
  5136  			break
  5137  		}
  5138  	}
  5139  
  5140  	// Emit basic blocks
  5141  	for i, b := range f.Blocks {
  5142  		s.bstart[b.ID] = s.pp.next
  5143  		s.pp.nextLive = LivenessInvalid
  5144  		s.lineRunStart = nil
  5145  
  5146  		// Emit values in block
  5147  		thearch.SSAMarkMoves(&s, b)
  5148  		for _, v := range b.Values {
  5149  			x := s.pp.next
  5150  			s.DebugFriendlySetPosFrom(v)
  5151  			// Attach this safe point to the next
  5152  			// instruction.
  5153  			s.pp.nextLive = s.livenessMap.Get(v)
  5154  			switch v.Op {
  5155  			case ssa.OpInitMem:
  5156  				// memory arg needs no code
  5157  			case ssa.OpArg:
  5158  				// input args need no code
  5159  			case ssa.OpSP, ssa.OpSB:
  5160  				// nothing to do
  5161  			case ssa.OpSelect0, ssa.OpSelect1:
  5162  				// nothing to do
  5163  			case ssa.OpGetG:
  5164  				// nothing to do when there's a g register,
  5165  				// and checkLower complains if there's not
  5166  			case ssa.OpVarDef, ssa.OpVarLive, ssa.OpKeepAlive, ssa.OpVarKill:
  5167  				// nothing to do; already used by liveness
  5168  			case ssa.OpPhi:
  5169  				CheckLoweredPhi(v)
  5170  			case ssa.OpConvert:
  5171  				// nothing to do; no-op conversion for liveness
  5172  				if v.Args[0].Reg() != v.Reg() {
  5173  					v.Fatalf("OpConvert should be a no-op: %s; %s", v.Args[0].LongString(), v.LongString())
  5174  				}
  5175  			case ssa.OpInlMark:
  5176  				p := thearch.Ginsnop(s.pp)
  5177  				if pp.curfn.Func.lsym != nil {
  5178  					// lsym is nil if the function name is "_".
  5179  					pp.curfn.Func.lsym.Func.AddInlMark(p, v.AuxInt32())
  5180  				}
  5181  				// TODO: if matching line number, merge somehow with previous instruction?
  5182  
  5183  			default:
  5184  				// let the backend handle it
  5185  				// Special case for first line in function; move it to the start.
  5186  				if firstPos != src.NoXPos {
  5187  					s.SetPos(firstPos)
  5188  					firstPos = src.NoXPos
  5189  				}
  5190  				thearch.SSAGenValue(&s, v)
  5191  			}
  5192  
  5193  			if Ctxt.Flag_locationlists {
  5194  				valueToProgAfter[v.ID] = s.pp.next
  5195  			}
  5196  
  5197  			if f.PrintOrHtmlSSA {
  5198  				for ; x != s.pp.next; x = x.Link {
  5199  					progToValue[x] = v
  5200  				}
  5201  			}
  5202  		}
  5203  		// Emit control flow instructions for block
  5204  		var next *ssa.Block
  5205  		if i < len(f.Blocks)-1 && Debug['N'] == 0 {
  5206  			// If -N, leave next==nil so every block with successors
  5207  			// ends in a JMP (except call blocks - plive doesn't like
  5208  			// select{send,recv} followed by a JMP call).  Helps keep
  5209  			// line numbers for otherwise empty blocks.
  5210  			next = f.Blocks[i+1]
  5211  		}
  5212  		x := s.pp.next
  5213  		s.SetPos(b.Pos)
  5214  		thearch.SSAGenBlock(&s, b, next)
  5215  		if f.PrintOrHtmlSSA {
  5216  			for ; x != s.pp.next; x = x.Link {
  5217  				progToBlock[x] = b
  5218  			}
  5219  		}
  5220  	}
  5221  
  5222  	if Ctxt.Flag_locationlists {
  5223  		e.curfn.Func.DebugInfo = ssa.BuildFuncDebug(Ctxt, f, Debug_locationlist > 1, stackOffset)
  5224  		bstart := s.bstart
  5225  		// Note that at this moment, Prog.Pc is a sequence number; it's
  5226  		// not a real PC until after assembly, so this mapping has to
  5227  		// be done later.
  5228  		e.curfn.Func.DebugInfo.GetPC = func(b, v ssa.ID) int64 {
  5229  			switch v {
  5230  			case ssa.BlockStart.ID:
  5231  				if b == f.Entry.ID {
  5232  					return 0 // Start at the very beginning, at the assembler-generated prologue.
  5233  					// this should only happen for function args (ssa.OpArg)
  5234  				}
  5235  				return bstart[b].Pc
  5236  			case ssa.BlockEnd.ID:
  5237  				return e.curfn.Func.lsym.Size
  5238  			default:
  5239  				return valueToProgAfter[v].Pc
  5240  			}
  5241  		}
  5242  	}
  5243  
  5244  	// Resolve branches, and relax DefaultStmt into NotStmt
  5245  	for _, br := range s.Branches {
  5246  		br.P.To.Val = s.bstart[br.B.ID]
  5247  		if br.P.Pos.IsStmt() != src.PosIsStmt {
  5248  			br.P.Pos = br.P.Pos.WithNotStmt()
  5249  		}
  5250  	}
  5251  
  5252  	if e.log { // spew to stdout
  5253  		filename := ""
  5254  		for p := pp.Text; p != nil; p = p.Link {
  5255  			if p.Pos.IsKnown() && p.InnermostFilename() != filename {
  5256  				filename = p.InnermostFilename()
  5257  				f.Logf("# %s\n", filename)
  5258  			}
  5259  
  5260  			var s string
  5261  			if v, ok := progToValue[p]; ok {
  5262  				s = v.String()
  5263  			} else if b, ok := progToBlock[p]; ok {
  5264  				s = b.String()
  5265  			} else {
  5266  				s = "   " // most value and branch strings are 2-3 characters long
  5267  			}
  5268  			f.Logf(" %-6s\t%.5d (%s)\t%s\n", s, p.Pc, p.InnermostLineNumber(), p.InstructionString())
  5269  		}
  5270  	}
  5271  	if f.HTMLWriter != nil { // spew to ssa.html
  5272  		var buf bytes.Buffer
  5273  		buf.WriteString("<code>")
  5274  		buf.WriteString("<dl class=\"ssa-gen\">")
  5275  		filename := ""
  5276  		for p := pp.Text; p != nil; p = p.Link {
  5277  			// Don't spam every line with the file name, which is often huge.
  5278  			// Only print changes, and "unknown" is not a change.
  5279  			if p.Pos.IsKnown() && p.InnermostFilename() != filename {
  5280  				filename = p.InnermostFilename()
  5281  				buf.WriteString("<dt class=\"ssa-prog-src\"></dt><dd class=\"ssa-prog\">")
  5282  				buf.WriteString(html.EscapeString("# " + filename))
  5283  				buf.WriteString("</dd>")
  5284  			}
  5285  
  5286  			buf.WriteString("<dt class=\"ssa-prog-src\">")
  5287  			if v, ok := progToValue[p]; ok {
  5288  				buf.WriteString(v.HTML())
  5289  			} else if b, ok := progToBlock[p]; ok {
  5290  				buf.WriteString("<b>" + b.HTML() + "</b>")
  5291  			}
  5292  			buf.WriteString("</dt>")
  5293  			buf.WriteString("<dd class=\"ssa-prog\">")
  5294  			buf.WriteString(fmt.Sprintf("%.5d <span class=\"l%v line-number\">(%s)</span> %s", p.Pc, p.InnermostLineNumber(), p.InnermostLineNumberHTML(), html.EscapeString(p.InstructionString())))
  5295  			buf.WriteString("</dd>")
  5296  		}
  5297  		buf.WriteString("</dl>")
  5298  		buf.WriteString("</code>")
  5299  		f.HTMLWriter.WriteColumn("genssa", "genssa", "ssa-prog", buf.String())
  5300  	}
  5301  
  5302  	defframe(&s, e)
  5303  
  5304  	f.HTMLWriter.Close()
  5305  	f.HTMLWriter = nil
  5306  }
  5307  
  5308  func defframe(s *SSAGenState, e *ssafn) {
  5309  	pp := s.pp
  5310  
  5311  	frame := Rnd(s.maxarg+e.stksize, int64(Widthreg))
  5312  	if thearch.PadFrame != nil {
  5313  		frame = thearch.PadFrame(frame)
  5314  	}
  5315  
  5316  	// Fill in argument and frame size.
  5317  	pp.Text.To.Type = obj.TYPE_TEXTSIZE
  5318  	pp.Text.To.Val = int32(Rnd(e.curfn.Type.ArgWidth(), int64(Widthreg)))
  5319  	pp.Text.To.Offset = frame
  5320  
  5321  	// Insert code to zero ambiguously live variables so that the
  5322  	// garbage collector only sees initialized values when it
  5323  	// looks for pointers.
  5324  	p := pp.Text
  5325  	var lo, hi int64
  5326  
  5327  	// Opaque state for backend to use. Current backends use it to
  5328  	// keep track of which helper registers have been zeroed.
  5329  	var state uint32
  5330  
  5331  	// Iterate through declarations. They are sorted in decreasing Xoffset order.
  5332  	for _, n := range e.curfn.Func.Dcl {
  5333  		if !n.Name.Needzero() {
  5334  			continue
  5335  		}
  5336  		if n.Class() != PAUTO {
  5337  			Fatalf("needzero class %d", n.Class())
  5338  		}
  5339  		if n.Type.Size()%int64(Widthptr) != 0 || n.Xoffset%int64(Widthptr) != 0 || n.Type.Size() == 0 {
  5340  			Fatalf("var %L has size %d offset %d", n, n.Type.Size(), n.Xoffset)
  5341  		}
  5342  
  5343  		if lo != hi && n.Xoffset+n.Type.Size() >= lo-int64(2*Widthreg) {
  5344  			// Merge with range we already have.
  5345  			lo = n.Xoffset
  5346  			continue
  5347  		}
  5348  
  5349  		// Zero old range
  5350  		p = thearch.ZeroRange(pp, p, frame+lo, hi-lo, &state)
  5351  
  5352  		// Set new range.
  5353  		lo = n.Xoffset
  5354  		hi = lo + n.Type.Size()
  5355  	}
  5356  
  5357  	// Zero final range.
  5358  	thearch.ZeroRange(pp, p, frame+lo, hi-lo, &state)
  5359  }
  5360  
  5361  type FloatingEQNEJump struct {
  5362  	Jump  obj.As
  5363  	Index int
  5364  }
  5365  
  5366  func (s *SSAGenState) oneFPJump(b *ssa.Block, jumps *FloatingEQNEJump) {
  5367  	p := s.Prog(jumps.Jump)
  5368  	p.To.Type = obj.TYPE_BRANCH
  5369  	p.Pos = b.Pos
  5370  	to := jumps.Index
  5371  	s.Branches = append(s.Branches, Branch{p, b.Succs[to].Block()})
  5372  }
  5373  
  5374  func (s *SSAGenState) FPJump(b, next *ssa.Block, jumps *[2][2]FloatingEQNEJump) {
  5375  	switch next {
  5376  	case b.Succs[0].Block():
  5377  		s.oneFPJump(b, &jumps[0][0])
  5378  		s.oneFPJump(b, &jumps[0][1])
  5379  	case b.Succs[1].Block():
  5380  		s.oneFPJump(b, &jumps[1][0])
  5381  		s.oneFPJump(b, &jumps[1][1])
  5382  	default:
  5383  		s.oneFPJump(b, &jumps[1][0])
  5384  		s.oneFPJump(b, &jumps[1][1])
  5385  		q := s.Prog(obj.AJMP)
  5386  		q.Pos = b.Pos
  5387  		q.To.Type = obj.TYPE_BRANCH
  5388  		s.Branches = append(s.Branches, Branch{q, b.Succs[1].Block()})
  5389  	}
  5390  }
  5391  
  5392  func AuxOffset(v *ssa.Value) (offset int64) {
  5393  	if v.Aux == nil {
  5394  		return 0
  5395  	}
  5396  	n, ok := v.Aux.(*Node)
  5397  	if !ok {
  5398  		v.Fatalf("bad aux type in %s\n", v.LongString())
  5399  	}
  5400  	if n.Class() == PAUTO {
  5401  		return n.Xoffset
  5402  	}
  5403  	return 0
  5404  }
  5405  
  5406  // AddAux adds the offset in the aux fields (AuxInt and Aux) of v to a.
  5407  func AddAux(a *obj.Addr, v *ssa.Value) {
  5408  	AddAux2(a, v, v.AuxInt)
  5409  }
  5410  func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) {
  5411  	if a.Type != obj.TYPE_MEM && a.Type != obj.TYPE_ADDR {
  5412  		v.Fatalf("bad AddAux addr %v", a)
  5413  	}
  5414  	// add integer offset
  5415  	a.Offset += offset
  5416  
  5417  	// If no additional symbol offset, we're done.
  5418  	if v.Aux == nil {
  5419  		return
  5420  	}
  5421  	// Add symbol's offset from its base register.
  5422  	switch n := v.Aux.(type) {
  5423  	case *obj.LSym:
  5424  		a.Name = obj.NAME_EXTERN
  5425  		a.Sym = n
  5426  	case *Node:
  5427  		if n.Class() == PPARAM || n.Class() == PPARAMOUT {
  5428  			a.Name = obj.NAME_PARAM
  5429  			a.Sym = n.Orig.Sym.Linksym()
  5430  			a.Offset += n.Xoffset
  5431  			break
  5432  		}
  5433  		a.Name = obj.NAME_AUTO
  5434  		a.Sym = n.Sym.Linksym()
  5435  		a.Offset += n.Xoffset
  5436  	default:
  5437  		v.Fatalf("aux in %s not implemented %#v", v, v.Aux)
  5438  	}
  5439  }
  5440  
  5441  // extendIndex extends v to a full int width.
  5442  // panic using the given function if v does not fit in an int (only on 32-bit archs).
  5443  func (s *state) extendIndex(v *ssa.Value, panicfn *obj.LSym) *ssa.Value {
  5444  	size := v.Type.Size()
  5445  	if size == s.config.PtrSize {
  5446  		return v
  5447  	}
  5448  	if size > s.config.PtrSize {
  5449  		// truncate 64-bit indexes on 32-bit pointer archs. Test the
  5450  		// high word and branch to out-of-bounds failure if it is not 0.
  5451  		if Debug['B'] == 0 {
  5452  			hi := s.newValue1(ssa.OpInt64Hi, types.Types[TUINT32], v)
  5453  			cmp := s.newValue2(ssa.OpEq32, types.Types[TBOOL], hi, s.constInt32(types.Types[TUINT32], 0))
  5454  			s.check(cmp, panicfn)
  5455  		}
  5456  		return s.newValue1(ssa.OpTrunc64to32, types.Types[TINT], v)
  5457  	}
  5458  
  5459  	// Extend value to the required size
  5460  	var op ssa.Op
  5461  	if v.Type.IsSigned() {
  5462  		switch 10*size + s.config.PtrSize {
  5463  		case 14:
  5464  			op = ssa.OpSignExt8to32
  5465  		case 18:
  5466  			op = ssa.OpSignExt8to64
  5467  		case 24:
  5468  			op = ssa.OpSignExt16to32
  5469  		case 28:
  5470  			op = ssa.OpSignExt16to64
  5471  		case 48:
  5472  			op = ssa.OpSignExt32to64
  5473  		default:
  5474  			s.Fatalf("bad signed index extension %s", v.Type)
  5475  		}
  5476  	} else {
  5477  		switch 10*size + s.config.PtrSize {
  5478  		case 14:
  5479  			op = ssa.OpZeroExt8to32
  5480  		case 18:
  5481  			op = ssa.OpZeroExt8to64
  5482  		case 24:
  5483  			op = ssa.OpZeroExt16to32
  5484  		case 28:
  5485  			op = ssa.OpZeroExt16to64
  5486  		case 48:
  5487  			op = ssa.OpZeroExt32to64
  5488  		default:
  5489  			s.Fatalf("bad unsigned index extension %s", v.Type)
  5490  		}
  5491  	}
  5492  	return s.newValue1(op, types.Types[TINT], v)
  5493  }
  5494  
  5495  // CheckLoweredPhi checks that regalloc and stackalloc correctly handled phi values.
  5496  // Called during ssaGenValue.
  5497  func CheckLoweredPhi(v *ssa.Value) {
  5498  	if v.Op != ssa.OpPhi {
  5499  		v.Fatalf("CheckLoweredPhi called with non-phi value: %v", v.LongString())
  5500  	}
  5501  	if v.Type.IsMemory() {
  5502  		return
  5503  	}
  5504  	f := v.Block.Func
  5505  	loc := f.RegAlloc[v.ID]
  5506  	for _, a := range v.Args {
  5507  		if aloc := f.RegAlloc[a.ID]; aloc != loc { // TODO: .Equal() instead?
  5508  			v.Fatalf("phi arg at different location than phi: %v @ %s, but arg %v @ %s\n%s\n", v, loc, a, aloc, v.Block.Func)
  5509  		}
  5510  	}
  5511  }
  5512  
  5513  // CheckLoweredGetClosurePtr checks that v is the first instruction in the function's entry block.
  5514  // The output of LoweredGetClosurePtr is generally hardwired to the correct register.
  5515  // That register contains the closure pointer on closure entry.
  5516  func CheckLoweredGetClosurePtr(v *ssa.Value) {
  5517  	entry := v.Block.Func.Entry
  5518  	if entry != v.Block || entry.Values[0] != v {
  5519  		Fatalf("in %s, badly placed LoweredGetClosurePtr: %v %v", v.Block.Func.Name, v.Block, v)
  5520  	}
  5521  }
  5522  
  5523  // AutoVar returns a *Node and int64 representing the auto variable and offset within it
  5524  // where v should be spilled.
  5525  func AutoVar(v *ssa.Value) (*Node, int64) {
  5526  	loc := v.Block.Func.RegAlloc[v.ID].(ssa.LocalSlot)
  5527  	if v.Type.Size() > loc.Type.Size() {
  5528  		v.Fatalf("spill/restore type %s doesn't fit in slot type %s", v.Type, loc.Type)
  5529  	}
  5530  	return loc.N.(*Node), loc.Off
  5531  }
  5532  
  5533  func AddrAuto(a *obj.Addr, v *ssa.Value) {
  5534  	n, off := AutoVar(v)
  5535  	a.Type = obj.TYPE_MEM
  5536  	a.Sym = n.Sym.Linksym()
  5537  	a.Reg = int16(thearch.REGSP)
  5538  	a.Offset = n.Xoffset + off
  5539  	if n.Class() == PPARAM || n.Class() == PPARAMOUT {
  5540  		a.Name = obj.NAME_PARAM
  5541  	} else {
  5542  		a.Name = obj.NAME_AUTO
  5543  	}
  5544  }
  5545  
  5546  func (s *SSAGenState) AddrScratch(a *obj.Addr) {
  5547  	if s.ScratchFpMem == nil {
  5548  		panic("no scratch memory available; forgot to declare usesScratch for Op?")
  5549  	}
  5550  	a.Type = obj.TYPE_MEM
  5551  	a.Name = obj.NAME_AUTO
  5552  	a.Sym = s.ScratchFpMem.Sym.Linksym()
  5553  	a.Reg = int16(thearch.REGSP)
  5554  	a.Offset = s.ScratchFpMem.Xoffset
  5555  }
  5556  
  5557  // Call returns a new CALL instruction for the SSA value v.
  5558  // It uses PrepareCall to prepare the call.
  5559  func (s *SSAGenState) Call(v *ssa.Value) *obj.Prog {
  5560  	s.PrepareCall(v)
  5561  
  5562  	p := s.Prog(obj.ACALL)
  5563  	p.Pos = v.Pos
  5564  	if sym, ok := v.Aux.(*obj.LSym); ok {
  5565  		p.To.Type = obj.TYPE_MEM
  5566  		p.To.Name = obj.NAME_EXTERN
  5567  		p.To.Sym = sym
  5568  	} else {
  5569  		// TODO(mdempsky): Can these differences be eliminated?
  5570  		switch thearch.LinkArch.Family {
  5571  		case sys.AMD64, sys.I386, sys.PPC64, sys.S390X, sys.Wasm:
  5572  			p.To.Type = obj.TYPE_REG
  5573  		case sys.ARM, sys.ARM64, sys.MIPS, sys.MIPS64:
  5574  			p.To.Type = obj.TYPE_MEM
  5575  		default:
  5576  			Fatalf("unknown indirect call family")
  5577  		}
  5578  		p.To.Reg = v.Args[0].Reg()
  5579  	}
  5580  	return p
  5581  }
  5582  
  5583  // PrepareCall prepares to emit a CALL instruction for v and does call-related bookkeeping.
  5584  // It must be called immediately before emitting the actual CALL instruction,
  5585  // since it emits PCDATA for the stack map at the call (calls are safe points).
  5586  func (s *SSAGenState) PrepareCall(v *ssa.Value) {
  5587  	idx := s.livenessMap.Get(v)
  5588  	if !idx.Valid() {
  5589  		// typedmemclr and typedmemmove are write barriers and
  5590  		// deeply non-preemptible. They are unsafe points and
  5591  		// hence should not have liveness maps.
  5592  		if sym, _ := v.Aux.(*obj.LSym); !(sym == typedmemclr || sym == typedmemmove) {
  5593  			Fatalf("missing stack map index for %v", v.LongString())
  5594  		}
  5595  	}
  5596  
  5597  	if sym, _ := v.Aux.(*obj.LSym); sym == Deferreturn {
  5598  		// Deferred calls will appear to be returning to
  5599  		// the CALL deferreturn(SB) that we are about to emit.
  5600  		// However, the stack trace code will show the line
  5601  		// of the instruction byte before the return PC.
  5602  		// To avoid that being an unrelated instruction,
  5603  		// insert an actual hardware NOP that will have the right line number.
  5604  		// This is different from obj.ANOP, which is a virtual no-op
  5605  		// that doesn't make it into the instruction stream.
  5606  		thearch.Ginsnopdefer(s.pp)
  5607  	}
  5608  
  5609  	if sym, ok := v.Aux.(*obj.LSym); ok {
  5610  		// Record call graph information for nowritebarrierrec
  5611  		// analysis.
  5612  		if nowritebarrierrecCheck != nil {
  5613  			nowritebarrierrecCheck.recordCall(s.pp.curfn, sym, v.Pos)
  5614  		}
  5615  	}
  5616  
  5617  	if s.maxarg < v.AuxInt {
  5618  		s.maxarg = v.AuxInt
  5619  	}
  5620  }
  5621  
  5622  // fieldIdx finds the index of the field referred to by the ODOT node n.
  5623  func fieldIdx(n *Node) int {
  5624  	t := n.Left.Type
  5625  	f := n.Sym
  5626  	if !t.IsStruct() {
  5627  		panic("ODOT's LHS is not a struct")
  5628  	}
  5629  
  5630  	var i int
  5631  	for _, t1 := range t.Fields().Slice() {
  5632  		if t1.Sym != f {
  5633  			i++
  5634  			continue
  5635  		}
  5636  		if t1.Offset != n.Xoffset {
  5637  			panic("field offset doesn't match")
  5638  		}
  5639  		return i
  5640  	}
  5641  	panic(fmt.Sprintf("can't find field in expr %v\n", n))
  5642  
  5643  	// TODO: keep the result of this function somewhere in the ODOT Node
  5644  	// so we don't have to recompute it each time we need it.
  5645  }
  5646  
  5647  // ssafn holds frontend information about a function that the backend is processing.
  5648  // It also exports a bunch of compiler services for the ssa backend.
  5649  type ssafn struct {
  5650  	curfn        *Node
  5651  	strings      map[string]interface{} // map from constant string to data symbols
  5652  	scratchFpMem *Node                  // temp for floating point register / memory moves on some architectures
  5653  	stksize      int64                  // stack size for current frame
  5654  	stkptrsize   int64                  // prefix of stack containing pointers
  5655  	log          bool                   // print ssa debug to the stdout
  5656  }
  5657  
  5658  // StringData returns a symbol (a *types.Sym wrapped in an interface) which
  5659  // is the data component of a global string constant containing s.
  5660  func (e *ssafn) StringData(s string) interface{} {
  5661  	if aux, ok := e.strings[s]; ok {
  5662  		return aux
  5663  	}
  5664  	if e.strings == nil {
  5665  		e.strings = make(map[string]interface{})
  5666  	}
  5667  	data := stringsym(e.curfn.Pos, s)
  5668  	e.strings[s] = data
  5669  	return data
  5670  }
  5671  
  5672  func (e *ssafn) Auto(pos src.XPos, t *types.Type) ssa.GCNode {
  5673  	n := tempAt(pos, e.curfn, t) // Note: adds new auto to e.curfn.Func.Dcl list
  5674  	return n
  5675  }
  5676  
  5677  func (e *ssafn) SplitString(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
  5678  	n := name.N.(*Node)
  5679  	ptrType := types.NewPtr(types.Types[TUINT8])
  5680  	lenType := types.Types[TINT]
  5681  	if n.Class() == PAUTO && !n.Addrtaken() {
  5682  		// Split this string up into two separate variables.
  5683  		p := e.splitSlot(&name, ".ptr", 0, ptrType)
  5684  		l := e.splitSlot(&name, ".len", ptrType.Size(), lenType)
  5685  		return p, l
  5686  	}
  5687  	// Return the two parts of the larger variable.
  5688  	return ssa.LocalSlot{N: n, Type: ptrType, Off: name.Off}, ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(Widthptr)}
  5689  }
  5690  
  5691  func (e *ssafn) SplitInterface(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
  5692  	n := name.N.(*Node)
  5693  	u := types.Types[TUINTPTR]
  5694  	t := types.NewPtr(types.Types[TUINT8])
  5695  	if n.Class() == PAUTO && !n.Addrtaken() {
  5696  		// Split this interface up into two separate variables.
  5697  		f := ".itab"
  5698  		if n.Type.IsEmptyInterface() {
  5699  			f = ".type"
  5700  		}
  5701  		c := e.splitSlot(&name, f, 0, u) // see comment in plive.go:onebitwalktype1.
  5702  		d := e.splitSlot(&name, ".data", u.Size(), t)
  5703  		return c, d
  5704  	}
  5705  	// Return the two parts of the larger variable.
  5706  	return ssa.LocalSlot{N: n, Type: u, Off: name.Off}, ssa.LocalSlot{N: n, Type: t, Off: name.Off + int64(Widthptr)}
  5707  }
  5708  
  5709  func (e *ssafn) SplitSlice(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot, ssa.LocalSlot) {
  5710  	n := name.N.(*Node)
  5711  	ptrType := types.NewPtr(name.Type.Elem())
  5712  	lenType := types.Types[TINT]
  5713  	if n.Class() == PAUTO && !n.Addrtaken() {
  5714  		// Split this slice up into three separate variables.
  5715  		p := e.splitSlot(&name, ".ptr", 0, ptrType)
  5716  		l := e.splitSlot(&name, ".len", ptrType.Size(), lenType)
  5717  		c := e.splitSlot(&name, ".cap", ptrType.Size()+lenType.Size(), lenType)
  5718  		return p, l, c
  5719  	}
  5720  	// Return the three parts of the larger variable.
  5721  	return ssa.LocalSlot{N: n, Type: ptrType, Off: name.Off},
  5722  		ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(Widthptr)},
  5723  		ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(2*Widthptr)}
  5724  }
  5725  
  5726  func (e *ssafn) SplitComplex(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
  5727  	n := name.N.(*Node)
  5728  	s := name.Type.Size() / 2
  5729  	var t *types.Type
  5730  	if s == 8 {
  5731  		t = types.Types[TFLOAT64]
  5732  	} else {
  5733  		t = types.Types[TFLOAT32]
  5734  	}
  5735  	if n.Class() == PAUTO && !n.Addrtaken() {
  5736  		// Split this complex up into two separate variables.
  5737  		r := e.splitSlot(&name, ".real", 0, t)
  5738  		i := e.splitSlot(&name, ".imag", t.Size(), t)
  5739  		return r, i
  5740  	}
  5741  	// Return the two parts of the larger variable.
  5742  	return ssa.LocalSlot{N: n, Type: t, Off: name.Off}, ssa.LocalSlot{N: n, Type: t, Off: name.Off + s}
  5743  }
  5744  
  5745  func (e *ssafn) SplitInt64(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
  5746  	n := name.N.(*Node)
  5747  	var t *types.Type
  5748  	if name.Type.IsSigned() {
  5749  		t = types.Types[TINT32]
  5750  	} else {
  5751  		t = types.Types[TUINT32]
  5752  	}
  5753  	if n.Class() == PAUTO && !n.Addrtaken() {
  5754  		// Split this int64 up into two separate variables.
  5755  		if thearch.LinkArch.ByteOrder == binary.BigEndian {
  5756  			return e.splitSlot(&name, ".hi", 0, t), e.splitSlot(&name, ".lo", t.Size(), types.Types[TUINT32])
  5757  		}
  5758  		return e.splitSlot(&name, ".hi", t.Size(), t), e.splitSlot(&name, ".lo", 0, types.Types[TUINT32])
  5759  	}
  5760  	// Return the two parts of the larger variable.
  5761  	if thearch.LinkArch.ByteOrder == binary.BigEndian {
  5762  		return ssa.LocalSlot{N: n, Type: t, Off: name.Off}, ssa.LocalSlot{N: n, Type: types.Types[TUINT32], Off: name.Off + 4}
  5763  	}
  5764  	return ssa.LocalSlot{N: n, Type: t, Off: name.Off + 4}, ssa.LocalSlot{N: n, Type: types.Types[TUINT32], Off: name.Off}
  5765  }
  5766  
  5767  func (e *ssafn) SplitStruct(name ssa.LocalSlot, i int) ssa.LocalSlot {
  5768  	n := name.N.(*Node)
  5769  	st := name.Type
  5770  	ft := st.FieldType(i)
  5771  	var offset int64
  5772  	for f := 0; f < i; f++ {
  5773  		offset += st.FieldType(f).Size()
  5774  	}
  5775  	if n.Class() == PAUTO && !n.Addrtaken() {
  5776  		// Note: the _ field may appear several times.  But
  5777  		// have no fear, identically-named but distinct Autos are
  5778  		// ok, albeit maybe confusing for a debugger.
  5779  		return e.splitSlot(&name, "."+st.FieldName(i), offset, ft)
  5780  	}
  5781  	return ssa.LocalSlot{N: n, Type: ft, Off: name.Off + st.FieldOff(i)}
  5782  }
  5783  
  5784  func (e *ssafn) SplitArray(name ssa.LocalSlot) ssa.LocalSlot {
  5785  	n := name.N.(*Node)
  5786  	at := name.Type
  5787  	if at.NumElem() != 1 {
  5788  		Fatalf("bad array size")
  5789  	}
  5790  	et := at.Elem()
  5791  	if n.Class() == PAUTO && !n.Addrtaken() {
  5792  		return e.splitSlot(&name, "[0]", 0, et)
  5793  	}
  5794  	return ssa.LocalSlot{N: n, Type: et, Off: name.Off}
  5795  }
  5796  
  5797  func (e *ssafn) DerefItab(it *obj.LSym, offset int64) *obj.LSym {
  5798  	return itabsym(it, offset)
  5799  }
  5800  
  5801  // splitSlot returns a slot representing the data of parent starting at offset.
  5802  func (e *ssafn) splitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot {
  5803  	s := &types.Sym{Name: parent.N.(*Node).Sym.Name + suffix, Pkg: localpkg}
  5804  
  5805  	n := &Node{
  5806  		Name: new(Name),
  5807  		Op:   ONAME,
  5808  		Pos:  parent.N.(*Node).Pos,
  5809  	}
  5810  	n.Orig = n
  5811  
  5812  	s.Def = asTypesNode(n)
  5813  	asNode(s.Def).Name.SetUsed(true)
  5814  	n.Sym = s
  5815  	n.Type = t
  5816  	n.SetClass(PAUTO)
  5817  	n.SetAddable(true)
  5818  	n.Esc = EscNever
  5819  	n.Name.Curfn = e.curfn
  5820  	e.curfn.Func.Dcl = append(e.curfn.Func.Dcl, n)
  5821  	dowidth(t)
  5822  	return ssa.LocalSlot{N: n, Type: t, Off: 0, SplitOf: parent, SplitOffset: offset}
  5823  }
  5824  
  5825  func (e *ssafn) CanSSA(t *types.Type) bool {
  5826  	return canSSAType(t)
  5827  }
  5828  
  5829  func (e *ssafn) Line(pos src.XPos) string {
  5830  	return linestr(pos)
  5831  }
  5832  
  5833  // Log logs a message from the compiler.
  5834  func (e *ssafn) Logf(msg string, args ...interface{}) {
  5835  	if e.log {
  5836  		fmt.Printf(msg, args...)
  5837  	}
  5838  }
  5839  
  5840  func (e *ssafn) Log() bool {
  5841  	return e.log
  5842  }
  5843  
  5844  // Fatal reports a compiler error and exits.
  5845  func (e *ssafn) Fatalf(pos src.XPos, msg string, args ...interface{}) {
  5846  	lineno = pos
  5847  	nargs := append([]interface{}{e.curfn.funcname()}, args...)
  5848  	Fatalf("'%s': "+msg, nargs...)
  5849  }
  5850  
  5851  // Warnl reports a "warning", which is usually flag-triggered
  5852  // logging output for the benefit of tests.
  5853  func (e *ssafn) Warnl(pos src.XPos, fmt_ string, args ...interface{}) {
  5854  	Warnl(pos, fmt_, args...)
  5855  }
  5856  
  5857  func (e *ssafn) Debug_checknil() bool {
  5858  	return Debug_checknil != 0
  5859  }
  5860  
  5861  func (e *ssafn) UseWriteBarrier() bool {
  5862  	return use_writebarrier
  5863  }
  5864  
  5865  func (e *ssafn) Syslook(name string) *obj.LSym {
  5866  	switch name {
  5867  	case "goschedguarded":
  5868  		return goschedguarded
  5869  	case "writeBarrier":
  5870  		return writeBarrier
  5871  	case "gcWriteBarrier":
  5872  		return gcWriteBarrier
  5873  	case "typedmemmove":
  5874  		return typedmemmove
  5875  	case "typedmemclr":
  5876  		return typedmemclr
  5877  	}
  5878  	Fatalf("unknown Syslook func %v", name)
  5879  	return nil
  5880  }
  5881  
  5882  func (e *ssafn) SetWBPos(pos src.XPos) {
  5883  	e.curfn.Func.setWBPos(pos)
  5884  }
  5885  
  5886  func (n *Node) Typ() *types.Type {
  5887  	return n.Type
  5888  }
  5889  func (n *Node) StorageClass() ssa.StorageClass {
  5890  	switch n.Class() {
  5891  	case PPARAM:
  5892  		return ssa.ClassParam
  5893  	case PPARAMOUT:
  5894  		return ssa.ClassParamOut
  5895  	case PAUTO:
  5896  		return ssa.ClassAuto
  5897  	default:
  5898  		Fatalf("untranslatable storage class for %v: %s", n, n.Class())
  5899  		return 0
  5900  	}
  5901  }