blob: 513be7d808f6fb13e53b18b2e7c402837b1a914d [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;
38static constexpr int kMaxInlineNumberOfBlocks = 3;
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000039static constexpr int kDepthLimit = 5;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040
41void HInliner::Run() {
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000042 const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder();
43 for (size_t i = 0; i < blocks.Size(); ++i) {
44 HBasicBlock* block = blocks.Get(i);
45 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
46 HInstruction* next = instruction->GetNext();
47 HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect();
48 if (call != nullptr) {
49 if (!TryInline(call, call->GetDexMethodIndex(), call->GetInvokeType())) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000050 if (kIsDebugBuild) {
51 std::string callee_name =
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000052 PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053 bool should_inline = callee_name.find("$inline$") != std::string::npos;
54 CHECK(!should_inline) << "Could not inline " << callee_name;
55 }
56 }
57 }
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +000058 instruction = next;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059 }
60 }
61}
62
63bool HInliner::TryInline(HInvoke* invoke_instruction,
64 uint32_t method_index,
65 InvokeType invoke_type) const {
66 ScopedObjectAccess soa(Thread::Current());
67 const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile();
68 VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, outer_dex_file);
69
70 StackHandleScope<3> hs(soa.Self());
71 Handle<mirror::DexCache> dex_cache(
72 hs.NewHandle(outer_compilation_unit_.GetClassLinker()->FindDexCache(outer_dex_file)));
73 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
74 soa.Decode<mirror::ClassLoader*>(outer_compilation_unit_.GetClassLoader())));
75 Handle<mirror::ArtMethod> resolved_method(hs.NewHandle(
76 compiler_driver_->ResolveMethod(
77 soa, dex_cache, class_loader, &outer_compilation_unit_, method_index, invoke_type)));
78
79 if (resolved_method.Get() == nullptr) {
80 VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, outer_dex_file);
81 return false;
82 }
83
84 if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) {
85 VLOG(compiler) << "Did not inline "
86 << PrettyMethod(method_index, outer_dex_file)
87 << " because it is in a different dex file";
88 return false;
89 }
90
91 const DexFile::CodeItem* code_item = resolved_method->GetCodeItem();
92
93 if (code_item == nullptr) {
94 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
95 << " is not inlined because it is native";
96 return false;
97 }
98
99 if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) {
100 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
101 << " is too big to inline";
102 return false;
103 }
104
105 if (code_item->tries_size_ != 0) {
106 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
107 << " is not inlined because of try block";
108 return false;
109 }
110
111 if (!resolved_method->GetDeclaringClass()->IsVerified()) {
112 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
113 << " is not inlined because its class could not be verified";
114 return false;
115 }
116
117 DexCompilationUnit dex_compilation_unit(
118 nullptr,
119 outer_compilation_unit_.GetClassLoader(),
120 outer_compilation_unit_.GetClassLinker(),
121 outer_dex_file,
122 code_item,
123 resolved_method->GetDeclaringClass()->GetDexClassDefIndex(),
124 method_index,
125 resolved_method->GetAccessFlags(),
126 nullptr);
127
128 OptimizingCompilerStats inline_stats;
129 HGraphBuilder builder(graph_->GetArena(),
130 &dex_compilation_unit,
131 &outer_compilation_unit_,
132 &outer_dex_file,
133 compiler_driver_,
134 &inline_stats);
135 HGraph* callee_graph = builder.BuildGraph(*code_item, graph_->GetCurrentInstructionId());
136
137 if (callee_graph == nullptr) {
138 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
139 << " could not be built, so cannot be inlined";
140 return false;
141 }
142
143 if (callee_graph->GetBlocks().Size() > kMaxInlineNumberOfBlocks) {
144 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
145 << " has too many blocks to be inlined: "
146 << callee_graph->GetBlocks().Size();
147 return false;
148 }
149
Nicolas Geoffray259136f2014-12-17 23:21:58 +0000150 if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph,
151 compiler_driver_->GetInstructionSet())) {
152 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
153 << " cannot be inlined because of the register allocator";
154 return false;
155 }
156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000157 if (!callee_graph->TryBuildingSsa()) {
158 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
159 << " could not be transformed to SSA";
160 return false;
161 }
162
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000163 // Run simple optimizations on the graph.
164 SsaRedundantPhiElimination redundant_phi(callee_graph);
165 SsaDeadPhiElimination dead_phi(callee_graph);
166 HDeadCodeElimination dce(callee_graph);
167 HConstantFolding fold(callee_graph);
168 InstructionSimplifier simplify(callee_graph);
169
170 HOptimization* optimizations[] = {
171 &redundant_phi,
172 &dead_phi,
173 &dce,
174 &fold,
175 &simplify,
176 };
177
178 for (size_t i = 0; i < arraysize(optimizations); ++i) {
179 HOptimization* optimization = optimizations[i];
180 optimization->Run();
181 }
182
183 if (depth_ + 1 < kDepthLimit) {
184 HInliner inliner(
185 callee_graph, outer_compilation_unit_, compiler_driver_, outer_stats_, depth_ + 1);
186 inliner.Run();
187 }
188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 HReversePostOrderIterator it(*callee_graph);
Nicolas Geoffrayef87c5d2015-01-30 12:41:14 +0000190 it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 for (; !it.Done(); it.Advance()) {
192 HBasicBlock* block = it.Current();
193 if (block->IsLoopHeader()) {
194 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
195 << " could not be inlined because it contains a loop";
196 return false;
197 }
198
199 for (HInstructionIterator instr_it(block->GetInstructions());
200 !instr_it.Done();
201 instr_it.Advance()) {
202 HInstruction* current = instr_it.Current();
203 if (current->CanThrow()) {
204 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
205 << " could not be inlined because " << current->DebugName()
206 << " can throw";
207 return false;
208 }
209
210 if (current->NeedsEnvironment()) {
211 VLOG(compiler) << "Method " << PrettyMethod(method_index, outer_dex_file)
212 << " could not be inlined because " << current->DebugName()
213 << " needs an environment";
214 return false;
215 }
216 }
217 }
218
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000219 callee_graph->InlineInto(graph_, invoke_instruction);
Nicolas Geoffray7c5367b2014-12-17 10:13:46 +0000220
221 // Now that we have inlined the callee, we need to update the next
222 // instruction id of the caller, so that new instructions added
223 // after optimizations get a unique id.
224 graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, outer_dex_file);
226 outer_stats_->RecordStat(kInlinedInvoke);
227 return true;
228}
229
230} // namespace art