blob: bd9267c4db3e33a8a519e550be4d0799c58882ed [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) {
89 VLOG(compiler) << "Did not inline "
90 << PrettyMethod(method_index, outer_dex_file)
91 << " because it is in a different dex file";
92 return false;
93 }
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
121 DexCompilationUnit dex_compilation_unit(
122 nullptr,
123 outer_compilation_unit_.GetClassLoader(),
124 outer_compilation_unit_.GetClassLinker(),
125 outer_dex_file,
126 code_item,
127 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
128 method_index,
129 resolved_method->GetAccessFlags(),
130 nullptr);
131
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000132 HGraph* callee_graph = new (graph_->GetArena()) HGraph(
133 graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId());
David Brazdil5e8b1372015-01-23 14:39:08 +0000134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000135 OptimizingCompilerStats inline_stats;
David Brazdil5e8b1372015-01-23 14:39:08 +0000136 HGraphBuilder builder(callee_graph,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000137 &dex_compilation_unit,
138 &outer_compilation_unit_,
139 &outer_dex_file,
140 compiler_driver_,
141 &inline_stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000142
David Brazdil5e8b1372015-01-23 14:39:08 +0000143 if (!builder.BuildGraph(*code_item)) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000144 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
145 << " could not be built, so cannot be inlined";
146 return false;
147 }
148
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000149 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
150 compiler_driver_->GetInstructionSet())) {
151 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
152 << " cannot be inlined because of the register allocator";
153 return false;
154 }
155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000156 if (!callee_graph->TryBuildingSsa()) {
157 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
158 << " could not be transformed to SSA";
159 return false;
160 }
161
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000162 // Run simple optimizations on the graph.
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000163 HDeadCodeElimination dce(callee_graph);
164 HConstantFolding fold(callee_graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000165 InstructionSimplifier simplify(callee_graph, stats_);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000166
167 HOptimization* optimizations[] = {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000168 &dce,
169 &fold,
170 &simplify,
171 };
172
173 for (size_t i = 0; i < arraysize(optimizations); ++i) {
174 HOptimization* optimization = optimizations[i];
175 optimization->Run();
176 }
177
178 if (depth_ + 1 < kDepthLimit) {
179 HInliner inliner(
Calin Juravleacf735c2015-02-12 15:25:22 +0000180 callee_graph, outer_compilation_unit_, compiler_driver_, stats_, depth_ + 1);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000181 inliner.Run();
182 }
183
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000184 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000185 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 for (; !it.Done(); it.Advance()) {
187 HBasicBlock* block = it.Current();
188 if (block->IsLoopHeader()) {
189 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
190 << " could not be inlined because it contains a loop";
191 return false;
192 }
193
194 for (HInstructionIterator instr_it(block->GetInstructions());
195 !instr_it.Done();
196 instr_it.Advance()) {
197 HInstruction* current = instr_it.Current();
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000198 if (current->IsSuspendCheck()) {
199 continue;
200 }
201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000202 if (current->CanThrow()) {
203 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
204 << " could not be inlined because " << current->DebugName()
205 << " can throw";
206 return false;
207 }
208
209 if (current->NeedsEnvironment()) {
210 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
211 << " could not be inlined because " << current->DebugName()
212 << " needs an environment";
213 return false;
214 }
215 }
216 }
217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000219
Mingyao Yange4335eb2015-03-02 15:14:13 -0800220 if (callee_graph->HasArrayAccesses()) {
221 graph_->SetHasArrayAccesses(true);
222 }
223
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000224 // Now that we have inlined the callee, we need to update the next
225 // instruction id of the caller, so that new instructions added
226 // after optimizations get a unique id.
227 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000228 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, outer_dex_file);
Calin Juravleacf735c2015-02-12 15:25:22 +0000229 MaybeRecordStat(kInlinedInvoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000230 return true;
231}
232
233} // namespace art