Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "cha.h" |
| 18 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 20 | #include "jit/jit.h" |
| 21 | #include "jit/jit_code_cache.h" |
| 22 | #include "runtime.h" |
| 23 | #include "scoped_thread_state_change-inl.h" |
| 24 | #include "stack.h" |
| 25 | #include "thread.h" |
| 26 | #include "thread_list.h" |
| 27 | #include "thread_pool.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | void ClassHierarchyAnalysis::AddDependency(ArtMethod* method, |
| 32 | ArtMethod* dependent_method, |
| 33 | OatQuickMethodHeader* dependent_header) { |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 34 | const auto it = cha_dependency_map_.insert( |
| 35 | decltype(cha_dependency_map_)::value_type(method, ListOfDependentPairs())).first; |
| 36 | it->second.push_back({dependent_method, dependent_header}); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 39 | static const ClassHierarchyAnalysis::ListOfDependentPairs s_empty_vector; |
| 40 | |
| 41 | const ClassHierarchyAnalysis::ListOfDependentPairs& ClassHierarchyAnalysis::GetDependents( |
| 42 | ArtMethod* method) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 43 | auto it = cha_dependency_map_.find(method); |
| 44 | if (it != cha_dependency_map_.end()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 45 | return it->second; |
| 46 | } |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 47 | return s_empty_vector; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 50 | void ClassHierarchyAnalysis::RemoveAllDependenciesFor(ArtMethod* method) { |
| 51 | cha_dependency_map_.erase(method); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void ClassHierarchyAnalysis::RemoveDependentsWithMethodHeaders( |
| 55 | const std::unordered_set<OatQuickMethodHeader*>& method_headers) { |
| 56 | // Iterate through all entries in the dependency map and remove any entry that |
| 57 | // contains one of those in method_headers. |
| 58 | for (auto map_it = cha_dependency_map_.begin(); map_it != cha_dependency_map_.end(); ) { |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 59 | ListOfDependentPairs& dependents = map_it->second; |
| 60 | dependents.erase( |
| 61 | std::remove_if( |
| 62 | dependents.begin(), |
| 63 | dependents.end(), |
| 64 | [&method_headers](MethodAndMethodHeaderPair& dependent) { |
| 65 | return method_headers.find(dependent.second) != method_headers.end(); |
| 66 | }), |
| 67 | dependents.end()); |
| 68 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 69 | // Remove the map entry if there are no more dependents. |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 70 | if (dependents.empty()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 71 | map_it = cha_dependency_map_.erase(map_it); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 72 | } else { |
| 73 | map_it++; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // This stack visitor walks the stack and for compiled code with certain method |
| 79 | // headers, sets the should_deoptimize flag on stack to 1. |
| 80 | // TODO: also set the register value to 1 when should_deoptimize is allocated in |
| 81 | // a register. |
| 82 | class CHAStackVisitor FINAL : public StackVisitor { |
| 83 | public: |
| 84 | CHAStackVisitor(Thread* thread_in, |
| 85 | Context* context, |
| 86 | const std::unordered_set<OatQuickMethodHeader*>& method_headers) |
| 87 | : StackVisitor(thread_in, context, StackVisitor::StackWalkKind::kSkipInlinedFrames), |
| 88 | method_headers_(method_headers) { |
| 89 | } |
| 90 | |
| 91 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 92 | ArtMethod* method = GetMethod(); |
Mingyao Yang | 7b9a83f | 2016-12-13 12:28:31 -0800 | [diff] [blame] | 93 | // Avoid types of methods that do not have an oat quick method header. |
| 94 | if (method == nullptr || |
| 95 | method->IsRuntimeMethod() || |
| 96 | method->IsNative() || |
| 97 | method->IsProxyMethod()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | if (GetCurrentQuickFrame() == nullptr) { |
| 101 | // Not compiled code. |
| 102 | return true; |
| 103 | } |
| 104 | // Method may have multiple versions of compiled code. Check |
| 105 | // the method header to see if it has should_deoptimize flag. |
| 106 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
Mingyao Yang | 7b9a83f | 2016-12-13 12:28:31 -0800 | [diff] [blame] | 107 | DCHECK(method_header != nullptr); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 108 | if (!method_header->HasShouldDeoptimizeFlag()) { |
| 109 | // This compiled version doesn't have should_deoptimize flag. Skip. |
| 110 | return true; |
| 111 | } |
| 112 | auto it = std::find(method_headers_.begin(), method_headers_.end(), method_header); |
| 113 | if (it == method_headers_.end()) { |
| 114 | // Not in the list of method headers that should be deoptimized. |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | // The compiled code on stack is not valid anymore. Need to deoptimize. |
| 119 | SetShouldDeoptimizeFlag(); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | void SetShouldDeoptimizeFlag() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 126 | QuickMethodFrameInfo frame_info = GetCurrentQuickFrameInfo(); |
| 127 | size_t frame_size = frame_info.FrameSizeInBytes(); |
| 128 | uint8_t* sp = reinterpret_cast<uint8_t*>(GetCurrentQuickFrame()); |
| 129 | size_t core_spill_size = POPCOUNT(frame_info.CoreSpillMask()) * |
| 130 | GetBytesPerGprSpillLocation(kRuntimeISA); |
| 131 | size_t fpu_spill_size = POPCOUNT(frame_info.FpSpillMask()) * |
| 132 | GetBytesPerFprSpillLocation(kRuntimeISA); |
| 133 | size_t offset = frame_size - core_spill_size - fpu_spill_size - kShouldDeoptimizeFlagSize; |
| 134 | uint8_t* should_deoptimize_addr = sp + offset; |
| 135 | // Set deoptimization flag to 1. |
| 136 | DCHECK(*should_deoptimize_addr == 0 || *should_deoptimize_addr == 1); |
| 137 | *should_deoptimize_addr = 1; |
| 138 | } |
| 139 | |
| 140 | // Set of method headers for compiled code that should be deoptimized. |
| 141 | const std::unordered_set<OatQuickMethodHeader*>& method_headers_; |
| 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(CHAStackVisitor); |
| 144 | }; |
| 145 | |
| 146 | class CHACheckpoint FINAL : public Closure { |
| 147 | public: |
| 148 | explicit CHACheckpoint(const std::unordered_set<OatQuickMethodHeader*>& method_headers) |
| 149 | : barrier_(0), |
| 150 | method_headers_(method_headers) {} |
| 151 | |
| 152 | void Run(Thread* thread) OVERRIDE { |
| 153 | // Note thread and self may not be equal if thread was already suspended at |
| 154 | // the point of the request. |
| 155 | Thread* self = Thread::Current(); |
| 156 | ScopedObjectAccess soa(self); |
| 157 | CHAStackVisitor visitor(thread, nullptr, method_headers_); |
| 158 | visitor.WalkStack(); |
| 159 | barrier_.Pass(self); |
| 160 | } |
| 161 | |
| 162 | void WaitForThreadsToRunThroughCheckpoint(size_t threads_running_checkpoint) { |
| 163 | Thread* self = Thread::Current(); |
| 164 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
| 165 | barrier_.Increment(self, threads_running_checkpoint); |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | // The barrier to be passed through and for the requestor to wait upon. |
| 170 | Barrier barrier_; |
| 171 | // List of method headers for invalidated compiled code. |
| 172 | const std::unordered_set<OatQuickMethodHeader*>& method_headers_; |
| 173 | |
| 174 | DISALLOW_COPY_AND_ASSIGN(CHACheckpoint); |
| 175 | }; |
| 176 | |
| 177 | void ClassHierarchyAnalysis::VerifyNonSingleImplementation(mirror::Class* verify_class, |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 178 | uint16_t verify_index, |
| 179 | ArtMethod* excluded_method) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 180 | // Grab cha_lock_ to make sure all single-implementation updates are seen. |
| 181 | PointerSize image_pointer_size = |
| 182 | Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 183 | MutexLock cha_mu(Thread::Current(), *Locks::cha_lock_); |
| 184 | while (verify_class != nullptr) { |
| 185 | if (verify_index >= verify_class->GetVTableLength()) { |
| 186 | return; |
| 187 | } |
| 188 | ArtMethod* verify_method = verify_class->GetVTableEntry(verify_index, image_pointer_size); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 189 | if (verify_method != excluded_method) { |
| 190 | DCHECK(!verify_method->HasSingleImplementation()) |
| 191 | << "class: " << verify_class->PrettyClass() |
Mingyao Yang | 37c8e5c | 2017-02-10 11:25:05 -0800 | [diff] [blame] | 192 | << " verify_method: " << verify_method->PrettyMethod(true) |
| 193 | << " excluded_method: " << excluded_method->PrettyMethod(true); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 194 | if (verify_method->IsAbstract()) { |
| 195 | DCHECK(verify_method->GetSingleImplementation(image_pointer_size) == nullptr); |
| 196 | } |
| 197 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 198 | verify_class = verify_class->GetSuperClass(); |
| 199 | } |
| 200 | } |
| 201 | |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 202 | void ClassHierarchyAnalysis::CheckVirtualMethodSingleImplementationInfo( |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 203 | Handle<mirror::Class> klass, |
| 204 | ArtMethod* virtual_method, |
| 205 | ArtMethod* method_in_super, |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 206 | std::unordered_set<ArtMethod*>& invalidated_single_impl_methods, |
| 207 | PointerSize pointer_size) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 208 | // TODO: if klass is not instantiable, virtual_method isn't invocable yet so |
| 209 | // even if it overrides, it doesn't invalidate single-implementation |
| 210 | // assumption. |
| 211 | |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 212 | DCHECK((virtual_method != method_in_super) || virtual_method->IsAbstract()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 213 | DCHECK(method_in_super->GetDeclaringClass()->IsResolved()) << "class isn't resolved"; |
| 214 | // If virtual_method doesn't come from a default interface method, it should |
| 215 | // be supplied by klass. |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 216 | DCHECK(virtual_method == method_in_super || |
| 217 | virtual_method->IsCopied() || |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 218 | virtual_method->GetDeclaringClass() == klass.Get()); |
| 219 | |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 220 | // To make updating single-implementation flags simple, we always maintain the following |
| 221 | // invariant: |
| 222 | // Say all virtual methods in the same vtable slot, starting from the bottom child class |
| 223 | // to super classes, is a sequence of unique methods m3, m2, m1, ... (after removing duplicate |
| 224 | // methods for inherited methods). |
| 225 | // For example for the following class hierarchy, |
| 226 | // class A { void m() { ... } } |
| 227 | // class B extends A { void m() { ... } } |
| 228 | // class C extends B {} |
| 229 | // class D extends C { void m() { ... } } |
| 230 | // the sequence is D.m(), B.m(), A.m(). |
| 231 | // The single-implementation status for that sequence of methods begin with one or two true's, |
| 232 | // then become all falses. The only case where two true's are possible is for one abstract |
| 233 | // method m and one non-abstract method mImpl that overrides method m. |
| 234 | // With the invariant, when linking in a new class, we only need to at most update one or |
| 235 | // two methods in the sequence for their single-implementation status, in order to maintain |
| 236 | // the invariant. |
| 237 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 238 | if (!method_in_super->HasSingleImplementation()) { |
| 239 | // method_in_super already has multiple implementations. All methods in the |
| 240 | // same vtable slots in its super classes should have |
| 241 | // non-single-implementation already. |
| 242 | if (kIsDebugBuild) { |
| 243 | VerifyNonSingleImplementation(klass->GetSuperClass()->GetSuperClass(), |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 244 | method_in_super->GetMethodIndex(), |
| 245 | nullptr /* excluded_method */); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 246 | } |
| 247 | return; |
| 248 | } |
| 249 | |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 250 | uint16_t method_index = method_in_super->GetMethodIndex(); |
| 251 | if (method_in_super->IsAbstract()) { |
| 252 | if (kIsDebugBuild) { |
| 253 | // An abstract method should have made all methods in the same vtable |
| 254 | // slot above it in the class hierarchy having non-single-implementation. |
| 255 | mirror::Class* super_super = klass->GetSuperClass()->GetSuperClass(); |
| 256 | VerifyNonSingleImplementation(super_super, |
| 257 | method_index, |
| 258 | method_in_super); |
| 259 | } |
| 260 | |
| 261 | if (virtual_method->IsAbstract()) { |
| 262 | // SUPER: abstract, VIRTUAL: abstract. |
| 263 | if (method_in_super == virtual_method) { |
| 264 | DCHECK(klass->IsInstantiable()); |
| 265 | // An instantiable subclass hasn't provided a concrete implementation of |
| 266 | // the abstract method. Invoking method_in_super may throw AbstractMethodError. |
| 267 | // This is an uncommon case, so we simply treat method_in_super as not |
| 268 | // having single-implementation. |
| 269 | invalidated_single_impl_methods.insert(method_in_super); |
| 270 | return; |
| 271 | } else { |
| 272 | // One abstract method overrides another abstract method. This is an uncommon |
| 273 | // case. We simply treat method_in_super as not having single-implementation. |
| 274 | invalidated_single_impl_methods.insert(method_in_super); |
| 275 | return; |
| 276 | } |
| 277 | } else { |
| 278 | // SUPER: abstract, VIRTUAL: non-abstract. |
| 279 | // A non-abstract method overrides an abstract method. |
| 280 | if (method_in_super->GetSingleImplementation(pointer_size) == nullptr) { |
| 281 | // Abstract method_in_super has no implementation yet. |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 282 | // We need to grab cha_lock_ since there may be multiple class linking |
| 283 | // going on that can check/modify the single-implementation flag/method |
| 284 | // of method_in_super. |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 285 | MutexLock cha_mu(Thread::Current(), *Locks::cha_lock_); |
| 286 | if (!method_in_super->HasSingleImplementation()) { |
| 287 | return; |
| 288 | } |
| 289 | if (method_in_super->GetSingleImplementation(pointer_size) == nullptr) { |
| 290 | // virtual_method becomes the first implementation for method_in_super. |
| 291 | method_in_super->SetSingleImplementation(virtual_method, pointer_size); |
| 292 | // Keep method_in_super's single-implementation status. |
| 293 | return; |
| 294 | } |
| 295 | // Fall through to invalidate method_in_super's single-implementation status. |
| 296 | } |
| 297 | // Abstract method_in_super already got one implementation. |
| 298 | // Invalidate method_in_super's single-implementation status. |
| 299 | invalidated_single_impl_methods.insert(method_in_super); |
| 300 | return; |
| 301 | } |
| 302 | } else { |
| 303 | if (virtual_method->IsAbstract()) { |
| 304 | // SUPER: non-abstract, VIRTUAL: abstract. |
| 305 | // An abstract method overrides a non-abstract method. This is an uncommon |
| 306 | // case, we simply treat both methods as not having single-implementation. |
| 307 | invalidated_single_impl_methods.insert(virtual_method); |
| 308 | // Fall-through to handle invalidating method_in_super of its |
| 309 | // single-implementation status. |
| 310 | } |
| 311 | |
| 312 | // SUPER: non-abstract, VIRTUAL: non-abstract/abstract(fall-through from previous if). |
| 313 | // Invalidate method_in_super's single-implementation status. |
| 314 | invalidated_single_impl_methods.insert(method_in_super); |
| 315 | |
| 316 | // method_in_super might be the single-implementation of another abstract method, |
| 317 | // which should be also invalidated of its single-implementation status. |
| 318 | mirror::Class* super_super = klass->GetSuperClass()->GetSuperClass(); |
| 319 | while (super_super != nullptr && |
| 320 | method_index < super_super->GetVTableLength()) { |
| 321 | ArtMethod* method_in_super_super = super_super->GetVTableEntry(method_index, pointer_size); |
| 322 | if (method_in_super_super != method_in_super) { |
| 323 | if (method_in_super_super->IsAbstract()) { |
| 324 | if (method_in_super_super->HasSingleImplementation()) { |
| 325 | // Invalidate method_in_super's single-implementation status. |
| 326 | invalidated_single_impl_methods.insert(method_in_super_super); |
| 327 | // No need to further traverse up the class hierarchy since if there |
| 328 | // are cases that one abstract method overrides another method, we |
| 329 | // should have made that method having non-single-implementation already. |
| 330 | } else { |
| 331 | // method_in_super_super is already non-single-implementation. |
| 332 | // No need to further traverse up the class hierarchy. |
| 333 | } |
| 334 | } else { |
| 335 | DCHECK(!method_in_super_super->HasSingleImplementation()); |
| 336 | // No need to further traverse up the class hierarchy since two non-abstract |
| 337 | // methods (method_in_super and method_in_super_super) should have set all |
| 338 | // other methods (abstract or not) in the vtable slot to be non-single-implementation. |
| 339 | } |
| 340 | |
| 341 | if (kIsDebugBuild) { |
| 342 | VerifyNonSingleImplementation(super_super->GetSuperClass(), |
| 343 | method_index, |
| 344 | method_in_super_super); |
| 345 | } |
| 346 | // No need to go any further. |
| 347 | return; |
| 348 | } else { |
| 349 | super_super = super_super->GetSuperClass(); |
| 350 | } |
| 351 | } |
| 352 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 355 | void ClassHierarchyAnalysis::CheckInterfaceMethodSingleImplementationInfo( |
| 356 | Handle<mirror::Class> klass, |
| 357 | ArtMethod* interface_method, |
| 358 | ArtMethod* implementation_method, |
| 359 | std::unordered_set<ArtMethod*>& invalidated_single_impl_methods, |
| 360 | PointerSize pointer_size) { |
| 361 | DCHECK(klass->IsInstantiable()); |
| 362 | DCHECK(interface_method->IsAbstract() || interface_method->IsDefault()); |
| 363 | |
| 364 | if (!interface_method->HasSingleImplementation()) { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | if (implementation_method->IsAbstract()) { |
| 369 | // An instantiable class doesn't supply an implementation for |
| 370 | // interface_method. Invoking the interface method on the class will throw |
| 371 | // AbstractMethodError. This is an uncommon case, so we simply treat |
| 372 | // interface_method as not having single-implementation. |
| 373 | invalidated_single_impl_methods.insert(interface_method); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | // We need to grab cha_lock_ since there may be multiple class linking going |
| 378 | // on that can check/modify the single-implementation flag/method of |
| 379 | // interface_method. |
| 380 | MutexLock cha_mu(Thread::Current(), *Locks::cha_lock_); |
| 381 | // Do this check again after we grab cha_lock_. |
| 382 | if (!interface_method->HasSingleImplementation()) { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | ArtMethod* single_impl = interface_method->GetSingleImplementation(pointer_size); |
| 387 | if (single_impl == nullptr) { |
| 388 | // implementation_method becomes the first implementation for |
| 389 | // interface_method. |
| 390 | interface_method->SetSingleImplementation(implementation_method, pointer_size); |
| 391 | // Keep interface_method's single-implementation status. |
| 392 | return; |
| 393 | } |
| 394 | DCHECK(!single_impl->IsAbstract()); |
| 395 | if (single_impl->GetDeclaringClass() == implementation_method->GetDeclaringClass()) { |
| 396 | // Same implementation. Since implementation_method may be a copy of a default |
| 397 | // method, we need to check the declaring class for equality. |
| 398 | return; |
| 399 | } |
| 400 | // Another implementation for interface_method. |
| 401 | invalidated_single_impl_methods.insert(interface_method); |
| 402 | } |
| 403 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 404 | void ClassHierarchyAnalysis::InitSingleImplementationFlag(Handle<mirror::Class> klass, |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 405 | ArtMethod* method, |
| 406 | PointerSize pointer_size) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 407 | DCHECK(method->IsCopied() || method->GetDeclaringClass() == klass.Get()); |
| 408 | if (klass->IsFinal() || method->IsFinal()) { |
| 409 | // Final classes or methods do not need CHA for devirtualization. |
| 410 | // This frees up modifier bits for intrinsics which currently are only |
| 411 | // used for static methods or methods of final classes. |
| 412 | return; |
| 413 | } |
Mingyao Yang | 37c8e5c | 2017-02-10 11:25:05 -0800 | [diff] [blame] | 414 | if (method->IsAbstract()) { |
| 415 | // single-implementation of abstract method shares the same field |
| 416 | // that's used for JNI function of native method. It's fine since a method |
| 417 | // cannot be both abstract and native. |
| 418 | DCHECK(!method->IsNative()) << "Abstract method cannot be native"; |
| 419 | |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 420 | if (method->GetDeclaringClass()->IsInstantiable()) { |
| 421 | // Rare case, but we do accept it (such as 800-smali/smali/b_26143249.smali). |
| 422 | // Do not attempt to devirtualize it. |
| 423 | method->SetHasSingleImplementation(false); |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 424 | DCHECK(method->GetSingleImplementation(pointer_size) == nullptr); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 425 | } else { |
| 426 | // Abstract method starts with single-implementation flag set and null |
| 427 | // implementation method. |
| 428 | method->SetHasSingleImplementation(true); |
| 429 | DCHECK(method->GetSingleImplementation(pointer_size) == nullptr); |
| 430 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 431 | } else { |
| 432 | method->SetHasSingleImplementation(true); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 433 | // Single implementation of non-abstract method is itself. |
| 434 | DCHECK_EQ(method->GetSingleImplementation(pointer_size), method); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | void ClassHierarchyAnalysis::UpdateAfterLoadingOf(Handle<mirror::Class> klass) { |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 439 | PointerSize image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 440 | if (klass->IsInterface()) { |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 441 | for (ArtMethod& method : klass->GetDeclaredVirtualMethods(image_pointer_size)) { |
| 442 | DCHECK(method.IsAbstract() || method.IsDefault()); |
| 443 | InitSingleImplementationFlag(klass, &method, image_pointer_size); |
| 444 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 445 | return; |
| 446 | } |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 447 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 448 | mirror::Class* super_class = klass->GetSuperClass(); |
| 449 | if (super_class == nullptr) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // Keeps track of all methods whose single-implementation assumption |
| 454 | // is invalidated by linking `klass`. |
| 455 | std::unordered_set<ArtMethod*> invalidated_single_impl_methods; |
| 456 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 457 | // Do an entry-by-entry comparison of vtable contents with super's vtable. |
| 458 | for (int32_t i = 0; i < super_class->GetVTableLength(); ++i) { |
| 459 | ArtMethod* method = klass->GetVTableEntry(i, image_pointer_size); |
| 460 | ArtMethod* method_in_super = super_class->GetVTableEntry(i, image_pointer_size); |
| 461 | if (method == method_in_super) { |
| 462 | // vtable slot entry is inherited from super class. |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 463 | if (method->IsAbstract() && klass->IsInstantiable()) { |
| 464 | // An instantiable class that inherits an abstract method is treated as |
| 465 | // supplying an implementation that throws AbstractMethodError. |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 466 | CheckVirtualMethodSingleImplementationInfo(klass, |
| 467 | method, |
| 468 | method_in_super, |
| 469 | invalidated_single_impl_methods, |
| 470 | image_pointer_size); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 471 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 472 | continue; |
| 473 | } |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 474 | InitSingleImplementationFlag(klass, method, image_pointer_size); |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 475 | CheckVirtualMethodSingleImplementationInfo(klass, |
| 476 | method, |
| 477 | method_in_super, |
| 478 | invalidated_single_impl_methods, |
| 479 | image_pointer_size); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 480 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 481 | // For new virtual methods that don't override. |
| 482 | for (int32_t i = super_class->GetVTableLength(); i < klass->GetVTableLength(); ++i) { |
| 483 | ArtMethod* method = klass->GetVTableEntry(i, image_pointer_size); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 484 | InitSingleImplementationFlag(klass, method, image_pointer_size); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 487 | if (klass->IsInstantiable()) { |
| 488 | auto* iftable = klass->GetIfTable(); |
| 489 | const size_t ifcount = klass->GetIfTableCount(); |
| 490 | for (size_t i = 0; i < ifcount; ++i) { |
| 491 | mirror::Class* interface = iftable->GetInterface(i); |
| 492 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 493 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size); |
| 494 | mirror::PointerArray* method_array = iftable->GetMethodArray(i); |
| 495 | ArtMethod* implementation_method = |
| 496 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size); |
| 497 | DCHECK(implementation_method != nullptr) << klass->PrettyClass(); |
| 498 | CheckInterfaceMethodSingleImplementationInfo(klass, |
| 499 | interface_method, |
| 500 | implementation_method, |
| 501 | invalidated_single_impl_methods, |
| 502 | image_pointer_size); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | InvalidateSingleImplementationMethods(invalidated_single_impl_methods); |
| 508 | } |
| 509 | |
| 510 | void ClassHierarchyAnalysis::InvalidateSingleImplementationMethods( |
| 511 | std::unordered_set<ArtMethod*>& invalidated_single_impl_methods) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 512 | if (!invalidated_single_impl_methods.empty()) { |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 513 | Runtime* const runtime = Runtime::Current(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 514 | Thread *self = Thread::Current(); |
| 515 | // Method headers for compiled code to be invalidated. |
| 516 | std::unordered_set<OatQuickMethodHeader*> dependent_method_headers; |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 517 | PointerSize image_pointer_size = |
| 518 | Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 519 | |
| 520 | { |
| 521 | // We do this under cha_lock_. Committing code also grabs this lock to |
| 522 | // make sure the code is only committed when all single-implementation |
| 523 | // assumptions are still true. |
| 524 | MutexLock cha_mu(self, *Locks::cha_lock_); |
| 525 | // Invalidate compiled methods that assume some virtual calls have only |
| 526 | // single implementations. |
| 527 | for (ArtMethod* invalidated : invalidated_single_impl_methods) { |
| 528 | if (!invalidated->HasSingleImplementation()) { |
| 529 | // It might have been invalidated already when other class linking is |
| 530 | // going on. |
| 531 | continue; |
| 532 | } |
| 533 | invalidated->SetHasSingleImplementation(false); |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 534 | if (invalidated->IsAbstract()) { |
| 535 | // Clear the single implementation method. |
| 536 | invalidated->SetSingleImplementation(nullptr, image_pointer_size); |
| 537 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 538 | |
| 539 | if (runtime->IsAotCompiler()) { |
| 540 | // No need to invalidate any compiled code as the AotCompiler doesn't |
| 541 | // run any code. |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | // Invalidate all dependents. |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 546 | for (const auto& dependent : GetDependents(invalidated)) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 547 | ArtMethod* method = dependent.first;; |
| 548 | OatQuickMethodHeader* method_header = dependent.second; |
| 549 | VLOG(class_linker) << "CHA invalidated compiled code for " << method->PrettyMethod(); |
| 550 | DCHECK(runtime->UseJitCompilation()); |
| 551 | runtime->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor( |
| 552 | method, method_header); |
| 553 | dependent_method_headers.insert(method_header); |
| 554 | } |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 555 | RemoveAllDependenciesFor(invalidated); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | if (dependent_method_headers.empty()) { |
| 560 | return; |
| 561 | } |
| 562 | // Deoptimze compiled code on stack that should have been invalidated. |
| 563 | CHACheckpoint checkpoint(dependent_method_headers); |
| 564 | size_t threads_running_checkpoint = runtime->GetThreadList()->RunCheckpoint(&checkpoint); |
| 565 | if (threads_running_checkpoint != 0) { |
| 566 | checkpoint.WaitForThreadsToRunThroughCheckpoint(threads_running_checkpoint); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | } // namespace art |