summaryrefslogtreecommitdiffstats
path: root/js/public/UbiNodeDominatorTree.h
blob: 7b534aa11ccf640ec6028670e6455d3e32e0fd39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef js_UbiNodeDominatorTree_h
#define js_UbiNodeDominatorTree_h

#include "mozilla/Attributes.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"

#include <utility>

#include "js/AllocPolicy.h"
#include "js/UbiNode.h"
#include "js/UbiNodePostOrder.h"
#include "js/Utility.h"
#include "js/Vector.h"

namespace JS {
namespace ubi {

/**
 * In a directed graph with a root node `R`, a node `A` is said to "dominate" a
 * node `B` iff every path from `R` to `B` contains `A`. A node `A` is said to
 * be the "immediate dominator" of a node `B` iff it dominates `B`, is not `B`
 * itself, and does not dominate any other nodes which also dominate `B` in
 * turn.
 *
 * If we take every node from a graph `G` and create a new graph `T` with edges
 * to each node from its immediate dominator, then `T` is a tree (each node has
 * only one immediate dominator, or none if it is the root). This tree is called
 * a "dominator tree".
 *
 * This class represents a dominator tree constructed from a `JS::ubi::Node`
 * heap graph. The domination relationship and dominator trees are useful tools
 * for analyzing heap graphs because they tell you:
 *
 *   - Exactly what could be reclaimed by the GC if some node `A` became
 *     unreachable: those nodes which are dominated by `A`,
 *
 *   - The "retained size" of a node in the heap graph, in contrast to its
 *     "shallow size". The "shallow size" is the space taken by a node itself,
 *     not counting anything it references. The "retained size" of a node is its
 *     shallow size plus the size of all the things that would be collected if
 *     the original node wasn't (directly or indirectly) referencing them. In
 *     other words, the retained size is the shallow size of a node plus the
 *     shallow sizes of every other node it dominates. For example, the root
 *     node in a binary tree might have a small shallow size that does not take
 *     up much space itself, but it dominates the rest of the binary tree and
 *     its retained size is therefore significant (assuming no external
 *     references into the tree).
 *
 * The simple, engineered algorithm presented in "A Simple, Fast Dominance
 * Algorithm" by Cooper el al[0] is used to find dominators and construct the
 * dominator tree. This algorithm runs in O(n^2) time, but is faster in practice
 * than alternative algorithms with better theoretical running times, such as
 * Lengauer-Tarjan which runs in O(e * log(n)). The big caveat to that statement
 * is that Cooper et al found it is faster in practice *on control flow graphs*
 * and I'm not convinced that this property also holds on *heap* graphs. That
 * said, the implementation of this algorithm is *much* simpler than
 * Lengauer-Tarjan and has been found to be fast enough at least for the time
 * being.
 *
 * [0]: http://www.cs.rice.edu/~keith/EMBED/dom.pdf
 */
class JS_PUBLIC_API DominatorTree {
 private:
  // Types.

  using PredecessorSets = js::HashMap<Node, NodeSetPtr, js::DefaultHasher<Node>,
                                      js::SystemAllocPolicy>;
  using NodeToIndexMap = js::HashMap<Node, uint32_t, js::DefaultHasher<Node>,
                                     js::SystemAllocPolicy>;
  class DominatedSets;

 public:
  class DominatedSetRange;

  /**
   * A pointer to an immediately dominated node.
   *
   * Don't use this type directly; it is no safer than regular pointers. This
   * is only for use indirectly with range-based for loops and
   * `DominatedSetRange`.
   *
   * @see JS::ubi::DominatorTree::getDominatedSet
   */
  class DominatedNodePtr {
    friend class DominatedSetRange;

    const JS::ubi::Vector<Node>& postOrder;
    const uint32_t* ptr;

    DominatedNodePtr(const JS::ubi::Vector<Node>& postOrder,
                     const uint32_t* ptr)
        : postOrder(postOrder), ptr(ptr) {}

   public:
    bool operator!=(const DominatedNodePtr& rhs) const {
      return ptr != rhs.ptr;
    }
    void operator++() { ptr++; }
    const Node& operator*() const { return postOrder[*ptr]; }
  };

