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

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssagen
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"cmp"
    11  	"fmt"
    12  	"go/constant"
    13  	"html"
    14  	"internal/buildcfg"
    15  	"internal/goexperiment"
    16  	"internal/runtime/gc"
    17  	"os"
    18  	"path/filepath"
    19  	"slices"
    20  	"strings"
    21  
    22  	"cmd/compile/internal/abi"
    23  	"cmd/compile/internal/base"
    24  	"cmd/compile/internal/ir"
    25  	"cmd/compile/internal/liveness"
    26  	"cmd/compile/internal/objw"
    27  	"cmd/compile/internal/reflectdata"
    28  	"cmd/compile/internal/rttype"
    29  	"cmd/compile/internal/ssa"
    30  	"cmd/compile/internal/staticdata"
    31  	"cmd/compile/internal/typecheck"
    32  	"cmd/compile/internal/types"
    33  	"cmd/internal/obj"
    34  	"cmd/internal/objabi"
    35  	"cmd/internal/src"
    36  	"cmd/internal/sys"
    37  
    38  	rtabi "internal/abi"
    39  )
    40  
    41  var ssaConfig *ssa.Config
    42  var ssaCaches []ssa.Cache
    43  
    44  var ssaDump string     // early copy of $GOSSAFUNC; the func name to dump output for
    45  var ssaDir string      // optional destination for ssa dump file
    46  var ssaDumpStdout bool // whether to dump to stdout
    47  var ssaDumpCFG string  // generate CFGs for these phases
    48  const ssaDumpFile = "ssa.html"
    49  
    50  // ssaDumpInlined holds all inlined functions when ssaDump contains a function name.
    51  var ssaDumpInlined []*ir.Func
    52  
    53  // Maximum size we will aggregate heap allocations of scalar locals.
    54  // Almost certainly can't hurt to be as big as the tiny allocator.
    55  // Might help to be a bit bigger.
    56  const maxAggregatedHeapAllocation = 16
    57  
    58  func DumpInline(fn *ir.Func) {
    59  	if ssaDump != "" && ssaDump == ir.FuncName(fn) {
    60  		ssaDumpInlined = append(ssaDumpInlined, fn)
    61  	}
    62  }
    63  
    64  func InitEnv() {
    65  	ssaDump = os.Getenv("GOSSAFUNC")
    66  	ssaDir = os.Getenv("GOSSADIR")
    67  	if ssaDump != "" {
    68  		if strings.HasSuffix(ssaDump, "+") {
    69  			ssaDump = ssaDump[:len(ssaDump)-1]
    70  			ssaDumpStdout = true
    71  		}
    72  		spl := strings.Split(ssaDump, ":")
    73  		if len(spl) > 1 {
    74  			ssaDump = spl[0]
    75  			ssaDumpCFG = spl[1]
    76  		}
    77  	}
    78  }
    79  
    80  func InitConfig() {
    81  	types_ := ssa.NewTypes()
    82  
    83  	if Arch.SoftFloat {
    84  		softfloatInit()
    85  	}
    86  
    87  	// Generate a few pointer types that are uncommon in the frontend but common in the backend.
    88  	// Caching is disabled in the backend, so generating these here avoids allocations.
    89  	_ = types.NewPtr(types.Types[types.TINTER])                             // *interface{}
    90  	_ = types.NewPtr(types.NewPtr(types.Types[types.TSTRING]))              // **string
    91  	_ = types.NewPtr(types.NewSlice(types.Types[types.TINTER]))             // *[]interface{}
    92  	_ = types.NewPtr(types.NewPtr(types.ByteType))                          // **byte
    93  	_ = types.NewPtr(types.NewSlice(types.ByteType))                        // *[]byte
    94  	_ = types.NewPtr(types.NewSlice(types.Types[types.TSTRING]))            // *[]string
    95  	_ = types.NewPtr(types.NewPtr(types.NewPtr(types.Types[types.TUINT8]))) // ***uint8
    96  	_ = types.NewPtr(types.Types[types.TINT16])                             // *int16
    97  	_ = types.NewPtr(types.Types[types.TINT64])                             // *int64
    98  	_ = types.NewPtr(types.ErrorType)                                       // *error
    99  	_ = types.NewPtr(reflectdata.MapType())                                 // *internal/runtime/maps.Map
   100  	_ = types.NewPtr(deferstruct())                                         // *runtime._defer
   101  	types.NewPtrCacheEnabled = false
   102  	ssaConfig = ssa.NewConfig(base.Ctxt.Arch.Name, *types_, base.Ctxt, base.Flag.N == 0, Arch.SoftFloat)
   103  	ssaConfig.Race = base.Flag.Race
   104  	ssaCaches = make([]ssa.Cache, base.Flag.LowerC)
   105  
   106  	// Set up some runtime functions we'll need to call.
   107  	ir.Syms.AssertE2I = typecheck.LookupRuntimeFunc("assertE2I")
   108  	ir.Syms.AssertE2I2 = typecheck.LookupRuntimeFunc("assertE2I2")
   109  	ir.Syms.CgoCheckMemmove = typecheck.LookupRuntimeFunc("cgoCheckMemmove")
   110  	ir.Syms.CgoCheckPtrWrite = typecheck.LookupRuntimeFunc("cgoCheckPtrWrite")
   111  	ir.Syms.CheckPtrAlignment = typecheck.LookupRuntimeFunc("checkptrAlignment")
   112  	ir.Syms.Deferproc = typecheck.LookupRuntimeFunc("deferproc")
   113  	ir.Syms.Deferprocat = typecheck.LookupRuntimeFunc("deferprocat")
   114  	ir.Syms.DeferprocStack = typecheck.LookupRuntimeFunc("deferprocStack")
   115  	ir.Syms.Deferreturn = typecheck.LookupRuntimeFunc("deferreturn")
   116  	ir.Syms.Duffcopy = typecheck.LookupRuntimeFunc("duffcopy")
   117  	ir.Syms.Duffzero = typecheck.LookupRuntimeFunc("duffzero")
   118  	ir.Syms.GCWriteBarrier[0] = typecheck.LookupRuntimeFunc("gcWriteBarrier1")
   119  	ir.Syms.GCWriteBarrier[1] = typecheck.LookupRuntimeFunc("gcWriteBarrier2")
   120  	ir.Syms.GCWriteBarrier[2] = typecheck.LookupRuntimeFunc("gcWriteBarrier3")
   121  	ir.Syms.GCWriteBarrier[3] = typecheck.LookupRuntimeFunc("gcWriteBarrier4")
   122  	ir.Syms.GCWriteBarrier[4] = typecheck.LookupRuntimeFunc("gcWriteBarrier5")
   123  	ir.Syms.GCWriteBarrier[5] = typecheck.LookupRuntimeFunc("gcWriteBarrier6")
   124  	ir.Syms.GCWriteBarrier[6] = typecheck.LookupRuntimeFunc("gcWriteBarrier7")
   125  	ir.Syms.GCWriteBarrier[7] = typecheck.LookupRuntimeFunc("gcWriteBarrier8")
   126  	ir.Syms.Goschedguarded = typecheck.LookupRuntimeFunc("goschedguarded")
   127  	ir.Syms.Growslice = typecheck.LookupRuntimeFunc("growslice")
   128  	ir.Syms.GrowsliceBuf = typecheck.LookupRuntimeFunc("growsliceBuf")
   129  	ir.Syms.GrowsliceBufNoAlias = typecheck.LookupRuntimeFunc("growsliceBufNoAlias")
   130  	ir.Syms.GrowsliceNoAlias = typecheck.LookupRuntimeFunc("growsliceNoAlias")
   131  	ir.Syms.MoveSlice = typecheck.LookupRuntimeFunc("moveSlice")
   132  	ir.Syms.MoveSliceNoScan = typecheck.LookupRuntimeFunc("moveSliceNoScan")
   133  	ir.Syms.MoveSliceNoCap = typecheck.LookupRuntimeFunc("moveSliceNoCap")
   134  	ir.Syms.MoveSliceNoCapNoScan = typecheck.LookupRuntimeFunc("moveSliceNoCapNoScan")
   135  	ir.Syms.InterfaceSwitch = typecheck.LookupRuntimeFunc("interfaceSwitch")
   136  	for i := 1; i < len(ir.Syms.MallocGCSmallNoScan); i++ {
   137  		ir.Syms.MallocGCSmallNoScan[i] = typecheck.LookupRuntimeFunc(fmt.Sprintf("mallocgcSmallNoScanSC%d", i))
   138  	}
   139  	for i := 1; i < len(ir.Syms.MallocGCSmallScanNoHeader); i++ {
   140  		ir.Syms.MallocGCSmallScanNoHeader[i] = typecheck.LookupRuntimeFunc(fmt.Sprintf("mallocgcSmallScanNoHeaderSC%d", i))
   141  	}
   142  	ir.Syms.MallocGCTiny = typecheck.LookupRuntimeFunc("mallocgcTinySC2")
   143  	ir.Syms.MallocGC = typecheck.LookupRuntimeFunc("mallocgc")
   144  	ir.Syms.Memmove = typecheck.LookupRuntimeFunc("memmove")
   145  	ir.Syms.Memequal = typecheck.LookupRuntimeFunc("memequal")
   146  	ir.Syms.Msanread = typecheck.LookupRuntimeFunc("msanread")
   147  	ir.Syms.Msanwrite = typecheck.LookupRuntimeFunc("msanwrite")
   148  	ir.Syms.Msanmove = typecheck.LookupRuntimeFunc("msanmove")
   149  	ir.Syms.Asanread = typecheck.LookupRuntimeFunc("asanread")
   150  	ir.Syms.Asanwrite = typecheck.LookupRuntimeFunc("asanwrite")
   151  	ir.Syms.Newobject = typecheck.LookupRuntimeFunc("newobject")
   152  	ir.Syms.Newproc = typecheck.LookupRuntimeFunc("newproc")
   153  	ir.Syms.PanicBounds = typecheck.LookupRuntimeFunc("panicBounds")
   154  	ir.Syms.PanicExtend = typecheck.LookupRuntimeFunc("panicExtend")
   155  	ir.Syms.Panicdivide = typecheck.LookupRuntimeFunc("panicdivide")
   156  	ir.Syms.PanicdottypeE = typecheck.LookupRuntimeFunc("panicdottypeE")
   157  	ir.Syms.PanicdottypeI = typecheck.LookupRuntimeFunc("panicdottypeI")
   158  	ir.Syms.Panicnildottype = typecheck.LookupRuntimeFunc("panicnildottype")
   159  	ir.Syms.Panicoverflow = typecheck.LookupRuntimeFunc("panicoverflow")
   160  	ir.Syms.Panicshift = typecheck.LookupRuntimeFunc("panicshift")
   161  	ir.Syms.PanicSimdImm = typecheck.LookupRuntimeFunc("panicSimdImm")
   162  	ir.Syms.Racefuncenter = typecheck.LookupRuntimeFunc("racefuncenter")
   163  	ir.Syms.Racefuncexit = typecheck.LookupRuntimeFunc("racefuncexit")
   164  	ir.Syms.Raceread = typecheck.LookupRuntimeFunc("raceread")
   165  	ir.Syms.Racereadrange = typecheck.LookupRuntimeFunc("racereadrange")
   166  	ir.Syms.Racewrite = typecheck.LookupRuntimeFunc("racewrite")
   167  	ir.Syms.Racewriterange = typecheck.LookupRuntimeFunc("racewriterange")
   168  	ir.Syms.TypeAssert = typecheck.LookupRuntimeFunc("typeAssert")
   169  	ir.Syms.WBZero = typecheck.LookupRuntimeFunc("wbZero")
   170  	ir.Syms.WBMove = typecheck.LookupRuntimeFunc("wbMove")
   171  	ir.Syms.X86HasAVX = typecheck.LookupRuntimeVar("x86HasAVX")                       // bool
   172  	ir.Syms.X86HasFMA = typecheck.LookupRuntimeVar("x86HasFMA")                       // bool
   173  	ir.Syms.X86HasPOPCNT = typecheck.LookupRuntimeVar("x86HasPOPCNT")                 // bool
   174  	ir.Syms.X86HasSSE41 = typecheck.LookupRuntimeVar("x86HasSSE41")                   // bool
   175  	ir.Syms.ARMHasVFPv4 = typecheck.LookupRuntimeVar("armHasVFPv4")                   // bool
   176  	ir.Syms.ARM64HasATOMICS = typecheck.LookupRuntimeVar("arm64HasATOMICS")           // bool
   177  	ir.Syms.Loong64HasLAMCAS = typecheck.LookupRuntimeVar("loong64HasLAMCAS")         // bool
   178  	ir.Syms.Loong64HasLAM_BH = typecheck.LookupRuntimeVar("loong64HasLAM_BH")         // bool
   179  	ir.Syms.Loong64HasDBAR_HINTS = typecheck.LookupRuntimeVar("loong64HasDBAR_HINTS") // bool
   180  	ir.Syms.Loong64HasLSX = typecheck.LookupRuntimeVar("loong64HasLSX")               // bool
   181  	ir.Syms.RISCV64HasZbb = typecheck.LookupRuntimeVar("riscv64HasZbb")               // bool
   182  	ir.Syms.Staticuint64s = typecheck.LookupRuntimeVar("staticuint64s")
   183  	ir.Syms.Typedmemmove = typecheck.LookupRuntimeFunc("typedmemmove")
   184  	ir.Syms.Udiv = typecheck.LookupRuntimeVar("udiv")                 // asm func with special ABI
   185  	ir.Syms.WriteBarrier = typecheck.LookupRuntimeVar("writeBarrier") // struct { bool; ... }
   186  	ir.Syms.Zerobase = typecheck.LookupRuntimeVar("zerobase")
   187  	ir.Syms.ZeroVal = typecheck.LookupRuntimeVar("zeroVal")
   188  
   189  	if Arch.LinkArch.Family == sys.Wasm {
   190  		BoundsCheckFunc[ssa.BoundsIndex] = typecheck.LookupRuntimeFunc("goPanicIndex")
   191  		BoundsCheckFunc[ssa.BoundsIndexU] = typecheck.LookupRuntimeFunc("goPanicIndexU")
   192  		BoundsCheckFunc[ssa.BoundsSliceAlen] = typecheck.LookupRuntimeFunc("goPanicSliceAlen")
   193  		BoundsCheckFunc[ssa.BoundsSliceAlenU] = typecheck.LookupRuntimeFunc("goPanicSliceAlenU")
   194  		BoundsCheckFunc[ssa.BoundsSliceAcap] = typecheck.LookupRuntimeFunc("goPanicSliceAcap")
   195  		BoundsCheckFunc[ssa.BoundsSliceAcapU] = typecheck.LookupRuntimeFunc("goPanicSliceAcapU")
   196  		BoundsCheckFunc[ssa.BoundsSliceB] = typecheck.LookupRuntimeFunc("goPanicSliceB")
   197  		BoundsCheckFunc[ssa.BoundsSliceBU] = typecheck.LookupRuntimeFunc("goPanicSliceBU")
   198  		BoundsCheckFunc[ssa.BoundsSlice3Alen] = typecheck.LookupRuntimeFunc("goPanicSlice3Alen")
   199  		BoundsCheckFunc[ssa.BoundsSlice3AlenU] = typecheck.LookupRuntimeFunc("goPanicSlice3AlenU")
   200  		BoundsCheckFunc[ssa.BoundsSlice3Acap] = typecheck.LookupRuntimeFunc("goPanicSlice3Acap")
   201  		BoundsCheckFunc[ssa.BoundsSlice3AcapU] = typecheck.LookupRuntimeFunc("goPanicSlice3AcapU")
   202  		BoundsCheckFunc[ssa.BoundsSlice3B] = typecheck.LookupRuntimeFunc("goPanicSlice3B")
   203  		BoundsCheckFunc[ssa.BoundsSlice3BU] = typecheck.LookupRuntimeFunc("goPanicSlice3BU")
   204  		BoundsCheckFunc[ssa.BoundsSlice3C] = typecheck.LookupRuntimeFunc("goPanicSlice3C")
   205  		BoundsCheckFunc[ssa.BoundsSlice3CU] = typecheck.LookupRuntimeFunc("goPanicSlice3CU")
   206  		BoundsCheckFunc[ssa.BoundsConvert] = typecheck.LookupRuntimeFunc("goPanicSliceConvert")
   207  	}
   208  
   209  	// Wasm (all asm funcs with special ABIs)
   210  	ir.Syms.WasmDiv = typecheck.LookupRuntimeVar("wasmDiv")
   211  	ir.Syms.WasmTruncS = typecheck.LookupRuntimeVar("wasmTruncS")
   212  	ir.Syms.WasmTruncU = typecheck.LookupRuntimeVar("wasmTruncU")
   213  	ir.Syms.SigPanic = typecheck.LookupRuntimeFunc("sigpanic")
   214  }
   215  
   216  func InitTables() {
   217  	initIntrinsics(nil)
   218  }
   219  
   220  // AbiForBodylessFuncStackMap returns the ABI for a bodyless function's stack map.
   221  // This is not necessarily the ABI used to call it.
   222  // Currently (1.17 dev) such a stack map is always ABI0;
   223  // any ABI wrapper that is present is nosplit, hence a precise
   224  // stack map is not needed there (the parameters survive only long
   225  // enough to call the wrapped assembly function).
   226  // This always returns a freshly copied ABI.
   227  func AbiForBodylessFuncStackMap(fn *ir.Func) *abi.ABIConfig {
   228  	return ssaConfig.ABI0.Copy() // No idea what races will result, be safe
   229  }
   230  
   231  // abiForFunc implements ABI policy for a function, but does not return a copy of the ABI.
   232  // Passing a nil function returns the default ABI based on experiment configuration.
   233  func abiForFunc(fn *ir.Func, abi0, abi1 *abi.ABIConfig) *abi.ABIConfig {
   234  	if buildcfg.Experiment.RegabiArgs {
   235  		// Select the ABI based on the function's defining ABI.
   236  		if fn == nil {
   237  			return abi1
   238  		}
   239  		switch fn.ABI {
   240  		case obj.ABI0:
   241  			return abi0
   242  		case obj.ABIInternal:
   243  			// TODO(austin): Clean up the nomenclature here.
   244  			// It's not clear that "abi1" is ABIInternal.
   245  			return abi1
   246  		}
   247  		base.Fatalf("function %v has unknown ABI %v", fn, fn.ABI)
   248  		panic("not reachable")
   249  	}
   250  
   251  	a := abi0
   252  	if fn != nil {
   253  		if fn.Pragma&ir.RegisterParams != 0 { // TODO(register args) remove after register abi is working
   254  			a = abi1
   255  		}
   256  	}
   257  	return a
   258  }
   259  
   260  // emitOpenDeferInfo emits FUNCDATA information about the defers in a function
   261  // that is using open-coded defers.  This funcdata is used to determine the active
   262  // defers in a function and execute those defers during panic processing.
   263  //
   264  // The funcdata is all encoded in varints (since values will almost always be less than
   265  // 128, but stack offsets could potentially be up to 2Gbyte). All "locations" (offsets)
   266  // for stack variables are specified as the number of bytes below varp (pointer to the
   267  // top of the local variables) for their starting address. The format is:
   268  //
   269  //   - Offset of the deferBits variable
   270  //   - Offset of the first closure slot (the rest are laid out consecutively).
   271  func (s *state) emitOpenDeferInfo() {
   272  	firstOffset := s.openDefers[0].closureNode.FrameOffset()
   273  
   274  	// Verify that cmpstackvarlt laid out the slots in order.
   275  	for i, r := range s.openDefers {
   276  		have := r.closureNode.FrameOffset()
   277  		want := firstOffset + int64(i)*int64(types.PtrSize)
   278  		if have != want {
   279  			base.FatalfAt(s.curfn.Pos(), "unexpected frame offset for open-coded defer slot #%v: have %v, want %v", i, have, want)
   280  		}
   281  	}
   282  
   283  	x := base.Ctxt.Lookup(s.curfn.LSym.Name + ".opendefer")
   284  	x.Set(obj.AttrContentAddressable, true)
   285  	x.Align = 1
   286  	s.curfn.LSym.Func().OpenCodedDeferInfo = x
   287  
   288  	off := 0
   289  	off = objw.Uvarint(x, off, uint64(-s.deferBitsTemp.FrameOffset()))
   290  	off = objw.Uvarint(x, off, uint64(-firstOffset))
   291  }
   292  
   293  // buildssa builds an SSA function for fn.
   294  // worker indicates which of the backend workers is doing the processing.
   295  func buildssa(fn *ir.Func, worker int, isPgoHot bool) *ssa.Func {
   296  	name := ir.FuncName(fn)
   297  
   298  	abiSelf := abiForFunc(fn, ssaConfig.ABI0, ssaConfig.ABI1)
   299  
   300  	printssa := false
   301  	// match either a simple name e.g. "(*Reader).Reset", package.name e.g. "compress/gzip.(*Reader).Reset", or subpackage name "gzip.(*Reader).Reset"
   302  	// optionally allows an ABI suffix specification in the GOSSAHASH, e.g. "(*Reader).Reset<0>" etc
   303  	if strings.Contains(ssaDump, name) { // in all the cases the function name is entirely contained within the GOSSAFUNC string.
   304  		nameOptABI := name
   305  		if l := len(ssaDump); l > 1 && ssaDump[l-2] == ',' { // ABI specification
   306  			nameOptABI = ssa.FuncNameABI(name, abiSelf.Which())
   307  		} else if strings.HasSuffix(ssaDump, ">") { // if they use the linker syntax instead....
   308  			l := len(ssaDump)
   309  			if l >= 3 && ssaDump[l-3] == '<' {
   310  				nameOptABI = ssa.FuncNameABI(name, abiSelf.Which())
   311  				ssaDump = ssaDump[:l-3] + "," + ssaDump[l-2:l-1]
   312  			}
   313  		}
   314  		pkgDotName := base.Ctxt.Pkgpath + "." + nameOptABI
   315  		printssa = nameOptABI == ssaDump || // "(*Reader).Reset"
   316  			pkgDotName == ssaDump || // "compress/gzip.(*Reader).Reset"
   317  			strings.HasSuffix(pkgDotName, ssaDump) && strings.HasSuffix(pkgDotName, "/"+ssaDump) // "gzip.(*Reader).Reset"
   318  	}
   319  
   320  	var astBuf *bytes.Buffer
   321  	if printssa {
   322  		astBuf = &bytes.Buffer{}
   323  		ir.FDumpList(astBuf, "buildssa-body", fn.Body)
   324  		if ssaDumpStdout {
   325  			fmt.Println("generating SSA for", name)
   326  			fmt.Print(astBuf.String())
   327  		}
   328  	}
   329  
   330  	var s state
   331  	s.pushLine(fn.Pos())
   332  	defer s.popLine()
   333  
   334  	s.hasdefer = fn.HasDefer()
   335  	if fn.Pragma&ir.CgoUnsafeArgs != 0 {
   336  		s.cgoUnsafeArgs = true
   337  	}
   338  	s.checkPtrEnabled = ir.ShouldCheckPtr(fn, 1)
   339  
   340  	if base.Flag.Cfg.Instrumenting && fn.Pragma&ir.Norace == 0 && !fn.Linksym().ABIWrapper() {
   341  		if !base.Flag.Race || !objabi.LookupPkgSpecial(fn.Sym().Pkg.Path).NoRaceFunc {
   342  			s.instrumentMemory = true
   343  			if base.Flag.Race {
   344  				s.instrumentEnterExit = true
   345  			}
   346  		}
   347  	}
   348  
   349  	fe := ssafn{
   350  		curfn: fn,
   351  		log:   printssa && ssaDumpStdout,
   352  	}
   353  	s.curfn = fn
   354  
   355  	cache := &ssaCaches[worker]
   356  	cache.Reset()
   357  
   358  	s.f = ssaConfig.NewFunc(&fe, cache)
   359  	s.config = ssaConfig
   360  	s.f.Type = fn.Type()
   361  	s.f.Name = name
   362  	s.f.PrintOrHtmlSSA = printssa
   363  	if fn.Pragma&ir.Nosplit != 0 {
   364  		s.f.NoSplit = true
   365  	}
   366  	s.f.ABI0 = ssaConfig.ABI0
   367  	s.f.ABI1 = ssaConfig.ABI1
   368  	s.f.ABIDefault = abiForFunc(nil, ssaConfig.ABI0, ssaConfig.ABI1)
   369  	s.f.ABISelf = abiSelf
   370  
   371  	s.panics = map[funcLine]*ssa.Block{}
   372  	s.softFloat = s.config.SoftFloat
   373  
   374  	// Allocate starting block
   375  	s.f.Entry = s.f.NewBlock(ssa.BlockPlain)
   376  	s.f.Entry.Pos = fn.Pos()
   377  	s.f.IsPgoHot = isPgoHot
   378  
   379  	if printssa {
   380  		ssaDF := ssaDumpFile
   381  		if ssaDir != "" {
   382  			ssaDF = filepath.Join(ssaDir, base.Ctxt.Pkgpath+"."+s.f.NameABI()+".html")
   383  			ssaD := filepath.Dir(ssaDF)
   384  			os.MkdirAll(ssaD, 0755)
   385  		}
   386  		s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDF, s.f, ssaDumpCFG)
   387  		// TODO: generate and print a mapping from nodes to values and blocks
   388  		dumpSourcesColumn(s.f.HTMLWriter, fn)
   389  		s.f.HTMLWriter.WriteAST("AST", astBuf)
   390  	}
   391  
   392  	// Allocate starting values
   393  	s.labels = map[string]*ssaLabel{}
   394  	s.fwdVars = map[ir.Node]*ssa.Value{}
   395  	s.startmem = s.entryNewValue0(ssa.OpInitMem, types.TypeMem)
   396  
   397  	s.hasOpenDefers = base.Flag.N == 0 && s.hasdefer && !s.curfn.OpenCodedDeferDisallowed()
   398  	switch {
   399  	case base.Debug.NoOpenDefer != 0:
   400  		s.hasOpenDefers = false
   401  	case s.hasOpenDefers && (base.Ctxt.Flag_shared || base.Ctxt.Flag_dynlink) && base.Ctxt.Arch.Name == "386":
   402  		// Don't support open-coded defers for 386 ONLY when using shared
   403  		// libraries, because there is extra code (added by rewriteToUseGot())
   404  		// preceding the deferreturn/ret code that we don't track correctly.
   405  		//
   406  		// TODO this restriction can be removed given adjusted offset in computeDeferReturn in cmd/link/internal/ld/pcln.go
   407  		s.hasOpenDefers = false
   408  	}
   409  	if s.hasOpenDefers && s.instrumentEnterExit {
   410  		// Skip doing open defers if we need to instrument function
   411  		// returns for the race detector, since we will not generate that
   412  		// code in the case of the extra deferreturn/ret segment.
   413  		s.hasOpenDefers = false
   414  	}
   415  	if s.hasOpenDefers {
   416  		// Similarly, skip if there are any heap-allocated result
   417  		// parameters that need to be copied back to their stack slots.
   418  		for _, f := range s.curfn.Type().Results() {
   419  			if !f.Nname.(*ir.Name).OnStack() {
   420  				s.hasOpenDefers = false
   421  				break
   422  			}
   423  		}
   424  	}
   425  	if s.hasOpenDefers &&
   426  		s.curfn.NumReturns*s.curfn.NumDefers > 15 {
   427  		// Since we are generating defer calls at every exit for
   428  		// open-coded defers, skip doing open-coded defers if there are
   429  		// too many returns (especially if there are multiple defers).
   430  		// Open-coded defers are most important for improving performance
   431  		// for smaller functions (which don't have many returns).
   432  		s.hasOpenDefers = false
   433  	}
   434  
   435  	s.sp = s.entryNewValue0(ssa.OpSP, types.Types[types.TUINTPTR]) // TODO: use generic pointer type (unsafe.Pointer?) instead
   436  	s.sb = s.entryNewValue0(ssa.OpSB, types.Types[types.TUINTPTR])
   437  
   438  	s.startBlock(s.f.Entry)
   439  	s.vars[memVar] = s.startmem
   440  	if s.hasOpenDefers {
   441  		// Create the deferBits variable and stack slot.  deferBits is a
   442  		// bitmask showing which of the open-coded defers in this function
   443  		// have been activated.
   444  		deferBitsTemp := typecheck.TempAt(src.NoXPos, s.curfn, types.Types[types.TUINT8])
   445  		deferBitsTemp.SetAddrtaken(true)
   446  		s.deferBitsTemp = deferBitsTemp
   447  		// For this value, AuxInt is initialized to zero by default
   448  		startDeferBits := s.entryNewValue0(ssa.OpConst8, types.Types[types.TUINT8])
   449  		s.vars[deferBitsVar] = startDeferBits
   450  		s.deferBitsAddr = s.addr(deferBitsTemp)
   451  		s.store(types.Types[types.TUINT8], s.deferBitsAddr, startDeferBits)
   452  		// Make sure that the deferBits stack slot is kept alive (for use
   453  		// by panics) and stores to deferBits are not eliminated, even if
   454  		// all checking code on deferBits in the function exit can be
   455  		// eliminated, because the defer statements were all
   456  		// unconditional.
   457  		s.vars[memVar] = s.newValue1Apos(ssa.OpVarLive, types.TypeMem, deferBitsTemp, s.mem(), false)
   458  	}
   459  
   460  	var params *abi.ABIParamResultInfo
   461  	params = s.f.ABISelf.ABIAnalyze(fn.Type(), true)
   462  
   463  	// The backend's stackframe pass prunes away entries from the fn's
   464  	// Dcl list, including PARAMOUT nodes that correspond to output
   465  	// params passed in registers. Walk the Dcl list and capture these
   466  	// nodes to a side list, so that we'll have them available during
   467  	// DWARF-gen later on. See issue 48573 for more details.
   468  	var debugInfo ssa.FuncDebug
   469  	for _, n := range fn.Dcl {
   470  		if n.Class == ir.PPARAMOUT && n.IsOutputParamInRegisters() {
   471  			debugInfo.RegOutputParams = append(debugInfo.RegOutputParams, n)
   472  		}
   473  	}
   474  	fn.DebugInfo = &debugInfo
   475  
   476  	// Generate addresses of local declarations
   477  	s.decladdrs = map[*ir.Name]*ssa.Value{}
   478  	for _, n := range fn.Dcl {
   479  		switch n.Class {
   480  		case ir.PPARAM:
   481  			// Be aware that blank and unnamed input parameters will not appear here, but do appear in the type
   482  			s.decladdrs[n] = s.entryNewValue2A(ssa.OpLocalAddr, types.NewPtr(n.Type()), n, s.sp, s.startmem)
   483  		case ir.PPARAMOUT:
   484  			s.decladdrs[n] = s.entryNewValue2A(ssa.OpLocalAddr, types.NewPtr(n.Type()), n, s.sp, s.startmem)
   485  		case ir.PAUTO:
   486  			// processed at each use, to prevent Addr coming
   487  			// before the decl.
   488  		default:
   489  			s.Fatalf("local variable with class %v unimplemented", n.Class)
   490  		}
   491  	}
   492  
   493  	s.f.OwnAux = ssa.OwnAuxCall(fn.LSym, params)
   494  
   495  	// Populate SSAable arguments.
   496  	for _, n := range fn.Dcl {
   497  		if n.Class == ir.PPARAM {
   498  			if s.canSSA(n) {
   499  				v := s.newValue0A(ssa.OpArg, n.Type(), n)
   500  				s.vars[n] = v
   501  				s.addNamedValue(n, v) // This helps with debugging information, not needed for compilation itself.
   502  			} else { // address was taken AND/OR too large for SSA
   503  				paramAssignment := ssa.ParamAssignmentForArgName(s.f, n)
   504  				if len(paramAssignment.Registers) > 0 {
   505  					if ssa.CanSSA(n.Type()) { // SSA-able type, so address was taken -- receive value in OpArg, DO NOT bind to var, store immediately to memory.
   506  						v := s.newValue0A(ssa.OpArg, n.Type(), n)
   507  						s.store(n.Type(), s.decladdrs[n], v)
   508  					} else { // Too big for SSA.
   509  						// Brute force, and early, do a bunch of stores from registers
   510  						// Note that expand calls knows about this and doesn't trouble itself with larger-than-SSA-able Args in registers.
   511  						s.storeParameterRegsToStack(s.f.ABISelf, paramAssignment, n, s.decladdrs[n], false)
   512  					}
   513  				}
   514  			}
   515  		}
   516  	}
   517  
   518  	// Populate closure variables.
   519  	if fn.Needctxt() {
   520  		clo := s.entryNewValue0(ssa.OpGetClosurePtr, s.f.Config.Types.BytePtr)
   521  		if fn.RangeParent != nil && base.Flag.N != 0 {
   522  			// For a range body closure, keep its closure pointer live on the
   523  			// stack with a special name, so the debugger can look for it and
   524  			// find the parent frame.
   525  			sym := &types.Sym{Name: ".closureptr", Pkg: types.LocalPkg}
   526  			cloSlot := s.curfn.NewLocal(src.NoXPos, sym, s.f.Config.Types.BytePtr)
   527  			cloSlot.SetUsed(true)
   528  			cloSlot.SetEsc(ir.EscNever)
   529  			cloSlot.SetAddrtaken(true)
   530  			s.f.CloSlot = cloSlot
   531  			s.vars[memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, cloSlot, s.mem(), false)
   532  			addr := s.addr(cloSlot)
   533  			s.store(s.f.Config.Types.BytePtr, addr, clo)
   534  			// Keep it from being dead-store eliminated.
   535  			s.vars[memVar] = s.newValue1Apos(ssa.OpVarLive, types.TypeMem, cloSlot, s.mem(), false)
   536  		}
   537  		csiter := typecheck.NewClosureStructIter(fn.ClosureVars)
   538  		for {
   539  			n, typ, offset := csiter.Next()
   540  			if n == nil {
   541  				break
   542  			}
   543  
   544  			ptr := s.newValue1I(ssa.OpOffPtr, types.NewPtr(typ), offset, clo)
   545  
   546  			// If n is a small variable captured by value, promote
   547  			// it to PAUTO so it can be converted to SSA.
   548  			//
   549  			// Note: While we never capture a variable by value if
   550  			// the user took its address, we may have generated
   551  			// runtime calls that did (#43701). Since we don't
   552  			// convert Addrtaken variables to SSA anyway, no point
   553  			// in promoting them either.
   554  			if n.Byval() && !n.Addrtaken() && ssa.CanSSA(n.Type()) {
   555  				n.Class = ir.PAUTO
   556  				fn.Dcl = append(fn.Dcl, n)
   557  				s.assign(n, s.load(n.Type(), ptr), false, 0)
   558  				continue
   559  			}
   560  
   561  			if !n.Byval() {
   562  				ptr = s.load(typ, ptr)
   563  			}
   564  			s.setHeapaddr(fn.Pos(), n, ptr)
   565  		}
   566  	}
   567  
   568  	// Convert the AST-based IR to the SSA-based IR
   569  	if s.instrumentEnterExit {
   570  		s.rtcall(ir.Syms.Racefuncenter, true, nil, s.newValue0(ssa.OpGetCallerPC, types.Types[types.TUINTPTR]))
   571  	}
   572  	s.zeroResults()
   573  	s.paramsToHeap()
   574  	s.stmtList(fn.Body)
   575  
   576  	// fallthrough to exit
   577  	if s.curBlock != nil {
   578  		s.pushLine(fn.Endlineno)
   579  		s.exit()
   580  		s.popLine()
   581  	}
   582  
   583  	for _, b := range s.f.Blocks {
   584  		if b.Pos != src.NoXPos {
   585  			s.updateUnsetPredPos(b)
   586  		}
   587  	}
   588  
   589  	s.f.HTMLWriter.WritePhase("before insert phis", "before insert phis")
   590  
   591  	s.insertPhis()
   592  
   593  	// Main call to ssa package to compile function
   594  	ssa.Compile(s.f)
   595  
   596  	fe.AllocFrame(s.f)
   597  
   598  	if len(s.openDefers) != 0 {
   599  		s.emitOpenDeferInfo()
   600  	}
   601  
   602  	// Record incoming parameter spill information for morestack calls emitted in the assembler.
   603  	// This is done here, using all the parameters (used, partially used, and unused) because
   604  	// it mimics the behavior of the former ABI (everything stored) and because it's not 100%
   605  	// clear if naming conventions are respected in autogenerated code.
   606  	// TODO figure out exactly what's unused, don't spill it. Make liveness fine-grained, also.
   607  	for _, p := range params.InParams() {
   608  		typs, offs := p.RegisterTypesAndOffsets()
   609  		if len(offs) < len(typs) {
   610  			s.Fatalf("len(offs)=%d < len(typs)=%d, params=\n%s", len(offs), len(typs), params)
   611  		}
   612  		for i, t := range typs {
   613  			o := offs[i]                // offset within parameter
   614  			fo := p.FrameOffset(params) // offset of parameter in frame
   615  			reg := ssa.ObjRegForAbiReg(p.Registers[i], s.f.Config)
   616  			s.f.RegArgs = append(s.f.RegArgs, ssa.Spill{Reg: reg, Offset: fo + o, Type: t})
   617  		}
   618  	}
   619  
   620  	return s.f
   621  }
   622  
   623  func (s *state) storeParameterRegsToStack(abi *abi.ABIConfig, paramAssignment *abi.ABIParamAssignment, n *ir.Name, addr *ssa.Value, pointersOnly bool) {
   624  	typs, offs := paramAssignment.RegisterTypesAndOffsets()
   625  	for i, t := range typs {
   626  		if pointersOnly && !t.IsPtrShaped() {
   627  			continue
   628  		}
   629  		r := paramAssignment.Registers[i]
   630  		o := offs[i]
   631  		op, reg := ssa.ArgOpAndRegisterFor(r, abi)
   632  		aux := &ssa.AuxNameOffset{Name: n, Offset: o}
   633  		v := s.newValue0I(op, t, reg)
   634  		v.Aux = aux
   635  		p := s.newValue1I(ssa.OpOffPtr, types.NewPtr(t), o, addr)
   636  		s.store(t, p, v)
   637  	}
   638  }
   639  
   640  // zeroResults zeros the return values at the start of the function.
   641  // We need to do this very early in the function.  Defer might stop a
   642  // panic and show the return values as they exist at the time of
   643  // panic.  For precise stacks, the garbage collector assumes results
   644  // are always live, so we need to zero them before any allocations,
   645  // even allocations to move params/results to the heap.
   646  func (s *state) zeroResults() {
   647  	for _, f := range s.curfn.Type().Results() {
   648  		n := f.Nname.(*ir.Name)
   649  		if !n.OnStack() {
   650  			// The local which points to the return value is the
   651  			// thing that needs zeroing. This is already handled
   652  			// by a Needzero annotation in plive.go:(*liveness).epilogue.
   653  			continue
   654  		}
   655  		// Zero the stack location containing f.
   656  		if typ := n.Type(); ssa.CanSSA(typ) {
   657  			s.assign(n, s.zeroVal(typ), false, 0)
   658  		} else {
   659  			if typ.HasPointers() || ssa.IsMergeCandidate(n) {
   660  				s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, n, s.mem())
   661  			}
   662  			s.zero(n.Type(), s.decladdrs[n])
   663  		}
   664  	}
   665  }
   666  
   667  // paramsToHeap produces code to allocate memory for heap-escaped parameters
   668  // and to copy non-result parameters' values from the stack.
   669  func (s *state) paramsToHeap() {
   670  	do := func(params []*types.Field) {
   671  		for _, f := range params {
   672  			if f.Nname == nil {
   673  				continue // anonymous or blank parameter
   674  			}
   675  			n := f.Nname.(*ir.Name)
   676  			if ir.IsBlank(n) || n.OnStack() {
   677  				continue
   678  			}
   679  			s.newHeapaddr(n)
   680  			if n.Class == ir.PPARAM {
   681  				s.move(n.Type(), s.expr(n.Heapaddr), s.decladdrs[n])
   682  			}
   683  		}
   684  	}
   685  
   686  	typ := s.curfn.Type()
   687  	do(typ.Recvs())
   688  	do(typ.Params())
   689  	do(typ.Results())
   690  }
   691  
   692  // allocSizeAndAlign returns the size and alignment of t.
   693  // Normally just t.Size() and t.Alignment(), but there
   694  // is a special case to handle 64-bit atomics on 32-bit systems.
   695  func allocSizeAndAlign(t *types.Type) (int64, int64) {
   696  	size, align := t.Size(), t.Alignment()
   697  	if types.PtrSize == 4 && align == 4 && size >= 8 {
   698  		// For 64-bit atomics on 32-bit systems.
   699  		size = types.RoundUp(size, 8)
   700  		align = 8
   701  	}
   702  	return size, align
   703  }
   704  func allocSize(t *types.Type) int64 {
   705  	size, _ := allocSizeAndAlign(t)
   706  	return size
   707  }
   708  func allocAlign(t *types.Type) int64 {
   709  	_, align := allocSizeAndAlign(t)
   710  	return align
   711  }
   712  
   713  // newHeapaddr allocates heap memory for n and sets its heap address.
   714  func (s *state) newHeapaddr(n *ir.Name) {
   715  	size := allocSize(n.Type())
   716  	if n.Type().HasPointers() || size >= maxAggregatedHeapAllocation || size == 0 {
   717  		s.setHeapaddr(n.Pos(), n, s.newObject(n.Type()))
   718  		return
   719  	}
   720  
   721  	// Do we have room together with our pending allocations?
   722  	// If not, flush all the current ones.
   723  	var used int64
   724  	for _, v := range s.pendingHeapAllocations {
   725  		used += allocSize(v.Type.Elem())
   726  	}
   727  	if used+size > maxAggregatedHeapAllocation {
   728  		s.flushPendingHeapAllocations()
   729  	}
   730  
   731  	var allocCall *ssa.Value // (SelectN [0] (call of runtime.newobject))
   732  	if len(s.pendingHeapAllocations) == 0 {
   733  		// Make an allocation, but the type being allocated is just
   734  		// the first pending object. We will come back and update it
   735  		// later if needed.
   736  		allocCall = s.newObjectNonSpecialized(n.Type(), nil)
   737  	} else {
   738  		allocCall = s.pendingHeapAllocations[0].Args[0]
   739  	}
   740  	// v is an offset to the shared allocation. Offsets are dummy 0s for now.
   741  	v := s.newValue1I(ssa.OpOffPtr, n.Type().PtrTo(), 0, allocCall)
   742  
   743  	// Add to list of pending allocations.
   744  	s.pendingHeapAllocations = append(s.pendingHeapAllocations, v)
   745  
   746  	// Finally, record for posterity.
   747  	s.setHeapaddr(n.Pos(), n, v)
   748  }
   749  
   750  func (s *state) flushPendingHeapAllocations() {
   751  	pending := s.pendingHeapAllocations
   752  	if len(pending) == 0 {
   753  		return // nothing to do
   754  	}
   755  	s.pendingHeapAllocations = nil // reset state
   756  	ptr := pending[0].Args[0]      // The SelectN [0] op
   757  	call := ptr.Args[0]            // The runtime.newobject call
   758  
   759  	if len(pending) == 1 {
   760  		// Just a single object, do a standard allocation.
   761  		v := pending[0]
   762  		v.Op = ssa.OpCopy // instead of OffPtr [0]
   763  		return
   764  	}
   765  
   766  	// Sort in decreasing alignment.
   767  	// This way we never have to worry about padding.
   768  	// (Stable not required; just cleaner to keep program order among equal alignments.)
   769  	slices.SortStableFunc(pending, func(x, y *ssa.Value) int {
   770  		return cmp.Compare(allocAlign(y.Type.Elem()), allocAlign(x.Type.Elem()))
   771  	})
   772  
   773  	// Figure out how much data we need allocate.
   774  	var size int64
   775  	for _, v := range pending {
   776  		v.AuxInt = size // Adjust OffPtr to the right value while we are here.
   777  		size += allocSize(v.Type.Elem())
   778  	}
   779  	align := allocAlign(pending[0].Type.Elem())
   780  	size = types.RoundUp(size, align)
   781  
   782  	// Convert newObject call to a mallocgc call.
   783  	args := []*ssa.Value{
   784  		s.constInt(types.Types[types.TUINTPTR], size),
   785  		s.constNil(call.Args[0].Type), // a nil *runtime._type
   786  		s.constBool(true),             // needZero TODO: false is ok?
   787  		call.Args[1],                  // memory
   788  	}
   789  	mallocSym := ir.Syms.MallocGC
   790  	if specialMallocSym := s.specializedMallocSym(size, false); specialMallocSym != nil {
   791  		mallocSym = specialMallocSym
   792  	}
   793  	call.Aux = ssa.StaticAuxCall(mallocSym, s.f.ABIDefault.ABIAnalyzeTypes(
   794  		[]*types.Type{args[0].Type, args[1].Type, args[2].Type},
   795  		[]*types.Type{types.Types[types.TUNSAFEPTR]},
   796  	))
   797  	call.AuxInt = 4 * s.config.PtrSize // arg+results size, uintptr/ptr/bool/ptr
   798  	call.SetArgs4(args[0], args[1], args[2], args[3])
   799  	// TODO: figure out how to pass alignment to runtime
   800  
   801  	call.Type = types.NewTuple(types.Types[types.TUNSAFEPTR], types.TypeMem)
   802  	ptr.Type = types.Types[types.TUNSAFEPTR]
   803  }
   804  
   805  func (s *state) specializedMallocSym(size int64, hasPointers bool) *obj.LSym {
   806  	if !s.sizeSpecializedMallocEnabled() {
   807  		return nil
   808  	}
   809  	const specializedMallocMax = 80 // This must match the constant in mkmalloc.
   810  	if size > specializedMallocMax {
   811  		return nil
   812  	}
   813  	divRoundUp := func(n, a uintptr) uintptr { return (n + a - 1) / a }
   814  	sizeClass := gc.SizeToSizeClass8[divRoundUp(uintptr(size), gc.SmallSizeDiv)]
   815  	if hasPointers {
   816  		return ir.Syms.MallocGCSmallScanNoHeader[sizeClass]
   817  	}
   818  	if size < gc.TinySize {
   819  		return ir.Syms.MallocGCTiny
   820  	}
   821  	return ir.Syms.MallocGCSmallNoScan[sizeClass]
   822  }
   823  
   824  func (s *state) sizeSpecializedMallocEnabled() bool {
   825  	if base.Flag.CompilingRuntime {
   826  		// The compiler forces the values of the asan, msan, and race flags to false if
   827  		// we're compiling the runtime, so we lose the information about whether we're
   828  		// building in asan, msan, or race mode. Because the specialized functions don't
   829  		// work in that mode, just turn if off in that case.
   830  		// TODO(matloob): Save the information about whether the flags were passed in
   831  		// originally so we can turn off size specialized malloc in that case instead
   832  		// using Instrumenting below. Then we can remove this condition.
   833  		return false
   834  	}
   835  
   836  	return buildcfg.Experiment.SizeSpecializedMalloc && !base.Flag.Cfg.Instrumenting
   837  }
   838  
   839  // setHeapaddr allocates a new PAUTO variable to store ptr (which must be non-nil)
   840  // and then sets it as n's heap address.
   841  func (s *state) setHeapaddr(pos src.XPos, n *ir.Name, ptr *ssa.Value) {
   842  	if !ptr.Type.IsPtr() || !types.Identical(n.Type(), ptr.Type.Elem()) {
   843  		base.FatalfAt(n.Pos(), "setHeapaddr %L with type %v", n, ptr.Type)
   844  	}
   845  
   846  	// Declare variable to hold address.
   847  	sym := &types.Sym{Name: "&" + n.Sym().Name, Pkg: types.LocalPkg}
   848  	addr := s.curfn.NewLocal(pos, sym, types.NewPtr(n.Type()))
   849  	addr.SetUsed(true)
   850  	types.CalcSize(addr.Type())
   851  
   852  	if n.Class == ir.PPARAMOUT {
   853  		addr.SetIsOutputParamHeapAddr(true)
   854  	}
   855  
   856  	n.Heapaddr = addr
   857  	s.assign(addr, ptr, false, 0)
   858  }
   859  
   860  // newObject returns an SSA value denoting new(typ).
   861  func (s *state) newObject(typ *types.Type) *ssa.Value {
   862  	if typ.Size() == 0 {
   863  		return s.newValue1A(ssa.OpAddr, types.NewPtr(typ), ir.Syms.Zerobase, s.sb)
   864  	}
   865  	rtype := s.reflectType(typ)
   866  	if specialMallocSym := s.specializedMallocSym(typ.Size(), typ.HasPointers()); specialMallocSym != nil {
   867  		return s.rtcall(specialMallocSym, true, []*types.Type{types.NewPtr(typ)},
   868  			s.constInt(types.Types[types.TUINTPTR], typ.Size()),
   869  			rtype,
   870  			s.constBool(true),
   871  		)[0]
   872  	}
   873  	return s.rtcall(ir.Syms.Newobject, true, []*types.Type{types.NewPtr(typ)}, rtype)[0]
   874  }
   875  
   876  // newObjectNonSpecialized returns an SSA value denoting new(typ). It does
   877  // not produce size-specialized malloc functions.
   878  func (s *state) newObjectNonSpecialized(typ *types.Type, rtype *ssa.Value) *ssa.Value {
   879  	if typ.Size() == 0 {
   880  		return s.newValue1A(ssa.OpAddr, types.NewPtr(typ), ir.Syms.Zerobase, s.sb)
   881  	}
   882  	if rtype == nil {
   883  		rtype = s.reflectType(typ)
   884  	}
   885  	return s.rtcall(ir.Syms.Newobject, true, []*types.Type{types.NewPtr(typ)}, rtype)[0]
   886  }
   887  
   888  func (s *state) checkPtrAlignment(n *ir.ConvExpr, v *ssa.Value, count *ssa.Value) {
   889  	if !n.Type().IsPtr() {
   890  		s.Fatalf("expected pointer type: %v", n.Type())
   891  	}
   892  	elem, rtypeExpr := n.Type().Elem(), n.ElemRType
   893  	if count != nil {
   894  		if !elem.IsArray() {
   895  			s.Fatalf("expected array type: %v", elem)
   896  		}
   897  		elem, rtypeExpr = elem.Elem(), n.ElemElemRType
   898  	}
   899  	size := elem.Size()
   900  	// Casting from larger type to smaller one is ok, so for smallest type, do nothing.
   901  	if elem.Alignment() == 1 && (size == 0 || size == 1 || count == nil) {
   902  		return
   903  	}
   904  	if count == nil {
   905  		count = s.constInt(types.Types[types.TUINTPTR], 1)
   906  	}
   907  	if count.Type.Size() != s.config.PtrSize {
   908  		s.Fatalf("expected count fit to a uintptr size, have: %d, want: %d", count.Type.Size(), s.config.PtrSize)
   909  	}
   910  	var rtype *ssa.Value
   911  	if rtypeExpr != nil {
   912  		rtype = s.expr(rtypeExpr)
   913  	} else {
   914  		rtype = s.reflectType(elem)
   915  	}
   916  	s.rtcall(ir.Syms.CheckPtrAlignment, true, nil, v, rtype, count)
   917  }
   918  
   919  // reflectType returns an SSA value representing a pointer to typ's
   920  // reflection type descriptor.
   921  func (s *state) reflectType(typ *types.Type) *ssa.Value {
   922  	// TODO(mdempsky): Make this Fatalf under Unified IR; frontend needs
   923  	// to supply RType expressions.
   924  	lsym := reflectdata.TypeLinksym(typ)
   925  	return s.entryNewValue1A(ssa.OpAddr, types.NewPtr(types.Types[types.TUINT8]), lsym, s.sb)
   926  }
   927  
   928  func dumpSourcesColumn(writer *ssa.HTMLWriter, fn *ir.Func) {
   929  	// Read sources of target function fn.
   930  	fname := base.Ctxt.PosTable.Pos(fn.Pos()).Filename()
   931  	targetFn, err := readFuncLines(fname, fn.Pos().Line(), fn.Endlineno.Line())
   932  	if err != nil {
   933  		writer.Logf("cannot read sources for function %v: %v", fn, err)
   934  	}
   935  
   936  	// Read sources of inlined functions.
   937  	var inlFns []*ssa.FuncLines
   938  	for _, fi := range ssaDumpInlined {
   939  		elno := fi.Endlineno
   940  		fname := base.Ctxt.PosTable.Pos(fi.Pos()).Filename()
   941  		fnLines, err := readFuncLines(fname, fi.Pos().Line(), elno.Line())
   942  		if err != nil {
   943  			writer.Logf("cannot read sources for inlined function %v: %v", fi, err)
   944  			continue
   945  		}
   946  		inlFns = append(inlFns, fnLines)
   947  	}
   948  
   949  	slices.SortFunc(inlFns, ssa.ByTopoCmp)
   950  	if targetFn != nil {
   951  		inlFns = append([]*ssa.FuncLines{targetFn}, inlFns...)
   952  	}
   953  
   954  	writer.WriteSources("sources", inlFns)
   955  }
   956  
   957  func readFuncLines(file string, start, end uint) (*ssa.FuncLines, error) {
   958  	f, err := os.Open(os.ExpandEnv(file))
   959  	if err != nil {
   960  		return nil, err
   961  	}
   962  	defer f.Close()
   963  	var lines []string
   964  	ln := uint(1)
   965  	scanner := bufio.NewScanner(f)
   966  	for scanner.Scan() && ln <= end {
   967  		if ln >= start {
   968  			lines = append(lines, scanner.Text())
   969  		}
   970  		ln++
   971  	}
   972  	return &ssa.FuncLines{Filename: file, StartLineno: start, Lines: lines}, nil
   973  }
   974  
   975  // updateUnsetPredPos propagates the earliest-value position information for b
   976  // towards all of b's predecessors that need a position, and recurs on that
   977  // predecessor if its position is updated. B should have a non-empty position.
   978  func (s *state) updateUnsetPredPos(b *ssa.Block) {
   979  	if b.Pos == src.NoXPos {
   980  		s.Fatalf("Block %s should have a position", b)
   981  	}
   982  	bestPos := src.NoXPos
   983  	for _, e := range b.Preds {
   984  		p := e.Block()
   985  		if !p.LackingPos() {
   986  			continue
   987  		}
   988  		if bestPos == src.NoXPos {
   989  			bestPos = b.Pos
   990  			for _, v := range b.Values {
   991  				if v.LackingPos() {
   992  					continue
   993  				}
   994  				if v.Pos != src.NoXPos {
   995  					// Assume values are still in roughly textual order;
   996  					// TODO: could also seek minimum position?
   997  					bestPos = v.Pos
   998  					break
   999  				}
  1000  			}
  1001  		}
  1002  		p.Pos = bestPos
  1003  		s.updateUnsetPredPos(p) // We do not expect long chains of these, thus recursion is okay.
  1004  	}
  1005  }
  1006  
  1007  // Information about each open-coded defer.
  1008  type openDeferInfo struct {
  1009  	// The node representing the call of the defer
  1010  	n *ir.CallExpr
  1011  	// If defer call is closure call, the address of the argtmp where the
  1012  	// closure is stored.
  1013  	closure *ssa.Value
  1014  	// The node representing the argtmp where the closure is stored - used for
  1015  	// function, method, or interface call, to store a closure that panic
  1016  	// processing can use for this defer.
  1017  	closureNode *ir.Name
  1018  }
  1019  
  1020  type state struct {
  1021  	// configuration (arch) information
  1022  	config *ssa.Config
  1023  
  1024  	// function we're building
  1025  	f *ssa.Func
  1026  
  1027  	// Node for function
  1028  	curfn *ir.Func
  1029  
  1030  	// labels in f
  1031  	labels map[string]*ssaLabel
  1032  
  1033  	// unlabeled break and continue statement tracking
  1034  	breakTo    *ssa.Block // current target for plain break statement
  1035  	continueTo *ssa.Block // current target for plain continue statement
  1036  
  1037  	// current location where we're interpreting the AST
  1038  	curBlock *ssa.Block
  1039  
  1040  	// variable assignments in the current block (map from variable symbol to ssa value)
  1041  	// *Node is the unique identifier (an ONAME Node) for the variable.
  1042  	// TODO: keep a single varnum map, then make all of these maps slices instead?
  1043  	vars map[ir.Node]*ssa.Value
  1044  
  1045  	// fwdVars are variables that are used before they are defined in the current block.
  1046  	// This map exists just to coalesce multiple references into a single FwdRef op.
  1047  	// *Node is the unique identifier (an ONAME Node) for the variable.
  1048  	fwdVars map[ir.Node]*ssa.Value
  1049  
  1050  	// all defined variables at the end of each block. Indexed by block ID.
  1051  	defvars []map[ir.Node]*ssa.Value
  1052  
  1053  	// addresses of PPARAM and PPARAMOUT variables on the stack.
  1054  	decladdrs map[*ir.Name]*ssa.Value
  1055  
  1056  	// starting values. Memory, stack pointer, and globals pointer
  1057  	startmem *ssa.Value
  1058  	sp       *ssa.Value
  1059  	sb       *ssa.Value
  1060  	// value representing address of where deferBits autotmp is stored
  1061  	deferBitsAddr *ssa.Value
  1062  	deferBitsTemp *ir.Name
  1063  
  1064  	// line number stack. The current line number is top of stack
  1065  	line []src.XPos
  1066  	// the last line number processed; it may have been popped
  1067  	lastPos src.XPos
  1068  
  1069  	// list of panic calls by function name and line number.
  1070  	// Used to deduplicate panic calls.
  1071  	panics map[funcLine]*ssa.Block
  1072  
  1073  	cgoUnsafeArgs       bool
  1074  	hasdefer            bool // whether the function contains a defer statement
  1075  	softFloat           bool
  1076  	hasOpenDefers       bool // whether we are doing open-coded defers
  1077  	checkPtrEnabled     bool // whether to insert checkptr instrumentation
  1078  	instrumentEnterExit bool // whether to instrument function enter/exit
  1079  	instrumentMemory    bool // whether to instrument memory operations
  1080  
  1081  	// If doing open-coded defers, list of info about the defer calls in
  1082  	// scanning order. Hence, at exit we should run these defers in reverse
  1083  	// order of this list
  1084  	openDefers []*openDeferInfo
  1085  	// For open-coded defers, this is the beginning and end blocks of the last
  1086  	// defer exit code that we have generated so far. We use these to share
  1087  	// code between exits if the shareDeferExits option (disabled by default)
  1088  	// is on.
  1089  	lastDeferExit       *ssa.Block // Entry block of last defer exit code we generated
  1090  	lastDeferFinalBlock *ssa.Block // Final block of last defer exit code we generated
  1091  	lastDeferCount      int        // Number of defers encountered at that point
  1092  
  1093  	prevCall *ssa.Value // the previous call; use this to tie results to the call op.
  1094  
  1095  	// List of allocations in the current block that are still pending.
  1096  	// They are all (OffPtr (Select0 (runtime call))) and have the correct types,
  1097  	// but the offsets are not set yet, and the type of the runtime call is also not final.
  1098  	pendingHeapAllocations []*ssa.Value
  1099  
  1100  	// First argument of append calls that could be stack allocated.
  1101  	appendTargets map[ir.Node]bool
  1102  
  1103  	// Block starting position, indexed by block id.
  1104  	blockStarts []src.XPos
  1105  
  1106  	// Information for stack allocation. Indexed by the first argument
  1107  	// to an append call. Normally a slice-typed variable, but not always.
  1108  	backingStores map[ir.Node]*backingStoreInfo
  1109  }
  1110  
  1111  type backingStoreInfo struct {
  1112  	// Size of backing store array (in elements)
  1113  	K int64
  1114  	// Stack-allocated backing store variable.
  1115  	store *ir.Name
  1116  	// Dynamic boolean variable marking the fact that we used this backing store.
  1117  	used *ir.Name
  1118  	// Have we used this variable statically yet? This is just a hint
  1119  	// to avoid checking the dynamic variable if the answer is obvious.
  1120  	// (usedStatic == true implies used == true)
  1121  	usedStatic bool
  1122  }
  1123  
  1124  type funcLine struct {
  1125  	f    *obj.LSym
  1126  	base *src.PosBase
  1127  	line uint
  1128  }
  1129  
  1130  type ssaLabel struct {
  1131  	target         *ssa.Block // block identified by this label
  1132  	breakTarget    *ssa.Block // block to break to in control flow node identified by this label
  1133  	continueTarget *ssa.Block // block to continue to in control flow node identified by this label
  1134  }
  1135  
  1136  // label returns the label associated with sym, creating it if necessary.
  1137  func (s *state) label(sym *types.Sym) *ssaLabel {
  1138  	lab := s.labels[sym.Name]
  1139  	if lab == nil {
  1140  		lab = new(ssaLabel)
  1141  		s.labels[sym.Name] = lab
  1142  	}
  1143  	return lab
  1144  }
  1145  
  1146  func (s *state) Logf(msg string, args ...any) { s.f.Logf(msg, args...) }
  1147  func (s *state) Log() bool                    { return s.f.Log() }
  1148  func (s *state) Fatalf(msg string, args ...any) {
  1149  	s.f.Frontend().Fatalf(s.peekPos(), msg, args...)
  1150  }
  1151  func (s *state) Warnl(pos src.XPos, msg string, args ...any) { s.f.Warnl(pos, msg, args...) }
  1152  func (s *state) Debug_checknil() bool                        { return s.f.Frontend().Debug_checknil() }
  1153  
  1154  func ssaMarker(name string) *ir.Name {
  1155  	return ir.NewNameAt(base.Pos, &types.Sym{Name: name}, nil)
  1156  }
  1157  
  1158  var (
  1159  	// marker node for the memory variable
  1160  	memVar = ssaMarker("mem")
  1161  
  1162  	// marker nodes for temporary variables
  1163  	ptrVar       = ssaMarker("ptr")
  1164  	lenVar       = ssaMarker("len")
  1165  	capVar       = ssaMarker("cap")
  1166  	typVar       = ssaMarker("typ")
  1167  	okVar        = ssaMarker("ok")
  1168  	deferBitsVar = ssaMarker("deferBits")
  1169  	hashVar      = ssaMarker("hash")
  1170  )
  1171  
  1172  // startBlock sets the current block we're generating code in to b.
  1173  func (s *state) startBlock(b *ssa.Block) {
  1174  	if s.curBlock != nil {
  1175  		s.Fatalf("starting block %v when block %v has not ended", b, s.curBlock)
  1176  	}
  1177  	s.curBlock = b
  1178  	s.vars = map[ir.Node]*ssa.Value{}
  1179  	clear(s.fwdVars)
  1180  	for len(s.blockStarts) <= int(b.ID) {
  1181  		s.blockStarts = append(s.blockStarts, src.NoXPos)
  1182  	}
  1183  }
  1184  
  1185  // endBlock marks the end of generating code for the current block.
  1186  // Returns the (former) current block. Returns nil if there is no current
  1187  // block, i.e. if no code flows to the current execution point.
  1188  func (s *state) endBlock() *ssa.Block {
  1189  	b := s.curBlock
  1190  	if b == nil {
  1191  		return nil
  1192  	}
  1193  
  1194  	s.flushPendingHeapAllocations()
  1195  
  1196  	for len(s.defvars) <= int(b.ID) {
  1197  		s.defvars = append(s.defvars, nil)
  1198  	}
  1199  	s.defvars[b.ID] = s.vars
  1200  	s.curBlock = nil
  1201  	s.vars = nil
  1202  	if b.LackingPos() {
  1203  		// Empty plain blocks get the line of their successor (handled after all blocks created),
  1204  		// except for increment blocks in For statements (handled in ssa conversion of OFOR),
  1205  		// and for blocks ending in GOTO/BREAK/CONTINUE.
  1206  		b.Pos = src.NoXPos
  1207  	} else {
  1208  		b.Pos = s.lastPos
  1209  		if s.blockStarts[b.ID] == src.NoXPos {
  1210  			s.blockStarts[b.ID] = s.lastPos
  1211  		}
  1212  	}
  1213  	return b
  1214  }
  1215  
  1216  // pushLine pushes a line number on the line number stack.
  1217  func (s *state) pushLine(line src.XPos) {
  1218  	if !line.IsKnown() {
  1219  		// the frontend may emit node with line number missing,
  1220  		// use the parent line number in this case.
  1221  		line = s.peekPos()
  1222  		if base.Flag.K != 0 {
  1223  			base.Warn("buildssa: unknown position (line 0)")
  1224  		}
  1225  	} else {
  1226  		s.lastPos = line
  1227  	}
  1228  	// The first position we see for a new block is its starting position
  1229  	// (the line number for its phis, if any).
  1230  	if b := s.curBlock; b != nil && s.blockStarts[b.ID] == src.NoXPos {
  1231  		s.blockStarts[b.ID] = line
  1232  	}
  1233  
  1234  	s.line = append(s.line, line)
  1235  }
  1236  
  1237  // popLine pops the top of the line number stack.
  1238  func (s *state) popLine() {
  1239  	s.line = s.line[:len(s.line)-1]
  1240  }
  1241  
  1242  // peekPos peeks the top of the line number stack.
  1243  func (s *state) peekPos() src.XPos {
  1244  	return s.line[len(s.line)-1]
  1245  }
  1246  
  1247  // newValue0 adds a new value with no arguments to the current block.
  1248  func (s *state) newValue0(op ssa.Op, t *types.Type) *ssa.Value {
  1249  	return s.curBlock.NewValue0(s.peekPos(), op, t)
  1250  }
  1251  
  1252  // newValue0A adds a new value with no arguments and an aux value to the current block.
  1253  func (s *state) newValue0A(op ssa.Op, t *types.Type, aux ssa.Aux) *ssa.Value {
  1254  	return s.curBlock.NewValue0A(s.peekPos(), op, t, aux)
  1255  }
  1256  
  1257  // newValue0I adds a new value with no arguments and an auxint value to the current block.
  1258  func (s *state) newValue0I(op ssa.Op, t *types.Type, auxint int64) *ssa.Value {
  1259  	return s.curBlock.NewValue0I(s.peekPos(), op, t, auxint)
  1260  }
  1261  
  1262  // newValue1 adds a new value with one argument to the current block.
  1263  func (s *state) newValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
  1264  	return s.curBlock.NewValue1(s.peekPos(), op, t, arg)
  1265  }
  1266  
  1267  // newValue1A adds a new value with one argument and an aux value to the current block.
  1268  func (s *state) newValue1A(op ssa.Op, t *types.Type, aux ssa.Aux, arg *ssa.Value) *ssa.Value {
  1269  	return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg)
  1270  }
  1271  
  1272  // newValue1Apos adds a new value with one argument and an aux value to the current block.
  1273  // isStmt determines whether the created values may be a statement or not
  1274  // (i.e., false means never, yes means maybe).
  1275  func (s *state) newValue1Apos(op ssa.Op, t *types.Type, aux ssa.Aux, arg *ssa.Value, isStmt bool) *ssa.Value {
  1276  	if isStmt {
  1277  		return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg)
  1278  	}
  1279  	return s.curBlock.NewValue1A(s.peekPos().WithNotStmt(), op, t, aux, arg)
  1280  }
  1281  
  1282  // newValue1I adds a new value with one argument and an auxint value to the current block.
  1283  func (s *state) newValue1I(op ssa.Op, t *types.Type, aux int64, arg *ssa.Value) *ssa.Value {
  1284  	return s.curBlock.NewValue1I(s.peekPos(), op, t, aux, arg)
  1285  }
  1286  
  1287  // newValue2 adds a new value with two arguments to the current block.
  1288  func (s *state) newValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
  1289  	return s.curBlock.NewValue2(s.peekPos(), op, t, arg0, arg1)
  1290  }
  1291  
  1292  // newValue2A adds a new value with two arguments and an aux value to the current block.
  1293  func (s *state) newValue2A(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1 *ssa.Value) *ssa.Value {
  1294  	return s.curBlock.NewValue2A(s.peekPos(), op, t, aux, arg0, arg1)
  1295  }
  1296  
  1297  // newValue2Apos adds a new value with two arguments and an aux value to the current block.
  1298  // isStmt determines whether the created values may be a statement or not
  1299  // (i.e., false means never, yes means maybe).
  1300  func (s *state) newValue2Apos(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1 *ssa.Value, isStmt bool) *ssa.Value {
  1301  	if isStmt {
  1302  		return s.curBlock.NewValue2A(s.peekPos(), op, t, aux, arg0, arg1)
  1303  	}
  1304  	return s.curBlock.NewValue2A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1)
  1305  }
  1306  
  1307  // newValue2I adds a new value with two arguments and an auxint value to the current block.
  1308  func (s *state) newValue2I(op ssa.Op, t *types.Type, aux int64, arg0, arg1 *ssa.Value) *ssa.Value {
  1309  	return s.curBlock.NewValue2I(s.peekPos(), op, t, aux, arg0, arg1)
  1310  }
  1311  
  1312  // newValue3 adds a new value with three arguments to the current block.
  1313  func (s *state) newValue3(op ssa.Op, t *types.Type, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
  1314  	return s.curBlock.NewValue3(s.peekPos(), op, t, arg0, arg1, arg2)
  1315  }
  1316  
  1317  // newValue3I adds a new value with three arguments and an auxint value to the current block.
  1318  func (s *state) newValue3I(op ssa.Op, t *types.Type, aux int64, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
  1319  	return s.curBlock.NewValue3I(s.peekPos(), op, t, aux, arg0, arg1, arg2)
  1320  }
  1321  
  1322  // newValue3A adds a new value with three arguments and an aux value to the current block.
  1323  func (s *state) newValue3A(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
  1324  	return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2)
  1325  }
  1326  
  1327  // newValue3Apos adds a new value with three arguments and an aux value to the current block.
  1328  // isStmt determines whether the created values may be a statement or not
  1329  // (i.e., false means never, yes means maybe).
  1330  func (s *state) newValue3Apos(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1, arg2 *ssa.Value, isStmt bool) *ssa.Value {
  1331  	if isStmt {
  1332  		return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2)
  1333  	}
  1334  	return s.curBlock.NewValue3A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1, arg2)
  1335  }
  1336  
  1337  // newValue4 adds a new value with four arguments to the current block.
  1338  func (s *state) newValue4(op ssa.Op, t *types.Type, arg0, arg1, arg2, arg3 *ssa.Value) *ssa.Value {
  1339  	return s.curBlock.NewValue4(s.peekPos(), op, t, arg0, arg1, arg2, arg3)
  1340  }
  1341  
  1342  // newValue4A adds a new value with four arguments and an aux value to the current block.
  1343  func (s *state) newValue4A(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1, arg2, arg3 *ssa.Value) *ssa.Value {
  1344  	return s.curBlock.NewValue4A(s.peekPos(), op, t, aux, arg0, arg1, arg2, arg3)
  1345  }
  1346  
  1347  // newValue4I adds a new value with four arguments and an auxint value to the current block.
  1348  func (s *state) newValue4I(op ssa.Op, t *types.Type, aux int64, arg0, arg1, arg2, arg3 *ssa.Value) *ssa.Value {
  1349  	return s.curBlock.NewValue4I(s.peekPos(), op, t, aux, arg0, arg1, arg2, arg3)
  1350  }
  1351  
  1352  func (s *state) entryBlock() *ssa.Block {
  1353  	b := s.f.Entry
  1354  	if base.Flag.N > 0 && s.curBlock != nil {
  1355  		// If optimizations are off, allocate in current block instead. Since with -N
  1356  		// we're not doing the CSE or tighten passes, putting lots of stuff in the
  1357  		// entry block leads to O(n^2) entries in the live value map during regalloc.
  1358  		// See issue 45897.
  1359  		b = s.curBlock
  1360  	}
  1361  	return b
  1362  }
  1363  
  1364  // entryNewValue0 adds a new value with no arguments to the entry block.
  1365  func (s *state) entryNewValue0(op ssa.Op, t *types.Type) *ssa.Value {
  1366  	return s.entryBlock().NewValue0(src.NoXPos, op, t)
  1367  }
  1368  
  1369  // entryNewValue0A adds a new value with no arguments and an aux value to the entry block.
  1370  func (s *state) entryNewValue0A(op ssa.Op, t *types.Type, aux ssa.Aux) *ssa.Value {
  1371  	return s.entryBlock().NewValue0A(src.NoXPos, op, t, aux)
  1372  }
  1373  
  1374  // entryNewValue1 adds a new value with one argument to the entry block.
  1375  func (s *state) entryNewValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
  1376  	return s.entryBlock().NewValue1(src.NoXPos, op, t, arg)
  1377  }
  1378  
  1379  // entryNewValue1I adds a new value with one argument and an auxint value to the entry block.
  1380  func (s *state) entryNewValue1I(op ssa.Op, t *types.Type, auxint int64, arg *ssa.Value) *ssa.Value {
  1381  	return s.entryBlock().NewValue1I(src.NoXPos, op, t, auxint, arg)
  1382  }
  1383  
  1384  // entryNewValue1A adds a new value with one argument and an aux value to the entry block.
  1385  func (s *state) entryNewValue1A(op ssa.Op, t *types.Type, aux ssa.Aux, arg *ssa.Value) *ssa.Value {
  1386  	return s.entryBlock().NewValue1A(src.NoXPos, op, t, aux, arg)
  1387  }
  1388  
  1389  // entryNewValue2 adds a new value with two arguments to the entry block.
  1390  func (s *state) entryNewValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
  1391  	return s.entryBlock().NewValue2(src.NoXPos, op, t, arg0, arg1)
  1392  }
  1393  
  1394  // entryNewValue2A adds a new value with two arguments and an aux value to the entry block.
  1395  func (s *state) entryNewValue2A(op ssa.Op, t *types.Type, aux ssa.Aux, arg0, arg1 *ssa.Value) *ssa.Value {
  1396  	return s.entryBlock().NewValue2A(src.NoXPos, op, t, aux, arg0, arg1)
  1397  }
  1398  
  1399  // const* routines add a new const value to the entry block.
  1400  func (s *state) constSlice(t *types.Type) *ssa.Value {
  1401  	return s.f.ConstSlice(t)
  1402  }
  1403  func (s *state) constInterface(t *types.Type) *ssa.Value {
  1404  	return s.f.ConstInterface(t)
  1405  }
  1406  func (s *state) constNil(t *types.Type) *ssa.Value { return s.f.ConstNil(t) }
  1407  func (s *state) constEmptyString(t *types.Type) *ssa.Value {
  1408  	return s.f.ConstEmptyString(t)
  1409  }
  1410  func (s *state) constBool(c bool) *ssa.Value {
  1411  	return s.f.ConstBool(types.Types[types.TBOOL], c)
  1412  }
  1413  func (s *state) constInt8(t *types.Type, c int8) *ssa.Value {
  1414  	return s.f.ConstInt8(t, c)
  1415  }
  1416  func (s *state) constInt16(t *types.Type, c int16) *ssa.Value {
  1417  	return s.f.ConstInt16(t, c)
  1418  }
  1419  func (s *state) constInt32(t *types.Type, c int32) *ssa.Value {
  1420  	return s.f.ConstInt32(t, c)
  1421  }
  1422  func (s *state) constInt64(t *types.Type, c int64) *ssa.Value {
  1423  	return s.f.ConstInt64(t, c)
  1424  }
  1425  func (s *state) constFloat32(t *types.Type, c float64) *ssa.Value {
  1426  	return s.f.ConstFloat32(t, c)
  1427  }
  1428  func (s *state) constFloat64(t *types.Type, c float64) *ssa.Value {
  1429  	return s.f.ConstFloat64(t, c)
  1430  }
  1431  func (s *state) constInt(t *types.Type, c int64) *ssa.Value {
  1432  	if s.config.PtrSize == 8 {
  1433  		return s.constInt64(t, c)
  1434  	}
  1435  	if int64(int32(c)) != c {
  1436  		s.Fatalf("integer constant too big %d", c)
  1437  	}
  1438  	return s.constInt32(t, int32(c))
  1439  }
  1440  
  1441  // newValueOrSfCall* are wrappers around newValue*, which may create a call to a
  1442  // soft-float runtime function instead (when emitting soft-float code).
  1443  func (s *state) newValueOrSfCall1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value {
  1444  	if s.softFloat {
  1445  		if c, ok := s.sfcall(op, arg); ok {
  1446  			return c
  1447  		}
  1448  	}
  1449  	return s.newValue1(op, t, arg)
  1450  }
  1451  func (s *state) newValueOrSfCall2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value {
  1452  	if s.softFloat {
  1453  		if c, ok := s.sfcall(op, arg0, arg1); ok {
  1454  			return c
  1455  		}
  1456  	}
  1457  	return s.newValue2(op, t, arg0, arg1)
  1458  }
  1459  
  1460  type instrumentKind uint8
  1461  
  1462  const (
  1463  	instrumentRead = iota
  1464  	instrumentWrite
  1465  	instrumentMove
  1466  )
  1467  
  1468  func (s *state) instrument(t *types.Type, addr *ssa.Value, kind instrumentKind) {
  1469  	s.instrument2(t, addr, nil, kind)
  1470  }
  1471  
  1472  // instrumentFields instruments a read/write operation on addr.
  1473  // If it is instrumenting for MSAN or ASAN and t is a struct type, it instruments
  1474  // operation for each field, instead of for the whole struct.
  1475  func (s *state) instrumentFields(t *types.Type, addr *ssa.Value, kind instrumentKind) {
  1476  	if !(base.Flag.MSan || base.Flag.ASan) || !isStructNotSIMD(t) {
  1477  		s.instrument(t, addr, kind)
  1478  		return
  1479  	}
  1480  	for _, f := range t.Fields() {
  1481  		if f.Sym.IsBlank() {
  1482  			continue
  1483  		}
  1484  		offptr := s.newValue1I(ssa.OpOffPtr, types.NewPtr(f.Type), f.Offset, addr)
  1485  		s.instrumentFields(f.Type, offptr, kind)
  1486  	}
  1487  }
  1488  
  1489  func (s *state) instrumentMove(t *types.Type, dst, src *ssa.Value) {
  1490  	if base.Flag.MSan {
  1491  		s.instrument2(t, dst, src, instrumentMove)
  1492  	} else {
  1493  		s.instrument(t, src, instrumentRead)
  1494  		s.instrument(t, dst, instrumentWrite)
  1495  	}
  1496  }
  1497  
  1498  func (s *state) instrument2(t *types.Type, addr, addr2 *ssa.Value, kind instrumentKind) {
  1499  	if !s.instrumentMemory {
  1500  		return
  1501  	}
  1502  
  1503  	w := t.Size()
  1504  	if w == 0 {
  1505  		return // can't race on zero-sized things
  1506  	}
  1507  
  1508  	if ssa.IsSanitizerSafeAddr(addr) {
  1509  		return
  1510  	}
  1511  
  1512  	var fn *obj.LSym
  1513  	needWidth := false
  1514  
  1515  	if addr2 != nil && kind != instrumentMove {
  1516  		panic("instrument2: non-nil addr2 for non-move instrumentation")
  1517  	}
  1518  
  1519  	if base.Flag.MSan {
  1520  		switch kind {
  1521  		case instrumentRead:
  1522  			fn = ir.Syms.Msanread
  1523  		case instrumentWrite:
  1524  			fn = ir.Syms.Msanwrite
  1525  		case instrumentMove:
  1526  			fn = ir.Syms.Msanmove
  1527  		default:
  1528  			panic("unreachable")
  1529  		}
  1530  		needWidth = true
  1531  	} else if base.Flag.Race && t.NumComponents(types.CountBlankFields) > 1 {
  1532  		// for composite objects we have to write every address
  1533  		// because a write might happen to any subobject.
  1534  		// composites with only one element don't have subobjects, though.
  1535  		switch kind {
  1536  		case instrumentRead:
  1537  			fn = ir.Syms.Racereadrange
  1538  		case instrumentWrite:
  1539  			fn = ir.Syms.Racewriterange
  1540  		default:
  1541  			panic("unreachable")
  1542  		}
  1543  		needWidth = true
  1544  	} else if base.Flag.Race {
  1545  		// for non-composite objects we can write just the start
  1546  		// address, as any write must write the first byte.
  1547  		switch kind {
  1548  		case instrumentRead:
  1549  			fn = ir.Syms.Raceread
  1550  		case instrumentWrite:
  1551  			fn = ir.Syms.Racewrite
  1552  		default:
  1553  			panic("unreachable")
  1554  		}
  1555  	} else if base.Flag.ASan {
  1556  		switch kind {
  1557  		case instrumentRead:
  1558  			fn = ir.Syms.Asanread
  1559  		case instrumentWrite:
  1560  			fn = ir.Syms.Asanwrite
  1561  		default:
  1562  			panic("unreachable")
  1563  		}
  1564  		needWidth = true
  1565  	} else {
  1566  		panic("unreachable")
  1567  	}
  1568  
  1569  	args := []*ssa.Value{addr}
  1570  	if addr2 != nil {
  1571  		args = append(args, addr2)
  1572  	}
  1573  	if needWidth {
  1574  		args = append(args, s.constInt(types.Types[types.TUINTPTR], w))
  1575  	}
  1576  	s.rtcall(fn, true, nil, args...)
  1577  }
  1578  
  1579  func (s *state) load(t *types.Type, src *ssa.Value) *ssa.Value {
  1580  	s.instrumentFields(t, src, instrumentRead)
  1581  	return s.rawLoad(t, src)
  1582  }
  1583  
  1584  func (s *state) rawLoad(t *types.Type, src *ssa.Value) *ssa.Value {
  1585  	return s.newValue2(ssa.OpLoad, t, src, s.mem())
  1586  }
  1587  
  1588  func (s *state) store(t *types.Type, dst, val *ssa.Value) {
  1589  	s.vars[memVar] = s.newValue3A(ssa.OpStore, types.TypeMem, t, dst, val, s.mem())
  1590  }
  1591  
  1592  func (s *state) zero(t *types.Type, dst *ssa.Value) {
  1593  	s.instrument(t, dst, instrumentWrite)
  1594  	store := s.newValue2I(ssa.OpZero, types.TypeMem, t.Size(), dst, s.mem())
  1595  	store.Aux = t
  1596  	s.vars[memVar] = store
  1597  }
  1598  
  1599  func (s *state) move(t *types.Type, dst, src *ssa.Value) {
  1600  	s.moveWhichMayOverlap(t, dst, src, false)
  1601  }
  1602  func (s *state) moveWhichMayOverlap(t *types.Type, dst, src *ssa.Value, mayOverlap bool) {
  1603  	s.instrumentMove(t, dst, src)
  1604  	if mayOverlap && t.IsArray() && t.NumElem() > 1 && !ssa.IsInlinableMemmove(dst, src, t.Size(), s.f.Config) {
  1605  		// Normally, when moving Go values of type T from one location to another,
  1606  		// we don't need to worry about partial overlaps. The two Ts must either be
  1607  		// in disjoint (nonoverlapping) memory or in exactly the same location.
  1608  		// There are 2 cases where this isn't true:
  1609  		//  1) Using unsafe you can arrange partial overlaps.
  1610  		//  2) Since Go 1.17, you can use a cast from a slice to a ptr-to-array.
  1611  		//     https://go.dev/ref/spec#Conversions_from_slice_to_array_pointer
  1612  		//     This feature can be used to construct partial overlaps of array types.
  1613  		//       var a [3]int
  1614  		//       p := (*[2]int)(a[:])
  1615  		//       q := (*[2]int)(a[1:])
  1616  		//       *p = *q
  1617  		// We don't care about solving 1. Or at least, we haven't historically
  1618  		// and no one has complained.
  1619  		// For 2, we need to ensure that if there might be partial overlap,
  1620  		// then we can't use OpMove; we must use memmove instead.
  1621  		// (memmove handles partial overlap by copying in the correct
  1622  		// direction. OpMove does not.)
  1623  		//
  1624  		// Note that we have to be careful here not to introduce a call when
  1625  		// we're marshaling arguments to a call or unmarshaling results from a call.
  1626  		// Cases where this is happening must pass mayOverlap to false.
  1627  		// (Currently this only happens when unmarshaling results of a call.)
  1628  		if t.HasPointers() {
  1629  			s.rtcall(ir.Syms.Typedmemmove, true, nil, s.reflectType(t), dst, src)
  1630  			// We would have otherwise implemented this move with straightline code,
  1631  			// including a write barrier. Pretend we issue a write barrier here,
  1632  			// so that the write barrier tests work. (Otherwise they'd need to know
  1633  			// the details of IsInlineableMemmove.)
  1634  			s.curfn.SetWBPos(s.peekPos())
  1635  		} else {
  1636  			s.rtcall(ir.Syms.Memmove, true, nil, dst, src, s.constInt(types.Types[types.TUINTPTR], t.Size()))
  1637  		}
  1638  		ssa.LogLargeCopy(s.f.Name, s.peekPos(), t.Size())
  1639  		return
  1640  	}
  1641  	store := s.newValue3I(ssa.OpMove, types.TypeMem, t.Size(), dst, src, s.mem())
  1642  	store.Aux = t
  1643  	s.vars[memVar] = store
  1644  }
  1645  
  1646  // stmtList converts the statement list n to SSA and adds it to s.
  1647  func (s *state) stmtList(l ir.Nodes) {
  1648  	for _, n := range l {
  1649  		s.stmt(n)
  1650  	}
  1651  }
  1652  
  1653  func peelConvNop(n ir.Node) ir.Node {
  1654  	if n == nil {
  1655  		return n
  1656  	}
  1657  	for n.Op() == ir.OCONVNOP {
  1658  		n = n.(*ir.ConvExpr).X
  1659  	}
  1660  	return n
  1661  }
  1662  
  1663  // stmt converts the statement n to SSA and adds it to s.
  1664  func (s *state) stmt(n ir.Node) {
  1665  	s.pushLine(n.Pos())
  1666  	defer s.popLine()
  1667  
  1668  	// If s.curBlock is nil, and n isn't a label (which might have an associated goto somewhere),
  1669  	// then this code is dead. Stop here.
  1670  	if s.curBlock == nil && n.Op() != ir.OLABEL {
  1671  		return
  1672  	}
  1673  
  1674  	s.stmtList(n.Init())
  1675  	switch n.Op() {
  1676  
  1677  	case ir.OBLOCK:
  1678  		n := n.(*ir.BlockStmt)
  1679  		s.stmtList(n.List)
  1680  
  1681  	case ir.OFALL: // no-op
  1682  
  1683  	// Expression statements
  1684  	case ir.OCALLFUNC:
  1685  		n := n.(*ir.CallExpr)
  1686  		if ir.IsIntrinsicCall(n) {
  1687  			s.intrinsicCall(n)
  1688  			return
  1689  		}
  1690  		fallthrough
  1691  
  1692  	case ir.OCALLINTER:
  1693  		n := n.(*ir.CallExpr)
  1694  		s.callResult(n, callNormal)
  1695  		if n.Op() == ir.OCALLFUNC && n.Fun.Op() == ir.ONAME && n.Fun.(*ir.Name).Class == ir.PFUNC {
  1696  			if fn := n.Fun.Sym().Name; base.Flag.CompilingRuntime && fn == "throw" ||
  1697  				n.Fun.Sym().Pkg == ir.Pkgs.Runtime &&
  1698  					(fn == "throwinit" || fn == "gopanic" || fn == "panicwrap" || fn == "block" ||
  1699  						fn == "panicmakeslicelen" || fn == "panicmakeslicecap" || fn == "panicunsafeslicelen" ||
  1700  						fn == "panicunsafeslicenilptr" || fn == "panicunsafestringlen" || fn == "panicunsafestringnilptr" ||
  1701  						fn == "panicrangestate") {
  1702  				m := s.mem()
  1703  				b := s.endBlock()
  1704  				b.Kind = ssa.BlockExit
  1705  				b.SetControl(m)
  1706  				// TODO: never rewrite OPANIC to OCALLFUNC in the
  1707  				// first place. Need to wait until all backends
  1708  				// go through SSA.
  1709  			}
  1710  		}
  1711  	case ir.ODEFER:
  1712  		n := n.(*ir.GoDeferStmt)
  1713  		if base.Debug.Defer > 0 {
  1714  			var defertype string
  1715  			if s.hasOpenDefers {
  1716  				defertype = "open-coded"
  1717  			} else if n.Esc() == ir.EscNever {
  1718  				defertype = "stack-allocated"
  1719  			} else {
  1720  				defertype = "heap-allocated"
  1721  			}
  1722  			base.WarnfAt(n.Pos(), "%s defer", defertype)
  1723  		}
  1724  		if s.hasOpenDefers {
  1725  			s.openDeferRecord(n.Call.(*ir.CallExpr))
  1726  		} else {
  1727  			d := callDefer
  1728  			if n.Esc() == ir.EscNever && n.DeferAt == nil {
  1729  				d = callDeferStack
  1730  			}
  1731  			s.call(n.Call.(*ir.CallExpr), d, false, n.DeferAt)
  1732  		}
  1733  	case ir.OGO:
  1734  		n := n.(*ir.GoDeferStmt)
  1735  		s.callResult(n.Call.(*ir.CallExpr), callGo)
  1736  
  1737  	case ir.OAS2DOTTYPE:
  1738  		n := n.(*ir.AssignListStmt)
  1739  		var res, resok *ssa.Value
  1740  		if n.Rhs[0].Op() == ir.ODOTTYPE2 {
  1741  			res, resok = s.dottype(n.Rhs[0].(*ir.TypeAssertExpr), true)
  1742  		} else {
  1743  			res, resok = s.dynamicDottype(n.Rhs[0].(*ir.DynamicTypeAssertExpr), true)
  1744  		}
  1745  		deref := false
  1746  		if !ssa.CanSSA(n.Rhs[0].Type()) {
  1747  			if res.Op != ssa.OpLoad {
  1748  				s.Fatalf("dottype of non-load")
  1749  			}
  1750  			mem := s.mem()
  1751  			if res.Args[1] != mem {
  1752  				s.Fatalf("memory no longer live from 2-result dottype load")
  1753  			}
  1754  			deref = true
  1755  			res = res.Args[0]
  1756  		}
  1757  		s.assign(n.Lhs[0], res, deref, 0)
  1758  		s.assign(n.Lhs[1], resok, false, 0)
  1759  		return
  1760  
  1761  	case ir.OAS2FUNC:
  1762  		// We come here only when it is an intrinsic call returning two values.
  1763  		n := n.(*ir.AssignListStmt)
  1764  		call := n.Rhs[0].(*ir.CallExpr)
  1765  		if !ir.IsIntrinsicCall(call) {
  1766  			s.Fatalf("non-intrinsic AS2FUNC not expanded %v", call)
  1767  		}
  1768  		v := s.intrinsicCall(call)
  1769  		v1 := s.newValue1(ssa.OpSelect0, n.Lhs[0].Type(), v)
  1770  		v2 := s.newValue1(ssa.OpSelect1, n.Lhs[1].Type(), v)
  1771  		s.assign(n.Lhs[0], v1, false, 0)
  1772  		s.assign(n.Lhs[1], v2, false, 0)
  1773  		return
  1774  
  1775  	case ir.ODCL:
  1776  		n := n.(*ir.Decl)
  1777  		if v := n.X; v.Esc() == ir.EscHeap {
  1778  			s.newHeapaddr(v)
  1779  		}
  1780  
  1781  	case ir.OLABEL:
  1782  		n := n.(*ir.LabelStmt)
  1783  		sym := n.Label
  1784  		if sym.IsBlank() {
  1785  			// Nothing to do because the label isn't targetable. See issue 52278.
  1786  			break
  1787  		}
  1788  		lab := s.label(sym)
  1789  
  1790  		// The label might already have a target block via a goto.
  1791  		if lab.target == nil {
  1792  			lab.target = s.f.NewBlock(ssa.BlockPlain)
  1793  		}
  1794  
  1795  		// Go to that label.
  1796  		// (We pretend "label:" is preceded by "goto label", unless the predecessor is unreachable.)
  1797  		if s.curBlock != nil {
  1798  			b := s.endBlock()
  1799  			b.AddEdgeTo(lab.target)
  1800  		}
  1801  		s.startBlock(lab.target)
  1802  
  1803  	case ir.OGOTO:
  1804  		n := n.(*ir.BranchStmt)
  1805  		sym := n.Label
  1806  
  1807  		lab := s.label(sym)
  1808  		if lab.target == nil {
  1809  			lab.target = s.f.NewBlock(ssa.BlockPlain)
  1810  		}
  1811  
  1812  		b := s.endBlock()
  1813  		b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block.
  1814  		b.AddEdgeTo(lab.target)
  1815  
  1816  	case ir.OAS:
  1817  		n := n.(*ir.AssignStmt)
  1818  		if n.X == n.Y && n.X.Op() == ir.ONAME {
  1819  			// An x=x assignment. No point in doing anything
  1820  			// here. In addition, skipping this assignment
  1821  			// prevents generating:
  1822  			//   VARDEF x
  1823  			//   COPY x -> x
  1824  			// which is bad because x is incorrectly considered
  1825  			// dead before the vardef. See issue #14904.
  1826  			return
  1827  		}
  1828  
  1829  		// mayOverlap keeps track of whether the LHS and RHS might
  1830  		// refer to partially overlapping memory. Partial overlapping can
  1831  		// only happen for arrays, see the comment in moveWhichMayOverlap.
  1832  		//
  1833  		// If both sides of the assignment are not dereferences, then partial
  1834  		// overlap can't happen. Partial overlap can only occur only when the
  1835  		// arrays referenced are strictly smaller parts of the same base array.
  1836  		// If one side of the assignment is a full array, then partial overlap
  1837  		// can't happen. (The arrays are either disjoint or identical.)
  1838  		ny := peelConvNop(n.Y)
  1839  		mayOverlap := n.X.Op() == ir.ODEREF && (n.Y != nil && ny.Op() == ir.ODEREF)
  1840  		if ny != nil && ny.Op() == ir.ODEREF {
  1841  			p := peelConvNop(ny.(*ir.StarExpr).X)
  1842  			if p.Op() == ir.OSPTR && p.(*ir.UnaryExpr).X.Type().IsString() {
  1843  				// Pointer fields of strings point to unmodifiable memory.
  1844  				// That memory can't overlap with the memory being written.
  1845  				mayOverlap = false
  1846  			}
  1847  		}
  1848  
  1849  		// Evaluate RHS.
  1850  		rhs := n.Y
  1851  		if rhs != nil {
  1852  			switch rhs.Op() {
  1853  			case ir.OSTRUCTLIT, ir.OARRAYLIT, ir.OSLICELIT:
  1854  				// All literals with nonzero fields have already been
  1855  				// rewritten during walk. Any that remain are just T{}
  1856  				// or equivalents. Use the zero value.
  1857  				if !ir.IsZero(rhs) {
  1858  					s.Fatalf("literal with nonzero value in SSA: %v", rhs)
  1859  				}
  1860  				rhs = nil
  1861  			case ir.OAPPEND:
  1862  				rhs := rhs.(*ir.CallExpr)
  1863  				// Check whether we're writing the result of an append back to the same slice.
  1864  				// If so, we handle it specially to avoid write barriers on the fast
  1865  				// (non-growth) path.
  1866  				if !ir.SameSafeExpr(n.X, rhs.Args[0]) || base.Flag.N != 0 {
  1867  					break
  1868  				}
  1869  				// If the slice can be SSA'd, it'll be on the stack,
  1870  				// so there will be no write barriers,
  1871  				// so there's no need to attempt to prevent them.
  1872  				if s.canSSA(n.X) {
  1873  					if base.Debug.Append > 0 { // replicating old diagnostic message
  1874  						base.WarnfAt(n.Pos(), "append: len-only update (in local slice)")
  1875  					}
  1876  					break
  1877  				}
  1878  				if base.Debug.Append > 0 {
  1879  					base.WarnfAt(n.Pos(), "append: len-only update")
  1880  				}
  1881  				s.append(rhs, true)
  1882  				return
  1883  			}
  1884  		}
  1885  
  1886  		if ir.IsBlank(n.X) {
  1887  			// _ = rhs
  1888  			// Just evaluate rhs for side-effects.
  1889  			if rhs != nil {
  1890  				s.expr(rhs)
  1891  			}
  1892  			return
  1893  		}
  1894  
  1895  		var t *types.Type
  1896  		if n.Y != nil {
  1897  			t = n.Y.Type()
  1898  		} else {
  1899  			t = n.X.Type()
  1900  		}
  1901  
  1902  		var r *ssa.Value
  1903  		deref := !ssa.CanSSA(t)
  1904  		if deref {
  1905  			if rhs == nil {
  1906  				r = nil // Signal assign to use OpZero.
  1907  			} else {
  1908  				r = s.addr(rhs)
  1909  			}
  1910  		} else {
  1911  			if rhs == nil {
  1912  				r = s.zeroVal(t)
  1913  			} else {
  1914  				r = s.expr(rhs)
  1915  			}
  1916  		}
  1917  
  1918  		var skip skipMask
  1919  		if rhs != nil && (rhs.Op() == ir.OSLICE || rhs.Op() == ir.OSLICE3 || rhs.Op() == ir.OSLICESTR) && ir.SameSafeExpr(rhs.(*ir.SliceExpr).X, n.X) {
  1920  			// We're assigning a slicing operation back to its source.
  1921  			// Don't write back fields we aren't changing. See issue #14855.
  1922  			rhs := rhs.(*ir.SliceExpr)
  1923  			i, j, k := rhs.Low, rhs.High, rhs.Max
  1924  			if i != nil && (i.Op() == ir.OLITERAL && i.Val().Kind() == constant.Int && ir.Int64Val(i) == 0) {
  1925  				// [0:...] is the same as [:...]
  1926  				i = nil
  1927  			}
  1928  			// TODO: detect defaults for len/cap also.
  1929  			// Currently doesn't really work because (*p)[:len(*p)] appears here as:
  1930  			//    tmp = len(*p)
  1931  			//    (*p)[:tmp]
  1932  			// if j != nil && (j.Op == OLEN && SameSafeExpr(j.Left, n.Left)) {
  1933  			//      j = nil
  1934  			// }
  1935  			// if k != nil && (k.Op == OCAP && SameSafeExpr(k.Left, n.Left)) {
  1936  			//      k = nil
  1937  			// }
  1938  			if i == nil {
  1939  				skip |= skipPtr
  1940  				if j == nil {
  1941  					skip |= skipLen
  1942  				}
  1943  				if k == nil {
  1944  					skip |= skipCap
  1945  				}
  1946  			}
  1947  		}
  1948  
  1949  		s.assignWhichMayOverlap(n.X, r, deref, skip, mayOverlap)
  1950  
  1951  	case ir.OIF:
  1952  		n := n.(*ir.IfStmt)
  1953  		if ir.IsConst(n.Cond, constant.Bool) {
  1954  			s.stmtList(n.Cond.Init())
  1955  			if ir.BoolVal(n.Cond) {
  1956  				s.stmtList(n.Body)
  1957  			} else {
  1958  				s.stmtList(n.Else)
  1959  			}
  1960  			break
  1961  		}
  1962  
  1963  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  1964  		var likely int8
  1965  		if n.Likely {
  1966  			likely = 1
  1967  		}
  1968  		var bThen *ssa.Block
  1969  		if len(n.Body) != 0 {
  1970  			bThen = s.f.NewBlock(ssa.BlockPlain)
  1971  		} else {
  1972  			bThen = bEnd
  1973  		}
  1974  		var bElse *ssa.Block
  1975  		if len(n.Else) != 0 {
  1976  			bElse = s.f.NewBlock(ssa.BlockPlain)
  1977  		} else {
  1978  			bElse = bEnd
  1979  		}
  1980  		s.condBranch(n.Cond, bThen, bElse, likely)
  1981  
  1982  		if len(n.Body) != 0 {
  1983  			s.startBlock(bThen)
  1984  			s.stmtList(n.Body)
  1985  			if b := s.endBlock(); b != nil {
  1986  				b.AddEdgeTo(bEnd)
  1987  			}
  1988  		}
  1989  		if len(n.Else) != 0 {
  1990  			s.startBlock(bElse)
  1991  			s.stmtList(n.Else)
  1992  			if b := s.endBlock(); b != nil {
  1993  				b.AddEdgeTo(bEnd)
  1994  			}
  1995  		}
  1996  		s.startBlock(bEnd)
  1997  
  1998  	case ir.ORETURN:
  1999  		n := n.(*ir.ReturnStmt)
  2000  		s.stmtList(n.Results)
  2001  		b := s.exit()
  2002  		b.Pos = s.lastPos.WithIsStmt()
  2003  
  2004  	case ir.OTAILCALL:
  2005  		n := n.(*ir.TailCallStmt)
  2006  		s.callResult(n.Call, callTail)
  2007  		call := s.mem()
  2008  		b := s.endBlock()
  2009  		b.Kind = ssa.BlockRetJmp // could use BlockExit. BlockRetJmp is mostly for clarity.
  2010  		b.SetControl(call)
  2011  
  2012  	case ir.OCONTINUE, ir.OBREAK:
  2013  		n := n.(*ir.BranchStmt)
  2014  		var to *ssa.Block
  2015  		if n.Label == nil {
  2016  			// plain break/continue
  2017  			switch n.Op() {
  2018  			case ir.OCONTINUE:
  2019  				to = s.continueTo
  2020  			case ir.OBREAK:
  2021  				to = s.breakTo
  2022  			}
  2023  		} else {
  2024  			// labeled break/continue; look up the target
  2025  			sym := n.Label
  2026  			lab := s.label(sym)
  2027  			switch n.Op() {
  2028  			case ir.OCONTINUE:
  2029  				to = lab.continueTarget
  2030  			case ir.OBREAK:
  2031  				to = lab.breakTarget
  2032  			}
  2033  		}
  2034  
  2035  		b := s.endBlock()
  2036  		b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block.
  2037  		b.AddEdgeTo(to)
  2038  
  2039  	case ir.OFOR:
  2040  		// OFOR: for Ninit; Left; Right { Nbody }
  2041  		// cond (Left); body (Nbody); incr (Right)
  2042  		n := n.(*ir.ForStmt)
  2043  		base.Assert(!n.DistinctVars) // Should all be rewritten before escape analysis
  2044  		bCond := s.f.NewBlock(ssa.BlockPlain)
  2045  		bBody := s.f.NewBlock(ssa.BlockPlain)
  2046  		bIncr := s.f.NewBlock(ssa.BlockPlain)
  2047  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  2048  
  2049  		// ensure empty for loops have correct position; issue #30167
  2050  		bBody.Pos = n.Pos()
  2051  
  2052  		// first, jump to condition test
  2053  		b := s.endBlock()
  2054  		b.AddEdgeTo(bCond)
  2055  
  2056  		// generate code to test condition
  2057  		s.startBlock(bCond)
  2058  		if n.Cond != nil {
  2059  			s.condBranch(n.Cond, bBody, bEnd, 1)
  2060  		} else {
  2061  			b := s.endBlock()
  2062  			b.Kind = ssa.BlockPlain
  2063  			b.AddEdgeTo(bBody)
  2064  		}
  2065  
  2066  		// set up for continue/break in body
  2067  		prevContinue := s.continueTo
  2068  		prevBreak := s.breakTo
  2069  		s.continueTo = bIncr
  2070  		s.breakTo = bEnd
  2071  		var lab *ssaLabel
  2072  		if sym := n.Label; sym != nil {
  2073  			// labeled for loop
  2074  			lab = s.label(sym)
  2075  			lab.continueTarget = bIncr
  2076  			lab.breakTarget = bEnd
  2077  		}
  2078  
  2079  		// generate body
  2080  		s.startBlock(bBody)
  2081  		s.stmtList(n.Body)
  2082  
  2083  		// tear down continue/break
  2084  		s.continueTo = prevContinue
  2085  		s.breakTo = prevBreak
  2086  		if lab != nil {
  2087  			lab.continueTarget = nil
  2088  			lab.breakTarget = nil
  2089  		}
  2090  
  2091  		// done with body, goto incr
  2092  		if b := s.endBlock(); b != nil {
  2093  			b.AddEdgeTo(bIncr)
  2094  		}
  2095  
  2096  		// generate incr
  2097  		s.startBlock(bIncr)
  2098  		if n.Post != nil {
  2099  			s.stmt(n.Post)
  2100  		}
  2101  		if b := s.endBlock(); b != nil {
  2102  			b.AddEdgeTo(bCond)
  2103  			// It can happen that bIncr ends in a block containing only VARKILL,
  2104  			// and that muddles the debugging experience.
  2105  			if b.Pos == src.NoXPos {
  2106  				b.Pos = bCond.Pos
  2107  			}
  2108  		}
  2109  
  2110  		s.startBlock(bEnd)
  2111  
  2112  	case ir.OSWITCH, ir.OSELECT:
  2113  		// These have been mostly rewritten by the front end into their Nbody fields.
  2114  		// Our main task is to correctly hook up any break statements.
  2115  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  2116  
  2117  		prevBreak := s.breakTo
  2118  		s.breakTo = bEnd
  2119  		var sym *types.Sym
  2120  		var body ir.Nodes
  2121  		if n.Op() == ir.OSWITCH {
  2122  			n := n.(*ir.SwitchStmt)
  2123  			sym = n.Label
  2124  			body = n.Compiled
  2125  		} else {
  2126  			n := n.(*ir.SelectStmt)
  2127  			sym = n.Label
  2128  			body = n.Compiled
  2129  		}
  2130  
  2131  		var lab *ssaLabel
  2132  		if sym != nil {
  2133  			// labeled
  2134  			lab = s.label(sym)
  2135  			lab.breakTarget = bEnd
  2136  		}
  2137  
  2138  		// generate body code
  2139  		s.stmtList(body)
  2140  
  2141  		s.breakTo = prevBreak
  2142  		if lab != nil {
  2143  			lab.breakTarget = nil
  2144  		}
  2145  
  2146  		// walk adds explicit OBREAK nodes to the end of all reachable code paths.
  2147  		// If we still have a current block here, then mark it unreachable.
  2148  		if s.curBlock != nil {
  2149  			m := s.mem()
  2150  			b := s.endBlock()
  2151  			b.Kind = ssa.BlockExit
  2152  			b.SetControl(m)
  2153  		}
  2154  		s.startBlock(bEnd)
  2155  
  2156  	case ir.OJUMPTABLE:
  2157  		n := n.(*ir.JumpTableStmt)
  2158  
  2159  		// Make blocks we'll need.
  2160  		jt := s.f.NewBlock(ssa.BlockJumpTable)
  2161  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  2162  
  2163  		// The only thing that needs evaluating is the index we're looking up.
  2164  		idx := s.expr(n.Idx)
  2165  		unsigned := idx.Type.IsUnsigned()
  2166  
  2167  		// Extend so we can do everything in uintptr arithmetic.
  2168  		t := types.Types[types.TUINTPTR]
  2169  		idx = s.conv(nil, idx, idx.Type, t)
  2170  
  2171  		// The ending condition for the current block decides whether we'll use
  2172  		// the jump table at all.
  2173  		// We check that min <= idx <= max and jump around the jump table
  2174  		// if that test fails.
  2175  		// We implement min <= idx <= max with 0 <= idx-min <= max-min, because
  2176  		// we'll need idx-min anyway as the control value for the jump table.
  2177  		var min, max uint64
  2178  		if unsigned {
  2179  			min, _ = constant.Uint64Val(n.Cases[0])
  2180  			max, _ = constant.Uint64Val(n.Cases[len(n.Cases)-1])
  2181  		} else {
  2182  			mn, _ := constant.Int64Val(n.Cases[0])
  2183  			mx, _ := constant.Int64Val(n.Cases[len(n.Cases)-1])
  2184  			min = uint64(mn)
  2185  			max = uint64(mx)
  2186  		}
  2187  		// Compare idx-min with max-min, to see if we can use the jump table.
  2188  		idx = s.newValue2(s.ssaOp(ir.OSUB, t), t, idx, s.uintptrConstant(min))
  2189  		width := s.uintptrConstant(max - min)
  2190  		cmp := s.newValue2(s.ssaOp(ir.OLE, t), types.Types[types.TBOOL], idx, width)
  2191  		b := s.endBlock()
  2192  		b.Kind = ssa.BlockIf
  2193  		b.SetControl(cmp)
  2194  		b.AddEdgeTo(jt)             // in range - use jump table
  2195  		b.AddEdgeTo(bEnd)           // out of range - no case in the jump table will trigger
  2196  		b.Likely = ssa.BranchLikely // TODO: assumes missing the table entirely is unlikely. True?
  2197  
  2198  		// Build jump table block.
  2199  		s.startBlock(jt)
  2200  		jt.Pos = n.Pos()
  2201  		if base.Flag.Cfg.SpectreIndex {
  2202  			idx = s.newValue2(ssa.OpSpectreSliceIndex, t, idx, width)
  2203  		}
  2204  		jt.SetControl(idx)
  2205  
  2206  		// Figure out where we should go for each index in the table.
  2207  		table := make([]*ssa.Block, max-min+1)
  2208  		for i := range table {
  2209  			table[i] = bEnd // default target
  2210  		}
  2211  		for i := range n.Targets {
  2212  			c := n.Cases[i]
  2213  			lab := s.label(n.Targets[i])
  2214  			if lab.target == nil {
  2215  				lab.target = s.f.NewBlock(ssa.BlockPlain)
  2216  			}
  2217  			var val uint64
  2218  			if unsigned {
  2219  				val, _ = constant.Uint64Val(c)
  2220  			} else {
  2221  				vl, _ := constant.Int64Val(c)
  2222  				val = uint64(vl)
  2223  			}
  2224  			// Overwrite the default target.
  2225  			table[val-min] = lab.target
  2226  		}
  2227  		for _, t := range table {
  2228  			jt.AddEdgeTo(t)
  2229  		}
  2230  		s.endBlock()
  2231  
  2232  		s.startBlock(bEnd)
  2233  
  2234  	case ir.OINTERFACESWITCH:
  2235  		n := n.(*ir.InterfaceSwitchStmt)
  2236  		typs := s.f.Config.Types
  2237  
  2238  		t := s.expr(n.RuntimeType)
  2239  		h := s.expr(n.Hash)
  2240  		d := s.newValue1A(ssa.OpAddr, typs.BytePtr, n.Descriptor, s.sb)
  2241  
  2242  		// Check the cache first.
  2243  		var merge *ssa.Block
  2244  		if base.Flag.N == 0 && rtabi.UseInterfaceSwitchCache(Arch.LinkArch.Family) {
  2245  			// Note: we can only use the cache if we have the right atomic load instruction.
  2246  			// Double-check that here.
  2247  			if intrinsics.lookup(Arch.LinkArch.Arch, "internal/runtime/atomic", "Loadp") == nil {
  2248  				s.Fatalf("atomic load not available")
  2249  			}
  2250  			merge = s.f.NewBlock(ssa.BlockPlain)
  2251  			cacheHit := s.f.NewBlock(ssa.BlockPlain)
  2252  			cacheMiss := s.f.NewBlock(ssa.BlockPlain)
  2253  			loopHead := s.f.NewBlock(ssa.BlockPlain)
  2254  			loopBody := s.f.NewBlock(ssa.BlockPlain)
  2255  
  2256  			// Pick right size ops.
  2257  			var mul, and, add, zext ssa.Op
  2258  			if s.config.PtrSize == 4 {
  2259  				mul = ssa.OpMul32
  2260  				and = ssa.OpAnd32
  2261  				add = ssa.OpAdd32
  2262  				zext = ssa.OpCopy
  2263  			} else {
  2264  				mul = ssa.OpMul64
  2265  				and = ssa.OpAnd64
  2266  				add = ssa.OpAdd64
  2267  				zext = ssa.OpZeroExt32to64
  2268  			}
  2269  
  2270  			// Load cache pointer out of descriptor, with an atomic load so
  2271  			// we ensure that we see a fully written cache.
  2272  			atomicLoad := s.newValue2(ssa.OpAtomicLoadPtr, types.NewTuple(typs.BytePtr, types.TypeMem), d, s.mem())
  2273  			cache := s.newValue1(ssa.OpSelect0, typs.BytePtr, atomicLoad)
  2274  			s.vars[memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, atomicLoad)
  2275  
  2276  			// Initialize hash variable.
  2277  			s.vars[hashVar] = s.newValue1(zext, typs.Uintptr, h)
  2278  
  2279  			// Load mask from cache.
  2280  			mask := s.newValue2(ssa.OpLoad, typs.Uintptr, cache, s.mem())
  2281  			// Jump to loop head.
  2282  			b := s.endBlock()
  2283  			b.AddEdgeTo(loopHead)
  2284  
  2285  			// At loop head, get pointer to the cache entry.
  2286  			//   e := &cache.Entries[hash&mask]
  2287  			s.startBlock(loopHead)
  2288  			entries := s.newValue2(ssa.OpAddPtr, typs.UintptrPtr, cache, s.uintptrConstant(uint64(s.config.PtrSize)))
  2289  			idx := s.newValue2(and, typs.Uintptr, s.variable(hashVar, typs.Uintptr), mask)
  2290  			idx = s.newValue2(mul, typs.Uintptr, idx, s.uintptrConstant(uint64(3*s.config.PtrSize)))
  2291  			e := s.newValue2(ssa.OpAddPtr, typs.UintptrPtr, entries, idx)
  2292  			//   hash++
  2293  			s.vars[hashVar] = s.newValue2(add, typs.Uintptr, s.variable(hashVar, typs.Uintptr), s.uintptrConstant(1))
  2294  
  2295  			// Look for a cache hit.
  2296  			//   if e.Typ == t { goto hit }
  2297  			eTyp := s.newValue2(ssa.OpLoad, typs.Uintptr, e, s.mem())
  2298  			cmp1 := s.newValue2(ssa.OpEqPtr, typs.Bool, t, eTyp)
  2299  			b = s.endBlock()
  2300  			b.Kind = ssa.BlockIf
  2301  			b.SetControl(cmp1)
  2302  			b.AddEdgeTo(cacheHit)
  2303  			b.AddEdgeTo(loopBody)
  2304  
  2305  			// Look for an empty entry, the tombstone for this hash table.
  2306  			//   if e.Typ == nil { goto miss }
  2307  			s.startBlock(loopBody)
  2308  			cmp2 := s.newValue2(ssa.OpEqPtr, typs.Bool, eTyp, s.constNil(typs.BytePtr))
  2309  			b = s.endBlock()
  2310  			b.Kind = ssa.BlockIf
  2311  			b.SetControl(cmp2)
  2312  			b.AddEdgeTo(cacheMiss)
  2313  			b.AddEdgeTo(loopHead)
  2314  
  2315  			// On a hit, load the data fields of the cache entry.
  2316  			//   Case = e.Case
  2317  			//   Itab = e.Itab
  2318  			s.startBlock(cacheHit)
  2319  			eCase := s.newValue2(ssa.OpLoad, typs.Int, s.newValue1I(ssa.OpOffPtr, typs.IntPtr, s.config.PtrSize, e), s.mem())
  2320  			eItab := s.newValue2(ssa.OpLoad, typs.BytePtr, s.newValue1I(ssa.OpOffPtr, typs.BytePtrPtr, 2*s.config.PtrSize, e), s.mem())
  2321  			s.assign(n.Case, eCase, false, 0)
  2322  			s.assign(n.Itab, eItab, false, 0)
  2323  			b = s.endBlock()
  2324  			b.AddEdgeTo(merge)
  2325  
  2326  			// On a miss, call into the runtime to get the answer.
  2327  			s.startBlock(cacheMiss)
  2328  		}
  2329  
  2330  		r := s.rtcall(ir.Syms.InterfaceSwitch, true, []*types.Type{typs.Int, typs.BytePtr}, d, t)
  2331  		s.assign(n.Case, r[0], false, 0)
  2332  		s.assign(n.Itab, r[1], false, 0)
  2333  
  2334  		if merge != nil {
  2335  			// Cache hits merge in here.
  2336  			b := s.endBlock()
  2337  			b.Kind = ssa.BlockPlain
  2338  			b.AddEdgeTo(merge)
  2339  			s.startBlock(merge)
  2340  		}
  2341  
  2342  	case ir.OCHECKNIL:
  2343  		n := n.(*ir.UnaryExpr)
  2344  		p := s.expr(n.X)
  2345  		_ = s.nilCheck(p)
  2346  		// TODO: check that throwing away the nilcheck result is ok.
  2347  
  2348  	case ir.OINLMARK:
  2349  		n := n.(*ir.InlineMarkStmt)
  2350  		s.newValue1I(ssa.OpInlMark, types.TypeVoid, n.Index, s.mem())
  2351  
  2352  	default:
  2353  		s.Fatalf("unhandled stmt %v", n.Op())
  2354  	}
  2355  }
  2356  
  2357  // If true, share as many open-coded defer exits as possible (with the downside of
  2358  // worse line-number information)
  2359  const shareDeferExits = false
  2360  
  2361  // exit processes any code that needs to be generated just before returning.
  2362  // It returns a BlockRet block that ends the control flow. Its control value
  2363  // will be set to the final memory state.
  2364  func (s *state) exit() *ssa.Block {
  2365  	if s.hasdefer {
  2366  		if s.hasOpenDefers {
  2367  			if shareDeferExits && s.lastDeferExit != nil && len(s.openDefers) == s.lastDeferCount {
  2368  				if s.curBlock.Kind != ssa.BlockPlain {
  2369  					panic("Block for an exit should be BlockPlain")
  2370  				}
  2371  				s.curBlock.AddEdgeTo(s.lastDeferExit)
  2372  				s.endBlock()
  2373  				return s.lastDeferFinalBlock
  2374  			}
  2375  			s.openDeferExit()
  2376  		} else {
  2377  			// Shared deferreturn is assigned the "last" position in the function.
  2378  			// The linker picks the first deferreturn call it sees, so this is
  2379  			// the only sensible "shared" place.
  2380  			// To not-share deferreturn, the protocol would need to be changed
  2381  			// so that the call to deferproc-etc would receive the PC offset from
  2382  			// the return PC, and the runtime would need to use that instead of
  2383  			// the deferreturn retrieved from the pcln information.
  2384  			// opendefers would remain a problem, however.
  2385  			s.pushLine(s.curfn.Endlineno)
  2386  			s.rtcall(ir.Syms.Deferreturn, true, nil)
  2387  			s.popLine()
  2388  		}
  2389  	}
  2390  
  2391  	// Do actual return.
  2392  	// These currently turn into self-copies (in many cases).
  2393  	resultFields := s.curfn.Type().Results()
  2394  	results := make([]*ssa.Value, len(resultFields)+1, len(resultFields)+1)
  2395  	// Store SSAable and heap-escaped PPARAMOUT variables back to stack locations.
  2396  	for i, f := range resultFields {
  2397  		n := f.Nname.(*ir.Name)
  2398  		if s.canSSA(n) { // result is in some SSA variable
  2399  			if !n.IsOutputParamInRegisters() && n.Type().HasPointers() {
  2400  				// We are about to store to the result slot.
  2401  				s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, n, s.mem())
  2402  			}
  2403  			results[i] = s.variable(n, n.Type())
  2404  		} else if !n.OnStack() { // result is actually heap allocated
  2405  			// We are about to copy the in-heap result to the result slot.
  2406  			if n.Type().HasPointers() {
  2407  				s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, n, s.mem())
  2408  			}
  2409  			ha := s.expr(n.Heapaddr)
  2410  			s.instrumentFields(n.Type(), ha, instrumentRead)
  2411  			results[i] = s.newValue2(ssa.OpDereference, n.Type(), ha, s.mem())
  2412  		} else { // result is not SSA-able; not escaped, so not on heap, but too large for SSA.
  2413  			// Before register ABI this ought to be a self-move, home=dest,
  2414  			// With register ABI, it's still a self-move if parameter is on stack (i.e., too big or overflowed)
  2415  			// No VarDef, as the result slot is already holding live value.
  2416  			results[i] = s.newValue2(ssa.OpDereference, n.Type(), s.addr(n), s.mem())
  2417  		}
  2418  	}
  2419  
  2420  	// In -race mode, we need to call racefuncexit.
  2421  	// Note: This has to happen after we load any heap-allocated results,
  2422  	// otherwise races will be attributed to the caller instead.
  2423  	if s.instrumentEnterExit {
  2424  		s.rtcall(ir.Syms.Racefuncexit, true, nil)
  2425  	}
  2426  
  2427  	results[len(results)-1] = s.mem()
  2428  	m := s.newValue0(ssa.OpMakeResult, s.f.OwnAux.LateExpansionResultType())
  2429  	m.AddArgs(results...)
  2430  
  2431  	b := s.endBlock()
  2432  	b.Kind = ssa.BlockRet
  2433  	b.SetControl(m)
  2434  	if s.hasdefer && s.hasOpenDefers {
  2435  		s.lastDeferFinalBlock = b
  2436  	}
  2437  	return b
  2438  }
  2439  
  2440  type opAndType struct {
  2441  	op    ir.Op
  2442  	etype types.Kind
  2443  }
  2444  
  2445  var opToSSA = map[opAndType]ssa.Op{
  2446  	{ir.OADD, types.TINT8}:    ssa.OpAdd8,
  2447  	{ir.OADD, types.TUINT8}:   ssa.OpAdd8,
  2448  	{ir.OADD, types.TINT16}:   ssa.OpAdd16,
  2449  	{ir.OADD, types.TUINT16}:  ssa.OpAdd16,
  2450  	{ir.OADD, types.TINT32}:   ssa.OpAdd32,
  2451  	{ir.OADD, types.TUINT32}:  ssa.OpAdd32,
  2452  	{ir.OADD, types.TINT64}:   ssa.OpAdd64,
  2453  	{ir.OADD, types.TUINT64}:  ssa.OpAdd64,
  2454  	{ir.OADD, types.TFLOAT32}: ssa.OpAdd32F,
  2455  	{ir.OADD, types.TFLOAT64}: ssa.OpAdd64F,
  2456  
  2457  	{ir.OSUB, types.TINT8}:    ssa.OpSub8,
  2458  	{ir.OSUB, types.TUINT8}:   ssa.OpSub8,
  2459  	{ir.OSUB, types.TINT16}:   ssa.OpSub16,
  2460  	{ir.OSUB, types.TUINT16}:  ssa.OpSub16,
  2461  	{ir.OSUB, types.TINT32}:   ssa.OpSub32,
  2462  	{ir.OSUB, types.TUINT32}:  ssa.OpSub32,
  2463  	{ir.OSUB, types.TINT64}:   ssa.OpSub64,
  2464  	{ir.OSUB, types.TUINT64}:  ssa.OpSub64,
  2465  	{ir.OSUB, types.TFLOAT32}: ssa.OpSub32F,
  2466  	{ir.OSUB, types.TFLOAT64}: ssa.OpSub64F,
  2467  
  2468  	{ir.ONOT, types.TBOOL}: ssa.OpNot,
  2469  
  2470  	{ir.ONEG, types.TINT8}:    ssa.OpNeg8,
  2471  	{ir.ONEG, types.TUINT8}:   ssa.OpNeg8,
  2472  	{ir.ONEG, types.TINT16}:   ssa.OpNeg16,
  2473  	{ir.ONEG, types.TUINT16}:  ssa.OpNeg16,
  2474  	{ir.ONEG, types.TINT32}:   ssa.OpNeg32,
  2475  	{ir.ONEG, types.TUINT32}:  ssa.OpNeg32,
  2476  	{ir.ONEG, types.TINT64}:   ssa.OpNeg64,
  2477  	{ir.ONEG, types.TUINT64}:  ssa.OpNeg64,
  2478  	{ir.ONEG, types.TFLOAT32}: ssa.OpNeg32F,
  2479  	{ir.ONEG, types.TFLOAT64}: ssa.OpNeg64F,
  2480  
  2481  	{ir.OBITNOT, types.TINT8}:   ssa.OpCom8,
  2482  	{ir.OBITNOT, types.TUINT8}:  ssa.OpCom8,
  2483  	{ir.OBITNOT, types.TINT16}:  ssa.OpCom16,
  2484  	{ir.OBITNOT, types.TUINT16}: ssa.OpCom16,
  2485  	{ir.OBITNOT, types.TINT32}:  ssa.OpCom32,
  2486  	{ir.OBITNOT, types.TUINT32}: ssa.OpCom32,
  2487  	{ir.OBITNOT, types.TINT64}:  ssa.OpCom64,
  2488  	{ir.OBITNOT, types.TUINT64}: ssa.OpCom64,
  2489  
  2490  	{ir.OIMAG, types.TCOMPLEX64}:  ssa.OpComplexImag,
  2491  	{ir.OIMAG, types.TCOMPLEX128}: ssa.OpComplexImag,
  2492  	{ir.OREAL, types.TCOMPLEX64}:  ssa.OpComplexReal,
  2493  	{ir.OREAL, types.TCOMPLEX128}: ssa.OpComplexReal,
  2494  
  2495  	{ir.OMUL, types.TINT8}:    ssa.OpMul8,
  2496  	{ir.OMUL, types.TUINT8}:   ssa.OpMul8,
  2497  	{ir.OMUL, types.TINT16}:   ssa.OpMul16,
  2498  	{ir.OMUL, types.TUINT16}:  ssa.OpMul16,
  2499  	{ir.OMUL, types.TINT32}:   ssa.OpMul32,
  2500  	{ir.OMUL, types.TUINT32}:  ssa.OpMul32,
  2501  	{ir.OMUL, types.TINT64}:   ssa.OpMul64,
  2502  	{ir.OMUL, types.TUINT64}:  ssa.OpMul64,
  2503  	{ir.OMUL, types.TFLOAT32}: ssa.OpMul32F,
  2504  	{ir.OMUL, types.TFLOAT64}: ssa.OpMul64F,
  2505  
  2506  	{ir.ODIV, types.TFLOAT32}: ssa.OpDiv32F,
  2507  	{ir.ODIV, types.TFLOAT64}: ssa.OpDiv64F,
  2508  
  2509  	{ir.ODIV, types.TINT8}:   ssa.OpDiv8,
  2510  	{ir.ODIV, types.TUINT8}:  ssa.OpDiv8u,
  2511  	{ir.ODIV, types.TINT16}:  ssa.OpDiv16,
  2512  	{ir.ODIV, types.TUINT16}: ssa.OpDiv16u,
  2513  	{ir.ODIV, types.TINT32}:  ssa.OpDiv32,
  2514  	{ir.ODIV, types.TUINT32}: ssa.OpDiv32u,
  2515  	{ir.ODIV, types.TINT64}:  ssa.OpDiv64,
  2516  	{ir.ODIV, types.TUINT64}: ssa.OpDiv64u,
  2517  
  2518  	{ir.OMOD, types.TINT8}:   ssa.OpMod8,
  2519  	{ir.OMOD, types.TUINT8}:  ssa.OpMod8u,
  2520  	{ir.OMOD, types.TINT16}:  ssa.OpMod16,
  2521  	{ir.OMOD, types.TUINT16}: ssa.OpMod16u,
  2522  	{ir.OMOD, types.TINT32}:  ssa.OpMod32,
  2523  	{ir.OMOD, types.TUINT32}: ssa.OpMod32u,
  2524  	{ir.OMOD, types.TINT64}:  ssa.OpMod64,
  2525  	{ir.OMOD, types.TUINT64}: ssa.OpMod64u,
  2526  
  2527  	{ir.OAND, types.TINT8}:   ssa.OpAnd8,
  2528  	{ir.OAND, types.TUINT8}:  ssa.OpAnd8,
  2529  	{ir.OAND, types.TINT16}:  ssa.OpAnd16,
  2530  	{ir.OAND, types.TUINT16}: ssa.OpAnd16,
  2531  	{ir.OAND, types.TINT32}:  ssa.OpAnd32,
  2532  	{ir.OAND, types.TUINT32}: ssa.OpAnd32,
  2533  	{ir.OAND, types.TINT64}:  ssa.OpAnd64,
  2534  	{ir.OAND, types.TUINT64}: ssa.OpAnd64,
  2535  
  2536  	{ir.OOR, types.TINT8}:   ssa.OpOr8,
  2537  	{ir.OOR, types.TUINT8}:  ssa.OpOr8,
  2538  	{ir.OOR, types.TINT16}:  ssa.OpOr16,
  2539  	{ir.OOR, types.TUINT16}: ssa.OpOr16,
  2540  	{ir.OOR, types.TINT32}:  ssa.OpOr32,
  2541  	{ir.OOR, types.TUINT32}: ssa.OpOr32,
  2542  	{ir.OOR, types.TINT64}:  ssa.OpOr64,
  2543  	{ir.OOR, types.TUINT64}: ssa.OpOr64,
  2544  
  2545  	{ir.OXOR, types.TINT8}:   ssa.OpXor8,
  2546  	{ir.OXOR, types.TUINT8}:  ssa.OpXor8,
  2547  	{ir.OXOR, types.TINT16}:  ssa.OpXor16,
  2548  	{ir.OXOR, types.TUINT16}: ssa.OpXor16,
  2549  	{ir.OXOR, types.TINT32}:  ssa.OpXor32,
  2550  	{ir.OXOR, types.TUINT32}: ssa.OpXor32,
  2551  	{ir.OXOR, types.TINT64}:  ssa.OpXor64,
  2552  	{ir.OXOR, types.TUINT64}: ssa.OpXor64,
  2553  
  2554  	{ir.OEQ, types.TBOOL}:      ssa.OpEqB,
  2555  	{ir.OEQ, types.TINT8}:      ssa.OpEq8,
  2556  	{ir.OEQ, types.TUINT8}:     ssa.OpEq8,
  2557  	{ir.OEQ, types.TINT16}:     ssa.OpEq16,
  2558  	{ir.OEQ, types.TUINT16}:    ssa.OpEq16,
  2559  	{ir.OEQ, types.TINT32}:     ssa.OpEq32,
  2560  	{ir.OEQ, types.TUINT32}:    ssa.OpEq32,
  2561  	{ir.OEQ, types.TINT64}:     ssa.OpEq64,
  2562  	{ir.OEQ, types.TUINT64}:    ssa.OpEq64,
  2563  	{ir.OEQ, types.TINTER}:     ssa.OpEqInter,
  2564  	{ir.OEQ, types.TSLICE}:     ssa.OpEqSlice,
  2565  	{ir.OEQ, types.TFUNC}:      ssa.OpEqPtr,
  2566  	{ir.OEQ, types.TMAP}:       ssa.OpEqPtr,
  2567  	{ir.OEQ, types.TCHAN}:      ssa.OpEqPtr,
  2568  	{ir.OEQ, types.TPTR}:       ssa.OpEqPtr,
  2569  	{ir.OEQ, types.TUINTPTR}:   ssa.OpEqPtr,
  2570  	{ir.OEQ, types.TUNSAFEPTR}: ssa.OpEqPtr,
  2571  	{ir.OEQ, types.TFLOAT64}:   ssa.OpEq64F,
  2572  	{ir.OEQ, types.TFLOAT32}:   ssa.OpEq32F,
  2573  
  2574  	{ir.ONE, types.TBOOL}:      ssa.OpNeqB,
  2575  	{ir.ONE, types.TINT8}:      ssa.OpNeq8,
  2576  	{ir.ONE, types.TUINT8}:     ssa.OpNeq8,
  2577  	{ir.ONE, types.TINT16}:     ssa.OpNeq16,
  2578  	{ir.ONE, types.TUINT16}:    ssa.OpNeq16,
  2579  	{ir.ONE, types.TINT32}:     ssa.OpNeq32,
  2580  	{ir.ONE, types.TUINT32}:    ssa.OpNeq32,
  2581  	{ir.ONE, types.TINT64}:     ssa.OpNeq64,
  2582  	{ir.ONE, types.TUINT64}:    ssa.OpNeq64,
  2583  	{ir.ONE, types.TINTER}:     ssa.OpNeqInter,
  2584  	{ir.ONE, types.TSLICE}:     ssa.OpNeqSlice,
  2585  	{ir.ONE, types.TFUNC}:      ssa.OpNeqPtr,
  2586  	{ir.ONE, types.TMAP}:       ssa.OpNeqPtr,
  2587  	{ir.ONE, types.TCHAN}:      ssa.OpNeqPtr,
  2588  	{ir.ONE, types.TPTR}:       ssa.OpNeqPtr,
  2589  	{ir.ONE, types.TUINTPTR}:   ssa.OpNeqPtr,
  2590  	{ir.ONE, types.TUNSAFEPTR}: ssa.OpNeqPtr,
  2591  	{ir.ONE, types.TFLOAT64}:   ssa.OpNeq64F,
  2592  	{ir.ONE, types.TFLOAT32}:   ssa.OpNeq32F,
  2593  
  2594  	{ir.OLT, types.TINT8}:    ssa.OpLess8,
  2595  	{ir.OLT, types.TUINT8}:   ssa.OpLess8U,
  2596  	{ir.OLT, types.TINT16}:   ssa.OpLess16,
  2597  	{ir.OLT, types.TUINT16}:  ssa.OpLess16U,
  2598  	{ir.OLT, types.TINT32}:   ssa.OpLess32,
  2599  	{ir.OLT, types.TUINT32}:  ssa.OpLess32U,
  2600  	{ir.OLT, types.TINT64}:   ssa.OpLess64,
  2601  	{ir.OLT, types.TUINT64}:  ssa.OpLess64U,
  2602  	{ir.OLT, types.TFLOAT64}: ssa.OpLess64F,
  2603  	{ir.OLT, types.TFLOAT32}: ssa.OpLess32F,
  2604  
  2605  	{ir.OLE, types.TINT8}:    ssa.OpLeq8,
  2606  	{ir.OLE, types.TUINT8}:   ssa.OpLeq8U,
  2607  	{ir.OLE, types.TINT16}:   ssa.OpLeq16,
  2608  	{ir.OLE, types.TUINT16}:  ssa.OpLeq16U,
  2609  	{ir.OLE, types.TINT32}:   ssa.OpLeq32,
  2610  	{ir.OLE, types.TUINT32}:  ssa.OpLeq32U,
  2611  	{ir.OLE, types.TINT64}:   ssa.OpLeq64,
  2612  	{ir.OLE, types.TUINT64}:  ssa.OpLeq64U,
  2613  	{ir.OLE, types.TFLOAT64}: ssa.OpLeq64F,
  2614  	{ir.OLE, types.TFLOAT32}: ssa.OpLeq32F,
  2615  }
  2616  
  2617  func (s *state) concreteEtype(t *types.Type) types.Kind {
  2618  	e := t.Kind()
  2619  	switch e {
  2620  	default:
  2621  		return e
  2622  	case types.TINT:
  2623  		if s.config.PtrSize == 8 {
  2624  			return types.TINT64
  2625  		}
  2626  		return types.TINT32
  2627  	case types.TUINT:
  2628  		if s.config.PtrSize == 8 {
  2629  			return types.TUINT64
  2630  		}
  2631  		return types.TUINT32
  2632  	case types.TUINTPTR:
  2633  		if s.config.PtrSize == 8 {
  2634  			return types.TUINT64
  2635  		}
  2636  		return types.TUINT32
  2637  	}
  2638  }
  2639  
  2640  func (s *state) ssaOp(op ir.Op, t *types.Type) ssa.Op {
  2641  	etype := s.concreteEtype(t)
  2642  	x, ok := opToSSA[opAndType{op, etype}]
  2643  	if !ok {
  2644  		s.Fatalf("unhandled binary op %v %s", op, etype)
  2645  	}
  2646  	return x
  2647  }
  2648  
  2649  type opAndTwoTypes struct {
  2650  	op     ir.Op
  2651  	etype1 types.Kind
  2652  	etype2 types.Kind
  2653  }
  2654  
  2655  type twoTypes struct {
  2656  	etype1 types.Kind
  2657  	etype2 types.Kind
  2658  }
  2659  
  2660  type twoOpsAndType struct {
  2661  	op1              ssa.Op
  2662  	op2              ssa.Op
  2663  	intermediateType types.Kind
  2664  }
  2665  
  2666  var fpConvOpToSSA = map[twoTypes]twoOpsAndType{
  2667  
  2668  	{types.TINT8, types.TFLOAT32}:  {ssa.OpSignExt8to32, ssa.OpCvt32to32F, types.TINT32},
  2669  	{types.TINT16, types.TFLOAT32}: {ssa.OpSignExt16to32, ssa.OpCvt32to32F, types.TINT32},
  2670  	{types.TINT32, types.TFLOAT32}: {ssa.OpCopy, ssa.OpCvt32to32F, types.TINT32},
  2671  	{types.TINT64, types.TFLOAT32}: {ssa.OpCopy, ssa.OpCvt64to32F, types.TINT64},
  2672  
  2673  	{types.TINT8, types.TFLOAT64}:  {ssa.OpSignExt8to32, ssa.OpCvt32to64F, types.TINT32},
  2674  	{types.TINT16, types.TFLOAT64}: {ssa.OpSignExt16to32, ssa.OpCvt32to64F, types.TINT32},
  2675  	{types.TINT32, types.TFLOAT64}: {ssa.OpCopy, ssa.OpCvt32to64F, types.TINT32},
  2676  	{types.TINT64, types.TFLOAT64}: {ssa.OpCopy, ssa.OpCvt64to64F, types.TINT64},
  2677  
  2678  	{types.TFLOAT32, types.TINT8}:  {ssa.OpCvt32Fto32, ssa.OpTrunc32to8, types.TINT32},
  2679  	{types.TFLOAT32, types.TINT16}: {ssa.OpCvt32Fto32, ssa.OpTrunc32to16, types.TINT32},
  2680  	{types.TFLOAT32, types.TINT32}: {ssa.OpCvt32Fto32, ssa.OpCopy, types.TINT32},
  2681  	{types.TFLOAT32, types.TINT64}: {ssa.OpCvt32Fto64, ssa.OpCopy, types.TINT64},
  2682  
  2683  	{types.TFLOAT64, types.TINT8}:  {ssa.OpCvt64Fto32, ssa.OpTrunc32to8, types.TINT32},
  2684  	{types.TFLOAT64, types.TINT16}: {ssa.OpCvt64Fto32, ssa.OpTrunc32to16, types.TINT32},
  2685  	{types.TFLOAT64, types.TINT32}: {ssa.OpCvt64Fto32, ssa.OpCopy, types.TINT32},
  2686  	{types.TFLOAT64, types.TINT64}: {ssa.OpCvt64Fto64, ssa.OpCopy, types.TINT64},
  2687  	// unsigned
  2688  	{types.TUINT8, types.TFLOAT32}:  {ssa.OpZeroExt8to32, ssa.OpCvt32to32F, types.TINT32},
  2689  	{types.TUINT16, types.TFLOAT32}: {ssa.OpZeroExt16to32, ssa.OpCvt32to32F, types.TINT32},
  2690  	{types.TUINT32, types.TFLOAT32}: {ssa.OpZeroExt32to64, ssa.OpCvt64to32F, types.TINT64}, // go wide to dodge unsigned
  2691  	{types.TUINT64, types.TFLOAT32}: {ssa.OpCopy, ssa.OpInvalid, types.TUINT64},            // Cvt64Uto32F, branchy code expansion instead
  2692  
  2693  	{types.TUINT8, types.TFLOAT64}:  {ssa.OpZeroExt8to32, ssa.OpCvt32to64F, types.TINT32},
  2694  	{types.TUINT16, types.TFLOAT64}: {ssa.OpZeroExt16to32, ssa.OpCvt32to64F, types.TINT32},
  2695  	{types.TUINT32, types.TFLOAT64}: {ssa.OpZeroExt32to64, ssa.OpCvt64to64F, types.TINT64}, // go wide to dodge unsigned
  2696  	{types.TUINT64, types.TFLOAT64}: {ssa.OpCopy, ssa.OpInvalid, types.TUINT64},            // Cvt64Uto64F, branchy code expansion instead
  2697  
  2698  	{types.TFLOAT32, types.TUINT8}:  {ssa.OpCvt32Fto32, ssa.OpTrunc32to8, types.TINT32},
  2699  	{types.TFLOAT32, types.TUINT16}: {ssa.OpCvt32Fto32, ssa.OpTrunc32to16, types.TINT32},
  2700  	{types.TFLOAT32, types.TUINT32}: {ssa.OpInvalid, ssa.OpCopy, types.TINT64},  // Cvt64Fto32U, branchy code expansion instead
  2701  	{types.TFLOAT32, types.TUINT64}: {ssa.OpInvalid, ssa.OpCopy, types.TUINT64}, // Cvt32Fto64U, branchy code expansion instead
  2702  
  2703  	{types.TFLOAT64, types.TUINT8}:  {ssa.OpCvt64Fto32, ssa.OpTrunc32to8, types.TINT32},
  2704  	{types.TFLOAT64, types.TUINT16}: {ssa.OpCvt64Fto32, ssa.OpTrunc32to16, types.TINT32},
  2705  	{types.TFLOAT64, types.TUINT32}: {ssa.OpInvalid, ssa.OpCopy, types.TINT64},  // Cvt64Fto32U, branchy code expansion instead
  2706  	{types.TFLOAT64, types.TUINT64}: {ssa.OpInvalid, ssa.OpCopy, types.TUINT64}, // Cvt64Fto64U, branchy code expansion instead
  2707  
  2708  	// float
  2709  	{types.TFLOAT64, types.TFLOAT32}: {ssa.OpCvt64Fto32F, ssa.OpCopy, types.TFLOAT32},
  2710  	{types.TFLOAT64, types.TFLOAT64}: {ssa.OpRound64F, ssa.OpCopy, types.TFLOAT64},
  2711  	{types.TFLOAT32, types.TFLOAT32}: {ssa.OpRound32F, ssa.OpCopy, types.TFLOAT32},
  2712  	{types.TFLOAT32, types.TFLOAT64}: {ssa.OpCvt32Fto64F, ssa.OpCopy, types.TFLOAT64},
  2713  }
  2714  
  2715  // this map is used only for 32-bit arch, and only includes the difference
  2716  // on 32-bit arch, don't use int64<->float conversion for uint32
  2717  var fpConvOpToSSA32 = map[twoTypes]twoOpsAndType{
  2718  	{types.TUINT32, types.TFLOAT32}: {ssa.OpCopy, ssa.OpCvt32Uto32F, types.TUINT32},
  2719  	{types.TUINT32, types.TFLOAT64}: {ssa.OpCopy, ssa.OpCvt32Uto64F, types.TUINT32},
  2720  	{types.TFLOAT32, types.TUINT32}: {ssa.OpCvt32Fto32U, ssa.OpCopy, types.TUINT32},
  2721  	{types.TFLOAT64, types.TUINT32}: {ssa.OpCvt64Fto32U, ssa.OpCopy, types.TUINT32},
  2722  }
  2723  
  2724  // uint64<->float conversions, only on machines that have instructions for that
  2725  var uint64fpConvOpToSSA = map[twoTypes]twoOpsAndType{
  2726  	{types.TUINT64, types.TFLOAT32}: {ssa.OpCopy, ssa.OpCvt64Uto32F, types.TUINT64},
  2727  	{types.TUINT64, types.TFLOAT64}: {ssa.OpCopy, ssa.OpCvt64Uto64F, types.TUINT64},
  2728  	{types.TFLOAT32, types.TUINT64}: {ssa.OpCvt32Fto64U, ssa.OpCopy, types.TUINT64},
  2729  	{types.TFLOAT64, types.TUINT64}: {ssa.OpCvt64Fto64U, ssa.OpCopy, types.TUINT64},
  2730  }
  2731  
  2732  var shiftOpToSSA = map[opAndTwoTypes]ssa.Op{
  2733  	{ir.OLSH, types.TINT8, types.TUINT8}:   ssa.OpLsh8x8,
  2734  	{ir.OLSH, types.TUINT8, types.TUINT8}:  ssa.OpLsh8x8,
  2735  	{ir.OLSH, types.TINT8, types.TUINT16}:  ssa.OpLsh8x16,
  2736  	{ir.OLSH, types.TUINT8, types.TUINT16}: ssa.OpLsh8x16,
  2737  	{ir.OLSH, types.TINT8, types.TUINT32}:  ssa.OpLsh8x32,
  2738  	{ir.OLSH, types.TUINT8, types.TUINT32}: ssa.OpLsh8x32,
  2739  	{ir.OLSH, types.TINT8, types.TUINT64}:  ssa.OpLsh8x64,
  2740  	{ir.OLSH, types.TUINT8, types.TUINT64}: ssa.OpLsh8x64,
  2741  
  2742  	{ir.OLSH, types.TINT16, types.TUINT8}:   ssa.OpLsh16x8,
  2743  	{ir.OLSH, types.TUINT16, types.TUINT8}:  ssa.OpLsh16x8,
  2744  	{ir.OLSH, types.TINT16, types.TUINT16}:  ssa.OpLsh16x16,
  2745  	{ir.OLSH, types.TUINT16, types.TUINT16}: ssa.OpLsh16x16,
  2746  	{ir.OLSH, types.TINT16, types.TUINT32}:  ssa.OpLsh16x32,
  2747  	{ir.OLSH, types.TUINT16, types.TUINT32}: ssa.OpLsh16x32,
  2748  	{ir.OLSH, types.TINT16, types.TUINT64}:  ssa.OpLsh16x64,
  2749  	{ir.OLSH, types.TUINT16, types.TUINT64}: ssa.OpLsh16x64,
  2750  
  2751  	{ir.OLSH, types.TINT32, types.TUINT8}:   ssa.OpLsh32x8,
  2752  	{ir.OLSH, types.TUINT32, types.TUINT8}:  ssa.OpLsh32x8,
  2753  	{ir.OLSH, types.TINT32, types.TUINT16}:  ssa.OpLsh32x16,
  2754  	{ir.OLSH, types.TUINT32, types.TUINT16}: ssa.OpLsh32x16,
  2755  	{ir.OLSH, types.TINT32, types.TUINT32}:  ssa.OpLsh32x32,
  2756  	{ir.OLSH, types.TUINT32, types.TUINT32}: ssa.OpLsh32x32,
  2757  	{ir.OLSH, types.TINT32, types.TUINT64}:  ssa.OpLsh32x64,
  2758  	{ir.OLSH, types.TUINT32, types.TUINT64}: ssa.OpLsh32x64,
  2759  
  2760  	{ir.OLSH, types.TINT64, types.TUINT8}:   ssa.OpLsh64x8,
  2761  	{ir.OLSH, types.TUINT64, types.TUINT8}:  ssa.OpLsh64x8,
  2762  	{ir.OLSH, types.TINT64, types.TUINT16}:  ssa.OpLsh64x16,
  2763  	{ir.OLSH, types.TUINT64, types.TUINT16}: ssa.OpLsh64x16,
  2764  	{ir.OLSH, types.TINT64, types.TUINT32}:  ssa.OpLsh64x32,
  2765  	{ir.OLSH, types.TUINT64, types.TUINT32}: ssa.OpLsh64x32,
  2766  	{ir.OLSH, types.TINT64, types.TUINT64}:  ssa.OpLsh64x64,
  2767  	{ir.OLSH, types.TUINT64, types.TUINT64}: ssa.OpLsh64x64,
  2768  
  2769  	{ir.ORSH, types.TINT8, types.TUINT8}:   ssa.OpRsh8x8,
  2770  	{ir.ORSH, types.TUINT8, types.TUINT8}:  ssa.OpRsh8Ux8,
  2771  	{ir.ORSH, types.TINT8, types.TUINT16}:  ssa.OpRsh8x16,
  2772  	{ir.ORSH, types.TUINT8, types.TUINT16}: ssa.OpRsh8Ux16,
  2773  	{ir.ORSH, types.TINT8, types.TUINT32}:  ssa.OpRsh8x32,
  2774  	{ir.ORSH, types.TUINT8, types.TUINT32}: ssa.OpRsh8Ux32,
  2775  	{ir.ORSH, types.TINT8, types.TUINT64}:  ssa.OpRsh8x64,
  2776  	{ir.ORSH, types.TUINT8, types.TUINT64}: ssa.OpRsh8Ux64,
  2777  
  2778  	{ir.ORSH, types.TINT16, types.TUINT8}:   ssa.OpRsh16x8,
  2779  	{ir.ORSH, types.TUINT16, types.TUINT8}:  ssa.OpRsh16Ux8,
  2780  	{ir.ORSH, types.TINT16, types.TUINT16}:  ssa.OpRsh16x16,
  2781  	{ir.ORSH, types.TUINT16, types.TUINT16}: ssa.OpRsh16Ux16,
  2782  	{ir.ORSH, types.TINT16, types.TUINT32}:  ssa.OpRsh16x32,
  2783  	{ir.ORSH, types.TUINT16, types.TUINT32}: ssa.OpRsh16Ux32,
  2784  	{ir.ORSH, types.TINT16, types.TUINT64}:  ssa.OpRsh16x64,
  2785  	{ir.ORSH, types.TUINT16, types.TUINT64}: ssa.OpRsh16Ux64,
  2786  
  2787  	{ir.ORSH, types.TINT32, types.TUINT8}:   ssa.OpRsh32x8,
  2788  	{ir.ORSH, types.TUINT32, types.TUINT8}:  ssa.OpRsh32Ux8,
  2789  	{ir.ORSH, types.TINT32, types.TUINT16}:  ssa.OpRsh32x16,
  2790  	{ir.ORSH, types.TUINT32, types.TUINT16}: ssa.OpRsh32Ux16,
  2791  	{ir.ORSH, types.TINT32, types.TUINT32}:  ssa.OpRsh32x32,
  2792  	{ir.ORSH, types.TUINT32, types.TUINT32}: ssa.OpRsh32Ux32,
  2793  	{ir.ORSH, types.TINT32, types.TUINT64}:  ssa.OpRsh32x64,
  2794  	{ir.ORSH, types.TUINT32, types.TUINT64}: ssa.OpRsh32Ux64,
  2795  
  2796  	{ir.ORSH, types.TINT64, types.TUINT8}:   ssa.OpRsh64x8,
  2797  	{ir.ORSH, types.TUINT64, types.TUINT8}:  ssa.OpRsh64Ux8,
  2798  	{ir.ORSH, types.TINT64, types.TUINT16}:  ssa.OpRsh64x16,
  2799  	{ir.ORSH, types.TUINT64, types.TUINT16}: ssa.OpRsh64Ux16,
  2800  	{ir.ORSH, types.TINT64, types.TUINT32}:  ssa.OpRsh64x32,
  2801  	{ir.ORSH, types.TUINT64, types.TUINT32}: ssa.OpRsh64Ux32,
  2802  	{ir.ORSH, types.TINT64, types.TUINT64}:  ssa.OpRsh64x64,
  2803  	{ir.ORSH, types.TUINT64, types.TUINT64}: ssa.OpRsh64Ux64,
  2804  }
  2805  
  2806  func (s *state) ssaShiftOp(op ir.Op, t *types.Type, u *types.Type) ssa.Op {
  2807  	etype1 := s.concreteEtype(t)
  2808  	etype2 := s.concreteEtype(u)
  2809  	x, ok := shiftOpToSSA[opAndTwoTypes{op, etype1, etype2}]
  2810  	if !ok {
  2811  		s.Fatalf("unhandled shift op %v etype=%s/%s", op, etype1, etype2)
  2812  	}
  2813  	return x
  2814  }
  2815  
  2816  func (s *state) uintptrConstant(v uint64) *ssa.Value {
  2817  	if s.config.PtrSize == 4 {
  2818  		return s.newValue0I(ssa.OpConst32, types.Types[types.TUINTPTR], int64(v))
  2819  	}
  2820  	return s.newValue0I(ssa.OpConst64, types.Types[types.TUINTPTR], int64(v))
  2821  }
  2822  
  2823  func (s *state) conv(n ir.Node, v *ssa.Value, ft, tt *types.Type) *ssa.Value {
  2824  	if ft.IsBoolean() && tt.IsKind(types.TUINT8) {
  2825  		// Bool -> uint8 is generated internally when indexing into runtime.staticbyte.
  2826  		return s.newValue1(ssa.OpCvtBoolToUint8, tt, v)
  2827  	}
  2828  	if ft.IsInteger() && tt.IsInteger() {
  2829  		var op ssa.Op
  2830  		if tt.Size() == ft.Size() {
  2831  			op = ssa.OpCopy
  2832  		} else if tt.Size() < ft.Size() {
  2833  			// truncation
  2834  			switch 10*ft.Size() + tt.Size() {
  2835  			case 21:
  2836  				op = ssa.OpTrunc16to8
  2837  			case 41:
  2838  				op = ssa.OpTrunc32to8
  2839  			case 42:
  2840  				op = ssa.OpTrunc32to16
  2841  			case 81:
  2842  				op = ssa.OpTrunc64to8
  2843  			case 82:
  2844  				op = ssa.OpTrunc64to16
  2845  			case 84:
  2846  				op = ssa.OpTrunc64to32
  2847  			default:
  2848  				s.Fatalf("weird integer truncation %v -> %v", ft, tt)
  2849  			}
  2850  		} else if ft.IsSigned() {
  2851  			// sign extension
  2852  			switch 10*ft.Size() + tt.Size() {
  2853  			case 12:
  2854  				op = ssa.OpSignExt8to16
  2855  			case 14:
  2856  				op = ssa.OpSignExt8to32
  2857  			case 18:
  2858  				op = ssa.OpSignExt8to64
  2859  			case 24:
  2860  				op = ssa.OpSignExt16to32
  2861  			case 28:
  2862  				op = ssa.OpSignExt16to64
  2863  			case 48:
  2864  				op = ssa.OpSignExt32to64
  2865  			default:
  2866  				s.Fatalf("bad integer sign extension %v -> %v", ft, tt)
  2867  			}
  2868  		} else {
  2869  			// zero extension
  2870  			switch 10*ft.Size() + tt.Size() {
  2871  			case 12:
  2872  				op = ssa.OpZeroExt8to16
  2873  			case 14:
  2874  				op = ssa.OpZeroExt8to32
  2875  			case 18:
  2876  				op = ssa.OpZeroExt8to64
  2877  			case 24:
  2878  				op = ssa.OpZeroExt16to32
  2879  			case 28:
  2880  				op = ssa.OpZeroExt16to64
  2881  			case 48:
  2882  				op = ssa.OpZeroExt32to64
  2883  			default:
  2884  				s.Fatalf("weird integer sign extension %v -> %v", ft, tt)
  2885  			}
  2886  		}
  2887  		return s.newValue1(op, tt, v)
  2888  	}
  2889  
  2890  	if ft.IsComplex() && tt.IsComplex() {
  2891  		var op ssa.Op
  2892  		if ft.Size() == tt.Size() {
  2893  			switch ft.Size() {
  2894  			case 8:
  2895  				op = ssa.OpRound32F
  2896  			case 16:
  2897  				op = ssa.OpRound64F
  2898  			default:
  2899  				s.Fatalf("weird complex conversion %v -> %v", ft, tt)
  2900  			}
  2901  		} else if ft.Size() == 8 && tt.Size() == 16 {
  2902  			op = ssa.OpCvt32Fto64F
  2903  		} else if ft.Size() == 16 && tt.Size() == 8 {
  2904  			op = ssa.OpCvt64Fto32F
  2905  		} else {
  2906  			s.Fatalf("weird complex conversion %v -> %v", ft, tt)
  2907  		}
  2908  		ftp := types.FloatForComplex(ft)
  2909  		ttp := types.FloatForComplex(tt)
  2910  		return s.newValue2(ssa.OpComplexMake, tt,
  2911  			s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexReal, ftp, v)),
  2912  			s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexImag, ftp, v)))
  2913  	}
  2914  
  2915  	if tt.IsComplex() { // and ft is not complex
  2916  		// Needed for generics support - can't happen in normal Go code.
  2917  		et := types.FloatForComplex(tt)
  2918  		v = s.conv(n, v, ft, et)
  2919  		return s.newValue2(ssa.OpComplexMake, tt, v, s.zeroVal(et))
  2920  	}
  2921  
  2922  	if ft.IsFloat() || tt.IsFloat() {
  2923  		cft, ctt := s.concreteEtype(ft), s.concreteEtype(tt)
  2924  		conv, ok := fpConvOpToSSA[twoTypes{cft, ctt}]
  2925  		// there's a change to a conversion-op table, this restores the old behavior if ConvertHash is false.
  2926  		// use salted hash to distinguish unsigned convert at a Pos from signed convert at a Pos
  2927  		if ctt == types.TUINT32 && ft.IsFloat() && !base.ConvertHash.MatchPosWithInfo(n.Pos(), "U", nil) {
  2928  			// revert to old behavior
  2929  			conv.op1 = ssa.OpCvt64Fto64
  2930  			if cft == types.TFLOAT32 {
  2931  				conv.op1 = ssa.OpCvt32Fto64
  2932  			}
  2933  			conv.op2 = ssa.OpTrunc64to32
  2934  
  2935  		}
  2936  		if s.config.RegSize == 4 && Arch.LinkArch.Family != sys.MIPS && !s.softFloat {
  2937  			if conv1, ok1 := fpConvOpToSSA32[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 {
  2938  				conv = conv1
  2939  			}
  2940  		}
  2941  		if Arch.LinkArch.Family == sys.ARM64 || Arch.LinkArch.Family == sys.Wasm || Arch.LinkArch.Family == sys.S390X || s.softFloat {
  2942  			if conv1, ok1 := uint64fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 {
  2943  				conv = conv1
  2944  			}
  2945  		}
  2946  
  2947  		if Arch.LinkArch.Family == sys.MIPS && !s.softFloat {
  2948  			if ft.Size() == 4 && ft.IsInteger() && !ft.IsSigned() {
  2949  				// tt is float32 or float64, and ft is also unsigned
  2950  				if tt.Size() == 4 {
  2951  					return s.uint32Tofloat32(n, v, ft, tt)
  2952  				}
  2953  				if tt.Size() == 8 {
  2954  					return s.uint32Tofloat64(n, v, ft, tt)
  2955  				}
  2956  			} else if tt.Size() == 4 && tt.IsInteger() && !tt.IsSigned() {
  2957  				// ft is float32 or float64, and tt is unsigned integer
  2958  				if ft.Size() == 4 {
  2959  					return s.float32ToUint32(n, v, ft, tt)
  2960  				}
  2961  				if ft.Size() == 8 {
  2962  					return s.float64ToUint32(n, v, ft, tt)
  2963  				}
  2964  			}
  2965  		}
  2966  
  2967  		if !ok {
  2968  			s.Fatalf("weird float conversion %v -> %v", ft, tt)
  2969  		}
  2970  		op1, op2, it := conv.op1, conv.op2, conv.intermediateType
  2971  
  2972  		if op1 != ssa.OpInvalid && op2 != ssa.OpInvalid {
  2973  			// normal case, not tripping over unsigned 64
  2974  			if op1 == ssa.OpCopy {
  2975  				if op2 == ssa.OpCopy {
  2976  					return v
  2977  				}
  2978  				return s.newValueOrSfCall1(op2, tt, v)
  2979  			}
  2980  			if op2 == ssa.OpCopy {
  2981  				return s.newValueOrSfCall1(op1, tt, v)
  2982  			}
  2983  			return s.newValueOrSfCall1(op2, tt, s.newValueOrSfCall1(op1, types.Types[it], v))
  2984  		}
  2985  		// Tricky 64-bit unsigned cases.
  2986  		if ft.IsInteger() {
  2987  			// tt is float32 or float64, and ft is also unsigned
  2988  			if tt.Size() == 4 {
  2989  				return s.uint64Tofloat32(n, v, ft, tt)
  2990  			}
  2991  			if tt.Size() == 8 {
  2992  				return s.uint64Tofloat64(n, v, ft, tt)
  2993  			}
  2994  			s.Fatalf("weird unsigned integer to float conversion %v -> %v", ft, tt)
  2995  		}
  2996  		// ft is float32 or float64, and tt is unsigned integer
  2997  		if ft.Size() == 4 {
  2998  			switch tt.Size() {
  2999  			case 8:
  3000  				return s.float32ToUint64(n, v, ft, tt)
  3001  			case 4, 2, 1:
  3002  				// TODO should 2 and 1 saturate or truncate?
  3003  				return s.float32ToUint32(n, v, ft, tt)
  3004  			}
  3005  		}
  3006  		if ft.Size() == 8 {
  3007  			switch tt.Size() {
  3008  			case 8:
  3009  				return s.float64ToUint64(n, v, ft, tt)
  3010  			case 4, 2, 1:
  3011  				// TODO should 2 and 1 saturate or truncate?
  3012  				return s.float64ToUint32(n, v, ft, tt)
  3013  			}
  3014  
  3015  		}
  3016  		s.Fatalf("weird float to unsigned integer conversion %v -> %v", ft, tt)
  3017  		return nil
  3018  	}
  3019  
  3020  	s.Fatalf("unhandled OCONV %s -> %s", ft.Kind(), tt.Kind())
  3021  	return nil
  3022  }
  3023  
  3024  // expr converts the expression n to ssa, adds it to s and returns the ssa result.
  3025  func (s *state) expr(n ir.Node) *ssa.Value {
  3026  	return s.exprCheckPtr(n, true)
  3027  }
  3028  
  3029  func (s *state) exprCheckPtr(n ir.Node, checkPtrOK bool) *ssa.Value {
  3030  	if ir.HasUniquePos(n) {
  3031  		// ONAMEs and named OLITERALs have the line number
  3032  		// of the decl, not the use. See issue 14742.
  3033  		s.pushLine(n.Pos())
  3034  		defer s.popLine()
  3035  	}
  3036  
  3037  	s.stmtList(n.Init())
  3038  	switch n.Op() {
  3039  	case ir.OBYTES2STRTMP:
  3040  		n := n.(*ir.ConvExpr)
  3041  		slice := s.expr(n.X)
  3042  		ptr := s.newValue1(ssa.OpSlicePtr, s.f.Config.Types.BytePtr, slice)
  3043  		len := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], slice)
  3044  		return s.newValue2(ssa.OpStringMake, n.Type(), ptr, len)
  3045  	case ir.OSTR2BYTESTMP:
  3046  		n := n.(*ir.ConvExpr)
  3047  		str := s.expr(n.X)
  3048  		ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, str)
  3049  		if !n.NonNil() {
  3050  			// We need to ensure []byte("") evaluates to []byte{}, and not []byte(nil).
  3051  			//
  3052  			// TODO(mdempsky): Investigate using "len != 0" instead of "ptr != nil".
  3053  			cond := s.newValue2(ssa.OpNeqPtr, types.Types[types.TBOOL], ptr, s.constNil(ptr.Type))
  3054  			zerobase := s.newValue1A(ssa.OpAddr, ptr.Type, ir.Syms.Zerobase, s.sb)
  3055  			ptr = s.ternary(cond, ptr, zerobase)
  3056  		}
  3057  		len := s.newValue1(ssa.OpStringLen, types.Types[types.TINT], str)
  3058  		return s.newValue3(ssa.OpSliceMake, n.Type(), ptr, len, len)
  3059  	case ir.OCFUNC:
  3060  		n := n.(*ir.UnaryExpr)
  3061  		aux := n.X.(*ir.Name).Linksym()
  3062  		// OCFUNC is used to build function values, which must
  3063  		// always reference ABIInternal entry points.
  3064  		if aux.ABI() != obj.ABIInternal {
  3065  			s.Fatalf("expected ABIInternal: %v", aux.ABI())
  3066  		}
  3067  		return s.entryNewValue1A(ssa.OpAddr, n.Type(), aux, s.sb)
  3068  	case ir.ONAME:
  3069  		n := n.(*ir.Name)
  3070  		if n.Class == ir.PFUNC {
  3071  			// "value" of a function is the address of the function's closure
  3072  			sym := staticdata.FuncLinksym(n)
  3073  			return s.entryNewValue1A(ssa.OpAddr, types.NewPtr(n.Type()), sym, s.sb)
  3074  		}
  3075  		if s.canSSA(n) {
  3076  			return s.variable(n, n.Type())
  3077  		}
  3078  		return s.load(n.Type(), s.addr(n))
  3079  	case ir.OLINKSYMOFFSET:
  3080  		n := n.(*ir.LinksymOffsetExpr)
  3081  		return s.load(n.Type(), s.addr(n))
  3082  	case ir.ONIL:
  3083  		n := n.(*ir.NilExpr)
  3084  		t := n.Type()
  3085  		switch {
  3086  		case t.IsSlice():
  3087  			return s.constSlice(t)
  3088  		case t.IsInterface():
  3089  			return s.constInterface(t)
  3090  		default:
  3091  			return s.constNil(t)
  3092  		}
  3093  	case ir.OLITERAL:
  3094  		switch u := n.Val(); u.Kind() {
  3095  		case constant.Int:
  3096  			i := ir.IntVal(n.Type(), u)
  3097  			switch n.Type().Size() {
  3098  			case 1:
  3099  				return s.constInt8(n.Type(), int8(i))
  3100  			case 2:
  3101  				return s.constInt16(n.Type(), int16(i))
  3102  			case 4:
  3103  				return s.constInt32(n.Type(), int32(i))
  3104  			case 8:
  3105  				return s.constInt64(n.Type(), i)
  3106  			default:
  3107  				s.Fatalf("bad integer size %d", n.Type().Size())
  3108  				return nil
  3109  			}
  3110  		case constant.String:
  3111  			i := constant.StringVal(u)
  3112  			if i == "" {
  3113  				return s.constEmptyString(n.Type())
  3114  			}
  3115  			return s.entryNewValue0A(ssa.OpConstString, n.Type(), ssa.StringToAux(i))
  3116  		case constant.Bool:
  3117  			return s.constBool(constant.BoolVal(u))
  3118  		case constant.Float:
  3119  			f, _ := constant.Float64Val(u)
  3120  			switch n.Type().Size() {
  3121  			case 4:
  3122  				return s.constFloat32(n.Type(), f)
  3123  			case 8:
  3124  				return s.constFloat64(n.Type(), f)
  3125  			default:
  3126  				s.Fatalf("bad float size %d", n.Type().Size())
  3127  				return nil
  3128  			}
  3129  		case constant.Complex:
  3130  			re, _ := constant.Float64Val(constant.Real(u))
  3131  			im, _ := constant.Float64Val(constant.Imag(u))
  3132  			switch n.Type().Size() {
  3133  			case 8:
  3134  				pt := types.Types[types.TFLOAT32]
  3135  				return s.newValue2(ssa.OpComplexMake, n.Type(),
  3136  					s.constFloat32(pt, re),
  3137  					s.constFloat32(pt, im))
  3138  			case 16:
  3139  				pt := types.Types[types.TFLOAT64]
  3140  				return s.newValue2(ssa.OpComplexMake, n.Type(),
  3141  					s.constFloat64(pt, re),
  3142  					s.constFloat64(pt, im))
  3143  			default:
  3144  				s.Fatalf("bad complex size %d", n.Type().Size())
  3145  				return nil
  3146  			}
  3147  		default:
  3148  			s.Fatalf("unhandled OLITERAL %v", u.Kind())
  3149  			return nil
  3150  		}
  3151  	case ir.OCONVNOP:
  3152  		n := n.(*ir.ConvExpr)
  3153  		to := n.Type()
  3154  		from := n.X.Type()
  3155  
  3156  		// Assume everything will work out, so set up our return value.
  3157  		// Anything interesting that happens from here is a fatal.
  3158  		x := s.expr(n.X)
  3159  		if to == from {
  3160  			return x
  3161  		}
  3162  
  3163  		// Special case for not confusing GC and liveness.
  3164  		// We don't want pointers accidentally classified
  3165  		// as not-pointers or vice-versa because of copy
  3166  		// elision.
  3167  		if to.IsPtrShaped() != from.IsPtrShaped() {
  3168  			return s.newValue2(ssa.OpConvert, to, x, s.mem())
  3169  		}
  3170  
  3171  		v := s.newValue1(ssa.OpCopy, to, x) // ensure that v has the right type
  3172  
  3173  		// CONVNOP closure
  3174  		if to.Kind() == types.TFUNC && from.IsPtrShaped() {
  3175  			return v
  3176  		}
  3177  
  3178  		// named <--> unnamed type or typed <--> untyped const
  3179  		if from.Kind() == to.Kind() {
  3180  			return v
  3181  		}
  3182  
  3183  		// unsafe.Pointer <--> *T
  3184  		if to.IsUnsafePtr() && from.IsPtrShaped() || from.IsUnsafePtr() && to.IsPtrShaped() {
  3185  			if s.checkPtrEnabled && checkPtrOK && to.IsPtr() && from.IsUnsafePtr() {
  3186  				s.checkPtrAlignment(n, v, nil)
  3187  			}
  3188  			return v
  3189  		}
  3190  
  3191  		// map <--> *internal/runtime/maps.Map
  3192  		mt := types.NewPtr(reflectdata.MapType())
  3193  		if to.Kind() == types.TMAP && from == mt {
  3194  			return v
  3195  		}
  3196  
  3197  		types.CalcSize(from)
  3198  		types.CalcSize(to)
  3199  		if from.Size() != to.Size() {
  3200  			s.Fatalf("CONVNOP width mismatch %v (%d) -> %v (%d)\n", from, from.Size(), to, to.Size())
  3201  			return nil
  3202  		}
  3203  		if etypesign(from.Kind()) != etypesign(to.Kind()) {
  3204  			s.Fatalf("CONVNOP sign mismatch %v (%s) -> %v (%s)\n", from, from.Kind(), to, to.Kind())
  3205  			return nil
  3206  		}
  3207  
  3208  		if base.Flag.Cfg.Instrumenting {
  3209  			// These appear to be fine, but they fail the
  3210  			// integer constraint below, so okay them here.
  3211  			// Sample non-integer conversion: map[string]string -> *uint8
  3212  			return v
  3213  		}
  3214  
  3215  		if etypesign(from.Kind()) == 0 {
  3216  			s.Fatalf("CONVNOP unrecognized non-integer %v -> %v\n", from, to)
  3217  			return nil
  3218  		}
  3219  
  3220  		// integer, same width, same sign
  3221  		return v
  3222  
  3223  	case ir.OCONV:
  3224  		n := n.(*ir.ConvExpr)
  3225  		x := s.expr(n.X)
  3226  		return s.conv(n, x, n.X.Type(), n.Type())
  3227  
  3228  	case ir.ODOTTYPE:
  3229  		n := n.(*ir.TypeAssertExpr)
  3230  		res, _ := s.dottype(n, false)
  3231  		return res
  3232  
  3233  	case ir.ODYNAMICDOTTYPE:
  3234  		n := n.(*ir.DynamicTypeAssertExpr)
  3235  		res, _ := s.dynamicDottype(n, false)
  3236  		return res
  3237  
  3238  	// binary ops
  3239  	case ir.OLT, ir.OEQ, ir.ONE, ir.OLE, ir.OGE, ir.OGT:
  3240  		n := n.(*ir.BinaryExpr)
  3241  		a := s.expr(n.X)
  3242  		b := s.expr(n.Y)
  3243  		if n.X.Type().IsComplex() {
  3244  			pt := types.FloatForComplex(n.X.Type())
  3245  			op := s.ssaOp(ir.OEQ, pt)
  3246  			r := s.newValueOrSfCall2(op, types.Types[types.TBOOL], s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b))
  3247  			i := s.newValueOrSfCall2(op, types.Types[types.TBOOL], s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))
  3248  			c := s.newValue2(ssa.OpAndB, types.Types[types.TBOOL], r, i)
  3249  			switch n.Op() {
  3250  			case ir.OEQ:
  3251  				return c
  3252  			case ir.ONE:
  3253  				return s.newValue1(ssa.OpNot, types.Types[types.TBOOL], c)
  3254  			default:
  3255  				s.Fatalf("ordered complex compare %v", n.Op())
  3256  			}
  3257  		}
  3258  
  3259  		// Convert OGE and OGT into OLE and OLT.
  3260  		op := n.Op()
  3261  		switch op {
  3262  		case ir.OGE:
  3263  			op, a, b = ir.OLE, b, a
  3264  		case ir.OGT:
  3265  			op, a, b = ir.OLT, b, a
  3266  		}
  3267  		if n.X.Type().IsFloat() {
  3268  			// float comparison
  3269  			return s.newValueOrSfCall2(s.ssaOp(op, n.X.Type()), types.Types[types.TBOOL], a, b)
  3270  		}
  3271  		// integer comparison
  3272  		return s.newValue2(s.ssaOp(op, n.X.Type()), types.Types[types.TBOOL], a, b)
  3273  	case ir.OMUL:
  3274  		n := n.(*ir.BinaryExpr)
  3275  		a := s.expr(n.X)
  3276  		b := s.expr(n.Y)
  3277  		if n.Type().IsComplex() {
  3278  			mulop := ssa.OpMul64F
  3279  			addop := ssa.OpAdd64F
  3280  			subop := ssa.OpSub64F
  3281  			pt := types.FloatForComplex(n.Type()) // Could be Float32 or Float64
  3282  			wt := types.Types[types.TFLOAT64]     // Compute in Float64 to minimize cancellation error
  3283  
  3284  			areal := s.newValue1(ssa.OpComplexReal, pt, a)
  3285  			breal := s.newValue1(ssa.OpComplexReal, pt, b)
  3286  			aimag := s.newValue1(ssa.OpComplexImag, pt, a)
  3287  			bimag := s.newValue1(ssa.OpComplexImag, pt, b)
  3288  
  3289  			if pt != wt { // Widen for calculation
  3290  				areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal)
  3291  				breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal)
  3292  				aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag)
  3293  				bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag)
  3294  			}
  3295  
  3296  			xreal := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag))
  3297  			ximag := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, bimag), s.newValueOrSfCall2(mulop, wt, aimag, breal))
  3298  
  3299  			if pt != wt { // Narrow to store back
  3300  				xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal)
  3301  				ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag)
  3302  			}
  3303  
  3304  			return s.newValue2(ssa.OpComplexMake, n.Type(), xreal, ximag)
  3305  		}
  3306  
  3307  		if n.Type().IsFloat() {
  3308  			return s.newValueOrSfCall2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3309  		}
  3310  
  3311  		return s.newValue2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3312  
  3313  	case ir.ODIV:
  3314  		n := n.(*ir.BinaryExpr)
  3315  		a := s.expr(n.X)
  3316  		b := s.expr(n.Y)
  3317  		if n.Type().IsComplex() {
  3318  			// TODO this is not executed because the front-end substitutes a runtime call.
  3319  			// That probably ought to change; with modest optimization the widen/narrow
  3320  			// conversions could all be elided in larger expression trees.
  3321  			mulop := ssa.OpMul64F
  3322  			addop := ssa.OpAdd64F
  3323  			subop := ssa.OpSub64F
  3324  			divop := ssa.OpDiv64F
  3325  			pt := types.FloatForComplex(n.Type()) // Could be Float32 or Float64
  3326  			wt := types.Types[types.TFLOAT64]     // Compute in Float64 to minimize cancellation error
  3327  
  3328  			areal := s.newValue1(ssa.OpComplexReal, pt, a)
  3329  			breal := s.newValue1(ssa.OpComplexReal, pt, b)
  3330  			aimag := s.newValue1(ssa.OpComplexImag, pt, a)
  3331  			bimag := s.newValue1(ssa.OpComplexImag, pt, b)
  3332  
  3333  			if pt != wt { // Widen for calculation
  3334  				areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal)
  3335  				breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal)
  3336  				aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag)
  3337  				bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag)
  3338  			}
  3339  
  3340  			denom := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, breal, breal), s.newValueOrSfCall2(mulop, wt, bimag, bimag))
  3341  			xreal := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag))
  3342  			ximag := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, aimag, breal), s.newValueOrSfCall2(mulop, wt, areal, bimag))
  3343  
  3344  			// TODO not sure if this is best done in wide precision or narrow
  3345  			// Double-rounding might be an issue.
  3346  			// Note that the pre-SSA implementation does the entire calculation
  3347  			// in wide format, so wide is compatible.
  3348  			xreal = s.newValueOrSfCall2(divop, wt, xreal, denom)
  3349  			ximag = s.newValueOrSfCall2(divop, wt, ximag, denom)
  3350  
  3351  			if pt != wt { // Narrow to store back
  3352  				xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal)
  3353  				ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag)
  3354  			}
  3355  			return s.newValue2(ssa.OpComplexMake, n.Type(), xreal, ximag)
  3356  		}
  3357  		if n.Type().IsFloat() {
  3358  			return s.newValueOrSfCall2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3359  		}
  3360  		return s.intDivide(n, a, b)
  3361  	case ir.OMOD:
  3362  		n := n.(*ir.BinaryExpr)
  3363  		a := s.expr(n.X)
  3364  		b := s.expr(n.Y)
  3365  		return s.intDivide(n, a, b)
  3366  	case ir.OADD, ir.OSUB:
  3367  		n := n.(*ir.BinaryExpr)
  3368  		a := s.expr(n.X)
  3369  		b := s.expr(n.Y)
  3370  		if n.Type().IsComplex() {
  3371  			pt := types.FloatForComplex(n.Type())
  3372  			op := s.ssaOp(n.Op(), pt)
  3373  			return s.newValue2(ssa.OpComplexMake, n.Type(),
  3374  				s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)),
  3375  				s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)))
  3376  		}
  3377  		if n.Type().IsFloat() {
  3378  			return s.newValueOrSfCall2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3379  		}
  3380  		return s.newValue2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3381  	case ir.OAND, ir.OOR, ir.OXOR:
  3382  		n := n.(*ir.BinaryExpr)
  3383  		a := s.expr(n.X)
  3384  		b := s.expr(n.Y)
  3385  		return s.newValue2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  3386  	case ir.OANDNOT:
  3387  		n := n.(*ir.BinaryExpr)
  3388  		a := s.expr(n.X)
  3389  		b := s.expr(n.Y)
  3390  		b = s.newValue1(s.ssaOp(ir.OBITNOT, b.Type), b.Type, b)
  3391  		return s.newValue2(s.ssaOp(ir.OAND, n.Type()), a.Type, a, b)
  3392  	case ir.OLSH, ir.ORSH:
  3393  		n := n.(*ir.BinaryExpr)
  3394  		a := s.expr(n.X)
  3395  		b := s.expr(n.Y)
  3396  		bt := b.Type
  3397  		if bt.IsSigned() {
  3398  			cmp := s.newValue2(s.ssaOp(ir.OLE, bt), types.Types[types.TBOOL], s.zeroVal(bt), b)
  3399  			s.check(cmp, ir.Syms.Panicshift)
  3400  			bt = bt.ToUnsigned()
  3401  		}
  3402  		return s.newValue2(s.ssaShiftOp(n.Op(), n.Type(), bt), a.Type, a, b)
  3403  	case ir.OANDAND, ir.OOROR:
  3404  		// To implement OANDAND (and OOROR), we introduce a
  3405  		// new temporary variable to hold the result. The
  3406  		// variable is associated with the OANDAND node in the
  3407  		// s.vars table (normally variables are only
  3408  		// associated with ONAME nodes). We convert
  3409  		//     A && B
  3410  		// to
  3411  		//     var = A
  3412  		//     if var {
  3413  		//         var = B
  3414  		//     }
  3415  		// Using var in the subsequent block introduces the
  3416  		// necessary phi variable.
  3417  		n := n.(*ir.LogicalExpr)
  3418  		el := s.expr(n.X)
  3419  		s.vars[n] = el
  3420  
  3421  		b := s.endBlock()
  3422  		b.Kind = ssa.BlockIf
  3423  		b.SetControl(el)
  3424  		// In theory, we should set b.Likely here based on context.
  3425  		// However, gc only gives us likeliness hints
  3426  		// in a single place, for plain OIF statements,
  3427  		// and passing around context is finicky, so don't bother for now.
  3428  
  3429  		bRight := s.f.NewBlock(ssa.BlockPlain)
  3430  		bResult := s.f.NewBlock(ssa.BlockPlain)
  3431  		if n.Op() == ir.OANDAND {
  3432  			b.AddEdgeTo(bRight)
  3433  			b.AddEdgeTo(bResult)
  3434  		} else if n.Op() == ir.OOROR {
  3435  			b.AddEdgeTo(bResult)
  3436  			b.AddEdgeTo(bRight)
  3437  		}
  3438  
  3439  		s.startBlock(bRight)
  3440  		er := s.expr(n.Y)
  3441  		s.vars[n] = er
  3442  
  3443  		b = s.endBlock()
  3444  		b.AddEdgeTo(bResult)
  3445  
  3446  		s.startBlock(bResult)
  3447  		return s.variable(n, types.Types[types.TBOOL])
  3448  	case ir.OCOMPLEX:
  3449  		n := n.(*ir.BinaryExpr)
  3450  		r := s.expr(n.X)
  3451  		i := s.expr(n.Y)
  3452  		return s.newValue2(ssa.OpComplexMake, n.Type(), r, i)
  3453  
  3454  	// unary ops
  3455  	case ir.ONEG:
  3456  		n := n.(*ir.UnaryExpr)
  3457  		a := s.expr(n.X)
  3458  		if n.Type().IsComplex() {
  3459  			tp := types.FloatForComplex(n.Type())
  3460  			negop := s.ssaOp(n.Op(), tp)
  3461  			return s.newValue2(ssa.OpComplexMake, n.Type(),
  3462  				s.newValue1(negop, tp, s.newValue1(ssa.OpComplexReal, tp, a)),
  3463  				s.newValue1(negop, tp, s.newValue1(ssa.OpComplexImag, tp, a)))
  3464  		}
  3465  		return s.newValue1(s.ssaOp(n.Op(), n.Type()), a.Type, a)
  3466  	case ir.ONOT, ir.OBITNOT:
  3467  		n := n.(*ir.UnaryExpr)
  3468  		a := s.expr(n.X)
  3469  		return s.newValue1(s.ssaOp(n.Op(), n.Type()), a.Type, a)
  3470  	case ir.OIMAG, ir.OREAL:
  3471  		n := n.(*ir.UnaryExpr)
  3472  		a := s.expr(n.X)
  3473  		return s.newValue1(s.ssaOp(n.Op(), n.X.Type()), n.Type(), a)
  3474  	case ir.OPLUS:
  3475  		n := n.(*ir.UnaryExpr)
  3476  		return s.expr(n.X)
  3477  
  3478  	case ir.OADDR:
  3479  		n := n.(*ir.AddrExpr)
  3480  		return s.addr(n.X)
  3481  
  3482  	case ir.ORESULT:
  3483  		n := n.(*ir.ResultExpr)
  3484  		if s.prevCall == nil || s.prevCall.Op != ssa.OpStaticLECall && s.prevCall.Op != ssa.OpInterLECall && s.prevCall.Op != ssa.OpClosureLECall {
  3485  			panic("Expected to see a previous call")
  3486  		}
  3487  		which := n.Index
  3488  		if which == -1 {
  3489  			panic(fmt.Errorf("ORESULT %v does not match call %s", n, s.prevCall))
  3490  		}
  3491  		return s.resultOfCall(s.prevCall, which, n.Type())
  3492  
  3493  	case ir.ODEREF:
  3494  		n := n.(*ir.StarExpr)
  3495  		p := s.exprPtr(n.X, n.Bounded(), n.Pos())
  3496  		return s.load(n.Type(), p)
  3497  
  3498  	case ir.ODOT:
  3499  		n := n.(*ir.SelectorExpr)
  3500  		if n.X.Op() == ir.OSTRUCTLIT {
  3501  			// All literals with nonzero fields have already been
  3502  			// rewritten during walk. Any that remain are just T{}
  3503  			// or equivalents. Use the zero value.
  3504  			if !ir.IsZero(n.X) {
  3505  				s.Fatalf("literal with nonzero value in SSA: %v", n.X)
  3506  			}
  3507  			return s.zeroVal(n.Type())
  3508  		}
  3509  		// If n is addressable and can't be represented in
  3510  		// SSA, then load just the selected field. This
  3511  		// prevents false memory dependencies in race/msan/asan
  3512  		// instrumentation.
  3513  		if ir.IsAddressable(n) && !s.canSSA(n) {
  3514  			p := s.addr(n)
  3515  			return s.load(n.Type(), p)
  3516  		}
  3517  		v := s.expr(n.X)
  3518  		return s.newValue1I(ssa.OpStructSelect, n.Type(), int64(fieldIdx(n)), v)
  3519  
  3520  	case ir.ODOTPTR:
  3521  		n := n.(*ir.SelectorExpr)
  3522  		p := s.exprPtr(n.X, n.Bounded(), n.Pos())
  3523  		p = s.newValue1I(ssa.OpOffPtr, types.NewPtr(n.Type()), n.Offset(), p)
  3524  		return s.load(n.Type(), p)
  3525  
  3526  	case ir.OINDEX:
  3527  		n := n.(*ir.IndexExpr)
  3528  		switch {
  3529  		case n.X.Type().IsString():
  3530  			if n.Bounded() && ir.IsConst(n.X, constant.String) && ir.IsConst(n.Index, constant.Int) {
  3531  				// Replace "abc"[1] with 'b'.
  3532  				// Delayed until now because "abc"[1] is not an ideal constant.
  3533  				// See test/fixedbugs/issue11370.go.
  3534  				return s.newValue0I(ssa.OpConst8, types.Types[types.TUINT8], int64(int8(ir.StringVal(n.X)[ir.Int64Val(n.Index)])))
  3535  			}
  3536  			a := s.expr(n.X)
  3537  			i := s.expr(n.Index)
  3538  			len := s.newValue1(ssa.OpStringLen, types.Types[types.TINT], a)
  3539  			i = s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded())
  3540  			ptrtyp := s.f.Config.Types.BytePtr
  3541  			ptr := s.newValue1(ssa.OpStringPtr, ptrtyp, a)
  3542  			if ir.IsConst(n.Index, constant.Int) {
  3543  				ptr = s.newValue1I(ssa.OpOffPtr, ptrtyp, ir.Int64Val(n.Index), ptr)
  3544  			} else {
  3545  				ptr = s.newValue2(ssa.OpAddPtr, ptrtyp, ptr, i)
  3546  			}
  3547  			return s.load(types.Types[types.TUINT8], ptr)
  3548  		case n.X.Type().IsSlice():
  3549  			p := s.addr(n)
  3550  			return s.load(n.X.Type().Elem(), p)
  3551  		case n.X.Type().IsArray():
  3552  			if ssa.CanSSA(n.X.Type()) {
  3553  				// SSA can handle arrays of length at most 1.
  3554  				bound := n.X.Type().NumElem()
  3555  				a := s.expr(n.X)
  3556  				i := s.expr(n.Index)
  3557  				if bound == 0 {
  3558  					// Bounds check will never succeed.  Might as well
  3559  					// use constants for the bounds check.
  3560  					z := s.constInt(types.Types[types.TINT], 0)
  3561  					s.boundsCheck(z, z, ssa.BoundsIndex, false)
  3562  					// The return value won't be live. In case bounds checks
  3563  					// are turned off, load from (*T)(nil) to cause a segfault.
  3564  					return s.load(n.Type(), s.constNil(n.Type().PtrTo()))
  3565  				}
  3566  				len := s.constInt(types.Types[types.TINT], bound)
  3567  				s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded()) // checks i == 0
  3568  				return s.newValue1I(ssa.OpArraySelect, n.Type(), 0, a)
  3569  			}
  3570  			p := s.addr(n)
  3571  			return s.load(n.X.Type().Elem(), p)
  3572  		default:
  3573  			s.Fatalf("bad type for index %v", n.X.Type())
  3574  			return nil
  3575  		}
  3576  
  3577  	case ir.OLEN, ir.OCAP:
  3578  		n := n.(*ir.UnaryExpr)
  3579  		// Note: all constant cases are handled by the frontend. If len or cap
  3580  		// makes it here, we want the side effects of the argument. See issue 72844.
  3581  		a := s.expr(n.X)
  3582  		t := n.X.Type()
  3583  		switch {
  3584  		case t.IsSlice():
  3585  			op := ssa.OpSliceLen
  3586  			if n.Op() == ir.OCAP {
  3587  				op = ssa.OpSliceCap
  3588  			}
  3589  			return s.newValue1(op, types.Types[types.TINT], a)
  3590  		case t.IsString(): // string; not reachable for OCAP
  3591  			return s.newValue1(ssa.OpStringLen, types.Types[types.TINT], a)
  3592  		case t.IsMap(), t.IsChan():
  3593  			return s.referenceTypeBuiltin(n, a)
  3594  		case t.IsArray():
  3595  			return s.constInt(types.Types[types.TINT], t.NumElem())
  3596  		case t.IsPtr() && t.Elem().IsArray():
  3597  			return s.constInt(types.Types[types.TINT], t.Elem().NumElem())
  3598  		default:
  3599  			s.Fatalf("bad type in len/cap: %v", t)
  3600  			return nil
  3601  		}
  3602  
  3603  	case ir.OSPTR:
  3604  		n := n.(*ir.UnaryExpr)
  3605  		a := s.expr(n.X)
  3606  		if n.X.Type().IsSlice() {
  3607  			if n.Bounded() {
  3608  				return s.newValue1(ssa.OpSlicePtr, n.Type(), a)
  3609  			}
  3610  			return s.newValue1(ssa.OpSlicePtrUnchecked, n.Type(), a)
  3611  		} else {
  3612  			return s.newValue1(ssa.OpStringPtr, n.Type(), a)
  3613  		}
  3614  
  3615  	case ir.OITAB:
  3616  		n := n.(*ir.UnaryExpr)
  3617  		a := s.expr(n.X)
  3618  		return s.newValue1(ssa.OpITab, n.Type(), a)
  3619  
  3620  	case ir.OIDATA:
  3621  		n := n.(*ir.UnaryExpr)
  3622  		a := s.expr(n.X)
  3623  		return s.newValue1(ssa.OpIData, n.Type(), a)
  3624  
  3625  	case ir.OMAKEFACE:
  3626  		n := n.(*ir.BinaryExpr)
  3627  		tab := s.expr(n.X)
  3628  		data := s.expr(n.Y)
  3629  		return s.newValue2(ssa.OpIMake, n.Type(), tab, data)
  3630  
  3631  	case ir.OSLICEHEADER:
  3632  		n := n.(*ir.SliceHeaderExpr)
  3633  		p := s.expr(n.Ptr)
  3634  		l := s.expr(n.Len)
  3635  		c := s.expr(n.Cap)
  3636  		return s.newValue3(ssa.OpSliceMake, n.Type(), p, l, c)
  3637  
  3638  	case ir.OSTRINGHEADER:
  3639  		n := n.(*ir.StringHeaderExpr)
  3640  		p := s.expr(n.Ptr)
  3641  		l := s.expr(n.Len)
  3642  		return s.newValue2(ssa.OpStringMake, n.Type(), p, l)
  3643  
  3644  	case ir.OSLICE, ir.OSLICEARR, ir.OSLICE3, ir.OSLICE3ARR:
  3645  		n := n.(*ir.SliceExpr)
  3646  		check := s.checkPtrEnabled && n.Op() == ir.OSLICE3ARR && n.X.Op() == ir.OCONVNOP && n.X.(*ir.ConvExpr).X.Type().IsUnsafePtr()
  3647  		v := s.exprCheckPtr(n.X, !check)
  3648  		var i, j, k *ssa.Value
  3649  		if n.Low != nil {
  3650  			i = s.expr(n.Low)
  3651  		}
  3652  		if n.High != nil {
  3653  			j = s.expr(n.High)
  3654  		}
  3655  		if n.Max != nil {
  3656  			k = s.expr(n.Max)
  3657  		}
  3658  		p, l, c := s.slice(v, i, j, k, n.Bounded())
  3659  		if check {
  3660  			// Emit checkptr instrumentation after bound check to prevent false positive, see #46938.
  3661  			s.checkPtrAlignment(n.X.(*ir.ConvExpr), v, s.conv(n.Max, k, k.Type, types.Types[types.TUINTPTR]))
  3662  		}
  3663  		return s.newValue3(ssa.OpSliceMake, n.Type(), p, l, c)
  3664  
  3665  	case ir.OSLICESTR:
  3666  		n := n.(*ir.SliceExpr)
  3667  		v := s.expr(n.X)
  3668  		var i, j *ssa.Value
  3669  		if n.Low != nil {
  3670  			i = s.expr(n.Low)
  3671  		}
  3672  		if n.High != nil {
  3673  			j = s.expr(n.High)
  3674  		}
  3675  		p, l, _ := s.slice(v, i, j, nil, n.Bounded())
  3676  		return s.newValue2(ssa.OpStringMake, n.Type(), p, l)
  3677  
  3678  	case ir.OSLICE2ARRPTR:
  3679  		// if arrlen > slice.len {
  3680  		//   panic(...)
  3681  		// }
  3682  		// slice.ptr
  3683  		n := n.(*ir.ConvExpr)
  3684  		v := s.expr(n.X)
  3685  		nelem := n.Type().Elem().NumElem()
  3686  		arrlen := s.constInt(types.Types[types.TINT], nelem)
  3687  		cap := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], v)
  3688  		s.boundsCheck(arrlen, cap, ssa.BoundsConvert, false)
  3689  		op := ssa.OpSlicePtr
  3690  		if nelem == 0 {
  3691  			op = ssa.OpSlicePtrUnchecked
  3692  		}
  3693  		return s.newValue1(op, n.Type(), v)
  3694  
  3695  	case ir.OCALLFUNC:
  3696  		n := n.(*ir.CallExpr)
  3697  		if ir.IsIntrinsicCall(n) {
  3698  			return s.intrinsicCall(n)
  3699  		}
  3700  		fallthrough
  3701  
  3702  	case ir.OCALLINTER:
  3703  		n := n.(*ir.CallExpr)
  3704  		return s.callResult(n, callNormal)
  3705  
  3706  	case ir.OGETG:
  3707  		n := n.(*ir.CallExpr)
  3708  		return s.newValue1(ssa.OpGetG, n.Type(), s.mem())
  3709  
  3710  	case ir.OGETCALLERSP:
  3711  		n := n.(*ir.CallExpr)
  3712  		return s.newValue1(ssa.OpGetCallerSP, n.Type(), s.mem())
  3713  
  3714  	case ir.OAPPEND:
  3715  		return s.append(n.(*ir.CallExpr), false)
  3716  
  3717  	case ir.OMOVE2HEAP:
  3718  		return s.move2heap(n.(*ir.MoveToHeapExpr))
  3719  
  3720  	case ir.OMIN, ir.OMAX:
  3721  		return s.minMax(n.(*ir.CallExpr))
  3722  
  3723  	case ir.OSTRUCTLIT, ir.OARRAYLIT:
  3724  		// All literals with nonzero fields have already been
  3725  		// rewritten during walk. Any that remain are just T{}
  3726  		// or equivalents. Use the zero value.
  3727  		n := n.(*ir.CompLitExpr)
  3728  		if !ir.IsZero(n) {
  3729  			s.Fatalf("literal with nonzero value in SSA: %v", n)
  3730  		}
  3731  		return s.zeroVal(n.Type())
  3732  
  3733  	case ir.ONEW:
  3734  		n := n.(*ir.UnaryExpr)
  3735  		if x, ok := n.X.(*ir.DynamicType); ok && x.Op() == ir.ODYNAMICTYPE {
  3736  			return s.newObjectNonSpecialized(n.Type().Elem(), s.expr(x.RType))
  3737  		}
  3738  		return s.newObject(n.Type().Elem())
  3739  
  3740  	case ir.OUNSAFEADD:
  3741  		n := n.(*ir.BinaryExpr)
  3742  		ptr := s.expr(n.X)
  3743  		len := s.expr(n.Y)
  3744  
  3745  		// Force len to uintptr to prevent misuse of garbage bits in the
  3746  		// upper part of the register (#48536).
  3747  		len = s.conv(n, len, len.Type, types.Types[types.TUINTPTR])
  3748  
  3749  		return s.newValue2(ssa.OpAddPtr, n.Type(), ptr, len)
  3750  
  3751  	default:
  3752  		s.Fatalf("unhandled expr %v", n.Op())
  3753  		return nil
  3754  	}
  3755  }
  3756  
  3757  func (s *state) resultOfCall(c *ssa.Value, which int64, t *types.Type) *ssa.Value {
  3758  	aux := c.Aux.(*ssa.AuxCall)
  3759  	pa := aux.ParamAssignmentForResult(which)
  3760  	// TODO(register args) determine if in-memory TypeOK is better loaded early from SelectNAddr or later when SelectN is expanded.
  3761  	// SelectN is better for pattern-matching and possible call-aware analysis we might want to do in the future.
  3762  	if len(pa.Registers) == 0 && !ssa.CanSSA(t) {
  3763  		addr := s.newValue1I(ssa.OpSelectNAddr, types.NewPtr(t), which, c)
  3764  		return s.rawLoad(t, addr)
  3765  	}
  3766  	return s.newValue1I(ssa.OpSelectN, t, which, c)
  3767  }
  3768  
  3769  func (s *state) resultAddrOfCall(c *ssa.Value, which int64, t *types.Type) *ssa.Value {
  3770  	aux := c.Aux.(*ssa.AuxCall)
  3771  	pa := aux.ParamAssignmentForResult(which)
  3772  	if len(pa.Registers) == 0 {
  3773  		return s.newValue1I(ssa.OpSelectNAddr, types.NewPtr(t), which, c)
  3774  	}
  3775  	_, addr := s.temp(c.Pos, t)
  3776  	rval := s.newValue1I(ssa.OpSelectN, t, which, c)
  3777  	s.vars[memVar] = s.newValue3Apos(ssa.OpStore, types.TypeMem, t, addr, rval, s.mem(), false)
  3778  	return addr
  3779  }
  3780  
  3781  // Get backing store information for an append call.
  3782  func (s *state) getBackingStoreInfoForAppend(n *ir.CallExpr) *backingStoreInfo {
  3783  	if n.Esc() != ir.EscNone {
  3784  		return nil
  3785  	}
  3786  	return s.getBackingStoreInfo(n.Args[0])
  3787  }
  3788  func (s *state) getBackingStoreInfo(n ir.Node) *backingStoreInfo {
  3789  	t := n.Type()
  3790  	et := t.Elem()
  3791  	maxStackSize := int64(base.Debug.VariableMakeThreshold)
  3792  	if et.Size() == 0 || et.Size() > maxStackSize {
  3793  		return nil
  3794  	}
  3795  	if base.Flag.N != 0 {
  3796  		return nil
  3797  	}
  3798  	if !base.VariableMakeHash.MatchPos(n.Pos(), nil) {
  3799  		return nil
  3800  	}
  3801  	i := s.backingStores[n]
  3802  	if i != nil {
  3803  		return i
  3804  	}
  3805  
  3806  	// Build type of backing store.
  3807  	K := maxStackSize / et.Size() // rounds down
  3808  	KT := types.NewArray(et, K)
  3809  	KT.SetNoalg(true)
  3810  	types.CalcArraySize(KT)
  3811  	// Align more than naturally for the type KT. See issue 73199.
  3812  	align := types.NewArray(types.Types[types.TUINTPTR], 0)
  3813  	types.CalcArraySize(align)
  3814  	storeTyp := types.NewStruct([]*types.Field{
  3815  		{Sym: types.BlankSym, Type: align},
  3816  		{Sym: types.BlankSym, Type: KT},
  3817  	})
  3818  	storeTyp.SetNoalg(true)
  3819  	types.CalcStructSize(storeTyp)
  3820  
  3821  	// Make backing store variable.
  3822  	backingStore := typecheck.TempAt(n.Pos(), s.curfn, storeTyp)
  3823  	backingStore.SetAddrtaken(true)
  3824  
  3825  	// Make "used" boolean.
  3826  	used := typecheck.TempAt(n.Pos(), s.curfn, types.Types[types.TBOOL])
  3827  	if s.curBlock == s.f.Entry {
  3828  		s.vars[used] = s.constBool(false)
  3829  	} else {
  3830  		// initialize this variable at end of entry block
  3831  		s.defvars[s.f.Entry.ID][used] = s.constBool(false)
  3832  	}
  3833  
  3834  	// Initialize an info structure.
  3835  	if s.backingStores == nil {
  3836  		s.backingStores = map[ir.Node]*backingStoreInfo{}
  3837  	}
  3838  	i = &backingStoreInfo{K: K, store: backingStore, used: used, usedStatic: false}
  3839  	s.backingStores[n] = i
  3840  	return i
  3841  }
  3842  
  3843  // append converts an OAPPEND node to SSA.
  3844  // If inplace is false, it converts the OAPPEND expression n to an ssa.Value,
  3845  // adds it to s, and returns the Value.
  3846  // If inplace is true, it writes the result of the OAPPEND expression n
  3847  // back to the slice being appended to, and returns nil.
  3848  // inplace MUST be set to false if the slice can be SSA'd.
  3849  // Note: this code only handles fixed-count appends. Dotdotdot appends
  3850  // have already been rewritten at this point (by walk).
  3851  func (s *state) append(n *ir.CallExpr, inplace bool) *ssa.Value {
  3852  	// If inplace is false, process as expression "append(s, e1, e2, e3)":
  3853  	//
  3854  	// ptr, len, cap := s
  3855  	// len += 3
  3856  	// if uint(len) > uint(cap) {
  3857  	//     ptr, len, cap = growslice(ptr, len, cap, 3, typ)
  3858  	//     Note that len is unmodified by growslice.
  3859  	// }
  3860  	// // with write barriers, if needed:
  3861  	// *(ptr+(len-3)) = e1
  3862  	// *(ptr+(len-2)) = e2
  3863  	// *(ptr+(len-1)) = e3
  3864  	// return makeslice(ptr, len, cap)
  3865  	//
  3866  	//
  3867  	// If inplace is true, process as statement "s = append(s, e1, e2, e3)":
  3868  	//
  3869  	// a := &s
  3870  	// ptr, len, cap := s
  3871  	// len += 3
  3872  	// if uint(len) > uint(cap) {
  3873  	//    ptr, len, cap = growslice(ptr, len, cap, 3, typ)
  3874  	//    vardef(a)    // if necessary, advise liveness we are writing a new a
  3875  	//    *a.cap = cap // write before ptr to avoid a spill
  3876  	//    *a.ptr = ptr // with write barrier
  3877  	// }
  3878  	// *a.len = len
  3879  	// // with write barriers, if needed:
  3880  	// *(ptr+(len-3)) = e1
  3881  	// *(ptr+(len-2)) = e2
  3882  	// *(ptr+(len-1)) = e3
  3883  
  3884  	et := n.Type().Elem()
  3885  	pt := types.NewPtr(et)
  3886  
  3887  	// Evaluate slice
  3888  	sn := n.Args[0] // the slice node is the first in the list
  3889  	var slice, addr *ssa.Value
  3890  	if inplace {
  3891  		addr = s.addr(sn)
  3892  		slice = s.load(n.Type(), addr)
  3893  	} else {
  3894  		slice = s.expr(sn)
  3895  	}
  3896  
  3897  	// Allocate new blocks
  3898  	grow := s.f.NewBlock(ssa.BlockPlain)
  3899  	assign := s.f.NewBlock(ssa.BlockPlain)
  3900  
  3901  	// Decomposse input slice.
  3902  	p := s.newValue1(ssa.OpSlicePtr, pt, slice)
  3903  	l := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], slice)
  3904  	c := s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], slice)
  3905  
  3906  	// Add number of new elements to length.
  3907  	nargs := s.constInt(types.Types[types.TINT], int64(len(n.Args)-1))
  3908  	oldLen := l
  3909  	l = s.newValue2(s.ssaOp(ir.OADD, types.Types[types.TINT]), types.Types[types.TINT], l, nargs)
  3910  
  3911  	// Decide if we need to grow
  3912  	cmp := s.newValue2(s.ssaOp(ir.OLT, types.Types[types.TUINT]), types.Types[types.TBOOL], c, l)
  3913  
  3914  	// Record values of ptr/len/cap before branch.
  3915  	s.vars[ptrVar] = p
  3916  	s.vars[lenVar] = l
  3917  	if !inplace {
  3918  		s.vars[capVar] = c
  3919  	}
  3920  
  3921  	b := s.endBlock()
  3922  	b.Kind = ssa.BlockIf
  3923  	b.Likely = ssa.BranchUnlikely
  3924  	b.SetControl(cmp)
  3925  	b.AddEdgeTo(grow)
  3926  	b.AddEdgeTo(assign)
  3927  
  3928  	// If the result of the append does not escape, we can use
  3929  	// a stack-allocated backing store if len is small enough.
  3930  	// A stack-allocated backing store could be used at every
  3931  	// append that qualifies, but we limit it in some cases to
  3932  	// avoid wasted code and stack space.
  3933  	//
  3934  	// Note that we have two different strategies.
  3935  	// 1. The standard strategy is just to allocate the full
  3936  	//    backing store at the first append.
  3937  	// 2. An alternate strategy is used when
  3938  	//        a. The backing store eventually escapes via move2heap
  3939  	//    and b. The capacity is used somehow
  3940  	//    In this case, we don't want to just allocate
  3941  	//    the full buffer at the first append, because when
  3942  	//    we move2heap the buffer to the heap when it escapes,
  3943  	//    we might end up wasting memory because we can't
  3944  	//    change the capacity.
  3945  	//    So in this case we use growsliceBuf to reuse the buffer
  3946  	//    and walk one step up the size class ladder each time.
  3947  	//
  3948  	// TODO: handle ... append case? Currently we handle only
  3949  	// a fixed number of appended elements.
  3950  	var info *backingStoreInfo
  3951  	if !inplace {
  3952  		info = s.getBackingStoreInfoForAppend(n)
  3953  	}
  3954  
  3955  	if !inplace && info != nil && !n.UseBuf && !info.usedStatic {
  3956  		// if l <= K {
  3957  		//   if !used {
  3958  		//     if oldLen == 0 {
  3959  		//       var store [K]T
  3960  		//       s = store[:l:K]
  3961  		//       used = true
  3962  		//     }
  3963  		//   }
  3964  		// }
  3965  		// ... if we didn't use the stack backing store, call growslice ...
  3966  		//
  3967  		// oldLen==0 is not strictly necessary, but requiring it means
  3968  		// we don't have to worry about copying existing elements.
  3969  		// Allowing oldLen>0 would add complication. Worth it? I would guess not.
  3970  		//
  3971  		// TODO: instead of the used boolean, we could insist that this only applies
  3972  		// to monotonic slices, those which once they have >0 entries never go back
  3973  		// to 0 entries. Then oldLen==0 is enough.
  3974  		//
  3975  		// We also do this for append(x, ...) once for every x.
  3976  		// It is ok to do it more often, but it is probably helpful only for
  3977  		// the first instance. TODO: this could use more tuning. Using ir.Node
  3978  		// as the key works for *ir.Name instances but probably nothing else.
  3979  		info.usedStatic = true
  3980  		// TODO: unset usedStatic somehow?
  3981  
  3982  		usedTestBlock := s.f.NewBlock(ssa.BlockPlain)
  3983  		oldLenTestBlock := s.f.NewBlock(ssa.BlockPlain)
  3984  		bodyBlock := s.f.NewBlock(ssa.BlockPlain)
  3985  		growSlice := s.f.NewBlock(ssa.BlockPlain)
  3986  		tInt := types.Types[types.TINT]
  3987  		tBool := types.Types[types.TBOOL]
  3988  
  3989  		// if l <= K
  3990  		s.startBlock(grow)
  3991  		kTest := s.newValue2(s.ssaOp(ir.OLE, tInt), tBool, l, s.constInt(tInt, info.K))
  3992  		b := s.endBlock()
  3993  		b.Kind = ssa.BlockIf
  3994  		b.SetControl(kTest)
  3995  		b.AddEdgeTo(usedTestBlock)
  3996  		b.AddEdgeTo(growSlice)
  3997  		b.Likely = ssa.BranchLikely
  3998  
  3999  		// if !used
  4000  		s.startBlock(usedTestBlock)
  4001  		usedTest := s.newValue1(ssa.OpNot, tBool, s.expr(info.used))
  4002  		b = s.endBlock()
  4003  		b.Kind = ssa.BlockIf
  4004  		b.SetControl(usedTest)
  4005  		b.AddEdgeTo(oldLenTestBlock)
  4006  		b.AddEdgeTo(growSlice)
  4007  		b.Likely = ssa.BranchLikely
  4008  
  4009  		// if oldLen == 0
  4010  		s.startBlock(oldLenTestBlock)
  4011  		oldLenTest := s.newValue2(s.ssaOp(ir.OEQ, tInt), tBool, oldLen, s.constInt(tInt, 0))
  4012  		b = s.endBlock()
  4013  		b.Kind = ssa.BlockIf
  4014  		b.SetControl(oldLenTest)
  4015  		b.AddEdgeTo(bodyBlock)
  4016  		b.AddEdgeTo(growSlice)
  4017  		b.Likely = ssa.BranchLikely
  4018  
  4019  		// var store struct { _ [0]uintptr; arr [K]T }
  4020  		s.startBlock(bodyBlock)
  4021  		if et.HasPointers() {
  4022  			s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, info.store, s.mem())
  4023  		}
  4024  		addr := s.addr(info.store)
  4025  		s.zero(info.store.Type(), addr)
  4026  
  4027  		// s = store.arr[:l:K]
  4028  		s.vars[ptrVar] = addr
  4029  		s.vars[lenVar] = l // nargs would also be ok because of the oldLen==0 test.
  4030  		s.vars[capVar] = s.constInt(tInt, info.K)
  4031  
  4032  		// used = true
  4033  		s.assign(info.used, s.constBool(true), false, 0)
  4034  		b = s.endBlock()
  4035  		b.AddEdgeTo(assign)
  4036  
  4037  		// New block to use for growslice call.
  4038  		grow = growSlice
  4039  	}
  4040  
  4041  	// Call growslice
  4042  	s.startBlock(grow)
  4043  	taddr := s.expr(n.Fun)
  4044  	var r []*ssa.Value
  4045  	if info != nil && n.UseBuf {
  4046  		// Use stack-allocated buffer as backing store, if we can.
  4047  		if et.HasPointers() && !info.usedStatic {
  4048  			// Initialize in the function header. Not the best place,
  4049  			// but it makes sure we don't scan this area before it is
  4050  			// initialized.
  4051  			mem := s.defvars[s.f.Entry.ID][memVar]
  4052  			mem = s.f.Entry.NewValue1A(n.Pos(), ssa.OpVarDef, types.TypeMem, info.store, mem)
  4053  			addr := s.f.Entry.NewValue2A(n.Pos(), ssa.OpLocalAddr, types.NewPtr(info.store.Type()), info.store, s.sp, mem)
  4054  			mem = s.f.Entry.NewValue2I(n.Pos(), ssa.OpZero, types.TypeMem, info.store.Type().Size(), addr, mem)
  4055  			mem.Aux = info.store.Type()
  4056  			s.defvars[s.f.Entry.ID][memVar] = mem
  4057  			info.usedStatic = true
  4058  		}
  4059  		fn := ir.Syms.GrowsliceBuf
  4060  		if goexperiment.RuntimeFreegc && n.AppendNoAlias && !et.HasPointers() {
  4061  			// The append is for a non-aliased slice where the runtime knows how to free
  4062  			// the old logically dead backing store after growth.
  4063  			// TODO(thepudds): for now, we only use the NoAlias version for element types
  4064  			// without pointers while waiting on additional runtime support (CL 698515).
  4065  			fn = ir.Syms.GrowsliceBufNoAlias
  4066  		}
  4067  		r = s.rtcall(fn, true, []*types.Type{n.Type()}, p, l, c, nargs, taddr, s.addr(info.store), s.constInt(types.Types[types.TINT], info.K))
  4068  	} else {
  4069  		fn := ir.Syms.Growslice
  4070  		if goexperiment.RuntimeFreegc && n.AppendNoAlias && !et.HasPointers() {
  4071  			// The append is for a non-aliased slice where the runtime knows how to free
  4072  			// the old logically dead backing store after growth.
  4073  			// TODO(thepudds): for now, we only use the NoAlias version for element types
  4074  			// without pointers while waiting on additional runtime support (CL 698515).
  4075  			fn = ir.Syms.GrowsliceNoAlias
  4076  		}
  4077  		r = s.rtcall(fn, true, []*types.Type{n.Type()}, p, l, c, nargs, taddr)
  4078  	}
  4079  
  4080  	// Decompose output slice
  4081  	p = s.newValue1(ssa.OpSlicePtr, pt, r[0])
  4082  	l = s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], r[0])
  4083  	c = s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], r[0])
  4084  
  4085  	s.vars[ptrVar] = p
  4086  	s.vars[lenVar] = l
  4087  	s.vars[capVar] = c
  4088  	if inplace {
  4089  		if sn.Op() == ir.ONAME {
  4090  			sn := sn.(*ir.Name)
  4091  			if sn.Class != ir.PEXTERN {
  4092  				// Tell liveness we're about to build a new slice
  4093  				s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, sn, s.mem())
  4094  			}
  4095  		}
  4096  		capaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, types.SliceCapOffset, addr)
  4097  		s.store(types.Types[types.TINT], capaddr, c)
  4098  		s.store(pt, addr, p)
  4099  	}
  4100  
  4101  	b = s.endBlock()
  4102  	b.AddEdgeTo(assign)
  4103  
  4104  	// assign new elements to slots
  4105  	s.startBlock(assign)
  4106  	p = s.variable(ptrVar, pt)                      // generates phi for ptr
  4107  	l = s.variable(lenVar, types.Types[types.TINT]) // generates phi for len
  4108  	if !inplace {
  4109  		c = s.variable(capVar, types.Types[types.TINT]) // generates phi for cap
  4110  	}
  4111  
  4112  	if inplace {
  4113  		// Update length in place.
  4114  		// We have to wait until here to make sure growslice succeeded.
  4115  		lenaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, types.SliceLenOffset, addr)
  4116  		s.store(types.Types[types.TINT], lenaddr, l)
  4117  	}
  4118  
  4119  	// Evaluate args
  4120  	type argRec struct {
  4121  		// if store is true, we're appending the value v.  If false, we're appending the
  4122  		// value at *v.
  4123  		v     *ssa.Value
  4124  		store bool
  4125  	}
  4126  	args := make([]argRec, 0, len(n.Args[1:]))
  4127  	for _, n := range n.Args[1:] {
  4128  		if ssa.CanSSA(n.Type()) {
  4129  			args = append(args, argRec{v: s.expr(n), store: true})
  4130  		} else {
  4131  			v := s.addr(n)
  4132  			args = append(args, argRec{v: v})
  4133  		}
  4134  	}
  4135  
  4136  	// Write args into slice.
  4137  	oldLen = s.newValue2(s.ssaOp(ir.OSUB, types.Types[types.TINT]), types.Types[types.TINT], l, nargs)
  4138  	p2 := s.newValue2(ssa.OpPtrIndex, pt, p, oldLen)
  4139  	for i, arg := range args {
  4140  		addr := s.newValue2(ssa.OpPtrIndex, pt, p2, s.constInt(types.Types[types.TINT], int64(i)))
  4141  		if arg.store {
  4142  			s.storeType(et, addr, arg.v, 0, true)
  4143  		} else {
  4144  			s.move(et, addr, arg.v)
  4145  		}
  4146  	}
  4147  
  4148  	// The following deletions have no practical effect at this time
  4149  	// because state.vars has been reset by the preceding state.startBlock.
  4150  	// They only enforce the fact that these variables are no longer need in
  4151  	// the current scope.
  4152  	delete(s.vars, ptrVar)
  4153  	delete(s.vars, lenVar)
  4154  	if !inplace {
  4155  		delete(s.vars, capVar)
  4156  	}
  4157  
  4158  	// make result
  4159  	if inplace {
  4160  		return nil
  4161  	}
  4162  	return s.newValue3(ssa.OpSliceMake, n.Type(), p, l, c)
  4163  }
  4164  
  4165  func (s *state) move2heap(n *ir.MoveToHeapExpr) *ssa.Value {
  4166  	// s := n.Slice
  4167  	// if s.ptr points to current stack frame {
  4168  	//     s2 := make([]T, s.len, s.cap)
  4169  	//     copy(s2[:cap], s[:cap])
  4170  	//     s = s2
  4171  	// }
  4172  	// return s
  4173  
  4174  	slice := s.expr(n.Slice)
  4175  	et := slice.Type.Elem()
  4176  	pt := types.NewPtr(et)
  4177  
  4178  	info := s.getBackingStoreInfo(n)
  4179  	if info == nil {
  4180  		// Backing store will never be stack allocated, so
  4181  		// move2heap is a no-op.
  4182  		return slice
  4183  	}
  4184  
  4185  	// Decomposse input slice.
  4186  	p := s.newValue1(ssa.OpSlicePtr, pt, slice)
  4187  	l := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], slice)
  4188  	c := s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], slice)
  4189  
  4190  	moveBlock := s.f.NewBlock(ssa.BlockPlain)
  4191  	mergeBlock := s.f.NewBlock(ssa.BlockPlain)
  4192  
  4193  	s.vars[ptrVar] = p
  4194  	s.vars[lenVar] = l
  4195  	s.vars[capVar] = c
  4196  
  4197  	// Decide if we need to move the slice backing store.
  4198  	// It needs to be moved if it is currently on the stack.
  4199  	sub := ssa.OpSub64
  4200  	less := ssa.OpLess64U
  4201  	if s.config.PtrSize == 4 {
  4202  		sub = ssa.OpSub32
  4203  		less = ssa.OpLess32U
  4204  	}
  4205  	callerSP := s.newValue1(ssa.OpGetCallerSP, types.Types[types.TUINTPTR], s.mem())
  4206  	frameSize := s.newValue2(sub, types.Types[types.TUINTPTR], callerSP, s.sp)
  4207  	pInt := s.newValue2(ssa.OpConvert, types.Types[types.TUINTPTR], p, s.mem())
  4208  	off := s.newValue2(sub, types.Types[types.TUINTPTR], pInt, s.sp)
  4209  	cond := s.newValue2(less, types.Types[types.TBOOL], off, frameSize)
  4210  
  4211  	b := s.endBlock()
  4212  	b.Kind = ssa.BlockIf
  4213  	b.Likely = ssa.BranchUnlikely // fast path is to not have to call into runtime
  4214  	b.SetControl(cond)
  4215  	b.AddEdgeTo(moveBlock)
  4216  	b.AddEdgeTo(mergeBlock)
  4217  
  4218  	// Move the slice to heap
  4219  	s.startBlock(moveBlock)
  4220  	var newSlice *ssa.Value
  4221  	if et.HasPointers() {
  4222  		typ := s.expr(n.RType)
  4223  		if n.PreserveCapacity {
  4224  			newSlice = s.rtcall(ir.Syms.MoveSlice, true, []*types.Type{slice.Type}, typ, p, l, c)[0]
  4225  		} else {
  4226  			newSlice = s.rtcall(ir.Syms.MoveSliceNoCap, true, []*types.Type{slice.Type}, typ, p, l)[0]
  4227  		}
  4228  	} else {
  4229  		elemSize := s.constInt(types.Types[types.TUINTPTR], et.Size())
  4230  		if n.PreserveCapacity {
  4231  			newSlice = s.rtcall(ir.Syms.MoveSliceNoScan, true, []*types.Type{slice.Type}, elemSize, p, l, c)[0]
  4232  		} else {
  4233  			newSlice = s.rtcall(ir.Syms.MoveSliceNoCapNoScan, true, []*types.Type{slice.Type}, elemSize, p, l)[0]
  4234  		}
  4235  	}
  4236  	// Decompose output slice
  4237  	s.vars[ptrVar] = s.newValue1(ssa.OpSlicePtr, pt, newSlice)
  4238  	s.vars[lenVar] = s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], newSlice)
  4239  	s.vars[capVar] = s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], newSlice)
  4240  	b = s.endBlock()
  4241  	b.AddEdgeTo(mergeBlock)
  4242  
  4243  	// Merge fast path (no moving) and slow path (moved)
  4244  	s.startBlock(mergeBlock)
  4245  	p = s.variable(ptrVar, pt)                      // generates phi for ptr
  4246  	l = s.variable(lenVar, types.Types[types.TINT]) // generates phi for len
  4247  	c = s.variable(capVar, types.Types[types.TINT]) // generates phi for cap
  4248  	delete(s.vars, ptrVar)
  4249  	delete(s.vars, lenVar)
  4250  	delete(s.vars, capVar)
  4251  	return s.newValue3(ssa.OpSliceMake, slice.Type, p, l, c)
  4252  }
  4253  
  4254  // minMax converts an OMIN/OMAX builtin call into SSA.
  4255  func (s *state) minMax(n *ir.CallExpr) *ssa.Value {
  4256  	// The OMIN/OMAX builtin is variadic, but its semantics are
  4257  	// equivalent to left-folding a binary min/max operation across the
  4258  	// arguments list.
  4259  	fold := func(op func(x, a *ssa.Value) *ssa.Value) *ssa.Value {
  4260  		x := s.expr(n.Args[0])
  4261  		for _, arg := range n.Args[1:] {
  4262  			x = op(x, s.expr(arg))
  4263  		}
  4264  		return x
  4265  	}
  4266  
  4267  	typ := n.Type()
  4268  
  4269  	if typ.IsFloat() || typ.IsString() {
  4270  		// min/max semantics for floats are tricky because of NaNs and
  4271  		// negative zero. Some architectures have instructions which
  4272  		// we can use to generate the right result. For others we must
  4273  		// call into the runtime instead.
  4274  		//
  4275  		// Strings are conceptually simpler, but we currently desugar
  4276  		// string comparisons during walk, not ssagen.
  4277  
  4278  		if typ.IsFloat() {
  4279  			hasIntrinsic := false
  4280  			switch Arch.LinkArch.Family {
  4281  			case sys.AMD64, sys.ARM64, sys.Loong64, sys.RISCV64, sys.S390X:
  4282  				hasIntrinsic = true
  4283  			case sys.PPC64:
  4284  				hasIntrinsic = buildcfg.GOPPC64 >= 9
  4285  			}
  4286  
  4287  			if hasIntrinsic {
  4288  				var op ssa.Op
  4289  				switch {
  4290  				case typ.Kind() == types.TFLOAT64 && n.Op() == ir.OMIN:
  4291  					op = ssa.OpMin64F
  4292  				case typ.Kind() == types.TFLOAT64 && n.Op() == ir.OMAX:
  4293  					op = ssa.OpMax64F
  4294  				case typ.Kind() == types.TFLOAT32 && n.Op() == ir.OMIN:
  4295  					op = ssa.OpMin32F
  4296  				case typ.Kind() == types.TFLOAT32 && n.Op() == ir.OMAX:
  4297  					op = ssa.OpMax32F
  4298  				}
  4299  				return fold(func(x, a *ssa.Value) *ssa.Value {
  4300  					return s.newValue2(op, typ, x, a)
  4301  				})
  4302  			}
  4303  		}
  4304  		var name string
  4305  		switch typ.Kind() {
  4306  		case types.TFLOAT32:
  4307  			switch n.Op() {
  4308  			case ir.OMIN:
  4309  				name = "fmin32"
  4310  			case ir.OMAX:
  4311  				name = "fmax32"
  4312  			}
  4313  		case types.TFLOAT64:
  4314  			switch n.Op() {
  4315  			case ir.OMIN:
  4316  				name = "fmin64"
  4317  			case ir.OMAX:
  4318  				name = "fmax64"
  4319  			}
  4320  		case types.TSTRING:
  4321  			switch n.Op() {
  4322  			case ir.OMIN:
  4323  				name = "strmin"
  4324  			case ir.OMAX:
  4325  				name = "strmax"
  4326  			}
  4327  		}
  4328  		fn := typecheck.LookupRuntimeFunc(name)
  4329  
  4330  		return fold(func(x, a *ssa.Value) *ssa.Value {
  4331  			return s.rtcall(fn, true, []*types.Type{typ}, x, a)[0]
  4332  		})
  4333  	}
  4334  
  4335  	if typ.IsInteger() {
  4336  		if Arch.LinkArch.Family == sys.RISCV64 && buildcfg.GORISCV64 >= 22 && typ.Size() == 8 {
  4337  			var op ssa.Op
  4338  			switch {
  4339  			case typ.IsSigned() && n.Op() == ir.OMIN:
  4340  				op = ssa.OpMin64
  4341  			case typ.IsSigned() && n.Op() == ir.OMAX:
  4342  				op = ssa.OpMax64
  4343  			case typ.IsUnsigned() && n.Op() == ir.OMIN:
  4344  				op = ssa.OpMin64u
  4345  			case typ.IsUnsigned() && n.Op() == ir.OMAX:
  4346  				op = ssa.OpMax64u
  4347  			}
  4348  			return fold(func(x, a *ssa.Value) *ssa.Value {
  4349  				return s.newValue2(op, typ, x, a)
  4350  			})
  4351  		}
  4352  	}
  4353  
  4354  	lt := s.ssaOp(ir.OLT, typ)
  4355  
  4356  	return fold(func(x, a *ssa.Value) *ssa.Value {
  4357  		switch n.Op() {
  4358  		case ir.OMIN:
  4359  			// a < x ? a : x
  4360  			return s.ternary(s.newValue2(lt, types.Types[types.TBOOL], a, x), a, x)
  4361  		case ir.OMAX:
  4362  			// x < a ? a : x
  4363  			return s.ternary(s.newValue2(lt, types.Types[types.TBOOL], x, a), a, x)
  4364  		}
  4365  		panic("unreachable")
  4366  	})
  4367  }
  4368  
  4369  // ternary emits code to evaluate cond ? x : y.
  4370  func (s *state) ternary(cond, x, y *ssa.Value) *ssa.Value {
  4371  	// Note that we need a new ternaryVar each time (unlike okVar where we can
  4372  	// reuse the variable) because it might have a different type every time.
  4373  	ternaryVar := ssaMarker("ternary")
  4374  
  4375  	bThen := s.f.NewBlock(ssa.BlockPlain)
  4376  	bElse := s.f.NewBlock(ssa.BlockPlain)
  4377  	bEnd := s.f.NewBlock(ssa.BlockPlain)
  4378  
  4379  	b := s.endBlock()
  4380  	b.Kind = ssa.BlockIf
  4381  	b.SetControl(cond)
  4382  	b.AddEdgeTo(bThen)
  4383  	b.AddEdgeTo(bElse)
  4384  
  4385  	s.startBlock(bThen)
  4386  	s.vars[ternaryVar] = x
  4387  	s.endBlock().AddEdgeTo(bEnd)
  4388  
  4389  	s.startBlock(bElse)
  4390  	s.vars[ternaryVar] = y
  4391  	s.endBlock().AddEdgeTo(bEnd)
  4392  
  4393  	s.startBlock(bEnd)
  4394  	r := s.variable(ternaryVar, x.Type)
  4395  	delete(s.vars, ternaryVar)
  4396  	return r
  4397  }
  4398  
  4399  // condBranch evaluates the boolean expression cond and branches to yes
  4400  // if cond is true and no if cond is false.
  4401  // This function is intended to handle && and || better than just calling
  4402  // s.expr(cond) and branching on the result.
  4403  func (s *state) condBranch(cond ir.Node, yes, no *ssa.Block, likely int8) {
  4404  	switch cond.Op() {
  4405  	case ir.OANDAND:
  4406  		cond := cond.(*ir.LogicalExpr)
  4407  		mid := s.f.NewBlock(ssa.BlockPlain)
  4408  		s.stmtList(cond.Init())
  4409  		s.condBranch(cond.X, mid, no, max(likely, 0))
  4410  		s.startBlock(mid)
  4411  		s.condBranch(cond.Y, yes, no, likely)
  4412  		return
  4413  		// Note: if likely==1, then both recursive calls pass 1.
  4414  		// If likely==-1, then we don't have enough information to decide
  4415  		// whether the first branch is likely or not. So we pass 0 for
  4416  		// the likeliness of the first branch.
  4417  		// TODO: have the frontend give us branch prediction hints for
  4418  		// OANDAND and OOROR nodes (if it ever has such info).
  4419  	case ir.OOROR:
  4420  		cond := cond.(*ir.LogicalExpr)
  4421  		mid := s.f.NewBlock(ssa.BlockPlain)
  4422  		s.stmtList(cond.Init())
  4423  		s.condBranch(cond.X, yes, mid, min(likely, 0))
  4424  		s.startBlock(mid)
  4425  		s.condBranch(cond.Y, yes, no, likely)
  4426  		return
  4427  		// Note: if likely==-1, then both recursive calls pass -1.
  4428  		// If likely==1, then we don't have enough info to decide
  4429  		// the likelihood of the first branch.
  4430  	case ir.ONOT:
  4431  		cond := cond.(*ir.UnaryExpr)
  4432  		s.stmtList(cond.Init())
  4433  		s.condBranch(cond.X, no, yes, -likely)
  4434  		return
  4435  	case ir.OCONVNOP:
  4436  		cond := cond.(*ir.ConvExpr)
  4437  		s.stmtList(cond.Init())
  4438  		s.condBranch(cond.X, yes, no, likely)
  4439  		return
  4440  	}
  4441  	c := s.expr(cond)
  4442  	b := s.endBlock()
  4443  	b.Kind = ssa.BlockIf
  4444  	b.SetControl(c)
  4445  	b.Likely = ssa.BranchPrediction(likely) // gc and ssa both use -1/0/+1 for likeliness
  4446  	b.AddEdgeTo(yes)
  4447  	b.AddEdgeTo(no)
  4448  }
  4449  
  4450  type skipMask uint8
  4451  
  4452  const (
  4453  	skipPtr skipMask = 1 << iota
  4454  	skipLen
  4455  	skipCap
  4456  )
  4457  
  4458  // assign does left = right.
  4459  // Right has already been evaluated to ssa, left has not.
  4460  // If deref is true, then we do left = *right instead (and right has already been nil-checked).
  4461  // If deref is true and right == nil, just do left = 0.
  4462  // skip indicates assignments (at the top level) that can be avoided.
  4463  // mayOverlap indicates whether left&right might partially overlap in memory. Default is false.
  4464  func (s *state) assign(left ir.Node, right *ssa.Value, deref bool, skip skipMask) {
  4465  	s.assignWhichMayOverlap(left, right, deref, skip, false)
  4466  }
  4467  func (s *state) assignWhichMayOverlap(left ir.Node, right *ssa.Value, deref bool, skip skipMask, mayOverlap bool) {
  4468  	if left.Op() == ir.ONAME && ir.IsBlank(left) {
  4469  		return
  4470  	}
  4471  	t := left.Type()
  4472  	types.CalcSize(t)
  4473  	if s.canSSA(left) {
  4474  		if deref {
  4475  			s.Fatalf("can SSA LHS %v but not RHS %s", left, right)
  4476  		}
  4477  		if left.Op() == ir.ODOT {
  4478  			// We're assigning to a field of an ssa-able value.
  4479  			// We need to build a new structure with the new value for the
  4480  			// field we're assigning and the old values for the other fields.
  4481  			// For instance:
  4482  			//   type T struct {a, b, c int}
  4483  			//   var T x
  4484  			//   x.b = 5
  4485  			// For the x.b = 5 assignment we want to generate x = T{x.a, 5, x.c}
  4486  
  4487  			// Grab information about the structure type.
  4488  			left := left.(*ir.SelectorExpr)
  4489  			t := left.X.Type()
  4490  			nf := t.NumFields()
  4491  			idx := fieldIdx(left)
  4492  
  4493  			// Grab old value of structure.
  4494  			old := s.expr(left.X)
  4495  
  4496  			if left.Type().Size() == 0 {
  4497  				// Nothing to do when assigning zero-sized things.
  4498  				return
  4499  			}
  4500  
  4501  			// Make new structure.
  4502  			new := s.newValue0(ssa.OpStructMake, t)
  4503  
  4504  			// Add fields as args.
  4505  			for i := 0; i < nf; i++ {
  4506  				if i == idx {
  4507  					new.AddArg(right)
  4508  				} else {
  4509  					new.AddArg(s.newValue1I(ssa.OpStructSelect, t.FieldType(i), int64(i), old))
  4510  				}
  4511  			}
  4512  
  4513  			// Recursively assign the new value we've made to the base of the dot op.
  4514  			s.assign(left.X, new, false, 0)
  4515  			// TODO: do we need to update named values here?
  4516  			return
  4517  		}
  4518  		if left.Op() == ir.OINDEX && left.(*ir.IndexExpr).X.Type().IsArray() {
  4519  			left := left.(*ir.IndexExpr)
  4520  			s.pushLine(left.Pos())
  4521  			defer s.popLine()
  4522  			// We're assigning to an element of an ssa-able array.
  4523  			// a[i] = v
  4524  			t := left.X.Type()
  4525  			n := t.NumElem()
  4526  
  4527  			i := s.expr(left.Index) // index
  4528  			if n == 0 {
  4529  				// The bounds check must fail.  Might as well
  4530  				// ignore the actual index and just use zeros.
  4531  				z := s.constInt(types.Types[types.TINT], 0)
  4532  				s.boundsCheck(z, z, ssa.BoundsIndex, false)
  4533  				return
  4534  			}
  4535  			if n != 1 {
  4536  				// This can happen in weird, always-panics cases, like:
  4537  				//     var x [0][2]int
  4538  				//     x[i][j] = 5
  4539  				// We know it always panics because the LHS is ssa-able,
  4540  				// and arrays of length > 1 can't be ssa-able unless
  4541  				// they are somewhere inside an outer [0].
  4542  				// We can ignore the actual assignment, it is dynamically
  4543  				// unreachable. See issue 77635.
  4544  				return
  4545  			}
  4546  			if t.Size() == 0 {
  4547  				len := s.constInt(types.Types[types.TINT], n)
  4548  				s.boundsCheck(i, len, ssa.BoundsIndex, false)
  4549  				return
  4550  			}
  4551  
  4552  			// Rewrite to a = [1]{v}
  4553  			len := s.constInt(types.Types[types.TINT], 1)
  4554  			s.boundsCheck(i, len, ssa.BoundsIndex, false) // checks i == 0
  4555  			v := s.newValue1(ssa.OpArrayMake1, t, right)
  4556  			s.assign(left.X, v, false, 0)
  4557  			return
  4558  		}
  4559  		left := left.(*ir.Name)
  4560  		// Update variable assignment.
  4561  		s.vars[left] = right
  4562  		s.addNamedValue(left, right)
  4563  		return
  4564  	}
  4565  
  4566  	// If this assignment clobbers an entire local variable, then emit
  4567  	// OpVarDef so liveness analysis knows the variable is redefined.
  4568  	if base, ok := clobberBase(left).(*ir.Name); ok && base.OnStack() && skip == 0 && (t.HasPointers() || ssa.IsMergeCandidate(base)) {
  4569  		s.vars[memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, base, s.mem(), !ir.IsAutoTmp(base))
  4570  	}
  4571  
  4572  	// Left is not ssa-able. Compute its address.
  4573  	addr := s.addr(left)
  4574  	if ir.IsReflectHeaderDataField(left) {
  4575  		// Package unsafe's documentation says storing pointers into
  4576  		// reflect.SliceHeader and reflect.StringHeader's Data fields
  4577  		// is valid, even though they have type uintptr (#19168).
  4578  		// Mark it pointer type to signal the writebarrier pass to
  4579  		// insert a write barrier.
  4580  		t = types.Types[types.TUNSAFEPTR]
  4581  	}
  4582  	if deref {
  4583  		// Treat as a mem->mem move.
  4584  		if right == nil {
  4585  			s.zero(t, addr)
  4586  		} else {
  4587  			s.moveWhichMayOverlap(t, addr, right, mayOverlap)
  4588  		}
  4589  		return
  4590  	}
  4591  	// Treat as a store.
  4592  	s.storeType(t, addr, right, skip, !ir.IsAutoTmp(left))
  4593  }
  4594  
  4595  // zeroVal returns the zero value for type t.
  4596  func (s *state) zeroVal(t *types.Type) *ssa.Value {
  4597  	if t.Size() == 0 {
  4598  		return s.entryNewValue0(ssa.OpEmpty, t)
  4599  	}
  4600  	switch {
  4601  	case t.IsInteger():
  4602  		switch t.Size() {
  4603  		case 1:
  4604  			return s.constInt8(t, 0)
  4605  		case 2:
  4606  			return s.constInt16(t, 0)
  4607  		case 4:
  4608  			return s.constInt32(t, 0)
  4609  		case 8:
  4610  			return s.constInt64(t, 0)
  4611  		default:
  4612  			s.Fatalf("bad sized integer type %v", t)
  4613  		}
  4614  	case t.IsFloat():
  4615  		switch t.Size() {
  4616  		case 4:
  4617  			return s.constFloat32(t, 0)
  4618  		case 8:
  4619  			return s.constFloat64(t, 0)
  4620  		default:
  4621  			s.Fatalf("bad sized float type %v", t)
  4622  		}
  4623  	case t.IsComplex():
  4624  		switch t.Size() {
  4625  		case 8:
  4626  			z := s.constFloat32(types.Types[types.TFLOAT32], 0)
  4627  			return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
  4628  		case 16:
  4629  			z := s.constFloat64(types.Types[types.TFLOAT64], 0)
  4630  			return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
  4631  		default:
  4632  			s.Fatalf("bad sized complex type %v", t)
  4633  		}
  4634  
  4635  	case t.IsString():
  4636  		return s.constEmptyString(t)
  4637  	case t.IsPtrShaped():
  4638  		return s.constNil(t)
  4639  	case t.IsBoolean():
  4640  		return s.constBool(false)
  4641  	case t.IsInterface():
  4642  		return s.constInterface(t)
  4643  	case t.IsSlice():
  4644  		return s.constSlice(t)
  4645  	case isStructNotSIMD(t):
  4646  		n := t.NumFields()
  4647  		v := s.entryNewValue0(ssa.OpStructMake, t)
  4648  		for i := 0; i < n; i++ {
  4649  			v.AddArg(s.zeroVal(t.FieldType(i)))
  4650  		}
  4651  		return v
  4652  	case t.IsArray() && t.NumElem() == 1:
  4653  		return s.entryNewValue1(ssa.OpArrayMake1, t, s.zeroVal(t.Elem()))
  4654  	case t.IsSIMD():
  4655  		return s.newValue0(ssa.OpZeroSIMD, t)
  4656  	}
  4657  	s.Fatalf("zero for type %v not implemented", t)
  4658  	return nil
  4659  }
  4660  
  4661  type callKind int8
  4662  
  4663  const (
  4664  	callNormal callKind = iota
  4665  	callDefer
  4666  	callDeferStack
  4667  	callGo
  4668  	callTail
  4669  )
  4670  
  4671  type sfRtCallDef struct {
  4672  	rtfn  *obj.LSym
  4673  	rtype types.Kind
  4674  }
  4675  
  4676  var softFloatOps map[ssa.Op]sfRtCallDef
  4677  
  4678  func softfloatInit() {
  4679  	// Some of these operations get transformed by sfcall.
  4680  	softFloatOps = map[ssa.Op]sfRtCallDef{
  4681  		ssa.OpAdd32F: {typecheck.LookupRuntimeFunc("fadd32"), types.TFLOAT32},
  4682  		ssa.OpAdd64F: {typecheck.LookupRuntimeFunc("fadd64"), types.TFLOAT64},
  4683  		ssa.OpSub32F: {typecheck.LookupRuntimeFunc("fadd32"), types.TFLOAT32},
  4684  		ssa.OpSub64F: {typecheck.LookupRuntimeFunc("fadd64"), types.TFLOAT64},
  4685  		ssa.OpMul32F: {typecheck.LookupRuntimeFunc("fmul32"), types.TFLOAT32},
  4686  		ssa.OpMul64F: {typecheck.LookupRuntimeFunc("fmul64"), types.TFLOAT64},
  4687  		ssa.OpDiv32F: {typecheck.LookupRuntimeFunc("fdiv32"), types.TFLOAT32},
  4688  		ssa.OpDiv64F: {typecheck.LookupRuntimeFunc("fdiv64"), types.TFLOAT64},
  4689  
  4690  		ssa.OpEq64F:   {typecheck.LookupRuntimeFunc("feq64"), types.TBOOL},
  4691  		ssa.OpEq32F:   {typecheck.LookupRuntimeFunc("feq32"), types.TBOOL},
  4692  		ssa.OpNeq64F:  {typecheck.LookupRuntimeFunc("feq64"), types.TBOOL},
  4693  		ssa.OpNeq32F:  {typecheck.LookupRuntimeFunc("feq32"), types.TBOOL},
  4694  		ssa.OpLess64F: {typecheck.LookupRuntimeFunc("fgt64"), types.TBOOL},
  4695  		ssa.OpLess32F: {typecheck.LookupRuntimeFunc("fgt32"), types.TBOOL},
  4696  		ssa.OpLeq64F:  {typecheck.LookupRuntimeFunc("fge64"), types.TBOOL},
  4697  		ssa.OpLeq32F:  {typecheck.LookupRuntimeFunc("fge32"), types.TBOOL},
  4698  
  4699  		ssa.OpCvt32to32F:  {typecheck.LookupRuntimeFunc("fint32to32"), types.TFLOAT32},
  4700  		ssa.OpCvt32Fto32:  {typecheck.LookupRuntimeFunc("f32toint32"), types.TINT32},
  4701  		ssa.OpCvt64to32F:  {typecheck.LookupRuntimeFunc("fint64to32"), types.TFLOAT32},
  4702  		ssa.OpCvt32Fto64:  {typecheck.LookupRuntimeFunc("f32toint64"), types.TINT64},
  4703  		ssa.OpCvt64Uto32F: {typecheck.LookupRuntimeFunc("fuint64to32"), types.TFLOAT32},
  4704  		ssa.OpCvt32Fto64U: {typecheck.LookupRuntimeFunc("f32touint64"), types.TUINT64},
  4705  		ssa.OpCvt32to64F:  {typecheck.LookupRuntimeFunc("fint32to64"), types.TFLOAT64},
  4706  		ssa.OpCvt64Fto32:  {typecheck.LookupRuntimeFunc("f64toint32"), types.TINT32},
  4707  		ssa.OpCvt64to64F:  {typecheck.LookupRuntimeFunc("fint64to64"), types.TFLOAT64},
  4708  		ssa.OpCvt64Fto64:  {typecheck.LookupRuntimeFunc("f64toint64"), types.TINT64},
  4709  		ssa.OpCvt64Uto64F: {typecheck.LookupRuntimeFunc("fuint64to64"), types.TFLOAT64},
  4710  		ssa.OpCvt64Fto64U: {typecheck.LookupRuntimeFunc("f64touint64"), types.TUINT64},
  4711  		ssa.OpCvt32Fto64F: {typecheck.LookupRuntimeFunc("f32to64"), types.TFLOAT64},
  4712  		ssa.OpCvt64Fto32F: {typecheck.LookupRuntimeFunc("f64to32"), types.TFLOAT32},
  4713  	}
  4714  }
  4715  
  4716  // TODO: do not emit sfcall if operation can be optimized to constant in later
  4717  // opt phase
  4718  func (s *state) sfcall(op ssa.Op, args ...*ssa.Value) (*ssa.Value, bool) {
  4719  	f2i := func(t *types.Type) *types.Type {
  4720  		switch t.Kind() {
  4721  		case types.TFLOAT32:
  4722  			return types.Types[types.TUINT32]
  4723  		case types.TFLOAT64:
  4724  			return types.Types[types.TUINT64]
  4725  		}
  4726  		return t
  4727  	}
  4728  
  4729  	if callDef, ok := softFloatOps[op]; ok {
  4730  		switch op {
  4731  		case ssa.OpLess32F,
  4732  			ssa.OpLess64F,
  4733  			ssa.OpLeq32F,
  4734  			ssa.OpLeq64F:
  4735  			args[0], args[1] = args[1], args[0]
  4736  		case ssa.OpSub32F,
  4737  			ssa.OpSub64F:
  4738  			args[1] = s.newValue1(s.ssaOp(ir.ONEG, types.Types[callDef.rtype]), args[1].Type, args[1])
  4739  		}
  4740  
  4741  		// runtime functions take uints for floats and returns uints.
  4742  		// Convert to uints so we use the right calling convention.
  4743  		for i, a := range args {
  4744  			if a.Type.IsFloat() {
  4745  				args[i] = s.newValue1(ssa.OpCopy, f2i(a.Type), a)
  4746  			}
  4747  		}
  4748  
  4749  		rt := types.Types[callDef.rtype]
  4750  		result := s.rtcall(callDef.rtfn, true, []*types.Type{f2i(rt)}, args...)[0]
  4751  		if rt.IsFloat() {
  4752  			result = s.newValue1(ssa.OpCopy, rt, result)
  4753  		}
  4754  		if op == ssa.OpNeq32F || op == ssa.OpNeq64F {
  4755  			result = s.newValue1(ssa.OpNot, result.Type, result)
  4756  		}
  4757  		return result, true
  4758  	}
  4759  	return nil, false
  4760  }
  4761  
  4762  // split breaks up a tuple-typed value into its 2 parts.
  4763  func (s *state) split(v *ssa.Value) (*ssa.Value, *ssa.Value) {
  4764  	p0 := s.newValue1(ssa.OpSelect0, v.Type.FieldType(0), v)
  4765  	p1 := s.newValue1(ssa.OpSelect1, v.Type.FieldType(1), v)
  4766  	return p0, p1
  4767  }
  4768  
  4769  // intrinsicCall converts a call to a recognized intrinsic function into the intrinsic SSA operation.
  4770  func (s *state) intrinsicCall(n *ir.CallExpr) *ssa.Value {
  4771  	v := findIntrinsic(n.Fun.Sym())(s, n, s.intrinsicArgs(n))
  4772  	if ssa.IntrinsicsDebug > 0 {
  4773  		x := v
  4774  		if x == nil {
  4775  			x = s.mem()
  4776  		}
  4777  		if x.Op == ssa.OpSelect0 || x.Op == ssa.OpSelect1 {
  4778  			x = x.Args[0]
  4779  		}
  4780  		base.WarnfAt(n.Pos(), "intrinsic substitution for %v with %s", n.Fun.Sym().Name, x.LongString())
  4781  	}
  4782  	return v
  4783  }
  4784  
  4785  // intrinsicArgs extracts args from n, evaluates them to SSA values, and returns them.
  4786  func (s *state) intrinsicArgs(n *ir.CallExpr) []*ssa.Value {
  4787  	args := make([]*ssa.Value, len(n.Args))
  4788  	for i, n := range n.Args {
  4789  		args[i] = s.expr(n)
  4790  	}
  4791  	return args
  4792  }
  4793  
  4794  // openDeferRecord adds code to evaluate and store the function for an open-code defer
  4795  // call, and records info about the defer, so we can generate proper code on the
  4796  // exit paths. n is the sub-node of the defer node that is the actual function
  4797  // call. We will also record funcdata information on where the function is stored
  4798  // (as well as the deferBits variable), and this will enable us to run the proper
  4799  // defer calls during panics.
  4800  func (s *state) openDeferRecord(n *ir.CallExpr) {
  4801  	if len(n.Args) != 0 || n.Op() != ir.OCALLFUNC || n.Fun.Type().NumResults() != 0 {
  4802  		s.Fatalf("defer call with arguments or results: %v", n)
  4803  	}
  4804  
  4805  	opendefer := &openDeferInfo{
  4806  		n: n,
  4807  	}
  4808  	fn := n.Fun
  4809  	// We must always store the function value in a stack slot for the
  4810  	// runtime panic code to use. But in the defer exit code, we will
  4811  	// call the function directly if it is a static function.
  4812  	closureVal := s.expr(fn)
  4813  	closure := s.openDeferSave(fn.Type(), closureVal)
  4814  	opendefer.closureNode = closure.Aux.(*ir.Name)
  4815  	if !(fn.Op() == ir.ONAME && fn.(*ir.Name).Class == ir.PFUNC) {
  4816  		opendefer.closure = closure
  4817  	}
  4818  	index := len(s.openDefers)
  4819  	s.openDefers = append(s.openDefers, opendefer)
  4820  
  4821  	// Update deferBits only after evaluation and storage to stack of
  4822  	// the function is successful.
  4823  	bitvalue := s.constInt8(types.Types[types.TUINT8], 1<<uint(index))
  4824  	newDeferBits := s.newValue2(ssa.OpOr8, types.Types[types.TUINT8], s.variable(deferBitsVar, types.Types[types.TUINT8]), bitvalue)
  4825  	s.vars[deferBitsVar] = newDeferBits
  4826  	s.store(types.Types[types.TUINT8], s.deferBitsAddr, newDeferBits)
  4827  }
  4828  
  4829  // openDeferSave generates SSA nodes to store a value (with type t) for an
  4830  // open-coded defer at an explicit autotmp location on the stack, so it can be
  4831  // reloaded and used for the appropriate call on exit. Type t must be a function type
  4832  // (therefore SSAable). val is the value to be stored. The function returns an SSA
  4833  // value representing a pointer to the autotmp location.
  4834  func (s *state) openDeferSave(t *types.Type, val *ssa.Value) *ssa.Value {
  4835  	if !ssa.CanSSA(t) {
  4836  		s.Fatalf("openDeferSave of non-SSA-able type %v val=%v", t, val)
  4837  	}
  4838  	if !t.HasPointers() {
  4839  		s.Fatalf("openDeferSave of pointerless type %v val=%v", t, val)
  4840  	}
  4841  	pos := val.Pos
  4842  	temp := typecheck.TempAt(pos.WithNotStmt(), s.curfn, t)
  4843  	temp.SetOpenDeferSlot(true)
  4844  	temp.SetFrameOffset(int64(len(s.openDefers))) // so cmpstackvarlt can order them
  4845  	var addrTemp *ssa.Value
  4846  	// Use OpVarLive to make sure stack slot for the closure is not removed by
  4847  	// dead-store elimination
  4848  	if s.curBlock.ID != s.f.Entry.ID {
  4849  		// Force the tmp storing this defer function to be declared in the entry
  4850  		// block, so that it will be live for the defer exit code (which will
  4851  		// actually access it only if the associated defer call has been activated).
  4852  		if t.HasPointers() {
  4853  			s.defvars[s.f.Entry.ID][memVar] = s.f.Entry.NewValue1A(src.NoXPos, ssa.OpVarDef, types.TypeMem, temp, s.defvars[s.f.Entry.ID][memVar])
  4854  		}
  4855  		s.defvars[s.f.Entry.ID][memVar] = s.f.Entry.NewValue1A(src.NoXPos, ssa.OpVarLive, types.TypeMem, temp, s.defvars[s.f.Entry.ID][memVar])
  4856  		addrTemp = s.f.Entry.NewValue2A(src.NoXPos, ssa.OpLocalAddr, types.NewPtr(temp.Type()), temp, s.sp, s.defvars[s.f.Entry.ID][memVar])
  4857  	} else {
  4858  		// Special case if we're still in the entry block. We can't use
  4859  		// the above code, since s.defvars[s.f.Entry.ID] isn't defined
  4860  		// until we end the entry block with s.endBlock().
  4861  		if t.HasPointers() {
  4862  			s.vars[memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, temp, s.mem(), false)
  4863  		}
  4864  		s.vars[memVar] = s.newValue1Apos(ssa.OpVarLive, types.TypeMem, temp, s.mem(), false)
  4865  		addrTemp = s.newValue2Apos(ssa.OpLocalAddr, types.NewPtr(temp.Type()), temp, s.sp, s.mem(), false)
  4866  	}
  4867  	// Since we may use this temp during exit depending on the
  4868  	// deferBits, we must define it unconditionally on entry.
  4869  	// Therefore, we must make sure it is zeroed out in the entry
  4870  	// block if it contains pointers, else GC may wrongly follow an
  4871  	// uninitialized pointer value.
  4872  	temp.SetNeedzero(true)
  4873  	// We are storing to the stack, hence we can avoid the full checks in
  4874  	// storeType() (no write barrier) and do a simple store().
  4875  	s.store(t, addrTemp, val)
  4876  	return addrTemp
  4877  }
  4878  
  4879  // openDeferExit generates SSA for processing all the open coded defers at exit.
  4880  // The code involves loading deferBits, and checking each of the bits to see if
  4881  // the corresponding defer statement was executed. For each bit that is turned
  4882  // on, the associated defer call is made.
  4883  func (s *state) openDeferExit() {
  4884  	deferExit := s.f.NewBlock(ssa.BlockPlain)
  4885  	s.endBlock().AddEdgeTo(deferExit)
  4886  	s.startBlock(deferExit)
  4887  	s.lastDeferExit = deferExit
  4888  	s.lastDeferCount = len(s.openDefers)
  4889  	zeroval := s.constInt8(types.Types[types.TUINT8], 0)
  4890  	// Test for and run defers in reverse order
  4891  	for i := len(s.openDefers) - 1; i >= 0; i-- {
  4892  		r := s.openDefers[i]
  4893  		bCond := s.f.NewBlock(ssa.BlockPlain)
  4894  		bEnd := s.f.NewBlock(ssa.BlockPlain)
  4895  
  4896  		deferBits := s.variable(deferBitsVar, types.Types[types.TUINT8])
  4897  		// Generate code to check if the bit associated with the current
  4898  		// defer is set.
  4899  		bitval := s.constInt8(types.Types[types.TUINT8], 1<<uint(i))
  4900  		andval := s.newValue2(ssa.OpAnd8, types.Types[types.TUINT8], deferBits, bitval)
  4901  		eqVal := s.newValue2(ssa.OpEq8, types.Types[types.TBOOL], andval, zeroval)
  4902  		b := s.endBlock()
  4903  		b.Kind = ssa.BlockIf
  4904  		b.SetControl(eqVal)
  4905  		b.AddEdgeTo(bEnd)
  4906  		b.AddEdgeTo(bCond)
  4907  		bCond.AddEdgeTo(bEnd)
  4908  		s.startBlock(bCond)
  4909  
  4910  		// Clear this bit in deferBits and force store back to stack, so
  4911  		// we will not try to re-run this defer call if this defer call panics.
  4912  		nbitval := s.newValue1(ssa.OpCom8, types.Types[types.TUINT8], bitval)
  4913  		maskedval := s.newValue2(ssa.OpAnd8, types.Types[types.TUINT8], deferBits, nbitval)
  4914  		s.store(types.Types[types.TUINT8], s.deferBitsAddr, maskedval)
  4915  		// Use this value for following tests, so we keep previous
  4916  		// bits cleared.
  4917  		s.vars[deferBitsVar] = maskedval
  4918  
  4919  		// Generate code to call the function call of the defer, using the
  4920  		// closure that were stored in argtmps at the point of the defer
  4921  		// statement.
  4922  		fn := r.n.Fun
  4923  		stksize := fn.Type().ArgWidth()
  4924  		var callArgs []*ssa.Value
  4925  		var call *ssa.Value
  4926  		if r.closure != nil {
  4927  			v := s.load(r.closure.Type.Elem(), r.closure)
  4928  			s.maybeNilCheckClosure(v, callDefer)
  4929  			codeptr := s.rawLoad(types.Types[types.TUINTPTR], v)
  4930  			aux := ssa.ClosureAuxCall(s.f.ABIDefault.ABIAnalyzeTypes(nil, nil))
  4931  			call = s.newValue2A(ssa.OpClosureLECall, aux.LateExpansionResultType(), aux, codeptr, v)
  4932  		} else {
  4933  			aux := ssa.StaticAuxCall(fn.(*ir.Name).Linksym(), s.f.ABIDefault.ABIAnalyzeTypes(nil, nil))
  4934  			call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux)
  4935  		}
  4936  		callArgs = append(callArgs, s.mem())
  4937  		call.AddArgs(callArgs...)
  4938  		call.AuxInt = stksize
  4939  		s.vars[memVar] = s.newValue1I(ssa.OpSelectN, types.TypeMem, 0, call)
  4940  		// Make sure that the stack slots with pointers are kept live
  4941  		// through the call (which is a pre-emption point). Also, we will
  4942  		// use the first call of the last defer exit to compute liveness
  4943  		// for the deferreturn, so we want all stack slots to be live.
  4944  		if r.closureNode != nil {
  4945  			s.vars[memVar] = s.newValue1Apos(ssa.OpVarLive, types.TypeMem, r.closureNode, s.mem(), false)
  4946  		}
  4947  
  4948  		s.endBlock()
  4949  		s.startBlock(bEnd)
  4950  	}
  4951  }
  4952  
  4953  func (s *state) callResult(n *ir.CallExpr, k callKind) *ssa.Value {
  4954  	return s.call(n, k, false, nil)
  4955  }
  4956  
  4957  func (s *state) callAddr(n *ir.CallExpr, k callKind) *ssa.Value {
  4958  	return s.call(n, k, true, nil)
  4959  }
  4960  
  4961  // Calls the function n using the specified call type.
  4962  // Returns the address of the return value (or nil if none).
  4963  func (s *state) call(n *ir.CallExpr, k callKind, returnResultAddr bool, deferExtra ir.Expr) *ssa.Value {
  4964  	s.prevCall = nil
  4965  	var calleeLSym *obj.LSym // target function (if static)
  4966  	var closure *ssa.Value   // ptr to closure to run (if dynamic)
  4967  	var codeptr *ssa.Value   // ptr to target code (if dynamic)
  4968  	var dextra *ssa.Value    // defer extra arg
  4969  	var rcvr *ssa.Value      // receiver to set
  4970  	fn := n.Fun
  4971  	var ACArgs []*types.Type    // AuxCall args
  4972  	var ACResults []*types.Type // AuxCall results
  4973  	var callArgs []*ssa.Value   // For late-expansion, the args themselves (not stored, args to the call instead).
  4974  
  4975  	callABI := s.f.ABIDefault
  4976  
  4977  	if k != callNormal && k != callTail && (len(n.Args) != 0 || n.Op() == ir.OCALLINTER || n.Fun.Type().NumResults() != 0) {
  4978  		s.Fatalf("go/defer call with arguments: %v", n)
  4979  	}
  4980  
  4981  	isCallDeferRangeFunc := false
  4982  
  4983  	switch n.Op() {
  4984  	case ir.OCALLFUNC:
  4985  		if (k == callNormal || k == callTail) && fn.Op() == ir.ONAME && fn.(*ir.Name).Class == ir.PFUNC {
  4986  			fn := fn.(*ir.Name)
  4987  			calleeLSym = callTargetLSym(fn)
  4988  			if buildcfg.Experiment.RegabiArgs {
  4989  				// This is a static call, so it may be
  4990  				// a direct call to a non-ABIInternal
  4991  				// function. fn.Func may be nil for
  4992  				// some compiler-generated functions,
  4993  				// but those are all ABIInternal.
  4994  				if fn.Func != nil {
  4995  					callABI = abiForFunc(fn.Func, s.f.ABI0, s.f.ABI1)
  4996  				}
  4997  			} else {
  4998  				// TODO(register args) remove after register abi is working
  4999  				inRegistersImported := fn.Pragma()&ir.RegisterParams != 0
  5000  				inRegistersSamePackage := fn.Func != nil && fn.Func.Pragma&ir.RegisterParams != 0
  5001  				if inRegistersImported || inRegistersSamePackage {
  5002  					callABI = s.f.ABI1
  5003  				}
  5004  			}
  5005  			if fn := n.Fun.Sym().Name; n.Fun.Sym().Pkg == ir.Pkgs.Runtime && fn == "deferrangefunc" {
  5006  				isCallDeferRangeFunc = true
  5007  			}
  5008  			break
  5009  		}
  5010  		closure = s.expr(fn)
  5011  		if k != callDefer && k != callDeferStack {
  5012  			// Deferred nil function needs to panic when the function is invoked,
  5013  			// not the point of defer statement.
  5014  			s.maybeNilCheckClosure(closure, k)
  5015  		}
  5016  	case ir.OCALLINTER:
  5017  		if fn.Op() != ir.ODOTINTER {
  5018  			s.Fatalf("OCALLINTER: n.Left not an ODOTINTER: %v", fn.Op())
  5019  		}
  5020  		fn := fn.(*ir.SelectorExpr)
  5021  		var iclosure *ssa.Value
  5022  		iclosure, rcvr = s.getClosureAndRcvr(fn)
  5023  		if k == callNormal || k == callTail {
  5024  			codeptr = s.load(types.Types[types.TUINTPTR], iclosure)
  5025  		} else {
  5026  			closure = iclosure
  5027  		}
  5028  	}
  5029  	if deferExtra != nil {
  5030  		dextra = s.expr(deferExtra)
  5031  	}
  5032  
  5033  	params := callABI.ABIAnalyze(n.Fun.Type(), false /* Do not set (register) nNames from caller side -- can cause races. */)
  5034  	types.CalcSize(fn.Type())
  5035  	stksize := params.ArgWidth() // includes receiver, args, and results
  5036  
  5037  	res := n.Fun.Type().Results()
  5038  	if k == callNormal || k == callTail {
  5039  		for _, p := range params.OutParams() {
  5040  			ACResults = append(ACResults, p.Type)
  5041  		}
  5042  	}
  5043  
  5044  	var call *ssa.Value
  5045  	if k == callDeferStack {
  5046  		if stksize != 0 {
  5047  			s.Fatalf("deferprocStack with non-zero stack size %d: %v", stksize, n)
  5048  		}
  5049  		// Make a defer struct on the stack.
  5050  		t := deferstruct()
  5051  		n, addr := s.temp(n.Pos(), t)
  5052  		n.SetNonMergeable(true)
  5053  		s.store(closure.Type,
  5054  			s.newValue1I(ssa.OpOffPtr, closure.Type.PtrTo(), t.FieldOff(deferStructFnField), addr),
  5055  			closure)
  5056  
  5057  		// Call runtime.deferprocStack with pointer to _defer record.
  5058  		ACArgs = append(ACArgs, types.Types[types.TUINTPTR])
  5059  		aux := ssa.StaticAuxCall(ir.Syms.DeferprocStack, s.f.ABIDefault.ABIAnalyzeTypes(ACArgs, ACResults))
  5060  		callArgs = append(callArgs, addr, s.mem())
  5061  		call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux)
  5062  		call.AddArgs(callArgs...)
  5063  		call.AuxInt = int64(types.PtrSize) // deferprocStack takes a *_defer arg
  5064  	} else {
  5065  		// Store arguments to stack, including defer/go arguments and receiver for method calls.
  5066  		// These are written in SP-offset order.
  5067  		argStart := base.Ctxt.Arch.FixedFrameSize
  5068  		// Defer/go args.
  5069  		if k != callNormal && k != callTail {
  5070  			// Write closure (arg to newproc/deferproc).
  5071  			ACArgs = append(ACArgs, types.Types[types.TUINTPTR]) // not argExtra
  5072  			callArgs = append(callArgs, closure)
  5073  			stksize += int64(types.PtrSize)
  5074  			argStart += int64(types.PtrSize)
  5075  			if dextra != nil {
  5076  				// Extra token of type any for deferproc
  5077  				ACArgs = append(ACArgs, types.Types[types.TINTER])
  5078  				callArgs = append(callArgs, dextra)
  5079  				stksize += 2 * int64(types.PtrSize)
  5080  				argStart += 2 * int64(types.PtrSize)
  5081  			}
  5082  		}
  5083  
  5084  		// Set receiver (for interface calls).
  5085  		if rcvr != nil {
  5086  			callArgs = append(callArgs, rcvr)
  5087  		}
  5088  
  5089  		// Write args.
  5090  		t := n.Fun.Type()
  5091  		args := n.Args
  5092  
  5093  		for _, p := range params.InParams() { // includes receiver for interface calls
  5094  			ACArgs = append(ACArgs, p.Type)
  5095  		}
  5096  
  5097  		// Split the entry block if there are open defers, because later calls to
  5098  		// openDeferSave may cause a mismatch between the mem for an OpDereference
  5099  		// and the call site which uses it. See #49282.
  5100  		if s.curBlock.ID == s.f.Entry.ID && s.hasOpenDefers {
  5101  			b := s.endBlock()
  5102  			b.Kind = ssa.BlockPlain
  5103  			curb := s.f.NewBlock(ssa.BlockPlain)
  5104  			b.AddEdgeTo(curb)
  5105  			s.startBlock(curb)
  5106  		}
  5107  
  5108  		for i, n := range args {
  5109  			callArgs = append(callArgs, s.putArg(n, t.Param(i).Type))
  5110  		}
  5111  
  5112  		callArgs = append(callArgs, s.mem())
  5113  
  5114  		// call target
  5115  		switch {
  5116  		case k == callDefer:
  5117  			sym := ir.Syms.Deferproc
  5118  			if dextra != nil {
  5119  				sym = ir.Syms.Deferprocat
  5120  			}
  5121  			aux := ssa.StaticAuxCall(sym, s.f.ABIDefault.ABIAnalyzeTypes(ACArgs, ACResults)) // TODO paramResultInfo for Deferproc(at)
  5122  			call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux)
  5123  		case k == callGo:
  5124  			aux := ssa.StaticAuxCall(ir.Syms.Newproc, s.f.ABIDefault.ABIAnalyzeTypes(ACArgs, ACResults))
  5125  			call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux) // TODO paramResultInfo for Newproc
  5126  		case closure != nil:
  5127  			// rawLoad because loading the code pointer from a
  5128  			// closure is always safe, but IsSanitizerSafeAddr
  5129  			// can't always figure that out currently, and it's
  5130  			// critical that we not clobber any arguments already
  5131  			// stored onto the stack.
  5132  			codeptr = s.rawLoad(types.Types[types.TUINTPTR], closure)
  5133  			aux := ssa.ClosureAuxCall(callABI.ABIAnalyzeTypes(ACArgs, ACResults))
  5134  			call = s.newValue2A(ssa.OpClosureLECall, aux.LateExpansionResultType(), aux, codeptr, closure)
  5135  		case codeptr != nil:
  5136  			// Note that the "receiver" parameter is nil because the actual receiver is the first input parameter.
  5137  			aux := ssa.InterfaceAuxCall(params)
  5138  			call = s.newValue1A(ssa.OpInterLECall, aux.LateExpansionResultType(), aux, codeptr)
  5139  			if k == callTail {
  5140  				call.Op = ssa.OpTailLECallInter
  5141  				stksize = 0 // Tail call does not use stack. We reuse caller's frame.
  5142  			}
  5143  		case calleeLSym != nil:
  5144  			aux := ssa.StaticAuxCall(calleeLSym, params)
  5145  			call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux)
  5146  			if k == callTail {
  5147  				call.Op = ssa.OpTailLECall
  5148  				stksize = 0 // Tail call does not use stack. We reuse caller's frame.
  5149  			}
  5150  		default:
  5151  			s.Fatalf("bad call type %v %v", n.Op(), n)
  5152  		}
  5153  		call.AddArgs(callArgs...)
  5154  		call.AuxInt = stksize // Call operations carry the argsize of the callee along with them
  5155  	}
  5156  	s.prevCall = call
  5157  	s.vars[memVar] = s.newValue1I(ssa.OpSelectN, types.TypeMem, int64(len(ACResults)), call)
  5158  	// Insert VarLive opcodes.
  5159  	for _, v := range n.KeepAlive {
  5160  		if !v.Addrtaken() {
  5161  			s.Fatalf("KeepAlive variable %v must have Addrtaken set", v)
  5162  		}
  5163  		switch v.Class {
  5164  		case ir.PAUTO, ir.PPARAM, ir.PPARAMOUT:
  5165  		default:
  5166  			s.Fatalf("KeepAlive variable %v must be Auto or Arg", v)
  5167  		}
  5168  		s.vars[memVar] = s.newValue1A(ssa.OpVarLive, types.TypeMem, v, s.mem())
  5169  	}
  5170  
  5171  	// Build result value (before we might end the defer block, below).
  5172  	var result *ssa.Value
  5173  	if len(res) == 0 || k != callNormal {
  5174  		result = nil
  5175  	} else {
  5176  		fp := res[0]
  5177  		if returnResultAddr {
  5178  			result = s.resultAddrOfCall(call, 0, fp.Type)
  5179  		} else {
  5180  			result = s.newValue1I(ssa.OpSelectN, fp.Type, 0, call)
  5181  		}
  5182  		if n.Reshape {
  5183  			result = s.newValue1(ssa.OpCopy, n.Type(), result)
  5184  		}
  5185  	}
  5186  
  5187  	// Finish block for defers
  5188  	if k == callDefer || k == callDeferStack || isCallDeferRangeFunc {
  5189  		b := s.endBlock()
  5190  		b.Kind = ssa.BlockDefer
  5191  		b.SetControl(call)
  5192  		bNext := s.f.NewBlock(ssa.BlockPlain)
  5193  		b.AddEdgeTo(bNext)
  5194  		r := s.f.DeferReturn // Share a single deferreturn among all defers
  5195  		if r == nil {
  5196  			r = s.f.NewBlock(ssa.BlockPlain)
  5197  			s.startBlock(r)
  5198  			s.exit()
  5199  			s.f.DeferReturn = r
  5200  		}
  5201  		b.AddEdgeTo(r) // Add recover edge to exit code.  This is a fake edge to keep the block live.
  5202  		b.Likely = ssa.BranchLikely
  5203  		s.startBlock(bNext)
  5204  	}
  5205  
  5206  	return result
  5207  }
  5208  
  5209  // maybeNilCheckClosure checks if a nil check of a closure is needed in some
  5210  // architecture-dependent situations and, if so, emits the nil check.
  5211  func (s *state) maybeNilCheckClosure(closure *ssa.Value, k callKind) {
  5212  	if Arch.LinkArch.Family == sys.Wasm || buildcfg.GOOS == "aix" && k != callGo {
  5213  		// 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.
  5214  		// TODO(neelance): On other architectures this should be eliminated by the optimization steps
  5215  		s.nilCheck(closure)
  5216  	}
  5217  }
  5218  
  5219  // getClosureAndRcvr returns values for the appropriate closure and receiver of an
  5220  // interface call
  5221  func (s *state) getClosureAndRcvr(fn *ir.SelectorExpr) (*ssa.Value, *ssa.Value) {
  5222  	i := s.expr(fn.X)
  5223  	itab := s.newValue1(ssa.OpITab, types.Types[types.TUINTPTR], i)
  5224  	s.nilCheck(itab)
  5225  	itabidx := fn.Offset() + rttype.ITab.OffsetOf("Fun")
  5226  	closure := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.UintptrPtr, itabidx, itab)
  5227  	rcvr := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, i)
  5228  	return closure, rcvr
  5229  }
  5230  
  5231  // etypesign returns the signed-ness of e, for integer/pointer etypes.
  5232  // -1 means signed, +1 means unsigned, 0 means non-integer/non-pointer.
  5233  func etypesign(e types.Kind) int8 {
  5234  	switch e {
  5235  	case types.TINT8, types.TINT16, types.TINT32, types.TINT64, types.TINT:
  5236  		return -1
  5237  	case types.TUINT8, types.TUINT16, types.TUINT32, types.TUINT64, types.TUINT, types.TUINTPTR, types.TUNSAFEPTR:
  5238  		return +1
  5239  	}
  5240  	return 0
  5241  }
  5242  
  5243  // addr converts the address of the expression n to SSA, adds it to s and returns the SSA result.
  5244  // The value that the returned Value represents is guaranteed to be non-nil.
  5245  func (s *state) addr(n ir.Node) *ssa.Value {
  5246  	if n.Op() != ir.ONAME {
  5247  		s.pushLine(n.Pos())
  5248  		defer s.popLine()
  5249  	}
  5250  
  5251  	if s.canSSA(n) {
  5252  		// This happens in weird, always-panics cases, like:
  5253  		//     var x [0][2]int
  5254  		//     x[i][j] = 5
  5255  		// The outer assignment, ...[j] = 5, is a fine
  5256  		// assignment to do, but requires computing the address
  5257  		// &x[i], which will always panic when evaluated.
  5258  		// We just return something reasonable in this case.
  5259  		// It will be dynamically unreachable. See issue 77635.
  5260  		return s.newValue1A(ssa.OpAddr, n.Type().PtrTo(), ir.Syms.Zerobase, s.sb)
  5261  	}
  5262  
  5263  	t := types.NewPtr(n.Type())
  5264  	linksymOffset := func(lsym *obj.LSym, offset int64) *ssa.Value {
  5265  		v := s.entryNewValue1A(ssa.OpAddr, t, lsym, s.sb)
  5266  		// TODO: Make OpAddr use AuxInt as well as Aux.
  5267  		if offset != 0 {
  5268  			v = s.entryNewValue1I(ssa.OpOffPtr, v.Type, offset, v)
  5269  		}
  5270  		return v
  5271  	}
  5272  	switch n.Op() {
  5273  	case ir.OLINKSYMOFFSET:
  5274  		no := n.(*ir.LinksymOffsetExpr)
  5275  		return linksymOffset(no.Linksym, no.Offset_)
  5276  	case ir.ONAME:
  5277  		n := n.(*ir.Name)
  5278  		if n.Heapaddr != nil {
  5279  			return s.expr(n.Heapaddr)
  5280  		}
  5281  		switch n.Class {
  5282  		case ir.PEXTERN:
  5283  			// global variable
  5284  			return linksymOffset(n.Linksym(), 0)
  5285  		case ir.PPARAM:
  5286  			// parameter slot
  5287  			v := s.decladdrs[n]
  5288  			if v != nil {
  5289  				return v
  5290  			}
  5291  			s.Fatalf("addr of undeclared ONAME %v. declared: %v", n, s.decladdrs)
  5292  			return nil
  5293  		case ir.PAUTO:
  5294  			return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), !ir.IsAutoTmp(n))
  5295  
  5296  		case ir.PPARAMOUT: // Same as PAUTO -- cannot generate LEA early.
  5297  			// ensure that we reuse symbols for out parameters so
  5298  			// that cse works on their addresses
  5299  			return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), true)
  5300  		default:
  5301  			s.Fatalf("variable address class %v not implemented", n.Class)
  5302  			return nil
  5303  		}
  5304  	case ir.ORESULT:
  5305  		// load return from callee
  5306  		n := n.(*ir.ResultExpr)
  5307  		return s.resultAddrOfCall(s.prevCall, n.Index, n.Type())
  5308  	case ir.OINDEX:
  5309  		n := n.(*ir.IndexExpr)
  5310  		if n.X.Type().IsSlice() {
  5311  			a := s.expr(n.X)
  5312  			i := s.expr(n.Index)
  5313  			len := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], a)
  5314  			i = s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded())
  5315  			p := s.newValue1(ssa.OpSlicePtr, t, a)
  5316  			return s.newValue2(ssa.OpPtrIndex, t, p, i)
  5317  		} else { // array
  5318  			a := s.addr(n.X)
  5319  			i := s.expr(n.Index)
  5320  			len := s.constInt(types.Types[types.TINT], n.X.Type().NumElem())
  5321  			i = s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded())
  5322  			return s.newValue2(ssa.OpPtrIndex, types.NewPtr(n.X.Type().Elem()), a, i)
  5323  		}
  5324  	case ir.ODEREF:
  5325  		n := n.(*ir.StarExpr)
  5326  		return s.exprPtr(n.X, n.Bounded(), n.Pos())
  5327  	case ir.ODOT:
  5328  		n := n.(*ir.SelectorExpr)
  5329  		p := s.addr(n.X)
  5330  		return s.newValue1I(ssa.OpOffPtr, t, n.Offset(), p)
  5331  	case ir.ODOTPTR:
  5332  		n := n.(*ir.SelectorExpr)
  5333  		p := s.exprPtr(n.X, n.Bounded(), n.Pos())
  5334  		return s.newValue1I(ssa.OpOffPtr, t, n.Offset(), p)
  5335  	case ir.OCONVNOP:
  5336  		n := n.(*ir.ConvExpr)
  5337  		if n.Type() == n.X.Type() {
  5338  			return s.addr(n.X)
  5339  		}
  5340  		addr := s.addr(n.X)
  5341  		return s.newValue1(ssa.OpCopy, t, addr) // ensure that addr has the right type
  5342  	case ir.OCALLFUNC, ir.OCALLINTER:
  5343  		n := n.(*ir.CallExpr)
  5344  		return s.callAddr(n, callNormal)
  5345  	case ir.ODOTTYPE, ir.ODYNAMICDOTTYPE:
  5346  		var v *ssa.Value
  5347  		if n.Op() == ir.ODOTTYPE {
  5348  			v, _ = s.dottype(n.(*ir.TypeAssertExpr), false)
  5349  		} else {
  5350  			v, _ = s.dynamicDottype(n.(*ir.DynamicTypeAssertExpr), false)
  5351  		}
  5352  		if v.Op != ssa.OpLoad {
  5353  			s.Fatalf("dottype of non-load")
  5354  		}
  5355  		if v.Args[1] != s.mem() {
  5356  			s.Fatalf("memory no longer live from dottype load")
  5357  		}
  5358  		return v.Args[0]
  5359  	default:
  5360  		s.Fatalf("unhandled addr %v", n.Op())
  5361  		return nil
  5362  	}
  5363  }
  5364  
  5365  // canSSA reports whether n is SSA-able.
  5366  // n must be an ONAME (or an ODOT sequence with an ONAME base).
  5367  func (s *state) canSSA(n ir.Node) bool {
  5368  	if base.Flag.N != 0 {
  5369  		return false
  5370  	}
  5371  	for {
  5372  		nn := n
  5373  		if nn.Op() == ir.ODOT {
  5374  			nn := nn.(*ir.SelectorExpr)
  5375  			n = nn.X
  5376  			continue
  5377  		}
  5378  		if nn.Op() == ir.OINDEX {
  5379  			nn := nn.(*ir.IndexExpr)
  5380  			if nn.X.Type().IsArray() {
  5381  				n = nn.X
  5382  				continue
  5383  			}
  5384  		}
  5385  		break
  5386  	}
  5387  	if n.Op() != ir.ONAME {
  5388  		return false
  5389  	}
  5390  	return s.canSSAName(n.(*ir.Name)) && ssa.CanSSA(n.Type())
  5391  }
  5392  
  5393  func (s *state) canSSAName(name *ir.Name) bool {
  5394  	if name.Addrtaken() || !name.OnStack() {
  5395  		return false
  5396  	}
  5397  	switch name.Class {
  5398  	case ir.PPARAMOUT:
  5399  		if s.hasdefer {
  5400  			// TODO: handle this case? Named return values must be
  5401  			// in memory so that the deferred function can see them.
  5402  			// Maybe do: if !strings.HasPrefix(n.String(), "~") { return false }
  5403  			// Or maybe not, see issue 18860.  Even unnamed return values
  5404  			// must be written back so if a defer recovers, the caller can see them.
  5405  			return false
  5406  		}
  5407  		if s.cgoUnsafeArgs {
  5408  			// Cgo effectively takes the address of all result args,
  5409  			// but the compiler can't see that.
  5410  			return false
  5411  		}
  5412  	}
  5413  	return true
  5414  	// TODO: try to make more variables SSAable?
  5415  }
  5416  
  5417  // exprPtr evaluates n to a pointer and nil-checks it.
  5418  func (s *state) exprPtr(n ir.Node, bounded bool, lineno src.XPos) *ssa.Value {
  5419  	p := s.expr(n)
  5420  	if bounded || n.NonNil() {
  5421  		if s.f.Frontend().Debug_checknil() && lineno.Line() > 1 {
  5422  			s.f.Warnl(lineno, "removed nil check")
  5423  		}
  5424  		return p
  5425  	}
  5426  	p = s.nilCheck(p)
  5427  	return p
  5428  }
  5429  
  5430  // nilCheck generates nil pointer checking code.
  5431  // Used only for automatically inserted nil checks,
  5432  // not for user code like 'x != nil'.
  5433  // Returns a "definitely not nil" copy of x to ensure proper ordering
  5434  // of the uses of the post-nilcheck pointer.
  5435  func (s *state) nilCheck(ptr *ssa.Value) *ssa.Value {
  5436  	if base.Debug.DisableNil != 0 || s.curfn.NilCheckDisabled() {
  5437  		return ptr
  5438  	}
  5439  	return s.newValue2(ssa.OpNilCheck, ptr.Type, ptr, s.mem())
  5440  }
  5441  
  5442  // boundsCheck generates bounds checking code. Checks if 0 <= idx <[=] len, branches to exit if not.
  5443  // Starts a new block on return.
  5444  // On input, len must be converted to full int width and be nonnegative.
  5445  // Returns idx converted to full int width.
  5446  // If bounded is true then caller guarantees the index is not out of bounds
  5447  // (but boundsCheck will still extend the index to full int width).
  5448  func (s *state) boundsCheck(idx, len *ssa.Value, kind ssa.BoundsKind, bounded bool) *ssa.Value {
  5449  	idx = s.extendIndex(idx, len, kind, bounded)
  5450  
  5451  	if bounded || base.Flag.B != 0 {
  5452  		// If bounded or bounds checking is flag-disabled, then no check necessary,
  5453  		// just return the extended index.
  5454  		//
  5455  		// Here, bounded == true if the compiler generated the index itself,
  5456  		// such as in the expansion of a slice initializer. These indexes are
  5457  		// compiler-generated, not Go program variables, so they cannot be
  5458  		// attacker-controlled, so we can omit Spectre masking as well.
  5459  		//
  5460  		// Note that we do not want to omit Spectre masking in code like:
  5461  		//
  5462  		//	if 0 <= i && i < len(x) {
  5463  		//		use(x[i])
  5464  		//	}
  5465  		//
  5466  		// Lucky for us, bounded==false for that code.
  5467  		// In that case (handled below), we emit a bound check (and Spectre mask)
  5468  		// and then the prove pass will remove the bounds check.
  5469  		// In theory the prove pass could potentially remove certain
  5470  		// Spectre masks, but it's very delicate and probably better
  5471  		// to be conservative and leave them all in.
  5472  		return idx
  5473  	}
  5474  
  5475  	bNext := s.f.NewBlock(ssa.BlockPlain)
  5476  	bPanic := s.f.NewBlock(ssa.BlockExit)
  5477  
  5478  	if !idx.Type.IsSigned() {
  5479  		switch kind {
  5480  		case ssa.BoundsIndex:
  5481  			kind = ssa.BoundsIndexU
  5482  		case ssa.BoundsSliceAlen:
  5483  			kind = ssa.BoundsSliceAlenU
  5484  		case ssa.BoundsSliceAcap:
  5485  			kind = ssa.BoundsSliceAcapU
  5486  		case ssa.BoundsSliceB:
  5487  			kind = ssa.BoundsSliceBU
  5488  		case ssa.BoundsSlice3Alen:
  5489  			kind = ssa.BoundsSlice3AlenU
  5490  		case ssa.BoundsSlice3Acap:
  5491  			kind = ssa.BoundsSlice3AcapU
  5492  		case ssa.BoundsSlice3B:
  5493  			kind = ssa.BoundsSlice3BU
  5494  		case ssa.BoundsSlice3C:
  5495  			kind = ssa.BoundsSlice3CU
  5496  		}
  5497  	}
  5498  
  5499  	var cmp *ssa.Value
  5500  	if kind == ssa.BoundsIndex || kind == ssa.BoundsIndexU {
  5501  		cmp = s.newValue2(ssa.OpIsInBounds, types.Types[types.TBOOL], idx, len)
  5502  	} else {
  5503  		cmp = s.newValue2(ssa.OpIsSliceInBounds, types.Types[types.TBOOL], idx, len)
  5504  	}
  5505  	b := s.endBlock()
  5506  	b.Kind = ssa.BlockIf
  5507  	b.SetControl(cmp)
  5508  	b.Likely = ssa.BranchLikely
  5509  	b.AddEdgeTo(bNext)
  5510  	b.AddEdgeTo(bPanic)
  5511  
  5512  	s.startBlock(bPanic)
  5513  	if Arch.LinkArch.Family == sys.Wasm {
  5514  		// TODO(khr): figure out how to do "register" based calling convention for bounds checks.
  5515  		// Should be similar to gcWriteBarrier, but I can't make it work.
  5516  		s.rtcall(BoundsCheckFunc[kind], false, nil, idx, len)
  5517  	} else {
  5518  		mem := s.newValue3I(ssa.OpPanicBounds, types.TypeMem, int64(kind), idx, len, s.mem())
  5519  		s.endBlock().SetControl(mem)
  5520  	}
  5521  	s.startBlock(bNext)
  5522  
  5523  	// In Spectre index mode, apply an appropriate mask to avoid speculative out-of-bounds accesses.
  5524  	if base.Flag.Cfg.SpectreIndex {
  5525  		op := ssa.OpSpectreIndex
  5526  		if kind != ssa.BoundsIndex && kind != ssa.BoundsIndexU {
  5527  			op = ssa.OpSpectreSliceIndex
  5528  		}
  5529  		idx = s.newValue2(op, types.Types[types.TINT], idx, len)
  5530  	}
  5531  
  5532  	return idx
  5533  }
  5534  
  5535  // If cmp (a bool) is false, panic using the given function.
  5536  func (s *state) check(cmp *ssa.Value, fn *obj.LSym) {
  5537  	b := s.endBlock()
  5538  	b.Kind = ssa.BlockIf
  5539  	b.SetControl(cmp)
  5540  	b.Likely = ssa.BranchLikely
  5541  	bNext := s.f.NewBlock(ssa.BlockPlain)
  5542  	line := s.peekPos()
  5543  	pos := base.Ctxt.PosTable.Pos(line)
  5544  	fl := funcLine{f: fn, base: pos.Base(), line: pos.Line()}
  5545  	bPanic := s.panics[fl]
  5546  	if bPanic == nil {
  5547  		bPanic = s.f.NewBlock(ssa.BlockPlain)
  5548  		s.panics[fl] = bPanic
  5549  		s.startBlock(bPanic)
  5550  		// The panic call takes/returns memory to ensure that the right
  5551  		// memory state is observed if the panic happens.
  5552  		s.rtcall(fn, false, nil)
  5553  	}
  5554  	b.AddEdgeTo(bNext)
  5555  	b.AddEdgeTo(bPanic)
  5556  	s.startBlock(bNext)
  5557  }
  5558  
  5559  func (s *state) intDivide(n ir.Node, a, b *ssa.Value) *ssa.Value {
  5560  	needcheck := true
  5561  	switch b.Op {
  5562  	case ssa.OpConst8, ssa.OpConst16, ssa.OpConst32, ssa.OpConst64:
  5563  		if b.AuxInt != 0 {
  5564  			needcheck = false
  5565  		}
  5566  	}
  5567  	if needcheck {
  5568  		// do a size-appropriate check for zero
  5569  		cmp := s.newValue2(s.ssaOp(ir.ONE, n.Type()), types.Types[types.TBOOL], b, s.zeroVal(n.Type()))
  5570  		s.check(cmp, ir.Syms.Panicdivide)
  5571  	}
  5572  	return s.newValue2(s.ssaOp(n.Op(), n.Type()), a.Type, a, b)
  5573  }
  5574  
  5575  // rtcall issues a call to the given runtime function fn with the listed args.
  5576  // Returns a slice of results of the given result types.
  5577  // The call is added to the end of the current block.
  5578  // If returns is false, the block is marked as an exit block.
  5579  func (s *state) rtcall(fn *obj.LSym, returns bool, results []*types.Type, args ...*ssa.Value) []*ssa.Value {
  5580  	s.prevCall = nil
  5581  	// Write args to the stack
  5582  	off := base.Ctxt.Arch.FixedFrameSize
  5583  	var callArgs []*ssa.Value
  5584  	var callArgTypes []*types.Type
  5585  
  5586  	for _, arg := range args {
  5587  		t := arg.Type
  5588  		off = types.RoundUp(off, t.Alignment())
  5589  		size := t.Size()
  5590  		callArgs = append(callArgs, arg)
  5591  		callArgTypes = append(callArgTypes, t)
  5592  		off += size
  5593  	}
  5594  	off = types.RoundUp(off, int64(types.RegSize))
  5595  
  5596  	// Issue call
  5597  	var call *ssa.Value
  5598  	aux := ssa.StaticAuxCall(fn, s.f.ABIDefault.ABIAnalyzeTypes(callArgTypes, results))
  5599  	callArgs = append(callArgs, s.mem())
  5600  	call = s.newValue0A(ssa.OpStaticLECall, aux.LateExpansionResultType(), aux)
  5601  	call.AddArgs(callArgs...)
  5602  	s.vars[memVar] = s.newValue1I(ssa.OpSelectN, types.TypeMem, int64(len(results)), call)
  5603  
  5604  	if !returns {
  5605  		// Finish block
  5606  		b := s.endBlock()
  5607  		b.Kind = ssa.BlockExit
  5608  		b.SetControl(call)
  5609  		call.AuxInt = off - base.Ctxt.Arch.FixedFrameSize
  5610  		if len(results) > 0 {
  5611  			s.Fatalf("panic call can't have results")
  5612  		}
  5613  		return nil
  5614  	}
  5615  
  5616  	// Load results
  5617  	res := make([]*ssa.Value, len(results))
  5618  	for i, t := range results {
  5619  		off = types.RoundUp(off, t.Alignment())
  5620  		res[i] = s.resultOfCall(call, int64(i), t)
  5621  		off += t.Size()
  5622  	}
  5623  	off = types.RoundUp(off, int64(types.PtrSize))
  5624  
  5625  	// Remember how much callee stack space we needed.
  5626  	call.AuxInt = off
  5627  
  5628  	return res
  5629  }
  5630  
  5631  // do *left = right for type t.
  5632  func (s *state) storeType(t *types.Type, left, right *ssa.Value, skip skipMask, leftIsStmt bool) {
  5633  	s.instrument(t, left, instrumentWrite)
  5634  
  5635  	if skip == 0 && (!t.HasPointers() || ssa.IsStackAddr(left)) {
  5636  		// Known to not have write barrier. Store the whole type.
  5637  		s.vars[memVar] = s.newValue3Apos(ssa.OpStore, types.TypeMem, t, left, right, s.mem(), leftIsStmt)
  5638  		return
  5639  	}
  5640  
  5641  	// store scalar fields first, so write barrier stores for
  5642  	// pointer fields can be grouped together, and scalar values
  5643  	// don't need to be live across the write barrier call.
  5644  	// TODO: if the writebarrier pass knows how to reorder stores,
  5645  	// we can do a single store here as long as skip==0.
  5646  	s.storeTypeScalars(t, left, right, skip)
  5647  	if skip&skipPtr == 0 && t.HasPointers() {
  5648  		s.storeTypePtrs(t, left, right)
  5649  	}
  5650  }
  5651  
  5652  // do *left = right for all scalar (non-pointer) parts of t.
  5653  func (s *state) storeTypeScalars(t *types.Type, left, right *ssa.Value, skip skipMask) {
  5654  	switch {
  5655  	case t.IsBoolean() || t.IsInteger() || t.IsFloat() || t.IsComplex() || t.IsSIMD():
  5656  		s.store(t, left, right)
  5657  	case t.IsPtrShaped():
  5658  		if t.IsPtr() && t.Elem().NotInHeap() {
  5659  			s.store(t, left, right) // see issue 42032
  5660  		}
  5661  		// otherwise, no scalar fields.
  5662  	case t.IsString():
  5663  		if skip&skipLen != 0 {
  5664  			return
  5665  		}
  5666  		len := s.newValue1(ssa.OpStringLen, types.Types[types.TINT], right)
  5667  		lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left)
  5668  		s.store(types.Types[types.TINT], lenAddr, len)
  5669  	case t.IsSlice():
  5670  		if skip&skipLen == 0 {
  5671  			len := s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], right)
  5672  			lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left)
  5673  			s.store(types.Types[types.TINT], lenAddr, len)
  5674  		}
  5675  		if skip&skipCap == 0 {
  5676  			cap := s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], right)
  5677  			capAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, 2*s.config.PtrSize, left)
  5678  			s.store(types.Types[types.TINT], capAddr, cap)
  5679  		}
  5680  	case t.IsInterface():
  5681  		// itab field doesn't need a write barrier (even though it is a pointer).
  5682  		itab := s.newValue1(ssa.OpITab, s.f.Config.Types.BytePtr, right)
  5683  		s.store(types.Types[types.TUINTPTR], left, itab)
  5684  	case isStructNotSIMD(t):
  5685  		n := t.NumFields()
  5686  		for i := 0; i < n; i++ {
  5687  			ft := t.FieldType(i)
  5688  			addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
  5689  			val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
  5690  			s.storeTypeScalars(ft, addr, val, 0)
  5691  		}
  5692  	case t.IsArray() && t.Size() == 0:
  5693  		// nothing
  5694  	case t.IsArray() && t.NumElem() == 1:
  5695  		s.storeTypeScalars(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right), 0)
  5696  	default:
  5697  		s.Fatalf("bad write barrier type %v", t)
  5698  	}
  5699  }
  5700  
  5701  // do *left = right for all pointer parts of t.
  5702  func (s *state) storeTypePtrs(t *types.Type, left, right *ssa.Value) {
  5703  	switch {
  5704  	case t.IsPtrShaped():
  5705  		if t.IsPtr() && t.Elem().NotInHeap() {
  5706  			break // see issue 42032
  5707  		}
  5708  		s.store(t, left, right)
  5709  	case t.IsString():
  5710  		ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, right)
  5711  		s.store(s.f.Config.Types.BytePtr, left, ptr)
  5712  	case t.IsSlice():
  5713  		elType := types.NewPtr(t.Elem())
  5714  		ptr := s.newValue1(ssa.OpSlicePtr, elType, right)
  5715  		s.store(elType, left, ptr)
  5716  	case t.IsInterface():
  5717  		// itab field is treated as a scalar.
  5718  		idata := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, right)
  5719  		idataAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.BytePtrPtr, s.config.PtrSize, left)
  5720  		s.store(s.f.Config.Types.BytePtr, idataAddr, idata)
  5721  	case isStructNotSIMD(t):
  5722  		n := t.NumFields()
  5723  		for i := 0; i < n; i++ {
  5724  			ft := t.FieldType(i)
  5725  			if !ft.HasPointers() {
  5726  				continue
  5727  			}
  5728  			addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
  5729  			val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
  5730  			s.storeTypePtrs(ft, addr, val)
  5731  		}
  5732  	case t.IsArray() && t.Size() == 0:
  5733  		// nothing
  5734  	case t.IsArray() && t.NumElem() == 1:
  5735  		s.storeTypePtrs(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right))
  5736  	default:
  5737  		s.Fatalf("bad write barrier type %v", t)
  5738  	}
  5739  }
  5740  
  5741  // putArg evaluates n for the purpose of passing it as an argument to a function and returns the value for the call.
  5742  func (s *state) putArg(n ir.Node, t *types.Type) *ssa.Value {
  5743  	var a *ssa.Value
  5744  	if !ssa.CanSSA(t) {
  5745  		a = s.newValue2(ssa.OpDereference, t, s.addr(n), s.mem())
  5746  	} else {
  5747  		a = s.expr(n)
  5748  	}
  5749  	return a
  5750  }
  5751  
  5752  // slice computes the slice v[i:j:k] and returns ptr, len, and cap of result.
  5753  // i,j,k may be nil, in which case they are set to their default value.
  5754  // v may be a slice, string or pointer to an array.
  5755  func (s *state) slice(v, i, j, k *ssa.Value, bounded bool) (p, l, c *ssa.Value) {
  5756  	t := v.Type
  5757  	var ptr, len, cap *ssa.Value
  5758  	switch {
  5759  	case t.IsSlice():
  5760  		ptr = s.newValue1(ssa.OpSlicePtr, types.NewPtr(t.Elem()), v)
  5761  		len = s.newValue1(ssa.OpSliceLen, types.Types[types.TINT], v)
  5762  		cap = s.newValue1(ssa.OpSliceCap, types.Types[types.TINT], v)
  5763  	case t.IsString():
  5764  		ptr = s.newValue1(ssa.OpStringPtr, types.NewPtr(types.Types[types.TUINT8]), v)
  5765  		len = s.newValue1(ssa.OpStringLen, types.Types[types.TINT], v)
  5766  		cap = len
  5767  	case t.IsPtr():
  5768  		if !t.Elem().IsArray() {
  5769  			s.Fatalf("bad ptr to array in slice %v\n", t)
  5770  		}
  5771  		nv := s.nilCheck(v)
  5772  		ptr = s.newValue1(ssa.OpCopy, types.NewPtr(t.Elem().Elem()), nv)
  5773  		len = s.constInt(types.Types[types.TINT], t.Elem().NumElem())
  5774  		cap = len
  5775  	default:
  5776  		s.Fatalf("bad type in slice %v\n", t)
  5777  	}
  5778  
  5779  	// Set default values
  5780  	if i == nil {
  5781  		i = s.constInt(types.Types[types.TINT], 0)
  5782  	}
  5783  	if j == nil {
  5784  		j = len
  5785  	}
  5786  	three := true
  5787  	if k == nil {
  5788  		three = false
  5789  		k = cap
  5790  	}
  5791  
  5792  	// Panic if slice indices are not in bounds.
  5793  	// Make sure we check these in reverse order so that we're always
  5794  	// comparing against a value known to be nonnegative. See issue 28797.
  5795  	if three {
  5796  		if k != cap {
  5797  			kind := ssa.BoundsSlice3Alen
  5798  			if t.IsSlice() {
  5799  				kind = ssa.BoundsSlice3Acap
  5800  			}
  5801  			k = s.boundsCheck(k, cap, kind, bounded)
  5802  		}
  5803  		if j != k {
  5804  			j = s.boundsCheck(j, k, ssa.BoundsSlice3B, bounded)
  5805  		}
  5806  		i = s.boundsCheck(i, j, ssa.BoundsSlice3C, bounded)
  5807  	} else {
  5808  		if j != k {
  5809  			kind := ssa.BoundsSliceAlen
  5810  			if t.IsSlice() {
  5811  				kind = ssa.BoundsSliceAcap
  5812  			}
  5813  			j = s.boundsCheck(j, k, kind, bounded)
  5814  		}
  5815  		i = s.boundsCheck(i, j, ssa.BoundsSliceB, bounded)
  5816  	}
  5817  
  5818  	// Word-sized integer operations.
  5819  	subOp := s.ssaOp(ir.OSUB, types.Types[types.TINT])
  5820  	mulOp := s.ssaOp(ir.OMUL, types.Types[types.TINT])
  5821  	andOp := s.ssaOp(ir.OAND, types.Types[types.TINT])
  5822  
  5823  	// Calculate the length (rlen) and capacity (rcap) of the new slice.
  5824  	// For strings the capacity of the result is unimportant. However,
  5825  	// we use rcap to test if we've generated a zero-length slice.
  5826  	// Use length of strings for that.
  5827  	rlen := s.newValue2(subOp, types.Types[types.TINT], j, i)
  5828  	rcap := rlen
  5829  	if j != k && !t.IsString() {
  5830  		rcap = s.newValue2(subOp, types.Types[types.TINT], k, i)
  5831  	}
  5832  
  5833  	if (i.Op == ssa.OpConst64 || i.Op == ssa.OpConst32) && i.AuxInt == 0 {
  5834  		// No pointer arithmetic necessary.
  5835  		return ptr, rlen, rcap
  5836  	}
  5837  
  5838  	// Calculate the base pointer (rptr) for the new slice.
  5839  	//
  5840  	// Generate the following code assuming that indexes are in bounds.
  5841  	// The masking is to make sure that we don't generate a slice
  5842  	// that points to the next object in memory. We cannot just set
  5843  	// the pointer to nil because then we would create a nil slice or
  5844  	// string.
  5845  	//
  5846  	//     rcap = k - i
  5847  	//     rlen = j - i
  5848  	//     rptr = ptr + (mask(rcap) & (i * stride))
  5849  	//
  5850  	// Where mask(x) is 0 if x==0 and -1 if x>0 and stride is the width
  5851  	// of the element type.
  5852  	stride := s.constInt(types.Types[types.TINT], ptr.Type.Elem().Size())
  5853  
  5854  	// The delta is the number of bytes to offset ptr by.
  5855  	delta := s.newValue2(mulOp, types.Types[types.TINT], i, stride)
  5856  
  5857  	// If we're slicing to the point where the capacity is zero,
  5858  	// zero out the delta.
  5859  	mask := s.newValue1(ssa.OpSlicemask, types.Types[types.TINT], rcap)
  5860  	delta = s.newValue2(andOp, types.Types[types.TINT], delta, mask)
  5861  
  5862  	// Compute rptr = ptr + delta.
  5863  	rptr := s.newValue2(ssa.OpAddPtr, ptr.Type, ptr, delta)
  5864  
  5865  	return rptr, rlen, rcap
  5866  }
  5867  
  5868  type u642fcvtTab struct {
  5869  	leq, cvt2F, and, rsh, or, add ssa.Op
  5870  	one                           func(*state, *types.Type, int64) *ssa.Value
  5871  }
  5872  
  5873  var u64_f64 = u642fcvtTab{
  5874  	leq:   ssa.OpLeq64,
  5875  	cvt2F: ssa.OpCvt64to64F,
  5876  	and:   ssa.OpAnd64,
  5877  	rsh:   ssa.OpRsh64Ux64,
  5878  	or:    ssa.OpOr64,
  5879  	add:   ssa.OpAdd64F,
  5880  	one:   (*state).constInt64,
  5881  }
  5882  
  5883  var u64_f32 = u642fcvtTab{
  5884  	leq:   ssa.OpLeq64,
  5885  	cvt2F: ssa.OpCvt64to32F,
  5886  	and:   ssa.OpAnd64,
  5887  	rsh:   ssa.OpRsh64Ux64,
  5888  	or:    ssa.OpOr64,
  5889  	add:   ssa.OpAdd32F,
  5890  	one:   (*state).constInt64,
  5891  }
  5892  
  5893  func (s *state) uint64Tofloat64(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5894  	return s.uint64Tofloat(&u64_f64, n, x, ft, tt)
  5895  }
  5896  
  5897  func (s *state) uint64Tofloat32(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5898  	return s.uint64Tofloat(&u64_f32, n, x, ft, tt)
  5899  }
  5900  
  5901  func (s *state) uint64Tofloat(cvttab *u642fcvtTab, n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5902  	// if x >= 0 {
  5903  	//    result = (floatY) x
  5904  	// } else {
  5905  	// 	  y = uintX(x) ; y = x & 1
  5906  	// 	  z = uintX(x) ; z = z >> 1
  5907  	// 	  z = z | y
  5908  	// 	  result = floatY(z)
  5909  	// 	  result = result + result
  5910  	// }
  5911  	//
  5912  	// Code borrowed from old code generator.
  5913  	// What's going on: large 64-bit "unsigned" looks like
  5914  	// negative number to hardware's integer-to-float
  5915  	// conversion. However, because the mantissa is only
  5916  	// 63 bits, we don't need the LSB, so instead we do an
  5917  	// unsigned right shift (divide by two), convert, and
  5918  	// double. However, before we do that, we need to be
  5919  	// sure that we do not lose a "1" if that made the
  5920  	// difference in the resulting rounding. Therefore, we
  5921  	// preserve it, and OR (not ADD) it back in. The case
  5922  	// that matters is when the eleven discarded bits are
  5923  	// equal to 10000000001; that rounds up, and the 1 cannot
  5924  	// be lost else it would round down if the LSB of the
  5925  	// candidate mantissa is 0.
  5926  
  5927  	cmp := s.newValue2(cvttab.leq, types.Types[types.TBOOL], s.zeroVal(ft), x)
  5928  
  5929  	b := s.endBlock()
  5930  	b.Kind = ssa.BlockIf
  5931  	b.SetControl(cmp)
  5932  	b.Likely = ssa.BranchLikely
  5933  
  5934  	bThen := s.f.NewBlock(ssa.BlockPlain)
  5935  	bElse := s.f.NewBlock(ssa.BlockPlain)
  5936  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  5937  
  5938  	b.AddEdgeTo(bThen)
  5939  	s.startBlock(bThen)
  5940  	a0 := s.newValue1(cvttab.cvt2F, tt, x)
  5941  	s.vars[n] = a0
  5942  	s.endBlock()
  5943  	bThen.AddEdgeTo(bAfter)
  5944  
  5945  	b.AddEdgeTo(bElse)
  5946  	s.startBlock(bElse)
  5947  	one := cvttab.one(s, ft, 1)
  5948  	y := s.newValue2(cvttab.and, ft, x, one)
  5949  	z := s.newValue2(cvttab.rsh, ft, x, one)
  5950  	z = s.newValue2(cvttab.or, ft, z, y)
  5951  	a := s.newValue1(cvttab.cvt2F, tt, z)
  5952  	a1 := s.newValue2(cvttab.add, tt, a, a)
  5953  	s.vars[n] = a1
  5954  	s.endBlock()
  5955  	bElse.AddEdgeTo(bAfter)
  5956  
  5957  	s.startBlock(bAfter)
  5958  	return s.variable(n, n.Type())
  5959  }
  5960  
  5961  type u322fcvtTab struct {
  5962  	cvtI2F, cvtF2F ssa.Op
  5963  }
  5964  
  5965  var u32_f64 = u322fcvtTab{
  5966  	cvtI2F: ssa.OpCvt32to64F,
  5967  	cvtF2F: ssa.OpCopy,
  5968  }
  5969  
  5970  var u32_f32 = u322fcvtTab{
  5971  	cvtI2F: ssa.OpCvt32to32F,
  5972  	cvtF2F: ssa.OpCvt64Fto32F,
  5973  }
  5974  
  5975  func (s *state) uint32Tofloat64(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5976  	return s.uint32Tofloat(&u32_f64, n, x, ft, tt)
  5977  }
  5978  
  5979  func (s *state) uint32Tofloat32(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5980  	return s.uint32Tofloat(&u32_f32, n, x, ft, tt)
  5981  }
  5982  
  5983  func (s *state) uint32Tofloat(cvttab *u322fcvtTab, n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  5984  	// if x >= 0 {
  5985  	// 	result = floatY(x)
  5986  	// } else {
  5987  	// 	result = floatY(float64(x) + (1<<32))
  5988  	// }
  5989  	cmp := s.newValue2(ssa.OpLeq32, types.Types[types.TBOOL], s.zeroVal(ft), x)
  5990  	b := s.endBlock()
  5991  	b.Kind = ssa.BlockIf
  5992  	b.SetControl(cmp)
  5993  	b.Likely = ssa.BranchLikely
  5994  
  5995  	bThen := s.f.NewBlock(ssa.BlockPlain)
  5996  	bElse := s.f.NewBlock(ssa.BlockPlain)
  5997  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  5998  
  5999  	b.AddEdgeTo(bThen)
  6000  	s.startBlock(bThen)
  6001  	a0 := s.newValue1(cvttab.cvtI2F, tt, x)
  6002  	s.vars[n] = a0
  6003  	s.endBlock()
  6004  	bThen.AddEdgeTo(bAfter)
  6005  
  6006  	b.AddEdgeTo(bElse)
  6007  	s.startBlock(bElse)
  6008  	a1 := s.newValue1(ssa.OpCvt32to64F, types.Types[types.TFLOAT64], x)
  6009  	twoToThe32 := s.constFloat64(types.Types[types.TFLOAT64], float64(1<<32))
  6010  	a2 := s.newValue2(ssa.OpAdd64F, types.Types[types.TFLOAT64], a1, twoToThe32)
  6011  	a3 := s.newValue1(cvttab.cvtF2F, tt, a2)
  6012  
  6013  	s.vars[n] = a3
  6014  	s.endBlock()
  6015  	bElse.AddEdgeTo(bAfter)
  6016  
  6017  	s.startBlock(bAfter)
  6018  	return s.variable(n, n.Type())
  6019  }
  6020  
  6021  // referenceTypeBuiltin generates code for the len/cap builtins for maps and channels.
  6022  func (s *state) referenceTypeBuiltin(n *ir.UnaryExpr, x *ssa.Value) *ssa.Value {
  6023  	if !n.X.Type().IsMap() && !n.X.Type().IsChan() {
  6024  		s.Fatalf("node must be a map or a channel")
  6025  	}
  6026  	if n.X.Type().IsChan() && n.Op() == ir.OLEN {
  6027  		s.Fatalf("cannot inline len(chan)") // must use runtime.chanlen now
  6028  	}
  6029  	if n.X.Type().IsChan() && n.Op() == ir.OCAP {
  6030  		s.Fatalf("cannot inline cap(chan)") // must use runtime.chancap now
  6031  	}
  6032  	if n.X.Type().IsMap() && n.Op() == ir.OCAP {
  6033  		s.Fatalf("cannot inline cap(map)") // cap(map) does not exist
  6034  	}
  6035  	// if n == nil {
  6036  	//   return 0
  6037  	// } else {
  6038  	//   // len, the actual loadType depends
  6039  	//   return int(*((*loadType)n))
  6040  	//   // cap (chan only, not used for now)
  6041  	//   return *(((*int)n)+1)
  6042  	// }
  6043  	lenType := n.Type()
  6044  	nilValue := s.constNil(types.Types[types.TUINTPTR])
  6045  	cmp := s.newValue2(ssa.OpEqPtr, types.Types[types.TBOOL], x, nilValue)
  6046  	b := s.endBlock()
  6047  	b.Kind = ssa.BlockIf
  6048  	b.SetControl(cmp)
  6049  	b.Likely = ssa.BranchUnlikely
  6050  
  6051  	bThen := s.f.NewBlock(ssa.BlockPlain)
  6052  	bElse := s.f.NewBlock(ssa.BlockPlain)
  6053  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  6054  
  6055  	// length/capacity of a nil map/chan is zero
  6056  	b.AddEdgeTo(bThen)
  6057  	s.startBlock(bThen)
  6058  	s.vars[n] = s.zeroVal(lenType)
  6059  	s.endBlock()
  6060  	bThen.AddEdgeTo(bAfter)
  6061  
  6062  	b.AddEdgeTo(bElse)
  6063  	s.startBlock(bElse)
  6064  	switch n.Op() {
  6065  	case ir.OLEN:
  6066  		if n.X.Type().IsMap() {
  6067  			// length is stored in the first word, but needs conversion to int.
  6068  			loadType := reflectdata.MapType().Field(0).Type // uint64
  6069  			load := s.load(loadType, x)
  6070  			s.vars[n] = s.conv(nil, load, loadType, lenType) // integer conversion doesn't need Node
  6071  		} else {
  6072  			// length is stored in the first word for chan, no conversion needed.
  6073  			s.vars[n] = s.load(lenType, x)
  6074  		}
  6075  	case ir.OCAP:
  6076  		// capacity is stored in the second word for chan
  6077  		sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Size(), x)
  6078  		s.vars[n] = s.load(lenType, sw)
  6079  	default:
  6080  		s.Fatalf("op must be OLEN or OCAP")
  6081  	}
  6082  	s.endBlock()
  6083  	bElse.AddEdgeTo(bAfter)
  6084  
  6085  	s.startBlock(bAfter)
  6086  	return s.variable(n, lenType)
  6087  }
  6088  
  6089  type f2uCvtTab struct {
  6090  	ltf, cvt2U, subf, or ssa.Op
  6091  	floatValue           func(*state, *types.Type, float64) *ssa.Value
  6092  	intValue             func(*state, *types.Type, int64) *ssa.Value
  6093  	cutoff               uint64
  6094  }
  6095  
  6096  var f32_u64 = f2uCvtTab{
  6097  	ltf:        ssa.OpLess32F,
  6098  	cvt2U:      ssa.OpCvt32Fto64,
  6099  	subf:       ssa.OpSub32F,
  6100  	or:         ssa.OpOr64,
  6101  	floatValue: (*state).constFloat32,
  6102  	intValue:   (*state).constInt64,
  6103  	cutoff:     1 << 63,
  6104  }
  6105  
  6106  var f64_u64 = f2uCvtTab{
  6107  	ltf:        ssa.OpLess64F,
  6108  	cvt2U:      ssa.OpCvt64Fto64,
  6109  	subf:       ssa.OpSub64F,
  6110  	or:         ssa.OpOr64,
  6111  	floatValue: (*state).constFloat64,
  6112  	intValue:   (*state).constInt64,
  6113  	cutoff:     1 << 63,
  6114  }
  6115  
  6116  var f32_u32 = f2uCvtTab{
  6117  	ltf:        ssa.OpLess32F,
  6118  	cvt2U:      ssa.OpCvt32Fto32,
  6119  	subf:       ssa.OpSub32F,
  6120  	or:         ssa.OpOr32,
  6121  	floatValue: (*state).constFloat32,
  6122  	intValue:   func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) },
  6123  	cutoff:     1 << 31,
  6124  }
  6125  
  6126  var f64_u32 = f2uCvtTab{
  6127  	ltf:        ssa.OpLess64F,
  6128  	cvt2U:      ssa.OpCvt64Fto32,
  6129  	subf:       ssa.OpSub64F,
  6130  	or:         ssa.OpOr32,
  6131  	floatValue: (*state).constFloat64,
  6132  	intValue:   func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) },
  6133  	cutoff:     1 << 31,
  6134  }
  6135  
  6136  func (s *state) float32ToUint64(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  6137  	return s.floatToUint(&f32_u64, n, x, ft, tt)
  6138  }
  6139  func (s *state) float64ToUint64(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  6140  	return s.floatToUint(&f64_u64, n, x, ft, tt)
  6141  }
  6142  
  6143  func (s *state) float32ToUint32(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  6144  	return s.floatToUint(&f32_u32, n, x, ft, tt)
  6145  }
  6146  
  6147  func (s *state) float64ToUint32(n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  6148  	return s.floatToUint(&f64_u32, n, x, ft, tt)
  6149  }
  6150  
  6151  func (s *state) floatToUint(cvttab *f2uCvtTab, n ir.Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value {
  6152  	// cutoff:=1<<(intY_Size-1)
  6153  	// if x < floatX(cutoff) {
  6154  	// 	result = uintY(x) // bThen
  6155  	//  // gated by ConvertHash, clamp negative inputs to zero
  6156  	// 	if x < 0 { // unlikely
  6157  	// 		result = 0 // bZero
  6158  	// 	}
  6159  	// } else {
  6160  	// 	y = x - floatX(cutoff) // bElse
  6161  	// 	z = uintY(y)
  6162  	// 	result = z | -(cutoff)
  6163  	// }
  6164  
  6165  	cutoff := cvttab.floatValue(s, ft, float64(cvttab.cutoff))
  6166  	cmp := s.newValueOrSfCall2(cvttab.ltf, types.Types[types.TBOOL], x, cutoff)
  6167  	b := s.endBlock()
  6168  	b.Kind = ssa.BlockIf
  6169  	b.SetControl(cmp)
  6170  	b.Likely = ssa.BranchLikely
  6171  
  6172  	var bThen, bZero *ssa.Block
  6173  	// use salted hash to distinguish unsigned convert at a Pos from signed convert at a Pos
  6174  	newConversion := base.ConvertHash.MatchPosWithInfo(n.Pos(), "U", nil)
  6175  	if newConversion {
  6176  		bZero = s.f.NewBlock(ssa.BlockPlain)
  6177  		bThen = s.f.NewBlock(ssa.BlockIf)
  6178  	} else {
  6179  		bThen = s.f.NewBlock(ssa.BlockPlain)
  6180  	}
  6181  
  6182  	bElse := s.f.NewBlock(ssa.BlockPlain)
  6183  	bAfter := s.f.NewBlock(ssa.BlockPlain)
  6184  
  6185  	b.AddEdgeTo(bThen)
  6186  	s.startBlock(bThen)
  6187  	a0 := s.newValueOrSfCall1(cvttab.cvt2U, tt, x)
  6188  	s.vars[n] = a0
  6189  
  6190  	if newConversion {
  6191  		cmpz := s.newValueOrSfCall2(cvttab.ltf, types.Types[types.TBOOL], x, cvttab.floatValue(s, ft, 0.0))
  6192  		s.endBlock()
  6193  		bThen.SetControl(cmpz)
  6194  		bThen.AddEdgeTo(bZero)
  6195  		bThen.Likely = ssa.BranchUnlikely
  6196  		bThen.AddEdgeTo(bAfter)
  6197  
  6198  		s.startBlock(bZero)
  6199  		s.vars[n] = cvttab.intValue(s, tt, 0)
  6200  		s.endBlock()
  6201  		bZero.AddEdgeTo(bAfter)
  6202  	} else {
  6203  		s.endBlock()
  6204  		bThen.AddEdgeTo(bAfter)
  6205  	}
  6206  
  6207  	b.AddEdgeTo(bElse)
  6208  	s.startBlock(bElse)
  6209  	y := s.newValueOrSfCall2(cvttab.subf, ft, x, cutoff)
  6210  	y = s.newValueOrSfCall1(cvttab.cvt2U, tt, y)
  6211  	z := cvttab.intValue(s, tt, int64(-cvttab.cutoff))
  6212  	a1 := s.newValue2(cvttab.or, tt, y, z)
  6213  	s.vars[n] = a1
  6214  	s.endBlock()
  6215  	bElse.AddEdgeTo(bAfter)
  6216  
  6217  	s.startBlock(bAfter)
  6218  	return s.variable(n, n.Type())
  6219  }
  6220  
  6221  // dottype generates SSA for a type assertion node.
  6222  // commaok indicates whether to panic or return a bool.
  6223  // If commaok is false, resok will be nil.
  6224  func (s *state) dottype(n *ir.TypeAssertExpr, commaok bool) (res, resok *ssa.Value) {
  6225  	iface := s.expr(n.X)              // input interface
  6226  	target := s.reflectType(n.Type()) // target type
  6227  	var targetItab *ssa.Value
  6228  	if n.ITab != nil {
  6229  		targetItab = s.expr(n.ITab)
  6230  	}
  6231  
  6232  	if n.UseNilPanic {
  6233  		if commaok {
  6234  			base.Fatalf("unexpected *ir.TypeAssertExpr with UseNilPanic == true && commaok == true")
  6235  		}
  6236  		if n.Type().IsInterface() {
  6237  			// Currently we do not expect the compiler to emit type assertions with UseNilPanic, that asserts to an interface type.
  6238  			// If needed, this can be relaxed in the future, but for now we can't assert that.
  6239  			base.Fatalf("unexpected *ir.TypeAssertExpr with UseNilPanic == true && Type().IsInterface() == true")
  6240  		}
  6241  		typs := s.f.Config.Types
  6242  		iface = s.newValue2(
  6243  			ssa.OpIMake,
  6244  			iface.Type,
  6245  			s.nilCheck(s.newValue1(ssa.OpITab, typs.BytePtr, iface)),
  6246  			s.newValue1(ssa.OpIData, typs.BytePtr, iface),
  6247  		)
  6248  	}
  6249  
  6250  	return s.dottype1(n.Pos(), n.X.Type(), n.Type(), iface, nil, target, targetItab, commaok, n.Descriptor)
  6251  }
  6252  
  6253  func (s *state) dynamicDottype(n *ir.DynamicTypeAssertExpr, commaok bool) (res, resok *ssa.Value) {
  6254  	iface := s.expr(n.X)
  6255  	var source, target, targetItab *ssa.Value
  6256  	if n.SrcRType != nil {
  6257  		source = s.expr(n.SrcRType)
  6258  	}
  6259  	if !n.X.Type().IsEmptyInterface() && !n.Type().IsInterface() {
  6260  		byteptr := s.f.Config.Types.BytePtr
  6261  		targetItab = s.expr(n.ITab)
  6262  		// TODO(mdempsky): Investigate whether compiling n.RType could be
  6263  		// better than loading itab.typ.
  6264  		target = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), targetItab))
  6265  	} else {
  6266  		target = s.expr(n.RType)
  6267  	}
  6268  	return s.dottype1(n.Pos(), n.X.Type(), n.Type(), iface, source, target, targetItab, commaok, nil)
  6269  }
  6270  
  6271  // dottype1 implements a x.(T) operation. iface is the argument (x), dst is the type we're asserting to (T)
  6272  // and src is the type we're asserting from.
  6273  // source is the *runtime._type of src
  6274  // target is the *runtime._type of dst.
  6275  // If src is a nonempty interface and dst is not an interface, targetItab is an itab representing (dst, src). Otherwise it is nil.
  6276  // commaok is true if the caller wants a boolean success value. Otherwise, the generated code panics if the conversion fails.
  6277  // descriptor is a compiler-allocated internal/abi.TypeAssert whose address is passed to runtime.typeAssert when
  6278  // the target type is a compile-time-known non-empty interface. It may be nil.
  6279  func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, target, targetItab *ssa.Value, commaok bool, descriptor *obj.LSym) (res, resok *ssa.Value) {
  6280  	typs := s.f.Config.Types
  6281  	byteptr := typs.BytePtr
  6282  	if dst.IsInterface() {
  6283  		if dst.IsEmptyInterface() {
  6284  			// Converting to an empty interface.
  6285  			// Input could be an empty or nonempty interface.
  6286  			if base.Debug.TypeAssert > 0 {
  6287  				base.WarnfAt(pos, "type assertion inlined")
  6288  			}
  6289  
  6290  			// Get itab/type field from input.
  6291  			itab := s.newValue1(ssa.OpITab, byteptr, iface)
  6292  			// Conversion succeeds iff that field is not nil.
  6293  			cond := s.newValue2(ssa.OpNeqPtr, types.Types[types.TBOOL], itab, s.constNil(byteptr))
  6294  
  6295  			if src.IsEmptyInterface() && commaok {
  6296  				// Converting empty interface to empty interface with ,ok is just a nil check.
  6297  				return iface, cond
  6298  			}
  6299  
  6300  			// Branch on nilness.
  6301  			b := s.endBlock()
  6302  			b.Kind = ssa.BlockIf
  6303  			b.SetControl(cond)
  6304  			b.Likely = ssa.BranchLikely
  6305  			bOk := s.f.NewBlock(ssa.BlockPlain)
  6306  			bFail := s.f.NewBlock(ssa.BlockPlain)
  6307  			b.AddEdgeTo(bOk)
  6308  			b.AddEdgeTo(bFail)
  6309  
  6310  			if !commaok {
  6311  				// On failure, panic by calling panicnildottype.
  6312  				s.startBlock(bFail)
  6313  				s.rtcall(ir.Syms.Panicnildottype, false, nil, target)
  6314  
  6315  				// On success, return (perhaps modified) input interface.
  6316  				s.startBlock(bOk)
  6317  				if src.IsEmptyInterface() {
  6318  					res = iface // Use input interface unchanged.
  6319  					return
  6320  				}
  6321  				// Load type out of itab, build interface with existing idata.
  6322  				off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab)
  6323  				typ := s.load(byteptr, off)
  6324  				idata := s.newValue1(ssa.OpIData, byteptr, iface)
  6325  				res = s.newValue2(ssa.OpIMake, dst, typ, idata)
  6326  				return
  6327  			}
  6328  
  6329  			s.startBlock(bOk)
  6330  			// nonempty -> empty
  6331  			// Need to load type from itab
  6332  			off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab)
  6333  			s.vars[typVar] = s.load(byteptr, off)
  6334  			s.endBlock()
  6335  
  6336  			// itab is nil, might as well use that as the nil result.
  6337  			s.startBlock(bFail)
  6338  			s.vars[typVar] = itab
  6339  			s.endBlock()
  6340  
  6341  			// Merge point.
  6342  			bEnd := s.f.NewBlock(ssa.BlockPlain)
  6343  			bOk.AddEdgeTo(bEnd)
  6344  			bFail.AddEdgeTo(bEnd)
  6345  			s.startBlock(bEnd)
  6346  			idata := s.newValue1(ssa.OpIData, byteptr, iface)
  6347  			res = s.newValue2(ssa.OpIMake, dst, s.variable(typVar, byteptr), idata)
  6348  			resok = cond
  6349  			delete(s.vars, typVar) // no practical effect, just to indicate typVar is no longer live.
  6350  			return
  6351  		}
  6352  		// converting to a nonempty interface needs a runtime call.
  6353  		if base.Debug.TypeAssert > 0 {
  6354  			base.WarnfAt(pos, "type assertion not inlined")
  6355  		}
  6356  
  6357  		itab := s.newValue1(ssa.OpITab, byteptr, iface)
  6358  		data := s.newValue1(ssa.OpIData, types.Types[types.TUNSAFEPTR], iface)
  6359  
  6360  		// First, check for nil.
  6361  		bNil := s.f.NewBlock(ssa.BlockPlain)
  6362  		bNonNil := s.f.NewBlock(ssa.BlockPlain)
  6363  		bMerge := s.f.NewBlock(ssa.BlockPlain)
  6364  		cond := s.newValue2(ssa.OpNeqPtr, types.Types[types.TBOOL], itab, s.constNil(byteptr))
  6365  		b := s.endBlock()
  6366  		b.Kind = ssa.BlockIf
  6367  		b.SetControl(cond)
  6368  		b.Likely = ssa.BranchLikely
  6369  		b.AddEdgeTo(bNonNil)
  6370  		b.AddEdgeTo(bNil)
  6371  
  6372  		s.startBlock(bNil)
  6373  		if commaok {
  6374  			s.vars[typVar] = itab // which will be nil
  6375  			b := s.endBlock()
  6376  			b.AddEdgeTo(bMerge)
  6377  		} else {
  6378  			// Panic if input is nil.
  6379  			s.rtcall(ir.Syms.Panicnildottype, false, nil, target)
  6380  		}
  6381  
  6382  		// Get typ, possibly by loading out of itab.
  6383  		s.startBlock(bNonNil)
  6384  		typ := itab
  6385  		if !src.IsEmptyInterface() {
  6386  			typ = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab))
  6387  		}
  6388  
  6389  		// Check the cache first.
  6390  		var d *ssa.Value
  6391  		if descriptor != nil {
  6392  			d = s.newValue1A(ssa.OpAddr, byteptr, descriptor, s.sb)
  6393  			if base.Flag.N == 0 && rtabi.UseInterfaceSwitchCache(Arch.LinkArch.Family) {
  6394  				// Note: we can only use the cache if we have the right atomic load instruction.
  6395  				// Double-check that here.
  6396  				if intrinsics.lookup(Arch.LinkArch.Arch, "internal/runtime/atomic", "Loadp") == nil {
  6397  					s.Fatalf("atomic load not available")
  6398  				}
  6399  				// Pick right size ops.
  6400  				var mul, and, add, zext ssa.Op
  6401  				if s.config.PtrSize == 4 {
  6402  					mul = ssa.OpMul32
  6403  					and = ssa.OpAnd32
  6404  					add = ssa.OpAdd32
  6405  					zext = ssa.OpCopy
  6406  				} else {
  6407  					mul = ssa.OpMul64
  6408  					and = ssa.OpAnd64
  6409  					add = ssa.OpAdd64
  6410  					zext = ssa.OpZeroExt32to64
  6411  				}
  6412  
  6413  				loopHead := s.f.NewBlock(ssa.BlockPlain)
  6414  				loopBody := s.f.NewBlock(ssa.BlockPlain)
  6415  				cacheHit := s.f.NewBlock(ssa.BlockPlain)
  6416  				cacheMiss := s.f.NewBlock(ssa.BlockPlain)
  6417  
  6418  				// Load cache pointer out of descriptor, with an atomic load so
  6419  				// we ensure that we see a fully written cache.
  6420  				atomicLoad := s.newValue2(ssa.OpAtomicLoadPtr, types.NewTuple(typs.BytePtr, types.TypeMem), d, s.mem())
  6421  				cache := s.newValue1(ssa.OpSelect0, typs.BytePtr, atomicLoad)
  6422  				s.vars[memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, atomicLoad)
  6423  
  6424  				// Load hash from type or itab.
  6425  				var hash *ssa.Value
  6426  				if src.IsEmptyInterface() {
  6427  					hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.Type.OffsetOf("Hash"), typ), s.mem())
  6428  				} else {
  6429  					hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.ITab.OffsetOf("Hash"), itab), s.mem())
  6430  				}
  6431  				hash = s.newValue1(zext, typs.Uintptr, hash)
  6432  				s.vars[hashVar] = hash
  6433  				// Load mask from cache.
  6434  				mask := s.newValue2(ssa.OpLoad, typs.Uintptr, cache, s.mem())
  6435  				// Jump to loop head.
  6436  				b := s.endBlock()
  6437  				b.AddEdgeTo(loopHead)
  6438  
  6439  				// At loop head, get pointer to the cache entry.
  6440  				//   e := &cache.Entries[hash&mask]
  6441  				s.startBlock(loopHead)
  6442  				idx := s.newValue2(and, typs.Uintptr, s.variable(hashVar, typs.Uintptr), mask)
  6443  				idx = s.newValue2(mul, typs.Uintptr, idx, s.uintptrConstant(uint64(2*s.config.PtrSize)))
  6444  				idx = s.newValue2(add, typs.Uintptr, idx, s.uintptrConstant(uint64(s.config.PtrSize)))
  6445  				e := s.newValue2(ssa.OpAddPtr, typs.UintptrPtr, cache, idx)
  6446  				//   hash++
  6447  				s.vars[hashVar] = s.newValue2(add, typs.Uintptr, s.variable(hashVar, typs.Uintptr), s.uintptrConstant(1))
  6448  
  6449  				// Look for a cache hit.
  6450  				//   if e.Typ == typ { goto hit }
  6451  				eTyp := s.newValue2(ssa.OpLoad, typs.Uintptr, e, s.mem())
  6452  				cmp1 := s.newValue2(ssa.OpEqPtr, typs.Bool, typ, eTyp)
  6453  				b = s.endBlock()
  6454  				b.Kind = ssa.BlockIf
  6455  				b.SetControl(cmp1)
  6456  				b.AddEdgeTo(cacheHit)
  6457  				b.AddEdgeTo(loopBody)
  6458  
  6459  				// Look for an empty entry, the tombstone for this hash table.
  6460  				//   if e.Typ == nil { goto miss }
  6461  				s.startBlock(loopBody)
  6462  				cmp2 := s.newValue2(ssa.OpEqPtr, typs.Bool, eTyp, s.constNil(typs.BytePtr))
  6463  				b = s.endBlock()
  6464  				b.Kind = ssa.BlockIf
  6465  				b.SetControl(cmp2)
  6466  				b.AddEdgeTo(cacheMiss)
  6467  				b.AddEdgeTo(loopHead)
  6468  
  6469  				// On a hit, load the data fields of the cache entry.
  6470  				//   Itab = e.Itab
  6471  				s.startBlock(cacheHit)
  6472  				eItab := s.newValue2(ssa.OpLoad, typs.BytePtr, s.newValue1I(ssa.OpOffPtr, typs.BytePtrPtr, s.config.PtrSize, e), s.mem())
  6473  				s.vars[typVar] = eItab
  6474  				b = s.endBlock()
  6475  				b.AddEdgeTo(bMerge)
  6476  
  6477  				// On a miss, call into the runtime to get the answer.
  6478  				s.startBlock(cacheMiss)
  6479  			}
  6480  		}
  6481  
  6482  		// Call into runtime to get itab for result.
  6483  		if descriptor != nil {
  6484  			itab = s.rtcall(ir.Syms.TypeAssert, true, []*types.Type{byteptr}, d, typ)[0]
  6485  		} else {
  6486  			var fn *obj.LSym
  6487  			if commaok {
  6488  				fn = ir.Syms.AssertE2I2
  6489  			} else {
  6490  				fn = ir.Syms.AssertE2I
  6491  			}
  6492  			itab = s.rtcall(fn, true, []*types.Type{byteptr}, target, typ)[0]
  6493  		}
  6494  		s.vars[typVar] = itab
  6495  		b = s.endBlock()
  6496  		b.AddEdgeTo(bMerge)
  6497  
  6498  		// Build resulting interface.
  6499  		s.startBlock(bMerge)
  6500  		itab = s.variable(typVar, byteptr)
  6501  		var ok *ssa.Value
  6502  		if commaok {
  6503  			ok = s.newValue2(ssa.OpNeqPtr, types.Types[types.TBOOL], itab, s.constNil(byteptr))
  6504  		}
  6505  		return s.newValue2(ssa.OpIMake, dst, itab, data), ok
  6506  	}
  6507  
  6508  	if base.Debug.TypeAssert > 0 {
  6509  		base.WarnfAt(pos, "type assertion inlined")
  6510  	}
  6511  
  6512  	// Converting to a concrete type.
  6513  	direct := types.IsDirectIface(dst)
  6514  	itab := s.newValue1(ssa.OpITab, byteptr, iface) // type word of interface
  6515  	if base.Debug.TypeAssert > 0 {
  6516  		base.WarnfAt(pos, "type assertion inlined")
  6517  	}
  6518  	var wantedFirstWord *ssa.Value
  6519  	if src.IsEmptyInterface() {
  6520  		// Looking for pointer to target type.
  6521  		wantedFirstWord = target
  6522  	} else {
  6523  		// Looking for pointer to itab for target type and source interface.
  6524  		wantedFirstWord = targetItab
  6525  	}
  6526  
  6527  	var tmp ir.Node     // temporary for use with large types
  6528  	var addr *ssa.Value // address of tmp
  6529  	if commaok && !ssa.CanSSA(dst) {
  6530  		// unSSAable type, use temporary.
  6531  		// TODO: get rid of some of these temporaries.
  6532  		tmp, addr = s.temp(pos, dst)
  6533  	}
  6534  
  6535  	cond := s.newValue2(ssa.OpEqPtr, types.Types[types.TBOOL], itab, wantedFirstWord)
  6536  	b := s.endBlock()
  6537  	b.Kind = ssa.BlockIf
  6538  	b.SetControl(cond)
  6539  	b.Likely = ssa.BranchLikely
  6540  
  6541  	bOk := s.f.NewBlock(ssa.BlockPlain)
  6542  	bFail := s.f.NewBlock(ssa.BlockPlain)
  6543  	b.AddEdgeTo(bOk)
  6544  	b.AddEdgeTo(bFail)
  6545  
  6546  	if !commaok {
  6547  		// on failure, panic by calling panicdottype
  6548  		s.startBlock(bFail)
  6549  		taddr := source
  6550  		if taddr == nil {
  6551  			taddr = s.reflectType(src)
  6552  		}
  6553  		if src.IsEmptyInterface() {
  6554  			s.rtcall(ir.Syms.PanicdottypeE, false, nil, itab, target, taddr)
  6555  		} else {
  6556  			s.rtcall(ir.Syms.PanicdottypeI, false, nil, itab, target, taddr)
  6557  		}
  6558  
  6559  		// on success, return data from interface
  6560  		s.startBlock(bOk)
  6561  		if direct {
  6562  			return s.newValue1(ssa.OpIData, dst, iface), nil
  6563  		}
  6564  		p := s.newValue1(ssa.OpIData, types.NewPtr(dst), iface)
  6565  		return s.load(dst, p), nil
  6566  	}
  6567  
  6568  	// commaok is the more complicated case because we have
  6569  	// a control flow merge point.
  6570  	bEnd := s.f.NewBlock(ssa.BlockPlain)
  6571  	// Note that we need a new valVar each time (unlike okVar where we can
  6572  	// reuse the variable) because it might have a different type every time.
  6573  	valVar := ssaMarker("val")
  6574  
  6575  	// type assertion succeeded
  6576  	s.startBlock(bOk)
  6577  	if tmp == nil {
  6578  		if direct {
  6579  			s.vars[valVar] = s.newValue1(ssa.OpIData, dst, iface)
  6580  		} else {
  6581  			p := s.newValue1(ssa.OpIData, types.NewPtr(dst), iface)
  6582  			s.vars[valVar] = s.load(dst, p)
  6583  		}
  6584  	} else {
  6585  		p := s.newValue1(ssa.OpIData, types.NewPtr(dst), iface)
  6586  		s.move(dst, addr, p)
  6587  	}
  6588  	s.vars[okVar] = s.constBool(true)
  6589  	s.endBlock()
  6590  	bOk.AddEdgeTo(bEnd)
  6591  
  6592  	// type assertion failed
  6593  	s.startBlock(bFail)
  6594  	if tmp == nil {
  6595  		s.vars[valVar] = s.zeroVal(dst)
  6596  	} else {
  6597  		s.zero(dst, addr)
  6598  	}
  6599  	s.vars[okVar] = s.constBool(false)
  6600  	s.endBlock()
  6601  	bFail.AddEdgeTo(bEnd)
  6602  
  6603  	// merge point
  6604  	s.startBlock(bEnd)
  6605  	if tmp == nil {
  6606  		res = s.variable(valVar, dst)
  6607  		delete(s.vars, valVar) // no practical effect, just to indicate typVar is no longer live.
  6608  	} else {
  6609  		res = s.load(dst, addr)
  6610  	}
  6611  	resok = s.variable(okVar, types.Types[types.TBOOL])
  6612  	delete(s.vars, okVar) // ditto
  6613  	return res, resok
  6614  }
  6615  
  6616  // temp allocates a temp of type t at position pos
  6617  func (s *state) temp(pos src.XPos, t *types.Type) (*ir.Name, *ssa.Value) {
  6618  	tmp := typecheck.TempAt(pos, s.curfn, t)
  6619  	if t.HasPointers() || (ssa.IsMergeCandidate(tmp) && t != deferstruct()) {
  6620  		s.vars[memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, tmp, s.mem())
  6621  	}
  6622  	addr := s.addr(tmp)
  6623  	return tmp, addr
  6624  }
  6625  
  6626  // variable returns the value of a variable at the current location.
  6627  func (s *state) variable(n ir.Node, t *types.Type) *ssa.Value {
  6628  	v := s.vars[n]
  6629  	if v != nil {
  6630  		return v
  6631  	}
  6632  	v = s.fwdVars[n]
  6633  	if v != nil {
  6634  		return v
  6635  	}
  6636  
  6637  	if s.curBlock == s.f.Entry {
  6638  		// No variable should be live at entry.
  6639  		s.f.Fatalf("value %v (%v) incorrectly live at entry", n, v)
  6640  	}
  6641  	// Make a FwdRef, which records a value that's live on block input.
  6642  	// We'll find the matching definition as part of insertPhis.
  6643  	v = s.newValue0A(ssa.OpFwdRef, t, fwdRefAux{N: n})
  6644  	s.fwdVars[n] = v
  6645  	if n.Op() == ir.ONAME {
  6646  		s.addNamedValue(n.(*ir.Name), v)
  6647  	}
  6648  	return v
  6649  }
  6650  
  6651  func (s *state) mem() *ssa.Value {
  6652  	return s.variable(memVar, types.TypeMem)
  6653  }
  6654  
  6655  func (s *state) addNamedValue(n *ir.Name, v *ssa.Value) {
  6656  	if n.Class == ir.Pxxx {
  6657  		// Don't track our marker nodes (memVar etc.).
  6658  		return
  6659  	}
  6660  	if ir.IsAutoTmp(n) {
  6661  		// Don't track temporary variables.
  6662  		return
  6663  	}
  6664  	if n.Class == ir.PPARAMOUT {
  6665  		// Don't track named output values.  This prevents return values
  6666  		// from being assigned too early. See #14591 and #14762. TODO: allow this.
  6667  		return
  6668  	}
  6669  	loc := ssa.LocalSlot{N: n, Type: n.Type(), Off: 0}
  6670  	values, ok := s.f.NamedValues[loc]
  6671  	if !ok {
  6672  		s.f.Names = append(s.f.Names, &loc)
  6673  		s.f.CanonicalLocalSlots[loc] = &loc
  6674  	}
  6675  	s.f.NamedValues[loc] = append(values, v)
  6676  }
  6677  
  6678  // Branch is an unresolved branch.
  6679  type Branch struct {
  6680  	P *obj.Prog  // branch instruction
  6681  	B *ssa.Block // target
  6682  }
  6683  
  6684  // State contains state needed during Prog generation.
  6685  type State struct {
  6686  	ABI obj.ABI
  6687  
  6688  	pp *objw.Progs
  6689  
  6690  	// Branches remembers all the branch instructions we've seen
  6691  	// and where they would like to go.
  6692  	Branches []Branch
  6693  
  6694  	// JumpTables remembers all the jump tables we've seen.
  6695  	JumpTables []*ssa.Block
  6696  
  6697  	// bstart remembers where each block starts (indexed by block ID)
  6698  	bstart []*obj.Prog
  6699  
  6700  	maxarg int64 // largest frame size for arguments to calls made by the function
  6701  
  6702  	// Map from GC safe points to liveness index, generated by
  6703  	// liveness analysis.
  6704  	livenessMap liveness.Map
  6705  
  6706  	// partLiveArgs includes arguments that may be partially live, for which we
  6707  	// need to generate instructions that spill the argument registers.
  6708  	partLiveArgs map[*ir.Name]bool
  6709  
  6710  	// lineRunStart records the beginning of the current run of instructions
  6711  	// within a single block sharing the same line number
  6712  	// Used to move statement marks to the beginning of such runs.
  6713  	lineRunStart *obj.Prog
  6714  
  6715  	// wasm: The number of values on the WebAssembly stack. This is only used as a safeguard.
  6716  	OnWasmStackSkipped int
  6717  }
  6718  
  6719  func (s *State) FuncInfo() *obj.FuncInfo {
  6720  	return s.pp.CurFunc.LSym.Func()
  6721  }
  6722  
  6723  // Prog appends a new Prog.
  6724  func (s *State) Prog(as obj.As) *obj.Prog {
  6725  	p := s.pp.Prog(as)
  6726  	if objw.LosesStmtMark(as) {
  6727  		return p
  6728  	}
  6729  	// Float a statement start to the beginning of any same-line run.
  6730  	// lineRunStart is reset at block boundaries, which appears to work well.
  6731  	if s.lineRunStart == nil || s.lineRunStart.Pos.Line() != p.Pos.Line() {
  6732  		s.lineRunStart = p
  6733  	} else if p.Pos.IsStmt() == src.PosIsStmt {
  6734  		s.lineRunStart.Pos = s.lineRunStart.Pos.WithIsStmt()
  6735  		p.Pos = p.Pos.WithNotStmt()
  6736  	}
  6737  	return p
  6738  }
  6739  
  6740  // Pc returns the current Prog.
  6741  func (s *State) Pc() *obj.Prog {
  6742  	return s.pp.Next
  6743  }
  6744  
  6745  // SetPos sets the current source position.
  6746  func (s *State) SetPos(pos src.XPos) {
  6747  	s.pp.Pos = pos
  6748  }
  6749  
  6750  // Br emits a single branch instruction and returns the instruction.
  6751  // Not all architectures need the returned instruction, but otherwise
  6752  // the boilerplate is common to all.
  6753  func (s *State) Br(op obj.As, target *ssa.Block) *obj.Prog {
  6754  	p := s.Prog(op)
  6755  	p.To.Type = obj.TYPE_BRANCH
  6756  	s.Branches = append(s.Branches, Branch{P: p, B: target})
  6757  	return p
  6758  }
  6759  
  6760  // DebugFriendlySetPosFrom adjusts Pos.IsStmt subject to heuristics
  6761  // that reduce "jumpy" line number churn when debugging.
  6762  // Spill/fill/copy instructions from the register allocator,
  6763  // phi functions, and instructions with a no-pos position
  6764  // are examples of instructions that can cause churn.
  6765  func (s *State) DebugFriendlySetPosFrom(v *ssa.Value) {
  6766  	switch v.Op {
  6767  	case ssa.OpPhi, ssa.OpCopy, ssa.OpLoadReg, ssa.OpStoreReg:
  6768  		// These are not statements
  6769  		s.SetPos(v.Pos.WithNotStmt())
  6770  	default:
  6771  		p := v.Pos
  6772  		if p != src.NoXPos {
  6773  			// If the position is defined, update the position.
  6774  			// Also convert default IsStmt to NotStmt; only
  6775  			// explicit statement boundaries should appear
  6776  			// in the generated code.
  6777  			if p.IsStmt() != src.PosIsStmt {
  6778  				if s.pp.Pos.IsStmt() == src.PosIsStmt && s.pp.Pos.SameFileAndLine(p) {
  6779  					// If s.pp.Pos already has a statement mark, then it was set here (below) for
  6780  					// the previous value.  If an actual instruction had been emitted for that
  6781  					// value, then the statement mark would have been reset.  Since the statement
  6782  					// mark of s.pp.Pos was not reset, this position (file/line) still needs a
  6783  					// statement mark on an instruction.  If file and line for this value are
  6784  					// the same as the previous value, then the first instruction for this
  6785  					// value will work to take the statement mark.  Return early to avoid
  6786  					// resetting the statement mark.
  6787  					//
  6788  					// The reset of s.pp.Pos occurs in (*Progs).Prog() -- if it emits
  6789  					// an instruction, and the instruction's statement mark was set,
  6790  					// and it is not one of the LosesStmtMark instructions,
  6791  					// then Prog() resets the statement mark on the (*Progs).Pos.
  6792  					return
  6793  				}
  6794  				p = p.WithNotStmt()
  6795  				// Calls use the pos attached to v, but copy the statement mark from State
  6796  			}
  6797  			s.SetPos(p)
  6798  		} else {
  6799  			s.SetPos(s.pp.Pos.WithNotStmt())
  6800  		}
  6801  	}
  6802  }
  6803  
  6804  // emit argument info (locations on stack) for traceback.
  6805  func emitArgInfo(e *ssafn, f *ssa.Func, pp *objw.Progs) {
  6806  	ft := e.curfn.Type()
  6807  	if ft.NumRecvs() == 0 && ft.NumParams() == 0 {
  6808  		return
  6809  	}
  6810  
  6811  	x := EmitArgInfo(e.curfn, f.OwnAux.ABIInfo())
  6812  	x.Set(obj.AttrContentAddressable, true)
  6813  	e.curfn.LSym.Func().ArgInfo = x
  6814  
  6815  	// Emit a funcdata pointing at the arg info data.
  6816  	p := pp.Prog(obj.AFUNCDATA)
  6817  	p.From.SetConst(rtabi.FUNCDATA_ArgInfo)
  6818  	p.To.Type = obj.TYPE_MEM
  6819  	p.To.Name = obj.NAME_EXTERN
  6820  	p.To.Sym = x
  6821  }
  6822  
  6823  // emit argument info (locations on stack) of f for traceback.
  6824  func EmitArgInfo(f *ir.Func, abiInfo *abi.ABIParamResultInfo) *obj.LSym {
  6825  	x := base.Ctxt.Lookup(fmt.Sprintf("%s.arginfo%d", f.LSym.Name, f.ABI))
  6826  	x.Align = 1
  6827  	// NOTE: do not set ContentAddressable here. This may be referenced from
  6828  	// assembly code by name (in this case f is a declaration).
  6829  	// Instead, set it in emitArgInfo above.
  6830  
  6831  	PtrSize := int64(types.PtrSize)
  6832  	uintptrTyp := types.Types[types.TUINTPTR]
  6833  
  6834  	isAggregate := func(t *types.Type) bool {
  6835  		return isStructNotSIMD(t) || t.IsArray() || t.IsComplex() || t.IsInterface() || t.IsString() || t.IsSlice()
  6836  	}
  6837  
  6838  	wOff := 0
  6839  	n := 0
  6840  	writebyte := func(o uint8) { wOff = objw.Uint8(x, wOff, o) }
  6841  
  6842  	// Write one non-aggregate arg/field/element.
  6843  	write1 := func(sz, offset int64) {
  6844  		if offset >= rtabi.TraceArgsSpecial {
  6845  			writebyte(rtabi.TraceArgsOffsetTooLarge)
  6846  		} else {
  6847  			writebyte(uint8(offset))
  6848  			writebyte(uint8(sz))
  6849  		}
  6850  		n++
  6851  	}
  6852  
  6853  	// Visit t recursively and write it out.
  6854  	// Returns whether to continue visiting.
  6855  	var visitType func(baseOffset int64, t *types.Type, depth int) bool
  6856  	visitType = func(baseOffset int64, t *types.Type, depth int) bool {
  6857  		if n >= rtabi.TraceArgsLimit {
  6858  			writebyte(rtabi.TraceArgsDotdotdot)
  6859  			return false
  6860  		}
  6861  		if !isAggregate(t) {
  6862  			write1(t.Size(), baseOffset)
  6863  			return true
  6864  		}
  6865  		writebyte(rtabi.TraceArgsStartAgg)
  6866  		depth++
  6867  		if depth >= rtabi.TraceArgsMaxDepth {
  6868  			writebyte(rtabi.TraceArgsDotdotdot)
  6869  			writebyte(rtabi.TraceArgsEndAgg)
  6870  			n++
  6871  			return true
  6872  		}
  6873  		switch {
  6874  		case t.IsInterface(), t.IsString():
  6875  			_ = visitType(baseOffset, uintptrTyp, depth) &&
  6876  				visitType(baseOffset+PtrSize, uintptrTyp, depth)
  6877  		case t.IsSlice():
  6878  			_ = visitType(baseOffset, uintptrTyp, depth) &&
  6879  				visitType(baseOffset+PtrSize, uintptrTyp, depth) &&
  6880  				visitType(baseOffset+PtrSize*2, uintptrTyp, depth)
  6881  		case t.IsComplex():
  6882  			_ = visitType(baseOffset, types.FloatForComplex(t), depth) &&
  6883  				visitType(baseOffset+t.Size()/2, types.FloatForComplex(t), depth)
  6884  		case t.IsArray():
  6885  			if t.NumElem() == 0 {
  6886  				n++ // {} counts as a component
  6887  				break
  6888  			}
  6889  			for i := int64(0); i < t.NumElem(); i++ {
  6890  				if !visitType(baseOffset, t.Elem(), depth) {
  6891  					break
  6892  				}
  6893  				baseOffset += t.Elem().Size()
  6894  			}
  6895  		case isStructNotSIMD(t):
  6896  			if t.NumFields() == 0 {
  6897  				n++ // {} counts as a component
  6898  				break
  6899  			}
  6900  			for _, field := range t.Fields() {
  6901  				if !visitType(baseOffset+field.Offset, field.Type, depth) {
  6902  					break
  6903  				}
  6904  			}
  6905  		}
  6906  		writebyte(rtabi.TraceArgsEndAgg)
  6907  		return true
  6908  	}
  6909  
  6910  	start := 0
  6911  	if strings.Contains(f.LSym.Name, "[") {
  6912  		// Skip the dictionary argument - it is implicit and the user doesn't need to see it.
  6913  		start = 1
  6914  	}
  6915  
  6916  	for _, a := range abiInfo.InParams()[start:] {
  6917  		if !visitType(a.FrameOffset(abiInfo), a.Type, 0) {
  6918  			break
  6919  		}
  6920  	}
  6921  	writebyte(rtabi.TraceArgsEndSeq)
  6922  	if wOff > rtabi.TraceArgsMaxLen {
  6923  		base.Fatalf("ArgInfo too large")
  6924  	}
  6925  
  6926  	return x
  6927  }
  6928  
  6929  // for wrapper, emit info of wrapped function.
  6930  func emitWrappedFuncInfo(e *ssafn, pp *objw.Progs) {
  6931  	if base.Ctxt.Flag_linkshared {
  6932  		// Relative reference (SymPtrOff) to another shared object doesn't work.
  6933  		// Unfortunate.
  6934  		return
  6935  	}
  6936  
  6937  	wfn := e.curfn.WrappedFunc
  6938  	if wfn == nil {
  6939  		return
  6940  	}
  6941  
  6942  	wsym := wfn.Linksym()
  6943  	x := base.Ctxt.LookupInit(fmt.Sprintf("%s.wrapinfo", wsym.Name), func(x *obj.LSym) {
  6944  		objw.SymPtrOff(x, 0, wsym)
  6945  		x.Set(obj.AttrContentAddressable, true)
  6946  		x.Align = 4
  6947  	})
  6948  	e.curfn.LSym.Func().WrapInfo = x
  6949  
  6950  	// Emit a funcdata pointing at the wrap info data.
  6951  	p := pp.Prog(obj.AFUNCDATA)
  6952  	p.From.SetConst(rtabi.FUNCDATA_WrapInfo)
  6953  	p.To.Type = obj.TYPE_MEM
  6954  	p.To.Name = obj.NAME_EXTERN
  6955  	p.To.Sym = x
  6956  }
  6957  
  6958  // genssa appends entries to pp for each instruction in f.
  6959  func genssa(f *ssa.Func, pp *objw.Progs) {
  6960  	var s State
  6961  	s.ABI = f.OwnAux.Fn.ABI()
  6962  
  6963  	e := f.Frontend().(*ssafn)
  6964  
  6965  	gatherPrintInfo := f.PrintOrHtmlSSA || ssa.GenssaDump[f.Name]
  6966  
  6967  	var lv *liveness.Liveness
  6968  	s.livenessMap, s.partLiveArgs, lv = liveness.Compute(e.curfn, f, e.stkptrsize, pp, gatherPrintInfo)
  6969  	emitArgInfo(e, f, pp)
  6970  	argLiveBlockMap, argLiveValueMap := liveness.ArgLiveness(e.curfn, f, pp)
  6971  
  6972  	openDeferInfo := e.curfn.LSym.Func().OpenCodedDeferInfo
  6973  	if openDeferInfo != nil {
  6974  		// This function uses open-coded defers -- write out the funcdata
  6975  		// info that we computed at the end of genssa.
  6976  		p := pp.Prog(obj.AFUNCDATA)
  6977  		p.From.SetConst(rtabi.FUNCDATA_OpenCodedDeferInfo)
  6978  		p.To.Type = obj.TYPE_MEM
  6979  		p.To.Name = obj.NAME_EXTERN
  6980  		p.To.Sym = openDeferInfo
  6981  	}
  6982  
  6983  	emitWrappedFuncInfo(e, pp)
  6984  
  6985  	// Remember where each block starts.
  6986  	s.bstart = make([]*obj.Prog, f.NumBlocks())
  6987  	s.pp = pp
  6988  	var progToValue map[*obj.Prog]*ssa.Value
  6989  	var progToBlock map[*obj.Prog]*ssa.Block
  6990  	var valueToProgAfter []*obj.Prog // The first Prog following computation of a value v; v is visible at this point.
  6991  	if gatherPrintInfo {
  6992  		progToValue = make(map[*obj.Prog]*ssa.Value, f.NumValues())
  6993  		progToBlock = make(map[*obj.Prog]*ssa.Block, f.NumBlocks())
  6994  		f.Logf("genssa %s\n", f.Name)
  6995  		progToBlock[s.pp.Next] = f.Blocks[0]
  6996  	}
  6997  
  6998  	if base.Ctxt.Flag_locationlists {
  6999  		if cap(f.Cache.ValueToProgAfter) < f.NumValues() {
  7000  			f.Cache.ValueToProgAfter = make([]*obj.Prog, f.NumValues())
  7001  		}
  7002  		valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()]
  7003  		clear(valueToProgAfter)
  7004  	}
  7005  
  7006  	// If the very first instruction is not tagged as a statement,
  7007  	// debuggers may attribute it to previous function in program.
  7008  	firstPos := src.NoXPos
  7009  	for _, v := range f.Entry.Values {
  7010  		if v.Pos.IsStmt() == src.PosIsStmt && v.Op != ssa.OpArg && v.Op != ssa.OpArgIntReg && v.Op != ssa.OpArgFloatReg && v.Op != ssa.OpLoadReg && v.Op != ssa.OpStoreReg {
  7011  			firstPos = v.Pos
  7012  			v.Pos = firstPos.WithDefaultStmt()
  7013  			break
  7014  		}
  7015  	}
  7016  
  7017  	// inlMarks has an entry for each Prog that implements an inline mark.
  7018  	// It maps from that Prog to the global inlining id of the inlined body
  7019  	// which should unwind to this Prog's location.
  7020  	var inlMarks map[*obj.Prog]int32
  7021  	var inlMarkList []*obj.Prog
  7022  
  7023  	// inlMarksByPos maps from a (column 1) source position to the set of
  7024  	// Progs that are in the set above and have that source position.
  7025  	var inlMarksByPos map[src.XPos][]*obj.Prog
  7026  
  7027  	var argLiveIdx int = -1 // argument liveness info index
  7028  
  7029  	// These control cache line alignment; if the required portion of
  7030  	// a cache line is not available, then pad to obtain cache line
  7031  	// alignment.  Not implemented on all architectures, may not be
  7032  	// useful on all architectures.
  7033  	var hotAlign, hotRequire int64
  7034  
  7035  	if base.Debug.AlignHot > 0 {
  7036  		switch base.Ctxt.Arch.Name {
  7037  		// enable this on a case-by-case basis, with benchmarking.
  7038  		// currently shown:
  7039  		//   good for amd64
  7040  		//   not helpful for Apple Silicon
  7041  		//
  7042  		case "amd64", "386":
  7043  			// Align to 64 if 31 or fewer bytes remain in a cache line
  7044  			// benchmarks a little better than always aligning, and also
  7045  			// adds slightly less to the (PGO-compiled) binary size.
  7046  			hotAlign = 64
  7047  			hotRequire = 31
  7048  		}
  7049  	}
  7050  
  7051  	// Emit basic blocks
  7052  	for i, b := range f.Blocks {
  7053  
  7054  		s.lineRunStart = nil
  7055  		s.SetPos(s.pp.Pos.WithNotStmt()) // It needs a non-empty Pos, but cannot be a statement boundary (yet).
  7056  
  7057  		if hotAlign > 0 && b.Hotness&ssa.HotPgoInitial == ssa.HotPgoInitial {
  7058  			// So far this has only been shown profitable for PGO-hot loop headers.
  7059  			// The Hotness values allows distinctions between initial blocks that are "hot" or not, and "flow-in" or not.
  7060  			// Currently only the initial blocks of loops are tagged in this way;
  7061  			// there are no blocks tagged "pgo-hot" that are not also tagged "initial".
  7062  			// TODO more heuristics, more architectures.
  7063  			p := s.pp.Prog(obj.APCALIGNMAX)
  7064  			p.From.SetConst(hotAlign)
  7065  			p.To.SetConst(hotRequire)
  7066  		}
  7067  
  7068  		s.bstart[b.ID] = s.pp.Next
  7069  
  7070  		if idx, ok := argLiveBlockMap[b.ID]; ok && idx != argLiveIdx {
  7071  			argLiveIdx = idx
  7072  			p := s.pp.Prog(obj.APCDATA)
  7073  			p.From.SetConst(rtabi.PCDATA_ArgLiveIndex)
  7074  			p.To.SetConst(int64(idx))
  7075  		}
  7076  
  7077  		// Emit values in block
  7078  		Arch.SSAMarkMoves(&s, b)
  7079  		for _, v := range b.Values {
  7080  			x := s.pp.Next
  7081  			s.DebugFriendlySetPosFrom(v)
  7082  
  7083  			if v.Op.ResultInArg0() && v.ResultReg() != v.Args[0].Reg() {
  7084  				v.Fatalf("input[0] and output not in same register %s", v.LongString())
  7085  			}
  7086  
  7087  			switch v.Op {
  7088  			case ssa.OpInitMem:
  7089  				// memory arg needs no code
  7090  			case ssa.OpArg:
  7091  				// input args need no code
  7092  			case ssa.OpSP, ssa.OpSB:
  7093  				// nothing to do
  7094  			case ssa.OpSelect0, ssa.OpSelect1, ssa.OpSelectN, ssa.OpMakeResult:
  7095  				// nothing to do
  7096  			case ssa.OpGetG:
  7097  				// nothing to do when there's a g register,
  7098  				// and checkLower complains if there's not
  7099  			case ssa.OpVarDef, ssa.OpVarLive, ssa.OpKeepAlive, ssa.OpWBend:
  7100  				// nothing to do; already used by liveness
  7101  			case ssa.OpPhi:
  7102  				CheckLoweredPhi(v)
  7103  			case ssa.OpConvert:
  7104  				// nothing to do; no-op conversion for liveness
  7105  				if v.Args[0].Reg() != v.Reg() {
  7106  					v.Fatalf("OpConvert should be a no-op: %s; %s", v.Args[0].LongString(), v.LongString())
  7107  				}
  7108  			case ssa.OpInlMark:
  7109  				p := Arch.Ginsnop(s.pp)
  7110  				if inlMarks == nil {
  7111  					inlMarks = map[*obj.Prog]int32{}
  7112  					inlMarksByPos = map[src.XPos][]*obj.Prog{}
  7113  				}
  7114  				inlMarks[p] = v.AuxInt32()
  7115  				inlMarkList = append(inlMarkList, p)
  7116  				pos := v.Pos.AtColumn1()
  7117  				inlMarksByPos[pos] = append(inlMarksByPos[pos], p)
  7118  				firstPos = src.NoXPos
  7119  
  7120  			default:
  7121  				// Special case for first line in function; move it to the start (which cannot be a register-valued instruction)
  7122  				if firstPos != src.NoXPos && v.Op != ssa.OpArgIntReg && v.Op != ssa.OpArgFloatReg && v.Op != ssa.OpLoadReg && v.Op != ssa.OpStoreReg {
  7123  					s.SetPos(firstPos)
  7124  					firstPos = src.NoXPos
  7125  				}
  7126  				// Attach this safe point to the next
  7127  				// instruction.
  7128  				s.pp.NextLive = s.livenessMap.Get(v)
  7129  				s.pp.NextUnsafe = s.livenessMap.GetUnsafe(v)
  7130  
  7131  				// let the backend handle it
  7132  				Arch.SSAGenValue(&s, v)
  7133  			}
  7134  
  7135  			if idx, ok := argLiveValueMap[v.ID]; ok && idx != argLiveIdx {
  7136  				argLiveIdx = idx
  7137  				p := s.pp.Prog(obj.APCDATA)
  7138  				p.From.SetConst(rtabi.PCDATA_ArgLiveIndex)
  7139  				p.To.SetConst(int64(idx))
  7140  			}
  7141  
  7142  			if base.Ctxt.Flag_locationlists {
  7143  				valueToProgAfter[v.ID] = s.pp.Next
  7144  			}
  7145  
  7146  			if gatherPrintInfo {
  7147  				for ; x != s.pp.Next; x = x.Link {
  7148  					progToValue[x] = v
  7149  				}
  7150  			}
  7151  		}
  7152  		// If this is an empty infinite loop, stick a hardware NOP in there so that debuggers are less confused.
  7153  		if s.bstart[b.ID] == s.pp.Next && len(b.Succs) == 1 && b.Succs[0].Block() == b {
  7154  			p := Arch.Ginsnop(s.pp)
  7155  			p.Pos = p.Pos.WithIsStmt()
  7156  			if b.Pos == src.NoXPos {
  7157  				b.Pos = p.Pos // It needs a file, otherwise a no-file non-zero line causes confusion.  See #35652.
  7158  				if b.Pos == src.NoXPos {
  7159  					b.Pos = s.pp.Text.Pos // Sometimes p.Pos is empty.  See #35695.
  7160  				}
  7161  			}
  7162  			b.Pos = b.Pos.WithBogusLine() // Debuggers are not good about infinite loops, force a change in line number
  7163  		}
  7164  
  7165  		// Set unsafe mark for any end-of-block generated instructions
  7166  		// (normally, conditional or unconditional branches).
  7167  		// This is particularly important for empty blocks, as there
  7168  		// are no values to inherit the unsafe mark from.
  7169  		s.pp.NextUnsafe = s.livenessMap.GetUnsafeBlock(b)
  7170  
  7171  		// Emit control flow instructions for block
  7172  		var next *ssa.Block
  7173  		if i < len(f.Blocks)-1 && base.Flag.N == 0 {
  7174  			// If -N, leave next==nil so every block with successors
  7175  			// ends in a JMP (except call blocks - plive doesn't like
  7176  			// select{send,recv} followed by a JMP call).  Helps keep
  7177  			// line numbers for otherwise empty blocks.
  7178  			next = f.Blocks[i+1]
  7179  		}
  7180  		x := s.pp.Next
  7181  		s.SetPos(b.Pos)
  7182  		Arch.SSAGenBlock(&s, b, next)
  7183  		if gatherPrintInfo {
  7184  			for ; x != s.pp.Next; x = x.Link {
  7185  				progToBlock[x] = b
  7186  			}
  7187  		}
  7188  	}
  7189  	if f.Blocks[len(f.Blocks)-1].Kind == ssa.BlockExit {
  7190  		// We need the return address of a panic call to
  7191  		// still be inside the function in question. So if
  7192  		// it ends in a call which doesn't return, add a
  7193  		// nop (which will never execute) after the call.
  7194  		Arch.Ginsnop(s.pp)
  7195  	}
  7196  	if openDeferInfo != nil {
  7197  		// When doing open-coded defers, generate a disconnected call to
  7198  		// deferreturn and a return. This will be used to during panic
  7199  		// recovery to unwind the stack and return back to the runtime.
  7200  
  7201  		// Note that this exit code doesn't work if a return parameter
  7202  		// is heap-allocated, but open defers aren't enabled in that case.
  7203  
  7204  		// TODO either make this handle heap-allocated return parameters or reuse the other-defers general-purpose code path.
  7205  		s.pp.NextLive = s.livenessMap.DeferReturn
  7206  		p := s.pp.Prog(obj.ACALL)
  7207  		p.To.Type = obj.TYPE_MEM
  7208  		p.To.Name = obj.NAME_EXTERN
  7209  		p.To.Sym = ir.Syms.Deferreturn
  7210  
  7211  		// Load results into registers. So when a deferred function
  7212  		// recovers a panic, it will return to caller with right results.
  7213  		// The results are already in memory, because they are not SSA'd
  7214  		// when the function has defers (see canSSAName).
  7215  		for _, o := range f.OwnAux.ABIInfo().OutParams() {
  7216  			n := o.Name
  7217  			rts, offs := o.RegisterTypesAndOffsets()
  7218  			for i := range o.Registers {
  7219  				Arch.LoadRegResult(&s, f, rts[i], ssa.ObjRegForAbiReg(o.Registers[i], f.Config), n, offs[i])
  7220  			}
  7221  		}
  7222  
  7223  		s.pp.Prog(obj.ARET)
  7224  	}
  7225  
  7226  	if inlMarks != nil {
  7227  		hasCall := false
  7228  
  7229  		// We have some inline marks. Try to find other instructions we're
  7230  		// going to emit anyway, and use those instructions instead of the
  7231  		// inline marks.
  7232  		for p := s.pp.Text; p != nil; p = p.Link {
  7233  			if p.As == obj.ANOP || p.As == obj.AFUNCDATA || p.As == obj.APCDATA || p.As == obj.ATEXT ||
  7234  				p.As == obj.APCALIGN || p.As == obj.APCALIGNMAX || Arch.LinkArch.Family == sys.Wasm {
  7235  				// Don't use 0-sized instructions as inline marks, because we need
  7236  				// to identify inline mark instructions by pc offset.
  7237  				// (Some of these instructions are sometimes zero-sized, sometimes not.
  7238  				// We must not use anything that even might be zero-sized.)
  7239  				// TODO: are there others?
  7240  				continue
  7241  			}
  7242  			if _, ok := inlMarks[p]; ok {
  7243  				// Don't use inline marks themselves. We don't know
  7244  				// whether they will be zero-sized or not yet.
  7245  				continue
  7246  			}
  7247  			if p.As == obj.ACALL || p.As == obj.ADUFFCOPY || p.As == obj.ADUFFZERO {
  7248  				hasCall = true
  7249  			}
  7250  			pos := p.Pos.AtColumn1()
  7251  			marks := inlMarksByPos[pos]
  7252  			if len(marks) == 0 {
  7253  				continue
  7254  			}
  7255  			for _, m := range marks {
  7256  				// We found an instruction with the same source position as
  7257  				// some of the inline marks.
  7258  				// Use this instruction instead.
  7259  				p.Pos = p.Pos.WithIsStmt() // promote position to a statement
  7260  				s.pp.CurFunc.LSym.Func().AddInlMark(p, inlMarks[m])
  7261  				// Make the inline mark a real nop, so it doesn't generate any code.
  7262  				m.As = obj.ANOP
  7263  				m.Pos = src.NoXPos
  7264  				m.From = obj.Addr{}
  7265  				m.To = obj.Addr{}
  7266  			}
  7267  			delete(inlMarksByPos, pos)
  7268  		}
  7269  		// Any unmatched inline marks now need to be added to the inlining tree (and will generate a nop instruction).
  7270  		for _, p := range inlMarkList {
  7271  			if p.As != obj.ANOP {
  7272  				s.pp.CurFunc.LSym.Func().AddInlMark(p, inlMarks[p])
  7273  			}
  7274  		}
  7275  
  7276  		if e.stksize == 0 && !hasCall {
  7277  			// Frameless leaf function. It doesn't need any preamble,
  7278  			// so make sure its first instruction isn't from an inlined callee.
  7279  			// If it is, add a nop at the start of the function with a position
  7280  			// equal to the start of the function.
  7281  			// This ensures that runtime.FuncForPC(uintptr(reflect.ValueOf(fn).Pointer())).Name()
  7282  			// returns the right answer. See issue 58300.
  7283  			for p := s.pp.Text; p != nil; p = p.Link {
  7284  				if p.As == obj.AFUNCDATA || p.As == obj.APCDATA || p.As == obj.ATEXT || p.As == obj.ANOP {
  7285  					continue
  7286  				}
  7287  				if base.Ctxt.PosTable.Pos(p.Pos).Base().InliningIndex() >= 0 {
  7288  					// Make a real (not 0-sized) nop.
  7289  					nop := Arch.Ginsnop(s.pp)
  7290  					nop.Pos = e.curfn.Pos().WithIsStmt()
  7291  
  7292  					// Unfortunately, Ginsnop puts the instruction at the
  7293  					// end of the list. Move it up to just before p.
  7294  
  7295  					// Unlink from the current list.
  7296  					for x := s.pp.Text; x != nil; x = x.Link {
  7297  						if x.Link == nop {
  7298  							x.Link = nop.Link
  7299  							break
  7300  						}
  7301  					}
  7302  					// Splice in right before p.
  7303  					for x := s.pp.Text; x != nil; x = x.Link {
  7304  						if x.Link == p {
  7305  							nop.Link = p
  7306  							x.Link = nop
  7307  							break
  7308  						}
  7309  					}
  7310  				}
  7311  				break
  7312  			}
  7313  		}
  7314  	}
  7315  
  7316  	if base.Ctxt.Flag_locationlists {
  7317  		var debugInfo *ssa.FuncDebug
  7318  		debugInfo = e.curfn.DebugInfo.(*ssa.FuncDebug)
  7319  		// Save off entry ID in case we need it later for DWARF generation
  7320  		// for return values promoted to the heap.
  7321  		debugInfo.EntryID = f.Entry.ID
  7322  		if e.curfn.ABI == obj.ABIInternal && base.Flag.N != 0 {
  7323  			ssa.BuildFuncDebugNoOptimized(base.Ctxt, f, base.Debug.LocationLists > 1, StackOffset, debugInfo)
  7324  		} else {
  7325  			ssa.BuildFuncDebug(base.Ctxt, f, base.Debug.LocationLists, StackOffset, debugInfo)
  7326  		}
  7327  		bstart := s.bstart
  7328  		idToIdx := make([]int, f.NumBlocks())
  7329  		for i, b := range f.Blocks {
  7330  			idToIdx[b.ID] = i
  7331  		}
  7332  		// Register a callback that will be used later to fill in PCs into location
  7333  		// lists. At the moment, Prog.Pc is a sequence number; it's not a real PC
  7334  		// until after assembly, so the translation needs to be deferred.
  7335  		debugInfo.GetPC = func(b, v ssa.ID) int64 {
  7336  			switch v {
  7337  			case ssa.BlockStart.ID:
  7338  				if b == f.Entry.ID {
  7339  					return 0 // Start at the very beginning, at the assembler-generated prologue.
  7340  					// this should only happen for function args (ssa.OpArg)
  7341  				}
  7342  				return bstart[b].Pc
  7343  			case ssa.BlockEnd.ID:
  7344  				blk := f.Blocks[idToIdx[b]]
  7345  				nv := len(blk.Values)
  7346  				return valueToProgAfter[blk.Values[nv-1].ID].Pc
  7347  			case ssa.FuncEnd.ID:
  7348  				return e.curfn.LSym.Size
  7349  			default:
  7350  				return valueToProgAfter[v].Pc
  7351  			}
  7352  		}
  7353  	}
  7354  
  7355  	// Resolve branches, and relax DefaultStmt into NotStmt
  7356  	for _, br := range s.Branches {
  7357  		br.P.To.SetTarget(s.bstart[br.B.ID])
  7358  		if br.P.Pos.IsStmt() != src.PosIsStmt {
  7359  			br.P.Pos = br.P.Pos.WithNotStmt()
  7360  		} else if v0 := br.B.FirstPossibleStmtValue(); v0 != nil && v0.Pos.Line() == br.P.Pos.Line() && v0.Pos.IsStmt() == src.PosIsStmt {
  7361  			br.P.Pos = br.P.Pos.WithNotStmt()
  7362  		}
  7363  
  7364  	}
  7365  
  7366  	// Resolve jump table destinations.
  7367  	for _, jt := range s.JumpTables {
  7368  		// Convert from *Block targets to *Prog targets.
  7369  		targets := make([]*obj.Prog, len(jt.Succs))
  7370  		for i, e := range jt.Succs {
  7371  			targets[i] = s.bstart[e.Block().ID]
  7372  		}
  7373  		// Add to list of jump tables to be resolved at assembly time.
  7374  		// The assembler converts from *Prog entries to absolute addresses
  7375  		// once it knows instruction byte offsets.
  7376  		fi := s.pp.CurFunc.LSym.Func()
  7377  		fi.JumpTables = append(fi.JumpTables, obj.JumpTable{Sym: jt.Aux.(*obj.LSym), Targets: targets})
  7378  	}
  7379  
  7380  	if e.log { // spew to stdout
  7381  		filename := ""
  7382  		for p := s.pp.Text; p != nil; p = p.Link {
  7383  			if p.Pos.IsKnown() && p.InnermostFilename() != filename {
  7384  				filename = p.InnermostFilename()
  7385  				f.Logf("# %s\n", filename)
  7386  			}
  7387  
  7388  			var s string
  7389  			if v, ok := progToValue[p]; ok {
  7390  				s = v.String()
  7391  			} else if b, ok := progToBlock[p]; ok {
  7392  				s = b.String()
  7393  			} else {
  7394  				s = "   " // most value and branch strings are 2-3 characters long
  7395  			}
  7396  			f.Logf(" %-6s\t%.5d (%s)\t%s\n", s, p.Pc, p.InnermostLineNumber(), p.InstructionString())
  7397  		}
  7398  	}
  7399  	if f.HTMLWriter != nil { // spew to ssa.html
  7400  		var buf strings.Builder
  7401  		buf.WriteString("<code>")
  7402  		buf.WriteString("<dl class=\"ssa-gen\">")
  7403  		filename := ""
  7404  
  7405  		liveness := lv.Format(nil)
  7406  		if liveness != "" {
  7407  			buf.WriteString("<dt class=\"ssa-prog-src\"></dt><dd class=\"ssa-prog\">")
  7408  			buf.WriteString(html.EscapeString("# " + liveness))
  7409  			buf.WriteString("</dd>")
  7410  		}
  7411  
  7412  		for p := s.pp.Text; p != nil; p = p.Link {
  7413  			// Don't spam every line with the file name, which is often huge.
  7414  			// Only print changes, and "unknown" is not a change.
  7415  			if p.Pos.IsKnown() && p.InnermostFilename() != filename {
  7416  				filename = p.InnermostFilename()
  7417  				buf.WriteString("<dt class=\"ssa-prog-src\"></dt><dd class=\"ssa-prog\">")
  7418  				buf.WriteString(html.EscapeString("# " + filename))
  7419  				buf.WriteString("</dd>")
  7420  			}
  7421  
  7422  			buf.WriteString("<dt class=\"ssa-prog-src\">")
  7423  			if v, ok := progToValue[p]; ok {
  7424  
  7425  				// Prefix calls with their liveness, if any
  7426  				if p.As != obj.APCDATA {
  7427  					if liveness := lv.Format(v); liveness != "" {
  7428  						// Steal this line, and restart a line
  7429  						buf.WriteString("</dt><dd class=\"ssa-prog\">")
  7430  						buf.WriteString(html.EscapeString("# " + liveness))
  7431  						buf.WriteString("</dd>")
  7432  						// restarting a line
  7433  						buf.WriteString("<dt class=\"ssa-prog-src\">")
  7434  					}
  7435  				}
  7436  
  7437  				buf.WriteString(v.HTML())
  7438  			} else if b, ok := progToBlock[p]; ok {
  7439  				buf.WriteString("<b>" + b.HTML() + "</b>")
  7440  			}
  7441  			buf.WriteString("</dt>")
  7442  			buf.WriteString("<dd class=\"ssa-prog\">")
  7443  			fmt.Fprintf(&buf, "%.5d <span class=\"l%v line-number\">(%s)</span> %s", p.Pc, p.InnermostLineNumber(), p.InnermostLineNumberHTML(), html.EscapeString(p.InstructionString()))
  7444  			buf.WriteString("</dd>")
  7445  		}
  7446  		buf.WriteString("</dl>")
  7447  		buf.WriteString("</code>")
  7448  		f.HTMLWriter.WriteColumn("genssa", "genssa", "ssa-prog", buf.String())
  7449  	}
  7450  	if ssa.GenssaDump[f.Name] {
  7451  		fi := f.DumpFileForPhase("genssa")
  7452  		if fi != nil {
  7453  
  7454  			// inliningDiffers if any filename changes or if any line number except the innermost (last index) changes.
  7455  			inliningDiffers := func(a, b []src.Pos) bool {
  7456  				if len(a) != len(b) {
  7457  					return true
  7458  				}
  7459  				for i := range a {
  7460  					if a[i].Filename() != b[i].Filename() {
  7461  						return true
  7462  					}
  7463  					if i != len(a)-1 && a[i].Line() != b[i].Line() {
  7464  						return true
  7465  					}
  7466  				}
  7467  				return false
  7468  			}
  7469  
  7470  			var allPosOld []src.Pos
  7471  			var allPos []src.Pos
  7472  
  7473  			for p := s.pp.Text; p != nil; p = p.Link {
  7474  				if p.Pos.IsKnown() {
  7475  					allPos = allPos[:0]
  7476  					p.Ctxt.AllPos(p.Pos, func(pos src.Pos) { allPos = append(allPos, pos) })
  7477  					if inliningDiffers(allPos, allPosOld) {
  7478  						for _, pos := range allPos {
  7479  							fmt.Fprintf(fi, "# %s:%d\n", pos.Filename(), pos.Line())
  7480  						}
  7481  						allPos, allPosOld = allPosOld, allPos // swap, not copy, so that they do not share slice storage.
  7482  					}
  7483  				}
  7484  
  7485  				var s string
  7486  				if v, ok := progToValue[p]; ok {
  7487  					s = v.String()
  7488  				} else if b, ok := progToBlock[p]; ok {
  7489  					s = b.String()
  7490  				} else {
  7491  					s = "   " // most value and branch strings are 2-3 characters long
  7492  				}
  7493  				fmt.Fprintf(fi, " %-6s\t%.5d %s\t%s\n", s, p.Pc, ssa.StmtString(p.Pos), p.InstructionString())
  7494  			}
  7495  			fi.Close()
  7496  		}
  7497  	}
  7498  
  7499  	defframe(&s, e, f)
  7500  
  7501  	f.HTMLWriter.Close()
  7502  	f.HTMLWriter = nil
  7503  }
  7504  
  7505  func defframe(s *State, e *ssafn, f *ssa.Func) {
  7506  	pp := s.pp
  7507  
  7508  	s.maxarg = types.RoundUp(s.maxarg, e.stkalign)
  7509  	frame := s.maxarg + e.stksize
  7510  	if Arch.PadFrame != nil {
  7511  		frame = Arch.PadFrame(frame)
  7512  	}
  7513  
  7514  	// Fill in argument and frame size.
  7515  	pp.Text.To.Type = obj.TYPE_TEXTSIZE
  7516  	pp.Text.To.Val = int32(types.RoundUp(f.OwnAux.ArgWidth(), int64(types.RegSize)))
  7517  	pp.Text.To.Offset = frame
  7518  
  7519  	p := pp.Text
  7520  
  7521  	// Insert code to spill argument registers if the named slot may be partially
  7522  	// live. That is, the named slot is considered live by liveness analysis,
  7523  	// (because a part of it is live), but we may not spill all parts into the
  7524  	// slot. This can only happen with aggregate-typed arguments that are SSA-able
  7525  	// and not address-taken (for non-SSA-able or address-taken arguments we always
  7526  	// spill upfront).
  7527  	// Note: spilling is unnecessary in the -N/no-optimize case, since all values
  7528  	// will be considered non-SSAable and spilled up front.
  7529  	// TODO(register args) Make liveness more fine-grained to that partial spilling is okay.
  7530  	if f.OwnAux.ABIInfo().InRegistersUsed() != 0 && base.Flag.N == 0 {
  7531  		// First, see if it is already spilled before it may be live. Look for a spill
  7532  		// in the entry block up to the first safepoint.
  7533  		type nameOff struct {
  7534  			n   *ir.Name
  7535  			off int64
  7536  		}
  7537  		partLiveArgsSpilled := make(map[nameOff]bool)
  7538  		for _, v := range f.Entry.Values {
  7539  			if v.Op.IsCall() {
  7540  				break
  7541  			}
  7542  			if v.Op != ssa.OpStoreReg || v.Args[0].Op != ssa.OpArgIntReg {
  7543  				continue
  7544  			}
  7545  			n, off := ssa.AutoVar(v)
  7546  			if n.Class != ir.PPARAM || n.Addrtaken() || !ssa.CanSSA(n.Type()) || !s.partLiveArgs[n] {
  7547  				continue
  7548  			}
  7549  			partLiveArgsSpilled[nameOff{n, off}] = true
  7550  		}
  7551  
  7552  		// Then, insert code to spill registers if not already.
  7553  		for _, a := range f.OwnAux.ABIInfo().InParams() {
  7554  			n := a.Name
  7555  			if n == nil || n.Addrtaken() || !ssa.CanSSA(n.Type()) || !s.partLiveArgs[n] || len(a.Registers) <= 1 {
  7556  				continue
  7557  			}
  7558  			rts, offs := a.RegisterTypesAndOffsets()
  7559  			for i := range a.Registers {
  7560  				if !rts[i].HasPointers() {
  7561  					continue
  7562  				}
  7563  				if partLiveArgsSpilled[nameOff{n, offs[i]}] {
  7564  					continue // already spilled
  7565  				}
  7566  				reg := ssa.ObjRegForAbiReg(a.Registers[i], f.Config)
  7567  				p = Arch.SpillArgReg(pp, p, f, rts[i], reg, n, offs[i])
  7568  			}
  7569  		}
  7570  	}
  7571  
  7572  	// Insert code to zero ambiguously live variables so that the
  7573  	// garbage collector only sees initialized values when it
  7574  	// looks for pointers.
  7575  	var lo, hi int64
  7576  
  7577  	// Opaque state for backend to use. Current backends use it to
  7578  	// keep track of which helper registers have been zeroed.
  7579  	var state uint32
  7580  
  7581  	// Iterate through declarations. Autos are sorted in decreasing
  7582  	// frame offset order.
  7583  	for _, n := range e.curfn.Dcl {
  7584  		if !n.Needzero() {
  7585  			continue
  7586  		}
  7587  		if n.Class != ir.PAUTO {
  7588  			e.Fatalf(n.Pos(), "needzero class %d", n.Class)
  7589  		}
  7590  		if n.Type().Size()%int64(types.PtrSize) != 0 || n.FrameOffset()%int64(types.PtrSize) != 0 || n.Type().Size() == 0 {
  7591  			e.Fatalf(n.Pos(), "var %L has size %d offset %d", n, n.Type().Size(), n.Offset_)
  7592  		}
  7593  
  7594  		if lo != hi && n.FrameOffset()+n.Type().Size() >= lo-int64(2*types.RegSize) {
  7595  			// Merge with range we already have.
  7596  			lo = n.FrameOffset()
  7597  			continue
  7598  		}
  7599  
  7600  		// Zero old range
  7601  		p = Arch.ZeroRange(pp, p, frame+lo, hi-lo, &state)
  7602  
  7603  		// Set new range.
  7604  		lo = n.FrameOffset()
  7605  		hi = lo + n.Type().Size()
  7606  	}
  7607  
  7608  	// Zero final range.
  7609  	Arch.ZeroRange(pp, p, frame+lo, hi-lo, &state)
  7610  }
  7611  
  7612  // For generating consecutive jump instructions to model a specific branching
  7613  type IndexJump struct {
  7614  	Jump  obj.As
  7615  	Index int
  7616  }
  7617  
  7618  func (s *State) oneJump(b *ssa.Block, jump *IndexJump) {
  7619  	p := s.Br(jump.Jump, b.Succs[jump.Index].Block())
  7620  	p.Pos = b.Pos
  7621  }
  7622  
  7623  // CombJump generates combinational instructions (2 at present) for a block jump,
  7624  // thereby the behaviour of non-standard condition codes could be simulated
  7625  func (s *State) CombJump(b, next *ssa.Block, jumps *[2][2]IndexJump) {
  7626  	switch next {
  7627  	case b.Succs[0].Block():
  7628  		s.oneJump(b, &jumps[0][0])
  7629  		s.oneJump(b, &jumps[0][1])
  7630  	case b.Succs[1].Block():
  7631  		s.oneJump(b, &jumps[1][0])
  7632  		s.oneJump(b, &jumps[1][1])
  7633  	default:
  7634  		var q *obj.Prog
  7635  		if b.Likely != ssa.BranchUnlikely {
  7636  			s.oneJump(b, &jumps[1][0])
  7637  			s.oneJump(b, &jumps[1][1])
  7638  			q = s.Br(obj.AJMP, b.Succs[1].Block())
  7639  		} else {
  7640  			s.oneJump(b, &jumps[0][0])
  7641  			s.oneJump(b, &jumps[0][1])
  7642  			q = s.Br(obj.AJMP, b.Succs[0].Block())
  7643  		}
  7644  		q.Pos = b.Pos
  7645  	}
  7646  }
  7647  
  7648  // AddAux adds the offset in the aux fields (AuxInt and Aux) of v to a.
  7649  func AddAux(a *obj.Addr, v *ssa.Value) {
  7650  	AddAux2(a, v, v.AuxInt)
  7651  }
  7652  func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) {
  7653  	if a.Type != obj.TYPE_MEM && a.Type != obj.TYPE_ADDR {
  7654  		v.Fatalf("bad AddAux addr %v", a)
  7655  	}
  7656  	// add integer offset
  7657  	a.Offset += offset
  7658  
  7659  	// If no additional symbol offset, we're done.
  7660  	if v.Aux == nil {
  7661  		return
  7662  	}
  7663  	// Add symbol's offset from its base register.
  7664  	switch n := v.Aux.(type) {
  7665  	case *ssa.AuxCall:
  7666  		a.Name = obj.NAME_EXTERN
  7667  		a.Sym = n.Fn
  7668  	case *obj.LSym:
  7669  		a.Name = obj.NAME_EXTERN
  7670  		a.Sym = n
  7671  	case *ir.Name:
  7672  		if n.Class == ir.PPARAM || (n.Class == ir.PPARAMOUT && !n.IsOutputParamInRegisters()) {
  7673  			a.Name = obj.NAME_PARAM
  7674  		} else {
  7675  			a.Name = obj.NAME_AUTO
  7676  		}
  7677  		a.Sym = n.Linksym()
  7678  		a.Offset += n.FrameOffset()
  7679  	default:
  7680  		v.Fatalf("aux in %s not implemented %#v", v, v.Aux)
  7681  	}
  7682  }
  7683  
  7684  // extendIndex extends v to a full int width.
  7685  // panic with the given kind if v does not fit in an int (only on 32-bit archs).
  7686  func (s *state) extendIndex(idx, len *ssa.Value, kind ssa.BoundsKind, bounded bool) *ssa.Value {
  7687  	size := idx.Type.Size()
  7688  	if size == s.config.PtrSize {
  7689  		return idx
  7690  	}
  7691  	if size > s.config.PtrSize {
  7692  		// truncate 64-bit indexes on 32-bit pointer archs. Test the
  7693  		// high word and branch to out-of-bounds failure if it is not 0.
  7694  		var lo *ssa.Value
  7695  		if idx.Type.IsSigned() {
  7696  			lo = s.newValue1(ssa.OpInt64Lo, types.Types[types.TINT], idx)
  7697  		} else {
  7698  			lo = s.newValue1(ssa.OpInt64Lo, types.Types[types.TUINT], idx)
  7699  		}
  7700  		if bounded || base.Flag.B != 0 {
  7701  			return lo
  7702  		}
  7703  		bNext := s.f.NewBlock(ssa.BlockPlain)
  7704  		bPanic := s.f.NewBlock(ssa.BlockExit)
  7705  		hi := s.newValue1(ssa.OpInt64Hi, types.Types[types.TUINT32], idx)
  7706  		cmp := s.newValue2(ssa.OpEq32, types.Types[types.TBOOL], hi, s.constInt32(types.Types[types.TUINT32], 0))
  7707  		if !idx.Type.IsSigned() {
  7708  			switch kind {
  7709  			case ssa.BoundsIndex:
  7710  				kind = ssa.BoundsIndexU
  7711  			case ssa.BoundsSliceAlen:
  7712  				kind = ssa.BoundsSliceAlenU
  7713  			case ssa.BoundsSliceAcap:
  7714  				kind = ssa.BoundsSliceAcapU
  7715  			case ssa.BoundsSliceB:
  7716  				kind = ssa.BoundsSliceBU
  7717  			case ssa.BoundsSlice3Alen:
  7718  				kind = ssa.BoundsSlice3AlenU
  7719  			case ssa.BoundsSlice3Acap:
  7720  				kind = ssa.BoundsSlice3AcapU
  7721  			case ssa.BoundsSlice3B:
  7722  				kind = ssa.BoundsSlice3BU
  7723  			case ssa.BoundsSlice3C:
  7724  				kind = ssa.BoundsSlice3CU
  7725  			}
  7726  		}
  7727  		b := s.endBlock()
  7728  		b.Kind = ssa.BlockIf
  7729  		b.SetControl(cmp)
  7730  		b.Likely = ssa.BranchLikely
  7731  		b.AddEdgeTo(bNext)
  7732  		b.AddEdgeTo(bPanic)
  7733  
  7734  		s.startBlock(bPanic)
  7735  		mem := s.newValue4I(ssa.OpPanicExtend, types.TypeMem, int64(kind), hi, lo, len, s.mem())
  7736  		s.endBlock().SetControl(mem)
  7737  		s.startBlock(bNext)
  7738  
  7739  		return lo
  7740  	}
  7741  
  7742  	// Extend value to the required size
  7743  	var op ssa.Op
  7744  	if idx.Type.IsSigned() {
  7745  		switch 10*size + s.config.PtrSize {
  7746  		case 14:
  7747  			op = ssa.OpSignExt8to32
  7748  		case 18:
  7749  			op = ssa.OpSignExt8to64
  7750  		case 24:
  7751  			op = ssa.OpSignExt16to32
  7752  		case 28:
  7753  			op = ssa.OpSignExt16to64
  7754  		case 48:
  7755  			op = ssa.OpSignExt32to64
  7756  		default:
  7757  			s.Fatalf("bad signed index extension %s", idx.Type)
  7758  		}
  7759  	} else {
  7760  		switch 10*size + s.config.PtrSize {
  7761  		case 14:
  7762  			op = ssa.OpZeroExt8to32
  7763  		case 18:
  7764  			op = ssa.OpZeroExt8to64
  7765  		case 24:
  7766  			op = ssa.OpZeroExt16to32
  7767  		case 28:
  7768  			op = ssa.OpZeroExt16to64
  7769  		case 48:
  7770  			op = ssa.OpZeroExt32to64
  7771  		default:
  7772  			s.Fatalf("bad unsigned index extension %s", idx.Type)
  7773  		}
  7774  	}
  7775  	return s.newValue1(op, types.Types[types.TINT], idx)
  7776  }
  7777  
  7778  // CheckLoweredPhi checks that regalloc and stackalloc correctly handled phi values.
  7779  // Called during ssaGenValue.
  7780  func CheckLoweredPhi(v *ssa.Value) {
  7781  	if v.Op != ssa.OpPhi {
  7782  		v.Fatalf("CheckLoweredPhi called with non-phi value: %v", v.LongString())
  7783  	}
  7784  	if v.Type.IsMemory() {
  7785  		return
  7786  	}
  7787  	f := v.Block.Func
  7788  	loc := f.RegAlloc[v.ID]
  7789  	for _, a := range v.Args {
  7790  		if aloc := f.RegAlloc[a.ID]; aloc != loc { // TODO: .Equal() instead?
  7791  			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)
  7792  		}
  7793  	}
  7794  }
  7795  
  7796  // CheckLoweredGetClosurePtr checks that v is the first instruction in the function's entry block,
  7797  // except for incoming in-register arguments.
  7798  // The output of LoweredGetClosurePtr is generally hardwired to the correct register.
  7799  // That register contains the closure pointer on closure entry.
  7800  func CheckLoweredGetClosurePtr(v *ssa.Value) {
  7801  	entry := v.Block.Func.Entry
  7802  	if entry != v.Block {
  7803  		base.Fatalf("in %s, badly placed LoweredGetClosurePtr: %v %v", v.Block.Func.Name, v.Block, v)
  7804  	}
  7805  	for _, w := range entry.Values {
  7806  		if w == v {
  7807  			break
  7808  		}
  7809  		switch w.Op {
  7810  		case ssa.OpArgIntReg, ssa.OpArgFloatReg:
  7811  			// okay
  7812  		default:
  7813  			base.Fatalf("in %s, badly placed LoweredGetClosurePtr: %v %v", v.Block.Func.Name, v.Block, v)
  7814  		}
  7815  	}
  7816  }
  7817  
  7818  // CheckArgReg ensures that v is in the function's entry block.
  7819  func CheckArgReg(v *ssa.Value) {
  7820  	entry := v.Block.Func.Entry
  7821  	if entry != v.Block {
  7822  		base.Fatalf("in %s, badly placed ArgIReg or ArgFReg: %v %v", v.Block.Func.Name, v.Block, v)
  7823  	}
  7824  }
  7825  
  7826  func AddrAuto(a *obj.Addr, v *ssa.Value) {
  7827  	n, off := ssa.AutoVar(v)
  7828  	a.Type = obj.TYPE_MEM
  7829  	a.Sym = n.Linksym()
  7830  	a.Reg = int16(Arch.REGSP)
  7831  	a.Offset = n.FrameOffset() + off
  7832  	if n.Class == ir.PPARAM || (n.Class == ir.PPARAMOUT && !n.IsOutputParamInRegisters()) {
  7833  		a.Name = obj.NAME_PARAM
  7834  	} else {
  7835  		a.Name = obj.NAME_AUTO
  7836  	}
  7837  }
  7838  
  7839  // Call returns a new CALL instruction for the SSA value v.
  7840  // It uses PrepareCall to prepare the call.
  7841  func (s *State) Call(v *ssa.Value) *obj.Prog {
  7842  	pPosIsStmt := s.pp.Pos.IsStmt() // The statement-ness of the call comes from ssaGenState
  7843  	s.PrepareCall(v)
  7844  
  7845  	p := s.Prog(obj.ACALL)
  7846  	if pPosIsStmt == src.PosIsStmt {
  7847  		p.Pos = v.Pos.WithIsStmt()
  7848  	} else {
  7849  		p.Pos = v.Pos.WithNotStmt()
  7850  	}
  7851  	if sym, ok := v.Aux.(*ssa.AuxCall); ok && sym.Fn != nil {
  7852  		p.To.Type = obj.TYPE_MEM
  7853  		p.To.Name = obj.NAME_EXTERN
  7854  		p.To.Sym = sym.Fn
  7855  	} else {
  7856  		// TODO(mdempsky): Can these differences be eliminated?
  7857  		switch Arch.LinkArch.Family {
  7858  		case sys.AMD64, sys.I386, sys.PPC64, sys.RISCV64, sys.S390X, sys.Wasm:
  7859  			p.To.Type = obj.TYPE_REG
  7860  		case sys.ARM, sys.ARM64, sys.Loong64, sys.MIPS, sys.MIPS64:
  7861  			p.To.Type = obj.TYPE_MEM
  7862  		default:
  7863  			base.Fatalf("unknown indirect call family")
  7864  		}
  7865  		p.To.Reg = v.Args[0].Reg()
  7866  	}
  7867  	return p
  7868  }
  7869  
  7870  // TailCall returns a new tail call instruction for the SSA value v.
  7871  // It is like Call, but for a tail call.
  7872  func (s *State) TailCall(v *ssa.Value) *obj.Prog {
  7873  	p := s.Call(v)
  7874  	p.As = obj.ARET
  7875  	return p
  7876  }
  7877  
  7878  // PrepareCall prepares to emit a CALL instruction for v and does call-related bookkeeping.
  7879  // It must be called immediately before emitting the actual CALL instruction,
  7880  // since it emits PCDATA for the stack map at the call (calls are safe points).
  7881  func (s *State) PrepareCall(v *ssa.Value) {
  7882  	idx := s.livenessMap.Get(v)
  7883  	if !idx.StackMapValid() {
  7884  		// See Liveness.hasStackMap.
  7885  		if sym, ok := v.Aux.(*ssa.AuxCall); !ok || !(sym.Fn == ir.Syms.WBZero || sym.Fn == ir.Syms.WBMove) {
  7886  			base.Fatalf("missing stack map index for %v", v.LongString())
  7887  		}
  7888  	}
  7889  
  7890  	call, ok := v.Aux.(*ssa.AuxCall)
  7891  
  7892  	if ok {
  7893  		// Record call graph information for nowritebarrierrec
  7894  		// analysis.
  7895  		if nowritebarrierrecCheck != nil {
  7896  			nowritebarrierrecCheck.recordCall(s.pp.CurFunc, call.Fn, v.Pos)
  7897  		}
  7898  	}
  7899  
  7900  	if s.maxarg < v.AuxInt {
  7901  		s.maxarg = v.AuxInt
  7902  	}
  7903  }
  7904  
  7905  // UseArgs records the fact that an instruction needs a certain amount of
  7906  // callee args space for its use.
  7907  func (s *State) UseArgs(n int64) {
  7908  	if s.maxarg < n {
  7909  		s.maxarg = n
  7910  	}
  7911  }
  7912  
  7913  // fieldIdx finds the index of the field referred to by the ODOT node n.
  7914  func fieldIdx(n *ir.SelectorExpr) int {
  7915  	t := n.X.Type()
  7916  	if !isStructNotSIMD(t) {
  7917  		panic("ODOT's LHS is not a struct")
  7918  	}
  7919  
  7920  	for i, f := range t.Fields() {
  7921  		if f.Sym == n.Sel {
  7922  			if f.Offset != n.Offset() {
  7923  				panic("field offset doesn't match")
  7924  			}
  7925  			return i
  7926  		}
  7927  	}
  7928  	panic(fmt.Sprintf("can't find field in expr %v\n", n))
  7929  
  7930  	// TODO: keep the result of this function somewhere in the ODOT Node
  7931  	// so we don't have to recompute it each time we need it.
  7932  }
  7933  
  7934  // ssafn holds frontend information about a function that the backend is processing.
  7935  // It also exports a bunch of compiler services for the ssa backend.
  7936  type ssafn struct {
  7937  	curfn      *ir.Func
  7938  	strings    map[string]*obj.LSym // map from constant string to data symbols
  7939  	stksize    int64                // stack size for current frame
  7940  	stkptrsize int64                // prefix of stack containing pointers
  7941  
  7942  	// alignment for current frame.
  7943  	// NOTE: when stkalign > PtrSize, currently this only ensures the offsets of
  7944  	// objects in the stack frame are aligned. The stack pointer is still aligned
  7945  	// only PtrSize.
  7946  	stkalign int64
  7947  
  7948  	log bool // print ssa debug to the stdout
  7949  }
  7950  
  7951  // StringData returns a symbol which
  7952  // is the data component of a global string constant containing s.
  7953  func (e *ssafn) StringData(s string) *obj.LSym {
  7954  	if aux, ok := e.strings[s]; ok {
  7955  		return aux
  7956  	}
  7957  	if e.strings == nil {
  7958  		e.strings = make(map[string]*obj.LSym)
  7959  	}
  7960  	data := staticdata.StringSym(e.curfn.Pos(), s)
  7961  	e.strings[s] = data
  7962  	return data
  7963  }
  7964  
  7965  // SplitSlot returns a slot representing the data of parent starting at offset.
  7966  func (e *ssafn) SplitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot {
  7967  	node := parent.N
  7968  
  7969  	if node.Class != ir.PAUTO || node.Addrtaken() {
  7970  		// addressed things and non-autos retain their parents (i.e., cannot truly be split)
  7971  		return ssa.LocalSlot{N: node, Type: t, Off: parent.Off + offset}
  7972  	}
  7973  
  7974  	sym := &types.Sym{Name: node.Sym().Name + suffix, Pkg: types.LocalPkg}
  7975  	n := e.curfn.NewLocal(parent.N.Pos(), sym, t)
  7976  	n.SetUsed(true)
  7977  	n.SetEsc(ir.EscNever)
  7978  	types.CalcSize(t)
  7979  	return ssa.LocalSlot{N: n, Type: t, Off: 0, SplitOf: parent, SplitOffset: offset}
  7980  }
  7981  
  7982  // Logf logs a message from the compiler.
  7983  func (e *ssafn) Logf(msg string, args ...any) {
  7984  	if e.log {
  7985  		fmt.Printf(msg, args...)
  7986  	}
  7987  }
  7988  
  7989  func (e *ssafn) Log() bool {
  7990  	return e.log
  7991  }
  7992  
  7993  // Fatalf reports a compiler error and exits.
  7994  func (e *ssafn) Fatalf(pos src.XPos, msg string, args ...any) {
  7995  	base.Pos = pos
  7996  	nargs := append([]any{ir.FuncName(e.curfn)}, args...)
  7997  	base.Fatalf("'%s': "+msg, nargs...)
  7998  }
  7999  
  8000  // Warnl reports a "warning", which is usually flag-triggered
  8001  // logging output for the benefit of tests.
  8002  func (e *ssafn) Warnl(pos src.XPos, fmt_ string, args ...any) {
  8003  	base.WarnfAt(pos, fmt_, args...)
  8004  }
  8005  
  8006  func (e *ssafn) Debug_checknil() bool {
  8007  	return base.Debug.Nil != 0
  8008  }
  8009  
  8010  func (e *ssafn) UseWriteBarrier() bool {
  8011  	return base.Flag.WB
  8012  }
  8013  
  8014  func (e *ssafn) Syslook(name string) *obj.LSym {
  8015  	switch name {
  8016  	case "goschedguarded":
  8017  		return ir.Syms.Goschedguarded
  8018  	case "writeBarrier":
  8019  		return ir.Syms.WriteBarrier
  8020  	case "wbZero":
  8021  		return ir.Syms.WBZero
  8022  	case "wbMove":
  8023  		return ir.Syms.WBMove
  8024  	case "cgoCheckMemmove":
  8025  		return ir.Syms.CgoCheckMemmove
  8026  	case "cgoCheckPtrWrite":
  8027  		return ir.Syms.CgoCheckPtrWrite
  8028  	}
  8029  	e.Fatalf(src.NoXPos, "unknown Syslook func %v", name)
  8030  	return nil
  8031  }
  8032  
  8033  func (e *ssafn) Func() *ir.Func {
  8034  	return e.curfn
  8035  }
  8036  
  8037  func clobberBase(n ir.Node) ir.Node {
  8038  	if n.Op() == ir.ODOT {
  8039  		n := n.(*ir.SelectorExpr)
  8040  		if n.X.Type().NumFields() == 1 {
  8041  			return clobberBase(n.X)
  8042  		}
  8043  	}
  8044  	if n.Op() == ir.OINDEX {
  8045  		n := n.(*ir.IndexExpr)
  8046  		if n.X.Type().IsArray() && n.X.Type().NumElem() == 1 {
  8047  			return clobberBase(n.X)
  8048  		}
  8049  	}
  8050  	return n
  8051  }
  8052  
  8053  // callTargetLSym returns the correct LSym to call 'callee' using its ABI.
  8054  func callTargetLSym(callee *ir.Name) *obj.LSym {
  8055  	if callee.Func == nil {
  8056  		// TODO(austin): This happens in case of interface method I.M from imported package.
  8057  		// It's ABIInternal, and would be better if callee.Func was never nil and we didn't
  8058  		// need this case.
  8059  		return callee.Linksym()
  8060  	}
  8061  
  8062  	return callee.LinksymABI(callee.Func.ABI)
  8063  }
  8064  
  8065  // deferStructFnField is the field index of _defer.fn.
  8066  const deferStructFnField = 4
  8067  
  8068  var deferType *types.Type
  8069  
  8070  // deferstruct returns a type interchangeable with runtime._defer.
  8071  // Make sure this stays in sync with runtime/runtime2.go:_defer.
  8072  func deferstruct() *types.Type {
  8073  	if deferType != nil {
  8074  		return deferType
  8075  	}
  8076  
  8077  	makefield := func(name string, t *types.Type) *types.Field {
  8078  		sym := (*types.Pkg)(nil).Lookup(name)
  8079  		return types.NewField(src.NoXPos, sym, t)
  8080  	}
  8081  
  8082  	fields := []*types.Field{
  8083  		makefield("heap", types.Types[types.TBOOL]),
  8084  		makefield("rangefunc", types.Types[types.TBOOL]),
  8085  		makefield("sp", types.Types[types.TUINTPTR]),
  8086  		makefield("pc", types.Types[types.TUINTPTR]),
  8087  		// Note: the types here don't really matter. Defer structures
  8088  		// are always scanned explicitly during stack copying and GC,
  8089  		// so we make them uintptr type even though they are real pointers.
  8090  		makefield("fn", types.Types[types.TUINTPTR]),
  8091  		makefield("link", types.Types[types.TUINTPTR]),
  8092  		makefield("head", types.Types[types.TUINTPTR]),
  8093  	}
  8094  	if name := fields[deferStructFnField].Sym.Name; name != "fn" {
  8095  		base.Fatalf("deferStructFnField is %q, not fn", name)
  8096  	}
  8097  
  8098  	n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, ir.Pkgs.Runtime.Lookup("_defer"))
  8099  	typ := types.NewNamed(n)
  8100  	n.SetType(typ)
  8101  	n.SetTypecheck(1)
  8102  
  8103  	// build struct holding the above fields
  8104  	typ.SetUnderlying(types.NewStruct(fields))
  8105  	types.CalcStructSize(typ)
  8106  
  8107  	deferType = typ
  8108  	return typ
  8109  }
  8110  
  8111  // SpillSlotAddr uses LocalSlot information to initialize an obj.Addr
  8112  // The resulting addr is used in a non-standard context -- in the prologue
  8113  // of a function, before the frame has been constructed, so the standard
  8114  // addressing for the parameters will be wrong.
  8115  func SpillSlotAddr(spill ssa.Spill, baseReg int16, extraOffset int64) obj.Addr {
  8116  	return obj.Addr{
  8117  		Name:   obj.NAME_NONE,
  8118  		Type:   obj.TYPE_MEM,
  8119  		Reg:    baseReg,
  8120  		Offset: spill.Offset + extraOffset,
  8121  	}
  8122  }
  8123  
  8124  func isStructNotSIMD(t *types.Type) bool {
  8125  	return t.IsStruct() && !t.IsSIMD()
  8126  }
  8127  
  8128  var BoundsCheckFunc [ssa.BoundsKindCount]*obj.LSym
  8129  

View as plain text