blob: 79f7a7bc8124dde88278e118531258a35743b893 [file] [log] [blame]
David Brazdil86ea7ee2016-02-16 09:26:07 +00001/*
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 Marko69d310e2017-10-09 14:12:23 +010020#include "base/scoped_arena_allocator.h"
21#include "base/scoped_arena_containers.h"
David Brazdil86ea7ee2016-02-16 09:26:07 +000022#include "dex_file.h"
23#include "nodes.h"
24
25namespace art {
26
27class HBasicBlockBuilder : public ValueObject {
28 public:
29 HBasicBlockBuilder(HGraph* graph,
30 const DexFile* const dex_file,
Vladimir Marko69d310e2017-10-09 14:12:23 +010031 const DexFile::CodeItem& code_item,
32 ScopedArenaAllocator* local_allocator)
33 : allocator_(graph->GetAllocator()),
David Brazdil86ea7ee2016-02-16 09:26:07 +000034 graph_(graph),
35 dex_file_(dex_file),
36 code_item_(code_item),
Vladimir Marko69d310e2017-10-09 14:12:23 +010037 local_allocator_(local_allocator),
David Brazdil86ea7ee2016-02-16 09:26:07 +000038 branch_targets_(code_item.insns_size_in_code_units_,
39 nullptr,
Vladimir Marko69d310e2017-10-09 14:12:23 +010040 local_allocator->Adapter(kArenaAllocGraphBuilder)),
41 throwing_blocks_(kDefaultNumberOfThrowingBlocks,
42 local_allocator->Adapter(kArenaAllocGraphBuilder)),
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070043 number_of_branches_(0u),
Vladimir Marko69d310e2017-10-09 14:12:23 +010044 quicken_index_for_dex_pc_(std::less<uint32_t>(),
45 local_allocator->Adapter(kArenaAllocGraphBuilder)) {}
David Brazdil86ea7ee2016-02-16 09:26:07 +000046
47 // Creates basic blocks in `graph_` at branch target dex_pc positions of the
48 // `code_item_`. Blocks are connected but left unpopulated with instructions.
49 // TryBoundary blocks are inserted at positions where control-flow enters/
50 // exits a try block.
51 bool Build();
52
53 size_t GetNumberOfBranches() const { return number_of_branches_; }
54 HBasicBlock* GetBlockAt(uint32_t dex_pc) const { return branch_targets_[dex_pc]; }
55
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070056 size_t GetQuickenIndex(uint32_t dex_pc) const;
57
David Brazdil86ea7ee2016-02-16 09:26:07 +000058 private:
59 // Creates a basic block starting at given `dex_pc`.
60 HBasicBlock* MaybeCreateBlockAt(uint32_t dex_pc);
61
62 // Creates a basic block for bytecode instructions at `semantic_dex_pc` and
63 // stores it under the `store_dex_pc` key. This is used when multiple blocks
64 // share the same semantic dex_pc, e.g. when building switch decision trees.
65 HBasicBlock* MaybeCreateBlockAt(uint32_t semantic_dex_pc, uint32_t store_dex_pc);
66
67 bool CreateBranchTargets();
68 void ConnectBasicBlocks();
69 void InsertTryBoundaryBlocks();
70
71 // Helper method which decides whether `catch_block` may have live normal
72 // predecessors and thus whether a synthetic catch block needs to be created
73 // to avoid mixing normal and exceptional predecessors.
74 // Should only be called during InsertTryBoundaryBlocks on blocks at catch
75 // handler dex_pcs.
76 bool MightHaveLiveNormalPredecessors(HBasicBlock* catch_block);
77
Vladimir Marko69d310e2017-10-09 14:12:23 +010078 ArenaAllocator* const allocator_;
David Brazdil86ea7ee2016-02-16 09:26:07 +000079 HGraph* const graph_;
80
81 const DexFile* const dex_file_;
82 const DexFile::CodeItem& code_item_;
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084 ScopedArenaAllocator* const local_allocator_;
85 ScopedArenaVector<HBasicBlock*> branch_targets_;
86 ScopedArenaVector<HBasicBlock*> throwing_blocks_;
David Brazdil86ea7ee2016-02-16 09:26:07 +000087 size_t number_of_branches_;
88
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070089 // A table to quickly find the quicken index for the first instruction of a basic block.
Vladimir Marko69d310e2017-10-09 14:12:23 +010090 ScopedArenaSafeMap<uint32_t, uint32_t> quicken_index_for_dex_pc_;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070091
David Brazdil86ea7ee2016-02-16 09:26:07 +000092 static constexpr size_t kDefaultNumberOfThrowingBlocks = 2u;
93
94 DISALLOW_COPY_AND_ASSIGN(HBasicBlockBuilder);
95};
96
97} // namespace art
98
99#endif // ART_COMPILER_OPTIMIZING_BLOCK_BUILDER_H_