• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "runtime/cha.h"
17 
18 #include "libpandabase/events/events.h"
19 #include "runtime/include/locks.h"
20 #include "runtime/include/runtime.h"
21 #include "runtime/include/panda_vm.h"
22 #include "runtime/mem/rendezvous.h"
23 #include "runtime/deoptimization.h"
24 
25 namespace panda {
26 
27 using os::memory::LockHolder;  // NOLINT(misc-unused-using-decls)
28 
Update(Class * klass)29 void ClassHierarchyAnalysis::Update(Class *klass)
30 {
31     auto parent = klass->GetBase();
32 
33     if (klass->IsInterface()) {
34         return;
35     }
36 
37     if (parent == nullptr) {
38         for (const auto &method : klass->GetVTable()) {
39             SetHasSingleImplementation(method, true);
40         }
41         return;
42     }
43 
44     ASSERT(klass->GetVTableSize() >= parent->GetVTableSize());
45 
46     PandaSet<Method *> invalidated_methods;
47 
48     for (size_t i = 0; i < parent->GetVTableSize(); ++i) {
49         auto method = klass->GetVTable()[i];
50         auto parent_method = parent->GetVTable()[i];
51         if (method == parent_method || method->IsDefaultInterfaceMethod()) {
52             continue;
53         }
54 
55         if (HasSingleImplementation(parent_method)) {
56             EVENT_CHA_INVALIDATE(std::string(parent_method->GetFullName()), klass->GetName());
57             invalidated_methods.insert(parent_method);
58         }
59         UpdateMethod(method);
60     }
61 
62     for (size_t i = parent->GetVTableSize(); i < klass->GetVTableSize(); ++i) {
63         auto method = klass->GetVTable()[i];
64         if (method->IsDefaultInterfaceMethod()) {
65             continue;
66         }
67         UpdateMethod(method);
68     }
69 
70     InvalidateMethods(invalidated_methods);
71 }
72 
HasSingleImplementation(Method * method)73 bool ClassHierarchyAnalysis::HasSingleImplementation(Method *method)
74 {
75     LockHolder lock(GetLock());
76     return method->HasSingleImplementation();
77 }
78 
SetHasSingleImplementation(Method * method,bool single_implementation)79 void ClassHierarchyAnalysis::SetHasSingleImplementation(Method *method, bool single_implementation)
80 {
81     LockHolder lock(GetLock());
82     method->SetHasSingleImplementation(single_implementation);
83 }
84 
UpdateMethod(Method * method)85 void ClassHierarchyAnalysis::UpdateMethod(Method *method)
86 {
87     // TODO(msherstennikov): Currently panda is allowed to execute abstract method, thus we cannot propagate single
88     // implementation property of the non-abstract method to the all overriden abstract methods.
89     SetHasSingleImplementation(method, !method->IsAbstract());
90 }
91 
InvalidateMethods(const PandaSet<Method * > & methods)92 void ClassHierarchyAnalysis::InvalidateMethods(const PandaSet<Method *> &methods)
93 {
94     PandaSet<Method *> dependent_methods;
95 
96     {
97         LockHolder lock(GetLock());
98         for (auto method : methods) {
99             InvalidateMethod(method, &dependent_methods);
100         }
101     }
102 
103     if (dependent_methods.empty()) {
104         return;
105     }
106 
107     InvalidateCompiledEntryPoint(dependent_methods, true);
108 }
109 
InvalidateMethod(Method * method,PandaSet<Method * > * dependent_methods)110 void ClassHierarchyAnalysis::InvalidateMethod(Method *method, PandaSet<Method *> *dependent_methods) REQUIRES(GetLock())
111 {
112     if (!method->HasSingleImplementation()) {
113         return;
114     }
115 
116     method->SetHasSingleImplementation(false);
117 
118     LOG(DEBUG, CLASS_LINKER) << "[CHA] Invalidate method " << method->GetFullName();
119 
120     auto it = dependency_map_.find(method);
121     if (it == dependency_map_.end()) {
122         return;
123     }
124 
125     for (auto dep_method : it->second) {
126         dependent_methods->insert(dep_method);
127     }
128 
129     dependency_map_.erase(method);
130 }
131 
AddDependency(Method * callee,Method * caller)132 void ClassHierarchyAnalysis::AddDependency(Method *callee, Method *caller)
133 {
134     LockHolder lock(GetLock());
135     LOG(DEBUG, CLASS_LINKER) << "[CHA] Add dependency: caller " << caller->GetFullName() << ", callee "
136                              << callee->GetFullName();
137     // Other thread can remove single implementation of the callee method while we compile caller method.
138     if (!callee->HasSingleImplementation()) {
139         return;
140     }
141     // There is no sense to store dependencies for abstract methods.
142     ASSERT(!callee->IsAbstract());
143     dependency_map_[callee].insert(caller);
144 }
145 
146 }  // namespace panda
147