gen_common.cc revision 9c86a0279aaf953377aa9e2277592e68bf814989
1/*
2 * Copyright (C) 2012 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/compiler_ir.h"
18#include "dex/compiler_internals.h"
19#include "dex/quick/mir_to_lir-inl.h"
20#include "entrypoints/quick/quick_entrypoints.h"
21#include "mirror/array.h"
22#include "mirror/object-inl.h"
23#include "verifier/method_verifier.h"
24#include <functional>
25
26namespace art {
27
28/*
29 * This source files contains "gen" codegen routines that should
30 * be applicable to most targets.  Only mid-level support utilities
31 * and "op" calls may be used here.
32 */
33
34/*
35 * Generate a kPseudoBarrier marker to indicate the boundary of special
36 * blocks.
37 */
38void Mir2Lir::GenBarrier() {
39  LIR* barrier = NewLIR0(kPseudoBarrier);
40  /* Mark all resources as being clobbered */
41  DCHECK(!barrier->flags.use_def_invalid);
42  barrier->u.m.def_mask = ENCODE_ALL;
43}
44
45// TODO: need to do some work to split out targets with
46// condition codes and those without
47LIR* Mir2Lir::GenCheck(ConditionCode c_code, ThrowKind kind) {
48  DCHECK_NE(cu_->instruction_set, kMips);
49  LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_);
50  LIR* branch = OpCondBranch(c_code, tgt);
51  // Remember branch target - will process later
52  throw_launchpads_.Insert(tgt);
53  return branch;
54}
55
56LIR* Mir2Lir::GenImmedCheck(ConditionCode c_code, int reg, int imm_val, ThrowKind kind) {
57  LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg, imm_val);
58  LIR* branch;
59  if (c_code == kCondAl) {
60    branch = OpUnconditionalBranch(tgt);
61  } else {
62    branch = OpCmpImmBranch(c_code, reg, imm_val, tgt);
63  }
64  // Remember branch target - will process later
65  throw_launchpads_.Insert(tgt);
66  return branch;
67}
68
69/* Perform null-check on a register.  */
70LIR* Mir2Lir::GenNullCheck(int s_reg, int m_reg, int opt_flags) {
71  if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) {
72    return NULL;
73  }
74  return GenImmedCheck(kCondEq, m_reg, 0, kThrowNullPointer);
75}
76
77/* Perform check on two registers */
78LIR* Mir2Lir::GenRegRegCheck(ConditionCode c_code, int reg1, int reg2,
79                             ThrowKind kind) {
80  LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind, current_dalvik_offset_, reg1, reg2);
81  LIR* branch = OpCmpBranch(c_code, reg1, reg2, tgt);
82  // Remember branch target - will process later
83  throw_launchpads_.Insert(tgt);
84  return branch;
85}
86
87void Mir2Lir::GenCompareAndBranch(Instruction::Code opcode, RegLocation rl_src1,
88                                  RegLocation rl_src2, LIR* taken,
89                                  LIR* fall_through) {
90  ConditionCode cond;
91  switch (opcode) {
92    case Instruction::IF_EQ:
93      cond = kCondEq;
94      break;
95    case Instruction::IF_NE:
96      cond = kCondNe;
97      break;
98    case Instruction::IF_LT:
99      cond = kCondLt;
100      break;
101    case Instruction::IF_GE:
102      cond = kCondGe;
103      break;
104    case Instruction::IF_GT:
105      cond = kCondGt;
106      break;
107    case Instruction::IF_LE:
108      cond = kCondLe;
109      break;
110    default:
111      cond = static_cast<ConditionCode>(0);
112      LOG(FATAL) << "Unexpected opcode " << opcode;
113  }
114
115  // Normalize such that if either operand is constant, src2 will be constant
116  if (rl_src1.is_const) {
117    RegLocation rl_temp = rl_src1;
118    rl_src1 = rl_src2;
119    rl_src2 = rl_temp;
120    cond = FlipComparisonOrder(cond);
121  }
122
123  rl_src1 = LoadValue(rl_src1, kCoreReg);
124  // Is this really an immediate comparison?
125  if (rl_src2.is_const) {
126    // If it's already live in a register or not easily materialized, just keep going
127    RegLocation rl_temp = UpdateLoc(rl_src2);
128    if ((rl_temp.location == kLocDalvikFrame) &&
129        InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src2))) {
130      // OK - convert this to a compare immediate and branch
131      OpCmpImmBranch(cond, rl_src1.low_reg, mir_graph_->ConstantValue(rl_src2), taken);
132      return;
133    }
134  }
135  rl_src2 = LoadValue(rl_src2, kCoreReg);
136  OpCmpBranch(cond, rl_src1.low_reg, rl_src2.low_reg, taken);
137}
138
139void Mir2Lir::GenCompareZeroAndBranch(Instruction::Code opcode, RegLocation rl_src, LIR* taken,
140                                      LIR* fall_through) {
141  ConditionCode cond;
142  rl_src = LoadValue(rl_src, kCoreReg);
143  switch (opcode) {
144    case Instruction::IF_EQZ:
145      cond = kCondEq;
146      break;
147    case Instruction::IF_NEZ:
148      cond = kCondNe;
149      break;
150    case Instruction::IF_LTZ:
151      cond = kCondLt;
152      break;
153    case Instruction::IF_GEZ:
154      cond = kCondGe;
155      break;
156    case Instruction::IF_GTZ:
157      cond = kCondGt;
158      break;
159    case Instruction::IF_LEZ:
160      cond = kCondLe;
161      break;
162    default:
163      cond = static_cast<ConditionCode>(0);
164      LOG(FATAL) << "Unexpected opcode " << opcode;
165  }
166  OpCmpImmBranch(cond, rl_src.low_reg, 0, taken);
167}
168
169void Mir2Lir::GenIntToLong(RegLocation rl_dest, RegLocation rl_src) {
170  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
171  if (rl_src.location == kLocPhysReg) {
172    OpRegCopy(rl_result.low_reg, rl_src.low_reg);
173  } else {
174    LoadValueDirect(rl_src, rl_result.low_reg);
175  }
176  OpRegRegImm(kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
177  StoreValueWide(rl_dest, rl_result);
178}
179
180void Mir2Lir::GenIntNarrowing(Instruction::Code opcode, RegLocation rl_dest,
181                              RegLocation rl_src) {
182  rl_src = LoadValue(rl_src, kCoreReg);
183  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
184  OpKind op = kOpInvalid;
185  switch (opcode) {
186    case Instruction::INT_TO_BYTE:
187      op = kOp2Byte;
188      break;
189    case Instruction::INT_TO_SHORT:
190       op = kOp2Short;
191       break;
192    case Instruction::INT_TO_CHAR:
193       op = kOp2Char;
194       break;
195    default:
196      LOG(ERROR) << "Bad int conversion type";
197  }
198  OpRegReg(op, rl_result.low_reg, rl_src.low_reg);
199  StoreValue(rl_dest, rl_result);
200}
201
202/*
203 * Let helper function take care of everything.  Will call
204 * Array::AllocFromCode(type_idx, method, count);
205 * Note: AllocFromCode will handle checks for errNegativeArraySize.
206 */
207void Mir2Lir::GenNewArray(uint32_t type_idx, RegLocation rl_dest,
208                          RegLocation rl_src) {
209  FlushAllRegs();  /* Everything to home location */
210  ThreadOffset func_offset(-1);
211  const DexFile* dex_file = cu_->dex_file;
212  CompilerDriver* driver = cu_->compiler_driver;
213  if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *dex_file,
214                                                       type_idx)) {
215    bool is_type_initialized;  // Ignored as an array does not have an initializer.
216    bool use_direct_type_ptr;
217    uintptr_t direct_type_ptr;
218    if (kEmbedClassInCode &&
219        driver->CanEmbedTypeInCode(*dex_file, type_idx,
220                                   &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
221      // The fast path.
222      if (!use_direct_type_ptr) {
223        LoadClassType(type_idx, kArg0);
224        func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
225        CallRuntimeHelperRegMethodRegLocation(func_offset, TargetReg(kArg0), rl_src, true);
226      } else {
227        // Use the direct pointer.
228        func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArrayResolved);
229        CallRuntimeHelperImmMethodRegLocation(func_offset, direct_type_ptr, rl_src, true);
230      }
231    } else {
232      // The slow path.
233      DCHECK_EQ(func_offset.Int32Value(), -1);
234      func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocArray);
235      CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
236    }
237    DCHECK_NE(func_offset.Int32Value(), -1);
238  } else {
239    func_offset= QUICK_ENTRYPOINT_OFFSET(pAllocArrayWithAccessCheck);
240    CallRuntimeHelperImmMethodRegLocation(func_offset, type_idx, rl_src, true);
241  }
242  RegLocation rl_result = GetReturn(false);
243  StoreValue(rl_dest, rl_result);
244}
245
246/*
247 * Similar to GenNewArray, but with post-allocation initialization.
248 * Verifier guarantees we're dealing with an array class.  Current
249 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
250 * Current code also throws internal unimp if not 'L', '[' or 'I'.
251 */
252void Mir2Lir::GenFilledNewArray(CallInfo* info) {
253  int elems = info->num_arg_words;
254  int type_idx = info->index;
255  FlushAllRegs();  /* Everything to home location */
256  ThreadOffset func_offset(-1);
257  if (cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx, *cu_->dex_file,
258                                                       type_idx)) {
259    func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArray);
260  } else {
261    func_offset = QUICK_ENTRYPOINT_OFFSET(pCheckAndAllocArrayWithAccessCheck);
262  }
263  CallRuntimeHelperImmMethodImm(func_offset, type_idx, elems, true);
264  FreeTemp(TargetReg(kArg2));
265  FreeTemp(TargetReg(kArg1));
266  /*
267   * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
268   * return region.  Because AllocFromCode placed the new array
269   * in kRet0, we'll just lock it into place.  When debugger support is
270   * added, it may be necessary to additionally copy all return
271   * values to a home location in thread-local storage
272   */
273  LockTemp(TargetReg(kRet0));
274
275  // TODO: use the correct component size, currently all supported types
276  // share array alignment with ints (see comment at head of function)
277  size_t component_size = sizeof(int32_t);
278
279  // Having a range of 0 is legal
280  if (info->is_range && (elems > 0)) {
281    /*
282     * Bit of ugliness here.  We're going generate a mem copy loop
283     * on the register range, but it is possible that some regs
284     * in the range have been promoted.  This is unlikely, but
285     * before generating the copy, we'll just force a flush
286     * of any regs in the source range that have been promoted to
287     * home location.
288     */
289    for (int i = 0; i < elems; i++) {
290      RegLocation loc = UpdateLoc(info->args[i]);
291      if (loc.location == kLocPhysReg) {
292        StoreBaseDisp(TargetReg(kSp), SRegOffset(loc.s_reg_low),
293                      loc.low_reg, kWord);
294      }
295    }
296    /*
297     * TUNING note: generated code here could be much improved, but
298     * this is an uncommon operation and isn't especially performance
299     * critical.
300     */
301    int r_src = AllocTemp();
302    int r_dst = AllocTemp();
303    int r_idx = AllocTemp();
304    int r_val = INVALID_REG;
305    switch (cu_->instruction_set) {
306      case kThumb2:
307        r_val = TargetReg(kLr);
308        break;
309      case kX86:
310        FreeTemp(TargetReg(kRet0));
311        r_val = AllocTemp();
312        break;
313      case kMips:
314        r_val = AllocTemp();
315        break;
316      default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
317    }
318    // Set up source pointer
319    RegLocation rl_first = info->args[0];
320    OpRegRegImm(kOpAdd, r_src, TargetReg(kSp), SRegOffset(rl_first.s_reg_low));
321    // Set up the target pointer
322    OpRegRegImm(kOpAdd, r_dst, TargetReg(kRet0),
323                mirror::Array::DataOffset(component_size).Int32Value());
324    // Set up the loop counter (known to be > 0)
325    LoadConstant(r_idx, elems - 1);
326    // Generate the copy loop.  Going backwards for convenience
327    LIR* target = NewLIR0(kPseudoTargetLabel);
328    // Copy next element
329    LoadBaseIndexed(r_src, r_idx, r_val, 2, kWord);
330    StoreBaseIndexed(r_dst, r_idx, r_val, 2, kWord);
331    FreeTemp(r_val);
332    OpDecAndBranch(kCondGe, r_idx, target);
333    if (cu_->instruction_set == kX86) {
334      // Restore the target pointer
335      OpRegRegImm(kOpAdd, TargetReg(kRet0), r_dst,
336                  -mirror::Array::DataOffset(component_size).Int32Value());
337    }
338  } else if (!info->is_range) {
339    // TUNING: interleave
340    for (int i = 0; i < elems; i++) {
341      RegLocation rl_arg = LoadValue(info->args[i], kCoreReg);
342      StoreBaseDisp(TargetReg(kRet0),
343                    mirror::Array::DataOffset(component_size).Int32Value() +
344                    i * 4, rl_arg.low_reg, kWord);
345      // If the LoadValue caused a temp to be allocated, free it
346      if (IsTemp(rl_arg.low_reg)) {
347        FreeTemp(rl_arg.low_reg);
348      }
349    }
350  }
351  if (info->result.location != kLocInvalid) {
352    StoreValue(info->result, GetReturn(false /* not fp */));
353  }
354}
355
356//
357// Slow path to ensure a class is initialized for sget/sput.
358//
359class StaticFieldSlowPath : public Mir2Lir::LIRSlowPath {
360 public:
361  StaticFieldSlowPath(Mir2Lir* m2l, LIR* unresolved, LIR* uninit, LIR* cont,
362           int storage_index, int r_base) :
363    LIRSlowPath(m2l, m2l->GetCurrentDexPc(), unresolved, cont), uninit_(uninit), storage_index_(storage_index),
364    r_base_(r_base) {
365  }
366
367  void Compile() {
368    LIR* unresolved_target = GenerateTargetLabel();
369    uninit_->target = unresolved_target;
370    m2l_->CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeStaticStorage),
371                            storage_index_, true);
372    // Copy helper's result into r_base, a no-op on all but MIPS.
373    m2l_->OpRegCopy(r_base_,  m2l_->TargetReg(kRet0));
374
375    m2l_->OpUnconditionalBranch(cont_);
376  }
377
378 private:
379  LIR* const uninit_;
380  const int storage_index_;
381  const int r_base_;
382};
383
384void Mir2Lir::GenSput(uint32_t field_idx, RegLocation rl_src, bool is_long_or_double,
385                      bool is_object) {
386  int field_offset;
387  int storage_index;
388  bool is_volatile;
389  bool is_referrers_class;
390  bool is_initialized;
391  bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
392      field_idx, mir_graph_->GetCurrentDexCompilationUnit(), true,
393      &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
394  if (fast_path && !SLOW_FIELD_PATH) {
395    DCHECK_GE(field_offset, 0);
396    int r_base;
397    if (is_referrers_class) {
398      // Fast path, static storage base is this method's class
399      RegLocation rl_method  = LoadCurrMethod();
400      r_base = AllocTemp();
401      LoadWordDisp(rl_method.low_reg,
402                   mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
403      if (IsTemp(rl_method.low_reg)) {
404        FreeTemp(rl_method.low_reg);
405      }
406    } else {
407      // Medium path, static storage base in a different class which requires checks that the other
408      // class is initialized.
409      // TODO: remove initialized check now that we are initializing classes in the compiler driver.
410      DCHECK_GE(storage_index, 0);
411      // May do runtime call so everything to home locations.
412      FlushAllRegs();
413      // Using fixed register to sync with possible call to runtime support.
414      int r_method = TargetReg(kArg1);
415      LockTemp(r_method);
416      LoadCurrMethodDirect(r_method);
417      r_base = TargetReg(kArg0);
418      LockTemp(r_base);
419      LoadWordDisp(r_method,
420                   mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
421                   r_base);
422      LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
423                   sizeof(int32_t*) * storage_index, r_base);
424      // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
425      if (!is_initialized) {
426        // Check if r_base is NULL or a not yet initialized class.
427
428        // The slow path is invoked if the r_base is NULL or the class pointed
429        // to by it is not initialized.
430        LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
431        int r_tmp = TargetReg(kArg2);
432        LockTemp(r_tmp);
433        LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
434                                          mirror::Class::StatusOffset().Int32Value(),
435                                          mirror::Class::kStatusInitialized, NULL);
436        LIR* cont = NewLIR0(kPseudoTargetLabel);
437
438        AddSlowPath(new (arena_) StaticFieldSlowPath(this,
439                                                     unresolved_branch, uninit_branch, cont,
440                                                     storage_index, r_base));
441
442        FreeTemp(r_tmp);
443      }
444      FreeTemp(r_method);
445    }
446    // rBase now holds static storage base
447    if (is_long_or_double) {
448      rl_src = LoadValueWide(rl_src, kAnyReg);
449    } else {
450      rl_src = LoadValue(rl_src, kAnyReg);
451    }
452    if (is_volatile) {
453      GenMemBarrier(kStoreStore);
454    }
455    if (is_long_or_double) {
456      StoreBaseDispWide(r_base, field_offset, rl_src.low_reg,
457                        rl_src.high_reg);
458    } else {
459      StoreWordDisp(r_base, field_offset, rl_src.low_reg);
460    }
461    if (is_volatile) {
462      GenMemBarrier(kStoreLoad);
463    }
464    if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
465      MarkGCCard(rl_src.low_reg, r_base);
466    }
467    FreeTemp(r_base);
468  } else {
469    FlushAllRegs();  // Everything to home locations
470    ThreadOffset setter_offset =
471        is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Static)
472                          : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjStatic)
473                                       : QUICK_ENTRYPOINT_OFFSET(pSet32Static));
474    CallRuntimeHelperImmRegLocation(setter_offset, field_idx, rl_src, true);
475  }
476}
477
478void Mir2Lir::GenSget(uint32_t field_idx, RegLocation rl_dest,
479                      bool is_long_or_double, bool is_object) {
480  int field_offset;
481  int storage_index;
482  bool is_volatile;
483  bool is_referrers_class;
484  bool is_initialized;
485  bool fast_path = cu_->compiler_driver->ComputeStaticFieldInfo(
486      field_idx, mir_graph_->GetCurrentDexCompilationUnit(), false,
487      &field_offset, &storage_index, &is_referrers_class, &is_volatile, &is_initialized);
488  if (fast_path && !SLOW_FIELD_PATH) {
489    DCHECK_GE(field_offset, 0);
490    int r_base;
491    if (is_referrers_class) {
492      // Fast path, static storage base is this method's class
493      RegLocation rl_method  = LoadCurrMethod();
494      r_base = AllocTemp();
495      LoadWordDisp(rl_method.low_reg,
496                   mirror::ArtMethod::DeclaringClassOffset().Int32Value(), r_base);
497    } else {
498      // Medium path, static storage base in a different class which requires checks that the other
499      // class is initialized
500      DCHECK_GE(storage_index, 0);
501      // May do runtime call so everything to home locations.
502      FlushAllRegs();
503      // Using fixed register to sync with possible call to runtime support.
504      int r_method = TargetReg(kArg1);
505      LockTemp(r_method);
506      LoadCurrMethodDirect(r_method);
507      r_base = TargetReg(kArg0);
508      LockTemp(r_base);
509      LoadWordDisp(r_method,
510                   mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
511                   r_base);
512      LoadWordDisp(r_base, mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
513                   sizeof(int32_t*) * storage_index, r_base);
514      // r_base now points at static storage (Class*) or NULL if the type is not yet resolved.
515      if (!is_initialized) {
516        // Check if r_base is NULL or a not yet initialized class.
517
518        // The slow path is invoked if the r_base is NULL or the class pointed
519        // to by it is not initialized.
520        LIR* unresolved_branch = OpCmpImmBranch(kCondEq, r_base, 0, NULL);
521        int r_tmp = TargetReg(kArg2);
522        LockTemp(r_tmp);
523        LIR* uninit_branch = OpCmpMemImmBranch(kCondLt, r_tmp, r_base,
524                                          mirror::Class::StatusOffset().Int32Value(),
525                                          mirror::Class::kStatusInitialized, NULL);
526        LIR* cont = NewLIR0(kPseudoTargetLabel);
527
528        AddSlowPath(new (arena_) StaticFieldSlowPath(this,
529                                                     unresolved_branch, uninit_branch, cont,
530                                                     storage_index, r_base));
531
532        FreeTemp(r_tmp);
533      }
534      FreeTemp(r_method);
535    }
536    // r_base now holds static storage base
537    RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
538    if (is_volatile) {
539      GenMemBarrier(kLoadLoad);
540    }
541    if (is_long_or_double) {
542      LoadBaseDispWide(r_base, field_offset, rl_result.low_reg,
543                       rl_result.high_reg, INVALID_SREG);
544    } else {
545      LoadWordDisp(r_base, field_offset, rl_result.low_reg);
546    }
547    FreeTemp(r_base);
548    if (is_long_or_double) {
549      StoreValueWide(rl_dest, rl_result);
550    } else {
551      StoreValue(rl_dest, rl_result);
552    }
553  } else {
554    FlushAllRegs();  // Everything to home locations
555    ThreadOffset getterOffset =
556        is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Static)
557                          :(is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjStatic)
558                                      : QUICK_ENTRYPOINT_OFFSET(pGet32Static));
559    CallRuntimeHelperImm(getterOffset, field_idx, true);
560    if (is_long_or_double) {
561      RegLocation rl_result = GetReturnWide(rl_dest.fp);
562      StoreValueWide(rl_dest, rl_result);
563    } else {
564      RegLocation rl_result = GetReturn(rl_dest.fp);
565      StoreValue(rl_dest, rl_result);
566    }
567  }
568}
569
570// Generate code for all slow paths.
571void Mir2Lir::HandleSlowPaths() {
572  int n = slow_paths_.Size();
573  for (int i = 0; i < n; ++i) {
574    LIRSlowPath* slowpath = slow_paths_.Get(i);
575    slowpath->Compile();
576  }
577  slow_paths_.Reset();
578}
579
580void Mir2Lir::HandleSuspendLaunchPads() {
581  int num_elems = suspend_launchpads_.Size();
582  ThreadOffset helper_offset = QUICK_ENTRYPOINT_OFFSET(pTestSuspend);
583  for (int i = 0; i < num_elems; i++) {
584    ResetRegPool();
585    ResetDefTracking();
586    LIR* lab = suspend_launchpads_.Get(i);
587    LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[0]));
588    current_dalvik_offset_ = lab->operands[1];
589    AppendLIR(lab);
590    int r_tgt = CallHelperSetup(helper_offset);
591    CallHelper(r_tgt, helper_offset, true /* MarkSafepointPC */);
592    OpUnconditionalBranch(resume_lab);
593  }
594}
595
596void Mir2Lir::HandleIntrinsicLaunchPads() {
597  int num_elems = intrinsic_launchpads_.Size();
598  for (int i = 0; i < num_elems; i++) {
599    ResetRegPool();
600    ResetDefTracking();
601    LIR* lab = intrinsic_launchpads_.Get(i);
602    CallInfo* info = reinterpret_cast<CallInfo*>(UnwrapPointer(lab->operands[0]));
603    current_dalvik_offset_ = info->offset;
604    AppendLIR(lab);
605    // NOTE: GenInvoke handles MarkSafepointPC
606    GenInvoke(info);
607    LIR* resume_lab = reinterpret_cast<LIR*>(UnwrapPointer(lab->operands[2]));
608    if (resume_lab != NULL) {
609      OpUnconditionalBranch(resume_lab);
610    }
611  }
612}
613
614void Mir2Lir::HandleThrowLaunchPads() {
615  int num_elems = throw_launchpads_.Size();
616  for (int i = 0; i < num_elems; i++) {
617    ResetRegPool();
618    ResetDefTracking();
619    LIR* lab = throw_launchpads_.Get(i);
620    current_dalvik_offset_ = lab->operands[1];
621    AppendLIR(lab);
622    ThreadOffset func_offset(-1);
623    int v1 = lab->operands[2];
624    int v2 = lab->operands[3];
625    bool target_x86 = (cu_->instruction_set == kX86);
626    switch (lab->operands[0]) {
627      case kThrowNullPointer:
628        func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowNullPointer);
629        break;
630      case kThrowConstantArrayBounds:  // v1 is length reg (for Arm/Mips), v2 constant index
631        // v1 holds the constant array index.  Mips/Arm uses v2 for length, x86 reloads.
632        if (target_x86) {
633          OpRegMem(kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
634        } else {
635          OpRegCopy(TargetReg(kArg1), v1);
636        }
637        // Make sure the following LoadConstant doesn't mess with kArg1.
638        LockTemp(TargetReg(kArg1));
639        LoadConstant(TargetReg(kArg0), v2);
640        func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
641        break;
642      case kThrowArrayBounds:
643        // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
644        if (v2 != TargetReg(kArg0)) {
645          OpRegCopy(TargetReg(kArg0), v1);
646          if (target_x86) {
647            // x86 leaves the array pointer in v2, so load the array length that the handler expects
648            OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
649          } else {
650            OpRegCopy(TargetReg(kArg1), v2);
651          }
652        } else {
653          if (v1 == TargetReg(kArg1)) {
654            // Swap v1 and v2, using kArg2 as a temp
655            OpRegCopy(TargetReg(kArg2), v1);
656            if (target_x86) {
657              // x86 leaves the array pointer in v2; load the array length that the handler expects
658              OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
659            } else {
660              OpRegCopy(TargetReg(kArg1), v2);
661            }
662            OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));
663          } else {
664            if (target_x86) {
665              // x86 leaves the array pointer in v2; load the array length that the handler expects
666              OpRegMem(kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
667            } else {
668              OpRegCopy(TargetReg(kArg1), v2);
669            }
670            OpRegCopy(TargetReg(kArg0), v1);
671          }
672        }
673        func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowArrayBounds);
674        break;
675      case kThrowDivZero:
676        func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowDivZero);
677        break;
678      case kThrowNoSuchMethod:
679        OpRegCopy(TargetReg(kArg0), v1);
680        func_offset =
681          QUICK_ENTRYPOINT_OFFSET(pThrowNoSuchMethod);
682        break;
683      case kThrowStackOverflow:
684        func_offset = QUICK_ENTRYPOINT_OFFSET(pThrowStackOverflow);
685        // Restore stack alignment
686        if (target_x86) {
687          OpRegImm(kOpAdd, TargetReg(kSp), frame_size_);
688        } else {
689          OpRegImm(kOpAdd, TargetReg(kSp), (num_core_spills_ + num_fp_spills_) * 4);
690        }
691        break;
692      default:
693        LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
694    }
695    ClobberCallerSave();
696    int r_tgt = CallHelperSetup(func_offset);
697    CallHelper(r_tgt, func_offset, true /* MarkSafepointPC */);
698  }
699}
700
701void Mir2Lir::GenIGet(uint32_t field_idx, int opt_flags, OpSize size,
702                      RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
703                      bool is_object) {
704  int field_offset;
705  bool is_volatile;
706
707  bool fast_path = FastInstance(field_idx, false, &field_offset, &is_volatile);
708
709  if (fast_path && !SLOW_FIELD_PATH) {
710    RegLocation rl_result;
711    RegisterClass reg_class = oat_reg_class_by_size(size);
712    DCHECK_GE(field_offset, 0);
713    rl_obj = LoadValue(rl_obj, kCoreReg);
714    if (is_long_or_double) {
715      DCHECK(rl_dest.wide);
716      GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
717      if (cu_->instruction_set == kX86) {
718        rl_result = EvalLoc(rl_dest, reg_class, true);
719        GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
720        LoadBaseDispWide(rl_obj.low_reg, field_offset, rl_result.low_reg,
721                         rl_result.high_reg, rl_obj.s_reg_low);
722        if (is_volatile) {
723          GenMemBarrier(kLoadLoad);
724        }
725      } else {
726        int reg_ptr = AllocTemp();
727        OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
728        rl_result = EvalLoc(rl_dest, reg_class, true);
729        LoadBaseDispWide(reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
730        if (is_volatile) {
731          GenMemBarrier(kLoadLoad);
732        }
733        FreeTemp(reg_ptr);
734      }
735      StoreValueWide(rl_dest, rl_result);
736    } else {
737      rl_result = EvalLoc(rl_dest, reg_class, true);
738      GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
739      LoadBaseDisp(rl_obj.low_reg, field_offset, rl_result.low_reg,
740                   kWord, rl_obj.s_reg_low);
741      if (is_volatile) {
742        GenMemBarrier(kLoadLoad);
743      }
744      StoreValue(rl_dest, rl_result);
745    }
746  } else {
747    ThreadOffset getterOffset =
748        is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pGet64Instance)
749                          : (is_object ? QUICK_ENTRYPOINT_OFFSET(pGetObjInstance)
750                                       : QUICK_ENTRYPOINT_OFFSET(pGet32Instance));
751    CallRuntimeHelperImmRegLocation(getterOffset, field_idx, rl_obj, true);
752    if (is_long_or_double) {
753      RegLocation rl_result = GetReturnWide(rl_dest.fp);
754      StoreValueWide(rl_dest, rl_result);
755    } else {
756      RegLocation rl_result = GetReturn(rl_dest.fp);
757      StoreValue(rl_dest, rl_result);
758    }
759  }
760}
761
762void Mir2Lir::GenIPut(uint32_t field_idx, int opt_flags, OpSize size,
763                      RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
764                      bool is_object) {
765  int field_offset;
766  bool is_volatile;
767
768  bool fast_path = FastInstance(field_idx, true, &field_offset, &is_volatile);
769  if (fast_path && !SLOW_FIELD_PATH) {
770    RegisterClass reg_class = oat_reg_class_by_size(size);
771    DCHECK_GE(field_offset, 0);
772    rl_obj = LoadValue(rl_obj, kCoreReg);
773    if (is_long_or_double) {
774      int reg_ptr;
775      rl_src = LoadValueWide(rl_src, kAnyReg);
776      GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
777      reg_ptr = AllocTemp();
778      OpRegRegImm(kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
779      if (is_volatile) {
780        GenMemBarrier(kStoreStore);
781      }
782      StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
783      if (is_volatile) {
784        GenMemBarrier(kLoadLoad);
785      }
786      FreeTemp(reg_ptr);
787    } else {
788      rl_src = LoadValue(rl_src, reg_class);
789      GenNullCheck(rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
790      if (is_volatile) {
791        GenMemBarrier(kStoreStore);
792      }
793      StoreBaseDisp(rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
794      if (is_volatile) {
795        GenMemBarrier(kLoadLoad);
796      }
797      if (is_object && !mir_graph_->IsConstantNullRef(rl_src)) {
798        MarkGCCard(rl_src.low_reg, rl_obj.low_reg);
799      }
800    }
801  } else {
802    ThreadOffset setter_offset =
803        is_long_or_double ? QUICK_ENTRYPOINT_OFFSET(pSet64Instance)
804                          : (is_object ? QUICK_ENTRYPOINT_OFFSET(pSetObjInstance)
805                                       : QUICK_ENTRYPOINT_OFFSET(pSet32Instance));
806    CallRuntimeHelperImmRegLocationRegLocation(setter_offset, field_idx, rl_obj, rl_src, true);
807  }
808}
809
810void Mir2Lir::GenArrayObjPut(int opt_flags, RegLocation rl_array, RegLocation rl_index,
811                             RegLocation rl_src) {
812  bool needs_range_check = !(opt_flags & MIR_IGNORE_RANGE_CHECK);
813  bool needs_null_check = !((cu_->disable_opt & (1 << kNullCheckElimination)) &&
814      (opt_flags & MIR_IGNORE_NULL_CHECK));
815  ThreadOffset helper = needs_range_check
816      ? (needs_null_check ? QUICK_ENTRYPOINT_OFFSET(pAputObjectWithNullAndBoundCheck)
817                          : QUICK_ENTRYPOINT_OFFSET(pAputObjectWithBoundCheck))
818      : QUICK_ENTRYPOINT_OFFSET(pAputObject);
819  CallRuntimeHelperRegLocationRegLocationRegLocation(helper, rl_array, rl_index, rl_src, true);
820}
821
822void Mir2Lir::GenConstClass(uint32_t type_idx, RegLocation rl_dest) {
823  RegLocation rl_method = LoadCurrMethod();
824  int res_reg = AllocTemp();
825  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
826  if (!cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
827                                                   *cu_->dex_file,
828                                                   type_idx)) {
829    // Call out to helper which resolves type and verifies access.
830    // Resolved type returned in kRet0.
831    CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
832                            type_idx, rl_method.low_reg, true);
833    RegLocation rl_result = GetReturn(false);
834    StoreValue(rl_dest, rl_result);
835  } else {
836    // We're don't need access checks, load type from dex cache
837    int32_t dex_cache_offset =
838        mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value();
839    LoadWordDisp(rl_method.low_reg, dex_cache_offset, res_reg);
840    int32_t offset_of_type =
841        mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
842                          * type_idx);
843    LoadWordDisp(res_reg, offset_of_type, rl_result.low_reg);
844    if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file,
845        type_idx) || SLOW_TYPE_PATH) {
846      // Slow path, at runtime test if type is null and if so initialize
847      FlushAllRegs();
848      LIR* branch = OpCmpImmBranch(kCondEq, rl_result.low_reg, 0, NULL);
849      LIR* cont = NewLIR0(kPseudoTargetLabel);
850
851      // Object to generate the slow path for class resolution.
852      class SlowPath : public LIRSlowPath {
853       public:
854        SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
855                 const RegLocation& rl_method, const RegLocation& rl_result) :
856                   LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
857                   rl_method_(rl_method), rl_result_(rl_result) {
858        }
859
860        void Compile() {
861          GenerateTargetLabel();
862
863          m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
864                                        rl_method_.low_reg, true);
865          m2l_->OpRegCopy(rl_result_.low_reg,  m2l_->TargetReg(kRet0));
866
867          m2l_->OpUnconditionalBranch(cont_);
868        }
869
870       private:
871        const int type_idx_;
872        const RegLocation rl_method_;
873        const RegLocation rl_result_;
874      };
875
876      // Add to list for future.
877      AddSlowPath(new (arena_) SlowPath(this, branch, cont,
878                                        type_idx, rl_method, rl_result));
879
880      StoreValue(rl_dest, rl_result);
881     } else {
882      // Fast path, we're done - just store result
883      StoreValue(rl_dest, rl_result);
884    }
885  }
886}
887
888void Mir2Lir::GenConstString(uint32_t string_idx, RegLocation rl_dest) {
889  /* NOTE: Most strings should be available at compile time */
890  int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
891                 (sizeof(mirror::String*) * string_idx);
892  if (!cu_->compiler_driver->CanAssumeStringIsPresentInDexCache(
893      *cu_->dex_file, string_idx) || SLOW_STRING_PATH) {
894    // slow path, resolve string if not in dex cache
895    FlushAllRegs();
896    LockCallTemps();  // Using explicit registers
897
898    // If the Method* is already in a register, we can save a copy.
899    RegLocation rl_method = mir_graph_->GetMethodLoc();
900    int r_method;
901    if (rl_method.location == kLocPhysReg) {
902      // A temp would conflict with register use below.
903      DCHECK(!IsTemp(rl_method.low_reg));
904      r_method = rl_method.low_reg;
905    } else {
906      r_method = TargetReg(kArg2);
907      LoadCurrMethodDirect(r_method);
908    }
909    LoadWordDisp(r_method, mirror::ArtMethod::DexCacheStringsOffset().Int32Value(),
910                 TargetReg(kArg0));
911
912    // Might call out to helper, which will return resolved string in kRet0
913    LoadWordDisp(TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
914    if (cu_->instruction_set == kThumb2 ||
915        cu_->instruction_set == kMips) {
916      //  OpRegImm(kOpCmp, TargetReg(kRet0), 0);  // Is resolved?
917      LoadConstant(TargetReg(kArg1), string_idx);
918      LIR* fromfast = OpCmpImmBranch(kCondEq, TargetReg(kRet0), 0, NULL);
919      LIR* cont = NewLIR0(kPseudoTargetLabel);
920      GenBarrier();
921
922      // Object to generate the slow path for string resolution.
923      class SlowPath : public LIRSlowPath {
924       public:
925        SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, int r_method) :
926          LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), r_method_(r_method) {
927        }
928
929        void Compile() {
930          GenerateTargetLabel();
931
932          int r_tgt = m2l_->CallHelperSetup(QUICK_ENTRYPOINT_OFFSET(pResolveString));
933
934          m2l_->OpRegCopy(m2l_->TargetReg(kArg0), r_method_);   // .eq
935          LIR* call_inst = m2l_->OpReg(kOpBlx, r_tgt);
936          m2l_->MarkSafepointPC(call_inst);
937          m2l_->FreeTemp(r_tgt);
938
939          m2l_->OpUnconditionalBranch(cont_);
940        }
941
942       private:
943         int r_method_;
944      };
945
946      // Add to list for future.
947      AddSlowPath(new (arena_) SlowPath(this, fromfast, cont, r_method));
948    } else {
949      DCHECK_EQ(cu_->instruction_set, kX86);
950      LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kRet0), 0, NULL);
951      LoadConstant(TargetReg(kArg1), string_idx);
952      CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pResolveString), r_method,
953                              TargetReg(kArg1), true);
954      LIR* target = NewLIR0(kPseudoTargetLabel);
955      branch->target = target;
956    }
957    GenBarrier();
958    StoreValue(rl_dest, GetReturn(false));
959  } else {
960    RegLocation rl_method = LoadCurrMethod();
961    int res_reg = AllocTemp();
962    RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
963    LoadWordDisp(rl_method.low_reg,
964                 mirror::ArtMethod::DexCacheStringsOffset().Int32Value(), res_reg);
965    LoadWordDisp(res_reg, offset_of_string, rl_result.low_reg);
966    StoreValue(rl_dest, rl_result);
967  }
968}
969
970/*
971 * Let helper function take care of everything.  Will
972 * call Class::NewInstanceFromCode(type_idx, method);
973 */
974void Mir2Lir::GenNewInstance(uint32_t type_idx, RegLocation rl_dest) {
975  FlushAllRegs();  /* Everything to home location */
976  // alloc will always check for resolution, do we also need to verify
977  // access because the verifier was unable to?
978  ThreadOffset func_offset(-1);
979  const DexFile* dex_file = cu_->dex_file;
980  CompilerDriver* driver = cu_->compiler_driver;
981  if (driver->CanAccessInstantiableTypeWithoutChecks(
982      cu_->method_idx, *dex_file, type_idx)) {
983    bool is_type_initialized;
984    bool use_direct_type_ptr;
985    uintptr_t direct_type_ptr;
986    if (kEmbedClassInCode &&
987        driver->CanEmbedTypeInCode(*dex_file, type_idx,
988                                   &is_type_initialized, &use_direct_type_ptr, &direct_type_ptr)) {
989      // The fast path.
990      if (!use_direct_type_ptr) {
991        LoadClassType(type_idx, kArg0);
992        if (!is_type_initialized) {
993          func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
994          CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
995        } else {
996          func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
997          CallRuntimeHelperRegMethod(func_offset, TargetReg(kArg0), true);
998        }
999      } else {
1000        // Use the direct pointer.
1001        if (!is_type_initialized) {
1002          func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectResolved);
1003          CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1004        } else {
1005          func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectInitialized);
1006          CallRuntimeHelperImmMethod(func_offset, direct_type_ptr, true);
1007        }
1008      }
1009    } else {
1010      // The slow path.
1011      DCHECK_EQ(func_offset.Int32Value(), -1);
1012      func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObject);
1013      CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1014    }
1015    DCHECK_NE(func_offset.Int32Value(), -1);
1016  } else {
1017    func_offset = QUICK_ENTRYPOINT_OFFSET(pAllocObjectWithAccessCheck);
1018    CallRuntimeHelperImmMethod(func_offset, type_idx, true);
1019  }
1020  RegLocation rl_result = GetReturn(false);
1021  StoreValue(rl_dest, rl_result);
1022}
1023
1024void Mir2Lir::GenThrow(RegLocation rl_src) {
1025  FlushAllRegs();
1026  CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
1027}
1028
1029// For final classes there are no sub-classes to check and so we can answer the instance-of
1030// question with simple comparisons.
1031void Mir2Lir::GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
1032                                 RegLocation rl_src) {
1033  // X86 has its own implementation.
1034  DCHECK_NE(cu_->instruction_set, kX86);
1035
1036  RegLocation object = LoadValue(rl_src, kCoreReg);
1037  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1038  int result_reg = rl_result.low_reg;
1039  if (result_reg == object.low_reg) {
1040    result_reg = AllocTypedTemp(false, kCoreReg);
1041  }
1042  LoadConstant(result_reg, 0);     // assume false
1043  LIR* null_branchover = OpCmpImmBranch(kCondEq, object.low_reg, 0, NULL);
1044
1045  int check_class = AllocTypedTemp(false, kCoreReg);
1046  int object_class = AllocTypedTemp(false, kCoreReg);
1047
1048  LoadCurrMethodDirect(check_class);
1049  if (use_declaring_class) {
1050    LoadWordDisp(check_class, mirror::ArtMethod::DeclaringClassOffset().Int32Value(),
1051                 check_class);
1052    LoadWordDisp(object.low_reg,  mirror::Object::ClassOffset().Int32Value(), object_class);
1053  } else {
1054    LoadWordDisp(check_class, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(),
1055                 check_class);
1056    LoadWordDisp(object.low_reg,  mirror::Object::ClassOffset().Int32Value(), object_class);
1057    int32_t offset_of_type =
1058      mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1059      (sizeof(mirror::Class*) * type_idx);
1060    LoadWordDisp(check_class, offset_of_type, check_class);
1061  }
1062
1063  LIR* ne_branchover = NULL;
1064  if (cu_->instruction_set == kThumb2) {
1065    OpRegReg(kOpCmp, check_class, object_class);  // Same?
1066    OpIT(kCondEq, "");   // if-convert the test
1067    LoadConstant(result_reg, 1);     // .eq case - load true
1068  } else {
1069    ne_branchover = OpCmpBranch(kCondNe, check_class, object_class, NULL);
1070    LoadConstant(result_reg, 1);     // eq case - load true
1071  }
1072  LIR* target = NewLIR0(kPseudoTargetLabel);
1073  null_branchover->target = target;
1074  if (ne_branchover != NULL) {
1075    ne_branchover->target = target;
1076  }
1077  FreeTemp(object_class);
1078  FreeTemp(check_class);
1079  if (IsTemp(result_reg)) {
1080    OpRegCopy(rl_result.low_reg, result_reg);
1081    FreeTemp(result_reg);
1082  }
1083  StoreValue(rl_dest, rl_result);
1084}
1085
1086void Mir2Lir::GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
1087                                         bool type_known_abstract, bool use_declaring_class,
1088                                         bool can_assume_type_is_in_dex_cache,
1089                                         uint32_t type_idx, RegLocation rl_dest,
1090                                         RegLocation rl_src) {
1091  // X86 has its own implementation.
1092  DCHECK_NE(cu_->instruction_set, kX86);
1093
1094  FlushAllRegs();
1095  // May generate a call - use explicit registers
1096  LockCallTemps();
1097  LoadCurrMethodDirect(TargetReg(kArg1));  // kArg1 <= current Method*
1098  int class_reg = TargetReg(kArg2);  // kArg2 will hold the Class*
1099  if (needs_access_check) {
1100    // Check we have access to type_idx and if not throw IllegalAccessError,
1101    // returns Class* in kArg0
1102    CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
1103                         type_idx, true);
1104    OpRegCopy(class_reg, TargetReg(kRet0));  // Align usage with fast path
1105    LoadValueDirectFixed(rl_src, TargetReg(kArg0));  // kArg0 <= ref
1106  } else if (use_declaring_class) {
1107    LoadValueDirectFixed(rl_src, TargetReg(kArg0));  // kArg0 <= ref
1108    LoadWordDisp(TargetReg(kArg1),
1109                 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
1110  } else {
1111    // Load dex cache entry into class_reg (kArg2)
1112    LoadValueDirectFixed(rl_src, TargetReg(kArg0));  // kArg0 <= ref
1113    LoadWordDisp(TargetReg(kArg1),
1114                 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
1115    int32_t offset_of_type =
1116        mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
1117        * type_idx);
1118    LoadWordDisp(class_reg, offset_of_type, class_reg);
1119    if (!can_assume_type_is_in_dex_cache) {
1120      // Need to test presence of type in dex cache at runtime
1121      LIR* hop_branch = OpCmpImmBranch(kCondNe, class_reg, 0, NULL);
1122      // Not resolved
1123      // Call out to helper, which will return resolved type in kRet0
1124      CallRuntimeHelperImm(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx, true);
1125      OpRegCopy(TargetReg(kArg2), TargetReg(kRet0));  // Align usage with fast path
1126      LoadValueDirectFixed(rl_src, TargetReg(kArg0));  /* reload Ref */
1127      // Rejoin code paths
1128      LIR* hop_target = NewLIR0(kPseudoTargetLabel);
1129      hop_branch->target = hop_target;
1130    }
1131  }
1132  /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
1133  RegLocation rl_result = GetReturn(false);
1134  if (cu_->instruction_set == kMips) {
1135    // On MIPS rArg0 != rl_result, place false in result if branch is taken.
1136    LoadConstant(rl_result.low_reg, 0);
1137  }
1138  LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1139
1140  /* load object->klass_ */
1141  DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1142  LoadWordDisp(TargetReg(kArg0),  mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
1143  /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
1144  LIR* branchover = NULL;
1145  if (type_known_final) {
1146    // rl_result == ref == null == 0.
1147    if (cu_->instruction_set == kThumb2) {
1148      OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2));  // Same?
1149      OpIT(kCondEq, "E");   // if-convert the test
1150      LoadConstant(rl_result.low_reg, 1);     // .eq case - load true
1151      LoadConstant(rl_result.low_reg, 0);     // .ne case - load false
1152    } else {
1153      LoadConstant(rl_result.low_reg, 0);     // ne case - load false
1154      branchover = OpCmpBranch(kCondNe, TargetReg(kArg1), TargetReg(kArg2), NULL);
1155      LoadConstant(rl_result.low_reg, 1);     // eq case - load true
1156    }
1157  } else {
1158    if (cu_->instruction_set == kThumb2) {
1159      int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1160      if (!type_known_abstract) {
1161      /* Uses conditional nullification */
1162        OpRegReg(kOpCmp, TargetReg(kArg1), TargetReg(kArg2));  // Same?
1163        OpIT(kCondEq, "EE");   // if-convert the test
1164        LoadConstant(TargetReg(kArg0), 1);     // .eq case - load true
1165      }
1166      OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));    // .ne case - arg0 <= class
1167      OpReg(kOpBlx, r_tgt);    // .ne case: helper(class, ref->class)
1168      FreeTemp(r_tgt);
1169    } else {
1170      if (!type_known_abstract) {
1171        /* Uses branchovers */
1172        LoadConstant(rl_result.low_reg, 1);     // assume true
1173        branchover = OpCmpBranch(kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1174      }
1175      int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pInstanceofNonTrivial));
1176      OpRegCopy(TargetReg(kArg0), TargetReg(kArg2));    // .ne case - arg0 <= class
1177      OpReg(kOpBlx, r_tgt);    // .ne case: helper(class, ref->class)
1178      FreeTemp(r_tgt);
1179    }
1180  }
1181  // TODO: only clobber when type isn't final?
1182  ClobberCallerSave();
1183  /* branch targets here */
1184  LIR* target = NewLIR0(kPseudoTargetLabel);
1185  StoreValue(rl_dest, rl_result);
1186  branch1->target = target;
1187  if (branchover != NULL) {
1188    branchover->target = target;
1189  }
1190}
1191
1192void Mir2Lir::GenInstanceof(uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src) {
1193  bool type_known_final, type_known_abstract, use_declaring_class;
1194  bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1195                                                                              *cu_->dex_file,
1196                                                                              type_idx,
1197                                                                              &type_known_final,
1198                                                                              &type_known_abstract,
1199                                                                              &use_declaring_class);
1200  bool can_assume_type_is_in_dex_cache = !needs_access_check &&
1201      cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx);
1202
1203  if ((use_declaring_class || can_assume_type_is_in_dex_cache) && type_known_final) {
1204    GenInstanceofFinal(use_declaring_class, type_idx, rl_dest, rl_src);
1205  } else {
1206    GenInstanceofCallingHelper(needs_access_check, type_known_final, type_known_abstract,
1207                               use_declaring_class, can_assume_type_is_in_dex_cache,
1208                               type_idx, rl_dest, rl_src);
1209  }
1210}
1211
1212void Mir2Lir::GenCheckCast(uint32_t insn_idx, uint32_t type_idx, RegLocation rl_src) {
1213  bool type_known_final, type_known_abstract, use_declaring_class;
1214  bool needs_access_check = !cu_->compiler_driver->CanAccessTypeWithoutChecks(cu_->method_idx,
1215                                                                              *cu_->dex_file,
1216                                                                              type_idx,
1217                                                                              &type_known_final,
1218                                                                              &type_known_abstract,
1219                                                                              &use_declaring_class);
1220  // Note: currently type_known_final is unused, as optimizing will only improve the performance
1221  // of the exception throw path.
1222  DexCompilationUnit* cu = mir_graph_->GetCurrentDexCompilationUnit();
1223  if (!needs_access_check && cu_->compiler_driver->IsSafeCast(cu, insn_idx)) {
1224    // Verifier type analysis proved this check cast would never cause an exception.
1225    return;
1226  }
1227  FlushAllRegs();
1228  // May generate a call - use explicit registers
1229  LockCallTemps();
1230  LoadCurrMethodDirect(TargetReg(kArg1));  // kArg1 <= current Method*
1231  int class_reg = TargetReg(kArg2);  // kArg2 will hold the Class*
1232  if (needs_access_check) {
1233    // Check we have access to type_idx and if not throw IllegalAccessError,
1234    // returns Class* in kRet0
1235    // InitializeTypeAndVerifyAccess(idx, method)
1236    CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccess),
1237                            type_idx, TargetReg(kArg1), true);
1238    OpRegCopy(class_reg, TargetReg(kRet0));  // Align usage with fast path
1239  } else if (use_declaring_class) {
1240    LoadWordDisp(TargetReg(kArg1),
1241                 mirror::ArtMethod::DeclaringClassOffset().Int32Value(), class_reg);
1242  } else {
1243    // Load dex cache entry into class_reg (kArg2)
1244    LoadWordDisp(TargetReg(kArg1),
1245                 mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
1246    int32_t offset_of_type =
1247        mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1248        (sizeof(mirror::Class*) * type_idx);
1249    LoadWordDisp(class_reg, offset_of_type, class_reg);
1250    if (!cu_->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu_->dex_file, type_idx)) {
1251      // Need to test presence of type in dex cache at runtime
1252      LIR* hop_branch = OpCmpImmBranch(kCondEq, class_reg, 0, NULL);
1253      LIR* cont = NewLIR0(kPseudoTargetLabel);
1254
1255      // Slow path to initialize the type.  Executed if the type is NULL.
1256      class SlowPath : public LIRSlowPath {
1257       public:
1258        SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, const int type_idx,
1259                 const int class_reg) :
1260                   LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), type_idx_(type_idx),
1261                   class_reg_(class_reg) {
1262        }
1263
1264        void Compile() {
1265          GenerateTargetLabel();
1266
1267          // Call out to helper, which will return resolved type in kArg0
1268          // InitializeTypeFromCode(idx, method)
1269          m2l_->CallRuntimeHelperImmReg(QUICK_ENTRYPOINT_OFFSET(pInitializeType), type_idx_,
1270                                        m2l_->TargetReg(kArg1), true);
1271          m2l_->OpRegCopy(class_reg_, m2l_->TargetReg(kRet0));  // Align usage with fast path
1272          m2l_->OpUnconditionalBranch(cont_);
1273        }
1274       public:
1275        const int type_idx_;
1276        const int class_reg_;
1277      };
1278
1279      AddSlowPath(new (arena_) SlowPath(this, hop_branch, cont,
1280                                        type_idx, class_reg));
1281    }
1282  }
1283  // At this point, class_reg (kArg2) has class
1284  LoadValueDirectFixed(rl_src, TargetReg(kArg0));  // kArg0 <= ref
1285
1286  // Slow path for the case where the classes are not equal.  In this case we need
1287  // to call a helper function to do the check.
1288  class SlowPath : public LIRSlowPath {
1289   public:
1290    SlowPath(Mir2Lir* m2l, LIR* fromfast, LIR* cont, bool load):
1291               LIRSlowPath(m2l, m2l->GetCurrentDexPc(), fromfast, cont), load_(load) {
1292    }
1293
1294    void Compile() {
1295      GenerateTargetLabel();
1296
1297      if (load_) {
1298        m2l_->LoadWordDisp(m2l_->TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1299                           m2l_->TargetReg(kArg1));
1300      }
1301      m2l_->CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pCheckCast), m2l_->TargetReg(kArg2),
1302                                    m2l_->TargetReg(kArg1), true);
1303
1304      m2l_->OpUnconditionalBranch(cont_);
1305    }
1306
1307   private:
1308    bool load_;
1309  };
1310
1311  if (type_known_abstract) {
1312    // Easier case, run slow path if target is non-null (slow path will load from target)
1313    LIR* branch = OpCmpImmBranch(kCondNe, TargetReg(kArg0), 0, NULL);
1314    LIR* cont = NewLIR0(kPseudoTargetLabel);
1315    AddSlowPath(new (arena_) SlowPath(this, branch, cont, true));
1316  } else {
1317    // Harder, more common case.  We need to generate a forward branch over the load
1318    // if the target is null.  If it's non-null we perform the load and branch to the
1319    // slow path if the classes are not equal.
1320
1321    /* Null is OK - continue */
1322    LIR* branch1 = OpCmpImmBranch(kCondEq, TargetReg(kArg0), 0, NULL);
1323    /* load object->klass_ */
1324    DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1325    LoadWordDisp(TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(),
1326                    TargetReg(kArg1));
1327
1328    LIR* branch2 = OpCmpBranch(kCondNe, TargetReg(kArg1), class_reg, NULL);
1329    LIR* cont = NewLIR0(kPseudoTargetLabel);
1330
1331    // Add the slow path that will not perform load since this is already done.
1332    AddSlowPath(new (arena_) SlowPath(this, branch2, cont, false));
1333
1334    // Set the null check to branch to the continuation.
1335    branch1->target = cont;
1336  }
1337}
1338
1339void Mir2Lir::GenLong3Addr(OpKind first_op, OpKind second_op, RegLocation rl_dest,
1340                           RegLocation rl_src1, RegLocation rl_src2) {
1341  RegLocation rl_result;
1342  if (cu_->instruction_set == kThumb2) {
1343    /*
1344     * NOTE:  This is the one place in the code in which we might have
1345     * as many as six live temporary registers.  There are 5 in the normal
1346     * set for Arm.  Until we have spill capabilities, temporarily add
1347     * lr to the temp set.  It is safe to do this locally, but note that
1348     * lr is used explicitly elsewhere in the code generator and cannot
1349     * normally be used as a general temp register.
1350     */
1351    MarkTemp(TargetReg(kLr));   // Add lr to the temp pool
1352    FreeTemp(TargetReg(kLr));   // and make it available
1353  }
1354  rl_src1 = LoadValueWide(rl_src1, kCoreReg);
1355  rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1356  rl_result = EvalLoc(rl_dest, kCoreReg, true);
1357  // The longs may overlap - use intermediate temp if so
1358  if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)) {
1359    int t_reg = AllocTemp();
1360    OpRegRegReg(first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1361    OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1362    OpRegCopy(rl_result.low_reg, t_reg);
1363    FreeTemp(t_reg);
1364  } else {
1365    OpRegRegReg(first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1366    OpRegRegReg(second_op, rl_result.high_reg, rl_src1.high_reg,
1367                rl_src2.high_reg);
1368  }
1369  /*
1370   * NOTE: If rl_dest refers to a frame variable in a large frame, the
1371   * following StoreValueWide might need to allocate a temp register.
1372   * To further work around the lack of a spill capability, explicitly
1373   * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
1374   * Remove when spill is functional.
1375   */
1376  FreeRegLocTemps(rl_result, rl_src1);
1377  FreeRegLocTemps(rl_result, rl_src2);
1378  StoreValueWide(rl_dest, rl_result);
1379  if (cu_->instruction_set == kThumb2) {
1380    Clobber(TargetReg(kLr));
1381    UnmarkTemp(TargetReg(kLr));  // Remove lr from the temp pool
1382  }
1383}
1384
1385
1386void Mir2Lir::GenShiftOpLong(Instruction::Code opcode, RegLocation rl_dest,
1387                             RegLocation rl_src1, RegLocation rl_shift) {
1388  ThreadOffset func_offset(-1);
1389
1390  switch (opcode) {
1391    case Instruction::SHL_LONG:
1392    case Instruction::SHL_LONG_2ADDR:
1393      func_offset = QUICK_ENTRYPOINT_OFFSET(pShlLong);
1394      break;
1395    case Instruction::SHR_LONG:
1396    case Instruction::SHR_LONG_2ADDR:
1397      func_offset = QUICK_ENTRYPOINT_OFFSET(pShrLong);
1398      break;
1399    case Instruction::USHR_LONG:
1400    case Instruction::USHR_LONG_2ADDR:
1401      func_offset = QUICK_ENTRYPOINT_OFFSET(pUshrLong);
1402      break;
1403    default:
1404      LOG(FATAL) << "Unexpected case";
1405  }
1406  FlushAllRegs();   /* Send everything to home location */
1407  CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_shift, false);
1408  RegLocation rl_result = GetReturnWide(false);
1409  StoreValueWide(rl_dest, rl_result);
1410}
1411
1412
1413void Mir2Lir::GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest,
1414                            RegLocation rl_src1, RegLocation rl_src2) {
1415  DCHECK_NE(cu_->instruction_set, kX86);
1416  OpKind op = kOpBkpt;
1417  bool is_div_rem = false;
1418  bool check_zero = false;
1419  bool unary = false;
1420  RegLocation rl_result;
1421  bool shift_op = false;
1422  switch (opcode) {
1423    case Instruction::NEG_INT:
1424      op = kOpNeg;
1425      unary = true;
1426      break;
1427    case Instruction::NOT_INT:
1428      op = kOpMvn;
1429      unary = true;
1430      break;
1431    case Instruction::ADD_INT:
1432    case Instruction::ADD_INT_2ADDR:
1433      op = kOpAdd;
1434      break;
1435    case Instruction::SUB_INT:
1436    case Instruction::SUB_INT_2ADDR:
1437      op = kOpSub;
1438      break;
1439    case Instruction::MUL_INT:
1440    case Instruction::MUL_INT_2ADDR:
1441      op = kOpMul;
1442      break;
1443    case Instruction::DIV_INT:
1444    case Instruction::DIV_INT_2ADDR:
1445      check_zero = true;
1446      op = kOpDiv;
1447      is_div_rem = true;
1448      break;
1449    /* NOTE: returns in kArg1 */
1450    case Instruction::REM_INT:
1451    case Instruction::REM_INT_2ADDR:
1452      check_zero = true;
1453      op = kOpRem;
1454      is_div_rem = true;
1455      break;
1456    case Instruction::AND_INT:
1457    case Instruction::AND_INT_2ADDR:
1458      op = kOpAnd;
1459      break;
1460    case Instruction::OR_INT:
1461    case Instruction::OR_INT_2ADDR:
1462      op = kOpOr;
1463      break;
1464    case Instruction::XOR_INT:
1465    case Instruction::XOR_INT_2ADDR:
1466      op = kOpXor;
1467      break;
1468    case Instruction::SHL_INT:
1469    case Instruction::SHL_INT_2ADDR:
1470      shift_op = true;
1471      op = kOpLsl;
1472      break;
1473    case Instruction::SHR_INT:
1474    case Instruction::SHR_INT_2ADDR:
1475      shift_op = true;
1476      op = kOpAsr;
1477      break;
1478    case Instruction::USHR_INT:
1479    case Instruction::USHR_INT_2ADDR:
1480      shift_op = true;
1481      op = kOpLsr;
1482      break;
1483    default:
1484      LOG(FATAL) << "Invalid word arith op: " << opcode;
1485  }
1486  if (!is_div_rem) {
1487    if (unary) {
1488      rl_src1 = LoadValue(rl_src1, kCoreReg);
1489      rl_result = EvalLoc(rl_dest, kCoreReg, true);
1490      OpRegReg(op, rl_result.low_reg, rl_src1.low_reg);
1491    } else {
1492      if (shift_op) {
1493        int t_reg = INVALID_REG;
1494        rl_src2 = LoadValue(rl_src2, kCoreReg);
1495        t_reg = AllocTemp();
1496        OpRegRegImm(kOpAnd, t_reg, rl_src2.low_reg, 31);
1497        rl_src1 = LoadValue(rl_src1, kCoreReg);
1498        rl_result = EvalLoc(rl_dest, kCoreReg, true);
1499        OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1500        FreeTemp(t_reg);
1501      } else {
1502        rl_src1 = LoadValue(rl_src1, kCoreReg);
1503        rl_src2 = LoadValue(rl_src2, kCoreReg);
1504        rl_result = EvalLoc(rl_dest, kCoreReg, true);
1505        OpRegRegReg(op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1506      }
1507    }
1508    StoreValue(rl_dest, rl_result);
1509  } else {
1510    bool done = false;      // Set to true if we happen to find a way to use a real instruction.
1511    if (cu_->instruction_set == kMips) {
1512      rl_src1 = LoadValue(rl_src1, kCoreReg);
1513      rl_src2 = LoadValue(rl_src2, kCoreReg);
1514      if (check_zero) {
1515          GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1516      }
1517      rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
1518      done = true;
1519    } else if (cu_->instruction_set == kThumb2) {
1520      if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1521        // Use ARM SDIV instruction for division.  For remainder we also need to
1522        // calculate using a MUL and subtract.
1523        rl_src1 = LoadValue(rl_src1, kCoreReg);
1524        rl_src2 = LoadValue(rl_src2, kCoreReg);
1525        if (check_zero) {
1526            GenImmedCheck(kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
1527        }
1528        rl_result = GenDivRem(rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
1529        done = true;
1530      }
1531    }
1532
1533    // If we haven't already generated the code use the callout function.
1534    if (!done) {
1535      ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
1536      FlushAllRegs();   /* Send everything to home location */
1537      LoadValueDirectFixed(rl_src2, TargetReg(kArg1));
1538      int r_tgt = CallHelperSetup(func_offset);
1539      LoadValueDirectFixed(rl_src1, TargetReg(kArg0));
1540      if (check_zero) {
1541        GenImmedCheck(kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
1542      }
1543      // NOTE: callout here is not a safepoint.
1544      CallHelper(r_tgt, func_offset, false /* not a safepoint */);
1545      if (op == kOpDiv)
1546        rl_result = GetReturn(false);
1547      else
1548        rl_result = GetReturnAlt();
1549    }
1550    StoreValue(rl_dest, rl_result);
1551  }
1552}
1553
1554/*
1555 * The following are the first-level codegen routines that analyze the format
1556 * of each bytecode then either dispatch special purpose codegen routines
1557 * or produce corresponding Thumb instructions directly.
1558 */
1559
1560// Returns true if no more than two bits are set in 'x'.
1561static bool IsPopCountLE2(unsigned int x) {
1562  x &= x - 1;
1563  return (x & (x - 1)) == 0;
1564}
1565
1566// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1567// and store the result in 'rl_dest'.
1568bool Mir2Lir::HandleEasyDivRem(Instruction::Code dalvik_opcode, bool is_div,
1569                               RegLocation rl_src, RegLocation rl_dest, int lit) {
1570  if ((lit < 2) || ((cu_->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
1571    return false;
1572  }
1573  // No divide instruction for Arm, so check for more special cases
1574  if ((cu_->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
1575    return SmallLiteralDivRem(dalvik_opcode, is_div, rl_src, rl_dest, lit);
1576  }
1577  int k = LowestSetBit(lit);
1578  if (k >= 30) {
1579    // Avoid special cases.
1580    return false;
1581  }
1582  rl_src = LoadValue(rl_src, kCoreReg);
1583  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1584  if (is_div) {
1585    int t_reg = AllocTemp();
1586    if (lit == 2) {
1587      // Division by 2 is by far the most common division by constant.
1588      OpRegRegImm(kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1589      OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1590      OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1591    } else {
1592      OpRegRegImm(kOpAsr, t_reg, rl_src.low_reg, 31);
1593      OpRegRegImm(kOpLsr, t_reg, t_reg, 32 - k);
1594      OpRegRegReg(kOpAdd, t_reg, t_reg, rl_src.low_reg);
1595      OpRegRegImm(kOpAsr, rl_result.low_reg, t_reg, k);
1596    }
1597  } else {
1598    int t_reg1 = AllocTemp();
1599    int t_reg2 = AllocTemp();
1600    if (lit == 2) {
1601      OpRegRegImm(kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1602      OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1603      OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit -1);
1604      OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1605    } else {
1606      OpRegRegImm(kOpAsr, t_reg1, rl_src.low_reg, 31);
1607      OpRegRegImm(kOpLsr, t_reg1, t_reg1, 32 - k);
1608      OpRegRegReg(kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1609      OpRegRegImm(kOpAnd, t_reg2, t_reg2, lit - 1);
1610      OpRegRegReg(kOpSub, rl_result.low_reg, t_reg2, t_reg1);
1611    }
1612  }
1613  StoreValue(rl_dest, rl_result);
1614  return true;
1615}
1616
1617// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1618// and store the result in 'rl_dest'.
1619bool Mir2Lir::HandleEasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) {
1620  // Can we simplify this multiplication?
1621  bool power_of_two = false;
1622  bool pop_count_le2 = false;
1623  bool power_of_two_minus_one = false;
1624  if (lit < 2) {
1625    // Avoid special cases.
1626    return false;
1627  } else if (IsPowerOfTwo(lit)) {
1628    power_of_two = true;
1629  } else if (IsPopCountLE2(lit)) {
1630    pop_count_le2 = true;
1631  } else if (IsPowerOfTwo(lit + 1)) {
1632    power_of_two_minus_one = true;
1633  } else {
1634    return false;
1635  }
1636  rl_src = LoadValue(rl_src, kCoreReg);
1637  RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
1638  if (power_of_two) {
1639    // Shift.
1640    OpRegRegImm(kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
1641  } else if (pop_count_le2) {
1642    // Shift and add and shift.
1643    int first_bit = LowestSetBit(lit);
1644    int second_bit = LowestSetBit(lit ^ (1 << first_bit));
1645    GenMultiplyByTwoBitMultiplier(rl_src, rl_result, lit, first_bit, second_bit);
1646  } else {
1647    // Reverse subtract: (src << (shift + 1)) - src.
1648    DCHECK(power_of_two_minus_one);
1649    // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
1650    int t_reg = AllocTemp();
1651    OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1652    OpRegRegReg(kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1653  }
1654  StoreValue(rl_dest, rl_result);
1655  return true;
1656}
1657
1658void Mir2Lir::GenArithOpIntLit(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src,
1659                               int lit) {
1660  RegLocation rl_result;
1661  OpKind op = static_cast<OpKind>(0);    /* Make gcc happy */
1662  int shift_op = false;
1663  bool is_div = false;
1664
1665  switch (opcode) {
1666    case Instruction::RSUB_INT_LIT8:
1667    case Instruction::RSUB_INT: {
1668      rl_src = LoadValue(rl_src, kCoreReg);
1669      rl_result = EvalLoc(rl_dest, kCoreReg, true);
1670      if (cu_->instruction_set == kThumb2) {
1671        OpRegRegImm(kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1672      } else {
1673        OpRegReg(kOpNeg, rl_result.low_reg, rl_src.low_reg);
1674        OpRegImm(kOpAdd, rl_result.low_reg, lit);
1675      }
1676      StoreValue(rl_dest, rl_result);
1677      return;
1678    }
1679
1680    case Instruction::SUB_INT:
1681    case Instruction::SUB_INT_2ADDR:
1682      lit = -lit;
1683      // Intended fallthrough
1684    case Instruction::ADD_INT:
1685    case Instruction::ADD_INT_2ADDR:
1686    case Instruction::ADD_INT_LIT8:
1687    case Instruction::ADD_INT_LIT16:
1688      op = kOpAdd;
1689      break;
1690    case Instruction::MUL_INT:
1691    case Instruction::MUL_INT_2ADDR:
1692    case Instruction::MUL_INT_LIT8:
1693    case Instruction::MUL_INT_LIT16: {
1694      if (HandleEasyMultiply(rl_src, rl_dest, lit)) {
1695        return;
1696      }
1697      op = kOpMul;
1698      break;
1699    }
1700    case Instruction::AND_INT:
1701    case Instruction::AND_INT_2ADDR:
1702    case Instruction::AND_INT_LIT8:
1703    case Instruction::AND_INT_LIT16:
1704      op = kOpAnd;
1705      break;
1706    case Instruction::OR_INT:
1707    case Instruction::OR_INT_2ADDR:
1708    case Instruction::OR_INT_LIT8:
1709    case Instruction::OR_INT_LIT16:
1710      op = kOpOr;
1711      break;
1712    case Instruction::XOR_INT:
1713    case Instruction::XOR_INT_2ADDR:
1714    case Instruction::XOR_INT_LIT8:
1715    case Instruction::XOR_INT_LIT16:
1716      op = kOpXor;
1717      break;
1718    case Instruction::SHL_INT_LIT8:
1719    case Instruction::SHL_INT:
1720    case Instruction::SHL_INT_2ADDR:
1721      lit &= 31;
1722      shift_op = true;
1723      op = kOpLsl;
1724      break;
1725    case Instruction::SHR_INT_LIT8:
1726    case Instruction::SHR_INT:
1727    case Instruction::SHR_INT_2ADDR:
1728      lit &= 31;
1729      shift_op = true;
1730      op = kOpAsr;
1731      break;
1732    case Instruction::USHR_INT_LIT8:
1733    case Instruction::USHR_INT:
1734    case Instruction::USHR_INT_2ADDR:
1735      lit &= 31;
1736      shift_op = true;
1737      op = kOpLsr;
1738      break;
1739
1740    case Instruction::DIV_INT:
1741    case Instruction::DIV_INT_2ADDR:
1742    case Instruction::DIV_INT_LIT8:
1743    case Instruction::DIV_INT_LIT16:
1744    case Instruction::REM_INT:
1745    case Instruction::REM_INT_2ADDR:
1746    case Instruction::REM_INT_LIT8:
1747    case Instruction::REM_INT_LIT16: {
1748      if (lit == 0) {
1749        GenImmedCheck(kCondAl, 0, 0, kThrowDivZero);
1750        return;
1751      }
1752      if ((opcode == Instruction::DIV_INT) ||
1753          (opcode == Instruction::DIV_INT_2ADDR) ||
1754          (opcode == Instruction::DIV_INT_LIT8) ||
1755          (opcode == Instruction::DIV_INT_LIT16)) {
1756        is_div = true;
1757      } else {
1758        is_div = false;
1759      }
1760      if (HandleEasyDivRem(opcode, is_div, rl_src, rl_dest, lit)) {
1761        return;
1762      }
1763
1764      bool done = false;
1765      if (cu_->instruction_set == kMips) {
1766        rl_src = LoadValue(rl_src, kCoreReg);
1767        rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
1768        done = true;
1769      } else if (cu_->instruction_set == kX86) {
1770        rl_result = GenDivRemLit(rl_dest, rl_src, lit, is_div);
1771        done = true;
1772      } else if (cu_->instruction_set == kThumb2) {
1773        if (cu_->GetInstructionSetFeatures().HasDivideInstruction()) {
1774          // Use ARM SDIV instruction for division.  For remainder we also need to
1775          // calculate using a MUL and subtract.
1776          rl_src = LoadValue(rl_src, kCoreReg);
1777          rl_result = GenDivRemLit(rl_dest, rl_src.low_reg, lit, is_div);
1778          done = true;
1779        }
1780      }
1781
1782      if (!done) {
1783        FlushAllRegs();   /* Everything to home location. */
1784        LoadValueDirectFixed(rl_src, TargetReg(kArg0));
1785        Clobber(TargetReg(kArg0));
1786        ThreadOffset func_offset = QUICK_ENTRYPOINT_OFFSET(pIdivmod);
1787        CallRuntimeHelperRegImm(func_offset, TargetReg(kArg0), lit, false);
1788        if (is_div)
1789          rl_result = GetReturn(false);
1790        else
1791          rl_result = GetReturnAlt();
1792      }
1793      StoreValue(rl_dest, rl_result);
1794      return;
1795    }
1796    default:
1797      LOG(FATAL) << "Unexpected opcode " << opcode;
1798  }
1799  rl_src = LoadValue(rl_src, kCoreReg);
1800  rl_result = EvalLoc(rl_dest, kCoreReg, true);
1801  // Avoid shifts by literal 0 - no support in Thumb.  Change to copy.
1802  if (shift_op && (lit == 0)) {
1803    OpRegCopy(rl_result.low_reg, rl_src.low_reg);
1804  } else {
1805    OpRegRegImm(op, rl_result.low_reg, rl_src.low_reg, lit);
1806  }
1807  StoreValue(rl_dest, rl_result);
1808}
1809
1810void Mir2Lir::GenArithOpLong(Instruction::Code opcode, RegLocation rl_dest,
1811                             RegLocation rl_src1, RegLocation rl_src2) {
1812  RegLocation rl_result;
1813  OpKind first_op = kOpBkpt;
1814  OpKind second_op = kOpBkpt;
1815  bool call_out = false;
1816  bool check_zero = false;
1817  ThreadOffset func_offset(-1);
1818  int ret_reg = TargetReg(kRet0);
1819
1820  switch (opcode) {
1821    case Instruction::NOT_LONG:
1822      rl_src2 = LoadValueWide(rl_src2, kCoreReg);
1823      rl_result = EvalLoc(rl_dest, kCoreReg, true);
1824      // Check for destructive overlap
1825      if (rl_result.low_reg == rl_src2.high_reg) {
1826        int t_reg = AllocTemp();
1827        OpRegCopy(t_reg, rl_src2.high_reg);
1828        OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1829        OpRegReg(kOpMvn, rl_result.high_reg, t_reg);
1830        FreeTemp(t_reg);
1831      } else {
1832        OpRegReg(kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1833        OpRegReg(kOpMvn, rl_result.high_reg, rl_src2.high_reg);
1834      }
1835      StoreValueWide(rl_dest, rl_result);
1836      return;
1837    case Instruction::ADD_LONG:
1838    case Instruction::ADD_LONG_2ADDR:
1839      if (cu_->instruction_set != kThumb2) {
1840        GenAddLong(opcode, rl_dest, rl_src1, rl_src2);
1841        return;
1842      }
1843      first_op = kOpAdd;
1844      second_op = kOpAdc;
1845      break;
1846    case Instruction::SUB_LONG:
1847    case Instruction::SUB_LONG_2ADDR:
1848      if (cu_->instruction_set != kThumb2) {
1849        GenSubLong(opcode, rl_dest, rl_src1, rl_src2);
1850        return;
1851      }
1852      first_op = kOpSub;
1853      second_op = kOpSbc;
1854      break;
1855    case Instruction::MUL_LONG:
1856    case Instruction::MUL_LONG_2ADDR:
1857      if (cu_->instruction_set != kMips) {
1858        GenMulLong(opcode, rl_dest, rl_src1, rl_src2);
1859        return;
1860      } else {
1861        call_out = true;
1862        ret_reg = TargetReg(kRet0);
1863        func_offset = QUICK_ENTRYPOINT_OFFSET(pLmul);
1864      }
1865      break;
1866    case Instruction::DIV_LONG:
1867    case Instruction::DIV_LONG_2ADDR:
1868      call_out = true;
1869      check_zero = true;
1870      ret_reg = TargetReg(kRet0);
1871      func_offset = QUICK_ENTRYPOINT_OFFSET(pLdiv);
1872      break;
1873    case Instruction::REM_LONG:
1874    case Instruction::REM_LONG_2ADDR:
1875      call_out = true;
1876      check_zero = true;
1877      func_offset = QUICK_ENTRYPOINT_OFFSET(pLmod);
1878      /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
1879      ret_reg = (cu_->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
1880      break;
1881    case Instruction::AND_LONG_2ADDR:
1882    case Instruction::AND_LONG:
1883      if (cu_->instruction_set == kX86) {
1884        return GenAndLong(opcode, rl_dest, rl_src1, rl_src2);
1885      }
1886      first_op = kOpAnd;
1887      second_op = kOpAnd;
1888      break;
1889    case Instruction::OR_LONG:
1890    case Instruction::OR_LONG_2ADDR:
1891      if (cu_->instruction_set == kX86) {
1892        GenOrLong(opcode, rl_dest, rl_src1, rl_src2);
1893        return;
1894      }
1895      first_op = kOpOr;
1896      second_op = kOpOr;
1897      break;
1898    case Instruction::XOR_LONG:
1899    case Instruction::XOR_LONG_2ADDR:
1900      if (cu_->instruction_set == kX86) {
1901        GenXorLong(opcode, rl_dest, rl_src1, rl_src2);
1902        return;
1903      }
1904      first_op = kOpXor;
1905      second_op = kOpXor;
1906      break;
1907    case Instruction::NEG_LONG: {
1908      GenNegLong(rl_dest, rl_src2);
1909      return;
1910    }
1911    default:
1912      LOG(FATAL) << "Invalid long arith op";
1913  }
1914  if (!call_out) {
1915    GenLong3Addr(first_op, second_op, rl_dest, rl_src1, rl_src2);
1916  } else {
1917    FlushAllRegs();   /* Send everything to home location */
1918    if (check_zero) {
1919      LoadValueDirectWideFixed(rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1920      int r_tgt = CallHelperSetup(func_offset);
1921      GenDivZeroCheck(TargetReg(kArg2), TargetReg(kArg3));
1922      LoadValueDirectWideFixed(rl_src1, TargetReg(kArg0), TargetReg(kArg1));
1923      // NOTE: callout here is not a safepoint
1924      CallHelper(r_tgt, func_offset, false /* not safepoint */);
1925    } else {
1926      CallRuntimeHelperRegLocationRegLocation(func_offset, rl_src1, rl_src2, false);
1927    }
1928    // Adjust return regs in to handle case of rem returning kArg2/kArg3
1929    if (ret_reg == TargetReg(kRet0))
1930      rl_result = GetReturnWide(false);
1931    else
1932      rl_result = GetReturnWideAlt();
1933    StoreValueWide(rl_dest, rl_result);
1934  }
1935}
1936
1937void Mir2Lir::GenConversionCall(ThreadOffset func_offset,
1938                                RegLocation rl_dest, RegLocation rl_src) {
1939  /*
1940   * Don't optimize the register usage since it calls out to support
1941   * functions
1942   */
1943  FlushAllRegs();   /* Send everything to home location */
1944  if (rl_src.wide) {
1945    LoadValueDirectWideFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1946                             rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
1947  } else {
1948    LoadValueDirectFixed(rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
1949  }
1950  CallRuntimeHelperRegLocation(func_offset, rl_src, false);
1951  if (rl_dest.wide) {
1952    RegLocation rl_result;
1953    rl_result = GetReturnWide(rl_dest.fp);
1954    StoreValueWide(rl_dest, rl_result);
1955  } else {
1956    RegLocation rl_result;
1957    rl_result = GetReturn(rl_dest.fp);
1958    StoreValue(rl_dest, rl_result);
1959  }
1960}
1961
1962/* Check if we need to check for pending suspend request */
1963void Mir2Lir::GenSuspendTest(int opt_flags) {
1964  if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1965    return;
1966  }
1967  FlushAllRegs();
1968  LIR* branch = OpTestSuspend(NULL);
1969  LIR* ret_lab = NewLIR0(kPseudoTargetLabel);
1970  LIR* target = RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(ret_lab),
1971                       current_dalvik_offset_);
1972  branch->target = target;
1973  suspend_launchpads_.Insert(target);
1974}
1975
1976/* Check if we need to check for pending suspend request */
1977void Mir2Lir::GenSuspendTestAndBranch(int opt_flags, LIR* target) {
1978  if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1979    OpUnconditionalBranch(target);
1980    return;
1981  }
1982  OpTestSuspend(target);
1983  LIR* launch_pad =
1984      RawLIR(current_dalvik_offset_, kPseudoSuspendTarget, WrapPointer(target),
1985             current_dalvik_offset_);
1986  FlushAllRegs();
1987  OpUnconditionalBranch(launch_pad);
1988  suspend_launchpads_.Insert(launch_pad);
1989}
1990
1991/* Call out to helper assembly routine that will null check obj and then lock it. */
1992void Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
1993  FlushAllRegs();
1994  CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pLockObject), rl_src, true);
1995}
1996
1997/* Call out to helper assembly routine that will null check obj and then unlock it. */
1998void Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
1999  FlushAllRegs();
2000  CallRuntimeHelperRegLocation(QUICK_ENTRYPOINT_OFFSET(pUnlockObject), rl_src, true);
2001}
2002
2003/* Generic code for generating a wide constant into a VR. */
2004void Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
2005  RegLocation rl_result = EvalLoc(rl_dest, kAnyReg, true);
2006  LoadConstantWide(rl_result.low_reg, rl_result.high_reg, value);
2007  StoreValueWide(rl_dest, rl_result);
2008}
2009
2010}  // namespace art
2011