blob: 0e50416a9e9a0327ced41f2372c01627c2b54d5d [file] [log] [blame]
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "inliner.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000020#include "builder.h"
21#include "class_linker.h"
22#include "constant_folding.h"
23#include "dead_code_elimination.h"
24#include "driver/compiler_driver-inl.h"
Calin Juravleec748352015-07-29 13:52:12 +010025#include "driver/compiler_options.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "driver/dex_compilation_unit.h"
27#include "instruction_simplifier.h"
Scott Wakelingd60a1af2015-07-22 14:32:44 +010028#include "intrinsics.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000029#include "mirror/class_loader.h"
30#include "mirror/dex_cache.h"
31#include "nodes.h"
Nicolas Geoffray335005e2015-06-25 10:01:47 +010032#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010033#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000034#include "register_allocator.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010035#include "sharpening.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000036#include "ssa_builder.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000037#include "ssa_phi_elimination.h"
38#include "scoped_thread_state_change.h"
39#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010040#include "dex/verified_method.h"
41#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000042
43namespace art {
44
Nicolas Geoffrayb17d1cc2015-12-17 15:26:21 +000045static constexpr size_t kMaximumNumberOfHInstructions = 12;
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070046
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000047void HInliner::Run() {
Calin Juravle8f96df82015-07-29 15:58:48 +010048 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
49 if ((compiler_options.GetInlineDepthLimit() == 0)
50 || (compiler_options.GetInlineMaxCodeUnits() == 0)) {
51 return;
52 }
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000053 if (graph_->IsDebuggable()) {
54 // For simplicity, we currently never inline when the graph is debuggable. This avoids
55 // doing some logic in the runtime to discover if a method could have been inlined.
56 return;
57 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010058 const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
59 DCHECK(!blocks.empty());
60 HBasicBlock* next_block = blocks[0];
61 for (size_t i = 0; i < blocks.size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010062 // Because we are changing the graph when inlining, we need to remember the next block.
63 // This avoids doing the inlining work again on the inlined blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010064 if (blocks[i] != next_block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010065 continue;
66 }
67 HBasicBlock* block = next_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +010068 next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1];
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000069 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
70 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010071 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070072 // As long as the call is not intrinsified, it is worth trying to inline.
73 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000074 // We use the original invoke type to ensure the resolution of the called method
75 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000076 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010077 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000078 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000079 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 bool should_inline = callee_name.find("$inline$") != std::string::npos;
81 CHECK(!should_inline) << "Could not inline " << callee_name;
82 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010083 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010084 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010085 std::string callee_name =
86 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
87 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
88 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
89 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000090 }
91 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000092 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000093 }
94 }
95}
96
Nicolas Geoffray454a4812015-06-09 10:37:32 +010097static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010099 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
100}
101
102/**
103 * Given the `resolved_method` looked up in the dex cache, try to find
104 * the actual runtime target of an interface or virtual call.
105 * Return nullptr if the runtime target cannot be proven.
106 */
107static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100109 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
110 // No need to lookup further, the resolved method will be the target.
111 return resolved_method;
112 }
113
114 HInstruction* receiver = invoke->InputAt(0);
115 if (receiver->IsNullCheck()) {
116 // Due to multiple levels of inlining within the same pass, it might be that
117 // null check does not have the reference type of the actual receiver.
118 receiver = receiver->InputAt(0);
119 }
120 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000121 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
122 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100123 // We currently only support inlining with known receivers.
124 // TODO: Remove this check, we should be able to inline final methods
125 // on unknown receivers.
126 return nullptr;
127 } else if (info.GetTypeHandle()->IsInterface()) {
128 // Statically knowing that the receiver has an interface type cannot
129 // help us find what is the target method.
130 return nullptr;
131 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
132 // The method that we're trying to call is not in the receiver's class or super classes.
133 return nullptr;
134 }
135
136 ClassLinker* cl = Runtime::Current()->GetClassLinker();
137 size_t pointer_size = cl->GetImagePointerSize();
138 if (invoke->IsInvokeInterface()) {
139 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
140 resolved_method, pointer_size);
141 } else {
142 DCHECK(invoke->IsInvokeVirtual());
143 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
144 resolved_method, pointer_size);
145 }
146
147 if (resolved_method == nullptr) {
148 // The information we had on the receiver was not enough to find
149 // the target method. Since we check above the exact type of the receiver,
150 // the only reason this can happen is an IncompatibleClassChangeError.
151 return nullptr;
Alex Light9139e002015-10-09 15:59:48 -0700152 } else if (!resolved_method->IsInvokable()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100153 // The information we had on the receiver was not enough to find
154 // the target method. Since we check above the exact type of the receiver,
155 // the only reason this can happen is an IncompatibleClassChangeError.
156 return nullptr;
157 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
158 // A final method has to be the target method.
159 return resolved_method;
160 } else if (info.IsExact()) {
161 // If we found a method and the receiver's concrete type is statically
162 // known, we know for sure the target.
163 return resolved_method;
164 } else {
165 // Even if we did find a method, the receiver type was not enough to
166 // statically find the runtime target.
167 return nullptr;
168 }
169}
170
171static uint32_t FindMethodIndexIn(ArtMethod* method,
172 const DexFile& dex_file,
173 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100175 if (IsSameDexFile(*method->GetDexFile(), dex_file)) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100176 return method->GetDexMethodIndex();
177 } else {
178 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
179 }
180}
181
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100182static uint32_t FindClassIndexIn(mirror::Class* cls, const DexFile& dex_file)
183 SHARED_REQUIRES(Locks::mutator_lock_) {
184 if (cls->GetDexCache() == nullptr) {
185 DCHECK(cls->IsArrayClass());
186 // TODO: find the class in `dex_file`.
187 return DexFile::kDexNoIndex;
188 } else if (cls->GetDexTypeIndex() == DexFile::kDexNoIndex16) {
189 // TODO: deal with proxy classes.
190 return DexFile::kDexNoIndex;
191 } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) {
192 // Update the dex cache to ensure the class is in. The generated code will
193 // consider it is. We make it safe by updating the dex cache, as other
194 // dex files might also load the class, and there is no guarantee the dex
195 // cache of the dex file of the class will be updated.
196 if (cls->GetDexCache()->GetResolvedType(cls->GetDexTypeIndex()) == nullptr) {
197 cls->GetDexCache()->SetResolvedType(cls->GetDexTypeIndex(), cls);
198 }
199 return cls->GetDexTypeIndex();
200 } else {
201 // TODO: find the class in `dex_file`.
202 return DexFile::kDexNoIndex;
203 }
204}
205
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700206bool HInliner::TryInline(HInvoke* invoke_instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +0100207 if (invoke_instruction->IsInvokeUnresolved()) {
208 return false; // Don't bother to move further if we know the method is unresolved.
209 }
210
Vladimir Marko58155012015-08-19 12:49:41 +0000211 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000213 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
214 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215
Nicolas Geoffray35071052015-06-09 15:43:38 +0100216 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
217 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000218 ArtMethod* resolved_method;
219 if (invoke_instruction->IsInvokeStaticOrDirect()) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000220 if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) {
221 VLOG(compiler) << "Not inlining a String.<init> method";
222 return false;
223 }
Vladimir Marko58155012015-08-19 12:49:41 +0000224 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700225 mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file)
226 ? caller_compilation_unit_.GetDexCache().Get()
227 : class_linker->FindDexCache(soa.Self(), *ref.dex_file);
228 resolved_method = dex_cache->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000229 ref.dex_method_index, class_linker->GetImagePointerSize());
230 } else {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700231 resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000232 method_index, class_linker->GetImagePointerSize());
233 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 if (resolved_method == nullptr) {
Calin Juravle175dc732015-08-25 15:42:32 +0100236 // TODO: Can this still happen?
Nicolas Geoffray35071052015-06-09 15:43:38 +0100237 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000238 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 return false;
240 }
241
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100242 if (invoke_instruction->IsInvokeStaticOrDirect()) {
243 return TryInline(invoke_instruction, resolved_method);
244 }
245
246 // Check if we can statically find the method.
247 ArtMethod* actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
248 if (actual_method != nullptr) {
249 return TryInline(invoke_instruction, actual_method);
250 }
251
252 // Check if we can use an inline cache.
253 ArtMethod* caller = graph_->GetArtMethod();
254 size_t pointer_size = class_linker->GetImagePointerSize();
255 // Under JIT, we should always know the caller.
256 DCHECK(!Runtime::Current()->UseJit() || (caller != nullptr));
257 if (caller != nullptr && caller->GetProfilingInfo(pointer_size) != nullptr) {
258 ProfilingInfo* profiling_info = caller->GetProfilingInfo(pointer_size);
259 const InlineCache& ic = *profiling_info->GetInlineCache(invoke_instruction->GetDexPc());
260 if (ic.IsUnitialized()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100261 VLOG(compiler) << "Interface or virtual call to "
262 << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100263 << " is not hit and not inlined";
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100264 return false;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100265 } else if (ic.IsMonomorphic()) {
266 MaybeRecordStat(kMonomorphicCall);
267 return TryInlineMonomorphicCall(invoke_instruction, resolved_method, ic);
268 } else if (ic.IsPolymorphic()) {
269 MaybeRecordStat(kPolymorphicCall);
270 return TryInlinePolymorphicCall(invoke_instruction, resolved_method, ic);
271 } else {
272 DCHECK(ic.IsMegamorphic());
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100273 VLOG(compiler) << "Interface or virtual call to "
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100274 << PrettyMethod(method_index, caller_dex_file)
275 << " is megamorphic and not inlined";
276 MaybeRecordStat(kMegamorphicCall);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100277 return false;
278 }
279 }
280
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100281 VLOG(compiler) << "Interface or virtual call to "
282 << PrettyMethod(method_index, caller_dex_file)
283 << " could not be statically determined";
284 return false;
285}
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000286
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100287bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction,
288 ArtMethod* resolved_method,
289 const InlineCache& ic) {
290 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
291 uint32_t class_index = FindClassIndexIn(ic.GetMonomorphicType(), caller_dex_file);
292 if (class_index == DexFile::kDexNoIndex) {
293 VLOG(compiler) << "Call to " << PrettyMethod(resolved_method)
294 << " from inline cache is not inlined because its class is not"
295 << " accessible to the caller";
296 return false;
297 }
298
299 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
300 size_t pointer_size = class_linker->GetImagePointerSize();
301 if (invoke_instruction->IsInvokeInterface()) {
302 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForInterface(
303 resolved_method, pointer_size);
304 } else {
305 DCHECK(invoke_instruction->IsInvokeVirtual());
306 resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForVirtual(
307 resolved_method, pointer_size);
308 }
309 DCHECK(resolved_method != nullptr);
310 HInstruction* receiver = invoke_instruction->InputAt(0);
311 HInstruction* cursor = invoke_instruction->GetPrevious();
312 HBasicBlock* bb_cursor = invoke_instruction->GetBlock();
313
314 if (!TryInline(invoke_instruction, resolved_method, /* do_rtp */ false)) {
315 return false;
316 }
317
318 // We successfully inlined, now add a guard.
319 ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0);
320 DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_");
321 HInstanceFieldGet* field_get = new (graph_->GetArena()) HInstanceFieldGet(
322 receiver,
323 Primitive::kPrimNot,
324 field->GetOffset(),
325 field->IsVolatile(),
326 field->GetDexFieldIndex(),
327 field->GetDeclaringClass()->GetDexClassDefIndex(),
328 *field->GetDexFile(),
329 handles_->NewHandle(field->GetDexCache()),
330 invoke_instruction->GetDexPc());
331
332 bool is_referrer =
333 (ic.GetMonomorphicType() == outermost_graph_->GetArtMethod()->GetDeclaringClass());
334 HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(),
335 class_index,
336 caller_dex_file,
337 is_referrer,
338 invoke_instruction->GetDexPc(),
339 /* needs_access_check */ false,
340 /* is_in_dex_cache */ true);
341
342 HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, field_get);
343 HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize(
344 compare, invoke_instruction->GetDexPc());
345 // TODO: Extend reference type propagation to understand the guard.
346 if (cursor != nullptr) {
347 bb_cursor->InsertInstructionAfter(load_class, cursor);
348 } else {
349 bb_cursor->InsertInstructionBefore(load_class, bb_cursor->GetFirstInstruction());
350 }
351 bb_cursor->InsertInstructionAfter(field_get, load_class);
352 bb_cursor->InsertInstructionAfter(compare, field_get);
353 bb_cursor->InsertInstructionAfter(deoptimize, compare);
354 deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment());
355
356 // Run type propagation to get the guard typed, and eventually propagate the
357 // type of the receiver.
358 ReferenceTypePropagation rtp_fixup(graph_, handles_);
359 rtp_fixup.Run();
360
361 MaybeRecordStat(kInlinedMonomorphicCall);
362 return true;
363}
364
365bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction ATTRIBUTE_UNUSED,
366 ArtMethod* resolved_method,
367 const InlineCache& ic ATTRIBUTE_UNUSED) {
368 // TODO
369 VLOG(compiler) << "Unimplemented polymorphic inlining for "
370 << PrettyMethod(resolved_method);
371 return false;
372}
373
374bool HInliner::TryInline(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) {
375 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800376
377 // Check whether we're allowed to inline. The outermost compilation unit is the relevant
378 // dex file here (though the transitivity of an inline chain would allow checking the calller).
379 if (!compiler_driver_->MayInline(method->GetDexFile(),
380 outer_compilation_unit_.GetDexFile())) {
381 VLOG(compiler) << "Won't inline " << PrettyMethod(method) << " in "
382 << outer_compilation_unit_.GetDexFile()->GetLocation() << " ("
383 << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from "
384 << method->GetDexFile()->GetLocation();
385 return false;
386 }
387
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100388 uint32_t method_index = FindMethodIndexIn(
389 method, caller_dex_file, invoke_instruction->GetDexMethodIndex());
390 if (method_index == DexFile::kDexNoIndex) {
391 VLOG(compiler) << "Call to "
392 << PrettyMethod(method)
393 << " cannot be inlined because unaccessible to caller";
394 return false;
395 }
396
397 bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile());
398
399 const DexFile::CodeItem* code_item = method->GetCodeItem();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400
401 if (code_item == nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100402 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 << " is not inlined because it is native";
404 return false;
405 }
406
Calin Juravleec748352015-07-29 13:52:12 +0100407 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
408 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100409 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 << " is too big to inline";
411 return false;
412 }
413
414 if (code_item->tries_size_ != 0) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100415 VLOG(compiler) << "Method " << PrettyMethod(method)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 << " is not inlined because of try block";
417 return false;
418 }
419
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100420 if (!method->GetDeclaringClass()->IsVerified()) {
421 uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex();
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100422 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100423 method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100424 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
425 << " couldn't be verified, so it cannot be inlined";
426 return false;
427 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 }
429
Roland Levillain4c0eb422015-04-24 16:43:49 +0100430 if (invoke_instruction->IsInvokeStaticOrDirect() &&
431 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
432 // Case of a static method that cannot be inlined because it implicitly
433 // requires an initialization check of its declaring class.
434 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
435 << " is not inlined because it is static and requires a clinit"
436 << " check that cannot be emitted due to Dex cache limitations";
437 return false;
438 }
439
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100440 if (!TryBuildAndInline(method, invoke_instruction, same_dex_file, do_rtp)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000441 return false;
442 }
443
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000444 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000445 MaybeRecordStat(kInlinedInvoke);
446 return true;
447}
448
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000450 HInvoke* invoke_instruction,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100451 bool same_dex_file,
452 bool do_rtp) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000453 ScopedObjectAccess soa(Thread::Current());
454 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100455 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
456 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000457 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700458 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 DexCompilationUnit dex_compilation_unit(
460 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000461 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000462 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000463 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000464 code_item,
465 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000466 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000467 resolved_method->GetAccessFlags(),
Mathieu Chartier736b5602015-09-02 14:54:11 -0700468 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index),
469 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000470
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100471 bool requires_ctor_barrier = false;
472
473 if (dex_compilation_unit.IsConstructor()) {
474 // If it's a super invocation and we already generate a barrier there's no need
475 // to generate another one.
476 // We identify super calls by looking at the "this" pointer. If its value is the
477 // same as the local "this" pointer then we must have a super invocation.
478 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
479 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
480 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
481 requires_ctor_barrier = false;
482 } else {
483 Thread* self = Thread::Current();
484 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
485 dex_compilation_unit.GetDexFile(),
486 dex_compilation_unit.GetClassDefIndex());
487 }
488 }
489
Nicolas Geoffray35071052015-06-09 15:43:38 +0100490 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
491 if (invoke_type == kInterface) {
492 // We have statically resolved the dispatch. To please the class linker
493 // at runtime, we change this call as if it was a virtual call.
494 invoke_type = kVirtual;
495 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000496 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100497 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100498 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100499 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100500 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700501 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100502 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100503 graph_->IsDebuggable(),
504 graph_->GetCurrentInstructionId());
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100505 callee_graph->SetArtMethod(resolved_method);
David Brazdil5e8b1372015-01-23 14:39:08 +0000506
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000507 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000508 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000509 &dex_compilation_unit,
510 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000511 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000512 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000513 &inline_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700514 resolved_method->GetQuickenedInfo(),
515 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000516
David Brazdil5e8b1372015-01-23 14:39:08 +0000517 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100518 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000519 << " could not be built, so cannot be inlined";
520 return false;
521 }
522
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000523 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
524 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100525 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000526 << " cannot be inlined because of the register allocator";
527 return false;
528 }
529
David Brazdil4833f5a2015-12-16 10:37:39 +0000530 if (callee_graph->TryBuildingSsa(handles_) != kBuildSsaSuccess) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100531 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000532 << " could not be transformed to SSA";
533 return false;
534 }
535
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700536 size_t parameter_index = 0;
537 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
538 !instructions.Done();
539 instructions.Advance()) {
540 HInstruction* current = instructions.Current();
541 if (current->IsParameterValue()) {
542 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
543 if (argument->IsNullConstant()) {
544 current->ReplaceWith(callee_graph->GetNullConstant());
545 } else if (argument->IsIntConstant()) {
546 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
547 } else if (argument->IsLongConstant()) {
548 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
549 } else if (argument->IsFloatConstant()) {
550 current->ReplaceWith(
551 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
552 } else if (argument->IsDoubleConstant()) {
553 current->ReplaceWith(
554 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
555 } else if (argument->GetType() == Primitive::kPrimNot) {
556 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
557 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
558 }
559 }
560 }
561
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000562 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100563 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +0000564 HConstantFolding fold(callee_graph);
Vladimir Markodc151b22015-10-15 18:02:30 +0100565 HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000566 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +0000567 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000568
569 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100570 &intrinsics,
Vladimir Markodc151b22015-10-15 18:02:30 +0100571 &sharpening,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000572 &simplify,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700573 &fold,
Vladimir Marko9e23df52015-11-10 17:14:35 +0000574 &dce,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000575 };
576
577 for (size_t i = 0; i < arraysize(optimizations); ++i) {
578 HOptimization* optimization = optimizations[i];
579 optimization->Run();
580 }
581
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700582 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Calin Juravleec748352015-07-29 13:52:12 +0100583 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000584 HInliner inliner(callee_graph,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100585 outermost_graph_,
Vladimir Markodc151b22015-10-15 18:02:30 +0100586 codegen_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000587 outer_compilation_unit_,
588 dex_compilation_unit,
589 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100590 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000591 stats_,
592 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000593 inliner.Run();
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700594 number_of_instructions_budget += inliner.number_of_inlined_instructions_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000595 }
596
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100597 // TODO: We should abort only if all predecessors throw. However,
598 // HGraph::InlineInto currently does not handle an exit block with
599 // a throw predecessor.
600 HBasicBlock* exit_block = callee_graph->GetExitBlock();
601 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100602 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100603 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100604 return false;
605 }
606
607 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +0000608 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
609 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100610 has_throw_predecessor = true;
611 break;
612 }
613 }
614 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100615 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100616 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100617 return false;
618 }
619
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000620 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000621 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700622 size_t number_of_instructions = 0;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000623 for (; !it.Done(); it.Advance()) {
624 HBasicBlock* block = it.Current();
625 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100626 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000627 << " could not be inlined because it contains a loop";
628 return false;
629 }
630
631 for (HInstructionIterator instr_it(block->GetInstructions());
632 !instr_it.Done();
633 instr_it.Advance()) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700634 if (number_of_instructions++ == number_of_instructions_budget) {
635 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayb17d1cc2015-12-17 15:26:21 +0000636 << " could not be inlined because it is too big.";
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700637 return false;
638 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000639 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000640
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100641 if (current->IsInvokeInterface()) {
642 // Disable inlining of interface calls. The cost in case of entering the
643 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100644 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100645 << " could not be inlined because it has an interface call.";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000646 return false;
647 }
648
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100649 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100650 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000651 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100652 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000653 return false;
654 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000655
Vladimir Markodc151b22015-10-15 18:02:30 +0100656 if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100657 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000658 << " could not be inlined because " << current->DebugName()
659 << " it is in a different dex file and requires access to the dex cache";
660 return false;
661 }
Nicolas Geoffrayd9309292015-10-31 22:21:31 +0000662
663 if (current->IsNewInstance() &&
664 (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
665 // Allocation entrypoint does not handle inlined frames.
666 return false;
667 }
668
669 if (current->IsNewArray() &&
670 (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
671 // Allocation entrypoint does not handle inlined frames.
672 return false;
673 }
674
675 if (current->IsUnresolvedStaticFieldGet() ||
676 current->IsUnresolvedInstanceFieldGet() ||
677 current->IsUnresolvedStaticFieldSet() ||
678 current->IsUnresolvedInstanceFieldSet()) {
679 // Entrypoint for unresolved fields does not handle inlined frames.
680 return false;
681 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000682 }
683 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700684 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000685
Calin Juravle2e768302015-07-28 14:41:11 +0000686 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
Calin Juravle214bbcd2015-10-20 14:54:07 +0100687 if (return_replacement != nullptr) {
688 DCHECK_EQ(graph_, return_replacement->GetBlock()->GetGraph());
689 }
Calin Juravle2e768302015-07-28 14:41:11 +0000690
Alex Light68289a52015-12-15 17:30:30 -0800691 // Check the integrity of reference types and run another type propagation if needed.
David Brazdil4833f5a2015-12-16 10:37:39 +0000692 if (return_replacement != nullptr) {
693 if (return_replacement->GetType() == Primitive::kPrimNot) {
694 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
695 // Make sure that we have a valid type for the return. We may get an invalid one when
696 // we inline invokes with multiple branches and create a Phi for the result.
697 // TODO: we could be more precise by merging the phi inputs but that requires
698 // some functionality from the reference type propagation.
699 DCHECK(return_replacement->IsPhi());
700 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
701 ReferenceTypeInfo::TypeHandle return_handle =
702 handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size));
703 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
704 return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */));
705 }
Alex Light68289a52015-12-15 17:30:30 -0800706
David Brazdil4833f5a2015-12-16 10:37:39 +0000707 if (do_rtp) {
708 // If the return type is a refinement of the declared type run the type propagation again.
709 ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo();
710 ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo();
711 if (invoke_rti.IsStrictSupertypeOf(return_rti)
712 || (return_rti.IsExact() && !invoke_rti.IsExact())
713 || !return_replacement->CanBeNull()) {
714 ReferenceTypePropagation(graph_, handles_).Run();
715 }
716 }
717 } else if (return_replacement->IsInstanceOf()) {
718 if (do_rtp) {
719 // Inlining InstanceOf into an If may put a tighter bound on reference types.
720 ReferenceTypePropagation(graph_, handles_).Run();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100721 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000722 }
Calin Juravle2e768302015-07-28 14:41:11 +0000723 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000724
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000725 return true;
726}
727
728} // namespace art