blob: b05090b6a9f71af9b5a8b27ee2120798d3c94fb5 [file] [log] [blame]
Roland Levillainccc07a92014-09-16 14:48:16 +01001/*
2 * Copyright (C) 2014 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#include "graph_checker.h"
18
19#include <string>
20#include <map>
21#include <sstream>
22
23namespace art {
24
25void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
26 current_block_ = block;
27
28 // Check consistency with respect to predecessors of `block`.
29 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
30 std::map<HBasicBlock*, size_t> predecessors_count;
31 for (size_t i = 0, e = predecessors.Size(); i < e; ++i) {
32 HBasicBlock* p = predecessors.Get(i);
33 ++predecessors_count[p];
34 }
35 for (auto& pc : predecessors_count) {
36 HBasicBlock* p = pc.first;
37 size_t p_count_in_block_predecessors = pc.second;
38 const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors();
39 size_t block_count_in_p_successors = 0;
40 for (size_t j = 0, f = p_successors.Size(); j < f; ++j) {
41 if (p_successors.Get(j) == block) {
42 ++block_count_in_p_successors;
43 }
44 }
45 if (p_count_in_block_predecessors != block_count_in_p_successors) {
46 std::stringstream error;
47 error << "Block " << block->GetBlockId()
48 << " lists " << p_count_in_block_predecessors
49 << " occurrences of block " << p->GetBlockId()
50 << " in its predecessors, whereas block " << p->GetBlockId()
51 << " lists " << block_count_in_p_successors
52 << " occurrences of block " << block->GetBlockId()
53 << " in its successors.";
54 errors_.Insert(error.str());
55 }
56 }
57
58 // Check consistency with respect to successors of `block`.
59 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
60 std::map<HBasicBlock*, size_t> successors_count;
61 for (size_t i = 0, e = successors.Size(); i < e; ++i) {
62 HBasicBlock* s = successors.Get(i);
63 ++successors_count[s];
64 }
65 for (auto& sc : successors_count) {
66 HBasicBlock* s = sc.first;
67 size_t s_count_in_block_successors = sc.second;
68 const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors();
69 size_t block_count_in_s_predecessors = 0;
70 for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) {
71 if (s_predecessors.Get(j) == block) {
72 ++block_count_in_s_predecessors;
73 }
74 }
75 if (s_count_in_block_successors != block_count_in_s_predecessors) {
76 std::stringstream error;
77 error << "Block " << block->GetBlockId()
78 << " lists " << s_count_in_block_successors
79 << " occurrences of block " << s->GetBlockId()
80 << " in its successors, whereas block " << s->GetBlockId()
81 << " lists " << block_count_in_s_predecessors
82 << " occurrences of block " << block->GetBlockId()
83 << " in its predecessors.";
84 errors_.Insert(error.str());
85 }
86 }
87
88 // Ensure `block` ends with a branch instruction.
89 HInstruction* last_inst = block->GetLastInstruction();
90 if (last_inst == nullptr || !last_inst->IsControlFlow()) {
91 std::stringstream error;
92 error << "Block " << block->GetBlockId()
93 << " does not end with a branch instruction.";
94 errors_.Insert(error.str());
95 }
96
97 // Visit this block's list of phis.
98 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
99 // Ensure this block's list of phis contains only phis.
100 if (!it.Current()->IsPhi()) {
101 std::stringstream error;
102 error << "Block " << current_block_->GetBlockId()
103 << " has a non-phi in its phi list.";
104 errors_.Insert(error.str());
105 }
106 it.Current()->Accept(this);
107 }
108
109 // Visit this block's list of instructions.
110 for (HInstructionIterator it(block->GetInstructions()); !it.Done();
111 it.Advance()) {
112 // Ensure this block's list of instructions does not contains phis.
113 if (it.Current()->IsPhi()) {
114 std::stringstream error;
115 error << "Block " << current_block_->GetBlockId()
116 << " has a phi in its non-phi list.";
117 errors_.Insert(error.str());
118 }
119 it.Current()->Accept(this);
120 }
121}
122
123void GraphChecker::VisitInstruction(HInstruction* instruction) {
124 // Ensure `instruction` is associated with `current_block_`.
125 if (instruction->GetBlock() != current_block_) {
126 std::stringstream error;
127 if (instruction->IsPhi()) {
128 error << "Phi ";
129 } else {
130 error << "Instruction ";
131 }
132 error << instruction->GetId() << " in block "
133 << current_block_->GetBlockId();
134 if (instruction->GetBlock() != nullptr) {
135 error << " associated with block "
136 << instruction->GetBlock()->GetBlockId() << ".";
137 } else {
138 error << " not associated with any block.";
139 }
140 errors_.Insert(error.str());
141 }
142}
143
144void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
145 super_type::VisitBasicBlock(block);
146
147 // Ensure there is no critical edge (i.e., an edge connecting a
148 // block with multiple successors to a block with multiple
149 // predecessors).
150 if (block->GetSuccessors().Size() > 1) {
151 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
152 HBasicBlock* successor = block->GetSuccessors().Get(j);
153 if (successor->GetPredecessors().Size() > 1) {
154 std::stringstream error;
155 error << "Critical edge between blocks " << block->GetBlockId()
156 << " and " << successor->GetBlockId() << ".";
157 errors_.Insert(error.str());
158 }
159 }
160 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100161
162 if (block->IsLoopHeader()) {
163 CheckLoop(block);
164 }
165}
166
167void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
168 int id = loop_header->GetBlockId();
169
170 // Ensure the pre-header block is first in the list of
171 // predecessors of a loop header.
172 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
173 std::stringstream error;
174 error << "Loop pre-header is not the first predecessor of the loop header "
175 << id << ".";
176 errors_.Insert(error.str());
177 }
178
179 // Ensure the loop header has only two predecessors and that only the
180 // second one is a back edge.
181 if (loop_header->GetPredecessors().Size() < 2) {
182 std::stringstream error;
183 error << "Loop header " << id << " has less than two predecessors.";
184 errors_.Insert(error.str());
185 } else if (loop_header->GetPredecessors().Size() > 2) {
186 std::stringstream error;
187 error << "Loop header " << id << " has more than two predecessors.";
188 errors_.Insert(error.str());
189 } else {
190 HLoopInformation* loop_information = loop_header->GetLoopInformation();
191 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
192 if (loop_information->IsBackEdge(first_predecessor)) {
193 std::stringstream error;
194 error << "First predecessor of loop header " << id << " is a back edge.";
195 errors_.Insert(error.str());
196 }
197 HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1);
198 if (!loop_information->IsBackEdge(second_predecessor)) {
199 std::stringstream error;
200 error << "Second predecessor of loop header " << id
201 << " is not a back edge.";
202 errors_.Insert(error.str());
203 }
204 }
205
206 // Ensure there is only one back edge per loop.
207 size_t num_back_edges =
208 loop_header->GetLoopInformation()->GetBackEdges().Size();
209 if (num_back_edges != 1) {
210 std::stringstream error;
211 error << "Loop defined by header " << id << " has "
212 << num_back_edges << " back edge(s).";
213 errors_.Insert(error.str());
214 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100215}
216
217void SSAChecker::VisitInstruction(HInstruction* instruction) {
218 super_type::VisitInstruction(instruction);
219
220 // Ensure an instruction dominates all its uses (or in the present
221 // case, that all uses of an instruction (used as input) are
222 // dominated by its definition).
223 for (HInputIterator input_it(instruction); !input_it.Done();
224 input_it.Advance()) {
225 HInstruction* input = input_it.Current();
226 if (!input->Dominates(instruction)) {
227 std::stringstream error;
228 error << "Instruction " << input->GetId()
229 << " in block " << input->GetBlock()->GetBlockId()
230 << " does not dominate use " << instruction->GetId()
231 << " in block " << current_block_->GetBlockId() << ".";
232 errors_.Insert(error.str());
233 }
234 }
235}
236
Roland Levillain6b879dd2014-09-22 17:13:44 +0100237void SSAChecker::VisitPhi(HPhi* phi) {
238 VisitInstruction(phi);
239
240 // Ensure the first input of a phi is not itself.
241 if (phi->InputAt(0) == phi) {
242 std::stringstream error;
243 error << "Loop phi " << phi->GetId()
244 << " in block " << phi->GetBlock()->GetBlockId()
245 << " is its own first input.";
246 errors_.Insert(error.str());
247 }
248
249 // Ensure the number of phi inputs is the same as the number of
250 // its predecessors.
251 const GrowableArray<HBasicBlock*>& predecessors =
252 phi->GetBlock()->GetPredecessors();
253 if (phi->InputCount() != predecessors.Size()) {
254 std::stringstream error;
255 error << "Phi " << phi->GetId()
256 << " in block " << phi->GetBlock()->GetBlockId()
257 << " has " << phi->InputCount() << " inputs, but block "
258 << phi->GetBlock()->GetBlockId() << " has "
259 << predecessors.Size() << " predecessors.";
260 errors_.Insert(error.str());
261 } else {
262 // Ensure phi input at index I either comes from the Ith
263 // predecessor or from a block that dominates this predecessor.
264 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
265 HInstruction* input = phi->InputAt(i);
266 HBasicBlock* predecessor = predecessors.Get(i);
267 if (!(input->GetBlock() == predecessor
268 || input->GetBlock()->Dominates(predecessor))) {
269 std::stringstream error;
270 error << "Input " << input->GetId() << " at index " << i
271 << " of phi " << phi->GetId()
272 << " from block " << phi->GetBlock()->GetBlockId()
273 << " is not defined in predecessor number " << i
274 << " nor in a block dominating it.";
275 errors_.Insert(error.str());
276 }
277 }
278 }
279}
280
Roland Levillainccc07a92014-09-16 14:48:16 +0100281} // namespace art