Source file src/cmd/compile/internal/ssa/tuple.go
Documentation: cmd/compile/internal/ssa
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. 4 5 package ssa 6 7 // tightenTupleSelectors ensures that tuple selectors (Select0 and 8 // Select1 ops) are in the same block as their tuple generator. The 9 // function also ensures that there are no duplicate tuple selectors. 10 // These properties are expected by the scheduler but may not have 11 // been maintained by the optimization pipeline up to this point. 12 // 13 // See issues 16741 and 39472. 14 func tightenTupleSelectors(f *Func) { 15 selectors := make(map[struct { 16 id ID 17 op Op 18 }]*Value) 19 for _, b := range f.Blocks { 20 for _, selector := range b.Values { 21 if selector.Op != OpSelect0 && selector.Op != OpSelect1 { 22 continue 23 } 24 25 // Get the tuple generator to use as a key for de-duplication. 26 tuple := selector.Args[0] 27 if !tuple.Type.IsTuple() { 28 f.Fatalf("arg of tuple selector %s is not a tuple: %s", selector.String(), tuple.LongString()) 29 } 30 31 // If there is a pre-existing selector in the target block then 32 // use that. Do this even if the selector is already in the 33 // target block to avoid duplicate tuple selectors. 34 key := struct { 35 id ID 36 op Op 37 }{tuple.ID, selector.Op} 38 if t := selectors[key]; t != nil { 39 if selector != t { 40 selector.copyOf(t) 41 } 42 continue 43 } 44 45 // If the selector is in the wrong block copy it into the target 46 // block. 47 if selector.Block != tuple.Block { 48 t := selector.copyInto(tuple.Block) 49 selector.copyOf(t) 50 selectors[key] = t 51 continue 52 } 53 54 // The selector is in the target block. Add it to the map so it 55 // cannot be duplicated. 56 selectors[key] = selector 57 } 58 } 59 } 60