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 | #ifndef ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ |
| 19 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 20 | #include "base/scoped_arena_allocator.h" |
| 21 | #include "base/scoped_arena_containers.h" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 22 | #include "dex_file.h" |
| 23 | #include "nodes.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class HBasicBlockBuilder : public ValueObject { |
| 28 | public: |
| 29 | HBasicBlockBuilder(HGraph* graph, |
| 30 | const DexFile* const dex_file, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 31 | const DexFile::CodeItem* code_item, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 32 | ScopedArenaAllocator* local_allocator) |
| 33 | : allocator_(graph->GetAllocator()), |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 34 | graph_(graph), |
| 35 | dex_file_(dex_file), |
| 36 | code_item_(code_item), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 37 | local_allocator_(local_allocator), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 38 | branch_targets_(code_item != nullptr ? code_item->insns_size_in_code_units_ |
| 39 | : /* fake dex_pc=0 for intrinsic graph */ 1u, |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 40 | nullptr, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 41 | local_allocator->Adapter(kArenaAllocGraphBuilder)), |
| 42 | throwing_blocks_(kDefaultNumberOfThrowingBlocks, |
| 43 | local_allocator->Adapter(kArenaAllocGraphBuilder)), |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 44 | number_of_branches_(0u), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 45 | quicken_index_for_dex_pc_(std::less<uint32_t>(), |
| 46 | local_allocator->Adapter(kArenaAllocGraphBuilder)) {} |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 47 | |
| 48 | // Creates basic blocks in `graph_` at branch target dex_pc positions of the |
| 49 | // `code_item_`. Blocks are connected but left unpopulated with instructions. |
| 50 | // TryBoundary blocks are inserted at positions where control-flow enters/ |
| 51 | // exits a try block. |
| 52 | bool Build(); |
| 53 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 54 | // Creates basic blocks in `graph_` for compiling an intrinsic. |
| 55 | void BuildIntrinsic(); |
| 56 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 57 | size_t GetNumberOfBranches() const { return number_of_branches_; } |
| 58 | HBasicBlock* GetBlockAt(uint32_t dex_pc) const { return branch_targets_[dex_pc]; } |
| 59 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 60 | size_t GetQuickenIndex(uint32_t dex_pc) const; |
| 61 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 62 | private: |
| 63 | // Creates a basic block starting at given `dex_pc`. |
| 64 | HBasicBlock* MaybeCreateBlockAt(uint32_t dex_pc); |
| 65 | |
| 66 | // Creates a basic block for bytecode instructions at `semantic_dex_pc` and |
| 67 | // stores it under the `store_dex_pc` key. This is used when multiple blocks |
| 68 | // share the same semantic dex_pc, e.g. when building switch decision trees. |
| 69 | HBasicBlock* MaybeCreateBlockAt(uint32_t semantic_dex_pc, uint32_t store_dex_pc); |
| 70 | |
| 71 | bool CreateBranchTargets(); |
| 72 | void ConnectBasicBlocks(); |
| 73 | void InsertTryBoundaryBlocks(); |
| 74 | |
| 75 | // Helper method which decides whether `catch_block` may have live normal |
| 76 | // predecessors and thus whether a synthetic catch block needs to be created |
| 77 | // to avoid mixing normal and exceptional predecessors. |
| 78 | // Should only be called during InsertTryBoundaryBlocks on blocks at catch |
| 79 | // handler dex_pcs. |
| 80 | bool MightHaveLiveNormalPredecessors(HBasicBlock* catch_block); |
| 81 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 82 | ArenaAllocator* const allocator_; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 83 | HGraph* const graph_; |
| 84 | |
| 85 | const DexFile* const dex_file_; |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 86 | const DexFile::CodeItem* const code_item_; // null for intrinsic graph. |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 87 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 88 | ScopedArenaAllocator* const local_allocator_; |
| 89 | ScopedArenaVector<HBasicBlock*> branch_targets_; |
| 90 | ScopedArenaVector<HBasicBlock*> throwing_blocks_; |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 91 | size_t number_of_branches_; |
| 92 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 93 | // A table to quickly find the quicken index for the first instruction of a basic block. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 94 | ScopedArenaSafeMap<uint32_t, uint32_t> quicken_index_for_dex_pc_; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 95 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 96 | static constexpr size_t kDefaultNumberOfThrowingBlocks = 2u; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(HBasicBlockBuilder); |
| 99 | }; |
| 100 | |
| 101 | } // namespace art |
| 102 | |
| 103 | #endif // ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ |