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