blob: 34b52a87b55615bff57ebef727515bee7ddeef71 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Vladimir Marko655e5852015-10-12 10:38:28 +010025#include "base/arena_containers.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010026#include "base/bit_vector-inl.h"
27
Roland Levillainccc07a92014-09-16 14:48:16 +010028namespace art {
29
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030using android::base::StringPrintf;
31
David Brazdil86ea7ee2016-02-16 09:26:07 +000032static bool IsAllowedToJumpToExitBlock(HInstruction* instruction) {
33 return instruction->IsThrow() || instruction->IsReturn() || instruction->IsReturnVoid();
34}
35
36static bool IsExitTryBoundaryIntoExitBlock(HBasicBlock* block) {
37 if (!block->IsSingleTryBoundary()) {
38 return false;
39 }
40
41 HTryBoundary* boundary = block->GetLastInstruction()->AsTryBoundary();
42 return block->GetPredecessors().size() == 1u &&
43 boundary->GetNormalFlowSuccessor()->IsExitBlock() &&
44 !boundary->IsEntry();
45}
46
Roland Levillainccc07a92014-09-16 14:48:16 +010047void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
48 current_block_ = block;
49
50 // Check consistency with respect to predecessors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000051 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +000052 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
53 ArenaVector<HBasicBlock*>& sorted_predecessors = blocks_storage_;
54 sorted_predecessors.assign(block->GetPredecessors().begin(), block->GetPredecessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +000055 std::sort(sorted_predecessors.begin(), sorted_predecessors.end());
56 for (auto it = sorted_predecessors.begin(), end = sorted_predecessors.end(); it != end; ) {
57 HBasicBlock* p = *it++;
58 size_t p_count_in_block_predecessors = 1u;
59 for (; it != end && *it == p; ++it) {
60 ++p_count_in_block_predecessors;
Vladimir Marko655e5852015-10-12 10:38:28 +010061 }
Vladimir Marko655e5852015-10-12 10:38:28 +010062 size_t block_count_in_p_successors =
63 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010064 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000065 AddError(StringPrintf(
66 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
67 "block %d lists %zu occurrences of block %d in its successors.",
68 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
69 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010070 }
71 }
72
73 // Check consistency with respect to successors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000074 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +000075 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
76 ArenaVector<HBasicBlock*>& sorted_successors = blocks_storage_;
77 sorted_successors.assign(block->GetSuccessors().begin(), block->GetSuccessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +000078 std::sort(sorted_successors.begin(), sorted_successors.end());
79 for (auto it = sorted_successors.begin(), end = sorted_successors.end(); it != end; ) {
80 HBasicBlock* s = *it++;
81 size_t s_count_in_block_successors = 1u;
82 for (; it != end && *it == s; ++it) {
83 ++s_count_in_block_successors;
Vladimir Marko655e5852015-10-12 10:38:28 +010084 }
Vladimir Marko655e5852015-10-12 10:38:28 +010085 size_t block_count_in_s_predecessors =
86 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010087 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000088 AddError(StringPrintf(
89 "Block %d lists %zu occurrences of block %d in its successors, whereas "
90 "block %d lists %zu occurrences of block %d in its predecessors.",
91 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
92 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010093 }
94 }
95
96 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000097 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
98 // dead code that falls out of the method will not end with a control-flow
99 // instruction. Such code is removed during the SSA-building DCE phase.
100 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000101 AddError(StringPrintf("Block %d does not end with a branch instruction.",
102 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100103 }
104
David Brazdil86ea7ee2016-02-16 09:26:07 +0000105 // Ensure that only Return(Void) and Throw jump to Exit. An exiting TryBoundary
106 // may be between the instructions if the Throw/Return(Void) is in a try block.
David Brazdilb618ade2015-07-29 10:31:29 +0100107 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +0000108 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000109 HInstruction* last_instruction = IsExitTryBoundaryIntoExitBlock(predecessor) ?
110 predecessor->GetSinglePredecessor()->GetLastInstruction() :
111 predecessor->GetLastInstruction();
112 if (!IsAllowedToJumpToExitBlock(last_instruction)) {
113 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
114 last_instruction->DebugName(),
115 last_instruction->GetId()));
David Brazdilb618ade2015-07-29 10:31:29 +0100116 }
117 }
118 }
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Visit this block's list of phis.
121 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100122 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100123 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100124 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000125 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
126 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100127 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100128 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
129 AddError(StringPrintf("The recorded last phi of block %d does not match "
130 "the actual last phi %d.",
131 current_block_->GetBlockId(),
132 current->GetId()));
133 }
134 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100135 }
136
137 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100138 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
139 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100140 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100141 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000142 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
143 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100144 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100145 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
146 AddError(StringPrintf("The recorded last instruction of block %d does not match "
147 "the actual last instruction %d.",
148 current_block_->GetBlockId(),
149 current->GetId()));
150 }
151 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100152 }
David Brazdilbadd8262016-02-02 16:28:56 +0000153
154 // Ensure that catch blocks are not normal successors, and normal blocks are
155 // never exceptional successors.
156 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
157 if (successor->IsCatchBlock()) {
158 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
159 successor->GetBlockId(),
160 block->GetBlockId()));
161 }
162 }
163 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
164 if (!successor->IsCatchBlock()) {
165 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
166 successor->GetBlockId(),
167 block->GetBlockId()));
168 }
169 }
170
171 // Ensure dominated blocks have `block` as the dominator.
172 for (HBasicBlock* dominated : block->GetDominatedBlocks()) {
173 if (dominated->GetDominator() != block) {
174 AddError(StringPrintf("Block %d should be the dominator of %d.",
175 block->GetBlockId(),
176 dominated->GetBlockId()));
177 }
178 }
179
180 // Ensure there is no critical edge (i.e., an edge connecting a
181 // block with multiple successors to a block with multiple
182 // predecessors). Exceptional edges are synthesized and hence
183 // not accounted for.
184 if (block->GetSuccessors().size() > 1) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000185 if (IsExitTryBoundaryIntoExitBlock(block)) {
186 // Allowed critical edge (Throw/Return/ReturnVoid)->TryBoundary->Exit.
187 } else {
188 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
189 if (successor->GetPredecessors().size() > 1) {
190 AddError(StringPrintf("Critical edge between blocks %d and %d.",
191 block->GetBlockId(),
192 successor->GetBlockId()));
193 }
David Brazdilbadd8262016-02-02 16:28:56 +0000194 }
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
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100263void GraphChecker::VisitDeoptimize(HDeoptimize* deopt) {
264 if (GetGraph()->IsCompilingOsr()) {
265 AddError(StringPrintf("A graph compiled OSR cannot have a HDeoptimize instruction"));
266 }
267
268 // Perform the instruction base checks too.
269 VisitInstruction(deopt);
270}
271
David Brazdilffee3d32015-07-06 11:48:53 +0100272void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000273 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
274
275 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100276 // Note that a normal-flow successor may be a catch block before CFG
David Brazdilbadd8262016-02-02 16:28:56 +0000277 // simplification. We only test normal-flow successors in GraphChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000278 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100279 if (!handler->IsCatchBlock()) {
280 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
281 "is not a catch block.",
282 current_block_->GetBlockId(),
283 try_boundary->DebugName(),
284 try_boundary->GetId(),
285 handler->GetBlockId()));
286 }
David Brazdild26a4112015-11-10 11:07:31 +0000287 }
288
289 // Ensure that handlers are not listed multiple times.
290 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000291 if (ContainsElement(handlers, handlers[i], i + 1)) {
292 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000293 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100294 try_boundary->DebugName(),
295 try_boundary->GetId()));
296 }
297 }
298
299 VisitInstruction(try_boundary);
300}
301
David Brazdil9bc43612015-11-05 21:25:24 +0000302void GraphChecker::VisitLoadException(HLoadException* load) {
303 // Ensure that LoadException is the first instruction in a catch block.
304 if (!load->GetBlock()->IsCatchBlock()) {
305 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
306 load->DebugName(),
307 load->GetId(),
308 load->GetBlock()->GetBlockId()));
309 } else if (load->GetBlock()->GetFirstInstruction() != load) {
310 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
311 load->DebugName(),
312 load->GetId(),
313 load->GetBlock()->GetBlockId()));
314 }
315}
316
Roland Levillainccc07a92014-09-16 14:48:16 +0100317void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000318 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000319 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
320 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000321 } else {
322 seen_ids_.SetBit(instruction->GetId());
323 }
324
Roland Levillainccc07a92014-09-16 14:48:16 +0100325 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000326 if (instruction->GetBlock() == nullptr) {
327 AddError(StringPrintf("%s %d in block %d not associated with any block.",
328 instruction->IsPhi() ? "Phi" : "Instruction",
329 instruction->GetId(),
330 current_block_->GetBlockId()));
331 } else if (instruction->GetBlock() != current_block_) {
332 AddError(StringPrintf("%s %d in block %d associated with block %d.",
333 instruction->IsPhi() ? "Phi" : "Instruction",
334 instruction->GetId(),
335 current_block_->GetBlockId(),
336 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100337 }
Roland Levillain6b469232014-09-25 10:10:38 +0100338
339 // Ensure the inputs of `instruction` are defined in a block of the graph.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100340 for (HInstruction* input : instruction->GetInputs()) {
Roland Levillain6b469232014-09-25 10:10:38 +0100341 const HInstructionList& list = input->IsPhi()
342 ? input->GetBlock()->GetPhis()
343 : input->GetBlock()->GetInstructions();
344 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000345 AddError(StringPrintf("Input %d of instruction %d is not defined "
346 "in a basic block of the control-flow graph.",
347 input->GetId(),
348 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100349 }
350 }
351
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100352 // Ensure the uses of `instruction` are defined in a block of the graph,
353 // and the entry in the use list is consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100354 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
355 HInstruction* user = use.GetUser();
356 const HInstructionList& list = user->IsPhi()
357 ? user->GetBlock()->GetPhis()
358 : user->GetBlock()->GetInstructions();
359 if (!list.Contains(user)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000360 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000361 "in a basic block of the control-flow graph.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100362 user->DebugName(),
363 user->GetId(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000364 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100365 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100366 size_t use_index = use.GetIndex();
Vladimir Markoe9004912016-06-16 16:50:52 +0100367 HConstInputsRef user_inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100368 if ((use_index >= user_inputs.size()) || (user_inputs[use_index] != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000369 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100370 "UseListNode index.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100371 user->DebugName(),
372 user->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000373 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100374 instruction->GetId()));
375 }
376 }
377
378 // Ensure the environment uses entries are consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100379 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
380 HEnvironment* user = use.GetUser();
381 size_t use_index = use.GetIndex();
382 if ((use_index >= user->Size()) || (user->GetInstructionAt(use_index) != instruction)) {
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100383 AddError(StringPrintf("Environment user of %s:%d has a wrong "
384 "UseListNode index.",
385 instruction->DebugName(),
386 instruction->GetId()));
387 }
Roland Levillain6b469232014-09-25 10:10:38 +0100388 }
David Brazdil1abb4192015-02-17 18:33:36 +0000389
390 // Ensure 'instruction' has pointers to its inputs' use entries.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100391 auto&& input_records = instruction->GetInputRecords();
392 for (size_t i = 0; i < input_records.size(); ++i) {
393 const HUserRecord<HInstruction*>& input_record = input_records[i];
David Brazdil1abb4192015-02-17 18:33:36 +0000394 HInstruction* input = input_record.GetInstruction();
Vladimir Marko46817b82016-03-29 12:21:58 +0100395 if ((input_record.GetBeforeUseNode() == input->GetUses().end()) ||
396 (input_record.GetUseNode() == input->GetUses().end()) ||
397 !input->GetUses().ContainsNode(*input_record.GetUseNode()) ||
398 (input_record.GetUseNode()->GetIndex() != i)) {
399 AddError(StringPrintf("Instruction %s:%d has an invalid iterator before use entry "
David Brazdil1abb4192015-02-17 18:33:36 +0000400 "at input %u (%s:%d).",
401 instruction->DebugName(),
402 instruction->GetId(),
403 static_cast<unsigned>(i),
404 input->DebugName(),
405 input->GetId()));
406 }
407 }
David Brazdilbadd8262016-02-02 16:28:56 +0000408
409 // Ensure an instruction dominates all its uses.
Vladimir Marko46817b82016-03-29 12:21:58 +0100410 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
411 HInstruction* user = use.GetUser();
412 if (!user->IsPhi() && !instruction->StrictlyDominates(user)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000413 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
414 "use %s:%d in block %d.",
415 instruction->DebugName(),
416 instruction->GetId(),
417 current_block_->GetBlockId(),
Vladimir Marko46817b82016-03-29 12:21:58 +0100418 user->DebugName(),
419 user->GetId(),
420 user->GetBlock()->GetBlockId()));
David Brazdilbadd8262016-02-02 16:28:56 +0000421 }
422 }
423
424 if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) {
425 AddError(StringPrintf("Instruction %s:%d in block %d requires an environment "
426 "but does not have one.",
427 instruction->DebugName(),
428 instruction->GetId(),
429 current_block_->GetBlockId()));
430 }
431
432 // Ensure an instruction having an environment is dominated by the
433 // instructions contained in the environment.
434 for (HEnvironment* environment = instruction->GetEnvironment();
435 environment != nullptr;
436 environment = environment->GetParent()) {
437 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
438 HInstruction* env_instruction = environment->GetInstructionAt(i);
439 if (env_instruction != nullptr
440 && !env_instruction->StrictlyDominates(instruction)) {
441 AddError(StringPrintf("Instruction %d in environment of instruction %d "
442 "from block %d does not dominate instruction %d.",
443 env_instruction->GetId(),
444 instruction->GetId(),
445 current_block_->GetBlockId(),
446 instruction->GetId()));
447 }
448 }
449 }
450
451 // Ensure that reference type instructions have reference type info.
452 if (instruction->GetType() == Primitive::kPrimNot) {
David Brazdilbadd8262016-02-02 16:28:56 +0000453 if (!instruction->GetReferenceTypeInfo().IsValid()) {
454 AddError(StringPrintf("Reference type instruction %s:%d does not have "
455 "valid reference type information.",
456 instruction->DebugName(),
457 instruction->GetId()));
458 }
459 }
460
461 if (instruction->CanThrowIntoCatchBlock()) {
462 // Find the top-level environment. This corresponds to the environment of
463 // the catch block since we do not inline methods with try/catch.
464 HEnvironment* environment = instruction->GetEnvironment();
465 while (environment->GetParent() != nullptr) {
466 environment = environment->GetParent();
467 }
468
469 // Find all catch blocks and test that `instruction` has an environment
470 // value for each one.
471 const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
472 for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) {
473 for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
474 HPhi* catch_phi = phi_it.Current()->AsPhi();
475 if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) {
476 AddError(StringPrintf("Instruction %s:%d throws into catch block %d "
477 "with catch phi %d for vreg %d but its "
478 "corresponding environment slot is empty.",
479 instruction->DebugName(),
480 instruction->GetId(),
481 catch_block->GetBlockId(),
482 catch_phi->GetId(),
483 catch_phi->GetRegNumber()));
484 }
485 }
486 }
487 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100488}
489
Roland Levillain4c0eb422015-04-24 16:43:49 +0100490void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
491 VisitInstruction(invoke);
492
493 if (invoke->IsStaticWithExplicitClinitCheck()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100494 const HInstruction* last_input = invoke->GetInputs().back();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100495 if (last_input == nullptr) {
496 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
497 "has a null pointer as last input.",
498 invoke->DebugName(),
499 invoke->GetId()));
500 }
501 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
502 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
503 "has a last instruction (%s:%d) which is neither a clinit check "
504 "nor a load class instruction.",
505 invoke->DebugName(),
506 invoke->GetId(),
507 last_input->DebugName(),
508 last_input->GetId()));
509 }
510 }
511}
512
David Brazdilfc6a86a2015-06-26 10:33:45 +0000513void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100514 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000515 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
516 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000517 AddError(StringPrintf("%s:%d does not jump to the exit block.",
518 ret->DebugName(),
519 ret->GetId()));
520 }
521}
522
523void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100524 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000525 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
526 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000527 AddError(StringPrintf("%s:%d does not jump to the exit block.",
528 ret->DebugName(),
529 ret->GetId()));
530 }
531}
532
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100533void GraphChecker::VisitCheckCast(HCheckCast* check) {
534 VisitInstruction(check);
535 HInstruction* input = check->InputAt(1);
536 if (!input->IsLoadClass()) {
537 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
538 check->DebugName(),
539 check->GetId(),
540 input->DebugName(),
541 input->GetId()));
542 }
543}
544
545void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
546 VisitInstruction(instruction);
547 HInstruction* input = instruction->InputAt(1);
548 if (!input->IsLoadClass()) {
549 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
550 instruction->DebugName(),
551 instruction->GetId(),
552 input->DebugName(),
553 input->GetId()));
554 }
555}
556
David Brazdilbadd8262016-02-02 16:28:56 +0000557void GraphChecker::HandleLoop(HBasicBlock* loop_header) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100558 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100559 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100560
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000561 if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
David Brazdildb51efb2015-11-06 01:36:20 +0000562 AddError(StringPrintf(
563 "Loop pre-header %d of loop defined by header %d has %zu successors.",
564 loop_information->GetPreHeader()->GetBlockId(),
565 id,
566 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100567 }
568
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000569 if (loop_information->GetSuspendCheck() == nullptr) {
570 AddError(StringPrintf(
571 "Loop with header %d does not have a suspend check.",
572 loop_header->GetBlockId()));
573 }
574
575 if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) {
576 AddError(StringPrintf(
577 "Loop header %d does not have the loop suspend check as the first instruction.",
578 loop_header->GetBlockId()));
579 }
580
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100581 // Ensure the loop header has only one incoming branch and the remaining
582 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000583 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000584 if (num_preds < 2) {
585 AddError(StringPrintf(
586 "Loop header %d has less than two predecessors: %zu.",
587 id,
588 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100589 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100590 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000591 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000592 AddError(StringPrintf(
593 "First predecessor of loop header %d is a back edge.",
594 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100595 }
Vladimir Marko60584552015-09-03 13:35:12 +0000596 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100597 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100598 if (!loop_information->IsBackEdge(*predecessor)) {
599 AddError(StringPrintf(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000600 "Loop header %d has multiple incoming (non back edge) blocks: %d.",
601 id,
602 predecessor->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100603 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100604 }
605 }
606
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100607 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100608
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100609 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100610 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000611 AddError(StringPrintf(
612 "Loop defined by header %d has no back edge.",
613 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100614 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100615 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
616 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100617 if (!loop_blocks.IsBitSet(back_edge_id)) {
618 AddError(StringPrintf(
619 "Loop defined by header %d has an invalid back edge %d.",
620 id,
621 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000622 } else if (back_edge->GetLoopInformation() != loop_information) {
623 AddError(StringPrintf(
624 "Back edge %d of loop defined by header %d belongs to nested loop "
625 "with header %d.",
626 back_edge_id,
627 id,
628 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100629 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100630 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100631 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100632
David Brazdil7d275372015-04-21 16:36:35 +0100633 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
634 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
635 HLoopInformation* outer_info = it.Current();
636 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
637 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
638 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100639 id,
David Brazdil7d275372015-04-21 16:36:35 +0100640 outer_info->GetHeader()->GetBlockId()));
641 }
642 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000643
644 // Ensure the pre-header block is first in the list of predecessors of a loop
645 // header and that the header block is its only successor.
646 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
647 AddError(StringPrintf(
648 "Loop pre-header is not the first predecessor of the loop header %d.",
649 id));
650 }
651
652 // Ensure all blocks in the loop are live and dominated by the loop header in
653 // the case of natural loops.
654 for (uint32_t i : loop_blocks.Indexes()) {
655 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
656 if (loop_block == nullptr) {
657 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
658 id,
659 i));
660 } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) {
661 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
662 i,
663 id));
664 }
665 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100666}
667
Vladimir Markoe9004912016-06-16 16:50:52 +0100668static bool IsSameSizeConstant(const HInstruction* insn1, const HInstruction* insn2) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000669 return insn1->IsConstant()
670 && insn2->IsConstant()
671 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
672}
673
Vladimir Markoe9004912016-06-16 16:50:52 +0100674static bool IsConstantEquivalent(const HInstruction* insn1,
675 const HInstruction* insn2,
676 BitVector* visited) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000677 if (insn1->IsPhi() &&
Vladimir Marko372f10e2016-05-17 16:30:10 +0100678 insn1->AsPhi()->IsVRegEquivalentOf(insn2)) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100679 HConstInputsRef insn1_inputs = insn1->GetInputs();
680 HConstInputsRef insn2_inputs = insn2->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100681 if (insn1_inputs.size() != insn2_inputs.size()) {
682 return false;
683 }
684
David Brazdil77a48ae2015-09-15 12:34:04 +0000685 // Testing only one of the two inputs for recursion is sufficient.
686 if (visited->IsBitSet(insn1->GetId())) {
687 return true;
688 }
689 visited->SetBit(insn1->GetId());
690
Vladimir Marko372f10e2016-05-17 16:30:10 +0100691 for (size_t i = 0; i < insn1_inputs.size(); ++i) {
692 if (!IsConstantEquivalent(insn1_inputs[i], insn2_inputs[i], visited)) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000693 return false;
694 }
695 }
696 return true;
697 } else if (IsSameSizeConstant(insn1, insn2)) {
698 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
699 } else {
700 return false;
701 }
702}
703
David Brazdilbadd8262016-02-02 16:28:56 +0000704void GraphChecker::VisitPhi(HPhi* phi) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100705 VisitInstruction(phi);
706
707 // Ensure the first input of a phi is not itself.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100708 ArrayRef<HUserRecord<HInstruction*>> input_records = phi->GetInputRecords();
709 if (input_records[0].GetInstruction() == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000710 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
711 phi->GetId(),
712 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100713 }
714
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000715 // Ensure that the inputs have the same primitive kind as the phi.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100716 for (size_t i = 0; i < input_records.size(); ++i) {
717 HInstruction* input = input_records[i].GetInstruction();
Roland Levillaina5c4a402016-03-15 15:02:50 +0000718 if (Primitive::PrimitiveKind(input->GetType()) != Primitive::PrimitiveKind(phi->GetType())) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000719 AddError(StringPrintf(
720 "Input %d at index %zu of phi %d from block %d does not have the "
Roland Levillaina5c4a402016-03-15 15:02:50 +0000721 "same kind as the phi: %s versus %s",
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000722 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
723 Primitive::PrettyDescriptor(input->GetType()),
724 Primitive::PrettyDescriptor(phi->GetType())));
725 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000726 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000727 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
728 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
729 phi->GetId(),
730 phi->GetBlock()->GetBlockId(),
731 Primitive::PrettyDescriptor(phi->GetType())));
732 }
David Brazdilffee3d32015-07-06 11:48:53 +0100733
734 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100735 // The number of inputs of a catch phi should be the total number of throwing
736 // instructions caught by this catch block. We do not enforce this, however,
737 // because we do not remove the corresponding inputs when we prove that an
738 // instruction cannot throw. Instead, we at least test that all phis have the
739 // same, non-zero number of inputs (b/24054676).
Vladimir Marko372f10e2016-05-17 16:30:10 +0100740 if (input_records.empty()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100741 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
742 phi->GetId(),
743 phi->GetBlock()->GetBlockId()));
744 } else {
745 HInstruction* next_phi = phi->GetNext();
746 if (next_phi != nullptr) {
747 size_t input_count_next = next_phi->InputCount();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100748 if (input_records.size() != input_count_next) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100749 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
750 "but phi %d has %zu inputs.",
751 phi->GetId(),
752 phi->GetBlock()->GetBlockId(),
Vladimir Marko372f10e2016-05-17 16:30:10 +0100753 input_records.size(),
David Brazdil3eaa32f2015-09-18 10:58:32 +0100754 next_phi->GetId(),
755 input_count_next));
756 }
757 }
758 }
David Brazdilffee3d32015-07-06 11:48:53 +0100759 } else {
760 // Ensure the number of inputs of a non-catch phi is the same as the number
761 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000762 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100763 if (input_records.size() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100764 AddError(StringPrintf(
765 "Phi %d in block %d has %zu inputs, "
766 "but block %d has %zu predecessors.",
Vladimir Marko372f10e2016-05-17 16:30:10 +0100767 phi->GetId(), phi->GetBlock()->GetBlockId(), input_records.size(),
Vladimir Marko60584552015-09-03 13:35:12 +0000768 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100769 } else {
770 // Ensure phi input at index I either comes from the Ith
771 // predecessor or from a block that dominates this predecessor.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100772 for (size_t i = 0; i < input_records.size(); ++i) {
773 HInstruction* input = input_records[i].GetInstruction();
Vladimir Marko60584552015-09-03 13:35:12 +0000774 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100775 if (!(input->GetBlock() == predecessor
776 || input->GetBlock()->Dominates(predecessor))) {
777 AddError(StringPrintf(
778 "Input %d at index %zu of phi %d from block %d is not defined in "
779 "predecessor number %zu nor in a block dominating it.",
780 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
781 i));
782 }
783 }
784 }
785 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000786
787 // Ensure that catch phis are sorted by their vreg number, as required by
788 // the register allocator and code generator. This does not apply to normal
789 // phis which can be constructed artifically.
790 if (phi->IsCatchPhi()) {
791 HInstruction* next_phi = phi->GetNext();
792 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
793 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
794 "vreg numbers.",
795 phi->GetId(),
796 next_phi->GetId(),
797 phi->GetBlock()->GetBlockId()));
798 }
799 }
800
Aart Bik3fc7f352015-11-20 22:03:03 -0800801 // Test phi equivalents. There should not be two of the same type and they should only be
802 // created for constants which were untyped in DEX. Note that this test can be skipped for
803 // a synthetic phi (indicated by lack of a virtual register).
804 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800805 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
806 !phi_it.Done();
807 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800808 HPhi* other_phi = phi_it.Current()->AsPhi();
809 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
810 if (phi->GetType() == other_phi->GetType()) {
811 std::stringstream type_str;
812 type_str << phi->GetType();
813 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000814 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800815 phi->GetRegNumber(),
816 type_str.str().c_str()));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000817 } else if (phi->GetType() == Primitive::kPrimNot) {
818 std::stringstream type_str;
819 type_str << other_phi->GetType();
820 AddError(StringPrintf(
821 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
822 phi->GetId(),
823 phi->GetRegNumber(),
824 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800825 } else {
Vladimir Marko947eb702016-03-25 15:31:35 +0000826 // If we get here, make sure we allocate all the necessary storage at once
827 // because the BitVector reallocation strategy has very bad worst-case behavior.
828 ArenaBitVector& visited = visited_storage_;
829 visited.SetBit(GetGraph()->GetCurrentInstructionId());
830 visited.ClearAllBits();
Aart Bik3fc7f352015-11-20 22:03:03 -0800831 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
832 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
833 "are not equivalents of constants.",
834 phi->GetId(),
835 other_phi->GetId(),
836 phi->GetRegNumber()));
837 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000838 }
839 }
840 }
841 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000842}
843
David Brazdilbadd8262016-02-02 16:28:56 +0000844void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
David Brazdil13b47182015-04-15 16:29:32 +0100845 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000846 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100847 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000848 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000849 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100850 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
851 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000852 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100853 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000854 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000855 }
David Brazdil11edec72016-03-24 12:40:52 +0000856 } else if (Primitive::PrimitiveKind(input->GetType()) != Primitive::kPrimInt) {
857 // TODO: We need a data-flow analysis to determine if an input like Phi,
858 // Select or a binary operation is actually Boolean. Allow for now.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000859 AddError(StringPrintf(
David Brazdil11edec72016-03-24 12:40:52 +0000860 "%s instruction %d has a non-integer input %d whose type is: %s.",
David Brazdil13b47182015-04-15 16:29:32 +0100861 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000862 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100863 static_cast<int>(input_index),
864 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000865 }
866}
867
David Brazdilbadd8262016-02-02 16:28:56 +0000868void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
Mark Mendellfe57faa2015-09-18 09:26:15 -0400869 VisitInstruction(instruction);
870 // Check that the number of block successors matches the switch count plus
871 // one for the default block.
872 HBasicBlock* block = instruction->GetBlock();
873 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
874 AddError(StringPrintf(
875 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
876 instruction->DebugName(),
877 instruction->GetId(),
878 block->GetBlockId(),
879 instruction->GetNumEntries() + 1u,
880 block->GetSuccessors().size()));
881 }
882}
883
David Brazdilbadd8262016-02-02 16:28:56 +0000884void GraphChecker::VisitIf(HIf* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100885 VisitInstruction(instruction);
886 HandleBooleanInput(instruction, 0);
887}
888
David Brazdilbadd8262016-02-02 16:28:56 +0000889void GraphChecker::VisitSelect(HSelect* instruction) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000890 VisitInstruction(instruction);
891 HandleBooleanInput(instruction, 2);
892}
893
David Brazdilbadd8262016-02-02 16:28:56 +0000894void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100895 VisitInstruction(instruction);
896 HandleBooleanInput(instruction, 0);
897}
898
David Brazdilbadd8262016-02-02 16:28:56 +0000899void GraphChecker::VisitCondition(HCondition* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000900 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000901 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000902 AddError(StringPrintf(
903 "Condition %s %d has a non-Boolean result type: %s.",
904 op->DebugName(), op->GetId(),
905 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000906 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000907 HInstruction* lhs = op->InputAt(0);
908 HInstruction* rhs = op->InputAt(1);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000909 if (Primitive::PrimitiveKind(lhs->GetType()) != Primitive::PrimitiveKind(rhs->GetType())) {
Calin Juravlea4f88312015-04-16 12:57:19 +0100910 AddError(StringPrintf(
Roland Levillaina5c4a402016-03-15 15:02:50 +0000911 "Condition %s %d has inputs of different kinds: %s, and %s.",
Calin Juravlea4f88312015-04-16 12:57:19 +0100912 op->DebugName(), op->GetId(),
913 Primitive::PrettyDescriptor(lhs->GetType()),
914 Primitive::PrettyDescriptor(rhs->GetType())));
915 }
916 if (!op->IsEqual() && !op->IsNotEqual()) {
917 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000918 AddError(StringPrintf(
919 "Condition %s %d uses an object as left-hand side input.",
920 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100921 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000922 AddError(StringPrintf(
923 "Condition %s %d uses an object as right-hand side input.",
924 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000925 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000926 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000927}
928
Roland Levillain937e6cd2016-03-22 11:54:37 +0000929void GraphChecker::VisitNeg(HNeg* instruction) {
930 VisitInstruction(instruction);
931 Primitive::Type input_type = instruction->InputAt(0)->GetType();
932 Primitive::Type result_type = instruction->GetType();
933 if (result_type != Primitive::PrimitiveKind(input_type)) {
934 AddError(StringPrintf("Binary operation %s %d has a result type different "
935 "from its input kind: %s vs %s.",
936 instruction->DebugName(), instruction->GetId(),
937 Primitive::PrettyDescriptor(result_type),
938 Primitive::PrettyDescriptor(input_type)));
939 }
940}
941
David Brazdilbadd8262016-02-02 16:28:56 +0000942void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000943 VisitInstruction(op);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000944 Primitive::Type lhs_type = op->InputAt(0)->GetType();
945 Primitive::Type rhs_type = op->InputAt(1)->GetType();
946 Primitive::Type result_type = op->GetType();
Roland Levillain5b5b9312016-03-22 14:57:31 +0000947
948 // Type consistency between inputs.
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000949 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000950 if (Primitive::PrimitiveKind(rhs_type) != Primitive::kPrimInt) {
Roland Levillain5b5b9312016-03-22 14:57:31 +0000951 AddError(StringPrintf("Shift/rotate operation %s %d has a non-int kind second input: "
952 "%s of type %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +0000953 op->DebugName(), op->GetId(),
954 op->InputAt(1)->DebugName(),
955 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000956 }
957 } else {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000958 if (Primitive::PrimitiveKind(lhs_type) != Primitive::PrimitiveKind(rhs_type)) {
959 AddError(StringPrintf("Binary operation %s %d has inputs of different kinds: %s, and %s.",
960 op->DebugName(), op->GetId(),
961 Primitive::PrettyDescriptor(lhs_type),
962 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000963 }
964 }
965
Roland Levillain5b5b9312016-03-22 14:57:31 +0000966 // Type consistency between result and input(s).
Nicolas Geoffray31596742014-11-24 15:28:45 +0000967 if (op->IsCompare()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000968 if (result_type != Primitive::kPrimInt) {
969 AddError(StringPrintf("Compare operation %d has a non-int result type: %s.",
970 op->GetId(),
971 Primitive::PrettyDescriptor(result_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000972 }
Roland Levillain5b5b9312016-03-22 14:57:31 +0000973 } else if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
974 // Only check the first input (value), as the second one (distance)
975 // must invariably be of kind `int`.
976 if (result_type != Primitive::PrimitiveKind(lhs_type)) {
977 AddError(StringPrintf("Shift/rotate operation %s %d has a result type different "
978 "from its left-hand side (value) input kind: %s vs %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +0000979 op->DebugName(), op->GetId(),
980 Primitive::PrettyDescriptor(result_type),
981 Primitive::PrettyDescriptor(lhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000982 }
Roland Levillain5b5b9312016-03-22 14:57:31 +0000983 } else {
984 if (Primitive::PrimitiveKind(result_type) != Primitive::PrimitiveKind(lhs_type)) {
985 AddError(StringPrintf("Binary operation %s %d has a result kind different "
986 "from its left-hand side input kind: %s vs %s.",
987 op->DebugName(), op->GetId(),
988 Primitive::PrettyDescriptor(result_type),
989 Primitive::PrettyDescriptor(lhs_type)));
990 }
991 if (Primitive::PrimitiveKind(result_type) != Primitive::PrimitiveKind(rhs_type)) {
992 AddError(StringPrintf("Binary operation %s %d has a result kind different "
993 "from its right-hand side input kind: %s vs %s.",
994 op->DebugName(), op->GetId(),
995 Primitive::PrettyDescriptor(result_type),
996 Primitive::PrettyDescriptor(rhs_type)));
997 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000998 }
999}
1000
David Brazdilbadd8262016-02-02 16:28:56 +00001001void GraphChecker::VisitConstant(HConstant* instruction) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001002 HBasicBlock* block = instruction->GetBlock();
1003 if (!block->IsEntryBlock()) {
1004 AddError(StringPrintf(
1005 "%s %d should be in the entry block but is in block %d.",
1006 instruction->DebugName(),
1007 instruction->GetId(),
1008 block->GetBlockId()));
1009 }
1010}
1011
David Brazdilbadd8262016-02-02 16:28:56 +00001012void GraphChecker::VisitBoundType(HBoundType* instruction) {
David Brazdilf5552582015-12-27 13:36:12 +00001013 VisitInstruction(instruction);
1014
David Brazdilf5552582015-12-27 13:36:12 +00001015 if (!instruction->GetUpperBound().IsValid()) {
1016 AddError(StringPrintf(
1017 "%s %d does not have a valid upper bound RTI.",
1018 instruction->DebugName(),
1019 instruction->GetId()));
1020 }
1021}
1022
Roland Levillainf355c3f2016-03-30 19:09:03 +01001023void GraphChecker::VisitTypeConversion(HTypeConversion* instruction) {
1024 VisitInstruction(instruction);
1025 Primitive::Type result_type = instruction->GetResultType();
1026 Primitive::Type input_type = instruction->GetInputType();
1027 // Invariant: We should never generate a conversion to a Boolean value.
1028 if (result_type == Primitive::kPrimBoolean) {
1029 AddError(StringPrintf(
1030 "%s %d converts to a %s (from a %s).",
1031 instruction->DebugName(),
1032 instruction->GetId(),
1033 Primitive::PrettyDescriptor(result_type),
1034 Primitive::PrettyDescriptor(input_type)));
1035 }
1036}
1037
Roland Levillainccc07a92014-09-16 14:48:16 +01001038} // namespace art