Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 20 | #include "base/stl_util.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 21 | #include "base/mutex-inl.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 22 | #include "driver/compiler_driver.h" |
| 23 | #include "driver/compiler_options.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | #include "thread-inl.h" |
| 26 | #include "verified_method.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 27 | #include "verifier/method_verifier-inl.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 31 | VerificationResults::VerificationResults(const CompilerOptions* compiler_options) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 32 | : compiler_options_(compiler_options), |
| 33 | verified_methods_lock_("compiler verified methods lock"), |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 34 | verified_methods_(), |
| 35 | rejected_classes_lock_("compiler rejected classes lock"), |
| 36 | rejected_classes_() { |
| 37 | } |
| 38 | |
| 39 | VerificationResults::~VerificationResults() { |
| 40 | Thread* self = Thread::Current(); |
| 41 | { |
| 42 | WriterMutexLock mu(self, verified_methods_lock_); |
| 43 | STLDeleteValues(&verified_methods_); |
| 44 | } |
| 45 | } |
| 46 | |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 47 | void VerificationResults::ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 48 | DCHECK(method_verifier != nullptr); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 49 | MethodReference ref = method_verifier->GetMethodReference(); |
| 50 | bool compile = IsCandidateForCompilation(ref, method_verifier->GetAccessFlags()); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 51 | const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile); |
| 52 | if (verified_method == nullptr) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 53 | // We'll punt this later. |
| 54 | return; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | WriterMutexLock mu(Thread::Current(), verified_methods_lock_); |
| 58 | auto it = verified_methods_.find(ref); |
| 59 | if (it != verified_methods_.end()) { |
| 60 | // TODO: Investigate why are we doing the work again for this method and try to avoid it. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 61 | LOG(WARNING) << "Method processed more than once: " << ref.PrettyMethod(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 62 | if (!Runtime::Current()->UseJitCompilation()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 63 | DCHECK_EQ(it->second->GetDevirtMap().size(), verified_method->GetDevirtMap().size()); |
| 64 | DCHECK_EQ(it->second->GetSafeCastSet().size(), verified_method->GetSafeCastSet().size()); |
| 65 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 66 | // Delete the new verified method since there was already an existing one registered. It |
| 67 | // is unsafe to replace the existing one since the JIT may be using it to generate a |
| 68 | // native GC map. |
| 69 | delete verified_method; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 70 | return; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 71 | } |
| 72 | verified_methods_.Put(ref, verified_method); |
| 73 | DCHECK(verified_methods_.find(ref) != verified_methods_.end()); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | const 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 Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 82 | void 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 | |
| 90 | bool VerificationResults::IsClassRejected(ClassReference ref) { |
| 91 | ReaderMutexLock mu(Thread::Current(), rejected_classes_lock_); |
| 92 | return (rejected_classes_.find(ref) != rejected_classes_.end()); |
| 93 | } |
| 94 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 95 | bool VerificationResults::IsCandidateForCompilation(MethodReference&, |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 96 | const uint32_t access_flags) { |
Vladimir Marko | f6d1e0f | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 97 | if (!compiler_options_->IsBytecodeCompilationEnabled()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
buzbee | c833299 | 2015-06-25 15:53:45 -0700 | [diff] [blame] | 100 | // Don't compile class initializers unless kEverything. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 101 | if ((compiler_options_->GetCompilerFilter() != CompilerFilter::kEverything) && |
buzbee | c833299 | 2015-06-25 15:53:45 -0700 | [diff] [blame] | 102 | ((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) { |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 105 | return true; |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace art |