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