blob: 7df09f140c63a815df12185636bda9e082b2ed8f [file] [log] [blame]
Andreas Gampe5eb0d382015-07-23 01:19:26 -07001/*
2 * Copyright (C) 2015 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_DEX_TO_DEX_COMPILER_H_
18#define ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_
19
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080020#include <set>
21#include <unordered_map>
22#include <unordered_set>
23
24#include "base/bit_vector.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file.h"
David Sehr8c0961f2018-01-23 16:11:38 -080026#include "dex/invoke_type.h"
Vladimir Marko8d6768d2017-03-14 10:13:21 +000027#include "handle.h"
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080028#include "method_reference.h"
29#include "quicken_info.h"
Andreas Gampe5eb0d382015-07-23 01:19:26 -070030
31namespace art {
32
33class CompiledMethod;
34class CompilerDriver;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080035class DexCompilationUnit;
Andreas Gampe5eb0d382015-07-23 01:19:26 -070036
Vladimir Marko8d6768d2017-03-14 10:13:21 +000037namespace mirror {
38class ClassLoader;
39} // namespace mirror
40
Andreas Gampe5eb0d382015-07-23 01:19:26 -070041namespace optimizer {
42
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080043class DexToDexCompiler {
44 public:
45 enum class CompilationLevel {
46 kDontDexToDexCompile, // Only meaning wrt image time interpretation.
47 kOptimize // Perform peep-hole optimizations.
48 };
Andreas Gampe5eb0d382015-07-23 01:19:26 -070049
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080050 explicit DexToDexCompiler(CompilerDriver* driver);
51
52 CompiledMethod* CompileMethod(const DexFile::CodeItem* code_item,
53 uint32_t access_flags,
54 InvokeType invoke_type,
55 uint16_t class_def_idx,
56 uint32_t method_idx,
57 Handle<mirror::ClassLoader> class_loader,
58 const DexFile& dex_file,
59 const CompilationLevel compilation_level) WARN_UNUSED;
60
61 void MarkForCompilation(Thread* self,
Mathieu Chartier279e3a32018-01-24 18:17:55 -080062 const MethodReference& method_ref);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080063
64 void ClearState();
65
Mathieu Chartier279e3a32018-01-24 18:17:55 -080066 // Unquicken all methods that have conflicting quicken info. This is not done during the
67 // quickening process to avoid race conditions.
68 void UnquickenConflictingMethods();
69
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080070 CompilerDriver* GetDriver() {
71 return driver_;
72 }
73
74 bool ShouldCompileMethod(const MethodReference& ref);
75
Mathieu Chartier279e3a32018-01-24 18:17:55 -080076 // Return the number of code items to quicken.
77 size_t NumCodeItemsToQuicken(Thread* self) const;
78
79 void SetDexFiles(const std::vector<const DexFile*>& dex_files);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080080
81 private:
82 // Holds the state for compiling a single method.
Mathieu Chartier279e3a32018-01-24 18:17:55 -080083 struct CompilationState;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080084
Mathieu Chartier279e3a32018-01-24 18:17:55 -080085 // Quicken state for a code item, may be referenced by multiple methods.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080086 struct QuickenState {
87 std::vector<MethodReference> methods_;
88 std::vector<uint8_t> quicken_data_;
Mathieu Chartier279e3a32018-01-24 18:17:55 -080089 bool optimized_return_void_ = false;
90 bool conflict_ = false;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080091 };
92
93 BitVector* GetOrAddBitVectorForDex(const DexFile* dex_file) REQUIRES(lock_);
94
95 CompilerDriver* const driver_;
96
97 // State for adding methods (should this be in its own class?).
98 const DexFile* active_dex_file_ = nullptr;
99 BitVector* active_bit_vector_ = nullptr;
100
101 // Lock that guards duplicate code items and the bitmap.
102 mutable Mutex lock_;
103 // Record what method references are going to get quickened.
104 std::unordered_map<const DexFile*, BitVector> should_quicken_;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800105 // Guarded by lock_ during writing, accessed without a lock during quickening.
106 // This is safe because no thread is adding to the shared code items during the quickening phase.
107 std::unordered_set<const DexFile::CodeItem*> shared_code_items_;
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800108 // Blacklisted code items are unquickened in UnquickenConflictingMethods.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800109 std::unordered_map<const DexFile::CodeItem*, QuickenState> shared_code_item_quicken_info_
110 GUARDED_BY(lock_);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800111 // Number of added code items.
112 size_t num_code_items_ GUARDED_BY(lock_) = 0u;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800113};
114
115std::ostream& operator<<(std::ostream& os, const DexToDexCompiler::CompilationLevel& rhs);
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700116
117} // namespace optimizer
118
119} // namespace art
120
121#endif // ART_COMPILER_DEX_DEX_TO_DEX_COMPILER_H_