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