Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "graph_checker.h" |
| 18 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 19 | #include <map> |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 20 | #include <string> |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 21 | #include <sstream> |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 22 | |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 23 | #include "base/bit_vector-inl.h" |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 24 | #include "base/stringprintf.h" |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 25 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 26 | namespace art { |
| 27 | |
| 28 | void GraphChecker::VisitBasicBlock(HBasicBlock* block) { |
| 29 | current_block_ = block; |
| 30 | |
| 31 | // Check consistency with respect to predecessors of `block`. |
| 32 | const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 33 | std::map<HBasicBlock*, size_t> predecessors_count; |
| 34 | for (size_t i = 0, e = predecessors.Size(); i < e; ++i) { |
| 35 | HBasicBlock* p = predecessors.Get(i); |
| 36 | ++predecessors_count[p]; |
| 37 | } |
| 38 | for (auto& pc : predecessors_count) { |
| 39 | HBasicBlock* p = pc.first; |
| 40 | size_t p_count_in_block_predecessors = pc.second; |
| 41 | const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors(); |
| 42 | size_t block_count_in_p_successors = 0; |
| 43 | for (size_t j = 0, f = p_successors.Size(); j < f; ++j) { |
| 44 | if (p_successors.Get(j) == block) { |
| 45 | ++block_count_in_p_successors; |
| 46 | } |
| 47 | } |
| 48 | if (p_count_in_block_predecessors != block_count_in_p_successors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 49 | AddError(StringPrintf( |
| 50 | "Block %d lists %zu occurrences of block %d in its predecessors, whereas " |
| 51 | "block %d lists %zu occurrences of block %d in its successors.", |
| 52 | block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(), |
| 53 | p->GetBlockId(), block_count_in_p_successors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | // Check consistency with respect to successors of `block`. |
| 58 | const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors(); |
| 59 | std::map<HBasicBlock*, size_t> successors_count; |
| 60 | for (size_t i = 0, e = successors.Size(); i < e; ++i) { |
| 61 | HBasicBlock* s = successors.Get(i); |
| 62 | ++successors_count[s]; |
| 63 | } |
| 64 | for (auto& sc : successors_count) { |
| 65 | HBasicBlock* s = sc.first; |
| 66 | size_t s_count_in_block_successors = sc.second; |
| 67 | const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors(); |
| 68 | size_t block_count_in_s_predecessors = 0; |
| 69 | for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) { |
| 70 | if (s_predecessors.Get(j) == block) { |
| 71 | ++block_count_in_s_predecessors; |
| 72 | } |
| 73 | } |
| 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 | |
| 92 | // Visit this block's list of phis. |
| 93 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 94 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 95 | // Ensure this block's list of phis contains only phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 96 | if (!current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 97 | AddError(StringPrintf("Block %d has a non-phi in its phi list.", |
| 98 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 99 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 100 | if (current->GetNext() == nullptr && current != block->GetLastPhi()) { |
| 101 | AddError(StringPrintf("The recorded last phi of block %d does not match " |
| 102 | "the actual last phi %d.", |
| 103 | current_block_->GetBlockId(), |
| 104 | current->GetId())); |
| 105 | } |
| 106 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Visit this block's list of instructions. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 110 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 111 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 112 | // Ensure this block's list of instructions does not contains phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 113 | if (current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 114 | AddError(StringPrintf("Block %d has a phi in its non-phi list.", |
| 115 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 116 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 117 | if (current->GetNext() == nullptr && current != block->GetLastInstruction()) { |
| 118 | AddError(StringPrintf("The recorded last instruction of block %d does not match " |
| 119 | "the actual last instruction %d.", |
| 120 | current_block_->GetBlockId(), |
| 121 | current->GetId())); |
| 122 | } |
| 123 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 127 | void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) { |
| 128 | if (!GetGraph()->HasBoundsChecks()) { |
| 129 | AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, " |
| 130 | "but HasBoundsChecks() returns false", |
| 131 | check->DebugName(), |
| 132 | check->GetId())); |
| 133 | } |
| 134 | |
| 135 | // Perform the instruction base checks too. |
| 136 | VisitInstruction(check); |
| 137 | } |
| 138 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 139 | void GraphChecker::VisitTryBoundary(HTryBoundary* try_boundary) { |
| 140 | // Ensure that all exception handlers are catch blocks and that handlers |
| 141 | // are not listed multiple times. |
| 142 | // Note that a normal-flow successor may be a catch block before CFG |
| 143 | // simplification. We only test normal-flow successors in SsaChecker. |
| 144 | for (HExceptionHandlerIterator it(*try_boundary); !it.Done(); it.Advance()) { |
| 145 | HBasicBlock* handler = it.Current(); |
| 146 | if (!handler->IsCatchBlock()) { |
| 147 | AddError(StringPrintf("Block %d with %s:%d has exceptional successor %d which " |
| 148 | "is not a catch block.", |
| 149 | current_block_->GetBlockId(), |
| 150 | try_boundary->DebugName(), |
| 151 | try_boundary->GetId(), |
| 152 | handler->GetBlockId())); |
| 153 | } |
| 154 | if (current_block_->GetSuccessors().Contains( |
| 155 | handler, /* start_from */ it.CurrentSuccessorIndex() + 1)) { |
| 156 | AddError(StringPrintf("Exception handler block %d of %s:%d is listed multiple times.", |
| 157 | handler->GetBlockId(), |
| 158 | try_boundary->DebugName(), |
| 159 | try_boundary->GetId())); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | VisitInstruction(try_boundary); |
| 164 | } |
| 165 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 166 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 167 | if (seen_ids_.IsBitSet(instruction->GetId())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 168 | AddError(StringPrintf("Instruction id %d is duplicate in graph.", |
| 169 | instruction->GetId())); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 170 | } else { |
| 171 | seen_ids_.SetBit(instruction->GetId()); |
| 172 | } |
| 173 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 174 | // Ensure `instruction` is associated with `current_block_`. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 175 | if (instruction->GetBlock() == nullptr) { |
| 176 | AddError(StringPrintf("%s %d in block %d not associated with any block.", |
| 177 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 178 | instruction->GetId(), |
| 179 | current_block_->GetBlockId())); |
| 180 | } else if (instruction->GetBlock() != current_block_) { |
| 181 | AddError(StringPrintf("%s %d in block %d associated with block %d.", |
| 182 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 183 | instruction->GetId(), |
| 184 | current_block_->GetBlockId(), |
| 185 | instruction->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 186 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 187 | |
| 188 | // Ensure the inputs of `instruction` are defined in a block of the graph. |
| 189 | for (HInputIterator input_it(instruction); !input_it.Done(); |
| 190 | input_it.Advance()) { |
| 191 | HInstruction* input = input_it.Current(); |
| 192 | const HInstructionList& list = input->IsPhi() |
| 193 | ? input->GetBlock()->GetPhis() |
| 194 | : input->GetBlock()->GetInstructions(); |
| 195 | if (!list.Contains(input)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 196 | AddError(StringPrintf("Input %d of instruction %d is not defined " |
| 197 | "in a basic block of the control-flow graph.", |
| 198 | input->GetId(), |
| 199 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 203 | // Ensure the uses of `instruction` are defined in a block of the graph, |
| 204 | // and the entry in the use list is consistent. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 205 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 206 | !use_it.Done(); use_it.Advance()) { |
| 207 | HInstruction* use = use_it.Current()->GetUser(); |
| 208 | const HInstructionList& list = use->IsPhi() |
| 209 | ? use->GetBlock()->GetPhis() |
| 210 | : use->GetBlock()->GetInstructions(); |
| 211 | if (!list.Contains(use)) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 212 | AddError(StringPrintf("User %s:%d of instruction %d is not defined " |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 213 | "in a basic block of the control-flow graph.", |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 214 | use->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 215 | use->GetId(), |
| 216 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 217 | } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 218 | size_t use_index = use_it.Current()->GetIndex(); |
| 219 | if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) { |
| 220 | AddError(StringPrintf("User %s:%d of instruction %d has a wrong " |
| 221 | "UseListNode index.", |
| 222 | use->DebugName(), |
| 223 | use->GetId(), |
| 224 | instruction->GetId())); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Ensure the environment uses entries are consistent. |
| 229 | for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses()); |
| 230 | !use_it.Done(); use_it.Advance()) { |
| 231 | HEnvironment* use = use_it.Current()->GetUser(); |
| 232 | size_t use_index = use_it.Current()->GetIndex(); |
| 233 | if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) { |
| 234 | AddError(StringPrintf("Environment user of %s:%d has a wrong " |
| 235 | "UseListNode index.", |
| 236 | instruction->DebugName(), |
| 237 | instruction->GetId())); |
| 238 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 239 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 240 | |
| 241 | // Ensure 'instruction' has pointers to its inputs' use entries. |
| 242 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 243 | HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i); |
| 244 | HInstruction* input = input_record.GetInstruction(); |
| 245 | HUseListNode<HInstruction*>* use_node = input_record.GetUseNode(); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 246 | size_t use_index = use_node->GetIndex(); |
| 247 | if ((use_node == nullptr) |
| 248 | || !input->GetUses().Contains(use_node) |
| 249 | || (use_index >= e) |
| 250 | || (use_index != i)) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 251 | AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry " |
| 252 | "at input %u (%s:%d).", |
| 253 | instruction->DebugName(), |
| 254 | instruction->GetId(), |
| 255 | static_cast<unsigned>(i), |
| 256 | input->DebugName(), |
| 257 | input->GetId())); |
| 258 | } |
| 259 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 262 | void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
| 263 | VisitInstruction(invoke); |
| 264 | |
| 265 | if (invoke->IsStaticWithExplicitClinitCheck()) { |
| 266 | size_t last_input_index = invoke->InputCount() - 1; |
| 267 | HInstruction* last_input = invoke->InputAt(last_input_index); |
| 268 | if (last_input == nullptr) { |
| 269 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 270 | "has a null pointer as last input.", |
| 271 | invoke->DebugName(), |
| 272 | invoke->GetId())); |
| 273 | } |
| 274 | if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) { |
| 275 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 276 | "has a last instruction (%s:%d) which is neither a clinit check " |
| 277 | "nor a load class instruction.", |
| 278 | invoke->DebugName(), |
| 279 | invoke->GetId(), |
| 280 | last_input->DebugName(), |
| 281 | last_input->GetId())); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 286 | void GraphChecker::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 287 | VisitInstruction(ret); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 288 | if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) { |
| 289 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 290 | ret->DebugName(), |
| 291 | ret->GetId())); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void GraphChecker::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 296 | VisitInstruction(ret); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 297 | if (!ret->GetBlock()->GetSingleSuccessor()->IsExitBlock()) { |
| 298 | AddError(StringPrintf("%s:%d does not jump to the exit block.", |
| 299 | ret->DebugName(), |
| 300 | ret->GetId())); |
| 301 | } |
| 302 | } |
| 303 | |
Nicolas Geoffray | f9a1995 | 2015-06-29 13:43:54 +0100 | [diff] [blame] | 304 | void GraphChecker::VisitCheckCast(HCheckCast* check) { |
| 305 | VisitInstruction(check); |
| 306 | HInstruction* input = check->InputAt(1); |
| 307 | if (!input->IsLoadClass()) { |
| 308 | AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.", |
| 309 | check->DebugName(), |
| 310 | check->GetId(), |
| 311 | input->DebugName(), |
| 312 | input->GetId())); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void GraphChecker::VisitInstanceOf(HInstanceOf* instruction) { |
| 317 | VisitInstruction(instruction); |
| 318 | HInstruction* input = instruction->InputAt(1); |
| 319 | if (!input->IsLoadClass()) { |
| 320 | AddError(StringPrintf("%s:%d expects a HLoadClass as second input, not %s:%d.", |
| 321 | instruction->DebugName(), |
| 322 | instruction->GetId(), |
| 323 | input->DebugName(), |
| 324 | input->GetId())); |
| 325 | } |
| 326 | } |
| 327 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 328 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 329 | super_type::VisitBasicBlock(block); |
| 330 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 331 | // Ensure that catch blocks are not normal successors, and normal blocks are |
| 332 | // never exceptional successors. |
| 333 | const size_t num_normal_successors = block->NumberOfNormalSuccessors(); |
| 334 | for (size_t j = 0; j < num_normal_successors; ++j) { |
| 335 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 336 | if (successor->IsCatchBlock()) { |
| 337 | AddError(StringPrintf("Catch block %d is a normal successor of block %d.", |
| 338 | successor->GetBlockId(), |
| 339 | block->GetBlockId())); |
| 340 | } |
| 341 | } |
| 342 | for (size_t j = num_normal_successors, e = block->GetSuccessors().Size(); j < e; ++j) { |
| 343 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 344 | if (!successor->IsCatchBlock()) { |
| 345 | AddError(StringPrintf("Normal block %d is an exceptional successor of block %d.", |
| 346 | successor->GetBlockId(), |
| 347 | block->GetBlockId())); |
| 348 | } |
| 349 | } |
| 350 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 351 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 352 | // block with multiple successors to a block with multiple |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 353 | // predecessors). Exceptional edges are synthesized and hence |
| 354 | // not accounted for. |
| 355 | if (block->NumberOfNormalSuccessors() > 1) { |
| 356 | for (size_t j = 0, e = block->NumberOfNormalSuccessors(); j < e; ++j) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 357 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 358 | if (successor->GetPredecessors().Size() > 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 359 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 360 | block->GetBlockId(), |
| 361 | successor->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 365 | |
Calin Juravle | 1d8b49f | 2015-05-12 12:40:07 +0000 | [diff] [blame] | 366 | // Check Phi uniqueness (no two Phis with the same type refer to the same register). |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 367 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 368 | HPhi* phi = it.Current()->AsPhi(); |
| 369 | if (phi->GetNextEquivalentPhiWithSameType() != nullptr) { |
| 370 | std::stringstream type_str; |
| 371 | type_str << phi->GetType(); |
| 372 | AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s", |
| 373 | phi->GetId(), phi->GetRegNumber(), type_str.str().c_str())); |
| 374 | } |
| 375 | } |
| 376 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 377 | // Ensure try membership information is consistent. |
| 378 | HTryBoundary* try_entry = block->GetTryEntry(); |
| 379 | if (block->IsCatchBlock()) { |
| 380 | if (try_entry != nullptr) { |
| 381 | AddError(StringPrintf("Catch blocks should not be try blocks but catch block %d " |
| 382 | "has try entry %s:%d.", |
| 383 | block->GetBlockId(), |
| 384 | try_entry->DebugName(), |
| 385 | try_entry->GetId())); |
| 386 | } |
| 387 | |
| 388 | if (block->IsLoopHeader()) { |
| 389 | AddError(StringPrintf("Catch blocks should not be loop headers but catch block %d is.", |
| 390 | block->GetBlockId())); |
| 391 | } |
| 392 | } else { |
| 393 | for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) { |
| 394 | HBasicBlock* predecessor = block->GetPredecessors().Get(i); |
| 395 | HTryBoundary* incoming_try_entry = predecessor->ComputeTryEntryOfSuccessors(); |
| 396 | if (try_entry == nullptr) { |
| 397 | if (incoming_try_entry != nullptr) { |
| 398 | AddError(StringPrintf("Block %d has no try entry but try entry %s:%d follows " |
| 399 | "from predecessor %d.", |
| 400 | block->GetBlockId(), |
| 401 | incoming_try_entry->DebugName(), |
| 402 | incoming_try_entry->GetId(), |
| 403 | predecessor->GetBlockId())); |
| 404 | } |
| 405 | } else if (incoming_try_entry == nullptr) { |
| 406 | AddError(StringPrintf("Block %d has try entry %s:%d but no try entry follows " |
| 407 | "from predecessor %d.", |
| 408 | block->GetBlockId(), |
| 409 | try_entry->DebugName(), |
| 410 | try_entry->GetId(), |
| 411 | predecessor->GetBlockId())); |
| 412 | } else if (!incoming_try_entry->HasSameExceptionHandlersAs(*try_entry)) { |
| 413 | AddError(StringPrintf("Block %d has try entry %s:%d which is not consistent " |
| 414 | "with %s:%d that follows from predecessor %d.", |
| 415 | block->GetBlockId(), |
| 416 | try_entry->DebugName(), |
| 417 | try_entry->GetId(), |
| 418 | incoming_try_entry->DebugName(), |
| 419 | incoming_try_entry->GetId(), |
| 420 | predecessor->GetBlockId())); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 425 | if (block->IsLoopHeader()) { |
| 426 | CheckLoop(block); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 431 | int id = loop_header->GetBlockId(); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 432 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 433 | |
| 434 | // Ensure the pre-header block is first in the list of |
| 435 | // predecessors of a loop header. |
| 436 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 437 | AddError(StringPrintf( |
| 438 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 439 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 442 | // Ensure the loop header has only one incoming branch and the remaining |
| 443 | // predecessors are back edges. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 444 | size_t num_preds = loop_header->GetPredecessors().Size(); |
| 445 | if (num_preds < 2) { |
| 446 | AddError(StringPrintf( |
| 447 | "Loop header %d has less than two predecessors: %zu.", |
| 448 | id, |
| 449 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 450 | } else { |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 451 | HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 452 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 453 | AddError(StringPrintf( |
| 454 | "First predecessor of loop header %d is a back edge.", |
| 455 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 456 | } |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 457 | for (size_t i = 1, e = loop_header->GetPredecessors().Size(); i < e; ++i) { |
| 458 | HBasicBlock* predecessor = loop_header->GetPredecessors().Get(i); |
| 459 | if (!loop_information->IsBackEdge(*predecessor)) { |
| 460 | AddError(StringPrintf( |
| 461 | "Loop header %d has multiple incoming (non back edge) blocks.", |
| 462 | id)); |
| 463 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 467 | const ArenaBitVector& loop_blocks = loop_information->GetBlocks(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 468 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 469 | // Ensure back edges belong to the loop. |
| 470 | size_t num_back_edges = loop_information->GetBackEdges().Size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 471 | if (num_back_edges == 0) { |
| 472 | AddError(StringPrintf( |
| 473 | "Loop defined by header %d has no back edge.", |
| 474 | id)); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 475 | } else { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 476 | for (size_t i = 0; i < num_back_edges; ++i) { |
| 477 | int back_edge_id = loop_information->GetBackEdges().Get(i)->GetBlockId(); |
| 478 | if (!loop_blocks.IsBitSet(back_edge_id)) { |
| 479 | AddError(StringPrintf( |
| 480 | "Loop defined by header %d has an invalid back edge %d.", |
| 481 | id, |
| 482 | back_edge_id)); |
| 483 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 484 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 485 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 486 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 487 | // 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] | 488 | for (uint32_t i : loop_blocks.Indexes()) { |
| 489 | HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 490 | if (loop_block == nullptr) { |
| 491 | AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.", |
| 492 | id, |
| 493 | i)); |
| 494 | } else if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 495 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 496 | i, |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 497 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 500 | |
| 501 | // If this is a nested loop, ensure the outer loops contain a superset of the blocks. |
| 502 | for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) { |
| 503 | HLoopInformation* outer_info = it.Current(); |
| 504 | if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) { |
| 505 | AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of " |
| 506 | "an outer loop defined by header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 507 | id, |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 508 | outer_info->GetHeader()->GetBlockId())); |
| 509 | } |
| 510 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 514 | super_type::VisitInstruction(instruction); |
| 515 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 516 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 517 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 518 | !use_it.Done(); use_it.Advance()) { |
| 519 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 520 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 521 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 522 | "use %d in block %d.", |
| 523 | instruction->GetId(), current_block_->GetBlockId(), |
| 524 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 525 | } |
| 526 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 527 | |
| 528 | // Ensure an instruction having an environment is dominated by the |
| 529 | // instructions contained in the environment. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 530 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 531 | environment != nullptr; |
| 532 | environment = environment->GetParent()) { |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 533 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 534 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 535 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 536 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 537 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 538 | "from block %d does not dominate instruction %d.", |
| 539 | env_instruction->GetId(), |
| 540 | instruction->GetId(), |
| 541 | current_block_->GetBlockId(), |
| 542 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 548 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 549 | switch (type) { |
| 550 | case Primitive::kPrimBoolean: |
| 551 | case Primitive::kPrimByte: |
| 552 | case Primitive::kPrimShort: |
| 553 | case Primitive::kPrimChar: |
| 554 | case Primitive::kPrimInt: |
| 555 | return Primitive::kPrimInt; |
| 556 | default: |
| 557 | return type; |
| 558 | } |
| 559 | } |
| 560 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 561 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 562 | VisitInstruction(phi); |
| 563 | |
| 564 | // Ensure the first input of a phi is not itself. |
| 565 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 566 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 567 | phi->GetId(), |
| 568 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 571 | // Ensure that the inputs have the same primitive kind as the phi. |
| 572 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 573 | HInstruction* input = phi->InputAt(i); |
| 574 | if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) { |
| 575 | AddError(StringPrintf( |
| 576 | "Input %d at index %zu of phi %d from block %d does not have the " |
| 577 | "same type as the phi: %s versus %s", |
| 578 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 579 | Primitive::PrettyDescriptor(input->GetType()), |
| 580 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 581 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 582 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 583 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 584 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 585 | phi->GetId(), |
| 586 | phi->GetBlock()->GetBlockId(), |
| 587 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 588 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 589 | |
| 590 | if (phi->IsCatchPhi()) { |
| 591 | // The number of inputs of a catch phi corresponds to the total number of |
| 592 | // throwing instructions caught by this catch block. |
| 593 | } else { |
| 594 | // Ensure the number of inputs of a non-catch phi is the same as the number |
| 595 | // of its predecessors. |
| 596 | const GrowableArray<HBasicBlock*>& predecessors = |
| 597 | phi->GetBlock()->GetPredecessors(); |
| 598 | if (phi->InputCount() != predecessors.Size()) { |
| 599 | AddError(StringPrintf( |
| 600 | "Phi %d in block %d has %zu inputs, " |
| 601 | "but block %d has %zu predecessors.", |
| 602 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
| 603 | phi->GetBlock()->GetBlockId(), predecessors.Size())); |
| 604 | } else { |
| 605 | // Ensure phi input at index I either comes from the Ith |
| 606 | // predecessor or from a block that dominates this predecessor. |
| 607 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 608 | HInstruction* input = phi->InputAt(i); |
| 609 | HBasicBlock* predecessor = predecessors.Get(i); |
| 610 | if (!(input->GetBlock() == predecessor |
| 611 | || input->GetBlock()->Dominates(predecessor))) { |
| 612 | AddError(StringPrintf( |
| 613 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 614 | "predecessor number %zu nor in a block dominating it.", |
| 615 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 616 | i)); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 621 | } |
| 622 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 623 | void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) { |
| 624 | HInstruction* input = instruction->InputAt(input_index); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 625 | if (input->IsIntConstant()) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 626 | int32_t value = input->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 627 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 628 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 629 | "%s instruction %d has a non-Boolean constant input %d whose value is: %d.", |
| 630 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 631 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 632 | static_cast<int>(input_index), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 633 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 634 | } |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame] | 635 | } else if (input->GetType() == Primitive::kPrimInt |
| 636 | && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) { |
| 637 | // TODO: We need a data-flow analysis to determine if the Phi or |
| 638 | // binary operation is actually Boolean. Allow for now. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 639 | } else if (input->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 640 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 641 | "%s instruction %d has a non-Boolean input %d whose type is: %s.", |
| 642 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 643 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 644 | static_cast<int>(input_index), |
| 645 | Primitive::PrettyDescriptor(input->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 649 | void SSAChecker::VisitIf(HIf* instruction) { |
| 650 | VisitInstruction(instruction); |
| 651 | HandleBooleanInput(instruction, 0); |
| 652 | } |
| 653 | |
| 654 | void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) { |
| 655 | VisitInstruction(instruction); |
| 656 | HandleBooleanInput(instruction, 0); |
| 657 | } |
| 658 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 659 | void SSAChecker::VisitCondition(HCondition* op) { |
| 660 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 661 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 662 | AddError(StringPrintf( |
| 663 | "Condition %s %d has a non-Boolean result type: %s.", |
| 664 | op->DebugName(), op->GetId(), |
| 665 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 666 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 667 | HInstruction* lhs = op->InputAt(0); |
| 668 | HInstruction* rhs = op->InputAt(1); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 669 | if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
| 670 | AddError(StringPrintf( |
| 671 | "Condition %s %d has inputs of different types: %s, and %s.", |
| 672 | op->DebugName(), op->GetId(), |
| 673 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 674 | Primitive::PrettyDescriptor(rhs->GetType()))); |
| 675 | } |
| 676 | if (!op->IsEqual() && !op->IsNotEqual()) { |
| 677 | if ((lhs->GetType() == Primitive::kPrimNot)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 678 | AddError(StringPrintf( |
| 679 | "Condition %s %d uses an object as left-hand side input.", |
| 680 | op->DebugName(), op->GetId())); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 681 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 682 | AddError(StringPrintf( |
| 683 | "Condition %s %d uses an object as right-hand side input.", |
| 684 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 685 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 686 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 690 | VisitInstruction(op); |
| 691 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 692 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 693 | AddError(StringPrintf( |
| 694 | "Shift operation %s %d has a non-int kind second input: " |
| 695 | "%s of type %s.", |
| 696 | op->DebugName(), op->GetId(), |
| 697 | op->InputAt(1)->DebugName(), |
| 698 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 699 | } |
| 700 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 701 | if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 702 | AddError(StringPrintf( |
| 703 | "Binary operation %s %d has inputs of different types: " |
| 704 | "%s, and %s.", |
| 705 | op->DebugName(), op->GetId(), |
| 706 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 707 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | if (op->IsCompare()) { |
| 712 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 713 | AddError(StringPrintf( |
| 714 | "Compare operation %d has a non-int result type: %s.", |
| 715 | op->GetId(), |
| 716 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 717 | } |
| 718 | } else { |
| 719 | // Use the first input, so that we can also make this check for shift operations. |
| 720 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 721 | AddError(StringPrintf( |
| 722 | "Binary operation %s %d has a result type different " |
| 723 | "from its input type: %s vs %s.", |
| 724 | op->DebugName(), op->GetId(), |
| 725 | Primitive::PrettyDescriptor(op->GetType()), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 726 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 731 | void SSAChecker::VisitConstant(HConstant* instruction) { |
| 732 | HBasicBlock* block = instruction->GetBlock(); |
| 733 | if (!block->IsEntryBlock()) { |
| 734 | AddError(StringPrintf( |
| 735 | "%s %d should be in the entry block but is in block %d.", |
| 736 | instruction->DebugName(), |
| 737 | instruction->GetId(), |
| 738 | block->GetBlockId())); |
| 739 | } |
| 740 | } |
| 741 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 742 | } // namespace art |