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" |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 20 | #include "file.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 21 | #include "instruction_set.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 22 | #include "ir_builder.h" |
| 23 | #include "logging.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> |
| 50 | #include <llvm/PassManager.h> |
| 51 | #include <llvm/Support/Debug.h> |
| 52 | #include <llvm/Support/FormattedStream.h> |
| 53 | #include <llvm/Support/ManagedStatic.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 54 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 55 | #include <llvm/Support/PassNameParser.h> |
| 56 | #include <llvm/Support/PluginLoader.h> |
| 57 | #include <llvm/Support/PrettyStackTrace.h> |
| 58 | #include <llvm/Support/Signals.h> |
| 59 | #include <llvm/Support/SystemUtils.h> |
| 60 | #include <llvm/Support/TargetRegistry.h> |
| 61 | #include <llvm/Support/TargetSelect.h> |
| 62 | #include <llvm/Support/ToolOutputFile.h> |
| 63 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 64 | #include <llvm/Support/system_error.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 65 | #include <llvm/Target/TargetData.h> |
| 66 | #include <llvm/Target/TargetLibraryInfo.h> |
| 67 | #include <llvm/Target/TargetMachine.h> |
Shih-wei Liao | f1cb9a5 | 2012-04-20 01:49:18 -0700 | [diff] [blame] | 68 | #include <llvm/Transforms/IPO.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 69 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 70 | #include <llvm/Transforms/Scalar.h> |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 71 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 72 | #include <sys/types.h> |
| 73 | #include <sys/wait.h> |
| 74 | #include <unistd.h> |
| 75 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 76 | #include <string> |
| 77 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 78 | namespace { |
| 79 | |
| 80 | class UpdateFrameSizePass : public llvm::MachineFunctionPass { |
| 81 | public: |
| 82 | static char ID; |
| 83 | |
| 84 | UpdateFrameSizePass() : llvm::MachineFunctionPass(ID), cunit_(NULL) { |
| 85 | LOG(FATAL) << "Unexpected instantiation of UpdateFrameSizePass"; |
| 86 | // NOTE: We have to declare this constructor for llvm::RegisterPass, but |
| 87 | // this constructor won't work because we have no information on |
| 88 | // CompilationUnit. Thus, we should place a LOG(FATAL) here. |
| 89 | } |
| 90 | |
| 91 | UpdateFrameSizePass(art::compiler_llvm::CompilationUnit* cunit) |
| 92 | : llvm::MachineFunctionPass(ID), cunit_(cunit) { |
| 93 | } |
| 94 | |
| 95 | virtual bool runOnMachineFunction(llvm::MachineFunction &MF) { |
| 96 | cunit_->UpdateFrameSizeInBytes(MF.getFunction(), |
| 97 | MF.getFrameInfo()->getStackSize()); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | art::compiler_llvm::CompilationUnit* cunit_; |
| 103 | }; |
| 104 | |
| 105 | char UpdateFrameSizePass::ID = 0; |
| 106 | |
| 107 | llvm::RegisterPass<UpdateFrameSizePass> reg_update_frame_size_pass_( |
| 108 | "update-frame-size", "Update frame size pass", false, false); |
| 109 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 110 | |
| 111 | // TODO: We may need something to manage these passes. |
| 112 | // TODO: We need high-level IR to analysis and do this at the IRBuilder level. |
| 113 | class AddSuspendCheckToLoopLatchPass : public llvm::LoopPass { |
| 114 | public: |
| 115 | static char ID; |
| 116 | |
| 117 | AddSuspendCheckToLoopLatchPass() : llvm::LoopPass(ID), irb_(NULL) { |
| 118 | LOG(FATAL) << "Unexpected instantiation of AddSuspendCheckToLoopLatchPass"; |
| 119 | // NOTE: We have to declare this constructor for llvm::RegisterPass, but |
| 120 | // this constructor won't work because we have no information on |
| 121 | // IRBuilder. Thus, we should place a LOG(FATAL) here. |
| 122 | } |
| 123 | |
| 124 | AddSuspendCheckToLoopLatchPass(art::compiler_llvm::IRBuilder* irb) |
| 125 | : llvm::LoopPass(ID), irb_(irb) { |
| 126 | } |
| 127 | |
| 128 | virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const { |
| 129 | AU.addRequiredID(llvm::LoopSimplifyID); |
| 130 | |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 131 | AU.addPreserved<llvm::DominatorTree>(); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 132 | AU.addPreserved<llvm::LoopInfo>(); |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 133 | AU.addPreservedID(llvm::LoopSimplifyID); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 134 | AU.addPreserved<llvm::ScalarEvolution>(); |
| 135 | AU.addPreservedID(llvm::BreakCriticalEdgesID); |
| 136 | } |
| 137 | |
| 138 | virtual bool runOnLoop(llvm::Loop *loop, llvm::LPPassManager &lpm) { |
| 139 | CHECK_EQ(loop->getNumBackEdges(), 1U) << "Loop must be simplified!"; |
| 140 | llvm::BasicBlock* bb = loop->getLoopLatch(); |
| 141 | CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist."; |
| 142 | |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 143 | irb_->SetInsertPoint(bb->getTerminator()); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 144 | |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 145 | using art::compiler_llvm::runtime_support::TestSuspend; |
| 146 | llvm::Value* runtime_func = irb_->GetRuntime(TestSuspend); |
| 147 | irb_->CreateCall(runtime_func, irb_->getJNull()); |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | art::compiler_llvm::IRBuilder* irb_; |
| 154 | }; |
| 155 | |
| 156 | char AddSuspendCheckToLoopLatchPass::ID = 0; |
| 157 | |
| 158 | llvm::RegisterPass<AddSuspendCheckToLoopLatchPass> reg_add_suspend_check_to_loop_latch_pass_( |
| 159 | "add-suspend-check-to-loop-latch", "Add suspend check to loop latch pass", false, false); |
| 160 | |
| 161 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 162 | } // end anonymous namespace |
| 163 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 164 | namespace art { |
| 165 | namespace compiler_llvm { |
| 166 | |
| 167 | llvm::Module* makeLLVMModuleContents(llvm::Module* module); |
| 168 | |
| 169 | |
Logan Chien | 6546ec5 | 2012-03-17 20:08:29 +0800 | [diff] [blame] | 170 | CompilationUnit::CompilationUnit(InstructionSet insn_set, size_t elf_idx) |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 171 | : cunit_lock_("compilation_unit_lock"), insn_set_(insn_set), elf_idx_(elf_idx), |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 172 | context_(new llvm::LLVMContext()), compiled_methods_map_(new CompiledMethodMap()), |
| 173 | mem_usage_(0), num_elf_funcs_(0) { |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 174 | |
| 175 | // Create the module and include the runtime function declaration |
| 176 | module_ = new llvm::Module("art", *context_); |
| 177 | makeLLVMModuleContents(module_); |
| 178 | |
| 179 | // Create IRBuilder |
| 180 | irb_.reset(new IRBuilder(*context_, *module_)); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 181 | |
| 182 | // We always need a switch case, so just use a normal function. |
| 183 | switch(insn_set_) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 184 | default: |
| 185 | runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_)); |
| 186 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 187 | case kArm: |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 188 | runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_)); |
| 189 | break; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 190 | case kThumb2: |
| 191 | runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_)); |
| 192 | break; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 193 | case kX86: |
| 194 | runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_)); |
| 195 | break; |
| 196 | } |
| 197 | |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 198 | runtime_support_->OptimizeRuntimeSupport(); |
| 199 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 200 | irb_->SetRuntimeSupport(runtime_support_.get()); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | CompilationUnit::~CompilationUnit() { |
| 205 | } |
| 206 | |
| 207 | |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 208 | bool CompilationUnit::Materialize(size_t thread_count) { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 209 | MutexLock GUARD(cunit_lock_); |
| 210 | |
Logan Chien | b5eb00c | 2012-05-18 19:56:46 +0800 | [diff] [blame] | 211 | // Materialize the bitcode to elf_image_ |
| 212 | llvm::raw_string_ostream str_os(elf_image_); |
| 213 | bool success = MaterializeToFile(str_os); |
| 214 | LOG(INFO) << "Compilation Unit: " << elf_idx_ << (success ? " (done)" : " (failed)"); |
TDYa127 | 388a83b | 2012-05-09 18:56:22 -0700 | [diff] [blame] | 215 | |
Logan Chien | b5eb00c | 2012-05-18 19:56:46 +0800 | [diff] [blame] | 216 | // Free the resources |
| 217 | context_.reset(NULL); |
| 218 | irb_.reset(NULL); |
| 219 | module_ = NULL; |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 220 | runtime_support_.reset(NULL); |
| 221 | compiled_methods_map_.reset(NULL); |
TDYa127 | 388a83b | 2012-05-09 18:56:22 -0700 | [diff] [blame] | 222 | |
Logan Chien | b5eb00c | 2012-05-18 19:56:46 +0800 | [diff] [blame] | 223 | return success; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 227 | void CompilationUnit::RegisterCompiledMethod(const llvm::Function* func, |
| 228 | CompiledMethod* compiled_method) { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 229 | MutexLock GUARD(cunit_lock_); |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 230 | compiled_methods_map_->Put(func, compiled_method); |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | void CompilationUnit::UpdateFrameSizeInBytes(const llvm::Function* func, |
| 235 | size_t frame_size_in_bytes) { |
Logan Chien | 8ba2fc5 | 2012-04-23 09:10:46 +0800 | [diff] [blame] | 236 | MutexLock GUARD(cunit_lock_); |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 237 | SafeMap<const llvm::Function*, CompiledMethod*>::iterator iter = |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 238 | compiled_methods_map_->find(func); |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 239 | |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 240 | if (iter != compiled_methods_map_->end()) { |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 241 | CompiledMethod* compiled_method = iter->second; |
| 242 | compiled_method->SetFrameSizeInBytes(frame_size_in_bytes); |
| 243 | |
| 244 | if (frame_size_in_bytes > 1728u) { |
| 245 | LOG(WARNING) << "Huge frame size: " << frame_size_in_bytes |
| 246 | << " elf_idx=" << compiled_method->GetElfIndex() |
| 247 | << " elf_func_idx=" << compiled_method->GetElfFuncIndex(); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Logan Chien | b1bab1c | 2012-05-11 11:05:45 +0800 | [diff] [blame] | 252 | bool CompilationUnit::MaterializeToFile(llvm::raw_ostream& out_stream) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 253 | // Lookup the LLVM target |
| 254 | char const* target_triple = NULL; |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 255 | char const* target_cpu = ""; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 256 | char const* target_attr = NULL; |
| 257 | |
Logan Chien | b1bab1c | 2012-05-11 11:05:45 +0800 | [diff] [blame] | 258 | switch (insn_set_) { |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 259 | case kThumb2: |
| 260 | target_triple = "thumb-none-linux-gnueabi"; |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 261 | target_cpu = "cortex-a9"; |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 262 | target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 263 | break; |
| 264 | |
| 265 | case kArm: |
| 266 | target_triple = "armv7-none-linux-gnueabi"; |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 267 | // TODO: Fix for Nexus S. |
| 268 | target_cpu = "cortex-a9"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 269 | // TODO: Fix for Xoom. |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 270 | target_attr = "+v7,+neon,+neonfp,+vfp3,+db"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case kX86: |
| 274 | target_triple = "i386-pc-linux-gnu"; |
| 275 | target_attr = ""; |
| 276 | break; |
| 277 | |
| 278 | case kMips: |
| 279 | target_triple = "mipsel-unknown-linux"; |
| 280 | target_attr = "mips32r2"; |
| 281 | break; |
| 282 | |
| 283 | default: |
Logan Chien | b1bab1c | 2012-05-11 11:05:45 +0800 | [diff] [blame] | 284 | LOG(FATAL) << "Unknown instruction set: " << insn_set_; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | std::string errmsg; |
| 288 | llvm::Target const* target = |
| 289 | llvm::TargetRegistry::lookupTarget(target_triple, errmsg); |
| 290 | |
| 291 | CHECK(target != NULL) << errmsg; |
| 292 | |
| 293 | // Target options |
| 294 | llvm::TargetOptions target_options; |
| 295 | target_options.FloatABIType = llvm::FloatABI::Soft; |
| 296 | target_options.NoFramePointerElim = true; |
| 297 | target_options.NoFramePointerElimNonLeaf = true; |
| 298 | target_options.UseSoftFloat = false; |
TDYa127 | 3978da5 | 2012-05-19 07:45:39 -0700 | [diff] [blame] | 299 | target_options.EnableFastISel = false; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 300 | |
| 301 | // Create the llvm::TargetMachine |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 302 | llvm::OwningPtr<llvm::TargetMachine> target_machine( |
Shih-wei Liao | 53519bf | 2012-06-17 03:45:00 -0700 | [diff] [blame] | 303 | target->createTargetMachine(target_triple, target_cpu, target_attr, target_options, |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 304 | llvm::Reloc::Static, llvm::CodeModel::Small, |
Shih-wei Liao | dac5eb2 | 2012-06-03 14:06:04 -0700 | [diff] [blame] | 305 | llvm::CodeGenOpt::Aggressive)); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 306 | |
Logan Chien | b6bed0b | 2012-05-04 15:03:56 +0800 | [diff] [blame] | 307 | CHECK(target_machine.get() != NULL) << "Failed to create target machine"; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 308 | |
| 309 | // Add target data |
| 310 | llvm::TargetData const* target_data = target_machine->getTargetData(); |
| 311 | |
| 312 | // PassManager for code generation passes |
| 313 | llvm::PassManager pm; |
| 314 | pm.add(new llvm::TargetData(*target_data)); |
| 315 | |
| 316 | // FunctionPassManager for optimization pass |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 317 | llvm::FunctionPassManager fpm(module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 318 | fpm.add(new llvm::TargetData(*target_data)); |
| 319 | |
TDYa127 | f15b0ab | 2012-05-11 21:01:36 -0700 | [diff] [blame] | 320 | if (bitcode_filename_.empty()) { |
| 321 | // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the |
| 322 | // regular FunctionPass. |
| 323 | fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get())); |
| 324 | } else { |
| 325 | // Run AddSuspendCheckToLoopLatchPass before we write the bitcode to file. |
| 326 | llvm::FunctionPassManager fpm2(module_); |
| 327 | fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get())); |
| 328 | fpm2.doInitialization(); |
| 329 | for (llvm::Module::iterator F = module_->begin(), E = module_->end(); |
| 330 | F != E; ++F) { |
| 331 | fpm2.run(*F); |
| 332 | } |
| 333 | fpm2.doFinalization(); |
| 334 | |
| 335 | |
| 336 | // Write bitcode to file |
| 337 | std::string errmsg; |
| 338 | |
| 339 | llvm::OwningPtr<llvm::tool_output_file> out_file( |
| 340 | new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg, |
| 341 | llvm::raw_fd_ostream::F_Binary)); |
| 342 | |
| 343 | |
| 344 | if (!errmsg.empty()) { |
| 345 | LOG(ERROR) << "Failed to create bitcode output file: " << errmsg; |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | llvm::WriteBitcodeToFile(module_, out_file->os()); |
| 350 | out_file->keep(); |
| 351 | } |
| 352 | |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 353 | // Add optimization pass |
| 354 | llvm::PassManagerBuilder pm_builder; |
Shih-wei Liao | e0e4024 | 2012-05-08 01:04:03 -0700 | [diff] [blame] | 355 | //pm_builder.Inliner = llvm::createFunctionInliningPass(); |
Shih-wei Liao | ba67d7d | 2012-06-23 18:48:04 -0700 | [diff] [blame^] | 356 | pm_builder.Inliner = llvm::createAlwaysInlinerPass(); |
Shih-wei Liao | 415576b | 2012-04-23 15:28:53 -0700 | [diff] [blame] | 357 | //pm_builder.Inliner = llvm::createPartialInliningPass(); |
| 358 | pm_builder.OptLevel = 3; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 359 | pm_builder.DisableSimplifyLibCalls = 1; |
TDYa127 | e4c2ccc | 2012-05-13 21:10:36 -0700 | [diff] [blame] | 360 | pm_builder.DisableUnitAtATime = 1; |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 361 | pm_builder.populateFunctionPassManager(fpm); |
TDYa127 | ce9c317 | 2012-05-15 06:09:27 -0700 | [diff] [blame] | 362 | pm_builder.populateModulePassManager(pm); |
| 363 | pm.add(llvm::createStripDeadPrototypesPass()); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 364 | |
| 365 | // Add passes to emit ELF image |
| 366 | { |
Logan Chien | 08e1ba3 | 2012-05-08 15:08:51 +0800 | [diff] [blame] | 367 | llvm::formatted_raw_ostream formatted_os(out_stream, false); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 368 | |
| 369 | // Ask the target to add backend passes as necessary. |
| 370 | if (target_machine->addPassesToEmitFile(pm, |
| 371 | formatted_os, |
| 372 | llvm::TargetMachine::CGFT_ObjectFile, |
| 373 | true)) { |
| 374 | LOG(FATAL) << "Unable to generate ELF for this target"; |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // FIXME: Unable to run the UpdateFrameSizePass pass since it tries to |
| 379 | // update the value reside in the different address space. |
| 380 | // Add pass to update the frame_size_in_bytes_ |
| 381 | //pm.add(new ::UpdateFrameSizePass(this)); |
| 382 | |
| 383 | // Run the per-function optimization |
| 384 | fpm.doInitialization(); |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 385 | for (llvm::Module::iterator F = module_->begin(), E = module_->end(); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 386 | F != E; ++F) { |
| 387 | fpm.run(*F); |
| 388 | } |
| 389 | fpm.doFinalization(); |
| 390 | |
| 391 | // Run the code generation passes |
Logan Chien | 799ef4f | 2012-04-23 00:17:47 +0800 | [diff] [blame] | 392 | pm.run(*module_); |
Shih-wei Liao | d7726e4 | 2012-04-20 15:23:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 397 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 398 | } // namespace compiler_llvm |
| 399 | } // namespace art |