-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalVariableInterferenceGraph.java
223 lines (183 loc) · 7.46 KB
/
LocalVariableInterferenceGraph.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package pt.up.fe.comp.ollir.optimization;
import org.specs.comp.ollir.*;
import java.util.*;
public class LocalVariableInterferenceGraph {
private static class VarNode {
int ogLocalVariable;
String varName;
Set<VarNode> adjacentNodes;
boolean isActive;
public VarNode(String name, int localVariable) {
ogLocalVariable = localVariable;
varName = name;
adjacentNodes = new HashSet<>();
isActive = true;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VarNode other = (VarNode) o;
return this.ogLocalVariable == other.ogLocalVariable &&
this.varName.equals(other.varName) &&
this.adjacentNodes.equals(other.adjacentNodes);
}
@Override
public int hashCode() {
return Objects.hash(varName, ogLocalVariable);
}
@Override
public String toString() {
return varName + " " + ogLocalVariable;
}
public List<String> getActiveAdjacentNodes() {
List<String> vars = new ArrayList<>();
for (VarNode adj : adjacentNodes) {
if (adj.isActive) {
vars.add(adj.varName);
}
}
return vars;
}
}
private final Map<Integer, VarNode> nodes;
private final Map<String, Descriptor> varTable;
private int minLocalVariables;
private final boolean isStaticMethod;
public LocalVariableInterferenceGraph(Map<Node, BitSet> inAlive, Map<Node, BitSet> outAlive, Map<Node, BitSet> define, Method method) {
nodes = new HashMap<>();
varTable = method.getVarTable();
isStaticMethod = method.isStaticMethod();
minLocalVariables = isStaticMethod ? 0 : 1; // THIS
addNodes();
addEdges(inAlive);
addEdges(outAlive);
addEdges(define, outAlive);
}
private void addNodes() {
for(String name: varTable.keySet()) {
Descriptor descriptor = varTable.get(name);
if (descriptor.getScope() == VarScope.PARAMETER) {
// save local variables for the parameters
minLocalVariables++;
} else if (descriptor.getVarType().getTypeOfElement() != ElementType.THIS
&& descriptor.getScope() != VarScope.FIELD) {
nodes.put(descriptor.getVirtualReg(),
new VarNode(name, descriptor.getVirtualReg()));
}
}
}
// Connect each pair of variables that belong to the same IN or OUT set
private void addEdges(Map<Node, BitSet> liveRange) {
for (Node instruction : liveRange.keySet()) {
BitSet bitset = liveRange.get(instruction);
List<Integer> varIdxs = new ArrayList<>();
for (int i = 0; i < bitset.length(); i++) {
if (bitset.get(i)) {
varIdxs.add(i);
}
}
for (int i = 0; i < varIdxs.size() - 1; i++) {
VarNode node1 = nodes.get(varIdxs.get(i));
for (int j = i + 1; j < varIdxs.size(); j++) {
VarNode node2 = nodes.get(varIdxs.get(j));
if (node1 != null && node2 != null) {
node1.adjacentNodes.add(node2);
node2.adjacentNodes.add(node1);
}
}
}
}
}
// Connect variables in KILL[i] with those in OUT[i]
private void addEdges(Map<Node, BitSet> define, Map<Node, BitSet> outAlive) {
for (Node instruction : define.keySet()) {
BitSet defineBitset = define.get(instruction);
BitSet outBitset = outAlive.get(instruction);
List<Integer> defVarIdxs = new ArrayList<>();
List<Integer> outVarIdxs = new ArrayList<>();
for (int i = 0; i < defineBitset.length(); i++) {
if (defineBitset.get(i)) {
defVarIdxs.add(i);
}
}
for (int i = 0; i < outBitset.length(); i++) {
if (outBitset.get(i)) {
outVarIdxs.add(i);
}
}
for (Integer defVarIdx : defVarIdxs) {
VarNode node1 = nodes.get(defVarIdx);
for (Integer outVarIdx : outVarIdxs) {
VarNode node2 = nodes.get(outVarIdx);
if (node1 != null && node2 != null) {
node1.adjacentNodes.add(node2);
node2.adjacentNodes.add(node1);
}
}
}
}
}
public AllocateVariablesRes allocateLocalVariables(int localVariableNum) {
if (localVariableNum < minLocalVariables) {
return allocateLocalVariables(localVariableNum+1);
}
Stack<VarNode> stack = new Stack<>();
while (!nodes.isEmpty()) {
Iterator<Map.Entry<Integer, VarNode>> it = nodes.entrySet().iterator();
while (it.hasNext()) {
VarNode node = it.next().getValue();
stack.push(node);
node.isActive = false;
it.remove();
}
}
// maps colors to the instructions
Map<Integer, List<String>> localVariables = new HashMap<>();
for (int i = minLocalVariables; i < localVariableNum; i++) {
localVariables.put(i, new ArrayList<>());
}
Map<String, Descriptor> updatedVarTable = new HashMap<>();
while (!stack.isEmpty()) {
VarNode node = stack.pop();
node.isActive = true;
nodes.put(node.ogLocalVariable, node);
if (!assignLocalVariable(localVariables, node, updatedVarTable)) {
while (!stack.isEmpty()) {
VarNode n = stack.pop();
n.isActive = true;
nodes.put(n.ogLocalVariable, n);
}
return allocateLocalVariables(localVariableNum+1);
}
}
int local = isStaticMethod ? 0 : 1;
for (String varName: varTable.keySet()) {
Descriptor d = varTable.get(varName);
if (d.getScope() == VarScope.PARAMETER) {
updatedVarTable.put(varName, new Descriptor(d.getScope(), local, d.getVarType()));
local++;
}
}
return new AllocateVariablesRes(updatedVarTable, localVariableNum);
}
private boolean assignLocalVariable(Map<Integer, List<String>> localVariables, VarNode node, Map<String, Descriptor> updatedVarTable) {
for (Integer local : localVariables.keySet()) {
if (canAssignLocalVariable(localVariables, node, local)) {
localVariables.get(local).add(node.varName);
Descriptor old = varTable.get(node.varName);
updatedVarTable.put(node.varName, new Descriptor(old.getScope(), local, old.getVarType()));
return true;
}
}
return false;
}
private boolean canAssignLocalVariable(Map<Integer, List<String>> localVariables, VarNode node, Integer local) {
for (String adjLocal : node.getActiveAdjacentNodes()) {
if (localVariables.get(local).contains(adjLocal)) {
return false;
}
}
return true;
}
}