blob: 4b33eceacc9ff2bf765f3aaa0074bb4e94f0cd60 [file] [log] [blame]
Logan Chien8b977d32012-02-21 19:14:55 +08001/*
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 Chien0f0899a2012-03-23 10:48:18 +080020#include "elf_image.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080021#include "globals.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080023#include "logging.h"
TDYa127d668a062012-04-13 12:36:57 -070024#include "runtime_support_builder.h"
25#include "runtime_support_func.h"
Logan Chien8b977d32012-02-21 19:14:55 +080026
27#include <UniquePtr.h>
28#include <string>
29
30namespace llvm {
31 class LLVMContext;
32 class Module;
33}
34
35namespace art {
36namespace compiler_llvm {
37
38class IRBuilder;
39
40class CompilationUnit {
41 public:
Logan Chien6546ec52012-03-17 20:08:29 +080042 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080043
44 ~CompilationUnit();
45
Logan Chien6546ec52012-03-17 20:08:29 +080046 size_t GetElfIndex() const {
47 return elf_idx_;
48 }
49
Logan Chien8b977d32012-02-21 19:14:55 +080050 InstructionSet GetInstructionSet() const {
51 return insn_set_;
52 }
53
54 llvm::LLVMContext* GetLLVMContext() const {
55 return context_.get();
56 }
57
58 llvm::Module* GetModule() const {
59 return module_;
60 }
61
62 IRBuilder* GetIRBuilder() const {
63 return irb_.get();
64 }
65
Logan Chien8b977d32012-02-21 19:14:55 +080066 std::string const& GetBitcodeFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080067 return bitcode_filename_;
68 }
69
Logan Chien8b977d32012-02-21 19:14:55 +080070 void SetBitcodeFileName(std::string const& filename) {
71 bitcode_filename_ = filename;
72 }
73
Logan Chien0f0899a2012-03-23 10:48:18 +080074 ElfImage GetElfImage() const {
75 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080076 }
77
Logan Chien937105a2012-04-02 02:37:37 +080078 uint16_t AcquireUniqueElfFuncIndex() {
79 CHECK(num_elf_funcs_ < UINT16_MAX);
80 return num_elf_funcs_++;
81 }
82
Logan Chien8b977d32012-02-21 19:14:55 +080083 bool WriteBitcodeToFile();
84
85 bool Materialize();
86
Logan Chien7f767612012-03-01 18:54:49 +080087 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080088 return (context_.get() == NULL);
89 }
90
Logan Chien8b977d32012-02-21 19:14:55 +080091 bool IsMaterializeThresholdReached() const {
92 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
93 }
94
95 void AddMemUsageApproximation(size_t usage) {
96 mem_usage_ += usage;
97 }
98
99 private:
100 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800101 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800102
103 UniquePtr<llvm::LLVMContext> context_;
104 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700105 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800106 llvm::Module* module_;
107
Logan Chienb9eaeea2012-03-17 19:45:01 +0800108 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800109 std::string bitcode_filename_;
110
111 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800112 uint16_t num_elf_funcs_;
Logan Chien8b977d32012-02-21 19:14:55 +0800113};
114
115} // namespace compiler_llvm
116} // namespace art
117
118#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_