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 | |
Andreas Gampe | b95c74b | 2017-04-20 19:43:21 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_ |
| 18 | #define ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_ |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 19 | |
| 20 | #include "base/macros.h" |
| 21 | #include "base/mutex.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file.h" |
| 23 | #include "dex/dex_instruction.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 24 | #include "dex/method_reference.h" |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * NOTE: This code is part of the quick compiler. It lives in the runtime |
| 28 | * only to allow the debugger to check whether a method has been inlined. |
| 29 | */ |
| 30 | |
| 31 | namespace art { |
| 32 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 33 | class CodeItemDataAccessor; |
| 34 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 35 | namespace verifier { |
| 36 | class MethodVerifier; |
| 37 | } // namespace verifier |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 38 | class ArtMethod; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 39 | |
| 40 | enum InlineMethodOpcode : uint16_t { |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 41 | kInlineOpNop, |
| 42 | kInlineOpReturnArg, |
| 43 | kInlineOpNonWideConst, |
| 44 | kInlineOpIGet, |
| 45 | kInlineOpIPut, |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 46 | kInlineOpConstructor, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct InlineIGetIPutData { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 50 | // The op_variant below is DexMemAccessType but the runtime doesn't know that enumeration. |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 51 | uint16_t op_variant : 3; |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 52 | uint16_t method_is_static : 1; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 53 | uint16_t object_arg : 4; |
| 54 | uint16_t src_arg : 4; // iput only |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 55 | uint16_t return_arg_plus1 : 4; // iput only, method argument to return + 1, 0 = return void. |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 56 | uint16_t field_idx; |
| 57 | uint32_t is_volatile : 1; |
| 58 | uint32_t field_offset : 31; |
| 59 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 60 | static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData"); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 61 | |
| 62 | struct InlineReturnArgData { |
| 63 | uint16_t arg; |
| 64 | uint16_t is_wide : 1; |
| 65 | uint16_t is_object : 1; |
| 66 | uint16_t reserved : 14; |
| 67 | uint32_t reserved2; |
| 68 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 69 | static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t), |
| 70 | "Invalid size of InlineReturnArgData"); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 71 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 72 | struct InlineConstructorData { |
| 73 | // There can be up to 3 IPUTs, unused fields are marked with kNoDexIndex16. |
| 74 | uint16_t iput0_field_index; |
| 75 | uint16_t iput1_field_index; |
| 76 | uint16_t iput2_field_index; |
| 77 | uint16_t iput0_arg : 4; |
| 78 | uint16_t iput1_arg : 4; |
| 79 | uint16_t iput2_arg : 4; |
| 80 | uint16_t reserved : 4; |
| 81 | }; |
| 82 | static_assert(sizeof(InlineConstructorData) == sizeof(uint64_t), |
| 83 | "Invalid size of InlineConstructorData"); |
| 84 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 85 | struct InlineMethod { |
| 86 | InlineMethodOpcode opcode; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 87 | union { |
| 88 | uint64_t data; |
| 89 | InlineIGetIPutData ifield_data; |
| 90 | InlineReturnArgData return_data; |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 91 | InlineConstructorData constructor_data; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 92 | } d; |
| 93 | }; |
| 94 | |
| 95 | class InlineMethodAnalyser { |
| 96 | public: |
| 97 | /** |
| 98 | * Analyse method code to determine if the method is a candidate for inlining. |
| 99 | * If it is, record the inlining data. |
| 100 | * |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 101 | * @return true if the method is a candidate for inlining, false otherwise. |
| 102 | */ |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 103 | static bool AnalyseMethodCode(ArtMethod* method, InlineMethod* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 104 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 105 | |
| 106 | static constexpr bool IsInstructionIGet(Instruction::Code opcode) { |
| 107 | return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT; |
| 108 | } |
| 109 | |
| 110 | static constexpr bool IsInstructionIPut(Instruction::Code opcode) { |
| 111 | return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT; |
| 112 | } |
| 113 | |
| 114 | static constexpr uint16_t IGetVariant(Instruction::Code opcode) { |
| 115 | return opcode - Instruction::IGET; |
| 116 | } |
| 117 | |
| 118 | static constexpr uint16_t IPutVariant(Instruction::Code opcode) { |
| 119 | return opcode - Instruction::IPUT; |
| 120 | } |
| 121 | |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 122 | // Determines whether the method is a synthetic accessor (method name starts with "access$"). |
| 123 | static bool IsSyntheticAccessor(MethodReference ref); |
| 124 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 125 | private: |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 126 | static bool AnalyseMethodCode(const CodeItemDataAccessor* code_item, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 127 | const MethodReference& method_ref, |
| 128 | bool is_static, |
| 129 | ArtMethod* method, |
| 130 | InlineMethod* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 131 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 132 | static bool AnalyseReturnMethod(const CodeItemDataAccessor* code_item, InlineMethod* result); |
| 133 | static bool AnalyseConstMethod(const CodeItemDataAccessor* code_item, InlineMethod* result); |
| 134 | static bool AnalyseIGetMethod(const CodeItemDataAccessor* code_item, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 135 | const MethodReference& method_ref, |
| 136 | bool is_static, |
| 137 | ArtMethod* method, |
| 138 | InlineMethod* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 139 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 140 | static bool AnalyseIPutMethod(const CodeItemDataAccessor* code_item, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 141 | const MethodReference& method_ref, |
| 142 | bool is_static, |
| 143 | ArtMethod* method, |
| 144 | InlineMethod* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 146 | |
| 147 | // Can we fast path instance field access in a verified accessor? |
| 148 | // If yes, computes field's offset and volatility and whether the method is static or not. |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 149 | static bool ComputeSpecialAccessorInfo(ArtMethod* method, |
| 150 | uint32_t field_idx, |
| 151 | bool is_put, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 152 | InlineIGetIPutData* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 153 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace art |
| 157 | |
Andreas Gampe | b95c74b | 2017-04-20 19:43:21 -0700 | [diff] [blame] | 158 | #endif // ART_COMPILER_DEX_INLINE_METHOD_ANALYSER_H_ |