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