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