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> |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 21 | |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 22 | #include "base/bit_vector-inl.h" |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 24 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 25 | namespace art { |
| 26 | |
| 27 | void GraphChecker::VisitBasicBlock(HBasicBlock* block) { |
| 28 | current_block_ = block; |
| 29 | |
| 30 | // Check consistency with respect to predecessors of `block`. |
| 31 | const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 32 | std::map<HBasicBlock*, size_t> predecessors_count; |
| 33 | for (size_t i = 0, e = predecessors.Size(); i < e; ++i) { |
| 34 | HBasicBlock* p = predecessors.Get(i); |
| 35 | ++predecessors_count[p]; |
| 36 | } |
| 37 | for (auto& pc : predecessors_count) { |
| 38 | HBasicBlock* p = pc.first; |
| 39 | size_t p_count_in_block_predecessors = pc.second; |
| 40 | const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors(); |
| 41 | size_t block_count_in_p_successors = 0; |
| 42 | for (size_t j = 0, f = p_successors.Size(); j < f; ++j) { |
| 43 | if (p_successors.Get(j) == block) { |
| 44 | ++block_count_in_p_successors; |
| 45 | } |
| 46 | } |
| 47 | if (p_count_in_block_predecessors != block_count_in_p_successors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 48 | AddError(StringPrintf( |
| 49 | "Block %d lists %zu occurrences of block %d in its predecessors, whereas " |
| 50 | "block %d lists %zu occurrences of block %d in its successors.", |
| 51 | block->GetBlockId(), p_count_in_block_predecessors, p->GetBlockId(), |
| 52 | p->GetBlockId(), block_count_in_p_successors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | // Check consistency with respect to successors of `block`. |
| 57 | const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors(); |
| 58 | std::map<HBasicBlock*, size_t> successors_count; |
| 59 | for (size_t i = 0, e = successors.Size(); i < e; ++i) { |
| 60 | HBasicBlock* s = successors.Get(i); |
| 61 | ++successors_count[s]; |
| 62 | } |
| 63 | for (auto& sc : successors_count) { |
| 64 | HBasicBlock* s = sc.first; |
| 65 | size_t s_count_in_block_successors = sc.second; |
| 66 | const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors(); |
| 67 | size_t block_count_in_s_predecessors = 0; |
| 68 | for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) { |
| 69 | if (s_predecessors.Get(j) == block) { |
| 70 | ++block_count_in_s_predecessors; |
| 71 | } |
| 72 | } |
| 73 | if (s_count_in_block_successors != block_count_in_s_predecessors) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 74 | AddError(StringPrintf( |
| 75 | "Block %d lists %zu occurrences of block %d in its successors, whereas " |
| 76 | "block %d lists %zu occurrences of block %d in its predecessors.", |
| 77 | block->GetBlockId(), s_count_in_block_successors, s->GetBlockId(), |
| 78 | s->GetBlockId(), block_count_in_s_predecessors, block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // Ensure `block` ends with a branch instruction. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 83 | if (!block->EndsWithControlFlowInstruction()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 84 | AddError(StringPrintf("Block %d does not end with a branch instruction.", |
| 85 | block->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Visit this block's list of phis. |
| 89 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 90 | // Ensure this block's list of phis contains only phis. |
| 91 | if (!it.Current()->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 92 | AddError(StringPrintf("Block %d has a non-phi in its phi list.", |
| 93 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 94 | } |
| 95 | it.Current()->Accept(this); |
| 96 | } |
| 97 | |
| 98 | // Visit this block's list of instructions. |
| 99 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); |
| 100 | it.Advance()) { |
| 101 | // Ensure this block's list of instructions does not contains phis. |
| 102 | if (it.Current()->IsPhi()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 103 | AddError(StringPrintf("Block %d has a phi in its non-phi list.", |
| 104 | current_block_->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 105 | } |
| 106 | it.Current()->Accept(this); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 111 | if (seen_ids_.IsBitSet(instruction->GetId())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 112 | AddError(StringPrintf("Instruction id %d is duplicate in graph.", |
| 113 | instruction->GetId())); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 114 | } else { |
| 115 | seen_ids_.SetBit(instruction->GetId()); |
| 116 | } |
| 117 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 118 | // Ensure `instruction` is associated with `current_block_`. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 119 | if (instruction->GetBlock() == nullptr) { |
| 120 | AddError(StringPrintf("%s %d in block %d not associated with any block.", |
| 121 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 122 | instruction->GetId(), |
| 123 | current_block_->GetBlockId())); |
| 124 | } else if (instruction->GetBlock() != current_block_) { |
| 125 | AddError(StringPrintf("%s %d in block %d associated with block %d.", |
| 126 | instruction->IsPhi() ? "Phi" : "Instruction", |
| 127 | instruction->GetId(), |
| 128 | current_block_->GetBlockId(), |
| 129 | instruction->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 130 | } |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 131 | |
| 132 | // Ensure the inputs of `instruction` are defined in a block of the graph. |
| 133 | for (HInputIterator input_it(instruction); !input_it.Done(); |
| 134 | input_it.Advance()) { |
| 135 | HInstruction* input = input_it.Current(); |
| 136 | const HInstructionList& list = input->IsPhi() |
| 137 | ? input->GetBlock()->GetPhis() |
| 138 | : input->GetBlock()->GetInstructions(); |
| 139 | if (!list.Contains(input)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 140 | AddError(StringPrintf("Input %d of instruction %d is not defined " |
| 141 | "in a basic block of the control-flow graph.", |
| 142 | input->GetId(), |
| 143 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | // Ensure the uses of `instruction` are defined in a block of the graph. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 148 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 149 | !use_it.Done(); use_it.Advance()) { |
| 150 | HInstruction* use = use_it.Current()->GetUser(); |
| 151 | const HInstructionList& list = use->IsPhi() |
| 152 | ? use->GetBlock()->GetPhis() |
| 153 | : use->GetBlock()->GetInstructions(); |
| 154 | if (!list.Contains(use)) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 155 | AddError(StringPrintf("User %s:%d of instruction %d is not defined " |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 156 | "in a basic block of the control-flow graph.", |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 157 | use->DebugName(), |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 158 | use->GetId(), |
| 159 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 162 | |
| 163 | // Ensure 'instruction' has pointers to its inputs' use entries. |
| 164 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 165 | HUserRecord<HInstruction*> input_record = instruction->InputRecordAt(i); |
| 166 | HInstruction* input = input_record.GetInstruction(); |
| 167 | HUseListNode<HInstruction*>* use_node = input_record.GetUseNode(); |
| 168 | if (use_node == nullptr || !input->GetUses().Contains(use_node)) { |
| 169 | AddError(StringPrintf("Instruction %s:%d has an invalid pointer to use entry " |
| 170 | "at input %u (%s:%d).", |
| 171 | instruction->DebugName(), |
| 172 | instruction->GetId(), |
| 173 | static_cast<unsigned>(i), |
| 174 | input->DebugName(), |
| 175 | input->GetId())); |
| 176 | } |
| 177 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 181 | super_type::VisitBasicBlock(block); |
| 182 | |
| 183 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 184 | // block with multiple successors to a block with multiple |
| 185 | // predecessors). |
| 186 | if (block->GetSuccessors().Size() > 1) { |
| 187 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 188 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 189 | if (successor->GetPredecessors().Size() > 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 190 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 191 | block->GetBlockId(), |
| 192 | successor->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 196 | |
| 197 | if (block->IsLoopHeader()) { |
| 198 | CheckLoop(block); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 203 | int id = loop_header->GetBlockId(); |
| 204 | |
| 205 | // Ensure the pre-header block is first in the list of |
| 206 | // predecessors of a loop header. |
| 207 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 208 | AddError(StringPrintf( |
| 209 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 210 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Ensure the loop header has only two predecessors and that only the |
| 214 | // second one is a back edge. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 215 | size_t num_preds = loop_header->GetPredecessors().Size(); |
| 216 | if (num_preds < 2) { |
| 217 | AddError(StringPrintf( |
| 218 | "Loop header %d has less than two predecessors: %zu.", |
| 219 | id, |
| 220 | num_preds)); |
| 221 | } else if (num_preds > 2) { |
| 222 | AddError(StringPrintf( |
| 223 | "Loop header %d has more than two predecessors: %zu.", |
| 224 | id, |
| 225 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 226 | } else { |
| 227 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
| 228 | HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 229 | if (loop_information->IsBackEdge(*first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 230 | AddError(StringPrintf( |
| 231 | "First predecessor of loop header %d is a back edge.", |
| 232 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 233 | } |
| 234 | HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 235 | if (!loop_information->IsBackEdge(*second_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 236 | AddError(StringPrintf( |
| 237 | "Second predecessor of loop header %d is not a back edge.", |
| 238 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | // Ensure there is only one back edge per loop. |
| 243 | size_t num_back_edges = |
| 244 | loop_header->GetLoopInformation()->GetBackEdges().Size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 245 | if (num_back_edges == 0) { |
| 246 | AddError(StringPrintf( |
| 247 | "Loop defined by header %d has no back edge.", |
| 248 | id)); |
| 249 | } else if (num_back_edges > 1) { |
| 250 | AddError(StringPrintf( |
| 251 | "Loop defined by header %d has several back edges: %zu.", |
| 252 | id, |
| 253 | num_back_edges)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 254 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 255 | |
| 256 | // Ensure all blocks in the loop are dominated by the loop header. |
| 257 | const ArenaBitVector& loop_blocks = |
| 258 | loop_header->GetLoopInformation()->GetBlocks(); |
| 259 | for (uint32_t i : loop_blocks.Indexes()) { |
| 260 | HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i); |
| 261 | if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 262 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
| 263 | loop_block->GetBlockId(), |
| 264 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 270 | super_type::VisitInstruction(instruction); |
| 271 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 272 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 273 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 274 | !use_it.Done(); use_it.Advance()) { |
| 275 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 276 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 277 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 278 | "use %d in block %d.", |
| 279 | instruction->GetId(), current_block_->GetBlockId(), |
| 280 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 281 | } |
| 282 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 283 | |
| 284 | // Ensure an instruction having an environment is dominated by the |
| 285 | // instructions contained in the environment. |
| 286 | HEnvironment* environment = instruction->GetEnvironment(); |
| 287 | if (environment != nullptr) { |
| 288 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 289 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 290 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 291 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 292 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 293 | "from block %d does not dominate instruction %d.", |
| 294 | env_instruction->GetId(), |
| 295 | instruction->GetId(), |
| 296 | current_block_->GetBlockId(), |
| 297 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 303 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 304 | switch (type) { |
| 305 | case Primitive::kPrimBoolean: |
| 306 | case Primitive::kPrimByte: |
| 307 | case Primitive::kPrimShort: |
| 308 | case Primitive::kPrimChar: |
| 309 | case Primitive::kPrimInt: |
| 310 | return Primitive::kPrimInt; |
| 311 | default: |
| 312 | return type; |
| 313 | } |
| 314 | } |
| 315 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 316 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 317 | VisitInstruction(phi); |
| 318 | |
| 319 | // Ensure the first input of a phi is not itself. |
| 320 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 321 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 322 | phi->GetId(), |
| 323 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 326 | // 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] | 327 | // its predecessors. |
| 328 | const GrowableArray<HBasicBlock*>& predecessors = |
| 329 | phi->GetBlock()->GetPredecessors(); |
| 330 | if (phi->InputCount() != predecessors.Size()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 331 | AddError(StringPrintf( |
| 332 | "Phi %d in block %d has %zu inputs, " |
| 333 | "but block %d has %zu predecessors.", |
| 334 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
| 335 | phi->GetBlock()->GetBlockId(), predecessors.Size())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 336 | } else { |
| 337 | // Ensure phi input at index I either comes from the Ith |
| 338 | // predecessor or from a block that dominates this predecessor. |
| 339 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 340 | HInstruction* input = phi->InputAt(i); |
| 341 | HBasicBlock* predecessor = predecessors.Get(i); |
| 342 | if (!(input->GetBlock() == predecessor |
| 343 | || input->GetBlock()->Dominates(predecessor))) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 344 | AddError(StringPrintf( |
| 345 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 346 | "predecessor number %zu nor in a block dominating it.", |
| 347 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 348 | i)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 352 | // Ensure that the inputs have the same primitive kind as the phi. |
| 353 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 354 | HInstruction* input = phi->InputAt(i); |
| 355 | if (PrimitiveKind(input->GetType()) != PrimitiveKind(phi->GetType())) { |
| 356 | AddError(StringPrintf( |
| 357 | "Input %d at index %zu of phi %d from block %d does not have the " |
| 358 | "same type as the phi: %s versus %s", |
| 359 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 360 | Primitive::PrettyDescriptor(input->GetType()), |
| 361 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 362 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 363 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 364 | if (phi->GetType() != HPhi::ToPhiType(phi->GetType())) { |
| 365 | AddError(StringPrintf("Phi %d in block %d does not have an expected phi type: %s", |
| 366 | phi->GetId(), |
| 367 | phi->GetBlock()->GetBlockId(), |
| 368 | Primitive::PrettyDescriptor(phi->GetType()))); |
| 369 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 372 | void SSAChecker::VisitIf(HIf* instruction) { |
| 373 | VisitInstruction(instruction); |
| 374 | HInstruction* input = instruction->InputAt(0); |
| 375 | if (input->IsIntConstant()) { |
| 376 | int value = input->AsIntConstant()->GetValue(); |
| 377 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 378 | AddError(StringPrintf( |
| 379 | "If instruction %d has a non-Boolean constant input " |
| 380 | "whose value is: %d.", |
| 381 | instruction->GetId(), |
| 382 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 383 | } |
| 384 | } else if (instruction->InputAt(0)->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 385 | AddError(StringPrintf( |
| 386 | "If instruction %d has a non-Boolean input type: %s.", |
| 387 | instruction->GetId(), |
| 388 | Primitive::PrettyDescriptor(instruction->InputAt(0)->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 392 | void SSAChecker::VisitCondition(HCondition* op) { |
| 393 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 394 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 395 | AddError(StringPrintf( |
| 396 | "Condition %s %d has a non-Boolean result type: %s.", |
| 397 | op->DebugName(), op->GetId(), |
| 398 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 399 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 400 | HInstruction* lhs = op->InputAt(0); |
| 401 | HInstruction* rhs = op->InputAt(1); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 402 | if (lhs->GetType() == Primitive::kPrimNot) { |
| 403 | if (!op->IsEqual() && !op->IsNotEqual()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 404 | AddError(StringPrintf( |
| 405 | "Condition %s %d uses an object as left-hand side input.", |
| 406 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 407 | } |
| 408 | if (rhs->IsIntConstant() && rhs->AsIntConstant()->GetValue() != 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 409 | AddError(StringPrintf( |
| 410 | "Condition %s %d compares an object with a non-zero integer: %d.", |
| 411 | op->DebugName(), op->GetId(), |
| 412 | rhs->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 413 | } |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 414 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
| 415 | if (!op->IsEqual() && !op->IsNotEqual()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 416 | AddError(StringPrintf( |
| 417 | "Condition %s %d uses an object as right-hand side input.", |
| 418 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 419 | } |
| 420 | if (lhs->IsIntConstant() && lhs->AsIntConstant()->GetValue() != 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 421 | AddError(StringPrintf( |
| 422 | "Condition %s %d compares a non-zero integer with an object: %d.", |
| 423 | op->DebugName(), op->GetId(), |
| 424 | lhs->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 425 | } |
| 426 | } else if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 427 | AddError(StringPrintf( |
| 428 | "Condition %s %d has inputs of different types: " |
| 429 | "%s, and %s.", |
| 430 | op->DebugName(), op->GetId(), |
| 431 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 432 | Primitive::PrettyDescriptor(rhs->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 433 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 437 | VisitInstruction(op); |
| 438 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 439 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 440 | AddError(StringPrintf( |
| 441 | "Shift operation %s %d has a non-int kind second input: " |
| 442 | "%s of type %s.", |
| 443 | op->DebugName(), op->GetId(), |
| 444 | op->InputAt(1)->DebugName(), |
| 445 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 446 | } |
| 447 | } else { |
| 448 | if (PrimitiveKind(op->InputAt(1)->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 449 | AddError(StringPrintf( |
| 450 | "Binary operation %s %d has inputs of different types: " |
| 451 | "%s, and %s.", |
| 452 | op->DebugName(), op->GetId(), |
| 453 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 454 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | if (op->IsCompare()) { |
| 459 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 460 | AddError(StringPrintf( |
| 461 | "Compare operation %d has a non-int result type: %s.", |
| 462 | op->GetId(), |
| 463 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 464 | } |
| 465 | } else { |
| 466 | // Use the first input, so that we can also make this check for shift operations. |
| 467 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 468 | AddError(StringPrintf( |
| 469 | "Binary operation %s %d has a result type different " |
| 470 | "from its input type: %s vs %s.", |
| 471 | op->DebugName(), op->GetId(), |
| 472 | Primitive::PrettyDescriptor(op->GetType()), |
| 473 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 478 | void SSAChecker::VisitConstant(HConstant* instruction) { |
| 479 | HBasicBlock* block = instruction->GetBlock(); |
| 480 | if (!block->IsEntryBlock()) { |
| 481 | AddError(StringPrintf( |
| 482 | "%s %d should be in the entry block but is in block %d.", |
| 483 | instruction->DebugName(), |
| 484 | instruction->GetId(), |
| 485 | block->GetBlockId())); |
| 486 | } |
| 487 | } |
| 488 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 489 | } // namespace art |