Home | History | Annotate | Download | only in compiler
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_COMPILER_CONTROL_EQUIVALENCE_H_
      6 #define V8_COMPILER_CONTROL_EQUIVALENCE_H_
      7 
      8 #include "src/base/compiler-specific.h"
      9 #include "src/compiler/graph.h"
     10 #include "src/compiler/node.h"
     11 #include "src/globals.h"
     12 #include "src/zone/zone-containers.h"
     13 
     14 namespace v8 {
     15 namespace internal {
     16 namespace compiler {
     17 
     18 // Determines control dependence equivalence classes for control nodes. Any two
     19 // nodes having the same set of control dependences land in one class. These
     20 // classes can in turn be used to:
     21 //  - Build a program structure tree (PST) for controls in the graph.
     22 //  - Determine single-entry single-exit (SESE) regions within the graph.
     23 //
     24 // Note that this implementation actually uses cycle equivalence to establish
     25 // class numbers. Any two nodes are cycle equivalent if they occur in the same
     26 // set of cycles. It can be shown that control dependence equivalence reduces
     27 // to undirected cycle equivalence for strongly connected control flow graphs.
     28 //
     29 // The algorithm is based on the paper, "The program structure tree: computing
     30 // control regions in linear time" by Johnson, Pearson & Pingali (PLDI94) which
     31 // also contains proofs for the aforementioned equivalence. References to line
     32 // numbers in the algorithm from figure 4 have been added [line:x].
     33 class V8_EXPORT_PRIVATE ControlEquivalence final
     34     : public NON_EXPORTED_BASE(ZoneObject) {
     35  public:
     36   ControlEquivalence(Zone* zone, Graph* graph)
     37       : zone_(zone),
     38         graph_(graph),
     39         dfs_number_(0),
     40         class_number_(1),
     41         node_data_(graph->NodeCount(), EmptyData(), zone) {}
     42 
     43   // Run the main algorithm starting from the {exit} control node. This causes
     44   // the following iterations over control edges of the graph:
     45   //  1) A breadth-first backwards traversal to determine the set of nodes that
     46   //     participate in the next step. Takes O(E) time and O(N) space.
     47   //  2) An undirected depth-first backwards traversal that determines class
     48   //     numbers for all participating nodes. Takes O(E) time and O(N) space.
     49   void Run(Node* exit);
     50 
     51   // Retrieves a previously computed class number.
     52   size_t ClassOf(Node* node) {
     53     DCHECK_NE(kInvalidClass, GetClass(node));
     54     return GetClass(node);
     55   }
     56 
     57  private:
     58   static const size_t kInvalidClass = static_cast<size_t>(-1);
     59   typedef enum { kInputDirection, kUseDirection } DFSDirection;
     60 
     61   struct Bracket {
     62     DFSDirection direction;  // Direction in which this bracket was added.
     63     size_t recent_class;     // Cached class when bracket was topmost.
     64     size_t recent_size;      // Cached set-size when bracket was topmost.
     65     Node* from;              // Node that this bracket originates from.
     66     Node* to;                // Node that this bracket points to.
     67   };
     68 
     69   // The set of brackets for each node during the DFS walk.
     70   typedef ZoneLinkedList<Bracket> BracketList;
     71 
     72   struct DFSStackEntry {
     73     DFSDirection direction;            // Direction currently used in DFS walk.
     74     Node::InputEdges::iterator input;  // Iterator used for "input" direction.
     75     Node::UseEdges::iterator use;      // Iterator used for "use" direction.
     76     Node* parent_node;                 // Parent node of entry during DFS walk.
     77     Node* node;                        // Node that this stack entry belongs to.
     78   };
     79 
     80   // The stack is used during the undirected DFS walk.
     81   typedef ZoneStack<DFSStackEntry> DFSStack;
     82 
     83   struct NodeData {
     84     size_t class_number;  // Equivalence class number assigned to node.
     85     size_t dfs_number;    // Pre-order DFS number assigned to node.
     86     bool visited;         // Indicates node has already been visited.
     87     bool on_stack;        // Indicates node is on DFS stack during walk.
     88     bool participates;    // Indicates node participates in DFS walk.
     89     BracketList blist;    // List of brackets per node.
     90   };
     91 
     92   // The per-node data computed during the DFS walk.
     93   typedef ZoneVector<NodeData> Data;
     94 
     95   // Called at pre-visit during DFS walk.
     96   void VisitPre(Node* node);
     97 
     98   // Called at mid-visit during DFS walk.
     99   void VisitMid(Node* node, DFSDirection direction);
    100 
    101   // Called at post-visit during DFS walk.
    102   void VisitPost(Node* node, Node* parent_node, DFSDirection direction);
    103 
    104   // Called when hitting a back edge in the DFS walk.
    105   void VisitBackedge(Node* from, Node* to, DFSDirection direction);
    106 
    107   // Performs and undirected DFS walk of the graph. Conceptually all nodes are
    108   // expanded, splitting "input" and "use" out into separate nodes. During the
    109   // traversal, edges towards the representative nodes are preferred.
    110   //
    111   //   \ /        - Pre-visit: When N1 is visited in direction D the preferred
    112   //    x   N1      edge towards N is taken next, calling VisitPre(N).
    113   //    |         - Mid-visit: After all edges out of N2 in direction D have
    114   //    |   N       been visited, we switch the direction and start considering
    115   //    |           edges out of N1 now, and we call VisitMid(N).
    116   //    x   N2    - Post-visit: After all edges out of N1 in direction opposite
    117   //   / \          to D have been visited, we pop N and call VisitPost(N).
    118   //
    119   // This will yield a true spanning tree (without cross or forward edges) and
    120   // also discover proper back edges in both directions.
    121   void RunUndirectedDFS(Node* exit);
    122 
    123   void DetermineParticipationEnqueue(ZoneQueue<Node*>& queue, Node* node);
    124   void DetermineParticipation(Node* exit);
    125 
    126  private:
    127   NodeData* GetData(Node* node) {
    128     size_t const index = node->id();
    129     if (index >= node_data_.size()) node_data_.resize(index + 1, EmptyData());
    130     return &node_data_[index];
    131   }
    132   int NewClassNumber() { return class_number_++; }
    133   int NewDFSNumber() { return dfs_number_++; }
    134 
    135   // Template used to initialize per-node data.
    136   NodeData EmptyData() {
    137     return {kInvalidClass, 0, false, false, false, BracketList(zone_)};
    138   }
    139 
    140   // Accessors for the DFS number stored within the per-node data.
    141   size_t GetNumber(Node* node) { return GetData(node)->dfs_number; }
    142   void SetNumber(Node* node, size_t number) {
    143     GetData(node)->dfs_number = number;
    144   }
    145 
    146   // Accessors for the equivalence class stored within the per-node data.
    147   size_t GetClass(Node* node) { return GetData(node)->class_number; }
    148   void SetClass(Node* node, size_t number) {
    149     GetData(node)->class_number = number;
    150   }
    151 
    152   // Accessors for the bracket list stored within the per-node data.
    153   BracketList& GetBracketList(Node* node) { return GetData(node)->blist; }
    154   void SetBracketList(Node* node, BracketList& list) {
    155     GetData(node)->blist = list;
    156   }
    157 
    158   // Mutates the DFS stack by pushing an entry.
    159   void DFSPush(DFSStack& stack, Node* node, Node* from, DFSDirection dir);
    160 
    161   // Mutates the DFS stack by popping an entry.
    162   void DFSPop(DFSStack& stack, Node* node);
    163 
    164   void BracketListDelete(BracketList& blist, Node* to, DFSDirection direction);
    165   void BracketListTRACE(BracketList& blist);
    166 
    167   Zone* const zone_;
    168   Graph* const graph_;
    169   int dfs_number_;    // Generates new DFS pre-order numbers on demand.
    170   int class_number_;  // Generates new equivalence class numbers on demand.
    171   Data node_data_;    // Per-node data stored as a side-table.
    172 };
    173 
    174 }  // namespace compiler
    175 }  // namespace internal
    176 }  // namespace v8
    177 
    178 #endif  // V8_COMPILER_CONTROL_EQUIVALENCE_H_
    179