blob: 0a1b72e6b4c5ddcc89abe22269eda4ea1a809b79 [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_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100221 uint32_t object_arg = object_reg - arg_start;
222
Vladimir Markoe3e02602014-03-12 15:42:41 +0000223 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
224 if (dst_reg != return_reg) {
225 return false; // Not returning the value retrieved by IGET?
226 }
227
Vladimir Markoe1fced12014-04-04 14:52:53 +0100228 if ((verifier->GetAccessFlags() & kAccStatic) != 0u || object_arg != 0u) {
Mathieu Chartier389e11d2014-04-04 18:02:04 -0700229 // TODO: Support inlining IGET on other register than "this".
230 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000231 }
232
Vladimir Markoe1fced12014-04-04 14:52:53 +0100233 // InlineIGetIPutData::object_arg is only 4 bits wide.
234 static constexpr uint16_t kMaxObjectArg = 15u;
235 if (object_arg > kMaxObjectArg) {
236 return false;
237 }
238
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100239 if (result != nullptr) {
240 InlineIGetIPutData* data = &result->d.ifield_data;
241 if (!ComputeSpecialAccessorInfo(field_idx, false, verifier, data)) {
242 return false;
243 }
244 result->opcode = kInlineOpIGet;
245 result->flags = kInlineSpecial;
246 data->op_variant = IGetVariant(opcode);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100247 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0 ? 1u : 0u;
248 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Mathieu Chartier389e11d2014-04-04 18:02:04 -0700249 data->src_arg = 0;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100250 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000251 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000252 return true;
253}
254
255bool InlineMethodAnalyser::AnalyseIPutMethod(verifier::MethodVerifier* verifier,
256 InlineMethod* result) {
257 const DexFile::CodeItem* code_item = verifier->CodeItem();
258 const Instruction* instruction = Instruction::At(code_item->insns_);
259 Instruction::Code opcode = instruction->Opcode();
260 DCHECK(IsInstructionIPut(opcode));
261
262 const Instruction* return_instruction = instruction->Next();
263 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100264 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
265 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000266 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100267 if (return_opcode != Instruction::RETURN &&
268 return_opcode != Instruction::RETURN_OBJECT &&
269 return_opcode != Instruction::RETURN_WIDE) {
270 return false;
271 }
272 // Returning an argument.
273 uint32_t return_reg = return_instruction->VRegA_11x();
274 DCHECK_GE(return_reg, arg_start);
275 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
276 code_item->registers_size_);
277 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000278 }
279
280 uint32_t src_reg = instruction->VRegA_22c();
281 uint32_t object_reg = instruction->VRegB_22c();
282 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000283 DCHECK_GE(object_reg, arg_start);
284 DCHECK_LT(object_reg, code_item->registers_size_);
285 DCHECK_GE(src_reg, arg_start);
286 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100287 uint32_t object_arg = object_reg - arg_start;
288 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000289
Vladimir Markoe1fced12014-04-04 14:52:53 +0100290 if ((verifier->GetAccessFlags() & kAccStatic) != 0 || object_arg != 0) {
Mathieu Chartier389e11d2014-04-04 18:02:04 -0700291 // TODO: Support inlining IPUT on other register than "this".
292 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000293 }
294
Vladimir Markoe1fced12014-04-04 14:52:53 +0100295 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
296 static constexpr uint16_t kMaxObjectArg = 15u;
297 static constexpr uint16_t kMaxSrcArg = 15u;
298 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
299 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
300 return false;
301 }
302
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100303 if (result != nullptr) {
304 InlineIGetIPutData* data = &result->d.ifield_data;
305 if (!ComputeSpecialAccessorInfo(field_idx, true, verifier, data)) {
306 return false;
307 }
308 result->opcode = kInlineOpIPut;
309 result->flags = kInlineSpecial;
310 data->op_variant = IPutVariant(opcode);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100311 data->method_is_static = (verifier->GetAccessFlags() & kAccStatic) != 0 ? 1u : 0u;
312 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
313 data->src_arg = src_arg;
314 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000315 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000316 return true;
317}
318
319bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put,
320 verifier::MethodVerifier* verifier,
321 InlineIGetIPutData* result) {
322 mirror::DexCache* dex_cache = verifier->GetDexCache();
323 uint32_t method_idx = verifier->GetMethodReference().dex_method_index;
324 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
325 mirror::ArtField* field = dex_cache->GetResolvedField(field_idx);
326 if (method == nullptr || field == nullptr || field->IsStatic()) {
327 return false;
328 }
329 mirror::Class* method_class = method->GetDeclaringClass();
330 mirror::Class* field_class = field->GetDeclaringClass();
331 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
332 (is_put && field->IsFinal() && method_class != field_class)) {
333 return false;
334 }
335 DCHECK_GE(field->GetOffset().Int32Value(), 0);
336 result->field_idx = field_idx;
337 result->field_offset = field->GetOffset().Int32Value();
338 result->is_volatile = field->IsVolatile();
339 return true;
340}
341
342} // namespace art