blob: 65543942c772d9db19e0a0fde71e8fb4a8e16db9 [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"
Mathieu Chartierc7853442015-03-27 14:35:38 -070018
19#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080022#include "dex_file-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000023#include "dex_instruction.h"
24#include "dex_instruction-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000025#include "mirror/class-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000026#include "mirror/dex_cache-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000027#include "verifier/method_verifier-inl.h"
28
29/*
30 * NOTE: This code is part of the quick compiler. It lives in the runtime
31 * only to allow the debugger to check whether a method has been inlined.
32 */
33
34namespace art {
35
Andreas Gampe575e78c2014-11-03 23:41:03 -080036static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
37static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
38static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
39 "iget_object type");
40static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
41 "iget_boolean type");
42static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
43static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
44static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
45static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
46static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
47static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
48 "iput_object type");
49static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
50 "iput_boolean type");
51static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
52static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
53static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
54static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
55 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
56static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
57 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
58static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
59 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
60static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
61 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
62static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
63 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
64static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
65 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
66static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
67 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +000068
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010069// This is used by compiler and debugger. We look into the dex cache for resolved methods and
70// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
71// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
72// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +000073bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
74 InlineMethod* method) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010075 DCHECK(verifier != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 if (!Runtime::Current()->UseJit()) {
77 DCHECK_EQ(verifier->CanLoadClasses(), method != nullptr);
78 }
Vladimir Markoe3e02602014-03-12 15:42:41 +000079 // We currently support only plain return or 2-instruction methods.
80
81 const DexFile::CodeItem* code_item = verifier->CodeItem();
82 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
83 const Instruction* instruction = Instruction::At(code_item->insns_);
84 Instruction::Code opcode = instruction->Opcode();
85
86 switch (opcode) {
87 case Instruction::RETURN_VOID:
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010088 if (method != nullptr) {
89 method->opcode = kInlineOpNop;
90 method->flags = kInlineSpecial;
91 method->d.data = 0u;
92 }
Vladimir Markoe3e02602014-03-12 15:42:41 +000093 return true;
94 case Instruction::RETURN:
95 case Instruction::RETURN_OBJECT:
96 case Instruction::RETURN_WIDE:
97 return AnalyseReturnMethod(code_item, method);
98 case Instruction::CONST:
99 case Instruction::CONST_4:
100 case Instruction::CONST_16:
101 case Instruction::CONST_HIGH16:
102 // TODO: Support wide constants (RETURN_WIDE).
103 return AnalyseConstMethod(code_item, method);
104 case Instruction::IGET:
105 case Instruction::IGET_OBJECT:
106 case Instruction::IGET_BOOLEAN:
107 case Instruction::IGET_BYTE:
108 case Instruction::IGET_CHAR:
109 case Instruction::IGET_SHORT:
110 case Instruction::IGET_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800111 // TODO: Add handling for JIT.
112 // case Instruction::IGET_QUICK:
113 // case Instruction::IGET_WIDE_QUICK:
114 // case Instruction::IGET_OBJECT_QUICK:
Vladimir Markoe3e02602014-03-12 15:42:41 +0000115 return AnalyseIGetMethod(verifier, method);
116 case Instruction::IPUT:
117 case Instruction::IPUT_OBJECT:
118 case Instruction::IPUT_BOOLEAN:
119 case Instruction::IPUT_BYTE:
120 case Instruction::IPUT_CHAR:
121 case Instruction::IPUT_SHORT:
122 case Instruction::IPUT_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 // TODO: Add handling for JIT.
124 // case Instruction::IPUT_QUICK:
125 // case Instruction::IPUT_WIDE_QUICK:
126 // case Instruction::IPUT_OBJECT_QUICK:
Vladimir Markoe3e02602014-03-12 15:42:41 +0000127 return AnalyseIPutMethod(verifier, method);
128 default:
129 return false;
130 }
131}
132
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100133bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
134 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
135 const char* method_name = ref.dex_file->GetMethodName(method_id);
Vladimir Markod5f10052015-05-06 14:09:04 +0100136 // javac names synthetic accessors "access$nnn",
137 // jack names them "-getN", "-putN", "-wrapN".
138 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
139 strncmp(method_name, "-", strlen("-")) == 0;
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100140}
141
Vladimir Markoe3e02602014-03-12 15:42:41 +0000142bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
143 InlineMethod* result) {
144 const Instruction* return_instruction = Instruction::At(code_item->insns_);
145 Instruction::Code return_opcode = return_instruction->Opcode();
146 uint32_t reg = return_instruction->VRegA_11x();
147 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
148 DCHECK_GE(reg, arg_start);
149 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
150 code_item->registers_size_);
151
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100152 if (result != nullptr) {
153 result->opcode = kInlineOpReturnArg;
154 result->flags = kInlineSpecial;
155 InlineReturnArgData* data = &result->d.return_data;
156 data->arg = reg - arg_start;
157 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
158 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
159 data->reserved = 0u;
160 data->reserved2 = 0u;
161 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000162 return true;
163}
164
165bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
166 InlineMethod* result) {
167 const Instruction* instruction = Instruction::At(code_item->insns_);
168 const Instruction* return_instruction = instruction->Next();
169 Instruction::Code return_opcode = return_instruction->Opcode();
170 if (return_opcode != Instruction::RETURN &&
171 return_opcode != Instruction::RETURN_OBJECT) {
172 return false;
173 }
174
Ian Rogers29a26482014-05-02 15:27:29 -0700175 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000176 DCHECK_LT(return_reg, code_item->registers_size_);
177
Ian Rogers29a26482014-05-02 15:27:29 -0700178 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000179 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700180 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000181 }
Ian Rogers29a26482014-05-02 15:27:29 -0700182 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
183 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000184 return false; // Not returning the value set by const?
185 }
Ian Rogers29a26482014-05-02 15:27:29 -0700186 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000187 return false; // Returning non-null reference constant?
188 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100189 if (result != nullptr) {
190 result->opcode = kInlineOpNonWideConst;
191 result->flags = kInlineSpecial;
Ian Rogers29a26482014-05-02 15:27:29 -0700192 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100193 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000194 return true;
195}
196
197bool InlineMethodAnalyser::AnalyseIGetMethod(verifier::MethodVerifier* verifier,
198 InlineMethod* result) {
199 const DexFile::CodeItem* code_item = verifier->CodeItem();
200 const Instruction* instruction = Instruction::At(code_item->insns_);
201 Instruction::Code opcode = instruction->Opcode();
202 DCHECK(IsInstructionIGet(opcode));
203
204 const Instruction* return_instruction = instruction->Next();
205 Instruction::Code return_opcode = return_instruction->Opcode();
206 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
207 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
208 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
209 opcode != Instruction::IGET_OBJECT)) {
210 return false;
211 }
212
213 uint32_t return_reg = return_instruction->VRegA_11x();
214 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
215 code_item->registers_size_);
216
217 uint32_t dst_reg = instruction->VRegA_22c();
218 uint32_t object_reg = instruction->VRegB_22c();
219 uint32_t field_idx = instruction->VRegC_22c();
220 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
221 DCHECK_GE(object_reg, arg_start);
222 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100223 uint32_t object_arg = object_reg - arg_start;
224
Vladimir Markoe3e02602014-03-12 15:42:41 +0000225 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
226 if (dst_reg != return_reg) {
227 return false; // Not returning the value retrieved by IGET?
228 }
229
Vladimir Markoe1fced12014-04-04 14:52:53 +0100230 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100231 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
232 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
233 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
234 return false;
235 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000236 }
237
Vladimir Markoe1fced12014-04-04 14:52:53 +0100238 // InlineIGetIPutData::object_arg is only 4 bits wide.
239 static constexpr uint16_t kMaxObjectArg = 15u;
240 if (object_arg > kMaxObjectArg) {
241 return false;
242 }
243
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100244 if (result != nullptr) {
245 InlineIGetIPutData* data = &result->d.ifield_data;
246 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
247 return false;
248 }
249 result->opcode = kInlineOpIGet;
250 result->flags = kInlineSpecial;
251 data->op_variant = IGetVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100252 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100253 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100254 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100255 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000256 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000257 return true;
258}
259
260bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
261 InlineMethod* result) {
262 const DexFile::CodeItem* code_item = verifier->CodeItem();
263 const Instruction* instruction = Instruction::At(code_item->insns_);
264 Instruction::Code opcode = instruction->Opcode();
265 DCHECK(IsInstructionIPut(opcode));
266
267 const Instruction* return_instruction = instruction->Next();
268 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100269 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
270 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000271 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100272 if (return_opcode != Instruction::RETURN &&
273 return_opcode != Instruction::RETURN_OBJECT &&
274 return_opcode != Instruction::RETURN_WIDE) {
275 return false;
276 }
277 // Returning an argument.
278 uint32_t return_reg = return_instruction->VRegA_11x();
279 DCHECK_GE(return_reg, arg_start);
280 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
281 code_item->registers_size_);
282 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000283 }
284
285 uint32_t src_reg = instruction->VRegA_22c();
286 uint32_t object_reg = instruction->VRegB_22c();
287 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000288 DCHECK_GE(object_reg, arg_start);
289 DCHECK_LT(object_reg, code_item->registers_size_);
290 DCHECK_GE(src_reg, arg_start);
291 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100292 uint32_t object_arg = object_reg - arg_start;
293 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000294
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100295 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
296 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
297 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
298 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
299 return false;
300 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000301 }
302
Vladimir Markoe1fced12014-04-04 14:52:53 +0100303 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
304 static constexpr uint16_t kMaxObjectArg = 15u;
305 static constexpr uint16_t kMaxSrcArg = 15u;
306 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
307 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
308 return false;
309 }
310
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100311 if (result != nullptr) {
312 InlineIGetIPutData* data = &result->d.ifield_data;
313 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
314 return false;
315 }
316 result->opcode = kInlineOpIPut;
317 result->flags = kInlineSpecial;
318 data->op_variant = IPutVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100319 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100320 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
321 data->src_arg = src_arg;
322 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000323 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000324 return true;
325}
326
327bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
328 verifier::MethodVerifier* verifier,
329 InlineIGetIPutData* result) {
330 mirror::DexCache* dex_cache = verifier->GetDexCache();
331 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 auto* cl = Runtime::Current()->GetClassLinker();
333 ArtMethod* method = dex_cache->GetResolvedMethod(method_idx, cl->GetImagePointerSize());
334 ArtField* field = cl->GetResolvedField(field_idx, dex_cache);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000335 if (method == nullptr || field == nullptr || field->IsStatic()) {
336 return false;
337 }
338 mirror::Class* method_class = method->GetDeclaringClass();
339 mirror::Class* field_class = field->GetDeclaringClass();
340 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
341 (is_put && field->IsFinal() && method_class != field_class)) {
342 return false;
343 }
344 DCHECK_GE(field->GetOffset().Int32Value(), 0);
345 result->field_idx = field_idx;
346 result->field_offset = field->GetOffset().Int32Value();
347 result->is_volatile = field->IsVolatile();
348 return true;
349}
350
351} // namespace art