blob: 2d220fca3ca858a0325484eaba8c5727ea38fa78 [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>
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
Vladimir Marko655e5852015-10-12 10:38:28 +010023#include "base/arena_containers.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010024#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000025#include "base/stringprintf.h"
David Brazdild9510df2015-11-04 23:30:22 +000026#include "handle_scope-inl.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010027
Roland Levillainccc07a92014-09-16 14:48:16 +010028namespace art {
29
30void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
31 current_block_ = block;
32
33 // Check consistency with respect to predecessors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000034 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
35 // than ArenaSafeMap<HBasicBlock*, size_t>.
36 ArenaVector<HBasicBlock*> sorted_predecessors(
37 block->GetPredecessors().begin(),
38 block->GetPredecessors().end(),
39 GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
40 std::sort(sorted_predecessors.begin(), sorted_predecessors.end());
41 for (auto it = sorted_predecessors.begin(), end = sorted_predecessors.end(); it != end; ) {
42 HBasicBlock* p = *it++;
43 size_t p_count_in_block_predecessors = 1u;
44 for (; it != end && *it == p; ++it) {
45 ++p_count_in_block_predecessors;
Vladimir Marko655e5852015-10-12 10:38:28 +010046 }
Vladimir Marko655e5852015-10-12 10:38:28 +010047 size_t block_count_in_p_successors =
48 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010049 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000050 AddError(StringPrintf(
51 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
52 "block %d lists %zu occurrences of block %d in its successors.",
53 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
54 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010055 }
56 }
57
58 // Check consistency with respect to successors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000059 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
60 // than ArenaSafeMap<HBasicBlock*, size_t>.
61 ArenaVector<HBasicBlock*> sorted_successors(
62 block->GetSuccessors().begin(),
63 block->GetSuccessors().end(),
64 GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
65 std::sort(sorted_successors.begin(), sorted_successors.end());
66 for (auto it = sorted_successors.begin(), end = sorted_successors.end(); it != end; ) {
67 HBasicBlock* s = *it++;
68 size_t s_count_in_block_successors = 1u;
69 for (; it != end && *it == s; ++it) {
70 ++s_count_in_block_successors;
Vladimir Marko655e5852015-10-12 10:38:28 +010071 }
Vladimir Marko655e5852015-10-12 10:38:28 +010072 size_t block_count_in_s_predecessors =
73 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010074 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
David Brazdil29fc0082015-08-18 17:17:38 +010092 // Ensure that only Return(Void) and Throw jump to Exit. An exiting
David Brazdilb618ade2015-07-29 10:31:29 +010093 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
94 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +000095 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilb618ade2015-07-29 10:31:29 +010096 if (predecessor->IsSingleTryBoundary()
97 && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) {
98 HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor();
99 HInstruction* last_instruction = real_predecessor->GetLastInstruction();
100 if (!last_instruction->IsThrow()) {
101 AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.",
102 last_instruction->DebugName(),
103 last_instruction->GetId()));
104 }
105 } else {
106 HInstruction* last_instruction = predecessor->GetLastInstruction();
107 if (!last_instruction->IsReturn()
108 && !last_instruction->IsReturnVoid()
109 && !last_instruction->IsThrow()) {
110 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
111 last_instruction->DebugName(),
112 last_instruction->GetId()));
113 }
114 }
115 }
116 }
117
Roland Levillainccc07a92014-09-16 14:48:16 +0100118 // Visit this block's list of phis.
119 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100120 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100122 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000123 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
124 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100125 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100126 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
127 AddError(StringPrintf("The recorded last phi of block %d does not match "
128 "the actual last phi %d.",
129 current_block_->GetBlockId(),
130 current->GetId()));
131 }
132 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100133 }
134
135 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100136 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
137 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100138 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100139 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000140 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
141 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100142 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
144 AddError(StringPrintf("The recorded last instruction of block %d does not match "
145 "the actual last instruction %d.",
146 current_block_->GetBlockId(),
147 current->GetId()));
148 }
149 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 }
David Brazdilbadd8262016-02-02 16:28:56 +0000151
152 // Ensure that catch blocks are not normal successors, and normal blocks are
153 // never exceptional successors.
154 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
155 if (successor->IsCatchBlock()) {
156 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
157 successor->GetBlockId(),
158 block->GetBlockId()));
159 }
160 }
161 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
162 if (!successor->IsCatchBlock()) {
163 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
164 successor->GetBlockId(),
165 block->GetBlockId()));
166 }
167 }
168
169 // Ensure dominated blocks have `block` as the dominator.
170 for (HBasicBlock* dominated : block->GetDominatedBlocks()) {
171 if (dominated->GetDominator() != block) {
172 AddError(StringPrintf("Block %d should be the dominator of %d.",
173 block->GetBlockId(),
174 dominated->GetBlockId()));
175 }
176 }
177
178 // Ensure there is no critical edge (i.e., an edge connecting a
179 // block with multiple successors to a block with multiple
180 // predecessors). Exceptional edges are synthesized and hence
181 // not accounted for.
182 if (block->GetSuccessors().size() > 1) {
183 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
184 if (successor->IsExitBlock() &&
185 block->IsSingleTryBoundary() &&
186 block->GetPredecessors().size() == 1u &&
187 block->GetSinglePredecessor()->GetLastInstruction()->IsThrow()) {
188 // Allowed critical edge Throw->TryBoundary->Exit.
189 } else if (successor->GetPredecessors().size() > 1) {
190 AddError(StringPrintf("Critical edge between blocks %d and %d.",
191 block->GetBlockId(),
192 successor->GetBlockId()));
193 }
194 }
195 }
196
197 // Ensure try membership information is consistent.
198 if (block->IsCatchBlock()) {
199 if (block->IsTryBlock()) {
200 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
201 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
202 "has try entry %s:%d.",
203 block->GetBlockId(),
204 try_entry.DebugName(),
205 try_entry.GetId()));
206 }
207
208 if (block->IsLoopHeader()) {
209 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
210 block->GetBlockId()));
211 }
212 } else {
213 for (HBasicBlock* predecessor : block->GetPredecessors()) {
214 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
215 if (block->IsTryBlock()) {
216 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
217 if (incoming_try_entry == nullptr) {
218 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
219 "from predecessor %d.",
220 block->GetBlockId(),
221 stored_try_entry.DebugName(),
222 stored_try_entry.GetId(),
223 predecessor->GetBlockId()));
224 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
225 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
226 "with %s:%d that follows from predecessor %d.",
227 block->GetBlockId(),
228 stored_try_entry.DebugName(),
229 stored_try_entry.GetId(),
230 incoming_try_entry->DebugName(),
231 incoming_try_entry->GetId(),
232 predecessor->GetBlockId()));
233 }
234 } else if (incoming_try_entry != nullptr) {
235 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
236 "from predecessor %d.",
237 block->GetBlockId(),
238 incoming_try_entry->DebugName(),
239 incoming_try_entry->GetId(),
240 predecessor->GetBlockId()));
241 }
242 }
243 }
244
245 if (block->IsLoopHeader()) {
246 HandleLoop(block);
247 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100248}
249
Mark Mendell1152c922015-04-24 17:06:35 -0400250void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
251 if (!GetGraph()->HasBoundsChecks()) {
252 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
253 "but HasBoundsChecks() returns false",
254 check->DebugName(),
255 check->GetId()));
256 }
257
258 // Perform the instruction base checks too.
259 VisitInstruction(check);
260}
261
David Brazdilffee3d32015-07-06 11:48:53 +0100262void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000263 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
264
265 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100266 // Note that a normal-flow successor may be a catch block before CFG
David Brazdilbadd8262016-02-02 16:28:56 +0000267 // simplification. We only test normal-flow successors in GraphChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000268 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100269 if (!handler->IsCatchBlock()) {
270 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
271 "is not a catch block.",
272 current_block_->GetBlockId(),
273 try_boundary->DebugName(),
274 try_boundary->GetId(),
275 handler->GetBlockId()));
276 }
David Brazdild26a4112015-11-10 11:07:31 +0000277 }
278
279 // Ensure that handlers are not listed multiple times.
280 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000281 if (ContainsElement(handlers, handlers[i], i + 1)) {
282 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000283 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100284 try_boundary->DebugName(),
285 try_boundary->GetId()));
286 }
287 }
288
289 VisitInstruction(try_boundary);
290}
291
David Brazdil9bc43612015-11-05 21:25:24 +0000292void GraphChecker::VisitLoadException(HLoadException* load) {
293 // Ensure that LoadException is the first instruction in a catch block.
294 if (!load->GetBlock()->IsCatchBlock()) {
295 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
296 load->DebugName(),
297 load->GetId(),
298 load->GetBlock()->GetBlockId()));
299 } else if (load->GetBlock()->GetFirstInstruction() != load) {
300 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
301 load->DebugName(),
302 load->GetId(),
303 load->GetBlock()->GetBlockId()));
304 }
305}
306
Roland Levillainccc07a92014-09-16 14:48:16 +0100307void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000308 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000309 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
310 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000311 } else {
312 seen_ids_.SetBit(instruction->GetId());
313 }
314
Roland Levillainccc07a92014-09-16 14:48:16 +0100315 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000316 if (instruction->GetBlock() == nullptr) {
317 AddError(StringPrintf("%s %d in block %d not associated with any block.",
318 instruction->IsPhi() ? "Phi" : "Instruction",
319 instruction->GetId(),
320 current_block_->GetBlockId()));
321 } else if (instruction->GetBlock() != current_block_) {
322 AddError(StringPrintf("%s %d in block %d associated with block %d.",
323 instruction->IsPhi() ? "Phi" : "Instruction",
324 instruction->GetId(),
325 current_block_->GetBlockId(),
326 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100327 }
Roland Levillain6b469232014-09-25 10:10:38 +0100328
329 // Ensure the inputs of `instruction` are defined in a block of the graph.
330 for (HInputIterator input_it(instruction); !input_it.Done();
331 input_it.Advance()) {
332 HInstruction* input = input_it.Current();
333 const HInstructionList& list = input->IsPhi()
334 ? input->GetBlock()->GetPhis()
335 : input->GetBlock()->GetInstructions();
336 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000337 AddError(StringPrintf("Input %d of instruction %d is not defined "
338 "in a basic block of the control-flow graph.",
339 input->GetId(),
340 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100341 }
342 }
343
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100344 // Ensure the uses of `instruction` are defined in a block of the graph,
345 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000346 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100347 !use_it.Done(); use_it.Advance()) {
348 HInstruction* use = use_it.Current()->GetUser();
349 const HInstructionList& list = use->IsPhi()
350 ? use->GetBlock()->GetPhis()
351 : use->GetBlock()->GetInstructions();
352 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000353 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000354 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000355 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000356 use->GetId(),
357 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100358 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100359 size_t use_index = use_it.Current()->GetIndex();
360 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000361 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100362 "UseListNode index.",
363 use->DebugName(),
364 use->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000365 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100366 instruction->GetId()));
367 }
368 }
369
370 // Ensure the environment uses entries are consistent.
371 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
372 !use_it.Done(); use_it.Advance()) {
373 HEnvironment* use = use_it.Current()->GetUser();
374 size_t use_index = use_it.Current()->GetIndex();
375 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
376 AddError(StringPrintf("Environment user of %s:%d has a wrong "
377 "UseListNode index.",
378 instruction->DebugName(),
379 instruction->GetId()));
380 }
Roland Levillain6b469232014-09-25 10:10:38 +0100381 }
David Brazdil1abb4192015-02-17 18:33:36 +0000382
383 // Ensure 'instruction' has pointers to its inputs' use entries.
384 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
385 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
386 HInstruction* input = input_record.GetInstruction();
387 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100388 size_t use_index = use_node->GetIndex();
389 if ((use_node == nullptr)
390 || !input->GetUses().Contains(use_node)
391 || (use_index >= e)
392 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000393 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
394 "at input %u (%s:%d).",
395 instruction->DebugName(),
396 instruction->GetId(),
397 static_cast<unsigned>(i),
398 input->DebugName(),
399 input->GetId()));
400 }
401 }
David Brazdilbadd8262016-02-02 16:28:56 +0000402
403 // Ensure an instruction dominates all its uses.
404 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
405 !use_it.Done(); use_it.Advance()) {
406 HInstruction* use = use_it.Current()->GetUser();
407 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
408 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
409 "use %s:%d in block %d.",
410 instruction->DebugName(),
411 instruction->GetId(),
412 current_block_->GetBlockId(),
413 use->DebugName(),
414 use->GetId(),
415 use->GetBlock()->GetBlockId()));
416 }
417 }
418
419 if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) {
420 AddError(StringPrintf("Instruction %s:%d in block %d requires an environment "
421 "but does not have one.",
422 instruction->DebugName(),
423 instruction->GetId(),
424 current_block_->GetBlockId()));
425 }
426
427 // Ensure an instruction having an environment is dominated by the
428 // instructions contained in the environment.
429 for (HEnvironment* environment = instruction->GetEnvironment();
430 environment != nullptr;
431 environment = environment->GetParent()) {
432 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
433 HInstruction* env_instruction = environment->GetInstructionAt(i);
434 if (env_instruction != nullptr
435 && !env_instruction->StrictlyDominates(instruction)) {
436 AddError(StringPrintf("Instruction %d in environment of instruction %d "
437 "from block %d does not dominate instruction %d.",
438 env_instruction->GetId(),
439 instruction->GetId(),
440 current_block_->GetBlockId(),
441 instruction->GetId()));
442 }
443 }
444 }
445
446 // Ensure that reference type instructions have reference type info.
447 if (instruction->GetType() == Primitive::kPrimNot) {
448 ScopedObjectAccess soa(Thread::Current());
449 if (!instruction->GetReferenceTypeInfo().IsValid()) {
450 AddError(StringPrintf("Reference type instruction %s:%d does not have "
451 "valid reference type information.",
452 instruction->DebugName(),
453 instruction->GetId()));
454 }
455 }
456
457 if (instruction->CanThrowIntoCatchBlock()) {
458 // Find the top-level environment. This corresponds to the environment of
459 // the catch block since we do not inline methods with try/catch.
460 HEnvironment* environment = instruction->GetEnvironment();
461 while (environment->GetParent() != nullptr) {
462 environment = environment->GetParent();
463 }
464
465 // Find all catch blocks and test that `instruction` has an environment
466 // value for each one.
467 const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
468 for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) {
469 for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
470 HPhi* catch_phi = phi_it.Current()->AsPhi();
471 if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) {
472 AddError(StringPrintf("Instruction %s:%d throws into catch block %d "
473 "with catch phi %d for vreg %d but its "
474 "corresponding environment slot is empty.",
475 instruction->DebugName(),
476 instruction->GetId(),
477 catch_block->GetBlockId(),
478 catch_phi->GetId(),
479 catch_phi->GetRegNumber()));
480 }
481 }
482 }
483 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100484}
485
Roland Levillain4c0eb422015-04-24 16:43:49 +0100486void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
487 VisitInstruction(invoke);
488
489 if (invoke->IsStaticWithExplicitClinitCheck()) {
490 size_t last_input_index = invoke->InputCount() - 1;
491 HInstruction* last_input = invoke->InputAt(last_input_index);
492 if (last_input == nullptr) {
493 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
494 "has a null pointer as last input.",
495 invoke->DebugName(),
496 invoke->GetId()));
497 }
498 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
499 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
500 "has a last instruction (%s:%d) which is neither a clinit check "
501 "nor a load class instruction.",
502 invoke->DebugName(),
503 invoke->GetId(),
504 last_input->DebugName(),
505 last_input->GetId()));
506 }
507 }
508}
509
David Brazdilfc6a86a2015-06-26 10:33:45 +0000510void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100511 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000512 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
513 AddError(StringPrintf("%s:%d does not jump to the exit block.",
514 ret->DebugName(),
515 ret->GetId()));
516 }
517}
518
519void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100520 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000521 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
522 AddError(StringPrintf("%s:%d does not jump to the exit block.",
523 ret->DebugName(),
524 ret->GetId()));
525 }
526}
527
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100528void GraphChecker::VisitCheckCast(HCheckCast* check) {
529 VisitInstruction(check);
530 HInstruction* input = check->InputAt(1);
531 if (!input->IsLoadClass()) {
532 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
533 check->DebugName(),
534 check->GetId(),
535 input->DebugName(),
536 input->GetId()));
537 }
538}
539
540void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
541 VisitInstruction(instruction);
542 HInstruction* input = instruction->InputAt(1);
543 if (!input->IsLoadClass()) {
544 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
545 instruction->DebugName(),
546 instruction->GetId(),
547 input->DebugName(),
548 input->GetId()));
549 }
550}
551
David Brazdilbadd8262016-02-02 16:28:56 +0000552void GraphChecker::HandleLoop(HBasicBlock* loop_header) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100553 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100554 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100555
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000556 if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
David Brazdildb51efb2015-11-06 01:36:20 +0000557 AddError(StringPrintf(
558 "Loop pre-header %d of loop defined by header %d has %zu successors.",
559 loop_information->GetPreHeader()->GetBlockId(),
560 id,
561 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100562 }
563
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000564 if (loop_information->GetSuspendCheck() == nullptr) {
565 AddError(StringPrintf(
566 "Loop with header %d does not have a suspend check.",
567 loop_header->GetBlockId()));
568 }
569
570 if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) {
571 AddError(StringPrintf(
572 "Loop header %d does not have the loop suspend check as the first instruction.",
573 loop_header->GetBlockId()));
574 }
575
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100576 // Ensure the loop header has only one incoming branch and the remaining
577 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000578 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000579 if (num_preds < 2) {
580 AddError(StringPrintf(
581 "Loop header %d has less than two predecessors: %zu.",
582 id,
583 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100584 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100585 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000586 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000587 AddError(StringPrintf(
588 "First predecessor of loop header %d is a back edge.",
589 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100590 }
Vladimir Marko60584552015-09-03 13:35:12 +0000591 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100592 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100593 if (!loop_information->IsBackEdge(*predecessor)) {
594 AddError(StringPrintf(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000595 "Loop header %d has multiple incoming (non back edge) blocks: %d.",
596 id,
597 predecessor->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100598 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100599 }
600 }
601
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100602 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100603
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100604 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100605 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000606 AddError(StringPrintf(
607 "Loop defined by header %d has no back edge.",
608 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100609 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100610 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
611 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100612 if (!loop_blocks.IsBitSet(back_edge_id)) {
613 AddError(StringPrintf(
614 "Loop defined by header %d has an invalid back edge %d.",
615 id,
616 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000617 } else if (back_edge->GetLoopInformation() != loop_information) {
618 AddError(StringPrintf(
619 "Back edge %d of loop defined by header %d belongs to nested loop "
620 "with header %d.",
621 back_edge_id,
622 id,
623 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100624 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100625 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100626 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100627
David Brazdil7d275372015-04-21 16:36:35 +0100628 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
629 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
630 HLoopInformation* outer_info = it.Current();
631 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
632 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
633 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100634 id,
David Brazdil7d275372015-04-21 16:36:35 +0100635 outer_info->GetHeader()->GetBlockId()));
636 }
637 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000638
639 // Ensure the pre-header block is first in the list of predecessors of a loop
640 // header and that the header block is its only successor.
641 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
642 AddError(StringPrintf(
643 "Loop pre-header is not the first predecessor of the loop header %d.",
644 id));
645 }
646
647 // Ensure all blocks in the loop are live and dominated by the loop header in
648 // the case of natural loops.
649 for (uint32_t i : loop_blocks.Indexes()) {
650 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
651 if (loop_block == nullptr) {
652 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
653 id,
654 i));
655 } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) {
656 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
657 i,
658 id));
659 }
660 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100661}
662
David Brazdil77a48ae2015-09-15 12:34:04 +0000663static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
664 return insn1->IsConstant()
665 && insn2->IsConstant()
666 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
667}
668
669static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
670 if (insn1->IsPhi() &&
671 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
672 insn1->InputCount() == insn2->InputCount()) {
673 // Testing only one of the two inputs for recursion is sufficient.
674 if (visited->IsBitSet(insn1->GetId())) {
675 return true;
676 }
677 visited->SetBit(insn1->GetId());
678
679 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
680 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
681 return false;
682 }
683 }
684 return true;
685 } else if (IsSameSizeConstant(insn1, insn2)) {
686 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
687 } else {
688 return false;
689 }
690}
691
David Brazdilbadd8262016-02-02 16:28:56 +0000692void GraphChecker::VisitPhi(HPhi* phi) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100693 VisitInstruction(phi);
694
695 // Ensure the first input of a phi is not itself.
696 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000697 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
698 phi->GetId(),
699 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100700 }
701
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000702 // Ensure that the inputs have the same primitive kind as the phi.
703 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
704 HInstruction* input = phi->InputAt(i);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000705 if (Primitive::PrimitiveKind(input->GetType()) != Primitive::PrimitiveKind(phi->GetType())) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000706 AddError(StringPrintf(
707 "Input %d at index %zu of phi %d from block %d does not have the "
Roland Levillaina5c4a402016-03-15 15:02:50 +0000708 "same kind as the phi: %s versus %s",
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000709 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
710 Primitive::PrettyDescriptor(input->GetType()),
711 Primitive::PrettyDescriptor(phi->GetType())));
712 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000713 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000714 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
715 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
716 phi->GetId(),
717 phi->GetBlock()->GetBlockId(),
718 Primitive::PrettyDescriptor(phi->GetType())));
719 }
David Brazdilffee3d32015-07-06 11:48:53 +0100720
721 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100722 // The number of inputs of a catch phi should be the total number of throwing
723 // instructions caught by this catch block. We do not enforce this, however,
724 // because we do not remove the corresponding inputs when we prove that an
725 // instruction cannot throw. Instead, we at least test that all phis have the
726 // same, non-zero number of inputs (b/24054676).
727 size_t input_count_this = phi->InputCount();
728 if (input_count_this == 0u) {
729 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
730 phi->GetId(),
731 phi->GetBlock()->GetBlockId()));
732 } else {
733 HInstruction* next_phi = phi->GetNext();
734 if (next_phi != nullptr) {
735 size_t input_count_next = next_phi->InputCount();
736 if (input_count_this != input_count_next) {
737 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
738 "but phi %d has %zu inputs.",
739 phi->GetId(),
740 phi->GetBlock()->GetBlockId(),
741 input_count_this,
742 next_phi->GetId(),
743 input_count_next));
744 }
745 }
746 }
David Brazdilffee3d32015-07-06 11:48:53 +0100747 } else {
748 // Ensure the number of inputs of a non-catch phi is the same as the number
749 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000750 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
751 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100752 AddError(StringPrintf(
753 "Phi %d in block %d has %zu inputs, "
754 "but block %d has %zu predecessors.",
755 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000756 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100757 } else {
758 // Ensure phi input at index I either comes from the Ith
759 // predecessor or from a block that dominates this predecessor.
760 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
761 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000762 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100763 if (!(input->GetBlock() == predecessor
764 || input->GetBlock()->Dominates(predecessor))) {
765 AddError(StringPrintf(
766 "Input %d at index %zu of phi %d from block %d is not defined in "
767 "predecessor number %zu nor in a block dominating it.",
768 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
769 i));
770 }
771 }
772 }
773 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000774
775 // Ensure that catch phis are sorted by their vreg number, as required by
776 // the register allocator and code generator. This does not apply to normal
777 // phis which can be constructed artifically.
778 if (phi->IsCatchPhi()) {
779 HInstruction* next_phi = phi->GetNext();
780 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
781 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
782 "vreg numbers.",
783 phi->GetId(),
784 next_phi->GetId(),
785 phi->GetBlock()->GetBlockId()));
786 }
787 }
788
Aart Bik3fc7f352015-11-20 22:03:03 -0800789 // Test phi equivalents. There should not be two of the same type and they should only be
790 // created for constants which were untyped in DEX. Note that this test can be skipped for
791 // a synthetic phi (indicated by lack of a virtual register).
792 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800793 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
794 !phi_it.Done();
795 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800796 HPhi* other_phi = phi_it.Current()->AsPhi();
797 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
798 if (phi->GetType() == other_phi->GetType()) {
799 std::stringstream type_str;
800 type_str << phi->GetType();
801 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000802 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800803 phi->GetRegNumber(),
804 type_str.str().c_str()));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000805 } else if (phi->GetType() == Primitive::kPrimNot) {
806 std::stringstream type_str;
807 type_str << other_phi->GetType();
808 AddError(StringPrintf(
809 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
810 phi->GetId(),
811 phi->GetRegNumber(),
812 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800813 } else {
Vladimir Markof6a35de2016-03-21 12:01:50 +0000814 ArenaBitVector visited(GetGraph()->GetArena(),
815 0,
816 /* expandable */ true,
817 kArenaAllocGraphChecker);
Aart Bik3fc7f352015-11-20 22:03:03 -0800818 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
819 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
820 "are not equivalents of constants.",
821 phi->GetId(),
822 other_phi->GetId(),
823 phi->GetRegNumber()));
824 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000825 }
826 }
827 }
828 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000829}
830
David Brazdilbadd8262016-02-02 16:28:56 +0000831void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
David Brazdil13b47182015-04-15 16:29:32 +0100832 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000833 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100834 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000835 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000836 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100837 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
838 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000839 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100840 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000841 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000842 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100843 } else if (input->GetType() == Primitive::kPrimInt
David Brazdil74eb1b22015-12-14 11:44:01 +0000844 && (input->IsPhi() ||
845 input->IsAnd() ||
846 input->IsOr() ||
847 input->IsXor() ||
848 input->IsSelect())) {
849 // TODO: We need a data-flow analysis to determine if the Phi or Select or
David Brazdil2fa194b2015-04-20 10:14:42 +0100850 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100851 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000852 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100853 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
854 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000855 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100856 static_cast<int>(input_index),
857 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000858 }
859}
860
David Brazdilbadd8262016-02-02 16:28:56 +0000861void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
Mark Mendellfe57faa2015-09-18 09:26:15 -0400862 VisitInstruction(instruction);
863 // Check that the number of block successors matches the switch count plus
864 // one for the default block.
865 HBasicBlock* block = instruction->GetBlock();
866 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
867 AddError(StringPrintf(
868 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
869 instruction->DebugName(),
870 instruction->GetId(),
871 block->GetBlockId(),
872 instruction->GetNumEntries() + 1u,
873 block->GetSuccessors().size()));
874 }
875}
876
David Brazdilbadd8262016-02-02 16:28:56 +0000877void GraphChecker::VisitIf(HIf* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100878 VisitInstruction(instruction);
879 HandleBooleanInput(instruction, 0);
880}
881
David Brazdilbadd8262016-02-02 16:28:56 +0000882void GraphChecker::VisitSelect(HSelect* instruction) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000883 VisitInstruction(instruction);
884 HandleBooleanInput(instruction, 2);
885}
886
David Brazdilbadd8262016-02-02 16:28:56 +0000887void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100888 VisitInstruction(instruction);
889 HandleBooleanInput(instruction, 0);
890}
891
David Brazdilbadd8262016-02-02 16:28:56 +0000892void GraphChecker::VisitCondition(HCondition* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000893 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000894 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000895 AddError(StringPrintf(
896 "Condition %s %d has a non-Boolean result type: %s.",
897 op->DebugName(), op->GetId(),
898 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000899 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000900 HInstruction* lhs = op->InputAt(0);
901 HInstruction* rhs = op->InputAt(1);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000902 if (Primitive::PrimitiveKind(lhs->GetType()) != Primitive::PrimitiveKind(rhs->GetType())) {
Calin Juravlea4f88312015-04-16 12:57:19 +0100903 AddError(StringPrintf(
Roland Levillaina5c4a402016-03-15 15:02:50 +0000904 "Condition %s %d has inputs of different kinds: %s, and %s.",
Calin Juravlea4f88312015-04-16 12:57:19 +0100905 op->DebugName(), op->GetId(),
906 Primitive::PrettyDescriptor(lhs->GetType()),
907 Primitive::PrettyDescriptor(rhs->GetType())));
908 }
909 if (!op->IsEqual() && !op->IsNotEqual()) {
910 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000911 AddError(StringPrintf(
912 "Condition %s %d uses an object as left-hand side input.",
913 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100914 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000915 AddError(StringPrintf(
916 "Condition %s %d uses an object as right-hand side input.",
917 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000918 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000919 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000920}
921
David Brazdilbadd8262016-02-02 16:28:56 +0000922void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000923 VisitInstruction(op);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000924 Primitive::Type lhs_type = op->InputAt(0)->GetType();
925 Primitive::Type rhs_type = op->InputAt(1)->GetType();
926 Primitive::Type result_type = op->GetType();
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000927 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000928 if (Primitive::PrimitiveKind(rhs_type) != Primitive::kPrimInt) {
929 AddError(StringPrintf("Shift operation %s %d has a non-int kind second input: %s of type %s.",
930 op->DebugName(), op->GetId(),
931 op->InputAt(1)->DebugName(),
932 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000933 }
934 } else {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000935 if (Primitive::PrimitiveKind(lhs_type) != Primitive::PrimitiveKind(rhs_type)) {
936 AddError(StringPrintf("Binary operation %s %d has inputs of different kinds: %s, and %s.",
937 op->DebugName(), op->GetId(),
938 Primitive::PrettyDescriptor(lhs_type),
939 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000940 }
941 }
942
943 if (op->IsCompare()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000944 if (result_type != Primitive::kPrimInt) {
945 AddError(StringPrintf("Compare operation %d has a non-int result type: %s.",
946 op->GetId(),
947 Primitive::PrettyDescriptor(result_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000948 }
949 } else {
Roland Levillain22c49222016-03-18 14:04:28 +0000950 // Use the first input, so that we can also make this check for shift and rotate operations.
Roland Levillaina5c4a402016-03-15 15:02:50 +0000951 if (Primitive::PrimitiveKind(result_type) != Primitive::PrimitiveKind(lhs_type)) {
952 AddError(StringPrintf("Binary operation %s %d has a result kind different "
953 "from its input kind: %s vs %s.",
954 op->DebugName(), op->GetId(),
955 Primitive::PrettyDescriptor(result_type),
956 Primitive::PrettyDescriptor(lhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000957 }
958 }
959}
960
David Brazdilbadd8262016-02-02 16:28:56 +0000961void GraphChecker::VisitConstant(HConstant* instruction) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000962 HBasicBlock* block = instruction->GetBlock();
963 if (!block->IsEntryBlock()) {
964 AddError(StringPrintf(
965 "%s %d should be in the entry block but is in block %d.",
966 instruction->DebugName(),
967 instruction->GetId(),
968 block->GetBlockId()));
969 }
970}
971
David Brazdilbadd8262016-02-02 16:28:56 +0000972void GraphChecker::VisitBoundType(HBoundType* instruction) {
David Brazdilf5552582015-12-27 13:36:12 +0000973 VisitInstruction(instruction);
974
975 ScopedObjectAccess soa(Thread::Current());
976 if (!instruction->GetUpperBound().IsValid()) {
977 AddError(StringPrintf(
978 "%s %d does not have a valid upper bound RTI.",
979 instruction->DebugName(),
980 instruction->GetId()));
981 }
982}
983
Roland Levillainccc07a92014-09-16 14:48:16 +0100984} // namespace art