blob: 52cb217980d7417e2f5e3627fd2624f635385a78 [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"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file-inl.h"
30#include "dex/dex_instruction-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#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 Chartier73f21d42018-01-02 14:26:50 -0800117 IterationRange<DexInstructionIterator> instructions(unit_.GetCodeItemAccessor().begin(),
118 unit_.GetCodeItemAccessor().end());
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800119 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 Carlstrom7940e442013-07-12 13:46:57 -0700122 switch (inst->Opcode()) {
123 case Instruction::RETURN_VOID:
124 CompileReturnVoid(inst, dex_pc);
125 break;
126
Sebastien Hertz543959c2013-07-03 12:00:19 +0200127 case Instruction::CHECK_CAST:
128 inst = CompileCheckCast(inst, dex_pc);
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700129 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 Chartier0021feb2017-11-07 00:08:52 -0800132 ++it;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700133 }
Sebastien Hertz543959c2013-07-03 12:00:19 +0200134 break;
135
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 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 Chartierffc605c2014-12-10 10:35:44 -0800148 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 Carlstrom7940e442013-07-12 13:46:57 -0700164 case Instruction::IPUT:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
166 break;
167
Fred Shih37f05ef2014-07-16 18:38:08 -0700168 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 Carlstrom7940e442013-07-12 13:46:57 -0700184 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 Chartierde4b08f2017-07-10 14:13:41 -0700200 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 Carlstrom7940e442013-07-12 13:46:57 -0700206 default:
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700207 DCHECK(!inst->IsQuickened());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 // Nothing to do.
209 break;
210 }
211 }
212}
213
214void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700215 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 Carlstrom7940e442013-07-12 13:46:57 -0700223 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700224 // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200225 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700226 << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
Sebastien Hertz543959c2013-07-03 12:00:19 +0200227 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700228 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700229 inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230}
231
Sebastien Hertz543959c2013-07-03 12:00:19 +0200232Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700233 if (!kEnableCheckCastEllision) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200234 return inst;
235 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000236 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200237 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 Sehr709b0702016-10-13 09:12:37 -0700248 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray01b70e82016-11-17 10:58:36 +0000249 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c()));
250 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c()));
Sebastien Hertz543959c2013-07-03 12:00:19 +0200251 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200252 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700253 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200254 // 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 Carlstrom7934ac22013-07-26 10:54:15 -0700258 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200259 return inst;
260}
261
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
263 uint32_t dex_pc,
264 Instruction::Code new_opcode,
265 bool is_put) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700266 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 return;
268 }
269 uint32_t field_idx = inst->VRegC_22c();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000270 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700272 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
273 &field_offset, &is_volatile);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800274 if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200275 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
276 << " to " << Instruction::Name(new_opcode)
277 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000278 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200279 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700280 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 inst->SetOpcode(new_opcode);
283 // Replace field index by field offset.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000284 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100285 quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 }
287}
288
Mathieu Chartier091d2382015-03-06 10:59:06 -0800289void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
290 Instruction::Code new_opcode, bool is_range) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700291 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 return;
293 }
294 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100295 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100296
297 ClassLinker* class_linker = unit_.GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100298 ArtMethod* resolved_method =
299 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100300 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 Chartier73f21d42018-01-02 14:26:50 -0800368 for (const DexInstructionPcPair& pair : unit.GetCodeItemAccessor()) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800369 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 Chartier73f21d42018-01-02 14:26:50 -0800380 const Instruction& inst = unit.GetCodeItemAccessor().InstructionAt(info.dex_pc);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -0700381 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