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