blob: a10d9772196683182b47c1cd19de01c9533c0482 [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"
21#include "logging.h"
22
23#include <UniquePtr.h>
24#include <string>
25
26namespace llvm {
27 class LLVMContext;
28 class Module;
29}
30
31namespace art {
32namespace compiler_llvm {
33
34class IRBuilder;
35
36class CompilationUnit {
37 public:
38 CompilationUnit(InstructionSet insn_set);
39
40 ~CompilationUnit();
41
42 InstructionSet GetInstructionSet() const {
43 return insn_set_;
44 }
45
46 llvm::LLVMContext* GetLLVMContext() const {
47 return context_.get();
48 }
49
50 llvm::Module* GetModule() const {
51 return module_;
52 }
53
54 IRBuilder* GetIRBuilder() const {
55 return irb_.get();
56 }
57
58 std::string const& GetElfFileName() const {
59 CHECK(IsFinalized());
60 return elf_filename_;
61 }
62
63 std::string const& GetBitcodeFileName() const {
64 CHECK(IsFinalized());
65 return bitcode_filename_;
66 }
67
68 void SetElfFileName(std::string const& filename) {
69 elf_filename_ = filename;
70 }
71
72 void SetBitcodeFileName(std::string const& filename) {
73 bitcode_filename_ = filename;
74 }
75
76 bool WriteBitcodeToFile();
77
78 bool Materialize();
79
80 bool IsFinalized() const {
81 return (context_.get() == NULL);
82 }
83
84 void Finalize();
85
86 bool IsMaterializeThresholdReached() const {
87 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
88 }
89
90 void AddMemUsageApproximation(size_t usage) {
91 mem_usage_ += usage;
92 }
93
94 private:
95 InstructionSet insn_set_;
96
97 UniquePtr<llvm::LLVMContext> context_;
98 UniquePtr<IRBuilder> irb_;
99 llvm::Module* module_;
100
101 std::string elf_filename_;
102 std::string bitcode_filename_;
103
104 size_t mem_usage_;
105};
106
107} // namespace compiler_llvm
108} // namespace art
109
110#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_