Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 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 "inliner.h" |
| 18 | |
| 19 | #include "builder.h" |
| 20 | #include "class_linker.h" |
| 21 | #include "constant_folding.h" |
| 22 | #include "dead_code_elimination.h" |
| 23 | #include "driver/compiler_driver-inl.h" |
| 24 | #include "driver/dex_compilation_unit.h" |
| 25 | #include "instruction_simplifier.h" |
| 26 | #include "mirror/art_method-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
| 29 | #include "nodes.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 30 | #include "register_allocator.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 31 | #include "ssa_phi_elimination.h" |
| 32 | #include "scoped_thread_state_change.h" |
| 33 | #include "thread.h" |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | static constexpr int kMaxInlineCodeUnits = 100; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 38 | static constexpr int kDepthLimit = 5; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 39 | |
| 40 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 41 | if (graph_->IsDebuggable()) { |
| 42 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 43 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 44 | return; |
| 45 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 46 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
| 47 | for (size_t i = 0; i < blocks.Size(); ++i) { |
| 48 | HBasicBlock* block = blocks.Get(i); |
| 49 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 50 | HInstruction* next = instruction->GetNext(); |
| 51 | HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect(); |
| 52 | if (call != nullptr) { |
| 53 | if (!TryInline(call, call->GetDexMethodIndex(), call->GetInvokeType())) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 54 | if (kIsDebugBuild) { |
| 55 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 56 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 57 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 58 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 59 | } |
| 60 | } |
| 61 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 62 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | bool HInliner::TryInline(HInvoke* invoke_instruction, |
| 68 | uint32_t method_index, |
| 69 | InvokeType invoke_type) const { |
| 70 | ScopedObjectAccess soa(Thread::Current()); |
| 71 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
| 72 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, outer_dex_file); |
| 73 | |
| 74 | StackHandleScope<3> hs(soa.Self()); |
| 75 | Handle<mirror::DexCache> dex_cache( |
| 76 | hs.NewHandle(outer_compilation_unit_.GetClassLinker()->FindDexCache(outer_dex_file))); |
| 77 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 78 | soa.Decode<mirror::ClassLoader*>(outer_compilation_unit_.GetClassLoader()))); |
| 79 | Handle<mirror::ArtMethod> resolved_method(hs.NewHandle( |
| 80 | compiler_driver_->ResolveMethod( |
| 81 | soa, dex_cache, class_loader, &outer_compilation_unit_, method_index, invoke_type))); |
| 82 | |
| 83 | if (resolved_method.Get() == nullptr) { |
| 84 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, outer_dex_file); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 89 | VLOG(compiler) << "Did not inline " |
| 90 | << PrettyMethod(method_index, outer_dex_file) |
| 91 | << " because it is in a different dex file"; |
| 92 | return false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 96 | |
| 97 | if (code_item == nullptr) { |
| 98 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 99 | << " is not inlined because it is native"; |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) { |
| 104 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 105 | << " is too big to inline"; |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (code_item->tries_size_ != 0) { |
| 110 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 111 | << " is not inlined because of try block"; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (!resolved_method->GetDeclaringClass()->IsVerified()) { |
| 116 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 117 | << " is not inlined because its class could not be verified"; |
| 118 | return false; |
| 119 | } |
| 120 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 121 | if (resolved_method->ShouldNotInline()) { |
| 122 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 123 | << " was already flagged as non inlineable"; |
| 124 | return false; |
| 125 | } |
| 126 | |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 127 | if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 128 | resolved_method->SetShouldNotInline(); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, outer_dex_file); |
| 133 | MaybeRecordStat(kInlinedInvoke); |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method, |
| 138 | HInvoke* invoke_instruction, |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 139 | uint32_t method_index) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 140 | ScopedObjectAccess soa(Thread::Current()); |
| 141 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 142 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
| 143 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 144 | DexCompilationUnit dex_compilation_unit( |
| 145 | nullptr, |
| 146 | outer_compilation_unit_.GetClassLoader(), |
| 147 | outer_compilation_unit_.GetClassLinker(), |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 148 | outer_dex_file, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 149 | code_item, |
| 150 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 151 | method_index, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 152 | resolved_method->GetAccessFlags(), |
| 153 | nullptr); |
| 154 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 155 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
| 156 | graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 157 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 158 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 159 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 160 | &dex_compilation_unit, |
| 161 | &outer_compilation_unit_, |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 162 | &outer_dex_file, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 163 | compiler_driver_, |
| 164 | &inline_stats); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 165 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 166 | if (!builder.BuildGraph(*code_item)) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 167 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 168 | << " could not be built, so cannot be inlined"; |
| 169 | return false; |
| 170 | } |
| 171 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 172 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 173 | compiler_driver_->GetInstructionSet())) { |
| 174 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 175 | << " cannot be inlined because of the register allocator"; |
| 176 | return false; |
| 177 | } |
| 178 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 179 | if (!callee_graph->TryBuildingSsa()) { |
| 180 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 181 | << " could not be transformed to SSA"; |
| 182 | return false; |
| 183 | } |
| 184 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 185 | // Run simple optimizations on the graph. |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 186 | HDeadCodeElimination dce(callee_graph); |
| 187 | HConstantFolding fold(callee_graph); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 188 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 189 | |
| 190 | HOptimization* optimizations[] = { |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 191 | &dce, |
| 192 | &fold, |
| 193 | &simplify, |
| 194 | }; |
| 195 | |
| 196 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 197 | HOptimization* optimization = optimizations[i]; |
| 198 | optimization->Run(); |
| 199 | } |
| 200 | |
| 201 | if (depth_ + 1 < kDepthLimit) { |
| 202 | HInliner inliner( |
Nicolas Geoffray | 6a816cf | 2015-03-24 16:17:56 +0000 | [diff] [blame] | 203 | callee_graph, outer_compilation_unit_, compiler_driver_, stats_, depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 204 | inliner.Run(); |
| 205 | } |
| 206 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 207 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 208 | it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 209 | for (; !it.Done(); it.Advance()) { |
| 210 | HBasicBlock* block = it.Current(); |
| 211 | if (block->IsLoopHeader()) { |
| 212 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 213 | << " could not be inlined because it contains a loop"; |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 218 | !instr_it.Done(); |
| 219 | instr_it.Advance()) { |
| 220 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 221 | if (current->IsSuspendCheck()) { |
| 222 | continue; |
| 223 | } |
| 224 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 225 | if (current->CanThrow()) { |
| 226 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 227 | << " could not be inlined because " << current->DebugName() |
| 228 | << " can throw"; |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | if (current->NeedsEnvironment()) { |
| 233 | VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file) |
| 234 | << " could not be inlined because " << current->DebugName() |
| 235 | << " needs an environment"; |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 241 | callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 242 | |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 243 | if (callee_graph->HasArrayAccesses()) { |
| 244 | graph_->SetHasArrayAccesses(true); |
| 245 | } |
| 246 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 247 | // Now that we have inlined the callee, we need to update the next |
| 248 | // instruction id of the caller, so that new instructions added |
| 249 | // after optimizations get a unique id. |
| 250 | graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
| 254 | } // namespace art |