blob: 4ed1612220514829445fd4b4872c4ccb89f4fb50 [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 Marko69d310e2017-10-09 14:12:23 +010027#include "instruction_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "optimizing_compiler_stats.h"
32#include "ssa_builder.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010033#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36namespace art {
37
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010038HGraphBuilder::HGraphBuilder(HGraph* graph,
Vladimir Markoca6fff82017-10-03 14:49:14 +010039 const DexCompilationUnit* dex_compilation_unit,
40 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010041 CompilerDriver* driver,
42 CodeGenerator* code_generator,
43 OptimizingCompilerStats* compiler_stats,
44 const uint8_t* interpreter_metadata,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010045 VariableSizedHandleScope* handles)
46 : graph_(graph),
47 dex_file_(&graph->GetDexFile()),
48 code_item_(*dex_compilation_unit->GetCodeItem()),
49 dex_compilation_unit_(dex_compilation_unit),
Vladimir Marko69d310e2017-10-09 14:12:23 +010050 outer_compilation_unit_(outer_compilation_unit),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051 compiler_driver_(driver),
Vladimir Marko69d310e2017-10-09 14:12:23 +010052 code_generator_(code_generator),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 compilation_stats_(compiler_stats),
Vladimir Marko69d310e2017-10-09 14:12:23 +010054 interpreter_metadata_(interpreter_metadata),
55 handles_(handles),
56 return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057
David Brazdil86ea7ee2016-02-16 09:26:07 +000058bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
59 if (compiler_driver_ == nullptr) {
60 // Note that the compiler driver is null when unit testing.
61 return false;
62 }
63
Calin Juravle48c2b032014-12-09 18:11:36 +000064 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +000065 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
66 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000067 return false;
68 }
69
David Brazdil86ea7ee2016-02-16 09:26:07 +000070 if (compiler_options.IsHugeMethod(code_item_.insns_size_in_code_units_)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000071 VLOG(compiler) << "Skip compilation of huge method "
David Sehr709b0702016-10-13 09:12:37 -070072 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdil86ea7ee2016-02-16 09:26:07 +000073 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Igor Murashkin1e065a52017-08-09 13:20:34 -070074 MaybeRecordStat(compilation_stats_,
75 MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000076 return true;
77 }
78
79 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil86ea7ee2016-02-16 09:26:07 +000080 if (compiler_options.IsLargeMethod(code_item_.insns_size_in_code_units_)
David Brazdil1b498722015-03-31 11:37:18 +010081 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000082 VLOG(compiler) << "Skip compilation of large method with no branch "
David Sehr709b0702016-10-13 09:12:37 -070083 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdil86ea7ee2016-02-16 09:26:07 +000084 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Igor Murashkin1e065a52017-08-09 13:20:34 -070085 MaybeRecordStat(compilation_stats_,
86 MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000087 return true;
88 }
89
90 return false;
91}
92
David Brazdildee58d62016-04-07 09:54:26 +000093GraphAnalysisResult HGraphBuilder::BuildGraph() {
David Brazdil60328912016-04-04 17:47:42 +000094 DCHECK(graph_->GetBlocks().empty());
David Brazdildee58d62016-04-07 09:54:26 +000095
96 graph_->SetNumberOfVRegs(code_item_.registers_size_);
97 graph_->SetNumberOfInVRegs(code_item_.ins_size_);
David Brazdil86ea7ee2016-02-16 09:26:07 +000098 graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_);
99 graph_->SetHasTryCatch(code_item_.tries_size_ != 0);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000100
Vladimir Marko69d310e2017-10-09 14:12:23 +0100101 // Use ScopedArenaAllocator for all local allocations.
102 ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
103 HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_, &local_allocator);
104 SsaBuilder ssa_builder(graph_,
105 dex_compilation_unit_->GetClassLoader(),
106 dex_compilation_unit_->GetDexCache(),
107 handles_,
108 &local_allocator);
109 HInstructionBuilder instruction_builder(graph_,
110 &block_builder,
111 &ssa_builder,
112 dex_file_,
113 code_item_,
114 return_type_,
115 dex_compilation_unit_,
116 outer_compilation_unit_,
117 compiler_driver_,
118 code_generator_,
119 interpreter_metadata_,
120 compilation_stats_,
121 handles_,
122 &local_allocator);
123
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 // 1) Create basic blocks and link them together. Basic blocks are left
125 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100126 if (!block_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 return kAnalysisInvalidBytecode;
128 }
129
130 // 2) Decide whether to skip this method based on its code size and number
131 // of branches.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100132 if (SkipCompilation(block_builder.GetNumberOfBranches())) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000133 return kAnalysisSkipped;
134 }
135
136 // 3) Build the dominator tree and fill in loop and try/catch metadata.
David Brazdilbadd8262016-02-02 16:28:56 +0000137 GraphAnalysisResult result = graph_->BuildDominatorTree();
138 if (result != kAnalysisSuccess) {
139 return result;
140 }
141
David Brazdil86ea7ee2016-02-16 09:26:07 +0000142 // 4) Populate basic blocks with instructions.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100143 if (!instruction_builder.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000144 return kAnalysisInvalidBytecode;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000145 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146
David Brazdil86ea7ee2016-02-16 09:26:07 +0000147 // 5) Type the graph and eliminate dead/redundant phis.
Vladimir Marko69d310e2017-10-09 14:12:23 +0100148 return ssa_builder.BuildSsa();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000149}
150
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000151} // namespace art