blob: 2e01fe9c3c05bedcc981c43dd7af8c886b824aa3 [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
Shih-wei Liao6edfde42012-03-01 15:49:12 -0800120 case kMips:
121 target_triple = "mipsel-unknown-linux";
122 target_attr = "mips32r2";
123 break;
Logan Chien8ee03b52012-03-01 13:53:02 +0800124
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;
Logan Chien8b977d32012-02-21 19:14:55 +0800137 target_options.FloatABIType = llvm::FloatABI::Soft;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800138 target_options.NoFramePointerElim = true;
139 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien6d6d7542012-03-02 14:00:31 +0800140 target_options.UseSoftFloat = false;
Logan Chien8b977d32012-02-21 19:14:55 +0800141
142 // Create the llvm::TargetMachine
143 llvm::TargetMachine* target_machine =
144 target->createTargetMachine(target_triple, "", target_attr, target_options,
145 llvm::Reloc::Static, llvm::CodeModel::Small,
146 llvm::CodeGenOpt::Aggressive);
147
148 CHECK(target_machine != NULL) << "Failed to create target machine";
149
150
151 // Add target data
152 llvm::TargetData const* target_data = target_machine->getTargetData();
153
154 // PassManager for code generation passes
155 llvm::PassManager pm;
156 pm.add(new llvm::TargetData(*target_data));
157
158 // FunctionPassManager for optimization pass
159 llvm::FunctionPassManager fpm(module_);
160 fpm.add(new llvm::TargetData(*target_data));
161
162 // Add optimization pass
163 llvm::PassManagerBuilder pm_builder;
164 pm_builder.Inliner = NULL; // TODO: add some inline in the future
165 pm_builder.OptLevel = 3;
166 pm_builder.DisableSimplifyLibCalls = 1;
167 pm_builder.populateModulePassManager(pm);
168 pm_builder.populateFunctionPassManager(fpm);
169
170 // Add passes to emit file
171 llvm::OwningPtr<llvm::tool_output_file> out_file(
172 new llvm::tool_output_file(elf_filename_.c_str(), errmsg,
173 llvm::raw_fd_ostream::F_Binary));
174
175 llvm::formatted_raw_ostream formatted_os(out_file->os(), false);
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();
Logan Chien8b977d32012-02-21 19:14:55 +0800193
194 // Run the code generation passes
195 pm.run(*module_);
Logan Chien8b977d32012-02-21 19:14:55 +0800196
Logan Chien7f767612012-03-01 18:54:49 +0800197 // Keep the generated executable
Logan Chien8b977d32012-02-21 19:14:55 +0800198 out_file->keep();
Logan Chien7f767612012-03-01 18:54:49 +0800199 LOG(INFO) << "ELF: " << elf_filename_ << " (done)";
200
201 // Free the resources
202 context_.reset(NULL);
203 irb_.reset(NULL);
204 module_ = NULL;
Logan Chien8b977d32012-02-21 19:14:55 +0800205
206 return true;
207}
208
209
Logan Chien8b977d32012-02-21 19:14:55 +0800210} // namespace compiler_llvm
211} // namespace art