blob: 476903522e634c57b0d858cbdaec1f09e89f844f [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Gampe5eb0d382015-07-23 01:19:26 -070017#include "dex_to_dex_compiler.h"
18
Andreas Gampe57943812017-12-06 21:39:13 -080019#include <android-base/logging.h>
20#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080024#include "base/logging.h" // For VLOG
Andreas Gampe57943812017-12-06 21:39:13 -080025#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "base/mutex.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070027#include "bytecode_utils.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010028#include "compiled_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "dex_file-inl.h"
30#include "dex_instruction-inl.h"
31#include "driver/compiler_driver.h"
32#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "mirror/dex_cache.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070034#include "quicken_info.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
37namespace art {
38namespace optimizer {
39
Andreas Gampe46ee31b2016-12-14 10:11:49 -080040using android::base::StringPrintf;
41
Brian Carlstrom7940e442013-07-12 13:46:57 -070042// Controls quickening activation.
43const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020044// Control check-cast elision.
45const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070046
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010047struct 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 Carlstrom7940e442013-07-12 13:46:57 -070054class DexCompiler {
55 public:
56 DexCompiler(art::CompilerDriver& compiler,
Sebastien Hertz75021222013-07-16 18:34:50 +020057 const DexCompilationUnit& unit,
58 DexToDexCompilationLevel dex_to_dex_compilation_level)
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 : driver_(compiler),
Sebastien Hertz75021222013-07-16 18:34:50 +020060 unit_(unit),
61 dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070062
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070063 ~DexCompiler() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070064
65 void Compile();
66
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010067 const std::vector<QuickenedInfo>& GetQuickenedInfo() const {
68 return quickened_info_;
69 }
70
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 private:
72 const DexFile& GetDexFile() const {
73 return *unit_.GetDexFile();
74 }
75
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 // 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 Hertz543959c2013-07-03 12:00:19 +020080 // 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 Carlstrom7940e442013-07-12 13:46:57 -070085 // 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 Fuller0e844392016-09-08 13:43:31 +010096 // Executable can be found. Therefore, this does not involve any resolution
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 // 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 Hertz75021222013-07-16 18:34:50 +0200105 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100107 // 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 Carlstrom7940e442013-07-12 13:46:57 -0700112 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
113};
114
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115void DexCompiler::Compile() {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700116 DCHECK_EQ(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kOptimize);
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800117 IterationRange<DexInstructionIterator> instructions = unit_.GetCodeItem()->Instructions();
118 for (DexInstructionIterator it = instructions.begin(); it != instructions.end(); ++it) {
119 const uint32_t dex_pc = it.DexPc();
120 Instruction* inst = const_cast<Instruction*>(&it.Inst());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 switch (inst->Opcode()) {
122 case Instruction::RETURN_VOID:
123 CompileReturnVoid(inst, dex_pc);
124 break;
125
Sebastien Hertz543959c2013-07-03 12:00:19 +0200126 case Instruction::CHECK_CAST:
127 inst = CompileCheckCast(inst, dex_pc);
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700128 if (inst->Opcode() == Instruction::NOP) {
129 // We turned the CHECK_CAST into two NOPs, avoid visiting the second NOP twice since this
130 // would add 2 quickening info entries.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800131 ++it;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700132 }
Sebastien Hertz543959c2013-07-03 12:00:19 +0200133 break;
134
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 case Instruction::IGET:
136 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
137 break;
138
139 case Instruction::IGET_WIDE:
140 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
141 break;
142
143 case Instruction::IGET_OBJECT:
144 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
145 break;
146
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800147 case Instruction::IGET_BOOLEAN:
148 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
149 break;
150
151 case Instruction::IGET_BYTE:
152 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
153 break;
154
155 case Instruction::IGET_CHAR:
156 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
157 break;
158
159 case Instruction::IGET_SHORT:
160 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
161 break;
162
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 case Instruction::IPUT:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
165 break;
166
Fred Shih37f05ef2014-07-16 18:38:08 -0700167 case Instruction::IPUT_BOOLEAN:
168 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
169 break;
170
171 case Instruction::IPUT_BYTE:
172 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
173 break;
174
175 case Instruction::IPUT_CHAR:
176 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
177 break;
178
179 case Instruction::IPUT_SHORT:
180 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
181 break;
182
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 case Instruction::IPUT_WIDE:
184 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
185 break;
186
187 case Instruction::IPUT_OBJECT:
188 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
189 break;
190
191 case Instruction::INVOKE_VIRTUAL:
192 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
193 break;
194
195 case Instruction::INVOKE_VIRTUAL_RANGE:
196 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
197 break;
198
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700199 case Instruction::NOP:
200 // We need to differentiate between check cast inserted NOP and normal NOP, put an invalid
201 // index in the map for normal nops. This should be rare in real code.
202 quickened_info_.push_back(QuickenedInfo(dex_pc, DexFile::kDexNoIndex16));
203 break;
204
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 default:
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700206 DCHECK(!inst->IsQuickened());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 // Nothing to do.
208 break;
209 }
210 }
211}
212
213void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700214 DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
215 if (unit_.IsConstructor()) {
216 // Are we compiling a non clinit constructor which needs a barrier ?
217 if (!unit_.IsStatic() &&
218 driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
219 unit_.GetClassDefIndex())) {
220 return;
221 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700223 // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200224 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700225 << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
Sebastien Hertz543959c2013-07-03 12:00:19 +0200226 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700227 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700228 inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229}
230
Sebastien Hertz543959c2013-07-03 12:00:19 +0200231Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700232 if (!kEnableCheckCastEllision) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200233 return inst;
234 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000235 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200236 return inst;
237 }
238 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
239 // units and a "nop" instruction size is 1 code unit, we need to replace it by
240 // 2 consecutive NOP instructions.
241 // Because the caller loops over instructions by calling Instruction::Next onto
242 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
243 // its next instruction is the former check-cast's next instruction.
244 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
245 << " by replacing it with 2 NOPs at dex pc "
246 << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700247 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray01b70e82016-11-17 10:58:36 +0000248 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c()));
249 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c()));
Sebastien Hertz543959c2013-07-03 12:00:19 +0200250 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200251 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200253 // Get to next instruction which is the second half of check-cast and replace
254 // it by a NOP.
255 inst = const_cast<Instruction*>(inst->Next());
256 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700257 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200258 return inst;
259}
260
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
262 uint32_t dex_pc,
263 Instruction::Code new_opcode,
264 bool is_put) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700265 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 return;
267 }
268 uint32_t field_idx = inst->VRegC_22c();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000269 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700271 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
272 &field_offset, &is_volatile);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800273 if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200274 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
275 << " to " << Instruction::Name(new_opcode)
276 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000277 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200278 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700279 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 inst->SetOpcode(new_opcode);
282 // Replace field index by field offset.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000283 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100284 quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 }
286}
287
Mathieu Chartier091d2382015-03-06 10:59:06 -0800288void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
289 Instruction::Code new_opcode, bool is_range) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700290 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 return;
292 }
293 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100294 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100295
296 ClassLinker* class_linker = unit_.GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100297 ArtMethod* resolved_method =
298 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
299 GetDexFile(),
300 method_idx,
301 unit_.GetDexCache(),
302 unit_.GetClassLoader(),
303 /* referrer */ nullptr,
304 kVirtual);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100305
306 if (UNLIKELY(resolved_method == nullptr)) {
307 // Clean up any exception left by type resolution.
308 soa.Self()->ClearException();
309 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100311
312 uint32_t vtable_idx = resolved_method->GetMethodIndex();
313 DCHECK(IsUint<16>(vtable_idx));
314 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
David Sehr709b0702016-10-13 09:12:37 -0700315 << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")"
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100316 << " 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 Sehr709b0702016-10-13 09:12:37 -0700320 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100321 // 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 Carlstrom7940e442013-07-12 13:46:57 -0700330}
331
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700332CompiledMethod* ArtCompileDEX(
333 CompilerDriver* driver,
334 const DexFile::CodeItem* code_item,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100335 uint32_t access_flags,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700336 InvokeType invoke_type ATTRIBUTE_UNUSED,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100337 uint16_t class_def_idx,
338 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000339 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700340 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 Chartier736b5602015-09-02 14:54:11 -0700344 ScopedObjectAccess soa(Thread::Current());
345 StackHandleScope<1> hs(soa.Self());
346 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markodf739842016-03-23 16:59:07 +0000347 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 Gampe5eb0d382015-07-23 01:19:26 -0700357 art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level);
Sebastien Hertz75021222013-07-16 18:34:50 +0200358 dex_compiler.Compile();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100359 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 Chartierde4b08f2017-07-10 14:13:41 -0700365 if (kIsDebugBuild) {
366 // Double check that the counts line up with the size of the quicken info.
367 size_t quicken_count = 0;
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800368 for (const DexInstructionPcPair& pair : code_item->Instructions()) {
369 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700370 ++quicken_count;
371 }
372 }
373 CHECK_EQ(quicken_count, dex_compiler.GetQuickenedInfo().size());
374 }
375 std::vector<uint8_t> quicken_data;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100376 for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700377 // 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 Chartier1d2d4ff2017-09-23 16:11:06 -0700380 const Instruction& inst = code_item->InstructionAt(info.dex_pc);
381 CHECK(QuickenInfoTable::NeedsIndexForInstruction(&inst)) << inst.Opcode();
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700382 // 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 Geoffrayc04c8002015-07-14 11:37:54 +0100385 }
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700386 InstructionSet instruction_set = driver->GetInstructionSet();
Vladimir Marko33bff252017-11-01 14:35:42 +0000387 if (instruction_set == InstructionSet::kThumb2) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100388 // Don't use the thumb2 instruction set to avoid the one off code delta.
Vladimir Marko33bff252017-11-01 14:35:42 +0000389 instruction_set = InstructionSet::kArm;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100390 }
391 return CompiledMethod::SwapAllocCompiledMethod(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700392 driver,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100393 instruction_set,
394 ArrayRef<const uint8_t>(), // no code
395 0,
396 0,
397 0,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700398 ArrayRef<const uint8_t>(), // method_info
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700399 ArrayRef<const uint8_t>(quicken_data), // vmap_table
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100400 ArrayRef<const uint8_t>(), // cfi data
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100401 ArrayRef<const linker::LinkerPatch>());
Sebastien Hertz75021222013-07-16 18:34:50 +0200402 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100403 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100405
406} // namespace optimizer
407
408} // namespace art