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.
12 // Block represents a basic block in the control flow graph of a function.
14 // A unique identifier for the block. The system will attempt to allocate
15 // these IDs densely, but no guarantees.
18 // Source position for block's control operation
21 // The kind of block this is.
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
31 // After flagalloc, records whether flags are live at the end of the block.
34 // Subsequent blocks, if any. The number and order depend on the block kind.
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?
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.
48 // The ControlValues() method may be used to get a slice with the non-nil
49 // control values that can be ranged over.
51 // Controls[1] must be nil if Controls[0] is nil.
54 // Auxiliary info for the block. Its value depends on the Kind.
58 // The unordered set of Values that define the operation of this block.
59 // After the scheduling pass, this list is ordered.
62 // The containing function
65 // Storage for Succs, Preds and Values.
71 // Edge represents a CFG edge.
72 // Example edges for b branching to either c or d.
73 // (c and d have other predecessors.)
75 // b.Succs = [{c,3}, {d,1}]
76 // c.Preds = [?, ?, ?, {b,0}]
77 // d.Preds = [?, {b,1}, ?]
79 // These indexes allow us to edit the CFG in constant time.
80 // In addition, it informs phi ops in degenerate cases like:
87 // Then the indexes tell you whether x is chosen from
88 // the if or else branch from b.
90 // b.Succs = [{c,0},{c,1}]
91 // c.Preds = [{b,0},{b,1}]
93 // means x is chosen if k is true.
95 // block edge goes to (in a Succs list) or from (in a Preds list)
97 // index of reverse edge. Invariant:
99 // e.b.Preds[e.i] = Edge{x,idx}
100 // and similarly for predecessors.
104 func (e Edge) Block() *Block {
107 func (e Edge) Index() int {
110 func (e Edge) String() string {
111 return fmt.Sprintf("{%v,%d}", e.b, e.i)
114 // BlockKind is the kind of SSA block.
118 func (b *Block) String() string {
119 return fmt.Sprintf("b%d", b.ID)
123 func (b *Block) LongString() string {
126 s += fmt.Sprintf(" {%s}", b.Aux)
128 if t := b.AuxIntString(); t != "" {
129 s += fmt.Sprintf(" [%s]", t)
131 for _, c := range b.ControlValues() {
132 s += fmt.Sprintf(" %s", c)
134 if len(b.Succs) > 0 {
136 for _, c := range b.Succs {
137 s += " " + c.b.String()
149 // NumControls returns the number of non-nil control values the
151 func (b *Block) NumControls() int {
152 if b.Controls[0] == nil {
155 if b.Controls[1] == nil {
161 // ControlValues returns a slice containing the non-nil control
162 // values of the block. The index of each control value will be
163 // the same as it is in the Controls property and can be used
164 // in ReplaceControl calls.
165 func (b *Block) ControlValues() []*Value {
166 if b.Controls[0] == nil {
167 return b.Controls[:0]
169 if b.Controls[1] == nil {
170 return b.Controls[:1]
172 return b.Controls[:2]
175 // SetControl removes all existing control values and then adds
176 // the control value provided. The number of control values after
177 // a call to SetControl will always be 1.
178 func (b *Block) SetControl(v *Value) {
184 // ResetControls sets the number of controls for the block to 0.
185 func (b *Block) ResetControls() {
186 if b.Controls[0] != nil {
189 if b.Controls[1] != nil {
192 b.Controls = [2]*Value{} // reset both controls to nil
195 // AddControl appends a control value to the existing list of control values.
196 func (b *Block) AddControl(v *Value) {
198 b.Controls[i] = v // panics if array is full
202 // ReplaceControl exchanges the existing control value at the index provided
203 // for the new value. The index must refer to a valid control value.
204 func (b *Block) ReplaceControl(i int, v *Value) {
210 // CopyControls replaces the controls for this block with those from the
211 // provided block. The provided block is not modified.
212 func (b *Block) CopyControls(from *Block) {
217 for _, c := range from.ControlValues() {
222 // Reset sets the block to the provided kind and clears all the blocks control
223 // and auxiliary values. Other properties of the block, such as its successors,
224 // predecessors and values are left unmodified.
225 func (b *Block) Reset(kind BlockKind) {
232 // resetWithControl resets b and adds control v.
233 // It is equivalent to b.Reset(kind); b.AddControl(v),
234 // except that it is one call instead of two and avoids a bounds check.
235 // It is intended for use by rewrite rules, where this matters.
236 func (b *Block) resetWithControl(kind BlockKind, v *Value) {
245 // resetWithControl2 resets b and adds controls v and w.
246 // It is equivalent to b.Reset(kind); b.AddControl(v); b.AddControl(w),
247 // except that it is one call instead of three and avoids two bounds checks.
248 // It is intended for use by rewrite rules, where this matters.
249 func (b *Block) resetWithControl2(kind BlockKind, v, w *Value) {
260 // truncateValues truncates b.Values at the ith element, zeroing subsequent elements.
261 // The values in b.Values after i must already have had their args reset,
262 // to maintain correct value uses counts.
263 func (b *Block) truncateValues(i int) {
265 for j := range tail {
268 b.Values = b.Values[:i]
271 // AddEdgeTo adds an edge from block b to block c.
272 func (b *Block) AddEdgeTo(c *Block) {
275 b.Succs = append(b.Succs, Edge{c, j})
276 c.Preds = append(c.Preds, Edge{b, i})
277 b.Func.invalidateCFG()
280 // removePred removes the ith input edge from b.
281 // It is the responsibility of the caller to remove
282 // the corresponding successor edge, and adjust any
283 // phi values by calling b.removePhiArg(v, i).
284 func (b *Block) removePred(i int) {
285 n := len(b.Preds) - 1
289 // Update the other end of the edge we moved.
293 b.Preds = b.Preds[:n]
294 b.Func.invalidateCFG()
297 // removeSucc removes the ith output edge from b.
298 // It is the responsibility of the caller to remove
299 // the corresponding predecessor edge.
300 func (b *Block) removeSucc(i int) {
301 n := len(b.Succs) - 1
305 // Update the other end of the edge we moved.
309 b.Succs = b.Succs[:n]
310 b.Func.invalidateCFG()
313 func (b *Block) swapSuccessors() {
314 if len(b.Succs) != 2 {
315 b.Fatalf("swapSuccessors with len(Succs)=%d", len(b.Succs))
321 e0.b.Preds[e0.i].i = 1
322 e1.b.Preds[e1.i].i = 0
326 // removePhiArg removes the ith arg from phi.
327 // It must be called after calling b.removePred(i) to
328 // adjust the corresponding phi value of the block:
331 // for _, v := range b.Values {
333 // if v.Op != OpPhi {
336 // b.removePhiArg(v, i)
339 func (b *Block) removePhiArg(phi *Value, i int) {
341 if numPhiArgs := len(phi.Args); numPhiArgs-1 != n {
342 b.Fatalf("inconsistent state, num predecessors: %d, num phi args: %d", n, numPhiArgs)
345 phi.Args[i] = phi.Args[n]
347 phi.Args = phi.Args[:n]
351 // LackingPos indicates whether b is a block whose position should be inherited
352 // from its successors. This is true if all the values within it have unreliable positions
353 // and if it is "plain", meaning that there is no control flow that is also very likely
354 // to correspond to a well-understood source position.
355 func (b *Block) LackingPos() bool {
356 // Non-plain predecessors are If or Defer, which both (1) have two successors,
357 // which might have different line numbers and (2) correspond to statements
358 // in the source code that have positions, so this case ought not occur anyway.
359 if b.Kind != BlockPlain {
362 if b.Pos != src.NoXPos {
365 for _, v := range b.Values {
374 func (b *Block) AuxIntString() string {
375 switch b.Kind.AuxIntType() {
377 return fmt.Sprintf("%v", int8(b.AuxInt))
379 return fmt.Sprintf("%v", uint8(b.AuxInt))
380 case "": // no aux int type
382 default: // type specified but not implemented - print as int64
383 return fmt.Sprintf("%v", b.AuxInt)
387 // likelyBranch reports whether block b is the likely branch of all of its predecessors.
388 func (b *Block) likelyBranch() bool {
389 if len(b.Preds) == 0 {
392 for _, e := range b.Preds {
394 if len(p.Succs) == 1 || len(p.Succs) == 2 && (p.Likely == BranchLikely && p.Succs[0].b == b ||
395 p.Likely == BranchUnlikely && p.Succs[1].b == b) {
403 func (b *Block) Logf(msg string, args ...interface{}) { b.Func.Logf(msg, args...) }
404 func (b *Block) Log() bool { return b.Func.Log() }
405 func (b *Block) Fatalf(msg string, args ...interface{}) { b.Func.Fatalf(msg, args...) }
407 type BranchPrediction int8
410 BranchUnlikely = BranchPrediction(-1)
411 BranchUnknown = BranchPrediction(0)
412 BranchLikely = BranchPrediction(+1)