blob: 659bc6496436d470a55aeddcc0d763d57593a6ab [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
20#include "constants.h"
Logan Chien0f0899a2012-03-23 10:48:18 +080021#include "elf_image.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080022#include "globals.h"
Logan Chien8b977d32012-02-21 19:14:55 +080023#include "logging.h"
24
25#include <UniquePtr.h>
26#include <string>
27
28namespace llvm {
29 class LLVMContext;
30 class Module;
31}
32
33namespace art {
34namespace compiler_llvm {
35
36class IRBuilder;
37
38class CompilationUnit {
39 public:
Logan Chien6546ec52012-03-17 20:08:29 +080040 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080041
42 ~CompilationUnit();
43
Logan Chien6546ec52012-03-17 20:08:29 +080044 size_t GetElfIndex() const {
45 return elf_idx_;
46 }
47
Logan Chien8b977d32012-02-21 19:14:55 +080048 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
64 std::string const& GetElfFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080065 return elf_filename_;
66 }
67
68 std::string const& GetBitcodeFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080069 return bitcode_filename_;
70 }
71
72 void SetElfFileName(std::string const& filename) {
73 elf_filename_ = filename;
74 }
75
76 void SetBitcodeFileName(std::string const& filename) {
77 bitcode_filename_ = filename;
78 }
79
Logan Chien0f0899a2012-03-23 10:48:18 +080080 ElfImage GetElfImage() const {
81 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080082 }
83
Logan Chien8b977d32012-02-21 19:14:55 +080084 bool WriteBitcodeToFile();
85
86 bool Materialize();
87
Logan Chien7f767612012-03-01 18:54:49 +080088 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080089 return (context_.get() == NULL);
90 }
91
Logan Chien8b977d32012-02-21 19:14:55 +080092 bool IsMaterializeThresholdReached() const {
93 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
94 }
95
96 void AddMemUsageApproximation(size_t usage) {
97 mem_usage_ += usage;
98 }
99
100 private:
101 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800102 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800103
104 UniquePtr<llvm::LLVMContext> context_;
105 UniquePtr<IRBuilder> irb_;
106 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 elf_filename_;
110 std::string bitcode_filename_;
111
112 size_t mem_usage_;
113};
114
115} // namespace compiler_llvm
116} // namespace art
117
118#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_