Source file src/cmd/compile/internal/ssa/copyelim.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 ssa
     6  
     7  import "cmd/compile/internal/ssa/ssaop"
     8  
     9  // PhiElimValue tries to convert the phi v to a copy.
    10  func PhiElimValue(v *Value) bool {
    11  	if v.Op != ssaop.OpPhi {
    12  		return false
    13  	}
    14  
    15  	// If there are two distinct args of v which
    16  	// are not v itself, then the phi must remain.
    17  	// Otherwise, we can replace it with a copy.
    18  	var w *Value
    19  	for _, x := range v.Args {
    20  		if x == v {
    21  			continue
    22  		}
    23  		if x == w {
    24  			continue
    25  		}
    26  		if w != nil {
    27  			return false
    28  		}
    29  		w = x
    30  	}
    31  
    32  	if w == nil {
    33  		// v references only itself. It must be in
    34  		// a dead code loop. Don't bother modifying it.
    35  		return false
    36  	}
    37  	v.Op = ssaop.OpCopy
    38  	v.SetArgs1(w)
    39  	f := v.Block.Func
    40  	if f.Pass.Debug > 0 {
    41  		f.Warnl(v.Pos, "eliminated phi")
    42  	}
    43  	return true
    44  }
    45  

View as plain text