Remove early SSA phi optimzation
Reverts #726 which added an early optimization to the SSAify pass in skipping over phi creation if only one unique operand. This is no longer necessary with the addition of a phi elimination pass added in #739.
Lauren Tan committed
Nov 7, 2022 at 12:25 UTC
4d9022a17ce369067660319168d8357c0648f6c0
1 file changed
+5
-36
compiler/forget/src/HIR/SSAify.ts
+5
-36
@@ -92,7 +92,7 @@ class SSABuilder {
92
if (this.unsealedPreds.get(block)! > 0) {
93
// We haven't visited all our predecessors, let's place an incomplete phi
94
// for now.
95
- const newId = { ...oldId, id: this.nextIdentifierId };
95
+ const newId = this.makeId(oldId);
96
state.incompletePhis.push({ oldId, newId });
97
state.defs.set(oldId, newId);
98
return newId;
@@ -107,47 +107,15 @@ class SSABuilder {
107
}
108
109
// There are multiple predecessors, we may need a phi.
110
- return this.maybeAddPhi(block, oldId, state);
111
- }
112
-
113
- maybeAddPhi(block: BasicBlock, oldId: Identifier, state: State): Identifier {
110
+ const newId = this.makeId(oldId);
111
// Adding a phi may loop back to our block if there is a loop in the CFG. We
112
// update our defs before adding the phi to terminate the recursion rather than
113
// looping infinitely.
117
- const newId = this.makeId(oldId);
114
state.defs.set(oldId, newId);
119
-
120
- const predDefs: Map<BasicBlock, Identifier> = new Map();
121
- const predIds: Set<Identifier> = new Set();
122
- for (const predBlock of block.preds) {
123
- const predId = this.getIdAt(oldId, predBlock);
124
- predDefs.set(predBlock, predId);
125
- predIds.add(predId);
126
- }
127
-
128
- // if all predecessors have the same id, then there is no need for a phi node.
129
- // note that in the case of a loop there are guaranteed to be multiple values,
130
- // since we have already updated this block with a new identifier to terminate
131
- // the recursion
132
- if (predIds.size === 1) {
133
- // there was only a single incoming id so we don't need a phi node,
134
- // replace with that incoming id instead
135
- const predId = [...predIds][0]!;
136
- state.defs.set(oldId, predId);
137
- return predId;
138
- }
139
-
140
- const phi: Phi = {
141
- kind: "Phi",
142
- id: newId,
143
- operands: predDefs,
144
- };
145
-
146
- block.phis.add(phi);
147
- return newId;
115
+ return this.addPhi(block, oldId, newId);
116
}
117
150
- addPhi(block: BasicBlock, oldId: Identifier, newId: Identifier) {
118
+ addPhi(block: BasicBlock, oldId: Identifier, newId: Identifier): Identifier {
119
const predDefs: Map<BasicBlock, Identifier> = new Map();
120
for (const predBlock of block.preds) {
121
const predId = this.getIdAt(oldId, predBlock);
@@ -161,6 +129,7 @@ class SSABuilder {
129
};
130
131
block.phis.add(phi);
132
+ return newId;
133
}
134
135
fixIncompletePhis(block: BasicBlock) {