Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |
| 18 | #define ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |
| 19 | |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 20 | #include "../mutex.h" |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 21 | #include "elf_image.h" |
Logan Chien | b9eaeea | 2012-03-17 19:45:01 +0800 | [diff] [blame] | 22 | #include "globals.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 23 | #include "instruction_set.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 24 | #include "logging.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 25 | #include "runtime_support_builder.h" |
| 26 | #include "runtime_support_func.h" |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 27 | #include "safe_map.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 28 | |
| 29 | #include <UniquePtr.h> |
| 30 | #include <string> |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 31 | #include <vector> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 32 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 33 | namespace art { |
| 34 | class CompiledMethod; |
| 35 | } |
| 36 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 37 | namespace llvm { |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 38 | class Function; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 39 | class LLVMContext; |
| 40 | class Module; |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 41 | class raw_ostream; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | namespace art { |
| 45 | namespace compiler_llvm { |
| 46 | |
| 47 | class IRBuilder; |
| 48 | |
| 49 | class CompilationUnit { |
| 50 | public: |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 51 | CompilationUnit(InstructionSet insn_set, size_t elf_idx); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 52 | |
| 53 | ~CompilationUnit(); |
| 54 | |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 55 | size_t GetElfIndex() const { |
| 56 | return elf_idx_; |
| 57 | } |
| 58 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 59 | InstructionSet GetInstructionSet() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 60 | cunit_lock_.AssertHeld(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 61 | return insn_set_; |
| 62 | } |
| 63 | |
| 64 | llvm::LLVMContext* GetLLVMContext() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 65 | cunit_lock_.AssertHeld(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 66 | return context_.get(); |
| 67 | } |
| 68 | |
| 69 | llvm::Module* GetModule() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 70 | cunit_lock_.AssertHeld(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 71 | return module_; |
| 72 | } |
| 73 | |
| 74 | IRBuilder* GetIRBuilder() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 75 | cunit_lock_.AssertHeld(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 76 | return irb_.get(); |
| 77 | } |
| 78 | |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 79 | ElfImage GetElfImage() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 80 | MutexLock GUARD(cunit_lock_); |
| 81 | CHECK_GT(elf_image_.size(), 0u); |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 82 | return ElfImage(elf_image_); |
Logan Chien | b9eaeea | 2012-03-17 19:45:01 +0800 | [diff] [blame] | 83 | } |
| 84 | |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 85 | uint16_t AcquireUniqueElfFuncIndex() { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 86 | cunit_lock_.AssertHeld(); |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 87 | CHECK(num_elf_funcs_ < UINT16_MAX); |
| 88 | return num_elf_funcs_++; |
| 89 | } |
| 90 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 91 | void SetBitcodeFileName(const std::string& bitcode_filename) { |
| 92 | MutexLock GUARD(cunit_lock_); |
| 93 | bitcode_filename_ = bitcode_filename; |
| 94 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 95 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 96 | bool Materialize(size_t thread_count); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 97 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 98 | bool IsMaterialized() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 99 | MutexLock GUARD(cunit_lock_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 100 | return (context_.get() == NULL); |
| 101 | } |
| 102 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 103 | bool IsMaterializeThresholdReached() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 104 | MutexLock GUARD(cunit_lock_); |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 105 | return (mem_usage_ > 1000000u); // (threshold: 1 MB) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void AddMemUsageApproximation(size_t usage) { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 109 | MutexLock GUARD(cunit_lock_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 110 | mem_usage_ += usage; |
| 111 | } |
| 112 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 113 | void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm); |
| 114 | |
| 115 | void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes); |
| 116 | |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 117 | mutable Mutex cunit_lock_; |
| 118 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 119 | private: |
| 120 | InstructionSet insn_set_; |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 121 | const size_t elf_idx_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 122 | |
| 123 | UniquePtr<llvm::LLVMContext> context_; |
| 124 | UniquePtr<IRBuilder> irb_; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 125 | UniquePtr<RuntimeSupportBuilder> runtime_support_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 126 | llvm::Module* module_; |
| 127 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 128 | std::string bitcode_filename_; |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 129 | std::string elf_image_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 130 | |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 131 | typedef SafeMap<const llvm::Function*, CompiledMethod*> CompiledMethodMap; |
| 132 | UniquePtr<CompiledMethodMap> compiled_methods_map_; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 133 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 134 | size_t mem_usage_; |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 135 | uint16_t num_elf_funcs_; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 136 | |
Logan Chien | b1bab1c | 2012-05-11 11:05:45 +0800 | [diff] [blame] | 137 | bool MaterializeToFile(llvm::raw_ostream& out_stream); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } // namespace compiler_llvm |
| 141 | } // namespace art |
| 142 | |
| 143 | #endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |