• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
23 #include "mirror/art_field-inl.h"
24 #include "mirror/art_method-inl.h"
25 #include "mirror/class-inl.h"
26 #include "mirror/dex_cache.h"
27 
28 namespace art {
29 namespace optimizer {
30 
31 // Controls quickening activation.
32 const bool kEnableQuickening = true;
33 // Control check-cast elision.
34 const bool kEnableCheckCastEllision = true;
35 
36 class DexCompiler {
37  public:
DexCompiler(art::CompilerDriver & compiler,const DexCompilationUnit & unit,DexToDexCompilationLevel dex_to_dex_compilation_level)38   DexCompiler(art::CompilerDriver& compiler,
39               const DexCompilationUnit& unit,
40               DexToDexCompilationLevel dex_to_dex_compilation_level)
41     : driver_(compiler),
42       unit_(unit),
43       dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
44 
~DexCompiler()45   ~DexCompiler() {}
46 
47   void Compile();
48 
49  private:
GetDexFile() const50   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.
GetModifiableDexFile()56   DexFile& GetModifiableDexFile() {
57     return *const_cast<DexFile*>(unit_.GetDexFile());
58   }
59 
PerformOptimizations() const60   bool PerformOptimizations() const {
61     return dex_to_dex_compilation_level_ >= kOptimize;
62   }
63 
64   // 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 
68   // 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 
73   // 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_;
93   const DexToDexCompilationLevel dex_to_dex_compilation_level_;
94 
95   DISALLOW_COPY_AND_ASSIGN(DexCompiler);
96 };
97 
Compile()98 void DexCompiler::Compile() {
99   DCHECK_GE(dex_to_dex_compilation_level_, kRequired);
100   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 
112       case Instruction::CHECK_CAST:
113         inst = CompileCheckCast(inst, dex_pc);
114         break;
115 
116       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 
CompileReturnVoid(Instruction * inst,uint32_t dex_pc)161 void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
162   DCHECK(inst->Opcode() == Instruction::RETURN_VOID);
163   // Are we compiling a non-clinit constructor?
164   if (!unit_.IsConstructor() || unit_.IsStatic()) {
165     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.
173   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);
177   inst->SetOpcode(Instruction::RETURN_VOID_BARRIER);
178 }
179 
CompileCheckCast(Instruction * inst,uint32_t dex_pc)180 Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
181   if (!kEnableCheckCastEllision || !PerformOptimizations()) {
182     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.
199   inst->SetOpcode(Instruction::NOP);
200   inst->SetVRegA_10x(0u);  // keep compliant with verifier.
201   // 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);
205   inst->SetVRegA_10x(0u);  // keep compliant with verifier.
206   return inst;
207 }
208 
CompileInstanceFieldAccess(Instruction * inst,uint32_t dex_pc,Instruction::Code new_opcode,bool is_put)209 void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
210                                              uint32_t dex_pc,
211                                              Instruction::Code new_opcode,
212                                              bool is_put) {
213   if (!kEnableQuickening || !PerformOptimizations()) {
214     return;
215   }
216   uint32_t field_idx = inst->VRegC_22c();
217   int field_offset;
218   bool is_volatile;
219   bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, field_offset,
220                                                     is_volatile, is_put);
221   if (fast_path && !is_volatile && IsUint(16, field_offset)) {
222     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);
228     // We are modifying 4 consecutive bytes.
229     inst->SetOpcode(new_opcode);
230     // Replace field index by field offset.
231     inst->SetVRegC_22c(static_cast<uint16_t>(field_offset));
232   }
233 }
234 
CompileInvokeVirtual(Instruction * inst,uint32_t dex_pc,Instruction::Code new_opcode,bool is_range)235 void DexCompiler::CompileInvokeVirtual(Instruction* inst,
236                                 uint32_t dex_pc,
237                                 Instruction::Code new_opcode,
238                                 bool is_range) {
239   if (!kEnableQuickening || !PerformOptimizations()) {
240     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;
249   bool fast_path = driver_.ComputeInvokeInfo(&unit_, dex_pc, invoke_type,
250                                              target_method, vtable_idx,
251                                              direct_code, direct_method,
252                                              false);
253   // TODO: support devirtualization.
254   if (fast_path && original_invoke_type == invoke_type) {
255     if (vtable_idx >= 0 && IsUint(16, vtable_idx)) {
256       VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
257                      << "(" << PrettyMethod(method_idx, GetDexFile(), true) << ")"
258                      << " to " << Instruction::Name(new_opcode)
259                      << " by replacing method index " << method_idx
260                      << " by vtable index " << vtable_idx
261                      << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
262                      << PrettyMethod(unit_.GetDexMethodIndex(), GetDexFile(), true);
263       // We are modifying 4 consecutive bytes.
264       inst->SetOpcode(new_opcode);
265       // Replace method index by vtable index.
266       if (is_range) {
267         inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
268       } else {
269         inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
270       }
271     }
272   }
273 }
274 
275 }  // namespace optimizer
276 }  // namespace art
277 
ArtCompileDEX(art::CompilerDriver & compiler,const art::DexFile::CodeItem * code_item,uint32_t access_flags,art::InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,jobject class_loader,const art::DexFile & dex_file,art::DexToDexCompilationLevel dex_to_dex_compilation_level)278 extern "C" void ArtCompileDEX(art::CompilerDriver& compiler, const art::DexFile::CodeItem* code_item,
279                   uint32_t access_flags, art::InvokeType invoke_type,
280                   uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
281                   const art::DexFile& dex_file,
282                   art::DexToDexCompilationLevel dex_to_dex_compilation_level) {
283   if (dex_to_dex_compilation_level != art::kDontDexToDexCompile) {
284     art::DexCompilationUnit unit(NULL, class_loader, art::Runtime::Current()->GetClassLinker(),
285                                  dex_file, code_item, class_def_idx, method_idx, access_flags);
286     art::optimizer::DexCompiler dex_compiler(compiler, unit, dex_to_dex_compilation_level);
287     dex_compiler.Compile();
288   }
289 }
290