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