blob: 2f985217caa9ef6c88ab59461ed984bcd4823e2b [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 Chien8ba2fc52012-04-23 09:10:46 +080020#include "../mutex.h"
Logan Chien0f0899a2012-03-23 10:48:18 +080021#include "elf_image.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080022#include "globals.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070023#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080024#include "logging.h"
TDYa127d668a062012-04-13 12:36:57 -070025#include "runtime_support_builder.h"
26#include "runtime_support_func.h"
Logan Chien110bcba2012-04-16 19:11:28 +080027#include "safe_map.h"
Logan Chien8b977d32012-02-21 19:14:55 +080028
29#include <UniquePtr.h>
30#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080031#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080032
Logan Chien110bcba2012-04-16 19:11:28 +080033namespace art {
34 class CompiledMethod;
35}
36
Logan Chien8b977d32012-02-21 19:14:55 +080037namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080038 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080039 class LLVMContext;
40 class Module;
41}
42
43namespace art {
44namespace compiler_llvm {
45
46class IRBuilder;
47
48class CompilationUnit {
49 public:
Logan Chien6546ec52012-03-17 20:08:29 +080050 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080051
52 ~CompilationUnit();
53
Logan Chien6546ec52012-03-17 20:08:29 +080054 size_t GetElfIndex() const {
55 return elf_idx_;
56 }
57
Logan Chien8b977d32012-02-21 19:14:55 +080058 InstructionSet GetInstructionSet() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080059 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080060 return insn_set_;
61 }
62
63 llvm::LLVMContext* GetLLVMContext() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080064 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080065 return context_.get();
66 }
67
68 llvm::Module* GetModule() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080069 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080070 return module_;
71 }
72
73 IRBuilder* GetIRBuilder() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080074 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080075 return irb_.get();
76 }
77
Logan Chien0f0899a2012-03-23 10:48:18 +080078 ElfImage GetElfImage() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080079 MutexLock GUARD(cunit_lock_);
80 CHECK_GT(elf_image_.size(), 0u);
Logan Chien0f0899a2012-03-23 10:48:18 +080081 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080082 }
83
Logan Chien937105a2012-04-02 02:37:37 +080084 uint16_t AcquireUniqueElfFuncIndex() {
Logan Chien8ba2fc52012-04-23 09:10:46 +080085 cunit_lock_.AssertHeld();
Logan Chien937105a2012-04-02 02:37:37 +080086 CHECK(num_elf_funcs_ < UINT16_MAX);
87 return num_elf_funcs_++;
88 }
89
Shih-wei Liaodbd00342012-04-20 14:27:29 -070090 bool WriteBitcodeToFile(const std::string& bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +080091
92 bool Materialize();
93
Logan Chien7f767612012-03-01 18:54:49 +080094 bool IsMaterialized() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080095 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +080096 return (context_.get() == NULL);
97 }
98
Logan Chien8b977d32012-02-21 19:14:55 +080099 bool IsMaterializeThresholdReached() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800100 MutexLock GUARD(cunit_lock_);
Shih-wei Liao97441942012-04-17 17:28:36 -0700101 return (mem_usage_ > 100000000u); // (threshold: 100 MB)
Logan Chien8b977d32012-02-21 19:14:55 +0800102 }
103
104 void AddMemUsageApproximation(size_t usage) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800105 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +0800106 mem_usage_ += usage;
107 }
108
Logan Chien110bcba2012-04-16 19:11:28 +0800109 void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm);
110
111 void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
112
Logan Chien8ba2fc52012-04-23 09:10:46 +0800113 mutable Mutex cunit_lock_;
114
Logan Chien8b977d32012-02-21 19:14:55 +0800115 private:
116 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800117 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800118
119 UniquePtr<llvm::LLVMContext> context_;
120 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700121 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800122 llvm::Module* module_;
123
Logan Chien799ef4f2012-04-23 00:17:47 +0800124 std::vector<uint8_t> elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800125
Logan Chien110bcba2012-04-16 19:11:28 +0800126 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
127
Logan Chien8b977d32012-02-21 19:14:55 +0800128 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800129 uint16_t num_elf_funcs_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700130
Logan Chien799ef4f2012-04-23 00:17:47 +0800131 bool MaterializeToFile(int output_fd, InstructionSet insn_set);
Logan Chien8b977d32012-02-21 19:14:55 +0800132};
133
134} // namespace compiler_llvm
135} // namespace art
136
137#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_