-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_block.go
64 lines (58 loc) · 2.04 KB
/
basic_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ossa
// BasicBlock represents a basic block in a control flow graph. A basic block
// is a straight sequence of instructions (represented as values) that always
// run as a single unit, followed by a terminator that implies zero or more
// graph edges to other successor blocks.
type BasicBlock struct {
Instructions []*Value
Terminator *Terminator
}
func NewBasicBlock() *BasicBlock {
return &BasicBlock{}
}
// AddSuccessors adds the successors of this block to the given set, modifying
// it in-place.
func (b *BasicBlock) AddSuccessors(to BasicBlockAdder) {
b.Terminator.AddSuccessors(to)
}
// AddReachable adds to the given set all of the blocks that are reachable
// from the receiver, including the receiver itself. The set is modified
// in-place.
//
// This method assumes that the given set, if not empty, was built by a prior
// call to AddReachable, and so any blocks already present in the set is
// already accompanied by all of the blocks reachable from it. In other words,
// it will not visit any of the descendents of any blocks already present in
// the set, even if they are reachable from the receiver.
func (b *BasicBlock) AddReachable(to BasicBlockSet) {
// todo serves as a work queue, but we'll use stack discipline for it just
// to keep things simple, since our order of processing is not important.
todo := make([]*BasicBlock, 0, 4) // cap 4 just to give us some room for simple graphs without more allocation
todo = append(todo, b)
for len(todo) > 0 {
block := todo[len(todo)-1]
todo = todo[:len(todo)-1]
if to.Has(block) {
continue
}
to.Add(block)
todo = b.Terminator.AppendSuccessors(todo)
}
}
// BasicBlockValue represents a (BasicBlock, Value) pair, used in a small
// number of value factory functions.
type BasicBlockValue struct {
Block *BasicBlock
Value *Value
}
func bbvsAsArgs(pairs []BasicBlockValue) []*Value {
args := make([]*Value, 0, len(pairs)*2)
for _, pair := range pairs {
args = append(args, &Value{
op: opBasicBlock,
aux: pair.Block,
})
args = append(args, pair.Value)
}
return args
}