buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 17 | #include <llvm/Support/Threading.h> |
| 18 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 19 | #include "compiler/driver/compiler_driver.h" |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 20 | #include "compiler_internals.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 21 | #include "dataflow_iterator.h" |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 22 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 23 | #include "compiler/llvm/llvm_compilation_unit.h" |
| 24 | #endif |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 25 | #include "leb128.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
buzbee | 395116c | 2013-02-27 14:30:25 -0800 | [diff] [blame] | 28 | #include "quick/codegen_util.h" |
| 29 | #include "portable/mir_to_gbc.h" |
| 30 | #include "quick/mir_to_lir.h" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 31 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 32 | namespace { |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 33 | #if !defined(ART_USE_PORTABLE_COMPILER) |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 34 | pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT; |
Shih-wei Liao | 215a926 | 2012-10-12 10:29:46 -0700 | [diff] [blame] | 35 | #endif |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 36 | void InitializeLLVMForQuick() { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 37 | ::llvm::llvm_start_multithreaded(); |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 41 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 42 | namespace llvm { |
| 43 | ::llvm::Module* makeLLVMModuleContents(::llvm::Module* module); |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 44 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 45 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 46 | LLVMInfo::LLVMInfo() { |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 47 | #if !defined(ART_USE_PORTABLE_COMPILER) |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 48 | pthread_once(&llvm_multi_init, InitializeLLVMForQuick); |
| 49 | #endif |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 50 | // Create context, module, intrinsic helper & ir builder |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 51 | llvm_context_.reset(new ::llvm::LLVMContext()); |
| 52 | llvm_module_ = new ::llvm::Module("art", *llvm_context_); |
| 53 | ::llvm::StructType::create(*llvm_context_, "JavaObject"); |
| 54 | art::llvm::makeLLVMModuleContents(llvm_module_); |
| 55 | intrinsic_helper_.reset( new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_)); |
| 56 | ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_)); |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 57 | } |
| 58 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 59 | LLVMInfo::~LLVMInfo() { |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 62 | extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) { |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 63 | CHECK(compiler.GetCompilerContext() == NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 64 | LLVMInfo* llvm_info = new LLVMInfo(); |
| 65 | compiler.SetCompilerContext(llvm_info); |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 68 | extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) { |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 69 | delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext()); |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 70 | compiler.SetCompilerContext(NULL); |
| 71 | } |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 72 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 73 | /* Default optimizer/debug setting for the compiler. */ |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 74 | static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations |
buzbee | 4ef3e45 | 2012-12-14 13:35:28 -0800 | [diff] [blame] | 75 | (1 << kLoadStoreElimination) | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 76 | //(1 << kLoadHoisting) | |
| 77 | //(1 << kSuppressLoads) | |
| 78 | //(1 << kNullCheckElimination) | |
| 79 | //(1 << kPromoteRegs) | |
| 80 | //(1 << kTrackLiveTemps) | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 81 | //(1 << kSafeOptimizations) | |
| 82 | //(1 << kBBOpt) | |
| 83 | //(1 << kMatch) | |
| 84 | //(1 << kPromoteCompilerTemps) | |
| 85 | 0; |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 86 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 87 | static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 88 | //(1 << kDebugDisplayMissingTargets) | |
| 89 | //(1 << kDebugVerbose) | |
| 90 | //(1 << kDebugDumpCFG) | |
| 91 | //(1 << kDebugSlowFieldPath) | |
| 92 | //(1 << kDebugSlowInvokePath) | |
| 93 | //(1 << kDebugSlowStringPath) | |
| 94 | //(1 << kDebugSlowestFieldPath) | |
| 95 | //(1 << kDebugSlowestStringPath) | |
| 96 | //(1 << kDebugExerciseResolveMethod) | |
| 97 | //(1 << kDebugVerifyDataflow) | |
| 98 | //(1 << kDebugShowMemoryUsage) | |
| 99 | //(1 << kDebugShowNops) | |
| 100 | //(1 << kDebugCountOpcodes) | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 101 | //(1 << kDebugDumpCheckStats) | |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 102 | //(1 << kDebugDumpBitcodeFile) | |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 103 | //(1 << kDebugVerifyBitcode) | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 104 | 0; |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 105 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 106 | static CompiledMethod* CompileMethod(CompilerDriver& compiler, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 107 | const CompilerBackend compiler_backend, |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 108 | const DexFile::CodeItem* code_item, |
| 109 | uint32_t access_flags, InvokeType invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 110 | uint32_t class_def_idx, uint32_t method_idx, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 111 | jobject class_loader, const DexFile& dex_file |
| 112 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 113 | , llvm::LlvmCompilationUnit* llvm_compilation_unit |
| 114 | #endif |
| 115 | ) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 116 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "..."; |
Brian Carlstrom | 94496d3 | 2011-08-22 09:22:47 -0700 | [diff] [blame] | 118 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 119 | // FIXME: now we detect this in MIRGraph. |
| 120 | SpecialCaseHandler special_case = kNoHandler; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 121 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 122 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 123 | UniquePtr<CompilationUnit> cu(new CompilationUnit); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 124 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 125 | if (!HeapInit(cu.get())) { |
| 126 | LOG(FATAL) << "Failed to initialize compiler heap"; |
| 127 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 128 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 129 | cu->compiler_driver = &compiler; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 130 | cu->class_linker = class_linker; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 131 | cu->instruction_set = compiler.GetInstructionSet(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 132 | DCHECK((cu->instruction_set == kThumb2) || |
| 133 | (cu->instruction_set == kX86) || |
| 134 | (cu->instruction_set == kMips)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 135 | |
| 136 | cu->gen_bitcode = (compiler_backend == kPortable); |
| 137 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 138 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 139 | cu->llvm_compilation_unit = llvm_compilation_unit; |
| 140 | cu->llvm_info = llvm_compilation_unit->GetQuickContext(); |
| 141 | cu->symbol = llvm_compilation_unit->GetDexCompilationUnit()->GetSymbol(); |
| 142 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 143 | /* Adjust this value accordingly once inlining is performed */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 144 | cu->num_dalvik_registers = code_item->registers_size_; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 145 | // TODO: set this from command line |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 146 | cu->compiler_flip_match = false; |
| 147 | bool use_match = !cu->compiler_method_match.empty(); |
| 148 | bool match = use_match && (cu->compiler_flip_match ^ |
| 149 | (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) != |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 150 | std::string::npos)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 151 | if (!use_match || match) { |
| 152 | cu->disable_opt = kCompilerOptimizerDisableFlags; |
| 153 | cu->enable_debug = kCompilerDebugFlags; |
| 154 | cu->verbose = VLOG_IS_ON(compiler) || |
| 155 | (cu->enable_debug & (1 << kDebugVerbose)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 156 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 157 | |
| 158 | // If debug build, always verify bitcode. |
| 159 | if (kIsDebugBuild && cu->gen_bitcode) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 160 | cu->enable_debug |= (1 << kDebugVerifyBitcode); |
buzbee | 6969d50 | 2012-06-15 16:40:31 -0700 | [diff] [blame] | 161 | } |
buzbee | 9281f00 | 2012-10-24 12:17:24 -0700 | [diff] [blame] | 162 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 163 | if (cu->instruction_set == kMips) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 164 | // Disable some optimizations for mips for now |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 165 | cu->disable_opt |= ( |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 166 | (1 << kLoadStoreElimination) | |
| 167 | (1 << kLoadHoisting) | |
| 168 | (1 << kSuppressLoads) | |
| 169 | (1 << kNullCheckElimination) | |
| 170 | (1 << kPromoteRegs) | |
| 171 | (1 << kTrackLiveTemps) | |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 172 | (1 << kSafeOptimizations) | |
| 173 | (1 << kBBOpt) | |
| 174 | (1 << kMatch) | |
| 175 | (1 << kPromoteCompilerTemps)); |
| 176 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 177 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 178 | /* Assume leaf */ |
| 179 | cu->attributes = METHOD_IS_LEAF; |
| 180 | |
| 181 | cu->mir_graph.reset(new MIRGraph(cu.get())); |
| 182 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 183 | /* Gathering opcode stats? */ |
| 184 | if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 185 | cu->mir_graph->EnableOpcodeCounting(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 188 | /* Build the raw MIR graph */ |
| 189 | cu->mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, |
| 190 | class_loader, dex_file); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 191 | |
buzbee | 0967a25 | 2012-09-14 10:43:54 -0700 | [diff] [blame] | 192 | /* Do a code layout pass */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 193 | cu->mir_graph->CodeLayout(); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 194 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 195 | if (cu->enable_debug & (1 << kDebugVerifyDataflow)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 196 | cu->mir_graph->VerifyDataflow(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 197 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 198 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 199 | /* Perform SSA transformation for the whole method */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 200 | cu->mir_graph->SSATransformation(); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 201 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 202 | /* Do constant propagation */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 203 | cu->mir_graph->PropagateConstants(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 204 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 205 | /* Count uses */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 206 | cu->mir_graph->MethodUseCount(); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 207 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 208 | /* Perform null check elimination */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 209 | cu->mir_graph->NullCheckElimination(); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 210 | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 211 | /* Combine basic blocks where possible */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 212 | cu->mir_graph->BasicBlockCombine(); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 213 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 214 | /* Do some basic block optimizations */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 215 | cu->mir_graph->BasicBlockOptimization(); |
buzbee | d850621 | 2012-12-20 14:15:05 -0800 | [diff] [blame] | 216 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 217 | if (cu->enable_debug & (1 << kDebugDumpCheckStats)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 218 | cu->mir_graph->DumpCheckStats(); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 219 | } |
| 220 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 221 | /* Set up regLocation[] array to describe values - one for each ssa_name. */ |
| 222 | cu->mir_graph->BuildRegLocations(); |
buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 223 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 224 | #if defined(ART_USE_PORTABLE_COMPILER) |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 225 | /* Go the LLVM path? */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 226 | if (cu->gen_bitcode) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 227 | // MIR->Bitcode |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 228 | MethodMIR2Bitcode(cu.get()); |
| 229 | if (compiler_backend == kPortable) { |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 230 | // all done |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 231 | ArenaReset(cu.get()); |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 234 | } else |
| 235 | #endif |
| 236 | { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 237 | switch (compiler.GetInstructionSet()) { |
| 238 | case kThumb2: |
| 239 | InitArmCodegen(cu.get()); break; |
| 240 | case kMips: |
| 241 | InitMipsCodegen(cu.get()); break; |
| 242 | case kX86: |
| 243 | InitX86Codegen(cu.get()); break; |
| 244 | default: |
| 245 | LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet(); |
| 246 | } |
| 247 | |
| 248 | // ** MOVE ALL OF THIS TO Codegen.materialize() |
| 249 | |
| 250 | /* Initialize the switch_tables list */ // TO CODEGEN |
| 251 | CompilerInitGrowableList(cu.get(), &cu->switch_tables, 4, |
| 252 | kListSwitchTables); |
| 253 | |
| 254 | /* Intialize the fill_array_data list */ // TO CODEGEN |
| 255 | CompilerInitGrowableList(cu.get(), &cu->fill_array_data, 4, |
| 256 | kListFillArrayData); |
| 257 | |
| 258 | /* Intialize the throw_launchpads list, estimate size based on insns_size */ // TO CODEGEN |
| 259 | CompilerInitGrowableList(cu.get(), &cu->throw_launchpads, code_item->insns_size_in_code_units_, |
| 260 | kListThrowLaunchPads); |
| 261 | |
| 262 | /* Intialize the instrinsic_launchpads list */ // TO_CODEGEN |
| 263 | CompilerInitGrowableList(cu.get(), &cu->intrinsic_launchpads, 4, |
| 264 | kListMisc); |
| 265 | |
| 266 | |
| 267 | /* Intialize the suspend_launchpads list */ // TO_CODEGEN |
| 268 | CompilerInitGrowableList(cu.get(), &cu->suspend_launchpads, 2048, |
| 269 | kListSuspendLaunchPads); |
| 270 | |
| 271 | // TODO: Push these to codegen |
| 272 | cu.get()->cg->CompilerInitializeRegAlloc(cu.get()); // Needs to happen after SSA naming |
| 273 | |
| 274 | /* Allocate Registers using simple local allocation scheme */ |
| 275 | cu.get()->cg->SimpleRegAlloc(cu.get()); |
| 276 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 277 | if (special_case != kNoHandler) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 278 | /* |
| 279 | * Custom codegen for special cases. If for any reason the |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 280 | * special codegen doesn't succeed, cu->first_lir_insn will |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 281 | * set to NULL; |
| 282 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 283 | SpecialMIR2LIR(cu.get(), special_case); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 284 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 285 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 286 | /* Convert MIR to LIR, etc. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 287 | if (cu->first_lir_insn == NULL) { |
| 288 | MethodMIR2LIR(cu.get()); |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 289 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 290 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 291 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 292 | /* Method is not empty */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 293 | if (cu->first_lir_insn) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 294 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 295 | // mark the targets of switch statement case labels |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 296 | ProcessSwitchTables(cu.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 297 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 298 | /* Convert LIR into machine code. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 299 | AssembleLIR(cu.get()); |
buzbee | 99ba964 | 2012-01-25 14:23:14 -0800 | [diff] [blame] | 300 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 301 | if (cu->verbose) { |
| 302 | CodegenDump(cu.get()); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 303 | } |
| 304 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 305 | } |
| 306 | |
| 307 | if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) { |
| 308 | cu->mir_graph->ShowOpcodeStats(); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 309 | } |
buzbee | a7c1268 | 2012-03-19 13:13:53 -0700 | [diff] [blame] | 310 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 311 | // Combine vmap tables - core regs, then fp regs - into vmap_table |
| 312 | std::vector<uint16_t> vmap_table; |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 313 | // Core regs may have been inserted out of order - sort first |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 314 | std::sort(cu->core_vmap_table.begin(), cu->core_vmap_table.end()); |
| 315 | for (size_t i = 0 ; i < cu->core_vmap_table.size(); i++) { |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 316 | // Copy, stripping out the phys register sort key |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 317 | vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & cu->core_vmap_table[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 318 | } |
| 319 | // If we have a frame, push a marker to take place of lr |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 320 | if (cu->frame_size > 0) { |
| 321 | vmap_table.push_back(INVALID_VREG); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 322 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 323 | DCHECK_EQ(__builtin_popcount(cu->core_spill_mask), 0); |
| 324 | DCHECK_EQ(__builtin_popcount(cu->fp_spill_mask), 0); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 325 | } |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 326 | // Combine vmap tables - core regs, then fp regs. fp regs already sorted |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 327 | for (uint32_t i = 0; i < cu->fp_vmap_table.size(); i++) { |
| 328 | vmap_table.push_back(cu->fp_vmap_table[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 329 | } |
| 330 | CompiledMethod* result = |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 331 | new CompiledMethod(cu->instruction_set, cu->code_buffer, |
| 332 | cu->frame_size, cu->core_spill_mask, cu->fp_spill_mask, |
| 333 | cu->combined_mapping_table, vmap_table, cu->native_gc_map); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 334 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 335 | VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file) |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 336 | << " (" << (cu->code_buffer.size() * sizeof(cu->code_buffer[0])) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 337 | << " bytes)"; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 338 | |
| 339 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) { |
| 341 | DumpMemStats(cu.get()); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 342 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 343 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 344 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 345 | ArenaReset(cu.get()); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 346 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 347 | return result; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 350 | CompiledMethod* CompileOneMethod(CompilerDriver& compiler, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 351 | const CompilerBackend backend, |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 352 | const DexFile::CodeItem* code_item, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 353 | uint32_t access_flags, |
| 354 | InvokeType invoke_type, |
| 355 | uint32_t class_def_idx, |
| 356 | uint32_t method_idx, |
| 357 | jobject class_loader, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 358 | const DexFile& dex_file, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 359 | llvm::LlvmCompilationUnit* llvm_compilation_unit) |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 360 | { |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 361 | return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 362 | method_idx, class_loader, dex_file |
| 363 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 364 | , llvm_compilation_unit |
| 365 | #endif |
| 366 | ); |
buzbee | abc4c6b | 2012-08-23 08:17:15 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 369 | } // namespace art |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 370 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 371 | extern "C" art::CompiledMethod* |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 372 | ArtQuickCompileMethod(art::CompilerDriver& compiler, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 373 | const art::DexFile::CodeItem* code_item, |
| 374 | uint32_t access_flags, art::InvokeType invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 375 | uint32_t class_def_idx, uint32_t method_idx, jobject class_loader, |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 376 | const art::DexFile& dex_file) |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 377 | { |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 378 | // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default |
| 379 | art::CompilerBackend backend = compiler.GetCompilerBackend(); |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 380 | return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type, |
Ian Rogers | fffdb02 | 2013-01-04 15:14:08 -0800 | [diff] [blame] | 381 | class_def_idx, method_idx, class_loader, dex_file, |
| 382 | NULL /* use thread llvm_info */); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 383 | } |