blob: d495dff7d982531cea657f7f3fa021c699b216c7 [file] [log] [blame]
Vladimir Marko2b5eaa22013-12-13 13:59:30 +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#ifndef ART_COMPILER_DEX_VERIFIED_METHODS_DATA_H_
18#define ART_COMPILER_DEX_VERIFIED_METHODS_DATA_H_
19
20#include <stdint.h>
21#include <set>
22#include <vector>
23
24#include "base/macros.h"
25#include "base/mutex.h"
26#include "class_reference.h"
27#include "method_reference.h"
28#include "safe_map.h"
29
30namespace art {
31
32namespace verifier {
33class MethodVerifier;
34} // namespace verifier
35
36class VerifiedMethodsData {
37 public:
38 VerifiedMethodsData();
39 ~VerifiedMethodsData();
40
41 bool ProcessVerifiedMethod(verifier::MethodVerifier* method_verifier)
42 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
43 LOCKS_EXCLUDED(dex_gc_maps_lock_, devirt_maps_lock_, safecast_map_lock_);
44
45 const std::vector<uint8_t>* GetDexGcMap(MethodReference ref)
46 LOCKS_EXCLUDED(dex_gc_maps_lock_);
47
48 const MethodReference* GetDevirtMap(const MethodReference& ref, uint32_t dex_pc)
49 LOCKS_EXCLUDED(devirt_maps_lock_);
50
51 // Returns true if the cast can statically be verified to be redundant
52 // by using the check-cast elision peephole optimization in the verifier
53 bool IsSafeCast(MethodReference ref, uint32_t pc) LOCKS_EXCLUDED(safecast_map_lock_);
54
55 void AddRejectedClass(ClassReference ref) LOCKS_EXCLUDED(rejected_classes_lock_);
56 bool IsClassRejected(ClassReference ref) LOCKS_EXCLUDED(rejected_classes_lock_);
57
58 static bool IsCandidateForCompilation(MethodReference& method_ref,
59 const uint32_t access_flags);
60
61 private:
62 /*
63 * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
64 * verification). For type-precise determination we have all the data we need, so we just need to
65 * encode it in some clever fashion.
66 * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
67 */
68 const std::vector<uint8_t>* GenerateGcMap(verifier::MethodVerifier* method_verifier);
69
70 // Verify that the GC map associated with method_ is well formed
71 void VerifyGcMap(verifier::MethodVerifier* method_verifier, const std::vector<uint8_t>& data);
72
73 // Compute sizes for GC map data
74 void ComputeGcMapSizes(verifier::MethodVerifier* method_verifier,
75 size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
76
77 // All the GC maps that the verifier has created
78 typedef SafeMap<const MethodReference, const std::vector<uint8_t>*,
79 MethodReferenceComparator> DexGcMapTable;
80 ReaderWriterMutex dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
81 DexGcMapTable dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
82 void SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* dex_gc_map)
83 LOCKS_EXCLUDED(dex_gc_maps_lock_);
84
85 // Cast elision types.
Vladimir Markoa9faa702013-12-17 11:17:52 +000086 // Since we're adding the dex PCs to the set in increasing order, a sorted vector
87 // is better for performance (not just memory usage), especially for large sets.
88 typedef std::vector<uint32_t> MethodSafeCastSet;
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000089 typedef SafeMap<MethodReference, const MethodSafeCastSet*,
90 MethodReferenceComparator> SafeCastMap;
91 MethodSafeCastSet* GenerateSafeCastSet(verifier::MethodVerifier* method_verifier)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93 void SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* mscs)
94 LOCKS_EXCLUDED(safecast_map_lock_);
95 ReaderWriterMutex safecast_map_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
96 SafeCastMap safecast_map_ GUARDED_BY(safecast_map_lock_);
97
98 // Devirtualization map.
99 typedef SafeMap<uint32_t, MethodReference> PcToConcreteMethodMap;
100 typedef SafeMap<MethodReference, const PcToConcreteMethodMap*,
101 MethodReferenceComparator> DevirtualizationMapTable;
102 PcToConcreteMethodMap* GenerateDevirtMap(verifier::MethodVerifier* method_verifier)
103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104 ReaderWriterMutex devirt_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
105 DevirtualizationMapTable devirt_maps_ GUARDED_BY(devirt_maps_lock_);
106 void SetDevirtMap(MethodReference ref, const PcToConcreteMethodMap* pc_method_map)
107 LOCKS_EXCLUDED(devirt_maps_lock_);
108
109 // Rejected classes
110 typedef std::set<ClassReference> RejectedClassesTable;
111 ReaderWriterMutex rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
112 RejectedClassesTable rejected_classes_ GUARDED_BY(rejected_classes_lock_);
113};
114
115} // namespace art
116
117#endif // ART_COMPILER_DEX_VERIFIED_METHODS_DATA_H_