OLD | NEW |
(Empty) | |
| 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_LOOP_ANALYSIS_H_ |
| 6 #define V8_COMPILER_LOOP_ANALYSIS_H_ |
| 7 |
| 8 #include "src/base/iterator.h" |
| 9 #include "src/compiler/graph.h" |
| 10 #include "src/compiler/node.h" |
| 11 #include "src/zone-containers.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 namespace compiler { |
| 16 |
| 17 class LoopFinderImpl; |
| 18 |
| 19 typedef base::iterator_range<Node**> NodeRange; |
| 20 |
| 21 // Represents a tree of loops in a graph. |
| 22 class LoopTree : public ZoneObject { |
| 23 public: |
| 24 LoopTree(size_t num_nodes, Zone* zone) |
| 25 : zone_(zone), |
| 26 outer_loops_(zone), |
| 27 all_loops_(zone), |
| 28 node_to_loop_num_(static_cast<int>(num_nodes), 0, zone), |
| 29 loop_nodes_(zone) {} |
| 30 |
| 31 // Represents a loop in the tree of loops, including the header nodes, |
| 32 // the body, and any nested loops. |
| 33 class Loop { |
| 34 public: |
| 35 Loop* parent() const { return parent_; } |
| 36 const ZoneVector<Loop*>& children() const { return children_; } |
| 37 size_t HeaderSize() const { return body_start_ - header_start_; } |
| 38 size_t BodySize() const { return body_end_ - body_start_; } |
| 39 size_t TotalSize() const { return body_end_ - header_start_; } |
| 40 |
| 41 private: |
| 42 friend class LoopTree; |
| 43 friend class LoopFinderImpl; |
| 44 |
| 45 explicit Loop(Zone* zone) |
| 46 : parent_(nullptr), |
| 47 depth_(0), |
| 48 children_(zone), |
| 49 header_start_(-1), |
| 50 body_start_(-1), |
| 51 body_end_(-1) {} |
| 52 Loop* parent_; |
| 53 int depth_; |
| 54 ZoneVector<Loop*> children_; |
| 55 int header_start_; |
| 56 int body_start_; |
| 57 int body_end_; |
| 58 }; |
| 59 |
| 60 // Return the innermost nested loop, if any, that contains {node}. |
| 61 Loop* ContainingLoop(Node* node) { |
| 62 if (node->id() >= static_cast<int>(node_to_loop_num_.size())) |
| 63 return nullptr; |
| 64 uint8_t num = node_to_loop_num_[node->id()]; |
| 65 return num > 0 ? &all_loops_[num - 1] : nullptr; |
| 66 } |
| 67 |
| 68 // Check if the {loop} contains the {node}, either directly or by containing |
| 69 // a nested loop that contains {node}. |
| 70 bool Contains(Loop* loop, Node* node) { |
| 71 for (Loop* c = ContainingLoop(node); c != nullptr; c = c->parent_) { |
| 72 if (c == loop) return true; |
| 73 } |
| 74 return false; |
| 75 } |
| 76 |
| 77 // Return the list of outer loops. |
| 78 const ZoneVector<Loop*>& outer_loops() const { return outer_loops_; } |
| 79 |
| 80 // Return the unique loop number for a given loop. Loop numbers start at {1}. |
| 81 int LoopNum(Loop* loop) const { |
| 82 return 1 + static_cast<int>(loop - &all_loops_[0]); |
| 83 } |
| 84 |
| 85 // Return a range which can iterate over the header nodes of {loop}. |
| 86 NodeRange HeaderNodes(Loop* loop) { |
| 87 return NodeRange(&loop_nodes_[0] + loop->header_start_, |
| 88 &loop_nodes_[0] + loop->body_start_); |
| 89 } |
| 90 |
| 91 // Return a range which can iterate over the body nodes of {loop}. |
| 92 NodeRange BodyNodes(Loop* loop) { |
| 93 return NodeRange(&loop_nodes_[0] + loop->body_start_, |
| 94 &loop_nodes_[0] + loop->body_end_); |
| 95 } |
| 96 |
| 97 private: |
| 98 friend class LoopFinderImpl; |
| 99 |
| 100 Loop* NewLoop() { |
| 101 all_loops_.push_back(Loop(zone_)); |
| 102 Loop* result = &all_loops_.back(); |
| 103 return result; |
| 104 } |
| 105 |
| 106 void SetParent(Loop* parent, Loop* child) { |
| 107 if (parent != nullptr) { |
| 108 parent->children_.push_back(child); |
| 109 child->parent_ = parent; |
| 110 child->depth_ = parent->depth_ + 1; |
| 111 } else { |
| 112 outer_loops_.push_back(child); |
| 113 } |
| 114 } |
| 115 |
| 116 Zone* zone_; |
| 117 ZoneVector<Loop*> outer_loops_; |
| 118 ZoneVector<Loop> all_loops_; |
| 119 // TODO(titzer): lift loop count restriction. |
| 120 ZoneVector<uint8_t> node_to_loop_num_; |
| 121 ZoneVector<Node*> loop_nodes_; |
| 122 }; |
| 123 |
| 124 |
| 125 class LoopFinder { |
| 126 public: |
| 127 // Build a loop tree for the entire graph. |
| 128 static LoopTree* BuildLoopTree(Graph* graph, Zone* temp_zone); |
| 129 }; |
| 130 |
| 131 } // namespace compiler |
| 132 } // namespace internal |
| 133 } // namespace v8 |
| 134 |
| 135 #endif // V8_COMPILER_LOOP_ANALYSIS_H_ |
OLD | NEW |