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. |
| 83 | HInstruction* last_inst = block->GetLastInstruction(); |
| 84 | if (last_inst == nullptr || !last_inst->IsControlFlow()) { |
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)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 156 | AddError(StringPrintf("User %d of instruction %d is not defined " |
| 157 | "in a basic block of the control-flow graph.", |
| 158 | use->GetId(), |
| 159 | instruction->GetId())); |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 165 | super_type::VisitBasicBlock(block); |
| 166 | |
| 167 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 168 | // block with multiple successors to a block with multiple |
| 169 | // predecessors). |
| 170 | if (block->GetSuccessors().Size() > 1) { |
| 171 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 172 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 173 | if (successor->GetPredecessors().Size() > 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 174 | AddError(StringPrintf("Critical edge between blocks %d and %d.", |
| 175 | block->GetBlockId(), |
| 176 | successor->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 180 | |
| 181 | if (block->IsLoopHeader()) { |
| 182 | CheckLoop(block); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void SSAChecker::CheckLoop(HBasicBlock* loop_header) { |
| 187 | int id = loop_header->GetBlockId(); |
| 188 | |
| 189 | // Ensure the pre-header block is first in the list of |
| 190 | // predecessors of a loop header. |
| 191 | if (!loop_header->IsLoopPreHeaderFirstPredecessor()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 192 | AddError(StringPrintf( |
| 193 | "Loop pre-header is not the first predecessor of the loop header %d.", |
| 194 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Ensure the loop header has only two predecessors and that only the |
| 198 | // second one is a back edge. |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 199 | size_t num_preds = loop_header->GetPredecessors().Size(); |
| 200 | if (num_preds < 2) { |
| 201 | AddError(StringPrintf( |
| 202 | "Loop header %d has less than two predecessors: %zu.", |
| 203 | id, |
| 204 | num_preds)); |
| 205 | } else if (num_preds > 2) { |
| 206 | AddError(StringPrintf( |
| 207 | "Loop header %d has more than two predecessors: %zu.", |
| 208 | id, |
| 209 | num_preds)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 210 | } else { |
| 211 | HLoopInformation* loop_information = loop_header->GetLoopInformation(); |
| 212 | HBasicBlock* first_predecessor = loop_header->GetPredecessors().Get(0); |
| 213 | if (loop_information->IsBackEdge(first_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 214 | AddError(StringPrintf( |
| 215 | "First predecessor of loop header %d is a back edge.", |
| 216 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 217 | } |
| 218 | HBasicBlock* second_predecessor = loop_header->GetPredecessors().Get(1); |
| 219 | if (!loop_information->IsBackEdge(second_predecessor)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 220 | AddError(StringPrintf( |
| 221 | "Second predecessor of loop header %d is not a back edge.", |
| 222 | id)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // Ensure there is only one back edge per loop. |
| 227 | size_t num_back_edges = |
| 228 | loop_header->GetLoopInformation()->GetBackEdges().Size(); |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 229 | if (num_back_edges == 0) { |
| 230 | AddError(StringPrintf( |
| 231 | "Loop defined by header %d has no back edge.", |
| 232 | id)); |
| 233 | } else if (num_back_edges > 1) { |
| 234 | AddError(StringPrintf( |
| 235 | "Loop defined by header %d has several back edges: %zu.", |
| 236 | id, |
| 237 | num_back_edges)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 238 | } |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 239 | |
| 240 | // Ensure all blocks in the loop are dominated by the loop header. |
| 241 | const ArenaBitVector& loop_blocks = |
| 242 | loop_header->GetLoopInformation()->GetBlocks(); |
| 243 | for (uint32_t i : loop_blocks.Indexes()) { |
| 244 | HBasicBlock* loop_block = GetGraph()->GetBlocks().Get(i); |
| 245 | if (!loop_header->Dominates(loop_block)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 246 | AddError(StringPrintf("Loop block %d not dominated by loop header %d.", |
| 247 | loop_block->GetBlockId(), |
| 248 | id)); |
Roland Levillain | 7e53b41 | 2014-09-23 10:50:22 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 254 | super_type::VisitInstruction(instruction); |
| 255 | |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 256 | // Ensure an instruction dominates all its uses. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame^] | 257 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 258 | !use_it.Done(); use_it.Advance()) { |
| 259 | HInstruction* use = use_it.Current()->GetUser(); |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 260 | if (!use->IsPhi() && !instruction->StrictlyDominates(use)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 261 | AddError(StringPrintf("Instruction %d in block %d does not dominate " |
| 262 | "use %d in block %d.", |
| 263 | instruction->GetId(), current_block_->GetBlockId(), |
| 264 | use->GetId(), use->GetBlock()->GetBlockId())); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 267 | |
| 268 | // Ensure an instruction having an environment is dominated by the |
| 269 | // instructions contained in the environment. |
| 270 | HEnvironment* environment = instruction->GetEnvironment(); |
| 271 | if (environment != nullptr) { |
| 272 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 273 | HInstruction* env_instruction = environment->GetInstructionAt(i); |
| 274 | if (env_instruction != nullptr |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 275 | && !env_instruction->StrictlyDominates(instruction)) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 276 | AddError(StringPrintf("Instruction %d in environment of instruction %d " |
| 277 | "from block %d does not dominate instruction %d.", |
| 278 | env_instruction->GetId(), |
| 279 | instruction->GetId(), |
| 280 | current_block_->GetBlockId(), |
| 281 | instruction->GetId())); |
Roland Levillain | a8069ce | 2014-10-01 10:48:29 +0100 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 287 | void SSAChecker::VisitPhi(HPhi* phi) { |
| 288 | VisitInstruction(phi); |
| 289 | |
| 290 | // Ensure the first input of a phi is not itself. |
| 291 | if (phi->InputAt(0) == phi) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 292 | AddError(StringPrintf("Loop phi %d in block %d is its own first input.", |
| 293 | phi->GetId(), |
| 294 | phi->GetBlock()->GetBlockId())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 297 | // 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] | 298 | // its predecessors. |
| 299 | const GrowableArray<HBasicBlock*>& predecessors = |
| 300 | phi->GetBlock()->GetPredecessors(); |
| 301 | if (phi->InputCount() != predecessors.Size()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 302 | AddError(StringPrintf( |
| 303 | "Phi %d in block %d has %zu inputs, " |
| 304 | "but block %d has %zu predecessors.", |
| 305 | phi->GetId(), phi->GetBlock()->GetBlockId(), phi->InputCount(), |
| 306 | phi->GetBlock()->GetBlockId(), predecessors.Size())); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 307 | } else { |
| 308 | // Ensure phi input at index I either comes from the Ith |
| 309 | // predecessor or from a block that dominates this predecessor. |
| 310 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 311 | HInstruction* input = phi->InputAt(i); |
| 312 | HBasicBlock* predecessor = predecessors.Get(i); |
| 313 | if (!(input->GetBlock() == predecessor |
| 314 | || input->GetBlock()->Dominates(predecessor))) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 315 | AddError(StringPrintf( |
| 316 | "Input %d at index %zu of phi %d from block %d is not defined in " |
| 317 | "predecessor number %zu nor in a block dominating it.", |
| 318 | input->GetId(), i, phi->GetId(), phi->GetBlock()->GetBlockId(), |
| 319 | i)); |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 325 | static Primitive::Type PrimitiveKind(Primitive::Type type) { |
| 326 | switch (type) { |
| 327 | case Primitive::kPrimBoolean: |
| 328 | case Primitive::kPrimByte: |
| 329 | case Primitive::kPrimShort: |
| 330 | case Primitive::kPrimChar: |
| 331 | case Primitive::kPrimInt: |
| 332 | return Primitive::kPrimInt; |
| 333 | default: |
| 334 | return type; |
| 335 | } |
| 336 | } |
| 337 | |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 338 | void SSAChecker::VisitIf(HIf* instruction) { |
| 339 | VisitInstruction(instruction); |
| 340 | HInstruction* input = instruction->InputAt(0); |
| 341 | if (input->IsIntConstant()) { |
| 342 | int value = input->AsIntConstant()->GetValue(); |
| 343 | if (value != 0 && value != 1) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 344 | AddError(StringPrintf( |
| 345 | "If instruction %d has a non-Boolean constant input " |
| 346 | "whose value is: %d.", |
| 347 | instruction->GetId(), |
| 348 | value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 349 | } |
| 350 | } else if (instruction->InputAt(0)->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 351 | AddError(StringPrintf( |
| 352 | "If instruction %d has a non-Boolean input type: %s.", |
| 353 | instruction->GetId(), |
| 354 | Primitive::PrettyDescriptor(instruction->InputAt(0)->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 358 | void SSAChecker::VisitCondition(HCondition* op) { |
| 359 | VisitInstruction(op); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 360 | if (op->GetType() != Primitive::kPrimBoolean) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 361 | AddError(StringPrintf( |
| 362 | "Condition %s %d has a non-Boolean result type: %s.", |
| 363 | op->DebugName(), op->GetId(), |
| 364 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 365 | } |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 366 | HInstruction* lhs = op->InputAt(0); |
| 367 | HInstruction* rhs = op->InputAt(1); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 368 | if (lhs->GetType() == Primitive::kPrimNot) { |
| 369 | if (!op->IsEqual() && !op->IsNotEqual()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 370 | AddError(StringPrintf( |
| 371 | "Condition %s %d uses an object as left-hand side input.", |
| 372 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 373 | } |
| 374 | if (rhs->IsIntConstant() && rhs->AsIntConstant()->GetValue() != 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 375 | AddError(StringPrintf( |
| 376 | "Condition %s %d compares an object with a non-zero integer: %d.", |
| 377 | op->DebugName(), op->GetId(), |
| 378 | rhs->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 379 | } |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 380 | } else if (rhs->GetType() == Primitive::kPrimNot) { |
| 381 | if (!op->IsEqual() && !op->IsNotEqual()) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 382 | AddError(StringPrintf( |
| 383 | "Condition %s %d uses an object as right-hand side input.", |
| 384 | op->DebugName(), op->GetId())); |
Roland Levillain | aecbd26 | 2015-01-19 12:44:01 +0000 | [diff] [blame] | 385 | } |
| 386 | if (lhs->IsIntConstant() && lhs->AsIntConstant()->GetValue() != 0) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 387 | AddError(StringPrintf( |
| 388 | "Condition %s %d compares a non-zero integer with an object: %d.", |
| 389 | op->DebugName(), op->GetId(), |
| 390 | lhs->AsIntConstant()->GetValue())); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 391 | } |
| 392 | } else if (PrimitiveKind(lhs->GetType()) != PrimitiveKind(rhs->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 393 | AddError(StringPrintf( |
| 394 | "Condition %s %d has inputs of different types: " |
| 395 | "%s, and %s.", |
| 396 | op->DebugName(), op->GetId(), |
| 397 | Primitive::PrettyDescriptor(lhs->GetType()), |
| 398 | Primitive::PrettyDescriptor(rhs->GetType()))); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 399 | } |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void SSAChecker::VisitBinaryOperation(HBinaryOperation* op) { |
| 403 | VisitInstruction(op); |
| 404 | if (op->IsUShr() || op->IsShr() || op->IsShl()) { |
| 405 | if (PrimitiveKind(op->InputAt(1)->GetType()) != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 406 | AddError(StringPrintf( |
| 407 | "Shift operation %s %d has a non-int kind second input: " |
| 408 | "%s of type %s.", |
| 409 | op->DebugName(), op->GetId(), |
| 410 | op->InputAt(1)->DebugName(), |
| 411 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 412 | } |
| 413 | } else { |
| 414 | if (PrimitiveKind(op->InputAt(1)->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 415 | AddError(StringPrintf( |
| 416 | "Binary operation %s %d has inputs of different types: " |
| 417 | "%s, and %s.", |
| 418 | op->DebugName(), op->GetId(), |
| 419 | Primitive::PrettyDescriptor(op->InputAt(0)->GetType()), |
| 420 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | if (op->IsCompare()) { |
| 425 | if (op->GetType() != Primitive::kPrimInt) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 426 | AddError(StringPrintf( |
| 427 | "Compare operation %d has a non-int result type: %s.", |
| 428 | op->GetId(), |
| 429 | Primitive::PrettyDescriptor(op->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 430 | } |
| 431 | } else { |
| 432 | // Use the first input, so that we can also make this check for shift operations. |
| 433 | if (PrimitiveKind(op->GetType()) != PrimitiveKind(op->InputAt(0)->GetType())) { |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 434 | AddError(StringPrintf( |
| 435 | "Binary operation %s %d has a result type different " |
| 436 | "from its input type: %s vs %s.", |
| 437 | op->DebugName(), op->GetId(), |
| 438 | Primitive::PrettyDescriptor(op->GetType()), |
| 439 | Primitive::PrettyDescriptor(op->InputAt(1)->GetType()))); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 444 | } // namespace art |