Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [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_INL_H_ |
| 18 | #define ART_COMPILER_DEX_DATAFLOW_ITERATOR_INL_H_ |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 19 | |
| 20 | #include "dataflow_iterator.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 24 | // Single forward pass over the nodes. |
| 25 | inline BasicBlock* DataflowIterator::ForwardSingleNext() { |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 26 | BasicBlock* res = NULL; |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 27 | |
| 28 | // Are we not yet at the end? |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 29 | if (idx_ < end_idx_) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 30 | // Get the next index. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 31 | BasicBlockId bb_id = (*block_id_list_)[idx_]; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 32 | res = mir_graph_->GetBasicBlock(bb_id); |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 33 | idx_++; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 34 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 35 | |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 36 | return res; |
| 37 | } |
| 38 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 39 | // Repeat full forward passes over all nodes until no change occurs during a complete pass. |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 40 | inline BasicBlock* DataflowIterator::ForwardRepeatNext() { |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 41 | BasicBlock* res = NULL; |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 42 | |
| 43 | // Are we at the end and have we changed something? |
| 44 | if ((idx_ >= end_idx_) && changed_ == true) { |
| 45 | // Reset the index. |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 46 | idx_ = start_idx_; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 47 | repeats_++; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 48 | changed_ = false; |
| 49 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 50 | |
| 51 | // Are we not yet at the end? |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 52 | if (idx_ < end_idx_) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 53 | // Get the BasicBlockId. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 54 | BasicBlockId bb_id = (*block_id_list_)[idx_]; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 55 | res = mir_graph_->GetBasicBlock(bb_id); |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 56 | idx_++; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 57 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 58 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 59 | return res; |
| 60 | } |
| 61 | |
| 62 | // Single reverse pass over the nodes. |
| 63 | inline BasicBlock* DataflowIterator::ReverseSingleNext() { |
| 64 | BasicBlock* res = NULL; |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 65 | |
| 66 | // Are we not yet at the end? |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 67 | if (idx_ >= 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 68 | // Get the BasicBlockId. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 69 | BasicBlockId bb_id = (*block_id_list_)[idx_]; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 70 | res = mir_graph_->GetBasicBlock(bb_id); |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 71 | idx_--; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 72 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 73 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 74 | return res; |
| 75 | } |
| 76 | |
| 77 | // Repeat full backwards passes over all nodes until no change occurs during a complete pass. |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 78 | inline BasicBlock* DataflowIterator::ReverseRepeatNext() { |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 79 | BasicBlock* res = NULL; |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 80 | |
| 81 | // Are we done and we changed something during the last iteration? |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 82 | if ((idx_ < 0) && changed_) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 83 | // Reset the index. |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 84 | idx_ = start_idx_; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 85 | repeats_++; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 86 | changed_ = false; |
| 87 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 88 | |
| 89 | // Are we not yet done? |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 90 | if (idx_ >= 0) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 91 | // Get the BasicBlockId. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 92 | BasicBlockId bb_id = (*block_id_list_)[idx_]; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 93 | res = mir_graph_->GetBasicBlock(bb_id); |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 94 | idx_--; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 95 | } |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 96 | |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 97 | return res; |
| 98 | } |
| 99 | |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 100 | // AllNodes uses the existing block list, and should be considered unordered. |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 101 | inline BasicBlock* AllNodesIterator::Next(bool had_change) { |
Jean Christophe Beyler | 6e3cb66 | 2013-12-20 15:47:52 -0800 | [diff] [blame] | 102 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 103 | changed_ |= had_change; |
| 104 | |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 105 | BasicBlock* res = nullptr; |
| 106 | while (idx_ != end_idx_) { |
| 107 | BasicBlock* bb = mir_graph_->GetBlockList()[idx_++]; |
| 108 | DCHECK(bb != nullptr); |
| 109 | if (!bb->hidden) { |
| 110 | res = bb; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 115 | return res; |
| 116 | } |
| 117 | |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 118 | inline BasicBlock* TopologicalSortIterator::Next(bool had_change) { |
| 119 | // Update changed: if had_changed is true, we remember it for the whole iteration. |
| 120 | changed_ |= had_change; |
| 121 | |
| 122 | while (loop_head_stack_->size() != 0u && |
| 123 | (*loop_ends_)[loop_head_stack_->back().first] == idx_) { |
| 124 | loop_head_stack_->pop_back(); |
| 125 | } |
| 126 | |
| 127 | if (idx_ == end_idx_) { |
| 128 | return nullptr; |
| 129 | } |
| 130 | |
| 131 | // Get next block and return it. |
| 132 | BasicBlockId idx = idx_; |
| 133 | idx_ += 1; |
| 134 | BasicBlock* bb = mir_graph_->GetBasicBlock((*block_id_list_)[idx]); |
| 135 | DCHECK(bb != nullptr); |
| 136 | if ((*loop_ends_)[idx] != 0u) { |
| 137 | loop_head_stack_->push_back(std::make_pair(idx, false)); // Not recalculating. |
| 138 | } |
| 139 | return bb; |
| 140 | } |
| 141 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 142 | inline BasicBlock* LoopRepeatingTopologicalSortIterator::Next(bool had_change) { |
| 143 | if (idx_ != 0) { |
| 144 | // Mark last processed block visited. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 145 | BasicBlock* bb = mir_graph_->GetBasicBlock((*block_id_list_)[idx_ - 1]); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 146 | bb->visited = true; |
| 147 | if (had_change) { |
| 148 | // If we had a change we need to revisit the children. |
| 149 | ChildBlockIterator iter(bb, mir_graph_); |
| 150 | for (BasicBlock* child_bb = iter.Next(); child_bb != nullptr; child_bb = iter.Next()) { |
| 151 | child_bb->visited = false; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | while (true) { |
| 157 | // Pop loops we have left and check if we need to recalculate one of them. |
| 158 | // NOTE: We need to do this even if idx_ == end_idx_. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 159 | while (loop_head_stack_->size() != 0u && |
| 160 | (*loop_ends_)[loop_head_stack_->back().first] == idx_) { |
| 161 | auto top = loop_head_stack_->back(); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 162 | uint16_t loop_head_idx = top.first; |
| 163 | bool recalculated = top.second; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 164 | loop_head_stack_->pop_back(); |
| 165 | BasicBlock* loop_head = mir_graph_->GetBasicBlock((*block_id_list_)[loop_head_idx]); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 166 | DCHECK(loop_head != nullptr); |
| 167 | if (!recalculated || !loop_head->visited) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 168 | // Recalculating this loop. |
| 169 | loop_head_stack_->push_back(std::make_pair(loop_head_idx, true)); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 170 | idx_ = loop_head_idx + 1; |
| 171 | return loop_head; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (idx_ == end_idx_) { |
| 176 | return nullptr; |
| 177 | } |
| 178 | |
| 179 | // Get next block and return it if unvisited. |
| 180 | BasicBlockId idx = idx_; |
| 181 | idx_ += 1; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 182 | BasicBlock* bb = mir_graph_->GetBasicBlock((*block_id_list_)[idx]); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 183 | DCHECK(bb != nullptr); |
| 184 | if (!bb->visited) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 185 | if ((*loop_ends_)[idx] != 0u) { |
| 186 | loop_head_stack_->push_back(std::make_pair(idx, false)); // Not recalculating. |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 187 | } |
| 188 | return bb; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 193 | } // namespace art |
| 194 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 195 | #endif // ART_COMPILER_DEX_DATAFLOW_ITERATOR_INL_H_ |