verification_results.cc revision cb6b0f31ede2275e79e6199ec391147585a37a2a
1/*
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 "driver/compiler_driver.h"
23#include "driver/compiler_options.h"
24#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
32VerificationResults::VerificationResults(const CompilerOptions* compiler_options)
33    : compiler_options_(compiler_options),
34      verified_methods_lock_("compiler verified methods lock"),
35      verified_methods_(),
36      rejected_classes_lock_("compiler rejected classes lock"),
37      rejected_classes_() {
38  UNUSED(compiler_options);
39}
40
41VerificationResults::~VerificationResults() {
42  Thread* self = Thread::Current();
43  {
44    WriterMutexLock mu(self, verified_methods_lock_);
45    STLDeleteValues(&verified_methods_);
46  }
47}
48
49bool VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) {
50  DCHECK(method_verifier != NULL);
51  MethodReference ref = method_verifier->GetMethodReference();
52  bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags());
53  // TODO: Check also for virtual/interface invokes when DEX-to-DEX supports devirtualization.
54  if (!compile && !method_verifier->HasCheckCasts()) {
55    return true;
56  }
57
58  const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
59  if (verified_method == nullptr) {
60    DCHECK(method_verifier->HasFailures());
61    return false;
62  }
63
64  WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
65  auto it = verified_methods_.find(ref);
66  if (it != verified_methods_.end()) {
67    // TODO: Investigate why are we doing the work again for this method and try to avoid it.
68    LOG(WARNING) << "Method processed more than once: "
69        << PrettyMethod(ref.dex_method_index, *ref.dex_file);
70    DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size());
71    DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size());
72    DCHECK_EQ(it->second->GetDexGcMap().size(), verified_method->GetDexGcMap().size());
73    delete it->second;
74    verified_methods_.erase(it);
75  }
76  verified_methods_.Put(ref, verified_method);
77  DCHECK(verified_methods_.find(ref) != verified_methods_.end());
78  return true;
79}
80
81const VerifiedMethod* VerificationResults::GetVerifiedMethod(MethodReference ref) {
82  ReaderMutexLock mu(Thread::Current(), verified_methods_lock_);
83  auto it = verified_methods_.find(ref);
84  return (it != verified_methods_.end()) ? it->second : nullptr;
85}
86
87void VerificationResults::AddRejectedClass(ClassReference ref) {
88  {
89    WriterMutexLock mu(Thread::Current(), rejected_classes_lock_);
90    rejected_classes_.insert(ref);
91  }
92  DCHECK(IsClassRejected(ref));
93}
94
95bool VerificationResults::IsClassRejected(ClassReference ref) {
96  ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_);
97  return (rejected_classes_.find(ref) != rejected_classes_.end());
98}
99
100bool VerificationResults::IsCandidateForCompilation(MethodReference& method_ref,
101                                                    const uint32_t access_flags) {
102#ifdef ART_SEA_IR_MODE
103  bool use_sea = compiler_options_->GetSeaIrMode();
104  use_sea = use_sea && (std::string::npos != PrettyMethod(
105                        method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
106  if (use_sea) {
107    return true;
108  }
109#endif
110  if (!compiler_options_->IsCompilationEnabled()) {
111    return false;
112  }
113  // Don't compile class initializers, ever.
114  if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
115    return false;
116  }
117  return true;
118}
119
120}  // namespace art
121