blob: b3a9c714cf45a2e75f47285329e7d7b9971c91d8 [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#include "compilation_unit.h"
18
Elliott Hughes0f3c5532012-03-30 14:51:51 -070019#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "ir_builder.h"
21#include "logging.h"
22
23#include <llvm/ADT/OwningPtr.h>
24#include <llvm/ADT/StringSet.h>
25#include <llvm/ADT/Triple.h>
26#include <llvm/Analysis/CallGraph.h>
27#include <llvm/Analysis/DebugInfo.h>
28#include <llvm/Analysis/LoopPass.h>
29#include <llvm/Analysis/RegionPass.h>
30#include <llvm/Analysis/Verifier.h>
31#include <llvm/Assembly/PrintModulePass.h>
32#include <llvm/Bitcode/ReaderWriter.h>
33#include <llvm/CallGraphSCCPass.h>
34#include <llvm/DerivedTypes.h>
35#include <llvm/LLVMContext.h>
Logan Chien8b977d32012-02-21 19:14:55 +080036#include <llvm/Module.h>
37#include <llvm/PassManager.h>
38#include <llvm/Support/Debug.h>
39#include <llvm/Support/FormattedStream.h>
40#include <llvm/Support/ManagedStatic.h>
41#include <llvm/Support/PassNameParser.h>
42#include <llvm/Support/PluginLoader.h>
43#include <llvm/Support/PrettyStackTrace.h>
44#include <llvm/Support/Signals.h>
45#include <llvm/Support/SystemUtils.h>
46#include <llvm/Support/TargetRegistry.h>
47#include <llvm/Support/TargetSelect.h>
48#include <llvm/Support/ToolOutputFile.h>
49#include <llvm/Support/raw_ostream.h>
50#include <llvm/Target/TargetData.h>
51#include <llvm/Target/TargetLibraryInfo.h>
52#include <llvm/Target/TargetMachine.h>
53#include <llvm/Transforms/IPO/PassManagerBuilder.h>
54
55#include <string>
56
57namespace art {
58namespace compiler_llvm {
59
60llvm::Module* makeLLVMModuleContents(llvm::Module* module);
61
62
Logan Chien6546ec52012-03-17 20:08:29 +080063CompilationUnit::CompilationUnit(InstructionSet insn_set, size_t elf_idx)
64: insn_set_(insn_set), elf_idx_(elf_idx), context_(new llvm::LLVMContext()),
Logan Chien937105a2012-04-02 02:37:37 +080065 mem_usage_(0), num_elf_funcs_(0) {
Logan Chien8b977d32012-02-21 19:14:55 +080066
67 // Create the module and include the runtime function declaration
68 module_ = new llvm::Module("art", *context_);
69 makeLLVMModuleContents(module_);
70
71 // Create IRBuilder
72 irb_.reset(new IRBuilder(*context_, *module_));
73}
74
75
76CompilationUnit::~CompilationUnit() {
77}
78
79
80bool CompilationUnit::WriteBitcodeToFile() {
81 std::string errmsg;
82
83 llvm::OwningPtr<llvm::tool_output_file> out_file(
84 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
85 llvm::raw_fd_ostream::F_Binary));
86
87
88 if (!errmsg.empty()) {
89 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
90 return false;
91 }
92
93 llvm::WriteBitcodeToFile(module_, out_file->os());
94 out_file->keep();
95
96 return true;
97}
98
99
100bool CompilationUnit::Materialize() {
Logan Chien8b977d32012-02-21 19:14:55 +0800101 // Lookup the LLVM target
102 char const* target_triple = NULL;
103 char const* target_attr = NULL;
104
Logan Chien8ee03b52012-03-01 13:53:02 +0800105 switch (insn_set_) {
106 case kThumb2:
Logan Chien8b977d32012-02-21 19:14:55 +0800107 target_triple = "thumb-none-linux-gnueabi";
108 target_attr = "+thumb2,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800109 break;
110
111 case kArm:
Logan Chien8b977d32012-02-21 19:14:55 +0800112 target_triple = "armv7-none-linux-gnueabi";
113 target_attr = "+v7,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800114 break;
115
116 case kX86:
Logan Chien8b977d32012-02-21 19:14:55 +0800117 target_triple = "i386-pc-linux-gnu";
118 target_attr = "";
Logan Chien8ee03b52012-03-01 13:53:02 +0800119 break;
120
Shih-wei Liao6edfde42012-03-01 15:49:12 -0800121 case kMips:
122 target_triple = "mipsel-unknown-linux";
123 target_attr = "mips32r2";
124 break;
Logan Chien8ee03b52012-03-01 13:53:02 +0800125
126 default:
Logan Chien8b977d32012-02-21 19:14:55 +0800127 LOG(FATAL) << "Unknown instruction set: " << insn_set_;
128 }
129
130 std::string errmsg;
131 llvm::Target const* target =
132 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
133
134 CHECK(target != NULL) << errmsg;
135
136 // Target options
137 llvm::TargetOptions target_options;
Logan Chien8b977d32012-02-21 19:14:55 +0800138 target_options.FloatABIType = llvm::FloatABI::Soft;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800139 target_options.NoFramePointerElim = true;
140 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien6d6d7542012-03-02 14:00:31 +0800141 target_options.UseSoftFloat = false;
Shih-wei Liaoc6c317e2012-03-30 01:41:18 -0700142 target_options.EnableFastISel = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800143
144 // Create the llvm::TargetMachine
145 llvm::TargetMachine* target_machine =
146 target->createTargetMachine(target_triple, "", target_attr, target_options,
147 llvm::Reloc::Static, llvm::CodeModel::Small,
148 llvm::CodeGenOpt::Aggressive);
149
150 CHECK(target_machine != NULL) << "Failed to create target machine";
151
152
153 // Add target data
154 llvm::TargetData const* target_data = target_machine->getTargetData();
155
156 // PassManager for code generation passes
157 llvm::PassManager pm;
158 pm.add(new llvm::TargetData(*target_data));
159
160 // FunctionPassManager for optimization pass
161 llvm::FunctionPassManager fpm(module_);
162 fpm.add(new llvm::TargetData(*target_data));
163
164 // Add optimization pass
165 llvm::PassManagerBuilder pm_builder;
166 pm_builder.Inliner = NULL; // TODO: add some inline in the future
167 pm_builder.OptLevel = 3;
168 pm_builder.DisableSimplifyLibCalls = 1;
169 pm_builder.populateModulePassManager(pm);
170 pm_builder.populateFunctionPassManager(fpm);
171
Logan Chienb9eaeea2012-03-17 19:45:01 +0800172 // Add passes to emit ELF image
173 {
174 llvm::formatted_raw_ostream formatted_os(
175 *(new llvm::raw_string_ostream(elf_image_)), true);
176
177 // Ask the target to add backend passes as necessary.
178 if (target_machine->addPassesToEmitFile(pm,
179 formatted_os,
180 llvm::TargetMachine::CGFT_ObjectFile,
181 true)) {
182 LOG(FATAL) << "Unable to generate ELF for this target";
183 return false;
184 }
185
186 // Run the per-function optimization
187 fpm.doInitialization();
188 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
189 F != E; ++F) {
190 fpm.run(*F);
191 }
192 fpm.doFinalization();
193
194 // Run the code generation passes
195 pm.run(*module_);
196 }
197
Logan Chiende08e842012-03-21 00:34:12 +0800198 LOG(INFO) << "Compilation Unit: " << elf_idx_ << " (done)";
Logan Chien7f767612012-03-01 18:54:49 +0800199
200 // Free the resources
201 context_.reset(NULL);
202 irb_.reset(NULL);
203 module_ = NULL;
Logan Chien8b977d32012-02-21 19:14:55 +0800204
205 return true;
206}
207
208
Logan Chien8b977d32012-02-21 19:14:55 +0800209} // namespace compiler_llvm
210} // namespace art