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