blob: d7e6bd816187a10794f270b1a1ccccdfd2c05839 [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 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
92 // Visit this block's list of phis.
93 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
David Brazdilc3d743f2015-04-22 13:40:50 +010094 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +010095 // Ensure this block's list of phis contains only phis.
David Brazdilc3d743f2015-04-22 13:40:50 +010096 if (!current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +000097 AddError(StringPrintf("Block %d has a non-phi in its phi list.",
98 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +010099 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100100 if (current->GetNext() == nullptr && current != block->GetLastPhi()) {
101 AddError(StringPrintf("The recorded last phi of block %d does not match "
102 "the actual last phi %d.",
103 current_block_->GetBlockId(),
104 current->GetId()));
105 }
106 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100107 }
108
109 // Visit this block's list of instructions.
David Brazdilc3d743f2015-04-22 13:40:50 +0100110 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
111 HInstruction* current = it.Current();
Roland Levillainccc07a92014-09-16 14:48:16 +0100112 // Ensure this block's list of instructions does not contains phis.
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 if (current->IsPhi()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000114 AddError(StringPrintf("Block %d has a phi in its non-phi list.",
115 current_block_->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100116 }
David Brazdilc3d743f2015-04-22 13:40:50 +0100117 if (current->GetNext() == nullptr && current != block->GetLastInstruction()) {
118 AddError(StringPrintf("The recorded last instruction of block %d does not match "
119 "the actual last instruction %d.",
120 current_block_->GetBlockId(),
121 current->GetId()));
122 }
123 current->Accept(this);
Roland Levillainccc07a92014-09-16 14:48:16 +0100124 }
125}
126
Mark Mendell1152c922015-04-24 17:06:35 -0400127void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) {
128 if (!GetGraph()->HasBoundsChecks()) {
129 AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, "
130 "but HasBoundsChecks() returns false",
131 check->DebugName(),
132 check->GetId()));
133 }
134
135 // Perform the instruction base checks too.
136 VisitInstruction(check);
137}
138
Roland Levillainccc07a92014-09-16 14:48:16 +0100139void GraphChecker::VisitInstruction(HInstruction* instruction) {
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000140 if (seen_ids_.IsBitSet(instruction->GetId())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000141 AddError(StringPrintf("Instruction id %d is duplicate in graph.",
142 instruction->GetId()));
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000143 } else {
144 seen_ids_.SetBit(instruction->GetId());
145 }
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Ensure `instruction` is associated with `current_block_`.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000148 if (instruction->GetBlock() == nullptr) {
149 AddError(StringPrintf("%s %d in block %d not associated with any block.",
150 instruction->IsPhi() ? "Phi" : "Instruction",
151 instruction->GetId(),
152 current_block_->GetBlockId()));
153 } else if (instruction->GetBlock() != current_block_) {
154 AddError(StringPrintf("%s %d in block %d associated with block %d.",
155 instruction->IsPhi() ? "Phi" : "Instruction",
156 instruction->GetId(),
157 current_block_->GetBlockId(),
158 instruction->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100159 }
Roland Levillain6b469232014-09-25 10:10:38 +0100160
161 // Ensure the inputs of `instruction` are defined in a block of the graph.
162 for (HInputIterator input_it(instruction); !input_it.Done();
163 input_it.Advance()) {
164 HInstruction* input = input_it.Current();
165 const HInstructionList& list = input->IsPhi()
166 ? input->GetBlock()->GetPhis()
167 : input->GetBlock()->GetInstructions();
168 if (!list.Contains(input)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000169 AddError(StringPrintf("Input %d of instruction %d is not defined "
170 "in a basic block of the control-flow graph.",
171 input->GetId(),
172 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100173 }
174 }
175
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100176 // Ensure the uses of `instruction` are defined in a block of the graph,
177 // and the entry in the use list is consistent.
David Brazdiled596192015-01-23 10:39:45 +0000178 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillain6b469232014-09-25 10:10:38 +0100179 !use_it.Done(); use_it.Advance()) {
180 HInstruction* use = use_it.Current()->GetUser();
181 const HInstructionList& list = use->IsPhi()
182 ? use->GetBlock()->GetPhis()
183 : use->GetBlock()->GetInstructions();
184 if (!list.Contains(use)) {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000185 AddError(StringPrintf("User %s:%d of instruction %d is not defined "
Roland Levillain5c4405e2015-01-21 11:39:58 +0000186 "in a basic block of the control-flow graph.",
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000187 use->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000188 use->GetId(),
189 instruction->GetId()));
Roland Levillain6b469232014-09-25 10:10:38 +0100190 }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100191 size_t use_index = use_it.Current()->GetIndex();
192 if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) {
193 AddError(StringPrintf("User %s:%d of instruction %d has a wrong "
194 "UseListNode index.",
195 use->DebugName(),
196 use->GetId(),
197 instruction->GetId()));
198 }
199 }
200
201 // Ensure the environment uses entries are consistent.
202 for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses());
203 !use_it.Done(); use_it.Advance()) {
204 HEnvironment* use = use_it.Current()->GetUser();
205 size_t use_index = use_it.Current()->GetIndex();
206 if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) {
207 AddError(StringPrintf("Environment user of %s:%d has a wrong "
208 "UseListNode index.",
209 instruction->DebugName(),
210 instruction->GetId()));
211 }
Roland Levillain6b469232014-09-25 10:10:38 +0100212 }
David Brazdil1abb4192015-02-17 18:33:36 +0000213
214 // Ensure 'instruction' has pointers to its inputs' use entries.
215 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
216 HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i);
217 HInstruction* input = input_record.GetInstruction();
218 HUseListNode<HInstruction*>* use_node = input_record.GetUseNode();
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100219 size_t use_index = use_node->GetIndex();
220 if ((use_node == nullptr)
221 || !input->GetUses().Contains(use_node)
222 || (use_index >= e)
223 || (use_index != i)) {
David Brazdil1abb4192015-02-17 18:33:36 +0000224 AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry "
225 "at input %u (%s:%d).",
226 instruction->DebugName(),
227 instruction->GetId(),
228 static_cast<unsigned>(i),
229 input->DebugName(),
230 input->GetId()));
231 }
232 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100233}
234
Roland Levillain4c0eb422015-04-24 16:43:49 +0100235void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
236 VisitInstruction(invoke);
237
238 if (invoke->IsStaticWithExplicitClinitCheck()) {
239 size_t last_input_index = invoke->InputCount() - 1;
240 HInstruction* last_input = invoke->InputAt(last_input_index);
241 if (last_input == nullptr) {
242 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
243 "has a null pointer as last input.",
244 invoke->DebugName(),
245 invoke->GetId()));
246 }
247 if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) {
248 AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check "
249 "has a last instruction (%s:%d) which is neither a clinit check "
250 "nor a load class instruction.",
251 invoke->DebugName(),
252 invoke->GetId(),
253 last_input->DebugName(),
254 last_input->GetId()));
255 }
256 }
257}
258
David Brazdilfc6a86a2015-06-26 10:33:45 +0000259void GraphChecker::VisitReturn(HReturn* ret) {
260 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
261 AddError(StringPrintf("%s:%d does not jump to the exit block.",
262 ret->DebugName(),
263 ret->GetId()));
264 }
265}
266
267void GraphChecker::VisitReturnVoid(HReturnVoid* ret) {
268 if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) {
269 AddError(StringPrintf("%s:%d does not jump to the exit block.",
270 ret->DebugName(),
271 ret->GetId()));
272 }
273}
274
Roland Levillainccc07a92014-09-16 14:48:16 +0100275void SSAChecker::VisitBasicBlock(HBasicBlock* block) {
276 super_type::VisitBasicBlock(block);
277
278 // Ensure there is no critical edge (i.e., an edge connecting a
279 // block with multiple successors to a block with multiple
280 // predecessors).
281 if (block->GetSuccessors().Size() > 1) {
282 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
283 HBasicBlock* successor = block->GetSuccessors().Get(j);
284 if (successor->GetPredecessors().Size() > 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000285 AddError(StringPrintf("Critical edge between blocks %d and %d.",
286 block->GetBlockId(),
287 successor->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100288 }
289 }
290 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100291
Calin Juravle1d8b49f2015-05-12 12:40:07 +0000292 // Check Phi uniqueness (no two Phis with the same type refer to the same register).
Calin Juravlea4f88312015-04-16 12:57:19 +0100293 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
294 HPhi* phi = it.Current()->AsPhi();
295 if (phi->GetNextEquivalentPhiWithSameType() != nullptr) {
296 std::stringstream type_str;
297 type_str << phi->GetType();
298 AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s",
299 phi->GetId(), phi->GetRegNumber(), type_str.str().c_str()));
300 }
301 }
302
Roland Levillain6b879dd2014-09-22 17:13:44 +0100303 if (block->IsLoopHeader()) {
304 CheckLoop(block);
305 }
306}
307
308void SSAChecker::CheckLoop(HBasicBlock* loop_header) {
309 int id = loop_header->GetBlockId();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100310 HLoopInformation* loop_information = loop_header->GetLoopInformation();
Roland Levillain6b879dd2014-09-22 17:13:44 +0100311
312 // Ensure the pre-header block is first in the list of
313 // predecessors of a loop header.
314 if (!loop_header->IsLoopPreHeaderFirstPredecessor()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000315 AddError(StringPrintf(
316 "Loop pre-header is not the first predecessor of the loop header %d.",
317 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100318 }
319
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100320 // Ensure the loop header has only one incoming branch and the remaining
321 // predecessors are back edges.
Roland Levillain5c4405e2015-01-21 11:39:58 +0000322 size_t num_preds = loop_header->GetPredecessors().Size();
323 if (num_preds < 2) {
324 AddError(StringPrintf(
325 "Loop header %d has less than two predecessors: %zu.",
326 id,
327 num_preds));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100328 } else {
Roland Levillain6b879dd2014-09-22 17:13:44 +0100329 HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0);
David Brazdil46e2a392015-03-16 17:31:52 +0000330 if (loop_information->IsBackEdge(*first_predecessor)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000331 AddError(StringPrintf(
332 "First predecessor of loop header %d is a back edge.",
333 id));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100334 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100335 for (size_t i = 1, e = loop_header->GetPredecessors().Size(); i < e; ++i) {
336 HBasicBlock* predecessor = loop_header->GetPredecessors().Get(i);
337 if (!loop_information->IsBackEdge(*predecessor)) {
338 AddError(StringPrintf(
339 "Loop header %d has multiple incoming (non back edge) blocks.",
340 id));
341 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100342 }
343 }
344
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100345 const ArenaBitVector& loop_blocks = loop_information->GetBlocks();
David Brazdil2d7352b2015-04-20 14:52:42 +0100346
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100347 // Ensure back edges belong to the loop.
348 size_t num_back_edges = loop_information->GetBackEdges().Size();
Roland Levillain5c4405e2015-01-21 11:39:58 +0000349 if (num_back_edges == 0) {
350 AddError(StringPrintf(
351 "Loop defined by header %d has no back edge.",
352 id));
David Brazdil2d7352b2015-04-20 14:52:42 +0100353 } else {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100354 for (size_t i = 0; i < num_back_edges; ++i) {
355 int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId();
356 if (!loop_blocks.IsBitSet(back_edge_id)) {
357 AddError(StringPrintf(
358 "Loop defined by header %d has an invalid back edge %d.",
359 id,
360 back_edge_id));
361 }
David Brazdil2d7352b2015-04-20 14:52:42 +0100362 }
Roland Levillain6b879dd2014-09-22 17:13:44 +0100363 }
Roland Levillain7e53b412014-09-23 10:50:22 +0100364
David Brazdil2d7352b2015-04-20 14:52:42 +0100365 // Ensure all blocks in the loop are live and dominated by the loop header.
Roland Levillain7e53b412014-09-23 10:50:22 +0100366 for (uint32_t i : loop_blocks.Indexes()) {
367 HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i);
David Brazdil2d7352b2015-04-20 14:52:42 +0100368 if (loop_block == nullptr) {
369 AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.",
370 id,
371 i));
372 } else if (!loop_header->Dominates(loop_block)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000373 AddError(StringPrintf("Loop block %d not dominated by loop header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100374 i,
Roland Levillain5c4405e2015-01-21 11:39:58 +0000375 id));
Roland Levillain7e53b412014-09-23 10:50:22 +0100376 }
377 }
David Brazdil7d275372015-04-21 16:36:35 +0100378
379 // If this is a nested loop, ensure the outer loops contain a superset of the blocks.
380 for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) {
381 HLoopInformation* outer_info = it.Current();
382 if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) {
383 AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of "
384 "an outer loop defined by header %d.",
David Brazdil2d7352b2015-04-20 14:52:42 +0100385 id,
David Brazdil7d275372015-04-21 16:36:35 +0100386 outer_info->GetHeader()->GetBlockId()));
387 }
388 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100389}
390
391void SSAChecker::VisitInstruction(HInstruction* instruction) {
392 super_type::VisitInstruction(instruction);
393
Roland Levillaina8069ce2014-10-01 10:48:29 +0100394 // Ensure an instruction dominates all its uses.
David Brazdiled596192015-01-23 10:39:45 +0000395 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
Roland Levillaina8069ce2014-10-01 10:48:29 +0100396 !use_it.Done(); use_it.Advance()) {
397 HInstruction* use = use_it.Current()->GetUser();
Roland Levillain6c82d402014-10-13 16:10:27 +0100398 if (!use->IsPhi() && !instruction->StrictlyDominates(use)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000399 AddError(StringPrintf("Instruction %d in block %d does not dominate "
400 "use %d in block %d.",
401 instruction->GetId(), current_block_->GetBlockId(),
402 use->GetId(), use->GetBlock()->GetBlockId()));
Roland Levillainccc07a92014-09-16 14:48:16 +0100403 }
404 }
Roland Levillaina8069ce2014-10-01 10:48:29 +0100405
406 // Ensure an instruction having an environment is dominated by the
407 // instructions contained in the environment.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100408 for (HEnvironment* environment = instruction->GetEnvironment();
409 environment != nullptr;
410 environment = environment->GetParent()) {
Roland Levillaina8069ce2014-10-01 10:48:29 +0100411 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
412 HInstruction* env_instruction = environment->GetInstructionAt(i);
413 if (env_instruction != nullptr
Roland Levillain6c82d402014-10-13 16:10:27 +0100414 && !env_instruction->StrictlyDominates(instruction)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000415 AddError(StringPrintf("Instruction %d in environment of instruction %d "
416 "from block %d does not dominate instruction %d.",
417 env_instruction->GetId(),
418 instruction->GetId(),
419 current_block_->GetBlockId(),
420 instruction->GetId()));
Roland Levillaina8069ce2014-10-01 10:48:29 +0100421 }
422 }
423 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100424}
425
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000426static Primitive::Type PrimitiveKind(Primitive::Type type) {
427 switch (type) {
428 case Primitive::kPrimBoolean:
429 case Primitive::kPrimByte:
430 case Primitive::kPrimShort:
431 case Primitive::kPrimChar:
432 case Primitive::kPrimInt:
433 return Primitive::kPrimInt;
434 default:
435 return type;
436 }
437}
438
Roland Levillain6b879dd2014-09-22 17:13:44 +0100439void SSAChecker::VisitPhi(HPhi* phi) {
440 VisitInstruction(phi);
441
442 // Ensure the first input of a phi is not itself.
443 if (phi->InputAt(0) == phi) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000444 AddError(StringPrintf("Loop phi %d in block %d is its own first input.",
445 phi->GetId(),
446 phi->GetBlock()->GetBlockId()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100447 }
448
Roland Levillain5c4405e2015-01-21 11:39:58 +0000449 // Ensure the number of inputs of a phi is the same as the number of
Roland Levillain6b879dd2014-09-22 17:13:44 +0100450 // its predecessors.
451 const GrowableArray<HBasicBlock*>& predecessors =
452 phi->GetBlock()->GetPredecessors();
453 if (phi->InputCount() != predecessors.Size()) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000454 AddError(StringPrintf(
455 "Phi %d in block %d has %zu inputs, "
456 "but block %d has %zu predecessors.",
457 phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(),
458 phi->GetBlock()->GetBlockId(), predecessors.Size()));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100459 } else {
460 // Ensure phi input at index I either comes from the Ith
461 // predecessor or from a block that dominates this predecessor.
462 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
463 HInstruction* input = phi->InputAt(i);
464 HBasicBlock* predecessor = predecessors.Get(i);
465 if (!(input->GetBlock() == predecessor
466 || input->GetBlock()->Dominates(predecessor))) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000467 AddError(StringPrintf(
468 "Input %d at index %zu of phi %d from block %d is not defined in "
469 "predecessor number %zu nor in a block dominating it.",
470 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
471 i));
Roland Levillain6b879dd2014-09-22 17:13:44 +0100472 }
473 }
474 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000475 // Ensure that the inputs have the same primitive kind as the phi.
476 for (size_t i = 0, e = phi->InputCount(); i < e; ++i) {
477 HInstruction* input = phi->InputAt(i);
478 if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) {
479 AddError(StringPrintf(
480 "Input %d at index %zu of phi %d from block %d does not have the "
481 "same type as the phi: %s versus %s",
482 input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(),
483 Primitive::PrettyDescriptor(input->GetType()),
484 Primitive::PrettyDescriptor(phi->GetType())));
485 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000486 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000487 if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) {
488 AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s",
489 phi->GetId(),
490 phi->GetBlock()->GetBlockId(),
491 Primitive::PrettyDescriptor(phi->GetType())));
492 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000493}
494
David Brazdil13b47182015-04-15 16:29:32 +0100495void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) {
496 HInstruction* input = instruction->InputAt(input_index);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000497 if (input->IsIntConstant()) {
David Brazdil13b47182015-04-15 16:29:32 +0100498 int32_t value = input->AsIntConstant()->GetValue();
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000499 if (value != 0 && value != 1) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000500 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100501 "%s instruction %d has a non-Boolean constant input %d whose value is: %d.",
502 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000503 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100504 static_cast<int>(input_index),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000505 value));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000506 }
David Brazdil2fa194b2015-04-20 10:14:42 +0100507 } else if (input->GetType() == Primitive::kPrimInt
508 && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) {
509 // TODO: We need a data-flow analysis to determine if the Phi or
510 // binary operation is actually Boolean. Allow for now.
David Brazdil13b47182015-04-15 16:29:32 +0100511 } else if (input->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000512 AddError(StringPrintf(
David Brazdil13b47182015-04-15 16:29:32 +0100513 "%s instruction %d has a non-Boolean input %d whose type is: %s.",
514 instruction->DebugName(),
Roland Levillain5c4405e2015-01-21 11:39:58 +0000515 instruction->GetId(),
David Brazdil13b47182015-04-15 16:29:32 +0100516 static_cast<int>(input_index),
517 Primitive::PrettyDescriptor(input->GetType())));
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000518 }
519}
520
David Brazdil13b47182015-04-15 16:29:32 +0100521void SSAChecker::VisitIf(HIf* instruction) {
522 VisitInstruction(instruction);
523 HandleBooleanInput(instruction, 0);
524}
525
526void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) {
527 VisitInstruction(instruction);
528 HandleBooleanInput(instruction, 0);
529}
530
Nicolas Geoffray31596742014-11-24 15:28:45 +0000531void SSAChecker::VisitCondition(HCondition* op) {
532 VisitInstruction(op);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000533 if (op->GetType() != Primitive::kPrimBoolean) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000534 AddError(StringPrintf(
535 "Condition %s %d has a non-Boolean result type: %s.",
536 op->DebugName(), op->GetId(),
537 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000538 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000539 HInstruction* lhs = op->InputAt(0);
540 HInstruction* rhs = op->InputAt(1);
Calin Juravlea4f88312015-04-16 12:57:19 +0100541 if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) {
542 AddError(StringPrintf(
543 "Condition %s %d has inputs of different types: %s, and %s.",
544 op->DebugName(), op->GetId(),
545 Primitive::PrettyDescriptor(lhs->GetType()),
546 Primitive::PrettyDescriptor(rhs->GetType())));
547 }
548 if (!op->IsEqual() && !op->IsNotEqual()) {
549 if ((lhs->GetType() == Primitive::kPrimNot)) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000550 AddError(StringPrintf(
551 "Condition %s %d uses an object as left-hand side input.",
552 op->DebugName(), op->GetId()));
Calin Juravlea4f88312015-04-16 12:57:19 +0100553 } else if (rhs->GetType() == Primitive::kPrimNot) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000554 AddError(StringPrintf(
555 "Condition %s %d uses an object as right-hand side input.",
556 op->DebugName(), op->GetId()));
Roland Levillainaecbd262015-01-19 12:44:01 +0000557 }
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000558 }
Nicolas Geoffray31596742014-11-24 15:28:45 +0000559}
560
561void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) {
562 VisitInstruction(op);
563 if (op->IsUShr() || op->IsShr() || op->IsShl()) {
564 if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000565 AddError(StringPrintf(
566 "Shift operation %s %d has a non-int kind second input: "
567 "%s of type %s.",
568 op->DebugName(), op->GetId(),
569 op->InputAt(1)->DebugName(),
570 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000571 }
572 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100573 if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000574 AddError(StringPrintf(
575 "Binary operation %s %d has inputs of different types: "
576 "%s, and %s.",
577 op->DebugName(), op->GetId(),
578 Primitive::PrettyDescriptor(op->InputAt(0)->GetType()),
579 Primitive::PrettyDescriptor(op->InputAt(1)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000580 }
581 }
582
583 if (op->IsCompare()) {
584 if (op->GetType() != Primitive::kPrimInt) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000585 AddError(StringPrintf(
586 "Compare operation %d has a non-int result type: %s.",
587 op->GetId(),
588 Primitive::PrettyDescriptor(op->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000589 }
590 } else {
591 // Use the first input, so that we can also make this check for shift operations.
592 if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) {
Roland Levillain5c4405e2015-01-21 11:39:58 +0000593 AddError(StringPrintf(
594 "Binary operation %s %d has a result type different "
595 "from its input type: %s vs %s.",
596 op->DebugName(), op->GetId(),
597 Primitive::PrettyDescriptor(op->GetType()),
Roland Levillain4c0eb422015-04-24 16:43:49 +0100598 Primitive::PrettyDescriptor(op->InputAt(0)->GetType())));
Nicolas Geoffray31596742014-11-24 15:28:45 +0000599 }
600 }
601}
602
David Brazdil8d5b8b22015-03-24 10:51:52 +0000603void SSAChecker::VisitConstant(HConstant* instruction) {
604 HBasicBlock* block = instruction->GetBlock();
605 if (!block->IsEntryBlock()) {
606 AddError(StringPrintf(
607 "%s %d should be in the entry block but is in block %d.",
608 instruction->DebugName(),
609 instruction->GetId(),
610 block->GetBlockId()));
611 }
612}
613
Roland Levillainccc07a92014-09-16 14:48:16 +0100614} // namespace art