[be] Move buildAliasSets to DisjointSet
Sathya Gunasekaran committed
Dec 12, 2022 at 20:35 UTC
e2e5e389af35ae9223a99acc09cd8da651240467
4 files changed
+24
-28
compiler/forget/src/HIR/DisjointSet.ts
+22
@@ -84,6 +84,28 @@ export default class DisjointSet<T> {
84
}
85
}
86
87
+ buildSets(): Array<Set<T>> {
88
+ const ids: Map<T, number> = new Map();
89
+ const sets: Map<number, Set<T>> = new Map();
90
+
91
+ this.forEach((identifier, groupIdentifier) => {
92
+ let id = ids.get(groupIdentifier);
93
+ if (id == null) {
94
+ id = ids.size;
95
+ ids.set(groupIdentifier, id);
96
+ }
97
+
98
+ let set = sets.get(id);
99
+ if (set === undefined) {
100
+ set = new Set();
101
+ sets.set(id, set);
102
+ }
103
+ set.add(identifier);
104
+ });
105
+
106
+ return [...sets.values()];
107
+ }
108
+
109
get size(): number {
110
return this.#entries.size;
111
}
compiler/forget/src/HIR/InferAlias.ts
-24
@@ -15,30 +15,6 @@ type AbstractPrimitive = {
15
16
export type AliasSet = Set<Identifier>;
17
18
-export function buildAliasSets(
19
- aliases: DisjointSet<Identifier>
20
-): Array<AliasSet> {
21
- const aliasIds: Map<Identifier, number> = new Map();
22
- const aliasSets: Map<number, Set<Identifier>> = new Map();
23
-
24
- aliases.forEach((identifier, groupIdentifier) => {
25
- let aliasId = aliasIds.get(groupIdentifier);
26
- if (aliasId == null) {
27
- aliasId = aliasIds.size;
28
- aliasIds.set(groupIdentifier, aliasId);
29
- }
30
-
31
- let aliasSet = aliasSets.get(aliasId);
32
- if (aliasSet === undefined) {
33
- aliasSet = new Set();
34
- aliasSets.set(aliasId, aliasSet);
35
- }
36
- aliasSet.add(identifier);
37
- });
38
-
39
- return [...aliasSets.values()];
40
-}
41
-
18
class AbstractState {
19
aliases = new DisjointSet<Identifier>();
20
// NOTE(gsn): Should this be a part of AbstractObject? No, because this has
compiler/forget/src/HIR/InferMutableRangesForAlias.ts
+1
-2
@@ -1,9 +1,8 @@
1
import DisjointSet from "./DisjointSet";
2
import { Identifier, InstructionId } from "./HIR";
3
-import { buildAliasSets } from "./InferAlias";
3
4
export function inferMutableRangesForAlias(aliases: DisjointSet<Identifier>) {
6
- const aliasSets = buildAliasSets(aliases);
5
+ const aliasSets = aliases.buildSets();
6
for (const aliasSet of aliasSets) {
7
// Update mutableRange.end only if the identifiers have actually been
8
// mutated.
compiler/forget/src/HIR/PrintHIR.ts
+1
-2
@@ -23,7 +23,6 @@ import {
23
SourceLocation,
24
Terminal,
25
} from "./HIR";
26
-import { buildAliasSets } from "./InferAlias";
26
import { eachReactiveScope } from "./visitors";
27
28
export type Options = {
@@ -335,7 +334,7 @@ export function printSourceLocation(loc: SourceLocation): string {
334
}
335
336
export function printAliases(aliases: DisjointSet<Identifier>): string {
338
- const aliasSets = buildAliasSets(aliases);
337
+ const aliasSets = aliases.buildSets();
338
339
const items = [];
340
for (const aliasSet of aliasSets) {