blob: a4b38bd80f03589e10f82036a4151db598575b6c [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
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
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,
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 */
buzbee311ca162013-02-28 15:56:43 -080042 class DataflowIterator {
43 public:
buzbee311ca162013-02-28 15:56:43 -080044
buzbee0665fe02013-03-21 12:32:21 -070045 virtual ~DataflowIterator(){}
buzbee311ca162013-02-28 15:56:43 -080046
buzbee0665fe02013-03-21 12:32:21 -070047 // 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),
Ian Rogerse7a5b7d2013-04-18 20:09:02 -070069 reverse_(reverse),
70 block_id_list_(NULL),
71 idx_(0),
72 changed_(false) {}
buzbee0665fe02013-03-21 12:32:21 -070073
74 virtual BasicBlock* NextBody(bool had_change);
75
76 MIRGraph* const mir_graph_;
77 const bool is_iterative_;
78 const int start_idx_;
79 const int end_idx_;
80 const bool reverse_;
buzbee862a7602013-04-05 10:58:54 -070081 GrowableArray<int>* block_id_list_;
buzbee0665fe02013-03-21 12:32:21 -070082 int idx_;
83 bool changed_;
buzbee311ca162013-02-28 15:56:43 -080084
85 }; // DataflowIterator
buzbee0665fe02013-03-21 12:32:21 -070086
87 class ReachableNodesIterator : public DataflowIterator {
88 public:
89
90 ReachableNodesIterator(MIRGraph* mir_graph, bool is_iterative)
91 : DataflowIterator(mir_graph, is_iterative, 0,
92 mir_graph->GetNumReachableBlocks(), false) {
93 idx_ = start_idx_;
94 block_id_list_ = mir_graph->GetDfsOrder();
95 }
96 };
97
98 class PreOrderDfsIterator : public DataflowIterator {
99 public:
100
101 PreOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative)
102 : DataflowIterator(mir_graph, is_iterative, 0,
103 mir_graph->GetNumReachableBlocks(), false) {
104 idx_ = start_idx_;
105 block_id_list_ = mir_graph->GetDfsOrder();
106 }
107 };
108
109 class PostOrderDfsIterator : public DataflowIterator {
110 public:
111
112 PostOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative)
113 : DataflowIterator(mir_graph, is_iterative, 0,
114 mir_graph->GetNumReachableBlocks(), false) {
115 idx_ = start_idx_;
116 block_id_list_ = mir_graph->GetDfsPostOrder();
117 }
118 };
119
120 class ReversePostOrderDfsIterator : public DataflowIterator {
121 public:
122
123 ReversePostOrderDfsIterator(MIRGraph* mir_graph, bool is_iterative)
124 : DataflowIterator(mir_graph, is_iterative,
125 mir_graph->GetNumReachableBlocks() -1, 0, true) {
126 idx_ = start_idx_;
127 block_id_list_ = mir_graph->GetDfsPostOrder();
128 }
129 };
130
131 class PostOrderDOMIterator : public DataflowIterator {
132 public:
133
134 PostOrderDOMIterator(MIRGraph* mir_graph, bool is_iterative)
135 : DataflowIterator(mir_graph, is_iterative, 0,
136 mir_graph->GetNumReachableBlocks(), false) {
137 idx_ = start_idx_;
138 block_id_list_ = mir_graph->GetDomPostOrder();
139 }
140 };
141
142 class AllNodesIterator : public DataflowIterator {
143 public:
144
145 AllNodesIterator(MIRGraph* mir_graph, bool is_iterative)
146 : DataflowIterator(mir_graph, is_iterative, 0, 0, false) {
buzbee862a7602013-04-05 10:58:54 -0700147 all_nodes_iterator_ =
148 new (mir_graph->GetArena()) GrowableArray<BasicBlock*>::Iterator (mir_graph->GetBlockList());
149 }
150
151 virtual void Reset() {
152 all_nodes_iterator_->Reset();
buzbee0665fe02013-03-21 12:32:21 -0700153 }
154
155 virtual BasicBlock* NextBody(bool had_change);
buzbee862a7602013-04-05 10:58:54 -0700156
157 private:
158 GrowableArray<BasicBlock*>::Iterator* all_nodes_iterator_;
buzbee0665fe02013-03-21 12:32:21 -0700159 };
160
buzbee311ca162013-02-28 15:56:43 -0800161} // namespace art
162
163#endif // ART_SRC_COMPILER_DEX_DATAFLOW_ITERATOR_H_