]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/ssa/block.go
e7776b231643c6c2d222b99a6b936ae1b0612b66
[gostls13.git] / src / cmd / compile / internal / ssa / block.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 (
8         "cmd/internal/src"
9         "fmt"
10 )
11
12 // Block represents a basic block in the control flow graph of a function.
13 type Block struct {
14         // A unique identifier for the block. The system will attempt to allocate
15         // these IDs densely, but no guarantees.
16         ID ID
17
18         // Source position for block's control operation
19         Pos src.XPos
20
21         // The kind of block this is.
22         Kind BlockKind
23
24         // Likely direction for branches.
25         // If BranchLikely, Succs[0] is the most likely branch taken.
26         // If BranchUnlikely, Succs[1] is the most likely branch taken.
27         // Ignored if len(Succs) < 2.
28         // Fatal if not BranchUnknown and len(Succs) > 2.
29         Likely BranchPrediction
30
31         // After flagalloc, records whether flags are live at the end of the block.
32         FlagsLiveAtEnd bool
33
34         // Subsequent blocks, if any. The number and order depend on the block kind.
35         Succs []Edge
36
37         // Inverse of successors.
38         // The order is significant to Phi nodes in the block.
39         // TODO: predecessors is a pain to maintain. Can we somehow order phi
40         // arguments by block id and have this field computed explicitly when needed?
41         Preds []Edge
42
43         // A list of values that determine how the block is exited. The number
44         // and type of control values depends on the Kind of the block. For
45         // instance, a BlockIf has a single boolean control value and BlockExit
46         // has a single memory control value.
47         //
48         // The ControlValues() method may be used to get a slice with the non-nil
49         // control values that can be ranged over.
50         //
51         // Controls[1] must be nil if Controls[0] is nil.
52         Controls [2]*Value
53
54         // Auxiliary info for the block. Its value depends on the Kind.
55         Aux    Aux
56         AuxInt int64
57
58         // The unordered set of Values that define the operation of this block.
59         // After the scheduling pass, this list is ordered.
60         Values []*Value
61
62         // The containing function
63         Func *Func
64
65         // Storage for Succs, Preds and Values.
66         succstorage [2]Edge
67         predstorage [4]Edge
68         valstorage  [9]*Value
69 }
70
71 // Edge represents a CFG edge.
72 // Example edges for b branching to either c or d.
73 // (c and d have other predecessors.)
74 //
75 //      b.Succs = [{c,3}, {d,1}]
76 //      c.Preds = [?, ?, ?, {b,0}]
77 //      d.Preds = [?, {b,1}, ?]
78 //
79 // These indexes allow us to edit the CFG in constant time.
80 // In addition, it informs phi ops in degenerate cases like:
81 //
82 //      b:
83 //         if k then c else c
84 //      c:
85 //         v = Phi(x, y)
86 //
87 // Then the indexes tell you whether x is chosen from
88 // the if or else branch from b.
89 //
90 //      b.Succs = [{c,0},{c,1}]
91 //      c.Preds = [{b,0},{b,1}]
92 //
93 // means x is chosen if k is true.
94 type Edge struct {
95         // block edge goes to (in a Succs list) or from (in a Preds list)
96         b *Block
97         // index of reverse edge.  Invariant:
98         //   e := x.Succs[idx]
99         //   e.b.Preds[e.i] = Edge{x,idx}
100         // and similarly for predecessors.
101         i int
102 }
103
104 func (e Edge) Block() *Block {
105         return e.b
106 }
107 func (e Edge) Index() int {
108         return e.i
109 }
110 func (e Edge) String() string {
111         return fmt.Sprintf("{%v,%d}", e.b, e.i)
112 }
113
114 // BlockKind is the kind of SSA block.
115 //
116 //        kind          controls        successors
117 //      ------------------------------------------
118 //        Exit      [return mem]                []
119 //       Plain                []            [next]
120 //          If   [boolean Value]      [then, else]
121 //       Defer             [mem]  [nopanic, panic]  (control opcode should be OpStaticCall to runtime.deferproc)
122 type BlockKind int16
123
124 // short form print
125 func (b *Block) String() string {
126         return fmt.Sprintf("b%d", b.ID)
127 }
128
129 // long form print
130 func (b *Block) LongString() string {
131         s := b.Kind.String()
132         if b.Aux != nil {
133                 s += fmt.Sprintf(" {%s}", b.Aux)
134         }
135         if t := b.AuxIntString(); t != "" {
136                 s += fmt.Sprintf(" [%s]", t)
137         }
138         for _, c := range b.ControlValues() {
139                 s += fmt.Sprintf(" %s", c)
140         }
141         if len(b.Succs) > 0 {
142                 s += " ->"
143                 for _, c := range b.Succs {
144                         s += " " + c.b.String()
145                 }
146         }
147         switch b.Likely {
148         case BranchUnlikely:
149                 s += " (unlikely)"
150         case BranchLikely:
151                 s += " (likely)"
152         }
153         return s
154 }
155
156 // NumControls returns the number of non-nil control values the
157 // block has.
158 func (b *Block) NumControls() int {
159         if b.Controls[0] == nil {
160                 return 0
161         }
162         if b.Controls[1] == nil {
163                 return 1
164         }
165         return 2
166 }
167
168 // ControlValues returns a slice containing the non-nil control
169 // values of the block. The index of each control value will be
170 // the same as it is in the Controls property and can be used
171 // in ReplaceControl calls.
172 func (b *Block) ControlValues() []*Value {
173         if b.Controls[0] == nil {
174                 return b.Controls[:0]
175         }
176         if b.Controls[1] == nil {
177                 return b.Controls[:1]
178         }
179         return b.Controls[:2]
180 }
181
182 // SetControl removes all existing control values and then adds
183 // the control value provided. The number of control values after
184 // a call to SetControl will always be 1.
185 func (b *Block) SetControl(v *Value) {
186         b.ResetControls()
187         b.Controls[0] = v
188         v.Uses++
189 }
190
191 // ResetControls sets the number of controls for the block to 0.
192 func (b *Block) ResetControls() {
193         if b.Controls[0] != nil {
194                 b.Controls[0].Uses--
195         }
196         if b.Controls[1] != nil {
197                 b.Controls[1].Uses--
198         }
199         b.Controls = [2]*Value{} // reset both controls to nil
200 }
201
202 // AddControl appends a control value to the existing list of control values.
203 func (b *Block) AddControl(v *Value) {
204         i := b.NumControls()
205         b.Controls[i] = v // panics if array is full
206         v.Uses++
207 }
208
209 // ReplaceControl exchanges the existing control value at the index provided
210 // for the new value. The index must refer to a valid control value.
211 func (b *Block) ReplaceControl(i int, v *Value) {
212         b.Controls[i].Uses--
213         b.Controls[i] = v
214         v.Uses++
215 }
216
217 // CopyControls replaces the controls for this block with those from the
218 // provided block. The provided block is not modified.
219 func (b *Block) CopyControls(from *Block) {
220         if b == from {
221                 return
222         }
223         b.ResetControls()
224         for _, c := range from.ControlValues() {
225                 b.AddControl(c)
226         }
227 }
228
229 // Reset sets the block to the provided kind and clears all the blocks control
230 // and auxiliary values. Other properties of the block, such as its successors,
231 // predecessors and values are left unmodified.
232 func (b *Block) Reset(kind BlockKind) {
233         b.Kind = kind
234         b.ResetControls()
235         b.Aux = nil
236         b.AuxInt = 0
237 }
238
239 // resetWithControl resets b and adds control v.
240 // It is equivalent to b.Reset(kind); b.AddControl(v),
241 // except that it is one call instead of two and avoids a bounds check.
242 // It is intended for use by rewrite rules, where this matters.
243 func (b *Block) resetWithControl(kind BlockKind, v *Value) {
244         b.Kind = kind
245         b.ResetControls()
246         b.Aux = nil
247         b.AuxInt = 0
248         b.Controls[0] = v
249         v.Uses++
250 }
251
252 // resetWithControl2 resets b and adds controls v and w.
253 // It is equivalent to b.Reset(kind); b.AddControl(v); b.AddControl(w),
254 // except that it is one call instead of three and avoids two bounds checks.
255 // It is intended for use by rewrite rules, where this matters.
256 func (b *Block) resetWithControl2(kind BlockKind, v, w *Value) {
257         b.Kind = kind
258         b.ResetControls()
259         b.Aux = nil
260         b.AuxInt = 0
261         b.Controls[0] = v
262         b.Controls[1] = w
263         v.Uses++
264         w.Uses++
265 }
266
267 // truncateValues truncates b.Values at the ith element, zeroing subsequent elements.
268 // The values in b.Values after i must already have had their args reset,
269 // to maintain correct value uses counts.
270 func (b *Block) truncateValues(i int) {
271         tail := b.Values[i:]
272         for j := range tail {
273                 tail[j] = nil
274         }
275         b.Values = b.Values[:i]
276 }
277
278 // AddEdgeTo adds an edge from block b to block c. Used during building of the
279 // SSA graph; do not use on an already-completed SSA graph.
280 func (b *Block) AddEdgeTo(c *Block) {
281         i := len(b.Succs)
282         j := len(c.Preds)
283         b.Succs = append(b.Succs, Edge{c, j})
284         c.Preds = append(c.Preds, Edge{b, i})
285         b.Func.invalidateCFG()
286 }
287
288 // removePred removes the ith input edge from b.
289 // It is the responsibility of the caller to remove
290 // the corresponding successor edge, and adjust any
291 // phi values by calling b.removePhiArg(v, i).
292 func (b *Block) removePred(i int) {
293         n := len(b.Preds) - 1
294         if i != n {
295                 e := b.Preds[n]
296                 b.Preds[i] = e
297                 // Update the other end of the edge we moved.
298                 e.b.Succs[e.i].i = i
299         }
300         b.Preds[n] = Edge{}
301         b.Preds = b.Preds[:n]
302         b.Func.invalidateCFG()
303 }
304
305 // removeSucc removes the ith output edge from b.
306 // It is the responsibility of the caller to remove
307 // the corresponding predecessor edge.
308 func (b *Block) removeSucc(i int) {
309         n := len(b.Succs) - 1
310         if i != n {
311                 e := b.Succs[n]
312                 b.Succs[i] = e
313                 // Update the other end of the edge we moved.
314                 e.b.Preds[e.i].i = i
315         }
316         b.Succs[n] = Edge{}
317         b.Succs = b.Succs[:n]
318         b.Func.invalidateCFG()
319 }
320
321 func (b *Block) swapSuccessors() {
322         if len(b.Succs) != 2 {
323                 b.Fatalf("swapSuccessors with len(Succs)=%d", len(b.Succs))
324         }
325         e0 := b.Succs[0]
326         e1 := b.Succs[1]
327         b.Succs[0] = e1
328         b.Succs[1] = e0
329         e0.b.Preds[e0.i].i = 1
330         e1.b.Preds[e1.i].i = 0
331         b.Likely *= -1
332 }
333
334 // removePhiArg removes the ith arg from phi.
335 // It must be called after calling b.removePred(i) to
336 // adjust the corresponding phi value of the block:
337 //
338 // b.removePred(i)
339 // for _, v := range b.Values {
340 //
341 //      if v.Op != OpPhi {
342 //          continue
343 //      }
344 //      b.removePhiArg(v, i)
345 //
346 // }
347 func (b *Block) removePhiArg(phi *Value, i int) {
348         n := len(b.Preds)
349         if numPhiArgs := len(phi.Args); numPhiArgs-1 != n {
350                 b.Fatalf("inconsistent state, num predecessors: %d, num phi args: %d", n, numPhiArgs)
351         }
352         phi.Args[i].Uses--
353         phi.Args[i] = phi.Args[n]
354         phi.Args[n] = nil
355         phi.Args = phi.Args[:n]
356         phielimValue(phi)
357 }
358
359 // LackingPos indicates whether b is a block whose position should be inherited
360 // from its successors.  This is true if all the values within it have unreliable positions
361 // and if it is "plain", meaning that there is no control flow that is also very likely
362 // to correspond to a well-understood source position.
363 func (b *Block) LackingPos() bool {
364         // Non-plain predecessors are If or Defer, which both (1) have two successors,
365         // which might have different line numbers and (2) correspond to statements
366         // in the source code that have positions, so this case ought not occur anyway.
367         if b.Kind != BlockPlain {
368                 return false
369         }
370         if b.Pos != src.NoXPos {
371                 return false
372         }
373         for _, v := range b.Values {
374                 if v.LackingPos() {
375                         continue
376                 }
377                 return false
378         }
379         return true
380 }
381
382 func (b *Block) AuxIntString() string {
383         switch b.Kind.AuxIntType() {
384         case "int8":
385                 return fmt.Sprintf("%v", int8(b.AuxInt))
386         case "uint8":
387                 return fmt.Sprintf("%v", uint8(b.AuxInt))
388         default: // type specified but not implemented - print as int64
389                 return fmt.Sprintf("%v", b.AuxInt)
390         case "": // no aux int type
391                 return ""
392         }
393 }
394
395 // likelyBranch reports whether block b is the likely branch of all of its predecessors.
396 func (b *Block) likelyBranch() bool {
397         if len(b.Preds) == 0 {
398                 return false
399         }
400         for _, e := range b.Preds {
401                 p := e.b
402                 if len(p.Succs) == 1 || len(p.Succs) == 2 && (p.Likely == BranchLikely && p.Succs[0].b == b ||
403                         p.Likely == BranchUnlikely && p.Succs[1].b == b) {
404                         continue
405                 }
406                 return false
407         }
408         return true
409 }
410
411 func (b *Block) Logf(msg string, args ...interface{})   { b.Func.Logf(msg, args...) }
412 func (b *Block) Log() bool                              { return b.Func.Log() }
413 func (b *Block) Fatalf(msg string, args ...interface{}) { b.Func.Fatalf(msg, args...) }
414
415 type BranchPrediction int8
416
417 const (
418         BranchUnlikely = BranchPrediction(-1)
419         BranchUnknown  = BranchPrediction(0)
420         BranchLikely   = BranchPrediction(+1)
421 )