Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "intrinsics.h" |
| 18 | |
Nicolas Geoffray | 4daa0b4 | 2015-08-20 14:05:14 +0100 | [diff] [blame^] | 19 | #include "art_method.h" |
| 20 | #include "class_linker.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 21 | #include "dex/quick/dex_file_method_inliner.h" |
| 22 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
| 23 | #include "driver/compiler_driver.h" |
| 24 | #include "invoke_type.h" |
Nicolas Geoffray | 4daa0b4 | 2015-08-20 14:05:14 +0100 | [diff] [blame^] | 25 | #include "mirror/dex_cache-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 26 | #include "nodes.h" |
| 27 | #include "quick/inline_method_analyser.h" |
Nicolas Geoffray | 4daa0b4 | 2015-08-20 14:05:14 +0100 | [diff] [blame^] | 28 | #include "scoped_thread_state_change.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | #include "utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | // Function that returns whether an intrinsic is static/direct or virtual. |
| 34 | static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) { |
| 35 | switch (i) { |
| 36 | case Intrinsics::kNone: |
| 37 | return kInterface; // Non-sensical for intrinsic. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 38 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 39 | case Intrinsics::k ## Name: \ |
| 40 | return IsStatic; |
| 41 | #include "intrinsics_list.h" |
| 42 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 43 | #undef INTRINSICS_LIST |
| 44 | #undef OPTIMIZING_INTRINSICS |
| 45 | } |
| 46 | return kInterface; |
| 47 | } |
| 48 | |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 49 | // Function that returns whether an intrinsic needs an environment or not. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 50 | static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) { |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 51 | switch (i) { |
| 52 | case Intrinsics::kNone: |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 53 | return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic. |
| 54 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \ |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 55 | case Intrinsics::k ## Name: \ |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 56 | return NeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 57 | #include "intrinsics_list.h" |
| 58 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 59 | #undef INTRINSICS_LIST |
| 60 | #undef OPTIMIZING_INTRINSICS |
| 61 | } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 62 | return kNeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 63 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 64 | |
| 65 | static Primitive::Type GetType(uint64_t data, bool is_op_size) { |
| 66 | if (is_op_size) { |
| 67 | switch (static_cast<OpSize>(data)) { |
| 68 | case kSignedByte: |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 69 | return Primitive::kPrimByte; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 70 | case kSignedHalf: |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 71 | return Primitive::kPrimShort; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 72 | case k32: |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 73 | return Primitive::kPrimInt; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 74 | case k64: |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 75 | return Primitive::kPrimLong; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 76 | default: |
| 77 | LOG(FATAL) << "Unknown/unsupported op size " << data; |
| 78 | UNREACHABLE(); |
| 79 | } |
| 80 | } else { |
| 81 | if ((data & kIntrinsicFlagIsLong) != 0) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 82 | return Primitive::kPrimLong; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 83 | } |
| 84 | if ((data & kIntrinsicFlagIsObject) != 0) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 85 | return Primitive::kPrimNot; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 86 | } |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 87 | return Primitive::kPrimInt; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
agicsaki | 6cff09a | 2015-08-12 21:20:43 -0700 | [diff] [blame] | 91 | static Intrinsics GetIntrinsic(InlineMethod method, InstructionSet instruction_set) { |
| 92 | if (instruction_set == kMips || instruction_set == kMips64) { |
| 93 | return Intrinsics::kNone; |
| 94 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 95 | switch (method.opcode) { |
| 96 | // Floating-point conversions. |
| 97 | case kIntrinsicDoubleCvt: |
| 98 | return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ? |
| 99 | Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble; |
| 100 | case kIntrinsicFloatCvt: |
| 101 | return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ? |
| 102 | Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat; |
| 103 | |
| 104 | // Bit manipulations. |
| 105 | case kIntrinsicReverseBits: |
| 106 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 107 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 108 | return Intrinsics::kIntegerReverse; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 109 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 110 | return Intrinsics::kLongReverse; |
| 111 | default: |
| 112 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 113 | UNREACHABLE(); |
| 114 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 115 | case kIntrinsicReverseBytes: |
| 116 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 117 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 118 | return Intrinsics::kShortReverseBytes; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 119 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 120 | return Intrinsics::kIntegerReverseBytes; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 121 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 122 | return Intrinsics::kLongReverseBytes; |
| 123 | default: |
| 124 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 125 | UNREACHABLE(); |
| 126 | } |
Scott Wakeling | 611d339 | 2015-07-10 11:42:06 +0100 | [diff] [blame] | 127 | case kIntrinsicNumberOfLeadingZeros: |
| 128 | switch (GetType(method.d.data, true)) { |
| 129 | case Primitive::kPrimInt: |
| 130 | return Intrinsics::kIntegerNumberOfLeadingZeros; |
| 131 | case Primitive::kPrimLong: |
| 132 | return Intrinsics::kLongNumberOfLeadingZeros; |
| 133 | default: |
| 134 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 135 | UNREACHABLE(); |
| 136 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 137 | |
| 138 | // Abs. |
| 139 | case kIntrinsicAbsDouble: |
| 140 | return Intrinsics::kMathAbsDouble; |
| 141 | case kIntrinsicAbsFloat: |
| 142 | return Intrinsics::kMathAbsFloat; |
| 143 | case kIntrinsicAbsInt: |
| 144 | return Intrinsics::kMathAbsInt; |
| 145 | case kIntrinsicAbsLong: |
| 146 | return Intrinsics::kMathAbsLong; |
| 147 | |
| 148 | // Min/max. |
| 149 | case kIntrinsicMinMaxDouble: |
| 150 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 151 | Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble; |
| 152 | case kIntrinsicMinMaxFloat: |
| 153 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 154 | Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat; |
| 155 | case kIntrinsicMinMaxInt: |
| 156 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 157 | Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt; |
| 158 | case kIntrinsicMinMaxLong: |
| 159 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 160 | Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong; |
| 161 | |
| 162 | // Misc math. |
| 163 | case kIntrinsicSqrt: |
| 164 | return Intrinsics::kMathSqrt; |
| 165 | case kIntrinsicCeil: |
| 166 | return Intrinsics::kMathCeil; |
| 167 | case kIntrinsicFloor: |
| 168 | return Intrinsics::kMathFloor; |
| 169 | case kIntrinsicRint: |
| 170 | return Intrinsics::kMathRint; |
| 171 | case kIntrinsicRoundDouble: |
| 172 | return Intrinsics::kMathRoundDouble; |
| 173 | case kIntrinsicRoundFloat: |
| 174 | return Intrinsics::kMathRoundFloat; |
| 175 | |
| 176 | // System.arraycopy. |
| 177 | case kIntrinsicSystemArrayCopyCharArray: |
| 178 | return Intrinsics::kSystemArrayCopyChar; |
| 179 | |
| 180 | // Thread.currentThread. |
| 181 | case kIntrinsicCurrentThread: |
| 182 | return Intrinsics::kThreadCurrentThread; |
| 183 | |
| 184 | // Memory.peek. |
| 185 | case kIntrinsicPeek: |
| 186 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 187 | case Primitive::kPrimByte: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 188 | return Intrinsics::kMemoryPeekByte; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 189 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 190 | return Intrinsics::kMemoryPeekShortNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 191 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 192 | return Intrinsics::kMemoryPeekIntNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 193 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 194 | return Intrinsics::kMemoryPeekLongNative; |
| 195 | default: |
| 196 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 197 | UNREACHABLE(); |
| 198 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 199 | |
| 200 | // Memory.poke. |
| 201 | case kIntrinsicPoke: |
| 202 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 203 | case Primitive::kPrimByte: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 204 | return Intrinsics::kMemoryPokeByte; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 205 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 206 | return Intrinsics::kMemoryPokeShortNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 207 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 208 | return Intrinsics::kMemoryPokeIntNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 209 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 210 | return Intrinsics::kMemoryPokeLongNative; |
| 211 | default: |
| 212 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 213 | UNREACHABLE(); |
| 214 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 215 | |
| 216 | // String. |
| 217 | case kIntrinsicCharAt: |
| 218 | return Intrinsics::kStringCharAt; |
| 219 | case kIntrinsicCompareTo: |
| 220 | return Intrinsics::kStringCompareTo; |
agicsaki | 7da072f | 2015-08-12 20:30:17 -0700 | [diff] [blame] | 221 | case kIntrinsicEquals: |
| 222 | return Intrinsics::kStringEquals; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 223 | case kIntrinsicGetCharsNoCheck: |
| 224 | return Intrinsics::kStringGetCharsNoCheck; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 225 | case kIntrinsicIsEmptyOrLength: |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 226 | // The inliner can handle these two cases - and this is the preferred approach |
| 227 | // since after inlining the call is no longer visible (as opposed to waiting |
| 228 | // until codegen to handle intrinsic). |
| 229 | return Intrinsics::kNone; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 230 | case kIntrinsicIndexOf: |
| 231 | return ((method.d.data & kIntrinsicFlagBase0) == 0) ? |
| 232 | Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 233 | case kIntrinsicNewStringFromBytes: |
| 234 | return Intrinsics::kStringNewStringFromBytes; |
| 235 | case kIntrinsicNewStringFromChars: |
| 236 | return Intrinsics::kStringNewStringFromChars; |
| 237 | case kIntrinsicNewStringFromString: |
| 238 | return Intrinsics::kStringNewStringFromString; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 239 | |
| 240 | case kIntrinsicCas: |
| 241 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 242 | case Primitive::kPrimNot: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 243 | return Intrinsics::kUnsafeCASObject; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 244 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 245 | return Intrinsics::kUnsafeCASInt; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 246 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 247 | return Intrinsics::kUnsafeCASLong; |
| 248 | default: |
| 249 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 250 | UNREACHABLE(); |
| 251 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 252 | case kIntrinsicUnsafeGet: { |
| 253 | const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile); |
| 254 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 255 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 256 | return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 257 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 258 | return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 259 | case Primitive::kPrimNot: |
| 260 | return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 261 | default: |
| 262 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 263 | UNREACHABLE(); |
| 264 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 265 | } |
| 266 | case kIntrinsicUnsafePut: { |
| 267 | enum Sync { kNoSync, kVolatile, kOrdered }; |
| 268 | const Sync sync = |
| 269 | ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile : |
| 270 | ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered : |
| 271 | kNoSync; |
| 272 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 273 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 274 | switch (sync) { |
| 275 | case kNoSync: |
| 276 | return Intrinsics::kUnsafePut; |
| 277 | case kVolatile: |
| 278 | return Intrinsics::kUnsafePutVolatile; |
| 279 | case kOrdered: |
| 280 | return Intrinsics::kUnsafePutOrdered; |
| 281 | } |
| 282 | break; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 283 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 284 | switch (sync) { |
| 285 | case kNoSync: |
| 286 | return Intrinsics::kUnsafePutLong; |
| 287 | case kVolatile: |
| 288 | return Intrinsics::kUnsafePutLongVolatile; |
| 289 | case kOrdered: |
| 290 | return Intrinsics::kUnsafePutLongOrdered; |
| 291 | } |
| 292 | break; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 293 | case Primitive::kPrimNot: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 294 | switch (sync) { |
| 295 | case kNoSync: |
| 296 | return Intrinsics::kUnsafePutObject; |
| 297 | case kVolatile: |
| 298 | return Intrinsics::kUnsafePutObjectVolatile; |
| 299 | case kOrdered: |
| 300 | return Intrinsics::kUnsafePutObjectOrdered; |
| 301 | } |
| 302 | break; |
| 303 | default: |
| 304 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 305 | UNREACHABLE(); |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | // Virtual cases. |
| 311 | |
| 312 | case kIntrinsicReferenceGetReferent: |
| 313 | return Intrinsics::kReferenceGetReferent; |
| 314 | |
| 315 | // Quick inliner cases. Remove after refactoring. They are here so that we can use the |
| 316 | // compiler to warn on missing cases. |
| 317 | |
| 318 | case kInlineOpNop: |
| 319 | case kInlineOpReturnArg: |
| 320 | case kInlineOpNonWideConst: |
| 321 | case kInlineOpIGet: |
| 322 | case kInlineOpIPut: |
| 323 | return Intrinsics::kNone; |
| 324 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 325 | // String init cases, not intrinsics. |
| 326 | |
| 327 | case kInlineStringInit: |
| 328 | return Intrinsics::kNone; |
| 329 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 330 | // No default case to make the compiler warn on missing cases. |
| 331 | } |
| 332 | return Intrinsics::kNone; |
| 333 | } |
| 334 | |
| 335 | static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) { |
| 336 | // The DexFileMethodInliner should have checked whether the methods are agreeing with |
| 337 | // what we expect, i.e., static methods are called as such. Add another check here for |
| 338 | // our expectations: |
| 339 | // Whenever the intrinsic is marked as static-or-direct, report an error if we find an |
| 340 | // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual |
| 341 | // functions that will perform a check inline. If the precise type is known, however, |
| 342 | // the instruction will be sharpened to an InvokeStaticOrDirect. |
| 343 | InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic); |
| 344 | InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ? |
| 345 | invoke->AsInvokeStaticOrDirect()->GetInvokeType() : |
| 346 | invoke->IsInvokeVirtual() ? kVirtual : kSuper; |
| 347 | switch (intrinsic_type) { |
| 348 | case kStatic: |
| 349 | return (invoke_type == kStatic); |
| 350 | case kDirect: |
| 351 | return (invoke_type == kDirect); |
| 352 | case kVirtual: |
| 353 | // Call might be devirtualized. |
| 354 | return (invoke_type == kVirtual || invoke_type == kDirect); |
| 355 | |
| 356 | default: |
| 357 | return false; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod. |
| 362 | void IntrinsicsRecognizer::Run() { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 363 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 364 | HBasicBlock* block = it.Current(); |
| 365 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 366 | inst_it.Advance()) { |
| 367 | HInstruction* inst = inst_it.Current(); |
| 368 | if (inst->IsInvoke()) { |
| 369 | HInvoke* invoke = inst->AsInvoke(); |
| 370 | InlineMethod method; |
Nicolas Geoffray | 4daa0b4 | 2015-08-20 14:05:14 +0100 | [diff] [blame^] | 371 | const DexFile& dex_file = invoke->GetDexFile(); |
| 372 | DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(&dex_file); |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 373 | DCHECK(inliner != nullptr); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 374 | if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) { |
agicsaki | 6cff09a | 2015-08-12 21:20:43 -0700 | [diff] [blame] | 375 | Intrinsics intrinsic = GetIntrinsic(method, graph_->GetInstructionSet()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 376 | |
| 377 | if (intrinsic != Intrinsics::kNone) { |
| 378 | if (!CheckInvokeType(intrinsic, invoke)) { |
Nicolas Geoffray | 4daa0b4 | 2015-08-20 14:05:14 +0100 | [diff] [blame^] | 379 | // We might be in a situation where we have inlined a method that calls an intrinsic, |
| 380 | // but that method is in a different dex file on which we do not have a |
| 381 | // verified_method that would have helped the compiler driver sharpen the call. |
| 382 | // We can still ensure the invoke types match by checking whether the called method |
| 383 | // is final or is in a final class. |
| 384 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 385 | { |
| 386 | ScopedObjectAccess soa(Thread::Current()); |
| 387 | ArtMethod* art_method = class_linker->FindDexCache(dex_file)->GetResolvedMethod( |
| 388 | invoke->GetDexMethodIndex(), class_linker->GetImagePointerSize()); |
| 389 | DCHECK(art_method != nullptr); |
| 390 | if (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal()) { |
| 391 | invoke->SetIntrinsic(intrinsic, NeedsEnvironmentOrCache(intrinsic)); |
| 392 | } else { |
| 393 | LOG(WARNING) << "Found an intrinsic with unexpected invoke type: " |
| 394 | << intrinsic << " for " |
| 395 | << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile()) |
| 396 | << invoke->DebugName(); |
| 397 | } |
| 398 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 399 | } else { |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 400 | invoke->SetIntrinsic(intrinsic, NeedsEnvironmentOrCache(intrinsic)); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) { |
| 410 | switch (intrinsic) { |
| 411 | case Intrinsics::kNone: |
David Brazdil | 109c89a | 2015-07-31 17:10:43 +0100 | [diff] [blame] | 412 | os << "None"; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 413 | break; |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 414 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 415 | case Intrinsics::k ## Name: \ |
| 416 | os << # Name; \ |
| 417 | break; |
| 418 | #include "intrinsics_list.h" |
| 419 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 420 | #undef STATIC_INTRINSICS_LIST |
| 421 | #undef VIRTUAL_INTRINSICS_LIST |
| 422 | #undef OPTIMIZING_INTRINSICS |
| 423 | } |
| 424 | return os; |
| 425 | } |
| 426 | |
| 427 | } // namespace art |