blob: 1dab54ea7287802697c1f9a928fceb83a5bb3d9d [file] [log] [blame]
buzbee311ca162013-02-28 15:56:43 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_
18#define ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_
buzbee311ca162013-02-28 15:56:43 -080019
20#include "compiler_ir.h"
21#include "mir_graph.h"
22
23namespace art {
24
buzbee0665fe02013-03-21 12:32:21 -070025 /*
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,
buzbee56c71782013-09-05 17:13:19 -070030 * 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.
buzbee0665fe02013-03-21 12:32:21 -070036 */
buzbee311ca162013-02-28 15:56:43 -080037 class DataflowIterator {
38 public:
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070039 virtual ~DataflowIterator() {}
buzbee311ca162013-02-28 15:56:43 -080040
buzbee0665fe02013-03-21 12:32:21 -070041 protected:
buzbee56c71782013-09-05 17:13:19 -070042 DataflowIterator(MIRGraph* mir_graph, int start_idx, int end_idx)
buzbee0665fe02013-03-21 12:32:21 -070043 : mir_graph_(mir_graph),
buzbee0665fe02013-03-21 12:32:21 -070044 start_idx_(start_idx),
45 end_idx_(end_idx),
Ian Rogerse7a5b7d2013-04-18 20:09:02 -070046 block_id_list_(NULL),
47 idx_(0),
48 changed_(false) {}
buzbee0665fe02013-03-21 12:32:21 -070049
buzbee56c71782013-09-05 17:13:19 -070050 virtual BasicBlock* ForwardSingleNext() ALWAYS_INLINE;
51 virtual BasicBlock* ReverseSingleNext() ALWAYS_INLINE;
52 virtual BasicBlock* ForwardRepeatNext(bool had_change) ALWAYS_INLINE;
53 virtual BasicBlock* ReverseRepeatNext(bool had_change) ALWAYS_INLINE;
buzbee0665fe02013-03-21 12:32:21 -070054
55 MIRGraph* const mir_graph_;
buzbee0665fe02013-03-21 12:32:21 -070056 const int start_idx_;
57 const int end_idx_;
buzbee862a7602013-04-05 10:58:54 -070058 GrowableArray<int>* block_id_list_;
buzbee0665fe02013-03-21 12:32:21 -070059 int idx_;
60 bool changed_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061 }; // DataflowIterator
buzbee0665fe02013-03-21 12:32:21 -070062
buzbee0665fe02013-03-21 12:32:21 -070063 class PreOrderDfsIterator : public DataflowIterator {
64 public:
buzbee56c71782013-09-05 17:13:19 -070065 explicit PreOrderDfsIterator(MIRGraph* mir_graph)
66 : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) {
buzbee0665fe02013-03-21 12:32:21 -070067 idx_ = start_idx_;
68 block_id_list_ = mir_graph->GetDfsOrder();
69 }
buzbee56c71782013-09-05 17:13:19 -070070
71 BasicBlock* Next() {
72 return ForwardSingleNext();
73 }
buzbee0665fe02013-03-21 12:32:21 -070074 };
75
buzbee56c71782013-09-05 17:13:19 -070076 class RepeatingPreOrderDfsIterator : public DataflowIterator {
buzbee0665fe02013-03-21 12:32:21 -070077 public:
buzbee56c71782013-09-05 17:13:19 -070078 explicit RepeatingPreOrderDfsIterator(MIRGraph* mir_graph)
79 : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) {
80 idx_ = start_idx_;
81 block_id_list_ = mir_graph->GetDfsOrder();
82 }
83
84 BasicBlock* Next(bool had_change) {
85 return ForwardRepeatNext(had_change);
86 }
87 };
88
89 class RepeatingPostOrderDfsIterator : public DataflowIterator {
90 public:
91 explicit RepeatingPostOrderDfsIterator(MIRGraph* mir_graph)
92 : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) {
buzbee0665fe02013-03-21 12:32:21 -070093 idx_ = start_idx_;
94 block_id_list_ = mir_graph->GetDfsPostOrder();
95 }
buzbee56c71782013-09-05 17:13:19 -070096
97 BasicBlock* Next(bool had_change) {
98 return ForwardRepeatNext(had_change);
99 }
buzbee0665fe02013-03-21 12:32:21 -0700100 };
101
102 class ReversePostOrderDfsIterator : public DataflowIterator {
103 public:
buzbee56c71782013-09-05 17:13:19 -0700104 explicit ReversePostOrderDfsIterator(MIRGraph* mir_graph)
105 : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) {
buzbee0665fe02013-03-21 12:32:21 -0700106 idx_ = start_idx_;
107 block_id_list_ = mir_graph->GetDfsPostOrder();
108 }
buzbee56c71782013-09-05 17:13:19 -0700109
110 BasicBlock* Next() {
111 return ReverseSingleNext();
112 }
113 };
114
115 class RepeatingReversePostOrderDfsIterator : public DataflowIterator {
116 public:
117 explicit RepeatingReversePostOrderDfsIterator(MIRGraph* mir_graph)
118 : DataflowIterator(mir_graph, mir_graph->GetNumReachableBlocks() -1, 0) {
119 idx_ = start_idx_;
120 block_id_list_ = mir_graph->GetDfsPostOrder();
121 }
122
123 BasicBlock* Next(bool had_change) {
124 return ReverseRepeatNext(had_change);
125 }
buzbee0665fe02013-03-21 12:32:21 -0700126 };
127
128 class PostOrderDOMIterator : public DataflowIterator {
129 public:
buzbee56c71782013-09-05 17:13:19 -0700130 explicit PostOrderDOMIterator(MIRGraph* mir_graph)
131 : DataflowIterator(mir_graph, 0, mir_graph->GetNumReachableBlocks()) {
buzbee0665fe02013-03-21 12:32:21 -0700132 idx_ = start_idx_;
133 block_id_list_ = mir_graph->GetDomPostOrder();
134 }
buzbee56c71782013-09-05 17:13:19 -0700135
136 BasicBlock* Next() {
137 return ForwardSingleNext();
138 }
buzbee0665fe02013-03-21 12:32:21 -0700139 };
140
141 class AllNodesIterator : public DataflowIterator {
142 public:
buzbee56c71782013-09-05 17:13:19 -0700143 explicit AllNodesIterator(MIRGraph* mir_graph)
144 : DataflowIterator(mir_graph, 0, 0) {
145 all_nodes_iterator_ = new
146 (mir_graph->GetArena()) GrowableArray<BasicBlock*>::Iterator(mir_graph->GetBlockList());
buzbee862a7602013-04-05 10:58:54 -0700147 }
148
Ian Rogers8d3a1172013-06-04 01:13:28 -0700149 void Reset() {
buzbee862a7602013-04-05 10:58:54 -0700150 all_nodes_iterator_->Reset();
buzbee0665fe02013-03-21 12:32:21 -0700151 }
152
buzbee56c71782013-09-05 17:13:19 -0700153 BasicBlock* Next() ALWAYS_INLINE;
buzbee862a7602013-04-05 10:58:54 -0700154
155 private:
156 GrowableArray<BasicBlock*>::Iterator* all_nodes_iterator_;
buzbee0665fe02013-03-21 12:32:21 -0700157 };
158
buzbee311ca162013-02-28 15:56:43 -0800159} // namespace art
160
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700161#endif // ART_COMPILER_DEX_DATAFLOW_ITERATOR_H_