blob: c124ef5ddeb9c2aa3f4fdb30bca623183f639b84 [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"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010027#include "compiled_method.h"
David Sehr312f3b22018-03-19 08:39:26 -070028#include "dex/bytecode_utils.h"
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -070029#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file-inl.h"
31#include "dex/dex_instruction-inl.h"
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080032#include "dex_to_dex_decompiler.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "driver/compiler_driver.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010034#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "mirror/dex_cache.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070037#include "quicken_info.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070038#include "thread-current-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
40namespace art {
41namespace optimizer {
42
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043using android::base::StringPrintf;
44
Brian Carlstrom7940e442013-07-12 13:46:57 -070045// Controls quickening activation.
46const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020047// Control check-cast elision.
48const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070049
Mathieu Chartier279e3a32018-01-24 18:17:55 -080050// Holds the state for compiling a single method.
51struct DexToDexCompiler::CompilationState {
52 struct QuickenedInfo {
53 QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {}
54
55 uint32_t dex_pc;
56 uint16_t dex_member_index;
57 };
58
59 CompilationState(DexToDexCompiler* compiler,
60 const DexCompilationUnit& unit,
61 const CompilationLevel compilation_level,
62 const std::vector<uint8_t>* quicken_data);
63
64 const std::vector<QuickenedInfo>& GetQuickenedInfo() const {
65 return quickened_info_;
66 }
67
68 // Returns the quickening info, or an empty array if it was not quickened.
69 // If already_quickened is true, then don't change anything but still return what the quicken
70 // data would have been.
71 std::vector<uint8_t> Compile();
72
73 const DexFile& GetDexFile() const;
74
75 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
76 // a barrier is required.
77 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
78
79 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
80 // this case, returns the second NOP instruction pointer. Otherwise, returns
81 // the given "inst".
82 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
83
84 // Compiles a field access into a quick field access.
85 // The field index is replaced by an offset within an Object where we can read
86 // from / write to this field. Therefore, this does not involve any resolution
87 // at runtime.
88 // Since the field index is encoded with 16 bits, we can replace it only if the
89 // field offset can be encoded with 16 bits too.
90 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
91 Instruction::Code new_opcode, bool is_put);
92
93 // Compiles a virtual method invocation into a quick virtual method invocation.
94 // The method index is replaced by the vtable index where the corresponding
95 // executable can be found. Therefore, this does not involve any resolution
96 // at runtime.
97 // Since the method index is encoded with 16 bits, we can replace it only if the
98 // vtable index can be encoded with 16 bits too.
99 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
100 Instruction::Code new_opcode, bool is_range);
101
102 // Return the next index.
103 uint16_t NextIndex();
104
105 // Returns the dequickened index if an instruction is quickened, otherwise return index.
106 uint16_t GetIndexForInstruction(const Instruction* inst, uint32_t index);
107
108 DexToDexCompiler* const compiler_;
109 CompilerDriver& driver_;
110 const DexCompilationUnit& unit_;
111 const CompilationLevel compilation_level_;
112
113 // Filled by the compiler when quickening, in order to encode that information
114 // in the .oat file. The runtime will use that information to get to the original
115 // opcodes.
116 std::vector<QuickenedInfo> quickened_info_;
117
118 // True if we optimized a return void to a return void no barrier.
119 bool optimized_return_void_ = false;
120
121 // If the code item was already quickened previously.
122 const bool already_quickened_;
123 const QuickenInfoTable existing_quicken_info_;
124 uint32_t quicken_index_ = 0u;
125
126 DISALLOW_COPY_AND_ASSIGN(CompilationState);
127};
128
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800129DexToDexCompiler::DexToDexCompiler(CompilerDriver* driver)
130 : driver_(driver),
131 lock_("Quicken lock", kDexToDexCompilerLock) {
132 DCHECK(driver != nullptr);
133}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100134
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800135void DexToDexCompiler::ClearState() {
136 MutexLock lock(Thread::Current(), lock_);
137 active_dex_file_ = nullptr;
138 active_bit_vector_ = nullptr;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800139 should_quicken_.clear();
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800140 shared_code_item_quicken_info_.clear();
141}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100142
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800143size_t DexToDexCompiler::NumCodeItemsToQuicken(Thread* self) const {
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800144 MutexLock lock(self, lock_);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800145 return num_code_items_;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800146}
147
148BitVector* DexToDexCompiler::GetOrAddBitVectorForDex(const DexFile* dex_file) {
149 if (active_dex_file_ != dex_file) {
150 active_dex_file_ = dex_file;
151 auto inserted = should_quicken_.emplace(dex_file,
152 BitVector(dex_file->NumMethodIds(),
153 /*expandable*/ false,
154 Allocator::GetMallocAllocator()));
155 active_bit_vector_ = &inserted.first->second;
156 }
157 return active_bit_vector_;
158}
159
160void DexToDexCompiler::MarkForCompilation(Thread* self,
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800161 const MethodReference& method_ref) {
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800162 MutexLock lock(self, lock_);
163 BitVector* const bitmap = GetOrAddBitVectorForDex(method_ref.dex_file);
164 DCHECK(bitmap != nullptr);
165 DCHECK(!bitmap->IsBitSet(method_ref.index));
166 bitmap->SetBit(method_ref.index);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800167 ++num_code_items_;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800168}
169
170DexToDexCompiler::CompilationState::CompilationState(DexToDexCompiler* compiler,
171 const DexCompilationUnit& unit,
172 const CompilationLevel compilation_level,
173 const std::vector<uint8_t>* quicken_data)
174 : compiler_(compiler),
175 driver_(*compiler->GetDriver()),
Sebastien Hertz75021222013-07-16 18:34:50 +0200176 unit_(unit),
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800177 compilation_level_(compilation_level),
178 already_quickened_(quicken_data != nullptr),
179 existing_quicken_info_(already_quickened_
180 ? ArrayRef<const uint8_t>(*quicken_data) : ArrayRef<const uint8_t>()) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800182uint16_t DexToDexCompiler::CompilationState::NextIndex() {
183 DCHECK(already_quickened_);
184 if (kIsDebugBuild && quicken_index_ >= existing_quicken_info_.NumIndices()) {
185 for (const DexInstructionPcPair& pair : unit_.GetCodeItemAccessor()) {
186 LOG(ERROR) << pair->DumpString(nullptr);
187 }
188 LOG(FATAL) << "Mismatched number of quicken slots.";
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100189 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800190 const uint16_t ret = existing_quicken_info_.GetData(quicken_index_);
191 quicken_index_++;
192 return ret;
193}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100194
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800195uint16_t DexToDexCompiler::CompilationState::GetIndexForInstruction(const Instruction* inst,
196 uint32_t index) {
197 if (UNLIKELY(already_quickened_)) {
198 return inst->IsQuickened() ? NextIndex() : index;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800200 DCHECK(!inst->IsQuickened());
201 return index;
202}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800204bool DexToDexCompiler::ShouldCompileMethod(const MethodReference& ref) {
205 // TODO: It's probably safe to avoid the lock here if the active_dex_file_ matches since we only
206 // only call ShouldCompileMethod on one dex at a time.
207 MutexLock lock(Thread::Current(), lock_);
208 return GetOrAddBitVectorForDex(ref.dex_file)->IsBitSet(ref.index);
209}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800211std::vector<uint8_t> DexToDexCompiler::CompilationState::Compile() {
212 DCHECK_EQ(compilation_level_, CompilationLevel::kOptimize);
213 const CodeItemDataAccessor& instructions = unit_.GetCodeItemAccessor();
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800214 for (DexInstructionIterator it = instructions.begin(); it != instructions.end(); ++it) {
215 const uint32_t dex_pc = it.DexPc();
216 Instruction* inst = const_cast<Instruction*>(&it.Inst());
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800217
218 if (!already_quickened_) {
219 DCHECK(!inst->IsQuickened());
220 }
221
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 switch (inst->Opcode()) {
223 case Instruction::RETURN_VOID:
224 CompileReturnVoid(inst, dex_pc);
225 break;
226
Sebastien Hertz543959c2013-07-03 12:00:19 +0200227 case Instruction::CHECK_CAST:
228 inst = CompileCheckCast(inst, dex_pc);
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700229 if (inst->Opcode() == Instruction::NOP) {
230 // We turned the CHECK_CAST into two NOPs, avoid visiting the second NOP twice since this
231 // would add 2 quickening info entries.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800232 ++it;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700233 }
Sebastien Hertz543959c2013-07-03 12:00:19 +0200234 break;
235
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 case Instruction::IGET:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800237 case Instruction::IGET_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
239 break;
240
241 case Instruction::IGET_WIDE:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800242 case Instruction::IGET_WIDE_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
244 break;
245
246 case Instruction::IGET_OBJECT:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800247 case Instruction::IGET_OBJECT_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
249 break;
250
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800251 case Instruction::IGET_BOOLEAN:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800252 case Instruction::IGET_BOOLEAN_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800253 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
254 break;
255
256 case Instruction::IGET_BYTE:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800257 case Instruction::IGET_BYTE_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800258 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
259 break;
260
261 case Instruction::IGET_CHAR:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800262 case Instruction::IGET_CHAR_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800263 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
264 break;
265
266 case Instruction::IGET_SHORT:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800267 case Instruction::IGET_SHORT_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800268 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
269 break;
270
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 case Instruction::IPUT:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800272 case Instruction::IPUT_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
274 break;
275
Fred Shih37f05ef2014-07-16 18:38:08 -0700276 case Instruction::IPUT_BOOLEAN:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800277 case Instruction::IPUT_BOOLEAN_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700278 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
279 break;
280
281 case Instruction::IPUT_BYTE:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800282 case Instruction::IPUT_BYTE_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700283 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
284 break;
285
286 case Instruction::IPUT_CHAR:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800287 case Instruction::IPUT_CHAR_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700288 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
289 break;
290
291 case Instruction::IPUT_SHORT:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800292 case Instruction::IPUT_SHORT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700293 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
294 break;
295
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 case Instruction::IPUT_WIDE:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800297 case Instruction::IPUT_WIDE_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
299 break;
300
301 case Instruction::IPUT_OBJECT:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800302 case Instruction::IPUT_OBJECT_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
304 break;
305
306 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800307 case Instruction::INVOKE_VIRTUAL_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
309 break;
310
311 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800312 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
314 break;
315
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700316 case Instruction::NOP:
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800317 if (already_quickened_) {
318 const uint16_t reference_index = NextIndex();
319 quickened_info_.push_back(QuickenedInfo(dex_pc, reference_index));
320 if (reference_index == DexFile::kDexNoIndex16) {
321 // This means it was a normal nop and not a check-cast.
322 break;
323 }
324 const uint16_t type_index = NextIndex();
325 if (driver_.IsSafeCast(&unit_, dex_pc)) {
326 quickened_info_.push_back(QuickenedInfo(dex_pc, type_index));
327 }
328 ++it;
329 } else {
330 // We need to differentiate between check cast inserted NOP and normal NOP, put an invalid
331 // index in the map for normal nops. This should be rare in real code.
332 quickened_info_.push_back(QuickenedInfo(dex_pc, DexFile::kDexNoIndex16));
333 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700334 break;
335
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 default:
337 // Nothing to do.
338 break;
339 }
340 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800341
342 if (already_quickened_) {
343 DCHECK_EQ(quicken_index_, existing_quicken_info_.NumIndices());
344 }
345
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800346 // Even if there are no indicies, generate an empty quicken info so that we know the method was
347 // quickened.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800348
349 std::vector<uint8_t> quicken_data;
350 if (kIsDebugBuild) {
351 // Double check that the counts line up with the size of the quicken info.
352 size_t quicken_count = 0;
353 for (const DexInstructionPcPair& pair : instructions) {
354 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
355 ++quicken_count;
356 }
357 }
358 CHECK_EQ(quicken_count, GetQuickenedInfo().size());
359 }
360
361 QuickenInfoTable::Builder builder(&quicken_data, GetQuickenedInfo().size());
362 // Length is encoded by the constructor.
363 for (const CompilationState::QuickenedInfo& info : GetQuickenedInfo()) {
364 // Dex pc is not serialized, only used for checking the instructions. Since we access the
365 // array based on the index of the quickened instruction, the indexes must line up perfectly.
366 // The reader side uses the NeedsIndexForInstruction function too.
367 const Instruction& inst = instructions.InstructionAt(info.dex_pc);
368 CHECK(QuickenInfoTable::NeedsIndexForInstruction(&inst)) << inst.Opcode();
369 builder.AddIndex(info.dex_member_index);
370 }
371 DCHECK(!quicken_data.empty());
372 return quicken_data;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373}
374
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800375void DexToDexCompiler::CompilationState::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700376 DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
377 if (unit_.IsConstructor()) {
378 // Are we compiling a non clinit constructor which needs a barrier ?
Vladimir Markoc1c34522018-10-31 13:56:49 +0000379 if (!unit_.IsStatic() && unit_.RequiresConstructorBarrier()) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700380 return;
381 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700383 // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200384 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700385 << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
Sebastien Hertz543959c2013-07-03 12:00:19 +0200386 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700387 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700388 inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800389 optimized_return_void_ = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390}
391
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800392Instruction* DexToDexCompiler::CompilationState::CompileCheckCast(Instruction* inst,
393 uint32_t dex_pc) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700394 if (!kEnableCheckCastEllision) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200395 return inst;
396 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000397 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200398 return inst;
399 }
400 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
401 // units and a "nop" instruction size is 1 code unit, we need to replace it by
402 // 2 consecutive NOP instructions.
403 // Because the caller loops over instructions by calling Instruction::Next onto
404 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
405 // its next instruction is the former check-cast's next instruction.
406 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
407 << " by replacing it with 2 NOPs at dex pc "
408 << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700409 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800410 if (!already_quickened_) {
411 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c()));
412 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c()));
413
414 // We are modifying 4 consecutive bytes.
415 inst->SetOpcode(Instruction::NOP);
416 inst->SetVRegA_10x(0u); // keep compliant with verifier.
417 // Get to next instruction which is the second half of check-cast and replace
418 // it by a NOP.
419 inst = const_cast<Instruction*>(inst->Next());
420 inst->SetOpcode(Instruction::NOP);
421 inst->SetVRegA_10x(0u); // keep compliant with verifier.
422 }
Sebastien Hertz543959c2013-07-03 12:00:19 +0200423 return inst;
424}
425
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800426void DexToDexCompiler::CompilationState::CompileInstanceFieldAccess(Instruction* inst,
427 uint32_t dex_pc,
428 Instruction::Code new_opcode,
429 bool is_put) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700430 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 return;
432 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800433 uint32_t field_idx = GetIndexForInstruction(inst, inst->VRegC_22c());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000434 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700436 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
437 &field_offset, &is_volatile);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800438 if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200439 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
440 << " to " << Instruction::Name(new_opcode)
441 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000442 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200443 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700444 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800445 if (!already_quickened_) {
446 // We are modifying 4 consecutive bytes.
447 inst->SetOpcode(new_opcode);
448 // Replace field index by field offset.
449 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
450 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100451 quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 }
453}
454
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800455const DexFile& DexToDexCompiler::CompilationState::GetDexFile() const {
456 return *unit_.GetDexFile();
457}
458
459void DexToDexCompiler::CompilationState::CompileInvokeVirtual(Instruction* inst,
460 uint32_t dex_pc,
461 Instruction::Code new_opcode,
462 bool is_range) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700463 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 return;
465 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800466 uint32_t method_idx = GetIndexForInstruction(inst,
467 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100468 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100469
470 ClassLinker* class_linker = unit_.GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100471 ArtMethod* resolved_method =
472 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100473 method_idx,
474 unit_.GetDexCache(),
475 unit_.GetClassLoader(),
476 /* referrer */ nullptr,
477 kVirtual);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100478
479 if (UNLIKELY(resolved_method == nullptr)) {
480 // Clean up any exception left by type resolution.
481 soa.Self()->ClearException();
482 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483 }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100484
485 uint32_t vtable_idx = resolved_method->GetMethodIndex();
486 DCHECK(IsUint<16>(vtable_idx));
487 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
David Sehr709b0702016-10-13 09:12:37 -0700488 << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")"
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100489 << " to " << Instruction::Name(new_opcode)
490 << " by replacing method index " << method_idx
491 << " by vtable index " << vtable_idx
492 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700493 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800494 if (!already_quickened_) {
495 // We are modifying 4 consecutive bytes.
496 inst->SetOpcode(new_opcode);
497 // Replace method index by vtable index.
498 if (is_range) {
499 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
500 } else {
501 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
502 }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100503 }
504 quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505}
506
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800507CompiledMethod* DexToDexCompiler::CompileMethod(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700508 const DexFile::CodeItem* code_item,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100509 uint32_t access_flags,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700510 InvokeType invoke_type ATTRIBUTE_UNUSED,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100511 uint16_t class_def_idx,
512 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000513 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700514 const DexFile& dex_file,
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800515 CompilationLevel compilation_level) {
516 if (compilation_level == CompilationLevel::kDontDexToDexCompile) {
517 return nullptr;
518 }
519
520 ScopedObjectAccess soa(Thread::Current());
521 StackHandleScope<1> hs(soa.Self());
522 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
523 art::DexCompilationUnit unit(
524 class_loader,
525 class_linker,
526 dex_file,
527 code_item,
528 class_def_idx,
529 method_idx,
530 access_flags,
Vladimir Marko2afaff72018-11-30 17:01:50 +0000531 driver_->GetCompilerOptions().GetVerifiedMethod(&dex_file, method_idx),
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800532 hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)));
533
534 std::vector<uint8_t> quicken_data;
535 // If the code item is shared with multiple different method ids, make sure that we quicken only
536 // once and verify that all the dequicken maps match.
537 if (UNLIKELY(shared_code_items_.find(code_item) != shared_code_items_.end())) {
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800538 // Avoid quickening the shared code items for now because the existing conflict detection logic
539 // does not currently handle cases where the code item is quickened in one place but
540 // compiled in another.
541 static constexpr bool kAvoidQuickeningSharedCodeItems = true;
542 if (kAvoidQuickeningSharedCodeItems) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100543 return nullptr;
544 }
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800545 // For shared code items, use a lock to prevent races.
546 MutexLock mu(soa.Self(), lock_);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800547 auto existing = shared_code_item_quicken_info_.find(code_item);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800548 QuickenState* existing_data = nullptr;
549 std::vector<uint8_t>* existing_quicken_data = nullptr;
550 if (existing != shared_code_item_quicken_info_.end()) {
551 existing_data = &existing->second;
552 if (existing_data->conflict_) {
553 return nullptr;
554 }
555 existing_quicken_data = &existing_data->quicken_data_;
556 }
557 bool optimized_return_void;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800558 {
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800559 CompilationState state(this, unit, compilation_level, existing_quicken_data);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800560 quicken_data = state.Compile();
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800561 optimized_return_void = state.optimized_return_void_;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800562 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100563
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800564 // Already quickened, check that the data matches what was previously seen.
565 MethodReference method_ref(&dex_file, method_idx);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800566 if (existing_data != nullptr) {
567 if (*existing_quicken_data != quicken_data ||
568 existing_data->optimized_return_void_ != optimized_return_void) {
569 VLOG(compiler) << "Quicken data mismatch, for method "
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800570 << dex_file.PrettyMethod(method_idx);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800571 // Mark the method as a conflict to never attempt to quicken it in the future.
572 existing_data->conflict_ = true;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700573 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800574 existing_data->methods_.push_back(method_ref);
575 } else {
576 QuickenState new_state;
577 new_state.methods_.push_back(method_ref);
578 new_state.quicken_data_ = quicken_data;
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800579 new_state.optimized_return_void_ = optimized_return_void;
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800580 bool inserted = shared_code_item_quicken_info_.emplace(code_item, new_state).second;
581 CHECK(inserted) << "Failed to insert " << dex_file.PrettyMethod(method_idx);
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700582 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800583
584 // Easy sanity check is to check that the existing stuff matches by re-quickening using the
585 // newly produced quicken data.
586 // Note that this needs to be behind the lock for this case since we may unquicken in another
587 // thread.
588 if (kIsDebugBuild) {
589 CompilationState state2(this, unit, compilation_level, &quicken_data);
590 std::vector<uint8_t> new_data = state2.Compile();
591 CHECK(new_data == quicken_data) << "Mismatch producing new quicken data";
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100592 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800593 } else {
594 CompilationState state(this, unit, compilation_level, /*quicken_data*/ nullptr);
595 quicken_data = state.Compile();
596
597 // Easy sanity check is to check that the existing stuff matches by re-quickening using the
598 // newly produced quicken data.
599 if (kIsDebugBuild) {
600 CompilationState state2(this, unit, compilation_level, &quicken_data);
601 std::vector<uint8_t> new_data = state2.Compile();
602 CHECK(new_data == quicken_data) << "Mismatch producing new quicken data";
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100603 }
Sebastien Hertz75021222013-07-16 18:34:50 +0200604 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800605
606 if (quicken_data.empty()) {
607 return nullptr;
608 }
609
610 // Create a `CompiledMethod`, with the quickened information in the vmap table.
Vladimir Markoa0431112018-06-25 09:32:54 +0100611 InstructionSet instruction_set = driver_->GetCompilerOptions().GetInstructionSet();
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800612 if (instruction_set == InstructionSet::kThumb2) {
613 // Don't use the thumb2 instruction set to avoid the one off code delta.
614 instruction_set = InstructionSet::kArm;
615 }
616 CompiledMethod* ret = CompiledMethod::SwapAllocCompiledMethod(
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000617 driver_->GetCompiledMethodStorage(),
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800618 instruction_set,
619 ArrayRef<const uint8_t>(), // no code
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800620 ArrayRef<const uint8_t>(quicken_data), // vmap_table
621 ArrayRef<const uint8_t>(), // cfi data
622 ArrayRef<const linker::LinkerPatch>());
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800623 DCHECK(ret != nullptr);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800624 return ret;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100626
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800627void DexToDexCompiler::SetDexFiles(const std::vector<const DexFile*>& dex_files) {
628 // Record what code items are already seen to detect when multiple methods have the same code
629 // item.
630 std::unordered_set<const DexFile::CodeItem*> seen_code_items;
631 for (const DexFile* dex_file : dex_files) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700632 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700633 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700634 const DexFile::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800635 // Detect the shared code items.
636 if (!seen_code_items.insert(code_item).second) {
637 shared_code_items_.insert(code_item);
638 }
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700639 }
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800640 }
641 }
642 VLOG(compiler) << "Shared code items " << shared_code_items_.size();
643}
644
645void DexToDexCompiler::UnquickenConflictingMethods() {
646 MutexLock mu(Thread::Current(), lock_);
647 size_t unquicken_count = 0;
648 for (const auto& pair : shared_code_item_quicken_info_) {
649 const DexFile::CodeItem* code_item = pair.first;
650 const QuickenState& state = pair.second;
651 CHECK_GE(state.methods_.size(), 1u);
652 if (state.conflict_) {
653 // Unquicken using the existing quicken data.
654 // TODO: Do we really need to pass a dex file in?
655 optimizer::ArtDecompileDEX(*state.methods_[0].dex_file,
656 *code_item,
657 ArrayRef<const uint8_t>(state.quicken_data_),
658 /* decompile_return_instruction*/ true);
659 ++unquicken_count;
660 // Go clear the vmaps for all the methods that were already quickened to avoid writing them
661 // out during oat writing.
662 for (const MethodReference& ref : state.methods_) {
663 CompiledMethod* method = driver_->RemoveCompiledMethod(ref);
664 if (method != nullptr) {
665 // There is up to one compiled method for each method ref. Releasing it leaves the
666 // deduped data intact, this means its safe to do even when other threads might be
667 // compiling.
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000668 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(driver_->GetCompiledMethodStorage(),
669 method);
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800670 }
671 }
672 }
673 }
674}
675
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100676} // namespace optimizer
677
678} // namespace art