2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2015-11-17 21:02:26 +03:00
|
|
|
* 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
|
|
|
|
|
2016-06-17 15:52:44 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-11-17 21:02:26 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2020-01-20 19:18:40 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2018-02-21 19:30:19 +03:00
|
|
|
#include "js/AllocPolicy.h"
|
2015-11-17 21:02:26 +03:00
|
|
|
#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
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_PUBLIC_API DominatorTree {
|
2015-11-17 21:02:26 +03:00
|
|
|
private:
|
2015-11-19 19:10:13 +03:00
|
|
|
// Types.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
using PredecessorSets = js::HashMap<Node, NodeSetPtr, js::DefaultHasher<Node>,
|
|
|
|
js::SystemAllocPolicy>;
|
|
|
|
using NodeToIndexMap = js::HashMap<Node, uint32_t, js::DefaultHasher<Node>,
|
|
|
|
js::SystemAllocPolicy>;
|
2015-11-19 19:10:13 +03:00
|
|
|
class DominatedSets;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
public:
|
2015-11-19 19:10:13 +03:00
|
|
|
class DominatedSetRange;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
/**
|
2015-11-19 19:10:13 +03:00
|
|
|
* A pointer to an immediately dominated node.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-19 19:10:13 +03:00
|
|
|
* 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`.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-19 19:10:13 +03:00
|
|
|
* @see JS::ubi::DominatorTree::getDominatedSet
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2015-11-19 19:10:13 +03:00
|
|
|
class DominatedNodePtr {
|
|
|
|
friend class DominatedSetRange;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
const JS::ubi::Vector<Node>& postOrder;
|
2015-11-19 19:10:13 +03:00
|
|
|
const uint32_t* ptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
DominatedNodePtr(const JS::ubi::Vector<Node>& postOrder,
|
|
|
|
const uint32_t* ptr)
|
2015-11-19 19:10:13 +03:00
|
|
|
: postOrder(postOrder), ptr(ptr) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
public:
|
2015-11-19 19:10:13 +03:00
|
|
|
bool operator!=(const DominatedNodePtr& rhs) const {
|
|
|
|
return ptr != rhs.ptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-19 19:10:13 +03:00
|
|
|
void operator++() { ptr++; }
|
|
|
|
const Node& operator*() const { return postOrder[*ptr]; }
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-11-19 19:10:13 +03:00
|
|
|
* A range of immediately dominated `JS::ubi::Node`s for use with
|
|
|
|
* range-based for loops.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-19 19:10:13 +03:00
|
|
|
* @see JS::ubi::DominatorTree::getDominatedSet
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2015-11-19 19:10:13 +03:00
|
|
|
class DominatedSetRange {
|
|
|
|
friend class DominatedSets;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
const JS::ubi::Vector<Node>& postOrder;
|
2015-11-19 19:10:13 +03:00
|
|
|
const uint32_t* beginPtr;
|
|
|
|
const uint32_t* endPtr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
DominatedSetRange(JS::ubi::Vector<Node>& postOrder, const uint32_t* begin,
|
|
|
|
const uint32_t* end)
|
|
|
|
: postOrder(postOrder), beginPtr(begin), endPtr(end) {
|
2015-11-19 19:10:13 +03:00
|
|
|
MOZ_ASSERT(begin <= end);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-19 19:10:13 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
public:
|
|
|
|
DominatedNodePtr begin() const {
|
|
|
|
MOZ_ASSERT(beginPtr <= endPtr);
|
|
|
|
return DominatedNodePtr(postOrder, beginPtr);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-19 19:10:13 +03:00
|
|
|
|
|
|
|
DominatedNodePtr end() const { return DominatedNodePtr(postOrder, endPtr); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
size_t length() const {
|
|
|
|
MOZ_ASSERT(beginPtr <= endPtr);
|
2015-12-01 04:38:06 +03:00
|
|
|
return endPtr - beginPtr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-19 19:10:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2018-11-30 13:46:48 +03:00
|
|
|
* }
|
2015-11-19 19:10:13 +03:00
|
|
|
*
|
|
|
|
* // Don't care about the first ten, for whatever reason.
|
|
|
|
* range->skip(10);
|
|
|
|
* for (const JS::ubi::Node& dominatedNode : *range) {
|
2018-11-30 13:46:48 +03:00
|
|
|
* // ...
|
|
|
|
* }
|
2015-11-19 19:10:13 +03:00
|
|
|
*/
|
|
|
|
void skip(size_t n) {
|
|
|
|
beginPtr += n;
|
|
|
|
if (beginPtr > endPtr) {
|
|
|
|
beginPtr = endPtr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
private:
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2016-06-18 12:46:13 +03:00
|
|
|
* The set of all dominated sets in a dominator tree.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2016-06-18 12:46:13 +03:00
|
|
|
* Internally stores the sets in a contiguous array, with a side table of
|
2015-11-19 19:10:13 +03:00
|
|
|
* indices into that contiguous array to denote the start index of each
|
|
|
|
* individual set.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2015-11-19 19:10:13 +03:00
|
|
|
class DominatedSets {
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> dominated;
|
2015-11-19 19:10:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> indices;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
DominatedSets(JS::ubi::Vector<uint32_t>&& dominated,
|
|
|
|
JS::ubi::Vector<uint32_t>&& indices)
|
|
|
|
: dominated(std::move(dominated)), indices(std::move(indices)) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
public:
|
2015-11-19 19:10:13 +03:00
|
|
|
// DominatedSets is not copy-able.
|
|
|
|
DominatedSets(const DominatedSets& rhs) = delete;
|
|
|
|
DominatedSets& operator=(const DominatedSets& rhs) = delete;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// 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.
|
|
|
|
*/
|
2016-06-18 12:46:13 +03:00
|
|
|
static mozilla::Maybe<DominatedSets> Create(
|
|
|
|
const JS::ubi::Vector<uint32_t>& doms) {
|
|
|
|
auto length = doms.length();
|
|
|
|
MOZ_ASSERT(length < UINT32_MAX);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// Create a vector `dominated` holding a flattened set of buckets of
|
|
|
|
// immediately dominated children nodes, with a lookup table
|
2018-05-30 22:15:35 +03:00
|
|
|
// `indices` mapping from each node to the beginning of its bucket.
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2015-11-19 19:10:13 +03:00
|
|
|
// This has three phases:
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2015-11-19 19:10:13 +03:00
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2018-05-30 22:15:35 +03:00
|
|
|
// 2. Convert the `indices` vector to store the cumulative sum of
|
2015-11-19 19:10:13 +03:00
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2015-11-19 19:10:13 +03:00
|
|
|
// 3. Iterate over the full set of nodes again, filling in bucket
|
|
|
|
// entries from the end of the bucket's range to its
|
2016-06-18 12:46:13 +03:00
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> dominated;
|
2015-11-19 19:10:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> indices;
|
|
|
|
if (!dominated.growBy(length) || !indices.growBy(length)) {
|
|
|
|
return mozilla::Nothing();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// 1
|
|
|
|
memset(indices.begin(), 0, length * sizeof(uint32_t));
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
indices[doms[i]]++;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// 2
|
|
|
|
uint32_t sumOfSizes = 0;
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
sumOfSizes += indices[i];
|
|
|
|
MOZ_ASSERT(sumOfSizes <= length);
|
|
|
|
indices[i] = sumOfSizes;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// 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(
|
2018-05-30 22:15:35 +03:00
|
|
|
DominatedSets(std::move(dominated), std::move(indices)));
|
2015-11-17 21:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the set of nodes immediately dominated by the node at
|
2016-07-23 20:52:25 +03:00
|
|
|
* `postOrder[nodeIndex]`.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2016-06-18 12:46:13 +03:00
|
|
|
DominatedSetRange dominatedSet(JS::ubi::Vector<Node>& postOrder,
|
2015-11-17 21:02:26 +03:00
|
|
|
uint32_t nodeIndex) const {
|
|
|
|
MOZ_ASSERT(postOrder.length() == indices.length());
|
2015-11-19 01:12:23 +03:00
|
|
|
MOZ_ASSERT(nodeIndex < indices.length());
|
|
|
|
auto end = nodeIndex == indices.length() - 1
|
|
|
|
? dominated.end()
|
|
|
|
: &dominated[indices[nodeIndex + 1]];
|
2018-05-30 22:15:35 +03:00
|
|
|
return DominatedSetRange(postOrder, &dominated[indices[nodeIndex]], end);
|
2015-11-17 21:02:26 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
private:
|
|
|
|
// Data members.
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<Node> postOrder;
|
2015-11-17 21:02:26 +03:00
|
|
|
NodeToIndexMap nodeToPostOrderIndex;
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> doms;
|
2015-11-17 21:02:26 +03:00
|
|
|
DominatedSets dominatedSets;
|
|
|
|
mozilla::Maybe<JS::ubi::Vector<JS::ubi::Node::Size>> retainedSizes;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
private:
|
2015-11-17 21:02:26 +03:00
|
|
|
// 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.
|
2016-06-18 12:46:13 +03:00
|
|
|
static const uint32_t UNDEFINED = UINT32_MAX;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
DominatorTree(JS::ubi::Vector<Node>&& postOrder,
|
2016-06-17 15:52:44 +03:00
|
|
|
NodeToIndexMap&& nodeToPostOrderIndex,
|
2015-11-17 21:02:26 +03:00
|
|
|
JS::ubi::Vector<uint32_t>&& doms, DominatedSets&& dominatedSets)
|
|
|
|
: postOrder(std::move(postOrder)),
|
|
|
|
nodeToPostOrderIndex(std::move(nodeToPostOrderIndex)),
|
2018-05-30 22:15:35 +03:00
|
|
|
doms(std::move(doms)),
|
|
|
|
dominatedSets(std::move(dominatedSets)),
|
2015-11-17 21:02:26 +03:00
|
|
|
retainedSizes(mozilla::Nothing()) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
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];
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
}
|
|
|
|
return finger1;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// Do the post order traversal of the heap graph and populate our
|
|
|
|
// predecessor sets.
|
2021-02-12 07:09:23 +03:00
|
|
|
[[nodiscard]] static bool doTraversal(JSContext* cx, AutoCheckCannotGC& noGC,
|
|
|
|
const Node& root,
|
|
|
|
JS::ubi::Vector<Node>& postOrder,
|
|
|
|
PredecessorSets& predecessorSets) {
|
2015-11-17 21:02:26 +03:00
|
|
|
uint32_t nodeCount = 0;
|
|
|
|
auto onNode = [&](const Node& node) {
|
|
|
|
nodeCount++;
|
|
|
|
if (MOZ_UNLIKELY(nodeCount == UINT32_MAX)) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
return postOrder.append(node);
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2015-11-17 21:02:26 +03:00
|
|
|
|
|
|
|
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;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
MOZ_ASSERT(p && p->value());
|
|
|
|
return p->value()->put(origin);
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2015-11-17 21:02:26 +03:00
|
|
|
|
|
|
|
PostOrder traversal(cx, noGC);
|
|
|
|
return traversal.addStart(root) && traversal.traverse(onNode, onEdge);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// Populates the given `map` with an entry for each node to its index in
|
|
|
|
// `postOrder`.
|
2021-02-12 07:09:23 +03:00
|
|
|
[[nodiscard]] static bool mapNodesToTheirIndices(
|
2015-11-17 21:02:26 +03:00
|
|
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// Convert the Node -> NodeSet predecessorSets to a index -> Vector<index>
|
2018-11-30 13:46:48 +03:00
|
|
|
// form.
|
2021-02-12 07:09:23 +03:00
|
|
|
[[nodiscard]] static bool convertPredecessorSetsToVectors(
|
2015-11-17 21:02:26 +03:00
|
|
|
const Node& root, JS::ubi::Vector<Node>& postOrder,
|
|
|
|
PredecessorSets& predecessorSets, NodeToIndexMap& nodeToPostOrderIndex,
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<JS::ubi::Vector<uint32_t>>& predecessorVectors) {
|
2015-11-17 21:02:26 +03:00
|
|
|
MOZ_ASSERT(postOrder.length() < UINT32_MAX);
|
2015-11-20 20:08:15 +03:00
|
|
|
uint32_t length = postOrder.length();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
MOZ_ASSERT(predecessorVectors.length() == 0);
|
|
|
|
if (!predecessorVectors.growBy(length)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
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 "
|
2016-07-23 20:52:25 +03:00
|
|
|
"order traversal.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
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.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
auto& predecessors = ptr->value();
|
|
|
|
if (!predecessorVectors[i].reserve(predecessors->count())) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
for (auto range = predecessors->all(); !range.empty(); range.popFront()) {
|
2015-11-20 20:08:15 +03:00
|
|
|
auto ptr = nodeToPostOrderIndex.lookup(range.front());
|
|
|
|
MOZ_ASSERT(ptr);
|
2015-11-17 21:02:26 +03:00
|
|
|
predecessorVectors[i].infallibleAppend(ptr->value());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.
Pros:
- No need for init() calls and subsequent checks.
- No memory allocated for empty tables, which are not that uncommon.
Cons:
- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
because the existing checkOverloaded() can handle it.
Specifics:
- Construction now can take a length parameter.
- init() is removed. Explicit length-setting, when necessary, now occurs in the
constructors.
- initialized() is removed.
- capacity() now returns zero when the entry storage is absent.
- lookupForAdd() is no longer `const`, because it can instantiate the storage,
which requires modifications.
- lookupForAdd() can now return an invalid AddPtr in two cases:
- old: hashing failure (due to OOM in the hasher)
- new: OOM while instantiating entry storage
The existing failure handling paths for the old case work for the new case.
- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
and reserve(). The old compactIfUnderloaded() is also removed.
- Capacity computation code is now in its own functions, bestCapacity() and
hashShift(). setTableSizeLog2() is removed.
- uint32_t is used throughout for capacities, instead of size_t, for
consistency with other similar values.
- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
handle !mTable.
Measurements:
- Total source code size is reduced by over 900 lines. Also, lots of existing
lines got shorter (i.e. two checks were reduced to one).
- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
are compensated for by the lack of init() calls.
- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
increased from 2.84 billion to 2.89 billion but any execution time change was
well below noise.
2018-08-10 11:00:29 +03:00
|
|
|
predecessorSets.clearAndCompact();
|
2015-11-17 21:02:26 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// Initialize `doms` such that the immediate dominator of the `root` is the
|
|
|
|
// `root` itself and all others are `UNDEFINED`.
|
2021-02-12 07:09:23 +03:00
|
|
|
[[nodiscard]] static bool initializeDominators(
|
|
|
|
JS::ubi::Vector<uint32_t>& doms, uint32_t length) {
|
2015-11-17 21:02:26 +03:00
|
|
|
MOZ_ASSERT(doms.length() == 0);
|
|
|
|
if (!doms.growByUninitialized(length)) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
doms[length - 1] = length - 1;
|
|
|
|
for (uint32_t i = 0; i < length - 1; i++) {
|
|
|
|
doms[i] = UNDEFINED;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
void assertSanity() const {
|
2015-11-19 19:10:13 +03:00
|
|
|
MOZ_ASSERT(postOrder.length() == doms.length());
|
Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.
Pros:
- No need for init() calls and subsequent checks.
- No memory allocated for empty tables, which are not that uncommon.
Cons:
- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
because the existing checkOverloaded() can handle it.
Specifics:
- Construction now can take a length parameter.
- init() is removed. Explicit length-setting, when necessary, now occurs in the
constructors.
- initialized() is removed.
- capacity() now returns zero when the entry storage is absent.
- lookupForAdd() is no longer `const`, because it can instantiate the storage,
which requires modifications.
- lookupForAdd() can now return an invalid AddPtr in two cases:
- old: hashing failure (due to OOM in the hasher)
- new: OOM while instantiating entry storage
The existing failure handling paths for the old case work for the new case.
- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
and reserve(). The old compactIfUnderloaded() is also removed.
- Capacity computation code is now in its own functions, bestCapacity() and
hashShift(). setTableSizeLog2() is removed.
- uint32_t is used throughout for capacities, instead of size_t, for
consistency with other similar values.
- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
handle !mTable.
Measurements:
- Total source code size is reduced by over 900 lines. Also, lots of existing
lines got shorter (i.e. two checks were reduced to one).
- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
are compensated for by the lack of init() calls.
- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
increased from 2.84 billion to 2.89 billion but any execution time change was
well below noise.
2018-08-10 11:00:29 +03:00
|
|
|
MOZ_ASSERT(postOrder.length() == nodeToPostOrderIndex.count());
|
2015-11-20 20:08:15 +03:00
|
|
|
MOZ_ASSERT_IF(retainedSizes.isSome(),
|
2015-11-19 19:10:13 +03:00
|
|
|
postOrder.length() == retainedSizes->length());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool computeRetainedSizes(mozilla::MallocSizeOf mallocSizeOf) {
|
2015-11-20 20:08:15 +03:00
|
|
|
MOZ_ASSERT(retainedSizes.isNothing());
|
|
|
|
auto length = postOrder.length();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
retainedSizes.emplace();
|
|
|
|
if (!retainedSizes->growBy(length)) {
|
|
|
|
retainedSizes = mozilla::Nothing();
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-20 20:08:15 +03:00
|
|
|
|
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
auto size = postOrder[i].size(mallocSizeOf);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
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;
|
2015-11-17 21:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
public:
|
|
|
|
// DominatorTree is not copy-able.
|
|
|
|
DominatorTree(const DominatorTree&) = delete;
|
|
|
|
DominatorTree& operator=(const DominatorTree&) = delete;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// DominatorTree is move-able.
|
|
|
|
DominatorTree(DominatorTree&& rhs)
|
2018-05-30 22:15:35 +03:00
|
|
|
: 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)) {
|
2015-11-17 21:02:26 +03:00
|
|
|
MOZ_ASSERT(this != &rhs, "self-move is not allowed");
|
|
|
|
}
|
|
|
|
DominatorTree& operator=(DominatorTree&& rhs) {
|
|
|
|
this->~DominatorTree();
|
2018-05-30 22:15:35 +03:00
|
|
|
new (this) DominatorTree(std::move(rhs));
|
|
|
|
return *this;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-17 21:02:26 +03:00
|
|
|
* Construct a `DominatorTree` of the heap graph visible from `root`. The
|
2018-05-30 22:15:35 +03:00
|
|
|
* `root` is also used as the root of the resulting dominator tree.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2018-05-30 22:15:35 +03:00
|
|
|
* The resulting `DominatorTree` instance must not outlive the
|
2015-11-17 21:02:26 +03:00
|
|
|
* `JS::ubi::Node` graph it was constructed from.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2018-05-30 22:15:35 +03:00
|
|
|
* - For `JS::ubi::Node` graphs backed by the live heap graph, this means
|
2015-11-17 21:02:26 +03:00
|
|
|
* that the `DominatorTree`'s lifetime _must_ be contained within the
|
|
|
|
* scope of the provided `AutoCheckCannotGC` reference because a GC will
|
|
|
|
* invalidate the nodes.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-17 21:02:26 +03:00
|
|
|
* - For `JS::ubi::Node` graphs backed by some other offline structure
|
2018-05-30 22:15:35 +03:00
|
|
|
* provided by the embedder, the resulting `DominatorTree`'s lifetime is
|
|
|
|
* bounded by that offline structure's lifetime.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-17 21:02:26 +03:00
|
|
|
* 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2015-11-17 21:02:26 +03:00
|
|
|
* Returns `mozilla::Nothing()` on OOM failure. It is the caller's
|
|
|
|
* responsibility to handle and report the OOM.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2018-05-30 22:15:35 +03:00
|
|
|
static mozilla::Maybe<DominatorTree> Create(JSContext* cx,
|
2016-07-23 20:52:25 +03:00
|
|
|
AutoCheckCannotGC& noGC,
|
|
|
|
const Node& root) {
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<Node> postOrder;
|
2015-11-17 21:02:26 +03:00
|
|
|
PredecessorSets predecessorSets;
|
2018-05-30 22:15:35 +03:00
|
|
|
if (!doTraversal(cx, noGC, root, postOrder, predecessorSets)) {
|
2015-11-17 21:02:26 +03:00
|
|
|
return mozilla::Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(postOrder.length() < UINT32_MAX);
|
|
|
|
uint32_t length = postOrder.length();
|
Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.
Pros:
- No need for init() calls and subsequent checks.
- No memory allocated for empty tables, which are not that uncommon.
Cons:
- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
because the existing checkOverloaded() can handle it.
Specifics:
- Construction now can take a length parameter.
- init() is removed. Explicit length-setting, when necessary, now occurs in the
constructors.
- initialized() is removed.
- capacity() now returns zero when the entry storage is absent.
- lookupForAdd() is no longer `const`, because it can instantiate the storage,
which requires modifications.
- lookupForAdd() can now return an invalid AddPtr in two cases:
- old: hashing failure (due to OOM in the hasher)
- new: OOM while instantiating entry storage
The existing failure handling paths for the old case work for the new case.
- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
and reserve(). The old compactIfUnderloaded() is also removed.
- Capacity computation code is now in its own functions, bestCapacity() and
hashShift(). setTableSizeLog2() is removed.
- uint32_t is used throughout for capacities, instead of size_t, for
consistency with other similar values.
- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
handle !mTable.
Measurements:
- Total source code size is reduced by over 900 lines. Also, lots of existing
lines got shorter (i.e. two checks were reduced to one).
- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
are compensated for by the lack of init() calls.
- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
increased from 2.84 billion to 2.89 billion but any execution time change was
well below noise.
2018-08-10 11:00:29 +03:00
|
|
|
MOZ_ASSERT(postOrder[length - 1] == root);
|
2015-11-17 21:02:26 +03:00
|
|
|
|
|
|
|
// 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();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
|
Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.
Pros:
- No need for init() calls and subsequent checks.
- No memory allocated for empty tables, which are not that uncommon.
Cons:
- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
because the existing checkOverloaded() can handle it.
Specifics:
- Construction now can take a length parameter.
- init() is removed. Explicit length-setting, when necessary, now occurs in the
constructors.
- initialized() is removed.
- capacity() now returns zero when the entry storage is absent.
- lookupForAdd() is no longer `const`, because it can instantiate the storage,
which requires modifications.
- lookupForAdd() can now return an invalid AddPtr in two cases:
- old: hashing failure (due to OOM in the hasher)
- new: OOM while instantiating entry storage
The existing failure handling paths for the old case work for the new case.
- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
and reserve(). The old compactIfUnderloaded() is also removed.
- Capacity computation code is now in its own functions, bestCapacity() and
hashShift(). setTableSizeLog2() is removed.
- uint32_t is used throughout for capacities, instead of size_t, for
consistency with other similar values.
- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
handle !mTable.
Measurements:
- Total source code size is reduced by over 900 lines. Also, lots of existing
lines got shorter (i.e. two checks were reduced to one).
- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
are compensated for by the lack of init() calls.
- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
increased from 2.84 billion to 2.89 billion but any execution time change was
well below noise.
2018-08-10 11:00:29 +03:00
|
|
|
JS::ubi::Vector<JS::ubi::Vector<uint32_t>> predecessorVectors;
|
2015-11-17 21:02:26 +03:00
|
|
|
if (!convertPredecessorSetsToVectors(root, postOrder, predecessorSets,
|
|
|
|
nodeToPostOrderIndex,
|
|
|
|
predecessorVectors))
|
|
|
|
return mozilla::Nothing();
|
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
JS::ubi::Vector<uint32_t> doms;
|
2015-11-17 21:02:26 +03:00
|
|
|
if (!initializeDominators(doms, length)) {
|
|
|
|
return mozilla::Nothing();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
|
|
|
|
bool changed = true;
|
|
|
|
while (changed) {
|
|
|
|
changed = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
// Iterate over the non-root nodes in reverse post order.
|
2016-06-18 12:46:13 +03:00
|
|
|
for (uint32_t indexPlusOne = length - 1; indexPlusOne > 0;
|
2015-11-17 21:02:26 +03:00
|
|
|
indexPlusOne--) {
|
|
|
|
MOZ_ASSERT(postOrder[indexPlusOne - 1] != root);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
// Take the intersection of every predecessor's dominator set;
|
2015-11-17 21:02:26 +03:00
|
|
|
// that is the current best guess at the immediate dominator for
|
|
|
|
// this node.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-18 12:46:13 +03:00
|
|
|
uint32_t newIDomIdx = UNDEFINED;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
|
|
|
|
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.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
for (; !range.empty(); range.popFront()) {
|
|
|
|
auto idx = range.front();
|
|
|
|
if (doms[idx] != UNDEFINED) {
|
|
|
|
newIDomIdx = intersect(doms, newIDomIdx, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
// If the immediate dominator changed, we will have to do
|
|
|
|
// another pass of the outer while loop to continue the forward
|
|
|
|
// dataflow.
|
2015-11-17 21:02:26 +03:00
|
|
|
if (newIDomIdx != doms[indexPlusOne - 1]) {
|
2015-11-19 19:10:13 +03:00
|
|
|
doms[indexPlusOne - 1] = newIDomIdx;
|
|
|
|
changed = true;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
auto maybeDominatedSets = DominatedSets::Create(doms);
|
|
|
|
if (maybeDominatedSets.isNothing()) {
|
|
|
|
return mozilla::Nothing();
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
return mozilla::Some(
|
2016-06-18 12:46:13 +03:00
|
|
|
DominatorTree(std::move(postOrder), std::move(nodeToPostOrderIndex),
|
2018-05-30 22:15:35 +03:00
|
|
|
std::move(doms), std::move(*maybeDominatedSets)));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
/**
|
|
|
|
* Get the root node for this dominator tree.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2015-11-19 19:10:13 +03:00
|
|
|
const Node& root() const { return postOrder[postOrder.length() - 1]; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
/**
|
2015-11-17 21:02:26 +03:00
|
|
|
* 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();
|
|
|
|
}
|
2015-11-19 19:10:13 +03:00
|
|
|
|
2015-11-17 21:02:26 +03:00
|
|
|
auto idx = ptr->value();
|
|
|
|
MOZ_ASSERT(idx < postOrder.length());
|
|
|
|
return postOrder[doms[idx]];
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
/**
|
|
|
|
* 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();
|
2015-11-20 20:08:15 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 19:10:13 +03:00
|
|
|
auto idx = ptr->value();
|
|
|
|
MOZ_ASSERT(idx < postOrder.length());
|
|
|
|
return mozilla::Some(dominatedSets.dominatedSet(postOrder, idx));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-02-12 07:09:22 +03:00
|
|
|
[[nodiscard]] bool getRetainedSize(const Node& node,
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf,
|
|
|
|
Node::Size& outSize) {
|
2015-11-20 20:08:15 +03:00
|
|
|
assertSanity();
|
|
|
|
auto ptr = nodeToPostOrderIndex.lookup(node);
|
|
|
|
if (!ptr) {
|
|
|
|
outSize = 0;
|
|
|
|
return true;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2015-11-20 20:08:15 +03:00
|
|
|
|
|
|
|
if (retainedSizes.isNothing() && !computeRetainedSizes(mallocSizeOf)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-20 20:08:15 +03:00
|
|
|
auto idx = ptr->value();
|
|
|
|
MOZ_ASSERT(idx < postOrder.length());
|
|
|
|
outSize = retainedSizes.ref()[idx];
|
2015-11-17 21:02:26 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-17 21:02:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ubi
|
|
|
|
} // namespace JS
|
|
|
|
|
|
|
|
#endif // js_UbiNodeDominatorTree_h
|