Add and use mapTerminalOperands()
Joe Savona committed
Nov 8, 2022 at 14:45 UTC
afabaa0af3fd636a0740e573daf929b8422d98c6
2 files changed
+41
-39
compiler/forget/src/HIR/EnterSSA.ts
+2
-39
@@ -10,7 +10,7 @@ import {
10
} from "./HIR";
11
import { Environment } from "./HIRBuilder";
12
import { printIdentifier } from "./PrintHIR";
13
-import { eachTerminalSuccessor } from "./visitors";
13
+import { eachTerminalSuccessor, mapTerminalOperands } from "./visitors";
14
15
type IncompletePhi = {
16
oldId: Identifier;
@@ -200,7 +200,7 @@ export default function enterSSA(func: HIRFunction, env: Environment) {
200
}
201
}
202
203
- rewriteTerminalOperands(block, builder);
203
+ mapTerminalOperands(block.terminal, (place) => builder.getPlace(place));
204
for (const outputId of eachTerminalSuccessor(block.terminal)) {
205
const output = func.body.blocks.get(outputId)!;
206
let count;
@@ -218,43 +218,6 @@ export default function enterSSA(func: HIRFunction, env: Environment) {
218
}
219
}
220
221
-function rewriteTerminalOperands(block: BasicBlock, builder: SSABuilder): void {
222
- const { terminal } = block;
223
- switch (terminal.kind) {
224
- case "return":
225
- case "throw": {
226
- if (terminal.value) {
227
- terminal.value = builder.getPlace(terminal.value);
228
- }
229
- break;
230
- }
231
- case "goto": {
232
- break;
233
- }
234
- case "if": {
235
- const { consequent, alternate } = terminal;
236
- terminal.test = builder.getPlace(terminal.test);
237
- break;
238
- }
239
- case "switch": {
240
- const { cases } = terminal;
241
- terminal.test = builder.getPlace(terminal.test);
242
- for (const case_ of [...cases]) {
243
- if (case_.test) {
244
- case_.test = builder.getPlace(case_.test);
245
- }
246
- }
247
- break;
248
- }
249
- default: {
250
- assertExhaustive(
251
- terminal,
252
- `Unexpected terminal kind '${(terminal as any).kind}'`
253
- );
254
- }
255
- }
256
-}
257
-
221
function rewriteInstructionUses(instr: Instruction, builder: SSABuilder) {
222
const instrValue = instr.value;
223
compiler/forget/src/HIR/visitors.ts
+39
@@ -166,6 +166,45 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
166
}
167
}
168
169
+export function mapTerminalOperands(
170
+ terminal: Terminal,
171
+ fn: (place: Place) => Place
172
+): void {
173
+ switch (terminal.kind) {
174
+ case "if": {
175
+ terminal.test = fn(terminal.test);
176
+ break;
177
+ }
178
+ case "switch": {
179
+ terminal.test = fn(terminal.test);
180
+ for (const case_ of terminal.cases) {
181
+ if (case_.test === null) {
182
+ continue;
183
+ }
184
+ case_.test = fn(case_.test);
185
+ }
186
+ break;
187
+ }
188
+ case "return":
189
+ case "throw": {
190
+ if (terminal.value !== null) {
191
+ terminal.value = fn(terminal.value);
192
+ }
193
+ break;
194
+ }
195
+ case "goto": {
196
+ // no-op
197
+ break;
198
+ }
199
+ default: {
200
+ assertExhaustive(
201
+ terminal,
202
+ `Unexpected terminal kind '${(terminal as any).kind}'`
203
+ );
204
+ }
205
+ }
206
+}
207
+
208
export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
209
switch (terminal.kind) {
210
case "if": {