blob: 73cb6a1b345bc9b2dd1f0b32b463b6509b81224d [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"
Matthew Dempsky8cf17662017-02-06 18:18:49 -08009 "cmd/internal/obj"
Robert Griesemer24597c02016-12-06 17:08:06 -080010 "cmd/internal/src"
Todd Neal19447a62015-09-04 06:33:56 -050011 "fmt"
12 "math"
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 {
124 s += " (" + strings.Join(names, ", ") + ")"
125 }
Keith Randallf52b2342015-03-03 13:38:14 -0800126 return s
127}
128
Frits van Bommelb13b2492016-04-30 11:13:29 +0200129func (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)
Cherry Zhangc8f38b32017-03-13 21:51:08 -0400149 case auxSym, auxTyp:
Frits van Bommelb13b2492016-04-30 11:13:29 +0200150 if v.Aux != nil {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700151 return fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200152 }
Cherry Zhangc8f38b32017-03-13 21:51:08 -0400153 case auxSymOff, auxSymInt32, auxTypSize:
Frits van Bommelb13b2492016-04-30 11:13:29 +0200154 s := ""
155 if v.Aux != nil {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700156 s = fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200157 }
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 {
Keith Randall2e2db7a2016-09-16 16:56:29 -0700165 s = fmt.Sprintf(" {%v}", v.Aux)
Frits van Bommelb13b2492016-04-30 11:13:29 +0200166 }
167 return s + fmt.Sprintf(" [%s]", v.AuxValAndOff())
168 }
169 return ""
170}
171
Keith Randallf52b2342015-03-03 13:38:14 -0800172func (v *Value) AddArg(w *Value) {
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700173 if v.Args == nil {
174 v.resetArgs() // use argstorage
175 }
Keith Randallf52b2342015-03-03 13:38:14 -0800176 v.Args = append(v.Args, w)
Keith Randall56e0ecc2016-03-15 20:45:50 -0700177 w.Uses++
Keith Randallf52b2342015-03-03 13:38:14 -0800178}
179func (v *Value) AddArgs(a ...*Value) {
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700180 if v.Args == nil {
181 v.resetArgs() // use argstorage
182 }
Keith Randallf52b2342015-03-03 13:38:14 -0800183 v.Args = append(v.Args, a...)
Keith Randall56e0ecc2016-03-15 20:45:50 -0700184 for _, x := range a {
185 x.Uses++
186 }
Keith Randallf52b2342015-03-03 13:38:14 -0800187}
188func (v *Value) SetArg(i int, w *Value) {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700189 v.Args[i].Uses--
Keith Randallf52b2342015-03-03 13:38:14 -0800190 v.Args[i] = w
Keith Randall56e0ecc2016-03-15 20:45:50 -0700191 w.Uses++
Keith Randallf52b2342015-03-03 13:38:14 -0800192}
193func (v *Value) RemoveArg(i int) {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700194 v.Args[i].Uses--
Keith Randallf52b2342015-03-03 13:38:14 -0800195 copy(v.Args[i:], v.Args[i+1:])
Josh Bleecher Snyder7c2c0b42015-03-16 16:31:13 -0700196 v.Args[len(v.Args)-1] = nil // aid GC
Keith Randallf52b2342015-03-03 13:38:14 -0800197 v.Args = v.Args[:len(v.Args)-1]
198}
199func (v *Value) SetArgs1(a *Value) {
200 v.resetArgs()
201 v.AddArg(a)
202}
203func (v *Value) SetArgs2(a *Value, b *Value) {
204 v.resetArgs()
205 v.AddArg(a)
206 v.AddArg(b)
207}
208
209func (v *Value) resetArgs() {
Keith Randall56e0ecc2016-03-15 20:45:50 -0700210 for _, a := range v.Args {
211 a.Uses--
212 }
Keith Randallf52b2342015-03-03 13:38:14 -0800213 v.argstorage[0] = nil
214 v.argstorage[1] = nil
Keith Randall7e406272016-04-11 13:17:52 -0700215 v.argstorage[2] = nil
Keith Randallf52b2342015-03-03 13:38:14 -0800216 v.Args = v.argstorage[:0]
217}
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700218
Alexandru Moșoi2df4b9c2016-02-04 17:21:57 +0100219func (v *Value) reset(op Op) {
220 v.Op = op
221 v.resetArgs()
222 v.AuxInt = 0
223 v.Aux = nil
224}
225
Keith Randallc140df02015-12-09 15:58:18 -0800226// copyInto makes a new value identical to v and adds it to the end of b.
227func (v *Value) copyInto(b *Block) *Value {
David Chase00263a82017-02-02 14:51:15 -0500228 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
David Chase643be702017-05-10 13:50:19 -0400240// 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).
David Chase00263a82017-02-02 14:51:15 -0500243func (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.
Keith Randallc140df02015-12-09 15:58:18 -0800245 c.Aux = v.Aux
246 c.AuxInt = v.AuxInt
247 c.AddArgs(v.Args...)
Keith Randallf94e0742016-01-26 15:47:08 -0800248 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 }
Keith Randallc140df02015-12-09 15:58:18 -0800253 return c
254}
255
Keith Randallda8af472016-01-13 11:14:57 -0800256func (v *Value) Logf(msg string, args ...interface{}) { v.Block.Logf(msg, args...) }
David Chase88b230e2016-01-29 14:44:15 -0500257func (v *Value) Log() bool { return v.Block.Log() }
Keith Randallda8af472016-01-13 11:14:57 -0800258func (v *Value) Fatalf(msg string, args ...interface{}) {
Josh Bleecher Snyder2cdb7f12017-03-16 22:42:10 -0700259 v.Block.Func.fe.Fatalf(v.Pos, msg, args...)
Keith Randallda8af472016-01-13 11:14:57 -0800260}
Keith Randall8c46aa52015-06-19 21:02:28 -0700261
Keith Randall47c9e132016-03-23 10:20:44 -0700262// isGenericIntConst returns whether v is a generic integer constant.
263func (v *Value) isGenericIntConst() bool {
264 return v != nil && (v.Op == OpConst64 || v.Op == OpConst32 || v.Op == OpConst16 || v.Op == OpConst8)
265}
266
Keith Randall8c46aa52015-06-19 21:02:28 -0700267// ExternSymbol is an aux value that encodes a variable's
268// constant offset from the static base pointer.
269type ExternSymbol struct {
Matthew Dempsky8cf17662017-02-06 18:18:49 -0800270 Sym *obj.LSym
Keith Randall8c46aa52015-06-19 21:02:28 -0700271 // 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).
277type ArgSymbol struct {
Keith Randallc24681a2015-10-22 14:22:38 -0700278 Node GCNode // A *gc.Node referring to the argument/result variable.
Keith Randall8c46aa52015-06-19 21:02:28 -0700279}
280
281// AutoSymbol is an aux value that encodes a local variable's
282// constant offset from SP.
283type AutoSymbol struct {
Keith Randallc24681a2015-10-22 14:22:38 -0700284 Node GCNode // A *gc.Node referring to a local (auto) variable.
Keith Randall8c46aa52015-06-19 21:02:28 -0700285}
286
287func (s *ExternSymbol) String() string {
288 return s.Sym.String()
289}
290
291func (s *ArgSymbol) String() string {
Keith Randalld2107fc2015-08-24 02:16:19 -0700292 return s.Node.String()
Keith Randall8c46aa52015-06-19 21:02:28 -0700293}
294
295func (s *AutoSymbol) String() string {
Keith Randalld2107fc2015-08-24 02:16:19 -0700296 return s.Node.String()
Keith Randall8c46aa52015-06-19 21:02:28 -0700297}
Keith Randall833ed7c2016-09-16 09:36:00 -0700298
299// Reg returns the register assigned to v, in cmd/internal/obj/$ARCH numbering.
300func (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.
309func (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.
318func (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
326func (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}
Philip Hoferb0e91d82017-03-03 13:44:18 -0800333
334// MemoryArg returns the memory argument for the Value.
Keith Randall256210c2017-05-15 09:00:55 -0700335// The returned value, if non-nil, will be memory-typed (or a tuple with a memory-typed second part).
336// Otherwise, nil is returned.
Philip Hoferb0e91d82017-03-03 13:44:18 -0800337func (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 }
Keith Randall256210c2017-05-15 09:00:55 -0700345 if m := v.Args[na-1]; m.Type.IsMemory() {
Philip Hoferb0e91d82017-03-03 13:44:18 -0800346 return m
347 }
348 return nil
349}