David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "block_builder.h" |
| 18 | |
| 19 | #include "bytecode_utils.h" |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 20 | #include "quicken_info.h" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t dex_pc) { |
| 25 | return MaybeCreateBlockAt(dex_pc, dex_pc); |
| 26 | } |
| 27 | |
| 28 | HBasicBlock* HBasicBlockBuilder::MaybeCreateBlockAt(uint32_t semantic_dex_pc, |
| 29 | uint32_t store_dex_pc) { |
| 30 | HBasicBlock* block = branch_targets_[store_dex_pc]; |
| 31 | if (block == nullptr) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 32 | block = new (allocator_) HBasicBlock(graph_, semantic_dex_pc); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 33 | branch_targets_[store_dex_pc] = block; |
| 34 | } |
| 35 | DCHECK_EQ(block->GetDexPc(), semantic_dex_pc); |
| 36 | return block; |
| 37 | } |
| 38 | |
| 39 | bool HBasicBlockBuilder::CreateBranchTargets() { |
| 40 | // Create the first block for the dex instructions, single successor of the entry block. |
| 41 | MaybeCreateBlockAt(0u); |
| 42 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 43 | if (code_item_->tries_size_ != 0) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 44 | // Create branch targets at the start/end of the TryItem range. These are |
| 45 | // places where the program might fall through into/out of the a block and |
| 46 | // where TryBoundary instructions will be inserted later. Other edges which |
| 47 | // enter/exit the try blocks are a result of branches/switches. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 48 | for (size_t idx = 0; idx < code_item_->tries_size_; ++idx) { |
| 49 | const DexFile::TryItem* try_item = DexFile::GetTryItems(*code_item_, idx); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 50 | uint32_t dex_pc_start = try_item->start_addr_; |
| 51 | uint32_t dex_pc_end = dex_pc_start + try_item->insn_count_; |
| 52 | MaybeCreateBlockAt(dex_pc_start); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 53 | if (dex_pc_end < code_item_->insns_size_in_code_units_) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 54 | // TODO: Do not create block if the last instruction cannot fall through. |
| 55 | MaybeCreateBlockAt(dex_pc_end); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 56 | } else if (dex_pc_end == code_item_->insns_size_in_code_units_) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 57 | // The TryItem spans until the very end of the CodeItem and therefore |
| 58 | // cannot have any code afterwards. |
| 59 | } else { |
| 60 | // The TryItem spans beyond the end of the CodeItem. This is invalid code. |
Nicolas Geoffray | dbb9aef | 2017-11-23 10:44:11 +0000 | [diff] [blame] | 61 | VLOG(compiler) << "Not compiled: TryItem spans beyond the end of the CodeItem"; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Create branch targets for exception handlers. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 67 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 68 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 69 | for (uint32_t idx = 0; idx < handlers_size; ++idx) { |
| 70 | CatchHandlerIterator iterator(handlers_ptr); |
| 71 | for (; iterator.HasNext(); iterator.Next()) { |
| 72 | MaybeCreateBlockAt(iterator.GetHandlerAddress()); |
| 73 | } |
| 74 | handlers_ptr = iterator.EndDataPointer(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 79 | // the locations these instructions branch to. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 80 | IterationRange<DexInstructionIterator> instructions = code_item_->Instructions(); |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 81 | for (const DexInstructionPcPair& pair : instructions) { |
| 82 | const uint32_t dex_pc = pair.DexPc(); |
| 83 | const Instruction& instruction = pair.Inst(); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 84 | |
| 85 | if (instruction.IsBranch()) { |
| 86 | number_of_branches_++; |
| 87 | MaybeCreateBlockAt(dex_pc + instruction.GetTargetOffset()); |
| 88 | } else if (instruction.IsSwitch()) { |
| 89 | DexSwitchTable table(instruction, dex_pc); |
| 90 | for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) { |
| 91 | MaybeCreateBlockAt(dex_pc + s_it.CurrentTargetOffset()); |
| 92 | |
| 93 | // Create N-1 blocks where we will insert comparisons of the input value |
| 94 | // against the Switch's case keys. |
| 95 | if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) { |
| 96 | // Store the block under dex_pc of the current key at the switch data |
| 97 | // instruction for uniqueness but give it the dex_pc of the SWITCH |
| 98 | // instruction which it semantically belongs to. |
| 99 | MaybeCreateBlockAt(dex_pc, s_it.GetDexPcForCurrentIndex()); |
| 100 | } |
| 101 | } |
| 102 | } else if (instruction.Opcode() == Instruction::MOVE_EXCEPTION) { |
| 103 | // End the basic block after MOVE_EXCEPTION. This simplifies the later |
| 104 | // stage of TryBoundary-block insertion. |
| 105 | } else { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | if (instruction.CanFlowThrough()) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 110 | DexInstructionIterator next(std::next(DexInstructionIterator(pair))); |
| 111 | if (next == instructions.end()) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 112 | // In the normal case we should never hit this but someone can artificially forge a dex |
| 113 | // file to fall-through out the method code. In this case we bail out compilation. |
Nicolas Geoffray | dbb9aef | 2017-11-23 10:44:11 +0000 | [diff] [blame] | 114 | VLOG(compiler) << "Not compiled: Fall-through beyond the CodeItem"; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 115 | return false; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 116 | } |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 117 | MaybeCreateBlockAt(next.DexPc()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | void HBasicBlockBuilder::ConnectBasicBlocks() { |
| 125 | HBasicBlock* block = graph_->GetEntryBlock(); |
| 126 | graph_->AddBlock(block); |
| 127 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 128 | size_t quicken_index = 0; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 129 | bool is_throwing_block = false; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 130 | // Calculate the qucikening index here instead of CreateBranchTargets since it's easier to |
| 131 | // calculate in dex_pc order. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 132 | for (const DexInstructionPcPair& pair : code_item_->Instructions()) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 133 | const uint32_t dex_pc = pair.DexPc(); |
| 134 | const Instruction& instruction = pair.Inst(); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 135 | |
| 136 | // Check if this dex_pc address starts a new basic block. |
| 137 | HBasicBlock* next_block = GetBlockAt(dex_pc); |
| 138 | if (next_block != nullptr) { |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 139 | // We only need quicken index entries for basic block boundaries. |
| 140 | quicken_index_for_dex_pc_.Put(dex_pc, quicken_index); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 141 | if (block != nullptr) { |
| 142 | // Last instruction did not end its basic block but a new one starts here. |
| 143 | // It must have been a block falling through into the next one. |
| 144 | block->AddSuccessor(next_block); |
| 145 | } |
| 146 | block = next_block; |
| 147 | is_throwing_block = false; |
| 148 | graph_->AddBlock(block); |
| 149 | } |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 150 | // Make sure to increment this before the continues. |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 151 | if (QuickenInfoTable::NeedsIndexForInstruction(&instruction)) { |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 152 | ++quicken_index; |
| 153 | } |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 154 | |
| 155 | if (block == nullptr) { |
| 156 | // Ignore dead code. |
| 157 | continue; |
| 158 | } |
| 159 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 160 | if (!is_throwing_block && IsThrowingDexInstruction(instruction)) { |
| 161 | DCHECK(!ContainsElement(throwing_blocks_, block)); |
| 162 | is_throwing_block = true; |
| 163 | throwing_blocks_.push_back(block); |
| 164 | } |
| 165 | |
| 166 | if (instruction.IsBranch()) { |
| 167 | uint32_t target_dex_pc = dex_pc + instruction.GetTargetOffset(); |
| 168 | block->AddSuccessor(GetBlockAt(target_dex_pc)); |
| 169 | } else if (instruction.IsReturn() || (instruction.Opcode() == Instruction::THROW)) { |
| 170 | block->AddSuccessor(graph_->GetExitBlock()); |
| 171 | } else if (instruction.IsSwitch()) { |
| 172 | DexSwitchTable table(instruction, dex_pc); |
| 173 | for (DexSwitchTableIterator s_it(table); !s_it.Done(); s_it.Advance()) { |
| 174 | uint32_t target_dex_pc = dex_pc + s_it.CurrentTargetOffset(); |
| 175 | block->AddSuccessor(GetBlockAt(target_dex_pc)); |
| 176 | |
| 177 | if (table.ShouldBuildDecisionTree() && !s_it.IsLast()) { |
| 178 | uint32_t next_case_dex_pc = s_it.GetDexPcForCurrentIndex(); |
| 179 | HBasicBlock* next_case_block = GetBlockAt(next_case_dex_pc); |
| 180 | block->AddSuccessor(next_case_block); |
| 181 | block = next_case_block; |
| 182 | graph_->AddBlock(block); |
| 183 | } |
| 184 | } |
| 185 | } else { |
| 186 | // Remaining code only applies to instructions which end their basic block. |
| 187 | continue; |
| 188 | } |
| 189 | |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 190 | // Go to the next instruction in case we read dex PC below. |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 191 | if (instruction.CanFlowThrough()) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 192 | block->AddSuccessor(GetBlockAt(std::next(DexInstructionIterator(pair)).DexPc())); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // The basic block ends here. Do not add any more instructions. |
| 196 | block = nullptr; |
| 197 | } |
| 198 | |
| 199 | graph_->AddBlock(graph_->GetExitBlock()); |
| 200 | } |
| 201 | |
| 202 | // Returns the TryItem stored for `block` or nullptr if there is no info for it. |
| 203 | static const DexFile::TryItem* GetTryItem( |
| 204 | HBasicBlock* block, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 205 | const ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*>& try_block_info) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 206 | auto iterator = try_block_info.find(block->GetBlockId()); |
| 207 | return (iterator == try_block_info.end()) ? nullptr : iterator->second; |
| 208 | } |
| 209 | |
| 210 | // Iterates over the exception handlers of `try_item`, finds the corresponding |
| 211 | // catch blocks and makes them successors of `try_boundary`. The order of |
| 212 | // successors matches the order in which runtime exception delivery searches |
| 213 | // for a handler. |
| 214 | static void LinkToCatchBlocks(HTryBoundary* try_boundary, |
| 215 | const DexFile::CodeItem& code_item, |
| 216 | const DexFile::TryItem* try_item, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 217 | const ScopedArenaSafeMap<uint32_t, HBasicBlock*>& catch_blocks) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 218 | for (CatchHandlerIterator it(code_item, *try_item); it.HasNext(); it.Next()) { |
| 219 | try_boundary->AddExceptionHandler(catch_blocks.Get(it.GetHandlerAddress())); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | bool HBasicBlockBuilder::MightHaveLiveNormalPredecessors(HBasicBlock* catch_block) { |
| 224 | if (kIsDebugBuild) { |
| 225 | DCHECK_NE(catch_block->GetDexPc(), kNoDexPc) << "Should not be called on synthetic blocks"; |
| 226 | DCHECK(!graph_->GetEntryBlock()->GetSuccessors().empty()) |
| 227 | << "Basic blocks must have been created and connected"; |
| 228 | for (HBasicBlock* predecessor : catch_block->GetPredecessors()) { |
| 229 | DCHECK(!predecessor->IsSingleTryBoundary()) |
| 230 | << "TryBoundary blocks must not have not been created yet"; |
| 231 | } |
| 232 | } |
| 233 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 234 | const Instruction& first = code_item_->InstructionAt(catch_block->GetDexPc()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 235 | if (first.Opcode() == Instruction::MOVE_EXCEPTION) { |
| 236 | // Verifier guarantees that if a catch block begins with MOVE_EXCEPTION then |
| 237 | // it has no live normal predecessors. |
| 238 | return false; |
| 239 | } else if (catch_block->GetPredecessors().empty()) { |
| 240 | // Normal control-flow edges have already been created. Since block's list of |
| 241 | // predecessors is empty, it cannot have any live or dead normal predecessors. |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // The catch block has normal predecessors but we do not know which are live |
| 246 | // and which will be removed during the initial DCE. Return `true` to signal |
| 247 | // that it may have live normal predecessors. |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | void HBasicBlockBuilder::InsertTryBoundaryBlocks() { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 252 | if (code_item_->tries_size_ == 0) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Keep a map of all try blocks and their respective TryItems. We do not use |
| 257 | // the block's pointer but rather its id to ensure deterministic iteration. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 258 | ScopedArenaSafeMap<uint32_t, const DexFile::TryItem*> try_block_info( |
| 259 | std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 260 | |
| 261 | // Obtain TryItem information for blocks with throwing instructions, and split |
| 262 | // blocks which are both try & catch to simplify the graph. |
| 263 | for (HBasicBlock* block : graph_->GetBlocks()) { |
| 264 | if (block->GetDexPc() == kNoDexPc) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | // Do not bother creating exceptional edges for try blocks which have no |
| 269 | // throwing instructions. In that case we simply assume that the block is |
| 270 | // not covered by a TryItem. This prevents us from creating a throw-catch |
| 271 | // loop for synchronized blocks. |
| 272 | if (ContainsElement(throwing_blocks_, block)) { |
| 273 | // Try to find a TryItem covering the block. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 274 | const int32_t try_item_idx = DexFile::FindTryItem(DexFile::GetTryItems(*code_item_, 0u), |
| 275 | code_item_->tries_size_, |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 276 | block->GetDexPc()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 277 | if (try_item_idx != -1) { |
| 278 | // Block throwing and in a TryItem. Store the try block information. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 279 | try_block_info.Put(block->GetBlockId(), DexFile::GetTryItems(*code_item_, try_item_idx)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Map from a handler dex_pc to the corresponding catch block. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 285 | ScopedArenaSafeMap<uint32_t, HBasicBlock*> catch_blocks( |
| 286 | std::less<uint32_t>(), local_allocator_->Adapter(kArenaAllocGraphBuilder)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 287 | |
| 288 | // Iterate over catch blocks, create artifical landing pads if necessary to |
| 289 | // simplify the CFG, and set metadata. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 290 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 291 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 292 | for (uint32_t idx = 0; idx < handlers_size; ++idx) { |
| 293 | CatchHandlerIterator iterator(handlers_ptr); |
| 294 | for (; iterator.HasNext(); iterator.Next()) { |
| 295 | uint32_t address = iterator.GetHandlerAddress(); |
| 296 | if (catch_blocks.find(address) != catch_blocks.end()) { |
| 297 | // Catch block already processed. |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | // Check if we should create an artifical landing pad for the catch block. |
| 302 | // We create one if the catch block is also a try block because we do not |
| 303 | // have a strategy for inserting TryBoundaries on exceptional edges. |
| 304 | // We also create one if the block might have normal predecessors so as to |
| 305 | // simplify register allocation. |
| 306 | HBasicBlock* catch_block = GetBlockAt(address); |
| 307 | bool is_try_block = (try_block_info.find(catch_block->GetBlockId()) != try_block_info.end()); |
| 308 | if (is_try_block || MightHaveLiveNormalPredecessors(catch_block)) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 309 | HBasicBlock* new_catch_block = new (allocator_) HBasicBlock(graph_, address); |
| 310 | new_catch_block->AddInstruction(new (allocator_) HGoto(address)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 311 | new_catch_block->AddSuccessor(catch_block); |
| 312 | graph_->AddBlock(new_catch_block); |
| 313 | catch_block = new_catch_block; |
| 314 | } |
| 315 | |
| 316 | catch_blocks.Put(address, catch_block); |
| 317 | catch_block->SetTryCatchInformation( |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 318 | new (allocator_) TryCatchInformation(iterator.GetHandlerTypeIndex(), *dex_file_)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 319 | } |
| 320 | handlers_ptr = iterator.EndDataPointer(); |
| 321 | } |
| 322 | |
| 323 | // Do a pass over the try blocks and insert entering TryBoundaries where at |
| 324 | // least one predecessor is not covered by the same TryItem as the try block. |
| 325 | // We do not split each edge separately, but rather create one boundary block |
| 326 | // that all predecessors are relinked to. This preserves loop headers (b/23895756). |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 327 | for (const auto& entry : try_block_info) { |
| 328 | uint32_t block_id = entry.first; |
| 329 | const DexFile::TryItem* try_item = entry.second; |
| 330 | HBasicBlock* try_block = graph_->GetBlocks()[block_id]; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 331 | for (HBasicBlock* predecessor : try_block->GetPredecessors()) { |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 332 | if (GetTryItem(predecessor, try_block_info) != try_item) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 333 | // Found a predecessor not covered by the same TryItem. Insert entering |
| 334 | // boundary block. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 335 | HTryBoundary* try_entry = new (allocator_) HTryBoundary( |
| 336 | HTryBoundary::BoundaryKind::kEntry, try_block->GetDexPc()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 337 | try_block->CreateImmediateDominator()->AddInstruction(try_entry); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 338 | LinkToCatchBlocks(try_entry, *code_item_, try_item, catch_blocks); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Do a second pass over the try blocks and insert exit TryBoundaries where |
| 345 | // the successor is not in the same TryItem. |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 346 | for (const auto& entry : try_block_info) { |
| 347 | uint32_t block_id = entry.first; |
| 348 | const DexFile::TryItem* try_item = entry.second; |
| 349 | HBasicBlock* try_block = graph_->GetBlocks()[block_id]; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 350 | // NOTE: Do not use iterators because SplitEdge would invalidate them. |
| 351 | for (size_t i = 0, e = try_block->GetSuccessors().size(); i < e; ++i) { |
| 352 | HBasicBlock* successor = try_block->GetSuccessors()[i]; |
| 353 | |
| 354 | // If the successor is a try block, all of its predecessors must be |
| 355 | // covered by the same TryItem. Otherwise the previous pass would have |
| 356 | // created a non-throwing boundary block. |
| 357 | if (GetTryItem(successor, try_block_info) != nullptr) { |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 358 | DCHECK_EQ(try_item, GetTryItem(successor, try_block_info)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 359 | continue; |
| 360 | } |
| 361 | |
| 362 | // Insert TryBoundary and link to catch blocks. |
| 363 | HTryBoundary* try_exit = |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 364 | new (allocator_) HTryBoundary(HTryBoundary::BoundaryKind::kExit, successor->GetDexPc()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 365 | graph_->SplitEdge(try_block, successor)->AddInstruction(try_exit); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 366 | LinkToCatchBlocks(try_exit, *code_item_, try_item, catch_blocks); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool HBasicBlockBuilder::Build() { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 372 | DCHECK(code_item_ != nullptr); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 373 | DCHECK(graph_->GetBlocks().empty()); |
| 374 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 375 | graph_->SetEntryBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc)); |
| 376 | graph_->SetExitBlock(new (allocator_) HBasicBlock(graph_, kNoDexPc)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 377 | |
| 378 | // TODO(dbrazdil): Do CreateBranchTargets and ConnectBasicBlocks in one pass. |
| 379 | if (!CreateBranchTargets()) { |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | ConnectBasicBlocks(); |
| 384 | InsertTryBoundaryBlocks(); |
| 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 389 | void HBasicBlockBuilder::BuildIntrinsic() { |
| 390 | DCHECK(code_item_ == nullptr); |
| 391 | DCHECK(graph_->GetBlocks().empty()); |
| 392 | |
| 393 | // Create blocks. |
| 394 | HBasicBlock* entry_block = new (allocator_) HBasicBlock(graph_, kNoDexPc); |
| 395 | HBasicBlock* exit_block = new (allocator_) HBasicBlock(graph_, kNoDexPc); |
| 396 | HBasicBlock* body = MaybeCreateBlockAt(/* semantic_dex_pc */ kNoDexPc, /* store_dex_pc */ 0u); |
| 397 | |
| 398 | // Add blocks to the graph. |
| 399 | graph_->AddBlock(entry_block); |
| 400 | graph_->AddBlock(body); |
| 401 | graph_->AddBlock(exit_block); |
| 402 | graph_->SetEntryBlock(entry_block); |
| 403 | graph_->SetExitBlock(exit_block); |
| 404 | |
| 405 | // Connect blocks. |
| 406 | entry_block->AddSuccessor(body); |
| 407 | body->AddSuccessor(exit_block); |
| 408 | } |
| 409 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 410 | size_t HBasicBlockBuilder::GetQuickenIndex(uint32_t dex_pc) const { |
| 411 | return quicken_index_for_dex_pc_.Get(dex_pc); |
| 412 | } |
| 413 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 414 | } // namespace art |