blob: 4a49c836114608d914aec5bedb9f2b86e23d7561 [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
Vladimir Marko655e5852015-10-12 10:38:28 +010019#include <algorithm>
Roland Levillainccc07a92014-09-16 14:48:16 +010020#include <map>
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000021#include <string>
Calin Juravlea4f88312015-04-16 12:57:19 +010022#include <sstream>
Roland Levillainccc07a92014-09-16 14:48:16 +010023
Vladimir Marko655e5852015-10-12 10:38:28 +010024#include "base/arena_containers.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010025#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000026#include "base/stringprintf.h"
David Brazdild9510df2015-11-04 23:30:22 +000027#include "handle_scope-inl.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010028
Roland Levillainccc07a92014-09-16 14:48:16 +010029namespace art {
30
31void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
32 current_block_ = block;
33
34 // Check consistency with respect to predecessors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010035 ArenaSafeMap<HBasicBlock*, size_t> predecessors_count(
36 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000037 for (HBasicBlock* p : block->GetPredecessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010038 auto it = predecessors_count.find(p);
39 if (it != predecessors_count.end()) {
40 ++it->second;
41 } else {
42 predecessors_count.Put(p, 1u);
43 }
Roland Levillainccc07a92014-09-16 14:48:16 +010044 }
45 for (auto& pc : predecessors_count) {
46 HBasicBlock* p = pc.first;
47 size_t p_count_in_block_predecessors = pc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010048 size_t block_count_in_p_successors =
49 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010050 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000051 AddError(StringPrintf(
52 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
53 "block %d lists %zu occurrences of block %d in its successors.",
54 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
55 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010056 }
57 }
58
59 // Check consistency with respect to successors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010060 ArenaSafeMap<HBasicBlock*, size_t> successors_count(
61 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000062 for (HBasicBlock* s : block->GetSuccessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010063 auto it = successors_count.find(s);
64 if (it != successors_count.end()) {
65 ++it->second;
66 } else {
67 successors_count.Put(s, 1u);
68 }
Roland Levillainccc07a92014-09-16 14:48:16 +010069 }
70 for (auto& sc : successors_count) {
71 HBasicBlock* s = sc.first;
72 size_t s_count_in_block_successors = sc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010073 size_t block_count_in_s_predecessors =
74 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010075 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000076 AddError(StringPrintf(
77 "Block %d lists %zu occurrences of block %d in its successors, whereas "
78 "block %d lists %zu occurrences of block %d in its predecessors.",
79 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
80 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010081 }
82 }
83
84 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000085 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
86 // dead code that falls out of the method will not end with a control-flow
87 // instruction. Such code is removed during the SSA-building DCE phase.
88 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000089 AddError(StringPrintf("Block %d does not end with a branch instruction.",
90 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010091 }
92
David Brazdil29fc0082015-08-18 17:17:38 +010093 // Ensure that only Return(Void) and Throw jump to Exit. An exiting
David Brazdilb618ade2015-07-29 10:31:29 +010094 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
95 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +000096 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilb618ade2015-07-29 10:31:29 +010097 if (predecessor->IsSingleTryBoundary()
98 && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) {
99 HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor();
100 HInstruction* last_instruction = real_predecessor->GetLastInstruction();
101 if (!last_instruction->IsThrow()) {
102 AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.",
103 last_instruction->DebugName(),
104 last_instruction->GetId()));
105 }
106 } else {
107 HInstruction* last_instruction = predecessor->GetLastInstruction();
108 if (!last_instruction->IsReturn()
109 && !last_instruction->IsReturnVoid()
110 && !last_instruction->IsThrow()) {
111 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
112 last_instruction->DebugName(),
113 last_instruction->GetId()));
114 }
115 }
116 }
117 }
118
Roland Levillainccc07a92014-09-16 14:48:16 +0100119 // Visit this block's list of phis.
120 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100121 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100123 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000124 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
125 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100126 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100127 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
128 AddError(StringPrintf("The recorded last phi of block %d does not match "
129 "the actual last phi %d.",
130 current_block_->GetBlockId(),
131 current->GetId()));
132 }
133 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100134 }
135
136 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100137 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
138 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100139 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000141 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
142 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100143 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
145 AddError(StringPrintf("The recorded last instruction of block %d does not match "
146 "the actual last instruction %d.",
147 current_block_->GetBlockId(),
148 current->GetId()));
149 }
150 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 }
David Brazdilbadd8262016-02-02 16:28:56 +0000152
153 // Ensure that catch blocks are not normal successors, and normal blocks are
154 // never exceptional successors.
155 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
156 if (successor->IsCatchBlock()) {
157 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
158 successor->GetBlockId(),
159 block->GetBlockId()));
160 }
161 }
162 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
163 if (!successor->IsCatchBlock()) {
164 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
165 successor->GetBlockId(),
166 block->GetBlockId()));
167 }
168 }
169
170 // Ensure dominated blocks have `block` as the dominator.
171 for (HBasicBlock* dominated : block->GetDominatedBlocks()) {
172 if (dominated->GetDominator() != block) {
173 AddError(StringPrintf("Block %d should be the dominator of %d.",
174 block->GetBlockId(),
175 dominated->GetBlockId()));
176 }
177 }
178
179 // Ensure there is no critical edge (i.e., an edge connecting a
180 // block with multiple successors to a block with multiple
181 // predecessors). Exceptional edges are synthesized and hence
182 // not accounted for.
183 if (block->GetSuccessors().size() > 1) {
184 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
185 if (successor->IsExitBlock() &&
186 block->IsSingleTryBoundary() &&
187 block->GetPredecessors().size() == 1u &&
188 block->GetSinglePredecessor()->GetLastInstruction()->IsThrow()) {
189 // Allowed critical edge Throw->TryBoundary->Exit.
190 } else if (successor->GetPredecessors().size() > 1) {
191 AddError(StringPrintf("Critical edge between blocks %d and %d.",
192 block->GetBlockId(),
193 successor->GetBlockId()));
194 }
195 }
196 }
197
198 // Ensure try membership information is consistent.
199 if (block->IsCatchBlock()) {
200 if (block->IsTryBlock()) {
201 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
202 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
203 "has try entry %s:%d.",
204 block->GetBlockId(),
205 try_entry.DebugName(),
206 try_entry.GetId()));
207 }
208
209 if (block->IsLoopHeader()) {
210 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
211 block->GetBlockId()));
212 }
213 } else {
214 for (HBasicBlock* predecessor : block->GetPredecessors()) {
215 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
216 if (block->IsTryBlock()) {
217 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
218 if (incoming_try_entry == nullptr) {
219 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
220 "from predecessor %d.",
221 block->GetBlockId(),
222 stored_try_entry.DebugName(),
223 stored_try_entry.GetId(),
224 predecessor->GetBlockId()));
225 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
226 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
227 "with %s:%d that follows from predecessor %d.",
228 block->GetBlockId(),
229 stored_try_entry.DebugName(),
230 stored_try_entry.GetId(),
231 incoming_try_entry->DebugName(),
232 incoming_try_entry->GetId(),
233 predecessor->GetBlockId()));
234 }
235 } else if (incoming_try_entry != nullptr) {
236 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
237 "from predecessor %d.",
238 block->GetBlockId(),
239 incoming_try_entry->DebugName(),
240 incoming_try_entry->GetId(),
241 predecessor->GetBlockId()));
242 }
243 }
244 }
245
246 if (block->IsLoopHeader()) {
247 HandleLoop(block);
248 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100249}
250
Mark Mendell1152c922015-04-24 17:06:35 -0400251void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
252 if (!GetGraph()->HasBoundsChecks()) {
253 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
254 "but HasBoundsChecks() returns false",
255 check->DebugName(),
256 check->GetId()));
257 }
258
259 // Perform the instruction base checks too.
260 VisitInstruction(check);
261}
262
David Brazdilffee3d32015-07-06 11:48:53 +0100263void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000264 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
265
266 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100267 // Note that a normal-flow successor may be a catch block before CFG
David Brazdilbadd8262016-02-02 16:28:56 +0000268 // simplification. We only test normal-flow successors in GraphChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000269 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100270 if (!handler->IsCatchBlock()) {
271 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
272 "is not a catch block.",
273 current_block_->GetBlockId(),
274 try_boundary->DebugName(),
275 try_boundary->GetId(),
276 handler->GetBlockId()));
277 }
David Brazdild26a4112015-11-10 11:07:31 +0000278 }
279
280 // Ensure that handlers are not listed multiple times.
281 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000282 if (ContainsElement(handlers, handlers[i], i + 1)) {
283 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000284 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100285 try_boundary->DebugName(),
286 try_boundary->GetId()));
287 }
288 }
289
290 VisitInstruction(try_boundary);
291}
292
David Brazdil9bc43612015-11-05 21:25:24 +0000293void GraphChecker::VisitLoadException(HLoadException* load) {
294 // Ensure that LoadException is the first instruction in a catch block.
295 if (!load->GetBlock()->IsCatchBlock()) {
296 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
297 load->DebugName(),
298 load->GetId(),
299 load->GetBlock()->GetBlockId()));
300 } else if (load->GetBlock()->GetFirstInstruction() != load) {
301 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
302 load->DebugName(),
303 load->GetId(),
304 load->GetBlock()->GetBlockId()));
305 }
306}
307
Roland Levillainccc07a92014-09-16 14:48:16 +0100308void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000309 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000310 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
311 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000312 } else {
313 seen_ids_.SetBit(instruction->GetId());
314 }
315
Roland Levillainccc07a92014-09-16 14:48:16 +0100316 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000317 if (instruction->GetBlock() == nullptr) {
318 AddError(StringPrintf("%s %d in block %d not associated with any block.",
319 instruction->IsPhi() ? "Phi" : "Instruction",
320 instruction->GetId(),
321 current_block_->GetBlockId()));
322 } else if (instruction->GetBlock() != current_block_) {
323 AddError(StringPrintf("%s %d in block %d associated with block %d.",
324 instruction->IsPhi() ? "Phi" : "Instruction",
325 instruction->GetId(),
326 current_block_->GetBlockId(),
327 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100328 }
Roland Levillain6b469232014-09-25 10:10:38 +0100329
330 // Ensure the inputs of `instruction` are defined in a block of the graph.
331 for (HInputIterator input_it(instruction); !input_it.Done();
332 input_it.Advance()) {
333 HInstruction* input = input_it.Current();
334 const HInstructionList& list = input->IsPhi()
335 ? input->GetBlock()->GetPhis()
336 : input->GetBlock()->GetInstructions();
337 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000338 AddError(StringPrintf("Input %d of instruction %d is not defined "
339 "in a basic block of the control-flow graph.",
340 input->GetId(),
341 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100342 }
343 }
344
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100345 // Ensure the uses of `instruction` are defined in a block of the graph,
346 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000347 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100348 !use_it.Done(); use_it.Advance()) {
349 HInstruction* use = use_it.Current()->GetUser();
350 const HInstructionList& list = use->IsPhi()
351 ? use->GetBlock()->GetPhis()
352 : use->GetBlock()->GetInstructions();
353 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000354 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000355 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000356 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000357 use->GetId(),
358 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100359 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100360 size_t use_index = use_it.Current()->GetIndex();
361 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000362 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100363 "UseListNode index.",
364 use->DebugName(),
365 use->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000366 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100367 instruction->GetId()));
368 }
369 }
370
371 // Ensure the environment uses entries are consistent.
372 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
373 !use_it.Done(); use_it.Advance()) {
374 HEnvironment* use = use_it.Current()->GetUser();
375 size_t use_index = use_it.Current()->GetIndex();
376 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
377 AddError(StringPrintf("Environment user of %s:%d has a wrong "
378 "UseListNode index.",
379 instruction->DebugName(),
380 instruction->GetId()));
381 }
Roland Levillain6b469232014-09-25 10:10:38 +0100382 }
David Brazdil1abb4192015-02-17 18:33:36 +0000383
384 // Ensure 'instruction' has pointers to its inputs' use entries.
385 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
386 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
387 HInstruction* input = input_record.GetInstruction();
388 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100389 size_t use_index = use_node->GetIndex();
390 if ((use_node == nullptr)
391 || !input->GetUses().Contains(use_node)
392 || (use_index >= e)
393 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000394 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
395 "at input %u (%s:%d).",
396 instruction->DebugName(),
397 instruction->GetId(),
398 static_cast<unsigned>(i),
399 input->DebugName(),
400 input->GetId()));
401 }
402 }
David Brazdilbadd8262016-02-02 16:28:56 +0000403
404 // Ensure an instruction dominates all its uses.
405 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
406 !use_it.Done(); use_it.Advance()) {
407 HInstruction* use = use_it.Current()->GetUser();
408 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
409 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
410 "use %s:%d in block %d.",
411 instruction->DebugName(),
412 instruction->GetId(),
413 current_block_->GetBlockId(),
414 use->DebugName(),
415 use->GetId(),
416 use->GetBlock()->GetBlockId()));
417 }
418 }
419
420 if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) {
421 AddError(StringPrintf("Instruction %s:%d in block %d requires an environment "
422 "but does not have one.",
423 instruction->DebugName(),
424 instruction->GetId(),
425 current_block_->GetBlockId()));
426 }
427
428 // Ensure an instruction having an environment is dominated by the
429 // instructions contained in the environment.
430 for (HEnvironment* environment = instruction->GetEnvironment();
431 environment != nullptr;
432 environment = environment->GetParent()) {
433 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
434 HInstruction* env_instruction = environment->GetInstructionAt(i);
435 if (env_instruction != nullptr
436 && !env_instruction->StrictlyDominates(instruction)) {
437 AddError(StringPrintf("Instruction %d in environment of instruction %d "
438 "from block %d does not dominate instruction %d.",
439 env_instruction->GetId(),
440 instruction->GetId(),
441 current_block_->GetBlockId(),
442 instruction->GetId()));
443 }
444 }
445 }
446
447 // Ensure that reference type instructions have reference type info.
448 if (instruction->GetType() == Primitive::kPrimNot) {
449 ScopedObjectAccess soa(Thread::Current());
450 if (!instruction->GetReferenceTypeInfo().IsValid()) {
451 AddError(StringPrintf("Reference type instruction %s:%d does not have "
452 "valid reference type information.",
453 instruction->DebugName(),
454 instruction->GetId()));
455 }
456 }
457
458 if (instruction->CanThrowIntoCatchBlock()) {
459 // Find the top-level environment. This corresponds to the environment of
460 // the catch block since we do not inline methods with try/catch.
461 HEnvironment* environment = instruction->GetEnvironment();
462 while (environment->GetParent() != nullptr) {
463 environment = environment->GetParent();
464 }
465
466 // Find all catch blocks and test that `instruction` has an environment
467 // value for each one.
468 const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
469 for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) {
470 for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
471 HPhi* catch_phi = phi_it.Current()->AsPhi();
472 if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) {
473 AddError(StringPrintf("Instruction %s:%d throws into catch block %d "
474 "with catch phi %d for vreg %d but its "
475 "corresponding environment slot is empty.",
476 instruction->DebugName(),
477 instruction->GetId(),
478 catch_block->GetBlockId(),
479 catch_phi->GetId(),
480 catch_phi->GetRegNumber()));
481 }
482 }
483 }
484 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100485}
486
Roland Levillain4c0eb422015-04-24 16:43:49 +0100487void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
488 VisitInstruction(invoke);
489
490 if (invoke->IsStaticWithExplicitClinitCheck()) {
491 size_t last_input_index = invoke->InputCount() - 1;
492 HInstruction* last_input = invoke->InputAt(last_input_index);
493 if (last_input == nullptr) {
494 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
495 "has a null pointer as last input.",
496 invoke->DebugName(),
497 invoke->GetId()));
498 }
499 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
500 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
501 "has a last instruction (%s:%d) which is neither a clinit check "
502 "nor a load class instruction.",
503 invoke->DebugName(),
504 invoke->GetId(),
505 last_input->DebugName(),
506 last_input->GetId()));
507 }
508 }
509}
510
David Brazdilfc6a86a2015-06-26 10:33:45 +0000511void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100512 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000513 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
514 AddError(StringPrintf("%s:%d does not jump to the exit block.",
515 ret->DebugName(),
516 ret->GetId()));
517 }
518}
519
520void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100521 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
523 AddError(StringPrintf("%s:%d does not jump to the exit block.",
524 ret->DebugName(),
525 ret->GetId()));
526 }
527}
528
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100529void GraphChecker::VisitCheckCast(HCheckCast* check) {
530 VisitInstruction(check);
531 HInstruction* input = check->InputAt(1);
532 if (!input->IsLoadClass()) {
533 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
534 check->DebugName(),
535 check->GetId(),
536 input->DebugName(),
537 input->GetId()));
538 }
539}
540
541void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
542 VisitInstruction(instruction);
543 HInstruction* input = instruction->InputAt(1);
544 if (!input->IsLoadClass()) {
545 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
546 instruction->DebugName(),
547 instruction->GetId(),
548 input->DebugName(),
549 input->GetId()));
550 }
551}
552
David Brazdilbadd8262016-02-02 16:28:56 +0000553void GraphChecker::HandleLoop(HBasicBlock* loop_header) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100554 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100555 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100556
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000557 if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
David Brazdildb51efb2015-11-06 01:36:20 +0000558 AddError(StringPrintf(
559 "Loop pre-header %d of loop defined by header %d has %zu successors.",
560 loop_information->GetPreHeader()->GetBlockId(),
561 id,
562 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100563 }
564
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000565 if (loop_information->GetSuspendCheck() == nullptr) {
566 AddError(StringPrintf(
567 "Loop with header %d does not have a suspend check.",
568 loop_header->GetBlockId()));
569 }
570
571 if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) {
572 AddError(StringPrintf(
573 "Loop header %d does not have the loop suspend check as the first instruction.",
574 loop_header->GetBlockId()));
575 }
576
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100577 // Ensure the loop header has only one incoming branch and the remaining
578 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000579 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000580 if (num_preds < 2) {
581 AddError(StringPrintf(
582 "Loop header %d has less than two predecessors: %zu.",
583 id,
584 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100585 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100586 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000587 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000588 AddError(StringPrintf(
589 "First predecessor of loop header %d is a back edge.",
590 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100591 }
Vladimir Marko60584552015-09-03 13:35:12 +0000592 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100593 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100594 if (!loop_information->IsBackEdge(*predecessor)) {
595 AddError(StringPrintf(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000596 "Loop header %d has multiple incoming (non back edge) blocks: %d.",
597 id,
598 predecessor->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100599 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100600 }
601 }
602
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100603 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100604
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100605 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100606 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000607 AddError(StringPrintf(
608 "Loop defined by header %d has no back edge.",
609 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100610 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100611 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
612 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100613 if (!loop_blocks.IsBitSet(back_edge_id)) {
614 AddError(StringPrintf(
615 "Loop defined by header %d has an invalid back edge %d.",
616 id,
617 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000618 } else if (back_edge->GetLoopInformation() != loop_information) {
619 AddError(StringPrintf(
620 "Back edge %d of loop defined by header %d belongs to nested loop "
621 "with header %d.",
622 back_edge_id,
623 id,
624 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100625 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100626 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100627 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100628
David Brazdil7d275372015-04-21 16:36:35 +0100629 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
630 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
631 HLoopInformation* outer_info = it.Current();
632 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
633 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
634 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100635 id,
David Brazdil7d275372015-04-21 16:36:35 +0100636 outer_info->GetHeader()->GetBlockId()));
637 }
638 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000639
640 // Ensure the pre-header block is first in the list of predecessors of a loop
641 // header and that the header block is its only successor.
642 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
643 AddError(StringPrintf(
644 "Loop pre-header is not the first predecessor of the loop header %d.",
645 id));
646 }
647
648 // Ensure all blocks in the loop are live and dominated by the loop header in
649 // the case of natural loops.
650 for (uint32_t i : loop_blocks.Indexes()) {
651 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
652 if (loop_block == nullptr) {
653 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
654 id,
655 i));
656 } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) {
657 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
658 i,
659 id));
660 }
661 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100662}
663
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000664static Primitive::Type PrimitiveKind(Primitive::Type type) {
665 switch (type) {
666 case Primitive::kPrimBoolean:
667 case Primitive::kPrimByte:
668 case Primitive::kPrimShort:
669 case Primitive::kPrimChar:
670 case Primitive::kPrimInt:
671 return Primitive::kPrimInt;
672 default:
673 return type;
674 }
675}
676
David Brazdil77a48ae2015-09-15 12:34:04 +0000677static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
678 return insn1->IsConstant()
679 && insn2->IsConstant()
680 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
681}
682
683static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
684 if (insn1->IsPhi() &&
685 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
686 insn1->InputCount() == insn2->InputCount()) {
687 // Testing only one of the two inputs for recursion is sufficient.
688 if (visited->IsBitSet(insn1->GetId())) {
689 return true;
690 }
691 visited->SetBit(insn1->GetId());
692
693 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
694 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
695 return false;
696 }
697 }
698 return true;
699 } else if (IsSameSizeConstant(insn1, insn2)) {
700 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
701 } else {
702 return false;
703 }
704}
705
David Brazdilbadd8262016-02-02 16:28:56 +0000706void GraphChecker::VisitPhi(HPhi* phi) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100707 VisitInstruction(phi);
708
709 // Ensure the first input of a phi is not itself.
710 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000711 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
712 phi->GetId(),
713 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100714 }
715
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000716 // Ensure that the inputs have the same primitive kind as the phi.
717 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
718 HInstruction* input = phi->InputAt(i);
719 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
720 AddError(StringPrintf(
721 "Input %d at index %zu of phi %d from block %d does not have the "
722 "same type as the phi: %s versus %s",
723 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
724 Primitive::PrettyDescriptor(input->GetType()),
725 Primitive::PrettyDescriptor(phi->GetType())));
726 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000727 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000728 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
729 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
730 phi->GetId(),
731 phi->GetBlock()->GetBlockId(),
732 Primitive::PrettyDescriptor(phi->GetType())));
733 }
David Brazdilffee3d32015-07-06 11:48:53 +0100734
735 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100736 // The number of inputs of a catch phi should be the total number of throwing
737 // instructions caught by this catch block. We do not enforce this, however,
738 // because we do not remove the corresponding inputs when we prove that an
739 // instruction cannot throw. Instead, we at least test that all phis have the
740 // same, non-zero number of inputs (b/24054676).
741 size_t input_count_this = phi->InputCount();
742 if (input_count_this == 0u) {
743 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
744 phi->GetId(),
745 phi->GetBlock()->GetBlockId()));
746 } else {
747 HInstruction* next_phi = phi->GetNext();
748 if (next_phi != nullptr) {
749 size_t input_count_next = next_phi->InputCount();
750 if (input_count_this != input_count_next) {
751 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
752 "but phi %d has %zu inputs.",
753 phi->GetId(),
754 phi->GetBlock()->GetBlockId(),
755 input_count_this,
756 next_phi->GetId(),
757 input_count_next));
758 }
759 }
760 }
David Brazdilffee3d32015-07-06 11:48:53 +0100761 } else {
762 // Ensure the number of inputs of a non-catch phi is the same as the number
763 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000764 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
765 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100766 AddError(StringPrintf(
767 "Phi %d in block %d has %zu inputs, "
768 "but block %d has %zu predecessors.",
769 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100771 } else {
772 // Ensure phi input at index I either comes from the Ith
773 // predecessor or from a block that dominates this predecessor.
774 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
775 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000776 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100777 if (!(input->GetBlock() == predecessor
778 || input->GetBlock()->Dominates(predecessor))) {
779 AddError(StringPrintf(
780 "Input %d at index %zu of phi %d from block %d is not defined in "
781 "predecessor number %zu nor in a block dominating it.",
782 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
783 i));
784 }
785 }
786 }
787 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000788
789 // Ensure that catch phis are sorted by their vreg number, as required by
790 // the register allocator and code generator. This does not apply to normal
791 // phis which can be constructed artifically.
792 if (phi->IsCatchPhi()) {
793 HInstruction* next_phi = phi->GetNext();
794 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
795 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
796 "vreg numbers.",
797 phi->GetId(),
798 next_phi->GetId(),
799 phi->GetBlock()->GetBlockId()));
800 }
801 }
802
Aart Bik3fc7f352015-11-20 22:03:03 -0800803 // Test phi equivalents. There should not be two of the same type and they should only be
804 // created for constants which were untyped in DEX. Note that this test can be skipped for
805 // a synthetic phi (indicated by lack of a virtual register).
806 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800807 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
808 !phi_it.Done();
809 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800810 HPhi* other_phi = phi_it.Current()->AsPhi();
811 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
812 if (phi->GetType() == other_phi->GetType()) {
813 std::stringstream type_str;
814 type_str << phi->GetType();
815 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000816 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800817 phi->GetRegNumber(),
818 type_str.str().c_str()));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000819 } else if (phi->GetType() == Primitive::kPrimNot) {
820 std::stringstream type_str;
821 type_str << other_phi->GetType();
822 AddError(StringPrintf(
823 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
824 phi->GetId(),
825 phi->GetRegNumber(),
826 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800827 } else {
828 ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true);
829 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
830 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
831 "are not equivalents of constants.",
832 phi->GetId(),
833 other_phi->GetId(),
834 phi->GetRegNumber()));
835 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000836 }
837 }
838 }
839 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000840}
841
David Brazdilbadd8262016-02-02 16:28:56 +0000842void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
David Brazdil13b47182015-04-15 16:29:32 +0100843 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000844 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100845 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000846 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000847 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100848 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
849 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000850 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100851 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000852 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000853 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100854 } else if (input->GetType() == Primitive::kPrimInt
David Brazdil74eb1b22015-12-14 11:44:01 +0000855 && (input->IsPhi() ||
856 input->IsAnd() ||
857 input->IsOr() ||
858 input->IsXor() ||
859 input->IsSelect())) {
860 // TODO: We need a data-flow analysis to determine if the Phi or Select or
David Brazdil2fa194b2015-04-20 10:14:42 +0100861 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100862 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000863 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100864 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
865 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000866 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100867 static_cast<int>(input_index),
868 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000869 }
870}
871
David Brazdilbadd8262016-02-02 16:28:56 +0000872void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
Mark Mendellfe57faa2015-09-18 09:26:15 -0400873 VisitInstruction(instruction);
874 // Check that the number of block successors matches the switch count plus
875 // one for the default block.
876 HBasicBlock* block = instruction->GetBlock();
877 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
878 AddError(StringPrintf(
879 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
880 instruction->DebugName(),
881 instruction->GetId(),
882 block->GetBlockId(),
883 instruction->GetNumEntries() + 1u,
884 block->GetSuccessors().size()));
885 }
886}
887
David Brazdilbadd8262016-02-02 16:28:56 +0000888void GraphChecker::VisitIf(HIf* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100889 VisitInstruction(instruction);
890 HandleBooleanInput(instruction, 0);
891}
892
David Brazdilbadd8262016-02-02 16:28:56 +0000893void GraphChecker::VisitSelect(HSelect* instruction) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000894 VisitInstruction(instruction);
895 HandleBooleanInput(instruction, 2);
896}
897
David Brazdilbadd8262016-02-02 16:28:56 +0000898void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100899 VisitInstruction(instruction);
900 HandleBooleanInput(instruction, 0);
901}
902
David Brazdilbadd8262016-02-02 16:28:56 +0000903void GraphChecker::VisitCondition(HCondition* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000904 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000905 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000906 AddError(StringPrintf(
907 "Condition %s %d has a non-Boolean result type: %s.",
908 op->DebugName(), op->GetId(),
909 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000910 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000911 HInstruction* lhs = op->InputAt(0);
912 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100913 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
914 AddError(StringPrintf(
915 "Condition %s %d has inputs of different types: %s, and %s.",
916 op->DebugName(), op->GetId(),
917 Primitive::PrettyDescriptor(lhs->GetType()),
918 Primitive::PrettyDescriptor(rhs->GetType())));
919 }
920 if (!op->IsEqual() && !op->IsNotEqual()) {
921 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000922 AddError(StringPrintf(
923 "Condition %s %d uses an object as left-hand side input.",
924 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100925 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000926 AddError(StringPrintf(
927 "Condition %s %d uses an object as right-hand side input.",
928 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000929 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000930 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000931}
932
David Brazdilbadd8262016-02-02 16:28:56 +0000933void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000934 VisitInstruction(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000935 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000936 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000937 AddError(StringPrintf(
938 "Shift operation %s %d has a non-int kind second input: "
939 "%s of type %s.",
940 op->DebugName(), op->GetId(),
941 op->InputAt(1)->DebugName(),
942 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000943 }
944 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100945 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000946 AddError(StringPrintf(
947 "Binary operation %s %d has inputs of different types: "
948 "%s, and %s.",
949 op->DebugName(), op->GetId(),
950 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
951 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000952 }
953 }
954
955 if (op->IsCompare()) {
956 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000957 AddError(StringPrintf(
958 "Compare operation %d has a non-int result type: %s.",
959 op->GetId(),
960 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000961 }
962 } else {
963 // Use the first input, so that we can also make this check for shift operations.
964 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000965 AddError(StringPrintf(
966 "Binary operation %s %d has a result type different "
967 "from its input type: %s vs %s.",
968 op->DebugName(), op->GetId(),
969 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100970 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000971 }
972 }
973}
974
David Brazdilbadd8262016-02-02 16:28:56 +0000975void GraphChecker::VisitConstant(HConstant* instruction) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000976 HBasicBlock* block = instruction->GetBlock();
977 if (!block->IsEntryBlock()) {
978 AddError(StringPrintf(
979 "%s %d should be in the entry block but is in block %d.",
980 instruction->DebugName(),
981 instruction->GetId(),
982 block->GetBlockId()));
983 }
984}
985
David Brazdilbadd8262016-02-02 16:28:56 +0000986void GraphChecker::VisitBoundType(HBoundType* instruction) {
David Brazdilf5552582015-12-27 13:36:12 +0000987 VisitInstruction(instruction);
988
989 ScopedObjectAccess soa(Thread::Current());
990 if (!instruction->GetUpperBound().IsValid()) {
991 AddError(StringPrintf(
992 "%s %d does not have a valid upper bound RTI.",
993 instruction->DebugName(),
994 instruction->GetId()));
995 }
996}
997
Roland Levillainccc07a92014-09-16 14:48:16 +0100998} // namespace art