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