blob: eb92ce445a8a367b2b8e32b7aa0c6a8237a27876 [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"
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
Logan Chien8b977d32012-02-21 19:14:55 +080064 std::string const& GetBitcodeFileName() const {
Logan Chien8b977d32012-02-21 19:14:55 +080065 return bitcode_filename_;
66 }
67
Logan Chien8b977d32012-02-21 19:14:55 +080068 void SetBitcodeFileName(std::string const& filename) {
69 bitcode_filename_ = filename;
70 }
71
Logan Chien0f0899a2012-03-23 10:48:18 +080072 ElfImage GetElfImage() const {
73 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080074 }
75
Logan Chien8b977d32012-02-21 19:14:55 +080076 bool WriteBitcodeToFile();
77
78 bool Materialize();
79
Logan Chien7f767612012-03-01 18:54:49 +080080 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080081 return (context_.get() == NULL);
82 }
83
Logan Chien8b977d32012-02-21 19:14:55 +080084 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 Chien6546ec52012-03-17 20:08:29 +080094 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +080095
96 UniquePtr<llvm::LLVMContext> context_;
97 UniquePtr<IRBuilder> irb_;
98 llvm::Module* module_;
99
Logan Chienb9eaeea2012-03-17 19:45:01 +0800100 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800101 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_