blob: 9c1d72b9be529efc9534152b28578bf8202d07d4 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "base/logging.h"
22#include "base/mutex.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010023#include "compiled_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex_file-inl.h"
25#include "dex_instruction-inl.h"
26#include "driver/compiler_driver.h"
27#include "driver/dex_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/class-inl.h"
29#include "mirror/dex_cache.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070030#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031
32namespace art {
33namespace optimizer {
34
35// Controls quickening activation.
36const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020037// Control check-cast elision.
38const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010040struct QuickenedInfo {
41 QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {}
42
43 uint32_t dex_pc;
44 uint16_t dex_member_index;
45};
46
Brian Carlstrom7940e442013-07-12 13:46:57 -070047class DexCompiler {
48 public:
49 DexCompiler(art::CompilerDriver& compiler,
Sebastien Hertz75021222013-07-16 18:34:50 +020050 const DexCompilationUnit& unit,
51 DexToDexCompilationLevel dex_to_dex_compilation_level)
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 : driver_(compiler),
Sebastien Hertz75021222013-07-16 18:34:50 +020053 unit_(unit),
54 dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070055
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070056 ~DexCompiler() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070057
58 void Compile();
59
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010060 const std::vector<QuickenedInfo>& GetQuickenedInfo() const {
61 return quickened_info_;
62 }
63
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 private:
65 const DexFile& GetDexFile() const {
66 return *unit_.GetDexFile();
67 }
68
Sebastien Hertz75021222013-07-16 18:34:50 +020069 bool PerformOptimizations() const {
Andreas Gampe5eb0d382015-07-23 01:19:26 -070070 return dex_to_dex_compilation_level_ >= DexToDexCompilationLevel::kOptimize;
Sebastien Hertz75021222013-07-16 18:34:50 +020071 }
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
74 // a barrier is required.
75 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
76
Sebastien Hertz543959c2013-07-03 12:00:19 +020077 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
78 // this case, returns the second NOP instruction pointer. Otherwise, returns
79 // the given "inst".
80 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
81
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 // Compiles a field access into a quick field access.
83 // The field index is replaced by an offset within an Object where we can read
84 // from / write to this field. Therefore, this does not involve any resolution
85 // at runtime.
86 // Since the field index is encoded with 16 bits, we can replace it only if the
87 // field offset can be encoded with 16 bits too.
88 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
89 Instruction::Code new_opcode, bool is_put);
90
91 // Compiles a virtual method invocation into a quick virtual method invocation.
92 // The method index is replaced by the vtable index where the corresponding
Neil Fuller0e844392016-09-08 13:43:31 +010093 // Executable can be found. Therefore, this does not involve any resolution
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 // at runtime.
95 // Since the method index is encoded with 16 bits, we can replace it only if the
96 // vtable index can be encoded with 16 bits too.
97 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
98 Instruction::Code new_opcode, bool is_range);
99
100 CompilerDriver& driver_;
101 const DexCompilationUnit& unit_;
Sebastien Hertz75021222013-07-16 18:34:50 +0200102 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100104 // Filled by the compiler when quickening, in order to encode that information
105 // in the .oat file. The runtime will use that information to get to the original
106 // opcodes.
107 std::vector<QuickenedInfo> quickened_info_;
108
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
110};
111
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112void DexCompiler::Compile() {
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700113 DCHECK_GE(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kRequired);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 const DexFile::CodeItem* code_item = unit_.GetCodeItem();
115 const uint16_t* insns = code_item->insns_;
116 const uint32_t insns_size = code_item->insns_size_in_code_units_;
117 Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
118
119 for (uint32_t dex_pc = 0; dex_pc < insns_size;
120 inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
121 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);
128 break;
129
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 case Instruction::IGET:
131 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
132 break;
133
134 case Instruction::IGET_WIDE:
135 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
136 break;
137
138 case Instruction::IGET_OBJECT:
139 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
140 break;
141
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800142 case Instruction::IGET_BOOLEAN:
143 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
144 break;
145
146 case Instruction::IGET_BYTE:
147 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
148 break;
149
150 case Instruction::IGET_CHAR:
151 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
152 break;
153
154 case Instruction::IGET_SHORT:
155 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
156 break;
157
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 case Instruction::IPUT:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
160 break;
161
Fred Shih37f05ef2014-07-16 18:38:08 -0700162 case Instruction::IPUT_BOOLEAN:
163 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
164 break;
165
166 case Instruction::IPUT_BYTE:
167 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
168 break;
169
170 case Instruction::IPUT_CHAR:
171 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
172 break;
173
174 case Instruction::IPUT_SHORT:
175 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
176 break;
177
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 case Instruction::IPUT_WIDE:
179 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
180 break;
181
182 case Instruction::IPUT_OBJECT:
183 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
184 break;
185
186 case Instruction::INVOKE_VIRTUAL:
187 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
188 break;
189
190 case Instruction::INVOKE_VIRTUAL_RANGE:
191 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
192 break;
193
194 default:
195 // Nothing to do.
196 break;
197 }
198 }
199}
200
201void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700202 DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
203 if (unit_.IsConstructor()) {
204 // Are we compiling a non clinit constructor which needs a barrier ?
205 if (!unit_.IsStatic() &&
206 driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
207 unit_.GetClassDefIndex())) {
208 return;
209 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700211 // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200212 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700213 << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
Sebastien Hertz543959c2013-07-03 12:00:19 +0200214 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700215 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700216 inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217}
218
Sebastien Hertz543959c2013-07-03 12:00:19 +0200219Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200220 if (!kEnableCheckCastEllision || !PerformOptimizations()) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200221 return inst;
222 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000223 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200224 return inst;
225 }
226 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
227 // units and a "nop" instruction size is 1 code unit, we need to replace it by
228 // 2 consecutive NOP instructions.
229 // Because the caller loops over instructions by calling Instruction::Next onto
230 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
231 // its next instruction is the former check-cast's next instruction.
232 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
233 << " by replacing it with 2 NOPs at dex pc "
234 << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700235 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Sebastien Hertz543959c2013-07-03 12:00:19 +0200236 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200237 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700238 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200239 // Get to next instruction which is the second half of check-cast and replace
240 // it by a NOP.
241 inst = const_cast<Instruction*>(inst->Next());
242 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700243 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200244 return inst;
245}
246
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
248 uint32_t dex_pc,
249 Instruction::Code new_opcode,
250 bool is_put) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200251 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 return;
253 }
254 uint32_t field_idx = inst->VRegC_22c();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000255 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700257 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
258 &field_offset, &is_volatile);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800259 if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200260 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
261 << " to " << Instruction::Name(new_opcode)
262 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000263 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200264 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700265 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 inst->SetOpcode(new_opcode);
268 // Replace field index by field offset.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000269 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100270 quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 }
272}
273
Mathieu Chartier091d2382015-03-06 10:59:06 -0800274void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
275 Instruction::Code new_opcode, bool is_range) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200276 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 return;
278 }
279 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100280 ScopedObjectAccess soa(Thread::Current());
281 StackHandleScope<1> hs(soa.Self());
282 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700283 soa.Decode<mirror::ClassLoader>(unit_.GetClassLoader())));
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100284
285 ClassLinker* class_linker = unit_.GetClassLinker();
286 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
287 GetDexFile(),
288 method_idx,
289 unit_.GetDexCache(),
290 class_loader,
291 /* referrer */ nullptr,
292 kVirtual);
293
294 if (UNLIKELY(resolved_method == nullptr)) {
295 // Clean up any exception left by type resolution.
296 soa.Self()->ClearException();
297 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100299
300 uint32_t vtable_idx = resolved_method->GetMethodIndex();
301 DCHECK(IsUint<16>(vtable_idx));
302 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
David Sehr709b0702016-10-13 09:12:37 -0700303 << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")"
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100304 << " to " << Instruction::Name(new_opcode)
305 << " by replacing method index " << method_idx
306 << " by vtable index " << vtable_idx
307 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
David Sehr709b0702016-10-13 09:12:37 -0700308 << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100309 // We are modifying 4 consecutive bytes.
310 inst->SetOpcode(new_opcode);
311 // Replace method index by vtable index.
312 if (is_range) {
313 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
314 } else {
315 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
316 }
317 quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318}
319
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700320CompiledMethod* ArtCompileDEX(
321 CompilerDriver* driver,
322 const DexFile::CodeItem* code_item,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100323 uint32_t access_flags,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700324 InvokeType invoke_type ATTRIBUTE_UNUSED,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100325 uint16_t class_def_idx,
326 uint32_t method_idx,
327 jobject class_loader,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700328 const DexFile& dex_file,
329 DexToDexCompilationLevel dex_to_dex_compilation_level) {
330 DCHECK(driver != nullptr);
331 if (dex_to_dex_compilation_level != DexToDexCompilationLevel::kDontDexToDexCompile) {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700332 ScopedObjectAccess soa(Thread::Current());
333 StackHandleScope<1> hs(soa.Self());
334 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markodf739842016-03-23 16:59:07 +0000335 art::DexCompilationUnit unit(
336 class_loader,
337 class_linker,
338 dex_file,
339 code_item,
340 class_def_idx,
341 method_idx,
342 access_flags,
343 driver->GetVerifiedMethod(&dex_file, method_idx),
344 hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)));
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700345 art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level);
Sebastien Hertz75021222013-07-16 18:34:50 +0200346 dex_compiler.Compile();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100347 if (dex_compiler.GetQuickenedInfo().empty()) {
348 // No need to create a CompiledMethod if there are no quickened opcodes.
349 return nullptr;
350 }
351
352 // Create a `CompiledMethod`, with the quickened information in the vmap table.
Vladimir Markof9f64412015-09-02 14:05:49 +0100353 Leb128EncodingVector<> builder;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100354 for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) {
355 builder.PushBackUnsigned(info.dex_pc);
356 builder.PushBackUnsigned(info.dex_member_index);
357 }
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700358 InstructionSet instruction_set = driver->GetInstructionSet();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100359 if (instruction_set == kThumb2) {
360 // Don't use the thumb2 instruction set to avoid the one off code delta.
361 instruction_set = kArm;
362 }
363 return CompiledMethod::SwapAllocCompiledMethod(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700364 driver,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100365 instruction_set,
366 ArrayRef<const uint8_t>(), // no code
367 0,
368 0,
369 0,
Vladimir Marko35831e82015-09-11 11:59:18 +0100370 ArrayRef<const SrcMapElem>(), // src_mapping_table
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100371 ArrayRef<const uint8_t>(builder.GetData()), // vmap_table
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100372 ArrayRef<const uint8_t>(), // cfi data
373 ArrayRef<const LinkerPatch>());
Sebastien Hertz75021222013-07-16 18:34:50 +0200374 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100375 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100377
378} // namespace optimizer
379
380} // namespace art