blob: a72817fade7f94eb228577b0bc543781b1cc6d69 [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
19#include "builder.h"
20#include "class_linker.h"
21#include "constant_folding.h"
22#include "dead_code_elimination.h"
23#include "driver/compiler_driver-inl.h"
24#include "driver/dex_compilation_unit.h"
25#include "instruction_simplifier.h"
26#include "mirror/art_method-inl.h"
27#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
39static constexpr int kMaxInlineCodeUnits = 100;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000040static constexpr int kDepthLimit = 5;
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();
49 for (size_t i = 0; i < blocks.Size(); ++i) {
50 HBasicBlock* block = blocks.Get(i);
51 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
52 HInstruction* next = instruction->GetNext();
53 HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070054 // As long as the call is not intrinsified, it is worth trying to inline.
55 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000056 // We use the original invoke type to ensure the resolution of the called method
57 // works properly.
58 if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059 if (kIsDebugBuild) {
60 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000061 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062 bool should_inline = callee_name.find("$inline$") != std::string::npos;
63 CHECK(!should_inline) << "Could not inline " << callee_name;
64 }
65 }
66 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000067 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000068 }
69 }
70}
71
72bool HInliner::TryInline(HInvoke* invoke_instruction,
73 uint32_t method_index,
74 InvokeType invoke_type) const {
75 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000076 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
77 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000078
79 StackHandleScope<3> hs(soa.Self());
80 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000081 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000083 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000084 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
85 compiler_driver_->ResolveMethod(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000086 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000087
88 if (resolved_method.Get() == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000089 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000090 return false;
91 }
92
Nicolas Geoffray9437b782015-03-25 10:08:51 +000093 bool can_use_dex_cache = true;
94 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000096 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000097 }
98
99 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
100
101 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000102 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000103 << " is not inlined because it is native";
104 return false;
105 }
106
107 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
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 too big to inline";
110 return false;
111 }
112
113 if (code_item->tries_size_ != 0) {
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 not inlined because of try block";
116 return false;
117 }
118
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100119 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
120 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
121 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000122 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100123 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 return false;
125 }
126
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000127 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000128 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000129 << " was already flagged as non inlineable";
130 return false;
131 }
132
Roland Levillain4c0eb422015-04-24 16:43:49 +0100133 if (invoke_instruction->IsInvokeStaticOrDirect() &&
134 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
135 // Case of a static method that cannot be inlined because it implicitly
136 // requires an initialization check of its declaring class.
137 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
138 << " is not inlined because it is static and requires a clinit"
139 << " check that cannot be emitted due to Dex cache limitations";
140 return false;
141 }
142
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000143 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000144 return false;
145 }
146
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000147 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000148 MaybeRecordStat(kInlinedInvoke);
149 return true;
150}
151
152bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method,
153 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000154 uint32_t method_index,
155 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000156 ScopedObjectAccess soa(Thread::Current());
157 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000158 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 DexCompilationUnit dex_compilation_unit(
161 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000162 caller_compilation_unit_.GetClassLoader(),
163 caller_compilation_unit_.GetClassLinker(),
164 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 code_item,
166 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000167 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168 resolved_method->GetAccessFlags(),
169 nullptr);
170
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100171 bool requires_ctor_barrier = false;
172
173 if (dex_compilation_unit.IsConstructor()) {
174 // If it's a super invocation and we already generate a barrier there's no need
175 // to generate another one.
176 // We identify super calls by looking at the "this" pointer. If its value is the
177 // same as the local "this" pointer then we must have a super invocation.
178 bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue()
179 && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis();
180 if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) {
181 requires_ctor_barrier = false;
182 } else {
183 Thread* self = Thread::Current();
184 requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self,
185 dex_compilation_unit.GetDexFile(),
186 dex_compilation_unit.GetClassDefIndex());
187 }
188 }
189
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000190 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100191 graph_->GetArena(),
192 caller_dex_file,
193 method_index,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100194 requires_ctor_barrier,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100195 invoke_instruction->GetOriginalInvokeType(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100196 graph_->IsDebuggable(),
197 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000200 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 &dex_compilation_unit,
202 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000203 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 compiler_driver_,
205 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000206
David Brazdil5e8b1372015-01-23 14:39:08 +0000207 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000208 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209 << " could not be built, so cannot be inlined";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100210 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 return false;
212 }
213
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000214 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
215 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000216 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000217 << " cannot be inlined because of the register allocator";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100218 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000219 return false;
220 }
221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 if (!callee_graph->TryBuildingSsa()) {
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 << " could not be transformed to SSA";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100225 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 return false;
227 }
228
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000229 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100230 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000231 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000232 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000233
234 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000235 &dce,
236 &fold,
237 &simplify,
238 };
239
240 for (size_t i = 0; i < arraysize(optimizations); ++i) {
241 HOptimization* optimization = optimizations[i];
242 optimization->Run();
243 }
244
245 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000246 HInliner inliner(callee_graph,
247 outer_compilation_unit_,
248 dex_compilation_unit,
249 compiler_driver_,
250 stats_,
251 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000252 inliner.Run();
253 }
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000256 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 for (; !it.Done(); it.Advance()) {
258 HBasicBlock* block = it.Current();
259 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000260 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 << " could not be inlined because it contains a loop";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100262 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000263 return false;
264 }
265
266 for (HInstructionIterator instr_it(block->GetInstructions());
267 !instr_it.Done();
268 instr_it.Advance()) {
269 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000270 if (current->IsSuspendCheck()) {
271 continue;
272 }
273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000275 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000276 << " could not be inlined because " << current->DebugName()
277 << " can throw";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100278 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000279 return false;
280 }
281
282 if (current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000283 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000284 << " could not be inlined because " << current->DebugName()
285 << " needs an environment";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100286 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000287 return false;
288 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000289
290 if (!can_use_dex_cache && current->NeedsDexCache()) {
291 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
292 << " could not be inlined because " << current->DebugName()
293 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffrayd0261432015-05-26 14:35:06 +0100294 // Do not flag the method as not-inlineable. A caller within the same
295 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000296 return false;
297 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000298 }
299 }
300
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000301 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000302
Mark Mendell1152c922015-04-24 17:06:35 -0400303 if (callee_graph->HasBoundsChecks()) {
304 graph_->SetHasBoundsChecks(true);
Mingyao Yange4335eb2015-03-02 15:14:13 -0800305 }
306
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000307 return true;
308}
309
310} // namespace art