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" |
| 23 | #include "mirror/abstract_method-inl.h" |
| 24 | #include "mirror/class-inl.h" |
| 25 | #include "mirror/dex_cache.h" |
| 26 | #include "mirror/field-inl.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace optimizer { |
| 30 | |
| 31 | // Controls quickening activation. |
| 32 | const bool kEnableQuickening = true; |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 33 | // Control check-cast elision. |
| 34 | const bool kEnableCheckCastEllision = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | |
| 36 | class DexCompiler { |
| 37 | public: |
| 38 | DexCompiler(art::CompilerDriver& compiler, |
| 39 | const DexCompilationUnit& unit) |
| 40 | : driver_(compiler), |
| 41 | unit_(unit) {}; |
| 42 | |
| 43 | ~DexCompiler() {}; |
| 44 | |
| 45 | void Compile(); |
| 46 | |
| 47 | private: |
| 48 | const DexFile& GetDexFile() const { |
| 49 | return *unit_.GetDexFile(); |
| 50 | } |
| 51 | |
| 52 | // TODO: since the whole compilation pipeline uses a "const DexFile", we need |
| 53 | // to "unconst" here. The DEX-to-DEX compiler should work on a non-const DexFile. |
| 54 | DexFile& GetModifiableDexFile() { |
| 55 | return *const_cast<DexFile*>(unit_.GetDexFile()); |
| 56 | } |
| 57 | |
| 58 | // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where |
| 59 | // a barrier is required. |
| 60 | void CompileReturnVoid(Instruction* inst, uint32_t dex_pc); |
| 61 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 62 | // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In |
| 63 | // this case, returns the second NOP instruction pointer. Otherwise, returns |
| 64 | // the given "inst". |
| 65 | Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc); |
| 66 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 67 | // Compiles a field access into a quick field access. |
| 68 | // The field index is replaced by an offset within an Object where we can read |
| 69 | // from / write to this field. Therefore, this does not involve any resolution |
| 70 | // at runtime. |
| 71 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 72 | // field offset can be encoded with 16 bits too. |
| 73 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 74 | Instruction::Code new_opcode, bool is_put); |
| 75 | |
| 76 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 77 | // The method index is replaced by the vtable index where the corresponding |
| 78 | // AbstractMethod can be found. Therefore, this does not involve any resolution |
| 79 | // at runtime. |
| 80 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 81 | // vtable index can be encoded with 16 bits too. |
| 82 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 83 | Instruction::Code new_opcode, bool is_range); |
| 84 | |
| 85 | CompilerDriver& driver_; |
| 86 | const DexCompilationUnit& unit_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(DexCompiler); |
| 89 | }; |
| 90 | |
| 91 | // Ensures write access to a part of DEX file. |
| 92 | // |
| 93 | // If a DEX file is read-only, it modifies its protection (mprotect) so it allows |
| 94 | // write access to the part of DEX file defined by an address and a length. |
| 95 | // In this case, it also takes the DexFile::modification_lock to prevent from |
| 96 | // concurrent protection modification from a parallel DEX-to-DEX compilation on |
| 97 | // the same DEX file. |
| 98 | // When the instance is destroyed, it recovers original protection and releases |
| 99 | // the lock. |
| 100 | // TODO: as this scoped class is similar to a MutexLock we should use annotalysis |
| 101 | // to capture the locking behavior. |
| 102 | class ScopedDexWriteAccess { |
| 103 | public: |
| 104 | ScopedDexWriteAccess(DexFile& dex_file, Instruction* inst, |
| 105 | size_t length) |
| 106 | : dex_file_(dex_file), |
| 107 | address_(reinterpret_cast<uint8_t*>(inst)), |
| 108 | length_(length), |
| 109 | is_read_only_(dex_file_.IsReadOnly()) { |
| 110 | if (is_read_only_) { |
| 111 | // We need to enable DEX write access. To avoid concurrent DEX write access |
| 112 | // modification, we take the DexFile::modification_lock before. |
| 113 | dex_file_.GetModificationLock().ExclusiveLock(Thread::Current()); |
| 114 | bool success = dex_file_.EnableWrite(address_, length_); |
| 115 | DCHECK(success) << "Failed to enable DEX write access"; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | ~ScopedDexWriteAccess() { |
| 120 | DCHECK_EQ(is_read_only_, dex_file_.IsReadOnly()); |
| 121 | if (is_read_only_) { |
| 122 | bool success = dex_file_.DisableWrite(address_, length_); |
| 123 | DCHECK(success) << "Failed to disable DEX write access"; |
| 124 | // Now we recovered original read-only protection, we can release the |
| 125 | // DexFile::modification_lock. |
| 126 | dex_file_.GetModificationLock().ExclusiveUnlock(Thread::Current()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | DexFile& dex_file_; |
| 132 | // TODO: make address_ const. |
| 133 | uint8_t* address_; |
| 134 | const size_t length_; |
| 135 | const bool is_read_only_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(ScopedDexWriteAccess); |
| 138 | }; |
| 139 | |
| 140 | void DexCompiler::Compile() { |
| 141 | const DexFile::CodeItem* code_item = unit_.GetCodeItem(); |
| 142 | const uint16_t* insns = code_item->insns_; |
| 143 | const uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 144 | Instruction* inst = const_cast<Instruction*>(Instruction::At(insns)); |
| 145 | |
| 146 | for (uint32_t dex_pc = 0; dex_pc < insns_size; |
| 147 | inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) { |
| 148 | switch (inst->Opcode()) { |
| 149 | case Instruction::RETURN_VOID: |
| 150 | CompileReturnVoid(inst, dex_pc); |
| 151 | break; |
| 152 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 153 | case Instruction::CHECK_CAST: |
| 154 | inst = CompileCheckCast(inst, dex_pc); |
| 155 | break; |
| 156 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 157 | case Instruction::IGET: |
| 158 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false); |
| 159 | break; |
| 160 | |
| 161 | case Instruction::IGET_WIDE: |
| 162 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false); |
| 163 | break; |
| 164 | |
| 165 | case Instruction::IGET_OBJECT: |
| 166 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false); |
| 167 | break; |
| 168 | |
| 169 | case Instruction::IPUT: |
| 170 | case Instruction::IPUT_BOOLEAN: |
| 171 | case Instruction::IPUT_BYTE: |
| 172 | case Instruction::IPUT_CHAR: |
| 173 | case Instruction::IPUT_SHORT: |
| 174 | // These opcodes have the same implementation in interpreter so group |
| 175 | // them under IPUT_QUICK. |
| 176 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true); |
| 177 | break; |
| 178 | |
| 179 | case Instruction::IPUT_WIDE: |
| 180 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true); |
| 181 | break; |
| 182 | |
| 183 | case Instruction::IPUT_OBJECT: |
| 184 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true); |
| 185 | break; |
| 186 | |
| 187 | case Instruction::INVOKE_VIRTUAL: |
| 188 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false); |
| 189 | break; |
| 190 | |
| 191 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 192 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true); |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | // Nothing to do. |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) { |
| 203 | DCHECK(inst->Opcode() == Instruction::RETURN_VOID); |
| 204 | // Are we compiling a constructor ? |
| 205 | if ((unit_.GetAccessFlags() & kAccConstructor) == 0) { |
| 206 | return; |
| 207 | } |
| 208 | // Do we need a constructor barrier ? |
| 209 | if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(), |
| 210 | unit_.GetClassDefIndex())) { |
| 211 | return; |
| 212 | } |
| 213 | // Replace RETURN_VOID by RETURN_VOID_BARRIER. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 214 | VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode()) |
| 215 | << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER) |
| 216 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 217 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 218 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 2u); |
| 219 | inst->SetOpcode(Instruction::RETURN_VOID_BARRIER); |
| 220 | } |
| 221 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 222 | Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) { |
| 223 | if (!kEnableCheckCastEllision) { |
| 224 | return inst; |
| 225 | } |
| 226 | MethodReference referrer(&GetDexFile(), unit_.GetDexMethodIndex()); |
| 227 | if (!driver_.IsSafeCast(referrer, dex_pc)) { |
| 228 | return inst; |
| 229 | } |
| 230 | // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code |
| 231 | // units and a "nop" instruction size is 1 code unit, we need to replace it by |
| 232 | // 2 consecutive NOP instructions. |
| 233 | // Because the caller loops over instructions by calling Instruction::Next onto |
| 234 | // the current instruction, we need to return the 2nd NOP instruction. Indeed, |
| 235 | // its next instruction is the former check-cast's next instruction. |
| 236 | VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode()) |
| 237 | << " by replacing it with 2 NOPs at dex pc " |
| 238 | << StringPrintf("0x%x", dex_pc) << " in method " |
| 239 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
| 240 | // We are modifying 4 consecutive bytes. |
| 241 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 4u); |
| 242 | inst->SetOpcode(Instruction::NOP); |
| 243 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
| 244 | // Get to next instruction which is the second half of check-cast and replace |
| 245 | // it by a NOP. |
| 246 | inst = const_cast<Instruction*>(inst->Next()); |
| 247 | inst->SetOpcode(Instruction::NOP); |
| 248 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
| 249 | return inst; |
| 250 | } |
| 251 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 252 | void DexCompiler::CompileInstanceFieldAccess(Instruction* inst, |
| 253 | uint32_t dex_pc, |
| 254 | Instruction::Code new_opcode, |
| 255 | bool is_put) { |
| 256 | if (!kEnableQuickening) { |
| 257 | return; |
| 258 | } |
| 259 | uint32_t field_idx = inst->VRegC_22c(); |
| 260 | int field_offset; |
| 261 | bool is_volatile; |
| 262 | bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, field_offset, |
| 263 | is_volatile, is_put); |
| 264 | if (fast_path && !is_volatile && IsUint(16, field_offset)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 265 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 266 | << " to " << Instruction::Name(new_opcode) |
| 267 | << " by replacing field index " << field_idx |
| 268 | << " by field offset " << field_offset |
| 269 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 270 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 271 | // We are modifying 4 consecutive bytes. |
| 272 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 4u); |
| 273 | inst->SetOpcode(new_opcode); |
| 274 | // Replace field index by field offset. |
| 275 | inst->SetVRegC_22c(static_cast<uint16_t>(field_offset)); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void DexCompiler::CompileInvokeVirtual(Instruction* inst, |
| 280 | uint32_t dex_pc, |
| 281 | Instruction::Code new_opcode, |
| 282 | bool is_range) { |
| 283 | if (!kEnableQuickening) { |
| 284 | return; |
| 285 | } |
| 286 | uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 287 | MethodReference target_method(&GetDexFile(), method_idx); |
| 288 | InvokeType invoke_type = kVirtual; |
| 289 | InvokeType original_invoke_type = invoke_type; |
| 290 | int vtable_idx; |
| 291 | uintptr_t direct_code; |
| 292 | uintptr_t direct_method; |
| 293 | bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc, invoke_type, |
| 294 | target_method, vtable_idx, |
| 295 | direct_code, direct_method, |
| 296 | false); |
| 297 | // TODO: support devirtualization. |
| 298 | if (fast_path && original_invoke_type == invoke_type) { |
| 299 | if (vtable_idx >= 0 && IsUint(16, vtable_idx)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame^] | 300 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 301 | << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")" |
| 302 | << " to " << Instruction::Name(new_opcode) |
| 303 | << " by replacing method index " << method_idx |
| 304 | << " by vtable index " << vtable_idx |
| 305 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 306 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 307 | // We are modifying 4 consecutive bytes. |
| 308 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 4u); |
| 309 | inst->SetOpcode(new_opcode); |
| 310 | // Replace method index by vtable index. |
| 311 | if (is_range) { |
| 312 | inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx)); |
| 313 | } else { |
| 314 | inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx)); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | } // namespace optimizer |
| 321 | } // namespace art |
| 322 | |
| 323 | extern "C" art::CompiledMethod* |
| 324 | ArtCompileDEX(art::CompilerDriver& compiler, const art::DexFile::CodeItem* code_item, |
| 325 | uint32_t access_flags, art::InvokeType invoke_type, |
| 326 | uint32_t class_def_idx, uint32_t method_idx, jobject class_loader, |
| 327 | const art::DexFile& dex_file) { |
| 328 | art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(), |
| 329 | dex_file, code_item, class_def_idx, method_idx, access_flags); |
| 330 | art::optimizer::DexCompiler dex_compiler(compiler, unit); |
| 331 | dex_compiler.Compile(); |
| 332 | return NULL; |
| 333 | } |