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 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 17 | #include "llvm_compilation_unit.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 18 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <unistd.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 22 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 23 | #include <string> |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 24 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 25 | #include <llvm/ADT/OwningPtr.h> |
| 26 | #include <llvm/ADT/StringSet.h> |
| 27 | #include <llvm/ADT/Triple.h> |
| 28 | #include <llvm/Analysis/CallGraph.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 29 | #include <llvm/Analysis/Dominators.h> |
| 30 | #include <llvm/Analysis/LoopInfo.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 31 | #include <llvm/Analysis/LoopPass.h> |
| 32 | #include <llvm/Analysis/RegionPass.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 33 | #include <llvm/Analysis/ScalarEvolution.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 34 | #include <llvm/Analysis/Verifier.h> |
| 35 | #include <llvm/Assembly/PrintModulePass.h> |
| 36 | #include <llvm/Bitcode/ReaderWriter.h> |
| 37 | #include <llvm/CallGraphSCCPass.h> |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 38 | #include <llvm/CodeGen/MachineFrameInfo.h> |
| 39 | #include <llvm/CodeGen/MachineFunction.h> |
| 40 | #include <llvm/CodeGen/MachineFunctionPass.h> |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 41 | #include <llvm/DebugInfo.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 42 | #include <llvm/DerivedTypes.h> |
| 43 | #include <llvm/LLVMContext.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 44 | #include <llvm/Module.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 45 | #include <llvm/Object/ObjectFile.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 46 | #include <llvm/PassManager.h> |
| 47 | #include <llvm/Support/Debug.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 48 | #include <llvm/Support/ELF.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 49 | #include <llvm/Support/FormattedStream.h> |
| 50 | #include <llvm/Support/ManagedStatic.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 51 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 52 | #include <llvm/Support/PassNameParser.h> |
| 53 | #include <llvm/Support/PluginLoader.h> |
| 54 | #include <llvm/Support/PrettyStackTrace.h> |
| 55 | #include <llvm/Support/Signals.h> |
| 56 | #include <llvm/Support/SystemUtils.h> |
| 57 | #include <llvm/Support/TargetRegistry.h> |
| 58 | #include <llvm/Support/TargetSelect.h> |
| 59 | #include <llvm/Support/ToolOutputFile.h> |
| 60 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 61 | #include <llvm/Support/system_error.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 62 | #include <llvm/Target/TargetData.h> |
| 63 | #include <llvm/Target/TargetLibraryInfo.h> |
| 64 | #include <llvm/Target/TargetMachine.h> |
Shih-wei Liao | f1cb9a5 | 2012-04-20 01:49:18 -0700 | [diff] [blame] | 65 | #include <llvm/Transforms/IPO.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 66 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 67 | #include <llvm/Transforms/Scalar.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 68 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 69 | #include "base/logging.h" |
| 70 | #include "base/unix_file/fd_file.h" |
| 71 | #include "compiled_method.h" |
| 72 | #include "compiler_llvm.h" |
| 73 | #include "instruction_set.h" |
| 74 | #include "ir_builder.h" |
| 75 | #include "os.h" |
| 76 | #include "runtime_support_builder_arm.h" |
| 77 | #include "runtime_support_builder_thumb2.h" |
| 78 | #include "runtime_support_builder_x86.h" |
| 79 | #include "utils_llvm.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 80 | |
| 81 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 82 | namespace llvm { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 83 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 84 | ::llvm::FunctionPass* |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 85 | CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 86 | CompilerDriver* compiler, const DexCompilationUnit* dex_compilation_unit); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 87 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 88 | ::llvm::Module* makeLLVMModuleContents(::llvm::Module* module); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 89 | |
| 90 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 91 | LlvmCompilationUnit::LlvmCompilationUnit(const CompilerLLVM* compiler_llvm, size_t cunit_id) |
| 92 | : compiler_llvm_(compiler_llvm), cunit_id_(cunit_id) { |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 93 | driver_ = NULL; |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 94 | dex_compilation_unit_ = NULL; |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 95 | llvm_info_.reset(new LLVMInfo()); |
| 96 | context_.reset(llvm_info_->GetLLVMContext()); |
| 97 | module_ = llvm_info_->GetLLVMModule(); |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 98 | |
| 99 | // Include the runtime function declaration |
| 100 | makeLLVMModuleContents(module_); |
| 101 | |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 102 | |
| 103 | intrinsic_helper_.reset(new IntrinsicHelper(*context_, *module_)); |
| 104 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 105 | // Create IRBuilder |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 106 | irb_.reset(new IRBuilder(*context_, *module_, *intrinsic_helper_)); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 107 | |
| 108 | // We always need a switch case, so just use a normal function. |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 109 | switch(GetInstructionSet()) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 110 | default: |
| 111 | runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_)); |
| 112 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 113 | case kArm: |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 114 | runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_)); |
| 115 | break; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 116 | case kThumb2: |
| 117 | runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_)); |
| 118 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 119 | case kX86: |
| 120 | runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_)); |
| 121 | break; |
| 122 | } |
| 123 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 124 | irb_->SetRuntimeSupport(runtime_support_.get()); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 128 | LlvmCompilationUnit::~LlvmCompilationUnit() { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 129 | ::llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_ |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 130 | CHECK(llvm_context != NULL); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 134 | InstructionSet LlvmCompilationUnit::GetInstructionSet() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 135 | return compiler_llvm_->GetInstructionSet(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Brian Carlstrom | 3559973 | 2013-03-13 15:15:23 -0700 | [diff] [blame] | 139 | static std::string DumpDirectory() { |
| 140 | if (kIsTargetBuild) { |
| 141 | return GetArtCacheOrDie(GetAndroidData()); |
| 142 | } |
| 143 | return "/tmp"; |
| 144 | } |
| 145 | |
| 146 | void LlvmCompilationUnit::DumpBitcodeToFile() { |
| 147 | std::string bitcode; |
| 148 | DumpBitcodeToString(bitcode); |
| 149 | std::string filename(StringPrintf("%s/Art%u.bc", DumpDirectory().c_str(), cunit_id_)); |
| 150 | UniquePtr<File> output(OS::OpenFile(filename.c_str(), true)); |
| 151 | output->WriteFully(bitcode.data(), bitcode.size()); |
| 152 | LOG(INFO) << ".bc file written successfully: " << filename; |
| 153 | } |
| 154 | |
| 155 | void LlvmCompilationUnit::DumpBitcodeToString(std::string& str_buffer) { |
| 156 | ::llvm::raw_string_ostream str_os(str_buffer); |
| 157 | ::llvm::WriteBitcodeToFile(module_, str_os); |
| 158 | } |
| 159 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 160 | bool LlvmCompilationUnit::Materialize() { |
Brian Carlstrom | 3559973 | 2013-03-13 15:15:23 -0700 | [diff] [blame] | 161 | |
| 162 | const bool kDumpBitcode = false; |
| 163 | if (kDumpBitcode) { |
| 164 | // Dump the bitcode for debugging |
| 165 | DumpBitcodeToFile(); |
| 166 | } |
| 167 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 168 | // Compile and prelink ::llvm::Module |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 169 | if (!MaterializeToString(elf_object_)) { |
| 170 | LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 171 | return false; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 172 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 173 | |
Brian Carlstrom | 3559973 | 2013-03-13 15:15:23 -0700 | [diff] [blame] | 174 | const bool kDumpELF = false; |
| 175 | if (kDumpELF) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 176 | // Dump the ELF image for debugging |
Brian Carlstrom | 3559973 | 2013-03-13 15:15:23 -0700 | [diff] [blame] | 177 | std::string filename(StringPrintf("%s/Art%u.o", DumpDirectory().c_str(), cunit_id_)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 178 | UniquePtr<File> output(OS::OpenFile(filename.c_str(), true)); |
| 179 | output->WriteFully(elf_object_.data(), elf_object_.size()); |
| 180 | LOG(INFO) << ".o file written successfully: " << filename; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 183 | return true; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 186 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 187 | bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 188 | ::llvm::raw_string_ostream str_os(str_buffer); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 189 | return MaterializeToRawOStream(str_os); |
| 190 | } |
| 191 | |
| 192 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 193 | bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 194 | // Lookup the LLVM target |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 195 | std::string target_triple; |
| 196 | std::string target_cpu; |
| 197 | std::string target_attr; |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 198 | CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 199 | |
| 200 | std::string errmsg; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 201 | const ::llvm::Target* target = |
| 202 | ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 203 | |
| 204 | CHECK(target != NULL) << errmsg; |
| 205 | |
| 206 | // Target options |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 207 | ::llvm::TargetOptions target_options; |
| 208 | target_options.FloatABIType = ::llvm::FloatABI::Soft; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 209 | target_options.NoFramePointerElim = true; |
| 210 | target_options.NoFramePointerElimNonLeaf = true; |
| 211 | target_options.UseSoftFloat = false; |
TDYa127 | 3978da5 | 2012-05-19 07:45:39 -0700 | [diff] [blame] | 212 | target_options.EnableFastISel = false; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 213 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 214 | // Create the ::llvm::TargetMachine |
| 215 | ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine( |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 216 | target->createTargetMachine(target_triple, target_cpu, target_attr, target_options, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 217 | ::llvm::Reloc::Static, ::llvm::CodeModel::Small, |
| 218 | ::llvm::CodeGenOpt::Aggressive)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 219 | |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 220 | CHECK(target_machine.get() != NULL) << "Failed to create target machine"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 221 | |
| 222 | // Add target data |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 223 | const ::llvm::TargetData* target_data = target_machine->getTargetData(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 224 | |
| 225 | // PassManager for code generation passes |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 226 | ::llvm::PassManager pm; |
| 227 | pm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 228 | |
| 229 | // FunctionPassManager for optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 230 | ::llvm::FunctionPassManager fpm(module_); |
| 231 | fpm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 232 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 233 | if (bitcode_filename_.empty()) { |
| 234 | // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the |
| 235 | // regular FunctionPass. |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 236 | fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 237 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 238 | } else { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 239 | ::llvm::FunctionPassManager fpm2(module_); |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 240 | fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 241 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 242 | fpm2.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 243 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 244 | F != E; ++F) { |
| 245 | fpm2.run(*F); |
| 246 | } |
| 247 | fpm2.doFinalization(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 248 | |
| 249 | // Write bitcode to file |
| 250 | std::string errmsg; |
| 251 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 252 | ::llvm::OwningPtr< ::llvm::tool_output_file> out_file( |
| 253 | new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg, |
| 254 | ::llvm::raw_fd_ostream::F_Binary)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 255 | |
| 256 | |
| 257 | if (!errmsg.empty()) { |
| 258 | LOG(ERROR) << "Failed to create bitcode output file: " << errmsg; |
| 259 | return false; |
| 260 | } |
| 261 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 262 | ::llvm::WriteBitcodeToFile(module_, out_file->os()); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 263 | out_file->keep(); |
| 264 | } |
| 265 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 266 | // Add optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 267 | ::llvm::PassManagerBuilder pm_builder; |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 268 | // TODO: Use inliner after we can do IPO. |
| 269 | pm_builder.Inliner = NULL; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 270 | //pm_builder.Inliner = ::llvm::createFunctionInliningPass(); |
| 271 | //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass(); |
| 272 | //pm_builder.Inliner = ::llvm::createPartialInliningPass(); |
Shih-wei Liao | 415576b | 2012-04-23 15:28:53 -0700 | [diff] [blame] | 273 | pm_builder.OptLevel = 3; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 274 | pm_builder.DisableSimplifyLibCalls = 1; |
TDYa127 | e4c2ccc | 2012-05-13 21:10:36 -0700 | [diff] [blame] | 275 | pm_builder.DisableUnitAtATime = 1; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 276 | pm_builder.populateFunctionPassManager(fpm); |
TDYa127 | ce9c317 | 2012-05-15 06:09:27 -0700 | [diff] [blame] | 277 | pm_builder.populateModulePassManager(pm); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 278 | pm.add(::llvm::createStripDeadPrototypesPass()); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 279 | |
| 280 | // Add passes to emit ELF image |
| 281 | { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 282 | ::llvm::formatted_raw_ostream formatted_os(out_stream, false); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 283 | |
| 284 | // Ask the target to add backend passes as necessary. |
| 285 | if (target_machine->addPassesToEmitFile(pm, |
| 286 | formatted_os, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 287 | ::llvm::TargetMachine::CGFT_ObjectFile, |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 288 | true)) { |
| 289 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 290 | return false; |
| 291 | } |
| 292 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 293 | // Run the per-function optimization |
| 294 | fpm.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 295 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 296 | F != E; ++F) { |
| 297 | fpm.run(*F); |
| 298 | } |
| 299 | fpm.doFinalization(); |
| 300 | |
| 301 | // Run the code generation passes |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 302 | pm.run(*module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | return true; |
| 306 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 307 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 308 | // Check whether the align is less than or equal to the code alignment of |
| 309 | // that architecture. Since the Oat writer only guarantee that the compiled |
| 310 | // method being aligned to kArchAlignment, we have no way to align the ELf |
| 311 | // section if the section alignment is greater than kArchAlignment. |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 312 | void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 313 | InstructionSet insn_set = GetInstructionSet(); |
| 314 | switch (insn_set) { |
| 315 | case kThumb2: |
| 316 | case kArm: |
| 317 | CHECK_LE(align, static_cast<uint32_t>(kArmAlignment)); |
| 318 | break; |
| 319 | |
| 320 | case kX86: |
| 321 | CHECK_LE(align, static_cast<uint32_t>(kX86Alignment)); |
| 322 | break; |
| 323 | |
| 324 | case kMips: |
| 325 | CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment)); |
| 326 | break; |
| 327 | |
| 328 | default: |
| 329 | LOG(FATAL) << "Unknown instruction set: " << insn_set; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 334 | } // namespace llvm |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 335 | } // namespace art |