xref: /aosp_15_r20/art/compiler/optimizing/builder.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "builder.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/arena_bit_vector.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/bit_vector-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
23*795d594fSAndroid Build Coastguard Worker #include "block_builder.h"
24*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
25*795d594fSAndroid Build Coastguard Worker #include "data_type-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
27*795d594fSAndroid Build Coastguard Worker #include "driver/dex_compilation_unit.h"
28*795d594fSAndroid Build Coastguard Worker #include "instruction_builder.h"
29*795d594fSAndroid Build Coastguard Worker #include "mirror/class_loader.h"
30*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache.h"
31*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
32*795d594fSAndroid Build Coastguard Worker #include "optimizing_compiler_stats.h"
33*795d594fSAndroid Build Coastguard Worker #include "ssa_builder.h"
34*795d594fSAndroid Build Coastguard Worker #include "thread.h"
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker 
HGraphBuilder(HGraph * graph,const CodeItemDebugInfoAccessor & accessor,const DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * outer_compilation_unit,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats)38*795d594fSAndroid Build Coastguard Worker HGraphBuilder::HGraphBuilder(HGraph* graph,
39*795d594fSAndroid Build Coastguard Worker                              const CodeItemDebugInfoAccessor& accessor,
40*795d594fSAndroid Build Coastguard Worker                              const DexCompilationUnit* dex_compilation_unit,
41*795d594fSAndroid Build Coastguard Worker                              const DexCompilationUnit* outer_compilation_unit,
42*795d594fSAndroid Build Coastguard Worker                              CodeGenerator* code_generator,
43*795d594fSAndroid Build Coastguard Worker                              OptimizingCompilerStats* compiler_stats)
44*795d594fSAndroid Build Coastguard Worker     : graph_(graph),
45*795d594fSAndroid Build Coastguard Worker       dex_file_(&graph->GetDexFile()),
46*795d594fSAndroid Build Coastguard Worker       code_item_accessor_(accessor),
47*795d594fSAndroid Build Coastguard Worker       dex_compilation_unit_(dex_compilation_unit),
48*795d594fSAndroid Build Coastguard Worker       outer_compilation_unit_(outer_compilation_unit),
49*795d594fSAndroid Build Coastguard Worker       code_generator_(code_generator),
50*795d594fSAndroid Build Coastguard Worker       compilation_stats_(compiler_stats),
51*795d594fSAndroid Build Coastguard Worker       return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {}
52*795d594fSAndroid Build Coastguard Worker 
HGraphBuilder(HGraph * graph,const DexCompilationUnit * dex_compilation_unit,const CodeItemDebugInfoAccessor & accessor,DataType::Type return_type)53*795d594fSAndroid Build Coastguard Worker HGraphBuilder::HGraphBuilder(HGraph* graph,
54*795d594fSAndroid Build Coastguard Worker                              const DexCompilationUnit* dex_compilation_unit,
55*795d594fSAndroid Build Coastguard Worker                              const CodeItemDebugInfoAccessor& accessor,
56*795d594fSAndroid Build Coastguard Worker                              DataType::Type return_type)
57*795d594fSAndroid Build Coastguard Worker     : graph_(graph),
58*795d594fSAndroid Build Coastguard Worker       dex_file_(&graph->GetDexFile()),
59*795d594fSAndroid Build Coastguard Worker       code_item_accessor_(accessor),
60*795d594fSAndroid Build Coastguard Worker       dex_compilation_unit_(dex_compilation_unit),
61*795d594fSAndroid Build Coastguard Worker       outer_compilation_unit_(nullptr),
62*795d594fSAndroid Build Coastguard Worker       code_generator_(nullptr),
63*795d594fSAndroid Build Coastguard Worker       compilation_stats_(nullptr),
64*795d594fSAndroid Build Coastguard Worker       return_type_(return_type) {}
65*795d594fSAndroid Build Coastguard Worker 
SkipCompilation()66*795d594fSAndroid Build Coastguard Worker bool HGraphBuilder::SkipCompilation() {
67*795d594fSAndroid Build Coastguard Worker   if (code_generator_ == nullptr) {
68*795d594fSAndroid Build Coastguard Worker     // Note that the codegen is null when unit testing.
69*795d594fSAndroid Build Coastguard Worker     return false;
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
73*795d594fSAndroid Build Coastguard Worker   CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
74*795d594fSAndroid Build Coastguard Worker   if (compiler_filter == CompilerFilter::kEverything) {
75*795d594fSAndroid Build Coastguard Worker     return false;
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker   const uint32_t code_units = code_item_accessor_.InsnsSizeInCodeUnits();
79*795d594fSAndroid Build Coastguard Worker   if (compiler_options.IsHugeMethod(code_units)) {
80*795d594fSAndroid Build Coastguard Worker     VLOG(compiler) << "Skip compilation of huge method "
81*795d594fSAndroid Build Coastguard Worker                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
82*795d594fSAndroid Build Coastguard Worker                    << ": " << code_units << " code units";
83*795d594fSAndroid Build Coastguard Worker     MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod);
84*795d594fSAndroid Build Coastguard Worker     return true;
85*795d594fSAndroid Build Coastguard Worker   }
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker   return false;
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker 
BuildGraph()90*795d594fSAndroid Build Coastguard Worker GraphAnalysisResult HGraphBuilder::BuildGraph() {
91*795d594fSAndroid Build Coastguard Worker   DCHECK(code_item_accessor_.HasCodeItem());
92*795d594fSAndroid Build Coastguard Worker   DCHECK(graph_->GetBlocks().empty());
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   graph_->SetNumberOfVRegs(code_item_accessor_.RegistersSize());
95*795d594fSAndroid Build Coastguard Worker   graph_->SetNumberOfInVRegs(code_item_accessor_.InsSize());
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   // Use ScopedArenaAllocator for all local allocations.
98*795d594fSAndroid Build Coastguard Worker   ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
99*795d594fSAndroid Build Coastguard Worker   HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_accessor_, &local_allocator);
100*795d594fSAndroid Build Coastguard Worker   SsaBuilder ssa_builder(graph_,
101*795d594fSAndroid Build Coastguard Worker                          dex_compilation_unit_->GetClassLoader(),
102*795d594fSAndroid Build Coastguard Worker                          dex_compilation_unit_->GetDexCache(),
103*795d594fSAndroid Build Coastguard Worker                          &local_allocator);
104*795d594fSAndroid Build Coastguard Worker   HInstructionBuilder instruction_builder(graph_,
105*795d594fSAndroid Build Coastguard Worker                                           &block_builder,
106*795d594fSAndroid Build Coastguard Worker                                           &ssa_builder,
107*795d594fSAndroid Build Coastguard Worker                                           dex_file_,
108*795d594fSAndroid Build Coastguard Worker                                           code_item_accessor_,
109*795d594fSAndroid Build Coastguard Worker                                           return_type_,
110*795d594fSAndroid Build Coastguard Worker                                           dex_compilation_unit_,
111*795d594fSAndroid Build Coastguard Worker                                           outer_compilation_unit_,
112*795d594fSAndroid Build Coastguard Worker                                           code_generator_,
113*795d594fSAndroid Build Coastguard Worker                                           compilation_stats_,
114*795d594fSAndroid Build Coastguard Worker                                           &local_allocator);
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker   // 1) Create basic blocks and link them together. Basic blocks are left
117*795d594fSAndroid Build Coastguard Worker   //    unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
118*795d594fSAndroid Build Coastguard Worker   if (!block_builder.Build()) {
119*795d594fSAndroid Build Coastguard Worker     return kAnalysisInvalidBytecode;
120*795d594fSAndroid Build Coastguard Worker   }
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker   // 2) Decide whether to skip compiling this method based on e.g. the compiler filter and method's
123*795d594fSAndroid Build Coastguard Worker   // code size.
124*795d594fSAndroid Build Coastguard Worker   if (SkipCompilation()) {
125*795d594fSAndroid Build Coastguard Worker     return kAnalysisSkipped;
126*795d594fSAndroid Build Coastguard Worker   }
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker   // 3) Build the dominator tree and fill in loop and try/catch metadata.
129*795d594fSAndroid Build Coastguard Worker   GraphAnalysisResult result = graph_->BuildDominatorTree();
130*795d594fSAndroid Build Coastguard Worker   if (result != kAnalysisSuccess) {
131*795d594fSAndroid Build Coastguard Worker     return result;
132*795d594fSAndroid Build Coastguard Worker   }
133*795d594fSAndroid Build Coastguard Worker 
134*795d594fSAndroid Build Coastguard Worker   // 4) Populate basic blocks with instructions.
135*795d594fSAndroid Build Coastguard Worker   if (!instruction_builder.Build()) {
136*795d594fSAndroid Build Coastguard Worker     return kAnalysisInvalidBytecode;
137*795d594fSAndroid Build Coastguard Worker   }
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker   // 5) Type the graph and eliminate dead/redundant phis.
140*795d594fSAndroid Build Coastguard Worker   return ssa_builder.BuildSsa();
141*795d594fSAndroid Build Coastguard Worker }
142*795d594fSAndroid Build Coastguard Worker 
BuildIntrinsicGraph(ArtMethod * method)143*795d594fSAndroid Build Coastguard Worker void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) {
144*795d594fSAndroid Build Coastguard Worker   DCHECK(!code_item_accessor_.HasCodeItem());
145*795d594fSAndroid Build Coastguard Worker   DCHECK(graph_->GetBlocks().empty());
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   // Determine the number of arguments and associated vregs.
148*795d594fSAndroid Build Coastguard Worker   uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
149*795d594fSAndroid Build Coastguard Worker   const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx));
150*795d594fSAndroid Build Coastguard Worker   size_t num_args = strlen(shorty + 1);
151*795d594fSAndroid Build Coastguard Worker   size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') +
152*795d594fSAndroid Build Coastguard Worker                          std::count(shorty + 1, shorty + 1 + num_args, 'D');
153*795d594fSAndroid Build Coastguard Worker   size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u);
154*795d594fSAndroid Build Coastguard Worker 
155*795d594fSAndroid Build Coastguard Worker   // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type.
156*795d594fSAndroid Build Coastguard Worker   size_t return_vregs = 2u;
157*795d594fSAndroid Build Coastguard Worker   graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs);
158*795d594fSAndroid Build Coastguard Worker   graph_->SetNumberOfInVRegs(num_arg_vregs);
159*795d594fSAndroid Build Coastguard Worker 
160*795d594fSAndroid Build Coastguard Worker   // Use ScopedArenaAllocator for all local allocations.
161*795d594fSAndroid Build Coastguard Worker   ScopedArenaAllocator local_allocator(graph_->GetArenaStack());
162*795d594fSAndroid Build Coastguard Worker   HBasicBlockBuilder block_builder(graph_,
163*795d594fSAndroid Build Coastguard Worker                                    dex_file_,
164*795d594fSAndroid Build Coastguard Worker                                    CodeItemDebugInfoAccessor(),
165*795d594fSAndroid Build Coastguard Worker                                    &local_allocator);
166*795d594fSAndroid Build Coastguard Worker   SsaBuilder ssa_builder(graph_,
167*795d594fSAndroid Build Coastguard Worker                          dex_compilation_unit_->GetClassLoader(),
168*795d594fSAndroid Build Coastguard Worker                          dex_compilation_unit_->GetDexCache(),
169*795d594fSAndroid Build Coastguard Worker                          &local_allocator);
170*795d594fSAndroid Build Coastguard Worker   HInstructionBuilder instruction_builder(graph_,
171*795d594fSAndroid Build Coastguard Worker                                           &block_builder,
172*795d594fSAndroid Build Coastguard Worker                                           &ssa_builder,
173*795d594fSAndroid Build Coastguard Worker                                           dex_file_,
174*795d594fSAndroid Build Coastguard Worker                                           CodeItemDebugInfoAccessor(),
175*795d594fSAndroid Build Coastguard Worker                                           return_type_,
176*795d594fSAndroid Build Coastguard Worker                                           dex_compilation_unit_,
177*795d594fSAndroid Build Coastguard Worker                                           outer_compilation_unit_,
178*795d594fSAndroid Build Coastguard Worker                                           code_generator_,
179*795d594fSAndroid Build Coastguard Worker                                           compilation_stats_,
180*795d594fSAndroid Build Coastguard Worker                                           &local_allocator);
181*795d594fSAndroid Build Coastguard Worker 
182*795d594fSAndroid Build Coastguard Worker   // 1) Create basic blocks for the intrinsic and link them together.
183*795d594fSAndroid Build Coastguard Worker   block_builder.BuildIntrinsic();
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   // 2) Build the trivial dominator tree.
186*795d594fSAndroid Build Coastguard Worker   GraphAnalysisResult bdt_result = graph_->BuildDominatorTree();
187*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(bdt_result, kAnalysisSuccess);
188*795d594fSAndroid Build Coastguard Worker 
189*795d594fSAndroid Build Coastguard Worker   // 3) Populate basic blocks with instructions for the intrinsic.
190*795d594fSAndroid Build Coastguard Worker   instruction_builder.BuildIntrinsic(method);
191*795d594fSAndroid Build Coastguard Worker 
192*795d594fSAndroid Build Coastguard Worker   // 4) Type the graph (no dead/redundant phis to eliminate).
193*795d594fSAndroid Build Coastguard Worker   GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa();
194*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(build_ssa_result, kAnalysisSuccess);
195*795d594fSAndroid Build Coastguard Worker }
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker }  // namespace art
198