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