blob: 83f8d83bc49784003fa9a5920faf2540688c488f [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();
60 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 }
72 }
73 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000074 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000075 }
76 }
77}
78
79bool HInliner::TryInline(HInvoke* invoke_instruction,
80 uint32_t method_index,
81 InvokeType invoke_type) const {
82 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000083 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
84 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000085
86 StackHandleScope<3> hs(soa.Self());
87 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000088 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000089 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000090 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 ArtMethod* resolved_method(compiler_driver_->ResolveMethod(
92 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000093
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 if (resolved_method == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000095 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000096 return false;
97 }
98
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010099 bool same_dex_file = true;
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000100 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100102 same_dex_file = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000103 }
104
105 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
106
107 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000108 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000109 << " is not inlined because it is native";
110 return false;
111 }
112
113 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000114 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 << " is too big to inline";
116 return false;
117 }
118
119 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000120 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000121 << " is not inlined because of try block";
122 return false;
123 }
124
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100125 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
126 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
127 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000128 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100129 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000130 return false;
131 }
132
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000133 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000134 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000135 << " was already flagged as non inlineable";
136 return false;
137 }
138
Roland Levillain4c0eb422015-04-24 16:43:49 +0100139 if (invoke_instruction->IsInvokeStaticOrDirect() &&
140 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
141 // Case of a static method that cannot be inlined because it implicitly
142 // requires an initialization check of its declaring class.
143 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
144 << " is not inlined because it is static and requires a clinit"
145 << " check that cannot be emitted due to Dex cache limitations";
146 return false;
147 }
148
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100149 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000150 return false;
151 }
152
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000153 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000154 MaybeRecordStat(kInlinedInvoke);
155 return true;
156}
157
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000159 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000160 uint32_t method_index,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100161 bool same_dex_file) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000162 ScopedObjectAccess soa(Thread::Current());
163 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000164 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000165
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000166 DexCompilationUnit dex_compilation_unit(
167 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000168 caller_compilation_unit_.GetClassLoader(),
169 caller_compilation_unit_.GetClassLinker(),
170 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 code_item,
172 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000173 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 resolved_method->GetAccessFlags(),
175 nullptr);
176
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100177 bool requires_ctor_barrier = false;
178
179 if (dex_compilation_unit.IsConstructor()) {
180 // If it's a super invocation and we already generate a barrier there's no need
181 // to generate another one.
182 // We identify super calls by looking at the "this" pointer. If its value is the
183 // same as the local "this" pointer then we must have a super invocation.
184 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
185 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
186 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
187 requires_ctor_barrier = false;
188 } else {
189 Thread* self = Thread::Current();
190 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
191 dex_compilation_unit.GetDexFile(),
192 dex_compilation_unit.GetClassDefIndex());
193 }
194 }
195
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000196 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100197 graph_->GetArena(),
198 caller_dex_file,
199 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100200 requires_ctor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201 compiler_driver_->GetInstructionSet(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100202 invoke_instruction->GetOriginalInvokeType(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100203 graph_->IsDebuggable(),
204 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000206 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000207 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000208 &dex_compilation_unit,
209 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000210 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 compiler_driver_,
212 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213
David Brazdil5e8b1372015-01-23 14:39:08 +0000214 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000215 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 << " could not be built, so cannot be inlined";
Nicolas Geoffray5ae13252015-05-27 12:53:36 +0100217 // There could be multiple reasons why the graph could not be built, including
218 // unaccessible methods/fields due to using a different dex cache. We do not mark
219 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 return false;
221 }
222
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000223 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
224 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000225 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000226 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100227 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000228 return false;
229 }
230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 if (!callee_graph->TryBuildingSsa()) {
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 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100234 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 return false;
236 }
237
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000238 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100239 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000240 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000241 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000242
243 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000244 &dce,
245 &fold,
246 &simplify,
247 };
248
249 for (size_t i = 0; i < arraysize(optimizations); ++i) {
250 HOptimization* optimization = optimizations[i];
251 optimization->Run();
252 }
253
254 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000255 HInliner inliner(callee_graph,
256 outer_compilation_unit_,
257 dex_compilation_unit,
258 compiler_driver_,
259 stats_,
260 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000261 inliner.Run();
262 }
263
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100264 // TODO: We should abort only if all predecessors throw. However,
265 // HGraph::InlineInto currently does not handle an exit block with
266 // a throw predecessor.
267 HBasicBlock* exit_block = callee_graph->GetExitBlock();
268 if (exit_block == nullptr) {
269 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
270 << " could not be inlined because it has an infinite loop";
271 resolved_method->SetShouldNotInline();
272 return false;
273 }
274
275 bool has_throw_predecessor = false;
276 for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) {
277 if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) {
278 has_throw_predecessor = true;
279 break;
280 }
281 }
282 if (has_throw_predecessor) {
283 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
284 << " could not be inlined because one branch always throws";
285 resolved_method->SetShouldNotInline();
286 return false;
287 }
288
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000289 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000290 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000291 for (; !it.Done(); it.Advance()) {
292 HBasicBlock* block = it.Current();
293 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000294 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000295 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100296 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000297 return false;
298 }
299
300 for (HInstructionIterator instr_it(block->GetInstructions());
301 !instr_it.Done();
302 instr_it.Advance()) {
303 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000304
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100305 if (current->IsInvokeInterface()) {
306 // Disable inlining of interface calls. The cost in case of entering the
307 // resolution conflict is currently too high.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000308 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100309 << " could not be inlined because it has an interface call.";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100310 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000311 return false;
312 }
313
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100314 if (!same_dex_file && current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000315 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000316 << " could not be inlined because " << current->DebugName()
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100317 << " needs an environment and is in a different dex file";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000318 return false;
319 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000320
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100321 if (!same_dex_file && current->NeedsDexCache()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000322 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
323 << " could not be inlined because " << current->DebugName()
324 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100325 // Do not flag the method as not-inlineable. A caller within the same
326 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000327 return false;
328 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000329 }
330 }
331
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000332 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000333
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000334 return true;
335}
336
337} // namespace art