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