[hir] Add support for handling phis in InferReferenceEffects
This just piggybacks on the infrastructure for handling Env.#variables. In the future, a better approach would be to simplify the environment creation and merging by leveraging the SSA property of the new IR -- 1) We don't need to track IdentifierId per environment as they are all unique 2) Rather than tracking values, we can just track Identifiers because Identifiers can never be reassigned.
Sathya Gunasekaran committed
Oct 27, 2022 at 15:16 UTC
6b9f07624f02de3466d896010139160a1439603d
1 file changed
+18
compiler/forget/src/HIR/InferReferenceEffects.ts
+18
@@ -14,6 +14,7 @@ import {
14
HIRFunction,
15
IdentifierId,
16
InstructionValue,
17
+ Phi,
18
Place,
19
Terminal,
20
ValueKind,
@@ -395,6 +396,19 @@ class Environment {
396
}
397
return result;
398
}
399
+
400
+ inferPhi(phi: Phi) {
401
+ const values: Set<InstructionValue> = new Set();
402
+ for (const [_, operand] of phi.operands) {
403
+ const operandValues = this.#variables.get(operand.id);
404
+ // This is a backedge that will be handled later by Environment.merge
405
+ if (operandValues === undefined) continue;
406
+ for (const v of operandValues) {
407
+ values.add(v);
408
+ }
409
+ }
410
+ this.#variables.set(phi.id.id, values);
411
+ }
412
}
413
414
/**
@@ -478,6 +492,10 @@ function mergeValues(a: ValueKind, b: ValueKind): ValueKind {
492
* recording references on the @param env according to JS semantics.
493
*/
494
function inferBlock(env: Environment, block: BasicBlock) {
495
+ for (const phi of block.phis) {
496
+ env.inferPhi(phi);
497
+ }
498
+
499
for (const instr of block.instructions) {
500
const instrValue = instr.value;
501
let valueKind: ValueKind;