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