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