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