blob: 091f1c7e89a6260b392c18dc7c56d802fa5915c0 [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 Brazdild26a4112015-11-10 11:07:31 +0000165// Returns true if there exists `index2` such that `index2 > index` and
166// `array[index] == array[index2]`.
167static bool ContainsSameValueAfter(ArrayRef<HBasicBlock* const> array, size_t index) {
168 ArrayRef<HBasicBlock* const> tail = array.SubArray(index + 1);
169 return std::find(tail.begin(), tail.end(), array[index]) != tail.end();
170}
171
David Brazdilffee3d32015-07-06 11:48:53 +0100172void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) {
David Brazdild26a4112015-11-10 11:07:31 +0000173 ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers();
174
175 // Ensure that all exception handlers are catch blocks.
David Brazdilffee3d32015-07-06 11:48:53 +0100176 // Note that a normal-flow successor may be a catch block before CFG
177 // simplification. We only test normal-flow successors in SsaChecker.
David Brazdild26a4112015-11-10 11:07:31 +0000178 for (HBasicBlock* handler : handlers) {
David Brazdilffee3d32015-07-06 11:48:53 +0100179 if (!handler->IsCatchBlock()) {
180 AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which "
181 "is not a catch block.",
182 current_block_->GetBlockId(),
183 try_boundary->DebugName(),
184 try_boundary->GetId(),
185 handler->GetBlockId()));
186 }
David Brazdild26a4112015-11-10 11:07:31 +0000187 }
188
189 // Ensure that handlers are not listed multiple times.
190 for (size_t i = 0, e = handlers.size(); i < e; ++i) {
191 if (ContainsSameValueAfter(handlers, i)) {
David Brazdilffee3d32015-07-06 11:48:53 +0100192 AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.",
David Brazdild26a4112015-11-10 11:07:31 +0000193 handlers[i]->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100194 try_boundary->DebugName(),
195 try_boundary->GetId()));
196 }
197 }
198
199 VisitInstruction(try_boundary);
200}
201
David Brazdil9bc43612015-11-05 21:25:24 +0000202void GraphChecker::VisitLoadException(HLoadException* load) {
203 // Ensure that LoadException is the first instruction in a catch block.
204 if (!load->GetBlock()->IsCatchBlock()) {
205 AddError(StringPrintf("%s:%d is in a non-catch block %d.",
206 load->DebugName(),
207 load->GetId(),
208 load->GetBlock()->GetBlockId()));
209 } else if (load->GetBlock()->GetFirstInstruction() != load) {
210 AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.",
211 load->DebugName(),
212 load->GetId(),
213 load->GetBlock()->GetBlockId()));
214 }
215}
216
Roland Levillainccc07a92014-09-16 14:48:16 +0100217void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000218 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000219 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
220 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000221 } else {
222 seen_ids_.SetBit(instruction->GetId());
223 }
224
Roland Levillainccc07a92014-09-16 14:48:16 +0100225 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000226 if (instruction->GetBlock() == nullptr) {
227 AddError(StringPrintf("%s %d in block %d not associated with any block.",
228 instruction->IsPhi() ? "Phi" : "Instruction",
229 instruction->GetId(),
230 current_block_->GetBlockId()));
231 } else if (instruction->GetBlock() != current_block_) {
232 AddError(StringPrintf("%s %d in block %d associated with block %d.",
233 instruction->IsPhi() ? "Phi" : "Instruction",
234 instruction->GetId(),
235 current_block_->GetBlockId(),
236 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100237 }
Roland Levillain6b469232014-09-25 10:10:38 +0100238
239 // Ensure the inputs of `instruction` are defined in a block of the graph.
240 for (HInputIterator input_it(instruction); !input_it.Done();
241 input_it.Advance()) {
242 HInstruction* input = input_it.Current();
243 const HInstructionList& list = input->IsPhi()
244 ? input->GetBlock()->GetPhis()
245 : input->GetBlock()->GetInstructions();
246 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000247 AddError(StringPrintf("Input %d of instruction %d is not defined "
248 "in a basic block of the control-flow graph.",
249 input->GetId(),
250 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100251 }
252 }
253
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100254 // Ensure the uses of `instruction` are defined in a block of the graph,
255 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000256 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100257 !use_it.Done(); use_it.Advance()) {
258 HInstruction* use = use_it.Current()->GetUser();
259 const HInstructionList& list = use->IsPhi()
260 ? use->GetBlock()->GetPhis()
261 : use->GetBlock()->GetInstructions();
262 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000263 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000264 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000265 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000266 use->GetId(),
267 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100268 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100269 size_t use_index = use_it.Current()->GetIndex();
270 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000271 AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong "
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100272 "UseListNode index.",
273 use->DebugName(),
274 use->GetId(),
Vladimir Markob554b5a2015-11-06 12:57:55 +0000275 instruction->DebugName(),
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100276 instruction->GetId()));
277 }
278 }
279
280 // Ensure the environment uses entries are consistent.
281 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
282 !use_it.Done(); use_it.Advance()) {
283 HEnvironment* use = use_it.Current()->GetUser();
284 size_t use_index = use_it.Current()->GetIndex();
285 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
286 AddError(StringPrintf("Environment user of %s:%d has a wrong "
287 "UseListNode index.",
288 instruction->DebugName(),
289 instruction->GetId()));
290 }
Roland Levillain6b469232014-09-25 10:10:38 +0100291 }
David Brazdil1abb4192015-02-17 18:33:36 +0000292
293 // Ensure 'instruction' has pointers to its inputs' use entries.
294 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
295 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
296 HInstruction* input = input_record.GetInstruction();
297 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100298 size_t use_index = use_node->GetIndex();
299 if ((use_node == nullptr)
300 || !input->GetUses().Contains(use_node)
301 || (use_index >= e)
302 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000303 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
304 "at input %u (%s:%d).",
305 instruction->DebugName(),
306 instruction->GetId(),
307 static_cast<unsigned>(i),
308 input->DebugName(),
309 input->GetId()));
310 }
311 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100312}
313
Roland Levillain4c0eb422015-04-24 16:43:49 +0100314void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
315 VisitInstruction(invoke);
316
317 if (invoke->IsStaticWithExplicitClinitCheck()) {
318 size_t last_input_index = invoke->InputCount() - 1;
319 HInstruction* last_input = invoke->InputAt(last_input_index);
320 if (last_input == nullptr) {
321 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
322 "has a null pointer as last input.",
323 invoke->DebugName(),
324 invoke->GetId()));
325 }
326 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
327 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
328 "has a last instruction (%s:%d) which is neither a clinit check "
329 "nor a load class instruction.",
330 invoke->DebugName(),
331 invoke->GetId(),
332 last_input->DebugName(),
333 last_input->GetId()));
334 }
335 }
336}
337
David Brazdilfc6a86a2015-06-26 10:33:45 +0000338void GraphChecker::VisitReturn(HReturn* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100339 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000340 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
341 AddError(StringPrintf("%s:%d does not jump to the exit block.",
342 ret->DebugName(),
343 ret->GetId()));
344 }
345}
346
347void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100348 VisitInstruction(ret);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000349 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
350 AddError(StringPrintf("%s:%d does not jump to the exit block.",
351 ret->DebugName(),
352 ret->GetId()));
353 }
354}
355
Nicolas Geoffrayf9a19952015-06-29 13:43:54 +0100356void GraphChecker::VisitCheckCast(HCheckCast* check) {
357 VisitInstruction(check);
358 HInstruction* input = check->InputAt(1);
359 if (!input->IsLoadClass()) {
360 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
361 check->DebugName(),
362 check->GetId(),
363 input->DebugName(),
364 input->GetId()));
365 }
366}
367
368void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) {
369 VisitInstruction(instruction);
370 HInstruction* input = instruction->InputAt(1);
371 if (!input->IsLoadClass()) {
372 AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.",
373 instruction->DebugName(),
374 instruction->GetId(),
375 input->DebugName(),
376 input->GetId()));
377 }
378}
379
Roland Levillainccc07a92014-09-16 14:48:16 +0100380void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
381 super_type::VisitBasicBlock(block);
382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Ensure that catch blocks are not normal successors, and normal blocks are
384 // never exceptional successors.
David Brazdild26a4112015-11-10 11:07:31 +0000385 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100386 if (successor->IsCatchBlock()) {
387 AddError(StringPrintf("Catch block %d is a normal successor of block %d.",
388 successor->GetBlockId(),
389 block->GetBlockId()));
390 }
391 }
David Brazdild26a4112015-11-10 11:07:31 +0000392 for (HBasicBlock* successor : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100393 if (!successor->IsCatchBlock()) {
394 AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.",
395 successor->GetBlockId(),
396 block->GetBlockId()));
397 }
398 }
399
Roland Levillainccc07a92014-09-16 14:48:16 +0100400 // Ensure there is no critical edge (i.e., an edge connecting a
401 // block with multiple successors to a block with multiple
David Brazdilffee3d32015-07-06 11:48:53 +0100402 // predecessors). Exceptional edges are synthesized and hence
403 // not accounted for.
David Brazdil81e479e2015-11-10 10:12:41 +0000404 if (block->GetSuccessors().size() > 1) {
David Brazdild26a4112015-11-10 11:07:31 +0000405 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdil81e479e2015-11-10 10:12:41 +0000406 if (successor->IsExitBlock() &&
407 block->IsSingleTryBoundary() &&
408 block->GetPredecessors().size() == 1u &&
409 block->GetSinglePredecessor()->GetLastInstruction()->IsThrow()) {
410 // Allowed critical edge Throw->TryBoundary->Exit.
411 } else if (successor->GetPredecessors().size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000412 AddError(StringPrintf("Critical edge between blocks %d and %d.",
413 block->GetBlockId(),
414 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100415 }
416 }
417 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100418
David Brazdilffee3d32015-07-06 11:48:53 +0100419 // Ensure try membership information is consistent.
David Brazdilffee3d32015-07-06 11:48:53 +0100420 if (block->IsCatchBlock()) {
David Brazdilec16f792015-08-19 15:04:01 +0100421 if (block->IsTryBlock()) {
422 const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry();
David Brazdilffee3d32015-07-06 11:48:53 +0100423 AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d "
424 "has try entry %s:%d.",
425 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100426 try_entry.DebugName(),
427 try_entry.GetId()));
David Brazdilffee3d32015-07-06 11:48:53 +0100428 }
429
430 if (block->IsLoopHeader()) {
431 AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.",
432 block->GetBlockId()));
433 }
434 } else {
Vladimir Marko60584552015-09-03 13:35:12 +0000435 for (HBasicBlock* predecessor : block->GetPredecessors()) {
David Brazdilec16f792015-08-19 15:04:01 +0100436 const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors();
437 if (block->IsTryBlock()) {
438 const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry();
439 if (incoming_try_entry == nullptr) {
440 AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100441 "from predecessor %d.",
442 block->GetBlockId(),
David Brazdilec16f792015-08-19 15:04:01 +0100443 stored_try_entry.DebugName(),
444 stored_try_entry.GetId(),
445 predecessor->GetBlockId()));
446 } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) {
447 AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent "
448 "with %s:%d that follows from predecessor %d.",
449 block->GetBlockId(),
450 stored_try_entry.DebugName(),
451 stored_try_entry.GetId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100452 incoming_try_entry->DebugName(),
453 incoming_try_entry->GetId(),
454 predecessor->GetBlockId()));
455 }
David Brazdilec16f792015-08-19 15:04:01 +0100456 } else if (incoming_try_entry != nullptr) {
457 AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows "
David Brazdilffee3d32015-07-06 11:48:53 +0100458 "from predecessor %d.",
459 block->GetBlockId(),
David Brazdilffee3d32015-07-06 11:48:53 +0100460 incoming_try_entry->DebugName(),
461 incoming_try_entry->GetId(),
462 predecessor->GetBlockId()));
463 }
464 }
465 }
466
Roland Levillain6b879dd2014-09-22 17:13:44 +0100467 if (block->IsLoopHeader()) {
468 CheckLoop(block);
469 }
470}
471
472void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
473 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100474 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100475
David Brazdildb51efb2015-11-06 01:36:20 +0000476 // Ensure the pre-header block is first in the list of predecessors of a loop
477 // header and that the header block is its only successor.
Roland Levillain6b879dd2014-09-22 17:13:44 +0100478 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000479 AddError(StringPrintf(
480 "Loop pre-header is not the first predecessor of the loop header %d.",
481 id));
David Brazdildb51efb2015-11-06 01:36:20 +0000482 } else if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) {
483 AddError(StringPrintf(
484 "Loop pre-header %d of loop defined by header %d has %zu successors.",
485 loop_information->GetPreHeader()->GetBlockId(),
486 id,
487 loop_information->GetPreHeader()->GetSuccessors().size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100488 }
489
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100490 // Ensure the loop header has only one incoming branch and the remaining
491 // predecessors are back edges.
Vladimir Marko60584552015-09-03 13:35:12 +0000492 size_t num_preds = loop_header->GetPredecessors().size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000493 if (num_preds < 2) {
494 AddError(StringPrintf(
495 "Loop header %d has less than two predecessors: %zu.",
496 id,
497 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100498 } else {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100499 HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0];
David Brazdil46e2a392015-03-16 17:31:52 +0000500 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000501 AddError(StringPrintf(
502 "First predecessor of loop header %d is a back edge.",
503 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100504 }
Vladimir Marko60584552015-09-03 13:35:12 +0000505 for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100506 HBasicBlock* predecessor = loop_header->GetPredecessors()[i];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100507 if (!loop_information->IsBackEdge(*predecessor)) {
508 AddError(StringPrintf(
509 "Loop header %d has multiple incoming (non back edge) blocks.",
510 id));
511 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100512 }
513 }
514
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100515 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100516
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100517 // Ensure back edges belong to the loop.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 if (loop_information->NumberOfBackEdges() == 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000519 AddError(StringPrintf(
520 "Loop defined by header %d has no back edge.",
521 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100522 } else {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100523 for (HBasicBlock* back_edge : loop_information->GetBackEdges()) {
524 int back_edge_id = back_edge->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100525 if (!loop_blocks.IsBitSet(back_edge_id)) {
526 AddError(StringPrintf(
527 "Loop defined by header %d has an invalid back edge %d.",
528 id,
529 back_edge_id));
David Brazdildb51efb2015-11-06 01:36:20 +0000530 } else if (back_edge->GetLoopInformation() != loop_information) {
531 AddError(StringPrintf(
532 "Back edge %d of loop defined by header %d belongs to nested loop "
533 "with header %d.",
534 back_edge_id,
535 id,
536 back_edge->GetLoopInformation()->GetHeader()->GetBlockId()));
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100537 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100538 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100539 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100540
David Brazdil2d7352b2015-04-20 14:52:42 +0100541 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100542 for (uint32_t i : loop_blocks.Indexes()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100543 HBasicBlock* loop_block = GetGraph()->GetBlocks()[i];
David Brazdil2d7352b2015-04-20 14:52:42 +0100544 if (loop_block == nullptr) {
545 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
546 id,
547 i));
548 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000549 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100550 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000551 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100552 }
553 }
David Brazdil7d275372015-04-21 16:36:35 +0100554
555 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
556 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
557 HLoopInformation* outer_info = it.Current();
558 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
559 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
560 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100561 id,
David Brazdil7d275372015-04-21 16:36:35 +0100562 outer_info->GetHeader()->GetBlockId()));
563 }
564 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100565}
566
567void SSAChecker::VisitInstruction(HInstruction* instruction) {
568 super_type::VisitInstruction(instruction);
569
Roland Levillaina8069ce2014-10-01 10:48:29 +0100570 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000571 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100572 !use_it.Done(); use_it.Advance()) {
573 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100574 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Vladimir Markob554b5a2015-11-06 12:57:55 +0000575 AddError(StringPrintf("Instruction %s:%d in block %d does not dominate "
576 "use %s:%d in block %d.",
577 instruction->DebugName(),
578 instruction->GetId(),
579 current_block_->GetBlockId(),
580 use->DebugName(),
581 use->GetId(),
582 use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100583 }
584 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100585
586 // Ensure an instruction having an environment is dominated by the
587 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100588 for (HEnvironment* environment = instruction->GetEnvironment();
589 environment != nullptr;
590 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100591 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
592 HInstruction* env_instruction = environment->GetInstructionAt(i);
593 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100594 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000595 AddError(StringPrintf("Instruction %d in environment of instruction %d "
596 "from block %d does not dominate instruction %d.",
597 env_instruction->GetId(),
598 instruction->GetId(),
599 current_block_->GetBlockId(),
600 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100601 }
602 }
603 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100604}
605
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000606static Primitive::Type PrimitiveKind(Primitive::Type type) {
607 switch (type) {
608 case Primitive::kPrimBoolean:
609 case Primitive::kPrimByte:
610 case Primitive::kPrimShort:
611 case Primitive::kPrimChar:
612 case Primitive::kPrimInt:
613 return Primitive::kPrimInt;
614 default:
615 return type;
616 }
617}
618
David Brazdil77a48ae2015-09-15 12:34:04 +0000619static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) {
620 return insn1->IsConstant()
621 && insn2->IsConstant()
622 && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType());
623}
624
625static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) {
626 if (insn1->IsPhi() &&
627 insn1->AsPhi()->IsVRegEquivalentOf(insn2) &&
628 insn1->InputCount() == insn2->InputCount()) {
629 // Testing only one of the two inputs for recursion is sufficient.
630 if (visited->IsBitSet(insn1->GetId())) {
631 return true;
632 }
633 visited->SetBit(insn1->GetId());
634
635 for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) {
636 if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) {
637 return false;
638 }
639 }
640 return true;
641 } else if (IsSameSizeConstant(insn1, insn2)) {
642 return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64();
643 } else {
644 return false;
645 }
646}
647
Roland Levillain6b879dd2014-09-22 17:13:44 +0100648void SSAChecker::VisitPhi(HPhi* phi) {
649 VisitInstruction(phi);
650
651 // Ensure the first input of a phi is not itself.
652 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000653 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
654 phi->GetId(),
655 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100656 }
657
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000658 // Ensure that the inputs have the same primitive kind as the phi.
659 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
660 HInstruction* input = phi->InputAt(i);
661 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
662 AddError(StringPrintf(
663 "Input %d at index %zu of phi %d from block %d does not have the "
664 "same type as the phi: %s versus %s",
665 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
666 Primitive::PrettyDescriptor(input->GetType()),
667 Primitive::PrettyDescriptor(phi->GetType())));
668 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000669 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000670 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
671 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
672 phi->GetId(),
673 phi->GetBlock()->GetBlockId(),
674 Primitive::PrettyDescriptor(phi->GetType())));
675 }
David Brazdilffee3d32015-07-06 11:48:53 +0100676
677 if (phi->IsCatchPhi()) {
David Brazdil3eaa32f2015-09-18 10:58:32 +0100678 // The number of inputs of a catch phi should be the total number of throwing
679 // instructions caught by this catch block. We do not enforce this, however,
680 // because we do not remove the corresponding inputs when we prove that an
681 // instruction cannot throw. Instead, we at least test that all phis have the
682 // same, non-zero number of inputs (b/24054676).
683 size_t input_count_this = phi->InputCount();
684 if (input_count_this == 0u) {
685 AddError(StringPrintf("Phi %d in catch block %d has zero inputs.",
686 phi->GetId(),
687 phi->GetBlock()->GetBlockId()));
688 } else {
689 HInstruction* next_phi = phi->GetNext();
690 if (next_phi != nullptr) {
691 size_t input_count_next = next_phi->InputCount();
692 if (input_count_this != input_count_next) {
693 AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, "
694 "but phi %d has %zu inputs.",
695 phi->GetId(),
696 phi->GetBlock()->GetBlockId(),
697 input_count_this,
698 next_phi->GetId(),
699 input_count_next));
700 }
701 }
702 }
David Brazdilffee3d32015-07-06 11:48:53 +0100703 } else {
704 // Ensure the number of inputs of a non-catch phi is the same as the number
705 // of its predecessors.
Vladimir Marko60584552015-09-03 13:35:12 +0000706 const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors();
707 if (phi->InputCount() != predecessors.size()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100708 AddError(StringPrintf(
709 "Phi %d in block %d has %zu inputs, "
710 "but block %d has %zu predecessors.",
711 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
Vladimir Marko60584552015-09-03 13:35:12 +0000712 phi->GetBlock()->GetBlockId(), predecessors.size()));
David Brazdilffee3d32015-07-06 11:48:53 +0100713 } else {
714 // Ensure phi input at index I either comes from the Ith
715 // predecessor or from a block that dominates this predecessor.
716 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
717 HInstruction* input = phi->InputAt(i);
Vladimir Marko60584552015-09-03 13:35:12 +0000718 HBasicBlock* predecessor = predecessors[i];
David Brazdilffee3d32015-07-06 11:48:53 +0100719 if (!(input->GetBlock() == predecessor
720 || input->GetBlock()->Dominates(predecessor))) {
721 AddError(StringPrintf(
722 "Input %d at index %zu of phi %d from block %d is not defined in "
723 "predecessor number %zu nor in a block dominating it.",
724 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
725 i));
726 }
727 }
728 }
729 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000730
731 // Ensure that catch phis are sorted by their vreg number, as required by
732 // the register allocator and code generator. This does not apply to normal
733 // phis which can be constructed artifically.
734 if (phi->IsCatchPhi()) {
735 HInstruction* next_phi = phi->GetNext();
736 if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) {
737 AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their "
738 "vreg numbers.",
739 phi->GetId(),
740 next_phi->GetId(),
741 phi->GetBlock()->GetBlockId()));
742 }
743 }
744
745 // Test phi equivalents. There should not be two of the same type and they
746 // should only be created for constants which were untyped in DEX.
747 for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
748 HPhi* other_phi = phi_it.Current()->AsPhi();
749 if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) {
750 if (phi->GetType() == other_phi->GetType()) {
751 std::stringstream type_str;
752 type_str << phi->GetType();
753 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.",
754 phi->GetId(),
755 phi->GetRegNumber(),
756 type_str.str().c_str()));
757 } else {
758 ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true);
759 if (!IsConstantEquivalent(phi, other_phi, &visited)) {
760 AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they "
761 "are not equivalents of constants.",
762 phi->GetId(),
763 other_phi->GetId(),
764 phi->GetRegNumber()));
765 }
766 }
767 }
768 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000769}
770
David Brazdil13b47182015-04-15 16:29:32 +0100771void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
772 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000773 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100774 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000775 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000776 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100777 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
778 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000779 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100780 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000781 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000782 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100783 } else if (input->GetType() == Primitive::kPrimInt
784 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
785 // TODO: We need a data-flow analysis to determine if the Phi or
786 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100787 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000788 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100789 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
790 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000791 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100792 static_cast<int>(input_index),
793 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000794 }
795}
796
Mark Mendellfe57faa2015-09-18 09:26:15 -0400797void SSAChecker::VisitPackedSwitch(HPackedSwitch* instruction) {
798 VisitInstruction(instruction);
799 // Check that the number of block successors matches the switch count plus
800 // one for the default block.
801 HBasicBlock* block = instruction->GetBlock();
802 if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) {
803 AddError(StringPrintf(
804 "%s instruction %d in block %d expects %u successors to the block, but found: %zu.",
805 instruction->DebugName(),
806 instruction->GetId(),
807 block->GetBlockId(),
808 instruction->GetNumEntries() + 1u,
809 block->GetSuccessors().size()));
810 }
811}
812
David Brazdil13b47182015-04-15 16:29:32 +0100813void SSAChecker::VisitIf(HIf* instruction) {
814 VisitInstruction(instruction);
815 HandleBooleanInput(instruction, 0);
816}
817
818void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
819 VisitInstruction(instruction);
820 HandleBooleanInput(instruction, 0);
821}
822
Nicolas Geoffray31596742014-11-24 15:28:45 +0000823void SSAChecker::VisitCondition(HCondition* op) {
824 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000825 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000826 AddError(StringPrintf(
827 "Condition %s %d has a non-Boolean result type: %s.",
828 op->DebugName(), op->GetId(),
829 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000830 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000831 HInstruction* lhs = op->InputAt(0);
832 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100833 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
834 AddError(StringPrintf(
835 "Condition %s %d has inputs of different types: %s, and %s.",
836 op->DebugName(), op->GetId(),
837 Primitive::PrettyDescriptor(lhs->GetType()),
838 Primitive::PrettyDescriptor(rhs->GetType())));
839 }
840 if (!op->IsEqual() && !op->IsNotEqual()) {
841 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000842 AddError(StringPrintf(
843 "Condition %s %d uses an object as left-hand side input.",
844 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100845 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000846 AddError(StringPrintf(
847 "Condition %s %d uses an object as right-hand side input.",
848 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000849 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000850 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000851}
852
853void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
854 VisitInstruction(op);
855 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
856 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000857 AddError(StringPrintf(
858 "Shift operation %s %d has a non-int kind second input: "
859 "%s of type %s.",
860 op->DebugName(), op->GetId(),
861 op->InputAt(1)->DebugName(),
862 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000863 }
864 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100865 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000866 AddError(StringPrintf(
867 "Binary operation %s %d has inputs of different types: "
868 "%s, and %s.",
869 op->DebugName(), op->GetId(),
870 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
871 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000872 }
873 }
874
875 if (op->IsCompare()) {
876 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000877 AddError(StringPrintf(
878 "Compare operation %d has a non-int result type: %s.",
879 op->GetId(),
880 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000881 }
882 } else {
883 // Use the first input, so that we can also make this check for shift operations.
884 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000885 AddError(StringPrintf(
886 "Binary operation %s %d has a result type different "
887 "from its input type: %s vs %s.",
888 op->DebugName(), op->GetId(),
889 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100890 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000891 }
892 }
893}
894
David Brazdil8d5b8b22015-03-24 10:51:52 +0000895void SSAChecker::VisitConstant(HConstant* instruction) {
896 HBasicBlock* block = instruction->GetBlock();
897 if (!block->IsEntryBlock()) {
898 AddError(StringPrintf(
899 "%s %d should be in the entry block but is in block %d.",
900 instruction->DebugName(),
901 instruction->GetId(),
902 block->GetBlockId()));
903 }
904}
905
Roland Levillainccc07a92014-09-16 14:48:16 +0100906} // namespace art