diff --git a/compiler/forget/src/Inference/InferAliasForPhis.ts b/compiler/forget/src/Inference/InferAliasForPhis.ts new file mode 100644 index 0000000000..0059cd3003 --- /dev/null +++ b/compiler/forget/src/Inference/InferAliasForPhis.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +import { HIRFunction, Identifier } from "../HIR/HIR"; +import DisjointSet from "../Utils/DisjointSet"; + +export function inferAliasForPhis( + func: HIRFunction, + aliases: DisjointSet +) { + for (const [_, block] of func.body.blocks) { + for (const phi of block.phis) { + const isPhiMutatedAfterCreation: boolean = + phi.id.mutableRange.end > + (block.instructions.at(0)?.id ?? block.terminal.id); + if (isPhiMutatedAfterCreation) { + for (const [, operand] of phi.operands) { + aliases.union([phi.id, operand]); + } + } + } + } +} diff --git a/compiler/forget/src/Inference/InferMutableRanges.ts b/compiler/forget/src/Inference/InferMutableRanges.ts index 0253fe2845..c5c168371f 100644 --- a/compiler/forget/src/Inference/InferMutableRanges.ts +++ b/compiler/forget/src/Inference/InferMutableRanges.ts @@ -7,6 +7,7 @@ import { HIRFunction, Identifier } from "../HIR/HIR"; import { inferAliases } from "./InferAlias"; +import { inferAliasForPhis } from "./InferAliasForPhis"; import { inferAliasForStores } from "./InferAliasForStores"; import { inferMutableLifetimes } from "./InferMutableLifetimes"; import { inferMutableRangesForAlias } from "./InferMutableRangesForAlias"; @@ -27,6 +28,9 @@ export function inferMutableRanges(ir: HIRFunction) { // Update aliasing information of fields inferAliasForStores(ir, aliases); + // Update aliasing information of phis + inferAliasForPhis(ir, aliases); + const nextAliases = aliases.canonicalize(); if (areEqualMaps(prevAliases, nextAliases)) { break; diff --git a/compiler/forget/src/Inference/InferMutableRangesForAlias.ts b/compiler/forget/src/Inference/InferMutableRangesForAlias.ts index 95b96c8ef7..4de85bec4b 100644 --- a/compiler/forget/src/Inference/InferMutableRangesForAlias.ts +++ b/compiler/forget/src/Inference/InferMutableRangesForAlias.ts @@ -31,17 +31,4 @@ export function inferMutableRangesForAlias( } } } - - for (const [_, block] of fn.body.blocks) { - for (const phi of block.phis) { - const isPhiMutatedAfterCreation: boolean = - phi.id.mutableRange.end > - (block.instructions.at(0)?.id ?? block.terminal.id); - if (isPhiMutatedAfterCreation) { - for (const [, operand] of phi.operands) { - aliases.union([phi.id, operand]); - } - } - } - } }