blob: 8bd8dbab7351dcf4bc6011bae9d7c4c6771249fd [file] [log] [blame]
Vladimir Markoe3e02602014-03-12 15:42:41 +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 "inline_method_analyser.h"
18#include "dex_instruction.h"
19#include "dex_instruction-inl.h"
20#include "mirror/art_field.h"
21#include "mirror/art_field-inl.h"
22#include "mirror/art_method.h"
23#include "mirror/art_method-inl.h"
24#include "mirror/class.h"
25#include "mirror/class-inl.h"
26#include "mirror/dex_cache.h"
27#include "mirror/dex_cache-inl.h"
28#include "verifier/method_verifier.h"
29#include "verifier/method_verifier-inl.h"
30
31/*
32 * NOTE: This code is part of the quick compiler. It lives in the runtime
33 * only to allow the debugger to check whether a method has been inlined.
34 */
35
36namespace art {
37
38COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET),
39 check_iget_type);
40COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE),
41 check_iget_wide_type);
42COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
43 check_iget_object_type);
44COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
45 check_iget_boolean_type);
46COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE),
47 check_iget_byte_type);
48COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR),
49 check_iget_char_type);
50COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT),
51 check_iget_short_type);
52
53COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT),
54 check_iput_type);
55COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE),
56 check_iput_wide_type);
57COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
58 check_iput_object_type);
59COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
60 check_iput_boolean_type);
61COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE),
62 check_iput_byte_type);
63COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR),
64 check_iput_char_type);
65COMPILE_ASSERT(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT),
66 check_iput_short_type);
67
68COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
69 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), check_iget_iput_variant);
70COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
71 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), check_iget_iput_wide_variant);
72COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
73 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), check_iget_iput_object_variant);
74COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
75 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), check_iget_iput_boolean_variant);
76COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
77 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), check_iget_iput_byte_variant);
78COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
79 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), check_iget_iput_char_variant);
80COMPILE_ASSERT(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
81 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), check_iget_iput_short_variant);
82
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010083// This is used by compiler and debugger. We look into the dex cache for resolved methods and
84// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
85// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
86// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +000087bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
88 InlineMethod* method) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010089 DCHECK(verifier != nullptr);
90 DCHECK_EQ(Runtime::Current()->IsCompiler(), method != nullptr);
91 DCHECK_EQ(verifier->CanLoadClasses(), method != nullptr);
Vladimir Markoe3e02602014-03-12 15:42:41 +000092 // We currently support only plain return or 2-instruction methods.
93
94 const DexFile::CodeItem* code_item = verifier->CodeItem();
95 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
96 const Instruction* instruction = Instruction::At(code_item->insns_);
97 Instruction::Code opcode = instruction->Opcode();
98
99 switch (opcode) {
100 case Instruction::RETURN_VOID:
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100101 if (method != nullptr) {
102 method->opcode = kInlineOpNop;
103 method->flags = kInlineSpecial;
104 method->d.data = 0u;
105 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000106 return true;
107 case Instruction::RETURN:
108 case Instruction::RETURN_OBJECT:
109 case Instruction::RETURN_WIDE:
110 return AnalyseReturnMethod(code_item, method);
111 case Instruction::CONST:
112 case Instruction::CONST_4:
113 case Instruction::CONST_16:
114 case Instruction::CONST_HIGH16:
115 // TODO: Support wide constants (RETURN_WIDE).
116 return AnalyseConstMethod(code_item, method);
117 case Instruction::IGET:
118 case Instruction::IGET_OBJECT:
119 case Instruction::IGET_BOOLEAN:
120 case Instruction::IGET_BYTE:
121 case Instruction::IGET_CHAR:
122 case Instruction::IGET_SHORT:
123 case Instruction::IGET_WIDE:
124 return AnalyseIGetMethod(verifier, method);
125 case Instruction::IPUT:
126 case Instruction::IPUT_OBJECT:
127 case Instruction::IPUT_BOOLEAN:
128 case Instruction::IPUT_BYTE:
129 case Instruction::IPUT_CHAR:
130 case Instruction::IPUT_SHORT:
131 case Instruction::IPUT_WIDE:
132 return AnalyseIPutMethod(verifier, method);
133 default:
134 return false;
135 }
136}
137
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100138bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
139 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
140 const char* method_name = ref.dex_file->GetMethodName(method_id);
141 return strncmp(method_name, "access$", strlen("access$")) == 0;
142}
143
Vladimir Markoe3e02602014-03-12 15:42:41 +0000144bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
145 InlineMethod* result) {
146 const Instruction* return_instruction = Instruction::At(code_item->insns_);
147 Instruction::Code return_opcode = return_instruction->Opcode();
148 uint32_t reg = return_instruction->VRegA_11x();
149 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
150 DCHECK_GE(reg, arg_start);
151 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
152 code_item->registers_size_);
153
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100154 if (result != nullptr) {
155 result->opcode = kInlineOpReturnArg;
156 result->flags = kInlineSpecial;
157 InlineReturnArgData* data = &result->d.return_data;
158 data->arg = reg - arg_start;
159 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
160 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
161 data->reserved = 0u;
162 data->reserved2 = 0u;
163 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000164 return true;
165}
166
167bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
168 InlineMethod* result) {
169 const Instruction* instruction = Instruction::At(code_item->insns_);
170 const Instruction* return_instruction = instruction->Next();
171 Instruction::Code return_opcode = return_instruction->Opcode();
172 if (return_opcode != Instruction::RETURN &&
173 return_opcode != Instruction::RETURN_OBJECT) {
174 return false;
175 }
176
177 uint32_t return_reg = return_instruction->VRegA_11x();
178 DCHECK_LT(return_reg, code_item->registers_size_);
179
180 uint32_t vA, vB, dummy;
181 uint64_t dummy_wide;
182 instruction->Decode(vA, vB, dummy_wide, dummy, nullptr);
183 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
184 vB <<= 16;
185 }
186 DCHECK_LT(vA, code_item->registers_size_);
187 if (vA != return_reg) {
188 return false; // Not returning the value set by const?
189 }
190 if (return_opcode == Instruction::RETURN_OBJECT && vB != 0) {
191 return false; // Returning non-null reference constant?
192 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100193 if (result != nullptr) {
194 result->opcode = kInlineOpNonWideConst;
195 result->flags = kInlineSpecial;
196 result->d.data = static_cast<uint64_t>(vB);
197 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000198 return true;
199}
200
201bool InlineMethodAnalyser::AnalyseIGetMethod(verifier::MethodVerifier* verifier,
202 InlineMethod* result) {
203 const DexFile::CodeItem* code_item = verifier->CodeItem();
204 const Instruction* instruction = Instruction::At(code_item->insns_);
205 Instruction::Code opcode = instruction->Opcode();
206 DCHECK(IsInstructionIGet(opcode));
207
208 const Instruction* return_instruction = instruction->Next();
209 Instruction::Code return_opcode = return_instruction->Opcode();
210 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
211 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
212 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
213 opcode != Instruction::IGET_OBJECT)) {
214 return false;
215 }
216
217 uint32_t return_reg = return_instruction->VRegA_11x();
218 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
219 code_item->registers_size_);
220
221 uint32_t dst_reg = instruction->VRegA_22c();
222 uint32_t object_reg = instruction->VRegB_22c();
223 uint32_t field_idx = instruction->VRegC_22c();
224 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
225 DCHECK_GE(object_reg, arg_start);
226 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100227 uint32_t object_arg = object_reg - arg_start;
228
Vladimir Markoe3e02602014-03-12 15:42:41 +0000229 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
230 if (dst_reg != return_reg) {
231 return false; // Not returning the value retrieved by IGET?
232 }
233
Vladimir Markoe1fced12014-04-04 14:52:53 +0100234 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100235 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
236 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
237 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
238 return false;
239 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000240 }
241
Vladimir Markoe1fced12014-04-04 14:52:53 +0100242 // InlineIGetIPutData::object_arg is only 4 bits wide.
243 static constexpr uint16_t kMaxObjectArg = 15u;
244 if (object_arg > kMaxObjectArg) {
245 return false;
246 }
247
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100248 if (result != nullptr) {
249 InlineIGetIPutData* data = &result->d.ifield_data;
250 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
251 return false;
252 }
253 result->opcode = kInlineOpIGet;
254 result->flags = kInlineSpecial;
255 data->op_variant = IGetVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100256 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100257 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100258 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100259 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000260 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000261 return true;
262}
263
264bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
265 InlineMethod* result) {
266 const DexFile::CodeItem* code_item = verifier->CodeItem();
267 const Instruction* instruction = Instruction::At(code_item->insns_);
268 Instruction::Code opcode = instruction->Opcode();
269 DCHECK(IsInstructionIPut(opcode));
270
271 const Instruction* return_instruction = instruction->Next();
272 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100273 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
274 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000275 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100276 if (return_opcode != Instruction::RETURN &&
277 return_opcode != Instruction::RETURN_OBJECT &&
278 return_opcode != Instruction::RETURN_WIDE) {
279 return false;
280 }
281 // Returning an argument.
282 uint32_t return_reg = return_instruction->VRegA_11x();
283 DCHECK_GE(return_reg, arg_start);
284 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
285 code_item->registers_size_);
286 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000287 }
288
289 uint32_t src_reg = instruction->VRegA_22c();
290 uint32_t object_reg = instruction->VRegB_22c();
291 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000292 DCHECK_GE(object_reg, arg_start);
293 DCHECK_LT(object_reg, code_item->registers_size_);
294 DCHECK_GE(src_reg, arg_start);
295 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100296 uint32_t object_arg = object_reg - arg_start;
297 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000298
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100299 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
300 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
301 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
302 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
303 return false;
304 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000305 }
306
Vladimir Markoe1fced12014-04-04 14:52:53 +0100307 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
308 static constexpr uint16_t kMaxObjectArg = 15u;
309 static constexpr uint16_t kMaxSrcArg = 15u;
310 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
311 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
312 return false;
313 }
314
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100315 if (result != nullptr) {
316 InlineIGetIPutData* data = &result->d.ifield_data;
317 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
318 return false;
319 }
320 result->opcode = kInlineOpIPut;
321 result->flags = kInlineSpecial;
322 data->op_variant = IPutVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100323 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100324 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
325 data->src_arg = src_arg;
326 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000327 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000328 return true;
329}
330
331bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
332 verifier::MethodVerifier* verifier,
333 InlineIGetIPutData* result) {
334 mirror::DexCache* dex_cache = verifier->GetDexCache();
335 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
336 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
337 mirror::ArtField* field = dex_cache->GetResolvedField(field_idx);
338 if (method == nullptr || field == nullptr || field->IsStatic()) {
339 return false;
340 }
341 mirror::Class* method_class = method->GetDeclaringClass();
342 mirror::Class* field_class = field->GetDeclaringClass();
343 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
344 (is_put && field->IsFinal() && method_class != field_class)) {
345 return false;
346 }
347 DCHECK_GE(field->GetOffset().Int32Value(), 0);
348 result->field_idx = field_idx;
349 result->field_offset = field->GetOffset().Int32Value();
350 result->is_volatile = field->IsVolatile();
351 return true;
352}
353
354} // namespace art