blob: 4a724b109adff48aa88856a41034a863ada13001 [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027
28namespace art {
29namespace optimizer {
30
31// Controls quickening activation.
32const bool kEnableQuickening = true;
Sebastien Hertz543959c2013-07-03 12:00:19 +020033// Control check-cast elision.
34const bool kEnableCheckCastEllision = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36class DexCompiler {
37 public:
38 DexCompiler(art::CompilerDriver& compiler,
Sebastien Hertz75021222013-07-16 18:34:50 +020039 const DexCompilationUnit& unit,
40 DexToDexCompilationLevel dex_to_dex_compilation_level)
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 : driver_(compiler),
Sebastien Hertz75021222013-07-16 18:34:50 +020042 unit_(unit),
43 dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070044
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070045 ~DexCompiler() {}
Brian Carlstrom7940e442013-07-12 13:46:57 -070046
47 void Compile();
48
49 private:
50 const DexFile& GetDexFile() const {
51 return *unit_.GetDexFile();
52 }
53
54 // TODO: since the whole compilation pipeline uses a "const DexFile", we need
55 // to "unconst" here. The DEX-to-DEX compiler should work on a non-const DexFile.
56 DexFile& GetModifiableDexFile() {
57 return *const_cast<DexFile*>(unit_.GetDexFile());
58 }
59
Sebastien Hertz75021222013-07-16 18:34:50 +020060 bool PerformOptimizations() const {
61 return dex_to_dex_compilation_level_ >= kOptimize;
62 }
63
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
65 // a barrier is required.
66 void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
67
Sebastien Hertz543959c2013-07-03 12:00:19 +020068 // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
69 // this case, returns the second NOP instruction pointer. Otherwise, returns
70 // the given "inst".
71 Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 // Compiles a field access into a quick field access.
74 // The field index is replaced by an offset within an Object where we can read
75 // from / write to this field. Therefore, this does not involve any resolution
76 // at runtime.
77 // Since the field index is encoded with 16 bits, we can replace it only if the
78 // field offset can be encoded with 16 bits too.
79 void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
80 Instruction::Code new_opcode, bool is_put);
81
82 // Compiles a virtual method invocation into a quick virtual method invocation.
83 // The method index is replaced by the vtable index where the corresponding
84 // AbstractMethod can be found. Therefore, this does not involve any resolution
85 // at runtime.
86 // Since the method index is encoded with 16 bits, we can replace it only if the
87 // vtable index can be encoded with 16 bits too.
88 void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
89 Instruction::Code new_opcode, bool is_range);
90
91 CompilerDriver& driver_;
92 const DexCompilationUnit& unit_;
Sebastien Hertz75021222013-07-16 18:34:50 +020093 const DexToDexCompilationLevel dex_to_dex_compilation_level_;
Brian Carlstrom7940e442013-07-12 13:46:57 -070094
95 DISALLOW_COPY_AND_ASSIGN(DexCompiler);
96};
97
Brian Carlstrom7940e442013-07-12 13:46:57 -070098void DexCompiler::Compile() {
Sebastien Hertz75021222013-07-16 18:34:50 +020099 DCHECK_GE(dex_to_dex_compilation_level_, kRequired);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 const DexFile::CodeItem* code_item = unit_.GetCodeItem();
101 const uint16_t* insns = code_item->insns_;
102 const uint32_t insns_size = code_item->insns_size_in_code_units_;
103 Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
104
105 for (uint32_t dex_pc = 0; dex_pc < insns_size;
106 inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
107 switch (inst->Opcode()) {
108 case Instruction::RETURN_VOID:
109 CompileReturnVoid(inst, dex_pc);
110 break;
111
Sebastien Hertz543959c2013-07-03 12:00:19 +0200112 case Instruction::CHECK_CAST:
113 inst = CompileCheckCast(inst, dex_pc);
114 break;
115
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 case Instruction::IGET:
117 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
118 break;
119
120 case Instruction::IGET_WIDE:
121 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
122 break;
123
124 case Instruction::IGET_OBJECT:
125 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
126 break;
127
128 case Instruction::IPUT:
129 case Instruction::IPUT_BOOLEAN:
130 case Instruction::IPUT_BYTE:
131 case Instruction::IPUT_CHAR:
132 case Instruction::IPUT_SHORT:
133 // These opcodes have the same implementation in interpreter so group
134 // them under IPUT_QUICK.
135 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
136 break;
137
138 case Instruction::IPUT_WIDE:
139 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
140 break;
141
142 case Instruction::IPUT_OBJECT:
143 CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
144 break;
145
146 case Instruction::INVOKE_VIRTUAL:
147 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
148 break;
149
150 case Instruction::INVOKE_VIRTUAL_RANGE:
151 CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
152 break;
153
154 default:
155 // Nothing to do.
156 break;
157 }
158 }
159}
160
161void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
162 DCHECK(inst->Opcode() == Instruction::RETURN_VOID);
Ian Rogers9fc16eb2013-07-31 14:49:16 -0700163 // Are we compiling a non-clinit constructor?
164 if (!unit_.IsConstructor() || unit_.IsStatic()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 return;
166 }
167 // Do we need a constructor barrier ?
168 if (!driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
169 unit_.GetClassDefIndex())) {
170 return;
171 }
172 // Replace RETURN_VOID by RETURN_VOID_BARRIER.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200173 VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
174 << " by " << Instruction::Name(Instruction::RETURN_VOID_BARRIER)
175 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
176 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 inst->SetOpcode(Instruction::RETURN_VOID_BARRIER);
178}
179
Sebastien Hertz543959c2013-07-03 12:00:19 +0200180Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200181 if (!kEnableCheckCastEllision || !PerformOptimizations()) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200182 return inst;
183 }
184 MethodReference referrer(&GetDexFile(), unit_.GetDexMethodIndex());
185 if (!driver_.IsSafeCast(referrer, dex_pc)) {
186 return inst;
187 }
188 // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
189 // units and a "nop" instruction size is 1 code unit, we need to replace it by
190 // 2 consecutive NOP instructions.
191 // Because the caller loops over instructions by calling Instruction::Next onto
192 // the current instruction, we need to return the 2nd NOP instruction. Indeed,
193 // its next instruction is the former check-cast's next instruction.
194 VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
195 << " by replacing it with 2 NOPs at dex pc "
196 << StringPrintf("0x%x", dex_pc) << " in method "
197 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
198 // We are modifying 4 consecutive bytes.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200199 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700200 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200201 // Get to next instruction which is the second half of check-cast and replace
202 // it by a NOP.
203 inst = const_cast<Instruction*>(inst->Next());
204 inst->SetOpcode(Instruction::NOP);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700205 inst->SetVRegA_10x(0u); // keep compliant with verifier.
Sebastien Hertz543959c2013-07-03 12:00:19 +0200206 return inst;
207}
208
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
210 uint32_t dex_pc,
211 Instruction::Code new_opcode,
212 bool is_put) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200213 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 return;
215 }
216 uint32_t field_idx = inst->VRegC_22c();
217 int field_offset;
218 bool is_volatile;
Ian Rogers9b297bf2013-09-06 11:11:25 -0700219 bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
220 &field_offset, &is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 if (fast_path && !is_volatile && IsUint(16, field_offset)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200222 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
223 << " to " << Instruction::Name(new_opcode)
224 << " by replacing field index " << field_idx
225 << " by field offset " << field_offset
226 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
227 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 inst->SetOpcode(new_opcode);
230 // Replace field index by field offset.
231 inst->SetVRegC_22c(static_cast<uint16_t>(field_offset));
232 }
233}
234
235void DexCompiler::CompileInvokeVirtual(Instruction* inst,
236 uint32_t dex_pc,
237 Instruction::Code new_opcode,
238 bool is_range) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200239 if (!kEnableQuickening || !PerformOptimizations()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 return;
241 }
242 uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
243 MethodReference target_method(&GetDexFile(), method_idx);
244 InvokeType invoke_type = kVirtual;
245 InvokeType original_invoke_type = invoke_type;
246 int vtable_idx;
247 uintptr_t direct_code;
248 uintptr_t direct_method;
Sebastien Hertz1e54d682013-09-06 14:52:10 +0200249 // TODO: support devirtualization.
250 const bool kEnableDevirtualization = false;
Ian Rogers65ec92c2013-09-06 10:49:58 -0700251 bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc,
252 false, kEnableDevirtualization,
253 &invoke_type,
254 &target_method, &vtable_idx,
255 &direct_code, &direct_method);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 if (fast_path && original_invoke_type == invoke_type) {
257 if (vtable_idx >= 0 && IsUint(16, vtable_idx)) {
Sebastien Hertz543959c2013-07-03 12:00:19 +0200258 VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
259 << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")"
260 << " to " << Instruction::Name(new_opcode)
261 << " by replacing method index " << method_idx
262 << " by vtable index " << vtable_idx
263 << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
264 << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 // We are modifying 4 consecutive bytes.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 inst->SetOpcode(new_opcode);
267 // Replace method index by vtable index.
268 if (is_range) {
269 inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
270 } else {
271 inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
272 }
273 }
274 }
275}
276
277} // namespace optimizer
278} // namespace art
279
Sebastien Hertz75021222013-07-16 18:34:50 +0200280extern "C" void ArtCompileDEX(art::CompilerDriver& compiler, const art::DexFile::CodeItem* code_item,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 uint32_t access_flags, art::InvokeType invoke_type,
282 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
Sebastien Hertz75021222013-07-16 18:34:50 +0200283 const art::DexFile& dex_file,
284 art::DexToDexCompilationLevel dex_to_dex_compilation_level) {
285 if (dex_to_dex_compilation_level != art::kDontDexToDexCompile) {
286 art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(),
287 dex_file, code_item, class_def_idx, method_idx, access_flags);
288 art::optimizer::DexCompiler dex_compiler(compiler, unit, dex_to_dex_compilation_level);
289 dex_compiler.Compile();
290 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291}