blob: c3fc33735a0dc628a3764f4b4260cf800fd593f1 [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.
Nicolas Geoffray35071052015-06-09 15:43:38 +010065 if (!TryInline(call, call->GetDexMethodIndex())) {
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
Nicolas Geoffray35071052015-06-09 15:43:38 +010086bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000087 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000088 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
89 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000090
Nicolas Geoffray35071052015-06-09 15:43:38 +010091 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
92 // We can query the dex cache directly. The verifier has populated it already.
93 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
94 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095
Mathieu Chartiere401d142015-04-22 13:56:20 -070096 if (resolved_method == nullptr) {
Nicolas Geoffray35071052015-06-09 15:43:38 +010097 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +000098 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000099 return false;
100 }
101
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100102 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000103 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000104 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100105 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000106 }
107
108 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
109
110 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000111 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000112 << " is not inlined because it is native";
113 return false;
114 }
115
116 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000117 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 << " is too big to inline";
119 return false;
120 }
121
122 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000123 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 << " is not inlined because of try block";
125 return false;
126 }
127
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100128 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
129 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
130 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000131 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100132 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000133 return false;
134 }
135
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000136 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000137 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000138 << " was already flagged as non inlineable";
139 return false;
140 }
141
Roland Levillain4c0eb422015-04-24 16:43:49 +0100142 if (invoke_instruction->IsInvokeStaticOrDirect() &&
143 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
144 // Case of a static method that cannot be inlined because it implicitly
145 // requires an initialization check of its declaring class.
146 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
147 << " is not inlined because it is static and requires a clinit"
148 << " check that cannot be emitted due to Dex cache limitations";
149 return false;
150 }
151
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100152 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000153 return false;
154 }
155
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000156 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000157 MaybeRecordStat(kInlinedInvoke);
158 return true;
159}
160
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000162 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000163 uint32_t method_index,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100164 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000165 ScopedObjectAccess soa(Thread::Current());
166 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000167 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000168
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 DexCompilationUnit dex_compilation_unit(
170 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000171 caller_compilation_unit_.GetClassLoader(),
172 caller_compilation_unit_.GetClassLinker(),
173 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 code_item,
175 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000176 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 resolved_method->GetAccessFlags(),
178 nullptr);
179
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100180 bool requires_ctor_barrier = false;
181
182 if (dex_compilation_unit.IsConstructor()) {
183 // If it's a super invocation and we already generate a barrier there's no need
184 // to generate another one.
185 // We identify super calls by looking at the "this" pointer. If its value is the
186 // same as the local "this" pointer then we must have a super invocation.
187 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
188 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
189 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
190 requires_ctor_barrier = false;
191 } else {
192 Thread* self = Thread::Current();
193 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
194 dex_compilation_unit.GetDexFile(),
195 dex_compilation_unit.GetClassDefIndex());
196 }
197 }
198
Nicolas Geoffray35071052015-06-09 15:43:38 +0100199 InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType();
200 if (invoke_type == kInterface) {
201 // We have statically resolved the dispatch. To please the class linker
202 // at runtime, we change this call as if it was a virtual call.
203 invoke_type = kVirtual;
204 }
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000205 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100206 graph_->GetArena(),
207 caller_dex_file,
208 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100209 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray35071052015-06-09 15:43:38 +0100211 invoke_type,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100212 graph_->IsDebuggable(),
213 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000216 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217 &dex_compilation_unit,
218 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000219 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 compiler_driver_,
221 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222
David Brazdil5e8b1372015-01-23 14:39:08 +0000223 if (!builder.BuildGraph(*code_item)) {
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 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100226 // There could be multiple reasons why the graph could not be built, including
227 // unaccessible methods/fields due to using a different dex cache. We do not mark
228 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229 return false;
230 }
231
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000232 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
233 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000234 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000235 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100236 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000237 return false;
238 }
239
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000241 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100243 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 return false;
245 }
246
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000247 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100248 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000249 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000250 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000251
252 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000253 &dce,
254 &fold,
255 &simplify,
256 };
257
258 for (size_t i = 0; i < arraysize(optimizations); ++i) {
259 HOptimization* optimization = optimizations[i];
260 optimization->Run();
261 }
262
263 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000264 HInliner inliner(callee_graph,
265 outer_compilation_unit_,
266 dex_compilation_unit,
267 compiler_driver_,
268 stats_,
269 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000270 inliner.Run();
271 }
272
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100273 // TODO: We should abort only if all predecessors throw. However,
274 // HGraph::InlineInto currently does not handle an exit block with
275 // a throw predecessor.
276 HBasicBlock* exit_block = callee_graph->GetExitBlock();
277 if (exit_block == nullptr) {
278 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
279 << " could not be inlined because it has an infinite loop";
280 resolved_method->SetShouldNotInline();
281 return false;
282 }
283
284 bool has_throw_predecessor = false;
285 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
286 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
287 has_throw_predecessor = true;
288 break;
289 }
290 }
291 if (has_throw_predecessor) {
292 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
293 << " could not be inlined because one branch always throws";
294 resolved_method->SetShouldNotInline();
295 return false;
296 }
297
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000298 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000299 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000300 for (; !it.Done(); it.Advance()) {
301 HBasicBlock* block = it.Current();
302 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000303 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000304 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100305 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306 return false;
307 }
308
309 for (HInstructionIterator instr_it(block->GetInstructions());
310 !instr_it.Done();
311 instr_it.Advance()) {
312 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000313
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100314 if (current->IsInvokeInterface()) {
315 // Disable inlining of interface calls. The cost in case of entering the
316 // resolution conflict is currently too high.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000317 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100318 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100319 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000320 return false;
321 }
322
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100323 if (!same_dex_file && current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000324 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000325 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100326 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327 return false;
328 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000329
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 if (!same_dex_file && current->NeedsDexCache()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000331 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
332 << " could not be inlined because " << current->DebugName()
333 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100334 // Do not flag the method as not-inlineable. A caller within the same
335 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000336 return false;
337 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000338 }
339 }
340
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000341 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000342
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000343 return true;
344}
345
346} // namespace art