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 | #ifndef ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_ |
| 18 | #define ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_ |
| 19 | |
| 20 | #include "base/macros.h" |
| 21 | #include "base/mutex.h" |
| 22 | #include "dex_file.h" |
| 23 | #include "dex_instruction.h" |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 24 | #include "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 | |
| 33 | namespace verifier { |
| 34 | class MethodVerifier; |
| 35 | } // namespace verifier |
| 36 | |
| 37 | enum InlineMethodOpcode : uint16_t { |
| 38 | kIntrinsicDoubleCvt, |
| 39 | kIntrinsicFloatCvt, |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 40 | kIntrinsicReverseBits, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 41 | kIntrinsicReverseBytes, |
Scott Wakeling | 611d339 | 2015-07-10 11:42:06 +0100 | [diff] [blame] | 42 | kIntrinsicNumberOfLeadingZeros, |
Scott Wakeling | 9ee23f4 | 2015-07-23 10:44:35 +0100 | [diff] [blame] | 43 | kIntrinsicNumberOfTrailingZeros, |
| 44 | kIntrinsicRotateRight, |
| 45 | kIntrinsicRotateLeft, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 46 | kIntrinsicAbsInt, |
| 47 | kIntrinsicAbsLong, |
| 48 | kIntrinsicAbsFloat, |
| 49 | kIntrinsicAbsDouble, |
| 50 | kIntrinsicMinMaxInt, |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 51 | kIntrinsicMinMaxLong, |
| 52 | kIntrinsicMinMaxFloat, |
| 53 | kIntrinsicMinMaxDouble, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 54 | kIntrinsicSqrt, |
Serban Constantinescu | 2eba1fa | 2014-07-31 19:07:17 +0100 | [diff] [blame] | 55 | kIntrinsicCeil, |
| 56 | kIntrinsicFloor, |
| 57 | kIntrinsicRint, |
| 58 | kIntrinsicRoundFloat, |
| 59 | kIntrinsicRoundDouble, |
Mathieu Chartier | cd48f2d | 2014-09-09 13:51:09 -0700 | [diff] [blame] | 60 | kIntrinsicReferenceGetReferent, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 61 | kIntrinsicCharAt, |
| 62 | kIntrinsicCompareTo, |
agicsaki | 7da072f | 2015-08-12 20:30:17 -0700 | [diff] [blame] | 63 | kIntrinsicEquals, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 64 | kIntrinsicGetCharsNoCheck, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 65 | kIntrinsicIsEmptyOrLength, |
| 66 | kIntrinsicIndexOf, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 67 | kIntrinsicNewStringFromBytes, |
| 68 | kIntrinsicNewStringFromChars, |
| 69 | kIntrinsicNewStringFromString, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 70 | kIntrinsicCurrentThread, |
| 71 | kIntrinsicPeek, |
| 72 | kIntrinsicPoke, |
| 73 | kIntrinsicCas, |
| 74 | kIntrinsicUnsafeGet, |
| 75 | kIntrinsicUnsafePut, |
DaniilSokolov | 70c4f06 | 2014-06-24 17:34:00 -0700 | [diff] [blame] | 76 | kIntrinsicSystemArrayCopyCharArray, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 77 | |
| 78 | kInlineOpNop, |
| 79 | kInlineOpReturnArg, |
| 80 | kInlineOpNonWideConst, |
| 81 | kInlineOpIGet, |
| 82 | kInlineOpIPut, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 83 | kInlineStringInit, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 84 | }; |
| 85 | std::ostream& operator<<(std::ostream& os, const InlineMethodOpcode& rhs); |
| 86 | |
| 87 | enum InlineMethodFlags : uint16_t { |
| 88 | kNoInlineMethodFlags = 0x0000, |
| 89 | kInlineIntrinsic = 0x0001, |
| 90 | kInlineSpecial = 0x0002, |
| 91 | }; |
| 92 | |
| 93 | // IntrinsicFlags are stored in InlineMethod::d::raw_data |
| 94 | enum IntrinsicFlags { |
| 95 | kIntrinsicFlagNone = 0, |
| 96 | |
| 97 | // kIntrinsicMinMaxInt |
| 98 | kIntrinsicFlagMax = kIntrinsicFlagNone, |
| 99 | kIntrinsicFlagMin = 1, |
| 100 | |
| 101 | // kIntrinsicIsEmptyOrLength |
| 102 | kIntrinsicFlagLength = kIntrinsicFlagNone, |
| 103 | kIntrinsicFlagIsEmpty = kIntrinsicFlagMin, |
| 104 | |
| 105 | // kIntrinsicIndexOf |
| 106 | kIntrinsicFlagBase0 = kIntrinsicFlagMin, |
| 107 | |
| 108 | // kIntrinsicUnsafeGet, kIntrinsicUnsafePut, kIntrinsicUnsafeCas |
| 109 | kIntrinsicFlagIsLong = kIntrinsicFlagMin, |
| 110 | // kIntrinsicUnsafeGet, kIntrinsicUnsafePut |
| 111 | kIntrinsicFlagIsVolatile = 2, |
| 112 | // kIntrinsicUnsafePut, kIntrinsicUnsafeCas |
| 113 | kIntrinsicFlagIsObject = 4, |
| 114 | // kIntrinsicUnsafePut |
| 115 | kIntrinsicFlagIsOrdered = 8, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 116 | |
| 117 | // kIntrinsicDoubleCvt, kIntrinsicFloatCvt. |
| 118 | kIntrinsicFlagToFloatingPoint = kIntrinsicFlagMin, |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | struct InlineIGetIPutData { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 122 | // 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] | 123 | uint16_t op_variant : 3; |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 124 | uint16_t method_is_static : 1; |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 125 | uint16_t object_arg : 4; |
| 126 | uint16_t src_arg : 4; // iput only |
Vladimir Marko | e1fced1 | 2014-04-04 14:52:53 +0100 | [diff] [blame] | 127 | 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] | 128 | uint16_t field_idx; |
| 129 | uint32_t is_volatile : 1; |
| 130 | uint32_t field_offset : 31; |
| 131 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 132 | static_assert(sizeof(InlineIGetIPutData) == sizeof(uint64_t), "Invalid size of InlineIGetIPutData"); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 133 | |
| 134 | struct InlineReturnArgData { |
| 135 | uint16_t arg; |
| 136 | uint16_t is_wide : 1; |
| 137 | uint16_t is_object : 1; |
| 138 | uint16_t reserved : 14; |
| 139 | uint32_t reserved2; |
| 140 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 141 | static_assert(sizeof(InlineReturnArgData) == sizeof(uint64_t), |
| 142 | "Invalid size of InlineReturnArgData"); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 143 | |
| 144 | struct InlineMethod { |
| 145 | InlineMethodOpcode opcode; |
| 146 | InlineMethodFlags flags; |
| 147 | union { |
| 148 | uint64_t data; |
| 149 | InlineIGetIPutData ifield_data; |
| 150 | InlineReturnArgData return_data; |
| 151 | } d; |
| 152 | }; |
| 153 | |
| 154 | class InlineMethodAnalyser { |
| 155 | public: |
| 156 | /** |
| 157 | * Analyse method code to determine if the method is a candidate for inlining. |
| 158 | * If it is, record the inlining data. |
| 159 | * |
| 160 | * @param verifier the method verifier holding data about the method to analyse. |
| 161 | * @param method placeholder for the inline method data. |
| 162 | * @return true if the method is a candidate for inlining, false otherwise. |
| 163 | */ |
| 164 | static bool AnalyseMethodCode(verifier::MethodVerifier* verifier, InlineMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 165 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 166 | |
| 167 | static constexpr bool IsInstructionIGet(Instruction::Code opcode) { |
| 168 | return Instruction::IGET <= opcode && opcode <= Instruction::IGET_SHORT; |
| 169 | } |
| 170 | |
| 171 | static constexpr bool IsInstructionIPut(Instruction::Code opcode) { |
| 172 | return Instruction::IPUT <= opcode && opcode <= Instruction::IPUT_SHORT; |
| 173 | } |
| 174 | |
| 175 | static constexpr uint16_t IGetVariant(Instruction::Code opcode) { |
| 176 | return opcode - Instruction::IGET; |
| 177 | } |
| 178 | |
| 179 | static constexpr uint16_t IPutVariant(Instruction::Code opcode) { |
| 180 | return opcode - Instruction::IPUT; |
| 181 | } |
| 182 | |
Vladimir Marko | c8f60a6 | 2014-04-02 15:24:05 +0100 | [diff] [blame] | 183 | // Determines whether the method is a synthetic accessor (method name starts with "access$"). |
| 184 | static bool IsSyntheticAccessor(MethodReference ref); |
| 185 | |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 186 | private: |
| 187 | static bool AnalyseReturnMethod(const DexFile::CodeItem* code_item, InlineMethod* result); |
| 188 | static bool AnalyseConstMethod(const DexFile::CodeItem* code_item, InlineMethod* result); |
| 189 | static bool AnalyseIGetMethod(verifier::MethodVerifier* verifier, InlineMethod* result) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 190 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 191 | static bool AnalyseIPutMethod(verifier::MethodVerifier* verifier, InlineMethod* result) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 192 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 193 | |
| 194 | // Can we fast path instance field access in a verified accessor? |
| 195 | // If yes, computes field's offset and volatility and whether the method is static or not. |
| 196 | static bool ComputeSpecialAccessorInfo(uint32_t field_idx, bool is_put, |
| 197 | verifier::MethodVerifier* verifier, |
| 198 | InlineIGetIPutData* result) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 199 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | e3e0260 | 2014-03-12 15:42:41 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace art |
| 203 | |
| 204 | #endif // ART_RUNTIME_QUICK_INLINE_METHOD_ANALYSER_H_ |