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 | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 84 | if (!block->EndsWithControlFlowInstruction()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 85 | AddError(StringPrintf("Block %d does not end with a branch instruction.", |
| 86 | block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Visit this block's list of phis. |
| 90 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 91 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 92 | // Ensure this block's list of phis contains only phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 93 | if (!current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 94 | AddError(StringPrintf("Block %d has a non-phi in its phi list.", |
| 95 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 96 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 97 | if (current->GetNext() == nullptr && current != block->GetLastPhi()) { |
| 98 | AddError(StringPrintf("The recorded last phi of block %d does not match " |
| 99 | "the actual last phi %d.", |
| 100 | current_block_->GetBlockId(), |
| 101 | current->GetId())); |
| 102 | } |
| 103 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Visit this block's list of instructions. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 107 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 108 | HInstruction* current = it.Current(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 109 | // Ensure this block's list of instructions does not contains phis. |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 110 | if (current->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 111 | AddError(StringPrintf("Block %d has a phi in its non-phi list.", |
| 112 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 113 | } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 114 | if (current->GetNext() == nullptr && current != block->GetLastInstruction()) { |
| 115 | AddError(StringPrintf("The recorded last instruction of block %d does not match " |
| 116 | "the actual last instruction %d.", |
| 117 | current_block_->GetBlockId(), |
| 118 | current->GetId())); |
| 119 | } |
| 120 | current->Accept(this); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 124 | void GraphChecker::VisitBoundsCheck(HBoundsCheck* check) { |
| 125 | if (!GetGraph()->HasBoundsChecks()) { |
| 126 | AddError(StringPrintf("Instruction %s:%d is a HBoundsCheck, " |
| 127 | "but HasBoundsChecks() returns false", |
| 128 | check->DebugName(), |
| 129 | check->GetId())); |
| 130 | } |
| 131 | |
| 132 | // Perform the instruction base checks too. |
| 133 | VisitInstruction(check); |
| 134 | } |
| 135 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 136 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 137 | if (seen_ids_.IsBitSet(instruction->GetId())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 138 | AddError(StringPrintf("Instruction id %d is duplicate in graph.", |
| 139 | instruction->GetId())); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 140 | } else { |
| 141 | seen_ids_.SetBit(instruction->GetId()); |
| 142 | } |
| 143 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 144 | // Ensure `instruction` is associated with `current_block_`. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 145 | if (instruction->GetBlock() == nullptr) { |
| 146 | AddError(StringPrintf("%s %d in block %d not associated with any block.", |
| 147 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 148 | instruction->GetId(), |
| 149 | current_block_->GetBlockId())); |
| 150 | } else if (instruction->GetBlock() != current_block_) { |
| 151 | AddError(StringPrintf("%s %d in block %d associated with block %d.", |
| 152 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 153 | instruction->GetId(), |
| 154 | current_block_->GetBlockId(), |
| 155 | instruction->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 156 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 157 | |
| 158 | // Ensure the inputs of `instruction` are defined in a block of the graph. |
| 159 | for (HInputIterator input_it(instruction); !input_it.Done(); |
| 160 | input_it.Advance()) { |
| 161 | HInstruction* input = input_it.Current(); |
| 162 | const HInstructionList& list = input->IsPhi() |
| 163 | ? input->GetBlock()->GetPhis() |
| 164 | : input->GetBlock()->GetInstructions(); |
| 165 | if (!list.Contains(input)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 166 | AddError(StringPrintf("Input %d of instruction %d is not defined " |
| 167 | "in a basic block of the control-flow graph.", |
| 168 | input->GetId(), |
| 169 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 173 | // Ensure the uses of `instruction` are defined in a block of the graph, |
| 174 | // and the entry in the use list is consistent. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 175 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 176 | !use_it.Done(); use_it.Advance()) { |
| 177 | HInstruction* use = use_it.Current()->GetUser(); |
| 178 | const HInstructionList& list = use->IsPhi() |
| 179 | ? use->GetBlock()->GetPhis() |
| 180 | : use->GetBlock()->GetInstructions(); |
| 181 | if (!list.Contains(use)) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 182 | AddError(StringPrintf("User %s:%d of instruction %d is not defined " |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 183 | "in a basic block of the control-flow graph.", |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 184 | use->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 185 | use->GetId(), |
| 186 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 187 | } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 188 | size_t use_index = use_it.Current()->GetIndex(); |
| 189 | if ((use_index >= use->InputCount()) || (use->InputAt(use_index) != instruction)) { |
| 190 | AddError(StringPrintf("User %s:%d of instruction %d has a wrong " |
| 191 | "UseListNode index.", |
| 192 | use->DebugName(), |
| 193 | use->GetId(), |
| 194 | instruction->GetId())); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Ensure the environment uses entries are consistent. |
| 199 | for (HUseIterator<HEnvironment*> use_it(instruction->GetEnvUses()); |
| 200 | !use_it.Done(); use_it.Advance()) { |
| 201 | HEnvironment* use = use_it.Current()->GetUser(); |
| 202 | size_t use_index = use_it.Current()->GetIndex(); |
| 203 | if ((use_index >= use->Size()) || (use->GetInstructionAt(use_index) != instruction)) { |
| 204 | AddError(StringPrintf("Environment user of %s:%d has a wrong " |
| 205 | "UseListNode index.", |
| 206 | instruction->DebugName(), |
| 207 | instruction->GetId())); |
| 208 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 209 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 210 | |
| 211 | // Ensure 'instruction' has pointers to its inputs' use entries. |
| 212 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 213 | HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i); |
| 214 | HInstruction* input = input_record.GetInstruction(); |
| 215 | HUseListNode<HInstruction*>* use_node = input_record.GetUseNode(); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 216 | size_t use_index = use_node->GetIndex(); |
| 217 | if ((use_node == nullptr) |
| 218 | || !input->GetUses().Contains(use_node) |
| 219 | || (use_index >= e) |
| 220 | || (use_index != i)) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 221 | AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry " |
| 222 | "at input %u (%s:%d).", |
| 223 | instruction->DebugName(), |
| 224 | instruction->GetId(), |
| 225 | static_cast<unsigned>(i), |
| 226 | input->DebugName(), |
| 227 | input->GetId())); |
| 228 | } |
| 229 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 232 | void GraphChecker::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { |
| 233 | VisitInstruction(invoke); |
| 234 | |
| 235 | if (invoke->IsStaticWithExplicitClinitCheck()) { |
| 236 | size_t last_input_index = invoke->InputCount() - 1; |
| 237 | HInstruction* last_input = invoke->InputAt(last_input_index); |
| 238 | if (last_input == nullptr) { |
| 239 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 240 | "has a null pointer as last input.", |
| 241 | invoke->DebugName(), |
| 242 | invoke->GetId())); |
| 243 | } |
| 244 | if (!last_input->IsClinitCheck() && !last_input->IsLoadClass()) { |
| 245 | AddError(StringPrintf("Static invoke %s:%d marked as having an explicit clinit check " |
| 246 | "has a last instruction (%s:%d) which is neither a clinit check " |
| 247 | "nor a load class instruction.", |
| 248 | invoke->DebugName(), |
| 249 | invoke->GetId(), |
| 250 | last_input->DebugName(), |
| 251 | last_input->GetId())); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 256 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 257 | super_type::VisitBasicBlock(block); |
| 258 | |
| 259 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 260 | // block with multiple successors to a block with multiple |
| 261 | // predecessors). |
| 262 | if (block->GetSuccessors().Size() > 1) { |
| 263 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 264 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 265 | if (successor->GetPredecessors().Size() > 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 266 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 267 | block->GetBlockId(), |
| 268 | successor->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 272 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 273 | // Check Phi uniqueness (no two Phis with the same type refer to the same register). |
| 274 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 275 | HPhi* phi = it.Current()->AsPhi(); |
| 276 | if (phi->GetNextEquivalentPhiWithSameType() != nullptr) { |
| 277 | std::stringstream type_str; |
| 278 | type_str << phi->GetType(); |
| 279 | AddError(StringPrintf("Equivalent phi (%d) found for VReg %d with type: %s", |
| 280 | phi->GetId(), phi->GetRegNumber(), type_str.str().c_str())); |
| 281 | } |
| 282 | } |
| 283 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 284 | if (block->IsLoopHeader()) { |
| 285 | CheckLoop(block); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 290 | int id = loop_header->GetBlockId(); |
| 291 | |
| 292 | // Ensure the pre-header block is first in the list of |
| 293 | // predecessors of a loop header. |
| 294 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 295 | AddError(StringPrintf( |
| 296 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 297 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Ensure the loop header has only two predecessors and that only the |
| 301 | // second one is a back edge. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 302 | size_t num_preds = loop_header->GetPredecessors().Size(); |
| 303 | if (num_preds < 2) { |
| 304 | AddError(StringPrintf( |
| 305 | "Loop header %d has less than two predecessors: %zu.", |
| 306 | id, |
| 307 | num_preds)); |
| 308 | } else if (num_preds > 2) { |
| 309 | AddError(StringPrintf( |
| 310 | "Loop header %d has more than two predecessors: %zu.", |
| 311 | id, |
| 312 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 313 | } else { |
| 314 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
| 315 | HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 316 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 317 | AddError(StringPrintf( |
| 318 | "First predecessor of loop header %d is a back edge.", |
| 319 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 320 | } |
| 321 | HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 322 | if (!loop_information->IsBackEdge(*second_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 323 | AddError(StringPrintf( |
| 324 | "Second predecessor of loop header %d is not a back edge.", |
| 325 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 329 | const ArenaBitVector& loop_blocks = loop_header->GetLoopInformation()->GetBlocks(); |
| 330 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 331 | // Ensure there is only one back edge per loop. |
| 332 | size_t num_back_edges = |
| 333 | loop_header->GetLoopInformation()->GetBackEdges().Size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 334 | if (num_back_edges == 0) { |
| 335 | AddError(StringPrintf( |
| 336 | "Loop defined by header %d has no back edge.", |
| 337 | id)); |
| 338 | } else if (num_back_edges > 1) { |
| 339 | AddError(StringPrintf( |
| 340 | "Loop defined by header %d has several back edges: %zu.", |
| 341 | id, |
| 342 | num_back_edges)); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 343 | } else { |
| 344 | DCHECK_EQ(num_back_edges, 1u); |
| 345 | int back_edge_id = loop_header->GetLoopInformation()->GetBackEdges().Get(0)->GetBlockId(); |
| 346 | if (!loop_blocks.IsBitSet(back_edge_id)) { |
| 347 | AddError(StringPrintf( |
| 348 | "Loop defined by header %d has an invalid back edge %d.", |
| 349 | id, |
| 350 | back_edge_id)); |
| 351 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 352 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 353 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 354 | // 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] | 355 | for (uint32_t i : loop_blocks.Indexes()) { |
| 356 | HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 357 | if (loop_block == nullptr) { |
| 358 | AddError(StringPrintf("Loop defined by header %d contains a previously removed block %d.", |
| 359 | id, |
| 360 | i)); |
| 361 | } else if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 362 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 363 | i, |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 364 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 367 | |
| 368 | // If this is a nested loop, ensure the outer loops contain a superset of the blocks. |
| 369 | for (HLoopInformationOutwardIterator it(*loop_header); !it.Done(); it.Advance()) { |
| 370 | HLoopInformation* outer_info = it.Current(); |
| 371 | if (!loop_blocks.IsSubsetOf(&outer_info->GetBlocks())) { |
| 372 | AddError(StringPrintf("Blocks of loop defined by header %d are not a subset of blocks of " |
| 373 | "an outer loop defined by header %d.", |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 374 | id, |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 375 | outer_info->GetHeader()->GetBlockId())); |
| 376 | } |
| 377 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 381 | super_type::VisitInstruction(instruction); |
| 382 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 383 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 384 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 385 | !use_it.Done(); use_it.Advance()) { |
| 386 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 387 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 388 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 389 | "use %d in block %d.", |
| 390 | instruction->GetId(), current_block_->GetBlockId(), |
| 391 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 394 | |
| 395 | // Ensure an instruction having an environment is dominated by the |
| 396 | // instructions contained in the environment. |
| 397 | HEnvironment* environment = instruction->GetEnvironment(); |
| 398 | if (environment != nullptr) { |
| 399 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 400 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 401 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 402 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 403 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 404 | "from block %d does not dominate instruction %d.", |
| 405 | env_instruction->GetId(), |
| 406 | instruction->GetId(), |
| 407 | current_block_->GetBlockId(), |
| 408 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 414 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 415 | switch (type) { |
| 416 | case Primitive::kPrimBoolean: |
| 417 | case Primitive::kPrimByte: |
| 418 | case Primitive::kPrimShort: |
| 419 | case Primitive::kPrimChar: |
| 420 | case Primitive::kPrimInt: |
| 421 | return Primitive::kPrimInt; |
| 422 | default: |
| 423 | return type; |
| 424 | } |
| 425 | } |
| 426 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 427 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 428 | VisitInstruction(phi); |
| 429 | |
| 430 | // Ensure the first input of a phi is not itself. |
| 431 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 432 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 433 | phi->GetId(), |
| 434 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 437 | // Ensure the number of inputs of a phi is the same as the number of |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 438 | // its predecessors. |
| 439 | const GrowableArray<HBasicBlock*>& predecessors = |
| 440 | phi->GetBlock()->GetPredecessors(); |
| 441 | if (phi->InputCount() != predecessors.Size()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 442 | AddError(StringPrintf( |
| 443 | "Phi %d in block %d has %zu inputs, " |
| 444 | "but block %d has %zu predecessors.", |
| 445 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
| 446 | phi->GetBlock()->GetBlockId(), predecessors.Size())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 447 | } else { |
| 448 | // Ensure phi input at index I either comes from the Ith |
| 449 | // predecessor or from a block that dominates this predecessor. |
| 450 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 451 | HInstruction* input = phi->InputAt(i); |
| 452 | HBasicBlock* predecessor = predecessors.Get(i); |
| 453 | if (!(input->GetBlock() == predecessor |
| 454 | || input->GetBlock()->Dominates(predecessor))) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 455 | AddError(StringPrintf( |
| 456 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 457 | "predecessor number %zu nor in a block dominating it.", |
| 458 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 459 | i)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 463 | // Ensure that the inputs have the same primitive kind as the phi. |
| 464 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 465 | HInstruction* input = phi->InputAt(i); |
| 466 | if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) { |
| 467 | AddError(StringPrintf( |
| 468 | "Input %d at index %zu of phi %d from block %d does not have the " |
| 469 | "same type as the phi: %s versus %s", |
| 470 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 471 | Primitive::PrettyDescriptor(input->GetType()), |
| 472 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 473 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 474 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 475 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 476 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 477 | phi->GetId(), |
| 478 | phi->GetBlock()->GetBlockId(), |
| 479 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 480 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 481 | } |
| 482 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 483 | void SSAChecker::HandleBooleanInput(HInstruction* instruction, size_t input_index) { |
| 484 | HInstruction* input = instruction->InputAt(input_index); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 485 | if (input->IsIntConstant()) { |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 486 | int32_t value = input->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 487 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 488 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 489 | "%s instruction %d has a non-Boolean constant input %d whose value is: %d.", |
| 490 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 491 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 492 | static_cast<int>(input_index), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 493 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 494 | } |
David Brazdil | 2fa194b | 2015-04-20 10:14:42 +0100 | [diff] [blame] | 495 | } else if (input->GetType() == Primitive::kPrimInt |
| 496 | && (input->IsPhi() || input->IsAnd() || input->IsOr() || input->IsXor())) { |
| 497 | // TODO: We need a data-flow analysis to determine if the Phi or |
| 498 | // binary operation is actually Boolean. Allow for now. |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 499 | } else if (input->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 500 | AddError(StringPrintf( |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 501 | "%s instruction %d has a non-Boolean input %d whose type is: %s.", |
| 502 | instruction->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 503 | instruction->GetId(), |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 504 | static_cast<int>(input_index), |
| 505 | Primitive::PrettyDescriptor(input->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
David Brazdil | 13b4718 | 2015-04-15 16:29:32 +0100 | [diff] [blame] | 509 | void SSAChecker::VisitIf(HIf* instruction) { |
| 510 | VisitInstruction(instruction); |
| 511 | HandleBooleanInput(instruction, 0); |
| 512 | } |
| 513 | |
| 514 | void SSAChecker::VisitBooleanNot(HBooleanNot* instruction) { |
| 515 | VisitInstruction(instruction); |
| 516 | HandleBooleanInput(instruction, 0); |
| 517 | } |
| 518 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 519 | void SSAChecker::VisitCondition(HCondition* op) { |
| 520 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 521 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 522 | AddError(StringPrintf( |
| 523 | "Condition %s %d has a non-Boolean result type: %s.", |
| 524 | op->DebugName(), op->GetId(), |
| 525 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 526 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 527 | HInstruction* lhs = op->InputAt(0); |
| 528 | HInstruction* rhs = op->InputAt(1); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 529 | if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
| 530 | AddError(StringPrintf( |
| 531 | "Condition %s %d has inputs of different types: %s, and %s.", |
| 532 | op->DebugName(), op->GetId(), |
| 533 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 534 | Primitive::PrettyDescriptor(rhs->GetType()))); |
| 535 | } |
| 536 | if (!op->IsEqual() && !op->IsNotEqual()) { |
| 537 | if ((lhs->GetType() == Primitive::kPrimNot)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 538 | AddError(StringPrintf( |
| 539 | "Condition %s %d uses an object as left-hand side input.", |
| 540 | op->DebugName(), op->GetId())); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 541 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 542 | AddError(StringPrintf( |
| 543 | "Condition %s %d uses an object as right-hand side input.", |
| 544 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 545 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 546 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 550 | VisitInstruction(op); |
| 551 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 552 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 553 | AddError(StringPrintf( |
| 554 | "Shift operation %s %d has a non-int kind second input: " |
| 555 | "%s of type %s.", |
| 556 | op->DebugName(), op->GetId(), |
| 557 | op->InputAt(1)->DebugName(), |
| 558 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 559 | } |
| 560 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 561 | if (PrimitiveKind(op->InputAt(0)->GetType()) != PrimitiveKind(op->InputAt(1)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 562 | AddError(StringPrintf( |
| 563 | "Binary operation %s %d has inputs of different types: " |
| 564 | "%s, and %s.", |
| 565 | op->DebugName(), op->GetId(), |
| 566 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 567 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | if (op->IsCompare()) { |
| 572 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 573 | AddError(StringPrintf( |
| 574 | "Compare operation %d has a non-int result type: %s.", |
| 575 | op->GetId(), |
| 576 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 577 | } |
| 578 | } else { |
| 579 | // Use the first input, so that we can also make this check for shift operations. |
| 580 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 581 | AddError(StringPrintf( |
| 582 | "Binary operation %s %d has a result type different " |
| 583 | "from its input type: %s vs %s.", |
| 584 | op->DebugName(), op->GetId(), |
| 585 | Primitive::PrettyDescriptor(op->GetType()), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 586 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 591 | void SSAChecker::VisitConstant(HConstant* instruction) { |
| 592 | HBasicBlock* block = instruction->GetBlock(); |
| 593 | if (!block->IsEntryBlock()) { |
| 594 | AddError(StringPrintf( |
| 595 | "%s %d should be in the entry block but is in block %d.", |
| 596 | instruction->DebugName(), |
| 597 | instruction->GetId(), |
| 598 | block->GetBlockId())); |
| 599 | } |
| 600 | } |
| 601 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 602 | } // namespace art |