  /**
   * A range of immediately dominated `JS::ubi::Node`s for use with
   * range-based for loops.
   *
   * @see JS::ubi::DominatorTree::getDominatedSet
   */
  class DominatedSetRange {
    friend class DominatedSets;

    const JS::ubi::Vector<Node>& postOrder;
    const uint32_t* beginPtr;
    const uint32_t* endPtr;

    DominatedSetRange(JS::ubi::Vector<Node>& postOrder, const uint32_t* begin,
                      const uint32_t* end)
        : postOrder(postOrder), beginPtr(begin), endPtr(end) {
      MOZ_ASSERT(begin <= end);
    }

   public:
    DominatedNodePtr begin() const {
      MOZ_ASSERT(beginPtr <= endPtr);
      return DominatedNodePtr(postOrder, beginPtr);
    }

    DominatedNodePtr end() const { return DominatedNodePtr(postOrder, endPtr); }

    size_t length() const {
      MOZ_ASSERT(beginPtr <= endPtr);
      return endPtr - beginPtr;
    }

    /**
     * Safely skip ahead `n` dominators in the range, in O(1) time.
     *
     * Example usage:
     *
     *     mozilla::Maybe<DominatedSetRange> range =
     *         myDominatorTree.getDominatedSet(myNode);
     *     if (range.isNothing()) {
     *         // Handle unknown nodes however you see fit...
     *         return false;
     *     }
     *
     *     // Don't care about the first ten, for whatever reason.
     *     range->skip(10);
     *     for (const JS::ubi::Node& dominatedNode : *range) {
     *         // ...
     *     }
     */
    void skip(size_t n) {
      beginPtr += n;
      if (beginPtr > endPtr) {
        beginPtr = endPtr;
      }
    }
  };

 private:
  /**
   * The set of all dominated sets in a dominator tree.
   *
   * Internally stores the sets in a contiguous array, with a side table of
   * indices into that contiguous array to denote the start index of each
   * individual set.
   */
  class DominatedSets {
    JS::ubi::Vector<uint32_t> dominated;
    JS::ubi::Vector<uint32_t> indices;

    DominatedSets(JS::ubi::Vector<uint32_t>&& dominated,
                  JS::ubi::Vector<uint32_t>&& indices)
        : dominated(std::move(dominated)), indices(std::move(indices)) {}

   public:
    // DominatedSets is not copy-able.
    DominatedSets(const DominatedSets& rhs) = delete;
    DominatedSets& operator=(const DominatedSets& rhs) = delete;

    // DominatedSets is move-able.
    DominatedSets(DominatedSets&& rhs)
        : dominated(std::move(rhs.dominated)), indices(std::move(rhs.indices)) {
      MOZ_ASSERT(this != &rhs, "self-move not allowed");
    }
    DominatedSets& operator=(DominatedSets&& rhs) {
      this->~DominatedSets();
      new (this) DominatedSets(std::move(rhs));
      return *this;
    }

    /**
     * Create the DominatedSets given the mapping of a node index to its
     * immediate dominator. Returns `Some` on success, `Nothing` on OOM
     * failure.
     */
    static mozilla::Maybe<DominatedSets> Create(
        const JS::ubi::Vector<uint32_t>& doms) {
      auto length = doms.length();
      MOZ_ASSERT(length < UINT32_MAX);

      // Create a vector `dominated` holding a flattened set of buckets of
      // immediately dominated children nodes, with a lookup table
      // `indices` mapping from each node to the beginning of its bucket.
      //
      // This has three phases:
      //
      // 1. Iterate over the full set of nodes and count up the size of
      //    each bucket. These bucket sizes are temporarily stored in the
      //    `indices` vector.
      //
      // 2. Convert the `indices` vector to store the cumulative sum of
      //    the sizes of all buckets before each index, resulting in a
      //    mapping from node index to one past the end of that node's
      //    bucket.
      //
      // 3. Iterate over the full set of nodes again, filling in bucket
      //    entries from the end of the bucket's range to its
      //    beginning. This decrements each index as a bucket entry is
      //    filled in. After having filled in all of a bucket's entries,
      //    the index points to the start of the bucket.

      JS::ubi::Vector<uint32_t> dominated;
      JS::ubi::Vector<uint32_t> indices;
      if (!dominated.growBy(length) || !indices.growBy(length)) {
        return mozilla::Nothing();
      }

      // 1
      memset(indices.begin(), 0, length * sizeof(uint32_t));
      for (uint32_t i = 0; i < length; i++) {
        indices[doms[i]]++;
      }

      // 2
      uint32_t sumOfSizes = 0;
      for (uint32_t i = 0; i < length; i++) {
        sumOfSizes += indices[i];
        MOZ_ASSERT(sumOfSizes <= length);
        indices[i] = sumOfSizes;
      }

      // 3
      for (uint32_t i = 0; i < length; i++) {
        auto idxOfDom = doms[i];
        indices[idxOfDom]--;
        dominated[indices[idxOfDom]] = i;
      }

#ifdef DEBUG
      // Assert that our buckets are non-overlapping and don't run off the
      // end of the vector.
      uint32_t lastIndex = 0;
      for (uint32_t i = 0; i < length; i++) {
        MOZ_ASSERT(indices[i] >= lastIndex);
        MOZ_ASSERT(indices[i] < length);
        lastIndex = indices[i];
      }
#endif

      return mozilla::Some(
          DominatedSets(std::move(dominated), std::move(indices)));
    }

