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 | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 23 | #include "data_type-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 24 | #include "dex/verified_method.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 25 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 26 | #include "mirror/class_loader.h" |
| 27 | #include "mirror/dex_cache.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 28 | #include "nodes.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 29 | #include "thread.h" |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 30 | #include "utils/dex_cache_arrays_layout-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 34 | HGraphBuilder::HGraphBuilder(HGraph* graph, |
| 35 | DexCompilationUnit* dex_compilation_unit, |
| 36 | const DexCompilationUnit* const outer_compilation_unit, |
| 37 | CompilerDriver* driver, |
| 38 | CodeGenerator* code_generator, |
| 39 | OptimizingCompilerStats* compiler_stats, |
| 40 | const uint8_t* interpreter_metadata, |
| 41 | Handle<mirror::DexCache> dex_cache, |
| 42 | VariableSizedHandleScope* handles) |
| 43 | : graph_(graph), |
| 44 | dex_file_(&graph->GetDexFile()), |
| 45 | code_item_(*dex_compilation_unit->GetCodeItem()), |
| 46 | dex_compilation_unit_(dex_compilation_unit), |
| 47 | compiler_driver_(driver), |
| 48 | compilation_stats_(compiler_stats), |
| 49 | block_builder_(graph, dex_file_, code_item_), |
| 50 | ssa_builder_(graph, |
| 51 | dex_compilation_unit->GetClassLoader(), |
| 52 | dex_compilation_unit->GetDexCache(), |
| 53 | handles), |
| 54 | instruction_builder_(graph, |
| 55 | &block_builder_, |
| 56 | &ssa_builder_, |
| 57 | dex_file_, |
| 58 | code_item_, |
| 59 | DataType::FromShorty(dex_compilation_unit_->GetShorty()[0]), |
| 60 | dex_compilation_unit, |
| 61 | outer_compilation_unit, |
| 62 | driver, |
| 63 | code_generator, |
| 64 | interpreter_metadata, |
| 65 | compiler_stats, |
| 66 | dex_cache, |
| 67 | handles) {} |
| 68 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 69 | bool HGraphBuilder::SkipCompilation(size_t number_of_branches) { |
| 70 | if (compiler_driver_ == nullptr) { |
| 71 | // Note that the compiler driver is null when unit testing. |
| 72 | return false; |
| 73 | } |
| 74 | |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 75 | const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 76 | CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter(); |
| 77 | if (compiler_filter == CompilerFilter::kEverything) { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 81 | if (compiler_options.IsHugeMethod(code_item_.insns_size_in_code_units_)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 82 | VLOG(compiler) << "Skip compilation of huge method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 83 | << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex()) |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 84 | << ": " << code_item_.insns_size_in_code_units_ << " code units"; |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 85 | MaybeRecordStat(compilation_stats_, |
| 86 | MethodCompilationStat::kNotCompiledHugeMethod); |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
| 90 | // If it's large and contains no branches, it's likely to be machine generated initialization. |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 91 | if (compiler_options.IsLargeMethod(code_item_.insns_size_in_code_units_) |
David Brazdil | 1b49872 | 2015-03-31 11:37:18 +0100 | [diff] [blame] | 92 | && (number_of_branches == 0)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 93 | VLOG(compiler) << "Skip compilation of large method with no branch " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 94 | << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex()) |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 95 | << ": " << code_item_.insns_size_in_code_units_ << " code units"; |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 96 | MaybeRecordStat(compilation_stats_, |
| 97 | MethodCompilationStat::kNotCompiledLargeMethodNoBranches); |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 104 | GraphAnalysisResult HGraphBuilder::BuildGraph() { |
David Brazdil | 6032891 | 2016-04-04 17:47:42 +0000 | [diff] [blame] | 105 | DCHECK(graph_->GetBlocks().empty()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 106 | |
| 107 | graph_->SetNumberOfVRegs(code_item_.registers_size_); |
| 108 | graph_->SetNumberOfInVRegs(code_item_.ins_size_); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 109 | graph_->SetMaximumNumberOfOutVRegs(code_item_.outs_size_); |
| 110 | graph_->SetHasTryCatch(code_item_.tries_size_ != 0); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 111 | |
| 112 | // 1) Create basic blocks and link them together. Basic blocks are left |
| 113 | // unpopulated with the exception of synthetic blocks, e.g. HTryBoundaries. |
| 114 | if (!block_builder_.Build()) { |
| 115 | return kAnalysisInvalidBytecode; |
| 116 | } |
| 117 | |
| 118 | // 2) Decide whether to skip this method based on its code size and number |
| 119 | // of branches. |
| 120 | if (SkipCompilation(block_builder_.GetNumberOfBranches())) { |
| 121 | return kAnalysisSkipped; |
| 122 | } |
| 123 | |
| 124 | // 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] | 125 | GraphAnalysisResult result = graph_->BuildDominatorTree(); |
| 126 | if (result != kAnalysisSuccess) { |
| 127 | return result; |
| 128 | } |
| 129 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 130 | // 4) Populate basic blocks with instructions. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 131 | if (!instruction_builder_.Build()) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 132 | return kAnalysisInvalidBytecode; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 133 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 134 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 135 | // 5) Type the graph and eliminate dead/redundant phis. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 136 | return ssa_builder_.BuildSsa(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 139 | } // namespace art |