blob: d1769cea0d3b9750a022540182f4f94e3e50f0f3 [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>
Calin Juravlea4f88312015-04-16 12:57:19 +010020#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <string>
Roland Levillainccc07a92014-09-16 14:48:16 +010022
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Roland Levillain7e53b412014-09-23 10:50:22 +010025#include "base/bit_vector-inl.h"
Vladimir Marko009d1662017-10-10 13:21:15 +010026#include "base/scoped_arena_allocator.h"
27#include "base/scoped_arena_containers.h"
Artem Serov07718842020-02-24 18:51:42 +000028#include "code_generator.h"
Vladimir Marko175e7862018-03-27 09:03:13 +000029#include "handle.h"
30#include "mirror/class.h"
31#include "obj_ptr-inl.h"
32#include "scoped_thread_state_change-inl.h"
33#include "subtype_check.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010034
Vladimir Marko0a516052019-10-14 13:00:44 +000035namespace art {
Roland Levillainccc07a92014-09-16 14:48:16 +010036
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037using android::base::StringPrintf;
38
David Brazdil86ea7ee2016-02-16 09:26:07 +000039static bool IsAllowedToJumpToExitBlock(HInstruction* instruction) {
Aart Bika8b8e9b2018-01-09 11:01:02 -080040 // Anything that returns is allowed to jump into the exit block.
41 if (instruction->IsReturn() || instruction->IsReturnVoid()) {
42 return true;
43 }
44 // Anything that always throws is allowed to jump into the exit block.
45 if (instruction->IsGoto() && instruction->GetPrevious() != nullptr) {
46 instruction = instruction->GetPrevious();
47 }
48 return instruction->AlwaysThrows();
David Brazdil86ea7ee2016-02-16 09:26:07 +000049}
50
51static bool IsExitTryBoundaryIntoExitBlock(HBasicBlock* block) {
52 if (!block->IsSingleTryBoundary()) {
53 return false;
54 }
55
56 HTryBoundary* boundary = block->GetLastInstruction()->AsTryBoundary();
57 return block->GetPredecessors().size() == 1u &&
58 boundary->GetNormalFlowSuccessor()->IsExitBlock() &&
59 !boundary->IsEntry();
60}
61
Aart Bika8360cd2018-05-02 16:07:51 -070062
63size_t GraphChecker::Run(bool pass_change, size_t last_size) {
64 size_t current_size = GetGraph()->GetReversePostOrder().size();
65 if (!pass_change) {
Ian Pedowitz2d536432020-07-22 14:33:00 -070066 // Nothing changed for certain. Do a quick check of the validity on that assertion
Aart Bika8360cd2018-05-02 16:07:51 -070067 // for anything other than the first call (when last size was still 0).
68 if (last_size != 0) {
69 if (current_size != last_size) {
70 AddError(StringPrintf("Incorrect no-change assertion, "
71 "last graph size %zu vs current graph size %zu",
72 last_size, current_size));
73 }
74 }
75 // TODO: if we would trust the "false" value of the flag completely, we
76 // could skip checking the graph at this point.
77 }
78
79 // VisitReversePostOrder is used instead of VisitInsertionOrder,
80 // as the latter might visit dead blocks removed by the dominator
81 // computation.
82 VisitReversePostOrder();
83 return current_size;
84}
85
Roland Levillainccc07a92014-09-16 14:48:16 +010086void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
87 current_block_ = block;
88
Vladimir Marko009d1662017-10-10 13:21:15 +010089 // Use local allocator for allocating memory.
90 ScopedArenaAllocator allocator(GetGraph()->GetArenaStack());
91
Roland Levillainccc07a92014-09-16 14:48:16 +010092 // Check consistency with respect to predecessors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +000093 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +000094 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
Vladimir Marko009d1662017-10-10 13:21:15 +010095 ScopedArenaVector<HBasicBlock*> sorted_predecessors(allocator.Adapter(kArenaAllocGraphChecker));
Vladimir Marko947eb702016-03-25 15:31:35 +000096 sorted_predecessors.assign(block->GetPredecessors().begin(), block->GetPredecessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +000097 std::sort(sorted_predecessors.begin(), sorted_predecessors.end());
98 for (auto it = sorted_predecessors.begin(), end = sorted_predecessors.end(); it != end; ) {
99 HBasicBlock* p = *it++;
100 size_t p_count_in_block_predecessors = 1u;
101 for (; it != end && *it == p; ++it) {
102 ++p_count_in_block_predecessors;
Vladimir Marko655e5852015-10-12 10:38:28 +0100103 }
Vladimir Marko655e5852015-10-12 10:38:28 +0100104 size_t block_count_in_p_successors =
105 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +0100106 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000107 AddError(StringPrintf(
108 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
109 "block %d lists %zu occurrences of block %d in its successors.",
110 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
111 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100112 }
113 }
114
115 // Check consistency with respect to successors of `block`.
Vladimir Marko0f49c822016-03-22 17:51:29 +0000116 // Note: Counting duplicates with a sorted vector uses up to 6x less memory
Vladimir Marko947eb702016-03-25 15:31:35 +0000117 // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse.
Vladimir Marko009d1662017-10-10 13:21:15 +0100118 ScopedArenaVector<HBasicBlock*> sorted_successors(allocator.Adapter(kArenaAllocGraphChecker));
Vladimir Marko947eb702016-03-25 15:31:35 +0000119 sorted_successors.assign(block->GetSuccessors().begin(), block->GetSuccessors().end());
Vladimir Marko0f49c822016-03-22 17:51:29 +0000120 std::sort(sorted_successors.begin(), sorted_successors.end());
121 for (auto it = sorted_successors.begin(), end = sorted_successors.end(); it != end; ) {
122 HBasicBlock* s = *it++;
123 size_t s_count_in_block_successors = 1u;
124 for (; it != end && *it == s; ++it) {
125 ++s_count_in_block_successors;
Vladimir Marko655e5852015-10-12 10:38:28 +0100126 }
Vladimir Marko655e5852015-10-12 10:38:28 +0100127 size_t block_count_in_s_predecessors =
128 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +0100129 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000130 AddError(StringPrintf(
131 "Block %d lists %zu occurrences of block %d in its successors, whereas "
132 "block %d lists %zu occurrences of block %d in its predecessors.",
133 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
134 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100135 }
136 }
137
138 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000139 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
140 // dead code that falls out of the method will not end with a control-flow
141 // instruction. Such code is removed during the SSA-building DCE phase.
142 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000143 AddError(StringPrintf("Block %d does not end with a branch instruction.",
144 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100145 }
146
David Brazdil86ea7ee2016-02-16 09:26:07 +0000147 // Ensure that only Return(Void) and Throw jump to Exit. An exiting TryBoundary
148 // may be between the instructions if the Throw/Return(Void) is in a try block.
David Brazdilb618ade2015-07-29 10:31:29 +0100149 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +0000150 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000151 HInstruction* last_instruction = IsExitTryBoundaryIntoExitBlock(predecessor) ?
152 predecessor->GetSinglePredecessor()->GetLastInstruction() :
153 predecessor->GetLastInstruction();
154 if (!IsAllowedToJumpToExitBlock(last_instruction)) {
155 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
156 last_instruction->DebugName(),
157 last_instruction->GetId()));
David Brazdilb618ade2015-07-29 10:31:29 +0100158 }
159 }
160 }
161
Roland Levillainccc07a92014-09-16 14:48:16 +0100162 // Visit this block's list of phis.
163 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100164 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100165 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100166 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000167 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
168 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100169 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100170 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
171 AddError(StringPrintf("The recorded last phi of block %d does not match "
172 "the actual last phi %d.",
173 current_block_->GetBlockId(),
174 current->GetId()));
175 }
176 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100177 }
178
179 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100180 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
181 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100182 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100183 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000184 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
185 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100186 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100187 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
188 AddError(StringPrintf("The recorded last instruction of block %d does not match "
189 "the actual last instruction %d.",
190 current_block_->GetBlockId(),
191 current->GetId()));
192 }
193 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100194 }
David Brazdilbadd8262016-02-02 16:28:56 +0000195
196 // Ensure that catch blocks are not normal successors, and normal blocks are
197 // never exceptional successors.
198 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
199 if (successor->IsCatchBlock()) {
200 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
201 successor->GetBlockId(),
202 block->GetBlockId()));
203 }
204 }
205 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
206 if (!successor->IsCatchBlock()) {
207 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
208 successor->GetBlockId(),
209 block->GetBlockId()));
210 }
211 }
212
213 // Ensure dominated blocks have `block` as the dominator.
214 for (HBasicBlock* dominated : block->GetDominatedBlocks()) {
215 if (dominated->GetDominator() != block) {
216 AddError(StringPrintf("Block %d should be the dominator of %d.",
217 block->GetBlockId(),
218 dominated->GetBlockId()));
219 }
220 }
221
222 // Ensure there is no critical edge (i.e., an edge connecting a
223 // block with multiple successors to a block with multiple
224 // predecessors). Exceptional edges are synthesized and hence
225 // not accounted for.
226 if (block->GetSuccessors().size() > 1) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000227 if (IsExitTryBoundaryIntoExitBlock(block)) {
228 // Allowed critical edge (Throw/Return/ReturnVoid)->TryBoundary->Exit.
229 } else {
230 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
231 if (successor->GetPredecessors().size() > 1) {
232 AddError(StringPrintf("Critical edge between blocks %d and %d.",
233 block->GetBlockId(),
234 successor->GetBlockId()));
235 }
David Brazdilbadd8262016-02-02 16:28:56 +0000236 }
237 }
238 }
239
240 // Ensure try membership information is consistent.
241 if (block->IsCatchBlock()) {
242 if (block->IsTryBlock()) {
243 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
244 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
245 "has try entry %s:%d.",
246 block->GetBlockId(),
247 try_entry.DebugName(),
248 try_entry.GetId()));
249 }
250
251 if (block->IsLoopHeader()) {
252 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
253 block->GetBlockId()));
254 }
255 } else {
256 for (HBasicBlock* predecessor : block->GetPredecessors()) {
257 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
258 if (block->IsTryBlock()) {
259 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
260 if (incoming_try_entry == nullptr) {
261 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
262 "from predecessor %d.",
263 block->GetBlockId(),
264 stored_try_entry.DebugName(),
265 stored_try_entry.GetId(),
266 predecessor->GetBlockId()));
267 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
268 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
269 "with %s:%d that follows from predecessor %d.",
270 block->GetBlockId(),
271 stored_try_entry.DebugName(),
272 stored_try_entry.GetId(),
273 incoming_try_entry->DebugName(),
274 incoming_try_entry->GetId(),
275 predecessor->GetBlockId()));
276 }
277 } else if (incoming_try_entry != nullptr) {
278 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
279 "from predecessor %d.",
280 block->GetBlockId(),
281 incoming_try_entry->DebugName(),
282 incoming_try_entry->GetId(),
283 predecessor->GetBlockId()));
284 }
285 }
286 }
287
288 if (block->IsLoopHeader()) {
289 HandleLoop(block);
290 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100291}
292
Mark Mendell1152c922015-04-24 17:06:35 -0400293void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
294 if (!GetGraph()->HasBoundsChecks()) {
295 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
296 "but HasBoundsChecks() returns false",
297 check->DebugName(),
298 check->GetId()));
299 }
300
301 // Perform the instruction base checks too.
302 VisitInstruction(check);
303}
304
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100305void GraphChecker::VisitDeoptimize(HDeoptimize* deopt) {
306 if (GetGraph()->IsCompilingOsr()) {
307 AddError(StringPrintf("A graph compiled OSR cannot have a HDeoptimize instruction"));
308 }
309
310 // Perform the instruction base checks too.
311 VisitInstruction(deopt);
312}
313
David Brazdilffee3d32015-07-06 11:48:53 +0100314void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000315 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
316
317 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100318 // Note that a normal-flow successor may be a catch block before CFG
David Brazdilbadd8262016-02-02 16:28:56 +0000319 // simplification. We only test normal-flow successors in GraphChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000320 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100321 if (!handler->IsCatchBlock()) {
322 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
323 "is not a catch block.",
324 current_block_->GetBlockId(),
325 try_boundary->DebugName(),
326 try_boundary->GetId(),
327 handler->GetBlockId()));
328 }
David Brazdild26a4112015-11-10 11:07:31 +0000329 }
330
331 // Ensure that handlers are not listed multiple times.
332 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000333 if (ContainsElement(handlers, handlers[i], i + 1)) {
334 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000335 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100336 try_boundary->DebugName(),
337 try_boundary->GetId()));
338 }
339 }
340
341 VisitInstruction(try_boundary);
342}
343
David Brazdil9bc43612015-11-05 21:25:24 +0000344void GraphChecker::VisitLoadException(HLoadException* load) {
345 // Ensure that LoadException is the first instruction in a catch block.
346 if (!load->GetBlock()->IsCatchBlock()) {
347 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
348 load->DebugName(),
349 load->GetId(),
350 load->GetBlock()->GetBlockId()));
351 } else if (load->GetBlock()->GetFirstInstruction() != load) {
352 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
353 load->DebugName(),
354 load->GetId(),
355 load->GetBlock()->GetBlockId()));
356 }
357}
358
Roland Levillainccc07a92014-09-16 14:48:16 +0100359void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000360 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000361 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
362 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000363 } else {
364 seen_ids_.SetBit(instruction->GetId());
365 }
366
Roland Levillainccc07a92014-09-16 14:48:16 +0100367 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000368 if (instruction->GetBlock() == nullptr) {
369 AddError(StringPrintf("%s %d in block %d not associated with any block.",
370 instruction->IsPhi() ? "Phi" : "Instruction",
371 instruction->GetId(),
372 current_block_->GetBlockId()));
373 } else if (instruction->GetBlock() != current_block_) {
374 AddError(StringPrintf("%s %d in block %d associated with block %d.",
375 instruction->IsPhi() ? "Phi" : "Instruction",
376 instruction->GetId(),
377 current_block_->GetBlockId(),
378 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100379 }
Roland Levillain6b469232014-09-25 10:10:38 +0100380
381 // Ensure the inputs of `instruction` are defined in a block of the graph.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100382 for (HInstruction* input : instruction->GetInputs()) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700383 if (input->GetBlock() == nullptr) {
384 AddError(StringPrintf("Input %d of instruction %d is not in any "
385 "basic block of the control-flow graph.",
386 input->GetId(),
387 instruction->GetId()));
Igor Murashkin4ae432d2017-05-04 14:15:08 -0700388 } else {
389 const HInstructionList& list = input->IsPhi()
390 ? input->GetBlock()->GetPhis()
391 : input->GetBlock()->GetInstructions();
392 if (!list.Contains(input)) {
393 AddError(StringPrintf("Input %d of instruction %d is not defined "
394 "in a basic block of the control-flow graph.",
395 input->GetId(),
396 instruction->GetId()));
397 }
Roland Levillain6b469232014-09-25 10:10:38 +0100398 }
399 }
400
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100401 // Ensure the uses of `instruction` are defined in a block of the graph,
402 // and the entry in the use list is consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100403 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
404 HInstruction* user = use.GetUser();
405 const HInstructionList& list = user->IsPhi()
406 ? user->GetBlock()->GetPhis()
407 : user->GetBlock()->GetInstructions();
408 if (!list.Contains(user)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000409 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000410 "in a basic block of the control-flow graph.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100411 user->DebugName(),
412 user->GetId(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000413 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100414 }
Vladimir Marko46817b82016-03-29 12:21:58 +0100415 size_t use_index = use.GetIndex();
Vladimir Markoe9004912016-06-16 16:50:52 +0100416 HConstInputsRef user_inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100417 if ((use_index >= user_inputs.size()) || (user_inputs[use_index] != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000418 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100419 "UseListNode index.",
Vladimir Marko46817b82016-03-29 12:21:58 +0100420 user->DebugName(),
421 user->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000422 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100423 instruction->GetId()));
424 }
425 }
426
427 // Ensure the environment uses entries are consistent.
Vladimir Marko46817b82016-03-29 12:21:58 +0100428 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
429 HEnvironment* user = use.GetUser();
430 size_t use_index = use.GetIndex();
431 if ((use_index >= user->Size()) || (user->GetInstructionAt(use_index) != instruction)) {
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100432 AddError(StringPrintf("Environment user of %s:%d has a wrong "
433 "UseListNode index.",
434 instruction->DebugName(),
435 instruction->GetId()));
436 }
Roland Levillain6b469232014-09-25 10:10:38 +0100437 }
David Brazdil1abb4192015-02-17 18:33:36 +0000438
439 // Ensure 'instruction' has pointers to its inputs' use entries.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100440 auto&& input_records = instruction->GetInputRecords();
441 for (size_t i = 0; i < input_records.size(); ++i) {
442 const HUserRecord<HInstruction*>& input_record = input_records[i];
David Brazdil1abb4192015-02-17 18:33:36 +0000443 HInstruction* input = input_record.GetInstruction();
Vladimir Marko46817b82016-03-29 12:21:58 +0100444 if ((input_record.GetBeforeUseNode() == input->GetUses().end()) ||
445 (input_record.GetUseNode() == input->GetUses().end()) ||
446 !input->GetUses().ContainsNode(*input_record.GetUseNode()) ||
447 (input_record.GetUseNode()->GetIndex() != i)) {
448 AddError(StringPrintf("Instruction %s:%d has an invalid iterator before use entry "
David Brazdil1abb4192015-02-17 18:33:36 +0000449 "at input %u (%s:%d).",
450 instruction->DebugName(),
451 instruction->GetId(),
452 static_cast<unsigned>(i),
453 input->DebugName(),
454 input->GetId()));
455 }
456 }
David Brazdilbadd8262016-02-02 16:28:56 +0000457
458 // Ensure an instruction dominates all its uses.
Vladimir Marko46817b82016-03-29 12:21:58 +0100459 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
460 HInstruction* user = use.GetUser();
461 if (!user->IsPhi() && !instruction->StrictlyDominates(user)) {
David Brazdilbadd8262016-02-02 16:28:56 +0000462 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
463 "use %s:%d in block %d.",
464 instruction->DebugName(),
465 instruction->GetId(),
466 current_block_->GetBlockId(),
Vladimir Marko46817b82016-03-29 12:21:58 +0100467 user->DebugName(),
468 user->GetId(),
469 user->GetBlock()->GetBlockId()));
David Brazdilbadd8262016-02-02 16:28:56 +0000470 }
471 }
472
473 if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) {
474 AddError(StringPrintf("Instruction %s:%d in block %d requires an environment "
475 "but does not have one.",
476 instruction->DebugName(),
477 instruction->GetId(),
478 current_block_->GetBlockId()));
479 }
480
481 // Ensure an instruction having an environment is dominated by the
482 // instructions contained in the environment.
483 for (HEnvironment* environment = instruction->GetEnvironment();
484 environment != nullptr;
485 environment = environment->GetParent()) {
486 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
487 HInstruction* env_instruction = environment->GetInstructionAt(i);
488 if (env_instruction != nullptr
489 && !env_instruction->StrictlyDominates(instruction)) {
490 AddError(StringPrintf("Instruction %d in environment of instruction %d "
491 "from block %d does not dominate instruction %d.",
492 env_instruction->GetId(),
493 instruction->GetId(),
494 current_block_->GetBlockId(),
495 instruction->GetId()));
496 }
497 }
498 }
499
500 // Ensure that reference type instructions have reference type info.
Evgeny Astigeevich7ee34a12019-12-10 11:36:33 +0000501 if (check_reference_type_info_ && instruction->GetType() == DataType::Type::kReference) {
David Brazdilbadd8262016-02-02 16:28:56 +0000502 if (!instruction->GetReferenceTypeInfo().IsValid()) {
503 AddError(StringPrintf("Reference type instruction %s:%d does not have "
504 "valid reference type information.",
505 instruction->DebugName(),
506 instruction->GetId()));
507 }
508 }
509
Vladimir Markod4290262021-05-21 16:36:23 +0100510 if (instruction->CanThrow() && !instruction->HasEnvironment()) {
511 AddError(StringPrintf("Throwing instruction %s:%d in block %d does not have an environment.",
512 instruction->DebugName(),
513 instruction->GetId(),
514 current_block_->GetBlockId()));
515 } else if (instruction->CanThrowIntoCatchBlock()) {
David Brazdilbadd8262016-02-02 16:28:56 +0000516 // Find the top-level environment. This corresponds to the environment of
517 // the catch block since we do not inline methods with try/catch.
518 HEnvironment* environment = instruction->GetEnvironment();
519 while (environment->GetParent() != nullptr) {
520 environment = environment->GetParent();
521 }
522
523 // Find all catch blocks and test that `instruction` has an environment
524 // value for each one.
525 const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
526 for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) {
527 for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
528 HPhi* catch_phi = phi_it.Current()->AsPhi();
529 if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) {
530 AddError(StringPrintf("Instruction %s:%d throws into catch block %d "
531 "with catch phi %d for vreg %d but its "
532 "corresponding environment slot is empty.",
533 instruction->DebugName(),
534 instruction->GetId(),
535 catch_block->GetBlockId(),
536 catch_phi->GetId(),
537 catch_phi->GetRegNumber()));
538 }
539 }
540 }
541 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100542}
543
Roland Levillain4c0eb422015-04-24 16:43:49 +0100544void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
545 VisitInstruction(invoke);
546
547 if (invoke->IsStaticWithExplicitClinitCheck()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100548 const HInstruction* last_input = invoke->GetInputs().back();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100549 if (last_input == nullptr) {
550 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
551 "has a null pointer as last input.",
552 invoke->DebugName(),
553 invoke->GetId()));
George Burgess IV72155d22017-04-25 15:17:16 -0700554 } else if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100555 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
556 "has a last instruction (%s:%d) which is neither a clinit check "
557 "nor a load class instruction.",
558 invoke->DebugName(),
559 invoke->GetId(),
560 last_input->DebugName(),
561 last_input->GetId()));
562 }
563 }
564}
565
David Brazdilfc6a86a2015-06-26 10:33:45 +0000566void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100567 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000568 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
569 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000570 AddError(StringPrintf("%s:%d does not jump to the exit block.",
571 ret->DebugName(),
572 ret->GetId()));
573 }
574}
575
576void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100577 VisitInstruction(ret);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000578 HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor();
579 if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000580 AddError(StringPrintf("%s:%d does not jump to the exit block.",
581 ret->DebugName(),
582 ret->GetId()));
583 }
584}
585
Vladimir Marko175e7862018-03-27 09:03:13 +0000586void GraphChecker::CheckTypeCheckBitstringInput(HTypeCheckInstruction* check,
587 size_t input_pos,
588 bool check_value,
589 uint32_t expected_value,
590 const char* name) {
591 if (!check->InputAt(input_pos)->IsIntConstant()) {
592 AddError(StringPrintf("%s:%d (bitstring) expects a HIntConstant input %zu (%s), not %s:%d.",
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000593 check->DebugName(),
594 check->GetId(),
Vladimir Marko175e7862018-03-27 09:03:13 +0000595 input_pos,
596 name,
597 check->InputAt(2)->DebugName(),
598 check->InputAt(2)->GetId()));
599 } else if (check_value) {
600 uint32_t actual_value =
601 static_cast<uint32_t>(check->InputAt(input_pos)->AsIntConstant()->GetValue());
602 if (actual_value != expected_value) {
603 AddError(StringPrintf("%s:%d (bitstring) has %s 0x%x, not 0x%x as expected.",
604 check->DebugName(),
605 check->GetId(),
606 name,
607 actual_value,
608 expected_value));
609 }
Nicolas Geoffraybff7a522018-01-25 13:33:07 +0000610 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000611}
612
Vladimir Marko175e7862018-03-27 09:03:13 +0000613void GraphChecker::HandleTypeCheckInstruction(HTypeCheckInstruction* check) {
614 VisitInstruction(check);
615 HInstruction* input = check->InputAt(1);
616 if (check->GetTypeCheckKind() == TypeCheckKind::kBitstringCheck) {
617 if (!input->IsNullConstant()) {
618 AddError(StringPrintf("%s:%d (bitstring) expects a HNullConstant as second input, not %s:%d.",
619 check->DebugName(),
620 check->GetId(),
621 input->DebugName(),
622 input->GetId()));
623 }
624 bool check_values = false;
625 BitString::StorageType expected_path_to_root = 0u;
626 BitString::StorageType expected_mask = 0u;
627 {
628 ScopedObjectAccess soa(Thread::Current());
629 ObjPtr<mirror::Class> klass = check->GetClass().Get();
630 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
631 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
632 if (state == SubtypeCheckInfo::kAssigned) {
633 expected_path_to_root =
634 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass);
635 expected_mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass);
636 check_values = true;
637 } else {
638 AddError(StringPrintf("%s:%d (bitstring) references a class with unassigned bitstring.",
639 check->DebugName(),
640 check->GetId()));
641 }
642 }
643 CheckTypeCheckBitstringInput(
Andreas Gampe3db70682018-12-26 15:12:03 -0800644 check, /* input_pos= */ 2, check_values, expected_path_to_root, "path_to_root");
645 CheckTypeCheckBitstringInput(check, /* input_pos= */ 3, check_values, expected_mask, "mask");
Vladimir Marko175e7862018-03-27 09:03:13 +0000646 } else {
647 if (!input->IsLoadClass()) {
648 AddError(StringPrintf("%s:%d (classic) expects a HLoadClass as second input, not %s:%d.",
649 check->DebugName(),
650 check->GetId(),
651 input->DebugName(),
652 input->GetId()));
653 }
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000654 }
Vladimir Marko3f413232018-02-12 18:39:15 +0000655}
656
Vladimir Marko175e7862018-03-27 09:03:13 +0000657void GraphChecker::VisitCheckCast(HCheckCast* check) {
658 HandleTypeCheckInstruction(check);
659}
660
661void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
662 HandleTypeCheckInstruction(instruction);
663}
664
David Brazdilbadd8262016-02-02 16:28:56 +0000665void GraphChecker::HandleLoop(HBasicBlock* loop_header) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100666 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100667 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100668
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000669 if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
David Brazdildb51efb2015-11-06 01:36:20 +0000670 AddError(StringPrintf(
671 "Loop pre-header %d of loop defined by header %d has %zu successors.",
672 loop_information->GetPreHeader()->GetBlockId(),
673 id,
674 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100675 }
676
Nicolas Geoffray8f6b99f2021-09-28 17:51:17 +0000677 if (loop_information->GetSuspendCheck() == nullptr) {
678 AddError(StringPrintf(
679 "Loop with header %d does not have a suspend check.",
680 loop_header->GetBlockId()));
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000681 }
682
Nicolas Geoffray8f6b99f2021-09-28 17:51:17 +0000683 if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) {
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000684 AddError(StringPrintf(
685 "Loop header %d does not have the loop suspend check as the first instruction.",
686 loop_header->GetBlockId()));
687 }
688
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100689 // Ensure the loop header has only one incoming branch and the remaining
690 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000691 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000692 if (num_preds < 2) {
693 AddError(StringPrintf(
694 "Loop header %d has less than two predecessors: %zu.",
695 id,
696 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100697 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100698 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000699 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000700 AddError(StringPrintf(
701 "First predecessor of loop header %d is a back edge.",
702 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100703 }
Vladimir Marko60584552015-09-03 13:35:12 +0000704 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100705 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100706 if (!loop_information->IsBackEdge(*predecessor)) {
707 AddError(StringPrintf(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 "Loop header %d has multiple incoming (non back edge) blocks: %d.",
709 id,
710 predecessor->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100711 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100712 }
713 }
714
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100715 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100716
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100717 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100718 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000719 AddError(StringPrintf(
720 "Loop defined by header %d has no back edge.",
721 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100722 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
724 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100725 if (!loop_blocks.IsBitSet(back_edge_id)) {
726 AddError(StringPrintf(
727 "Loop defined by header %d has an invalid back edge %d.",
728 id,
729 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000730 } else if (back_edge->GetLoopInformation() != loop_information) {
731 AddError(StringPrintf(
732 "Back edge %d of loop defined by header %d belongs to nested loop "
733 "with header %d.",
734 back_edge_id,
735 id,
736 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100737 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100738 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100739 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100740
David Brazdil7d275372015-04-21 16:36:35 +0100741 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
742 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
743 HLoopInformation* outer_info = it.Current();
744 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
745 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
746 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100747 id,
David Brazdil7d275372015-04-21 16:36:35 +0100748 outer_info->GetHeader()->GetBlockId()));
749 }
750 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000751
752 // Ensure the pre-header block is first in the list of predecessors of a loop
753 // header and that the header block is its only successor.
754 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
755 AddError(StringPrintf(
756 "Loop pre-header is not the first predecessor of the loop header %d.",
757 id));
758 }
759
760 // Ensure all blocks in the loop are live and dominated by the loop header in
761 // the case of natural loops.
762 for (uint32_t i : loop_blocks.Indexes()) {
763 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
764 if (loop_block == nullptr) {
765 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
766 id,
767 i));
768 } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) {
769 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
770 i,
771 id));
772 }
773 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100774}
775
Vladimir Markoe9004912016-06-16 16:50:52 +0100776static bool IsSameSizeConstant(const HInstruction* insn1, const HInstruction* insn2) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000777 return insn1->IsConstant()
778 && insn2->IsConstant()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100779 && DataType::Is64BitType(insn1->GetType()) == DataType::Is64BitType(insn2->GetType());
David Brazdil77a48ae2015-09-15 12:34:04 +0000780}
781
Vladimir Markoe9004912016-06-16 16:50:52 +0100782static bool IsConstantEquivalent(const HInstruction* insn1,
783 const HInstruction* insn2,
784 BitVector* visited) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000785 if (insn1->IsPhi() &&
Vladimir Marko372f10e2016-05-17 16:30:10 +0100786 insn1->AsPhi()->IsVRegEquivalentOf(insn2)) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100787 HConstInputsRef insn1_inputs = insn1->GetInputs();
788 HConstInputsRef insn2_inputs = insn2->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100789 if (insn1_inputs.size() != insn2_inputs.size()) {
790 return false;
791 }
792
David Brazdil77a48ae2015-09-15 12:34:04 +0000793 // Testing only one of the two inputs for recursion is sufficient.
794 if (visited->IsBitSet(insn1->GetId())) {
795 return true;
796 }
797 visited->SetBit(insn1->GetId());
798
Vladimir Marko372f10e2016-05-17 16:30:10 +0100799 for (size_t i = 0; i < insn1_inputs.size(); ++i) {
800 if (!IsConstantEquivalent(insn1_inputs[i], insn2_inputs[i], visited)) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000801 return false;
802 }
803 }
804 return true;
805 } else if (IsSameSizeConstant(insn1, insn2)) {
806 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
807 } else {
808 return false;
809 }
810}
811
David Brazdilbadd8262016-02-02 16:28:56 +0000812void GraphChecker::VisitPhi(HPhi* phi) {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100813 VisitInstruction(phi);
814
815 // Ensure the first input of a phi is not itself.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100816 ArrayRef<HUserRecord<HInstruction*>> input_records = phi->GetInputRecords();
817 if (input_records[0].GetInstruction() == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000818 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
819 phi->GetId(),
820 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100821 }
822
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000823 // Ensure that the inputs have the same primitive kind as the phi.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100824 for (size_t i = 0; i < input_records.size(); ++i) {
825 HInstruction* input = input_records[i].GetInstruction();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100826 if (DataType::Kind(input->GetType()) != DataType::Kind(phi->GetType())) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000827 AddError(StringPrintf(
828 "Input %d at index %zu of phi %d from block %d does not have the "
Roland Levillaina5c4a402016-03-15 15:02:50 +0000829 "same kind as the phi: %s versus %s",
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000830 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 DataType::PrettyDescriptor(input->GetType()),
832 DataType::PrettyDescriptor(phi->GetType())));
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000833 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000834 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000835 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
836 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
837 phi->GetId(),
838 phi->GetBlock()->GetBlockId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::PrettyDescriptor(phi->GetType())));
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000840 }
David Brazdilffee3d32015-07-06 11:48:53 +0100841
842 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100843 // The number of inputs of a catch phi should be the total number of throwing
844 // instructions caught by this catch block. We do not enforce this, however,
845 // because we do not remove the corresponding inputs when we prove that an
846 // instruction cannot throw. Instead, we at least test that all phis have the
847 // same, non-zero number of inputs (b/24054676).
Vladimir Marko372f10e2016-05-17 16:30:10 +0100848 if (input_records.empty()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100849 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
850 phi->GetId(),
851 phi->GetBlock()->GetBlockId()));
852 } else {
853 HInstruction* next_phi = phi->GetNext();
854 if (next_phi != nullptr) {
855 size_t input_count_next = next_phi->InputCount();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100856 if (input_records.size() != input_count_next) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100857 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
858 "but phi %d has %zu inputs.",
859 phi->GetId(),
860 phi->GetBlock()->GetBlockId(),
Vladimir Marko372f10e2016-05-17 16:30:10 +0100861 input_records.size(),
David Brazdil3eaa32f2015-09-18 10:58:32 +0100862 next_phi->GetId(),
863 input_count_next));
864 }
865 }
866 }
David Brazdilffee3d32015-07-06 11:48:53 +0100867 } else {
868 // Ensure the number of inputs of a non-catch phi is the same as the number
869 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000870 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100871 if (input_records.size() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100872 AddError(StringPrintf(
873 "Phi %d in block %d has %zu inputs, "
874 "but block %d has %zu predecessors.",
Vladimir Marko372f10e2016-05-17 16:30:10 +0100875 phi->GetId(), phi->GetBlock()->GetBlockId(), input_records.size(),
Vladimir Marko60584552015-09-03 13:35:12 +0000876 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100877 } else {
878 // Ensure phi input at index I either comes from the Ith
879 // predecessor or from a block that dominates this predecessor.
Vladimir Marko372f10e2016-05-17 16:30:10 +0100880 for (size_t i = 0; i < input_records.size(); ++i) {
881 HInstruction* input = input_records[i].GetInstruction();
Vladimir Marko60584552015-09-03 13:35:12 +0000882 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100883 if (!(input->GetBlock() == predecessor
884 || input->GetBlock()->Dominates(predecessor))) {
885 AddError(StringPrintf(
886 "Input %d at index %zu of phi %d from block %d is not defined in "
887 "predecessor number %zu nor in a block dominating it.",
888 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
889 i));
890 }
891 }
892 }
893 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000894
895 // Ensure that catch phis are sorted by their vreg number, as required by
896 // the register allocator and code generator. This does not apply to normal
897 // phis which can be constructed artifically.
898 if (phi->IsCatchPhi()) {
899 HInstruction* next_phi = phi->GetNext();
900 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
901 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
902 "vreg numbers.",
903 phi->GetId(),
904 next_phi->GetId(),
905 phi->GetBlock()->GetBlockId()));
906 }
907 }
908
Aart Bik3fc7f352015-11-20 22:03:03 -0800909 // Test phi equivalents. There should not be two of the same type and they should only be
910 // created for constants which were untyped in DEX. Note that this test can be skipped for
911 // a synthetic phi (indicated by lack of a virtual register).
912 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800913 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
914 !phi_it.Done();
915 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800916 HPhi* other_phi = phi_it.Current()->AsPhi();
917 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
918 if (phi->GetType() == other_phi->GetType()) {
919 std::stringstream type_str;
920 type_str << phi->GetType();
921 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000922 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800923 phi->GetRegNumber(),
924 type_str.str().c_str()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100925 } else if (phi->GetType() == DataType::Type::kReference) {
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000926 std::stringstream type_str;
927 type_str << other_phi->GetType();
928 AddError(StringPrintf(
929 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
930 phi->GetId(),
931 phi->GetRegNumber(),
932 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800933 } else {
Vladimir Marko009d1662017-10-10 13:21:15 +0100934 // Use local allocator for allocating memory.
935 ScopedArenaAllocator allocator(GetGraph()->GetArenaStack());
Vladimir Marko947eb702016-03-25 15:31:35 +0000936 // If we get here, make sure we allocate all the necessary storage at once
937 // because the BitVector reallocation strategy has very bad worst-case behavior.
Vladimir Marko009d1662017-10-10 13:21:15 +0100938 ArenaBitVector visited(&allocator,
939 GetGraph()->GetCurrentInstructionId(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800940 /* expandable= */ false,
Vladimir Marko009d1662017-10-10 13:21:15 +0100941 kArenaAllocGraphChecker);
Vladimir Marko947eb702016-03-25 15:31:35 +0000942 visited.ClearAllBits();
Aart Bik3fc7f352015-11-20 22:03:03 -0800943 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
944 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
945 "are not equivalents of constants.",
946 phi->GetId(),
947 other_phi->GetId(),
948 phi->GetRegNumber()));
949 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000950 }
951 }
952 }
953 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000954}
955
David Brazdilbadd8262016-02-02 16:28:56 +0000956void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
David Brazdil13b47182015-04-15 16:29:32 +0100957 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000958 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100959 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000960 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000961 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100962 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
963 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000964 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100965 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000966 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000967 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100968 } else if (DataType::Kind(input->GetType()) != DataType::Type::kInt32) {
David Brazdil11edec72016-03-24 12:40:52 +0000969 // TODO: We need a data-flow analysis to determine if an input like Phi,
970 // Select or a binary operation is actually Boolean. Allow for now.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000971 AddError(StringPrintf(
David Brazdil11edec72016-03-24 12:40:52 +0000972 "%s instruction %d has a non-integer input %d whose type is: %s.",
David Brazdil13b47182015-04-15 16:29:32 +0100973 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000974 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100975 static_cast<int>(input_index),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100976 DataType::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000977 }
978}
979
David Brazdilbadd8262016-02-02 16:28:56 +0000980void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
Mark Mendellfe57faa2015-09-18 09:26:15 -0400981 VisitInstruction(instruction);
982 // Check that the number of block successors matches the switch count plus
983 // one for the default block.
984 HBasicBlock* block = instruction->GetBlock();
985 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
986 AddError(StringPrintf(
987 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
988 instruction->DebugName(),
989 instruction->GetId(),
990 block->GetBlockId(),
991 instruction->GetNumEntries() + 1u,
992 block->GetSuccessors().size()));
993 }
994}
995
David Brazdilbadd8262016-02-02 16:28:56 +0000996void GraphChecker::VisitIf(HIf* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +0100997 VisitInstruction(instruction);
998 HandleBooleanInput(instruction, 0);
999}
1000
David Brazdilbadd8262016-02-02 16:28:56 +00001001void GraphChecker::VisitSelect(HSelect* instruction) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001002 VisitInstruction(instruction);
1003 HandleBooleanInput(instruction, 2);
1004}
1005
David Brazdilbadd8262016-02-02 16:28:56 +00001006void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil13b47182015-04-15 16:29:32 +01001007 VisitInstruction(instruction);
1008 HandleBooleanInput(instruction, 0);
1009}
1010
David Brazdilbadd8262016-02-02 16:28:56 +00001011void GraphChecker::VisitCondition(HCondition* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +00001012 VisitInstruction(op);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001013 if (op->GetType() != DataType::Type::kBool) {
Roland Levillain5c4405e2015-01-21 11:39:58 +00001014 AddError(StringPrintf(
1015 "Condition %s %d has a non-Boolean result type: %s.",
1016 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001017 DataType::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +00001018 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001019 HInstruction* lhs = op->InputAt(0);
1020 HInstruction* rhs = op->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001021 if (DataType::Kind(lhs->GetType()) != DataType::Kind(rhs->GetType())) {
Calin Juravlea4f88312015-04-16 12:57:19 +01001022 AddError(StringPrintf(
Roland Levillaina5c4a402016-03-15 15:02:50 +00001023 "Condition %s %d has inputs of different kinds: %s, and %s.",
Calin Juravlea4f88312015-04-16 12:57:19 +01001024 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001025 DataType::PrettyDescriptor(lhs->GetType()),
1026 DataType::PrettyDescriptor(rhs->GetType())));
Calin Juravlea4f88312015-04-16 12:57:19 +01001027 }
1028 if (!op->IsEqual() && !op->IsNotEqual()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001029 if ((lhs->GetType() == DataType::Type::kReference)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +00001030 AddError(StringPrintf(
1031 "Condition %s %d uses an object as left-hand side input.",
1032 op->DebugName(), op->GetId()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001033 } else if (rhs->GetType() == DataType::Type::kReference) {
Roland Levillain5c4405e2015-01-21 11:39:58 +00001034 AddError(StringPrintf(
1035 "Condition %s %d uses an object as right-hand side input.",
1036 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +00001037 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +00001038 }
Nicolas Geoffray31596742014-11-24 15:28:45 +00001039}
1040
Roland Levillain937e6cd2016-03-22 11:54:37 +00001041void GraphChecker::VisitNeg(HNeg* instruction) {
1042 VisitInstruction(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001043 DataType::Type input_type = instruction->InputAt(0)->GetType();
1044 DataType::Type result_type = instruction->GetType();
1045 if (result_type != DataType::Kind(input_type)) {
Roland Levillain937e6cd2016-03-22 11:54:37 +00001046 AddError(StringPrintf("Binary operation %s %d has a result type different "
1047 "from its input kind: %s vs %s.",
1048 instruction->DebugName(), instruction->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001049 DataType::PrettyDescriptor(result_type),
1050 DataType::PrettyDescriptor(input_type)));
Roland Levillain937e6cd2016-03-22 11:54:37 +00001051 }
1052}
1053
David Brazdilbadd8262016-02-02 16:28:56 +00001054void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) {
Nicolas Geoffray31596742014-11-24 15:28:45 +00001055 VisitInstruction(op);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001056 DataType::Type lhs_type = op->InputAt(0)->GetType();
1057 DataType::Type rhs_type = op->InputAt(1)->GetType();
1058 DataType::Type result_type = op->GetType();
Roland Levillain5b5b9312016-03-22 14:57:31 +00001059
1060 // Type consistency between inputs.
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001061 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001062 if (DataType::Kind(rhs_type) != DataType::Type::kInt32) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001063 AddError(StringPrintf("Shift/rotate operation %s %d has a non-int kind second input: "
1064 "%s of type %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +00001065 op->DebugName(), op->GetId(),
1066 op->InputAt(1)->DebugName(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001067 DataType::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +00001068 }
1069 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001070 if (DataType::Kind(lhs_type) != DataType::Kind(rhs_type)) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001071 AddError(StringPrintf("Binary operation %s %d has inputs of different kinds: %s, and %s.",
1072 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001073 DataType::PrettyDescriptor(lhs_type),
1074 DataType::PrettyDescriptor(rhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +00001075 }
1076 }
1077
Roland Levillain5b5b9312016-03-22 14:57:31 +00001078 // Type consistency between result and input(s).
Nicolas Geoffray31596742014-11-24 15:28:45 +00001079 if (op->IsCompare()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001080 if (result_type != DataType::Type::kInt32) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001081 AddError(StringPrintf("Compare operation %d has a non-int result type: %s.",
1082 op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::PrettyDescriptor(result_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +00001084 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00001085 } else if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
1086 // Only check the first input (value), as the second one (distance)
1087 // must invariably be of kind `int`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001088 if (result_type != DataType::Kind(lhs_type)) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001089 AddError(StringPrintf("Shift/rotate operation %s %d has a result type different "
1090 "from its left-hand side (value) input kind: %s vs %s.",
Roland Levillaina5c4a402016-03-15 15:02:50 +00001091 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001092 DataType::PrettyDescriptor(result_type),
1093 DataType::PrettyDescriptor(lhs_type)));
Nicolas Geoffray31596742014-11-24 15:28:45 +00001094 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00001095 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001096 if (DataType::Kind(result_type) != DataType::Kind(lhs_type)) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001097 AddError(StringPrintf("Binary operation %s %d has a result kind different "
1098 "from its left-hand side input kind: %s vs %s.",
1099 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001100 DataType::PrettyDescriptor(result_type),
1101 DataType::PrettyDescriptor(lhs_type)));
Roland Levillain5b5b9312016-03-22 14:57:31 +00001102 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001103 if (DataType::Kind(result_type) != DataType::Kind(rhs_type)) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001104 AddError(StringPrintf("Binary operation %s %d has a result kind different "
1105 "from its right-hand side input kind: %s vs %s.",
1106 op->DebugName(), op->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001107 DataType::PrettyDescriptor(result_type),
1108 DataType::PrettyDescriptor(rhs_type)));
Roland Levillain5b5b9312016-03-22 14:57:31 +00001109 }
Nicolas Geoffray31596742014-11-24 15:28:45 +00001110 }
1111}
1112
David Brazdilbadd8262016-02-02 16:28:56 +00001113void GraphChecker::VisitConstant(HConstant* instruction) {
David Brazdil8d5b8b22015-03-24 10:51:52 +00001114 HBasicBlock* block = instruction->GetBlock();
1115 if (!block->IsEntryBlock()) {
1116 AddError(StringPrintf(
1117 "%s %d should be in the entry block but is in block %d.",
1118 instruction->DebugName(),
1119 instruction->GetId(),
1120 block->GetBlockId()));
1121 }
1122}
1123
David Brazdilbadd8262016-02-02 16:28:56 +00001124void GraphChecker::VisitBoundType(HBoundType* instruction) {
David Brazdilf5552582015-12-27 13:36:12 +00001125 VisitInstruction(instruction);
1126
David Brazdilf5552582015-12-27 13:36:12 +00001127 if (!instruction->GetUpperBound().IsValid()) {
1128 AddError(StringPrintf(
1129 "%s %d does not have a valid upper bound RTI.",
1130 instruction->DebugName(),
1131 instruction->GetId()));
1132 }
1133}
1134
Roland Levillainf355c3f2016-03-30 19:09:03 +01001135void GraphChecker::VisitTypeConversion(HTypeConversion* instruction) {
1136 VisitInstruction(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 DataType::Type result_type = instruction->GetResultType();
1138 DataType::Type input_type = instruction->GetInputType();
Roland Levillainf355c3f2016-03-30 19:09:03 +01001139 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001140 if (result_type == DataType::Type::kBool) {
Roland Levillainf355c3f2016-03-30 19:09:03 +01001141 AddError(StringPrintf(
1142 "%s %d converts to a %s (from a %s).",
1143 instruction->DebugName(),
1144 instruction->GetId(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DataType::PrettyDescriptor(result_type),
1146 DataType::PrettyDescriptor(input_type)));
Roland Levillainf355c3f2016-03-30 19:09:03 +01001147 }
1148}
1149
Artem Serov07718842020-02-24 18:51:42 +00001150void GraphChecker::VisitVecOperation(HVecOperation* instruction) {
1151 VisitInstruction(instruction);
1152 if (codegen_ == nullptr) {
1153 return;
1154 }
1155
1156 if (!codegen_->SupportsPredicatedSIMD() && instruction->IsPredicated()) {
1157 AddError(StringPrintf(
1158 "%s %d must not be predicated.",
1159 instruction->DebugName(),
1160 instruction->GetId()));
1161 }
1162
1163 if (codegen_->SupportsPredicatedSIMD() &&
1164 (instruction->MustBePredicatedInPredicatedSIMDMode() != instruction->IsPredicated())) {
1165 AddError(StringPrintf(
1166 "%s %d predication mode is incorrect; see HVecOperation::MustBePredicated.",
1167 instruction->DebugName(),
1168 instruction->GetId()));
1169 }
1170}
1171
Roland Levillainccc07a92014-09-16 14:48:16 +01001172} // namespace art