blob: 76350a6d55ff4689fb1981bc6e96b239d3672761 [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 Marko0ebe0d82017-09-21 22:50:39 +010023#include "data_type-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "dex/verified_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000025#include "driver/compiler_options.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010026#include "mirror/class_loader.h"
27#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000028#include "nodes.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "thread.h"
Vladimir Marko58155012015-08-19 12:49:41 +000030#include "utils/dex_cache_arrays_layout-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031
32namespace art {
33
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010034HGraphBuilder::HGraphBuilder(HGraph* graph,
Vladimir Markoca6fff82017-10-03 14:49:14 +010035 const DexCompilationUnit* dex_compilation_unit,
36 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010037 CompilerDriver* driver,
38 CodeGenerator* code_generator,
39 OptimizingCompilerStats* compiler_stats,
40 const uint8_t* interpreter_metadata,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010041 VariableSizedHandleScope* handles)
42 : graph_(graph),
43 dex_file_(&graph->GetDexFile()),
44 code_item_(*dex_compilation_unit->GetCodeItem()),
45 dex_compilation_unit_(dex_compilation_unit),
46 compiler_driver_(driver),
47 compilation_stats_(compiler_stats),
48 block_builder_(graph, dex_file_, code_item_),
49 ssa_builder_(graph,
50 dex_compilation_unit->GetClassLoader(),
51 dex_compilation_unit->GetDexCache(),
52 handles),
53 instruction_builder_(graph,
54 &block_builder_,
55 &ssa_builder_,
56 dex_file_,
57 code_item_,
58 DataType::FromShorty(dex_compilation_unit_->GetShorty()[0]),
59 dex_compilation_unit,
60 outer_compilation_unit,
61 driver,
62 code_generator,
63 interpreter_metadata,
64 compiler_stats,
Vladimir Markoca6fff82017-10-03 14:49:14 +010065 dex_compilation_unit->GetDexCache(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 handles) {}
67
David Brazdil86ea7ee2016-02-16 09:26:07 +000068bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
69 if (compiler_driver_ == nullptr) {
70 // Note that the compiler driver is null when unit testing.
71 return false;
72 }
73
Calin Juravle48c2b032014-12-09 18:11:36 +000074 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +000075 CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
76 if (compiler_filter == CompilerFilter::kEverything) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000077 return false;
78 }
79
David Brazdil86ea7ee2016-02-16 09:26:07 +000080 if (compiler_options.IsHugeMethod(code_item_.insns_size_in_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())
David Brazdil86ea7ee2016-02-16 09:26:07 +000083 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Igor Murashkin1e065a52017-08-09 13:20:34 -070084 MaybeRecordStat(compilation_stats_,
85 MethodCompilationStat::kNotCompiledHugeMethod);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000086 return true;
87 }
88
89 // If it's large and contains no branches, it's likely to be machine generated initialization.
David Brazdil86ea7ee2016-02-16 09:26:07 +000090 if (compiler_options.IsLargeMethod(code_item_.insns_size_in_code_units_)
David Brazdil1b498722015-03-31 11:37:18 +010091 && (number_of_branches == 0)) {
Calin Juravle48c2b032014-12-09 18:11:36 +000092 VLOG(compiler) << "Skip compilation of large method with no branch "
David Sehr709b0702016-10-13 09:12:37 -070093 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdil86ea7ee2016-02-16 09:26:07 +000094 << ": " << code_item_.insns_size_in_code_units_ << " code units";
Igor Murashkin1e065a52017-08-09 13:20:34 -070095 MaybeRecordStat(compilation_stats_,
96 MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000097 return true;
98 }
99
100 return false;
101}
102
David Brazdildee58d62016-04-07 09:54:26 +0000103GraphAnalysisResult HGraphBuilder::BuildGraph() {
David Brazdil60328912016-04-04 17:47:42 +0000104 DCHECK(graph_->GetBlocks().empty());
David Brazdildee58d62016-04-07 09:54:26 +0000105
106 graph_->SetNumberOfVRegs(code_item_.registers_size_);
107 graph_->SetNumberOfInVRegs(code_item_.ins_size_);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000108 graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_);
109 graph_->SetHasTryCatch(code_item_.tries_size_ != 0);
David Brazdil86ea7ee2016-02-16 09:26:07 +0000110
111 // 1) Create basic blocks and link them together. Basic blocks are left
112 // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
113 if (!block_builder_.Build()) {
114 return kAnalysisInvalidBytecode;
115 }
116
117 // 2) Decide whether to skip this method based on its code size and number
118 // of branches.
119 if (SkipCompilation(block_builder_.GetNumberOfBranches())) {
120 return kAnalysisSkipped;
121 }
122
123 // 3) Build the dominator tree and fill in loop and try/catch metadata.
David Brazdilbadd8262016-02-02 16:28:56 +0000124 GraphAnalysisResult result = graph_->BuildDominatorTree();
125 if (result != kAnalysisSuccess) {
126 return result;
127 }
128
David Brazdil86ea7ee2016-02-16 09:26:07 +0000129 // 4) Populate basic blocks with instructions.
David Brazdildee58d62016-04-07 09:54:26 +0000130 if (!instruction_builder_.Build()) {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000131 return kAnalysisInvalidBytecode;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000132 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133
David Brazdil86ea7ee2016-02-16 09:26:07 +0000134 // 5) Type the graph and eliminate dead/redundant phis.
David Brazdildee58d62016-04-07 09:54:26 +0000135 return ssa_builder_.BuildSsa();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136}
137
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138} // namespace art