blob: 074ed710258c4e0059e15ce455722e8af5a1f5c5 [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
David Brazdilffee3d32015-07-06 11:48:53 +0100385 // Ensure try membership information is consistent.
David Brazdilffee3d32015-07-06 11:48:53 +0100386 if (block->IsCatchBlock()) {
David Brazdilec16f792015-08-19 15:04:01 +0100387 if (block->IsTryBlock()) {
388 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +0100389 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
390 "has try entry %s:%d.",
391 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100392 try_entry.DebugName(),
393 try_entry.GetId()));
David Brazdilffee3d32015-07-06 11:48:53 +0100394 }
395
396 if (block->IsLoopHeader()) {
397 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
398 block->GetBlockId()));
399 }
400 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000401 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilec16f792015-08-19 15:04:01 +0100402 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
403 if (block->IsTryBlock()) {
404 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
405 if (incoming_try_entry == nullptr) {
406 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100407 "from predecessor %d.",
408 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100409 stored_try_entry.DebugName(),
410 stored_try_entry.GetId(),
411 predecessor->GetBlockId()));
412 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
413 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
414 "with %s:%d that follows from predecessor %d.",
415 block->GetBlockId(),
416 stored_try_entry.DebugName(),
417 stored_try_entry.GetId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100418 incoming_try_entry->DebugName(),
419 incoming_try_entry->GetId(),
420 predecessor->GetBlockId()));
421 }
David Brazdilec16f792015-08-19 15:04:01 +0100422 } else if (incoming_try_entry != nullptr) {
423 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100424 "from predecessor %d.",
425 block->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100426 incoming_try_entry->DebugName(),
427 incoming_try_entry->GetId(),
428 predecessor->GetBlockId()));
429 }
430 }
431 }
432
Roland Levillain6b879dd2014-09-22 17:13:44 +0100433 if (block->IsLoopHeader()) {
434 CheckLoop(block);
435 }
436}
437
438void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
439 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100440 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100441
442 // Ensure the pre-header block is first in the list of
443 // predecessors of a loop header.
444 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000445 AddError(StringPrintf(
446 "Loop pre-header is not the first predecessor of the loop header %d.",
447 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100448 }
449
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100450 // Ensure the loop header has only one incoming branch and the remaining
451 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000452 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000453 if (num_preds < 2) {
454 AddError(StringPrintf(
455 "Loop header %d has less than two predecessors: %zu.",
456 id,
457 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100458 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000459 HBasicBlock* first_predecessor = loop_header->GetPredecessor(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000460 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000461 AddError(StringPrintf(
462 "First predecessor of loop header %d is a back edge.",
463 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100464 }
Vladimir Marko60584552015-09-03 13:35:12 +0000465 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
466 HBasicBlock* predecessor = loop_header->GetPredecessor(i);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100467 if (!loop_information->IsBackEdge(*predecessor)) {
468 AddError(StringPrintf(
469 "Loop header %d has multiple incoming (non back edge) blocks.",
470 id));
471 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100472 }
473 }
474
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100475 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100476
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100477 // Ensure back edges belong to the loop.
478 size_t num_back_edges = loop_information->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000479 if (num_back_edges == 0) {
480 AddError(StringPrintf(
481 "Loop defined by header %d has no back edge.",
482 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100483 } else {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100484 for (size_t i = 0; i < num_back_edges; ++i) {
485 int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId();
486 if (!loop_blocks.IsBitSet(back_edge_id)) {
487 AddError(StringPrintf(
488 "Loop defined by header %d has an invalid back edge %d.",
489 id,
490 back_edge_id));
491 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100492 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100493 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100494
David Brazdil2d7352b2015-04-20 14:52:42 +0100495 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100496 for (uint32_t i : loop_blocks.Indexes()) {
497 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100498 if (loop_block == nullptr) {
499 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
500 id,
501 i));
502 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000503 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100504 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000505 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100506 }
507 }
David Brazdil7d275372015-04-21 16:36:35 +0100508
509 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
510 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
511 HLoopInformation* outer_info = it.Current();
512 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
513 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
514 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100515 id,
David Brazdil7d275372015-04-21 16:36:35 +0100516 outer_info->GetHeader()->GetBlockId()));
517 }
518 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100519}
520
521void SSAChecker::VisitInstruction(HInstruction* instruction) {
522 super_type::VisitInstruction(instruction);
523
Roland Levillaina8069ce2014-10-01 10:48:29 +0100524 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000525 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100526 !use_it.Done(); use_it.Advance()) {
527 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100528 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000529 AddError(StringPrintf("Instruction %d in block %d does not dominate "
530 "use %d in block %d.",
531 instruction->GetId(), current_block_->GetBlockId(),
532 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100533 }
534 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100535
536 // Ensure an instruction having an environment is dominated by the
537 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100538 for (HEnvironment* environment = instruction->GetEnvironment();
539 environment != nullptr;
540 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100541 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
542 HInstruction* env_instruction = environment->GetInstructionAt(i);
543 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100544 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000545 AddError(StringPrintf("Instruction %d in environment of instruction %d "
546 "from block %d does not dominate instruction %d.",
547 env_instruction->GetId(),
548 instruction->GetId(),
549 current_block_->GetBlockId(),
550 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100551 }
552 }
553 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100554}
555
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000556static Primitive::Type PrimitiveKind(Primitive::Type type) {
557 switch (type) {
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimByte:
560 case Primitive::kPrimShort:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimInt:
563 return Primitive::kPrimInt;
564 default:
565 return type;
566 }
567}
568
David Brazdil77a48ae2015-09-15 12:34:04 +0000569static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
570 return insn1->IsConstant()
571 && insn2->IsConstant()
572 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
573}
574
575static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
576 if (insn1->IsPhi() &&
577 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
578 insn1->InputCount() == insn2->InputCount()) {
579 // Testing only one of the two inputs for recursion is sufficient.
580 if (visited->IsBitSet(insn1->GetId())) {
581 return true;
582 }
583 visited->SetBit(insn1->GetId());
584
585 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
586 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
587 return false;
588 }
589 }
590 return true;
591 } else if (IsSameSizeConstant(insn1, insn2)) {
592 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
593 } else {
594 return false;
595 }
596}
597
Roland Levillain6b879dd2014-09-22 17:13:44 +0100598void SSAChecker::VisitPhi(HPhi* phi) {
599 VisitInstruction(phi);
600
601 // Ensure the first input of a phi is not itself.
602 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000603 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
604 phi->GetId(),
605 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100606 }
607
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000608 // Ensure that the inputs have the same primitive kind as the phi.
609 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
610 HInstruction* input = phi->InputAt(i);
611 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
612 AddError(StringPrintf(
613 "Input %d at index %zu of phi %d from block %d does not have the "
614 "same type as the phi: %s versus %s",
615 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
616 Primitive::PrettyDescriptor(input->GetType()),
617 Primitive::PrettyDescriptor(phi->GetType())));
618 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000619 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000620 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
621 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
622 phi->GetId(),
623 phi->GetBlock()->GetBlockId(),
624 Primitive::PrettyDescriptor(phi->GetType())));
625 }
David Brazdilffee3d32015-07-06 11:48:53 +0100626
627 if (phi->IsCatchPhi()) {
628 // The number of inputs of a catch phi corresponds to the total number of
629 // throwing instructions caught by this catch block.
630 } else {
631 // Ensure the number of inputs of a non-catch phi is the same as the number
632 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000633 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
634 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100635 AddError(StringPrintf(
636 "Phi %d in block %d has %zu inputs, "
637 "but block %d has %zu predecessors.",
638 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000639 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100640 } else {
641 // Ensure phi input at index I either comes from the Ith
642 // predecessor or from a block that dominates this predecessor.
643 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
644 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000645 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100646 if (!(input->GetBlock() == predecessor
647 || input->GetBlock()->Dominates(predecessor))) {
648 AddError(StringPrintf(
649 "Input %d at index %zu of phi %d from block %d is not defined in "
650 "predecessor number %zu nor in a block dominating it.",
651 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
652 i));
653 }
654 }
655 }
656 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000657
658 // Ensure that catch phis are sorted by their vreg number, as required by
659 // the register allocator and code generator. This does not apply to normal
660 // phis which can be constructed artifically.
661 if (phi->IsCatchPhi()) {
662 HInstruction* next_phi = phi->GetNext();
663 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
664 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
665 "vreg numbers.",
666 phi->GetId(),
667 next_phi->GetId(),
668 phi->GetBlock()->GetBlockId()));
669 }
670 }
671
672 // Test phi equivalents. There should not be two of the same type and they
673 // should only be created for constants which were untyped in DEX.
674 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
675 HPhi* other_phi = phi_it.Current()->AsPhi();
676 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
677 if (phi->GetType() == other_phi->GetType()) {
678 std::stringstream type_str;
679 type_str << phi->GetType();
680 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
681 phi->GetId(),
682 phi->GetRegNumber(),
683 type_str.str().c_str()));
684 } else {
685 ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true);
686 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
687 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
688 "are not equivalents of constants.",
689 phi->GetId(),
690 other_phi->GetId(),
691 phi->GetRegNumber()));
692 }
693 }
694 }
695 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000696}
697
David Brazdil13b47182015-04-15 16:29:32 +0100698void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
699 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000700 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100701 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000702 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000703 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100704 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
705 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000706 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100707 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000708 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000709 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100710 } else if (input->GetType() == Primitive::kPrimInt
711 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
712 // TODO: We need a data-flow analysis to determine if the Phi or
713 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100714 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000715 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100716 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
717 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000718 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100719 static_cast<int>(input_index),
720 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000721 }
722}
723
David Brazdil13b47182015-04-15 16:29:32 +0100724void SSAChecker::VisitIf(HIf* instruction) {
725 VisitInstruction(instruction);
726 HandleBooleanInput(instruction, 0);
727}
728
729void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
730 VisitInstruction(instruction);
731 HandleBooleanInput(instruction, 0);
732}
733
Nicolas Geoffray31596742014-11-24 15:28:45 +0000734void SSAChecker::VisitCondition(HCondition* op) {
735 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000736 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000737 AddError(StringPrintf(
738 "Condition %s %d has a non-Boolean result type: %s.",
739 op->DebugName(), op->GetId(),
740 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000741 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000742 HInstruction* lhs = op->InputAt(0);
743 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100744 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
745 AddError(StringPrintf(
746 "Condition %s %d has inputs of different types: %s, and %s.",
747 op->DebugName(), op->GetId(),
748 Primitive::PrettyDescriptor(lhs->GetType()),
749 Primitive::PrettyDescriptor(rhs->GetType())));
750 }
751 if (!op->IsEqual() && !op->IsNotEqual()) {
752 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000753 AddError(StringPrintf(
754 "Condition %s %d uses an object as left-hand side input.",
755 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100756 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000757 AddError(StringPrintf(
758 "Condition %s %d uses an object as right-hand side input.",
759 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000760 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000761 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000762}
763
764void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
765 VisitInstruction(op);
766 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
767 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000768 AddError(StringPrintf(
769 "Shift operation %s %d has a non-int kind second input: "
770 "%s of type %s.",
771 op->DebugName(), op->GetId(),
772 op->InputAt(1)->DebugName(),
773 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000774 }
775 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100776 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000777 AddError(StringPrintf(
778 "Binary operation %s %d has inputs of different types: "
779 "%s, and %s.",
780 op->DebugName(), op->GetId(),
781 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
782 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000783 }
784 }
785
786 if (op->IsCompare()) {
787 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000788 AddError(StringPrintf(
789 "Compare operation %d has a non-int result type: %s.",
790 op->GetId(),
791 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000792 }
793 } else {
794 // Use the first input, so that we can also make this check for shift operations.
795 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000796 AddError(StringPrintf(
797 "Binary operation %s %d has a result type different "
798 "from its input type: %s vs %s.",
799 op->DebugName(), op->GetId(),
800 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100801 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000802 }
803 }
804}
805
David Brazdil8d5b8b22015-03-24 10:51:52 +0000806void SSAChecker::VisitConstant(HConstant* instruction) {
807 HBasicBlock* block = instruction->GetBlock();
808 if (!block->IsEntryBlock()) {
809 AddError(StringPrintf(
810 "%s %d should be in the entry block but is in block %d.",
811 instruction->DebugName(),
812 instruction->GetId(),
813 block->GetBlockId()));
814 }
815}
816
Roland Levillainccc07a92014-09-16 14:48:16 +0100817} // namespace art