blob: d050820c35f05de5e569d6cc73dcd313f901fcc4 [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"
TDYa127d668a062012-04-13 12:36:57 -070024#include "runtime_support_builder.h"
25#include "runtime_support_func.h"
Logan Chien110bcba2012-04-16 19:11:28 +080026#include "safe_map.h"
Logan Chien8b977d32012-02-21 19:14:55 +080027
28#include <UniquePtr.h>
29#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080030#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080031
Logan Chien110bcba2012-04-16 19:11:28 +080032namespace art {
33 class CompiledMethod;
34}
35
Logan Chien8b977d32012-02-21 19:14:55 +080036namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080037 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080038 class LLVMContext;
39 class Module;
40}
41
42namespace art {
43namespace compiler_llvm {
44
45class IRBuilder;
46
47class CompilationUnit {
48 public:
Logan Chien6546ec52012-03-17 20:08:29 +080049 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080050
51 ~CompilationUnit();
52
Logan Chien6546ec52012-03-17 20:08:29 +080053 size_t GetElfIndex() const {
54 return elf_idx_;
55 }
56
Logan Chien8b977d32012-02-21 19:14:55 +080057 InstructionSet GetInstructionSet() const {
58 return insn_set_;
59 }
60
61 llvm::LLVMContext* GetLLVMContext() const {
62 return context_.get();
63 }
64
65 llvm::Module* GetModule() const {
66 return module_;
67 }
68
69 IRBuilder* GetIRBuilder() const {
70 return irb_.get();
71 }
72
Logan Chien0f0899a2012-03-23 10:48:18 +080073 ElfImage GetElfImage() const {
74 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080075 }
76
Logan Chien937105a2012-04-02 02:37:37 +080077 uint16_t AcquireUniqueElfFuncIndex() {
78 CHECK(num_elf_funcs_ < UINT16_MAX);
79 return num_elf_funcs_++;
80 }
81
Shih-wei Liaodbd00342012-04-20 14:27:29 -070082 bool WriteBitcodeToFile(const std::string& bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +080083
84 bool Materialize();
85
Logan Chien7f767612012-03-01 18:54:49 +080086 bool IsMaterialized() const {
Logan Chien8b977d32012-02-21 19:14:55 +080087 return (context_.get() == NULL);
88 }
89
Logan Chien8b977d32012-02-21 19:14:55 +080090 bool IsMaterializeThresholdReached() const {
Shih-wei Liao97441942012-04-17 17:28:36 -070091 return (mem_usage_ > 100000000u); // (threshold: 100 MB)
Logan Chien8b977d32012-02-21 19:14:55 +080092 }
93
94 void AddMemUsageApproximation(size_t usage) {
95 mem_usage_ += usage;
96 }
97
Logan Chien110bcba2012-04-16 19:11:28 +080098 void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm);
99
100 void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
101
Logan Chien8b977d32012-02-21 19:14:55 +0800102 private:
103 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800104 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800105
106 UniquePtr<llvm::LLVMContext> context_;
107 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700108 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800109 llvm::Module* module_;
110
Logan Chien799ef4f2012-04-23 00:17:47 +0800111 std::vector<uint8_t> elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800112
Logan Chien110bcba2012-04-16 19:11:28 +0800113 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
114
Logan Chien8b977d32012-02-21 19:14:55 +0800115 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800116 uint16_t num_elf_funcs_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700117
Logan Chien799ef4f2012-04-23 00:17:47 +0800118 bool MaterializeToFile(int output_fd, InstructionSet insn_set);
Logan Chien8b977d32012-02-21 19:14:55 +0800119};
120
121} // namespace compiler_llvm
122} // namespace art
123
124#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_