blob: e0dba21d711f4d90dce9635f6fc782bf31b665cc [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 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_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 return next_location;
95}
96
97Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
98 return Mips64ReturnLocation(type);
99}
100
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100101// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
102#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700103#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
105class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100110 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
112 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000113 if (instruction_->CanThrowIntoCatchBlock()) {
114 // Live registers will be restored in the catch block if caught.
115 SaveLiveRegisters(codegen, instruction_->GetLocations());
116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
119 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
122 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
125 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100126 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
127 ? kQuickThrowStringBounds
128 : kQuickThrowArrayBounds;
129 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
132 }
133
Alexandre Rames8158f282015-08-07 10:26:17 +0100134 bool IsFatal() const OVERRIDE { return true; }
135
Roland Levillain46648892015-06-19 16:07:18 +0100136 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
137
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
140};
141
142class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
143 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
145 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146
147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
148 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
149 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100150 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700151 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Roland Levillain46648892015-06-19 16:07:18 +0100156 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
157
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
160};
161
162class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
163 public:
164 LoadClassSlowPathMIPS64(HLoadClass* cls,
165 HInstruction* at,
166 uint32_t dex_pc,
167 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000168 : SlowPathCodeMIPS64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
170 }
171
172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000173 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700174 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
175
176 __ Bind(GetEntryLabel());
177 SaveLiveRegisters(codegen, locations);
178
179 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000180 dex::TypeIndex type_index = cls_->GetTypeIndex();
181 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100182 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
183 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000184 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 if (do_clinit_) {
186 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
187 } else {
188 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
189 }
190
191 // Move the class to the desired location.
192 Location out = locations->Out();
193 if (out.IsValid()) {
194 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700196 mips64_codegen->MoveLocation(out,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
198 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700199 }
200
201 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000202 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
203 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
204 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
205 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000206 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000207 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000208 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
209 __ Sw(out.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
210 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700211 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 }
213
Roland Levillain46648892015-06-19 16:07:18 +0100214 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
215
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 private:
217 // The class this slow path will load.
218 HLoadClass* const cls_;
219
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 // The dex PC of `at_`.
221 const uint32_t dex_pc_;
222
223 // Whether to initialize the class.
224 const bool do_clinit_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227};
228
229class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000231 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 LocationSummary* locations = instruction_->GetLocations();
235 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237
238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
241 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800242 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000243 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100245 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700246 instruction_,
247 instruction_->GetDexPc(),
248 this);
249 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
250 Primitive::Type type = instruction_->GetType();
251 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700252 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700253 type);
254
255 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800256
257 // Store the resolved String to the BSS entry.
Alexey Frunzef63f5692016-12-13 17:43:11 -0800258 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunzef63f5692016-12-13 17:43:11 -0800259 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
260 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
261 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
262 __ Sw(out, AT, /* placeholder */ 0x5678);
263
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700264 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 }
266
Roland Levillain46648892015-06-19 16:07:18 +0100267 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
268
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
271};
272
273class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
274 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
279 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000280 if (instruction_->CanThrowIntoCatchBlock()) {
281 // Live registers will be restored in the catch block if caught.
282 SaveLiveRegisters(codegen, instruction_->GetLocations());
283 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100284 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700285 instruction_,
286 instruction_->GetDexPc(),
287 this);
288 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Roland Levillain46648892015-06-19 16:07:18 +0100293 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
294
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200305 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700306 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
307 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200308 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100309 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200311 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 }
317 }
318
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700319 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 DCHECK(successor_ == nullptr);
321 return &return_label_;
322 }
323
Roland Levillain46648892015-06-19 16:07:18 +0100324 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
325
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 // If not null, the block to branch to after the suspend check.
328 HBasicBlock* const successor_;
329
330 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700331 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332
333 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
334};
335
336class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
337 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800338 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
339 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
347 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
348
349 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800350 if (!is_fatal_) {
351 SaveLiveRegisters(codegen, locations);
352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100364 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800365 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 Primitive::Type ret_type = instruction_->GetType();
367 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
368 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 } else {
370 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800371 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
372 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 }
374
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800375 if (!is_fatal_) {
376 RestoreLiveRegisters(codegen, locations);
377 __ Bc(GetExitLabel());
378 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 }
380
Roland Levillain46648892015-06-19 16:07:18 +0100381 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
382
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 bool IsFatal() const OVERRIDE { return is_fatal_; }
384
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 const bool is_fatal_;
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
389};
390
391class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
392 public:
Aart Bik42249c32016-01-07 15:33:50 -0800393 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800397 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100399 LocationSummary* locations = instruction_->GetLocations();
400 SaveLiveRegisters(codegen, locations);
401 InvokeRuntimeCallingConvention calling_convention;
402 __ LoadConst32(calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100404 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 }
407
Roland Levillain46648892015-06-19 16:07:18 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
409
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
412};
413
Alexey Frunze15958152017-02-09 19:08:30 -0800414class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
415 public:
416 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
424 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
428 Primitive::kPrimNot,
429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
433 Primitive::kPrimInt,
434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
438 Primitive::kPrimNot,
439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
443 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
445 RestoreLiveRegisters(codegen, locations);
446 __ Bc(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
450
451 private:
452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
453};
454
455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
466//
467// If `entrypoint` is a valid location it is assumed to already be
468// holding the entrypoint. The case where the entrypoint is passed in
469// is for the GcRoot read barrier.
470class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
471 public:
472 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
473 Location ref,
474 Location entrypoint = Location::NoLocation())
475 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
476 DCHECK(kEmitCompilerReadBarrier);
477 }
478
479 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 LocationSummary* locations = instruction_->GetLocations();
483 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
484 DCHECK(locations->CanCall());
485 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
486 DCHECK(instruction_->IsInstanceFieldGet() ||
487 instruction_->IsStaticFieldGet() ||
488 instruction_->IsArrayGet() ||
489 instruction_->IsArraySet() ||
490 instruction_->IsLoadClass() ||
491 instruction_->IsLoadString() ||
492 instruction_->IsInstanceOf() ||
493 instruction_->IsCheckCast() ||
494 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
495 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
496 << "Unexpected instruction in read barrier marking slow path: "
497 << instruction_->DebugName();
498
499 __ Bind(GetEntryLabel());
500 // No need to save live registers; it's taken care of by the
501 // entrypoint. Also, there is no need to update the stack mask,
502 // as this runtime call will not trigger a garbage collection.
503 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
504 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
505 (S2 <= ref_reg && ref_reg <= S7) ||
506 (ref_reg == S8)) << ref_reg;
507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in A0 and V0 respectively):
511 //
512 // A0 <- ref
513 // V0 <- ReadBarrierMark(A0)
514 // ref <- V0
515 //
516 // we just use rX (the register containing `ref`) as input and output
517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
521 if (entrypoint_.IsValid()) {
522 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
523 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
524 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
525 __ Nop();
526 } else {
527 int32_t entry_point_offset =
528 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
529 // This runtime call does not require a stack map.
530 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
531 instruction_,
532 this);
533 }
534 __ Bc(GetExitLabel());
535 }
536
537 private:
538 // The location (register) of the marked object reference.
539 const Location ref_;
540
541 // The location of the entrypoint if already loaded.
542 const Location entrypoint_;
543
544 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
545};
546
547// Slow path marking an object reference `ref` during a read barrier,
548// and if needed, atomically updating the field `obj.field` in the
549// object `obj` holding this reference after marking (contrary to
550// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
551// `obj.field`).
552//
553// This means that after the execution of this slow path, both `ref`
554// and `obj.field` will be up-to-date; i.e., after the flip, both will
555// hold the same to-space reference (unless another thread installed
556// another object reference (different from `ref`) in `obj.field`).
557class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
558 public:
559 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
560 Location ref,
561 GpuRegister obj,
562 Location field_offset,
563 GpuRegister temp1)
564 : SlowPathCodeMIPS64(instruction),
565 ref_(ref),
566 obj_(obj),
567 field_offset_(field_offset),
568 temp1_(temp1) {
569 DCHECK(kEmitCompilerReadBarrier);
570 }
571
572 const char* GetDescription() const OVERRIDE {
573 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
574 }
575
576 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
577 LocationSummary* locations = instruction_->GetLocations();
578 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
579 DCHECK(locations->CanCall());
580 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
581 // This slow path is only used by the UnsafeCASObject intrinsic.
582 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
583 << "Unexpected instruction in read barrier marking and field updating slow path: "
584 << instruction_->DebugName();
585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
587 DCHECK(field_offset_.IsRegister()) << field_offset_;
588
589 __ Bind(GetEntryLabel());
590
591 // Save the old reference.
592 // Note that we cannot use AT or TMP to save the old reference, as those
593 // are used by the code that follows, but we need the old reference after
594 // the call to the ReadBarrierMarkRegX entry point.
595 DCHECK_NE(temp1_, AT);
596 DCHECK_NE(temp1_, TMP);
597 __ Move(temp1_, ref_reg);
598
599 // No need to save live registers; it's taken care of by the
600 // entrypoint. Also, there is no need to update the stack mask,
601 // as this runtime call will not trigger a garbage collection.
602 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
603 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
604 (S2 <= ref_reg && ref_reg <= S7) ||
605 (ref_reg == S8)) << ref_reg;
606 // "Compact" slow path, saving two moves.
607 //
608 // Instead of using the standard runtime calling convention (input
609 // and output in A0 and V0 respectively):
610 //
611 // A0 <- ref
612 // V0 <- ReadBarrierMark(A0)
613 // ref <- V0
614 //
615 // we just use rX (the register containing `ref`) as input and output
616 // of a dedicated entrypoint:
617 //
618 // rX <- ReadBarrierMarkRegX(rX)
619 //
620 int32_t entry_point_offset =
621 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
622 // This runtime call does not require a stack map.
623 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
624 instruction_,
625 this);
626
627 // If the new reference is different from the old reference,
628 // update the field in the holder (`*(obj_ + field_offset_)`).
629 //
630 // Note that this field could also hold a different object, if
631 // another thread had concurrently changed it. In that case, the
632 // the compare-and-set (CAS) loop below would abort, leaving the
633 // field as-is.
634 Mips64Label done;
635 __ Beqc(temp1_, ref_reg, &done);
636
637 // Update the the holder's field atomically. This may fail if
638 // mutator updates before us, but it's OK. This is achieved
639 // using a strong compare-and-set (CAS) operation with relaxed
640 // memory synchronization ordering, where the expected value is
641 // the old reference and the desired value is the new reference.
642
643 // Convenience aliases.
644 GpuRegister base = obj_;
645 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
646 GpuRegister expected = temp1_;
647 GpuRegister value = ref_reg;
648 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
649 GpuRegister tmp = AT; // Value in memory.
650
651 __ Daddu(tmp_ptr, base, offset);
652
653 if (kPoisonHeapReferences) {
654 __ PoisonHeapReference(expected);
655 // Do not poison `value` if it is the same register as
656 // `expected`, which has just been poisoned.
657 if (value != expected) {
658 __ PoisonHeapReference(value);
659 }
660 }
661
662 // do {
663 // tmp = [r_ptr] - expected;
664 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
665
666 Mips64Label loop_head, exit_loop;
667 __ Bind(&loop_head);
668 __ Ll(tmp, tmp_ptr);
669 // The LL instruction sign-extends the 32-bit value, but
670 // 32-bit references must be zero-extended. Zero-extend `tmp`.
671 __ Dext(tmp, tmp, 0, 32);
672 __ Bnec(tmp, expected, &exit_loop);
673 __ Move(tmp, value);
674 __ Sc(tmp, tmp_ptr);
675 __ Beqzc(tmp, &loop_head);
676 __ Bind(&exit_loop);
677
678 if (kPoisonHeapReferences) {
679 __ UnpoisonHeapReference(expected);
680 // Do not unpoison `value` if it is the same register as
681 // `expected`, which has just been unpoisoned.
682 if (value != expected) {
683 __ UnpoisonHeapReference(value);
684 }
685 }
686
687 __ Bind(&done);
688 __ Bc(GetExitLabel());
689 }
690
691 private:
692 // The location (register) of the marked object reference.
693 const Location ref_;
694 // The register containing the object holding the marked object reference field.
695 const GpuRegister obj_;
696 // The location of the offset of the marked reference field within `obj_`.
697 Location field_offset_;
698
699 const GpuRegister temp1_;
700
701 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
702};
703
704// Slow path generating a read barrier for a heap reference.
705class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
706 public:
707 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
708 Location out,
709 Location ref,
710 Location obj,
711 uint32_t offset,
712 Location index)
713 : SlowPathCodeMIPS64(instruction),
714 out_(out),
715 ref_(ref),
716 obj_(obj),
717 offset_(offset),
718 index_(index) {
719 DCHECK(kEmitCompilerReadBarrier);
720 // If `obj` is equal to `out` or `ref`, it means the initial object
721 // has been overwritten by (or after) the heap object reference load
722 // to be instrumented, e.g.:
723 //
724 // __ LoadFromOffset(kLoadWord, out, out, offset);
725 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
726 //
727 // In that case, we have lost the information about the original
728 // object, and the emitted read barrier cannot work properly.
729 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
730 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
731 }
732
733 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
734 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
735 LocationSummary* locations = instruction_->GetLocations();
736 Primitive::Type type = Primitive::kPrimNot;
737 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
738 DCHECK(locations->CanCall());
739 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
740 DCHECK(instruction_->IsInstanceFieldGet() ||
741 instruction_->IsStaticFieldGet() ||
742 instruction_->IsArrayGet() ||
743 instruction_->IsInstanceOf() ||
744 instruction_->IsCheckCast() ||
745 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier for heap reference slow path: "
747 << instruction_->DebugName();
748
749 __ Bind(GetEntryLabel());
750 SaveLiveRegisters(codegen, locations);
751
752 // We may have to change the index's value, but as `index_` is a
753 // constant member (like other "inputs" of this slow path),
754 // introduce a copy of it, `index`.
755 Location index = index_;
756 if (index_.IsValid()) {
757 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
758 if (instruction_->IsArrayGet()) {
759 // Compute the actual memory offset and store it in `index`.
760 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
761 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
762 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
763 // We are about to change the value of `index_reg` (see the
764 // calls to art::mips64::Mips64Assembler::Sll and
765 // art::mips64::MipsAssembler::Addiu32 below), but it has
766 // not been saved by the previous call to
767 // art::SlowPathCode::SaveLiveRegisters, as it is a
768 // callee-save register --
769 // art::SlowPathCode::SaveLiveRegisters does not consider
770 // callee-save registers, as it has been designed with the
771 // assumption that callee-save registers are supposed to be
772 // handled by the called function. So, as a callee-save
773 // register, `index_reg` _would_ eventually be saved onto
774 // the stack, but it would be too late: we would have
775 // changed its value earlier. Therefore, we manually save
776 // it here into another freely available register,
777 // `free_reg`, chosen of course among the caller-save
778 // registers (as a callee-save `free_reg` register would
779 // exhibit the same problem).
780 //
781 // Note we could have requested a temporary register from
782 // the register allocator instead; but we prefer not to, as
783 // this is a slow path, and we know we can find a
784 // caller-save register that is available.
785 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
786 __ Move(free_reg, index_reg);
787 index_reg = free_reg;
788 index = Location::RegisterLocation(index_reg);
789 } else {
790 // The initial register stored in `index_` has already been
791 // saved in the call to art::SlowPathCode::SaveLiveRegisters
792 // (as it is not a callee-save register), so we can freely
793 // use it.
794 }
795 // Shifting the index value contained in `index_reg` by the scale
796 // factor (2) cannot overflow in practice, as the runtime is
797 // unable to allocate object arrays with a size larger than
798 // 2^26 - 1 (that is, 2^28 - 4 bytes).
799 __ Sll(index_reg, index_reg, TIMES_4);
800 static_assert(
801 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
802 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
803 __ Addiu32(index_reg, index_reg, offset_);
804 } else {
805 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
806 // intrinsics, `index_` is not shifted by a scale factor of 2
807 // (as in the case of ArrayGet), as it is actually an offset
808 // to an object field within an object.
809 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
810 DCHECK(instruction_->GetLocations()->Intrinsified());
811 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
812 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
813 << instruction_->AsInvoke()->GetIntrinsic();
814 DCHECK_EQ(offset_, 0U);
815 DCHECK(index_.IsRegister());
816 }
817 }
818
819 // We're moving two or three locations to locations that could
820 // overlap, so we need a parallel move resolver.
821 InvokeRuntimeCallingConvention calling_convention;
822 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
823 parallel_move.AddMove(ref_,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
825 Primitive::kPrimNot,
826 nullptr);
827 parallel_move.AddMove(obj_,
828 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
829 Primitive::kPrimNot,
830 nullptr);
831 if (index.IsValid()) {
832 parallel_move.AddMove(index,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
834 Primitive::kPrimInt,
835 nullptr);
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 } else {
838 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
839 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
840 }
841 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
842 instruction_,
843 instruction_->GetDexPc(),
844 this);
845 CheckEntrypointTypes<
846 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
847 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ Bc(GetExitLabel());
851 }
852
853 const char* GetDescription() const OVERRIDE {
854 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
855 }
856
857 private:
858 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
859 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
860 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
861 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
862 if (i != ref &&
863 i != obj &&
864 !codegen->IsCoreCalleeSaveRegister(i) &&
865 !codegen->IsBlockedCoreRegister(i)) {
866 return static_cast<GpuRegister>(i);
867 }
868 }
869 // We shall never fail to find a free caller-save register, as
870 // there are more than two core caller-save registers on MIPS64
871 // (meaning it is possible to find one which is different from
872 // `ref` and `obj`).
873 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
874 LOG(FATAL) << "Could not find a free caller-save register";
875 UNREACHABLE();
876 }
877
878 const Location out_;
879 const Location ref_;
880 const Location obj_;
881 const uint32_t offset_;
882 // An additional location containing an index to an array.
883 // Only used for HArrayGet and the UnsafeGetObject &
884 // UnsafeGetObjectVolatile intrinsics.
885 const Location index_;
886
887 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
888};
889
890// Slow path generating a read barrier for a GC root.
891class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
892 public:
893 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
894 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
895 DCHECK(kEmitCompilerReadBarrier);
896 }
897
898 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
899 LocationSummary* locations = instruction_->GetLocations();
900 Primitive::Type type = Primitive::kPrimNot;
901 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
902 DCHECK(locations->CanCall());
903 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
904 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
905 << "Unexpected instruction in read barrier for GC root slow path: "
906 << instruction_->DebugName();
907
908 __ Bind(GetEntryLabel());
909 SaveLiveRegisters(codegen, locations);
910
911 InvokeRuntimeCallingConvention calling_convention;
912 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
913 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
914 root_,
915 Primitive::kPrimNot);
916 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
917 instruction_,
918 instruction_->GetDexPc(),
919 this);
920 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
921 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
922
923 RestoreLiveRegisters(codegen, locations);
924 __ Bc(GetExitLabel());
925 }
926
927 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
928
929 private:
930 const Location out_;
931 const Location root_;
932
933 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
934};
935
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
937 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100938 const CompilerOptions& compiler_options,
939 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940 : CodeGenerator(graph,
941 kNumberOfGpuRegisters,
942 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000943 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
945 arraysize(kCoreCalleeSaves)),
946 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
947 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100948 compiler_options,
949 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100950 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 location_builder_(graph, this),
952 instruction_visitor_(graph, this),
953 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100954 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800955 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800956 uint32_literals_(std::less<uint32_t>(),
957 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 uint64_literals_(std::less<uint64_t>(),
959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800960 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800961 boot_image_string_patches_(StringReferenceValueComparator(),
962 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
964 boot_image_type_patches_(TypeReferenceValueComparator(),
965 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
966 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000967 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800968 jit_string_patches_(StringReferenceValueComparator(),
969 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
970 jit_class_patches_(TypeReferenceValueComparator(),
971 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700972 // Save RA (containing the return address) to mimic Quick.
973 AddAllocatedRegister(Location::RegisterLocation(RA));
974}
975
976#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100977// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
978#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700979#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700980
981void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700982 // Ensure that we fix up branches.
983 __ FinalizeCode();
984
985 // Adjust native pc offsets in stack maps.
986 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800987 uint32_t old_position =
988 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700989 uint32_t new_position = __ GetAdjustedPosition(old_position);
990 DCHECK_GE(new_position, old_position);
991 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
992 }
993
994 // Adjust pc offsets for the disassembly information.
995 if (disasm_info_ != nullptr) {
996 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
997 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
998 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
999 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1000 it.second.start = __ GetAdjustedPosition(it.second.start);
1001 it.second.end = __ GetAdjustedPosition(it.second.end);
1002 }
1003 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1004 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1005 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1006 }
1007 }
1008
Alexey Frunze4dda3372015-06-01 18:31:49 -07001009 CodeGenerator::Finalize(allocator);
1010}
1011
1012Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1013 return codegen_->GetAssembler();
1014}
1015
1016void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001017 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001018 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1019}
1020
1021void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001022 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001023 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1024}
1025
1026void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1027 // Pop reg
1028 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001029 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001030}
1031
1032void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1033 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001034 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001035 __ Sd(GpuRegister(reg), SP, 0);
1036}
1037
1038void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1039 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1040 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1041 // Allocate a scratch register other than TMP, if available.
1042 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1043 // automatically unspilled when the scratch scope object is destroyed).
1044 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1045 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001046 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001047 __ LoadFromOffset(load_type,
1048 GpuRegister(ensure_scratch.GetRegister()),
1049 SP,
1050 index1 + stack_offset);
1051 __ LoadFromOffset(load_type,
1052 TMP,
1053 SP,
1054 index2 + stack_offset);
1055 __ StoreToOffset(store_type,
1056 GpuRegister(ensure_scratch.GetRegister()),
1057 SP,
1058 index2 + stack_offset);
1059 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1060}
1061
1062static dwarf::Reg DWARFReg(GpuRegister reg) {
1063 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1064}
1065
David Srbeckyba702002016-02-01 18:15:29 +00001066static dwarf::Reg DWARFReg(FpuRegister reg) {
1067 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1068}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001069
1070void CodeGeneratorMIPS64::GenerateFrameEntry() {
1071 __ Bind(&frame_entry_label_);
1072
1073 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1074
1075 if (do_overflow_check) {
1076 __ LoadFromOffset(kLoadWord,
1077 ZERO,
1078 SP,
1079 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1080 RecordPcInfo(nullptr, 0);
1081 }
1082
Alexey Frunze4dda3372015-06-01 18:31:49 -07001083 if (HasEmptyFrame()) {
1084 return;
1085 }
1086
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001087 // Make sure the frame size isn't unreasonably large.
1088 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1089 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1090 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091
1092 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001093
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001094 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001095 __ IncreaseFrameSize(ofs);
1096
1097 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1098 GpuRegister reg = kCoreCalleeSaves[i];
1099 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001100 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001101 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001102 __ cfi().RelOffset(DWARFReg(reg), ofs);
1103 }
1104 }
1105
1106 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1107 FpuRegister reg = kFpuCalleeSaves[i];
1108 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001109 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001110 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001111 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001112 }
1113 }
1114
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001115 // Save the current method if we need it. Note that we do not
1116 // do this in HCurrentMethod, as the instruction might have been removed
1117 // in the SSA graph.
1118 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001119 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001120 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001121
1122 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1123 // Initialize should_deoptimize flag to 0.
1124 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1125 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001126}
1127
1128void CodeGeneratorMIPS64::GenerateFrameExit() {
1129 __ cfi().RememberState();
1130
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001133
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001134 // For better instruction scheduling restore RA before other registers.
1135 uint32_t ofs = GetFrameSize();
1136 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 GpuRegister reg = kCoreCalleeSaves[i];
1138 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001139 ofs -= kMips64DoublewordSize;
1140 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001141 __ cfi().Restore(DWARFReg(reg));
1142 }
1143 }
1144
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001145 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1146 FpuRegister reg = kFpuCalleeSaves[i];
1147 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1148 ofs -= kMips64DoublewordSize;
1149 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1150 __ cfi().Restore(DWARFReg(reg));
1151 }
1152 }
1153
1154 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001155 }
1156
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001157 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001158
1159 __ cfi().RestoreState();
1160 __ cfi().DefCFAOffset(GetFrameSize());
1161}
1162
1163void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1164 __ Bind(GetLabelOf(block));
1165}
1166
1167void CodeGeneratorMIPS64::MoveLocation(Location destination,
1168 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001169 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001170 if (source.Equals(destination)) {
1171 return;
1172 }
1173
1174 // A valid move can always be inferred from the destination and source
1175 // locations. When moving from and to a register, the argument type can be
1176 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001177 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178 DCHECK_EQ(unspecified_type, false);
1179
1180 if (destination.IsRegister() || destination.IsFpuRegister()) {
1181 if (unspecified_type) {
1182 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1183 if (source.IsStackSlot() ||
1184 (src_cst != nullptr && (src_cst->IsIntConstant()
1185 || src_cst->IsFloatConstant()
1186 || src_cst->IsNullConstant()))) {
1187 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001188 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001189 } else {
1190 // If the source is a double stack slot or a 64bit constant, a 64bit
1191 // type is appropriate. Else the source is a register, and since the
1192 // type has not been specified, we chose a 64bit type to force a 64bit
1193 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001194 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 }
1196 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001197 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1198 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1200 // Move to GPR/FPR from stack
1201 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001202 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001203 __ LoadFpuFromOffset(load_type,
1204 destination.AsFpuRegister<FpuRegister>(),
1205 SP,
1206 source.GetStackIndex());
1207 } else {
1208 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1209 __ LoadFromOffset(load_type,
1210 destination.AsRegister<GpuRegister>(),
1211 SP,
1212 source.GetStackIndex());
1213 }
1214 } else if (source.IsConstant()) {
1215 // Move to GPR/FPR from constant
1216 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218 gpr = destination.AsRegister<GpuRegister>();
1219 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001221 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1222 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1223 gpr = ZERO;
1224 } else {
1225 __ LoadConst32(gpr, value);
1226 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001227 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001228 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1229 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1230 gpr = ZERO;
1231 } else {
1232 __ LoadConst64(gpr, value);
1233 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001234 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001235 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001236 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1239 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001240 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001241 if (destination.IsRegister()) {
1242 // Move to GPR from GPR
1243 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1244 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 DCHECK(destination.IsFpuRegister());
1246 if (Primitive::Is64BitType(dst_type)) {
1247 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1248 } else {
1249 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1250 }
1251 }
1252 } else if (source.IsFpuRegister()) {
1253 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001254 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001256 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1257 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1260 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 } else {
1262 DCHECK(destination.IsRegister());
1263 if (Primitive::Is64BitType(dst_type)) {
1264 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1265 } else {
1266 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1267 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001268 }
1269 }
1270 } else { // The destination is not a register. It must be a stack slot.
1271 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1272 if (source.IsRegister() || source.IsFpuRegister()) {
1273 if (unspecified_type) {
1274 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001276 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 }
1279 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1281 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 // Move to stack from GPR/FPR
1283 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1284 if (source.IsRegister()) {
1285 __ StoreToOffset(store_type,
1286 source.AsRegister<GpuRegister>(),
1287 SP,
1288 destination.GetStackIndex());
1289 } else {
1290 __ StoreFpuToOffset(store_type,
1291 source.AsFpuRegister<FpuRegister>(),
1292 SP,
1293 destination.GetStackIndex());
1294 }
1295 } else if (source.IsConstant()) {
1296 // Move to stack from constant
1297 HConstant* src_cst = source.GetConstant();
1298 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001299 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001300 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001301 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1302 if (value != 0) {
1303 gpr = TMP;
1304 __ LoadConst32(gpr, value);
1305 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001306 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001307 DCHECK(destination.IsDoubleStackSlot());
1308 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1309 if (value != 0) {
1310 gpr = TMP;
1311 __ LoadConst64(gpr, value);
1312 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001313 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001314 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001315 } else {
1316 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1317 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1318 // Move to stack from stack
1319 if (destination.IsStackSlot()) {
1320 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1321 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1322 } else {
1323 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1324 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1325 }
1326 }
1327 }
1328}
1329
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001330void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001331 DCHECK(!loc1.IsConstant());
1332 DCHECK(!loc2.IsConstant());
1333
1334 if (loc1.Equals(loc2)) {
1335 return;
1336 }
1337
1338 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1339 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1340 bool is_fp_reg1 = loc1.IsFpuRegister();
1341 bool is_fp_reg2 = loc2.IsFpuRegister();
1342
1343 if (loc2.IsRegister() && loc1.IsRegister()) {
1344 // Swap 2 GPRs
1345 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1346 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1347 __ Move(TMP, r2);
1348 __ Move(r2, r1);
1349 __ Move(r1, TMP);
1350 } else if (is_fp_reg2 && is_fp_reg1) {
1351 // Swap 2 FPRs
1352 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1353 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001354 if (type == Primitive::kPrimFloat) {
1355 __ MovS(FTMP, r1);
1356 __ MovS(r1, r2);
1357 __ MovS(r2, FTMP);
1358 } else {
1359 DCHECK_EQ(type, Primitive::kPrimDouble);
1360 __ MovD(FTMP, r1);
1361 __ MovD(r1, r2);
1362 __ MovD(r2, FTMP);
1363 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 } else if (is_slot1 != is_slot2) {
1365 // Swap GPR/FPR and stack slot
1366 Location reg_loc = is_slot1 ? loc2 : loc1;
1367 Location mem_loc = is_slot1 ? loc1 : loc2;
1368 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1369 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1370 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1371 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1372 if (reg_loc.IsFpuRegister()) {
1373 __ StoreFpuToOffset(store_type,
1374 reg_loc.AsFpuRegister<FpuRegister>(),
1375 SP,
1376 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001377 if (mem_loc.IsStackSlot()) {
1378 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1379 } else {
1380 DCHECK(mem_loc.IsDoubleStackSlot());
1381 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1382 }
1383 } else {
1384 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1385 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1386 }
1387 } else if (is_slot1 && is_slot2) {
1388 move_resolver_.Exchange(loc1.GetStackIndex(),
1389 loc2.GetStackIndex(),
1390 loc1.IsDoubleStackSlot());
1391 } else {
1392 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1393 }
1394}
1395
Calin Juravle175dc732015-08-25 15:42:32 +01001396void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1397 DCHECK(location.IsRegister());
1398 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1399}
1400
Calin Juravlee460d1d2015-09-29 04:52:17 +01001401void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1402 if (location.IsRegister()) {
1403 locations->AddTemp(location);
1404 } else {
1405 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1406 }
1407}
1408
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001409void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1410 GpuRegister value,
1411 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001412 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001413 GpuRegister card = AT;
1414 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001415 if (value_can_be_null) {
1416 __ Beqzc(value, &done);
1417 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 __ LoadFromOffset(kLoadDoubleword,
1419 card,
1420 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001421 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001422 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1423 __ Daddu(temp, card, temp);
1424 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001425 if (value_can_be_null) {
1426 __ Bind(&done);
1427 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001428}
1429
Alexey Frunze19f6c692016-11-30 19:19:55 -08001430template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1431inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1432 const ArenaDeque<PcRelativePatchInfo>& infos,
1433 ArenaVector<LinkerPatch>* linker_patches) {
1434 for (const PcRelativePatchInfo& info : infos) {
1435 const DexFile& dex_file = info.target_dex_file;
1436 size_t offset_or_index = info.offset_or_index;
1437 DCHECK(info.pc_rel_label.IsBound());
1438 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1439 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
1440 }
1441}
1442
1443void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1444 DCHECK(linker_patches->empty());
1445 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -08001446 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001447 pc_relative_string_patches_.size() +
1448 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001449 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001450 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001451 boot_image_type_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001452 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001453 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1454 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001455 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001456 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001457 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1458 linker_patches);
1459 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001460 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1461 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001462 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1463 linker_patches);
1464 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001465 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1466 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001467 for (const auto& entry : boot_image_string_patches_) {
1468 const StringReference& target_string = entry.first;
1469 Literal* literal = entry.second;
1470 DCHECK(literal->GetLabel()->IsBound());
1471 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1472 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1473 target_string.dex_file,
1474 target_string.string_index.index_));
1475 }
1476 for (const auto& entry : boot_image_type_patches_) {
1477 const TypeReference& target_type = entry.first;
1478 Literal* literal = entry.second;
1479 DCHECK(literal->GetLabel()->IsBound());
1480 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1481 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1482 target_type.dex_file,
1483 target_type.type_index.index_));
1484 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001485 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001486}
1487
1488CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001489 const DexFile& dex_file, dex::StringIndex string_index) {
1490 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001491}
1492
1493CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1494 const DexFile& dex_file, dex::TypeIndex type_index) {
1495 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001496}
1497
Vladimir Marko1998cd02017-01-13 13:02:58 +00001498CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1499 const DexFile& dex_file, dex::TypeIndex type_index) {
1500 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1501}
1502
Alexey Frunze19f6c692016-11-30 19:19:55 -08001503CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1504 const DexFile& dex_file, uint32_t element_offset) {
1505 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1506}
1507
Alexey Frunze19f6c692016-11-30 19:19:55 -08001508CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1509 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1510 patches->emplace_back(dex_file, offset_or_index);
1511 return &patches->back();
1512}
1513
Alexey Frunzef63f5692016-12-13 17:43:11 -08001514Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1515 return map->GetOrCreate(
1516 value,
1517 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1518}
1519
Alexey Frunze19f6c692016-11-30 19:19:55 -08001520Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1521 return uint64_literals_.GetOrCreate(
1522 value,
1523 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1524}
1525
1526Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1527 MethodToLiteralMap* map) {
1528 return map->GetOrCreate(
1529 target_method,
1530 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1531}
1532
Alexey Frunzef63f5692016-12-13 17:43:11 -08001533Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1534 dex::StringIndex string_index) {
1535 return boot_image_string_patches_.GetOrCreate(
1536 StringReference(&dex_file, string_index),
1537 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1538}
1539
1540Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1541 dex::TypeIndex type_index) {
1542 return boot_image_type_patches_.GetOrCreate(
1543 TypeReference(&dex_file, type_index),
1544 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1545}
1546
1547Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001548 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001549}
1550
Alexey Frunze19f6c692016-11-30 19:19:55 -08001551void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1552 GpuRegister out) {
1553 __ Bind(&info->pc_rel_label);
1554 // Add the high half of a 32-bit offset to PC.
1555 __ Auipc(out, /* placeholder */ 0x1234);
1556 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001557 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001558}
1559
Alexey Frunze627c1a02017-01-30 19:28:14 -08001560Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1561 dex::StringIndex string_index,
1562 Handle<mirror::String> handle) {
1563 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1564 reinterpret_cast64<uint64_t>(handle.GetReference()));
1565 return jit_string_patches_.GetOrCreate(
1566 StringReference(&dex_file, string_index),
1567 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1568}
1569
1570Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1571 dex::TypeIndex type_index,
1572 Handle<mirror::Class> handle) {
1573 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1574 reinterpret_cast64<uint64_t>(handle.GetReference()));
1575 return jit_class_patches_.GetOrCreate(
1576 TypeReference(&dex_file, type_index),
1577 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1578}
1579
1580void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1581 const uint8_t* roots_data,
1582 const Literal* literal,
1583 uint64_t index_in_table) const {
1584 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1585 uintptr_t address =
1586 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1587 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1588}
1589
1590void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1591 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001592 const StringReference& string_reference = entry.first;
1593 Literal* table_entry_literal = entry.second;
1594 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001595 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001596 uint64_t index_in_table = it->second;
1597 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001598 }
1599 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001600 const TypeReference& type_reference = entry.first;
1601 Literal* table_entry_literal = entry.second;
1602 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001603 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001604 uint64_t index_in_table = it->second;
1605 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001606 }
1607}
1608
David Brazdil58282f42016-01-14 12:45:10 +00001609void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001610 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1611 blocked_core_registers_[ZERO] = true;
1612 blocked_core_registers_[K0] = true;
1613 blocked_core_registers_[K1] = true;
1614 blocked_core_registers_[GP] = true;
1615 blocked_core_registers_[SP] = true;
1616 blocked_core_registers_[RA] = true;
1617
Lazar Trsicd9672662015-09-03 17:33:01 +02001618 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1619 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 blocked_core_registers_[AT] = true;
1621 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001622 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001623 blocked_fpu_registers_[FTMP] = true;
1624
1625 // Reserve suspend and thread registers.
1626 blocked_core_registers_[S0] = true;
1627 blocked_core_registers_[TR] = true;
1628
1629 // Reserve T9 for function calls
1630 blocked_core_registers_[T9] = true;
1631
Goran Jakovljevic782be112016-06-21 12:39:04 +02001632 if (GetGraph()->IsDebuggable()) {
1633 // Stubs do not save callee-save floating point registers. If the graph
1634 // is debuggable, we need to deal with these registers differently. For
1635 // now, just block them.
1636 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1637 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1638 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001639 }
1640}
1641
Alexey Frunze4dda3372015-06-01 18:31:49 -07001642size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1643 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001644 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001645}
1646
1647size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1648 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001649 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001650}
1651
1652size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001653 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1654 FpuRegister(reg_id),
1655 SP,
1656 stack_index);
1657 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001658}
1659
1660size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001661 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1662 FpuRegister(reg_id),
1663 SP,
1664 stack_index);
1665 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001666}
1667
1668void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001669 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001670}
1671
1672void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001673 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674}
1675
Calin Juravle175dc732015-08-25 15:42:32 +01001676void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001677 HInstruction* instruction,
1678 uint32_t dex_pc,
1679 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001680 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001681 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001682 if (EntrypointRequiresStackMap(entrypoint)) {
1683 RecordPcInfo(instruction, dex_pc, slow_path);
1684 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685}
1686
Alexey Frunze15958152017-02-09 19:08:30 -08001687void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1688 HInstruction* instruction,
1689 SlowPathCode* slow_path) {
1690 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1691 GenerateInvokeRuntime(entry_point_offset);
1692}
1693
1694void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1695 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1696 __ Jalr(T9);
1697 __ Nop();
1698}
1699
Alexey Frunze4dda3372015-06-01 18:31:49 -07001700void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1701 GpuRegister class_reg) {
1702 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1703 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1704 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001705 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1706 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707 __ Bind(slow_path->GetExitLabel());
1708}
1709
1710void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1711 __ Sync(0); // only stype 0 is supported
1712}
1713
1714void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1715 HBasicBlock* successor) {
1716 SuspendCheckSlowPathMIPS64* slow_path =
1717 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1718 codegen_->AddSlowPath(slow_path);
1719
1720 __ LoadFromOffset(kLoadUnsignedHalfword,
1721 TMP,
1722 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001723 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 if (successor == nullptr) {
1725 __ Bnezc(TMP, slow_path->GetEntryLabel());
1726 __ Bind(slow_path->GetReturnLabel());
1727 } else {
1728 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001729 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 // slow_path will return to GetLabelOf(successor).
1731 }
1732}
1733
1734InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1735 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001736 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737 assembler_(codegen->GetAssembler()),
1738 codegen_(codegen) {}
1739
1740void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1741 DCHECK_EQ(instruction->InputCount(), 2U);
1742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1743 Primitive::Type type = instruction->GetResultType();
1744 switch (type) {
1745 case Primitive::kPrimInt:
1746 case Primitive::kPrimLong: {
1747 locations->SetInAt(0, Location::RequiresRegister());
1748 HInstruction* right = instruction->InputAt(1);
1749 bool can_use_imm = false;
1750 if (right->IsConstant()) {
1751 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1752 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1753 can_use_imm = IsUint<16>(imm);
1754 } else if (instruction->IsAdd()) {
1755 can_use_imm = IsInt<16>(imm);
1756 } else {
1757 DCHECK(instruction->IsSub());
1758 can_use_imm = IsInt<16>(-imm);
1759 }
1760 }
1761 if (can_use_imm)
1762 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1763 else
1764 locations->SetInAt(1, Location::RequiresRegister());
1765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1766 }
1767 break;
1768
1769 case Primitive::kPrimFloat:
1770 case Primitive::kPrimDouble:
1771 locations->SetInAt(0, Location::RequiresFpuRegister());
1772 locations->SetInAt(1, Location::RequiresFpuRegister());
1773 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1774 break;
1775
1776 default:
1777 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1778 }
1779}
1780
1781void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1782 Primitive::Type type = instruction->GetType();
1783 LocationSummary* locations = instruction->GetLocations();
1784
1785 switch (type) {
1786 case Primitive::kPrimInt:
1787 case Primitive::kPrimLong: {
1788 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1789 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1790 Location rhs_location = locations->InAt(1);
1791
1792 GpuRegister rhs_reg = ZERO;
1793 int64_t rhs_imm = 0;
1794 bool use_imm = rhs_location.IsConstant();
1795 if (use_imm) {
1796 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1797 } else {
1798 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1799 }
1800
1801 if (instruction->IsAnd()) {
1802 if (use_imm)
1803 __ Andi(dst, lhs, rhs_imm);
1804 else
1805 __ And(dst, lhs, rhs_reg);
1806 } else if (instruction->IsOr()) {
1807 if (use_imm)
1808 __ Ori(dst, lhs, rhs_imm);
1809 else
1810 __ Or(dst, lhs, rhs_reg);
1811 } else if (instruction->IsXor()) {
1812 if (use_imm)
1813 __ Xori(dst, lhs, rhs_imm);
1814 else
1815 __ Xor(dst, lhs, rhs_reg);
1816 } else if (instruction->IsAdd()) {
1817 if (type == Primitive::kPrimInt) {
1818 if (use_imm)
1819 __ Addiu(dst, lhs, rhs_imm);
1820 else
1821 __ Addu(dst, lhs, rhs_reg);
1822 } else {
1823 if (use_imm)
1824 __ Daddiu(dst, lhs, rhs_imm);
1825 else
1826 __ Daddu(dst, lhs, rhs_reg);
1827 }
1828 } else {
1829 DCHECK(instruction->IsSub());
1830 if (type == Primitive::kPrimInt) {
1831 if (use_imm)
1832 __ Addiu(dst, lhs, -rhs_imm);
1833 else
1834 __ Subu(dst, lhs, rhs_reg);
1835 } else {
1836 if (use_imm)
1837 __ Daddiu(dst, lhs, -rhs_imm);
1838 else
1839 __ Dsubu(dst, lhs, rhs_reg);
1840 }
1841 }
1842 break;
1843 }
1844 case Primitive::kPrimFloat:
1845 case Primitive::kPrimDouble: {
1846 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1847 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1848 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1849 if (instruction->IsAdd()) {
1850 if (type == Primitive::kPrimFloat)
1851 __ AddS(dst, lhs, rhs);
1852 else
1853 __ AddD(dst, lhs, rhs);
1854 } else if (instruction->IsSub()) {
1855 if (type == Primitive::kPrimFloat)
1856 __ SubS(dst, lhs, rhs);
1857 else
1858 __ SubD(dst, lhs, rhs);
1859 } else {
1860 LOG(FATAL) << "Unexpected floating-point binary operation";
1861 }
1862 break;
1863 }
1864 default:
1865 LOG(FATAL) << "Unexpected binary operation type " << type;
1866 }
1867}
1868
1869void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001870 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001871
1872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1873 Primitive::Type type = instr->GetResultType();
1874 switch (type) {
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimLong: {
1877 locations->SetInAt(0, Location::RequiresRegister());
1878 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 break;
1881 }
1882 default:
1883 LOG(FATAL) << "Unexpected shift type " << type;
1884 }
1885}
1886
1887void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001888 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001889 LocationSummary* locations = instr->GetLocations();
1890 Primitive::Type type = instr->GetType();
1891
1892 switch (type) {
1893 case Primitive::kPrimInt:
1894 case Primitive::kPrimLong: {
1895 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1896 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1897 Location rhs_location = locations->InAt(1);
1898
1899 GpuRegister rhs_reg = ZERO;
1900 int64_t rhs_imm = 0;
1901 bool use_imm = rhs_location.IsConstant();
1902 if (use_imm) {
1903 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1904 } else {
1905 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1906 }
1907
1908 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001909 uint32_t shift_value = rhs_imm &
1910 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 if (shift_value == 0) {
1913 if (dst != lhs) {
1914 __ Move(dst, lhs);
1915 }
1916 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001917 if (instr->IsShl()) {
1918 __ Sll(dst, lhs, shift_value);
1919 } else if (instr->IsShr()) {
1920 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001921 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001923 } else {
1924 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001925 }
1926 } else {
1927 if (shift_value < 32) {
1928 if (instr->IsShl()) {
1929 __ Dsll(dst, lhs, shift_value);
1930 } else if (instr->IsShr()) {
1931 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001932 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001933 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001934 } else {
1935 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001936 }
1937 } else {
1938 shift_value -= 32;
1939 if (instr->IsShl()) {
1940 __ Dsll32(dst, lhs, shift_value);
1941 } else if (instr->IsShr()) {
1942 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001943 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001944 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001945 } else {
1946 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001947 }
1948 }
1949 }
1950 } else {
1951 if (type == Primitive::kPrimInt) {
1952 if (instr->IsShl()) {
1953 __ Sllv(dst, lhs, rhs_reg);
1954 } else if (instr->IsShr()) {
1955 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001956 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001957 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001958 } else {
1959 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001960 }
1961 } else {
1962 if (instr->IsShl()) {
1963 __ Dsllv(dst, lhs, rhs_reg);
1964 } else if (instr->IsShr()) {
1965 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001966 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001968 } else {
1969 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001970 }
1971 }
1972 }
1973 break;
1974 }
1975 default:
1976 LOG(FATAL) << "Unexpected shift operation type " << type;
1977 }
1978}
1979
1980void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1981 HandleBinaryOp(instruction);
1982}
1983
1984void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1985 HandleBinaryOp(instruction);
1986}
1987
1988void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1989 HandleBinaryOp(instruction);
1990}
1991
1992void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1993 HandleBinaryOp(instruction);
1994}
1995
1996void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001997 Primitive::Type type = instruction->GetType();
1998 bool object_array_get_with_read_barrier =
1999 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002000 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002001 new (GetGraph()->GetArena()) LocationSummary(instruction,
2002 object_array_get_with_read_barrier
2003 ? LocationSummary::kCallOnSlowPath
2004 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002005 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2007 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002010 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002011 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2012 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002013 // The output overlaps in the case of an object array get with
2014 // read barriers enabled: we do not want the move to overwrite the
2015 // array's location, as we need it to emit the read barrier.
2016 locations->SetOut(Location::RequiresRegister(),
2017 object_array_get_with_read_barrier
2018 ? Location::kOutputOverlap
2019 : Location::kNoOutputOverlap);
2020 }
2021 // We need a temporary register for the read barrier marking slow
2022 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2023 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2024 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002025 }
2026}
2027
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002028static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2029 auto null_checker = [codegen, instruction]() {
2030 codegen->MaybeRecordImplicitNullCheck(instruction);
2031 };
2032 return null_checker;
2033}
2034
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2036 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002037 Location obj_loc = locations->InAt(0);
2038 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2039 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002041 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002042 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002044 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002045 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2046 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002047 switch (type) {
2048 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002049 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 if (index.IsConstant()) {
2051 size_t offset =
2052 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002053 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002054 } else {
2055 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002056 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002062 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063 if (index.IsConstant()) {
2064 size_t offset =
2065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002066 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002067 } else {
2068 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002069 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002070 }
2071 break;
2072 }
2073
2074 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002075 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002076 if (index.IsConstant()) {
2077 size_t offset =
2078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002079 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002080 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002081 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002082 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002083 }
2084 break;
2085 }
2086
2087 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002088 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002089 if (maybe_compressed_char_at) {
2090 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002091 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002092 __ Dext(TMP, TMP, 0, 1);
2093 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2094 "Expecting 0=compressed, 1=uncompressed");
2095 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002097 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2098 if (maybe_compressed_char_at) {
2099 Mips64Label uncompressed_load, done;
2100 __ Bnezc(TMP, &uncompressed_load);
2101 __ LoadFromOffset(kLoadUnsignedByte,
2102 out,
2103 obj,
2104 data_offset + (const_index << TIMES_1));
2105 __ Bc(&done);
2106 __ Bind(&uncompressed_load);
2107 __ LoadFromOffset(kLoadUnsignedHalfword,
2108 out,
2109 obj,
2110 data_offset + (const_index << TIMES_2));
2111 __ Bind(&done);
2112 } else {
2113 __ LoadFromOffset(kLoadUnsignedHalfword,
2114 out,
2115 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002116 data_offset + (const_index << TIMES_2),
2117 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002118 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002120 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2121 if (maybe_compressed_char_at) {
2122 Mips64Label uncompressed_load, done;
2123 __ Bnezc(TMP, &uncompressed_load);
2124 __ Daddu(TMP, obj, index_reg);
2125 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2126 __ Bc(&done);
2127 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002128 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002129 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2130 __ Bind(&done);
2131 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002132 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002133 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002134 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002135 }
2136 break;
2137 }
2138
Alexey Frunze15958152017-02-09 19:08:30 -08002139 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002141 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002142 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2143 if (index.IsConstant()) {
2144 size_t offset =
2145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002146 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002147 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002148 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002149 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002150 }
2151 break;
2152 }
2153
Alexey Frunze15958152017-02-09 19:08:30 -08002154 case Primitive::kPrimNot: {
2155 static_assert(
2156 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2157 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2158 // /* HeapReference<Object> */ out =
2159 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2160 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2161 Location temp = locations->GetTemp(0);
2162 // Note that a potential implicit null check is handled in this
2163 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2164 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2165 out_loc,
2166 obj,
2167 data_offset,
2168 index,
2169 temp,
2170 /* needs_null_check */ true);
2171 } else {
2172 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2173 if (index.IsConstant()) {
2174 size_t offset =
2175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2176 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2177 // If read barriers are enabled, emit read barriers other than
2178 // Baker's using a slow path (and also unpoison the loaded
2179 // reference, if heap poisoning is enabled).
2180 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2181 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002182 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002183 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2184 // If read barriers are enabled, emit read barriers other than
2185 // Baker's using a slow path (and also unpoison the loaded
2186 // reference, if heap poisoning is enabled).
2187 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2188 out_loc,
2189 out_loc,
2190 obj_loc,
2191 data_offset,
2192 index);
2193 }
2194 }
2195 break;
2196 }
2197
Alexey Frunze4dda3372015-06-01 18:31:49 -07002198 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002199 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 if (index.IsConstant()) {
2201 size_t offset =
2202 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002203 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002205 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002206 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 }
2208 break;
2209 }
2210
2211 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002212 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002213 if (index.IsConstant()) {
2214 size_t offset =
2215 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002216 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002217 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002218 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002219 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002220 }
2221 break;
2222 }
2223
2224 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002225 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002226 if (index.IsConstant()) {
2227 size_t offset =
2228 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002229 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002230 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002231 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002232 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002233 }
2234 break;
2235 }
2236
2237 case Primitive::kPrimVoid:
2238 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2239 UNREACHABLE();
2240 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241}
2242
2243void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2247}
2248
2249void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2250 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002251 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002252 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2253 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2254 __ LoadFromOffset(kLoadWord, out, obj, offset);
2255 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002256 // Mask out compression flag from String's array length.
2257 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2258 __ Srl(out, out, 1u);
2259 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002260}
2261
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002262Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2263 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2264 ? Location::ConstantLocation(instruction->AsConstant())
2265 : Location::RequiresRegister();
2266}
2267
2268Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2269 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2270 // We can store a non-zero float or double constant without first loading it into the FPU,
2271 // but we should only prefer this if the constant has a single use.
2272 if (instruction->IsConstant() &&
2273 (instruction->AsConstant()->IsZeroBitPattern() ||
2274 instruction->GetUses().HasExactlyOneElement())) {
2275 return Location::ConstantLocation(instruction->AsConstant());
2276 // Otherwise fall through and require an FPU register for the constant.
2277 }
2278 return Location::RequiresFpuRegister();
2279}
2280
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002282 Primitive::Type value_type = instruction->GetComponentType();
2283
2284 bool needs_write_barrier =
2285 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2286 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2287
Alexey Frunze4dda3372015-06-01 18:31:49 -07002288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2289 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002290 may_need_runtime_call_for_type_check ?
2291 LocationSummary::kCallOnSlowPath :
2292 LocationSummary::kNoCall);
2293
2294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2296 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2297 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002299 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2300 }
2301 if (needs_write_barrier) {
2302 // Temporary register for the write barrier.
2303 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002304 }
2305}
2306
2307void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2308 LocationSummary* locations = instruction->GetLocations();
2309 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2310 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002311 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002312 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002313 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002314 bool needs_write_barrier =
2315 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002316 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002317 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002318
2319 switch (value_type) {
2320 case Primitive::kPrimBoolean:
2321 case Primitive::kPrimByte: {
2322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002323 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002324 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002326 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2327 }
2328 if (value_location.IsConstant()) {
2329 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2330 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2331 } else {
2332 GpuRegister value = value_location.AsRegister<GpuRegister>();
2333 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002334 }
2335 break;
2336 }
2337
2338 case Primitive::kPrimShort:
2339 case Primitive::kPrimChar: {
2340 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002341 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002342 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002344 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002345 }
2346 if (value_location.IsConstant()) {
2347 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2348 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2349 } else {
2350 GpuRegister value = value_location.AsRegister<GpuRegister>();
2351 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002352 }
2353 break;
2354 }
2355
Alexey Frunze15958152017-02-09 19:08:30 -08002356 case Primitive::kPrimInt: {
2357 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2358 if (index.IsConstant()) {
2359 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2360 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002361 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002362 }
2363 if (value_location.IsConstant()) {
2364 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2365 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2366 } else {
2367 GpuRegister value = value_location.AsRegister<GpuRegister>();
2368 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2369 }
2370 break;
2371 }
2372
Alexey Frunze4dda3372015-06-01 18:31:49 -07002373 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002374 if (value_location.IsConstant()) {
2375 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002376 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002378 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002380 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002381 }
Alexey Frunze15958152017-02-09 19:08:30 -08002382 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2383 DCHECK_EQ(value, 0);
2384 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2385 DCHECK(!needs_write_barrier);
2386 DCHECK(!may_need_runtime_call_for_type_check);
2387 break;
2388 }
2389
2390 DCHECK(needs_write_barrier);
2391 GpuRegister value = value_location.AsRegister<GpuRegister>();
2392 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2393 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2394 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2395 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2396 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2397 Mips64Label done;
2398 SlowPathCodeMIPS64* slow_path = nullptr;
2399
2400 if (may_need_runtime_call_for_type_check) {
2401 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2402 codegen_->AddSlowPath(slow_path);
2403 if (instruction->GetValueCanBeNull()) {
2404 Mips64Label non_zero;
2405 __ Bnezc(value, &non_zero);
2406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2407 if (index.IsConstant()) {
2408 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002409 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002410 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002411 }
Alexey Frunze15958152017-02-09 19:08:30 -08002412 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2413 __ Bc(&done);
2414 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415 }
Alexey Frunze15958152017-02-09 19:08:30 -08002416
2417 // Note that when read barriers are enabled, the type checks
2418 // are performed without read barriers. This is fine, even in
2419 // the case where a class object is in the from-space after
2420 // the flip, as a comparison involving such a type would not
2421 // produce a false positive; it may of course produce a false
2422 // negative, in which case we would take the ArraySet slow
2423 // path.
2424
2425 // /* HeapReference<Class> */ temp1 = obj->klass_
2426 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2427 __ MaybeUnpoisonHeapReference(temp1);
2428
2429 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2430 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2431 // /* HeapReference<Class> */ temp2 = value->klass_
2432 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2433 // If heap poisoning is enabled, no need to unpoison `temp1`
2434 // nor `temp2`, as we are comparing two poisoned references.
2435
2436 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2437 Mips64Label do_put;
2438 __ Beqc(temp1, temp2, &do_put);
2439 // If heap poisoning is enabled, the `temp1` reference has
2440 // not been unpoisoned yet; unpoison it now.
2441 __ MaybeUnpoisonHeapReference(temp1);
2442
2443 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2444 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2445 // If heap poisoning is enabled, no need to unpoison
2446 // `temp1`, as we are comparing against null below.
2447 __ Bnezc(temp1, slow_path->GetEntryLabel());
2448 __ Bind(&do_put);
2449 } else {
2450 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2451 }
2452 }
2453
2454 GpuRegister source = value;
2455 if (kPoisonHeapReferences) {
2456 // Note that in the case where `value` is a null reference,
2457 // we do not enter this block, as a null reference does not
2458 // need poisoning.
2459 __ Move(temp1, value);
2460 __ PoisonHeapReference(temp1);
2461 source = temp1;
2462 }
2463
2464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2465 if (index.IsConstant()) {
2466 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002467 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002468 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002469 }
2470 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2471
2472 if (!may_need_runtime_call_for_type_check) {
2473 codegen_->MaybeRecordImplicitNullCheck(instruction);
2474 }
2475
2476 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2477
2478 if (done.IsLinked()) {
2479 __ Bind(&done);
2480 }
2481
2482 if (slow_path != nullptr) {
2483 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 }
2485 break;
2486 }
2487
2488 case Primitive::kPrimLong: {
2489 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002490 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002491 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002492 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002493 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002494 }
2495 if (value_location.IsConstant()) {
2496 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2497 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2498 } else {
2499 GpuRegister value = value_location.AsRegister<GpuRegister>();
2500 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002501 }
2502 break;
2503 }
2504
2505 case Primitive::kPrimFloat: {
2506 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002507 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002508 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002509 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002510 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002511 }
2512 if (value_location.IsConstant()) {
2513 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2514 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2515 } else {
2516 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2517 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 }
2519 break;
2520 }
2521
2522 case Primitive::kPrimDouble: {
2523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002525 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002526 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002527 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002528 }
2529 if (value_location.IsConstant()) {
2530 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2531 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2532 } else {
2533 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2534 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002535 }
2536 break;
2537 }
2538
2539 case Primitive::kPrimVoid:
2540 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2541 UNREACHABLE();
2542 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002543}
2544
2545void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002546 RegisterSet caller_saves = RegisterSet::Empty();
2547 InvokeRuntimeCallingConvention calling_convention;
2548 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2549 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2550 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 locations->SetInAt(0, Location::RequiresRegister());
2552 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002553}
2554
2555void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2556 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002557 BoundsCheckSlowPathMIPS64* slow_path =
2558 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002559 codegen_->AddSlowPath(slow_path);
2560
2561 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2562 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2563
2564 // length is limited by the maximum positive signed 32-bit integer.
2565 // Unsigned comparison of length and index checks for index < 0
2566 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002567 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002568}
2569
Alexey Frunze15958152017-02-09 19:08:30 -08002570// Temp is used for read barrier.
2571static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2572 if (kEmitCompilerReadBarrier &&
2573 (kUseBakerReadBarrier ||
2574 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2575 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2576 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2577 return 1;
2578 }
2579 return 0;
2580}
2581
2582// Extra temp is used for read barrier.
2583static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2584 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2585}
2586
Alexey Frunze4dda3372015-06-01 18:31:49 -07002587void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002588 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2589 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2590
2591 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2592 switch (type_check_kind) {
2593 case TypeCheckKind::kExactCheck:
2594 case TypeCheckKind::kAbstractClassCheck:
2595 case TypeCheckKind::kClassHierarchyCheck:
2596 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002597 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002598 ? LocationSummary::kCallOnSlowPath
2599 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2600 break;
2601 case TypeCheckKind::kArrayCheck:
2602 case TypeCheckKind::kUnresolvedCheck:
2603 case TypeCheckKind::kInterfaceCheck:
2604 call_kind = LocationSummary::kCallOnSlowPath;
2605 break;
2606 }
2607
2608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609 locations->SetInAt(0, Location::RequiresRegister());
2610 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002611 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002612}
2613
2614void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002615 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002617 Location obj_loc = locations->InAt(0);
2618 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002619 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002620 Location temp_loc = locations->GetTemp(0);
2621 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2622 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2623 DCHECK_LE(num_temps, 2u);
2624 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002625 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2626 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2627 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2628 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2629 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2630 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2631 const uint32_t object_array_data_offset =
2632 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2633 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002635 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2636 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2637 // read barriers is done for performance and code size reasons.
2638 bool is_type_check_slow_path_fatal = false;
2639 if (!kEmitCompilerReadBarrier) {
2640 is_type_check_slow_path_fatal =
2641 (type_check_kind == TypeCheckKind::kExactCheck ||
2642 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2643 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2644 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2645 !instruction->CanThrowIntoCatchBlock();
2646 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002647 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002648 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2649 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002650 codegen_->AddSlowPath(slow_path);
2651
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002652 // Avoid this check if we know `obj` is not null.
2653 if (instruction->MustDoNullCheck()) {
2654 __ Beqzc(obj, &done);
2655 }
2656
2657 switch (type_check_kind) {
2658 case TypeCheckKind::kExactCheck:
2659 case TypeCheckKind::kArrayCheck: {
2660 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002661 GenerateReferenceLoadTwoRegisters(instruction,
2662 temp_loc,
2663 obj_loc,
2664 class_offset,
2665 maybe_temp2_loc,
2666 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002667 // Jump to slow path for throwing the exception or doing a
2668 // more involved array check.
2669 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2670 break;
2671 }
2672
2673 case TypeCheckKind::kAbstractClassCheck: {
2674 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002675 GenerateReferenceLoadTwoRegisters(instruction,
2676 temp_loc,
2677 obj_loc,
2678 class_offset,
2679 maybe_temp2_loc,
2680 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002681 // If the class is abstract, we eagerly fetch the super class of the
2682 // object to avoid doing a comparison we know will fail.
2683 Mips64Label loop;
2684 __ Bind(&loop);
2685 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002686 GenerateReferenceLoadOneRegister(instruction,
2687 temp_loc,
2688 super_offset,
2689 maybe_temp2_loc,
2690 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002691 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2692 // exception.
2693 __ Beqzc(temp, slow_path->GetEntryLabel());
2694 // Otherwise, compare the classes.
2695 __ Bnec(temp, cls, &loop);
2696 break;
2697 }
2698
2699 case TypeCheckKind::kClassHierarchyCheck: {
2700 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002701 GenerateReferenceLoadTwoRegisters(instruction,
2702 temp_loc,
2703 obj_loc,
2704 class_offset,
2705 maybe_temp2_loc,
2706 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002707 // Walk over the class hierarchy to find a match.
2708 Mips64Label loop;
2709 __ Bind(&loop);
2710 __ Beqc(temp, cls, &done);
2711 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002712 GenerateReferenceLoadOneRegister(instruction,
2713 temp_loc,
2714 super_offset,
2715 maybe_temp2_loc,
2716 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002717 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2718 // exception. Otherwise, jump to the beginning of the loop.
2719 __ Bnezc(temp, &loop);
2720 __ Bc(slow_path->GetEntryLabel());
2721 break;
2722 }
2723
2724 case TypeCheckKind::kArrayObjectCheck: {
2725 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002726 GenerateReferenceLoadTwoRegisters(instruction,
2727 temp_loc,
2728 obj_loc,
2729 class_offset,
2730 maybe_temp2_loc,
2731 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002732 // Do an exact check.
2733 __ Beqc(temp, cls, &done);
2734 // Otherwise, we need to check that the object's class is a non-primitive array.
2735 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002736 GenerateReferenceLoadOneRegister(instruction,
2737 temp_loc,
2738 component_offset,
2739 maybe_temp2_loc,
2740 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002741 // If the component type is null, jump to the slow path to throw the exception.
2742 __ Beqzc(temp, slow_path->GetEntryLabel());
2743 // Otherwise, the object is indeed an array, further check that this component
2744 // type is not a primitive type.
2745 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2746 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2747 __ Bnezc(temp, slow_path->GetEntryLabel());
2748 break;
2749 }
2750
2751 case TypeCheckKind::kUnresolvedCheck:
2752 // We always go into the type check slow path for the unresolved check case.
2753 // We cannot directly call the CheckCast runtime entry point
2754 // without resorting to a type checking slow path here (i.e. by
2755 // calling InvokeRuntime directly), as it would require to
2756 // assign fixed registers for the inputs of this HInstanceOf
2757 // instruction (following the runtime calling convention), which
2758 // might be cluttered by the potential first read barrier
2759 // emission at the beginning of this method.
2760 __ Bc(slow_path->GetEntryLabel());
2761 break;
2762
2763 case TypeCheckKind::kInterfaceCheck: {
2764 // Avoid read barriers to improve performance of the fast path. We can not get false
2765 // positives by doing this.
2766 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002767 GenerateReferenceLoadTwoRegisters(instruction,
2768 temp_loc,
2769 obj_loc,
2770 class_offset,
2771 maybe_temp2_loc,
2772 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002773 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002774 GenerateReferenceLoadTwoRegisters(instruction,
2775 temp_loc,
2776 temp_loc,
2777 iftable_offset,
2778 maybe_temp2_loc,
2779 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002780 // Iftable is never null.
2781 __ Lw(TMP, temp, array_length_offset);
2782 // Loop through the iftable and check if any class matches.
2783 Mips64Label loop;
2784 __ Bind(&loop);
2785 __ Beqzc(TMP, slow_path->GetEntryLabel());
2786 __ Lwu(AT, temp, object_array_data_offset);
2787 __ MaybeUnpoisonHeapReference(AT);
2788 // Go to next interface.
2789 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2790 __ Addiu(TMP, TMP, -2);
2791 // Compare the classes and continue the loop if they do not match.
2792 __ Bnec(AT, cls, &loop);
2793 break;
2794 }
2795 }
2796
2797 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002798 __ Bind(slow_path->GetExitLabel());
2799}
2800
2801void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2802 LocationSummary* locations =
2803 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2804 locations->SetInAt(0, Location::RequiresRegister());
2805 if (check->HasUses()) {
2806 locations->SetOut(Location::SameAsFirstInput());
2807 }
2808}
2809
2810void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2811 // We assume the class is not null.
2812 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2813 check->GetLoadClass(),
2814 check,
2815 check->GetDexPc(),
2816 true);
2817 codegen_->AddSlowPath(slow_path);
2818 GenerateClassInitializationCheck(slow_path,
2819 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2820}
2821
2822void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2823 Primitive::Type in_type = compare->InputAt(0)->GetType();
2824
Alexey Frunze299a9392015-12-08 16:08:02 -08002825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002826
2827 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002828 case Primitive::kPrimBoolean:
2829 case Primitive::kPrimByte:
2830 case Primitive::kPrimShort:
2831 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002832 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002833 case Primitive::kPrimLong:
2834 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002835 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2837 break;
2838
2839 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002840 case Primitive::kPrimDouble:
2841 locations->SetInAt(0, Location::RequiresFpuRegister());
2842 locations->SetInAt(1, Location::RequiresFpuRegister());
2843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002844 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845
2846 default:
2847 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2848 }
2849}
2850
2851void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2852 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002853 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002854 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2855
2856 // 0 if: left == right
2857 // 1 if: left > right
2858 // -1 if: left < right
2859 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002860 case Primitive::kPrimBoolean:
2861 case Primitive::kPrimByte:
2862 case Primitive::kPrimShort:
2863 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002864 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002865 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002866 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002867 Location rhs_location = locations->InAt(1);
2868 bool use_imm = rhs_location.IsConstant();
2869 GpuRegister rhs = ZERO;
2870 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002871 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002872 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2873 if (value != 0) {
2874 rhs = AT;
2875 __ LoadConst64(rhs, value);
2876 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002877 } else {
2878 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2879 if (value != 0) {
2880 rhs = AT;
2881 __ LoadConst32(rhs, value);
2882 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002883 }
2884 } else {
2885 rhs = rhs_location.AsRegister<GpuRegister>();
2886 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002887 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002888 __ Slt(res, rhs, lhs);
2889 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002890 break;
2891 }
2892
Alexey Frunze299a9392015-12-08 16:08:02 -08002893 case Primitive::kPrimFloat: {
2894 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2895 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2896 Mips64Label done;
2897 __ CmpEqS(FTMP, lhs, rhs);
2898 __ LoadConst32(res, 0);
2899 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002900 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002901 __ CmpLtS(FTMP, lhs, rhs);
2902 __ LoadConst32(res, -1);
2903 __ Bc1nez(FTMP, &done);
2904 __ LoadConst32(res, 1);
2905 } else {
2906 __ CmpLtS(FTMP, rhs, lhs);
2907 __ LoadConst32(res, 1);
2908 __ Bc1nez(FTMP, &done);
2909 __ LoadConst32(res, -1);
2910 }
2911 __ Bind(&done);
2912 break;
2913 }
2914
Alexey Frunze4dda3372015-06-01 18:31:49 -07002915 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002916 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2917 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2918 Mips64Label done;
2919 __ CmpEqD(FTMP, lhs, rhs);
2920 __ LoadConst32(res, 0);
2921 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002922 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002923 __ CmpLtD(FTMP, lhs, rhs);
2924 __ LoadConst32(res, -1);
2925 __ Bc1nez(FTMP, &done);
2926 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002927 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002928 __ CmpLtD(FTMP, rhs, lhs);
2929 __ LoadConst32(res, 1);
2930 __ Bc1nez(FTMP, &done);
2931 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002932 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002933 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002934 break;
2935 }
2936
2937 default:
2938 LOG(FATAL) << "Unimplemented compare type " << in_type;
2939 }
2940}
2941
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002942void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002944 switch (instruction->InputAt(0)->GetType()) {
2945 default:
2946 case Primitive::kPrimLong:
2947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2949 break;
2950
2951 case Primitive::kPrimFloat:
2952 case Primitive::kPrimDouble:
2953 locations->SetInAt(0, Location::RequiresFpuRegister());
2954 locations->SetInAt(1, Location::RequiresFpuRegister());
2955 break;
2956 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002957 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2959 }
2960}
2961
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002962void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002963 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002964 return;
2965 }
2966
Alexey Frunze299a9392015-12-08 16:08:02 -08002967 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002968 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002969 switch (type) {
2970 default:
2971 // Integer case.
2972 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2973 return;
2974 case Primitive::kPrimLong:
2975 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2976 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002977 case Primitive::kPrimFloat:
2978 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002979 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2980 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002981 }
2982}
2983
Alexey Frunzec857c742015-09-23 15:12:39 -07002984void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2985 DCHECK(instruction->IsDiv() || instruction->IsRem());
2986 Primitive::Type type = instruction->GetResultType();
2987
2988 LocationSummary* locations = instruction->GetLocations();
2989 Location second = locations->InAt(1);
2990 DCHECK(second.IsConstant());
2991
2992 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2993 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2994 int64_t imm = Int64FromConstant(second.GetConstant());
2995 DCHECK(imm == 1 || imm == -1);
2996
2997 if (instruction->IsRem()) {
2998 __ Move(out, ZERO);
2999 } else {
3000 if (imm == -1) {
3001 if (type == Primitive::kPrimInt) {
3002 __ Subu(out, ZERO, dividend);
3003 } else {
3004 DCHECK_EQ(type, Primitive::kPrimLong);
3005 __ Dsubu(out, ZERO, dividend);
3006 }
3007 } else if (out != dividend) {
3008 __ Move(out, dividend);
3009 }
3010 }
3011}
3012
3013void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3014 DCHECK(instruction->IsDiv() || instruction->IsRem());
3015 Primitive::Type type = instruction->GetResultType();
3016
3017 LocationSummary* locations = instruction->GetLocations();
3018 Location second = locations->InAt(1);
3019 DCHECK(second.IsConstant());
3020
3021 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3022 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3023 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003024 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003025 int ctz_imm = CTZ(abs_imm);
3026
3027 if (instruction->IsDiv()) {
3028 if (type == Primitive::kPrimInt) {
3029 if (ctz_imm == 1) {
3030 // Fast path for division by +/-2, which is very common.
3031 __ Srl(TMP, dividend, 31);
3032 } else {
3033 __ Sra(TMP, dividend, 31);
3034 __ Srl(TMP, TMP, 32 - ctz_imm);
3035 }
3036 __ Addu(out, dividend, TMP);
3037 __ Sra(out, out, ctz_imm);
3038 if (imm < 0) {
3039 __ Subu(out, ZERO, out);
3040 }
3041 } else {
3042 DCHECK_EQ(type, Primitive::kPrimLong);
3043 if (ctz_imm == 1) {
3044 // Fast path for division by +/-2, which is very common.
3045 __ Dsrl32(TMP, dividend, 31);
3046 } else {
3047 __ Dsra32(TMP, dividend, 31);
3048 if (ctz_imm > 32) {
3049 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3050 } else {
3051 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3052 }
3053 }
3054 __ Daddu(out, dividend, TMP);
3055 if (ctz_imm < 32) {
3056 __ Dsra(out, out, ctz_imm);
3057 } else {
3058 __ Dsra32(out, out, ctz_imm - 32);
3059 }
3060 if (imm < 0) {
3061 __ Dsubu(out, ZERO, out);
3062 }
3063 }
3064 } else {
3065 if (type == Primitive::kPrimInt) {
3066 if (ctz_imm == 1) {
3067 // Fast path for modulo +/-2, which is very common.
3068 __ Sra(TMP, dividend, 31);
3069 __ Subu(out, dividend, TMP);
3070 __ Andi(out, out, 1);
3071 __ Addu(out, out, TMP);
3072 } else {
3073 __ Sra(TMP, dividend, 31);
3074 __ Srl(TMP, TMP, 32 - ctz_imm);
3075 __ Addu(out, dividend, TMP);
3076 if (IsUint<16>(abs_imm - 1)) {
3077 __ Andi(out, out, abs_imm - 1);
3078 } else {
3079 __ Sll(out, out, 32 - ctz_imm);
3080 __ Srl(out, out, 32 - ctz_imm);
3081 }
3082 __ Subu(out, out, TMP);
3083 }
3084 } else {
3085 DCHECK_EQ(type, Primitive::kPrimLong);
3086 if (ctz_imm == 1) {
3087 // Fast path for modulo +/-2, which is very common.
3088 __ Dsra32(TMP, dividend, 31);
3089 __ Dsubu(out, dividend, TMP);
3090 __ Andi(out, out, 1);
3091 __ Daddu(out, out, TMP);
3092 } else {
3093 __ Dsra32(TMP, dividend, 31);
3094 if (ctz_imm > 32) {
3095 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3096 } else {
3097 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3098 }
3099 __ Daddu(out, dividend, TMP);
3100 if (IsUint<16>(abs_imm - 1)) {
3101 __ Andi(out, out, abs_imm - 1);
3102 } else {
3103 if (ctz_imm > 32) {
3104 __ Dsll(out, out, 64 - ctz_imm);
3105 __ Dsrl(out, out, 64 - ctz_imm);
3106 } else {
3107 __ Dsll32(out, out, 32 - ctz_imm);
3108 __ Dsrl32(out, out, 32 - ctz_imm);
3109 }
3110 }
3111 __ Dsubu(out, out, TMP);
3112 }
3113 }
3114 }
3115}
3116
3117void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3118 DCHECK(instruction->IsDiv() || instruction->IsRem());
3119
3120 LocationSummary* locations = instruction->GetLocations();
3121 Location second = locations->InAt(1);
3122 DCHECK(second.IsConstant());
3123
3124 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3125 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3126 int64_t imm = Int64FromConstant(second.GetConstant());
3127
3128 Primitive::Type type = instruction->GetResultType();
3129 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3130
3131 int64_t magic;
3132 int shift;
3133 CalculateMagicAndShiftForDivRem(imm,
3134 (type == Primitive::kPrimLong),
3135 &magic,
3136 &shift);
3137
3138 if (type == Primitive::kPrimInt) {
3139 __ LoadConst32(TMP, magic);
3140 __ MuhR6(TMP, dividend, TMP);
3141
3142 if (imm > 0 && magic < 0) {
3143 __ Addu(TMP, TMP, dividend);
3144 } else if (imm < 0 && magic > 0) {
3145 __ Subu(TMP, TMP, dividend);
3146 }
3147
3148 if (shift != 0) {
3149 __ Sra(TMP, TMP, shift);
3150 }
3151
3152 if (instruction->IsDiv()) {
3153 __ Sra(out, TMP, 31);
3154 __ Subu(out, TMP, out);
3155 } else {
3156 __ Sra(AT, TMP, 31);
3157 __ Subu(AT, TMP, AT);
3158 __ LoadConst32(TMP, imm);
3159 __ MulR6(TMP, AT, TMP);
3160 __ Subu(out, dividend, TMP);
3161 }
3162 } else {
3163 __ LoadConst64(TMP, magic);
3164 __ Dmuh(TMP, dividend, TMP);
3165
3166 if (imm > 0 && magic < 0) {
3167 __ Daddu(TMP, TMP, dividend);
3168 } else if (imm < 0 && magic > 0) {
3169 __ Dsubu(TMP, TMP, dividend);
3170 }
3171
3172 if (shift >= 32) {
3173 __ Dsra32(TMP, TMP, shift - 32);
3174 } else if (shift > 0) {
3175 __ Dsra(TMP, TMP, shift);
3176 }
3177
3178 if (instruction->IsDiv()) {
3179 __ Dsra32(out, TMP, 31);
3180 __ Dsubu(out, TMP, out);
3181 } else {
3182 __ Dsra32(AT, TMP, 31);
3183 __ Dsubu(AT, TMP, AT);
3184 __ LoadConst64(TMP, imm);
3185 __ Dmul(TMP, AT, TMP);
3186 __ Dsubu(out, dividend, TMP);
3187 }
3188 }
3189}
3190
3191void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3192 DCHECK(instruction->IsDiv() || instruction->IsRem());
3193 Primitive::Type type = instruction->GetResultType();
3194 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3195
3196 LocationSummary* locations = instruction->GetLocations();
3197 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3198 Location second = locations->InAt(1);
3199
3200 if (second.IsConstant()) {
3201 int64_t imm = Int64FromConstant(second.GetConstant());
3202 if (imm == 0) {
3203 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3204 } else if (imm == 1 || imm == -1) {
3205 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003206 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003207 DivRemByPowerOfTwo(instruction);
3208 } else {
3209 DCHECK(imm <= -2 || imm >= 2);
3210 GenerateDivRemWithAnyConstant(instruction);
3211 }
3212 } else {
3213 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3214 GpuRegister divisor = second.AsRegister<GpuRegister>();
3215 if (instruction->IsDiv()) {
3216 if (type == Primitive::kPrimInt)
3217 __ DivR6(out, dividend, divisor);
3218 else
3219 __ Ddiv(out, dividend, divisor);
3220 } else {
3221 if (type == Primitive::kPrimInt)
3222 __ ModR6(out, dividend, divisor);
3223 else
3224 __ Dmod(out, dividend, divisor);
3225 }
3226 }
3227}
3228
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3232 switch (div->GetResultType()) {
3233 case Primitive::kPrimInt:
3234 case Primitive::kPrimLong:
3235 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003236 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3238 break;
3239
3240 case Primitive::kPrimFloat:
3241 case Primitive::kPrimDouble:
3242 locations->SetInAt(0, Location::RequiresFpuRegister());
3243 locations->SetInAt(1, Location::RequiresFpuRegister());
3244 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3245 break;
3246
3247 default:
3248 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3249 }
3250}
3251
3252void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3253 Primitive::Type type = instruction->GetType();
3254 LocationSummary* locations = instruction->GetLocations();
3255
3256 switch (type) {
3257 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003258 case Primitive::kPrimLong:
3259 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003260 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261 case Primitive::kPrimFloat:
3262 case Primitive::kPrimDouble: {
3263 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3264 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3265 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3266 if (type == Primitive::kPrimFloat)
3267 __ DivS(dst, lhs, rhs);
3268 else
3269 __ DivD(dst, lhs, rhs);
3270 break;
3271 }
3272 default:
3273 LOG(FATAL) << "Unexpected div type " << type;
3274 }
3275}
3276
3277void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003278 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003279 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003280}
3281
3282void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3283 SlowPathCodeMIPS64* slow_path =
3284 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3285 codegen_->AddSlowPath(slow_path);
3286 Location value = instruction->GetLocations()->InAt(0);
3287
3288 Primitive::Type type = instruction->GetType();
3289
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003290 if (!Primitive::IsIntegralType(type)) {
3291 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003292 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003293 }
3294
3295 if (value.IsConstant()) {
3296 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3297 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003298 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003299 } else {
3300 // A division by a non-null constant is valid. We don't need to perform
3301 // any check, so simply fall through.
3302 }
3303 } else {
3304 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3305 }
3306}
3307
3308void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3309 LocationSummary* locations =
3310 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3311 locations->SetOut(Location::ConstantLocation(constant));
3312}
3313
3314void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3315 // Will be generated at use site.
3316}
3317
3318void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3319 exit->SetLocations(nullptr);
3320}
3321
3322void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3323}
3324
3325void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3326 LocationSummary* locations =
3327 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3328 locations->SetOut(Location::ConstantLocation(constant));
3329}
3330
3331void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3332 // Will be generated at use site.
3333}
3334
David Brazdilfc6a86a2015-06-26 10:33:45 +00003335void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003336 DCHECK(!successor->IsExitBlock());
3337 HBasicBlock* block = got->GetBlock();
3338 HInstruction* previous = got->GetPrevious();
3339 HLoopInformation* info = block->GetLoopInformation();
3340
3341 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3342 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3343 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3344 return;
3345 }
3346 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3347 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3348 }
3349 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003350 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003351 }
3352}
3353
David Brazdilfc6a86a2015-06-26 10:33:45 +00003354void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3355 got->SetLocations(nullptr);
3356}
3357
3358void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3359 HandleGoto(got, got->GetSuccessor());
3360}
3361
3362void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3363 try_boundary->SetLocations(nullptr);
3364}
3365
3366void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3367 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3368 if (!successor->IsExitBlock()) {
3369 HandleGoto(try_boundary, successor);
3370 }
3371}
3372
Alexey Frunze299a9392015-12-08 16:08:02 -08003373void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3374 bool is64bit,
3375 LocationSummary* locations) {
3376 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3377 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3378 Location rhs_location = locations->InAt(1);
3379 GpuRegister rhs_reg = ZERO;
3380 int64_t rhs_imm = 0;
3381 bool use_imm = rhs_location.IsConstant();
3382 if (use_imm) {
3383 if (is64bit) {
3384 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3385 } else {
3386 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3387 }
3388 } else {
3389 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3390 }
3391 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3392
3393 switch (cond) {
3394 case kCondEQ:
3395 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003396 if (use_imm && IsInt<16>(-rhs_imm)) {
3397 if (rhs_imm == 0) {
3398 if (cond == kCondEQ) {
3399 __ Sltiu(dst, lhs, 1);
3400 } else {
3401 __ Sltu(dst, ZERO, lhs);
3402 }
3403 } else {
3404 if (is64bit) {
3405 __ Daddiu(dst, lhs, -rhs_imm);
3406 } else {
3407 __ Addiu(dst, lhs, -rhs_imm);
3408 }
3409 if (cond == kCondEQ) {
3410 __ Sltiu(dst, dst, 1);
3411 } else {
3412 __ Sltu(dst, ZERO, dst);
3413 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003414 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003415 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003416 if (use_imm && IsUint<16>(rhs_imm)) {
3417 __ Xori(dst, lhs, rhs_imm);
3418 } else {
3419 if (use_imm) {
3420 rhs_reg = TMP;
3421 __ LoadConst64(rhs_reg, rhs_imm);
3422 }
3423 __ Xor(dst, lhs, rhs_reg);
3424 }
3425 if (cond == kCondEQ) {
3426 __ Sltiu(dst, dst, 1);
3427 } else {
3428 __ Sltu(dst, ZERO, dst);
3429 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003430 }
3431 break;
3432
3433 case kCondLT:
3434 case kCondGE:
3435 if (use_imm && IsInt<16>(rhs_imm)) {
3436 __ Slti(dst, lhs, rhs_imm);
3437 } else {
3438 if (use_imm) {
3439 rhs_reg = TMP;
3440 __ LoadConst64(rhs_reg, rhs_imm);
3441 }
3442 __ Slt(dst, lhs, rhs_reg);
3443 }
3444 if (cond == kCondGE) {
3445 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3446 // only the slt instruction but no sge.
3447 __ Xori(dst, dst, 1);
3448 }
3449 break;
3450
3451 case kCondLE:
3452 case kCondGT:
3453 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3454 // Simulate lhs <= rhs via lhs < rhs + 1.
3455 __ Slti(dst, lhs, rhs_imm_plus_one);
3456 if (cond == kCondGT) {
3457 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3458 // only the slti instruction but no sgti.
3459 __ Xori(dst, dst, 1);
3460 }
3461 } else {
3462 if (use_imm) {
3463 rhs_reg = TMP;
3464 __ LoadConst64(rhs_reg, rhs_imm);
3465 }
3466 __ Slt(dst, rhs_reg, lhs);
3467 if (cond == kCondLE) {
3468 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3469 // only the slt instruction but no sle.
3470 __ Xori(dst, dst, 1);
3471 }
3472 }
3473 break;
3474
3475 case kCondB:
3476 case kCondAE:
3477 if (use_imm && IsInt<16>(rhs_imm)) {
3478 // Sltiu sign-extends its 16-bit immediate operand before
3479 // the comparison and thus lets us compare directly with
3480 // unsigned values in the ranges [0, 0x7fff] and
3481 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3482 __ Sltiu(dst, lhs, rhs_imm);
3483 } else {
3484 if (use_imm) {
3485 rhs_reg = TMP;
3486 __ LoadConst64(rhs_reg, rhs_imm);
3487 }
3488 __ Sltu(dst, lhs, rhs_reg);
3489 }
3490 if (cond == kCondAE) {
3491 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3492 // only the sltu instruction but no sgeu.
3493 __ Xori(dst, dst, 1);
3494 }
3495 break;
3496
3497 case kCondBE:
3498 case kCondA:
3499 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3500 // Simulate lhs <= rhs via lhs < rhs + 1.
3501 // Note that this only works if rhs + 1 does not overflow
3502 // to 0, hence the check above.
3503 // Sltiu sign-extends its 16-bit immediate operand before
3504 // the comparison and thus lets us compare directly with
3505 // unsigned values in the ranges [0, 0x7fff] and
3506 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3507 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3508 if (cond == kCondA) {
3509 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3510 // only the sltiu instruction but no sgtiu.
3511 __ Xori(dst, dst, 1);
3512 }
3513 } else {
3514 if (use_imm) {
3515 rhs_reg = TMP;
3516 __ LoadConst64(rhs_reg, rhs_imm);
3517 }
3518 __ Sltu(dst, rhs_reg, lhs);
3519 if (cond == kCondBE) {
3520 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3521 // only the sltu instruction but no sleu.
3522 __ Xori(dst, dst, 1);
3523 }
3524 }
3525 break;
3526 }
3527}
3528
3529void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3530 bool is64bit,
3531 LocationSummary* locations,
3532 Mips64Label* label) {
3533 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3534 Location rhs_location = locations->InAt(1);
3535 GpuRegister rhs_reg = ZERO;
3536 int64_t rhs_imm = 0;
3537 bool use_imm = rhs_location.IsConstant();
3538 if (use_imm) {
3539 if (is64bit) {
3540 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3541 } else {
3542 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3543 }
3544 } else {
3545 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3546 }
3547
3548 if (use_imm && rhs_imm == 0) {
3549 switch (cond) {
3550 case kCondEQ:
3551 case kCondBE: // <= 0 if zero
3552 __ Beqzc(lhs, label);
3553 break;
3554 case kCondNE:
3555 case kCondA: // > 0 if non-zero
3556 __ Bnezc(lhs, label);
3557 break;
3558 case kCondLT:
3559 __ Bltzc(lhs, label);
3560 break;
3561 case kCondGE:
3562 __ Bgezc(lhs, label);
3563 break;
3564 case kCondLE:
3565 __ Blezc(lhs, label);
3566 break;
3567 case kCondGT:
3568 __ Bgtzc(lhs, label);
3569 break;
3570 case kCondB: // always false
3571 break;
3572 case kCondAE: // always true
3573 __ Bc(label);
3574 break;
3575 }
3576 } else {
3577 if (use_imm) {
3578 rhs_reg = TMP;
3579 __ LoadConst64(rhs_reg, rhs_imm);
3580 }
3581 switch (cond) {
3582 case kCondEQ:
3583 __ Beqc(lhs, rhs_reg, label);
3584 break;
3585 case kCondNE:
3586 __ Bnec(lhs, rhs_reg, label);
3587 break;
3588 case kCondLT:
3589 __ Bltc(lhs, rhs_reg, label);
3590 break;
3591 case kCondGE:
3592 __ Bgec(lhs, rhs_reg, label);
3593 break;
3594 case kCondLE:
3595 __ Bgec(rhs_reg, lhs, label);
3596 break;
3597 case kCondGT:
3598 __ Bltc(rhs_reg, lhs, label);
3599 break;
3600 case kCondB:
3601 __ Bltuc(lhs, rhs_reg, label);
3602 break;
3603 case kCondAE:
3604 __ Bgeuc(lhs, rhs_reg, label);
3605 break;
3606 case kCondBE:
3607 __ Bgeuc(rhs_reg, lhs, label);
3608 break;
3609 case kCondA:
3610 __ Bltuc(rhs_reg, lhs, label);
3611 break;
3612 }
3613 }
3614}
3615
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003616void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3617 bool gt_bias,
3618 Primitive::Type type,
3619 LocationSummary* locations) {
3620 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3621 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3622 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3623 if (type == Primitive::kPrimFloat) {
3624 switch (cond) {
3625 case kCondEQ:
3626 __ CmpEqS(FTMP, lhs, rhs);
3627 __ Mfc1(dst, FTMP);
3628 __ Andi(dst, dst, 1);
3629 break;
3630 case kCondNE:
3631 __ CmpEqS(FTMP, lhs, rhs);
3632 __ Mfc1(dst, FTMP);
3633 __ Addiu(dst, dst, 1);
3634 break;
3635 case kCondLT:
3636 if (gt_bias) {
3637 __ CmpLtS(FTMP, lhs, rhs);
3638 } else {
3639 __ CmpUltS(FTMP, lhs, rhs);
3640 }
3641 __ Mfc1(dst, FTMP);
3642 __ Andi(dst, dst, 1);
3643 break;
3644 case kCondLE:
3645 if (gt_bias) {
3646 __ CmpLeS(FTMP, lhs, rhs);
3647 } else {
3648 __ CmpUleS(FTMP, lhs, rhs);
3649 }
3650 __ Mfc1(dst, FTMP);
3651 __ Andi(dst, dst, 1);
3652 break;
3653 case kCondGT:
3654 if (gt_bias) {
3655 __ CmpUltS(FTMP, rhs, lhs);
3656 } else {
3657 __ CmpLtS(FTMP, rhs, lhs);
3658 }
3659 __ Mfc1(dst, FTMP);
3660 __ Andi(dst, dst, 1);
3661 break;
3662 case kCondGE:
3663 if (gt_bias) {
3664 __ CmpUleS(FTMP, rhs, lhs);
3665 } else {
3666 __ CmpLeS(FTMP, rhs, lhs);
3667 }
3668 __ Mfc1(dst, FTMP);
3669 __ Andi(dst, dst, 1);
3670 break;
3671 default:
3672 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3673 UNREACHABLE();
3674 }
3675 } else {
3676 DCHECK_EQ(type, Primitive::kPrimDouble);
3677 switch (cond) {
3678 case kCondEQ:
3679 __ CmpEqD(FTMP, lhs, rhs);
3680 __ Mfc1(dst, FTMP);
3681 __ Andi(dst, dst, 1);
3682 break;
3683 case kCondNE:
3684 __ CmpEqD(FTMP, lhs, rhs);
3685 __ Mfc1(dst, FTMP);
3686 __ Addiu(dst, dst, 1);
3687 break;
3688 case kCondLT:
3689 if (gt_bias) {
3690 __ CmpLtD(FTMP, lhs, rhs);
3691 } else {
3692 __ CmpUltD(FTMP, lhs, rhs);
3693 }
3694 __ Mfc1(dst, FTMP);
3695 __ Andi(dst, dst, 1);
3696 break;
3697 case kCondLE:
3698 if (gt_bias) {
3699 __ CmpLeD(FTMP, lhs, rhs);
3700 } else {
3701 __ CmpUleD(FTMP, lhs, rhs);
3702 }
3703 __ Mfc1(dst, FTMP);
3704 __ Andi(dst, dst, 1);
3705 break;
3706 case kCondGT:
3707 if (gt_bias) {
3708 __ CmpUltD(FTMP, rhs, lhs);
3709 } else {
3710 __ CmpLtD(FTMP, rhs, lhs);
3711 }
3712 __ Mfc1(dst, FTMP);
3713 __ Andi(dst, dst, 1);
3714 break;
3715 case kCondGE:
3716 if (gt_bias) {
3717 __ CmpUleD(FTMP, rhs, lhs);
3718 } else {
3719 __ CmpLeD(FTMP, rhs, lhs);
3720 }
3721 __ Mfc1(dst, FTMP);
3722 __ Andi(dst, dst, 1);
3723 break;
3724 default:
3725 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3726 UNREACHABLE();
3727 }
3728 }
3729}
3730
Alexey Frunze299a9392015-12-08 16:08:02 -08003731void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3732 bool gt_bias,
3733 Primitive::Type type,
3734 LocationSummary* locations,
3735 Mips64Label* label) {
3736 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3737 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3738 if (type == Primitive::kPrimFloat) {
3739 switch (cond) {
3740 case kCondEQ:
3741 __ CmpEqS(FTMP, lhs, rhs);
3742 __ Bc1nez(FTMP, label);
3743 break;
3744 case kCondNE:
3745 __ CmpEqS(FTMP, lhs, rhs);
3746 __ Bc1eqz(FTMP, label);
3747 break;
3748 case kCondLT:
3749 if (gt_bias) {
3750 __ CmpLtS(FTMP, lhs, rhs);
3751 } else {
3752 __ CmpUltS(FTMP, lhs, rhs);
3753 }
3754 __ Bc1nez(FTMP, label);
3755 break;
3756 case kCondLE:
3757 if (gt_bias) {
3758 __ CmpLeS(FTMP, lhs, rhs);
3759 } else {
3760 __ CmpUleS(FTMP, lhs, rhs);
3761 }
3762 __ Bc1nez(FTMP, label);
3763 break;
3764 case kCondGT:
3765 if (gt_bias) {
3766 __ CmpUltS(FTMP, rhs, lhs);
3767 } else {
3768 __ CmpLtS(FTMP, rhs, lhs);
3769 }
3770 __ Bc1nez(FTMP, label);
3771 break;
3772 case kCondGE:
3773 if (gt_bias) {
3774 __ CmpUleS(FTMP, rhs, lhs);
3775 } else {
3776 __ CmpLeS(FTMP, rhs, lhs);
3777 }
3778 __ Bc1nez(FTMP, label);
3779 break;
3780 default:
3781 LOG(FATAL) << "Unexpected non-floating-point condition";
3782 }
3783 } else {
3784 DCHECK_EQ(type, Primitive::kPrimDouble);
3785 switch (cond) {
3786 case kCondEQ:
3787 __ CmpEqD(FTMP, lhs, rhs);
3788 __ Bc1nez(FTMP, label);
3789 break;
3790 case kCondNE:
3791 __ CmpEqD(FTMP, lhs, rhs);
3792 __ Bc1eqz(FTMP, label);
3793 break;
3794 case kCondLT:
3795 if (gt_bias) {
3796 __ CmpLtD(FTMP, lhs, rhs);
3797 } else {
3798 __ CmpUltD(FTMP, lhs, rhs);
3799 }
3800 __ Bc1nez(FTMP, label);
3801 break;
3802 case kCondLE:
3803 if (gt_bias) {
3804 __ CmpLeD(FTMP, lhs, rhs);
3805 } else {
3806 __ CmpUleD(FTMP, lhs, rhs);
3807 }
3808 __ Bc1nez(FTMP, label);
3809 break;
3810 case kCondGT:
3811 if (gt_bias) {
3812 __ CmpUltD(FTMP, rhs, lhs);
3813 } else {
3814 __ CmpLtD(FTMP, rhs, lhs);
3815 }
3816 __ Bc1nez(FTMP, label);
3817 break;
3818 case kCondGE:
3819 if (gt_bias) {
3820 __ CmpUleD(FTMP, rhs, lhs);
3821 } else {
3822 __ CmpLeD(FTMP, rhs, lhs);
3823 }
3824 __ Bc1nez(FTMP, label);
3825 break;
3826 default:
3827 LOG(FATAL) << "Unexpected non-floating-point condition";
3828 }
3829 }
3830}
3831
Alexey Frunze4dda3372015-06-01 18:31:49 -07003832void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003833 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003834 Mips64Label* true_target,
3835 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003836 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003837
David Brazdil0debae72015-11-12 18:37:00 +00003838 if (true_target == nullptr && false_target == nullptr) {
3839 // Nothing to do. The code always falls through.
3840 return;
3841 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003842 // Constant condition, statically compared against "true" (integer value 1).
3843 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003844 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003845 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003846 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003847 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003848 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003849 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003850 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003851 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003852 }
David Brazdil0debae72015-11-12 18:37:00 +00003853 return;
3854 }
3855
3856 // The following code generates these patterns:
3857 // (1) true_target == nullptr && false_target != nullptr
3858 // - opposite condition true => branch to false_target
3859 // (2) true_target != nullptr && false_target == nullptr
3860 // - condition true => branch to true_target
3861 // (3) true_target != nullptr && false_target != nullptr
3862 // - condition true => branch to true_target
3863 // - branch to false_target
3864 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003865 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003866 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003868 if (true_target == nullptr) {
3869 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3870 } else {
3871 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3872 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003873 } else {
3874 // The condition instruction has not been materialized, use its inputs as
3875 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003876 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003877 Primitive::Type type = condition->InputAt(0)->GetType();
3878 LocationSummary* locations = cond->GetLocations();
3879 IfCondition if_cond = condition->GetCondition();
3880 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003881
David Brazdil0debae72015-11-12 18:37:00 +00003882 if (true_target == nullptr) {
3883 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003884 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003885 }
3886
Alexey Frunze299a9392015-12-08 16:08:02 -08003887 switch (type) {
3888 default:
3889 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3890 break;
3891 case Primitive::kPrimLong:
3892 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3893 break;
3894 case Primitive::kPrimFloat:
3895 case Primitive::kPrimDouble:
3896 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3897 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003898 }
3899 }
David Brazdil0debae72015-11-12 18:37:00 +00003900
3901 // If neither branch falls through (case 3), the conditional branch to `true_target`
3902 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3903 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003904 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003905 }
3906}
3907
3908void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003910 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003911 locations->SetInAt(0, Location::RequiresRegister());
3912 }
3913}
3914
3915void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003916 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3917 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003918 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003919 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003920 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003921 nullptr : codegen_->GetLabelOf(false_successor);
3922 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003923}
3924
3925void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3926 LocationSummary* locations = new (GetGraph()->GetArena())
3927 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003928 InvokeRuntimeCallingConvention calling_convention;
3929 RegisterSet caller_saves = RegisterSet::Empty();
3930 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3931 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003932 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003933 locations->SetInAt(0, Location::RequiresRegister());
3934 }
3935}
3936
3937void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003938 SlowPathCodeMIPS64* slow_path =
3939 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003940 GenerateTestAndBranch(deoptimize,
3941 /* condition_input_index */ 0,
3942 slow_path->GetEntryLabel(),
3943 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003944}
3945
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003946void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3947 LocationSummary* locations = new (GetGraph()->GetArena())
3948 LocationSummary(flag, LocationSummary::kNoCall);
3949 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003950}
3951
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003952void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3953 __ LoadFromOffset(kLoadWord,
3954 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3955 SP,
3956 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003957}
3958
David Brazdil74eb1b22015-12-14 11:44:01 +00003959void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3961 if (Primitive::IsFloatingPointType(select->GetType())) {
3962 locations->SetInAt(0, Location::RequiresFpuRegister());
3963 locations->SetInAt(1, Location::RequiresFpuRegister());
3964 } else {
3965 locations->SetInAt(0, Location::RequiresRegister());
3966 locations->SetInAt(1, Location::RequiresRegister());
3967 }
3968 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3969 locations->SetInAt(2, Location::RequiresRegister());
3970 }
3971 locations->SetOut(Location::SameAsFirstInput());
3972}
3973
3974void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3975 LocationSummary* locations = select->GetLocations();
3976 Mips64Label false_target;
3977 GenerateTestAndBranch(select,
3978 /* condition_input_index */ 2,
3979 /* true_target */ nullptr,
3980 &false_target);
3981 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3982 __ Bind(&false_target);
3983}
3984
David Srbecky0cf44932015-12-09 14:09:59 +00003985void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3986 new (GetGraph()->GetArena()) LocationSummary(info);
3987}
3988
David Srbeckyd28f4a02016-03-14 17:14:24 +00003989void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3990 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003991}
3992
3993void CodeGeneratorMIPS64::GenerateNop() {
3994 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003995}
3996
Alexey Frunze4dda3372015-06-01 18:31:49 -07003997void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003998 const FieldInfo& field_info) {
3999 Primitive::Type field_type = field_info.GetFieldType();
4000 bool object_field_get_with_read_barrier =
4001 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4002 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4003 instruction,
4004 object_field_get_with_read_barrier
4005 ? LocationSummary::kCallOnSlowPath
4006 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004007 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4008 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4009 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004010 locations->SetInAt(0, Location::RequiresRegister());
4011 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4012 locations->SetOut(Location::RequiresFpuRegister());
4013 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004014 // The output overlaps in the case of an object field get with
4015 // read barriers enabled: we do not want the move to overwrite the
4016 // object's location, as we need it to emit the read barrier.
4017 locations->SetOut(Location::RequiresRegister(),
4018 object_field_get_with_read_barrier
4019 ? Location::kOutputOverlap
4020 : Location::kNoOutputOverlap);
4021 }
4022 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4023 // We need a temporary register for the read barrier marking slow
4024 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
4025 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004026 }
4027}
4028
4029void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4030 const FieldInfo& field_info) {
4031 Primitive::Type type = field_info.GetFieldType();
4032 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004033 Location obj_loc = locations->InAt(0);
4034 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4035 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004036 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004037 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004038 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004039 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4040
Alexey Frunze4dda3372015-06-01 18:31:49 -07004041 switch (type) {
4042 case Primitive::kPrimBoolean:
4043 load_type = kLoadUnsignedByte;
4044 break;
4045 case Primitive::kPrimByte:
4046 load_type = kLoadSignedByte;
4047 break;
4048 case Primitive::kPrimShort:
4049 load_type = kLoadSignedHalfword;
4050 break;
4051 case Primitive::kPrimChar:
4052 load_type = kLoadUnsignedHalfword;
4053 break;
4054 case Primitive::kPrimInt:
4055 case Primitive::kPrimFloat:
4056 load_type = kLoadWord;
4057 break;
4058 case Primitive::kPrimLong:
4059 case Primitive::kPrimDouble:
4060 load_type = kLoadDoubleword;
4061 break;
4062 case Primitive::kPrimNot:
4063 load_type = kLoadUnsignedWord;
4064 break;
4065 case Primitive::kPrimVoid:
4066 LOG(FATAL) << "Unreachable type " << type;
4067 UNREACHABLE();
4068 }
4069 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004070 DCHECK(dst_loc.IsRegister());
4071 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4072 if (type == Primitive::kPrimNot) {
4073 // /* HeapReference<Object> */ dst = *(obj + offset)
4074 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4075 Location temp_loc = locations->GetTemp(0);
4076 // Note that a potential implicit null check is handled in this
4077 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4078 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4079 dst_loc,
4080 obj,
4081 offset,
4082 temp_loc,
4083 /* needs_null_check */ true);
4084 if (is_volatile) {
4085 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4086 }
4087 } else {
4088 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4089 if (is_volatile) {
4090 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4091 }
4092 // If read barriers are enabled, emit read barriers other than
4093 // Baker's using a slow path (and also unpoison the loaded
4094 // reference, if heap poisoning is enabled).
4095 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4096 }
4097 } else {
4098 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4099 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004100 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004101 DCHECK(dst_loc.IsFpuRegister());
4102 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004103 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004104 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004105
Alexey Frunze15958152017-02-09 19:08:30 -08004106 // Memory barriers, in the case of references, are handled in the
4107 // previous switch statement.
4108 if (is_volatile && (type != Primitive::kPrimNot)) {
4109 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111}
4112
4113void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4114 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4115 LocationSummary* locations =
4116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4117 locations->SetInAt(0, Location::RequiresRegister());
4118 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004119 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004120 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004121 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004122 }
4123}
4124
4125void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004126 const FieldInfo& field_info,
4127 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128 Primitive::Type type = field_info.GetFieldType();
4129 LocationSummary* locations = instruction->GetLocations();
4130 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004131 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004132 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004133 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004134 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4135 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004136 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4137
Alexey Frunze4dda3372015-06-01 18:31:49 -07004138 switch (type) {
4139 case Primitive::kPrimBoolean:
4140 case Primitive::kPrimByte:
4141 store_type = kStoreByte;
4142 break;
4143 case Primitive::kPrimShort:
4144 case Primitive::kPrimChar:
4145 store_type = kStoreHalfword;
4146 break;
4147 case Primitive::kPrimInt:
4148 case Primitive::kPrimFloat:
4149 case Primitive::kPrimNot:
4150 store_type = kStoreWord;
4151 break;
4152 case Primitive::kPrimLong:
4153 case Primitive::kPrimDouble:
4154 store_type = kStoreDoubleword;
4155 break;
4156 case Primitive::kPrimVoid:
4157 LOG(FATAL) << "Unreachable type " << type;
4158 UNREACHABLE();
4159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004160
Alexey Frunze15958152017-02-09 19:08:30 -08004161 if (is_volatile) {
4162 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4163 }
4164
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004165 if (value_location.IsConstant()) {
4166 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4167 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4168 } else {
4169 if (!Primitive::IsFloatingPointType(type)) {
4170 DCHECK(value_location.IsRegister());
4171 GpuRegister src = value_location.AsRegister<GpuRegister>();
4172 if (kPoisonHeapReferences && needs_write_barrier) {
4173 // Note that in the case where `value` is a null reference,
4174 // we do not enter this block, as a null reference does not
4175 // need poisoning.
4176 DCHECK_EQ(type, Primitive::kPrimNot);
4177 __ PoisonHeapReference(TMP, src);
4178 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4179 } else {
4180 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4181 }
4182 } else {
4183 DCHECK(value_location.IsFpuRegister());
4184 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4185 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4186 }
4187 }
Alexey Frunze15958152017-02-09 19:08:30 -08004188
Alexey Frunzec061de12017-02-14 13:27:23 -08004189 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004190 DCHECK(value_location.IsRegister());
4191 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004192 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004193 }
Alexey Frunze15958152017-02-09 19:08:30 -08004194
4195 if (is_volatile) {
4196 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4197 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004198}
4199
4200void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4201 HandleFieldGet(instruction, instruction->GetFieldInfo());
4202}
4203
4204void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4205 HandleFieldGet(instruction, instruction->GetFieldInfo());
4206}
4207
4208void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4209 HandleFieldSet(instruction, instruction->GetFieldInfo());
4210}
4211
4212void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004213 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214}
4215
Alexey Frunze15958152017-02-09 19:08:30 -08004216void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4217 HInstruction* instruction,
4218 Location out,
4219 uint32_t offset,
4220 Location maybe_temp,
4221 ReadBarrierOption read_barrier_option) {
4222 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4223 if (read_barrier_option == kWithReadBarrier) {
4224 CHECK(kEmitCompilerReadBarrier);
4225 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4226 if (kUseBakerReadBarrier) {
4227 // Load with fast path based Baker's read barrier.
4228 // /* HeapReference<Object> */ out = *(out + offset)
4229 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4230 out,
4231 out_reg,
4232 offset,
4233 maybe_temp,
4234 /* needs_null_check */ false);
4235 } else {
4236 // Load with slow path based read barrier.
4237 // Save the value of `out` into `maybe_temp` before overwriting it
4238 // in the following move operation, as we will need it for the
4239 // read barrier below.
4240 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4241 // /* HeapReference<Object> */ out = *(out + offset)
4242 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4243 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4244 }
4245 } else {
4246 // Plain load with no read barrier.
4247 // /* HeapReference<Object> */ out = *(out + offset)
4248 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4249 __ MaybeUnpoisonHeapReference(out_reg);
4250 }
4251}
4252
4253void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4254 HInstruction* instruction,
4255 Location out,
4256 Location obj,
4257 uint32_t offset,
4258 Location maybe_temp,
4259 ReadBarrierOption read_barrier_option) {
4260 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4261 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4262 if (read_barrier_option == kWithReadBarrier) {
4263 CHECK(kEmitCompilerReadBarrier);
4264 if (kUseBakerReadBarrier) {
4265 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4266 // Load with fast path based Baker's read barrier.
4267 // /* HeapReference<Object> */ out = *(obj + offset)
4268 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4269 out,
4270 obj_reg,
4271 offset,
4272 maybe_temp,
4273 /* needs_null_check */ false);
4274 } else {
4275 // Load with slow path based read barrier.
4276 // /* HeapReference<Object> */ out = *(obj + offset)
4277 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4278 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4279 }
4280 } else {
4281 // Plain load with no read barrier.
4282 // /* HeapReference<Object> */ out = *(obj + offset)
4283 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4284 __ MaybeUnpoisonHeapReference(out_reg);
4285 }
4286}
4287
Alexey Frunzef63f5692016-12-13 17:43:11 -08004288void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004289 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004290 Location root,
4291 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004292 uint32_t offset,
4293 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004294 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004295 if (read_barrier_option == kWithReadBarrier) {
4296 DCHECK(kEmitCompilerReadBarrier);
4297 if (kUseBakerReadBarrier) {
4298 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4299 // Baker's read barrier are used:
4300 //
4301 // root = obj.field;
4302 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4303 // if (temp != null) {
4304 // root = temp(root)
4305 // }
4306
4307 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4308 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4309 static_assert(
4310 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4311 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4312 "have different sizes.");
4313 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4314 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4315 "have different sizes.");
4316
4317 // Slow path marking the GC root `root`.
4318 Location temp = Location::RegisterLocation(T9);
4319 SlowPathCodeMIPS64* slow_path =
4320 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4321 instruction,
4322 root,
4323 /*entrypoint*/ temp);
4324 codegen_->AddSlowPath(slow_path);
4325
4326 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4327 const int32_t entry_point_offset =
4328 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4329 // Loading the entrypoint does not require a load acquire since it is only changed when
4330 // threads are suspended or running a checkpoint.
4331 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4332 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4333 // checking GetIsGcMarking.
4334 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4335 __ Bind(slow_path->GetExitLabel());
4336 } else {
4337 // GC root loaded through a slow path for read barriers other
4338 // than Baker's.
4339 // /* GcRoot<mirror::Object>* */ root = obj + offset
4340 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4341 // /* mirror::Object* */ root = root->Read()
4342 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4343 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004344 } else {
4345 // Plain GC root load with no read barrier.
4346 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4347 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4348 // Note that GC roots are not affected by heap poisoning, thus we
4349 // do not have to unpoison `root_reg` here.
4350 }
4351}
4352
Alexey Frunze15958152017-02-09 19:08:30 -08004353void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4354 Location ref,
4355 GpuRegister obj,
4356 uint32_t offset,
4357 Location temp,
4358 bool needs_null_check) {
4359 DCHECK(kEmitCompilerReadBarrier);
4360 DCHECK(kUseBakerReadBarrier);
4361
4362 // /* HeapReference<Object> */ ref = *(obj + offset)
4363 Location no_index = Location::NoLocation();
4364 ScaleFactor no_scale_factor = TIMES_1;
4365 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4366 ref,
4367 obj,
4368 offset,
4369 no_index,
4370 no_scale_factor,
4371 temp,
4372 needs_null_check);
4373}
4374
4375void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4376 Location ref,
4377 GpuRegister obj,
4378 uint32_t data_offset,
4379 Location index,
4380 Location temp,
4381 bool needs_null_check) {
4382 DCHECK(kEmitCompilerReadBarrier);
4383 DCHECK(kUseBakerReadBarrier);
4384
4385 static_assert(
4386 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4387 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4388 // /* HeapReference<Object> */ ref =
4389 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4390 ScaleFactor scale_factor = TIMES_4;
4391 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4392 ref,
4393 obj,
4394 data_offset,
4395 index,
4396 scale_factor,
4397 temp,
4398 needs_null_check);
4399}
4400
4401void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4402 Location ref,
4403 GpuRegister obj,
4404 uint32_t offset,
4405 Location index,
4406 ScaleFactor scale_factor,
4407 Location temp,
4408 bool needs_null_check,
4409 bool always_update_field) {
4410 DCHECK(kEmitCompilerReadBarrier);
4411 DCHECK(kUseBakerReadBarrier);
4412
4413 // In slow path based read barriers, the read barrier call is
4414 // inserted after the original load. However, in fast path based
4415 // Baker's read barriers, we need to perform the load of
4416 // mirror::Object::monitor_ *before* the original reference load.
4417 // This load-load ordering is required by the read barrier.
4418 // The fast path/slow path (for Baker's algorithm) should look like:
4419 //
4420 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4421 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4422 // HeapReference<Object> ref = *src; // Original reference load.
4423 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4424 // if (is_gray) {
4425 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4426 // }
4427 //
4428 // Note: the original implementation in ReadBarrier::Barrier is
4429 // slightly more complex as it performs additional checks that we do
4430 // not do here for performance reasons.
4431
4432 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4433 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4434 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4435
4436 // /* int32_t */ monitor = obj->monitor_
4437 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4438 if (needs_null_check) {
4439 MaybeRecordImplicitNullCheck(instruction);
4440 }
4441 // /* LockWord */ lock_word = LockWord(monitor)
4442 static_assert(sizeof(LockWord) == sizeof(int32_t),
4443 "art::LockWord and int32_t have different sizes.");
4444
4445 __ Sync(0); // Barrier to prevent load-load reordering.
4446
4447 // The actual reference load.
4448 if (index.IsValid()) {
4449 // Load types involving an "index": ArrayGet,
4450 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4451 // intrinsics.
4452 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4453 if (index.IsConstant()) {
4454 size_t computed_offset =
4455 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4456 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4457 } else {
4458 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004459 if (scale_factor == TIMES_1) {
4460 __ Daddu(TMP, index_reg, obj);
4461 } else {
4462 __ Dlsa(TMP, index_reg, obj, scale_factor);
4463 }
Alexey Frunze15958152017-02-09 19:08:30 -08004464 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4465 }
4466 } else {
4467 // /* HeapReference<Object> */ ref = *(obj + offset)
4468 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4469 }
4470
4471 // Object* ref = ref_addr->AsMirrorPtr()
4472 __ MaybeUnpoisonHeapReference(ref_reg);
4473
4474 // Slow path marking the object `ref` when it is gray.
4475 SlowPathCodeMIPS64* slow_path;
4476 if (always_update_field) {
4477 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4478 // of the form `obj + field_offset`, where `obj` is a register and
4479 // `field_offset` is a register. Thus `offset` and `scale_factor`
4480 // above are expected to be null in this code path.
4481 DCHECK_EQ(offset, 0u);
4482 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4483 slow_path = new (GetGraph()->GetArena())
4484 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4485 ref,
4486 obj,
4487 /* field_offset */ index,
4488 temp_reg);
4489 } else {
4490 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4491 }
4492 AddSlowPath(slow_path);
4493
4494 // if (rb_state == ReadBarrier::GrayState())
4495 // ref = ReadBarrier::Mark(ref);
4496 // Given the numeric representation, it's enough to check the low bit of the
4497 // rb_state. We do that by shifting the bit into the sign bit (31) and
4498 // performing a branch on less than zero.
4499 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4500 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4501 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4502 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4503 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4504 __ Bind(slow_path->GetExitLabel());
4505}
4506
4507void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4508 Location out,
4509 Location ref,
4510 Location obj,
4511 uint32_t offset,
4512 Location index) {
4513 DCHECK(kEmitCompilerReadBarrier);
4514
4515 // Insert a slow path based read barrier *after* the reference load.
4516 //
4517 // If heap poisoning is enabled, the unpoisoning of the loaded
4518 // reference will be carried out by the runtime within the slow
4519 // path.
4520 //
4521 // Note that `ref` currently does not get unpoisoned (when heap
4522 // poisoning is enabled), which is alright as the `ref` argument is
4523 // not used by the artReadBarrierSlow entry point.
4524 //
4525 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4526 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4527 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4528 AddSlowPath(slow_path);
4529
4530 __ Bc(slow_path->GetEntryLabel());
4531 __ Bind(slow_path->GetExitLabel());
4532}
4533
4534void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4535 Location out,
4536 Location ref,
4537 Location obj,
4538 uint32_t offset,
4539 Location index) {
4540 if (kEmitCompilerReadBarrier) {
4541 // Baker's read barriers shall be handled by the fast path
4542 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4543 DCHECK(!kUseBakerReadBarrier);
4544 // If heap poisoning is enabled, unpoisoning will be taken care of
4545 // by the runtime within the slow path.
4546 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4547 } else if (kPoisonHeapReferences) {
4548 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4549 }
4550}
4551
4552void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4553 Location out,
4554 Location root) {
4555 DCHECK(kEmitCompilerReadBarrier);
4556
4557 // Insert a slow path based read barrier *after* the GC root load.
4558 //
4559 // Note that GC roots are not affected by heap poisoning, so we do
4560 // not need to do anything special for this here.
4561 SlowPathCodeMIPS64* slow_path =
4562 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4563 AddSlowPath(slow_path);
4564
4565 __ Bc(slow_path->GetEntryLabel());
4566 __ Bind(slow_path->GetExitLabel());
4567}
4568
Alexey Frunze4dda3372015-06-01 18:31:49 -07004569void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004570 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4571 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004572 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004573 switch (type_check_kind) {
4574 case TypeCheckKind::kExactCheck:
4575 case TypeCheckKind::kAbstractClassCheck:
4576 case TypeCheckKind::kClassHierarchyCheck:
4577 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004578 call_kind =
4579 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004580 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004581 break;
4582 case TypeCheckKind::kArrayCheck:
4583 case TypeCheckKind::kUnresolvedCheck:
4584 case TypeCheckKind::kInterfaceCheck:
4585 call_kind = LocationSummary::kCallOnSlowPath;
4586 break;
4587 }
4588
Alexey Frunze4dda3372015-06-01 18:31:49 -07004589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004590 if (baker_read_barrier_slow_path) {
4591 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4592 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004593 locations->SetInAt(0, Location::RequiresRegister());
4594 locations->SetInAt(1, Location::RequiresRegister());
4595 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004596 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004597 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004598 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004599}
4600
4601void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004602 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004603 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004604 Location obj_loc = locations->InAt(0);
4605 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004606 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004607 Location out_loc = locations->Out();
4608 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4609 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4610 DCHECK_LE(num_temps, 1u);
4611 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004612 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4613 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4614 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4615 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004616 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004617 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004618
4619 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004620 // Avoid this check if we know `obj` is not null.
4621 if (instruction->MustDoNullCheck()) {
4622 __ Move(out, ZERO);
4623 __ Beqzc(obj, &done);
4624 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004625
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004626 switch (type_check_kind) {
4627 case TypeCheckKind::kExactCheck: {
4628 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004629 GenerateReferenceLoadTwoRegisters(instruction,
4630 out_loc,
4631 obj_loc,
4632 class_offset,
4633 maybe_temp_loc,
4634 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004635 // Classes must be equal for the instanceof to succeed.
4636 __ Xor(out, out, cls);
4637 __ Sltiu(out, out, 1);
4638 break;
4639 }
4640
4641 case TypeCheckKind::kAbstractClassCheck: {
4642 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004643 GenerateReferenceLoadTwoRegisters(instruction,
4644 out_loc,
4645 obj_loc,
4646 class_offset,
4647 maybe_temp_loc,
4648 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004649 // If the class is abstract, we eagerly fetch the super class of the
4650 // object to avoid doing a comparison we know will fail.
4651 Mips64Label loop;
4652 __ Bind(&loop);
4653 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004654 GenerateReferenceLoadOneRegister(instruction,
4655 out_loc,
4656 super_offset,
4657 maybe_temp_loc,
4658 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004659 // If `out` is null, we use it for the result, and jump to `done`.
4660 __ Beqzc(out, &done);
4661 __ Bnec(out, cls, &loop);
4662 __ LoadConst32(out, 1);
4663 break;
4664 }
4665
4666 case TypeCheckKind::kClassHierarchyCheck: {
4667 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004668 GenerateReferenceLoadTwoRegisters(instruction,
4669 out_loc,
4670 obj_loc,
4671 class_offset,
4672 maybe_temp_loc,
4673 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004674 // Walk over the class hierarchy to find a match.
4675 Mips64Label loop, success;
4676 __ Bind(&loop);
4677 __ Beqc(out, cls, &success);
4678 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004679 GenerateReferenceLoadOneRegister(instruction,
4680 out_loc,
4681 super_offset,
4682 maybe_temp_loc,
4683 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004684 __ Bnezc(out, &loop);
4685 // If `out` is null, we use it for the result, and jump to `done`.
4686 __ Bc(&done);
4687 __ Bind(&success);
4688 __ LoadConst32(out, 1);
4689 break;
4690 }
4691
4692 case TypeCheckKind::kArrayObjectCheck: {
4693 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004694 GenerateReferenceLoadTwoRegisters(instruction,
4695 out_loc,
4696 obj_loc,
4697 class_offset,
4698 maybe_temp_loc,
4699 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004700 // Do an exact check.
4701 Mips64Label success;
4702 __ Beqc(out, cls, &success);
4703 // Otherwise, we need to check that the object's class is a non-primitive array.
4704 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004705 GenerateReferenceLoadOneRegister(instruction,
4706 out_loc,
4707 component_offset,
4708 maybe_temp_loc,
4709 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004710 // If `out` is null, we use it for the result, and jump to `done`.
4711 __ Beqzc(out, &done);
4712 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4713 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4714 __ Sltiu(out, out, 1);
4715 __ Bc(&done);
4716 __ Bind(&success);
4717 __ LoadConst32(out, 1);
4718 break;
4719 }
4720
4721 case TypeCheckKind::kArrayCheck: {
4722 // No read barrier since the slow path will retry upon failure.
4723 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004724 GenerateReferenceLoadTwoRegisters(instruction,
4725 out_loc,
4726 obj_loc,
4727 class_offset,
4728 maybe_temp_loc,
4729 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004730 DCHECK(locations->OnlyCallsOnSlowPath());
4731 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4732 /* is_fatal */ false);
4733 codegen_->AddSlowPath(slow_path);
4734 __ Bnec(out, cls, slow_path->GetEntryLabel());
4735 __ LoadConst32(out, 1);
4736 break;
4737 }
4738
4739 case TypeCheckKind::kUnresolvedCheck:
4740 case TypeCheckKind::kInterfaceCheck: {
4741 // Note that we indeed only call on slow path, but we always go
4742 // into the slow path for the unresolved and interface check
4743 // cases.
4744 //
4745 // We cannot directly call the InstanceofNonTrivial runtime
4746 // entry point without resorting to a type checking slow path
4747 // here (i.e. by calling InvokeRuntime directly), as it would
4748 // require to assign fixed registers for the inputs of this
4749 // HInstanceOf instruction (following the runtime calling
4750 // convention), which might be cluttered by the potential first
4751 // read barrier emission at the beginning of this method.
4752 //
4753 // TODO: Introduce a new runtime entry point taking the object
4754 // to test (instead of its class) as argument, and let it deal
4755 // with the read barrier issues. This will let us refactor this
4756 // case of the `switch` code as it was previously (with a direct
4757 // call to the runtime not using a type checking slow path).
4758 // This should also be beneficial for the other cases above.
4759 DCHECK(locations->OnlyCallsOnSlowPath());
4760 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4761 /* is_fatal */ false);
4762 codegen_->AddSlowPath(slow_path);
4763 __ Bc(slow_path->GetEntryLabel());
4764 break;
4765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 }
4767
4768 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004769
4770 if (slow_path != nullptr) {
4771 __ Bind(slow_path->GetExitLabel());
4772 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004773}
4774
4775void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4777 locations->SetOut(Location::ConstantLocation(constant));
4778}
4779
4780void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4781 // Will be generated at use site.
4782}
4783
4784void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4786 locations->SetOut(Location::ConstantLocation(constant));
4787}
4788
4789void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4790 // Will be generated at use site.
4791}
4792
Calin Juravle175dc732015-08-25 15:42:32 +01004793void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4794 // The trampoline uses the same calling convention as dex calling conventions,
4795 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4796 // the method_idx.
4797 HandleInvoke(invoke);
4798}
4799
4800void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4801 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4802}
4803
Alexey Frunze4dda3372015-06-01 18:31:49 -07004804void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4805 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4806 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4807}
4808
4809void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4810 HandleInvoke(invoke);
4811 // The register T0 is required to be used for the hidden argument in
4812 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4813 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4814}
4815
4816void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4817 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4818 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004819 Location receiver = invoke->GetLocations()->InAt(0);
4820 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004821 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004822
4823 // Set the hidden argument.
4824 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4825 invoke->GetDexMethodIndex());
4826
4827 // temp = object->GetClass();
4828 if (receiver.IsStackSlot()) {
4829 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4830 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4831 } else {
4832 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4833 }
4834 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004835 // Instead of simply (possibly) unpoisoning `temp` here, we should
4836 // emit a read barrier for the previous class reference load.
4837 // However this is not required in practice, as this is an
4838 // intermediate/temporary reference and because the current
4839 // concurrent copying collector keeps the from-space memory
4840 // intact/accessible until the end of the marking phase (the
4841 // concurrent copying collector may not in the future).
4842 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004843 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4844 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4845 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004846 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847 // temp = temp->GetImtEntryAt(method_offset);
4848 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4849 // T9 = temp->GetEntryPoint();
4850 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4851 // T9();
4852 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004853 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004854 DCHECK(!codegen_->IsLeafMethod());
4855 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4856}
4857
4858void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004859 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4860 if (intrinsic.TryDispatch(invoke)) {
4861 return;
4862 }
4863
Alexey Frunze4dda3372015-06-01 18:31:49 -07004864 HandleInvoke(invoke);
4865}
4866
4867void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004868 // Explicit clinit checks triggered by static invokes must have been pruned by
4869 // art::PrepareForRegisterAllocation.
4870 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004871
Chris Larsen3039e382015-08-26 07:54:08 -07004872 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4873 if (intrinsic.TryDispatch(invoke)) {
4874 return;
4875 }
4876
Alexey Frunze4dda3372015-06-01 18:31:49 -07004877 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004878}
4879
Orion Hodsonac141392017-01-13 11:53:47 +00004880void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4881 HandleInvoke(invoke);
4882}
4883
4884void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4885 codegen_->GenerateInvokePolymorphicCall(invoke);
4886}
4887
Chris Larsen3039e382015-08-26 07:54:08 -07004888static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004889 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004890 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4891 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004892 return true;
4893 }
4894 return false;
4895}
4896
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004897HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004898 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004899 bool fallback_load = false;
4900 switch (desired_string_load_kind) {
4901 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4902 DCHECK(!GetCompilerOptions().GetCompilePic());
4903 break;
4904 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4905 DCHECK(GetCompilerOptions().GetCompilePic());
4906 break;
4907 case HLoadString::LoadKind::kBootImageAddress:
4908 break;
4909 case HLoadString::LoadKind::kBssEntry:
4910 DCHECK(!Runtime::Current()->UseJitCompilation());
4911 break;
4912 case HLoadString::LoadKind::kDexCacheViaMethod:
4913 break;
4914 case HLoadString::LoadKind::kJitTableAddress:
4915 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004916 break;
4917 }
4918 if (fallback_load) {
4919 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4920 }
4921 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004922}
4923
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4925 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004926 bool fallback_load = false;
4927 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004928 case HLoadClass::LoadKind::kInvalid:
4929 LOG(FATAL) << "UNREACHABLE";
4930 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004931 case HLoadClass::LoadKind::kReferrersClass:
4932 break;
4933 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4934 DCHECK(!GetCompilerOptions().GetCompilePic());
4935 break;
4936 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4937 DCHECK(GetCompilerOptions().GetCompilePic());
4938 break;
4939 case HLoadClass::LoadKind::kBootImageAddress:
4940 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004941 case HLoadClass::LoadKind::kBssEntry:
4942 DCHECK(!Runtime::Current()->UseJitCompilation());
4943 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004944 case HLoadClass::LoadKind::kJitTableAddress:
4945 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004946 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004947 case HLoadClass::LoadKind::kDexCacheViaMethod:
4948 break;
4949 }
4950 if (fallback_load) {
4951 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4952 }
4953 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004954}
4955
Vladimir Markodc151b22015-10-15 18:02:30 +01004956HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4957 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004958 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004959 // On MIPS64 we support all dispatch types.
4960 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004961}
4962
Alexey Frunze4dda3372015-06-01 18:31:49 -07004963void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4964 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004965 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004966 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4967 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4968
Alexey Frunze19f6c692016-11-30 19:19:55 -08004969 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004970 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004971 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004972 uint32_t offset =
4973 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004974 __ LoadFromOffset(kLoadDoubleword,
4975 temp.AsRegister<GpuRegister>(),
4976 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004977 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004978 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004979 }
Vladimir Marko58155012015-08-19 12:49:41 +00004980 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004981 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004982 break;
4983 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004984 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4985 kLoadDoubleword,
4986 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004987 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004988 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4989 uint32_t offset = invoke->GetDexCacheArrayOffset();
4990 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004991 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004992 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4993 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4994 break;
4995 }
Vladimir Marko58155012015-08-19 12:49:41 +00004996 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004997 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004998 GpuRegister reg = temp.AsRegister<GpuRegister>();
4999 GpuRegister method_reg;
5000 if (current_method.IsRegister()) {
5001 method_reg = current_method.AsRegister<GpuRegister>();
5002 } else {
5003 // TODO: use the appropriate DCHECK() here if possible.
5004 // DCHECK(invoke->GetLocations()->Intrinsified());
5005 DCHECK(!current_method.IsValid());
5006 method_reg = reg;
5007 __ Ld(reg, SP, kCurrentMethodStackOffset);
5008 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005009
Vladimir Marko58155012015-08-19 12:49:41 +00005010 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01005011 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00005012 reg,
5013 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01005014 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005015 // temp = temp[index_in_cache];
5016 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5017 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00005018 __ LoadFromOffset(kLoadDoubleword,
5019 reg,
5020 reg,
5021 CodeGenerator::GetCachePointerOffset(index_in_cache));
5022 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005023 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005024 }
5025
Alexey Frunze19f6c692016-11-30 19:19:55 -08005026 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005027 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005028 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005029 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005030 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5031 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5032 __ LoadFromOffset(kLoadDoubleword,
5033 T9,
5034 callee_method.AsRegister<GpuRegister>(),
5035 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005036 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005037 // T9()
5038 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005039 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005040 break;
5041 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005042 DCHECK(!IsLeafMethod());
5043}
5044
5045void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005046 // Explicit clinit checks triggered by static invokes must have been pruned by
5047 // art::PrepareForRegisterAllocation.
5048 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005049
5050 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5051 return;
5052 }
5053
5054 LocationSummary* locations = invoke->GetLocations();
5055 codegen_->GenerateStaticOrDirectCall(invoke,
5056 locations->HasTemps()
5057 ? locations->GetTemp(0)
5058 : Location::NoLocation());
5059 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5060}
5061
Alexey Frunze53afca12015-11-05 16:34:23 -08005062void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005063 // Use the calling convention instead of the location of the receiver, as
5064 // intrinsics may have put the receiver in a different register. In the intrinsics
5065 // slow path, the arguments have been moved to the right place, so here we are
5066 // guaranteed that the receiver is the first register of the calling convention.
5067 InvokeDexCallingConvention calling_convention;
5068 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5069
Alexey Frunze53afca12015-11-05 16:34:23 -08005070 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005071 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5072 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5073 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005074 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005075
5076 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005077 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005078 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005079 // Instead of simply (possibly) unpoisoning `temp` here, we should
5080 // emit a read barrier for the previous class reference load.
5081 // However this is not required in practice, as this is an
5082 // intermediate/temporary reference and because the current
5083 // concurrent copying collector keeps the from-space memory
5084 // intact/accessible until the end of the marking phase (the
5085 // concurrent copying collector may not in the future).
5086 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005087 // temp = temp->GetMethodAt(method_offset);
5088 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5089 // T9 = temp->GetEntryPoint();
5090 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5091 // T9();
5092 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005093 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08005094}
5095
5096void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5097 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5098 return;
5099 }
5100
5101 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005102 DCHECK(!codegen_->IsLeafMethod());
5103 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5104}
5105
5106void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005107 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5108 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005109 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005110 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5111 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005112 return;
5113 }
Vladimir Marko41559982017-01-06 14:04:23 +00005114 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005115
Alexey Frunze15958152017-02-09 19:08:30 -08005116 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5117 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005118 ? LocationSummary::kCallOnSlowPath
5119 : LocationSummary::kNoCall;
5120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005121 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5122 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5123 }
Vladimir Marko41559982017-01-06 14:04:23 +00005124 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005125 locations->SetInAt(0, Location::RequiresRegister());
5126 }
5127 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005128 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5129 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5130 // Rely on the type resolution or initialization and marking to save everything we need.
5131 RegisterSet caller_saves = RegisterSet::Empty();
5132 InvokeRuntimeCallingConvention calling_convention;
5133 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5134 locations->SetCustomSlowPathCallerSaves(caller_saves);
5135 } else {
5136 // For non-Baker read barrier we have a temp-clobbering call.
5137 }
5138 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005139}
5140
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005141// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5142// move.
5143void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005144 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5145 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5146 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005147 return;
5148 }
Vladimir Marko41559982017-01-06 14:04:23 +00005149 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005150
Vladimir Marko41559982017-01-06 14:04:23 +00005151 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005152 Location out_loc = locations->Out();
5153 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5154 GpuRegister current_method_reg = ZERO;
5155 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5156 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5157 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5158 }
5159
Alexey Frunze15958152017-02-09 19:08:30 -08005160 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5161 ? kWithoutReadBarrier
5162 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005163 bool generate_null_check = false;
5164 switch (load_kind) {
5165 case HLoadClass::LoadKind::kReferrersClass:
5166 DCHECK(!cls->CanCallRuntime());
5167 DCHECK(!cls->MustGenerateClinitCheck());
5168 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5169 GenerateGcRootFieldLoad(cls,
5170 out_loc,
5171 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005172 ArtMethod::DeclaringClassOffset().Int32Value(),
5173 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005174 break;
5175 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005176 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005177 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005178 __ LoadLiteral(out,
5179 kLoadUnsignedWord,
5180 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5181 cls->GetTypeIndex()));
5182 break;
5183 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005184 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005185 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005186 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5187 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5188 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5189 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5190 break;
5191 }
5192 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005193 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005194 uint32_t address = dchecked_integral_cast<uint32_t>(
5195 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5196 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005197 __ LoadLiteral(out,
5198 kLoadUnsignedWord,
5199 codegen_->DeduplicateBootImageAddressLiteral(address));
5200 break;
5201 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005202 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005203 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005204 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005205 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005206 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005207 generate_null_check = true;
5208 break;
5209 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005210 case HLoadClass::LoadKind::kJitTableAddress:
5211 __ LoadLiteral(out,
5212 kLoadUnsignedWord,
5213 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5214 cls->GetTypeIndex(),
5215 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005216 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005217 break;
Vladimir Marko41559982017-01-06 14:04:23 +00005218 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005219 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005220 LOG(FATAL) << "UNREACHABLE";
5221 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005222 }
5223
5224 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5225 DCHECK(cls->CanCallRuntime());
5226 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5227 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5228 codegen_->AddSlowPath(slow_path);
5229 if (generate_null_check) {
5230 __ Beqzc(out, slow_path->GetEntryLabel());
5231 }
5232 if (cls->MustGenerateClinitCheck()) {
5233 GenerateClassInitializationCheck(slow_path, out);
5234 } else {
5235 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005236 }
5237 }
5238}
5239
David Brazdilcb1c0552015-08-04 16:22:25 +01005240static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005241 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005242}
5243
Alexey Frunze4dda3372015-06-01 18:31:49 -07005244void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5245 LocationSummary* locations =
5246 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5247 locations->SetOut(Location::RequiresRegister());
5248}
5249
5250void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5251 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005252 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5253}
5254
5255void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5256 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5257}
5258
5259void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5260 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005261}
5262
Alexey Frunze4dda3372015-06-01 18:31:49 -07005263void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005264 HLoadString::LoadKind load_kind = load->GetLoadKind();
5265 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005267 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5268 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005269 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005270 } else {
5271 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005272 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5273 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5274 // Rely on the pResolveString and marking to save everything we need.
5275 RegisterSet caller_saves = RegisterSet::Empty();
5276 InvokeRuntimeCallingConvention calling_convention;
5277 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5278 locations->SetCustomSlowPathCallerSaves(caller_saves);
5279 } else {
5280 // For non-Baker read barrier we have a temp-clobbering call.
5281 }
5282 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005283 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005284}
5285
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005286// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5287// move.
5288void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005289 HLoadString::LoadKind load_kind = load->GetLoadKind();
5290 LocationSummary* locations = load->GetLocations();
5291 Location out_loc = locations->Out();
5292 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5293
5294 switch (load_kind) {
5295 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005296 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005297 __ LoadLiteral(out,
5298 kLoadUnsignedWord,
5299 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5300 load->GetStringIndex()));
5301 return; // No dex cache slow path.
5302 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5303 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5304 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005305 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005306 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5307 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5308 return; // No dex cache slow path.
5309 }
5310 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005311 uint32_t address = dchecked_integral_cast<uint32_t>(
5312 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5313 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005314 __ LoadLiteral(out,
5315 kLoadUnsignedWord,
5316 codegen_->DeduplicateBootImageAddressLiteral(address));
5317 return; // No dex cache slow path.
5318 }
5319 case HLoadString::LoadKind::kBssEntry: {
5320 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5321 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005322 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005323 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005324 GenerateGcRootFieldLoad(load,
5325 out_loc,
5326 out,
5327 /* placeholder */ 0x5678,
5328 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005329 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5330 codegen_->AddSlowPath(slow_path);
5331 __ Beqzc(out, slow_path->GetEntryLabel());
5332 __ Bind(slow_path->GetExitLabel());
5333 return;
5334 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005335 case HLoadString::LoadKind::kJitTableAddress:
5336 __ LoadLiteral(out,
5337 kLoadUnsignedWord,
5338 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5339 load->GetStringIndex(),
5340 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005341 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005342 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005343 default:
5344 break;
5345 }
5346
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005347 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005348 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5349 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005350 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005351 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5352 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5353 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005354}
5355
Alexey Frunze4dda3372015-06-01 18:31:49 -07005356void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5358 locations->SetOut(Location::ConstantLocation(constant));
5359}
5360
5361void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5362 // Will be generated at use site.
5363}
5364
5365void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5366 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005368 InvokeRuntimeCallingConvention calling_convention;
5369 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5370}
5371
5372void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005373 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005374 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005375 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005376 if (instruction->IsEnter()) {
5377 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5378 } else {
5379 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5380 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005381}
5382
5383void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5384 LocationSummary* locations =
5385 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5386 switch (mul->GetResultType()) {
5387 case Primitive::kPrimInt:
5388 case Primitive::kPrimLong:
5389 locations->SetInAt(0, Location::RequiresRegister());
5390 locations->SetInAt(1, Location::RequiresRegister());
5391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5392 break;
5393
5394 case Primitive::kPrimFloat:
5395 case Primitive::kPrimDouble:
5396 locations->SetInAt(0, Location::RequiresFpuRegister());
5397 locations->SetInAt(1, Location::RequiresFpuRegister());
5398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5399 break;
5400
5401 default:
5402 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5403 }
5404}
5405
5406void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5407 Primitive::Type type = instruction->GetType();
5408 LocationSummary* locations = instruction->GetLocations();
5409
5410 switch (type) {
5411 case Primitive::kPrimInt:
5412 case Primitive::kPrimLong: {
5413 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5414 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5415 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5416 if (type == Primitive::kPrimInt)
5417 __ MulR6(dst, lhs, rhs);
5418 else
5419 __ Dmul(dst, lhs, rhs);
5420 break;
5421 }
5422 case Primitive::kPrimFloat:
5423 case Primitive::kPrimDouble: {
5424 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5425 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5426 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5427 if (type == Primitive::kPrimFloat)
5428 __ MulS(dst, lhs, rhs);
5429 else
5430 __ MulD(dst, lhs, rhs);
5431 break;
5432 }
5433 default:
5434 LOG(FATAL) << "Unexpected mul type " << type;
5435 }
5436}
5437
5438void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5439 LocationSummary* locations =
5440 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5441 switch (neg->GetResultType()) {
5442 case Primitive::kPrimInt:
5443 case Primitive::kPrimLong:
5444 locations->SetInAt(0, Location::RequiresRegister());
5445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5446 break;
5447
5448 case Primitive::kPrimFloat:
5449 case Primitive::kPrimDouble:
5450 locations->SetInAt(0, Location::RequiresFpuRegister());
5451 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5452 break;
5453
5454 default:
5455 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5456 }
5457}
5458
5459void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5460 Primitive::Type type = instruction->GetType();
5461 LocationSummary* locations = instruction->GetLocations();
5462
5463 switch (type) {
5464 case Primitive::kPrimInt:
5465 case Primitive::kPrimLong: {
5466 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5467 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5468 if (type == Primitive::kPrimInt)
5469 __ Subu(dst, ZERO, src);
5470 else
5471 __ Dsubu(dst, ZERO, src);
5472 break;
5473 }
5474 case Primitive::kPrimFloat:
5475 case Primitive::kPrimDouble: {
5476 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5477 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5478 if (type == Primitive::kPrimFloat)
5479 __ NegS(dst, src);
5480 else
5481 __ NegD(dst, src);
5482 break;
5483 }
5484 default:
5485 LOG(FATAL) << "Unexpected neg type " << type;
5486 }
5487}
5488
5489void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5490 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005492 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005493 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005494 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5495 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005496}
5497
5498void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005499 // Note: if heap poisoning is enabled, the entry point takes care
5500 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005501 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5502 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005503}
5504
5505void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5506 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005507 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005508 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005509 if (instruction->IsStringAlloc()) {
5510 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5511 } else {
5512 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005513 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005514 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5515}
5516
5517void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005518 // Note: if heap poisoning is enabled, the entry point takes care
5519 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005520 if (instruction->IsStringAlloc()) {
5521 // String is allocated through StringFactory. Call NewEmptyString entry point.
5522 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005523 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005524 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005525 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5526 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5527 __ Jalr(T9);
5528 __ Nop();
5529 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5530 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005531 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005532 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005533 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005534}
5535
5536void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5538 locations->SetInAt(0, Location::RequiresRegister());
5539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5540}
5541
5542void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5543 Primitive::Type type = instruction->GetType();
5544 LocationSummary* locations = instruction->GetLocations();
5545
5546 switch (type) {
5547 case Primitive::kPrimInt:
5548 case Primitive::kPrimLong: {
5549 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5550 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5551 __ Nor(dst, src, ZERO);
5552 break;
5553 }
5554
5555 default:
5556 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5557 }
5558}
5559
5560void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5562 locations->SetInAt(0, Location::RequiresRegister());
5563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5564}
5565
5566void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5567 LocationSummary* locations = instruction->GetLocations();
5568 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5569 locations->InAt(0).AsRegister<GpuRegister>(),
5570 1);
5571}
5572
5573void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005574 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5575 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005576}
5577
Calin Juravle2ae48182016-03-16 14:05:09 +00005578void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5579 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005580 return;
5581 }
5582 Location obj = instruction->GetLocations()->InAt(0);
5583
5584 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005585 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005586}
5587
Calin Juravle2ae48182016-03-16 14:05:09 +00005588void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005589 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005590 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005591
5592 Location obj = instruction->GetLocations()->InAt(0);
5593
5594 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5595}
5596
5597void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005598 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005599}
5600
5601void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5602 HandleBinaryOp(instruction);
5603}
5604
5605void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5606 HandleBinaryOp(instruction);
5607}
5608
5609void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5610 LOG(FATAL) << "Unreachable";
5611}
5612
5613void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5614 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5615}
5616
5617void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5619 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5620 if (location.IsStackSlot()) {
5621 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5622 } else if (location.IsDoubleStackSlot()) {
5623 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5624 }
5625 locations->SetOut(location);
5626}
5627
5628void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5629 ATTRIBUTE_UNUSED) {
5630 // Nothing to do, the parameter is already at its location.
5631}
5632
5633void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5634 LocationSummary* locations =
5635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5636 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5637}
5638
5639void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5640 ATTRIBUTE_UNUSED) {
5641 // Nothing to do, the method is already at its location.
5642}
5643
5644void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005646 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005647 locations->SetInAt(i, Location::Any());
5648 }
5649 locations->SetOut(Location::Any());
5650}
5651
5652void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5653 LOG(FATAL) << "Unreachable";
5654}
5655
5656void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5657 Primitive::Type type = rem->GetResultType();
5658 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005659 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5660 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5662
5663 switch (type) {
5664 case Primitive::kPrimInt:
5665 case Primitive::kPrimLong:
5666 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005667 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005668 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5669 break;
5670
5671 case Primitive::kPrimFloat:
5672 case Primitive::kPrimDouble: {
5673 InvokeRuntimeCallingConvention calling_convention;
5674 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5675 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5676 locations->SetOut(calling_convention.GetReturnLocation(type));
5677 break;
5678 }
5679
5680 default:
5681 LOG(FATAL) << "Unexpected rem type " << type;
5682 }
5683}
5684
5685void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5686 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005687
5688 switch (type) {
5689 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005690 case Primitive::kPrimLong:
5691 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005692 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005693
5694 case Primitive::kPrimFloat:
5695 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005696 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5697 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005698 if (type == Primitive::kPrimFloat) {
5699 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5700 } else {
5701 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5702 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005703 break;
5704 }
5705 default:
5706 LOG(FATAL) << "Unexpected rem type " << type;
5707 }
5708}
5709
Igor Murashkind01745e2017-04-05 16:40:31 -07005710void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5711 constructor_fence->SetLocations(nullptr);
5712}
5713
5714void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5715 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5716 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5717}
5718
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5720 memory_barrier->SetLocations(nullptr);
5721}
5722
5723void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5724 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5725}
5726
5727void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5729 Primitive::Type return_type = ret->InputAt(0)->GetType();
5730 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5731}
5732
5733void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5734 codegen_->GenerateFrameExit();
5735}
5736
5737void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5738 ret->SetLocations(nullptr);
5739}
5740
5741void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5742 codegen_->GenerateFrameExit();
5743}
5744
Alexey Frunze92d90602015-12-18 18:16:36 -08005745void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5746 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005747}
5748
Alexey Frunze92d90602015-12-18 18:16:36 -08005749void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5750 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005751}
5752
Alexey Frunze4dda3372015-06-01 18:31:49 -07005753void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5754 HandleShift(shl);
5755}
5756
5757void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5758 HandleShift(shl);
5759}
5760
5761void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5762 HandleShift(shr);
5763}
5764
5765void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5766 HandleShift(shr);
5767}
5768
Alexey Frunze4dda3372015-06-01 18:31:49 -07005769void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5770 HandleBinaryOp(instruction);
5771}
5772
5773void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5774 HandleBinaryOp(instruction);
5775}
5776
5777void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5778 HandleFieldGet(instruction, instruction->GetFieldInfo());
5779}
5780
5781void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5782 HandleFieldGet(instruction, instruction->GetFieldInfo());
5783}
5784
5785void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5786 HandleFieldSet(instruction, instruction->GetFieldInfo());
5787}
5788
5789void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005790 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005791}
5792
Calin Juravlee460d1d2015-09-29 04:52:17 +01005793void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5794 HUnresolvedInstanceFieldGet* instruction) {
5795 FieldAccessCallingConventionMIPS64 calling_convention;
5796 codegen_->CreateUnresolvedFieldLocationSummary(
5797 instruction, instruction->GetFieldType(), calling_convention);
5798}
5799
5800void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5801 HUnresolvedInstanceFieldGet* instruction) {
5802 FieldAccessCallingConventionMIPS64 calling_convention;
5803 codegen_->GenerateUnresolvedFieldAccess(instruction,
5804 instruction->GetFieldType(),
5805 instruction->GetFieldIndex(),
5806 instruction->GetDexPc(),
5807 calling_convention);
5808}
5809
5810void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5811 HUnresolvedInstanceFieldSet* instruction) {
5812 FieldAccessCallingConventionMIPS64 calling_convention;
5813 codegen_->CreateUnresolvedFieldLocationSummary(
5814 instruction, instruction->GetFieldType(), calling_convention);
5815}
5816
5817void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5818 HUnresolvedInstanceFieldSet* instruction) {
5819 FieldAccessCallingConventionMIPS64 calling_convention;
5820 codegen_->GenerateUnresolvedFieldAccess(instruction,
5821 instruction->GetFieldType(),
5822 instruction->GetFieldIndex(),
5823 instruction->GetDexPc(),
5824 calling_convention);
5825}
5826
5827void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5828 HUnresolvedStaticFieldGet* instruction) {
5829 FieldAccessCallingConventionMIPS64 calling_convention;
5830 codegen_->CreateUnresolvedFieldLocationSummary(
5831 instruction, instruction->GetFieldType(), calling_convention);
5832}
5833
5834void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5835 HUnresolvedStaticFieldGet* instruction) {
5836 FieldAccessCallingConventionMIPS64 calling_convention;
5837 codegen_->GenerateUnresolvedFieldAccess(instruction,
5838 instruction->GetFieldType(),
5839 instruction->GetFieldIndex(),
5840 instruction->GetDexPc(),
5841 calling_convention);
5842}
5843
5844void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5845 HUnresolvedStaticFieldSet* instruction) {
5846 FieldAccessCallingConventionMIPS64 calling_convention;
5847 codegen_->CreateUnresolvedFieldLocationSummary(
5848 instruction, instruction->GetFieldType(), calling_convention);
5849}
5850
5851void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5852 HUnresolvedStaticFieldSet* instruction) {
5853 FieldAccessCallingConventionMIPS64 calling_convention;
5854 codegen_->GenerateUnresolvedFieldAccess(instruction,
5855 instruction->GetFieldType(),
5856 instruction->GetFieldIndex(),
5857 instruction->GetDexPc(),
5858 calling_convention);
5859}
5860
Alexey Frunze4dda3372015-06-01 18:31:49 -07005861void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005862 LocationSummary* locations =
5863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02005864 // In suspend check slow path, usually there are no caller-save registers at all.
5865 // If SIMD instructions are present, however, we force spilling all live SIMD
5866 // registers in full width (since the runtime only saves/restores lower part).
5867 locations->SetCustomSlowPathCallerSaves(
5868 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005869}
5870
5871void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5872 HBasicBlock* block = instruction->GetBlock();
5873 if (block->GetLoopInformation() != nullptr) {
5874 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5875 // The back edge will generate the suspend check.
5876 return;
5877 }
5878 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5879 // The goto will generate the suspend check.
5880 return;
5881 }
5882 GenerateSuspendCheck(instruction, nullptr);
5883}
5884
Alexey Frunze4dda3372015-06-01 18:31:49 -07005885void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5886 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005888 InvokeRuntimeCallingConvention calling_convention;
5889 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5890}
5891
5892void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005893 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005894 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5895}
5896
5897void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5898 Primitive::Type input_type = conversion->GetInputType();
5899 Primitive::Type result_type = conversion->GetResultType();
5900 DCHECK_NE(input_type, result_type);
5901
5902 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5903 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5904 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5905 }
5906
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5908
5909 if (Primitive::IsFloatingPointType(input_type)) {
5910 locations->SetInAt(0, Location::RequiresFpuRegister());
5911 } else {
5912 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005913 }
5914
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005915 if (Primitive::IsFloatingPointType(result_type)) {
5916 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005917 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005919 }
5920}
5921
5922void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5923 LocationSummary* locations = conversion->GetLocations();
5924 Primitive::Type result_type = conversion->GetResultType();
5925 Primitive::Type input_type = conversion->GetInputType();
5926
5927 DCHECK_NE(input_type, result_type);
5928
5929 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5930 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5931 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5932
5933 switch (result_type) {
5934 case Primitive::kPrimChar:
5935 __ Andi(dst, src, 0xFFFF);
5936 break;
5937 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005938 if (input_type == Primitive::kPrimLong) {
5939 // Type conversion from long to types narrower than int is a result of code
5940 // transformations. To avoid unpredictable results for SEB and SEH, we first
5941 // need to sign-extend the low 32-bit value into bits 32 through 63.
5942 __ Sll(dst, src, 0);
5943 __ Seb(dst, dst);
5944 } else {
5945 __ Seb(dst, src);
5946 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005947 break;
5948 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005949 if (input_type == Primitive::kPrimLong) {
5950 // Type conversion from long to types narrower than int is a result of code
5951 // transformations. To avoid unpredictable results for SEB and SEH, we first
5952 // need to sign-extend the low 32-bit value into bits 32 through 63.
5953 __ Sll(dst, src, 0);
5954 __ Seh(dst, dst);
5955 } else {
5956 __ Seh(dst, src);
5957 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005958 break;
5959 case Primitive::kPrimInt:
5960 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005961 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5962 // conversions, except when the input and output registers are the same and we are not
5963 // converting longs to shorter types. In these cases, do nothing.
5964 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5965 __ Sll(dst, src, 0);
5966 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005967 break;
5968
5969 default:
5970 LOG(FATAL) << "Unexpected type conversion from " << input_type
5971 << " to " << result_type;
5972 }
5973 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005974 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5975 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5976 if (input_type == Primitive::kPrimLong) {
5977 __ Dmtc1(src, FTMP);
5978 if (result_type == Primitive::kPrimFloat) {
5979 __ Cvtsl(dst, FTMP);
5980 } else {
5981 __ Cvtdl(dst, FTMP);
5982 }
5983 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005984 __ Mtc1(src, FTMP);
5985 if (result_type == Primitive::kPrimFloat) {
5986 __ Cvtsw(dst, FTMP);
5987 } else {
5988 __ Cvtdw(dst, FTMP);
5989 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005990 }
5991 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5992 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005993 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5994 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005995
5996 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00005997 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005998 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005999 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006000 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006001 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006002 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006003 } else {
6004 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006005 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006006 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006007 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006008 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006009 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006010 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006011 } else if (Primitive::IsFloatingPointType(result_type) &&
6012 Primitive::IsFloatingPointType(input_type)) {
6013 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6014 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6015 if (result_type == Primitive::kPrimFloat) {
6016 __ Cvtsd(dst, src);
6017 } else {
6018 __ Cvtds(dst, src);
6019 }
6020 } else {
6021 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6022 << " to " << result_type;
6023 }
6024}
6025
6026void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6027 HandleShift(ushr);
6028}
6029
6030void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6031 HandleShift(ushr);
6032}
6033
6034void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6035 HandleBinaryOp(instruction);
6036}
6037
6038void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6039 HandleBinaryOp(instruction);
6040}
6041
6042void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6043 // Nothing to do, this should be removed during prepare for register allocator.
6044 LOG(FATAL) << "Unreachable";
6045}
6046
6047void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6048 // Nothing to do, this should be removed during prepare for register allocator.
6049 LOG(FATAL) << "Unreachable";
6050}
6051
6052void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006053 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006054}
6055
6056void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006057 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006058}
6059
6060void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006061 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006062}
6063
6064void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006065 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006066}
6067
6068void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006069 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006070}
6071
6072void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006073 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006074}
6075
6076void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006077 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006078}
6079
6080void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006081 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006082}
6083
6084void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006085 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006086}
6087
6088void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006089 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006090}
6091
6092void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006093 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006094}
6095
6096void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006097 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006098}
6099
Aart Bike9f37602015-10-09 11:15:55 -07006100void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006101 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006102}
6103
6104void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006105 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006106}
6107
6108void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006109 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006110}
6111
6112void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006114}
6115
6116void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006118}
6119
6120void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006122}
6123
6124void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006125 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006126}
6127
6128void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006129 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006130}
6131
Mark Mendellfe57faa2015-09-18 09:26:15 -04006132// Simple implementation of packed switch - generate cascaded compare/jumps.
6133void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6134 LocationSummary* locations =
6135 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6136 locations->SetInAt(0, Location::RequiresRegister());
6137}
6138
Alexey Frunze0960ac52016-12-20 17:24:59 -08006139void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6140 int32_t lower_bound,
6141 uint32_t num_entries,
6142 HBasicBlock* switch_block,
6143 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006144 // Create a set of compare/jumps.
6145 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006146 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006147 // Jump to default if index is negative
6148 // Note: We don't check the case that index is positive while value < lower_bound, because in
6149 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6150 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6151
Alexey Frunze0960ac52016-12-20 17:24:59 -08006152 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006153 // Jump to successors[0] if value == lower_bound.
6154 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6155 int32_t last_index = 0;
6156 for (; num_entries - last_index > 2; last_index += 2) {
6157 __ Addiu(temp_reg, temp_reg, -2);
6158 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6159 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6160 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6161 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6162 }
6163 if (num_entries - last_index == 2) {
6164 // The last missing case_value.
6165 __ Addiu(temp_reg, temp_reg, -1);
6166 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006167 }
6168
6169 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006170 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006171 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006172 }
6173}
6174
Alexey Frunze0960ac52016-12-20 17:24:59 -08006175void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6176 int32_t lower_bound,
6177 uint32_t num_entries,
6178 HBasicBlock* switch_block,
6179 HBasicBlock* default_block) {
6180 // Create a jump table.
6181 std::vector<Mips64Label*> labels(num_entries);
6182 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6183 for (uint32_t i = 0; i < num_entries; i++) {
6184 labels[i] = codegen_->GetLabelOf(successors[i]);
6185 }
6186 JumpTable* table = __ CreateJumpTable(std::move(labels));
6187
6188 // Is the value in range?
6189 __ Addiu32(TMP, value_reg, -lower_bound);
6190 __ LoadConst32(AT, num_entries);
6191 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6192
6193 // We are in the range of the table.
6194 // Load the target address from the jump table, indexing by the value.
6195 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006196 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006197 __ Lw(TMP, TMP, 0);
6198 // Compute the absolute target address by adding the table start address
6199 // (the table contains offsets to targets relative to its start).
6200 __ Daddu(TMP, TMP, AT);
6201 // And jump.
6202 __ Jr(TMP);
6203 __ Nop();
6204}
6205
6206void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6207 int32_t lower_bound = switch_instr->GetStartValue();
6208 uint32_t num_entries = switch_instr->GetNumEntries();
6209 LocationSummary* locations = switch_instr->GetLocations();
6210 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6211 HBasicBlock* switch_block = switch_instr->GetBlock();
6212 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6213
6214 if (num_entries > kPackedSwitchJumpTableThreshold) {
6215 GenTableBasedPackedSwitch(value_reg,
6216 lower_bound,
6217 num_entries,
6218 switch_block,
6219 default_block);
6220 } else {
6221 GenPackedSwitchWithCompares(value_reg,
6222 lower_bound,
6223 num_entries,
6224 switch_block,
6225 default_block);
6226 }
6227}
6228
Chris Larsenc9905a62017-03-13 17:06:18 -07006229void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6230 LocationSummary* locations =
6231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6232 locations->SetInAt(0, Location::RequiresRegister());
6233 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006234}
6235
Chris Larsenc9905a62017-03-13 17:06:18 -07006236void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6237 LocationSummary* locations = instruction->GetLocations();
6238 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6239 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6240 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6241 __ LoadFromOffset(kLoadDoubleword,
6242 locations->Out().AsRegister<GpuRegister>(),
6243 locations->InAt(0).AsRegister<GpuRegister>(),
6244 method_offset);
6245 } else {
6246 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6247 instruction->GetIndex(), kMips64PointerSize));
6248 __ LoadFromOffset(kLoadDoubleword,
6249 locations->Out().AsRegister<GpuRegister>(),
6250 locations->InAt(0).AsRegister<GpuRegister>(),
6251 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6252 __ LoadFromOffset(kLoadDoubleword,
6253 locations->Out().AsRegister<GpuRegister>(),
6254 locations->Out().AsRegister<GpuRegister>(),
6255 method_offset);
6256 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006257}
6258
Alexey Frunze4dda3372015-06-01 18:31:49 -07006259} // namespace mips64
6260} // namespace art