-
Notifications
You must be signed in to change notification settings - Fork 0
/
LivenessAnalyser.java
287 lines (228 loc) · 9.84 KB
/
LivenessAnalyser.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package pt.up.fe.comp.ollir.optimization;
import org.specs.comp.ollir.*;
import java.util.*;
public class LivenessAnalyser {
private final Map<Node, BitSet> defined;
private final Map<Node, BitSet> used;
private final Map<Node, BitSet> inAlive;
private final Map<Node, BitSet> outAlive;
private final List<Instruction> nodes;
private final int varNum;
public LivenessAnalyser(Method method) {
defined = new HashMap<>();
used = new HashMap<>();
inAlive = new HashMap<>();
outAlive = new HashMap<>();
varNum = method.getVarTable().size();
for (Instruction instruction : method.getInstructions()) {
defined.put(instruction, getDefinedVars(instruction, method.getVarTable()));
used.put(instruction, getUsedVars(instruction, method.getVarTable()));
inAlive.put(instruction, new BitSet(varNum));
outAlive.put(instruction, new BitSet(varNum));
}
nodes = new ArrayList<>(method.getInstructions());
Collections.reverse(nodes);
}
public void analyse(boolean debug) {
if (debug) {
System.out.println("LIVENESS ANALYSIS:");
}
int iterNum = 0;
while (true) {
if (debug) {
showMaps(iterNum);
}
Map<Node, BitSet> inAlivePrev = new HashMap<>(inAlive);
Map<Node, BitSet> outAlivePrev = new HashMap<>(outAlive);
for (Instruction node : nodes) {
BitSet outAliveNew = new BitSet(varNum);
if (node.getSucc1() != null) {
if (node.getSucc1().getNodeType() != NodeType.END) {
outAliveNew = (BitSet) inAlive.get(node.getSucc1()).clone();
if (node.getSucc2() != null) {
outAliveNew.or(inAlive.get(node.getSucc2()));
}
}
}
outAlive.replace(node, outAliveNew);
BitSet inAliveNew = (BitSet) outAlive.get(node).clone();
BitSet def = defined.get(node);
for (int i = 0; i < inAliveNew.length(); i++) {
if (inAliveNew.get(i) && def.get(i)) {
inAliveNew.clear(i);
}
}
inAliveNew.or(used.get(node));
inAlive.replace(node, inAliveNew);
}
if (isDone(inAlivePrev, outAlivePrev)) {
break;
}
iterNum++;
}
iterNum++;
if (debug) {
showMaps(iterNum);
}
}
private boolean isDone(Map<Node, BitSet> inAlivePrev, Map<Node, BitSet> outAlivePrev) {
for (Instruction instruction : nodes) {
if (!inAlive.get(instruction).equals(inAlivePrev.get(instruction)) ||
!outAlive.get(instruction).equals(outAlivePrev.get(instruction))) {
return false;
}
}
return true;
}
public Map<Node, BitSet> getInAlive() {
return inAlive;
}
public Map<Node, BitSet> getOutAlive() {
return outAlive;
}
public Map<Node, BitSet> getDefined() {
return this.defined;
}
private BitSet getUsedVars(Instruction instruction, Map<String, Descriptor> varTable) {
switch (instruction.getInstType()) {
case UNARYOPER:
return getUsedVars((UnaryOpInstruction) instruction, varTable);
case BINARYOPER:
return getUsedVars((BinaryOpInstruction) instruction, varTable);
case NOPER:
return getUsedVars((SingleOpInstruction) instruction, varTable);
case ASSIGN:
return getUsedVars((AssignInstruction) instruction, varTable);
case CALL:
return getUsedVars((CallInstruction) instruction, varTable);
case BRANCH:
return getUsedVars((CondBranchInstruction) instruction, varTable);
case RETURN:
return getUsedVars((ReturnInstruction) instruction, varTable);
case GETFIELD:
return getUsedVars((GetFieldInstruction) instruction, varTable);
case PUTFIELD:
return getUsedVars((PutFieldInstruction) instruction, varTable);
default:
break;
}
return new BitSet();
}
private BitSet getUsedVars(UnaryOpInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
setElement(vars, instruction.getOperand(), varTable);
return vars;
}
private BitSet getUsedVars(BinaryOpInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
setElement(vars, instruction.getRightOperand(), varTable);
setElement(vars, instruction.getLeftOperand(), varTable);
return vars;
}
private BitSet getUsedVars(SingleOpInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
setElement(vars, instruction.getSingleOperand(), varTable);
return vars;
}
private BitSet getUsedVars(AssignInstruction instruction, Map<String, Descriptor> varTable) {
Operand dest = (Operand) instruction.getDest();
Descriptor descriptor = varTable.get(dest.getName());
BitSet vars = new BitSet();
// array[index] = ...
// 'array' and 'index' are used
if (descriptor.getVarType().getTypeOfElement() == ElementType.ARRAYREF
&& dest.getType().getTypeOfElement() == ElementType.INT32) {
for (Element index : ((ArrayOperand) dest).getIndexOperands()) {
setElement(vars, index, varTable);
}
setElement(vars, dest, varTable);
}
vars.or(getUsedVars(instruction.getRhs(), varTable));
return vars;
}
private BitSet getUsedVars(CallInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
CallType callType = instruction.getInvocationType();
if (callType.equals(CallType.invokevirtual) || callType.equals(CallType.invokespecial) || callType.equals(CallType.arraylength)) {
setElement(vars, instruction.getFirstArg(), varTable);
}
if (instruction.getNumOperands() > 1) {
if (instruction.getInvocationType() != CallType.NEW) {
setElement(vars, instruction.getSecondArg(), varTable);
}
for (Element arg : instruction.getListOfOperands()) {
setElement(vars, arg, varTable);
}
}
return vars;
}
private BitSet getUsedVars(CondBranchInstruction instruction, Map<String, Descriptor> varTable) {
return getUsedVars(instruction.getCondition(), varTable);
}
private BitSet getUsedVars(ReturnInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
if (instruction.hasReturnValue()) {
setElement(vars, instruction.getOperand(), varTable);
}
return vars;
}
private BitSet getUsedVars(GetFieldInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
setElement(vars, instruction.getFirstOperand(), varTable);
return vars;
}
private BitSet getUsedVars(PutFieldInstruction instruction, Map<String, Descriptor> varTable) {
BitSet vars = new BitSet();
setElement(vars, instruction.getFirstOperand(), varTable);
setElement(vars, instruction.getThirdOperand(), varTable);
return vars;
}
private BitSet getDefinedVars(Instruction instruction, HashMap<String, Descriptor> varTable) {
BitSet vars = new BitSet();
if (instruction.getInstType() == InstructionType.ASSIGN) {
setElement(vars, ((AssignInstruction) instruction).getDest(), varTable, false);
} else if (instruction.getInstType() == InstructionType.PUTFIELD) {
setElement(vars, ((PutFieldInstruction) instruction).getFirstOperand(), varTable, false);
}
return vars;
}
private void setElement(BitSet vars, Element element, Map<String, Descriptor> varTable) {
setElement(vars, element, varTable, true);
}
private void setElement(BitSet vars, Element element, Map<String, Descriptor> varTable, boolean getUsed) {
if (element.isLiteral()) {
return;
}
if (element.getType().getTypeOfElement() == ElementType.THIS
|| (element.getType().getTypeOfElement() == ElementType.OBJECTREF && ((Operand) element).getName().equals("this"))) {
vars.set(0);
return;
}
Descriptor descriptor = varTable.get(((Operand) element).getName());
if (getUsed) {
// array[index], set 'index' as used also
if (descriptor.getVarType().getTypeOfElement() == ElementType.ARRAYREF
&& element.getType().getTypeOfElement() == ElementType.INT32) {
for (Element index : ((ArrayOperand) element).getIndexOperands()) {
setElement(vars, index, varTable);
}
}
}
if (descriptor.getScope() == VarScope.PARAMETER ||
descriptor.getScope() == VarScope.FIELD) {
return;
}
int reg = descriptor.getVirtualReg();
vars.set(reg);
}
public void showMaps(int iterNum) {
System.out.println("\nIteration: (" + iterNum + ")");
for (var v : nodes) {
System.out.println("KEY: " + v.getId());
System.out.println("DEF : " + defined.get(v));
System.out.println("USED : " + used.get(v));
System.out.println("IN ALIVE : " + inAlive.get(v));
System.out.println("OUT ALIVE: " + outAlive.get(v));
}
}
}