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