    /**
     * Get the set of nodes immediately dominated by the node at
     * `postOrder[nodeIndex]`.
     */
    DominatedSetRange dominatedSet(JS::ubi::Vector<Node>& postOrder,
                                   uint32_t nodeIndex) const {
      MOZ_ASSERT(postOrder.length() == indices.length());
      MOZ_ASSERT(nodeIndex < indices.length());
      auto end = nodeIndex == indices.length() - 1
                     ? dominated.end()
                     : &dominated[indices[nodeIndex + 1]];
      return DominatedSetRange(postOrder, &dominated[indices[nodeIndex]], end);
    }
  };

 private:
  // Data members.
  JS::ubi::Vector<Node> postOrder;
  NodeToIndexMap nodeToPostOrderIndex;
  JS::ubi::Vector<uint32_t> doms;
  DominatedSets dominatedSets;
  mozilla::Maybe<JS::ubi::Vector<JS::ubi::Node::Size>> retainedSizes;

 private:
  // We use `UNDEFINED` as a sentinel value in the `doms` vector to signal
  // that we haven't found any dominators for the node at the corresponding
  // index in `postOrder` yet.
  static const uint32_t UNDEFINED = UINT32_MAX;

  DominatorTree(JS::ubi::Vector<Node>&& postOrder,
                NodeToIndexMap&& nodeToPostOrderIndex,
                JS::ubi::Vector<uint32_t>&& doms, DominatedSets&& dominatedSets)
      : postOrder(std::move(postOrder)),
        nodeToPostOrderIndex(std::move(nodeToPostOrderIndex)),
        doms(std::move(doms)),
        dominatedSets(std::move(dominatedSets)),
        retainedSizes(mozilla::Nothing()) {}

  static uint32_t intersect(JS::ubi::Vector<uint32_t>& doms, uint32_t finger1,
                            uint32_t finger2) {
    while (finger1 != finger2) {
      if (finger1 < finger2) {
        finger1 = doms[finger1];
      } else if (finger2 < finger1) {
        finger2 = doms[finger2];
      }
    }
    return finger1;
  }

  // Do the post order traversal of the heap graph and populate our
  // predecessor sets.
  [[nodiscard]] static bool doTraversal(JSContext* cx, AutoCheckCannotGC& noGC,
                                        const Node& root,
                                        JS::ubi::Vector<Node>& postOrder,
                                        PredecessorSets& predecessorSets) {
    uint32_t nodeCount = 0;
    auto onNode = [&](const Node& node) {
      nodeCount++;
      if (MOZ_UNLIKELY(nodeCount == UINT32_MAX)) {
        return false;
      }
      return postOrder.append(node);
    };

    auto onEdge = [&](const Node& origin, const Edge& edge) {
      auto p = predecessorSets.lookupForAdd(edge.referent);
      if (!p) {
        mozilla::UniquePtr<NodeSet, DeletePolicy<NodeSet>> set(
            js_new<NodeSet>());
        if (!set || !predecessorSets.add(p, edge.referent, std::move(set))) {
          return false;
        }
      }
      MOZ_ASSERT(p && p->value());
      return p->value()->put(origin);
    };

    PostOrder traversal(cx, noGC);
    return traversal.addStart(root) && traversal.traverse(onNode, onEdge);
  }

