blob: 4b990f1dddca3c4f0107ded4211f32b5fd3a9e2a [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) {
Nicolas Geoffray79041292015-03-26 10:05:54 +000053 // We use the original invoke type to ensure the resolution of the called method
54 // works properly.
55 if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000056 if (kIsDebugBuild) {
57 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000058 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059 bool should_inline = callee_name.find("$inline$") != std::string::npos;
60 CHECK(!should_inline) << "Could not inline " << callee_name;
61 }
62 }
63 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000064 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000065 }
66 }
67}
68
69bool HInliner::TryInline(HInvoke* invoke_instruction,
70 uint32_t method_index,
71 InvokeType invoke_type) const {
72 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000073 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
74 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000075
76 StackHandleScope<3> hs(soa.Self());
77 Handle<mirror::DexCache> dex_cache(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000078 hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000079 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000080 soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader())));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000081 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
82 compiler_driver_->ResolveMethod(
Nicolas Geoffray9437b782015-03-25 10:08:51 +000083 soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type)));
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000084
85 if (resolved_method.Get() == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000086 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000087 return false;
88 }
89
Nicolas Geoffray9437b782015-03-25 10:08:51 +000090 bool can_use_dex_cache = true;
91 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000092 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000093 can_use_dex_cache = false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000094 }
95
96 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
97
98 if (code_item == nullptr) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +000099 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000100 << " is not inlined because it is native";
101 return false;
102 }
103
104 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000105 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000106 << " is too big to inline";
107 return false;
108 }
109
110 if (code_item->tries_size_ != 0) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000111 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000112 << " is not inlined because of try block";
113 return false;
114 }
115
116 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000117 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000118 << " is not inlined because its class could not be verified";
119 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
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000128 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000129 resolved_method->SetShouldNotInline();
130 return false;
131 }
132
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000133 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file);
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000134 MaybeRecordStat(kInlinedInvoke);
135 return true;
136}
137
138bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method,
139 HInvoke* invoke_instruction,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000140 uint32_t method_index,
141 bool can_use_dex_cache) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000142 ScopedObjectAccess soa(Thread::Current());
143 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000144 const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile();
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 DexCompilationUnit dex_compilation_unit(
147 nullptr,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000148 caller_compilation_unit_.GetClassLoader(),
149 caller_compilation_unit_.GetClassLinker(),
150 *resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 code_item,
152 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000153 resolved_method->GetDexMethodIndex(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000154 resolved_method->GetAccessFlags(),
155 nullptr);
156
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000157 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
158 graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000159
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000161 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000162 &dex_compilation_unit,
163 &outer_compilation_unit_,
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000164 resolved_method->GetDexFile(),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000165 compiler_driver_,
166 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167
David Brazdil5e8b1372015-01-23 14:39:08 +0000168 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000169 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000170 << " could not be built, so cannot be inlined";
171 return false;
172 }
173
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000174 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
175 compiler_driver_->GetInstructionSet())) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000176 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000177 << " cannot be inlined because of the register allocator";
178 return false;
179 }
180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 if (!callee_graph->TryBuildingSsa()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000182 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 << " could not be transformed to SSA";
184 return false;
185 }
186
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000187 // Run simple optimizations on the graph.
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000188 HDeadCodeElimination dce(callee_graph);
189 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000190 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000191
192 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000193 &dce,
194 &fold,
195 &simplify,
196 };
197
198 for (size_t i = 0; i < arraysize(optimizations); ++i) {
199 HOptimization* optimization = optimizations[i];
200 optimization->Run();
201 }
202
203 if (depth_ + 1 < kDepthLimit) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000204 HInliner inliner(callee_graph,
205 outer_compilation_unit_,
206 dex_compilation_unit,
207 compiler_driver_,
208 stats_,
209 depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000210 inliner.Run();
211 }
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000214 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 for (; !it.Done(); it.Advance()) {
216 HBasicBlock* block = it.Current();
217 if (block->IsLoopHeader()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000218 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000219 << " could not be inlined because it contains a loop";
220 return false;
221 }
222
223 for (HInstructionIterator instr_it(block->GetInstructions());
224 !instr_it.Done();
225 instr_it.Advance()) {
226 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000227 if (current->IsSuspendCheck()) {
228 continue;
229 }
230
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000231 if (current->CanThrow()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000232 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000233 << " could not be inlined because " << current->DebugName()
234 << " can throw";
235 return false;
236 }
237
238 if (current->NeedsEnvironment()) {
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000239 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 << " could not be inlined because " << current->DebugName()
241 << " needs an environment";
242 return false;
243 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000244
245 if (!can_use_dex_cache && current->NeedsDexCache()) {
246 VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file)
247 << " could not be inlined because " << current->DebugName()
248 << " it is in a different dex file and requires access to the dex cache";
249 return false;
250 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000251 }
252 }
253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000254 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000255
Mingyao Yange4335eb2015-03-02 15:14:13 -0800256 if (callee_graph->HasArrayAccesses()) {
257 graph_->SetHasArrayAccesses(true);
258 }
259
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000260 // Now that we have inlined the callee, we need to update the next
261 // instruction id of the caller, so that new instructions added
262 // after optimizations get a unique id.
263 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000264 return true;
265}
266
267} // namespace art