code_generator_arm.cc revision 70e97462116a47ef2e582ea29a037847debcc029
1/*
2 * Copyright (C) 2014 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 "code_generator_arm.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
20#include "art_method.h"
21#include "code_generator_utils.h"
22#include "compiled_method.h"
23#include "entrypoints/quick/quick_entrypoints.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
26#include "intrinsics_arm.h"
27#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "thread.h"
30#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
32#include "utils/assembler.h"
33#include "utils/stack_checks.h"
34
35namespace art {
36
37template<class MirrorType>
38class GcRoot;
39
40namespace arm {
41
42static bool ExpectedPairLayout(Location location) {
43  // We expected this for both core and fpu register pairs.
44  return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
47static constexpr int kCurrentMethodStackOffset = 0;
48static constexpr Register kMethodRegisterArgument = R0;
49
50static constexpr Register kCoreAlwaysSpillRegister = R5;
51static constexpr Register kCoreCalleeSaves[] =
52    { R5, R6, R7, R8, R10, R11, LR };
53static constexpr SRegister kFpuCalleeSaves[] =
54    { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
55
56// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
60static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
61
62// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->  // NOLINT
64#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
65
66class NullCheckSlowPathARM : public SlowPathCode {
67 public:
68  explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCode(instruction) {}
69
70  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
71    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
72    __ Bind(GetEntryLabel());
73    if (instruction_->CanThrowIntoCatchBlock()) {
74      // Live registers will be restored in the catch block if caught.
75      SaveLiveRegisters(codegen, instruction_->GetLocations());
76    }
77    arm_codegen->InvokeRuntime(
78        QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
79    CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
80  }
81
82  bool IsFatal() const OVERRIDE { return true; }
83
84  const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
85
86 private:
87  DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
90class DivZeroCheckSlowPathARM : public SlowPathCode {
91 public:
92  explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
93
94  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
95    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96    __ Bind(GetEntryLabel());
97    if (instruction_->CanThrowIntoCatchBlock()) {
98      // Live registers will be restored in the catch block if caught.
99      SaveLiveRegisters(codegen, instruction_->GetLocations());
100    }
101    arm_codegen->InvokeRuntime(
102        QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
103    CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
104  }
105
106  bool IsFatal() const OVERRIDE { return true; }
107
108  const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
110 private:
111  DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
112};
113
114class SuspendCheckSlowPathARM : public SlowPathCode {
115 public:
116  SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
117      : SlowPathCode(instruction), successor_(successor) {}
118
119  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
120    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
121    __ Bind(GetEntryLabel());
122    arm_codegen->InvokeRuntime(
123        QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
124    CheckEntrypointTypes<kQuickTestSuspend, void, void>();
125    if (successor_ == nullptr) {
126      __ b(GetReturnLabel());
127    } else {
128      __ b(arm_codegen->GetLabelOf(successor_));
129    }
130  }
131
132  Label* GetReturnLabel() {
133    DCHECK(successor_ == nullptr);
134    return &return_label_;
135  }
136
137  HBasicBlock* GetSuccessor() const {
138    return successor_;
139  }
140
141  const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
142
143 private:
144  // If not null, the block to branch to after the suspend check.
145  HBasicBlock* const successor_;
146
147  // If `successor_` is null, the label to branch to after the suspend check.
148  Label return_label_;
149
150  DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
151};
152
153class BoundsCheckSlowPathARM : public SlowPathCode {
154 public:
155  explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
156      : SlowPathCode(instruction) {}
157
158  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
160    LocationSummary* locations = instruction_->GetLocations();
161
162    __ Bind(GetEntryLabel());
163    if (instruction_->CanThrowIntoCatchBlock()) {
164      // Live registers will be restored in the catch block if caught.
165      SaveLiveRegisters(codegen, instruction_->GetLocations());
166    }
167    // We're moving two locations to locations that could overlap, so we need a parallel
168    // move resolver.
169    InvokeRuntimeCallingConvention calling_convention;
170    codegen->EmitParallelMoves(
171        locations->InAt(0),
172        Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
173        Primitive::kPrimInt,
174        locations->InAt(1),
175        Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
176        Primitive::kPrimInt);
177    uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
178        ? QUICK_ENTRY_POINT(pThrowStringBounds)
179        : QUICK_ENTRY_POINT(pThrowArrayBounds);
180    arm_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
181    CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
182    CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183  }
184
185  bool IsFatal() const OVERRIDE { return true; }
186
187  const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
188
189 private:
190  DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
191};
192
193class LoadClassSlowPathARM : public SlowPathCode {
194 public:
195  LoadClassSlowPathARM(HLoadClass* cls,
196                       HInstruction* at,
197                       uint32_t dex_pc,
198                       bool do_clinit)
199      : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
200    DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201  }
202
203  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204    LocationSummary* locations = at_->GetLocations();
205
206    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
207    __ Bind(GetEntryLabel());
208    SaveLiveRegisters(codegen, locations);
209
210    InvokeRuntimeCallingConvention calling_convention;
211    __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
212    int32_t entry_point_offset = do_clinit_
213        ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
214        : QUICK_ENTRY_POINT(pInitializeType);
215    arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
216    if (do_clinit_) {
217      CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
218    } else {
219      CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
220    }
221
222    // Move the class to the desired location.
223    Location out = locations->Out();
224    if (out.IsValid()) {
225      DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
226      arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
227    }
228    RestoreLiveRegisters(codegen, locations);
229    __ b(GetExitLabel());
230  }
231
232  const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
233
234 private:
235  // The class this slow path will load.
236  HLoadClass* const cls_;
237
238  // The instruction where this slow path is happening.
239  // (Might be the load class or an initialization check).
240  HInstruction* const at_;
241
242  // The dex PC of `at_`.
243  const uint32_t dex_pc_;
244
245  // Whether to initialize the class.
246  const bool do_clinit_;
247
248  DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
249};
250
251class LoadStringSlowPathARM : public SlowPathCode {
252 public:
253  explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
254
255  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
256    LocationSummary* locations = instruction_->GetLocations();
257    DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
258
259    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
260    __ Bind(GetEntryLabel());
261    SaveLiveRegisters(codegen, locations);
262
263    InvokeRuntimeCallingConvention calling_convention;
264    const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
265    __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
266    arm_codegen->InvokeRuntime(
267        QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
268    CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
269    arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
270
271    RestoreLiveRegisters(codegen, locations);
272    __ b(GetExitLabel());
273  }
274
275  const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
276
277 private:
278  DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
279};
280
281class TypeCheckSlowPathARM : public SlowPathCode {
282 public:
283  TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
284      : SlowPathCode(instruction), is_fatal_(is_fatal) {}
285
286  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287    LocationSummary* locations = instruction_->GetLocations();
288    Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
289                                                        : locations->Out();
290    DCHECK(instruction_->IsCheckCast()
291           || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
292
293    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
294    __ Bind(GetEntryLabel());
295
296    if (!is_fatal_) {
297      SaveLiveRegisters(codegen, locations);
298    }
299
300    // We're moving two locations to locations that could overlap, so we need a parallel
301    // move resolver.
302    InvokeRuntimeCallingConvention calling_convention;
303    codegen->EmitParallelMoves(
304        locations->InAt(1),
305        Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
306        Primitive::kPrimNot,
307        object_class,
308        Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309        Primitive::kPrimNot);
310
311    if (instruction_->IsInstanceOf()) {
312      arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313                                 instruction_,
314                                 instruction_->GetDexPc(),
315                                 this);
316      CheckEntrypointTypes<
317          kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
318      arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
319    } else {
320      DCHECK(instruction_->IsCheckCast());
321      arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
322                                 instruction_,
323                                 instruction_->GetDexPc(),
324                                 this);
325      CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
326    }
327
328    if (!is_fatal_) {
329      RestoreLiveRegisters(codegen, locations);
330      __ b(GetExitLabel());
331    }
332  }
333
334  const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
335
336  bool IsFatal() const OVERRIDE { return is_fatal_; }
337
338 private:
339  const bool is_fatal_;
340
341  DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
342};
343
344class DeoptimizationSlowPathARM : public SlowPathCode {
345 public:
346  explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
347    : SlowPathCode(instruction) {}
348
349  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
350    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
351    __ Bind(GetEntryLabel());
352    SaveLiveRegisters(codegen, instruction_->GetLocations());
353    arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354                               instruction_,
355                               instruction_->GetDexPc(),
356                               this);
357    CheckEntrypointTypes<kQuickDeoptimize, void, void>();
358  }
359
360  const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
361
362 private:
363  DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
364};
365
366class ArraySetSlowPathARM : public SlowPathCode {
367 public:
368  explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
369
370  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
371    LocationSummary* locations = instruction_->GetLocations();
372    __ Bind(GetEntryLabel());
373    SaveLiveRegisters(codegen, locations);
374
375    InvokeRuntimeCallingConvention calling_convention;
376    HParallelMove parallel_move(codegen->GetGraph()->GetArena());
377    parallel_move.AddMove(
378        locations->InAt(0),
379        Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
380        Primitive::kPrimNot,
381        nullptr);
382    parallel_move.AddMove(
383        locations->InAt(1),
384        Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
385        Primitive::kPrimInt,
386        nullptr);
387    parallel_move.AddMove(
388        locations->InAt(2),
389        Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
390        Primitive::kPrimNot,
391        nullptr);
392    codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
393
394    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
395    arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
396                               instruction_,
397                               instruction_->GetDexPc(),
398                               this);
399    CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
400    RestoreLiveRegisters(codegen, locations);
401    __ b(GetExitLabel());
402  }
403
404  const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
405
406 private:
407  DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
408};
409
410// Slow path marking an object during a read barrier.
411class ReadBarrierMarkSlowPathARM : public SlowPathCode {
412 public:
413  ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
414      : SlowPathCode(instruction), obj_(obj) {
415    DCHECK(kEmitCompilerReadBarrier);
416  }
417
418  const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
419
420  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
421    LocationSummary* locations = instruction_->GetLocations();
422    Register reg = obj_.AsRegister<Register>();
423    DCHECK(locations->CanCall());
424    DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
425    DCHECK(instruction_->IsInstanceFieldGet() ||
426           instruction_->IsStaticFieldGet() ||
427           instruction_->IsArrayGet() ||
428           instruction_->IsLoadClass() ||
429           instruction_->IsLoadString() ||
430           instruction_->IsInstanceOf() ||
431           instruction_->IsCheckCast() ||
432           (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
433           (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
434        << "Unexpected instruction in read barrier marking slow path: "
435        << instruction_->DebugName();
436
437    __ Bind(GetEntryLabel());
438    // No need to save live registers; it's taken care of by the
439    // entrypoint. Also, there is no need to update the stack mask,
440    // as this runtime call will not trigger a garbage collection.
441    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
442    DCHECK_NE(reg, SP);
443    DCHECK_NE(reg, LR);
444    DCHECK_NE(reg, PC);
445    // IP is used internally by the ReadBarrierMarkRegX entry point
446    // as a temporary, it cannot be the entry point's input/output.
447    DCHECK_NE(reg, IP);
448    DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
449    // "Compact" slow path, saving two moves.
450    //
451    // Instead of using the standard runtime calling convention (input
452    // and output in R0):
453    //
454    //   R0 <- obj
455    //   R0 <- ReadBarrierMark(R0)
456    //   obj <- R0
457    //
458    // we just use rX (the register holding `obj`) as input and output
459    // of a dedicated entrypoint:
460    //
461    //   rX <- ReadBarrierMarkRegX(rX)
462    //
463    int32_t entry_point_offset =
464        CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
465    // This runtime call does not require a stack map.
466    arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
467    __ b(GetExitLabel());
468  }
469
470 private:
471  const Location obj_;
472
473  DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
474};
475
476// Slow path generating a read barrier for a heap reference.
477class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
478 public:
479  ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
480                                         Location out,
481                                         Location ref,
482                                         Location obj,
483                                         uint32_t offset,
484                                         Location index)
485      : SlowPathCode(instruction),
486        out_(out),
487        ref_(ref),
488        obj_(obj),
489        offset_(offset),
490        index_(index) {
491    DCHECK(kEmitCompilerReadBarrier);
492    // If `obj` is equal to `out` or `ref`, it means the initial object
493    // has been overwritten by (or after) the heap object reference load
494    // to be instrumented, e.g.:
495    //
496    //   __ LoadFromOffset(kLoadWord, out, out, offset);
497    //   codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
498    //
499    // In that case, we have lost the information about the original
500    // object, and the emitted read barrier cannot work properly.
501    DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
502    DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
503  }
504
505  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
507    LocationSummary* locations = instruction_->GetLocations();
508    Register reg_out = out_.AsRegister<Register>();
509    DCHECK(locations->CanCall());
510    DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
511    DCHECK(instruction_->IsInstanceFieldGet() ||
512           instruction_->IsStaticFieldGet() ||
513           instruction_->IsArrayGet() ||
514           instruction_->IsInstanceOf() ||
515           instruction_->IsCheckCast() ||
516           (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
517        << "Unexpected instruction in read barrier for heap reference slow path: "
518        << instruction_->DebugName();
519
520    __ Bind(GetEntryLabel());
521    SaveLiveRegisters(codegen, locations);
522
523    // We may have to change the index's value, but as `index_` is a
524    // constant member (like other "inputs" of this slow path),
525    // introduce a copy of it, `index`.
526    Location index = index_;
527    if (index_.IsValid()) {
528      // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
529      if (instruction_->IsArrayGet()) {
530        // Compute the actual memory offset and store it in `index`.
531        Register index_reg = index_.AsRegister<Register>();
532        DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
533        if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
534          // We are about to change the value of `index_reg` (see the
535          // calls to art::arm::Thumb2Assembler::Lsl and
536          // art::arm::Thumb2Assembler::AddConstant below), but it has
537          // not been saved by the previous call to
538          // art::SlowPathCode::SaveLiveRegisters, as it is a
539          // callee-save register --
540          // art::SlowPathCode::SaveLiveRegisters does not consider
541          // callee-save registers, as it has been designed with the
542          // assumption that callee-save registers are supposed to be
543          // handled by the called function.  So, as a callee-save
544          // register, `index_reg` _would_ eventually be saved onto
545          // the stack, but it would be too late: we would have
546          // changed its value earlier.  Therefore, we manually save
547          // it here into another freely available register,
548          // `free_reg`, chosen of course among the caller-save
549          // registers (as a callee-save `free_reg` register would
550          // exhibit the same problem).
551          //
552          // Note we could have requested a temporary register from
553          // the register allocator instead; but we prefer not to, as
554          // this is a slow path, and we know we can find a
555          // caller-save register that is available.
556          Register free_reg = FindAvailableCallerSaveRegister(codegen);
557          __ Mov(free_reg, index_reg);
558          index_reg = free_reg;
559          index = Location::RegisterLocation(index_reg);
560        } else {
561          // The initial register stored in `index_` has already been
562          // saved in the call to art::SlowPathCode::SaveLiveRegisters
563          // (as it is not a callee-save register), so we can freely
564          // use it.
565        }
566        // Shifting the index value contained in `index_reg` by the scale
567        // factor (2) cannot overflow in practice, as the runtime is
568        // unable to allocate object arrays with a size larger than
569        // 2^26 - 1 (that is, 2^28 - 4 bytes).
570        __ Lsl(index_reg, index_reg, TIMES_4);
571        static_assert(
572            sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
573            "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
574        __ AddConstant(index_reg, index_reg, offset_);
575      } else {
576        // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
577        // intrinsics, `index_` is not shifted by a scale factor of 2
578        // (as in the case of ArrayGet), as it is actually an offset
579        // to an object field within an object.
580        DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
581        DCHECK(instruction_->GetLocations()->Intrinsified());
582        DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
583               (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
584            << instruction_->AsInvoke()->GetIntrinsic();
585        DCHECK_EQ(offset_, 0U);
586        DCHECK(index_.IsRegisterPair());
587        // UnsafeGet's offset location is a register pair, the low
588        // part contains the correct offset.
589        index = index_.ToLow();
590      }
591    }
592
593    // We're moving two or three locations to locations that could
594    // overlap, so we need a parallel move resolver.
595    InvokeRuntimeCallingConvention calling_convention;
596    HParallelMove parallel_move(codegen->GetGraph()->GetArena());
597    parallel_move.AddMove(ref_,
598                          Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
599                          Primitive::kPrimNot,
600                          nullptr);
601    parallel_move.AddMove(obj_,
602                          Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
603                          Primitive::kPrimNot,
604                          nullptr);
605    if (index.IsValid()) {
606      parallel_move.AddMove(index,
607                            Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
608                            Primitive::kPrimInt,
609                            nullptr);
610      codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
611    } else {
612      codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613      __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
614    }
615    arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
616                               instruction_,
617                               instruction_->GetDexPc(),
618                               this);
619    CheckEntrypointTypes<
620        kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
621    arm_codegen->Move32(out_, Location::RegisterLocation(R0));
622
623    RestoreLiveRegisters(codegen, locations);
624    __ b(GetExitLabel());
625  }
626
627  const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
628
629 private:
630  Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
631    size_t ref = static_cast<int>(ref_.AsRegister<Register>());
632    size_t obj = static_cast<int>(obj_.AsRegister<Register>());
633    for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
634      if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
635        return static_cast<Register>(i);
636      }
637    }
638    // We shall never fail to find a free caller-save register, as
639    // there are more than two core caller-save registers on ARM
640    // (meaning it is possible to find one which is different from
641    // `ref` and `obj`).
642    DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
643    LOG(FATAL) << "Could not find a free caller-save register";
644    UNREACHABLE();
645  }
646
647  const Location out_;
648  const Location ref_;
649  const Location obj_;
650  const uint32_t offset_;
651  // An additional location containing an index to an array.
652  // Only used for HArrayGet and the UnsafeGetObject &
653  // UnsafeGetObjectVolatile intrinsics.
654  const Location index_;
655
656  DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
657};
658
659// Slow path generating a read barrier for a GC root.
660class ReadBarrierForRootSlowPathARM : public SlowPathCode {
661 public:
662  ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
663      : SlowPathCode(instruction), out_(out), root_(root) {
664    DCHECK(kEmitCompilerReadBarrier);
665  }
666
667  void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
668    LocationSummary* locations = instruction_->GetLocations();
669    Register reg_out = out_.AsRegister<Register>();
670    DCHECK(locations->CanCall());
671    DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
672    DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
673        << "Unexpected instruction in read barrier for GC root slow path: "
674        << instruction_->DebugName();
675
676    __ Bind(GetEntryLabel());
677    SaveLiveRegisters(codegen, locations);
678
679    InvokeRuntimeCallingConvention calling_convention;
680    CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
681    arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
682    arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
683                               instruction_,
684                               instruction_->GetDexPc(),
685                               this);
686    CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
687    arm_codegen->Move32(out_, Location::RegisterLocation(R0));
688
689    RestoreLiveRegisters(codegen, locations);
690    __ b(GetExitLabel());
691  }
692
693  const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
694
695 private:
696  const Location out_;
697  const Location root_;
698
699  DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
700};
701
702#undef __
703// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
704#define __ down_cast<ArmAssembler*>(GetAssembler())->  // NOLINT
705
706inline Condition ARMCondition(IfCondition cond) {
707  switch (cond) {
708    case kCondEQ: return EQ;
709    case kCondNE: return NE;
710    case kCondLT: return LT;
711    case kCondLE: return LE;
712    case kCondGT: return GT;
713    case kCondGE: return GE;
714    case kCondB:  return LO;
715    case kCondBE: return LS;
716    case kCondA:  return HI;
717    case kCondAE: return HS;
718  }
719  LOG(FATAL) << "Unreachable";
720  UNREACHABLE();
721}
722
723// Maps signed condition to unsigned condition.
724inline Condition ARMUnsignedCondition(IfCondition cond) {
725  switch (cond) {
726    case kCondEQ: return EQ;
727    case kCondNE: return NE;
728    // Signed to unsigned.
729    case kCondLT: return LO;
730    case kCondLE: return LS;
731    case kCondGT: return HI;
732    case kCondGE: return HS;
733    // Unsigned remain unchanged.
734    case kCondB:  return LO;
735    case kCondBE: return LS;
736    case kCondA:  return HI;
737    case kCondAE: return HS;
738  }
739  LOG(FATAL) << "Unreachable";
740  UNREACHABLE();
741}
742
743inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
744  // The ARM condition codes can express all the necessary branches, see the
745  // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
746  // There is no dex instruction or HIR that would need the missing conditions
747  // "equal or unordered" or "not equal".
748  switch (cond) {
749    case kCondEQ: return EQ;
750    case kCondNE: return NE /* unordered */;
751    case kCondLT: return gt_bias ? CC : LT /* unordered */;
752    case kCondLE: return gt_bias ? LS : LE /* unordered */;
753    case kCondGT: return gt_bias ? HI /* unordered */ : GT;
754    case kCondGE: return gt_bias ? CS /* unordered */ : GE;
755    default:
756      LOG(FATAL) << "UNREACHABLE";
757      UNREACHABLE();
758  }
759}
760
761void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
762  stream << Register(reg);
763}
764
765void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
766  stream << SRegister(reg);
767}
768
769size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
770  __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
771  return kArmWordSize;
772}
773
774size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
775  __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
776  return kArmWordSize;
777}
778
779size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
780  __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
781  return kArmWordSize;
782}
783
784size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
785  __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
786  return kArmWordSize;
787}
788
789CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
790                                   const ArmInstructionSetFeatures& isa_features,
791                                   const CompilerOptions& compiler_options,
792                                   OptimizingCompilerStats* stats)
793    : CodeGenerator(graph,
794                    kNumberOfCoreRegisters,
795                    kNumberOfSRegisters,
796                    kNumberOfRegisterPairs,
797                    ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
798                                        arraysize(kCoreCalleeSaves)),
799                    ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
800                                        arraysize(kFpuCalleeSaves)),
801                    compiler_options,
802                    stats),
803      block_labels_(nullptr),
804      location_builder_(graph, this),
805      instruction_visitor_(graph, this),
806      move_resolver_(graph->GetArena(), this),
807      assembler_(graph->GetArena()),
808      isa_features_(isa_features),
809      uint32_literals_(std::less<uint32_t>(),
810                       graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
811      method_patches_(MethodReferenceComparator(),
812                      graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
813      call_patches_(MethodReferenceComparator(),
814                    graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
815      relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
816      pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
817      boot_image_string_patches_(StringReferenceValueComparator(),
818                                 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
819      pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
820      boot_image_type_patches_(TypeReferenceValueComparator(),
821                               graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
822      pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
823      boot_image_address_patches_(std::less<uint32_t>(),
824                                  graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
825  // Always save the LR register to mimic Quick.
826  AddAllocatedRegister(Location::RegisterLocation(LR));
827}
828
829void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
830  // Ensure that we fix up branches and literal loads and emit the literal pool.
831  __ FinalizeCode();
832
833  // Adjust native pc offsets in stack maps.
834  for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
835    uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
836    uint32_t new_position = __ GetAdjustedPosition(old_position);
837    stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
838  }
839  // Adjust pc offsets for the disassembly information.
840  if (disasm_info_ != nullptr) {
841    GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
842    frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
843    frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
844    for (auto& it : *disasm_info_->GetInstructionIntervals()) {
845      it.second.start = __ GetAdjustedPosition(it.second.start);
846      it.second.end = __ GetAdjustedPosition(it.second.end);
847    }
848    for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
849      it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
850      it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
851    }
852  }
853
854  CodeGenerator::Finalize(allocator);
855}
856
857void CodeGeneratorARM::SetupBlockedRegisters() const {
858  // Don't allocate the dalvik style register pair passing.
859  blocked_register_pairs_[R1_R2] = true;
860
861  // Stack register, LR and PC are always reserved.
862  blocked_core_registers_[SP] = true;
863  blocked_core_registers_[LR] = true;
864  blocked_core_registers_[PC] = true;
865
866  // Reserve thread register.
867  blocked_core_registers_[TR] = true;
868
869  // Reserve temp register.
870  blocked_core_registers_[IP] = true;
871
872  if (GetGraph()->IsDebuggable()) {
873    // Stubs do not save callee-save floating point registers. If the graph
874    // is debuggable, we need to deal with these registers differently. For
875    // now, just block them.
876    for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
877      blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
878    }
879  }
880
881  UpdateBlockedPairRegisters();
882}
883
884void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
885  for (int i = 0; i < kNumberOfRegisterPairs; i++) {
886    ArmManagedRegister current =
887        ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
888    if (blocked_core_registers_[current.AsRegisterPairLow()]
889        || blocked_core_registers_[current.AsRegisterPairHigh()]) {
890      blocked_register_pairs_[i] = true;
891    }
892  }
893}
894
895InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
896      : InstructionCodeGenerator(graph, codegen),
897        assembler_(codegen->GetAssembler()),
898        codegen_(codegen) {}
899
900void CodeGeneratorARM::ComputeSpillMask() {
901  core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
902  DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
903  // There is no easy instruction to restore just the PC on thumb2. We spill and
904  // restore another arbitrary register.
905  core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
906  fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
907  // We use vpush and vpop for saving and restoring floating point registers, which take
908  // a SRegister and the number of registers to save/restore after that SRegister. We
909  // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
910  // but in the range.
911  if (fpu_spill_mask_ != 0) {
912    uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
913    uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
914    for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
915      fpu_spill_mask_ |= (1 << i);
916    }
917  }
918}
919
920static dwarf::Reg DWARFReg(Register reg) {
921  return dwarf::Reg::ArmCore(static_cast<int>(reg));
922}
923
924static dwarf::Reg DWARFReg(SRegister reg) {
925  return dwarf::Reg::ArmFp(static_cast<int>(reg));
926}
927
928void CodeGeneratorARM::GenerateFrameEntry() {
929  bool skip_overflow_check =
930      IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
931  DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
932  __ Bind(&frame_entry_label_);
933
934  if (HasEmptyFrame()) {
935    return;
936  }
937
938  if (!skip_overflow_check) {
939    __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
940    __ LoadFromOffset(kLoadWord, IP, IP, 0);
941    RecordPcInfo(nullptr, 0);
942  }
943
944  __ PushList(core_spill_mask_);
945  __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
946  __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
947  if (fpu_spill_mask_ != 0) {
948    SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
949    __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
950    __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
951    __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
952  }
953  int adjust = GetFrameSize() - FrameEntrySpillSize();
954  __ AddConstant(SP, -adjust);
955  __ cfi().AdjustCFAOffset(adjust);
956  __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
957}
958
959void CodeGeneratorARM::GenerateFrameExit() {
960  if (HasEmptyFrame()) {
961    __ bx(LR);
962    return;
963  }
964  __ cfi().RememberState();
965  int adjust = GetFrameSize() - FrameEntrySpillSize();
966  __ AddConstant(SP, adjust);
967  __ cfi().AdjustCFAOffset(-adjust);
968  if (fpu_spill_mask_ != 0) {
969    SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
970    __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
971    __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
972    __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
973  }
974  // Pop LR into PC to return.
975  DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
976  uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
977  __ PopList(pop_mask);
978  __ cfi().RestoreState();
979  __ cfi().DefCFAOffset(GetFrameSize());
980}
981
982void CodeGeneratorARM::Bind(HBasicBlock* block) {
983  Label* label = GetLabelOf(block);
984  __ BindTrackedLabel(label);
985}
986
987Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
988  switch (type) {
989    case Primitive::kPrimBoolean:
990    case Primitive::kPrimByte:
991    case Primitive::kPrimChar:
992    case Primitive::kPrimShort:
993    case Primitive::kPrimInt:
994    case Primitive::kPrimNot: {
995      uint32_t index = gp_index_++;
996      uint32_t stack_index = stack_index_++;
997      if (index < calling_convention.GetNumberOfRegisters()) {
998        return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
999      } else {
1000        return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1001      }
1002    }
1003
1004    case Primitive::kPrimLong: {
1005      uint32_t index = gp_index_;
1006      uint32_t stack_index = stack_index_;
1007      gp_index_ += 2;
1008      stack_index_ += 2;
1009      if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1010        if (calling_convention.GetRegisterAt(index) == R1) {
1011          // Skip R1, and use R2_R3 instead.
1012          gp_index_++;
1013          index++;
1014        }
1015      }
1016      if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1017        DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
1018                  calling_convention.GetRegisterAt(index + 1));
1019
1020        return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
1021                                              calling_convention.GetRegisterAt(index + 1));
1022      } else {
1023        return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1024      }
1025    }
1026
1027    case Primitive::kPrimFloat: {
1028      uint32_t stack_index = stack_index_++;
1029      if (float_index_ % 2 == 0) {
1030        float_index_ = std::max(double_index_, float_index_);
1031      }
1032      if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1033        return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1034      } else {
1035        return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1036      }
1037    }
1038
1039    case Primitive::kPrimDouble: {
1040      double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1041      uint32_t stack_index = stack_index_;
1042      stack_index_ += 2;
1043      if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1044        uint32_t index = double_index_;
1045        double_index_ += 2;
1046        Location result = Location::FpuRegisterPairLocation(
1047          calling_convention.GetFpuRegisterAt(index),
1048          calling_convention.GetFpuRegisterAt(index + 1));
1049        DCHECK(ExpectedPairLayout(result));
1050        return result;
1051      } else {
1052        return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1053      }
1054    }
1055
1056    case Primitive::kPrimVoid:
1057      LOG(FATAL) << "Unexpected parameter type " << type;
1058      break;
1059  }
1060  return Location::NoLocation();
1061}
1062
1063Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
1064  switch (type) {
1065    case Primitive::kPrimBoolean:
1066    case Primitive::kPrimByte:
1067    case Primitive::kPrimChar:
1068    case Primitive::kPrimShort:
1069    case Primitive::kPrimInt:
1070    case Primitive::kPrimNot: {
1071      return Location::RegisterLocation(R0);
1072    }
1073
1074    case Primitive::kPrimFloat: {
1075      return Location::FpuRegisterLocation(S0);
1076    }
1077
1078    case Primitive::kPrimLong: {
1079      return Location::RegisterPairLocation(R0, R1);
1080    }
1081
1082    case Primitive::kPrimDouble: {
1083      return Location::FpuRegisterPairLocation(S0, S1);
1084    }
1085
1086    case Primitive::kPrimVoid:
1087      return Location::NoLocation();
1088  }
1089
1090  UNREACHABLE();
1091}
1092
1093Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1094  return Location::RegisterLocation(kMethodRegisterArgument);
1095}
1096
1097void CodeGeneratorARM::Move32(Location destination, Location source) {
1098  if (source.Equals(destination)) {
1099    return;
1100  }
1101  if (destination.IsRegister()) {
1102    if (source.IsRegister()) {
1103      __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
1104    } else if (source.IsFpuRegister()) {
1105      __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
1106    } else {
1107      __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1108    }
1109  } else if (destination.IsFpuRegister()) {
1110    if (source.IsRegister()) {
1111      __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
1112    } else if (source.IsFpuRegister()) {
1113      __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
1114    } else {
1115      __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
1116    }
1117  } else {
1118    DCHECK(destination.IsStackSlot()) << destination;
1119    if (source.IsRegister()) {
1120      __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1121    } else if (source.IsFpuRegister()) {
1122      __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
1123    } else {
1124      DCHECK(source.IsStackSlot()) << source;
1125      __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1126      __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
1127    }
1128  }
1129}
1130
1131void CodeGeneratorARM::Move64(Location destination, Location source) {
1132  if (source.Equals(destination)) {
1133    return;
1134  }
1135  if (destination.IsRegisterPair()) {
1136    if (source.IsRegisterPair()) {
1137      EmitParallelMoves(
1138          Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1139          Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
1140          Primitive::kPrimInt,
1141          Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
1142          Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1143          Primitive::kPrimInt);
1144    } else if (source.IsFpuRegister()) {
1145      UNIMPLEMENTED(FATAL);
1146    } else if (source.IsFpuRegisterPair()) {
1147      __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1148                 destination.AsRegisterPairHigh<Register>(),
1149                 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
1150    } else {
1151      DCHECK(source.IsDoubleStackSlot());
1152      DCHECK(ExpectedPairLayout(destination));
1153      __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1154                        SP, source.GetStackIndex());
1155    }
1156  } else if (destination.IsFpuRegisterPair()) {
1157    if (source.IsDoubleStackSlot()) {
1158      __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1159                         SP,
1160                         source.GetStackIndex());
1161    } else if (source.IsRegisterPair()) {
1162      __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1163                 source.AsRegisterPairLow<Register>(),
1164                 source.AsRegisterPairHigh<Register>());
1165    } else {
1166      UNIMPLEMENTED(FATAL);
1167    }
1168  } else {
1169    DCHECK(destination.IsDoubleStackSlot());
1170    if (source.IsRegisterPair()) {
1171      // No conflict possible, so just do the moves.
1172      if (source.AsRegisterPairLow<Register>() == R1) {
1173        DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
1174        __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1175        __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
1176      } else {
1177        __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
1178                         SP, destination.GetStackIndex());
1179      }
1180    } else if (source.IsFpuRegisterPair()) {
1181      __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1182                        SP,
1183                        destination.GetStackIndex());
1184    } else {
1185      DCHECK(source.IsDoubleStackSlot());
1186      EmitParallelMoves(
1187          Location::StackSlot(source.GetStackIndex()),
1188          Location::StackSlot(destination.GetStackIndex()),
1189          Primitive::kPrimInt,
1190          Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
1191          Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1192          Primitive::kPrimInt);
1193    }
1194  }
1195}
1196
1197void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1198  DCHECK(location.IsRegister());
1199  __ LoadImmediate(location.AsRegister<Register>(), value);
1200}
1201
1202void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1203  HParallelMove move(GetGraph()->GetArena());
1204  move.AddMove(src, dst, dst_type, nullptr);
1205  GetMoveResolver()->EmitNativeCode(&move);
1206}
1207
1208void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1209  if (location.IsRegister()) {
1210    locations->AddTemp(location);
1211  } else if (location.IsRegisterPair()) {
1212    locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1213    locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1214  } else {
1215    UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1216  }
1217}
1218
1219void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1220                                     HInstruction* instruction,
1221                                     uint32_t dex_pc,
1222                                     SlowPathCode* slow_path) {
1223  InvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value(),
1224                instruction,
1225                dex_pc,
1226                slow_path);
1227}
1228
1229void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1230                                     HInstruction* instruction,
1231                                     uint32_t dex_pc,
1232                                     SlowPathCode* slow_path) {
1233  ValidateInvokeRuntime(instruction, slow_path);
1234  __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1235  __ blx(LR);
1236  RecordPcInfo(instruction, dex_pc, slow_path);
1237}
1238
1239void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1240                                                           HInstruction* instruction,
1241                                                           SlowPathCode* slow_path) {
1242  ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1243  __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1244  __ blx(LR);
1245}
1246
1247void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
1248  DCHECK(!successor->IsExitBlock());
1249
1250  HBasicBlock* block = got->GetBlock();
1251  HInstruction* previous = got->GetPrevious();
1252
1253  HLoopInformation* info = block->GetLoopInformation();
1254  if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
1255    codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1256    GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1257    return;
1258  }
1259
1260  if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1261    GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1262  }
1263  if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
1264    __ b(codegen_->GetLabelOf(successor));
1265  }
1266}
1267
1268void LocationsBuilderARM::VisitGoto(HGoto* got) {
1269  got->SetLocations(nullptr);
1270}
1271
1272void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1273  HandleGoto(got, got->GetSuccessor());
1274}
1275
1276void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1277  try_boundary->SetLocations(nullptr);
1278}
1279
1280void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1281  HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1282  if (!successor->IsExitBlock()) {
1283    HandleGoto(try_boundary, successor);
1284  }
1285}
1286
1287void LocationsBuilderARM::VisitExit(HExit* exit) {
1288  exit->SetLocations(nullptr);
1289}
1290
1291void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1292}
1293
1294void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1295  Primitive::Type type = instruction->InputAt(0)->GetType();
1296  Location lhs_loc = instruction->GetLocations()->InAt(0);
1297  Location rhs_loc = instruction->GetLocations()->InAt(1);
1298  if (rhs_loc.IsConstant()) {
1299    // 0.0 is the only immediate that can be encoded directly in
1300    // a VCMP instruction.
1301    //
1302    // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1303    // specify that in a floating-point comparison, positive zero
1304    // and negative zero are considered equal, so we can use the
1305    // literal 0.0 for both cases here.
1306    //
1307    // Note however that some methods (Float.equal, Float.compare,
1308    // Float.compareTo, Double.equal, Double.compare,
1309    // Double.compareTo, Math.max, Math.min, StrictMath.max,
1310    // StrictMath.min) consider 0.0 to be (strictly) greater than
1311    // -0.0. So if we ever translate calls to these methods into a
1312    // HCompare instruction, we must handle the -0.0 case with
1313    // care here.
1314    DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1315    if (type == Primitive::kPrimFloat) {
1316      __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1317    } else {
1318      DCHECK_EQ(type, Primitive::kPrimDouble);
1319      __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1320    }
1321  } else {
1322    if (type == Primitive::kPrimFloat) {
1323      __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1324    } else {
1325      DCHECK_EQ(type, Primitive::kPrimDouble);
1326      __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1327               FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1328    }
1329  }
1330}
1331
1332void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1333                                                  Label* true_label,
1334                                                  Label* false_label ATTRIBUTE_UNUSED) {
1335  __ vmstat();  // transfer FP status register to ARM APSR.
1336  __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
1337}
1338
1339void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1340                                                               Label* true_label,
1341                                                               Label* false_label) {
1342  LocationSummary* locations = cond->GetLocations();
1343  Location left = locations->InAt(0);
1344  Location right = locations->InAt(1);
1345  IfCondition if_cond = cond->GetCondition();
1346
1347  Register left_high = left.AsRegisterPairHigh<Register>();
1348  Register left_low = left.AsRegisterPairLow<Register>();
1349  IfCondition true_high_cond = if_cond;
1350  IfCondition false_high_cond = cond->GetOppositeCondition();
1351  Condition final_condition = ARMUnsignedCondition(if_cond);  // unsigned on lower part
1352
1353  // Set the conditions for the test, remembering that == needs to be
1354  // decided using the low words.
1355  // TODO: consider avoiding jumps with temporary and CMP low+SBC high
1356  switch (if_cond) {
1357    case kCondEQ:
1358    case kCondNE:
1359      // Nothing to do.
1360      break;
1361    case kCondLT:
1362      false_high_cond = kCondGT;
1363      break;
1364    case kCondLE:
1365      true_high_cond = kCondLT;
1366      break;
1367    case kCondGT:
1368      false_high_cond = kCondLT;
1369      break;
1370    case kCondGE:
1371      true_high_cond = kCondGT;
1372      break;
1373    case kCondB:
1374      false_high_cond = kCondA;
1375      break;
1376    case kCondBE:
1377      true_high_cond = kCondB;
1378      break;
1379    case kCondA:
1380      false_high_cond = kCondB;
1381      break;
1382    case kCondAE:
1383      true_high_cond = kCondA;
1384      break;
1385  }
1386  if (right.IsConstant()) {
1387    int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1388    int32_t val_low = Low32Bits(value);
1389    int32_t val_high = High32Bits(value);
1390
1391    __ CmpConstant(left_high, val_high);
1392    if (if_cond == kCondNE) {
1393      __ b(true_label, ARMCondition(true_high_cond));
1394    } else if (if_cond == kCondEQ) {
1395      __ b(false_label, ARMCondition(false_high_cond));
1396    } else {
1397      __ b(true_label, ARMCondition(true_high_cond));
1398      __ b(false_label, ARMCondition(false_high_cond));
1399    }
1400    // Must be equal high, so compare the lows.
1401    __ CmpConstant(left_low, val_low);
1402  } else {
1403    Register right_high = right.AsRegisterPairHigh<Register>();
1404    Register right_low = right.AsRegisterPairLow<Register>();
1405
1406    __ cmp(left_high, ShifterOperand(right_high));
1407    if (if_cond == kCondNE) {
1408      __ b(true_label, ARMCondition(true_high_cond));
1409    } else if (if_cond == kCondEQ) {
1410      __ b(false_label, ARMCondition(false_high_cond));
1411    } else {
1412      __ b(true_label, ARMCondition(true_high_cond));
1413      __ b(false_label, ARMCondition(false_high_cond));
1414    }
1415    // Must be equal high, so compare the lows.
1416    __ cmp(left_low, ShifterOperand(right_low));
1417  }
1418  // The last comparison might be unsigned.
1419  // TODO: optimize cases where this is always true/false
1420  __ b(true_label, final_condition);
1421}
1422
1423void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1424                                                               Label* true_target_in,
1425                                                               Label* false_target_in) {
1426  // Generated branching requires both targets to be explicit. If either of the
1427  // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1428  Label fallthrough_target;
1429  Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1430  Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1431
1432  Primitive::Type type = condition->InputAt(0)->GetType();
1433  switch (type) {
1434    case Primitive::kPrimLong:
1435      GenerateLongComparesAndJumps(condition, true_target, false_target);
1436      break;
1437    case Primitive::kPrimFloat:
1438    case Primitive::kPrimDouble:
1439      GenerateVcmp(condition);
1440      GenerateFPJumps(condition, true_target, false_target);
1441      break;
1442    default:
1443      LOG(FATAL) << "Unexpected compare type " << type;
1444  }
1445
1446  if (false_target != &fallthrough_target) {
1447    __ b(false_target);
1448  }
1449
1450  if (fallthrough_target.IsLinked()) {
1451    __ Bind(&fallthrough_target);
1452  }
1453}
1454
1455void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1456                                                        size_t condition_input_index,
1457                                                        Label* true_target,
1458                                                        Label* false_target) {
1459  HInstruction* cond = instruction->InputAt(condition_input_index);
1460
1461  if (true_target == nullptr && false_target == nullptr) {
1462    // Nothing to do. The code always falls through.
1463    return;
1464  } else if (cond->IsIntConstant()) {
1465    // Constant condition, statically compared against "true" (integer value 1).
1466    if (cond->AsIntConstant()->IsTrue()) {
1467      if (true_target != nullptr) {
1468        __ b(true_target);
1469      }
1470    } else {
1471      DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
1472      if (false_target != nullptr) {
1473        __ b(false_target);
1474      }
1475    }
1476    return;
1477  }
1478
1479  // The following code generates these patterns:
1480  //  (1) true_target == nullptr && false_target != nullptr
1481  //        - opposite condition true => branch to false_target
1482  //  (2) true_target != nullptr && false_target == nullptr
1483  //        - condition true => branch to true_target
1484  //  (3) true_target != nullptr && false_target != nullptr
1485  //        - condition true => branch to true_target
1486  //        - branch to false_target
1487  if (IsBooleanValueOrMaterializedCondition(cond)) {
1488    // Condition has been materialized, compare the output to 0.
1489    Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1490    DCHECK(cond_val.IsRegister());
1491    if (true_target == nullptr) {
1492      __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1493    } else {
1494      __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
1495    }
1496  } else {
1497    // Condition has not been materialized. Use its inputs as the comparison and
1498    // its condition as the branch condition.
1499    HCondition* condition = cond->AsCondition();
1500
1501    // If this is a long or FP comparison that has been folded into
1502    // the HCondition, generate the comparison directly.
1503    Primitive::Type type = condition->InputAt(0)->GetType();
1504    if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1505      GenerateCompareTestAndBranch(condition, true_target, false_target);
1506      return;
1507    }
1508
1509    LocationSummary* locations = cond->GetLocations();
1510    DCHECK(locations->InAt(0).IsRegister());
1511    Register left = locations->InAt(0).AsRegister<Register>();
1512    Location right = locations->InAt(1);
1513    if (right.IsRegister()) {
1514      __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
1515    } else {
1516      DCHECK(right.IsConstant());
1517      __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1518    }
1519    if (true_target == nullptr) {
1520      __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1521    } else {
1522      __ b(true_target, ARMCondition(condition->GetCondition()));
1523    }
1524  }
1525
1526  // If neither branch falls through (case 3), the conditional branch to `true_target`
1527  // was already emitted (case 2) and we need to emit a jump to `false_target`.
1528  if (true_target != nullptr && false_target != nullptr) {
1529    __ b(false_target);
1530  }
1531}
1532
1533void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1534  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1535  if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
1536    locations->SetInAt(0, Location::RequiresRegister());
1537  }
1538}
1539
1540void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1541  HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1542  HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1543  Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1544      nullptr : codegen_->GetLabelOf(true_successor);
1545  Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1546      nullptr : codegen_->GetLabelOf(false_successor);
1547  GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
1548}
1549
1550void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1551  LocationSummary* locations = new (GetGraph()->GetArena())
1552      LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1553  if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
1554    locations->SetInAt(0, Location::RequiresRegister());
1555  }
1556}
1557
1558void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1559  SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
1560  GenerateTestAndBranch(deoptimize,
1561                        /* condition_input_index */ 0,
1562                        slow_path->GetEntryLabel(),
1563                        /* false_target */ nullptr);
1564}
1565
1566void LocationsBuilderARM::VisitSelect(HSelect* select) {
1567  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1568  if (Primitive::IsFloatingPointType(select->GetType())) {
1569    locations->SetInAt(0, Location::RequiresFpuRegister());
1570    locations->SetInAt(1, Location::RequiresFpuRegister());
1571  } else {
1572    locations->SetInAt(0, Location::RequiresRegister());
1573    locations->SetInAt(1, Location::RequiresRegister());
1574  }
1575  if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1576    locations->SetInAt(2, Location::RequiresRegister());
1577  }
1578  locations->SetOut(Location::SameAsFirstInput());
1579}
1580
1581void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1582  LocationSummary* locations = select->GetLocations();
1583  Label false_target;
1584  GenerateTestAndBranch(select,
1585                        /* condition_input_index */ 2,
1586                        /* true_target */ nullptr,
1587                        &false_target);
1588  codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1589  __ Bind(&false_target);
1590}
1591
1592void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1593  new (GetGraph()->GetArena()) LocationSummary(info);
1594}
1595
1596void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1597  // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
1598}
1599
1600void CodeGeneratorARM::GenerateNop() {
1601  __ nop();
1602}
1603
1604void LocationsBuilderARM::HandleCondition(HCondition* cond) {
1605  LocationSummary* locations =
1606      new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
1607  // Handle the long/FP comparisons made in instruction simplification.
1608  switch (cond->InputAt(0)->GetType()) {
1609    case Primitive::kPrimLong:
1610      locations->SetInAt(0, Location::RequiresRegister());
1611      locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1612      if (!cond->IsEmittedAtUseSite()) {
1613        locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1614      }
1615      break;
1616
1617    case Primitive::kPrimFloat:
1618    case Primitive::kPrimDouble:
1619      locations->SetInAt(0, Location::RequiresFpuRegister());
1620      locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
1621      if (!cond->IsEmittedAtUseSite()) {
1622        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1623      }
1624      break;
1625
1626    default:
1627      locations->SetInAt(0, Location::RequiresRegister());
1628      locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1629      if (!cond->IsEmittedAtUseSite()) {
1630        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1631      }
1632  }
1633}
1634
1635void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
1636  if (cond->IsEmittedAtUseSite()) {
1637    return;
1638  }
1639
1640  LocationSummary* locations = cond->GetLocations();
1641  Location left = locations->InAt(0);
1642  Location right = locations->InAt(1);
1643  Register out = locations->Out().AsRegister<Register>();
1644  Label true_label, false_label;
1645
1646  switch (cond->InputAt(0)->GetType()) {
1647    default: {
1648      // Integer case.
1649      if (right.IsRegister()) {
1650        __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1651      } else {
1652        DCHECK(right.IsConstant());
1653        __ CmpConstant(left.AsRegister<Register>(),
1654                       CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1655      }
1656      __ it(ARMCondition(cond->GetCondition()), kItElse);
1657      __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1658             ARMCondition(cond->GetCondition()));
1659      __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1660             ARMCondition(cond->GetOppositeCondition()));
1661      return;
1662    }
1663    case Primitive::kPrimLong:
1664      GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1665      break;
1666    case Primitive::kPrimFloat:
1667    case Primitive::kPrimDouble:
1668      GenerateVcmp(cond);
1669      GenerateFPJumps(cond, &true_label, &false_label);
1670      break;
1671  }
1672
1673  // Convert the jumps into the result.
1674  Label done_label;
1675
1676  // False case: result = 0.
1677  __ Bind(&false_label);
1678  __ LoadImmediate(out, 0);
1679  __ b(&done_label);
1680
1681  // True case: result = 1.
1682  __ Bind(&true_label);
1683  __ LoadImmediate(out, 1);
1684  __ Bind(&done_label);
1685}
1686
1687void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1688  HandleCondition(comp);
1689}
1690
1691void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1692  HandleCondition(comp);
1693}
1694
1695void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1696  HandleCondition(comp);
1697}
1698
1699void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1700  HandleCondition(comp);
1701}
1702
1703void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1704  HandleCondition(comp);
1705}
1706
1707void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1708  HandleCondition(comp);
1709}
1710
1711void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1712  HandleCondition(comp);
1713}
1714
1715void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1716  HandleCondition(comp);
1717}
1718
1719void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1720  HandleCondition(comp);
1721}
1722
1723void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1724  HandleCondition(comp);
1725}
1726
1727void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1728  HandleCondition(comp);
1729}
1730
1731void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1732  HandleCondition(comp);
1733}
1734
1735void LocationsBuilderARM::VisitBelow(HBelow* comp) {
1736  HandleCondition(comp);
1737}
1738
1739void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
1740  HandleCondition(comp);
1741}
1742
1743void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1744  HandleCondition(comp);
1745}
1746
1747void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1748  HandleCondition(comp);
1749}
1750
1751void LocationsBuilderARM::VisitAbove(HAbove* comp) {
1752  HandleCondition(comp);
1753}
1754
1755void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
1756  HandleCondition(comp);
1757}
1758
1759void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1760  HandleCondition(comp);
1761}
1762
1763void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1764  HandleCondition(comp);
1765}
1766
1767void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
1768  LocationSummary* locations =
1769      new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1770  locations->SetOut(Location::ConstantLocation(constant));
1771}
1772
1773void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
1774  // Will be generated at use site.
1775}
1776
1777void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1778  LocationSummary* locations =
1779      new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1780  locations->SetOut(Location::ConstantLocation(constant));
1781}
1782
1783void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
1784  // Will be generated at use site.
1785}
1786
1787void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
1788  LocationSummary* locations =
1789      new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1790  locations->SetOut(Location::ConstantLocation(constant));
1791}
1792
1793void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
1794  // Will be generated at use site.
1795}
1796
1797void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1798  LocationSummary* locations =
1799      new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1800  locations->SetOut(Location::ConstantLocation(constant));
1801}
1802
1803void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
1804  // Will be generated at use site.
1805}
1806
1807void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1808  LocationSummary* locations =
1809      new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810  locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
1813void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
1814  // Will be generated at use site.
1815}
1816
1817void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1818  memory_barrier->SetLocations(nullptr);
1819}
1820
1821void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1822  codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1823}
1824
1825void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
1826  ret->SetLocations(nullptr);
1827}
1828
1829void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
1830  codegen_->GenerateFrameExit();
1831}
1832
1833void LocationsBuilderARM::VisitReturn(HReturn* ret) {
1834  LocationSummary* locations =
1835      new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
1836  locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
1837}
1838
1839void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
1840  codegen_->GenerateFrameExit();
1841}
1842
1843void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1844  // The trampoline uses the same calling convention as dex calling conventions,
1845  // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1846  // the method_idx.
1847  HandleInvoke(invoke);
1848}
1849
1850void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1851  codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1852}
1853
1854void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1855  // Explicit clinit checks triggered by static invokes must have been pruned by
1856  // art::PrepareForRegisterAllocation.
1857  DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1858
1859  IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1860                                         codegen_->GetAssembler(),
1861                                         codegen_->GetInstructionSetFeatures());
1862  if (intrinsic.TryDispatch(invoke)) {
1863    if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1864      invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1865    }
1866    return;
1867  }
1868
1869  HandleInvoke(invoke);
1870
1871  // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1872  if (invoke->HasPcRelativeDexCache()) {
1873    invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1874  }
1875}
1876
1877static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1878  if (invoke->GetLocations()->Intrinsified()) {
1879    IntrinsicCodeGeneratorARM intrinsic(codegen);
1880    intrinsic.Dispatch(invoke);
1881    return true;
1882  }
1883  return false;
1884}
1885
1886void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
1887  // Explicit clinit checks triggered by static invokes must have been pruned by
1888  // art::PrepareForRegisterAllocation.
1889  DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1890
1891  if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1892    return;
1893  }
1894
1895  LocationSummary* locations = invoke->GetLocations();
1896  codegen_->GenerateStaticOrDirectCall(
1897      invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
1898  codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1899}
1900
1901void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
1902  InvokeDexCallingConventionVisitorARM calling_convention_visitor;
1903  CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
1904}
1905
1906void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1907  IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1908                                         codegen_->GetAssembler(),
1909                                         codegen_->GetInstructionSetFeatures());
1910  if (intrinsic.TryDispatch(invoke)) {
1911    return;
1912  }
1913
1914  HandleInvoke(invoke);
1915}
1916
1917void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1918  if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1919    return;
1920  }
1921
1922  codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
1923  DCHECK(!codegen_->IsLeafMethod());
1924  codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1925}
1926
1927void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1928  HandleInvoke(invoke);
1929  // Add the hidden argument.
1930  invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1931}
1932
1933void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1934  // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1935  LocationSummary* locations = invoke->GetLocations();
1936  Register temp = locations->GetTemp(0).AsRegister<Register>();
1937  Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
1938  Location receiver = locations->InAt(0);
1939  uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1940
1941  // Set the hidden argument. This is safe to do this here, as R12
1942  // won't be modified thereafter, before the `blx` (call) instruction.
1943  DCHECK_EQ(R12, hidden_reg);
1944  __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
1945
1946  if (receiver.IsStackSlot()) {
1947    __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1948    // /* HeapReference<Class> */ temp = temp->klass_
1949    __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1950  } else {
1951    // /* HeapReference<Class> */ temp = receiver->klass_
1952    __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
1953  }
1954  codegen_->MaybeRecordImplicitNullCheck(invoke);
1955  // Instead of simply (possibly) unpoisoning `temp` here, we should
1956  // emit a read barrier for the previous class reference load.
1957  // However this is not required in practice, as this is an
1958  // intermediate/temporary reference and because the current
1959  // concurrent copying collector keeps the from-space memory
1960  // intact/accessible until the end of the marking phase (the
1961  // concurrent copying collector may not in the future).
1962  __ MaybeUnpoisonHeapReference(temp);
1963  __ LoadFromOffset(kLoadWord, temp, temp,
1964        mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1965  uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
1966      invoke->GetImtIndex(), kArmPointerSize));
1967  // temp = temp->GetImtEntryAt(method_offset);
1968  __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1969  uint32_t entry_point =
1970      ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
1971  // LR = temp->GetEntryPoint();
1972  __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1973  // LR();
1974  __ blx(LR);
1975  DCHECK(!codegen_->IsLeafMethod());
1976  codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1977}
1978
1979void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1980  LocationSummary* locations =
1981      new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1982  switch (neg->GetResultType()) {
1983    case Primitive::kPrimInt: {
1984      locations->SetInAt(0, Location::RequiresRegister());
1985      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1986      break;
1987    }
1988    case Primitive::kPrimLong: {
1989      locations->SetInAt(0, Location::RequiresRegister());
1990      locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1991      break;
1992    }
1993
1994    case Primitive::kPrimFloat:
1995    case Primitive::kPrimDouble:
1996      locations->SetInAt(0, Location::RequiresFpuRegister());
1997      locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1998      break;
1999
2000    default:
2001      LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2002  }
2003}
2004
2005void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2006  LocationSummary* locations = neg->GetLocations();
2007  Location out = locations->Out();
2008  Location in = locations->InAt(0);
2009  switch (neg->GetResultType()) {
2010    case Primitive::kPrimInt:
2011      DCHECK(in.IsRegister());
2012      __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
2013      break;
2014
2015    case Primitive::kPrimLong:
2016      DCHECK(in.IsRegisterPair());
2017      // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2018      __ rsbs(out.AsRegisterPairLow<Register>(),
2019              in.AsRegisterPairLow<Register>(),
2020              ShifterOperand(0));
2021      // We cannot emit an RSC (Reverse Subtract with Carry)
2022      // instruction here, as it does not exist in the Thumb-2
2023      // instruction set.  We use the following approach
2024      // using SBC and SUB instead.
2025      //
2026      // out.hi = -C
2027      __ sbc(out.AsRegisterPairHigh<Register>(),
2028             out.AsRegisterPairHigh<Register>(),
2029             ShifterOperand(out.AsRegisterPairHigh<Register>()));
2030      // out.hi = out.hi - in.hi
2031      __ sub(out.AsRegisterPairHigh<Register>(),
2032             out.AsRegisterPairHigh<Register>(),
2033             ShifterOperand(in.AsRegisterPairHigh<Register>()));
2034      break;
2035
2036    case Primitive::kPrimFloat:
2037      DCHECK(in.IsFpuRegister());
2038      __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
2039      break;
2040
2041    case Primitive::kPrimDouble:
2042      DCHECK(in.IsFpuRegisterPair());
2043      __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2044               FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2045      break;
2046
2047    default:
2048      LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2049  }
2050}
2051
2052void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
2053  Primitive::Type result_type = conversion->GetResultType();
2054  Primitive::Type input_type = conversion->GetInputType();
2055  DCHECK_NE(result_type, input_type);
2056
2057  // The float-to-long, double-to-long and long-to-float type conversions
2058  // rely on a call to the runtime.
2059  LocationSummary::CallKind call_kind =
2060      (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2061        && result_type == Primitive::kPrimLong)
2062       || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
2063      ? LocationSummary::kCallOnMainOnly
2064      : LocationSummary::kNoCall;
2065  LocationSummary* locations =
2066      new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2067
2068  // The Java language does not allow treating boolean as an integral type but
2069  // our bit representation makes it safe.
2070
2071  switch (result_type) {
2072    case Primitive::kPrimByte:
2073      switch (input_type) {
2074        case Primitive::kPrimLong:
2075          // Type conversion from long to byte is a result of code transformations.
2076        case Primitive::kPrimBoolean:
2077          // Boolean input is a result of code transformations.
2078        case Primitive::kPrimShort:
2079        case Primitive::kPrimInt:
2080        case Primitive::kPrimChar:
2081          // Processing a Dex `int-to-byte' instruction.
2082          locations->SetInAt(0, Location::RequiresRegister());
2083          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2084          break;
2085
2086        default:
2087          LOG(FATAL) << "Unexpected type conversion from " << input_type
2088                     << " to " << result_type;
2089      }
2090      break;
2091
2092    case Primitive::kPrimShort:
2093      switch (input_type) {
2094        case Primitive::kPrimLong:
2095          // Type conversion from long to short is a result of code transformations.
2096        case Primitive::kPrimBoolean:
2097          // Boolean input is a result of code transformations.
2098        case Primitive::kPrimByte:
2099        case Primitive::kPrimInt:
2100        case Primitive::kPrimChar:
2101          // Processing a Dex `int-to-short' instruction.
2102          locations->SetInAt(0, Location::RequiresRegister());
2103          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104          break;
2105
2106        default:
2107          LOG(FATAL) << "Unexpected type conversion from " << input_type
2108                     << " to " << result_type;
2109      }
2110      break;
2111
2112    case Primitive::kPrimInt:
2113      switch (input_type) {
2114        case Primitive::kPrimLong:
2115          // Processing a Dex `long-to-int' instruction.
2116          locations->SetInAt(0, Location::Any());
2117          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2118          break;
2119
2120        case Primitive::kPrimFloat:
2121          // Processing a Dex `float-to-int' instruction.
2122          locations->SetInAt(0, Location::RequiresFpuRegister());
2123          locations->SetOut(Location::RequiresRegister());
2124          locations->AddTemp(Location::RequiresFpuRegister());
2125          break;
2126
2127        case Primitive::kPrimDouble:
2128          // Processing a Dex `double-to-int' instruction.
2129          locations->SetInAt(0, Location::RequiresFpuRegister());
2130          locations->SetOut(Location::RequiresRegister());
2131          locations->AddTemp(Location::RequiresFpuRegister());
2132          break;
2133
2134        default:
2135          LOG(FATAL) << "Unexpected type conversion from " << input_type
2136                     << " to " << result_type;
2137      }
2138      break;
2139
2140    case Primitive::kPrimLong:
2141      switch (input_type) {
2142        case Primitive::kPrimBoolean:
2143          // Boolean input is a result of code transformations.
2144        case Primitive::kPrimByte:
2145        case Primitive::kPrimShort:
2146        case Primitive::kPrimInt:
2147        case Primitive::kPrimChar:
2148          // Processing a Dex `int-to-long' instruction.
2149          locations->SetInAt(0, Location::RequiresRegister());
2150          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2151          break;
2152
2153        case Primitive::kPrimFloat: {
2154          // Processing a Dex `float-to-long' instruction.
2155          InvokeRuntimeCallingConvention calling_convention;
2156          locations->SetInAt(0, Location::FpuRegisterLocation(
2157              calling_convention.GetFpuRegisterAt(0)));
2158          locations->SetOut(Location::RegisterPairLocation(R0, R1));
2159          break;
2160        }
2161
2162        case Primitive::kPrimDouble: {
2163          // Processing a Dex `double-to-long' instruction.
2164          InvokeRuntimeCallingConvention calling_convention;
2165          locations->SetInAt(0, Location::FpuRegisterPairLocation(
2166              calling_convention.GetFpuRegisterAt(0),
2167              calling_convention.GetFpuRegisterAt(1)));
2168          locations->SetOut(Location::RegisterPairLocation(R0, R1));
2169          break;
2170        }
2171
2172        default:
2173          LOG(FATAL) << "Unexpected type conversion from " << input_type
2174                     << " to " << result_type;
2175      }
2176      break;
2177
2178    case Primitive::kPrimChar:
2179      switch (input_type) {
2180        case Primitive::kPrimLong:
2181          // Type conversion from long to char is a result of code transformations.
2182        case Primitive::kPrimBoolean:
2183          // Boolean input is a result of code transformations.
2184        case Primitive::kPrimByte:
2185        case Primitive::kPrimShort:
2186        case Primitive::kPrimInt:
2187          // Processing a Dex `int-to-char' instruction.
2188          locations->SetInAt(0, Location::RequiresRegister());
2189          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190          break;
2191
2192        default:
2193          LOG(FATAL) << "Unexpected type conversion from " << input_type
2194                     << " to " << result_type;
2195      }
2196      break;
2197
2198    case Primitive::kPrimFloat:
2199      switch (input_type) {
2200        case Primitive::kPrimBoolean:
2201          // Boolean input is a result of code transformations.
2202        case Primitive::kPrimByte:
2203        case Primitive::kPrimShort:
2204        case Primitive::kPrimInt:
2205        case Primitive::kPrimChar:
2206          // Processing a Dex `int-to-float' instruction.
2207          locations->SetInAt(0, Location::RequiresRegister());
2208          locations->SetOut(Location::RequiresFpuRegister());
2209          break;
2210
2211        case Primitive::kPrimLong: {
2212          // Processing a Dex `long-to-float' instruction.
2213          InvokeRuntimeCallingConvention calling_convention;
2214          locations->SetInAt(0, Location::RegisterPairLocation(
2215              calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2216          locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2217          break;
2218        }
2219
2220        case Primitive::kPrimDouble:
2221          // Processing a Dex `double-to-float' instruction.
2222          locations->SetInAt(0, Location::RequiresFpuRegister());
2223          locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2224          break;
2225
2226        default:
2227          LOG(FATAL) << "Unexpected type conversion from " << input_type
2228                     << " to " << result_type;
2229      };
2230      break;
2231
2232    case Primitive::kPrimDouble:
2233      switch (input_type) {
2234        case Primitive::kPrimBoolean:
2235          // Boolean input is a result of code transformations.
2236        case Primitive::kPrimByte:
2237        case Primitive::kPrimShort:
2238        case Primitive::kPrimInt:
2239        case Primitive::kPrimChar:
2240          // Processing a Dex `int-to-double' instruction.
2241          locations->SetInAt(0, Location::RequiresRegister());
2242          locations->SetOut(Location::RequiresFpuRegister());
2243          break;
2244
2245        case Primitive::kPrimLong:
2246          // Processing a Dex `long-to-double' instruction.
2247          locations->SetInAt(0, Location::RequiresRegister());
2248          locations->SetOut(Location::RequiresFpuRegister());
2249          locations->AddTemp(Location::RequiresFpuRegister());
2250          locations->AddTemp(Location::RequiresFpuRegister());
2251          break;
2252
2253        case Primitive::kPrimFloat:
2254          // Processing a Dex `float-to-double' instruction.
2255          locations->SetInAt(0, Location::RequiresFpuRegister());
2256          locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2257          break;
2258
2259        default:
2260          LOG(FATAL) << "Unexpected type conversion from " << input_type
2261                     << " to " << result_type;
2262      };
2263      break;
2264
2265    default:
2266      LOG(FATAL) << "Unexpected type conversion from " << input_type
2267                 << " to " << result_type;
2268  }
2269}
2270
2271void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2272  LocationSummary* locations = conversion->GetLocations();
2273  Location out = locations->Out();
2274  Location in = locations->InAt(0);
2275  Primitive::Type result_type = conversion->GetResultType();
2276  Primitive::Type input_type = conversion->GetInputType();
2277  DCHECK_NE(result_type, input_type);
2278  switch (result_type) {
2279    case Primitive::kPrimByte:
2280      switch (input_type) {
2281        case Primitive::kPrimLong:
2282          // Type conversion from long to byte is a result of code transformations.
2283          __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2284          break;
2285        case Primitive::kPrimBoolean:
2286          // Boolean input is a result of code transformations.
2287        case Primitive::kPrimShort:
2288        case Primitive::kPrimInt:
2289        case Primitive::kPrimChar:
2290          // Processing a Dex `int-to-byte' instruction.
2291          __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
2292          break;
2293
2294        default:
2295          LOG(FATAL) << "Unexpected type conversion from " << input_type
2296                     << " to " << result_type;
2297      }
2298      break;
2299
2300    case Primitive::kPrimShort:
2301      switch (input_type) {
2302        case Primitive::kPrimLong:
2303          // Type conversion from long to short is a result of code transformations.
2304          __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2305          break;
2306        case Primitive::kPrimBoolean:
2307          // Boolean input is a result of code transformations.
2308        case Primitive::kPrimByte:
2309        case Primitive::kPrimInt:
2310        case Primitive::kPrimChar:
2311          // Processing a Dex `int-to-short' instruction.
2312          __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
2313          break;
2314
2315        default:
2316          LOG(FATAL) << "Unexpected type conversion from " << input_type
2317                     << " to " << result_type;
2318      }
2319      break;
2320
2321    case Primitive::kPrimInt:
2322      switch (input_type) {
2323        case Primitive::kPrimLong:
2324          // Processing a Dex `long-to-int' instruction.
2325          DCHECK(out.IsRegister());
2326          if (in.IsRegisterPair()) {
2327            __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2328          } else if (in.IsDoubleStackSlot()) {
2329            __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
2330          } else {
2331            DCHECK(in.IsConstant());
2332            DCHECK(in.GetConstant()->IsLongConstant());
2333            int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2334            __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
2335          }
2336          break;
2337
2338        case Primitive::kPrimFloat: {
2339          // Processing a Dex `float-to-int' instruction.
2340          SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2341          __ vcvtis(temp, in.AsFpuRegister<SRegister>());
2342          __ vmovrs(out.AsRegister<Register>(), temp);
2343          break;
2344        }
2345
2346        case Primitive::kPrimDouble: {
2347          // Processing a Dex `double-to-int' instruction.
2348          SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2349          __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2350          __ vmovrs(out.AsRegister<Register>(), temp_s);
2351          break;
2352        }
2353
2354        default:
2355          LOG(FATAL) << "Unexpected type conversion from " << input_type
2356                     << " to " << result_type;
2357      }
2358      break;
2359
2360    case Primitive::kPrimLong:
2361      switch (input_type) {
2362        case Primitive::kPrimBoolean:
2363          // Boolean input is a result of code transformations.
2364        case Primitive::kPrimByte:
2365        case Primitive::kPrimShort:
2366        case Primitive::kPrimInt:
2367        case Primitive::kPrimChar:
2368          // Processing a Dex `int-to-long' instruction.
2369          DCHECK(out.IsRegisterPair());
2370          DCHECK(in.IsRegister());
2371          __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
2372          // Sign extension.
2373          __ Asr(out.AsRegisterPairHigh<Register>(),
2374                 out.AsRegisterPairLow<Register>(),
2375                 31);
2376          break;
2377
2378        case Primitive::kPrimFloat:
2379          // Processing a Dex `float-to-long' instruction.
2380          codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2381                                  conversion,
2382                                  conversion->GetDexPc(),
2383                                  nullptr);
2384          CheckEntrypointTypes<kQuickF2l, int64_t, float>();
2385          break;
2386
2387        case Primitive::kPrimDouble:
2388          // Processing a Dex `double-to-long' instruction.
2389          codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2390                                  conversion,
2391                                  conversion->GetDexPc(),
2392                                  nullptr);
2393          CheckEntrypointTypes<kQuickD2l, int64_t, double>();
2394          break;
2395
2396        default:
2397          LOG(FATAL) << "Unexpected type conversion from " << input_type
2398                     << " to " << result_type;
2399      }
2400      break;
2401
2402    case Primitive::kPrimChar:
2403      switch (input_type) {
2404        case Primitive::kPrimLong:
2405          // Type conversion from long to char is a result of code transformations.
2406          __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2407          break;
2408        case Primitive::kPrimBoolean:
2409          // Boolean input is a result of code transformations.
2410        case Primitive::kPrimByte:
2411        case Primitive::kPrimShort:
2412        case Primitive::kPrimInt:
2413          // Processing a Dex `int-to-char' instruction.
2414          __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
2415          break;
2416
2417        default:
2418          LOG(FATAL) << "Unexpected type conversion from " << input_type
2419                     << " to " << result_type;
2420      }
2421      break;
2422
2423    case Primitive::kPrimFloat:
2424      switch (input_type) {
2425        case Primitive::kPrimBoolean:
2426          // Boolean input is a result of code transformations.
2427        case Primitive::kPrimByte:
2428        case Primitive::kPrimShort:
2429        case Primitive::kPrimInt:
2430        case Primitive::kPrimChar: {
2431          // Processing a Dex `int-to-float' instruction.
2432          __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2433          __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
2434          break;
2435        }
2436
2437        case Primitive::kPrimLong:
2438          // Processing a Dex `long-to-float' instruction.
2439          codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2440                                  conversion,
2441                                  conversion->GetDexPc(),
2442                                  nullptr);
2443          CheckEntrypointTypes<kQuickL2f, float, int64_t>();
2444          break;
2445
2446        case Primitive::kPrimDouble:
2447          // Processing a Dex `double-to-float' instruction.
2448          __ vcvtsd(out.AsFpuRegister<SRegister>(),
2449                    FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2450          break;
2451
2452        default:
2453          LOG(FATAL) << "Unexpected type conversion from " << input_type
2454                     << " to " << result_type;
2455      };
2456      break;
2457
2458    case Primitive::kPrimDouble:
2459      switch (input_type) {
2460        case Primitive::kPrimBoolean:
2461          // Boolean input is a result of code transformations.
2462        case Primitive::kPrimByte:
2463        case Primitive::kPrimShort:
2464        case Primitive::kPrimInt:
2465        case Primitive::kPrimChar: {
2466          // Processing a Dex `int-to-double' instruction.
2467          __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
2468          __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2469                    out.AsFpuRegisterPairLow<SRegister>());
2470          break;
2471        }
2472
2473        case Primitive::kPrimLong: {
2474          // Processing a Dex `long-to-double' instruction.
2475          Register low = in.AsRegisterPairLow<Register>();
2476          Register high = in.AsRegisterPairHigh<Register>();
2477          SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2478          DRegister out_d = FromLowSToD(out_s);
2479          SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2480          DRegister temp_d = FromLowSToD(temp_s);
2481          SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2482          DRegister constant_d = FromLowSToD(constant_s);
2483
2484          // temp_d = int-to-double(high)
2485          __ vmovsr(temp_s, high);
2486          __ vcvtdi(temp_d, temp_s);
2487          // constant_d = k2Pow32EncodingForDouble
2488          __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2489          // out_d = unsigned-to-double(low)
2490          __ vmovsr(out_s, low);
2491          __ vcvtdu(out_d, out_s);
2492          // out_d += temp_d * constant_d
2493          __ vmlad(out_d, temp_d, constant_d);
2494          break;
2495        }
2496
2497        case Primitive::kPrimFloat:
2498          // Processing a Dex `float-to-double' instruction.
2499          __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2500                    in.AsFpuRegister<SRegister>());
2501          break;
2502
2503        default:
2504          LOG(FATAL) << "Unexpected type conversion from " << input_type
2505                     << " to " << result_type;
2506      };
2507      break;
2508
2509    default:
2510      LOG(FATAL) << "Unexpected type conversion from " << input_type
2511                 << " to " << result_type;
2512  }
2513}
2514
2515void LocationsBuilderARM::VisitAdd(HAdd* add) {
2516  LocationSummary* locations =
2517      new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
2518  switch (add->GetResultType()) {
2519    case Primitive::kPrimInt: {
2520      locations->SetInAt(0, Location::RequiresRegister());
2521      locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2522      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2523      break;
2524    }
2525
2526    case Primitive::kPrimLong: {
2527      locations->SetInAt(0, Location::RequiresRegister());
2528      locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
2529      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2530      break;
2531    }
2532
2533    case Primitive::kPrimFloat:
2534    case Primitive::kPrimDouble: {
2535      locations->SetInAt(0, Location::RequiresFpuRegister());
2536      locations->SetInAt(1, Location::RequiresFpuRegister());
2537      locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2538      break;
2539    }
2540
2541    default:
2542      LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2543  }
2544}
2545
2546void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2547  LocationSummary* locations = add->GetLocations();
2548  Location out = locations->Out();
2549  Location first = locations->InAt(0);
2550  Location second = locations->InAt(1);
2551  switch (add->GetResultType()) {
2552    case Primitive::kPrimInt:
2553      if (second.IsRegister()) {
2554        __ add(out.AsRegister<Register>(),
2555               first.AsRegister<Register>(),
2556               ShifterOperand(second.AsRegister<Register>()));
2557      } else {
2558        __ AddConstant(out.AsRegister<Register>(),
2559                       first.AsRegister<Register>(),
2560                       second.GetConstant()->AsIntConstant()->GetValue());
2561      }
2562      break;
2563
2564    case Primitive::kPrimLong: {
2565      if (second.IsConstant()) {
2566        uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2567        GenerateAddLongConst(out, first, value);
2568      } else {
2569        DCHECK(second.IsRegisterPair());
2570        __ adds(out.AsRegisterPairLow<Register>(),
2571                first.AsRegisterPairLow<Register>(),
2572                ShifterOperand(second.AsRegisterPairLow<Register>()));
2573        __ adc(out.AsRegisterPairHigh<Register>(),
2574               first.AsRegisterPairHigh<Register>(),
2575               ShifterOperand(second.AsRegisterPairHigh<Register>()));
2576      }
2577      break;
2578    }
2579
2580    case Primitive::kPrimFloat:
2581      __ vadds(out.AsFpuRegister<SRegister>(),
2582               first.AsFpuRegister<SRegister>(),
2583               second.AsFpuRegister<SRegister>());
2584      break;
2585
2586    case Primitive::kPrimDouble:
2587      __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2588               FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2589               FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2590      break;
2591
2592    default:
2593      LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2594  }
2595}
2596
2597void LocationsBuilderARM::VisitSub(HSub* sub) {
2598  LocationSummary* locations =
2599      new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
2600  switch (sub->GetResultType()) {
2601    case Primitive::kPrimInt: {
2602      locations->SetInAt(0, Location::RequiresRegister());
2603      locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
2604      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2605      break;
2606    }
2607
2608    case Primitive::kPrimLong: {
2609      locations->SetInAt(0, Location::RequiresRegister());
2610      locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
2611      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612      break;
2613    }
2614    case Primitive::kPrimFloat:
2615    case Primitive::kPrimDouble: {
2616      locations->SetInAt(0, Location::RequiresFpuRegister());
2617      locations->SetInAt(1, Location::RequiresFpuRegister());
2618      locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2619      break;
2620    }
2621    default:
2622      LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2623  }
2624}
2625
2626void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2627  LocationSummary* locations = sub->GetLocations();
2628  Location out = locations->Out();
2629  Location first = locations->InAt(0);
2630  Location second = locations->InAt(1);
2631  switch (sub->GetResultType()) {
2632    case Primitive::kPrimInt: {
2633      if (second.IsRegister()) {
2634        __ sub(out.AsRegister<Register>(),
2635               first.AsRegister<Register>(),
2636               ShifterOperand(second.AsRegister<Register>()));
2637      } else {
2638        __ AddConstant(out.AsRegister<Register>(),
2639                       first.AsRegister<Register>(),
2640                       -second.GetConstant()->AsIntConstant()->GetValue());
2641      }
2642      break;
2643    }
2644
2645    case Primitive::kPrimLong: {
2646      if (second.IsConstant()) {
2647        uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2648        GenerateAddLongConst(out, first, -value);
2649      } else {
2650        DCHECK(second.IsRegisterPair());
2651        __ subs(out.AsRegisterPairLow<Register>(),
2652                first.AsRegisterPairLow<Register>(),
2653                ShifterOperand(second.AsRegisterPairLow<Register>()));
2654        __ sbc(out.AsRegisterPairHigh<Register>(),
2655               first.AsRegisterPairHigh<Register>(),
2656               ShifterOperand(second.AsRegisterPairHigh<Register>()));
2657      }
2658      break;
2659    }
2660
2661    case Primitive::kPrimFloat: {
2662      __ vsubs(out.AsFpuRegister<SRegister>(),
2663               first.AsFpuRegister<SRegister>(),
2664               second.AsFpuRegister<SRegister>());
2665      break;
2666    }
2667
2668    case Primitive::kPrimDouble: {
2669      __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2670               FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2671               FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2672      break;
2673    }
2674
2675
2676    default:
2677      LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
2678  }
2679}
2680
2681void LocationsBuilderARM::VisitMul(HMul* mul) {
2682  LocationSummary* locations =
2683      new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2684  switch (mul->GetResultType()) {
2685    case Primitive::kPrimInt:
2686    case Primitive::kPrimLong:  {
2687      locations->SetInAt(0, Location::RequiresRegister());
2688      locations->SetInAt(1, Location::RequiresRegister());
2689      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2690      break;
2691    }
2692
2693    case Primitive::kPrimFloat:
2694    case Primitive::kPrimDouble: {
2695      locations->SetInAt(0, Location::RequiresFpuRegister());
2696      locations->SetInAt(1, Location::RequiresFpuRegister());
2697      locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2698      break;
2699    }
2700
2701    default:
2702      LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2703  }
2704}
2705
2706void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2707  LocationSummary* locations = mul->GetLocations();
2708  Location out = locations->Out();
2709  Location first = locations->InAt(0);
2710  Location second = locations->InAt(1);
2711  switch (mul->GetResultType()) {
2712    case Primitive::kPrimInt: {
2713      __ mul(out.AsRegister<Register>(),
2714             first.AsRegister<Register>(),
2715             second.AsRegister<Register>());
2716      break;
2717    }
2718    case Primitive::kPrimLong: {
2719      Register out_hi = out.AsRegisterPairHigh<Register>();
2720      Register out_lo = out.AsRegisterPairLow<Register>();
2721      Register in1_hi = first.AsRegisterPairHigh<Register>();
2722      Register in1_lo = first.AsRegisterPairLow<Register>();
2723      Register in2_hi = second.AsRegisterPairHigh<Register>();
2724      Register in2_lo = second.AsRegisterPairLow<Register>();
2725
2726      // Extra checks to protect caused by the existence of R1_R2.
2727      // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2728      // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2729      DCHECK_NE(out_hi, in1_lo);
2730      DCHECK_NE(out_hi, in2_lo);
2731
2732      // input: in1 - 64 bits, in2 - 64 bits
2733      // output: out
2734      // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2735      // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2736      // parts: out.lo = (in1.lo * in2.lo)[31:0]
2737
2738      // IP <- in1.lo * in2.hi
2739      __ mul(IP, in1_lo, in2_hi);
2740      // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2741      __ mla(out_hi, in1_hi, in2_lo, IP);
2742      // out.lo <- (in1.lo * in2.lo)[31:0];
2743      __ umull(out_lo, IP, in1_lo, in2_lo);
2744      // out.hi <- in2.hi * in1.lo +  in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2745      __ add(out_hi, out_hi, ShifterOperand(IP));
2746      break;
2747    }
2748
2749    case Primitive::kPrimFloat: {
2750      __ vmuls(out.AsFpuRegister<SRegister>(),
2751               first.AsFpuRegister<SRegister>(),
2752               second.AsFpuRegister<SRegister>());
2753      break;
2754    }
2755
2756    case Primitive::kPrimDouble: {
2757      __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2758               FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2759               FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2760      break;
2761    }
2762
2763    default:
2764      LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2765  }
2766}
2767
2768void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2769  DCHECK(instruction->IsDiv() || instruction->IsRem());
2770  DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2771
2772  LocationSummary* locations = instruction->GetLocations();
2773  Location second = locations->InAt(1);
2774  DCHECK(second.IsConstant());
2775
2776  Register out = locations->Out().AsRegister<Register>();
2777  Register dividend = locations->InAt(0).AsRegister<Register>();
2778  int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2779  DCHECK(imm == 1 || imm == -1);
2780
2781  if (instruction->IsRem()) {
2782    __ LoadImmediate(out, 0);
2783  } else {
2784    if (imm == 1) {
2785      __ Mov(out, dividend);
2786    } else {
2787      __ rsb(out, dividend, ShifterOperand(0));
2788    }
2789  }
2790}
2791
2792void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2793  DCHECK(instruction->IsDiv() || instruction->IsRem());
2794  DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2795
2796  LocationSummary* locations = instruction->GetLocations();
2797  Location second = locations->InAt(1);
2798  DCHECK(second.IsConstant());
2799
2800  Register out = locations->Out().AsRegister<Register>();
2801  Register dividend = locations->InAt(0).AsRegister<Register>();
2802  Register temp = locations->GetTemp(0).AsRegister<Register>();
2803  int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2804  uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
2805  int ctz_imm = CTZ(abs_imm);
2806
2807  if (ctz_imm == 1) {
2808    __ Lsr(temp, dividend, 32 - ctz_imm);
2809  } else {
2810    __ Asr(temp, dividend, 31);
2811    __ Lsr(temp, temp, 32 - ctz_imm);
2812  }
2813  __ add(out, temp, ShifterOperand(dividend));
2814
2815  if (instruction->IsDiv()) {
2816    __ Asr(out, out, ctz_imm);
2817    if (imm < 0) {
2818      __ rsb(out, out, ShifterOperand(0));
2819    }
2820  } else {
2821    __ ubfx(out, out, 0, ctz_imm);
2822    __ sub(out, out, ShifterOperand(temp));
2823  }
2824}
2825
2826void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2827  DCHECK(instruction->IsDiv() || instruction->IsRem());
2828  DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2829
2830  LocationSummary* locations = instruction->GetLocations();
2831  Location second = locations->InAt(1);
2832  DCHECK(second.IsConstant());
2833
2834  Register out = locations->Out().AsRegister<Register>();
2835  Register dividend = locations->InAt(0).AsRegister<Register>();
2836  Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2837  Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2838  int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2839
2840  int64_t magic;
2841  int shift;
2842  CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2843
2844  __ LoadImmediate(temp1, magic);
2845  __ smull(temp2, temp1, dividend, temp1);
2846
2847  if (imm > 0 && magic < 0) {
2848    __ add(temp1, temp1, ShifterOperand(dividend));
2849  } else if (imm < 0 && magic > 0) {
2850    __ sub(temp1, temp1, ShifterOperand(dividend));
2851  }
2852
2853  if (shift != 0) {
2854    __ Asr(temp1, temp1, shift);
2855  }
2856
2857  if (instruction->IsDiv()) {
2858    __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2859  } else {
2860    __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2861    // TODO: Strength reduction for mls.
2862    __ LoadImmediate(temp2, imm);
2863    __ mls(out, temp1, temp2, dividend);
2864  }
2865}
2866
2867void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2868  DCHECK(instruction->IsDiv() || instruction->IsRem());
2869  DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2870
2871  LocationSummary* locations = instruction->GetLocations();
2872  Location second = locations->InAt(1);
2873  DCHECK(second.IsConstant());
2874
2875  int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2876  if (imm == 0) {
2877    // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2878  } else if (imm == 1 || imm == -1) {
2879    DivRemOneOrMinusOne(instruction);
2880  } else if (IsPowerOfTwo(AbsOrMin(imm))) {
2881    DivRemByPowerOfTwo(instruction);
2882  } else {
2883    DCHECK(imm <= -2 || imm >= 2);
2884    GenerateDivRemWithAnyConstant(instruction);
2885  }
2886}
2887
2888void LocationsBuilderARM::VisitDiv(HDiv* div) {
2889  LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2890  if (div->GetResultType() == Primitive::kPrimLong) {
2891    // pLdiv runtime call.
2892    call_kind = LocationSummary::kCallOnMainOnly;
2893  } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2894    // sdiv will be replaced by other instruction sequence.
2895  } else if (div->GetResultType() == Primitive::kPrimInt &&
2896             !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2897    // pIdivmod runtime call.
2898    call_kind = LocationSummary::kCallOnMainOnly;
2899  }
2900
2901  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2902
2903  switch (div->GetResultType()) {
2904    case Primitive::kPrimInt: {
2905      if (div->InputAt(1)->IsConstant()) {
2906        locations->SetInAt(0, Location::RequiresRegister());
2907        locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
2908        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2909        int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2910        if (value == 1 || value == 0 || value == -1) {
2911          // No temp register required.
2912        } else {
2913          locations->AddTemp(Location::RequiresRegister());
2914          if (!IsPowerOfTwo(AbsOrMin(value))) {
2915            locations->AddTemp(Location::RequiresRegister());
2916          }
2917        }
2918      } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2919        locations->SetInAt(0, Location::RequiresRegister());
2920        locations->SetInAt(1, Location::RequiresRegister());
2921        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2922      } else {
2923        InvokeRuntimeCallingConvention calling_convention;
2924        locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2925        locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2926        // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2927        //       we only need the former.
2928        locations->SetOut(Location::RegisterLocation(R0));
2929      }
2930      break;
2931    }
2932    case Primitive::kPrimLong: {
2933      InvokeRuntimeCallingConvention calling_convention;
2934      locations->SetInAt(0, Location::RegisterPairLocation(
2935          calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2936      locations->SetInAt(1, Location::RegisterPairLocation(
2937          calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2938      locations->SetOut(Location::RegisterPairLocation(R0, R1));
2939      break;
2940    }
2941    case Primitive::kPrimFloat:
2942    case Primitive::kPrimDouble: {
2943      locations->SetInAt(0, Location::RequiresFpuRegister());
2944      locations->SetInAt(1, Location::RequiresFpuRegister());
2945      locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2946      break;
2947    }
2948
2949    default:
2950      LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2951  }
2952}
2953
2954void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2955  LocationSummary* locations = div->GetLocations();
2956  Location out = locations->Out();
2957  Location first = locations->InAt(0);
2958  Location second = locations->InAt(1);
2959
2960  switch (div->GetResultType()) {
2961    case Primitive::kPrimInt: {
2962      if (second.IsConstant()) {
2963        GenerateDivRemConstantIntegral(div);
2964      } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2965        __ sdiv(out.AsRegister<Register>(),
2966                first.AsRegister<Register>(),
2967                second.AsRegister<Register>());
2968      } else {
2969        InvokeRuntimeCallingConvention calling_convention;
2970        DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2971        DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2972        DCHECK_EQ(R0, out.AsRegister<Register>());
2973
2974        codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2975        CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
2976      }
2977      break;
2978    }
2979
2980    case Primitive::kPrimLong: {
2981      InvokeRuntimeCallingConvention calling_convention;
2982      DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2983      DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2984      DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2985      DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2986      DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
2987      DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
2988
2989      codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
2990      CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2991      break;
2992    }
2993
2994    case Primitive::kPrimFloat: {
2995      __ vdivs(out.AsFpuRegister<SRegister>(),
2996               first.AsFpuRegister<SRegister>(),
2997               second.AsFpuRegister<SRegister>());
2998      break;
2999    }
3000
3001    case Primitive::kPrimDouble: {
3002      __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3003               FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3004               FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3005      break;
3006    }
3007
3008    default:
3009      LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3010  }
3011}
3012
3013void LocationsBuilderARM::VisitRem(HRem* rem) {
3014  Primitive::Type type = rem->GetResultType();
3015
3016  // Most remainders are implemented in the runtime.
3017  LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
3018  if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3019    // sdiv will be replaced by other instruction sequence.
3020    call_kind = LocationSummary::kNoCall;
3021  } else if ((rem->GetResultType() == Primitive::kPrimInt)
3022             && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3023    // Have hardware divide instruction for int, do it with three instructions.
3024    call_kind = LocationSummary::kNoCall;
3025  }
3026
3027  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3028
3029  switch (type) {
3030    case Primitive::kPrimInt: {
3031      if (rem->InputAt(1)->IsConstant()) {
3032        locations->SetInAt(0, Location::RequiresRegister());
3033        locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
3034        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3035        int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3036        if (value == 1 || value == 0 || value == -1) {
3037          // No temp register required.
3038        } else {
3039          locations->AddTemp(Location::RequiresRegister());
3040          if (!IsPowerOfTwo(AbsOrMin(value))) {
3041            locations->AddTemp(Location::RequiresRegister());
3042          }
3043        }
3044      } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3045        locations->SetInAt(0, Location::RequiresRegister());
3046        locations->SetInAt(1, Location::RequiresRegister());
3047        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3048        locations->AddTemp(Location::RequiresRegister());
3049      } else {
3050        InvokeRuntimeCallingConvention calling_convention;
3051        locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3052        locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3053        // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3054        //       we only need the latter.
3055        locations->SetOut(Location::RegisterLocation(R1));
3056      }
3057      break;
3058    }
3059    case Primitive::kPrimLong: {
3060      InvokeRuntimeCallingConvention calling_convention;
3061      locations->SetInAt(0, Location::RegisterPairLocation(
3062          calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3063      locations->SetInAt(1, Location::RegisterPairLocation(
3064          calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3065      // The runtime helper puts the output in R2,R3.
3066      locations->SetOut(Location::RegisterPairLocation(R2, R3));
3067      break;
3068    }
3069    case Primitive::kPrimFloat: {
3070      InvokeRuntimeCallingConvention calling_convention;
3071      locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3072      locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3073      locations->SetOut(Location::FpuRegisterLocation(S0));
3074      break;
3075    }
3076
3077    case Primitive::kPrimDouble: {
3078      InvokeRuntimeCallingConvention calling_convention;
3079      locations->SetInAt(0, Location::FpuRegisterPairLocation(
3080          calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3081      locations->SetInAt(1, Location::FpuRegisterPairLocation(
3082          calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3083      locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
3084      break;
3085    }
3086
3087    default:
3088      LOG(FATAL) << "Unexpected rem type " << type;
3089  }
3090}
3091
3092void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3093  LocationSummary* locations = rem->GetLocations();
3094  Location out = locations->Out();
3095  Location first = locations->InAt(0);
3096  Location second = locations->InAt(1);
3097
3098  Primitive::Type type = rem->GetResultType();
3099  switch (type) {
3100    case Primitive::kPrimInt: {
3101        if (second.IsConstant()) {
3102          GenerateDivRemConstantIntegral(rem);
3103        } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3104        Register reg1 = first.AsRegister<Register>();
3105        Register reg2 = second.AsRegister<Register>();
3106        Register temp = locations->GetTemp(0).AsRegister<Register>();
3107
3108        // temp = reg1 / reg2  (integer division)
3109        // dest = reg1 - temp * reg2
3110        __ sdiv(temp, reg1, reg2);
3111        __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
3112      } else {
3113        InvokeRuntimeCallingConvention calling_convention;
3114        DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3115        DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3116        DCHECK_EQ(R1, out.AsRegister<Register>());
3117
3118        codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
3119        CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
3120      }
3121      break;
3122    }
3123
3124    case Primitive::kPrimLong: {
3125      codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
3126        CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3127      break;
3128    }
3129
3130    case Primitive::kPrimFloat: {
3131      codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
3132      CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3133      break;
3134    }
3135
3136    case Primitive::kPrimDouble: {
3137      codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
3138      CheckEntrypointTypes<kQuickFmod, double, double, double>();
3139      break;
3140    }
3141
3142    default:
3143      LOG(FATAL) << "Unexpected rem type " << type;
3144  }
3145}
3146
3147void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3148  LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3149      ? LocationSummary::kCallOnSlowPath
3150      : LocationSummary::kNoCall;
3151  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3152  locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3153  if (instruction->HasUses()) {
3154    locations->SetOut(Location::SameAsFirstInput());
3155  }
3156}
3157
3158void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3159  SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
3160  codegen_->AddSlowPath(slow_path);
3161
3162  LocationSummary* locations = instruction->GetLocations();
3163  Location value = locations->InAt(0);
3164
3165  switch (instruction->GetType()) {
3166    case Primitive::kPrimBoolean:
3167    case Primitive::kPrimByte:
3168    case Primitive::kPrimChar:
3169    case Primitive::kPrimShort:
3170    case Primitive::kPrimInt: {
3171      if (value.IsRegister()) {
3172        __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3173      } else {
3174        DCHECK(value.IsConstant()) << value;
3175        if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3176          __ b(slow_path->GetEntryLabel());
3177        }
3178      }
3179      break;
3180    }
3181    case Primitive::kPrimLong: {
3182      if (value.IsRegisterPair()) {
3183        __ orrs(IP,
3184                value.AsRegisterPairLow<Register>(),
3185                ShifterOperand(value.AsRegisterPairHigh<Register>()));
3186        __ b(slow_path->GetEntryLabel(), EQ);
3187      } else {
3188        DCHECK(value.IsConstant()) << value;
3189        if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3190          __ b(slow_path->GetEntryLabel());
3191        }
3192      }
3193      break;
3194    default:
3195      LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3196    }
3197  }
3198}
3199
3200void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3201  Register in = locations->InAt(0).AsRegister<Register>();
3202  Location rhs = locations->InAt(1);
3203  Register out = locations->Out().AsRegister<Register>();
3204
3205  if (rhs.IsConstant()) {
3206    // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3207    // so map all rotations to a +ve. equivalent in that range.
3208    // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3209    uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3210    if (rot) {
3211      // Rotate, mapping left rotations to right equivalents if necessary.
3212      // (e.g. left by 2 bits == right by 30.)
3213      __ Ror(out, in, rot);
3214    } else if (out != in) {
3215      __ Mov(out, in);
3216    }
3217  } else {
3218    __ Ror(out, in, rhs.AsRegister<Register>());
3219  }
3220}
3221
3222// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3223// rotates by swapping input regs (effectively rotating by the first 32-bits of
3224// a larger rotation) or flipping direction (thus treating larger right/left
3225// rotations as sub-word sized rotations in the other direction) as appropriate.
3226void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3227  Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3228  Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3229  Location rhs = locations->InAt(1);
3230  Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3231  Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3232
3233  if (rhs.IsConstant()) {
3234    uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3235    // Map all rotations to +ve. equivalents on the interval [0,63].
3236    rot &= kMaxLongShiftDistance;
3237    // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3238    // logic below to a simple pair of binary orr.
3239    // (e.g. 34 bits == in_reg swap + 2 bits right.)
3240    if (rot >= kArmBitsPerWord) {
3241      rot -= kArmBitsPerWord;
3242      std::swap(in_reg_hi, in_reg_lo);
3243    }
3244    // Rotate, or mov to out for zero or word size rotations.
3245    if (rot != 0u) {
3246      __ Lsr(out_reg_hi, in_reg_hi, rot);
3247      __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3248      __ Lsr(out_reg_lo, in_reg_lo, rot);
3249      __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3250    } else {
3251      __ Mov(out_reg_lo, in_reg_lo);
3252      __ Mov(out_reg_hi, in_reg_hi);
3253    }
3254  } else {
3255    Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3256    Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3257    Label end;
3258    Label shift_by_32_plus_shift_right;
3259
3260    __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3261    __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3262    __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3263    __ b(&shift_by_32_plus_shift_right, CC);
3264
3265    // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3266    // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3267    __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3268    __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3269    __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3270    __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3271    __ Lsr(shift_left, in_reg_hi, shift_right);
3272    __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3273    __ b(&end);
3274
3275    __ Bind(&shift_by_32_plus_shift_right);  // Shift by 32+shift_right.
3276    // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3277    // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3278    __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3279    __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3280    __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3281    __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3282    __ Lsl(shift_right, in_reg_hi, shift_left);
3283    __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3284
3285    __ Bind(&end);
3286  }
3287}
3288
3289void LocationsBuilderARM::VisitRor(HRor* ror) {
3290  LocationSummary* locations =
3291      new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3292  switch (ror->GetResultType()) {
3293    case Primitive::kPrimInt: {
3294      locations->SetInAt(0, Location::RequiresRegister());
3295      locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3296      locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3297      break;
3298    }
3299    case Primitive::kPrimLong: {
3300      locations->SetInAt(0, Location::RequiresRegister());
3301      if (ror->InputAt(1)->IsConstant()) {
3302        locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3303      } else {
3304        locations->SetInAt(1, Location::RequiresRegister());
3305        locations->AddTemp(Location::RequiresRegister());
3306        locations->AddTemp(Location::RequiresRegister());
3307      }
3308      locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3309      break;
3310    }
3311    default:
3312      LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3313  }
3314}
3315
3316void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
3317  LocationSummary* locations = ror->GetLocations();
3318  Primitive::Type type = ror->GetResultType();
3319  switch (type) {
3320    case Primitive::kPrimInt: {
3321      HandleIntegerRotate(locations);
3322      break;
3323    }
3324    case Primitive::kPrimLong: {
3325      HandleLongRotate(locations);
3326      break;
3327    }
3328    default:
3329      LOG(FATAL) << "Unexpected operation type " << type;
3330      UNREACHABLE();
3331  }
3332}
3333
3334void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3335  DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3336
3337  LocationSummary* locations =
3338      new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3339
3340  switch (op->GetResultType()) {
3341    case Primitive::kPrimInt: {
3342      locations->SetInAt(0, Location::RequiresRegister());
3343      if (op->InputAt(1)->IsConstant()) {
3344        locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3345        locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3346      } else {
3347        locations->SetInAt(1, Location::RequiresRegister());
3348        // Make the output overlap, as it will be used to hold the masked
3349        // second input.
3350        locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3351      }
3352      break;
3353    }
3354    case Primitive::kPrimLong: {
3355      locations->SetInAt(0, Location::RequiresRegister());
3356      if (op->InputAt(1)->IsConstant()) {
3357        locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3358        // For simplicity, use kOutputOverlap even though we only require that low registers
3359        // don't clash with high registers which the register allocator currently guarantees.
3360        locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3361      } else {
3362        locations->SetInAt(1, Location::RequiresRegister());
3363        locations->AddTemp(Location::RequiresRegister());
3364        locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3365      }
3366      break;
3367    }
3368    default:
3369      LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3370  }
3371}
3372
3373void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3374  DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3375
3376  LocationSummary* locations = op->GetLocations();
3377  Location out = locations->Out();
3378  Location first = locations->InAt(0);
3379  Location second = locations->InAt(1);
3380
3381  Primitive::Type type = op->GetResultType();
3382  switch (type) {
3383    case Primitive::kPrimInt: {
3384      Register out_reg = out.AsRegister<Register>();
3385      Register first_reg = first.AsRegister<Register>();
3386      if (second.IsRegister()) {
3387        Register second_reg = second.AsRegister<Register>();
3388        // ARM doesn't mask the shift count so we need to do it ourselves.
3389        __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
3390        if (op->IsShl()) {
3391          __ Lsl(out_reg, first_reg, out_reg);
3392        } else if (op->IsShr()) {
3393          __ Asr(out_reg, first_reg, out_reg);
3394        } else {
3395          __ Lsr(out_reg, first_reg, out_reg);
3396        }
3397      } else {
3398        int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3399        uint32_t shift_value = cst & kMaxIntShiftDistance;
3400        if (shift_value == 0) {  // ARM does not support shifting with 0 immediate.
3401          __ Mov(out_reg, first_reg);
3402        } else if (op->IsShl()) {
3403          __ Lsl(out_reg, first_reg, shift_value);
3404        } else if (op->IsShr()) {
3405          __ Asr(out_reg, first_reg, shift_value);
3406        } else {
3407          __ Lsr(out_reg, first_reg, shift_value);
3408        }
3409      }
3410      break;
3411    }
3412    case Primitive::kPrimLong: {
3413      Register o_h = out.AsRegisterPairHigh<Register>();
3414      Register o_l = out.AsRegisterPairLow<Register>();
3415
3416      Register high = first.AsRegisterPairHigh<Register>();
3417      Register low = first.AsRegisterPairLow<Register>();
3418
3419      if (second.IsRegister()) {
3420        Register temp = locations->GetTemp(0).AsRegister<Register>();
3421
3422        Register second_reg = second.AsRegister<Register>();
3423
3424        if (op->IsShl()) {
3425          __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
3426          // Shift the high part
3427          __ Lsl(o_h, high, o_l);
3428          // Shift the low part and `or` what overflew on the high part
3429          __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3430          __ Lsr(temp, low, temp);
3431          __ orr(o_h, o_h, ShifterOperand(temp));
3432          // If the shift is > 32 bits, override the high part
3433          __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3434          __ it(PL);
3435          __ Lsl(o_h, low, temp, PL);
3436          // Shift the low part
3437          __ Lsl(o_l, low, o_l);
3438        } else if (op->IsShr()) {
3439          __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
3440          // Shift the low part
3441          __ Lsr(o_l, low, o_h);
3442          // Shift the high part and `or` what underflew on the low part
3443          __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3444          __ Lsl(temp, high, temp);
3445          __ orr(o_l, o_l, ShifterOperand(temp));
3446          // If the shift is > 32 bits, override the low part
3447          __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3448          __ it(PL);
3449          __ Asr(o_l, high, temp, PL);
3450          // Shift the high part
3451          __ Asr(o_h, high, o_h);
3452        } else {
3453          __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
3454          // same as Shr except we use `Lsr`s and not `Asr`s
3455          __ Lsr(o_l, low, o_h);
3456          __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3457          __ Lsl(temp, high, temp);
3458          __ orr(o_l, o_l, ShifterOperand(temp));
3459          __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3460          __ it(PL);
3461          __ Lsr(o_l, high, temp, PL);
3462          __ Lsr(o_h, high, o_h);
3463        }
3464      } else {
3465        // Register allocator doesn't create partial overlap.
3466        DCHECK_NE(o_l, high);
3467        DCHECK_NE(o_h, low);
3468        int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3469        uint32_t shift_value = cst & kMaxLongShiftDistance;
3470        if (shift_value > 32) {
3471          if (op->IsShl()) {
3472            __ Lsl(o_h, low, shift_value - 32);
3473            __ LoadImmediate(o_l, 0);
3474          } else if (op->IsShr()) {
3475            __ Asr(o_l, high, shift_value - 32);
3476            __ Asr(o_h, high, 31);
3477          } else {
3478            __ Lsr(o_l, high, shift_value - 32);
3479            __ LoadImmediate(o_h, 0);
3480          }
3481        } else if (shift_value == 32) {
3482          if (op->IsShl()) {
3483            __ mov(o_h, ShifterOperand(low));
3484            __ LoadImmediate(o_l, 0);
3485          } else if (op->IsShr()) {
3486            __ mov(o_l, ShifterOperand(high));
3487            __ Asr(o_h, high, 31);
3488          } else {
3489            __ mov(o_l, ShifterOperand(high));
3490            __ LoadImmediate(o_h, 0);
3491          }
3492        } else if (shift_value == 1) {
3493          if (op->IsShl()) {
3494            __ Lsls(o_l, low, 1);
3495            __ adc(o_h, high, ShifterOperand(high));
3496          } else if (op->IsShr()) {
3497            __ Asrs(o_h, high, 1);
3498            __ Rrx(o_l, low);
3499          } else {
3500            __ Lsrs(o_h, high, 1);
3501            __ Rrx(o_l, low);
3502          }
3503        } else {
3504          DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
3505          if (op->IsShl()) {
3506            __ Lsl(o_h, high, shift_value);
3507            __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3508            __ Lsl(o_l, low, shift_value);
3509          } else if (op->IsShr()) {
3510            __ Lsr(o_l, low, shift_value);
3511            __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3512            __ Asr(o_h, high, shift_value);
3513          } else {
3514            __ Lsr(o_l, low, shift_value);
3515            __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3516            __ Lsr(o_h, high, shift_value);
3517          }
3518        }
3519      }
3520      break;
3521    }
3522    default:
3523      LOG(FATAL) << "Unexpected operation type " << type;
3524      UNREACHABLE();
3525  }
3526}
3527
3528void LocationsBuilderARM::VisitShl(HShl* shl) {
3529  HandleShift(shl);
3530}
3531
3532void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3533  HandleShift(shl);
3534}
3535
3536void LocationsBuilderARM::VisitShr(HShr* shr) {
3537  HandleShift(shr);
3538}
3539
3540void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3541  HandleShift(shr);
3542}
3543
3544void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3545  HandleShift(ushr);
3546}
3547
3548void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3549  HandleShift(ushr);
3550}
3551
3552void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
3553  LocationSummary* locations =
3554      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3555  if (instruction->IsStringAlloc()) {
3556    locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3557  } else {
3558    InvokeRuntimeCallingConvention calling_convention;
3559    locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3560    locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3561  }
3562  locations->SetOut(Location::RegisterLocation(R0));
3563}
3564
3565void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3566  // Note: if heap poisoning is enabled, the entry point takes cares
3567  // of poisoning the reference.
3568  if (instruction->IsStringAlloc()) {
3569    // String is allocated through StringFactory. Call NewEmptyString entry point.
3570    Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3571    MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
3572    __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3573    __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3574    __ blx(LR);
3575    codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3576  } else {
3577    codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3578                            instruction,
3579                            instruction->GetDexPc(),
3580                            nullptr);
3581    CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3582  }
3583}
3584
3585void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3586  LocationSummary* locations =
3587      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
3588  InvokeRuntimeCallingConvention calling_convention;
3589  locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3590  locations->SetOut(Location::RegisterLocation(R0));
3591  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3592  locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3593}
3594
3595void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3596  InvokeRuntimeCallingConvention calling_convention;
3597  __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3598  // Note: if heap poisoning is enabled, the entry point takes cares
3599  // of poisoning the reference.
3600  codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3601                          instruction,
3602                          instruction->GetDexPc(),
3603                          nullptr);
3604  CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3605}
3606
3607void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
3608  LocationSummary* locations =
3609      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3610  Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3611  if (location.IsStackSlot()) {
3612    location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3613  } else if (location.IsDoubleStackSlot()) {
3614    location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3615  }
3616  locations->SetOut(location);
3617}
3618
3619void InstructionCodeGeneratorARM::VisitParameterValue(
3620    HParameterValue* instruction ATTRIBUTE_UNUSED) {
3621  // Nothing to do, the parameter is already at its location.
3622}
3623
3624void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3625  LocationSummary* locations =
3626      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3627  locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3628}
3629
3630void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3631  // Nothing to do, the method is already at its location.
3632}
3633
3634void LocationsBuilderARM::VisitNot(HNot* not_) {
3635  LocationSummary* locations =
3636      new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
3637  locations->SetInAt(0, Location::RequiresRegister());
3638  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3639}
3640
3641void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3642  LocationSummary* locations = not_->GetLocations();
3643  Location out = locations->Out();
3644  Location in = locations->InAt(0);
3645  switch (not_->GetResultType()) {
3646    case Primitive::kPrimInt:
3647      __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
3648      break;
3649
3650    case Primitive::kPrimLong:
3651      __ mvn(out.AsRegisterPairLow<Register>(),
3652             ShifterOperand(in.AsRegisterPairLow<Register>()));
3653      __ mvn(out.AsRegisterPairHigh<Register>(),
3654             ShifterOperand(in.AsRegisterPairHigh<Register>()));
3655      break;
3656
3657    default:
3658      LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3659  }
3660}
3661
3662void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3663  LocationSummary* locations =
3664      new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3665  locations->SetInAt(0, Location::RequiresRegister());
3666  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3667}
3668
3669void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
3670  LocationSummary* locations = bool_not->GetLocations();
3671  Location out = locations->Out();
3672  Location in = locations->InAt(0);
3673  __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3674}
3675
3676void LocationsBuilderARM::VisitCompare(HCompare* compare) {
3677  LocationSummary* locations =
3678      new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3679  switch (compare->InputAt(0)->GetType()) {
3680    case Primitive::kPrimBoolean:
3681    case Primitive::kPrimByte:
3682    case Primitive::kPrimShort:
3683    case Primitive::kPrimChar:
3684    case Primitive::kPrimInt:
3685    case Primitive::kPrimLong: {
3686      locations->SetInAt(0, Location::RequiresRegister());
3687      locations->SetInAt(1, Location::RequiresRegister());
3688      // Output overlaps because it is written before doing the low comparison.
3689      locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3690      break;
3691    }
3692    case Primitive::kPrimFloat:
3693    case Primitive::kPrimDouble: {
3694      locations->SetInAt(0, Location::RequiresFpuRegister());
3695      locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
3696      locations->SetOut(Location::RequiresRegister());
3697      break;
3698    }
3699    default:
3700      LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3701  }
3702}
3703
3704void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
3705  LocationSummary* locations = compare->GetLocations();
3706  Register out = locations->Out().AsRegister<Register>();
3707  Location left = locations->InAt(0);
3708  Location right = locations->InAt(1);
3709
3710  Label less, greater, done;
3711  Primitive::Type type = compare->InputAt(0)->GetType();
3712  Condition less_cond;
3713  switch (type) {
3714    case Primitive::kPrimBoolean:
3715    case Primitive::kPrimByte:
3716    case Primitive::kPrimShort:
3717    case Primitive::kPrimChar:
3718    case Primitive::kPrimInt: {
3719      __ LoadImmediate(out, 0);
3720      __ cmp(left.AsRegister<Register>(),
3721             ShifterOperand(right.AsRegister<Register>()));  // Signed compare.
3722      less_cond = LT;
3723      break;
3724    }
3725    case Primitive::kPrimLong: {
3726      __ cmp(left.AsRegisterPairHigh<Register>(),
3727             ShifterOperand(right.AsRegisterPairHigh<Register>()));  // Signed compare.
3728      __ b(&less, LT);
3729      __ b(&greater, GT);
3730      // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
3731      __ LoadImmediate(out, 0);
3732      __ cmp(left.AsRegisterPairLow<Register>(),
3733             ShifterOperand(right.AsRegisterPairLow<Register>()));  // Unsigned compare.
3734      less_cond = LO;
3735      break;
3736    }
3737    case Primitive::kPrimFloat:
3738    case Primitive::kPrimDouble: {
3739      __ LoadImmediate(out, 0);
3740      GenerateVcmp(compare);
3741      __ vmstat();  // transfer FP status register to ARM APSR.
3742      less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
3743      break;
3744    }
3745    default:
3746      LOG(FATAL) << "Unexpected compare type " << type;
3747      UNREACHABLE();
3748  }
3749
3750  __ b(&done, EQ);
3751  __ b(&less, less_cond);
3752
3753  __ Bind(&greater);
3754  __ LoadImmediate(out, 1);
3755  __ b(&done);
3756
3757  __ Bind(&less);
3758  __ LoadImmediate(out, -1);
3759
3760  __ Bind(&done);
3761}
3762
3763void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
3764  LocationSummary* locations =
3765      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3766  for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
3767    locations->SetInAt(i, Location::Any());
3768  }
3769  locations->SetOut(Location::Any());
3770}
3771
3772void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3773  LOG(FATAL) << "Unreachable";
3774}
3775
3776void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3777  // TODO (ported from quick): revisit ARM barrier kinds.
3778  DmbOptions flavor = DmbOptions::ISH;  // Quiet C++ warnings.
3779  switch (kind) {
3780    case MemBarrierKind::kAnyStore:
3781    case MemBarrierKind::kLoadAny:
3782    case MemBarrierKind::kAnyAny: {
3783      flavor = DmbOptions::ISH;
3784      break;
3785    }
3786    case MemBarrierKind::kStoreStore: {
3787      flavor = DmbOptions::ISHST;
3788      break;
3789    }
3790    default:
3791      LOG(FATAL) << "Unexpected memory barrier " << kind;
3792  }
3793  __ dmb(flavor);
3794}
3795
3796void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3797                                                         uint32_t offset,
3798                                                         Register out_lo,
3799                                                         Register out_hi) {
3800  if (offset != 0) {
3801    // Ensure `out_lo` is different from `addr`, so that loading
3802    // `offset` into `out_lo` does not clutter `addr`.
3803    DCHECK_NE(out_lo, addr);
3804    __ LoadImmediate(out_lo, offset);
3805    __ add(IP, addr, ShifterOperand(out_lo));
3806    addr = IP;
3807  }
3808  __ ldrexd(out_lo, out_hi, addr);
3809}
3810
3811void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3812                                                          uint32_t offset,
3813                                                          Register value_lo,
3814                                                          Register value_hi,
3815                                                          Register temp1,
3816                                                          Register temp2,
3817                                                          HInstruction* instruction) {
3818  Label fail;
3819  if (offset != 0) {
3820    __ LoadImmediate(temp1, offset);
3821    __ add(IP, addr, ShifterOperand(temp1));
3822    addr = IP;
3823  }
3824  __ Bind(&fail);
3825  // We need a load followed by store. (The address used in a STREX instruction must
3826  // be the same as the address in the most recently executed LDREX instruction.)
3827  __ ldrexd(temp1, temp2, addr);
3828  codegen_->MaybeRecordImplicitNullCheck(instruction);
3829  __ strexd(temp1, value_lo, value_hi, addr);
3830  __ CompareAndBranchIfNonZero(temp1, &fail);
3831}
3832
3833void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3834  DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3835
3836  LocationSummary* locations =
3837      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3838  locations->SetInAt(0, Location::RequiresRegister());
3839
3840  Primitive::Type field_type = field_info.GetFieldType();
3841  if (Primitive::IsFloatingPointType(field_type)) {
3842    locations->SetInAt(1, Location::RequiresFpuRegister());
3843  } else {
3844    locations->SetInAt(1, Location::RequiresRegister());
3845  }
3846
3847  bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
3848  bool generate_volatile = field_info.IsVolatile()
3849      && is_wide
3850      && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3851  bool needs_write_barrier =
3852      CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3853  // Temporary registers for the write barrier.
3854  // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
3855  if (needs_write_barrier) {
3856    locations->AddTemp(Location::RequiresRegister());  // Possibly used for reference poisoning too.
3857    locations->AddTemp(Location::RequiresRegister());
3858  } else if (generate_volatile) {
3859    // ARM encoding have some additional constraints for ldrexd/strexd:
3860    // - registers need to be consecutive
3861    // - the first register should be even but not R14.
3862    // We don't test for ARM yet, and the assertion makes sure that we
3863    // revisit this if we ever enable ARM encoding.
3864    DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3865
3866    locations->AddTemp(Location::RequiresRegister());
3867    locations->AddTemp(Location::RequiresRegister());
3868    if (field_type == Primitive::kPrimDouble) {
3869      // For doubles we need two more registers to copy the value.
3870      locations->AddTemp(Location::RegisterLocation(R2));
3871      locations->AddTemp(Location::RegisterLocation(R3));
3872    }
3873  }
3874}
3875
3876void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
3877                                                 const FieldInfo& field_info,
3878                                                 bool value_can_be_null) {
3879  DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3880
3881  LocationSummary* locations = instruction->GetLocations();
3882  Register base = locations->InAt(0).AsRegister<Register>();
3883  Location value = locations->InAt(1);
3884
3885  bool is_volatile = field_info.IsVolatile();
3886  bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
3887  Primitive::Type field_type = field_info.GetFieldType();
3888  uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3889  bool needs_write_barrier =
3890      CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
3891
3892  if (is_volatile) {
3893    codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3894  }
3895
3896  switch (field_type) {
3897    case Primitive::kPrimBoolean:
3898    case Primitive::kPrimByte: {
3899      __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
3900      break;
3901    }
3902
3903    case Primitive::kPrimShort:
3904    case Primitive::kPrimChar: {
3905      __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
3906      break;
3907    }
3908
3909    case Primitive::kPrimInt:
3910    case Primitive::kPrimNot: {
3911      if (kPoisonHeapReferences && needs_write_barrier) {
3912        // Note that in the case where `value` is a null reference,
3913        // we do not enter this block, as a null reference does not
3914        // need poisoning.
3915        DCHECK_EQ(field_type, Primitive::kPrimNot);
3916        Register temp = locations->GetTemp(0).AsRegister<Register>();
3917        __ Mov(temp, value.AsRegister<Register>());
3918        __ PoisonHeapReference(temp);
3919        __ StoreToOffset(kStoreWord, temp, base, offset);
3920      } else {
3921        __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3922      }
3923      break;
3924    }
3925
3926    case Primitive::kPrimLong: {
3927      if (is_volatile && !atomic_ldrd_strd) {
3928        GenerateWideAtomicStore(base, offset,
3929                                value.AsRegisterPairLow<Register>(),
3930                                value.AsRegisterPairHigh<Register>(),
3931                                locations->GetTemp(0).AsRegister<Register>(),
3932                                locations->GetTemp(1).AsRegister<Register>(),
3933                                instruction);
3934      } else {
3935        __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
3936        codegen_->MaybeRecordImplicitNullCheck(instruction);
3937      }
3938      break;
3939    }
3940
3941    case Primitive::kPrimFloat: {
3942      __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
3943      break;
3944    }
3945
3946    case Primitive::kPrimDouble: {
3947      DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
3948      if (is_volatile && !atomic_ldrd_strd) {
3949        Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3950        Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3951
3952        __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3953
3954        GenerateWideAtomicStore(base, offset,
3955                                value_reg_lo,
3956                                value_reg_hi,
3957                                locations->GetTemp(2).AsRegister<Register>(),
3958                                locations->GetTemp(3).AsRegister<Register>(),
3959                                instruction);
3960      } else {
3961        __ StoreDToOffset(value_reg, base, offset);
3962        codegen_->MaybeRecordImplicitNullCheck(instruction);
3963      }
3964      break;
3965    }
3966
3967    case Primitive::kPrimVoid:
3968      LOG(FATAL) << "Unreachable type " << field_type;
3969      UNREACHABLE();
3970  }
3971
3972  // Longs and doubles are handled in the switch.
3973  if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3974    codegen_->MaybeRecordImplicitNullCheck(instruction);
3975  }
3976
3977  if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3978    Register temp = locations->GetTemp(0).AsRegister<Register>();
3979    Register card = locations->GetTemp(1).AsRegister<Register>();
3980    codegen_->MarkGCCard(
3981        temp, card, base, value.AsRegister<Register>(), value_can_be_null);
3982  }
3983
3984  if (is_volatile) {
3985    codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3986  }
3987}
3988
3989void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3990  DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3991
3992  bool object_field_get_with_read_barrier =
3993      kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
3994  LocationSummary* locations =
3995      new (GetGraph()->GetArena()) LocationSummary(instruction,
3996                                                   object_field_get_with_read_barrier ?
3997                                                       LocationSummary::kCallOnSlowPath :
3998                                                       LocationSummary::kNoCall);
3999  if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4000    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
4001  }
4002  locations->SetInAt(0, Location::RequiresRegister());
4003
4004  bool volatile_for_double = field_info.IsVolatile()
4005      && (field_info.GetFieldType() == Primitive::kPrimDouble)
4006      && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4007  // The output overlaps in case of volatile long: we don't want the
4008  // code generated by GenerateWideAtomicLoad to overwrite the
4009  // object's location.  Likewise, in the case of an object field get
4010  // with read barriers enabled, we do not want the load to overwrite
4011  // the object's location, as we need it to emit the read barrier.
4012  bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4013      object_field_get_with_read_barrier;
4014
4015  if (Primitive::IsFloatingPointType(instruction->GetType())) {
4016    locations->SetOut(Location::RequiresFpuRegister());
4017  } else {
4018    locations->SetOut(Location::RequiresRegister(),
4019                      (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4020  }
4021  if (volatile_for_double) {
4022    // ARM encoding have some additional constraints for ldrexd/strexd:
4023    // - registers need to be consecutive
4024    // - the first register should be even but not R14.
4025    // We don't test for ARM yet, and the assertion makes sure that we
4026    // revisit this if we ever enable ARM encoding.
4027    DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4028    locations->AddTemp(Location::RequiresRegister());
4029    locations->AddTemp(Location::RequiresRegister());
4030  } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4031    // We need a temporary register for the read barrier marking slow
4032    // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4033    locations->AddTemp(Location::RequiresRegister());
4034  }
4035}
4036
4037Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4038  DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4039      << input->GetType();
4040  if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4041      (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4042    return Location::ConstantLocation(input->AsConstant());
4043  } else {
4044    return Location::RequiresFpuRegister();
4045  }
4046}
4047
4048Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4049                                                             Opcode opcode) {
4050  DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4051  if (constant->IsConstant() &&
4052      CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4053    return Location::ConstantLocation(constant->AsConstant());
4054  }
4055  return Location::RequiresRegister();
4056}
4057
4058bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4059                                                       Opcode opcode) {
4060  uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4061  if (Primitive::Is64BitType(input_cst->GetType())) {
4062    Opcode high_opcode = opcode;
4063    SetCc low_set_cc = kCcDontCare;
4064    switch (opcode) {
4065      case SUB:
4066        // Flip the operation to an ADD.
4067        value = -value;
4068        opcode = ADD;
4069        FALLTHROUGH_INTENDED;
4070      case ADD:
4071        if (Low32Bits(value) == 0u) {
4072          return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4073        }
4074        high_opcode = ADC;
4075        low_set_cc = kCcSet;
4076        break;
4077      default:
4078        break;
4079    }
4080    return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4081        CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
4082  } else {
4083    return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4084  }
4085}
4086
4087bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4088                                                       Opcode opcode,
4089                                                       SetCc set_cc) {
4090  ShifterOperand so;
4091  ArmAssembler* assembler = codegen_->GetAssembler();
4092  if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
4093    return true;
4094  }
4095  Opcode neg_opcode = kNoOperand;
4096  switch (opcode) {
4097    case AND: neg_opcode = BIC; value = ~value; break;
4098    case ORR: neg_opcode = ORN; value = ~value; break;
4099    case ADD: neg_opcode = SUB; value = -value; break;
4100    case ADC: neg_opcode = SBC; value = ~value; break;
4101    case SUB: neg_opcode = ADD; value = -value; break;
4102    case SBC: neg_opcode = ADC; value = ~value; break;
4103    default:
4104      return false;
4105  }
4106  return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
4107}
4108
4109void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4110                                                 const FieldInfo& field_info) {
4111  DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
4112
4113  LocationSummary* locations = instruction->GetLocations();
4114  Location base_loc = locations->InAt(0);
4115  Register base = base_loc.AsRegister<Register>();
4116  Location out = locations->Out();
4117  bool is_volatile = field_info.IsVolatile();
4118  bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
4119  Primitive::Type field_type = field_info.GetFieldType();
4120  uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4121
4122  switch (field_type) {
4123    case Primitive::kPrimBoolean:
4124      __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
4125      break;
4126
4127    case Primitive::kPrimByte:
4128      __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
4129      break;
4130
4131    case Primitive::kPrimShort:
4132      __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
4133      break;
4134
4135    case Primitive::kPrimChar:
4136      __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
4137      break;
4138
4139    case Primitive::kPrimInt:
4140      __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4141      break;
4142
4143    case Primitive::kPrimNot: {
4144      // /* HeapReference<Object> */ out = *(base + offset)
4145      if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4146        Location temp_loc = locations->GetTemp(0);
4147        // Note that a potential implicit null check is handled in this
4148        // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4149        codegen_->GenerateFieldLoadWithBakerReadBarrier(
4150            instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4151        if (is_volatile) {
4152          codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4153        }
4154      } else {
4155        __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4156        codegen_->MaybeRecordImplicitNullCheck(instruction);
4157        if (is_volatile) {
4158          codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4159        }
4160        // If read barriers are enabled, emit read barriers other than
4161        // Baker's using a slow path (and also unpoison the loaded
4162        // reference, if heap poisoning is enabled).
4163        codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4164      }
4165      break;
4166    }
4167
4168    case Primitive::kPrimLong:
4169      if (is_volatile && !atomic_ldrd_strd) {
4170        GenerateWideAtomicLoad(base, offset,
4171                               out.AsRegisterPairLow<Register>(),
4172                               out.AsRegisterPairHigh<Register>());
4173      } else {
4174        __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4175      }
4176      break;
4177
4178    case Primitive::kPrimFloat:
4179      __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
4180      break;
4181
4182    case Primitive::kPrimDouble: {
4183      DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
4184      if (is_volatile && !atomic_ldrd_strd) {
4185        Register lo = locations->GetTemp(0).AsRegister<Register>();
4186        Register hi = locations->GetTemp(1).AsRegister<Register>();
4187        GenerateWideAtomicLoad(base, offset, lo, hi);
4188        codegen_->MaybeRecordImplicitNullCheck(instruction);
4189        __ vmovdrr(out_reg, lo, hi);
4190      } else {
4191        __ LoadDFromOffset(out_reg, base, offset);
4192        codegen_->MaybeRecordImplicitNullCheck(instruction);
4193      }
4194      break;
4195    }
4196
4197    case Primitive::kPrimVoid:
4198      LOG(FATAL) << "Unreachable type " << field_type;
4199      UNREACHABLE();
4200  }
4201
4202  if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4203    // Potential implicit null checks, in the case of reference or
4204    // double fields, are handled in the previous switch statement.
4205  } else {
4206    codegen_->MaybeRecordImplicitNullCheck(instruction);
4207  }
4208
4209  if (is_volatile) {
4210    if (field_type == Primitive::kPrimNot) {
4211      // Memory barriers, in the case of references, are also handled
4212      // in the previous switch statement.
4213    } else {
4214      codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4215    }
4216  }
4217}
4218
4219void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4220  HandleFieldSet(instruction, instruction->GetFieldInfo());
4221}
4222
4223void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4224  HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4225}
4226
4227void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4228  HandleFieldGet(instruction, instruction->GetFieldInfo());
4229}
4230
4231void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4232  HandleFieldGet(instruction, instruction->GetFieldInfo());
4233}
4234
4235void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4236  HandleFieldGet(instruction, instruction->GetFieldInfo());
4237}
4238
4239void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4240  HandleFieldGet(instruction, instruction->GetFieldInfo());
4241}
4242
4243void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4244  HandleFieldSet(instruction, instruction->GetFieldInfo());
4245}
4246
4247void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4248  HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
4249}
4250
4251void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4252    HUnresolvedInstanceFieldGet* instruction) {
4253  FieldAccessCallingConventionARM calling_convention;
4254  codegen_->CreateUnresolvedFieldLocationSummary(
4255      instruction, instruction->GetFieldType(), calling_convention);
4256}
4257
4258void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4259    HUnresolvedInstanceFieldGet* instruction) {
4260  FieldAccessCallingConventionARM calling_convention;
4261  codegen_->GenerateUnresolvedFieldAccess(instruction,
4262                                          instruction->GetFieldType(),
4263                                          instruction->GetFieldIndex(),
4264                                          instruction->GetDexPc(),
4265                                          calling_convention);
4266}
4267
4268void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4269    HUnresolvedInstanceFieldSet* instruction) {
4270  FieldAccessCallingConventionARM calling_convention;
4271  codegen_->CreateUnresolvedFieldLocationSummary(
4272      instruction, instruction->GetFieldType(), calling_convention);
4273}
4274
4275void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4276    HUnresolvedInstanceFieldSet* instruction) {
4277  FieldAccessCallingConventionARM calling_convention;
4278  codegen_->GenerateUnresolvedFieldAccess(instruction,
4279                                          instruction->GetFieldType(),
4280                                          instruction->GetFieldIndex(),
4281                                          instruction->GetDexPc(),
4282                                          calling_convention);
4283}
4284
4285void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4286    HUnresolvedStaticFieldGet* instruction) {
4287  FieldAccessCallingConventionARM calling_convention;
4288  codegen_->CreateUnresolvedFieldLocationSummary(
4289      instruction, instruction->GetFieldType(), calling_convention);
4290}
4291
4292void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4293    HUnresolvedStaticFieldGet* instruction) {
4294  FieldAccessCallingConventionARM calling_convention;
4295  codegen_->GenerateUnresolvedFieldAccess(instruction,
4296                                          instruction->GetFieldType(),
4297                                          instruction->GetFieldIndex(),
4298                                          instruction->GetDexPc(),
4299                                          calling_convention);
4300}
4301
4302void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4303    HUnresolvedStaticFieldSet* instruction) {
4304  FieldAccessCallingConventionARM calling_convention;
4305  codegen_->CreateUnresolvedFieldLocationSummary(
4306      instruction, instruction->GetFieldType(), calling_convention);
4307}
4308
4309void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4310    HUnresolvedStaticFieldSet* instruction) {
4311  FieldAccessCallingConventionARM calling_convention;
4312  codegen_->GenerateUnresolvedFieldAccess(instruction,
4313                                          instruction->GetFieldType(),
4314                                          instruction->GetFieldIndex(),
4315                                          instruction->GetDexPc(),
4316                                          calling_convention);
4317}
4318
4319void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
4320  LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4321      ? LocationSummary::kCallOnSlowPath
4322      : LocationSummary::kNoCall;
4323  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4324  locations->SetInAt(0, Location::RequiresRegister());
4325  if (instruction->HasUses()) {
4326    locations->SetOut(Location::SameAsFirstInput());
4327  }
4328}
4329
4330void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4331  if (CanMoveNullCheckToUser(instruction)) {
4332    return;
4333  }
4334  Location obj = instruction->GetLocations()->InAt(0);
4335
4336  __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4337  RecordPcInfo(instruction, instruction->GetDexPc());
4338}
4339
4340void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
4341  SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
4342  AddSlowPath(slow_path);
4343
4344  LocationSummary* locations = instruction->GetLocations();
4345  Location obj = locations->InAt(0);
4346
4347  __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4348}
4349
4350void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
4351  codegen_->GenerateNullCheck(instruction);
4352}
4353
4354static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4355  switch (type) {
4356    case Primitive::kPrimNot:
4357      return kLoadWord;
4358    case Primitive::kPrimBoolean:
4359      return kLoadUnsignedByte;
4360    case Primitive::kPrimByte:
4361      return kLoadSignedByte;
4362    case Primitive::kPrimChar:
4363      return kLoadUnsignedHalfword;
4364    case Primitive::kPrimShort:
4365      return kLoadSignedHalfword;
4366    case Primitive::kPrimInt:
4367      return kLoadWord;
4368    case Primitive::kPrimLong:
4369      return kLoadWordPair;
4370    case Primitive::kPrimFloat:
4371      return kLoadSWord;
4372    case Primitive::kPrimDouble:
4373      return kLoadDWord;
4374    default:
4375      LOG(FATAL) << "Unreachable type " << type;
4376      UNREACHABLE();
4377  }
4378}
4379
4380static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4381  switch (type) {
4382    case Primitive::kPrimNot:
4383      return kStoreWord;
4384    case Primitive::kPrimBoolean:
4385    case Primitive::kPrimByte:
4386      return kStoreByte;
4387    case Primitive::kPrimChar:
4388    case Primitive::kPrimShort:
4389      return kStoreHalfword;
4390    case Primitive::kPrimInt:
4391      return kStoreWord;
4392    case Primitive::kPrimLong:
4393      return kStoreWordPair;
4394    case Primitive::kPrimFloat:
4395      return kStoreSWord;
4396    case Primitive::kPrimDouble:
4397      return kStoreDWord;
4398    default:
4399      LOG(FATAL) << "Unreachable type " << type;
4400      UNREACHABLE();
4401  }
4402}
4403
4404void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4405                                                Location out_loc,
4406                                                Register base,
4407                                                Register reg_offset,
4408                                                Condition cond) {
4409  uint32_t shift_count = Primitive::ComponentSizeShift(type);
4410  Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4411
4412  switch (type) {
4413    case Primitive::kPrimByte:
4414      __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4415      break;
4416    case Primitive::kPrimBoolean:
4417      __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4418      break;
4419    case Primitive::kPrimShort:
4420      __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4421      break;
4422    case Primitive::kPrimChar:
4423      __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4424      break;
4425    case Primitive::kPrimNot:
4426    case Primitive::kPrimInt:
4427      __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4428      break;
4429    // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4430    case Primitive::kPrimLong:
4431    case Primitive::kPrimFloat:
4432    case Primitive::kPrimDouble:
4433    default:
4434      LOG(FATAL) << "Unreachable type " << type;
4435      UNREACHABLE();
4436  }
4437}
4438
4439void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4440                                               Location loc,
4441                                               Register base,
4442                                               Register reg_offset,
4443                                               Condition cond) {
4444  uint32_t shift_count = Primitive::ComponentSizeShift(type);
4445  Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4446
4447  switch (type) {
4448    case Primitive::kPrimByte:
4449    case Primitive::kPrimBoolean:
4450      __ strb(loc.AsRegister<Register>(), mem_address, cond);
4451      break;
4452    case Primitive::kPrimShort:
4453    case Primitive::kPrimChar:
4454      __ strh(loc.AsRegister<Register>(), mem_address, cond);
4455      break;
4456    case Primitive::kPrimNot:
4457    case Primitive::kPrimInt:
4458      __ str(loc.AsRegister<Register>(), mem_address, cond);
4459      break;
4460    // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4461    case Primitive::kPrimLong:
4462    case Primitive::kPrimFloat:
4463    case Primitive::kPrimDouble:
4464    default:
4465      LOG(FATAL) << "Unreachable type " << type;
4466      UNREACHABLE();
4467  }
4468}
4469
4470void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
4471  bool object_array_get_with_read_barrier =
4472      kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
4473  LocationSummary* locations =
4474      new (GetGraph()->GetArena()) LocationSummary(instruction,
4475                                                   object_array_get_with_read_barrier ?
4476                                                       LocationSummary::kCallOnSlowPath :
4477                                                       LocationSummary::kNoCall);
4478  if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4479    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
4480  }
4481  locations->SetInAt(0, Location::RequiresRegister());
4482  locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4483  if (Primitive::IsFloatingPointType(instruction->GetType())) {
4484    locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4485  } else {
4486    // The output overlaps in the case of an object array get with
4487    // read barriers enabled: we do not want the move to overwrite the
4488    // array's location, as we need it to emit the read barrier.
4489    locations->SetOut(
4490        Location::RequiresRegister(),
4491        object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
4492  }
4493  // We need a temporary register for the read barrier marking slow
4494  // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4495  if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4496    locations->AddTemp(Location::RequiresRegister());
4497  }
4498}
4499
4500void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4501  LocationSummary* locations = instruction->GetLocations();
4502  Location obj_loc = locations->InAt(0);
4503  Register obj = obj_loc.AsRegister<Register>();
4504  Location index = locations->InAt(1);
4505  Location out_loc = locations->Out();
4506  uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
4507  Primitive::Type type = instruction->GetType();
4508  HInstruction* array_instr = instruction->GetArray();
4509  bool has_intermediate_address = array_instr->IsIntermediateAddress();
4510  // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4511  DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4512
4513  switch (type) {
4514    case Primitive::kPrimBoolean:
4515    case Primitive::kPrimByte:
4516    case Primitive::kPrimShort:
4517    case Primitive::kPrimChar:
4518    case Primitive::kPrimInt: {
4519      if (index.IsConstant()) {
4520        int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4521        uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4522
4523        LoadOperandType load_type = GetLoadOperandType(type);
4524        __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4525      } else {
4526        Register temp = IP;
4527
4528        if (has_intermediate_address) {
4529          // We do not need to compute the intermediate address from the array: the
4530          // input instruction has done it already. See the comment in
4531          // `TryExtractArrayAccessAddress()`.
4532          if (kIsDebugBuild) {
4533            HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4534            DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4535          }
4536          temp = obj;
4537        } else {
4538          __ add(temp, obj, ShifterOperand(data_offset));
4539        }
4540        codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4541      }
4542      break;
4543    }
4544
4545    case Primitive::kPrimNot: {
4546      static_assert(
4547          sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4548          "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4549      // /* HeapReference<Object> */ out =
4550      //     *(obj + data_offset + index * sizeof(HeapReference<Object>))
4551      if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4552        Location temp = locations->GetTemp(0);
4553        // Note that a potential implicit null check is handled in this
4554        // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4555        codegen_->GenerateArrayLoadWithBakerReadBarrier(
4556            instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4557      } else {
4558        Register out = out_loc.AsRegister<Register>();
4559        if (index.IsConstant()) {
4560          size_t offset =
4561              (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4562          __ LoadFromOffset(kLoadWord, out, obj, offset);
4563          codegen_->MaybeRecordImplicitNullCheck(instruction);
4564          // If read barriers are enabled, emit read barriers other than
4565          // Baker's using a slow path (and also unpoison the loaded
4566          // reference, if heap poisoning is enabled).
4567          codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4568        } else {
4569          Register temp = IP;
4570
4571          if (has_intermediate_address) {
4572            // We do not need to compute the intermediate address from the array: the
4573            // input instruction has done it already. See the comment in
4574            // `TryExtractArrayAccessAddress()`.
4575            if (kIsDebugBuild) {
4576              HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4577              DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4578            }
4579            temp = obj;
4580          } else {
4581            __ add(temp, obj, ShifterOperand(data_offset));
4582          }
4583          codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4584
4585          codegen_->MaybeRecordImplicitNullCheck(instruction);
4586          // If read barriers are enabled, emit read barriers other than
4587          // Baker's using a slow path (and also unpoison the loaded
4588          // reference, if heap poisoning is enabled).
4589          codegen_->MaybeGenerateReadBarrierSlow(
4590              instruction, out_loc, out_loc, obj_loc, data_offset, index);
4591        }
4592      }
4593      break;
4594    }
4595
4596    case Primitive::kPrimLong: {
4597      if (index.IsConstant()) {
4598        size_t offset =
4599            (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4600        __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
4601      } else {
4602        __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4603        __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
4604      }
4605      break;
4606    }
4607
4608    case Primitive::kPrimFloat: {
4609      SRegister out = out_loc.AsFpuRegister<SRegister>();
4610      if (index.IsConstant()) {
4611        size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4612        __ LoadSFromOffset(out, obj, offset);
4613      } else {
4614        __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4615        __ LoadSFromOffset(out, IP, data_offset);
4616      }
4617      break;
4618    }
4619
4620    case Primitive::kPrimDouble: {
4621      SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
4622      if (index.IsConstant()) {
4623        size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4624        __ LoadDFromOffset(FromLowSToD(out), obj, offset);
4625      } else {
4626        __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4627        __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
4628      }
4629      break;
4630    }
4631
4632    case Primitive::kPrimVoid:
4633      LOG(FATAL) << "Unreachable type " << type;
4634      UNREACHABLE();
4635  }
4636
4637  if (type == Primitive::kPrimNot) {
4638    // Potential implicit null checks, in the case of reference
4639    // arrays, are handled in the previous switch statement.
4640  } else {
4641    codegen_->MaybeRecordImplicitNullCheck(instruction);
4642  }
4643}
4644
4645void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
4646  Primitive::Type value_type = instruction->GetComponentType();
4647
4648  bool needs_write_barrier =
4649      CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4650  bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4651
4652  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4653      instruction,
4654      may_need_runtime_call_for_type_check ?
4655          LocationSummary::kCallOnSlowPath :
4656          LocationSummary::kNoCall);
4657
4658  locations->SetInAt(0, Location::RequiresRegister());
4659  locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4660  if (Primitive::IsFloatingPointType(value_type)) {
4661    locations->SetInAt(2, Location::RequiresFpuRegister());
4662  } else {
4663    locations->SetInAt(2, Location::RequiresRegister());
4664  }
4665  if (needs_write_barrier) {
4666    // Temporary registers for the write barrier.
4667    locations->AddTemp(Location::RequiresRegister());  // Possibly used for ref. poisoning too.
4668    locations->AddTemp(Location::RequiresRegister());
4669  }
4670}
4671
4672void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4673  LocationSummary* locations = instruction->GetLocations();
4674  Location array_loc = locations->InAt(0);
4675  Register array = array_loc.AsRegister<Register>();
4676  Location index = locations->InAt(1);
4677  Primitive::Type value_type = instruction->GetComponentType();
4678  bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4679  bool needs_write_barrier =
4680      CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4681  uint32_t data_offset =
4682      mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4683  Location value_loc = locations->InAt(2);
4684  HInstruction* array_instr = instruction->GetArray();
4685  bool has_intermediate_address = array_instr->IsIntermediateAddress();
4686  // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4687  DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4688
4689  switch (value_type) {
4690    case Primitive::kPrimBoolean:
4691    case Primitive::kPrimByte:
4692    case Primitive::kPrimShort:
4693    case Primitive::kPrimChar:
4694    case Primitive::kPrimInt: {
4695      if (index.IsConstant()) {
4696        int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4697        uint32_t full_offset =
4698            data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4699        StoreOperandType store_type = GetStoreOperandType(value_type);
4700        __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
4701      } else {
4702        Register temp = IP;
4703
4704        if (has_intermediate_address) {
4705          // We do not need to compute the intermediate address from the array: the
4706          // input instruction has done it already. See the comment in
4707          // `TryExtractArrayAccessAddress()`.
4708          if (kIsDebugBuild) {
4709            HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4710            DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4711          }
4712          temp = array;
4713        } else {
4714          __ add(temp, array, ShifterOperand(data_offset));
4715        }
4716        codegen_->StoreToShiftedRegOffset(value_type,
4717                                          value_loc,
4718                                          temp,
4719                                          index.AsRegister<Register>());
4720      }
4721      break;
4722    }
4723
4724    case Primitive::kPrimNot: {
4725      Register value = value_loc.AsRegister<Register>();
4726      // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4727      // See the comment in instruction_simplifier_shared.cc.
4728      DCHECK(!has_intermediate_address);
4729
4730      if (instruction->InputAt(2)->IsNullConstant()) {
4731        // Just setting null.
4732        if (index.IsConstant()) {
4733          size_t offset =
4734              (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4735          __ StoreToOffset(kStoreWord, value, array, offset);
4736        } else {
4737          DCHECK(index.IsRegister()) << index;
4738          __ add(IP, array, ShifterOperand(data_offset));
4739          codegen_->StoreToShiftedRegOffset(value_type,
4740                                            value_loc,
4741                                            IP,
4742                                            index.AsRegister<Register>());
4743        }
4744        codegen_->MaybeRecordImplicitNullCheck(instruction);
4745        DCHECK(!needs_write_barrier);
4746        DCHECK(!may_need_runtime_call_for_type_check);
4747        break;
4748      }
4749
4750      DCHECK(needs_write_barrier);
4751      Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4752      Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4753      uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4754      uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4755      uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4756      Label done;
4757      SlowPathCode* slow_path = nullptr;
4758
4759      if (may_need_runtime_call_for_type_check) {
4760        slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4761        codegen_->AddSlowPath(slow_path);
4762        if (instruction->GetValueCanBeNull()) {
4763          Label non_zero;
4764          __ CompareAndBranchIfNonZero(value, &non_zero);
4765          if (index.IsConstant()) {
4766            size_t offset =
4767               (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4768            __ StoreToOffset(kStoreWord, value, array, offset);
4769          } else {
4770            DCHECK(index.IsRegister()) << index;
4771            __ add(IP, array, ShifterOperand(data_offset));
4772            codegen_->StoreToShiftedRegOffset(value_type,
4773                                              value_loc,
4774                                              IP,
4775                                              index.AsRegister<Register>());
4776          }
4777          codegen_->MaybeRecordImplicitNullCheck(instruction);
4778          __ b(&done);
4779          __ Bind(&non_zero);
4780        }
4781
4782        if (kEmitCompilerReadBarrier) {
4783          // When read barriers are enabled, the type checking
4784          // instrumentation requires two read barriers:
4785          //
4786          //   __ Mov(temp2, temp1);
4787          //   // /* HeapReference<Class> */ temp1 = temp1->component_type_
4788          //   __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4789          //   codegen_->GenerateReadBarrierSlow(
4790          //       instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4791          //
4792          //   // /* HeapReference<Class> */ temp2 = value->klass_
4793          //   __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4794          //   codegen_->GenerateReadBarrierSlow(
4795          //       instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4796          //
4797          //   __ cmp(temp1, ShifterOperand(temp2));
4798          //
4799          // However, the second read barrier may trash `temp`, as it
4800          // is a temporary register, and as such would not be saved
4801          // along with live registers before calling the runtime (nor
4802          // restored afterwards).  So in this case, we bail out and
4803          // delegate the work to the array set slow path.
4804          //
4805          // TODO: Extend the register allocator to support a new
4806          // "(locally) live temp" location so as to avoid always
4807          // going into the slow path when read barriers are enabled.
4808          __ b(slow_path->GetEntryLabel());
4809        } else {
4810          // /* HeapReference<Class> */ temp1 = array->klass_
4811          __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4812          codegen_->MaybeRecordImplicitNullCheck(instruction);
4813          __ MaybeUnpoisonHeapReference(temp1);
4814
4815          // /* HeapReference<Class> */ temp1 = temp1->component_type_
4816          __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4817          // /* HeapReference<Class> */ temp2 = value->klass_
4818          __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4819          // If heap poisoning is enabled, no need to unpoison `temp1`
4820          // nor `temp2`, as we are comparing two poisoned references.
4821          __ cmp(temp1, ShifterOperand(temp2));
4822
4823          if (instruction->StaticTypeOfArrayIsObjectArray()) {
4824            Label do_put;
4825            __ b(&do_put, EQ);
4826            // If heap poisoning is enabled, the `temp1` reference has
4827            // not been unpoisoned yet; unpoison it now.
4828            __ MaybeUnpoisonHeapReference(temp1);
4829
4830            // /* HeapReference<Class> */ temp1 = temp1->super_class_
4831            __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4832            // If heap poisoning is enabled, no need to unpoison
4833            // `temp1`, as we are comparing against null below.
4834            __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4835            __ Bind(&do_put);
4836          } else {
4837            __ b(slow_path->GetEntryLabel(), NE);
4838          }
4839        }
4840      }
4841
4842      Register source = value;
4843      if (kPoisonHeapReferences) {
4844        // Note that in the case where `value` is a null reference,
4845        // we do not enter this block, as a null reference does not
4846        // need poisoning.
4847        DCHECK_EQ(value_type, Primitive::kPrimNot);
4848        __ Mov(temp1, value);
4849        __ PoisonHeapReference(temp1);
4850        source = temp1;
4851      }
4852
4853      if (index.IsConstant()) {
4854        size_t offset =
4855            (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4856        __ StoreToOffset(kStoreWord, source, array, offset);
4857      } else {
4858        DCHECK(index.IsRegister()) << index;
4859
4860        __ add(IP, array, ShifterOperand(data_offset));
4861        codegen_->StoreToShiftedRegOffset(value_type,
4862                                          Location::RegisterLocation(source),
4863                                          IP,
4864                                          index.AsRegister<Register>());
4865      }
4866
4867      if (!may_need_runtime_call_for_type_check) {
4868        codegen_->MaybeRecordImplicitNullCheck(instruction);
4869      }
4870
4871      codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4872
4873      if (done.IsLinked()) {
4874        __ Bind(&done);
4875      }
4876
4877      if (slow_path != nullptr) {
4878        __ Bind(slow_path->GetExitLabel());
4879      }
4880
4881      break;
4882    }
4883
4884    case Primitive::kPrimLong: {
4885      Location value = locations->InAt(2);
4886      if (index.IsConstant()) {
4887        size_t offset =
4888            (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4889        __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
4890      } else {
4891        __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4892        __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
4893      }
4894      break;
4895    }
4896
4897    case Primitive::kPrimFloat: {
4898      Location value = locations->InAt(2);
4899      DCHECK(value.IsFpuRegister());
4900      if (index.IsConstant()) {
4901        size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4902        __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
4903      } else {
4904        __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4905        __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4906      }
4907      break;
4908    }
4909
4910    case Primitive::kPrimDouble: {
4911      Location value = locations->InAt(2);
4912      DCHECK(value.IsFpuRegisterPair());
4913      if (index.IsConstant()) {
4914        size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4915        __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
4916      } else {
4917        __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4918        __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4919      }
4920
4921      break;
4922    }
4923
4924    case Primitive::kPrimVoid:
4925      LOG(FATAL) << "Unreachable type " << value_type;
4926      UNREACHABLE();
4927  }
4928
4929  // Objects are handled in the switch.
4930  if (value_type != Primitive::kPrimNot) {
4931    codegen_->MaybeRecordImplicitNullCheck(instruction);
4932  }
4933}
4934
4935void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
4936  LocationSummary* locations =
4937      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4938  locations->SetInAt(0, Location::RequiresRegister());
4939  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4940}
4941
4942void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4943  LocationSummary* locations = instruction->GetLocations();
4944  uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
4945  Register obj = locations->InAt(0).AsRegister<Register>();
4946  Register out = locations->Out().AsRegister<Register>();
4947  __ LoadFromOffset(kLoadWord, out, obj, offset);
4948  codegen_->MaybeRecordImplicitNullCheck(instruction);
4949}
4950
4951void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4952  // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4953  DCHECK(!kEmitCompilerReadBarrier);
4954  LocationSummary* locations =
4955      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4956
4957  locations->SetInAt(0, Location::RequiresRegister());
4958  locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4959  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4960}
4961
4962void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4963  LocationSummary* locations = instruction->GetLocations();
4964  Location out = locations->Out();
4965  Location first = locations->InAt(0);
4966  Location second = locations->InAt(1);
4967
4968  // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4969  DCHECK(!kEmitCompilerReadBarrier);
4970
4971  if (second.IsRegister()) {
4972    __ add(out.AsRegister<Register>(),
4973           first.AsRegister<Register>(),
4974           ShifterOperand(second.AsRegister<Register>()));
4975  } else {
4976    __ AddConstant(out.AsRegister<Register>(),
4977                   first.AsRegister<Register>(),
4978                   second.GetConstant()->AsIntConstant()->GetValue());
4979  }
4980}
4981
4982void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4983  LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4984      ? LocationSummary::kCallOnSlowPath
4985      : LocationSummary::kNoCall;
4986  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4987  locations->SetInAt(0, Location::RequiresRegister());
4988  locations->SetInAt(1, Location::RequiresRegister());
4989  if (instruction->HasUses()) {
4990    locations->SetOut(Location::SameAsFirstInput());
4991  }
4992}
4993
4994void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4995  LocationSummary* locations = instruction->GetLocations();
4996  SlowPathCode* slow_path =
4997      new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
4998  codegen_->AddSlowPath(slow_path);
4999
5000  Register index = locations->InAt(0).AsRegister<Register>();
5001  Register length = locations->InAt(1).AsRegister<Register>();
5002
5003  __ cmp(index, ShifterOperand(length));
5004  __ b(slow_path->GetEntryLabel(), HS);
5005}
5006
5007void CodeGeneratorARM::MarkGCCard(Register temp,
5008                                  Register card,
5009                                  Register object,
5010                                  Register value,
5011                                  bool can_be_null) {
5012  Label is_null;
5013  if (can_be_null) {
5014    __ CompareAndBranchIfZero(value, &is_null);
5015  }
5016  __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
5017  __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5018  __ strb(card, Address(card, temp));
5019  if (can_be_null) {
5020    __ Bind(&is_null);
5021  }
5022}
5023
5024void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5025  LOG(FATAL) << "Unreachable";
5026}
5027
5028void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
5029  codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5030}
5031
5032void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
5033  LocationSummary* locations =
5034      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5035  locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
5036}
5037
5038void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
5039  HBasicBlock* block = instruction->GetBlock();
5040  if (block->GetLoopInformation() != nullptr) {
5041    DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5042    // The back edge will generate the suspend check.
5043    return;
5044  }
5045  if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5046    // The goto will generate the suspend check.
5047    return;
5048  }
5049  GenerateSuspendCheck(instruction, nullptr);
5050}
5051
5052void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5053                                                       HBasicBlock* successor) {
5054  SuspendCheckSlowPathARM* slow_path =
5055      down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5056  if (slow_path == nullptr) {
5057    slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5058    instruction->SetSlowPath(slow_path);
5059    codegen_->AddSlowPath(slow_path);
5060    if (successor != nullptr) {
5061      DCHECK(successor->IsLoopHeader());
5062      codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5063    }
5064  } else {
5065    DCHECK_EQ(slow_path->GetSuccessor(), successor);
5066  }
5067
5068  __ LoadFromOffset(
5069      kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
5070  if (successor == nullptr) {
5071    __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
5072    __ Bind(slow_path->GetReturnLabel());
5073  } else {
5074    __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
5075    __ b(slow_path->GetEntryLabel());
5076  }
5077}
5078
5079ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5080  return codegen_->GetAssembler();
5081}
5082
5083void ParallelMoveResolverARM::EmitMove(size_t index) {
5084  MoveOperands* move = moves_[index];
5085  Location source = move->GetSource();
5086  Location destination = move->GetDestination();
5087
5088  if (source.IsRegister()) {
5089    if (destination.IsRegister()) {
5090      __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
5091    } else if (destination.IsFpuRegister()) {
5092      __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
5093    } else {
5094      DCHECK(destination.IsStackSlot());
5095      __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
5096                       SP, destination.GetStackIndex());
5097    }
5098  } else if (source.IsStackSlot()) {
5099    if (destination.IsRegister()) {
5100      __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
5101                        SP, source.GetStackIndex());
5102    } else if (destination.IsFpuRegister()) {
5103      __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
5104    } else {
5105      DCHECK(destination.IsStackSlot());
5106      __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5107      __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5108    }
5109  } else if (source.IsFpuRegister()) {
5110    if (destination.IsRegister()) {
5111      __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5112    } else if (destination.IsFpuRegister()) {
5113      __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
5114    } else {
5115      DCHECK(destination.IsStackSlot());
5116      __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5117    }
5118  } else if (source.IsDoubleStackSlot()) {
5119    if (destination.IsDoubleStackSlot()) {
5120      __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5121      __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
5122    } else if (destination.IsRegisterPair()) {
5123      DCHECK(ExpectedPairLayout(destination));
5124      __ LoadFromOffset(
5125          kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5126    } else {
5127      DCHECK(destination.IsFpuRegisterPair()) << destination;
5128      __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5129                         SP,
5130                         source.GetStackIndex());
5131    }
5132  } else if (source.IsRegisterPair()) {
5133    if (destination.IsRegisterPair()) {
5134      __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5135      __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5136    } else if (destination.IsFpuRegisterPair()) {
5137      __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5138                 source.AsRegisterPairLow<Register>(),
5139                 source.AsRegisterPairHigh<Register>());
5140    } else {
5141      DCHECK(destination.IsDoubleStackSlot()) << destination;
5142      DCHECK(ExpectedPairLayout(source));
5143      __ StoreToOffset(
5144          kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5145    }
5146  } else if (source.IsFpuRegisterPair()) {
5147    if (destination.IsRegisterPair()) {
5148      __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5149                 destination.AsRegisterPairHigh<Register>(),
5150                 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5151    } else if (destination.IsFpuRegisterPair()) {
5152      __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5153               FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5154    } else {
5155      DCHECK(destination.IsDoubleStackSlot()) << destination;
5156      __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5157                        SP,
5158                        destination.GetStackIndex());
5159    }
5160  } else {
5161    DCHECK(source.IsConstant()) << source;
5162    HConstant* constant = source.GetConstant();
5163    if (constant->IsIntConstant() || constant->IsNullConstant()) {
5164      int32_t value = CodeGenerator::GetInt32ValueOf(constant);
5165      if (destination.IsRegister()) {
5166        __ LoadImmediate(destination.AsRegister<Register>(), value);
5167      } else {
5168        DCHECK(destination.IsStackSlot());
5169        __ LoadImmediate(IP, value);
5170        __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5171      }
5172    } else if (constant->IsLongConstant()) {
5173      int64_t value = constant->AsLongConstant()->GetValue();
5174      if (destination.IsRegisterPair()) {
5175        __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5176        __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
5177      } else {
5178        DCHECK(destination.IsDoubleStackSlot()) << destination;
5179        __ LoadImmediate(IP, Low32Bits(value));
5180        __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5181        __ LoadImmediate(IP, High32Bits(value));
5182        __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5183      }
5184    } else if (constant->IsDoubleConstant()) {
5185      double value = constant->AsDoubleConstant()->GetValue();
5186      if (destination.IsFpuRegisterPair()) {
5187        __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
5188      } else {
5189        DCHECK(destination.IsDoubleStackSlot()) << destination;
5190        uint64_t int_value = bit_cast<uint64_t, double>(value);
5191        __ LoadImmediate(IP, Low32Bits(int_value));
5192        __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5193        __ LoadImmediate(IP, High32Bits(int_value));
5194        __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5195      }
5196    } else {
5197      DCHECK(constant->IsFloatConstant()) << constant->DebugName();
5198      float value = constant->AsFloatConstant()->GetValue();
5199      if (destination.IsFpuRegister()) {
5200        __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5201      } else {
5202        DCHECK(destination.IsStackSlot());
5203        __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5204        __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5205      }
5206    }
5207  }
5208}
5209
5210void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5211  __ Mov(IP, reg);
5212  __ LoadFromOffset(kLoadWord, reg, SP, mem);
5213  __ StoreToOffset(kStoreWord, IP, SP, mem);
5214}
5215
5216void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5217  ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5218  int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5219  __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5220                    SP, mem1 + stack_offset);
5221  __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5222  __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5223                   SP, mem2 + stack_offset);
5224  __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5225}
5226
5227void ParallelMoveResolverARM::EmitSwap(size_t index) {
5228  MoveOperands* move = moves_[index];
5229  Location source = move->GetSource();
5230  Location destination = move->GetDestination();
5231
5232  if (source.IsRegister() && destination.IsRegister()) {
5233    DCHECK_NE(source.AsRegister<Register>(), IP);
5234    DCHECK_NE(destination.AsRegister<Register>(), IP);
5235    __ Mov(IP, source.AsRegister<Register>());
5236    __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5237    __ Mov(destination.AsRegister<Register>(), IP);
5238  } else if (source.IsRegister() && destination.IsStackSlot()) {
5239    Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
5240  } else if (source.IsStackSlot() && destination.IsRegister()) {
5241    Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
5242  } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5243    Exchange(source.GetStackIndex(), destination.GetStackIndex());
5244  } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5245    __ vmovrs(IP, source.AsFpuRegister<SRegister>());
5246    __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
5247    __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
5248  } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
5249    __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
5250    __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
5251    __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
5252    __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5253               destination.AsRegisterPairHigh<Register>(),
5254               DTMP);
5255  } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
5256    Register low_reg = source.IsRegisterPair()
5257        ? source.AsRegisterPairLow<Register>()
5258        : destination.AsRegisterPairLow<Register>();
5259    int mem = source.IsRegisterPair()
5260        ? destination.GetStackIndex()
5261        : source.GetStackIndex();
5262    DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
5263    __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
5264    __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
5265    __ StoreDToOffset(DTMP, SP, mem);
5266  } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
5267    DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5268    DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5269    __ vmovd(DTMP, first);
5270    __ vmovd(first, second);
5271    __ vmovd(second, DTMP);
5272  } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5273    DRegister reg = source.IsFpuRegisterPair()
5274        ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5275        : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5276    int mem = source.IsFpuRegisterPair()
5277        ? destination.GetStackIndex()
5278        : source.GetStackIndex();
5279    __ vmovd(DTMP, reg);
5280    __ LoadDFromOffset(reg, SP, mem);
5281    __ StoreDToOffset(DTMP, SP, mem);
5282  } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5283    SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5284                                           : destination.AsFpuRegister<SRegister>();
5285    int mem = source.IsFpuRegister()
5286        ? destination.GetStackIndex()
5287        : source.GetStackIndex();
5288
5289    __ vmovrs(IP, reg);
5290    __ LoadSFromOffset(reg, SP, mem);
5291    __ StoreToOffset(kStoreWord, IP, SP, mem);
5292  } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5293    Exchange(source.GetStackIndex(), destination.GetStackIndex());
5294    Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
5295  } else {
5296    LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
5297  }
5298}
5299
5300void ParallelMoveResolverARM::SpillScratch(int reg) {
5301  __ Push(static_cast<Register>(reg));
5302}
5303
5304void ParallelMoveResolverARM::RestoreScratch(int reg) {
5305  __ Pop(static_cast<Register>(reg));
5306}
5307
5308HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5309    HLoadClass::LoadKind desired_class_load_kind) {
5310  if (kEmitCompilerReadBarrier) {
5311    switch (desired_class_load_kind) {
5312      case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5313      case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5314      case HLoadClass::LoadKind::kBootImageAddress:
5315        // TODO: Implement for read barrier.
5316        return HLoadClass::LoadKind::kDexCacheViaMethod;
5317      default:
5318        break;
5319    }
5320  }
5321  switch (desired_class_load_kind) {
5322    case HLoadClass::LoadKind::kReferrersClass:
5323      break;
5324    case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5325      DCHECK(!GetCompilerOptions().GetCompilePic());
5326      break;
5327    case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5328      DCHECK(GetCompilerOptions().GetCompilePic());
5329      break;
5330    case HLoadClass::LoadKind::kBootImageAddress:
5331      break;
5332    case HLoadClass::LoadKind::kDexCacheAddress:
5333      DCHECK(Runtime::Current()->UseJitCompilation());
5334      break;
5335    case HLoadClass::LoadKind::kDexCachePcRelative:
5336      DCHECK(!Runtime::Current()->UseJitCompilation());
5337      // We disable pc-relative load when there is an irreducible loop, as the optimization
5338      // is incompatible with it.
5339      // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5340      // with irreducible loops.
5341      if (GetGraph()->HasIrreducibleLoops()) {
5342        return HLoadClass::LoadKind::kDexCacheViaMethod;
5343      }
5344      break;
5345    case HLoadClass::LoadKind::kDexCacheViaMethod:
5346      break;
5347  }
5348  return desired_class_load_kind;
5349}
5350
5351void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
5352  if (cls->NeedsAccessCheck()) {
5353    InvokeRuntimeCallingConvention calling_convention;
5354    CodeGenerator::CreateLoadClassLocationSummary(
5355        cls,
5356        Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5357        Location::RegisterLocation(R0),
5358        /* code_generator_supports_read_barrier */ true);
5359    return;
5360  }
5361
5362  LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5363      ? LocationSummary::kCallOnSlowPath
5364      : LocationSummary::kNoCall;
5365  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5366  if (kUseBakerReadBarrier && !cls->NeedsEnvironment()) {
5367    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
5368  }
5369
5370  HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5371  if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5372      load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5373      load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5374    locations->SetInAt(0, Location::RequiresRegister());
5375  }
5376  locations->SetOut(Location::RequiresRegister());
5377}
5378
5379void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
5380  LocationSummary* locations = cls->GetLocations();
5381  if (cls->NeedsAccessCheck()) {
5382    codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5383    codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5384                            cls,
5385                            cls->GetDexPc(),
5386                            nullptr);
5387    CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
5388    return;
5389  }
5390
5391  Location out_loc = locations->Out();
5392  Register out = out_loc.AsRegister<Register>();
5393
5394  bool generate_null_check = false;
5395  switch (cls->GetLoadKind()) {
5396    case HLoadClass::LoadKind::kReferrersClass: {
5397      DCHECK(!cls->CanCallRuntime());
5398      DCHECK(!cls->MustGenerateClinitCheck());
5399      // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5400      Register current_method = locations->InAt(0).AsRegister<Register>();
5401      GenerateGcRootFieldLoad(
5402          cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5403      break;
5404    }
5405    case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5406      DCHECK(!kEmitCompilerReadBarrier);
5407      __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5408                                                                    cls->GetTypeIndex()));
5409      break;
5410    }
5411    case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5412      DCHECK(!kEmitCompilerReadBarrier);
5413      CodeGeneratorARM::PcRelativePatchInfo* labels =
5414          codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5415      __ BindTrackedLabel(&labels->movw_label);
5416      __ movw(out, /* placeholder */ 0u);
5417      __ BindTrackedLabel(&labels->movt_label);
5418      __ movt(out, /* placeholder */ 0u);
5419      __ BindTrackedLabel(&labels->add_pc_label);
5420      __ add(out, out, ShifterOperand(PC));
5421      break;
5422    }
5423    case HLoadClass::LoadKind::kBootImageAddress: {
5424      DCHECK(!kEmitCompilerReadBarrier);
5425      DCHECK_NE(cls->GetAddress(), 0u);
5426      uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5427      __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5428      break;
5429    }
5430    case HLoadClass::LoadKind::kDexCacheAddress: {
5431      DCHECK_NE(cls->GetAddress(), 0u);
5432      uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5433      // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5434      // a 128B range. To try and reduce the number of literals if we load multiple types,
5435      // simply split the dex cache address to a 128B aligned base loaded from a literal
5436      // and the remaining offset embedded in the load.
5437      static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5438      DCHECK_ALIGNED(cls->GetAddress(), 4u);
5439      constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5440      uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5441      uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5442      __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5443      // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5444      GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5445      generate_null_check = !cls->IsInDexCache();
5446      break;
5447    }
5448    case HLoadClass::LoadKind::kDexCachePcRelative: {
5449      Register base_reg = locations->InAt(0).AsRegister<Register>();
5450      HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5451      int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5452      // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5453      GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5454      generate_null_check = !cls->IsInDexCache();
5455      break;
5456    }
5457    case HLoadClass::LoadKind::kDexCacheViaMethod: {
5458      // /* GcRoot<mirror::Class>[] */ out =
5459      //        current_method.ptr_sized_fields_->dex_cache_resolved_types_
5460      Register current_method = locations->InAt(0).AsRegister<Register>();
5461      __ LoadFromOffset(kLoadWord,
5462                        out,
5463                        current_method,
5464                        ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5465      // /* GcRoot<mirror::Class> */ out = out[type_index]
5466      size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5467      GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5468      generate_null_check = !cls->IsInDexCache();
5469    }
5470  }
5471
5472  if (generate_null_check || cls->MustGenerateClinitCheck()) {
5473    DCHECK(cls->CanCallRuntime());
5474    SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5475        cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5476    codegen_->AddSlowPath(slow_path);
5477    if (generate_null_check) {
5478      __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5479    }
5480    if (cls->MustGenerateClinitCheck()) {
5481      GenerateClassInitializationCheck(slow_path, out);
5482    } else {
5483      __ Bind(slow_path->GetExitLabel());
5484    }
5485  }
5486}
5487
5488void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5489  LocationSummary* locations =
5490      new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5491  locations->SetInAt(0, Location::RequiresRegister());
5492  if (check->HasUses()) {
5493    locations->SetOut(Location::SameAsFirstInput());
5494  }
5495}
5496
5497void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
5498  // We assume the class is not null.
5499  SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5500      check->GetLoadClass(), check, check->GetDexPc(), true);
5501  codegen_->AddSlowPath(slow_path);
5502  GenerateClassInitializationCheck(slow_path,
5503                                   check->GetLocations()->InAt(0).AsRegister<Register>());
5504}
5505
5506void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
5507    SlowPathCode* slow_path, Register class_reg) {
5508  __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5509  __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5510  __ b(slow_path->GetEntryLabel(), LT);
5511  // Even if the initialized flag is set, we may be in a situation where caches are not synced
5512  // properly. Therefore, we do a memory fence.
5513  __ dmb(ISH);
5514  __ Bind(slow_path->GetExitLabel());
5515}
5516
5517HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5518    HLoadString::LoadKind desired_string_load_kind) {
5519  if (kEmitCompilerReadBarrier) {
5520    switch (desired_string_load_kind) {
5521      case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5522      case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5523      case HLoadString::LoadKind::kBootImageAddress:
5524        // TODO: Implement for read barrier.
5525        return HLoadString::LoadKind::kDexCacheViaMethod;
5526      default:
5527        break;
5528    }
5529  }
5530  switch (desired_string_load_kind) {
5531    case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5532      DCHECK(!GetCompilerOptions().GetCompilePic());
5533      break;
5534    case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5535      DCHECK(GetCompilerOptions().GetCompilePic());
5536      break;
5537    case HLoadString::LoadKind::kBootImageAddress:
5538      break;
5539    case HLoadString::LoadKind::kDexCacheAddress:
5540      DCHECK(Runtime::Current()->UseJitCompilation());
5541      break;
5542    case HLoadString::LoadKind::kDexCachePcRelative:
5543      DCHECK(!Runtime::Current()->UseJitCompilation());
5544      // We disable pc-relative load when there is an irreducible loop, as the optimization
5545      // is incompatible with it.
5546      // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5547      // with irreducible loops.
5548      if (GetGraph()->HasIrreducibleLoops()) {
5549        return HLoadString::LoadKind::kDexCacheViaMethod;
5550      }
5551      break;
5552    case HLoadString::LoadKind::kDexCacheViaMethod:
5553      break;
5554  }
5555  return desired_string_load_kind;
5556}
5557
5558void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
5559  LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
5560      ? LocationSummary::kCallOnSlowPath
5561      : LocationSummary::kNoCall;
5562  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
5563  if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
5564    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
5565  }
5566
5567  HLoadString::LoadKind load_kind = load->GetLoadKind();
5568  if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5569      load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5570    locations->SetInAt(0, Location::RequiresRegister());
5571  }
5572  locations->SetOut(Location::RequiresRegister());
5573}
5574
5575void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
5576  LocationSummary* locations = load->GetLocations();
5577  Location out_loc = locations->Out();
5578  Register out = out_loc.AsRegister<Register>();
5579
5580  switch (load->GetLoadKind()) {
5581    case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5582      DCHECK(!kEmitCompilerReadBarrier);
5583      __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5584                                                                      load->GetStringIndex()));
5585      return;  // No dex cache slow path.
5586    }
5587    case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5588      DCHECK(!kEmitCompilerReadBarrier);
5589      CodeGeneratorARM::PcRelativePatchInfo* labels =
5590          codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5591      __ BindTrackedLabel(&labels->movw_label);
5592      __ movw(out, /* placeholder */ 0u);
5593      __ BindTrackedLabel(&labels->movt_label);
5594      __ movt(out, /* placeholder */ 0u);
5595      __ BindTrackedLabel(&labels->add_pc_label);
5596      __ add(out, out, ShifterOperand(PC));
5597      return;  // No dex cache slow path.
5598    }
5599    case HLoadString::LoadKind::kBootImageAddress: {
5600      DCHECK(!kEmitCompilerReadBarrier);
5601      DCHECK_NE(load->GetAddress(), 0u);
5602      uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5603      __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5604      return;  // No dex cache slow path.
5605    }
5606    default:
5607      break;
5608  }
5609
5610  // TODO: Re-add the compiler code to do string dex cache lookup again.
5611  SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5612  codegen_->AddSlowPath(slow_path);
5613  __ b(slow_path->GetEntryLabel());
5614  __ Bind(slow_path->GetExitLabel());
5615}
5616
5617static int32_t GetExceptionTlsOffset() {
5618  return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
5619}
5620
5621void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5622  LocationSummary* locations =
5623      new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5624  locations->SetOut(Location::RequiresRegister());
5625}
5626
5627void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
5628  Register out = load->GetLocations()->Out().AsRegister<Register>();
5629  __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5630}
5631
5632void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5633  new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5634}
5635
5636void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5637  __ LoadImmediate(IP, 0);
5638  __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
5639}
5640
5641void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5642  LocationSummary* locations =
5643      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
5644  InvokeRuntimeCallingConvention calling_convention;
5645  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5646}
5647
5648void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5649  codegen_->InvokeRuntime(
5650      QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
5651  CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5652}
5653
5654static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5655  return kEmitCompilerReadBarrier &&
5656      (kUseBakerReadBarrier ||
5657       type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5658       type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5659       type_check_kind == TypeCheckKind::kArrayObjectCheck);
5660}
5661
5662void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
5663  LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5664  TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5665  bool baker_read_barrier_slow_path = false;
5666  switch (type_check_kind) {
5667    case TypeCheckKind::kExactCheck:
5668    case TypeCheckKind::kAbstractClassCheck:
5669    case TypeCheckKind::kClassHierarchyCheck:
5670    case TypeCheckKind::kArrayObjectCheck:
5671      call_kind =
5672          kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5673      baker_read_barrier_slow_path = kUseBakerReadBarrier;
5674      break;
5675    case TypeCheckKind::kArrayCheck:
5676    case TypeCheckKind::kUnresolvedCheck:
5677    case TypeCheckKind::kInterfaceCheck:
5678      call_kind = LocationSummary::kCallOnSlowPath;
5679      break;
5680  }
5681
5682  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5683  if (baker_read_barrier_slow_path) {
5684    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
5685  }
5686  locations->SetInAt(0, Location::RequiresRegister());
5687  locations->SetInAt(1, Location::RequiresRegister());
5688  // The "out" register is used as a temporary, so it overlaps with the inputs.
5689  // Note that TypeCheckSlowPathARM uses this register too.
5690  locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5691  // When read barriers are enabled, we need a temporary register for
5692  // some cases.
5693  if (TypeCheckNeedsATemporary(type_check_kind)) {
5694    locations->AddTemp(Location::RequiresRegister());
5695  }
5696}
5697
5698void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
5699  TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5700  LocationSummary* locations = instruction->GetLocations();
5701  Location obj_loc = locations->InAt(0);
5702  Register obj = obj_loc.AsRegister<Register>();
5703  Register cls = locations->InAt(1).AsRegister<Register>();
5704  Location out_loc = locations->Out();
5705  Register out = out_loc.AsRegister<Register>();
5706  Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5707      locations->GetTemp(0) :
5708      Location::NoLocation();
5709  uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5710  uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5711  uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5712  uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5713  Label done, zero;
5714  SlowPathCode* slow_path = nullptr;
5715
5716  // Return 0 if `obj` is null.
5717  // avoid null check if we know obj is not null.
5718  if (instruction->MustDoNullCheck()) {
5719    __ CompareAndBranchIfZero(obj, &zero);
5720  }
5721
5722  // /* HeapReference<Class> */ out = obj->klass_
5723  GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
5724
5725  switch (type_check_kind) {
5726    case TypeCheckKind::kExactCheck: {
5727      __ cmp(out, ShifterOperand(cls));
5728      // Classes must be equal for the instanceof to succeed.
5729      __ b(&zero, NE);
5730      __ LoadImmediate(out, 1);
5731      __ b(&done);
5732      break;
5733    }
5734
5735    case TypeCheckKind::kAbstractClassCheck: {
5736      // If the class is abstract, we eagerly fetch the super class of the
5737      // object to avoid doing a comparison we know will fail.
5738      Label loop;
5739      __ Bind(&loop);
5740      // /* HeapReference<Class> */ out = out->super_class_
5741      GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
5742      // If `out` is null, we use it for the result, and jump to `done`.
5743      __ CompareAndBranchIfZero(out, &done);
5744      __ cmp(out, ShifterOperand(cls));
5745      __ b(&loop, NE);
5746      __ LoadImmediate(out, 1);
5747      if (zero.IsLinked()) {
5748        __ b(&done);
5749      }
5750      break;
5751    }
5752
5753    case TypeCheckKind::kClassHierarchyCheck: {
5754      // Walk over the class hierarchy to find a match.
5755      Label loop, success;
5756      __ Bind(&loop);
5757      __ cmp(out, ShifterOperand(cls));
5758      __ b(&success, EQ);
5759      // /* HeapReference<Class> */ out = out->super_class_
5760      GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
5761      __ CompareAndBranchIfNonZero(out, &loop);
5762      // If `out` is null, we use it for the result, and jump to `done`.
5763      __ b(&done);
5764      __ Bind(&success);
5765      __ LoadImmediate(out, 1);
5766      if (zero.IsLinked()) {
5767        __ b(&done);
5768      }
5769      break;
5770    }
5771
5772    case TypeCheckKind::kArrayObjectCheck: {
5773      // Do an exact check.
5774      Label exact_check;
5775      __ cmp(out, ShifterOperand(cls));
5776      __ b(&exact_check, EQ);
5777      // Otherwise, we need to check that the object's class is a non-primitive array.
5778      // /* HeapReference<Class> */ out = out->component_type_
5779      GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
5780      // If `out` is null, we use it for the result, and jump to `done`.
5781      __ CompareAndBranchIfZero(out, &done);
5782      __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5783      static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5784      __ CompareAndBranchIfNonZero(out, &zero);
5785      __ Bind(&exact_check);
5786      __ LoadImmediate(out, 1);
5787      __ b(&done);
5788      break;
5789    }
5790
5791    case TypeCheckKind::kArrayCheck: {
5792      __ cmp(out, ShifterOperand(cls));
5793      DCHECK(locations->OnlyCallsOnSlowPath());
5794      slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5795                                                                    /* is_fatal */ false);
5796      codegen_->AddSlowPath(slow_path);
5797      __ b(slow_path->GetEntryLabel(), NE);
5798      __ LoadImmediate(out, 1);
5799      if (zero.IsLinked()) {
5800        __ b(&done);
5801      }
5802      break;
5803    }
5804
5805    case TypeCheckKind::kUnresolvedCheck:
5806    case TypeCheckKind::kInterfaceCheck: {
5807      // Note that we indeed only call on slow path, but we always go
5808      // into the slow path for the unresolved and interface check
5809      // cases.
5810      //
5811      // We cannot directly call the InstanceofNonTrivial runtime
5812      // entry point without resorting to a type checking slow path
5813      // here (i.e. by calling InvokeRuntime directly), as it would
5814      // require to assign fixed registers for the inputs of this
5815      // HInstanceOf instruction (following the runtime calling
5816      // convention), which might be cluttered by the potential first
5817      // read barrier emission at the beginning of this method.
5818      //
5819      // TODO: Introduce a new runtime entry point taking the object
5820      // to test (instead of its class) as argument, and let it deal
5821      // with the read barrier issues. This will let us refactor this
5822      // case of the `switch` code as it was previously (with a direct
5823      // call to the runtime not using a type checking slow path).
5824      // This should also be beneficial for the other cases above.
5825      DCHECK(locations->OnlyCallsOnSlowPath());
5826      slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5827                                                                    /* is_fatal */ false);
5828      codegen_->AddSlowPath(slow_path);
5829      __ b(slow_path->GetEntryLabel());
5830      if (zero.IsLinked()) {
5831        __ b(&done);
5832      }
5833      break;
5834    }
5835  }
5836
5837  if (zero.IsLinked()) {
5838    __ Bind(&zero);
5839    __ LoadImmediate(out, 0);
5840  }
5841
5842  if (done.IsLinked()) {
5843    __ Bind(&done);
5844  }
5845
5846  if (slow_path != nullptr) {
5847    __ Bind(slow_path->GetExitLabel());
5848  }
5849}
5850
5851void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
5852  LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5853  bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5854
5855  TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5856  bool baker_read_barrier_slow_path = false;
5857  switch (type_check_kind) {
5858    case TypeCheckKind::kExactCheck:
5859    case TypeCheckKind::kAbstractClassCheck:
5860    case TypeCheckKind::kClassHierarchyCheck:
5861    case TypeCheckKind::kArrayObjectCheck:
5862      call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5863          LocationSummary::kCallOnSlowPath :
5864          LocationSummary::kNoCall;  // In fact, call on a fatal (non-returning) slow path.
5865      baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
5866      break;
5867    case TypeCheckKind::kArrayCheck:
5868    case TypeCheckKind::kUnresolvedCheck:
5869    case TypeCheckKind::kInterfaceCheck:
5870      call_kind = LocationSummary::kCallOnSlowPath;
5871      break;
5872  }
5873
5874  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5875  if (baker_read_barrier_slow_path) {
5876    locations->SetCustomSlowPathCallerSaves(RegisterSet());  // No caller-save registers.
5877  }
5878  locations->SetInAt(0, Location::RequiresRegister());
5879  locations->SetInAt(1, Location::RequiresRegister());
5880  // Note that TypeCheckSlowPathARM uses this "temp" register too.
5881  locations->AddTemp(Location::RequiresRegister());
5882  // When read barriers are enabled, we need an additional temporary
5883  // register for some cases.
5884  if (TypeCheckNeedsATemporary(type_check_kind)) {
5885    locations->AddTemp(Location::RequiresRegister());
5886  }
5887}
5888
5889void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
5890  TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5891  LocationSummary* locations = instruction->GetLocations();
5892  Location obj_loc = locations->InAt(0);
5893  Register obj = obj_loc.AsRegister<Register>();
5894  Register cls = locations->InAt(1).AsRegister<Register>();
5895  Location temp_loc = locations->GetTemp(0);
5896  Register temp = temp_loc.AsRegister<Register>();
5897  Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5898      locations->GetTemp(1) :
5899      Location::NoLocation();
5900  uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5901  uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5902  uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5903  uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5904
5905  bool is_type_check_slow_path_fatal =
5906      (type_check_kind == TypeCheckKind::kExactCheck ||
5907       type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5908       type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5909       type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5910      !instruction->CanThrowIntoCatchBlock();
5911  SlowPathCode* type_check_slow_path =
5912      new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5913                                                        is_type_check_slow_path_fatal);
5914  codegen_->AddSlowPath(type_check_slow_path);
5915
5916  Label done;
5917  // Avoid null check if we know obj is not null.
5918  if (instruction->MustDoNullCheck()) {
5919    __ CompareAndBranchIfZero(obj, &done);
5920  }
5921
5922  // /* HeapReference<Class> */ temp = obj->klass_
5923  GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5924
5925  switch (type_check_kind) {
5926    case TypeCheckKind::kExactCheck:
5927    case TypeCheckKind::kArrayCheck: {
5928      __ cmp(temp, ShifterOperand(cls));
5929      // Jump to slow path for throwing the exception or doing a
5930      // more involved array check.
5931      __ b(type_check_slow_path->GetEntryLabel(), NE);
5932      break;
5933    }
5934
5935    case TypeCheckKind::kAbstractClassCheck: {
5936      // If the class is abstract, we eagerly fetch the super class of the
5937      // object to avoid doing a comparison we know will fail.
5938      Label loop, compare_classes;
5939      __ Bind(&loop);
5940      // /* HeapReference<Class> */ temp = temp->super_class_
5941      GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
5942
5943      // If the class reference currently in `temp` is not null, jump
5944      // to the `compare_classes` label to compare it with the checked
5945      // class.
5946      __ CompareAndBranchIfNonZero(temp, &compare_classes);
5947      // Otherwise, jump to the slow path to throw the exception.
5948      //
5949      // But before, move back the object's class into `temp` before
5950      // going into the slow path, as it has been overwritten in the
5951      // meantime.
5952      // /* HeapReference<Class> */ temp = obj->klass_
5953      GenerateReferenceLoadTwoRegisters(
5954          instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5955      __ b(type_check_slow_path->GetEntryLabel());
5956
5957      __ Bind(&compare_classes);
5958      __ cmp(temp, ShifterOperand(cls));
5959      __ b(&loop, NE);
5960      break;
5961    }
5962
5963    case TypeCheckKind::kClassHierarchyCheck: {
5964      // Walk over the class hierarchy to find a match.
5965      Label loop;
5966      __ Bind(&loop);
5967      __ cmp(temp, ShifterOperand(cls));
5968      __ b(&done, EQ);
5969
5970      // /* HeapReference<Class> */ temp = temp->super_class_
5971      GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
5972
5973      // If the class reference currently in `temp` is not null, jump
5974      // back at the beginning of the loop.
5975      __ CompareAndBranchIfNonZero(temp, &loop);
5976      // Otherwise, jump to the slow path to throw the exception.
5977      //
5978      // But before, move back the object's class into `temp` before
5979      // going into the slow path, as it has been overwritten in the
5980      // meantime.
5981      // /* HeapReference<Class> */ temp = obj->klass_
5982      GenerateReferenceLoadTwoRegisters(
5983          instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
5984      __ b(type_check_slow_path->GetEntryLabel());
5985      break;
5986    }
5987
5988    case TypeCheckKind::kArrayObjectCheck: {
5989      // Do an exact check.
5990      Label check_non_primitive_component_type;
5991      __ cmp(temp, ShifterOperand(cls));
5992      __ b(&done, EQ);
5993
5994      // Otherwise, we need to check that the object's class is a non-primitive array.
5995      // /* HeapReference<Class> */ temp = temp->component_type_
5996      GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
5997
5998      // If the component type is not null (i.e. the object is indeed
5999      // an array), jump to label `check_non_primitive_component_type`
6000      // to further check that this component type is not a primitive
6001      // type.
6002      __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6003      // Otherwise, jump to the slow path to throw the exception.
6004      //
6005      // But before, move back the object's class into `temp` before
6006      // going into the slow path, as it has been overwritten in the
6007      // meantime.
6008      // /* HeapReference<Class> */ temp = obj->klass_
6009      GenerateReferenceLoadTwoRegisters(
6010          instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6011      __ b(type_check_slow_path->GetEntryLabel());
6012
6013      __ Bind(&check_non_primitive_component_type);
6014      __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
6015      static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6016      __ CompareAndBranchIfZero(temp, &done);
6017      // Same comment as above regarding `temp` and the slow path.
6018      // /* HeapReference<Class> */ temp = obj->klass_
6019      GenerateReferenceLoadTwoRegisters(
6020          instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
6021      __ b(type_check_slow_path->GetEntryLabel());
6022      break;
6023    }
6024
6025    case TypeCheckKind::kUnresolvedCheck:
6026    case TypeCheckKind::kInterfaceCheck:
6027      // We always go into the type check slow path for the unresolved
6028      // and interface check cases.
6029      //
6030      // We cannot directly call the CheckCast runtime entry point
6031      // without resorting to a type checking slow path here (i.e. by
6032      // calling InvokeRuntime directly), as it would require to
6033      // assign fixed registers for the inputs of this HInstanceOf
6034      // instruction (following the runtime calling convention), which
6035      // might be cluttered by the potential first read barrier
6036      // emission at the beginning of this method.
6037      //
6038      // TODO: Introduce a new runtime entry point taking the object
6039      // to test (instead of its class) as argument, and let it deal
6040      // with the read barrier issues. This will let us refactor this
6041      // case of the `switch` code as it was previously (with a direct
6042      // call to the runtime not using a type checking slow path).
6043      // This should also be beneficial for the other cases above.
6044      __ b(type_check_slow_path->GetEntryLabel());
6045      break;
6046  }
6047  __ Bind(&done);
6048
6049  __ Bind(type_check_slow_path->GetExitLabel());
6050}
6051
6052void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6053  LocationSummary* locations =
6054      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
6055  InvokeRuntimeCallingConvention calling_convention;
6056  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6057}
6058
6059void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6060  codegen_->InvokeRuntime(instruction->IsEnter()
6061        ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6062      instruction,
6063      instruction->GetDexPc(),
6064      nullptr);
6065  if (instruction->IsEnter()) {
6066    CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6067  } else {
6068    CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6069  }
6070}
6071
6072void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6073void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6074void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
6075
6076void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
6077  LocationSummary* locations =
6078      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6079  DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6080         || instruction->GetResultType() == Primitive::kPrimLong);
6081  // Note: GVN reorders commutative operations to have the constant on the right hand side.
6082  locations->SetInAt(0, Location::RequiresRegister());
6083  locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
6084  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6085}
6086
6087void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6088  HandleBitwiseOperation(instruction);
6089}
6090
6091void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6092  HandleBitwiseOperation(instruction);
6093}
6094
6095void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6096  HandleBitwiseOperation(instruction);
6097}
6098
6099
6100void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6101  LocationSummary* locations =
6102      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6103  DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6104         || instruction->GetResultType() == Primitive::kPrimLong);
6105
6106  locations->SetInAt(0, Location::RequiresRegister());
6107  locations->SetInAt(1, Location::RequiresRegister());
6108  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6109}
6110
6111void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6112  LocationSummary* locations = instruction->GetLocations();
6113  Location first = locations->InAt(0);
6114  Location second = locations->InAt(1);
6115  Location out = locations->Out();
6116
6117  if (instruction->GetResultType() == Primitive::kPrimInt) {
6118    Register first_reg = first.AsRegister<Register>();
6119    ShifterOperand second_reg(second.AsRegister<Register>());
6120    Register out_reg = out.AsRegister<Register>();
6121
6122    switch (instruction->GetOpKind()) {
6123      case HInstruction::kAnd:
6124        __ bic(out_reg, first_reg, second_reg);
6125        break;
6126      case HInstruction::kOr:
6127        __ orn(out_reg, first_reg, second_reg);
6128        break;
6129      // There is no EON on arm.
6130      case HInstruction::kXor:
6131      default:
6132        LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6133        UNREACHABLE();
6134    }
6135    return;
6136
6137  } else {
6138    DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6139    Register first_low = first.AsRegisterPairLow<Register>();
6140    Register first_high = first.AsRegisterPairHigh<Register>();
6141    ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6142    ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6143    Register out_low = out.AsRegisterPairLow<Register>();
6144    Register out_high = out.AsRegisterPairHigh<Register>();
6145
6146    switch (instruction->GetOpKind()) {
6147      case HInstruction::kAnd:
6148        __ bic(out_low, first_low, second_low);
6149        __ bic(out_high, first_high, second_high);
6150        break;
6151      case HInstruction::kOr:
6152        __ orn(out_low, first_low, second_low);
6153        __ orn(out_high, first_high, second_high);
6154        break;
6155      // There is no EON on arm.
6156      case HInstruction::kXor:
6157      default:
6158        LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6159        UNREACHABLE();
6160    }
6161  }
6162}
6163
6164void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6165  // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6166  if (value == 0xffffffffu) {
6167    if (out != first) {
6168      __ mov(out, ShifterOperand(first));
6169    }
6170    return;
6171  }
6172  if (value == 0u) {
6173    __ mov(out, ShifterOperand(0));
6174    return;
6175  }
6176  ShifterOperand so;
6177  if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6178    __ and_(out, first, so);
6179  } else {
6180    DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6181    __ bic(out, first, ShifterOperand(~value));
6182  }
6183}
6184
6185void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6186  // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6187  if (value == 0u) {
6188    if (out != first) {
6189      __ mov(out, ShifterOperand(first));
6190    }
6191    return;
6192  }
6193  if (value == 0xffffffffu) {
6194    __ mvn(out, ShifterOperand(0));
6195    return;
6196  }
6197  ShifterOperand so;
6198  if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6199    __ orr(out, first, so);
6200  } else {
6201    DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6202    __ orn(out, first, ShifterOperand(~value));
6203  }
6204}
6205
6206void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6207  // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6208  if (value == 0u) {
6209    if (out != first) {
6210      __ mov(out, ShifterOperand(first));
6211    }
6212    return;
6213  }
6214  __ eor(out, first, ShifterOperand(value));
6215}
6216
6217void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6218                                                       Location first,
6219                                                       uint64_t value) {
6220  Register out_low = out.AsRegisterPairLow<Register>();
6221  Register out_high = out.AsRegisterPairHigh<Register>();
6222  Register first_low = first.AsRegisterPairLow<Register>();
6223  Register first_high = first.AsRegisterPairHigh<Register>();
6224  uint32_t value_low = Low32Bits(value);
6225  uint32_t value_high = High32Bits(value);
6226  if (value_low == 0u) {
6227    if (out_low != first_low) {
6228      __ mov(out_low, ShifterOperand(first_low));
6229    }
6230    __ AddConstant(out_high, first_high, value_high);
6231    return;
6232  }
6233  __ AddConstantSetFlags(out_low, first_low, value_low);
6234  ShifterOperand so;
6235  if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6236    __ adc(out_high, first_high, so);
6237  } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6238    __ sbc(out_high, first_high, so);
6239  } else {
6240    LOG(FATAL) << "Unexpected constant " << value_high;
6241    UNREACHABLE();
6242  }
6243}
6244
6245void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6246  LocationSummary* locations = instruction->GetLocations();
6247  Location first = locations->InAt(0);
6248  Location second = locations->InAt(1);
6249  Location out = locations->Out();
6250
6251  if (second.IsConstant()) {
6252    uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6253    uint32_t value_low = Low32Bits(value);
6254    if (instruction->GetResultType() == Primitive::kPrimInt) {
6255      Register first_reg = first.AsRegister<Register>();
6256      Register out_reg = out.AsRegister<Register>();
6257      if (instruction->IsAnd()) {
6258        GenerateAndConst(out_reg, first_reg, value_low);
6259      } else if (instruction->IsOr()) {
6260        GenerateOrrConst(out_reg, first_reg, value_low);
6261      } else {
6262        DCHECK(instruction->IsXor());
6263        GenerateEorConst(out_reg, first_reg, value_low);
6264      }
6265    } else {
6266      DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6267      uint32_t value_high = High32Bits(value);
6268      Register first_low = first.AsRegisterPairLow<Register>();
6269      Register first_high = first.AsRegisterPairHigh<Register>();
6270      Register out_low = out.AsRegisterPairLow<Register>();
6271      Register out_high = out.AsRegisterPairHigh<Register>();
6272      if (instruction->IsAnd()) {
6273        GenerateAndConst(out_low, first_low, value_low);
6274        GenerateAndConst(out_high, first_high, value_high);
6275      } else if (instruction->IsOr()) {
6276        GenerateOrrConst(out_low, first_low, value_low);
6277        GenerateOrrConst(out_high, first_high, value_high);
6278      } else {
6279        DCHECK(instruction->IsXor());
6280        GenerateEorConst(out_low, first_low, value_low);
6281        GenerateEorConst(out_high, first_high, value_high);
6282      }
6283    }
6284    return;
6285  }
6286
6287  if (instruction->GetResultType() == Primitive::kPrimInt) {
6288    Register first_reg = first.AsRegister<Register>();
6289    ShifterOperand second_reg(second.AsRegister<Register>());
6290    Register out_reg = out.AsRegister<Register>();
6291    if (instruction->IsAnd()) {
6292      __ and_(out_reg, first_reg, second_reg);
6293    } else if (instruction->IsOr()) {
6294      __ orr(out_reg, first_reg, second_reg);
6295    } else {
6296      DCHECK(instruction->IsXor());
6297      __ eor(out_reg, first_reg, second_reg);
6298    }
6299  } else {
6300    DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6301    Register first_low = first.AsRegisterPairLow<Register>();
6302    Register first_high = first.AsRegisterPairHigh<Register>();
6303    ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6304    ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6305    Register out_low = out.AsRegisterPairLow<Register>();
6306    Register out_high = out.AsRegisterPairHigh<Register>();
6307    if (instruction->IsAnd()) {
6308      __ and_(out_low, first_low, second_low);
6309      __ and_(out_high, first_high, second_high);
6310    } else if (instruction->IsOr()) {
6311      __ orr(out_low, first_low, second_low);
6312      __ orr(out_high, first_high, second_high);
6313    } else {
6314      DCHECK(instruction->IsXor());
6315      __ eor(out_low, first_low, second_low);
6316      __ eor(out_high, first_high, second_high);
6317    }
6318  }
6319}
6320
6321void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6322                                                                   Location out,
6323                                                                   uint32_t offset,
6324                                                                   Location maybe_temp) {
6325  Register out_reg = out.AsRegister<Register>();
6326  if (kEmitCompilerReadBarrier) {
6327    DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6328    if (kUseBakerReadBarrier) {
6329      // Load with fast path based Baker's read barrier.
6330      // /* HeapReference<Object> */ out = *(out + offset)
6331      codegen_->GenerateFieldLoadWithBakerReadBarrier(
6332          instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
6333    } else {
6334      // Load with slow path based read barrier.
6335      // Save the value of `out` into `maybe_temp` before overwriting it
6336      // in the following move operation, as we will need it for the
6337      // read barrier below.
6338      __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
6339      // /* HeapReference<Object> */ out = *(out + offset)
6340      __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6341      codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6342    }
6343  } else {
6344    // Plain load with no read barrier.
6345    // /* HeapReference<Object> */ out = *(out + offset)
6346    __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6347    __ MaybeUnpoisonHeapReference(out_reg);
6348  }
6349}
6350
6351void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6352                                                                    Location out,
6353                                                                    Location obj,
6354                                                                    uint32_t offset,
6355                                                                    Location maybe_temp) {
6356  Register out_reg = out.AsRegister<Register>();
6357  Register obj_reg = obj.AsRegister<Register>();
6358  if (kEmitCompilerReadBarrier) {
6359    if (kUseBakerReadBarrier) {
6360      DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6361      // Load with fast path based Baker's read barrier.
6362      // /* HeapReference<Object> */ out = *(obj + offset)
6363      codegen_->GenerateFieldLoadWithBakerReadBarrier(
6364          instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
6365    } else {
6366      // Load with slow path based read barrier.
6367      // /* HeapReference<Object> */ out = *(obj + offset)
6368      __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6369      codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6370    }
6371  } else {
6372    // Plain load with no read barrier.
6373    // /* HeapReference<Object> */ out = *(obj + offset)
6374    __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6375    __ MaybeUnpoisonHeapReference(out_reg);
6376  }
6377}
6378
6379void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6380                                                          Location root,
6381                                                          Register obj,
6382                                                          uint32_t offset) {
6383  Register root_reg = root.AsRegister<Register>();
6384  if (kEmitCompilerReadBarrier) {
6385    if (kUseBakerReadBarrier) {
6386      // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6387      // Baker's read barrier are used:
6388      //
6389      //   root = obj.field;
6390      //   if (Thread::Current()->GetIsGcMarking()) {
6391      //     root = ReadBarrier::Mark(root)
6392      //   }
6393
6394      // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6395      __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6396      static_assert(
6397          sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6398          "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6399          "have different sizes.");
6400      static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6401                    "art::mirror::CompressedReference<mirror::Object> and int32_t "
6402                    "have different sizes.");
6403
6404      // Slow path marking the GC root `root`.
6405      SlowPathCode* slow_path =
6406          new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
6407      codegen_->AddSlowPath(slow_path);
6408
6409      // IP = Thread::Current()->GetIsGcMarking()
6410      __ LoadFromOffset(
6411          kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
6412      __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6413      __ Bind(slow_path->GetExitLabel());
6414    } else {
6415      // GC root loaded through a slow path for read barriers other
6416      // than Baker's.
6417      // /* GcRoot<mirror::Object>* */ root = obj + offset
6418      __ AddConstant(root_reg, obj, offset);
6419      // /* mirror::Object* */ root = root->Read()
6420      codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6421    }
6422  } else {
6423    // Plain GC root load with no read barrier.
6424    // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6425    __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6426    // Note that GC roots are not affected by heap poisoning, thus we
6427    // do not have to unpoison `root_reg` here.
6428  }
6429}
6430
6431void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6432                                                             Location ref,
6433                                                             Register obj,
6434                                                             uint32_t offset,
6435                                                             Location temp,
6436                                                             bool needs_null_check) {
6437  DCHECK(kEmitCompilerReadBarrier);
6438  DCHECK(kUseBakerReadBarrier);
6439
6440  // /* HeapReference<Object> */ ref = *(obj + offset)
6441  Location no_index = Location::NoLocation();
6442  ScaleFactor no_scale_factor = TIMES_1;
6443  GenerateReferenceLoadWithBakerReadBarrier(
6444      instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
6445}
6446
6447void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6448                                                             Location ref,
6449                                                             Register obj,
6450                                                             uint32_t data_offset,
6451                                                             Location index,
6452                                                             Location temp,
6453                                                             bool needs_null_check) {
6454  DCHECK(kEmitCompilerReadBarrier);
6455  DCHECK(kUseBakerReadBarrier);
6456
6457  static_assert(
6458      sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6459      "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6460  // /* HeapReference<Object> */ ref =
6461  //     *(obj + data_offset + index * sizeof(HeapReference<Object>))
6462  ScaleFactor scale_factor = TIMES_4;
6463  GenerateReferenceLoadWithBakerReadBarrier(
6464      instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
6465}
6466
6467void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6468                                                                 Location ref,
6469                                                                 Register obj,
6470                                                                 uint32_t offset,
6471                                                                 Location index,
6472                                                                 ScaleFactor scale_factor,
6473                                                                 Location temp,
6474                                                                 bool needs_null_check) {
6475  DCHECK(kEmitCompilerReadBarrier);
6476  DCHECK(kUseBakerReadBarrier);
6477
6478  // In slow path based read barriers, the read barrier call is
6479  // inserted after the original load. However, in fast path based
6480  // Baker's read barriers, we need to perform the load of
6481  // mirror::Object::monitor_ *before* the original reference load.
6482  // This load-load ordering is required by the read barrier.
6483  // The fast path/slow path (for Baker's algorithm) should look like:
6484  //
6485  //   uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6486  //   lfence;  // Load fence or artificial data dependency to prevent load-load reordering
6487  //   HeapReference<Object> ref = *src;  // Original reference load.
6488  //   bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6489  //   if (is_gray) {
6490  //     ref = ReadBarrier::Mark(ref);  // Performed by runtime entrypoint slow path.
6491  //   }
6492  //
6493  // Note: the original implementation in ReadBarrier::Barrier is
6494  // slightly more complex as it performs additional checks that we do
6495  // not do here for performance reasons.
6496
6497  Register ref_reg = ref.AsRegister<Register>();
6498  Register temp_reg = temp.AsRegister<Register>();
6499  uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6500
6501  // /* int32_t */ monitor = obj->monitor_
6502  __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6503  if (needs_null_check) {
6504    MaybeRecordImplicitNullCheck(instruction);
6505  }
6506  // /* LockWord */ lock_word = LockWord(monitor)
6507  static_assert(sizeof(LockWord) == sizeof(int32_t),
6508                "art::LockWord and int32_t have different sizes.");
6509
6510  // Introduce a dependency on the lock_word including the rb_state,
6511  // which shall prevent load-load reordering without using
6512  // a memory barrier (which would be more expensive).
6513  // `obj` is unchanged by this operation, but its value now depends
6514  // on `temp_reg`.
6515  __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
6516
6517  // The actual reference load.
6518  if (index.IsValid()) {
6519    // Load types involving an "index": ArrayGet and
6520    // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6521    // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6522    if (index.IsConstant()) {
6523      size_t computed_offset =
6524          (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6525      __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6526    } else {
6527      // Handle the special case of the
6528      // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6529      // a register pair as index ("long offset"), of which only the low
6530      // part contains data.
6531      Register index_reg = index.IsRegisterPair()
6532          ? index.AsRegisterPairLow<Register>()
6533          : index.AsRegister<Register>();
6534      __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
6535      __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6536    }
6537  } else {
6538    // /* HeapReference<Object> */ ref = *(obj + offset)
6539    __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6540  }
6541
6542  // Object* ref = ref_addr->AsMirrorPtr()
6543  __ MaybeUnpoisonHeapReference(ref_reg);
6544
6545  // Slow path marking the object `ref` when it is gray.
6546  SlowPathCode* slow_path =
6547      new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
6548  AddSlowPath(slow_path);
6549
6550  // if (rb_state == ReadBarrier::gray_ptr_)
6551  //   ref = ReadBarrier::Mark(ref);
6552  // Given the numeric representation, it's enough to check the low bit of the
6553  // rb_state. We do that by shifting the bit out of the lock word with LSRS
6554  // which can be a 16-bit instruction unlike the TST immediate.
6555  static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6556  static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6557  static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6558  __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6559  __ b(slow_path->GetEntryLabel(), CS);  // Carry flag is the last bit shifted out by LSRS.
6560  __ Bind(slow_path->GetExitLabel());
6561}
6562
6563void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6564                                               Location out,
6565                                               Location ref,
6566                                               Location obj,
6567                                               uint32_t offset,
6568                                               Location index) {
6569  DCHECK(kEmitCompilerReadBarrier);
6570
6571  // Insert a slow path based read barrier *after* the reference load.
6572  //
6573  // If heap poisoning is enabled, the unpoisoning of the loaded
6574  // reference will be carried out by the runtime within the slow
6575  // path.
6576  //
6577  // Note that `ref` currently does not get unpoisoned (when heap
6578  // poisoning is enabled), which is alright as the `ref` argument is
6579  // not used by the artReadBarrierSlow entry point.
6580  //
6581  // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6582  SlowPathCode* slow_path = new (GetGraph()->GetArena())
6583      ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6584  AddSlowPath(slow_path);
6585
6586  __ b(slow_path->GetEntryLabel());
6587  __ Bind(slow_path->GetExitLabel());
6588}
6589
6590void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6591                                                    Location out,
6592                                                    Location ref,
6593                                                    Location obj,
6594                                                    uint32_t offset,
6595                                                    Location index) {
6596  if (kEmitCompilerReadBarrier) {
6597    // Baker's read barriers shall be handled by the fast path
6598    // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6599    DCHECK(!kUseBakerReadBarrier);
6600    // If heap poisoning is enabled, unpoisoning will be taken care of
6601    // by the runtime within the slow path.
6602    GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6603  } else if (kPoisonHeapReferences) {
6604    __ UnpoisonHeapReference(out.AsRegister<Register>());
6605  }
6606}
6607
6608void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6609                                                      Location out,
6610                                                      Location root) {
6611  DCHECK(kEmitCompilerReadBarrier);
6612
6613  // Insert a slow path based read barrier *after* the GC root load.
6614  //
6615  // Note that GC roots are not affected by heap poisoning, so we do
6616  // not need to do anything special for this here.
6617  SlowPathCode* slow_path =
6618      new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6619  AddSlowPath(slow_path);
6620
6621  __ b(slow_path->GetEntryLabel());
6622  __ Bind(slow_path->GetExitLabel());
6623}
6624
6625HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6626      const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6627      MethodReference target_method) {
6628  HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6629  // We disable pc-relative load when there is an irreducible loop, as the optimization
6630  // is incompatible with it.
6631  // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6632  // with irreducible loops.
6633  if (GetGraph()->HasIrreducibleLoops() &&
6634      (dispatch_info.method_load_kind ==
6635          HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6636    dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6637  }
6638
6639  if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
6640    const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6641    if (&outer_dex_file != target_method.dex_file) {
6642      // Calls across dex files are more likely to exceed the available BL range,
6643      // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6644      HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6645          (desired_dispatch_info.method_load_kind ==
6646           HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6647          ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6648          : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6649      return HInvokeStaticOrDirect::DispatchInfo {
6650        dispatch_info.method_load_kind,
6651        code_ptr_location,
6652        dispatch_info.method_load_data,
6653        0u
6654      };
6655    }
6656  }
6657  return dispatch_info;
6658}
6659
6660Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6661                                                                 Register temp) {
6662  DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6663  Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6664  if (!invoke->GetLocations()->Intrinsified()) {
6665    return location.AsRegister<Register>();
6666  }
6667  // For intrinsics we allow any location, so it may be on the stack.
6668  if (!location.IsRegister()) {
6669    __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6670    return temp;
6671  }
6672  // For register locations, check if the register was saved. If so, get it from the stack.
6673  // Note: There is a chance that the register was saved but not overwritten, so we could
6674  // save one load. However, since this is just an intrinsic slow path we prefer this
6675  // simple and more robust approach rather that trying to determine if that's the case.
6676  SlowPathCode* slow_path = GetCurrentSlowPath();
6677  DCHECK(slow_path != nullptr);  // For intrinsified invokes the call is emitted on the slow path.
6678  if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6679    int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6680    __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6681    return temp;
6682  }
6683  return location.AsRegister<Register>();
6684}
6685
6686void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
6687  // For better instruction scheduling we load the direct code pointer before the method pointer.
6688  switch (invoke->GetCodePtrLocation()) {
6689    case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6690      // LR = code address from literal pool with link-time patch.
6691      __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
6692      break;
6693    case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6694      // LR = invoke->GetDirectCodePtr();
6695      __ LoadImmediate(LR, invoke->GetDirectCodePtr());
6696      break;
6697    default:
6698      break;
6699  }
6700
6701  Location callee_method = temp;  // For all kinds except kRecursive, callee will be in temp.
6702  switch (invoke->GetMethodLoadKind()) {
6703    case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6704      // temp = thread->string_init_entrypoint
6705      __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6706      break;
6707    case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
6708      callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6709      break;
6710    case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6711      __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6712      break;
6713    case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6714      __ LoadLiteral(temp.AsRegister<Register>(),
6715                     DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6716      break;
6717    case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6718      HArmDexCacheArraysBase* base =
6719          invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6720      Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6721                                                                temp.AsRegister<Register>());
6722      int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6723      __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6724      break;
6725    }
6726    case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
6727      Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6728      Register method_reg;
6729      Register reg = temp.AsRegister<Register>();
6730      if (current_method.IsRegister()) {
6731        method_reg = current_method.AsRegister<Register>();
6732      } else {
6733        DCHECK(invoke->GetLocations()->Intrinsified());
6734        DCHECK(!current_method.IsValid());
6735        method_reg = reg;
6736        __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6737      }
6738      // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6739      __ LoadFromOffset(kLoadWord,
6740                        reg,
6741                        method_reg,
6742                        ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
6743      // temp = temp[index_in_cache];
6744      // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6745      uint32_t index_in_cache = invoke->GetDexMethodIndex();
6746      __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6747      break;
6748    }
6749  }
6750
6751  switch (invoke->GetCodePtrLocation()) {
6752    case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6753      __ bl(GetFrameEntryLabel());
6754      break;
6755    case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
6756      relative_call_patches_.emplace_back(invoke->GetTargetMethod());
6757      __ BindTrackedLabel(&relative_call_patches_.back().label);
6758      // Arbitrarily branch to the BL itself, override at link time.
6759      __ bl(&relative_call_patches_.back().label);
6760      break;
6761    case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6762    case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6763      // LR prepared above for better instruction scheduling.
6764      // LR()
6765      __ blx(LR);
6766      break;
6767    case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6768      // LR = callee_method->entry_point_from_quick_compiled_code_
6769      __ LoadFromOffset(
6770          kLoadWord, LR, callee_method.AsRegister<Register>(),
6771          ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
6772      // LR()
6773      __ blx(LR);
6774      break;
6775  }
6776
6777  DCHECK(!IsLeafMethod());
6778}
6779
6780void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6781  Register temp = temp_location.AsRegister<Register>();
6782  uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6783      invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
6784
6785  // Use the calling convention instead of the location of the receiver, as
6786  // intrinsics may have put the receiver in a different register. In the intrinsics
6787  // slow path, the arguments have been moved to the right place, so here we are
6788  // guaranteed that the receiver is the first register of the calling convention.
6789  InvokeDexCallingConvention calling_convention;
6790  Register receiver = calling_convention.GetRegisterAt(0);
6791  uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6792  // /* HeapReference<Class> */ temp = receiver->klass_
6793  __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
6794  MaybeRecordImplicitNullCheck(invoke);
6795  // Instead of simply (possibly) unpoisoning `temp` here, we should
6796  // emit a read barrier for the previous class reference load.
6797  // However this is not required in practice, as this is an
6798  // intermediate/temporary reference and because the current
6799  // concurrent copying collector keeps the from-space memory
6800  // intact/accessible until the end of the marking phase (the
6801  // concurrent copying collector may not in the future).
6802  __ MaybeUnpoisonHeapReference(temp);
6803  // temp = temp->GetMethodAt(method_offset);
6804  uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6805      kArmPointerSize).Int32Value();
6806  __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6807  // LR = temp->GetEntryPoint();
6808  __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6809  // LR();
6810  __ blx(LR);
6811}
6812
6813CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6814    const DexFile& dex_file, uint32_t string_index) {
6815  return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6816}
6817
6818CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6819    const DexFile& dex_file, uint32_t type_index) {
6820  return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6821}
6822
6823CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6824    const DexFile& dex_file, uint32_t element_offset) {
6825  return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6826}
6827
6828CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6829    const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6830  patches->emplace_back(dex_file, offset_or_index);
6831  return &patches->back();
6832}
6833
6834Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6835                                                             uint32_t string_index) {
6836  return boot_image_string_patches_.GetOrCreate(
6837      StringReference(&dex_file, string_index),
6838      [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6839}
6840
6841Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6842                                                           uint32_t type_index) {
6843  return boot_image_type_patches_.GetOrCreate(
6844      TypeReference(&dex_file, type_index),
6845      [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6846}
6847
6848Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6849  bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6850  Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6851  return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6852}
6853
6854Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6855  return DeduplicateUint32Literal(address, &uint32_literals_);
6856}
6857
6858void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6859  DCHECK(linker_patches->empty());
6860  size_t size =
6861      method_patches_.size() +
6862      call_patches_.size() +
6863      relative_call_patches_.size() +
6864      /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6865      boot_image_string_patches_.size() +
6866      /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
6867      boot_image_type_patches_.size() +
6868      /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
6869      boot_image_address_patches_.size();
6870  linker_patches->reserve(size);
6871  for (const auto& entry : method_patches_) {
6872    const MethodReference& target_method = entry.first;
6873    Literal* literal = entry.second;
6874    DCHECK(literal->GetLabel()->IsBound());
6875    uint32_t literal_offset = literal->GetLabel()->Position();
6876    linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6877                                                       target_method.dex_file,
6878                                                       target_method.dex_method_index));
6879  }
6880  for (const auto& entry : call_patches_) {
6881    const MethodReference& target_method = entry.first;
6882    Literal* literal = entry.second;
6883    DCHECK(literal->GetLabel()->IsBound());
6884    uint32_t literal_offset = literal->GetLabel()->Position();
6885    linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6886                                                     target_method.dex_file,
6887                                                     target_method.dex_method_index));
6888  }
6889  for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6890    uint32_t literal_offset = info.label.Position();
6891    linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6892                                                             info.target_method.dex_file,
6893                                                             info.target_method.dex_method_index));
6894  }
6895  for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6896    const DexFile& dex_file = info.target_dex_file;
6897    size_t base_element_offset = info.offset_or_index;
6898    DCHECK(info.add_pc_label.IsBound());
6899    uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6900    // Add MOVW patch.
6901    DCHECK(info.movw_label.IsBound());
6902    uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6903    linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6904                                                              &dex_file,
6905                                                              add_pc_offset,
6906                                                              base_element_offset));
6907    // Add MOVT patch.
6908    DCHECK(info.movt_label.IsBound());
6909    uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6910    linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6911                                                              &dex_file,
6912                                                              add_pc_offset,
6913                                                              base_element_offset));
6914  }
6915  for (const auto& entry : boot_image_string_patches_) {
6916    const StringReference& target_string = entry.first;
6917    Literal* literal = entry.second;
6918    DCHECK(literal->GetLabel()->IsBound());
6919    uint32_t literal_offset = literal->GetLabel()->Position();
6920    linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6921                                                       target_string.dex_file,
6922                                                       target_string.string_index));
6923  }
6924  for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6925    const DexFile& dex_file = info.target_dex_file;
6926    uint32_t string_index = info.offset_or_index;
6927    DCHECK(info.add_pc_label.IsBound());
6928    uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6929    // Add MOVW patch.
6930    DCHECK(info.movw_label.IsBound());
6931    uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6932    linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6933                                                               &dex_file,
6934                                                               add_pc_offset,
6935                                                               string_index));
6936    // Add MOVT patch.
6937    DCHECK(info.movt_label.IsBound());
6938    uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6939    linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6940                                                               &dex_file,
6941                                                               add_pc_offset,
6942                                                               string_index));
6943  }
6944  for (const auto& entry : boot_image_type_patches_) {
6945    const TypeReference& target_type = entry.first;
6946    Literal* literal = entry.second;
6947    DCHECK(literal->GetLabel()->IsBound());
6948    uint32_t literal_offset = literal->GetLabel()->Position();
6949    linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6950                                                     target_type.dex_file,
6951                                                     target_type.type_index));
6952  }
6953  for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6954    const DexFile& dex_file = info.target_dex_file;
6955    uint32_t type_index = info.offset_or_index;
6956    DCHECK(info.add_pc_label.IsBound());
6957    uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6958    // Add MOVW patch.
6959    DCHECK(info.movw_label.IsBound());
6960    uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6961    linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6962                                                             &dex_file,
6963                                                             add_pc_offset,
6964                                                             type_index));
6965    // Add MOVT patch.
6966    DCHECK(info.movt_label.IsBound());
6967    uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6968    linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6969                                                             &dex_file,
6970                                                             add_pc_offset,
6971                                                             type_index));
6972  }
6973  for (const auto& entry : boot_image_address_patches_) {
6974    DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6975    Literal* literal = entry.second;
6976    DCHECK(literal->GetLabel()->IsBound());
6977    uint32_t literal_offset = literal->GetLabel()->Position();
6978    linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6979  }
6980}
6981
6982Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6983  return map->GetOrCreate(
6984      value,
6985      [this, value]() { return __ NewLiteral<uint32_t>(value); });
6986}
6987
6988Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6989                                                    MethodToLiteralMap* map) {
6990  return map->GetOrCreate(
6991      target_method,
6992      [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6993}
6994
6995Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6996  return DeduplicateMethodLiteral(target_method, &method_patches_);
6997}
6998
6999Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7000  return DeduplicateMethodLiteral(target_method, &call_patches_);
7001}
7002
7003void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7004  LocationSummary* locations =
7005      new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7006  locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7007                     Location::RequiresRegister());
7008  locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7009  locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7010  locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7011}
7012
7013void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7014  LocationSummary* locations = instr->GetLocations();
7015  Register res = locations->Out().AsRegister<Register>();
7016  Register accumulator =
7017      locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7018  Register mul_left =
7019      locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7020  Register mul_right =
7021      locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7022
7023  if (instr->GetOpKind() == HInstruction::kAdd) {
7024    __ mla(res, mul_left, mul_right, accumulator);
7025  } else {
7026    __ mls(res, mul_left, mul_right, accumulator);
7027  }
7028}
7029
7030void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7031  // Nothing to do, this should be removed during prepare for register allocator.
7032  LOG(FATAL) << "Unreachable";
7033}
7034
7035void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7036  // Nothing to do, this should be removed during prepare for register allocator.
7037  LOG(FATAL) << "Unreachable";
7038}
7039
7040// Simple implementation of packed switch - generate cascaded compare/jumps.
7041void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7042  LocationSummary* locations =
7043      new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7044  locations->SetInAt(0, Location::RequiresRegister());
7045  if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
7046      codegen_->GetAssembler()->IsThumb()) {
7047    locations->AddTemp(Location::RequiresRegister());  // We need a temp for the table base.
7048    if (switch_instr->GetStartValue() != 0) {
7049      locations->AddTemp(Location::RequiresRegister());  // We need a temp for the bias.
7050    }
7051  }
7052}
7053
7054void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7055  int32_t lower_bound = switch_instr->GetStartValue();
7056  uint32_t num_entries = switch_instr->GetNumEntries();
7057  LocationSummary* locations = switch_instr->GetLocations();
7058  Register value_reg = locations->InAt(0).AsRegister<Register>();
7059  HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7060
7061  if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
7062    // Create a series of compare/jumps.
7063    Register temp_reg = IP;
7064    // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7065    // the immediate, because IP is used as the destination register. For the other
7066    // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7067    // and they can be encoded in the instruction without making use of IP register.
7068    __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7069
7070    const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7071    // Jump to successors[0] if value == lower_bound.
7072    __ b(codegen_->GetLabelOf(successors[0]), EQ);
7073    int32_t last_index = 0;
7074    for (; num_entries - last_index > 2; last_index += 2) {
7075      __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7076      // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7077      __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7078      // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7079      __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7080    }
7081    if (num_entries - last_index == 2) {
7082      // The last missing case_value.
7083      __ CmpConstant(temp_reg, 1);
7084      __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
7085    }
7086
7087    // And the default for any other value.
7088    if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7089      __ b(codegen_->GetLabelOf(default_block));
7090    }
7091  } else {
7092    // Create a table lookup.
7093    Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7094
7095    // Materialize a pointer to the switch table
7096    std::vector<Label*> labels(num_entries);
7097    const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7098    for (uint32_t i = 0; i < num_entries; i++) {
7099      labels[i] = codegen_->GetLabelOf(successors[i]);
7100    }
7101    JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7102
7103    // Remove the bias.
7104    Register key_reg;
7105    if (lower_bound != 0) {
7106      key_reg = locations->GetTemp(1).AsRegister<Register>();
7107      __ AddConstant(key_reg, value_reg, -lower_bound);
7108    } else {
7109      key_reg = value_reg;
7110    }
7111
7112    // Check whether the value is in the table, jump to default block if not.
7113    __ CmpConstant(key_reg, num_entries - 1);
7114    __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7115
7116    // Load the displacement from the table.
7117    __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7118
7119    // Dispatch is a direct add to the PC (for Thumb2).
7120    __ EmitJumpTableDispatch(table, temp_reg);
7121  }
7122}
7123
7124void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7125  LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7126  locations->SetOut(Location::RequiresRegister());
7127}
7128
7129void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7130  Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
7131  CodeGeneratorARM::PcRelativePatchInfo* labels =
7132      codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
7133  __ BindTrackedLabel(&labels->movw_label);
7134  __ movw(base_reg, /* placeholder */ 0u);
7135  __ BindTrackedLabel(&labels->movt_label);
7136  __ movt(base_reg, /* placeholder */ 0u);
7137  __ BindTrackedLabel(&labels->add_pc_label);
7138  __ add(base_reg, base_reg, ShifterOperand(PC));
7139}
7140
7141void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7142  if (!trg.IsValid()) {
7143    DCHECK_EQ(type, Primitive::kPrimVoid);
7144    return;
7145  }
7146
7147  DCHECK_NE(type, Primitive::kPrimVoid);
7148
7149  Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7150  if (return_loc.Equals(trg)) {
7151    return;
7152  }
7153
7154  // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7155  //       with the last branch.
7156  if (type == Primitive::kPrimLong) {
7157    HParallelMove parallel_move(GetGraph()->GetArena());
7158    parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7159    parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7160    GetMoveResolver()->EmitNativeCode(&parallel_move);
7161  } else if (type == Primitive::kPrimDouble) {
7162    HParallelMove parallel_move(GetGraph()->GetArena());
7163    parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7164    parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7165    GetMoveResolver()->EmitNativeCode(&parallel_move);
7166  } else {
7167    // Let the parallel move resolver take care of all of this.
7168    HParallelMove parallel_move(GetGraph()->GetArena());
7169    parallel_move.AddMove(return_loc, trg, type, nullptr);
7170    GetMoveResolver()->EmitNativeCode(&parallel_move);
7171  }
7172}
7173
7174void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7175  LocationSummary* locations =
7176      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7177  locations->SetInAt(0, Location::RequiresRegister());
7178  locations->SetOut(Location::RequiresRegister());
7179}
7180
7181void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7182  LocationSummary* locations = instruction->GetLocations();
7183  if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7184    uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7185        instruction->GetIndex(), kArmPointerSize).SizeValue();
7186    __ LoadFromOffset(kLoadWord,
7187                      locations->Out().AsRegister<Register>(),
7188                      locations->InAt(0).AsRegister<Register>(),
7189                      method_offset);
7190  } else {
7191    uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7192        instruction->GetIndex(), kArmPointerSize));
7193    __ LoadFromOffset(kLoadWord,
7194                      locations->Out().AsRegister<Register>(),
7195                      locations->InAt(0).AsRegister<Register>(),
7196                      mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7197    __ LoadFromOffset(kLoadWord,
7198                      locations->Out().AsRegister<Register>(),
7199                      locations->Out().AsRegister<Register>(),
7200                      method_offset);
7201  }
7202}
7203
7204#undef __
7205#undef QUICK_ENTRY_POINT
7206
7207}  // namespace arm
7208}  // namespace art
7209