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