blob: 01e89bb304dbed7e8cde8b622fa975751197fef5 [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000021#include "builder.h"
22#include "class_linker.h"
23#include "constant_folding.h"
24#include "dead_code_elimination.h"
Vladimir Markobe10e8e2016-01-22 12:09:44 +000025#include "dex/verified_method.h"
26#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000027#include "driver/compiler_driver-inl.h"
Calin Juravleec748352015-07-29 13:52:12 +010028#include "driver/compiler_options.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000029#include "driver/dex_compilation_unit.h"
30#include "instruction_simplifier.h"
Scott Wakelingd60a1af2015-07-22 14:32:44 +010031#include "intrinsics.h"
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +000032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "mirror/class_loader.h"
35#include "mirror/dex_cache.h"
36#include "nodes.h"
Nicolas Geoffray335005e2015-06-25 10:01:47 +010037#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010038#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070039#include "register_allocator_linear_scan.h"
Vladimir Markobe10e8e2016-01-22 12:09:44 +000040#include "quick/inline_method_analyser.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010041#include "sharpening.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000042#include "ssa_builder.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000043#include "ssa_phi_elimination.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070044#include "scoped_thread_state_change-inl.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045#include "thread.h"
46
47namespace art {
48
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000049static constexpr size_t kMaximumNumberOfHInstructions = 32;
50
51// Limit the number of dex registers that we accumulate while inlining
52// to avoid creating large amount of nested environments.
53static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64;
54
55// Avoid inlining within a huge method due to memory pressure.
56static constexpr size_t kMaximumCodeUnitSize = 4096;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070057
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000058void HInliner::Run() {
Calin Juravle8f96df82015-07-29 15:58:48 +010059 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
60 if ((compiler_options.GetInlineDepthLimit() == 0)
61 || (compiler_options.GetInlineMaxCodeUnits() == 0)) {
62 return;
63 }
Nicolas Geoffray5949fa02015-12-18 10:57:10 +000064 if (caller_compilation_unit_.GetCodeItem()->insns_size_in_code_units_ > kMaximumCodeUnitSize) {
65 return;
66 }
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000067 if (graph_->IsDebuggable()) {
68 // For simplicity, we currently never inline when the graph is debuggable. This avoids
69 // doing some logic in the runtime to discover if a method could have been inlined.
70 return;
71 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010072 const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
73 DCHECK(!blocks.empty());
74 HBasicBlock* next_block = blocks[0];
75 for (size_t i = 0; i < blocks.size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010076 // Because we are changing the graph when inlining, we need to remember the next block.
77 // This avoids doing the inlining work again on the inlined blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010078 if (blocks[i] != next_block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010079 continue;
80 }
81 HBasicBlock* block = next_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +010082 next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1];
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000083 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
84 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010085 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070086 // As long as the call is not intrinsified, it is worth trying to inline.
87 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000088 // We use the original invoke type to ensure the resolution of the called method
89 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000090 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010091 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092 std::string callee_name =
David Sehr709b0702016-10-13 09:12:37 -070093 outer_compilation_unit_.GetDexFile()->PrettyMethod(call->GetDexMethodIndex());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000094 bool should_inline = callee_name.find("$inline$") != std::string::npos;
95 CHECK(!should_inline) << "Could not inline " << callee_name;
96 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010097 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010098 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010099 std::string callee_name =
David Sehr709b0702016-10-13 09:12:37 -0700100 outer_compilation_unit_.GetDexFile()->PrettyMethod(call->GetDexMethodIndex());
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +0100101 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
102 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
103 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000104 }
105 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000106 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000107 }
108 }
109}
110
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100111static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100113 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
114}
115
116/**
117 * Given the `resolved_method` looked up in the dex cache, try to find
118 * the actual runtime target of an interface or virtual call.
119 * Return nullptr if the runtime target cannot be proven.
120 */
121static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100123 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
124 // No need to lookup further, the resolved method will be the target.
125 return resolved_method;
126 }
127
128 HInstruction* receiver = invoke->InputAt(0);
129 if (receiver->IsNullCheck()) {
130 // Due to multiple levels of inlining within the same pass, it might be that
131 // null check does not have the reference type of the actual receiver.
132 receiver = receiver->InputAt(0);
133 }
134 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000135 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
136 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100137 // We currently only support inlining with known receivers.
138 // TODO: Remove this check, we should be able to inline final methods
139 // on unknown receivers.
140 return nullptr;
141 } else if (info.GetTypeHandle()->IsInterface()) {
142 // Statically knowing that the receiver has an interface type cannot
143 // help us find what is the target method.
144 return nullptr;
145 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
146 // The method that we're trying to call is not in the receiver's class or super classes.
147 return nullptr;
Nicolas Geoffrayab5327d2016-03-18 11:36:20 +0000148 } else if (info.GetTypeHandle()->IsErroneous()) {
149 // If the type is erroneous, do not go further, as we are going to query the vtable or
150 // imt table, that we can only safely do on non-erroneous classes.
151 return nullptr;
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100152 }
153
154 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700155 PointerSize pointer_size = cl->GetImagePointerSize();
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100156 if (invoke->IsInvokeInterface()) {
157 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
158 resolved_method, pointer_size);
159 } else {
160 DCHECK(invoke->IsInvokeVirtual());
161 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
162 resolved_method, pointer_size);
163 }
164
165 if (resolved_method == nullptr) {
166 // The information we had on the receiver was not enough to find
167 // the target method. Since we check above the exact type of the receiver,
168 // the only reason this can happen is an IncompatibleClassChangeError.
169 return nullptr;
Alex Light9139e002015-10-09 15:59:48 -0700170 } else if (!resolved_method->IsInvokable()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100171 // The information we had on the receiver was not enough to find
172 // the target method. Since we check above the exact type of the receiver,
173 // the only reason this can happen is an IncompatibleClassChangeError.
174 return nullptr;
175 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
176 // A final method has to be the target method.
177 return resolved_method;
178 } else if (info.IsExact()) {
179 // If we found a method and the receiver's concrete type is statically
180 // known, we know for sure the target.
181 return resolved_method;
182 } else {
183 // Even if we did find a method, the receiver type was not enough to
184 // statically find the runtime target.
185 return nullptr;
186 }
187}
188
189static uint32_t FindMethodIndexIn(ArtMethod* method,
190 const DexFile& dex_file,
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000191 uint32_t name_and_signature_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100193 if (IsSameDexFile(*method->GetDexFile(), dex_file)) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100194 return method->GetDexMethodIndex();
195 } else {
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000196 return method->FindDexMethodIndexInOtherDexFile(dex_file, name_and_signature_index);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100197 }
198}
199
Andreas Gampea5b09a62016-11-17 15:21:22 -0800200static dex::TypeIndex FindClassIndexIn(mirror::Class* cls,
201 const DexFile& dex_file,
202 Handle<mirror::DexCache> dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800204 dex::TypeIndex index;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100205 if (cls->GetDexCache() == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700206 DCHECK(cls->IsArrayClass()) << cls->PrettyClass();
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000207 index = cls->FindTypeIndexInOtherDexFile(dex_file);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800208 } else if (!cls->GetDexTypeIndex().IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700209 DCHECK(cls->IsProxyClass()) << cls->PrettyClass();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100210 // TODO: deal with proxy classes.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100211 } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) {
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100212 DCHECK_EQ(cls->GetDexCache(), dex_cache.Get());
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000213 index = cls->GetDexTypeIndex();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100214 // Update the dex cache to ensure the class is in. The generated code will
215 // consider it is. We make it safe by updating the dex cache, as other
216 // dex files might also load the class, and there is no guarantee the dex
217 // cache of the dex file of the class will be updated.
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000218 if (dex_cache->GetResolvedType(index) == nullptr) {
219 dex_cache->SetResolvedType(index, cls);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100220 }
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100221 } else {
222 index = cls->FindTypeIndexInOtherDexFile(dex_file);
223 // We cannot guarantee the entry in the dex cache will resolve to the same class,
224 // as there may be different class loaders. So only return the index if it's
225 // the right class in the dex cache already.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800226 if (index.IsValid() && dex_cache->GetResolvedType(index) != cls) {
227 index = dex::TypeIndex::Invalid();
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100228 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100229 }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000230
231 return index;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100232}
233
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000234class ScopedProfilingInfoInlineUse {
235 public:
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000236 explicit ScopedProfilingInfoInlineUse(ArtMethod* method, Thread* self)
237 : method_(method),
238 self_(self),
239 // Fetch the profiling info ahead of using it. If it's null when fetching,
240 // we should not call JitCodeCache::DoneInlining.
241 profiling_info_(
242 Runtime::Current()->GetJit()->GetCodeCache()->NotifyCompilerUse(method, self)) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000243 }
244
245 ~ScopedProfilingInfoInlineUse() {
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000246 if (profiling_info_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700247 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000248 DCHECK_EQ(profiling_info_, method_->GetProfilingInfo(pointer_size));
249 Runtime::Current()->GetJit()->GetCodeCache()->DoneCompilerUse(method_, self_);
250 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000251 }
252
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000253 ProfilingInfo* GetProfilingInfo() const { return profiling_info_; }
254
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000255 private:
256 ArtMethod* const method_;
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000257 Thread* const self_;
258 ProfilingInfo* const profiling_info_;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000259};
260
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000261static bool IsMonomorphic(Handle<mirror::ObjectArray<mirror::Class>> classes)
262 REQUIRES_SHARED(Locks::mutator_lock_) {
263 DCHECK_GE(InlineCache::kIndividualCacheSize, 2);
264 return classes->Get(0) != nullptr && classes->Get(1) == nullptr;
265}
266
267static bool IsMegamorphic(Handle<mirror::ObjectArray<mirror::Class>> classes)
268 REQUIRES_SHARED(Locks::mutator_lock_) {
269 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
270 if (classes->Get(i) == nullptr) {
271 return false;
272 }
273 }
274 return true;
275}
276
277static mirror::Class* GetMonomorphicType(Handle<mirror::ObjectArray<mirror::Class>> classes)
278 REQUIRES_SHARED(Locks::mutator_lock_) {
279 DCHECK(classes->Get(0) != nullptr);
280 return classes->Get(0);
281}
282
283static bool IsUninitialized(Handle<mirror::ObjectArray<mirror::Class>> classes)
284 REQUIRES_SHARED(Locks::mutator_lock_) {
285 return classes->Get(0) == nullptr;
286}
287
288static bool IsPolymorphic(Handle<mirror::ObjectArray<mirror::Class>> classes)
289 REQUIRES_SHARED(Locks::mutator_lock_) {
290 DCHECK_GE(InlineCache::kIndividualCacheSize, 3);
291 return classes->Get(1) != nullptr &&
292 classes->Get(InlineCache::kIndividualCacheSize - 1) == nullptr;
293}
294
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700295bool HInliner::TryInline(HInvoke* invoke_instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +0100296 if (invoke_instruction->IsInvokeUnresolved()) {
297 return false; // Don't bother to move further if we know the method is unresolved.
298 }
299
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000300 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100301 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000302 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
David Sehr709b0702016-10-13 09:12:37 -0700303 VLOG(compiler) << "Try inlining " << caller_dex_file.PrettyMethod(method_index);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000304
Nicolas Geoffray35071052015-06-09 15:43:38 +0100305 // We can query the dex cache directly. The verifier has populated it already.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100306 ArtMethod* resolved_method = invoke_instruction->GetResolvedMethod();
Andreas Gampefd2140f2015-12-23 16:30:44 -0800307 ArtMethod* actual_method = nullptr;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100308 if (resolved_method == nullptr) {
309 DCHECK(invoke_instruction->IsInvokeStaticOrDirect());
310 DCHECK(invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit());
311 VLOG(compiler) << "Not inlining a String.<init> method";
312 return false;
313 } else if (invoke_instruction->IsInvokeStaticOrDirect()) {
Andreas Gampefd2140f2015-12-23 16:30:44 -0800314 actual_method = resolved_method;
Vladimir Marko58155012015-08-19 12:49:41 +0000315 } else {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100316 // Check if we can statically find the method.
317 actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000318 }
319
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100320 if (actual_method != nullptr) {
Calin Juravle69158982016-03-16 11:53:41 +0000321 bool result = TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ true);
322 if (result && !invoke_instruction->IsInvokeStaticOrDirect()) {
323 MaybeRecordStat(kInlinedInvokeVirtualOrInterface);
324 }
325 return result;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100326 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000327
Andreas Gampefd2140f2015-12-23 16:30:44 -0800328 DCHECK(!invoke_instruction->IsInvokeStaticOrDirect());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100329
330 // Check if we can use an inline cache.
331 ArtMethod* caller = graph_->GetArtMethod();
Calin Juravleffc87072016-04-20 14:22:09 +0100332 if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000333 // Under JIT, we should always know the caller.
334 DCHECK(caller != nullptr);
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +0000335 ScopedProfilingInfoInlineUse spiis(caller, soa.Self());
336 ProfilingInfo* profiling_info = spiis.GetProfilingInfo();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000337 if (profiling_info != nullptr) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000338 StackHandleScope<1> hs(soa.Self());
339 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
340 Handle<mirror::ObjectArray<mirror::Class>> inline_cache = hs.NewHandle(
341 mirror::ObjectArray<mirror::Class>::Alloc(
342 soa.Self(),
343 class_linker->GetClassRoot(ClassLinker::kClassArrayClass),
344 InlineCache::kIndividualCacheSize));
345 if (inline_cache.Get() == nullptr) {
346 // We got an OOME. Just clear the exception, and don't inline.
347 DCHECK(soa.Self()->IsExceptionPending());
348 soa.Self()->ClearException();
349 VLOG(compiler) << "Out of memory in the compiler when trying to inline";
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000350 return false;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000351 } else {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000352 Runtime::Current()->GetJit()->GetCodeCache()->CopyInlineCacheInto(
353 *profiling_info->GetInlineCache(invoke_instruction->GetDexPc()),
354 inline_cache);
355 if (IsUninitialized(inline_cache)) {
356 VLOG(compiler) << "Interface or virtual call to "
357 << caller_dex_file.PrettyMethod(method_index)
358 << " is not hit and not inlined";
359 return false;
360 } else if (IsMonomorphic(inline_cache)) {
361 MaybeRecordStat(kMonomorphicCall);
362 if (outermost_graph_->IsCompilingOsr()) {
363 // If we are compiling OSR, we pretend this call is polymorphic, as we may come from the
364 // interpreter and it may have seen different receiver types.
365 return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache);
366 } else {
367 return TryInlineMonomorphicCall(invoke_instruction, resolved_method, inline_cache);
368 }
369 } else if (IsPolymorphic(inline_cache)) {
370 MaybeRecordStat(kPolymorphicCall);
371 return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache);
372 } else {
373 DCHECK(IsMegamorphic(inline_cache));
374 VLOG(compiler) << "Interface or virtual call to "
375 << caller_dex_file.PrettyMethod(method_index)
376 << " is megamorphic and not inlined";
377 MaybeRecordStat(kMegamorphicCall);
378 return false;
379 }
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000380 }
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100381 }
382 }
383
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100384 VLOG(compiler) << "Interface or virtual call to "
David Sehr709b0702016-10-13 09:12:37 -0700385 << caller_dex_file.PrettyMethod(method_index)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100386 << " could not be statically determined";
387 return false;
388}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000390HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker,
391 HInstruction* receiver,
392 uint32_t dex_pc) const {
393 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
394 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000395 HInstanceFieldGet* result = new (graph_->GetArena()) HInstanceFieldGet(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000396 receiver,
397 Primitive::kPrimNot,
398 field->GetOffset(),
399 field->IsVolatile(),
400 field->GetDexFieldIndex(),
401 field->GetDeclaringClass()->GetDexClassDefIndex(),
402 *field->GetDexFile(),
403 handles_->NewHandle(field->GetDexCache()),
404 dex_pc);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000405 // The class of a field is effectively final, and does not have any memory dependencies.
406 result->SetSideEffects(SideEffects::None());
407 return result;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000408}
409
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100410bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction,
411 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000412 Handle<mirror::ObjectArray<mirror::Class>> classes) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000413 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
414 << invoke_instruction->DebugName();
415
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100416 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -0800417 dex::TypeIndex class_index = FindClassIndexIn(
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000418 GetMonomorphicType(classes), caller_dex_file, caller_compilation_unit_.GetDexCache());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800419 if (!class_index.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700420 VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100421 << " from inline cache is not inlined because its class is not"
422 << " accessible to the caller";
423 return false;
424 }
425
426 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700427 PointerSize pointer_size = class_linker->GetImagePointerSize();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100428 if (invoke_instruction->IsInvokeInterface()) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000429 resolved_method = GetMonomorphicType(classes)->FindVirtualMethodForInterface(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100430 resolved_method, pointer_size);
431 } else {
432 DCHECK(invoke_instruction->IsInvokeVirtual());
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000433 resolved_method = GetMonomorphicType(classes)->FindVirtualMethodForVirtual(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100434 resolved_method, pointer_size);
435 }
436 DCHECK(resolved_method != nullptr);
437 HInstruction* receiver = invoke_instruction->InputAt(0);
438 HInstruction* cursor = invoke_instruction->GetPrevious();
439 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
440
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000441 if (!TryInlineAndReplace(invoke_instruction, resolved_method, /* do_rtp */ false)) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100442 return false;
443 }
444
445 // We successfully inlined, now add a guard.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100446 bool is_referrer =
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000447 (GetMonomorphicType(classes) == outermost_graph_->GetArtMethod()->GetDeclaringClass());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000448 AddTypeGuard(receiver,
449 cursor,
450 bb_cursor,
451 class_index,
452 is_referrer,
453 invoke_instruction,
454 /* with_deoptimization */ true);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100455
456 // Run type propagation to get the guard typed, and eventually propagate the
457 // type of the receiver.
Vladimir Marko456307a2016-04-19 14:12:13 +0000458 ReferenceTypePropagation rtp_fixup(graph_,
459 outer_compilation_unit_.GetDexCache(),
460 handles_,
461 /* is_first_run */ false);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100462 rtp_fixup.Run();
463
464 MaybeRecordStat(kInlinedMonomorphicCall);
465 return true;
466}
467
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000468HInstruction* HInliner::AddTypeGuard(HInstruction* receiver,
469 HInstruction* cursor,
470 HBasicBlock* bb_cursor,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800471 dex::TypeIndex class_index,
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000472 bool is_referrer,
473 HInstruction* invoke_instruction,
474 bool with_deoptimization) {
475 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
476 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
477 class_linker, receiver, invoke_instruction->GetDexPc());
478
479 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
480 // Note that we will just compare the classes, so we don't need Java semantics access checks.
481 // Also, the caller of `AddTypeGuard` must have guaranteed that the class is in the dex cache.
482 HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(),
483 class_index,
484 caller_dex_file,
485 is_referrer,
486 invoke_instruction->GetDexPc(),
487 /* needs_access_check */ false,
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700488 /* is_in_dex_cache */ true,
489 /* is_in_boot_image */ false);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000490
491 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class);
492 // TODO: Extend reference type propagation to understand the guard.
493 if (cursor != nullptr) {
494 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
495 } else {
496 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
497 }
498 bb_cursor->InsertInstructionAfter(load_class, receiver_class);
499 bb_cursor->InsertInstructionAfter(compare, load_class);
500 if (with_deoptimization) {
501 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
502 compare, invoke_instruction->GetDexPc());
503 bb_cursor->InsertInstructionAfter(deoptimize, compare);
504 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
505 }
506 return compare;
507}
508
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000509bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100510 ArtMethod* resolved_method,
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000511 Handle<mirror::ObjectArray<mirror::Class>> classes) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000512 DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface())
513 << invoke_instruction->DebugName();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000514
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000515 if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, classes)) {
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000516 return true;
517 }
518
519 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700520 PointerSize pointer_size = class_linker->GetImagePointerSize();
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000521 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
522
523 bool all_targets_inlined = true;
524 bool one_target_inlined = false;
525 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000526 if (classes->Get(i) == nullptr) {
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000527 break;
528 }
529 ArtMethod* method = nullptr;
530 if (invoke_instruction->IsInvokeInterface()) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000531 method = classes->Get(i)->FindVirtualMethodForInterface(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000532 resolved_method, pointer_size);
533 } else {
534 DCHECK(invoke_instruction->IsInvokeVirtual());
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000535 method = classes->Get(i)->FindVirtualMethodForVirtual(
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000536 resolved_method, pointer_size);
537 }
538
539 HInstruction* receiver = invoke_instruction->InputAt(0);
540 HInstruction* cursor = invoke_instruction->GetPrevious();
541 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
542
Andreas Gampea5b09a62016-11-17 15:21:22 -0800543 dex::TypeIndex class_index = FindClassIndexIn(
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000544 classes->Get(i), caller_dex_file, caller_compilation_unit_.GetDexCache());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000545 HInstruction* return_replacement = nullptr;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800546 if (!class_index.IsValid() ||
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000547 !TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
548 all_targets_inlined = false;
549 } else {
550 one_target_inlined = true;
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000551 bool is_referrer = (classes->Get(i) == outermost_graph_->GetArtMethod()->GetDeclaringClass());
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000552
553 // If we have inlined all targets before, and this receiver is the last seen,
554 // we deoptimize instead of keeping the original invoke instruction.
555 bool deoptimize = all_targets_inlined &&
556 (i != InlineCache::kIndividualCacheSize - 1) &&
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000557 (classes->Get(i + 1) == nullptr);
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100558
559 if (outermost_graph_->IsCompilingOsr()) {
560 // We do not support HDeoptimize in OSR methods.
561 deoptimize = false;
562 }
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000563 HInstruction* compare = AddTypeGuard(
564 receiver, cursor, bb_cursor, class_index, is_referrer, invoke_instruction, deoptimize);
565 if (deoptimize) {
566 if (return_replacement != nullptr) {
567 invoke_instruction->ReplaceWith(return_replacement);
568 }
569 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
570 // Because the inline cache data can be populated concurrently, we force the end of the
571 // iteration. Otherhwise, we could see a new receiver type.
572 break;
573 } else {
574 CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction);
575 }
576 }
577 }
578
579 if (!one_target_inlined) {
David Sehr709b0702016-10-13 09:12:37 -0700580 VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method)
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000581 << " from inline cache is not inlined because none"
582 << " of its targets could be inlined";
583 return false;
584 }
585 MaybeRecordStat(kInlinedPolymorphicCall);
586
587 // Run type propagation to get the guards typed.
Vladimir Marko456307a2016-04-19 14:12:13 +0000588 ReferenceTypePropagation rtp_fixup(graph_,
589 outer_compilation_unit_.GetDexCache(),
590 handles_,
591 /* is_first_run */ false);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000592 rtp_fixup.Run();
593 return true;
594}
595
596void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare,
597 HInstruction* return_replacement,
598 HInstruction* invoke_instruction) {
599 uint32_t dex_pc = invoke_instruction->GetDexPc();
600 HBasicBlock* cursor_block = compare->GetBlock();
601 HBasicBlock* original_invoke_block = invoke_instruction->GetBlock();
602 ArenaAllocator* allocator = graph_->GetArena();
603
604 // Spit the block after the compare: `cursor_block` will now be the start of the diamond,
605 // and the returned block is the start of the then branch (that could contain multiple blocks).
606 HBasicBlock* then = cursor_block->SplitAfterForInlining(compare);
607
608 // Split the block containing the invoke before and after the invoke. The returned block
609 // of the split before will contain the invoke and will be the otherwise branch of
610 // the diamond. The returned block of the split after will be the merge block
611 // of the diamond.
612 HBasicBlock* end_then = invoke_instruction->GetBlock();
613 HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction);
614 HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction);
615
616 // If the methods we are inlining return a value, we create a phi in the merge block
617 // that will have the `invoke_instruction and the `return_replacement` as inputs.
618 if (return_replacement != nullptr) {
619 HPhi* phi = new (allocator) HPhi(
620 allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc);
621 merge->AddPhi(phi);
622 invoke_instruction->ReplaceWith(phi);
623 phi->AddInput(return_replacement);
624 phi->AddInput(invoke_instruction);
625 }
626
627 // Add the control flow instructions.
628 otherwise->AddInstruction(new (allocator) HGoto(dex_pc));
629 end_then->AddInstruction(new (allocator) HGoto(dex_pc));
630 cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc));
631
632 // Add the newly created blocks to the graph.
633 graph_->AddBlock(then);
634 graph_->AddBlock(otherwise);
635 graph_->AddBlock(merge);
636
637 // Set up successor (and implictly predecessor) relations.
638 cursor_block->AddSuccessor(otherwise);
639 cursor_block->AddSuccessor(then);
640 end_then->AddSuccessor(merge);
641 otherwise->AddSuccessor(merge);
642
643 // Set up dominance information.
644 then->SetDominator(cursor_block);
645 cursor_block->AddDominatedBlock(then);
646 otherwise->SetDominator(cursor_block);
647 cursor_block->AddDominatedBlock(otherwise);
648 merge->SetDominator(cursor_block);
649 cursor_block->AddDominatedBlock(merge);
650
651 // Update the revert post order.
652 size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block);
653 MakeRoomFor(&graph_->reverse_post_order_, 1, index);
654 graph_->reverse_post_order_[++index] = then;
655 index = IndexOfElement(graph_->reverse_post_order_, end_then);
656 MakeRoomFor(&graph_->reverse_post_order_, 2, index);
657 graph_->reverse_post_order_[++index] = otherwise;
658 graph_->reverse_post_order_[++index] = merge;
659
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000660
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000661 graph_->UpdateLoopAndTryInformationOfNewBlock(
662 then, original_invoke_block, /* replace_if_back_edge */ false);
663 graph_->UpdateLoopAndTryInformationOfNewBlock(
664 otherwise, original_invoke_block, /* replace_if_back_edge */ false);
665
666 // In case the original invoke location was a back edge, we need to update
667 // the loop to now have the merge block as a back edge.
668 graph_->UpdateLoopAndTryInformationOfNewBlock(
669 merge, original_invoke_block, /* replace_if_back_edge */ true);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000670}
671
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000672bool HInliner::TryInlinePolymorphicCallToSameTarget(
673 HInvoke* invoke_instruction,
674 ArtMethod* resolved_method,
675 Handle<mirror::ObjectArray<mirror::Class>> classes) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000676 // This optimization only works under JIT for now.
Calin Juravleffc87072016-04-20 14:22:09 +0100677 DCHECK(Runtime::Current()->UseJitCompilation());
Roland Levillain2aba7cd2016-02-03 12:27:20 +0000678 if (graph_->GetInstructionSet() == kMips64) {
679 // TODO: Support HClassTableGet for mips64.
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000680 return false;
681 }
682 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700683 PointerSize pointer_size = class_linker->GetImagePointerSize();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000684
685 DCHECK(resolved_method != nullptr);
686 ArtMethod* actual_method = nullptr;
Nicolas Geoffray4f97a212016-02-25 16:17:54 +0000687 size_t method_index = invoke_instruction->IsInvokeVirtual()
688 ? invoke_instruction->AsInvokeVirtual()->GetVTableIndex()
689 : invoke_instruction->AsInvokeInterface()->GetImtIndex();
690
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000691 // Check whether we are actually calling the same method among
692 // the different types seen.
693 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000694 if (classes->Get(i) == nullptr) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000695 break;
696 }
697 ArtMethod* new_method = nullptr;
698 if (invoke_instruction->IsInvokeInterface()) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000699 new_method = classes->Get(i)->GetImt(pointer_size)->Get(
Matthew Gharrity465ecc82016-07-19 21:32:52 +0000700 method_index, pointer_size);
Nicolas Geoffray4f97a212016-02-25 16:17:54 +0000701 if (new_method->IsRuntimeMethod()) {
702 // Bail out as soon as we see a conflict trampoline in one of the target's
703 // interface table.
704 return false;
705 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000706 } else {
707 DCHECK(invoke_instruction->IsInvokeVirtual());
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000708 new_method = classes->Get(i)->GetEmbeddedVTableEntry(method_index, pointer_size);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000709 }
Nicolas Geoffray4f97a212016-02-25 16:17:54 +0000710 DCHECK(new_method != nullptr);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000711 if (actual_method == nullptr) {
712 actual_method = new_method;
713 } else if (actual_method != new_method) {
714 // Different methods, bailout.
David Sehr709b0702016-10-13 09:12:37 -0700715 VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method)
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000716 << " from inline cache is not inlined because it resolves"
717 << " to different methods";
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000718 return false;
719 }
720 }
721
722 HInstruction* receiver = invoke_instruction->InputAt(0);
723 HInstruction* cursor = invoke_instruction->GetPrevious();
724 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
725
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100726 HInstruction* return_replacement = nullptr;
727 if (!TryBuildAndInline(invoke_instruction, actual_method, &return_replacement)) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000728 return false;
729 }
730
731 // We successfully inlined, now add a guard.
732 HInstanceFieldGet* receiver_class = BuildGetReceiverClass(
733 class_linker, receiver, invoke_instruction->GetDexPc());
734
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000735 Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet())
736 ? Primitive::kPrimLong
737 : Primitive::kPrimInt;
738 HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet(
739 receiver_class,
740 type,
Vladimir Markoa1de9182016-02-25 11:37:38 +0000741 invoke_instruction->IsInvokeVirtual() ? HClassTableGet::TableKind::kVTable
742 : HClassTableGet::TableKind::kIMTable,
Nicolas Geoffray4f97a212016-02-25 16:17:54 +0000743 method_index,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000744 invoke_instruction->GetDexPc());
745
746 HConstant* constant;
747 if (type == Primitive::kPrimLong) {
748 constant = graph_->GetLongConstant(
749 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
750 } else {
751 constant = graph_->GetIntConstant(
752 reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc());
753 }
754
755 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000756 if (cursor != nullptr) {
757 bb_cursor->InsertInstructionAfter(receiver_class, cursor);
758 } else {
759 bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction());
760 }
761 bb_cursor->InsertInstructionAfter(class_table_get, receiver_class);
762 bb_cursor->InsertInstructionAfter(compare, class_table_get);
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100763
764 if (outermost_graph_->IsCompilingOsr()) {
765 CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction);
766 } else {
767 // TODO: Extend reference type propagation to understand the guard.
768 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
769 compare, invoke_instruction->GetDexPc());
770 bb_cursor->InsertInstructionAfter(deoptimize, compare);
771 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
772 if (return_replacement != nullptr) {
773 invoke_instruction->ReplaceWith(return_replacement);
774 }
Nicolas Geoffray1be7cbd2016-04-29 13:56:01 +0100775 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100776 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000777
778 // Run type propagation to get the guard typed.
Vladimir Marko456307a2016-04-19 14:12:13 +0000779 ReferenceTypePropagation rtp_fixup(graph_,
780 outer_compilation_unit_.GetDexCache(),
781 handles_,
782 /* is_first_run */ false);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000783 rtp_fixup.Run();
784
785 MaybeRecordStat(kInlinedPolymorphicCall);
786
787 return true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100788}
789
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000790bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) {
791 HInstruction* return_replacement = nullptr;
792 if (!TryBuildAndInline(invoke_instruction, method, &return_replacement)) {
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000793 if (invoke_instruction->IsInvokeInterface()) {
794 // Turn an invoke-interface into an invoke-virtual. An invoke-virtual is always
795 // better than an invoke-interface because:
796 // 1) In the best case, the interface call has one more indirection (to fetch the IMT).
797 // 2) We will not go to the conflict trampoline with an invoke-virtual.
798 // TODO: Consider sharpening once it is not dependent on the compiler driver.
799 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100800 uint32_t dex_method_index = FindMethodIndexIn(
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000801 method, caller_dex_file, invoke_instruction->GetDexMethodIndex());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100802 if (dex_method_index == DexFile::kDexNoIndex) {
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000803 return false;
804 }
805 HInvokeVirtual* new_invoke = new (graph_->GetArena()) HInvokeVirtual(
806 graph_->GetArena(),
807 invoke_instruction->GetNumberOfArguments(),
808 invoke_instruction->GetType(),
809 invoke_instruction->GetDexPc(),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100810 dex_method_index,
811 method,
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +0000812 method->GetMethodIndex());
813 HInputsRef inputs = invoke_instruction->GetInputs();
814 for (size_t index = 0; index != inputs.size(); ++index) {
815 new_invoke->SetArgumentAt(index, inputs[index]);
816 }
817 invoke_instruction->GetBlock()->InsertInstructionBefore(new_invoke, invoke_instruction);
818 new_invoke->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
819 if (invoke_instruction->GetType() == Primitive::kPrimNot) {
820 new_invoke->SetReferenceTypeInfo(invoke_instruction->GetReferenceTypeInfo());
821 }
822 return_replacement = new_invoke;
823 } else {
824 // TODO: Consider sharpening an invoke virtual once it is not dependent on the
825 // compiler driver.
826 return false;
827 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000828 }
829 if (return_replacement != nullptr) {
830 invoke_instruction->ReplaceWith(return_replacement);
831 }
832 invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction);
David Brazdil94ab38f2016-06-21 17:48:19 +0100833 FixUpReturnReferenceType(method, return_replacement);
834 if (do_rtp && ReturnTypeMoreSpecific(invoke_instruction, return_replacement)) {
835 // Actual return value has a more specific type than the method's declared
836 // return type. Run RTP again on the outer graph to propagate it.
837 ReferenceTypePropagation(graph_,
838 outer_compilation_unit_.GetDexCache(),
839 handles_,
840 /* is_first_run */ false).Run();
841 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000842 return true;
843}
844
845bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction,
846 ArtMethod* method,
847 HInstruction** return_replacement) {
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100848 if (method->IsProxyMethod()) {
David Sehr709b0702016-10-13 09:12:37 -0700849 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100850 << " is not inlined because of unimplemented inline support for proxy methods.";
851 return false;
852 }
853
Jeff Haodcdc85b2015-12-04 14:06:18 -0800854 // Check whether we're allowed to inline. The outermost compilation unit is the relevant
855 // dex file here (though the transitivity of an inline chain would allow checking the calller).
856 if (!compiler_driver_->MayInline(method->GetDexFile(),
857 outer_compilation_unit_.GetDexFile())) {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000858 if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) {
David Sehr709b0702016-10-13 09:12:37 -0700859 VLOG(compiler) << "Successfully replaced pattern of invoke "
860 << method->PrettyMethod();
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000861 MaybeRecordStat(kReplacedInvokeWithSimplePattern);
862 return true;
863 }
David Sehr709b0702016-10-13 09:12:37 -0700864 VLOG(compiler) << "Won't inline " << method->PrettyMethod() << " in "
Jeff Haodcdc85b2015-12-04 14:06:18 -0800865 << outer_compilation_unit_.GetDexFile()->GetLocation() << " ("
866 << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from "
867 << method->GetDexFile()->GetLocation();
868 return false;
869 }
870
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100871 bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile());
872
873 const DexFile::CodeItem* code_item = method->GetCodeItem();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000874
875 if (code_item == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700876 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000877 << " is not inlined because it is native";
878 return false;
879 }
880
Calin Juravleec748352015-07-29 13:52:12 +0100881 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
882 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
David Sehr709b0702016-10-13 09:12:37 -0700883 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000884 << " is too big to inline: "
885 << code_item->insns_size_in_code_units_
886 << " > "
887 << inline_max_code_units;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000888 return false;
889 }
890
891 if (code_item->tries_size_ != 0) {
David Sehr709b0702016-10-13 09:12:37 -0700892 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000893 << " is not inlined because of try block";
894 return false;
895 }
896
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100897 if (!method->IsCompilable()) {
David Sehr709b0702016-10-13 09:12:37 -0700898 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100899 << " has soft failures un-handled by the compiler, so it cannot be inlined";
900 }
901
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100902 if (!method->GetDeclaringClass()->IsVerified()) {
903 uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex();
Calin Juravleffc87072016-04-20 14:22:09 +0100904 if (Runtime::Current()->UseJitCompilation() ||
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000905 !compiler_driver_->IsMethodVerifiedWithoutFailures(
906 method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) {
David Sehr709b0702016-10-13 09:12:37 -0700907 VLOG(compiler) << "Method " << method->PrettyMethod()
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100908 << " couldn't be verified, so it cannot be inlined";
909 return false;
910 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000911 }
912
Roland Levillain4c0eb422015-04-24 16:43:49 +0100913 if (invoke_instruction->IsInvokeStaticOrDirect() &&
914 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
915 // Case of a static method that cannot be inlined because it implicitly
916 // requires an initialization check of its declaring class.
David Sehr709b0702016-10-13 09:12:37 -0700917 VLOG(compiler) << "Method " << method->PrettyMethod()
Roland Levillain4c0eb422015-04-24 16:43:49 +0100918 << " is not inlined because it is static and requires a clinit"
919 << " check that cannot be emitted due to Dex cache limitations";
920 return false;
921 }
922
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000923 if (!TryBuildAndInlineHelper(invoke_instruction, method, same_dex_file, return_replacement)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000924 return false;
925 }
926
David Sehr709b0702016-10-13 09:12:37 -0700927 VLOG(compiler) << "Successfully inlined " << method->PrettyMethod();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000928 MaybeRecordStat(kInlinedInvoke);
929 return true;
930}
931
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000932static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction,
933 size_t arg_vreg_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700934 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000935 size_t input_index = 0;
936 for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) {
937 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
938 if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) {
939 ++i;
940 DCHECK_NE(i, arg_vreg_index);
941 }
942 }
943 DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments());
944 return invoke_instruction->InputAt(input_index);
945}
946
947// Try to recognize known simple patterns and replace invoke call with appropriate instructions.
948bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction,
949 ArtMethod* resolved_method,
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000950 HInstruction** return_replacement) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000951 InlineMethod inline_method;
952 if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) {
953 return false;
954 }
955
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000956 switch (inline_method.opcode) {
957 case kInlineOpNop:
958 DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000959 *return_replacement = nullptr;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000960 break;
961 case kInlineOpReturnArg:
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000962 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction,
963 inline_method.d.return_data.arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000964 break;
965 case kInlineOpNonWideConst:
966 if (resolved_method->GetShorty()[0] == 'L') {
967 DCHECK_EQ(inline_method.d.data, 0u);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000968 *return_replacement = graph_->GetNullConstant();
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000969 } else {
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000970 *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000971 }
972 break;
973 case kInlineOpIGet: {
974 const InlineIGetIPutData& data = inline_method.d.ifield_data;
975 if (data.method_is_static || data.object_arg != 0u) {
976 // TODO: Needs null check.
977 return false;
978 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000979 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000980 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000981 HInstanceFieldGet* iget = CreateInstanceFieldGet(dex_cache, data.field_idx, obj);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000982 DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset);
983 DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile);
984 invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000985 *return_replacement = iget;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000986 break;
987 }
988 case kInlineOpIPut: {
989 const InlineIGetIPutData& data = inline_method.d.ifield_data;
990 if (data.method_is_static || data.object_arg != 0u) {
991 // TODO: Needs null check.
992 return false;
993 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000994 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000995 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg);
996 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg);
Vladimir Marko354efa62016-02-04 19:46:56 +0000997 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, data.field_idx, obj, value);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000998 DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset);
999 DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile);
1000 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
1001 if (data.return_arg_plus1 != 0u) {
1002 size_t return_arg = data.return_arg_plus1 - 1u;
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001003 *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001004 }
1005 break;
1006 }
Vladimir Marko354efa62016-02-04 19:46:56 +00001007 case kInlineOpConstructor: {
1008 const InlineConstructorData& data = inline_method.d.constructor_data;
1009 // Get the indexes to arrays for easier processing.
1010 uint16_t iput_field_indexes[] = {
1011 data.iput0_field_index, data.iput1_field_index, data.iput2_field_index
1012 };
1013 uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg };
1014 static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch");
1015 // Count valid field indexes.
1016 size_t number_of_iputs = 0u;
1017 while (number_of_iputs != arraysize(iput_field_indexes) &&
1018 iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) {
1019 // Check that there are no duplicate valid field indexes.
1020 DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1,
1021 iput_field_indexes + arraysize(iput_field_indexes),
1022 iput_field_indexes[number_of_iputs]));
1023 ++number_of_iputs;
1024 }
1025 // Check that there are no valid field indexes in the rest of the array.
1026 DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs,
1027 iput_field_indexes + arraysize(iput_field_indexes),
1028 [](uint16_t index) { return index != DexFile::kDexNoIndex16; }));
1029
1030 // Create HInstanceFieldSet for each IPUT that stores non-zero data.
1031 Handle<mirror::DexCache> dex_cache;
1032 HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u);
1033 bool needs_constructor_barrier = false;
1034 for (size_t i = 0; i != number_of_iputs; ++i) {
1035 HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]);
Roland Levillain1a653882016-03-18 18:05:57 +00001036 if (!value->IsConstant() || !value->AsConstant()->IsZeroBitPattern()) {
Vladimir Marko354efa62016-02-04 19:46:56 +00001037 if (dex_cache.GetReference() == nullptr) {
1038 dex_cache = handles_->NewHandle(resolved_method->GetDexCache());
1039 }
1040 uint16_t field_index = iput_field_indexes[i];
1041 HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, field_index, obj, value);
1042 invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction);
1043
1044 // Check whether the field is final. If it is, we need to add a barrier.
Andreas Gampe542451c2016-07-26 09:02:02 -07001045 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Marko354efa62016-02-04 19:46:56 +00001046 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
1047 DCHECK(resolved_field != nullptr);
1048 if (resolved_field->IsFinal()) {
1049 needs_constructor_barrier = true;
1050 }
1051 }
1052 }
1053 if (needs_constructor_barrier) {
1054 HMemoryBarrier* barrier = new (graph_->GetArena()) HMemoryBarrier(kStoreStore, kNoDexPc);
1055 invoke_instruction->GetBlock()->InsertInstructionBefore(barrier, invoke_instruction);
1056 }
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001057 *return_replacement = nullptr;
Vladimir Marko354efa62016-02-04 19:46:56 +00001058 break;
1059 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001060 default:
1061 LOG(FATAL) << "UNREACHABLE";
1062 UNREACHABLE();
1063 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001064 return true;
1065}
1066
Vladimir Marko354efa62016-02-04 19:46:56 +00001067HInstanceFieldGet* HInliner::CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001068 uint32_t field_index,
1069 HInstruction* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001070 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001071 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001072 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
1073 DCHECK(resolved_field != nullptr);
1074 HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet(
1075 obj,
1076 resolved_field->GetTypeAsPrimitiveType(),
1077 resolved_field->GetOffset(),
1078 resolved_field->IsVolatile(),
1079 field_index,
1080 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +00001081 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001082 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +00001083 // Read barrier generates a runtime call in slow path and we need a valid
1084 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
1085 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001086 if (iget->GetType() == Primitive::kPrimNot) {
Vladimir Marko456307a2016-04-19 14:12:13 +00001087 // Use the same dex_cache that we used for field lookup as the hint_dex_cache.
1088 ReferenceTypePropagation rtp(graph_, dex_cache, handles_, /* is_first_run */ false);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001089 rtp.Visit(iget);
1090 }
1091 return iget;
1092}
1093
Vladimir Marko354efa62016-02-04 19:46:56 +00001094HInstanceFieldSet* HInliner::CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache,
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001095 uint32_t field_index,
1096 HInstruction* obj,
1097 HInstruction* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001098 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001099 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001100 ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size);
1101 DCHECK(resolved_field != nullptr);
1102 HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet(
1103 obj,
1104 value,
1105 resolved_field->GetTypeAsPrimitiveType(),
1106 resolved_field->GetOffset(),
1107 resolved_field->IsVolatile(),
1108 field_index,
1109 resolved_field->GetDeclaringClass()->GetDexClassDefIndex(),
Vladimir Marko354efa62016-02-04 19:46:56 +00001110 *dex_cache->GetDexFile(),
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001111 dex_cache,
Vladimir Markoadda4352016-01-29 10:24:41 +00001112 // Read barrier generates a runtime call in slow path and we need a valid
1113 // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537.
1114 /* dex_pc */ 0);
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001115 return iput;
1116}
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001117
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001118bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction,
1119 ArtMethod* resolved_method,
1120 bool same_dex_file,
1121 HInstruction** return_replacement) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +00001122 ScopedObjectAccess soa(Thread::Current());
1123 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001124 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
1125 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +00001126 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -07001127 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffrayf1aedb12016-07-28 03:49:14 +01001128 Handle<mirror::ClassLoader> class_loader(handles_->NewHandle(
1129 resolved_method->GetDeclaringClass()->GetClassLoader()));
1130
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001131 DexCompilationUnit dex_compilation_unit(
Nicolas Geoffrayf1aedb12016-07-28 03:49:14 +01001132 class_loader.ToJObject(),
Nicolas Geoffray5b82d332016-02-18 14:22:32 +00001133 class_linker,
1134 callee_dex_file,
1135 code_item,
1136 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
1137 method_index,
1138 resolved_method->GetAccessFlags(),
1139 /* verified_method */ nullptr,
1140 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001141
Calin Juravle3cd4fc82015-05-14 15:15:42 +01001142 bool requires_ctor_barrier = false;
1143
1144 if (dex_compilation_unit.IsConstructor()) {
1145 // If it's a super invocation and we already generate a barrier there's no need
1146 // to generate another one.
1147 // We identify super calls by looking at the "this" pointer. If its value is the
1148 // same as the local "this" pointer then we must have a super invocation.
1149 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
1150 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
1151 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
1152 requires_ctor_barrier = false;
1153 } else {
1154 Thread* self = Thread::Current();
1155 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
1156 dex_compilation_unit.GetDexFile(),
1157 dex_compilation_unit.GetClassDefIndex());
1158 }
1159 }
1160
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001161 InvokeType invoke_type = invoke_instruction->GetInvokeType();
Nicolas Geoffray35071052015-06-09 15:43:38 +01001162 if (invoke_type == kInterface) {
1163 // We have statically resolved the dispatch. To please the class linker
1164 // at runtime, we change this call as if it was a virtual call.
1165 invoke_type = kVirtual;
1166 }
David Brazdil3f523062016-02-29 16:53:33 +00001167
1168 const int32_t caller_instruction_counter = graph_->GetCurrentInstructionId();
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00001169 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001170 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +01001171 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001172 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +01001173 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001174 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +01001175 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001176 graph_->IsDebuggable(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001177 /* osr */ false,
David Brazdil3f523062016-02-29 16:53:33 +00001178 caller_instruction_counter);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001179 callee_graph->SetArtMethod(resolved_method);
David Brazdil5e8b1372015-01-23 14:39:08 +00001180
Roland Levillaina8013fd2016-04-04 15:34:31 +01001181 // When they are needed, allocate `inline_stats` on the heap instead
1182 // of on the stack, as Clang might produce a stack frame too large
1183 // for this function, that would not fit the requirements of the
1184 // `-Wframe-larger-than` option.
1185 std::unique_ptr<OptimizingCompilerStats> inline_stats =
1186 (stats_ == nullptr) ? nullptr : MakeUnique<OptimizingCompilerStats>();
David Brazdil5e8b1372015-01-23 14:39:08 +00001187 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001188 &dex_compilation_unit,
1189 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001190 resolved_method->GetDexFile(),
David Brazdil86ea7ee2016-02-16 09:26:07 +00001191 *code_item,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001192 compiler_driver_,
Roland Levillaina8013fd2016-04-04 15:34:31 +01001193 inline_stats.get(),
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001194 resolved_method->GetQuickenedInfo(class_linker->GetImagePointerSize()),
David Brazdildee58d62016-04-07 09:54:26 +00001195 dex_cache,
1196 handles_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001197
David Brazdildee58d62016-04-07 09:54:26 +00001198 if (builder.BuildGraph() != kAnalysisSuccess) {
David Sehr709b0702016-10-13 09:12:37 -07001199 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001200 << " could not be built, so cannot be inlined";
1201 return false;
1202 }
1203
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001204 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
1205 compiler_driver_->GetInstructionSet())) {
David Sehr709b0702016-10-13 09:12:37 -07001206 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffray259136f2014-12-17 23:21:58 +00001207 << " cannot be inlined because of the register allocator";
1208 return false;
1209 }
1210
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001211 size_t parameter_index = 0;
1212 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
1213 !instructions.Done();
1214 instructions.Advance()) {
1215 HInstruction* current = instructions.Current();
1216 if (current->IsParameterValue()) {
1217 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
1218 if (argument->IsNullConstant()) {
1219 current->ReplaceWith(callee_graph->GetNullConstant());
1220 } else if (argument->IsIntConstant()) {
1221 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
1222 } else if (argument->IsLongConstant()) {
1223 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
1224 } else if (argument->IsFloatConstant()) {
1225 current->ReplaceWith(
1226 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
1227 } else if (argument->IsDoubleConstant()) {
1228 current->ReplaceWith(
1229 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
1230 } else if (argument->GetType() == Primitive::kPrimNot) {
1231 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
1232 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
1233 }
1234 }
1235 }
1236
David Brazdil94ab38f2016-06-21 17:48:19 +01001237 // We have replaced formal arguments with actual arguments. If actual types
1238 // are more specific than the declared ones, run RTP again on the inner graph.
1239 if (ArgumentTypesMoreSpecific(invoke_instruction, resolved_method)) {
1240 ReferenceTypePropagation(callee_graph,
1241 dex_compilation_unit.GetDexCache(),
1242 handles_,
1243 /* is_first_run */ false).Run();
1244 }
1245
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001246 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Roland Levillaina3aef2e2016-04-06 17:45:58 +01001247 size_t number_of_inlined_instructions =
1248 RunOptimizations(callee_graph, code_item, dex_compilation_unit);
1249 number_of_instructions_budget += number_of_inlined_instructions;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +00001250
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001251 // TODO: We should abort only if all predecessors throw. However,
1252 // HGraph::InlineInto currently does not handle an exit block with
1253 // a throw predecessor.
1254 HBasicBlock* exit_block = callee_graph->GetExitBlock();
1255 if (exit_block == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001256 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001257 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001258 return false;
1259 }
1260
1261 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +00001262 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
1263 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001264 has_throw_predecessor = true;
1265 break;
1266 }
1267 }
1268 if (has_throw_predecessor) {
David Sehr709b0702016-10-13 09:12:37 -07001269 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001270 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001271 return false;
1272 }
1273
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001274 size_t number_of_instructions = 0;
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001275
1276 bool can_inline_environment =
1277 total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters;
1278
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001279 // Skip the entry block, it does not contain instructions that prevent inlining.
1280 for (HBasicBlock* block : callee_graph->GetReversePostOrderSkipEntryBlock()) {
David Sehrc757dec2016-11-04 15:48:34 -07001281 if (block->IsLoopHeader()) {
1282 if (block->GetLoopInformation()->IsIrreducible()) {
1283 // Don't inline methods with irreducible loops, they could prevent some
1284 // optimizations to run.
1285 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
1286 << " could not be inlined because it contains an irreducible loop";
1287 return false;
1288 }
1289 if (!block->GetLoopInformation()->HasExitEdge()) {
1290 // Don't inline methods with loops without exit, since they cause the
1291 // loop information to be computed incorrectly when updating after
1292 // inlining.
1293 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
1294 << " could not be inlined because it contains a loop with no exit";
1295 return false;
1296 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001297 }
1298
1299 for (HInstructionIterator instr_it(block->GetInstructions());
1300 !instr_it.Done();
1301 instr_it.Advance()) {
Roland Levillaina3aef2e2016-04-06 17:45:58 +01001302 if (number_of_instructions++ == number_of_instructions_budget) {
David Sehr709b0702016-10-13 09:12:37 -07001303 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001304 << " is not inlined because its caller has reached"
1305 << " its instruction budget limit.";
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001306 return false;
1307 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001308 HInstruction* current = instr_it.Current();
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001309 if (!can_inline_environment && current->NeedsEnvironment()) {
David Sehr709b0702016-10-13 09:12:37 -07001310 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffray5949fa02015-12-18 10:57:10 +00001311 << " is not inlined because its caller has reached"
1312 << " its environment budget limit.";
1313 return false;
1314 }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001315
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001316 if (!same_dex_file && current->NeedsEnvironment()) {
David Sehr709b0702016-10-13 09:12:37 -07001317 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001318 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001319 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001320 return false;
1321 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001322
Vladimir Markodc151b22015-10-15 18:02:30 +01001323 if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001324 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001325 << " could not be inlined because " << current->DebugName()
1326 << " it is in a different dex file and requires access to the dex cache";
1327 return false;
1328 }
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001329
1330 if (current->IsNewInstance() &&
1331 (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
David Sehr709b0702016-10-13 09:12:37 -07001332 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001333 << " could not be inlined because it is using an entrypoint"
1334 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001335 // Allocation entrypoint does not handle inlined frames.
1336 return false;
1337 }
1338
1339 if (current->IsNewArray() &&
1340 (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
David Sehr709b0702016-10-13 09:12:37 -07001341 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001342 << " could not be inlined because it is using an entrypoint"
1343 << " with access checks";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001344 // Allocation entrypoint does not handle inlined frames.
1345 return false;
1346 }
1347
1348 if (current->IsUnresolvedStaticFieldGet() ||
1349 current->IsUnresolvedInstanceFieldGet() ||
1350 current->IsUnresolvedStaticFieldSet() ||
1351 current->IsUnresolvedInstanceFieldSet()) {
1352 // Entrypoint for unresolved fields does not handle inlined frames.
David Sehr709b0702016-10-13 09:12:37 -07001353 VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index)
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +00001354 << " could not be inlined because it is using an unresolved"
1355 << " entrypoint";
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00001356 return false;
1357 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001358 }
1359 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07001360 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001361
David Brazdil3f523062016-02-29 16:53:33 +00001362 DCHECK_EQ(caller_instruction_counter, graph_->GetCurrentInstructionId())
1363 << "No instructions can be added to the outer graph while inner graph is being built";
1364
1365 const int32_t callee_instruction_counter = callee_graph->GetCurrentInstructionId();
1366 graph_->SetCurrentInstructionId(callee_instruction_counter);
Nicolas Geoffray55bd7492016-02-16 15:37:12 +00001367 *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
David Brazdil3f523062016-02-29 16:53:33 +00001368
1369 DCHECK_EQ(callee_instruction_counter, callee_graph->GetCurrentInstructionId())
1370 << "No instructions can be added to the inner graph during inlining into the outer graph";
1371
Vladimir Markobe10e8e2016-01-22 12:09:44 +00001372 return true;
1373}
Calin Juravle2e768302015-07-28 14:41:11 +00001374
Roland Levillaina3aef2e2016-04-06 17:45:58 +01001375size_t HInliner::RunOptimizations(HGraph* callee_graph,
1376 const DexFile::CodeItem* code_item,
1377 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray93a18c52016-04-22 13:16:14 +01001378 // Note: if the outermost_graph_ is being compiled OSR, we should not run any
1379 // optimization that could lead to a HDeoptimize. The following optimizations do not.
Andreas Gampeca620d72016-11-08 08:09:33 -08001380 HDeadCodeElimination dce(callee_graph, stats_, "dead_code_elimination$inliner");
1381 HConstantFolding fold(callee_graph, "constant_folding$inliner");
Roland Levillaina3aef2e2016-04-06 17:45:58 +01001382 HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_);
1383 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffray762869d2016-07-15 15:28:35 +01001384 IntrinsicsRecognizer intrinsics(callee_graph, stats_);
Roland Levillaina3aef2e2016-04-06 17:45:58 +01001385
1386 HOptimization* optimizations[] = {
1387 &intrinsics,
1388 &sharpening,
1389 &simplify,
1390 &fold,
1391 &dce,
1392 };
1393
1394 for (size_t i = 0; i < arraysize(optimizations); ++i) {
1395 HOptimization* optimization = optimizations[i];
1396 optimization->Run();
1397 }
1398
1399 size_t number_of_inlined_instructions = 0u;
1400 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
1401 HInliner inliner(callee_graph,
1402 outermost_graph_,
1403 codegen_,
1404 outer_compilation_unit_,
1405 dex_compilation_unit,
1406 compiler_driver_,
1407 handles_,
1408 stats_,
1409 total_number_of_dex_registers_ + code_item->registers_size_,
1410 depth_ + 1);
1411 inliner.Run();
1412 number_of_inlined_instructions += inliner.number_of_inlined_instructions_;
1413 }
1414
1415 return number_of_inlined_instructions;
1416}
1417
David Brazdil94ab38f2016-06-21 17:48:19 +01001418static bool IsReferenceTypeRefinement(ReferenceTypeInfo declared_rti,
1419 bool declared_can_be_null,
1420 HInstruction* actual_obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001421 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +01001422 if (declared_can_be_null && !actual_obj->CanBeNull()) {
1423 return true;
1424 }
1425
1426 ReferenceTypeInfo actual_rti = actual_obj->GetReferenceTypeInfo();
1427 return (actual_rti.IsExact() && !declared_rti.IsExact()) ||
1428 declared_rti.IsStrictSupertypeOf(actual_rti);
1429}
1430
1431ReferenceTypeInfo HInliner::GetClassRTI(mirror::Class* klass) {
1432 return ReferenceTypePropagation::IsAdmissible(klass)
1433 ? ReferenceTypeInfo::Create(handles_->NewHandle(klass))
1434 : graph_->GetInexactObjectRti();
1435}
1436
1437bool HInliner::ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) {
1438 // If this is an instance call, test whether the type of the `this` argument
1439 // is more specific than the class which declares the method.
1440 if (!resolved_method->IsStatic()) {
1441 if (IsReferenceTypeRefinement(GetClassRTI(resolved_method->GetDeclaringClass()),
1442 /* declared_can_be_null */ false,
1443 invoke_instruction->InputAt(0u))) {
1444 return true;
1445 }
1446 }
1447
Andreas Gampe542451c2016-07-26 09:02:02 -07001448 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
David Brazdil94ab38f2016-06-21 17:48:19 +01001449
1450 // Iterate over the list of parameter types and test whether any of the
1451 // actual inputs has a more specific reference type than the type declared in
1452 // the signature.
1453 const DexFile::TypeList* param_list = resolved_method->GetParameterTypeList();
1454 for (size_t param_idx = 0,
1455 input_idx = resolved_method->IsStatic() ? 0 : 1,
1456 e = (param_list == nullptr ? 0 : param_list->Size());
1457 param_idx < e;
1458 ++param_idx, ++input_idx) {
1459 HInstruction* input = invoke_instruction->InputAt(input_idx);
1460 if (input->GetType() == Primitive::kPrimNot) {
1461 mirror::Class* param_cls = resolved_method->GetDexCacheResolvedType(
1462 param_list->GetTypeItem(param_idx).type_idx_,
1463 pointer_size);
1464 if (IsReferenceTypeRefinement(GetClassRTI(param_cls),
1465 /* declared_can_be_null */ true,
1466 input)) {
1467 return true;
1468 }
1469 }
1470 }
1471
1472 return false;
1473}
1474
1475bool HInliner::ReturnTypeMoreSpecific(HInvoke* invoke_instruction,
1476 HInstruction* return_replacement) {
Alex Light68289a52015-12-15 17:30:30 -08001477 // Check the integrity of reference types and run another type propagation if needed.
David Brazdil4833f5a2015-12-16 10:37:39 +00001478 if (return_replacement != nullptr) {
1479 if (return_replacement->GetType() == Primitive::kPrimNot) {
David Brazdil94ab38f2016-06-21 17:48:19 +01001480 // Test if the return type is a refinement of the declared return type.
1481 if (IsReferenceTypeRefinement(invoke_instruction->GetReferenceTypeInfo(),
1482 /* declared_can_be_null */ true,
1483 return_replacement)) {
1484 return true;
1485 }
1486 } else if (return_replacement->IsInstanceOf()) {
1487 // Inlining InstanceOf into an If may put a tighter bound on reference types.
1488 return true;
1489 }
1490 }
1491
1492 return false;
1493}
1494
1495void HInliner::FixUpReturnReferenceType(ArtMethod* resolved_method,
1496 HInstruction* return_replacement) {
1497 if (return_replacement != nullptr) {
1498 if (return_replacement->GetType() == Primitive::kPrimNot) {
David Brazdil4833f5a2015-12-16 10:37:39 +00001499 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
1500 // Make sure that we have a valid type for the return. We may get an invalid one when
1501 // we inline invokes with multiple branches and create a Phi for the result.
1502 // TODO: we could be more precise by merging the phi inputs but that requires
1503 // some functionality from the reference type propagation.
1504 DCHECK(return_replacement->IsPhi());
Andreas Gampe542451c2016-07-26 09:02:02 -07001505 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +00001506 mirror::Class* cls = resolved_method->GetReturnType(false /* resolve */, pointer_size);
David Brazdil94ab38f2016-06-21 17:48:19 +01001507 return_replacement->SetReferenceTypeInfo(GetClassRTI(cls));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001508 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +00001509 }
Calin Juravle2e768302015-07-28 14:41:11 +00001510 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001511}
1512
1513} // namespace art