1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "jit/ValueNumbering.h"
8 
9 #include "jit/AliasAnalysis.h"
10 #include "jit/IonAnalysis.h"
11 #include "jit/JitSpewer.h"
12 #include "jit/MIRGenerator.h"
13 
14 using namespace js;
15 using namespace js::jit;
16 
17 /*
18  * [SMDOC] IonMonkey Value Numbering
19  *
20  * Some notes on the main algorithm here:
21  *  - The SSA identifier id() is the value number. We do replaceAllUsesWith as
22  *    we go, so there's always at most one visible value with a given number.
23  *
24  *  - Consequently, the GVN algorithm is effectively pessimistic. This means it
25  *    is not as powerful as an optimistic GVN would be, but it is simpler and
26  *    faster.
27  *
28  *  - We iterate in RPO, so that when visiting a block, we've already optimized
29  *    and hashed all values in dominating blocks. With occasional exceptions,
30  *    this allows us to do everything in a single pass.
31  *
32  *  - When we do use multiple passes, we just re-run the algorithm on the whole
33  *    graph instead of doing sparse propagation. This is a tradeoff to keep the
34  *    algorithm simpler and lighter on inputs that don't have a lot of
35  *    interesting unreachable blocks or degenerate loop induction variables, at
36  *    the expense of being slower on inputs that do. The loop for this always
37  *    terminates, because it only iterates when code is or will be removed, so
38  *    eventually it must stop iterating.
39  *
40  *  - Values are not immediately removed from the hash set when they go out of
41  *    scope. Instead, we check for dominance after a lookup. If the dominance
42  *    check fails, the value is removed.
43  */
44 
hash(Lookup ins)45 HashNumber ValueNumberer::VisibleValues::ValueHasher::hash(Lookup ins) {
46   return ins->valueHash();
47 }
48 
49 // Test whether two MDefinitions are congruent.
match(Key k,Lookup l)50 bool ValueNumberer::VisibleValues::ValueHasher::match(Key k, Lookup l) {
51   // If one of the instructions depends on a store, and the other instruction
52   // does not depend on the same store, the instructions are not congruent.
53   if (k->dependency() != l->dependency()) {
54     return false;
55   }
56 
57   bool congruent =
58       k->congruentTo(l);  // Ask the values themselves what they think.
59 #ifdef JS_JITSPEW
60   if (congruent != l->congruentTo(k)) {
61     JitSpew(
62         JitSpew_GVN,
63         "      congruentTo relation is not symmetric between %s%u and %s%u!!",
64         k->opName(), k->id(), l->opName(), l->id());
65   }
66 #endif
67   return congruent;
68 }
69 
rekey(Key & k,Key newKey)70 void ValueNumberer::VisibleValues::ValueHasher::rekey(Key& k, Key newKey) {
71   k = newKey;
72 }
73 
VisibleValues(TempAllocator & alloc)74 ValueNumberer::VisibleValues::VisibleValues(TempAllocator& alloc)
75     : set_(alloc) {}
76 
77 // Look up the first entry for |def|.
findLeader(const MDefinition * def) const78 ValueNumberer::VisibleValues::Ptr ValueNumberer::VisibleValues::findLeader(
79     const MDefinition* def) const {
80   return set_.lookup(def);
81 }
82 
83 // Look up the first entry for |def|.
84 ValueNumberer::VisibleValues::AddPtr
findLeaderForAdd(MDefinition * def)85 ValueNumberer::VisibleValues::findLeaderForAdd(MDefinition* def) {
86   return set_.lookupForAdd(def);
87 }
88 
89 // Insert a value into the set.
add(AddPtr p,MDefinition * def)90 bool ValueNumberer::VisibleValues::add(AddPtr p, MDefinition* def) {
91   return set_.add(p, def);
92 }
93 
94 // Insert a value onto the set overwriting any existing entry.
overwrite(AddPtr p,MDefinition * def)95 void ValueNumberer::VisibleValues::overwrite(AddPtr p, MDefinition* def) {
96   set_.replaceKey(p, def);
97 }
98 
99 // |def| will be discarded, so remove it from any sets.
forget(const MDefinition * def)100 void ValueNumberer::VisibleValues::forget(const MDefinition* def) {
101   Ptr p = set_.lookup(def);
102   if (p && *p == def) {
103     set_.remove(p);
104   }
105 }
106 
107 // Clear all state.
clear()108 void ValueNumberer::VisibleValues::clear() { set_.clear(); }
109 
110 #ifdef DEBUG
111 // Test whether |def| is in the set.
has(const MDefinition * def) const112 bool ValueNumberer::VisibleValues::has(const MDefinition* def) const {
113   Ptr p = set_.lookup(def);
114   return p && *p == def;
115 }
116 #endif
117 
118 // Call MDefinition::justReplaceAllUsesWith, and add some GVN-specific asserts.
ReplaceAllUsesWith(MDefinition * from,MDefinition * to)119 static void ReplaceAllUsesWith(MDefinition* from, MDefinition* to) {
120   MOZ_ASSERT(from != to, "GVN shouldn't try to replace a value with itself");
121   MOZ_ASSERT(from->type() == to->type(), "Def replacement has different type");
122   MOZ_ASSERT(!to->isDiscarded(),
123              "GVN replaces an instruction by a removed instruction");
124 
125   // We don't need the extra setting of UseRemoved flags that the regular
126   // replaceAllUsesWith does because we do it ourselves.
127   from->justReplaceAllUsesWith(to);
128 }
129 
130 // Test whether |succ| is a successor of |block|.
HasSuccessor(const MControlInstruction * block,const MBasicBlock * succ)131 static bool HasSuccessor(const MControlInstruction* block,
132                          const MBasicBlock* succ) {
133   for (size_t i = 0, e = block->numSuccessors(); i != e; ++i) {
134     if (block->getSuccessor(i) == succ) {
135       return true;
136     }
137   }
138   return false;
139 }
140 
141 // Given a block which has had predecessors removed but is still reachable, test
142 // whether the block's new dominator will be closer than its old one and whether
143 // it will expose potential optimization opportunities.
ComputeNewDominator(MBasicBlock * block,MBasicBlock * old)144 static MBasicBlock* ComputeNewDominator(MBasicBlock* block, MBasicBlock* old) {
145   MBasicBlock* now = block->getPredecessor(0);
146   for (size_t i = 1, e = block->numPredecessors(); i < e; ++i) {
147     MBasicBlock* pred = block->getPredecessor(i);
148     // Note that dominators haven't been recomputed yet, so we have to check
149     // whether now dominates pred, not block.
150     while (!now->dominates(pred)) {
151       MBasicBlock* next = now->immediateDominator();
152       if (next == old) {
153         return old;
154       }
155       if (next == now) {
156         MOZ_ASSERT(block == old,
157                    "Non-self-dominating block became self-dominating");
158         return block;
159       }
160       now = next;
161     }
162   }
163   MOZ_ASSERT(old != block || old != now,
164              "Missed self-dominating block staying self-dominating");
165   return now;
166 }
167 
168 // Test for any defs which look potentially interesting to GVN.
BlockHasInterestingDefs(MBasicBlock * block)169 static bool BlockHasInterestingDefs(MBasicBlock* block) {
170   return !block->phisEmpty() || *block->begin() != block->lastIns();
171 }
172 
173 // Walk up the dominator tree from |block| to the root and test for any defs
174 // which look potentially interesting to GVN.
ScanDominatorsForDefs(MBasicBlock * block)175 static bool ScanDominatorsForDefs(MBasicBlock* block) {
176   for (MBasicBlock* i = block;;) {
177     if (BlockHasInterestingDefs(block)) {
178       return true;
179     }
180 
181     MBasicBlock* immediateDominator = i->immediateDominator();
182     if (immediateDominator == i) {
183       break;
184     }
185     i = immediateDominator;
186   }
187   return false;
188 }
189 
190 // Walk up the dominator tree from |now| to |old| and test for any defs which
191 // look potentially interesting to GVN.
ScanDominatorsForDefs(MBasicBlock * now,MBasicBlock * old)192 static bool ScanDominatorsForDefs(MBasicBlock* now, MBasicBlock* old) {
193   MOZ_ASSERT(old->dominates(now),
194              "Refined dominator not dominated by old dominator");
195 
196   for (MBasicBlock* i = now; i != old; i = i->immediateDominator()) {
197     if (BlockHasInterestingDefs(i)) {
198       return true;
199     }
200   }
201   return false;
202 }
203 
204 // Given a block which has had predecessors removed but is still reachable, test
205 // whether the block's new dominator will be closer than its old one and whether
206 // it will expose potential optimization opportunities.
IsDominatorRefined(MBasicBlock * block)207 static bool IsDominatorRefined(MBasicBlock* block) {
208   MBasicBlock* old = block->immediateDominator();
209   MBasicBlock* now = ComputeNewDominator(block, old);
210 
211   // If this block is just a goto and it doesn't dominate its destination,
212   // removing its predecessors won't refine the dominators of anything
213   // interesting.
214   MControlInstruction* control = block->lastIns();
215   if (*block->begin() == control && block->phisEmpty() && control->isGoto() &&
216       !block->dominates(control->toGoto()->target())) {
217     return false;
218   }
219 
220   // We've computed block's new dominator. Test whether there are any
221   // newly-dominating definitions which look interesting.
222   if (block == old) {
223     return block != now && ScanDominatorsForDefs(now);
224   }
225   MOZ_ASSERT(block != now, "Non-self-dominating block became self-dominating");
226   return ScanDominatorsForDefs(now, old);
227 }
228 
229 // |def| has just had one of its users release it. If it's now dead, enqueue it
230 // for discarding, otherwise just make note of it.
handleUseReleased(MDefinition * def,UseRemovedOption useRemovedOption)231 bool ValueNumberer::handleUseReleased(MDefinition* def,
232                                       UseRemovedOption useRemovedOption) {
233   if (IsDiscardable(def)) {
234     values_.forget(def);
235     if (!deadDefs_.append(def)) {
236       return false;
237     }
238   } else {
239     if (useRemovedOption == SetUseRemoved) {
240       def->setUseRemovedUnchecked();
241     }
242   }
243   return true;
244 }
245 
246 // Discard |def| and anything in its use-def subtree which is no longer needed.
discardDefsRecursively(MDefinition * def)247 bool ValueNumberer::discardDefsRecursively(MDefinition* def) {
248   MOZ_ASSERT(deadDefs_.empty(), "deadDefs_ not cleared");
249 
250   return discardDef(def) && processDeadDefs();
251 }
252 
253 // Assuming |resume| is unreachable, release its operands.
254 // It might be nice to integrate this code with prepareForDiscard, however GVN
255 // needs it to call handleUseReleased so that it can observe when a definition
256 // becomes unused, so it isn't trivial to do.
releaseResumePointOperands(MResumePoint * resume)257 bool ValueNumberer::releaseResumePointOperands(MResumePoint* resume) {
258   for (size_t i = 0, e = resume->numOperands(); i < e; ++i) {
259     if (!resume->hasOperand(i)) {
260       continue;
261     }
262     MDefinition* op = resume->getOperand(i);
263     resume->releaseOperand(i);
264 
265     // We set the UseRemoved flag when removing resume point operands,
266     // because even though we may think we're certain that a particular
267     // branch might not be taken, the type information might be incomplete.
268     if (!handleUseReleased(op, SetUseRemoved)) {
269       return false;
270     }
271   }
272   return true;
273 }
274 
275 // Assuming |phi| is dead, release and remove its operands. If an operand
276 // becomes dead, push it to the discard worklist.
releaseAndRemovePhiOperands(MPhi * phi)277 bool ValueNumberer::releaseAndRemovePhiOperands(MPhi* phi) {
278   // MPhi saves operands in a vector so we iterate in reverse.
279   for (int o = phi->numOperands() - 1; o >= 0; --o) {
280     MDefinition* op = phi->getOperand(o);
281     phi->removeOperand(o);
282     if (!handleUseReleased(op, DontSetUseRemoved)) {
283       return false;
284     }
285   }
286   return true;
287 }
288 
289 // Assuming |def| is dead, release its operands. If an operand becomes dead,
290 // push it to the discard worklist.
releaseOperands(MDefinition * def)291 bool ValueNumberer::releaseOperands(MDefinition* def) {
292   for (size_t o = 0, e = def->numOperands(); o < e; ++o) {
293     MDefinition* op = def->getOperand(o);
294     def->releaseOperand(o);
295     if (!handleUseReleased(op, DontSetUseRemoved)) {
296       return false;
297     }
298   }
299   return true;
300 }
301 
302 // Discard |def| and mine its operands for any subsequently dead defs.
discardDef(MDefinition * def)303 bool ValueNumberer::discardDef(MDefinition* def) {
304 #ifdef JS_JITSPEW
305   JitSpew(JitSpew_GVN, "      Discarding %s %s%u",
306           def->block()->isMarked() ? "unreachable" : "dead", def->opName(),
307           def->id());
308 #endif
309 #ifdef DEBUG
310   MOZ_ASSERT(def != nextDef_, "Invalidating the MDefinition iterator");
311   if (def->block()->isMarked()) {
312     MOZ_ASSERT(!def->hasUses(), "Discarding def that still has uses");
313   } else {
314     MOZ_ASSERT(IsDiscardable(def), "Discarding non-discardable definition");
315     MOZ_ASSERT(!values_.has(def), "Discarding a definition still in the set");
316   }
317 #endif
318 
319   MBasicBlock* block = def->block();
320   if (def->isPhi()) {
321     MPhi* phi = def->toPhi();
322     if (!releaseAndRemovePhiOperands(phi)) {
323       return false;
324     }
325     block->discardPhi(phi);
326   } else {
327     MInstruction* ins = def->toInstruction();
328     if (MResumePoint* resume = ins->resumePoint()) {
329       if (!releaseResumePointOperands(resume)) {
330         return false;
331       }
332     }
333     if (!releaseOperands(ins)) {
334       return false;
335     }
336     block->discardIgnoreOperands(ins);
337   }
338 
339   // If that was the last definition in the block, it can be safely removed
340   // from the graph.
341   if (block->phisEmpty() && block->begin() == block->end()) {
342     MOZ_ASSERT(block->isMarked(),
343                "Reachable block lacks at least a control instruction");
344 
345     // As a special case, don't remove a block which is a dominator tree
346     // root so that we don't invalidate the iterator in visitGraph. We'll
347     // check for this and remove it later.
348     if (block->immediateDominator() != block) {
349       JitSpew(JitSpew_GVN, "      Block block%u is now empty; discarding",
350               block->id());
351       graph_.removeBlock(block);
352       blocksRemoved_ = true;
353     } else {
354       JitSpew(JitSpew_GVN,
355               "      Dominator root block%u is now empty; will discard later",
356               block->id());
357     }
358   }
359 
360   return true;
361 }
362 
363 // Recursively discard all the defs on the deadDefs_ worklist.
processDeadDefs()364 bool ValueNumberer::processDeadDefs() {
365   MDefinition* nextDef = nextDef_;
366   while (!deadDefs_.empty()) {
367     MDefinition* def = deadDefs_.popCopy();
368 
369     // Don't invalidate the MDefinition iterator. This is what we're going
370     // to visit next, so we won't miss anything.
371     if (def == nextDef) {
372       continue;
373     }
374 
375     if (!discardDef(def)) {
376       return false;
377     }
378   }
379   return true;
380 }
381 
382 // Test whether |block|, which is a loop header, has any predecessors other than
383 // |loopPred|, the loop predecessor, which it doesn't dominate.
hasNonDominatingPredecessor(MBasicBlock * block,MBasicBlock * loopPred)384 static bool hasNonDominatingPredecessor(MBasicBlock* block,
385                                         MBasicBlock* loopPred) {
386   MOZ_ASSERT(block->isLoopHeader());
387   MOZ_ASSERT(block->loopPredecessor() == loopPred);
388 
389   for (uint32_t i = 0, e = block->numPredecessors(); i < e; ++i) {
390     MBasicBlock* pred = block->getPredecessor(i);
391     if (pred != loopPred && !block->dominates(pred)) {
392       return true;
393     }
394   }
395   return false;
396 }
397 
398 // A loop is about to be made reachable only through an OSR entry into one of
399 // its nested loops. Fix everything up.
fixupOSROnlyLoop(MBasicBlock * block,MBasicBlock * backedge)400 bool ValueNumberer::fixupOSROnlyLoop(MBasicBlock* block,
401                                      MBasicBlock* backedge) {
402   // Create an empty and unreachable(!) block which jumps to |block|. This
403   // allows |block| to remain marked as a loop header, so we don't have to
404   // worry about moving a different block into place as the new loop header,
405   // which is hard, especially if the OSR is into a nested loop. Doing all
406   // that would produce slightly more optimal code, but this is so
407   // extraordinarily rare that it isn't worth the complexity.
408   MBasicBlock* fake =
409       MBasicBlock::New(graph_, block->info(), nullptr, MBasicBlock::NORMAL);
410   if (fake == nullptr) {
411     return false;
412   }
413 
414   graph_.insertBlockBefore(block, fake);
415   fake->setImmediateDominator(fake);
416   fake->addNumDominated(1);
417   fake->setDomIndex(fake->id());
418   fake->setUnreachable();
419 
420   // Create zero-input phis to use as inputs for any phis in |block|.
421   // Again, this is a little odd, but it's the least-odd thing we can do
422   // without significant complexity.
423   for (MPhiIterator iter(block->phisBegin()), end(block->phisEnd());
424        iter != end; ++iter) {
425     MPhi* phi = *iter;
426     MPhi* fakePhi = MPhi::New(graph_.alloc(), phi->type());
427     fake->addPhi(fakePhi);
428     if (!phi->addInputSlow(fakePhi)) {
429       return false;
430     }
431   }
432 
433   fake->end(MGoto::New(graph_.alloc(), block));
434 
435   if (!block->addPredecessorWithoutPhis(fake)) {
436     return false;
437   }
438 
439   // Restore |backedge| as |block|'s loop backedge.
440   block->clearLoopHeader();
441   block->setLoopHeader(backedge);
442 
443   JitSpew(JitSpew_GVN, "        Created fake block%u", fake->id());
444   hasOSRFixups_ = true;
445   return true;
446 }
447 
448 // Remove the CFG edge between |pred| and |block|, after releasing the phi
449 // operands on that edge and discarding any definitions consequently made dead.
removePredecessorAndDoDCE(MBasicBlock * block,MBasicBlock * pred,size_t predIndex)450 bool ValueNumberer::removePredecessorAndDoDCE(MBasicBlock* block,
451                                               MBasicBlock* pred,
452                                               size_t predIndex) {
453   MOZ_ASSERT(
454       !block->isMarked(),
455       "Block marked unreachable should have predecessors removed already");
456 
457   // Before removing the predecessor edge, scan the phi operands for that edge
458   // for dead code before they get removed.
459   MOZ_ASSERT(nextDef_ == nullptr);
460   for (MPhiIterator iter(block->phisBegin()), end(block->phisEnd());
461        iter != end;) {
462     MPhi* phi = *iter++;
463     MOZ_ASSERT(!values_.has(phi),
464                "Visited phi in block having predecessor removed");
465     MOZ_ASSERT(!phi->isGuard());
466 
467     MDefinition* op = phi->getOperand(predIndex);
468     phi->removeOperand(predIndex);
469 
470     nextDef_ = iter != end ? *iter : nullptr;
471     if (!handleUseReleased(op, DontSetUseRemoved) || !processDeadDefs()) {
472       return false;
473     }
474 
475     // If |nextDef_| became dead while we had it pinned, advance the
476     // iterator and discard it now.
477     while (nextDef_ && !nextDef_->hasUses() &&
478            !nextDef_->isGuardRangeBailouts()) {
479       phi = nextDef_->toPhi();
480       iter++;
481       nextDef_ = iter != end ? *iter : nullptr;
482       if (!discardDefsRecursively(phi)) {
483         return false;
484       }
485     }
486   }
487   nextDef_ = nullptr;
488 
489   block->removePredecessorWithoutPhiOperands(pred, predIndex);
490   return true;
491 }
492 
493 // Remove the CFG edge between |pred| and |block|, and if this makes |block|
494 // unreachable, mark it so, and remove the rest of its incoming edges too. And
495 // discard any instructions made dead by the entailed release of any phi
496 // operands.
removePredecessorAndCleanUp(MBasicBlock * block,MBasicBlock * pred)497 bool ValueNumberer::removePredecessorAndCleanUp(MBasicBlock* block,
498                                                 MBasicBlock* pred) {
499   MOZ_ASSERT(!block->isMarked(),
500              "Removing predecessor on block already marked unreachable");
501 
502   // We'll be removing a predecessor, so anything we know about phis in this
503   // block will be wrong.
504   for (MPhiIterator iter(block->phisBegin()), end(block->phisEnd());
505        iter != end; ++iter) {
506     values_.forget(*iter);
507   }
508 
509   // If this is a loop header, test whether it will become an unreachable
510   // loop, or whether it needs special OSR-related fixups.
511   bool isUnreachableLoop = false;
512   if (block->isLoopHeader()) {
513     if (block->loopPredecessor() == pred) {
514       if (MOZ_UNLIKELY(hasNonDominatingPredecessor(block, pred))) {
515         JitSpew(JitSpew_GVN,
516                 "      "
517                 "Loop with header block%u is now only reachable through an "
518                 "OSR entry into the middle of the loop!!",
519                 block->id());
520       } else {
521         // Deleting the entry into the loop makes the loop unreachable.
522         isUnreachableLoop = true;
523         JitSpew(JitSpew_GVN,
524                 "      "
525                 "Loop with header block%u is no longer reachable",
526                 block->id());
527       }
528 #ifdef JS_JITSPEW
529     } else if (block->hasUniqueBackedge() && block->backedge() == pred) {
530       JitSpew(JitSpew_GVN, "      Loop with header block%u is no longer a loop",
531               block->id());
532 #endif
533     }
534   }
535 
536   // Actually remove the CFG edge.
537   if (!removePredecessorAndDoDCE(block, pred,
538                                  block->getPredecessorIndex(pred))) {
539     return false;
540   }
541 
542   // We've now edited the CFG; check to see if |block| became unreachable.
543   if (block->numPredecessors() == 0 || isUnreachableLoop) {
544     JitSpew(JitSpew_GVN, "      Disconnecting block%u", block->id());
545 
546     // Remove |block| from its dominator parent's subtree. This is the only
547     // immediately-dominated-block information we need to update, because
548     // everything dominated by this block is about to be swept away.
549     MBasicBlock* parent = block->immediateDominator();
550     if (parent != block) {
551       parent->removeImmediatelyDominatedBlock(block);
552     }
553 
554     // Completely disconnect it from the CFG. We do this now rather than
555     // just doing it later when we arrive there in visitUnreachableBlock
556     // so that we don't leave a partially broken loop sitting around. This
557     // also lets visitUnreachableBlock assert that numPredecessors() == 0,
558     // which is a nice invariant.
559     if (block->isLoopHeader()) {
560       block->clearLoopHeader();
561     }
562     for (size_t i = 0, e = block->numPredecessors(); i < e; ++i) {
563       if (!removePredecessorAndDoDCE(block, block->getPredecessor(i), i)) {
564         return false;
565       }
566     }
567 
568     // Clear out the resume point operands, as they can hold things that
569     // don't appear to dominate them live.
570     if (MResumePoint* resume = block->entryResumePoint()) {
571       if (!releaseResumePointOperands(resume) || !processDeadDefs()) {
572         return false;
573       }
574       if (MResumePoint* outer = block->outerResumePoint()) {
575         if (!releaseResumePointOperands(outer) || !processDeadDefs()) {
576           return false;
577         }
578       }
579       MOZ_ASSERT(nextDef_ == nullptr);
580       for (MInstructionIterator iter(block->begin()), end(block->end());
581            iter != end;) {
582         MInstruction* ins = *iter++;
583         nextDef_ = iter != end ? *iter : nullptr;
584         if (MResumePoint* resume = ins->resumePoint()) {
585           if (!releaseResumePointOperands(resume) || !processDeadDefs()) {
586             return false;
587           }
588         }
589       }
590       nextDef_ = nullptr;
591     } else {
592 #ifdef DEBUG
593       MOZ_ASSERT(block->outerResumePoint() == nullptr,
594                  "Outer resume point in block without an entry resume point");
595       for (MInstructionIterator iter(block->begin()), end(block->end());
596            iter != end; ++iter) {
597         MOZ_ASSERT(iter->resumePoint() == nullptr,
598                    "Instruction with resume point in block without entry "
599                    "resume point");
600       }
601 #endif
602     }
603 
604     // Use the mark to note that we've already removed all its predecessors,
605     // and we know it's unreachable.
606     block->mark();
607   }
608 
609   return true;
610 }
611 
612 // Return a simplified form of |def|, if we can.
simplified(MDefinition * def) const613 MDefinition* ValueNumberer::simplified(MDefinition* def) const {
614   return def->foldsTo(graph_.alloc());
615 }
616 
617 // If an equivalent and dominating value already exists in the set, return it.
618 // Otherwise insert |def| into the set and return it.
leader(MDefinition * def)619 MDefinition* ValueNumberer::leader(MDefinition* def) {
620   // If the value isn't suitable for eliminating, don't bother hashing it. The
621   // convention is that congruentTo returns false for node kinds that wish to
622   // opt out of redundance elimination.
623   // TODO: It'd be nice to clean up that convention (bug 1031406).
624   if (!def->isEffectful() && def->congruentTo(def)) {
625     // Look for a match.
626     VisibleValues::AddPtr p = values_.findLeaderForAdd(def);
627     if (p) {
628       MDefinition* rep = *p;
629       if (!rep->isDiscarded() && rep->block()->dominates(def->block())) {
630         // We found a dominating congruent value.
631         return rep;
632       }
633 
634       // The congruent value doesn't dominate. It never will again in this
635       // dominator tree, so overwrite it.
636       values_.overwrite(p, def);
637     } else {
638       // No match. Add a new entry.
639       if (!values_.add(p, def)) {
640         return nullptr;
641       }
642     }
643 
644 #ifdef JS_JITSPEW
645     JitSpew(JitSpew_GVN, "      Recording %s%u", def->opName(), def->id());
646 #endif
647   }
648 
649   return def;
650 }
651 
652 // Test whether |phi| is dominated by a congruent phi.
hasLeader(const MPhi * phi,const MBasicBlock * phiBlock) const653 bool ValueNumberer::hasLeader(const MPhi* phi,
654                               const MBasicBlock* phiBlock) const {
655   if (VisibleValues::Ptr p = values_.findLeader(phi)) {
656     const MDefinition* rep = *p;
657     return rep != phi && rep->block()->dominates(phiBlock);
658   }
659   return false;
660 }
661 
662 // Test whether there are any phis in |header| which are newly optimizable, as a
663 // result of optimizations done inside the loop. This is not a sparse approach,
664 // but restarting is rare enough in practice. Termination is ensured by
665 // discarding the phi triggering the iteration.
loopHasOptimizablePhi(MBasicBlock * header) const666 bool ValueNumberer::loopHasOptimizablePhi(MBasicBlock* header) const {
667   // If the header is unreachable, don't bother re-optimizing it.
668   if (header->isMarked()) {
669     return false;
670   }
671 
672   // Rescan the phis for any that can be simplified, since they may be reading
673   // values from backedges.
674   for (MPhiIterator iter(header->phisBegin()), end(header->phisEnd());
675        iter != end; ++iter) {
676     MPhi* phi = *iter;
677     MOZ_ASSERT_IF(!phi->hasUses(), !DeadIfUnused(phi));
678 
679     if (phi->operandIfRedundant() || hasLeader(phi, header)) {
680       return true;  // Phi can be simplified.
681     }
682   }
683   return false;
684 }
685 
686 // Visit |def|.
visitDefinition(MDefinition * def)687 bool ValueNumberer::visitDefinition(MDefinition* def) {
688   // Nop does not fit in any of the previous optimization, as its only purpose
689   // is to reduce the register pressure by keeping additional resume
690   // point. Still, there is no need consecutive list of MNop instructions, and
691   // this will slow down every other iteration on the Graph.
692   if (def->isNop()) {
693     MNop* nop = def->toNop();
694     MBasicBlock* block = nop->block();
695 
696     // We look backward to know if we can remove the previous Nop, we do not
697     // look forward as we would not benefit from the folding made by GVN.
698     MInstructionReverseIterator iter = ++block->rbegin(nop);
699 
700     // This nop is at the beginning of the basic block, just replace the
701     // resume point of the basic block by the one from the resume point.
702     if (iter == block->rend()) {
703       JitSpew(JitSpew_GVN, "      Removing Nop%u", nop->id());
704       nop->moveResumePointAsEntry();
705       block->discard(nop);
706       return true;
707     }
708 
709     // The previous instruction is also a Nop, no need to keep it anymore.
710     MInstruction* prev = *iter;
711     if (prev->isNop()) {
712       JitSpew(JitSpew_GVN, "      Removing Nop%u", prev->id());
713       block->discard(prev);
714       return true;
715     }
716 
717     // The Nop is introduced to capture the result and make sure the operands
718     // are not live anymore when there are no further uses. Though when
719     // all operands are still needed the Nop doesn't decrease the liveness
720     // and can get removed.
721     MResumePoint* rp = nop->resumePoint();
722     if (rp && rp->numOperands() > 0 &&
723         rp->getOperand(rp->numOperands() - 1) == prev &&
724         !nop->block()->lastIns()->isThrow() &&
725         !prev->isAssertRecoveredOnBailout()) {
726       size_t numOperandsLive = 0;
727       for (size_t j = 0; j < prev->numOperands(); j++) {
728         for (size_t i = 0; i < rp->numOperands(); i++) {
729           if (prev->getOperand(j) == rp->getOperand(i)) {
730             numOperandsLive++;
731             break;
732           }
733         }
734       }
735 
736       if (numOperandsLive == prev->numOperands()) {
737         JitSpew(JitSpew_GVN, "      Removing Nop%u", nop->id());
738         block->discard(nop);
739       }
740     }
741 
742     return true;
743   }
744 
745   // Skip optimizations on instructions which are recovered on bailout, to
746   // avoid mixing instructions which are recovered on bailouts with
747   // instructions which are not.
748   if (def->isRecoveredOnBailout()) {
749     return true;
750   }
751 
752   // If this instruction has a dependency() into an unreachable block, we'll
753   // need to update AliasAnalysis.
754   MDefinition* dep = def->dependency();
755   if (dep != nullptr && (dep->isDiscarded() || dep->block()->isDead())) {
756     JitSpew(JitSpew_GVN, "      AliasAnalysis invalidated");
757     if (updateAliasAnalysis_ && !dependenciesBroken_) {
758       // TODO: Recomputing alias-analysis could theoretically expose more
759       // GVN opportunities.
760       JitSpew(JitSpew_GVN, "        Will recompute!");
761       dependenciesBroken_ = true;
762     }
763     // Temporarily clear its dependency, to protect foldsTo, which may
764     // wish to use the dependency to do store-to-load forwarding.
765     def->setDependency(def->toInstruction());
766   } else {
767     dep = nullptr;
768   }
769 
770   // Look for a simplified form of |def|.
771   MDefinition* sim = simplified(def);
772   if (sim != def) {
773     if (sim == nullptr) {
774       return false;
775     }
776 
777     bool isNewInstruction = sim->block() == nullptr;
778 
779     // If |sim| doesn't belong to a block, insert it next to |def|.
780     if (isNewInstruction) {
781       def->block()->insertAfter(def->toInstruction(), sim->toInstruction());
782     }
783 
784 #ifdef JS_JITSPEW
785     JitSpew(JitSpew_GVN, "      Folded %s%u to %s%u", def->opName(), def->id(),
786             sim->opName(), sim->id());
787 #endif
788     MOZ_ASSERT(!sim->isDiscarded());
789     ReplaceAllUsesWith(def, sim);
790 
791     // The node's foldsTo said |def| can be replaced by |rep|. If |def| is a
792     // guard, then either |rep| is also a guard, or a guard isn't actually
793     // needed, so we can clear |def|'s guard flag and let it be discarded.
794     def->setNotGuardUnchecked();
795 
796     if (def->isGuardRangeBailouts()) {
797       sim->setGuardRangeBailoutsUnchecked();
798     }
799 
800     if (DeadIfUnused(def)) {
801       if (!discardDefsRecursively(def)) {
802         return false;
803       }
804 
805       // If that ended up discarding |sim|, then we're done here.
806       if (sim->isDiscarded()) {
807         return true;
808       }
809     }
810 
811     if (!rerun_ && def->isPhi() && !sim->isPhi()) {
812       rerun_ = true;
813       JitSpew(JitSpew_GVN,
814               "      Replacing phi%u may have enabled cascading optimisations; "
815               "will re-run",
816               def->id());
817     }
818 
819     // Otherwise, procede to optimize with |sim| in place of |def|.
820     def = sim;
821 
822     // If the simplified instruction was already part of the graph, then we
823     // probably already visited and optimized this instruction.
824     if (!isNewInstruction) {
825       return true;
826     }
827   }
828 
829   // Now that foldsTo is done, re-enable the original dependency. Even though
830   // it may be pointing into a discarded block, it's still valid for the
831   // purposes of detecting congruent loads.
832   if (dep != nullptr) {
833     def->setDependency(dep);
834   }
835 
836   // Look for a dominating def which makes |def| redundant.
837   MDefinition* rep = leader(def);
838   if (rep != def) {
839     if (rep == nullptr) {
840       return false;
841     }
842     if (rep->updateForReplacement(def)) {
843 #ifdef JS_JITSPEW
844       JitSpew(JitSpew_GVN, "      Replacing %s%u with %s%u", def->opName(),
845               def->id(), rep->opName(), rep->id());
846 #endif
847       ReplaceAllUsesWith(def, rep);
848 
849       // The node's congruentTo said |def| is congruent to |rep|, and it's
850       // dominated by |rep|. If |def| is a guard, it's covered by |rep|,
851       // so we can clear |def|'s guard flag and let it be discarded.
852       def->setNotGuardUnchecked();
853 
854       if (DeadIfUnused(def)) {
855         // discardDef should not add anything to the deadDefs, as the
856         // redundant operation should have the same input operands.
857         mozilla::DebugOnly<bool> r = discardDef(def);
858         MOZ_ASSERT(
859             r,
860             "discardDef shouldn't have tried to add anything to the worklist, "
861             "so it shouldn't have failed");
862         MOZ_ASSERT(deadDefs_.empty(),
863                    "discardDef shouldn't have added anything to the worklist");
864       }
865       def = rep;
866     }
867   }
868 
869   return true;
870 }
871 
872 // Visit the control instruction at the end of |block|.
visitControlInstruction(MBasicBlock * block)873 bool ValueNumberer::visitControlInstruction(MBasicBlock* block) {
874   // Look for a simplified form of the control instruction.
875   MControlInstruction* control = block->lastIns();
876   MDefinition* rep = simplified(control);
877   if (rep == control) {
878     return true;
879   }
880 
881   if (rep == nullptr) {
882     return false;
883   }
884 
885   MControlInstruction* newControl = rep->toControlInstruction();
886   MOZ_ASSERT(!newControl->block(),
887              "Control instruction replacement shouldn't already be in a block");
888 #ifdef JS_JITSPEW
889   JitSpew(JitSpew_GVN, "      Folded control instruction %s%u to %s%u",
890           control->opName(), control->id(), newControl->opName(),
891           graph_.getNumInstructionIds());
892 #endif
893 
894   // If the simplification removes any CFG edges, update the CFG and remove
895   // any blocks that become dead.
896   size_t oldNumSuccs = control->numSuccessors();
897   size_t newNumSuccs = newControl->numSuccessors();
898   if (newNumSuccs != oldNumSuccs) {
899     MOZ_ASSERT(newNumSuccs < oldNumSuccs,
900                "New control instruction has too many successors");
901     for (size_t i = 0; i != oldNumSuccs; ++i) {
902       MBasicBlock* succ = control->getSuccessor(i);
903       if (HasSuccessor(newControl, succ)) {
904         continue;
905       }
906       if (succ->isMarked()) {
907         continue;
908       }
909       if (!removePredecessorAndCleanUp(succ, block)) {
910         return false;
911       }
912       if (succ->isMarked()) {
913         continue;
914       }
915       if (!rerun_) {
916         if (!remainingBlocks_.append(succ)) {
917           return false;
918         }
919       }
920     }
921   }
922 
923   if (!releaseOperands(control)) {
924     return false;
925   }
926   block->discardIgnoreOperands(control);
927   block->end(newControl);
928   if (block->entryResumePoint() && newNumSuccs != oldNumSuccs) {
929     block->flagOperandsOfPrunedBranches(newControl);
930   }
931   return processDeadDefs();
932 }
933 
934 // |block| is unreachable. Mine it for opportunities to delete more dead
935 // code, and then discard it.
visitUnreachableBlock(MBasicBlock * block)936 bool ValueNumberer::visitUnreachableBlock(MBasicBlock* block) {
937   JitSpew(JitSpew_GVN, "    Visiting unreachable block%u%s%s%s", block->id(),
938           block->isLoopHeader() ? " (loop header)" : "",
939           block->isSplitEdge() ? " (split edge)" : "",
940           block->immediateDominator() == block ? " (dominator root)" : "");
941 
942   MOZ_ASSERT(block->isMarked(),
943              "Visiting unmarked (and therefore reachable?) block");
944   MOZ_ASSERT(block->numPredecessors() == 0,
945              "Block marked unreachable still has predecessors");
946   MOZ_ASSERT(block != graph_.entryBlock(), "Removing normal entry block");
947   MOZ_ASSERT(block != graph_.osrBlock(), "Removing OSR entry block");
948   MOZ_ASSERT(deadDefs_.empty(), "deadDefs_ not cleared");
949 
950   // Disconnect all outgoing CFG edges.
951   for (size_t i = 0, e = block->numSuccessors(); i < e; ++i) {
952     MBasicBlock* succ = block->getSuccessor(i);
953     if (succ->isDead() || succ->isMarked()) {
954       continue;
955     }
956     if (!removePredecessorAndCleanUp(succ, block)) {
957       return false;
958     }
959     if (succ->isMarked()) {
960       continue;
961     }
962     // |succ| is still reachable. Make a note of it so that we can scan
963     // it for interesting dominator tree changes later.
964     if (!rerun_) {
965       if (!remainingBlocks_.append(succ)) {
966         return false;
967       }
968     }
969   }
970 
971   // Discard any instructions with no uses. The remaining instructions will be
972   // discarded when their last use is discarded.
973   MOZ_ASSERT(nextDef_ == nullptr);
974   for (MDefinitionIterator iter(block); iter;) {
975     MDefinition* def = *iter++;
976     if (def->hasUses()) {
977       continue;
978     }
979     nextDef_ = iter ? *iter : nullptr;
980     if (!discardDefsRecursively(def)) {
981       return false;
982     }
983   }
984 
985   nextDef_ = nullptr;
986   MControlInstruction* control = block->lastIns();
987   return discardDefsRecursively(control);
988 }
989 
990 // Visit all the phis and instructions |block|.
visitBlock(MBasicBlock * block)991 bool ValueNumberer::visitBlock(MBasicBlock* block) {
992   MOZ_ASSERT(!block->isMarked(), "Blocks marked unreachable during GVN");
993   MOZ_ASSERT(!block->isDead(), "Block to visit is already dead");
994 
995   JitSpew(JitSpew_GVN, "    Visiting block%u", block->id());
996 
997   // Visit the definitions in the block top-down.
998   MOZ_ASSERT(nextDef_ == nullptr);
999   for (MDefinitionIterator iter(block); iter;) {
1000     if (!graph_.alloc().ensureBallast()) {
1001       return false;
1002     }
1003     MDefinition* def = *iter++;
1004 
1005     // Remember where our iterator is so that we don't invalidate it.
1006     nextDef_ = iter ? *iter : nullptr;
1007 
1008     // If the definition is dead, discard it.
1009     if (IsDiscardable(def)) {
1010       if (!discardDefsRecursively(def)) {
1011         return false;
1012       }
1013       continue;
1014     }
1015 
1016     if (!visitDefinition(def)) {
1017       return false;
1018     }
1019   }
1020   nextDef_ = nullptr;
1021 
1022   if (!graph_.alloc().ensureBallast()) {
1023     return false;
1024   }
1025 
1026   return visitControlInstruction(block);
1027 }
1028 
1029 // Visit all the blocks dominated by dominatorRoot.
visitDominatorTree(MBasicBlock * dominatorRoot)1030 bool ValueNumberer::visitDominatorTree(MBasicBlock* dominatorRoot) {
1031   JitSpew(JitSpew_GVN,
1032           "  Visiting dominator tree (with %" PRIu64
1033           " blocks) rooted at block%u%s",
1034           uint64_t(dominatorRoot->numDominated()), dominatorRoot->id(),
1035           dominatorRoot == graph_.entryBlock()
1036               ? " (normal entry block)"
1037               : dominatorRoot == graph_.osrBlock()
1038                     ? " (OSR entry block)"
1039                     : dominatorRoot->numPredecessors() == 0
1040                           ? " (odd unreachable block)"
1041                           : " (merge point from normal entry and OSR entry)");
1042   MOZ_ASSERT(dominatorRoot->immediateDominator() == dominatorRoot,
1043              "root is not a dominator tree root");
1044 
1045   // Visit all blocks dominated by dominatorRoot, in RPO. This has the nice
1046   // property that we'll always visit a block before any block it dominates,
1047   // so we can make a single pass through the list and see every full
1048   // redundance.
1049   size_t numVisited = 0;
1050   size_t numDiscarded = 0;
1051   for (ReversePostorderIterator iter(graph_.rpoBegin(dominatorRoot));;) {
1052     MOZ_ASSERT(iter != graph_.rpoEnd(), "Inconsistent dominator information");
1053     MBasicBlock* block = *iter++;
1054     // We're only visiting blocks in dominatorRoot's tree right now.
1055     if (!dominatorRoot->dominates(block)) {
1056       continue;
1057     }
1058 
1059     // If this is a loop backedge, remember the header, as we may not be able
1060     // to find it after we simplify the block.
1061     MBasicBlock* header =
1062         block->isLoopBackedge() ? block->loopHeaderOfBackedge() : nullptr;
1063 
1064     if (block->isMarked()) {
1065       // This block has become unreachable; handle it specially.
1066       if (!visitUnreachableBlock(block)) {
1067         return false;
1068       }
1069       ++numDiscarded;
1070     } else {
1071       // Visit the block!
1072       if (!visitBlock(block)) {
1073         return false;
1074       }
1075       ++numVisited;
1076     }
1077 
1078     // If the block is/was a loop backedge, check to see if the block that
1079     // is/was its header has optimizable phis, which would want a re-run.
1080     if (!rerun_ && header && loopHasOptimizablePhi(header)) {
1081       JitSpew(JitSpew_GVN,
1082               "    Loop phi in block%u can now be optimized; will re-run GVN!",
1083               header->id());
1084       rerun_ = true;
1085       remainingBlocks_.clear();
1086     }
1087 
1088     MOZ_ASSERT(numVisited <= dominatorRoot->numDominated() - numDiscarded,
1089                "Visited blocks too many times");
1090     if (numVisited >= dominatorRoot->numDominated() - numDiscarded) {
1091       break;
1092     }
1093   }
1094 
1095   totalNumVisited_ += numVisited;
1096   values_.clear();
1097   return true;
1098 }
1099 
1100 // Visit all the blocks in the graph.
visitGraph()1101 bool ValueNumberer::visitGraph() {
1102   // Due to OSR blocks, the set of blocks dominated by a blocks may not be
1103   // contiguous in the RPO. Do a separate traversal for each dominator tree
1104   // root. There's always the main entry, and sometimes there's an OSR entry,
1105   // and then there are the roots formed where the OSR paths merge with the
1106   // main entry paths.
1107   for (ReversePostorderIterator iter(graph_.rpoBegin());;) {
1108     MOZ_ASSERT(iter != graph_.rpoEnd(), "Inconsistent dominator information");
1109     MBasicBlock* block = *iter;
1110     if (block->immediateDominator() == block) {
1111       if (!visitDominatorTree(block)) {
1112         return false;
1113       }
1114 
1115       // Normally unreachable blocks would be removed by now, but if this
1116       // block is a dominator tree root, it has been special-cased and left
1117       // in place in order to avoid invalidating our iterator. Now that
1118       // we've finished the tree, increment the iterator, and then if it's
1119       // marked for removal, remove it.
1120       ++iter;
1121       if (block->isMarked()) {
1122         JitSpew(JitSpew_GVN, "      Discarding dominator root block%u",
1123                 block->id());
1124         MOZ_ASSERT(
1125             block->begin() == block->end(),
1126             "Unreachable dominator tree root has instructions after tree walk");
1127         MOZ_ASSERT(block->phisEmpty(),
1128                    "Unreachable dominator tree root has phis after tree walk");
1129         graph_.removeBlock(block);
1130         blocksRemoved_ = true;
1131       }
1132 
1133       MOZ_ASSERT(totalNumVisited_ <= graph_.numBlocks(),
1134                  "Visited blocks too many times");
1135       if (totalNumVisited_ >= graph_.numBlocks()) {
1136         break;
1137       }
1138     } else {
1139       // This block a dominator tree root. Proceed to the next one.
1140       ++iter;
1141     }
1142   }
1143   totalNumVisited_ = 0;
1144   return true;
1145 }
1146 
insertOSRFixups()1147 bool ValueNumberer::insertOSRFixups() {
1148   ReversePostorderIterator end(graph_.end());
1149   for (ReversePostorderIterator iter(graph_.begin()); iter != end;) {
1150     MBasicBlock* block = *iter++;
1151 
1152     // Only add fixup block above for loops which can be reached from OSR.
1153     if (!block->isLoopHeader()) {
1154       continue;
1155     }
1156 
1157     // If the loop header is not self-dominated, then this loop does not
1158     // have to deal with a second entry point, so there is no need to add a
1159     // second entry point with a fixup block.
1160     if (block->immediateDominator() != block) {
1161       continue;
1162     }
1163 
1164     if (!fixupOSROnlyLoop(block, block->backedge())) {
1165       return false;
1166     }
1167   }
1168 
1169   return true;
1170 }
1171 
1172 // OSR fixups serve the purpose of representing the non-OSR entry into a loop
1173 // when the only real entry is an OSR entry into the middle. However, if the
1174 // entry into the middle is subsequently folded away, the loop may actually
1175 // have become unreachable. Mark-and-sweep all blocks to remove all such code.
cleanupOSRFixups()1176 bool ValueNumberer::cleanupOSRFixups() {
1177   // Mark.
1178   Vector<MBasicBlock*, 0, JitAllocPolicy> worklist(graph_.alloc());
1179   unsigned numMarked = 2;
1180   graph_.entryBlock()->mark();
1181   graph_.osrBlock()->mark();
1182   if (!worklist.append(graph_.entryBlock()) ||
1183       !worklist.append(graph_.osrBlock())) {
1184     return false;
1185   }
1186   while (!worklist.empty()) {
1187     MBasicBlock* block = worklist.popCopy();
1188     for (size_t i = 0, e = block->numSuccessors(); i != e; ++i) {
1189       MBasicBlock* succ = block->getSuccessor(i);
1190       if (!succ->isMarked()) {
1191         ++numMarked;
1192         succ->mark();
1193         if (!worklist.append(succ)) {
1194           return false;
1195         }
1196       } else if (succ->isLoopHeader() && succ->loopPredecessor() == block &&
1197                  succ->numPredecessors() == 3) {
1198         // Unmark fixup blocks if the loop predecessor is marked after
1199         // the loop header.
1200         succ->getPredecessor(1)->unmarkUnchecked();
1201       }
1202     }
1203 
1204     // OSR fixup blocks are needed if and only if the loop header is
1205     // reachable from its backedge (via the OSR block) and not from its
1206     // original loop predecessor.
1207     //
1208     // Thus OSR fixup blocks are removed if the loop header is not
1209     // reachable, or if the loop header is reachable from both its backedge
1210     // and its original loop predecessor.
1211     if (block->isLoopHeader()) {
1212       MBasicBlock* maybeFixupBlock = nullptr;
1213       if (block->numPredecessors() == 2) {
1214         maybeFixupBlock = block->getPredecessor(0);
1215       } else {
1216         MOZ_ASSERT(block->numPredecessors() == 3);
1217         if (!block->loopPredecessor()->isMarked()) {
1218           maybeFixupBlock = block->getPredecessor(1);
1219         }
1220       }
1221 
1222       if (maybeFixupBlock && !maybeFixupBlock->isMarked() &&
1223           maybeFixupBlock->numPredecessors() == 0) {
1224         MOZ_ASSERT(maybeFixupBlock->numSuccessors() == 1,
1225                    "OSR fixup block should have exactly one successor");
1226         MOZ_ASSERT(maybeFixupBlock != graph_.entryBlock(),
1227                    "OSR fixup block shouldn't be the entry block");
1228         MOZ_ASSERT(maybeFixupBlock != graph_.osrBlock(),
1229                    "OSR fixup block shouldn't be the OSR entry block");
1230         maybeFixupBlock->mark();
1231       }
1232     }
1233   }
1234 
1235   // And sweep.
1236   return RemoveUnmarkedBlocks(mir_, graph_, numMarked);
1237 }
1238 
ValueNumberer(MIRGenerator * mir,MIRGraph & graph)1239 ValueNumberer::ValueNumberer(MIRGenerator* mir, MIRGraph& graph)
1240     : mir_(mir),
1241       graph_(graph),
1242       // Initialize the value set. It's tempting to pass in a length that is a
1243       // function of graph_.getNumInstructionIds(). But if we start out with a
1244       // large capacity, it will be far larger than the actual element count for
1245       // most of the pass, so when we remove elements, it would often think it
1246       // needs to compact itself. Empirically, just letting the HashTable grow
1247       // as needed on its own seems to work pretty well.
1248       values_(graph.alloc()),
1249       deadDefs_(graph.alloc()),
1250       remainingBlocks_(graph.alloc()),
1251       nextDef_(nullptr),
1252       totalNumVisited_(0),
1253       rerun_(false),
1254       blocksRemoved_(false),
1255       updateAliasAnalysis_(false),
1256       dependenciesBroken_(false),
1257       hasOSRFixups_(false) {}
1258 
run(UpdateAliasAnalysisFlag updateAliasAnalysis)1259 bool ValueNumberer::run(UpdateAliasAnalysisFlag updateAliasAnalysis) {
1260   updateAliasAnalysis_ = updateAliasAnalysis == UpdateAliasAnalysis;
1261 
1262   JitSpew(JitSpew_GVN, "Running GVN on graph (with %" PRIu64 " blocks)",
1263           uint64_t(graph_.numBlocks()));
1264 
1265   // Adding fixup blocks only make sense iff we have a second entry point into
1266   // the graph which cannot be reached any more from the entry point.
1267   if (graph_.osrBlock()) {
1268     if (!insertOSRFixups()) {
1269       return false;
1270     }
1271   }
1272 
1273   // Top level non-sparse iteration loop. If an iteration performs a
1274   // significant change, such as discarding a block which changes the
1275   // dominator tree and may enable more optimization, this loop takes another
1276   // iteration.
1277   int runs = 0;
1278   for (;;) {
1279     if (!visitGraph()) {
1280       return false;
1281     }
1282 
1283     // Test whether any block which was not removed but which had at least
1284     // one predecessor removed will have a new dominator parent.
1285     while (!remainingBlocks_.empty()) {
1286       MBasicBlock* block = remainingBlocks_.popCopy();
1287       if (!block->isDead() && IsDominatorRefined(block)) {
1288         JitSpew(JitSpew_GVN,
1289                 "  Dominator for block%u can now be refined; will re-run GVN!",
1290                 block->id());
1291         rerun_ = true;
1292         remainingBlocks_.clear();
1293         break;
1294       }
1295     }
1296 
1297     if (blocksRemoved_) {
1298       if (!AccountForCFGChanges(mir_, graph_, dependenciesBroken_,
1299                                 /* underValueNumberer = */ true)) {
1300         return false;
1301       }
1302 
1303       blocksRemoved_ = false;
1304       dependenciesBroken_ = false;
1305     }
1306 
1307     if (mir_->shouldCancel("GVN (outer loop)")) {
1308       return false;
1309     }
1310 
1311     // If no further opportunities have been discovered, we're done.
1312     if (!rerun_) {
1313       break;
1314     }
1315 
1316     rerun_ = false;
1317 
1318     // Enforce an arbitrary iteration limit. This is rarely reached, and
1319     // isn't even strictly necessary, as the algorithm is guaranteed to
1320     // terminate on its own in a finite amount of time (since every time we
1321     // re-run we discard the construct which triggered the re-run), but it
1322     // does help avoid slow compile times on pathological code.
1323     ++runs;
1324     if (runs == 6) {
1325       JitSpew(JitSpew_GVN, "Re-run cutoff of %d reached. Terminating GVN!",
1326               runs);
1327       break;
1328     }
1329 
1330     JitSpew(JitSpew_GVN,
1331             "Re-running GVN on graph (run %d, now with %" PRIu64 " blocks)",
1332             runs, uint64_t(graph_.numBlocks()));
1333   }
1334 
1335   if (MOZ_UNLIKELY(hasOSRFixups_)) {
1336     if (!cleanupOSRFixups()) {
1337       return false;
1338     }
1339     hasOSRFixups_ = false;
1340   }
1341 
1342   return true;
1343 }
1344