blob: 7636f845dba344582528562992891dc35c4ade20 [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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080022#include "dex_file-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000023#include "dex_instruction.h"
24#include "dex_instruction-inl.h"
Vladimir Marko354efa62016-02-04 19:46:56 +000025#include "dex_instruction_utils.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000026#include "mirror/class-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000027#include "mirror/dex_cache-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000028#include "verifier/method_verifier-inl.h"
29
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)
143 SHARED_REQUIRES(Locks::mutator_lock_) {
144 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();
148 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
149 ArtMethod* target_method =
150 method->GetDexCache()->GetResolvedMethod(method_index, pointer_size);
151 if (kIsDebugBuild && target_method != nullptr) {
152 CHECK(!target_method->IsStatic());
153 CHECK(target_method->IsConstructor());
154 CHECK(target_method->GetDeclaringClass() == method->GetDeclaringClass() ||
155 target_method->GetDeclaringClass() == method->GetDeclaringClass()->GetSuperClass());
156 }
157 return target_method;
158}
159
160// Return the forwarded arguments and check that all remaining arguments are zero.
161// If the check fails, return static_cast<size_t>(-1).
162size_t CountForwardedConstructorArguments(const DexFile::CodeItem* code_item,
163 const Instruction* invoke_direct,
164 uint16_t zero_vreg_mask) {
165 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
166 size_t number_of_args = invoke_direct->VRegA_35c();
167 DCHECK_NE(number_of_args, 0u);
168 uint32_t args[Instruction::kMaxVarArgRegs];
169 invoke_direct->GetVarArgs(args);
170 uint16_t this_vreg = args[0];
171 DCHECK_EQ(this_vreg, code_item->registers_size_ - code_item->ins_size_); // Checked by verifier.
172 size_t forwarded = 1u;
173 while (forwarded < number_of_args &&
174 args[forwarded] == this_vreg + forwarded &&
175 (zero_vreg_mask & (1u << args[forwarded])) == 0) {
176 ++forwarded;
177 }
178 for (size_t i = forwarded; i != number_of_args; ++i) {
179 if ((zero_vreg_mask & (1u << args[i])) == 0) {
180 return static_cast<size_t>(-1);
181 }
182 }
183 return forwarded;
184}
185
186uint16_t GetZeroVRegMask(const Instruction* const0) {
187 DCHECK(IsInstructionDirectConst(const0->Opcode()));
188 DCHECK((const0->Opcode() == Instruction::CONST_WIDE) ? const0->VRegB_51l() == 0u
189 : const0->VRegB() == 0);
190 uint16_t base_mask = IsInstructionConstWide(const0->Opcode()) ? 3u : 1u;
191 return base_mask << const0->VRegA();
192}
193
194// We limit the number of IPUTs storing parameters. There can be any number
195// of IPUTs that store the value 0 as they are useless in a constructor as
196// the object always starts zero-initialized. We also eliminate all but the
197// last store to any field as they are not observable; not even if the field
198// is volatile as no reference to the object can escape from a constructor
199// with this pattern.
200static constexpr size_t kMaxConstructorIPuts = 3u;
201
202struct ConstructorIPutData {
203 ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { }
204
205 uint16_t field_index;
206 uint16_t arg;
207};
208
209bool RecordConstructorIPut(ArtMethod* method,
210 const Instruction* new_iput,
211 uint16_t this_vreg,
212 uint16_t zero_vreg_mask,
213 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
214 SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsInstructionIPut(new_iput->Opcode()));
216 uint32_t field_index = new_iput->VRegC_22c();
217 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
218 mirror::DexCache* dex_cache = method->GetDexCache();
219 ArtField* field = dex_cache->GetResolvedField(field_index, pointer_size);
220 if (UNLIKELY(field == nullptr)) {
221 return false;
222 }
223 // Remove previous IPUT to the same field, if any. Different field indexes may refer
224 // to the same field, so we need to compare resolved fields from the dex cache.
225 for (size_t old_pos = 0; old_pos != arraysize(iputs); ++old_pos) {
226 if (iputs[old_pos].field_index == DexFile::kDexNoIndex16) {
227 break;
228 }
229 ArtField* f = dex_cache->GetResolvedField(iputs[old_pos].field_index, pointer_size);
230 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])
255 SHARED_REQUIRES(Locks::mutator_lock_) {
256 // 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()) {
Andreas Gampe1ee29d32016-03-22 22:03:46 -0700323 if (kIsDebugBuild) {
324 Instruction::Code op = Instruction::At(target_method->GetCodeItem()->insns_)->Opcode();
325 DCHECK(op == Instruction::RETURN_VOID || op == Instruction::RETURN_VOID_NO_BARRIER)
326 << op;
327 }
Vladimir Marko354efa62016-02-04 19:46:56 +0000328 } else {
329 const DexFile::CodeItem* target_code_item = target_method->GetCodeItem();
330 if (target_code_item == nullptr) {
331 return false; // Native constructor?
332 }
333 if (!DoAnalyseConstructor(target_code_item, target_method, iputs)) {
334 return false;
335 }
336 // Prune IPUTs with zero input.
337 auto kept_end = std::remove_if(
338 iputs,
339 iputs + arraysize(iputs),
340 [forwarded](const ConstructorIPutData& iput_data) {
341 return iput_data.arg >= forwarded;
342 });
343 std::fill(kept_end, iputs + arraysize(iputs), ConstructorIPutData());
344 // If we have any IPUTs from the call, check that the target method is in the same
345 // dex file (compare DexCache references), otherwise field_indexes would be bogus.
346 if (iputs[0].field_index != DexFile::kDexNoIndex16 &&
347 target_method->GetDexCache() != method->GetDexCache()) {
348 return false;
349 }
350 }
351 } else if (IsInstructionDirectConst(instruction->Opcode())) {
352 zero_vreg_mask |= GetZeroVRegMask(instruction);
353 if ((zero_vreg_mask & (1u << this_vreg)) != 0u) {
354 return false; // Overwriting `this` is unsupported.
355 }
356 } else {
357 DCHECK(IsInstructionIPut(instruction->Opcode()));
358 DCHECK_EQ(instruction->VRegB_22c(), this_vreg);
359 if (!RecordConstructorIPut(method, instruction, this_vreg, zero_vreg_mask, iputs)) {
360 return false;
361 }
362 }
363 }
364 return true;
365}
366
367} // anonymous namespace
368
369bool AnalyseConstructor(const DexFile::CodeItem* code_item,
370 ArtMethod* method,
371 InlineMethod* result)
372 SHARED_REQUIRES(Locks::mutator_lock_) {
373 ConstructorIPutData iputs[kMaxConstructorIPuts];
374 if (!DoAnalyseConstructor(code_item, method, iputs)) {
375 return false;
376 }
377 static_assert(kMaxConstructorIPuts == 3, "Unexpected limit"); // Code below depends on this.
378 DCHECK(iputs[0].field_index != DexFile::kDexNoIndex16 ||
379 iputs[1].field_index == DexFile::kDexNoIndex16);
380 DCHECK(iputs[1].field_index != DexFile::kDexNoIndex16 ||
381 iputs[2].field_index == DexFile::kDexNoIndex16);
382
383#define STORE_IPUT(n) \
384 do { \
385 result->d.constructor_data.iput##n##_field_index = iputs[n].field_index; \
386 result->d.constructor_data.iput##n##_arg = iputs[n].arg; \
387 } while (false)
388
389 STORE_IPUT(0);
390 STORE_IPUT(1);
391 STORE_IPUT(2);
392#undef STORE_IPUT
393
394 result->opcode = kInlineOpConstructor;
395 result->flags = kInlineSpecial;
396 result->d.constructor_data.reserved = 0u;
397 return true;
398}
399
Andreas Gampe575e78c2014-11-03 23:41:03 -0800400static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type");
401static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type");
402static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT),
403 "iget_object type");
404static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN),
405 "iget_boolean type");
406static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type");
407static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type");
408static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type");
409static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type");
410static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type");
411static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT),
412 "iput_object type");
413static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN),
414 "iput_boolean type");
415static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type");
416static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type");
417static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type");
418static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) ==
419 InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant");
420static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) ==
421 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant");
422static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) ==
423 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant");
424static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) ==
425 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant");
426static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) ==
427 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant");
428static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) ==
429 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant");
430static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) ==
431 InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant");
Vladimir Markoe3e02602014-03-12 15:42:41 +0000432
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100433// This is used by compiler and debugger. We look into the dex cache for resolved methods and
434// fields. However, in the context of the debugger, not all methods and fields are resolved. Since
435// we need to be able to detect possibly inlined method, we pass a null inline method to indicate
436// we don't want to take unresolved methods and fields into account during analysis.
Vladimir Markoe3e02602014-03-12 15:42:41 +0000437bool InlineMethodAnalyser::AnalyseMethodCode(verifier::MethodVerifier* verifier,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000438 InlineMethod* result) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100439 DCHECK(verifier != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800440 if (!Runtime::Current()->UseJit()) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000441 DCHECK_EQ(verifier->CanLoadClasses(), result != nullptr);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800442 }
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000443
444 // Note: verifier->GetMethod() may be null.
445 return AnalyseMethodCode(verifier->CodeItem(),
446 verifier->GetMethodReference(),
447 (verifier->GetAccessFlags() & kAccStatic) != 0u,
448 verifier->GetMethod(),
449 result);
450}
451
452bool InlineMethodAnalyser::AnalyseMethodCode(ArtMethod* method, InlineMethod* result) {
453 const DexFile::CodeItem* code_item = method->GetCodeItem();
454 if (code_item == nullptr) {
455 // Native or abstract.
456 return false;
457 }
458 return AnalyseMethodCode(
459 code_item, method->ToMethodReference(), method->IsStatic(), method, result);
460}
461
462bool InlineMethodAnalyser::AnalyseMethodCode(const DexFile::CodeItem* code_item,
463 const MethodReference& method_ref,
464 bool is_static,
465 ArtMethod* method,
466 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000467 // We currently support only plain return or 2-instruction methods.
468
Vladimir Markoe3e02602014-03-12 15:42:41 +0000469 DCHECK_NE(code_item->insns_size_in_code_units_, 0u);
470 const Instruction* instruction = Instruction::At(code_item->insns_);
471 Instruction::Code opcode = instruction->Opcode();
472
473 switch (opcode) {
474 case Instruction::RETURN_VOID:
Vladimir Marko9f35ccd2016-02-02 20:12:32 +0000475 if (result != nullptr) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000476 result->opcode = kInlineOpNop;
477 result->flags = kInlineSpecial;
478 result->d.data = 0u;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100479 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000480 return true;
481 case Instruction::RETURN:
482 case Instruction::RETURN_OBJECT:
483 case Instruction::RETURN_WIDE:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000484 return AnalyseReturnMethod(code_item, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000485 case Instruction::CONST:
486 case Instruction::CONST_4:
487 case Instruction::CONST_16:
488 case Instruction::CONST_HIGH16:
489 // TODO: Support wide constants (RETURN_WIDE).
Vladimir Marko354efa62016-02-04 19:46:56 +0000490 if (AnalyseConstMethod(code_item, result)) {
491 return true;
492 }
493 FALLTHROUGH_INTENDED;
494 case Instruction::CONST_WIDE:
495 case Instruction::CONST_WIDE_16:
496 case Instruction::CONST_WIDE_32:
497 case Instruction::CONST_WIDE_HIGH16:
498 case Instruction::INVOKE_DIRECT:
499 if (method != nullptr && !method->IsStatic() && method->IsConstructor()) {
500 return AnalyseConstructor(code_item, method, result);
501 }
502 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000503 case Instruction::IGET:
504 case Instruction::IGET_OBJECT:
505 case Instruction::IGET_BOOLEAN:
506 case Instruction::IGET_BYTE:
507 case Instruction::IGET_CHAR:
508 case Instruction::IGET_SHORT:
509 case Instruction::IGET_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800510 // TODO: Add handling for JIT.
511 // case Instruction::IGET_QUICK:
512 // case Instruction::IGET_WIDE_QUICK:
513 // case Instruction::IGET_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000514 return AnalyseIGetMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000515 case Instruction::IPUT:
516 case Instruction::IPUT_OBJECT:
517 case Instruction::IPUT_BOOLEAN:
518 case Instruction::IPUT_BYTE:
519 case Instruction::IPUT_CHAR:
520 case Instruction::IPUT_SHORT:
521 case Instruction::IPUT_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800522 // TODO: Add handling for JIT.
523 // case Instruction::IPUT_QUICK:
524 // case Instruction::IPUT_WIDE_QUICK:
525 // case Instruction::IPUT_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000526 return AnalyseIPutMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000527 default:
528 return false;
529 }
530}
531
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100532bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
533 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index);
534 const char* method_name = ref.dex_file->GetMethodName(method_id);
Vladimir Markod5f10052015-05-06 14:09:04 +0100535 // javac names synthetic accessors "access$nnn",
536 // jack names them "-getN", "-putN", "-wrapN".
537 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
538 strncmp(method_name, "-", strlen("-")) == 0;
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100539}
540
Vladimir Markoe3e02602014-03-12 15:42:41 +0000541bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item,
542 InlineMethod* result) {
543 const Instruction* return_instruction = Instruction::At(code_item->insns_);
544 Instruction::Code return_opcode = return_instruction->Opcode();
545 uint32_t reg = return_instruction->VRegA_11x();
546 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
547 DCHECK_GE(reg, arg_start);
548 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
549 code_item->registers_size_);
550
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100551 if (result != nullptr) {
552 result->opcode = kInlineOpReturnArg;
553 result->flags = kInlineSpecial;
554 InlineReturnArgData* data = &result->d.return_data;
555 data->arg = reg - arg_start;
556 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
557 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
558 data->reserved = 0u;
559 data->reserved2 = 0u;
560 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000561 return true;
562}
563
564bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item,
565 InlineMethod* result) {
566 const Instruction* instruction = Instruction::At(code_item->insns_);
567 const Instruction* return_instruction = instruction->Next();
568 Instruction::Code return_opcode = return_instruction->Opcode();
569 if (return_opcode != Instruction::RETURN &&
570 return_opcode != Instruction::RETURN_OBJECT) {
571 return false;
572 }
573
Ian Rogers29a26482014-05-02 15:27:29 -0700574 int32_t return_reg = return_instruction->VRegA_11x();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000575 DCHECK_LT(return_reg, code_item->registers_size_);
576
Ian Rogers29a26482014-05-02 15:27:29 -0700577 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000578 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700579 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000580 }
Ian Rogers29a26482014-05-02 15:27:29 -0700581 DCHECK_LT(instruction->VRegA(), code_item->registers_size_);
582 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000583 return false; // Not returning the value set by const?
584 }
Ian Rogers29a26482014-05-02 15:27:29 -0700585 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000586 return false; // Returning non-null reference constant?
587 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100588 if (result != nullptr) {
589 result->opcode = kInlineOpNonWideConst;
590 result->flags = kInlineSpecial;
Ian Rogers29a26482014-05-02 15:27:29 -0700591 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100592 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000593 return true;
594}
595
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000596bool InlineMethodAnalyser::AnalyseIGetMethod(const DexFile::CodeItem* code_item,
597 const MethodReference& method_ref,
598 bool is_static,
599 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000600 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000601 const Instruction* instruction = Instruction::At(code_item->insns_);
602 Instruction::Code opcode = instruction->Opcode();
603 DCHECK(IsInstructionIGet(opcode));
604
605 const Instruction* return_instruction = instruction->Next();
606 Instruction::Code return_opcode = return_instruction->Opcode();
607 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
608 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
609 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
610 opcode != Instruction::IGET_OBJECT)) {
611 return false;
612 }
613
614 uint32_t return_reg = return_instruction->VRegA_11x();
615 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
616 code_item->registers_size_);
617
618 uint32_t dst_reg = instruction->VRegA_22c();
619 uint32_t object_reg = instruction->VRegB_22c();
620 uint32_t field_idx = instruction->VRegC_22c();
621 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
622 DCHECK_GE(object_reg, arg_start);
623 DCHECK_LT(object_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100624 uint32_t object_arg = object_reg - arg_start;
625
Vladimir Markoe3e02602014-03-12 15:42:41 +0000626 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_);
627 if (dst_reg != return_reg) {
628 return false; // Not returning the value retrieved by IGET?
629 }
630
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000631 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100632 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
633 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000634 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100635 return false;
636 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000637 }
638
Vladimir Markoe1fced12014-04-04 14:52:53 +0100639 // InlineIGetIPutData::object_arg is only 4 bits wide.
640 static constexpr uint16_t kMaxObjectArg = 15u;
641 if (object_arg > kMaxObjectArg) {
642 return false;
643 }
644
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100645 if (result != nullptr) {
646 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000647 if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100648 return false;
649 }
650 result->opcode = kInlineOpIGet;
651 result->flags = kInlineSpecial;
652 data->op_variant = IGetVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000653 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100654 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100655 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100656 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000657 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000658 return true;
659}
660
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000661bool InlineMethodAnalyser::AnalyseIPutMethod(const DexFile::CodeItem* code_item,
662 const MethodReference& method_ref,
663 bool is_static,
664 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000665 InlineMethod* result) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000666 const Instruction* instruction = Instruction::At(code_item->insns_);
667 Instruction::Code opcode = instruction->Opcode();
668 DCHECK(IsInstructionIPut(opcode));
669
670 const Instruction* return_instruction = instruction->Next();
671 Instruction::Code return_opcode = return_instruction->Opcode();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100672 uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_;
673 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000674 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100675 if (return_opcode != Instruction::RETURN &&
676 return_opcode != Instruction::RETURN_OBJECT &&
677 return_opcode != Instruction::RETURN_WIDE) {
678 return false;
679 }
680 // Returning an argument.
681 uint32_t return_reg = return_instruction->VRegA_11x();
682 DCHECK_GE(return_reg, arg_start);
683 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
684 code_item->registers_size_);
685 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000686 }
687
688 uint32_t src_reg = instruction->VRegA_22c();
689 uint32_t object_reg = instruction->VRegB_22c();
690 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000691 DCHECK_GE(object_reg, arg_start);
692 DCHECK_LT(object_reg, code_item->registers_size_);
693 DCHECK_GE(src_reg, arg_start);
694 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_);
Vladimir Markoe1fced12014-04-04 14:52:53 +0100695 uint32_t object_arg = object_reg - arg_start;
696 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000697
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000698 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100699 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
700 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000701 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100702 return false;
703 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000704 }
705
Vladimir Markoe1fced12014-04-04 14:52:53 +0100706 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
707 static constexpr uint16_t kMaxObjectArg = 15u;
708 static constexpr uint16_t kMaxSrcArg = 15u;
709 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
710 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
711 return false;
712 }
713
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100714 if (result != nullptr) {
715 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000716 if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100717 return false;
718 }
719 result->opcode = kInlineOpIPut;
720 result->flags = kInlineSpecial;
721 data->op_variant = IPutVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000722 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100723 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
724 data->src_arg = src_arg;
725 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000726 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000727 return true;
728}
729
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000730bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method,
731 uint32_t field_idx,
732 bool is_put,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000733 InlineIGetIPutData* result) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000734 if (method == nullptr) {
735 return false;
736 }
737 mirror::DexCache* dex_cache = method->GetDexCache();
738 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
739 ArtField* field = dex_cache->GetResolvedField(field_idx, pointer_size);
740 if (field == nullptr || field->IsStatic()) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000741 return false;
742 }
743 mirror::Class* method_class = method->GetDeclaringClass();
744 mirror::Class* field_class = field->GetDeclaringClass();
745 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
746 (is_put && field->IsFinal() && method_class != field_class)) {
747 return false;
748 }
749 DCHECK_GE(field->GetOffset().Int32Value(), 0);
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000750 // Do not interleave function calls with bit field writes to placate valgrind. Bug: 27552451.
751 uint32_t field_offset = field->GetOffset().Uint32Value();
752 bool is_volatile = field->IsVolatile();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000753 result->field_idx = field_idx;
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000754 result->field_offset = field_offset;
755 result->is_volatile = is_volatile ? 1u : 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000756 return true;
757}
758
759} // namespace art