blob: 4a230952f8a30c625a080b16cb0a073a74d4f6e1 [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 Chienb9eaeea2012-03-17 19:45:01 +080021#include "globals.h"
Logan Chien8b977d32012-02-21 19:14:55 +080022#include "logging.h"
23
24#include <UniquePtr.h>
25#include <string>
26
27namespace llvm {
28 class LLVMContext;
29 class Module;
30}
31
32namespace art {
33namespace compiler_llvm {
34
35class IRBuilder;
36
37class CompilationUnit {
38 public:
Logan Chien6546ec52012-03-17 20:08:29 +080039 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080040
41 ~CompilationUnit();
42
Logan Chien6546ec52012-03-17 20:08:29 +080043 size_t GetElfIndex() const {
44 return elf_idx_;
45 }
46
Logan Chien8b977d32012-02-21 19:14:55 +080047 InstructionSet GetInstructionSet() const {
48 return insn_set_;
49 }
50
51 llvm::LLVMContext* GetLLVMContext() const {
52 return context_.get();
53 }
54
55 llvm::Module* GetModule() const {
56 return module_;
57 }
58
59 IRBuilder* GetIRBuilder() const {
60 return irb_.get();
61 }
62
63 std::string const& GetElfFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080064 return elf_filename_;
65 }
66
67 std::string const& GetBitcodeFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080068 return bitcode_filename_;
69 }
70
71 void SetElfFileName(std::string const& filename) {
72 elf_filename_ = filename;
73 }
74
75 void SetBitcodeFileName(std::string const& filename) {
76 bitcode_filename_ = filename;
77 }
78
Logan Chienb9eaeea2012-03-17 19:45:01 +080079 const byte* GetElfImage() const {
80 return reinterpret_cast<const byte*>(&*elf_image_.begin());
81 }
82
83 size_t GetElfSize() const {
84 return elf_image_.size();
85 }
86
Logan Chien8b977d32012-02-21 19:14:55 +080087 bool WriteBitcodeToFile();
88
89 bool Materialize();
90
Logan Chien7f767612012-03-01 18:54:49 +080091 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080092 return (context_.get() == NULL);
93 }
94
Logan Chien8b977d32012-02-21 19:14:55 +080095 bool IsMaterializeThresholdReached() const {
96 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
97 }
98
99 void AddMemUsageApproximation(size_t usage) {
100 mem_usage_ += usage;
101 }
102
103 private:
104 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800105 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800106
107 UniquePtr<llvm::LLVMContext> context_;
108 UniquePtr<IRBuilder> irb_;
109 llvm::Module* module_;
110
Logan Chienb9eaeea2012-03-17 19:45:01 +0800111 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800112 std::string elf_filename_;
113 std::string bitcode_filename_;
114
115 size_t mem_usage_;
116};
117
118} // namespace compiler_llvm
119} // namespace art
120
121#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_