blob: 3e358358ae5cf44e9b8961faab51ff15b3a2ad2c [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
Roland Levillainccc07a92014-09-16 14:48:16 +010019#include <map>
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
Roland Levillain7e53b412014-09-23 10:50:22 +010023#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000024#include "base/stringprintf.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010025
Roland Levillainccc07a92014-09-16 14:48:16 +010026namespace art {
27
28void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
29 current_block_ = block;
30
31 // Check consistency with respect to predecessors of `block`.
Roland Levillainccc07a92014-09-16 14:48:16 +010032 std::map<HBasicBlock*, size_t> predecessors_count;
Vladimir Marko60584552015-09-03 13:35:12 +000033 for (HBasicBlock* p : block->GetPredecessors()) {
Roland Levillainccc07a92014-09-16 14:48:16 +010034 ++predecessors_count[p];
35 }
36 for (auto& pc : predecessors_count) {
37 HBasicBlock* p = pc.first;
38 size_t p_count_in_block_predecessors = pc.second;
Roland Levillainccc07a92014-09-16 14:48:16 +010039 size_t block_count_in_p_successors = 0;
Vladimir Marko60584552015-09-03 13:35:12 +000040 for (HBasicBlock* p_successor : p->GetSuccessors()) {
41 if (p_successor == block) {
Roland Levillainccc07a92014-09-16 14:48:16 +010042 ++block_count_in_p_successors;
43 }
44 }
45 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000046 AddError(StringPrintf(
47 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
48 "block %d lists %zu occurrences of block %d in its successors.",
49 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
50 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010051 }
52 }
53
54 // Check consistency with respect to successors of `block`.
Roland Levillainccc07a92014-09-16 14:48:16 +010055 std::map<HBasicBlock*, size_t> successors_count;
Vladimir Marko60584552015-09-03 13:35:12 +000056 for (HBasicBlock* s : block->GetSuccessors()) {
Roland Levillainccc07a92014-09-16 14:48:16 +010057 ++successors_count[s];
58 }
59 for (auto& sc : successors_count) {
60 HBasicBlock* s = sc.first;
61 size_t s_count_in_block_successors = sc.second;
Roland Levillainccc07a92014-09-16 14:48:16 +010062 size_t block_count_in_s_predecessors = 0;
Vladimir Marko60584552015-09-03 13:35:12 +000063 for (HBasicBlock* s_predecessor : s->GetPredecessors()) {
64 if (s_predecessor == block) {
Roland Levillainccc07a92014-09-16 14:48:16 +010065 ++block_count_in_s_predecessors;
66 }
67 }
68 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000069 AddError(StringPrintf(
70 "Block %d lists %zu occurrences of block %d in its successors, whereas "
71 "block %d lists %zu occurrences of block %d in its predecessors.",
72 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
73 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010074 }
75 }
76
77 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000078 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
79 // dead code that falls out of the method will not end with a control-flow
80 // instruction. Such code is removed during the SSA-building DCE phase.
81 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000082 AddError(StringPrintf("Block %d does not end with a branch instruction.",
83 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010084 }
85
David Brazdil29fc0082015-08-18 17:17:38 +010086 // Ensure that only Return(Void) and Throw jump to Exit. An exiting
David Brazdilb618ade2015-07-29 10:31:29 +010087 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
88 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +000089 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilb618ade2015-07-29 10:31:29 +010090 if (predecessor->IsSingleTryBoundary()
91 && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) {
92 HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor();
93 HInstruction* last_instruction = real_predecessor->GetLastInstruction();
94 if (!last_instruction->IsThrow()) {
95 AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.",
96 last_instruction->DebugName(),
97 last_instruction->GetId()));
98 }
99 } else {
100 HInstruction* last_instruction = predecessor->GetLastInstruction();
101 if (!last_instruction->IsReturn()
102 && !last_instruction->IsReturnVoid()
103 && !last_instruction->IsThrow()) {
104 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
105 last_instruction->DebugName(),
106 last_instruction->GetId()));
107 }
108 }
109 }
110 }
111
Roland Levillainccc07a92014-09-16 14:48:16 +0100112 // Visit this block's list of phis.
113 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100115 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100116 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000117 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
118 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100119 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100120 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
121 AddError(StringPrintf("The recorded last phi of block %d does not match "
122 "the actual last phi %d.",
123 current_block_->GetBlockId(),
124 current->GetId()));
125 }
126 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100127 }
128
129 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100130 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
131 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100132 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100133 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000134 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
135 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100136 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100137 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
138 AddError(StringPrintf("The recorded last instruction of block %d does not match "
139 "the actual last instruction %d.",
140 current_block_->GetBlockId(),
141 current->GetId()));
142 }
143 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100144 }
145}
146
Mark Mendell1152c922015-04-24 17:06:35 -0400147void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
148 if (!GetGraph()->HasBoundsChecks()) {
149 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
150 "but HasBoundsChecks() returns false",
151 check->DebugName(),
152 check->GetId()));
153 }
154
155 // Perform the instruction base checks too.
156 VisitInstruction(check);
157}
158
David Brazdilffee3d32015-07-06 11:48:53 +0100159void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
160 // Ensure that all exception handlers are catch blocks and that handlers
161 // are not listed multiple times.
162 // Note that a normal-flow successor may be a catch block before CFG
163 // simplification. We only test normal-flow successors in SsaChecker.
164 for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) {
165 HBasicBlock* handler = it.Current();
166 if (!handler->IsCatchBlock()) {
167 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
168 "is not a catch block.",
169 current_block_->GetBlockId(),
170 try_boundary->DebugName(),
171 try_boundary->GetId(),
172 handler->GetBlockId()));
173 }
Vladimir Marko60584552015-09-03 13:35:12 +0000174 if (current_block_->HasSuccessor(handler, it.CurrentSuccessorIndex() + 1)) {
David Brazdilffee3d32015-07-06 11:48:53 +0100175 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
176 handler->GetBlockId(),
177 try_boundary->DebugName(),
178 try_boundary->GetId()));
179 }
180 }
181
182 VisitInstruction(try_boundary);
183}
184
Roland Levillainccc07a92014-09-16 14:48:16 +0100185void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000186 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000187 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
188 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000189 } else {
190 seen_ids_.SetBit(instruction->GetId());
191 }
192
Roland Levillainccc07a92014-09-16 14:48:16 +0100193 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000194 if (instruction->GetBlock() == nullptr) {
195 AddError(StringPrintf("%s %d in block %d not associated with any block.",
196 instruction->IsPhi() ? "Phi" : "Instruction",
197 instruction->GetId(),
198 current_block_->GetBlockId()));
199 } else if (instruction->GetBlock() != current_block_) {
200 AddError(StringPrintf("%s %d in block %d associated with block %d.",
201 instruction->IsPhi() ? "Phi" : "Instruction",
202 instruction->GetId(),
203 current_block_->GetBlockId(),
204 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100205 }
Roland Levillain6b469232014-09-25 10:10:38 +0100206
207 // Ensure the inputs of `instruction` are defined in a block of the graph.
208 for (HInputIterator input_it(instruction); !input_it.Done();
209 input_it.Advance()) {
210 HInstruction* input = input_it.Current();
211 const HInstructionList& list = input->IsPhi()
212 ? input->GetBlock()->GetPhis()
213 : input->GetBlock()->GetInstructions();
214 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000215 AddError(StringPrintf("Input %d of instruction %d is not defined "
216 "in a basic block of the control-flow graph.",
217 input->GetId(),
218 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100219 }
220 }
221
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100222 // Ensure the uses of `instruction` are defined in a block of the graph,
223 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000224 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100225 !use_it.Done(); use_it.Advance()) {
226 HInstruction* use = use_it.Current()->GetUser();
227 const HInstructionList& list = use->IsPhi()
228 ? use->GetBlock()->GetPhis()
229 : use->GetBlock()->GetInstructions();
230 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000231 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000232 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000233 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000234 use->GetId(),
235 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100236 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100237 size_t use_index = use_it.Current()->GetIndex();
238 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
239 AddError(StringPrintf("User %s:%d of instruction %d has a wrong "
240 "UseListNode index.",
241 use->DebugName(),
242 use->GetId(),
243 instruction->GetId()));
244 }
245 }
246
247 // Ensure the environment uses entries are consistent.
248 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
249 !use_it.Done(); use_it.Advance()) {
250 HEnvironment* use = use_it.Current()->GetUser();
251 size_t use_index = use_it.Current()->GetIndex();
252 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
253 AddError(StringPrintf("Environment user of %s:%d has a wrong "
254 "UseListNode index.",
255 instruction->DebugName(),
256 instruction->GetId()));
257 }
Roland Levillain6b469232014-09-25 10:10:38 +0100258 }
David Brazdil1abb4192015-02-17 18:33:36 +0000259
260 // Ensure 'instruction' has pointers to its inputs' use entries.
261 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
262 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
263 HInstruction* input = input_record.GetInstruction();
264 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100265 size_t use_index = use_node->GetIndex();
266 if ((use_node == nullptr)
267 || !input->GetUses().Contains(use_node)
268 || (use_index >= e)
269 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000270 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
271 "at input %u (%s:%d).",
272 instruction->DebugName(),
273 instruction->GetId(),
274 static_cast<unsigned>(i),
275 input->DebugName(),
276 input->GetId()));
277 }
278 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100279}
280
Roland Levillain4c0eb422015-04-24 16:43:49 +0100281void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
282 VisitInstruction(invoke);
283
284 if (invoke->IsStaticWithExplicitClinitCheck()) {
285 size_t last_input_index = invoke->InputCount() - 1;
286 HInstruction* last_input = invoke->InputAt(last_input_index);
287 if (last_input == nullptr) {
288 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
289 "has a null pointer as last input.",
290 invoke->DebugName(),
291 invoke->GetId()));
292 }
293 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
294 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
295 "has a last instruction (%s:%d) which is neither a clinit check "
296 "nor a load class instruction.",
297 invoke->DebugName(),
298 invoke->GetId(),
299 last_input->DebugName(),
300 last_input->GetId()));
301 }
302 }
303}
304
David Brazdilfc6a86a2015-06-26 10:33:45 +0000305void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100306 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000307 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
308 AddError(StringPrintf("%s:%d does not jump to the exit block.",
309 ret->DebugName(),
310 ret->GetId()));
311 }
312}
313
314void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100315 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000316 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
317 AddError(StringPrintf("%s:%d does not jump to the exit block.",
318 ret->DebugName(),
319 ret->GetId()));
320 }
321}
322
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100323void GraphChecker::VisitCheckCast(HCheckCast* check) {
324 VisitInstruction(check);
325 HInstruction* input = check->InputAt(1);
326 if (!input->IsLoadClass()) {
327 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
328 check->DebugName(),
329 check->GetId(),
330 input->DebugName(),
331 input->GetId()));
332 }
333}
334
335void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
336 VisitInstruction(instruction);
337 HInstruction* input = instruction->InputAt(1);
338 if (!input->IsLoadClass()) {
339 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
340 instruction->DebugName(),
341 instruction->GetId(),
342 input->DebugName(),
343 input->GetId()));
344 }
345}
346
Roland Levillainccc07a92014-09-16 14:48:16 +0100347void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
348 super_type::VisitBasicBlock(block);
349
David Brazdilffee3d32015-07-06 11:48:53 +0100350 // Ensure that catch blocks are not normal successors, and normal blocks are
351 // never exceptional successors.
352 const size_t num_normal_successors = block->NumberOfNormalSuccessors();
353 for (size_t j = 0; j < num_normal_successors; ++j) {
Vladimir Marko60584552015-09-03 13:35:12 +0000354 HBasicBlock* successor = block->GetSuccessor(j);
David Brazdilffee3d32015-07-06 11:48:53 +0100355 if (successor->IsCatchBlock()) {
356 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
357 successor->GetBlockId(),
358 block->GetBlockId()));
359 }
360 }
Vladimir Marko60584552015-09-03 13:35:12 +0000361 for (size_t j = num_normal_successors, e = block->GetSuccessors().size(); j < e; ++j) {
362 HBasicBlock* successor = block->GetSuccessor(j);
David Brazdilffee3d32015-07-06 11:48:53 +0100363 if (!successor->IsCatchBlock()) {
364 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
365 successor->GetBlockId(),
366 block->GetBlockId()));
367 }
368 }
369
Roland Levillainccc07a92014-09-16 14:48:16 +0100370 // Ensure there is no critical edge (i.e., an edge connecting a
371 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100372 // predecessors). Exceptional edges are synthesized and hence
373 // not accounted for.
374 if (block->NumberOfNormalSuccessors() > 1) {
375 for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) {
Vladimir Marko60584552015-09-03 13:35:12 +0000376 HBasicBlock* successor = block->GetSuccessor(j);
377 if (successor->GetPredecessors().size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000378 AddError(StringPrintf("Critical edge between blocks %d and %d.",
379 block->GetBlockId(),
380 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100381 }
382 }
383 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100384
Calin Juravle1d8b49f2015-05-12 12:40:07 +0000385 // Check Phi uniqueness (no two Phis with the same type refer to the same register).
Calin Juravlea4f88312015-04-16 12:57:19 +0100386 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
387 HPhi* phi = it.Current()->AsPhi();
388 if (phi->GetNextEquivalentPhiWithSameType() != nullptr) {
389 std::stringstream type_str;
390 type_str << phi->GetType();
391 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s",
392 phi->GetId(), phi->GetRegNumber(), type_str.str().c_str()));
393 }
394 }
395
David Brazdilffee3d32015-07-06 11:48:53 +0100396 // Ensure try membership information is consistent.
David Brazdilffee3d32015-07-06 11:48:53 +0100397 if (block->IsCatchBlock()) {
David Brazdilec16f792015-08-19 15:04:01 +0100398 if (block->IsTryBlock()) {
399 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +0100400 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
401 "has try entry %s:%d.",
402 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100403 try_entry.DebugName(),
404 try_entry.GetId()));
David Brazdilffee3d32015-07-06 11:48:53 +0100405 }
406
407 if (block->IsLoopHeader()) {
408 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
409 block->GetBlockId()));
410 }
411 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000412 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilec16f792015-08-19 15:04:01 +0100413 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
414 if (block->IsTryBlock()) {
415 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
416 if (incoming_try_entry == nullptr) {
417 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100418 "from predecessor %d.",
419 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100420 stored_try_entry.DebugName(),
421 stored_try_entry.GetId(),
422 predecessor->GetBlockId()));
423 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
424 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
425 "with %s:%d that follows from predecessor %d.",
426 block->GetBlockId(),
427 stored_try_entry.DebugName(),
428 stored_try_entry.GetId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100429 incoming_try_entry->DebugName(),
430 incoming_try_entry->GetId(),
431 predecessor->GetBlockId()));
432 }
David Brazdilec16f792015-08-19 15:04:01 +0100433 } else if (incoming_try_entry != nullptr) {
434 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100435 "from predecessor %d.",
436 block->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100437 incoming_try_entry->DebugName(),
438 incoming_try_entry->GetId(),
439 predecessor->GetBlockId()));
440 }
441 }
442 }
443
Roland Levillain6b879dd2014-09-22 17:13:44 +0100444 if (block->IsLoopHeader()) {
445 CheckLoop(block);
446 }
447}
448
449void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
450 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100451 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100452
453 // Ensure the pre-header block is first in the list of
454 // predecessors of a loop header.
455 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000456 AddError(StringPrintf(
457 "Loop pre-header is not the first predecessor of the loop header %d.",
458 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100459 }
460
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100461 // Ensure the loop header has only one incoming branch and the remaining
462 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000463 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000464 if (num_preds < 2) {
465 AddError(StringPrintf(
466 "Loop header %d has less than two predecessors: %zu.",
467 id,
468 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100469 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000470 HBasicBlock* first_predecessor = loop_header->GetPredecessor(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000471 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000472 AddError(StringPrintf(
473 "First predecessor of loop header %d is a back edge.",
474 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100475 }
Vladimir Marko60584552015-09-03 13:35:12 +0000476 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
477 HBasicBlock* predecessor = loop_header->GetPredecessor(i);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100478 if (!loop_information->IsBackEdge(*predecessor)) {
479 AddError(StringPrintf(
480 "Loop header %d has multiple incoming (non back edge) blocks.",
481 id));
482 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100483 }
484 }
485
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100486 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100487
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100488 // Ensure back edges belong to the loop.
489 size_t num_back_edges = loop_information->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000490 if (num_back_edges == 0) {
491 AddError(StringPrintf(
492 "Loop defined by header %d has no back edge.",
493 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100494 } else {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100495 for (size_t i = 0; i < num_back_edges; ++i) {
496 int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId();
497 if (!loop_blocks.IsBitSet(back_edge_id)) {
498 AddError(StringPrintf(
499 "Loop defined by header %d has an invalid back edge %d.",
500 id,
501 back_edge_id));
502 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100503 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100504 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100505
David Brazdil2d7352b2015-04-20 14:52:42 +0100506 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100507 for (uint32_t i : loop_blocks.Indexes()) {
508 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100509 if (loop_block == nullptr) {
510 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
511 id,
512 i));
513 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000514 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100515 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000516 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100517 }
518 }
David Brazdil7d275372015-04-21 16:36:35 +0100519
520 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
521 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
522 HLoopInformation* outer_info = it.Current();
523 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
524 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
525 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100526 id,
David Brazdil7d275372015-04-21 16:36:35 +0100527 outer_info->GetHeader()->GetBlockId()));
528 }
529 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100530}
531
532void SSAChecker::VisitInstruction(HInstruction* instruction) {
533 super_type::VisitInstruction(instruction);
534
Roland Levillaina8069ce2014-10-01 10:48:29 +0100535 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000536 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100537 !use_it.Done(); use_it.Advance()) {
538 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100539 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000540 AddError(StringPrintf("Instruction %d in block %d does not dominate "
541 "use %d in block %d.",
542 instruction->GetId(), current_block_->GetBlockId(),
543 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100544 }
545 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100546
547 // Ensure an instruction having an environment is dominated by the
548 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100549 for (HEnvironment* environment = instruction->GetEnvironment();
550 environment != nullptr;
551 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100552 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
553 HInstruction* env_instruction = environment->GetInstructionAt(i);
554 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100555 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000556 AddError(StringPrintf("Instruction %d in environment of instruction %d "
557 "from block %d does not dominate instruction %d.",
558 env_instruction->GetId(),
559 instruction->GetId(),
560 current_block_->GetBlockId(),
561 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100562 }
563 }
564 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100565}
566
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000567static Primitive::Type PrimitiveKind(Primitive::Type type) {
568 switch (type) {
569 case Primitive::kPrimBoolean:
570 case Primitive::kPrimByte:
571 case Primitive::kPrimShort:
572 case Primitive::kPrimChar:
573 case Primitive::kPrimInt:
574 return Primitive::kPrimInt;
575 default:
576 return type;
577 }
578}
579
Roland Levillain6b879dd2014-09-22 17:13:44 +0100580void SSAChecker::VisitPhi(HPhi* phi) {
581 VisitInstruction(phi);
582
583 // Ensure the first input of a phi is not itself.
584 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000585 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
586 phi->GetId(),
587 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100588 }
589
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000590 // Ensure that the inputs have the same primitive kind as the phi.
591 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
592 HInstruction* input = phi->InputAt(i);
593 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
594 AddError(StringPrintf(
595 "Input %d at index %zu of phi %d from block %d does not have the "
596 "same type as the phi: %s versus %s",
597 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
598 Primitive::PrettyDescriptor(input->GetType()),
599 Primitive::PrettyDescriptor(phi->GetType())));
600 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000601 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000602 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
603 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
604 phi->GetId(),
605 phi->GetBlock()->GetBlockId(),
606 Primitive::PrettyDescriptor(phi->GetType())));
607 }
David Brazdilffee3d32015-07-06 11:48:53 +0100608
609 if (phi->IsCatchPhi()) {
610 // The number of inputs of a catch phi corresponds to the total number of
611 // throwing instructions caught by this catch block.
612 } else {
613 // Ensure the number of inputs of a non-catch phi is the same as the number
614 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000615 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
616 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100617 AddError(StringPrintf(
618 "Phi %d in block %d has %zu inputs, "
619 "but block %d has %zu predecessors.",
620 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000621 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100622 } else {
623 // Ensure phi input at index I either comes from the Ith
624 // predecessor or from a block that dominates this predecessor.
625 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
626 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000627 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100628 if (!(input->GetBlock() == predecessor
629 || input->GetBlock()->Dominates(predecessor))) {
630 AddError(StringPrintf(
631 "Input %d at index %zu of phi %d from block %d is not defined in "
632 "predecessor number %zu nor in a block dominating it.",
633 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
634 i));
635 }
636 }
637 }
638 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000639}
640
David Brazdil13b47182015-04-15 16:29:32 +0100641void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
642 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000643 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100644 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000645 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000646 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100647 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
648 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000649 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100650 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000651 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000652 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100653 } else if (input->GetType() == Primitive::kPrimInt
654 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
655 // TODO: We need a data-flow analysis to determine if the Phi or
656 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100657 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000658 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100659 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
660 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000661 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100662 static_cast<int>(input_index),
663 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000664 }
665}
666
David Brazdil13b47182015-04-15 16:29:32 +0100667void SSAChecker::VisitIf(HIf* instruction) {
668 VisitInstruction(instruction);
669 HandleBooleanInput(instruction, 0);
670}
671
672void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
673 VisitInstruction(instruction);
674 HandleBooleanInput(instruction, 0);
675}
676
Nicolas Geoffray31596742014-11-24 15:28:45 +0000677void SSAChecker::VisitCondition(HCondition* op) {
678 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000679 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000680 AddError(StringPrintf(
681 "Condition %s %d has a non-Boolean result type: %s.",
682 op->DebugName(), op->GetId(),
683 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000684 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000685 HInstruction* lhs = op->InputAt(0);
686 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100687 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
688 AddError(StringPrintf(
689 "Condition %s %d has inputs of different types: %s, and %s.",
690 op->DebugName(), op->GetId(),
691 Primitive::PrettyDescriptor(lhs->GetType()),
692 Primitive::PrettyDescriptor(rhs->GetType())));
693 }
694 if (!op->IsEqual() && !op->IsNotEqual()) {
695 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000696 AddError(StringPrintf(
697 "Condition %s %d uses an object as left-hand side input.",
698 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100699 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000700 AddError(StringPrintf(
701 "Condition %s %d uses an object as right-hand side input.",
702 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000703 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000704 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000705}
706
707void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
708 VisitInstruction(op);
709 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
710 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000711 AddError(StringPrintf(
712 "Shift operation %s %d has a non-int kind second input: "
713 "%s of type %s.",
714 op->DebugName(), op->GetId(),
715 op->InputAt(1)->DebugName(),
716 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000717 }
718 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100719 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000720 AddError(StringPrintf(
721 "Binary operation %s %d has inputs of different types: "
722 "%s, and %s.",
723 op->DebugName(), op->GetId(),
724 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
725 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000726 }
727 }
728
729 if (op->IsCompare()) {
730 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000731 AddError(StringPrintf(
732 "Compare operation %d has a non-int result type: %s.",
733 op->GetId(),
734 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000735 }
736 } else {
737 // Use the first input, so that we can also make this check for shift operations.
738 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000739 AddError(StringPrintf(
740 "Binary operation %s %d has a result type different "
741 "from its input type: %s vs %s.",
742 op->DebugName(), op->GetId(),
743 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100744 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000745 }
746 }
747}
748
David Brazdil8d5b8b22015-03-24 10:51:52 +0000749void SSAChecker::VisitConstant(HConstant* instruction) {
750 HBasicBlock* block = instruction->GetBlock();
751 if (!block->IsEntryBlock()) {
752 AddError(StringPrintf(
753 "%s %d should be in the entry block but is in block %d.",
754 instruction->DebugName(),
755 instruction->GetId(),
756 block->GetBlockId()));
757 }
758}
759
Roland Levillainccc07a92014-09-16 14:48:16 +0100760} // namespace art