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