blob: 4c746798be445570d597f84e681847b030d30760 [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.
Nicolas Geoffray35071052015-06-09 15:43:38 +010066 if (!TryInline(call, call->GetDexMethodIndex())) {
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
Nicolas Geoffray35071052015-06-09 15:43:38 +0100172bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000174 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
175 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176
Nicolas Geoffray35071052015-06-09 15:43:38 +0100177 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
178 // We can query the dex cache directly. The verifier has populated it already.
179 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
180 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100183 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000184 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 return false;
186 }
187
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100188 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
189 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
190 if (resolved_method == nullptr) {
191 VLOG(compiler) << "Interface or virtual call to "
192 << PrettyMethod(method_index, caller_dex_file)
193 << " could not be statically determined";
194 return false;
195 }
196 // We have found a method, but we need to find where that method is for the caller's
197 // dex file.
198 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
199 if (method_index == DexFile::kDexNoIndex) {
200 VLOG(compiler) << "Interface or virtual call to "
201 << PrettyMethod(resolved_method)
202 << " cannot be inlined because unaccessible to caller";
203 return false;
204 }
205 }
206
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100207 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000208 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100210 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 }
212
213 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
214
215 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000216 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217 << " is not inlined because it is native";
218 return false;
219 }
220
Calin Juravleec748352015-07-29 13:52:12 +0100221 size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
222 if (code_item->insns_size_in_code_units_ > inline_max_code_units) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000223 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 << " is too big to inline";
225 return false;
226 }
227
228 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000229 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230 << " is not inlined because of try block";
231 return false;
232 }
233
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100234 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
235 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
236 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000237 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100238 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 return false;
240 }
241
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000242 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000243 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000244 << " was already flagged as non inlineable";
245 return false;
246 }
247
Roland Levillain4c0eb422015-04-24 16:43:49 +0100248 if (invoke_instruction->IsInvokeStaticOrDirect() &&
249 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
250 // Case of a static method that cannot be inlined because it implicitly
251 // requires an initialization check of its declaring class.
252 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
253 << " is not inlined because it is static and requires a clinit"
254 << " check that cannot be emitted due to Dex cache limitations";
255 return false;
256 }
257
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100258 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000259 return false;
260 }
261
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000262 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000263 MaybeRecordStat(kInlinedInvoke);
264 return true;
265}
266
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000268 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100269 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000270 ScopedObjectAccess soa(Thread::Current());
271 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100272 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
273 uint32_t method_index = resolved_method->GetDexMethodIndex();
Calin Juravle2e768302015-07-28 14:41:11 +0000274 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000275 DexCompilationUnit dex_compilation_unit(
276 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000277 caller_compilation_unit_.GetClassLoader(),
Calin Juravle2e768302015-07-28 14:41:11 +0000278 class_linker,
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000279 callee_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000280 code_item,
281 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000282 method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000283 resolved_method->GetAccessFlags(),
Nicolas Geoffray8dbf0cf2015-08-11 02:14:38 +0000284 compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000285
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100286 bool requires_ctor_barrier = false;
287
288 if (dex_compilation_unit.IsConstructor()) {
289 // If it's a super invocation and we already generate a barrier there's no need
290 // to generate another one.
291 // We identify super calls by looking at the "this" pointer. If its value is the
292 // same as the local "this" pointer then we must have a super invocation.
293 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
294 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
295 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
296 requires_ctor_barrier = false;
297 } else {
298 Thread* self = Thread::Current();
299 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
300 dex_compilation_unit.GetDexFile(),
301 dex_compilation_unit.GetClassDefIndex());
302 }
303 }
304
Nicolas Geoffray35071052015-06-09 15:43:38 +0100305 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
306 if (invoke_type == kInterface) {
307 // We have statically resolved the dispatch. To please the class linker
308 // at runtime, we change this call as if it was a virtual call.
309 invoke_type = kVirtual;
310 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000311 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100313 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100315 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100317 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 graph_->IsDebuggable(),
319 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000320
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000321 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000322 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000323 &dex_compilation_unit,
324 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000325 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000326 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000327 &inline_stats,
328 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000329
David Brazdil5e8b1372015-01-23 14:39:08 +0000330 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100331 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000332 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100333 // There could be multiple reasons why the graph could not be built, including
334 // unaccessible methods/fields due to using a different dex cache. We do not mark
335 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000336 return false;
337 }
338
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000339 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
340 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100341 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000342 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100343 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000344 return false;
345 }
346
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000347 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100348 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100350 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 return false;
352 }
353
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000354 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100355 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000356 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100357 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000358 InstructionSimplifier simplify(callee_graph, stats_);
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100359 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000360
361 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100362 &intrinsics,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000363 &dce,
364 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100365 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000366 &simplify,
367 };
368
369 for (size_t i = 0; i < arraysize(optimizations); ++i) {
370 HOptimization* optimization = optimizations[i];
371 optimization->Run();
372 }
373
Calin Juravleec748352015-07-29 13:52:12 +0100374 if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000375 HInliner inliner(callee_graph,
376 outer_compilation_unit_,
377 dex_compilation_unit,
378 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100379 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000380 stats_,
381 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000382 inliner.Run();
383 }
384
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100385 // TODO: We should abort only if all predecessors throw. However,
386 // HGraph::InlineInto currently does not handle an exit block with
387 // a throw predecessor.
388 HBasicBlock* exit_block = callee_graph->GetExitBlock();
389 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100390 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100391 << " could not be inlined because it has an infinite loop";
392 resolved_method->SetShouldNotInline();
393 return false;
394 }
395
396 bool has_throw_predecessor = false;
397 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
398 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
399 has_throw_predecessor = true;
400 break;
401 }
402 }
403 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100404 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 << " could not be inlined because one branch always throws";
406 resolved_method->SetShouldNotInline();
407 return false;
408 }
409
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000411 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000412 for (; !it.Done(); it.Advance()) {
413 HBasicBlock* block = it.Current();
414 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100415 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100417 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 return false;
419 }
420
421 for (HInstructionIterator instr_it(block->GetInstructions());
422 !instr_it.Done();
423 instr_it.Advance()) {
424 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000425
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100426 if (current->IsInvokeInterface()) {
427 // Disable inlining of interface calls. The cost in case of entering the
428 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100429 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100430 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100431 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000432 return false;
433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100436 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000437 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 return false;
440 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000441
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100443 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000444 << " could not be inlined because " << current->DebugName()
445 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100446 // Do not flag the method as not-inlineable. A caller within the same
447 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000448 return false;
449 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 }
451 }
452
Calin Juravle2e768302015-07-28 14:41:11 +0000453 HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction);
454
455 // When merging the graph we might create a new NullConstant in the caller graph which does
456 // not have the chance to be typed. We assign the correct type here so that we can keep the
457 // assertion that every reference has a valid type. This also simplifies checks along the way.
458 HNullConstant* null_constant = graph_->GetNullConstant();
459 if (!null_constant->GetReferenceTypeInfo().IsValid()) {
460 ReferenceTypeInfo::TypeHandle obj_handle =
461 handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject));
462 null_constant->SetReferenceTypeInfo(
463 ReferenceTypeInfo::Create(obj_handle, false /* is_exact */));
464 }
465
466 if ((return_replacement != nullptr)
467 && (return_replacement->GetType() == Primitive::kPrimNot)) {
468 if (!return_replacement->GetReferenceTypeInfo().IsValid()) {
469 // Make sure that we have a valid type for the return. We may get an invalid one when
470 // we inline invokes with multiple branches and create a Phi for the result.
471 // TODO: we could be more precise by merging the phi inputs but that requires
472 // some functionality from the reference type propagation.
473 DCHECK(return_replacement->IsPhi());
474 ReferenceTypeInfo::TypeHandle return_handle =
475 handles_->NewHandle(resolved_method->GetReturnType());
476 return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create(
477 return_handle, return_handle->IsFinal() /* is_exact */));
478 }
479 }
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000480
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000481 return true;
482}
483
484} // namespace art