blob: e4bc9e68e1bf043ae0a1c8aca74da5624a2ae03d [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`.
32 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
33 std::map<HBasicBlock*, size_t> predecessors_count;
34 for (size_t i = 0, e = predecessors.Size(); i < e; ++i) {
35 HBasicBlock* p = predecessors.Get(i);
36 ++predecessors_count[p];
37 }
38 for (auto& pc : predecessors_count) {
39 HBasicBlock* p = pc.first;
40 size_t p_count_in_block_predecessors = pc.second;
41 const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors();
42 size_t block_count_in_p_successors = 0;
43 for (size_t j = 0, f = p_successors.Size(); j < f; ++j) {
44 if (p_successors.Get(j) == block) {
45 ++block_count_in_p_successors;
46 }
47 }
48 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000049 AddError(StringPrintf(
50 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
51 "block %d lists %zu occurrences of block %d in its successors.",
52 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
53 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010054 }
55 }
56
57 // Check consistency with respect to successors of `block`.
58 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
59 std::map<HBasicBlock*, size_t> successors_count;
60 for (size_t i = 0, e = successors.Size(); i < e; ++i) {
61 HBasicBlock* s = successors.Get(i);
62 ++successors_count[s];
63 }
64 for (auto& sc : successors_count) {
65 HBasicBlock* s = sc.first;
66 size_t s_count_in_block_successors = sc.second;
67 const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors();
68 size_t block_count_in_s_predecessors = 0;
69 for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) {
70 if (s_predecessors.Get(j) == block) {
71 ++block_count_in_s_predecessors;
72 }
73 }
74 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000075 AddError(StringPrintf(
76 "Block %d lists %zu occurrences of block %d in its successors, whereas "
77 "block %d lists %zu occurrences of block %d in its predecessors.",
78 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
79 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010080 }
81 }
82
83 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000084 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
85 // dead code that falls out of the method will not end with a control-flow
86 // instruction. Such code is removed during the SSA-building DCE phase.
87 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000088 AddError(StringPrintf("Block %d does not end with a branch instruction.",
89 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010090 }
91
David Brazdilb618ade2015-07-29 10:31:29 +010092 // Ensure that the only Return(Void) and Throw jump to Exit. An exiting
93 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
94 if (block->IsExitBlock()) {
95 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
96 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
97 if (predecessor->IsSingleTryBoundary()
98 && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) {
99 HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor();
100 HInstruction* last_instruction = real_predecessor->GetLastInstruction();
101 if (!last_instruction->IsThrow()) {
102 AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.",
103 last_instruction->DebugName(),
104 last_instruction->GetId()));
105 }
106 } else {
107 HInstruction* last_instruction = predecessor->GetLastInstruction();
108 if (!last_instruction->IsReturn()
109 && !last_instruction->IsReturnVoid()
110 && !last_instruction->IsThrow()) {
111 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
112 last_instruction->DebugName(),
113 last_instruction->GetId()));
114 }
115 }
116 }
117 }
118
Roland Levillainccc07a92014-09-16 14:48:16 +0100119 // Visit this block's list of phis.
120 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100121 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100123 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000124 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
125 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100126 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100127 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
128 AddError(StringPrintf("The recorded last phi of block %d does not match "
129 "the actual last phi %d.",
130 current_block_->GetBlockId(),
131 current->GetId()));
132 }
133 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100134 }
135
136 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100137 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
138 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100139 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000141 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
142 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100143 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
145 AddError(StringPrintf("The recorded last instruction of block %d does not match "
146 "the actual last instruction %d.",
147 current_block_->GetBlockId(),
148 current->GetId()));
149 }
150 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 }
152}
153
Mark Mendell1152c922015-04-24 17:06:35 -0400154void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
155 if (!GetGraph()->HasBoundsChecks()) {
156 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
157 "but HasBoundsChecks() returns false",
158 check->DebugName(),
159 check->GetId()));
160 }
161
162 // Perform the instruction base checks too.
163 VisitInstruction(check);
164}
165
David Brazdilffee3d32015-07-06 11:48:53 +0100166void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
167 // Ensure that all exception handlers are catch blocks and that handlers
168 // are not listed multiple times.
169 // Note that a normal-flow successor may be a catch block before CFG
170 // simplification. We only test normal-flow successors in SsaChecker.
171 for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) {
172 HBasicBlock* handler = it.Current();
173 if (!handler->IsCatchBlock()) {
174 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
175 "is not a catch block.",
176 current_block_->GetBlockId(),
177 try_boundary->DebugName(),
178 try_boundary->GetId(),
179 handler->GetBlockId()));
180 }
181 if (current_block_->GetSuccessors().Contains(
182 handler, /* start_from */ it.CurrentSuccessorIndex() + 1)) {
183 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
184 handler->GetBlockId(),
185 try_boundary->DebugName(),
186 try_boundary->GetId()));
187 }
188 }
189
190 VisitInstruction(try_boundary);
191}
192
Roland Levillainccc07a92014-09-16 14:48:16 +0100193void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000194 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000195 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
196 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000197 } else {
198 seen_ids_.SetBit(instruction->GetId());
199 }
200
Roland Levillainccc07a92014-09-16 14:48:16 +0100201 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000202 if (instruction->GetBlock() == nullptr) {
203 AddError(StringPrintf("%s %d in block %d not associated with any block.",
204 instruction->IsPhi() ? "Phi" : "Instruction",
205 instruction->GetId(),
206 current_block_->GetBlockId()));
207 } else if (instruction->GetBlock() != current_block_) {
208 AddError(StringPrintf("%s %d in block %d associated with block %d.",
209 instruction->IsPhi() ? "Phi" : "Instruction",
210 instruction->GetId(),
211 current_block_->GetBlockId(),
212 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100213 }
Roland Levillain6b469232014-09-25 10:10:38 +0100214
215 // Ensure the inputs of `instruction` are defined in a block of the graph.
216 for (HInputIterator input_it(instruction); !input_it.Done();
217 input_it.Advance()) {
218 HInstruction* input = input_it.Current();
219 const HInstructionList& list = input->IsPhi()
220 ? input->GetBlock()->GetPhis()
221 : input->GetBlock()->GetInstructions();
222 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000223 AddError(StringPrintf("Input %d of instruction %d is not defined "
224 "in a basic block of the control-flow graph.",
225 input->GetId(),
226 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100227 }
228 }
229
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100230 // Ensure the uses of `instruction` are defined in a block of the graph,
231 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000232 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100233 !use_it.Done(); use_it.Advance()) {
234 HInstruction* use = use_it.Current()->GetUser();
235 const HInstructionList& list = use->IsPhi()
236 ? use->GetBlock()->GetPhis()
237 : use->GetBlock()->GetInstructions();
238 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000239 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000240 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000241 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000242 use->GetId(),
243 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100244 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100245 size_t use_index = use_it.Current()->GetIndex();
246 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
247 AddError(StringPrintf("User %s:%d of instruction %d has a wrong "
248 "UseListNode index.",
249 use->DebugName(),
250 use->GetId(),
251 instruction->GetId()));
252 }
253 }
254
255 // Ensure the environment uses entries are consistent.
256 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
257 !use_it.Done(); use_it.Advance()) {
258 HEnvironment* use = use_it.Current()->GetUser();
259 size_t use_index = use_it.Current()->GetIndex();
260 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
261 AddError(StringPrintf("Environment user of %s:%d has a wrong "
262 "UseListNode index.",
263 instruction->DebugName(),
264 instruction->GetId()));
265 }
Roland Levillain6b469232014-09-25 10:10:38 +0100266 }
David Brazdil1abb4192015-02-17 18:33:36 +0000267
268 // Ensure 'instruction' has pointers to its inputs' use entries.
269 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
270 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
271 HInstruction* input = input_record.GetInstruction();
272 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100273 size_t use_index = use_node->GetIndex();
274 if ((use_node == nullptr)
275 || !input->GetUses().Contains(use_node)
276 || (use_index >= e)
277 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000278 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
279 "at input %u (%s:%d).",
280 instruction->DebugName(),
281 instruction->GetId(),
282 static_cast<unsigned>(i),
283 input->DebugName(),
284 input->GetId()));
285 }
286 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100287}
288
Roland Levillain4c0eb422015-04-24 16:43:49 +0100289void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
290 VisitInstruction(invoke);
291
292 if (invoke->IsStaticWithExplicitClinitCheck()) {
293 size_t last_input_index = invoke->InputCount() - 1;
294 HInstruction* last_input = invoke->InputAt(last_input_index);
295 if (last_input == nullptr) {
296 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
297 "has a null pointer as last input.",
298 invoke->DebugName(),
299 invoke->GetId()));
300 }
301 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
302 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
303 "has a last instruction (%s:%d) which is neither a clinit check "
304 "nor a load class instruction.",
305 invoke->DebugName(),
306 invoke->GetId(),
307 last_input->DebugName(),
308 last_input->GetId()));
309 }
310 }
311}
312
David Brazdilfc6a86a2015-06-26 10:33:45 +0000313void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100314 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000315 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
316 AddError(StringPrintf("%s:%d does not jump to the exit block.",
317 ret->DebugName(),
318 ret->GetId()));
319 }
320}
321
322void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100323 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000324 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
325 AddError(StringPrintf("%s:%d does not jump to the exit block.",
326 ret->DebugName(),
327 ret->GetId()));
328 }
329}
330
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100331void GraphChecker::VisitCheckCast(HCheckCast* check) {
332 VisitInstruction(check);
333 HInstruction* input = check->InputAt(1);
334 if (!input->IsLoadClass()) {
335 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
336 check->DebugName(),
337 check->GetId(),
338 input->DebugName(),
339 input->GetId()));
340 }
341}
342
343void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
344 VisitInstruction(instruction);
345 HInstruction* input = instruction->InputAt(1);
346 if (!input->IsLoadClass()) {
347 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
348 instruction->DebugName(),
349 instruction->GetId(),
350 input->DebugName(),
351 input->GetId()));
352 }
353}
354
Roland Levillainccc07a92014-09-16 14:48:16 +0100355void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
356 super_type::VisitBasicBlock(block);
357
David Brazdilb618ade2015-07-29 10:31:29 +0100358 // Ensure that only catch blocks have exceptional predecessors, and if they do
359 // these are instructions which throw into them.
360 if (block->IsCatchBlock()) {
361 for (size_t i = 0, e = block->GetExceptionalPredecessors().Size(); i < e; ++i) {
362 HInstruction* thrower = block->GetExceptionalPredecessors().Get(i);
363 HBasicBlock* try_block = thrower->GetBlock();
364 if (!thrower->CanThrow()) {
365 AddError(StringPrintf("Exceptional predecessor %s:%d of catch block %d does not throw.",
366 thrower->DebugName(),
367 thrower->GetId(),
368 block->GetBlockId()));
369 } else if (!try_block->IsInTry()) {
370 AddError(StringPrintf("Exceptional predecessor %s:%d of catch block %d "
371 "is not in a try block.",
372 thrower->DebugName(),
373 thrower->GetId(),
374 block->GetBlockId()));
375 } else if (!try_block->GetTryEntry()->HasExceptionHandler(*block)) {
376 AddError(StringPrintf("Catch block %d is not an exception handler of "
377 "its exceptional predecessor %s:%d.",
378 block->GetBlockId(),
379 thrower->DebugName(),
380 thrower->GetId()));
381 }
382 }
383 } else {
384 if (!block->GetExceptionalPredecessors().IsEmpty()) {
385 AddError(StringPrintf("Normal block %d has %zu exceptional predecessors.",
386 block->GetBlockId(),
387 block->GetExceptionalPredecessors().Size()));
388 }
389 }
390
David Brazdilffee3d32015-07-06 11:48:53 +0100391 // Ensure that catch blocks are not normal successors, and normal blocks are
392 // never exceptional successors.
393 const size_t num_normal_successors = block->NumberOfNormalSuccessors();
394 for (size_t j = 0; j < num_normal_successors; ++j) {
395 HBasicBlock* successor = block->GetSuccessors().Get(j);
396 if (successor->IsCatchBlock()) {
397 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
398 successor->GetBlockId(),
399 block->GetBlockId()));
400 }
401 }
402 for (size_t j = num_normal_successors, e = block->GetSuccessors().Size(); j < e; ++j) {
403 HBasicBlock* successor = block->GetSuccessors().Get(j);
404 if (!successor->IsCatchBlock()) {
405 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
406 successor->GetBlockId(),
407 block->GetBlockId()));
408 }
409 }
410
Roland Levillainccc07a92014-09-16 14:48:16 +0100411 // Ensure there is no critical edge (i.e., an edge connecting a
412 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100413 // predecessors). Exceptional edges are synthesized and hence
414 // not accounted for.
415 if (block->NumberOfNormalSuccessors() > 1) {
416 for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) {
Roland Levillainccc07a92014-09-16 14:48:16 +0100417 HBasicBlock* successor = block->GetSuccessors().Get(j);
418 if (successor->GetPredecessors().Size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000419 AddError(StringPrintf("Critical edge between blocks %d and %d.",
420 block->GetBlockId(),
421 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100422 }
423 }
424 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100425
Calin Juravle1d8b49f2015-05-12 12:40:07 +0000426 // Check Phi uniqueness (no two Phis with the same type refer to the same register).
Calin Juravlea4f88312015-04-16 12:57:19 +0100427 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
428 HPhi* phi = it.Current()->AsPhi();
429 if (phi->GetNextEquivalentPhiWithSameType() != nullptr) {
430 std::stringstream type_str;
431 type_str << phi->GetType();
432 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s",
433 phi->GetId(), phi->GetRegNumber(), type_str.str().c_str()));
434 }
435 }
436
David Brazdilffee3d32015-07-06 11:48:53 +0100437 // Ensure try membership information is consistent.
438 HTryBoundary* try_entry = block->GetTryEntry();
439 if (block->IsCatchBlock()) {
440 if (try_entry != nullptr) {
441 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
442 "has try entry %s:%d.",
443 block->GetBlockId(),
444 try_entry->DebugName(),
445 try_entry->GetId()));
446 }
447
448 if (block->IsLoopHeader()) {
449 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
450 block->GetBlockId()));
451 }
452 } else {
453 for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) {
454 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
455 HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
456 if (try_entry == nullptr) {
457 if (incoming_try_entry != nullptr) {
458 AddError(StringPrintf("Block %d has no try entry but try entry %s:%d follows "
459 "from predecessor %d.",
460 block->GetBlockId(),
461 incoming_try_entry->DebugName(),
462 incoming_try_entry->GetId(),
463 predecessor->GetBlockId()));
464 }
465 } else if (incoming_try_entry == nullptr) {
466 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
467 "from predecessor %d.",
468 block->GetBlockId(),
469 try_entry->DebugName(),
470 try_entry->GetId(),
471 predecessor->GetBlockId()));
472 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(*try_entry)) {
473 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
474 "with %s:%d that follows from predecessor %d.",
475 block->GetBlockId(),
476 try_entry->DebugName(),
477 try_entry->GetId(),
478 incoming_try_entry->DebugName(),
479 incoming_try_entry->GetId(),
480 predecessor->GetBlockId()));
481 }
482 }
483 }
484
Roland Levillain6b879dd2014-09-22 17:13:44 +0100485 if (block->IsLoopHeader()) {
486 CheckLoop(block);
487 }
488}
489
490void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
491 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100492 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100493
494 // Ensure the pre-header block is first in the list of
495 // predecessors of a loop header.
496 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000497 AddError(StringPrintf(
498 "Loop pre-header is not the first predecessor of the loop header %d.",
499 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100500 }
501
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100502 // Ensure the loop header has only one incoming branch and the remaining
503 // predecessors are back edges.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000504 size_t num_preds = loop_header->GetPredecessors().Size();
505 if (num_preds < 2) {
506 AddError(StringPrintf(
507 "Loop header %d has less than two predecessors: %zu.",
508 id,
509 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100510 } else {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100511 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000512 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000513 AddError(StringPrintf(
514 "First predecessor of loop header %d is a back edge.",
515 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100516 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100517 for (size_t i = 1, e = loop_header->GetPredecessors().Size(); i < e; ++i) {
518 HBasicBlock* predecessor = loop_header->GetPredecessors().Get(i);
519 if (!loop_information->IsBackEdge(*predecessor)) {
520 AddError(StringPrintf(
521 "Loop header %d has multiple incoming (non back edge) blocks.",
522 id));
523 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100524 }
525 }
526
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100527 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100528
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100529 // Ensure back edges belong to the loop.
530 size_t num_back_edges = loop_information->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000531 if (num_back_edges == 0) {
532 AddError(StringPrintf(
533 "Loop defined by header %d has no back edge.",
534 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100535 } else {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100536 for (size_t i = 0; i < num_back_edges; ++i) {
537 int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId();
538 if (!loop_blocks.IsBitSet(back_edge_id)) {
539 AddError(StringPrintf(
540 "Loop defined by header %d has an invalid back edge %d.",
541 id,
542 back_edge_id));
543 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100544 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100545 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100546
David Brazdil2d7352b2015-04-20 14:52:42 +0100547 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100548 for (uint32_t i : loop_blocks.Indexes()) {
549 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100550 if (loop_block == nullptr) {
551 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
552 id,
553 i));
554 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000555 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100556 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000557 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100558 }
559 }
David Brazdil7d275372015-04-21 16:36:35 +0100560
561 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
562 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
563 HLoopInformation* outer_info = it.Current();
564 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
565 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
566 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100567 id,
David Brazdil7d275372015-04-21 16:36:35 +0100568 outer_info->GetHeader()->GetBlockId()));
569 }
570 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100571}
572
573void SSAChecker::VisitInstruction(HInstruction* instruction) {
574 super_type::VisitInstruction(instruction);
David Brazdilb618ade2015-07-29 10:31:29 +0100575 HBasicBlock* block = instruction->GetBlock();
Roland Levillainccc07a92014-09-16 14:48:16 +0100576
Roland Levillaina8069ce2014-10-01 10:48:29 +0100577 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000578 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100579 !use_it.Done(); use_it.Advance()) {
580 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100581 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000582 AddError(StringPrintf("Instruction %d in block %d does not dominate "
583 "use %d in block %d.",
584 instruction->GetId(), current_block_->GetBlockId(),
585 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100586 }
587 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100588
589 // Ensure an instruction having an environment is dominated by the
590 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100591 for (HEnvironment* environment = instruction->GetEnvironment();
592 environment != nullptr;
593 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100594 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
595 HInstruction* env_instruction = environment->GetInstructionAt(i);
596 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100597 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000598 AddError(StringPrintf("Instruction %d in environment of instruction %d "
599 "from block %d does not dominate instruction %d.",
600 env_instruction->GetId(),
601 instruction->GetId(),
602 current_block_->GetBlockId(),
603 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100604 }
605 }
606 }
David Brazdilb618ade2015-07-29 10:31:29 +0100607
608 // Ensure that throwing instructions in try blocks are listed as exceptional
609 // predecessors in their exception handlers.
610 if (instruction->CanThrow() && block->IsInTry()) {
611 for (HExceptionHandlerIterator handler_it(*block->GetTryEntry());
612 !handler_it.Done();
613 handler_it.Advance()) {
614 if (!handler_it.Current()->GetExceptionalPredecessors().Contains(instruction)) {
615 AddError(StringPrintf("Instruction %s:%d is in try block %d and can throw "
616 "but its exception handler %d does not list it in "
617 "its exceptional predecessors.",
618 instruction->DebugName(),
619 instruction->GetId(),
620 block->GetBlockId(),
621 handler_it.Current()->GetBlockId()));
622 }
623 }
624 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100625}
626
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000627static Primitive::Type PrimitiveKind(Primitive::Type type) {
628 switch (type) {
629 case Primitive::kPrimBoolean:
630 case Primitive::kPrimByte:
631 case Primitive::kPrimShort:
632 case Primitive::kPrimChar:
633 case Primitive::kPrimInt:
634 return Primitive::kPrimInt;
635 default:
636 return type;
637 }
638}
639
Roland Levillain6b879dd2014-09-22 17:13:44 +0100640void SSAChecker::VisitPhi(HPhi* phi) {
641 VisitInstruction(phi);
642
643 // Ensure the first input of a phi is not itself.
644 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000645 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
646 phi->GetId(),
647 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100648 }
649
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000650 // Ensure that the inputs have the same primitive kind as the phi.
651 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
652 HInstruction* input = phi->InputAt(i);
653 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
654 AddError(StringPrintf(
655 "Input %d at index %zu of phi %d from block %d does not have the "
656 "same type as the phi: %s versus %s",
657 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
658 Primitive::PrettyDescriptor(input->GetType()),
659 Primitive::PrettyDescriptor(phi->GetType())));
660 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000661 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000662 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
663 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
664 phi->GetId(),
665 phi->GetBlock()->GetBlockId(),
666 Primitive::PrettyDescriptor(phi->GetType())));
667 }
David Brazdilffee3d32015-07-06 11:48:53 +0100668
669 if (phi->IsCatchPhi()) {
670 // The number of inputs of a catch phi corresponds to the total number of
671 // throwing instructions caught by this catch block.
David Brazdilb618ade2015-07-29 10:31:29 +0100672 const GrowableArray<HInstruction*>& predecessors =
673 phi->GetBlock()->GetExceptionalPredecessors();
674 if (phi->InputCount() != predecessors.Size()) {
675 AddError(StringPrintf(
676 "Phi %d in catch block %d has %zu inputs, "
677 "but catch block %d has %zu exceptional predecessors.",
678 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
679 phi->GetBlock()->GetBlockId(), predecessors.Size()));
680 } else {
681 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
682 HInstruction* input = phi->InputAt(i);
683 HInstruction* thrower = predecessors.Get(i);
684 if (!input->StrictlyDominates(thrower)) {
685 AddError(StringPrintf(
686 "Input %d at index %zu of phi %d from catch block %d does not "
687 "dominate the throwing instruction %s:%d.",
688 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
689 thrower->DebugName(), thrower->GetId()));
690 }
691 }
692 }
David Brazdilffee3d32015-07-06 11:48:53 +0100693 } else {
694 // Ensure the number of inputs of a non-catch phi is the same as the number
695 // of its predecessors.
696 const GrowableArray<HBasicBlock*>& predecessors =
David Brazdilb618ade2015-07-29 10:31:29 +0100697 phi->GetBlock()->GetPredecessors();
David Brazdilffee3d32015-07-06 11:48:53 +0100698 if (phi->InputCount() != predecessors.Size()) {
699 AddError(StringPrintf(
700 "Phi %d in block %d has %zu inputs, "
701 "but block %d has %zu predecessors.",
702 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
703 phi->GetBlock()->GetBlockId(), predecessors.Size()));
704 } else {
705 // Ensure phi input at index I either comes from the Ith
706 // predecessor or from a block that dominates this predecessor.
707 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
708 HInstruction* input = phi->InputAt(i);
709 HBasicBlock* predecessor = predecessors.Get(i);
710 if (!(input->GetBlock() == predecessor
711 || input->GetBlock()->Dominates(predecessor))) {
712 AddError(StringPrintf(
713 "Input %d at index %zu of phi %d from block %d is not defined in "
714 "predecessor number %zu nor in a block dominating it.",
715 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
716 i));
717 }
718 }
719 }
720 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000721}
722
David Brazdil13b47182015-04-15 16:29:32 +0100723void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
724 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000725 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100726 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000727 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000728 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100729 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
730 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000731 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100732 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000733 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000734 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100735 } else if (input->GetType() == Primitive::kPrimInt
736 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
737 // TODO: We need a data-flow analysis to determine if the Phi or
738 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100739 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000740 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100741 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
742 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000743 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100744 static_cast<int>(input_index),
745 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000746 }
747}
748
David Brazdil13b47182015-04-15 16:29:32 +0100749void SSAChecker::VisitIf(HIf* instruction) {
750 VisitInstruction(instruction);
751 HandleBooleanInput(instruction, 0);
752}
753
754void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
755 VisitInstruction(instruction);
756 HandleBooleanInput(instruction, 0);
757}
758
Nicolas Geoffray31596742014-11-24 15:28:45 +0000759void SSAChecker::VisitCondition(HCondition* op) {
760 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000761 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000762 AddError(StringPrintf(
763 "Condition %s %d has a non-Boolean result type: %s.",
764 op->DebugName(), op->GetId(),
765 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000766 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000767 HInstruction* lhs = op->InputAt(0);
768 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100769 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
770 AddError(StringPrintf(
771 "Condition %s %d has inputs of different types: %s, and %s.",
772 op->DebugName(), op->GetId(),
773 Primitive::PrettyDescriptor(lhs->GetType()),
774 Primitive::PrettyDescriptor(rhs->GetType())));
775 }
776 if (!op->IsEqual() && !op->IsNotEqual()) {
777 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000778 AddError(StringPrintf(
779 "Condition %s %d uses an object as left-hand side input.",
780 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100781 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000782 AddError(StringPrintf(
783 "Condition %s %d uses an object as right-hand side input.",
784 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000785 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000786 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000787}
788
789void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
790 VisitInstruction(op);
791 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
792 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000793 AddError(StringPrintf(
794 "Shift operation %s %d has a non-int kind second input: "
795 "%s of type %s.",
796 op->DebugName(), op->GetId(),
797 op->InputAt(1)->DebugName(),
798 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000799 }
800 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100801 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000802 AddError(StringPrintf(
803 "Binary operation %s %d has inputs of different types: "
804 "%s, and %s.",
805 op->DebugName(), op->GetId(),
806 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
807 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000808 }
809 }
810
811 if (op->IsCompare()) {
812 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000813 AddError(StringPrintf(
814 "Compare operation %d has a non-int result type: %s.",
815 op->GetId(),
816 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000817 }
818 } else {
819 // Use the first input, so that we can also make this check for shift operations.
820 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000821 AddError(StringPrintf(
822 "Binary operation %s %d has a result type different "
823 "from its input type: %s vs %s.",
824 op->DebugName(), op->GetId(),
825 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100826 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000827 }
828 }
829}
830
David Brazdil8d5b8b22015-03-24 10:51:52 +0000831void SSAChecker::VisitConstant(HConstant* instruction) {
832 HBasicBlock* block = instruction->GetBlock();
833 if (!block->IsEntryBlock()) {
834 AddError(StringPrintf(
835 "%s %d should be in the entry block but is in block %d.",
836 instruction->DebugName(),
837 instruction->GetId(),
838 block->GetBlockId()));
839 }
840}
841
Roland Levillainccc07a92014-09-16 14:48:16 +0100842} // namespace art