1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 21*795d594fSAndroid Build Coastguard Worker #include "base/scoped_arena_allocator.h" 22*795d594fSAndroid Build Coastguard Worker #include "base/scoped_arena_containers.h" 23*795d594fSAndroid Build Coastguard Worker #include "dex/code_item_accessors.h" 24*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h" 25*795d594fSAndroid Build Coastguard Worker #include "nodes.h" 26*795d594fSAndroid Build Coastguard Worker 27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 28*795d594fSAndroid Build Coastguard Worker 29*795d594fSAndroid Build Coastguard Worker class HBasicBlockBuilder : public ValueObject { 30*795d594fSAndroid Build Coastguard Worker public: 31*795d594fSAndroid Build Coastguard Worker HBasicBlockBuilder(HGraph* graph, 32*795d594fSAndroid Build Coastguard Worker const DexFile* const dex_file, 33*795d594fSAndroid Build Coastguard Worker const CodeItemDebugInfoAccessor& accessor, 34*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator* local_allocator); 35*795d594fSAndroid Build Coastguard Worker 36*795d594fSAndroid Build Coastguard Worker // Creates basic blocks in `graph_` at branch target dex_pc positions of the 37*795d594fSAndroid Build Coastguard Worker // `code_item_`. Blocks are connected but left unpopulated with instructions. 38*795d594fSAndroid Build Coastguard Worker // TryBoundary blocks are inserted at positions where control-flow enters/ 39*795d594fSAndroid Build Coastguard Worker // exits a try block. 40*795d594fSAndroid Build Coastguard Worker bool Build(); 41*795d594fSAndroid Build Coastguard Worker 42*795d594fSAndroid Build Coastguard Worker // Creates basic blocks in `graph_` for compiling an intrinsic. 43*795d594fSAndroid Build Coastguard Worker void BuildIntrinsic(); 44*795d594fSAndroid Build Coastguard Worker GetBlockAt(uint32_t dex_pc)45*795d594fSAndroid Build Coastguard Worker HBasicBlock* GetBlockAt(uint32_t dex_pc) const { return branch_targets_[dex_pc]; } 46*795d594fSAndroid Build Coastguard Worker 47*795d594fSAndroid Build Coastguard Worker private: 48*795d594fSAndroid Build Coastguard Worker // Creates a basic block starting at given `dex_pc`. 49*795d594fSAndroid Build Coastguard Worker HBasicBlock* MaybeCreateBlockAt(uint32_t dex_pc); 50*795d594fSAndroid Build Coastguard Worker 51*795d594fSAndroid Build Coastguard Worker // Creates a basic block for bytecode instructions at `semantic_dex_pc` and 52*795d594fSAndroid Build Coastguard Worker // stores it under the `store_dex_pc` key. This is used when multiple blocks 53*795d594fSAndroid Build Coastguard Worker // share the same semantic dex_pc, e.g. when building switch decision trees. 54*795d594fSAndroid Build Coastguard Worker HBasicBlock* MaybeCreateBlockAt(uint32_t semantic_dex_pc, uint32_t store_dex_pc); 55*795d594fSAndroid Build Coastguard Worker 56*795d594fSAndroid Build Coastguard Worker bool CreateBranchTargets(); 57*795d594fSAndroid Build Coastguard Worker void ConnectBasicBlocks(); 58*795d594fSAndroid Build Coastguard Worker void InsertTryBoundaryBlocks(); 59*795d594fSAndroid Build Coastguard Worker 60*795d594fSAndroid Build Coastguard Worker // To ensure branches with negative offsets can always OSR jump to compiled 61*795d594fSAndroid Build Coastguard Worker // code, we insert synthesized loops before each block that is the target of a 62*795d594fSAndroid Build Coastguard Worker // negative branch. 63*795d594fSAndroid Build Coastguard Worker void InsertSynthesizedLoopsForOsr(); 64*795d594fSAndroid Build Coastguard Worker 65*795d594fSAndroid Build Coastguard Worker // Helper method which decides whether `catch_block` may have live normal 66*795d594fSAndroid Build Coastguard Worker // predecessors and thus whether a synthetic catch block needs to be created 67*795d594fSAndroid Build Coastguard Worker // to avoid mixing normal and exceptional predecessors. 68*795d594fSAndroid Build Coastguard Worker // Should only be called during InsertTryBoundaryBlocks on blocks at catch 69*795d594fSAndroid Build Coastguard Worker // handler dex_pcs. 70*795d594fSAndroid Build Coastguard Worker bool MightHaveLiveNormalPredecessors(HBasicBlock* catch_block); 71*795d594fSAndroid Build Coastguard Worker 72*795d594fSAndroid Build Coastguard Worker ArenaAllocator* const allocator_; 73*795d594fSAndroid Build Coastguard Worker HGraph* const graph_; 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker const DexFile* const dex_file_; 76*795d594fSAndroid Build Coastguard Worker CodeItemDataAccessor code_item_accessor_; // null code item for intrinsic graph. 77*795d594fSAndroid Build Coastguard Worker 78*795d594fSAndroid Build Coastguard Worker ScopedArenaAllocator* const local_allocator_; 79*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HBasicBlock*> branch_targets_; 80*795d594fSAndroid Build Coastguard Worker ScopedArenaVector<HBasicBlock*> throwing_blocks_; 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker static constexpr size_t kDefaultNumberOfThrowingBlocks = 2u; 83*795d594fSAndroid Build Coastguard Worker 84*795d594fSAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(HBasicBlockBuilder); 85*795d594fSAndroid Build Coastguard Worker }; 86*795d594fSAndroid Build Coastguard Worker 87*795d594fSAndroid Build Coastguard Worker } // namespace art 88*795d594fSAndroid Build Coastguard Worker 89*795d594fSAndroid Build Coastguard Worker #endif // ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_ 90