github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/compile/internal/ssa/value.go (about)

     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 (
     8  	"cmd/compile/internal/types"
     9  	"cmd/internal/src"
    10  	"fmt"
    11  	"math"
    12  	"sort"
    13  	"strings"
    14  )
    15  
    16  // A Value represents a value in the SSA representation of the program.
    17  // The ID and Type fields must not be modified. The remainder may be modified
    18  // if they preserve the value of the Value (e.g. changing a (mul 2 x) to an (add x x)).
    19  type Value struct {
    20  	// A unique identifier for the value. For performance we allocate these IDs
    21  	// densely starting at 1.  There is no guarantee that there won't be occasional holes, though.
    22  	ID ID
    23  
    24  	// The operation that computes this value. See op.go.
    25  	Op Op
    26  
    27  	// The type of this value. Normally this will be a Go type, but there
    28  	// are a few other pseudo-types, see type.go.
    29  	Type *types.Type
    30  
    31  	// Auxiliary info for this value. The type of this information depends on the opcode and type.
    32  	// AuxInt is used for integer values, Aux is used for other values.
    33  	// Floats are stored in AuxInt using math.Float64bits(f).
    34  	AuxInt int64
    35  	Aux    interface{}
    36  
    37  	// Arguments of this value
    38  	Args []*Value
    39  
    40  	// Containing basic block
    41  	Block *Block
    42  
    43  	// Source position
    44  	Pos src.XPos
    45  
    46  	// Use count. Each appearance in Value.Args and Block.Control counts once.
    47  	Uses int32
    48  
    49  	// Storage for the first three args
    50  	argstorage [3]*Value
    51  }
    52  
    53  // Examples:
    54  // Opcode          aux   args
    55  //  OpAdd          nil      2
    56  //  OpConst     string      0    string constant
    57  //  OpConst      int64      0    int64 constant
    58  //  OpAddcq      int64      1    amd64 op: v = arg[0] + constant
    59  
    60  // short form print. Just v#.
    61  func (v *Value) String() string {
    62  	if v == nil {
    63  		return "nil" // should never happen, but not panicking helps with debugging
    64  	}
    65  	return fmt.Sprintf("v%d", v.ID)
    66  }
    67  
    68  func (v *Value) AuxInt8() int8 {
    69  	if opcodeTable[v.Op].auxType != auxInt8 {
    70  		v.Fatalf("op %s doesn't have an int8 aux field", v.Op)
    71  	}
    72  	return int8(v.AuxInt)
    73  }
    74  
    75  func (v *Value) AuxInt16() int16 {
    76  	if opcodeTable[v.Op].auxType != auxInt16 {
    77  		v.Fatalf("op %s doesn't have an int16 aux field", v.Op)
    78  	}
    79  	return int16(v.AuxInt)
    80  }
    81  
    82  func (v *Value) AuxInt32() int32 {
    83  	if opcodeTable[v.Op].auxType != auxInt32 {
    84  		v.Fatalf("op %s doesn't have an int32 aux field", v.Op)
    85  	}
    86  	return int32(v.AuxInt)
    87  }
    88  
    89  func (v *Value) AuxFloat() float64 {
    90  	if opcodeTable[v.Op].auxType != auxFloat32 && opcodeTable[v.Op].auxType != auxFloat64 {
    91  		v.Fatalf("op %s doesn't have a float aux field", v.Op)
    92  	}
    93  	return math.Float64frombits(uint64(v.AuxInt))
    94  }
    95  func (v *Value) AuxValAndOff() ValAndOff {
    96  	if opcodeTable[v.Op].auxType != auxSymValAndOff {
    97  		v.Fatalf("op %s doesn't have a ValAndOff aux field", v.Op)
    98  	}
    99  	return ValAndOff(v.AuxInt)
   100  }
   101  
   102  // long form print.  v# = opcode <type> [aux] args [: reg] (names)
   103  func (v *Value) LongString() string {
   104  	s := fmt.Sprintf("v%d = %s", v.ID, v.Op)
   105  	s += " <" + v.Type.String() + ">"
   106  	s += v.auxString()
   107  	for _, a := range v.Args {
   108  		s += fmt.Sprintf(" %v", a)
   109  	}
   110  	r := v.Block.Func.RegAlloc
   111  	if int(v.ID) < len(r) && r[v.ID] != nil {
   112  		s += " : " + r[v.ID].String()
   113  	}
   114  	var names []string
   115  	for name, values := range v.Block.Func.NamedValues {
   116  		for _, value := range values {
   117  			if value == v {
   118  				names = append(names, name.String())
   119  				break // drop duplicates.
   120  			}
   121  		}
   122  	}
   123  	if len(names) != 0 {
   124  		sort.Strings(names) // Otherwise a source of variation in debugging output.
   125  		s += " (" + strings.Join(names, ", ") + ")"
   126  	}
   127  	return s
   128  }
   129  
   130  func (v *Value) auxString() string {
   131  	switch opcodeTable[v.Op].auxType {
   132  	case auxBool:
   133  		if v.AuxInt == 0 {
   134  			return " [false]"
   135  		} else {
   136  			return " [true]"
   137  		}
   138  	case auxInt8:
   139  		return fmt.Sprintf(" [%d]", v.AuxInt8())
   140  	case auxInt16:
   141  		return fmt.Sprintf(" [%d]", v.AuxInt16())
   142  	case auxInt32:
   143  		return fmt.Sprintf(" [%d]", v.AuxInt32())
   144  	case auxInt64, auxInt128:
   145  		return fmt.Sprintf(" [%d]", v.AuxInt)
   146  	case auxFloat32, auxFloat64:
   147  		return fmt.Sprintf(" [%g]", v.AuxFloat())
   148  	case auxString:
   149  		return fmt.Sprintf(" {%q}", v.Aux)
   150  	case auxSym, auxTyp:
   151  		if v.Aux != nil {
   152  			return fmt.Sprintf(" {%v}", v.Aux)
   153  		}
   154  	case auxSymOff, auxSymInt32, auxTypSize:
   155  		s := ""
   156  		if v.Aux != nil {
   157  			s = fmt.Sprintf(" {%v}", v.Aux)
   158  		}
   159  		if v.AuxInt != 0 {
   160  			s += fmt.Sprintf(" [%v]", v.AuxInt)
   161  		}
   162  		return s
   163  	case auxSymValAndOff:
   164  		s := ""
   165  		if v.Aux != nil {
   166  			s = fmt.Sprintf(" {%v}", v.Aux)
   167  		}
   168  		return s + fmt.Sprintf(" [%s]", v.AuxValAndOff())
   169  	case auxCCop:
   170  		return fmt.Sprintf(" {%s}", v.Aux.(Op))
   171  	}
   172  	return ""
   173  }
   174  
   175  func (v *Value) AddArg(w *Value) {
   176  	if v.Args == nil {
   177  		v.resetArgs() // use argstorage
   178  	}
   179  	v.Args = append(v.Args, w)
   180  	w.Uses++
   181  }
   182  func (v *Value) AddArgs(a ...*Value) {
   183  	if v.Args == nil {
   184  		v.resetArgs() // use argstorage
   185  	}
   186  	v.Args = append(v.Args, a...)
   187  	for _, x := range a {
   188  		x.Uses++
   189  	}
   190  }
   191  func (v *Value) SetArg(i int, w *Value) {
   192  	v.Args[i].Uses--
   193  	v.Args[i] = w
   194  	w.Uses++
   195  }
   196  func (v *Value) RemoveArg(i int) {
   197  	v.Args[i].Uses--
   198  	copy(v.Args[i:], v.Args[i+1:])
   199  	v.Args[len(v.Args)-1] = nil // aid GC
   200  	v.Args = v.Args[:len(v.Args)-1]
   201  }
   202  func (v *Value) SetArgs1(a *Value) {
   203  	v.resetArgs()
   204  	v.AddArg(a)
   205  }
   206  func (v *Value) SetArgs2(a *Value, b *Value) {
   207  	v.resetArgs()
   208  	v.AddArg(a)
   209  	v.AddArg(b)
   210  }
   211  
   212  func (v *Value) resetArgs() {
   213  	for _, a := range v.Args {
   214  		a.Uses--
   215  	}
   216  	v.argstorage[0] = nil
   217  	v.argstorage[1] = nil
   218  	v.argstorage[2] = nil
   219  	v.Args = v.argstorage[:0]
   220  }
   221  
   222  func (v *Value) reset(op Op) {
   223  	v.Op = op
   224  	v.resetArgs()
   225  	v.AuxInt = 0
   226  	v.Aux = nil
   227  }
   228  
   229  // copyInto makes a new value identical to v and adds it to the end of b.
   230  func (v *Value) copyInto(b *Block) *Value {
   231  	c := b.NewValue0(v.Pos, v.Op, v.Type) // Lose the position, this causes line number churn otherwise.
   232  	c.Aux = v.Aux
   233  	c.AuxInt = v.AuxInt
   234  	c.AddArgs(v.Args...)
   235  	for _, a := range v.Args {
   236  		if a.Type.IsMemory() {
   237  			v.Fatalf("can't move a value with a memory arg %s", v.LongString())
   238  		}
   239  	}
   240  	return c
   241  }
   242  
   243  // copyIntoNoXPos makes a new value identical to v and adds it to the end of b.
   244  // The copied value receives no source code position to avoid confusing changes
   245  // in debugger information (the intended user is the register allocator).
   246  func (v *Value) copyIntoNoXPos(b *Block) *Value {
   247  	return v.copyIntoWithXPos(b, src.NoXPos)
   248  }
   249  
   250  // copyIntoWithXPos makes a new value identical to v and adds it to the end of b.
   251  // The supplied position is used as the position of the new value.
   252  func (v *Value) copyIntoWithXPos(b *Block, pos src.XPos) *Value {
   253  	c := b.NewValue0(pos, v.Op, v.Type)
   254  	c.Aux = v.Aux
   255  	c.AuxInt = v.AuxInt
   256  	c.AddArgs(v.Args...)
   257  	for _, a := range v.Args {
   258  		if a.Type.IsMemory() {
   259  			v.Fatalf("can't move a value with a memory arg %s", v.LongString())
   260  		}
   261  	}
   262  	return c
   263  }
   264  
   265  func (v *Value) Logf(msg string, args ...interface{}) { v.Block.Logf(msg, args...) }
   266  func (v *Value) Log() bool                            { return v.Block.Log() }
   267  func (v *Value) Fatalf(msg string, args ...interface{}) {
   268  	v.Block.Func.fe.Fatalf(v.Pos, msg, args...)
   269  }
   270  
   271  // isGenericIntConst returns whether v is a generic integer constant.
   272  func (v *Value) isGenericIntConst() bool {
   273  	return v != nil && (v.Op == OpConst64 || v.Op == OpConst32 || v.Op == OpConst16 || v.Op == OpConst8)
   274  }
   275  
   276  // Reg returns the register assigned to v, in cmd/internal/obj/$ARCH numbering.
   277  func (v *Value) Reg() int16 {
   278  	reg := v.Block.Func.RegAlloc[v.ID]
   279  	if reg == nil {
   280  		v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   281  	}
   282  	return reg.(*Register).objNum
   283  }
   284  
   285  // Reg0 returns the register assigned to the first output of v, in cmd/internal/obj/$ARCH numbering.
   286  func (v *Value) Reg0() int16 {
   287  	reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[0]
   288  	if reg == nil {
   289  		v.Fatalf("nil first register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   290  	}
   291  	return reg.(*Register).objNum
   292  }
   293  
   294  // Reg1 returns the register assigned to the second output of v, in cmd/internal/obj/$ARCH numbering.
   295  func (v *Value) Reg1() int16 {
   296  	reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[1]
   297  	if reg == nil {
   298  		v.Fatalf("nil second register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   299  	}
   300  	return reg.(*Register).objNum
   301  }
   302  
   303  func (v *Value) RegName() string {
   304  	reg := v.Block.Func.RegAlloc[v.ID]
   305  	if reg == nil {
   306  		v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   307  	}
   308  	return reg.(*Register).name
   309  }
   310  
   311  // MemoryArg returns the memory argument for the Value.
   312  // The returned value, if non-nil, will be memory-typed (or a tuple with a memory-typed second part).
   313  // Otherwise, nil is returned.
   314  func (v *Value) MemoryArg() *Value {
   315  	if v.Op == OpPhi {
   316  		v.Fatalf("MemoryArg on Phi")
   317  	}
   318  	na := len(v.Args)
   319  	if na == 0 {
   320  		return nil
   321  	}
   322  	if m := v.Args[na-1]; m.Type.IsMemory() {
   323  		return m
   324  	}
   325  	return nil
   326  }
   327  
   328  // LackingPos indicates whether v is a value that is unlikely to have a correct
   329  // position assigned to it.  Ignoring such values leads to more user-friendly positions
   330  // assigned to nearby values and the blocks containing them.
   331  func (v *Value) LackingPos() bool {
   332  	// The exact definition of LackingPos is somewhat heuristically defined and may change
   333  	// in the future, for example if some of these operations are generated more carefully
   334  	// with respect to their source position.
   335  	return v.Op == OpVarDef || v.Op == OpVarKill || v.Op == OpVarLive || v.Op == OpPhi ||
   336  		(v.Op == OpFwdRef || v.Op == OpCopy) && v.Type == types.TypeMem
   337  }