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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |
| 18 | #define ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 19 | |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 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, |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 30 | * necessitating another pass over the list. If this behavior is required, use the |
| 31 | * "Repeating" variant. For the repeating variant, the caller must tell the iterator |
| 32 | * whether a change has been made that necessitates another pass. Note that calling Next(true) |
| 33 | * does not affect the iteration order or short-circuit the current pass - it simply tells |
| 34 | * the iterator that once it has finished walking through the block list it should reset and |
| 35 | * do another full pass through the list. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 36 | */ |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 37 | /** |
| 38 | * @class DataflowIterator |
| 39 | * @brief The main iterator class, all other iterators derive of this one to define an iteration order. |
| 40 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 41 | class DataflowIterator { |
| 42 | public: |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 43 | virtual ~DataflowIterator() {} |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * @brief How many times have we repeated the iterator across the BasicBlocks? |
| 47 | * @return the number of iteration repetitions. |
| 48 | */ |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 49 | int32_t GetRepeatCount() { return repeats_; } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 50 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 51 | /** |
| 52 | * @brief Has the user of the iterator reported a change yet? |
| 53 | * @details Does not mean there was or not a change, it is only whether the user passed a true to the Next function call. |
| 54 | * @return whether the user of the iterator reported a change yet. |
| 55 | */ |
| 56 | int32_t GetChanged() { return changed_; } |
| 57 | |
| 58 | /** |
| 59 | * @brief Get the next BasicBlock depending on iteration order. |
| 60 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 61 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 62 | */ |
| 63 | virtual BasicBlock* Next(bool had_change = false) = 0; |
| 64 | |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 65 | protected: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 66 | /** |
| 67 | * @param mir_graph the MIRGraph we are interested in. |
| 68 | * @param start_idx the first index we want to iterate across. |
| 69 | * @param end_idx the last index we want to iterate (not included). |
| 70 | */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 71 | DataflowIterator(MIRGraph* mir_graph, int32_t start_idx, int32_t end_idx) |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 72 | : mir_graph_(mir_graph), |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 73 | start_idx_(start_idx), |
| 74 | end_idx_(end_idx), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 75 | block_id_list_(nullptr), |
Ian Rogers | e7a5b7d | 2013-04-18 20:09:02 -0700 | [diff] [blame] | 76 | idx_(0), |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 77 | repeats_(0), |
Ian Rogers | e7a5b7d | 2013-04-18 20:09:02 -0700 | [diff] [blame] | 78 | changed_(false) {} |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 79 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 80 | /** |
| 81 | * @brief Get the next BasicBlock iterating forward. |
| 82 | * @return the next BasicBlock iterating forward. |
| 83 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 84 | virtual BasicBlock* ForwardSingleNext() ALWAYS_INLINE; |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief Get the next BasicBlock iterating backward. |
| 88 | * @return the next BasicBlock iterating backward. |
| 89 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 90 | virtual BasicBlock* ReverseSingleNext() ALWAYS_INLINE; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 91 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Get the next BasicBlock iterating forward, restart if a BasicBlock was reported changed during the last iteration. |
| 94 | * @return the next BasicBlock iterating forward, with chance of repeating the iteration. |
| 95 | */ |
| 96 | virtual BasicBlock* ForwardRepeatNext() ALWAYS_INLINE; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 97 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 98 | /** |
| 99 | * @brief Get the next BasicBlock iterating backward, restart if a BasicBlock was reported changed during the last iteration. |
| 100 | * @return the next BasicBlock iterating backward, with chance of repeating the iteration. |
| 101 | */ |
| 102 | virtual BasicBlock* ReverseRepeatNext() ALWAYS_INLINE; |
| 103 | |
| 104 | MIRGraph* const mir_graph_; /**< @brief the MIRGraph */ |
| 105 | const int32_t start_idx_; /**< @brief the start index for the iteration */ |
| 106 | const int32_t end_idx_; /**< @brief the last index for the iteration */ |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 107 | const ArenaVector<BasicBlockId>* block_id_list_; /**< @brief the list of BasicBlocks we want to iterate on */ |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 108 | int32_t idx_; /**< @brief Current index for the iterator */ |
| 109 | int32_t repeats_; /**< @brief Number of repeats over the iteration */ |
| 110 | bool changed_; /**< @brief Has something changed during the current iteration? */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 111 | }; // DataflowIterator |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 112 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 113 | /** |
| 114 | * @class PreOrderDfsIterator |
| 115 | * @brief Used to perform a Pre-order Depth-First-Search Iteration of a MIRGraph. |
| 116 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 117 | class PreOrderDfsIterator : public DataflowIterator { |
| 118 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 119 | /** |
| 120 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 121 | * @param mir_graph The MIRGraph considered. |
| 122 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 123 | explicit PreOrderDfsIterator(MIRGraph* mir_graph) |
| 124 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 125 | // Extra setup for the PreOrderDfsIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 126 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 127 | block_id_list_ = &mir_graph->GetDfsOrder(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 128 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 129 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @brief Get the next BasicBlock depending on iteration order. |
| 132 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 133 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 134 | */ |
| 135 | virtual BasicBlock* Next(bool had_change = false) { |
| 136 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 137 | changed_ |= had_change; |
| 138 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 139 | return ForwardSingleNext(); |
| 140 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 143 | /** |
| 144 | * @class RepeatingPreOrderDfsIterator |
| 145 | * @brief Used to perform a Repeating Pre-order Depth-First-Search Iteration of a MIRGraph. |
| 146 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 147 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 148 | class RepeatingPreOrderDfsIterator : public DataflowIterator { |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 149 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 150 | /** |
| 151 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 152 | * @param mir_graph The MIRGraph considered. |
| 153 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 154 | explicit RepeatingPreOrderDfsIterator(MIRGraph* mir_graph) |
| 155 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 156 | // Extra setup for the RepeatingPreOrderDfsIterator. |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 157 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 158 | block_id_list_ = &mir_graph->GetDfsOrder(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 161 | /** |
| 162 | * @brief Get the next BasicBlock depending on iteration order. |
| 163 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 164 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 165 | */ |
| 166 | virtual BasicBlock* Next(bool had_change = false) { |
| 167 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 168 | changed_ |= had_change; |
| 169 | |
| 170 | return ForwardRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 171 | } |
| 172 | }; |
| 173 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 174 | /** |
| 175 | * @class RepeatingPostOrderDfsIterator |
| 176 | * @brief Used to perform a Repeating Post-order Depth-First-Search Iteration of a MIRGraph. |
| 177 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 178 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 179 | class RepeatingPostOrderDfsIterator : public DataflowIterator { |
| 180 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 181 | /** |
| 182 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 183 | * @param mir_graph The MIRGraph considered. |
| 184 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 185 | explicit RepeatingPostOrderDfsIterator(MIRGraph* mir_graph) |
| 186 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 187 | // Extra setup for the RepeatingPostOrderDfsIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 188 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 189 | block_id_list_ = &mir_graph->GetDfsPostOrder(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 190 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 191 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 192 | /** |
| 193 | * @brief Get the next BasicBlock depending on iteration order. |
| 194 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 195 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 196 | */ |
| 197 | virtual BasicBlock* Next(bool had_change = false) { |
| 198 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 199 | changed_ |= had_change; |
| 200 | |
| 201 | return ForwardRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 202 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 205 | /** |
| 206 | * @class ReversePostOrderDfsIterator |
| 207 | * @brief Used to perform a Reverse Post-order Depth-First-Search Iteration of a MIRGraph. |
| 208 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 209 | class ReversePostOrderDfsIterator : public DataflowIterator { |
| 210 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 211 | /** |
| 212 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 213 | * @param mir_graph The MIRGraph considered. |
| 214 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 215 | explicit ReversePostOrderDfsIterator(MIRGraph* mir_graph) |
| 216 | : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 217 | // Extra setup for the ReversePostOrderDfsIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 218 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 219 | block_id_list_ = &mir_graph->GetDfsPostOrder(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 220 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 221 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 222 | /** |
| 223 | * @brief Get the next BasicBlock depending on iteration order. |
| 224 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 225 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 226 | */ |
| 227 | virtual BasicBlock* Next(bool had_change = false) { |
| 228 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 229 | changed_ |= had_change; |
| 230 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 231 | return ReverseSingleNext(); |
| 232 | } |
| 233 | }; |
| 234 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 235 | /** |
| 236 | * @class ReversePostOrderDfsIterator |
| 237 | * @brief Used to perform a Repeating Reverse Post-order Depth-First-Search Iteration of a MIRGraph. |
| 238 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 239 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 240 | class RepeatingReversePostOrderDfsIterator : public DataflowIterator { |
| 241 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 242 | /** |
| 243 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 244 | * @param mir_graph The MIRGraph considered. |
| 245 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 246 | explicit RepeatingReversePostOrderDfsIterator(MIRGraph* mir_graph) |
| 247 | : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 248 | // Extra setup for the RepeatingReversePostOrderDfsIterator |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 249 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 250 | block_id_list_ = &mir_graph->GetDfsPostOrder(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 253 | /** |
| 254 | * @brief Get the next BasicBlock depending on iteration order. |
| 255 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 256 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 257 | */ |
| 258 | virtual BasicBlock* Next(bool had_change = false) { |
| 259 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 260 | changed_ |= had_change; |
| 261 | |
| 262 | return ReverseRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 263 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 266 | /** |
| 267 | * @class PostOrderDOMIterator |
| 268 | * @brief Used to perform a Post-order Domination Iteration of a MIRGraph. |
| 269 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 270 | class PostOrderDOMIterator : public DataflowIterator { |
| 271 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 272 | /** |
| 273 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 274 | * @param mir_graph The MIRGraph considered. |
| 275 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 276 | explicit PostOrderDOMIterator(MIRGraph* mir_graph) |
| 277 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 278 | // Extra setup for thePostOrderDOMIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 279 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 280 | block_id_list_ = &mir_graph->GetDomPostOrder(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 281 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 282 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 283 | /** |
| 284 | * @brief Get the next BasicBlock depending on iteration order. |
| 285 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 286 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 287 | */ |
| 288 | virtual BasicBlock* Next(bool had_change = false) { |
| 289 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 290 | changed_ |= had_change; |
| 291 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 292 | return ForwardSingleNext(); |
| 293 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 296 | /** |
| 297 | * @class AllNodesIterator |
| 298 | * @brief Used to perform an iteration on all the BasicBlocks a MIRGraph. |
| 299 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 300 | class AllNodesIterator : public DataflowIterator { |
| 301 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 302 | /** |
| 303 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 304 | * @param mir_graph The MIRGraph considered. |
| 305 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 306 | explicit AllNodesIterator(MIRGraph* mir_graph) |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 307 | : DataflowIterator(mir_graph, 0, mir_graph->GetBlockList().size()) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 310 | /** |
| 311 | * @brief Resetting the iterator. |
| 312 | */ |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 313 | void Reset() { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 314 | idx_ = 0; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 317 | /** |
| 318 | * @brief Get the next BasicBlock depending on iteration order. |
| 319 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 320 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 321 | */ |
| 322 | virtual BasicBlock* Next(bool had_change = false) ALWAYS_INLINE; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 325 | /** |
| 326 | * @class TopologicalSortIterator |
| 327 | * @brief Used to perform a Topological Sort Iteration of a MIRGraph. |
| 328 | */ |
| 329 | class TopologicalSortIterator : public DataflowIterator { |
| 330 | public: |
| 331 | /** |
| 332 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 333 | * @param mir_graph The MIRGraph considered. |
| 334 | */ |
| 335 | explicit TopologicalSortIterator(MIRGraph* mir_graph) |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 336 | : DataflowIterator(mir_graph, 0, mir_graph->GetTopologicalSortOrder().size()), |
| 337 | loop_ends_(&mir_graph->GetTopologicalSortOrderLoopEnds()), |
| 338 | loop_head_stack_(mir_graph_->GetTopologicalSortOrderLoopHeadStack()) { |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 339 | // Extra setup for TopologicalSortIterator. |
| 340 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 341 | block_id_list_ = &mir_graph->GetTopologicalSortOrder(); |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @brief Get the next BasicBlock depending on iteration order. |
| 346 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 347 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 348 | */ |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 349 | virtual BasicBlock* Next(bool had_change = false) OVERRIDE; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 350 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 351 | private: |
| 352 | const ArenaVector<BasicBlockId>* const loop_ends_; |
| 353 | ArenaVector<std::pair<uint16_t, bool>>* const loop_head_stack_; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 356 | /** |
| 357 | * @class LoopRepeatingTopologicalSortIterator |
| 358 | * @brief Used to perform a Topological Sort Iteration of a MIRGraph, repeating loops as needed. |
| 359 | * @details The iterator uses the visited flags to keep track of the blocks that need |
| 360 | * recalculation and keeps a stack of loop heads in the MIRGraph. At the end of the loop |
| 361 | * it returns back to the loop head if it needs to be recalculated. Due to the use of |
| 362 | * the visited flags and the loop head stack in the MIRGraph, it's not possible to use |
| 363 | * two iterators at the same time or modify this data during iteration (though inspection |
| 364 | * of this data is allowed and sometimes even expected). |
| 365 | * |
| 366 | * NOTE: This iterator is not suitable for passes that need to propagate changes to |
| 367 | * predecessors, such as type inferrence. |
| 368 | */ |
| 369 | class LoopRepeatingTopologicalSortIterator : public DataflowIterator { |
| 370 | public: |
| 371 | /** |
| 372 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 373 | * @param mir_graph The MIRGraph considered. |
| 374 | */ |
| 375 | explicit LoopRepeatingTopologicalSortIterator(MIRGraph* mir_graph) |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 376 | : DataflowIterator(mir_graph, 0, mir_graph->GetTopologicalSortOrder().size()), |
| 377 | loop_ends_(&mir_graph->GetTopologicalSortOrderLoopEnds()), |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 378 | loop_head_stack_(mir_graph_->GetTopologicalSortOrderLoopHeadStack()) { |
| 379 | // Extra setup for RepeatingTopologicalSortIterator. |
| 380 | idx_ = start_idx_; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 381 | block_id_list_ = &mir_graph->GetTopologicalSortOrder(); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 382 | // Clear visited flags and check that the loop head stack is empty. |
| 383 | mir_graph->ClearAllVisitedFlags(); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 384 | DCHECK_EQ(loop_head_stack_->size(), 0u); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | ~LoopRepeatingTopologicalSortIterator() { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 388 | DCHECK_EQ(loop_head_stack_->size(), 0u); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @brief Get the next BasicBlock depending on iteration order. |
| 393 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 394 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 395 | */ |
| 396 | virtual BasicBlock* Next(bool had_change = false) OVERRIDE; |
| 397 | |
| 398 | private: |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 399 | const ArenaVector<BasicBlockId>* const loop_ends_; |
| 400 | ArenaVector<std::pair<uint16_t, bool>>* const loop_head_stack_; |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 401 | }; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 402 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 403 | } // namespace art |
| 404 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 405 | #endif // ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |