blob: 12fd1c37d7aa78f6eea9750758b7ff7735ab2dc5 [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"
Calin Juravleec748352015-07-29 13:52:12 +010025#include "driver/compiler_options.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "driver/dex_compilation_unit.h"
27#include "instruction_simplifier.h"
Scott Wakelingd60a1af2015-07-22 14:32:44 +010028#include "intrinsics.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000029#include "mirror/class_loader.h"
30#include "mirror/dex_cache.h"
31#include "nodes.h"
Nicolas Geoffray335005e2015-06-25 10:01:47 +010032#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010033#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000034#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000035#include "ssa_phi_elimination.h"
36#include "scoped_thread_state_change.h"
37#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010038#include "dex/verified_method.h"
39#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040
41namespace art {
42
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070043static constexpr size_t kMaximumNumberOfHInstructions = 12;
44
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000046 if (graph_->IsDebuggable()) {
47 // For simplicity, we currently never inline when the graph is debuggable. This avoids
48 // doing some logic in the runtime to discover if a method could have been inlined.
49 return;
50 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010051 const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
52 DCHECK(!blocks.empty());
53 HBasicBlock* next_block = blocks[0];
54 for (size_t i = 0; i < blocks.size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010055 // Because we are changing the graph when inlining, we need to remember the next block.
56 // This avoids doing the inlining work again on the inlined blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010057 if (blocks[i] != next_block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010058 continue;
59 }
60 HBasicBlock* block = next_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +010061 next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1];
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000062 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
63 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010064 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070065 // As long as the call is not intrinsified, it is worth trying to inline.
66 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000067 // We use the original invoke type to ensure the resolution of the called method
68 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000069 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010070 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000071 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000072 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000073 bool should_inline = callee_name.find("$inline$") != std::string::npos;
74 CHECK(!should_inline) << "Could not inline " << callee_name;
75 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010076 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010077 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010078 std::string callee_name =
79 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
80 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
81 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
82 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000083 }
84 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000085 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000086 }
87 }
88}
89
Nicolas Geoffray454a4812015-06-09 10:37:32 +010090static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070091 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010092 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
93}
94
95/**
96 * Given the `resolved_method` looked up in the dex cache, try to find
97 * the actual runtime target of an interface or virtual call.
98 * Return nullptr if the runtime target cannot be proven.
99 */
100static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100102 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
103 // No need to lookup further, the resolved method will be the target.
104 return resolved_method;
105 }
106
107 HInstruction* receiver = invoke->InputAt(0);
108 if (receiver->IsNullCheck()) {
109 // Due to multiple levels of inlining within the same pass, it might be that
110 // null check does not have the reference type of the actual receiver.
111 receiver = receiver->InputAt(0);
112 }
113 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000114 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
115 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 Geoffraye418dda2015-08-11 20:03:09 -0700175bool HInliner::TryInline(HInvoke* invoke_instruction) {
Vladimir Marko58155012015-08-19 12:49:41 +0000176 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000178 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
179 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180
Nicolas Geoffray35071052015-06-09 15:43:38 +0100181 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
182 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000183 ArtMethod* resolved_method;
184 if (invoke_instruction->IsInvokeStaticOrDirect()) {
185 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700186 mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file)
187 ? caller_compilation_unit_.GetDexCache().Get()
188 : class_linker->FindDexCache(soa.Self(), *ref.dex_file);
189 resolved_method = dex_cache->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000190 ref.dex_method_index, class_linker->GetImagePointerSize());
191 } else {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700192 resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000193 method_index, class_linker->GetImagePointerSize());
194 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000195
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100197 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000198 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 return false;
200 }
201
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100202 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
203 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
204 if (resolved_method == nullptr) {
205 VLOG(compiler) << "Interface or virtual call to "
206 << PrettyMethod(method_index, caller_dex_file)
207 << " could not be statically determined";
208 return false;
209 }
210 // We have found a method, but we need to find where that method is for the caller's
211 // dex file.
212 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
213 if (method_index == DexFile::kDexNoIndex) {
214 VLOG(compiler) << "Interface or virtual call to "
215 << PrettyMethod(resolved_method)
216 << " cannot be inlined because unaccessible to caller";
217 return false;
218 }
219 }
220
Vladimir Marko58155012015-08-19 12:49:41 +0000221 bool same_dex_file =
222 IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *resolved_method->GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223
224 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
225
226 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000227 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000228 << " is not inlined because it is native";
229 return false;
230 }
231
Calin Juravleec748352015-07-29 13:52:12 +0100232 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
233 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000234 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 << " is too big to inline";
236 return false;
237 }
238
239 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000240 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 << " is not inlined because of try block";
242 return false;
243 }
244
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100245 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
246 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
247 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000248 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100249 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000250 return false;
251 }
252
Roland Levillain4c0eb422015-04-24 16:43:49 +0100253 if (invoke_instruction->IsInvokeStaticOrDirect() &&
254 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
255 // Case of a static method that cannot be inlined because it implicitly
256 // requires an initialization check of its declaring class.
257 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
258 << " is not inlined because it is static and requires a clinit"
259 << " check that cannot be emitted due to Dex cache limitations";
260 return false;
261 }
262
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100263 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000264 return false;
265 }
266
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000267 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000268 MaybeRecordStat(kInlinedInvoke);
269 return true;
270}
271
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000273 HInvoke* invoke_instruction,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700274 bool same_dex_file) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000275 ScopedObjectAccess soa(Thread::Current());
276 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100277 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
278 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000279 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700280 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000281 DexCompilationUnit dex_compilation_unit(
282 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000283 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000284 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000285 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000286 code_item,
287 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000288 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000289 resolved_method->GetAccessFlags(),
Mathieu Chartier736b5602015-09-02 14:54:11 -0700290 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index),
291 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000292
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100293 bool requires_ctor_barrier = false;
294
295 if (dex_compilation_unit.IsConstructor()) {
296 // If it's a super invocation and we already generate a barrier there's no need
297 // to generate another one.
298 // We identify super calls by looking at the "this" pointer. If its value is the
299 // same as the local "this" pointer then we must have a super invocation.
300 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
301 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
302 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
303 requires_ctor_barrier = false;
304 } else {
305 Thread* self = Thread::Current();
306 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
307 dex_compilation_unit.GetDexFile(),
308 dex_compilation_unit.GetClassDefIndex());
309 }
310 }
311
Nicolas Geoffray35071052015-06-09 15:43:38 +0100312 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
313 if (invoke_type == kInterface) {
314 // We have statically resolved the dispatch. To please the class linker
315 // at runtime, we change this call as if it was a virtual call.
316 invoke_type = kVirtual;
317 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000318 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100320 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100322 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100324 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100325 graph_->IsDebuggable(),
326 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000327
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000329 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000330 &dex_compilation_unit,
331 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000332 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000333 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000334 &inline_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700335 resolved_method->GetQuickenedInfo(),
336 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000337
David Brazdil5e8b1372015-01-23 14:39:08 +0000338 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100339 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000340 << " could not be built, so cannot be inlined";
341 return false;
342 }
343
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000344 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
345 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100346 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000347 << " cannot be inlined because of the register allocator";
348 return false;
349 }
350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100352 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353 << " could not be transformed to SSA";
354 return false;
355 }
356
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700357 size_t parameter_index = 0;
358 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
359 !instructions.Done();
360 instructions.Advance()) {
361 HInstruction* current = instructions.Current();
362 if (current->IsParameterValue()) {
363 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
364 if (argument->IsNullConstant()) {
365 current->ReplaceWith(callee_graph->GetNullConstant());
366 } else if (argument->IsIntConstant()) {
367 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
368 } else if (argument->IsLongConstant()) {
369 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
370 } else if (argument->IsFloatConstant()) {
371 current->ReplaceWith(
372 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
373 } else if (argument->IsDoubleConstant()) {
374 current->ReplaceWith(
375 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
376 } else if (argument->GetType() == Primitive::kPrimNot) {
377 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
378 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
379 }
380 }
381 }
382
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000383 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100384 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000385 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100386 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000387 InstructionSimplifier simplify(callee_graph, stats_);
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100388 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000389
390 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100391 &intrinsics,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100392 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000393 &simplify,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700394 &dce,
395 &fold,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000396 };
397
398 for (size_t i = 0; i < arraysize(optimizations); ++i) {
399 HOptimization* optimization = optimizations[i];
400 optimization->Run();
401 }
402
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700403 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Calin Juravleec748352015-07-29 13:52:12 +0100404 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000405 HInliner inliner(callee_graph,
406 outer_compilation_unit_,
407 dex_compilation_unit,
408 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100409 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000410 stats_,
411 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000412 inliner.Run();
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700413 number_of_instructions_budget += inliner.number_of_inlined_instructions_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000414 }
415
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100416 // TODO: We should abort only if all predecessors throw. However,
417 // HGraph::InlineInto currently does not handle an exit block with
418 // a throw predecessor.
419 HBasicBlock* exit_block = callee_graph->GetExitBlock();
420 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100421 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100422 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100423 return false;
424 }
425
426 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +0000427 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
428 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 has_throw_predecessor = true;
430 break;
431 }
432 }
433 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100434 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 return false;
437 }
438
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000440 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700441 size_t number_of_instructions = 0;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 for (; !it.Done(); it.Advance()) {
443 HBasicBlock* block = it.Current();
444 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100445 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 << " could not be inlined because it contains a loop";
447 return false;
448 }
449
450 for (HInstructionIterator instr_it(block->GetInstructions());
451 !instr_it.Done();
452 instr_it.Advance()) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700453 if (number_of_instructions++ == number_of_instructions_budget) {
454 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
455 << " could not be inlined because it is too big.";
456 return false;
457 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000458 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000459
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100460 if (current->IsInvokeInterface()) {
461 // Disable inlining of interface calls. The cost in case of entering the
462 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100463 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 << " could not be inlined because it has an interface call.";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000465 return false;
466 }
467
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100468 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100469 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000470 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100471 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000472 return false;
473 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000474
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100475 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100476 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000477 << " could not be inlined because " << current->DebugName()
478 << " it is in a different dex file and requires access to the dex cache";
479 return false;
480 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000481 }
482 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700483 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000484
Calin Juravle2e768302015-07-28 14:41:11 +0000485 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
486
487 // When merging the graph we might create a new NullConstant in the caller graph which does
488 // not have the chance to be typed. We assign the correct type here so that we can keep the
489 // assertion that every reference has a valid type. This also simplifies checks along the way.
490 HNullConstant* null_constant = graph_->GetNullConstant();
491 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
492 ReferenceTypeInfo::TypeHandle obj_handle =
493 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
494 null_constant->SetReferenceTypeInfo(
495 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
496 }
497
498 if ((return_replacement != nullptr)
499 && (return_replacement->GetType() == Primitive::kPrimNot)) {
500 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
501 // Make sure that we have a valid type for the return. We may get an invalid one when
502 // we inline invokes with multiple branches and create a Phi for the result.
503 // TODO: we could be more precise by merging the phi inputs but that requires
504 // some functionality from the reference type propagation.
505 DCHECK(return_replacement->IsPhi());
Vladimir Marko05792b92015-08-03 11:56:49 +0100506 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Calin Juravle2e768302015-07-28 14:41:11 +0000507 ReferenceTypeInfo::TypeHandle return_handle =
Vladimir Marko05792b92015-08-03 11:56:49 +0100508 handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size));
Calin Juravle2e768302015-07-28 14:41:11 +0000509 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
David Brazdilbaf89b82015-09-15 11:36:54 +0100510 return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */));
Calin Juravle2e768302015-07-28 14:41:11 +0000511 }
512 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000513
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000514 return true;
515}
516
517} // namespace art