blob: f7968c225ace88bd6546abec7891327487826b56 [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
17#include "base/logging.h"
18#include "base/mutex.h"
19#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
21#include "driver/compiler_driver.h"
22#include "driver/dex_compilation_unit.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field-inl.h"
24#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "mirror/class-inl.h"
26#include "mirror/dex_cache.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070027#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028
29namespace art {
30namespace optimizer {
31
32// Controls quickening activation.
33const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020034// Control check-cast elision.
35const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
37class DexCompiler {
38 public:
39 DexCompiler(art::CompilerDriver& compiler,
Sebastien Hertz75021222013-07-16 18:34:50 +020040 const DexCompilationUnit& unit,
41 DexToDexCompilationLevel dex_to_dex_compilation_level)
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 : driver_(compiler),
Sebastien Hertz75021222013-07-16 18:34:50 +020043 unit_(unit),
44 dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070045
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070046 ~DexCompiler() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070047
48 void Compile();
49
50 private:
51 const DexFile& GetDexFile() const {
52 return *unit_.GetDexFile();
53 }
54
Sebastien Hertz75021222013-07-16 18:34:50 +020055 bool PerformOptimizations() const {
56 return dex_to_dex_compilation_level_ >= kOptimize;
57 }
58
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
60 // a barrier is required.
61 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
62
Sebastien Hertz543959c2013-07-03 12:00:19 +020063 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
64 // this case, returns the second NOP instruction pointer. Otherwise, returns
65 // the given "inst".
66 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
67
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 // Compiles a field access into a quick field access.
69 // The field index is replaced by an offset within an Object where we can read
70 // from / write to this field. Therefore, this does not involve any resolution
71 // at runtime.
72 // Since the field index is encoded with 16 bits, we can replace it only if the
73 // field offset can be encoded with 16 bits too.
74 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
75 Instruction::Code new_opcode, bool is_put);
76
77 // Compiles a virtual method invocation into a quick virtual method invocation.
78 // The method index is replaced by the vtable index where the corresponding
79 // AbstractMethod can be found. Therefore, this does not involve any resolution
80 // at runtime.
81 // Since the method index is encoded with 16 bits, we can replace it only if the
82 // vtable index can be encoded with 16 bits too.
83 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
84 Instruction::Code new_opcode, bool is_range);
85
86 CompilerDriver& driver_;
87 const DexCompilationUnit& unit_;
Sebastien Hertz75021222013-07-16 18:34:50 +020088 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070089
90 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
91};
92
Brian Carlstrom7940e442013-07-12 13:46:57 -070093void DexCompiler::Compile() {
Sebastien Hertz75021222013-07-16 18:34:50 +020094 DCHECK_GE(dex_to_dex_compilation_level_, kRequired);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 const DexFile::CodeItem* code_item = unit_.GetCodeItem();
96 const uint16_t* insns = code_item->insns_;
97 const uint32_t insns_size = code_item->insns_size_in_code_units_;
98 Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
99
100 for (uint32_t dex_pc = 0; dex_pc < insns_size;
101 inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
102 switch (inst->Opcode()) {
103 case Instruction::RETURN_VOID:
104 CompileReturnVoid(inst, dex_pc);
105 break;
106
Sebastien Hertz543959c2013-07-03 12:00:19 +0200107 case Instruction::CHECK_CAST:
108 inst = CompileCheckCast(inst, dex_pc);
109 break;
110
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 case Instruction::IGET:
112 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
113 break;
114
115 case Instruction::IGET_WIDE:
116 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
117 break;
118
119 case Instruction::IGET_OBJECT:
120 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
121 break;
122
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800123 case Instruction::IGET_BOOLEAN:
124 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
125 break;
126
127 case Instruction::IGET_BYTE:
128 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
129 break;
130
131 case Instruction::IGET_CHAR:
132 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
133 break;
134
135 case Instruction::IGET_SHORT:
136 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
137 break;
138
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 case Instruction::IPUT:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
141 break;
142
Fred Shih37f05ef2014-07-16 18:38:08 -0700143 case Instruction::IPUT_BOOLEAN:
144 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
145 break;
146
147 case Instruction::IPUT_BYTE:
148 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
149 break;
150
151 case Instruction::IPUT_CHAR:
152 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
153 break;
154
155 case Instruction::IPUT_SHORT:
156 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
157 break;
158
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 case Instruction::IPUT_WIDE:
160 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
161 break;
162
163 case Instruction::IPUT_OBJECT:
164 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
165 break;
166
167 case Instruction::INVOKE_VIRTUAL:
168 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
169 break;
170
171 case Instruction::INVOKE_VIRTUAL_RANGE:
172 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
173 break;
174
175 default:
176 // Nothing to do.
177 break;
178 }
179 }
180}
181
182void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
183 DCHECK(inst->Opcode() == Instruction::RETURN_VOID);
Ian Rogers9fc16eb2013-07-31 14:49:16 -0700184 // Are we compiling a non-clinit constructor?
185 if (!unit_.IsConstructor() || unit_.IsStatic()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 return;
187 }
188 // Do we need a constructor barrier ?
189 if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
190 unit_.GetClassDefIndex())) {
191 return;
192 }
193 // Replace RETURN_VOID by RETURN_VOID_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200194 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
195 << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER)
196 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
197 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 inst->SetOpcode(Instruction::RETURN_VOID_BARRIER);
199}
200
Sebastien Hertz543959c2013-07-03 12:00:19 +0200201Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200202 if (!kEnableCheckCastEllision || !PerformOptimizations()) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200203 return inst;
204 }
Vladimir Marko2730db02014-01-27 11:15:17 +0000205 if (!driver_.IsSafeCast(&unit_, dex_pc)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200206 return inst;
207 }
208 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
209 // units and a "nop" instruction size is 1 code unit, we need to replace it by
210 // 2 consecutive NOP instructions.
211 // Because the caller loops over instructions by calling Instruction::Next onto
212 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
213 // its next instruction is the former check-cast's next instruction.
214 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
215 << " by replacing it with 2 NOPs at dex pc "
216 << StringPrintf("0x%x", dex_pc) << " in method "
217 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
218 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200219 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700220 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200221 // Get to next instruction which is the second half of check-cast and replace
222 // it by a NOP.
223 inst = const_cast<Instruction*>(inst->Next());
224 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700225 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200226 return inst;
227}
228
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
230 uint32_t dex_pc,
231 Instruction::Code new_opcode,
232 bool is_put) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200233 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 return;
235 }
236 uint32_t field_idx = inst->VRegC_22c();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000237 MemberOffset field_offset(0u);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700239 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
240 &field_offset, &is_volatile);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000241 if (fast_path && !is_volatile && IsUint(16, field_offset.Int32Value())) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200242 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
243 << " to " << Instruction::Name(new_opcode)
244 << " by replacing field index " << field_idx
Vladimir Markobe0e5462014-02-26 11:24:15 +0000245 << " by field offset " << field_offset.Int32Value()
Sebastien Hertz543959c2013-07-03 12:00:19 +0200246 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
247 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 inst->SetOpcode(new_opcode);
250 // Replace field index by field offset.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000251 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253}
254
255void DexCompiler::CompileInvokeVirtual(Instruction* inst,
256 uint32_t dex_pc,
257 Instruction::Code new_opcode,
258 bool is_range) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200259 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 return;
261 }
262 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
263 MethodReference target_method(&GetDexFile(), method_idx);
264 InvokeType invoke_type = kVirtual;
265 InvokeType original_invoke_type = invoke_type;
266 int vtable_idx;
267 uintptr_t direct_code;
268 uintptr_t direct_method;
Sebastien Hertz1e54d682013-09-06 14:52:10 +0200269 // TODO: support devirtualization.
270 const bool kEnableDevirtualization = false;
Ian Rogers65ec92c2013-09-06 10:49:58 -0700271 bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc,
272 false, kEnableDevirtualization,
273 &invoke_type,
274 &target_method, &vtable_idx,
275 &direct_code, &direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 if (fast_path && original_invoke_type == invoke_type) {
277 if (vtable_idx >= 0 && IsUint(16, vtable_idx)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200278 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
279 << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")"
280 << " to " << Instruction::Name(new_opcode)
281 << " by replacing method index " << method_idx
282 << " by vtable index " << vtable_idx
283 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
284 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 inst->SetOpcode(new_opcode);
287 // Replace method index by vtable index.
288 if (is_range) {
289 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
290 } else {
291 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
292 }
293 }
294 }
295}
296
297} // namespace optimizer
298} // namespace art
299
Vladimir Marko2730db02014-01-27 11:15:17 +0000300extern "C" void ArtCompileDEX(art::CompilerDriver& driver, const art::DexFile::CodeItem* code_item,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700301 uint32_t access_flags, art::InvokeType invoke_type,
302 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
303 const art::DexFile& dex_file,
304 art::DexToDexCompilationLevel dex_to_dex_compilation_level) {
305 UNUSED(invoke_type);
Sebastien Hertz75021222013-07-16 18:34:50 +0200306 if (dex_to_dex_compilation_level != art::kDontDexToDexCompile) {
307 art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000308 dex_file, code_item, class_def_idx, method_idx, access_flags,
309 driver.GetVerifiedMethod(&dex_file, method_idx));
310 art::optimizer::DexCompiler dex_compiler(driver, unit, dex_to_dex_compilation_level);
Sebastien Hertz75021222013-07-16 18:34:50 +0200311 dex_compiler.Compile();
312 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313}