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 | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 139 | bool LlvmCompilationUnit::Materialize() { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 140 | // Compile and prelink ::llvm::Module |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 141 | if (!MaterializeToString(elf_object_)) { |
| 142 | LOG(ERROR) << "Failed to materialize compilation unit " << cunit_id_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 143 | return false; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 144 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 145 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 146 | if (false) { |
| 147 | // Dump the ELF image for debugging |
| 148 | std::string directory; |
| 149 | if (kIsTargetBuild) { |
| 150 | directory += GetArtCacheOrDie(GetAndroidData()); |
| 151 | } else { |
| 152 | directory += "/tmp"; |
| 153 | } |
| 154 | std::string filename(StringPrintf("%s/Art%u.o", directory.c_str(), cunit_id_)); |
| 155 | UniquePtr<File> output(OS::OpenFile(filename.c_str(), true)); |
| 156 | output->WriteFully(elf_object_.data(), elf_object_.size()); |
| 157 | LOG(INFO) << ".o file written successfully: " << filename; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 160 | return true; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 163 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 164 | bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 165 | ::llvm::raw_string_ostream str_os(str_buffer); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 166 | return MaterializeToRawOStream(str_os); |
| 167 | } |
| 168 | |
| 169 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 170 | bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 171 | // Lookup the LLVM target |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 172 | std::string target_triple; |
| 173 | std::string target_cpu; |
| 174 | std::string target_attr; |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 175 | CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 176 | |
| 177 | std::string errmsg; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 178 | const ::llvm::Target* target = |
| 179 | ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 180 | |
| 181 | CHECK(target != NULL) << errmsg; |
| 182 | |
| 183 | // Target options |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 184 | ::llvm::TargetOptions target_options; |
| 185 | target_options.FloatABIType = ::llvm::FloatABI::Soft; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 186 | target_options.NoFramePointerElim = true; |
| 187 | target_options.NoFramePointerElimNonLeaf = true; |
| 188 | target_options.UseSoftFloat = false; |
TDYa127 | 3978da5 | 2012-05-19 07:45:39 -0700 | [diff] [blame] | 189 | target_options.EnableFastISel = false; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 190 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 191 | // Create the ::llvm::TargetMachine |
| 192 | ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine( |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 193 | target->createTargetMachine(target_triple, target_cpu, target_attr, target_options, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 194 | ::llvm::Reloc::Static, ::llvm::CodeModel::Small, |
| 195 | ::llvm::CodeGenOpt::Aggressive)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 196 | |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 197 | CHECK(target_machine.get() != NULL) << "Failed to create target machine"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 198 | |
| 199 | // Add target data |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 200 | const ::llvm::TargetData* target_data = target_machine->getTargetData(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 201 | |
| 202 | // PassManager for code generation passes |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 203 | ::llvm::PassManager pm; |
| 204 | pm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 205 | |
| 206 | // FunctionPassManager for optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 207 | ::llvm::FunctionPassManager fpm(module_); |
| 208 | fpm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 209 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 210 | if (bitcode_filename_.empty()) { |
| 211 | // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the |
| 212 | // regular FunctionPass. |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 213 | fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 214 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 215 | } else { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 216 | ::llvm::FunctionPassManager fpm2(module_); |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 217 | fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 218 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 219 | fpm2.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 220 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 221 | F != E; ++F) { |
| 222 | fpm2.run(*F); |
| 223 | } |
| 224 | fpm2.doFinalization(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 225 | |
| 226 | // Write bitcode to file |
| 227 | std::string errmsg; |
| 228 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 229 | ::llvm::OwningPtr< ::llvm::tool_output_file> out_file( |
| 230 | new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg, |
| 231 | ::llvm::raw_fd_ostream::F_Binary)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 232 | |
| 233 | |
| 234 | if (!errmsg.empty()) { |
| 235 | LOG(ERROR) << "Failed to create bitcode output file: " << errmsg; |
| 236 | return false; |
| 237 | } |
| 238 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 239 | ::llvm::WriteBitcodeToFile(module_, out_file->os()); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 240 | out_file->keep(); |
| 241 | } |
| 242 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 243 | // Add optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 244 | ::llvm::PassManagerBuilder pm_builder; |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 245 | // TODO: Use inliner after we can do IPO. |
| 246 | pm_builder.Inliner = NULL; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 247 | //pm_builder.Inliner = ::llvm::createFunctionInliningPass(); |
| 248 | //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass(); |
| 249 | //pm_builder.Inliner = ::llvm::createPartialInliningPass(); |
Shih-wei Liao | 415576b | 2012-04-23 15:28:53 -0700 | [diff] [blame] | 250 | pm_builder.OptLevel = 3; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 251 | pm_builder.DisableSimplifyLibCalls = 1; |
TDYa127 | e4c2ccc | 2012-05-13 21:10:36 -0700 | [diff] [blame] | 252 | pm_builder.DisableUnitAtATime = 1; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 253 | pm_builder.populateFunctionPassManager(fpm); |
TDYa127 | ce9c317 | 2012-05-15 06:09:27 -0700 | [diff] [blame] | 254 | pm_builder.populateModulePassManager(pm); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 255 | pm.add(::llvm::createStripDeadPrototypesPass()); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 256 | |
| 257 | // Add passes to emit ELF image |
| 258 | { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 259 | ::llvm::formatted_raw_ostream formatted_os(out_stream, false); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 260 | |
| 261 | // Ask the target to add backend passes as necessary. |
| 262 | if (target_machine->addPassesToEmitFile(pm, |
| 263 | formatted_os, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 264 | ::llvm::TargetMachine::CGFT_ObjectFile, |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 265 | true)) { |
| 266 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 267 | return false; |
| 268 | } |
| 269 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 270 | // Run the per-function optimization |
| 271 | fpm.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 272 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 273 | F != E; ++F) { |
| 274 | fpm.run(*F); |
| 275 | } |
| 276 | fpm.doFinalization(); |
| 277 | |
| 278 | // Run the code generation passes |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 279 | pm.run(*module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 284 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 285 | // Check whether the align is less than or equal to the code alignment of |
| 286 | // that architecture. Since the Oat writer only guarantee that the compiled |
| 287 | // method being aligned to kArchAlignment, we have no way to align the ELf |
| 288 | // section if the section alignment is greater than kArchAlignment. |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 289 | void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 290 | InstructionSet insn_set = GetInstructionSet(); |
| 291 | switch (insn_set) { |
| 292 | case kThumb2: |
| 293 | case kArm: |
| 294 | CHECK_LE(align, static_cast<uint32_t>(kArmAlignment)); |
| 295 | break; |
| 296 | |
| 297 | case kX86: |
| 298 | CHECK_LE(align, static_cast<uint32_t>(kX86Alignment)); |
| 299 | break; |
| 300 | |
| 301 | case kMips: |
| 302 | CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment)); |
| 303 | break; |
| 304 | |
| 305 | default: |
| 306 | LOG(FATAL) << "Unknown instruction set: " << insn_set; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 311 | } // namespace llvm |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 312 | } // namespace art |