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 | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 21 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | #include "base/logging.h" |
| 24 | #include "base/mutex.h" |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 25 | #include "compiled_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
| 27 | #include "dex_instruction-inl.h" |
| 28 | #include "driver/compiler_driver.h" |
| 29 | #include "driver/dex_compilation_unit.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
| 31 | #include "mirror/dex_cache.h" |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 32 | #include "thread-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace optimizer { |
| 36 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 39 | // Controls quickening activation. |
| 40 | const bool kEnableQuickening = true; |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 41 | // Control check-cast elision. |
| 42 | const bool kEnableCheckCastEllision = true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 43 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 44 | struct QuickenedInfo { |
| 45 | QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {} |
| 46 | |
| 47 | uint32_t dex_pc; |
| 48 | uint16_t dex_member_index; |
| 49 | }; |
| 50 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | class DexCompiler { |
| 52 | public: |
| 53 | DexCompiler(art::CompilerDriver& compiler, |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 54 | const DexCompilationUnit& unit, |
| 55 | DexToDexCompilationLevel dex_to_dex_compilation_level) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | : driver_(compiler), |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 57 | unit_(unit), |
| 58 | dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 60 | ~DexCompiler() {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 61 | |
| 62 | void Compile(); |
| 63 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 64 | const std::vector<QuickenedInfo>& GetQuickenedInfo() const { |
| 65 | return quickened_info_; |
| 66 | } |
| 67 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | private: |
| 69 | const DexFile& GetDexFile() const { |
| 70 | return *unit_.GetDexFile(); |
| 71 | } |
| 72 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 73 | // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where |
| 74 | // a barrier is required. |
| 75 | void CompileReturnVoid(Instruction* inst, uint32_t dex_pc); |
| 76 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 77 | // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In |
| 78 | // this case, returns the second NOP instruction pointer. Otherwise, returns |
| 79 | // the given "inst". |
| 80 | Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc); |
| 81 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 82 | // Compiles a field access into a quick field access. |
| 83 | // The field index is replaced by an offset within an Object where we can read |
| 84 | // from / write to this field. Therefore, this does not involve any resolution |
| 85 | // at runtime. |
| 86 | // Since the field index is encoded with 16 bits, we can replace it only if the |
| 87 | // field offset can be encoded with 16 bits too. |
| 88 | void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc, |
| 89 | Instruction::Code new_opcode, bool is_put); |
| 90 | |
| 91 | // Compiles a virtual method invocation into a quick virtual method invocation. |
| 92 | // The method index is replaced by the vtable index where the corresponding |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 93 | // Executable can be found. Therefore, this does not involve any resolution |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 94 | // at runtime. |
| 95 | // Since the method index is encoded with 16 bits, we can replace it only if the |
| 96 | // vtable index can be encoded with 16 bits too. |
| 97 | void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 98 | Instruction::Code new_opcode, bool is_range); |
| 99 | |
| 100 | CompilerDriver& driver_; |
| 101 | const DexCompilationUnit& unit_; |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 102 | const DexToDexCompilationLevel dex_to_dex_compilation_level_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 104 | // Filled by the compiler when quickening, in order to encode that information |
| 105 | // in the .oat file. The runtime will use that information to get to the original |
| 106 | // opcodes. |
| 107 | std::vector<QuickenedInfo> quickened_info_; |
| 108 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(DexCompiler); |
| 110 | }; |
| 111 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 112 | void DexCompiler::Compile() { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 113 | DCHECK_EQ(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kOptimize); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 114 | const DexFile::CodeItem* code_item = unit_.GetCodeItem(); |
| 115 | const uint16_t* insns = code_item->insns_; |
| 116 | const uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 117 | Instruction* inst = const_cast<Instruction*>(Instruction::At(insns)); |
| 118 | |
| 119 | for (uint32_t dex_pc = 0; dex_pc < insns_size; |
| 120 | inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) { |
| 121 | switch (inst->Opcode()) { |
| 122 | case Instruction::RETURN_VOID: |
| 123 | CompileReturnVoid(inst, dex_pc); |
| 124 | break; |
| 125 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 126 | case Instruction::CHECK_CAST: |
| 127 | inst = CompileCheckCast(inst, dex_pc); |
| 128 | break; |
| 129 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 130 | case Instruction::IGET: |
| 131 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false); |
| 132 | break; |
| 133 | |
| 134 | case Instruction::IGET_WIDE: |
| 135 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false); |
| 136 | break; |
| 137 | |
| 138 | case Instruction::IGET_OBJECT: |
| 139 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false); |
| 140 | break; |
| 141 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 142 | case Instruction::IGET_BOOLEAN: |
| 143 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false); |
| 144 | break; |
| 145 | |
| 146 | case Instruction::IGET_BYTE: |
| 147 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false); |
| 148 | break; |
| 149 | |
| 150 | case Instruction::IGET_CHAR: |
| 151 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false); |
| 152 | break; |
| 153 | |
| 154 | case Instruction::IGET_SHORT: |
| 155 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false); |
| 156 | break; |
| 157 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 158 | case Instruction::IPUT: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true); |
| 160 | break; |
| 161 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 162 | case Instruction::IPUT_BOOLEAN: |
| 163 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true); |
| 164 | break; |
| 165 | |
| 166 | case Instruction::IPUT_BYTE: |
| 167 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true); |
| 168 | break; |
| 169 | |
| 170 | case Instruction::IPUT_CHAR: |
| 171 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true); |
| 172 | break; |
| 173 | |
| 174 | case Instruction::IPUT_SHORT: |
| 175 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true); |
| 176 | break; |
| 177 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | case Instruction::IPUT_WIDE: |
| 179 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true); |
| 180 | break; |
| 181 | |
| 182 | case Instruction::IPUT_OBJECT: |
| 183 | CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true); |
| 184 | break; |
| 185 | |
| 186 | case Instruction::INVOKE_VIRTUAL: |
| 187 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false); |
| 188 | break; |
| 189 | |
| 190 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 191 | CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true); |
| 192 | break; |
| 193 | |
| 194 | default: |
| 195 | // Nothing to do. |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) { |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 202 | DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID); |
| 203 | if (unit_.IsConstructor()) { |
| 204 | // Are we compiling a non clinit constructor which needs a barrier ? |
| 205 | if (!unit_.IsStatic() && |
| 206 | driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(), |
| 207 | unit_.GetClassDefIndex())) { |
| 208 | return; |
| 209 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 210 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 211 | // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 212 | VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode()) |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 213 | << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER) |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 214 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 215 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 216 | inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 219 | Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 220 | if (!kEnableCheckCastEllision) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 221 | return inst; |
| 222 | } |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 223 | if (!driver_.IsSafeCast(&unit_, dex_pc)) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 224 | return inst; |
| 225 | } |
| 226 | // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code |
| 227 | // units and a "nop" instruction size is 1 code unit, we need to replace it by |
| 228 | // 2 consecutive NOP instructions. |
| 229 | // Because the caller loops over instructions by calling Instruction::Next onto |
| 230 | // the current instruction, we need to return the 2nd NOP instruction. Indeed, |
| 231 | // its next instruction is the former check-cast's next instruction. |
| 232 | VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode()) |
| 233 | << " by replacing it with 2 NOPs at dex pc " |
| 234 | << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 235 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Nicolas Geoffray | 01b70e8 | 2016-11-17 10:58:36 +0000 | [diff] [blame] | 236 | quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c())); |
| 237 | quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c())); |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 238 | // We are modifying 4 consecutive bytes. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 239 | inst->SetOpcode(Instruction::NOP); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 240 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 241 | // Get to next instruction which is the second half of check-cast and replace |
| 242 | // it by a NOP. |
| 243 | inst = const_cast<Instruction*>(inst->Next()); |
| 244 | inst->SetOpcode(Instruction::NOP); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 245 | inst->SetVRegA_10x(0u); // keep compliant with verifier. |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 246 | return inst; |
| 247 | } |
| 248 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 249 | void DexCompiler::CompileInstanceFieldAccess(Instruction* inst, |
| 250 | uint32_t dex_pc, |
| 251 | Instruction::Code new_opcode, |
| 252 | bool is_put) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 253 | if (!kEnableQuickening) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | uint32_t field_idx = inst->VRegC_22c(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 257 | MemberOffset field_offset(0u); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 258 | bool is_volatile; |
Ian Rogers | 9b297bf | 2013-09-06 11:11:25 -0700 | [diff] [blame] | 259 | bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put, |
| 260 | &field_offset, &is_volatile); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 261 | if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) { |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 262 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
| 263 | << " to " << Instruction::Name(new_opcode) |
| 264 | << " by replacing field index " << field_idx |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 265 | << " by field offset " << field_offset.Int32Value() |
Sebastien Hertz | 543959c | 2013-07-03 12:00:19 +0200 | [diff] [blame] | 266 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 267 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 268 | // We are modifying 4 consecutive bytes. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 269 | inst->SetOpcode(new_opcode); |
| 270 | // Replace field index by field offset. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 271 | inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value())); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 272 | quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 276 | void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc, |
| 277 | Instruction::Code new_opcode, bool is_range) { |
Andreas Gampe | 1a4bc7f | 2017-03-27 14:57:30 -0700 | [diff] [blame] | 278 | if (!kEnableQuickening) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 282 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 283 | |
| 284 | ClassLinker* class_linker = unit_.GetClassLinker(); |
| 285 | ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>( |
| 286 | GetDexFile(), |
| 287 | method_idx, |
| 288 | unit_.GetDexCache(), |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 289 | unit_.GetClassLoader(), |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 290 | /* referrer */ nullptr, |
| 291 | kVirtual); |
| 292 | |
| 293 | if (UNLIKELY(resolved_method == nullptr)) { |
| 294 | // Clean up any exception left by type resolution. |
| 295 | soa.Self()->ClearException(); |
| 296 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 297 | } |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 298 | |
| 299 | uint32_t vtable_idx = resolved_method->GetMethodIndex(); |
| 300 | DCHECK(IsUint<16>(vtable_idx)); |
| 301 | VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 302 | << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")" |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 303 | << " to " << Instruction::Name(new_opcode) |
| 304 | << " by replacing method index " << method_idx |
| 305 | << " by vtable index " << vtable_idx |
| 306 | << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 307 | << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 308 | // We are modifying 4 consecutive bytes. |
| 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 | quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 319 | CompiledMethod* ArtCompileDEX( |
| 320 | CompilerDriver* driver, |
| 321 | const DexFile::CodeItem* code_item, |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 322 | uint32_t access_flags, |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 323 | InvokeType invoke_type ATTRIBUTE_UNUSED, |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 324 | uint16_t class_def_idx, |
| 325 | uint32_t method_idx, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 326 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 327 | const DexFile& dex_file, |
| 328 | DexToDexCompilationLevel dex_to_dex_compilation_level) { |
| 329 | DCHECK(driver != nullptr); |
| 330 | if (dex_to_dex_compilation_level != DexToDexCompilationLevel::kDontDexToDexCompile) { |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 331 | ScopedObjectAccess soa(Thread::Current()); |
| 332 | StackHandleScope<1> hs(soa.Self()); |
| 333 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 334 | art::DexCompilationUnit unit( |
| 335 | class_loader, |
| 336 | class_linker, |
| 337 | dex_file, |
| 338 | code_item, |
| 339 | class_def_idx, |
| 340 | method_idx, |
| 341 | access_flags, |
| 342 | driver->GetVerifiedMethod(&dex_file, method_idx), |
| 343 | hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file))); |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 344 | art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level); |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 345 | dex_compiler.Compile(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 346 | if (dex_compiler.GetQuickenedInfo().empty()) { |
| 347 | // No need to create a CompiledMethod if there are no quickened opcodes. |
| 348 | return nullptr; |
| 349 | } |
| 350 | |
| 351 | // Create a `CompiledMethod`, with the quickened information in the vmap table. |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 352 | Leb128EncodingVector<> builder; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 353 | for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) { |
| 354 | builder.PushBackUnsigned(info.dex_pc); |
| 355 | builder.PushBackUnsigned(info.dex_member_index); |
| 356 | } |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 357 | InstructionSet instruction_set = driver->GetInstructionSet(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 358 | if (instruction_set == kThumb2) { |
| 359 | // Don't use the thumb2 instruction set to avoid the one off code delta. |
| 360 | instruction_set = kArm; |
| 361 | } |
| 362 | return CompiledMethod::SwapAllocCompiledMethod( |
Andreas Gampe | 5eb0d38 | 2015-07-23 01:19:26 -0700 | [diff] [blame] | 363 | driver, |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 364 | instruction_set, |
| 365 | ArrayRef<const uint8_t>(), // no code |
| 366 | 0, |
| 367 | 0, |
| 368 | 0, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 369 | ArrayRef<const uint8_t>(), // method_info |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 370 | ArrayRef<const uint8_t>(builder.GetData()), // vmap_table |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 371 | ArrayRef<const uint8_t>(), // cfi data |
| 372 | ArrayRef<const LinkerPatch>()); |
Sebastien Hertz | 7502122 | 2013-07-16 18:34:50 +0200 | [diff] [blame] | 373 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 374 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 375 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 376 | |
| 377 | } // namespace optimizer |
| 378 | |
| 379 | } // namespace art |