...

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, Select1,
     8  // and SelectN 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  		which int
    18  	}]*Value)
    19  	for _, b := range f.Blocks {
    20  		for _, selector := range b.Values {
    21  			// Key fields for de-duplication
    22  			var tuple *Value
    23  			idx := 0
    24  			switch selector.Op {
    25  			default:
    26  				continue
    27  			case OpSelect1:
    28  				idx = 1
    29  				fallthrough
    30  			case OpSelect0:
    31  				tuple = selector.Args[0]
    32  				if !tuple.Type.IsTuple() {
    33  					f.Fatalf("arg of tuple selector %s is not a tuple: %s", selector.String(), tuple.LongString())
    34  				}
    35  			case OpSelectN:
    36  				tuple = selector.Args[0]
    37  				idx = int(selector.AuxInt)
    38  				if !tuple.Type.IsResults() {
    39  					f.Fatalf("arg of result selector %s is not a results: %s", selector.String(), tuple.LongString())
    40  				}
    41  			}
    42  
    43  			// If there is a pre-existing selector in the target block then
    44  			// use that. Do this even if the selector is already in the
    45  			// target block to avoid duplicate tuple selectors.
    46  			key := struct {
    47  				id    ID
    48  				which int
    49  			}{tuple.ID, idx}
    50  			if t := selectors[key]; t != nil {
    51  				if selector != t {
    52  					selector.copyOf(t)
    53  				}
    54  				continue
    55  			}
    56  
    57  			// If the selector is in the wrong block copy it into the target
    58  			// block.
    59  			if selector.Block != tuple.Block {
    60  				t := selector.copyInto(tuple.Block)
    61  				selector.copyOf(t)
    62  				selectors[key] = t
    63  				continue
    64  			}
    65  
    66  			// The selector is in the target block. Add it to the map so it
    67  			// cannot be duplicated.
    68  			selectors[key] = selector
    69  		}
    70  	}
    71  }
    72  

View as plain text