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 | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 354 | HBasicBlock* successor = block->GetSuccessors()[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) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 362 | HBasicBlock* successor = block->GetSuccessors()[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 | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 376 | HBasicBlock* successor = block->GetSuccessors()[j]; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 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 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 385 | // Ensure try membership information is consistent. |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 386 | if (block->IsCatchBlock()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 387 | if (block->IsTryBlock()) { |
| 388 | const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 389 | AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d " |
| 390 | "has try entry %s:%d.", |
| 391 | block->GetBlockId(), |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 392 | try_entry.DebugName(), |
| 393 | try_entry.GetId())); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (block->IsLoopHeader()) { |
| 397 | AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.", |
| 398 | block->GetBlockId())); |
| 399 | } |
| 400 | } else { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 401 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 402 | const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors(); |
| 403 | if (block->IsTryBlock()) { |
| 404 | const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
| 405 | if (incoming_try_entry == nullptr) { |
| 406 | 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] | 407 | "from predecessor %d.", |
| 408 | block->GetBlockId(), |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 409 | stored_try_entry.DebugName(), |
| 410 | stored_try_entry.GetId(), |
| 411 | predecessor->GetBlockId())); |
| 412 | } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) { |
| 413 | AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent " |
| 414 | "with %s:%d that follows from predecessor %d.", |
| 415 | block->GetBlockId(), |
| 416 | stored_try_entry.DebugName(), |
| 417 | stored_try_entry.GetId(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 418 | incoming_try_entry->DebugName(), |
| 419 | incoming_try_entry->GetId(), |
| 420 | predecessor->GetBlockId())); |
| 421 | } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 422 | } else if (incoming_try_entry != nullptr) { |
| 423 | 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] | 424 | "from predecessor %d.", |
| 425 | block->GetBlockId(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 426 | incoming_try_entry->DebugName(), |
| 427 | incoming_try_entry->GetId(), |
| 428 | predecessor->GetBlockId())); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 433 | if (block->IsLoopHeader()) { |
| 434 | CheckLoop(block); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 439 | int id = loop_header->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 440 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 441 | |
| 442 | // Ensure the pre-header block is first in the list of |
| 443 | // predecessors of a loop header. |
| 444 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 445 | AddError(StringPrintf( |
| 446 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 447 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 450 | // Ensure the loop header has only one incoming branch and the remaining |
| 451 | // predecessors are back edges. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 452 | size_t num_preds = loop_header->GetPredecessors().size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 453 | if (num_preds < 2) { |
| 454 | AddError(StringPrintf( |
| 455 | "Loop header %d has less than two predecessors: %zu.", |
| 456 | id, |
| 457 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 458 | } else { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 459 | HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0]; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 460 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 461 | AddError(StringPrintf( |
| 462 | "First predecessor of loop header %d is a back edge.", |
| 463 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 464 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 465 | for (size_t i = 1, e = loop_header->GetPredecessors().size(); i < e; ++i) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 466 | HBasicBlock* predecessor = loop_header->GetPredecessors()[i]; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 467 | if (!loop_information->IsBackEdge(*predecessor)) { |
| 468 | AddError(StringPrintf( |
| 469 | "Loop header %d has multiple incoming (non back edge) blocks.", |
| 470 | id)); |
| 471 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 475 | const ArenaBitVector& loop_blocks = loop_information->GetBlocks(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 476 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 477 | // Ensure back edges belong to the loop. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 478 | if (loop_information->NumberOfBackEdges() == 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 479 | AddError(StringPrintf( |
| 480 | "Loop defined by header %d has no back edge.", |
| 481 | id)); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 482 | } else { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 483 | for (HBasicBlock* back_edge : loop_information->GetBackEdges()) { |
| 484 | int back_edge_id = back_edge->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 485 | if (!loop_blocks.IsBitSet(back_edge_id)) { |
| 486 | AddError(StringPrintf( |
| 487 | "Loop defined by header %d has an invalid back edge %d.", |
| 488 | id, |
| 489 | back_edge_id)); |
| 490 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 491 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 492 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 493 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 494 | // 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] | 495 | for (uint32_t i : loop_blocks.Indexes()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame^] | 496 | HBasicBlock* loop_block = GetGraph()->GetBlocks()[i]; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 497 | if (loop_block == nullptr) { |
| 498 | AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.", |
| 499 | id, |
| 500 | i)); |
| 501 | } else if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 502 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 503 | i, |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 504 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 505 | } |
| 506 | } |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 507 | |
| 508 | // If this is a nested loop, ensure the outer loops contain a superset of the blocks. |
| 509 | for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) { |
| 510 | HLoopInformation* outer_info = it.Current(); |
| 511 | if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) { |
| 512 | AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of " |
| 513 | "an outer loop defined by header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 514 | id, |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 515 | outer_info->GetHeader()->GetBlockId())); |
| 516 | } |
| 517 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 521 | super_type::VisitInstruction(instruction); |
| 522 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 523 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 524 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 525 | !use_it.Done(); use_it.Advance()) { |
| 526 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 527 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 528 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 529 | "use %d in block %d.", |
| 530 | instruction->GetId(), current_block_->GetBlockId(), |
| 531 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 532 | } |
| 533 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 534 | |
| 535 | // Ensure an instruction having an environment is dominated by the |
| 536 | // instructions contained in the environment. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 537 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 538 | environment != nullptr; |
| 539 | environment = environment->GetParent()) { |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 540 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 541 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 542 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 543 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 544 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 545 | "from block %d does not dominate instruction %d.", |
| 546 | env_instruction->GetId(), |
| 547 | instruction->GetId(), |
| 548 | current_block_->GetBlockId(), |
| 549 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 555 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 556 | switch (type) { |
| 557 | case Primitive::kPrimBoolean: |
| 558 | case Primitive::kPrimByte: |
| 559 | case Primitive::kPrimShort: |
| 560 | case Primitive::kPrimChar: |
| 561 | case Primitive::kPrimInt: |
| 562 | return Primitive::kPrimInt; |
| 563 | default: |
| 564 | return type; |
| 565 | } |
| 566 | } |
| 567 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 568 | static bool IsSameSizeConstant(HInstruction* insn1, HInstruction* insn2) { |
| 569 | return insn1->IsConstant() |
| 570 | && insn2->IsConstant() |
| 571 | && Primitive::Is64BitType(insn1->GetType()) == Primitive::Is64BitType(insn2->GetType()); |
| 572 | } |
| 573 | |
| 574 | static bool IsConstantEquivalent(HInstruction* insn1, HInstruction* insn2, BitVector* visited) { |
| 575 | if (insn1->IsPhi() && |
| 576 | insn1->AsPhi()->IsVRegEquivalentOf(insn2) && |
| 577 | insn1->InputCount() == insn2->InputCount()) { |
| 578 | // Testing only one of the two inputs for recursion is sufficient. |
| 579 | if (visited->IsBitSet(insn1->GetId())) { |
| 580 | return true; |
| 581 | } |
| 582 | visited->SetBit(insn1->GetId()); |
| 583 | |
| 584 | for (size_t i = 0, e = insn1->InputCount(); i < e; ++i) { |
| 585 | if (!IsConstantEquivalent(insn1->InputAt(i), insn2->InputAt(i), visited)) { |
| 586 | return false; |
| 587 | } |
| 588 | } |
| 589 | return true; |
| 590 | } else if (IsSameSizeConstant(insn1, insn2)) { |
| 591 | return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64(); |
| 592 | } else { |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 597 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 598 | VisitInstruction(phi); |
| 599 | |
| 600 | // Ensure the first input of a phi is not itself. |
| 601 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 602 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 603 | phi->GetId(), |
| 604 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 607 | // Ensure that the inputs have the same primitive kind as the phi. |
| 608 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 609 | HInstruction* input = phi->InputAt(i); |
| 610 | if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) { |
| 611 | AddError(StringPrintf( |
| 612 | "Input %d at index %zu of phi %d from block %d does not have the " |
| 613 | "same type as the phi: %s versus %s", |
| 614 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 615 | Primitive::PrettyDescriptor(input->GetType()), |
| 616 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 617 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 618 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 619 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 620 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 621 | phi->GetId(), |
| 622 | phi->GetBlock()->GetBlockId(), |
| 623 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 624 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 625 | |
| 626 | if (phi->IsCatchPhi()) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 627 | // The number of inputs of a catch phi should be the total number of throwing |
| 628 | // instructions caught by this catch block. We do not enforce this, however, |
| 629 | // because we do not remove the corresponding inputs when we prove that an |
| 630 | // instruction cannot throw. Instead, we at least test that all phis have the |
| 631 | // same, non-zero number of inputs (b/24054676). |
| 632 | size_t input_count_this = phi->InputCount(); |
| 633 | if (input_count_this == 0u) { |
| 634 | AddError(StringPrintf("Phi %d in catch block %d has zero inputs.", |
| 635 | phi->GetId(), |
| 636 | phi->GetBlock()->GetBlockId())); |
| 637 | } else { |
| 638 | HInstruction* next_phi = phi->GetNext(); |
| 639 | if (next_phi != nullptr) { |
| 640 | size_t input_count_next = next_phi->InputCount(); |
| 641 | if (input_count_this != input_count_next) { |
| 642 | AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, " |
| 643 | "but phi %d has %zu inputs.", |
| 644 | phi->GetId(), |
| 645 | phi->GetBlock()->GetBlockId(), |
| 646 | input_count_this, |
| 647 | next_phi->GetId(), |
| 648 | input_count_next)); |
| 649 | } |
| 650 | } |
| 651 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 652 | } else { |
| 653 | // Ensure the number of inputs of a non-catch phi is the same as the number |
| 654 | // of its predecessors. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 655 | const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors(); |
| 656 | if (phi->InputCount() != predecessors.size()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 657 | AddError(StringPrintf( |
| 658 | "Phi %d in block %d has %zu inputs, " |
| 659 | "but block %d has %zu predecessors.", |
| 660 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 661 | phi->GetBlock()->GetBlockId(), predecessors.size())); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 662 | } else { |
| 663 | // Ensure phi input at index I either comes from the Ith |
| 664 | // predecessor or from a block that dominates this predecessor. |
| 665 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 666 | HInstruction* input = phi->InputAt(i); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 667 | HBasicBlock* predecessor = predecessors[i]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 668 | if (!(input->GetBlock() == predecessor |
| 669 | || input->GetBlock()->Dominates(predecessor))) { |
| 670 | AddError(StringPrintf( |
| 671 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 672 | "predecessor number %zu nor in a block dominating it.", |
| 673 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 674 | i)); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 679 | |
| 680 | // Ensure that catch phis are sorted by their vreg number, as required by |
| 681 | // the register allocator and code generator. This does not apply to normal |
| 682 | // phis which can be constructed artifically. |
| 683 | if (phi->IsCatchPhi()) { |
| 684 | HInstruction* next_phi = phi->GetNext(); |
| 685 | if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) { |
| 686 | AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their " |
| 687 | "vreg numbers.", |
| 688 | phi->GetId(), |
| 689 | next_phi->GetId(), |
| 690 | phi->GetBlock()->GetBlockId())); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // Test phi equivalents. There should not be two of the same type and they |
| 695 | // should only be created for constants which were untyped in DEX. |
| 696 | for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 697 | HPhi* other_phi = phi_it.Current()->AsPhi(); |
| 698 | if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) { |
| 699 | if (phi->GetType() == other_phi->GetType()) { |
| 700 | std::stringstream type_str; |
| 701 | type_str << phi->GetType(); |
| 702 | AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.", |
| 703 | phi->GetId(), |
| 704 | phi->GetRegNumber(), |
| 705 | type_str.str().c_str())); |
| 706 | } else { |
| 707 | ArenaBitVector visited(GetGraph()->GetArena(), 0, /* expandable */ true); |
| 708 | if (!IsConstantEquivalent(phi, other_phi, &visited)) { |
| 709 | AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they " |
| 710 | "are not equivalents of constants.", |
| 711 | phi->GetId(), |
| 712 | other_phi->GetId(), |
| 713 | phi->GetRegNumber())); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 718 | } |
| 719 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 720 | void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) { |
| 721 | HInstruction* input = instruction->InputAt(input_index); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 722 | if (input->IsIntConstant()) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 723 | int32_t value = input->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 724 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 725 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 726 | "%s instruction %d has a non-Boolean constant input %d whose value is: %d.", |
| 727 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 728 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 729 | static_cast<int>(input_index), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 730 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 731 | } |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame] | 732 | } else if (input->GetType() == Primitive::kPrimInt |
| 733 | && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) { |
| 734 | // TODO: We need a data-flow analysis to determine if the Phi or |
| 735 | // binary operation is actually Boolean. Allow for now. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 736 | } else if (input->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 737 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 738 | "%s instruction %d has a non-Boolean input %d whose type is: %s.", |
| 739 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 740 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 741 | static_cast<int>(input_index), |
| 742 | Primitive::PrettyDescriptor(input->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 746 | void SSAChecker::VisitPackedSwitch(HPackedSwitch* instruction) { |
| 747 | VisitInstruction(instruction); |
| 748 | // Check that the number of block successors matches the switch count plus |
| 749 | // one for the default block. |
| 750 | HBasicBlock* block = instruction->GetBlock(); |
| 751 | if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) { |
| 752 | AddError(StringPrintf( |
| 753 | "%s instruction %d in block %d expects %u successors to the block, but found: %zu.", |
| 754 | instruction->DebugName(), |
| 755 | instruction->GetId(), |
| 756 | block->GetBlockId(), |
| 757 | instruction->GetNumEntries() + 1u, |
| 758 | block->GetSuccessors().size())); |
| 759 | } |
| 760 | } |
| 761 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 762 | void SSAChecker::VisitIf(HIf* instruction) { |
| 763 | VisitInstruction(instruction); |
| 764 | HandleBooleanInput(instruction, 0); |
| 765 | } |
| 766 | |
| 767 | void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) { |
| 768 | VisitInstruction(instruction); |
| 769 | HandleBooleanInput(instruction, 0); |
| 770 | } |
| 771 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 772 | void SSAChecker::VisitCondition(HCondition* op) { |
| 773 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 774 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 775 | AddError(StringPrintf( |
| 776 | "Condition %s %d has a non-Boolean result type: %s.", |
| 777 | op->DebugName(), op->GetId(), |
| 778 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 779 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 780 | HInstruction* lhs = op->InputAt(0); |
| 781 | HInstruction* rhs = op->InputAt(1); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 782 | if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
| 783 | AddError(StringPrintf( |
| 784 | "Condition %s %d has inputs of different types: %s, and %s.", |
| 785 | op->DebugName(), op->GetId(), |
| 786 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 787 | Primitive::PrettyDescriptor(rhs->GetType()))); |
| 788 | } |
| 789 | if (!op->IsEqual() && !op->IsNotEqual()) { |
| 790 | if ((lhs->GetType() == Primitive::kPrimNot)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 791 | AddError(StringPrintf( |
| 792 | "Condition %s %d uses an object as left-hand side input.", |
| 793 | op->DebugName(), op->GetId())); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 794 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 795 | AddError(StringPrintf( |
| 796 | "Condition %s %d uses an object as right-hand side input.", |
| 797 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 798 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 799 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 803 | VisitInstruction(op); |
| 804 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 805 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 806 | AddError(StringPrintf( |
| 807 | "Shift operation %s %d has a non-int kind second input: " |
| 808 | "%s of type %s.", |
| 809 | op->DebugName(), op->GetId(), |
| 810 | op->InputAt(1)->DebugName(), |
| 811 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 812 | } |
| 813 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 814 | if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 815 | AddError(StringPrintf( |
| 816 | "Binary operation %s %d has inputs of different types: " |
| 817 | "%s, and %s.", |
| 818 | op->DebugName(), op->GetId(), |
| 819 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 820 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | if (op->IsCompare()) { |
| 825 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 826 | AddError(StringPrintf( |
| 827 | "Compare operation %d has a non-int result type: %s.", |
| 828 | op->GetId(), |
| 829 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 830 | } |
| 831 | } else { |
| 832 | // Use the first input, so that we can also make this check for shift operations. |
| 833 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 834 | AddError(StringPrintf( |
| 835 | "Binary operation %s %d has a result type different " |
| 836 | "from its input type: %s vs %s.", |
| 837 | op->DebugName(), op->GetId(), |
| 838 | Primitive::PrettyDescriptor(op->GetType()), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 839 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 844 | void SSAChecker::VisitConstant(HConstant* instruction) { |
| 845 | HBasicBlock* block = instruction->GetBlock(); |
| 846 | if (!block->IsEntryBlock()) { |
| 847 | AddError(StringPrintf( |
| 848 | "%s %d should be in the entry block but is in block %d.", |
| 849 | instruction->DebugName(), |
| 850 | instruction->GetId(), |
| 851 | block->GetBlockId())); |
| 852 | } |
| 853 | } |
| 854 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 855 | } // namespace art |