blob: a92dabebb161536a1b7d73584e6094de3117f952 [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;
Logan Chien08e1ba32012-05-08 15:08:51 +080041 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080042}
43
44namespace art {
45namespace compiler_llvm {
46
47class IRBuilder;
48
49class CompilationUnit {
50 public:
Logan Chien6546ec52012-03-17 20:08:29 +080051 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080052
53 ~CompilationUnit();
54
Logan Chien6546ec52012-03-17 20:08:29 +080055 size_t GetElfIndex() const {
56 return elf_idx_;
57 }
58
Logan Chien8b977d32012-02-21 19:14:55 +080059 InstructionSet GetInstructionSet() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080060 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080061 return insn_set_;
62 }
63
64 llvm::LLVMContext* GetLLVMContext() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080065 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080066 return context_.get();
67 }
68
69 llvm::Module* GetModule() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080070 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080071 return module_;
72 }
73
74 IRBuilder* GetIRBuilder() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080075 cunit_lock_.AssertHeld();
Logan Chien8b977d32012-02-21 19:14:55 +080076 return irb_.get();
77 }
78
Logan Chien0f0899a2012-03-23 10:48:18 +080079 ElfImage GetElfImage() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080080 MutexLock GUARD(cunit_lock_);
81 CHECK_GT(elf_image_.size(), 0u);
Logan Chien0f0899a2012-03-23 10:48:18 +080082 return ElfImage(elf_image_);
Logan Chienb9eaeea2012-03-17 19:45:01 +080083 }
84
Logan Chien937105a2012-04-02 02:37:37 +080085 uint16_t AcquireUniqueElfFuncIndex() {
Logan Chien8ba2fc52012-04-23 09:10:46 +080086 cunit_lock_.AssertHeld();
Logan Chien937105a2012-04-02 02:37:37 +080087 CHECK(num_elf_funcs_ < UINT16_MAX);
88 return num_elf_funcs_++;
89 }
90
Shih-wei Liaodbd00342012-04-20 14:27:29 -070091 bool WriteBitcodeToFile(const std::string& bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +080092
Logan Chien08e1ba32012-05-08 15:08:51 +080093 bool Materialize(size_t thread_count);
Logan Chien8b977d32012-02-21 19:14:55 +080094
Logan Chien7f767612012-03-01 18:54:49 +080095 bool IsMaterialized() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080096 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +080097 return (context_.get() == NULL);
98 }
99
Logan Chien8b977d32012-02-21 19:14:55 +0800100 bool IsMaterializeThresholdReached() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800101 MutexLock GUARD(cunit_lock_);
Shih-wei Liaoe0e40242012-05-08 01:04:03 -0700102 return (mem_usage_ > 5000000u); // (threshold: 5 MB)
Logan Chien8b977d32012-02-21 19:14:55 +0800103 }
104
105 void AddMemUsageApproximation(size_t usage) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800106 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +0800107 mem_usage_ += usage;
108 }
109
Logan Chien110bcba2012-04-16 19:11:28 +0800110 void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm);
111
112 void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
113
Logan Chien8ba2fc52012-04-23 09:10:46 +0800114 mutable Mutex cunit_lock_;
115
Logan Chien8b977d32012-02-21 19:14:55 +0800116 private:
117 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800118 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800119
120 UniquePtr<llvm::LLVMContext> context_;
121 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700122 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800123 llvm::Module* module_;
124
Logan Chien08e1ba32012-05-08 15:08:51 +0800125 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800126
Logan Chien110bcba2012-04-16 19:11:28 +0800127 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
128
Logan Chien8b977d32012-02-21 19:14:55 +0800129 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800130 uint16_t num_elf_funcs_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700131
Logan Chien08e1ba32012-05-08 15:08:51 +0800132 bool MaterializeToFile(llvm::raw_ostream& out_stream,
133 InstructionSet insn_set);
Logan Chien8b977d32012-02-21 19:14:55 +0800134};
135
136} // namespace compiler_llvm
137} // namespace art
138
139#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_