blob: 968fe3e73c06bf14fe2bcacf205830fc957d48da [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());
71 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
72 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, outer_dex_file);
73
74 StackHandleScope<3> hs(soa.Self());
75 Handle<mirror::DexCache> dex_cache(
76 hs.NewHandle(outer_compilation_unit_.GetClassLinker()->FindDexCache(outer_dex_file)));
77 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
78 soa.Decode<mirror::ClassLoader*>(outer_compilation_unit_.GetClassLoader())));
79 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
80 compiler_driver_->ResolveMethod(
81 soa, dex_cache, class_loader, &outer_compilation_unit_, method_index, invoke_type)));
82
83 if (resolved_method.Get() == nullptr) {
84 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, outer_dex_file);
85 return false;
86 }
87
88 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +000089 VLOG(compiler) << "Did not inline "
90 << PrettyMethod(method_index, outer_dex_file)
91 << " because it is in a different dex file";
92 return 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) {
98 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
99 << " is not inlined because it is native";
100 return false;
101 }
102
103 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
104 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
105 << " is too big to inline";
106 return false;
107 }
108
109 if (code_item->tries_size_ != 0) {
110 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
111 << " is not inlined because of try block";
112 return false;
113 }
114
115 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
116 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
117 << " is not inlined because its class could not be verified";
118 return false;
119 }
120
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000121 if (resolved_method->ShouldNotInline()) {
122 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
123 << " was already flagged as non inlineable";
124 return false;
125 }
126
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000127 if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index)) {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000128 resolved_method->SetShouldNotInline();
129 return false;
130 }
131
132 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, outer_dex_file);
133 MaybeRecordStat(kInlinedInvoke);
134 return true;
135}
136
137bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method,
138 HInvoke* invoke_instruction,
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000139 uint32_t method_index) const {
Nicolas Geoffrayc0365b12015-03-18 18:31:52 +0000140 ScopedObjectAccess soa(Thread::Current());
141 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
142 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
143
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 DexCompilationUnit dex_compilation_unit(
145 nullptr,
146 outer_compilation_unit_.GetClassLoader(),
147 outer_compilation_unit_.GetClassLinker(),
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000148 outer_dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 code_item,
150 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000151 method_index,
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 Geoffray6a816cf2015-03-24 16:17:56 +0000162 &outer_dex_file,
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 Geoffraye53798a2014-12-01 10:31:54 +0000167 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
168 << " 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())) {
174 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
175 << " 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()) {
180 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
181 << " 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) {
202 HInliner inliner(
Nicolas Geoffray6a816cf2015-03-24 16:17:56 +0000203 callee_graph, outer_compilation_unit_, compiler_driver_, stats_, depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000204 inliner.Run();
205 }
206
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000207 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000208 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000209 for (; !it.Done(); it.Advance()) {
210 HBasicBlock* block = it.Current();
211 if (block->IsLoopHeader()) {
212 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
213 << " could not be inlined because it contains a loop";
214 return false;
215 }
216
217 for (HInstructionIterator instr_it(block->GetInstructions());
218 !instr_it.Done();
219 instr_it.Advance()) {
220 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000221 if (current->IsSuspendCheck()) {
222 continue;
223 }
224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 if (current->CanThrow()) {
226 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
227 << " could not be inlined because " << current->DebugName()
228 << " can throw";
229 return false;
230 }
231
232 if (current->NeedsEnvironment()) {
233 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
234 << " could not be inlined because " << current->DebugName()
235 << " needs an environment";
236 return false;
237 }
238 }
239 }
240
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000241 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000242
Mingyao Yange4335eb2015-03-02 15:14:13 -0800243 if (callee_graph->HasArrayAccesses()) {
244 graph_->SetHasArrayAccesses(true);
245 }
246
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000247 // Now that we have inlined the callee, we need to update the next
248 // instruction id of the caller, so that new instructions added
249 // after optimizations get a unique id.
250 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000251 return true;
252}
253
254} // namespace art