blob: 1db54fc4130103776e7e1649531989256bc3c066 [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 Chien937105a2012-04-02 02:37:37 +080076 uint16_t AcquireUniqueElfFuncIndex() {
77 CHECK(num_elf_funcs_ < UINT16_MAX);
78 return num_elf_funcs_++;
79 }
80
Logan Chien8b977d32012-02-21 19:14:55 +080081 bool WriteBitcodeToFile();
82
83 bool Materialize();
84
Logan Chien7f767612012-03-01 18:54:49 +080085 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080086 return (context_.get() == NULL);
87 }
88
Logan Chien8b977d32012-02-21 19:14:55 +080089 bool IsMaterializeThresholdReached() const {
90 return (mem_usage_ > 300000000u); // (threshold: 300 MB)
91 }
92
93 void AddMemUsageApproximation(size_t usage) {
94 mem_usage_ += usage;
95 }
96
97 private:
98 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +080099 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800100
101 UniquePtr<llvm::LLVMContext> context_;
102 UniquePtr<IRBuilder> irb_;
103 llvm::Module* module_;
104
Logan Chienb9eaeea2012-03-17 19:45:01 +0800105 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800106 std::string bitcode_filename_;
107
108 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800109 uint16_t num_elf_funcs_;
Logan Chien8b977d32012-02-21 19:14:55 +0800110};
111
112} // namespace compiler_llvm
113} // namespace art
114
115#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_