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" |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame^] | 22 | #include "driver/compiler_options.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 23 | #include "invoke_type.h" |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 24 | #include "mirror/dex_cache-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 25 | #include "nodes.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 27 | #include "thread-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 28 | #include "utils.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | // Function that returns whether an intrinsic is static/direct or virtual. |
| 33 | static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) { |
| 34 | switch (i) { |
| 35 | case Intrinsics::kNone: |
| 36 | return kInterface; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 37 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 38 | case Intrinsics::k ## Name: \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 39 | return IsStatic; |
| 40 | #include "intrinsics_list.h" |
| 41 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 42 | #undef INTRINSICS_LIST |
| 43 | #undef OPTIMIZING_INTRINSICS |
| 44 | } |
| 45 | return kInterface; |
| 46 | } |
| 47 | |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 48 | // Function that returns whether an intrinsic needs an environment or not. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 49 | static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) { |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 50 | switch (i) { |
| 51 | case Intrinsics::kNone: |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 52 | return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 53 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 54 | case Intrinsics::k ## Name: \ |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 55 | return NeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 56 | #include "intrinsics_list.h" |
| 57 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 58 | #undef INTRINSICS_LIST |
| 59 | #undef OPTIMIZING_INTRINSICS |
| 60 | } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 61 | return kNeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 62 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 63 | |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 64 | // Function that returns whether an intrinsic has side effects. |
| 65 | static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) { |
| 66 | switch (i) { |
| 67 | case Intrinsics::kNone: |
| 68 | return kAllSideEffects; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 69 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 70 | case Intrinsics::k ## Name: \ |
| 71 | return SideEffects; |
| 72 | #include "intrinsics_list.h" |
| 73 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 74 | #undef INTRINSICS_LIST |
| 75 | #undef OPTIMIZING_INTRINSICS |
| 76 | } |
| 77 | return kAllSideEffects; |
| 78 | } |
| 79 | |
| 80 | // Function that returns whether an intrinsic can throw exceptions. |
| 81 | static inline IntrinsicExceptions GetExceptions(Intrinsics i) { |
| 82 | switch (i) { |
| 83 | case Intrinsics::kNone: |
| 84 | return kCanThrow; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 85 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 86 | case Intrinsics::k ## Name: \ |
| 87 | return Exceptions; |
| 88 | #include "intrinsics_list.h" |
| 89 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 90 | #undef INTRINSICS_LIST |
| 91 | #undef OPTIMIZING_INTRINSICS |
| 92 | } |
| 93 | return kCanThrow; |
| 94 | } |
| 95 | |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 96 | static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) { |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 97 | // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual. |
| 98 | // |
| 99 | // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization |
| 100 | // failure occured. We might be in a situation where we have inlined a method that calls an |
| 101 | // intrinsic, but that method is in a different dex file on which we do not have a |
| 102 | // verified_method that would have helped the compiler driver sharpen the call. In that case, |
| 103 | // make sure that the intrinsic is actually for some final method (or in a final class), as |
| 104 | // otherwise the intrinsics setup is broken. |
| 105 | // |
| 106 | // For the last direction, we have intrinsics for virtual functions that will perform a check |
| 107 | // inline. If the precise type is known, however, the instruction will be sharpened to an |
| 108 | // InvokeStaticOrDirect. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 109 | InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 110 | InvokeType invoke_type = invoke->GetInvokeType(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 111 | switch (intrinsic_type) { |
| 112 | case kStatic: |
| 113 | return (invoke_type == kStatic); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 114 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 115 | case kDirect: |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 116 | if (invoke_type == kDirect) { |
| 117 | return true; |
| 118 | } |
| 119 | if (invoke_type == kVirtual) { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 120 | ArtMethod* art_method = invoke->GetResolvedMethod(); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 121 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 122 | return (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal()); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 123 | } |
| 124 | return false; |
| 125 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 126 | case kVirtual: |
| 127 | // Call might be devirtualized. |
| 128 | return (invoke_type == kVirtual || invoke_type == kDirect); |
| 129 | |
| 130 | default: |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 135 | void IntrinsicsRecognizer::Run() { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 136 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 137 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 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 " |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 149 | << art_method->PrettyMethod() |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 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 | |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame^] | 182 | void IntrinsicVisitor::ComputeIntegerValueOfLocations(HInvoke* invoke, |
| 183 | CodeGenerator* codegen, |
| 184 | Location return_location, |
| 185 | Location first_argument_location) { |
| 186 | if (Runtime::Current()->IsAotCompiler()) { |
| 187 | if (codegen->GetCompilerOptions().IsBootImage() || |
| 188 | codegen->GetCompilerOptions().GetCompilePic()) { |
| 189 | // TODO(ngeoffray): Support boot image compilation. |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | IntegerValueOfInfo info = ComputeIntegerValueOfInfo(); |
| 195 | |
| 196 | // Most common case is that we have found all we needed (classes are initialized |
| 197 | // and in the boot image). Bail if not. |
| 198 | if (info.integer_cache == nullptr || |
| 199 | info.integer == nullptr || |
| 200 | info.cache == nullptr || |
| 201 | info.value_offset == 0 || |
| 202 | // low and high cannot be 0, per the spec. |
| 203 | info.low == 0 || |
| 204 | info.high == 0) { |
| 205 | LOG(INFO) << "Integer.valueOf will not be optimized"; |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // The intrinsic will call if it needs to allocate a j.l.Integer. |
| 210 | LocationSummary* locations = new (invoke->GetBlock()->GetGraph()->GetArena()) LocationSummary( |
| 211 | invoke, LocationSummary::kCallOnMainOnly, kIntrinsified); |
| 212 | if (!invoke->InputAt(0)->IsConstant()) { |
| 213 | locations->SetInAt(0, Location::RequiresRegister()); |
| 214 | } |
| 215 | locations->AddTemp(first_argument_location); |
| 216 | locations->SetOut(return_location); |
| 217 | } |
| 218 | |
| 219 | IntrinsicVisitor::IntegerValueOfInfo IntrinsicVisitor::ComputeIntegerValueOfInfo() { |
| 220 | // Note that we could cache all of the data looked up here. but there's no good |
| 221 | // location for it. We don't want to add it to WellKnownClasses, to avoid creating global |
| 222 | // jni values. Adding it as state to the compiler singleton seems like wrong |
| 223 | // separation of concerns. |
| 224 | // The need for this data should be pretty rare though. |
| 225 | |
| 226 | // The most common case is that the classes are in the boot image and initialized, |
| 227 | // which is easy to generate code for. We bail if not. |
| 228 | Thread* self = Thread::Current(); |
| 229 | ScopedObjectAccess soa(self); |
| 230 | Runtime* runtime = Runtime::Current(); |
| 231 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 232 | gc::Heap* heap = runtime->GetHeap(); |
| 233 | IntegerValueOfInfo info; |
| 234 | info.integer_cache = class_linker->FindSystemClass(self, "Ljava/lang/Integer$IntegerCache;"); |
| 235 | if (info.integer_cache == nullptr) { |
| 236 | self->ClearException(); |
| 237 | return info; |
| 238 | } |
| 239 | if (!heap->ObjectIsInBootImageSpace(info.integer_cache) || !info.integer_cache->IsInitialized()) { |
| 240 | // Optimization only works if the class is initialized and in the boot image. |
| 241 | return info; |
| 242 | } |
| 243 | info.integer = class_linker->FindSystemClass(self, "Ljava/lang/Integer;"); |
| 244 | if (info.integer == nullptr) { |
| 245 | self->ClearException(); |
| 246 | return info; |
| 247 | } |
| 248 | if (!heap->ObjectIsInBootImageSpace(info.integer) || !info.integer->IsInitialized()) { |
| 249 | // Optimization only works if the class is initialized and in the boot image. |
| 250 | return info; |
| 251 | } |
| 252 | |
| 253 | ArtField* field = info.integer_cache->FindDeclaredStaticField("cache", "[Ljava/lang/Integer;"); |
| 254 | if (field == nullptr) { |
| 255 | return info; |
| 256 | } |
| 257 | info.cache = static_cast<mirror::ObjectArray<mirror::Object>*>( |
| 258 | field->GetObject(info.integer_cache).Ptr()); |
| 259 | if (info.cache == nullptr) { |
| 260 | return info; |
| 261 | } |
| 262 | |
| 263 | if (!heap->ObjectIsInBootImageSpace(info.cache)) { |
| 264 | // Optimization only works if the object is in the boot image. |
| 265 | return info; |
| 266 | } |
| 267 | |
| 268 | field = info.integer->FindDeclaredInstanceField("value", "I"); |
| 269 | if (field == nullptr) { |
| 270 | return info; |
| 271 | } |
| 272 | info.value_offset = field->GetOffset().Int32Value(); |
| 273 | |
| 274 | field = info.integer_cache->FindDeclaredStaticField("low", "I"); |
| 275 | if (field == nullptr) { |
| 276 | return info; |
| 277 | } |
| 278 | info.low = field->GetInt(info.integer_cache); |
| 279 | |
| 280 | field = info.integer_cache->FindDeclaredStaticField("high", "I"); |
| 281 | if (field == nullptr) { |
| 282 | return info; |
| 283 | } |
| 284 | info.high = field->GetInt(info.integer_cache); |
| 285 | |
| 286 | DCHECK_EQ(info.cache->GetLength(), info.high - info.low + 1); |
| 287 | return info; |
| 288 | } |
| 289 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 290 | } // namespace art |