Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1 | /* |
| 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 Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 36 | #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 | |
| 57 | namespace art { |
| 58 | namespace compiler_llvm { |
| 59 | |
| 60 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
| 61 | |
| 62 | |
| 63 | CompilationUnit::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 | |
| 75 | CompilationUnit::~CompilationUnit() { |
| 76 | } |
| 77 | |
| 78 | |
| 79 | bool 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 | |
| 99 | bool CompilationUnit::Materialize() { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 100 | // Lookup the LLVM target |
| 101 | char const* target_triple = NULL; |
| 102 | char const* target_attr = NULL; |
| 103 | |
Logan Chien | 8ee03b5 | 2012-03-01 13:53:02 +0800 | [diff] [blame] | 104 | switch (insn_set_) { |
| 105 | case kThumb2: |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 106 | target_triple = "thumb-none-linux-gnueabi"; |
| 107 | target_attr = "+thumb2,+neon,+neonfp,+vfp3"; |
Logan Chien | 8ee03b5 | 2012-03-01 13:53:02 +0800 | [diff] [blame] | 108 | break; |
| 109 | |
| 110 | case kArm: |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 111 | target_triple = "armv7-none-linux-gnueabi"; |
| 112 | target_attr = "+v7,+neon,+neonfp,+vfp3"; |
Logan Chien | 8ee03b5 | 2012-03-01 13:53:02 +0800 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case kX86: |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 116 | target_triple = "i386-pc-linux-gnu"; |
| 117 | target_attr = ""; |
Logan Chien | 8ee03b5 | 2012-03-01 13:53:02 +0800 | [diff] [blame] | 118 | break; |
| 119 | |
Shih-wei Liao | 6edfde4 | 2012-03-01 15:49:12 -0800 | [diff] [blame^] | 120 | case kMips: |
| 121 | target_triple = "mipsel-unknown-linux"; |
| 122 | target_attr = "mips32r2"; |
| 123 | break; |
Logan Chien | 8ee03b5 | 2012-03-01 13:53:02 +0800 | [diff] [blame] | 124 | |
| 125 | default: |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 126 | 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 | |
| 138 | target_options.FloatABIType = llvm::FloatABI::Soft; |
| 139 | target_options.UseSoftFloat = false; |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 140 | target_options.NoFramePointerElim = true; |
| 141 | target_options.NoFramePointerElimNonLeaf = true; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 142 | |
| 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 | |
| 171 | // Add passes to emit file |
| 172 | llvm::OwningPtr<llvm::tool_output_file> out_file( |
| 173 | new llvm::tool_output_file(elf_filename_.c_str(), errmsg, |
| 174 | llvm::raw_fd_ostream::F_Binary)); |
| 175 | |
| 176 | llvm::formatted_raw_ostream formatted_os(out_file->os(), false); |
| 177 | |
| 178 | // Ask the target to add backend passes as necessary. |
| 179 | if (target_machine->addPassesToEmitFile(pm, |
| 180 | formatted_os, |
| 181 | llvm::TargetMachine::CGFT_ObjectFile, |
| 182 | true)) { |
| 183 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // Run the per-function optimization |
| 188 | fpm.doInitialization(); |
| 189 | for (llvm::Module::iterator F = module_->begin(), E = module_->end(); |
| 190 | F != E; ++F) { |
| 191 | fpm.run(*F); |
| 192 | } |
| 193 | fpm.doFinalization(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 194 | |
| 195 | // Run the code generation passes |
| 196 | pm.run(*module_); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 197 | |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 198 | // Keep the generated executable |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 199 | out_file->keep(); |
Logan Chien | 7f76761 | 2012-03-01 18:54:49 +0800 | [diff] [blame] | 200 | LOG(INFO) << "ELF: " << elf_filename_ << " (done)"; |
| 201 | |
| 202 | // Free the resources |
| 203 | context_.reset(NULL); |
| 204 | irb_.reset(NULL); |
| 205 | module_ = NULL; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 211 | } // namespace compiler_llvm |
| 212 | } // namespace art |