blob: a1a5692ef659bbe20401821cd86d6a0ff5fe6ba6 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * 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
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
David Srbecky0cf44932015-12-09 14:09:59 +000020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
Andreas Gamped881df52014-11-24 23:28:39 -080022#include "base/logging.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010023#include "block_builder.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010024#include "data_type-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080025#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "driver/compiler_options.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000027#include "driver/dex_compilation_unit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010028#include "instruction_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "mirror/class_loader.h"
30#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031#include "nodes.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010032#include "optimizing_compiler_stats.h"
33#include "ssa_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010034#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000035#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036
37namespace art {
38
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010039HGraphBuilder::HGraphBuilder(HGraph* graph,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080040 const CodeItemDebugInfoAccessor& accessor,
Vladimir Markoca6fff82017-10-03 14:49:14 +010041 const DexCompilationUnit* dex_compilation_unit,
42 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010043 CompilerDriver* driver,
44 CodeGenerator* code_generator,
45 OptimizingCompilerStats* compiler_stats,
Mathieu Chartier210531f2018-01-12 10:15:51 -080046 ArrayRef<const uint8_t> interpreter_metadata,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010047 VariableSizedHandleScope* handles)
48 : graph_(graph),
49 dex_file_(&graph->GetDexFile()),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080050 code_item_accessor_(accessor),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051 dex_compilation_unit_(dex_compilation_unit),
Vladimir Marko69d310e2017-10-09 14:12:23 +010052 outer_compilation_unit_(outer_compilation_unit),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 compiler_driver_(driver),
Vladimir Marko69d310e2017-10-09 14:12:23 +010054 code_generator_(code_generator),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 compilation_stats_(compiler_stats),
Vladimir Marko69d310e2017-10-09 14:12:23 +010056 interpreter_metadata_(interpreter_metadata),
57 handles_(handles),
58 return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059
Mathieu Chartier808c7a52017-12-15 11:19:33 -080060HGraphBuilder::HGraphBuilder(HGraph* graph,
61 const DexCompilationUnit* dex_compilation_unit,
62 const CodeItemDebugInfoAccessor& accessor,
63 VariableSizedHandleScope* handles,
64 DataType::Type return_type)
65 : graph_(graph),
66 dex_file_(&graph->GetDexFile()),
67 code_item_accessor_(accessor),
68 dex_compilation_unit_(dex_compilation_unit),
69 outer_compilation_unit_(nullptr),
70 compiler_driver_(nullptr),
71 code_generator_(nullptr),
72 compilation_stats_(nullptr),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080073 handles_(handles),
74 return_type_(return_type) {}
75
David Brazdil86ea7ee2016-02-16 09:26:07 +000076bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
77 if (compiler_driver_ == nullptr) {
78 // Note that the compiler driver is null when unit testing.
79 return false;
80 }
81
Calin Juravle48c2b032014-12-09 18:11:36 +000082 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +000083 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
84 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000085 return false;
86 }
87
Mathieu Chartier808c7a52017-12-15 11:19:33 -080088 const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
89 if (compiler_options.IsHugeMethod(code_units)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000090 VLOG(compiler) << "Skip compilation of huge method "
David Sehr709b0702016-10-13 09:12:37 -070091 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Mathieu Chartier808c7a52017-12-15 11:19:33 -080092 << ": " << code_units << " code units";
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000093 MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000094 return true;
95 }
96
97 // If it's large and contains no branches, it's likely to be machine generated initialization.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080098 if (compiler_options.IsLargeMethod(code_units) && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000099 VLOG(compiler) << "Skip compilation of large method with no branch "
David Sehr709b0702016-10-13 09:12:37 -0700100 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800101 << ": " << code_units << " code units";
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000102 MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000103 return true;
104 }
105
106 return false;
107}
108
David Brazdildee58d62016-04-07 09:54:26 +0000109GraphAnalysisResult HGraphBuilder::BuildGraph() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800110 DCHECK(code_item_accessor_.HasCodeItem());
David Brazdil60328912016-04-04 17:47:42 +0000111 DCHECK(graph_->GetBlocks().empty());
David Brazdildee58d62016-04-07 09:54:26 +0000112
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800113 graph_->SetNumberOfVRegs(code_item_accessor_.RegistersSize());
114 graph_->SetNumberOfInVRegs(code_item_accessor_.InsSize());
115 graph_->SetMaximumNumberOfOutVRegs(code_item_accessor_.OutsSize());
116 graph_->SetHasTryCatch(code_item_accessor_.TriesSize() != 0);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000117
Vladimir Marko69d310e2017-10-09 14:12:23 +0100118 // Use ScopedArenaAllocator for all local allocations.
119 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800120 HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100121 SsaBuilder ssa_builder(graph_,
122 dex_compilation_unit_->GetClassLoader(),
123 dex_compilation_unit_->GetDexCache(),
124 handles_,
125 &local_allocator);
126 HInstructionBuilder instruction_builder(graph_,
127 &block_builder,
128 &ssa_builder,
129 dex_file_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800130 code_item_accessor_,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100131 return_type_,
132 dex_compilation_unit_,
133 outer_compilation_unit_,
134 compiler_driver_,
135 code_generator_,
136 interpreter_metadata_,
137 compilation_stats_,
138 handles_,
139 &local_allocator);
140
David Brazdil86ea7ee2016-02-16 09:26:07 +0000141 // 1) Create basic blocks and link them together. Basic blocks are left
142 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100143 if (!block_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000144 return kAnalysisInvalidBytecode;
145 }
146
147 // 2) Decide whether to skip this method based on its code size and number
148 // of branches.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100149 if (SkipCompilation(block_builder.GetNumberOfBranches())) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000150 return kAnalysisSkipped;
151 }
152
153 // 3) Build the dominator tree and fill in loop and try/catch metadata.
David Brazdilbadd8262016-02-02 16:28:56 +0000154 GraphAnalysisResult result = graph_->BuildDominatorTree();
155 if (result != kAnalysisSuccess) {
156 return result;
157 }
158
David Brazdil86ea7ee2016-02-16 09:26:07 +0000159 // 4) Populate basic blocks with instructions.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100160 if (!instruction_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000161 return kAnalysisInvalidBytecode;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000162 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163
David Brazdil86ea7ee2016-02-16 09:26:07 +0000164 // 5) Type the graph and eliminate dead/redundant phis.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100165 return ssa_builder.BuildSsa();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000166}
167
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000168void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800169 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000170 DCHECK(graph_->GetBlocks().empty());
171
172 // Determine the number of arguments and associated vregs.
173 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
174 const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx));
175 size_t num_args = strlen(shorty + 1);
176 size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') +
177 std::count(shorty + 1, shorty + 1 + num_args, 'D');
178 size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u);
179
180 // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type.
181 size_t return_vregs = 2u;
182 graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs);
183 graph_->SetNumberOfInVRegs(num_arg_vregs);
184 graph_->SetMaximumNumberOfOutVRegs(num_arg_vregs);
185 graph_->SetHasTryCatch(false);
186
187 // Use ScopedArenaAllocator for all local allocations.
188 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800189 HBasicBlockBuilder block_builder(graph_,
190 dex_file_,
191 CodeItemDebugInfoAccessor(),
192 &local_allocator);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000193 SsaBuilder ssa_builder(graph_,
194 dex_compilation_unit_->GetClassLoader(),
195 dex_compilation_unit_->GetDexCache(),
196 handles_,
197 &local_allocator);
198 HInstructionBuilder instruction_builder(graph_,
199 &block_builder,
200 &ssa_builder,
201 dex_file_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800202 CodeItemDebugInfoAccessor(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000203 return_type_,
204 dex_compilation_unit_,
205 outer_compilation_unit_,
206 compiler_driver_,
207 code_generator_,
208 interpreter_metadata_,
209 compilation_stats_,
210 handles_,
211 &local_allocator);
212
213 // 1) Create basic blocks for the intrinsic and link them together.
214 block_builder.BuildIntrinsic();
215
216 // 2) Build the trivial dominator tree.
217 GraphAnalysisResult bdt_result = graph_->BuildDominatorTree();
218 DCHECK_EQ(bdt_result, kAnalysisSuccess);
219
220 // 3) Populate basic blocks with instructions for the intrinsic.
221 instruction_builder.BuildIntrinsic(method);
222
223 // 4) Type the graph (no dead/redundant phis to eliminate).
224 GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa();
225 DCHECK_EQ(build_ssa_result, kAnalysisSuccess);
226}
227
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228} // namespace art