blob: ac956ef6c417182c8d32e061029c5f3536769c89 [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
19#include "constants.h"
20#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
63CompilationUnit::CompilationUnit(InstructionSet insn_set)
64: insn_set_(insn_set), context_(new llvm::LLVMContext()), mem_usage_(0) {
65
66 // Create the module and include the runtime function declaration
67 module_ = new llvm::Module("art", *context_);
68 makeLLVMModuleContents(module_);
69
70 // Create IRBuilder
71 irb_.reset(new IRBuilder(*context_, *module_));
72}
73
74
75CompilationUnit::~CompilationUnit() {
76}
77
78
79bool CompilationUnit::WriteBitcodeToFile() {
80 std::string errmsg;
81
82 llvm::OwningPtr<llvm::tool_output_file> out_file(
83 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
84 llvm::raw_fd_ostream::F_Binary));
85
86
87 if (!errmsg.empty()) {
88 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
89 return false;
90 }
91
92 llvm::WriteBitcodeToFile(module_, out_file->os());
93 out_file->keep();
94
95 return true;
96}
97
98
99bool CompilationUnit::Materialize() {
Logan Chien8b977d32012-02-21 19:14:55 +0800100 // Lookup the LLVM target
101 char const* target_triple = NULL;
102 char const* target_attr = NULL;
103
Logan Chien8ee03b52012-03-01 13:53:02 +0800104 switch (insn_set_) {
105 case kThumb2:
Logan Chien8b977d32012-02-21 19:14:55 +0800106 target_triple = "thumb-none-linux-gnueabi";
107 target_attr = "+thumb2,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800108 break;
109
110 case kArm:
Logan Chien8b977d32012-02-21 19:14:55 +0800111 target_triple = "armv7-none-linux-gnueabi";
112 target_attr = "+v7,+neon,+neonfp,+vfp3";
Logan Chien8ee03b52012-03-01 13:53:02 +0800113 break;
114
115 case kX86:
Logan Chien8b977d32012-02-21 19:14:55 +0800116 target_triple = "i386-pc-linux-gnu";
117 target_attr = "";
Logan Chien8ee03b52012-03-01 13:53:02 +0800118 break;
119
120 //case kMips:
Logan Chien8b977d32012-02-21 19:14:55 +0800121 // target_triple = "mipsel-unknown-linux";
122 // target_attr = "";
Logan Chien8ee03b52012-03-01 13:53:02 +0800123 // break;
124
125 default:
Logan Chien8b977d32012-02-21 19:14:55 +0800126 LOG(FATAL) << "Unknown instruction set: " << insn_set_;
127 }
128
129 std::string errmsg;
130 llvm::Target const* target =
131 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
132
133 CHECK(target != NULL) << errmsg;
134
135 // Target options
136 llvm::TargetOptions target_options;
137
Logan Chien1557ea52012-03-01 13:15:34 +0800138 target_options.NoFramePointerElim = true;
139 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800140 target_options.FloatABIType = llvm::FloatABI::Soft;
141 target_options.UseSoftFloat = false;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800142 target_options.NoFramePointerElim = true;
143 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800144
145 // Create the llvm::TargetMachine
146 llvm::TargetMachine* target_machine =
147 target->createTargetMachine(target_triple, "", target_attr, target_options,
148 llvm::Reloc::Static, llvm::CodeModel::Small,
149 llvm::CodeGenOpt::Aggressive);
150
151 CHECK(target_machine != NULL) << "Failed to create target machine";
152
153
154 // Add target data
155 llvm::TargetData const* target_data = target_machine->getTargetData();
156
157 // PassManager for code generation passes
158 llvm::PassManager pm;
159 pm.add(new llvm::TargetData(*target_data));
160
161 // FunctionPassManager for optimization pass
162 llvm::FunctionPassManager fpm(module_);
163 fpm.add(new llvm::TargetData(*target_data));
164
165 // Add optimization pass
166 llvm::PassManagerBuilder pm_builder;
167 pm_builder.Inliner = NULL; // TODO: add some inline in the future
168 pm_builder.OptLevel = 3;
169 pm_builder.DisableSimplifyLibCalls = 1;
170 pm_builder.populateModulePassManager(pm);
171 pm_builder.populateFunctionPassManager(fpm);
172
173 // Add passes to emit file
174 llvm::OwningPtr<llvm::tool_output_file> out_file(
175 new llvm::tool_output_file(elf_filename_.c_str(), errmsg,
176 llvm::raw_fd_ostream::F_Binary));
177
178 llvm::formatted_raw_ostream formatted_os(out_file->os(), false);
179
180 // Ask the target to add backend passes as necessary.
181 if (target_machine->addPassesToEmitFile(pm,
182 formatted_os,
183 llvm::TargetMachine::CGFT_ObjectFile,
184 true)) {
185 LOG(FATAL) << "Unable to generate ELF for this target";
186 return false;
187 }
188
189 // Run the per-function optimization
190 fpm.doInitialization();
191 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
192 F != E; ++F) {
193 fpm.run(*F);
194 }
195 fpm.doFinalization();
Logan Chien8b977d32012-02-21 19:14:55 +0800196
197 // Run the code generation passes
198 pm.run(*module_);
Logan Chien8b977d32012-02-21 19:14:55 +0800199
Logan Chien7f767612012-03-01 18:54:49 +0800200 // Keep the generated executable
Logan Chien8b977d32012-02-21 19:14:55 +0800201 out_file->keep();
Logan Chien7f767612012-03-01 18:54:49 +0800202 LOG(INFO) << "ELF: " << elf_filename_ << " (done)";
203
204 // Free the resources
205 context_.reset(NULL);
206 irb_.reset(NULL);
207 module_ = NULL;
Logan Chien8b977d32012-02-21 19:14:55 +0800208
209 return true;
210}
211
212
Logan Chien8b977d32012-02-21 19:14:55 +0800213} // namespace compiler_llvm
214} // namespace art