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> |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 20 | #include <sstream> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <string> |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
| 24 | |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 25 | #include "base/bit_vector-inl.h" |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 26 | #include "base/scoped_arena_allocator.h" |
| 27 | #include "base/scoped_arena_containers.h" |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 28 | #include "code_generator.h" |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 29 | #include "handle.h" |
| 30 | #include "mirror/class.h" |
| 31 | #include "obj_ptr-inl.h" |
| 32 | #include "scoped_thread_state_change-inl.h" |
| 33 | #include "subtype_check.h" |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 34 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 35 | namespace art { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 36 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 39 | static bool IsAllowedToJumpToExitBlock(HInstruction* instruction) { |
Aart Bik | a8b8e9b | 2018-01-09 11:01:02 -0800 | [diff] [blame] | 40 | // Anything that returns is allowed to jump into the exit block. |
| 41 | if (instruction->IsReturn() || instruction->IsReturnVoid()) { |
| 42 | return true; |
| 43 | } |
| 44 | // Anything that always throws is allowed to jump into the exit block. |
| 45 | if (instruction->IsGoto() && instruction->GetPrevious() != nullptr) { |
| 46 | instruction = instruction->GetPrevious(); |
| 47 | } |
| 48 | return instruction->AlwaysThrows(); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static bool IsExitTryBoundaryIntoExitBlock(HBasicBlock* block) { |
| 52 | if (!block->IsSingleTryBoundary()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | HTryBoundary* boundary = block->GetLastInstruction()->AsTryBoundary(); |
| 57 | return block->GetPredecessors().size() == 1u && |
| 58 | boundary->GetNormalFlowSuccessor()->IsExitBlock() && |
| 59 | !boundary->IsEntry(); |
| 60 | } |
| 61 | |
Aart Bik | a8360cd | 2018-05-02 16:07:51 -0700 | [diff] [blame] | 62 | |
| 63 | size_t GraphChecker::Run(bool pass_change, size_t last_size) { |
| 64 | size_t current_size = GetGraph()->GetReversePostOrder().size(); |
| 65 | if (!pass_change) { |
Ian Pedowitz | 2d53643 | 2020-07-22 14:33:00 -0700 | [diff] [blame] | 66 | // Nothing changed for certain. Do a quick check of the validity on that assertion |
Aart Bik | a8360cd | 2018-05-02 16:07:51 -0700 | [diff] [blame] | 67 | // for anything other than the first call (when last size was still 0). |
| 68 | if (last_size != 0) { |
| 69 | if (current_size != last_size) { |
| 70 | AddError(StringPrintf("Incorrect no-change assertion, " |
| 71 | "last graph size %zu vs current graph size %zu", |
| 72 | last_size, current_size)); |
| 73 | } |
| 74 | } |
| 75 | // TODO: if we would trust the "false" value of the flag completely, we |
| 76 | // could skip checking the graph at this point. |
| 77 | } |
| 78 | |
| 79 | // VisitReversePostOrder is used instead of VisitInsertionOrder, |
| 80 | // as the latter might visit dead blocks removed by the dominator |
| 81 | // computation. |
| 82 | VisitReversePostOrder(); |
| 83 | return current_size; |
| 84 | } |
| 85 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 86 | void GraphChecker::VisitBasicBlock(HBasicBlock* block) { |
| 87 | current_block_ = block; |
| 88 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 89 | // Use local allocator for allocating memory. |
| 90 | ScopedArenaAllocator allocator(GetGraph()->GetArenaStack()); |
| 91 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 92 | // Check consistency with respect to predecessors of `block`. |
Vladimir Marko | 0f49c82 | 2016-03-22 17:51:29 +0000 | [diff] [blame] | 93 | // Note: Counting duplicates with a sorted vector uses up to 6x less memory |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 94 | // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 95 | ScopedArenaVector<HBasicBlock*> sorted_predecessors(allocator.Adapter(kArenaAllocGraphChecker)); |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 96 | sorted_predecessors.assign(block->GetPredecessors().begin(), block->GetPredecessors().end()); |
Vladimir Marko | 0f49c82 | 2016-03-22 17:51:29 +0000 | [diff] [blame] | 97 | std::sort(sorted_predecessors.begin(), sorted_predecessors.end()); |
| 98 | for (auto it = sorted_predecessors.begin(), end = sorted_predecessors.end(); it != end; ) { |
| 99 | HBasicBlock* p = *it++; |
| 100 | size_t p_count_in_block_predecessors = 1u; |
| 101 | for (; it != end && *it == p; ++it) { |
| 102 | ++p_count_in_block_predecessors; |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 103 | } |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 104 | size_t block_count_in_p_successors = |
| 105 | std::count(p->GetSuccessors().begin(), p->GetSuccessors().end(), block); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 106 | if (p_count_in_block_predecessors != block_count_in_p_successors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 107 | AddError(StringPrintf( |
| 108 | "Block %d lists %zu occurrences of block %d in its predecessors, whereas " |
| 109 | "block %d lists %zu occurrences of block %d in its successors.", |
| 110 | block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(), |
| 111 | p->GetBlockId(), block_count_in_p_successors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | // Check consistency with respect to successors of `block`. |
Vladimir Marko | 0f49c82 | 2016-03-22 17:51:29 +0000 | [diff] [blame] | 116 | // Note: Counting duplicates with a sorted vector uses up to 6x less memory |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 117 | // than ArenaSafeMap<HBasicBlock*, size_t> and also allows storage reuse. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 118 | ScopedArenaVector<HBasicBlock*> sorted_successors(allocator.Adapter(kArenaAllocGraphChecker)); |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 119 | sorted_successors.assign(block->GetSuccessors().begin(), block->GetSuccessors().end()); |
Vladimir Marko | 0f49c82 | 2016-03-22 17:51:29 +0000 | [diff] [blame] | 120 | std::sort(sorted_successors.begin(), sorted_successors.end()); |
| 121 | for (auto it = sorted_successors.begin(), end = sorted_successors.end(); it != end; ) { |
| 122 | HBasicBlock* s = *it++; |
| 123 | size_t s_count_in_block_successors = 1u; |
| 124 | for (; it != end && *it == s; ++it) { |
| 125 | ++s_count_in_block_successors; |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 126 | } |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 127 | size_t block_count_in_s_predecessors = |
| 128 | std::count(s->GetPredecessors().begin(), s->GetPredecessors().end(), block); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 129 | if (s_count_in_block_successors != block_count_in_s_predecessors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 130 | AddError(StringPrintf( |
| 131 | "Block %d lists %zu occurrences of block %d in its successors, whereas " |
| 132 | "block %d lists %zu occurrences of block %d in its predecessors.", |
| 133 | block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(), |
| 134 | s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | // Ensure `block` ends with a branch instruction. |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 139 | // This invariant is not enforced on non-SSA graphs. Graph built from DEX with |
| 140 | // dead code that falls out of the method will not end with a control-flow |
| 141 | // instruction. Such code is removed during the SSA-building DCE phase. |
| 142 | if (GetGraph()->IsInSsaForm() && !block->EndsWithControlFlowInstruction()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 143 | AddError(StringPrintf("Block %d does not end with a branch instruction.", |
| 144 | block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 145 | } |
| 146 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 147 | // Ensure that only Return(Void) and Throw jump to Exit. An exiting TryBoundary |
| 148 | // may be between the instructions if the Throw/Return(Void) is in a try block. |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 149 | if (block->IsExitBlock()) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 150 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 151 | HInstruction* last_instruction = IsExitTryBoundaryIntoExitBlock(predecessor) ? |
| 152 | predecessor->GetSinglePredecessor()->GetLastInstruction() : |
| 153 | predecessor->GetLastInstruction(); |
| 154 | if (!IsAllowedToJumpToExitBlock(last_instruction)) { |
| 155 | AddError(StringPrintf("Unexpected instruction %s:%d jumps into the exit block.", |
| 156 | last_instruction->DebugName(), |
| 157 | last_instruction->GetId())); |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 162 | // Visit this block's list of phis. |
| 163 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 164 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 165 | // Ensure this block's list of phis contains only phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 166 | if (!current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 167 | AddError(StringPrintf("Block %d has a non-phi in its phi list.", |
| 168 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 169 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 170 | if (current->GetNext() == nullptr && current != block->GetLastPhi()) { |
| 171 | AddError(StringPrintf("The recorded last phi of block %d does not match " |
| 172 | "the actual last phi %d.", |
| 173 | current_block_->GetBlockId(), |
| 174 | current->GetId())); |
| 175 | } |
| 176 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Visit this block's list of instructions. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 180 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 181 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 182 | // Ensure this block's list of instructions does not contains phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 183 | if (current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 184 | AddError(StringPrintf("Block %d has a phi in its non-phi list.", |
| 185 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 186 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 187 | if (current->GetNext() == nullptr && current != block->GetLastInstruction()) { |
| 188 | AddError(StringPrintf("The recorded last instruction of block %d does not match " |
| 189 | "the actual last instruction %d.", |
| 190 | current_block_->GetBlockId(), |
| 191 | current->GetId())); |
| 192 | } |
| 193 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 194 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 195 | |
| 196 | // Ensure that catch blocks are not normal successors, and normal blocks are |
| 197 | // never exceptional successors. |
| 198 | for (HBasicBlock* successor : block->GetNormalSuccessors()) { |
| 199 | if (successor->IsCatchBlock()) { |
| 200 | AddError(StringPrintf("Catch block %d is a normal successor of block %d.", |
| 201 | successor->GetBlockId(), |
| 202 | block->GetBlockId())); |
| 203 | } |
| 204 | } |
| 205 | for (HBasicBlock* successor : block->GetExceptionalSuccessors()) { |
| 206 | if (!successor->IsCatchBlock()) { |
| 207 | AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.", |
| 208 | successor->GetBlockId(), |
| 209 | block->GetBlockId())); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Ensure dominated blocks have `block` as the dominator. |
| 214 | for (HBasicBlock* dominated : block->GetDominatedBlocks()) { |
| 215 | if (dominated->GetDominator() != block) { |
| 216 | AddError(StringPrintf("Block %d should be the dominator of %d.", |
| 217 | block->GetBlockId(), |
| 218 | dominated->GetBlockId())); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 223 | // block with multiple successors to a block with multiple |
| 224 | // predecessors). Exceptional edges are synthesized and hence |
| 225 | // not accounted for. |
| 226 | if (block->GetSuccessors().size() > 1) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 227 | if (IsExitTryBoundaryIntoExitBlock(block)) { |
| 228 | // Allowed critical edge (Throw/Return/ReturnVoid)->TryBoundary->Exit. |
| 229 | } else { |
| 230 | for (HBasicBlock* successor : block->GetNormalSuccessors()) { |
| 231 | if (successor->GetPredecessors().size() > 1) { |
| 232 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 233 | block->GetBlockId(), |
| 234 | successor->GetBlockId())); |
| 235 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Ensure try membership information is consistent. |
| 241 | if (block->IsCatchBlock()) { |
| 242 | if (block->IsTryBlock()) { |
| 243 | const HTryBoundary& try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
| 244 | AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d " |
| 245 | "has try entry %s:%d.", |
| 246 | block->GetBlockId(), |
| 247 | try_entry.DebugName(), |
| 248 | try_entry.GetId())); |
| 249 | } |
| 250 | |
| 251 | if (block->IsLoopHeader()) { |
| 252 | AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.", |
| 253 | block->GetBlockId())); |
| 254 | } |
| 255 | } else { |
| 256 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 257 | const HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors(); |
| 258 | if (block->IsTryBlock()) { |
| 259 | const HTryBoundary& stored_try_entry = block->GetTryCatchInformation()->GetTryEntry(); |
| 260 | if (incoming_try_entry == nullptr) { |
| 261 | AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows " |
| 262 | "from predecessor %d.", |
| 263 | block->GetBlockId(), |
| 264 | stored_try_entry.DebugName(), |
| 265 | stored_try_entry.GetId(), |
| 266 | predecessor->GetBlockId())); |
| 267 | } else if (!incoming_try_entry->HasSameExceptionHandlersAs(stored_try_entry)) { |
| 268 | AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent " |
| 269 | "with %s:%d that follows from predecessor %d.", |
| 270 | block->GetBlockId(), |
| 271 | stored_try_entry.DebugName(), |
| 272 | stored_try_entry.GetId(), |
| 273 | incoming_try_entry->DebugName(), |
| 274 | incoming_try_entry->GetId(), |
| 275 | predecessor->GetBlockId())); |
| 276 | } |
| 277 | } else if (incoming_try_entry != nullptr) { |
| 278 | AddError(StringPrintf("Block %d is not a try block but try entry %s:%d follows " |
| 279 | "from predecessor %d.", |
| 280 | block->GetBlockId(), |
| 281 | incoming_try_entry->DebugName(), |
| 282 | incoming_try_entry->GetId(), |
| 283 | predecessor->GetBlockId())); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (block->IsLoopHeader()) { |
| 289 | HandleLoop(block); |
| 290 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 293 | void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) { |
| 294 | if (!GetGraph()->HasBoundsChecks()) { |
| 295 | AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, " |
| 296 | "but HasBoundsChecks() returns false", |
| 297 | check->DebugName(), |
| 298 | check->GetId())); |
| 299 | } |
| 300 | |
| 301 | // Perform the instruction base checks too. |
| 302 | VisitInstruction(check); |
| 303 | } |
| 304 | |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 305 | void GraphChecker::VisitDeoptimize(HDeoptimize* deopt) { |
| 306 | if (GetGraph()->IsCompilingOsr()) { |
| 307 | AddError(StringPrintf("A graph compiled OSR cannot have a HDeoptimize instruction")); |
| 308 | } |
| 309 | |
| 310 | // Perform the instruction base checks too. |
| 311 | VisitInstruction(deopt); |
| 312 | } |
| 313 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 314 | void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) { |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 315 | ArrayRef<HBasicBlock* const> handlers = try_boundary->GetExceptionHandlers(); |
| 316 | |
| 317 | // Ensure that all exception handlers are catch blocks. |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 318 | // Note that a normal-flow successor may be a catch block before CFG |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 319 | // simplification. We only test normal-flow successors in GraphChecker. |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 320 | for (HBasicBlock* handler : handlers) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 321 | if (!handler->IsCatchBlock()) { |
| 322 | AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which " |
| 323 | "is not a catch block.", |
| 324 | current_block_->GetBlockId(), |
| 325 | try_boundary->DebugName(), |
| 326 | try_boundary->GetId(), |
| 327 | handler->GetBlockId())); |
| 328 | } |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Ensure that handlers are not listed multiple times. |
| 332 | for (size_t i = 0, e = handlers.size(); i < e; ++i) { |
David Brazdil | d8ef0c6 | 2015-11-10 18:49:28 +0000 | [diff] [blame] | 333 | if (ContainsElement(handlers, handlers[i], i + 1)) { |
| 334 | 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] | 335 | handlers[i]->GetBlockId(), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 336 | try_boundary->DebugName(), |
| 337 | try_boundary->GetId())); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | VisitInstruction(try_boundary); |
| 342 | } |
| 343 | |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 344 | void GraphChecker::VisitLoadException(HLoadException* load) { |
| 345 | // Ensure that LoadException is the first instruction in a catch block. |
| 346 | if (!load->GetBlock()->IsCatchBlock()) { |
| 347 | AddError(StringPrintf("%s:%d is in a non-catch block %d.", |
| 348 | load->DebugName(), |
| 349 | load->GetId(), |
| 350 | load->GetBlock()->GetBlockId())); |
| 351 | } else if (load->GetBlock()->GetFirstInstruction() != load) { |
| 352 | AddError(StringPrintf("%s:%d is not the first instruction in catch block %d.", |
| 353 | load->DebugName(), |
| 354 | load->GetId(), |
| 355 | load->GetBlock()->GetBlockId())); |
| 356 | } |
| 357 | } |
| 358 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 359 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 360 | if (seen_ids_.IsBitSet(instruction->GetId())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 361 | AddError(StringPrintf("Instruction id %d is duplicate in graph.", |
| 362 | instruction->GetId())); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 363 | } else { |
| 364 | seen_ids_.SetBit(instruction->GetId()); |
| 365 | } |
| 366 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 367 | // Ensure `instruction` is associated with `current_block_`. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 368 | if (instruction->GetBlock() == nullptr) { |
| 369 | AddError(StringPrintf("%s %d in block %d not associated with any block.", |
| 370 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 371 | instruction->GetId(), |
| 372 | current_block_->GetBlockId())); |
| 373 | } else if (instruction->GetBlock() != current_block_) { |
| 374 | AddError(StringPrintf("%s %d in block %d associated with block %d.", |
| 375 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 376 | instruction->GetId(), |
| 377 | current_block_->GetBlockId(), |
| 378 | instruction->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 379 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 380 | |
| 381 | // Ensure the inputs of `instruction` are defined in a block of the graph. |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 382 | for (HInstruction* input : instruction->GetInputs()) { |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 383 | if (input->GetBlock() == nullptr) { |
| 384 | AddError(StringPrintf("Input %d of instruction %d is not in any " |
| 385 | "basic block of the control-flow graph.", |
| 386 | input->GetId(), |
| 387 | instruction->GetId())); |
Igor Murashkin | 4ae432d | 2017-05-04 14:15:08 -0700 | [diff] [blame] | 388 | } else { |
| 389 | const HInstructionList& list = input->IsPhi() |
| 390 | ? input->GetBlock()->GetPhis() |
| 391 | : input->GetBlock()->GetInstructions(); |
| 392 | if (!list.Contains(input)) { |
| 393 | AddError(StringPrintf("Input %d of instruction %d is not defined " |
| 394 | "in a basic block of the control-flow graph.", |
| 395 | input->GetId(), |
| 396 | instruction->GetId())); |
| 397 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 401 | // Ensure the uses of `instruction` are defined in a block of the graph, |
| 402 | // and the entry in the use list is consistent. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 403 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 404 | HInstruction* user = use.GetUser(); |
| 405 | const HInstructionList& list = user->IsPhi() |
| 406 | ? user->GetBlock()->GetPhis() |
| 407 | : user->GetBlock()->GetInstructions(); |
| 408 | if (!list.Contains(user)) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 409 | AddError(StringPrintf("User %s:%d of instruction %d is not defined " |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 410 | "in a basic block of the control-flow graph.", |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 411 | user->DebugName(), |
| 412 | user->GetId(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 413 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 414 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 415 | size_t use_index = use.GetIndex(); |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 416 | HConstInputsRef user_inputs = user->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 417 | if ((use_index >= user_inputs.size()) || (user_inputs[use_index] != instruction)) { |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 418 | AddError(StringPrintf("User %s:%d of instruction %s:%d has a wrong " |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 419 | "UseListNode index.", |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 420 | user->DebugName(), |
| 421 | user->GetId(), |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 422 | instruction->DebugName(), |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 423 | instruction->GetId())); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // Ensure the environment uses entries are consistent. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 428 | for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) { |
| 429 | HEnvironment* user = use.GetUser(); |
| 430 | size_t use_index = use.GetIndex(); |
| 431 | if ((use_index >= user->Size()) || (user->GetInstructionAt(use_index) != instruction)) { |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 432 | AddError(StringPrintf("Environment user of %s:%d has a wrong " |
| 433 | "UseListNode index.", |
| 434 | instruction->DebugName(), |
| 435 | instruction->GetId())); |
| 436 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 437 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 438 | |
| 439 | // Ensure 'instruction' has pointers to its inputs' use entries. |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 440 | auto&& input_records = instruction->GetInputRecords(); |
| 441 | for (size_t i = 0; i < input_records.size(); ++i) { |
| 442 | const HUserRecord<HInstruction*>& input_record = input_records[i]; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 443 | HInstruction* input = input_record.GetInstruction(); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 444 | if ((input_record.GetBeforeUseNode() == input->GetUses().end()) || |
| 445 | (input_record.GetUseNode() == input->GetUses().end()) || |
| 446 | !input->GetUses().ContainsNode(*input_record.GetUseNode()) || |
| 447 | (input_record.GetUseNode()->GetIndex() != i)) { |
| 448 | AddError(StringPrintf("Instruction %s:%d has an invalid iterator before use entry " |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 449 | "at input %u (%s:%d).", |
| 450 | instruction->DebugName(), |
| 451 | instruction->GetId(), |
| 452 | static_cast<unsigned>(i), |
| 453 | input->DebugName(), |
| 454 | input->GetId())); |
| 455 | } |
| 456 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 457 | |
| 458 | // Ensure an instruction dominates all its uses. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 459 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 460 | HInstruction* user = use.GetUser(); |
| 461 | if (!user->IsPhi() && !instruction->StrictlyDominates(user)) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 462 | AddError(StringPrintf("Instruction %s:%d in block %d does not dominate " |
| 463 | "use %s:%d in block %d.", |
| 464 | instruction->DebugName(), |
| 465 | instruction->GetId(), |
| 466 | current_block_->GetBlockId(), |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 467 | user->DebugName(), |
| 468 | user->GetId(), |
| 469 | user->GetBlock()->GetBlockId())); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | if (instruction->NeedsEnvironment() && !instruction->HasEnvironment()) { |
| 474 | AddError(StringPrintf("Instruction %s:%d in block %d requires an environment " |
| 475 | "but does not have one.", |
| 476 | instruction->DebugName(), |
| 477 | instruction->GetId(), |
| 478 | current_block_->GetBlockId())); |
| 479 | } |
| 480 | |
| 481 | // Ensure an instruction having an environment is dominated by the |
| 482 | // instructions contained in the environment. |
| 483 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 484 | environment != nullptr; |
| 485 | environment = environment->GetParent()) { |
| 486 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 487 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 488 | if (env_instruction != nullptr |
| 489 | && !env_instruction->StrictlyDominates(instruction)) { |
| 490 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 491 | "from block %d does not dominate instruction %d.", |
| 492 | env_instruction->GetId(), |
| 493 | instruction->GetId(), |
| 494 | current_block_->GetBlockId(), |
| 495 | instruction->GetId())); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // Ensure that reference type instructions have reference type info. |
Evgeny Astigeevich | 7ee34a1 | 2019-12-10 11:36:33 +0000 | [diff] [blame] | 501 | if (check_reference_type_info_ && instruction->GetType() == DataType::Type::kReference) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 502 | if (!instruction->GetReferenceTypeInfo().IsValid()) { |
| 503 | AddError(StringPrintf("Reference type instruction %s:%d does not have " |
| 504 | "valid reference type information.", |
| 505 | instruction->DebugName(), |
| 506 | instruction->GetId())); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (instruction->CanThrowIntoCatchBlock()) { |
| 511 | // Find the top-level environment. This corresponds to the environment of |
| 512 | // the catch block since we do not inline methods with try/catch. |
| 513 | HEnvironment* environment = instruction->GetEnvironment(); |
| 514 | while (environment->GetParent() != nullptr) { |
| 515 | environment = environment->GetParent(); |
| 516 | } |
| 517 | |
| 518 | // Find all catch blocks and test that `instruction` has an environment |
| 519 | // value for each one. |
| 520 | const HTryBoundary& entry = instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry(); |
| 521 | for (HBasicBlock* catch_block : entry.GetExceptionHandlers()) { |
| 522 | for (HInstructionIterator phi_it(catch_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 523 | HPhi* catch_phi = phi_it.Current()->AsPhi(); |
| 524 | if (environment->GetInstructionAt(catch_phi->GetRegNumber()) == nullptr) { |
| 525 | AddError(StringPrintf("Instruction %s:%d throws into catch block %d " |
| 526 | "with catch phi %d for vreg %d but its " |
| 527 | "corresponding environment slot is empty.", |
| 528 | instruction->DebugName(), |
| 529 | instruction->GetId(), |
| 530 | catch_block->GetBlockId(), |
| 531 | catch_phi->GetId(), |
| 532 | catch_phi->GetRegNumber())); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 539 | void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
| 540 | VisitInstruction(invoke); |
| 541 | |
| 542 | if (invoke->IsStaticWithExplicitClinitCheck()) { |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 543 | const HInstruction* last_input = invoke->GetInputs().back(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 544 | if (last_input == nullptr) { |
| 545 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 546 | "has a null pointer as last input.", |
| 547 | invoke->DebugName(), |
| 548 | invoke->GetId())); |
George Burgess IV | 72155d2 | 2017-04-25 15:17:16 -0700 | [diff] [blame] | 549 | } else if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 550 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 551 | "has a last instruction (%s:%d) which is neither a clinit check " |
| 552 | "nor a load class instruction.", |
| 553 | invoke->DebugName(), |
| 554 | invoke->GetId(), |
| 555 | last_input->DebugName(), |
| 556 | last_input->GetId())); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 561 | void GraphChecker::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 562 | VisitInstruction(ret); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 563 | HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor(); |
| 564 | if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 565 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 566 | ret->DebugName(), |
| 567 | ret->GetId())); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void GraphChecker::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 572 | VisitInstruction(ret); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 573 | HBasicBlock* successor = ret->GetBlock()->GetSingleSuccessor(); |
| 574 | if (!successor->IsExitBlock() && !IsExitTryBoundaryIntoExitBlock(successor)) { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 575 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 576 | ret->DebugName(), |
| 577 | ret->GetId())); |
| 578 | } |
| 579 | } |
| 580 | |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 581 | void GraphChecker::CheckTypeCheckBitstringInput(HTypeCheckInstruction* check, |
| 582 | size_t input_pos, |
| 583 | bool check_value, |
| 584 | uint32_t expected_value, |
| 585 | const char* name) { |
| 586 | if (!check->InputAt(input_pos)->IsIntConstant()) { |
| 587 | AddError(StringPrintf("%s:%d (bitstring) expects a HIntConstant input %zu (%s), not %s:%d.", |
Andreas Gampe | 3fbd3ad | 2018-03-26 21:14:46 +0000 | [diff] [blame] | 588 | check->DebugName(), |
| 589 | check->GetId(), |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 590 | input_pos, |
| 591 | name, |
| 592 | check->InputAt(2)->DebugName(), |
| 593 | check->InputAt(2)->GetId())); |
| 594 | } else if (check_value) { |
| 595 | uint32_t actual_value = |
| 596 | static_cast<uint32_t>(check->InputAt(input_pos)->AsIntConstant()->GetValue()); |
| 597 | if (actual_value != expected_value) { |
| 598 | AddError(StringPrintf("%s:%d (bitstring) has %s 0x%x, not 0x%x as expected.", |
| 599 | check->DebugName(), |
| 600 | check->GetId(), |
| 601 | name, |
| 602 | actual_value, |
| 603 | expected_value)); |
| 604 | } |
Nicolas Geoffray | bff7a52 | 2018-01-25 13:33:07 +0000 | [diff] [blame] | 605 | } |
Vladimir Marko | eb0ebed | 2018-01-10 18:26:38 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 608 | void GraphChecker::HandleTypeCheckInstruction(HTypeCheckInstruction* check) { |
| 609 | VisitInstruction(check); |
| 610 | HInstruction* input = check->InputAt(1); |
| 611 | if (check->GetTypeCheckKind() == TypeCheckKind::kBitstringCheck) { |
| 612 | if (!input->IsNullConstant()) { |
| 613 | AddError(StringPrintf("%s:%d (bitstring) expects a HNullConstant as second input, not %s:%d.", |
| 614 | check->DebugName(), |
| 615 | check->GetId(), |
| 616 | input->DebugName(), |
| 617 | input->GetId())); |
| 618 | } |
| 619 | bool check_values = false; |
| 620 | BitString::StorageType expected_path_to_root = 0u; |
| 621 | BitString::StorageType expected_mask = 0u; |
| 622 | { |
| 623 | ScopedObjectAccess soa(Thread::Current()); |
| 624 | ObjPtr<mirror::Class> klass = check->GetClass().Get(); |
| 625 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 626 | SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass); |
| 627 | if (state == SubtypeCheckInfo::kAssigned) { |
| 628 | expected_path_to_root = |
| 629 | SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass); |
| 630 | expected_mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass); |
| 631 | check_values = true; |
| 632 | } else { |
| 633 | AddError(StringPrintf("%s:%d (bitstring) references a class with unassigned bitstring.", |
| 634 | check->DebugName(), |
| 635 | check->GetId())); |
| 636 | } |
| 637 | } |
| 638 | CheckTypeCheckBitstringInput( |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 639 | check, /* input_pos= */ 2, check_values, expected_path_to_root, "path_to_root"); |
| 640 | CheckTypeCheckBitstringInput(check, /* input_pos= */ 3, check_values, expected_mask, "mask"); |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 641 | } else { |
| 642 | if (!input->IsLoadClass()) { |
| 643 | AddError(StringPrintf("%s:%d (classic) expects a HLoadClass as second input, not %s:%d.", |
| 644 | check->DebugName(), |
| 645 | check->GetId(), |
| 646 | input->DebugName(), |
| 647 | input->GetId())); |
| 648 | } |
Andreas Gampe | 3fbd3ad | 2018-03-26 21:14:46 +0000 | [diff] [blame] | 649 | } |
Vladimir Marko | 3f41323 | 2018-02-12 18:39:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Vladimir Marko | 175e786 | 2018-03-27 09:03:13 +0000 | [diff] [blame] | 652 | void GraphChecker::VisitCheckCast(HCheckCast* check) { |
| 653 | HandleTypeCheckInstruction(check); |
| 654 | } |
| 655 | |
| 656 | void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) { |
| 657 | HandleTypeCheckInstruction(instruction); |
| 658 | } |
| 659 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 660 | void GraphChecker::HandleLoop(HBasicBlock* loop_header) { |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 661 | int id = loop_header->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 662 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 663 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 664 | if (loop_information->GetPreHeader()->GetSuccessors().size() != 1) { |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 665 | AddError(StringPrintf( |
| 666 | "Loop pre-header %d of loop defined by header %d has %zu successors.", |
| 667 | loop_information->GetPreHeader()->GetBlockId(), |
| 668 | id, |
| 669 | loop_information->GetPreHeader()->GetSuccessors().size())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Nicolas Geoffray | 09aa147 | 2016-01-19 10:52:54 +0000 | [diff] [blame] | 672 | if (loop_information->GetSuspendCheck() == nullptr) { |
| 673 | AddError(StringPrintf( |
| 674 | "Loop with header %d does not have a suspend check.", |
| 675 | loop_header->GetBlockId())); |
| 676 | } |
| 677 | |
| 678 | if (loop_information->GetSuspendCheck() != loop_header->GetFirstInstructionDisregardMoves()) { |
| 679 | AddError(StringPrintf( |
| 680 | "Loop header %d does not have the loop suspend check as the first instruction.", |
| 681 | loop_header->GetBlockId())); |
| 682 | } |
| 683 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 684 | // Ensure the loop header has only one incoming branch and the remaining |
| 685 | // predecessors are back edges. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 686 | size_t num_preds = loop_header->GetPredecessors().size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 687 | if (num_preds < 2) { |
| 688 | AddError(StringPrintf( |
| 689 | "Loop header %d has less than two predecessors: %zu.", |
| 690 | id, |
| 691 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 692 | } else { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 693 | HBasicBlock* first_predecessor = loop_header->GetPredecessors()[0]; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 694 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 695 | AddError(StringPrintf( |
| 696 | "First predecessor of loop header %d is a back edge.", |
| 697 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 698 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 699 | 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] | 700 | HBasicBlock* predecessor = loop_header->GetPredecessors()[i]; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 701 | if (!loop_information->IsBackEdge(*predecessor)) { |
| 702 | AddError(StringPrintf( |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 703 | "Loop header %d has multiple incoming (non back edge) blocks: %d.", |
| 704 | id, |
| 705 | predecessor->GetBlockId())); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 706 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 710 | const ArenaBitVector& loop_blocks = loop_information->GetBlocks(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 711 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 712 | // Ensure back edges belong to the loop. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 713 | if (loop_information->NumberOfBackEdges() == 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 714 | AddError(StringPrintf( |
| 715 | "Loop defined by header %d has no back edge.", |
| 716 | id)); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 717 | } else { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 718 | for (HBasicBlock* back_edge : loop_information->GetBackEdges()) { |
| 719 | int back_edge_id = back_edge->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 720 | if (!loop_blocks.IsBitSet(back_edge_id)) { |
| 721 | AddError(StringPrintf( |
| 722 | "Loop defined by header %d has an invalid back edge %d.", |
| 723 | id, |
| 724 | back_edge_id)); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 725 | } else if (back_edge->GetLoopInformation() != loop_information) { |
| 726 | AddError(StringPrintf( |
| 727 | "Back edge %d of loop defined by header %d belongs to nested loop " |
| 728 | "with header %d.", |
| 729 | back_edge_id, |
| 730 | id, |
| 731 | back_edge->GetLoopInformation()->GetHeader()->GetBlockId())); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 732 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 733 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 734 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 735 | |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 736 | // If this is a nested loop, ensure the outer loops contain a superset of the blocks. |
| 737 | for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) { |
| 738 | HLoopInformation* outer_info = it.Current(); |
| 739 | if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) { |
| 740 | AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of " |
| 741 | "an outer loop defined by header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 742 | id, |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 743 | outer_info->GetHeader()->GetBlockId())); |
| 744 | } |
| 745 | } |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 746 | |
| 747 | // Ensure the pre-header block is first in the list of predecessors of a loop |
| 748 | // header and that the header block is its only successor. |
| 749 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
| 750 | AddError(StringPrintf( |
| 751 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 752 | id)); |
| 753 | } |
| 754 | |
| 755 | // Ensure all blocks in the loop are live and dominated by the loop header in |
| 756 | // the case of natural loops. |
| 757 | for (uint32_t i : loop_blocks.Indexes()) { |
| 758 | HBasicBlock* loop_block = GetGraph()->GetBlocks()[i]; |
| 759 | if (loop_block == nullptr) { |
| 760 | AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.", |
| 761 | id, |
| 762 | i)); |
| 763 | } else if (!loop_information->IsIrreducible() && !loop_header->Dominates(loop_block)) { |
| 764 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
| 765 | i, |
| 766 | id)); |
| 767 | } |
| 768 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 771 | static bool IsSameSizeConstant(const HInstruction* insn1, const HInstruction* insn2) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 772 | return insn1->IsConstant() |
| 773 | && insn2->IsConstant() |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 774 | && DataType::Is64BitType(insn1->GetType()) == DataType::Is64BitType(insn2->GetType()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 777 | static bool IsConstantEquivalent(const HInstruction* insn1, |
| 778 | const HInstruction* insn2, |
| 779 | BitVector* visited) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 780 | if (insn1->IsPhi() && |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 781 | insn1->AsPhi()->IsVRegEquivalentOf(insn2)) { |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 782 | HConstInputsRef insn1_inputs = insn1->GetInputs(); |
| 783 | HConstInputsRef insn2_inputs = insn2->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 784 | if (insn1_inputs.size() != insn2_inputs.size()) { |
| 785 | return false; |
| 786 | } |
| 787 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 788 | // Testing only one of the two inputs for recursion is sufficient. |
| 789 | if (visited->IsBitSet(insn1->GetId())) { |
| 790 | return true; |
| 791 | } |
| 792 | visited->SetBit(insn1->GetId()); |
| 793 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 794 | for (size_t i = 0; i < insn1_inputs.size(); ++i) { |
| 795 | if (!IsConstantEquivalent(insn1_inputs[i], insn2_inputs[i], visited)) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
| 798 | } |
| 799 | return true; |
| 800 | } else if (IsSameSizeConstant(insn1, insn2)) { |
| 801 | return insn1->AsConstant()->GetValueAsUint64() == insn2->AsConstant()->GetValueAsUint64(); |
| 802 | } else { |
| 803 | return false; |
| 804 | } |
| 805 | } |
| 806 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 807 | void GraphChecker::VisitPhi(HPhi* phi) { |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 808 | VisitInstruction(phi); |
| 809 | |
| 810 | // Ensure the first input of a phi is not itself. |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 811 | ArrayRef<HUserRecord<HInstruction*>> input_records = phi->GetInputRecords(); |
| 812 | if (input_records[0].GetInstruction() == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 813 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 814 | phi->GetId(), |
| 815 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 818 | // Ensure that the inputs have the same primitive kind as the phi. |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 819 | for (size_t i = 0; i < input_records.size(); ++i) { |
| 820 | HInstruction* input = input_records[i].GetInstruction(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 821 | if (DataType::Kind(input->GetType()) != DataType::Kind(phi->GetType())) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 822 | AddError(StringPrintf( |
| 823 | "Input %d at index %zu of phi %d from block %d does not have the " |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 824 | "same kind as the phi: %s versus %s", |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 825 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 826 | DataType::PrettyDescriptor(input->GetType()), |
| 827 | DataType::PrettyDescriptor(phi->GetType()))); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 828 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 829 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 830 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 831 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 832 | phi->GetId(), |
| 833 | phi->GetBlock()->GetBlockId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 834 | DataType::PrettyDescriptor(phi->GetType()))); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 835 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 836 | |
| 837 | if (phi->IsCatchPhi()) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 838 | // The number of inputs of a catch phi should be the total number of throwing |
| 839 | // instructions caught by this catch block. We do not enforce this, however, |
| 840 | // because we do not remove the corresponding inputs when we prove that an |
| 841 | // instruction cannot throw. Instead, we at least test that all phis have the |
| 842 | // same, non-zero number of inputs (b/24054676). |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 843 | if (input_records.empty()) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 844 | AddError(StringPrintf("Phi %d in catch block %d has zero inputs.", |
| 845 | phi->GetId(), |
| 846 | phi->GetBlock()->GetBlockId())); |
| 847 | } else { |
| 848 | HInstruction* next_phi = phi->GetNext(); |
| 849 | if (next_phi != nullptr) { |
| 850 | size_t input_count_next = next_phi->InputCount(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 851 | if (input_records.size() != input_count_next) { |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 852 | AddError(StringPrintf("Phi %d in catch block %d has %zu inputs, " |
| 853 | "but phi %d has %zu inputs.", |
| 854 | phi->GetId(), |
| 855 | phi->GetBlock()->GetBlockId(), |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 856 | input_records.size(), |
David Brazdil | 3eaa32f | 2015-09-18 10:58:32 +0100 | [diff] [blame] | 857 | next_phi->GetId(), |
| 858 | input_count_next)); |
| 859 | } |
| 860 | } |
| 861 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 862 | } else { |
| 863 | // Ensure the number of inputs of a non-catch phi is the same as the number |
| 864 | // of its predecessors. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 865 | const ArenaVector<HBasicBlock*>& predecessors = phi->GetBlock()->GetPredecessors(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 866 | if (input_records.size() != predecessors.size()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 867 | AddError(StringPrintf( |
| 868 | "Phi %d in block %d has %zu inputs, " |
| 869 | "but block %d has %zu predecessors.", |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 870 | phi->GetId(), phi->GetBlock()->GetBlockId(), input_records.size(), |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 871 | phi->GetBlock()->GetBlockId(), predecessors.size())); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 872 | } else { |
| 873 | // Ensure phi input at index I either comes from the Ith |
| 874 | // predecessor or from a block that dominates this predecessor. |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 875 | for (size_t i = 0; i < input_records.size(); ++i) { |
| 876 | HInstruction* input = input_records[i].GetInstruction(); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 877 | HBasicBlock* predecessor = predecessors[i]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 878 | if (!(input->GetBlock() == predecessor |
| 879 | || input->GetBlock()->Dominates(predecessor))) { |
| 880 | AddError(StringPrintf( |
| 881 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 882 | "predecessor number %zu nor in a block dominating it.", |
| 883 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 884 | i)); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 889 | |
| 890 | // Ensure that catch phis are sorted by their vreg number, as required by |
| 891 | // the register allocator and code generator. This does not apply to normal |
| 892 | // phis which can be constructed artifically. |
| 893 | if (phi->IsCatchPhi()) { |
| 894 | HInstruction* next_phi = phi->GetNext(); |
| 895 | if (next_phi != nullptr && phi->GetRegNumber() > next_phi->AsPhi()->GetRegNumber()) { |
| 896 | AddError(StringPrintf("Catch phis %d and %d in block %d are not sorted by their " |
| 897 | "vreg numbers.", |
| 898 | phi->GetId(), |
| 899 | next_phi->GetId(), |
| 900 | phi->GetBlock()->GetBlockId())); |
| 901 | } |
| 902 | } |
| 903 | |
Aart Bik | 3fc7f35 | 2015-11-20 22:03:03 -0800 | [diff] [blame] | 904 | // Test phi equivalents. There should not be two of the same type and they should only be |
| 905 | // created for constants which were untyped in DEX. Note that this test can be skipped for |
| 906 | // a synthetic phi (indicated by lack of a virtual register). |
| 907 | if (phi->GetRegNumber() != kNoRegNumber) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 908 | for (HInstructionIterator phi_it(phi->GetBlock()->GetPhis()); |
| 909 | !phi_it.Done(); |
| 910 | phi_it.Advance()) { |
Aart Bik | 3fc7f35 | 2015-11-20 22:03:03 -0800 | [diff] [blame] | 911 | HPhi* other_phi = phi_it.Current()->AsPhi(); |
| 912 | if (phi != other_phi && phi->GetRegNumber() == other_phi->GetRegNumber()) { |
| 913 | if (phi->GetType() == other_phi->GetType()) { |
| 914 | std::stringstream type_str; |
| 915 | type_str << phi->GetType(); |
| 916 | AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s.", |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 917 | phi->GetId(), |
Aart Bik | 3fc7f35 | 2015-11-20 22:03:03 -0800 | [diff] [blame] | 918 | phi->GetRegNumber(), |
| 919 | type_str.str().c_str())); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 920 | } else if (phi->GetType() == DataType::Type::kReference) { |
Nicolas Geoffray | f5f64ef | 2015-12-15 14:11:59 +0000 | [diff] [blame] | 921 | std::stringstream type_str; |
| 922 | type_str << other_phi->GetType(); |
| 923 | AddError(StringPrintf( |
| 924 | "Equivalent non-reference phi (%d) found for VReg %d with type: %s.", |
| 925 | phi->GetId(), |
| 926 | phi->GetRegNumber(), |
| 927 | type_str.str().c_str())); |
Aart Bik | 3fc7f35 | 2015-11-20 22:03:03 -0800 | [diff] [blame] | 928 | } else { |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 929 | // Use local allocator for allocating memory. |
| 930 | ScopedArenaAllocator allocator(GetGraph()->GetArenaStack()); |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 931 | // If we get here, make sure we allocate all the necessary storage at once |
| 932 | // because the BitVector reallocation strategy has very bad worst-case behavior. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 933 | ArenaBitVector visited(&allocator, |
| 934 | GetGraph()->GetCurrentInstructionId(), |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 935 | /* expandable= */ false, |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 936 | kArenaAllocGraphChecker); |
Vladimir Marko | 947eb70 | 2016-03-25 15:31:35 +0000 | [diff] [blame] | 937 | visited.ClearAllBits(); |
Aart Bik | 3fc7f35 | 2015-11-20 22:03:03 -0800 | [diff] [blame] | 938 | if (!IsConstantEquivalent(phi, other_phi, &visited)) { |
| 939 | AddError(StringPrintf("Two phis (%d and %d) found for VReg %d but they " |
| 940 | "are not equivalents of constants.", |
| 941 | phi->GetId(), |
| 942 | other_phi->GetId(), |
| 943 | phi->GetRegNumber())); |
| 944 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 949 | } |
| 950 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 951 | void GraphChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 952 | HInstruction* input = instruction->InputAt(input_index); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 953 | if (input->IsIntConstant()) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 954 | int32_t value = input->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 955 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 956 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 957 | "%s instruction %d has a non-Boolean constant input %d whose value is: %d.", |
| 958 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 959 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 960 | static_cast<int>(input_index), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 961 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 962 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 963 | } else if (DataType::Kind(input->GetType()) != DataType::Type::kInt32) { |
David Brazdil | 11edec7 | 2016-03-24 12:40:52 +0000 | [diff] [blame] | 964 | // TODO: We need a data-flow analysis to determine if an input like Phi, |
| 965 | // Select or a binary operation is actually Boolean. Allow for now. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 966 | AddError(StringPrintf( |
David Brazdil | 11edec7 | 2016-03-24 12:40:52 +0000 | [diff] [blame] | 967 | "%s instruction %d has a non-integer input %d whose type is: %s.", |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 968 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 969 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 970 | static_cast<int>(input_index), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 971 | DataType::PrettyDescriptor(input->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 975 | void GraphChecker::VisitPackedSwitch(HPackedSwitch* instruction) { |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 976 | VisitInstruction(instruction); |
| 977 | // Check that the number of block successors matches the switch count plus |
| 978 | // one for the default block. |
| 979 | HBasicBlock* block = instruction->GetBlock(); |
| 980 | if (instruction->GetNumEntries() + 1u != block->GetSuccessors().size()) { |
| 981 | AddError(StringPrintf( |
| 982 | "%s instruction %d in block %d expects %u successors to the block, but found: %zu.", |
| 983 | instruction->DebugName(), |
| 984 | instruction->GetId(), |
| 985 | block->GetBlockId(), |
| 986 | instruction->GetNumEntries() + 1u, |
| 987 | block->GetSuccessors().size())); |
| 988 | } |
| 989 | } |
| 990 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 991 | void GraphChecker::VisitIf(HIf* instruction) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 992 | VisitInstruction(instruction); |
| 993 | HandleBooleanInput(instruction, 0); |
| 994 | } |
| 995 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 996 | void GraphChecker::VisitSelect(HSelect* instruction) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 997 | VisitInstruction(instruction); |
| 998 | HandleBooleanInput(instruction, 2); |
| 999 | } |
| 1000 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1001 | void GraphChecker::VisitBooleanNot(HBooleanNot* instruction) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 1002 | VisitInstruction(instruction); |
| 1003 | HandleBooleanInput(instruction, 0); |
| 1004 | } |
| 1005 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1006 | void GraphChecker::VisitCondition(HCondition* op) { |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1007 | VisitInstruction(op); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1008 | if (op->GetType() != DataType::Type::kBool) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 1009 | AddError(StringPrintf( |
| 1010 | "Condition %s %d has a non-Boolean result type: %s.", |
| 1011 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1012 | DataType::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1013 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 1014 | HInstruction* lhs = op->InputAt(0); |
| 1015 | HInstruction* rhs = op->InputAt(1); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1016 | if (DataType::Kind(lhs->GetType()) != DataType::Kind(rhs->GetType())) { |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 1017 | AddError(StringPrintf( |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 1018 | "Condition %s %d has inputs of different kinds: %s, and %s.", |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 1019 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1020 | DataType::PrettyDescriptor(lhs->GetType()), |
| 1021 | DataType::PrettyDescriptor(rhs->GetType()))); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 1022 | } |
| 1023 | if (!op->IsEqual() && !op->IsNotEqual()) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1024 | if ((lhs->GetType() == DataType::Type::kReference)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 1025 | AddError(StringPrintf( |
| 1026 | "Condition %s %d uses an object as left-hand side input.", |
| 1027 | op->DebugName(), op->GetId())); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1028 | } else if (rhs->GetType() == DataType::Type::kReference) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 1029 | AddError(StringPrintf( |
| 1030 | "Condition %s %d uses an object as right-hand side input.", |
| 1031 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 1032 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 1033 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Roland Levillain | 937e6cd | 2016-03-22 11:54:37 +0000 | [diff] [blame] | 1036 | void GraphChecker::VisitNeg(HNeg* instruction) { |
| 1037 | VisitInstruction(instruction); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1038 | DataType::Type input_type = instruction->InputAt(0)->GetType(); |
| 1039 | DataType::Type result_type = instruction->GetType(); |
| 1040 | if (result_type != DataType::Kind(input_type)) { |
Roland Levillain | 937e6cd | 2016-03-22 11:54:37 +0000 | [diff] [blame] | 1041 | AddError(StringPrintf("Binary operation %s %d has a result type different " |
| 1042 | "from its input kind: %s vs %s.", |
| 1043 | instruction->DebugName(), instruction->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1044 | DataType::PrettyDescriptor(result_type), |
| 1045 | DataType::PrettyDescriptor(input_type))); |
Roland Levillain | 937e6cd | 2016-03-22 11:54:37 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1049 | void GraphChecker::VisitBinaryOperation(HBinaryOperation* op) { |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1050 | VisitInstruction(op); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1051 | DataType::Type lhs_type = op->InputAt(0)->GetType(); |
| 1052 | DataType::Type rhs_type = op->InputAt(1)->GetType(); |
| 1053 | DataType::Type result_type = op->GetType(); |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Type consistency between inputs. |
Scott Wakeling | 40a04bf | 2015-12-11 09:50:36 +0000 | [diff] [blame] | 1056 | if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1057 | if (DataType::Kind(rhs_type) != DataType::Type::kInt32) { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1058 | AddError(StringPrintf("Shift/rotate operation %s %d has a non-int kind second input: " |
| 1059 | "%s of type %s.", |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 1060 | op->DebugName(), op->GetId(), |
| 1061 | op->InputAt(1)->DebugName(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1062 | DataType::PrettyDescriptor(rhs_type))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1065 | if (DataType::Kind(lhs_type) != DataType::Kind(rhs_type)) { |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 1066 | AddError(StringPrintf("Binary operation %s %d has inputs of different kinds: %s, and %s.", |
| 1067 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1068 | DataType::PrettyDescriptor(lhs_type), |
| 1069 | DataType::PrettyDescriptor(rhs_type))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1073 | // Type consistency between result and input(s). |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1074 | if (op->IsCompare()) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1075 | if (result_type != DataType::Type::kInt32) { |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 1076 | AddError(StringPrintf("Compare operation %d has a non-int result type: %s.", |
| 1077 | op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1078 | DataType::PrettyDescriptor(result_type))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1079 | } |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1080 | } else if (op->IsUShr() || op->IsShr() || op->IsShl() || op->IsRor()) { |
| 1081 | // Only check the first input (value), as the second one (distance) |
| 1082 | // must invariably be of kind `int`. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1083 | if (result_type != DataType::Kind(lhs_type)) { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1084 | AddError(StringPrintf("Shift/rotate operation %s %d has a result type different " |
| 1085 | "from its left-hand side (value) input kind: %s vs %s.", |
Roland Levillain | a5c4a40 | 2016-03-15 15:02:50 +0000 | [diff] [blame] | 1086 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1087 | DataType::PrettyDescriptor(result_type), |
| 1088 | DataType::PrettyDescriptor(lhs_type))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1089 | } |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1090 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1091 | if (DataType::Kind(result_type) != DataType::Kind(lhs_type)) { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1092 | AddError(StringPrintf("Binary operation %s %d has a result kind different " |
| 1093 | "from its left-hand side input kind: %s vs %s.", |
| 1094 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1095 | DataType::PrettyDescriptor(result_type), |
| 1096 | DataType::PrettyDescriptor(lhs_type))); |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1097 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1098 | if (DataType::Kind(result_type) != DataType::Kind(rhs_type)) { |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1099 | AddError(StringPrintf("Binary operation %s %d has a result kind different " |
| 1100 | "from its right-hand side input kind: %s vs %s.", |
| 1101 | op->DebugName(), op->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1102 | DataType::PrettyDescriptor(result_type), |
| 1103 | DataType::PrettyDescriptor(rhs_type))); |
Roland Levillain | 5b5b931 | 2016-03-22 14:57:31 +0000 | [diff] [blame] | 1104 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1108 | void GraphChecker::VisitConstant(HConstant* instruction) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1109 | HBasicBlock* block = instruction->GetBlock(); |
| 1110 | if (!block->IsEntryBlock()) { |
| 1111 | AddError(StringPrintf( |
| 1112 | "%s %d should be in the entry block but is in block %d.", |
| 1113 | instruction->DebugName(), |
| 1114 | instruction->GetId(), |
| 1115 | block->GetBlockId())); |
| 1116 | } |
| 1117 | } |
| 1118 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1119 | void GraphChecker::VisitBoundType(HBoundType* instruction) { |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 1120 | VisitInstruction(instruction); |
| 1121 | |
David Brazdil | f555258 | 2015-12-27 13:36:12 +0000 | [diff] [blame] | 1122 | if (!instruction->GetUpperBound().IsValid()) { |
| 1123 | AddError(StringPrintf( |
| 1124 | "%s %d does not have a valid upper bound RTI.", |
| 1125 | instruction->DebugName(), |
| 1126 | instruction->GetId())); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Roland Levillain | f355c3f | 2016-03-30 19:09:03 +0100 | [diff] [blame] | 1130 | void GraphChecker::VisitTypeConversion(HTypeConversion* instruction) { |
| 1131 | VisitInstruction(instruction); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1132 | DataType::Type result_type = instruction->GetResultType(); |
| 1133 | DataType::Type input_type = instruction->GetInputType(); |
Roland Levillain | f355c3f | 2016-03-30 19:09:03 +0100 | [diff] [blame] | 1134 | // Invariant: We should never generate a conversion to a Boolean value. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1135 | if (result_type == DataType::Type::kBool) { |
Roland Levillain | f355c3f | 2016-03-30 19:09:03 +0100 | [diff] [blame] | 1136 | AddError(StringPrintf( |
| 1137 | "%s %d converts to a %s (from a %s).", |
| 1138 | instruction->DebugName(), |
| 1139 | instruction->GetId(), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1140 | DataType::PrettyDescriptor(result_type), |
| 1141 | DataType::PrettyDescriptor(input_type))); |
Roland Levillain | f355c3f | 2016-03-30 19:09:03 +0100 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Artem Serov | 0771884 | 2020-02-24 18:51:42 +0000 | [diff] [blame] | 1145 | void GraphChecker::VisitVecOperation(HVecOperation* instruction) { |
| 1146 | VisitInstruction(instruction); |
| 1147 | if (codegen_ == nullptr) { |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | if (!codegen_->SupportsPredicatedSIMD() && instruction->IsPredicated()) { |
| 1152 | AddError(StringPrintf( |
| 1153 | "%s %d must not be predicated.", |
| 1154 | instruction->DebugName(), |
| 1155 | instruction->GetId())); |
| 1156 | } |
| 1157 | |
| 1158 | if (codegen_->SupportsPredicatedSIMD() && |
| 1159 | (instruction->MustBePredicatedInPredicatedSIMDMode() != instruction->IsPredicated())) { |
| 1160 | AddError(StringPrintf( |
| 1161 | "%s %d predication mode is incorrect; see HVecOperation::MustBePredicated.", |
| 1162 | instruction->DebugName(), |
| 1163 | instruction->GetId())); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1167 | } // namespace art |