blob: ffd7905dfe7ea2f6634504dd55b856639226519c [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
55 // TODO: since the whole compilation pipeline uses a "const DexFile", we need
56 // to "unconst" here. The DEX-to-DEX compiler should work on a non-const DexFile.
57 DexFile& GetModifiableDexFile() {
58 return *const_cast<DexFile*>(unit_.GetDexFile());
59 }
60
Sebastien Hertz75021222013-07-16 18:34:50 +020061 bool PerformOptimizations() const {
62 return dex_to_dex_compilation_level_ >= kOptimize;
63 }
64
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
66 // a barrier is required.
67 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
68
Sebastien Hertz543959c2013-07-03 12:00:19 +020069 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
70 // this case, returns the second NOP instruction pointer. Otherwise, returns
71 // the given "inst".
72 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
73
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 // Compiles a field access into a quick field access.
75 // The field index is replaced by an offset within an Object where we can read
76 // from / write to this field. Therefore, this does not involve any resolution
77 // at runtime.
78 // Since the field index is encoded with 16 bits, we can replace it only if the
79 // field offset can be encoded with 16 bits too.
80 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
81 Instruction::Code new_opcode, bool is_put);
82
83 // Compiles a virtual method invocation into a quick virtual method invocation.
84 // The method index is replaced by the vtable index where the corresponding
85 // AbstractMethod can be found. Therefore, this does not involve any resolution
86 // at runtime.
87 // Since the method index is encoded with 16 bits, we can replace it only if the
88 // vtable index can be encoded with 16 bits too.
89 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
90 Instruction::Code new_opcode, bool is_range);
91
92 CompilerDriver& driver_;
93 const DexCompilationUnit& unit_;
Sebastien Hertz75021222013-07-16 18:34:50 +020094 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070095
96 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
97};
98
Brian Carlstrom7940e442013-07-12 13:46:57 -070099void DexCompiler::Compile() {
Sebastien Hertz75021222013-07-16 18:34:50 +0200100 DCHECK_GE(dex_to_dex_compilation_level_, kRequired);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 const DexFile::CodeItem* code_item = unit_.GetCodeItem();
102 const uint16_t* insns = code_item->insns_;
103 const uint32_t insns_size = code_item->insns_size_in_code_units_;
104 Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
105
106 for (uint32_t dex_pc = 0; dex_pc < insns_size;
107 inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
108 switch (inst->Opcode()) {
109 case Instruction::RETURN_VOID:
110 CompileReturnVoid(inst, dex_pc);
111 break;
112
Sebastien Hertz543959c2013-07-03 12:00:19 +0200113 case Instruction::CHECK_CAST:
114 inst = CompileCheckCast(inst, dex_pc);
115 break;
116
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 case Instruction::IGET:
118 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
119 break;
120
121 case Instruction::IGET_WIDE:
122 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
123 break;
124
125 case Instruction::IGET_OBJECT:
126 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
127 break;
128
129 case Instruction::IPUT:
130 case Instruction::IPUT_BOOLEAN:
131 case Instruction::IPUT_BYTE:
132 case Instruction::IPUT_CHAR:
133 case Instruction::IPUT_SHORT:
134 // These opcodes have the same implementation in interpreter so group
135 // them under IPUT_QUICK.
136 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
137 break;
138
139 case Instruction::IPUT_WIDE:
140 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
141 break;
142
143 case Instruction::IPUT_OBJECT:
144 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
145 break;
146
147 case Instruction::INVOKE_VIRTUAL:
148 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
149 break;
150
151 case Instruction::INVOKE_VIRTUAL_RANGE:
152 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
153 break;
154
155 default:
156 // Nothing to do.
157 break;
158 }
159 }
160}
161
162void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
163 DCHECK(inst->Opcode() == Instruction::RETURN_VOID);
Ian Rogers9fc16eb2013-07-31 14:49:16 -0700164 // Are we compiling a non-clinit constructor?
165 if (!unit_.IsConstructor() || unit_.IsStatic()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 return;
167 }
168 // Do we need a constructor barrier ?
169 if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
170 unit_.GetClassDefIndex())) {
171 return;
172 }
173 // Replace RETURN_VOID by RETURN_VOID_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200174 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
175 << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER)
176 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
177 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 inst->SetOpcode(Instruction::RETURN_VOID_BARRIER);
179}
180
Sebastien Hertz543959c2013-07-03 12:00:19 +0200181Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200182 if (!kEnableCheckCastEllision || !PerformOptimizations()) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200183 return inst;
184 }
185 MethodReference referrer(&GetDexFile(), unit_.GetDexMethodIndex());
186 if (!driver_.IsSafeCast(referrer, dex_pc)) {
187 return inst;
188 }
189 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
190 // units and a "nop" instruction size is 1 code unit, we need to replace it by
191 // 2 consecutive NOP instructions.
192 // Because the caller loops over instructions by calling Instruction::Next onto
193 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
194 // its next instruction is the former check-cast's next instruction.
195 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
196 << " by replacing it with 2 NOPs at dex pc "
197 << StringPrintf("0x%x", dex_pc) << " in method "
198 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
199 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200200 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700201 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200202 // Get to next instruction which is the second half of check-cast and replace
203 // it by a NOP.
204 inst = const_cast<Instruction*>(inst->Next());
205 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700206 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200207 return inst;
208}
209
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
211 uint32_t dex_pc,
212 Instruction::Code new_opcode,
213 bool is_put) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200214 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 return;
216 }
217 uint32_t field_idx = inst->VRegC_22c();
218 int field_offset;
219 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700220 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
221 &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 if (fast_path && !is_volatile && IsUint(16, field_offset)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200223 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
224 << " to " << Instruction::Name(new_opcode)
225 << " by replacing field index " << field_idx
226 << " by field offset " << field_offset
227 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
228 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 inst->SetOpcode(new_opcode);
231 // Replace field index by field offset.
232 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset));
233 }
234}
235
236void DexCompiler::CompileInvokeVirtual(Instruction* inst,
237 uint32_t dex_pc,
238 Instruction::Code new_opcode,
239 bool is_range) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200240 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 return;
242 }
243 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
244 MethodReference target_method(&GetDexFile(), method_idx);
245 InvokeType invoke_type = kVirtual;
246 InvokeType original_invoke_type = invoke_type;
247 int vtable_idx;
248 uintptr_t direct_code;
249 uintptr_t direct_method;
Sebastien Hertz1e54d682013-09-06 14:52:10 +0200250 // TODO: support devirtualization.
251 const bool kEnableDevirtualization = false;
Ian Rogers65ec92c2013-09-06 10:49:58 -0700252 bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc,
253 false, kEnableDevirtualization,
254 &invoke_type,
255 &target_method, &vtable_idx,
256 &direct_code, &direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 if (fast_path && original_invoke_type == invoke_type) {
258 if (vtable_idx >= 0 && IsUint(16, vtable_idx)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200259 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
260 << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")"
261 << " to " << Instruction::Name(new_opcode)
262 << " by replacing method index " << method_idx
263 << " by vtable index " << vtable_idx
264 << " 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 method index by vtable index.
269 if (is_range) {
270 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
271 } else {
272 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
273 }
274 }
275 }
276}
277
278} // namespace optimizer
279} // namespace art
280
Sebastien Hertz75021222013-07-16 18:34:50 +0200281extern "C" void ArtCompileDEX(art::CompilerDriver& compiler, const art::DexFile::CodeItem* code_item,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 uint32_t access_flags, art::InvokeType invoke_type,
283 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
Sebastien Hertz75021222013-07-16 18:34:50 +0200284 const art::DexFile& dex_file,
285 art::DexToDexCompilationLevel dex_to_dex_compilation_level) {
286 if (dex_to_dex_compilation_level != art::kDontDexToDexCompile) {
287 art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(),
288 dex_file, code_item, class_def_idx, method_idx, access_flags);
289 art::optimizer::DexCompiler dex_compiler(compiler, unit, dex_to_dex_compilation_level);
290 dex_compiler.Compile();
291 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292}