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 
GetBlockAt(uint32_t dex_pc)45   HBasicBlock* GetBlockAt(uint32_t dex_pc) const { return branch_targets_[dex_pc]; }
46 
47  private:
48   // Creates a basic block starting at given `dex_pc`.
49   HBasicBlock* MaybeCreateBlockAt(uint32_t dex_pc);
50 
51   // Creates a basic block for bytecode instructions at `semantic_dex_pc` and
52   // stores it under the `store_dex_pc` key. This is used when multiple blocks
53   // share the same semantic dex_pc, e.g. when building switch decision trees.
54   HBasicBlock* MaybeCreateBlockAt(uint32_t semantic_dex_pc, uint32_t store_dex_pc);
55 
56   bool CreateBranchTargets();
57   void ConnectBasicBlocks();
58   void InsertTryBoundaryBlocks();
59 
60   // To ensure branches with negative offsets can always OSR jump to compiled
61   // code, we insert synthesized loops before each block that is the target of a
62   // negative branch.
63   void InsertSynthesizedLoopsForOsr();
64 
65   // Helper method which decides whether `catch_block` may have live normal
66   // predecessors and thus whether a synthetic catch block needs to be created
67   // to avoid mixing normal and exceptional predecessors.
68   // Should only be called during InsertTryBoundaryBlocks on blocks at catch
69   // handler dex_pcs.
70   bool MightHaveLiveNormalPredecessors(HBasicBlock* catch_block);
71 
72   ArenaAllocator* const allocator_;
73   HGraph* const graph_;
74 
75   const DexFile* const dex_file_;
76   CodeItemDataAccessor code_item_accessor_;  // null code item for intrinsic graph.
77 
78   ScopedArenaAllocator* const local_allocator_;
79   ScopedArenaVector<HBasicBlock*> branch_targets_;
80   ScopedArenaVector<HBasicBlock*> throwing_blocks_;
81 
82   static constexpr size_t kDefaultNumberOfThrowingBlocks = 2u;
83 
84   DISALLOW_COPY_AND_ASSIGN(HBasicBlockBuilder);
85 };
86 
87 }  // namespace art
88 
89 #endif  // ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_
90