-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrilPRE.java
291 lines (256 loc) · 11.4 KB
/
BrilPRE.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
288
289
290
291
import Bril.*;
import PREStruct.*;
import Utils.ExpSet;
import Utils.Util;
import org.json.JSONObject;
import picocli.CommandLine;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.Callable;
import static PREStruct.CFGBlock.linkBlocks;
@CommandLine.Command(name = "BrilPRE", version = "BrilPRE 0.1.0", mixinStandardHelpOptions = true,
description = "A partial redundancy elimination pass for Bril")
public class BrilPRE implements Callable<Integer> {
@CommandLine.Parameters(index = "0", description = "The source code file in JSON.")
File inputFile;
@CommandLine.Parameters(index = "1", description = "The output code file in JSON.")
File outputFile;
public static void main(String[] args) {
int exitCode = new CommandLine(new BrilPRE()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
JSONObject ans = Util.programToJSON(PREPass(Util.parseJson(inputFile)));
Program program = Util.parseJson(inputFile);
//save bytecode into disk
FileWriter out = new FileWriter(outputFile);
out.write(ans.toString(4));
out.close();
return 0;
}
Program PREPass(Program program) {
CFG cfg = generateCFG(program);
System.err.println("finished cfg generation");
//cfg.display();
preprocess(cfg); // precalc usedInBlock, kill;
System.err.println("finished preprocess");
//pass1_anticipated(cfg);
WorkListAnticipated.work(cfg);
System.err.println("finished pass1");
//cfg.displayPRE();
//pass2_earliest(cfg);
WorkListEarliest.work(cfg);
System.err.println("finished pass2");
//cfg.displayPRE();
//pass3_postponable(cfg);
WorkListPostponable.work(cfg);
System.err.println("finished pass3");
//pass4_used(cfg);
WorkListUsed.work(cfg);
System.err.println("finished pass4");
//cfg.displayPRE();
VarNameGenerator vng = new VarNameGenerator(cfg.varSet);
transform(cfg, vng);
//cfg.display();
return cfgToProgram(cfg, vng);
}
Program cfgToProgram(CFG cfg, VarNameGenerator vng) {
Program program = new Program();
ArrayList<Instruction> instrs = new ArrayList<>();
ArrayList<CFGBlock> sortedBlocks = new ArrayList<>();
for (CFGBlock block : cfg.blocks) {
if (block == cfg.exit || block == cfg.entry) continue;
if (block.originalNo < 0) continue;
sortedBlocks.add(block);
for (int i = 0; i < block.succs.size(); ++i) {
CFGBlock succ = block.succs.get(i);
if (succ.originalNo < 0 && succ != cfg.exit) {
Instruction lastInstr = block.instrs.getLast();
if (lastInstr instanceof EffectOperation && ((EffectOperation) lastInstr).op.matches("jmp|br")) {
EffectOperation eo = (EffectOperation) lastInstr;
if (eo.op.equals("jmp")) {
block.instrs.removeLast();
for (Instruction instr : succ.instrs) {
block.instrs.add(instr);
}
block.instrs.add(eo);
linkBlocks(block, succ, succ.succs.get(0));
} else { //br
Label labelInstr = (Label) succ.succs.get(0).instrs.getLast();
String labelName = labelInstr.labelName;
String newLabelName = vng.genLabelName();
Label newLabel = new Label(newLabelName);
succ.instrs.addFirst(newLabel);
for (int bri = 1; bri <= 2; ++bri) {
if (labelName.equals(eo.args.get(bri))) {
eo.args.set(bri, newLabelName);
ArrayList<String> jmpArgs = new ArrayList<>();
jmpArgs.add(labelName);
Instruction newJmp = new EffectOperation("jmp", jmpArgs);
succ.instrs.addLast(newJmp);
break;
}
}
sortedBlocks.add(succ);
}
} else { // no jump
for (Instruction instr : succ.instrs) {
block.instrs.add(instr);
}
linkBlocks(block, succ, succ.succs.get(0));
}
}
}
}
// System.err.println("final block seq: ");
for (CFGBlock block : sortedBlocks) {
//block.display();
instrs.addAll(block.instrs);
}
Function funcMain = new Function("main", instrs);
program.functions.add(funcMain);
return program;
}
void transform(CFG cfg, VarNameGenerator vng) {
for (CFGBlock block : cfg.blocks) {
ExpSet tempExps = ExpSet.intersect(block.preInfo.latest, block.preInfo.toBeUsed.out);
if (block.instrs.size() > 0) {
Instruction instr = block.instrs.getFirst();
if (instr instanceof ValueOperation) {
ValueOperation vo = (ValueOperation) instr;
if (!vo.opName.equals("id")) {
Expression exp = vo.getExp();
if (!block.preInfo.latest.contains(exp) || block.preInfo.toBeUsed.out.contains(exp)) {
ArrayList<String> newArgs = new ArrayList<>();
newArgs.add(vng.genVarName(exp));
Instruction newInstr = new ValueOperation("id", vo.destName, vo.type, newArgs);
block.instrs.removeFirst();
block.instrs.addFirst(newInstr);
}
}
}
}
for (Expression exp : tempExps.expList) {
String destName = vng.genVarName(exp);
Instruction newInstr = new ValueOperation(exp.opName, destName, exp.type, exp.args);
block.instrs.addFirst(newInstr);
}
}
// remove empty blocks
for (CFGBlock block : cfg.blocks) {
if (block.instrs.size() < 1 && block != cfg.entry && block != cfg.exit) {
// must len(preds) == 1 and len(succs) == 1
CFGBlock pred = block.preds.get(0), succ = block.succs.get(0);
linkBlocks(pred, block, succ);
}
}
}
void preprocess(CFG cfg) {
// precalc usedInBlock, kill
// each block at most contain 1 instr
for (CFGBlock cur : cfg.blocks) {
if (cur.instrs.size() < 1) continue;
Instruction instr = cur.instrs.getFirst();
if (instr instanceof ConstOperation) {
// doesn't affect used, do kill
if (instr instanceof ConstBoolOperation) {
String destName = ((ConstBoolOperation) instr).dest;
cur.preInfo.kill.add(destName);
cfg.varSet.add(destName);
}
else if (instr instanceof ConstIntOperation) { // constintoperation
String destName = ((ConstIntOperation) instr).dest;
cur.preInfo.kill.add(((ConstIntOperation) instr).dest);
cfg.varSet.add(destName);
}
} else if (instr instanceof ValueOperation) {
ValueOperation vo = (ValueOperation) instr;
// uniform exp
// vo.setExp(vo.getExp());
cur.preInfo.kill.add(vo.destName);
if (!vo.opName.equals("id"))
cur.preInfo.usedInBlock.add(vo.getExp());
cfg.varSet.add(vo.destName);
for (String s : vo.args) {
cfg.varSet.add(s);
}
} else {
if (instr instanceof Label) {
cfg.varSet.add(((Label) instr).labelName);
}
// EffectOp doesn't affect anything
}
}
}
CFG generateCFG(Program program) {
// assume only one function main
Function funcMain = program.functions.get(0);
CFG cfg = new CFG();
HashMap<Integer, CFGBlock> indexToBlock = new HashMap<>(); // will not lookup entry
HashMap<String, CFGBlock> labelToBlock = new HashMap<>();
indexToBlock.put(funcMain.instrs.size(), cfg.exit);
// create one block for each instruction (label included)
for (int i = 0; i < funcMain.instrs.size(); ++i) {
CFGBlock newBlock = new CFGBlock();
newBlock.originalNo = i;
indexToBlock.put(i, newBlock);
Instruction ins = funcMain.instrs.get(i);
// System.err.println(i);
newBlock.instrs.add(ins);
if (ins instanceof Label) {
labelToBlock.put(((Label) ins).labelName, newBlock);
}
cfg.blocks.add(newBlock);
}
cfg.calcAllExp();
//cfg.display();
// link nodes
linkBlocks(cfg.entry, indexToBlock.get(0));
for (int i = 0; i < funcMain.instrs.size(); ++i) {
Instruction ins = funcMain.instrs.get(i);
CFGBlock thisBlock = indexToBlock.get(i);
if (ins instanceof EffectOperation &&
((EffectOperation) ins).op.matches("jmp|br|ret")) {
EffectOperation eo = (EffectOperation) ins;
if (eo.op.equals("br")) {
String l = eo.args.get(1), r = eo.args.get(2);
// System.err.println("br " + l + " " + r);
linkBlocks(thisBlock, labelToBlock.get(l));
linkBlocks(thisBlock, labelToBlock.get(r));
} else if (eo.op.equals("jmp")){ // jmp
String l = eo.args.get(0);
linkBlocks(thisBlock, labelToBlock.get(l));
} else { //ret
linkBlocks(thisBlock, indexToBlock.get(funcMain.instrs.size()));
}
} else {
linkBlocks(thisBlock, indexToBlock.get(i + 1));
}
}
// add empty blocks for edges (a->b) where b has multiple preds
ArrayList<CFGBlock> newBlocks = new ArrayList<>();
for (CFGBlock thisBlock : cfg.blocks) {
if (thisBlock.preds.size() > 1) {
for (int i = 0; i < thisBlock.preds.size(); ++i) {
CFGBlock pred = thisBlock.preds.get(i);
CFGBlock newBlock = new CFGBlock();
for (int j = 0; j < pred.succs.size(); ++j) {
if (pred.succs.get(j) == thisBlock) {
pred.succs.set(j, newBlock);
newBlock.preds.add(pred);
newBlock.succs.add(thisBlock);
thisBlock.preds.set(i, newBlock);
break;
}
}
newBlocks.add(newBlock);
}
}
}
cfg.blocks.addAll(newBlocks);
return cfg;
}
}