[ssa] Remove lambdas for update functions
Rather than passing lambdas above to the builder, pass the builder down and update places.
Sathya Gunasekaran committed
Oct 18, 2022 at 19:30 UTC
eeb01b17e6a4b183bdc10233eb69746dcf592401
1 file changed
+17
-67
compiler/forget/src/HIR/SSAify.ts
+17
-67
@@ -193,10 +193,7 @@ export default function buildSSA(func: HIRFunction, env: Environment) {
193
194
builder.startBlock(block);
195
for (const instr of block.instructions) {
196
- const uses = collectUses(instr);
197
- for (const { place, updateFn } of uses) {
198
- updateFn(builder.getPlace(place));
199
- }
196
+ rewriteUses(instr, builder);
197
198
if (instr.lvalue != null) {
199
const oldPlace = instr.lvalue.place;
@@ -205,11 +202,7 @@ export default function buildSSA(func: HIRFunction, env: Environment) {
202
}
203
}
204
208
- const { uses, outputs } = collectOutputs(block);
209
- for (const { place, updateFn } of uses) {
210
- updateFn(builder.getPlace(place));
211
- }
212
-
205
+ const outputs = rewriteUsesAndCollectOutputs(block, builder);
206
const outputBlocks = outputs.map((id) => func.body.blocks.get(id)!);
207
for (const output of outputBlocks) {
208
let count;
@@ -233,23 +226,17 @@ export default function buildSSA(func: HIRFunction, env: Environment) {
226
visit(func.body.entry);
227
}
228
236
-function collectOutputs(block: BasicBlock): {
237
- uses: Array<Uses>;
238
- outputs: Array<BlockId>;
239
-} {
229
+function rewriteUsesAndCollectOutputs(
230
+ block: BasicBlock,
231
+ builder: SSABuilder
232
+): Array<BlockId> {
233
const outputs: Array<BlockId> = [];
241
- const uses: Array<Uses> = [];
234
const { terminal } = block;
235
switch (terminal.kind) {
236
case "return":
237
case "throw": {
238
if (terminal.value) {
247
- uses.push({
248
- place: terminal.value,
249
- updateFn: (newPlace) => {
250
- terminal.value = newPlace;
251
- },
252
- });
239
+ terminal.value = builder.getPlace(terminal.value);
240
}
241
break;
242
}
@@ -258,33 +245,18 @@ function collectOutputs(block: BasicBlock): {
245
break;
246
}
247
case "if": {
261
- const { test, consequent, alternate } = terminal;
262
- uses.push({
263
- place: test,
264
- updateFn: (newPlace) => {
265
- terminal.test = newPlace;
266
- },
267
- });
248
+ const { consequent, alternate } = terminal;
249
+ terminal.test = builder.getPlace(terminal.test);
250
outputs.push(alternate);
251
outputs.push(consequent);
252
break;
253
}
254
case "switch": {
273
- const { test, cases } = terminal;
274
- uses.push({
275
- place: test,
276
- updateFn: (newPlace) => {
277
- terminal.test = newPlace;
278
- },
279
- });
255
+ const { cases } = terminal;
256
+ terminal.test = builder.getPlace(terminal.test);
257
for (const case_ of [...cases].reverse()) {
258
if (case_.test) {
282
- uses.push({
283
- place: case_.test,
284
- updateFn: (newPlace) => {
285
- case_.test = newPlace;
286
- },
287
- });
259
+ case_.test = builder.getPlace(case_.test);
260
}
261
outputs.push(case_.block);
262
}
@@ -298,44 +270,22 @@ function collectOutputs(block: BasicBlock): {
270
}
271
}
272
301
- return { uses, outputs };
273
+ return outputs;
274
}
275
304
-type Uses = {
305
- place: Place;
306
- updateFn: (place: Place) => void;
307
-};
308
-
309
-function collectUses(instr: Instruction): Array<Uses> {
310
- const uses: Array<Uses> = [];
276
+function rewriteUses(instr: Instruction, builder: SSABuilder) {
277
const instrValue = instr.value;
278
279
// TODO(gsn): Handle more kinds of Instructions
280
switch (instrValue.kind) {
281
case "BinaryExpression": {
316
- uses.push({
317
- place: instrValue.left,
318
- updateFn: (newPlace) => {
319
- instrValue.left = newPlace;
320
- },
321
- });
322
- uses.push({
323
- place: instrValue.right,
324
- updateFn: (newPlace) => {
325
- instrValue.right = newPlace;
326
- },
327
- });
282
+ instrValue.left = builder.getPlace(instrValue.left);
283
+ instrValue.right = builder.getPlace(instrValue.right);
284
break;
285
}
286
case "Identifier": {
331
- uses.push({
332
- place: instrValue,
333
- updateFn: (newPlace) => {
334
- instr.value = newPlace;
335
- },
336
- });
287
+ instr.value = builder.getPlace(instrValue);
288
break;
289
}
290
}
340
- return uses;
291
}