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