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 | |
| 19 | #include <string> |
| 20 | #include <map> |
| 21 | #include <sstream> |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | void GraphChecker::VisitBasicBlock(HBasicBlock* block) { |
| 26 | current_block_ = block; |
| 27 | |
| 28 | // Check consistency with respect to predecessors of `block`. |
| 29 | const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 30 | std::map<HBasicBlock*, size_t> predecessors_count; |
| 31 | for (size_t i = 0, e = predecessors.Size(); i < e; ++i) { |
| 32 | HBasicBlock* p = predecessors.Get(i); |
| 33 | ++predecessors_count[p]; |
| 34 | } |
| 35 | for (auto& pc : predecessors_count) { |
| 36 | HBasicBlock* p = pc.first; |
| 37 | size_t p_count_in_block_predecessors = pc.second; |
| 38 | const GrowableArray<HBasicBlock*>& p_successors = p->GetSuccessors(); |
| 39 | size_t block_count_in_p_successors = 0; |
| 40 | for (size_t j = 0, f = p_successors.Size(); j < f; ++j) { |
| 41 | if (p_successors.Get(j) == block) { |
| 42 | ++block_count_in_p_successors; |
| 43 | } |
| 44 | } |
| 45 | if (p_count_in_block_predecessors != block_count_in_p_successors) { |
| 46 | std::stringstream error; |
| 47 | error << "Block " << block->GetBlockId() |
| 48 | << " lists " << p_count_in_block_predecessors |
| 49 | << " occurrences of block " << p->GetBlockId() |
| 50 | << " in its predecessors, whereas block " << p->GetBlockId() |
| 51 | << " lists " << block_count_in_p_successors |
| 52 | << " occurrences of block " << block->GetBlockId() |
| 53 | << " in its successors."; |
| 54 | errors_.Insert(error.str()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Check consistency with respect to successors of `block`. |
| 59 | const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors(); |
| 60 | std::map<HBasicBlock*, size_t> successors_count; |
| 61 | for (size_t i = 0, e = successors.Size(); i < e; ++i) { |
| 62 | HBasicBlock* s = successors.Get(i); |
| 63 | ++successors_count[s]; |
| 64 | } |
| 65 | for (auto& sc : successors_count) { |
| 66 | HBasicBlock* s = sc.first; |
| 67 | size_t s_count_in_block_successors = sc.second; |
| 68 | const GrowableArray<HBasicBlock*>& s_predecessors = s->GetPredecessors(); |
| 69 | size_t block_count_in_s_predecessors = 0; |
| 70 | for (size_t j = 0, f = s_predecessors.Size(); j < f; ++j) { |
| 71 | if (s_predecessors.Get(j) == block) { |
| 72 | ++block_count_in_s_predecessors; |
| 73 | } |
| 74 | } |
| 75 | if (s_count_in_block_successors != block_count_in_s_predecessors) { |
| 76 | std::stringstream error; |
| 77 | error << "Block " << block->GetBlockId() |
| 78 | << " lists " << s_count_in_block_successors |
| 79 | << " occurrences of block " << s->GetBlockId() |
| 80 | << " in its successors, whereas block " << s->GetBlockId() |
| 81 | << " lists " << block_count_in_s_predecessors |
| 82 | << " occurrences of block " << block->GetBlockId() |
| 83 | << " in its predecessors."; |
| 84 | errors_.Insert(error.str()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Ensure `block` ends with a branch instruction. |
| 89 | HInstruction* last_inst = block->GetLastInstruction(); |
| 90 | if (last_inst == nullptr || !last_inst->IsControlFlow()) { |
| 91 | std::stringstream error; |
| 92 | error << "Block " << block->GetBlockId() |
| 93 | << " does not end with a branch instruction."; |
| 94 | errors_.Insert(error.str()); |
| 95 | } |
| 96 | |
| 97 | // Visit this block's list of phis. |
| 98 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 99 | // Ensure this block's list of phis contains only phis. |
| 100 | if (!it.Current()->IsPhi()) { |
| 101 | std::stringstream error; |
| 102 | error << "Block " << current_block_->GetBlockId() |
| 103 | << " has a non-phi in its phi list."; |
| 104 | errors_.Insert(error.str()); |
| 105 | } |
| 106 | it.Current()->Accept(this); |
| 107 | } |
| 108 | |
| 109 | // Visit this block's list of instructions. |
| 110 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); |
| 111 | it.Advance()) { |
| 112 | // Ensure this block's list of instructions does not contains phis. |
| 113 | if (it.Current()->IsPhi()) { |
| 114 | std::stringstream error; |
| 115 | error << "Block " << current_block_->GetBlockId() |
| 116 | << " has a phi in its non-phi list."; |
| 117 | errors_.Insert(error.str()); |
| 118 | } |
| 119 | it.Current()->Accept(this); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void GraphChecker::VisitInstruction(HInstruction* instruction) { |
| 124 | // Ensure `instruction` is associated with `current_block_`. |
| 125 | if (instruction->GetBlock() != current_block_) { |
| 126 | std::stringstream error; |
| 127 | if (instruction->IsPhi()) { |
| 128 | error << "Phi "; |
| 129 | } else { |
| 130 | error << "Instruction "; |
| 131 | } |
| 132 | error << instruction->GetId() << " in block " |
| 133 | << current_block_->GetBlockId(); |
| 134 | if (instruction->GetBlock() != nullptr) { |
| 135 | error << " associated with block " |
| 136 | << instruction->GetBlock()->GetBlockId() << "."; |
| 137 | } else { |
| 138 | error << " not associated with any block."; |
| 139 | } |
| 140 | errors_.Insert(error.str()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void SSAChecker::VisitBasicBlock(HBasicBlock* block) { |
| 145 | super_type::VisitBasicBlock(block); |
| 146 | |
| 147 | // Ensure there is no critical edge (i.e., an edge connecting a |
| 148 | // block with multiple successors to a block with multiple |
| 149 | // predecessors). |
| 150 | if (block->GetSuccessors().Size() > 1) { |
| 151 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 152 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 153 | if (successor->GetPredecessors().Size() > 1) { |
| 154 | std::stringstream error; |
| 155 | error << "Critical edge between blocks " << block->GetBlockId() |
| 156 | << " and " << successor->GetBlockId() << "."; |
| 157 | errors_.Insert(error.str()); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void SSAChecker::VisitInstruction(HInstruction* instruction) { |
| 164 | super_type::VisitInstruction(instruction); |
| 165 | |
| 166 | // Ensure an instruction dominates all its uses (or in the present |
| 167 | // case, that all uses of an instruction (used as input) are |
| 168 | // dominated by its definition). |
| 169 | for (HInputIterator input_it(instruction); !input_it.Done(); |
| 170 | input_it.Advance()) { |
| 171 | HInstruction* input = input_it.Current(); |
| 172 | if (!input->Dominates(instruction)) { |
| 173 | std::stringstream error; |
| 174 | error << "Instruction " << input->GetId() |
| 175 | << " in block " << input->GetBlock()->GetBlockId() |
| 176 | << " does not dominate use " << instruction->GetId() |
| 177 | << " in block " << current_block_->GetBlockId() << "."; |
| 178 | errors_.Insert(error.str()); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace art |