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 | |
Shih-wei Liao | dbd0034 | 2012-04-20 14:27:29 -0700 | [diff] [blame] | 91 | bool WriteBitcodeToFile(const std::string& bitcode_filename); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 92 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 93 | bool Materialize(size_t thread_count); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 94 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 95 | bool IsMaterialized() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 96 | MutexLock GUARD(cunit_lock_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 97 | return (context_.get() == NULL); |
| 98 | } |
| 99 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 100 | bool IsMaterializeThresholdReached() const { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 101 | MutexLock GUARD(cunit_lock_); |
Shih-wei Liao | e0e4024 | 2012-05-08 01:04:03 -0700 | [diff] [blame] | 102 | return (mem_usage_ > 5000000u); // (threshold: 5 MB) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void AddMemUsageApproximation(size_t usage) { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 106 | MutexLock GUARD(cunit_lock_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 107 | mem_usage_ += usage; |
| 108 | } |
| 109 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 110 | void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm); |
| 111 | |
| 112 | void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes); |
| 113 | |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 114 | mutable Mutex cunit_lock_; |
| 115 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 116 | private: |
| 117 | InstructionSet insn_set_; |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 118 | const size_t elf_idx_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 119 | |
| 120 | UniquePtr<llvm::LLVMContext> context_; |
| 121 | UniquePtr<IRBuilder> irb_; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 122 | UniquePtr<RuntimeSupportBuilder> runtime_support_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 123 | llvm::Module* module_; |
| 124 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 125 | std::string elf_image_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 126 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 127 | SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_; |
| 128 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 129 | size_t mem_usage_; |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 130 | uint16_t num_elf_funcs_; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 131 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 132 | bool MaterializeToFile(llvm::raw_ostream& out_stream, |
| 133 | InstructionSet insn_set); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace compiler_llvm |
| 137 | } // namespace art |
| 138 | |
| 139 | #endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |