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