blob: dc6f4ebc9c6acc75fc211afd29b6db5d229ffc8b [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "class_linker-inl.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080023#include "dex_file-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000024#include "dex_instruction.h"
25#include "dex_instruction-inl.h"
Vladimir Marko354efa62016-02-04 19:46:56 +000026#include "dex_instruction_utils.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000027#include "mirror/class-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000028#include "mirror/dex_cache-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000029#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
Vladimir Marko354efa62016-02-04 19:46:56 +000038namespace { // anonymous namespace
39
40// Helper class for matching a pattern.
41class Matcher {
42 public:
43 // Match function type.
44 typedef bool MatchFn(Matcher* matcher);
45
46 template <size_t size>
47 static bool Match(const DexFile::CodeItem* code_item, MatchFn* const (&pattern)[size]);
48
49 // Match and advance.
50
51 static bool Mark(Matcher* matcher);
52
53 template <bool (Matcher::*Fn)()>
54 static bool Required(Matcher* matcher);
55
56 template <bool (Matcher::*Fn)()>
57 static bool Repeated(Matcher* matcher); // On match, returns to the mark.
58
59 // Match an individual instruction.
60
61 template <Instruction::Code opcode> bool Opcode();
62 bool Const0();
63 bool IPutOnThis();
64
65 private:
66 explicit Matcher(const DexFile::CodeItem* code_item)
67 : code_item_(code_item),
68 instruction_(Instruction::At(code_item->insns_)),
69 pos_(0u),
70 mark_(0u) { }
71
72 static bool DoMatch(const DexFile::CodeItem* code_item, MatchFn* const* pattern, size_t size);
73
74 const DexFile::CodeItem* const code_item_;
75 const Instruction* instruction_;
76 size_t pos_;
77 size_t mark_;
78};
79
80template <size_t size>
81bool Matcher::Match(const DexFile::CodeItem* code_item, MatchFn* const (&pattern)[size]) {
82 return DoMatch(code_item, pattern, size);
83}
84
85bool Matcher::Mark(Matcher* matcher) {
86 matcher->pos_ += 1u; // Advance to the next match function before marking.
87 matcher->mark_ = matcher->pos_;
88 return true;
89}
90
91template <bool (Matcher::*Fn)()>
92bool Matcher::Required(Matcher* matcher) {
93 if (!(matcher->*Fn)()) {
94 return false;
95 }
96 matcher->pos_ += 1u;
97 matcher->instruction_ = matcher->instruction_->Next();
98 return true;
99}
100
101template <bool (Matcher::*Fn)()>
102bool Matcher::Repeated(Matcher* matcher) {
103 if (!(matcher->*Fn)()) {
104 // Didn't match optional instruction, try the next match function.
105 matcher->pos_ += 1u;
106 return true;
107 }
108 matcher->pos_ = matcher->mark_;
109 matcher->instruction_ = matcher->instruction_->Next();
110 return true;
111}
112
113template <Instruction::Code opcode>
114bool Matcher::Opcode() {
115 return instruction_->Opcode() == opcode;
116}
117
118// Match const 0.
119bool Matcher::Const0() {
120 return IsInstructionDirectConst(instruction_->Opcode()) &&
121 (instruction_->Opcode() == Instruction::CONST_WIDE ? instruction_->VRegB_51l() == 0
122 : instruction_->VRegB() == 0);
123}
124
125bool Matcher::IPutOnThis() {
126 DCHECK_NE(code_item_->ins_size_, 0u);
127 return IsInstructionIPut(instruction_->Opcode()) &&
128 instruction_->VRegB_22c() == code_item_->registers_size_ - code_item_->ins_size_;
129}
130
131bool Matcher::DoMatch(const DexFile::CodeItem* code_item, MatchFn* const* pattern, size_t size) {
132 Matcher matcher(code_item);
133 while (matcher.pos_ != size) {
134 if (!pattern[matcher.pos_](&matcher)) {
135 return false;
136 }
137 }
138 return true;
139}
140
141// Used for a single invoke in a constructor. In that situation, the method verifier makes
142// sure we invoke a constructor either in the same class or superclass with at least "this".
143ArtMethod* GetTargetConstructor(ArtMethod* method, const Instruction* invoke_direct)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000145 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
146 DCHECK_EQ(invoke_direct->VRegC_35c(),
147 method->GetCodeItem()->registers_size_ - method->GetCodeItem()->ins_size_);
148 uint32_t method_index = invoke_direct->VRegB_35c();
Andreas Gampe542451c2016-07-26 09:02:02 -0700149 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko354efa62016-02-04 19:46:56 +0000150 ArtMethod* target_method =
151 method->GetDexCache()->GetResolvedMethod(method_index, pointer_size);
152 if (kIsDebugBuild && target_method != nullptr) {
153 CHECK(!target_method->IsStatic());
154 CHECK(target_method->IsConstructor());
155 CHECK(target_method->GetDeclaringClass() == method->GetDeclaringClass() ||
156 target_method->GetDeclaringClass() == method->GetDeclaringClass()->GetSuperClass());
157 }
158 return target_method;
159}
160
161// Return the forwarded arguments and check that all remaining arguments are zero.
162// If the check fails, return static_cast<size_t>(-1).
163size_t CountForwardedConstructorArguments(const DexFile::CodeItem* code_item,
164 const Instruction* invoke_direct,
165 uint16_t zero_vreg_mask) {
166 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
167 size_t number_of_args = invoke_direct->VRegA_35c();
168 DCHECK_NE(number_of_args, 0u);
169 uint32_t args[Instruction::kMaxVarArgRegs];
170 invoke_direct->GetVarArgs(args);
171 uint16_t this_vreg = args[0];
172 DCHECK_EQ(this_vreg, code_item->registers_size_ - code_item->ins_size_); // Checked by verifier.
173 size_t forwarded = 1u;
174 while (forwarded < number_of_args &&
175 args[forwarded] == this_vreg + forwarded &&
176 (zero_vreg_mask & (1u << args[forwarded])) == 0) {
177 ++forwarded;
178 }
179 for (size_t i = forwarded; i != number_of_args; ++i) {
180 if ((zero_vreg_mask & (1u << args[i])) == 0) {
181 return static_cast<size_t>(-1);
182 }
183 }
184 return forwarded;
185}
186
187uint16_t GetZeroVRegMask(const Instruction* const0) {
188 DCHECK(IsInstructionDirectConst(const0->Opcode()));
189 DCHECK((const0->Opcode() == Instruction::CONST_WIDE) ? const0->VRegB_51l() == 0u
190 : const0->VRegB() == 0);
191 uint16_t base_mask = IsInstructionConstWide(const0->Opcode()) ? 3u : 1u;
192 return base_mask << const0->VRegA();
193}
194
195// We limit the number of IPUTs storing parameters. There can be any number
196// of IPUTs that store the value 0 as they are useless in a constructor as
197// the object always starts zero-initialized. We also eliminate all but the
198// last store to any field as they are not observable; not even if the field
199// is volatile as no reference to the object can escape from a constructor
200// with this pattern.
201static constexpr size_t kMaxConstructorIPuts = 3u;
202
203struct ConstructorIPutData {
204 ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { }
205
206 uint16_t field_index;
207 uint16_t arg;
208};
209
210bool RecordConstructorIPut(ArtMethod* method,
211 const Instruction* new_iput,
212 uint16_t this_vreg,
213 uint16_t zero_vreg_mask,
214 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000216 DCHECK(IsInstructionIPut(new_iput->Opcode()));
217 uint32_t field_index = new_iput->VRegC_22c();
Andreas Gampe542451c2016-07-26 09:02:02 -0700218 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko354efa62016-02-04 19:46:56 +0000219 mirror::DexCache* dex_cache = method->GetDexCache();
220 ArtField* field = dex_cache->GetResolvedField(field_index, pointer_size);
221 if (UNLIKELY(field == nullptr)) {
222 return false;
223 }
224 // Remove previous IPUT to the same field, if any. Different field indexes may refer
225 // to the same field, so we need to compare resolved fields from the dex cache.
226 for (size_t old_pos = 0; old_pos != arraysize(iputs); ++old_pos) {
227 if (iputs[old_pos].field_index == DexFile::kDexNoIndex16) {
228 break;
229 }
230 ArtField* f = dex_cache->GetResolvedField(iputs[old_pos].field_index, pointer_size);
231 DCHECK(f != nullptr);
232 if (f == field) {
233 auto back_it = std::copy(iputs + old_pos + 1, iputs + arraysize(iputs), iputs + old_pos);
234 *back_it = ConstructorIPutData();
235 break;
236 }
237 }
238 // If the stored value isn't zero, record the IPUT.
239 if ((zero_vreg_mask & (1u << new_iput->VRegA_22c())) == 0u) {
240 size_t new_pos = 0;
241 while (new_pos != arraysize(iputs) && iputs[new_pos].field_index != DexFile::kDexNoIndex16) {
242 ++new_pos;
243 }
244 if (new_pos == arraysize(iputs)) {
245 return false; // Exceeded capacity of the output array.
246 }
247 iputs[new_pos].field_index = field_index;
248 iputs[new_pos].arg = new_iput->VRegA_22c() - this_vreg;
249 }
250 return true;
251}
252
253bool DoAnalyseConstructor(const DexFile::CodeItem* code_item,
254 ArtMethod* method,
255 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000257 // On entry we should not have any IPUTs yet.
258 DCHECK_EQ(0, std::count_if(
259 iputs,
260 iputs + arraysize(iputs),
261 [](const ConstructorIPutData& iput_data) {
262 return iput_data.field_index != DexFile::kDexNoIndex16;
263 }));
264
265 // Limit the maximum number of code units we're willing to match.
266 static constexpr size_t kMaxCodeUnits = 16u;
267
268 // Limit the number of registers that the constructor may use to 16.
269 // Given that IPUTs must use low 16 registers and we do not match MOVEs,
270 // this is a reasonable limitation.
271 static constexpr size_t kMaxVRegs = 16u;
272
273 // We try to match a constructor that calls another constructor (either in
274 // superclass or in the same class) with the same parameters, or with some
275 // parameters truncated (allowed only for calls to superclass constructor)
276 // or with extra parameters with value 0 (with any type, including null).
277 // This call can be followed by optional IPUTs on "this" storing either one
278 // of the parameters or 0 and the code must then finish with RETURN_VOID.
279 // The called constructor must be either java.lang.Object.<init>() or it
280 // must also match the same pattern.
281 static Matcher::MatchFn* const kConstructorPattern[] = {
282 &Matcher::Mark,
283 &Matcher::Repeated<&Matcher::Const0>,
284 &Matcher::Required<&Matcher::Opcode<Instruction::INVOKE_DIRECT>>,
285 &Matcher::Mark,
286 &Matcher::Repeated<&Matcher::Const0>,
287 &Matcher::Repeated<&Matcher::IPutOnThis>,
288 &Matcher::Required<&Matcher::Opcode<Instruction::RETURN_VOID>>,
289 };
290
291 DCHECK(method != nullptr);
292 DCHECK(!method->IsStatic());
293 DCHECK(method->IsConstructor());
294 DCHECK(code_item != nullptr);
295 if (!method->GetDeclaringClass()->IsVerified() ||
296 code_item->insns_size_in_code_units_ > kMaxCodeUnits ||
297 code_item->registers_size_ > kMaxVRegs ||
298 !Matcher::Match(code_item, kConstructorPattern)) {
299 return false;
300 }
301
302 // Verify the invoke, prevent a few odd cases and collect IPUTs.
303 uint16_t this_vreg = code_item->registers_size_ - code_item->ins_size_;
304 uint16_t zero_vreg_mask = 0u;
305 for (const Instruction* instruction = Instruction::At(code_item->insns_);
306 instruction->Opcode() != Instruction::RETURN_VOID;
307 instruction = instruction->Next()) {
308 if (instruction->Opcode() == Instruction::INVOKE_DIRECT) {
309 ArtMethod* target_method = GetTargetConstructor(method, instruction);
310 if (target_method == nullptr) {
311 return false;
312 }
313 // We allow forwarding constructors only if they pass more arguments
314 // to prevent infinite recursion.
315 if (target_method->GetDeclaringClass() == method->GetDeclaringClass() &&
316 instruction->VRegA_35c() <= code_item->ins_size_) {
317 return false;
318 }
319 size_t forwarded = CountForwardedConstructorArguments(code_item, instruction, zero_vreg_mask);
320 if (forwarded == static_cast<size_t>(-1)) {
321 return false;
322 }
323 if (target_method->GetDeclaringClass()->IsObjectClass()) {
Nicolas Geoffray141de8c2016-03-23 08:22:12 +0000324 DCHECK_EQ(Instruction::At(target_method->GetCodeItem()->insns_)->Opcode(),
325 Instruction::RETURN_VOID);
Vladimir Marko354efa62016-02-04 19:46:56 +0000326 } else {
327 const DexFile::CodeItem* target_code_item = target_method->GetCodeItem();
328 if (target_code_item == nullptr) {
329 return false; // Native constructor?
330 }
331 if (!DoAnalyseConstructor(target_code_item, target_method, iputs)) {
332 return false;
333 }
334 // Prune IPUTs with zero input.
335 auto kept_end = std::remove_if(
336 iputs,
337 iputs + arraysize(iputs),
338 [forwarded](const ConstructorIPutData& iput_data) {
339 return iput_data.arg >= forwarded;
340 });
341 std::fill(kept_end, iputs + arraysize(iputs), ConstructorIPutData());
342 // If we have any IPUTs from the call, check that the target method is in the same
343 // dex file (compare DexCache references), otherwise field_indexes would be bogus.
344 if (iputs[0].field_index != DexFile::kDexNoIndex16 &&
345 target_method->GetDexCache() != method->GetDexCache()) {
346 return false;
347 }
348 }
349 } else if (IsInstructionDirectConst(instruction->Opcode())) {
350 zero_vreg_mask |= GetZeroVRegMask(instruction);
351 if ((zero_vreg_mask & (1u << this_vreg)) != 0u) {
352 return false; // Overwriting `this` is unsupported.
353 }
354 } else {
355 DCHECK(IsInstructionIPut(instruction->Opcode()));
356 DCHECK_EQ(instruction->VRegB_22c(), this_vreg);
357 if (!RecordConstructorIPut(method, instruction, this_vreg, zero_vreg_mask, iputs)) {
358 return false;
359 }
360 }
361 }
362 return true;
363}
364
365} // anonymous namespace
366
367bool AnalyseConstructor(const DexFile::CodeItem* code_item,
368 ArtMethod* method,
369 InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700370 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000371 ConstructorIPutData iputs[kMaxConstructorIPuts];
372 if (!DoAnalyseConstructor(code_item, method, iputs)) {
373 return false;
374 }
375 static_assert(kMaxConstructorIPuts == 3, "Unexpected limit"); // Code below depends on this.
376 DCHECK(iputs[0].field_index != DexFile::kDexNoIndex16 ||
377 iputs[1].field_index == DexFile::kDexNoIndex16);
378 DCHECK(iputs[1].field_index != DexFile::kDexNoIndex16 ||
379 iputs[2].field_index == DexFile::kDexNoIndex16);
380
381#define STORE_IPUT(n) \
382 do { \
383 result->d.constructor_data.iput##n##_field_index = iputs[n].field_index; \
384 result->d.constructor_data.iput##n##_arg = iputs[n].arg; \
385 } while (false)
386
387 STORE_IPUT(0);
388 STORE_IPUT(1);
389 STORE_IPUT(2);
390#undef STORE_IPUT
391
392 result->opcode = kInlineOpConstructor;
393 result->flags = kInlineSpecial;
394 result->d.constructor_data.reserved = 0u;
395 return true;
396}
397
Andreas Gampe575e78c2014-11-03 23:41:03 -0800398static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
399static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
400static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
401 "iget_object type");
402static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
403 "iget_boolean type");
404static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
405static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
406static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
407static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
408static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
409static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
410 "iput_object type");
411static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
412 "iput_boolean type");
413static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
414static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
415static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
416static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
417 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
418static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
419 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
420static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
421 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
422static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
423 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
424static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
425 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
426static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
427 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
428static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
429 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000430
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100431// This is used by compiler and debugger. We look into the dex cache for resolved methods and
432// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
433// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
434// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000435bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000436 InlineMethod* result) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100437 DCHECK(verifier != nullptr);
Calin Juravleffc87072016-04-20 14:22:09 +0100438 if (!Runtime::Current()->UseJitCompilation()) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000439 DCHECK_EQ(verifier->CanLoadClasses(), result != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800440 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000441
442 // Note: verifier->GetMethod() may be null.
443 return AnalyseMethodCode(verifier->CodeItem(),
444 verifier->GetMethodReference(),
445 (verifier->GetAccessFlags() & kAccStatic) != 0u,
446 verifier->GetMethod(),
447 result);
448}
449
450bool InlineMethodAnalyser::AnalyseMethodCode(ArtMethod* method, InlineMethod* result) {
451 const DexFile::CodeItem* code_item = method->GetCodeItem();
452 if (code_item == nullptr) {
453 // Native or abstract.
454 return false;
455 }
456 return AnalyseMethodCode(
457 code_item, method->ToMethodReference(), method->IsStatic(), method, result);
458}
459
460bool InlineMethodAnalyser::AnalyseMethodCode(const DexFile::CodeItem* code_item,
461 const MethodReference& method_ref,
462 bool is_static,
463 ArtMethod* method,
464 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000465 // We currently support only plain return or 2-instruction methods.
466
Vladimir Markoe3e02602014-03-12 15:42:41 +0000467 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
468 const Instruction* instruction = Instruction::At(code_item->insns_);
469 Instruction::Code opcode = instruction->Opcode();
470
471 switch (opcode) {
472 case Instruction::RETURN_VOID:
Vladimir Marko9f35ccd2016-02-02 20:12:32 +0000473 if (result != nullptr) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000474 result->opcode = kInlineOpNop;
475 result->flags = kInlineSpecial;
476 result->d.data = 0u;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100477 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000478 return true;
479 case Instruction::RETURN:
480 case Instruction::RETURN_OBJECT:
481 case Instruction::RETURN_WIDE:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000482 return AnalyseReturnMethod(code_item, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000483 case Instruction::CONST:
484 case Instruction::CONST_4:
485 case Instruction::CONST_16:
486 case Instruction::CONST_HIGH16:
487 // TODO: Support wide constants (RETURN_WIDE).
Vladimir Marko354efa62016-02-04 19:46:56 +0000488 if (AnalyseConstMethod(code_item, result)) {
489 return true;
490 }
491 FALLTHROUGH_INTENDED;
492 case Instruction::CONST_WIDE:
493 case Instruction::CONST_WIDE_16:
494 case Instruction::CONST_WIDE_32:
495 case Instruction::CONST_WIDE_HIGH16:
496 case Instruction::INVOKE_DIRECT:
497 if (method != nullptr && !method->IsStatic() && method->IsConstructor()) {
498 return AnalyseConstructor(code_item, method, result);
499 }
500 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000501 case Instruction::IGET:
502 case Instruction::IGET_OBJECT:
503 case Instruction::IGET_BOOLEAN:
504 case Instruction::IGET_BYTE:
505 case Instruction::IGET_CHAR:
506 case Instruction::IGET_SHORT:
507 case Instruction::IGET_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800508 // TODO: Add handling for JIT.
509 // case Instruction::IGET_QUICK:
510 // case Instruction::IGET_WIDE_QUICK:
511 // case Instruction::IGET_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000512 return AnalyseIGetMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000513 case Instruction::IPUT:
514 case Instruction::IPUT_OBJECT:
515 case Instruction::IPUT_BOOLEAN:
516 case Instruction::IPUT_BYTE:
517 case Instruction::IPUT_CHAR:
518 case Instruction::IPUT_SHORT:
519 case Instruction::IPUT_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800520 // TODO: Add handling for JIT.
521 // case Instruction::IPUT_QUICK:
522 // case Instruction::IPUT_WIDE_QUICK:
523 // case Instruction::IPUT_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000524 return AnalyseIPutMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000525 default:
526 return false;
527 }
528}
529
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100530bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
531 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
532 const char* method_name = ref.dex_file->GetMethodName(method_id);
Vladimir Markod5f10052015-05-06 14:09:04 +0100533 // javac names synthetic accessors "access$nnn",
534 // jack names them "-getN", "-putN", "-wrapN".
535 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
536 strncmp(method_name, "-", strlen("-")) == 0;
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100537}
538
Vladimir Markoe3e02602014-03-12 15:42:41 +0000539bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
540 InlineMethod* result) {
541 const Instruction* return_instruction = Instruction::At(code_item->insns_);
542 Instruction::Code return_opcode = return_instruction->Opcode();
543 uint32_t reg = return_instruction->VRegA_11x();
544 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
545 DCHECK_GE(reg, arg_start);
546 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
547 code_item->registers_size_);
548
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100549 if (result != nullptr) {
550 result->opcode = kInlineOpReturnArg;
551 result->flags = kInlineSpecial;
552 InlineReturnArgData* data = &result->d.return_data;
553 data->arg = reg - arg_start;
554 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
555 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
556 data->reserved = 0u;
557 data->reserved2 = 0u;
558 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000559 return true;
560}
561
562bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
563 InlineMethod* result) {
564 const Instruction* instruction = Instruction::At(code_item->insns_);
565 const Instruction* return_instruction = instruction->Next();
566 Instruction::Code return_opcode = return_instruction->Opcode();
567 if (return_opcode != Instruction::RETURN &&
568 return_opcode != Instruction::RETURN_OBJECT) {
569 return false;
570 }
571
Ian Rogers29a26482014-05-02 15:27:29 -0700572 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000573 DCHECK_LT(return_reg, code_item->registers_size_);
574
Ian Rogers29a26482014-05-02 15:27:29 -0700575 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000576 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700577 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000578 }
Ian Rogers29a26482014-05-02 15:27:29 -0700579 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
580 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000581 return false; // Not returning the value set by const?
582 }
Ian Rogers29a26482014-05-02 15:27:29 -0700583 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000584 return false; // Returning non-null reference constant?
585 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100586 if (result != nullptr) {
587 result->opcode = kInlineOpNonWideConst;
588 result->flags = kInlineSpecial;
Ian Rogers29a26482014-05-02 15:27:29 -0700589 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100590 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000591 return true;
592}
593
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000594bool InlineMethodAnalyser::AnalyseIGetMethod(const DexFile::CodeItem* code_item,
595 const MethodReference& method_ref,
596 bool is_static,
597 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000598 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000599 const Instruction* instruction = Instruction::At(code_item->insns_);
600 Instruction::Code opcode = instruction->Opcode();
601 DCHECK(IsInstructionIGet(opcode));
602
603 const Instruction* return_instruction = instruction->Next();
604 Instruction::Code return_opcode = return_instruction->Opcode();
605 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
606 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
607 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
608 opcode != Instruction::IGET_OBJECT)) {
609 return false;
610 }
611
612 uint32_t return_reg = return_instruction->VRegA_11x();
613 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
614 code_item->registers_size_);
615
616 uint32_t dst_reg = instruction->VRegA_22c();
617 uint32_t object_reg = instruction->VRegB_22c();
618 uint32_t field_idx = instruction->VRegC_22c();
619 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
620 DCHECK_GE(object_reg, arg_start);
621 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100622 uint32_t object_arg = object_reg - arg_start;
623
Vladimir Markoe3e02602014-03-12 15:42:41 +0000624 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
625 if (dst_reg != return_reg) {
626 return false; // Not returning the value retrieved by IGET?
627 }
628
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000629 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100630 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
631 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000632 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100633 return false;
634 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000635 }
636
Vladimir Markoe1fced12014-04-04 14:52:53 +0100637 // InlineIGetIPutData::object_arg is only 4 bits wide.
638 static constexpr uint16_t kMaxObjectArg = 15u;
639 if (object_arg > kMaxObjectArg) {
640 return false;
641 }
642
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100643 if (result != nullptr) {
644 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000645 if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100646 return false;
647 }
648 result->opcode = kInlineOpIGet;
649 result->flags = kInlineSpecial;
650 data->op_variant = IGetVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000651 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100652 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100653 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100654 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000655 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000656 return true;
657}
658
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000659bool InlineMethodAnalyser::AnalyseIPutMethod(const DexFile::CodeItem* code_item,
660 const MethodReference& method_ref,
661 bool is_static,
662 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000663 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000664 const Instruction* instruction = Instruction::At(code_item->insns_);
665 Instruction::Code opcode = instruction->Opcode();
666 DCHECK(IsInstructionIPut(opcode));
667
668 const Instruction* return_instruction = instruction->Next();
669 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100670 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
671 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000672 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100673 if (return_opcode != Instruction::RETURN &&
674 return_opcode != Instruction::RETURN_OBJECT &&
675 return_opcode != Instruction::RETURN_WIDE) {
676 return false;
677 }
678 // Returning an argument.
679 uint32_t return_reg = return_instruction->VRegA_11x();
680 DCHECK_GE(return_reg, arg_start);
681 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
682 code_item->registers_size_);
683 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000684 }
685
686 uint32_t src_reg = instruction->VRegA_22c();
687 uint32_t object_reg = instruction->VRegB_22c();
688 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000689 DCHECK_GE(object_reg, arg_start);
690 DCHECK_LT(object_reg, code_item->registers_size_);
691 DCHECK_GE(src_reg, arg_start);
692 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100693 uint32_t object_arg = object_reg - arg_start;
694 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000695
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000696 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100697 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
698 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000699 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100700 return false;
701 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000702 }
703
Vladimir Markoe1fced12014-04-04 14:52:53 +0100704 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
705 static constexpr uint16_t kMaxObjectArg = 15u;
706 static constexpr uint16_t kMaxSrcArg = 15u;
707 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
708 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
709 return false;
710 }
711
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100712 if (result != nullptr) {
713 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000714 if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100715 return false;
716 }
717 result->opcode = kInlineOpIPut;
718 result->flags = kInlineSpecial;
719 data->op_variant = IPutVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000720 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100721 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
722 data->src_arg = src_arg;
723 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000724 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000725 return true;
726}
727
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000728bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method,
729 uint32_t field_idx,
730 bool is_put,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000731 InlineIGetIPutData* result) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000732 if (method == nullptr) {
733 return false;
734 }
735 mirror::DexCache* dex_cache = method->GetDexCache();
Andreas Gampe542451c2016-07-26 09:02:02 -0700736 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000737 ArtField* field = dex_cache->GetResolvedField(field_idx, pointer_size);
738 if (field == nullptr || field->IsStatic()) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000739 return false;
740 }
741 mirror::Class* method_class = method->GetDeclaringClass();
742 mirror::Class* field_class = field->GetDeclaringClass();
743 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
744 (is_put && field->IsFinal() && method_class != field_class)) {
745 return false;
746 }
747 DCHECK_GE(field->GetOffset().Int32Value(), 0);
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000748 // Do not interleave function calls with bit field writes to placate valgrind. Bug: 27552451.
749 uint32_t field_offset = field->GetOffset().Uint32Value();
750 bool is_volatile = field->IsVolatile();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000751 result->field_idx = field_idx;
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000752 result->field_offset = field_offset;
753 result->is_volatile = is_volatile ? 1u : 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000754 return true;
755}
756
757} // namespace art