blob: b409eb2dbbee42631012afdf3f80fd423333466b [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"
Mathieu Chartier69147f12017-11-06 20:02:24 -080023#include "code_item_accessors-inl.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080024#include "dex_file-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000025#include "dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "dex_instruction.h"
Vladimir Marko354efa62016-02-04 19:46:56 +000027#include "dex_instruction_utils.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000028#include "mirror/class-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000029#include "mirror/dex_cache-inl.h"
Vladimir Markoe3e02602014-03-12 15:42:41 +000030
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>
Mathieu Chartier69147f12017-11-06 20:02:24 -080047 static bool Match(const CodeItemDataAccessor* code_item, MatchFn* const (&pattern)[size]);
Vladimir Marko354efa62016-02-04 19:46:56 +000048
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:
Mathieu Chartier69147f12017-11-06 20:02:24 -080066 explicit Matcher(const CodeItemDataAccessor* code_item)
Vladimir Marko354efa62016-02-04 19:46:56 +000067 : code_item_(code_item),
Mathieu Chartier69147f12017-11-06 20:02:24 -080068 instruction_(code_item->begin()) {}
Vladimir Marko354efa62016-02-04 19:46:56 +000069
Mathieu Chartier69147f12017-11-06 20:02:24 -080070 static bool DoMatch(const CodeItemDataAccessor* code_item, MatchFn* const* pattern, size_t size);
Vladimir Marko354efa62016-02-04 19:46:56 +000071
Mathieu Chartier69147f12017-11-06 20:02:24 -080072 const CodeItemDataAccessor* const code_item_;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070073 DexInstructionIterator instruction_;
Mathieu Chartier69147f12017-11-06 20:02:24 -080074 size_t pos_ = 0u;
75 size_t mark_ = 0u;
Vladimir Marko354efa62016-02-04 19:46:56 +000076};
77
78template <size_t size>
Mathieu Chartier69147f12017-11-06 20:02:24 -080079bool Matcher::Match(const CodeItemDataAccessor* code_item, MatchFn* const (&pattern)[size]) {
Vladimir Marko354efa62016-02-04 19:46:56 +000080 return DoMatch(code_item, pattern, size);
81}
82
83bool Matcher::Mark(Matcher* matcher) {
84 matcher->pos_ += 1u; // Advance to the next match function before marking.
85 matcher->mark_ = matcher->pos_;
86 return true;
87}
88
89template <bool (Matcher::*Fn)()>
90bool Matcher::Required(Matcher* matcher) {
91 if (!(matcher->*Fn)()) {
92 return false;
93 }
94 matcher->pos_ += 1u;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -070095 ++matcher->instruction_;
Vladimir Marko354efa62016-02-04 19:46:56 +000096 return true;
97}
98
99template <bool (Matcher::*Fn)()>
100bool Matcher::Repeated(Matcher* matcher) {
101 if (!(matcher->*Fn)()) {
102 // Didn't match optional instruction, try the next match function.
103 matcher->pos_ += 1u;
104 return true;
105 }
106 matcher->pos_ = matcher->mark_;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700107 ++matcher->instruction_;
Vladimir Marko354efa62016-02-04 19:46:56 +0000108 return true;
109}
110
111template <Instruction::Code opcode>
112bool Matcher::Opcode() {
113 return instruction_->Opcode() == opcode;
114}
115
116// Match const 0.
117bool Matcher::Const0() {
118 return IsInstructionDirectConst(instruction_->Opcode()) &&
119 (instruction_->Opcode() == Instruction::CONST_WIDE ? instruction_->VRegB_51l() == 0
120 : instruction_->VRegB() == 0);
121}
122
123bool Matcher::IPutOnThis() {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800124 DCHECK_NE(code_item_->InsSize(), 0u);
Vladimir Marko354efa62016-02-04 19:46:56 +0000125 return IsInstructionIPut(instruction_->Opcode()) &&
Mathieu Chartier69147f12017-11-06 20:02:24 -0800126 instruction_->VRegB_22c() == code_item_->RegistersSize() - code_item_->InsSize();
Vladimir Marko354efa62016-02-04 19:46:56 +0000127}
128
Mathieu Chartier69147f12017-11-06 20:02:24 -0800129bool Matcher::DoMatch(const CodeItemDataAccessor* code_item, MatchFn* const* pattern, size_t size) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000130 Matcher matcher(code_item);
131 while (matcher.pos_ != size) {
132 if (!pattern[matcher.pos_](&matcher)) {
133 return false;
134 }
135 }
136 return true;
137}
138
139// Used for a single invoke in a constructor. In that situation, the method verifier makes
140// sure we invoke a constructor either in the same class or superclass with at least "this".
141ArtMethod* GetTargetConstructor(ArtMethod* method, const Instruction* invoke_direct)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000143 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
144 DCHECK_EQ(invoke_direct->VRegC_35c(),
145 method->GetCodeItem()->registers_size_ - method->GetCodeItem()->ins_size_);
146 uint32_t method_index = invoke_direct->VRegB_35c();
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100147 ArtMethod* target_method = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
148 method_index, method->GetDexCache(), method->GetClassLoader());
Vladimir Marko354efa62016-02-04 19:46:56 +0000149 if (kIsDebugBuild && target_method != nullptr) {
150 CHECK(!target_method->IsStatic());
151 CHECK(target_method->IsConstructor());
152 CHECK(target_method->GetDeclaringClass() == method->GetDeclaringClass() ||
153 target_method->GetDeclaringClass() == method->GetDeclaringClass()->GetSuperClass());
154 }
155 return target_method;
156}
157
158// Return the forwarded arguments and check that all remaining arguments are zero.
159// If the check fails, return static_cast<size_t>(-1).
Mathieu Chartier69147f12017-11-06 20:02:24 -0800160size_t CountForwardedConstructorArguments(const CodeItemDataAccessor* code_item,
Vladimir Marko354efa62016-02-04 19:46:56 +0000161 const Instruction* invoke_direct,
162 uint16_t zero_vreg_mask) {
163 DCHECK_EQ(invoke_direct->Opcode(), Instruction::INVOKE_DIRECT);
164 size_t number_of_args = invoke_direct->VRegA_35c();
165 DCHECK_NE(number_of_args, 0u);
166 uint32_t args[Instruction::kMaxVarArgRegs];
167 invoke_direct->GetVarArgs(args);
168 uint16_t this_vreg = args[0];
Mathieu Chartier69147f12017-11-06 20:02:24 -0800169 DCHECK_EQ(this_vreg, code_item->RegistersSize() - code_item->InsSize()); // Checked by verifier.
Vladimir Marko354efa62016-02-04 19:46:56 +0000170 size_t forwarded = 1u;
171 while (forwarded < number_of_args &&
172 args[forwarded] == this_vreg + forwarded &&
173 (zero_vreg_mask & (1u << args[forwarded])) == 0) {
174 ++forwarded;
175 }
176 for (size_t i = forwarded; i != number_of_args; ++i) {
177 if ((zero_vreg_mask & (1u << args[i])) == 0) {
178 return static_cast<size_t>(-1);
179 }
180 }
181 return forwarded;
182}
183
184uint16_t GetZeroVRegMask(const Instruction* const0) {
185 DCHECK(IsInstructionDirectConst(const0->Opcode()));
186 DCHECK((const0->Opcode() == Instruction::CONST_WIDE) ? const0->VRegB_51l() == 0u
187 : const0->VRegB() == 0);
188 uint16_t base_mask = IsInstructionConstWide(const0->Opcode()) ? 3u : 1u;
189 return base_mask << const0->VRegA();
190}
191
192// We limit the number of IPUTs storing parameters. There can be any number
193// of IPUTs that store the value 0 as they are useless in a constructor as
194// the object always starts zero-initialized. We also eliminate all but the
195// last store to any field as they are not observable; not even if the field
196// is volatile as no reference to the object can escape from a constructor
197// with this pattern.
198static constexpr size_t kMaxConstructorIPuts = 3u;
199
200struct ConstructorIPutData {
201 ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { }
202
203 uint16_t field_index;
204 uint16_t arg;
205};
206
207bool RecordConstructorIPut(ArtMethod* method,
208 const Instruction* new_iput,
209 uint16_t this_vreg,
210 uint16_t zero_vreg_mask,
211 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000213 DCHECK(IsInstructionIPut(new_iput->Opcode()));
214 uint32_t field_index = new_iput->VRegC_22c();
Vladimir Markof44d36c2017-03-14 14:18:46 +0000215 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
216 ArtField* field = class_linker->LookupResolvedField(field_index, method, /* is_static */ false);
Vladimir Marko354efa62016-02-04 19:46:56 +0000217 if (UNLIKELY(field == nullptr)) {
218 return false;
219 }
220 // Remove previous IPUT to the same field, if any. Different field indexes may refer
221 // to the same field, so we need to compare resolved fields from the dex cache.
222 for (size_t old_pos = 0; old_pos != arraysize(iputs); ++old_pos) {
223 if (iputs[old_pos].field_index == DexFile::kDexNoIndex16) {
224 break;
225 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000226 ArtField* f = class_linker->LookupResolvedField(iputs[old_pos].field_index,
227 method,
228 /* is_static */ false);
Vladimir Marko354efa62016-02-04 19:46:56 +0000229 DCHECK(f != nullptr);
230 if (f == field) {
231 auto back_it = std::copy(iputs + old_pos + 1, iputs + arraysize(iputs), iputs + old_pos);
232 *back_it = ConstructorIPutData();
233 break;
234 }
235 }
236 // If the stored value isn't zero, record the IPUT.
237 if ((zero_vreg_mask & (1u << new_iput->VRegA_22c())) == 0u) {
238 size_t new_pos = 0;
239 while (new_pos != arraysize(iputs) && iputs[new_pos].field_index != DexFile::kDexNoIndex16) {
240 ++new_pos;
241 }
242 if (new_pos == arraysize(iputs)) {
243 return false; // Exceeded capacity of the output array.
244 }
245 iputs[new_pos].field_index = field_index;
246 iputs[new_pos].arg = new_iput->VRegA_22c() - this_vreg;
247 }
248 return true;
249}
250
Mathieu Chartier69147f12017-11-06 20:02:24 -0800251bool DoAnalyseConstructor(const CodeItemDataAccessor* code_item,
Vladimir Marko354efa62016-02-04 19:46:56 +0000252 ArtMethod* method,
253 /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts])
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000255 // On entry we should not have any IPUTs yet.
256 DCHECK_EQ(0, std::count_if(
257 iputs,
258 iputs + arraysize(iputs),
259 [](const ConstructorIPutData& iput_data) {
260 return iput_data.field_index != DexFile::kDexNoIndex16;
261 }));
262
263 // Limit the maximum number of code units we're willing to match.
264 static constexpr size_t kMaxCodeUnits = 16u;
265
266 // Limit the number of registers that the constructor may use to 16.
267 // Given that IPUTs must use low 16 registers and we do not match MOVEs,
268 // this is a reasonable limitation.
269 static constexpr size_t kMaxVRegs = 16u;
270
271 // We try to match a constructor that calls another constructor (either in
272 // superclass or in the same class) with the same parameters, or with some
273 // parameters truncated (allowed only for calls to superclass constructor)
274 // or with extra parameters with value 0 (with any type, including null).
275 // This call can be followed by optional IPUTs on "this" storing either one
276 // of the parameters or 0 and the code must then finish with RETURN_VOID.
277 // The called constructor must be either java.lang.Object.<init>() or it
278 // must also match the same pattern.
279 static Matcher::MatchFn* const kConstructorPattern[] = {
280 &Matcher::Mark,
281 &Matcher::Repeated<&Matcher::Const0>,
282 &Matcher::Required<&Matcher::Opcode<Instruction::INVOKE_DIRECT>>,
283 &Matcher::Mark,
284 &Matcher::Repeated<&Matcher::Const0>,
285 &Matcher::Repeated<&Matcher::IPutOnThis>,
286 &Matcher::Required<&Matcher::Opcode<Instruction::RETURN_VOID>>,
287 };
288
289 DCHECK(method != nullptr);
290 DCHECK(!method->IsStatic());
291 DCHECK(method->IsConstructor());
292 DCHECK(code_item != nullptr);
293 if (!method->GetDeclaringClass()->IsVerified() ||
Mathieu Chartier69147f12017-11-06 20:02:24 -0800294 code_item->InsnsSizeInCodeUnits() > kMaxCodeUnits ||
295 code_item->RegistersSize() > kMaxVRegs ||
Vladimir Marko354efa62016-02-04 19:46:56 +0000296 !Matcher::Match(code_item, kConstructorPattern)) {
297 return false;
298 }
299
300 // Verify the invoke, prevent a few odd cases and collect IPUTs.
Mathieu Chartier69147f12017-11-06 20:02:24 -0800301 uint16_t this_vreg = code_item->RegistersSize() - code_item->InsSize();
Vladimir Marko354efa62016-02-04 19:46:56 +0000302 uint16_t zero_vreg_mask = 0u;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700303
Mathieu Chartier69147f12017-11-06 20:02:24 -0800304 for (const DexInstructionPcPair& pair : *code_item) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700305 const Instruction& instruction = pair.Inst();
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700306 if (instruction.Opcode() == Instruction::RETURN_VOID) {
307 break;
308 } else if (instruction.Opcode() == Instruction::INVOKE_DIRECT) {
309 ArtMethod* target_method = GetTargetConstructor(method, &instruction);
Vladimir Marko354efa62016-02-04 19:46:56 +0000310 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() &&
Mathieu Chartier69147f12017-11-06 20:02:24 -0800316 instruction.VRegA_35c() <= code_item->InsSize()) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000317 return false;
318 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700319 size_t forwarded = CountForwardedConstructorArguments(code_item, &instruction, zero_vreg_mask);
Vladimir Marko354efa62016-02-04 19:46:56 +0000320 if (forwarded == static_cast<size_t>(-1)) {
321 return false;
322 }
323 if (target_method->GetDeclaringClass()->IsObjectClass()) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800324 DCHECK_EQ(CodeItemDataAccessor(target_method).begin()->Opcode(), Instruction::RETURN_VOID);
Vladimir Marko354efa62016-02-04 19:46:56 +0000325 } else {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800326 CodeItemDataAccessor target_code_item = CodeItemDataAccessor::CreateNullable(target_method);
327 if (!target_code_item.HasCodeItem()) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000328 return false; // Native constructor?
329 }
Mathieu Chartier69147f12017-11-06 20:02:24 -0800330 if (!DoAnalyseConstructor(&target_code_item, target_method, iputs)) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000331 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 }
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700348 } else if (IsInstructionDirectConst(instruction.Opcode())) {
349 zero_vreg_mask |= GetZeroVRegMask(&instruction);
Vladimir Marko354efa62016-02-04 19:46:56 +0000350 if ((zero_vreg_mask & (1u << this_vreg)) != 0u) {
351 return false; // Overwriting `this` is unsupported.
352 }
353 } else {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700354 DCHECK(IsInstructionIPut(instruction.Opcode()));
355 DCHECK_EQ(instruction.VRegB_22c(), this_vreg);
356 if (!RecordConstructorIPut(method, &instruction, this_vreg, zero_vreg_mask, iputs)) {
Vladimir Marko354efa62016-02-04 19:46:56 +0000357 return false;
358 }
359 }
360 }
361 return true;
362}
363
364} // anonymous namespace
365
Mathieu Chartier69147f12017-11-06 20:02:24 -0800366bool AnalyseConstructor(const CodeItemDataAccessor* code_item,
Vladimir Marko354efa62016-02-04 19:46:56 +0000367 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) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800430 CodeItemDataAccessor code_item = CodeItemDataAccessor::CreateNullable(method);
431 if (!code_item.HasCodeItem()) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000432 // Native or abstract.
433 return false;
434 }
Mathieu Chartier69147f12017-11-06 20:02:24 -0800435 return AnalyseMethodCode(&code_item,
Andreas Gampe5d08fcc2017-06-05 17:56:46 -0700436 MethodReference(method->GetDexFile(), method->GetDexMethodIndex()),
437 method->IsStatic(),
438 method,
439 result);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000440}
441
Mathieu Chartier69147f12017-11-06 20:02:24 -0800442bool InlineMethodAnalyser::AnalyseMethodCode(const CodeItemDataAccessor* code_item,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000443 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
Mathieu Chartier69147f12017-11-06 20:02:24 -0800449 DCHECK_NE(code_item->InsnsSizeInCodeUnits(), 0u);
450 Instruction::Code opcode = code_item->begin()->Opcode();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000451
452 switch (opcode) {
453 case Instruction::RETURN_VOID:
Vladimir Marko9f35ccd2016-02-02 20:12:32 +0000454 if (result != nullptr) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000455 result->opcode = kInlineOpNop;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000456 result->d.data = 0u;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100457 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000458 return true;
459 case Instruction::RETURN:
460 case Instruction::RETURN_OBJECT:
461 case Instruction::RETURN_WIDE:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000462 return AnalyseReturnMethod(code_item, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000463 case Instruction::CONST:
464 case Instruction::CONST_4:
465 case Instruction::CONST_16:
466 case Instruction::CONST_HIGH16:
467 // TODO: Support wide constants (RETURN_WIDE).
Vladimir Marko354efa62016-02-04 19:46:56 +0000468 if (AnalyseConstMethod(code_item, result)) {
469 return true;
470 }
471 FALLTHROUGH_INTENDED;
472 case Instruction::CONST_WIDE:
473 case Instruction::CONST_WIDE_16:
474 case Instruction::CONST_WIDE_32:
475 case Instruction::CONST_WIDE_HIGH16:
476 case Instruction::INVOKE_DIRECT:
477 if (method != nullptr && !method->IsStatic() && method->IsConstructor()) {
478 return AnalyseConstructor(code_item, method, result);
479 }
480 return false;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000481 case Instruction::IGET:
482 case Instruction::IGET_OBJECT:
483 case Instruction::IGET_BOOLEAN:
484 case Instruction::IGET_BYTE:
485 case Instruction::IGET_CHAR:
486 case Instruction::IGET_SHORT:
487 case Instruction::IGET_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800488 // TODO: Add handling for JIT.
489 // case Instruction::IGET_QUICK:
490 // case Instruction::IGET_WIDE_QUICK:
491 // case Instruction::IGET_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000492 return AnalyseIGetMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000493 case Instruction::IPUT:
494 case Instruction::IPUT_OBJECT:
495 case Instruction::IPUT_BOOLEAN:
496 case Instruction::IPUT_BYTE:
497 case Instruction::IPUT_CHAR:
498 case Instruction::IPUT_SHORT:
499 case Instruction::IPUT_WIDE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800500 // TODO: Add handling for JIT.
501 // case Instruction::IPUT_QUICK:
502 // case Instruction::IPUT_WIDE_QUICK:
503 // case Instruction::IPUT_OBJECT_QUICK:
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000504 return AnalyseIPutMethod(code_item, method_ref, is_static, method, result);
Vladimir Markoe3e02602014-03-12 15:42:41 +0000505 default:
506 return false;
507 }
508}
509
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100510bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700511 const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.index);
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100512 const char* method_name = ref.dex_file->GetMethodName(method_id);
Vladimir Markod5f10052015-05-06 14:09:04 +0100513 // javac names synthetic accessors "access$nnn",
514 // jack names them "-getN", "-putN", "-wrapN".
515 return strncmp(method_name, "access$", strlen("access$")) == 0 ||
516 strncmp(method_name, "-", strlen("-")) == 0;
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100517}
518
Mathieu Chartier69147f12017-11-06 20:02:24 -0800519bool InlineMethodAnalyser::AnalyseReturnMethod(const CodeItemDataAccessor* code_item,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000520 InlineMethod* result) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800521 DexInstructionIterator return_instruction = code_item->begin();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000522 Instruction::Code return_opcode = return_instruction->Opcode();
523 uint32_t reg = return_instruction->VRegA_11x();
Mathieu Chartier69147f12017-11-06 20:02:24 -0800524 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000525 DCHECK_GE(reg, arg_start);
526 DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg,
Mathieu Chartier69147f12017-11-06 20:02:24 -0800527 code_item->RegistersSize());
Vladimir Markoe3e02602014-03-12 15:42:41 +0000528
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100529 if (result != nullptr) {
530 result->opcode = kInlineOpReturnArg;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100531 InlineReturnArgData* data = &result->d.return_data;
532 data->arg = reg - arg_start;
533 data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u;
534 data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u;
535 data->reserved = 0u;
536 data->reserved2 = 0u;
537 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000538 return true;
539}
540
Mathieu Chartier69147f12017-11-06 20:02:24 -0800541bool InlineMethodAnalyser::AnalyseConstMethod(const CodeItemDataAccessor* code_item,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000542 InlineMethod* result) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800543 DexInstructionIterator instruction = code_item->begin();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000544 const Instruction* return_instruction = instruction->Next();
545 Instruction::Code return_opcode = return_instruction->Opcode();
546 if (return_opcode != Instruction::RETURN &&
547 return_opcode != Instruction::RETURN_OBJECT) {
548 return false;
549 }
550
Ian Rogers29a26482014-05-02 15:27:29 -0700551 int32_t return_reg = return_instruction->VRegA_11x();
Mathieu Chartier69147f12017-11-06 20:02:24 -0800552 DCHECK_LT(return_reg, code_item->RegistersSize());
Vladimir Markoe3e02602014-03-12 15:42:41 +0000553
Ian Rogers29a26482014-05-02 15:27:29 -0700554 int32_t const_value = instruction->VRegB();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000555 if (instruction->Opcode() == Instruction::CONST_HIGH16) {
Ian Rogers29a26482014-05-02 15:27:29 -0700556 const_value <<= 16;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000557 }
Mathieu Chartier69147f12017-11-06 20:02:24 -0800558 DCHECK_LT(instruction->VRegA(), code_item->RegistersSize());
Ian Rogers29a26482014-05-02 15:27:29 -0700559 if (instruction->VRegA() != return_reg) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000560 return false; // Not returning the value set by const?
561 }
Ian Rogers29a26482014-05-02 15:27:29 -0700562 if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000563 return false; // Returning non-null reference constant?
564 }
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100565 if (result != nullptr) {
566 result->opcode = kInlineOpNonWideConst;
Ian Rogers29a26482014-05-02 15:27:29 -0700567 result->d.data = static_cast<uint64_t>(const_value);
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100568 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000569 return true;
570}
571
Mathieu Chartier69147f12017-11-06 20:02:24 -0800572bool InlineMethodAnalyser::AnalyseIGetMethod(const CodeItemDataAccessor* code_item,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000573 const MethodReference& method_ref,
574 bool is_static,
575 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000576 InlineMethod* result) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800577 DexInstructionIterator instruction = code_item->begin();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000578 Instruction::Code opcode = instruction->Opcode();
579 DCHECK(IsInstructionIGet(opcode));
580
581 const Instruction* return_instruction = instruction->Next();
582 Instruction::Code return_opcode = return_instruction->Opcode();
583 if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) &&
584 !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) &&
585 !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE &&
586 opcode != Instruction::IGET_OBJECT)) {
587 return false;
588 }
589
590 uint32_t return_reg = return_instruction->VRegA_11x();
591 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg,
Mathieu Chartier69147f12017-11-06 20:02:24 -0800592 code_item->RegistersSize());
Vladimir Markoe3e02602014-03-12 15:42:41 +0000593
594 uint32_t dst_reg = instruction->VRegA_22c();
595 uint32_t object_reg = instruction->VRegB_22c();
596 uint32_t field_idx = instruction->VRegC_22c();
Mathieu Chartier69147f12017-11-06 20:02:24 -0800597 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000598 DCHECK_GE(object_reg, arg_start);
Mathieu Chartier69147f12017-11-06 20:02:24 -0800599 DCHECK_LT(object_reg, code_item->RegistersSize());
Vladimir Markoe1fced12014-04-04 14:52:53 +0100600 uint32_t object_arg = object_reg - arg_start;
601
Mathieu Chartier69147f12017-11-06 20:02:24 -0800602 DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->RegistersSize());
Vladimir Markoe3e02602014-03-12 15:42:41 +0000603 if (dst_reg != return_reg) {
604 return false; // Not returning the value retrieved by IGET?
605 }
606
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000607 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100608 // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE).
609 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000610 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100611 return false;
612 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000613 }
614
Vladimir Markoe1fced12014-04-04 14:52:53 +0100615 // InlineIGetIPutData::object_arg is only 4 bits wide.
616 static constexpr uint16_t kMaxObjectArg = 15u;
617 if (object_arg > kMaxObjectArg) {
618 return false;
619 }
620
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100621 if (result != nullptr) {
622 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000623 if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100624 return false;
625 }
626 result->opcode = kInlineOpIGet;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100627 data->op_variant = IGetVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000628 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100629 data->object_arg = object_arg; // Allow IGET on any register, not just "this".
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100630 data->src_arg = 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100631 data->return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000632 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000633 return true;
634}
635
Mathieu Chartier69147f12017-11-06 20:02:24 -0800636bool InlineMethodAnalyser::AnalyseIPutMethod(const CodeItemDataAccessor* code_item,
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000637 const MethodReference& method_ref,
638 bool is_static,
639 ArtMethod* method,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000640 InlineMethod* result) {
Mathieu Chartier69147f12017-11-06 20:02:24 -0800641 DexInstructionIterator instruction = code_item->begin();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000642 Instruction::Code opcode = instruction->Opcode();
643 DCHECK(IsInstructionIPut(opcode));
644
645 const Instruction* return_instruction = instruction->Next();
646 Instruction::Code return_opcode = return_instruction->Opcode();
Mathieu Chartier69147f12017-11-06 20:02:24 -0800647 uint32_t arg_start = code_item->RegistersSize() - code_item->InsSize();
Vladimir Markoe1fced12014-04-04 14:52:53 +0100648 uint16_t return_arg_plus1 = 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000649 if (return_opcode != Instruction::RETURN_VOID) {
Vladimir Markoe1fced12014-04-04 14:52:53 +0100650 if (return_opcode != Instruction::RETURN &&
651 return_opcode != Instruction::RETURN_OBJECT &&
652 return_opcode != Instruction::RETURN_WIDE) {
653 return false;
654 }
655 // Returning an argument.
656 uint32_t return_reg = return_instruction->VRegA_11x();
657 DCHECK_GE(return_reg, arg_start);
658 DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg,
Mathieu Chartier69147f12017-11-06 20:02:24 -0800659 code_item->RegistersSize());
Vladimir Markoe1fced12014-04-04 14:52:53 +0100660 return_arg_plus1 = return_reg - arg_start + 1u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000661 }
662
663 uint32_t src_reg = instruction->VRegA_22c();
664 uint32_t object_reg = instruction->VRegB_22c();
665 uint32_t field_idx = instruction->VRegC_22c();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000666 DCHECK_GE(object_reg, arg_start);
Mathieu Chartier69147f12017-11-06 20:02:24 -0800667 DCHECK_LT(object_reg, code_item->RegistersSize());
Vladimir Markoe3e02602014-03-12 15:42:41 +0000668 DCHECK_GE(src_reg, arg_start);
Mathieu Chartier69147f12017-11-06 20:02:24 -0800669 DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->RegistersSize());
Vladimir Markoe1fced12014-04-04 14:52:53 +0100670 uint32_t object_arg = object_reg - arg_start;
671 uint32_t src_arg = src_reg - arg_start;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000672
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000673 if (is_static || object_arg != 0u) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100674 // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE).
675 // Allow synthetic accessors. We don't care about losing their stack frame in NPE.
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000676 if (!IsSyntheticAccessor(method_ref)) {
Vladimir Markoc8f60a62014-04-02 15:24:05 +0100677 return false;
678 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000679 }
680
Vladimir Markoe1fced12014-04-04 14:52:53 +0100681 // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide.
682 static constexpr uint16_t kMaxObjectArg = 15u;
683 static constexpr uint16_t kMaxSrcArg = 15u;
684 static constexpr uint16_t kMaxReturnArgPlus1 = 15u;
685 if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) {
686 return false;
687 }
688
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100689 if (result != nullptr) {
690 InlineIGetIPutData* data = &result->d.ifield_data;
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000691 if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) {
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100692 return false;
693 }
694 result->opcode = kInlineOpIPut;
Sebastien Hertz2c87c4d2014-03-21 11:31:51 +0100695 data->op_variant = IPutVariant(opcode);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000696 data->method_is_static = is_static ? 1u : 0u;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100697 data->object_arg = object_arg; // Allow IPUT on any register, not just "this".
698 data->src_arg = src_arg;
699 data->return_arg_plus1 = return_arg_plus1;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000700 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000701 return true;
702}
703
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000704bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method,
705 uint32_t field_idx,
706 bool is_put,
Vladimir Markoe3e02602014-03-12 15:42:41 +0000707 InlineIGetIPutData* result) {
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000708 if (method == nullptr) {
709 return false;
710 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000711 ObjPtr<mirror::DexCache> dex_cache = method->GetDexCache();
712 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
713 ArtField* field = class_linker->LookupResolvedField(field_idx, method, /* is_static */ false);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000714 if (field == nullptr || field->IsStatic()) {
Vladimir Markoe3e02602014-03-12 15:42:41 +0000715 return false;
716 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700717 ObjPtr<mirror::Class> method_class = method->GetDeclaringClass();
718 ObjPtr<mirror::Class> field_class = field->GetDeclaringClass();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000719 if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) ||
720 (is_put && field->IsFinal() && method_class != field_class)) {
721 return false;
722 }
723 DCHECK_GE(field->GetOffset().Int32Value(), 0);
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000724 // Do not interleave function calls with bit field writes to placate valgrind. Bug: 27552451.
725 uint32_t field_offset = field->GetOffset().Uint32Value();
726 bool is_volatile = field->IsVolatile();
Vladimir Markoe3e02602014-03-12 15:42:41 +0000727 result->field_idx = field_idx;
Vladimir Marko8b3f8352016-03-09 13:45:39 +0000728 result->field_offset = field_offset;
729 result->is_volatile = is_volatile ? 1u : 0u;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000730 return true;
731}
732
733} // namespace art