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