Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 17 | #include "builder.h" |
| 18 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 20 | #include "base/arena_bit_vector.h" |
| 21 | #include "base/bit_vector-inl.h" |
Andreas Gampe | d881df5 | 2014-11-24 23:28:39 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 23 | #include "block_builder.h" |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 24 | #include "data_type-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 25 | #include "dex/verified_method.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 26 | #include "driver/compiler_options.h" |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 27 | #include "driver/dex_compilation_unit.h" |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 28 | #include "instruction_builder.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
| 30 | #include "mirror/dex_cache.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 31 | #include "nodes.h" |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 32 | #include "optimizing_compiler_stats.h" |
| 33 | #include "ssa_builder.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 34 | #include "thread.h" |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 35 | #include "utils/dex_cache_arrays_layout-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 39 | HGraphBuilder::HGraphBuilder(HGraph* graph, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 40 | const DexFile::CodeItem* code_item, |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 41 | const DexCompilationUnit* dex_compilation_unit, |
| 42 | const DexCompilationUnit* outer_compilation_unit, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 43 | CompilerDriver* driver, |
| 44 | CodeGenerator* code_generator, |
| 45 | OptimizingCompilerStats* compiler_stats, |
| 46 | const uint8_t* interpreter_metadata, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 47 | VariableSizedHandleScope* handles) |
| 48 | : graph_(graph), |
| 49 | dex_file_(&graph->GetDexFile()), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 50 | code_item_(code_item), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 51 | dex_compilation_unit_(dex_compilation_unit), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 52 | outer_compilation_unit_(outer_compilation_unit), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 53 | compiler_driver_(driver), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 54 | code_generator_(code_generator), |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 55 | compilation_stats_(compiler_stats), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 56 | interpreter_metadata_(interpreter_metadata), |
| 57 | handles_(handles), |
| 58 | return_type_(DataType::FromShorty(dex_compilation_unit_->GetShorty()[0])) {} |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 59 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 60 | bool HGraphBuilder::SkipCompilation(size_t number_of_branches) { |
| 61 | if (compiler_driver_ == nullptr) { |
| 62 | // Note that the compiler driver is null when unit testing. |
| 63 | return false; |
| 64 | } |
| 65 | |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 66 | const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 67 | CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter(); |
| 68 | if (compiler_filter == CompilerFilter::kEverything) { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 72 | if (compiler_options.IsHugeMethod(code_item_->insns_size_in_code_units_)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 73 | VLOG(compiler) << "Skip compilation of huge method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 74 | << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex()) |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 75 | << ": " << code_item_->insns_size_in_code_units_ << " code units"; |
| 76 | MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledHugeMethod); |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // If it's large and contains no branches, it's likely to be machine generated initialization. |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 81 | if (compiler_options.IsLargeMethod(code_item_->insns_size_in_code_units_) |
David Brazdil | 1b49872 | 2015-03-31 11:37:18 +0100 | [diff] [blame] | 82 | && (number_of_branches == 0)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 83 | VLOG(compiler) << "Skip compilation of large method with no branch " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 84 | << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex()) |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 85 | << ": " << code_item_->insns_size_in_code_units_ << " code units"; |
| 86 | MaybeRecordStat(compilation_stats_, MethodCompilationStat::kNotCompiledLargeMethodNoBranches); |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 93 | GraphAnalysisResult HGraphBuilder::BuildGraph() { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 94 | DCHECK(code_item_ != nullptr); |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame] | 95 | DCHECK(graph_->GetBlocks().empty()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 96 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 97 | graph_->SetNumberOfVRegs(code_item_->registers_size_); |
| 98 | graph_->SetNumberOfInVRegs(code_item_->ins_size_); |
| 99 | graph_->SetMaximumNumberOfOutVRegs(code_item_->outs_size_); |
| 100 | graph_->SetHasTryCatch(code_item_->tries_size_ != 0); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 101 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 102 | // Use ScopedArenaAllocator for all local allocations. |
| 103 | ScopedArenaAllocator local_allocator(graph_->GetArenaStack()); |
| 104 | HBasicBlockBuilder block_builder(graph_, dex_file_, code_item_, &local_allocator); |
| 105 | SsaBuilder ssa_builder(graph_, |
| 106 | dex_compilation_unit_->GetClassLoader(), |
| 107 | dex_compilation_unit_->GetDexCache(), |
| 108 | handles_, |
| 109 | &local_allocator); |
| 110 | HInstructionBuilder instruction_builder(graph_, |
| 111 | &block_builder, |
| 112 | &ssa_builder, |
| 113 | dex_file_, |
| 114 | code_item_, |
| 115 | return_type_, |
| 116 | dex_compilation_unit_, |
| 117 | outer_compilation_unit_, |
| 118 | compiler_driver_, |
| 119 | code_generator_, |
| 120 | interpreter_metadata_, |
| 121 | compilation_stats_, |
| 122 | handles_, |
| 123 | &local_allocator); |
| 124 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 125 | // 1) Create basic blocks and link them together. Basic blocks are left |
| 126 | // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 127 | if (!block_builder.Build()) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 128 | return kAnalysisInvalidBytecode; |
| 129 | } |
| 130 | |
| 131 | // 2) Decide whether to skip this method based on its code size and number |
| 132 | // of branches. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 133 | if (SkipCompilation(block_builder.GetNumberOfBranches())) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 134 | return kAnalysisSkipped; |
| 135 | } |
| 136 | |
| 137 | // 3) Build the dominator tree and fill in loop and try/catch metadata. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 138 | GraphAnalysisResult result = graph_->BuildDominatorTree(); |
| 139 | if (result != kAnalysisSuccess) { |
| 140 | return result; |
| 141 | } |
| 142 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 143 | // 4) Populate basic blocks with instructions. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 144 | if (!instruction_builder.Build()) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 145 | return kAnalysisInvalidBytecode; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 146 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 147 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 148 | // 5) Type the graph and eliminate dead/redundant phis. |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 149 | return ssa_builder.BuildSsa(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame^] | 152 | void HGraphBuilder::BuildIntrinsicGraph(ArtMethod* method) { |
| 153 | DCHECK(code_item_ == nullptr); |
| 154 | DCHECK(graph_->GetBlocks().empty()); |
| 155 | |
| 156 | // Determine the number of arguments and associated vregs. |
| 157 | uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex(); |
| 158 | const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx)); |
| 159 | size_t num_args = strlen(shorty + 1); |
| 160 | size_t num_wide_args = std::count(shorty + 1, shorty + 1 + num_args, 'J') + |
| 161 | std::count(shorty + 1, shorty + 1 + num_args, 'D'); |
| 162 | size_t num_arg_vregs = num_args + num_wide_args + (dex_compilation_unit_->IsStatic() ? 0u : 1u); |
| 163 | |
| 164 | // For simplicity, reserve 2 vregs (the maximum) for return value regardless of the return type. |
| 165 | size_t return_vregs = 2u; |
| 166 | graph_->SetNumberOfVRegs(return_vregs + num_arg_vregs); |
| 167 | graph_->SetNumberOfInVRegs(num_arg_vregs); |
| 168 | graph_->SetMaximumNumberOfOutVRegs(num_arg_vregs); |
| 169 | graph_->SetHasTryCatch(false); |
| 170 | |
| 171 | // Use ScopedArenaAllocator for all local allocations. |
| 172 | ScopedArenaAllocator local_allocator(graph_->GetArenaStack()); |
| 173 | HBasicBlockBuilder block_builder(graph_, dex_file_, /* code_item */ nullptr, &local_allocator); |
| 174 | SsaBuilder ssa_builder(graph_, |
| 175 | dex_compilation_unit_->GetClassLoader(), |
| 176 | dex_compilation_unit_->GetDexCache(), |
| 177 | handles_, |
| 178 | &local_allocator); |
| 179 | HInstructionBuilder instruction_builder(graph_, |
| 180 | &block_builder, |
| 181 | &ssa_builder, |
| 182 | dex_file_, |
| 183 | /* code_item */ nullptr, |
| 184 | return_type_, |
| 185 | dex_compilation_unit_, |
| 186 | outer_compilation_unit_, |
| 187 | compiler_driver_, |
| 188 | code_generator_, |
| 189 | interpreter_metadata_, |
| 190 | compilation_stats_, |
| 191 | handles_, |
| 192 | &local_allocator); |
| 193 | |
| 194 | // 1) Create basic blocks for the intrinsic and link them together. |
| 195 | block_builder.BuildIntrinsic(); |
| 196 | |
| 197 | // 2) Build the trivial dominator tree. |
| 198 | GraphAnalysisResult bdt_result = graph_->BuildDominatorTree(); |
| 199 | DCHECK_EQ(bdt_result, kAnalysisSuccess); |
| 200 | |
| 201 | // 3) Populate basic blocks with instructions for the intrinsic. |
| 202 | instruction_builder.BuildIntrinsic(method); |
| 203 | |
| 204 | // 4) Type the graph (no dead/redundant phis to eliminate). |
| 205 | GraphAnalysisResult build_ssa_result = ssa_builder.BuildSsa(); |
| 206 | DCHECK_EQ(build_ssa_result, kAnalysisSuccess); |
| 207 | } |
| 208 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 209 | } // namespace art |