| 1 |
// Copyright 2022 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 ir |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
) |
| 11 |
|
| 12 |
// This file implements the BasicBlock type. |
| 13 |
|
| 14 |
// addEdge adds a control-flow graph edge from from to to. |
| 15 |
func addEdge(from, to *BasicBlock) { |
| 16 |
from.Succs = append(from.Succs, to) |
| 17 |
to.Preds = append(to.Preds, from) |
| 18 |
} |
| 19 |
|
| 20 |
// Control returns the last instruction in the block. |
| 21 |
func (b *BasicBlock) Control() Instruction { |
| 22 |
if len(b.Instrs) == 0 { |
| 23 |
return nil |
| 24 |
} |
| 25 |
return b.Instrs[len(b.Instrs)-1] |
| 26 |
} |
| 27 |
|
| 28 |
// Parent returns the function that contains block b. |
| 29 |
func (b *BasicBlock) Parent() *Function { return b.parent } |
| 30 |
|
| 31 |
func (b *BasicBlock) Reaches(o *BasicBlock) bool { |
| 32 |
return b.SCC.reachable.Bit(o.SCC.Index) != 0 |
| 33 |
} |
| 34 |
|
| 35 |
// String returns a human-readable label of this block. |
| 36 |
// It is not guaranteed unique within the function. |
| 37 |
func (b *BasicBlock) String() string { |
| 38 |
return fmt.Sprintf("%d", b.Index) |
| 39 |
} |
| 40 |
|
| 41 |
// emit appends an instruction to the current basic block. |
| 42 |
// If the instruction defines a Value, it is returned. |
| 43 |
func (b *BasicBlock) emit(i Instruction, source ast.Node) Value { |
| 44 |
i.setSource(source) |
| 45 |
i.setBlock(b) |
| 46 |
b.Instrs = append(b.Instrs, i) |
| 47 |
v, _ := i.(Value) |
| 48 |
return v |
| 49 |
} |
| 50 |
|
| 51 |
// predIndex returns the i such that b.Preds[i] == c or panics if |
| 52 |
// there is none. |
| 53 |
func (b *BasicBlock) predIndex(c *BasicBlock) int { |
| 54 |
for i, pred := range b.Preds { |
| 55 |
if pred == c { |
| 56 |
return i |
| 57 |
} |
| 58 |
} |
| 59 |
panic(fmt.Sprintf("no edge %s -> %s", c, b)) |
| 60 |
} |
| 61 |
|
| 62 |
// hasPhi returns true if b.Instrs contains φ-nodes. |
| 63 |
func (b *BasicBlock) hasPhi() bool { |
| 64 |
_, ok := b.Instrs[0].(*Phi) |
| 65 |
return ok |
| 66 |
} |
| 67 |
|
| 68 |
func (b *BasicBlock) Phis() []Instruction { |
| 69 |
return b.phis() |
| 70 |
} |
| 71 |
|
| 72 |
// phis returns the prefix of b.Instrs containing all the block's φ-nodes. |
| 73 |
func (b *BasicBlock) phis() []Instruction { |
| 74 |
for i, instr := range b.Instrs { |
| 75 |
if _, ok := instr.(*Phi); !ok { |
| 76 |
return b.Instrs[:i] |
| 77 |
} |
| 78 |
} |
| 79 |
return nil // unreachable in well-formed blocks |
| 80 |
} |
| 81 |
|
| 82 |
// replacePred replaces all occurrences of p in b's predecessor list with q. |
| 83 |
// Ordinarily there should be at most one. |
| 84 |
func (b *BasicBlock) replacePred(p, q *BasicBlock) { |
| 85 |
for i, pred := range b.Preds { |
| 86 |
if pred == p { |
| 87 |
b.Preds[i] = q |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// replaceSucc replaces all occurrences of p in b's successor list with q. |
| 93 |
// Ordinarily there should be at most one. |
| 94 |
func (b *BasicBlock) replaceSucc(p, q *BasicBlock) { |
| 95 |
for i, succ := range b.Succs { |
| 96 |
if succ == p { |
| 97 |
b.Succs[i] = q |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// removePred removes all occurrences of p in b's |
| 103 |
// predecessor list and φ-nodes. |
| 104 |
// Ordinarily there should be at most one. |
| 105 |
func (b *BasicBlock) removePred(p *BasicBlock) { |
| 106 |
phis := b.phis() |
| 107 |
|
| 108 |
// We must preserve edge order for φ-nodes. |
| 109 |
j := 0 |
| 110 |
for i, pred := range b.Preds { |
| 111 |
if pred != p { |
| 112 |
b.Preds[j] = b.Preds[i] |
| 113 |
// Strike out φ-edge too. |
| 114 |
for _, instr := range phis { |
| 115 |
phi := instr.(*Phi) |
| 116 |
phi.Edges[j] = phi.Edges[i] |
| 117 |
} |
| 118 |
j++ |
| 119 |
} |
| 120 |
} |
| 121 |
// Nil out b.Preds[j:] and φ-edges[j:] to aid GC. |
| 122 |
for i := j; i < len(b.Preds); i++ { |
| 123 |
b.Preds[i] = nil |
| 124 |
for _, instr := range phis { |
| 125 |
instr.(*Phi).Edges[i] = nil |
| 126 |
} |
| 127 |
} |
| 128 |
b.Preds = b.Preds[:j] |
| 129 |
for _, instr := range phis { |
| 130 |
phi := instr.(*Phi) |
| 131 |
phi.Edges = phi.Edges[:j] |
| 132 |
} |
| 133 |
} |