blob: 6d0bdbe19be0aa97cd5e34dbdc24e7b989a2ba6c [file] [log] [blame]
Roland Levillainccc07a92014-09-16 14:48:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "graph_checker.h"
18
Vladimir Marko655e5852015-10-12 10:38:28 +010019#include <algorithm>
Roland Levillainccc07a92014-09-16 14:48:16 +010020#include <map>
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000021#include <string>
Calin Juravlea4f88312015-04-16 12:57:19 +010022#include <sstream>
Roland Levillainccc07a92014-09-16 14:48:16 +010023
Vladimir Marko655e5852015-10-12 10:38:28 +010024#include "base/arena_containers.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010025#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000026#include "base/stringprintf.h"
David Brazdild9510df2015-11-04 23:30:22 +000027#include "handle_scope-inl.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010028
Roland Levillainccc07a92014-09-16 14:48:16 +010029namespace art {
30
31void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
32 current_block_ = block;
33
34 // Check consistency with respect to predecessors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010035 ArenaSafeMap<HBasicBlock*, size_t> predecessors_count(
36 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000037 for (HBasicBlock* p : block->GetPredecessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010038 auto it = predecessors_count.find(p);
39 if (it != predecessors_count.end()) {
40 ++it->second;
41 } else {
42 predecessors_count.Put(p, 1u);
43 }
Roland Levillainccc07a92014-09-16 14:48:16 +010044 }
45 for (auto& pc : predecessors_count) {
46 HBasicBlock* p = pc.first;
47 size_t p_count_in_block_predecessors = pc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010048 size_t block_count_in_p_successors =
49 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010050 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000051 AddError(StringPrintf(
52 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
53 "block %d lists %zu occurrences of block %d in its successors.",
54 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
55 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010056 }
57 }
58
59 // Check consistency with respect to successors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010060 ArenaSafeMap<HBasicBlock*, size_t> successors_count(
61 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000062 for (HBasicBlock* s : block->GetSuccessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010063 auto it = successors_count.find(s);
64 if (it != successors_count.end()) {
65 ++it->second;
66 } else {
67 successors_count.Put(s, 1u);
68 }
Roland Levillainccc07a92014-09-16 14:48:16 +010069 }
70 for (auto& sc : successors_count) {
71 HBasicBlock* s = sc.first;
72 size_t s_count_in_block_successors = sc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010073 size_t block_count_in_s_predecessors =
74 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010075 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000076 AddError(StringPrintf(
77 "Block %d lists %zu occurrences of block %d in its successors, whereas "
78 "block %d lists %zu occurrences of block %d in its predecessors.",
79 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
80 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010081 }
82 }
83
84 // Ensure `block` ends with a branch instruction.
David Brazdilfc6a86a2015-06-26 10:33:45 +000085 // This invariant is not enforced on non-SSA graphs. Graph built from DEX with
86 // dead code that falls out of the method will not end with a control-flow
87 // instruction. Such code is removed during the SSA-building DCE phase.
88 if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000089 AddError(StringPrintf("Block %d does not end with a branch instruction.",
90 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010091 }
92
David Brazdil29fc0082015-08-18 17:17:38 +010093 // Ensure that only Return(Void) and Throw jump to Exit. An exiting
David Brazdilb618ade2015-07-29 10:31:29 +010094 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
95 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +000096 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilb618ade2015-07-29 10:31:29 +010097 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) {
David Brazdild26a4112015-11-10 11:07:31 +0000167 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
168
169 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100170 // Note that a normal-flow successor may be a catch block before CFG
171 // simplification. We only test normal-flow successors in SsaChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000172 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100173 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 }
David Brazdild26a4112015-11-10 11:07:31 +0000181 }
182
183 // Ensure that handlers are not listed multiple times.
184 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
David Brazdild8ef0c62015-11-10 18:49:28 +0000185 if (ContainsElement(handlers, handlers[i], i + 1)) {
186 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000187 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100188 try_boundary->DebugName(),
189 try_boundary->GetId()));
190 }
191 }
192
193 VisitInstruction(try_boundary);
194}
195
David Brazdil9bc43612015-11-05 21:25:24 +0000196void GraphChecker::VisitLoadException(HLoadException* load) {
197 // Ensure that LoadException is the first instruction in a catch block.
198 if (!load->GetBlock()->IsCatchBlock()) {
199 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
200 load->DebugName(),
201 load->GetId(),
202 load->GetBlock()->GetBlockId()));
203 } else if (load->GetBlock()->GetFirstInstruction() != load) {
204 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
205 load->DebugName(),
206 load->GetId(),
207 load->GetBlock()->GetBlockId()));
208 }
209}
210
Roland Levillainccc07a92014-09-16 14:48:16 +0100211void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000212 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000213 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
214 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000215 } else {
216 seen_ids_.SetBit(instruction->GetId());
217 }
218
Roland Levillainccc07a92014-09-16 14:48:16 +0100219 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000220 if (instruction->GetBlock() == nullptr) {
221 AddError(StringPrintf("%s %d in block %d not associated with any block.",
222 instruction->IsPhi() ? "Phi" : "Instruction",
223 instruction->GetId(),
224 current_block_->GetBlockId()));
225 } else if (instruction->GetBlock() != current_block_) {
226 AddError(StringPrintf("%s %d in block %d associated with block %d.",
227 instruction->IsPhi() ? "Phi" : "Instruction",
228 instruction->GetId(),
229 current_block_->GetBlockId(),
230 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100231 }
Roland Levillain6b469232014-09-25 10:10:38 +0100232
233 // Ensure the inputs of `instruction` are defined in a block of the graph.
234 for (HInputIterator input_it(instruction); !input_it.Done();
235 input_it.Advance()) {
236 HInstruction* input = input_it.Current();
237 const HInstructionList& list = input->IsPhi()
238 ? input->GetBlock()->GetPhis()
239 : input->GetBlock()->GetInstructions();
240 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000241 AddError(StringPrintf("Input %d of instruction %d is not defined "
242 "in a basic block of the control-flow graph.",
243 input->GetId(),
244 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100245 }
246 }
247
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100248 // Ensure the uses of `instruction` are defined in a block of the graph,
249 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000250 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100251 !use_it.Done(); use_it.Advance()) {
252 HInstruction* use = use_it.Current()->GetUser();
253 const HInstructionList& list = use->IsPhi()
254 ? use->GetBlock()->GetPhis()
255 : use->GetBlock()->GetInstructions();
256 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000257 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000258 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000259 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000260 use->GetId(),
261 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100262 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100263 size_t use_index = use_it.Current()->GetIndex();
264 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000265 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100266 "UseListNode index.",
267 use->DebugName(),
268 use->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000269 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100270 instruction->GetId()));
271 }
272 }
273
274 // Ensure the environment uses entries are consistent.
275 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
276 !use_it.Done(); use_it.Advance()) {
277 HEnvironment* use = use_it.Current()->GetUser();
278 size_t use_index = use_it.Current()->GetIndex();
279 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
280 AddError(StringPrintf("Environment user of %s:%d has a wrong "
281 "UseListNode index.",
282 instruction->DebugName(),
283 instruction->GetId()));
284 }
Roland Levillain6b469232014-09-25 10:10:38 +0100285 }
David Brazdil1abb4192015-02-17 18:33:36 +0000286
287 // Ensure 'instruction' has pointers to its inputs' use entries.
288 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
289 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
290 HInstruction* input = input_record.GetInstruction();
291 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100292 size_t use_index = use_node->GetIndex();
293 if ((use_node == nullptr)
294 || !input->GetUses().Contains(use_node)
295 || (use_index >= e)
296 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000297 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
298 "at input %u (%s:%d).",
299 instruction->DebugName(),
300 instruction->GetId(),
301 static_cast<unsigned>(i),
302 input->DebugName(),
303 input->GetId()));
304 }
305 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100306}
307
Roland Levillain4c0eb422015-04-24 16:43:49 +0100308void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
309 VisitInstruction(invoke);
310
311 if (invoke->IsStaticWithExplicitClinitCheck()) {
312 size_t last_input_index = invoke->InputCount() - 1;
313 HInstruction* last_input = invoke->InputAt(last_input_index);
314 if (last_input == nullptr) {
315 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
316 "has a null pointer as last input.",
317 invoke->DebugName(),
318 invoke->GetId()));
319 }
320 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
321 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
322 "has a last instruction (%s:%d) which is neither a clinit check "
323 "nor a load class instruction.",
324 invoke->DebugName(),
325 invoke->GetId(),
326 last_input->DebugName(),
327 last_input->GetId()));
328 }
329 }
330}
331
David Brazdilfc6a86a2015-06-26 10:33:45 +0000332void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100333 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000334 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
335 AddError(StringPrintf("%s:%d does not jump to the exit block.",
336 ret->DebugName(),
337 ret->GetId()));
338 }
339}
340
341void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100342 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000343 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
344 AddError(StringPrintf("%s:%d does not jump to the exit block.",
345 ret->DebugName(),
346 ret->GetId()));
347 }
348}
349
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100350void GraphChecker::VisitCheckCast(HCheckCast* check) {
351 VisitInstruction(check);
352 HInstruction* input = check->InputAt(1);
353 if (!input->IsLoadClass()) {
354 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
355 check->DebugName(),
356 check->GetId(),
357 input->DebugName(),
358 input->GetId()));
359 }
360}
361
362void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
363 VisitInstruction(instruction);
364 HInstruction* input = instruction->InputAt(1);
365 if (!input->IsLoadClass()) {
366 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
367 instruction->DebugName(),
368 instruction->GetId(),
369 input->DebugName(),
370 input->GetId()));
371 }
372}
373
Roland Levillainccc07a92014-09-16 14:48:16 +0100374void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
375 super_type::VisitBasicBlock(block);
376
David Brazdilffee3d32015-07-06 11:48:53 +0100377 // Ensure that catch blocks are not normal successors, and normal blocks are
378 // never exceptional successors.
David Brazdild26a4112015-11-10 11:07:31 +0000379 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100380 if (successor->IsCatchBlock()) {
381 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
382 successor->GetBlockId(),
383 block->GetBlockId()));
384 }
385 }
David Brazdild26a4112015-11-10 11:07:31 +0000386 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100387 if (!successor->IsCatchBlock()) {
388 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
389 successor->GetBlockId(),
390 block->GetBlockId()));
391 }
392 }
393
Roland Levillainccc07a92014-09-16 14:48:16 +0100394 // Ensure there is no critical edge (i.e., an edge connecting a
395 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100396 // predecessors). Exceptional edges are synthesized and hence
397 // not accounted for.
David Brazdil81e479e2015-11-10 10:12:41 +0000398 if (block->GetSuccessors().size() > 1) {
David Brazdild26a4112015-11-10 11:07:31 +0000399 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdil81e479e2015-11-10 10:12:41 +0000400 if (successor->IsExitBlock() &&
401 block->IsSingleTryBoundary() &&
402 block->GetPredecessors().size() == 1u &&
403 block->GetSinglePredecessor()->GetLastInstruction()->IsThrow()) {
404 // Allowed critical edge Throw->TryBoundary->Exit.
405 } else if (successor->GetPredecessors().size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000406 AddError(StringPrintf("Critical edge between blocks %d and %d.",
407 block->GetBlockId(),
408 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100409 }
410 }
411 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412
David Brazdilffee3d32015-07-06 11:48:53 +0100413 // Ensure try membership information is consistent.
David Brazdilffee3d32015-07-06 11:48:53 +0100414 if (block->IsCatchBlock()) {
David Brazdilec16f792015-08-19 15:04:01 +0100415 if (block->IsTryBlock()) {
416 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +0100417 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
418 "has try entry %s:%d.",
419 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100420 try_entry.DebugName(),
421 try_entry.GetId()));
David Brazdilffee3d32015-07-06 11:48:53 +0100422 }
423
424 if (block->IsLoopHeader()) {
425 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
426 block->GetBlockId()));
427 }
428 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000429 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilec16f792015-08-19 15:04:01 +0100430 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
431 if (block->IsTryBlock()) {
432 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
433 if (incoming_try_entry == nullptr) {
434 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100435 "from predecessor %d.",
436 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100437 stored_try_entry.DebugName(),
438 stored_try_entry.GetId(),
439 predecessor->GetBlockId()));
440 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
441 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
442 "with %s:%d that follows from predecessor %d.",
443 block->GetBlockId(),
444 stored_try_entry.DebugName(),
445 stored_try_entry.GetId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100446 incoming_try_entry->DebugName(),
447 incoming_try_entry->GetId(),
448 predecessor->GetBlockId()));
449 }
David Brazdilec16f792015-08-19 15:04:01 +0100450 } else if (incoming_try_entry != nullptr) {
451 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100452 "from predecessor %d.",
453 block->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100454 incoming_try_entry->DebugName(),
455 incoming_try_entry->GetId(),
456 predecessor->GetBlockId()));
457 }
458 }
459 }
460
Roland Levillain6b879dd2014-09-22 17:13:44 +0100461 if (block->IsLoopHeader()) {
462 CheckLoop(block);
463 }
464}
465
466void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
467 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100468 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100469
David Brazdildb51efb2015-11-06 01:36:20 +0000470 // Ensure the pre-header block is first in the list of predecessors of a loop
471 // header and that the header block is its only successor.
Roland Levillain6b879dd2014-09-22 17:13:44 +0100472 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000473 AddError(StringPrintf(
474 "Loop pre-header is not the first predecessor of the loop header %d.",
475 id));
David Brazdildb51efb2015-11-06 01:36:20 +0000476 } else if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
477 AddError(StringPrintf(
478 "Loop pre-header %d of loop defined by header %d has %zu successors.",
479 loop_information->GetPreHeader()->GetBlockId(),
480 id,
481 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100482 }
483
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100484 // Ensure the loop header has only one incoming branch and the remaining
485 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000486 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000487 if (num_preds < 2) {
488 AddError(StringPrintf(
489 "Loop header %d has less than two predecessors: %zu.",
490 id,
491 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100492 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100493 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000494 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000495 AddError(StringPrintf(
496 "First predecessor of loop header %d is a back edge.",
497 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100498 }
Vladimir Marko60584552015-09-03 13:35:12 +0000499 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100500 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100501 if (!loop_information->IsBackEdge(*predecessor)) {
502 AddError(StringPrintf(
503 "Loop header %d has multiple incoming (non back edge) blocks.",
504 id));
505 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100506 }
507 }
508
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100509 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000513 AddError(StringPrintf(
514 "Loop defined by header %d has no back edge.",
515 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100516 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
518 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100519 if (!loop_blocks.IsBitSet(back_edge_id)) {
520 AddError(StringPrintf(
521 "Loop defined by header %d has an invalid back edge %d.",
522 id,
523 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000524 } else if (back_edge->GetLoopInformation() != loop_information) {
525 AddError(StringPrintf(
526 "Back edge %d of loop defined by header %d belongs to nested loop "
527 "with header %d.",
528 back_edge_id,
529 id,
530 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100531 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100532 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100533 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100534
David Brazdil2d7352b2015-04-20 14:52:42 +0100535 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100536 for (uint32_t i : loop_blocks.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100537 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
David Brazdil2d7352b2015-04-20 14:52:42 +0100538 if (loop_block == nullptr) {
539 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
540 id,
541 i));
542 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000543 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100544 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000545 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100546 }
547 }
David Brazdil7d275372015-04-21 16:36:35 +0100548
549 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
550 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
551 HLoopInformation* outer_info = it.Current();
552 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
553 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
554 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100555 id,
David Brazdil7d275372015-04-21 16:36:35 +0100556 outer_info->GetHeader()->GetBlockId()));
557 }
558 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100559}
560
561void SSAChecker::VisitInstruction(HInstruction* instruction) {
562 super_type::VisitInstruction(instruction);
563
Roland Levillaina8069ce2014-10-01 10:48:29 +0100564 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000565 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100566 !use_it.Done(); use_it.Advance()) {
567 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100568 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000569 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
570 "use %s:%d in block %d.",
571 instruction->DebugName(),
572 instruction->GetId(),
573 current_block_->GetBlockId(),
574 use->DebugName(),
575 use->GetId(),
576 use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100577 }
578 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100579
580 // Ensure an instruction having an environment is dominated by the
581 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100582 for (HEnvironment* environment = instruction->GetEnvironment();
583 environment != nullptr;
584 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100585 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
586 HInstruction* env_instruction = environment->GetInstructionAt(i);
587 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100588 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000589 AddError(StringPrintf("Instruction %d in environment of instruction %d "
590 "from block %d does not dominate instruction %d.",
591 env_instruction->GetId(),
592 instruction->GetId(),
593 current_block_->GetBlockId(),
594 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100595 }
596 }
597 }
David Brazdild9510df2015-11-04 23:30:22 +0000598
599 // Ensure that reference type instructions have reference type info.
600 if (instruction->GetType() == Primitive::kPrimNot) {
601 ScopedObjectAccess soa(Thread::Current());
602 if (!instruction->GetReferenceTypeInfo().IsValid()) {
603 AddError(StringPrintf("Reference type instruction %s:%d does not have "
604 "valid reference type information.",
605 instruction->DebugName(),
606 instruction->GetId()));
607 }
608 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100609}
610
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000611static Primitive::Type PrimitiveKind(Primitive::Type type) {
612 switch (type) {
613 case Primitive::kPrimBoolean:
614 case Primitive::kPrimByte:
615 case Primitive::kPrimShort:
616 case Primitive::kPrimChar:
617 case Primitive::kPrimInt:
618 return Primitive::kPrimInt;
619 default:
620 return type;
621 }
622}
623
David Brazdil77a48ae2015-09-15 12:34:04 +0000624static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
625 return insn1->IsConstant()
626 && insn2->IsConstant()
627 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
628}
629
630static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
631 if (insn1->IsPhi() &&
632 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
633 insn1->InputCount() == insn2->InputCount()) {
634 // Testing only one of the two inputs for recursion is sufficient.
635 if (visited->IsBitSet(insn1->GetId())) {
636 return true;
637 }
638 visited->SetBit(insn1->GetId());
639
640 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
641 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
642 return false;
643 }
644 }
645 return true;
646 } else if (IsSameSizeConstant(insn1, insn2)) {
647 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
648 } else {
649 return false;
650 }
651}
652
Roland Levillain6b879dd2014-09-22 17:13:44 +0100653void SSAChecker::VisitPhi(HPhi* phi) {
654 VisitInstruction(phi);
655
656 // Ensure the first input of a phi is not itself.
657 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000658 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
659 phi->GetId(),
660 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100661 }
662
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000663 // Ensure that the inputs have the same primitive kind as the phi.
664 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
665 HInstruction* input = phi->InputAt(i);
666 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
667 AddError(StringPrintf(
668 "Input %d at index %zu of phi %d from block %d does not have the "
669 "same type as the phi: %s versus %s",
670 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
671 Primitive::PrettyDescriptor(input->GetType()),
672 Primitive::PrettyDescriptor(phi->GetType())));
673 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000674 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000675 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
676 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
677 phi->GetId(),
678 phi->GetBlock()->GetBlockId(),
679 Primitive::PrettyDescriptor(phi->GetType())));
680 }
David Brazdilffee3d32015-07-06 11:48:53 +0100681
682 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100683 // The number of inputs of a catch phi should be the total number of throwing
684 // instructions caught by this catch block. We do not enforce this, however,
685 // because we do not remove the corresponding inputs when we prove that an
686 // instruction cannot throw. Instead, we at least test that all phis have the
687 // same, non-zero number of inputs (b/24054676).
688 size_t input_count_this = phi->InputCount();
689 if (input_count_this == 0u) {
690 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
691 phi->GetId(),
692 phi->GetBlock()->GetBlockId()));
693 } else {
694 HInstruction* next_phi = phi->GetNext();
695 if (next_phi != nullptr) {
696 size_t input_count_next = next_phi->InputCount();
697 if (input_count_this != input_count_next) {
698 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
699 "but phi %d has %zu inputs.",
700 phi->GetId(),
701 phi->GetBlock()->GetBlockId(),
702 input_count_this,
703 next_phi->GetId(),
704 input_count_next));
705 }
706 }
707 }
David Brazdilffee3d32015-07-06 11:48:53 +0100708 } else {
709 // Ensure the number of inputs of a non-catch phi is the same as the number
710 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000711 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
712 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100713 AddError(StringPrintf(
714 "Phi %d in block %d has %zu inputs, "
715 "but block %d has %zu predecessors.",
716 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000717 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100718 } else {
719 // Ensure phi input at index I either comes from the Ith
720 // predecessor or from a block that dominates this predecessor.
721 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
722 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000723 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100724 if (!(input->GetBlock() == predecessor
725 || input->GetBlock()->Dominates(predecessor))) {
726 AddError(StringPrintf(
727 "Input %d at index %zu of phi %d from block %d is not defined in "
728 "predecessor number %zu nor in a block dominating it.",
729 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
730 i));
731 }
732 }
733 }
734 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000735
736 // Ensure that catch phis are sorted by their vreg number, as required by
737 // the register allocator and code generator. This does not apply to normal
738 // phis which can be constructed artifically.
739 if (phi->IsCatchPhi()) {
740 HInstruction* next_phi = phi->GetNext();
741 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
742 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
743 "vreg numbers.",
744 phi->GetId(),
745 next_phi->GetId(),
746 phi->GetBlock()->GetBlockId()));
747 }
748 }
749
Aart Bik3fc7f352015-11-20 22:03:03 -0800750 // Test phi equivalents. There should not be two of the same type and they should only be
751 // created for constants which were untyped in DEX. Note that this test can be skipped for
752 // a synthetic phi (indicated by lack of a virtual register).
753 if (phi->GetRegNumber() != kNoRegNumber) {
Aart Bik4a342772015-11-30 10:17:46 -0800754 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis());
755 !phi_it.Done();
756 phi_it.Advance()) {
Aart Bik3fc7f352015-11-20 22:03:03 -0800757 HPhi* other_phi = phi_it.Current()->AsPhi();
758 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
759 if (phi->GetType() == other_phi->GetType()) {
760 std::stringstream type_str;
761 type_str << phi->GetType();
762 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
David Brazdil77a48ae2015-09-15 12:34:04 +0000763 phi->GetId(),
Aart Bik3fc7f352015-11-20 22:03:03 -0800764 phi->GetRegNumber(),
765 type_str.str().c_str()));
Nicolas Geoffrayf5f64ef2015-12-15 14:11:59 +0000766 } else if (phi->GetType() == Primitive::kPrimNot) {
767 std::stringstream type_str;
768 type_str << other_phi->GetType();
769 AddError(StringPrintf(
770 "Equivalent non-reference phi (%d) found for VReg %d with type: %s.",
771 phi->GetId(),
772 phi->GetRegNumber(),
773 type_str.str().c_str()));
Aart Bik3fc7f352015-11-20 22:03:03 -0800774 } else {
775 ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true);
776 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
777 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
778 "are not equivalents of constants.",
779 phi->GetId(),
780 other_phi->GetId(),
781 phi->GetRegNumber()));
782 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000783 }
784 }
785 }
786 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000787}
788
David Brazdil13b47182015-04-15 16:29:32 +0100789void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
790 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000791 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100792 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000793 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000794 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100795 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
796 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000797 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100798 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000799 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000800 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100801 } else if (input->GetType() == Primitive::kPrimInt
802 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
803 // TODO: We need a data-flow analysis to determine if the Phi or
804 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100805 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000806 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100807 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
808 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000809 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100810 static_cast<int>(input_index),
811 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000812 }
813}
814
Mark Mendellfe57faa2015-09-18 09:26:15 -0400815void SSAChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
816 VisitInstruction(instruction);
817 // Check that the number of block successors matches the switch count plus
818 // one for the default block.
819 HBasicBlock* block = instruction->GetBlock();
820 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
821 AddError(StringPrintf(
822 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
823 instruction->DebugName(),
824 instruction->GetId(),
825 block->GetBlockId(),
826 instruction->GetNumEntries() + 1u,
827 block->GetSuccessors().size()));
828 }
829}
830
David Brazdil13b47182015-04-15 16:29:32 +0100831void SSAChecker::VisitIf(HIf* instruction) {
832 VisitInstruction(instruction);
833 HandleBooleanInput(instruction, 0);
834}
835
836void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
837 VisitInstruction(instruction);
838 HandleBooleanInput(instruction, 0);
839}
840
Nicolas Geoffray31596742014-11-24 15:28:45 +0000841void SSAChecker::VisitCondition(HCondition* op) {
842 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000843 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000844 AddError(StringPrintf(
845 "Condition %s %d has a non-Boolean result type: %s.",
846 op->DebugName(), op->GetId(),
847 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000848 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000849 HInstruction* lhs = op->InputAt(0);
850 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100851 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
852 AddError(StringPrintf(
853 "Condition %s %d has inputs of different types: %s, and %s.",
854 op->DebugName(), op->GetId(),
855 Primitive::PrettyDescriptor(lhs->GetType()),
856 Primitive::PrettyDescriptor(rhs->GetType())));
857 }
858 if (!op->IsEqual() && !op->IsNotEqual()) {
859 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000860 AddError(StringPrintf(
861 "Condition %s %d uses an object as left-hand side input.",
862 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100863 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000864 AddError(StringPrintf(
865 "Condition %s %d uses an object as right-hand side input.",
866 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000867 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000868 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000869}
870
871void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
872 VisitInstruction(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +0000873 if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000874 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000875 AddError(StringPrintf(
876 "Shift operation %s %d has a non-int kind second input: "
877 "%s of type %s.",
878 op->DebugName(), op->GetId(),
879 op->InputAt(1)->DebugName(),
880 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000881 }
882 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100883 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000884 AddError(StringPrintf(
885 "Binary operation %s %d has inputs of different types: "
886 "%s, and %s.",
887 op->DebugName(), op->GetId(),
888 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
889 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000890 }
891 }
892
893 if (op->IsCompare()) {
894 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000895 AddError(StringPrintf(
896 "Compare operation %d has a non-int result type: %s.",
897 op->GetId(),
898 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000899 }
900 } else {
901 // Use the first input, so that we can also make this check for shift operations.
902 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000903 AddError(StringPrintf(
904 "Binary operation %s %d has a result type different "
905 "from its input type: %s vs %s.",
906 op->DebugName(), op->GetId(),
907 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100908 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000909 }
910 }
911}
912
David Brazdil8d5b8b22015-03-24 10:51:52 +0000913void SSAChecker::VisitConstant(HConstant* instruction) {
914 HBasicBlock* block = instruction->GetBlock();
915 if (!block->IsEntryBlock()) {
916 AddError(StringPrintf(
917 "%s %d should be in the entry block but is in block %d.",
918 instruction->DebugName(),
919 instruction->GetId(),
920 block->GetBlockId()));
921 }
922}
923
David Brazdilf5552582015-12-27 13:36:12 +0000924void SSAChecker::VisitBoundType(HBoundType* instruction) {
925 VisitInstruction(instruction);
926
927 ScopedObjectAccess soa(Thread::Current());
928 if (!instruction->GetUpperBound().IsValid()) {
929 AddError(StringPrintf(
930 "%s %d does not have a valid upper bound RTI.",
931 instruction->DebugName(),
932 instruction->GetId()));
933 }
934}
935
Roland Levillainccc07a92014-09-16 14:48:16 +0100936} // namespace art