blob: cfb1868bff6a6546721bee4e8a34a351caa79bdf [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"
25#include "driver/dex_compilation_unit.h"
26#include "instruction_simplifier.h"
Scott Wakelingd60a1af2015-07-22 14:32:44 +010027#include "intrinsics.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000028#include "mirror/class_loader.h"
29#include "mirror/dex_cache.h"
30#include "nodes.h"
Nicolas Geoffray335005e2015-06-25 10:01:47 +010031#include "optimizing_compiler.h"
Nicolas Geoffray454a4812015-06-09 10:37:32 +010032#include "reference_type_propagation.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000033#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "ssa_phi_elimination.h"
35#include "scoped_thread_state_change.h"
36#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010037#include "dex/verified_method.h"
38#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039
40namespace art {
41
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010042static constexpr int kMaxInlineCodeUnits = 18;
43static constexpr int kDepthLimit = 3;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044
45void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000046 if (graph_->IsDebuggable()) {
47 // For simplicity, we currently never inline when the graph is debuggable. This avoids
48 // doing some logic in the runtime to discover if a method could have been inlined.
49 return;
50 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000051 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010052 HBasicBlock* next_block = blocks.Get(0);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000053 for (size_t i = 0; i < blocks.Size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010054 // Because we are changing the graph when inlining, we need to remember the next block.
55 // This avoids doing the inlining work again on the inlined blocks.
56 if (blocks.Get(i) != next_block) {
57 continue;
58 }
59 HBasicBlock* block = next_block;
60 next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000061 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
62 HInstruction* next = instruction->GetNext();
Nicolas Geoffray454a4812015-06-09 10:37:32 +010063 HInvoke* call = instruction->AsInvoke();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070064 // As long as the call is not intrinsified, it is worth trying to inline.
65 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000066 // We use the original invoke type to ensure the resolution of the called method
67 // works properly.
Nicolas Geoffray35071052015-06-09 15:43:38 +010068 if (!TryInline(call, call->GetDexMethodIndex())) {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010069 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000070 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000071 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000072 bool should_inline = callee_name.find("$inline$") != std::string::npos;
73 CHECK(!should_inline) << "Could not inline " << callee_name;
74 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010075 } else {
Nicolas Geoffray335005e2015-06-25 10:01:47 +010076 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010077 std::string callee_name =
78 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
79 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
80 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
81 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082 }
83 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000084 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000085 }
86 }
87}
88
Nicolas Geoffray454a4812015-06-09 10:37:32 +010089static bool IsMethodOrDeclaringClassFinal(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070090 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +010091 return method->IsFinal() || method->GetDeclaringClass()->IsFinal();
92}
93
94/**
95 * Given the `resolved_method` looked up in the dex cache, try to find
96 * the actual runtime target of an interface or virtual call.
97 * Return nullptr if the runtime target cannot be proven.
98 */
99static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700100 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100101 if (IsMethodOrDeclaringClassFinal(resolved_method)) {
102 // No need to lookup further, the resolved method will be the target.
103 return resolved_method;
104 }
105
106 HInstruction* receiver = invoke->InputAt(0);
107 if (receiver->IsNullCheck()) {
108 // Due to multiple levels of inlining within the same pass, it might be that
109 // null check does not have the reference type of the actual receiver.
110 receiver = receiver->InputAt(0);
111 }
112 ReferenceTypeInfo info = receiver->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000113 DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName();
114 if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100115 // We currently only support inlining with known receivers.
116 // TODO: Remove this check, we should be able to inline final methods
117 // on unknown receivers.
118 return nullptr;
119 } else if (info.GetTypeHandle()->IsInterface()) {
120 // Statically knowing that the receiver has an interface type cannot
121 // help us find what is the target method.
122 return nullptr;
123 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
124 // The method that we're trying to call is not in the receiver's class or super classes.
125 return nullptr;
126 }
127
128 ClassLinker* cl = Runtime::Current()->GetClassLinker();
129 size_t pointer_size = cl->GetImagePointerSize();
130 if (invoke->IsInvokeInterface()) {
131 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
132 resolved_method, pointer_size);
133 } else {
134 DCHECK(invoke->IsInvokeVirtual());
135 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
136 resolved_method, pointer_size);
137 }
138
139 if (resolved_method == nullptr) {
140 // The information we had on the receiver was not enough to find
141 // the target method. Since we check above the exact type of the receiver,
142 // the only reason this can happen is an IncompatibleClassChangeError.
143 return nullptr;
144 } else if (resolved_method->IsAbstract()) {
145 // The information we had on the receiver was not enough to find
146 // the target method. Since we check above the exact type of the receiver,
147 // the only reason this can happen is an IncompatibleClassChangeError.
148 return nullptr;
149 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
150 // A final method has to be the target method.
151 return resolved_method;
152 } else if (info.IsExact()) {
153 // If we found a method and the receiver's concrete type is statically
154 // known, we know for sure the target.
155 return resolved_method;
156 } else {
157 // Even if we did find a method, the receiver type was not enough to
158 // statically find the runtime target.
159 return nullptr;
160 }
161}
162
163static uint32_t FindMethodIndexIn(ArtMethod* method,
164 const DexFile& dex_file,
165 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100167 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
168 return method->GetDexMethodIndex();
169 } else {
170 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
171 }
172}
173
Nicolas Geoffray35071052015-06-09 15:43:38 +0100174bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000176 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
177 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178
Nicolas Geoffray35071052015-06-09 15:43:38 +0100179 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
180 // We can query the dex cache directly. The verifier has populated it already.
181 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
182 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100185 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000186 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000187 return false;
188 }
189
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100190 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
191 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
192 if (resolved_method == nullptr) {
193 VLOG(compiler) << "Interface or virtual call to "
194 << PrettyMethod(method_index, caller_dex_file)
195 << " could not be statically determined";
196 return false;
197 }
198 // We have found a method, but we need to find where that method is for the caller's
199 // dex file.
200 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
201 if (method_index == DexFile::kDexNoIndex) {
202 VLOG(compiler) << "Interface or virtual call to "
203 << PrettyMethod(resolved_method)
204 << " cannot be inlined because unaccessible to caller";
205 return false;
206 }
207 }
208
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100209 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000210 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100212 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 }
214
215 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
216
217 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000218 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000219 << " is not inlined because it is native";
220 return false;
221 }
222
223 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000224 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 << " is too big to inline";
226 return false;
227 }
228
229 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000230 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 << " is not inlined because of try block";
232 return false;
233 }
234
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100235 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
236 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
237 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000238 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100239 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 return false;
241 }
242
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000243 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000244 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000245 << " was already flagged as non inlineable";
246 return false;
247 }
248
Roland Levillain4c0eb422015-04-24 16:43:49 +0100249 if (invoke_instruction->IsInvokeStaticOrDirect() &&
250 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
251 // Case of a static method that cannot be inlined because it implicitly
252 // requires an initialization check of its declaring class.
253 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
254 << " is not inlined because it is static and requires a clinit"
255 << " check that cannot be emitted due to Dex cache limitations";
256 return false;
257 }
258
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100259 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000260 return false;
261 }
262
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000263 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000264 MaybeRecordStat(kInlinedInvoke);
265 return true;
266}
267
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000269 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100270 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000271 ScopedObjectAccess soa(Thread::Current());
272 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100273 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
274 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000275 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000276 DexCompilationUnit dex_compilation_unit(
277 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000278 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000279 class_linker,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000280 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000281 code_item,
282 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000283 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000284 resolved_method->GetAccessFlags(),
285 nullptr);
286
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100287 bool requires_ctor_barrier = false;
288
289 if (dex_compilation_unit.IsConstructor()) {
290 // If it's a super invocation and we already generate a barrier there's no need
291 // to generate another one.
292 // We identify super calls by looking at the "this" pointer. If its value is the
293 // same as the local "this" pointer then we must have a super invocation.
294 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
295 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
296 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
297 requires_ctor_barrier = false;
298 } else {
299 Thread* self = Thread::Current();
300 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
301 dex_compilation_unit.GetDexFile(),
302 dex_compilation_unit.GetClassDefIndex());
303 }
304 }
305
Nicolas Geoffray35071052015-06-09 15:43:38 +0100306 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
307 if (invoke_type == kInterface) {
308 // We have statically resolved the dispatch. To please the class linker
309 // at runtime, we change this call as if it was a virtual call.
310 invoke_type = kVirtual;
311 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000312 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100314 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100316 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100318 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 graph_->IsDebuggable(),
320 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000321
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000322 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000323 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000324 &dex_compilation_unit,
325 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000326 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000328 &inline_stats,
329 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000330
David Brazdil5e8b1372015-01-23 14:39:08 +0000331 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100332 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000333 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100334 // There could be multiple reasons why the graph could not be built, including
335 // unaccessible methods/fields due to using a different dex cache. We do not mark
336 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000337 return false;
338 }
339
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000340 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
341 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100342 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000343 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100344 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000345 return false;
346 }
347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000348 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100349 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100351 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352 return false;
353 }
354
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000355 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100356 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000357 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100358 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000359 InstructionSimplifier simplify(callee_graph, stats_);
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100360 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000361
362 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100363 &intrinsics,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000364 &dce,
365 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100366 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000367 &simplify,
368 };
369
370 for (size_t i = 0; i < arraysize(optimizations); ++i) {
371 HOptimization* optimization = optimizations[i];
372 optimization->Run();
373 }
374
375 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000376 HInliner inliner(callee_graph,
377 outer_compilation_unit_,
378 dex_compilation_unit,
379 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100380 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000381 stats_,
382 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000383 inliner.Run();
384 }
385
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100386 // TODO: We should abort only if all predecessors throw. However,
387 // HGraph::InlineInto currently does not handle an exit block with
388 // a throw predecessor.
389 HBasicBlock* exit_block = callee_graph->GetExitBlock();
390 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100391 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100392 << " could not be inlined because it has an infinite loop";
393 resolved_method->SetShouldNotInline();
394 return false;
395 }
396
397 bool has_throw_predecessor = false;
398 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
399 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
400 has_throw_predecessor = true;
401 break;
402 }
403 }
404 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100405 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100406 << " could not be inlined because one branch always throws";
407 resolved_method->SetShouldNotInline();
408 return false;
409 }
410
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000411 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000412 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 for (; !it.Done(); it.Advance()) {
414 HBasicBlock* block = it.Current();
415 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100416 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100418 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000419 return false;
420 }
421
422 for (HInstructionIterator instr_it(block->GetInstructions());
423 !instr_it.Done();
424 instr_it.Advance()) {
425 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000426
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100427 if (current->IsInvokeInterface()) {
428 // Disable inlining of interface calls. The cost in case of entering the
429 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100430 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100432 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 return false;
434 }
435
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100437 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000438 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100439 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 return false;
441 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000442
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100444 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000445 << " could not be inlined because " << current->DebugName()
446 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100447 // Do not flag the method as not-inlineable. A caller within the same
448 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000449 return false;
450 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 }
452 }
453
Calin Juravle2e768302015-07-28 14:41:11 +0000454 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
455
456 // When merging the graph we might create a new NullConstant in the caller graph which does
457 // not have the chance to be typed. We assign the correct type here so that we can keep the
458 // assertion that every reference has a valid type. This also simplifies checks along the way.
459 HNullConstant* null_constant = graph_->GetNullConstant();
460 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
461 ReferenceTypeInfo::TypeHandle obj_handle =
462 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
463 null_constant->SetReferenceTypeInfo(
464 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
465 }
466
467 if ((return_replacement != nullptr)
468 && (return_replacement->GetType() == Primitive::kPrimNot)) {
469 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
470 // Make sure that we have a valid type for the return. We may get an invalid one when
471 // we inline invokes with multiple branches and create a Phi for the result.
472 // TODO: we could be more precise by merging the phi inputs but that requires
473 // some functionality from the reference type propagation.
474 DCHECK(return_replacement->IsPhi());
475 ReferenceTypeInfo::TypeHandle return_handle =
476 handles_->NewHandle(resolved_method->GetReturnType());
477 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
478 return_handle, return_handle->IsFinal() /* is_exact */));
479 }
480 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000481
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000482 return true;
483}
484
485} // namespace art