blob: cfebb77dd7518d27ad34a9c1a69cc48141f821c9 [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
Roland Levillainccc07a92014-09-16 14:48:16 +010019#include <map>
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000020#include <string>
Calin Juravlea4f88312015-04-16 12:57:19 +010021#include <sstream>
Roland Levillainccc07a92014-09-16 14:48:16 +010022
Roland Levillain7e53b412014-09-23 10:50:22 +010023#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000024#include "base/stringprintf.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010025
Roland Levillainccc07a92014-09-16 14:48:16 +010026namespace art {
27
28void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
29 current_block_ = block;
30
31 // Check consistency with respect to predecessors of `block`.
32 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
33 std::map<HBasicBlock*, size_t> predecessors_count;
34 for (size_t i = 0, e = predecessors.Size(); i < e; ++i) {
35 HBasicBlock* p = predecessors.Get(i);
36 ++predecessors_count[p];
37 }
38 for (auto& pc : predecessors_count) {
39 HBasicBlock* p = pc.first;
40 size_t p_count_in_block_predecessors = pc.second;
41 const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors();
42 size_t block_count_in_p_successors = 0;
43 for (size_t j = 0, f = p_successors.Size(); j < f; ++j) {
44 if (p_successors.Get(j) == block) {
45 ++block_count_in_p_successors;
46 }
47 }
48 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000049 AddError(StringPrintf(
50 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
51 "block %d lists %zu occurrences of block %d in its successors.",
52 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
53 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010054 }
55 }
56
57 // Check consistency with respect to successors of `block`.
58 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
59 std::map<HBasicBlock*, size_t> successors_count;
60 for (size_t i = 0, e = successors.Size(); i < e; ++i) {
61 HBasicBlock* s = successors.Get(i);
62 ++successors_count[s];
63 }
64 for (auto& sc : successors_count) {
65 HBasicBlock* s = sc.first;
66 size_t s_count_in_block_successors = sc.second;
67 const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors();
68 size_t block_count_in_s_predecessors = 0;
69 for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) {
70 if (s_predecessors.Get(j) == block) {
71 ++block_count_in_s_predecessors;
72 }
73 }
74 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000075 AddError(StringPrintf(
76 "Block %d lists %zu occurrences of block %d in its successors, whereas "
77 "block %d lists %zu occurrences of block %d in its predecessors.",
78 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
79 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010080 }
81 }
82
83 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000084 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
85 // dead code that falls out of the method will not end with a control-flow
86 // instruction. Such code is removed during the SSA-building DCE phase.
87 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000088 AddError(StringPrintf("Block %d does not end with a branch instruction.",
89 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010090 }
91
92 // Visit this block's list of phis.
93 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +010094 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +010095 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +010096 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000097 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
98 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010099 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100100 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
101 AddError(StringPrintf("The recorded last phi of block %d does not match "
102 "the actual last phi %d.",
103 current_block_->GetBlockId(),
104 current->GetId()));
105 }
106 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100107 }
108
109 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100110 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
111 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100112 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000114 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
115 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100116 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100117 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
118 AddError(StringPrintf("The recorded last instruction of block %d does not match "
119 "the actual last instruction %d.",
120 current_block_->GetBlockId(),
121 current->GetId()));
122 }
123 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100124 }
125}
126
Mark Mendell1152c922015-04-24 17:06:35 -0400127void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
128 if (!GetGraph()->HasBoundsChecks()) {
129 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
130 "but HasBoundsChecks() returns false",
131 check->DebugName(),
132 check->GetId()));
133 }
134
135 // Perform the instruction base checks too.
136 VisitInstruction(check);
137}
138
David Brazdilffee3d32015-07-06 11:48:53 +0100139void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
140 // Ensure that all exception handlers are catch blocks and that handlers
141 // are not listed multiple times.
142 // Note that a normal-flow successor may be a catch block before CFG
143 // simplification. We only test normal-flow successors in SsaChecker.
144 for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) {
145 HBasicBlock* handler = it.Current();
146 if (!handler->IsCatchBlock()) {
147 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
148 "is not a catch block.",
149 current_block_->GetBlockId(),
150 try_boundary->DebugName(),
151 try_boundary->GetId(),
152 handler->GetBlockId()));
153 }
154 if (current_block_->GetSuccessors().Contains(
155 handler, /* start_from */ it.CurrentSuccessorIndex() + 1)) {
156 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
157 handler->GetBlockId(),
158 try_boundary->DebugName(),
159 try_boundary->GetId()));
160 }
161 }
162
163 VisitInstruction(try_boundary);
164}
165
Roland Levillainccc07a92014-09-16 14:48:16 +0100166void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000167 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000168 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
169 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000170 } else {
171 seen_ids_.SetBit(instruction->GetId());
172 }
173
Roland Levillainccc07a92014-09-16 14:48:16 +0100174 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000175 if (instruction->GetBlock() == nullptr) {
176 AddError(StringPrintf("%s %d in block %d not associated with any block.",
177 instruction->IsPhi() ? "Phi" : "Instruction",
178 instruction->GetId(),
179 current_block_->GetBlockId()));
180 } else if (instruction->GetBlock() != current_block_) {
181 AddError(StringPrintf("%s %d in block %d associated with block %d.",
182 instruction->IsPhi() ? "Phi" : "Instruction",
183 instruction->GetId(),
184 current_block_->GetBlockId(),
185 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100186 }
Roland Levillain6b469232014-09-25 10:10:38 +0100187
188 // Ensure the inputs of `instruction` are defined in a block of the graph.
189 for (HInputIterator input_it(instruction); !input_it.Done();
190 input_it.Advance()) {
191 HInstruction* input = input_it.Current();
192 const HInstructionList& list = input->IsPhi()
193 ? input->GetBlock()->GetPhis()
194 : input->GetBlock()->GetInstructions();
195 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000196 AddError(StringPrintf("Input %d of instruction %d is not defined "
197 "in a basic block of the control-flow graph.",
198 input->GetId(),
199 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100200 }
201 }
202
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100203 // Ensure the uses of `instruction` are defined in a block of the graph,
204 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000205 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100206 !use_it.Done(); use_it.Advance()) {
207 HInstruction* use = use_it.Current()->GetUser();
208 const HInstructionList& list = use->IsPhi()
209 ? use->GetBlock()->GetPhis()
210 : use->GetBlock()->GetInstructions();
211 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000212 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000213 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000214 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000215 use->GetId(),
216 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100217 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100218 size_t use_index = use_it.Current()->GetIndex();
219 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
220 AddError(StringPrintf("User %s:%d of instruction %d has a wrong "
221 "UseListNode index.",
222 use->DebugName(),
223 use->GetId(),
224 instruction->GetId()));
225 }
226 }
227
228 // Ensure the environment uses entries are consistent.
229 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
230 !use_it.Done(); use_it.Advance()) {
231 HEnvironment* use = use_it.Current()->GetUser();
232 size_t use_index = use_it.Current()->GetIndex();
233 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
234 AddError(StringPrintf("Environment user of %s:%d has a wrong "
235 "UseListNode index.",
236 instruction->DebugName(),
237 instruction->GetId()));
238 }
Roland Levillain6b469232014-09-25 10:10:38 +0100239 }
David Brazdil1abb4192015-02-17 18:33:36 +0000240
241 // Ensure 'instruction' has pointers to its inputs' use entries.
242 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
243 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
244 HInstruction* input = input_record.GetInstruction();
245 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100246 size_t use_index = use_node->GetIndex();
247 if ((use_node == nullptr)
248 || !input->GetUses().Contains(use_node)
249 || (use_index >= e)
250 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000251 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
252 "at input %u (%s:%d).",
253 instruction->DebugName(),
254 instruction->GetId(),
255 static_cast<unsigned>(i),
256 input->DebugName(),
257 input->GetId()));
258 }
259 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100260}
261
Roland Levillain4c0eb422015-04-24 16:43:49 +0100262void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
263 VisitInstruction(invoke);
264
265 if (invoke->IsStaticWithExplicitClinitCheck()) {
266 size_t last_input_index = invoke->InputCount() - 1;
267 HInstruction* last_input = invoke->InputAt(last_input_index);
268 if (last_input == nullptr) {
269 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
270 "has a null pointer as last input.",
271 invoke->DebugName(),
272 invoke->GetId()));
273 }
274 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
275 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
276 "has a last instruction (%s:%d) which is neither a clinit check "
277 "nor a load class instruction.",
278 invoke->DebugName(),
279 invoke->GetId(),
280 last_input->DebugName(),
281 last_input->GetId()));
282 }
283 }
284}
285
David Brazdilfc6a86a2015-06-26 10:33:45 +0000286void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100287 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000288 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
289 AddError(StringPrintf("%s:%d does not jump to the exit block.",
290 ret->DebugName(),
291 ret->GetId()));
292 }
293}
294
295void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100296 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000297 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
298 AddError(StringPrintf("%s:%d does not jump to the exit block.",
299 ret->DebugName(),
300 ret->GetId()));
301 }
302}
303
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100304void GraphChecker::VisitCheckCast(HCheckCast* check) {
305 VisitInstruction(check);
306 HInstruction* input = check->InputAt(1);
307 if (!input->IsLoadClass()) {
308 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
309 check->DebugName(),
310 check->GetId(),
311 input->DebugName(),
312 input->GetId()));
313 }
314}
315
316void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
317 VisitInstruction(instruction);
318 HInstruction* input = instruction->InputAt(1);
319 if (!input->IsLoadClass()) {
320 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
321 instruction->DebugName(),
322 instruction->GetId(),
323 input->DebugName(),
324 input->GetId()));
325 }
326}
327
Roland Levillainccc07a92014-09-16 14:48:16 +0100328void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
329 super_type::VisitBasicBlock(block);
330
David Brazdilffee3d32015-07-06 11:48:53 +0100331 // Ensure that catch blocks are not normal successors, and normal blocks are
332 // never exceptional successors.
333 const size_t num_normal_successors = block->NumberOfNormalSuccessors();
334 for (size_t j = 0; j < num_normal_successors; ++j) {
335 HBasicBlock* successor = block->GetSuccessors().Get(j);
336 if (successor->IsCatchBlock()) {
337 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
338 successor->GetBlockId(),
339 block->GetBlockId()));
340 }
341 }
342 for (size_t j = num_normal_successors, e = block->GetSuccessors().Size(); j < e; ++j) {
343 HBasicBlock* successor = block->GetSuccessors().Get(j);
344 if (!successor->IsCatchBlock()) {
345 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
346 successor->GetBlockId(),
347 block->GetBlockId()));
348 }
349 }
350
Roland Levillainccc07a92014-09-16 14:48:16 +0100351 // Ensure there is no critical edge (i.e., an edge connecting a
352 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100353 // predecessors). Exceptional edges are synthesized and hence
354 // not accounted for.
355 if (block->NumberOfNormalSuccessors() > 1) {
356 for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) {
Roland Levillainccc07a92014-09-16 14:48:16 +0100357 HBasicBlock* successor = block->GetSuccessors().Get(j);
358 if (successor->GetPredecessors().Size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000359 AddError(StringPrintf("Critical edge between blocks %d and %d.",
360 block->GetBlockId(),
361 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100362 }
363 }
364 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100365
Calin Juravle1d8b49f2015-05-12 12:40:07 +0000366 // Check Phi uniqueness (no two Phis with the same type refer to the same register).
Calin Juravlea4f88312015-04-16 12:57:19 +0100367 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
368 HPhi* phi = it.Current()->AsPhi();
369 if (phi->GetNextEquivalentPhiWithSameType() != nullptr) {
370 std::stringstream type_str;
371 type_str << phi->GetType();
372 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s",
373 phi->GetId(), phi->GetRegNumber(), type_str.str().c_str()));
374 }
375 }
376
David Brazdilffee3d32015-07-06 11:48:53 +0100377 // Ensure try membership information is consistent.
378 HTryBoundary* try_entry = block->GetTryEntry();
379 if (block->IsCatchBlock()) {
380 if (try_entry != nullptr) {
381 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
382 "has try entry %s:%d.",
383 block->GetBlockId(),
384 try_entry->DebugName(),
385 try_entry->GetId()));
386 }
387
388 if (block->IsLoopHeader()) {
389 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
390 block->GetBlockId()));
391 }
392 } else {
393 for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) {
394 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
395 HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
396 if (try_entry == nullptr) {
397 if (incoming_try_entry != nullptr) {
398 AddError(StringPrintf("Block %d has no try entry but try entry %s:%d follows "
399 "from predecessor %d.",
400 block->GetBlockId(),
401 incoming_try_entry->DebugName(),
402 incoming_try_entry->GetId(),
403 predecessor->GetBlockId()));
404 }
405 } else if (incoming_try_entry == nullptr) {
406 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
407 "from predecessor %d.",
408 block->GetBlockId(),
409 try_entry->DebugName(),
410 try_entry->GetId(),
411 predecessor->GetBlockId()));
412 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(*try_entry)) {
413 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
414 "with %s:%d that follows from predecessor %d.",
415 block->GetBlockId(),
416 try_entry->DebugName(),
417 try_entry->GetId(),
418 incoming_try_entry->DebugName(),
419 incoming_try_entry->GetId(),
420 predecessor->GetBlockId()));
421 }
422 }
423 }
424
Roland Levillain6b879dd2014-09-22 17:13:44 +0100425 if (block->IsLoopHeader()) {
426 CheckLoop(block);
427 }
428}
429
430void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
431 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100432 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100433
434 // Ensure the pre-header block is first in the list of
435 // predecessors of a loop header.
436 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000437 AddError(StringPrintf(
438 "Loop pre-header is not the first predecessor of the loop header %d.",
439 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100440 }
441
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100442 // Ensure the loop header has only one incoming branch and the remaining
443 // predecessors are back edges.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000444 size_t num_preds = loop_header->GetPredecessors().Size();
445 if (num_preds < 2) {
446 AddError(StringPrintf(
447 "Loop header %d has less than two predecessors: %zu.",
448 id,
449 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100450 } else {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100451 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000452 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000453 AddError(StringPrintf(
454 "First predecessor of loop header %d is a back edge.",
455 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100456 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100457 for (size_t i = 1, e = loop_header->GetPredecessors().Size(); i < e; ++i) {
458 HBasicBlock* predecessor = loop_header->GetPredecessors().Get(i);
459 if (!loop_information->IsBackEdge(*predecessor)) {
460 AddError(StringPrintf(
461 "Loop header %d has multiple incoming (non back edge) blocks.",
462 id));
463 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100464 }
465 }
466
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100467 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100468
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100469 // Ensure back edges belong to the loop.
470 size_t num_back_edges = loop_information->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000471 if (num_back_edges == 0) {
472 AddError(StringPrintf(
473 "Loop defined by header %d has no back edge.",
474 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100475 } else {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100476 for (size_t i = 0; i < num_back_edges; ++i) {
477 int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId();
478 if (!loop_blocks.IsBitSet(back_edge_id)) {
479 AddError(StringPrintf(
480 "Loop defined by header %d has an invalid back edge %d.",
481 id,
482 back_edge_id));
483 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100484 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100485 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100486
David Brazdil2d7352b2015-04-20 14:52:42 +0100487 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100488 for (uint32_t i : loop_blocks.Indexes()) {
489 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100490 if (loop_block == nullptr) {
491 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
492 id,
493 i));
494 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000495 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100496 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000497 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100498 }
499 }
David Brazdil7d275372015-04-21 16:36:35 +0100500
501 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
502 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
503 HLoopInformation* outer_info = it.Current();
504 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
505 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
506 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100507 id,
David Brazdil7d275372015-04-21 16:36:35 +0100508 outer_info->GetHeader()->GetBlockId()));
509 }
510 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100511}
512
513void SSAChecker::VisitInstruction(HInstruction* instruction) {
514 super_type::VisitInstruction(instruction);
515
Roland Levillaina8069ce2014-10-01 10:48:29 +0100516 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000517 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100518 !use_it.Done(); use_it.Advance()) {
519 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100520 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000521 AddError(StringPrintf("Instruction %d in block %d does not dominate "
522 "use %d in block %d.",
523 instruction->GetId(), current_block_->GetBlockId(),
524 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100525 }
526 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100527
528 // Ensure an instruction having an environment is dominated by the
529 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100530 for (HEnvironment* environment = instruction->GetEnvironment();
531 environment != nullptr;
532 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100533 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
534 HInstruction* env_instruction = environment->GetInstructionAt(i);
535 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100536 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000537 AddError(StringPrintf("Instruction %d in environment of instruction %d "
538 "from block %d does not dominate instruction %d.",
539 env_instruction->GetId(),
540 instruction->GetId(),
541 current_block_->GetBlockId(),
542 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100543 }
544 }
545 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100546}
547
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548static Primitive::Type PrimitiveKind(Primitive::Type type) {
549 switch (type) {
550 case Primitive::kPrimBoolean:
551 case Primitive::kPrimByte:
552 case Primitive::kPrimShort:
553 case Primitive::kPrimChar:
554 case Primitive::kPrimInt:
555 return Primitive::kPrimInt;
556 default:
557 return type;
558 }
559}
560
Roland Levillain6b879dd2014-09-22 17:13:44 +0100561void SSAChecker::VisitPhi(HPhi* phi) {
562 VisitInstruction(phi);
563
564 // Ensure the first input of a phi is not itself.
565 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000566 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
567 phi->GetId(),
568 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100569 }
570
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000571 // Ensure that the inputs have the same primitive kind as the phi.
572 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
573 HInstruction* input = phi->InputAt(i);
574 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
575 AddError(StringPrintf(
576 "Input %d at index %zu of phi %d from block %d does not have the "
577 "same type as the phi: %s versus %s",
578 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
579 Primitive::PrettyDescriptor(input->GetType()),
580 Primitive::PrettyDescriptor(phi->GetType())));
581 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000582 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000583 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
584 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
585 phi->GetId(),
586 phi->GetBlock()->GetBlockId(),
587 Primitive::PrettyDescriptor(phi->GetType())));
588 }
David Brazdilffee3d32015-07-06 11:48:53 +0100589
590 if (phi->IsCatchPhi()) {
591 // The number of inputs of a catch phi corresponds to the total number of
592 // throwing instructions caught by this catch block.
593 } else {
594 // Ensure the number of inputs of a non-catch phi is the same as the number
595 // of its predecessors.
596 const GrowableArray<HBasicBlock*>& predecessors =
597 phi->GetBlock()->GetPredecessors();
598 if (phi->InputCount() != predecessors.Size()) {
599 AddError(StringPrintf(
600 "Phi %d in block %d has %zu inputs, "
601 "but block %d has %zu predecessors.",
602 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
603 phi->GetBlock()->GetBlockId(), predecessors.Size()));
604 } else {
605 // Ensure phi input at index I either comes from the Ith
606 // predecessor or from a block that dominates this predecessor.
607 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
608 HInstruction* input = phi->InputAt(i);
609 HBasicBlock* predecessor = predecessors.Get(i);
610 if (!(input->GetBlock() == predecessor
611 || input->GetBlock()->Dominates(predecessor))) {
612 AddError(StringPrintf(
613 "Input %d at index %zu of phi %d from block %d is not defined in "
614 "predecessor number %zu nor in a block dominating it.",
615 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
616 i));
617 }
618 }
619 }
620 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000621}
622
David Brazdil13b47182015-04-15 16:29:32 +0100623void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
624 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000625 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100626 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000627 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000628 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100629 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
630 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000631 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100632 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000633 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000634 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100635 } else if (input->GetType() == Primitive::kPrimInt
636 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
637 // TODO: We need a data-flow analysis to determine if the Phi or
638 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100639 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000640 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100641 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
642 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000643 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100644 static_cast<int>(input_index),
645 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000646 }
647}
648
David Brazdil13b47182015-04-15 16:29:32 +0100649void SSAChecker::VisitIf(HIf* instruction) {
650 VisitInstruction(instruction);
651 HandleBooleanInput(instruction, 0);
652}
653
654void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
655 VisitInstruction(instruction);
656 HandleBooleanInput(instruction, 0);
657}
658
Nicolas Geoffray31596742014-11-24 15:28:45 +0000659void SSAChecker::VisitCondition(HCondition* op) {
660 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000661 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000662 AddError(StringPrintf(
663 "Condition %s %d has a non-Boolean result type: %s.",
664 op->DebugName(), op->GetId(),
665 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000666 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000667 HInstruction* lhs = op->InputAt(0);
668 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100669 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
670 AddError(StringPrintf(
671 "Condition %s %d has inputs of different types: %s, and %s.",
672 op->DebugName(), op->GetId(),
673 Primitive::PrettyDescriptor(lhs->GetType()),
674 Primitive::PrettyDescriptor(rhs->GetType())));
675 }
676 if (!op->IsEqual() && !op->IsNotEqual()) {
677 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000678 AddError(StringPrintf(
679 "Condition %s %d uses an object as left-hand side input.",
680 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100681 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000682 AddError(StringPrintf(
683 "Condition %s %d uses an object as right-hand side input.",
684 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000685 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000686 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000687}
688
689void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
690 VisitInstruction(op);
691 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
692 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000693 AddError(StringPrintf(
694 "Shift operation %s %d has a non-int kind second input: "
695 "%s of type %s.",
696 op->DebugName(), op->GetId(),
697 op->InputAt(1)->DebugName(),
698 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000699 }
700 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100701 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000702 AddError(StringPrintf(
703 "Binary operation %s %d has inputs of different types: "
704 "%s, and %s.",
705 op->DebugName(), op->GetId(),
706 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
707 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000708 }
709 }
710
711 if (op->IsCompare()) {
712 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000713 AddError(StringPrintf(
714 "Compare operation %d has a non-int result type: %s.",
715 op->GetId(),
716 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000717 }
718 } else {
719 // Use the first input, so that we can also make this check for shift operations.
720 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000721 AddError(StringPrintf(
722 "Binary operation %s %d has a result type different "
723 "from its input type: %s vs %s.",
724 op->DebugName(), op->GetId(),
725 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100726 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000727 }
728 }
729}
730
David Brazdil8d5b8b22015-03-24 10:51:52 +0000731void SSAChecker::VisitConstant(HConstant* instruction) {
732 HBasicBlock* block = instruction->GetBlock();
733 if (!block->IsEntryBlock()) {
734 AddError(StringPrintf(
735 "%s %d should be in the entry block but is in block %d.",
736 instruction->DebugName(),
737 instruction->GetId(),
738 block->GetBlockId()));
739 }
740}
741
Roland Levillainccc07a92014-09-16 14:48:16 +0100742} // namespace art