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 | |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 17 | #include "dex_to_dex_compiler.h" |
| 18 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 24 | #include "base/logging.h" // For VLOG |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "base/mutex.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 27 | #include "compiled_method.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 28 | #include "dex/bytecode_utils.h" |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 29 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 30 | #include "dex/dex_file-inl.h" |
| 31 | #include "dex/dex_instruction-inl.h" |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 32 | #include "dex_to_dex_decompiler.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "driver/compiler_driver.h" |
| 34 | #include "driver/dex_compilation_unit.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | #include "mirror/dex_cache.h" |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 36 | #include "quicken_info.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 37 | #include "thread-current-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | namespace optimizer { |
| 41 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | // Controls quickening activation. |
| 45 | const bool kEnableQuickening = true; |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 46 | // Control check-cast elision. |
| 47 | const bool kEnableCheckCastEllision = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 48 | |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 49 | // Holds the state for compiling a single method. |
| 50 | struct DexToDexCompiler::CompilationState { |
| 51 | struct QuickenedInfo { |
| 52 | QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {} |
| 53 | |
| 54 | uint32_t dex_pc; |
| 55 | uint16_t dex_member_index; |
| 56 | }; |
| 57 | |
| 58 | CompilationState(DexToDexCompiler* compiler, |
| 59 | const DexCompilationUnit& unit, |
| 60 | const CompilationLevel compilation_level, |
| 61 | const std::vector<uint8_t>* quicken_data); |
| 62 | |
| 63 | const std::vector<QuickenedInfo>& GetQuickenedInfo() const { |
| 64 | return quickened_info_; |
| 65 | } |
| 66 | |
| 67 | // Returns the quickening info, or an empty array if it was not quickened. |
| 68 | // If already_quickened is true, then don't change anything but still return what the quicken |
| 69 | // data would have been. |
| 70 | std::vector<uint8_t> Compile(); |
| 71 | |
| 72 | const DexFile& GetDexFile() const; |
| 73 | |
| 74 | // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where |
| 75 | // a barrier is required. |
| 76 | void CompileReturnVoid(Instruction* inst, uint32_t dex_pc); |
| 77 | |
| 78 | // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In |
| 79 | // this case, returns the second NOP instruction pointer. Otherwise, returns |
| 80 | // the given "inst". |
| 81 | Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc); |
| 82 | |
| 83 | // Compiles a field access into a quick field access. |
| 84 | // The field index is replaced by an offset within an Object where we can read |
| 85 | // from / write to this field. Therefore, this does not involve any resolution |
| 86 | // at runtime. |
| 87 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 88 | // field offset can be encoded with 16 bits too. |
| 89 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 90 | Instruction::Code new_opcode, bool is_put); |
| 91 | |
| 92 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 93 | // The method index is replaced by the vtable index where the corresponding |
| 94 | // executable can be found. Therefore, this does not involve any resolution |
| 95 | // at runtime. |
| 96 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 97 | // vtable index can be encoded with 16 bits too. |
| 98 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 99 | Instruction::Code new_opcode, bool is_range); |
| 100 | |
| 101 | // Return the next index. |
| 102 | uint16_t NextIndex(); |
| 103 | |
| 104 | // Returns the dequickened index if an instruction is quickened, otherwise return index. |
| 105 | uint16_t GetIndexForInstruction(const Instruction* inst, uint32_t index); |
| 106 | |
| 107 | DexToDexCompiler* const compiler_; |
| 108 | CompilerDriver& driver_; |
| 109 | const DexCompilationUnit& unit_; |
| 110 | const CompilationLevel compilation_level_; |
| 111 | |
| 112 | // Filled by the compiler when quickening, in order to encode that information |
| 113 | // in the .oat file. The runtime will use that information to get to the original |
| 114 | // opcodes. |
| 115 | std::vector<QuickenedInfo> quickened_info_; |
| 116 | |
| 117 | // True if we optimized a return void to a return void no barrier. |
| 118 | bool optimized_return_void_ = false; |
| 119 | |
| 120 | // If the code item was already quickened previously. |
| 121 | const bool already_quickened_; |
| 122 | const QuickenInfoTable existing_quicken_info_; |
| 123 | uint32_t quicken_index_ = 0u; |
| 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(CompilationState); |
| 126 | }; |
| 127 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 128 | DexToDexCompiler::DexToDexCompiler(CompilerDriver* driver) |
| 129 | : driver_(driver), |
| 130 | lock_("Quicken lock", kDexToDexCompilerLock) { |
| 131 | DCHECK(driver != nullptr); |
| 132 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 133 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 134 | void DexToDexCompiler::ClearState() { |
| 135 | MutexLock lock(Thread::Current(), lock_); |
| 136 | active_dex_file_ = nullptr; |
| 137 | active_bit_vector_ = nullptr; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 138 | should_quicken_.clear(); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 139 | shared_code_item_quicken_info_.clear(); |
| 140 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 141 | |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 142 | size_t DexToDexCompiler::NumCodeItemsToQuicken(Thread* self) const { |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 143 | MutexLock lock(self, lock_); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 144 | return num_code_items_; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | BitVector* DexToDexCompiler::GetOrAddBitVectorForDex(const DexFile* dex_file) { |
| 148 | if (active_dex_file_ != dex_file) { |
| 149 | active_dex_file_ = dex_file; |
| 150 | auto inserted = should_quicken_.emplace(dex_file, |
| 151 | BitVector(dex_file->NumMethodIds(), |
| 152 | /*expandable*/ false, |
| 153 | Allocator::GetMallocAllocator())); |
| 154 | active_bit_vector_ = &inserted.first->second; |
| 155 | } |
| 156 | return active_bit_vector_; |
| 157 | } |
| 158 | |
| 159 | void DexToDexCompiler::MarkForCompilation(Thread* self, |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 160 | const MethodReference& method_ref) { |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 161 | MutexLock lock(self, lock_); |
| 162 | BitVector* const bitmap = GetOrAddBitVectorForDex(method_ref.dex_file); |
| 163 | DCHECK(bitmap != nullptr); |
| 164 | DCHECK(!bitmap->IsBitSet(method_ref.index)); |
| 165 | bitmap->SetBit(method_ref.index); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 166 | ++num_code_items_; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | DexToDexCompiler::CompilationState::CompilationState(DexToDexCompiler* compiler, |
| 170 | const DexCompilationUnit& unit, |
| 171 | const CompilationLevel compilation_level, |
| 172 | const std::vector<uint8_t>* quicken_data) |
| 173 | : compiler_(compiler), |
| 174 | driver_(*compiler->GetDriver()), |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 175 | unit_(unit), |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 176 | compilation_level_(compilation_level), |
| 177 | already_quickened_(quicken_data != nullptr), |
| 178 | existing_quicken_info_(already_quickened_ |
| 179 | ? ArrayRef<const uint8_t>(*quicken_data) : ArrayRef<const uint8_t>()) {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 181 | uint16_t DexToDexCompiler::CompilationState::NextIndex() { |
| 182 | DCHECK(already_quickened_); |
| 183 | if (kIsDebugBuild && quicken_index_ >= existing_quicken_info_.NumIndices()) { |
| 184 | for (const DexInstructionPcPair& pair : unit_.GetCodeItemAccessor()) { |
| 185 | LOG(ERROR) << pair->DumpString(nullptr); |
| 186 | } |
| 187 | LOG(FATAL) << "Mismatched number of quicken slots."; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 188 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 189 | const uint16_t ret = existing_quicken_info_.GetData(quicken_index_); |
| 190 | quicken_index_++; |
| 191 | return ret; |
| 192 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 193 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 194 | uint16_t DexToDexCompiler::CompilationState::GetIndexForInstruction(const Instruction* inst, |
| 195 | uint32_t index) { |
| 196 | if (UNLIKELY(already_quickened_)) { |
| 197 | return inst->IsQuickened() ? NextIndex() : index; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 198 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 199 | DCHECK(!inst->IsQuickened()); |
| 200 | return index; |
| 201 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 202 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 203 | bool DexToDexCompiler::ShouldCompileMethod(const MethodReference& ref) { |
| 204 | // TODO: It's probably safe to avoid the lock here if the active_dex_file_ matches since we only |
| 205 | // only call ShouldCompileMethod on one dex at a time. |
| 206 | MutexLock lock(Thread::Current(), lock_); |
| 207 | return GetOrAddBitVectorForDex(ref.dex_file)->IsBitSet(ref.index); |
| 208 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 209 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 210 | std::vector<uint8_t> DexToDexCompiler::CompilationState::Compile() { |
| 211 | DCHECK_EQ(compilation_level_, CompilationLevel::kOptimize); |
| 212 | const CodeItemDataAccessor& instructions = unit_.GetCodeItemAccessor(); |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 213 | for (DexInstructionIterator it = instructions.begin(); it != instructions.end(); ++it) { |
| 214 | const uint32_t dex_pc = it.DexPc(); |
| 215 | Instruction* inst = const_cast<Instruction*>(&it.Inst()); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 216 | |
| 217 | if (!already_quickened_) { |
| 218 | DCHECK(!inst->IsQuickened()); |
| 219 | } |
| 220 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 221 | switch (inst->Opcode()) { |
| 222 | case Instruction::RETURN_VOID: |
| 223 | CompileReturnVoid(inst, dex_pc); |
| 224 | break; |
| 225 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 226 | case Instruction::CHECK_CAST: |
| 227 | inst = CompileCheckCast(inst, dex_pc); |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 228 | if (inst->Opcode() == Instruction::NOP) { |
| 229 | // We turned the CHECK_CAST into two NOPs, avoid visiting the second NOP twice since this |
| 230 | // would add 2 quickening info entries. |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 231 | ++it; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 232 | } |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 233 | break; |
| 234 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 235 | case Instruction::IGET: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 236 | case Instruction::IGET_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 237 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false); |
| 238 | break; |
| 239 | |
| 240 | case Instruction::IGET_WIDE: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 241 | case Instruction::IGET_WIDE_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 242 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false); |
| 243 | break; |
| 244 | |
| 245 | case Instruction::IGET_OBJECT: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 246 | case Instruction::IGET_OBJECT_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 247 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false); |
| 248 | break; |
| 249 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 250 | case Instruction::IGET_BOOLEAN: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 251 | case Instruction::IGET_BOOLEAN_QUICK: |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 252 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false); |
| 253 | break; |
| 254 | |
| 255 | case Instruction::IGET_BYTE: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 256 | case Instruction::IGET_BYTE_QUICK: |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 257 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false); |
| 258 | break; |
| 259 | |
| 260 | case Instruction::IGET_CHAR: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 261 | case Instruction::IGET_CHAR_QUICK: |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 262 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false); |
| 263 | break; |
| 264 | |
| 265 | case Instruction::IGET_SHORT: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 266 | case Instruction::IGET_SHORT_QUICK: |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 267 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false); |
| 268 | break; |
| 269 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | case Instruction::IPUT: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 271 | case Instruction::IPUT_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 272 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true); |
| 273 | break; |
| 274 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 275 | case Instruction::IPUT_BOOLEAN: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 276 | case Instruction::IPUT_BOOLEAN_QUICK: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 277 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true); |
| 278 | break; |
| 279 | |
| 280 | case Instruction::IPUT_BYTE: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 281 | case Instruction::IPUT_BYTE_QUICK: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 282 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true); |
| 283 | break; |
| 284 | |
| 285 | case Instruction::IPUT_CHAR: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 286 | case Instruction::IPUT_CHAR_QUICK: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 287 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true); |
| 288 | break; |
| 289 | |
| 290 | case Instruction::IPUT_SHORT: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 291 | case Instruction::IPUT_SHORT_QUICK: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 292 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true); |
| 293 | break; |
| 294 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 295 | case Instruction::IPUT_WIDE: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 296 | case Instruction::IPUT_WIDE_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 297 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true); |
| 298 | break; |
| 299 | |
| 300 | case Instruction::IPUT_OBJECT: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 301 | case Instruction::IPUT_OBJECT_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 302 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true); |
| 303 | break; |
| 304 | |
| 305 | case Instruction::INVOKE_VIRTUAL: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 306 | case Instruction::INVOKE_VIRTUAL_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 307 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false); |
| 308 | break; |
| 309 | |
| 310 | case Instruction::INVOKE_VIRTUAL_RANGE: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 311 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true); |
| 313 | break; |
| 314 | |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 315 | case Instruction::NOP: |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 316 | if (already_quickened_) { |
| 317 | const uint16_t reference_index = NextIndex(); |
| 318 | quickened_info_.push_back(QuickenedInfo(dex_pc, reference_index)); |
| 319 | if (reference_index == DexFile::kDexNoIndex16) { |
| 320 | // This means it was a normal nop and not a check-cast. |
| 321 | break; |
| 322 | } |
| 323 | const uint16_t type_index = NextIndex(); |
| 324 | if (driver_.IsSafeCast(&unit_, dex_pc)) { |
| 325 | quickened_info_.push_back(QuickenedInfo(dex_pc, type_index)); |
| 326 | } |
| 327 | ++it; |
| 328 | } else { |
| 329 | // We need to differentiate between check cast inserted NOP and normal NOP, put an invalid |
| 330 | // index in the map for normal nops. This should be rare in real code. |
| 331 | quickened_info_.push_back(QuickenedInfo(dex_pc, DexFile::kDexNoIndex16)); |
| 332 | } |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 333 | break; |
| 334 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 335 | default: |
| 336 | // Nothing to do. |
| 337 | break; |
| 338 | } |
| 339 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 340 | |
| 341 | if (already_quickened_) { |
| 342 | DCHECK_EQ(quicken_index_, existing_quicken_info_.NumIndices()); |
| 343 | } |
| 344 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 345 | // Even if there are no indicies, generate an empty quicken info so that we know the method was |
| 346 | // quickened. |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 347 | |
| 348 | std::vector<uint8_t> quicken_data; |
| 349 | if (kIsDebugBuild) { |
| 350 | // Double check that the counts line up with the size of the quicken info. |
| 351 | size_t quicken_count = 0; |
| 352 | for (const DexInstructionPcPair& pair : instructions) { |
| 353 | if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) { |
| 354 | ++quicken_count; |
| 355 | } |
| 356 | } |
| 357 | CHECK_EQ(quicken_count, GetQuickenedInfo().size()); |
| 358 | } |
| 359 | |
| 360 | QuickenInfoTable::Builder builder(&quicken_data, GetQuickenedInfo().size()); |
| 361 | // Length is encoded by the constructor. |
| 362 | for (const CompilationState::QuickenedInfo& info : GetQuickenedInfo()) { |
| 363 | // Dex pc is not serialized, only used for checking the instructions. Since we access the |
| 364 | // array based on the index of the quickened instruction, the indexes must line up perfectly. |
| 365 | // The reader side uses the NeedsIndexForInstruction function too. |
| 366 | const Instruction& inst = instructions.InstructionAt(info.dex_pc); |
| 367 | CHECK(QuickenInfoTable::NeedsIndexForInstruction(&inst)) << inst.Opcode(); |
| 368 | builder.AddIndex(info.dex_member_index); |
| 369 | } |
| 370 | DCHECK(!quicken_data.empty()); |
| 371 | return quicken_data; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 374 | void DexToDexCompiler::CompilationState::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) { |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 375 | DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID); |
| 376 | if (unit_.IsConstructor()) { |
| 377 | // Are we compiling a non clinit constructor which needs a barrier ? |
| 378 | if (!unit_.IsStatic() && |
| 379 | driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(), |
| 380 | unit_.GetClassDefIndex())) { |
| 381 | return; |
| 382 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 383 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 384 | // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 385 | VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode()) |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 386 | << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER) |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 387 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 388 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 389 | inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 390 | optimized_return_void_ = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 393 | Instruction* DexToDexCompiler::CompilationState::CompileCheckCast(Instruction* inst, |
| 394 | uint32_t dex_pc) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 395 | if (!kEnableCheckCastEllision) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 396 | return inst; |
| 397 | } |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 398 | if (!driver_.IsSafeCast(&unit_, dex_pc)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 399 | return inst; |
| 400 | } |
| 401 | // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code |
| 402 | // units and a "nop" instruction size is 1 code unit, we need to replace it by |
| 403 | // 2 consecutive NOP instructions. |
| 404 | // Because the caller loops over instructions by calling Instruction::Next onto |
| 405 | // the current instruction, we need to return the 2nd NOP instruction. Indeed, |
| 406 | // its next instruction is the former check-cast's next instruction. |
| 407 | VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode()) |
| 408 | << " by replacing it with 2 NOPs at dex pc " |
| 409 | << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 410 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 411 | if (!already_quickened_) { |
| 412 | quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c())); |
| 413 | quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c())); |
| 414 | |
| 415 | // We are modifying 4 consecutive bytes. |
| 416 | inst->SetOpcode(Instruction::NOP); |
| 417 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
| 418 | // Get to next instruction which is the second half of check-cast and replace |
| 419 | // it by a NOP. |
| 420 | inst = const_cast<Instruction*>(inst->Next()); |
| 421 | inst->SetOpcode(Instruction::NOP); |
| 422 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
| 423 | } |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 424 | return inst; |
| 425 | } |
| 426 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 427 | void DexToDexCompiler::CompilationState::CompileInstanceFieldAccess(Instruction* inst, |
| 428 | uint32_t dex_pc, |
| 429 | Instruction::Code new_opcode, |
| 430 | bool is_put) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 431 | if (!kEnableQuickening) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 434 | uint32_t field_idx = GetIndexForInstruction(inst, inst->VRegC_22c()); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 435 | MemberOffset field_offset(0u); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 436 | bool is_volatile; |
Ian Rogers | 9b297bf | 2013-09-06 11:11:25 -0700 | [diff] [blame] | 437 | bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put, |
| 438 | &field_offset, &is_volatile); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 439 | if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 440 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 441 | << " to " << Instruction::Name(new_opcode) |
| 442 | << " by replacing field index " << field_idx |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 443 | << " by field offset " << field_offset.Int32Value() |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 444 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 445 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 446 | if (!already_quickened_) { |
| 447 | // We are modifying 4 consecutive bytes. |
| 448 | inst->SetOpcode(new_opcode); |
| 449 | // Replace field index by field offset. |
| 450 | inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value())); |
| 451 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 452 | quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 456 | const DexFile& DexToDexCompiler::CompilationState::GetDexFile() const { |
| 457 | return *unit_.GetDexFile(); |
| 458 | } |
| 459 | |
| 460 | void DexToDexCompiler::CompilationState::CompileInvokeVirtual(Instruction* inst, |
| 461 | uint32_t dex_pc, |
| 462 | Instruction::Code new_opcode, |
| 463 | bool is_range) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 464 | if (!kEnableQuickening) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 467 | uint32_t method_idx = GetIndexForInstruction(inst, |
| 468 | is_range ? inst->VRegB_3rc() : inst->VRegB_35c()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 469 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 470 | |
| 471 | ClassLinker* class_linker = unit_.GetClassLinker(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 472 | ArtMethod* resolved_method = |
| 473 | class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 474 | method_idx, |
| 475 | unit_.GetDexCache(), |
| 476 | unit_.GetClassLoader(), |
| 477 | /* referrer */ nullptr, |
| 478 | kVirtual); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 479 | |
| 480 | if (UNLIKELY(resolved_method == nullptr)) { |
| 481 | // Clean up any exception left by type resolution. |
| 482 | soa.Self()->ClearException(); |
| 483 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 484 | } |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 485 | |
| 486 | uint32_t vtable_idx = resolved_method->GetMethodIndex(); |
| 487 | DCHECK(IsUint<16>(vtable_idx)); |
| 488 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 489 | << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")" |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 490 | << " to " << Instruction::Name(new_opcode) |
| 491 | << " by replacing method index " << method_idx |
| 492 | << " by vtable index " << vtable_idx |
| 493 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 494 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 495 | if (!already_quickened_) { |
| 496 | // We are modifying 4 consecutive bytes. |
| 497 | inst->SetOpcode(new_opcode); |
| 498 | // Replace method index by vtable index. |
| 499 | if (is_range) { |
| 500 | inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx)); |
| 501 | } else { |
| 502 | inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx)); |
| 503 | } |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 504 | } |
| 505 | quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 508 | CompiledMethod* DexToDexCompiler::CompileMethod( |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 509 | const DexFile::CodeItem* code_item, |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 510 | uint32_t access_flags, |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 511 | InvokeType invoke_type ATTRIBUTE_UNUSED, |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 512 | uint16_t class_def_idx, |
| 513 | uint32_t method_idx, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 514 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 515 | const DexFile& dex_file, |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 516 | CompilationLevel compilation_level) { |
| 517 | if (compilation_level == CompilationLevel::kDontDexToDexCompile) { |
| 518 | return nullptr; |
| 519 | } |
| 520 | |
| 521 | ScopedObjectAccess soa(Thread::Current()); |
| 522 | StackHandleScope<1> hs(soa.Self()); |
| 523 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 524 | art::DexCompilationUnit unit( |
| 525 | class_loader, |
| 526 | class_linker, |
| 527 | dex_file, |
| 528 | code_item, |
| 529 | class_def_idx, |
| 530 | method_idx, |
| 531 | access_flags, |
| 532 | driver_->GetVerifiedMethod(&dex_file, method_idx), |
| 533 | hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file))); |
| 534 | |
| 535 | std::vector<uint8_t> quicken_data; |
| 536 | // If the code item is shared with multiple different method ids, make sure that we quicken only |
| 537 | // once and verify that all the dequicken maps match. |
| 538 | if (UNLIKELY(shared_code_items_.find(code_item) != shared_code_items_.end())) { |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 539 | // Avoid quickening the shared code items for now because the existing conflict detection logic |
| 540 | // does not currently handle cases where the code item is quickened in one place but |
| 541 | // compiled in another. |
| 542 | static constexpr bool kAvoidQuickeningSharedCodeItems = true; |
| 543 | if (kAvoidQuickeningSharedCodeItems) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 544 | return nullptr; |
| 545 | } |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 546 | // For shared code items, use a lock to prevent races. |
| 547 | MutexLock mu(soa.Self(), lock_); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 548 | auto existing = shared_code_item_quicken_info_.find(code_item); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 549 | QuickenState* existing_data = nullptr; |
| 550 | std::vector<uint8_t>* existing_quicken_data = nullptr; |
| 551 | if (existing != shared_code_item_quicken_info_.end()) { |
| 552 | existing_data = &existing->second; |
| 553 | if (existing_data->conflict_) { |
| 554 | return nullptr; |
| 555 | } |
| 556 | existing_quicken_data = &existing_data->quicken_data_; |
| 557 | } |
| 558 | bool optimized_return_void; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 559 | { |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 560 | CompilationState state(this, unit, compilation_level, existing_quicken_data); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 561 | quicken_data = state.Compile(); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 562 | optimized_return_void = state.optimized_return_void_; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 563 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 564 | |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 565 | // Already quickened, check that the data matches what was previously seen. |
| 566 | MethodReference method_ref(&dex_file, method_idx); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 567 | if (existing_data != nullptr) { |
| 568 | if (*existing_quicken_data != quicken_data || |
| 569 | existing_data->optimized_return_void_ != optimized_return_void) { |
| 570 | VLOG(compiler) << "Quicken data mismatch, for method " |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 571 | << dex_file.PrettyMethod(method_idx); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 572 | // Mark the method as a conflict to never attempt to quicken it in the future. |
| 573 | existing_data->conflict_ = true; |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 574 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 575 | existing_data->methods_.push_back(method_ref); |
| 576 | } else { |
| 577 | QuickenState new_state; |
| 578 | new_state.methods_.push_back(method_ref); |
| 579 | new_state.quicken_data_ = quicken_data; |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 580 | new_state.optimized_return_void_ = optimized_return_void; |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 581 | bool inserted = shared_code_item_quicken_info_.emplace(code_item, new_state).second; |
| 582 | CHECK(inserted) << "Failed to insert " << dex_file.PrettyMethod(method_idx); |
Mathieu Chartier | de4b08f | 2017-07-10 14:13:41 -0700 | [diff] [blame] | 583 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 584 | |
| 585 | // Easy sanity check is to check that the existing stuff matches by re-quickening using the |
| 586 | // newly produced quicken data. |
| 587 | // Note that this needs to be behind the lock for this case since we may unquicken in another |
| 588 | // thread. |
| 589 | if (kIsDebugBuild) { |
| 590 | CompilationState state2(this, unit, compilation_level, &quicken_data); |
| 591 | std::vector<uint8_t> new_data = state2.Compile(); |
| 592 | CHECK(new_data == quicken_data) << "Mismatch producing new quicken data"; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 593 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 594 | } else { |
| 595 | CompilationState state(this, unit, compilation_level, /*quicken_data*/ nullptr); |
| 596 | quicken_data = state.Compile(); |
| 597 | |
| 598 | // Easy sanity check is to check that the existing stuff matches by re-quickening using the |
| 599 | // newly produced quicken data. |
| 600 | if (kIsDebugBuild) { |
| 601 | CompilationState state2(this, unit, compilation_level, &quicken_data); |
| 602 | std::vector<uint8_t> new_data = state2.Compile(); |
| 603 | CHECK(new_data == quicken_data) << "Mismatch producing new quicken data"; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 604 | } |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 605 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 606 | |
| 607 | if (quicken_data.empty()) { |
| 608 | return nullptr; |
| 609 | } |
| 610 | |
| 611 | // Create a `CompiledMethod`, with the quickened information in the vmap table. |
| 612 | InstructionSet instruction_set = driver_->GetInstructionSet(); |
| 613 | if (instruction_set == InstructionSet::kThumb2) { |
| 614 | // Don't use the thumb2 instruction set to avoid the one off code delta. |
| 615 | instruction_set = InstructionSet::kArm; |
| 616 | } |
| 617 | CompiledMethod* ret = CompiledMethod::SwapAllocCompiledMethod( |
| 618 | driver_, |
| 619 | instruction_set, |
| 620 | ArrayRef<const uint8_t>(), // no code |
| 621 | 0, |
| 622 | 0, |
| 623 | 0, |
| 624 | ArrayRef<const uint8_t>(), // method_info |
| 625 | ArrayRef<const uint8_t>(quicken_data), // vmap_table |
| 626 | ArrayRef<const uint8_t>(), // cfi data |
| 627 | ArrayRef<const linker::LinkerPatch>()); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 628 | DCHECK(ret != nullptr); |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 629 | return ret; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 630 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 631 | |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 632 | void DexToDexCompiler::SetDexFiles(const std::vector<const DexFile*>& dex_files) { |
| 633 | // Record what code items are already seen to detect when multiple methods have the same code |
| 634 | // item. |
| 635 | std::unordered_set<const DexFile::CodeItem*> seen_code_items; |
| 636 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 637 | for (ClassAccessor accessor : dex_file->GetClasses()) { |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame^] | 638 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
Mathieu Chartier | c8c8d5f | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 639 | const DexFile::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 640 | // Detect the shared code items. |
| 641 | if (!seen_code_items.insert(code_item).second) { |
| 642 | shared_code_items_.insert(code_item); |
| 643 | } |
Mathieu Chartier | 0d896bd | 2018-05-25 00:20:27 -0700 | [diff] [blame^] | 644 | } |
Mathieu Chartier | 279e3a3 | 2018-01-24 18:17:55 -0800 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | VLOG(compiler) << "Shared code items " << shared_code_items_.size(); |
| 648 | } |
| 649 | |
| 650 | void DexToDexCompiler::UnquickenConflictingMethods() { |
| 651 | MutexLock mu(Thread::Current(), lock_); |
| 652 | size_t unquicken_count = 0; |
| 653 | for (const auto& pair : shared_code_item_quicken_info_) { |
| 654 | const DexFile::CodeItem* code_item = pair.first; |
| 655 | const QuickenState& state = pair.second; |
| 656 | CHECK_GE(state.methods_.size(), 1u); |
| 657 | if (state.conflict_) { |
| 658 | // Unquicken using the existing quicken data. |
| 659 | // TODO: Do we really need to pass a dex file in? |
| 660 | optimizer::ArtDecompileDEX(*state.methods_[0].dex_file, |
| 661 | *code_item, |
| 662 | ArrayRef<const uint8_t>(state.quicken_data_), |
| 663 | /* decompile_return_instruction*/ true); |
| 664 | ++unquicken_count; |
| 665 | // Go clear the vmaps for all the methods that were already quickened to avoid writing them |
| 666 | // out during oat writing. |
| 667 | for (const MethodReference& ref : state.methods_) { |
| 668 | CompiledMethod* method = driver_->RemoveCompiledMethod(ref); |
| 669 | if (method != nullptr) { |
| 670 | // There is up to one compiled method for each method ref. Releasing it leaves the |
| 671 | // deduped data intact, this means its safe to do even when other threads might be |
| 672 | // compiling. |
| 673 | CompiledMethod::ReleaseSwapAllocatedCompiledMethod(driver_, method); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 680 | } // namespace optimizer |
| 681 | |
| 682 | } // namespace art |