blob: e14eb334db97c132840855b4de30c4980b923030 [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 Geoffraye4084a52016-02-18 14:43:42 +0000193static uint32_t FindClassIndexIn(mirror::Class* cls,
194 const DexFile& dex_file,
195 Handle<mirror::DexCache> dex_cache)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100196 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000197 uint32_t index = DexFile::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100198 if (cls->GetDexCache() == nullptr) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000199 DCHECK(cls->IsArrayClass()) << PrettyClass(cls);
200 index = cls->FindTypeIndexInOtherDexFile(dex_file);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100201 } else if (cls->GetDexTypeIndex() == DexFile::kDexNoIndex16) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000202 DCHECK(cls->IsProxyClass()) << PrettyClass(cls);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100203 // TODO: deal with proxy classes.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100204 } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000205 index = cls->GetDexTypeIndex();
206 } else {
207 index = cls->FindTypeIndexInOtherDexFile(dex_file);
208 }
209
210 if (index != DexFile::kDexNoIndex) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100211 // Update the dex cache to ensure the class is in. The generated code will
212 // consider it is. We make it safe by updating the dex cache, as other
213 // dex files might also load the class, and there is no guarantee the dex
214 // cache of the dex file of the class will be updated.
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000215 if (dex_cache->GetResolvedType(index) == nullptr) {
216 dex_cache->SetResolvedType(index, cls);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100217 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100218 }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000219
220 return index;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100221}
222
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700223bool HInliner::TryInline(HInvoke* invoke_instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +0100224 if (invoke_instruction->IsInvokeUnresolved()) {
225 return false; // Don't bother to move further if we know the method is unresolved.
226 }
227
Vladimir Marko58155012015-08-19 12:49:41 +0000228 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000230 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
231 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232
Nicolas Geoffray35071052015-06-09 15:43:38 +0100233 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
234 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000235 ArtMethod* resolved_method;
Andreas Gampefd2140f2015-12-23 16:30:44 -0800236 ArtMethod* actual_method = nullptr;
Vladimir Marko58155012015-08-19 12:49:41 +0000237 if (invoke_instruction->IsInvokeStaticOrDirect()) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000238 if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) {
239 VLOG(compiler) << "Not inlining a String.<init> method";
240 return false;
241 }
Vladimir Marko58155012015-08-19 12:49:41 +0000242 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700243 mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file)
244 ? caller_compilation_unit_.GetDexCache().Get()
245 : class_linker->FindDexCache(soa.Self(), *ref.dex_file);
246 resolved_method = dex_cache->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000247 ref.dex_method_index, class_linker->GetImagePointerSize());
Andreas Gampefd2140f2015-12-23 16:30:44 -0800248 // actual_method == resolved_method for direct or static calls.
249 actual_method = resolved_method;
Vladimir Marko58155012015-08-19 12:49:41 +0000250 } else {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700251 resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000252 method_index, class_linker->GetImagePointerSize());
Andreas Gampefd2140f2015-12-23 16:30:44 -0800253 if (resolved_method != nullptr) {
254 // Check if we can statically find the method.
255 actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
256 }
Vladimir Marko58155012015-08-19 12:49:41 +0000257 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000258
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 if (resolved_method == nullptr) {
Calin Juravle175dc732015-08-25 15:42:32 +0100260 // TODO: Can this still happen?
Nicolas Geoffray35071052015-06-09 15:43:38 +0100261 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000262 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000263 return false;
264 }
265
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100266 if (actual_method != nullptr) {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000267 return TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ true);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100268 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000269
Andreas Gampefd2140f2015-12-23 16:30:44 -0800270 DCHECK(!invoke_instruction->IsInvokeStaticOrDirect());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100271
272 // Check if we can use an inline cache.
273 ArtMethod* caller = graph_->GetArtMethod();
274 size_t pointer_size = class_linker->GetImagePointerSize();
275 // Under JIT, we should always know the caller.
276 DCHECK(!Runtime::Current()->UseJit() || (caller != nullptr));
277 if (caller != nullptr && caller->GetProfilingInfo(pointer_size) != nullptr) {
278 ProfilingInfo* profiling_info = caller->GetProfilingInfo(pointer_size);
279 const InlineCache& ic = *profiling_info->GetInlineCache(invoke_instruction->GetDexPc());
280 if (ic.IsUnitialized()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100281 VLOG(compiler) << "Interface or virtual call to "
282 << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100283 << " is not hit and not inlined";
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100284 return false;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100285 } else if (ic.IsMonomorphic()) {
286 MaybeRecordStat(kMonomorphicCall);
287 return TryInlineMonomorphicCall(invoke_instruction, resolved_method, ic);
288 } else if (ic.IsPolymorphic()) {
289 MaybeRecordStat(kPolymorphicCall);
290 return TryInlinePolymorphicCall(invoke_instruction, resolved_method, ic);
291 } else {
292 DCHECK(ic.IsMegamorphic());
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100293 VLOG(compiler) << "Interface or virtual call to "
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100294 << PrettyMethod(method_index, caller_dex_file)
295 << " is megamorphic and not inlined";
296 MaybeRecordStat(kMegamorphicCall);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100297 return false;
298 }
299 }
300
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100301 VLOG(compiler) << "Interface or virtual call to "
302 << PrettyMethod(method_index, caller_dex_file)
303 << " could not be statically determined";
304 return false;
305}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000307HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker,
308 HInstruction* receiver,
309 uint32_t dex_pc) const {
310 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
311 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000312 HInstanceFieldGet* result = new (graph_->GetArena()) HInstanceFieldGet(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000313 receiver,
314 Primitive::kPrimNot,
315 field->GetOffset(),
316 field->IsVolatile(),
317 field->GetDexFieldIndex(),
318 field->GetDeclaringClass()->GetDexClassDefIndex(),
319 *field->GetDexFile(),
320 handles_->NewHandle(field->GetDexCache()),
321 dex_pc);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000322 // The class of a field is effectively final, and does not have any memory dependencies.
323 result->SetSideEffects(SideEffects::None());
324 return result;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000325}
326
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100327bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction,
328 ArtMethod* resolved_method,
329 const InlineCache& ic) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000330 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
331 << invoke_instruction->DebugName();
332
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100333 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000334 uint32_t class_index = FindClassIndexIn(
335 ic.GetMonomorphicType(), caller_dex_file, caller_compilation_unit_.GetDexCache());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100336 if (class_index == DexFile::kDexNoIndex) {
337 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
338 << " from inline cache is not inlined because its class is not"
339 << " accessible to the caller";
340 return false;
341 }
342
343 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
344 size_t pointer_size = class_linker->GetImagePointerSize();
345 if (invoke_instruction->IsInvokeInterface()) {
346 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForInterface(
347 resolved_method, pointer_size);
348 } else {
349 DCHECK(invoke_instruction->IsInvokeVirtual());
350 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForVirtual(
351 resolved_method, pointer_size);
352 }
353 DCHECK(resolved_method != nullptr);
354 HInstruction* receiver = invoke_instruction->InputAt(0);
355 HInstruction* cursor = invoke_instruction->GetPrevious();
356 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
357
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000358 if (!TryInlineAndReplace(invoke_instruction, resolved_method, /* do_rtp */ false)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100359 return false;
360 }
361
362 // We successfully inlined, now add a guard.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100363 bool is_referrer =
364 (ic.GetMonomorphicType() == outermost_graph_->GetArtMethod()->GetDeclaringClass());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000365 AddTypeGuard(receiver,
366 cursor,
367 bb_cursor,
368 class_index,
369 is_referrer,
370 invoke_instruction,
371 /* with_deoptimization */ true);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100372
373 // Run type propagation to get the guard typed, and eventually propagate the
374 // type of the receiver.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000375 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100376 rtp_fixup.Run();
377
378 MaybeRecordStat(kInlinedMonomorphicCall);
379 return true;
380}
381
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000382HInstruction* HInliner::AddTypeGuard(HInstruction* receiver,
383 HInstruction* cursor,
384 HBasicBlock* bb_cursor,
385 uint32_t class_index,
386 bool is_referrer,
387 HInstruction* invoke_instruction,
388 bool with_deoptimization) {
389 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
390 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
391 class_linker, receiver, invoke_instruction->GetDexPc());
392
393 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
394 // Note that we will just compare the classes, so we don't need Java semantics access checks.
395 // Also, the caller of `AddTypeGuard` must have guaranteed that the class is in the dex cache.
396 HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(),
397 class_index,
398 caller_dex_file,
399 is_referrer,
400 invoke_instruction->GetDexPc(),
401 /* needs_access_check */ false,
402 /* is_in_dex_cache */ true);
403
404 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class);
405 // TODO: Extend reference type propagation to understand the guard.
406 if (cursor != nullptr) {
407 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
408 } else {
409 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
410 }
411 bb_cursor->InsertInstructionAfter(load_class, receiver_class);
412 bb_cursor->InsertInstructionAfter(compare, load_class);
413 if (with_deoptimization) {
414 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
415 compare, invoke_instruction->GetDexPc());
416 bb_cursor->InsertInstructionAfter(deoptimize, compare);
417 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
418 }
419 return compare;
420}
421
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000422bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100423 ArtMethod* resolved_method,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000424 const InlineCache& ic) {
425 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
426 << invoke_instruction->DebugName();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000427
428 if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, ic)) {
429 return true;
430 }
431
432 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
433 size_t pointer_size = class_linker->GetImagePointerSize();
434 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
435
436 bool all_targets_inlined = true;
437 bool one_target_inlined = false;
438 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
439 if (ic.GetTypeAt(i) == nullptr) {
440 break;
441 }
442 ArtMethod* method = nullptr;
443 if (invoke_instruction->IsInvokeInterface()) {
444 method = ic.GetTypeAt(i)->FindVirtualMethodForInterface(
445 resolved_method, pointer_size);
446 } else {
447 DCHECK(invoke_instruction->IsInvokeVirtual());
448 method = ic.GetTypeAt(i)->FindVirtualMethodForVirtual(
449 resolved_method, pointer_size);
450 }
451
452 HInstruction* receiver = invoke_instruction->InputAt(0);
453 HInstruction* cursor = invoke_instruction->GetPrevious();
454 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
455
Nicolas Geoffray1fe26e12016-02-18 16:55:42 +0000456 uint32_t class_index = FindClassIndexIn(
457 ic.GetTypeAt(i), caller_dex_file, caller_compilation_unit_.GetDexCache());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000458 HInstruction* return_replacement = nullptr;
459 if (class_index == DexFile::kDexNoIndex ||
460 !TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
461 all_targets_inlined = false;
462 } else {
463 one_target_inlined = true;
464 bool is_referrer = (ic.GetTypeAt(i) == outermost_graph_->GetArtMethod()->GetDeclaringClass());
465
466 // If we have inlined all targets before, and this receiver is the last seen,
467 // we deoptimize instead of keeping the original invoke instruction.
468 bool deoptimize = all_targets_inlined &&
469 (i != InlineCache::kIndividualCacheSize - 1) &&
470 (ic.GetTypeAt(i + 1) == nullptr);
471 HInstruction* compare = AddTypeGuard(
472 receiver, cursor, bb_cursor, class_index, is_referrer, invoke_instruction, deoptimize);
473 if (deoptimize) {
474 if (return_replacement != nullptr) {
475 invoke_instruction->ReplaceWith(return_replacement);
476 }
477 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
478 // Because the inline cache data can be populated concurrently, we force the end of the
479 // iteration. Otherhwise, we could see a new receiver type.
480 break;
481 } else {
482 CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction);
483 }
484 }
485 }
486
487 if (!one_target_inlined) {
488 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
489 << " from inline cache is not inlined because none"
490 << " of its targets could be inlined";
491 return false;
492 }
493 MaybeRecordStat(kInlinedPolymorphicCall);
494
495 // Run type propagation to get the guards typed.
496 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
497 rtp_fixup.Run();
498 return true;
499}
500
501void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
502 HInstruction* return_replacement,
503 HInstruction* invoke_instruction) {
504 uint32_t dex_pc = invoke_instruction->GetDexPc();
505 HBasicBlock* cursor_block = compare->GetBlock();
506 HBasicBlock* original_invoke_block = invoke_instruction->GetBlock();
507 ArenaAllocator* allocator = graph_->GetArena();
508
509 // Spit the block after the compare: `cursor_block` will now be the start of the diamond,
510 // and the returned block is the start of the then branch (that could contain multiple blocks).
511 HBasicBlock* then = cursor_block->SplitAfterForInlining(compare);
512
513 // Split the block containing the invoke before and after the invoke. The returned block
514 // of the split before will contain the invoke and will be the otherwise branch of
515 // the diamond. The returned block of the split after will be the merge block
516 // of the diamond.
517 HBasicBlock* end_then = invoke_instruction->GetBlock();
518 HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction);
519 HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction);
520
521 // If the methods we are inlining return a value, we create a phi in the merge block
522 // that will have the `invoke_instruction and the `return_replacement` as inputs.
523 if (return_replacement != nullptr) {
524 HPhi* phi = new (allocator) HPhi(
525 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc);
526 merge->AddPhi(phi);
527 invoke_instruction->ReplaceWith(phi);
528 phi->AddInput(return_replacement);
529 phi->AddInput(invoke_instruction);
530 }
531
532 // Add the control flow instructions.
533 otherwise->AddInstruction(new (allocator) HGoto(dex_pc));
534 end_then->AddInstruction(new (allocator) HGoto(dex_pc));
535 cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc));
536
537 // Add the newly created blocks to the graph.
538 graph_->AddBlock(then);
539 graph_->AddBlock(otherwise);
540 graph_->AddBlock(merge);
541
542 // Set up successor (and implictly predecessor) relations.
543 cursor_block->AddSuccessor(otherwise);
544 cursor_block->AddSuccessor(then);
545 end_then->AddSuccessor(merge);
546 otherwise->AddSuccessor(merge);
547
548 // Set up dominance information.
549 then->SetDominator(cursor_block);
550 cursor_block->AddDominatedBlock(then);
551 otherwise->SetDominator(cursor_block);
552 cursor_block->AddDominatedBlock(otherwise);
553 merge->SetDominator(cursor_block);
554 cursor_block->AddDominatedBlock(merge);
555
556 // Update the revert post order.
557 size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block);
558 MakeRoomFor(&graph_->reverse_post_order_, 1, index);
559 graph_->reverse_post_order_[++index] = then;
560 index = IndexOfElement(graph_->reverse_post_order_, end_then);
561 MakeRoomFor(&graph_->reverse_post_order_, 2, index);
562 graph_->reverse_post_order_[++index] = otherwise;
563 graph_->reverse_post_order_[++index] = merge;
564
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000565
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000566 graph_->UpdateLoopAndTryInformationOfNewBlock(
567 then, original_invoke_block, /* replace_if_back_edge */ false);
568 graph_->UpdateLoopAndTryInformationOfNewBlock(
569 otherwise, original_invoke_block, /* replace_if_back_edge */ false);
570
571 // In case the original invoke location was a back edge, we need to update
572 // the loop to now have the merge block as a back edge.
573 graph_->UpdateLoopAndTryInformationOfNewBlock(
574 merge, original_invoke_block, /* replace_if_back_edge */ true);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000575}
576
577bool HInliner::TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction,
578 ArtMethod* resolved_method,
579 const InlineCache& ic) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000580 // This optimization only works under JIT for now.
581 DCHECK(Runtime::Current()->UseJit());
Roland Levillain2aba7cd2016-02-03 12:27:20 +0000582 if (graph_->GetInstructionSet() == kMips64) {
583 // TODO: Support HClassTableGet for mips64.
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000584 return false;
585 }
586 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
587 size_t pointer_size = class_linker->GetImagePointerSize();
588
589 DCHECK(resolved_method != nullptr);
590 ArtMethod* actual_method = nullptr;
591 // Check whether we are actually calling the same method among
592 // the different types seen.
593 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
594 if (ic.GetTypeAt(i) == nullptr) {
595 break;
596 }
597 ArtMethod* new_method = nullptr;
598 if (invoke_instruction->IsInvokeInterface()) {
599 new_method = ic.GetTypeAt(i)->FindVirtualMethodForInterface(
600 resolved_method, pointer_size);
601 } else {
602 DCHECK(invoke_instruction->IsInvokeVirtual());
603 new_method = ic.GetTypeAt(i)->FindVirtualMethodForVirtual(
604 resolved_method, pointer_size);
605 }
606 if (actual_method == nullptr) {
607 actual_method = new_method;
608 } else if (actual_method != new_method) {
609 // Different methods, bailout.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000610 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
611 << " from inline cache is not inlined because it resolves"
612 << " to different methods";
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000613 return false;
614 }
615 }
616
617 HInstruction* receiver = invoke_instruction->InputAt(0);
618 HInstruction* cursor = invoke_instruction->GetPrevious();
619 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
620
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000621 if (!TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ false)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000622 return false;
623 }
624
625 // We successfully inlined, now add a guard.
626 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
627 class_linker, receiver, invoke_instruction->GetDexPc());
628
629 size_t method_offset = invoke_instruction->IsInvokeVirtual()
630 ? actual_method->GetVtableIndex()
631 : invoke_instruction->AsInvokeInterface()->GetImtIndex();
632
633 Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet())
634 ? Primitive::kPrimLong
635 : Primitive::kPrimInt;
636 HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet(
637 receiver_class,
638 type,
Vladimir Markoa1de9182016-02-25 11:37:38 +0000639 invoke_instruction->IsInvokeVirtual() ? HClassTableGet::TableKind::kVTable
640 : HClassTableGet::TableKind::kIMTable,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000641 method_offset,
642 invoke_instruction->GetDexPc());
643
644 HConstant* constant;
645 if (type == Primitive::kPrimLong) {
646 constant = graph_->GetLongConstant(
647 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
648 } else {
649 constant = graph_->GetIntConstant(
650 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
651 }
652
653 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant);
654 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
655 compare, invoke_instruction->GetDexPc());
656 // TODO: Extend reference type propagation to understand the guard.
657 if (cursor != nullptr) {
658 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
659 } else {
660 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
661 }
662 bb_cursor->InsertInstructionAfter(class_table_get, receiver_class);
663 bb_cursor->InsertInstructionAfter(compare, class_table_get);
664 bb_cursor->InsertInstructionAfter(deoptimize, compare);
665 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
666
667 // Run type propagation to get the guard typed.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000668 ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000669 rtp_fixup.Run();
670
671 MaybeRecordStat(kInlinedPolymorphicCall);
672
673 return true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100674}
675
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000676bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) {
677 HInstruction* return_replacement = nullptr;
678 if (!TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
679 return false;
680 }
681 if (return_replacement != nullptr) {
682 invoke_instruction->ReplaceWith(return_replacement);
683 }
684 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
685 FixUpReturnReferenceType(invoke_instruction, method, return_replacement, do_rtp);
686 return true;
687}
688
689bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction,
690 ArtMethod* method,
691 HInstruction** return_replacement) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100692 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800693
694 // Check whether we're allowed to inline. The outermost compilation unit is the relevant
695 // dex file here (though the transitivity of an inline chain would allow checking the calller).
696 if (!compiler_driver_->MayInline(method->GetDexFile(),
697 outer_compilation_unit_.GetDexFile())) {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000698 if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000699 VLOG(compiler) << "Successfully replaced pattern of invoke " << PrettyMethod(method);
700 MaybeRecordStat(kReplacedInvokeWithSimplePattern);
701 return true;
702 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800703 VLOG(compiler) << "Won't inline " << PrettyMethod(method) << " in "
704 << outer_compilation_unit_.GetDexFile()->GetLocation() << " ("
705 << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from "
706 << method->GetDexFile()->GetLocation();
707 return false;
708 }
709
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100710 uint32_t method_index = FindMethodIndexIn(
711 method, caller_dex_file, invoke_instruction->GetDexMethodIndex());
712 if (method_index == DexFile::kDexNoIndex) {
713 VLOG(compiler) << "Call to "
714 << PrettyMethod(method)
715 << " cannot be inlined because unaccessible to caller";
716 return false;
717 }
718
719 bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile());
720
721 const DexFile::CodeItem* code_item = method->GetCodeItem();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000722
723 if (code_item == nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100724 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000725 << " is not inlined because it is native";
726 return false;
727 }
728
Calin Juravleec748352015-07-29 13:52:12 +0100729 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
730 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100731 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000732 << " is too big to inline: "
733 << code_item->insns_size_in_code_units_
734 << " > "
735 << inline_max_code_units;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000736 return false;
737 }
738
739 if (code_item->tries_size_ != 0) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100740 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000741 << " is not inlined because of try block";
742 return false;
743 }
744
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100745 if (!method->GetDeclaringClass()->IsVerified()) {
746 uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex();
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000747 if (Runtime::Current()->UseJit() ||
748 !compiler_driver_->IsMethodVerifiedWithoutFailures(
749 method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100750 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
751 << " couldn't be verified, so it cannot be inlined";
752 return false;
753 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000754 }
755
Roland Levillain4c0eb422015-04-24 16:43:49 +0100756 if (invoke_instruction->IsInvokeStaticOrDirect() &&
757 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
758 // Case of a static method that cannot be inlined because it implicitly
759 // requires an initialization check of its declaring class.
760 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
761 << " is not inlined because it is static and requires a clinit"
762 << " check that cannot be emitted due to Dex cache limitations";
763 return false;
764 }
765
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000766 if (!TryBuildAndInlineHelper(invoke_instruction, method, same_dex_file, return_replacement)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000767 return false;
768 }
769
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000770 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000771 MaybeRecordStat(kInlinedInvoke);
772 return true;
773}
774
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000775static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction,
776 size_t arg_vreg_index)
777 SHARED_REQUIRES(Locks::mutator_lock_) {
778 size_t input_index = 0;
779 for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) {
780 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
781 if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) {
782 ++i;
783 DCHECK_NE(i, arg_vreg_index);
784 }
785 }
786 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
787 return invoke_instruction->InputAt(input_index);
788}
789
790// Try to recognize known simple patterns and replace invoke call with appropriate instructions.
791bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction,
792 ArtMethod* resolved_method,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000793 HInstruction** return_replacement) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000794 InlineMethod inline_method;
795 if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) {
796 return false;
797 }
798
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000799 switch (inline_method.opcode) {
800 case kInlineOpNop:
801 DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000802 *return_replacement = nullptr;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000803 break;
804 case kInlineOpReturnArg:
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000805 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction,
806 inline_method.d.return_data.arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000807 break;
808 case kInlineOpNonWideConst:
809 if (resolved_method->GetShorty()[0] == 'L') {
810 DCHECK_EQ(inline_method.d.data, 0u);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000811 *return_replacement = graph_->GetNullConstant();
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000812 } else {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000813 *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000814 }
815 break;
816 case kInlineOpIGet: {
817 const InlineIGetIPutData& data = inline_method.d.ifield_data;
818 if (data.method_is_static || data.object_arg != 0u) {
819 // TODO: Needs null check.
820 return false;
821 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000822 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000823 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000824 HInstanceFieldGet* iget = CreateInstanceFieldGet(dex_cache, data.field_idx, obj);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000825 DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset);
826 DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile);
827 invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000828 *return_replacement = iget;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000829 break;
830 }
831 case kInlineOpIPut: {
832 const InlineIGetIPutData& data = inline_method.d.ifield_data;
833 if (data.method_is_static || data.object_arg != 0u) {
834 // TODO: Needs null check.
835 return false;
836 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000837 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000838 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
839 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000840 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, data.field_idx, obj, value);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000841 DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset);
842 DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile);
843 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
844 if (data.return_arg_plus1 != 0u) {
845 size_t return_arg = data.return_arg_plus1 - 1u;
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000846 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000847 }
848 break;
849 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000850 case kInlineOpConstructor: {
851 const InlineConstructorData& data = inline_method.d.constructor_data;
852 // Get the indexes to arrays for easier processing.
853 uint16_t iput_field_indexes[] = {
854 data.iput0_field_index, data.iput1_field_index, data.iput2_field_index
855 };
856 uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg };
857 static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch");
858 // Count valid field indexes.
859 size_t number_of_iputs = 0u;
860 while (number_of_iputs != arraysize(iput_field_indexes) &&
861 iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) {
862 // Check that there are no duplicate valid field indexes.
863 DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1,
864 iput_field_indexes + arraysize(iput_field_indexes),
865 iput_field_indexes[number_of_iputs]));
866 ++number_of_iputs;
867 }
868 // Check that there are no valid field indexes in the rest of the array.
869 DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs,
870 iput_field_indexes + arraysize(iput_field_indexes),
871 [](uint16_t index) { return index != DexFile::kDexNoIndex16; }));
872
873 // Create HInstanceFieldSet for each IPUT that stores non-zero data.
874 Handle<mirror::DexCache> dex_cache;
875 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u);
876 bool needs_constructor_barrier = false;
877 for (size_t i = 0; i != number_of_iputs; ++i) {
878 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]);
879 if (!value->IsConstant() ||
880 (!value->AsConstant()->IsZero() && !value->IsNullConstant())) {
881 if (dex_cache.GetReference() == nullptr) {
882 dex_cache = handles_->NewHandle(resolved_method->GetDexCache());
883 }
884 uint16_t field_index = iput_field_indexes[i];
885 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, field_index, obj, value);
886 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
887
888 // Check whether the field is final. If it is, we need to add a barrier.
889 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
890 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
891 DCHECK(resolved_field != nullptr);
892 if (resolved_field->IsFinal()) {
893 needs_constructor_barrier = true;
894 }
895 }
896 }
897 if (needs_constructor_barrier) {
898 HMemoryBarrier* barrier = new (graph_->GetArena()) HMemoryBarrier(kStoreStore, kNoDexPc);
899 invoke_instruction->GetBlock()->InsertInstructionBefore(barrier, invoke_instruction);
900 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000901 *return_replacement = nullptr;
Vladimir Marko354efa62016-02-04 19:46:56 +0000902 break;
903 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000904 default:
905 LOG(FATAL) << "UNREACHABLE";
906 UNREACHABLE();
907 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000908 return true;
909}
910
Vladimir Marko354efa62016-02-04 19:46:56 +0000911HInstanceFieldGet* HInliner::CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000912 uint32_t field_index,
913 HInstruction* obj)
914 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000915 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
916 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
917 DCHECK(resolved_field != nullptr);
918 HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet(
919 obj,
920 resolved_field->GetTypeAsPrimitiveType(),
921 resolved_field->GetOffset(),
922 resolved_field->IsVolatile(),
923 field_index,
924 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +0000925 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000926 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +0000927 // Read barrier generates a runtime call in slow path and we need a valid
928 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
929 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000930 if (iget->GetType() == Primitive::kPrimNot) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000931 ReferenceTypePropagation rtp(graph_, handles_, /* is_first_run */ false);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000932 rtp.Visit(iget);
933 }
934 return iget;
935}
936
Vladimir Marko354efa62016-02-04 19:46:56 +0000937HInstanceFieldSet* HInliner::CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000938 uint32_t field_index,
939 HInstruction* obj,
940 HInstruction* value)
941 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000942 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
943 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
944 DCHECK(resolved_field != nullptr);
945 HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet(
946 obj,
947 value,
948 resolved_field->GetTypeAsPrimitiveType(),
949 resolved_field->GetOffset(),
950 resolved_field->IsVolatile(),
951 field_index,
952 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +0000953 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000954 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +0000955 // Read barrier generates a runtime call in slow path and we need a valid
956 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
957 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000958 return iput;
959}
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000960
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000961bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction,
962 ArtMethod* resolved_method,
963 bool same_dex_file,
964 HInstruction** return_replacement) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000965 ScopedObjectAccess soa(Thread::Current());
966 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100967 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
968 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000969 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700970 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000971 DexCompilationUnit dex_compilation_unit(
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000972 nullptr,
973 caller_compilation_unit_.GetClassLoader(),
974 class_linker,
975 callee_dex_file,
976 code_item,
977 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
978 method_index,
979 resolved_method->GetAccessFlags(),
980 /* verified_method */ nullptr,
981 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000982
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100983 bool requires_ctor_barrier = false;
984
985 if (dex_compilation_unit.IsConstructor()) {
986 // If it's a super invocation and we already generate a barrier there's no need
987 // to generate another one.
988 // We identify super calls by looking at the "this" pointer. If its value is the
989 // same as the local "this" pointer then we must have a super invocation.
990 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
991 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
992 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
993 requires_ctor_barrier = false;
994 } else {
995 Thread* self = Thread::Current();
996 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
997 dex_compilation_unit.GetDexFile(),
998 dex_compilation_unit.GetClassDefIndex());
999 }
1000 }
1001
Nicolas Geoffray35071052015-06-09 15:43:38 +01001002 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
1003 if (invoke_type == kInterface) {
1004 // We have statically resolved the dispatch. To please the class linker
1005 // at runtime, we change this call as if it was a virtual call.
1006 invoke_type = kVirtual;
1007 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00001008 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001009 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001010 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001011 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +01001012 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001013 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +01001014 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001015 graph_->IsDebuggable(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001016 /* osr */ false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001017 graph_->GetCurrentInstructionId());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001018 callee_graph->SetArtMethod(resolved_method);
David Brazdil5e8b1372015-01-23 14:39:08 +00001019
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001020 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +00001021 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001022 &dex_compilation_unit,
1023 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001024 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001025 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +00001026 &inline_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001027 resolved_method->GetQuickenedInfo(),
1028 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001029
David Brazdilbadd8262016-02-02 16:28:56 +00001030 if (builder.BuildGraph(*code_item, handles_) != kAnalysisSuccess) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001031 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001032 << " could not be built, so cannot be inlined";
1033 return false;
1034 }
1035
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001036 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
1037 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001038 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001039 << " cannot be inlined because of the register allocator";
1040 return false;
1041 }
1042
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001043 size_t parameter_index = 0;
1044 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
1045 !instructions.Done();
1046 instructions.Advance()) {
1047 HInstruction* current = instructions.Current();
1048 if (current->IsParameterValue()) {
1049 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
1050 if (argument->IsNullConstant()) {
1051 current->ReplaceWith(callee_graph->GetNullConstant());
1052 } else if (argument->IsIntConstant()) {
1053 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
1054 } else if (argument->IsLongConstant()) {
1055 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
1056 } else if (argument->IsFloatConstant()) {
1057 current->ReplaceWith(
1058 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
1059 } else if (argument->IsDoubleConstant()) {
1060 current->ReplaceWith(
1061 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
1062 } else if (argument->GetType() == Primitive::kPrimNot) {
1063 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
1064 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
1065 }
1066 }
1067 }
1068
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001069 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +01001070 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +00001071 HConstantFolding fold(callee_graph);
Vladimir Markodc151b22015-10-15 18:02:30 +01001072 HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_);
Calin Juravleacf735c2015-02-12 15:25:22 +00001073 InstructionSimplifier simplify(callee_graph, stats_);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +01001074 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001075
1076 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +01001077 &intrinsics,
Vladimir Markodc151b22015-10-15 18:02:30 +01001078 &sharpening,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001079 &simplify,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001080 &fold,
Vladimir Marko9e23df52015-11-10 17:14:35 +00001081 &dce,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001082 };
1083
1084 for (size_t i = 0; i < arraysize(optimizations); ++i) {
1085 HOptimization* optimization = optimizations[i];
1086 optimization->Run();
1087 }
1088
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001089 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Calin Juravleec748352015-07-29 13:52:12 +01001090 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001091 HInliner inliner(callee_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001092 outermost_graph_,
Vladimir Markodc151b22015-10-15 18:02:30 +01001093 codegen_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001094 outer_compilation_unit_,
1095 dex_compilation_unit,
1096 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +01001097 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001098 stats_,
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001099 total_number_of_dex_registers_ + code_item->registers_size_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001100 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001101 inliner.Run();
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001102 number_of_instructions_budget += inliner.number_of_inlined_instructions_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001103 }
1104
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001105 // TODO: We should abort only if all predecessors throw. However,
1106 // HGraph::InlineInto currently does not handle an exit block with
1107 // a throw predecessor.
1108 HBasicBlock* exit_block = callee_graph->GetExitBlock();
1109 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001110 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001111 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001112 return false;
1113 }
1114
1115 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +00001116 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
1117 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001118 has_throw_predecessor = true;
1119 break;
1120 }
1121 }
1122 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001123 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001124 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001125 return false;
1126 }
1127
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001128 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001129 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001130 size_t number_of_instructions = 0;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001131
1132 bool can_inline_environment =
1133 total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters;
1134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001135 for (; !it.Done(); it.Advance()) {
1136 HBasicBlock* block = it.Current();
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00001137
1138 if (block->IsLoopHeader() && block->GetLoopInformation()->IsIrreducible()) {
1139 // Don't inline methods with irreducible loops, they could prevent some
1140 // optimizations to run.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001141 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00001142 << " could not be inlined because it contains an irreducible loop";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001143 return false;
1144 }
1145
1146 for (HInstructionIterator instr_it(block->GetInstructions());
1147 !instr_it.Done();
1148 instr_it.Advance()) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001149 if (number_of_instructions++ == number_of_instructions_budget) {
1150 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001151 << " is not inlined because its caller has reached"
1152 << " its instruction budget limit.";
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001153 return false;
1154 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001155 HInstruction* current = instr_it.Current();
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001156 if (!can_inline_environment && current->NeedsEnvironment()) {
1157 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1158 << " is not inlined because its caller has reached"
1159 << " its environment budget limit.";
1160 return false;
1161 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001162
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001163 if (current->IsInvokeInterface()) {
1164 // Disable inlining of interface calls. The cost in case of entering the
1165 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001166 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001167 << " could not be inlined because it has an interface call.";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168 return false;
1169 }
1170
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001171 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001172 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001173 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001174 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001175 return false;
1176 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001177
Vladimir Markodc151b22015-10-15 18:02:30 +01001178 if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001179 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001180 << " could not be inlined because " << current->DebugName()
1181 << " it is in a different dex file and requires access to the dex cache";
1182 return false;
1183 }
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001184
1185 if (current->IsNewInstance() &&
1186 (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001187 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1188 << " could not be inlined because it is using an entrypoint"
1189 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001190 // Allocation entrypoint does not handle inlined frames.
1191 return false;
1192 }
1193
1194 if (current->IsNewArray() &&
1195 (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001196 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1197 << " could not be inlined because it is using an entrypoint"
1198 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001199 // Allocation entrypoint does not handle inlined frames.
1200 return false;
1201 }
1202
1203 if (current->IsUnresolvedStaticFieldGet() ||
1204 current->IsUnresolvedInstanceFieldGet() ||
1205 current->IsUnresolvedStaticFieldSet() ||
1206 current->IsUnresolvedInstanceFieldSet()) {
1207 // Entrypoint for unresolved fields does not handle inlined frames.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001208 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
1209 << " could not be inlined because it is using an unresolved"
1210 << " entrypoint";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001211 return false;
1212 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001213 }
1214 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001215 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001216
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001217 *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001218 return true;
1219}
Calin Juravle2e768302015-07-28 14:41:11 +00001220
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001221void HInliner::FixUpReturnReferenceType(HInvoke* invoke_instruction,
1222 ArtMethod* resolved_method,
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001223 HInstruction* return_replacement,
1224 bool do_rtp) {
Alex Light68289a52015-12-15 17:30:30 -08001225 // Check the integrity of reference types and run another type propagation if needed.
David Brazdil4833f5a2015-12-16 10:37:39 +00001226 if (return_replacement != nullptr) {
1227 if (return_replacement->GetType() == Primitive::kPrimNot) {
1228 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
1229 // Make sure that we have a valid type for the return. We may get an invalid one when
1230 // we inline invokes with multiple branches and create a Phi for the result.
1231 // TODO: we could be more precise by merging the phi inputs but that requires
1232 // some functionality from the reference type propagation.
1233 DCHECK(return_replacement->IsPhi());
1234 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1235 ReferenceTypeInfo::TypeHandle return_handle =
1236 handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size));
1237 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
1238 return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */));
1239 }
Alex Light68289a52015-12-15 17:30:30 -08001240
David Brazdil4833f5a2015-12-16 10:37:39 +00001241 if (do_rtp) {
1242 // If the return type is a refinement of the declared type run the type propagation again.
1243 ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo();
1244 ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo();
1245 if (invoke_rti.IsStrictSupertypeOf(return_rti)
1246 || (return_rti.IsExact() && !invoke_rti.IsExact())
1247 || !return_replacement->CanBeNull()) {
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001248 ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run();
David Brazdil4833f5a2015-12-16 10:37:39 +00001249 }
1250 }
1251 } else if (return_replacement->IsInstanceOf()) {
1252 if (do_rtp) {
1253 // Inlining InstanceOf into an If may put a tighter bound on reference types.
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001254 ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001255 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001256 }
Calin Juravle2e768302015-07-28 14:41:11 +00001257 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001258}
1259
1260} // namespace art