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