-
Notifications
You must be signed in to change notification settings - Fork 22
/
phi.go
153 lines (130 loc) · 4.08 KB
/
phi.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ssa
import (
`sort`
`github.com/oleiade/lane`
)
type _PhiDesc struct {
r Reg
b []*BasicBlock
}
func appendReg(buf map[Reg]bool, r Reg) map[Reg]bool {
if buf == nil {
return map[Reg]bool { r: true }
} else {
buf[r] = true
return buf
}
}
func appendBlock(buf map[int]*BasicBlock, bb *BasicBlock) map[int]*BasicBlock {
if buf == nil {
return map[int]*BasicBlock { bb.Id: bb }
} else {
buf[bb.Id] = bb
return buf
}
}
func insertPhiNodes(cfg *CFG) {
q := lane.NewQueue()
phi := make(map[Reg]map[int]bool)
orig := make(map[int]map[Reg]bool)
defs := make(map[Reg]map[int]*BasicBlock)
/* find out all the variable origins */
for q.Enqueue(cfg.Root); !q.Empty(); {
p := q.Dequeue().(*BasicBlock)
addImmediateDominated(cfg.DominatorOf, p, q)
/* mark all the definition sites */
for _, ins := range p.Ins {
if def, ok := ins.(IrDefinitions); ok {
for _, d := range def.Definitions() {
if k := d.Kind(); k != K_zero {
orig[p.Id] = appendReg(orig[p.Id], *d)
}
}
}
}
}
/* find out all the variable defination sites */
for q.Enqueue(cfg.Root); !q.Empty(); {
p := q.Dequeue().(*BasicBlock)
addImmediateDominated(cfg.DominatorOf, p, q)
/* mark all the defination sites */
for def := range orig[p.Id] {
defs[def] = appendBlock(defs[def], p)
}
}
/* reserve buffer for Phi descriptors */
nb := len(defs)
pd := make([]_PhiDesc, nb)
/* dump the descriptors */
for r, v := range defs {
n := len(v)
b := make([]*BasicBlock, 0, n)
/* dump the blocks */
for _, p := range v {
b = append(b, p)
}
/* sort blocks by ID */
sort.Slice(b, func(i int, j int) bool {
return b[i].Id < b[j].Id
})
/* add the descriptor */
pd = append(pd, _PhiDesc {
r: r,
b: b,
})
}
/* sort descriptors by register */
sort.Slice(pd, func(i int, j int) bool {
return pd[i].r < pd[j].r
})
/* insert Phi node for every variable */
for _, p := range pd {
for len(p.b) != 0 {
n := p.b[0]
p.b = p.b[1:]
/* insert Phi nodes */
for _, y := range cfg.DominanceFrontier[n.Id] {
if rem := phi[p.r]; !rem[y.Id] {
id := y.Id
src := make(map[*BasicBlock]*Reg)
/* mark as processed */
if rem != nil {
rem[id] = true
} else {
phi[p.r] = map[int]bool { id: true }
}
/* build the Phi node args */
for _, pred := range y.Pred {
src[pred] = new(Reg)
*src[pred] = p.r
}
/* insert a new Phi node */
y.Phi = append(y.Phi, &IrPhi {
R: p.r,
V: src,
})
/* a node may contain both an ordinary definition and a
* Phi node for the same variable */
if !orig[y.Id][p.r] {
p.b = append(p.b, y)
}
}
}
}
}
}