"src/compiler/common-operator.h",
"src/compiler/control-builders.cc",
"src/compiler/control-builders.h",
+ "src/compiler/control-equivalence.h",
"src/compiler/control-reducer.cc",
"src/compiler/control-reducer.h",
"src/compiler/diamond.h",
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef V8_COMPILER_CONTROL_EQUIVALENCE_H_
+#define V8_COMPILER_CONTROL_EQUIVALENCE_H_
+
+#include "src/v8.h"
+
+#include "src/compiler/graph.h"
+#include "src/compiler/node.h"
+#include "src/compiler/node-properties.h"
+#include "src/zone-containers.h"
+
+namespace v8 {
+namespace internal {
+namespace compiler {
+
+// Determines control dependence equivalence classes for control nodes. Any two
+// nodes having the same set of control dependences land in one class. These
+// classes can in turn be used to:
+// - Build a program structure tree (PST) for controls in the graph.
+// - Determine single-entry single-exit (SESE) regions within the graph.
+//
+// Note that this implementation actually uses cycle equivalence to establish
+// class numbers. Any two nodes are cycle equivalent if they occur in the same
+// set of cycles. It can be shown that control dependence equivalence reduces
+// to undirected cycle equivalence for strongly connected control flow graphs.
+//
+// The algorithm is based on the paper, "The program structure tree: computing
+// control regions in linear time" by Johnson, Pearson & Pingali (PLDI94) which
+// also contains proofs for the aforementioned equivalence. References to line
+// numbers in the algorithm from figure 4 have been added [line:x].
+class ControlEquivalence : public ZoneObject {
+ public:
+ ControlEquivalence(Zone* zone, Graph* graph)
+ : zone_(zone),
+ graph_(graph),
+ dfs_number_(0),
+ class_number_(1),
+ node_data_(graph->NodeCount(), EmptyData(), zone) {}
+
+ // Run the main algorithm starting from the {exit} control node. This causes
+ // the following iterations over control edges of the graph:
+ // 1) A breadth-first backwards traversal to determine the set of nodes that
+ // participate in the next step. Takes O(E) time and O(N) space.
+ // 2) An undirected depth-first backwards traversal that determines class
+ // numbers for all participating nodes. Takes O(E) time and O(N) space.
+ void Run(Node* exit) {
+ if (GetClass(exit) != kInvalidClass) return;
+ DetermineParticipation(exit);
+ RunUndirectedDFS(exit);
+ }
+
+ // Retrieves a previously computed class number.
+ size_t ClassOf(Node* node) {
+ DCHECK(GetClass(node) != kInvalidClass);
+ return GetClass(node);
+ }
+
+ private:
+ static const size_t kInvalidClass = static_cast<size_t>(-1);
+ typedef enum { kInputDirection, kUseDirection } DFSDirection;
+
+ struct Bracket {
+ DFSDirection direction; // Direction in which this bracket was added.
+ size_t recent_class; // Cached class when bracket was topmost.
+ size_t recent_size; // Cached set-size when bracket was topmost.
+ Node* from; // Node that this bracket originates from.
+ Node* to; // Node that this bracket points to.
+ };
+
+ // The set of brackets for each node during the DFS walk.
+ typedef ZoneLinkedList<Bracket> BracketList;
+
+ struct DFSStackEntry {
+ DFSDirection direction; // Direction currently used in DFS walk.
+ Node::InputEdges::iterator input; // Iterator used for "input" direction.
+ Node::UseEdges::iterator use; // Iterator used for "use" direction.
+ Node* parent_node; // Parent node of entry during DFS walk.
+ Node* node; // Node that this stack entry belongs to.
+ };
+
+ // The stack is used during the undirected DFS walk.
+ typedef ZoneStack<DFSStackEntry> DFSStack;
+
+ struct NodeData {
+ size_t class_number; // Equivalence class number assigned to node.
+ size_t dfs_number; // Pre-order DFS number assigned to node.
+ bool on_stack; // Indicates node is on DFS stack during walk.
+ bool participates; // Indicates node participates in DFS walk.
+ BracketList blist; // List of brackets per node.
+ };
+
+ // The per-node data computed during the DFS walk.
+ typedef ZoneVector<NodeData> Data;
+
+ // Called at pre-visit during DFS walk.
+ void VisitPre(Node* node) {
+ Trace("CEQ: Pre-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
+
+ // Dispense a new pre-order number.
+ SetNumber(node, NewDFSNumber());
+ Trace(" Assigned DFS number is %d\n", GetNumber(node));
+ }
+
+ // Called at mid-visit during DFS walk.
+ void VisitMid(Node* node, DFSDirection direction) {
+ Trace("CEQ: Mid-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
+ BracketList& blist = GetBracketList(node);
+
+ // Remove brackets pointing to this node [line:19].
+ BracketListDelete(blist, node, direction);
+
+ // Potentially introduce artificial dependency from start to end.
+ if (blist.empty()) {
+ DCHECK_EQ(graph_->start(), node);
+ DCHECK_EQ(kInputDirection, direction);
+ VisitBackedge(graph_->start(), graph_->end(), kInputDirection);
+ }
+
+ // Potentially start a new equivalence class [line:37].
+ BracketListTrace(blist);
+ Bracket* recent = &blist.back();
+ if (recent->recent_size != blist.size()) {
+ recent->recent_size = blist.size();
+ recent->recent_class = NewClassNumber();
+ }
+
+ // Assign equivalence class to node.
+ SetClass(node, recent->recent_class);
+ Trace(" Assigned class number is %d\n", GetClass(node));
+ }
+
+ // Called at post-visit during DFS walk.
+ void VisitPost(Node* node, Node* parent_node, DFSDirection direction) {
+ Trace("CEQ: Post-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
+ BracketList& blist = GetBracketList(node);
+
+ // Remove brackets pointing to this node [line:19].
+ BracketListDelete(blist, node, direction);
+
+ // Propagate bracket list up the DFS tree [line:13].
+ if (parent_node != NULL) {
+ BracketList& parent_blist = GetBracketList(parent_node);
+ parent_blist.splice(parent_blist.end(), blist);
+ }
+ }
+
+ // Called when hitting a back edge in the DFS walk.
+ void VisitBackedge(Node* from, Node* to, DFSDirection direction) {
+ Trace("CEQ: Backedge from #%d:%s to #%d:%s\n", from->id(),
+ from->op()->mnemonic(), to->id(), to->op()->mnemonic());
+
+ // Push backedge onto the bracket list [line:25].
+ Bracket bracket = {direction, kInvalidClass, 0, from, to};
+ GetBracketList(from).push_back(bracket);
+ }
+
+ // Performs and undirected DFS walk of the graph. Conceptually all nodes are
+ // expanded, splitting "input" and "use" out into separate nodes. During the
+ // traversal, edges towards the representative nodes are preferred.
+ //
+ // \ / - Pre-visit: When N1 is visited in direction D the preferred
+ // x N1 edge towards N is taken next, calling VisitPre(N).
+ // | - Mid-visit: After all edges out of N2 in direction D have
+ // | N been visited, we switch the direction and start considering
+ // | edges out of N1 now, and we call VisitMid(N).
+ // x N2 - Post-visit: After all edges out of N1 in direction opposite
+ // / \ to D have been visited, we pop N and call VisitPost(N).
+ //
+ // This will yield a true spanning tree (without cross or forward edges) and
+ // also discover proper back edges in both directions.
+ void RunUndirectedDFS(Node* exit) {
+ ZoneStack<DFSStackEntry> stack(zone_);
+ DFSPush(stack, exit, NULL, kInputDirection);
+ VisitPre(exit);
+
+ while (!stack.empty()) { // Undirected depth-first backwards traversal.
+ DFSStackEntry& entry = stack.top();
+ Node* node = entry.node;
+
+ if (entry.direction == kInputDirection) {
+ if (entry.input != node->input_edges().end()) {
+ Edge edge = *entry.input;
+ Node* input = edge.to();
+ ++(entry.input);
+ if (NodeProperties::IsControlEdge(edge) &&
+ NodeProperties::IsControl(input)) {
+ // Visit next control input.
+ if (!GetData(input)->participates) continue;
+ if (GetData(input)->on_stack) {
+ // Found backedge if input is on stack.
+ if (input != entry.parent_node) {
+ VisitBackedge(node, input, kInputDirection);
+ }
+ } else {
+ // Push input onto stack.
+ DFSPush(stack, input, node, kInputDirection);
+ VisitPre(input);
+ }
+ }
+ continue;
+ }
+ if (entry.use != node->use_edges().end()) {
+ // Switch direction to uses.
+ entry.direction = kUseDirection;
+ VisitMid(node, kInputDirection);
+ continue;
+ }
+ }
+
+ if (entry.direction == kUseDirection) {
+ if (entry.use != node->use_edges().end()) {
+ Edge edge = *entry.use;
+ Node* use = edge.from();
+ ++(entry.use);
+ if (NodeProperties::IsControlEdge(edge) &&
+ NodeProperties::IsControl(use)) {
+ // Visit next control use.
+ if (!GetData(use)->participates) continue;
+ if (GetData(use)->on_stack) {
+ // Found backedge if use is on stack.
+ if (use != entry.parent_node) {
+ VisitBackedge(node, use, kUseDirection);
+ }
+ } else {
+ // Push use onto stack.
+ DFSPush(stack, use, node, kUseDirection);
+ VisitPre(use);
+ }
+ }
+ continue;
+ }
+ if (entry.input != node->input_edges().end()) {
+ // Switch direction to inputs.
+ entry.direction = kInputDirection;
+ VisitMid(node, kUseDirection);
+ continue;
+ }
+ }
+
+ // Pop node from stack when done with all inputs and uses.
+ DCHECK(entry.input == node->input_edges().end());
+ DCHECK(entry.use == node->use_edges().end());
+ DFSPop(stack, node);
+ VisitPost(node, entry.parent_node, entry.direction);
+ }
+ }
+
+ void DetermineParticipationEnqueue(ZoneQueue<Node*>& queue, Node* node) {
+ if (!GetData(node)->participates) {
+ GetData(node)->participates = true;
+ queue.push(node);
+ }
+ }
+
+ void DetermineParticipation(Node* exit) {
+ ZoneQueue<Node*> queue(zone_);
+ DetermineParticipationEnqueue(queue, exit);
+ while (!queue.empty()) { // Breadth-first backwards traversal.
+ Node* node = queue.front();
+ queue.pop();
+ int max = NodeProperties::PastControlIndex(node);
+ for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
+ DetermineParticipationEnqueue(queue, node->InputAt(i));
+ }
+ }
+ }
+
+ private:
+ NodeData* GetData(Node* node) { return &node_data_[node->id()]; }
+ int NewClassNumber() { return class_number_++; }
+ int NewDFSNumber() { return dfs_number_++; }
+
+ // Template used to initialize per-node data.
+ NodeData EmptyData() {
+ return {kInvalidClass, 0, false, false, BracketList(zone_)};
+ }
+
+ // Accessors for the DFS number stored within the per-node data.
+ size_t GetNumber(Node* node) { return GetData(node)->dfs_number; }
+ void SetNumber(Node* node, size_t number) {
+ GetData(node)->dfs_number = number;
+ }
+
+ // Accessors for the equivalence class stored within the per-node data.
+ size_t GetClass(Node* node) { return GetData(node)->class_number; }
+ void SetClass(Node* node, size_t number) {
+ GetData(node)->class_number = number;
+ }
+
+ // Accessors for the bracket list stored within the per-node data.
+ BracketList& GetBracketList(Node* node) { return GetData(node)->blist; }
+ void SetBracketList(Node* node, BracketList& list) {
+ GetData(node)->blist = list;
+ }
+
+ // Mutates the DFS stack by pushing an entry.
+ void DFSPush(DFSStack& stack, Node* node, Node* from, DFSDirection dir) {
+ DCHECK(GetData(node)->participates);
+ GetData(node)->on_stack = true;
+ Node::InputEdges::iterator input = node->input_edges().begin();
+ Node::UseEdges::iterator use = node->use_edges().begin();
+ stack.push({dir, input, use, from, node});
+ }
+
+ // Mutates the DFS stack by popping an entry.
+ void DFSPop(DFSStack& stack, Node* node) {
+ DCHECK_EQ(stack.top().node, node);
+ GetData(node)->on_stack = false;
+ GetData(node)->participates = false;
+ stack.pop();
+ }
+
+ // TODO(mstarzinger): Optimize this to avoid linear search.
+ void BracketListDelete(BracketList& blist, Node* to, DFSDirection direction) {
+ for (BracketList::iterator i = blist.begin(); i != blist.end(); /*nop*/) {
+ if (i->to == to && i->direction != direction) {
+ Trace(" BList erased: {%d->%d}\n", i->from->id(), i->to->id());
+ i = blist.erase(i);
+ } else {
+ ++i;
+ }
+ }
+ }
+
+ void BracketListTrace(BracketList& blist) {
+ if (FLAG_trace_turbo_scheduler) {
+ Trace(" BList: ");
+ for (Bracket bracket : blist) {
+ Trace("{%d->%d} ", bracket.from->id(), bracket.to->id());
+ }
+ Trace("\n");
+ }
+ }
+
+ void Trace(const char* msg, ...) {
+ if (FLAG_trace_turbo_scheduler) {
+ va_list arguments;
+ va_start(arguments, msg);
+ base::OS::VPrint(msg, arguments);
+ va_end(arguments);
+ }
+ }
+
+ Zone* zone_;
+ Graph* graph_;
+ int dfs_number_; // Generates new DFS pre-order numbers on demand.
+ int class_number_; // Generates new equivalence class numbers on demand.
+ Data node_data_; // Per-node data stored as a side-table.
+};
+
+} // namespace compiler
+} // namespace internal
+} // namespace v8
+
+#endif // V8_COMPILER_CONTROL_EQUIVALENCE_H_
#include "src/compiler/scheduler.h"
#include "src/bit-vector.h"
+#include "src/compiler/control-equivalence.h"
#include "src/compiler/graph.h"
#include "src/compiler/graph-inl.h"
#include "src/compiler/node.h"
Scheduler::SchedulerData Scheduler::DefaultSchedulerData() {
- SchedulerData def = {schedule_->start(), 0, false, false, kUnknown};
+ SchedulerData def = {schedule_->start(), 0, false, kUnknown};
return def;
}
data->placement_ = (p == kFixed ? kFixed : kCoupled);
break;
}
-#define DEFINE_FLOATING_CONTROL_CASE(V) case IrOpcode::k##V:
- CONTROL_OP_LIST(DEFINE_FLOATING_CONTROL_CASE)
-#undef DEFINE_FLOATING_CONTROL_CASE
+#define DEFINE_CONTROL_CASE(V) case IrOpcode::k##V:
+ CONTROL_OP_LIST(DEFINE_CONTROL_CASE)
+#undef DEFINE_CONTROL_CASE
{
// Control nodes that were not control-reachable from end may float.
data->placement_ = kSchedulable;
- if (!data->is_connected_control_) {
- data->is_floating_control_ = true;
- Trace("Floating control found: #%d:%s\n", node->id(),
- node->op()->mnemonic());
- }
break;
}
default:
schedule_->AddNode(block, node);
break;
}
-#define DEFINE_FLOATING_CONTROL_CASE(V) case IrOpcode::k##V:
- CONTROL_OP_LIST(DEFINE_FLOATING_CONTROL_CASE)
-#undef DEFINE_FLOATING_CONTROL_CASE
+#define DEFINE_CONTROL_CASE(V) case IrOpcode::k##V:
+ CONTROL_OP_LIST(DEFINE_CONTROL_CASE)
+#undef DEFINE_CONTROL_CASE
{
// Control nodes force coupled uses to be placed.
Node::Uses uses = node->uses();
schedule_(scheduler->schedule_),
queue_(zone),
control_(zone),
- component_head_(NULL),
+ component_entry_(NULL),
component_start_(NULL),
component_end_(NULL) {}
}
// Run the control flow graph construction for a minimal control-connected
- // component ending in {node} and merge that component into an existing
+ // component ending in {exit} and merge that component into an existing
// control flow graph at the bottom of {block}.
- void Run(BasicBlock* block, Node* node) {
+ void Run(BasicBlock* block, Node* exit) {
ResetDataStructures();
- Queue(node);
+ Queue(exit);
+ component_entry_ = NULL;
component_start_ = block;
- component_end_ = schedule_->block(node);
+ component_end_ = schedule_->block(exit);
+ scheduler_->equivalence_->Run(exit);
while (!queue_.empty()) { // Breadth-first backwards traversal.
Node* node = queue_.front();
queue_.pop();
- bool is_dom = true;
+
+ // Use control dependence equivalence to find a canonical single-entry
+ // single-exit region that makes up a minimal component to be scheduled.
+ if (IsSingleEntrySingleExitRegion(node, exit)) {
+ Trace("Found SESE at #%d:%s\n", node->id(), node->op()->mnemonic());
+ DCHECK_EQ(NULL, component_entry_);
+ component_entry_ = node;
+ continue;
+ }
+
int max = NodeProperties::PastControlIndex(node);
for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
- is_dom = is_dom &&
- !scheduler_->GetData(node->InputAt(i))->is_floating_control_;
Queue(node->InputAt(i));
}
- // TODO(mstarzinger): This is a hacky way to find component dominator.
- if (is_dom) component_head_ = node;
}
- DCHECK_NOT_NULL(component_head_);
+ DCHECK_NE(NULL, component_entry_);
for (NodeVector::iterator i = control_.begin(); i != control_.end(); ++i) {
- scheduler_->GetData(*i)->is_floating_control_ = false;
ConnectBlocks(*i); // Connect block to its predecessor/successors.
}
}
}
}
-
void BuildBlocks(Node* node) {
switch (node->opcode()) {
case IrOpcode::kEnd:
IrOpcode::Value false_opcode) {
buffer[0] = NULL;
buffer[1] = NULL;
- for (UseIter i = node->uses().begin(); i != node->uses().end(); ++i) {
- if ((*i)->opcode() == true_opcode) {
+ for (Node* use : node->uses()) {
+ if (use->opcode() == true_opcode) {
DCHECK_EQ(NULL, buffer[0]);
- buffer[0] = *i;
+ buffer[0] = use;
}
- if ((*i)->opcode() == false_opcode) {
+ if (use->opcode() == false_opcode) {
DCHECK_EQ(NULL, buffer[1]);
- buffer[1] = *i;
+ buffer[1] = use;
}
}
DCHECK_NE(NULL, buffer[0]);
break;
}
- if (branch == component_head_) {
+ if (branch == component_entry_) {
TraceConnect(branch, component_start_, successor_blocks[0]);
TraceConnect(branch, component_start_, successor_blocks[1]);
schedule_->InsertBranch(component_start_, component_end_, branch,
DCHECK(block != NULL);
// For all of the merge's control inputs, add a goto at the end to the
// merge's basic block.
- for (Node* const j : merge->inputs()) {
- BasicBlock* predecessor_block = schedule_->block(j);
+ for (Node* const input : merge->inputs()) {
+ BasicBlock* predecessor_block = schedule_->block(input);
TraceConnect(merge, predecessor_block, block);
schedule_->AddGoto(predecessor_block, block);
}
node == scheduler_->graph_->end()->InputAt(0));
}
+ bool IsSingleEntrySingleExitRegion(Node* entry, Node* exit) const {
+ size_t entry_class = scheduler_->equivalence_->ClassOf(entry);
+ size_t exit_class = scheduler_->equivalence_->ClassOf(exit);
+ return entry != exit && entry_class == exit_class;
+ }
+
void ResetDataStructures() {
control_.clear();
DCHECK(queue_.empty());
Schedule* schedule_;
ZoneQueue<Node*> queue_;
NodeVector control_;
- Node* component_head_;
+ Node* component_entry_;
BasicBlock* component_start_;
BasicBlock* component_end_;
};
void Scheduler::BuildCFG() {
Trace("--- CREATING CFG -------------------------------------------\n");
+ // Instantiate a new control equivalence algorithm for the graph.
+ equivalence_ = new (zone_) ControlEquivalence(zone_, graph_);
+
// Build a control-flow graph for the main control-connected component that
// is being spanned by the graph's start and end nodes.
control_flow_builder_ = new (zone_) CFGBuilder(zone_, this);
}
void ScheduleFloatingControl(BasicBlock* block, Node* node) {
- DCHECK(scheduler_->GetData(node)->is_floating_control_);
scheduler_->FuseFloatingControl(block, node);
}
namespace compiler {
class CFGBuilder;
+class ControlEquivalence;
class SpecialRPONumberer;
// Computes a schedule from a graph, placing nodes into basic blocks and
BasicBlock* minimum_block_; // Minimum legal RPO placement.
int unscheduled_count_; // Number of unscheduled uses of this node.
bool is_connected_control_; // {true} if control-connected to the end node.
- bool is_floating_control_; // {true} if control, but not control-connected
- // to the end node.
Placement placement_; // Whether the node is fixed, schedulable,
// coupled to another node, or not yet known.
};
ZoneVector<SchedulerData> node_data_; // Per-node data for all nodes.
CFGBuilder* control_flow_builder_; // Builds basic blocks for controls.
SpecialRPONumberer* special_rpo_; // Special RPO numbering of blocks.
+ ControlEquivalence* equivalence_; // Control dependence equivalence.
Scheduler(Zone* zone, Graph* graph, Schedule* schedule);
}
void destroy(pointer p) { p->~T(); }
- bool operator==(zone_allocator const& other) {
+ bool operator==(zone_allocator const& other) const {
return zone_ == other.zone_;
}
- bool operator!=(zone_allocator const& other) {
+ bool operator!=(zone_allocator const& other) const {
return zone_ != other.zone_;
}
#define V8_ZONE_CONTAINERS_H_
#include <deque>
+#include <list>
#include <queue>
#include <stack>
#include <vector>
// A wrapper subclass for std::vector to make it easy to construct one
// that uses a zone allocator.
template <typename T>
-class ZoneVector : public std::vector<T, zone_allocator<T> > {
+class ZoneVector : public std::vector<T, zone_allocator<T>> {
public:
// Constructs an empty vector.
explicit ZoneVector(Zone* zone)
- : std::vector<T, zone_allocator<T> >(zone_allocator<T>(zone)) {}
+ : std::vector<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
// Constructs a new vector and fills it with {size} elements, each
// constructed via the default constructor.
ZoneVector(int size, Zone* zone)
- : std::vector<T, zone_allocator<T> >(size, T(), zone_allocator<T>(zone)) {
- }
+ : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
// Constructs a new vector and fills it with {size} elements, each
// having the value {def}.
ZoneVector(int size, T def, Zone* zone)
- : std::vector<T, zone_allocator<T> >(size, def, zone_allocator<T>(zone)) {
- }
+ : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
};
// A wrapper subclass std::deque to make it easy to construct one
// that uses a zone allocator.
template <typename T>
-class ZoneDeque : public std::deque<T, zone_allocator<T> > {
+class ZoneDeque : public std::deque<T, zone_allocator<T>> {
public:
// Constructs an empty deque.
explicit ZoneDeque(Zone* zone)
- : std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone)) {}
+ : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
+};
+
+
+// A wrapper subclass std::list to make it easy to construct one
+// that uses a zone allocator.
+// TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
+// own home-grown ZoneList that actually is a ZoneVector.
+template <typename T>
+class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
+ public:
+ // Constructs an empty list.
+ explicit ZoneLinkedList(Zone* zone)
+ : std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
};
}
+TEST(NestedFloatingDiamondWithChain) {
+ HandleAndZoneScope scope;
+ Graph graph(scope.main_zone());
+ CommonOperatorBuilder common(scope.main_zone());
+
+ Node* start = graph.NewNode(common.Start(2));
+ graph.SetStart(start);
+
+ Node* p0 = graph.NewNode(common.Parameter(0), start);
+ Node* p1 = graph.NewNode(common.Parameter(1), start);
+ Node* c = graph.NewNode(common.Int32Constant(7));
+
+ Node* brA1 = graph.NewNode(common.Branch(), p0, graph.start());
+ Node* tA1 = graph.NewNode(common.IfTrue(), brA1);
+ Node* fA1 = graph.NewNode(common.IfFalse(), brA1);
+ Node* mA1 = graph.NewNode(common.Merge(2), tA1, fA1);
+ Node* phiA1 = graph.NewNode(common.Phi(kMachAnyTagged, 2), p0, p1, mA1);
+
+ Node* brB1 = graph.NewNode(common.Branch(), p1, graph.start());
+ Node* tB1 = graph.NewNode(common.IfTrue(), brB1);
+ Node* fB1 = graph.NewNode(common.IfFalse(), brB1);
+ Node* mB1 = graph.NewNode(common.Merge(2), tB1, fB1);
+ Node* phiB1 = graph.NewNode(common.Phi(kMachAnyTagged, 2), p0, p1, mB1);
+
+ Node* brA2 = graph.NewNode(common.Branch(), phiB1, mA1);
+ Node* tA2 = graph.NewNode(common.IfTrue(), brA2);
+ Node* fA2 = graph.NewNode(common.IfFalse(), brA2);
+ Node* mA2 = graph.NewNode(common.Merge(2), tA2, fA2);
+ Node* phiA2 = graph.NewNode(common.Phi(kMachAnyTagged, 2), phiB1, c, mA2);
+
+ Node* brB2 = graph.NewNode(common.Branch(), phiA1, mB1);
+ Node* tB2 = graph.NewNode(common.IfTrue(), brB2);
+ Node* fB2 = graph.NewNode(common.IfFalse(), brB2);
+ Node* mB2 = graph.NewNode(common.Merge(2), tB2, fB2);
+ Node* phiB2 = graph.NewNode(common.Phi(kMachAnyTagged, 2), phiA1, c, mB2);
+
+ Node* add = graph.NewNode(&kIntAdd, phiA2, phiB2);
+ Node* ret = graph.NewNode(common.Return(), add, start, start);
+ Node* end = graph.NewNode(common.End(), ret, start);
+
+ graph.SetEnd(end);
+
+ ComputeAndVerifySchedule(35, &graph);
+}
+
+
TEST(NestedFloatingDiamondWithLoop) {
HandleAndZoneScope scope;
Graph graph(scope.main_zone());
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/compiler/control-equivalence.h"
+#include "src/compiler/graph-visualizer.h"
+#include "src/compiler/node-properties-inl.h"
+#include "src/zone-containers.h"
+#include "test/unittests/compiler/graph-unittest.h"
+
+namespace v8 {
+namespace internal {
+namespace compiler {
+
+#define ASSERT_EQUIVALENCE(...) \
+ do { \
+ Node* __n[] = {__VA_ARGS__}; \
+ ASSERT_TRUE(IsEquivalenceClass(arraysize(__n), __n)); \
+ } while (false);
+
+class ControlEquivalenceTest : public GraphTest {
+ public:
+ ControlEquivalenceTest() : all_nodes_(zone()), classes_(zone()) {
+ Store(graph()->start());
+ }
+
+ protected:
+ void ComputeEquivalence(Node* node) {
+ graph()->SetEnd(graph()->NewNode(common()->End(), node));
+ if (FLAG_trace_turbo) {
+ OFStream os(stdout);
+ os << AsDOT(*graph());
+ }
+ ControlEquivalence equivalence(zone(), graph());
+ equivalence.Run(node);
+ classes_.resize(graph()->NodeCount());
+ for (Node* node : all_nodes_) {
+ classes_[node->id()] = equivalence.ClassOf(node);
+ }
+ }
+
+ bool IsEquivalenceClass(size_t length, Node** nodes) {
+ BitVector in_class(graph()->NodeCount(), zone());
+ size_t expected_class = classes_[nodes[0]->id()];
+ for (size_t i = 0; i < length; ++i) {
+ in_class.Add(nodes[i]->id());
+ }
+ for (Node* node : all_nodes_) {
+ if (in_class.Contains(node->id())) {
+ if (classes_[node->id()] != expected_class) return false;
+ } else {
+ if (classes_[node->id()] == expected_class) return false;
+ }
+ }
+ return true;
+ }
+
+ Node* Value() { return NumberConstant(0.0); }
+
+ Node* Branch(Node* control) {
+ return Store(graph()->NewNode(common()->Branch(), Value(), control));
+ }
+
+ Node* IfTrue(Node* control) {
+ return Store(graph()->NewNode(common()->IfTrue(), control));
+ }
+
+ Node* IfFalse(Node* control) {
+ return Store(graph()->NewNode(common()->IfFalse(), control));
+ }
+
+ Node* Merge2(Node* control1, Node* control2) {
+ return Store(graph()->NewNode(common()->Merge(2), control1, control2));
+ }
+
+ Node* Loop2(Node* control) {
+ return Store(graph()->NewNode(common()->Loop(2), control, control));
+ }
+
+ Node* End(Node* control) {
+ return Store(graph()->NewNode(common()->End(), control));
+ }
+
+ private:
+ Node* Store(Node* node) {
+ all_nodes_.push_back(node);
+ return node;
+ }
+
+ ZoneVector<Node*> all_nodes_;
+ ZoneVector<size_t> classes_;
+};
+
+
+// -----------------------------------------------------------------------------
+// Test cases.
+
+
+TEST_F(ControlEquivalenceTest, Empty1) {
+ Node* start = graph()->start();
+ ComputeEquivalence(start);
+
+ ASSERT_EQUIVALENCE(start);
+}
+
+
+TEST_F(ControlEquivalenceTest, Empty2) {
+ Node* start = graph()->start();
+ Node* end = End(start);
+ ComputeEquivalence(end);
+
+ ASSERT_EQUIVALENCE(start, end);
+}
+
+
+TEST_F(ControlEquivalenceTest, Diamond1) {
+ Node* start = graph()->start();
+ Node* b = Branch(start);
+ Node* t = IfTrue(b);
+ Node* f = IfFalse(b);
+ Node* m = Merge2(t, f);
+ ComputeEquivalence(m);
+
+ ASSERT_EQUIVALENCE(b, m, start);
+ ASSERT_EQUIVALENCE(f);
+ ASSERT_EQUIVALENCE(t);
+}
+
+
+TEST_F(ControlEquivalenceTest, Diamond2) {
+ Node* start = graph()->start();
+ Node* b1 = Branch(start);
+ Node* t1 = IfTrue(b1);
+ Node* f1 = IfFalse(b1);
+ Node* b2 = Branch(f1);
+ Node* t2 = IfTrue(b2);
+ Node* f2 = IfFalse(b2);
+ Node* m2 = Merge2(t2, f2);
+ Node* m1 = Merge2(t1, m2);
+ ComputeEquivalence(m1);
+
+ ASSERT_EQUIVALENCE(b1, m1, start);
+ ASSERT_EQUIVALENCE(t1);
+ ASSERT_EQUIVALENCE(f1, b2, m2);
+ ASSERT_EQUIVALENCE(t2);
+ ASSERT_EQUIVALENCE(f2);
+}
+
+
+TEST_F(ControlEquivalenceTest, Diamond3) {
+ Node* start = graph()->start();
+ Node* b1 = Branch(start);
+ Node* t1 = IfTrue(b1);
+ Node* f1 = IfFalse(b1);
+ Node* m1 = Merge2(t1, f1);
+ Node* b2 = Branch(m1);
+ Node* t2 = IfTrue(b2);
+ Node* f2 = IfFalse(b2);
+ Node* m2 = Merge2(t2, f2);
+ ComputeEquivalence(m2);
+
+ ASSERT_EQUIVALENCE(b1, m1, b2, m2, start);
+ ASSERT_EQUIVALENCE(t1);
+ ASSERT_EQUIVALENCE(f1);
+ ASSERT_EQUIVALENCE(t2);
+ ASSERT_EQUIVALENCE(f2);
+}
+
+
+TEST_F(ControlEquivalenceTest, Switch1) {
+ Node* start = graph()->start();
+ Node* b1 = Branch(start);
+ Node* t1 = IfTrue(b1);
+ Node* f1 = IfFalse(b1);
+ Node* b2 = Branch(f1);
+ Node* t2 = IfTrue(b2);
+ Node* f2 = IfFalse(b2);
+ Node* b3 = Branch(f2);
+ Node* t3 = IfTrue(b3);
+ Node* f3 = IfFalse(b3);
+ Node* m1 = Merge2(t1, t2);
+ Node* m2 = Merge2(m1, t3);
+ Node* m3 = Merge2(m2, f3);
+ ComputeEquivalence(m3);
+
+ ASSERT_EQUIVALENCE(b1, m3, start);
+ ASSERT_EQUIVALENCE(t1);
+ ASSERT_EQUIVALENCE(f1, b2);
+ ASSERT_EQUIVALENCE(t2);
+ ASSERT_EQUIVALENCE(f2, b3);
+ ASSERT_EQUIVALENCE(t3);
+ ASSERT_EQUIVALENCE(f3);
+ ASSERT_EQUIVALENCE(m1);
+ ASSERT_EQUIVALENCE(m2);
+}
+
+
+TEST_F(ControlEquivalenceTest, Loop1) {
+ Node* start = graph()->start();
+ Node* l = Loop2(start);
+ l->ReplaceInput(1, l);
+ ComputeEquivalence(l);
+
+ ASSERT_EQUIVALENCE(start);
+ ASSERT_EQUIVALENCE(l);
+}
+
+
+TEST_F(ControlEquivalenceTest, Loop2) {
+ Node* start = graph()->start();
+ Node* l = Loop2(start);
+ Node* b = Branch(l);
+ Node* t = IfTrue(b);
+ Node* f = IfFalse(b);
+ l->ReplaceInput(1, t);
+ ComputeEquivalence(f);
+
+ ASSERT_EQUIVALENCE(f, start);
+ ASSERT_EQUIVALENCE(t);
+ ASSERT_EQUIVALENCE(l, b);
+}
+
+
+TEST_F(ControlEquivalenceTest, Irreducible) {
+ Node* start = graph()->start();
+ Node* b1 = Branch(start);
+ Node* t1 = IfTrue(b1);
+ Node* f1 = IfFalse(b1);
+ Node* lp = Loop2(f1);
+ Node* m1 = Merge2(t1, lp);
+ Node* b2 = Branch(m1);
+ Node* t2 = IfTrue(b2);
+ Node* f2 = IfFalse(b2);
+ Node* m2 = Merge2(t2, f2);
+ Node* b3 = Branch(m2);
+ Node* t3 = IfTrue(b3);
+ Node* f3 = IfFalse(b3);
+ lp->ReplaceInput(1, f3);
+ ComputeEquivalence(t3);
+
+ ASSERT_EQUIVALENCE(b1, t3, start);
+ ASSERT_EQUIVALENCE(t1);
+ ASSERT_EQUIVALENCE(f1);
+ ASSERT_EQUIVALENCE(m1, b2, m2, b3);
+ ASSERT_EQUIVALENCE(t2);
+ ASSERT_EQUIVALENCE(f2);
+ ASSERT_EQUIVALENCE(f3);
+ ASSERT_EQUIVALENCE(lp);
+}
+
+
+} // namespace compiler
+} // namespace internal
+} // namespace v8
'compiler/change-lowering-unittest.cc',
'compiler/common-operator-unittest.cc',
'compiler/compiler-test-utils.h',
+ 'compiler/control-equivalence-unittest.cc',
'compiler/diamond-unittest.cc',
'compiler/graph-reducer-unittest.cc',
'compiler/graph-unittest.cc',
'../../src/compiler/common-operator.h',
'../../src/compiler/control-builders.cc',
'../../src/compiler/control-builders.h',
+ '../../src/compiler/control-equivalence.h',
'../../src/compiler/control-reducer.cc',
'../../src/compiler/control-reducer.h',
'../../src/compiler/diamond.h',