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 "dex/verified_method.h"
24 #include "driver/compiler_options.h"
25 #include "mirror/class_loader.h"
26 #include "mirror/dex_cache.h"
27 #include "nodes.h"
28 #include "primitive.h"
29 #include "thread.h"
30 #include "utils/dex_cache_arrays_layout-inl.h"
31 
32 namespace art {
33 
MaybeRecordStat(MethodCompilationStat compilation_stat)34 void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
35   if (compilation_stats_ != nullptr) {
36     compilation_stats_->RecordStat(compilation_stat);
37   }
38 }
39 
SkipCompilation(size_t number_of_branches)40 bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
41   if (compiler_driver_ == nullptr) {
42     // Note that the compiler driver is null when unit testing.
43     return false;
44   }
45 
46   const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
47   CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
48   if (compiler_filter == CompilerFilter::kEverything) {
49     return false;
50   }
51 
52   if (compiler_options.IsHugeMethod(code_item_.insns_size_in_code_units_)) {
53     VLOG(compiler) << "Skip compilation of huge method "
54                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
55                    << ": " << code_item_.insns_size_in_code_units_ << " code units";
56     MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod);
57     return true;
58   }
59 
60   // If it's large and contains no branches, it's likely to be machine generated initialization.
61   if (compiler_options.IsLargeMethod(code_item_.insns_size_in_code_units_)
62       && (number_of_branches == 0)) {
63     VLOG(compiler) << "Skip compilation of large method with no branch "
64                    << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
65                    << ": " << code_item_.insns_size_in_code_units_ << " code units";
66     MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches);
67     return true;
68   }
69 
70   return false;
71 }
72 
BuildGraph()73 GraphAnalysisResult HGraphBuilder::BuildGraph() {
74   DCHECK(graph_->GetBlocks().empty());
75 
76   graph_->SetNumberOfVRegs(code_item_.registers_size_);
77   graph_->SetNumberOfInVRegs(code_item_.ins_size_);
78   graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_);
79   graph_->SetHasTryCatch(code_item_.tries_size_ != 0);
80 
81   // 1) Create basic blocks and link them together. Basic blocks are left
82   //    unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries.
83   if (!block_builder_.Build()) {
84     return kAnalysisInvalidBytecode;
85   }
86 
87   // 2) Decide whether to skip this method based on its code size and number
88   //    of branches.
89   if (SkipCompilation(block_builder_.GetNumberOfBranches())) {
90     return kAnalysisSkipped;
91   }
92 
93   // 3) Build the dominator tree and fill in loop and try/catch metadata.
94   GraphAnalysisResult result = graph_->BuildDominatorTree();
95   if (result != kAnalysisSuccess) {
96     return result;
97   }
98 
99   // 4) Populate basic blocks with instructions.
100   if (!instruction_builder_.Build()) {
101     return kAnalysisInvalidBytecode;
102   }
103 
104   // 5) Type the graph and eliminate dead/redundant phis.
105   return ssa_builder_.BuildSsa();
106 }
107 
108 }  // namespace art
109