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