blob: c8e3d5edd888a6f0110952d857a0577383fe16ff [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-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "dex_instruction.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
30/*
31 * NOTE: This code is part of the quick compiler. It lives in the runtime
32 * only to allow the debugger to check whether a method has been inlined.
33 */
34
35namespace art {
36
Vladimir Marko354efa62016-02-04 19:46:56 +000037namespace { // anonymous namespace
38
39// Helper class for matching a pattern.
40class Matcher {
41 public:
42 // Match function type.
43 typedef bool MatchFn(Matcher* matcher);
44
45 template <size_t size>
46 static bool Match(const DexFile::CodeItem* code_item, MatchFn* const (&pattern)[size]);
47
48 // Match and advance.
49
50 static bool Mark(Matcher* matcher);
51
52 template <bool (Matcher::*Fn)()>
53 static bool Required(Matcher* matcher);
54
55 template <bool (Matcher::*Fn)()>
56 static bool Repeated(Matcher* matcher); // On match, returns to the mark.
57
58 // Match an individual instruction.
59
60 template <Instruction::Code opcode> bool Opcode();
61 bool Const0();
62 bool IPutOnThis();
63
64 private:
65 explicit Matcher(const DexFile::CodeItem* code_item)
66 : code_item_(code_item),
67 instruction_(Instruction::At(code_item->insns_)),
68 pos_(0u),
69 mark_(0u) { }
70
71 static bool DoMatch(const DexFile::CodeItem* code_item, MatchFn* const* pattern, size_t size);
72
73 const DexFile::CodeItem* const code_item_;
74 const Instruction* instruction_;
75 size_t pos_;
76 size_t mark_;
77};
78
79template <size_t size>
80bool Matcher::Match(const DexFile::CodeItem* code_item, MatchFn* const (&pattern)[size]) {
81 return DoMatch(code_item, pattern, size);
82}
83
84bool Matcher::Mark(Matcher* matcher) {
85 matcher->pos_ += 1u; // Advance to the next match function before marking.
86 matcher->mark_ = matcher->pos_;
87 return true;
88}
89
90template <bool (Matcher::*Fn)()>
91bool Matcher::Required(Matcher* matcher) {
92 if (!(matcher->*Fn)()) {
93 return false;
94 }
95 matcher->pos_ += 1u;
96 matcher->instruction_ = matcher->instruction_->Next();
97 return true;
98}
99
100template <bool (Matcher::*Fn)()>
101bool Matcher::Repeated(Matcher* matcher) {
102 if (!(matcher->*Fn)()) {
103 // Didn't match optional instruction, try the next match function.
104 matcher->pos_ += 1u;
105 return true;
106 }
107 matcher->pos_ = matcher->mark_;
108 matcher->instruction_ = matcher->instruction_->Next();
109 return true;
110}
111
112template <Instruction::Code opcode>
113bool Matcher::Opcode() {
114 return instruction_->Opcode() == opcode;
115}
116
117// Match const 0.
118bool Matcher::Const0() {
119 return IsInstructionDirectConst(instruction_->Opcode()) &&
120 (instruction_->Opcode() == Instruction::CONST_WIDE ? instruction_->VRegB_51l() == 0
121 : instruction_->VRegB() == 0);
122}
123
124bool Matcher::IPutOnThis() {
125 DCHECK_NE(code_item_->ins_size_, 0u);
126 return IsInstructionIPut(instruction_->Opcode()) &&
127 instruction_->VRegB_22c() == code_item_->registers_size_ - code_item_->ins_size_;
128}
129
130bool Matcher::DoMatch(const DexFile::CodeItem* code_item, MatchFn* const* pattern, size_t size) {
131 Matcher matcher(code_item);
132 while (matcher.pos_ != size) {
133 if (!pattern[matcher.pos_](&matcher)) {
134 return false;
135 }
136 }
137 return true;
138}
139
140// Used for a single invoke in a constructor. In that situation, the method verifier makes
141// sure we invoke a constructor either in the same class or superclass with at least "this".
142ArtMethod* GetTargetConstructor(ArtMethod* method, const Instruction* invoke_direct)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000144 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
145 DCHECK_EQ(invoke_direct->VRegC_35c(),
146 method->GetCodeItem()->registers_size_ - method->GetCodeItem()->ins_size_);
147 uint32_t method_index = invoke_direct->VRegB_35c();
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100148 ArtMethod* target_method = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
149 method_index, method->GetDexCache(), method->GetClassLoader());
Vladimir Marko354efa62016-02-04 19:46:56 +0000150 if (kIsDebugBuild && target_method != nullptr) {
151 CHECK(!target_method->IsStatic());
152 CHECK(target_method->IsConstructor());
153 CHECK(target_method->GetDeclaringClass() == method->GetDeclaringClass() ||
154 target_method->GetDeclaringClass() == method->GetDeclaringClass()->GetSuperClass());
155 }
156 return target_method;
157}
158
159// Return the forwarded arguments and check that all remaining arguments are zero.
160// If the check fails, return static_cast<size_t>(-1).
161size_t CountForwardedConstructorArguments(const DexFile::CodeItem* code_item,
162 const Instruction* invoke_direct,
163 uint16_t zero_vreg_mask) {
164 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
165 size_t number_of_args = invoke_direct->VRegA_35c();
166 DCHECK_NE(number_of_args, 0u);
167 uint32_t args[Instruction::kMaxVarArgRegs];
168 invoke_direct->GetVarArgs(args);
169 uint16_t this_vreg = args[0];
170 DCHECK_EQ(this_vreg, code_item->registers_size_ - code_item->ins_size_); // Checked by verifier.
171 size_t forwarded = 1u;
172 while (forwarded < number_of_args &&
173 args[forwarded] == this_vreg + forwarded &&
174 (zero_vreg_mask & (1u << args[forwarded])) == 0) {
175 ++forwarded;
176 }
177 for (size_t i = forwarded; i != number_of_args; ++i) {
178 if ((zero_vreg_mask & (1u << args[i])) == 0) {
179 return static_cast<size_t>(-1);
180 }
181 }
182 return forwarded;
183}
184
185uint16_t GetZeroVRegMask(const Instruction* const0) {
186 DCHECK(IsInstructionDirectConst(const0->Opcode()));
187 DCHECK((const0->Opcode() == Instruction::CONST_WIDE) ? const0->VRegB_51l() == 0u
188 : const0->VRegB() == 0);
189 uint16_t base_mask = IsInstructionConstWide(const0->Opcode()) ? 3u : 1u;
190 return base_mask << const0->VRegA();
191}
192
193// We limit the number of IPUTs storing parameters. There can be any number
194// of IPUTs that store the value 0 as they are useless in a constructor as
195// the object always starts zero-initialized. We also eliminate all but the
196// last store to any field as they are not observable; not even if the field
197// is volatile as no reference to the object can escape from a constructor
198// with this pattern.
199static constexpr size_t kMaxConstructorIPuts = 3u;
200
201struct ConstructorIPutData {
202 ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { }
203
204 uint16_t field_index;
205 uint16_t arg;
206};
207
208bool RecordConstructorIPut(ArtMethod* method,
209 const Instruction* new_iput,
210 uint16_t this_vreg,
211 uint16_t zero_vreg_mask,
212 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000214 DCHECK(IsInstructionIPut(new_iput->Opcode()));
215 uint32_t field_index = new_iput->VRegC_22c();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000216 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
217 ArtField* field = class_linker->LookupResolvedField(field_index, method, /* is_static */ false);
Vladimir Marko354efa62016-02-04 19:46:56 +0000218 if (UNLIKELY(field == nullptr)) {
219 return false;
220 }
221 // Remove previous IPUT to the same field, if any. Different field indexes may refer
222 // to the same field, so we need to compare resolved fields from the dex cache.
223 for (size_t old_pos = 0; old_pos != arraysize(iputs); ++old_pos) {
224 if (iputs[old_pos].field_index == DexFile::kDexNoIndex16) {
225 break;
226 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000227 ArtField* f = class_linker->LookupResolvedField(iputs[old_pos].field_index,
228 method,
229 /* is_static */ false);
Vladimir Marko354efa62016-02-04 19:46:56 +0000230 DCHECK(f != nullptr);
231 if (f == field) {
232 auto back_it = std::copy(iputs + old_pos + 1, iputs + arraysize(iputs), iputs + old_pos);
233 *back_it = ConstructorIPutData();
234 break;
235 }
236 }
237 // If the stored value isn't zero, record the IPUT.
238 if ((zero_vreg_mask & (1u << new_iput->VRegA_22c())) == 0u) {
239 size_t new_pos = 0;
240 while (new_pos != arraysize(iputs) && iputs[new_pos].field_index != DexFile::kDexNoIndex16) {
241 ++new_pos;
242 }
243 if (new_pos == arraysize(iputs)) {
244 return false; // Exceeded capacity of the output array.
245 }
246 iputs[new_pos].field_index = field_index;
247 iputs[new_pos].arg = new_iput->VRegA_22c() - this_vreg;
248 }
249 return true;
250}
251
252bool DoAnalyseConstructor(const DexFile::CodeItem* code_item,
253 ArtMethod* method,
254 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000256 // On entry we should not have any IPUTs yet.
257 DCHECK_EQ(0, std::count_if(
258 iputs,
259 iputs + arraysize(iputs),
260 [](const ConstructorIPutData& iput_data) {
261 return iput_data.field_index != DexFile::kDexNoIndex16;
262 }));
263
264 // Limit the maximum number of code units we're willing to match.
265 static constexpr size_t kMaxCodeUnits = 16u;
266
267 // Limit the number of registers that the constructor may use to 16.
268 // Given that IPUTs must use low 16 registers and we do not match MOVEs,
269 // this is a reasonable limitation.
270 static constexpr size_t kMaxVRegs = 16u;
271
272 // We try to match a constructor that calls another constructor (either in
273 // superclass or in the same class) with the same parameters, or with some
274 // parameters truncated (allowed only for calls to superclass constructor)
275 // or with extra parameters with value 0 (with any type, including null).
276 // This call can be followed by optional IPUTs on "this" storing either one
277 // of the parameters or 0 and the code must then finish with RETURN_VOID.
278 // The called constructor must be either java.lang.Object.<init>() or it
279 // must also match the same pattern.
280 static Matcher::MatchFn* const kConstructorPattern[] = {
281 &Matcher::Mark,
282 &Matcher::Repeated<&Matcher::Const0>,
283 &Matcher::Required<&Matcher::Opcode<Instruction::INVOKE_DIRECT>>,
284 &Matcher::Mark,
285 &Matcher::Repeated<&Matcher::Const0>,
286 &Matcher::Repeated<&Matcher::IPutOnThis>,
287 &Matcher::Required<&Matcher::Opcode<Instruction::RETURN_VOID>>,
288 };
289
290 DCHECK(method != nullptr);
291 DCHECK(!method->IsStatic());
292 DCHECK(method->IsConstructor());
293 DCHECK(code_item != nullptr);
294 if (!method->GetDeclaringClass()->IsVerified() ||
295 code_item->insns_size_in_code_units_ > kMaxCodeUnits ||
296 code_item->registers_size_ > kMaxVRegs ||
297 !Matcher::Match(code_item, kConstructorPattern)) {
298 return false;
299 }
300
301 // Verify the invoke, prevent a few odd cases and collect IPUTs.
302 uint16_t this_vreg = code_item->registers_size_ - code_item->ins_size_;
303 uint16_t zero_vreg_mask = 0u;
304 for (const Instruction* instruction = Instruction::At(code_item->insns_);
305 instruction->Opcode() != Instruction::RETURN_VOID;
306 instruction = instruction->Next()) {
307 if (instruction->Opcode() == Instruction::INVOKE_DIRECT) {
308 ArtMethod* target_method = GetTargetConstructor(method, instruction);
309 if (target_method == nullptr) {
310 return false;
311 }
312 // We allow forwarding constructors only if they pass more arguments
313 // to prevent infinite recursion.
314 if (target_method->GetDeclaringClass() == method->GetDeclaringClass() &&
315 instruction->VRegA_35c() <= code_item->ins_size_) {
316 return false;
317 }
318 size_t forwarded = CountForwardedConstructorArguments(code_item, instruction, zero_vreg_mask);
319 if (forwarded == static_cast<size_t>(-1)) {
320 return false;
321 }
322 if (target_method->GetDeclaringClass()->IsObjectClass()) {
Nicolas Geoffray141de8c2016-03-23 08:22:12 +0000323 DCHECK_EQ(Instruction::At(target_method->GetCodeItem()->insns_)->Opcode(),
324 Instruction::RETURN_VOID);
Vladimir Marko354efa62016-02-04 19:46:56 +0000325 } else {
326 const DexFile::CodeItem* target_code_item = target_method->GetCodeItem();
327 if (target_code_item == nullptr) {
328 return false; // Native constructor?
329 }
330 if (!DoAnalyseConstructor(target_code_item, target_method, iputs)) {
331 return false;
332 }
333 // Prune IPUTs with zero input.
334 auto kept_end = std::remove_if(
335 iputs,
336 iputs + arraysize(iputs),
337 [forwarded](const ConstructorIPutData& iput_data) {
338 return iput_data.arg >= forwarded;
339 });
340 std::fill(kept_end, iputs + arraysize(iputs), ConstructorIPutData());
341 // If we have any IPUTs from the call, check that the target method is in the same
342 // dex file (compare DexCache references), otherwise field_indexes would be bogus.
343 if (iputs[0].field_index != DexFile::kDexNoIndex16 &&
344 target_method->GetDexCache() != method->GetDexCache()) {
345 return false;
346 }
347 }
348 } else if (IsInstructionDirectConst(instruction->Opcode())) {
349 zero_vreg_mask |= GetZeroVRegMask(instruction);
350 if ((zero_vreg_mask & (1u << this_vreg)) != 0u) {
351 return false; // Overwriting `this` is unsupported.
352 }
353 } else {
354 DCHECK(IsInstructionIPut(instruction->Opcode()));
355 DCHECK_EQ(instruction->VRegB_22c(), this_vreg);
356 if (!RecordConstructorIPut(method, instruction, this_vreg, zero_vreg_mask, iputs)) {
357 return false;
358 }
359 }
360 }
361 return true;
362}
363
364} // anonymous namespace
365
366bool AnalyseConstructor(const DexFile::CodeItem* code_item,
367 ArtMethod* method,
368 InlineMethod* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700369 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000370 ConstructorIPutData iputs[kMaxConstructorIPuts];
371 if (!DoAnalyseConstructor(code_item, method, iputs)) {
372 return false;
373 }
374 static_assert(kMaxConstructorIPuts == 3, "Unexpected limit"); // Code below depends on this.
375 DCHECK(iputs[0].field_index != DexFile::kDexNoIndex16 ||
376 iputs[1].field_index == DexFile::kDexNoIndex16);
377 DCHECK(iputs[1].field_index != DexFile::kDexNoIndex16 ||
378 iputs[2].field_index == DexFile::kDexNoIndex16);
379
380#define STORE_IPUT(n) \
381 do { \
382 result->d.constructor_data.iput##n##_field_index = iputs[n].field_index; \
383 result->d.constructor_data.iput##n##_arg = iputs[n].arg; \
384 } while (false)
385
386 STORE_IPUT(0);
387 STORE_IPUT(1);
388 STORE_IPUT(2);
389#undef STORE_IPUT
390
391 result->opcode = kInlineOpConstructor;
Vladimir Marko354efa62016-02-04 19:46:56 +0000392 result->d.constructor_data.reserved = 0u;
393 return true;
394}
395
Andreas Gampe575e78c2014-11-03 23:41:03 -0800396static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
397static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
398static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
399 "iget_object type");
400static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
401 "iget_boolean type");
402static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
403static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
404static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
405static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
406static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
407static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
408 "iput_object type");
409static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
410 "iput_boolean type");
411static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
412static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
413static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
414static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
415 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
416static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
417 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
418static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
419 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
420static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
421 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
422static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
423 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
424static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
425 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
426static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
427 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000428
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000429bool InlineMethodAnalyser::AnalyseMethodCode(ArtMethod* method, InlineMethod* result) {
430 const DexFile::CodeItem* code_item = method->GetCodeItem();
431 if (code_item == nullptr) {
432 // Native or abstract.
433 return false;
434 }
Andreas Gampe5d08fcc2017-06-05 17:56:46 -0700435 return AnalyseMethodCode(code_item,
436 MethodReference(method->GetDexFile(), method->GetDexMethodIndex()),
437 method->IsStatic(),
438 method,
439 result);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000440}
441
442bool InlineMethodAnalyser::AnalyseMethodCode(const DexFile::CodeItem* code_item,
443 const MethodReference& method_ref,
444 bool is_static,
445 ArtMethod* method,
446 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000447 // We currently support only plain return or 2-instruction methods.
448
Vladimir Markoe3e02602014-03-12 15:42:41 +0000449 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
450 const Instruction* instruction = Instruction::At(code_item->insns_);
451 Instruction::Code opcode = instruction->Opcode();
452
453 switch (opcode) {
454 case Instruction::RETURN_VOID:
Vladimir Marko9f35ccd2016-02-02 20:12:32 +0000455 if (result != nullptr) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000456 result->opcode = kInlineOpNop;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000457 result->d.data = 0u;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100458 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000459 return true;
460 case Instruction::RETURN:
461 case Instruction::RETURN_OBJECT:
462 case Instruction::RETURN_WIDE:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000463 return AnalyseReturnMethod(code_item, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000464 case Instruction::CONST:
465 case Instruction::CONST_4:
466 case Instruction::CONST_16:
467 case Instruction::CONST_HIGH16:
468 // TODO: Support wide constants (RETURN_WIDE).
Vladimir Marko354efa62016-02-04 19:46:56 +0000469 if (AnalyseConstMethod(code_item, result)) {
470 return true;
471 }
472 FALLTHROUGH_INTENDED;
473 case Instruction::CONST_WIDE:
474 case Instruction::CONST_WIDE_16:
475 case Instruction::CONST_WIDE_32:
476 case Instruction::CONST_WIDE_HIGH16:
477 case Instruction::INVOKE_DIRECT:
478 if (method != nullptr && !method->IsStatic() && method->IsConstructor()) {
479 return AnalyseConstructor(code_item, method, result);
480 }
481 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000482 case Instruction::IGET:
483 case Instruction::IGET_OBJECT:
484 case Instruction::IGET_BOOLEAN:
485 case Instruction::IGET_BYTE:
486 case Instruction::IGET_CHAR:
487 case Instruction::IGET_SHORT:
488 case Instruction::IGET_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800489 // TODO: Add handling for JIT.
490 // case Instruction::IGET_QUICK:
491 // case Instruction::IGET_WIDE_QUICK:
492 // case Instruction::IGET_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000493 return AnalyseIGetMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000494 case Instruction::IPUT:
495 case Instruction::IPUT_OBJECT:
496 case Instruction::IPUT_BOOLEAN:
497 case Instruction::IPUT_BYTE:
498 case Instruction::IPUT_CHAR:
499 case Instruction::IPUT_SHORT:
500 case Instruction::IPUT_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800501 // TODO: Add handling for JIT.
502 // case Instruction::IPUT_QUICK:
503 // case Instruction::IPUT_WIDE_QUICK:
504 // case Instruction::IPUT_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000505 return AnalyseIPutMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000506 default:
507 return false;
508 }
509}
510
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100511bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700512 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.index);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100513 const char* method_name = ref.dex_file->GetMethodName(method_id);
Vladimir Markod5f10052015-05-06 14:09:04 +0100514 // javac names synthetic accessors "access$nnn",
515 // jack names them "-getN", "-putN", "-wrapN".
516 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
517 strncmp(method_name, "-", strlen("-")) == 0;
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100518}
519
Vladimir Markoe3e02602014-03-12 15:42:41 +0000520bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
521 InlineMethod* result) {
522 const Instruction* return_instruction = Instruction::At(code_item->insns_);
523 Instruction::Code return_opcode = return_instruction->Opcode();
524 uint32_t reg = return_instruction->VRegA_11x();
525 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
526 DCHECK_GE(reg, arg_start);
527 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
528 code_item->registers_size_);
529
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100530 if (result != nullptr) {
531 result->opcode = kInlineOpReturnArg;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100532 InlineReturnArgData* data = &result->d.return_data;
533 data->arg = reg - arg_start;
534 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
535 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
536 data->reserved = 0u;
537 data->reserved2 = 0u;
538 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000539 return true;
540}
541
542bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
543 InlineMethod* result) {
544 const Instruction* instruction = Instruction::At(code_item->insns_);
545 const Instruction* return_instruction = instruction->Next();
546 Instruction::Code return_opcode = return_instruction->Opcode();
547 if (return_opcode != Instruction::RETURN &&
548 return_opcode != Instruction::RETURN_OBJECT) {
549 return false;
550 }
551
Ian Rogers29a26482014-05-02 15:27:29 -0700552 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000553 DCHECK_LT(return_reg, code_item->registers_size_);
554
Ian Rogers29a26482014-05-02 15:27:29 -0700555 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000556 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700557 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000558 }
Ian Rogers29a26482014-05-02 15:27:29 -0700559 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
560 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000561 return false; // Not returning the value set by const?
562 }
Ian Rogers29a26482014-05-02 15:27:29 -0700563 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000564 return false; // Returning non-null reference constant?
565 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100566 if (result != nullptr) {
567 result->opcode = kInlineOpNonWideConst;
Ian Rogers29a26482014-05-02 15:27:29 -0700568 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100569 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000570 return true;
571}
572
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000573bool InlineMethodAnalyser::AnalyseIGetMethod(const DexFile::CodeItem* code_item,
574 const MethodReference& method_ref,
575 bool is_static,
576 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000577 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000578 const Instruction* instruction = Instruction::At(code_item->insns_);
579 Instruction::Code opcode = instruction->Opcode();
580 DCHECK(IsInstructionIGet(opcode));
581
582 const Instruction* return_instruction = instruction->Next();
583 Instruction::Code return_opcode = return_instruction->Opcode();
584 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
585 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
586 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
587 opcode != Instruction::IGET_OBJECT)) {
588 return false;
589 }
590
591 uint32_t return_reg = return_instruction->VRegA_11x();
592 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
593 code_item->registers_size_);
594
595 uint32_t dst_reg = instruction->VRegA_22c();
596 uint32_t object_reg = instruction->VRegB_22c();
597 uint32_t field_idx = instruction->VRegC_22c();
598 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
599 DCHECK_GE(object_reg, arg_start);
600 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100601 uint32_t object_arg = object_reg - arg_start;
602
Vladimir Markoe3e02602014-03-12 15:42:41 +0000603 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
604 if (dst_reg != return_reg) {
605 return false; // Not returning the value retrieved by IGET?
606 }
607
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000608 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100609 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
610 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000611 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100612 return false;
613 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000614 }
615
Vladimir Markoe1fced12014-04-04 14:52:53 +0100616 // InlineIGetIPutData::object_arg is only 4 bits wide.
617 static constexpr uint16_t kMaxObjectArg = 15u;
618 if (object_arg > kMaxObjectArg) {
619 return false;
620 }
621
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100622 if (result != nullptr) {
623 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000624 if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100625 return false;
626 }
627 result->opcode = kInlineOpIGet;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100628 data->op_variant = IGetVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000629 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100630 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100631 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100632 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000633 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000634 return true;
635}
636
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000637bool InlineMethodAnalyser::AnalyseIPutMethod(const DexFile::CodeItem* code_item,
638 const MethodReference& method_ref,
639 bool is_static,
640 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000641 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000642 const Instruction* instruction = Instruction::At(code_item->insns_);
643 Instruction::Code opcode = instruction->Opcode();
644 DCHECK(IsInstructionIPut(opcode));
645
646 const Instruction* return_instruction = instruction->Next();
647 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100648 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
649 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000650 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100651 if (return_opcode != Instruction::RETURN &&
652 return_opcode != Instruction::RETURN_OBJECT &&
653 return_opcode != Instruction::RETURN_WIDE) {
654 return false;
655 }
656 // Returning an argument.
657 uint32_t return_reg = return_instruction->VRegA_11x();
658 DCHECK_GE(return_reg, arg_start);
659 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
660 code_item->registers_size_);
661 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000662 }
663
664 uint32_t src_reg = instruction->VRegA_22c();
665 uint32_t object_reg = instruction->VRegB_22c();
666 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000667 DCHECK_GE(object_reg, arg_start);
668 DCHECK_LT(object_reg, code_item->registers_size_);
669 DCHECK_GE(src_reg, arg_start);
670 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100671 uint32_t object_arg = object_reg - arg_start;
672 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000673
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000674 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100675 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
676 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000677 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100678 return false;
679 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000680 }
681
Vladimir Markoe1fced12014-04-04 14:52:53 +0100682 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
683 static constexpr uint16_t kMaxObjectArg = 15u;
684 static constexpr uint16_t kMaxSrcArg = 15u;
685 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
686 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
687 return false;
688 }
689
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100690 if (result != nullptr) {
691 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000692 if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100693 return false;
694 }
695 result->opcode = kInlineOpIPut;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100696 data->op_variant = IPutVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000697 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100698 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
699 data->src_arg = src_arg;
700 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000701 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000702 return true;
703}
704
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000705bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method,
706 uint32_t field_idx,
707 bool is_put,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000708 InlineIGetIPutData* result) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000709 if (method == nullptr) {
710 return false;
711 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000712 ObjPtr<mirror::DexCache> dex_cache = method->GetDexCache();
713 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
714 ArtField* field = class_linker->LookupResolvedField(field_idx, method, /* is_static */ false);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000715 if (field == nullptr || field->IsStatic()) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000716 return false;
717 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700718 ObjPtr<mirror::Class> method_class = method->GetDeclaringClass();
719 ObjPtr<mirror::Class> field_class = field->GetDeclaringClass();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000720 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
721 (is_put && field->IsFinal() && method_class != field_class)) {
722 return false;
723 }
724 DCHECK_GE(field->GetOffset().Int32Value(), 0);
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000725 // Do not interleave function calls with bit field writes to placate valgrind. Bug: 27552451.
726 uint32_t field_offset = field->GetOffset().Uint32Value();
727 bool is_volatile = field->IsVolatile();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000728 result->field_idx = field_idx;
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000729 result->field_offset = field_offset;
730 result->is_volatile = is_volatile ? 1u : 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000731 return true;
732}
733
734} // namespace art