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 "dex_to_dex_compiler.h"
18 
19 #include "android-base/stringprintf.h"
20 
21 #include "art_field-inl.h"
22 #include "art_method-inl.h"
23 #include "base/logging.h"
24 #include "base/mutex.h"
25 #include "compiled_method.h"
26 #include "dex_file-inl.h"
27 #include "dex_instruction-inl.h"
28 #include "driver/compiler_driver.h"
29 #include "driver/dex_compilation_unit.h"
30 #include "mirror/dex_cache.h"
31 #include "thread-inl.h"
32 
33 namespace art {
34 namespace optimizer {
35 
36 using android::base::StringPrintf;
37 
38 // Controls quickening activation.
39 const bool kEnableQuickening = true;
40 // Control check-cast elision.
41 const bool kEnableCheckCastEllision = true;
42 
43 struct QuickenedInfo {
QuickenedInfoart::optimizer::QuickenedInfo44   QuickenedInfo(uint32_t pc, uint16_t index) : dex_pc(pc), dex_member_index(index) {}
45 
46   uint32_t dex_pc;
47   uint16_t dex_member_index;
48 };
49 
50 class DexCompiler {
51  public:
DexCompiler(art::CompilerDriver & compiler,const DexCompilationUnit & unit,DexToDexCompilationLevel dex_to_dex_compilation_level)52   DexCompiler(art::CompilerDriver& compiler,
53               const DexCompilationUnit& unit,
54               DexToDexCompilationLevel dex_to_dex_compilation_level)
55     : driver_(compiler),
56       unit_(unit),
57       dex_to_dex_compilation_level_(dex_to_dex_compilation_level) {}
58 
~DexCompiler()59   ~DexCompiler() {}
60 
61   void Compile();
62 
GetQuickenedInfo() const63   const std::vector<QuickenedInfo>& GetQuickenedInfo() const {
64     return quickened_info_;
65   }
66 
67  private:
GetDexFile() const68   const DexFile& GetDexFile() const {
69     return *unit_.GetDexFile();
70   }
71 
72   // Compiles a RETURN-VOID into a RETURN-VOID-BARRIER within a constructor where
73   // a barrier is required.
74   void CompileReturnVoid(Instruction* inst, uint32_t dex_pc);
75 
76   // Compiles a CHECK-CAST into 2 NOP instructions if it is known to be safe. In
77   // this case, returns the second NOP instruction pointer. Otherwise, returns
78   // the given "inst".
79   Instruction* CompileCheckCast(Instruction* inst, uint32_t dex_pc);
80 
81   // Compiles a field access into a quick field access.
82   // The field index is replaced by an offset within an Object where we can read
83   // from / write to this field. Therefore, this does not involve any resolution
84   // at runtime.
85   // Since the field index is encoded with 16 bits, we can replace it only if the
86   // field offset can be encoded with 16 bits too.
87   void CompileInstanceFieldAccess(Instruction* inst, uint32_t dex_pc,
88                                   Instruction::Code new_opcode, bool is_put);
89 
90   // Compiles a virtual method invocation into a quick virtual method invocation.
91   // The method index is replaced by the vtable index where the corresponding
92   // Executable can be found. Therefore, this does not involve any resolution
93   // at runtime.
94   // Since the method index is encoded with 16 bits, we can replace it only if the
95   // vtable index can be encoded with 16 bits too.
96   void CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
97                             Instruction::Code new_opcode, bool is_range);
98 
99   CompilerDriver& driver_;
100   const DexCompilationUnit& unit_;
101   const DexToDexCompilationLevel dex_to_dex_compilation_level_;
102 
103   // Filled by the compiler when quickening, in order to encode that information
104   // in the .oat file. The runtime will use that information to get to the original
105   // opcodes.
106   std::vector<QuickenedInfo> quickened_info_;
107 
108   DISALLOW_COPY_AND_ASSIGN(DexCompiler);
109 };
110 
Compile()111 void DexCompiler::Compile() {
112   DCHECK_EQ(dex_to_dex_compilation_level_, DexToDexCompilationLevel::kOptimize);
113   const DexFile::CodeItem* code_item = unit_.GetCodeItem();
114   const uint16_t* insns = code_item->insns_;
115   const uint32_t insns_size = code_item->insns_size_in_code_units_;
116   Instruction* inst = const_cast<Instruction*>(Instruction::At(insns));
117 
118   for (uint32_t dex_pc = 0; dex_pc < insns_size;
119        inst = const_cast<Instruction*>(inst->Next()), dex_pc = inst->GetDexPc(insns)) {
120     switch (inst->Opcode()) {
121       case Instruction::RETURN_VOID:
122         CompileReturnVoid(inst, dex_pc);
123         break;
124 
125       case Instruction::CHECK_CAST:
126         inst = CompileCheckCast(inst, dex_pc);
127         break;
128 
129       case Instruction::IGET:
130         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_QUICK, false);
131         break;
132 
133       case Instruction::IGET_WIDE:
134         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_WIDE_QUICK, false);
135         break;
136 
137       case Instruction::IGET_OBJECT:
138         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_OBJECT_QUICK, false);
139         break;
140 
141       case Instruction::IGET_BOOLEAN:
142         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BOOLEAN_QUICK, false);
143         break;
144 
145       case Instruction::IGET_BYTE:
146         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_BYTE_QUICK, false);
147         break;
148 
149       case Instruction::IGET_CHAR:
150         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_CHAR_QUICK, false);
151         break;
152 
153       case Instruction::IGET_SHORT:
154         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IGET_SHORT_QUICK, false);
155         break;
156 
157       case Instruction::IPUT:
158         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_QUICK, true);
159         break;
160 
161       case Instruction::IPUT_BOOLEAN:
162         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BOOLEAN_QUICK, true);
163         break;
164 
165       case Instruction::IPUT_BYTE:
166         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_BYTE_QUICK, true);
167         break;
168 
169       case Instruction::IPUT_CHAR:
170         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_CHAR_QUICK, true);
171         break;
172 
173       case Instruction::IPUT_SHORT:
174         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_SHORT_QUICK, true);
175         break;
176 
177       case Instruction::IPUT_WIDE:
178         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_WIDE_QUICK, true);
179         break;
180 
181       case Instruction::IPUT_OBJECT:
182         CompileInstanceFieldAccess(inst, dex_pc, Instruction::IPUT_OBJECT_QUICK, true);
183         break;
184 
185       case Instruction::INVOKE_VIRTUAL:
186         CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_QUICK, false);
187         break;
188 
189       case Instruction::INVOKE_VIRTUAL_RANGE:
190         CompileInvokeVirtual(inst, dex_pc, Instruction::INVOKE_VIRTUAL_RANGE_QUICK, true);
191         break;
192 
193       default:
194         // Nothing to do.
195         break;
196     }
197   }
198 }
199 
CompileReturnVoid(Instruction * inst,uint32_t dex_pc)200 void DexCompiler::CompileReturnVoid(Instruction* inst, uint32_t dex_pc) {
201   DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
202   if (unit_.IsConstructor()) {
203     // Are we compiling a non clinit constructor which needs a barrier ?
204     if (!unit_.IsStatic() &&
205         driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
206                                            unit_.GetClassDefIndex())) {
207       return;
208     }
209   }
210   // Replace RETURN_VOID by RETURN_VOID_NO_BARRIER.
211   VLOG(compiler) << "Replacing " << Instruction::Name(inst->Opcode())
212                  << " by " << Instruction::Name(Instruction::RETURN_VOID_NO_BARRIER)
213                  << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
214                  << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
215   inst->SetOpcode(Instruction::RETURN_VOID_NO_BARRIER);
216 }
217 
CompileCheckCast(Instruction * inst,uint32_t dex_pc)218 Instruction* DexCompiler::CompileCheckCast(Instruction* inst, uint32_t dex_pc) {
219   if (!kEnableCheckCastEllision) {
220     return inst;
221   }
222   if (!driver_.IsSafeCast(&unit_, dex_pc)) {
223     return inst;
224   }
225   // Ok, this is a safe cast. Since the "check-cast" instruction size is 2 code
226   // units and a "nop" instruction size is 1 code unit, we need to replace it by
227   // 2 consecutive NOP instructions.
228   // Because the caller loops over instructions by calling Instruction::Next onto
229   // the current instruction, we need to return the 2nd NOP instruction. Indeed,
230   // its next instruction is the former check-cast's next instruction.
231   VLOG(compiler) << "Removing " << Instruction::Name(inst->Opcode())
232                  << " by replacing it with 2 NOPs at dex pc "
233                  << StringPrintf("0x%x", dex_pc) << " in method "
234                  << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
235   quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegA_21c()));
236   quickened_info_.push_back(QuickenedInfo(dex_pc, inst->VRegB_21c()));
237   // We are modifying 4 consecutive bytes.
238   inst->SetOpcode(Instruction::NOP);
239   inst->SetVRegA_10x(0u);  // keep compliant with verifier.
240   // Get to next instruction which is the second half of check-cast and replace
241   // it by a NOP.
242   inst = const_cast<Instruction*>(inst->Next());
243   inst->SetOpcode(Instruction::NOP);
244   inst->SetVRegA_10x(0u);  // keep compliant with verifier.
245   return inst;
246 }
247 
CompileInstanceFieldAccess(Instruction * inst,uint32_t dex_pc,Instruction::Code new_opcode,bool is_put)248 void DexCompiler::CompileInstanceFieldAccess(Instruction* inst,
249                                              uint32_t dex_pc,
250                                              Instruction::Code new_opcode,
251                                              bool is_put) {
252   if (!kEnableQuickening) {
253     return;
254   }
255   uint32_t field_idx = inst->VRegC_22c();
256   MemberOffset field_offset(0u);
257   bool is_volatile;
258   bool fast_path = driver_.ComputeInstanceFieldInfo(field_idx, &unit_, is_put,
259                                                     &field_offset, &is_volatile);
260   if (fast_path && !is_volatile && IsUint<16>(field_offset.Int32Value())) {
261     VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
262                    << " to " << Instruction::Name(new_opcode)
263                    << " by replacing field index " << field_idx
264                    << " by field offset " << field_offset.Int32Value()
265                    << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
266                    << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
267     // We are modifying 4 consecutive bytes.
268     inst->SetOpcode(new_opcode);
269     // Replace field index by field offset.
270     inst->SetVRegC_22c(static_cast<uint16_t>(field_offset.Int32Value()));
271     quickened_info_.push_back(QuickenedInfo(dex_pc, field_idx));
272   }
273 }
274 
CompileInvokeVirtual(Instruction * inst,uint32_t dex_pc,Instruction::Code new_opcode,bool is_range)275 void DexCompiler::CompileInvokeVirtual(Instruction* inst, uint32_t dex_pc,
276                                        Instruction::Code new_opcode, bool is_range) {
277   if (!kEnableQuickening) {
278     return;
279   }
280   uint32_t method_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
281   ScopedObjectAccess soa(Thread::Current());
282 
283   ClassLinker* class_linker = unit_.GetClassLinker();
284   ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
285       GetDexFile(),
286       method_idx,
287       unit_.GetDexCache(),
288       unit_.GetClassLoader(),
289       /* referrer */ nullptr,
290       kVirtual);
291 
292   if (UNLIKELY(resolved_method == nullptr)) {
293     // Clean up any exception left by type resolution.
294     soa.Self()->ClearException();
295     return;
296   }
297 
298   uint32_t vtable_idx = resolved_method->GetMethodIndex();
299   DCHECK(IsUint<16>(vtable_idx));
300   VLOG(compiler) << "Quickening " << Instruction::Name(inst->Opcode())
301                  << "(" << GetDexFile().PrettyMethod(method_idx, true) << ")"
302                  << " to " << Instruction::Name(new_opcode)
303                  << " by replacing method index " << method_idx
304                  << " by vtable index " << vtable_idx
305                  << " at dex pc " << StringPrintf("0x%x", dex_pc) << " in method "
306                  << GetDexFile().PrettyMethod(unit_.GetDexMethodIndex(), true);
307   // We are modifying 4 consecutive bytes.
308   inst->SetOpcode(new_opcode);
309   // Replace method index by vtable index.
310   if (is_range) {
311     inst->SetVRegB_3rc(static_cast<uint16_t>(vtable_idx));
312   } else {
313     inst->SetVRegB_35c(static_cast<uint16_t>(vtable_idx));
314   }
315   quickened_info_.push_back(QuickenedInfo(dex_pc, method_idx));
316 }
317 
ArtCompileDEX(CompilerDriver * driver,const DexFile::CodeItem * code_item,uint32_t access_flags,InvokeType invoke_type ATTRIBUTE_UNUSED,uint16_t class_def_idx,uint32_t method_idx,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,DexToDexCompilationLevel dex_to_dex_compilation_level)318 CompiledMethod* ArtCompileDEX(
319     CompilerDriver* driver,
320     const DexFile::CodeItem* code_item,
321     uint32_t access_flags,
322     InvokeType invoke_type ATTRIBUTE_UNUSED,
323     uint16_t class_def_idx,
324     uint32_t method_idx,
325     Handle<mirror::ClassLoader> class_loader,
326     const DexFile& dex_file,
327     DexToDexCompilationLevel dex_to_dex_compilation_level) {
328   DCHECK(driver != nullptr);
329   if (dex_to_dex_compilation_level != DexToDexCompilationLevel::kDontDexToDexCompile) {
330     ScopedObjectAccess soa(Thread::Current());
331     StackHandleScope<1> hs(soa.Self());
332     ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
333     art::DexCompilationUnit unit(
334         class_loader,
335         class_linker,
336         dex_file,
337         code_item,
338         class_def_idx,
339         method_idx,
340         access_flags,
341         driver->GetVerifiedMethod(&dex_file, method_idx),
342         hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)));
343     art::optimizer::DexCompiler dex_compiler(*driver, unit, dex_to_dex_compilation_level);
344     dex_compiler.Compile();
345     if (dex_compiler.GetQuickenedInfo().empty()) {
346       // No need to create a CompiledMethod if there are no quickened opcodes.
347       return nullptr;
348     }
349 
350     // Create a `CompiledMethod`, with the quickened information in the vmap table.
351     Leb128EncodingVector<> builder;
352     for (QuickenedInfo info : dex_compiler.GetQuickenedInfo()) {
353       builder.PushBackUnsigned(info.dex_pc);
354       builder.PushBackUnsigned(info.dex_member_index);
355     }
356     InstructionSet instruction_set = driver->GetInstructionSet();
357     if (instruction_set == kThumb2) {
358       // Don't use the thumb2 instruction set to avoid the one off code delta.
359       instruction_set = kArm;
360     }
361     return CompiledMethod::SwapAllocCompiledMethod(
362         driver,
363         instruction_set,
364         ArrayRef<const uint8_t>(),                   // no code
365         0,
366         0,
367         0,
368         ArrayRef<const uint8_t>(),                   // method_info
369         ArrayRef<const uint8_t>(builder.GetData()),  // vmap_table
370         ArrayRef<const uint8_t>(),                   // cfi data
371         ArrayRef<const LinkerPatch>());
372   }
373   return nullptr;
374 }
375 
376 }  // namespace optimizer
377 
378 }  // namespace art
379