blob: 5ab56c46fe4e82e7df4eac21c03301f07cb23f05 [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
104 if (insn_set_ == kThumb2) {
105 target_triple = "thumb-none-linux-gnueabi";
106 target_attr = "+thumb2,+neon,+neonfp,+vfp3";
107 } else if (insn_set_ == kArm) {
108 target_triple = "armv7-none-linux-gnueabi";
109 target_attr = "+v7,+neon,+neonfp,+vfp3";
110 } else if (insn_set_ == kX86) {
111 target_triple = "i386-pc-linux-gnu";
112 target_attr = "";
113 // } else if (insn_set_ == kMips) {
114 // target_triple = "mipsel-unknown-linux";
115 // target_attr = "";
116 } else {
117 LOG(FATAL) << "Unknown instruction set: " << insn_set_;
118 }
119
120 std::string errmsg;
121 llvm::Target const* target =
122 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
123
124 CHECK(target != NULL) << errmsg;
125
126 // Target options
127 llvm::TargetOptions target_options;
128
Logan Chien1557ea52012-03-01 13:15:34 +0800129 target_options.NoFramePointerElim = true;
130 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800131 target_options.FloatABIType = llvm::FloatABI::Soft;
132 target_options.UseSoftFloat = false;
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800133 target_options.NoFramePointerElim = true;
134 target_options.NoFramePointerElimNonLeaf = true;
Logan Chien8b977d32012-02-21 19:14:55 +0800135
136 // Create the llvm::TargetMachine
137 llvm::TargetMachine* target_machine =
138 target->createTargetMachine(target_triple, "", target_attr, target_options,
139 llvm::Reloc::Static, llvm::CodeModel::Small,
140 llvm::CodeGenOpt::Aggressive);
141
142 CHECK(target_machine != NULL) << "Failed to create target machine";
143
144
145 // Add target data
146 llvm::TargetData const* target_data = target_machine->getTargetData();
147
148 // PassManager for code generation passes
149 llvm::PassManager pm;
150 pm.add(new llvm::TargetData(*target_data));
151
152 // FunctionPassManager for optimization pass
153 llvm::FunctionPassManager fpm(module_);
154 fpm.add(new llvm::TargetData(*target_data));
155
156 // Add optimization pass
157 llvm::PassManagerBuilder pm_builder;
158 pm_builder.Inliner = NULL; // TODO: add some inline in the future
159 pm_builder.OptLevel = 3;
160 pm_builder.DisableSimplifyLibCalls = 1;
161 pm_builder.populateModulePassManager(pm);
162 pm_builder.populateFunctionPassManager(fpm);
163
164 // Add passes to emit file
165 llvm::OwningPtr<llvm::tool_output_file> out_file(
166 new llvm::tool_output_file(elf_filename_.c_str(), errmsg,
167 llvm::raw_fd_ostream::F_Binary));
168
169 llvm::formatted_raw_ostream formatted_os(out_file->os(), false);
170
171 // Ask the target to add backend passes as necessary.
172 if (target_machine->addPassesToEmitFile(pm,
173 formatted_os,
174 llvm::TargetMachine::CGFT_ObjectFile,
175 true)) {
176 LOG(FATAL) << "Unable to generate ELF for this target";
177 return false;
178 }
179
180 // Run the per-function optimization
181 fpm.doInitialization();
182 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
183 F != E; ++F) {
184 fpm.run(*F);
185 }
186 fpm.doFinalization();
187 LOG(INFO) << "Intraprocedural optimization finished!";
188
189 // Run the code generation passes
190 pm.run(*module_);
191 LOG(INFO) << "Code generation finished!";
192
193 out_file->keep();
194 LOG(DEBUG) << "ELF: " << elf_filename_ << " (done)";
195
196 return true;
197}
198
199
200void CompilationUnit::Finalize() {
201 context_.reset(NULL);
202 irb_.reset(NULL);
203 module_ = NULL;
204}
205
206} // namespace compiler_llvm
207} // namespace art