blob: 256e85b6ce5d222d33c72e46c03d60fef99c4af5 [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"
34
35namespace art {
36
37static constexpr int kMaxInlineCodeUnits = 100;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000038static constexpr int kDepthLimit = 5;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039
40void HInliner::Run() {
Nicolas Geoffraye50b8d22015-03-13 08:57:42 +000041 if (graph_->IsDebuggable()) {
42 // For simplicity, we currently never inline when the graph is debuggable. This avoids
43 // doing some logic in the runtime to discover if a method could have been inlined.
44 return;
45 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000046 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
47 for (size_t i = 0; i < blocks.Size(); ++i) {
48 HBasicBlock* block = blocks.Get(i);
49 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
50 HInstruction* next = instruction->GetNext();
51 HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect();
52 if (call != nullptr) {
53 if (!TryInline(call, call->GetDexMethodIndex(), call->GetInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054 if (kIsDebugBuild) {
55 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000056 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057 bool should_inline = callee_name.find("$inline$") != std::string::npos;
58 CHECK(!should_inline) << "Could not inline " << callee_name;
59 }
60 }
61 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000062 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000063 }
64 }
65}
66
67bool HInliner::TryInline(HInvoke* invoke_instruction,
68 uint32_t method_index,
69 InvokeType invoke_type) const {
70 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000071 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
72 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000073
74 StackHandleScope<3> hs(soa.Self());
75 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000076 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000077 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000078 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000079 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
80 compiler_driver_->ResolveMethod(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000081 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082
83 if (resolved_method.Get() == nullptr) {
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
114 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000115 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000116 << " is not inlined because its class could not be verified";
117 return false;
118 }
119
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000120 if (resolved_method->ShouldNotInline()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000121 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000122 << " was already flagged as non inlineable";
123 return false;
124 }
125
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000126 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000127 resolved_method->SetShouldNotInline();
128 return false;
129 }
130
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000131 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000132 MaybeRecordStat(kInlinedInvoke);
133 return true;
134}
135
136bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method,
137 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000138 uint32_t method_index,
139 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000140 ScopedObjectAccess soa(Thread::Current());
141 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000142 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000143
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 DexCompilationUnit dex_compilation_unit(
145 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000146 caller_compilation_unit_.GetClassLoader(),
147 caller_compilation_unit_.GetClassLinker(),
148 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 code_item,
150 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000151 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000152 resolved_method->GetAccessFlags(),
153 nullptr);
154
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000155 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
156 graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000159 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 &dex_compilation_unit,
161 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000162 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 compiler_driver_,
164 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165
David Brazdil5e8b1372015-01-23 14:39:08 +0000166 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000167 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168 << " could not be built, so cannot be inlined";
169 return false;
170 }
171
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000172 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
173 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000174 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000175 << " cannot be inlined because of the register allocator";
176 return false;
177 }
178
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000179 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000180 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 << " could not be transformed to SSA";
182 return false;
183 }
184
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000185 // Run simple optimizations on the graph.
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000186 HDeadCodeElimination dce(callee_graph);
187 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000188 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000189
190 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000191 &dce,
192 &fold,
193 &simplify,
194 };
195
196 for (size_t i = 0; i < arraysize(optimizations); ++i) {
197 HOptimization* optimization = optimizations[i];
198 optimization->Run();
199 }
200
201 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000202 HInliner inliner(callee_graph,
203 outer_compilation_unit_,
204 dex_compilation_unit,
205 compiler_driver_,
206 stats_,
207 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000208 inliner.Run();
209 }
210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000211 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000212 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 for (; !it.Done(); it.Advance()) {
214 HBasicBlock* block = it.Current();
215 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000216 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000217 << " could not be inlined because it contains a loop";
218 return false;
219 }
220
221 for (HInstructionIterator instr_it(block->GetInstructions());
222 !instr_it.Done();
223 instr_it.Advance()) {
224 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000225 if (current->IsSuspendCheck()) {
226 continue;
227 }
228
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000230 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 << " could not be inlined because " << current->DebugName()
232 << " can throw";
233 return false;
234 }
235
236 if (current->NeedsEnvironment()) {
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 " << current->DebugName()
239 << " needs an environment";
240 return false;
241 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000242
243 if (!can_use_dex_cache && current->NeedsDexCache()) {
244 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
245 << " could not be inlined because " << current->DebugName()
246 << " it is in a different dex file and requires access to the dex cache";
247 return false;
248 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000249 }
250 }
251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000253
Mingyao Yange4335eb2015-03-02 15:14:13 -0800254 if (callee_graph->HasArrayAccesses()) {
255 graph_->SetHasArrayAccesses(true);
256 }
257
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000258 // Now that we have inlined the callee, we need to update the next
259 // instruction id of the caller, so that new instructions added
260 // after optimizations get a unique id.
261 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 return true;
263}
264
265} // namespace art