  // Populates the given `map` with an entry for each node to its index in
  // `postOrder`.
  [[nodiscard]] static bool mapNodesToTheirIndices(
      JS::ubi::Vector<Node>& postOrder, NodeToIndexMap& map) {
    MOZ_ASSERT(map.empty());
    MOZ_ASSERT(postOrder.length() < UINT32_MAX);
    uint32_t length = postOrder.length();
    if (!map.reserve(length)) {
      return false;
    }
    for (uint32_t i = 0; i < length; i++) {
      map.putNewInfallible(postOrder[i], i);
    }
    return true;
  }

  // Convert the Node -> NodeSet predecessorSets to a index -> Vector<index>
  // form.
  [[nodiscard]] static bool convertPredecessorSetsToVectors(
      const Node& root, JS::ubi::Vector<Node>& postOrder,
      PredecessorSets& predecessorSets, NodeToIndexMap& nodeToPostOrderIndex,
      JS::ubi::Vector<JS::ubi::Vector<uint32_t>>& predecessorVectors) {
    MOZ_ASSERT(postOrder.length() < UINT32_MAX);
    uint32_t length = postOrder.length();

    MOZ_ASSERT(predecessorVectors.length() == 0);
    if (!predecessorVectors.growBy(length)) {
      return false;
    }

    for (uint32_t i = 0; i < length - 1; i++) {
      auto& node = postOrder[i];
      MOZ_ASSERT(node != root,
                 "Only the last node should be root, since this was a post "
                 "order traversal.");

      auto ptr = predecessorSets.lookup(node);
      MOZ_ASSERT(ptr,
                 "Because this isn't the root, it had better have "
                 "predecessors, or else how "
                 "did we even find it.");

      auto& predecessors = ptr->value();
      if (!predecessorVectors[i].reserve(predecessors->count())) {
        return false;
      }
      for (auto range = predecessors->all(); !range.empty(); range.popFront()) {
        auto ptr = nodeToPostOrderIndex.lookup(range.front());
        MOZ_ASSERT(ptr);
        predecessorVectors[i].infallibleAppend(ptr->value());
      }
    }
    predecessorSets.clearAndCompact();
    return true;
  }

  // Initialize `doms` such that the immediate dominator of the `root` is the
  // `root` itself and all others are `UNDEFINED`.
  [[nodiscard]] static bool initializeDominators(
      JS::ubi::Vector<uint32_t>& doms, uint32_t length) {
    MOZ_ASSERT(doms.length() == 0);
    if (!doms.growByUninitialized(length)) {
      return false;
    }
    doms[length - 1] = length - 1;
    for (uint32_t i = 0; i < length - 1; i++) {
      doms[i] = UNDEFINED;
    }
    return true;
  }

  void assertSanity() const {
    MOZ_ASSERT(postOrder.length() == doms.length());
    MOZ_ASSERT(postOrder.length() == nodeToPostOrderIndex.count());
    MOZ_ASSERT_IF(retainedSizes.isSome(),
                  postOrder.length() == retainedSizes->length());
  }

  [[nodiscard]] bool computeRetainedSizes(mozilla::MallocSizeOf mallocSizeOf) {
    MOZ_ASSERT(retainedSizes.isNothing());
    auto length = postOrder.length();

    retainedSizes.emplace();
    if (!retainedSizes->growBy(length)) {
      retainedSizes = mozilla::Nothing();
      return false;
    }

    // Iterate in forward order so that we know all of a node's children in
    // the dominator tree have already had their retained size
    // computed. Then we can simply say that the retained size of a node is
    // its shallow size (JS::ubi::Node::size) plus the retained sizes of its
    // immediate children in the tree.

    for (uint32_t i = 0; i < length; i++) {
      auto size = postOrder[i].size(mallocSizeOf);

      for (const auto& dominated : dominatedSets.dominatedSet(postOrder, i)) {
        // The root node dominates itself, but shouldn't contribute to
        // its own retained size.
        if (dominated == postOrder[length - 1]) {
          MOZ_ASSERT(i == length - 1);
          continue;
        }

        auto ptr = nodeToPostOrderIndex.lookup(dominated);
        MOZ_ASSERT(ptr);
        auto idxOfDominated = ptr->value();
        MOZ_ASSERT(idxOfDominated < i);
        size += retainedSizes.ref()[idxOfDominated];
      }

      retainedSizes.ref()[i] = size;
    }

    return true;
  }

