blob: 4836041ce09f2a800b1f7c3c9ca018c1de409ab2 [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
93 // AbstractMethod can be found. Therefore, this does not involve any resolution
94 // 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 "
215 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), 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 "
235 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
236 // 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 "
265 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), 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();
280 MethodReference target_method(&GetDexFile(), method_idx);
281 InvokeType invoke_type = kVirtual;
282 InvokeType original_invoke_type = invoke_type;
283 int vtable_idx;
284 uintptr_t direct_code;
285 uintptr_t direct_method;
Sebastien Hertz1e54d682013-09-06 14:52:10 +0200286 // TODO: support devirtualization.
287 const bool kEnableDevirtualization = false;
Ian Rogers65ec92c2013-09-06 10:49:58 -0700288 bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc,
289 false, kEnableDevirtualization,
290 &invoke_type,
291 &target_method, &vtable_idx,
292 &direct_code, &direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 if (fast_path && original_invoke_type == invoke_type) {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800294 if (vtable_idx >= 0 && IsUint<16>(vtable_idx)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200295 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
296 << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")"
297 << " to " << Instruction::Name(new_opcode)
298 << " by replacing method index " << method_idx
299 << " by vtable index " << vtable_idx
300 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
301 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 inst->SetOpcode(new_opcode);
304 // Replace method index by vtable index.
305 if (is_range) {
306 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
307 } else {
308 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
309 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100310 quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
312 }
313}
314
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700315CompiledMethod* ArtCompileDEX(
316 CompilerDriver* driver,
317 const DexFile::CodeItem* code_item,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100318 uint32_t access_flags,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700319 InvokeType invoke_type ATTRIBUTE_UNUSED,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100320 uint16_t class_def_idx,
321 uint32_t method_idx,
322 jobject class_loader,
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700323 const DexFile& dex_file,
324 DexToDexCompilationLevel dex_to_dex_compilation_level) {
325 DCHECK(driver != nullptr);
326 if (dex_to_dex_compilation_level != DexToDexCompilationLevel::kDontDexToDexCompile) {
Mathieu Chartier736b5602015-09-02 14:54:11 -0700327 ScopedObjectAccess soa(Thread::Current());
328 StackHandleScope<1> hs(soa.Self());
329 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
330 art::DexCompilationUnit unit(nullptr, class_loader, class_linker,
Vladimir Marko2730db02014-01-27 11:15:17 +0000331 dex_file, code_item, class_def_idx, method_idx, access_flags,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700332 driver->GetVerifiedMethod(&dex_file, method_idx),
333 hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)));
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700334 art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level);
Sebastien Hertz75021222013-07-16 18:34:50 +0200335 dex_compiler.Compile();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100336 if (dex_compiler.GetQuickenedInfo().empty()) {
337 // No need to create a CompiledMethod if there are no quickened opcodes.
338 return nullptr;
339 }
340
341 // Create a `CompiledMethod`, with the quickened information in the vmap table.
Vladimir Markof9f64412015-09-02 14:05:49 +0100342 Leb128EncodingVector<> builder;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100343 for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) {
344 builder.PushBackUnsigned(info.dex_pc);
345 builder.PushBackUnsigned(info.dex_member_index);
346 }
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700347 InstructionSet instruction_set = driver->GetInstructionSet();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100348 if (instruction_set == kThumb2) {
349 // Don't use the thumb2 instruction set to avoid the one off code delta.
350 instruction_set = kArm;
351 }
352 return CompiledMethod::SwapAllocCompiledMethod(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700353 driver,
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100354 instruction_set,
355 ArrayRef<const uint8_t>(), // no code
356 0,
357 0,
358 0,
Vladimir Marko35831e82015-09-11 11:59:18 +0100359 ArrayRef<const SrcMapElem>(), // src_mapping_table
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100360 ArrayRef<const uint8_t>(), // mapping_table
361 ArrayRef<const uint8_t>(builder.GetData()), // vmap_table
362 ArrayRef<const uint8_t>(), // gc_map
363 ArrayRef<const uint8_t>(), // cfi data
364 ArrayRef<const LinkerPatch>());
Sebastien Hertz75021222013-07-16 18:34:50 +0200365 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100366 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367}
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100368
369} // namespace optimizer
370
371} // namespace art