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" |
| 24 | |
| 25 | #include <UniquePtr.h> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace llvm { |
| 29 | class LLVMContext; |
| 30 | class Module; |
| 31 | } |
| 32 | |
| 33 | namespace art { |
| 34 | namespace compiler_llvm { |
| 35 | |
| 36 | class IRBuilder; |
| 37 | |
| 38 | class CompilationUnit { |
| 39 | public: |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 40 | CompilationUnit(InstructionSet insn_set, size_t elf_idx); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 41 | |
| 42 | ~CompilationUnit(); |
| 43 | |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 44 | size_t GetElfIndex() const { |
| 45 | return elf_idx_; |
| 46 | } |
| 47 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 48 | InstructionSet GetInstructionSet() const { |
| 49 | return insn_set_; |
| 50 | } |
| 51 | |
| 52 | llvm::LLVMContext* GetLLVMContext() const { |
| 53 | return context_.get(); |
| 54 | } |
| 55 | |
| 56 | llvm::Module* GetModule() const { |
| 57 | return module_; |
| 58 | } |
| 59 | |
| 60 | IRBuilder* GetIRBuilder() const { |
| 61 | return irb_.get(); |
| 62 | } |
| 63 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 64 | std::string const& GetBitcodeFileName() const { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 65 | return bitcode_filename_; |
| 66 | } |
| 67 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 68 | void SetBitcodeFileName(std::string const& filename) { |
| 69 | bitcode_filename_ = filename; |
| 70 | } |
| 71 | |
Logan Chien | 0f0899a | 2012-03-23 10:48:18 +0800 | [diff] [blame] | 72 | ElfImage GetElfImage() const { |
| 73 | return ElfImage(elf_image_); |
Logan Chien | b9eaeea | 2012-03-17 19:45:01 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 76 | bool WriteBitcodeToFile(); |
| 77 | |
| 78 | bool Materialize(); |
| 79 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 80 | bool IsMaterialized() const { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 81 | return (context_.get() == NULL); |
| 82 | } |
| 83 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 84 | bool IsMaterializeThresholdReached() const { |
| 85 | return (mem_usage_ > 300000000u); // (threshold: 300 MB) |
| 86 | } |
| 87 | |
| 88 | void AddMemUsageApproximation(size_t usage) { |
| 89 | mem_usage_ += usage; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | InstructionSet insn_set_; |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 94 | const size_t elf_idx_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 95 | |
| 96 | UniquePtr<llvm::LLVMContext> context_; |
| 97 | UniquePtr<IRBuilder> irb_; |
| 98 | llvm::Module* module_; |
| 99 | |
Logan Chien | b9eaeea | 2012-03-17 19:45:01 +0800 | [diff] [blame] | 100 | std::string elf_image_; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 101 | std::string bitcode_filename_; |
| 102 | |
| 103 | size_t mem_usage_; |
| 104 | }; |
| 105 | |
| 106 | } // namespace compiler_llvm |
| 107 | } // namespace art |
| 108 | |
| 109 | #endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_ |