blob: a7f67e73bafcaaa8a6b2cfec15ef526bf29933cf [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"
Brian Carlstrom6449c622014-02-10 23:48:36 -080022#include "driver/compiler_driver.h"
23#include "driver/compiler_options.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024#include "thread.h"
25#include "thread-inl.h"
26#include "verified_method.h"
27#include "verifier/method_verifier.h"
28#include "verifier/method_verifier-inl.h"
29
30namespace art {
31
Brian Carlstrom6449c622014-02-10 23:48:36 -080032VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
Ian Rogersa03de6d2014-03-08 23:37:07 +000033 : verified_methods_lock_("compiler verified methods lock"),
Vladimir Markoc7f83202014-01-24 17:55:18 +000034 verified_methods_(),
35 rejected_classes_lock_("compiler rejected classes lock"),
36 rejected_classes_() {
Ian Rogersa03de6d2014-03-08 23:37:07 +000037 UNUSED(compiler_options);
Vladimir Markoc7f83202014-01-24 17:55:18 +000038}
39
40VerificationResults::~VerificationResults() {
41 Thread* self = Thread::Current();
42 {
43 WriterMutexLock mu(self, verified_methods_lock_);
44 STLDeleteValues(&verified_methods_);
45 }
46}
47
48bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
Brian Carlstrom6449c622014-02-10 23:48:36 -080049 DCHECK(method_verifier != NULL);
Vladimir Markoc7f83202014-01-24 17:55:18 +000050 MethodReference ref = method_verifier->GetMethodReference();
51 bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
52 // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
53 if (!compile && !method_verifier->HasCheckCasts()) {
54 return true;
55 }
56
57 const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
58 if (verified_method == nullptr) {
59 DCHECK(method_verifier->HasFailures());
60 return false;
61 }
62
63 WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
64 auto it = verified_methods_.find(ref);
65 if (it != verified_methods_.end()) {
66 // TODO: Investigate why are we doing the work again for this method and try to avoid it.
67 LOG(WARNING) << "Method processed more than once: "
68 << PrettyMethod(ref.dex_method_index, *ref.dex_file);
69 DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
70 DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
71 DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
72 delete it->second;
73 verified_methods_.erase(it);
74 }
75 verified_methods_.Put(ref, verified_method);
76 DCHECK(verified_methods_.find(ref) != verified_methods_.end());
77 return true;
78}
79
80const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
81 ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
82 auto it = verified_methods_.find(ref);
83 return (it != verified_methods_.end()) ? it->second : nullptr;
84}
85
Vladimir Markoc7f83202014-01-24 17:55:18 +000086void VerificationResults::AddRejectedClass(ClassReference ref) {
87 {
88 WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
89 rejected_classes_.insert(ref);
90 }
91 DCHECK(IsClassRejected(ref));
92}
93
94bool VerificationResults::IsClassRejected(ClassReference ref) {
95 ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
96 return (rejected_classes_.find(ref) != rejected_classes_.end());
97}
98
99bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
100 const uint32_t access_flags) {
101#ifdef ART_SEA_IR_MODE
Brian Carlstrom6449c622014-02-10 23:48:36 -0800102 bool use_sea = compiler_options_->GetSeaIrMode();
103 use_sea = use_sea && (std::string::npos != PrettyMethod(
104 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
105 if (use_sea) {
106 return true;
107 }
Vladimir Markoc7f83202014-01-24 17:55:18 +0000108#endif
109 // Don't compile class initializers, ever.
110 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
111 return false;
112 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800113 return true;
Vladimir Markoc7f83202014-01-24 17:55:18 +0000114}
115
116} // namespace art