• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
37 namespace art {
38 
HGraphBuilder(HGraph * graph,const CodeItemDebugInfoAccessor & accessor,const DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * outer_compilation_unit,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats)39 HGraphBuilder::HGraphBuilder(HGraph* graph,
40                              const CodeItemDebugInfoAccessor& accessor,
41                              const DexCompilationUnit* dex_compilation_unit,
42                              const DexCompilationUnit* outer_compilation_unit,
43                              CodeGenerator* code_generator,
44                              OptimizingCompilerStats* compiler_stats)
45     : graph_(graph),
46       dex_file_(&graph->GetDexFile()),
47       code_item_accessor_(accessor),
48       dex_compilation_unit_(dex_compilation_unit),
49       outer_compilation_unit_(outer_compilation_unit),
50       code_generator_(code_generator),
51       compilation_stats_(compiler_stats),
52       return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
53 
HGraphBuilder(HGraph * graph,const DexCompilationUnit * dex_compilation_unit,const CodeItemDebugInfoAccessor & accessor,DataType::Type return_type)54 HGraphBuilder::HGraphBuilder(HGraph* graph,
55                              const DexCompilationUnit* dex_compilation_unit,
56                              const CodeItemDebugInfoAccessor& accessor,
57                              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),
63       code_generator_(nullptr),
64       compilation_stats_(nullptr),
65       return_type_(return_type) {}
66 
SkipCompilation(size_t number_of_branches)67 bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
68   if (code_generator_ == nullptr) {
69     // Note that the codegen is null when unit testing.
70     return false;
71   }
72 
73   const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
74   CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
75   if (compiler_filter == CompilerFilter::kEverything) {
76     return false;
77   }
78 
79   const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
80   if (compiler_options.IsHugeMethod(code_units)) {
81     VLOG(compiler) << "Skip compilation of huge method "
82                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
83                    << ": " << code_units << " code units";
84     MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
85     return true;
86   }
87 
88   // If it's large and contains no branches, it's likely to be machine generated initialization.
89   if (compiler_options.IsLargeMethod(code_units) && (number_of_branches == 0)) {
90     VLOG(compiler) << "Skip compilation of large method with no branch "
91                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
92                    << ": " << code_units << " code units";
93     MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
94     return true;
95   }
96 
97   return false;
98 }
99 
BuildGraph()100 GraphAnalysisResult HGraphBuilder::BuildGraph() {
101   DCHECK(code_item_accessor_.HasCodeItem());
102   DCHECK(graph_->GetBlocks().empty());
103 
104   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);
108 
109   // Use ScopedArenaAllocator for all local allocations.
110   ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
111   HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
112   SsaBuilder ssa_builder(graph_,
113                          dex_compilation_unit_->GetClassLoader(),
114                          dex_compilation_unit_->GetDexCache(),
115                          &local_allocator);
116   HInstructionBuilder instruction_builder(graph_,
117                                           &block_builder,
118                                           &ssa_builder,
119                                           dex_file_,
120                                           code_item_accessor_,
121                                           return_type_,
122                                           dex_compilation_unit_,
123                                           outer_compilation_unit_,
124                                           code_generator_,
125                                           compilation_stats_,
126                                           &local_allocator);
127 
128   // 1) Create basic blocks and link them together. Basic blocks are left
129   //    unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
130   if (!block_builder.Build()) {
131     return kAnalysisInvalidBytecode;
132   }
133 
134   // 2) Decide whether to skip this method based on its code size and number
135   //    of branches.
136   if (SkipCompilation(block_builder.GetNumberOfBranches())) {
137     return kAnalysisSkipped;
138   }
139 
140   // 3) Build the dominator tree and fill in loop and try/catch metadata.
141   GraphAnalysisResult result = graph_->BuildDominatorTree();
142   if (result != kAnalysisSuccess) {
143     return result;
144   }
145 
146   // 4) Populate basic blocks with instructions.
147   if (!instruction_builder.Build()) {
148     return kAnalysisInvalidBytecode;
149   }
150 
151   // 5) Type the graph and eliminate dead/redundant phis.
152   return ssa_builder.BuildSsa();
153 }
154 
BuildIntrinsicGraph(ArtMethod * method)155 void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
156   DCHECK(!code_item_accessor_.HasCodeItem());
157   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());
176   HBasicBlockBuilder block_builder(graph_,
177                                    dex_file_,
178                                    CodeItemDebugInfoAccessor(),
179                                    &local_allocator);
180   SsaBuilder ssa_builder(graph_,
181                          dex_compilation_unit_->GetClassLoader(),
182                          dex_compilation_unit_->GetDexCache(),
183                          &local_allocator);
184   HInstructionBuilder instruction_builder(graph_,
185                                           &block_builder,
186                                           &ssa_builder,
187                                           dex_file_,
188                                           CodeItemDebugInfoAccessor(),
189                                           return_type_,
190                                           dex_compilation_unit_,
191                                           outer_compilation_unit_,
192                                           code_generator_,
193                                           compilation_stats_,
194                                           &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 
211 }  // namespace art
212