Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 19 | #include <map> |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 20 | #include <string> |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 21 | #include <sstream> |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 22 | |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 23 | #include "base/bit_vector-inl.h" |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 24 | #include "base/stringprintf.h" |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 25 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | void GraphChecker::VisitBasicBlock(HBasicBlock* block) { |
| 29 | current_block_ = block; |
| 30 | |
| 31 | // Check consistency with respect to predecessors of `block`. |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 32 | std::map<HBasicBlock*, size_t> predecessors_count; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 33 | for (HBasicBlock* p : block->GetPredecessors()) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 34 | ++predecessors_count[p]; |
| 35 | } |
| 36 | for (auto& pc : predecessors_count) { |
| 37 | HBasicBlock* p = pc.first; |
| 38 | size_t p_count_in_block_predecessors = pc.second; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 39 | size_t block_count_in_p_successors = 0; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 40 | for (HBasicBlock* p_successor : p->GetSuccessors()) { |
| 41 | if (p_successor == block) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 42 | ++block_count_in_p_successors; |
| 43 | } |
| 44 | } |
| 45 | if (p_count_in_block_predecessors != block_count_in_p_successors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 46 | AddError(StringPrintf( |
| 47 | "Block %d lists %zu occurrences of block %d in its predecessors, whereas " |
| 48 | "block %d lists %zu occurrences of block %d in its successors.", |
| 49 | block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(), |
| 50 | p->GetBlockId(), block_count_in_p_successors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | // Check consistency with respect to successors of `block`. |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 55 | std::map<HBasicBlock*, size_t> successors_count; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 56 | for (HBasicBlock* s : block->GetSuccessors()) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 57 | ++successors_count[s]; |
| 58 | } |
| 59 | for (auto& sc : successors_count) { |
| 60 | HBasicBlock* s = sc.first; |
| 61 | size_t s_count_in_block_successors = sc.second; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 62 | size_t block_count_in_s_predecessors = 0; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 63 | for (HBasicBlock* s_predecessor : s->GetPredecessors()) { |
| 64 | if (s_predecessor == block) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 65 | ++block_count_in_s_predecessors; |
| 66 | } |
| 67 | } |
| 68 | if (s_count_in_block_successors != block_count_in_s_predecessors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 69 | AddError(StringPrintf( |
| 70 | "Block %d lists %zu occurrences of block %d in its successors, whereas " |
| 71 | "block %d lists %zu occurrences of block %d in its predecessors.", |
| 72 | block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(), |
| 73 | s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | // Ensure `block` ends with a branch instruction. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 78 | // This invariant is not enforced on non-SSA graphs. Graph built from DEX with |
| 79 | // dead code that falls out of the method will not end with a control-flow |
| 80 | // instruction. Such code is removed during the SSA-building DCE phase. |
| 81 | if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 82 | AddError(StringPrintf("Block %d does not end with a branch instruction.", |
| 83 | block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 84 | } |
| 85 | |
David Brazdil | 29fc008 | 2015-08-18 17:17:38 +0100 | [diff] [blame] | 86 | // Ensure that only Return(Void) and Throw jump to Exit. An exiting |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 87 | // TryBoundary may be between a Throw and the Exit if the Throw is in a try. |
| 88 | if (block->IsExitBlock()) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 89 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 90 | if (predecessor->IsSingleTryBoundary() |
| 91 | && !predecessor->GetLastInstruction()->AsTryBoundary()->IsEntry()) { |
| 92 | HBasicBlock* real_predecessor = predecessor->GetSinglePredecessor(); |
| 93 | HInstruction* last_instruction = real_predecessor->GetLastInstruction(); |
| 94 | if (!last_instruction->IsThrow()) { |
| 95 | AddError(StringPrintf("Unexpected TryBoundary between %s:%d and Exit.", |
| 96 | last_instruction->DebugName(), |
| 97 | last_instruction->GetId())); |
| 98 | } |
| 99 | } else { |
| 100 | HInstruction* last_instruction = predecessor->GetLastInstruction(); |
| 101 | if (!last_instruction->IsReturn() |
| 102 | && !last_instruction->IsReturnVoid() |
| 103 | && !last_instruction->IsThrow()) { |
| 104 | AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.", |
| 105 | last_instruction->DebugName(), |
| 106 | last_instruction->GetId())); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 112 | // Visit this block's list of phis. |
| 113 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 114 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 115 | // Ensure this block's list of phis contains only phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 116 | if (!current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 117 | AddError(StringPrintf("Block %d has a non-phi in its phi list.", |
| 118 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 119 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 120 | if (current->GetNext() == nullptr && current != block->GetLastPhi()) { |
| 121 | AddError(StringPrintf("The recorded last phi of block %d does not match " |
| 122 | "the actual last phi %d.", |
| 123 | current_block_->GetBlockId(), |
| 124 | current->GetId())); |
| 125 | } |
| 126 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Visit this block's list of instructions. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 130 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 131 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 132 | // Ensure this block's list of instructions does not contains phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 133 | if (current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 134 | AddError(StringPrintf("Block %d has a phi in its non-phi list.", |
| 135 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 136 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 137 | if (current->GetNext() == nullptr && current != block->GetLastInstruction()) { |
| 138 | AddError(StringPrintf("The recorded last instruction of block %d does not match " |
| 139 | "the actual last instruction %d.", |
| 140 | current_block_->GetBlockId(), |
| 141 | current->GetId())); |
| 142 | } |
| 143 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 147 | void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) { |
| 148 | if (!GetGraph()->HasBoundsChecks()) { |
| 149 | AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, " |
| 150 | "but HasBoundsChecks() returns false", |
| 151 | check->DebugName(), |
| 152 | check->GetId())); |
| 153 | } |
| 154 | |
| 155 | // Perform the instruction base checks too. |
| 156 | VisitInstruction(check); |
| 157 | } |
| 158 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 159 | void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) { |
| 160 | // Ensure that all exception handlers are catch blocks and that handlers |
| 161 | // are not listed multiple times. |
| 162 | // Note that a normal-flow successor may be a catch block before CFG |
| 163 | // simplification. We only test normal-flow successors in SsaChecker. |
| 164 | for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) { |
| 165 | HBasicBlock* handler = it.Current(); |
| 166 | if (!handler->IsCatchBlock()) { |
| 167 | AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which " |
| 168 | "is not a catch block.", |
| 169 | current_block_->GetBlockId(), |
| 170 | try_boundary->DebugName(), |
| 171 | try_boundary->GetId(), |
| 172 | handler->GetBlockId())); |
| 173 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 174 | if (current_block_->HasSuccessor(handler, it.CurrentSuccessorIndex() + 1)) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 175 | AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.", |
| 176 | handler->GetBlockId(), |
| 177 | try_boundary->DebugName(), |
| 178 | try_boundary->GetId())); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | VisitInstruction(try_boundary); |
| 183 | } |
| 184 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 185 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 186 | if (seen_ids_.IsBitSet(instruction->GetId())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 187 | AddError(StringPrintf("Instruction id %d is duplicate in graph.", |
| 188 | instruction->GetId())); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 189 | } else { |
| 190 | seen_ids_.SetBit(instruction->GetId()); |
| 191 | } |
| 192 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 193 | // Ensure `instruction` is associated with `current_block_`. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 194 | if (instruction->GetBlock() == nullptr) { |
| 195 | AddError(StringPrintf("%s %d in block %d not associated with any block.", |
| 196 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 197 | instruction->GetId(), |
| 198 | current_block_->GetBlockId())); |
| 199 | } else if (instruction->GetBlock() != current_block_) { |
| 200 | AddError(StringPrintf("%s %d in block %d associated with block %d.", |
| 201 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 202 | instruction->GetId(), |
| 203 | current_block_->GetBlockId(), |
| 204 | instruction->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 205 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 206 | |
| 207 | // Ensure the inputs of `instruction` are defined in a block of the graph. |
| 208 | for (HInputIterator input_it(instruction); !input_it.Done(); |
| 209 | input_it.Advance()) { |
| 210 | HInstruction* input = input_it.Current(); |
| 211 | const HInstructionList& list = input->IsPhi() |
| 212 | ? input->GetBlock()->GetPhis() |
| 213 | : input->GetBlock()->GetInstructions(); |
| 214 | if (!list.Contains(input)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 215 | AddError(StringPrintf("Input %d of instruction %d is not defined " |
| 216 | "in a basic block of the control-flow graph.", |
| 217 | input->GetId(), |
| 218 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 222 | // Ensure the uses of `instruction` are defined in a block of the graph, |
| 223 | // and the entry in the use list is consistent. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 224 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 225 | !use_it.Done(); use_it.Advance()) { |
| 226 | HInstruction* use = use_it.Current()->GetUser(); |
| 227 | const HInstructionList& list = use->IsPhi() |
| 228 | ? use->GetBlock()->GetPhis() |
| 229 | : use->GetBlock()->GetInstructions(); |
| 230 | if (!list.Contains(use)) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 231 | AddError(StringPrintf("User %s:%d of instruction %d is not defined " |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 232 | "in a basic block of the control-flow graph.", |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 233 | use->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 234 | use->GetId(), |
| 235 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 236 | } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 237 | size_t use_index = use_it.Current()->GetIndex(); |
| 238 | if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) { |
| 239 | AddError(StringPrintf("User %s:%d of instruction %d has a wrong " |
| 240 | "UseListNode index.", |
| 241 | use->DebugName(), |
| 242 | use->GetId(), |
| 243 | instruction->GetId())); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Ensure the environment uses entries are consistent. |
| 248 | for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses()); |
| 249 | !use_it.Done(); use_it.Advance()) { |
| 250 | HEnvironment* use = use_it.Current()->GetUser(); |
| 251 | size_t use_index = use_it.Current()->GetIndex(); |
| 252 | if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) { |
| 253 | AddError(StringPrintf("Environment user of %s:%d has a wrong " |
| 254 | "UseListNode index.", |
| 255 | instruction->DebugName(), |
| 256 | instruction->GetId())); |
| 257 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 258 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 259 | |
| 260 | // Ensure 'instruction' has pointers to its inputs' use entries. |
| 261 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 262 | HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i); |
| 263 | HInstruction* input = input_record.GetInstruction(); |
| 264 | HUseListNode<HInstruction*>* use_node = input_record.GetUseNode(); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 265 | size_t use_index = use_node->GetIndex(); |
| 266 | if ((use_node == nullptr) |
| 267 | || !input->GetUses().Contains(use_node) |
| 268 | || (use_index >= e) |
| 269 | || (use_index != i)) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 270 | AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry " |
| 271 | "at input %u (%s:%d).", |
| 272 | instruction->DebugName(), |
| 273 | instruction->GetId(), |
| 274 | static_cast<unsigned>(i), |
| 275 | input->DebugName(), |
| 276 | input->GetId())); |
| 277 | } |
| 278 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 281 | void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
| 282 | VisitInstruction(invoke); |
| 283 | |
| 284 | if (invoke->IsStaticWithExplicitClinitCheck()) { |
| 285 | size_t last_input_index = invoke->InputCount() - 1; |
| 286 | HInstruction* last_input = invoke->InputAt(last_input_index); |
| 287 | if (last_input == nullptr) { |
| 288 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 289 | "has a null pointer as last input.", |
| 290 | invoke->DebugName(), |
| 291 | invoke->GetId())); |
| 292 | } |
| 293 | if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) { |
| 294 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 295 | "has a last instruction (%s:%d) which is neither a clinit check " |
| 296 | "nor a load class instruction.", |
| 297 | invoke->DebugName(), |
| 298 | invoke->GetId(), |
| 299 | last_input->DebugName(), |
| 300 | last_input->GetId())); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 305 | void GraphChecker::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 306 | VisitInstruction(ret); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 307 | if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) { |
| 308 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 309 | ret->DebugName(), |
| 310 | ret->GetId())); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void GraphChecker::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 315 | VisitInstruction(ret); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 316 | if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) { |
| 317 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 318 | ret->DebugName(), |
| 319 | ret->GetId())); |
| 320 | } |
| 321 | } |
| 322 | |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 323 | void GraphChecker::VisitCheckCast(HCheckCast* check) { |
| 324 | VisitInstruction(check); |
| 325 | HInstruction* input = check->InputAt(1); |
| 326 | if (!input->IsLoadClass()) { |
| 327 | AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.", |
| 328 | check->DebugName(), |
| 329 | check->GetId(), |
| 330 | input->DebugName(), |
| 331 | input->GetId())); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) { |
| 336 | VisitInstruction(instruction); |
| 337 | HInstruction* input = instruction->InputAt(1); |
| 338 | if (!input->IsLoadClass()) { |
| 339 | AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.", |
| 340 | instruction->DebugName(), |
| 341 | instruction->GetId(), |
| 342 | input->DebugName(), |
| 343 | input->GetId())); |
| 344 | } |
| 345 | } |
| 346 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 347 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 348 | super_type::VisitBasicBlock(block); |
| 349 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 350 | // Ensure that catch blocks are not normal successors, and normal blocks are |
| 351 | // never exceptional successors. |
| 352 | const size_t num_normal_successors = block->NumberOfNormalSuccessors(); |
| 353 | for (size_t j = 0; j < num_normal_successors; ++j) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 354 | HBasicBlock* successor = block->GetSuccessor(j); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 355 | if (successor->IsCatchBlock()) { |
| 356 | AddError(StringPrintf("Catch block %d is a normal successor of block %d.", |
| 357 | successor->GetBlockId(), |
| 358 | block->GetBlockId())); |
| 359 | } |
| 360 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 361 | for (size_t j = num_normal_successors, e = block->GetSuccessors().size(); j < e; ++j) { |
| 362 | HBasicBlock* successor = block->GetSuccessor(j); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 363 | if (!successor->IsCatchBlock()) { |
| 364 | AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.", |
| 365 | successor->GetBlockId(), |
| 366 | block->GetBlockId())); |
| 367 | } |
| 368 | } |
| 369 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 370 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 371 | // block with multiple successors to a block with multiple |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 372 | // predecessors). Exceptional edges are synthesized and hence |
| 373 | // not accounted for. |
| 374 | if (block->NumberOfNormalSuccessors() > 1) { |
| 375 | for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 376 | HBasicBlock* successor = block->GetSuccessor(j); |
| 377 | if (successor->GetPredecessors().size() > 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 378 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 379 | block->GetBlockId(), |
| 380 | successor->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 384 | |
Calin Juravle | 1d8b49f | 2015-05-12 12:40:07 +0000 | [diff] [blame] | 385 | // Check Phi uniqueness (no two Phis with the same type refer to the same register). |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 386 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 387 | HPhi* phi = it.Current()->AsPhi(); |
| 388 | if (phi->GetNextEquivalentPhiWithSameType() != nullptr) { |
| 389 | std::stringstream type_str; |
| 390 | type_str << phi->GetType(); |
| 391 | AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s", |
| 392 | phi->GetId(), phi->GetRegNumber(), type_str.str().c_str())); |
| 393 | } |
| 394 | } |
| 395 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 396 | // Ensure try membership information is consistent. |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 397 | if (block->IsCatchBlock()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 398 | if (block->IsTryBlock()) { |
| 399 | const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 400 | AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d " |
| 401 | "has try entry %s:%d.", |
| 402 | block->GetBlockId(), |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 403 | try_entry.DebugName(), |
| 404 | try_entry.GetId())); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (block->IsLoopHeader()) { |
| 408 | AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.", |
| 409 | block->GetBlockId())); |
| 410 | } |
| 411 | } else { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 412 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 413 | const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors(); |
| 414 | if (block->IsTryBlock()) { |
| 415 | const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
| 416 | if (incoming_try_entry == nullptr) { |
| 417 | AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows " |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 418 | "from predecessor %d.", |
| 419 | block->GetBlockId(), |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 420 | stored_try_entry.DebugName(), |
| 421 | stored_try_entry.GetId(), |
| 422 | predecessor->GetBlockId())); |
| 423 | } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) { |
| 424 | AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent " |
| 425 | "with %s:%d that follows from predecessor %d.", |
| 426 | block->GetBlockId(), |
| 427 | stored_try_entry.DebugName(), |
| 428 | stored_try_entry.GetId(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 429 | incoming_try_entry->DebugName(), |
| 430 | incoming_try_entry->GetId(), |
| 431 | predecessor->GetBlockId())); |
| 432 | } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 433 | } else if (incoming_try_entry != nullptr) { |
| 434 | AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows " |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 435 | "from predecessor %d.", |
| 436 | block->GetBlockId(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 437 | incoming_try_entry->DebugName(), |
| 438 | incoming_try_entry->GetId(), |
| 439 | predecessor->GetBlockId())); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 444 | if (block->IsLoopHeader()) { |
| 445 | CheckLoop(block); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 450 | int id = loop_header->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 451 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 452 | |
| 453 | // Ensure the pre-header block is first in the list of |
| 454 | // predecessors of a loop header. |
| 455 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 456 | AddError(StringPrintf( |
| 457 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 458 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 461 | // Ensure the loop header has only one incoming branch and the remaining |
| 462 | // predecessors are back edges. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 463 | size_t num_preds = loop_header->GetPredecessors().size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 464 | if (num_preds < 2) { |
| 465 | AddError(StringPrintf( |
| 466 | "Loop header %d has less than two predecessors: %zu.", |
| 467 | id, |
| 468 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 469 | } else { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 470 | HBasicBlock* first_predecessor = loop_header->GetPredecessor(0); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 471 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 472 | AddError(StringPrintf( |
| 473 | "First predecessor of loop header %d is a back edge.", |
| 474 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 475 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 476 | for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) { |
| 477 | HBasicBlock* predecessor = loop_header->GetPredecessor(i); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 478 | if (!loop_information->IsBackEdge(*predecessor)) { |
| 479 | AddError(StringPrintf( |
| 480 | "Loop header %d has multiple incoming (non back edge) blocks.", |
| 481 | id)); |
| 482 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 486 | const ArenaBitVector& loop_blocks = loop_information->GetBlocks(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 487 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 488 | // Ensure back edges belong to the loop. |
| 489 | size_t num_back_edges = loop_information->GetBackEdges().Size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 490 | if (num_back_edges == 0) { |
| 491 | AddError(StringPrintf( |
| 492 | "Loop defined by header %d has no back edge.", |
| 493 | id)); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 494 | } else { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 495 | for (size_t i = 0; i < num_back_edges; ++i) { |
| 496 | int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId(); |
| 497 | if (!loop_blocks.IsBitSet(back_edge_id)) { |
| 498 | AddError(StringPrintf( |
| 499 | "Loop defined by header %d has an invalid back edge %d.", |
| 500 | id, |
| 501 | back_edge_id)); |
| 502 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 503 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 504 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 505 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 506 | // Ensure all blocks in the loop are live and dominated by the loop header. |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 507 | for (uint32_t i : loop_blocks.Indexes()) { |
| 508 | HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 509 | if (loop_block == nullptr) { |
| 510 | AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.", |
| 511 | id, |
| 512 | i)); |
| 513 | } else if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 514 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 515 | i, |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 516 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 517 | } |
| 518 | } |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 519 | |
| 520 | // If this is a nested loop, ensure the outer loops contain a superset of the blocks. |
| 521 | for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) { |
| 522 | HLoopInformation* outer_info = it.Current(); |
| 523 | if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) { |
| 524 | AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of " |
| 525 | "an outer loop defined by header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 526 | id, |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 527 | outer_info->GetHeader()->GetBlockId())); |
| 528 | } |
| 529 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 533 | super_type::VisitInstruction(instruction); |
| 534 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 535 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 536 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 537 | !use_it.Done(); use_it.Advance()) { |
| 538 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 539 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 540 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 541 | "use %d in block %d.", |
| 542 | instruction->GetId(), current_block_->GetBlockId(), |
| 543 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 546 | |
| 547 | // Ensure an instruction having an environment is dominated by the |
| 548 | // instructions contained in the environment. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 549 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 550 | environment != nullptr; |
| 551 | environment = environment->GetParent()) { |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 552 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 553 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 554 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 555 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 556 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 557 | "from block %d does not dominate instruction %d.", |
| 558 | env_instruction->GetId(), |
| 559 | instruction->GetId(), |
| 560 | current_block_->GetBlockId(), |
| 561 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 567 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 568 | switch (type) { |
| 569 | case Primitive::kPrimBoolean: |
| 570 | case Primitive::kPrimByte: |
| 571 | case Primitive::kPrimShort: |
| 572 | case Primitive::kPrimChar: |
| 573 | case Primitive::kPrimInt: |
| 574 | return Primitive::kPrimInt; |
| 575 | default: |
| 576 | return type; |
| 577 | } |
| 578 | } |
| 579 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 580 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 581 | VisitInstruction(phi); |
| 582 | |
| 583 | // Ensure the first input of a phi is not itself. |
| 584 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 585 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 586 | phi->GetId(), |
| 587 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 590 | // Ensure that the inputs have the same primitive kind as the phi. |
| 591 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 592 | HInstruction* input = phi->InputAt(i); |
| 593 | if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) { |
| 594 | AddError(StringPrintf( |
| 595 | "Input %d at index %zu of phi %d from block %d does not have the " |
| 596 | "same type as the phi: %s versus %s", |
| 597 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 598 | Primitive::PrettyDescriptor(input->GetType()), |
| 599 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 600 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 601 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 602 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 603 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 604 | phi->GetId(), |
| 605 | phi->GetBlock()->GetBlockId(), |
| 606 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 607 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 608 | |
| 609 | if (phi->IsCatchPhi()) { |
| 610 | // The number of inputs of a catch phi corresponds to the total number of |
| 611 | // throwing instructions caught by this catch block. |
| 612 | } else { |
| 613 | // Ensure the number of inputs of a non-catch phi is the same as the number |
| 614 | // of its predecessors. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 615 | const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors(); |
| 616 | if (phi->InputCount() != predecessors.size()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 617 | AddError(StringPrintf( |
| 618 | "Phi %d in block %d has %zu inputs, " |
| 619 | "but block %d has %zu predecessors.", |
| 620 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 621 | phi->GetBlock()->GetBlockId(), predecessors.size())); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 622 | } else { |
| 623 | // Ensure phi input at index I either comes from the Ith |
| 624 | // predecessor or from a block that dominates this predecessor. |
| 625 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 626 | HInstruction* input = phi->InputAt(i); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame^] | 627 | HBasicBlock* predecessor = predecessors[i]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 628 | if (!(input->GetBlock() == predecessor |
| 629 | || input->GetBlock()->Dominates(predecessor))) { |
| 630 | AddError(StringPrintf( |
| 631 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 632 | "predecessor number %zu nor in a block dominating it.", |
| 633 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 634 | i)); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 639 | } |
| 640 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 641 | void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) { |
| 642 | HInstruction* input = instruction->InputAt(input_index); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 643 | if (input->IsIntConstant()) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 644 | int32_t value = input->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 645 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 646 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 647 | "%s instruction %d has a non-Boolean constant input %d whose value is: %d.", |
| 648 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 649 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 650 | static_cast<int>(input_index), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 651 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 652 | } |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame] | 653 | } else if (input->GetType() == Primitive::kPrimInt |
| 654 | && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) { |
| 655 | // TODO: We need a data-flow analysis to determine if the Phi or |
| 656 | // binary operation is actually Boolean. Allow for now. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 657 | } else if (input->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 658 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 659 | "%s instruction %d has a non-Boolean input %d whose type is: %s.", |
| 660 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 661 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 662 | static_cast<int>(input_index), |
| 663 | Primitive::PrettyDescriptor(input->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 667 | void SSAChecker::VisitIf(HIf* instruction) { |
| 668 | VisitInstruction(instruction); |
| 669 | HandleBooleanInput(instruction, 0); |
| 670 | } |
| 671 | |
| 672 | void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) { |
| 673 | VisitInstruction(instruction); |
| 674 | HandleBooleanInput(instruction, 0); |
| 675 | } |
| 676 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 677 | void SSAChecker::VisitCondition(HCondition* op) { |
| 678 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 679 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 680 | AddError(StringPrintf( |
| 681 | "Condition %s %d has a non-Boolean result type: %s.", |
| 682 | op->DebugName(), op->GetId(), |
| 683 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 684 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 685 | HInstruction* lhs = op->InputAt(0); |
| 686 | HInstruction* rhs = op->InputAt(1); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 687 | if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
| 688 | AddError(StringPrintf( |
| 689 | "Condition %s %d has inputs of different types: %s, and %s.", |
| 690 | op->DebugName(), op->GetId(), |
| 691 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 692 | Primitive::PrettyDescriptor(rhs->GetType()))); |
| 693 | } |
| 694 | if (!op->IsEqual() && !op->IsNotEqual()) { |
| 695 | if ((lhs->GetType() == Primitive::kPrimNot)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 696 | AddError(StringPrintf( |
| 697 | "Condition %s %d uses an object as left-hand side input.", |
| 698 | op->DebugName(), op->GetId())); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 699 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 700 | AddError(StringPrintf( |
| 701 | "Condition %s %d uses an object as right-hand side input.", |
| 702 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 703 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 704 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 708 | VisitInstruction(op); |
| 709 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 710 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 711 | AddError(StringPrintf( |
| 712 | "Shift operation %s %d has a non-int kind second input: " |
| 713 | "%s of type %s.", |
| 714 | op->DebugName(), op->GetId(), |
| 715 | op->InputAt(1)->DebugName(), |
| 716 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 717 | } |
| 718 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 719 | if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 720 | AddError(StringPrintf( |
| 721 | "Binary operation %s %d has inputs of different types: " |
| 722 | "%s, and %s.", |
| 723 | op->DebugName(), op->GetId(), |
| 724 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 725 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | if (op->IsCompare()) { |
| 730 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 731 | AddError(StringPrintf( |
| 732 | "Compare operation %d has a non-int result type: %s.", |
| 733 | op->GetId(), |
| 734 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 735 | } |
| 736 | } else { |
| 737 | // Use the first input, so that we can also make this check for shift operations. |
| 738 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 739 | AddError(StringPrintf( |
| 740 | "Binary operation %s %d has a result type different " |
| 741 | "from its input type: %s vs %s.", |
| 742 | op->DebugName(), op->GetId(), |
| 743 | Primitive::PrettyDescriptor(op->GetType()), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 744 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 749 | void SSAChecker::VisitConstant(HConstant* instruction) { |
| 750 | HBasicBlock* block = instruction->GetBlock(); |
| 751 | if (!block->IsEntryBlock()) { |
| 752 | AddError(StringPrintf( |
| 753 | "%s %d should be in the entry block but is in block %d.", |
| 754 | instruction->DebugName(), |
| 755 | instruction->GetId(), |
| 756 | block->GetBlockId())); |
| 757 | } |
| 758 | } |
| 759 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 760 | } // namespace art |