 public:
  // DominatorTree is not copy-able.
  DominatorTree(const DominatorTree&) = delete;
  DominatorTree& operator=(const DominatorTree&) = delete;

  // DominatorTree is move-able.
  DominatorTree(DominatorTree&& rhs)
      : postOrder(std::move(rhs.postOrder)),
        nodeToPostOrderIndex(std::move(rhs.nodeToPostOrderIndex)),
        doms(std::move(rhs.doms)),
        dominatedSets(std::move(rhs.dominatedSets)),
        retainedSizes(std::move(rhs.retainedSizes)) {
    MOZ_ASSERT(this != &rhs, "self-move is not allowed");
  }
  DominatorTree& operator=(DominatorTree&& rhs) {
    this->~DominatorTree();
    new (this) DominatorTree(std::move(rhs));
    return *this;
  }

  /**
   * Construct a `DominatorTree` of the heap graph visible from `root`. The
   * `root` is also used as the root of the resulting dominator tree.
   *
   * The resulting `DominatorTree` instance must not outlive the
   * `JS::ubi::Node` graph it was constructed from.
   *
   *   - For `JS::ubi::Node` graphs backed by the live heap graph, this means
   *     that the `DominatorTree`'s lifetime _must_ be contained within the
   *     scope of the provided `AutoCheckCannotGC` reference because a GC will
   *     invalidate the nodes.
   *
   *   - For `JS::ubi::Node` graphs backed by some other offline structure
   *     provided by the embedder, the resulting `DominatorTree`'s lifetime is
   *     bounded by that offline structure's lifetime.
   *
   * In practice, this means that within SpiderMonkey we must treat
   * `DominatorTree` as if it were backed by the live heap graph and trust
   * that embedders with knowledge of the graph's implementation will do the
   * Right Thing.
   *
   * Returns `mozilla::Nothing()` on OOM failure. It is the caller's
   * responsibility to handle and report the OOM.
   */
  static mozilla::Maybe<DominatorTree> Create(JSContext* cx,
                                              AutoCheckCannotGC& noGC,
                                              const Node& root) {
    JS::ubi::Vector<Node> postOrder;
    PredecessorSets predecessorSets;
    if (!doTraversal(cx, noGC, root, postOrder, predecessorSets)) {
      return mozilla::Nothing();
    }

    MOZ_ASSERT(postOrder.length() < UINT32_MAX);
    uint32_t length = postOrder.length();
    MOZ_ASSERT(postOrder[length - 1] == root);

    // From here on out we wish to avoid hash table lookups, and we use
    // indices into `postOrder` instead of actual nodes wherever
    // possible. This greatly improves the performance of this
    // implementation, but we have to pay a little bit of upfront cost to
    // convert our data structures to play along first.

    NodeToIndexMap nodeToPostOrderIndex(postOrder.length());
    if (!mapNodesToTheirIndices(postOrder, nodeToPostOrderIndex)) {
      return mozilla::Nothing();
    }

    JS::ubi::Vector<JS::ubi::Vector<uint32_t>> predecessorVectors;
    if (!convertPredecessorSetsToVectors(root, postOrder, predecessorSets,
                                         nodeToPostOrderIndex,
                                         predecessorVectors))
      return mozilla::Nothing();

    JS::ubi::Vector<uint32_t> doms;
    if (!initializeDominators(doms, length)) {
      return mozilla::Nothing();
    }

    bool changed = true;
    while (changed) {
      changed = false;

      // Iterate over the non-root nodes in reverse post order.
      for (uint32_t indexPlusOne = length - 1; indexPlusOne > 0;
           indexPlusOne--) {
        MOZ_ASSERT(postOrder[indexPlusOne - 1] != root);

        // Take the intersection of every predecessor's dominator set;
        // that is the current best guess at the immediate dominator for
        // this node.

        uint32_t newIDomIdx = UNDEFINED;

        auto& predecessors = predecessorVectors[indexPlusOne - 1];
        auto range = predecessors.all();
        for (; !range.empty(); range.popFront()) {
          auto idx = range.front();
          if (doms[idx] != UNDEFINED) {
            newIDomIdx = idx;
            break;
          }
        }

        MOZ_ASSERT(newIDomIdx != UNDEFINED,
                   "Because the root is initialized to dominate itself and is "
                   "the first "
                   "node in every path, there must exist a predecessor to this "
                   "node that "
                   "also has a dominator.");

        for (; !range.empty(); range.popFront()) {
          auto idx = range.front();
          if (doms[idx] != UNDEFINED) {
            newIDomIdx = intersect(doms, newIDomIdx, idx);
          }
        }

        // If the immediate dominator changed, we will have to do
        // another pass of the outer while loop to continue the forward
        // dataflow.
        if (newIDomIdx != doms[indexPlusOne - 1]) {
          doms[indexPlusOne - 1] = newIDomIdx;
          changed = true;
        }
      }
    }

    auto maybeDominatedSets = DominatedSets::Create(doms);
    if (maybeDominatedSets.isNothing()) {
      return mozilla::Nothing();
    }

    return mozilla::Some(
        DominatorTree(std::move(postOrder), std::move(nodeToPostOrderIndex),
                      std::move(doms), std::move(*maybeDominatedSets)));
  }

