blob: 906c8e8c768e8aadb09ed9ece6765c39ce3d4fee [file] [log] [blame]
Roland Levillainccc07a92014-09-16 14:48:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "graph_checker.h"
18
Roland Levillainccc07a92014-09-16 14:48:16 +010019#include <map>
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +000020#include <string>
Roland Levillainccc07a92014-09-16 14:48:16 +010021
Roland Levillain7e53b412014-09-23 10:50:22 +010022#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000023#include "base/stringprintf.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010024
Roland Levillainccc07a92014-09-16 14:48:16 +010025namespace art {
26
27void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
28 current_block_ = block;
29
30 // Check consistency with respect to predecessors of `block`.
31 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
32 std::map<HBasicBlock*, size_t> predecessors_count;
33 for (size_t i = 0, e = predecessors.Size(); i < e; ++i) {
34 HBasicBlock* p = predecessors.Get(i);
35 ++predecessors_count[p];
36 }
37 for (auto& pc : predecessors_count) {
38 HBasicBlock* p = pc.first;
39 size_t p_count_in_block_predecessors = pc.second;
40 const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors();
41 size_t block_count_in_p_successors = 0;
42 for (size_t j = 0, f = p_successors.Size(); j < f; ++j) {
43 if (p_successors.Get(j) == block) {
44 ++block_count_in_p_successors;
45 }
46 }
47 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000048 AddError(StringPrintf(
49 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
50 "block %d lists %zu occurrences of block %d in its successors.",
51 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
52 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010053 }
54 }
55
56 // Check consistency with respect to successors of `block`.
57 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
58 std::map<HBasicBlock*, size_t> successors_count;
59 for (size_t i = 0, e = successors.Size(); i < e; ++i) {
60 HBasicBlock* s = successors.Get(i);
61 ++successors_count[s];
62 }
63 for (auto& sc : successors_count) {
64 HBasicBlock* s = sc.first;
65 size_t s_count_in_block_successors = sc.second;
66 const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors();
67 size_t block_count_in_s_predecessors = 0;
68 for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) {
69 if (s_predecessors.Get(j) == block) {
70 ++block_count_in_s_predecessors;
71 }
72 }
73 if (s_count_in_block_successors != block_count_in_s_predecessors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000074 AddError(StringPrintf(
75 "Block %d lists %zu occurrences of block %d in its successors, whereas "
76 "block %d lists %zu occurrences of block %d in its predecessors.",
77 block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(),
78 s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010079 }
80 }
81
82 // Ensure `block` ends with a branch instruction.
David Brazdil8d5b8b22015-03-24 10:51:52 +000083 if (!block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000084 AddError(StringPrintf("Block %d does not end with a branch instruction.",
85 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010086 }
87
88 // Visit this block's list of phis.
89 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
90 // Ensure this block's list of phis contains only phis.
91 if (!it.Current()->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000092 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
93 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010094 }
95 it.Current()->Accept(this);
96 }
97
98 // Visit this block's list of instructions.
99 for (HInstructionIterator it(block->GetInstructions()); !it.Done();
100 it.Advance()) {
101 // Ensure this block's list of instructions does not contains phis.
102 if (it.Current()->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000103 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
104 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100105 }
106 it.Current()->Accept(this);
107 }
108}
109
110void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000111 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000112 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
113 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000114 } else {
115 seen_ids_.SetBit(instruction->GetId());
116 }
117
Roland Levillainccc07a92014-09-16 14:48:16 +0100118 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000119 if (instruction->GetBlock() == nullptr) {
120 AddError(StringPrintf("%s %d in block %d not associated with any block.",
121 instruction->IsPhi() ? "Phi" : "Instruction",
122 instruction->GetId(),
123 current_block_->GetBlockId()));
124 } else if (instruction->GetBlock() != current_block_) {
125 AddError(StringPrintf("%s %d in block %d associated with block %d.",
126 instruction->IsPhi() ? "Phi" : "Instruction",
127 instruction->GetId(),
128 current_block_->GetBlockId(),
129 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100130 }
Roland Levillain6b469232014-09-25 10:10:38 +0100131
132 // Ensure the inputs of `instruction` are defined in a block of the graph.
133 for (HInputIterator input_it(instruction); !input_it.Done();
134 input_it.Advance()) {
135 HInstruction* input = input_it.Current();
136 const HInstructionList& list = input->IsPhi()
137 ? input->GetBlock()->GetPhis()
138 : input->GetBlock()->GetInstructions();
139 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000140 AddError(StringPrintf("Input %d of instruction %d is not defined "
141 "in a basic block of the control-flow graph.",
142 input->GetId(),
143 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100144 }
145 }
146
147 // Ensure the uses of `instruction` are defined in a block of the graph.
David Brazdiled596192015-01-23 10:39:45 +0000148 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100149 !use_it.Done(); use_it.Advance()) {
150 HInstruction* use = use_it.Current()->GetUser();
151 const HInstructionList& list = use->IsPhi()
152 ? use->GetBlock()->GetPhis()
153 : use->GetBlock()->GetInstructions();
154 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000155 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000156 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000158 use->GetId(),
159 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100160 }
161 }
David Brazdil1abb4192015-02-17 18:33:36 +0000162
163 // Ensure 'instruction' has pointers to its inputs' use entries.
164 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
165 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
166 HInstruction* input = input_record.GetInstruction();
167 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
168 if (use_node == nullptr || !input->GetUses().Contains(use_node)) {
169 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
170 "at input %u (%s:%d).",
171 instruction->DebugName(),
172 instruction->GetId(),
173 static_cast<unsigned>(i),
174 input->DebugName(),
175 input->GetId()));
176 }
177 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100178}
179
180void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
181 super_type::VisitBasicBlock(block);
182
183 // Ensure there is no critical edge (i.e., an edge connecting a
184 // block with multiple successors to a block with multiple
185 // predecessors).
186 if (block->GetSuccessors().Size() > 1) {
187 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
188 HBasicBlock* successor = block->GetSuccessors().Get(j);
189 if (successor->GetPredecessors().Size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000190 AddError(StringPrintf("Critical edge between blocks %d and %d.",
191 block->GetBlockId(),
192 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100193 }
194 }
195 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100196
197 if (block->IsLoopHeader()) {
198 CheckLoop(block);
199 }
200}
201
202void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
203 int id = loop_header->GetBlockId();
204
205 // Ensure the pre-header block is first in the list of
206 // predecessors of a loop header.
207 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000208 AddError(StringPrintf(
209 "Loop pre-header is not the first predecessor of the loop header %d.",
210 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100211 }
212
213 // Ensure the loop header has only two predecessors and that only the
214 // second one is a back edge.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000215 size_t num_preds = loop_header->GetPredecessors().Size();
216 if (num_preds < 2) {
217 AddError(StringPrintf(
218 "Loop header %d has less than two predecessors: %zu.",
219 id,
220 num_preds));
221 } else if (num_preds > 2) {
222 AddError(StringPrintf(
223 "Loop header %d has more than two predecessors: %zu.",
224 id,
225 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100226 } else {
227 HLoopInformation* loop_information = loop_header->GetLoopInformation();
228 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000229 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000230 AddError(StringPrintf(
231 "First predecessor of loop header %d is a back edge.",
232 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100233 }
234 HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1);
David Brazdil46e2a392015-03-16 17:31:52 +0000235 if (!loop_information->IsBackEdge(*second_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000236 AddError(StringPrintf(
237 "Second predecessor of loop header %d is not a back edge.",
238 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100239 }
240 }
241
242 // Ensure there is only one back edge per loop.
243 size_t num_back_edges =
244 loop_header->GetLoopInformation()->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000245 if (num_back_edges == 0) {
246 AddError(StringPrintf(
247 "Loop defined by header %d has no back edge.",
248 id));
249 } else if (num_back_edges > 1) {
250 AddError(StringPrintf(
251 "Loop defined by header %d has several back edges: %zu.",
252 id,
253 num_back_edges));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100254 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100255
256 // Ensure all blocks in the loop are dominated by the loop header.
257 const ArenaBitVector& loop_blocks =
258 loop_header->GetLoopInformation()->GetBlocks();
259 for (uint32_t i : loop_blocks.Indexes()) {
260 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
261 if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000262 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
263 loop_block->GetBlockId(),
264 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100265 }
266 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100267}
268
269void SSAChecker::VisitInstruction(HInstruction* instruction) {
270 super_type::VisitInstruction(instruction);
271
Roland Levillaina8069ce2014-10-01 10:48:29 +0100272 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000273 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100274 !use_it.Done(); use_it.Advance()) {
275 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100276 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000277 AddError(StringPrintf("Instruction %d in block %d does not dominate "
278 "use %d in block %d.",
279 instruction->GetId(), current_block_->GetBlockId(),
280 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100281 }
282 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100283
284 // Ensure an instruction having an environment is dominated by the
285 // instructions contained in the environment.
286 HEnvironment* environment = instruction->GetEnvironment();
287 if (environment != nullptr) {
288 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
289 HInstruction* env_instruction = environment->GetInstructionAt(i);
290 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100291 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000292 AddError(StringPrintf("Instruction %d in environment of instruction %d "
293 "from block %d does not dominate instruction %d.",
294 env_instruction->GetId(),
295 instruction->GetId(),
296 current_block_->GetBlockId(),
297 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100298 }
299 }
300 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100301}
302
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000303static Primitive::Type PrimitiveKind(Primitive::Type type) {
304 switch (type) {
305 case Primitive::kPrimBoolean:
306 case Primitive::kPrimByte:
307 case Primitive::kPrimShort:
308 case Primitive::kPrimChar:
309 case Primitive::kPrimInt:
310 return Primitive::kPrimInt;
311 default:
312 return type;
313 }
314}
315
Roland Levillain6b879dd2014-09-22 17:13:44 +0100316void SSAChecker::VisitPhi(HPhi* phi) {
317 VisitInstruction(phi);
318
319 // Ensure the first input of a phi is not itself.
320 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000321 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
322 phi->GetId(),
323 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100324 }
325
Roland Levillain5c4405e2015-01-21 11:39:58 +0000326 // Ensure the number of inputs of a phi is the same as the number of
Roland Levillain6b879dd2014-09-22 17:13:44 +0100327 // its predecessors.
328 const GrowableArray<HBasicBlock*>& predecessors =
329 phi->GetBlock()->GetPredecessors();
330 if (phi->InputCount() != predecessors.Size()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000331 AddError(StringPrintf(
332 "Phi %d in block %d has %zu inputs, "
333 "but block %d has %zu predecessors.",
334 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
335 phi->GetBlock()->GetBlockId(), predecessors.Size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100336 } else {
337 // Ensure phi input at index I either comes from the Ith
338 // predecessor or from a block that dominates this predecessor.
339 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
340 HInstruction* input = phi->InputAt(i);
341 HBasicBlock* predecessor = predecessors.Get(i);
342 if (!(input->GetBlock() == predecessor
343 || input->GetBlock()->Dominates(predecessor))) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000344 AddError(StringPrintf(
345 "Input %d at index %zu of phi %d from block %d is not defined in "
346 "predecessor number %zu nor in a block dominating it.",
347 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
348 i));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100349 }
350 }
351 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352 // Ensure that the inputs have the same primitive kind as the phi.
353 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
354 HInstruction* input = phi->InputAt(i);
355 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
356 AddError(StringPrintf(
357 "Input %d at index %zu of phi %d from block %d does not have the "
358 "same type as the phi: %s versus %s",
359 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
360 Primitive::PrettyDescriptor(input->GetType()),
361 Primitive::PrettyDescriptor(phi->GetType())));
362 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000363 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000364 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
365 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
366 phi->GetId(),
367 phi->GetBlock()->GetBlockId(),
368 Primitive::PrettyDescriptor(phi->GetType())));
369 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000370}
371
David Brazdil13b47182015-04-15 16:29:32 +0100372void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
373 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000374 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100375 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000376 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000377 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100378 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
379 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000380 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100381 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000382 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000383 }
David Brazdil13b47182015-04-15 16:29:32 +0100384 } else if (input->GetType() == Primitive::kPrimInt && input->IsPhi()) {
385 // TODO: We need a data-flow analysis which determines if the Phi is boolean.
386 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000387 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100388 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
389 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000390 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100391 static_cast<int>(input_index),
392 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000393 }
394}
395
David Brazdil13b47182015-04-15 16:29:32 +0100396void SSAChecker::VisitIf(HIf* instruction) {
397 VisitInstruction(instruction);
398 HandleBooleanInput(instruction, 0);
399}
400
401void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
402 VisitInstruction(instruction);
403 HandleBooleanInput(instruction, 0);
404}
405
Nicolas Geoffray31596742014-11-24 15:28:45 +0000406void SSAChecker::VisitCondition(HCondition* op) {
407 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000408 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000409 AddError(StringPrintf(
410 "Condition %s %d has a non-Boolean result type: %s.",
411 op->DebugName(), op->GetId(),
412 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000413 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000414 HInstruction* lhs = op->InputAt(0);
415 HInstruction* rhs = op->InputAt(1);
Roland Levillainaecbd262015-01-19 12:44:01 +0000416 if (lhs->GetType() == Primitive::kPrimNot) {
417 if (!op->IsEqual() && !op->IsNotEqual()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000418 AddError(StringPrintf(
419 "Condition %s %d uses an object as left-hand side input.",
420 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000421 }
422 if (rhs->IsIntConstant() && rhs->AsIntConstant()->GetValue() != 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000423 AddError(StringPrintf(
424 "Condition %s %d compares an object with a non-zero integer: %d.",
425 op->DebugName(), op->GetId(),
426 rhs->AsIntConstant()->GetValue()));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000427 }
Roland Levillainaecbd262015-01-19 12:44:01 +0000428 } else if (rhs->GetType() == Primitive::kPrimNot) {
429 if (!op->IsEqual() && !op->IsNotEqual()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000430 AddError(StringPrintf(
431 "Condition %s %d uses an object as right-hand side input.",
432 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000433 }
434 if (lhs->IsIntConstant() && lhs->AsIntConstant()->GetValue() != 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000435 AddError(StringPrintf(
436 "Condition %s %d compares a non-zero integer with an object: %d.",
437 op->DebugName(), op->GetId(),
438 lhs->AsIntConstant()->GetValue()));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000439 }
440 } else if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000441 AddError(StringPrintf(
442 "Condition %s %d has inputs of different types: "
443 "%s, and %s.",
444 op->DebugName(), op->GetId(),
445 Primitive::PrettyDescriptor(lhs->GetType()),
446 Primitive::PrettyDescriptor(rhs->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000447 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000448}
449
450void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
451 VisitInstruction(op);
452 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
453 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000454 AddError(StringPrintf(
455 "Shift operation %s %d has a non-int kind second input: "
456 "%s of type %s.",
457 op->DebugName(), op->GetId(),
458 op->InputAt(1)->DebugName(),
459 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000460 }
461 } else {
462 if (PrimitiveKind(op->InputAt(1)->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000463 AddError(StringPrintf(
464 "Binary operation %s %d has inputs of different types: "
465 "%s, and %s.",
466 op->DebugName(), op->GetId(),
467 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
468 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000469 }
470 }
471
472 if (op->IsCompare()) {
473 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000474 AddError(StringPrintf(
475 "Compare operation %d has a non-int result type: %s.",
476 op->GetId(),
477 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000478 }
479 } else {
480 // Use the first input, so that we can also make this check for shift operations.
481 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000482 AddError(StringPrintf(
483 "Binary operation %s %d has a result type different "
484 "from its input type: %s vs %s.",
485 op->DebugName(), op->GetId(),
486 Primitive::PrettyDescriptor(op->GetType()),
487 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000488 }
489 }
490}
491
David Brazdil8d5b8b22015-03-24 10:51:52 +0000492void SSAChecker::VisitConstant(HConstant* instruction) {
493 HBasicBlock* block = instruction->GetBlock();
494 if (!block->IsEntryBlock()) {
495 AddError(StringPrintf(
496 "%s %d should be in the entry block but is in block %d.",
497 instruction->DebugName(),
498 instruction->GetId(),
499 block->GetBlockId()));
500 }
501}
502
Roland Levillainccc07a92014-09-16 14:48:16 +0100503} // namespace art