1 /*
2 * Copyright (C) 2014 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 #include "builder.h"
18
19 #include "art_field-inl.h"
20 #include "base/arena_bit_vector.h"
21 #include "base/bit_vector-inl.h"
22 #include "base/logging.h"
23 #include "block_builder.h"
24 #include "code_generator.h"
25 #include "data_type-inl.h"
26 #include "driver/compiler_options.h"
27 #include "driver/dex_compilation_unit.h"
28 #include "instruction_builder.h"
29 #include "mirror/class_loader.h"
30 #include "mirror/dex_cache.h"
31 #include "nodes.h"
32 #include "optimizing_compiler_stats.h"
33 #include "ssa_builder.h"
34 #include "thread.h"
35
36 namespace art HIDDEN {
37
HGraphBuilder(HGraph * graph,const CodeItemDebugInfoAccessor & accessor,const DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * outer_compilation_unit,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats)38 HGraphBuilder::HGraphBuilder(HGraph* graph,
39 const CodeItemDebugInfoAccessor& accessor,
40 const DexCompilationUnit* dex_compilation_unit,
41 const DexCompilationUnit* outer_compilation_unit,
42 CodeGenerator* code_generator,
43 OptimizingCompilerStats* compiler_stats)
44 : graph_(graph),
45 dex_file_(&graph->GetDexFile()),
46 code_item_accessor_(accessor),
47 dex_compilation_unit_(dex_compilation_unit),
48 outer_compilation_unit_(outer_compilation_unit),
49 code_generator_(code_generator),
50 compilation_stats_(compiler_stats),
51 return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
52
HGraphBuilder(HGraph * graph,const DexCompilationUnit * dex_compilation_unit,const CodeItemDebugInfoAccessor & accessor,DataType::Type return_type)53 HGraphBuilder::HGraphBuilder(HGraph* graph,
54 const DexCompilationUnit* dex_compilation_unit,
55 const CodeItemDebugInfoAccessor& accessor,
56 DataType::Type return_type)
57 : graph_(graph),
58 dex_file_(&graph->GetDexFile()),
59 code_item_accessor_(accessor),
60 dex_compilation_unit_(dex_compilation_unit),
61 outer_compilation_unit_(nullptr),
62 code_generator_(nullptr),
63 compilation_stats_(nullptr),
64 return_type_(return_type) {}
65
SkipCompilation(size_t number_of_branches)66 bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
67 if (code_generator_ == nullptr) {
68 // Note that the codegen is null when unit testing.
69 return false;
70 }
71
72 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
73 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
74 if (compiler_filter == CompilerFilter::kEverything) {
75 return false;
76 }
77
78 const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
79 if (compiler_options.IsHugeMethod(code_units)) {
80 VLOG(compiler) << "Skip compilation of huge method "
81 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
82 << ": " << code_units << " code units";
83 MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
84 return true;
85 }
86
87 // If it's large and contains no branches, it's likely to be machine generated initialization.
88 if (compiler_options.IsLargeMethod(code_units) && (number_of_branches == 0)) {
89 VLOG(compiler) << "Skip compilation of large method with no branch "
90 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
91 << ": " << code_units << " code units";
92 MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
93 return true;
94 }
95
96 return false;
97 }
98
BuildGraph()99 GraphAnalysisResult HGraphBuilder::BuildGraph() {
100 DCHECK(code_item_accessor_.HasCodeItem());
101 DCHECK(graph_->GetBlocks().empty());
102
103 graph_->SetNumberOfVRegs(code_item_accessor_.RegistersSize());
104 graph_->SetNumberOfInVRegs(code_item_accessor_.InsSize());
105 graph_->SetMaximumNumberOfOutVRegs(code_item_accessor_.OutsSize());
106
107 // Use ScopedArenaAllocator for all local allocations.
108 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
109 HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
110 SsaBuilder ssa_builder(graph_,
111 dex_compilation_unit_->GetClassLoader(),
112 dex_compilation_unit_->GetDexCache(),
113 &local_allocator);
114 HInstructionBuilder instruction_builder(graph_,
115 &block_builder,
116 &ssa_builder,
117 dex_file_,
118 code_item_accessor_,
119 return_type_,
120 dex_compilation_unit_,
121 outer_compilation_unit_,
122 code_generator_,
123 compilation_stats_,
124 &local_allocator);
125
126 // 1) Create basic blocks and link them together. Basic blocks are left
127 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
128 if (!block_builder.Build()) {
129 return kAnalysisInvalidBytecode;
130 }
131
132 // 2) Decide whether to skip this method based on its code size and number
133 // of branches.
134 if (SkipCompilation(block_builder.GetNumberOfBranches())) {
135 return kAnalysisSkipped;
136 }
137
138 // 3) Build the dominator tree and fill in loop and try/catch metadata.
139 GraphAnalysisResult result = graph_->BuildDominatorTree();
140 if (result != kAnalysisSuccess) {
141 return result;
142 }
143
144 // 4) Populate basic blocks with instructions.
145 if (!instruction_builder.Build()) {
146 return kAnalysisInvalidBytecode;
147 }
148
149 // 5) Type the graph and eliminate dead/redundant phis.
150 return ssa_builder.BuildSsa();
151 }
152
BuildIntrinsicGraph(ArtMethod * method)153 void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
154 DCHECK(!code_item_accessor_.HasCodeItem());
155 DCHECK(graph_->GetBlocks().empty());
156
157 // Determine the number of arguments and associated vregs.
158 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
159 const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx));
160 size_t num_args = strlen(shorty + 1);
161 size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') +
162 std::count(shorty + 1, shorty + 1 + num_args, 'D');
163 size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u);
164
165 // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type.
166 size_t return_vregs = 2u;
167 graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs);
168 graph_->SetNumberOfInVRegs(num_arg_vregs);
169 graph_->SetMaximumNumberOfOutVRegs(num_arg_vregs);
170
171 // Use ScopedArenaAllocator for all local allocations.
172 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
173 HBasicBlockBuilder block_builder(graph_,
174 dex_file_,
175 CodeItemDebugInfoAccessor(),
176 &local_allocator);
177 SsaBuilder ssa_builder(graph_,
178 dex_compilation_unit_->GetClassLoader(),
179 dex_compilation_unit_->GetDexCache(),
180 &local_allocator);
181 HInstructionBuilder instruction_builder(graph_,
182 &block_builder,
183 &ssa_builder,
184 dex_file_,
185 CodeItemDebugInfoAccessor(),
186 return_type_,
187 dex_compilation_unit_,
188 outer_compilation_unit_,
189 code_generator_,
190 compilation_stats_,
191 &local_allocator);
192
193 // 1) Create basic blocks for the intrinsic and link them together.
194 block_builder.BuildIntrinsic();
195
196 // 2) Build the trivial dominator tree.
197 GraphAnalysisResult bdt_result = graph_->BuildDominatorTree();
198 DCHECK_EQ(bdt_result, kAnalysisSuccess);
199
200 // 3) Populate basic blocks with instructions for the intrinsic.
201 instruction_builder.BuildIntrinsic(method);
202
203 // 4) Type the graph (no dead/redundant phis to eliminate).
204 GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa();
205 DCHECK_EQ(build_ssa_result, kAnalysisSuccess);
206 }
207
208 } // namespace art
209