blob: a5d4e117e674efa43da5a211e44d2bb882e07ed2 [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
Brian Carlstrom641ce032013-01-31 15:21:37 -080017#ifndef ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
18#define ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_
Logan Chien8b977d32012-02-21 19:14:55 +080019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080021#include "base/mutex.h"
buzbee395116c2013-02-27 14:30:25 -080022#include "compiler/dex/compiler_internals.h"
Ian Rogers1212a022013-03-04 10:48:41 -080023#include "compiler/driver/compiler_driver.h"
Logan Chienb9eaeea2012-03-17 19:45:01 +080024#include "globals.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "instruction_set.h"
Ian Rogers89756f22013-03-04 16:40:02 -080026#include "compiler/driver/dex_compilation_unit.h"
TDYa127d668a062012-04-13 12:36:57 -070027#include "runtime_support_builder.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070028#include "runtime_support_llvm_func.h"
Logan Chien110bcba2012-04-16 19:11:28 +080029#include "safe_map.h"
Logan Chien8b977d32012-02-21 19:14:55 +080030
31#include <UniquePtr.h>
32#include <string>
Logan Chien799ef4f2012-04-23 00:17:47 +080033#include <vector>
Logan Chien8b977d32012-02-21 19:14:55 +080034
Logan Chien110bcba2012-04-16 19:11:28 +080035namespace art {
36 class CompiledMethod;
37}
38
Logan Chien8b977d32012-02-21 19:14:55 +080039namespace llvm {
Logan Chien110bcba2012-04-16 19:11:28 +080040 class Function;
Logan Chien8b977d32012-02-21 19:14:55 +080041 class LLVMContext;
42 class Module;
Logan Chien08e1ba32012-05-08 15:08:51 +080043 class raw_ostream;
Logan Chien8b977d32012-02-21 19:14:55 +080044}
45
46namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080047namespace llvm {
Logan Chien8b977d32012-02-21 19:14:55 +080048
Logan Chien971bf3f2012-05-01 15:47:55 +080049class CompilerLLVM;
Logan Chien8b977d32012-02-21 19:14:55 +080050class IRBuilder;
51
Brian Carlstrom641ce032013-01-31 15:21:37 -080052class LlvmCompilationUnit {
Logan Chien8b977d32012-02-21 19:14:55 +080053 public:
Brian Carlstrom641ce032013-01-31 15:21:37 -080054 ~LlvmCompilationUnit();
Logan Chien8b977d32012-02-21 19:14:55 +080055
Brian Carlstrom265091e2013-01-30 14:08:26 -080056 uint32_t GetCompilationUnitId() const {
57 return cunit_id_;
Logan Chien6546ec52012-03-17 20:08:29 +080058 }
59
Logan Chien971bf3f2012-05-01 15:47:55 +080060 InstructionSet GetInstructionSet() const;
Logan Chien8b977d32012-02-21 19:14:55 +080061
Ian Rogers4c1c2832013-03-04 18:30:13 -080062 ::llvm::LLVMContext* GetLLVMContext() const {
Logan Chien8b977d32012-02-21 19:14:55 +080063 return context_.get();
64 }
65
Ian Rogers4c1c2832013-03-04 18:30:13 -080066 ::llvm::Module* GetModule() const {
Logan Chien8b977d32012-02-21 19:14:55 +080067 return module_;
68 }
69
70 IRBuilder* GetIRBuilder() const {
71 return irb_.get();
72 }
73
TDYa127f15b0ab2012-05-11 21:01:36 -070074 void SetBitcodeFileName(const std::string& bitcode_filename) {
TDYa127f15b0ab2012-05-11 21:01:36 -070075 bitcode_filename_ = bitcode_filename;
76 }
Logan Chien8b977d32012-02-21 19:14:55 +080077
buzbee4df2bbd2012-10-11 14:46:06 -070078 LLVMInfo* GetQuickContext() const {
79 return llvm_info_.get();
TDYa12755e5e6c2012-09-11 15:14:42 -070080 }
Ian Rogers0d94eb62013-03-06 15:20:50 -080081 void SetCompilerDriver(CompilerDriver* driver) {
Ian Rogers1212a022013-03-04 10:48:41 -080082 driver_ = driver;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070083 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070084 DexCompilationUnit* GetDexCompilationUnit() {
Brian Carlstrom265091e2013-01-30 14:08:26 -080085 return dex_compilation_unit_;
86 }
Ian Rogers1bf8d4d2013-05-30 00:18:49 -070087 void SetDexCompilationUnit(DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -080088 dex_compilation_unit_ = dex_compilation_unit;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070089 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070090
Logan Chien971bf3f2012-05-01 15:47:55 +080091 bool Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +080092
Logan Chien7f767612012-03-01 18:54:49 +080093 bool IsMaterialized() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -080094 return !elf_object_.empty();
Logan Chien8b977d32012-02-21 19:14:55 +080095 }
96
Brian Carlstrom265091e2013-01-30 14:08:26 -080097 const std::string& GetElfObject() const {
Logan Chien971bf3f2012-05-01 15:47:55 +080098 DCHECK(IsMaterialized());
Brian Carlstrom265091e2013-01-30 14:08:26 -080099 return elf_object_;
Logan Chien8b977d32012-02-21 19:14:55 +0800100 }
101
Logan Chien8b977d32012-02-21 19:14:55 +0800102 private:
Brian Carlstrom641ce032013-01-31 15:21:37 -0800103 LlvmCompilationUnit(const CompilerLLVM* compiler_llvm,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800104 uint32_t cunit_id);
Brian Carlstrom641ce032013-01-31 15:21:37 -0800105
Logan Chien971bf3f2012-05-01 15:47:55 +0800106 const CompilerLLVM* compiler_llvm_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800107 const uint32_t cunit_id_;
Logan Chien8b977d32012-02-21 19:14:55 +0800108
Ian Rogers4c1c2832013-03-04 18:30:13 -0800109 UniquePtr< ::llvm::LLVMContext> context_;
Logan Chien8b977d32012-02-21 19:14:55 +0800110 UniquePtr<IRBuilder> irb_;
TDYa127d668a062012-04-13 12:36:57 -0700111 UniquePtr<RuntimeSupportBuilder> runtime_support_;
Ian Rogers4c1c2832013-03-04 18:30:13 -0800112 ::llvm::Module* module_; // Managed by context_
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800113 UniquePtr<IntrinsicHelper> intrinsic_helper_;
buzbee4df2bbd2012-10-11 14:46:06 -0700114 UniquePtr<LLVMInfo> llvm_info_;
Ian Rogers1212a022013-03-04 10:48:41 -0800115 CompilerDriver* driver_;
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700116 DexCompilationUnit* dex_compilation_unit_;
Logan Chien8b977d32012-02-21 19:14:55 +0800117
TDYa127f15b0ab2012-05-11 21:01:36 -0700118 std::string bitcode_filename_;
Logan Chien8b977d32012-02-21 19:14:55 +0800119
Brian Carlstrom265091e2013-01-30 14:08:26 -0800120 std::string elf_object_;
Logan Chien110bcba2012-04-16 19:11:28 +0800121
Ian Rogers4c1c2832013-03-04 18:30:13 -0800122 SafeMap<const ::llvm::Function*, CompiledMethod*> compiled_methods_map_;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700123
Logan Chien971bf3f2012-05-01 15:47:55 +0800124 void CheckCodeAlign(uint32_t offset) const;
125
Brian Carlstrom35599732013-03-13 15:15:23 -0700126 void DumpBitcodeToFile();
127 void DumpBitcodeToString(std::string& str_buffer);
128
Logan Chien971bf3f2012-05-01 15:47:55 +0800129 bool MaterializeToString(std::string& str_buffer);
Ian Rogers4c1c2832013-03-04 18:30:13 -0800130 bool MaterializeToRawOStream(::llvm::raw_ostream& out_stream);
Logan Chien971bf3f2012-05-01 15:47:55 +0800131
Brian Carlstrom641ce032013-01-31 15:21:37 -0800132 friend class CompilerLLVM; // For LlvmCompilationUnit constructor
Logan Chien8b977d32012-02-21 19:14:55 +0800133};
134
Ian Rogers4c1c2832013-03-04 18:30:13 -0800135} // namespace llvm
Logan Chien8b977d32012-02-21 19:14:55 +0800136} // namespace art
137
Brian Carlstrom641ce032013-01-31 15:21:37 -0800138#endif // ART_SRC_COMPILER_LLVM_LLVM_COMPILATION_UNIT_H_