blob: d6b8aa4c0a45ddb194ac79a4cc33cc0f983c7b2d [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 Juravle2e768302015-07-28 14:41:11 +0000112 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
113 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100114 // We currently only support inlining with known receivers.
115 // TODO: Remove this check, we should be able to inline final methods
116 // on unknown receivers.
117 return nullptr;
118 } else if (info.GetTypeHandle()->IsInterface()) {
119 // Statically knowing that the receiver has an interface type cannot
120 // help us find what is the target method.
121 return nullptr;
122 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
123 // The method that we're trying to call is not in the receiver's class or super classes.
124 return nullptr;
125 }
126
127 ClassLinker* cl = Runtime::Current()->GetClassLinker();
128 size_t pointer_size = cl->GetImagePointerSize();
129 if (invoke->IsInvokeInterface()) {
130 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
131 resolved_method, pointer_size);
132 } else {
133 DCHECK(invoke->IsInvokeVirtual());
134 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
135 resolved_method, pointer_size);
136 }
137
138 if (resolved_method == nullptr) {
139 // The information we had on the receiver was not enough to find
140 // the target method. Since we check above the exact type of the receiver,
141 // the only reason this can happen is an IncompatibleClassChangeError.
142 return nullptr;
143 } else if (resolved_method->IsAbstract()) {
144 // The information we had on the receiver was not enough to find
145 // the target method. Since we check above the exact type of the receiver,
146 // the only reason this can happen is an IncompatibleClassChangeError.
147 return nullptr;
148 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
149 // A final method has to be the target method.
150 return resolved_method;
151 } else if (info.IsExact()) {
152 // If we found a method and the receiver's concrete type is statically
153 // known, we know for sure the target.
154 return resolved_method;
155 } else {
156 // Even if we did find a method, the receiver type was not enough to
157 // statically find the runtime target.
158 return nullptr;
159 }
160}
161
162static uint32_t FindMethodIndexIn(ArtMethod* method,
163 const DexFile& dex_file,
164 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100166 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
167 return method->GetDexMethodIndex();
168 } else {
169 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
170 }
171}
172
Nicolas Geoffray35071052015-06-09 15:43:38 +0100173bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000175 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
176 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177
Nicolas Geoffray35071052015-06-09 15:43:38 +0100178 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
179 // We can query the dex cache directly. The verifier has populated it already.
180 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
181 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100184 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000185 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 return false;
187 }
188
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100189 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
190 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
191 if (resolved_method == nullptr) {
192 VLOG(compiler) << "Interface or virtual call to "
193 << PrettyMethod(method_index, caller_dex_file)
194 << " could not be statically determined";
195 return false;
196 }
197 // We have found a method, but we need to find where that method is for the caller's
198 // dex file.
199 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
200 if (method_index == DexFile::kDexNoIndex) {
201 VLOG(compiler) << "Interface or virtual call to "
202 << PrettyMethod(resolved_method)
203 << " cannot be inlined because unaccessible to caller";
204 return false;
205 }
206 }
207
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100208 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000209 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000210 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100211 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 }
213
214 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
215
216 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000217 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 << " is not inlined because it is native";
219 return false;
220 }
221
222 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000223 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 << " is too big to inline";
225 return false;
226 }
227
228 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000229 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230 << " is not inlined because of try block";
231 return false;
232 }
233
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100234 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
235 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
236 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000237 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100238 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 return false;
240 }
241
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000242 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000243 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000244 << " was already flagged as non inlineable";
245 return false;
246 }
247
Roland Levillain4c0eb422015-04-24 16:43:49 +0100248 if (invoke_instruction->IsInvokeStaticOrDirect() &&
249 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
250 // Case of a static method that cannot be inlined because it implicitly
251 // requires an initialization check of its declaring class.
252 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
253 << " is not inlined because it is static and requires a clinit"
254 << " check that cannot be emitted due to Dex cache limitations";
255 return false;
256 }
257
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100258 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000259 return false;
260 }
261
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000262 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000263 MaybeRecordStat(kInlinedInvoke);
264 return true;
265}
266
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000268 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000270 ScopedObjectAccess soa(Thread::Current());
271 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100272 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
273 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000274 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000275 DexCompilationUnit dex_compilation_unit(
276 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000277 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000278 class_linker,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000279 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000280 code_item,
281 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000282 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000283 resolved_method->GetAccessFlags(),
284 nullptr);
285
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100286 bool requires_ctor_barrier = false;
287
288 if (dex_compilation_unit.IsConstructor()) {
289 // If it's a super invocation and we already generate a barrier there's no need
290 // to generate another one.
291 // We identify super calls by looking at the "this" pointer. If its value is the
292 // same as the local "this" pointer then we must have a super invocation.
293 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
294 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
295 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
296 requires_ctor_barrier = false;
297 } else {
298 Thread* self = Thread::Current();
299 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
300 dex_compilation_unit.GetDexFile(),
301 dex_compilation_unit.GetClassDefIndex());
302 }
303 }
304
Nicolas Geoffray35071052015-06-09 15:43:38 +0100305 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
306 if (invoke_type == kInterface) {
307 // We have statically resolved the dispatch. To please the class linker
308 // at runtime, we change this call as if it was a virtual call.
309 invoke_type = kVirtual;
310 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000311 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100313 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100315 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100317 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 graph_->IsDebuggable(),
319 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000321 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000322 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000323 &dex_compilation_unit,
324 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000325 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000326 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000327 &inline_stats,
328 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000329
David Brazdil5e8b1372015-01-23 14:39:08 +0000330 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100331 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000332 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100333 // There could be multiple reasons why the graph could not be built, including
334 // unaccessible methods/fields due to using a different dex cache. We do not mark
335 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000336 return false;
337 }
338
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000339 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
340 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100341 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000342 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100343 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000344 return false;
345 }
346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000347 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100348 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100350 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 return false;
352 }
353
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000354 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100355 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000356 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100357 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000358 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000359
360 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000361 &dce,
362 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100363 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000364 &simplify,
365 };
366
367 for (size_t i = 0; i < arraysize(optimizations); ++i) {
368 HOptimization* optimization = optimizations[i];
369 optimization->Run();
370 }
371
372 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000373 HInliner inliner(callee_graph,
374 outer_compilation_unit_,
375 dex_compilation_unit,
376 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100377 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000378 stats_,
379 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000380 inliner.Run();
381 }
382
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100383 // TODO: We should abort only if all predecessors throw. However,
384 // HGraph::InlineInto currently does not handle an exit block with
385 // a throw predecessor.
386 HBasicBlock* exit_block = callee_graph->GetExitBlock();
387 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100388 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100389 << " could not be inlined because it has an infinite loop";
390 resolved_method->SetShouldNotInline();
391 return false;
392 }
393
394 bool has_throw_predecessor = false;
395 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
396 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
397 has_throw_predecessor = true;
398 break;
399 }
400 }
401 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100402 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100403 << " could not be inlined because one branch always throws";
404 resolved_method->SetShouldNotInline();
405 return false;
406 }
407
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000408 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000409 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 for (; !it.Done(); it.Advance()) {
411 HBasicBlock* block = it.Current();
412 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100413 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100415 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 return false;
417 }
418
419 for (HInstructionIterator instr_it(block->GetInstructions());
420 !instr_it.Done();
421 instr_it.Advance()) {
422 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000423
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100424 if (current->IsInvokeInterface()) {
425 // Disable inlining of interface calls. The cost in case of entering the
426 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100427 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100428 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100429 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000430 return false;
431 }
432
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100433 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100434 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000435 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000437 return false;
438 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000439
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100440 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100441 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000442 << " could not be inlined because " << current->DebugName()
443 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100444 // Do not flag the method as not-inlineable. A caller within the same
445 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000446 return false;
447 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 }
449 }
450
Calin Juravle2e768302015-07-28 14:41:11 +0000451 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
452
453 // When merging the graph we might create a new NullConstant in the caller graph which does
454 // not have the chance to be typed. We assign the correct type here so that we can keep the
455 // assertion that every reference has a valid type. This also simplifies checks along the way.
456 HNullConstant* null_constant = graph_->GetNullConstant();
457 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
458 ReferenceTypeInfo::TypeHandle obj_handle =
459 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
460 null_constant->SetReferenceTypeInfo(
461 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
462 }
463
464 if ((return_replacement != nullptr)
465 && (return_replacement->GetType() == Primitive::kPrimNot)) {
466 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
467 // Make sure that we have a valid type for the return. We may get an invalid one when
468 // we inline invokes with multiple branches and create a Phi for the result.
469 // TODO: we could be more precise by merging the phi inputs but that requires
470 // some functionality from the reference type propagation.
471 DCHECK(return_replacement->IsPhi());
472 ReferenceTypeInfo::TypeHandle return_handle =
473 handles_->NewHandle(resolved_method->GetReturnType());
474 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
475 return_handle, return_handle->IsFinal() /* is_exact */));
476 }
477 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000478
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000479 return true;
480}
481
482} // namespace art