[hir] Use type inference for Effect.Store refinement

Only identifiers of type object can be safely refined to Effect.Store, default 
unknown identifiers to Effect.Mutate.
This commit is contained in:
Sathya Gunasekaran
2022-12-21 21:13:58 +00:00
parent ff29264050
commit 6ba2117227
3 changed files with 15 additions and 8 deletions
+4
View File
@@ -522,3 +522,7 @@ function objectTypeEquals(tA: Type, tB: Type): boolean {
function funcTypeEquals(tA: Type, tB: Type): boolean {
return tA.kind === "Function" && tB.kind === "Function";
}
export function isObjectType(id: Identifier): boolean {
return id.type.kind === "Object";
}
@@ -14,6 +14,7 @@ import {
HIRFunction,
IdentifierId,
InstructionValue,
isObjectType,
Phi,
Place,
ValueKind,
@@ -585,12 +586,14 @@ function inferBlock(env: Environment, block: BasicBlock) {
// redefine lvalue: `a = b.c.d`
env.initialize(instrValue, env.kind(instrValue));
env.define(lvalue.place, instrValue);
} else if (instrValue.memberPath === null) {
// no-op: `a.b.c = d`
env.reference(lvalue.place, Effect.Store);
} else {
// no-op: `a.b.c = d`
// or
// no-op: `a.b.c = d.e.f`
env.reference(lvalue.place, Effect.Store);
const effect = isObjectType(lvalue.place.identifier)
? Effect.Store
: Effect.Mutate;
env.reference(lvalue.place, effect);
}
}
continue;
@@ -63,8 +63,8 @@ function f$0() {
```
bb0:
[1] Const mutate $6:TPrimitive = 1
[2] Const mutate $7_@0:TPrimitive[0:6] = Binary read a$5_@0.b.c + read $6:TPrimitive
[3] Reassign store a$5_@0.b.c[0:6] = read $7_@0:TPrimitive
[2] Const mutate $7:TPrimitive = Binary read a$5_@0.b.c + read $6:TPrimitive
[3] Reassign read a$5_@0.b.c[0:6] = read $7:TPrimitive
[4] Const mutate $8:TPrimitive = 2
[5] Reassign mutate a$5_@0.b.c[0:6] = Binary read a$5_@0.b.c * read $8:TPrimitive
[6] Return
@@ -77,8 +77,8 @@ function g(
a,
) {
[1] Const mutate $6:TPrimitive = 1
[2] Const mutate $7_@0:TPrimitive[0:6] = Binary read a$5_@0.b.c + read $6:TPrimitive
[3] Reassign store a$5_@0.b.c[0:6] = read $7_@0:TPrimitive
[2] Const mutate $7:TPrimitive = Binary read a$5_@0.b.c + read $6:TPrimitive
[3] Reassign read a$5_@0.b.c[0:6] = read $7:TPrimitive
[4] Const mutate $8:TPrimitive = 2
[5] Reassign mutate a$5_@0.b.c[0:6] = Binary read a$5_@0.b.c * read $8:TPrimitive
return