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