blob: d65b2d5241b64fec6a73529f8ee86b63fbf3038a [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"
Elliott Hughes956af0f2014-12-11 14:34:28 -080018#include "dex_file-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000019#include "dex_instruction.h"
20#include "dex_instruction-inl.h"
21#include "mirror/art_field.h"
22#include "mirror/art_field-inl.h"
23#include "mirror/art_method.h"
24#include "mirror/art_method-inl.h"
25#include "mirror/class.h"
26#include "mirror/class-inl.h"
27#include "mirror/dex_cache.h"
28#include "mirror/dex_cache-inl.h"
29#include "verifier/method_verifier.h"
30#include "verifier/method_verifier-inl.h"
31
32/*
33 * NOTE: This code is part of the quick compiler. It lives in the runtime
34 * only to allow the debugger to check whether a method has been inlined.
35 */
36
37namespace art {
38
Andreas Gampe575e78c2014-11-03 23:41:03 -080039static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
40static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
41static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
42 "iget_object type");
43static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
44 "iget_boolean type");
45static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
46static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
47static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
48static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
49static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
50static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
51 "iput_object type");
52static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
53 "iput_boolean type");
54static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
55static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
56static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
57static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
58 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
59static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
60 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
61static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
62 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
63static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
64 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
65static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
66 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
67static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
68 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
69static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
70 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +000071
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010072// This is used by compiler and debugger. We look into the dex cache for resolved methods and
73// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
74// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
75// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +000076bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
77 InlineMethod* method) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010078 DCHECK(verifier != nullptr);
79 DCHECK_EQ(Runtime::Current()->IsCompiler(), method != nullptr);
80 DCHECK_EQ(verifier->CanLoadClasses(), method != nullptr);
Vladimir Markoe3e02602014-03-12 15:42:41 +000081 // We currently support only plain return or 2-instruction methods.
82
83 const DexFile::CodeItem* code_item = verifier->CodeItem();
84 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
85 const Instruction* instruction = Instruction::At(code_item->insns_);
86 Instruction::Code opcode = instruction->Opcode();
87
88 switch (opcode) {
89 case Instruction::RETURN_VOID:
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010090 if (method != nullptr) {
91 method->opcode = kInlineOpNop;
92 method->flags = kInlineSpecial;
93 method->d.data = 0u;
94 }
Vladimir Markoe3e02602014-03-12 15:42:41 +000095 return true;
96 case Instruction::RETURN:
97 case Instruction::RETURN_OBJECT:
98 case Instruction::RETURN_WIDE:
99 return AnalyseReturnMethod(code_item, method);
100 case Instruction::CONST:
101 case Instruction::CONST_4:
102 case Instruction::CONST_16:
103 case Instruction::CONST_HIGH16:
104 // TODO: Support wide constants (RETURN_WIDE).
105 return AnalyseConstMethod(code_item, method);
106 case Instruction::IGET:
107 case Instruction::IGET_OBJECT:
108 case Instruction::IGET_BOOLEAN:
109 case Instruction::IGET_BYTE:
110 case Instruction::IGET_CHAR:
111 case Instruction::IGET_SHORT:
112 case Instruction::IGET_WIDE:
113 return AnalyseIGetMethod(verifier, method);
114 case Instruction::IPUT:
115 case Instruction::IPUT_OBJECT:
116 case Instruction::IPUT_BOOLEAN:
117 case Instruction::IPUT_BYTE:
118 case Instruction::IPUT_CHAR:
119 case Instruction::IPUT_SHORT:
120 case Instruction::IPUT_WIDE:
121 return AnalyseIPutMethod(verifier, method);
122 default:
123 return false;
124 }
125}
126
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100127bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
128 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
129 const char* method_name = ref.dex_file->GetMethodName(method_id);
130 return strncmp(method_name, "access$", strlen("access$")) == 0;
131}
132
Vladimir Markoe3e02602014-03-12 15:42:41 +0000133bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
134 InlineMethod* result) {
135 const Instruction* return_instruction = Instruction::At(code_item->insns_);
136 Instruction::Code return_opcode = return_instruction->Opcode();
137 uint32_t reg = return_instruction->VRegA_11x();
138 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
139 DCHECK_GE(reg, arg_start);
140 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
141 code_item->registers_size_);
142
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100143 if (result != nullptr) {
144 result->opcode = kInlineOpReturnArg;
145 result->flags = kInlineSpecial;
146 InlineReturnArgData* data = &result->d.return_data;
147 data->arg = reg - arg_start;
148 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
149 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
150 data->reserved = 0u;
151 data->reserved2 = 0u;
152 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000153 return true;
154}
155
156bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
157 InlineMethod* result) {
158 const Instruction* instruction = Instruction::At(code_item->insns_);
159 const Instruction* return_instruction = instruction->Next();
160 Instruction::Code return_opcode = return_instruction->Opcode();
161 if (return_opcode != Instruction::RETURN &&
162 return_opcode != Instruction::RETURN_OBJECT) {
163 return false;
164 }
165
Ian Rogers29a26482014-05-02 15:27:29 -0700166 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000167 DCHECK_LT(return_reg, code_item->registers_size_);
168
Ian Rogers29a26482014-05-02 15:27:29 -0700169 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000170 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700171 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000172 }
Ian Rogers29a26482014-05-02 15:27:29 -0700173 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
174 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000175 return false; // Not returning the value set by const?
176 }
Ian Rogers29a26482014-05-02 15:27:29 -0700177 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000178 return false; // Returning non-null reference constant?
179 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100180 if (result != nullptr) {
181 result->opcode = kInlineOpNonWideConst;
182 result->flags = kInlineSpecial;
Ian Rogers29a26482014-05-02 15:27:29 -0700183 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100184 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000185 return true;
186}
187
188bool InlineMethodAnalyser::AnalyseIGetMethod(verifier::MethodVerifier* verifier,
189 InlineMethod* result) {
190 const DexFile::CodeItem* code_item = verifier->CodeItem();
191 const Instruction* instruction = Instruction::At(code_item->insns_);
192 Instruction::Code opcode = instruction->Opcode();
193 DCHECK(IsInstructionIGet(opcode));
194
195 const Instruction* return_instruction = instruction->Next();
196 Instruction::Code return_opcode = return_instruction->Opcode();
197 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
198 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
199 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
200 opcode != Instruction::IGET_OBJECT)) {
201 return false;
202 }
203
204 uint32_t return_reg = return_instruction->VRegA_11x();
205 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
206 code_item->registers_size_);
207
208 uint32_t dst_reg = instruction->VRegA_22c();
209 uint32_t object_reg = instruction->VRegB_22c();
210 uint32_t field_idx = instruction->VRegC_22c();
211 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
212 DCHECK_GE(object_reg, arg_start);
213 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100214 uint32_t object_arg = object_reg - arg_start;
215
Vladimir Markoe3e02602014-03-12 15:42:41 +0000216 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
217 if (dst_reg != return_reg) {
218 return false; // Not returning the value retrieved by IGET?
219 }
220
Vladimir Markoe1fced12014-04-04 14:52:53 +0100221 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100222 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
223 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
224 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
225 return false;
226 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000227 }
228
Vladimir Markoe1fced12014-04-04 14:52:53 +0100229 // InlineIGetIPutData::object_arg is only 4 bits wide.
230 static constexpr uint16_t kMaxObjectArg = 15u;
231 if (object_arg > kMaxObjectArg) {
232 return false;
233 }
234
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100235 if (result != nullptr) {
236 InlineIGetIPutData* data = &result->d.ifield_data;
237 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
238 return false;
239 }
240 result->opcode = kInlineOpIGet;
241 result->flags = kInlineSpecial;
242 data->op_variant = IGetVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100243 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100244 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100245 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100246 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000247 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000248 return true;
249}
250
251bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
252 InlineMethod* result) {
253 const DexFile::CodeItem* code_item = verifier->CodeItem();
254 const Instruction* instruction = Instruction::At(code_item->insns_);
255 Instruction::Code opcode = instruction->Opcode();
256 DCHECK(IsInstructionIPut(opcode));
257
258 const Instruction* return_instruction = instruction->Next();
259 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100260 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
261 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000262 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100263 if (return_opcode != Instruction::RETURN &&
264 return_opcode != Instruction::RETURN_OBJECT &&
265 return_opcode != Instruction::RETURN_WIDE) {
266 return false;
267 }
268 // Returning an argument.
269 uint32_t return_reg = return_instruction->VRegA_11x();
270 DCHECK_GE(return_reg, arg_start);
271 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
272 code_item->registers_size_);
273 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000274 }
275
276 uint32_t src_reg = instruction->VRegA_22c();
277 uint32_t object_reg = instruction->VRegB_22c();
278 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000279 DCHECK_GE(object_reg, arg_start);
280 DCHECK_LT(object_reg, code_item->registers_size_);
281 DCHECK_GE(src_reg, arg_start);
282 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100283 uint32_t object_arg = object_reg - arg_start;
284 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000285
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100286 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
287 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
288 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
289 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
290 return false;
291 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000292 }
293
Vladimir Markoe1fced12014-04-04 14:52:53 +0100294 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
295 static constexpr uint16_t kMaxObjectArg = 15u;
296 static constexpr uint16_t kMaxSrcArg = 15u;
297 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
298 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
299 return false;
300 }
301
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100302 if (result != nullptr) {
303 InlineIGetIPutData* data = &result->d.ifield_data;
304 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
305 return false;
306 }
307 result->opcode = kInlineOpIPut;
308 result->flags = kInlineSpecial;
309 data->op_variant = IPutVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100310 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100311 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
312 data->src_arg = src_arg;
313 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000314 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000315 return true;
316}
317
318bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
319 verifier::MethodVerifier* verifier,
320 InlineIGetIPutData* result) {
321 mirror::DexCache* dex_cache = verifier->GetDexCache();
322 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
323 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
324 mirror::ArtField* field = dex_cache->GetResolvedField(field_idx);
325 if (method == nullptr || field == nullptr || field->IsStatic()) {
326 return false;
327 }
328 mirror::Class* method_class = method->GetDeclaringClass();
329 mirror::Class* field_class = field->GetDeclaringClass();
330 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
331 (is_put && field->IsFinal() && method_class != field_class)) {
332 return false;
333 }
334 DCHECK_GE(field->GetOffset().Int32Value(), 0);
335 result->field_idx = field_idx;
336 result->field_offset = field->GetOffset().Int32Value();
337 result->is_volatile = field->IsVolatile();
338 return true;
339}
340
341} // namespace art