Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 1 | /* |
| 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 Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 24 | #include "dex_instruction.h" |
| 25 | #include "dex_instruction-inl.h" |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 26 | #include "dex_instruction_utils.h" |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 27 | #include "mirror/class-inl.h" |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 28 | #include "mirror/dex_cache-inl.h" |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 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 | |
| 35 | namespace art { |
| 36 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 37 | namespace { // anonymous namespace |
| 38 | |
| 39 | // Helper class for matching a pattern. |
| 40 | class 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 | |
| 79 | template <size_t size> |
| 80 | bool Matcher::Match(const DexFile::CodeItem* code_item, MatchFn* const (&pattern)[size]) { |
| 81 | return DoMatch(code_item, pattern, size); |
| 82 | } |
| 83 | |
| 84 | bool 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 | |
| 90 | template <bool (Matcher::*Fn)()> |
| 91 | bool 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 | |
| 100 | template <bool (Matcher::*Fn)()> |
| 101 | bool 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 | |
| 112 | template <Instruction::Code opcode> |
| 113 | bool Matcher::Opcode() { |
| 114 | return instruction_->Opcode() == opcode; |
| 115 | } |
| 116 | |
| 117 | // Match const 0. |
| 118 | bool Matcher::Const0() { |
| 119 | return IsInstructionDirectConst(instruction_->Opcode()) && |
| 120 | (instruction_->Opcode() == Instruction::CONST_WIDE ? instruction_->VRegB_51l() == 0 |
| 121 | : instruction_->VRegB() == 0); |
| 122 | } |
| 123 | |
| 124 | bool 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 | |
| 130 | bool 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". |
| 142 | ArtMethod* GetTargetConstructor(ArtMethod* method, const Instruction* invoke_direct) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 143 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 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(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 148 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 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). |
| 162 | size_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 | |
| 186 | uint16_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. |
| 200 | static constexpr size_t kMaxConstructorIPuts = 3u; |
| 201 | |
| 202 | struct ConstructorIPutData { |
| 203 | ConstructorIPutData() : field_index(DexFile::kDexNoIndex16), arg(0u) { } |
| 204 | |
| 205 | uint16_t field_index; |
| 206 | uint16_t arg; |
| 207 | }; |
| 208 | |
| 209 | bool RecordConstructorIPut(ArtMethod* method, |
| 210 | const Instruction* new_iput, |
| 211 | uint16_t this_vreg, |
| 212 | uint16_t zero_vreg_mask, |
| 213 | /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts]) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 214 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 215 | DCHECK(IsInstructionIPut(new_iput->Opcode())); |
| 216 | uint32_t field_index = new_iput->VRegC_22c(); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 217 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 218 | ArtField* field = class_linker->LookupResolvedField(field_index, method, /* is_static */ false); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 219 | if (UNLIKELY(field == nullptr)) { |
| 220 | return false; |
| 221 | } |
| 222 | // Remove previous IPUT to the same field, if any. Different field indexes may refer |
| 223 | // to the same field, so we need to compare resolved fields from the dex cache. |
| 224 | for (size_t old_pos = 0; old_pos != arraysize(iputs); ++old_pos) { |
| 225 | if (iputs[old_pos].field_index == DexFile::kDexNoIndex16) { |
| 226 | break; |
| 227 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 228 | ArtField* f = class_linker->LookupResolvedField(iputs[old_pos].field_index, |
| 229 | method, |
| 230 | /* is_static */ false); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 231 | DCHECK(f != nullptr); |
| 232 | if (f == field) { |
| 233 | auto back_it = std::copy(iputs + old_pos + 1, iputs + arraysize(iputs), iputs + old_pos); |
| 234 | *back_it = ConstructorIPutData(); |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | // If the stored value isn't zero, record the IPUT. |
| 239 | if ((zero_vreg_mask & (1u << new_iput->VRegA_22c())) == 0u) { |
| 240 | size_t new_pos = 0; |
| 241 | while (new_pos != arraysize(iputs) && iputs[new_pos].field_index != DexFile::kDexNoIndex16) { |
| 242 | ++new_pos; |
| 243 | } |
| 244 | if (new_pos == arraysize(iputs)) { |
| 245 | return false; // Exceeded capacity of the output array. |
| 246 | } |
| 247 | iputs[new_pos].field_index = field_index; |
| 248 | iputs[new_pos].arg = new_iput->VRegA_22c() - this_vreg; |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool DoAnalyseConstructor(const DexFile::CodeItem* code_item, |
| 254 | ArtMethod* method, |
| 255 | /*inout*/ ConstructorIPutData (&iputs)[kMaxConstructorIPuts]) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 256 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 257 | // On entry we should not have any IPUTs yet. |
| 258 | DCHECK_EQ(0, std::count_if( |
| 259 | iputs, |
| 260 | iputs + arraysize(iputs), |
| 261 | [](const ConstructorIPutData& iput_data) { |
| 262 | return iput_data.field_index != DexFile::kDexNoIndex16; |
| 263 | })); |
| 264 | |
| 265 | // Limit the maximum number of code units we're willing to match. |
| 266 | static constexpr size_t kMaxCodeUnits = 16u; |
| 267 | |
| 268 | // Limit the number of registers that the constructor may use to 16. |
| 269 | // Given that IPUTs must use low 16 registers and we do not match MOVEs, |
| 270 | // this is a reasonable limitation. |
| 271 | static constexpr size_t kMaxVRegs = 16u; |
| 272 | |
| 273 | // We try to match a constructor that calls another constructor (either in |
| 274 | // superclass or in the same class) with the same parameters, or with some |
| 275 | // parameters truncated (allowed only for calls to superclass constructor) |
| 276 | // or with extra parameters with value 0 (with any type, including null). |
| 277 | // This call can be followed by optional IPUTs on "this" storing either one |
| 278 | // of the parameters or 0 and the code must then finish with RETURN_VOID. |
| 279 | // The called constructor must be either java.lang.Object.<init>() or it |
| 280 | // must also match the same pattern. |
| 281 | static Matcher::MatchFn* const kConstructorPattern[] = { |
| 282 | &Matcher::Mark, |
| 283 | &Matcher::Repeated<&Matcher::Const0>, |
| 284 | &Matcher::Required<&Matcher::Opcode<Instruction::INVOKE_DIRECT>>, |
| 285 | &Matcher::Mark, |
| 286 | &Matcher::Repeated<&Matcher::Const0>, |
| 287 | &Matcher::Repeated<&Matcher::IPutOnThis>, |
| 288 | &Matcher::Required<&Matcher::Opcode<Instruction::RETURN_VOID>>, |
| 289 | }; |
| 290 | |
| 291 | DCHECK(method != nullptr); |
| 292 | DCHECK(!method->IsStatic()); |
| 293 | DCHECK(method->IsConstructor()); |
| 294 | DCHECK(code_item != nullptr); |
| 295 | if (!method->GetDeclaringClass()->IsVerified() || |
| 296 | code_item->insns_size_in_code_units_ > kMaxCodeUnits || |
| 297 | code_item->registers_size_ > kMaxVRegs || |
| 298 | !Matcher::Match(code_item, kConstructorPattern)) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // Verify the invoke, prevent a few odd cases and collect IPUTs. |
| 303 | uint16_t this_vreg = code_item->registers_size_ - code_item->ins_size_; |
| 304 | uint16_t zero_vreg_mask = 0u; |
| 305 | for (const Instruction* instruction = Instruction::At(code_item->insns_); |
| 306 | instruction->Opcode() != Instruction::RETURN_VOID; |
| 307 | instruction = instruction->Next()) { |
| 308 | if (instruction->Opcode() == Instruction::INVOKE_DIRECT) { |
| 309 | ArtMethod* target_method = GetTargetConstructor(method, instruction); |
| 310 | if (target_method == nullptr) { |
| 311 | return false; |
| 312 | } |
| 313 | // We allow forwarding constructors only if they pass more arguments |
| 314 | // to prevent infinite recursion. |
| 315 | if (target_method->GetDeclaringClass() == method->GetDeclaringClass() && |
| 316 | instruction->VRegA_35c() <= code_item->ins_size_) { |
| 317 | return false; |
| 318 | } |
| 319 | size_t forwarded = CountForwardedConstructorArguments(code_item, instruction, zero_vreg_mask); |
| 320 | if (forwarded == static_cast<size_t>(-1)) { |
| 321 | return false; |
| 322 | } |
| 323 | if (target_method->GetDeclaringClass()->IsObjectClass()) { |
Nicolas Geoffray | 141de8c | 2016-03-23 08:22:12 +0000 | [diff] [blame] | 324 | DCHECK_EQ(Instruction::At(target_method->GetCodeItem()->insns_)->Opcode(), |
| 325 | Instruction::RETURN_VOID); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 326 | } else { |
| 327 | const DexFile::CodeItem* target_code_item = target_method->GetCodeItem(); |
| 328 | if (target_code_item == nullptr) { |
| 329 | return false; // Native constructor? |
| 330 | } |
| 331 | if (!DoAnalyseConstructor(target_code_item, target_method, iputs)) { |
| 332 | return false; |
| 333 | } |
| 334 | // Prune IPUTs with zero input. |
| 335 | auto kept_end = std::remove_if( |
| 336 | iputs, |
| 337 | iputs + arraysize(iputs), |
| 338 | [forwarded](const ConstructorIPutData& iput_data) { |
| 339 | return iput_data.arg >= forwarded; |
| 340 | }); |
| 341 | std::fill(kept_end, iputs + arraysize(iputs), ConstructorIPutData()); |
| 342 | // If we have any IPUTs from the call, check that the target method is in the same |
| 343 | // dex file (compare DexCache references), otherwise field_indexes would be bogus. |
| 344 | if (iputs[0].field_index != DexFile::kDexNoIndex16 && |
| 345 | target_method->GetDexCache() != method->GetDexCache()) { |
| 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | } else if (IsInstructionDirectConst(instruction->Opcode())) { |
| 350 | zero_vreg_mask |= GetZeroVRegMask(instruction); |
| 351 | if ((zero_vreg_mask & (1u << this_vreg)) != 0u) { |
| 352 | return false; // Overwriting `this` is unsupported. |
| 353 | } |
| 354 | } else { |
| 355 | DCHECK(IsInstructionIPut(instruction->Opcode())); |
| 356 | DCHECK_EQ(instruction->VRegB_22c(), this_vreg); |
| 357 | if (!RecordConstructorIPut(method, instruction, this_vreg, zero_vreg_mask, iputs)) { |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | } // anonymous namespace |
| 366 | |
| 367 | bool AnalyseConstructor(const DexFile::CodeItem* code_item, |
| 368 | ArtMethod* method, |
| 369 | InlineMethod* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 370 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 371 | ConstructorIPutData iputs[kMaxConstructorIPuts]; |
| 372 | if (!DoAnalyseConstructor(code_item, method, iputs)) { |
| 373 | return false; |
| 374 | } |
| 375 | static_assert(kMaxConstructorIPuts == 3, "Unexpected limit"); // Code below depends on this. |
| 376 | DCHECK(iputs[0].field_index != DexFile::kDexNoIndex16 || |
| 377 | iputs[1].field_index == DexFile::kDexNoIndex16); |
| 378 | DCHECK(iputs[1].field_index != DexFile::kDexNoIndex16 || |
| 379 | iputs[2].field_index == DexFile::kDexNoIndex16); |
| 380 | |
| 381 | #define STORE_IPUT(n) \ |
| 382 | do { \ |
| 383 | result->d.constructor_data.iput##n##_field_index = iputs[n].field_index; \ |
| 384 | result->d.constructor_data.iput##n##_arg = iputs[n].arg; \ |
| 385 | } while (false) |
| 386 | |
| 387 | STORE_IPUT(0); |
| 388 | STORE_IPUT(1); |
| 389 | STORE_IPUT(2); |
| 390 | #undef STORE_IPUT |
| 391 | |
| 392 | result->opcode = kInlineOpConstructor; |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 393 | result->d.constructor_data.reserved = 0u; |
| 394 | return true; |
| 395 | } |
| 396 | |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 397 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET), "iget type"); |
| 398 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_WIDE), "iget_wide type"); |
| 399 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_OBJECT), |
| 400 | "iget_object type"); |
| 401 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BOOLEAN), |
| 402 | "iget_boolean type"); |
| 403 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_BYTE), "iget_byte type"); |
| 404 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_CHAR), "iget_char type"); |
| 405 | static_assert(InlineMethodAnalyser::IsInstructionIGet(Instruction::IGET_SHORT), "iget_short type"); |
| 406 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT), "iput type"); |
| 407 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_WIDE), "iput_wide type"); |
| 408 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_OBJECT), |
| 409 | "iput_object type"); |
| 410 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BOOLEAN), |
| 411 | "iput_boolean type"); |
| 412 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_BYTE), "iput_byte type"); |
| 413 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_CHAR), "iput_char type"); |
| 414 | static_assert(InlineMethodAnalyser::IsInstructionIPut(Instruction::IPUT_SHORT), "iput_short type"); |
| 415 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET) == |
| 416 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT), "iget/iput variant"); |
| 417 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE) == |
| 418 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE), "iget/iput_wide variant"); |
| 419 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT) == |
| 420 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT), "iget/iput_object variant"); |
| 421 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BOOLEAN) == |
| 422 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BOOLEAN), "iget/iput_boolean variant"); |
| 423 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_BYTE) == |
| 424 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_BYTE), "iget/iput_byte variant"); |
| 425 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_CHAR) == |
| 426 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_CHAR), "iget/iput_char variant"); |
| 427 | static_assert(InlineMethodAnalyser::IGetVariant(Instruction::IGET_SHORT) == |
| 428 | InlineMethodAnalyser::IPutVariant(Instruction::IPUT_SHORT), "iget/iput_short variant"); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 429 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 430 | bool InlineMethodAnalyser::AnalyseMethodCode(ArtMethod* method, InlineMethod* result) { |
| 431 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 432 | if (code_item == nullptr) { |
| 433 | // Native or abstract. |
| 434 | return false; |
| 435 | } |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame^] | 436 | return AnalyseMethodCode(code_item, |
| 437 | MethodReference(method->GetDexFile(), method->GetDexMethodIndex()), |
| 438 | method->IsStatic(), |
| 439 | method, |
| 440 | result); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | bool InlineMethodAnalyser::AnalyseMethodCode(const DexFile::CodeItem* code_item, |
| 444 | const MethodReference& method_ref, |
| 445 | bool is_static, |
| 446 | ArtMethod* method, |
| 447 | InlineMethod* result) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 448 | // We currently support only plain return or 2-instruction methods. |
| 449 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 450 | DCHECK_NE(code_item->insns_size_in_code_units_, 0u); |
| 451 | const Instruction* instruction = Instruction::At(code_item->insns_); |
| 452 | Instruction::Code opcode = instruction->Opcode(); |
| 453 | |
| 454 | switch (opcode) { |
| 455 | case Instruction::RETURN_VOID: |
Vladimir Marko | 9f35ccd | 2016-02-02 20:12:32 +0000 | [diff] [blame] | 456 | if (result != nullptr) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 457 | result->opcode = kInlineOpNop; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 458 | result->d.data = 0u; |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 459 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 460 | return true; |
| 461 | case Instruction::RETURN: |
| 462 | case Instruction::RETURN_OBJECT: |
| 463 | case Instruction::RETURN_WIDE: |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 464 | return AnalyseReturnMethod(code_item, result); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 465 | case Instruction::CONST: |
| 466 | case Instruction::CONST_4: |
| 467 | case Instruction::CONST_16: |
| 468 | case Instruction::CONST_HIGH16: |
| 469 | // TODO: Support wide constants (RETURN_WIDE). |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 470 | if (AnalyseConstMethod(code_item, result)) { |
| 471 | return true; |
| 472 | } |
| 473 | FALLTHROUGH_INTENDED; |
| 474 | case Instruction::CONST_WIDE: |
| 475 | case Instruction::CONST_WIDE_16: |
| 476 | case Instruction::CONST_WIDE_32: |
| 477 | case Instruction::CONST_WIDE_HIGH16: |
| 478 | case Instruction::INVOKE_DIRECT: |
| 479 | if (method != nullptr && !method->IsStatic() && method->IsConstructor()) { |
| 480 | return AnalyseConstructor(code_item, method, result); |
| 481 | } |
| 482 | return false; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 483 | case Instruction::IGET: |
| 484 | case Instruction::IGET_OBJECT: |
| 485 | case Instruction::IGET_BOOLEAN: |
| 486 | case Instruction::IGET_BYTE: |
| 487 | case Instruction::IGET_CHAR: |
| 488 | case Instruction::IGET_SHORT: |
| 489 | case Instruction::IGET_WIDE: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 490 | // TODO: Add handling for JIT. |
| 491 | // case Instruction::IGET_QUICK: |
| 492 | // case Instruction::IGET_WIDE_QUICK: |
| 493 | // case Instruction::IGET_OBJECT_QUICK: |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 494 | return AnalyseIGetMethod(code_item, method_ref, is_static, method, result); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 495 | case Instruction::IPUT: |
| 496 | case Instruction::IPUT_OBJECT: |
| 497 | case Instruction::IPUT_BOOLEAN: |
| 498 | case Instruction::IPUT_BYTE: |
| 499 | case Instruction::IPUT_CHAR: |
| 500 | case Instruction::IPUT_SHORT: |
| 501 | case Instruction::IPUT_WIDE: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 502 | // TODO: Add handling for JIT. |
| 503 | // case Instruction::IPUT_QUICK: |
| 504 | // case Instruction::IPUT_WIDE_QUICK: |
| 505 | // case Instruction::IPUT_OBJECT_QUICK: |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 506 | return AnalyseIPutMethod(code_item, method_ref, is_static, method, result); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 507 | default: |
| 508 | return false; |
| 509 | } |
| 510 | } |
| 511 | |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 512 | bool InlineMethodAnalyser::IsSyntheticAccessor(MethodReference ref) { |
| 513 | const DexFile::MethodId& method_id = ref.dex_file->GetMethodId(ref.dex_method_index); |
| 514 | const char* method_name = ref.dex_file->GetMethodName(method_id); |
Vladimir Marko | d5f1005 | 2015-05-06 14:09:04 +0100 | [diff] [blame] | 515 | // javac names synthetic accessors "access$nnn", |
| 516 | // jack names them "-getN", "-putN", "-wrapN". |
| 517 | return strncmp(method_name, "access$", strlen("access$")) == 0 || |
| 518 | strncmp(method_name, "-", strlen("-")) == 0; |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 521 | bool InlineMethodAnalyser::AnalyseReturnMethod(const DexFile::CodeItem* code_item, |
| 522 | InlineMethod* result) { |
| 523 | const Instruction* return_instruction = Instruction::At(code_item->insns_); |
| 524 | Instruction::Code return_opcode = return_instruction->Opcode(); |
| 525 | uint32_t reg = return_instruction->VRegA_11x(); |
| 526 | uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_; |
| 527 | DCHECK_GE(reg, arg_start); |
| 528 | DCHECK_LT((return_opcode == Instruction::RETURN_WIDE) ? reg + 1 : reg, |
| 529 | code_item->registers_size_); |
| 530 | |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 531 | if (result != nullptr) { |
| 532 | result->opcode = kInlineOpReturnArg; |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 533 | InlineReturnArgData* data = &result->d.return_data; |
| 534 | data->arg = reg - arg_start; |
| 535 | data->is_wide = (return_opcode == Instruction::RETURN_WIDE) ? 1u : 0u; |
| 536 | data->is_object = (return_opcode == Instruction::RETURN_OBJECT) ? 1u : 0u; |
| 537 | data->reserved = 0u; |
| 538 | data->reserved2 = 0u; |
| 539 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
| 543 | bool InlineMethodAnalyser::AnalyseConstMethod(const DexFile::CodeItem* code_item, |
| 544 | InlineMethod* result) { |
| 545 | const Instruction* instruction = Instruction::At(code_item->insns_); |
| 546 | const Instruction* return_instruction = instruction->Next(); |
| 547 | Instruction::Code return_opcode = return_instruction->Opcode(); |
| 548 | if (return_opcode != Instruction::RETURN && |
| 549 | return_opcode != Instruction::RETURN_OBJECT) { |
| 550 | return false; |
| 551 | } |
| 552 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 553 | int32_t return_reg = return_instruction->VRegA_11x(); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 554 | DCHECK_LT(return_reg, code_item->registers_size_); |
| 555 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 556 | int32_t const_value = instruction->VRegB(); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 557 | if (instruction->Opcode() == Instruction::CONST_HIGH16) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 558 | const_value <<= 16; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 559 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 560 | DCHECK_LT(instruction->VRegA(), code_item->registers_size_); |
| 561 | if (instruction->VRegA() != return_reg) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 562 | return false; // Not returning the value set by const? |
| 563 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 564 | if (return_opcode == Instruction::RETURN_OBJECT && const_value != 0) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 565 | return false; // Returning non-null reference constant? |
| 566 | } |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 567 | if (result != nullptr) { |
| 568 | result->opcode = kInlineOpNonWideConst; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 569 | result->d.data = static_cast<uint64_t>(const_value); |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 570 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 574 | bool InlineMethodAnalyser::AnalyseIGetMethod(const DexFile::CodeItem* code_item, |
| 575 | const MethodReference& method_ref, |
| 576 | bool is_static, |
| 577 | ArtMethod* method, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 578 | InlineMethod* result) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 579 | const Instruction* instruction = Instruction::At(code_item->insns_); |
| 580 | Instruction::Code opcode = instruction->Opcode(); |
| 581 | DCHECK(IsInstructionIGet(opcode)); |
| 582 | |
| 583 | const Instruction* return_instruction = instruction->Next(); |
| 584 | Instruction::Code return_opcode = return_instruction->Opcode(); |
| 585 | if (!(return_opcode == Instruction::RETURN_WIDE && opcode == Instruction::IGET_WIDE) && |
| 586 | !(return_opcode == Instruction::RETURN_OBJECT && opcode == Instruction::IGET_OBJECT) && |
| 587 | !(return_opcode == Instruction::RETURN && opcode != Instruction::IGET_WIDE && |
| 588 | opcode != Instruction::IGET_OBJECT)) { |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | uint32_t return_reg = return_instruction->VRegA_11x(); |
| 593 | DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1 : return_reg, |
| 594 | code_item->registers_size_); |
| 595 | |
| 596 | uint32_t dst_reg = instruction->VRegA_22c(); |
| 597 | uint32_t object_reg = instruction->VRegB_22c(); |
| 598 | uint32_t field_idx = instruction->VRegC_22c(); |
| 599 | uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_; |
| 600 | DCHECK_GE(object_reg, arg_start); |
| 601 | DCHECK_LT(object_reg, code_item->registers_size_); |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 602 | uint32_t object_arg = object_reg - arg_start; |
| 603 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 604 | DCHECK_LT(opcode == Instruction::IGET_WIDE ? dst_reg + 1 : dst_reg, code_item->registers_size_); |
| 605 | if (dst_reg != return_reg) { |
| 606 | return false; // Not returning the value retrieved by IGET? |
| 607 | } |
| 608 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 609 | if (is_static || object_arg != 0u) { |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 610 | // TODO: Implement inlining of IGET on non-"this" registers (needs correct stack trace for NPE). |
| 611 | // Allow synthetic accessors. We don't care about losing their stack frame in NPE. |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 612 | if (!IsSyntheticAccessor(method_ref)) { |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 613 | return false; |
| 614 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 617 | // InlineIGetIPutData::object_arg is only 4 bits wide. |
| 618 | static constexpr uint16_t kMaxObjectArg = 15u; |
| 619 | if (object_arg > kMaxObjectArg) { |
| 620 | return false; |
| 621 | } |
| 622 | |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 623 | if (result != nullptr) { |
| 624 | InlineIGetIPutData* data = &result->d.ifield_data; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 625 | if (!ComputeSpecialAccessorInfo(method, field_idx, false, data)) { |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | result->opcode = kInlineOpIGet; |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 629 | data->op_variant = IGetVariant(opcode); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 630 | data->method_is_static = is_static ? 1u : 0u; |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 631 | data->object_arg = object_arg; // Allow IGET on any register, not just "this". |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 632 | data->src_arg = 0u; |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 633 | data->return_arg_plus1 = 0u; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 634 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 638 | bool InlineMethodAnalyser::AnalyseIPutMethod(const DexFile::CodeItem* code_item, |
| 639 | const MethodReference& method_ref, |
| 640 | bool is_static, |
| 641 | ArtMethod* method, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 642 | InlineMethod* result) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 643 | const Instruction* instruction = Instruction::At(code_item->insns_); |
| 644 | Instruction::Code opcode = instruction->Opcode(); |
| 645 | DCHECK(IsInstructionIPut(opcode)); |
| 646 | |
| 647 | const Instruction* return_instruction = instruction->Next(); |
| 648 | Instruction::Code return_opcode = return_instruction->Opcode(); |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 649 | uint32_t arg_start = code_item->registers_size_ - code_item->ins_size_; |
| 650 | uint16_t return_arg_plus1 = 0u; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 651 | if (return_opcode != Instruction::RETURN_VOID) { |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 652 | if (return_opcode != Instruction::RETURN && |
| 653 | return_opcode != Instruction::RETURN_OBJECT && |
| 654 | return_opcode != Instruction::RETURN_WIDE) { |
| 655 | return false; |
| 656 | } |
| 657 | // Returning an argument. |
| 658 | uint32_t return_reg = return_instruction->VRegA_11x(); |
| 659 | DCHECK_GE(return_reg, arg_start); |
| 660 | DCHECK_LT(return_opcode == Instruction::RETURN_WIDE ? return_reg + 1u : return_reg, |
| 661 | code_item->registers_size_); |
| 662 | return_arg_plus1 = return_reg - arg_start + 1u; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | uint32_t src_reg = instruction->VRegA_22c(); |
| 666 | uint32_t object_reg = instruction->VRegB_22c(); |
| 667 | uint32_t field_idx = instruction->VRegC_22c(); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 668 | DCHECK_GE(object_reg, arg_start); |
| 669 | DCHECK_LT(object_reg, code_item->registers_size_); |
| 670 | DCHECK_GE(src_reg, arg_start); |
| 671 | DCHECK_LT(opcode == Instruction::IPUT_WIDE ? src_reg + 1 : src_reg, code_item->registers_size_); |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 672 | uint32_t object_arg = object_reg - arg_start; |
| 673 | uint32_t src_arg = src_reg - arg_start; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 674 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 675 | if (is_static || object_arg != 0u) { |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 676 | // TODO: Implement inlining of IPUT on non-"this" registers (needs correct stack trace for NPE). |
| 677 | // Allow synthetic accessors. We don't care about losing their stack frame in NPE. |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 678 | if (!IsSyntheticAccessor(method_ref)) { |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 679 | return false; |
| 680 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 683 | // InlineIGetIPutData::object_arg/src_arg/return_arg_plus1 are each only 4 bits wide. |
| 684 | static constexpr uint16_t kMaxObjectArg = 15u; |
| 685 | static constexpr uint16_t kMaxSrcArg = 15u; |
| 686 | static constexpr uint16_t kMaxReturnArgPlus1 = 15u; |
| 687 | if (object_arg > kMaxObjectArg || src_arg > kMaxSrcArg || return_arg_plus1 > kMaxReturnArgPlus1) { |
| 688 | return false; |
| 689 | } |
| 690 | |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 691 | if (result != nullptr) { |
| 692 | InlineIGetIPutData* data = &result->d.ifield_data; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 693 | if (!ComputeSpecialAccessorInfo(method, field_idx, true, data)) { |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 694 | return false; |
| 695 | } |
| 696 | result->opcode = kInlineOpIPut; |
Sebastien Hertz | 2c87c4d | 2014-03-21 11:31:51 +0100 | [diff] [blame] | 697 | data->op_variant = IPutVariant(opcode); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 698 | data->method_is_static = is_static ? 1u : 0u; |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 699 | data->object_arg = object_arg; // Allow IPUT on any register, not just "this". |
| 700 | data->src_arg = src_arg; |
| 701 | data->return_arg_plus1 = return_arg_plus1; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 702 | } |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 706 | bool InlineMethodAnalyser::ComputeSpecialAccessorInfo(ArtMethod* method, |
| 707 | uint32_t field_idx, |
| 708 | bool is_put, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 709 | InlineIGetIPutData* result) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 710 | if (method == nullptr) { |
| 711 | return false; |
| 712 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 713 | ObjPtr<mirror::DexCache> dex_cache = method->GetDexCache(); |
| 714 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 715 | ArtField* field = class_linker->LookupResolvedField(field_idx, method, /* is_static */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 716 | if (field == nullptr || field->IsStatic()) { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 717 | return false; |
| 718 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 719 | ObjPtr<mirror::Class> method_class = method->GetDeclaringClass(); |
| 720 | ObjPtr<mirror::Class> field_class = field->GetDeclaringClass(); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 721 | if (!method_class->CanAccessResolvedField(field_class, field, dex_cache, field_idx) || |
| 722 | (is_put && field->IsFinal() && method_class != field_class)) { |
| 723 | return false; |
| 724 | } |
| 725 | DCHECK_GE(field->GetOffset().Int32Value(), 0); |
Vladimir Marko | 8b3f835 | 2016-03-09 13:45:39 +0000 | [diff] [blame] | 726 | // Do not interleave function calls with bit field writes to placate valgrind. Bug: 27552451. |
| 727 | uint32_t field_offset = field->GetOffset().Uint32Value(); |
| 728 | bool is_volatile = field->IsVolatile(); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 729 | result->field_idx = field_idx; |
Vladimir Marko | 8b3f835 | 2016-03-09 13:45:39 +0000 | [diff] [blame] | 730 | result->field_offset = field_offset; |
| 731 | result->is_volatile = is_volatile ? 1u : 0u; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | |
| 735 | } // namespace art |