blob: a9072d814c6b8a57b2c17ee0e9fb19dafddae9e2 [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
138bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
139 InlineMethod* result) {
140 const Instruction* return_instruction = Instruction::At(code_item->insns_);
141 Instruction::Code return_opcode = return_instruction->Opcode();
142 uint32_t reg = return_instruction->VRegA_11x();
143 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
144 DCHECK_GE(reg, arg_start);
145 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
146 code_item->registers_size_);
147
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100148 if (result != nullptr) {
149 result->opcode = kInlineOpReturnArg;
150 result->flags = kInlineSpecial;
151 InlineReturnArgData* data = &result->d.return_data;
152 data->arg = reg - arg_start;
153 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
154 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
155 data->reserved = 0u;
156 data->reserved2 = 0u;
157 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000158 return true;
159}
160
161bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
162 InlineMethod* result) {
163 const Instruction* instruction = Instruction::At(code_item->insns_);
164 const Instruction* return_instruction = instruction->Next();
165 Instruction::Code return_opcode = return_instruction->Opcode();
166 if (return_opcode != Instruction::RETURN &&
167 return_opcode != Instruction::RETURN_OBJECT) {
168 return false;
169 }
170
171 uint32_t return_reg = return_instruction->VRegA_11x();
172 DCHECK_LT(return_reg, code_item->registers_size_);
173
174 uint32_t vA, vB, dummy;
175 uint64_t dummy_wide;
176 instruction->Decode(vA, vB, dummy_wide, dummy, nullptr);
177 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
178 vB <<= 16;
179 }
180 DCHECK_LT(vA, code_item->registers_size_);
181 if (vA != return_reg) {
182 return false; // Not returning the value set by const?
183 }
184 if (return_opcode == Instruction::RETURN_OBJECT && vB != 0) {
185 return false; // Returning non-null reference constant?
186 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100187 if (result != nullptr) {
188 result->opcode = kInlineOpNonWideConst;
189 result->flags = kInlineSpecial;
190 result->d.data = static_cast<uint64_t>(vB);
191 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000192 return true;
193}
194
195bool InlineMethodAnalyser::AnalyseIGetMethod(verifier::MethodVerifier* verifier,
196 InlineMethod* result) {
197 const DexFile::CodeItem* code_item = verifier->CodeItem();
198 const Instruction* instruction = Instruction::At(code_item->insns_);
199 Instruction::Code opcode = instruction->Opcode();
200 DCHECK(IsInstructionIGet(opcode));
201
202 const Instruction* return_instruction = instruction->Next();
203 Instruction::Code return_opcode = return_instruction->Opcode();
204 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
205 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
206 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
207 opcode != Instruction::IGET_OBJECT)) {
208 return false;
209 }
210
211 uint32_t return_reg = return_instruction->VRegA_11x();
212 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
213 code_item->registers_size_);
214
215 uint32_t dst_reg = instruction->VRegA_22c();
216 uint32_t object_reg = instruction->VRegB_22c();
217 uint32_t field_idx = instruction->VRegC_22c();
218 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
219 DCHECK_GE(object_reg, arg_start);
220 DCHECK_LT(object_reg, code_item->registers_size_);
221 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
222 if (dst_reg != return_reg) {
223 return false; // Not returning the value retrieved by IGET?
224 }
225
226 if ((verifier->GetAccessFlags() & kAccStatic) != 0 || object_reg != arg_start) {
227 // TODO: Support inlining IGET on other register than "this".
228 return false;
229 }
230
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100231 if (result != nullptr) {
232 InlineIGetIPutData* data = &result->d.ifield_data;
233 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
234 return false;
235 }
236 result->opcode = kInlineOpIGet;
237 result->flags = kInlineSpecial;
238 data->op_variant = IGetVariant(opcode);
239 data->object_arg = object_reg - arg_start; // Allow IGET on any register, not just "this".
240 data->src_arg = 0;
241 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0;
242 data->reserved = 0;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000243 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000244 return true;
245}
246
247bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
248 InlineMethod* result) {
249 const DexFile::CodeItem* code_item = verifier->CodeItem();
250 const Instruction* instruction = Instruction::At(code_item->insns_);
251 Instruction::Code opcode = instruction->Opcode();
252 DCHECK(IsInstructionIPut(opcode));
253
254 const Instruction* return_instruction = instruction->Next();
255 Instruction::Code return_opcode = return_instruction->Opcode();
256 if (return_opcode != Instruction::RETURN_VOID) {
257 // TODO: Support returning an argument.
258 // This is needed by builder classes and generated accessor setters.
259 // builder.setX(value): iput value, this, fieldX; return-object this;
260 // object.access$nnn(value): iput value, this, fieldX; return value;
261 // Use InlineIGetIPutData::reserved to hold the information.
262 return false;
263 }
264
265 uint32_t src_reg = instruction->VRegA_22c();
266 uint32_t object_reg = instruction->VRegB_22c();
267 uint32_t field_idx = instruction->VRegC_22c();
268 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
269 DCHECK_GE(object_reg, arg_start);
270 DCHECK_LT(object_reg, code_item->registers_size_);
271 DCHECK_GE(src_reg, arg_start);
272 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
273
274 if ((verifier->GetAccessFlags() & kAccStatic) != 0 || object_reg != arg_start) {
275 // TODO: Support inlining IPUT on other register than "this".
276 return false;
277 }
278
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100279 if (result != nullptr) {
280 InlineIGetIPutData* data = &result->d.ifield_data;
281 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
282 return false;
283 }
284 result->opcode = kInlineOpIPut;
285 result->flags = kInlineSpecial;
286 data->op_variant = IPutVariant(opcode);
287 data->object_arg = object_reg - arg_start; // Allow IPUT on any register, not just "this".
288 data->src_arg = src_reg - arg_start;
289 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0;
290 data->reserved = 0;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000291 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000292 return true;
293}
294
295bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
296 verifier::MethodVerifier* verifier,
297 InlineIGetIPutData* result) {
298 mirror::DexCache* dex_cache = verifier->GetDexCache();
299 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
300 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
301 mirror::ArtField* field = dex_cache->GetResolvedField(field_idx);
302 if (method == nullptr || field == nullptr || field->IsStatic()) {
303 return false;
304 }
305 mirror::Class* method_class = method->GetDeclaringClass();
306 mirror::Class* field_class = field->GetDeclaringClass();
307 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
308 (is_put && field->IsFinal() && method_class != field_class)) {
309 return false;
310 }
311 DCHECK_GE(field->GetOffset().Int32Value(), 0);
312 result->field_idx = field_idx;
313 result->field_offset = field->GetOffset().Int32Value();
314 result->is_volatile = field->IsVolatile();
315 return true;
316}
317
318} // namespace art