github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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/obj"
    10  	"cmd/internal/src"
    11  	"fmt"
    12  	"math"
    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  		s += " (" + strings.Join(names, ", ") + ")"
   125  	}
   126  	return s
   127  }
   128  
   129  func (v *Value) auxString() string {
   130  	switch opcodeTable[v.Op].auxType {
   131  	case auxBool:
   132  		if v.AuxInt == 0 {
   133  			return " [false]"
   134  		} else {
   135  			return " [true]"
   136  		}
   137  	case auxInt8:
   138  		return fmt.Sprintf(" [%d]", v.AuxInt8())
   139  	case auxInt16:
   140  		return fmt.Sprintf(" [%d]", v.AuxInt16())
   141  	case auxInt32:
   142  		return fmt.Sprintf(" [%d]", v.AuxInt32())
   143  	case auxInt64, auxInt128:
   144  		return fmt.Sprintf(" [%d]", v.AuxInt)
   145  	case auxFloat32, auxFloat64:
   146  		return fmt.Sprintf(" [%g]", v.AuxFloat())
   147  	case auxString:
   148  		return fmt.Sprintf(" {%q}", v.Aux)
   149  	case auxSym, auxTyp:
   150  		if v.Aux != nil {
   151  			return fmt.Sprintf(" {%v}", v.Aux)
   152  		}
   153  	case auxSymOff, auxSymInt32, auxTypSize:
   154  		s := ""
   155  		if v.Aux != nil {
   156  			s = fmt.Sprintf(" {%v}", v.Aux)
   157  		}
   158  		if v.AuxInt != 0 {
   159  			s += fmt.Sprintf(" [%v]", v.AuxInt)
   160  		}
   161  		return s
   162  	case auxSymValAndOff:
   163  		s := ""
   164  		if v.Aux != nil {
   165  			s = fmt.Sprintf(" {%v}", v.Aux)
   166  		}
   167  		return s + fmt.Sprintf(" [%s]", v.AuxValAndOff())
   168  	}
   169  	return ""
   170  }
   171  
   172  func (v *Value) AddArg(w *Value) {
   173  	if v.Args == nil {
   174  		v.resetArgs() // use argstorage
   175  	}
   176  	v.Args = append(v.Args, w)
   177  	w.Uses++
   178  }
   179  func (v *Value) AddArgs(a ...*Value) {
   180  	if v.Args == nil {
   181  		v.resetArgs() // use argstorage
   182  	}
   183  	v.Args = append(v.Args, a...)
   184  	for _, x := range a {
   185  		x.Uses++
   186  	}
   187  }
   188  func (v *Value) SetArg(i int, w *Value) {
   189  	v.Args[i].Uses--
   190  	v.Args[i] = w
   191  	w.Uses++
   192  }
   193  func (v *Value) RemoveArg(i int) {
   194  	v.Args[i].Uses--
   195  	copy(v.Args[i:], v.Args[i+1:])
   196  	v.Args[len(v.Args)-1] = nil // aid GC
   197  	v.Args = v.Args[:len(v.Args)-1]
   198  }
   199  func (v *Value) SetArgs1(a *Value) {
   200  	v.resetArgs()
   201  	v.AddArg(a)
   202  }
   203  func (v *Value) SetArgs2(a *Value, b *Value) {
   204  	v.resetArgs()
   205  	v.AddArg(a)
   206  	v.AddArg(b)
   207  }
   208  
   209  func (v *Value) resetArgs() {
   210  	for _, a := range v.Args {
   211  		a.Uses--
   212  	}
   213  	v.argstorage[0] = nil
   214  	v.argstorage[1] = nil
   215  	v.argstorage[2] = nil
   216  	v.Args = v.argstorage[:0]
   217  }
   218  
   219  func (v *Value) reset(op Op) {
   220  	v.Op = op
   221  	v.resetArgs()
   222  	v.AuxInt = 0
   223  	v.Aux = nil
   224  }
   225  
   226  // copyInto makes a new value identical to v and adds it to the end of b.
   227  func (v *Value) copyInto(b *Block) *Value {
   228  	c := b.NewValue0(v.Pos, v.Op, v.Type) // Lose the position, this causes line number churn otherwise.
   229  	c.Aux = v.Aux
   230  	c.AuxInt = v.AuxInt
   231  	c.AddArgs(v.Args...)
   232  	for _, a := range v.Args {
   233  		if a.Type.IsMemory() {
   234  			v.Fatalf("can't move a value with a memory arg %s", v.LongString())
   235  		}
   236  	}
   237  	return c
   238  }
   239  
   240  // copyIntoNoXPos makes a new value identical to v and adds it to the end of b.
   241  // The copied value receives no source code position to avoid confusing changes
   242  // in debugger information (the intended user is the register allocator).
   243  func (v *Value) copyIntoNoXPos(b *Block) *Value {
   244  	c := b.NewValue0(src.NoXPos, v.Op, v.Type) // Lose the position, this causes line number churn otherwise.
   245  	c.Aux = v.Aux
   246  	c.AuxInt = v.AuxInt
   247  	c.AddArgs(v.Args...)
   248  	for _, a := range v.Args {
   249  		if a.Type.IsMemory() {
   250  			v.Fatalf("can't move a value with a memory arg %s", v.LongString())
   251  		}
   252  	}
   253  	return c
   254  }
   255  
   256  func (v *Value) Logf(msg string, args ...interface{}) { v.Block.Logf(msg, args...) }
   257  func (v *Value) Log() bool                            { return v.Block.Log() }
   258  func (v *Value) Fatalf(msg string, args ...interface{}) {
   259  	v.Block.Func.fe.Fatalf(v.Pos, msg, args...)
   260  }
   261  
   262  // isGenericIntConst returns whether v is a generic integer constant.
   263  func (v *Value) isGenericIntConst() bool {
   264  	return v != nil && (v.Op == OpConst64 || v.Op == OpConst32 || v.Op == OpConst16 || v.Op == OpConst8)
   265  }
   266  
   267  // ExternSymbol is an aux value that encodes a variable's
   268  // constant offset from the static base pointer.
   269  type ExternSymbol struct {
   270  	Sym *obj.LSym
   271  	// Note: the offset for an external symbol is not
   272  	// calculated until link time.
   273  }
   274  
   275  // ArgSymbol is an aux value that encodes an argument or result
   276  // variable's constant offset from FP (FP = SP + framesize).
   277  type ArgSymbol struct {
   278  	Node GCNode // A *gc.Node referring to the argument/result variable.
   279  }
   280  
   281  // AutoSymbol is an aux value that encodes a local variable's
   282  // constant offset from SP.
   283  type AutoSymbol struct {
   284  	Node GCNode // A *gc.Node referring to a local (auto) variable.
   285  }
   286  
   287  func (s *ExternSymbol) String() string {
   288  	return s.Sym.String()
   289  }
   290  
   291  func (s *ArgSymbol) String() string {
   292  	return s.Node.String()
   293  }
   294  
   295  func (s *AutoSymbol) String() string {
   296  	return s.Node.String()
   297  }
   298  
   299  // Reg returns the register assigned to v, in cmd/internal/obj/$ARCH numbering.
   300  func (v *Value) Reg() int16 {
   301  	reg := v.Block.Func.RegAlloc[v.ID]
   302  	if reg == nil {
   303  		v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   304  	}
   305  	return reg.(*Register).objNum
   306  }
   307  
   308  // Reg0 returns the register assigned to the first output of v, in cmd/internal/obj/$ARCH numbering.
   309  func (v *Value) Reg0() int16 {
   310  	reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[0]
   311  	if reg == nil {
   312  		v.Fatalf("nil first register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   313  	}
   314  	return reg.(*Register).objNum
   315  }
   316  
   317  // Reg1 returns the register assigned to the second output of v, in cmd/internal/obj/$ARCH numbering.
   318  func (v *Value) Reg1() int16 {
   319  	reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[1]
   320  	if reg == nil {
   321  		v.Fatalf("nil second register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   322  	}
   323  	return reg.(*Register).objNum
   324  }
   325  
   326  func (v *Value) RegName() string {
   327  	reg := v.Block.Func.RegAlloc[v.ID]
   328  	if reg == nil {
   329  		v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
   330  	}
   331  	return reg.(*Register).name
   332  }
   333  
   334  // MemoryArg returns the memory argument for the Value.
   335  // The returned value, if non-nil, will be memory-typed (or a tuple with a memory-typed second part).
   336  // Otherwise, nil is returned.
   337  func (v *Value) MemoryArg() *Value {
   338  	if v.Op == OpPhi {
   339  		v.Fatalf("MemoryArg on Phi")
   340  	}
   341  	na := len(v.Args)
   342  	if na == 0 {
   343  		return nil
   344  	}
   345  	if m := v.Args[na-1]; m.Type.IsMemory() {
   346  		return m
   347  	}
   348  	return nil
   349  }