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; |
Aart Bik | 2a6aad9 | 2016-02-25 11:32:32 -0800 | [diff] [blame] | 133 | case kIntrinsicFloat2Int: |
| 134 | return Intrinsics::kFloatFloatToIntBits; |
| 135 | case kIntrinsicDouble2Long: |
| 136 | return Intrinsics::kDoubleDoubleToLongBits; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 137 | |
Aart Bik | 59c9454 | 2016-01-25 14:20:58 -0800 | [diff] [blame] | 138 | // Floating-point tests. |
| 139 | case kIntrinsicFloatIsInfinite: |
| 140 | return Intrinsics::kFloatIsInfinite; |
| 141 | case kIntrinsicDoubleIsInfinite: |
| 142 | return Intrinsics::kDoubleIsInfinite; |
| 143 | case kIntrinsicFloatIsNaN: |
| 144 | return Intrinsics::kFloatIsNaN; |
| 145 | case kIntrinsicDoubleIsNaN: |
| 146 | return Intrinsics::kDoubleIsNaN; |
| 147 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 148 | // Bit manipulations. |
| 149 | case kIntrinsicReverseBits: |
| 150 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 151 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 152 | return Intrinsics::kIntegerReverse; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 153 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 154 | return Intrinsics::kLongReverse; |
| 155 | default: |
| 156 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 157 | UNREACHABLE(); |
| 158 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 159 | case kIntrinsicReverseBytes: |
| 160 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 161 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 162 | return Intrinsics::kShortReverseBytes; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 163 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 164 | return Intrinsics::kIntegerReverseBytes; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 165 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 166 | return Intrinsics::kLongReverseBytes; |
| 167 | default: |
| 168 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 169 | UNREACHABLE(); |
| 170 | } |
Scott Wakeling | 9ee23f4 | 2015-07-23 10:44:35 +0100 | [diff] [blame] | 171 | case kIntrinsicRotateRight: |
| 172 | switch (GetType(method.d.data, true)) { |
| 173 | case Primitive::kPrimInt: |
| 174 | return Intrinsics::kIntegerRotateRight; |
| 175 | case Primitive::kPrimLong: |
| 176 | return Intrinsics::kLongRotateRight; |
| 177 | default: |
| 178 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 179 | UNREACHABLE(); |
| 180 | } |
| 181 | case kIntrinsicRotateLeft: |
| 182 | switch (GetType(method.d.data, true)) { |
| 183 | case Primitive::kPrimInt: |
| 184 | return Intrinsics::kIntegerRotateLeft; |
| 185 | case Primitive::kPrimLong: |
| 186 | return Intrinsics::kLongRotateLeft; |
| 187 | default: |
| 188 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 189 | UNREACHABLE(); |
| 190 | } |
| 191 | |
| 192 | // Misc data processing. |
Aart Bik | 3f67e69 | 2016-01-15 14:35:12 -0800 | [diff] [blame] | 193 | case kIntrinsicBitCount: |
| 194 | switch (GetType(method.d.data, true)) { |
| 195 | case Primitive::kPrimInt: |
| 196 | return Intrinsics::kIntegerBitCount; |
| 197 | case Primitive::kPrimLong: |
| 198 | return Intrinsics::kLongBitCount; |
| 199 | default: |
| 200 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 201 | UNREACHABLE(); |
| 202 | } |
Aart Bik | 59c9454 | 2016-01-25 14:20:58 -0800 | [diff] [blame] | 203 | case kIntrinsicCompare: |
| 204 | switch (GetType(method.d.data, true)) { |
| 205 | case Primitive::kPrimInt: |
| 206 | return Intrinsics::kIntegerCompare; |
| 207 | case Primitive::kPrimLong: |
| 208 | return Intrinsics::kLongCompare; |
| 209 | default: |
| 210 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 211 | UNREACHABLE(); |
| 212 | } |
| 213 | case kIntrinsicHighestOneBit: |
| 214 | switch (GetType(method.d.data, true)) { |
| 215 | case Primitive::kPrimInt: |
| 216 | return Intrinsics::kIntegerHighestOneBit; |
| 217 | case Primitive::kPrimLong: |
| 218 | return Intrinsics::kLongHighestOneBit; |
| 219 | default: |
| 220 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 221 | UNREACHABLE(); |
| 222 | } |
| 223 | case kIntrinsicLowestOneBit: |
| 224 | switch (GetType(method.d.data, true)) { |
| 225 | case Primitive::kPrimInt: |
| 226 | return Intrinsics::kIntegerLowestOneBit; |
| 227 | case Primitive::kPrimLong: |
| 228 | return Intrinsics::kLongLowestOneBit; |
| 229 | default: |
| 230 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 231 | UNREACHABLE(); |
| 232 | } |
Scott Wakeling | 611d339 | 2015-07-10 11:42:06 +0100 | [diff] [blame] | 233 | case kIntrinsicNumberOfLeadingZeros: |
| 234 | switch (GetType(method.d.data, true)) { |
| 235 | case Primitive::kPrimInt: |
| 236 | return Intrinsics::kIntegerNumberOfLeadingZeros; |
| 237 | case Primitive::kPrimLong: |
| 238 | return Intrinsics::kLongNumberOfLeadingZeros; |
| 239 | default: |
| 240 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 241 | UNREACHABLE(); |
| 242 | } |
Scott Wakeling | 9ee23f4 | 2015-07-23 10:44:35 +0100 | [diff] [blame] | 243 | case kIntrinsicNumberOfTrailingZeros: |
| 244 | switch (GetType(method.d.data, true)) { |
| 245 | case Primitive::kPrimInt: |
| 246 | return Intrinsics::kIntegerNumberOfTrailingZeros; |
| 247 | case Primitive::kPrimLong: |
| 248 | return Intrinsics::kLongNumberOfTrailingZeros; |
| 249 | default: |
| 250 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 251 | UNREACHABLE(); |
| 252 | } |
Aart Bik | 59c9454 | 2016-01-25 14:20:58 -0800 | [diff] [blame] | 253 | case kIntrinsicSignum: |
| 254 | switch (GetType(method.d.data, true)) { |
| 255 | case Primitive::kPrimInt: |
| 256 | return Intrinsics::kIntegerSignum; |
| 257 | case Primitive::kPrimLong: |
| 258 | return Intrinsics::kLongSignum; |
| 259 | default: |
| 260 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 261 | UNREACHABLE(); |
| 262 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 263 | |
| 264 | // Abs. |
| 265 | case kIntrinsicAbsDouble: |
| 266 | return Intrinsics::kMathAbsDouble; |
| 267 | case kIntrinsicAbsFloat: |
| 268 | return Intrinsics::kMathAbsFloat; |
| 269 | case kIntrinsicAbsInt: |
| 270 | return Intrinsics::kMathAbsInt; |
| 271 | case kIntrinsicAbsLong: |
| 272 | return Intrinsics::kMathAbsLong; |
| 273 | |
| 274 | // Min/max. |
| 275 | case kIntrinsicMinMaxDouble: |
| 276 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 277 | Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble; |
| 278 | case kIntrinsicMinMaxFloat: |
| 279 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 280 | Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat; |
| 281 | case kIntrinsicMinMaxInt: |
| 282 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 283 | Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt; |
| 284 | case kIntrinsicMinMaxLong: |
| 285 | return ((method.d.data & kIntrinsicFlagMin) == 0) ? |
| 286 | Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong; |
| 287 | |
Mark Mendell | a4f1220 | 2015-08-06 15:23:34 -0400 | [diff] [blame] | 288 | // More math builtins. |
| 289 | case kIntrinsicCos: |
| 290 | return Intrinsics::kMathCos; |
| 291 | case kIntrinsicSin: |
| 292 | return Intrinsics::kMathSin; |
| 293 | case kIntrinsicAcos: |
| 294 | return Intrinsics::kMathAcos; |
| 295 | case kIntrinsicAsin: |
| 296 | return Intrinsics::kMathAsin; |
| 297 | case kIntrinsicAtan: |
| 298 | return Intrinsics::kMathAtan; |
| 299 | case kIntrinsicAtan2: |
| 300 | return Intrinsics::kMathAtan2; |
| 301 | case kIntrinsicCbrt: |
| 302 | return Intrinsics::kMathCbrt; |
| 303 | case kIntrinsicCosh: |
| 304 | return Intrinsics::kMathCosh; |
| 305 | case kIntrinsicExp: |
| 306 | return Intrinsics::kMathExp; |
| 307 | case kIntrinsicExpm1: |
| 308 | return Intrinsics::kMathExpm1; |
| 309 | case kIntrinsicHypot: |
| 310 | return Intrinsics::kMathHypot; |
| 311 | case kIntrinsicLog: |
| 312 | return Intrinsics::kMathLog; |
| 313 | case kIntrinsicLog10: |
| 314 | return Intrinsics::kMathLog10; |
| 315 | case kIntrinsicNextAfter: |
| 316 | return Intrinsics::kMathNextAfter; |
| 317 | case kIntrinsicSinh: |
| 318 | return Intrinsics::kMathSinh; |
| 319 | case kIntrinsicTan: |
| 320 | return Intrinsics::kMathTan; |
| 321 | case kIntrinsicTanh: |
| 322 | return Intrinsics::kMathTanh; |
| 323 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 324 | // Misc math. |
| 325 | case kIntrinsicSqrt: |
| 326 | return Intrinsics::kMathSqrt; |
| 327 | case kIntrinsicCeil: |
| 328 | return Intrinsics::kMathCeil; |
| 329 | case kIntrinsicFloor: |
| 330 | return Intrinsics::kMathFloor; |
| 331 | case kIntrinsicRint: |
| 332 | return Intrinsics::kMathRint; |
| 333 | case kIntrinsicRoundDouble: |
| 334 | return Intrinsics::kMathRoundDouble; |
| 335 | case kIntrinsicRoundFloat: |
| 336 | return Intrinsics::kMathRoundFloat; |
| 337 | |
| 338 | // System.arraycopy. |
| 339 | case kIntrinsicSystemArrayCopyCharArray: |
| 340 | return Intrinsics::kSystemArrayCopyChar; |
| 341 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 342 | case kIntrinsicSystemArrayCopy: |
| 343 | return Intrinsics::kSystemArrayCopy; |
| 344 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 345 | // Thread.currentThread. |
| 346 | case kIntrinsicCurrentThread: |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 347 | return Intrinsics::kThreadCurrentThread; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 348 | |
| 349 | // Memory.peek. |
| 350 | case kIntrinsicPeek: |
| 351 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 352 | case Primitive::kPrimByte: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 353 | return Intrinsics::kMemoryPeekByte; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 354 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 355 | return Intrinsics::kMemoryPeekShortNative; |
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 Intrinsics::kMemoryPeekIntNative; |
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 Intrinsics::kMemoryPeekLongNative; |
| 360 | default: |
| 361 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 362 | UNREACHABLE(); |
| 363 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 364 | |
| 365 | // Memory.poke. |
| 366 | case kIntrinsicPoke: |
| 367 | switch (GetType(method.d.data, true)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 368 | case Primitive::kPrimByte: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 369 | return Intrinsics::kMemoryPokeByte; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 370 | case Primitive::kPrimShort: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 371 | return Intrinsics::kMemoryPokeShortNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 372 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 373 | return Intrinsics::kMemoryPokeIntNative; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 374 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 375 | return Intrinsics::kMemoryPokeLongNative; |
| 376 | default: |
| 377 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 378 | UNREACHABLE(); |
| 379 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 380 | |
| 381 | // String. |
| 382 | case kIntrinsicCharAt: |
| 383 | return Intrinsics::kStringCharAt; |
| 384 | case kIntrinsicCompareTo: |
| 385 | return Intrinsics::kStringCompareTo; |
agicsaki | 7da072f | 2015-08-12 20:30:17 -0700 | [diff] [blame] | 386 | case kIntrinsicEquals: |
| 387 | return Intrinsics::kStringEquals; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 388 | case kIntrinsicGetCharsNoCheck: |
| 389 | return Intrinsics::kStringGetCharsNoCheck; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 390 | case kIntrinsicIsEmptyOrLength: |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 391 | return ((method.d.data & kIntrinsicFlagIsEmpty) == 0) ? |
| 392 | Intrinsics::kStringLength : Intrinsics::kStringIsEmpty; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 393 | case kIntrinsicIndexOf: |
| 394 | return ((method.d.data & kIntrinsicFlagBase0) == 0) ? |
| 395 | Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 396 | case kIntrinsicNewStringFromBytes: |
| 397 | return Intrinsics::kStringNewStringFromBytes; |
| 398 | case kIntrinsicNewStringFromChars: |
| 399 | return Intrinsics::kStringNewStringFromChars; |
| 400 | case kIntrinsicNewStringFromString: |
| 401 | return Intrinsics::kStringNewStringFromString; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 402 | |
| 403 | case kIntrinsicCas: |
| 404 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 405 | case Primitive::kPrimNot: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 406 | return Intrinsics::kUnsafeCASObject; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 407 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 408 | return Intrinsics::kUnsafeCASInt; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 409 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 410 | return Intrinsics::kUnsafeCASLong; |
| 411 | default: |
| 412 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 413 | UNREACHABLE(); |
| 414 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 415 | case kIntrinsicUnsafeGet: { |
| 416 | const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile); |
| 417 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 418 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 419 | return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 420 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 421 | return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 422 | case Primitive::kPrimNot: |
| 423 | return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 424 | default: |
| 425 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 426 | UNREACHABLE(); |
| 427 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 428 | } |
| 429 | case kIntrinsicUnsafePut: { |
| 430 | enum Sync { kNoSync, kVolatile, kOrdered }; |
| 431 | const Sync sync = |
| 432 | ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile : |
| 433 | ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered : |
| 434 | kNoSync; |
| 435 | switch (GetType(method.d.data, false)) { |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 436 | case Primitive::kPrimInt: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 437 | switch (sync) { |
| 438 | case kNoSync: |
| 439 | return Intrinsics::kUnsafePut; |
| 440 | case kVolatile: |
| 441 | return Intrinsics::kUnsafePutVolatile; |
| 442 | case kOrdered: |
| 443 | return Intrinsics::kUnsafePutOrdered; |
| 444 | } |
| 445 | break; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 446 | case Primitive::kPrimLong: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 447 | switch (sync) { |
| 448 | case kNoSync: |
| 449 | return Intrinsics::kUnsafePutLong; |
| 450 | case kVolatile: |
| 451 | return Intrinsics::kUnsafePutLongVolatile; |
| 452 | case kOrdered: |
| 453 | return Intrinsics::kUnsafePutLongOrdered; |
| 454 | } |
| 455 | break; |
Andreas Gampe | 878d58c | 2015-01-15 23:24:00 -0800 | [diff] [blame] | 456 | case Primitive::kPrimNot: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 457 | switch (sync) { |
| 458 | case kNoSync: |
| 459 | return Intrinsics::kUnsafePutObject; |
| 460 | case kVolatile: |
| 461 | return Intrinsics::kUnsafePutObjectVolatile; |
| 462 | case kOrdered: |
| 463 | return Intrinsics::kUnsafePutObjectOrdered; |
| 464 | } |
| 465 | break; |
| 466 | default: |
| 467 | LOG(FATAL) << "Unknown/unsupported op size " << method.d.data; |
| 468 | UNREACHABLE(); |
| 469 | } |
| 470 | break; |
| 471 | } |
| 472 | |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 473 | // 1.8. |
| 474 | case kIntrinsicUnsafeGetAndAddInt: |
| 475 | return Intrinsics::kUnsafeGetAndAddInt; |
| 476 | case kIntrinsicUnsafeGetAndAddLong: |
| 477 | return Intrinsics::kUnsafeGetAndAddLong; |
| 478 | case kIntrinsicUnsafeGetAndSetInt: |
| 479 | return Intrinsics::kUnsafeGetAndSetInt; |
| 480 | case kIntrinsicUnsafeGetAndSetLong: |
| 481 | return Intrinsics::kUnsafeGetAndSetLong; |
| 482 | case kIntrinsicUnsafeGetAndSetObject: |
| 483 | return Intrinsics::kUnsafeGetAndSetObject; |
| 484 | case kIntrinsicUnsafeLoadFence: |
| 485 | return Intrinsics::kUnsafeLoadFence; |
| 486 | case kIntrinsicUnsafeStoreFence: |
| 487 | return Intrinsics::kUnsafeStoreFence; |
| 488 | case kIntrinsicUnsafeFullFence: |
| 489 | return Intrinsics::kUnsafeFullFence; |
| 490 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 491 | // Virtual cases. |
| 492 | |
| 493 | case kIntrinsicReferenceGetReferent: |
| 494 | return Intrinsics::kReferenceGetReferent; |
| 495 | |
| 496 | // Quick inliner cases. Remove after refactoring. They are here so that we can use the |
| 497 | // compiler to warn on missing cases. |
| 498 | |
| 499 | case kInlineOpNop: |
| 500 | case kInlineOpReturnArg: |
| 501 | case kInlineOpNonWideConst: |
| 502 | case kInlineOpIGet: |
| 503 | case kInlineOpIPut: |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 504 | case kInlineOpConstructor: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 505 | return Intrinsics::kNone; |
| 506 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 507 | // String init cases, not intrinsics. |
| 508 | |
| 509 | case kInlineStringInit: |
| 510 | return Intrinsics::kNone; |
| 511 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 512 | // No default case to make the compiler warn on missing cases. |
| 513 | } |
| 514 | return Intrinsics::kNone; |
| 515 | } |
| 516 | |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 517 | static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke, const DexFile& dex_file) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 518 | // The DexFileMethodInliner should have checked whether the methods are agreeing with |
| 519 | // what we expect, i.e., static methods are called as such. Add another check here for |
| 520 | // our expectations: |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 521 | // |
| 522 | // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual. |
| 523 | // |
| 524 | // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization |
| 525 | // failure occured. We might be in a situation where we have inlined a method that calls an |
| 526 | // intrinsic, but that method is in a different dex file on which we do not have a |
| 527 | // verified_method that would have helped the compiler driver sharpen the call. In that case, |
| 528 | // make sure that the intrinsic is actually for some final method (or in a final class), as |
| 529 | // otherwise the intrinsics setup is broken. |
| 530 | // |
| 531 | // For the last direction, we have intrinsics for virtual functions that will perform a check |
| 532 | // inline. If the precise type is known, however, the instruction will be sharpened to an |
| 533 | // InvokeStaticOrDirect. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 534 | InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame^] | 535 | InvokeType invoke_type = invoke->GetInvokeType(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 536 | switch (intrinsic_type) { |
| 537 | case kStatic: |
| 538 | return (invoke_type == kStatic); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 539 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 540 | case kDirect: |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 541 | if (invoke_type == kDirect) { |
| 542 | return true; |
| 543 | } |
| 544 | if (invoke_type == kVirtual) { |
| 545 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 546 | ScopedObjectAccess soa(Thread::Current()); |
| 547 | ArtMethod* art_method = |
| 548 | class_linker->FindDexCache(soa.Self(), dex_file)->GetResolvedMethod( |
| 549 | invoke->GetDexMethodIndex(), class_linker->GetImagePointerSize()); |
| 550 | return art_method != nullptr && |
| 551 | (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal()); |
| 552 | } |
| 553 | return false; |
| 554 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 555 | case kVirtual: |
| 556 | // Call might be devirtualized. |
| 557 | return (invoke_type == kVirtual || invoke_type == kDirect); |
| 558 | |
| 559 | default: |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod. |
| 565 | void IntrinsicsRecognizer::Run() { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 566 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 567 | HBasicBlock* block = it.Current(); |
| 568 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 569 | inst_it.Advance()) { |
| 570 | HInstruction* inst = inst_it.Current(); |
| 571 | if (inst->IsInvoke()) { |
| 572 | HInvoke* invoke = inst->AsInvoke(); |
| 573 | InlineMethod method; |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 574 | const DexFile& dex_file = invoke->GetDexFile(); |
| 575 | DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(&dex_file); |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 576 | DCHECK(inliner != nullptr); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 577 | if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) { |
Chris Larsen | 16ba2b4 | 2015-11-02 10:58:31 -0800 | [diff] [blame] | 578 | Intrinsics intrinsic = GetIntrinsic(method); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 579 | |
| 580 | if (intrinsic != Intrinsics::kNone) { |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 581 | if (!CheckInvokeType(intrinsic, invoke, dex_file)) { |
Andreas Gampe | a14b9fe | 2015-08-24 22:49:59 +0000 | [diff] [blame] | 582 | LOG(WARNING) << "Found an intrinsic with unexpected invoke type: " |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 583 | << intrinsic << " for " |
| 584 | << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile()) |
| 585 | << invoke->DebugName(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 586 | } else { |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 587 | invoke->SetIntrinsic(intrinsic, |
| 588 | NeedsEnvironmentOrCache(intrinsic), |
| 589 | GetSideEffects(intrinsic), |
| 590 | GetExceptions(intrinsic)); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 591 | MaybeRecordStat(MethodCompilationStat::kIntrinsicRecognized); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) { |
| 601 | switch (intrinsic) { |
| 602 | case Intrinsics::kNone: |
David Brazdil | 109c89a | 2015-07-31 17:10:43 +0100 | [diff] [blame] | 603 | os << "None"; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 604 | break; |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 605 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 606 | case Intrinsics::k ## Name: \ |
| 607 | os << # Name; \ |
| 608 | break; |
| 609 | #include "intrinsics_list.h" |
| 610 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 611 | #undef STATIC_INTRINSICS_LIST |
| 612 | #undef VIRTUAL_INTRINSICS_LIST |
| 613 | #undef OPTIMIZING_INTRINSICS |
| 614 | } |
| 615 | return os; |
| 616 | } |
| 617 | |
| 618 | } // namespace art |