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 "driver/compiler_driver.h" |
| 22 | #include "invoke_type.h" |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 23 | #include "mirror/dex_cache-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 24 | #include "nodes.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 26 | #include "thread-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | // Function that returns whether an intrinsic is static/direct or virtual. |
| 32 | static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) { |
| 33 | switch (i) { |
| 34 | case Intrinsics::kNone: |
| 35 | return kInterface; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 36 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 37 | case Intrinsics::k ## Name: \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 38 | return IsStatic; |
| 39 | #include "intrinsics_list.h" |
| 40 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 41 | #undef INTRINSICS_LIST |
| 42 | #undef OPTIMIZING_INTRINSICS |
| 43 | } |
| 44 | return kInterface; |
| 45 | } |
| 46 | |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 47 | // Function that returns whether an intrinsic needs an environment or not. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 48 | static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) { |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 49 | switch (i) { |
| 50 | case Intrinsics::kNone: |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 51 | return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 52 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 53 | case Intrinsics::k ## Name: \ |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 54 | return NeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 55 | #include "intrinsics_list.h" |
| 56 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 57 | #undef INTRINSICS_LIST |
| 58 | #undef OPTIMIZING_INTRINSICS |
| 59 | } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 60 | return kNeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 61 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 62 | |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 63 | // Function that returns whether an intrinsic has side effects. |
| 64 | static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) { |
| 65 | switch (i) { |
| 66 | case Intrinsics::kNone: |
| 67 | return kAllSideEffects; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 68 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 69 | case Intrinsics::k ## Name: \ |
| 70 | return SideEffects; |
| 71 | #include "intrinsics_list.h" |
| 72 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 73 | #undef INTRINSICS_LIST |
| 74 | #undef OPTIMIZING_INTRINSICS |
| 75 | } |
| 76 | return kAllSideEffects; |
| 77 | } |
| 78 | |
| 79 | // Function that returns whether an intrinsic can throw exceptions. |
| 80 | static inline IntrinsicExceptions GetExceptions(Intrinsics i) { |
| 81 | switch (i) { |
| 82 | case Intrinsics::kNone: |
| 83 | return kCanThrow; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 84 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 85 | case Intrinsics::k ## Name: \ |
| 86 | return Exceptions; |
| 87 | #include "intrinsics_list.h" |
| 88 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 89 | #undef INTRINSICS_LIST |
| 90 | #undef OPTIMIZING_INTRINSICS |
| 91 | } |
| 92 | return kCanThrow; |
| 93 | } |
| 94 | |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 95 | static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) { |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 96 | // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual. |
| 97 | // |
| 98 | // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization |
| 99 | // failure occured. We might be in a situation where we have inlined a method that calls an |
| 100 | // intrinsic, but that method is in a different dex file on which we do not have a |
| 101 | // verified_method that would have helped the compiler driver sharpen the call. In that case, |
| 102 | // make sure that the intrinsic is actually for some final method (or in a final class), as |
| 103 | // otherwise the intrinsics setup is broken. |
| 104 | // |
| 105 | // For the last direction, we have intrinsics for virtual functions that will perform a check |
| 106 | // inline. If the precise type is known, however, the instruction will be sharpened to an |
| 107 | // InvokeStaticOrDirect. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 108 | InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 109 | InvokeType invoke_type = invoke->GetInvokeType(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 110 | switch (intrinsic_type) { |
| 111 | case kStatic: |
| 112 | return (invoke_type == kStatic); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 113 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 114 | case kDirect: |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 115 | if (invoke_type == kDirect) { |
| 116 | return true; |
| 117 | } |
| 118 | if (invoke_type == kVirtual) { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 119 | ArtMethod* art_method = invoke->GetResolvedMethod(); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 120 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 121 | return (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal()); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 122 | } |
| 123 | return false; |
| 124 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 125 | case kVirtual: |
| 126 | // Call might be devirtualized. |
| 127 | return (invoke_type == kVirtual || invoke_type == kDirect); |
| 128 | |
| 129 | default: |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 134 | void IntrinsicsRecognizer::Run() { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 135 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 136 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 137 | HBasicBlock* block = it.Current(); |
| 138 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 139 | inst_it.Advance()) { |
| 140 | HInstruction* inst = inst_it.Current(); |
| 141 | if (inst->IsInvoke()) { |
| 142 | HInvoke* invoke = inst->AsInvoke(); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 143 | ArtMethod* art_method = invoke->GetResolvedMethod(); |
| 144 | if (art_method != nullptr && art_method->IsIntrinsic()) { |
| 145 | Intrinsics intrinsic = static_cast<Intrinsics>(art_method->GetIntrinsic()); |
| 146 | if (!CheckInvokeType(intrinsic, invoke)) { |
| 147 | LOG(WARNING) << "Found an intrinsic with unexpected invoke type: " |
| 148 | << intrinsic << " for " |
| 149 | << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile()) |
| 150 | << invoke->DebugName(); |
| 151 | } else { |
| 152 | invoke->SetIntrinsic(intrinsic, |
| 153 | NeedsEnvironmentOrCache(intrinsic), |
| 154 | GetSideEffects(intrinsic), |
| 155 | GetExceptions(intrinsic)); |
| 156 | MaybeRecordStat(MethodCompilationStat::kIntrinsicRecognized); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) { |
| 165 | switch (intrinsic) { |
| 166 | case Intrinsics::kNone: |
David Brazdil | 109c89a | 2015-07-31 17:10:43 +0100 | [diff] [blame] | 167 | os << "None"; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 168 | break; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 169 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 170 | case Intrinsics::k ## Name: \ |
| 171 | os << # Name; \ |
| 172 | break; |
| 173 | #include "intrinsics_list.h" |
| 174 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 175 | #undef STATIC_INTRINSICS_LIST |
| 176 | #undef VIRTUAL_INTRINSICS_LIST |
| 177 | #undef OPTIMIZING_INTRINSICS |
| 178 | } |
| 179 | return os; |
| 180 | } |
| 181 | |
| 182 | } // namespace art |