blob: c185b5887b63f818b291a07834151485ba181ad0 [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 Geoffray335005e2015-06-25 10:01:47 +010030#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010031#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000032#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "ssa_phi_elimination.h"
34#include "scoped_thread_state_change.h"
35#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010036#include "dex/verified_method.h"
37#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000038
39namespace art {
40
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010041static constexpr int kMaxInlineCodeUnits = 18;
42static constexpr int kDepthLimit = 3;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000043
44void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000045 if (graph_->IsDebuggable()) {
46 // For simplicity, we currently never inline when the graph is debuggable. This avoids
47 // doing some logic in the runtime to discover if a method could have been inlined.
48 return;
49 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000050 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010051 HBasicBlock* next_block = blocks.Get(0);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000052 for (size_t i = 0; i < blocks.Size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010053 // Because we are changing the graph when inlining, we need to remember the next block.
54 // This avoids doing the inlining work again on the inlined blocks.
55 if (blocks.Get(i) != next_block) {
56 continue;
57 }
58 HBasicBlock* block = next_block;
59 next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000060 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
61 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010062 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070063 // As long as the call is not intrinsified, it is worth trying to inline.
64 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000065 // We use the original invoke type to ensure the resolution of the called method
66 // works properly.
Nicolas Geoffray35071052015-06-09 15:43:38 +010067 if (!TryInline(call, call->GetDexMethodIndex())) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010068 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000069 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000070 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000071 bool should_inline = callee_name.find("$inline$") != std::string::npos;
72 CHECK(!should_inline) << "Could not inline " << callee_name;
73 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010074 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010075 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010076 std::string callee_name =
77 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
78 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
79 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
80 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000081 }
82 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000083 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000084 }
85 }
86}
87
Nicolas Geoffray454a4812015-06-09 10:37:32 +010088static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070089 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010090 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
91}
92
93/**
94 * Given the `resolved_method` looked up in the dex cache, try to find
95 * the actual runtime target of an interface or virtual call.
96 * Return nullptr if the runtime target cannot be proven.
97 */
98static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100100 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
101 // No need to lookup further, the resolved method will be the target.
102 return resolved_method;
103 }
104
105 HInstruction* receiver = invoke->InputAt(0);
106 if (receiver->IsNullCheck()) {
107 // Due to multiple levels of inlining within the same pass, it might be that
108 // null check does not have the reference type of the actual receiver.
109 receiver = receiver->InputAt(0);
110 }
111 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravleb7348082015-07-28 11:52:02 +0000112 if (info.IsTop()) {
113 // We have no information on the receiver.
114 return nullptr;
115 } else if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100116 // We currently only support inlining with known receivers.
117 // TODO: Remove this check, we should be able to inline final methods
118 // on unknown receivers.
119 return nullptr;
120 } else if (info.GetTypeHandle()->IsInterface()) {
121 // Statically knowing that the receiver has an interface type cannot
122 // help us find what is the target method.
123 return nullptr;
124 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
125 // The method that we're trying to call is not in the receiver's class or super classes.
126 return nullptr;
127 }
128
129 ClassLinker* cl = Runtime::Current()->GetClassLinker();
130 size_t pointer_size = cl->GetImagePointerSize();
131 if (invoke->IsInvokeInterface()) {
132 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
133 resolved_method, pointer_size);
134 } else {
135 DCHECK(invoke->IsInvokeVirtual());
136 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
137 resolved_method, pointer_size);
138 }
139
140 if (resolved_method == nullptr) {
141 // The information we had on the receiver was not enough to find
142 // the target method. Since we check above the exact type of the receiver,
143 // the only reason this can happen is an IncompatibleClassChangeError.
144 return nullptr;
145 } else if (resolved_method->IsAbstract()) {
146 // The information we had on the receiver was not enough to find
147 // the target method. Since we check above the exact type of the receiver,
148 // the only reason this can happen is an IncompatibleClassChangeError.
149 return nullptr;
150 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
151 // A final method has to be the target method.
152 return resolved_method;
153 } else if (info.IsExact()) {
154 // If we found a method and the receiver's concrete type is statically
155 // known, we know for sure the target.
156 return resolved_method;
157 } else {
158 // Even if we did find a method, the receiver type was not enough to
159 // statically find the runtime target.
160 return nullptr;
161 }
162}
163
164static uint32_t FindMethodIndexIn(ArtMethod* method,
165 const DexFile& dex_file,
166 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100168 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
169 return method->GetDexMethodIndex();
170 } else {
171 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
172 }
173}
174
Nicolas Geoffray35071052015-06-09 15:43:38 +0100175bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000177 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
178 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000179
Nicolas Geoffray35071052015-06-09 15:43:38 +0100180 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
181 // We can query the dex cache directly. The verifier has populated it already.
182 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
183 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100186 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000187 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 return false;
189 }
190
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100191 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
192 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
193 if (resolved_method == nullptr) {
194 VLOG(compiler) << "Interface or virtual call to "
195 << PrettyMethod(method_index, caller_dex_file)
196 << " could not be statically determined";
197 return false;
198 }
199 // We have found a method, but we need to find where that method is for the caller's
200 // dex file.
201 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
202 if (method_index == DexFile::kDexNoIndex) {
203 VLOG(compiler) << "Interface or virtual call to "
204 << PrettyMethod(resolved_method)
205 << " cannot be inlined because unaccessible to caller";
206 return false;
207 }
208 }
209
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100210 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000211 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100213 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 }
215
216 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
217
218 if (code_item == nullptr) {
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 it is native";
221 return false;
222 }
223
224 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000225 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 << " is too big to inline";
227 return false;
228 }
229
230 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000231 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 << " is not inlined because of try block";
233 return false;
234 }
235
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100236 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
237 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
238 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000239 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100240 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 return false;
242 }
243
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000244 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000245 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000246 << " was already flagged as non inlineable";
247 return false;
248 }
249
Roland Levillain4c0eb422015-04-24 16:43:49 +0100250 if (invoke_instruction->IsInvokeStaticOrDirect() &&
251 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
252 // Case of a static method that cannot be inlined because it implicitly
253 // requires an initialization check of its declaring class.
254 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
255 << " is not inlined because it is static and requires a clinit"
256 << " check that cannot be emitted due to Dex cache limitations";
257 return false;
258 }
259
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100260 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000261 return false;
262 }
263
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000264 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000265 MaybeRecordStat(kInlinedInvoke);
266 return true;
267}
268
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000270 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100271 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000272 ScopedObjectAccess soa(Thread::Current());
273 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100274 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
275 uint32_t method_index = resolved_method->GetDexMethodIndex();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000276
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000277 DexCompilationUnit dex_compilation_unit(
278 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000279 caller_compilation_unit_.GetClassLoader(),
280 caller_compilation_unit_.GetClassLinker(),
281 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000282 code_item,
283 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000284 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000285 resolved_method->GetAccessFlags(),
286 nullptr);
287
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100288 bool requires_ctor_barrier = false;
289
290 if (dex_compilation_unit.IsConstructor()) {
291 // If it's a super invocation and we already generate a barrier there's no need
292 // to generate another one.
293 // We identify super calls by looking at the "this" pointer. If its value is the
294 // same as the local "this" pointer then we must have a super invocation.
295 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
296 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
297 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
298 requires_ctor_barrier = false;
299 } else {
300 Thread* self = Thread::Current();
301 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
302 dex_compilation_unit.GetDexFile(),
303 dex_compilation_unit.GetClassDefIndex());
304 }
305 }
306
Nicolas Geoffray35071052015-06-09 15:43:38 +0100307 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
308 if (invoke_type == kInterface) {
309 // We have statically resolved the dispatch. To please the class linker
310 // at runtime, we change this call as if it was a virtual call.
311 invoke_type = kVirtual;
312 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000313 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100315 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100317 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100319 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100320 graph_->IsDebuggable(),
321 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000322
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000323 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000324 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000325 &dex_compilation_unit,
326 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000327 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000329 &inline_stats,
330 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000331
David Brazdil5e8b1372015-01-23 14:39:08 +0000332 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100333 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000334 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100335 // There could be multiple reasons why the graph could not be built, including
336 // unaccessible methods/fields due to using a different dex cache. We do not mark
337 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000338 return false;
339 }
340
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000341 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
342 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100343 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000344 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100345 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000346 return false;
347 }
348
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100350 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100352 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353 return false;
354 }
355
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000356 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100357 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000358 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100359 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000360 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000361
362 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000363 &dce,
364 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100365 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000366 &simplify,
367 };
368
369 for (size_t i = 0; i < arraysize(optimizations); ++i) {
370 HOptimization* optimization = optimizations[i];
371 optimization->Run();
372 }
373
374 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000375 HInliner inliner(callee_graph,
376 outer_compilation_unit_,
377 dex_compilation_unit,
378 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100379 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000380 stats_,
381 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000382 inliner.Run();
383 }
384
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100385 // TODO: We should abort only if all predecessors throw. However,
386 // HGraph::InlineInto currently does not handle an exit block with
387 // a throw predecessor.
388 HBasicBlock* exit_block = callee_graph->GetExitBlock();
389 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100390 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100391 << " could not be inlined because it has an infinite loop";
392 resolved_method->SetShouldNotInline();
393 return false;
394 }
395
396 bool has_throw_predecessor = false;
397 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
398 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
399 has_throw_predecessor = true;
400 break;
401 }
402 }
403 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100404 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 << " could not be inlined because one branch always throws";
406 resolved_method->SetShouldNotInline();
407 return false;
408 }
409
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000411 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000412 for (; !it.Done(); it.Advance()) {
413 HBasicBlock* block = it.Current();
414 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100415 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100417 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 return false;
419 }
420
421 for (HInstructionIterator instr_it(block->GetInstructions());
422 !instr_it.Done();
423 instr_it.Advance()) {
424 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000425
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100426 if (current->IsInvokeInterface()) {
427 // Disable inlining of interface calls. The cost in case of entering the
428 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100429 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100430 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100431 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000432 return false;
433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100436 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000437 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 return false;
440 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000441
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100443 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000444 << " could not be inlined because " << current->DebugName()
445 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100446 // Do not flag the method as not-inlineable. A caller within the same
447 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000448 return false;
449 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 }
451 }
452
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000454
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000455 return true;
456}
457
458} // namespace art