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 | |
| 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, |
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), |
Ian Rogers | e7a5b7d | 2013-04-18 20:09:02 -0700 | [diff] [blame] | 75 | block_id_list_(NULL), |
| 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 */ |
| 107 | GrowableArray<BasicBlockId>* block_id_list_; /**< @brief the list of BasicBlocks we want to iterate on */ |
| 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_; |
| 127 | block_id_list_ = mir_graph->GetDfsOrder(); |
| 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 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * @brief Redefine the new operator to use the arena |
| 144 | * @param size actually unused, we use our own class size |
| 145 | * @param arena the arena to perform the actual allocation |
| 146 | * @return the pointer to the newly allocated object |
| 147 | */ |
| 148 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 149 | return arena->Alloc(sizeof(PreOrderDfsIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 154 | */ |
| 155 | static void operator delete(void* p) {} |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 158 | /** |
| 159 | * @class RepeatingPreOrderDfsIterator |
| 160 | * @brief Used to perform a Repeating Pre-order Depth-First-Search Iteration of a MIRGraph. |
| 161 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 162 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 163 | class RepeatingPreOrderDfsIterator : public DataflowIterator { |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 164 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 165 | /** |
| 166 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 167 | * @param mir_graph The MIRGraph considered. |
| 168 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 169 | explicit RepeatingPreOrderDfsIterator(MIRGraph* mir_graph) |
| 170 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 171 | // Extra setup for the RepeatingPreOrderDfsIterator. |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 172 | idx_ = start_idx_; |
| 173 | block_id_list_ = mir_graph->GetDfsOrder(); |
| 174 | } |
| 175 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 176 | /** |
| 177 | * @brief Get the next BasicBlock depending on iteration order. |
| 178 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 179 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 180 | */ |
| 181 | virtual BasicBlock* Next(bool had_change = false) { |
| 182 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 183 | changed_ |= had_change; |
| 184 | |
| 185 | return ForwardRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 186 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * @brief Redefine the new operator to use the arena |
| 190 | * @param size actually unused, we use our own class size |
| 191 | * @param arena the arena to perform the actual allocation |
| 192 | * @return the pointer to the newly allocated object |
| 193 | */ |
| 194 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 195 | return arena->Alloc(sizeof(RepeatingPreOrderDfsIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 200 | */ |
| 201 | static void operator delete(void* p) {} |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 204 | /** |
| 205 | * @class RepeatingPostOrderDfsIterator |
| 206 | * @brief Used to perform a Repeating Post-order Depth-First-Search Iteration of a MIRGraph. |
| 207 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 208 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 209 | class RepeatingPostOrderDfsIterator : 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 RepeatingPostOrderDfsIterator(MIRGraph* mir_graph) |
| 216 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 217 | // Extra setup for the RepeatingPostOrderDfsIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 218 | idx_ = start_idx_; |
| 219 | block_id_list_ = mir_graph->GetDfsPostOrder(); |
| 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 | |
| 231 | return ForwardRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 232 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * @brief Redefine the new operator to use the arena |
| 236 | * @param size actually unused, we use our own class size |
| 237 | * @param arena the arena to perform the actual allocation |
| 238 | * @return the pointer to the newly allocated object |
| 239 | */ |
| 240 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 241 | return arena->Alloc(sizeof(RepeatingPostOrderDfsIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 246 | */ |
| 247 | static void operator delete(void* p) {} |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 250 | /** |
| 251 | * @class ReversePostOrderDfsIterator |
| 252 | * @brief Used to perform a Reverse Post-order Depth-First-Search Iteration of a MIRGraph. |
| 253 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 254 | class ReversePostOrderDfsIterator : public DataflowIterator { |
| 255 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 256 | /** |
| 257 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 258 | * @param mir_graph The MIRGraph considered. |
| 259 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 260 | explicit ReversePostOrderDfsIterator(MIRGraph* mir_graph) |
| 261 | : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 262 | // Extra setup for the ReversePostOrderDfsIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 263 | idx_ = start_idx_; |
| 264 | block_id_list_ = mir_graph->GetDfsPostOrder(); |
| 265 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 266 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 267 | /** |
| 268 | * @brief Get the next BasicBlock depending on iteration order. |
| 269 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 270 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 271 | */ |
| 272 | virtual BasicBlock* Next(bool had_change = false) { |
| 273 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 274 | changed_ |= had_change; |
| 275 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 276 | return ReverseSingleNext(); |
| 277 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * @brief Redefine the new operator to use the arena |
| 281 | * @param size actually unused, we use our own class size |
| 282 | * @param arena the arena to perform the actual allocation |
| 283 | * @return the pointer to the newly allocated object |
| 284 | */ |
| 285 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 286 | return arena->Alloc(sizeof(ReversePostOrderDfsIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 291 | */ |
| 292 | static void operator delete(void* p) {} |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 295 | /** |
| 296 | * @class ReversePostOrderDfsIterator |
| 297 | * @brief Used to perform a Repeating Reverse Post-order Depth-First-Search Iteration of a MIRGraph. |
| 298 | * @details If there is a change during an iteration, the iteration starts over at the end of the iteration. |
| 299 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 300 | class RepeatingReversePostOrderDfsIterator : 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 RepeatingReversePostOrderDfsIterator(MIRGraph* mir_graph) |
| 307 | : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 308 | // Extra setup for the RepeatingReversePostOrderDfsIterator |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 309 | idx_ = start_idx_; |
| 310 | block_id_list_ = mir_graph->GetDfsPostOrder(); |
| 311 | } |
| 312 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 313 | /** |
| 314 | * @brief Get the next BasicBlock depending on iteration order. |
| 315 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 316 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 317 | */ |
| 318 | virtual BasicBlock* Next(bool had_change = false) { |
| 319 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 320 | changed_ |= had_change; |
| 321 | |
| 322 | return ReverseRepeatNext(); |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 323 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * @brief Redefine the new operator to use the arena |
| 327 | * @param size actually unused, we use our own class size |
| 328 | * @param arena the arena to perform the actual allocation |
| 329 | * @return the pointer to the newly allocated object |
| 330 | */ |
| 331 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 332 | return arena->Alloc(sizeof(RepeatingReversePostOrderDfsIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 337 | */ |
| 338 | static void operator delete(void* p) {} |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 341 | /** |
| 342 | * @class PostOrderDOMIterator |
| 343 | * @brief Used to perform a Post-order Domination Iteration of a MIRGraph. |
| 344 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 345 | class PostOrderDOMIterator : public DataflowIterator { |
| 346 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 347 | /** |
| 348 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 349 | * @param mir_graph The MIRGraph considered. |
| 350 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 351 | explicit PostOrderDOMIterator(MIRGraph* mir_graph) |
| 352 | : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 353 | // Extra setup for thePostOrderDOMIterator. |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 354 | idx_ = start_idx_; |
| 355 | block_id_list_ = mir_graph->GetDomPostOrder(); |
| 356 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 357 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 358 | /** |
| 359 | * @brief Get the next BasicBlock depending on iteration order. |
| 360 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 361 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 362 | */ |
| 363 | virtual BasicBlock* Next(bool had_change = false) { |
| 364 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 365 | changed_ |= had_change; |
| 366 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 367 | return ForwardSingleNext(); |
| 368 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * @brief Redefine the new operator to use the arena |
| 372 | * @param size actually unused, we use our own class size |
| 373 | * @param arena the arena to perform the actual allocation |
| 374 | * @return the pointer to the newly allocated object |
| 375 | */ |
| 376 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 377 | return arena->Alloc(sizeof(PostOrderDOMIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 382 | */ |
| 383 | static void operator delete(void* p) {} |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 384 | }; |
| 385 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 386 | /** |
| 387 | * @class AllNodesIterator |
| 388 | * @brief Used to perform an iteration on all the BasicBlocks a MIRGraph. |
| 389 | */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 390 | class AllNodesIterator : public DataflowIterator { |
| 391 | public: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 392 | /** |
| 393 | * @brief The constructor, using all of the reachable blocks of the MIRGraph. |
| 394 | * @param mir_graph The MIRGraph considered. |
| 395 | */ |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 396 | explicit AllNodesIterator(MIRGraph* mir_graph) |
| 397 | : DataflowIterator(mir_graph, 0, 0) { |
| 398 | all_nodes_iterator_ = new |
| 399 | (mir_graph->GetArena()) GrowableArray<BasicBlock*>::Iterator(mir_graph->GetBlockList()); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 402 | /** |
| 403 | * @brief Resetting the iterator. |
| 404 | */ |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 405 | void Reset() { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 406 | all_nodes_iterator_->Reset(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 409 | /** |
| 410 | * @brief Get the next BasicBlock depending on iteration order. |
| 411 | * @param had_change did the user of the iteration change the previous BasicBlock. |
| 412 | * @return the next BasicBlock following the iteration order, 0 if finished. |
| 413 | */ |
| 414 | virtual BasicBlock* Next(bool had_change = false) ALWAYS_INLINE; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 415 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 416 | /** |
| 417 | * @brief Redefine the new operator to use the arena |
| 418 | * @param size actually unused, we use our own class size |
| 419 | * @param arena the arena to perform the actual allocation |
| 420 | * @return the pointer to the newly allocated object |
| 421 | */ |
| 422 | static void* operator new(size_t size, ArenaAllocator* arena) { |
| 423 | return arena->Alloc(sizeof(AllNodesIterator), ArenaAllocator::kAllocGrowableBitMap); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * @brief Redefine delete to not actually delete anything since we are using the arena |
| 428 | */ |
| 429 | static void operator delete(void* p) {} |
| 430 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 431 | private: |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 432 | GrowableArray<BasicBlock*>::Iterator* all_nodes_iterator_; /**< @brief The list of all the nodes */ |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 435 | } // namespace art |
| 436 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 437 | #endif // ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_ |