blob: 5c73fd869640cf75777d1fed95c6553e14acec8b [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"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000027#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
29#include "nodes.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000030#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000031#include "ssa_phi_elimination.h"
32#include "scoped_thread_state_change.h"
33#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010034#include "dex/verified_method.h"
35#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036
37namespace art {
38
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010039static constexpr int kMaxInlineCodeUnits = 18;
40static constexpr int kDepthLimit = 3;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000041
42void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000043 if (graph_->IsDebuggable()) {
44 // For simplicity, we currently never inline when the graph is debuggable. This avoids
45 // doing some logic in the runtime to discover if a method could have been inlined.
46 return;
47 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000048 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010049 HBasicBlock* next_block = blocks.Get(0);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000050 for (size_t i = 0; i < blocks.Size(); ++i) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010051 // Because we are changing the graph when inlining, we need to remember the next block.
52 // This avoids doing the inlining work again on the inlined blocks.
53 if (blocks.Get(i) != next_block) {
54 continue;
55 }
56 HBasicBlock* block = next_block;
57 next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000058 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
59 HInstruction* next = instruction->GetNext();
Nicolas Geoffray6e475862015-06-08 15:52:23 +000060 HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070061 // As long as the call is not intrinsified, it is worth trying to inline.
62 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000063 // We use the original invoke type to ensure the resolution of the called method
64 // works properly.
65 if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000066 if (kIsDebugBuild) {
67 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000068 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000069 bool should_inline = callee_name.find("$inline$") != std::string::npos;
70 CHECK(!should_inline) << "Could not inline " << callee_name;
71 }
Guillaume "Vermeille" Sancheze918d382015-06-03 15:32:41 +010072 } else {
73 if (kIsDebugBuild) {
74 std::string callee_name =
75 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
76 bool must_not_inline = callee_name.find("$noinline$") != std::string::npos;
77 CHECK(!must_not_inline) << "Should not have inlined " << callee_name;
78 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000079 }
80 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000081 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082 }
83 }
84}
85
86bool HInliner::TryInline(HInvoke* invoke_instruction,
87 uint32_t method_index,
88 InvokeType invoke_type) const {
89 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000090 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
91 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092
Nicolas Geoffray6e475862015-06-08 15:52:23 +000093 StackHandleScope<3> hs(soa.Self());
94 Handle<mirror::DexCache> dex_cache(
95 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
96 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
97 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
98 ArtMethod* resolved_method(compiler_driver_->ResolveMethod(
99 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000100
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101 if (resolved_method == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000102 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000103 return false;
104 }
105
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100106 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000107 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100109 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000110 }
111
112 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
113
114 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000115 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 << " is not inlined because it is native";
117 return false;
118 }
119
120 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000121 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000122 << " is too big to inline";
123 return false;
124 }
125
126 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000127 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000128 << " is not inlined because of try block";
129 return false;
130 }
131
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100132 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
133 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
134 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000135 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100136 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 return false;
138 }
139
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000140 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000141 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000142 << " was already flagged as non inlineable";
143 return false;
144 }
145
Roland Levillain4c0eb422015-04-24 16:43:49 +0100146 if (invoke_instruction->IsInvokeStaticOrDirect() &&
147 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
148 // Case of a static method that cannot be inlined because it implicitly
149 // requires an initialization check of its declaring class.
150 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
151 << " is not inlined because it is static and requires a clinit"
152 << " check that cannot be emitted due to Dex cache limitations";
153 return false;
154 }
155
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100156 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000157 return false;
158 }
159
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000160 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000161 MaybeRecordStat(kInlinedInvoke);
162 return true;
163}
164
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000166 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000167 uint32_t method_index,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100168 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000169 ScopedObjectAccess soa(Thread::Current());
170 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000171 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 DexCompilationUnit dex_compilation_unit(
174 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000175 caller_compilation_unit_.GetClassLoader(),
176 caller_compilation_unit_.GetClassLinker(),
177 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 code_item,
179 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000180 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 resolved_method->GetAccessFlags(),
182 nullptr);
183
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100184 bool requires_ctor_barrier = false;
185
186 if (dex_compilation_unit.IsConstructor()) {
187 // If it's a super invocation and we already generate a barrier there's no need
188 // to generate another one.
189 // We identify super calls by looking at the "this" pointer. If its value is the
190 // same as the local "this" pointer then we must have a super invocation.
191 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
192 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
193 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
194 requires_ctor_barrier = false;
195 } else {
196 Thread* self = Thread::Current();
197 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
198 dex_compilation_unit.GetDexFile(),
199 dex_compilation_unit.GetClassDefIndex());
200 }
201 }
202
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000203 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100204 graph_->GetArena(),
205 caller_dex_file,
206 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100207 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 compiler_driver_->GetInstructionSet(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100209 invoke_instruction->GetOriginalInvokeType(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100210 graph_->IsDebuggable(),
211 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000214 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 &dex_compilation_unit,
216 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000217 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 compiler_driver_,
219 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220
David Brazdil5e8b1372015-01-23 14:39:08 +0000221 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000222 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100224 // There could be multiple reasons why the graph could not be built, including
225 // unaccessible methods/fields due to using a different dex cache. We do not mark
226 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 return false;
228 }
229
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000230 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
231 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000232 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000233 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100234 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000235 return false;
236 }
237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000239 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100241 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 return false;
243 }
244
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000245 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100246 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000247 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000248 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000249
250 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000251 &dce,
252 &fold,
253 &simplify,
254 };
255
256 for (size_t i = 0; i < arraysize(optimizations); ++i) {
257 HOptimization* optimization = optimizations[i];
258 optimization->Run();
259 }
260
261 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000262 HInliner inliner(callee_graph,
263 outer_compilation_unit_,
264 dex_compilation_unit,
265 compiler_driver_,
266 stats_,
267 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000268 inliner.Run();
269 }
270
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100271 // TODO: We should abort only if all predecessors throw. However,
272 // HGraph::InlineInto currently does not handle an exit block with
273 // a throw predecessor.
274 HBasicBlock* exit_block = callee_graph->GetExitBlock();
275 if (exit_block == nullptr) {
276 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
277 << " could not be inlined because it has an infinite loop";
278 resolved_method->SetShouldNotInline();
279 return false;
280 }
281
282 bool has_throw_predecessor = false;
283 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
284 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
285 has_throw_predecessor = true;
286 break;
287 }
288 }
289 if (has_throw_predecessor) {
290 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
291 << " could not be inlined because one branch always throws";
292 resolved_method->SetShouldNotInline();
293 return false;
294 }
295
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000296 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000297 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000298 for (; !it.Done(); it.Advance()) {
299 HBasicBlock* block = it.Current();
300 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000301 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000302 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100303 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000304 return false;
305 }
306
307 for (HInstructionIterator instr_it(block->GetInstructions());
308 !instr_it.Done();
309 instr_it.Advance()) {
310 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000311
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100312 if (current->IsInvokeInterface()) {
313 // Disable inlining of interface calls. The cost in case of entering the
314 // resolution conflict is currently too high.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000315 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100316 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100317 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000318 return false;
319 }
320
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100321 if (!same_dex_file && current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000322 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000323 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000325 return false;
326 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000327
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100328 if (!same_dex_file && current->NeedsDexCache()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000329 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
330 << " could not be inlined because " << current->DebugName()
331 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100332 // Do not flag the method as not-inlineable. A caller within the same
333 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000334 return false;
335 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000336 }
337 }
338
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000339 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000340
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000341 return true;
342}
343
344} // namespace art