blob: f0e644477554a0040246f483ac3fb62894357f37 [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 Juravle7733bd62015-07-22 17:14:50 +0000113 if (info.IsTop()) {
114 // We have no information on the receiver.
115 return nullptr;
116 } else if (!info.IsExact()) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100117 // We currently only support inlining with known receivers.
118 // TODO: Remove this check, we should be able to inline final methods
119 // on unknown receivers.
120 return nullptr;
121 } else if (info.GetTypeHandle()->IsInterface()) {
122 // Statically knowing that the receiver has an interface type cannot
123 // help us find what is the target method.
124 return nullptr;
125 } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) {
126 // The method that we're trying to call is not in the receiver's class or super classes.
127 return nullptr;
128 }
129
130 ClassLinker* cl = Runtime::Current()->GetClassLinker();
131 size_t pointer_size = cl->GetImagePointerSize();
132 if (invoke->IsInvokeInterface()) {
133 resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface(
134 resolved_method, pointer_size);
135 } else {
136 DCHECK(invoke->IsInvokeVirtual());
137 resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual(
138 resolved_method, pointer_size);
139 }
140
141 if (resolved_method == nullptr) {
142 // The information we had on the receiver was not enough to find
143 // the target method. Since we check above the exact type of the receiver,
144 // the only reason this can happen is an IncompatibleClassChangeError.
145 return nullptr;
146 } else if (resolved_method->IsAbstract()) {
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;
151 } else if (IsMethodOrDeclaringClassFinal(resolved_method)) {
152 // A final method has to be the target method.
153 return resolved_method;
154 } else if (info.IsExact()) {
155 // If we found a method and the receiver's concrete type is statically
156 // known, we know for sure the target.
157 return resolved_method;
158 } else {
159 // Even if we did find a method, the receiver type was not enough to
160 // statically find the runtime target.
161 return nullptr;
162 }
163}
164
165static uint32_t FindMethodIndexIn(ArtMethod* method,
166 const DexFile& dex_file,
167 uint32_t referrer_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100169 if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) {
170 return method->GetDexMethodIndex();
171 } else {
172 return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index);
173 }
174}
175
Nicolas Geoffray35071052015-06-09 15:43:38 +0100176bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000178 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
179 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180
Nicolas Geoffray35071052015-06-09 15:43:38 +0100181 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
182 // We can query the dex cache directly. The verifier has populated it already.
183 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
184 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +0100187 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000188 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 return false;
190 }
191
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100192 if (!invoke_instruction->IsInvokeStaticOrDirect()) {
193 resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method);
194 if (resolved_method == nullptr) {
195 VLOG(compiler) << "Interface or virtual call to "
196 << PrettyMethod(method_index, caller_dex_file)
197 << " could not be statically determined";
198 return false;
199 }
200 // We have found a method, but we need to find where that method is for the caller's
201 // dex file.
202 method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index);
203 if (method_index == DexFile::kDexNoIndex) {
204 VLOG(compiler) << "Interface or virtual call to "
205 << PrettyMethod(resolved_method)
206 << " cannot be inlined because unaccessible to caller";
207 return false;
208 }
209 }
210
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100211 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000212 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100214 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 }
216
217 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
218
219 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000220 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 << " is not inlined because it is native";
222 return false;
223 }
224
225 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000226 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 << " is too big to inline";
228 return false;
229 }
230
231 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000232 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000233 << " is not inlined because of try block";
234 return false;
235 }
236
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100237 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
238 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
239 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000240 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100241 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 return false;
243 }
244
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000245 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000246 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000247 << " was already flagged as non inlineable";
248 return false;
249 }
250
Roland Levillain4c0eb422015-04-24 16:43:49 +0100251 if (invoke_instruction->IsInvokeStaticOrDirect() &&
252 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
253 // Case of a static method that cannot be inlined because it implicitly
254 // requires an initialization check of its declaring class.
255 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
256 << " is not inlined because it is static and requires a clinit"
257 << " check that cannot be emitted due to Dex cache limitations";
258 return false;
259 }
260
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100261 if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000262 return false;
263 }
264
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000265 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000266 MaybeRecordStat(kInlinedInvoke);
267 return true;
268}
269
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000271 HInvoke* invoke_instruction,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100272 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000273 ScopedObjectAccess soa(Thread::Current());
274 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100275 const DexFile& callee_dex_file = *resolved_method->GetDexFile();
276 uint32_t method_index = resolved_method->GetDexMethodIndex();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000277
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000278 DexCompilationUnit dex_compilation_unit(
279 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000280 caller_compilation_unit_.GetClassLoader(),
281 caller_compilation_unit_.GetClassLinker(),
282 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000283 code_item,
284 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000285 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000286 resolved_method->GetAccessFlags(),
287 nullptr);
288
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100289 bool requires_ctor_barrier = false;
290
291 if (dex_compilation_unit.IsConstructor()) {
292 // If it's a super invocation and we already generate a barrier there's no need
293 // to generate another one.
294 // We identify super calls by looking at the "this" pointer. If its value is the
295 // same as the local "this" pointer then we must have a super invocation.
296 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
297 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
298 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
299 requires_ctor_barrier = false;
300 } else {
301 Thread* self = Thread::Current();
302 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
303 dex_compilation_unit.GetDexFile(),
304 dex_compilation_unit.GetClassDefIndex());
305 }
306 }
307
Nicolas Geoffray35071052015-06-09 15:43:38 +0100308 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
309 if (invoke_type == kInterface) {
310 // We have statically resolved the dispatch. To please the class linker
311 // at runtime, we change this call as if it was a virtual call.
312 invoke_type = kVirtual;
313 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000314 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 graph_->GetArena(),
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100316 callee_dex_file,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100318 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100320 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 graph_->IsDebuggable(),
322 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000323
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000324 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000325 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000326 &dex_compilation_unit,
327 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000328 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000329 compiler_driver_,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000330 &inline_stats,
331 resolved_method->GetQuickenedInfo());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000332
David Brazdil5e8b1372015-01-23 14:39:08 +0000333 if (!builder.BuildGraph(*code_item)) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100334 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000335 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100336 // There could be multiple reasons why the graph could not be built, including
337 // unaccessible methods/fields due to using a different dex cache. We do not mark
338 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 return false;
340 }
341
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000342 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
343 compiler_driver_->GetInstructionSet())) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100344 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000345 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100346 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000347 return false;
348 }
349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000350 if (!callee_graph->TryBuildingSsa()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100351 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100353 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 return false;
355 }
356
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000357 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100358 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000359 HConstantFolding fold(callee_graph);
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100360 ReferenceTypePropagation type_propagation(callee_graph, handles_);
Calin Juravleacf735c2015-02-12 15:25:22 +0000361 InstructionSimplifier simplify(callee_graph, stats_);
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100362 IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000363
364 HOptimization* optimizations[] = {
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100365 &intrinsics,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000366 &dce,
367 &fold,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100368 &type_propagation,
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000369 &simplify,
370 };
371
372 for (size_t i = 0; i < arraysize(optimizations); ++i) {
373 HOptimization* optimization = optimizations[i];
374 optimization->Run();
375 }
376
377 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000378 HInliner inliner(callee_graph,
379 outer_compilation_unit_,
380 dex_compilation_unit,
381 compiler_driver_,
Nicolas Geoffray454a4812015-06-09 10:37:32 +0100382 handles_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000383 stats_,
384 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000385 inliner.Run();
386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 // TODO: We should abort only if all predecessors throw. However,
389 // HGraph::InlineInto currently does not handle an exit block with
390 // a throw predecessor.
391 HBasicBlock* exit_block = callee_graph->GetExitBlock();
392 if (exit_block == nullptr) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100393 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100394 << " could not be inlined because it has an infinite loop";
395 resolved_method->SetShouldNotInline();
396 return false;
397 }
398
399 bool has_throw_predecessor = false;
400 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
401 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
402 has_throw_predecessor = true;
403 break;
404 }
405 }
406 if (has_throw_predecessor) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100407 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100408 << " could not be inlined because one branch always throws";
409 resolved_method->SetShouldNotInline();
410 return false;
411 }
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000414 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 for (; !it.Done(); it.Advance()) {
416 HBasicBlock* block = it.Current();
417 if (block->IsLoopHeader()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100418 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000419 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100420 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 return false;
422 }
423
424 for (HInstructionIterator instr_it(block->GetInstructions());
425 !instr_it.Done();
426 instr_it.Advance()) {
427 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000428
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 if (current->IsInvokeInterface()) {
430 // Disable inlining of interface calls. The cost in case of entering the
431 // resolution conflict is currently too high.
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100432 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100433 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100434 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000435 return false;
436 }
437
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 if (!same_dex_file && current->NeedsEnvironment()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100439 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100441 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 return false;
443 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000444
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 if (!same_dex_file && current->NeedsDexCache()) {
Guillaume "Vermeille" Sanchezae09d2d2015-05-29 10:52:55 +0100446 VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file)
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000447 << " could not be inlined because " << current->DebugName()
448 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100449 // Do not flag the method as not-inlineable. A caller within the same
450 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000451 return false;
452 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 }
454 }
455
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000456 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000458 return true;
459}
460
461} // namespace art