@@ -51,56 +51,32 @@ namespace B3FixSSAInternal {
5151static constexpr bool verbose = false ;
5252}
5353
54- bool killDeadVariables (Procedure& proc)
55- {
56- IndexSet<Variable*> liveVariables;
57- for (Value* value : proc.values ()) {
58- if (value->opcode () == Get)
59- liveVariables.add (value->as <VariableValue>()->variable ());
60- }
61-
62- for (Value* value : proc.values ()) {
63- if (value->opcode () == Set && !liveVariables.contains (value->as <VariableValue>()->variable ()))
64- value->replaceWithNop ();
65- }
66-
67- if (liveVariables.isEmpty ()) {
68- proc.deleteAllVariables ();
69- return true ;
70- }
71-
72- for (Variable* variable : proc.variables ()) {
73- if (!liveVariables.contains (variable))
74- proc.deleteVariable (variable);
75- }
76- return false ;
77- }
78-
79- void fixSSALocally (Procedure& proc)
54+ // Many variables often have trivial local liveness (i.e. within a single block), which we can resolve
55+ // without global SSA. Returns true if no variables remain.
56+ bool fixSSALocally (Procedure& proc, IndexSet<Value*>& valuesToDelete)
8057{
8158 IndexSparseSet<KeyValuePair<unsigned , Value*>> mapping (proc.variables ().size ());
82- for (BasicBlock* block : proc.blocksInPreOrder ()) {
59+ IndexSet<Variable*> liveVariables;
60+ Vector<Value*> sets;
61+ for (BasicBlock* block : proc) {
8362 mapping.clear ();
8463
85- for (unsigned valueIndex = 0 ; valueIndex < block->size (); ++valueIndex) {
86- Value* value = block->at (valueIndex);
87- value->performSubstitution ();
88-
64+ for (Value* value : *block) {
8965 switch (value->opcode ()) {
9066 case Get: {
91- VariableValue* variableValue = value->as <VariableValue>();
92- Variable* variable = variableValue->variable ();
93-
94- if (KeyValuePair<unsigned , Value*>* replacement = mapping.get (variable->index ()))
67+ Variable* variable = value->as <VariableValue>()->variable ();
68+ if (KeyValuePair<unsigned , Value*>* replacement = mapping.get (variable->index ())) {
9569 value->replaceWithIdentity (replacement->value );
70+ valuesToDelete.add (value);
71+ } else
72+ liveVariables.add (variable);
9673 break ;
9774 }
98-
99- case Set: {
100- VariableValue* variableValue = value->as <VariableValue>();
101- Variable* variable = variableValue->variable ();
10275
103- mapping.set (variable->index (), value->child (0 ));
76+ case Set: {
77+ Variable* variable = value->as <VariableValue>()->variable ();
78+ mapping.set (variable->index (), value->child (0 )->foldIdentity ());
79+ sets.append (value);
10480 break ;
10581 }
10682
@@ -109,9 +85,25 @@ void fixSSALocally(Procedure& proc)
10985 }
11086 }
11187 }
88+
89+ for (Value* set : sets) {
90+ if (!liveVariables.contains (set->as <VariableValue>()->variable ()))
91+ set->replaceWithNop ();
92+ }
93+
94+ if (liveVariables.isEmpty ()) {
95+ proc.deleteAllVariables ();
96+ return true ;
97+ }
98+
99+ for (Variable* variable : proc.variables ()) {
100+ if (!liveVariables.contains (variable))
101+ proc.deleteVariable (variable);
102+ }
103+ return false ;
112104}
113105
114- void fixSSAGlobally (Procedure& proc)
106+ void fixSSAGlobally (Procedure& proc, IndexSet<Value*>& valuesToDelete )
115107{
116108 VariableLiveness liveness (proc);
117109
@@ -141,24 +133,21 @@ void fixSSAGlobally(Procedure& proc)
141133 variableToCalcVar[variable] = calcVar;
142134 }
143135
144- // Create Defs for all of the stores to the stack variable.
145136 for (BasicBlock* block : proc) {
146137 for (Value* value : *block) {
147138 if (value->opcode () != Set)
148139 continue ;
149140
150141 Variable* variable = value->as <VariableValue>()->variable ();
151-
152- if (SSACalculator::Variable* calcVar = variableToCalcVar[variable])
153- ssa.newDef (calcVar, block, value->child (0 ));
142+ ssa.newDefBlock (variableToCalcVar[variable], block);
154143 }
155144 }
156145
157146 // Decide where Phis are to be inserted. This creates them but does not insert them.
158147 {
159148 CompilerTimingScope timingScope (" B3" _s, " fixSSA: computePhis" _s);
160149 ssa.computePhis (
161- [&] (SSACalculator::Variable* calcVar, BasicBlock* block) -> Value* {
150+ [&](SSACalculator::Variable* calcVar, BasicBlock* block) -> Value* {
162151 Variable* variable = calcVarToVariable[calcVar->index ()];
163152 if (!liveAtHead.isLiveAtHead (block, variable))
164153 return nullptr ;
@@ -176,96 +165,98 @@ void fixSSAGlobally(Procedure& proc)
176165 // Now perform the conversion.
177166 CompilerTimingScope timingScope (" B3" _s, " fixSSA: convert" _s);
178167 InsertionSet insertionSet (proc);
179- IndexSparseSet<KeyValuePair<unsigned , Value*>> mapping (proc.variables ().size ());
180- IndexSet<Value*> valuesToDelete;
181- for (BasicBlock* block : proc.blocksInPreOrder ()) {
182- mapping.clear ();
183-
184- auto ensureMapping = [&] (Variable* variable, unsigned valueIndex, Origin origin) -> Value* {
185- KeyValuePair<unsigned , Value*>* found = mapping.get (variable->index ());
186- if (found)
187- return found->value ;
188-
189- SSACalculator::Variable* calcVar = variableToCalcVar[variable];
190- SSACalculator::Def* def = ssa.reachingDefAtHead (block, calcVar);
191- if (def) {
192- mapping.set (variable->index (), def->value ());
193- return def->value ();
194- }
195-
196- return insertionSet.insertBottom (valueIndex, origin, variable->type ());
197- };
198168
199- for (SSACalculator::Def* phiDef : ssa.phisForBlock (block)) {
200- Variable* variable = calcVarToVariable[phiDef->variable ()->index ()];
169+ Vector<Value*> reachingDefs (FillWith { }, proc.variables ().size (), nullptr );
170+ Vector<std::pair<unsigned , Value*>> undoLog;
171+ Vector<unsigned , 16 > undoLogSizeAtEntry;
172+ auto setReachingDef = [&](Variable* variable, Value* value) {
173+ undoLog.append ({ variable->index (), reachingDefs[variable->index ()] });
174+ reachingDefs[variable->index ()] = value;
175+ };
176+
177+ proc.dominators ().forAllBlocksInDominatorTreePreOrder (
178+ [&](BasicBlock* block) {
179+ undoLogSizeAtEntry.append (undoLog.size ());
180+
181+ auto reachingDef = [&](Variable* variable, unsigned valueIndex, Origin origin) -> Value* {
182+ if (Value* def = reachingDefs[variable->index ()])
183+ return def;
184+ Value* bottom = insertionSet.insertBottom (valueIndex, origin, variable->type ());
185+ setReachingDef (variable, bottom);
186+ return bottom;
187+ };
188+
189+ for (SSACalculator::Def* phiDef : ssa.phisForBlock (block)) {
190+ Variable* variable = calcVarToVariable[phiDef->variable ()->index ()];
191+
192+ insertionSet.insertValue (0 , phiDef->value ());
193+ setReachingDef (variable, phiDef->value ());
194+ }
201195
202- insertionSet. insertValue ( 0 , phiDef-> value ());
203- mapping. set (variable-> index (), phiDef-> value () );
204- }
196+ for ( unsigned valueIndex = 0 ; valueIndex < block-> size (); ++valueIndex) {
197+ Value* value = block-> at (valueIndex );
198+ value-> performSubstitution ();
205199
206- for (unsigned valueIndex = 0 ; valueIndex < block->size (); ++valueIndex) {
207- Value* value = block->at (valueIndex);
208- value->performSubstitution ();
200+ switch (value->opcode ()) {
201+ case Get: {
202+ Variable* variable = value->as <VariableValue>()->variable ();
203+ value->replaceWithIdentity (reachingDef (variable, valueIndex, value->origin ()));
204+ valuesToDelete.add (value);
205+ break ;
206+ }
209207
210- switch (value->opcode ()) {
211- case Get: {
212- VariableValue* variableValue = value->as <VariableValue>();
213- Variable* variable = variableValue->variable ();
208+ case Set: {
209+ Variable* variable = value->as <VariableValue>()->variable ();
210+ setReachingDef (variable, value->child (0 ));
211+ value->replaceWithNop ();
212+ break ;
213+ }
214214
215- value-> replaceWithIdentity ( ensureMapping (variable, valueIndex, value-> origin ()));
216- valuesToDelete. add (value) ;
217- break ;
215+ default :
216+ break ;
217+ }
218218 }
219-
220- case Set: {
221- VariableValue* variableValue = value->as <VariableValue>();
222- Variable* variable = variableValue->variable ();
223219
224- mapping.set (variable->index (), value->child (0 ));
225- value->replaceWithNop ();
226- break ;
227- }
220+ unsigned upsilonInsertionPoint = block->size () - 1 ;
221+ Origin upsilonOrigin = block->last ()->origin ();
222+ for (BasicBlock* successorBlock : block->successorBlocks ()) {
223+ for (SSACalculator::Def* phiDef : ssa.phisForBlock (successorBlock)) {
224+ Value* phi = phiDef->value ();
225+ Variable* variable = calcVarToVariable[phiDef->variable ()->index ()];
226+
227+ Value* mappedValue = reachingDef (variable, upsilonInsertionPoint, upsilonOrigin);
228+ if (B3FixSSAInternal::verbose) {
229+ dataLog (
230+ " Mapped value for " , *variable, " with successor Phi " , *phi,
231+ " at end of " , *block, " : " , pointerDump (mappedValue), " \n " );
232+ }
228233
229- default :
230- break ;
234+ insertionSet.insert <UpsilonValue>(
235+ upsilonInsertionPoint, upsilonOrigin, mappedValue->foldIdentity (), phi);
236+ }
231237 }
232- }
233238
234- unsigned upsilonInsertionPoint = block->size () - 1 ;
235- Origin upsilonOrigin = block->last ()->origin ();
236- for (BasicBlock* successorBlock : block->successorBlocks ()) {
237- for (SSACalculator::Def* phiDef : ssa.phisForBlock (successorBlock)) {
238- Value* phi = phiDef->value ();
239- SSACalculator::Variable* calcVar = phiDef->variable ();
240- Variable* variable = calcVarToVariable[calcVar->index ()];
241-
242- Value* mappedValue = ensureMapping (variable, upsilonInsertionPoint, upsilonOrigin);
243- if (B3FixSSAInternal::verbose) {
244- dataLog (
245- " Mapped value for " , *variable, " with successor Phi " , *phi,
246- " at end of " , *block, " : " , pointerDump (mappedValue), " \n " );
247- }
248-
249- insertionSet.insert <UpsilonValue>(
250- upsilonInsertionPoint, upsilonOrigin, mappedValue->foldIdentity (), phi);
239+ insertionSet.execute (block);
240+ },
241+ [&](BasicBlock* block) {
242+ unsigned undoLogSize = undoLogSizeAtEntry.takeLast ();
243+ while (undoLog.size () > undoLogSize) {
244+ auto [index, previous] = undoLog.takeLast ();
245+ reachingDefs[index] = previous;
251246 }
252- }
253247
254- insertionSet.execute (block);
255- }
256-
257- // This is isn't strictly necessary, but it leaves the IR nice and tidy, which is particularly
258- // useful for phases that do size estimates.
259- for (BasicBlock* block : proc) {
260- block->values ().removeAllMatching (
261- [&] (Value* value) -> bool {
262- if (!valuesToDelete.contains (value) && value->opcode () != Nop)
263- return false ;
264-
265- proc.deleteValue (value);
266- return true ;
267- });
268- }
248+ // Now that we've visited every block these `Get`s dominate, and all their users have been
249+ // substituted, we can delete the `Get`. This isn't strictly necessary, but it leaves the IR
250+ // tidy for phases that do size estimates.
251+ block->values ().removeAllMatching (
252+ [&](Value* value) -> bool {
253+ if (!valuesToDelete.remove (value) && value->opcode () != Nop)
254+ return false ;
255+
256+ proc.deleteValue (value);
257+ return true ;
258+ });
259+ });
269260
270261 if (B3FixSSAInternal::verbose) {
271262 dataLog (" B3 after SSA conversion:\n " );
@@ -360,19 +351,27 @@ bool fixSSA(Procedure& proc)
360351 if (proc.variables ().isEmpty ())
361352 return false ;
362353
363- // Lots of variables have trivial local liveness. We can allocate those without any
364- // trouble.
365- fixSSALocally (proc);
366-
367- // Just for sanity, remove any unused variables first. It's unlikely that this code has any
368- // bugs having to do with dead variables, but it would be silly to have to fix such a bug if
369- // it did arise.
370- if (killDeadVariables (proc))
354+ IndexSet<Value*> valuesToDelete;
355+ if (fixSSALocally (proc, valuesToDelete)) {
356+ // Users of a forwarded Get can be in any block, so substitute everywhere before deleting.
357+ for (Value* value : proc.values ())
358+ value->performSubstitution ();
359+ for (BasicBlock* block : proc) {
360+ block->values ().removeAllMatching (
361+ [&](Value* value) -> bool {
362+ if (!valuesToDelete.remove (value))
363+ return false ;
364+
365+ proc.deleteValue (value);
366+ return true ;
367+ });
368+ }
371369 return false ;
370+ }
372371
373372 breakCriticalEdges (proc);
374373
375- fixSSAGlobally (proc);
374+ fixSSAGlobally (proc, valuesToDelete );
376375
377376 proc.deleteAllVariables ();
378377
0 commit comments