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