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