Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -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 | |
| 17 | #include "base/logging.h" |
| 18 | #include "base/mutex.h" |
| 19 | #include "dex_file-inl.h" |
| 20 | #include "dex_instruction-inl.h" |
| 21 | #include "driver/compiler_driver.h" |
| 22 | #include "driver/dex_compilation_unit.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field-inl.h" |
| 24 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| 26 | #include "mirror/dex_cache.h" |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 27 | #include "thread-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace optimizer { |
| 31 | |
| 32 | // Controls quickening activation. |
| 33 | const bool kEnableQuickening = true; |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 34 | // Control check-cast elision. |
| 35 | const bool kEnableCheckCastEllision = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | |
| 37 | class DexCompiler { |
| 38 | public: |
| 39 | DexCompiler(art::CompilerDriver& compiler, |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 40 | const DexCompilationUnit& unit, |
| 41 | DexToDexCompilationLevel dex_to_dex_compilation_level) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | : driver_(compiler), |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 43 | unit_(unit), |
| 44 | dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 46 | ~DexCompiler() {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 47 | |
| 48 | void Compile(); |
| 49 | |
| 50 | private: |
| 51 | const DexFile& GetDexFile() const { |
| 52 | return *unit_.GetDexFile(); |
| 53 | } |
| 54 | |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 55 | bool PerformOptimizations() const { |
| 56 | return dex_to_dex_compilation_level_ >= kOptimize; |
| 57 | } |
| 58 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where |
| 60 | // a barrier is required. |
| 61 | void CompileReturnVoid(Instruction* inst, uint32_t dex_pc); |
| 62 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 63 | // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In |
| 64 | // this case, returns the second NOP instruction pointer. Otherwise, returns |
| 65 | // the given "inst". |
| 66 | Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc); |
| 67 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | // Compiles a field access into a quick field access. |
| 69 | // The field index is replaced by an offset within an Object where we can read |
| 70 | // from / write to this field. Therefore, this does not involve any resolution |
| 71 | // at runtime. |
| 72 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 73 | // field offset can be encoded with 16 bits too. |
| 74 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 75 | Instruction::Code new_opcode, bool is_put); |
| 76 | |
| 77 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 78 | // The method index is replaced by the vtable index where the corresponding |
| 79 | // AbstractMethod can be found. Therefore, this does not involve any resolution |
| 80 | // at runtime. |
| 81 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 82 | // vtable index can be encoded with 16 bits too. |
| 83 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 84 | Instruction::Code new_opcode, bool is_range); |
| 85 | |
| 86 | CompilerDriver& driver_; |
| 87 | const DexCompilationUnit& unit_; |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 88 | const DexToDexCompilationLevel dex_to_dex_compilation_level_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(DexCompiler); |
| 91 | }; |
| 92 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 93 | void DexCompiler::Compile() { |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 94 | DCHECK_GE(dex_to_dex_compilation_level_, kRequired); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 95 | const DexFile::CodeItem* code_item = unit_.GetCodeItem(); |
| 96 | const uint16_t* insns = code_item->insns_; |
| 97 | const uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 98 | Instruction* inst = const_cast<Instruction*>(Instruction::At(insns)); |
| 99 | |
| 100 | for (uint32_t dex_pc = 0; dex_pc < insns_size; |
| 101 | inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) { |
| 102 | switch (inst->Opcode()) { |
| 103 | case Instruction::RETURN_VOID: |
| 104 | CompileReturnVoid(inst, dex_pc); |
| 105 | break; |
| 106 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 107 | case Instruction::CHECK_CAST: |
| 108 | inst = CompileCheckCast(inst, dex_pc); |
| 109 | break; |
| 110 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 111 | case Instruction::IGET: |
| 112 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false); |
| 113 | break; |
| 114 | |
| 115 | case Instruction::IGET_WIDE: |
| 116 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false); |
| 117 | break; |
| 118 | |
| 119 | case Instruction::IGET_OBJECT: |
| 120 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false); |
| 121 | break; |
| 122 | |
| 123 | case Instruction::IPUT: |
| 124 | case Instruction::IPUT_BOOLEAN: |
| 125 | case Instruction::IPUT_BYTE: |
| 126 | case Instruction::IPUT_CHAR: |
| 127 | case Instruction::IPUT_SHORT: |
| 128 | // These opcodes have the same implementation in interpreter so group |
| 129 | // them under IPUT_QUICK. |
| 130 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true); |
| 131 | break; |
| 132 | |
| 133 | case Instruction::IPUT_WIDE: |
| 134 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true); |
| 135 | break; |
| 136 | |
| 137 | case Instruction::IPUT_OBJECT: |
| 138 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true); |
| 139 | break; |
| 140 | |
| 141 | case Instruction::INVOKE_VIRTUAL: |
| 142 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false); |
| 143 | break; |
| 144 | |
| 145 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 146 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true); |
| 147 | break; |
| 148 | |
| 149 | default: |
| 150 | // Nothing to do. |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) { |
| 157 | DCHECK(inst->Opcode() == Instruction::RETURN_VOID); |
Ian Rogers | 9fc16eb | 2013-07-31 14:49:16 -0700 | [diff] [blame] | 158 | // Are we compiling a non-clinit constructor? |
| 159 | if (!unit_.IsConstructor() || unit_.IsStatic()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | // Do we need a constructor barrier ? |
| 163 | if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(), |
| 164 | unit_.GetClassDefIndex())) { |
| 165 | return; |
| 166 | } |
| 167 | // Replace RETURN_VOID by RETURN_VOID_BARRIER. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 168 | VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode()) |
| 169 | << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER) |
| 170 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 171 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 172 | inst->SetOpcode(Instruction::RETURN_VOID_BARRIER); |
| 173 | } |
| 174 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 175 | Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) { |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 176 | if (!kEnableCheckCastEllision || !PerformOptimizations()) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 177 | return inst; |
| 178 | } |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 179 | if (!driver_.IsSafeCast(&unit_, dex_pc)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 180 | return inst; |
| 181 | } |
| 182 | // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code |
| 183 | // units and a "nop" instruction size is 1 code unit, we need to replace it by |
| 184 | // 2 consecutive NOP instructions. |
| 185 | // Because the caller loops over instructions by calling Instruction::Next onto |
| 186 | // the current instruction, we need to return the 2nd NOP instruction. Indeed, |
| 187 | // its next instruction is the former check-cast's next instruction. |
| 188 | VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode()) |
| 189 | << " by replacing it with 2 NOPs at dex pc " |
| 190 | << StringPrintf("0x%x", dex_pc) << " in method " |
| 191 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
| 192 | // We are modifying 4 consecutive bytes. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 193 | inst->SetOpcode(Instruction::NOP); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 194 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 195 | // Get to next instruction which is the second half of check-cast and replace |
| 196 | // it by a NOP. |
| 197 | inst = const_cast<Instruction*>(inst->Next()); |
| 198 | inst->SetOpcode(Instruction::NOP); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 199 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 200 | return inst; |
| 201 | } |
| 202 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 203 | void DexCompiler::CompileInstanceFieldAccess(Instruction* inst, |
| 204 | uint32_t dex_pc, |
| 205 | Instruction::Code new_opcode, |
| 206 | bool is_put) { |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 207 | if (!kEnableQuickening || !PerformOptimizations()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | uint32_t field_idx = inst->VRegC_22c(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 211 | MemberOffset field_offset(0u); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 212 | bool is_volatile; |
Ian Rogers | 9b297bf | 2013-09-06 11:11:25 -0700 | [diff] [blame] | 213 | bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put, |
| 214 | &field_offset, &is_volatile); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 215 | if (fast_path && !is_volatile && IsUint(16, field_offset.Int32Value())) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 216 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 217 | << " to " << Instruction::Name(new_opcode) |
| 218 | << " by replacing field index " << field_idx |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 219 | << " by field offset " << field_offset.Int32Value() |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 220 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 221 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 222 | // We are modifying 4 consecutive bytes. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 223 | inst->SetOpcode(new_opcode); |
| 224 | // Replace field index by field offset. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 225 | inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | void DexCompiler::CompileInvokeVirtual(Instruction* inst, |
| 230 | uint32_t dex_pc, |
| 231 | Instruction::Code new_opcode, |
| 232 | bool is_range) { |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 233 | if (!kEnableQuickening || !PerformOptimizations()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 237 | MethodReference target_method(&GetDexFile(), method_idx); |
| 238 | InvokeType invoke_type = kVirtual; |
| 239 | InvokeType original_invoke_type = invoke_type; |
| 240 | int vtable_idx; |
| 241 | uintptr_t direct_code; |
| 242 | uintptr_t direct_method; |
Sebastien Hertz | 1e54d68 | 2013-09-06 14:52:10 +0200 | [diff] [blame] | 243 | // TODO: support devirtualization. |
| 244 | const bool kEnableDevirtualization = false; |
Ian Rogers | 65ec92c | 2013-09-06 10:49:58 -0700 | [diff] [blame] | 245 | bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc, |
| 246 | false, kEnableDevirtualization, |
| 247 | &invoke_type, |
| 248 | &target_method, &vtable_idx, |
| 249 | &direct_code, &direct_method); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 250 | if (fast_path && original_invoke_type == invoke_type) { |
| 251 | if (vtable_idx >= 0 && IsUint(16, vtable_idx)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 252 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 253 | << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")" |
| 254 | << " to " << Instruction::Name(new_opcode) |
| 255 | << " by replacing method index " << method_idx |
| 256 | << " by vtable index " << vtable_idx |
| 257 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 258 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 259 | // We are modifying 4 consecutive bytes. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 260 | inst->SetOpcode(new_opcode); |
| 261 | // Replace method index by vtable index. |
| 262 | if (is_range) { |
| 263 | inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx)); |
| 264 | } else { |
| 265 | inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx)); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | } // namespace optimizer |
| 272 | } // namespace art |
| 273 | |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 274 | extern "C" void ArtCompileDEX(art::CompilerDriver& driver, const art::DexFile::CodeItem* code_item, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 275 | uint32_t access_flags, art::InvokeType invoke_type, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 276 | uint16_t class_def_idx, uint32_t method_idx, jobject class_loader, |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 277 | const art::DexFile& dex_file, |
| 278 | art::DexToDexCompilationLevel dex_to_dex_compilation_level) { |
| 279 | if (dex_to_dex_compilation_level != art::kDontDexToDexCompile) { |
| 280 | art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(), |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 281 | dex_file, code_item, class_def_idx, method_idx, access_flags, |
| 282 | driver.GetVerifiedMethod(&dex_file, method_idx)); |
| 283 | art::optimizer::DexCompiler dex_compiler(driver, unit, dex_to_dex_compilation_level); |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 284 | dex_compiler.Compile(); |
| 285 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 286 | } |