buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |
| 18 | #define ART_SRC_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |
| 19 | |
| 20 | #include "compiler_ir.h" |
| 21 | #include "mir_graph.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 25 | /* |
| 26 | * This class supports iterating over lists of basic blocks in various |
| 27 | * interesting orders. Note that for efficiency, the visit orders have been pre-computed. |
| 28 | * The order itself will not change during the iteration. However, for some uses, |
| 29 | * auxiliary data associated with the basic blocks may be changed during the iteration, |
| 30 | * necessitating another pass over the list. |
| 31 | * |
| 32 | * To support this usage, we have is_iterative_. If false, the iteration is a one-shot |
| 33 | * pass through the pre-computed list using Next(). If true, the caller must tell the |
| 34 | * iterator whether a change has been made that necessitates another pass. Use |
| 35 | * Next(had_change) for this. The general idea is that the iterative_ use case means |
| 36 | * that the iterator will keep repeating the full basic block list until a complete pass |
| 37 | * is made through it with no changes. Note that calling Next(true) does not affect |
| 38 | * the iteration order or short-curcuit the current pass - it simply tells the iterator |
| 39 | * that once it has finished walking through the block list it should reset and do another |
| 40 | * full pass through the list. |
| 41 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 42 | class DataflowIterator { |
| 43 | public: |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 44 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 45 | virtual ~DataflowIterator(){} |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 46 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 47 | // Return the next BasicBlock* to visit. |
| 48 | BasicBlock* Next() { |
| 49 | DCHECK(!is_iterative_); |
| 50 | return NextBody(false); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Return the next BasicBlock* to visit, and tell the iterator whether any change |
| 55 | * has occurred that requires another full pass over the block list. |
| 56 | */ |
| 57 | BasicBlock* Next(bool had_change) { |
| 58 | DCHECK(is_iterative_); |
| 59 | return NextBody(had_change); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | DataflowIterator(MIRGraph* mir_graph, bool is_iterative, int start_idx, int end_idx, |
| 64 | bool reverse) |
| 65 | : mir_graph_(mir_graph), |
| 66 | is_iterative_(is_iterative), |
| 67 | start_idx_(start_idx), |
| 68 | end_idx_(end_idx), |
| 69 | reverse_(reverse) {} |
| 70 | |
| 71 | virtual BasicBlock* NextBody(bool had_change); |
| 72 | |
| 73 | MIRGraph* const mir_graph_; |
| 74 | const bool is_iterative_; |
| 75 | const int start_idx_; |
| 76 | const int end_idx_; |
| 77 | const bool reverse_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 78 | GrowableArray<int>* block_id_list_; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 79 | int idx_; |
| 80 | bool changed_; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 81 | |
| 82 | }; // DataflowIterator |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 83 | |
| 84 | class ReachableNodesIterator : public DataflowIterator { |
| 85 | public: |
| 86 | |
| 87 | ReachableNodesIterator(MIRGraph* mir_graph, bool is_iterative) |
| 88 | : DataflowIterator(mir_graph, is_iterative, 0, |
| 89 | mir_graph->GetNumReachableBlocks(), false) { |
| 90 | idx_ = start_idx_; |
| 91 | block_id_list_ = mir_graph->GetDfsOrder(); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class PreOrderDfsIterator : public DataflowIterator { |
| 96 | public: |
| 97 | |
| 98 | PreOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative) |
| 99 | : DataflowIterator(mir_graph, is_iterative, 0, |
| 100 | mir_graph->GetNumReachableBlocks(), false) { |
| 101 | idx_ = start_idx_; |
| 102 | block_id_list_ = mir_graph->GetDfsOrder(); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | class PostOrderDfsIterator : public DataflowIterator { |
| 107 | public: |
| 108 | |
| 109 | PostOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative) |
| 110 | : DataflowIterator(mir_graph, is_iterative, 0, |
| 111 | mir_graph->GetNumReachableBlocks(), false) { |
| 112 | idx_ = start_idx_; |
| 113 | block_id_list_ = mir_graph->GetDfsPostOrder(); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | class ReversePostOrderDfsIterator : public DataflowIterator { |
| 118 | public: |
| 119 | |
| 120 | ReversePostOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative) |
| 121 | : DataflowIterator(mir_graph, is_iterative, |
| 122 | mir_graph->GetNumReachableBlocks() -1, 0, true) { |
| 123 | idx_ = start_idx_; |
| 124 | block_id_list_ = mir_graph->GetDfsPostOrder(); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | class PostOrderDOMIterator : public DataflowIterator { |
| 129 | public: |
| 130 | |
| 131 | PostOrderDOMIterator(MIRGraph* mir_graph, bool is_iterative) |
| 132 | : DataflowIterator(mir_graph, is_iterative, 0, |
| 133 | mir_graph->GetNumReachableBlocks(), false) { |
| 134 | idx_ = start_idx_; |
| 135 | block_id_list_ = mir_graph->GetDomPostOrder(); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | class AllNodesIterator : public DataflowIterator { |
| 140 | public: |
| 141 | |
| 142 | AllNodesIterator(MIRGraph* mir_graph, bool is_iterative) |
| 143 | : DataflowIterator(mir_graph, is_iterative, 0, 0, false) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 144 | all_nodes_iterator_ = |
| 145 | new (mir_graph->GetArena()) GrowableArray<BasicBlock*>::Iterator (mir_graph->GetBlockList()); |
| 146 | } |
| 147 | |
| 148 | virtual void Reset() { |
| 149 | all_nodes_iterator_->Reset(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | virtual BasicBlock* NextBody(bool had_change); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 153 | |
| 154 | private: |
| 155 | GrowableArray<BasicBlock*>::Iterator* all_nodes_iterator_; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 158 | } // namespace art |
| 159 | |
| 160 | #endif // ART_SRC_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |