blob: 6d93be37a7a21d04e60d31db919072b9223e0b12 [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"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "ssa_phi_elimination.h"
37#include "scoped_thread_state_change.h"
38#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010039#include "dex/verified_method.h"
40#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000041
42namespace art {
43
Nicolas Geoffraye418dda2015-08-11 20:03:09 -070044static constexpr size_t kMaximumNumberOfHInstructions = 12;
45
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000046void HInliner::Run() {
Calin Juravle8f96df82015-07-29 15:58:48 +010047 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
48 if ((compiler_options.GetInlineDepthLimit() == 0)
49 || (compiler_options.GetInlineMaxCodeUnits() == 0)) {
50 return;
51 }
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000052 if (graph_->IsDebuggable()) {
53 // For simplicity, we currently never inline when the graph is debuggable. This avoids
54 // doing some logic in the runtime to discover if a method could have been inlined.
55 return;
56 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +010057 const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
58 DCHECK(!blocks.empty());
59 HBasicBlock* next_block = blocks[0];
60 for (size_t i = 0; i < blocks.size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010061 // Because we are changing the graph when inlining, we need to remember the next block.
62 // This avoids doing the inlining work again on the inlined blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +010063 if (blocks[i] != next_block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010064 continue;
65 }
66 HBasicBlock* block = next_block;
Vladimir Markofa6b93c2015-09-15 10:15:55 +010067 next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1];
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000068 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
69 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010070 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070071 // As long as the call is not intrinsified, it is worth trying to inline.
72 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000073 // We use the original invoke type to ensure the resolution of the called method
74 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000075 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010076 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000077 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000078 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000079 bool should_inline = callee_name.find("$inline$") != std::string::npos;
80 CHECK(!should_inline) << "Could not inline " << callee_name;
81 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010082 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010083 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010084 std::string callee_name =
85 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
86 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
87 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
88 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000089 }
90 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000091 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092 }
93 }
94}
95
Nicolas Geoffray454a4812015-06-09 10:37:32 +010096static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070097 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010098 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
99}
100
101/**
102 * Given the `resolved_method` looked up in the dex cache, try to find
103 * the actual runtime target of an interface or virtual call.
104 * Return nullptr if the runtime target cannot be proven.
105 */
106static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100108 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
109 // No need to lookup further, the resolved method will be the target.
110 return resolved_method;
111 }
112
113 HInstruction* receiver = invoke->InputAt(0);
114 if (receiver->IsNullCheck()) {
115 // Due to multiple levels of inlining within the same pass, it might be that
116 // null check does not have the reference type of the actual receiver.
117 receiver = receiver->InputAt(0);
118 }
119 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000120 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
121 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100122 // We currently only support inlining with known receivers.
123 // TODO: Remove this check, we should be able to inline final methods
124 // on unknown receivers.
125 return nullptr;
126 } else if (info.GetTypeHandle()->IsInterface()) {
127 // Statically knowing that the receiver has an interface type cannot
128 // help us find what is the target method.
129 return nullptr;
130 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
131 // The method that we're trying to call is not in the receiver's class or super classes.
132 return nullptr;
133 }
134
135 ClassLinker* cl = Runtime::Current()->GetClassLinker();
136 size_t pointer_size = cl->GetImagePointerSize();
137 if (invoke->IsInvokeInterface()) {
138 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
139 resolved_method, pointer_size);
140 } else {
141 DCHECK(invoke->IsInvokeVirtual());
142 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
143 resolved_method, pointer_size);
144 }
145
146 if (resolved_method == nullptr) {
147 // The information we had on the receiver was not enough to find
148 // the target method. Since we check above the exact type of the receiver,
149 // the only reason this can happen is an IncompatibleClassChangeError.
150 return nullptr;
Alex Light9139e002015-10-09 15:59:48 -0700151 } else if (!resolved_method->IsInvokable()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100152 // The information we had on the receiver was not enough to find
153 // the target method. Since we check above the exact type of the receiver,
154 // the only reason this can happen is an IncompatibleClassChangeError.
155 return nullptr;
156 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
157 // A final method has to be the target method.
158 return resolved_method;
159 } else if (info.IsExact()) {
160 // If we found a method and the receiver's concrete type is statically
161 // known, we know for sure the target.
162 return resolved_method;
163 } else {
164 // Even if we did find a method, the receiver type was not enough to
165 // statically find the runtime target.
166 return nullptr;
167 }
168}
169
170static uint32_t FindMethodIndexIn(ArtMethod* method,
171 const DexFile& dex_file,
172 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100174 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
175 return method->GetDexMethodIndex();
176 } else {
177 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
178 }
179}
180
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700181bool HInliner::TryInline(HInvoke* invoke_instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +0100182 if (invoke_instruction->IsInvokeUnresolved()) {
183 return false; // Don't bother to move further if we know the method is unresolved.
184 }
185
Vladimir Marko58155012015-08-19 12:49:41 +0000186 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000188 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
189 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000190
Nicolas Geoffray35071052015-06-09 15:43:38 +0100191 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
192 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000193 ArtMethod* resolved_method;
194 if (invoke_instruction->IsInvokeStaticOrDirect()) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000195 if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) {
196 VLOG(compiler) << "Not inlining a String.<init> method";
197 return false;
198 }
Vladimir Marko58155012015-08-19 12:49:41 +0000199 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700200 mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file)
201 ? caller_compilation_unit_.GetDexCache().Get()
202 : class_linker->FindDexCache(soa.Self(), *ref.dex_file);
203 resolved_method = dex_cache->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000204 ref.dex_method_index, class_linker->GetImagePointerSize());
205 } else {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700206 resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod(
Vladimir Marko58155012015-08-19 12:49:41 +0000207 method_index, class_linker->GetImagePointerSize());
208 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 if (resolved_method == nullptr) {
Calin Juravle175dc732015-08-25 15:42:32 +0100211 // TODO: Can this still happen?
Nicolas Geoffray35071052015-06-09 15:43:38 +0100212 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000213 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 return false;
215 }
216
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100217 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
218 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
219 if (resolved_method == nullptr) {
220 VLOG(compiler) << "Interface or virtual call to "
221 << PrettyMethod(method_index, caller_dex_file)
222 << " could not be statically determined";
223 return false;
224 }
225 // We have found a method, but we need to find where that method is for the caller's
226 // dex file.
227 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
228 if (method_index == DexFile::kDexNoIndex) {
229 VLOG(compiler) << "Interface or virtual call to "
230 << PrettyMethod(resolved_method)
231 << " cannot be inlined because unaccessible to caller";
232 return false;
233 }
234 }
235
Vladimir Marko58155012015-08-19 12:49:41 +0000236 bool same_dex_file =
237 IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *resolved_method->GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238
239 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
240
241 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000242 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000243 << " is not inlined because it is native";
244 return false;
245 }
246
Calin Juravleec748352015-07-29 13:52:12 +0100247 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
248 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000249 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000250 << " is too big to inline";
251 return false;
252 }
253
254 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000255 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000256 << " is not inlined because of try block";
257 return false;
258 }
259
Nicolas Geoffray481303b2015-10-02 12:38:40 +0100260 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
Nicolas Geoffrayccc61972015-10-01 14:34:20 +0100261 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
262 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
263 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
264 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
265 << " couldn't be verified, so it cannot be inlined";
266 return false;
267 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000268 }
269
Roland Levillain4c0eb422015-04-24 16:43:49 +0100270 if (invoke_instruction->IsInvokeStaticOrDirect() &&
271 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
272 // Case of a static method that cannot be inlined because it implicitly
273 // requires an initialization check of its declaring class.
274 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
275 << " is not inlined because it is static and requires a clinit"
276 << " check that cannot be emitted due to Dex cache limitations";
277 return false;
278 }
279
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100280 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000281 return false;
282 }
283
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000284 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000285 MaybeRecordStat(kInlinedInvoke);
286 return true;
287}
288
Mathieu Chartiere401d142015-04-22 13:56:20 -0700289bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000290 HInvoke* invoke_instruction,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700291 bool same_dex_file) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000292 ScopedObjectAccess soa(Thread::Current());
293 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100294 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
295 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000296 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Mathieu Chartier736b5602015-09-02 14:54:11 -0700297 Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache()));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000298 DexCompilationUnit dex_compilation_unit(
299 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000300 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000301 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000302 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000303 code_item,
304 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000305 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306 resolved_method->GetAccessFlags(),
Mathieu Chartier736b5602015-09-02 14:54:11 -0700307 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index),
308 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000309
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100310 bool requires_ctor_barrier = false;
311
312 if (dex_compilation_unit.IsConstructor()) {
313 // If it's a super invocation and we already generate a barrier there's no need
314 // to generate another one.
315 // We identify super calls by looking at the "this" pointer. If its value is the
316 // same as the local "this" pointer then we must have a super invocation.
317 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
318 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
319 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
320 requires_ctor_barrier = false;
321 } else {
322 Thread* self = Thread::Current();
323 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
324 dex_compilation_unit.GetDexFile(),
325 dex_compilation_unit.GetClassDefIndex());
326 }
327 }
328
Nicolas Geoffray35071052015-06-09 15:43:38 +0100329 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
330 if (invoke_type == kInterface) {
331 // We have statically resolved the dispatch. To please the class linker
332 // at runtime, we change this call as if it was a virtual call.
333 invoke_type = kVirtual;
334 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000335 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100337 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100339 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100341 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100342 graph_->IsDebuggable(),
343 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000346 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000347 &dex_compilation_unit,
348 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000349 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000351 &inline_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700352 resolved_method->GetQuickenedInfo(),
353 dex_cache);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354
David Brazdil5e8b1372015-01-23 14:39:08 +0000355 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100356 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000357 << " could not be built, so cannot be inlined";
358 return false;
359 }
360
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000361 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
362 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100363 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000364 << " cannot be inlined because of the register allocator";
365 return false;
366 }
367
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000368 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100369 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 << " could not be transformed to SSA";
371 return false;
372 }
373
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700374 size_t parameter_index = 0;
375 for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions());
376 !instructions.Done();
377 instructions.Advance()) {
378 HInstruction* current = instructions.Current();
379 if (current->IsParameterValue()) {
380 HInstruction* argument = invoke_instruction->InputAt(parameter_index++);
381 if (argument->IsNullConstant()) {
382 current->ReplaceWith(callee_graph->GetNullConstant());
383 } else if (argument->IsIntConstant()) {
384 current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue()));
385 } else if (argument->IsLongConstant()) {
386 current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue()));
387 } else if (argument->IsFloatConstant()) {
388 current->ReplaceWith(
389 callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue()));
390 } else if (argument->IsDoubleConstant()) {
391 current->ReplaceWith(
392 callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue()));
393 } else if (argument->GetType() == Primitive::kPrimNot) {
394 current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo());
395 current->AsParameterValue()->SetCanBeNull(argument->CanBeNull());
396 }
397 }
398 }
399
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000400 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100401 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +0000402 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100403 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Vladimir Markodc151b22015-10-15 18:02:30 +0100404 HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000405 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffraye34648d2015-11-23 08:59:07 +0000406 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000407
408 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100409 &intrinsics,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100410 &type_propagation,
Vladimir Markodc151b22015-10-15 18:02:30 +0100411 &sharpening,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000412 &simplify,
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700413 &fold,
Vladimir Marko9e23df52015-11-10 17:14:35 +0000414 &dce,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000415 };
416
417 for (size_t i = 0; i < arraysize(optimizations); ++i) {
418 HOptimization* optimization = optimizations[i];
419 optimization->Run();
420 }
421
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700422 size_t number_of_instructions_budget = kMaximumNumberOfHInstructions;
Calin Juravleec748352015-07-29 13:52:12 +0100423 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000424 HInliner inliner(callee_graph,
Vladimir Markodc151b22015-10-15 18:02:30 +0100425 codegen_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000426 outer_compilation_unit_,
427 dex_compilation_unit,
428 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100429 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000430 stats_,
431 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000432 inliner.Run();
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700433 number_of_instructions_budget += inliner.number_of_inlined_instructions_;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000434 }
435
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 // TODO: We should abort only if all predecessors throw. However,
437 // HGraph::InlineInto currently does not handle an exit block with
438 // a throw predecessor.
439 HBasicBlock* exit_block = callee_graph->GetExitBlock();
440 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100441 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 << " could not be inlined because it has an infinite loop";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 return false;
444 }
445
446 bool has_throw_predecessor = false;
Vladimir Marko60584552015-09-03 13:35:12 +0000447 for (HBasicBlock* predecessor : exit_block->GetPredecessors()) {
448 if (predecessor->GetLastInstruction()->IsThrow()) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100449 has_throw_predecessor = true;
450 break;
451 }
452 }
453 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100454 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 << " could not be inlined because one branch always throws";
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100456 return false;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000460 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700461 size_t number_of_instructions = 0;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000462 for (; !it.Done(); it.Advance()) {
463 HBasicBlock* block = it.Current();
464 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100465 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000466 << " could not be inlined because it contains a loop";
467 return false;
468 }
469
470 for (HInstructionIterator instr_it(block->GetInstructions());
471 !instr_it.Done();
472 instr_it.Advance()) {
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700473 if (number_of_instructions++ == number_of_instructions_budget) {
474 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
475 << " could not be inlined because it is too big.";
476 return false;
477 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000478 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000479
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100480 if (current->IsInvokeInterface()) {
481 // Disable inlining of interface calls. The cost in case of entering the
482 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100483 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100484 << " could not be inlined because it has an interface call.";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000485 return false;
486 }
487
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100488 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100489 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000490 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100491 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000492 return false;
493 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000494
Vladimir Markodc151b22015-10-15 18:02:30 +0100495 if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100496 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000497 << " could not be inlined because " << current->DebugName()
498 << " it is in a different dex file and requires access to the dex cache";
499 return false;
500 }
Nicolas Geoffrayd9309292015-10-31 22:21:31 +0000501
502 if (current->IsNewInstance() &&
503 (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) {
504 // Allocation entrypoint does not handle inlined frames.
505 return false;
506 }
507
508 if (current->IsNewArray() &&
509 (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) {
510 // Allocation entrypoint does not handle inlined frames.
511 return false;
512 }
513
514 if (current->IsUnresolvedStaticFieldGet() ||
515 current->IsUnresolvedInstanceFieldGet() ||
516 current->IsUnresolvedStaticFieldSet() ||
517 current->IsUnresolvedInstanceFieldSet()) {
518 // Entrypoint for unresolved fields does not handle inlined frames.
519 return false;
520 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000521 }
522 }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -0700523 number_of_inlined_instructions_ += number_of_instructions;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000524
Calin Juravle2e768302015-07-28 14:41:11 +0000525 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
Calin Juravle214bbcd2015-10-20 14:54:07 +0100526 if (return_replacement != nullptr) {
527 DCHECK_EQ(graph_, return_replacement->GetBlock()->GetGraph());
528 }
Calin Juravle2e768302015-07-28 14:41:11 +0000529
530 // When merging the graph we might create a new NullConstant in the caller graph which does
531 // not have the chance to be typed. We assign the correct type here so that we can keep the
532 // assertion that every reference has a valid type. This also simplifies checks along the way.
533 HNullConstant* null_constant = graph_->GetNullConstant();
534 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
535 ReferenceTypeInfo::TypeHandle obj_handle =
536 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
537 null_constant->SetReferenceTypeInfo(
538 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
539 }
540
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000541 // Check the integrity of reference types and run another type propagation if needed.
Calin Juravle2e768302015-07-28 14:41:11 +0000542 if ((return_replacement != nullptr)
543 && (return_replacement->GetType() == Primitive::kPrimNot)) {
544 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
545 // Make sure that we have a valid type for the return. We may get an invalid one when
546 // we inline invokes with multiple branches and create a Phi for the result.
547 // TODO: we could be more precise by merging the phi inputs but that requires
548 // some functionality from the reference type propagation.
549 DCHECK(return_replacement->IsPhi());
Vladimir Marko05792b92015-08-03 11:56:49 +0100550 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Calin Juravle2e768302015-07-28 14:41:11 +0000551 ReferenceTypeInfo::TypeHandle return_handle =
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000552 handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size));
Calin Juravle2e768302015-07-28 14:41:11 +0000553 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
David Brazdilbaf89b82015-09-15 11:36:54 +0100554 return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */));
Calin Juravle2e768302015-07-28 14:41:11 +0000555 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000556
557 // If the return type is a refinement of the declared type run the type propagation again.
558 ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo();
559 ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo();
560 if (invoke_rti.IsStrictSupertypeOf(return_rti)
561 || (return_rti.IsExact() && !invoke_rti.IsExact())
562 || !return_replacement->CanBeNull()) {
563 ReferenceTypePropagation rtp_fixup(graph_, handles_);
564 rtp_fixup.Run();
565 }
Calin Juravle2e768302015-07-28 14:41:11 +0000566 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000567
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000568 return true;
569}
570
571} // namespace art