blob: 3415e8f9ba5c5f82331ad70398179bb95e315994 [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
Andreas Gampe575e78c2014-11-03 23:41:03 -080038static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
39static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
40static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
41 "iget_object type");
42static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
43 "iget_boolean type");
44static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
45static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
46static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
47static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
48static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
49static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
50 "iput_object type");
51static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
52 "iput_boolean type");
53static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
54static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
55static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
56static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
57 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
58static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
59 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
60static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
61 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
62static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
63 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
64static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
65 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
66static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
67 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
68static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
69 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +000070
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010071// This is used by compiler and debugger. We look into the dex cache for resolved methods and
72// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
73// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
74// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +000075bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
76 InlineMethod* method) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010077 DCHECK(verifier != nullptr);
78 DCHECK_EQ(Runtime::Current()->IsCompiler(), method != nullptr);
79 DCHECK_EQ(verifier->CanLoadClasses(), method != nullptr);
Vladimir Markoe3e02602014-03-12 15:42:41 +000080 // We currently support only plain return or 2-instruction methods.
81
82 const DexFile::CodeItem* code_item = verifier->CodeItem();
83 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
84 const Instruction* instruction = Instruction::At(code_item->insns_);
85 Instruction::Code opcode = instruction->Opcode();
86
87 switch (opcode) {
88 case Instruction::RETURN_VOID:
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +010089 if (method != nullptr) {
90 method->opcode = kInlineOpNop;
91 method->flags = kInlineSpecial;
92 method->d.data = 0u;
93 }
Vladimir Markoe3e02602014-03-12 15:42:41 +000094 return true;
95 case Instruction::RETURN:
96 case Instruction::RETURN_OBJECT:
97 case Instruction::RETURN_WIDE:
98 return AnalyseReturnMethod(code_item, method);
99 case Instruction::CONST:
100 case Instruction::CONST_4:
101 case Instruction::CONST_16:
102 case Instruction::CONST_HIGH16:
103 // TODO: Support wide constants (RETURN_WIDE).
104 return AnalyseConstMethod(code_item, method);
105 case Instruction::IGET:
106 case Instruction::IGET_OBJECT:
107 case Instruction::IGET_BOOLEAN:
108 case Instruction::IGET_BYTE:
109 case Instruction::IGET_CHAR:
110 case Instruction::IGET_SHORT:
111 case Instruction::IGET_WIDE:
112 return AnalyseIGetMethod(verifier, method);
113 case Instruction::IPUT:
114 case Instruction::IPUT_OBJECT:
115 case Instruction::IPUT_BOOLEAN:
116 case Instruction::IPUT_BYTE:
117 case Instruction::IPUT_CHAR:
118 case Instruction::IPUT_SHORT:
119 case Instruction::IPUT_WIDE:
120 return AnalyseIPutMethod(verifier, method);
121 default:
122 return false;
123 }
124}
125
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100126bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
127 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
128 const char* method_name = ref.dex_file->GetMethodName(method_id);
129 return strncmp(method_name, "access$", strlen("access$")) == 0;
130}
131
Vladimir Markoe3e02602014-03-12 15:42:41 +0000132bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
133 InlineMethod* result) {
134 const Instruction* return_instruction = Instruction::At(code_item->insns_);
135 Instruction::Code return_opcode = return_instruction->Opcode();
136 uint32_t reg = return_instruction->VRegA_11x();
137 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
138 DCHECK_GE(reg, arg_start);
139 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
140 code_item->registers_size_);
141
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100142 if (result != nullptr) {
143 result->opcode = kInlineOpReturnArg;
144 result->flags = kInlineSpecial;
145 InlineReturnArgData* data = &result->d.return_data;
146 data->arg = reg - arg_start;
147 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
148 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
149 data->reserved = 0u;
150 data->reserved2 = 0u;
151 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000152 return true;
153}
154
155bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
156 InlineMethod* result) {
157 const Instruction* instruction = Instruction::At(code_item->insns_);
158 const Instruction* return_instruction = instruction->Next();
159 Instruction::Code return_opcode = return_instruction->Opcode();
160 if (return_opcode != Instruction::RETURN &&
161 return_opcode != Instruction::RETURN_OBJECT) {
162 return false;
163 }
164
Ian Rogers29a26482014-05-02 15:27:29 -0700165 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000166 DCHECK_LT(return_reg, code_item->registers_size_);
167
Ian Rogers29a26482014-05-02 15:27:29 -0700168 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000169 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700170 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000171 }
Ian Rogers29a26482014-05-02 15:27:29 -0700172 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
173 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000174 return false; // Not returning the value set by const?
175 }
Ian Rogers29a26482014-05-02 15:27:29 -0700176 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000177 return false; // Returning non-null reference constant?
178 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100179 if (result != nullptr) {
180 result->opcode = kInlineOpNonWideConst;
181 result->flags = kInlineSpecial;
Ian Rogers29a26482014-05-02 15:27:29 -0700182 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100183 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000184 return true;
185}
186
187bool InlineMethodAnalyser::AnalyseIGetMethod(verifier::MethodVerifier* verifier,
188 InlineMethod* result) {
189 const DexFile::CodeItem* code_item = verifier->CodeItem();
190 const Instruction* instruction = Instruction::At(code_item->insns_);
191 Instruction::Code opcode = instruction->Opcode();
192 DCHECK(IsInstructionIGet(opcode));
193
194 const Instruction* return_instruction = instruction->Next();
195 Instruction::Code return_opcode = return_instruction->Opcode();
196 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
197 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
198 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
199 opcode != Instruction::IGET_OBJECT)) {
200 return false;
201 }
202
203 uint32_t return_reg = return_instruction->VRegA_11x();
204 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
205 code_item->registers_size_);
206
207 uint32_t dst_reg = instruction->VRegA_22c();
208 uint32_t object_reg = instruction->VRegB_22c();
209 uint32_t field_idx = instruction->VRegC_22c();
210 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
211 DCHECK_GE(object_reg, arg_start);
212 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100213 uint32_t object_arg = object_reg - arg_start;
214
Vladimir Markoe3e02602014-03-12 15:42:41 +0000215 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
216 if (dst_reg != return_reg) {
217 return false; // Not returning the value retrieved by IGET?
218 }
219
Vladimir Markoe1fced12014-04-04 14:52:53 +0100220 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100221 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
222 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
223 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
224 return false;
225 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000226 }
227
Vladimir Markoe1fced12014-04-04 14:52:53 +0100228 // InlineIGetIPutData::object_arg is only 4 bits wide.
229 static constexpr uint16_t kMaxObjectArg = 15u;
230 if (object_arg > kMaxObjectArg) {
231 return false;
232 }
233
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100234 if (result != nullptr) {
235 InlineIGetIPutData* data = &result->d.ifield_data;
236 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
237 return false;
238 }
239 result->opcode = kInlineOpIGet;
240 result->flags = kInlineSpecial;
241 data->op_variant = IGetVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100242 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100243 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100244 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100245 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000246 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000247 return true;
248}
249
250bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
251 InlineMethod* result) {
252 const DexFile::CodeItem* code_item = verifier->CodeItem();
253 const Instruction* instruction = Instruction::At(code_item->insns_);
254 Instruction::Code opcode = instruction->Opcode();
255 DCHECK(IsInstructionIPut(opcode));
256
257 const Instruction* return_instruction = instruction->Next();
258 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100259 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
260 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000261 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100262 if (return_opcode != Instruction::RETURN &&
263 return_opcode != Instruction::RETURN_OBJECT &&
264 return_opcode != Instruction::RETURN_WIDE) {
265 return false;
266 }
267 // Returning an argument.
268 uint32_t return_reg = return_instruction->VRegA_11x();
269 DCHECK_GE(return_reg, arg_start);
270 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
271 code_item->registers_size_);
272 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000273 }
274
275 uint32_t src_reg = instruction->VRegA_22c();
276 uint32_t object_reg = instruction->VRegB_22c();
277 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000278 DCHECK_GE(object_reg, arg_start);
279 DCHECK_LT(object_reg, code_item->registers_size_);
280 DCHECK_GE(src_reg, arg_start);
281 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100282 uint32_t object_arg = object_reg - arg_start;
283 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000284
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100285 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
286 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
287 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
288 if (!IsSyntheticAccessor(verifier->GetMethodReference())) {
289 return false;
290 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000291 }
292
Vladimir Markoe1fced12014-04-04 14:52:53 +0100293 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
294 static constexpr uint16_t kMaxObjectArg = 15u;
295 static constexpr uint16_t kMaxSrcArg = 15u;
296 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
297 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
298 return false;
299 }
300
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100301 if (result != nullptr) {
302 InlineIGetIPutData* data = &result->d.ifield_data;
303 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
304 return false;
305 }
306 result->opcode = kInlineOpIPut;
307 result->flags = kInlineSpecial;
308 data->op_variant = IPutVariant(opcode);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100309 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0u ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100310 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
311 data->src_arg = src_arg;
312 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000313 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000314 return true;
315}
316
317bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
318 verifier::MethodVerifier* verifier,
319 InlineIGetIPutData* result) {
320 mirror::DexCache* dex_cache = verifier->GetDexCache();
321 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
322 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
323 mirror::ArtField* field = dex_cache->GetResolvedField(field_idx);
324 if (method == nullptr || field == nullptr || field->IsStatic()) {
325 return false;
326 }
327 mirror::Class* method_class = method->GetDeclaringClass();
328 mirror::Class* field_class = field->GetDeclaringClass();
329 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
330 (is_put && field->IsFinal() && method_class != field_class)) {
331 return false;
332 }
333 DCHECK_GE(field->GetOffset().Int32Value(), 0);
334 result->field_idx = field_idx;
335 result->field_offset = field->GetOffset().Int32Value();
336 result->is_volatile = field->IsVolatile();
337 return true;
338}
339
340} // namespace art