• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "base/macros.h"
21 #include "base/scoped_arena_allocator.h"
22 #include "base/scoped_arena_containers.h"
23 #include "dex/code_item_accessors.h"
24 #include "dex/dex_file.h"
25 #include "nodes.h"
26 
27 namespace art HIDDEN {
28 
29 class HBasicBlockBuilder : public ValueObject {
30  public:
31   HBasicBlockBuilder(HGraph* graph,
32                      const DexFile* const dex_file,
33                      const CodeItemDebugInfoAccessor& accessor,
34                      ScopedArenaAllocator* local_allocator);
35 
36   // Creates basic blocks in `graph_` at branch target dex_pc positions of the
37   // `code_item_`. Blocks are connected but left unpopulated with instructions.
38   // TryBoundary blocks are inserted at positions where control-flow enters/
39   // exits a try block.
40   bool Build();
41 
42   // Creates basic blocks in `graph_` for compiling an intrinsic.
43   void BuildIntrinsic();
44 
GetNumberOfBranches()45   size_t GetNumberOfBranches() const { return number_of_branches_; }
GetBlockAt(uint32_t dex_pc)46   HBasicBlock* GetBlockAt(uint32_t dex_pc) const { return branch_targets_[dex_pc]; }
47 
48   size_t GetQuickenIndex(uint32_t dex_pc) const;
49 
50  private:
51   // Creates a basic block starting at given `dex_pc`.
52   HBasicBlock* MaybeCreateBlockAt(uint32_t dex_pc);
53 
54   // Creates a basic block for bytecode instructions at `semantic_dex_pc` and
55   // stores it under the `store_dex_pc` key. This is used when multiple blocks
56   // share the same semantic dex_pc, e.g. when building switch decision trees.
57   HBasicBlock* MaybeCreateBlockAt(uint32_t semantic_dex_pc, uint32_t store_dex_pc);
58 
59   bool CreateBranchTargets();
60   void ConnectBasicBlocks();
61   void InsertTryBoundaryBlocks();
62 
63   // To ensure branches with negative offsets can always OSR jump to compiled
64   // code, we insert synthesized loops before each block that is the target of a
65   // negative branch.
66   void InsertSynthesizedLoopsForOsr();
67 
68   // Helper method which decides whether `catch_block` may have live normal
69   // predecessors and thus whether a synthetic catch block needs to be created
70   // to avoid mixing normal and exceptional predecessors.
71   // Should only be called during InsertTryBoundaryBlocks on blocks at catch
72   // handler dex_pcs.
73   bool MightHaveLiveNormalPredecessors(HBasicBlock* catch_block);
74 
75   ArenaAllocator* const allocator_;
76   HGraph* const graph_;
77 
78   const DexFile* const dex_file_;
79   CodeItemDataAccessor code_item_accessor_;  // null code item for intrinsic graph.
80 
81   ScopedArenaAllocator* const local_allocator_;
82   ScopedArenaVector<HBasicBlock*> branch_targets_;
83   ScopedArenaVector<HBasicBlock*> throwing_blocks_;
84   size_t number_of_branches_;
85 
86   // A table to quickly find the quicken index for the first instruction of a basic block.
87   ScopedArenaSafeMap<uint32_t, uint32_t> quicken_index_for_dex_pc_;
88 
89   static constexpr size_t kDefaultNumberOfThrowingBlocks = 2u;
90 
91   DISALLOW_COPY_AND_ASSIGN(HBasicBlockBuilder);
92 };
93 
94 }  // namespace art
95 
96 #endif  // ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_
97