blob: 81458db601e46197693ecd2fd45cd5b3b3cd0b85 [file] [log] [blame]
Mingyao Yang063fc772016-08-02 11:02:54 -07001/*
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#ifndef ART_RUNTIME_CHA_H_
18#define ART_RUNTIME_CHA_H_
19
Mingyao Yang063fc772016-08-02 11:02:54 -070020#include "base/enums.h"
21#include "base/mutex.h"
22#include "handle.h"
23#include "mirror/class.h"
24#include "oat_quick_method_header.h"
25#include <unordered_map>
26#include <unordered_set>
27
28namespace art {
29
Andreas Gamped4901292017-05-30 18:41:34 -070030class ArtMethod;
31
Mingyao Yang063fc772016-08-02 11:02:54 -070032/**
33 * Class Hierarchy Analysis (CHA) tries to devirtualize virtual calls into
34 * direct calls based on the info generated by analyzing class hierarchies.
35 * If a class is not subclassed, or even if it's subclassed but one of its
36 * virtual methods isn't overridden, a virtual call for that method can be
37 * changed into a direct call.
38 *
39 * Each virtual method carries a single-implementation status. The status is
40 * incrementally maintained at the end of class linking time when method
41 * overriding takes effect.
42 *
43 * Compiler takes advantage of the single-implementation info of a
44 * method. If a method A has the single-implementation flag set, the compiler
45 * devirtualizes the virtual call for method A into a direct call, and
46 * further try to inline the direct call as a result. The compiler will
47 * also register a dependency that the compiled code depends on the
48 * assumption that method A has single-implementation status.
49 *
50 * When single-implementation info is updated at the end of class linking,
51 * and if method A's single-implementation status is invalidated, all compiled
52 * code that depends on the assumption that method A has single-implementation
53 * status need to be invalidated. Method entrypoints that have this dependency
54 * will be updated as a result. Method A can later be recompiled with less
55 * aggressive assumptions.
56 *
57 * For live compiled code that's on stack, deoptmization will be initiated
58 * to force the invalidated compiled code into interpreter mode to guarantee
59 * correctness. The deoptimization mechanism used is a hybrid of
60 * synchronous and asynchronous deoptimization. The synchronous deoptimization
61 * part checks a hidden local variable flag for the method, and if true,
62 * initiates deoptimization. The asynchronous deoptimization part issues a
63 * checkpoint that walks the stack and for any compiled code on the stack
64 * that should be deoptimized, set the hidden local variable value to be true.
65 *
66 * A cha_lock_ needs to be held for updating single-implementation status,
67 * and registering/unregistering CHA dependencies. Registering CHA dependency
68 * and making compiled code visible also need to be atomic. Otherwise, we
69 * may miss invalidating CHA dependents or making compiled code visible even
70 * after it is invalidated. Care needs to be taken between cha_lock_ and
71 * JitCodeCache::lock_ to guarantee the atomicity.
72 *
73 * We base our CHA on dynamically linked class profiles instead of doing static
74 * analysis. Static analysis can be too aggressive due to dynamic class loading
75 * at runtime, and too conservative since some classes may not be really loaded
76 * at runtime.
77 */
78class ClassHierarchyAnalysis {
79 public:
80 // Types for recording CHA dependencies.
81 // For invalidating CHA dependency, we need to know both the ArtMethod and
82 // the method header. If the ArtMethod has compiled code with the method header
83 // as the entrypoint, we update the entrypoint to the interpreter bridge.
84 // We will also deoptimize frames that are currently executing the code of
85 // the method header.
86 typedef std::pair<ArtMethod*, OatQuickMethodHeader*> MethodAndMethodHeaderPair;
87 typedef std::vector<MethodAndMethodHeaderPair> ListOfDependentPairs;
88
89 ClassHierarchyAnalysis() {}
90
91 // Add a dependency that compiled code with `dependent_header` for `dependent_method`
92 // assumes that virtual `method` has single-implementation.
93 void AddDependency(ArtMethod* method,
94 ArtMethod* dependent_method,
95 OatQuickMethodHeader* dependent_header) REQUIRES(Locks::cha_lock_);
96
97 // Return compiled code that assumes that `method` has single-implementation.
Mingyao Yangcc104502017-05-24 17:13:03 -070098 const ListOfDependentPairs& GetDependents(ArtMethod* method) REQUIRES(Locks::cha_lock_);
Mingyao Yang063fc772016-08-02 11:02:54 -070099
100 // Remove dependency tracking for compiled code that assumes that
101 // `method` has single-implementation.
Mingyao Yangcc104502017-05-24 17:13:03 -0700102 void RemoveAllDependenciesFor(ArtMethod* method) REQUIRES(Locks::cha_lock_);
Mingyao Yang063fc772016-08-02 11:02:54 -0700103
104 // Remove from cha_dependency_map_ all entries that contain OatQuickMethodHeader from
105 // the given `method_headers` set.
106 // This is used when some compiled code is freed.
107 void RemoveDependentsWithMethodHeaders(
108 const std::unordered_set<OatQuickMethodHeader*>& method_headers)
109 REQUIRES(Locks::cha_lock_);
110
111 // Update CHA info for methods that `klass` overrides, after loading `klass`.
112 void UpdateAfterLoadingOf(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
113
114 private:
Mingyao Yange8fcd012017-01-20 10:43:30 -0800115 void InitSingleImplementationFlag(Handle<mirror::Class> klass,
116 ArtMethod* method,
117 PointerSize pointer_size)
Mingyao Yang063fc772016-08-02 11:02:54 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
119
Nicolas Geoffray18ea1c92017-03-27 08:00:18 +0000120 // Check/update single-implementation info when one virtual method
121 // overrides another.
Mingyao Yang063fc772016-08-02 11:02:54 -0700122 // `virtual_method` in `klass` overrides `method_in_super`.
Nicolas Geoffray18ea1c92017-03-27 08:00:18 +0000123 // This may invalidate some assumptions on single-implementation.
Mingyao Yang063fc772016-08-02 11:02:54 -0700124 // Append methods that should have their single-implementation flag invalidated
125 // to `invalidated_single_impl_methods`.
Nicolas Geoffray18ea1c92017-03-27 08:00:18 +0000126 void CheckVirtualMethodSingleImplementationInfo(
Mingyao Yang063fc772016-08-02 11:02:54 -0700127 Handle<mirror::Class> klass,
128 ArtMethod* virtual_method,
129 ArtMethod* method_in_super,
Mingyao Yange8fcd012017-01-20 10:43:30 -0800130 std::unordered_set<ArtMethod*>& invalidated_single_impl_methods,
131 PointerSize pointer_size)
Mingyao Yang063fc772016-08-02 11:02:54 -0700132 REQUIRES_SHARED(Locks::mutator_lock_);
133
Nicolas Geoffray18ea1c92017-03-27 08:00:18 +0000134 // Check/update single-implementation info when one method
135 // implements an interface method.
136 // `implementation_method` in `klass` implements `interface_method`.
137 // Append `interface_method` to `invalidated_single_impl_methods`
138 // if `interface_method` gets a new implementation.
139 void CheckInterfaceMethodSingleImplementationInfo(
140 Handle<mirror::Class> klass,
141 ArtMethod* interface_method,
142 ArtMethod* implementation_method,
143 std::unordered_set<ArtMethod*>& invalidated_single_impl_methods,
144 PointerSize pointer_size)
145 REQUIRES_SHARED(Locks::mutator_lock_);
146
147 void InvalidateSingleImplementationMethods(
148 std::unordered_set<ArtMethod*>& invalidated_single_impl_methods)
149 REQUIRES_SHARED(Locks::mutator_lock_);
150
Mingyao Yange8fcd012017-01-20 10:43:30 -0800151 // For all methods in vtable slot at `verify_index` of `verify_class` and its
152 // superclasses, single-implementation status should be false, except if the
153 // method is `excluded_method`.
154 void VerifyNonSingleImplementation(mirror::Class* verify_class,
155 uint16_t verify_index,
156 ArtMethod* excluded_method)
Mingyao Yang063fc772016-08-02 11:02:54 -0700157 REQUIRES_SHARED(Locks::mutator_lock_);
158
159 // A map that maps a method to a set of compiled code that assumes that method has a
160 // single implementation, which is used to do CHA-based devirtualization.
Mingyao Yangcc104502017-05-24 17:13:03 -0700161 std::unordered_map<ArtMethod*, ListOfDependentPairs> cha_dependency_map_
Mingyao Yang063fc772016-08-02 11:02:54 -0700162 GUARDED_BY(Locks::cha_lock_);
163
164 DISALLOW_COPY_AND_ASSIGN(ClassHierarchyAnalysis);
165};
166
167} // namespace art
168
169#endif // ART_RUNTIME_CHA_H_