blob: 832ed8d320dd6141fe7fddb129036be1eeb5313f [file] [log] [blame]
Keith Randallf52b2342015-03-03 13:38:14 -08001// 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
5package ssa
6
Todd Neal19447a62015-09-04 06:33:56 -05007import (
Josh Bleecher Snyder46b88c92017-04-28 14:12:28 -07008 "cmd/compile/internal/types"
Robert Griesemer24597c02016-12-06 17:08:06 -08009 "cmd/internal/src"
Todd Neal19447a62015-09-04 06:33:56 -050010 "fmt"
11 "math"
David Chasebd41c672017-11-20 16:47:08 -050012 "sort"
Heschi Kreinick4c54a042017-07-21 18:30:19 -040013 "strings"
Todd Neal19447a62015-09-04 06:33:56 -050014)
Keith Randallf52b2342015-03-03 13:38:14 -080015
16// A Value represents a value in the SSA representation of the program.
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000017// The ID and Type fields must not be modified. The remainder may be modified
Keith Randallf52b2342015-03-03 13:38:14 -080018// if they preserve the value of the Value (e.g. changing a (mul 2 x) to an (add x x)).
19type Value struct {
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000020 // A unique identifier for the value. For performance we allocate these IDs
Keith Randall0b46b422015-08-11 12:51:33 -070021 // densely starting at 1. There is no guarantee that there won't be occasional holes, though.
Keith Randallf52b2342015-03-03 13:38:14 -080022 ID ID
23
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000024 // The operation that computes this value. See op.go.
Keith Randallf52b2342015-03-03 13:38:14 -080025 Op Op
26
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000027 // The type of this value. Normally this will be a Go type, but there
Keith Randallf52b2342015-03-03 13:38:14 -080028 // are a few other pseudo-types, see type.go.
Josh Bleecher Snyder46b88c92017-04-28 14:12:28 -070029 Type *types.Type
Keith Randallf52b2342015-03-03 13:38:14 -080030
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000031 // Auxiliary info for this value. The type of this information depends on the opcode and type.
Keith Randall8f22b522015-06-11 21:29:25 -070032 // AuxInt is used for integer values, Aux is used for other values.
Kevin Burke8e24a982016-08-20 22:05:47 -070033 // Floats are stored in AuxInt using math.Float64bits(f).
Keith Randall8f22b522015-06-11 21:29:25 -070034 AuxInt int64
35 Aux interface{}
Keith Randallf52b2342015-03-03 13:38:14 -080036
37 // Arguments of this value
38 Args []*Value
39
40 // Containing basic block
41 Block *Block
42
Robert Griesemercfd17f52016-12-07 18:14:35 -080043 // Source position
Robert Griesemer472c7922016-12-15 17:17:01 -080044 Pos src.XPos
Michael Matloob81ccf502015-05-30 01:03:06 -040045
Keith Randall56e0ecc2016-03-15 20:45:50 -070046 // Use count. Each appearance in Value.Args and Block.Control counts once.
47 Uses int32
48
Josh Bleecher Snyderf3a29f12016-03-06 21:22:11 -080049 // Storage for the first three args
50 argstorage [3]*Value
Keith Randallf52b2342015-03-03 13:38:14 -080051}
52
53// Examples:
54// Opcode aux args
55// OpAdd nil 2
Keith Randall2c9b4912015-03-26 10:49:03 -070056// OpConst string 0 string constant
57// OpConst int64 0 int64 constant
Keith Randallf52b2342015-03-03 13:38:14 -080058// OpAddcq int64 1 amd64 op: v = arg[0] + constant
59
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000060// short form print. Just v#.
Keith Randallf52b2342015-03-03 13:38:14 -080061func (v *Value) String() string {
Josh Bleecher Snyder220e7052015-08-22 13:30:45 -070062 if v == nil {
63 return "nil" // should never happen, but not panicking helps with debugging
64 }
Keith Randallf52b2342015-03-03 13:38:14 -080065 return fmt.Sprintf("v%d", v.ID)
66}
67
Keith Randall16b1fce2016-01-31 11:39:39 -080068func (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
75func (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
82func (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}
Todd Nealc17b6b42016-02-19 16:58:21 -060088
Keith Randall16b1fce2016-01-31 11:39:39 -080089func (v *Value) AuxFloat() float64 {
Todd Nealf6ceed22016-03-11 19:36:54 -060090 if opcodeTable[v.Op].auxType != auxFloat32 && opcodeTable[v.Op].auxType != auxFloat64 {
Keith Randall16b1fce2016-01-31 11:39:39 -080091 v.Fatalf("op %s doesn't have a float aux field", v.Op)
92 }
93 return math.Float64frombits(uint64(v.AuxInt))
94}
95func (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
Heschi Kreinick4c54a042017-07-21 18:30:19 -0400102// long form print. v# = opcode <type> [aux] args [: reg] (names)
Keith Randallf52b2342015-03-03 13:38:14 -0800103func (v *Value) LongString() string {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700104 s := fmt.Sprintf("v%d = %s", v.ID, v.Op)
Keith Randallf52b2342015-03-03 13:38:14 -0800105 s += " <" + v.Type.String() + ">"
Frits van Bommelb13b2492016-04-30 11:13:29 +0200106 s += v.auxString()
Keith Randallf52b2342015-03-03 13:38:14 -0800107 for _, a := range v.Args {
108 s += fmt.Sprintf(" %v", a)
109 }
110 r := v.Block.Func.RegAlloc
Keith Randall0b46b422015-08-11 12:51:33 -0700111 if int(v.ID) < len(r) && r[v.ID] != nil {
Keith Randallbf4d8d32017-08-17 12:23:34 -0700112 s += " : " + r[v.ID].String()
Keith Randallf52b2342015-03-03 13:38:14 -0800113 }
Heschi Kreinick4c54a042017-07-21 18:30:19 -0400114 var names []string
115 for name, values := range v.Block.Func.NamedValues {
116 for _, value := range values {
117 if value == v {
Keith Randallbf4d8d32017-08-17 12:23:34 -0700118 names = append(names, name.String())
Heschi Kreinick4c54a042017-07-21 18:30:19 -0400119 break // drop duplicates.
120 }
121 }
122 }
123 if len(names) != 0 {
David Chasebd41c672017-11-20 16:47:08 -0500124 sort.Strings(names) // Otherwise a source of variation in debugging output.
Heschi Kreinick4c54a042017-07-21 18:30:19 -0400125 s += " (" + strings.Join(names, ", ") + ")"
126 }
Keith Randallf52b2342015-03-03 13:38:14 -0800127 return s
128}
129
Frits van Bommelb13b2492016-04-30 11:13:29 +0200130func (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)
Cherry Zhangc8f38b32017-03-13 21:51:08 -0400150 case auxSym, auxTyp:
Frits van Bommelb13b2492016-04-30 11:13:29 +0200151 if v.Aux != nil {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700152 return fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200153 }
Cherry Zhangc8f38b32017-03-13 21:51:08 -0400154 case auxSymOff, auxSymInt32, auxTypSize:
Frits van Bommelb13b2492016-04-30 11:13:29 +0200155 s := ""
156 if v.Aux != nil {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700157 s = fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200158 }
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 {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700166 s = fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200167 }
168 return s + fmt.Sprintf(" [%s]", v.AuxValAndOff())
169 }
170 return ""
171}
172
Keith Randallf52b2342015-03-03 13:38:14 -0800173func (v *Value) AddArg(w *Value) {
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700174 if v.Args == nil {
175 v.resetArgs() // use argstorage
176 }
Keith Randallf52b2342015-03-03 13:38:14 -0800177 v.Args = append(v.Args, w)
Keith Randall56e0ecc2016-03-15 20:45:50 -0700178 w.Uses++
Keith Randallf52b2342015-03-03 13:38:14 -0800179}
180func (v *Value) AddArgs(a ...*Value) {
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700181 if v.Args == nil {
182 v.resetArgs() // use argstorage
183 }
Keith Randallf52b2342015-03-03 13:38:14 -0800184 v.Args = append(v.Args, a...)
Keith Randall56e0ecc2016-03-15 20:45:50 -0700185 for _, x := range a {
186 x.Uses++
187 }
Keith Randallf52b2342015-03-03 13:38:14 -0800188}
189func (v *Value) SetArg(i int, w *Value) {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700190 v.Args[i].Uses--
Keith Randallf52b2342015-03-03 13:38:14 -0800191 v.Args[i] = w
Keith Randall56e0ecc2016-03-15 20:45:50 -0700192 w.Uses++
Keith Randallf52b2342015-03-03 13:38:14 -0800193}
194func (v *Value) RemoveArg(i int) {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700195 v.Args[i].Uses--
Keith Randallf52b2342015-03-03 13:38:14 -0800196 copy(v.Args[i:], v.Args[i+1:])
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700197 v.Args[len(v.Args)-1] = nil // aid GC
Keith Randallf52b2342015-03-03 13:38:14 -0800198 v.Args = v.Args[:len(v.Args)-1]
199}
200func (v *Value) SetArgs1(a *Value) {
201 v.resetArgs()
202 v.AddArg(a)
203}
204func (v *Value) SetArgs2(a *Value, b *Value) {
205 v.resetArgs()
206 v.AddArg(a)
207 v.AddArg(b)
208}
209
210func (v *Value) resetArgs() {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700211 for _, a := range v.Args {
212 a.Uses--
213 }
Keith Randallf52b2342015-03-03 13:38:14 -0800214 v.argstorage[0] = nil
215 v.argstorage[1] = nil
Keith Randall7e406272016-04-11 13:17:52 -0700216 v.argstorage[2] = nil
Keith Randallf52b2342015-03-03 13:38:14 -0800217 v.Args = v.argstorage[:0]
218}
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700219
Alexandru Moșoi2df4b9c2016-02-04 17:21:57 +0100220func (v *Value) reset(op Op) {
221 v.Op = op
222 v.resetArgs()
223 v.AuxInt = 0
224 v.Aux = nil
225}
226
Keith Randallc140df02015-12-09 15:58:18 -0800227// copyInto makes a new value identical to v and adds it to the end of b.
228func (v *Value) copyInto(b *Block) *Value {
David Chase00263a82017-02-02 14:51:15 -0500229 c := b.NewValue0(v.Pos, v.Op, v.Type) // Lose the position, this causes line number churn otherwise.
230 c.Aux = v.Aux
231 c.AuxInt = v.AuxInt
232 c.AddArgs(v.Args...)
233 for _, a := range v.Args {
234 if a.Type.IsMemory() {
235 v.Fatalf("can't move a value with a memory arg %s", v.LongString())
236 }
237 }
238 return c
239}
240
David Chase643be702017-05-10 13:50:19 -0400241// copyIntoNoXPos makes a new value identical to v and adds it to the end of b.
242// The copied value receives no source code position to avoid confusing changes
243// in debugger information (the intended user is the register allocator).
David Chase00263a82017-02-02 14:51:15 -0500244func (v *Value) copyIntoNoXPos(b *Block) *Value {
David Chaseca360c32017-07-21 12:00:38 -0400245 return v.copyIntoWithXPos(b, src.NoXPos)
246}
247
248// copyIntoWithXPos makes a new value identical to v and adds it to the end of b.
249// The supplied position is used as the position of the new value.
250func (v *Value) copyIntoWithXPos(b *Block, pos src.XPos) *Value {
251 c := b.NewValue0(pos, v.Op, v.Type)
Keith Randallc140df02015-12-09 15:58:18 -0800252 c.Aux = v.Aux
253 c.AuxInt = v.AuxInt
254 c.AddArgs(v.Args...)
Keith Randallf94e0742016-01-26 15:47:08 -0800255 for _, a := range v.Args {
256 if a.Type.IsMemory() {
257 v.Fatalf("can't move a value with a memory arg %s", v.LongString())
258 }
259 }
Keith Randallc140df02015-12-09 15:58:18 -0800260 return c
261}
262
Keith Randallda8af472016-01-13 11:14:57 -0800263func (v *Value) Logf(msg string, args ...interface{}) { v.Block.Logf(msg, args...) }
David Chase88b230e2016-01-29 14:44:15 -0500264func (v *Value) Log() bool { return v.Block.Log() }
Keith Randallda8af472016-01-13 11:14:57 -0800265func (v *Value) Fatalf(msg string, args ...interface{}) {
Josh Bleecher Snyder2cdb7f12017-03-16 22:42:10 -0700266 v.Block.Func.fe.Fatalf(v.Pos, msg, args...)
Keith Randallda8af472016-01-13 11:14:57 -0800267}
Keith Randall8c46aa52015-06-19 21:02:28 -0700268
Keith Randall47c9e132016-03-23 10:20:44 -0700269// isGenericIntConst returns whether v is a generic integer constant.
270func (v *Value) isGenericIntConst() bool {
271 return v != nil && (v.Op == OpConst64 || v.Op == OpConst32 || v.Op == OpConst16 || v.Op == OpConst8)
272}
273
Keith Randall833ed7c2016-09-16 09:36:00 -0700274// Reg returns the register assigned to v, in cmd/internal/obj/$ARCH numbering.
275func (v *Value) Reg() int16 {
276 reg := v.Block.Func.RegAlloc[v.ID]
277 if reg == nil {
278 v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
279 }
280 return reg.(*Register).objNum
281}
282
283// Reg0 returns the register assigned to the first output of v, in cmd/internal/obj/$ARCH numbering.
284func (v *Value) Reg0() int16 {
285 reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[0]
286 if reg == nil {
287 v.Fatalf("nil first register for value: %s\n%s\n", v.LongString(), v.Block.Func)
288 }
289 return reg.(*Register).objNum
290}
291
292// Reg1 returns the register assigned to the second output of v, in cmd/internal/obj/$ARCH numbering.
293func (v *Value) Reg1() int16 {
294 reg := v.Block.Func.RegAlloc[v.ID].(LocPair)[1]
295 if reg == nil {
296 v.Fatalf("nil second register for value: %s\n%s\n", v.LongString(), v.Block.Func)
297 }
298 return reg.(*Register).objNum
299}
300
301func (v *Value) RegName() string {
302 reg := v.Block.Func.RegAlloc[v.ID]
303 if reg == nil {
304 v.Fatalf("nil register for value: %s\n%s\n", v.LongString(), v.Block.Func)
305 }
306 return reg.(*Register).name
307}
Philip Hoferb0e91d82017-03-03 13:44:18 -0800308
309// MemoryArg returns the memory argument for the Value.
Keith Randall256210c2017-05-15 09:00:55 -0700310// The returned value, if non-nil, will be memory-typed (or a tuple with a memory-typed second part).
311// Otherwise, nil is returned.
Philip Hoferb0e91d82017-03-03 13:44:18 -0800312func (v *Value) MemoryArg() *Value {
313 if v.Op == OpPhi {
314 v.Fatalf("MemoryArg on Phi")
315 }
316 na := len(v.Args)
317 if na == 0 {
318 return nil
319 }
Keith Randall256210c2017-05-15 09:00:55 -0700320 if m := v.Args[na-1]; m.Type.IsMemory() {
Philip Hoferb0e91d82017-03-03 13:44:18 -0800321 return m
322 }
323 return nil
324}
David Chasea0422212017-10-29 11:53:18 -0400325
326// LackingPos indicates whether v is a value that is unlikely to have a correct
327// position assigned to it. Ignoring such values leads to more user-friendly positions
328// assigned to nearby values and the blocks containing them.
329func (v *Value) LackingPos() bool {
330 // The exact definition of LackingPos is somewhat heuristically defined and may change
331 // in the future, for example if some of these operations are generated more carefully
332 // with respect to their source position.
333 return v.Op == OpVarDef || v.Op == OpVarKill || v.Op == OpVarLive || v.Op == OpPhi ||
334 (v.Op == OpFwdRef || v.Op == OpCopy) && v.Type == types.TypeMem
335}