blob: 2c17a678677b2f144bf5e26915e384f6a18dc883 [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();
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -070052 // As long as the call is not intrinsified, it is worth trying to inline.
53 if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000054 // We use the original invoke type to ensure the resolution of the called method
55 // works properly.
56 if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057 if (kIsDebugBuild) {
58 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000059 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060 bool should_inline = callee_name.find("$inline$") != std::string::npos;
61 CHECK(!should_inline) << "Could not inline " << callee_name;
62 }
63 }
64 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000065 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000066 }
67 }
68}
69
70bool HInliner::TryInline(HInvoke* invoke_instruction,
71 uint32_t method_index,
72 InvokeType invoke_type) const {
73 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
77 StackHandleScope<3> hs(soa.Self());
78 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000079 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000081 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000082 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
83 compiler_driver_->ResolveMethod(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000084 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000085
86 if (resolved_method.Get() == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000087 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000088 return false;
89 }
90
Nicolas Geoffray9437b782015-03-25 10:08:51 +000091 bool can_use_dex_cache = true;
92 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000093 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000094 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000095 }
96
97 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
98
99 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000100 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 << " is not inlined because it is native";
102 return false;
103 }
104
105 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000106 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000107 << " is too big to inline";
108 return false;
109 }
110
111 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000112 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000113 << " is not inlined because of try block";
114 return false;
115 }
116
117 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000118 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000119 << " is not inlined because its class could not be verified";
120 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
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000129 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000130 resolved_method->SetShouldNotInline();
131 return false;
132 }
133
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000134 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000135 MaybeRecordStat(kInlinedInvoke);
136 return true;
137}
138
139bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method,
140 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000141 uint32_t method_index,
142 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000143 ScopedObjectAccess soa(Thread::Current());
144 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000145 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000146
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000147 DexCompilationUnit dex_compilation_unit(
148 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000149 caller_compilation_unit_.GetClassLoader(),
150 caller_compilation_unit_.GetClassLinker(),
151 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000152 code_item,
153 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000154 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 resolved_method->GetAccessFlags(),
156 nullptr);
157
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000158 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
159 graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000160
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000161 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000162 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000163 &dex_compilation_unit,
164 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000165 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000166 compiler_driver_,
167 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000168
David Brazdil5e8b1372015-01-23 14:39:08 +0000169 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000170 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 << " could not be built, so cannot be inlined";
172 return false;
173 }
174
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000175 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
176 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000177 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000178 << " cannot be inlined because of the register allocator";
179 return false;
180 }
181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 if (!callee_graph->TryBuildingSsa()) {
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 transformed to SSA";
185 return false;
186 }
187
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000188 // Run simple optimizations on the graph.
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000189 HDeadCodeElimination dce(callee_graph);
190 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000191 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000192
193 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000194 &dce,
195 &fold,
196 &simplify,
197 };
198
199 for (size_t i = 0; i < arraysize(optimizations); ++i) {
200 HOptimization* optimization = optimizations[i];
201 optimization->Run();
202 }
203
204 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000205 HInliner inliner(callee_graph,
206 outer_compilation_unit_,
207 dex_compilation_unit,
208 compiler_driver_,
209 stats_,
210 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000211 inliner.Run();
212 }
213
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000214 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000215 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000216 for (; !it.Done(); it.Advance()) {
217 HBasicBlock* block = it.Current();
218 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000219 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000220 << " could not be inlined because it contains a loop";
221 return false;
222 }
223
224 for (HInstructionIterator instr_it(block->GetInstructions());
225 !instr_it.Done();
226 instr_it.Advance()) {
227 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000228 if (current->IsSuspendCheck()) {
229 continue;
230 }
231
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000233 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000234 << " could not be inlined because " << current->DebugName()
235 << " can throw";
236 return false;
237 }
238
239 if (current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000240 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 << " could not be inlined because " << current->DebugName()
242 << " needs an environment";
243 return false;
244 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000245
246 if (!can_use_dex_cache && current->NeedsDexCache()) {
247 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
248 << " could not be inlined because " << current->DebugName()
249 << " it is in a different dex file and requires access to the dex cache";
250 return false;
251 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000252 }
253 }
254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000255 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000256
Mingyao Yange4335eb2015-03-02 15:14:13 -0800257 if (callee_graph->HasArrayAccesses()) {
258 graph_->SetHasArrayAccesses(true);
259 }
260
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000261 // Now that we have inlined the callee, we need to update the next
262 // instruction id of the caller, so that new instructions added
263 // after optimizations get a unique id.
264 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265 return true;
266}
267
268} // namespace art