xref: /aosp_15_r20/art/compiler/compiler.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2014 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_COMPILER_H_
18 #define ART_COMPILER_COMPILER_H_
19 
20 #include "base/macros.h"
21 #include "base/mutex.h"
22 #include "base/os.h"
23 #include "compilation_kind.h"
24 #include "dex/invoke_type.h"
25 
26 namespace art HIDDEN {
27 
28 namespace dex {
29 struct CodeItem;
30 }  // namespace dex
31 namespace jit {
32 class JitCodeCache;
33 class JitLogger;
34 class JitMemoryRegion;
35 }  // namespace jit
36 namespace mirror {
37 class ClassLoader;
38 class DexCache;
39 }  // namespace mirror
40 
41 class ArtMethod;
42 class CompiledCodeStorage;
43 class CompiledMethod;
44 class CompilerOptions;
45 class DexFile;
46 template<class T> class Handle;
47 class Thread;
48 
49 class Compiler {
50  public:
51   EXPORT static Compiler* Create(const CompilerOptions& compiler_options,
52                                  CompiledCodeStorage* storage);
53 
54   virtual bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const = 0;
55 
56   virtual CompiledMethod* Compile(const dex::CodeItem* code_item,
57                                   uint32_t access_flags,
58                                   uint16_t class_def_idx,
59                                   uint32_t method_idx,
60                                   Handle<mirror::ClassLoader> class_loader,
61                                   const DexFile& dex_file,
62                                   Handle<mirror::DexCache> dex_cache) const = 0;
63 
64   virtual CompiledMethod* JniCompile(uint32_t access_flags,
65                                      uint32_t method_idx,
66                                      const DexFile& dex_file,
67                                      Handle<mirror::DexCache> dex_cache) const = 0;
68 
JitCompile(Thread * self,jit::JitCodeCache * code_cache,jit::JitMemoryRegion * region,ArtMethod * method,CompilationKind compilation_kind,jit::JitLogger * jit_logger)69   virtual bool JitCompile([[maybe_unused]] Thread* self,
70                           [[maybe_unused]] jit::JitCodeCache* code_cache,
71                           [[maybe_unused]] jit::JitMemoryRegion* region,
72                           [[maybe_unused]] ArtMethod* method,
73                           [[maybe_unused]] CompilationKind compilation_kind,
74                           [[maybe_unused]] jit::JitLogger* jit_logger)
75       REQUIRES_SHARED(Locks::mutator_lock_) {
76     return false;
77   }
78 
79   virtual uintptr_t GetEntryPointOf(ArtMethod* method) const
80      REQUIRES_SHARED(Locks::mutator_lock_) = 0;
81 
GetMaximumCompilationTimeBeforeWarning()82   uint64_t GetMaximumCompilationTimeBeforeWarning() const {
83     return maximum_compilation_time_before_warning_;
84   }
85 
~Compiler()86   virtual ~Compiler() {}
87 
88   // Returns whether the method to compile is such a pathological case that
89   // it's not worth compiling.
90   static bool IsPathologicalCase(const dex::CodeItem& code_item,
91                                  uint32_t method_idx,
92                                  const DexFile& dex_file);
93 
94  protected:
Compiler(const CompilerOptions & compiler_options,CompiledCodeStorage * storage,uint64_t warning)95   Compiler(const CompilerOptions& compiler_options,
96            CompiledCodeStorage* storage,
97            uint64_t warning) :
98       compiler_options_(compiler_options),
99       storage_(storage),
100       maximum_compilation_time_before_warning_(warning) {
101   }
102 
GetCompilerOptions()103   const CompilerOptions& GetCompilerOptions() const {
104     return compiler_options_;
105   }
106 
GetCompiledCodeStorage()107   CompiledCodeStorage* GetCompiledCodeStorage() const {
108     return storage_;
109   }
110 
111  private:
112   const CompilerOptions& compiler_options_;
113   CompiledCodeStorage* const storage_;
114   const uint64_t maximum_compilation_time_before_warning_;
115 
116   DISALLOW_COPY_AND_ASSIGN(Compiler);
117 };
118 
119 }  // namespace art
120 
121 #endif  // ART_COMPILER_COMPILER_H_
122