blob: c8cba205fd1f9eb3356aa7d0aaa9aa21551d3ee6 [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
David Brazdil86ea7ee2016-02-16 09:26:07 +000030static bool IsAllowedToJumpToExitBlock(HInstruction* instruction) {
31 return instruction->IsThrow() || instruction->IsReturn() || instruction->IsReturnVoid();
32}
33
34static bool IsExitTryBoundaryIntoExitBlock(HBasicBlock* block) {
35 if (!block->IsSingleTryBoundary()) {
36 return false;
37 }
38
39 HTryBoundary* boundary = block->GetLastInstruction()->AsTryBoundary();
40 return block->GetPredecessors().size() == 1u &&
41 boundary->GetNormalFlowSuccessor()->IsExitBlock() &&
42 !boundary->IsEntry();
43}
44
Roland Levillainccc07a92014-09-16 14:48:16 +010045void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
46 current_block_ = block;
47
48 // Check consistency with respect to predecessors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000049 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +000050 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
51 ArenaVector<HBasicBlock*>& sorted_predecessors = blocks_storage_;
52 sorted_predecessors.assign(block->GetPredecessors().begin(), block->GetPredecessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +000053 std::sort(sorted_predecessors.begin(), sorted_predecessors.end());
54 for (auto it = sorted_predecessors.begin(), end = sorted_predecessors.end(); it != end; ) {
55 HBasicBlock* p = *it++;
56 size_t p_count_in_block_predecessors = 1u;
57 for (; it != end && *it == p; ++it) {
58 ++p_count_in_block_predecessors;
Vladimir Marko655e5852015-10-12 10:38:28 +010059 }
Vladimir Marko655e5852015-10-12 10:38:28 +010060 size_t block_count_in_p_successors =
61 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010062 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000063 AddError(StringPrintf(
64 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
65 "block %d lists %zu occurrences of block %d in its successors.",
66 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
67 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010068 }
69 }
70
71 // Check consistency with respect to successors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000072 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +000073 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
74 ArenaVector<HBasicBlock*>& sorted_successors = blocks_storage_;
75 sorted_successors.assign(block->GetSuccessors().begin(), block->GetSuccessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +000076 std::sort(sorted_successors.begin(), sorted_successors.end());
77 for (auto it = sorted_successors.begin(), end = sorted_successors.end(); it != end; ) {
78 HBasicBlock* s = *it++;
79 size_t s_count_in_block_successors = 1u;
80 for (; it != end && *it == s; ++it) {
81 ++s_count_in_block_successors;
Vladimir Marko655e5852015-10-12 10:38:28 +010082 }
Vladimir Marko655e5852015-10-12 10:38:28 +010083 size_t block_count_in_s_predecessors =
84 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010085 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000086 AddError(StringPrintf(
87 "Block %d lists %zu occurrences of block %d in its successors, whereas "
88 "block %d lists %zu occurrences of block %d in its predecessors.",
89 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
90 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010091 }
92 }
93
94 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000095 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
96 // dead code that falls out of the method will not end with a control-flow
97 // instruction. Such code is removed during the SSA-building DCE phase.
98 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000099 AddError(StringPrintf("Block %d does not end with a branch instruction.",
100 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100101 }
102
David Brazdil86ea7ee2016-02-16 09:26:07 +0000103 // Ensure that only Return(Void) and Throw jump to Exit. An exiting TryBoundary
104 // may be between the instructions if the Throw/Return(Void) is in a try block.
David Brazdilb618ade2015-07-29 10:31:29 +0100105 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +0000106 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000107 HInstruction* last_instruction = IsExitTryBoundaryIntoExitBlock(predecessor) ?
108 predecessor->GetSinglePredecessor()->GetLastInstruction() :
109 predecessor->GetLastInstruction();
110 if (!IsAllowedToJumpToExitBlock(last_instruction)) {
111 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
112 last_instruction->DebugName(),
113 last_instruction->GetId()));
David Brazdilb618ade2015-07-29 10:31:29 +0100114 }
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) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000183 if (IsExitTryBoundaryIntoExitBlock(block)) {
184 // Allowed critical edge (Throw/Return/ReturnVoid)->TryBoundary->Exit.
185 } else {
186 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
187 if (successor->GetPredecessors().size() > 1) {
188 AddError(StringPrintf("Critical edge between blocks %d and %d.",
189 block->GetBlockId(),
190 successor->GetBlockId()));
191 }
David Brazdilbadd8262016-02-02 16:28:56 +0000192 }
193 }
194 }
195
196 // Ensure try membership information is consistent.
197 if (block->IsCatchBlock()) {
198 if (block->IsTryBlock()) {
199 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
200 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
201 "has try entry %s:%d.",
202 block->GetBlockId(),
203 try_entry.DebugName(),
204 try_entry.GetId()));
205 }
206
207 if (block->IsLoopHeader()) {
208 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
209 block->GetBlockId()));
210 }
211 } else {
212 for (HBasicBlock* predecessor : block->GetPredecessors()) {
213 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
214 if (block->IsTryBlock()) {
215 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
216 if (incoming_try_entry == nullptr) {
217 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
218 "from predecessor %d.",
219 block->GetBlockId(),
220 stored_try_entry.DebugName(),
221 stored_try_entry.GetId(),
222 predecessor->GetBlockId()));
223 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
224 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
225 "with %s:%d that follows from predecessor %d.",
226 block->GetBlockId(),
227 stored_try_entry.DebugName(),
228 stored_try_entry.GetId(),
229 incoming_try_entry->DebugName(),
230 incoming_try_entry->GetId(),
231 predecessor->GetBlockId()));
232 }
233 } else if (incoming_try_entry != nullptr) {
234 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
235 "from predecessor %d.",
236 block->GetBlockId(),
237 incoming_try_entry->DebugName(),
238 incoming_try_entry->GetId(),
239 predecessor->GetBlockId()));
240 }
241 }
242 }
243
244 if (block->IsLoopHeader()) {
245 HandleLoop(block);
246 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100247}
248
Mark Mendell1152c922015-04-24 17:06:35 -0400249void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
250 if (!GetGraph()->HasBoundsChecks()) {
251 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
252 "but HasBoundsChecks() returns false",
253 check->DebugName(),
254 check->GetId()));
255 }
256
257 // Perform the instruction base checks too.
258 VisitInstruction(check);
259}
260
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100261void GraphChecker::VisitDeoptimize(HDeoptimize* deopt) {
262 if (GetGraph()->IsCompilingOsr()) {
263 AddError(StringPrintf("A graph compiled OSR cannot have a HDeoptimize instruction"));
264 }
265
266 // Perform the instruction base checks too.
267 VisitInstruction(deopt);
268}
269
David Brazdilffee3d32015-07-06 11:48:53 +0100270void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000271 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
272
273 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100274 // Note that a normal-flow successor may be a catch block before CFG
David Brazdilbadd8262016-02-02 16:28:56 +0000275 // simplification. We only test normal-flow successors in GraphChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000276 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100277 if (!handler->IsCatchBlock()) {
278 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
279 "is not a catch block.",
280 current_block_->GetBlockId(),
281 try_boundary->DebugName(),
282 try_boundary->GetId(),
283 handler->GetBlockId()));
284 }
David Brazdild26a4112015-11-10 11:07:31 +0000285 }
286
287 // Ensure that handlers are not listed multiple times.
288 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000289 if (ContainsElement(handlers, handlers[i], i + 1)) {
290 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000291 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100292 try_boundary->DebugName(),
293 try_boundary->GetId()));
294 }
295 }
296
297 VisitInstruction(try_boundary);
298}
299
David Brazdil9bc43612015-11-05 21:25:24 +0000300void GraphChecker::VisitLoadException(HLoadException* load) {
301 // Ensure that LoadException is the first instruction in a catch block.
302 if (!load->GetBlock()->IsCatchBlock()) {
303 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
304 load->DebugName(),
305 load->GetId(),
306 load->GetBlock()->GetBlockId()));
307 } else if (load->GetBlock()->GetFirstInstruction() != load) {
308 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
309 load->DebugName(),
310 load->GetId(),
311 load->GetBlock()->GetBlockId()));
312 }
313}
314
Roland Levillainccc07a92014-09-16 14:48:16 +0100315void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000316 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000317 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
318 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000319 } else {
320 seen_ids_.SetBit(instruction->GetId());
321 }
322
Roland Levillainccc07a92014-09-16 14:48:16 +0100323 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000324 if (instruction->GetBlock() == nullptr) {
325 AddError(StringPrintf("%s %d in block %d not associated with any block.",
326 instruction->IsPhi() ? "Phi" : "Instruction",
327 instruction->GetId(),
328 current_block_->GetBlockId()));
329 } else if (instruction->GetBlock() != current_block_) {
330 AddError(StringPrintf("%s %d in block %d associated with block %d.",
331 instruction->IsPhi() ? "Phi" : "Instruction",
332 instruction->GetId(),
333 current_block_->GetBlockId(),
334 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100335 }
Roland Levillain6b469232014-09-25 10:10:38 +0100336
337 // Ensure the inputs of `instruction` are defined in a block of the graph.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100338 for (HInstruction* input : instruction->GetInputs()) {
Roland Levillain6b469232014-09-25 10:10:38 +0100339 const HInstructionList& list = input->IsPhi()
340 ? input->GetBlock()->GetPhis()
341 : input->GetBlock()->GetInstructions();
342 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000343 AddError(StringPrintf("Input %d of instruction %d is not defined "
344 "in a basic block of the control-flow graph.",
345 input->GetId(),
346 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100347 }
348 }
349
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100350 // Ensure the uses of `instruction` are defined in a block of the graph,
351 // and the entry in the use list is consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100352 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
353 HInstruction* user = use.GetUser();
354 const HInstructionList& list = user->IsPhi()
355 ? user->GetBlock()->GetPhis()
356 : user->GetBlock()->GetInstructions();
357 if (!list.Contains(user)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000358 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000359 "in a basic block of the control-flow graph.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100360 user->DebugName(),
361 user->GetId(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000362 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100363 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100364 size_t use_index = use.GetIndex();
Vladimir Markoe9004912016-06-16 16:50:52 +0100365 HConstInputsRef user_inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100366 if ((use_index >= user_inputs.size()) || (user_inputs[use_index] != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000367 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100368 "UseListNode index.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100369 user->DebugName(),
370 user->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000371 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100372 instruction->GetId()));
373 }
374 }
375
376 // Ensure the environment uses entries are consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100377 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
378 HEnvironment* user = use.GetUser();
379 size_t use_index = use.GetIndex();
380 if ((use_index >= user->Size()) || (user->GetInstructionAt(use_index) != instruction)) {
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100381 AddError(StringPrintf("Environment user of %s:%d has a wrong "
382 "UseListNode index.",
383 instruction->DebugName(),
384 instruction->GetId()));
385 }
Roland Levillain6b469232014-09-25 10:10:38 +0100386 }
David Brazdil1abb4192015-02-17 18:33:36 +0000387
388 // Ensure 'instruction' has pointers to its inputs' use entries.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100389 auto&& input_records = instruction->GetInputRecords();
390 for (size_t i = 0; i < input_records.size(); ++i) {
391 const HUserRecord<HInstruction*>& input_record = input_records[i];
David Brazdil1abb4192015-02-17 18:33:36 +0000392 HInstruction* input = input_record.GetInstruction();
Vladimir Marko46817b82016-03-29 12:21:58 +0100393 if ((input_record.GetBeforeUseNode() == input->GetUses().end()) ||
394 (input_record.GetUseNode() == input->GetUses().end()) ||
395 !input->GetUses().ContainsNode(*input_record.GetUseNode()) ||
396 (input_record.GetUseNode()->GetIndex() != i)) {
397 AddError(StringPrintf("Instruction %s:%d has an invalid iterator before use entry "
David Brazdil1abb4192015-02-17 18:33:36 +0000398 "at input %u (%s:%d).",
399 instruction->DebugName(),
400 instruction->GetId(),
401 static_cast<unsigned>(i),
402 input->DebugName(),
403 input->GetId()));
404 }
405 }
David Brazdilbadd8262016-02-02 16:28:56 +0000406
407 // Ensure an instruction dominates all its uses.
Vladimir Marko46817b82016-03-29 12:21:58 +0100408 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
409 HInstruction* user = use.GetUser();
410 if (!user->IsPhi() && !instruction->StrictlyDominates(user)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000411 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
412 "use %s:%d in block %d.",
413 instruction->DebugName(),
414 instruction->GetId(),
415 current_block_->GetBlockId(),
Vladimir Marko46817b82016-03-29 12:21:58 +0100416 user->DebugName(),
417 user->GetId(),
418 user->GetBlock()->GetBlockId()));
David Brazdilbadd8262016-02-02 16:28:56 +0000419 }
420 }
421
422 if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) {
423 AddError(StringPrintf("Instruction %s:%d in block %d requires an environment "
424 "but does not have one.",
425 instruction->DebugName(),
426 instruction->GetId(),
427 current_block_->GetBlockId()));
428 }
429
430 // Ensure an instruction having an environment is dominated by the
431 // instructions contained in the environment.
432 for (HEnvironment* environment = instruction->GetEnvironment();
433 environment != nullptr;
434 environment = environment->GetParent()) {
435 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
436 HInstruction* env_instruction = environment->GetInstructionAt(i);
437 if (env_instruction != nullptr
438 && !env_instruction->StrictlyDominates(instruction)) {
439 AddError(StringPrintf("Instruction %d in environment of instruction %d "
440 "from block %d does not dominate instruction %d.",
441 env_instruction->GetId(),
442 instruction->GetId(),
443 current_block_->GetBlockId(),
444 instruction->GetId()));
445 }
446 }
447 }
448
449 // Ensure that reference type instructions have reference type info.
450 if (instruction->GetType() == Primitive::kPrimNot) {
451 ScopedObjectAccess soa(Thread::Current());
452 if (!instruction->GetReferenceTypeInfo().IsValid()) {
453 AddError(StringPrintf("Reference type instruction %s:%d does not have "
454 "valid reference type information.",
455 instruction->DebugName(),
456 instruction->GetId()));
457 }
458 }
459
460 if (instruction->CanThrowIntoCatchBlock()) {
461 // Find the top-level environment. This corresponds to the environment of
462 // the catch block since we do not inline methods with try/catch.
463 HEnvironment* environment = instruction->GetEnvironment();
464 while (environment->GetParent() != nullptr) {
465 environment = environment->GetParent();
466 }
467
468 // Find all catch blocks and test that `instruction` has an environment
469 // value for each one.
470 const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
471 for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) {
472 for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
473 HPhi* catch_phi = phi_it.Current()->AsPhi();
474 if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) {
475 AddError(StringPrintf("Instruction %s:%d throws into catch block %d "
476 "with catch phi %d for vreg %d but its "
477 "corresponding environment slot is empty.",
478 instruction->DebugName(),
479 instruction->GetId(),
480 catch_block->GetBlockId(),
481 catch_phi->GetId(),
482 catch_phi->GetRegNumber()));
483 }
484 }
485 }
486 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100487}
488
Roland Levillain4c0eb422015-04-24 16:43:49 +0100489void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
490 VisitInstruction(invoke);
491
492 if (invoke->IsStaticWithExplicitClinitCheck()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100493 const HInstruction* last_input = invoke->GetInputs().back();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100494 if (last_input == nullptr) {
495 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
496 "has a null pointer as last input.",
497 invoke->DebugName(),
498 invoke->GetId()));
499 }
500 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
501 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
502 "has a last instruction (%s:%d) which is neither a clinit check "
503 "nor a load class instruction.",
504 invoke->DebugName(),
505 invoke->GetId(),
506 last_input->DebugName(),
507 last_input->GetId()));
508 }
509 }
510}
511
David Brazdilfc6a86a2015-06-26 10:33:45 +0000512void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100513 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000514 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
515 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000516 AddError(StringPrintf("%s:%d does not jump to the exit block.",
517 ret->DebugName(),
518 ret->GetId()));
519 }
520}
521
522void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100523 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000524 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
525 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000526 AddError(StringPrintf("%s:%d does not jump to the exit block.",
527 ret->DebugName(),
528 ret->GetId()));
529 }
530}
531
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100532void GraphChecker::VisitCheckCast(HCheckCast* check) {
533 VisitInstruction(check);
534 HInstruction* input = check->InputAt(1);
535 if (!input->IsLoadClass()) {
536 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
537 check->DebugName(),
538 check->GetId(),
539 input->DebugName(),
540 input->GetId()));
541 }
542}
543
544void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
545 VisitInstruction(instruction);
546 HInstruction* input = instruction->InputAt(1);
547 if (!input->IsLoadClass()) {
548 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
549 instruction->DebugName(),
550 instruction->GetId(),
551 input->DebugName(),
552 input->GetId()));
553 }
554}
555
David Brazdilbadd8262016-02-02 16:28:56 +0000556void GraphChecker::HandleLoop(HBasicBlock* loop_header) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100557 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100558 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100559
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000560 if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
David Brazdildb51efb2015-11-06 01:36:20 +0000561 AddError(StringPrintf(
562 "Loop pre-header %d of loop defined by header %d has %zu successors.",
563 loop_information->GetPreHeader()->GetBlockId(),
564 id,
565 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100566 }
567
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000568 if (loop_information->GetSuspendCheck() == nullptr) {
569 AddError(StringPrintf(
570 "Loop with header %d does not have a suspend check.",
571 loop_header->GetBlockId()));
572 }
573
574 if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) {
575 AddError(StringPrintf(
576 "Loop header %d does not have the loop suspend check as the first instruction.",
577 loop_header->GetBlockId()));
578 }
579
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100580 // Ensure the loop header has only one incoming branch and the remaining
581 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000582 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000583 if (num_preds < 2) {
584 AddError(StringPrintf(
585 "Loop header %d has less than two predecessors: %zu.",
586 id,
587 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100588 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100589 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000590 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000591 AddError(StringPrintf(
592 "First predecessor of loop header %d is a back edge.",
593 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100594 }
Vladimir Marko60584552015-09-03 13:35:12 +0000595 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100596 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100597 if (!loop_information->IsBackEdge(*predecessor)) {
598 AddError(StringPrintf(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000599 "Loop header %d has multiple incoming (non back edge) blocks: %d.",
600 id,
601 predecessor->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100602 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100603 }
604 }
605
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100606 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100607
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100608 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100609 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000610 AddError(StringPrintf(
611 "Loop defined by header %d has no back edge.",
612 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100613 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100614 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
615 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100616 if (!loop_blocks.IsBitSet(back_edge_id)) {
617 AddError(StringPrintf(
618 "Loop defined by header %d has an invalid back edge %d.",
619 id,
620 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000621 } else if (back_edge->GetLoopInformation() != loop_information) {
622 AddError(StringPrintf(
623 "Back edge %d of loop defined by header %d belongs to nested loop "
624 "with header %d.",
625 back_edge_id,
626 id,
627 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100628 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100629 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100630 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100631
David Brazdil7d275372015-04-21 16:36:35 +0100632 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
633 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
634 HLoopInformation* outer_info = it.Current();
635 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
636 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
637 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100638 id,
David Brazdil7d275372015-04-21 16:36:35 +0100639 outer_info->GetHeader()->GetBlockId()));
640 }
641 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000642
643 // Ensure the pre-header block is first in the list of predecessors of a loop
644 // header and that the header block is its only successor.
645 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
646 AddError(StringPrintf(
647 "Loop pre-header is not the first predecessor of the loop header %d.",
648 id));
649 }
650
651 // Ensure all blocks in the loop are live and dominated by the loop header in
652 // the case of natural loops.
653 for (uint32_t i : loop_blocks.Indexes()) {
654 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
655 if (loop_block == nullptr) {
656 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
657 id,
658 i));
659 } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) {
660 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
661 i,
662 id));
663 }
664 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100665}
666
Vladimir Markoe9004912016-06-16 16:50:52 +0100667static bool IsSameSizeConstant(const HInstruction* insn1, const HInstruction* insn2) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000668 return insn1->IsConstant()
669 && insn2->IsConstant()
670 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
671}
672
Vladimir Markoe9004912016-06-16 16:50:52 +0100673static bool IsConstantEquivalent(const HInstruction* insn1,
674 const HInstruction* insn2,
675 BitVector* visited) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000676 if (insn1->IsPhi() &&
Vladimir Marko372f10e2016-05-17 16:30:10 +0100677 insn1->AsPhi()->IsVRegEquivalentOf(insn2)) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100678 HConstInputsRef insn1_inputs = insn1->GetInputs();
679 HConstInputsRef insn2_inputs = insn2->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100680 if (insn1_inputs.size() != insn2_inputs.size()) {
681 return false;
682 }
683
David Brazdil77a48ae2015-09-15 12:34:04 +0000684 // Testing only one of the two inputs for recursion is sufficient.
685 if (visited->IsBitSet(insn1->GetId())) {
686 return true;
687 }
688 visited->SetBit(insn1->GetId());
689
Vladimir Marko372f10e2016-05-17 16:30:10 +0100690 for (size_t i = 0; i < insn1_inputs.size(); ++i) {
691 if (!IsConstantEquivalent(insn1_inputs[i], insn2_inputs[i], visited)) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000692 return false;
693 }
694 }
695 return true;
696 } else if (IsSameSizeConstant(insn1, insn2)) {
697 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
698 } else {
699 return false;
700 }
701}
702
David Brazdilbadd8262016-02-02 16:28:56 +0000703void GraphChecker::VisitPhi(HPhi* phi) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100704 VisitInstruction(phi);
705
706 // Ensure the first input of a phi is not itself.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100707 ArrayRef<HUserRecord<HInstruction*>> input_records = phi->GetInputRecords();
708 if (input_records[0].GetInstruction() == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000709 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
710 phi->GetId(),
711 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100712 }
713
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000714 // Ensure that the inputs have the same primitive kind as the phi.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100715 for (size_t i = 0; i < input_records.size(); ++i) {
716 HInstruction* input = input_records[i].GetInstruction();
Roland Levillaina5c4a402016-03-15 15:02:50 +0000717 if (Primitive::PrimitiveKind(input->GetType()) != Primitive::PrimitiveKind(phi->GetType())) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000718 AddError(StringPrintf(
719 "Input %d at index %zu of phi %d from block %d does not have the "
Roland Levillaina5c4a402016-03-15 15:02:50 +0000720 "same kind as the phi: %s versus %s",
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000721 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
722 Primitive::PrettyDescriptor(input->GetType()),
723 Primitive::PrettyDescriptor(phi->GetType())));
724 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000725 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000726 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
727 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
728 phi->GetId(),
729 phi->GetBlock()->GetBlockId(),
730 Primitive::PrettyDescriptor(phi->GetType())));
731 }
David Brazdilffee3d32015-07-06 11:48:53 +0100732
733 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100734 // The number of inputs of a catch phi should be the total number of throwing
735 // instructions caught by this catch block. We do not enforce this, however,
736 // because we do not remove the corresponding inputs when we prove that an
737 // instruction cannot throw. Instead, we at least test that all phis have the
738 // same, non-zero number of inputs (b/24054676).
Vladimir Marko372f10e2016-05-17 16:30:10 +0100739 if (input_records.empty()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100740 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
741 phi->GetId(),
742 phi->GetBlock()->GetBlockId()));
743 } else {
744 HInstruction* next_phi = phi->GetNext();
745 if (next_phi != nullptr) {
746 size_t input_count_next = next_phi->InputCount();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100747 if (input_records.size() != input_count_next) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100748 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
749 "but phi %d has %zu inputs.",
750 phi->GetId(),
751 phi->GetBlock()->GetBlockId(),
Vladimir Marko372f10e2016-05-17 16:30:10 +0100752 input_records.size(),
David Brazdil3eaa32f2015-09-18 10:58:32 +0100753 next_phi->GetId(),
754 input_count_next));
755 }
756 }
757 }
David Brazdilffee3d32015-07-06 11:48:53 +0100758 } else {
759 // Ensure the number of inputs of a non-catch phi is the same as the number
760 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000761 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100762 if (input_records.size() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100763 AddError(StringPrintf(
764 "Phi %d in block %d has %zu inputs, "
765 "but block %d has %zu predecessors.",
Vladimir Marko372f10e2016-05-17 16:30:10 +0100766 phi->GetId(), phi->GetBlock()->GetBlockId(), input_records.size(),
Vladimir Marko60584552015-09-03 13:35:12 +0000767 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100768 } else {
769 // Ensure phi input at index I either comes from the Ith
770 // predecessor or from a block that dominates this predecessor.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100771 for (size_t i = 0; i < input_records.size(); ++i) {
772 HInstruction* input = input_records[i].GetInstruction();
Vladimir Marko60584552015-09-03 13:35:12 +0000773 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100774 if (!(input->GetBlock() == predecessor
775 || input->GetBlock()->Dominates(predecessor))) {
776 AddError(StringPrintf(
777 "Input %d at index %zu of phi %d from block %d is not defined in "
778 "predecessor number %zu nor in a block dominating it.",
779 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
780 i));
781 }
782 }
783 }
784 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000785
786 // Ensure that catch phis are sorted by their vreg number, as required by
787 // the register allocator and code generator. This does not apply to normal
788 // phis which can be constructed artifically.
789 if (phi->IsCatchPhi()) {
790 HInstruction* next_phi = phi->GetNext();
791 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
792 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
793 "vreg numbers.",
794 phi->GetId(),
795 next_phi->GetId(),
796 phi->GetBlock()->GetBlockId()));
797 }
798 }
799
Aart Bik3fc7f352015-11-20 22:03:03 -0800800 // Test phi equivalents. There should not be two of the same type and they should only be
801 // created for constants which were untyped in DEX. Note that this test can be skipped for
802 // a synthetic phi (indicated by lack of a virtual register).
803 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800804 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
805 !phi_it.Done();
806 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800807 HPhi* other_phi = phi_it.Current()->AsPhi();
808 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
809 if (phi->GetType() == other_phi->GetType()) {
810 std::stringstream type_str;
811 type_str << phi->GetType();
812 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000813 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800814 phi->GetRegNumber(),
815 type_str.str().c_str()));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000816 } else if (phi->GetType() == Primitive::kPrimNot) {
817 std::stringstream type_str;
818 type_str << other_phi->GetType();
819 AddError(StringPrintf(
820 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
821 phi->GetId(),
822 phi->GetRegNumber(),
823 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800824 } else {
Vladimir Marko947eb702016-03-25 15:31:35 +0000825 // If we get here, make sure we allocate all the necessary storage at once
826 // because the BitVector reallocation strategy has very bad worst-case behavior.
827 ArenaBitVector& visited = visited_storage_;
828 visited.SetBit(GetGraph()->GetCurrentInstructionId());
829 visited.ClearAllBits();
Aart Bik3fc7f352015-11-20 22:03:03 -0800830 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
831 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
832 "are not equivalents of constants.",
833 phi->GetId(),
834 other_phi->GetId(),
835 phi->GetRegNumber()));
836 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000837 }
838 }
839 }
840 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000841}
842
David Brazdilbadd8262016-02-02 16:28:56 +0000843void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
David Brazdil13b47182015-04-15 16:29:32 +0100844 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000845 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100846 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000847 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000848 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100849 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
850 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000851 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100852 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000853 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000854 }
David Brazdil11edec72016-03-24 12:40:52 +0000855 } else if (Primitive::PrimitiveKind(input->GetType()) != Primitive::kPrimInt) {
856 // TODO: We need a data-flow analysis to determine if an input like Phi,
857 // Select or a binary operation is actually Boolean. Allow for now.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000858 AddError(StringPrintf(
David Brazdil11edec72016-03-24 12:40:52 +0000859 "%s instruction %d has a non-integer input %d whose type is: %s.",
David Brazdil13b47182015-04-15 16:29:32 +0100860 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000861 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100862 static_cast<int>(input_index),
863 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000864 }
865}
866
David Brazdilbadd8262016-02-02 16:28:56 +0000867void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
Mark Mendellfe57faa2015-09-18 09:26:15 -0400868 VisitInstruction(instruction);
869 // Check that the number of block successors matches the switch count plus
870 // one for the default block.
871 HBasicBlock* block = instruction->GetBlock();
872 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
873 AddError(StringPrintf(
874 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
875 instruction->DebugName(),
876 instruction->GetId(),
877 block->GetBlockId(),
878 instruction->GetNumEntries() + 1u,
879 block->GetSuccessors().size()));
880 }
881}
882
David Brazdilbadd8262016-02-02 16:28:56 +0000883void GraphChecker::VisitIf(HIf* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100884 VisitInstruction(instruction);
885 HandleBooleanInput(instruction, 0);
886}
887
David Brazdilbadd8262016-02-02 16:28:56 +0000888void GraphChecker::VisitSelect(HSelect* instruction) {
David Brazdil74eb1b22015-12-14 11:44:01 +0000889 VisitInstruction(instruction);
890 HandleBooleanInput(instruction, 2);
891}
892
David Brazdilbadd8262016-02-02 16:28:56 +0000893void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100894 VisitInstruction(instruction);
895 HandleBooleanInput(instruction, 0);
896}
897
David Brazdilbadd8262016-02-02 16:28:56 +0000898void GraphChecker::VisitCondition(HCondition* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000899 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000900 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000901 AddError(StringPrintf(
902 "Condition %s %d has a non-Boolean result type: %s.",
903 op->DebugName(), op->GetId(),
904 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000905 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000906 HInstruction* lhs = op->InputAt(0);
907 HInstruction* rhs = op->InputAt(1);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000908 if (Primitive::PrimitiveKind(lhs->GetType()) != Primitive::PrimitiveKind(rhs->GetType())) {
Calin Juravlea4f88312015-04-16 12:57:19 +0100909 AddError(StringPrintf(
Roland Levillaina5c4a402016-03-15 15:02:50 +0000910 "Condition %s %d has inputs of different kinds: %s, and %s.",
Calin Juravlea4f88312015-04-16 12:57:19 +0100911 op->DebugName(), op->GetId(),
912 Primitive::PrettyDescriptor(lhs->GetType()),
913 Primitive::PrettyDescriptor(rhs->GetType())));
914 }
915 if (!op->IsEqual() && !op->IsNotEqual()) {
916 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000917 AddError(StringPrintf(
918 "Condition %s %d uses an object as left-hand side input.",
919 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100920 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000921 AddError(StringPrintf(
922 "Condition %s %d uses an object as right-hand side input.",
923 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000924 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000925 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000926}
927
Roland Levillain937e6cd2016-03-22 11:54:37 +0000928void GraphChecker::VisitNeg(HNeg* instruction) {
929 VisitInstruction(instruction);
930 Primitive::Type input_type = instruction->InputAt(0)->GetType();
931 Primitive::Type result_type = instruction->GetType();
932 if (result_type != Primitive::PrimitiveKind(input_type)) {
933 AddError(StringPrintf("Binary operation %s %d has a result type different "
934 "from its input kind: %s vs %s.",
935 instruction->DebugName(), instruction->GetId(),
936 Primitive::PrettyDescriptor(result_type),
937 Primitive::PrettyDescriptor(input_type)));
938 }
939}
940
David Brazdilbadd8262016-02-02 16:28:56 +0000941void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000942 VisitInstruction(op);
Roland Levillaina5c4a402016-03-15 15:02:50 +0000943 Primitive::Type lhs_type = op->InputAt(0)->GetType();
944 Primitive::Type rhs_type = op->InputAt(1)->GetType();
945 Primitive::Type result_type = op->GetType();
Roland Levillain5b5b9312016-03-22 14:57:31 +0000946
947 // Type consistency between inputs.
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000948 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000949 if (Primitive::PrimitiveKind(rhs_type) != Primitive::kPrimInt) {
Roland Levillain5b5b9312016-03-22 14:57:31 +0000950 AddError(StringPrintf("Shift/rotate operation %s %d has a non-int kind second input: "
951 "%s of type %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +0000952 op->DebugName(), op->GetId(),
953 op->InputAt(1)->DebugName(),
954 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000955 }
956 } else {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000957 if (Primitive::PrimitiveKind(lhs_type) != Primitive::PrimitiveKind(rhs_type)) {
958 AddError(StringPrintf("Binary operation %s %d has inputs of different kinds: %s, and %s.",
959 op->DebugName(), op->GetId(),
960 Primitive::PrettyDescriptor(lhs_type),
961 Primitive::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000962 }
963 }
964
Roland Levillain5b5b9312016-03-22 14:57:31 +0000965 // Type consistency between result and input(s).
Nicolas Geoffray31596742014-11-24 15:28:45 +0000966 if (op->IsCompare()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +0000967 if (result_type != Primitive::kPrimInt) {
968 AddError(StringPrintf("Compare operation %d has a non-int result type: %s.",
969 op->GetId(),
970 Primitive::PrettyDescriptor(result_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000971 }
Roland Levillain5b5b9312016-03-22 14:57:31 +0000972 } else if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
973 // Only check the first input (value), as the second one (distance)
974 // must invariably be of kind `int`.
975 if (result_type != Primitive::PrimitiveKind(lhs_type)) {
976 AddError(StringPrintf("Shift/rotate operation %s %d has a result type different "
977 "from its left-hand side (value) input kind: %s vs %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +0000978 op->DebugName(), op->GetId(),
979 Primitive::PrettyDescriptor(result_type),
980 Primitive::PrettyDescriptor(lhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000981 }
Roland Levillain5b5b9312016-03-22 14:57:31 +0000982 } else {
983 if (Primitive::PrimitiveKind(result_type) != Primitive::PrimitiveKind(lhs_type)) {
984 AddError(StringPrintf("Binary operation %s %d has a result kind different "
985 "from its left-hand side input kind: %s vs %s.",
986 op->DebugName(), op->GetId(),
987 Primitive::PrettyDescriptor(result_type),
988 Primitive::PrettyDescriptor(lhs_type)));
989 }
990 if (Primitive::PrimitiveKind(result_type) != Primitive::PrimitiveKind(rhs_type)) {
991 AddError(StringPrintf("Binary operation %s %d has a result kind different "
992 "from its right-hand side input kind: %s vs %s.",
993 op->DebugName(), op->GetId(),
994 Primitive::PrettyDescriptor(result_type),
995 Primitive::PrettyDescriptor(rhs_type)));
996 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000997 }
998}
999
David Brazdilbadd8262016-02-02 16:28:56 +00001000void GraphChecker::VisitConstant(HConstant* instruction) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001001 HBasicBlock* block = instruction->GetBlock();
1002 if (!block->IsEntryBlock()) {
1003 AddError(StringPrintf(
1004 "%s %d should be in the entry block but is in block %d.",
1005 instruction->DebugName(),
1006 instruction->GetId(),
1007 block->GetBlockId()));
1008 }
1009}
1010
David Brazdilbadd8262016-02-02 16:28:56 +00001011void GraphChecker::VisitBoundType(HBoundType* instruction) {
David Brazdilf5552582015-12-27 13:36:12 +00001012 VisitInstruction(instruction);
1013
1014 ScopedObjectAccess soa(Thread::Current());
1015 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