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 | |
| 17 | #include "compilation_unit.h" |
| 18 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 19 | #include "compiled_method.h" |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 20 | #include "compiler_llvm.h" |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 21 | #include "file.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" |
| 24 | #include "logging.h" |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 25 | #include "os.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 26 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 27 | #include "runtime_support_builder_arm.h" |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 28 | #include "runtime_support_builder_thumb2.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 29 | #include "runtime_support_builder_x86.h" |
| 30 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 31 | #include <llvm/ADT/OwningPtr.h> |
| 32 | #include <llvm/ADT/StringSet.h> |
| 33 | #include <llvm/ADT/Triple.h> |
| 34 | #include <llvm/Analysis/CallGraph.h> |
| 35 | #include <llvm/Analysis/DebugInfo.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 36 | #include <llvm/Analysis/Dominators.h> |
| 37 | #include <llvm/Analysis/LoopInfo.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 38 | #include <llvm/Analysis/LoopPass.h> |
| 39 | #include <llvm/Analysis/RegionPass.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 40 | #include <llvm/Analysis/ScalarEvolution.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 41 | #include <llvm/Analysis/Verifier.h> |
| 42 | #include <llvm/Assembly/PrintModulePass.h> |
| 43 | #include <llvm/Bitcode/ReaderWriter.h> |
| 44 | #include <llvm/CallGraphSCCPass.h> |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 45 | #include <llvm/CodeGen/MachineFrameInfo.h> |
| 46 | #include <llvm/CodeGen/MachineFunction.h> |
| 47 | #include <llvm/CodeGen/MachineFunctionPass.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 48 | #include <llvm/DerivedTypes.h> |
| 49 | #include <llvm/LLVMContext.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 50 | #include <llvm/Module.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 51 | #include <llvm/Object/ObjectFile.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 52 | #include <llvm/PassManager.h> |
| 53 | #include <llvm/Support/Debug.h> |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 54 | #include <llvm/Support/ELF.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 55 | #include <llvm/Support/FormattedStream.h> |
| 56 | #include <llvm/Support/ManagedStatic.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 57 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 58 | #include <llvm/Support/PassNameParser.h> |
| 59 | #include <llvm/Support/PluginLoader.h> |
| 60 | #include <llvm/Support/PrettyStackTrace.h> |
| 61 | #include <llvm/Support/Signals.h> |
| 62 | #include <llvm/Support/SystemUtils.h> |
| 63 | #include <llvm/Support/TargetRegistry.h> |
| 64 | #include <llvm/Support/TargetSelect.h> |
| 65 | #include <llvm/Support/ToolOutputFile.h> |
| 66 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 67 | #include <llvm/Support/system_error.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 68 | #include <llvm/Target/TargetData.h> |
| 69 | #include <llvm/Target/TargetLibraryInfo.h> |
| 70 | #include <llvm/Target/TargetMachine.h> |
Shih-wei Liao | f1cb9a5 | 2012-04-20 01:49:18 -0700 | [diff] [blame] | 71 | #include <llvm/Transforms/IPO.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 72 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 73 | #include <llvm/Transforms/Scalar.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 74 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 75 | #include <sys/types.h> |
| 76 | #include <sys/wait.h> |
| 77 | #include <unistd.h> |
| 78 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 79 | #include <string> |
| 80 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 81 | namespace { |
| 82 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 83 | // TODO: We may need something to manage these passes. |
| 84 | // TODO: We need high-level IR to analysis and do this at the IRBuilder level. |
| 85 | class AddSuspendCheckToLoopLatchPass : public llvm::LoopPass { |
| 86 | public: |
| 87 | static char ID; |
| 88 | |
| 89 | AddSuspendCheckToLoopLatchPass() : llvm::LoopPass(ID), irb_(NULL) { |
| 90 | LOG(FATAL) << "Unexpected instantiation of AddSuspendCheckToLoopLatchPass"; |
| 91 | // NOTE: We have to declare this constructor for llvm::RegisterPass, but |
| 92 | // this constructor won't work because we have no information on |
| 93 | // IRBuilder. Thus, we should place a LOG(FATAL) here. |
| 94 | } |
| 95 | |
| 96 | AddSuspendCheckToLoopLatchPass(art::compiler_llvm::IRBuilder* irb) |
| 97 | : llvm::LoopPass(ID), irb_(irb) { |
| 98 | } |
| 99 | |
| 100 | virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 101 | AU.addRequired<llvm::LoopInfo>(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 102 | AU.addRequiredID(llvm::LoopSimplifyID); |
| 103 | |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame] | 104 | AU.addPreserved<llvm::DominatorTree>(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 105 | AU.addPreserved<llvm::LoopInfo>(); |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame] | 106 | AU.addPreservedID(llvm::LoopSimplifyID); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 107 | AU.addPreserved<llvm::ScalarEvolution>(); |
| 108 | AU.addPreservedID(llvm::BreakCriticalEdgesID); |
| 109 | } |
| 110 | |
| 111 | virtual bool runOnLoop(llvm::Loop *loop, llvm::LPPassManager &lpm) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 112 | llvm::LoopInfo* loop_info = &getAnalysis<llvm::LoopInfo>(); |
| 113 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 114 | CHECK_EQ(loop->getNumBackEdges(), 1U) << "Loop must be simplified!"; |
| 115 | llvm::BasicBlock* bb = loop->getLoopLatch(); |
| 116 | CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist."; |
| 117 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 118 | llvm::BasicBlock* tb = bb->splitBasicBlock(bb->getTerminator(), "suspend_exit"); |
| 119 | // Remove unconditional branch which is added by splitBasicBlock. |
| 120 | bb->getTerminator()->eraseFromParent(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 121 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 122 | irb_->SetInsertPoint(bb); |
| 123 | irb_->Runtime().EmitTestSuspend(); |
| 124 | irb_->CreateBr(tb); |
| 125 | |
| 126 | loop->addBasicBlockToLoop(tb, loop_info->getBase()); |
| 127 | // EmitTestSuspend() creates some basic blocks. We should add them to using |
| 128 | // addBasicBlockToLoop(...) as above. |
| 129 | for (llvm::succ_iterator succ_iter = llvm::succ_begin(bb), succ_end = llvm::succ_end(bb); |
| 130 | succ_iter != succ_end; |
| 131 | succ_iter++) { |
| 132 | loop->addBasicBlockToLoop(*succ_iter, loop_info->getBase()); |
| 133 | } |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | art::compiler_llvm::IRBuilder* irb_; |
| 140 | }; |
| 141 | |
| 142 | char AddSuspendCheckToLoopLatchPass::ID = 0; |
| 143 | |
| 144 | llvm::RegisterPass<AddSuspendCheckToLoopLatchPass> reg_add_suspend_check_to_loop_latch_pass_( |
| 145 | "add-suspend-check-to-loop-latch", "Add suspend check to loop latch pass", false, false); |
| 146 | |
| 147 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 148 | } // end anonymous namespace |
| 149 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 150 | namespace art { |
| 151 | namespace compiler_llvm { |
| 152 | |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 153 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 154 | llvm::FunctionPass* |
| 155 | CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper, |
| 156 | IRBuilder& irb); |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 157 | #elif defined(ART_USE_QUICK_COMPILER) |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 158 | llvm::FunctionPass* |
| 159 | CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper, IRBuilder& irb, |
| 160 | Compiler* compiler, OatCompilationUnit* oat_compilation_unit); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 161 | #endif |
| 162 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 163 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
| 164 | |
| 165 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 166 | CompilationUnit::CompilationUnit(const CompilerLLVM* compiler_llvm, |
| 167 | size_t cunit_idx) |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 168 | : compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx) { |
| 169 | #if !defined(ART_USE_QUICK_COMPILER) |
| 170 | context_.reset(new llvm::LLVMContext()); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 171 | module_ = new llvm::Module("art", *context_); |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 172 | #else |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 173 | compiler_ = NULL; |
| 174 | oat_compilation_unit_ = NULL; |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 175 | quick_ctx_.reset(new QuickCompiler()); |
| 176 | context_.reset(quick_ctx_->GetLLVMContext()); |
| 177 | module_ = quick_ctx_->GetLLVMModule(); |
| 178 | #endif |
| 179 | |
| 180 | // Include the runtime function declaration |
| 181 | makeLLVMModuleContents(module_); |
| 182 | |
| 183 | #if defined(ART_USE_DEXLANG_FRONTEND) |
| 184 | dex_lang_ctx_ = new greenland::DexLang::Context(*module_); |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 185 | #endif |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 186 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 187 | // Create IRBuilder |
| 188 | irb_.reset(new IRBuilder(*context_, *module_)); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 189 | |
| 190 | // We always need a switch case, so just use a normal function. |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 191 | switch(GetInstructionSet()) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 192 | default: |
| 193 | runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_)); |
| 194 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 195 | case kArm: |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 196 | runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_)); |
| 197 | break; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 198 | case kThumb2: |
| 199 | runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_)); |
| 200 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 201 | case kX86: |
| 202 | runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_)); |
| 203 | break; |
| 204 | } |
| 205 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 206 | irb_->SetRuntimeSupport(runtime_support_.get()); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
| 210 | CompilationUnit::~CompilationUnit() { |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 211 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 212 | delete dex_lang_ctx_; |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 213 | #elif defined(ART_USE_QUICK_COMPILER) |
| 214 | llvm::LLVMContext* llvm_context = context_.release(); // Managed by quick_ctx_ |
| 215 | CHECK(llvm_context != NULL); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 216 | #endif |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 220 | InstructionSet CompilationUnit::GetInstructionSet() const { |
| 221 | return compiler_llvm_->GetInstructionSet(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 225 | bool CompilationUnit::Materialize() { |
| 226 | std::string elf_image; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 227 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 228 | // Compile and prelink llvm::Module |
| 229 | if (!MaterializeToString(elf_image)) { |
| 230 | LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 231 | return false; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 232 | } |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 233 | |
| 234 | #if 0 |
| 235 | // Dump the ELF image for debugging |
| 236 | std::string filename(StringPrintf("%s/Art%zu.elf", |
| 237 | GetArtCacheOrDie(GetAndroidData()).c_str(), |
| 238 | cunit_idx_)); |
| 239 | UniquePtr<File> output(OS::OpenFile(filename.c_str(), true)); |
| 240 | output->WriteFully(elf_image.data(), elf_image.size()); |
| 241 | #endif |
| 242 | |
| 243 | // Extract the .text section and prelink the code |
| 244 | if (!ExtractCodeAndPrelink(elf_image)) { |
| 245 | LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 249 | return true; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 252 | |
| 253 | bool CompilationUnit::MaterializeToString(std::string& str_buffer) { |
| 254 | llvm::raw_string_ostream str_os(str_buffer); |
| 255 | return MaterializeToRawOStream(str_os); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | bool CompilationUnit::MaterializeToRawOStream(llvm::raw_ostream& out_stream) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 260 | // Lookup the LLVM target |
Logan Chien | 1258417 | 2012-07-10 04:07:28 -0700 | [diff] [blame] | 261 | const char* target_triple = NULL; |
| 262 | const char* target_cpu = ""; |
| 263 | const char* target_attr = NULL; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 264 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 265 | InstructionSet insn_set = GetInstructionSet(); |
| 266 | switch (insn_set) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 267 | case kThumb2: |
| 268 | target_triple = "thumb-none-linux-gnueabi"; |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 269 | target_cpu = "cortex-a9"; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 270 | target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case kArm: |
| 274 | target_triple = "armv7-none-linux-gnueabi"; |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 275 | // TODO: Fix for Nexus S. |
| 276 | target_cpu = "cortex-a9"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 277 | // TODO: Fix for Xoom. |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 278 | target_attr = "+v7,+neon,+neonfp,+vfp3,+db"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 279 | break; |
| 280 | |
| 281 | case kX86: |
| 282 | target_triple = "i386-pc-linux-gnu"; |
| 283 | target_attr = ""; |
| 284 | break; |
| 285 | |
| 286 | case kMips: |
| 287 | target_triple = "mipsel-unknown-linux"; |
| 288 | target_attr = "mips32r2"; |
| 289 | break; |
| 290 | |
| 291 | default: |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 292 | LOG(FATAL) << "Unknown instruction set: " << insn_set; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | std::string errmsg; |
Logan Chien | 1258417 | 2012-07-10 04:07:28 -0700 | [diff] [blame] | 296 | const llvm::Target* target = |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 297 | llvm::TargetRegistry::lookupTarget(target_triple, errmsg); |
| 298 | |
| 299 | CHECK(target != NULL) << errmsg; |
| 300 | |
| 301 | // Target options |
| 302 | llvm::TargetOptions target_options; |
| 303 | target_options.FloatABIType = llvm::FloatABI::Soft; |
| 304 | target_options.NoFramePointerElim = true; |
| 305 | target_options.NoFramePointerElimNonLeaf = true; |
| 306 | target_options.UseSoftFloat = false; |
TDYa127 | 3978da5 | 2012-05-19 07:45:39 -0700 | [diff] [blame] | 307 | target_options.EnableFastISel = false; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 308 | |
| 309 | // Create the llvm::TargetMachine |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 310 | llvm::OwningPtr<llvm::TargetMachine> target_machine( |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 311 | target->createTargetMachine(target_triple, target_cpu, target_attr, target_options, |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 312 | llvm::Reloc::Static, llvm::CodeModel::Small, |
Shih-wei Liao | dac5eb2 | 2012-06-03 14:06:04 -0700 | [diff] [blame] | 313 | llvm::CodeGenOpt::Aggressive)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 314 | |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 315 | CHECK(target_machine.get() != NULL) << "Failed to create target machine"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 316 | |
| 317 | // Add target data |
Logan Chien | 1258417 | 2012-07-10 04:07:28 -0700 | [diff] [blame] | 318 | const llvm::TargetData* target_data = target_machine->getTargetData(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 319 | |
| 320 | // PassManager for code generation passes |
| 321 | llvm::PassManager pm; |
| 322 | pm.add(new llvm::TargetData(*target_data)); |
| 323 | |
| 324 | // FunctionPassManager for optimization pass |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 325 | llvm::FunctionPassManager fpm(module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 326 | fpm.add(new llvm::TargetData(*target_data)); |
| 327 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 328 | if (bitcode_filename_.empty()) { |
| 329 | // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the |
| 330 | // regular FunctionPass. |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 331 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 332 | fpm.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get())); |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 333 | #elif defined(ART_USE_QUICK_COMPILER) |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 334 | fpm.add(CreateGBCExpanderPass(*quick_ctx_->GetIntrinsicHelper(), *irb_.get(), |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 335 | compiler_, oat_compilation_unit_)); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 336 | #endif |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 337 | fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get())); |
| 338 | } else { |
| 339 | // Run AddSuspendCheckToLoopLatchPass before we write the bitcode to file. |
| 340 | llvm::FunctionPassManager fpm2(module_); |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 341 | #if defined(ART_USE_DEXLANG_FRONTEND) |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 342 | fpm2.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get())); |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 343 | #elif defined(ART_USE_QUICK_COMPILER) |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame^] | 344 | fpm2.add(CreateGBCExpanderPass(*quick_ctx_->GetIntrinsicHelper(), *irb_.get(), |
Shih-wei Liao | bb33f2f | 2012-08-23 13:20:00 -0700 | [diff] [blame] | 345 | compiler_, oat_compilation_unit_)); |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 346 | #endif |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 347 | fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get())); |
| 348 | fpm2.doInitialization(); |
| 349 | for (llvm::Module::iterator F = module_->begin(), E = module_->end(); |
| 350 | F != E; ++F) { |
| 351 | fpm2.run(*F); |
| 352 | } |
| 353 | fpm2.doFinalization(); |
| 354 | |
| 355 | |
| 356 | // Write bitcode to file |
| 357 | std::string errmsg; |
| 358 | |
| 359 | llvm::OwningPtr<llvm::tool_output_file> out_file( |
| 360 | new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg, |
| 361 | llvm::raw_fd_ostream::F_Binary)); |
| 362 | |
| 363 | |
| 364 | if (!errmsg.empty()) { |
| 365 | LOG(ERROR) << "Failed to create bitcode output file: " << errmsg; |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | llvm::WriteBitcodeToFile(module_, out_file->os()); |
| 370 | out_file->keep(); |
| 371 | } |
| 372 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 373 | // Add optimization pass |
| 374 | llvm::PassManagerBuilder pm_builder; |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 375 | // TODO: Use inliner after we can do IPO. |
| 376 | pm_builder.Inliner = NULL; |
Shih-wei Liao | e0e4024 | 2012-05-08 01:04:03 -0700 | [diff] [blame] | 377 | //pm_builder.Inliner = llvm::createFunctionInliningPass(); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 378 | //pm_builder.Inliner = llvm::createAlwaysInlinerPass(); |
Shih-wei Liao | 415576b | 2012-04-23 15:28:53 -0700 | [diff] [blame] | 379 | //pm_builder.Inliner = llvm::createPartialInliningPass(); |
| 380 | pm_builder.OptLevel = 3; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 381 | pm_builder.DisableSimplifyLibCalls = 1; |
TDYa127 | e4c2ccc | 2012-05-13 21:10:36 -0700 | [diff] [blame] | 382 | pm_builder.DisableUnitAtATime = 1; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 383 | pm_builder.populateFunctionPassManager(fpm); |
TDYa127 | ce9c317 | 2012-05-15 06:09:27 -0700 | [diff] [blame] | 384 | pm_builder.populateModulePassManager(pm); |
| 385 | pm.add(llvm::createStripDeadPrototypesPass()); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 386 | |
| 387 | // Add passes to emit ELF image |
| 388 | { |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 389 | llvm::formatted_raw_ostream formatted_os(out_stream, false); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 390 | |
| 391 | // Ask the target to add backend passes as necessary. |
| 392 | if (target_machine->addPassesToEmitFile(pm, |
| 393 | formatted_os, |
| 394 | llvm::TargetMachine::CGFT_ObjectFile, |
| 395 | true)) { |
| 396 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 397 | return false; |
| 398 | } |
| 399 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 400 | // Run the per-function optimization |
| 401 | fpm.doInitialization(); |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 402 | for (llvm::Module::iterator F = module_->begin(), E = module_->end(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 403 | F != E; ++F) { |
| 404 | fpm.run(*F); |
| 405 | } |
| 406 | fpm.doFinalization(); |
| 407 | |
| 408 | // Run the code generation passes |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 409 | pm.run(*module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 414 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 415 | |
| 416 | bool CompilationUnit::ExtractCodeAndPrelink(const std::string& elf_image) { |
Shih-wei Liao | 02a1e35 | 2012-06-30 00:42:07 -0700 | [diff] [blame] | 417 | if (GetInstructionSet() == kX86) { |
| 418 | compiled_code_.push_back(0xccU); |
| 419 | compiled_code_.push_back(0xccU); |
| 420 | compiled_code_.push_back(0xccU); |
| 421 | compiled_code_.push_back(0xccU); |
| 422 | return true; |
| 423 | } |
| 424 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 425 | llvm::OwningPtr<llvm::MemoryBuffer> elf_image_buff( |
| 426 | llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(elf_image.data(), |
| 427 | elf_image.size()))); |
| 428 | |
| 429 | llvm::OwningPtr<llvm::object::ObjectFile> elf_file( |
| 430 | llvm::object::ObjectFile::createELFObjectFile(elf_image_buff.take())); |
| 431 | |
| 432 | llvm::error_code ec; |
| 433 | |
| 434 | const ProcedureLinkageTable& plt = compiler_llvm_->GetProcedureLinkageTable(); |
| 435 | |
| 436 | for (llvm::object::section_iterator |
| 437 | sec_iter = elf_file->begin_sections(), |
| 438 | sec_end = elf_file->end_sections(); |
| 439 | sec_iter != sec_end; sec_iter.increment(ec)) { |
| 440 | |
| 441 | CHECK(ec == 0) << "Failed to read section because " << ec.message(); |
| 442 | |
| 443 | // Read the section information |
| 444 | llvm::StringRef name; |
| 445 | uint64_t alignment = 0u; |
| 446 | uint64_t size = 0u; |
| 447 | |
| 448 | CHECK(sec_iter->getName(name) == 0); |
| 449 | CHECK(sec_iter->getSize(size) == 0); |
| 450 | CHECK(sec_iter->getAlignment(alignment) == 0); |
| 451 | |
| 452 | if (name == ".data" || name == ".bss" || name == ".rodata") { |
| 453 | if (size > 0) { |
| 454 | LOG(FATAL) << "Compilation unit " << cunit_idx_ << " has non-empty " |
| 455 | << name.str() << " section"; |
| 456 | } |
| 457 | |
| 458 | } else if (name == "" || name == ".rel.text" || |
| 459 | name == ".ARM.attributes" || name == ".symtab" || |
| 460 | name == ".strtab" || name == ".shstrtab") { |
| 461 | // We can ignore these sections. We don't have to copy them into |
| 462 | // the result Oat file. |
| 463 | |
| 464 | } else if (name == ".text") { |
| 465 | // Ensure the alignment requirement is less than or equal to |
| 466 | // kArchAlignment |
| 467 | CheckCodeAlign(alignment); |
| 468 | |
| 469 | // Copy the compiled code |
| 470 | llvm::StringRef contents; |
| 471 | CHECK(sec_iter->getContents(contents) == 0); |
| 472 | |
| 473 | copy(contents.data(), |
| 474 | contents.data() + contents.size(), |
| 475 | back_inserter(compiled_code_)); |
| 476 | |
| 477 | // Prelink the compiled code |
| 478 | for (llvm::object::relocation_iterator |
| 479 | rel_iter = sec_iter->begin_relocations(), |
| 480 | rel_end = sec_iter->end_relocations(); rel_iter != rel_end; |
| 481 | rel_iter.increment(ec)) { |
| 482 | |
| 483 | CHECK(ec == 0) << "Failed to read relocation because " << ec.message(); |
| 484 | |
| 485 | // Read the relocation information |
| 486 | llvm::object::SymbolRef sym_ref; |
| 487 | uint64_t rel_offset = 0; |
| 488 | uint64_t rel_type = 0; |
| 489 | int64_t rel_addend = 0; |
| 490 | |
| 491 | CHECK(rel_iter->getSymbol(sym_ref) == 0); |
| 492 | CHECK(rel_iter->getOffset(rel_offset) == 0); |
| 493 | CHECK(rel_iter->getType(rel_type) == 0); |
| 494 | CHECK(rel_iter->getAdditionalInfo(rel_addend) == 0); |
| 495 | |
| 496 | // Read the symbol related to this relocation fixup |
| 497 | llvm::StringRef sym_name; |
| 498 | CHECK(sym_ref.getName(sym_name) == 0); |
| 499 | |
| 500 | // Relocate the fixup. |
| 501 | // TODO: Support more relocation type. |
| 502 | CHECK(rel_type == llvm::ELF::R_ARM_ABS32); |
| 503 | CHECK_LE(rel_offset + 4, compiled_code_.size()); |
| 504 | |
| 505 | uintptr_t dest_addr = plt.GetEntryAddress(sym_name.str().c_str()); |
| 506 | uintptr_t final_addr = dest_addr + rel_addend; |
| 507 | compiled_code_[rel_offset] = final_addr & 0xff; |
| 508 | compiled_code_[rel_offset + 1] = (final_addr >> 8) & 0xff; |
| 509 | compiled_code_[rel_offset + 2] = (final_addr >> 16) & 0xff; |
| 510 | compiled_code_[rel_offset + 3] = (final_addr >> 24) & 0xff; |
| 511 | } |
| 512 | |
| 513 | } else { |
| 514 | LOG(WARNING) << "Unexpected section: " << name.str(); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | // Check whether the align is less than or equal to the code alignment of |
| 523 | // that architecture. Since the Oat writer only guarantee that the compiled |
| 524 | // method being aligned to kArchAlignment, we have no way to align the ELf |
| 525 | // section if the section alignment is greater than kArchAlignment. |
| 526 | void CompilationUnit::CheckCodeAlign(uint32_t align) const { |
| 527 | InstructionSet insn_set = GetInstructionSet(); |
| 528 | switch (insn_set) { |
| 529 | case kThumb2: |
| 530 | case kArm: |
| 531 | CHECK_LE(align, static_cast<uint32_t>(kArmAlignment)); |
| 532 | break; |
| 533 | |
| 534 | case kX86: |
| 535 | CHECK_LE(align, static_cast<uint32_t>(kX86Alignment)); |
| 536 | break; |
| 537 | |
| 538 | case kMips: |
| 539 | CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment)); |
| 540 | break; |
| 541 | |
| 542 | default: |
| 543 | LOG(FATAL) << "Unknown instruction set: " << insn_set; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 548 | } // namespace compiler_llvm |
| 549 | } // namespace art |