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; |
| 33 | // Controls logging. |
| 34 | const bool kEnableLogging = false; |
| 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 | |
| 62 | // Compiles a field access into a quick field access. |
| 63 | // The field index is replaced by an offset within an Object where we can read |
| 64 | // from / write to this field. Therefore, this does not involve any resolution |
| 65 | // at runtime. |
| 66 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 67 | // field offset can be encoded with 16 bits too. |
| 68 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 69 | Instruction::Code new_opcode, bool is_put); |
| 70 | |
| 71 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 72 | // The method index is replaced by the vtable index where the corresponding |
| 73 | // AbstractMethod can be found. Therefore, this does not involve any resolution |
| 74 | // at runtime. |
| 75 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 76 | // vtable index can be encoded with 16 bits too. |
| 77 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 78 | Instruction::Code new_opcode, bool is_range); |
| 79 | |
| 80 | CompilerDriver& driver_; |
| 81 | const DexCompilationUnit& unit_; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(DexCompiler); |
| 84 | }; |
| 85 | |
| 86 | // Ensures write access to a part of DEX file. |
| 87 | // |
| 88 | // If a DEX file is read-only, it modifies its protection (mprotect) so it allows |
| 89 | // write access to the part of DEX file defined by an address and a length. |
| 90 | // In this case, it also takes the DexFile::modification_lock to prevent from |
| 91 | // concurrent protection modification from a parallel DEX-to-DEX compilation on |
| 92 | // the same DEX file. |
| 93 | // When the instance is destroyed, it recovers original protection and releases |
| 94 | // the lock. |
| 95 | // TODO: as this scoped class is similar to a MutexLock we should use annotalysis |
| 96 | // to capture the locking behavior. |
| 97 | class ScopedDexWriteAccess { |
| 98 | public: |
| 99 | ScopedDexWriteAccess(DexFile& dex_file, Instruction* inst, |
| 100 | size_t length) |
| 101 | : dex_file_(dex_file), |
| 102 | address_(reinterpret_cast<uint8_t*>(inst)), |
| 103 | length_(length), |
| 104 | is_read_only_(dex_file_.IsReadOnly()) { |
| 105 | if (is_read_only_) { |
| 106 | // We need to enable DEX write access. To avoid concurrent DEX write access |
| 107 | // modification, we take the DexFile::modification_lock before. |
| 108 | dex_file_.GetModificationLock().ExclusiveLock(Thread::Current()); |
| 109 | bool success = dex_file_.EnableWrite(address_, length_); |
| 110 | DCHECK(success) << "Failed to enable DEX write access"; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | ~ScopedDexWriteAccess() { |
| 115 | DCHECK_EQ(is_read_only_, dex_file_.IsReadOnly()); |
| 116 | if (is_read_only_) { |
| 117 | bool success = dex_file_.DisableWrite(address_, length_); |
| 118 | DCHECK(success) << "Failed to disable DEX write access"; |
| 119 | // Now we recovered original read-only protection, we can release the |
| 120 | // DexFile::modification_lock. |
| 121 | dex_file_.GetModificationLock().ExclusiveUnlock(Thread::Current()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | DexFile& dex_file_; |
| 127 | // TODO: make address_ const. |
| 128 | uint8_t* address_; |
| 129 | const size_t length_; |
| 130 | const bool is_read_only_; |
| 131 | |
| 132 | DISALLOW_COPY_AND_ASSIGN(ScopedDexWriteAccess); |
| 133 | }; |
| 134 | |
| 135 | void DexCompiler::Compile() { |
| 136 | const DexFile::CodeItem* code_item = unit_.GetCodeItem(); |
| 137 | const uint16_t* insns = code_item->insns_; |
| 138 | const uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 139 | Instruction* inst = const_cast<Instruction*>(Instruction::At(insns)); |
| 140 | |
| 141 | for (uint32_t dex_pc = 0; dex_pc < insns_size; |
| 142 | inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) { |
| 143 | switch (inst->Opcode()) { |
| 144 | case Instruction::RETURN_VOID: |
| 145 | CompileReturnVoid(inst, dex_pc); |
| 146 | break; |
| 147 | |
| 148 | case Instruction::IGET: |
| 149 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false); |
| 150 | break; |
| 151 | |
| 152 | case Instruction::IGET_WIDE: |
| 153 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false); |
| 154 | break; |
| 155 | |
| 156 | case Instruction::IGET_OBJECT: |
| 157 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false); |
| 158 | break; |
| 159 | |
| 160 | case Instruction::IPUT: |
| 161 | case Instruction::IPUT_BOOLEAN: |
| 162 | case Instruction::IPUT_BYTE: |
| 163 | case Instruction::IPUT_CHAR: |
| 164 | case Instruction::IPUT_SHORT: |
| 165 | // These opcodes have the same implementation in interpreter so group |
| 166 | // them under IPUT_QUICK. |
| 167 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true); |
| 168 | break; |
| 169 | |
| 170 | case Instruction::IPUT_WIDE: |
| 171 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true); |
| 172 | break; |
| 173 | |
| 174 | case Instruction::IPUT_OBJECT: |
| 175 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true); |
| 176 | break; |
| 177 | |
| 178 | case Instruction::INVOKE_VIRTUAL: |
| 179 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false); |
| 180 | break; |
| 181 | |
| 182 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 183 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true); |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | // Nothing to do. |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) { |
| 194 | DCHECK(inst->Opcode() == Instruction::RETURN_VOID); |
| 195 | // Are we compiling a constructor ? |
| 196 | if ((unit_.GetAccessFlags() & kAccConstructor) == 0) { |
| 197 | return; |
| 198 | } |
| 199 | // Do we need a constructor barrier ? |
| 200 | if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(), |
| 201 | unit_.GetClassDefIndex())) { |
| 202 | return; |
| 203 | } |
| 204 | // Replace RETURN_VOID by RETURN_VOID_BARRIER. |
| 205 | if (kEnableLogging) { |
| 206 | LOG(INFO) << "Replacing " << Instruction::Name(inst->Opcode()) |
| 207 | << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER) |
| 208 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 209 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
| 210 | } |
| 211 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 2u); |
| 212 | inst->SetOpcode(Instruction::RETURN_VOID_BARRIER); |
| 213 | } |
| 214 | |
| 215 | void DexCompiler::CompileInstanceFieldAccess(Instruction* inst, |
| 216 | uint32_t dex_pc, |
| 217 | Instruction::Code new_opcode, |
| 218 | bool is_put) { |
| 219 | if (!kEnableQuickening) { |
| 220 | return; |
| 221 | } |
| 222 | uint32_t field_idx = inst->VRegC_22c(); |
| 223 | int field_offset; |
| 224 | bool is_volatile; |
| 225 | bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, field_offset, |
| 226 | is_volatile, is_put); |
| 227 | if (fast_path && !is_volatile && IsUint(16, field_offset)) { |
| 228 | // TODO: use VLOG ? |
| 229 | if (kEnableLogging) { |
| 230 | LOG(INFO) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 231 | << " to " << Instruction::Name(new_opcode) |
| 232 | << " by replacing field index " << field_idx |
| 233 | << " by field offset " << field_offset |
| 234 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 235 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
| 236 | } |
| 237 | // We are modifying 4 consecutive bytes. |
| 238 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 4u); |
| 239 | inst->SetOpcode(new_opcode); |
| 240 | // Replace field index by field offset. |
| 241 | inst->SetVRegC_22c(static_cast<uint16_t>(field_offset)); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void DexCompiler::CompileInvokeVirtual(Instruction* inst, |
| 246 | uint32_t dex_pc, |
| 247 | Instruction::Code new_opcode, |
| 248 | bool is_range) { |
| 249 | if (!kEnableQuickening) { |
| 250 | return; |
| 251 | } |
| 252 | uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 253 | MethodReference target_method(&GetDexFile(), method_idx); |
| 254 | InvokeType invoke_type = kVirtual; |
| 255 | InvokeType original_invoke_type = invoke_type; |
| 256 | int vtable_idx; |
| 257 | uintptr_t direct_code; |
| 258 | uintptr_t direct_method; |
| 259 | bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc, invoke_type, |
| 260 | target_method, vtable_idx, |
| 261 | direct_code, direct_method, |
| 262 | false); |
| 263 | // TODO: support devirtualization. |
| 264 | if (fast_path && original_invoke_type == invoke_type) { |
| 265 | if (vtable_idx >= 0 && IsUint(16, vtable_idx)) { |
| 266 | // TODO: use VLOG ? |
| 267 | if (kEnableLogging) { |
| 268 | LOG(INFO) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 269 | << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")" |
| 270 | << " to " << Instruction::Name(new_opcode) |
| 271 | << " by replacing method index " << method_idx |
| 272 | << " by vtable index " << vtable_idx |
| 273 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
| 274 | << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true); |
| 275 | } |
| 276 | // We are modifying 4 consecutive bytes. |
| 277 | ScopedDexWriteAccess sdwa(GetModifiableDexFile(), inst, 4u); |
| 278 | inst->SetOpcode(new_opcode); |
| 279 | // Replace method index by vtable index. |
| 280 | if (is_range) { |
| 281 | inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx)); |
| 282 | } else { |
| 283 | inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx)); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } // namespace optimizer |
| 290 | } // namespace art |
| 291 | |
| 292 | extern "C" art::CompiledMethod* |
| 293 | ArtCompileDEX(art::CompilerDriver& compiler, const art::DexFile::CodeItem* code_item, |
| 294 | uint32_t access_flags, art::InvokeType invoke_type, |
| 295 | uint32_t class_def_idx, uint32_t method_idx, jobject class_loader, |
| 296 | const art::DexFile& dex_file) { |
| 297 | art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(), |
| 298 | dex_file, code_item, class_def_idx, method_idx, access_flags); |
| 299 | art::optimizer::DexCompiler dex_compiler(compiler, unit); |
| 300 | dex_compiler.Compile(); |
| 301 | return NULL; |
| 302 | } |