blob: b984ef7a42a408aa0f7a561c13a5650598ab673f [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.
Nicolas Geoffray88593112015-06-22 14:15:07 +000058 if (!TryInline(call, call->GetDexMethodIndex())) {
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
Nicolas Geoffray88593112015-06-22 14:15:07 +000072bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000073 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000074 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
75 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000076
Nicolas Geoffray88593112015-06-22 14:15:07 +000077 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
78 // We can query the dex cache directly. The verifier has populated it already.
79 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
80 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000081
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070082 if (resolved_method == nullptr) {
Nicolas Geoffray88593112015-06-22 14:15:07 +000083 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +000084 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000085 return false;
86 }
87
Nicolas Geoffray9437b782015-03-25 10:08:51 +000088 bool can_use_dex_cache = true;
89 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000090 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000091 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092 }
93
94 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
95
96 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000097 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000098 << " is not inlined because it is native";
99 return false;
100 }
101
102 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000103 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000104 << " is too big to inline";
105 return false;
106 }
107
108 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000109 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000110 << " is not inlined because of try block";
111 return false;
112 }
113
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100114 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
115 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
116 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000117 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100118 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000119 return false;
120 }
121
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000122 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000123 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000124 << " was already flagged as non inlineable";
125 return false;
126 }
127
Roland Levillain4c0eb422015-04-24 16:43:49 +0100128 if (invoke_instruction->IsInvokeStaticOrDirect() &&
129 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
130 // Case of a static method that cannot be inlined because it implicitly
131 // requires an initialization check of its declaring class.
132 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
133 << " is not inlined because it is static and requires a clinit"
134 << " check that cannot be emitted due to Dex cache limitations";
135 return false;
136 }
137
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000138 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000139 return false;
140 }
141
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000142 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000143 MaybeRecordStat(kInlinedInvoke);
144 return true;
145}
146
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700147bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000148 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000149 uint32_t method_index,
150 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000151 ScopedObjectAccess soa(Thread::Current());
152 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000153 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 DexCompilationUnit dex_compilation_unit(
156 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000157 caller_compilation_unit_.GetClassLoader(),
158 caller_compilation_unit_.GetClassLinker(),
159 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 code_item,
161 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000162 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 resolved_method->GetAccessFlags(),
164 nullptr);
165
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000166 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100167 graph_->GetArena(),
168 caller_dex_file,
169 method_index,
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700170 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100171 graph_->IsDebuggable(),
172 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000175 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 &dex_compilation_unit,
177 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000178 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000179 compiler_driver_,
180 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181
David Brazdil5e8b1372015-01-23 14:39:08 +0000182 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000183 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 << " could not be built, so cannot be inlined";
Nicolas Geoffray26531492015-05-27 12:53:36 +0100185 // There could be multiple reasons why the graph could not be built, including
186 // unaccessible methods/fields due to using a different dex cache. We do not mark
187 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000188 return false;
189 }
190
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000191 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
192 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000193 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000194 << " cannot be inlined because of the register allocator";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100195 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000196 return false;
197 }
198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000199 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000200 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000201 << " could not be transformed to SSA";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100202 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203 return false;
204 }
205
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000206 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100207 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000208 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000209 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000210
211 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000212 &dce,
213 &fold,
214 &simplify,
215 };
216
217 for (size_t i = 0; i < arraysize(optimizations); ++i) {
218 HOptimization* optimization = optimizations[i];
219 optimization->Run();
220 }
221
222 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000223 HInliner inliner(callee_graph,
224 outer_compilation_unit_,
225 dex_compilation_unit,
226 compiler_driver_,
227 stats_,
228 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000229 inliner.Run();
230 }
231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000233 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 for (; !it.Done(); it.Advance()) {
235 HBasicBlock* block = it.Current();
236 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000237 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 << " could not be inlined because it contains a loop";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100239 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 return false;
241 }
242
243 for (HInstructionIterator instr_it(block->GetInstructions());
244 !instr_it.Done();
245 instr_it.Advance()) {
246 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000247 if (current->IsSuspendCheck()) {
248 continue;
249 }
250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000251 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000252 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000253 << " could not be inlined because " << current->DebugName()
254 << " can throw";
255 return false;
256 }
257
258 if (current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000259 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000260 << " could not be inlined because " << current->DebugName()
261 << " needs an environment";
262 return false;
263 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000264
265 if (!can_use_dex_cache && current->NeedsDexCache()) {
266 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
267 << " could not be inlined because " << current->DebugName()
268 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100269 // Do not flag the method as not-inlineable. A caller within the same
270 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000271 return false;
272 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000273 }
274 }
275
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000276 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000277
Mark Mendell1152c922015-04-24 17:06:35 -0400278 if (callee_graph->HasBoundsChecks()) {
279 graph_->SetHasBoundsChecks(true);
Mingyao Yange4335eb2015-03-02 15:14:13 -0800280 }
281
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000282 return true;
283}
284
285} // namespace art