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