blob: e334100e1c5bf81f947daa1d3b4cd2c74376da33 [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 Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
144 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
172 HDivZeroCheck* const instruction_;
173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
187 LocationSummary* locations = at_->GetLocations();
188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
189
190 __ Bind(GetEntryLabel());
191 SaveLiveRegisters(codegen, locations);
192
193 InvokeRuntimeCallingConvention calling_convention;
194 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
195 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
196 : QUICK_ENTRY_POINT(pInitializeType);
197 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
198 if (do_clinit_) {
199 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
200 } else {
201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
202 }
203
204 // Move the class to the desired location.
205 Location out = locations->Out();
206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 Primitive::Type type = at_->GetType();
209 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
210 }
211
212 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700213 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 }
215
Roland Levillain46648892015-06-19 16:07:18 +0100216 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 private:
219 // The class this slow path will load.
220 HLoadClass* const cls_;
221
222 // The instruction where this slow path is happening.
223 // (Might be the load class or an initialization check).
224 HInstruction* const at_;
225
226 // The dex PC of `at_`.
227 const uint32_t dex_pc_;
228
229 // Whether to initialize the class.
230 const bool do_clinit_;
231
232 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
233};
234
235class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
236 public:
237 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = instruction_->GetLocations();
241 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
242 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
243
244 __ Bind(GetEntryLabel());
245 SaveLiveRegisters(codegen, locations);
246
247 InvokeRuntimeCallingConvention calling_convention;
248 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700260 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
266 HLoadString* const instruction_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
269};
270
271class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
272 public:
273 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
276 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
277 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
283 instruction_,
284 instruction_->GetDexPc(),
285 this);
286 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
287 }
288
Alexandre Rames8158f282015-08-07 10:26:17 +0100289 bool IsFatal() const OVERRIDE { return true; }
290
Roland Levillain46648892015-06-19 16:07:18 +0100291 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
292
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 private:
294 HNullCheck* const instruction_;
295
296 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)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302 : instruction_(instruction), successor_(successor) {}
303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
313 RestoreLiveRegisters(codegen, instruction_->GetLocations());
314 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 }
319 }
320
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700321 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 private:
329 HSuspendCheck* const instruction_;
330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 DCHECK(instruction_->IsCheckCast()
348 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
349 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
350
351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, locations);
353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
363
364 if (instruction_->IsInstanceOf()) {
365 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
366 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000369 CheckEntrypointTypes<
370 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 Primitive::Type ret_type = instruction_->GetType();
372 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
373 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 } else {
375 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100376 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
378 }
379
380 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700381 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
387 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
390};
391
392class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
393 public:
394 explicit DeoptimizationSlowPathMIPS64(HInstruction* instruction)
395 : instruction_(instruction) {}
396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
398 __ Bind(GetEntryLabel());
399 SaveLiveRegisters(codegen, instruction_->GetLocations());
400 DCHECK(instruction_->IsDeoptimize());
401 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
402 uint32_t dex_pc = deoptimize->GetDexPc();
403 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
404 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
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:
411 HInstruction* const instruction_;
412 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
413};
414
415CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
416 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100417 const CompilerOptions& compiler_options,
418 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 : CodeGenerator(graph,
420 kNumberOfGpuRegisters,
421 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000422 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
424 arraysize(kCoreCalleeSaves)),
425 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
426 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100427 compiler_options,
428 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100429 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700430 location_builder_(graph, this),
431 instruction_visitor_(graph, this),
432 move_resolver_(graph->GetArena(), this),
433 isa_features_(isa_features) {
434 // Save RA (containing the return address) to mimic Quick.
435 AddAllocatedRegister(Location::RegisterLocation(RA));
436}
437
438#undef __
439#define __ down_cast<Mips64Assembler*>(GetAssembler())->
440#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
441
442void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700443 // Ensure that we fix up branches.
444 __ FinalizeCode();
445
446 // Adjust native pc offsets in stack maps.
447 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
448 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
449 uint32_t new_position = __ GetAdjustedPosition(old_position);
450 DCHECK_GE(new_position, old_position);
451 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
452 }
453
454 // Adjust pc offsets for the disassembly information.
455 if (disasm_info_ != nullptr) {
456 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
457 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
458 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
459 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
460 it.second.start = __ GetAdjustedPosition(it.second.start);
461 it.second.end = __ GetAdjustedPosition(it.second.end);
462 }
463 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
464 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
465 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
466 }
467 }
468
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469 CodeGenerator::Finalize(allocator);
470}
471
472Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
473 return codegen_->GetAssembler();
474}
475
476void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100477 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700478 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
479}
480
481void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100482 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
484}
485
486void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
487 // Pop reg
488 __ Ld(GpuRegister(reg), SP, 0);
489 __ DecreaseFrameSize(kMips64WordSize);
490}
491
492void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
493 // Push reg
494 __ IncreaseFrameSize(kMips64WordSize);
495 __ Sd(GpuRegister(reg), SP, 0);
496}
497
498void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
499 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
500 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
501 // Allocate a scratch register other than TMP, if available.
502 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
503 // automatically unspilled when the scratch scope object is destroyed).
504 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
505 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
506 int stack_offset = ensure_scratch.IsSpilled() ? kMips64WordSize : 0;
507 __ LoadFromOffset(load_type,
508 GpuRegister(ensure_scratch.GetRegister()),
509 SP,
510 index1 + stack_offset);
511 __ LoadFromOffset(load_type,
512 TMP,
513 SP,
514 index2 + stack_offset);
515 __ StoreToOffset(store_type,
516 GpuRegister(ensure_scratch.GetRegister()),
517 SP,
518 index2 + stack_offset);
519 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
520}
521
522static dwarf::Reg DWARFReg(GpuRegister reg) {
523 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
524}
525
526// TODO: mapping of floating-point registers to DWARF
527
528void CodeGeneratorMIPS64::GenerateFrameEntry() {
529 __ Bind(&frame_entry_label_);
530
531 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
532
533 if (do_overflow_check) {
534 __ LoadFromOffset(kLoadWord,
535 ZERO,
536 SP,
537 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
538 RecordPcInfo(nullptr, 0);
539 }
540
541 // TODO: anything related to T9/GP/GOT/PIC/.so's?
542
543 if (HasEmptyFrame()) {
544 return;
545 }
546
547 // Make sure the frame size isn't unreasonably large. Per the various APIs
548 // it looks like it should always be less than 2GB in size, which allows
549 // us using 32-bit signed offsets from the stack pointer.
550 if (GetFrameSize() > 0x7FFFFFFF)
551 LOG(FATAL) << "Stack frame larger than 2GB";
552
553 // Spill callee-saved registers.
554 // Note that their cumulative size is small and they can be indexed using
555 // 16-bit offsets.
556
557 // TODO: increment/decrement SP in one step instead of two or remove this comment.
558
559 uint32_t ofs = FrameEntrySpillSize();
560 __ IncreaseFrameSize(ofs);
561
562 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
563 GpuRegister reg = kCoreCalleeSaves[i];
564 if (allocated_registers_.ContainsCoreRegister(reg)) {
565 ofs -= kMips64WordSize;
566 __ Sd(reg, SP, ofs);
567 __ cfi().RelOffset(DWARFReg(reg), ofs);
568 }
569 }
570
571 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
572 FpuRegister reg = kFpuCalleeSaves[i];
573 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
574 ofs -= kMips64WordSize;
575 __ Sdc1(reg, SP, ofs);
576 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
577 }
578 }
579
580 // Allocate the rest of the frame and store the current method pointer
581 // at its end.
582
583 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
584
585 static_assert(IsInt<16>(kCurrentMethodStackOffset),
586 "kCurrentMethodStackOffset must fit into int16_t");
587 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
588}
589
590void CodeGeneratorMIPS64::GenerateFrameExit() {
591 __ cfi().RememberState();
592
593 // TODO: anything related to T9/GP/GOT/PIC/.so's?
594
595 if (!HasEmptyFrame()) {
596 // Deallocate the rest of the frame.
597
598 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
599
600 // Restore callee-saved registers.
601 // Note that their cumulative size is small and they can be indexed using
602 // 16-bit offsets.
603
604 // TODO: increment/decrement SP in one step instead of two or remove this comment.
605
606 uint32_t ofs = 0;
607
608 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
609 FpuRegister reg = kFpuCalleeSaves[i];
610 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
611 __ Ldc1(reg, SP, ofs);
612 ofs += kMips64WordSize;
613 // TODO: __ cfi().Restore(DWARFReg(reg));
614 }
615 }
616
617 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
618 GpuRegister reg = kCoreCalleeSaves[i];
619 if (allocated_registers_.ContainsCoreRegister(reg)) {
620 __ Ld(reg, SP, ofs);
621 ofs += kMips64WordSize;
622 __ cfi().Restore(DWARFReg(reg));
623 }
624 }
625
626 DCHECK_EQ(ofs, FrameEntrySpillSize());
627 __ DecreaseFrameSize(ofs);
628 }
629
630 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700631 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700632
633 __ cfi().RestoreState();
634 __ cfi().DefCFAOffset(GetFrameSize());
635}
636
637void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
638 __ Bind(GetLabelOf(block));
639}
640
641void CodeGeneratorMIPS64::MoveLocation(Location destination,
642 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 if (source.Equals(destination)) {
645 return;
646 }
647
648 // A valid move can always be inferred from the destination and source
649 // locations. When moving from and to a register, the argument type can be
650 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 DCHECK_EQ(unspecified_type, false);
653
654 if (destination.IsRegister() || destination.IsFpuRegister()) {
655 if (unspecified_type) {
656 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
657 if (source.IsStackSlot() ||
658 (src_cst != nullptr && (src_cst->IsIntConstant()
659 || src_cst->IsFloatConstant()
660 || src_cst->IsNullConstant()))) {
661 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 } else {
664 // If the source is a double stack slot or a 64bit constant, a 64bit
665 // type is appropriate. Else the source is a register, and since the
666 // type has not been specified, we chose a 64bit type to force a 64bit
667 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 }
670 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100671 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
672 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700673 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
674 // Move to GPR/FPR from stack
675 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100676 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700677 __ LoadFpuFromOffset(load_type,
678 destination.AsFpuRegister<FpuRegister>(),
679 SP,
680 source.GetStackIndex());
681 } else {
682 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
683 __ LoadFromOffset(load_type,
684 destination.AsRegister<GpuRegister>(),
685 SP,
686 source.GetStackIndex());
687 }
688 } else if (source.IsConstant()) {
689 // Move to GPR/FPR from constant
690 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 gpr = destination.AsRegister<GpuRegister>();
693 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100694 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700695 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
696 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
697 gpr = ZERO;
698 } else {
699 __ LoadConst32(gpr, value);
700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700702 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
703 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
704 gpr = ZERO;
705 } else {
706 __ LoadConst64(gpr, value);
707 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
713 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700715 if (destination.IsRegister()) {
716 // Move to GPR from GPR
717 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
718 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100719 DCHECK(destination.IsFpuRegister());
720 if (Primitive::Is64BitType(dst_type)) {
721 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
722 } else {
723 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
724 }
725 }
726 } else if (source.IsFpuRegister()) {
727 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100732 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700733 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
734 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100735 } else {
736 DCHECK(destination.IsRegister());
737 if (Primitive::Is64BitType(dst_type)) {
738 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
739 } else {
740 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 }
743 }
744 } else { // The destination is not a register. It must be a stack slot.
745 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
746 if (source.IsRegister() || source.IsFpuRegister()) {
747 if (unspecified_type) {
748 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100749 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700750 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 }
753 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100754 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
755 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700756 // Move to stack from GPR/FPR
757 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
758 if (source.IsRegister()) {
759 __ StoreToOffset(store_type,
760 source.AsRegister<GpuRegister>(),
761 SP,
762 destination.GetStackIndex());
763 } else {
764 __ StoreFpuToOffset(store_type,
765 source.AsFpuRegister<FpuRegister>(),
766 SP,
767 destination.GetStackIndex());
768 }
769 } else if (source.IsConstant()) {
770 // Move to stack from constant
771 HConstant* src_cst = source.GetConstant();
772 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700773 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700774 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
776 if (value != 0) {
777 gpr = TMP;
778 __ LoadConst32(gpr, value);
779 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700780 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700781 DCHECK(destination.IsDoubleStackSlot());
782 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
783 if (value != 0) {
784 gpr = TMP;
785 __ LoadConst64(gpr, value);
786 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700787 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 } else {
790 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
791 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
792 // Move to stack from stack
793 if (destination.IsStackSlot()) {
794 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
795 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
796 } else {
797 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
798 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
799 }
800 }
801 }
802}
803
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700804void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700805 DCHECK(!loc1.IsConstant());
806 DCHECK(!loc2.IsConstant());
807
808 if (loc1.Equals(loc2)) {
809 return;
810 }
811
812 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
813 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
814 bool is_fp_reg1 = loc1.IsFpuRegister();
815 bool is_fp_reg2 = loc2.IsFpuRegister();
816
817 if (loc2.IsRegister() && loc1.IsRegister()) {
818 // Swap 2 GPRs
819 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
820 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
821 __ Move(TMP, r2);
822 __ Move(r2, r1);
823 __ Move(r1, TMP);
824 } else if (is_fp_reg2 && is_fp_reg1) {
825 // Swap 2 FPRs
826 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
827 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700828 if (type == Primitive::kPrimFloat) {
829 __ MovS(FTMP, r1);
830 __ MovS(r1, r2);
831 __ MovS(r2, FTMP);
832 } else {
833 DCHECK_EQ(type, Primitive::kPrimDouble);
834 __ MovD(FTMP, r1);
835 __ MovD(r1, r2);
836 __ MovD(r2, FTMP);
837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700838 } else if (is_slot1 != is_slot2) {
839 // Swap GPR/FPR and stack slot
840 Location reg_loc = is_slot1 ? loc2 : loc1;
841 Location mem_loc = is_slot1 ? loc1 : loc2;
842 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
843 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
844 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
845 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
846 if (reg_loc.IsFpuRegister()) {
847 __ StoreFpuToOffset(store_type,
848 reg_loc.AsFpuRegister<FpuRegister>(),
849 SP,
850 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700851 if (mem_loc.IsStackSlot()) {
852 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
853 } else {
854 DCHECK(mem_loc.IsDoubleStackSlot());
855 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
856 }
857 } else {
858 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
859 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
860 }
861 } else if (is_slot1 && is_slot2) {
862 move_resolver_.Exchange(loc1.GetStackIndex(),
863 loc2.GetStackIndex(),
864 loc1.IsDoubleStackSlot());
865 } else {
866 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
867 }
868}
869
870void CodeGeneratorMIPS64::Move(HInstruction* instruction,
871 Location location,
872 HInstruction* move_for) {
873 LocationSummary* locations = instruction->GetLocations();
874 Primitive::Type type = instruction->GetType();
875 DCHECK_NE(type, Primitive::kPrimVoid);
876
877 if (instruction->IsCurrentMethod()) {
878 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
879 } else if (locations != nullptr && locations->Out().Equals(location)) {
880 return;
881 } else if (instruction->IsIntConstant()
882 || instruction->IsLongConstant()
883 || instruction->IsNullConstant()) {
884 if (location.IsRegister()) {
885 // Move to GPR from constant
886 GpuRegister dst = location.AsRegister<GpuRegister>();
887 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
888 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
889 } else {
890 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
891 }
892 } else {
893 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
894 // Move to stack from constant
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700895 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700896 if (location.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700897 int32_t value = GetInt32ValueOf(instruction->AsConstant());
898 if (value != 0) {
899 gpr = TMP;
900 __ LoadConst32(gpr, value);
901 }
902 __ StoreToOffset(kStoreWord, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700903 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700904 DCHECK(location.IsDoubleStackSlot());
905 int64_t value = instruction->AsLongConstant()->GetValue();
906 if (value != 0) {
907 gpr = TMP;
908 __ LoadConst64(gpr, value);
909 }
910 __ StoreToOffset(kStoreDoubleword, gpr, SP, location.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 }
912 }
913 } else if (instruction->IsTemporary()) {
914 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
915 MoveLocation(location, temp_location, type);
916 } else if (instruction->IsLoadLocal()) {
917 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
918 if (Primitive::Is64BitType(type)) {
919 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
920 } else {
921 MoveLocation(location, Location::StackSlot(stack_slot), type);
922 }
923 } else {
924 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
925 MoveLocation(location, locations->Out(), type);
926 }
927}
928
Calin Juravle175dc732015-08-25 15:42:32 +0100929void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
930 DCHECK(location.IsRegister());
931 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
932}
933
Calin Juravlee460d1d2015-09-29 04:52:17 +0100934void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
935 if (location.IsRegister()) {
936 locations->AddTemp(location);
937 } else {
938 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
939 }
940}
941
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
943 Primitive::Type type = load->GetType();
944
945 switch (type) {
946 case Primitive::kPrimNot:
947 case Primitive::kPrimInt:
948 case Primitive::kPrimFloat:
949 return Location::StackSlot(GetStackSlot(load->GetLocal()));
950
951 case Primitive::kPrimLong:
952 case Primitive::kPrimDouble:
953 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
954
955 case Primitive::kPrimBoolean:
956 case Primitive::kPrimByte:
957 case Primitive::kPrimChar:
958 case Primitive::kPrimShort:
959 case Primitive::kPrimVoid:
960 LOG(FATAL) << "Unexpected type " << type;
961 }
962
963 LOG(FATAL) << "Unreachable";
964 return Location::NoLocation();
965}
966
967void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object, GpuRegister value) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700968 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969 GpuRegister card = AT;
970 GpuRegister temp = TMP;
971 __ Beqzc(value, &done);
972 __ LoadFromOffset(kLoadDoubleword,
973 card,
974 TR,
975 Thread::CardTableOffset<kMips64WordSize>().Int32Value());
976 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
977 __ Daddu(temp, card, temp);
978 __ Sb(card, temp, 0);
979 __ Bind(&done);
980}
981
982void CodeGeneratorMIPS64::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
983 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
984 blocked_core_registers_[ZERO] = true;
985 blocked_core_registers_[K0] = true;
986 blocked_core_registers_[K1] = true;
987 blocked_core_registers_[GP] = true;
988 blocked_core_registers_[SP] = true;
989 blocked_core_registers_[RA] = true;
990
991 // AT and TMP(T8) are used as temporary/scratch registers
992 // (similar to how AT is used by MIPS assemblers).
993 blocked_core_registers_[AT] = true;
994 blocked_core_registers_[TMP] = true;
995 blocked_fpu_registers_[FTMP] = true;
996
997 // Reserve suspend and thread registers.
998 blocked_core_registers_[S0] = true;
999 blocked_core_registers_[TR] = true;
1000
1001 // Reserve T9 for function calls
1002 blocked_core_registers_[T9] = true;
1003
1004 // TODO: review; anything else?
1005
1006 // TODO: make these two for's conditional on is_baseline once
1007 // all the issues with register saving/restoring are sorted out.
1008 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1009 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1010 }
1011
1012 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1013 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1014 }
1015}
1016
1017Location CodeGeneratorMIPS64::AllocateFreeRegister(Primitive::Type type) const {
1018 if (type == Primitive::kPrimVoid) {
1019 LOG(FATAL) << "Unreachable type " << type;
1020 }
1021
1022 if (Primitive::IsFloatingPointType(type)) {
1023 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFpuRegisters);
1024 return Location::FpuRegisterLocation(reg);
1025 } else {
1026 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfGpuRegisters);
1027 return Location::RegisterLocation(reg);
1028 }
1029}
1030
1031size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1032 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
1033 return kMips64WordSize;
1034}
1035
1036size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1037 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
1038 return kMips64WordSize;
1039}
1040
1041size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1042 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
1043 return kMips64WordSize;
1044}
1045
1046size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1047 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
1048 return kMips64WordSize;
1049}
1050
1051void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001052 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001053}
1054
1055void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001056 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001057}
1058
Calin Juravle175dc732015-08-25 15:42:32 +01001059void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1060 HInstruction* instruction,
1061 uint32_t dex_pc,
1062 SlowPathCode* slow_path) {
1063 InvokeRuntime(GetThreadOffset<kMips64WordSize>(entrypoint).Int32Value(),
1064 instruction,
1065 dex_pc,
1066 slow_path);
1067}
1068
Alexey Frunze4dda3372015-06-01 18:31:49 -07001069void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1070 HInstruction* instruction,
1071 uint32_t dex_pc,
1072 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001073 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001074 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1075 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1076 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001077 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001078 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001079}
1080
1081void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1082 GpuRegister class_reg) {
1083 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1084 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1085 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1086 // TODO: barrier needed?
1087 __ Bind(slow_path->GetExitLabel());
1088}
1089
1090void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1091 __ Sync(0); // only stype 0 is supported
1092}
1093
1094void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1095 HBasicBlock* successor) {
1096 SuspendCheckSlowPathMIPS64* slow_path =
1097 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1098 codegen_->AddSlowPath(slow_path);
1099
1100 __ LoadFromOffset(kLoadUnsignedHalfword,
1101 TMP,
1102 TR,
1103 Thread::ThreadFlagsOffset<kMips64WordSize>().Int32Value());
1104 if (successor == nullptr) {
1105 __ Bnezc(TMP, slow_path->GetEntryLabel());
1106 __ Bind(slow_path->GetReturnLabel());
1107 } else {
1108 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001109 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001110 // slow_path will return to GetLabelOf(successor).
1111 }
1112}
1113
1114InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1115 CodeGeneratorMIPS64* codegen)
1116 : HGraphVisitor(graph),
1117 assembler_(codegen->GetAssembler()),
1118 codegen_(codegen) {}
1119
1120void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1121 DCHECK_EQ(instruction->InputCount(), 2U);
1122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1123 Primitive::Type type = instruction->GetResultType();
1124 switch (type) {
1125 case Primitive::kPrimInt:
1126 case Primitive::kPrimLong: {
1127 locations->SetInAt(0, Location::RequiresRegister());
1128 HInstruction* right = instruction->InputAt(1);
1129 bool can_use_imm = false;
1130 if (right->IsConstant()) {
1131 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1132 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1133 can_use_imm = IsUint<16>(imm);
1134 } else if (instruction->IsAdd()) {
1135 can_use_imm = IsInt<16>(imm);
1136 } else {
1137 DCHECK(instruction->IsSub());
1138 can_use_imm = IsInt<16>(-imm);
1139 }
1140 }
1141 if (can_use_imm)
1142 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1143 else
1144 locations->SetInAt(1, Location::RequiresRegister());
1145 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1146 }
1147 break;
1148
1149 case Primitive::kPrimFloat:
1150 case Primitive::kPrimDouble:
1151 locations->SetInAt(0, Location::RequiresFpuRegister());
1152 locations->SetInAt(1, Location::RequiresFpuRegister());
1153 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1154 break;
1155
1156 default:
1157 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1158 }
1159}
1160
1161void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1162 Primitive::Type type = instruction->GetType();
1163 LocationSummary* locations = instruction->GetLocations();
1164
1165 switch (type) {
1166 case Primitive::kPrimInt:
1167 case Primitive::kPrimLong: {
1168 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1169 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1170 Location rhs_location = locations->InAt(1);
1171
1172 GpuRegister rhs_reg = ZERO;
1173 int64_t rhs_imm = 0;
1174 bool use_imm = rhs_location.IsConstant();
1175 if (use_imm) {
1176 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1177 } else {
1178 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1179 }
1180
1181 if (instruction->IsAnd()) {
1182 if (use_imm)
1183 __ Andi(dst, lhs, rhs_imm);
1184 else
1185 __ And(dst, lhs, rhs_reg);
1186 } else if (instruction->IsOr()) {
1187 if (use_imm)
1188 __ Ori(dst, lhs, rhs_imm);
1189 else
1190 __ Or(dst, lhs, rhs_reg);
1191 } else if (instruction->IsXor()) {
1192 if (use_imm)
1193 __ Xori(dst, lhs, rhs_imm);
1194 else
1195 __ Xor(dst, lhs, rhs_reg);
1196 } else if (instruction->IsAdd()) {
1197 if (type == Primitive::kPrimInt) {
1198 if (use_imm)
1199 __ Addiu(dst, lhs, rhs_imm);
1200 else
1201 __ Addu(dst, lhs, rhs_reg);
1202 } else {
1203 if (use_imm)
1204 __ Daddiu(dst, lhs, rhs_imm);
1205 else
1206 __ Daddu(dst, lhs, rhs_reg);
1207 }
1208 } else {
1209 DCHECK(instruction->IsSub());
1210 if (type == Primitive::kPrimInt) {
1211 if (use_imm)
1212 __ Addiu(dst, lhs, -rhs_imm);
1213 else
1214 __ Subu(dst, lhs, rhs_reg);
1215 } else {
1216 if (use_imm)
1217 __ Daddiu(dst, lhs, -rhs_imm);
1218 else
1219 __ Dsubu(dst, lhs, rhs_reg);
1220 }
1221 }
1222 break;
1223 }
1224 case Primitive::kPrimFloat:
1225 case Primitive::kPrimDouble: {
1226 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1227 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1228 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1229 if (instruction->IsAdd()) {
1230 if (type == Primitive::kPrimFloat)
1231 __ AddS(dst, lhs, rhs);
1232 else
1233 __ AddD(dst, lhs, rhs);
1234 } else if (instruction->IsSub()) {
1235 if (type == Primitive::kPrimFloat)
1236 __ SubS(dst, lhs, rhs);
1237 else
1238 __ SubD(dst, lhs, rhs);
1239 } else {
1240 LOG(FATAL) << "Unexpected floating-point binary operation";
1241 }
1242 break;
1243 }
1244 default:
1245 LOG(FATAL) << "Unexpected binary operation type " << type;
1246 }
1247}
1248
1249void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
1250 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1251
1252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1253 Primitive::Type type = instr->GetResultType();
1254 switch (type) {
1255 case Primitive::kPrimInt:
1256 case Primitive::kPrimLong: {
1257 locations->SetInAt(0, Location::RequiresRegister());
1258 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001260 break;
1261 }
1262 default:
1263 LOG(FATAL) << "Unexpected shift type " << type;
1264 }
1265}
1266
1267void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
1268 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1269 LocationSummary* locations = instr->GetLocations();
1270 Primitive::Type type = instr->GetType();
1271
1272 switch (type) {
1273 case Primitive::kPrimInt:
1274 case Primitive::kPrimLong: {
1275 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1276 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1277 Location rhs_location = locations->InAt(1);
1278
1279 GpuRegister rhs_reg = ZERO;
1280 int64_t rhs_imm = 0;
1281 bool use_imm = rhs_location.IsConstant();
1282 if (use_imm) {
1283 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1284 } else {
1285 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1286 }
1287
1288 if (use_imm) {
1289 uint32_t shift_value = (type == Primitive::kPrimInt)
1290 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1291 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1292
1293 if (type == Primitive::kPrimInt) {
1294 if (instr->IsShl()) {
1295 __ Sll(dst, lhs, shift_value);
1296 } else if (instr->IsShr()) {
1297 __ Sra(dst, lhs, shift_value);
1298 } else {
1299 __ Srl(dst, lhs, shift_value);
1300 }
1301 } else {
1302 if (shift_value < 32) {
1303 if (instr->IsShl()) {
1304 __ Dsll(dst, lhs, shift_value);
1305 } else if (instr->IsShr()) {
1306 __ Dsra(dst, lhs, shift_value);
1307 } else {
1308 __ Dsrl(dst, lhs, shift_value);
1309 }
1310 } else {
1311 shift_value -= 32;
1312 if (instr->IsShl()) {
1313 __ Dsll32(dst, lhs, shift_value);
1314 } else if (instr->IsShr()) {
1315 __ Dsra32(dst, lhs, shift_value);
1316 } else {
1317 __ Dsrl32(dst, lhs, shift_value);
1318 }
1319 }
1320 }
1321 } else {
1322 if (type == Primitive::kPrimInt) {
1323 if (instr->IsShl()) {
1324 __ Sllv(dst, lhs, rhs_reg);
1325 } else if (instr->IsShr()) {
1326 __ Srav(dst, lhs, rhs_reg);
1327 } else {
1328 __ Srlv(dst, lhs, rhs_reg);
1329 }
1330 } else {
1331 if (instr->IsShl()) {
1332 __ Dsllv(dst, lhs, rhs_reg);
1333 } else if (instr->IsShr()) {
1334 __ Dsrav(dst, lhs, rhs_reg);
1335 } else {
1336 __ Dsrlv(dst, lhs, rhs_reg);
1337 }
1338 }
1339 }
1340 break;
1341 }
1342 default:
1343 LOG(FATAL) << "Unexpected shift operation type " << type;
1344 }
1345}
1346
1347void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1348 HandleBinaryOp(instruction);
1349}
1350
1351void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1352 HandleBinaryOp(instruction);
1353}
1354
1355void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1356 HandleBinaryOp(instruction);
1357}
1358
1359void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1360 HandleBinaryOp(instruction);
1361}
1362
1363void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1366 locations->SetInAt(0, Location::RequiresRegister());
1367 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1368 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1369 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1370 } else {
1371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1372 }
1373}
1374
1375void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1376 LocationSummary* locations = instruction->GetLocations();
1377 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1378 Location index = locations->InAt(1);
1379 Primitive::Type type = instruction->GetType();
1380
1381 switch (type) {
1382 case Primitive::kPrimBoolean: {
1383 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1384 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1385 if (index.IsConstant()) {
1386 size_t offset =
1387 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1388 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1389 } else {
1390 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1391 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1392 }
1393 break;
1394 }
1395
1396 case Primitive::kPrimByte: {
1397 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1398 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1399 if (index.IsConstant()) {
1400 size_t offset =
1401 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1402 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1403 } else {
1404 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1405 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimShort: {
1411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1412 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1413 if (index.IsConstant()) {
1414 size_t offset =
1415 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1416 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1417 } else {
1418 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1419 __ Daddu(TMP, obj, TMP);
1420 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1421 }
1422 break;
1423 }
1424
1425 case Primitive::kPrimChar: {
1426 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1427 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1428 if (index.IsConstant()) {
1429 size_t offset =
1430 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1431 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1432 } else {
1433 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1434 __ Daddu(TMP, obj, TMP);
1435 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1436 }
1437 break;
1438 }
1439
1440 case Primitive::kPrimInt:
1441 case Primitive::kPrimNot: {
1442 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1444 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1445 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1446 if (index.IsConstant()) {
1447 size_t offset =
1448 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1449 __ LoadFromOffset(load_type, out, obj, offset);
1450 } else {
1451 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1452 __ Daddu(TMP, obj, TMP);
1453 __ LoadFromOffset(load_type, out, TMP, data_offset);
1454 }
1455 break;
1456 }
1457
1458 case Primitive::kPrimLong: {
1459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1460 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1461 if (index.IsConstant()) {
1462 size_t offset =
1463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1464 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1465 } else {
1466 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1467 __ Daddu(TMP, obj, TMP);
1468 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1469 }
1470 break;
1471 }
1472
1473 case Primitive::kPrimFloat: {
1474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1475 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1476 if (index.IsConstant()) {
1477 size_t offset =
1478 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1479 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1480 } else {
1481 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1482 __ Daddu(TMP, obj, TMP);
1483 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1484 }
1485 break;
1486 }
1487
1488 case Primitive::kPrimDouble: {
1489 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1490 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1491 if (index.IsConstant()) {
1492 size_t offset =
1493 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1494 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1495 } else {
1496 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1497 __ Daddu(TMP, obj, TMP);
1498 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1499 }
1500 break;
1501 }
1502
1503 case Primitive::kPrimVoid:
1504 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1505 UNREACHABLE();
1506 }
1507 codegen_->MaybeRecordImplicitNullCheck(instruction);
1508}
1509
1510void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1512 locations->SetInAt(0, Location::RequiresRegister());
1513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1514}
1515
1516void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1517 LocationSummary* locations = instruction->GetLocations();
1518 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1519 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1520 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1521 __ LoadFromOffset(kLoadWord, out, obj, offset);
1522 codegen_->MaybeRecordImplicitNullCheck(instruction);
1523}
1524
1525void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001526 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001527 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1528 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001529 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1530 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001531 InvokeRuntimeCallingConvention calling_convention;
1532 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1533 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1534 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1535 } else {
1536 locations->SetInAt(0, Location::RequiresRegister());
1537 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1538 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1539 locations->SetInAt(2, Location::RequiresFpuRegister());
1540 } else {
1541 locations->SetInAt(2, Location::RequiresRegister());
1542 }
1543 }
1544}
1545
1546void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1547 LocationSummary* locations = instruction->GetLocations();
1548 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1549 Location index = locations->InAt(1);
1550 Primitive::Type value_type = instruction->GetComponentType();
1551 bool needs_runtime_call = locations->WillCall();
1552 bool needs_write_barrier =
1553 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1554
1555 switch (value_type) {
1556 case Primitive::kPrimBoolean:
1557 case Primitive::kPrimByte: {
1558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1559 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1560 if (index.IsConstant()) {
1561 size_t offset =
1562 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1563 __ StoreToOffset(kStoreByte, value, obj, offset);
1564 } else {
1565 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1566 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1567 }
1568 break;
1569 }
1570
1571 case Primitive::kPrimShort:
1572 case Primitive::kPrimChar: {
1573 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1574 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1575 if (index.IsConstant()) {
1576 size_t offset =
1577 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1578 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1579 } else {
1580 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1581 __ Daddu(TMP, obj, TMP);
1582 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1583 }
1584 break;
1585 }
1586
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimNot: {
1589 if (!needs_runtime_call) {
1590 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1591 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1592 if (index.IsConstant()) {
1593 size_t offset =
1594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1595 __ StoreToOffset(kStoreWord, value, obj, offset);
1596 } else {
1597 DCHECK(index.IsRegister()) << index;
1598 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1599 __ Daddu(TMP, obj, TMP);
1600 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1601 }
1602 codegen_->MaybeRecordImplicitNullCheck(instruction);
1603 if (needs_write_barrier) {
1604 DCHECK_EQ(value_type, Primitive::kPrimNot);
1605 codegen_->MarkGCCard(obj, value);
1606 }
1607 } else {
1608 DCHECK_EQ(value_type, Primitive::kPrimNot);
1609 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1610 instruction,
1611 instruction->GetDexPc(),
1612 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001613 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001614 }
1615 break;
1616 }
1617
1618 case Primitive::kPrimLong: {
1619 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1620 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1621 if (index.IsConstant()) {
1622 size_t offset =
1623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1624 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1625 } else {
1626 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1627 __ Daddu(TMP, obj, TMP);
1628 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1629 }
1630 break;
1631 }
1632
1633 case Primitive::kPrimFloat: {
1634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1635 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1636 DCHECK(locations->InAt(2).IsFpuRegister());
1637 if (index.IsConstant()) {
1638 size_t offset =
1639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1640 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1641 } else {
1642 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1643 __ Daddu(TMP, obj, TMP);
1644 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1645 }
1646 break;
1647 }
1648
1649 case Primitive::kPrimDouble: {
1650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1651 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1652 DCHECK(locations->InAt(2).IsFpuRegister());
1653 if (index.IsConstant()) {
1654 size_t offset =
1655 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1656 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1657 } else {
1658 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1659 __ Daddu(TMP, obj, TMP);
1660 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1661 }
1662 break;
1663 }
1664
1665 case Primitive::kPrimVoid:
1666 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1667 UNREACHABLE();
1668 }
1669
1670 // Ints and objects are handled in the switch.
1671 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1672 codegen_->MaybeRecordImplicitNullCheck(instruction);
1673 }
1674}
1675
1676void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001677 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1678 ? LocationSummary::kCallOnSlowPath
1679 : LocationSummary::kNoCall;
1680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 locations->SetInAt(0, Location::RequiresRegister());
1682 locations->SetInAt(1, Location::RequiresRegister());
1683 if (instruction->HasUses()) {
1684 locations->SetOut(Location::SameAsFirstInput());
1685 }
1686}
1687
1688void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1689 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001690 BoundsCheckSlowPathMIPS64* slow_path =
1691 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 codegen_->AddSlowPath(slow_path);
1693
1694 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1695 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1696
1697 // length is limited by the maximum positive signed 32-bit integer.
1698 // Unsigned comparison of length and index checks for index < 0
1699 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001700 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701}
1702
1703void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1705 instruction,
1706 LocationSummary::kCallOnSlowPath);
1707 locations->SetInAt(0, Location::RequiresRegister());
1708 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001709 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710 locations->AddTemp(Location::RequiresRegister());
1711}
1712
1713void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1714 LocationSummary* locations = instruction->GetLocations();
1715 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1716 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1717 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1718
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001719 SlowPathCodeMIPS64* slow_path =
1720 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001721 codegen_->AddSlowPath(slow_path);
1722
1723 // TODO: avoid this check if we know obj is not null.
1724 __ Beqzc(obj, slow_path->GetExitLabel());
1725 // Compare the class of `obj` with `cls`.
1726 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1727 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1728 __ Bind(slow_path->GetExitLabel());
1729}
1730
1731void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1732 LocationSummary* locations =
1733 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1734 locations->SetInAt(0, Location::RequiresRegister());
1735 if (check->HasUses()) {
1736 locations->SetOut(Location::SameAsFirstInput());
1737 }
1738}
1739
1740void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1741 // We assume the class is not null.
1742 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1743 check->GetLoadClass(),
1744 check,
1745 check->GetDexPc(),
1746 true);
1747 codegen_->AddSlowPath(slow_path);
1748 GenerateClassInitializationCheck(slow_path,
1749 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1750}
1751
1752void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1753 Primitive::Type in_type = compare->InputAt(0)->GetType();
1754
Alexey Frunze299a9392015-12-08 16:08:02 -08001755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756
1757 switch (in_type) {
1758 case Primitive::kPrimLong:
1759 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001760 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1762 break;
1763
1764 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001765 case Primitive::kPrimDouble:
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetInAt(1, Location::RequiresFpuRegister());
1768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001769 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770
1771 default:
1772 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1773 }
1774}
1775
1776void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1777 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001778 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08001780 bool gt_bias = instruction->IsGtBias();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781
1782 // 0 if: left == right
1783 // 1 if: left > right
1784 // -1 if: left < right
1785 switch (in_type) {
1786 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001788 Location rhs_location = locations->InAt(1);
1789 bool use_imm = rhs_location.IsConstant();
1790 GpuRegister rhs = ZERO;
1791 if (use_imm) {
1792 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1793 if (value != 0) {
1794 rhs = AT;
1795 __ LoadConst64(rhs, value);
1796 }
1797 } else {
1798 rhs = rhs_location.AsRegister<GpuRegister>();
1799 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001800 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001801 __ Slt(res, rhs, lhs);
1802 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 break;
1804 }
1805
Alexey Frunze299a9392015-12-08 16:08:02 -08001806 case Primitive::kPrimFloat: {
1807 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1808 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1809 Mips64Label done;
1810 __ CmpEqS(FTMP, lhs, rhs);
1811 __ LoadConst32(res, 0);
1812 __ Bc1nez(FTMP, &done);
1813 if (gt_bias) {
1814 __ CmpLtS(FTMP, lhs, rhs);
1815 __ LoadConst32(res, -1);
1816 __ Bc1nez(FTMP, &done);
1817 __ LoadConst32(res, 1);
1818 } else {
1819 __ CmpLtS(FTMP, rhs, lhs);
1820 __ LoadConst32(res, 1);
1821 __ Bc1nez(FTMP, &done);
1822 __ LoadConst32(res, -1);
1823 }
1824 __ Bind(&done);
1825 break;
1826 }
1827
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001829 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1830 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1831 Mips64Label done;
1832 __ CmpEqD(FTMP, lhs, rhs);
1833 __ LoadConst32(res, 0);
1834 __ Bc1nez(FTMP, &done);
1835 if (gt_bias) {
1836 __ CmpLtD(FTMP, lhs, rhs);
1837 __ LoadConst32(res, -1);
1838 __ Bc1nez(FTMP, &done);
1839 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001840 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001841 __ CmpLtD(FTMP, rhs, lhs);
1842 __ LoadConst32(res, 1);
1843 __ Bc1nez(FTMP, &done);
1844 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001846 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001847 break;
1848 }
1849
1850 default:
1851 LOG(FATAL) << "Unimplemented compare type " << in_type;
1852 }
1853}
1854
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001857 switch (instruction->InputAt(0)->GetType()) {
1858 default:
1859 case Primitive::kPrimLong:
1860 locations->SetInAt(0, Location::RequiresRegister());
1861 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1862 break;
1863
1864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble:
1866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetInAt(1, Location::RequiresFpuRegister());
1868 break;
1869 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001870 if (instruction->NeedsMaterialization()) {
1871 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1872 }
1873}
1874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001875void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001876 if (!instruction->NeedsMaterialization()) {
1877 return;
1878 }
1879
Alexey Frunze299a9392015-12-08 16:08:02 -08001880 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001881 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001882 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001883 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001884
Alexey Frunze299a9392015-12-08 16:08:02 -08001885 switch (type) {
1886 default:
1887 // Integer case.
1888 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1889 return;
1890 case Primitive::kPrimLong:
1891 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1892 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893
Alexey Frunze299a9392015-12-08 16:08:02 -08001894 case Primitive::kPrimFloat:
1895 case Primitive::kPrimDouble:
1896 // TODO: don't use branches.
1897 GenerateFpCompareAndBranch(instruction->GetCondition(),
1898 instruction->IsGtBias(),
1899 type,
1900 locations,
1901 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001902 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001904
1905 // Convert the branches into the result.
1906 Mips64Label done;
1907
1908 // False case: result = 0.
1909 __ LoadConst32(dst, 0);
1910 __ Bc(&done);
1911
1912 // True case: result = 1.
1913 __ Bind(&true_label);
1914 __ LoadConst32(dst, 1);
1915 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916}
1917
Alexey Frunzec857c742015-09-23 15:12:39 -07001918void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1919 DCHECK(instruction->IsDiv() || instruction->IsRem());
1920 Primitive::Type type = instruction->GetResultType();
1921
1922 LocationSummary* locations = instruction->GetLocations();
1923 Location second = locations->InAt(1);
1924 DCHECK(second.IsConstant());
1925
1926 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1927 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1928 int64_t imm = Int64FromConstant(second.GetConstant());
1929 DCHECK(imm == 1 || imm == -1);
1930
1931 if (instruction->IsRem()) {
1932 __ Move(out, ZERO);
1933 } else {
1934 if (imm == -1) {
1935 if (type == Primitive::kPrimInt) {
1936 __ Subu(out, ZERO, dividend);
1937 } else {
1938 DCHECK_EQ(type, Primitive::kPrimLong);
1939 __ Dsubu(out, ZERO, dividend);
1940 }
1941 } else if (out != dividend) {
1942 __ Move(out, dividend);
1943 }
1944 }
1945}
1946
1947void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1948 DCHECK(instruction->IsDiv() || instruction->IsRem());
1949 Primitive::Type type = instruction->GetResultType();
1950
1951 LocationSummary* locations = instruction->GetLocations();
1952 Location second = locations->InAt(1);
1953 DCHECK(second.IsConstant());
1954
1955 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1956 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1957 int64_t imm = Int64FromConstant(second.GetConstant());
1958 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
1959 DCHECK(IsPowerOfTwo(abs_imm));
1960 int ctz_imm = CTZ(abs_imm);
1961
1962 if (instruction->IsDiv()) {
1963 if (type == Primitive::kPrimInt) {
1964 if (ctz_imm == 1) {
1965 // Fast path for division by +/-2, which is very common.
1966 __ Srl(TMP, dividend, 31);
1967 } else {
1968 __ Sra(TMP, dividend, 31);
1969 __ Srl(TMP, TMP, 32 - ctz_imm);
1970 }
1971 __ Addu(out, dividend, TMP);
1972 __ Sra(out, out, ctz_imm);
1973 if (imm < 0) {
1974 __ Subu(out, ZERO, out);
1975 }
1976 } else {
1977 DCHECK_EQ(type, Primitive::kPrimLong);
1978 if (ctz_imm == 1) {
1979 // Fast path for division by +/-2, which is very common.
1980 __ Dsrl32(TMP, dividend, 31);
1981 } else {
1982 __ Dsra32(TMP, dividend, 31);
1983 if (ctz_imm > 32) {
1984 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1985 } else {
1986 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1987 }
1988 }
1989 __ Daddu(out, dividend, TMP);
1990 if (ctz_imm < 32) {
1991 __ Dsra(out, out, ctz_imm);
1992 } else {
1993 __ Dsra32(out, out, ctz_imm - 32);
1994 }
1995 if (imm < 0) {
1996 __ Dsubu(out, ZERO, out);
1997 }
1998 }
1999 } else {
2000 if (type == Primitive::kPrimInt) {
2001 if (ctz_imm == 1) {
2002 // Fast path for modulo +/-2, which is very common.
2003 __ Sra(TMP, dividend, 31);
2004 __ Subu(out, dividend, TMP);
2005 __ Andi(out, out, 1);
2006 __ Addu(out, out, TMP);
2007 } else {
2008 __ Sra(TMP, dividend, 31);
2009 __ Srl(TMP, TMP, 32 - ctz_imm);
2010 __ Addu(out, dividend, TMP);
2011 if (IsUint<16>(abs_imm - 1)) {
2012 __ Andi(out, out, abs_imm - 1);
2013 } else {
2014 __ Sll(out, out, 32 - ctz_imm);
2015 __ Srl(out, out, 32 - ctz_imm);
2016 }
2017 __ Subu(out, out, TMP);
2018 }
2019 } else {
2020 DCHECK_EQ(type, Primitive::kPrimLong);
2021 if (ctz_imm == 1) {
2022 // Fast path for modulo +/-2, which is very common.
2023 __ Dsra32(TMP, dividend, 31);
2024 __ Dsubu(out, dividend, TMP);
2025 __ Andi(out, out, 1);
2026 __ Daddu(out, out, TMP);
2027 } else {
2028 __ Dsra32(TMP, dividend, 31);
2029 if (ctz_imm > 32) {
2030 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2031 } else {
2032 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2033 }
2034 __ Daddu(out, dividend, TMP);
2035 if (IsUint<16>(abs_imm - 1)) {
2036 __ Andi(out, out, abs_imm - 1);
2037 } else {
2038 if (ctz_imm > 32) {
2039 __ Dsll(out, out, 64 - ctz_imm);
2040 __ Dsrl(out, out, 64 - ctz_imm);
2041 } else {
2042 __ Dsll32(out, out, 32 - ctz_imm);
2043 __ Dsrl32(out, out, 32 - ctz_imm);
2044 }
2045 }
2046 __ Dsubu(out, out, TMP);
2047 }
2048 }
2049 }
2050}
2051
2052void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2053 DCHECK(instruction->IsDiv() || instruction->IsRem());
2054
2055 LocationSummary* locations = instruction->GetLocations();
2056 Location second = locations->InAt(1);
2057 DCHECK(second.IsConstant());
2058
2059 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2060 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2061 int64_t imm = Int64FromConstant(second.GetConstant());
2062
2063 Primitive::Type type = instruction->GetResultType();
2064 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2065
2066 int64_t magic;
2067 int shift;
2068 CalculateMagicAndShiftForDivRem(imm,
2069 (type == Primitive::kPrimLong),
2070 &magic,
2071 &shift);
2072
2073 if (type == Primitive::kPrimInt) {
2074 __ LoadConst32(TMP, magic);
2075 __ MuhR6(TMP, dividend, TMP);
2076
2077 if (imm > 0 && magic < 0) {
2078 __ Addu(TMP, TMP, dividend);
2079 } else if (imm < 0 && magic > 0) {
2080 __ Subu(TMP, TMP, dividend);
2081 }
2082
2083 if (shift != 0) {
2084 __ Sra(TMP, TMP, shift);
2085 }
2086
2087 if (instruction->IsDiv()) {
2088 __ Sra(out, TMP, 31);
2089 __ Subu(out, TMP, out);
2090 } else {
2091 __ Sra(AT, TMP, 31);
2092 __ Subu(AT, TMP, AT);
2093 __ LoadConst32(TMP, imm);
2094 __ MulR6(TMP, AT, TMP);
2095 __ Subu(out, dividend, TMP);
2096 }
2097 } else {
2098 __ LoadConst64(TMP, magic);
2099 __ Dmuh(TMP, dividend, TMP);
2100
2101 if (imm > 0 && magic < 0) {
2102 __ Daddu(TMP, TMP, dividend);
2103 } else if (imm < 0 && magic > 0) {
2104 __ Dsubu(TMP, TMP, dividend);
2105 }
2106
2107 if (shift >= 32) {
2108 __ Dsra32(TMP, TMP, shift - 32);
2109 } else if (shift > 0) {
2110 __ Dsra(TMP, TMP, shift);
2111 }
2112
2113 if (instruction->IsDiv()) {
2114 __ Dsra32(out, TMP, 31);
2115 __ Dsubu(out, TMP, out);
2116 } else {
2117 __ Dsra32(AT, TMP, 31);
2118 __ Dsubu(AT, TMP, AT);
2119 __ LoadConst64(TMP, imm);
2120 __ Dmul(TMP, AT, TMP);
2121 __ Dsubu(out, dividend, TMP);
2122 }
2123 }
2124}
2125
2126void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2127 DCHECK(instruction->IsDiv() || instruction->IsRem());
2128 Primitive::Type type = instruction->GetResultType();
2129 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2130
2131 LocationSummary* locations = instruction->GetLocations();
2132 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2133 Location second = locations->InAt(1);
2134
2135 if (second.IsConstant()) {
2136 int64_t imm = Int64FromConstant(second.GetConstant());
2137 if (imm == 0) {
2138 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2139 } else if (imm == 1 || imm == -1) {
2140 DivRemOneOrMinusOne(instruction);
2141 } else if (IsPowerOfTwo(std::abs(imm))) {
2142 DivRemByPowerOfTwo(instruction);
2143 } else {
2144 DCHECK(imm <= -2 || imm >= 2);
2145 GenerateDivRemWithAnyConstant(instruction);
2146 }
2147 } else {
2148 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2149 GpuRegister divisor = second.AsRegister<GpuRegister>();
2150 if (instruction->IsDiv()) {
2151 if (type == Primitive::kPrimInt)
2152 __ DivR6(out, dividend, divisor);
2153 else
2154 __ Ddiv(out, dividend, divisor);
2155 } else {
2156 if (type == Primitive::kPrimInt)
2157 __ ModR6(out, dividend, divisor);
2158 else
2159 __ Dmod(out, dividend, divisor);
2160 }
2161 }
2162}
2163
Alexey Frunze4dda3372015-06-01 18:31:49 -07002164void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2165 LocationSummary* locations =
2166 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2167 switch (div->GetResultType()) {
2168 case Primitive::kPrimInt:
2169 case Primitive::kPrimLong:
2170 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002171 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2173 break;
2174
2175 case Primitive::kPrimFloat:
2176 case Primitive::kPrimDouble:
2177 locations->SetInAt(0, Location::RequiresFpuRegister());
2178 locations->SetInAt(1, Location::RequiresFpuRegister());
2179 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2180 break;
2181
2182 default:
2183 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2184 }
2185}
2186
2187void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2188 Primitive::Type type = instruction->GetType();
2189 LocationSummary* locations = instruction->GetLocations();
2190
2191 switch (type) {
2192 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002193 case Primitive::kPrimLong:
2194 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196 case Primitive::kPrimFloat:
2197 case Primitive::kPrimDouble: {
2198 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2199 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2200 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2201 if (type == Primitive::kPrimFloat)
2202 __ DivS(dst, lhs, rhs);
2203 else
2204 __ DivD(dst, lhs, rhs);
2205 break;
2206 }
2207 default:
2208 LOG(FATAL) << "Unexpected div type " << type;
2209 }
2210}
2211
2212void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002213 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2214 ? LocationSummary::kCallOnSlowPath
2215 : LocationSummary::kNoCall;
2216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002217 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2218 if (instruction->HasUses()) {
2219 locations->SetOut(Location::SameAsFirstInput());
2220 }
2221}
2222
2223void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2224 SlowPathCodeMIPS64* slow_path =
2225 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2226 codegen_->AddSlowPath(slow_path);
2227 Location value = instruction->GetLocations()->InAt(0);
2228
2229 Primitive::Type type = instruction->GetType();
2230
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002231 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002232 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002233 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002234 }
2235
2236 if (value.IsConstant()) {
2237 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2238 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002239 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002240 } else {
2241 // A division by a non-null constant is valid. We don't need to perform
2242 // any check, so simply fall through.
2243 }
2244 } else {
2245 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2246 }
2247}
2248
2249void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2250 LocationSummary* locations =
2251 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2252 locations->SetOut(Location::ConstantLocation(constant));
2253}
2254
2255void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2256 // Will be generated at use site.
2257}
2258
2259void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2260 exit->SetLocations(nullptr);
2261}
2262
2263void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2264}
2265
2266void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2269 locations->SetOut(Location::ConstantLocation(constant));
2270}
2271
2272void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2273 // Will be generated at use site.
2274}
2275
David Brazdilfc6a86a2015-06-26 10:33:45 +00002276void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002277 DCHECK(!successor->IsExitBlock());
2278 HBasicBlock* block = got->GetBlock();
2279 HInstruction* previous = got->GetPrevious();
2280 HLoopInformation* info = block->GetLoopInformation();
2281
2282 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2283 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2284 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2285 return;
2286 }
2287 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2288 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2289 }
2290 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002291 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002292 }
2293}
2294
David Brazdilfc6a86a2015-06-26 10:33:45 +00002295void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2296 got->SetLocations(nullptr);
2297}
2298
2299void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2300 HandleGoto(got, got->GetSuccessor());
2301}
2302
2303void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2304 try_boundary->SetLocations(nullptr);
2305}
2306
2307void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2308 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2309 if (!successor->IsExitBlock()) {
2310 HandleGoto(try_boundary, successor);
2311 }
2312}
2313
Alexey Frunze299a9392015-12-08 16:08:02 -08002314void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2315 bool is64bit,
2316 LocationSummary* locations) {
2317 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2318 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2319 Location rhs_location = locations->InAt(1);
2320 GpuRegister rhs_reg = ZERO;
2321 int64_t rhs_imm = 0;
2322 bool use_imm = rhs_location.IsConstant();
2323 if (use_imm) {
2324 if (is64bit) {
2325 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2326 } else {
2327 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2328 }
2329 } else {
2330 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2331 }
2332 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2333
2334 switch (cond) {
2335 case kCondEQ:
2336 case kCondNE:
2337 if (use_imm && IsUint<16>(rhs_imm)) {
2338 __ Xori(dst, lhs, rhs_imm);
2339 } else {
2340 if (use_imm) {
2341 rhs_reg = TMP;
2342 __ LoadConst64(rhs_reg, rhs_imm);
2343 }
2344 __ Xor(dst, lhs, rhs_reg);
2345 }
2346 if (cond == kCondEQ) {
2347 __ Sltiu(dst, dst, 1);
2348 } else {
2349 __ Sltu(dst, ZERO, dst);
2350 }
2351 break;
2352
2353 case kCondLT:
2354 case kCondGE:
2355 if (use_imm && IsInt<16>(rhs_imm)) {
2356 __ Slti(dst, lhs, rhs_imm);
2357 } else {
2358 if (use_imm) {
2359 rhs_reg = TMP;
2360 __ LoadConst64(rhs_reg, rhs_imm);
2361 }
2362 __ Slt(dst, lhs, rhs_reg);
2363 }
2364 if (cond == kCondGE) {
2365 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2366 // only the slt instruction but no sge.
2367 __ Xori(dst, dst, 1);
2368 }
2369 break;
2370
2371 case kCondLE:
2372 case kCondGT:
2373 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2374 // Simulate lhs <= rhs via lhs < rhs + 1.
2375 __ Slti(dst, lhs, rhs_imm_plus_one);
2376 if (cond == kCondGT) {
2377 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2378 // only the slti instruction but no sgti.
2379 __ Xori(dst, dst, 1);
2380 }
2381 } else {
2382 if (use_imm) {
2383 rhs_reg = TMP;
2384 __ LoadConst64(rhs_reg, rhs_imm);
2385 }
2386 __ Slt(dst, rhs_reg, lhs);
2387 if (cond == kCondLE) {
2388 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2389 // only the slt instruction but no sle.
2390 __ Xori(dst, dst, 1);
2391 }
2392 }
2393 break;
2394
2395 case kCondB:
2396 case kCondAE:
2397 if (use_imm && IsInt<16>(rhs_imm)) {
2398 // Sltiu sign-extends its 16-bit immediate operand before
2399 // the comparison and thus lets us compare directly with
2400 // unsigned values in the ranges [0, 0x7fff] and
2401 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2402 __ Sltiu(dst, lhs, rhs_imm);
2403 } else {
2404 if (use_imm) {
2405 rhs_reg = TMP;
2406 __ LoadConst64(rhs_reg, rhs_imm);
2407 }
2408 __ Sltu(dst, lhs, rhs_reg);
2409 }
2410 if (cond == kCondAE) {
2411 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2412 // only the sltu instruction but no sgeu.
2413 __ Xori(dst, dst, 1);
2414 }
2415 break;
2416
2417 case kCondBE:
2418 case kCondA:
2419 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2420 // Simulate lhs <= rhs via lhs < rhs + 1.
2421 // Note that this only works if rhs + 1 does not overflow
2422 // to 0, hence the check above.
2423 // Sltiu sign-extends its 16-bit immediate operand before
2424 // the comparison and thus lets us compare directly with
2425 // unsigned values in the ranges [0, 0x7fff] and
2426 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2427 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2428 if (cond == kCondA) {
2429 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2430 // only the sltiu instruction but no sgtiu.
2431 __ Xori(dst, dst, 1);
2432 }
2433 } else {
2434 if (use_imm) {
2435 rhs_reg = TMP;
2436 __ LoadConst64(rhs_reg, rhs_imm);
2437 }
2438 __ Sltu(dst, rhs_reg, lhs);
2439 if (cond == kCondBE) {
2440 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2441 // only the sltu instruction but no sleu.
2442 __ Xori(dst, dst, 1);
2443 }
2444 }
2445 break;
2446 }
2447}
2448
2449void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2450 bool is64bit,
2451 LocationSummary* locations,
2452 Mips64Label* label) {
2453 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2454 Location rhs_location = locations->InAt(1);
2455 GpuRegister rhs_reg = ZERO;
2456 int64_t rhs_imm = 0;
2457 bool use_imm = rhs_location.IsConstant();
2458 if (use_imm) {
2459 if (is64bit) {
2460 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2461 } else {
2462 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2463 }
2464 } else {
2465 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2466 }
2467
2468 if (use_imm && rhs_imm == 0) {
2469 switch (cond) {
2470 case kCondEQ:
2471 case kCondBE: // <= 0 if zero
2472 __ Beqzc(lhs, label);
2473 break;
2474 case kCondNE:
2475 case kCondA: // > 0 if non-zero
2476 __ Bnezc(lhs, label);
2477 break;
2478 case kCondLT:
2479 __ Bltzc(lhs, label);
2480 break;
2481 case kCondGE:
2482 __ Bgezc(lhs, label);
2483 break;
2484 case kCondLE:
2485 __ Blezc(lhs, label);
2486 break;
2487 case kCondGT:
2488 __ Bgtzc(lhs, label);
2489 break;
2490 case kCondB: // always false
2491 break;
2492 case kCondAE: // always true
2493 __ Bc(label);
2494 break;
2495 }
2496 } else {
2497 if (use_imm) {
2498 rhs_reg = TMP;
2499 __ LoadConst64(rhs_reg, rhs_imm);
2500 }
2501 switch (cond) {
2502 case kCondEQ:
2503 __ Beqc(lhs, rhs_reg, label);
2504 break;
2505 case kCondNE:
2506 __ Bnec(lhs, rhs_reg, label);
2507 break;
2508 case kCondLT:
2509 __ Bltc(lhs, rhs_reg, label);
2510 break;
2511 case kCondGE:
2512 __ Bgec(lhs, rhs_reg, label);
2513 break;
2514 case kCondLE:
2515 __ Bgec(rhs_reg, lhs, label);
2516 break;
2517 case kCondGT:
2518 __ Bltc(rhs_reg, lhs, label);
2519 break;
2520 case kCondB:
2521 __ Bltuc(lhs, rhs_reg, label);
2522 break;
2523 case kCondAE:
2524 __ Bgeuc(lhs, rhs_reg, label);
2525 break;
2526 case kCondBE:
2527 __ Bgeuc(rhs_reg, lhs, label);
2528 break;
2529 case kCondA:
2530 __ Bltuc(rhs_reg, lhs, label);
2531 break;
2532 }
2533 }
2534}
2535
2536void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2537 bool gt_bias,
2538 Primitive::Type type,
2539 LocationSummary* locations,
2540 Mips64Label* label) {
2541 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2542 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2543 if (type == Primitive::kPrimFloat) {
2544 switch (cond) {
2545 case kCondEQ:
2546 __ CmpEqS(FTMP, lhs, rhs);
2547 __ Bc1nez(FTMP, label);
2548 break;
2549 case kCondNE:
2550 __ CmpEqS(FTMP, lhs, rhs);
2551 __ Bc1eqz(FTMP, label);
2552 break;
2553 case kCondLT:
2554 if (gt_bias) {
2555 __ CmpLtS(FTMP, lhs, rhs);
2556 } else {
2557 __ CmpUltS(FTMP, lhs, rhs);
2558 }
2559 __ Bc1nez(FTMP, label);
2560 break;
2561 case kCondLE:
2562 if (gt_bias) {
2563 __ CmpLeS(FTMP, lhs, rhs);
2564 } else {
2565 __ CmpUleS(FTMP, lhs, rhs);
2566 }
2567 __ Bc1nez(FTMP, label);
2568 break;
2569 case kCondGT:
2570 if (gt_bias) {
2571 __ CmpUltS(FTMP, rhs, lhs);
2572 } else {
2573 __ CmpLtS(FTMP, rhs, lhs);
2574 }
2575 __ Bc1nez(FTMP, label);
2576 break;
2577 case kCondGE:
2578 if (gt_bias) {
2579 __ CmpUleS(FTMP, rhs, lhs);
2580 } else {
2581 __ CmpLeS(FTMP, rhs, lhs);
2582 }
2583 __ Bc1nez(FTMP, label);
2584 break;
2585 default:
2586 LOG(FATAL) << "Unexpected non-floating-point condition";
2587 }
2588 } else {
2589 DCHECK_EQ(type, Primitive::kPrimDouble);
2590 switch (cond) {
2591 case kCondEQ:
2592 __ CmpEqD(FTMP, lhs, rhs);
2593 __ Bc1nez(FTMP, label);
2594 break;
2595 case kCondNE:
2596 __ CmpEqD(FTMP, lhs, rhs);
2597 __ Bc1eqz(FTMP, label);
2598 break;
2599 case kCondLT:
2600 if (gt_bias) {
2601 __ CmpLtD(FTMP, lhs, rhs);
2602 } else {
2603 __ CmpUltD(FTMP, lhs, rhs);
2604 }
2605 __ Bc1nez(FTMP, label);
2606 break;
2607 case kCondLE:
2608 if (gt_bias) {
2609 __ CmpLeD(FTMP, lhs, rhs);
2610 } else {
2611 __ CmpUleD(FTMP, lhs, rhs);
2612 }
2613 __ Bc1nez(FTMP, label);
2614 break;
2615 case kCondGT:
2616 if (gt_bias) {
2617 __ CmpUltD(FTMP, rhs, lhs);
2618 } else {
2619 __ CmpLtD(FTMP, rhs, lhs);
2620 }
2621 __ Bc1nez(FTMP, label);
2622 break;
2623 case kCondGE:
2624 if (gt_bias) {
2625 __ CmpUleD(FTMP, rhs, lhs);
2626 } else {
2627 __ CmpLeD(FTMP, rhs, lhs);
2628 }
2629 __ Bc1nez(FTMP, label);
2630 break;
2631 default:
2632 LOG(FATAL) << "Unexpected non-floating-point condition";
2633 }
2634 }
2635}
2636
Alexey Frunze4dda3372015-06-01 18:31:49 -07002637void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002638 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002639 Mips64Label* true_target,
2640 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002641 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642
David Brazdil0debae72015-11-12 18:37:00 +00002643 if (true_target == nullptr && false_target == nullptr) {
2644 // Nothing to do. The code always falls through.
2645 return;
2646 } else if (cond->IsIntConstant()) {
2647 // Constant condition, statically compared against 1.
2648 if (cond->AsIntConstant()->IsOne()) {
2649 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002650 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002651 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002652 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002653 DCHECK(cond->AsIntConstant()->IsZero());
2654 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002655 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002656 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 }
David Brazdil0debae72015-11-12 18:37:00 +00002658 return;
2659 }
2660
2661 // The following code generates these patterns:
2662 // (1) true_target == nullptr && false_target != nullptr
2663 // - opposite condition true => branch to false_target
2664 // (2) true_target != nullptr && false_target == nullptr
2665 // - condition true => branch to true_target
2666 // (3) true_target != nullptr && false_target != nullptr
2667 // - condition true => branch to true_target
2668 // - branch to false_target
2669 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002670 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002671 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002672 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002673 if (true_target == nullptr) {
2674 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2675 } else {
2676 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2677 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002678 } else {
2679 // The condition instruction has not been materialized, use its inputs as
2680 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002681 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002682 Primitive::Type type = condition->InputAt(0)->GetType();
2683 LocationSummary* locations = cond->GetLocations();
2684 IfCondition if_cond = condition->GetCondition();
2685 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002686
David Brazdil0debae72015-11-12 18:37:00 +00002687 if (true_target == nullptr) {
2688 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002689 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002690 }
2691
Alexey Frunze299a9392015-12-08 16:08:02 -08002692 switch (type) {
2693 default:
2694 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2695 break;
2696 case Primitive::kPrimLong:
2697 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2698 break;
2699 case Primitive::kPrimFloat:
2700 case Primitive::kPrimDouble:
2701 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2702 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002703 }
2704 }
David Brazdil0debae72015-11-12 18:37:00 +00002705
2706 // If neither branch falls through (case 3), the conditional branch to `true_target`
2707 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2708 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002709 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002710 }
2711}
2712
2713void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002715 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002716 locations->SetInAt(0, Location::RequiresRegister());
2717 }
2718}
2719
2720void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002721 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2722 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002723 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002724 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002725 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002726 nullptr : codegen_->GetLabelOf(false_successor);
2727 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002728}
2729
2730void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2731 LocationSummary* locations = new (GetGraph()->GetArena())
2732 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002733 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002734 locations->SetInAt(0, Location::RequiresRegister());
2735 }
2736}
2737
2738void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2739 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
2740 DeoptimizationSlowPathMIPS64(deoptimize);
2741 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002742 GenerateTestAndBranch(deoptimize,
2743 /* condition_input_index */ 0,
2744 slow_path->GetEntryLabel(),
2745 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746}
2747
David Srbecky0cf44932015-12-09 14:09:59 +00002748void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2749 new (GetGraph()->GetArena()) LocationSummary(info);
2750}
2751
2752void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002753 if (codegen_->HasStackMapAtCurrentPc()) {
2754 // Ensure that we do not collide with the stack map of the previous instruction.
2755 __ Nop();
2756 }
David Srbecky0cf44932015-12-09 14:09:59 +00002757 codegen_->RecordPcInfo(info, info->GetDexPc());
2758}
2759
Alexey Frunze4dda3372015-06-01 18:31:49 -07002760void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2761 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2762 LocationSummary* locations =
2763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2764 locations->SetInAt(0, Location::RequiresRegister());
2765 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2766 locations->SetOut(Location::RequiresFpuRegister());
2767 } else {
2768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2769 }
2770}
2771
2772void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2773 const FieldInfo& field_info) {
2774 Primitive::Type type = field_info.GetFieldType();
2775 LocationSummary* locations = instruction->GetLocations();
2776 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2777 LoadOperandType load_type = kLoadUnsignedByte;
2778 switch (type) {
2779 case Primitive::kPrimBoolean:
2780 load_type = kLoadUnsignedByte;
2781 break;
2782 case Primitive::kPrimByte:
2783 load_type = kLoadSignedByte;
2784 break;
2785 case Primitive::kPrimShort:
2786 load_type = kLoadSignedHalfword;
2787 break;
2788 case Primitive::kPrimChar:
2789 load_type = kLoadUnsignedHalfword;
2790 break;
2791 case Primitive::kPrimInt:
2792 case Primitive::kPrimFloat:
2793 load_type = kLoadWord;
2794 break;
2795 case Primitive::kPrimLong:
2796 case Primitive::kPrimDouble:
2797 load_type = kLoadDoubleword;
2798 break;
2799 case Primitive::kPrimNot:
2800 load_type = kLoadUnsignedWord;
2801 break;
2802 case Primitive::kPrimVoid:
2803 LOG(FATAL) << "Unreachable type " << type;
2804 UNREACHABLE();
2805 }
2806 if (!Primitive::IsFloatingPointType(type)) {
2807 DCHECK(locations->Out().IsRegister());
2808 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2809 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2810 } else {
2811 DCHECK(locations->Out().IsFpuRegister());
2812 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2813 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2814 }
2815
2816 codegen_->MaybeRecordImplicitNullCheck(instruction);
2817 // TODO: memory barrier?
2818}
2819
2820void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2821 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2822 LocationSummary* locations =
2823 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2824 locations->SetInAt(0, Location::RequiresRegister());
2825 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2826 locations->SetInAt(1, Location::RequiresFpuRegister());
2827 } else {
2828 locations->SetInAt(1, Location::RequiresRegister());
2829 }
2830}
2831
2832void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
2833 const FieldInfo& field_info) {
2834 Primitive::Type type = field_info.GetFieldType();
2835 LocationSummary* locations = instruction->GetLocations();
2836 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2837 StoreOperandType store_type = kStoreByte;
2838 switch (type) {
2839 case Primitive::kPrimBoolean:
2840 case Primitive::kPrimByte:
2841 store_type = kStoreByte;
2842 break;
2843 case Primitive::kPrimShort:
2844 case Primitive::kPrimChar:
2845 store_type = kStoreHalfword;
2846 break;
2847 case Primitive::kPrimInt:
2848 case Primitive::kPrimFloat:
2849 case Primitive::kPrimNot:
2850 store_type = kStoreWord;
2851 break;
2852 case Primitive::kPrimLong:
2853 case Primitive::kPrimDouble:
2854 store_type = kStoreDoubleword;
2855 break;
2856 case Primitive::kPrimVoid:
2857 LOG(FATAL) << "Unreachable type " << type;
2858 UNREACHABLE();
2859 }
2860 if (!Primitive::IsFloatingPointType(type)) {
2861 DCHECK(locations->InAt(1).IsRegister());
2862 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2863 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2864 } else {
2865 DCHECK(locations->InAt(1).IsFpuRegister());
2866 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2867 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2868 }
2869
2870 codegen_->MaybeRecordImplicitNullCheck(instruction);
2871 // TODO: memory barriers?
2872 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2873 DCHECK(locations->InAt(1).IsRegister());
2874 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2875 codegen_->MarkGCCard(obj, src);
2876 }
2877}
2878
2879void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2880 HandleFieldGet(instruction, instruction->GetFieldInfo());
2881}
2882
2883void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2884 HandleFieldGet(instruction, instruction->GetFieldInfo());
2885}
2886
2887void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2888 HandleFieldSet(instruction, instruction->GetFieldInfo());
2889}
2890
2891void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2892 HandleFieldSet(instruction, instruction->GetFieldInfo());
2893}
2894
2895void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2896 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002897 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2899 locations->SetInAt(0, Location::RequiresRegister());
2900 locations->SetInAt(1, Location::RequiresRegister());
2901 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002902 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002903 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2904}
2905
2906void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2907 LocationSummary* locations = instruction->GetLocations();
2908 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2909 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2910 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2911
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002912 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913
2914 // Return 0 if `obj` is null.
2915 // TODO: Avoid this check if we know `obj` is not null.
2916 __ Move(out, ZERO);
2917 __ Beqzc(obj, &done);
2918
2919 // Compare the class of `obj` with `cls`.
2920 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002921 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002922 // Classes must be equal for the instanceof to succeed.
2923 __ Xor(out, out, cls);
2924 __ Sltiu(out, out, 1);
2925 } else {
2926 // If the classes are not equal, we go into a slow path.
2927 DCHECK(locations->OnlyCallsOnSlowPath());
2928 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002929 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002930 codegen_->AddSlowPath(slow_path);
2931 __ Bnec(out, cls, slow_path->GetEntryLabel());
2932 __ LoadConst32(out, 1);
2933 __ Bind(slow_path->GetExitLabel());
2934 }
2935
2936 __ Bind(&done);
2937}
2938
2939void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2941 locations->SetOut(Location::ConstantLocation(constant));
2942}
2943
2944void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2945 // Will be generated at use site.
2946}
2947
2948void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2950 locations->SetOut(Location::ConstantLocation(constant));
2951}
2952
2953void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2954 // Will be generated at use site.
2955}
2956
Calin Juravle175dc732015-08-25 15:42:32 +01002957void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2958 // The trampoline uses the same calling convention as dex calling conventions,
2959 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2960 // the method_idx.
2961 HandleInvoke(invoke);
2962}
2963
2964void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2965 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2966}
2967
Alexey Frunze4dda3372015-06-01 18:31:49 -07002968void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2969 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2970 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2971}
2972
2973void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2974 HandleInvoke(invoke);
2975 // The register T0 is required to be used for the hidden argument in
2976 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2977 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2978}
2979
2980void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2981 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2982 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2983 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2984 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2985 Location receiver = invoke->GetLocations()->InAt(0);
2986 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2987 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2988
2989 // Set the hidden argument.
2990 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2991 invoke->GetDexMethodIndex());
2992
2993 // temp = object->GetClass();
2994 if (receiver.IsStackSlot()) {
2995 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2996 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2997 } else {
2998 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2999 }
3000 codegen_->MaybeRecordImplicitNullCheck(invoke);
3001 // temp = temp->GetImtEntryAt(method_offset);
3002 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3003 // T9 = temp->GetEntryPoint();
3004 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3005 // T9();
3006 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003007 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003008 DCHECK(!codegen_->IsLeafMethod());
3009 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3010}
3011
3012void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003013 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3014 if (intrinsic.TryDispatch(invoke)) {
3015 return;
3016 }
3017
Alexey Frunze4dda3372015-06-01 18:31:49 -07003018 HandleInvoke(invoke);
3019}
3020
3021void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3022 // When we do not run baseline, explicit clinit checks triggered by static
3023 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3024 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3025
Chris Larsen3039e382015-08-26 07:54:08 -07003026 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3027 if (intrinsic.TryDispatch(invoke)) {
3028 return;
3029 }
3030
Alexey Frunze4dda3372015-06-01 18:31:49 -07003031 HandleInvoke(invoke);
3032
3033 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3034 // clobbering somewhere else, reduce further register pressure by avoiding
3035 // allocation of a register for the current method pointer like on x86 baseline.
3036 // TODO: remove this once all the issues with register saving/restoring are
3037 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003038 if (invoke->HasCurrentMethodInput()) {
3039 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003040 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003041 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003042 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003043 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003044 }
3045}
3046
Chris Larsen3039e382015-08-26 07:54:08 -07003047static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003049 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3050 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051 return true;
3052 }
3053 return false;
3054}
3055
Vladimir Markodc151b22015-10-15 18:02:30 +01003056HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3057 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3058 MethodReference target_method ATTRIBUTE_UNUSED) {
3059 switch (desired_dispatch_info.method_load_kind) {
3060 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3061 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3062 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3063 return HInvokeStaticOrDirect::DispatchInfo {
3064 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3065 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3066 0u,
3067 0u
3068 };
3069 default:
3070 break;
3071 }
3072 switch (desired_dispatch_info.code_ptr_location) {
3073 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3074 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3075 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3076 return HInvokeStaticOrDirect::DispatchInfo {
3077 desired_dispatch_info.method_load_kind,
3078 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3079 desired_dispatch_info.method_load_data,
3080 0u
3081 };
3082 default:
3083 return desired_dispatch_info;
3084 }
3085}
3086
Alexey Frunze4dda3372015-06-01 18:31:49 -07003087void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3088 // All registers are assumed to be correctly set up per the calling convention.
3089
Vladimir Marko58155012015-08-19 12:49:41 +00003090 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3091 switch (invoke->GetMethodLoadKind()) {
3092 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3093 // temp = thread->string_init_entrypoint
3094 __ LoadFromOffset(kLoadDoubleword,
3095 temp.AsRegister<GpuRegister>(),
3096 TR,
3097 invoke->GetStringInitOffset());
3098 break;
3099 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003100 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003101 break;
3102 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3103 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3104 break;
3105 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003106 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003107 // TODO: Implement these types.
3108 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3109 LOG(FATAL) << "Unsupported";
3110 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003111 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003112 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003113 GpuRegister reg = temp.AsRegister<GpuRegister>();
3114 GpuRegister method_reg;
3115 if (current_method.IsRegister()) {
3116 method_reg = current_method.AsRegister<GpuRegister>();
3117 } else {
3118 // TODO: use the appropriate DCHECK() here if possible.
3119 // DCHECK(invoke->GetLocations()->Intrinsified());
3120 DCHECK(!current_method.IsValid());
3121 method_reg = reg;
3122 __ Ld(reg, SP, kCurrentMethodStackOffset);
3123 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003124
Vladimir Marko58155012015-08-19 12:49:41 +00003125 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003126 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003127 reg,
3128 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003129 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003130 // temp = temp[index_in_cache]
3131 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3132 __ LoadFromOffset(kLoadDoubleword,
3133 reg,
3134 reg,
3135 CodeGenerator::GetCachePointerOffset(index_in_cache));
3136 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003137 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003138 }
3139
Vladimir Marko58155012015-08-19 12:49:41 +00003140 switch (invoke->GetCodePtrLocation()) {
3141 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003142 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003143 break;
3144 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3145 // LR = invoke->GetDirectCodePtr();
3146 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3147 // LR()
3148 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003149 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003150 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003151 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003152 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3153 // TODO: Implement these types.
3154 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3155 LOG(FATAL) << "Unsupported";
3156 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003157 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3158 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3159 __ LoadFromOffset(kLoadDoubleword,
3160 T9,
3161 callee_method.AsRegister<GpuRegister>(),
3162 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3163 kMips64WordSize).Int32Value());
3164 // T9()
3165 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003166 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003167 break;
3168 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003169 DCHECK(!IsLeafMethod());
3170}
3171
3172void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3173 // When we do not run baseline, explicit clinit checks triggered by static
3174 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3175 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3176
3177 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3178 return;
3179 }
3180
3181 LocationSummary* locations = invoke->GetLocations();
3182 codegen_->GenerateStaticOrDirectCall(invoke,
3183 locations->HasTemps()
3184 ? locations->GetTemp(0)
3185 : Location::NoLocation());
3186 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3187}
3188
Alexey Frunze53afca12015-11-05 16:34:23 -08003189void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003190 // Use the calling convention instead of the location of the receiver, as
3191 // intrinsics may have put the receiver in a different register. In the intrinsics
3192 // slow path, the arguments have been moved to the right place, so here we are
3193 // guaranteed that the receiver is the first register of the calling convention.
3194 InvokeDexCallingConvention calling_convention;
3195 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3196
Alexey Frunze53afca12015-11-05 16:34:23 -08003197 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003198 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3199 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3200 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3201 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
3202
3203 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003204 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003205 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003206 // temp = temp->GetMethodAt(method_offset);
3207 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3208 // T9 = temp->GetEntryPoint();
3209 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3210 // T9();
3211 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003212 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003213}
3214
3215void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3216 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3217 return;
3218 }
3219
3220 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003221 DCHECK(!codegen_->IsLeafMethod());
3222 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3223}
3224
3225void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003226 InvokeRuntimeCallingConvention calling_convention;
3227 CodeGenerator::CreateLoadClassLocationSummary(
3228 cls,
3229 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003230 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231}
3232
3233void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3234 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003235 if (cls->NeedsAccessCheck()) {
3236 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3237 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3238 cls,
3239 cls->GetDexPc(),
3240 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003241 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003242 return;
3243 }
3244
3245 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3246 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3247 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003248 DCHECK(!cls->CanCallRuntime());
3249 DCHECK(!cls->MustGenerateClinitCheck());
3250 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3251 ArtMethod::DeclaringClassOffset().Int32Value());
3252 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003253 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3254 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003255 __ LoadFromOffset(
3256 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003257 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003258 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3259 DCHECK(cls->CanCallRuntime());
3260 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3261 cls,
3262 cls,
3263 cls->GetDexPc(),
3264 cls->MustGenerateClinitCheck());
3265 codegen_->AddSlowPath(slow_path);
3266 if (!cls->IsInDexCache()) {
3267 __ Beqzc(out, slow_path->GetEntryLabel());
3268 }
3269 if (cls->MustGenerateClinitCheck()) {
3270 GenerateClassInitializationCheck(slow_path, out);
3271 } else {
3272 __ Bind(slow_path->GetExitLabel());
3273 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003274 }
3275 }
3276}
3277
David Brazdilcb1c0552015-08-04 16:22:25 +01003278static int32_t GetExceptionTlsOffset() {
3279 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
3280}
3281
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3283 LocationSummary* locations =
3284 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3285 locations->SetOut(Location::RequiresRegister());
3286}
3287
3288void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3289 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003290 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3291}
3292
3293void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3294 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3295}
3296
3297void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3298 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003299}
3300
3301void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3302 load->SetLocations(nullptr);
3303}
3304
3305void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3306 // Nothing to do, this is driven by the code generator.
3307}
3308
3309void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003310 LocationSummary::CallKind call_kind = load->IsInDexCache()
3311 ? LocationSummary::kNoCall
3312 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetOut(Location::RequiresRegister());
3316}
3317
3318void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003319 LocationSummary* locations = load->GetLocations();
3320 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3321 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3322 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3323 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003324 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003325 __ LoadFromOffset(
3326 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003327 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003328
3329 if (!load->IsInDexCache()) {
3330 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3331 codegen_->AddSlowPath(slow_path);
3332 __ Beqzc(out, slow_path->GetEntryLabel());
3333 __ Bind(slow_path->GetExitLabel());
3334 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003335}
3336
3337void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3338 local->SetLocations(nullptr);
3339}
3340
3341void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3342 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3343}
3344
3345void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3347 locations->SetOut(Location::ConstantLocation(constant));
3348}
3349
3350void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3351 // Will be generated at use site.
3352}
3353
3354void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3355 LocationSummary* locations =
3356 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3357 InvokeRuntimeCallingConvention calling_convention;
3358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3359}
3360
3361void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3362 codegen_->InvokeRuntime(instruction->IsEnter()
3363 ? QUICK_ENTRY_POINT(pLockObject)
3364 : QUICK_ENTRY_POINT(pUnlockObject),
3365 instruction,
3366 instruction->GetDexPc(),
3367 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003368 if (instruction->IsEnter()) {
3369 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3370 } else {
3371 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3372 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003373}
3374
3375void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3376 LocationSummary* locations =
3377 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3378 switch (mul->GetResultType()) {
3379 case Primitive::kPrimInt:
3380 case Primitive::kPrimLong:
3381 locations->SetInAt(0, Location::RequiresRegister());
3382 locations->SetInAt(1, Location::RequiresRegister());
3383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3384 break;
3385
3386 case Primitive::kPrimFloat:
3387 case Primitive::kPrimDouble:
3388 locations->SetInAt(0, Location::RequiresFpuRegister());
3389 locations->SetInAt(1, Location::RequiresFpuRegister());
3390 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3391 break;
3392
3393 default:
3394 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3395 }
3396}
3397
3398void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3399 Primitive::Type type = instruction->GetType();
3400 LocationSummary* locations = instruction->GetLocations();
3401
3402 switch (type) {
3403 case Primitive::kPrimInt:
3404 case Primitive::kPrimLong: {
3405 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3406 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3407 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3408 if (type == Primitive::kPrimInt)
3409 __ MulR6(dst, lhs, rhs);
3410 else
3411 __ Dmul(dst, lhs, rhs);
3412 break;
3413 }
3414 case Primitive::kPrimFloat:
3415 case Primitive::kPrimDouble: {
3416 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3417 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3418 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3419 if (type == Primitive::kPrimFloat)
3420 __ MulS(dst, lhs, rhs);
3421 else
3422 __ MulD(dst, lhs, rhs);
3423 break;
3424 }
3425 default:
3426 LOG(FATAL) << "Unexpected mul type " << type;
3427 }
3428}
3429
3430void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3431 LocationSummary* locations =
3432 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3433 switch (neg->GetResultType()) {
3434 case Primitive::kPrimInt:
3435 case Primitive::kPrimLong:
3436 locations->SetInAt(0, Location::RequiresRegister());
3437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3438 break;
3439
3440 case Primitive::kPrimFloat:
3441 case Primitive::kPrimDouble:
3442 locations->SetInAt(0, Location::RequiresFpuRegister());
3443 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3444 break;
3445
3446 default:
3447 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3448 }
3449}
3450
3451void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3452 Primitive::Type type = instruction->GetType();
3453 LocationSummary* locations = instruction->GetLocations();
3454
3455 switch (type) {
3456 case Primitive::kPrimInt:
3457 case Primitive::kPrimLong: {
3458 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3459 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3460 if (type == Primitive::kPrimInt)
3461 __ Subu(dst, ZERO, src);
3462 else
3463 __ Dsubu(dst, ZERO, src);
3464 break;
3465 }
3466 case Primitive::kPrimFloat:
3467 case Primitive::kPrimDouble: {
3468 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3469 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3470 if (type == Primitive::kPrimFloat)
3471 __ NegS(dst, src);
3472 else
3473 __ NegD(dst, src);
3474 break;
3475 }
3476 default:
3477 LOG(FATAL) << "Unexpected neg type " << type;
3478 }
3479}
3480
3481void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3482 LocationSummary* locations =
3483 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3484 InvokeRuntimeCallingConvention calling_convention;
3485 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3486 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3487 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3488 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3489}
3490
3491void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3492 LocationSummary* locations = instruction->GetLocations();
3493 // Move an uint16_t value to a register.
3494 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003495 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3496 instruction,
3497 instruction->GetDexPc(),
3498 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003499 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3500}
3501
3502void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3503 LocationSummary* locations =
3504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3505 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003506 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3507 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3509}
3510
3511void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Calin Juravle175dc732015-08-25 15:42:32 +01003512 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3513 instruction,
3514 instruction->GetDexPc(),
3515 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3517}
3518
3519void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3521 locations->SetInAt(0, Location::RequiresRegister());
3522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3523}
3524
3525void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3526 Primitive::Type type = instruction->GetType();
3527 LocationSummary* locations = instruction->GetLocations();
3528
3529 switch (type) {
3530 case Primitive::kPrimInt:
3531 case Primitive::kPrimLong: {
3532 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3533 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3534 __ Nor(dst, src, ZERO);
3535 break;
3536 }
3537
3538 default:
3539 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3540 }
3541}
3542
3543void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3545 locations->SetInAt(0, Location::RequiresRegister());
3546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3547}
3548
3549void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3550 LocationSummary* locations = instruction->GetLocations();
3551 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3552 locations->InAt(0).AsRegister<GpuRegister>(),
3553 1);
3554}
3555
3556void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003557 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3558 ? LocationSummary::kCallOnSlowPath
3559 : LocationSummary::kNoCall;
3560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 locations->SetInAt(0, Location::RequiresRegister());
3562 if (instruction->HasUses()) {
3563 locations->SetOut(Location::SameAsFirstInput());
3564 }
3565}
3566
3567void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3568 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3569 return;
3570 }
3571 Location obj = instruction->GetLocations()->InAt(0);
3572
3573 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3574 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3575}
3576
3577void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3578 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3579 codegen_->AddSlowPath(slow_path);
3580
3581 Location obj = instruction->GetLocations()->InAt(0);
3582
3583 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3584}
3585
3586void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003587 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 GenerateImplicitNullCheck(instruction);
3589 } else {
3590 GenerateExplicitNullCheck(instruction);
3591 }
3592}
3593
3594void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3595 HandleBinaryOp(instruction);
3596}
3597
3598void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3599 HandleBinaryOp(instruction);
3600}
3601
3602void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3603 LOG(FATAL) << "Unreachable";
3604}
3605
3606void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3607 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3608}
3609
3610void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3612 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3613 if (location.IsStackSlot()) {
3614 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3615 } else if (location.IsDoubleStackSlot()) {
3616 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3617 }
3618 locations->SetOut(location);
3619}
3620
3621void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3622 ATTRIBUTE_UNUSED) {
3623 // Nothing to do, the parameter is already at its location.
3624}
3625
3626void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3627 LocationSummary* locations =
3628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3629 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3630}
3631
3632void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3633 ATTRIBUTE_UNUSED) {
3634 // Nothing to do, the method is already at its location.
3635}
3636
3637void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3639 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3640 locations->SetInAt(i, Location::Any());
3641 }
3642 locations->SetOut(Location::Any());
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3646 LOG(FATAL) << "Unreachable";
3647}
3648
3649void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3650 Primitive::Type type = rem->GetResultType();
3651 LocationSummary::CallKind call_kind =
3652 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3654
3655 switch (type) {
3656 case Primitive::kPrimInt:
3657 case Primitive::kPrimLong:
3658 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003659 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 break;
3662
3663 case Primitive::kPrimFloat:
3664 case Primitive::kPrimDouble: {
3665 InvokeRuntimeCallingConvention calling_convention;
3666 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3667 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3668 locations->SetOut(calling_convention.GetReturnLocation(type));
3669 break;
3670 }
3671
3672 default:
3673 LOG(FATAL) << "Unexpected rem type " << type;
3674 }
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3678 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003679
3680 switch (type) {
3681 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003682 case Primitive::kPrimLong:
3683 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003684 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003685
3686 case Primitive::kPrimFloat:
3687 case Primitive::kPrimDouble: {
3688 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3689 : QUICK_ENTRY_POINT(pFmod);
3690 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003691 if (type == Primitive::kPrimFloat) {
3692 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3693 } else {
3694 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3695 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003696 break;
3697 }
3698 default:
3699 LOG(FATAL) << "Unexpected rem type " << type;
3700 }
3701}
3702
3703void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3704 memory_barrier->SetLocations(nullptr);
3705}
3706
3707void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3708 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3709}
3710
3711void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3713 Primitive::Type return_type = ret->InputAt(0)->GetType();
3714 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3715}
3716
3717void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3718 codegen_->GenerateFrameExit();
3719}
3720
3721void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3722 ret->SetLocations(nullptr);
3723}
3724
3725void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3726 codegen_->GenerateFrameExit();
3727}
3728
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003729void LocationsBuilderMIPS64::VisitRor(HRor* ror ATTRIBUTE_UNUSED) {
3730 LOG(FATAL) << "Unreachable";
3731 UNREACHABLE();
3732}
3733
3734void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror ATTRIBUTE_UNUSED) {
3735 LOG(FATAL) << "Unreachable";
3736 UNREACHABLE();
3737}
3738
Alexey Frunze4dda3372015-06-01 18:31:49 -07003739void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3740 HandleShift(shl);
3741}
3742
3743void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3744 HandleShift(shl);
3745}
3746
3747void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3748 HandleShift(shr);
3749}
3750
3751void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3752 HandleShift(shr);
3753}
3754
3755void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3756 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3757 Primitive::Type field_type = store->InputAt(1)->GetType();
3758 switch (field_type) {
3759 case Primitive::kPrimNot:
3760 case Primitive::kPrimBoolean:
3761 case Primitive::kPrimByte:
3762 case Primitive::kPrimChar:
3763 case Primitive::kPrimShort:
3764 case Primitive::kPrimInt:
3765 case Primitive::kPrimFloat:
3766 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3767 break;
3768
3769 case Primitive::kPrimLong:
3770 case Primitive::kPrimDouble:
3771 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3772 break;
3773
3774 default:
3775 LOG(FATAL) << "Unimplemented local type " << field_type;
3776 }
3777}
3778
3779void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3780}
3781
3782void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3783 HandleBinaryOp(instruction);
3784}
3785
3786void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3787 HandleBinaryOp(instruction);
3788}
3789
3790void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3791 HandleFieldGet(instruction, instruction->GetFieldInfo());
3792}
3793
3794void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3795 HandleFieldGet(instruction, instruction->GetFieldInfo());
3796}
3797
3798void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3799 HandleFieldSet(instruction, instruction->GetFieldInfo());
3800}
3801
3802void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3803 HandleFieldSet(instruction, instruction->GetFieldInfo());
3804}
3805
Calin Juravlee460d1d2015-09-29 04:52:17 +01003806void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3807 HUnresolvedInstanceFieldGet* instruction) {
3808 FieldAccessCallingConventionMIPS64 calling_convention;
3809 codegen_->CreateUnresolvedFieldLocationSummary(
3810 instruction, instruction->GetFieldType(), calling_convention);
3811}
3812
3813void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3814 HUnresolvedInstanceFieldGet* instruction) {
3815 FieldAccessCallingConventionMIPS64 calling_convention;
3816 codegen_->GenerateUnresolvedFieldAccess(instruction,
3817 instruction->GetFieldType(),
3818 instruction->GetFieldIndex(),
3819 instruction->GetDexPc(),
3820 calling_convention);
3821}
3822
3823void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3824 HUnresolvedInstanceFieldSet* instruction) {
3825 FieldAccessCallingConventionMIPS64 calling_convention;
3826 codegen_->CreateUnresolvedFieldLocationSummary(
3827 instruction, instruction->GetFieldType(), calling_convention);
3828}
3829
3830void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3831 HUnresolvedInstanceFieldSet* instruction) {
3832 FieldAccessCallingConventionMIPS64 calling_convention;
3833 codegen_->GenerateUnresolvedFieldAccess(instruction,
3834 instruction->GetFieldType(),
3835 instruction->GetFieldIndex(),
3836 instruction->GetDexPc(),
3837 calling_convention);
3838}
3839
3840void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3841 HUnresolvedStaticFieldGet* instruction) {
3842 FieldAccessCallingConventionMIPS64 calling_convention;
3843 codegen_->CreateUnresolvedFieldLocationSummary(
3844 instruction, instruction->GetFieldType(), calling_convention);
3845}
3846
3847void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3848 HUnresolvedStaticFieldGet* instruction) {
3849 FieldAccessCallingConventionMIPS64 calling_convention;
3850 codegen_->GenerateUnresolvedFieldAccess(instruction,
3851 instruction->GetFieldType(),
3852 instruction->GetFieldIndex(),
3853 instruction->GetDexPc(),
3854 calling_convention);
3855}
3856
3857void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3858 HUnresolvedStaticFieldSet* instruction) {
3859 FieldAccessCallingConventionMIPS64 calling_convention;
3860 codegen_->CreateUnresolvedFieldLocationSummary(
3861 instruction, instruction->GetFieldType(), calling_convention);
3862}
3863
3864void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3865 HUnresolvedStaticFieldSet* instruction) {
3866 FieldAccessCallingConventionMIPS64 calling_convention;
3867 codegen_->GenerateUnresolvedFieldAccess(instruction,
3868 instruction->GetFieldType(),
3869 instruction->GetFieldIndex(),
3870 instruction->GetDexPc(),
3871 calling_convention);
3872}
3873
Alexey Frunze4dda3372015-06-01 18:31:49 -07003874void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3876}
3877
3878void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3879 HBasicBlock* block = instruction->GetBlock();
3880 if (block->GetLoopInformation() != nullptr) {
3881 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3882 // The back edge will generate the suspend check.
3883 return;
3884 }
3885 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3886 // The goto will generate the suspend check.
3887 return;
3888 }
3889 GenerateSuspendCheck(instruction, nullptr);
3890}
3891
3892void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3893 temp->SetLocations(nullptr);
3894}
3895
3896void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3897 // Nothing to do, this is driven by the code generator.
3898}
3899
3900void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3901 LocationSummary* locations =
3902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3903 InvokeRuntimeCallingConvention calling_convention;
3904 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3905}
3906
3907void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3908 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3909 instruction,
3910 instruction->GetDexPc(),
3911 nullptr);
3912 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3913}
3914
3915void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3916 Primitive::Type input_type = conversion->GetInputType();
3917 Primitive::Type result_type = conversion->GetResultType();
3918 DCHECK_NE(input_type, result_type);
3919
3920 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3921 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3922 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3923 }
3924
3925 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3926 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3927 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3928 call_kind = LocationSummary::kCall;
3929 }
3930
3931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3932
3933 if (call_kind == LocationSummary::kNoCall) {
3934 if (Primitive::IsFloatingPointType(input_type)) {
3935 locations->SetInAt(0, Location::RequiresFpuRegister());
3936 } else {
3937 locations->SetInAt(0, Location::RequiresRegister());
3938 }
3939
3940 if (Primitive::IsFloatingPointType(result_type)) {
3941 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3942 } else {
3943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3944 }
3945 } else {
3946 InvokeRuntimeCallingConvention calling_convention;
3947
3948 if (Primitive::IsFloatingPointType(input_type)) {
3949 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3950 } else {
3951 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3952 }
3953
3954 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3955 }
3956}
3957
3958void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3959 LocationSummary* locations = conversion->GetLocations();
3960 Primitive::Type result_type = conversion->GetResultType();
3961 Primitive::Type input_type = conversion->GetInputType();
3962
3963 DCHECK_NE(input_type, result_type);
3964
3965 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3966 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3967 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3968
3969 switch (result_type) {
3970 case Primitive::kPrimChar:
3971 __ Andi(dst, src, 0xFFFF);
3972 break;
3973 case Primitive::kPrimByte:
3974 // long is never converted into types narrower than int directly,
3975 // so SEB and SEH can be used without ever causing unpredictable results
3976 // on 64-bit inputs
3977 DCHECK(input_type != Primitive::kPrimLong);
3978 __ Seb(dst, src);
3979 break;
3980 case Primitive::kPrimShort:
3981 // long is never converted into types narrower than int directly,
3982 // so SEB and SEH can be used without ever causing unpredictable results
3983 // on 64-bit inputs
3984 DCHECK(input_type != Primitive::kPrimLong);
3985 __ Seh(dst, src);
3986 break;
3987 case Primitive::kPrimInt:
3988 case Primitive::kPrimLong:
3989 // Sign-extend 32-bit int into bits 32 through 63 for
3990 // int-to-long and long-to-int conversions
3991 __ Sll(dst, src, 0);
3992 break;
3993
3994 default:
3995 LOG(FATAL) << "Unexpected type conversion from " << input_type
3996 << " to " << result_type;
3997 }
3998 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3999 if (input_type != Primitive::kPrimLong) {
4000 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4001 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4002 __ Mtc1(src, FTMP);
4003 if (result_type == Primitive::kPrimFloat) {
4004 __ Cvtsw(dst, FTMP);
4005 } else {
4006 __ Cvtdw(dst, FTMP);
4007 }
4008 } else {
4009 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4010 : QUICK_ENTRY_POINT(pL2d);
4011 codegen_->InvokeRuntime(entry_offset,
4012 conversion,
4013 conversion->GetDexPc(),
4014 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004015 if (result_type == Primitive::kPrimFloat) {
4016 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4017 } else {
4018 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4019 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004020 }
4021 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4022 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4023 int32_t entry_offset;
4024 if (result_type != Primitive::kPrimLong) {
4025 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4026 : QUICK_ENTRY_POINT(pD2iz);
4027 } else {
4028 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4029 : QUICK_ENTRY_POINT(pD2l);
4030 }
4031 codegen_->InvokeRuntime(entry_offset,
4032 conversion,
4033 conversion->GetDexPc(),
4034 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004035 if (result_type != Primitive::kPrimLong) {
4036 if (input_type == Primitive::kPrimFloat) {
4037 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4038 } else {
4039 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4040 }
4041 } else {
4042 if (input_type == Primitive::kPrimFloat) {
4043 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4044 } else {
4045 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4046 }
4047 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004048 } else if (Primitive::IsFloatingPointType(result_type) &&
4049 Primitive::IsFloatingPointType(input_type)) {
4050 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4051 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4052 if (result_type == Primitive::kPrimFloat) {
4053 __ Cvtsd(dst, src);
4054 } else {
4055 __ Cvtds(dst, src);
4056 }
4057 } else {
4058 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4059 << " to " << result_type;
4060 }
4061}
4062
4063void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4064 HandleShift(ushr);
4065}
4066
4067void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4068 HandleShift(ushr);
4069}
4070
4071void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4072 HandleBinaryOp(instruction);
4073}
4074
4075void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4076 HandleBinaryOp(instruction);
4077}
4078
4079void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4080 // Nothing to do, this should be removed during prepare for register allocator.
4081 LOG(FATAL) << "Unreachable";
4082}
4083
4084void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4085 // Nothing to do, this should be removed during prepare for register allocator.
4086 LOG(FATAL) << "Unreachable";
4087}
4088
4089void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004090 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091}
4092
4093void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004094 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004095}
4096
4097void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004098 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099}
4100
4101void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004102 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004103}
4104
4105void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004106 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107}
4108
4109void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004110 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111}
4112
4113void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004114 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115}
4116
4117void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004118 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004119}
4120
4121void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004122 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123}
4124
4125void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004126 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127}
4128
4129void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004130 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131}
4132
4133void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004134 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004135}
4136
Aart Bike9f37602015-10-09 11:15:55 -07004137void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004138 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004139}
4140
4141void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004142 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004143}
4144
4145void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004146 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004147}
4148
4149void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004150 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004151}
4152
4153void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004154 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004155}
4156
4157void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004158 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004159}
4160
4161void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004162 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004163}
4164
4165void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004166 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004167}
4168
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004169void LocationsBuilderMIPS64::VisitFakeString(HFakeString* instruction) {
4170 DCHECK(codegen_->IsBaseline());
4171 LocationSummary* locations =
4172 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4173 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4174}
4175
4176void InstructionCodeGeneratorMIPS64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4177 DCHECK(codegen_->IsBaseline());
4178 // Will be generated at use site.
4179}
4180
Mark Mendellfe57faa2015-09-18 09:26:15 -04004181// Simple implementation of packed switch - generate cascaded compare/jumps.
4182void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4183 LocationSummary* locations =
4184 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4185 locations->SetInAt(0, Location::RequiresRegister());
4186}
4187
4188void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4189 int32_t lower_bound = switch_instr->GetStartValue();
4190 int32_t num_entries = switch_instr->GetNumEntries();
4191 LocationSummary* locations = switch_instr->GetLocations();
4192 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4193 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4194
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004195 // Create a set of compare/jumps.
4196 GpuRegister temp_reg = TMP;
4197 if (IsInt<16>(-lower_bound)) {
4198 __ Addiu(temp_reg, value_reg, -lower_bound);
4199 } else {
4200 __ LoadConst32(AT, -lower_bound);
4201 __ Addu(temp_reg, value_reg, AT);
4202 }
4203 // Jump to default if index is negative
4204 // Note: We don't check the case that index is positive while value < lower_bound, because in
4205 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4206 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4207
Mark Mendellfe57faa2015-09-18 09:26:15 -04004208 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004209 // Jump to successors[0] if value == lower_bound.
4210 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4211 int32_t last_index = 0;
4212 for (; num_entries - last_index > 2; last_index += 2) {
4213 __ Addiu(temp_reg, temp_reg, -2);
4214 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4215 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4216 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4217 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4218 }
4219 if (num_entries - last_index == 2) {
4220 // The last missing case_value.
4221 __ Addiu(temp_reg, temp_reg, -1);
4222 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004223 }
4224
4225 // And the default for any other value.
4226 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004227 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004228 }
4229}
4230
Alexey Frunze4dda3372015-06-01 18:31:49 -07004231} // namespace mips64
4232} // namespace art