blob: 9d8770422a81c500d5828a3356b9bb3d35681618 [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>
30
Logan Chien110bcba2012-04-16 19:11:28 +080031namespace art {
32 class CompiledMethod;
33}
34
Logan Chien8b977d32012-02-21 19:14:55 +080035namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080036 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080037 class LLVMContext;
38 class Module;
39}
40
41namespace art {
42namespace compiler_llvm {
43
44class IRBuilder;
45
46class CompilationUnit {
47 public:
Logan Chien6546ec52012-03-17 20:08:29 +080048 CompilationUnit(InstructionSet insn_set, size_t elf_idx);
Logan Chien8b977d32012-02-21 19:14:55 +080049
50 ~CompilationUnit();
51
Logan Chien6546ec52012-03-17 20:08:29 +080052 size_t GetElfIndex() const {
53 return elf_idx_;
54 }
55
Logan Chien8b977d32012-02-21 19:14:55 +080056 InstructionSet GetInstructionSet() const {
57 return insn_set_;
58 }
59
60 llvm::LLVMContext* GetLLVMContext() const {
61 return context_.get();
62 }
63
64 llvm::Module* GetModule() const {
65 return module_;
66 }
67
68 IRBuilder* GetIRBuilder() const {
69 return irb_.get();
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
Shih-wei Liaodbd00342012-04-20 14:27:29 -070081 bool WriteBitcodeToFile(const std::string& bitcode_filename);
Logan Chien8b977d32012-02-21 19:14:55 +080082
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 {
Shih-wei Liao97441942012-04-17 17:28:36 -070090 return (mem_usage_ > 100000000u); // (threshold: 100 MB)
Logan Chien8b977d32012-02-21 19:14:55 +080091 }
92
93 void AddMemUsageApproximation(size_t usage) {
94 mem_usage_ += usage;
95 }
96
Logan Chien110bcba2012-04-16 19:11:28 +080097 void RegisterCompiledMethod(const llvm::Function* func, CompiledMethod* cm);
98
99 void UpdateFrameSizeInBytes(const llvm::Function* func, size_t frame_size_in_bytes);
100
Logan Chien8b977d32012-02-21 19:14:55 +0800101 private:
102 InstructionSet insn_set_;
Logan Chien6546ec52012-03-17 20:08:29 +0800103 const size_t elf_idx_;
Logan Chien8b977d32012-02-21 19:14:55 +0800104
105 UniquePtr<llvm::LLVMContext> context_;
106 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700107 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Logan Chien8b977d32012-02-21 19:14:55 +0800108 llvm::Module* module_;
109
Logan Chienb9eaeea2012-03-17 19:45:01 +0800110 std::string elf_image_;
Logan Chien8b977d32012-02-21 19:14:55 +0800111
Logan Chien110bcba2012-04-16 19:11:28 +0800112 SafeMap<const llvm::Function*, CompiledMethod*> compiled_methods_map_;
113
Logan Chien8b977d32012-02-21 19:14:55 +0800114 size_t mem_usage_;
Logan Chien937105a2012-04-02 02:37:37 +0800115 uint16_t num_elf_funcs_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700116
117 static bool MaterializeFile(int input_fd, int output_fd, InstructionSet insn_set);
Logan Chien8b977d32012-02-21 19:14:55 +0800118};
119
120} // namespace compiler_llvm
121} // namespace art
122
123#endif // ART_SRC_COMPILER_LLVM_COMPILATION_UNIT_H_