blob: 07d0dd6b49c0d5925d6f950a339da9fffab0fbf2 [file] [log] [blame]
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001/*
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 Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000020#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 Geoffraye53798a2014-12-01 10:31:54 +000027#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
29#include "nodes.h"
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +010030#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000031#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "ssa_phi_elimination.h"
33#include "scoped_thread_state_change.h"
34#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010035#include "dex/verified_method.h"
36#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000037
38namespace art {
39
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010040static constexpr int kMaxInlineCodeUnits = 18;
41static constexpr int kDepthLimit = 3;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000042
43void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000044 if (graph_->IsDebuggable()) {
45 // For simplicity, we currently never inline when the graph is debuggable. This avoids
46 // doing some logic in the runtime to discover if a method could have been inlined.
47 return;
48 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000049 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010050 HBasicBlock* next_block = blocks.Get(0);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000051 for (size_t i = 0; i < blocks.Size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010052 // Because we are changing the graph when inlining, we need to remember the next block.
53 // This avoids doing the inlining work again on the inlined blocks.
54 if (blocks.Get(i) != next_block) {
55 continue;
56 }
57 HBasicBlock* block = next_block;
58 next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000059 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
60 HInstruction* next = instruction->GetNext();
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +010061 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070062 // As long as the call is not intrinsified, it is worth trying to inline.
63 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000064 // We use the original invoke type to ensure the resolution of the called method
65 // works properly.
66 if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000067 if (kIsDebugBuild) {
68 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000069 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000070 bool should_inline = callee_name.find("$inline$") != std::string::npos;
71 CHECK(!should_inline) << "Could not inline " << callee_name;
72 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010073 } else {
74 if (kIsDebugBuild) {
75 std::string callee_name =
76 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
77 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
78 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
79 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 }
81 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000082 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000083 }
84 }
85}
86
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +010087static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
90}
91
92/**
93 * Given the `resolved_method` looked up in the dex cache, try to find
94 * the actual runtime target of an interface or virtual call.
95 * Return nullptr if the runtime target cannot be proven.
96 */
97static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
100 // No need to lookup further, the resolved method will be the target.
101 return resolved_method;
102 }
103
104 HInstruction* receiver = invoke->InputAt(0);
105 if (receiver->IsNullCheck()) {
106 // Due to multiple levels of inlining within the same pass, it might be that
107 // null check does not have the reference type of the actual receiver.
108 receiver = receiver->InputAt(0);
109 }
110 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
111 if (info.IsTop()) {
112 // We have no information on the receiver.
113 return nullptr;
114 } else if (!info.IsExact()) {
115 // We currently only support inlining with known receivers.
116 // TODO: Remove this check, we should be able to inline final methods
117 // on unknown receivers.
118 return nullptr;
119 } else if (info.GetTypeHandle()->IsInterface()) {
120 // Statically knowing that the receiver has an interface type cannot
121 // help us find what is the target method.
122 return nullptr;
Nicolas Geoffray7ce4b3d2015-06-04 18:02:34 +0100123 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
124 // The method that we're trying to call is not in the receiver's class or super classes.
125 return nullptr;
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100126 }
127
128 ClassLinker* cl = Runtime::Current()->GetClassLinker();
129 size_t pointer_size = cl->GetImagePointerSize();
130 if (invoke->IsInvokeInterface()) {
131 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
132 resolved_method, pointer_size);
133 } else {
134 DCHECK(invoke->IsInvokeVirtual());
135 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
136 resolved_method, pointer_size);
137 }
138
139 if (resolved_method == nullptr) {
140 // The information we had on the receiver was not enough to find
141 // the target method. Since we check above the exact type of the receiver,
142 // the only reason this can happen is an IncompatibleClassChangeError.
143 return nullptr;
144 } else if (resolved_method->IsAbstract()) {
145 // The information we had on the receiver was not enough to find
146 // the target method. Since we check above the exact type of the receiver,
147 // the only reason this can happen is an IncompatibleClassChangeError.
148 return nullptr;
149 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
150 // A final method has to be the target method.
151 return resolved_method;
152 } else if (info.IsExact()) {
153 // If we found a method and the receiver's concrete type is statically
154 // known, we know for sure the target.
155 return resolved_method;
156 } else {
157 // Even if we did find a method, the receiver type was not enough to
158 // statically find the runtime target.
159 return nullptr;
160 }
161}
162
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163bool HInliner::TryInline(HInvoke* invoke_instruction,
164 uint32_t method_index,
165 InvokeType invoke_type) const {
166 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000167 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
168 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100170 ArtMethod* resolved_method = nullptr;
171 {
172 // Don't keep this handle scope on stack, otherwise we cannot do a reference type
173 // propagation while inlining.
174 StackHandleScope<2> hs(soa.Self());
175 Handle<mirror::DexCache> dex_cache(
176 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
177 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
178 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
179 resolved_method = compiler_driver_->ResolveMethod(
180 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type);
181 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 if (resolved_method == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000184 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 return false;
186 }
187
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100188 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
189 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
190 if (resolved_method == nullptr) {
191 VLOG(compiler) << "Interface or virtual call to "
192 << PrettyMethod(method_index, caller_dex_file)
193 << "could not be statically determined";
194 return false;
195 }
196 }
197
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100198 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000199 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100201 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 }
203
204 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
205
206 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000207 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 << " is not inlined because it is native";
209 return false;
210 }
211
212 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000213 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 << " is too big to inline";
215 return false;
216 }
217
218 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000219 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 << " is not inlined because of try block";
221 return false;
222 }
223
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100224 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
225 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
226 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000227 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100228 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229 return false;
230 }
231
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000232 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000233 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000234 << " was already flagged as non inlineable";
235 return false;
236 }
237
Roland Levillain4c0eb422015-04-24 16:43:49 +0100238 if (invoke_instruction->IsInvokeStaticOrDirect() &&
239 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
240 // Case of a static method that cannot be inlined because it implicitly
241 // requires an initialization check of its declaring class.
242 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
243 << " is not inlined because it is static and requires a clinit"
244 << " check that cannot be emitted due to Dex cache limitations";
245 return false;
246 }
247
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100248 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000249 return false;
250 }
251
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000252 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000253 MaybeRecordStat(kInlinedInvoke);
254 return true;
255}
256
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000258 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000259 uint32_t method_index,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100260 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000261 ScopedObjectAccess soa(Thread::Current());
262 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000263 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000264
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 DexCompilationUnit dex_compilation_unit(
266 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000267 caller_compilation_unit_.GetClassLoader(),
268 caller_compilation_unit_.GetClassLinker(),
269 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000270 code_item,
271 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000272 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000273 resolved_method->GetAccessFlags(),
274 nullptr);
275
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100276 bool requires_ctor_barrier = false;
277
278 if (dex_compilation_unit.IsConstructor()) {
279 // If it's a super invocation and we already generate a barrier there's no need
280 // to generate another one.
281 // We identify super calls by looking at the "this" pointer. If its value is the
282 // same as the local "this" pointer then we must have a super invocation.
283 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
284 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
285 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
286 requires_ctor_barrier = false;
287 } else {
288 Thread* self = Thread::Current();
289 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
290 dex_compilation_unit.GetDexFile(),
291 dex_compilation_unit.GetClassDefIndex());
292 }
293 }
294
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000295 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100296 graph_->GetArena(),
297 caller_dex_file,
298 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100299 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 compiler_driver_->GetInstructionSet(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100301 invoke_instruction->GetOriginalInvokeType(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 graph_->IsDebuggable(),
303 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000304
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000305 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000306 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000307 &dex_compilation_unit,
308 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000309 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000310 compiler_driver_,
311 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000312
David Brazdil5e8b1372015-01-23 14:39:08 +0000313 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000314 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000315 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100316 // There could be multiple reasons why the graph could not be built, including
317 // unaccessible methods/fields due to using a different dex cache. We do not mark
318 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000319 return false;
320 }
321
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000322 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
323 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000324 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000325 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100326 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000327 return false;
328 }
329
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000330 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000331 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000332 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100333 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000334 return false;
335 }
336
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000337 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100338 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000339 HConstantFolding fold(callee_graph);
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100340 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000341 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000342
343 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000344 &dce,
345 &fold,
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100346 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000347 &simplify,
348 };
349
350 for (size_t i = 0; i < arraysize(optimizations); ++i) {
351 HOptimization* optimization = optimizations[i];
352 optimization->Run();
353 }
354
355 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000356 HInliner inliner(callee_graph,
357 outer_compilation_unit_,
358 dex_compilation_unit,
359 compiler_driver_,
Nicolas Geoffray1d5006c2015-06-03 15:04:32 +0100360 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000361 stats_,
362 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000363 inliner.Run();
364 }
365
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100366 // TODO: We should abort only if all predecessors throw. However,
367 // HGraph::InlineInto currently does not handle an exit block with
368 // a throw predecessor.
369 HBasicBlock* exit_block = callee_graph->GetExitBlock();
370 if (exit_block == nullptr) {
371 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
372 << " could not be inlined because it has an infinite loop";
373 resolved_method->SetShouldNotInline();
374 return false;
375 }
376
377 bool has_throw_predecessor = false;
378 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
379 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
380 has_throw_predecessor = true;
381 break;
382 }
383 }
384 if (has_throw_predecessor) {
385 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
386 << " could not be inlined because one branch always throws";
387 resolved_method->SetShouldNotInline();
388 return false;
389 }
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000392 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 for (; !it.Done(); it.Advance()) {
394 HBasicBlock* block = it.Current();
395 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000396 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100398 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399 return false;
400 }
401
402 for (HInstructionIterator instr_it(block->GetInstructions());
403 !instr_it.Done();
404 instr_it.Advance()) {
405 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000406
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100407 if (current->IsInvokeInterface()) {
408 // Disable inlining of interface calls. The cost in case of entering the
409 // resolution conflict is currently too high.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000410 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100411 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100412 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 return false;
414 }
415
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100416 if (!same_dex_file && current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000417 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100419 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 return false;
421 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000422
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100423 if (!same_dex_file && current->NeedsDexCache()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000424 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
425 << " could not be inlined because " << current->DebugName()
426 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100427 // Do not flag the method as not-inlineable. A caller within the same
428 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000429 return false;
430 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 }
432 }
433
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000435
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000436 return true;
437}
438
439} // namespace art