blob: 2db99cda3e39f679141342f8b21558cd402b6469 [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 Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "base/logging.h"
24#include "base/mutex.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010025#include "compiled_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "dex_file-inl.h"
27#include "dex_instruction-inl.h"
28#include "driver/compiler_driver.h"
29#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mirror/dex_cache.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "thread-current-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
33namespace art {
34namespace optimizer {
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036using android::base::StringPrintf;
37
Brian Carlstrom7940e442013-07-12 13:46:57 -070038// Controls quickening activation.
39const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020040// Control check-cast elision.
41const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070042
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010043struct QuickenedInfo {
44 QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {}
45
46 uint32_t dex_pc;
47 uint16_t dex_member_index;
48};
49
Brian Carlstrom7940e442013-07-12 13:46:57 -070050class DexCompiler {
51 public:
52 DexCompiler(art::CompilerDriver& compiler,
Sebastien Hertz75021222013-07-16 18:34:50 +020053 const DexCompilationUnit& unit,
54 DexToDexCompilationLevel dex_to_dex_compilation_level)
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 : driver_(compiler),
Sebastien Hertz75021222013-07-16 18:34:50 +020056 unit_(unit),
57 dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070058
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070059 ~DexCompiler() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070060
61 void Compile();
62
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010063 const std::vector<QuickenedInfo>& GetQuickenedInfo() const {
64 return quickened_info_;
65 }
66
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 private:
68 const DexFile& GetDexFile() const {
69 return *unit_.GetDexFile();
70 }
71
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
73 // a barrier is required.
74 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
75
Sebastien Hertz543959c2013-07-03 12:00:19 +020076 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
77 // this case, returns the second NOP instruction pointer. Otherwise, returns
78 // the given "inst".
79 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
80
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 // Compiles a field access into a quick field access.
82 // The field index is replaced by an offset within an Object where we can read
83 // from / write to this field. Therefore, this does not involve any resolution
84 // at runtime.
85 // Since the field index is encoded with 16 bits, we can replace it only if the
86 // field offset can be encoded with 16 bits too.
87 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
88 Instruction::Code new_opcode, bool is_put);
89
90 // Compiles a virtual method invocation into a quick virtual method invocation.
91 // The method index is replaced by the vtable index where the corresponding
Neil Fuller0e844392016-09-08 13:43:31 +010092 // Executable can be found. Therefore, this does not involve any resolution
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 // at runtime.
94 // Since the method index is encoded with 16 bits, we can replace it only if the
95 // vtable index can be encoded with 16 bits too.
96 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
97 Instruction::Code new_opcode, bool is_range);
98
99 CompilerDriver& driver_;
100 const DexCompilationUnit& unit_;
Sebastien Hertz75021222013-07-16 18:34:50 +0200101 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100103 // Filled by the compiler when quickening, in order to encode that information
104 // in the .oat file. The runtime will use that information to get to the original
105 // opcodes.
106 std::vector<QuickenedInfo> quickened_info_;
107
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
109};
110
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111void DexCompiler::Compile() {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700112 DCHECK_EQ(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kOptimize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 const DexFile::CodeItem* code_item = unit_.GetCodeItem();
114 const uint16_t* insns = code_item->insns_;
115 const uint32_t insns_size = code_item->insns_size_in_code_units_;
116 Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
117
118 for (uint32_t dex_pc = 0; dex_pc < insns_size;
119 inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
120 switch (inst->Opcode()) {
121 case Instruction::RETURN_VOID:
122 CompileReturnVoid(inst, dex_pc);
123 break;
124
Sebastien Hertz543959c2013-07-03 12:00:19 +0200125 case Instruction::CHECK_CAST:
126 inst = CompileCheckCast(inst, dex_pc);
127 break;
128
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 case Instruction::IGET:
130 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
131 break;
132
133 case Instruction::IGET_WIDE:
134 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
135 break;
136
137 case Instruction::IGET_OBJECT:
138 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
139 break;
140
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800141 case Instruction::IGET_BOOLEAN:
142 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
143 break;
144
145 case Instruction::IGET_BYTE:
146 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
147 break;
148
149 case Instruction::IGET_CHAR:
150 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
151 break;
152
153 case Instruction::IGET_SHORT:
154 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
155 break;
156
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 case Instruction::IPUT:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
159 break;
160
Fred Shih37f05ef2014-07-16 18:38:08 -0700161 case Instruction::IPUT_BOOLEAN:
162 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
163 break;
164
165 case Instruction::IPUT_BYTE:
166 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
167 break;
168
169 case Instruction::IPUT_CHAR:
170 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
171 break;
172
173 case Instruction::IPUT_SHORT:
174 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
175 break;
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 case Instruction::IPUT_WIDE:
178 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
179 break;
180
181 case Instruction::IPUT_OBJECT:
182 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
183 break;
184
185 case Instruction::INVOKE_VIRTUAL:
186 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
187 break;
188
189 case Instruction::INVOKE_VIRTUAL_RANGE:
190 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
191 break;
192
193 default:
194 // Nothing to do.
195 break;
196 }
197 }
198}
199
200void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700201 DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
202 if (unit_.IsConstructor()) {
203 // Are we compiling a non clinit constructor which needs a barrier ?
204 if (!unit_.IsStatic() &&
205 driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
206 unit_.GetClassDefIndex())) {
207 return;
208 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700210 // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200211 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700212 << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
Sebastien Hertz543959c2013-07-03 12:00:19 +0200213 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700214 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700215 inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216}
217
Sebastien Hertz543959c2013-07-03 12:00:19 +0200218Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700219 if (!kEnableCheckCastEllision) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200220 return inst;
221 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000222 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200223 return inst;
224 }
225 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
226 // units and a "nop" instruction size is 1 code unit, we need to replace it by
227 // 2 consecutive NOP instructions.
228 // Because the caller loops over instructions by calling Instruction::Next onto
229 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
230 // its next instruction is the former check-cast's next instruction.
231 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
232 << " by replacing it with 2 NOPs at dex pc "
233 << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700234 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray01b70e82016-11-17 10:58:36 +0000235 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c()));
236 quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c()));
Sebastien Hertz543959c2013-07-03 12:00:19 +0200237 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200238 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700239 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200240 // Get to next instruction which is the second half of check-cast and replace
241 // it by a NOP.
242 inst = const_cast<Instruction*>(inst->Next());
243 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700244 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200245 return inst;
246}
247
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
249 uint32_t dex_pc,
250 Instruction::Code new_opcode,
251 bool is_put) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700252 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 return;
254 }
255 uint32_t field_idx = inst->VRegC_22c();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000256 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700258 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
259 &field_offset, &is_volatile);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800260 if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200261 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
262 << " to " << Instruction::Name(new_opcode)
263 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000264 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200265 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700266 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 inst->SetOpcode(new_opcode);
269 // Replace field index by field offset.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000270 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100271 quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 }
273}
274
Mathieu Chartier091d2382015-03-06 10:59:06 -0800275void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
276 Instruction::Code new_opcode, bool is_range) {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700277 if (!kEnableQuickening) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 return;
279 }
280 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100281 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100282
283 ClassLinker* class_linker = unit_.GetClassLinker();
284 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
285 GetDexFile(),
286 method_idx,
287 unit_.GetDexCache(),
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000288 unit_.GetClassLoader(),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100289 /* referrer */ nullptr,
290 kVirtual);
291
292 if (UNLIKELY(resolved_method == nullptr)) {
293 // Clean up any exception left by type resolution.
294 soa.Self()->ClearException();
295 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100297
298 uint32_t vtable_idx = resolved_method->GetMethodIndex();
299 DCHECK(IsUint<16>(vtable_idx));
300 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
David Sehr709b0702016-10-13 09:12:37 -0700301 << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")"
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100302 << " to " << Instruction::Name(new_opcode)
303 << " by replacing method index " << method_idx
304 << " by vtable index " << vtable_idx
305 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700306 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100307 // We are modifying 4 consecutive bytes.
308 inst->SetOpcode(new_opcode);
309 // Replace method index by vtable index.
310 if (is_range) {
311 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
312 } else {
313 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
314 }
315 quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316}
317
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700318CompiledMethod* ArtCompileDEX(
319 CompilerDriver* driver,
320 const DexFile::CodeItem* code_item,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100321 uint32_t access_flags,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700322 InvokeType invoke_type ATTRIBUTE_UNUSED,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100323 uint16_t class_def_idx,
324 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000325 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700326 const DexFile& dex_file,
327 DexToDexCompilationLevel dex_to_dex_compilation_level) {
328 DCHECK(driver != nullptr);
329 if (dex_to_dex_compilation_level != DexToDexCompilationLevel::kDontDexToDexCompile) {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700330 ScopedObjectAccess soa(Thread::Current());
331 StackHandleScope<1> hs(soa.Self());
332 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markodf739842016-03-23 16:59:07 +0000333 art::DexCompilationUnit unit(
334 class_loader,
335 class_linker,
336 dex_file,
337 code_item,
338 class_def_idx,
339 method_idx,
340 access_flags,
341 driver->GetVerifiedMethod(&dex_file, method_idx),
342 hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)));
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700343 art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level);
Sebastien Hertz75021222013-07-16 18:34:50 +0200344 dex_compiler.Compile();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100345 if (dex_compiler.GetQuickenedInfo().empty()) {
346 // No need to create a CompiledMethod if there are no quickened opcodes.
347 return nullptr;
348 }
349
350 // Create a `CompiledMethod`, with the quickened information in the vmap table.
Vladimir Markof9f64412015-09-02 14:05:49 +0100351 Leb128EncodingVector<> builder;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100352 for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) {
353 builder.PushBackUnsigned(info.dex_pc);
354 builder.PushBackUnsigned(info.dex_member_index);
355 }
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700356 InstructionSet instruction_set = driver->GetInstructionSet();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100357 if (instruction_set == kThumb2) {
358 // Don't use the thumb2 instruction set to avoid the one off code delta.
359 instruction_set = kArm;
360 }
361 return CompiledMethod::SwapAllocCompiledMethod(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700362 driver,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100363 instruction_set,
364 ArrayRef<const uint8_t>(), // no code
365 0,
366 0,
367 0,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700368 ArrayRef<const uint8_t>(), // method_info
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100369 ArrayRef<const uint8_t>(builder.GetData()), // vmap_table
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100370 ArrayRef<const uint8_t>(), // cfi data
371 ArrayRef<const LinkerPatch>());
Sebastien Hertz75021222013-07-16 18:34:50 +0200372 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100373 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100375
376} // namespace optimizer
377
378} // namespace art