blob: 8253a433892746983e22250d01e979172202818a [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 Chartier3d21bdf2015-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
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())));
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070084 ArtMethod* resolved_method(compiler_driver_->ResolveMethod(
85 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000086
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070087 if (resolved_method == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000088 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000089 return false;
90 }
91
Nicolas Geoffray9437b782015-03-25 10:08:51 +000092 bool can_use_dex_cache = true;
93 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000094 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000095 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000096 }
97
98 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
99
100 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000101 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000102 << " is not inlined because it is native";
103 return false;
104 }
105
106 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000107 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000108 << " is too big to inline";
109 return false;
110 }
111
112 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000113 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000114 << " is not inlined because of try block";
115 return false;
116 }
117
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100118 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
119 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
120 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000121 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100122 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000123 return false;
124 }
125
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000126 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000127 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000128 << " was already flagged as non inlineable";
129 return false;
130 }
131
Roland Levillain4c0eb422015-04-24 16:43:49 +0100132 if (invoke_instruction->IsInvokeStaticOrDirect() &&
133 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
134 // Case of a static method that cannot be inlined because it implicitly
135 // requires an initialization check of its declaring class.
136 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
137 << " is not inlined because it is static and requires a clinit"
138 << " check that cannot be emitted due to Dex cache limitations";
139 return false;
140 }
141
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000142 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000143 return false;
144 }
145
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000146 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000147 MaybeRecordStat(kInlinedInvoke);
148 return true;
149}
150
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700151bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000152 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000153 uint32_t method_index,
154 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000155 ScopedObjectAccess soa(Thread::Current());
156 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000157 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000158
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000159 DexCompilationUnit dex_compilation_unit(
160 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000161 caller_compilation_unit_.GetClassLoader(),
162 caller_compilation_unit_.GetClassLinker(),
163 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 code_item,
165 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000166 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167 resolved_method->GetAccessFlags(),
168 nullptr);
169
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000170 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 graph_->GetArena(),
172 caller_dex_file,
173 method_index,
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700174 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100175 graph_->IsDebuggable(),
176 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000179 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 &dex_compilation_unit,
181 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000182 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 compiler_driver_,
184 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185
David Brazdil5e8b1372015-01-23 14:39:08 +0000186 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000187 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 << " could not be built, so cannot be inlined";
Nicolas Geoffray26531492015-05-27 12:53:36 +0100189 // There could be multiple reasons why the graph could not be built, including
190 // unaccessible methods/fields due to using a different dex cache. We do not mark
191 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000192 return false;
193 }
194
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000195 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
196 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000197 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000198 << " cannot be inlined because of the register allocator";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100199 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000200 return false;
201 }
202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000204 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000205 << " could not be transformed to SSA";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100206 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000207 return false;
208 }
209
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000210 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100211 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000212 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000213 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000214
215 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000216 &dce,
217 &fold,
218 &simplify,
219 };
220
221 for (size_t i = 0; i < arraysize(optimizations); ++i) {
222 HOptimization* optimization = optimizations[i];
223 optimization->Run();
224 }
225
226 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000227 HInliner inliner(callee_graph,
228 outer_compilation_unit_,
229 dex_compilation_unit,
230 compiler_driver_,
231 stats_,
232 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000233 inliner.Run();
234 }
235
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000236 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000237 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 for (; !it.Done(); it.Advance()) {
239 HBasicBlock* block = it.Current();
240 if (block->IsLoopHeader()) {
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 inlined because it contains a loop";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100243 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 return false;
245 }
246
247 for (HInstructionIterator instr_it(block->GetInstructions());
248 !instr_it.Done();
249 instr_it.Advance()) {
250 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000251 if (current->IsSuspendCheck()) {
252 continue;
253 }
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000256 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000257 << " could not be inlined because " << current->DebugName()
258 << " can throw";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100259 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 return false;
261 }
262
263 if (current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000264 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 << " could not be inlined because " << current->DebugName()
266 << " needs an environment";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100267 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000268 return false;
269 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000270
271 if (!can_use_dex_cache && current->NeedsDexCache()) {
272 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
273 << " could not be inlined because " << current->DebugName()
274 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100275 // Do not flag the method as not-inlineable. A caller within the same
276 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000277 return false;
278 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000279 }
280 }
281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000282 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000283
Mark Mendell1152c922015-04-24 17:06:35 -0400284 if (callee_graph->HasBoundsChecks()) {
285 graph_->SetHasBoundsChecks(true);
Mingyao Yange4335eb2015-03-02 15:14:13 -0800286 }
287
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000288 return true;
289}
290
291} // namespace art