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 "driver/compiler_options.h"
27 #include "driver/dex_compilation_unit.h"
28 #include "instruction_builder.h"
29 #include "mirror/class_loader.h"
30 #include "mirror/dex_cache.h"
31 #include "nodes.h"
32 #include "optimizing_compiler_stats.h"
33 #include "ssa_builder.h"
34 #include "thread.h"
35 
36 namespace art HIDDEN {
37 
HGraphBuilder(HGraph * graph,const CodeItemDebugInfoAccessor & accessor,const DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * outer_compilation_unit,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats)38 HGraphBuilder::HGraphBuilder(HGraph* graph,
39                              const CodeItemDebugInfoAccessor& accessor,
40                              const DexCompilationUnit* dex_compilation_unit,
41                              const DexCompilationUnit* outer_compilation_unit,
42                              CodeGenerator* code_generator,
43                              OptimizingCompilerStats* compiler_stats)
44     : graph_(graph),
45       dex_file_(&graph->GetDexFile()),
46       code_item_accessor_(accessor),
47       dex_compilation_unit_(dex_compilation_unit),
48       outer_compilation_unit_(outer_compilation_unit),
49       code_generator_(code_generator),
50       compilation_stats_(compiler_stats),
51       return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
52 
HGraphBuilder(HGraph * graph,const DexCompilationUnit * dex_compilation_unit,const CodeItemDebugInfoAccessor & accessor,DataType::Type return_type)53 HGraphBuilder::HGraphBuilder(HGraph* graph,
54                              const DexCompilationUnit* dex_compilation_unit,
55                              const CodeItemDebugInfoAccessor& accessor,
56                              DataType::Type return_type)
57     : graph_(graph),
58       dex_file_(&graph->GetDexFile()),
59       code_item_accessor_(accessor),
60       dex_compilation_unit_(dex_compilation_unit),
61       outer_compilation_unit_(nullptr),
62       code_generator_(nullptr),
63       compilation_stats_(nullptr),
64       return_type_(return_type) {}
65 
SkipCompilation()66 bool HGraphBuilder::SkipCompilation() {
67   if (code_generator_ == nullptr) {
68     // Note that the codegen is null when unit testing.
69     return false;
70   }
71 
72   const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
73   CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
74   if (compiler_filter == CompilerFilter::kEverything) {
75     return false;
76   }
77 
78   const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
79   if (compiler_options.IsHugeMethod(code_units)) {
80     VLOG(compiler) << "Skip compilation of huge method "
81                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
82                    << ": " << code_units << " code units";
83     MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
84     return true;
85   }
86 
87   return false;
88 }
89 
BuildGraph()90 GraphAnalysisResult HGraphBuilder::BuildGraph() {
91   DCHECK(code_item_accessor_.HasCodeItem());
92   DCHECK(graph_->GetBlocks().empty());
93 
94   graph_->SetNumberOfVRegs(code_item_accessor_.RegistersSize());
95   graph_->SetNumberOfInVRegs(code_item_accessor_.InsSize());
96   graph_->SetMaximumNumberOfOutVRegs(code_item_accessor_.OutsSize());
97 
98   // Use ScopedArenaAllocator for all local allocations.
99   ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
100   HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
101   SsaBuilder ssa_builder(graph_,
102                          dex_compilation_unit_->GetClassLoader(),
103                          dex_compilation_unit_->GetDexCache(),
104                          &local_allocator);
105   HInstructionBuilder instruction_builder(graph_,
106                                           &block_builder,
107                                           &ssa_builder,
108                                           dex_file_,
109                                           code_item_accessor_,
110                                           return_type_,
111                                           dex_compilation_unit_,
112                                           outer_compilation_unit_,
113                                           code_generator_,
114                                           compilation_stats_,
115                                           &local_allocator);
116 
117   // 1) Create basic blocks and link them together. Basic blocks are left
118   //    unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
119   if (!block_builder.Build()) {
120     return kAnalysisInvalidBytecode;
121   }
122 
123   // 2) Decide whether to skip compiling this method based on e.g. the compiler filter and method's
124   // code size.
125   if (SkipCompilation()) {
126     return kAnalysisSkipped;
127   }
128 
129   // 3) Build the dominator tree and fill in loop and try/catch metadata.
130   GraphAnalysisResult result = graph_->BuildDominatorTree();
131   if (result != kAnalysisSuccess) {
132     return result;
133   }
134 
135   // 4) Populate basic blocks with instructions.
136   if (!instruction_builder.Build()) {
137     return kAnalysisInvalidBytecode;
138   }
139 
140   // 5) Type the graph and eliminate dead/redundant phis.
141   return ssa_builder.BuildSsa();
142 }
143 
BuildIntrinsicGraph(ArtMethod * method)144 void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
145   DCHECK(!code_item_accessor_.HasCodeItem());
146   DCHECK(graph_->GetBlocks().empty());
147 
148   // Determine the number of arguments and associated vregs.
149   uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
150   const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx));
151   size_t num_args = strlen(shorty + 1);
152   size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') +
153                          std::count(shorty + 1, shorty + 1 + num_args, 'D');
154   size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u);
155 
156   // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type.
157   size_t return_vregs = 2u;
158   graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs);
159   graph_->SetNumberOfInVRegs(num_arg_vregs);
160   graph_->SetMaximumNumberOfOutVRegs(num_arg_vregs);
161 
162   // Use ScopedArenaAllocator for all local allocations.
163   ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
164   HBasicBlockBuilder block_builder(graph_,
165                                    dex_file_,
166                                    CodeItemDebugInfoAccessor(),
167                                    &local_allocator);
168   SsaBuilder ssa_builder(graph_,
169                          dex_compilation_unit_->GetClassLoader(),
170                          dex_compilation_unit_->GetDexCache(),
171                          &local_allocator);
172   HInstructionBuilder instruction_builder(graph_,
173                                           &block_builder,
174                                           &ssa_builder,
175                                           dex_file_,
176                                           CodeItemDebugInfoAccessor(),
177                                           return_type_,
178                                           dex_compilation_unit_,
179                                           outer_compilation_unit_,
180                                           code_generator_,
181                                           compilation_stats_,
182                                           &local_allocator);
183 
184   // 1) Create basic blocks for the intrinsic and link them together.
185   block_builder.BuildIntrinsic();
186 
187   // 2) Build the trivial dominator tree.
188   GraphAnalysisResult bdt_result = graph_->BuildDominatorTree();
189   DCHECK_EQ(bdt_result, kAnalysisSuccess);
190 
191   // 3) Populate basic blocks with instructions for the intrinsic.
192   instruction_builder.BuildIntrinsic(method);
193 
194   // 4) Type the graph (no dead/redundant phis to eliminate).
195   GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa();
196   DCHECK_EQ(build_ssa_result, kAnalysisSuccess);
197 }
198 
199 }  // namespace art
200