blob: edccec55ba70c8929663c4269b729515d7e49e09 [file] [log] [blame]
Vladimir Markoc7f83202014-01-24 17:55:18 +00001/*
2 * Copyright (C) 2013 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 "verification_results.h"
18
19#include "base/stl_util.h"
20#include "base/mutex.h"
21#include "base/mutex-inl.h"
22#include "thread.h"
23#include "thread-inl.h"
24#include "verified_method.h"
25#include "verifier/method_verifier.h"
26#include "verifier/method_verifier-inl.h"
27
28namespace art {
29
30VerificationResults::VerificationResults()
31 : verified_methods_lock_("compiler verified methods lock"),
32 verified_methods_(),
33 rejected_classes_lock_("compiler rejected classes lock"),
34 rejected_classes_() {
35}
36
37VerificationResults::~VerificationResults() {
38 Thread* self = Thread::Current();
39 {
40 WriterMutexLock mu(self, verified_methods_lock_);
41 STLDeleteValues(&verified_methods_);
42 }
43}
44
45bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
46 MethodReference ref = method_verifier->GetMethodReference();
47 bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
48 // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
49 if (!compile && !method_verifier->HasCheckCasts()) {
50 return true;
51 }
52
53 const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
54 if (verified_method == nullptr) {
55 DCHECK(method_verifier->HasFailures());
56 return false;
57 }
58
59 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
60 auto it = verified_methods_.find(ref);
61 if (it != verified_methods_.end()) {
62 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
63 LOG(WARNING) << "Method processed more than once: "
64 << PrettyMethod(ref.dex_method_index, *ref.dex_file);
65 DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
66 DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
67 DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
68 delete it->second;
69 verified_methods_.erase(it);
70 }
71 verified_methods_.Put(ref, verified_method);
72 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
73 return true;
74}
75
76const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
77 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
78 auto it = verified_methods_.find(ref);
79 return (it != verified_methods_.end()) ? it->second : nullptr;
80}
81
Vladimir Markoc7f83202014-01-24 17:55:18 +000082void VerificationResults::AddRejectedClass(ClassReference ref) {
83 {
84 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
85 rejected_classes_.insert(ref);
86 }
87 DCHECK(IsClassRejected(ref));
88}
89
90bool VerificationResults::IsClassRejected(ClassReference ref) {
91 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
92 return (rejected_classes_.find(ref) != rejected_classes_.end());
93}
94
95bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
96 const uint32_t access_flags) {
97#ifdef ART_SEA_IR_MODE
98 bool use_sea = Runtime::Current()->IsSeaIRMode();
99 use_sea = use_sea && (std::string::npos != PrettyMethod(
100 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
101 if (use_sea) return true;
102#endif
103 // Don't compile class initializers, ever.
104 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
105 return false;
106 }
107 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
108}
109
110} // namespace art