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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 20 | #include "compiled_method.h" |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 21 | #include "compiler_llvm.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 22 | #include "instruction_set.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 23 | #include "ir_builder.h" |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 24 | #include "os.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 25 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 26 | #include "runtime_support_builder_arm.h" |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 27 | #include "runtime_support_builder_thumb2.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 28 | #include "runtime_support_builder_x86.h" |
| 29 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 30 | #include <llvm/ADT/OwningPtr.h> |
| 31 | #include <llvm/ADT/StringSet.h> |
| 32 | #include <llvm/ADT/Triple.h> |
| 33 | #include <llvm/Analysis/CallGraph.h> |
| 34 | #include <llvm/Analysis/DebugInfo.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 35 | #include <llvm/Analysis/Dominators.h> |
| 36 | #include <llvm/Analysis/LoopInfo.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 37 | #include <llvm/Analysis/LoopPass.h> |
| 38 | #include <llvm/Analysis/RegionPass.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 39 | #include <llvm/Analysis/ScalarEvolution.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 40 | #include <llvm/Analysis/Verifier.h> |
| 41 | #include <llvm/Assembly/PrintModulePass.h> |
| 42 | #include <llvm/Bitcode/ReaderWriter.h> |
| 43 | #include <llvm/CallGraphSCCPass.h> |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 44 | #include <llvm/CodeGen/MachineFrameInfo.h> |
| 45 | #include <llvm/CodeGen/MachineFunction.h> |
| 46 | #include <llvm/CodeGen/MachineFunctionPass.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 47 | #include <llvm/DerivedTypes.h> |
| 48 | #include <llvm/LLVMContext.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 49 | #include <llvm/Module.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 50 | #include <llvm/Object/ObjectFile.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 51 | #include <llvm/PassManager.h> |
| 52 | #include <llvm/Support/Debug.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 53 | #include <llvm/Support/ELF.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 54 | #include <llvm/Support/FormattedStream.h> |
| 55 | #include <llvm/Support/ManagedStatic.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 56 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 57 | #include <llvm/Support/PassNameParser.h> |
| 58 | #include <llvm/Support/PluginLoader.h> |
| 59 | #include <llvm/Support/PrettyStackTrace.h> |
| 60 | #include <llvm/Support/Signals.h> |
| 61 | #include <llvm/Support/SystemUtils.h> |
| 62 | #include <llvm/Support/TargetRegistry.h> |
| 63 | #include <llvm/Support/TargetSelect.h> |
| 64 | #include <llvm/Support/ToolOutputFile.h> |
| 65 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 66 | #include <llvm/Support/system_error.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 67 | #include <llvm/Target/TargetData.h> |
| 68 | #include <llvm/Target/TargetLibraryInfo.h> |
| 69 | #include <llvm/Target/TargetMachine.h> |
Shih-wei Liao | f1cb9a5 | 2012-04-20 01:49:18 -0700 | [diff] [blame] | 70 | #include <llvm/Transforms/IPO.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 71 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 72 | #include <llvm/Transforms/Scalar.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 73 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 74 | #include <sys/types.h> |
| 75 | #include <sys/wait.h> |
| 76 | #include <unistd.h> |
| 77 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 78 | #include <string> |
| 79 | |
| 80 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 81 | namespace llvm { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 82 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 83 | ::llvm::FunctionPass* |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 84 | CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb, |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 85 | CompilerDriver* compiler, DexCompilationUnit* dex_compilation_unit); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 86 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 87 | ::llvm::Module* makeLLVMModuleContents(::llvm::Module* module); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 88 | |
| 89 | |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 90 | LlvmCompilationUnit::LlvmCompilationUnit(const CompilerLLVM* compiler_llvm, size_t cunit_idx) |
| 91 | : compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx) { |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 92 | driver_ = NULL; |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 93 | dex_compilation_unit_ = NULL; |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 94 | llvm_info_.reset(new LLVMInfo()); |
| 95 | context_.reset(llvm_info_->GetLLVMContext()); |
| 96 | module_ = llvm_info_->GetLLVMModule(); |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 97 | |
| 98 | // Include the runtime function declaration |
| 99 | makeLLVMModuleContents(module_); |
| 100 | |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 101 | |
| 102 | intrinsic_helper_.reset(new IntrinsicHelper(*context_, *module_)); |
| 103 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 104 | // Create IRBuilder |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 105 | irb_.reset(new IRBuilder(*context_, *module_, *intrinsic_helper_)); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 106 | |
| 107 | // We always need a switch case, so just use a normal function. |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 108 | switch(GetInstructionSet()) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 109 | default: |
| 110 | runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_)); |
| 111 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 112 | case kArm: |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 113 | runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_)); |
| 114 | break; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 115 | case kThumb2: |
| 116 | runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_)); |
| 117 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 118 | case kX86: |
| 119 | runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_)); |
| 120 | break; |
| 121 | } |
| 122 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 123 | irb_->SetRuntimeSupport(runtime_support_.get()); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 127 | LlvmCompilationUnit::~LlvmCompilationUnit() { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 128 | ::llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_ |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 129 | CHECK(llvm_context != NULL); |
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 | InstructionSet LlvmCompilationUnit::GetInstructionSet() const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 134 | return compiler_llvm_->GetInstructionSet(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 138 | bool LlvmCompilationUnit::Materialize() { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 139 | std::string elf_image; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 140 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 141 | // Compile and prelink ::llvm::Module |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 142 | if (!MaterializeToString(elf_image)) { |
| 143 | LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 144 | return false; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 145 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 146 | |
| 147 | #if 0 |
| 148 | // Dump the ELF image for debugging |
| 149 | std::string filename(StringPrintf("%s/Art%zu.elf", |
| 150 | GetArtCacheOrDie(GetAndroidData()).c_str(), |
| 151 | cunit_idx_)); |
| 152 | UniquePtr<File> output(OS::OpenFile(filename.c_str(), true)); |
| 153 | output->WriteFully(elf_image.data(), elf_image.size()); |
| 154 | #endif |
| 155 | |
| 156 | // Extract the .text section and prelink the code |
| 157 | if (!ExtractCodeAndPrelink(elf_image)) { |
| 158 | LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 162 | return true; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 165 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 166 | bool LlvmCompilationUnit::MaterializeToString(std::string& str_buffer) { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 167 | ::llvm::raw_string_ostream str_os(str_buffer); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 168 | return MaterializeToRawOStream(str_os); |
| 169 | } |
| 170 | |
| 171 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 172 | bool LlvmCompilationUnit::MaterializeToRawOStream(::llvm::raw_ostream& out_stream) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 173 | // Lookup the LLVM target |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 174 | std::string target_triple; |
| 175 | std::string target_cpu; |
| 176 | std::string target_attr; |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 177 | CompilerDriver::InstructionSetToLLVMTarget(GetInstructionSet(), target_triple, target_cpu, target_attr); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 178 | |
| 179 | std::string errmsg; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 180 | const ::llvm::Target* target = |
| 181 | ::llvm::TargetRegistry::lookupTarget(target_triple, errmsg); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 182 | |
| 183 | CHECK(target != NULL) << errmsg; |
| 184 | |
| 185 | // Target options |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 186 | ::llvm::TargetOptions target_options; |
| 187 | target_options.FloatABIType = ::llvm::FloatABI::Soft; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 188 | target_options.NoFramePointerElim = true; |
| 189 | target_options.NoFramePointerElimNonLeaf = true; |
| 190 | target_options.UseSoftFloat = false; |
TDYa127 | 3978da5 | 2012-05-19 07:45:39 -0700 | [diff] [blame] | 191 | target_options.EnableFastISel = false; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 192 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 193 | // Create the ::llvm::TargetMachine |
| 194 | ::llvm::OwningPtr< ::llvm::TargetMachine> target_machine( |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 195 | target->createTargetMachine(target_triple, target_cpu, target_attr, target_options, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 196 | ::llvm::Reloc::Static, ::llvm::CodeModel::Small, |
| 197 | ::llvm::CodeGenOpt::Aggressive)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 198 | |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 199 | CHECK(target_machine.get() != NULL) << "Failed to create target machine"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 200 | |
| 201 | // Add target data |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 202 | const ::llvm::TargetData* target_data = target_machine->getTargetData(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 203 | |
| 204 | // PassManager for code generation passes |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 205 | ::llvm::PassManager pm; |
| 206 | pm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 207 | |
| 208 | // FunctionPassManager for optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 209 | ::llvm::FunctionPassManager fpm(module_); |
| 210 | fpm.add(new ::llvm::TargetData(*target_data)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 211 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 212 | if (bitcode_filename_.empty()) { |
| 213 | // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the |
| 214 | // regular FunctionPass. |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 215 | fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 216 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 217 | } else { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 218 | ::llvm::FunctionPassManager fpm2(module_); |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 219 | fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(), |
Ian Rogers | 89756f2 | 2013-03-04 16:40:02 -0800 | [diff] [blame] | 220 | driver_, dex_compilation_unit_)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 221 | fpm2.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 222 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 223 | F != E; ++F) { |
| 224 | fpm2.run(*F); |
| 225 | } |
| 226 | fpm2.doFinalization(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 227 | |
| 228 | // Write bitcode to file |
| 229 | std::string errmsg; |
| 230 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 231 | ::llvm::OwningPtr< ::llvm::tool_output_file> out_file( |
| 232 | new ::llvm::tool_output_file(bitcode_filename_.c_str(), errmsg, |
| 233 | ::llvm::raw_fd_ostream::F_Binary)); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 234 | |
| 235 | |
| 236 | if (!errmsg.empty()) { |
| 237 | LOG(ERROR) << "Failed to create bitcode output file: " << errmsg; |
| 238 | return false; |
| 239 | } |
| 240 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 241 | ::llvm::WriteBitcodeToFile(module_, out_file->os()); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 242 | out_file->keep(); |
| 243 | } |
| 244 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 245 | // Add optimization pass |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 246 | ::llvm::PassManagerBuilder pm_builder; |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 247 | // TODO: Use inliner after we can do IPO. |
| 248 | pm_builder.Inliner = NULL; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 249 | //pm_builder.Inliner = ::llvm::createFunctionInliningPass(); |
| 250 | //pm_builder.Inliner = ::llvm::createAlwaysInlinerPass(); |
| 251 | //pm_builder.Inliner = ::llvm::createPartialInliningPass(); |
Shih-wei Liao | 415576b | 2012-04-23 15:28:53 -0700 | [diff] [blame] | 252 | pm_builder.OptLevel = 3; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 253 | pm_builder.DisableSimplifyLibCalls = 1; |
TDYa127 | e4c2ccc | 2012-05-13 21:10:36 -0700 | [diff] [blame] | 254 | pm_builder.DisableUnitAtATime = 1; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 255 | pm_builder.populateFunctionPassManager(fpm); |
TDYa127 | ce9c317 | 2012-05-15 06:09:27 -0700 | [diff] [blame] | 256 | pm_builder.populateModulePassManager(pm); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 257 | pm.add(::llvm::createStripDeadPrototypesPass()); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 258 | |
| 259 | // Add passes to emit ELF image |
| 260 | { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 261 | ::llvm::formatted_raw_ostream formatted_os(out_stream, false); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 262 | |
| 263 | // Ask the target to add backend passes as necessary. |
| 264 | if (target_machine->addPassesToEmitFile(pm, |
| 265 | formatted_os, |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 266 | ::llvm::TargetMachine::CGFT_ObjectFile, |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 267 | true)) { |
| 268 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 269 | return false; |
| 270 | } |
| 271 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 272 | // Run the per-function optimization |
| 273 | fpm.doInitialization(); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 274 | for (::llvm::Module::iterator F = module_->begin(), E = module_->end(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 275 | F != E; ++F) { |
| 276 | fpm.run(*F); |
| 277 | } |
| 278 | fpm.doFinalization(); |
| 279 | |
| 280 | // Run the code generation passes |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 281 | pm.run(*module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 286 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 287 | bool LlvmCompilationUnit::ExtractCodeAndPrelink(const std::string& elf_image) { |
Shih-wei Liao | 02a1e35 | 2012-06-30 00:42:07 -0700 | [diff] [blame] | 288 | if (GetInstructionSet() == kX86) { |
| 289 | compiled_code_.push_back(0xccU); |
| 290 | compiled_code_.push_back(0xccU); |
| 291 | compiled_code_.push_back(0xccU); |
| 292 | compiled_code_.push_back(0xccU); |
| 293 | return true; |
| 294 | } |
| 295 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 296 | ::llvm::OwningPtr< ::llvm::MemoryBuffer> elf_image_buff( |
| 297 | ::llvm::MemoryBuffer::getMemBuffer(::llvm::StringRef(elf_image.data(), |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 298 | elf_image.size()))); |
| 299 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 300 | ::llvm::OwningPtr< ::llvm::object::ObjectFile> elf_file( |
| 301 | ::llvm::object::ObjectFile::createELFObjectFile(elf_image_buff.take())); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 302 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 303 | ::llvm::error_code ec; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 304 | |
| 305 | const ProcedureLinkageTable& plt = compiler_llvm_->GetProcedureLinkageTable(); |
| 306 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 307 | for (::llvm::object::section_iterator |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 308 | sec_iter = elf_file->begin_sections(), |
| 309 | sec_end = elf_file->end_sections(); |
| 310 | sec_iter != sec_end; sec_iter.increment(ec)) { |
| 311 | |
| 312 | CHECK(ec == 0) << "Failed to read section because " << ec.message(); |
| 313 | |
| 314 | // Read the section information |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 315 | ::llvm::StringRef name; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 316 | uint64_t alignment = 0u; |
| 317 | uint64_t size = 0u; |
| 318 | |
| 319 | CHECK(sec_iter->getName(name) == 0); |
| 320 | CHECK(sec_iter->getSize(size) == 0); |
| 321 | CHECK(sec_iter->getAlignment(alignment) == 0); |
| 322 | |
| 323 | if (name == ".data" || name == ".bss" || name == ".rodata") { |
| 324 | if (size > 0) { |
| 325 | LOG(FATAL) << "Compilation unit " << cunit_idx_ << " has non-empty " |
| 326 | << name.str() << " section"; |
| 327 | } |
| 328 | |
| 329 | } else if (name == "" || name == ".rel.text" || |
| 330 | name == ".ARM.attributes" || name == ".symtab" || |
| 331 | name == ".strtab" || name == ".shstrtab") { |
| 332 | // We can ignore these sections. We don't have to copy them into |
| 333 | // the result Oat file. |
| 334 | |
| 335 | } else if (name == ".text") { |
| 336 | // Ensure the alignment requirement is less than or equal to |
| 337 | // kArchAlignment |
| 338 | CheckCodeAlign(alignment); |
| 339 | |
| 340 | // Copy the compiled code |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 341 | ::llvm::StringRef contents; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 342 | CHECK(sec_iter->getContents(contents) == 0); |
| 343 | |
| 344 | copy(contents.data(), |
| 345 | contents.data() + contents.size(), |
| 346 | back_inserter(compiled_code_)); |
| 347 | |
| 348 | // Prelink the compiled code |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 349 | for (::llvm::object::relocation_iterator |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 350 | rel_iter = sec_iter->begin_relocations(), |
| 351 | rel_end = sec_iter->end_relocations(); rel_iter != rel_end; |
| 352 | rel_iter.increment(ec)) { |
| 353 | |
| 354 | CHECK(ec == 0) << "Failed to read relocation because " << ec.message(); |
| 355 | |
| 356 | // Read the relocation information |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 357 | ::llvm::object::SymbolRef sym_ref; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 358 | uint64_t rel_offset = 0; |
| 359 | uint64_t rel_type = 0; |
| 360 | int64_t rel_addend = 0; |
| 361 | |
| 362 | CHECK(rel_iter->getSymbol(sym_ref) == 0); |
| 363 | CHECK(rel_iter->getOffset(rel_offset) == 0); |
| 364 | CHECK(rel_iter->getType(rel_type) == 0); |
| 365 | CHECK(rel_iter->getAdditionalInfo(rel_addend) == 0); |
| 366 | |
| 367 | // Read the symbol related to this relocation fixup |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 368 | ::llvm::StringRef sym_name; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 369 | CHECK(sym_ref.getName(sym_name) == 0); |
| 370 | |
| 371 | // Relocate the fixup. |
| 372 | // TODO: Support more relocation type. |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 373 | CHECK(rel_type == ::llvm::ELF::R_ARM_ABS32); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 374 | CHECK_LE(rel_offset + 4, compiled_code_.size()); |
| 375 | |
| 376 | uintptr_t dest_addr = plt.GetEntryAddress(sym_name.str().c_str()); |
| 377 | uintptr_t final_addr = dest_addr + rel_addend; |
| 378 | compiled_code_[rel_offset] = final_addr & 0xff; |
| 379 | compiled_code_[rel_offset + 1] = (final_addr >> 8) & 0xff; |
| 380 | compiled_code_[rel_offset + 2] = (final_addr >> 16) & 0xff; |
| 381 | compiled_code_[rel_offset + 3] = (final_addr >> 24) & 0xff; |
| 382 | } |
| 383 | |
| 384 | } else { |
| 385 | LOG(WARNING) << "Unexpected section: " << name.str(); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | // Check whether the align is less than or equal to the code alignment of |
| 394 | // that architecture. Since the Oat writer only guarantee that the compiled |
| 395 | // method being aligned to kArchAlignment, we have no way to align the ELf |
| 396 | // section if the section alignment is greater than kArchAlignment. |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 397 | void LlvmCompilationUnit::CheckCodeAlign(uint32_t align) const { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 398 | InstructionSet insn_set = GetInstructionSet(); |
| 399 | switch (insn_set) { |
| 400 | case kThumb2: |
| 401 | case kArm: |
| 402 | CHECK_LE(align, static_cast<uint32_t>(kArmAlignment)); |
| 403 | break; |
| 404 | |
| 405 | case kX86: |
| 406 | CHECK_LE(align, static_cast<uint32_t>(kX86Alignment)); |
| 407 | break; |
| 408 | |
| 409 | case kMips: |
| 410 | CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment)); |
| 411 | break; |
| 412 | |
| 413 | default: |
| 414 | LOG(FATAL) << "Unknown instruction set: " << insn_set; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame^] | 419 | } // namespace llvm |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 420 | } // namespace art |