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