blob: f77576db6c4ca9c492207540fd005f328babccfa [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"
Roland Levillain7e53b412014-09-23 10:50:22 +010027
Roland Levillainccc07a92014-09-16 14:48:16 +010028namespace art {
29
30void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
31 current_block_ = block;
32
33 // Check consistency with respect to predecessors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010034 ArenaSafeMap<HBasicBlock*, size_t> predecessors_count(
35 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000036 for (HBasicBlock* p : block->GetPredecessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010037 auto it = predecessors_count.find(p);
38 if (it != predecessors_count.end()) {
39 ++it->second;
40 } else {
41 predecessors_count.Put(p, 1u);
42 }
Roland Levillainccc07a92014-09-16 14:48:16 +010043 }
44 for (auto& pc : predecessors_count) {
45 HBasicBlock* p = pc.first;
46 size_t p_count_in_block_predecessors = pc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010047 size_t block_count_in_p_successors =
48 std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010049 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000050 AddError(StringPrintf(
51 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
52 "block %d lists %zu occurrences of block %d in its successors.",
53 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
54 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010055 }
56 }
57
58 // Check consistency with respect to successors of `block`.
Vladimir Marko655e5852015-10-12 10:38:28 +010059 ArenaSafeMap<HBasicBlock*, size_t> successors_count(
60 std::less<HBasicBlock*>(), GetGraph()->GetArena()->Adapter(kArenaAllocGraphChecker));
Vladimir Marko60584552015-09-03 13:35:12 +000061 for (HBasicBlock* s : block->GetSuccessors()) {
Vladimir Marko655e5852015-10-12 10:38:28 +010062 auto it = successors_count.find(s);
63 if (it != successors_count.end()) {
64 ++it->second;
65 } else {
66 successors_count.Put(s, 1u);
67 }
Roland Levillainccc07a92014-09-16 14:48:16 +010068 }
69 for (auto& sc : successors_count) {
70 HBasicBlock* s = sc.first;
71 size_t s_count_in_block_successors = sc.second;
Vladimir Marko655e5852015-10-12 10:38:28 +010072 size_t block_count_in_s_predecessors =
73 std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block);
Roland Levillainccc07a92014-09-16 14:48:16 +010074 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 Brazdil29fc0082015-08-18 17:17:38 +010092 // Ensure that only Return(Void) and Throw jump to Exit. An exiting
David Brazdilb618ade2015-07-29 10:31:29 +010093 // TryBoundary may be between a Throw and the Exit if the Throw is in a try.
94 if (block->IsExitBlock()) {
Vladimir Marko60584552015-09-03 13:35:12 +000095 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilb618ade2015-07-29 10:31:29 +010096 if (predecessor->IsSingleTryBoundary()
97 && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) {
98 HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor();
99 HInstruction* last_instruction = real_predecessor->GetLastInstruction();
100 if (!last_instruction->IsThrow()) {
101 AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.",
102 last_instruction->DebugName(),
103 last_instruction->GetId()));
104 }
105 } else {
106 HInstruction* last_instruction = predecessor->GetLastInstruction();
107 if (!last_instruction->IsReturn()
108 && !last_instruction->IsReturnVoid()
109 && !last_instruction->IsThrow()) {
110 AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.",
111 last_instruction->DebugName(),
112 last_instruction->GetId()));
113 }
114 }
115 }
116 }
117
Roland Levillainccc07a92014-09-16 14:48:16 +0100118 // Visit this block's list of phis.
119 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +0100120 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100122 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000123 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
124 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100125 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100126 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
127 AddError(StringPrintf("The recorded last phi of block %d does not match "
128 "the actual last phi %d.",
129 current_block_->GetBlockId(),
130 current->GetId()));
131 }
132 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100133 }
134
135 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100136 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
137 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100138 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100139 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000140 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
141 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100142 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
144 AddError(StringPrintf("The recorded last instruction of block %d does not match "
145 "the actual last instruction %d.",
146 current_block_->GetBlockId(),
147 current->GetId()));
148 }
149 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 }
151}
152
Mark Mendell1152c922015-04-24 17:06:35 -0400153void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
154 if (!GetGraph()->HasBoundsChecks()) {
155 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
156 "but HasBoundsChecks() returns false",
157 check->DebugName(),
158 check->GetId()));
159 }
160
161 // Perform the instruction base checks too.
162 VisitInstruction(check);
163}
164
David Brazdilffee3d32015-07-06 11:48:53 +0100165void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
166 // Ensure that all exception handlers are catch blocks and that handlers
167 // are not listed multiple times.
168 // Note that a normal-flow successor may be a catch block before CFG
169 // simplification. We only test normal-flow successors in SsaChecker.
170 for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) {
171 HBasicBlock* handler = it.Current();
172 if (!handler->IsCatchBlock()) {
173 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
174 "is not a catch block.",
175 current_block_->GetBlockId(),
176 try_boundary->DebugName(),
177 try_boundary->GetId(),
178 handler->GetBlockId()));
179 }
Vladimir Marko60584552015-09-03 13:35:12 +0000180 if (current_block_->HasSuccessor(handler, it.CurrentSuccessorIndex() + 1)) {
David Brazdilffee3d32015-07-06 11:48:53 +0100181 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
182 handler->GetBlockId(),
183 try_boundary->DebugName(),
184 try_boundary->GetId()));
185 }
186 }
187
188 VisitInstruction(try_boundary);
189}
190
David Brazdil9bc43612015-11-05 21:25:24 +0000191void GraphChecker::VisitLoadException(HLoadException* load) {
192 // Ensure that LoadException is the first instruction in a catch block.
193 if (!load->GetBlock()->IsCatchBlock()) {
194 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
195 load->DebugName(),
196 load->GetId(),
197 load->GetBlock()->GetBlockId()));
198 } else if (load->GetBlock()->GetFirstInstruction() != load) {
199 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
200 load->DebugName(),
201 load->GetId(),
202 load->GetBlock()->GetBlockId()));
203 }
204}
205
Roland Levillainccc07a92014-09-16 14:48:16 +0100206void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000207 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000208 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
209 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000210 } else {
211 seen_ids_.SetBit(instruction->GetId());
212 }
213
Roland Levillainccc07a92014-09-16 14:48:16 +0100214 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000215 if (instruction->GetBlock() == nullptr) {
216 AddError(StringPrintf("%s %d in block %d not associated with any block.",
217 instruction->IsPhi() ? "Phi" : "Instruction",
218 instruction->GetId(),
219 current_block_->GetBlockId()));
220 } else if (instruction->GetBlock() != current_block_) {
221 AddError(StringPrintf("%s %d in block %d associated with block %d.",
222 instruction->IsPhi() ? "Phi" : "Instruction",
223 instruction->GetId(),
224 current_block_->GetBlockId(),
225 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100226 }
Roland Levillain6b469232014-09-25 10:10:38 +0100227
228 // Ensure the inputs of `instruction` are defined in a block of the graph.
229 for (HInputIterator input_it(instruction); !input_it.Done();
230 input_it.Advance()) {
231 HInstruction* input = input_it.Current();
232 const HInstructionList& list = input->IsPhi()
233 ? input->GetBlock()->GetPhis()
234 : input->GetBlock()->GetInstructions();
235 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000236 AddError(StringPrintf("Input %d of instruction %d is not defined "
237 "in a basic block of the control-flow graph.",
238 input->GetId(),
239 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100240 }
241 }
242
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100243 // Ensure the uses of `instruction` are defined in a block of the graph,
244 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000245 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100246 !use_it.Done(); use_it.Advance()) {
247 HInstruction* use = use_it.Current()->GetUser();
248 const HInstructionList& list = use->IsPhi()
249 ? use->GetBlock()->GetPhis()
250 : use->GetBlock()->GetInstructions();
251 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000252 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000253 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000254 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000255 use->GetId(),
256 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100257 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100258 size_t use_index = use_it.Current()->GetIndex();
259 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
260 AddError(StringPrintf("User %s:%d of instruction %d has a wrong "
261 "UseListNode index.",
262 use->DebugName(),
263 use->GetId(),
264 instruction->GetId()));
265 }
266 }
267
268 // Ensure the environment uses entries are consistent.
269 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
270 !use_it.Done(); use_it.Advance()) {
271 HEnvironment* use = use_it.Current()->GetUser();
272 size_t use_index = use_it.Current()->GetIndex();
273 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
274 AddError(StringPrintf("Environment user of %s:%d has a wrong "
275 "UseListNode index.",
276 instruction->DebugName(),
277 instruction->GetId()));
278 }
Roland Levillain6b469232014-09-25 10:10:38 +0100279 }
David Brazdil1abb4192015-02-17 18:33:36 +0000280
281 // Ensure 'instruction' has pointers to its inputs' use entries.
282 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
283 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
284 HInstruction* input = input_record.GetInstruction();
285 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100286 size_t use_index = use_node->GetIndex();
287 if ((use_node == nullptr)
288 || !input->GetUses().Contains(use_node)
289 || (use_index >= e)
290 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000291 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
292 "at input %u (%s:%d).",
293 instruction->DebugName(),
294 instruction->GetId(),
295 static_cast<unsigned>(i),
296 input->DebugName(),
297 input->GetId()));
298 }
299 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100300}
301
Roland Levillain4c0eb422015-04-24 16:43:49 +0100302void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
303 VisitInstruction(invoke);
304
305 if (invoke->IsStaticWithExplicitClinitCheck()) {
306 size_t last_input_index = invoke->InputCount() - 1;
307 HInstruction* last_input = invoke->InputAt(last_input_index);
308 if (last_input == nullptr) {
309 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
310 "has a null pointer as last input.",
311 invoke->DebugName(),
312 invoke->GetId()));
313 }
314 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
315 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
316 "has a last instruction (%s:%d) which is neither a clinit check "
317 "nor a load class instruction.",
318 invoke->DebugName(),
319 invoke->GetId(),
320 last_input->DebugName(),
321 last_input->GetId()));
322 }
323 }
324}
325
David Brazdilfc6a86a2015-06-26 10:33:45 +0000326void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100327 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000328 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
329 AddError(StringPrintf("%s:%d does not jump to the exit block.",
330 ret->DebugName(),
331 ret->GetId()));
332 }
333}
334
335void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100336 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000337 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
338 AddError(StringPrintf("%s:%d does not jump to the exit block.",
339 ret->DebugName(),
340 ret->GetId()));
341 }
342}
343
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100344void GraphChecker::VisitCheckCast(HCheckCast* check) {
345 VisitInstruction(check);
346 HInstruction* input = check->InputAt(1);
347 if (!input->IsLoadClass()) {
348 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
349 check->DebugName(),
350 check->GetId(),
351 input->DebugName(),
352 input->GetId()));
353 }
354}
355
356void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
357 VisitInstruction(instruction);
358 HInstruction* input = instruction->InputAt(1);
359 if (!input->IsLoadClass()) {
360 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
361 instruction->DebugName(),
362 instruction->GetId(),
363 input->DebugName(),
364 input->GetId()));
365 }
366}
367
Roland Levillainccc07a92014-09-16 14:48:16 +0100368void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
369 super_type::VisitBasicBlock(block);
370
David Brazdilffee3d32015-07-06 11:48:53 +0100371 // Ensure that catch blocks are not normal successors, and normal blocks are
372 // never exceptional successors.
373 const size_t num_normal_successors = block->NumberOfNormalSuccessors();
374 for (size_t j = 0; j < num_normal_successors; ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100375 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100376 if (successor->IsCatchBlock()) {
377 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
378 successor->GetBlockId(),
379 block->GetBlockId()));
380 }
381 }
Vladimir Marko60584552015-09-03 13:35:12 +0000382 for (size_t j = num_normal_successors, e = block->GetSuccessors().size(); j < e; ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100383 HBasicBlock* successor = block->GetSuccessors()[j];
David Brazdilffee3d32015-07-06 11:48:53 +0100384 if (!successor->IsCatchBlock()) {
385 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
386 successor->GetBlockId(),
387 block->GetBlockId()));
388 }
389 }
390
Roland Levillainccc07a92014-09-16 14:48:16 +0100391 // Ensure there is no critical edge (i.e., an edge connecting a
392 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100393 // predecessors). Exceptional edges are synthesized and hence
394 // not accounted for.
395 if (block->NumberOfNormalSuccessors() > 1) {
396 for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100397 HBasicBlock* successor = block->GetSuccessors()[j];
Vladimir Marko60584552015-09-03 13:35:12 +0000398 if (successor->GetPredecessors().size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000399 AddError(StringPrintf("Critical edge between blocks %d and %d.",
400 block->GetBlockId(),
401 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100402 }
403 }
404 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100405
David Brazdilffee3d32015-07-06 11:48:53 +0100406 // Ensure try membership information is consistent.
David Brazdilffee3d32015-07-06 11:48:53 +0100407 if (block->IsCatchBlock()) {
David Brazdilec16f792015-08-19 15:04:01 +0100408 if (block->IsTryBlock()) {
409 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +0100410 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
411 "has try entry %s:%d.",
412 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100413 try_entry.DebugName(),
414 try_entry.GetId()));
David Brazdilffee3d32015-07-06 11:48:53 +0100415 }
416
417 if (block->IsLoopHeader()) {
418 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
419 block->GetBlockId()));
420 }
421 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000422 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilec16f792015-08-19 15:04:01 +0100423 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
424 if (block->IsTryBlock()) {
425 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
426 if (incoming_try_entry == nullptr) {
427 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100428 "from predecessor %d.",
429 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100430 stored_try_entry.DebugName(),
431 stored_try_entry.GetId(),
432 predecessor->GetBlockId()));
433 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
434 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
435 "with %s:%d that follows from predecessor %d.",
436 block->GetBlockId(),
437 stored_try_entry.DebugName(),
438 stored_try_entry.GetId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100439 incoming_try_entry->DebugName(),
440 incoming_try_entry->GetId(),
441 predecessor->GetBlockId()));
442 }
David Brazdilec16f792015-08-19 15:04:01 +0100443 } else if (incoming_try_entry != nullptr) {
444 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100445 "from predecessor %d.",
446 block->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100447 incoming_try_entry->DebugName(),
448 incoming_try_entry->GetId(),
449 predecessor->GetBlockId()));
450 }
451 }
452 }
453
Roland Levillain6b879dd2014-09-22 17:13:44 +0100454 if (block->IsLoopHeader()) {
455 CheckLoop(block);
456 }
457}
458
459void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
460 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100461 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100462
David Brazdildb51efb2015-11-06 01:36:20 +0000463 // Ensure the pre-header block is first in the list of predecessors of a loop
464 // header and that the header block is its only successor.
Roland Levillain6b879dd2014-09-22 17:13:44 +0100465 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000466 AddError(StringPrintf(
467 "Loop pre-header is not the first predecessor of the loop header %d.",
468 id));
David Brazdildb51efb2015-11-06 01:36:20 +0000469 } else if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
470 AddError(StringPrintf(
471 "Loop pre-header %d of loop defined by header %d has %zu successors.",
472 loop_information->GetPreHeader()->GetBlockId(),
473 id,
474 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100475 }
476
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100477 // Ensure the loop header has only one incoming branch and the remaining
478 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000479 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000480 if (num_preds < 2) {
481 AddError(StringPrintf(
482 "Loop header %d has less than two predecessors: %zu.",
483 id,
484 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100485 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100486 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000487 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000488 AddError(StringPrintf(
489 "First predecessor of loop header %d is a back edge.",
490 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100491 }
Vladimir Marko60584552015-09-03 13:35:12 +0000492 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100493 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100494 if (!loop_information->IsBackEdge(*predecessor)) {
495 AddError(StringPrintf(
496 "Loop header %d has multiple incoming (non back edge) blocks.",
497 id));
498 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100499 }
500 }
501
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100502 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100503
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100504 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100505 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000506 AddError(StringPrintf(
507 "Loop defined by header %d has no back edge.",
508 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100509 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
511 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100512 if (!loop_blocks.IsBitSet(back_edge_id)) {
513 AddError(StringPrintf(
514 "Loop defined by header %d has an invalid back edge %d.",
515 id,
516 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000517 } else if (back_edge->GetLoopInformation() != loop_information) {
518 AddError(StringPrintf(
519 "Back edge %d of loop defined by header %d belongs to nested loop "
520 "with header %d.",
521 back_edge_id,
522 id,
523 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100524 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100525 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100526 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100527
David Brazdil2d7352b2015-04-20 14:52:42 +0100528 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100529 for (uint32_t i : loop_blocks.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100530 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
David Brazdil2d7352b2015-04-20 14:52:42 +0100531 if (loop_block == nullptr) {
532 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
533 id,
534 i));
535 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000536 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100537 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000538 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100539 }
540 }
David Brazdil7d275372015-04-21 16:36:35 +0100541
542 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
543 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
544 HLoopInformation* outer_info = it.Current();
545 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
546 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
547 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100548 id,
David Brazdil7d275372015-04-21 16:36:35 +0100549 outer_info->GetHeader()->GetBlockId()));
550 }
551 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100552}
553
554void SSAChecker::VisitInstruction(HInstruction* instruction) {
555 super_type::VisitInstruction(instruction);
556
Roland Levillaina8069ce2014-10-01 10:48:29 +0100557 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000558 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100559 !use_it.Done(); use_it.Advance()) {
560 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100561 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000562 AddError(StringPrintf("Instruction %d in block %d does not dominate "
563 "use %d in block %d.",
564 instruction->GetId(), current_block_->GetBlockId(),
565 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100566 }
567 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100568
569 // Ensure an instruction having an environment is dominated by the
570 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100571 for (HEnvironment* environment = instruction->GetEnvironment();
572 environment != nullptr;
573 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100574 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
575 HInstruction* env_instruction = environment->GetInstructionAt(i);
576 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100577 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000578 AddError(StringPrintf("Instruction %d in environment of instruction %d "
579 "from block %d does not dominate instruction %d.",
580 env_instruction->GetId(),
581 instruction->GetId(),
582 current_block_->GetBlockId(),
583 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100584 }
585 }
586 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100587}
588
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000589static Primitive::Type PrimitiveKind(Primitive::Type type) {
590 switch (type) {
591 case Primitive::kPrimBoolean:
592 case Primitive::kPrimByte:
593 case Primitive::kPrimShort:
594 case Primitive::kPrimChar:
595 case Primitive::kPrimInt:
596 return Primitive::kPrimInt;
597 default:
598 return type;
599 }
600}
601
David Brazdil77a48ae2015-09-15 12:34:04 +0000602static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
603 return insn1->IsConstant()
604 && insn2->IsConstant()
605 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
606}
607
608static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
609 if (insn1->IsPhi() &&
610 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
611 insn1->InputCount() == insn2->InputCount()) {
612 // Testing only one of the two inputs for recursion is sufficient.
613 if (visited->IsBitSet(insn1->GetId())) {
614 return true;
615 }
616 visited->SetBit(insn1->GetId());
617
618 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
619 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
620 return false;
621 }
622 }
623 return true;
624 } else if (IsSameSizeConstant(insn1, insn2)) {
625 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
626 } else {
627 return false;
628 }
629}
630
Roland Levillain6b879dd2014-09-22 17:13:44 +0100631void SSAChecker::VisitPhi(HPhi* phi) {
632 VisitInstruction(phi);
633
634 // Ensure the first input of a phi is not itself.
635 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000636 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
637 phi->GetId(),
638 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100639 }
640
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000641 // Ensure that the inputs have the same primitive kind as the phi.
642 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
643 HInstruction* input = phi->InputAt(i);
644 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
645 AddError(StringPrintf(
646 "Input %d at index %zu of phi %d from block %d does not have the "
647 "same type as the phi: %s versus %s",
648 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
649 Primitive::PrettyDescriptor(input->GetType()),
650 Primitive::PrettyDescriptor(phi->GetType())));
651 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000652 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000653 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
654 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
655 phi->GetId(),
656 phi->GetBlock()->GetBlockId(),
657 Primitive::PrettyDescriptor(phi->GetType())));
658 }
David Brazdilffee3d32015-07-06 11:48:53 +0100659
660 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100661 // The number of inputs of a catch phi should be the total number of throwing
662 // instructions caught by this catch block. We do not enforce this, however,
663 // because we do not remove the corresponding inputs when we prove that an
664 // instruction cannot throw. Instead, we at least test that all phis have the
665 // same, non-zero number of inputs (b/24054676).
666 size_t input_count_this = phi->InputCount();
667 if (input_count_this == 0u) {
668 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
669 phi->GetId(),
670 phi->GetBlock()->GetBlockId()));
671 } else {
672 HInstruction* next_phi = phi->GetNext();
673 if (next_phi != nullptr) {
674 size_t input_count_next = next_phi->InputCount();
675 if (input_count_this != input_count_next) {
676 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
677 "but phi %d has %zu inputs.",
678 phi->GetId(),
679 phi->GetBlock()->GetBlockId(),
680 input_count_this,
681 next_phi->GetId(),
682 input_count_next));
683 }
684 }
685 }
David Brazdilffee3d32015-07-06 11:48:53 +0100686 } else {
687 // Ensure the number of inputs of a non-catch phi is the same as the number
688 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000689 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
690 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100691 AddError(StringPrintf(
692 "Phi %d in block %d has %zu inputs, "
693 "but block %d has %zu predecessors.",
694 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000695 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100696 } else {
697 // Ensure phi input at index I either comes from the Ith
698 // predecessor or from a block that dominates this predecessor.
699 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
700 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000701 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100702 if (!(input->GetBlock() == predecessor
703 || input->GetBlock()->Dominates(predecessor))) {
704 AddError(StringPrintf(
705 "Input %d at index %zu of phi %d from block %d is not defined in "
706 "predecessor number %zu nor in a block dominating it.",
707 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
708 i));
709 }
710 }
711 }
712 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000713
714 // Ensure that catch phis are sorted by their vreg number, as required by
715 // the register allocator and code generator. This does not apply to normal
716 // phis which can be constructed artifically.
717 if (phi->IsCatchPhi()) {
718 HInstruction* next_phi = phi->GetNext();
719 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
720 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
721 "vreg numbers.",
722 phi->GetId(),
723 next_phi->GetId(),
724 phi->GetBlock()->GetBlockId()));
725 }
726 }
727
728 // Test phi equivalents. There should not be two of the same type and they
729 // should only be created for constants which were untyped in DEX.
730 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
731 HPhi* other_phi = phi_it.Current()->AsPhi();
732 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
733 if (phi->GetType() == other_phi->GetType()) {
734 std::stringstream type_str;
735 type_str << phi->GetType();
736 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
737 phi->GetId(),
738 phi->GetRegNumber(),
739 type_str.str().c_str()));
740 } else {
741 ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true);
742 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
743 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
744 "are not equivalents of constants.",
745 phi->GetId(),
746 other_phi->GetId(),
747 phi->GetRegNumber()));
748 }
749 }
750 }
751 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000752}
753
David Brazdil13b47182015-04-15 16:29:32 +0100754void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
755 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000756 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100757 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000758 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000759 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100760 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
761 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000762 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100763 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000764 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000765 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100766 } else if (input->GetType() == Primitive::kPrimInt
767 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
768 // TODO: We need a data-flow analysis to determine if the Phi or
769 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100770 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000771 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100772 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
773 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000774 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100775 static_cast<int>(input_index),
776 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000777 }
778}
779
Mark Mendellfe57faa2015-09-18 09:26:15 -0400780void SSAChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
781 VisitInstruction(instruction);
782 // Check that the number of block successors matches the switch count plus
783 // one for the default block.
784 HBasicBlock* block = instruction->GetBlock();
785 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
786 AddError(StringPrintf(
787 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
788 instruction->DebugName(),
789 instruction->GetId(),
790 block->GetBlockId(),
791 instruction->GetNumEntries() + 1u,
792 block->GetSuccessors().size()));
793 }
794}
795
David Brazdil13b47182015-04-15 16:29:32 +0100796void SSAChecker::VisitIf(HIf* instruction) {
797 VisitInstruction(instruction);
798 HandleBooleanInput(instruction, 0);
799}
800
801void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
802 VisitInstruction(instruction);
803 HandleBooleanInput(instruction, 0);
804}
805
Nicolas Geoffray31596742014-11-24 15:28:45 +0000806void SSAChecker::VisitCondition(HCondition* op) {
807 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000808 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000809 AddError(StringPrintf(
810 "Condition %s %d has a non-Boolean result type: %s.",
811 op->DebugName(), op->GetId(),
812 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000813 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000814 HInstruction* lhs = op->InputAt(0);
815 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100816 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
817 AddError(StringPrintf(
818 "Condition %s %d has inputs of different types: %s, and %s.",
819 op->DebugName(), op->GetId(),
820 Primitive::PrettyDescriptor(lhs->GetType()),
821 Primitive::PrettyDescriptor(rhs->GetType())));
822 }
823 if (!op->IsEqual() && !op->IsNotEqual()) {
824 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000825 AddError(StringPrintf(
826 "Condition %s %d uses an object as left-hand side input.",
827 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100828 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000829 AddError(StringPrintf(
830 "Condition %s %d uses an object as right-hand side input.",
831 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000832 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000833 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000834}
835
836void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
837 VisitInstruction(op);
838 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
839 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000840 AddError(StringPrintf(
841 "Shift operation %s %d has a non-int kind second input: "
842 "%s of type %s.",
843 op->DebugName(), op->GetId(),
844 op->InputAt(1)->DebugName(),
845 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000846 }
847 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100848 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000849 AddError(StringPrintf(
850 "Binary operation %s %d has inputs of different types: "
851 "%s, and %s.",
852 op->DebugName(), op->GetId(),
853 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
854 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000855 }
856 }
857
858 if (op->IsCompare()) {
859 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000860 AddError(StringPrintf(
861 "Compare operation %d has a non-int result type: %s.",
862 op->GetId(),
863 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000864 }
865 } else {
866 // Use the first input, so that we can also make this check for shift operations.
867 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000868 AddError(StringPrintf(
869 "Binary operation %s %d has a result type different "
870 "from its input type: %s vs %s.",
871 op->DebugName(), op->GetId(),
872 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100873 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000874 }
875 }
876}
877
David Brazdil8d5b8b22015-03-24 10:51:52 +0000878void SSAChecker::VisitConstant(HConstant* instruction) {
879 HBasicBlock* block = instruction->GetBlock();
880 if (!block->IsEntryBlock()) {
881 AddError(StringPrintf(
882 "%s %d should be in the entry block but is in block %d.",
883 instruction->DebugName(),
884 instruction->GetId(),
885 block->GetBlockId()));
886 }
887}
888
Roland Levillainccc07a92014-09-16 14:48:16 +0100889} // namespace art