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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 19 | #include "art_method-inl.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 20 | #include "builder.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "constant_folding.h" |
| 23 | #include "dead_code_elimination.h" |
| 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "instruction_simplifier.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 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" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 34 | #include "dex/verified_method.h" |
| 35 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 39 | static constexpr int kMaxInlineCodeUnits = 18; |
| 40 | static constexpr int kDepthLimit = 3; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 41 | |
| 42 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 43 | if (graph_->IsDebuggable()) { |
| 44 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 45 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 46 | return; |
| 47 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 48 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 49 | HBasicBlock* next_block = blocks.Get(0); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 50 | for (size_t i = 0; i < blocks.Size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 51 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 52 | // This avoids doing the inlining work again on the inlined blocks. |
| 53 | if (blocks.Get(i) != next_block) { |
| 54 | continue; |
| 55 | } |
| 56 | HBasicBlock* block = next_block; |
| 57 | next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 58 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 59 | HInstruction* next = instruction->GetNext(); |
| 60 | HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 61 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 62 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 63 | // We use the original invoke type to ensure the resolution of the called method |
| 64 | // works properly. |
| 65 | if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 66 | if (kIsDebugBuild) { |
| 67 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 68 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 69 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 70 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 71 | } |
| 72 | } |
| 73 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 74 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool HInliner::TryInline(HInvoke* invoke_instruction, |
| 80 | uint32_t method_index, |
| 81 | InvokeType invoke_type) const { |
| 82 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 83 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 84 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 85 | |
| 86 | StackHandleScope<3> hs(soa.Self()); |
| 87 | Handle<mirror::DexCache> dex_cache( |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 88 | hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file))); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 89 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 90 | soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader()))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 91 | ArtMethod* resolved_method(compiler_driver_->ResolveMethod( |
| 92 | soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type)); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 93 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 94 | if (resolved_method == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 95 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 99 | bool same_dex_file = true; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 100 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 101 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 102 | same_dex_file = false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 106 | |
| 107 | if (code_item == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 108 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 109 | << " is not inlined because it is native"; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 114 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 115 | << " is too big to inline"; |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 120 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 121 | << " is not inlined because of try block"; |
| 122 | return false; |
| 123 | } |
| 124 | |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 125 | uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex(); |
| 126 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 127 | resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 128 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 129 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 133 | if (resolved_method->ShouldNotInline()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 134 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 135 | << " was already flagged as non inlineable"; |
| 136 | return false; |
| 137 | } |
| 138 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 139 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 140 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 141 | // Case of a static method that cannot be inlined because it implicitly |
| 142 | // requires an initialization check of its declaring class. |
| 143 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 144 | << " is not inlined because it is static and requires a clinit" |
| 145 | << " check that cannot be emitted due to Dex cache limitations"; |
| 146 | return false; |
| 147 | } |
| 148 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 149 | if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 153 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 154 | MaybeRecordStat(kInlinedInvoke); |
| 155 | return true; |
| 156 | } |
| 157 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 158 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 159 | HInvoke* invoke_instruction, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 160 | uint32_t method_index, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 161 | bool same_dex_file) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 162 | ScopedObjectAccess soa(Thread::Current()); |
| 163 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 164 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 165 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 166 | DexCompilationUnit dex_compilation_unit( |
| 167 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 168 | caller_compilation_unit_.GetClassLoader(), |
| 169 | caller_compilation_unit_.GetClassLinker(), |
| 170 | *resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 171 | code_item, |
| 172 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 173 | resolved_method->GetDexMethodIndex(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 174 | resolved_method->GetAccessFlags(), |
| 175 | nullptr); |
| 176 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 177 | bool requires_ctor_barrier = false; |
| 178 | |
| 179 | if (dex_compilation_unit.IsConstructor()) { |
| 180 | // If it's a super invocation and we already generate a barrier there's no need |
| 181 | // to generate another one. |
| 182 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 183 | // same as the local "this" pointer then we must have a super invocation. |
| 184 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 185 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 186 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 187 | requires_ctor_barrier = false; |
| 188 | } else { |
| 189 | Thread* self = Thread::Current(); |
| 190 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 191 | dex_compilation_unit.GetDexFile(), |
| 192 | dex_compilation_unit.GetClassDefIndex()); |
| 193 | } |
| 194 | } |
| 195 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 196 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 197 | graph_->GetArena(), |
| 198 | caller_dex_file, |
| 199 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 200 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 201 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 202 | invoke_instruction->GetOriginalInvokeType(), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 203 | graph_->IsDebuggable(), |
| 204 | graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 205 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 206 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 207 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 208 | &dex_compilation_unit, |
| 209 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 210 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 211 | compiler_driver_, |
| 212 | &inline_stats); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 213 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 214 | if (!builder.BuildGraph(*code_item)) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 215 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 216 | << " could not be built, so cannot be inlined"; |
Nicolas Geoffray | 5ae1325 | 2015-05-27 12:53:36 +0100 | [diff] [blame] | 217 | // There could be multiple reasons why the graph could not be built, including |
| 218 | // unaccessible methods/fields due to using a different dex cache. We do not mark |
| 219 | // the method as non-inlineable so that other callers can still try to inline it. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 223 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 224 | compiler_driver_->GetInstructionSet())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 225 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 226 | << " cannot be inlined because of the register allocator"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 227 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 231 | if (!callee_graph->TryBuildingSsa()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 232 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 233 | << " could not be transformed to SSA"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 234 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 238 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 239 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 240 | HConstantFolding fold(callee_graph); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 241 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 242 | |
| 243 | HOptimization* optimizations[] = { |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 244 | &dce, |
| 245 | &fold, |
| 246 | &simplify, |
| 247 | }; |
| 248 | |
| 249 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 250 | HOptimization* optimization = optimizations[i]; |
| 251 | optimization->Run(); |
| 252 | } |
| 253 | |
| 254 | if (depth_ + 1 < kDepthLimit) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 255 | HInliner inliner(callee_graph, |
| 256 | outer_compilation_unit_, |
| 257 | dex_compilation_unit, |
| 258 | compiler_driver_, |
| 259 | stats_, |
| 260 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 261 | inliner.Run(); |
| 262 | } |
| 263 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 264 | // TODO: We should abort only if all predecessors throw. However, |
| 265 | // HGraph::InlineInto currently does not handle an exit block with |
| 266 | // a throw predecessor. |
| 267 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 268 | if (exit_block == nullptr) { |
| 269 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 270 | << " could not be inlined because it has an infinite loop"; |
| 271 | resolved_method->SetShouldNotInline(); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | bool has_throw_predecessor = false; |
| 276 | for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) { |
| 277 | if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) { |
| 278 | has_throw_predecessor = true; |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | if (has_throw_predecessor) { |
| 283 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 284 | << " could not be inlined because one branch always throws"; |
| 285 | resolved_method->SetShouldNotInline(); |
| 286 | return false; |
| 287 | } |
| 288 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 289 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 290 | 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] | 291 | for (; !it.Done(); it.Advance()) { |
| 292 | HBasicBlock* block = it.Current(); |
| 293 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 294 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 295 | << " could not be inlined because it contains a loop"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 296 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 301 | !instr_it.Done(); |
| 302 | instr_it.Advance()) { |
| 303 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 304 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 305 | if (current->IsInvokeInterface()) { |
| 306 | // Disable inlining of interface calls. The cost in case of entering the |
| 307 | // resolution conflict is currently too high. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 308 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 309 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 310 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 314 | if (!same_dex_file && current->NeedsEnvironment()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 315 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 316 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 317 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 320 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 321 | if (!same_dex_file && current->NeedsDexCache()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 322 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 323 | << " could not be inlined because " << current->DebugName() |
| 324 | << " it is in a different dex file and requires access to the dex cache"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 325 | // Do not flag the method as not-inlineable. A caller within the same |
| 326 | // dex file could still successfully inline it. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 327 | return false; |
| 328 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 332 | callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 333 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
| 337 | } // namespace art |