blob: 8b4fa1a82d431d807e439a8a39fa9a343e335845 [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
82const std::vector<uint8_t>* VerificationResults::GetDexGcMap(MethodReference ref) {
83 const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
84 CHECK(verified_method != nullptr)
85 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
86 return &verified_method->GetDexGcMap();
87}
88
89const MethodReference* VerificationResults::GetDevirtMap(const MethodReference& ref,
90 uint32_t dex_pc) {
91 const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
92 return (verified_method != nullptr) ? verified_method->GetDevirtTarget(dex_pc) : nullptr;
93}
94
95bool VerificationResults::IsSafeCast(MethodReference ref, uint32_t pc) {
96 const VerifiedMethod* verified_method = GetVerifiedMethod(ref);
97 return (verified_method != nullptr) && (verified_method->IsSafeCast(pc));
98}
99
100void VerificationResults::AddRejectedClass(ClassReference ref) {
101 {
102 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
103 rejected_classes_.insert(ref);
104 }
105 DCHECK(IsClassRejected(ref));
106}
107
108bool VerificationResults::IsClassRejected(ClassReference ref) {
109 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
110 return (rejected_classes_.find(ref) != rejected_classes_.end());
111}
112
113bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
114 const uint32_t access_flags) {
115#ifdef ART_SEA_IR_MODE
116 bool use_sea = Runtime::Current()->IsSeaIRMode();
117 use_sea = use_sea && (std::string::npos != PrettyMethod(
118 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
119 if (use_sea) return true;
120#endif
121 // Don't compile class initializers, ever.
122 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
123 return false;
124 }
125 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
126}
127
128} // namespace art