blob: 3263e9ca3ad53548660bd3b8db401c4c77e7b5ce [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 Markoc1c34522018-10-31 13:56:49 +000024#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010025#include "data_type-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080026#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000028#include "driver/dex_compilation_unit.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010029#include "instruction_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010030#include "mirror/class_loader.h"
31#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "nodes.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "optimizing_compiler_stats.h"
34#include "ssa_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010035#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036
Vladimir Marko0a516052019-10-14 13:00:44 +000037namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
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 CodeGenerator* code_generator,
Nicolas Geoffray4924ea92021-03-23 08:25:31 +000044 OptimizingCompilerStats* compiler_stats)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010045 : graph_(graph),
46 dex_file_(&graph->GetDexFile()),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080047 code_item_accessor_(accessor),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010048 dex_compilation_unit_(dex_compilation_unit),
Vladimir Marko69d310e2017-10-09 14:12:23 +010049 outer_compilation_unit_(outer_compilation_unit),
Vladimir Marko69d310e2017-10-09 14:12:23 +010050 code_generator_(code_generator),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051 compilation_stats_(compiler_stats),
Vladimir Marko69d310e2017-10-09 14:12:23 +010052 return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053
Mathieu Chartier808c7a52017-12-15 11:19:33 -080054HGraphBuilder::HGraphBuilder(HGraph* graph,
55 const DexCompilationUnit* dex_compilation_unit,
56 const CodeItemDebugInfoAccessor& accessor,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080057 DataType::Type return_type)
58 : graph_(graph),
59 dex_file_(&graph->GetDexFile()),
60 code_item_accessor_(accessor),
61 dex_compilation_unit_(dex_compilation_unit),
62 outer_compilation_unit_(nullptr),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080063 code_generator_(nullptr),
64 compilation_stats_(nullptr),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080065 return_type_(return_type) {}
66
David Brazdil86ea7ee2016-02-16 09:26:07 +000067bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
Vladimir Markoc1c34522018-10-31 13:56:49 +000068 if (code_generator_ == nullptr) {
69 // Note that the codegen is null when unit testing.
David Brazdil86ea7ee2016-02-16 09:26:07 +000070 return false;
71 }
72
Vladimir Markoc1c34522018-10-31 13:56:49 +000073 const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +000074 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
75 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000076 return false;
77 }
78
Mathieu Chartier808c7a52017-12-15 11:19:33 -080079 const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
80 if (compiler_options.IsHugeMethod(code_units)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000081 VLOG(compiler) << "Skip compilation of huge method "
David Sehr709b0702016-10-13 09:12:37 -070082 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
Mathieu Chartier808c7a52017-12-15 11:19:33 -080083 << ": " << code_units << " code units";
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000084 MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000085 return true;
86 }
87
88 // If it's large and contains no branches, it's likely to be machine generated initialization.
Mathieu Chartier808c7a52017-12-15 11:19:33 -080089 if (compiler_options.IsLargeMethod(code_units) && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000090 VLOG(compiler) << "Skip compilation of large method with no branch "
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::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000094 return true;
95 }
96
97 return false;
98}
99
David Brazdildee58d62016-04-07 09:54:26 +0000100GraphAnalysisResult HGraphBuilder::BuildGraph() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800101 DCHECK(code_item_accessor_.HasCodeItem());
David Brazdil60328912016-04-04 17:47:42 +0000102 DCHECK(graph_->GetBlocks().empty());
David Brazdildee58d62016-04-07 09:54:26 +0000103
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800104 graph_->SetNumberOfVRegs(code_item_accessor_.RegistersSize());
105 graph_->SetNumberOfInVRegs(code_item_accessor_.InsSize());
106 graph_->SetMaximumNumberOfOutVRegs(code_item_accessor_.OutsSize());
107 graph_->SetHasTryCatch(code_item_accessor_.TriesSize() != 0);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000108
Vladimir Marko69d310e2017-10-09 14:12:23 +0100109 // Use ScopedArenaAllocator for all local allocations.
110 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800111 HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100112 SsaBuilder ssa_builder(graph_,
113 dex_compilation_unit_->GetClassLoader(),
114 dex_compilation_unit_->GetDexCache(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100115 &local_allocator);
116 HInstructionBuilder instruction_builder(graph_,
117 &block_builder,
118 &ssa_builder,
119 dex_file_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800120 code_item_accessor_,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100121 return_type_,
122 dex_compilation_unit_,
123 outer_compilation_unit_,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100124 code_generator_,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100125 compilation_stats_,
Vladimir Marko69d310e2017-10-09 14:12:23 +0100126 &local_allocator);
127
David Brazdil86ea7ee2016-02-16 09:26:07 +0000128 // 1) Create basic blocks and link them together. Basic blocks are left
129 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100130 if (!block_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000131 return kAnalysisInvalidBytecode;
132 }
133
134 // 2) Decide whether to skip this method based on its code size and number
135 // of branches.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100136 if (SkipCompilation(block_builder.GetNumberOfBranches())) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000137 return kAnalysisSkipped;
138 }
139
140 // 3) Build the dominator tree and fill in loop and try/catch metadata.
David Brazdilbadd8262016-02-02 16:28:56 +0000141 GraphAnalysisResult result = graph_->BuildDominatorTree();
142 if (result != kAnalysisSuccess) {
143 return result;
144 }
145
David Brazdil86ea7ee2016-02-16 09:26:07 +0000146 // 4) Populate basic blocks with instructions.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100147 if (!instruction_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000148 return kAnalysisInvalidBytecode;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000149 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150
David Brazdil86ea7ee2016-02-16 09:26:07 +0000151 // 5) Type the graph and eliminate dead/redundant phis.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100152 return ssa_builder.BuildSsa();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000153}
154
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000155void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800156 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000157 DCHECK(graph_->GetBlocks().empty());
158
159 // Determine the number of arguments and associated vregs.
160 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
161 const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx));
162 size_t num_args = strlen(shorty + 1);
163 size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') +
164 std::count(shorty + 1, shorty + 1 + num_args, 'D');
165 size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u);
166
167 // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type.
168 size_t return_vregs = 2u;
169 graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs);
170 graph_->SetNumberOfInVRegs(num_arg_vregs);
171 graph_->SetMaximumNumberOfOutVRegs(num_arg_vregs);
172 graph_->SetHasTryCatch(false);
173
174 // Use ScopedArenaAllocator for all local allocations.
175 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800176 HBasicBlockBuilder block_builder(graph_,
177 dex_file_,
178 CodeItemDebugInfoAccessor(),
179 &local_allocator);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000180 SsaBuilder ssa_builder(graph_,
181 dex_compilation_unit_->GetClassLoader(),
182 dex_compilation_unit_->GetDexCache(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000183 &local_allocator);
184 HInstructionBuilder instruction_builder(graph_,
185 &block_builder,
186 &ssa_builder,
187 dex_file_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800188 CodeItemDebugInfoAccessor(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000189 return_type_,
190 dex_compilation_unit_,
191 outer_compilation_unit_,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000192 code_generator_,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000193 compilation_stats_,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000194 &local_allocator);
195
196 // 1) Create basic blocks for the intrinsic and link them together.
197 block_builder.BuildIntrinsic();
198
199 // 2) Build the trivial dominator tree.
200 GraphAnalysisResult bdt_result = graph_->BuildDominatorTree();
201 DCHECK_EQ(bdt_result, kAnalysisSuccess);
202
203 // 3) Populate basic blocks with instructions for the intrinsic.
204 instruction_builder.BuildIntrinsic(method);
205
206 // 4) Type the graph (no dead/redundant phis to eliminate).
207 GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa();
208 DCHECK_EQ(build_ssa_result, kAnalysisSuccess);
209}
210
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211} // namespace art