blob: b73e99709dd8e40b20f2e6fd228c9b72a4aa98eb [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
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()),
65 mem_usage_(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;
Logan Chien8b977d32012-02-21 19:14:55 +0800142
143 // Create the llvm::TargetMachine
144 llvm::TargetMachine* target_machine =
145 target->createTargetMachine(target_triple, "", target_attr, target_options,
146 llvm::Reloc::Static, llvm::CodeModel::Small,
147 llvm::CodeGenOpt::Aggressive);
148
149 CHECK(target_machine != NULL) << "Failed to create target machine";
150
151
152 // Add target data
153 llvm::TargetData const* target_data = target_machine->getTargetData();
154
155 // PassManager for code generation passes
156 llvm::PassManager pm;
157 pm.add(new llvm::TargetData(*target_data));
158
159 // FunctionPassManager for optimization pass
160 llvm::FunctionPassManager fpm(module_);
161 fpm.add(new llvm::TargetData(*target_data));
162
163 // Add optimization pass
164 llvm::PassManagerBuilder pm_builder;
165 pm_builder.Inliner = NULL; // TODO: add some inline in the future
166 pm_builder.OptLevel = 3;
167 pm_builder.DisableSimplifyLibCalls = 1;
168 pm_builder.populateModulePassManager(pm);
169 pm_builder.populateFunctionPassManager(fpm);
170
Logan Chienb9eaeea2012-03-17 19:45:01 +0800171 // Add passes to emit ELF image
172 {
173 llvm::formatted_raw_ostream formatted_os(
174 *(new llvm::raw_string_ostream(elf_image_)), true);
175
176 // Ask the target to add backend passes as necessary.
177 if (target_machine->addPassesToEmitFile(pm,
178 formatted_os,
179 llvm::TargetMachine::CGFT_ObjectFile,
180 true)) {
181 LOG(FATAL) << "Unable to generate ELF for this target";
182 return false;
183 }
184
185 // Run the per-function optimization
186 fpm.doInitialization();
187 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
188 F != E; ++F) {
189 fpm.run(*F);
190 }
191 fpm.doFinalization();
192
193 // Run the code generation passes
194 pm.run(*module_);
195 }
196
197 // Write ELF image to file
198 // TODO: Remove this when we can embed the ELF image in the Oat file.
199 // We are keeping these code to run the unit test.
Logan Chien8b977d32012-02-21 19:14:55 +0800200 llvm::OwningPtr<llvm::tool_output_file> out_file(
201 new llvm::tool_output_file(elf_filename_.c_str(), errmsg,
202 llvm::raw_fd_ostream::F_Binary));
Logan Chien0cc6ab62012-03-20 22:57:52 +0800203 out_file->os().write(elf_image_.data(), elf_image_.size());
Logan Chien8b977d32012-02-21 19:14:55 +0800204 out_file->keep();
Logan Chienb9eaeea2012-03-17 19:45:01 +0800205
Logan Chien7f767612012-03-01 18:54:49 +0800206 LOG(INFO) << "ELF: " << elf_filename_ << " (done)";
207
208 // Free the resources
209 context_.reset(NULL);
210 irb_.reset(NULL);
211 module_ = NULL;
Logan Chien8b977d32012-02-21 19:14:55 +0800212
213 return true;
214}
215
216
Logan Chien8b977d32012-02-21 19:14:55 +0800217} // namespace compiler_llvm
218} // namespace art