blob: 202f3f074d136e2040d4feb82568fe7d91481e0f [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"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000035#include "ssa_phi_elimination.h"
36#include "scoped_thread_state_change.h"
37#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010038#include "dex/verified_method.h"
39#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040
41namespace art {
42
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000043void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000044 if (graph_->IsDebuggable()) {
45 // For simplicity, we currently never inline when the graph is debuggable. This avoids
46 // doing some logic in the runtime to discover if a method could have been inlined.
47 return;
48 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000049 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010050 HBasicBlock* next_block = blocks.Get(0);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000051 for (size_t i = 0; i < blocks.Size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010052 // Because we are changing the graph when inlining, we need to remember the next block.
53 // This avoids doing the inlining work again on the inlined blocks.
54 if (blocks.Get(i) != next_block) {
55 continue;
56 }
57 HBasicBlock* block = next_block;
58 next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000059 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
60 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010061 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070062 // As long as the call is not intrinsified, it is worth trying to inline.
63 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000064 // We use the original invoke type to ensure the resolution of the called method
65 // works properly.
Vladimir Marko58155012015-08-19 12:49:41 +000066 if (!TryInline(call)) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010067 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000068 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000069 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000070 bool should_inline = callee_name.find("$inline$") != std::string::npos;
71 CHECK(!should_inline) << "Could not inline " << callee_name;
72 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010073 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010074 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010075 std::string callee_name =
76 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
77 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
78 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
79 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 }
81 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000082 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000083 }
84 }
85}
86
Nicolas Geoffray454a4812015-06-09 10:37:32 +010087static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070088 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010089 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
90}
91
92/**
93 * Given the `resolved_method` looked up in the dex cache, try to find
94 * the actual runtime target of an interface or virtual call.
95 * Return nullptr if the runtime target cannot be proven.
96 */
97static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010099 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
100 // No need to lookup further, the resolved method will be the target.
101 return resolved_method;
102 }
103
104 HInstruction* receiver = invoke->InputAt(0);
105 if (receiver->IsNullCheck()) {
106 // Due to multiple levels of inlining within the same pass, it might be that
107 // null check does not have the reference type of the actual receiver.
108 receiver = receiver->InputAt(0);
109 }
110 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000111 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
112 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100113 // We currently only support inlining with known receivers.
114 // TODO: Remove this check, we should be able to inline final methods
115 // on unknown receivers.
116 return nullptr;
117 } else if (info.GetTypeHandle()->IsInterface()) {
118 // Statically knowing that the receiver has an interface type cannot
119 // help us find what is the target method.
120 return nullptr;
121 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
122 // The method that we're trying to call is not in the receiver's class or super classes.
123 return nullptr;
124 }
125
126 ClassLinker* cl = Runtime::Current()->GetClassLinker();
127 size_t pointer_size = cl->GetImagePointerSize();
128 if (invoke->IsInvokeInterface()) {
129 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
130 resolved_method, pointer_size);
131 } else {
132 DCHECK(invoke->IsInvokeVirtual());
133 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
134 resolved_method, pointer_size);
135 }
136
137 if (resolved_method == nullptr) {
138 // The information we had on the receiver was not enough to find
139 // the target method. Since we check above the exact type of the receiver,
140 // the only reason this can happen is an IncompatibleClassChangeError.
141 return nullptr;
142 } else if (resolved_method->IsAbstract()) {
143 // The information we had on the receiver was not enough to find
144 // the target method. Since we check above the exact type of the receiver,
145 // the only reason this can happen is an IncompatibleClassChangeError.
146 return nullptr;
147 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
148 // A final method has to be the target method.
149 return resolved_method;
150 } else if (info.IsExact()) {
151 // If we found a method and the receiver's concrete type is statically
152 // known, we know for sure the target.
153 return resolved_method;
154 } else {
155 // Even if we did find a method, the receiver type was not enough to
156 // statically find the runtime target.
157 return nullptr;
158 }
159}
160
161static uint32_t FindMethodIndexIn(ArtMethod* method,
162 const DexFile& dex_file,
163 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100165 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
166 return method->GetDexMethodIndex();
167 } else {
168 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
169 }
170}
171
Vladimir Marko58155012015-08-19 12:49:41 +0000172bool HInliner::TryInline(HInvoke* invoke_instruction) const {
173 uint32_t method_index = invoke_instruction->GetDexMethodIndex();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000175 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
176 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177
Nicolas Geoffray35071052015-06-09 15:43:38 +0100178 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
179 // We can query the dex cache directly. The verifier has populated it already.
Vladimir Marko58155012015-08-19 12:49:41 +0000180 ArtMethod* resolved_method;
181 if (invoke_instruction->IsInvokeStaticOrDirect()) {
182 MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod();
183 resolved_method = class_linker->FindDexCache(*ref.dex_file)->GetResolvedMethod(
184 ref.dex_method_index, class_linker->GetImagePointerSize());
185 } else {
186 resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
187 method_index, class_linker->GetImagePointerSize());
188 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100191 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000192 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000193 return false;
194 }
195
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100196 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
197 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
198 if (resolved_method == nullptr) {
199 VLOG(compiler) << "Interface or virtual call to "
200 << PrettyMethod(method_index, caller_dex_file)
201 << " could not be statically determined";
202 return false;
203 }
204 // We have found a method, but we need to find where that method is for the caller's
205 // dex file.
206 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
207 if (method_index == DexFile::kDexNoIndex) {
208 VLOG(compiler) << "Interface or virtual call to "
209 << PrettyMethod(resolved_method)
210 << " cannot be inlined because unaccessible to caller";
211 return false;
212 }
213 }
214
Vladimir Marko58155012015-08-19 12:49:41 +0000215 bool same_dex_file =
216 IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *resolved_method->GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217
218 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
219
220 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000221 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 << " is not inlined because it is native";
223 return false;
224 }
225
Calin Juravleec748352015-07-29 13:52:12 +0100226 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
227 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000228 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229 << " is too big to inline";
230 return false;
231 }
232
233 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000234 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 << " is not inlined because of try block";
236 return false;
237 }
238
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100239 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
240 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
241 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000242 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100243 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 return false;
245 }
246
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000247 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000248 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000249 << " was already flagged as non inlineable";
250 return false;
251 }
252
Roland Levillain4c0eb422015-04-24 16:43:49 +0100253 if (invoke_instruction->IsInvokeStaticOrDirect() &&
254 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
255 // Case of a static method that cannot be inlined because it implicitly
256 // requires an initialization check of its declaring class.
257 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
258 << " is not inlined because it is static and requires a clinit"
259 << " check that cannot be emitted due to Dex cache limitations";
260 return false;
261 }
262
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100263 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000264 return false;
265 }
266
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000267 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000268 MaybeRecordStat(kInlinedInvoke);
269 return true;
270}
271
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000273 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100274 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000275 ScopedObjectAccess soa(Thread::Current());
276 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100277 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
278 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000279 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000280 DexCompilationUnit dex_compilation_unit(
281 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000282 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000283 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000284 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000285 code_item,
286 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000287 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000288 resolved_method->GetAccessFlags(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000289 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000290
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100291 bool requires_ctor_barrier = false;
292
293 if (dex_compilation_unit.IsConstructor()) {
294 // If it's a super invocation and we already generate a barrier there's no need
295 // to generate another one.
296 // We identify super calls by looking at the "this" pointer. If its value is the
297 // same as the local "this" pointer then we must have a super invocation.
298 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
299 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
300 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
301 requires_ctor_barrier = false;
302 } else {
303 Thread* self = Thread::Current();
304 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
305 dex_compilation_unit.GetDexFile(),
306 dex_compilation_unit.GetClassDefIndex());
307 }
308 }
309
Nicolas Geoffray35071052015-06-09 15:43:38 +0100310 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
311 if (invoke_type == kInterface) {
312 // We have statically resolved the dispatch. To please the class linker
313 // at runtime, we change this call as if it was a virtual call.
314 invoke_type = kVirtual;
315 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000316 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100318 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100320 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100322 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100323 graph_->IsDebuggable(),
324 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000325
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000326 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000327 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000328 &dex_compilation_unit,
329 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000330 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000331 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000332 &inline_stats,
333 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000334
David Brazdil5e8b1372015-01-23 14:39:08 +0000335 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100336 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000337 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100338 // There could be multiple reasons why the graph could not be built, including
339 // unaccessible methods/fields due to using a different dex cache. We do not mark
340 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000341 return false;
342 }
343
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000344 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
345 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100346 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000347 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100348 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000349 return false;
350 }
351
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100353 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100355 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000356 return false;
357 }
358
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000359 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100360 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000361 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100362 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000363 InstructionSimplifier simplify(callee_graph, stats_);
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100364 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000365
366 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100367 &intrinsics,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000368 &dce,
369 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100370 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000371 &simplify,
372 };
373
374 for (size_t i = 0; i < arraysize(optimizations); ++i) {
375 HOptimization* optimization = optimizations[i];
376 optimization->Run();
377 }
378
Calin Juravleec748352015-07-29 13:52:12 +0100379 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000380 HInliner inliner(callee_graph,
381 outer_compilation_unit_,
382 dex_compilation_unit,
383 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100384 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000385 stats_,
386 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000387 inliner.Run();
388 }
389
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100390 // TODO: We should abort only if all predecessors throw. However,
391 // HGraph::InlineInto currently does not handle an exit block with
392 // a throw predecessor.
393 HBasicBlock* exit_block = callee_graph->GetExitBlock();
394 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100395 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100396 << " could not be inlined because it has an infinite loop";
397 resolved_method->SetShouldNotInline();
398 return false;
399 }
400
401 bool has_throw_predecessor = false;
402 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
403 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
404 has_throw_predecessor = true;
405 break;
406 }
407 }
408 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100409 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100410 << " could not be inlined because one branch always throws";
411 resolved_method->SetShouldNotInline();
412 return false;
413 }
414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000416 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 for (; !it.Done(); it.Advance()) {
418 HBasicBlock* block = it.Current();
419 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100420 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100422 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 return false;
424 }
425
426 for (HInstructionIterator instr_it(block->GetInstructions());
427 !instr_it.Done();
428 instr_it.Advance()) {
429 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000430
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 if (current->IsInvokeInterface()) {
432 // Disable inlining of interface calls. The cost in case of entering the
433 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100434 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100436 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000437 return false;
438 }
439
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100440 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100441 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 return false;
445 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000446
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100447 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100448 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000449 << " could not be inlined because " << current->DebugName()
450 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100451 // Do not flag the method as not-inlineable. A caller within the same
452 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000453 return false;
454 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000455 }
456 }
457
Calin Juravle2e768302015-07-28 14:41:11 +0000458 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
459
460 // When merging the graph we might create a new NullConstant in the caller graph which does
461 // not have the chance to be typed. We assign the correct type here so that we can keep the
462 // assertion that every reference has a valid type. This also simplifies checks along the way.
463 HNullConstant* null_constant = graph_->GetNullConstant();
464 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
465 ReferenceTypeInfo::TypeHandle obj_handle =
466 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
467 null_constant->SetReferenceTypeInfo(
468 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
469 }
470
471 if ((return_replacement != nullptr)
472 && (return_replacement->GetType() == Primitive::kPrimNot)) {
473 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
474 // Make sure that we have a valid type for the return. We may get an invalid one when
475 // we inline invokes with multiple branches and create a Phi for the result.
476 // TODO: we could be more precise by merging the phi inputs but that requires
477 // some functionality from the reference type propagation.
478 DCHECK(return_replacement->IsPhi());
479 ReferenceTypeInfo::TypeHandle return_handle =
480 handles_->NewHandle(resolved_method->GetReturnType());
481 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
482 return_handle, return_handle->IsFinal() /* is_exact */));
483 }
484 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000485
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000486 return true;
487}
488
489} // namespace art