blob: 498739caf3c11f9be0b931ae1ef0651b53ab0b3b [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"
Vladimir Markobe10e8e2016-01-22 12:09:44 +000024#include "dex/verified_method.h"
25#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "driver/compiler_driver-inl.h"
Calin Juravleec748352015-07-29 13:52:12 +010027#include "driver/compiler_options.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028#include "driver/dex_compilation_unit.h"
29#include "instruction_simplifier.h"
Scott Wakelingd60a1af2015-07-22 14:32:44 +010030#include "intrinsics.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "mirror/class_loader.h"
32#include "mirror/dex_cache.h"
33#include "nodes.h"
Nicolas Geoffray335005e2015-06-25 10:01:47 +010034#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010035#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000036#include "register_allocator.h"
Vladimir Markobe10e8e2016-01-22 12:09:44 +000037#include "quick/inline_method_analyser.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010038#include "sharpening.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000039#include "ssa_builder.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040#include "ssa_phi_elimination.h"
41#include "scoped_thread_state_change.h"
42#include "thread.h"
43
44namespace art {
45
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000046static constexpr size_t kMaximumNumberOfHInstructions = 32;
47
48// Limit the number of dex registers that we accumulate while inlining
49// to avoid creating large amount of nested environments.
50static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64;
51
52// Avoid inlining within a huge method due to memory pressure.
53static constexpr size_t kMaximumCodeUnitSize = 4096;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070054
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055void HInliner::Run() {
Calin Juravle8f96df82015-07-29 15:58:48 +010056 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
57 if ((compiler_options.GetInlineDepthLimit() == 0)
58 || (compiler_options.GetInlineMaxCodeUnits() == 0)) {
59 return;
60 }
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000061 if (caller_compilation_unit_.GetCodeItem()->insns_size_in_code_units_ > kMaximumCodeUnitSize) {
62 return;
63 }
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000064 if (graph_->IsDebuggable()) {
65 // For simplicity, we currently never inline when the graph is debuggable. This avoids
66 // doing some logic in the runtime to discover if a method could have been inlined.
67 return;
68 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010069 const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
70 DCHECK(!blocks.empty());
71 HBasicBlock* next_block = blocks[0];
72 for (size_t i = 0; i < blocks.size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010073 // Because we are changing the graph when inlining, we need to remember the next block.
74 // This avoids doing the inlining work again on the inlined blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010075 if (blocks[i] != next_block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010076 continue;
77 }
78 HBasicBlock* block = next_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +010079 next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1];
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000080 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
81 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010082 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070083 // As long as the call is not intrinsified, it is worth trying to inline.
84 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000085 // We use the original invoke type to ensure the resolution of the called method
86 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000087 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010088 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000089 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000090 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 bool should_inline = callee_name.find("$inline$") != std::string::npos;
92 CHECK(!should_inline) << "Could not inline " << callee_name;
93 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010094 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010095 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010096 std::string callee_name =
97 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
98 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
99 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
100 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 }
102 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000103 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000104 }
105 }
106}
107
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100108static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700109 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100110 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
111}
112
113/**
114 * Given the `resolved_method` looked up in the dex cache, try to find
115 * the actual runtime target of an interface or virtual call.
116 * Return nullptr if the runtime target cannot be proven.
117 */
118static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100120 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
121 // No need to lookup further, the resolved method will be the target.
122 return resolved_method;
123 }
124
125 HInstruction* receiver = invoke->InputAt(0);
126 if (receiver->IsNullCheck()) {
127 // Due to multiple levels of inlining within the same pass, it might be that
128 // null check does not have the reference type of the actual receiver.
129 receiver = receiver->InputAt(0);
130 }
131 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000132 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
133 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100134 // We currently only support inlining with known receivers.
135 // TODO: Remove this check, we should be able to inline final methods
136 // on unknown receivers.
137 return nullptr;
138 } else if (info.GetTypeHandle()->IsInterface()) {
139 // Statically knowing that the receiver has an interface type cannot
140 // help us find what is the target method.
141 return nullptr;
142 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
143 // The method that we're trying to call is not in the receiver's class or super classes.
144 return nullptr;
145 }
146
147 ClassLinker* cl = Runtime::Current()->GetClassLinker();
148 size_t pointer_size = cl->GetImagePointerSize();
149 if (invoke->IsInvokeInterface()) {
150 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
151 resolved_method, pointer_size);
152 } else {
153 DCHECK(invoke->IsInvokeVirtual());
154 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
155 resolved_method, pointer_size);
156 }
157
158 if (resolved_method == nullptr) {
159 // The information we had on the receiver was not enough to find
160 // the target method. Since we check above the exact type of the receiver,
161 // the only reason this can happen is an IncompatibleClassChangeError.
162 return nullptr;
Alex Light9139e002015-10-09 15:59:48 -0700163 } else if (!resolved_method->IsInvokable()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100164 // The information we had on the receiver was not enough to find
165 // the target method. Since we check above the exact type of the receiver,
166 // the only reason this can happen is an IncompatibleClassChangeError.
167 return nullptr;
168 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
169 // A final method has to be the target method.
170 return resolved_method;
171 } else if (info.IsExact()) {
172 // If we found a method and the receiver's concrete type is statically
173 // known, we know for sure the target.
174 return resolved_method;
175 } else {
176 // Even if we did find a method, the receiver type was not enough to
177 // statically find the runtime target.
178 return nullptr;
179 }
180}
181
182static uint32_t FindMethodIndexIn(ArtMethod* method,
183 const DexFile& dex_file,
184 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700185 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100186 if (IsSameDexFile(*method->GetDexFile(), dex_file)) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100187 return method->GetDexMethodIndex();
188 } else {
189 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
190 }
191}
192
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100193static uint32_t FindClassIndexIn(mirror::Class* cls, const DexFile& dex_file)
194 SHARED_REQUIRES(Locks::mutator_lock_) {
195 if (cls->GetDexCache() == nullptr) {
196 DCHECK(cls->IsArrayClass());
197 // TODO: find the class in `dex_file`.
198 return DexFile::kDexNoIndex;
199 } else if (cls->GetDexTypeIndex() == DexFile::kDexNoIndex16) {
200 // TODO: deal with proxy classes.
201 return DexFile::kDexNoIndex;
202 } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) {
203 // Update the dex cache to ensure the class is in. The generated code will
204 // consider it is. We make it safe by updating the dex cache, as other
205 // dex files might also load the class, and there is no guarantee the dex
206 // cache of the dex file of the class will be updated.
207 if (cls->GetDexCache()->GetResolvedType(cls->GetDexTypeIndex()) == nullptr) {
208 cls->GetDexCache()->SetResolvedType(cls->GetDexTypeIndex(), cls);
209 }
210 return cls->GetDexTypeIndex();
211 } else {
212 // TODO: find the class in `dex_file`.
213 return DexFile::kDexNoIndex;
214 }
215}
216
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700217bool HInliner::TryInline(HInvoke* invoke_instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +0100218 if (invoke_instruction->IsInvokeUnresolved()) {
219 return false; // Don't bother to move further if we know the method is unresolved.
220 }
221
Vladimir Marko58155012015-08-19 12:49:41 +0000222 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000224 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
225 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226
Nicolas Geoffray35071052015-06-09 15:43:38 +0100227 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
228 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000229 ArtMethod* resolved_method;
Andreas Gampefd2140f2015-12-23 16:30:44 -0800230 ArtMethod* actual_method = nullptr;
Vladimir Marko58155012015-08-19 12:49:41 +0000231 if (invoke_instruction->IsInvokeStaticOrDirect()) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000232 if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) {
233 VLOG(compiler) << "Not inlining a String.<init> method";
234 return false;
235 }
Vladimir Marko58155012015-08-19 12:49:41 +0000236 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700237 mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file)
238 ? caller_compilation_unit_.GetDexCache().Get()
239 : class_linker->FindDexCache(soa.Self(), *ref.dex_file);
240 resolved_method = dex_cache->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000241 ref.dex_method_index, class_linker->GetImagePointerSize());
Andreas Gampefd2140f2015-12-23 16:30:44 -0800242 // actual_method == resolved_method for direct or static calls.
243 actual_method = resolved_method;
Vladimir Marko58155012015-08-19 12:49:41 +0000244 } else {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700245 resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000246 method_index, class_linker->GetImagePointerSize());
Andreas Gampefd2140f2015-12-23 16:30:44 -0800247 if (resolved_method != nullptr) {
248 // Check if we can statically find the method.
249 actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
250 }
Vladimir Marko58155012015-08-19 12:49:41 +0000251 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 if (resolved_method == nullptr) {
Calin Juravle175dc732015-08-25 15:42:32 +0100254 // TODO: Can this still happen?
Nicolas Geoffray35071052015-06-09 15:43:38 +0100255 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000256 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 return false;
258 }
259
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100260 if (actual_method != nullptr) {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000261 return TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ true);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100262 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000263
Andreas Gampefd2140f2015-12-23 16:30:44 -0800264 DCHECK(!invoke_instruction->IsInvokeStaticOrDirect());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100265
266 // Check if we can use an inline cache.
267 ArtMethod* caller = graph_->GetArtMethod();
268 size_t pointer_size = class_linker->GetImagePointerSize();
269 // Under JIT, we should always know the caller.
270 DCHECK(!Runtime::Current()->UseJit() || (caller != nullptr));
271 if (caller != nullptr && caller->GetProfilingInfo(pointer_size) != nullptr) {
272 ProfilingInfo* profiling_info = caller->GetProfilingInfo(pointer_size);
273 const InlineCache& ic = *profiling_info->GetInlineCache(invoke_instruction->GetDexPc());
274 if (ic.IsUnitialized()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100275 VLOG(compiler) << "Interface or virtual call to "
276 << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100277 << " is not hit and not inlined";
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100278 return false;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100279 } else if (ic.IsMonomorphic()) {
280 MaybeRecordStat(kMonomorphicCall);
281 return TryInlineMonomorphicCall(invoke_instruction, resolved_method, ic);
282 } else if (ic.IsPolymorphic()) {
283 MaybeRecordStat(kPolymorphicCall);
284 return TryInlinePolymorphicCall(invoke_instruction, resolved_method, ic);
285 } else {
286 DCHECK(ic.IsMegamorphic());
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100287 VLOG(compiler) << "Interface or virtual call to "
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100288 << PrettyMethod(method_index, caller_dex_file)
289 << " is megamorphic and not inlined";
290 MaybeRecordStat(kMegamorphicCall);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100291 return false;
292 }
293 }
294
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100295 VLOG(compiler) << "Interface or virtual call to "
296 << PrettyMethod(method_index, caller_dex_file)
297 << " could not be statically determined";
298 return false;
299}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000300
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000301HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker,
302 HInstruction* receiver,
303 uint32_t dex_pc) const {
304 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
305 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
306 return new (graph_->GetArena()) HInstanceFieldGet(
307 receiver,
308 Primitive::kPrimNot,
309 field->GetOffset(),
310 field->IsVolatile(),
311 field->GetDexFieldIndex(),
312 field->GetDeclaringClass()->GetDexClassDefIndex(),
313 *field->GetDexFile(),
314 handles_->NewHandle(field->GetDexCache()),
315 dex_pc);
316}
317
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100318bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction,
319 ArtMethod* resolved_method,
320 const InlineCache& ic) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000321 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
322 << invoke_instruction->DebugName();
323
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100324 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
325 uint32_t class_index = FindClassIndexIn(ic.GetMonomorphicType(), caller_dex_file);
326 if (class_index == DexFile::kDexNoIndex) {
327 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
328 << " from inline cache is not inlined because its class is not"
329 << " accessible to the caller";
330 return false;
331 }
332
333 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
334 size_t pointer_size = class_linker->GetImagePointerSize();
335 if (invoke_instruction->IsInvokeInterface()) {
336 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForInterface(
337 resolved_method, pointer_size);
338 } else {
339 DCHECK(invoke_instruction->IsInvokeVirtual());
340 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForVirtual(
341 resolved_method, pointer_size);
342 }
343 DCHECK(resolved_method != nullptr);
344 HInstruction* receiver = invoke_instruction->InputAt(0);
345 HInstruction* cursor = invoke_instruction->GetPrevious();
346 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
347
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000348 if (!TryInlineAndReplace(invoke_instruction, resolved_method, /* do_rtp */ false)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100349 return false;
350 }
351
352 // We successfully inlined, now add a guard.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100353 bool is_referrer =
354 (ic.GetMonomorphicType() == outermost_graph_->GetArtMethod()->GetDeclaringClass());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000355 AddTypeGuard(receiver,
356 cursor,
357 bb_cursor,
358 class_index,
359 is_referrer,
360 invoke_instruction,
361 /* with_deoptimization */ true);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100362
363 // Run type propagation to get the guard typed, and eventually propagate the
364 // type of the receiver.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000365 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100366 rtp_fixup.Run();
367
368 MaybeRecordStat(kInlinedMonomorphicCall);
369 return true;
370}
371
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000372HInstruction* HInliner::AddTypeGuard(HInstruction* receiver,
373 HInstruction* cursor,
374 HBasicBlock* bb_cursor,
375 uint32_t class_index,
376 bool is_referrer,
377 HInstruction* invoke_instruction,
378 bool with_deoptimization) {
379 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
380 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
381 class_linker, receiver, invoke_instruction->GetDexPc());
382
383 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
384 // Note that we will just compare the classes, so we don't need Java semantics access checks.
385 // Also, the caller of `AddTypeGuard` must have guaranteed that the class is in the dex cache.
386 HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(),
387 class_index,
388 caller_dex_file,
389 is_referrer,
390 invoke_instruction->GetDexPc(),
391 /* needs_access_check */ false,
392 /* is_in_dex_cache */ true);
393
394 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class);
395 // TODO: Extend reference type propagation to understand the guard.
396 if (cursor != nullptr) {
397 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
398 } else {
399 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
400 }
401 bb_cursor->InsertInstructionAfter(load_class, receiver_class);
402 bb_cursor->InsertInstructionAfter(compare, load_class);
403 if (with_deoptimization) {
404 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
405 compare, invoke_instruction->GetDexPc());
406 bb_cursor->InsertInstructionAfter(deoptimize, compare);
407 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
408 }
409 return compare;
410}
411
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000412bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100413 ArtMethod* resolved_method,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000414 const InlineCache& ic) {
415 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
416 << invoke_instruction->DebugName();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000417
418 if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, ic)) {
419 return true;
420 }
421
422 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
423 size_t pointer_size = class_linker->GetImagePointerSize();
424 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
425
426 bool all_targets_inlined = true;
427 bool one_target_inlined = false;
428 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
429 if (ic.GetTypeAt(i) == nullptr) {
430 break;
431 }
432 ArtMethod* method = nullptr;
433 if (invoke_instruction->IsInvokeInterface()) {
434 method = ic.GetTypeAt(i)->FindVirtualMethodForInterface(
435 resolved_method, pointer_size);
436 } else {
437 DCHECK(invoke_instruction->IsInvokeVirtual());
438 method = ic.GetTypeAt(i)->FindVirtualMethodForVirtual(
439 resolved_method, pointer_size);
440 }
441
442 HInstruction* receiver = invoke_instruction->InputAt(0);
443 HInstruction* cursor = invoke_instruction->GetPrevious();
444 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
445
446 uint32_t class_index = FindClassIndexIn(ic.GetTypeAt(i), caller_dex_file);
447 HInstruction* return_replacement = nullptr;
448 if (class_index == DexFile::kDexNoIndex ||
449 !TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
450 all_targets_inlined = false;
451 } else {
452 one_target_inlined = true;
453 bool is_referrer = (ic.GetTypeAt(i) == outermost_graph_->GetArtMethod()->GetDeclaringClass());
454
455 // If we have inlined all targets before, and this receiver is the last seen,
456 // we deoptimize instead of keeping the original invoke instruction.
457 bool deoptimize = all_targets_inlined &&
458 (i != InlineCache::kIndividualCacheSize - 1) &&
459 (ic.GetTypeAt(i + 1) == nullptr);
460 HInstruction* compare = AddTypeGuard(
461 receiver, cursor, bb_cursor, class_index, is_referrer, invoke_instruction, deoptimize);
462 if (deoptimize) {
463 if (return_replacement != nullptr) {
464 invoke_instruction->ReplaceWith(return_replacement);
465 }
466 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
467 // Because the inline cache data can be populated concurrently, we force the end of the
468 // iteration. Otherhwise, we could see a new receiver type.
469 break;
470 } else {
471 CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction);
472 }
473 }
474 }
475
476 if (!one_target_inlined) {
477 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
478 << " from inline cache is not inlined because none"
479 << " of its targets could be inlined";
480 return false;
481 }
482 MaybeRecordStat(kInlinedPolymorphicCall);
483
484 // Run type propagation to get the guards typed.
485 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
486 rtp_fixup.Run();
487 return true;
488}
489
490void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
491 HInstruction* return_replacement,
492 HInstruction* invoke_instruction) {
493 uint32_t dex_pc = invoke_instruction->GetDexPc();
494 HBasicBlock* cursor_block = compare->GetBlock();
495 HBasicBlock* original_invoke_block = invoke_instruction->GetBlock();
496 ArenaAllocator* allocator = graph_->GetArena();
497
498 // Spit the block after the compare: `cursor_block` will now be the start of the diamond,
499 // and the returned block is the start of the then branch (that could contain multiple blocks).
500 HBasicBlock* then = cursor_block->SplitAfterForInlining(compare);
501
502 // Split the block containing the invoke before and after the invoke. The returned block
503 // of the split before will contain the invoke and will be the otherwise branch of
504 // the diamond. The returned block of the split after will be the merge block
505 // of the diamond.
506 HBasicBlock* end_then = invoke_instruction->GetBlock();
507 HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction);
508 HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction);
509
510 // If the methods we are inlining return a value, we create a phi in the merge block
511 // that will have the `invoke_instruction and the `return_replacement` as inputs.
512 if (return_replacement != nullptr) {
513 HPhi* phi = new (allocator) HPhi(
514 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc);
515 merge->AddPhi(phi);
516 invoke_instruction->ReplaceWith(phi);
517 phi->AddInput(return_replacement);
518 phi->AddInput(invoke_instruction);
519 }
520
521 // Add the control flow instructions.
522 otherwise->AddInstruction(new (allocator) HGoto(dex_pc));
523 end_then->AddInstruction(new (allocator) HGoto(dex_pc));
524 cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc));
525
526 // Add the newly created blocks to the graph.
527 graph_->AddBlock(then);
528 graph_->AddBlock(otherwise);
529 graph_->AddBlock(merge);
530
531 // Set up successor (and implictly predecessor) relations.
532 cursor_block->AddSuccessor(otherwise);
533 cursor_block->AddSuccessor(then);
534 end_then->AddSuccessor(merge);
535 otherwise->AddSuccessor(merge);
536
537 // Set up dominance information.
538 then->SetDominator(cursor_block);
539 cursor_block->AddDominatedBlock(then);
540 otherwise->SetDominator(cursor_block);
541 cursor_block->AddDominatedBlock(otherwise);
542 merge->SetDominator(cursor_block);
543 cursor_block->AddDominatedBlock(merge);
544
545 // Update the revert post order.
546 size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block);
547 MakeRoomFor(&graph_->reverse_post_order_, 1, index);
548 graph_->reverse_post_order_[++index] = then;
549 index = IndexOfElement(graph_->reverse_post_order_, end_then);
550 MakeRoomFor(&graph_->reverse_post_order_, 2, index);
551 graph_->reverse_post_order_[++index] = otherwise;
552 graph_->reverse_post_order_[++index] = merge;
553
554 // Set the loop information of the newly created blocks.
555 HLoopInformation* loop_info = cursor_block->GetLoopInformation();
556 if (loop_info != nullptr) {
557 then->SetLoopInformation(cursor_block->GetLoopInformation());
558 merge->SetLoopInformation(cursor_block->GetLoopInformation());
559 otherwise->SetLoopInformation(cursor_block->GetLoopInformation());
560 for (HLoopInformationOutwardIterator loop_it(*cursor_block);
561 !loop_it.Done();
562 loop_it.Advance()) {
563 loop_it.Current()->Add(then);
564 loop_it.Current()->Add(merge);
565 loop_it.Current()->Add(otherwise);
566 }
567 // In case the original invoke location was a back edge, we need to update
568 // the loop to now have the merge block as a back edge.
569 if (loop_info->IsBackEdge(*original_invoke_block)) {
570 loop_info->RemoveBackEdge(original_invoke_block);
571 loop_info->AddBackEdge(merge);
572 }
573 }
574
575 // Set the try/catch information of the newly created blocks.
576 then->SetTryCatchInformation(cursor_block->GetTryCatchInformation());
577 merge->SetTryCatchInformation(cursor_block->GetTryCatchInformation());
578 otherwise->SetTryCatchInformation(cursor_block->GetTryCatchInformation());
579}
580
581bool HInliner::TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
582 ArtMethod* resolved_method,
583 const InlineCache& ic) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000584 // This optimization only works under JIT for now.
585 DCHECK(Runtime::Current()->UseJit());
Roland Levillain2aba7cd2016-02-03 12:27:20 +0000586 if (graph_->GetInstructionSet() == kMips64) {
587 // TODO: Support HClassTableGet for mips64.
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000588 return false;
589 }
590 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
591 size_t pointer_size = class_linker->GetImagePointerSize();
592
593 DCHECK(resolved_method != nullptr);
594 ArtMethod* actual_method = nullptr;
595 // Check whether we are actually calling the same method among
596 // the different types seen.
597 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
598 if (ic.GetTypeAt(i) == nullptr) {
599 break;
600 }
601 ArtMethod* new_method = nullptr;
602 if (invoke_instruction->IsInvokeInterface()) {
603 new_method = ic.GetTypeAt(i)->FindVirtualMethodForInterface(
604 resolved_method, pointer_size);
605 } else {
606 DCHECK(invoke_instruction->IsInvokeVirtual());
607 new_method = ic.GetTypeAt(i)->FindVirtualMethodForVirtual(
608 resolved_method, pointer_size);
609 }
610 if (actual_method == nullptr) {
611 actual_method = new_method;
612 } else if (actual_method != new_method) {
613 // Different methods, bailout.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000614 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
615 << " from inline cache is not inlined because it resolves"
616 << " to different methods";
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000617 return false;
618 }
619 }
620
621 HInstruction* receiver = invoke_instruction->InputAt(0);
622 HInstruction* cursor = invoke_instruction->GetPrevious();
623 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
624
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000625 if (!TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ false)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000626 return false;
627 }
628
629 // We successfully inlined, now add a guard.
630 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
631 class_linker, receiver, invoke_instruction->GetDexPc());
632
633 size_t method_offset = invoke_instruction->IsInvokeVirtual()
634 ? actual_method->GetVtableIndex()
635 : invoke_instruction->AsInvokeInterface()->GetImtIndex();
636
637 Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet())
638 ? Primitive::kPrimLong
639 : Primitive::kPrimInt;
640 HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet(
641 receiver_class,
642 type,
643 invoke_instruction->IsInvokeVirtual() ? HClassTableGet::kVTable : HClassTableGet::kIMTable,
644 method_offset,
645 invoke_instruction->GetDexPc());
646
647 HConstant* constant;
648 if (type == Primitive::kPrimLong) {
649 constant = graph_->GetLongConstant(
650 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
651 } else {
652 constant = graph_->GetIntConstant(
653 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
654 }
655
656 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant);
657 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
658 compare, invoke_instruction->GetDexPc());
659 // TODO: Extend reference type propagation to understand the guard.
660 if (cursor != nullptr) {
661 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
662 } else {
663 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
664 }
665 bb_cursor->InsertInstructionAfter(class_table_get, receiver_class);
666 bb_cursor->InsertInstructionAfter(compare, class_table_get);
667 bb_cursor->InsertInstructionAfter(deoptimize, compare);
668 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
669
670 // Run type propagation to get the guard typed.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000671 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000672 rtp_fixup.Run();
673
674 MaybeRecordStat(kInlinedPolymorphicCall);
675
676 return true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100677}
678
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000679bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) {
680 HInstruction* return_replacement = nullptr;
681 if (!TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
682 return false;
683 }
684 if (return_replacement != nullptr) {
685 invoke_instruction->ReplaceWith(return_replacement);
686 }
687 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
688 FixUpReturnReferenceType(invoke_instruction, method, return_replacement, do_rtp);
689 return true;
690}
691
692bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction,
693 ArtMethod* method,
694 HInstruction** return_replacement) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100695 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800696
697 // Check whether we're allowed to inline. The outermost compilation unit is the relevant
698 // dex file here (though the transitivity of an inline chain would allow checking the calller).
699 if (!compiler_driver_->MayInline(method->GetDexFile(),
700 outer_compilation_unit_.GetDexFile())) {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000701 if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000702 VLOG(compiler) << "Successfully replaced pattern of invoke " << PrettyMethod(method);
703 MaybeRecordStat(kReplacedInvokeWithSimplePattern);
704 return true;
705 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800706 VLOG(compiler) << "Won't inline " << PrettyMethod(method) << " in "
707 << outer_compilation_unit_.GetDexFile()->GetLocation() << " ("
708 << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from "
709 << method->GetDexFile()->GetLocation();
710 return false;
711 }
712
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100713 uint32_t method_index = FindMethodIndexIn(
714 method, caller_dex_file, invoke_instruction->GetDexMethodIndex());
715 if (method_index == DexFile::kDexNoIndex) {
716 VLOG(compiler) << "Call to "
717 << PrettyMethod(method)
718 << " cannot be inlined because unaccessible to caller";
719 return false;
720 }
721
722 bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile());
723
724 const DexFile::CodeItem* code_item = method->GetCodeItem();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000725
726 if (code_item == nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100727 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000728 << " is not inlined because it is native";
729 return false;
730 }
731
Calin Juravleec748352015-07-29 13:52:12 +0100732 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
733 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100734 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000735 << " is too big to inline: "
736 << code_item->insns_size_in_code_units_
737 << " > "
738 << inline_max_code_units;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000739 return false;
740 }
741
742 if (code_item->tries_size_ != 0) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100743 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000744 << " is not inlined because of try block";
745 return false;
746 }
747
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100748 if (!method->GetDeclaringClass()->IsVerified()) {
749 uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex();
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100750 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100751 method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100752 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
753 << " couldn't be verified, so it cannot be inlined";
754 return false;
755 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000756 }
757
Roland Levillain4c0eb422015-04-24 16:43:49 +0100758 if (invoke_instruction->IsInvokeStaticOrDirect() &&
759 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
760 // Case of a static method that cannot be inlined because it implicitly
761 // requires an initialization check of its declaring class.
762 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
763 << " is not inlined because it is static and requires a clinit"
764 << " check that cannot be emitted due to Dex cache limitations";
765 return false;
766 }
767
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000768 if (!TryBuildAndInlineHelper(invoke_instruction, method, same_dex_file, return_replacement)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000769 return false;
770 }
771
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000772 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000773 MaybeRecordStat(kInlinedInvoke);
774 return true;
775}
776
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000777static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction,
778 size_t arg_vreg_index)
779 SHARED_REQUIRES(Locks::mutator_lock_) {
780 size_t input_index = 0;
781 for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) {
782 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
783 if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) {
784 ++i;
785 DCHECK_NE(i, arg_vreg_index);
786 }
787 }
788 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
789 return invoke_instruction->InputAt(input_index);
790}
791
792// Try to recognize known simple patterns and replace invoke call with appropriate instructions.
793bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction,
794 ArtMethod* resolved_method,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000795 HInstruction** return_replacement) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000796 InlineMethod inline_method;
797 if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) {
798 return false;
799 }
800
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000801 switch (inline_method.opcode) {
802 case kInlineOpNop:
803 DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000804 *return_replacement = nullptr;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000805 break;
806 case kInlineOpReturnArg:
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000807 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction,
808 inline_method.d.return_data.arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000809 break;
810 case kInlineOpNonWideConst:
811 if (resolved_method->GetShorty()[0] == 'L') {
812 DCHECK_EQ(inline_method.d.data, 0u);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000813 *return_replacement = graph_->GetNullConstant();
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000814 } else {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000815 *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000816 }
817 break;
818 case kInlineOpIGet: {
819 const InlineIGetIPutData& data = inline_method.d.ifield_data;
820 if (data.method_is_static || data.object_arg != 0u) {
821 // TODO: Needs null check.
822 return false;
823 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000824 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000825 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000826 HInstanceFieldGet* iget = CreateInstanceFieldGet(dex_cache, data.field_idx, obj);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000827 DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset);
828 DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile);
829 invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000830 *return_replacement = iget;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000831 break;
832 }
833 case kInlineOpIPut: {
834 const InlineIGetIPutData& data = inline_method.d.ifield_data;
835 if (data.method_is_static || data.object_arg != 0u) {
836 // TODO: Needs null check.
837 return false;
838 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000839 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000840 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
841 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000842 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, data.field_idx, obj, value);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000843 DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset);
844 DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile);
845 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
846 if (data.return_arg_plus1 != 0u) {
847 size_t return_arg = data.return_arg_plus1 - 1u;
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000848 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000849 }
850 break;
851 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000852 case kInlineOpConstructor: {
853 const InlineConstructorData& data = inline_method.d.constructor_data;
854 // Get the indexes to arrays for easier processing.
855 uint16_t iput_field_indexes[] = {
856 data.iput0_field_index, data.iput1_field_index, data.iput2_field_index
857 };
858 uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg };
859 static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch");
860 // Count valid field indexes.
861 size_t number_of_iputs = 0u;
862 while (number_of_iputs != arraysize(iput_field_indexes) &&
863 iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) {
864 // Check that there are no duplicate valid field indexes.
865 DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1,
866 iput_field_indexes + arraysize(iput_field_indexes),
867 iput_field_indexes[number_of_iputs]));
868 ++number_of_iputs;
869 }
870 // Check that there are no valid field indexes in the rest of the array.
871 DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs,
872 iput_field_indexes + arraysize(iput_field_indexes),
873 [](uint16_t index) { return index != DexFile::kDexNoIndex16; }));
874
875 // Create HInstanceFieldSet for each IPUT that stores non-zero data.
876 Handle<mirror::DexCache> dex_cache;
877 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u);
878 bool needs_constructor_barrier = false;
879 for (size_t i = 0; i != number_of_iputs; ++i) {
880 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]);
881 if (!value->IsConstant() ||
882 (!value->AsConstant()->IsZero() && !value->IsNullConstant())) {
883 if (dex_cache.GetReference() == nullptr) {
884 dex_cache = handles_->NewHandle(resolved_method->GetDexCache());
885 }
886 uint16_t field_index = iput_field_indexes[i];
887 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, field_index, obj, value);
888 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
889
890 // Check whether the field is final. If it is, we need to add a barrier.
891 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
892 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
893 DCHECK(resolved_field != nullptr);
894 if (resolved_field->IsFinal()) {
895 needs_constructor_barrier = true;
896 }
897 }
898 }
899 if (needs_constructor_barrier) {
900 HMemoryBarrier* barrier = new (graph_->GetArena()) HMemoryBarrier(kStoreStore, kNoDexPc);
901 invoke_instruction->GetBlock()->InsertInstructionBefore(barrier, invoke_instruction);
902 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000903 *return_replacement = nullptr;
Vladimir Marko354efa62016-02-04 19:46:56 +0000904 break;
905 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000906 default:
907 LOG(FATAL) << "UNREACHABLE";
908 UNREACHABLE();
909 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000910 return true;
911}
912
Vladimir Marko354efa62016-02-04 19:46:56 +0000913HInstanceFieldGet* HInliner::CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000914 uint32_t field_index,
915 HInstruction* obj)
916 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000917 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
918 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
919 DCHECK(resolved_field != nullptr);
920 HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet(
921 obj,
922 resolved_field->GetTypeAsPrimitiveType(),
923 resolved_field->GetOffset(),
924 resolved_field->IsVolatile(),
925 field_index,
926 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +0000927 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000928 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +0000929 // Read barrier generates a runtime call in slow path and we need a valid
930 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
931 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000932 if (iget->GetType() == Primitive::kPrimNot) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000933 ReferenceTypePropagation rtp(graph_, handles_, /* is_first_run */ false);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000934 rtp.Visit(iget);
935 }
936 return iget;
937}
938
Vladimir Marko354efa62016-02-04 19:46:56 +0000939HInstanceFieldSet* HInliner::CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000940 uint32_t field_index,
941 HInstruction* obj,
942 HInstruction* value)
943 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000944 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
945 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
946 DCHECK(resolved_field != nullptr);
947 HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet(
948 obj,
949 value,
950 resolved_field->GetTypeAsPrimitiveType(),
951 resolved_field->GetOffset(),
952 resolved_field->IsVolatile(),
953 field_index,
954 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +0000955 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000956 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +0000957 // Read barrier generates a runtime call in slow path and we need a valid
958 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
959 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000960 return iput;
961}
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000962
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000963bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction,
964 ArtMethod* resolved_method,
965 bool same_dex_file,
966 HInstruction** return_replacement) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000967 ScopedObjectAccess soa(Thread::Current());
968 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100969 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
970 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000971 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700972 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000973 DexCompilationUnit dex_compilation_unit(
974 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000975 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000976 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000977 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000978 code_item,
979 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000980 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000981 resolved_method->GetAccessFlags(),
Mathieu Chartier736b5602015-09-02 14:54:11 -0700982 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index),
983 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000984
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100985 bool requires_ctor_barrier = false;
986
987 if (dex_compilation_unit.IsConstructor()) {
988 // If it's a super invocation and we already generate a barrier there's no need
989 // to generate another one.
990 // We identify super calls by looking at the "this" pointer. If its value is the
991 // same as the local "this" pointer then we must have a super invocation.
992 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
993 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
994 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
995 requires_ctor_barrier = false;
996 } else {
997 Thread* self = Thread::Current();
998 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
999 dex_compilation_unit.GetDexFile(),
1000 dex_compilation_unit.GetClassDefIndex());
1001 }
1002 }
1003
Nicolas Geoffray35071052015-06-09 15:43:38 +01001004 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
1005 if (invoke_type == kInterface) {
1006 // We have statically resolved the dispatch. To please the class linker
1007 // at runtime, we change this call as if it was a virtual call.
1008 invoke_type = kVirtual;
1009 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00001010 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001011 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001012 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001013 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +01001014 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001015 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +01001016 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001017 graph_->IsDebuggable(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001018 /* osr */ false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001019 graph_->GetCurrentInstructionId());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001020 callee_graph->SetArtMethod(resolved_method);
David Brazdil5e8b1372015-01-23 14:39:08 +00001021
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001022 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +00001023 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001024 &dex_compilation_unit,
1025 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001026 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001027 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001028 &inline_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001029 resolved_method->GetQuickenedInfo(),
1030 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001031
David Brazdilbadd8262016-02-02 16:28:56 +00001032 if (builder.BuildGraph(*code_item, handles_) != kAnalysisSuccess) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001033 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001034 << " could not be built, so cannot be inlined";
1035 return false;
1036 }
1037
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001038 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
1039 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001040 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001041 << " cannot be inlined because of the register allocator";
1042 return false;
1043 }
1044
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001045 size_t parameter_index = 0;
1046 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
1047 !instructions.Done();
1048 instructions.Advance()) {
1049 HInstruction* current = instructions.Current();
1050 if (current->IsParameterValue()) {
1051 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
1052 if (argument->IsNullConstant()) {
1053 current->ReplaceWith(callee_graph->GetNullConstant());
1054 } else if (argument->IsIntConstant()) {
1055 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
1056 } else if (argument->IsLongConstant()) {
1057 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
1058 } else if (argument->IsFloatConstant()) {
1059 current->ReplaceWith(
1060 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
1061 } else if (argument->IsDoubleConstant()) {
1062 current->ReplaceWith(
1063 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
1064 } else if (argument->GetType() == Primitive::kPrimNot) {
1065 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
1066 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
1067 }
1068 }
1069 }
1070
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001071 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +01001072 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +00001073 HConstantFolding fold(callee_graph);
Vladimir Markodc151b22015-10-15 18:02:30 +01001074 HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_);
Calin Juravleacf735c2015-02-12 15:25:22 +00001075 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +00001076 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001077
1078 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +01001079 &intrinsics,
Vladimir Markodc151b22015-10-15 18:02:30 +01001080 &sharpening,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001081 &simplify,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001082 &fold,
Vladimir Marko9e23df52015-11-10 17:14:35 +00001083 &dce,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001084 };
1085
1086 for (size_t i = 0; i < arraysize(optimizations); ++i) {
1087 HOptimization* optimization = optimizations[i];
1088 optimization->Run();
1089 }
1090
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001091 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Calin Juravleec748352015-07-29 13:52:12 +01001092 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001093 HInliner inliner(callee_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001094 outermost_graph_,
Vladimir Markodc151b22015-10-15 18:02:30 +01001095 codegen_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001096 outer_compilation_unit_,
1097 dex_compilation_unit,
1098 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +01001099 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001100 stats_,
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001101 total_number_of_dex_registers_ + code_item->registers_size_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001102 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001103 inliner.Run();
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001104 number_of_instructions_budget += inliner.number_of_inlined_instructions_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001105 }
1106
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001107 // TODO: We should abort only if all predecessors throw. However,
1108 // HGraph::InlineInto currently does not handle an exit block with
1109 // a throw predecessor.
1110 HBasicBlock* exit_block = callee_graph->GetExitBlock();
1111 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001112 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001113 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001114 return false;
1115 }
1116
1117 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +00001118 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
1119 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001120 has_throw_predecessor = true;
1121 break;
1122 }
1123 }
1124 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001125 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001126 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001127 return false;
1128 }
1129
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001130 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001131 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001132 size_t number_of_instructions = 0;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001133
1134 bool can_inline_environment =
1135 total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters;
1136
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001137 for (; !it.Done(); it.Advance()) {
1138 HBasicBlock* block = it.Current();
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00001139
1140 if (block->IsLoopHeader() && block->GetLoopInformation()->IsIrreducible()) {
1141 // Don't inline methods with irreducible loops, they could prevent some
1142 // optimizations to run.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001143 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00001144 << " could not be inlined because it contains an irreducible loop";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145 return false;
1146 }
1147
1148 for (HInstructionIterator instr_it(block->GetInstructions());
1149 !instr_it.Done();
1150 instr_it.Advance()) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001151 if (number_of_instructions++ == number_of_instructions_budget) {
1152 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001153 << " is not inlined because its caller has reached"
1154 << " its instruction budget limit.";
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001155 return false;
1156 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001157 HInstruction* current = instr_it.Current();
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001158 if (!can_inline_environment && current->NeedsEnvironment()) {
1159 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1160 << " is not inlined because its caller has reached"
1161 << " its environment budget limit.";
1162 return false;
1163 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001165 if (current->IsInvokeInterface()) {
1166 // Disable inlining of interface calls. The cost in case of entering the
1167 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001168 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001169 << " could not be inlined because it has an interface call.";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001170 return false;
1171 }
1172
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001173 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001174 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001175 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001176 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001177 return false;
1178 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001179
Vladimir Markodc151b22015-10-15 18:02:30 +01001180 if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001181 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001182 << " could not be inlined because " << current->DebugName()
1183 << " it is in a different dex file and requires access to the dex cache";
1184 return false;
1185 }
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001186
1187 if (current->IsNewInstance() &&
1188 (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001189 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1190 << " could not be inlined because it is using an entrypoint"
1191 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001192 // Allocation entrypoint does not handle inlined frames.
1193 return false;
1194 }
1195
1196 if (current->IsNewArray() &&
1197 (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001198 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1199 << " could not be inlined because it is using an entrypoint"
1200 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001201 // Allocation entrypoint does not handle inlined frames.
1202 return false;
1203 }
1204
1205 if (current->IsUnresolvedStaticFieldGet() ||
1206 current->IsUnresolvedInstanceFieldGet() ||
1207 current->IsUnresolvedStaticFieldSet() ||
1208 current->IsUnresolvedInstanceFieldSet()) {
1209 // Entrypoint for unresolved fields does not handle inlined frames.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001210 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1211 << " could not be inlined because it is using an unresolved"
1212 << " entrypoint";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001213 return false;
1214 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215 }
1216 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001217 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001218
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001219 *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001220 return true;
1221}
Calin Juravle2e768302015-07-28 14:41:11 +00001222
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001223void HInliner::FixUpReturnReferenceType(HInvoke* invoke_instruction,
1224 ArtMethod* resolved_method,
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001225 HInstruction* return_replacement,
1226 bool do_rtp) {
Alex Light68289a52015-12-15 17:30:30 -08001227 // Check the integrity of reference types and run another type propagation if needed.
David Brazdil4833f5a2015-12-16 10:37:39 +00001228 if (return_replacement != nullptr) {
1229 if (return_replacement->GetType() == Primitive::kPrimNot) {
1230 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
1231 // Make sure that we have a valid type for the return. We may get an invalid one when
1232 // we inline invokes with multiple branches and create a Phi for the result.
1233 // TODO: we could be more precise by merging the phi inputs but that requires
1234 // some functionality from the reference type propagation.
1235 DCHECK(return_replacement->IsPhi());
1236 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1237 ReferenceTypeInfo::TypeHandle return_handle =
1238 handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size));
1239 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
1240 return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */));
1241 }
Alex Light68289a52015-12-15 17:30:30 -08001242
David Brazdil4833f5a2015-12-16 10:37:39 +00001243 if (do_rtp) {
1244 // If the return type is a refinement of the declared type run the type propagation again.
1245 ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo();
1246 ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo();
1247 if (invoke_rti.IsStrictSupertypeOf(return_rti)
1248 || (return_rti.IsExact() && !invoke_rti.IsExact())
1249 || !return_replacement->CanBeNull()) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001250 ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +00001251 }
1252 }
1253 } else if (return_replacement->IsInstanceOf()) {
1254 if (do_rtp) {
1255 // Inlining InstanceOf into an If may put a tighter bound on reference types.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001256 ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001257 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001258 }
Calin Juravle2e768302015-07-28 14:41:11 +00001259 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260}
1261
1262} // namespace art