blob: eeb16368f3ed3b0fb2be495999a8b1fe65559631 [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 Geoffray20d60dd2015-06-25 10:01:47 +010030#include "optimizing_compiler.h"
Nicolas Geoffray259136f2014-12-17 23:21:58 +000031#include "register_allocator.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "ssa_phi_elimination.h"
33#include "scoped_thread_state_change.h"
34#include "thread.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010035#include "dex/verified_method.h"
36#include "dex/verification_results.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000037
38namespace art {
39
40static constexpr int kMaxInlineCodeUnits = 100;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000041static constexpr int kDepthLimit = 5;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000042
43void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000044 if (graph_->IsDebuggable()) {
45 // For simplicity, we currently never inline when the graph is debuggable. This avoids
46 // doing some logic in the runtime to discover if a method could have been inlined.
47 return;
48 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000049 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
50 for (size_t i = 0; i < blocks.Size(); ++i) {
51 HBasicBlock* block = blocks.Get(i);
52 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
53 HInstruction* next = instruction->GetNext();
54 HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070055 // As long as the call is not intrinsified, it is worth trying to inline.
56 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000057 // We use the original invoke type to ensure the resolution of the called method
58 // works properly.
Nicolas Geoffray88593112015-06-22 14:15:07 +000059 if (!TryInline(call, call->GetDexMethodIndex())) {
Nicolas Geoffray20d60dd2015-06-25 10:01:47 +010060 if (kIsDebugBuild && IsCompilingWithCoreImage()) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000062 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000063 bool should_inline = callee_name.find("$inline$") != std::string::npos;
64 CHECK(!should_inline) << "Could not inline " << callee_name;
65 }
66 }
67 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000068 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000069 }
70 }
71}
72
Nicolas Geoffray88593112015-06-22 14:15:07 +000073bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000074 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000075 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
76 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000077
Nicolas Geoffray88593112015-06-22 14:15:07 +000078 ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker();
79 // We can query the dex cache directly. The verifier has populated it already.
80 ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod(
81 method_index, class_linker->GetImagePointerSize());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070083 if (resolved_method == nullptr) {
Nicolas Geoffray88593112015-06-22 14:15:07 +000084 // Method cannot be resolved if it is in another dex file we do not have access to.
Nicolas Geoffray9437b782015-03-25 10:08:51 +000085 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000086 return false;
87 }
88
Nicolas Geoffray9437b782015-03-25 10:08:51 +000089 bool can_use_dex_cache = true;
90 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000092 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000093 }
94
95 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
96
97 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000098 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000099 << " is not inlined because it is native";
100 return false;
101 }
102
103 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000104 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000105 << " is too big to inline";
106 return false;
107 }
108
109 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000110 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000111 << " is not inlined because of try block";
112 return false;
113 }
114
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100115 uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex();
116 if (!compiler_driver_->IsMethodVerifiedWithoutFailures(
117 resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000118 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Calin Juravlef1c6d9e2015-04-13 18:42:21 +0100119 << " couldn't be verified, so it cannot be inlined";
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000120 return false;
121 }
122
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000123 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000124 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000125 << " was already flagged as non inlineable";
126 return false;
127 }
128
Roland Levillain4c0eb422015-04-24 16:43:49 +0100129 if (invoke_instruction->IsInvokeStaticOrDirect() &&
130 invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) {
131 // Case of a static method that cannot be inlined because it implicitly
132 // requires an initialization check of its declaring class.
133 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
134 << " is not inlined because it is static and requires a clinit"
135 << " check that cannot be emitted due to Dex cache limitations";
136 return false;
137 }
138
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000139 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000140 return false;
141 }
142
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000143 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000144 MaybeRecordStat(kInlinedInvoke);
145 return true;
146}
147
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700148bool HInliner::TryBuildAndInline(ArtMethod* resolved_method,
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000149 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000150 uint32_t method_index,
151 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000152 ScopedObjectAccess soa(Thread::Current());
153 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000154 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000156 DexCompilationUnit dex_compilation_unit(
157 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000158 caller_compilation_unit_.GetClassLoader(),
159 caller_compilation_unit_.GetClassLinker(),
160 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000161 code_item,
162 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000163 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000164 resolved_method->GetAccessFlags(),
165 nullptr);
166
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000167 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100168 graph_->GetArena(),
169 caller_dex_file,
170 method_index,
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700171 compiler_driver_->GetInstructionSet(),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100172 graph_->IsDebuggable(),
173 graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000174
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000176 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 &dex_compilation_unit,
178 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000179 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 compiler_driver_,
181 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182
David Brazdil5e8b1372015-01-23 14:39:08 +0000183 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000184 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000185 << " could not be built, so cannot be inlined";
Nicolas Geoffray26531492015-05-27 12:53:36 +0100186 // There could be multiple reasons why the graph could not be built, including
187 // unaccessible methods/fields due to using a different dex cache. We do not mark
188 // the method as non-inlineable so that other callers can still try to inline it.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 return false;
190 }
191
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000192 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
193 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000194 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000195 << " cannot be inlined because of the register allocator";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100196 resolved_method->SetShouldNotInline();
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000197 return false;
198 }
199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000201 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 << " could not be transformed to SSA";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100203 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000204 return false;
205 }
206
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000207 // Run simple optimizations on the graph.
Calin Juravle7a9c8852015-04-21 14:07:50 +0100208 HDeadCodeElimination dce(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000209 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000210 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000211
212 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000213 &dce,
214 &fold,
215 &simplify,
216 };
217
218 for (size_t i = 0; i < arraysize(optimizations); ++i) {
219 HOptimization* optimization = optimizations[i];
220 optimization->Run();
221 }
222
223 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000224 HInliner inliner(callee_graph,
225 outer_compilation_unit_,
226 dex_compilation_unit,
227 compiler_driver_,
228 stats_,
229 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000230 inliner.Run();
231 }
232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000233 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000234 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 for (; !it.Done(); it.Advance()) {
236 HBasicBlock* block = it.Current();
237 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000238 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000239 << " could not be inlined because it contains a loop";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100240 resolved_method->SetShouldNotInline();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 return false;
242 }
243
244 for (HInstructionIterator instr_it(block->GetInstructions());
245 !instr_it.Done();
246 instr_it.Advance()) {
247 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000248 if (current->IsSuspendCheck()) {
249 continue;
250 }
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000253 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000254 << " could not be inlined because " << current->DebugName()
255 << " can throw";
256 return false;
257 }
258
259 if (current->NeedsEnvironment()) {
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 " << current->DebugName()
262 << " needs an environment";
263 return false;
264 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000265
266 if (!can_use_dex_cache && current->NeedsDexCache()) {
267 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
268 << " could not be inlined because " << current->DebugName()
269 << " it is in a different dex file and requires access to the dex cache";
Nicolas Geoffray4fa04a62015-05-26 14:35:06 +0100270 // Do not flag the method as not-inlineable. A caller within the same
271 // dex file could still successfully inline it.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000272 return false;
273 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000274 }
275 }
276
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000277 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000278
Mark Mendell1152c922015-04-24 17:06:35 -0400279 if (callee_graph->HasBoundsChecks()) {
280 graph_->SetHasBoundsChecks(true);
Mingyao Yange4335eb2015-03-02 15:14:13 -0800281 }
282
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000283 return true;
284}
285
286} // namespace art