  /**
   * Get the root node for this dominator tree.
   */
  const Node& root() const { return postOrder[postOrder.length() - 1]; }

  /**
   * Return the immediate dominator of the given `node`. If `node` was not
   * reachable from the `root` that this dominator tree was constructed from,
   * then return the null `JS::ubi::Node`.
   */
  Node getImmediateDominator(const Node& node) const {
    assertSanity();
    auto ptr = nodeToPostOrderIndex.lookup(node);
    if (!ptr) {
      return Node();
    }

    auto idx = ptr->value();
    MOZ_ASSERT(idx < postOrder.length());
    return postOrder[doms[idx]];
  }

  /**
   * Get the set of nodes immediately dominated by the given `node`. If `node`
   * is not a member of this dominator tree, return `Nothing`.
   *
   * Example usage:
   *
   *     mozilla::Maybe<DominatedSetRange> range =
   *         myDominatorTree.getDominatedSet(myNode);
   *     if (range.isNothing()) {
   *         // Handle unknown node however you see fit...
   *         return false;
   *     }
   *
   *     for (const JS::ubi::Node& dominatedNode : *range) {
   *         // Do something with each immediately dominated node...
   *     }
   */
  mozilla::Maybe<DominatedSetRange> getDominatedSet(const Node& node) {
    assertSanity();
    auto ptr = nodeToPostOrderIndex.lookup(node);
    if (!ptr) {
      return mozilla::Nothing();
    }

    auto idx = ptr->value();
    MOZ_ASSERT(idx < postOrder.length());
    return mozilla::Some(dominatedSets.dominatedSet(postOrder, idx));
  }

  /**
   * Get the retained size of the given `node`. The size is placed in
   * `outSize`, or 0 if `node` is not a member of the dominator tree. Returns
   * false on OOM failure, leaving `outSize` unchanged.
   */
  [[nodiscard]] bool getRetainedSize(const Node& node,
                                     mozilla::MallocSizeOf mallocSizeOf,
                                     Node::Size& outSize) {
    assertSanity();
    auto ptr = nodeToPostOrderIndex.lookup(node);
    if (!ptr) {
      outSize = 0;
      return true;
    }

    if (retainedSizes.isNothing() && !computeRetainedSizes(mallocSizeOf)) {
      return false;
    }

    auto idx = ptr->value();
    MOZ_ASSERT(idx < postOrder.length());
    outSize = retainedSizes.ref()[idx];
    return true;
  }
};

}  // namespace ubi
}  // namespace JS

#endif  // js_UbiNodeDominatorTree_h