blob: e1649fd3cd72c2348c9c88630962ebcc0c5fd0ea [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>
Calin Juravlea4f88312015-04-16 12:57:19 +010021#include <sstream>
Roland Levillainccc07a92014-09-16 14:48:16 +010022
Roland Levillain7e53b412014-09-23 10:50:22 +010023#include "base/bit_vector-inl.h"
Roland Levillain5c4405e2015-01-21 11:39:58 +000024#include "base/stringprintf.h"
Roland Levillain7e53b412014-09-23 10:50:22 +010025
Roland Levillainccc07a92014-09-16 14:48:16 +010026namespace art {
27
28void GraphChecker::VisitBasicBlock(HBasicBlock* block) {
29 current_block_ = block;
30
31 // Check consistency with respect to predecessors of `block`.
32 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
33 std::map<HBasicBlock*, size_t> predecessors_count;
34 for (size_t i = 0, e = predecessors.Size(); i < e; ++i) {
35 HBasicBlock* p = predecessors.Get(i);
36 ++predecessors_count[p];
37 }
38 for (auto& pc : predecessors_count) {
39 HBasicBlock* p = pc.first;
40 size_t p_count_in_block_predecessors = pc.second;
41 const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors();
42 size_t block_count_in_p_successors = 0;
43 for (size_t j = 0, f = p_successors.Size(); j < f; ++j) {
44 if (p_successors.Get(j) == block) {
45 ++block_count_in_p_successors;
46 }
47 }
48 if (p_count_in_block_predecessors != block_count_in_p_successors) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000049 AddError(StringPrintf(
50 "Block %d lists %zu occurrences of block %d in its predecessors, whereas "
51 "block %d lists %zu occurrences of block %d in its successors.",
52 block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(),
53 p->GetBlockId(), block_count_in_p_successors, block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010054 }
55 }
56
57 // Check consistency with respect to successors of `block`.
58 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
59 std::map<HBasicBlock*, size_t> successors_count;
60 for (size_t i = 0, e = successors.Size(); i < e; ++i) {
61 HBasicBlock* s = successors.Get(i);
62 ++successors_count[s];
63 }
64 for (auto& sc : successors_count) {
65 HBasicBlock* s = sc.first;
66 size_t s_count_in_block_successors = sc.second;
67 const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors();
68 size_t block_count_in_s_predecessors = 0;
69 for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) {
70 if (s_predecessors.Get(j) == block) {
71 ++block_count_in_s_predecessors;
72 }
73 }
74 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 Brazdil8d5b8b22015-03-24 10:51:52 +000084 if (!block->EndsWithControlFlowInstruction()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000085 AddError(StringPrintf("Block %d does not end with a branch instruction.",
86 block->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010087 }
88
89 // Visit this block's list of phis.
90 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +010091 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +010092 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +010093 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000094 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
95 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010096 }
David Brazdilc3d743f2015-04-22 13:40:50 +010097 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
98 AddError(StringPrintf("The recorded last phi of block %d does not match "
99 "the actual last phi %d.",
100 current_block_->GetBlockId(),
101 current->GetId()));
102 }
103 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100104 }
105
106 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100107 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
108 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100109 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100110 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000111 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
112 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100113 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
115 AddError(StringPrintf("The recorded last instruction of block %d does not match "
116 "the actual last instruction %d.",
117 current_block_->GetBlockId(),
118 current->GetId()));
119 }
120 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 }
122}
123
124void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000125 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000126 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
127 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000128 } else {
129 seen_ids_.SetBit(instruction->GetId());
130 }
131
Roland Levillainccc07a92014-09-16 14:48:16 +0100132 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000133 if (instruction->GetBlock() == nullptr) {
134 AddError(StringPrintf("%s %d in block %d not associated with any block.",
135 instruction->IsPhi() ? "Phi" : "Instruction",
136 instruction->GetId(),
137 current_block_->GetBlockId()));
138 } else if (instruction->GetBlock() != current_block_) {
139 AddError(StringPrintf("%s %d in block %d associated with block %d.",
140 instruction->IsPhi() ? "Phi" : "Instruction",
141 instruction->GetId(),
142 current_block_->GetBlockId(),
143 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100144 }
Roland Levillain6b469232014-09-25 10:10:38 +0100145
146 // Ensure the inputs of `instruction` are defined in a block of the graph.
147 for (HInputIterator input_it(instruction); !input_it.Done();
148 input_it.Advance()) {
149 HInstruction* input = input_it.Current();
150 const HInstructionList& list = input->IsPhi()
151 ? input->GetBlock()->GetPhis()
152 : input->GetBlock()->GetInstructions();
153 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000154 AddError(StringPrintf("Input %d of instruction %d is not defined "
155 "in a basic block of the control-flow graph.",
156 input->GetId(),
157 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100158 }
159 }
160
161 // Ensure the uses of `instruction` are defined in a block of the graph.
David Brazdiled596192015-01-23 10:39:45 +0000162 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100163 !use_it.Done(); use_it.Advance()) {
164 HInstruction* use = use_it.Current()->GetUser();
165 const HInstructionList& list = use->IsPhi()
166 ? use->GetBlock()->GetPhis()
167 : use->GetBlock()->GetInstructions();
168 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000169 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000170 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000171 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000172 use->GetId(),
173 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100174 }
175 }
David Brazdil1abb4192015-02-17 18:33:36 +0000176
177 // Ensure 'instruction' has pointers to its inputs' use entries.
178 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
179 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
180 HInstruction* input = input_record.GetInstruction();
181 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
182 if (use_node == nullptr || !input->GetUses().Contains(use_node)) {
183 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
184 "at input %u (%s:%d).",
185 instruction->DebugName(),
186 instruction->GetId(),
187 static_cast<unsigned>(i),
188 input->DebugName(),
189 input->GetId()));
190 }
191 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100192}
193
194void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
195 super_type::VisitBasicBlock(block);
196
197 // Ensure there is no critical edge (i.e., an edge connecting a
198 // block with multiple successors to a block with multiple
199 // predecessors).
200 if (block->GetSuccessors().Size() > 1) {
201 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
202 HBasicBlock* successor = block->GetSuccessors().Get(j);
203 if (successor->GetPredecessors().Size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000204 AddError(StringPrintf("Critical edge between blocks %d and %d.",
205 block->GetBlockId(),
206 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100207 }
208 }
209 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100210
Calin Juravlea4f88312015-04-16 12:57:19 +0100211 // Check Phi uniqueness (no two Phis with the same type refer to the same register).
212 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
213 HPhi* phi = it.Current()->AsPhi();
214 if (phi->GetNextEquivalentPhiWithSameType() != nullptr) {
215 std::stringstream type_str;
216 type_str << phi->GetType();
217 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s",
218 phi->GetId(), phi->GetRegNumber(), type_str.str().c_str()));
219 }
220 }
221
Roland Levillain6b879dd2014-09-22 17:13:44 +0100222 if (block->IsLoopHeader()) {
223 CheckLoop(block);
224 }
225}
226
227void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
228 int id = loop_header->GetBlockId();
229
230 // Ensure the pre-header block is first in the list of
231 // predecessors of a loop header.
232 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000233 AddError(StringPrintf(
234 "Loop pre-header is not the first predecessor of the loop header %d.",
235 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100236 }
237
238 // Ensure the loop header has only two predecessors and that only the
239 // second one is a back edge.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000240 size_t num_preds = loop_header->GetPredecessors().Size();
241 if (num_preds < 2) {
242 AddError(StringPrintf(
243 "Loop header %d has less than two predecessors: %zu.",
244 id,
245 num_preds));
246 } else if (num_preds > 2) {
247 AddError(StringPrintf(
248 "Loop header %d has more than two predecessors: %zu.",
249 id,
250 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100251 } else {
252 HLoopInformation* loop_information = loop_header->GetLoopInformation();
253 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000254 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000255 AddError(StringPrintf(
256 "First predecessor of loop header %d is a back edge.",
257 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100258 }
259 HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1);
David Brazdil46e2a392015-03-16 17:31:52 +0000260 if (!loop_information->IsBackEdge(*second_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000261 AddError(StringPrintf(
262 "Second predecessor of loop header %d is not a back edge.",
263 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100264 }
265 }
266
David Brazdil2d7352b2015-04-20 14:52:42 +0100267 const ArenaBitVector& loop_blocks = loop_header->GetLoopInformation()->GetBlocks();
268
Roland Levillain6b879dd2014-09-22 17:13:44 +0100269 // Ensure there is only one back edge per loop.
270 size_t num_back_edges =
271 loop_header->GetLoopInformation()->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000272 if (num_back_edges == 0) {
273 AddError(StringPrintf(
274 "Loop defined by header %d has no back edge.",
275 id));
276 } else if (num_back_edges > 1) {
277 AddError(StringPrintf(
278 "Loop defined by header %d has several back edges: %zu.",
279 id,
280 num_back_edges));
David Brazdil2d7352b2015-04-20 14:52:42 +0100281 } else {
282 DCHECK_EQ(num_back_edges, 1u);
283 int back_edge_id = loop_header->GetLoopInformation()->GetBackEdges().Get(0)->GetBlockId();
284 if (!loop_blocks.IsBitSet(back_edge_id)) {
285 AddError(StringPrintf(
286 "Loop defined by header %d has an invalid back edge %d.",
287 id,
288 back_edge_id));
289 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100290 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100291
David Brazdil2d7352b2015-04-20 14:52:42 +0100292 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100293 for (uint32_t i : loop_blocks.Indexes()) {
294 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100295 if (loop_block == nullptr) {
296 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
297 id,
298 i));
299 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000300 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100301 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000302 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100303 }
304 }
David Brazdil7d275372015-04-21 16:36:35 +0100305
306 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
307 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
308 HLoopInformation* outer_info = it.Current();
309 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
310 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
311 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100312 id,
David Brazdil7d275372015-04-21 16:36:35 +0100313 outer_info->GetHeader()->GetBlockId()));
314 }
315 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100316}
317
318void SSAChecker::VisitInstruction(HInstruction* instruction) {
319 super_type::VisitInstruction(instruction);
320
Roland Levillaina8069ce2014-10-01 10:48:29 +0100321 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000322 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100323 !use_it.Done(); use_it.Advance()) {
324 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100325 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000326 AddError(StringPrintf("Instruction %d in block %d does not dominate "
327 "use %d in block %d.",
328 instruction->GetId(), current_block_->GetBlockId(),
329 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100330 }
331 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100332
333 // Ensure an instruction having an environment is dominated by the
334 // instructions contained in the environment.
335 HEnvironment* environment = instruction->GetEnvironment();
336 if (environment != nullptr) {
337 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
338 HInstruction* env_instruction = environment->GetInstructionAt(i);
339 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100340 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000341 AddError(StringPrintf("Instruction %d in environment of instruction %d "
342 "from block %d does not dominate instruction %d.",
343 env_instruction->GetId(),
344 instruction->GetId(),
345 current_block_->GetBlockId(),
346 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100347 }
348 }
349 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100350}
351
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352static Primitive::Type PrimitiveKind(Primitive::Type type) {
353 switch (type) {
354 case Primitive::kPrimBoolean:
355 case Primitive::kPrimByte:
356 case Primitive::kPrimShort:
357 case Primitive::kPrimChar:
358 case Primitive::kPrimInt:
359 return Primitive::kPrimInt;
360 default:
361 return type;
362 }
363}
364
Roland Levillain6b879dd2014-09-22 17:13:44 +0100365void SSAChecker::VisitPhi(HPhi* phi) {
366 VisitInstruction(phi);
367
368 // Ensure the first input of a phi is not itself.
369 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000370 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
371 phi->GetId(),
372 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100373 }
374
Roland Levillain5c4405e2015-01-21 11:39:58 +0000375 // Ensure the number of inputs of a phi is the same as the number of
Roland Levillain6b879dd2014-09-22 17:13:44 +0100376 // its predecessors.
377 const GrowableArray<HBasicBlock*>& predecessors =
378 phi->GetBlock()->GetPredecessors();
379 if (phi->InputCount() != predecessors.Size()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000380 AddError(StringPrintf(
381 "Phi %d in block %d has %zu inputs, "
382 "but block %d has %zu predecessors.",
383 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
384 phi->GetBlock()->GetBlockId(), predecessors.Size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100385 } else {
386 // Ensure phi input at index I either comes from the Ith
387 // predecessor or from a block that dominates this predecessor.
388 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
389 HInstruction* input = phi->InputAt(i);
390 HBasicBlock* predecessor = predecessors.Get(i);
391 if (!(input->GetBlock() == predecessor
392 || input->GetBlock()->Dominates(predecessor))) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000393 AddError(StringPrintf(
394 "Input %d at index %zu of phi %d from block %d is not defined in "
395 "predecessor number %zu nor in a block dominating it.",
396 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
397 i));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100398 }
399 }
400 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000401 // Ensure that the inputs have the same primitive kind as the phi.
402 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
403 HInstruction* input = phi->InputAt(i);
404 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
405 AddError(StringPrintf(
406 "Input %d at index %zu of phi %d from block %d does not have the "
407 "same type as the phi: %s versus %s",
408 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
409 Primitive::PrettyDescriptor(input->GetType()),
410 Primitive::PrettyDescriptor(phi->GetType())));
411 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000412 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000413 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
414 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
415 phi->GetId(),
416 phi->GetBlock()->GetBlockId(),
417 Primitive::PrettyDescriptor(phi->GetType())));
418 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000419}
420
David Brazdil13b47182015-04-15 16:29:32 +0100421void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
422 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000423 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100424 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000425 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000426 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100427 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
428 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000429 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100430 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000431 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000432 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100433 } else if (input->GetType() == Primitive::kPrimInt
434 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
435 // TODO: We need a data-flow analysis to determine if the Phi or
436 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100437 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000438 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100439 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
440 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000441 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100442 static_cast<int>(input_index),
443 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000444 }
445}
446
David Brazdil13b47182015-04-15 16:29:32 +0100447void SSAChecker::VisitIf(HIf* instruction) {
448 VisitInstruction(instruction);
449 HandleBooleanInput(instruction, 0);
450}
451
452void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
453 VisitInstruction(instruction);
454 HandleBooleanInput(instruction, 0);
455}
456
Nicolas Geoffray31596742014-11-24 15:28:45 +0000457void SSAChecker::VisitCondition(HCondition* op) {
458 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000459 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000460 AddError(StringPrintf(
461 "Condition %s %d has a non-Boolean result type: %s.",
462 op->DebugName(), op->GetId(),
463 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000464 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000465 HInstruction* lhs = op->InputAt(0);
466 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100467 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
468 AddError(StringPrintf(
469 "Condition %s %d has inputs of different types: %s, and %s.",
470 op->DebugName(), op->GetId(),
471 Primitive::PrettyDescriptor(lhs->GetType()),
472 Primitive::PrettyDescriptor(rhs->GetType())));
473 }
474 if (!op->IsEqual() && !op->IsNotEqual()) {
475 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000476 AddError(StringPrintf(
477 "Condition %s %d uses an object as left-hand side input.",
478 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100479 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000480 AddError(StringPrintf(
481 "Condition %s %d uses an object as right-hand side input.",
482 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000483 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000484 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000485}
486
487void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
488 VisitInstruction(op);
489 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
490 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000491 AddError(StringPrintf(
492 "Shift operation %s %d has a non-int kind second input: "
493 "%s of type %s.",
494 op->DebugName(), op->GetId(),
495 op->InputAt(1)->DebugName(),
496 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000497 }
498 } else {
499 if (PrimitiveKind(op->InputAt(1)->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000500 AddError(StringPrintf(
501 "Binary operation %s %d has inputs of different types: "
502 "%s, and %s.",
503 op->DebugName(), op->GetId(),
504 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
505 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000506 }
507 }
508
509 if (op->IsCompare()) {
510 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000511 AddError(StringPrintf(
512 "Compare operation %d has a non-int result type: %s.",
513 op->GetId(),
514 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000515 }
516 } else {
517 // Use the first input, so that we can also make this check for shift operations.
518 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000519 AddError(StringPrintf(
520 "Binary operation %s %d has a result type different "
521 "from its input type: %s vs %s.",
522 op->DebugName(), op->GetId(),
523 Primitive::PrettyDescriptor(op->GetType()),
524 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000525 }
526 }
527}
528
David Brazdil8d5b8b22015-03-24 10:51:52 +0000529void SSAChecker::VisitConstant(HConstant* instruction) {
530 HBasicBlock* block = instruction->GetBlock();
531 if (!block->IsEntryBlock()) {
532 AddError(StringPrintf(
533 "%s %d should be in the entry block but is in block %d.",
534 instruction->DebugName(),
535 instruction->GetId(),
536 block->GetBlockId()));
537 }
538}
539
Roland Levillainccc07a92014-09-16 14:48:16 +0100540} // namespace art