blob: d3916207984c648661a8ec01e45ac591bc7edee5 [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
TDYa127f15b0ab2012-05-11 21:01:36 -070091 void SetBitcodeFileName(const std::string& bitcode_filename) {
92 MutexLock GUARD(cunit_lock_);
93 bitcode_filename_ = bitcode_filename;
94 }
Logan Chien8b977d32012-02-21 19:14:55 +080095
Logan Chien08e1ba32012-05-08 15:08:51 +080096 bool Materialize(size_t thread_count);
Logan Chien8b977d32012-02-21 19:14:55 +080097
Logan Chien7f767612012-03-01 18:54:49 +080098 bool IsMaterialized() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +080099 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +0800100 return (context_.get() == NULL);
101 }
102
Logan Chien8b977d32012-02-21 19:14:55 +0800103 bool IsMaterializeThresholdReached() const {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800104 MutexLock GUARD(cunit_lock_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700105 return (mem_usage_ > 1000000u); // (threshold: 1 MB)
Logan Chien8b977d32012-02-21 19:14:55 +0800106 }
107
108 void AddMemUsageApproximation(size_t usage) {
Logan Chien8ba2fc52012-04-23 09:10:46 +0800109 MutexLock GUARD(cunit_lock_);
Logan Chien8b977d32012-02-21 19:14:55 +0800110 mem_usage_ += usage;
111 }
112
Logan Chien110bcba2012-04-16 19:11:28 +0800113 void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm);
114
115 void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
116
Logan Chien8ba2fc52012-04-23 09:10:46 +0800117 mutable Mutex cunit_lock_;
118
Logan Chien8b977d32012-02-21 19:14:55 +0800119 private:
120 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800121 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800122
123 UniquePtr<llvm::LLVMContext> context_;
124 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700125 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800126 llvm::Module* module_;
127
TDYa127f15b0ab2012-05-11 21:01:36 -0700128 std::string bitcode_filename_;
Logan Chien08e1ba32012-05-08 15:08:51 +0800129 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800130
TDYa127b2eb5c12012-05-24 15:52:10 -0700131 typedef SafeMap<const llvm::Function*, CompiledMethod*> CompiledMethodMap;
132 UniquePtr<CompiledMethodMap> compiled_methods_map_;
Logan Chien110bcba2012-04-16 19:11:28 +0800133
Logan Chien8b977d32012-02-21 19:14:55 +0800134 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800135 uint16_t num_elf_funcs_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700136
Logan Chienb1bab1c2012-05-11 11:05:45 +0800137 bool MaterializeToFile(llvm::raw_ostream& out_stream);
Logan Chien8b977d32012-02-21 19:14:55 +0800138};
139
140} // namespace compiler_llvm
141} // namespace art
142
143#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_