1 // Copyright 2020 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.
8 "cmd/compile/internal/types"
21 type offsetKey struct {
27 func isBlockMultiValueExit(b *Block) bool {
28 return (b.Kind == BlockRet || b.Kind == BlockRetJmp) && len(b.Controls) > 0 && b.Controls[0].Op == OpMakeResult
31 // expandCalls converts LE (Late Expansion) calls that act like they receive value args into a lower-level form
32 // that is more oriented to a platform's ABI. The SelectN operations that extract results are rewritten into
33 // more appropriate forms, and any StructMake or ArrayMake inputs are decomposed until non-struct values are
34 // reached. On the callee side, OpArg nodes are not decomposed until this phase is run.
35 // TODO results should not be lowered until this phase.
36 func expandCalls(f *Func) {
37 // Calls that need lowering have some number of inputs, including a memory input,
38 // and produce a tuple of (value1, value2, ..., mem) where valueK may or may not be SSA-able.
40 // With the current ABI those inputs need to be converted into stores to memory,
41 // rethreading the call's memory input to the first, and the new call now receiving the last.
43 // With the current ABI, the outputs need to be converted to loads, which will all use the call's
44 // memory output as their input.
45 if !LateCallExpansionEnabledWithin(f) {
48 debug := f.pass.debug > 0
51 fmt.Printf("\nexpandsCalls(%s)\n", f.Name)
54 canSSAType := f.fe.CanSSA
55 regSize := f.Config.RegSize
57 typ := &f.Config.Types
58 ptrSize := f.Config.PtrSize
60 // For 32-bit, need to deal with decomposition of 64-bit integers, which depends on endianness.
61 var hiOffset, lowOffset int64
62 if f.Config.BigEndian {
68 namedSelects := make(map[*Value][]namedVal)
72 common := make(map[selKey]*Value)
74 // intPairTypes returns the pair of 32-bit int types needed to encode a 64-bit integer type on a target
75 // that has no 64-bit integer registers.
76 intPairTypes := func(et types.Kind) (tHi, tLo *types.Type) {
78 if et == types.TINT64 {
85 // isAlreadyExpandedAggregateType returns whether a type is an SSA-able "aggregate" (multiple register) type
86 // that was expanded in an earlier phase (currently, expand_calls is intended to run after decomposeBuiltin,
87 // so this is all aggregate types -- small struct and array, complex, interface, string, slice, and 64-bit
88 // integer on 32-bit).
89 isAlreadyExpandedAggregateType := func(t *types.Type) bool {
93 return t.IsStruct() || t.IsArray() || t.IsComplex() || t.IsInterface() || t.IsString() || t.IsSlice() ||
94 t.Size() > regSize && t.IsInteger()
97 offsets := make(map[offsetKey]*Value)
99 // offsetFrom creates an offset from a pointer, simplifying chained offsets and offsets from SP
100 // TODO should also optimize offsets from SB?
101 offsetFrom := func(from *Value, offset int64, pt *types.Type) *Value {
102 if offset == 0 && from.Type == pt { // this is not actually likely
105 // Simplify, canonicalize
106 for from.Op == OpOffPtr {
107 offset += from.AuxInt
111 return f.ConstOffPtrSP(pt, offset, sp)
113 key := offsetKey{from, offset, pt}
118 v = from.Block.NewValue1I(from.Pos.WithNotStmt(), OpOffPtr, pt, offset, from)
123 // splitSlots splits one "field" (specified by sfx, offset, and ty) out of the LocalSlots in ls and returns the new LocalSlots this generates.
124 splitSlots := func(ls []LocalSlot, sfx string, offset int64, ty *types.Type) []LocalSlot {
127 locs = append(locs, f.fe.SplitSlot(&ls[i], sfx, offset, ty))
132 // removeTrivialWrapperTypes unwraps layers of
133 // struct { singleField SomeType } and [1]SomeType
134 // until a non-wrapper type is reached. This is useful
135 // for working with assignments to/from interface data
136 // fields (either second operand to OpIMake or OpIData)
137 // where the wrapping or type conversion can be elided
138 // because of type conversions/assertions in source code
139 // that do not appear in SSA.
140 removeTrivialWrapperTypes := func(t *types.Type) *types.Type {
142 if t.IsStruct() && t.NumFields() == 1 {
146 if t.IsArray() && t.NumElem() == 1 {
155 // Calls that need lowering have some number of inputs, including a memory input,
156 // and produce a tuple of (value1, value2, ..., mem) where valueK may or may not be SSA-able.
158 // With the current ABI those inputs need to be converted into stores to memory,
159 // rethreading the call's memory input to the first, and the new call now receiving the last.
161 // With the current ABI, the outputs need to be converted to loads, which will all use the call's
162 // memory output as their input.
164 // rewriteSelect recursively walks from leaf selector to a root (OpSelectN, OpLoad, OpArg)
165 // through a chain of Struct/Array/builtin Select operations. If the chain of selectors does not
166 // end in an expected root, it does nothing (this can happen depending on compiler phase ordering).
167 // The "leaf" provides the type, the root supplies the container, and the leaf-to-root path
168 // accumulates the offset.
169 // It emits the code necessary to implement the leaf select operation that leads to the root.
171 // TODO when registers really arrive, must also decompose anything split across two registers or registers and memory.
172 var rewriteSelect func(leaf *Value, selector *Value, offset int64) []LocalSlot
173 rewriteSelect = func(leaf *Value, selector *Value, offset int64) []LocalSlot {
175 fmt.Printf("rewriteSelect(%s, %s, %d)\n", leaf.LongString(), selector.LongString(), offset)
178 leafType := leaf.Type
179 if len(selector.Args) > 0 {
180 w := selector.Args[0]
185 selector.SetArg(0, w)
190 if !isAlreadyExpandedAggregateType(selector.Type) {
191 if leafType == selector.Type { // OpIData leads us here, sometimes.
192 leaf.copyOf(selector)
194 f.Fatalf("Unexpected OpArg type, selector=%s, leaf=%s\n", selector.LongString(), leaf.LongString())
197 fmt.Printf("\tOpArg, break\n")
202 case OpIData, OpStructSelect, OpArraySelect:
203 leafType = removeTrivialWrapperTypes(leaf.Type)
206 auxInt := selector.AuxInt + offset
207 if leaf.Block == selector.Block {
213 w := selector.Block.NewValue0IA(leaf.Pos, OpArg, leafType, auxInt, aux)
216 fmt.Printf("\tnew %s\n", w.LongString())
219 for _, s := range namedSelects[selector] {
220 locs = append(locs, f.Names[s.locIndex])
223 case OpLoad: // We end up here because of IData of immediate structures.
225 // (note the failure case is very rare; w/o this case, make.bash and run.bash both pass, as well as
226 // the hard cases of building {syscall,math,math/cmplx,math/bits,go/constant} on ppc64le and mips-softfloat).
228 // GOSSAFUNC='(*dumper).dump' go build -gcflags=-l -tags=math_big_pure_go cmd/compile/internal/gc
229 // cmd/compile/internal/gc/dump.go:136:14: internal compiler error: '(*dumper).dump': not lowered: v827, StructSelect PTR PTR
231 // v20 (+142) = StaticLECall <interface {},mem> {AuxCall{reflect.Value.Interface([reflect.Value,0])[interface {},24]}} [40] v8 v1
232 // v21 (142) = SelectN <mem> [1] v20
233 // v22 (142) = SelectN <interface {}> [0] v20
235 // v71 (+143) = IData <Nodes> v22 (v[Nodes])
236 // v73 (+146) = StaticLECall <[]*Node,mem> {AuxCall{"".Nodes.Slice([Nodes,0])[[]*Node,8]}} [32] v71 v21
238 // translates (w/o the "case OpLoad:" above) to:
241 // v20 (+142) = StaticCall <mem> {AuxCall{reflect.Value.Interface([reflect.Value,0])[interface {},24]}} [40] v715
242 // v23 (142) = Load <*uintptr> v19 v20
243 // v823 (142) = IsNonNil <bool> v23
244 // v67 (+143) = Load <*[]*Node> v880 v20
246 // v827 (146) = StructSelect <*[]*Node> [0] v67
247 // v846 (146) = Store <mem> {*[]*Node} v769 v827 v20
248 // v73 (+146) = StaticCall <mem> {AuxCall{"".Nodes.Slice([Nodes,0])[[]*Node,8]}} [32] v846
249 // i.e., the struct select is generated and remains in because it is not applied to an actual structure.
250 // The OpLoad was created to load the single field of the IData
251 // This case removes that StructSelect.
252 if leafType != selector.Type {
253 f.Fatalf("Unexpected Load as selector, leaf=%s, selector=%s\n", leaf.LongString(), selector.LongString())
255 leaf.copyOf(selector)
256 for _, s := range namedSelects[selector] {
257 locs = append(locs, f.Names[s.locIndex])
261 // TODO these may be duplicated. Should memoize. Intermediate selectors will go dead, no worries there.
262 call := selector.Args[0]
263 aux := call.Aux.(*AuxCall)
264 which := selector.AuxInt
265 if which == aux.NResults() { // mem is after the results.
266 // rewrite v as a Copy of call -- the replacement call will produce a mem.
269 leafType := removeTrivialWrapperTypes(leaf.Type)
270 if canSSAType(leafType) {
271 pt := types.NewPtr(leafType)
272 off := offsetFrom(sp, offset+aux.OffsetOfResult(which), pt)
273 // Any selection right out of the arg area/registers has to be same Block as call, use call as mem input.
274 if leaf.Block == call.Block {
276 leaf.SetArgs2(off, call)
279 w := call.Block.NewValue2(leaf.Pos, OpLoad, leafType, off, call)
282 fmt.Printf("\tnew %s\n", w.LongString())
285 for _, s := range namedSelects[selector] {
286 locs = append(locs, f.Names[s.locIndex])
289 f.Fatalf("Should not have non-SSA-able OpSelectN, selector=%s", selector.LongString())
294 w := selector.Args[0]
296 if w.Type.Kind() != types.TSTRUCT { // IData artifact
297 ls = rewriteSelect(leaf, w, offset)
299 ls = rewriteSelect(leaf, w, offset+w.Type.FieldOff(int(selector.AuxInt)))
301 for _, l := range ls {
302 locs = append(locs, f.fe.SplitStruct(l, int(selector.AuxInt)))
308 w := selector.Args[0]
309 rewriteSelect(leaf, w, offset+selector.Type.Size()*selector.AuxInt)
312 w := selector.Args[0]
313 ls := rewriteSelect(leaf, w, offset+hiOffset)
314 locs = splitSlots(ls, ".hi", hiOffset, leafType)
317 w := selector.Args[0]
318 ls := rewriteSelect(leaf, w, offset+lowOffset)
319 locs = splitSlots(ls, ".lo", lowOffset, leafType)
322 ls := rewriteSelect(leaf, selector.Args[0], offset)
323 locs = splitSlots(ls, ".ptr", 0, typ.BytePtr)
326 w := selector.Args[0]
327 ls := rewriteSelect(leaf, w, offset)
328 locs = splitSlots(ls, ".ptr", 0, types.NewPtr(w.Type.Elem()))
331 w := selector.Args[0]
332 ls := rewriteSelect(leaf, w, offset)
334 if w.Type.IsEmptyInterface() {
337 locs = splitSlots(ls, sfx, 0, typ.Uintptr)
340 ls := rewriteSelect(leaf, selector.Args[0], offset)
341 locs = splitSlots(ls, ".real", 0, leafType)
344 ls := rewriteSelect(leaf, selector.Args[0], offset+leafType.Width) // result is FloatNN, width of result is offset of imaginary part.
345 locs = splitSlots(ls, ".imag", leafType.Width, leafType)
347 case OpStringLen, OpSliceLen:
348 ls := rewriteSelect(leaf, selector.Args[0], offset+ptrSize)
349 locs = splitSlots(ls, ".len", ptrSize, leafType)
352 ls := rewriteSelect(leaf, selector.Args[0], offset+ptrSize)
353 locs = splitSlots(ls, ".data", ptrSize, leafType)
356 ls := rewriteSelect(leaf, selector.Args[0], offset+2*ptrSize)
357 locs = splitSlots(ls, ".cap", 2*ptrSize, leafType)
359 case OpCopy: // If it's an intermediate result, recurse
360 locs = rewriteSelect(leaf, selector.Args[0], offset)
361 for _, s := range namedSelects[selector] {
362 // this copy may have had its own name, preserve that, too.
363 locs = append(locs, f.Names[s.locIndex])
367 // Ignore dead ends. These can occur if this phase is run before decompose builtin (which is not intended, but allowed).
373 // storeArgOrLoad converts stores of SSA-able aggregate arguments (passed to a call) into a series of primitive-typed
374 // stores of non-aggregate types. It recursively walks up a chain of selectors until it reaches a Load or an Arg.
375 // If it does not reach a Load or an Arg, nothing happens; this allows a little freedom in phase ordering.
376 var storeArgOrLoad func(pos src.XPos, b *Block, base, source, mem *Value, t *types.Type, offset int64) *Value
378 // decomposeArgOrLoad is a helper for storeArgOrLoad.
379 // It decomposes a Load or an Arg into smaller parts, parameterized by the decomposeOne and decomposeTwo functions
380 // passed to it, and returns the new mem. If the type does not match one of the expected aggregate types, it returns nil instead.
381 decomposeArgOrLoad := func(pos src.XPos, b *Block, base, source, mem *Value, t *types.Type, offset int64,
382 decomposeOne func(pos src.XPos, b *Block, base, source, mem *Value, t1 *types.Type, offArg, offStore int64) *Value,
383 decomposeTwo func(pos src.XPos, b *Block, base, source, mem *Value, t1, t2 *types.Type, offArg, offStore int64) *Value) *Value {
388 for i := int64(0); i < u.NumElem(); i++ {
389 elemOff := i * elem.Size()
390 mem = decomposeOne(pos, b, base, source, mem, elem, source.AuxInt+elemOff, offset+elemOff)
391 pos = pos.WithNotStmt()
395 for i := 0; i < u.NumFields(); i++ {
397 mem = decomposeOne(pos, b, base, source, mem, fld.Type, source.AuxInt+fld.Offset, offset+fld.Offset)
398 pos = pos.WithNotStmt()
401 case types.TINT64, types.TUINT64:
402 if t.Width == regSize {
405 tHi, tLo := intPairTypes(t.Kind())
406 mem = decomposeOne(pos, b, base, source, mem, tHi, source.AuxInt+hiOffset, offset+hiOffset)
407 pos = pos.WithNotStmt()
408 return decomposeOne(pos, b, base, source, mem, tLo, source.AuxInt+lowOffset, offset+lowOffset)
410 return decomposeTwo(pos, b, base, source, mem, typ.Uintptr, typ.BytePtr, source.AuxInt, offset)
412 return decomposeTwo(pos, b, base, source, mem, typ.BytePtr, typ.Int, source.AuxInt, offset)
413 case types.TCOMPLEX64:
414 return decomposeTwo(pos, b, base, source, mem, typ.Float32, typ.Float32, source.AuxInt, offset)
415 case types.TCOMPLEX128:
416 return decomposeTwo(pos, b, base, source, mem, typ.Float64, typ.Float64, source.AuxInt, offset)
418 mem = decomposeTwo(pos, b, base, source, mem, typ.BytePtr, typ.Int, source.AuxInt, offset)
419 return decomposeOne(pos, b, base, source, mem, typ.Int, source.AuxInt+2*ptrSize, offset+2*ptrSize)
424 // storeOneArg creates a decomposed (one step) arg that is then stored.
425 // pos and b locate the store instruction, base is the base of the store target, source is the "base" of the value input,
426 // mem is the input mem, t is the type in question, and offArg and offStore are the offsets from the respective bases.
427 storeOneArg := func(pos src.XPos, b *Block, base, source, mem *Value, t *types.Type, offArg, offStore int64) *Value {
428 w := common[selKey{source, offArg, t.Width, t}]
430 w = source.Block.NewValue0IA(source.Pos, OpArg, t, offArg, source.Aux)
431 common[selKey{source, offArg, t.Width, t}] = w
433 return storeArgOrLoad(pos, b, base, w, mem, t, offStore)
436 // storeOneLoad creates a decomposed (one step) load that is then stored.
437 storeOneLoad := func(pos src.XPos, b *Block, base, source, mem *Value, t *types.Type, offArg, offStore int64) *Value {
438 from := offsetFrom(source.Args[0], offArg, types.NewPtr(t))
439 w := source.Block.NewValue2(source.Pos, OpLoad, t, from, mem)
440 return storeArgOrLoad(pos, b, base, w, mem, t, offStore)
443 storeTwoArg := func(pos src.XPos, b *Block, base, source, mem *Value, t1, t2 *types.Type, offArg, offStore int64) *Value {
444 mem = storeOneArg(pos, b, base, source, mem, t1, offArg, offStore)
445 pos = pos.WithNotStmt()
447 return storeOneArg(pos, b, base, source, mem, t2, offArg+t1Size, offStore+t1Size)
450 storeTwoLoad := func(pos src.XPos, b *Block, base, source, mem *Value, t1, t2 *types.Type, offArg, offStore int64) *Value {
451 mem = storeOneLoad(pos, b, base, source, mem, t1, offArg, offStore)
452 pos = pos.WithNotStmt()
454 return storeOneLoad(pos, b, base, source, mem, t2, offArg+t1Size, offStore+t1Size)
457 storeArgOrLoad = func(pos src.XPos, b *Block, base, source, mem *Value, t *types.Type, offset int64) *Value {
459 fmt.Printf("\tstoreArgOrLoad(%s; %s; %s; %s; %d)\n", base.LongString(), source.LongString(), mem.String(), t.String(), offset)
464 return storeArgOrLoad(pos, b, base, source.Args[0], mem, t, offset)
467 ret := decomposeArgOrLoad(pos, b, base, source, mem, t, offset, storeOneLoad, storeTwoLoad)
473 ret := decomposeArgOrLoad(pos, b, base, source, mem, t, offset, storeOneArg, storeTwoArg)
478 case OpArrayMake0, OpStructMake0:
481 case OpStructMake1, OpStructMake2, OpStructMake3, OpStructMake4:
482 for i := 0; i < t.NumFields(); i++ {
484 mem = storeArgOrLoad(pos, b, base, source.Args[i], mem, fld.Type, offset+fld.Offset)
485 pos = pos.WithNotStmt()
490 return storeArgOrLoad(pos, b, base, source.Args[0], mem, t.Elem(), offset)
493 tHi, tLo := intPairTypes(t.Kind())
494 mem = storeArgOrLoad(pos, b, base, source.Args[0], mem, tHi, offset+hiOffset)
495 pos = pos.WithNotStmt()
496 return storeArgOrLoad(pos, b, base, source.Args[1], mem, tLo, offset+lowOffset)
504 mem = storeArgOrLoad(pos, b, base, source.Args[0], mem, tPart, offset)
505 pos = pos.WithNotStmt()
506 return storeArgOrLoad(pos, b, base, source.Args[1], mem, tPart, offset+wPart)
509 mem = storeArgOrLoad(pos, b, base, source.Args[0], mem, typ.Uintptr, offset)
510 pos = pos.WithNotStmt()
511 return storeArgOrLoad(pos, b, base, source.Args[1], mem, typ.BytePtr, offset+ptrSize)
514 mem = storeArgOrLoad(pos, b, base, source.Args[0], mem, typ.BytePtr, offset)
515 pos = pos.WithNotStmt()
516 return storeArgOrLoad(pos, b, base, source.Args[1], mem, typ.Int, offset+ptrSize)
519 mem = storeArgOrLoad(pos, b, base, source.Args[0], mem, typ.BytePtr, offset)
520 pos = pos.WithNotStmt()
521 mem = storeArgOrLoad(pos, b, base, source.Args[1], mem, typ.Int, offset+ptrSize)
522 return storeArgOrLoad(pos, b, base, source.Args[2], mem, typ.Int, offset+2*ptrSize)
525 // For nodes that cannot be taken apart -- OpSelectN, other structure selectors.
529 if source.Type != t && t.NumElem() == 1 && elt.Width == t.Width && t.Width == regSize {
530 t = removeTrivialWrapperTypes(t)
531 // it could be a leaf type, but the "leaf" could be complex64 (for example)
532 return storeArgOrLoad(pos, b, base, source, mem, t, offset)
534 for i := int64(0); i < t.NumElem(); i++ {
535 sel := source.Block.NewValue1I(pos, OpArraySelect, elt, i, source)
536 mem = storeArgOrLoad(pos, b, base, sel, mem, elt, offset+i*elt.Width)
537 pos = pos.WithNotStmt()
542 if source.Type != t && t.NumFields() == 1 && t.Field(0).Type.Width == t.Width && t.Width == regSize {
543 // This peculiar test deals with accesses to immediate interface data.
544 // It works okay because everything is the same size.
545 // Example code that triggers this can be found in go/constant/value.go, function ToComplex
546 // v119 (+881) = IData <intVal> v6
547 // v121 (+882) = StaticLECall <floatVal,mem> {AuxCall{"".itof([intVal,0])[floatVal,8]}} [16] v119 v1
548 // This corresponds to the generic rewrite rule "(StructSelect [0] (IData x)) => (IData x)"
549 // Guard against "struct{struct{*foo}}"
550 // Other rewriting phases create minor glitches when they transform IData, for instance the
551 // interface-typed Arg "x" of ToFloat in go/constant/value.go
552 // v6 (858) = Arg <Value> {x} (x[Value], x[Value])
553 // is rewritten by decomposeArgs into
554 // v141 (858) = Arg <uintptr> {x}
555 // v139 (858) = Arg <*uint8> {x} [8]
556 // because of a type case clause on line 862 of go/constant/value.go
559 // v139 is later stored as an intVal == struct{val *big.Int} which naively requires the fields of
560 // of a *uint8, which does not succeed.
561 t = removeTrivialWrapperTypes(t)
562 // it could be a leaf type, but the "leaf" could be complex64 (for example)
563 return storeArgOrLoad(pos, b, base, source, mem, t, offset)
566 for i := 0; i < t.NumFields(); i++ {
568 sel := source.Block.NewValue1I(pos, OpStructSelect, fld.Type, int64(i), source)
569 mem = storeArgOrLoad(pos, b, base, sel, mem, fld.Type, offset+fld.Offset)
570 pos = pos.WithNotStmt()
574 case types.TINT64, types.TUINT64:
575 if t.Width == regSize {
578 tHi, tLo := intPairTypes(t.Kind())
579 sel := source.Block.NewValue1(pos, OpInt64Hi, tHi, source)
580 mem = storeArgOrLoad(pos, b, base, sel, mem, tHi, offset+hiOffset)
581 pos = pos.WithNotStmt()
582 sel = source.Block.NewValue1(pos, OpInt64Lo, tLo, source)
583 return storeArgOrLoad(pos, b, base, sel, mem, tLo, offset+lowOffset)
586 sel := source.Block.NewValue1(pos, OpITab, typ.BytePtr, source)
587 mem = storeArgOrLoad(pos, b, base, sel, mem, typ.BytePtr, offset)
588 pos = pos.WithNotStmt()
589 sel = source.Block.NewValue1(pos, OpIData, typ.BytePtr, source)
590 return storeArgOrLoad(pos, b, base, sel, mem, typ.BytePtr, offset+ptrSize)
593 sel := source.Block.NewValue1(pos, OpStringPtr, typ.BytePtr, source)
594 mem = storeArgOrLoad(pos, b, base, sel, mem, typ.BytePtr, offset)
595 pos = pos.WithNotStmt()
596 sel = source.Block.NewValue1(pos, OpStringLen, typ.Int, source)
597 return storeArgOrLoad(pos, b, base, sel, mem, typ.Int, offset+ptrSize)
600 et := types.NewPtr(t.Elem())
601 sel := source.Block.NewValue1(pos, OpSlicePtr, et, source)
602 mem = storeArgOrLoad(pos, b, base, sel, mem, et, offset)
603 pos = pos.WithNotStmt()
604 sel = source.Block.NewValue1(pos, OpSliceLen, typ.Int, source)
605 mem = storeArgOrLoad(pos, b, base, sel, mem, typ.Int, offset+ptrSize)
606 sel = source.Block.NewValue1(pos, OpSliceCap, typ.Int, source)
607 return storeArgOrLoad(pos, b, base, sel, mem, typ.Int, offset+2*ptrSize)
609 case types.TCOMPLEX64:
610 sel := source.Block.NewValue1(pos, OpComplexReal, typ.Float32, source)
611 mem = storeArgOrLoad(pos, b, base, sel, mem, typ.Float32, offset)
612 pos = pos.WithNotStmt()
613 sel = source.Block.NewValue1(pos, OpComplexImag, typ.Float32, source)
614 return storeArgOrLoad(pos, b, base, sel, mem, typ.Float32, offset+4)
616 case types.TCOMPLEX128:
617 sel := source.Block.NewValue1(pos, OpComplexReal, typ.Float64, source)
618 mem = storeArgOrLoad(pos, b, base, sel, mem, typ.Float64, offset)
619 pos = pos.WithNotStmt()
620 sel = source.Block.NewValue1(pos, OpComplexImag, typ.Float64, source)
621 return storeArgOrLoad(pos, b, base, sel, mem, typ.Float64, offset+8)
624 dst := offsetFrom(base, offset, types.NewPtr(t))
625 x := b.NewValue3A(pos, OpStore, types.TypeMem, t, dst, source, mem)
627 fmt.Printf("\t\tstoreArg returns %s\n", x.LongString())
632 rewriteDereference := func(b *Block, base, a, mem *Value, offset, size int64, typ *types.Type, pos src.XPos) *Value {
634 dst := offsetFrom(base, offset, source.Type)
635 if a.Uses == 1 && a.Block == b {
638 a.Type = types.TypeMem
641 a.SetArgs3(dst, source, mem)
644 mem = b.NewValue3A(pos, OpMove, types.TypeMem, typ, dst, source, mem)
650 // rewriteArgs removes all the Args from a call and converts the call args into appropriate
651 // stores (or later, register movement). Extra args for interface and closure calls are ignored,
653 rewriteArgs := func(v *Value, firstArg int) *Value {
654 // Thread the stores on the memory arg
655 aux := v.Aux.(*AuxCall)
656 pos := v.Pos.WithNotStmt()
659 for i, a := range v.Args {
663 if a == m0 { // mem is last.
666 auxI := int64(i - firstArg)
667 if a.Op == OpDereference {
668 if a.MemoryArg() != m0 {
669 f.Fatalf("Op...LECall and OpDereference have mismatched mem, %s and %s", v.LongString(), a.LongString())
671 // "Dereference" of addressed (probably not-SSA-eligible) value becomes Move
672 // TODO this will be more complicated with registers in the picture.
673 mem = rewriteDereference(v.Block, sp, a, mem, aux.OffsetOfArg(auxI), aux.SizeOfArg(auxI), aux.TypeOfArg(auxI), pos)
676 fmt.Printf("storeArg %s, %v, %d\n", a.LongString(), aux.TypeOfArg(auxI), aux.OffsetOfArg(auxI))
678 mem = storeArgOrLoad(pos, v.Block, sp, a, mem, aux.TypeOfArg(auxI), aux.OffsetOfArg(auxI))
685 // TODO if too slow, whole program iteration can be replaced w/ slices of appropriate values, accumulated in first loop here.
687 // Step 0: rewrite the calls to convert incoming args to stores.
688 for _, b := range f.Blocks {
689 for _, v := range b.Values {
692 mem := rewriteArgs(v, 0)
694 case OpClosureLECall:
697 mem := rewriteArgs(v, 2)
698 v.SetArgs3(code, context, mem)
701 mem := rewriteArgs(v, 1)
702 v.SetArgs2(code, mem)
705 if isBlockMultiValueExit(b) {
706 // Very similar to code in rewriteArgs, but results instead of args.
711 pos := v.Pos.WithNotStmt()
712 for j, a := range v.Args {
717 auxType := aux.TypeOfResult(i)
718 auxBase := b.NewValue2A(v.Pos, OpLocalAddr, types.NewPtr(auxType), aux.results[i].Name, sp, mem)
719 auxOffset := int64(0)
720 auxSize := aux.SizeOfResult(i)
721 if a.Op == OpDereference {
722 // Avoid a self-move, and if one is detected try to remove the already-inserted VarDef for the assignment that won't happen.
723 if dAddr, dMem := a.Args[0], a.Args[1]; dAddr.Op == OpLocalAddr && dAddr.Args[0].Op == OpSP &&
724 dAddr.Args[1] == dMem && dAddr.Aux == aux.results[i].Name {
725 if dMem.Op == OpVarDef && dMem.Aux == dAddr.Aux {
726 dMem.copyOf(dMem.MemoryArg()) // elide the VarDef
730 mem = rewriteDereference(v.Block, auxBase, a, mem, auxOffset, auxSize, auxType, pos)
732 if a.Op == OpLoad && a.Args[0].Op == OpLocalAddr {
734 if addr.MemoryArg() == a.MemoryArg() && addr.Aux == aux.results[i].Name {
738 mem = storeArgOrLoad(v.Pos, b, auxBase, a, mem, aux.TypeOfResult(i), auxOffset)
742 v.reset(OpInvalid) // otherwise it can have a mem operand which will fail check(), even though it is dead.
746 for i, name := range f.Names {
748 if isAlreadyExpandedAggregateType(t) {
749 for j, v := range f.NamedValues[name] {
750 if v.Op == OpSelectN || v.Op == OpArg && isAlreadyExpandedAggregateType(v.Type) {
751 ns := namedSelects[v]
752 namedSelects[v] = append(ns, namedVal{locIndex: i, valIndex: j})
758 // Step 1: any stores of aggregates remaining are believed to be sourced from call results or args.
759 // Decompose those stores into a series of smaller stores, adding selection ops as necessary.
760 for _, b := range f.Blocks {
761 for _, v := range b.Values {
763 t := v.Aux.(*types.Type)
766 iAEATt := isAlreadyExpandedAggregateType(t)
769 // guarding against store immediate struct into interface data field -- store type is *uint8
770 // TODO can this happen recursively?
771 iAEATt = isAlreadyExpandedAggregateType(tSrc)
778 fmt.Printf("Splitting store %s\n", v.LongString())
780 dst, mem := v.Args[0], v.Args[2]
781 mem = storeArgOrLoad(v.Pos, b, dst, source, mem, t, 0)
788 val2Preds := make(map[*Value]int32) // Used to accumulate dependency graph of selection operations for topological ordering.
790 // Step 2: transform or accumulate selection operations for rewrite in topological order.
792 // Aggregate types that have already (in earlier phases) been transformed must be lowered comprehensively to finish
793 // the transformation (user-defined structs and arrays, slices, strings, interfaces, complex, 64-bit on 32-bit architectures),
795 // Any select-for-addressing applied to call results can be transformed directly.
796 for _, b := range f.Blocks {
797 for _, v := range b.Values {
798 // Accumulate chains of selectors for processing in topological order
800 case OpStructSelect, OpArraySelect,
802 OpStringPtr, OpStringLen,
803 OpSlicePtr, OpSliceLen, OpSliceCap,
804 OpComplexReal, OpComplexImag,
805 OpInt64Hi, OpInt64Lo:
808 case OpStructSelect, OpArraySelect, OpSelectN, OpArg:
811 fmt.Printf("v2p[%s] = %d\n", w.LongString(), val2Preds[w])
817 if _, ok := val2Preds[v]; !ok {
820 fmt.Printf("v2p[%s] = %d\n", v.LongString(), val2Preds[v])
825 if !isAlreadyExpandedAggregateType(v.Type) {
828 if _, ok := val2Preds[v]; !ok {
831 fmt.Printf("v2p[%s] = %d\n", v.LongString(), val2Preds[v])
836 // Do these directly, there are no chains of selectors.
839 aux := call.Aux.(*AuxCall)
841 off := offsetFrom(sp, aux.OffsetOfResult(which), pt)
847 // Step 3: Compute topological order of selectors,
848 // then process it in reverse to eliminate duplicates,
849 // then forwards to rewrite selectors.
851 // All chains of selectors end up in same block as the call.
853 // Compilation must be deterministic, so sort after extracting first zeroes from map.
854 // Sorting allows dominators-last order within each batch,
855 // so that the backwards scan for duplicates will most often find copies from dominating blocks (it is best-effort).
856 var toProcess []*Value
857 less := func(i, j int) bool {
858 vi, vj := toProcess[i], toProcess[j]
859 bi, bj := vi.Block, vj.Block
863 return sdom.domorder(bi) > sdom.domorder(bj) // reverse the order to put dominators last.
866 // Accumulate order in allOrdered
867 var allOrdered []*Value
868 for v, n := range val2Preds {
870 allOrdered = append(allOrdered, v)
873 last := 0 // allOrdered[0:last] has been top-sorted and processed
874 for len(val2Preds) > 0 {
875 toProcess = allOrdered[last:]
876 last = len(allOrdered)
877 sort.SliceStable(toProcess, less)
878 for _, v := range toProcess {
881 continue // no Args[0], hence done.
884 n, ok := val2Preds[w]
889 allOrdered = append(allOrdered, w)
897 common = make(map[selKey]*Value)
898 // Rewrite duplicate selectors as copies where possible.
899 for i := len(allOrdered) - 1; i >= 0; i-- {
913 continue // handled elsewhere, not an indexable result
919 if w.Type.Kind() == types.TSTRUCT {
920 offset = w.Type.FieldOff(int(v.AuxInt))
921 } else { // Immediate interface data artifact, offset is zero.
922 f.Fatalf("Expand calls interface data problem, func %s, v=%s, w=%s\n", f.Name, v.LongString(), w.LongString())
925 offset = size * v.AuxInt
927 offset = w.Aux.(*AuxCall).OffsetOfResult(v.AuxInt)
932 case OpStringLen, OpSliceLen, OpIData:
939 sk := selKey{from: w, size: size, offset: offset, typ: typ}
943 } else if sdom.IsAncestorEq(dupe.Block, v.Block) {
946 // Because values are processed in dominator order, the old common[s] will never dominate after a miss is seen.
947 // Installing the new value might match some future values.
952 // Indices of entries in f.Names that need to be deleted.
953 var toDelete []namedVal
955 // Rewrite selectors.
956 for i, v := range allOrdered {
959 fmt.Printf("allOrdered[%d] = b%d, %s, uses=%d\n", i, b.ID, v.LongString(), v.Uses)
968 locs := rewriteSelect(v, v, 0)
969 // Install new names.
970 if v.Type.IsMemory() {
973 // Leaf types may have debug locations
974 if !isAlreadyExpandedAggregateType(v.Type) {
975 for _, l := range locs {
976 f.NamedValues[l] = append(f.NamedValues[l], v)
978 f.Names = append(f.Names, locs...)
981 // Not-leaf types that had debug locations need to lose them.
982 if ns, ok := namedSelects[v]; ok {
983 toDelete = append(toDelete, ns...)
987 deleteNamedVals(f, toDelete)
989 // Step 4: rewrite the calls themselves, correcting the type
990 for _, b := range f.Blocks {
991 for _, v := range b.Values {
995 v.Type = types.TypeMem
996 case OpClosureLECall:
998 v.Type = types.TypeMem
1001 v.Type = types.TypeMem
1006 // Step 5: elide any copies introduced.
1007 for _, b := range f.Blocks {
1008 for _, v := range b.Values {
1009 for i, a := range v.Args {