blob: 7c3c2bf03d1fcff53519b8e7166c06fdfac9f5e5 [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
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000372void SSAChecker::VisitIf(HIf* instruction) {
373 VisitInstruction(instruction);
374 HInstruction* input = instruction->InputAt(0);
375 if (input->IsIntConstant()) {
376 int value = input->AsIntConstant()->GetValue();
377 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000378 AddError(StringPrintf(
379 "If instruction %d has a non-Boolean constant input "
380 "whose value is: %d.",
381 instruction->GetId(),
382 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000383 }
384 } else if (instruction->InputAt(0)->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000385 AddError(StringPrintf(
386 "If instruction %d has a non-Boolean input type: %s.",
387 instruction->GetId(),
388 Primitive::PrettyDescriptor(instruction->InputAt(0)->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000389 }
390}
391
Nicolas Geoffray31596742014-11-24 15:28:45 +0000392void SSAChecker::VisitCondition(HCondition* op) {
393 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000394 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000395 AddError(StringPrintf(
396 "Condition %s %d has a non-Boolean result type: %s.",
397 op->DebugName(), op->GetId(),
398 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000399 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000400 HInstruction* lhs = op->InputAt(0);
401 HInstruction* rhs = op->InputAt(1);
Roland Levillainaecbd262015-01-19 12:44:01 +0000402 if (lhs->GetType() == Primitive::kPrimNot) {
403 if (!op->IsEqual() && !op->IsNotEqual()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000404 AddError(StringPrintf(
405 "Condition %s %d uses an object as left-hand side input.",
406 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000407 }
408 if (rhs->IsIntConstant() && rhs->AsIntConstant()->GetValue() != 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000409 AddError(StringPrintf(
410 "Condition %s %d compares an object with a non-zero integer: %d.",
411 op->DebugName(), op->GetId(),
412 rhs->AsIntConstant()->GetValue()));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000413 }
Roland Levillainaecbd262015-01-19 12:44:01 +0000414 } else if (rhs->GetType() == Primitive::kPrimNot) {
415 if (!op->IsEqual() && !op->IsNotEqual()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000416 AddError(StringPrintf(
417 "Condition %s %d uses an object as right-hand side input.",
418 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000419 }
420 if (lhs->IsIntConstant() && lhs->AsIntConstant()->GetValue() != 0) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000421 AddError(StringPrintf(
422 "Condition %s %d compares a non-zero integer with an object: %d.",
423 op->DebugName(), op->GetId(),
424 lhs->AsIntConstant()->GetValue()));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000425 }
426 } else if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000427 AddError(StringPrintf(
428 "Condition %s %d has inputs of different types: "
429 "%s, and %s.",
430 op->DebugName(), op->GetId(),
431 Primitive::PrettyDescriptor(lhs->GetType()),
432 Primitive::PrettyDescriptor(rhs->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000433 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000434}
435
436void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
437 VisitInstruction(op);
438 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
439 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000440 AddError(StringPrintf(
441 "Shift operation %s %d has a non-int kind second input: "
442 "%s of type %s.",
443 op->DebugName(), op->GetId(),
444 op->InputAt(1)->DebugName(),
445 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000446 }
447 } else {
448 if (PrimitiveKind(op->InputAt(1)->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000449 AddError(StringPrintf(
450 "Binary operation %s %d has inputs of different types: "
451 "%s, and %s.",
452 op->DebugName(), op->GetId(),
453 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
454 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000455 }
456 }
457
458 if (op->IsCompare()) {
459 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000460 AddError(StringPrintf(
461 "Compare operation %d has a non-int result type: %s.",
462 op->GetId(),
463 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000464 }
465 } else {
466 // Use the first input, so that we can also make this check for shift operations.
467 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000468 AddError(StringPrintf(
469 "Binary operation %s %d has a result type different "
470 "from its input type: %s vs %s.",
471 op->DebugName(), op->GetId(),
472 Primitive::PrettyDescriptor(op->GetType()),
473 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000474 }
475 }
476}
477
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478void SSAChecker::VisitConstant(HConstant* instruction) {
479 HBasicBlock* block = instruction->GetBlock();
480 if (!block->IsEntryBlock()) {
481 AddError(StringPrintf(
482 "%s %d should be in the entry block but is in block %d.",
483 instruction->DebugName(),
484 instruction->GetId(),
485 block->GetBlockId()));
486 }
487}
488
Roland Levillainccc07a92014-09-16 14:48:16 +0100489} // namespace art