blob: a68fafcf18fe73d713bb02d16ee1db28ae775b44 [file] [log] [blame]
Anton Kirilov5ec62182016-10-13 20:16:02 +01001/*
2 * Copyright (C) 2016 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 "intrinsics_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010021#include "code_generator_arm_vixl.h"
22#include "common_arm.h"
23#include "lock_word.h"
24#include "mirror/array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "mirror/reference.h"
26#include "mirror/string.h"
27#include "scoped_thread_state_change-inl.h"
28#include "thread-inl.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010029
30#include "aarch32/constants-aarch32.h"
31
32namespace art {
33namespace arm {
34
35#define __ assembler->GetVIXLAssembler()->
36
37using helpers::DRegisterFrom;
38using helpers::HighRegisterFrom;
39using helpers::InputDRegisterAt;
40using helpers::InputRegisterAt;
41using helpers::InputSRegisterAt;
42using helpers::InputVRegisterAt;
43using helpers::Int32ConstantFrom;
44using helpers::LocationFrom;
45using helpers::LowRegisterFrom;
46using helpers::LowSRegisterFrom;
xueliang.zhong53463ba2017-02-16 15:18:03 +000047using helpers::HighSRegisterFrom;
Anton Kirilov5ec62182016-10-13 20:16:02 +010048using helpers::OutputDRegister;
xueliang.zhongc032e742016-03-28 16:44:32 +010049using helpers::OutputSRegister;
Anton Kirilov5ec62182016-10-13 20:16:02 +010050using helpers::OutputRegister;
51using helpers::OutputVRegister;
52using helpers::RegisterFrom;
53using helpers::SRegisterFrom;
xueliang.zhongc032e742016-03-28 16:44:32 +010054using helpers::DRegisterFromS;
Anton Kirilov5ec62182016-10-13 20:16:02 +010055
56using namespace vixl::aarch32; // NOLINT(build/namespaces)
57
Artem Serov0fb37192016-12-06 18:13:40 +000058using vixl::ExactAssemblyScope;
59using vixl::CodeBufferCheckScope;
60
Anton Kirilov5ec62182016-10-13 20:16:02 +010061ArmVIXLAssembler* IntrinsicCodeGeneratorARMVIXL::GetAssembler() {
62 return codegen_->GetAssembler();
63}
64
65ArenaAllocator* IntrinsicCodeGeneratorARMVIXL::GetAllocator() {
66 return codegen_->GetGraph()->GetArena();
67}
68
69// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
70// intrinsified call. This will copy the arguments into the positions for a regular call.
71//
72// Note: The actual parameters are required to be in the locations given by the invoke's location
73// summary. If an intrinsic modifies those locations before a slowpath call, they must be
74// restored!
75//
76// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
77// sub-optimal (compared to a direct pointer call), but this is a slow-path.
78
79class IntrinsicSlowPathARMVIXL : public SlowPathCodeARMVIXL {
80 public:
81 explicit IntrinsicSlowPathARMVIXL(HInvoke* invoke)
82 : SlowPathCodeARMVIXL(invoke), invoke_(invoke) {}
83
84 Location MoveArguments(CodeGenerator* codegen) {
Artem Serovd4cc5b22016-11-04 11:19:09 +000085 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Anton Kirilov5ec62182016-10-13 20:16:02 +010086 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
87 return calling_convention_visitor.GetMethodLocation();
88 }
89
90 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
91 ArmVIXLAssembler* assembler = down_cast<ArmVIXLAssembler*>(codegen->GetAssembler());
92 __ Bind(GetEntryLabel());
93
94 SaveLiveRegisters(codegen, invoke_->GetLocations());
95
96 Location method_loc = MoveArguments(codegen);
97
98 if (invoke_->IsInvokeStaticOrDirect()) {
99 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), method_loc);
100 } else {
101 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc);
102 }
103 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
104
105 // Copy the result back to the expected output.
106 Location out = invoke_->GetLocations()->Out();
107 if (out.IsValid()) {
108 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
109 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
110 codegen->MoveFromReturnRegister(out, invoke_->GetType());
111 }
112
113 RestoreLiveRegisters(codegen, invoke_->GetLocations());
114 __ B(GetExitLabel());
115 }
116
117 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPath"; }
118
119 private:
120 // The instruction where this slow path is happening.
121 HInvoke* const invoke_;
122
123 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARMVIXL);
124};
125
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000126// Compute base address for the System.arraycopy intrinsic in `base`.
127static void GenSystemArrayCopyBaseAddress(ArmVIXLAssembler* assembler,
128 Primitive::Type type,
129 const vixl32::Register& array,
130 const Location& pos,
131 const vixl32::Register& base) {
132 // This routine is only used by the SystemArrayCopy intrinsic at the
133 // moment. We can allow Primitive::kPrimNot as `type` to implement
134 // the SystemArrayCopyChar intrinsic.
135 DCHECK_EQ(type, Primitive::kPrimNot);
136 const int32_t element_size = Primitive::ComponentSize(type);
137 const uint32_t element_size_shift = Primitive::ComponentSizeShift(type);
138 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
139
140 if (pos.IsConstant()) {
141 int32_t constant = Int32ConstantFrom(pos);
142 __ Add(base, array, element_size * constant + data_offset);
143 } else {
144 __ Add(base, array, Operand(RegisterFrom(pos), vixl32::LSL, element_size_shift));
145 __ Add(base, base, data_offset);
146 }
147}
148
149// Compute end address for the System.arraycopy intrinsic in `end`.
150static void GenSystemArrayCopyEndAddress(ArmVIXLAssembler* assembler,
151 Primitive::Type type,
152 const Location& copy_length,
153 const vixl32::Register& base,
154 const vixl32::Register& end) {
155 // This routine is only used by the SystemArrayCopy intrinsic at the
156 // moment. We can allow Primitive::kPrimNot as `type` to implement
157 // the SystemArrayCopyChar intrinsic.
158 DCHECK_EQ(type, Primitive::kPrimNot);
159 const int32_t element_size = Primitive::ComponentSize(type);
160 const uint32_t element_size_shift = Primitive::ComponentSizeShift(type);
161
162 if (copy_length.IsConstant()) {
163 int32_t constant = Int32ConstantFrom(copy_length);
164 __ Add(end, base, element_size * constant);
165 } else {
166 __ Add(end, base, Operand(RegisterFrom(copy_length), vixl32::LSL, element_size_shift));
167 }
168}
169
Anton Kirilov5ec62182016-10-13 20:16:02 +0100170// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
171class ReadBarrierSystemArrayCopySlowPathARMVIXL : public SlowPathCodeARMVIXL {
172 public:
173 explicit ReadBarrierSystemArrayCopySlowPathARMVIXL(HInstruction* instruction)
174 : SlowPathCodeARMVIXL(instruction) {
175 DCHECK(kEmitCompilerReadBarrier);
176 DCHECK(kUseBakerReadBarrier);
177 }
178
179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
180 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
181 ArmVIXLAssembler* assembler = arm_codegen->GetAssembler();
182 LocationSummary* locations = instruction_->GetLocations();
183 DCHECK(locations->CanCall());
184 DCHECK(instruction_->IsInvokeStaticOrDirect())
185 << "Unexpected instruction in read barrier arraycopy slow path: "
186 << instruction_->DebugName();
187 DCHECK(instruction_->GetLocations()->Intrinsified());
188 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
189
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000190 Primitive::Type type = Primitive::kPrimNot;
191 const int32_t element_size = Primitive::ComponentSize(type);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100192
193 vixl32::Register dest = InputRegisterAt(instruction_, 2);
194 Location dest_pos = locations->InAt(3);
195 vixl32::Register src_curr_addr = RegisterFrom(locations->GetTemp(0));
196 vixl32::Register dst_curr_addr = RegisterFrom(locations->GetTemp(1));
197 vixl32::Register src_stop_addr = RegisterFrom(locations->GetTemp(2));
198 vixl32::Register tmp = RegisterFrom(locations->GetTemp(3));
199
200 __ Bind(GetEntryLabel());
201 // Compute the base destination address in `dst_curr_addr`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000202 GenSystemArrayCopyBaseAddress(assembler, type, dest, dest_pos, dst_curr_addr);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100203
204 vixl32::Label loop;
205 __ Bind(&loop);
206 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
207 assembler->MaybeUnpoisonHeapReference(tmp);
208 // TODO: Inline the mark bit check before calling the runtime?
209 // tmp = ReadBarrier::Mark(tmp);
210 // No need to save live registers; it's taken care of by the
211 // entrypoint. Also, there is no need to update the stack mask,
212 // as this runtime call will not trigger a garbage collection.
213 // (See ReadBarrierMarkSlowPathARM::EmitNativeCode for more
214 // explanations.)
215 DCHECK(!tmp.IsSP());
216 DCHECK(!tmp.IsLR());
217 DCHECK(!tmp.IsPC());
218 // IP is used internally by the ReadBarrierMarkRegX entry point
219 // as a temporary (and not preserved). It thus cannot be used by
220 // any live register in this slow path.
221 DCHECK(!src_curr_addr.Is(ip));
222 DCHECK(!dst_curr_addr.Is(ip));
223 DCHECK(!src_stop_addr.Is(ip));
224 DCHECK(!tmp.Is(ip));
225 DCHECK(tmp.IsRegister()) << tmp;
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000226 // TODO: Load the entrypoint once before the loop, instead of
227 // loading it at every iteration.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100228 int32_t entry_point_offset =
229 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(tmp.GetCode());
230 // This runtime call does not require a stack map.
231 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
232 assembler->MaybePoisonHeapReference(tmp);
233 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
234 __ Cmp(src_curr_addr, src_stop_addr);
Artem Serov517d9f62016-12-12 15:51:15 +0000235 __ B(ne, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100236 __ B(GetExitLabel());
237 }
238
239 const char* GetDescription() const OVERRIDE {
240 return "ReadBarrierSystemArrayCopySlowPathARMVIXL";
241 }
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARMVIXL);
245};
246
247IntrinsicLocationsBuilderARMVIXL::IntrinsicLocationsBuilderARMVIXL(CodeGeneratorARMVIXL* codegen)
248 : arena_(codegen->GetGraph()->GetArena()),
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000249 codegen_(codegen),
Anton Kirilov5ec62182016-10-13 20:16:02 +0100250 assembler_(codegen->GetAssembler()),
251 features_(codegen->GetInstructionSetFeatures()) {}
252
253bool IntrinsicLocationsBuilderARMVIXL::TryDispatch(HInvoke* invoke) {
254 Dispatch(invoke);
255 LocationSummary* res = invoke->GetLocations();
256 if (res == nullptr) {
257 return false;
258 }
259 return res->Intrinsified();
260}
261
262static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
263 LocationSummary* locations = new (arena) LocationSummary(invoke,
264 LocationSummary::kNoCall,
265 kIntrinsified);
266 locations->SetInAt(0, Location::RequiresFpuRegister());
267 locations->SetOut(Location::RequiresRegister());
268}
269
270static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
271 LocationSummary* locations = new (arena) LocationSummary(invoke,
272 LocationSummary::kNoCall,
273 kIntrinsified);
274 locations->SetInAt(0, Location::RequiresRegister());
275 locations->SetOut(Location::RequiresFpuRegister());
276}
277
278static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
279 Location input = locations->InAt(0);
280 Location output = locations->Out();
281 if (is64bit) {
282 __ Vmov(LowRegisterFrom(output), HighRegisterFrom(output), DRegisterFrom(input));
283 } else {
284 __ Vmov(RegisterFrom(output), SRegisterFrom(input));
285 }
286}
287
288static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
289 Location input = locations->InAt(0);
290 Location output = locations->Out();
291 if (is64bit) {
292 __ Vmov(DRegisterFrom(output), LowRegisterFrom(input), HighRegisterFrom(input));
293 } else {
294 __ Vmov(SRegisterFrom(output), RegisterFrom(input));
295 }
296}
297
298void IntrinsicLocationsBuilderARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
299 CreateFPToIntLocations(arena_, invoke);
300}
301void IntrinsicLocationsBuilderARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
302 CreateIntToFPLocations(arena_, invoke);
303}
304
305void IntrinsicCodeGeneratorARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
306 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
307}
308void IntrinsicCodeGeneratorARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
309 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
310}
311
312void IntrinsicLocationsBuilderARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
313 CreateFPToIntLocations(arena_, invoke);
314}
315void IntrinsicLocationsBuilderARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
316 CreateIntToFPLocations(arena_, invoke);
317}
318
319void IntrinsicCodeGeneratorARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
320 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
321}
322void IntrinsicCodeGeneratorARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
323 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
324}
325
326static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
327 LocationSummary* locations = new (arena) LocationSummary(invoke,
328 LocationSummary::kNoCall,
329 kIntrinsified);
330 locations->SetInAt(0, Location::RequiresRegister());
331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
332}
333
334static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
335 LocationSummary* locations = new (arena) LocationSummary(invoke,
336 LocationSummary::kNoCall,
337 kIntrinsified);
338 locations->SetInAt(0, Location::RequiresFpuRegister());
339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
340}
341
Anton Kirilov6f644202017-02-27 18:29:45 +0000342static void GenNumberOfLeadingZeros(HInvoke* invoke,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100343 Primitive::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000344 CodeGeneratorARMVIXL* codegen) {
345 ArmVIXLAssembler* assembler = codegen->GetAssembler();
346 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100347 Location in = locations->InAt(0);
348 vixl32::Register out = RegisterFrom(locations->Out());
349
350 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
351
352 if (type == Primitive::kPrimLong) {
353 vixl32::Register in_reg_lo = LowRegisterFrom(in);
354 vixl32::Register in_reg_hi = HighRegisterFrom(in);
355 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000356 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100357 __ Clz(out, in_reg_hi);
Anton Kirilov6f644202017-02-27 18:29:45 +0000358 __ CompareAndBranchIfNonZero(in_reg_hi, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100359 __ Clz(out, in_reg_lo);
360 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000361 if (end.IsReferenced()) {
362 __ Bind(&end);
363 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100364 } else {
365 __ Clz(out, RegisterFrom(in));
366 }
367}
368
369void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
370 CreateIntToIntLocations(arena_, invoke);
371}
372
373void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000374 GenNumberOfLeadingZeros(invoke, Primitive::kPrimInt, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100375}
376
377void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
378 LocationSummary* locations = new (arena_) LocationSummary(invoke,
379 LocationSummary::kNoCall,
380 kIntrinsified);
381 locations->SetInAt(0, Location::RequiresRegister());
382 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
383}
384
385void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000386 GenNumberOfLeadingZeros(invoke, Primitive::kPrimLong, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100387}
388
Anton Kirilov6f644202017-02-27 18:29:45 +0000389static void GenNumberOfTrailingZeros(HInvoke* invoke,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100390 Primitive::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000391 CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100392 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
393
Anton Kirilov6f644202017-02-27 18:29:45 +0000394 ArmVIXLAssembler* assembler = codegen->GetAssembler();
395 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100396 vixl32::Register out = RegisterFrom(locations->Out());
397
398 if (type == Primitive::kPrimLong) {
399 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
400 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
401 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000402 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100403 __ Rbit(out, in_reg_lo);
404 __ Clz(out, out);
Anton Kirilov6f644202017-02-27 18:29:45 +0000405 __ CompareAndBranchIfNonZero(in_reg_lo, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100406 __ Rbit(out, in_reg_hi);
407 __ Clz(out, out);
408 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000409 if (end.IsReferenced()) {
410 __ Bind(&end);
411 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100412 } else {
413 vixl32::Register in = RegisterFrom(locations->InAt(0));
414 __ Rbit(out, in);
415 __ Clz(out, out);
416 }
417}
418
419void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
420 LocationSummary* locations = new (arena_) LocationSummary(invoke,
421 LocationSummary::kNoCall,
422 kIntrinsified);
423 locations->SetInAt(0, Location::RequiresRegister());
424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
425}
426
427void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000428 GenNumberOfTrailingZeros(invoke, Primitive::kPrimInt, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100429}
430
431void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
432 LocationSummary* locations = new (arena_) LocationSummary(invoke,
433 LocationSummary::kNoCall,
434 kIntrinsified);
435 locations->SetInAt(0, Location::RequiresRegister());
436 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
437}
438
439void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000440 GenNumberOfTrailingZeros(invoke, Primitive::kPrimLong, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100441}
442
443static void MathAbsFP(HInvoke* invoke, ArmVIXLAssembler* assembler) {
444 __ Vabs(OutputVRegister(invoke), InputVRegisterAt(invoke, 0));
445}
446
447void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsDouble(HInvoke* invoke) {
448 CreateFPToFPLocations(arena_, invoke);
449}
450
451void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsDouble(HInvoke* invoke) {
452 MathAbsFP(invoke, GetAssembler());
453}
454
455void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsFloat(HInvoke* invoke) {
456 CreateFPToFPLocations(arena_, invoke);
457}
458
459void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsFloat(HInvoke* invoke) {
460 MathAbsFP(invoke, GetAssembler());
461}
462
463static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
464 LocationSummary* locations = new (arena) LocationSummary(invoke,
465 LocationSummary::kNoCall,
466 kIntrinsified);
467 locations->SetInAt(0, Location::RequiresRegister());
468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
469
470 locations->AddTemp(Location::RequiresRegister());
471}
472
473static void GenAbsInteger(LocationSummary* locations,
474 bool is64bit,
475 ArmVIXLAssembler* assembler) {
476 Location in = locations->InAt(0);
477 Location output = locations->Out();
478
479 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
480
481 if (is64bit) {
482 vixl32::Register in_reg_lo = LowRegisterFrom(in);
483 vixl32::Register in_reg_hi = HighRegisterFrom(in);
484 vixl32::Register out_reg_lo = LowRegisterFrom(output);
485 vixl32::Register out_reg_hi = HighRegisterFrom(output);
486
487 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
488
489 __ Asr(mask, in_reg_hi, 31);
490 __ Adds(out_reg_lo, in_reg_lo, mask);
491 __ Adc(out_reg_hi, in_reg_hi, mask);
492 __ Eor(out_reg_lo, mask, out_reg_lo);
493 __ Eor(out_reg_hi, mask, out_reg_hi);
494 } else {
495 vixl32::Register in_reg = RegisterFrom(in);
496 vixl32::Register out_reg = RegisterFrom(output);
497
498 __ Asr(mask, in_reg, 31);
499 __ Add(out_reg, in_reg, mask);
500 __ Eor(out_reg, mask, out_reg);
501 }
502}
503
504void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsInt(HInvoke* invoke) {
505 CreateIntToIntPlusTemp(arena_, invoke);
506}
507
508void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsInt(HInvoke* invoke) {
509 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
510}
511
512
513void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsLong(HInvoke* invoke) {
514 CreateIntToIntPlusTemp(arena_, invoke);
515}
516
517void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsLong(HInvoke* invoke) {
518 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
519}
520
Anton Kirilov6f644202017-02-27 18:29:45 +0000521static void GenMinMaxFloat(HInvoke* invoke, bool is_min, CodeGeneratorARMVIXL* codegen) {
522 ArmVIXLAssembler* assembler = codegen->GetAssembler();
xueliang.zhongc032e742016-03-28 16:44:32 +0100523 Location op1_loc = invoke->GetLocations()->InAt(0);
524 Location op2_loc = invoke->GetLocations()->InAt(1);
525 Location out_loc = invoke->GetLocations()->Out();
526
527 // Optimization: don't generate any code if inputs are the same.
528 if (op1_loc.Equals(op2_loc)) {
529 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
530 return;
531 }
532
533 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
534 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
535 vixl32::SRegister out = OutputSRegister(invoke);
536 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
537 const vixl32::Register temp1 = temps.Acquire();
538 vixl32::Register temp2 = RegisterFrom(invoke->GetLocations()->GetTemp(0));
539 vixl32::Label nan, done;
Anton Kirilov6f644202017-02-27 18:29:45 +0000540 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &done);
xueliang.zhongc032e742016-03-28 16:44:32 +0100541
542 DCHECK(op1.Is(out));
543
544 __ Vcmp(op1, op2);
545 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
546 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
547
548 // op1 <> op2
549 vixl32::ConditionType cond = is_min ? gt : lt;
550 {
551 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
552 2 * kMaxInstructionSizeInBytes,
553 CodeBufferCheckScope::kMaximumSize);
554 __ it(cond);
555 __ vmov(cond, F32, out, op2);
556 }
Anton Kirilov6f644202017-02-27 18:29:45 +0000557 // for <>(not equal), we've done min/max calculation.
558 __ B(ne, final_label, /* far_target */ false);
xueliang.zhongc032e742016-03-28 16:44:32 +0100559
560 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
561 __ Vmov(temp1, op1);
562 __ Vmov(temp2, op2);
563 if (is_min) {
564 __ Orr(temp1, temp1, temp2);
565 } else {
566 __ And(temp1, temp1, temp2);
567 }
568 __ Vmov(out, temp1);
Anton Kirilov6f644202017-02-27 18:29:45 +0000569 __ B(final_label);
xueliang.zhongc032e742016-03-28 16:44:32 +0100570
571 // handle NaN input.
572 __ Bind(&nan);
573 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
574 __ Vmov(out, temp1);
575
Anton Kirilov6f644202017-02-27 18:29:45 +0000576 if (done.IsReferenced()) {
577 __ Bind(&done);
578 }
xueliang.zhongc032e742016-03-28 16:44:32 +0100579}
580
581static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
582 LocationSummary* locations = new (arena) LocationSummary(invoke,
583 LocationSummary::kNoCall,
584 kIntrinsified);
585 locations->SetInAt(0, Location::RequiresFpuRegister());
586 locations->SetInAt(1, Location::RequiresFpuRegister());
587 locations->SetOut(Location::SameAsFirstInput());
588}
589
590void IntrinsicLocationsBuilderARMVIXL::VisitMathMinFloatFloat(HInvoke* invoke) {
591 CreateFPFPToFPLocations(arena_, invoke);
592 invoke->GetLocations()->AddTemp(Location::RequiresRegister());
593}
594
595void IntrinsicCodeGeneratorARMVIXL::VisitMathMinFloatFloat(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000596 GenMinMaxFloat(invoke, /* is_min */ true, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100597}
598
599void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxFloatFloat(HInvoke* invoke) {
600 CreateFPFPToFPLocations(arena_, invoke);
601 invoke->GetLocations()->AddTemp(Location::RequiresRegister());
602}
603
604void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxFloatFloat(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000605 GenMinMaxFloat(invoke, /* is_min */ false, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100606}
607
Anton Kirilov6f644202017-02-27 18:29:45 +0000608static void GenMinMaxDouble(HInvoke* invoke, bool is_min, CodeGeneratorARMVIXL* codegen) {
609 ArmVIXLAssembler* assembler = codegen->GetAssembler();
xueliang.zhongc032e742016-03-28 16:44:32 +0100610 Location op1_loc = invoke->GetLocations()->InAt(0);
611 Location op2_loc = invoke->GetLocations()->InAt(1);
612 Location out_loc = invoke->GetLocations()->Out();
613
614 // Optimization: don't generate any code if inputs are the same.
615 if (op1_loc.Equals(op2_loc)) {
616 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
617 return;
618 }
619
620 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
621 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
622 vixl32::DRegister out = OutputDRegister(invoke);
623 vixl32::Label handle_nan_eq, done;
Anton Kirilov6f644202017-02-27 18:29:45 +0000624 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &done);
xueliang.zhongc032e742016-03-28 16:44:32 +0100625
626 DCHECK(op1.Is(out));
627
628 __ Vcmp(op1, op2);
629 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
630 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
631
632 // op1 <> op2
633 vixl32::ConditionType cond = is_min ? gt : lt;
634 {
635 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
636 2 * kMaxInstructionSizeInBytes,
637 CodeBufferCheckScope::kMaximumSize);
638 __ it(cond);
639 __ vmov(cond, F64, out, op2);
640 }
Anton Kirilov6f644202017-02-27 18:29:45 +0000641 // for <>(not equal), we've done min/max calculation.
642 __ B(ne, final_label, /* far_target */ false);
xueliang.zhongc032e742016-03-28 16:44:32 +0100643
644 // handle op1 == op2, max(+0.0,-0.0).
645 if (!is_min) {
646 __ Vand(F64, out, op1, op2);
Anton Kirilov6f644202017-02-27 18:29:45 +0000647 __ B(final_label);
xueliang.zhongc032e742016-03-28 16:44:32 +0100648 }
649
650 // handle op1 == op2, min(+0.0,-0.0), NaN input.
651 __ Bind(&handle_nan_eq);
652 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
653
Anton Kirilov6f644202017-02-27 18:29:45 +0000654 if (done.IsReferenced()) {
655 __ Bind(&done);
656 }
xueliang.zhongc032e742016-03-28 16:44:32 +0100657}
658
659void IntrinsicLocationsBuilderARMVIXL::VisitMathMinDoubleDouble(HInvoke* invoke) {
660 CreateFPFPToFPLocations(arena_, invoke);
661}
662
663void IntrinsicCodeGeneratorARMVIXL::VisitMathMinDoubleDouble(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000664 GenMinMaxDouble(invoke, /* is_min */ true , codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100665}
666
667void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxDoubleDouble(HInvoke* invoke) {
668 CreateFPFPToFPLocations(arena_, invoke);
669}
670
671void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000672 GenMinMaxDouble(invoke, /* is_min */ false, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100673}
674
675static void GenMinMaxLong(HInvoke* invoke, bool is_min, ArmVIXLAssembler* assembler) {
676 Location op1_loc = invoke->GetLocations()->InAt(0);
677 Location op2_loc = invoke->GetLocations()->InAt(1);
678 Location out_loc = invoke->GetLocations()->Out();
679
680 // Optimization: don't generate any code if inputs are the same.
681 if (op1_loc.Equals(op2_loc)) {
682 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
683 return;
684 }
685
686 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
687 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
688 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
689 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
690 vixl32::Register out_lo = LowRegisterFrom(out_loc);
691 vixl32::Register out_hi = HighRegisterFrom(out_loc);
692 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
693 const vixl32::Register temp = temps.Acquire();
694
695 DCHECK(op1_lo.Is(out_lo));
696 DCHECK(op1_hi.Is(out_hi));
697
698 // Compare op1 >= op2, or op1 < op2.
699 __ Cmp(out_lo, op2_lo);
700 __ Sbcs(temp, out_hi, op2_hi);
701
702 // Now GE/LT condition code is correct for the long comparison.
703 {
704 vixl32::ConditionType cond = is_min ? ge : lt;
705 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
706 3 * kMaxInstructionSizeInBytes,
707 CodeBufferCheckScope::kMaximumSize);
708 __ itt(cond);
709 __ mov(cond, out_lo, op2_lo);
710 __ mov(cond, out_hi, op2_hi);
711 }
712}
713
714static void CreateLongLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
715 LocationSummary* locations = new (arena) LocationSummary(invoke,
716 LocationSummary::kNoCall,
717 kIntrinsified);
718 locations->SetInAt(0, Location::RequiresRegister());
719 locations->SetInAt(1, Location::RequiresRegister());
720 locations->SetOut(Location::SameAsFirstInput());
721}
722
723void IntrinsicLocationsBuilderARMVIXL::VisitMathMinLongLong(HInvoke* invoke) {
724 CreateLongLongToLongLocations(arena_, invoke);
725}
726
727void IntrinsicCodeGeneratorARMVIXL::VisitMathMinLongLong(HInvoke* invoke) {
728 GenMinMaxLong(invoke, /* is_min */ true, GetAssembler());
729}
730
731void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxLongLong(HInvoke* invoke) {
732 CreateLongLongToLongLocations(arena_, invoke);
733}
734
735void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxLongLong(HInvoke* invoke) {
736 GenMinMaxLong(invoke, /* is_min */ false, GetAssembler());
737}
738
Anton Kirilov5ec62182016-10-13 20:16:02 +0100739static void GenMinMax(HInvoke* invoke, bool is_min, ArmVIXLAssembler* assembler) {
740 vixl32::Register op1 = InputRegisterAt(invoke, 0);
741 vixl32::Register op2 = InputRegisterAt(invoke, 1);
742 vixl32::Register out = OutputRegister(invoke);
743
744 __ Cmp(op1, op2);
745
746 {
Artem Serov0fb37192016-12-06 18:13:40 +0000747 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
748 3 * kMaxInstructionSizeInBytes,
749 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100750
751 __ ite(is_min ? lt : gt);
752 __ mov(is_min ? lt : gt, out, op1);
753 __ mov(is_min ? ge : le, out, op2);
754 }
755}
756
757static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
758 LocationSummary* locations = new (arena) LocationSummary(invoke,
759 LocationSummary::kNoCall,
760 kIntrinsified);
761 locations->SetInAt(0, Location::RequiresRegister());
762 locations->SetInAt(1, Location::RequiresRegister());
763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
764}
765
766void IntrinsicLocationsBuilderARMVIXL::VisitMathMinIntInt(HInvoke* invoke) {
767 CreateIntIntToIntLocations(arena_, invoke);
768}
769
770void IntrinsicCodeGeneratorARMVIXL::VisitMathMinIntInt(HInvoke* invoke) {
771 GenMinMax(invoke, /* is_min */ true, GetAssembler());
772}
773
774void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxIntInt(HInvoke* invoke) {
775 CreateIntIntToIntLocations(arena_, invoke);
776}
777
778void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxIntInt(HInvoke* invoke) {
779 GenMinMax(invoke, /* is_min */ false, GetAssembler());
780}
781
782void IntrinsicLocationsBuilderARMVIXL::VisitMathSqrt(HInvoke* invoke) {
783 CreateFPToFPLocations(arena_, invoke);
784}
785
786void IntrinsicCodeGeneratorARMVIXL::VisitMathSqrt(HInvoke* invoke) {
787 ArmVIXLAssembler* assembler = GetAssembler();
788 __ Vsqrt(OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
789}
790
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100791void IntrinsicLocationsBuilderARMVIXL::VisitMathRint(HInvoke* invoke) {
792 if (features_.HasARMv8AInstructions()) {
793 CreateFPToFPLocations(arena_, invoke);
794 }
795}
796
797void IntrinsicCodeGeneratorARMVIXL::VisitMathRint(HInvoke* invoke) {
798 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
799 ArmVIXLAssembler* assembler = GetAssembler();
800 __ Vrintn(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
801}
802
xueliang.zhong53463ba2017-02-16 15:18:03 +0000803void IntrinsicLocationsBuilderARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
804 if (features_.HasARMv8AInstructions()) {
805 LocationSummary* locations = new (arena_) LocationSummary(invoke,
806 LocationSummary::kNoCall,
807 kIntrinsified);
808 locations->SetInAt(0, Location::RequiresFpuRegister());
809 locations->SetOut(Location::RequiresRegister());
810 locations->AddTemp(Location::RequiresFpuRegister());
811 }
812}
813
814void IntrinsicCodeGeneratorARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
815 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
816
817 ArmVIXLAssembler* assembler = GetAssembler();
818 vixl32::SRegister in_reg = InputSRegisterAt(invoke, 0);
819 vixl32::Register out_reg = OutputRegister(invoke);
820 vixl32::SRegister temp1 = LowSRegisterFrom(invoke->GetLocations()->GetTemp(0));
821 vixl32::SRegister temp2 = HighSRegisterFrom(invoke->GetLocations()->GetTemp(0));
822 vixl32::Label done;
823 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
824
825 // Round to nearest integer, ties away from zero.
826 __ Vcvta(S32, F32, temp1, in_reg);
827 __ Vmov(out_reg, temp1);
828
829 // For positive, zero or NaN inputs, rounding is done.
830 __ Cmp(out_reg, 0);
831 __ B(ge, final_label, /* far_target */ false);
832
833 // Handle input < 0 cases.
834 // If input is negative but not a tie, previous result (round to nearest) is valid.
835 // If input is a negative tie, change rounding direction to positive infinity, out_reg += 1.
836 __ Vrinta(F32, F32, temp1, in_reg);
837 __ Vmov(temp2, 0.5);
838 __ Vsub(F32, temp1, in_reg, temp1);
839 __ Vcmp(F32, temp1, temp2);
840 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
841 {
842 // Use ExactAsemblyScope here because we are using IT.
843 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
844 2 * kMaxInstructionSizeInBytes,
845 CodeBufferCheckScope::kMaximumSize);
846 __ it(eq);
847 __ add(eq, out_reg, out_reg, 1);
848 }
849
850 if (done.IsReferenced()) {
851 __ Bind(&done);
852 }
853}
854
Anton Kirilov5ec62182016-10-13 20:16:02 +0100855void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
856 CreateIntToIntLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
860 ArmVIXLAssembler* assembler = GetAssembler();
861 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000862 __ Ldrsb(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100863}
864
865void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
866 CreateIntToIntLocations(arena_, invoke);
867}
868
869void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
870 ArmVIXLAssembler* assembler = GetAssembler();
871 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000872 __ Ldr(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100873}
874
875void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
876 CreateIntToIntLocations(arena_, invoke);
877}
878
879void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
880 ArmVIXLAssembler* assembler = GetAssembler();
881 // Ignore upper 4B of long address.
882 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
883 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
884 // exception. So we can't use ldrd as addr may be unaligned.
885 vixl32::Register lo = LowRegisterFrom(invoke->GetLocations()->Out());
886 vixl32::Register hi = HighRegisterFrom(invoke->GetLocations()->Out());
887 if (addr.Is(lo)) {
888 __ Ldr(hi, MemOperand(addr, 4));
Scott Wakelingb77051e2016-11-21 19:46:00 +0000889 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100890 } else {
Scott Wakelingb77051e2016-11-21 19:46:00 +0000891 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100892 __ Ldr(hi, MemOperand(addr, 4));
893 }
894}
895
896void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
897 CreateIntToIntLocations(arena_, invoke);
898}
899
900void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
901 ArmVIXLAssembler* assembler = GetAssembler();
902 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000903 __ Ldrsh(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100904}
905
906static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
907 LocationSummary* locations = new (arena) LocationSummary(invoke,
908 LocationSummary::kNoCall,
909 kIntrinsified);
910 locations->SetInAt(0, Location::RequiresRegister());
911 locations->SetInAt(1, Location::RequiresRegister());
912}
913
914void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
915 CreateIntIntToVoidLocations(arena_, invoke);
916}
917
918void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
919 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000920 __ Strb(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100921}
922
923void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
924 CreateIntIntToVoidLocations(arena_, invoke);
925}
926
927void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
928 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000929 __ Str(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100930}
931
932void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
933 CreateIntIntToVoidLocations(arena_, invoke);
934}
935
936void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
937 ArmVIXLAssembler* assembler = GetAssembler();
938 // Ignore upper 4B of long address.
939 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
940 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
941 // exception. So we can't use ldrd as addr may be unaligned.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000942 __ Str(LowRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100943 __ Str(HighRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr, 4));
944}
945
946void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
947 CreateIntIntToVoidLocations(arena_, invoke);
948}
949
950void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
951 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000952 __ Strh(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100953}
954
955void IntrinsicLocationsBuilderARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
956 LocationSummary* locations = new (arena_) LocationSummary(invoke,
957 LocationSummary::kNoCall,
958 kIntrinsified);
959 locations->SetOut(Location::RequiresRegister());
960}
961
962void IntrinsicCodeGeneratorARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
963 ArmVIXLAssembler* assembler = GetAssembler();
964 __ Ldr(OutputRegister(invoke),
965 MemOperand(tr, Thread::PeerOffset<kArmPointerSize>().Int32Value()));
966}
967
968static void GenUnsafeGet(HInvoke* invoke,
969 Primitive::Type type,
970 bool is_volatile,
971 CodeGeneratorARMVIXL* codegen) {
972 LocationSummary* locations = invoke->GetLocations();
973 ArmVIXLAssembler* assembler = codegen->GetAssembler();
974 Location base_loc = locations->InAt(1);
975 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
976 Location offset_loc = locations->InAt(2);
977 vixl32::Register offset = LowRegisterFrom(offset_loc); // Long offset, lo part only.
978 Location trg_loc = locations->Out();
979
980 switch (type) {
981 case Primitive::kPrimInt: {
982 vixl32::Register trg = RegisterFrom(trg_loc);
983 __ Ldr(trg, MemOperand(base, offset));
984 if (is_volatile) {
985 __ Dmb(vixl32::ISH);
986 }
987 break;
988 }
989
990 case Primitive::kPrimNot: {
991 vixl32::Register trg = RegisterFrom(trg_loc);
992 if (kEmitCompilerReadBarrier) {
993 if (kUseBakerReadBarrier) {
994 Location temp = locations->GetTemp(0);
995 codegen->GenerateReferenceLoadWithBakerReadBarrier(
996 invoke, trg_loc, base, 0U, offset_loc, TIMES_1, temp, /* needs_null_check */ false);
997 if (is_volatile) {
998 __ Dmb(vixl32::ISH);
999 }
1000 } else {
1001 __ Ldr(trg, MemOperand(base, offset));
1002 if (is_volatile) {
1003 __ Dmb(vixl32::ISH);
1004 }
1005 codegen->GenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
1006 }
1007 } else {
1008 __ Ldr(trg, MemOperand(base, offset));
1009 if (is_volatile) {
1010 __ Dmb(vixl32::ISH);
1011 }
1012 assembler->MaybeUnpoisonHeapReference(trg);
1013 }
1014 break;
1015 }
1016
1017 case Primitive::kPrimLong: {
1018 vixl32::Register trg_lo = LowRegisterFrom(trg_loc);
1019 vixl32::Register trg_hi = HighRegisterFrom(trg_loc);
1020 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
Artem Serov657022c2016-11-23 14:19:38 +00001021 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1022 const vixl32::Register temp_reg = temps.Acquire();
1023 __ Add(temp_reg, base, offset);
1024 __ Ldrexd(trg_lo, trg_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001025 } else {
1026 __ Ldrd(trg_lo, trg_hi, MemOperand(base, offset));
1027 }
1028 if (is_volatile) {
1029 __ Dmb(vixl32::ISH);
1030 }
1031 break;
1032 }
1033
1034 default:
1035 LOG(FATAL) << "Unexpected type " << type;
1036 UNREACHABLE();
1037 }
1038}
1039
1040static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1041 HInvoke* invoke,
1042 Primitive::Type type) {
1043 bool can_call = kEmitCompilerReadBarrier &&
1044 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1045 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
1046 LocationSummary* locations = new (arena) LocationSummary(invoke,
1047 (can_call
1048 ? LocationSummary::kCallOnSlowPath
1049 : LocationSummary::kNoCall),
1050 kIntrinsified);
1051 if (can_call && kUseBakerReadBarrier) {
1052 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1053 }
1054 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1055 locations->SetInAt(1, Location::RequiresRegister());
1056 locations->SetInAt(2, Location::RequiresRegister());
1057 locations->SetOut(Location::RequiresRegister(),
1058 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
1059 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1060 // We need a temporary register for the read barrier marking slow
1061 // path in InstructionCodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier.
1062 locations->AddTemp(Location::RequiresRegister());
1063 }
1064}
1065
1066void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
1067 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
1068}
1069void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
1070 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
1071}
1072void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
1073 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
1074}
1075void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1076 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
1077}
1078void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
1079 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
1080}
1081void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1082 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
1083}
1084
1085void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
1086 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
1087}
1088void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
1089 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
1090}
1091void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
1092 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
1093}
1094void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1095 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
1096}
1097void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
1098 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
1099}
1100void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1101 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
1102}
1103
1104static void CreateIntIntIntIntToVoid(ArenaAllocator* arena,
1105 const ArmInstructionSetFeatures& features,
1106 Primitive::Type type,
1107 bool is_volatile,
1108 HInvoke* invoke) {
1109 LocationSummary* locations = new (arena) LocationSummary(invoke,
1110 LocationSummary::kNoCall,
1111 kIntrinsified);
1112 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1113 locations->SetInAt(1, Location::RequiresRegister());
1114 locations->SetInAt(2, Location::RequiresRegister());
1115 locations->SetInAt(3, Location::RequiresRegister());
1116
1117 if (type == Primitive::kPrimLong) {
1118 // Potentially need temps for ldrexd-strexd loop.
1119 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
1120 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
1121 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
1122 }
1123 } else if (type == Primitive::kPrimNot) {
1124 // Temps for card-marking.
1125 locations->AddTemp(Location::RequiresRegister()); // Temp.
1126 locations->AddTemp(Location::RequiresRegister()); // Card.
1127 }
1128}
1129
1130void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePut(HInvoke* invoke) {
1131 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
1132}
1133void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
1134 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
1135}
1136void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
1137 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ true, invoke);
1138}
1139void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
1140 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
1141}
1142void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1143 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
1144}
1145void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1146 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ true, invoke);
1147}
1148void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
1149 CreateIntIntIntIntToVoid(
1150 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
1151}
1152void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1153 CreateIntIntIntIntToVoid(
1154 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
1155}
1156void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1157 CreateIntIntIntIntToVoid(
1158 arena_, features_, Primitive::kPrimLong, /* is_volatile */ true, invoke);
1159}
1160
1161static void GenUnsafePut(LocationSummary* locations,
1162 Primitive::Type type,
1163 bool is_volatile,
1164 bool is_ordered,
1165 CodeGeneratorARMVIXL* codegen) {
1166 ArmVIXLAssembler* assembler = codegen->GetAssembler();
1167
1168 vixl32::Register base = RegisterFrom(locations->InAt(1)); // Object pointer.
1169 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Long offset, lo part only.
1170 vixl32::Register value;
1171
1172 if (is_volatile || is_ordered) {
1173 __ Dmb(vixl32::ISH);
1174 }
1175
1176 if (type == Primitive::kPrimLong) {
1177 vixl32::Register value_lo = LowRegisterFrom(locations->InAt(3));
1178 vixl32::Register value_hi = HighRegisterFrom(locations->InAt(3));
1179 value = value_lo;
1180 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
1181 vixl32::Register temp_lo = RegisterFrom(locations->GetTemp(0));
1182 vixl32::Register temp_hi = RegisterFrom(locations->GetTemp(1));
1183 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1184 const vixl32::Register temp_reg = temps.Acquire();
1185
1186 __ Add(temp_reg, base, offset);
1187 vixl32::Label loop_head;
1188 __ Bind(&loop_head);
Scott Wakelingb77051e2016-11-21 19:46:00 +00001189 __ Ldrexd(temp_lo, temp_hi, MemOperand(temp_reg));
1190 __ Strexd(temp_lo, value_lo, value_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001191 __ Cmp(temp_lo, 0);
Artem Serov517d9f62016-12-12 15:51:15 +00001192 __ B(ne, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001193 } else {
1194 __ Strd(value_lo, value_hi, MemOperand(base, offset));
1195 }
1196 } else {
1197 value = RegisterFrom(locations->InAt(3));
1198 vixl32::Register source = value;
1199 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1200 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1201 __ Mov(temp, value);
1202 assembler->PoisonHeapReference(temp);
1203 source = temp;
1204 }
1205 __ Str(source, MemOperand(base, offset));
1206 }
1207
1208 if (is_volatile) {
1209 __ Dmb(vixl32::ISH);
1210 }
1211
1212 if (type == Primitive::kPrimNot) {
1213 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1214 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
1215 bool value_can_be_null = true; // TODO: Worth finding out this information?
1216 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
1217 }
1218}
1219
1220void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePut(HInvoke* invoke) {
1221 GenUnsafePut(invoke->GetLocations(),
1222 Primitive::kPrimInt,
1223 /* is_volatile */ false,
1224 /* is_ordered */ false,
1225 codegen_);
1226}
1227void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
1228 GenUnsafePut(invoke->GetLocations(),
1229 Primitive::kPrimInt,
1230 /* is_volatile */ false,
1231 /* is_ordered */ true,
1232 codegen_);
1233}
1234void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
1235 GenUnsafePut(invoke->GetLocations(),
1236 Primitive::kPrimInt,
1237 /* is_volatile */ true,
1238 /* is_ordered */ false,
1239 codegen_);
1240}
1241void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
1242 GenUnsafePut(invoke->GetLocations(),
1243 Primitive::kPrimNot,
1244 /* is_volatile */ false,
1245 /* is_ordered */ false,
1246 codegen_);
1247}
1248void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1249 GenUnsafePut(invoke->GetLocations(),
1250 Primitive::kPrimNot,
1251 /* is_volatile */ false,
1252 /* is_ordered */ true,
1253 codegen_);
1254}
1255void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1256 GenUnsafePut(invoke->GetLocations(),
1257 Primitive::kPrimNot,
1258 /* is_volatile */ true,
1259 /* is_ordered */ false,
1260 codegen_);
1261}
1262void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
1263 GenUnsafePut(invoke->GetLocations(),
1264 Primitive::kPrimLong,
1265 /* is_volatile */ false,
1266 /* is_ordered */ false,
1267 codegen_);
1268}
1269void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1270 GenUnsafePut(invoke->GetLocations(),
1271 Primitive::kPrimLong,
1272 /* is_volatile */ false,
1273 /* is_ordered */ true,
1274 codegen_);
1275}
1276void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1277 GenUnsafePut(invoke->GetLocations(),
1278 Primitive::kPrimLong,
1279 /* is_volatile */ true,
1280 /* is_ordered */ false,
1281 codegen_);
1282}
1283
1284static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena,
1285 HInvoke* invoke,
1286 Primitive::Type type) {
1287 bool can_call = kEmitCompilerReadBarrier &&
1288 kUseBakerReadBarrier &&
1289 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
1290 LocationSummary* locations = new (arena) LocationSummary(invoke,
1291 (can_call
1292 ? LocationSummary::kCallOnSlowPath
1293 : LocationSummary::kNoCall),
1294 kIntrinsified);
1295 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1296 locations->SetInAt(1, Location::RequiresRegister());
1297 locations->SetInAt(2, Location::RequiresRegister());
1298 locations->SetInAt(3, Location::RequiresRegister());
1299 locations->SetInAt(4, Location::RequiresRegister());
1300
1301 // If heap poisoning is enabled, we don't want the unpoisoning
1302 // operations to potentially clobber the output. Likewise when
1303 // emitting a (Baker) read barrier, which may call.
1304 Location::OutputOverlap overlaps =
1305 ((kPoisonHeapReferences && type == Primitive::kPrimNot) || can_call)
1306 ? Location::kOutputOverlap
1307 : Location::kNoOutputOverlap;
1308 locations->SetOut(Location::RequiresRegister(), overlaps);
1309
1310 // Temporary registers used in CAS. In the object case
1311 // (UnsafeCASObject intrinsic), these are also used for
1312 // card-marking, and possibly for (Baker) read barrier.
1313 locations->AddTemp(Location::RequiresRegister()); // Pointer.
1314 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
1315}
1316
1317static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorARMVIXL* codegen) {
1318 DCHECK_NE(type, Primitive::kPrimLong);
1319
1320 ArmVIXLAssembler* assembler = codegen->GetAssembler();
1321 LocationSummary* locations = invoke->GetLocations();
1322
1323 Location out_loc = locations->Out();
1324 vixl32::Register out = OutputRegister(invoke); // Boolean result.
1325
1326 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
1327 Location offset_loc = locations->InAt(2);
1328 vixl32::Register offset = LowRegisterFrom(offset_loc); // Offset (discard high 4B).
1329 vixl32::Register expected = InputRegisterAt(invoke, 3); // Expected.
1330 vixl32::Register value = InputRegisterAt(invoke, 4); // Value.
1331
1332 Location tmp_ptr_loc = locations->GetTemp(0);
1333 vixl32::Register tmp_ptr = RegisterFrom(tmp_ptr_loc); // Pointer to actual memory.
1334 vixl32::Register tmp = RegisterFrom(locations->GetTemp(1)); // Value in memory.
1335
1336 if (type == Primitive::kPrimNot) {
1337 // The only read barrier implementation supporting the
1338 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1339 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1340
1341 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1342 // object and scan the receiver at the next GC for nothing.
1343 bool value_can_be_null = true; // TODO: Worth finding out this information?
1344 codegen->MarkGCCard(tmp_ptr, tmp, base, value, value_can_be_null);
1345
1346 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1347 // Need to make sure the reference stored in the field is a to-space
1348 // one before attempting the CAS or the CAS could fail incorrectly.
1349 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1350 invoke,
1351 out_loc, // Unused, used only as a "temporary" within the read barrier.
1352 base,
1353 /* offset */ 0u,
1354 /* index */ offset_loc,
1355 ScaleFactor::TIMES_1,
1356 tmp_ptr_loc,
1357 /* needs_null_check */ false,
1358 /* always_update_field */ true,
1359 &tmp);
1360 }
1361 }
1362
1363 // Prevent reordering with prior memory operations.
1364 // Emit a DMB ISH instruction instead of an DMB ISHST one, as the
1365 // latter allows a preceding load to be delayed past the STXR
1366 // instruction below.
1367 __ Dmb(vixl32::ISH);
1368
1369 __ Add(tmp_ptr, base, offset);
1370
1371 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1372 codegen->GetAssembler()->PoisonHeapReference(expected);
1373 if (value.Is(expected)) {
1374 // Do not poison `value`, as it is the same register as
1375 // `expected`, which has just been poisoned.
1376 } else {
1377 codegen->GetAssembler()->PoisonHeapReference(value);
1378 }
1379 }
1380
1381 // do {
1382 // tmp = [r_ptr] - expected;
1383 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1384 // result = tmp != 0;
1385
1386 vixl32::Label loop_head;
1387 __ Bind(&loop_head);
1388
Scott Wakelingb77051e2016-11-21 19:46:00 +00001389 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001390
1391 __ Subs(tmp, tmp, expected);
1392
1393 {
Artem Serov0fb37192016-12-06 18:13:40 +00001394 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1395 3 * kMaxInstructionSizeInBytes,
1396 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001397
1398 __ itt(eq);
Scott Wakelingb77051e2016-11-21 19:46:00 +00001399 __ strex(eq, tmp, value, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001400 __ cmp(eq, tmp, 1);
1401 }
1402
Artem Serov517d9f62016-12-12 15:51:15 +00001403 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001404
1405 __ Dmb(vixl32::ISH);
1406
1407 __ Rsbs(out, tmp, 1);
1408
1409 {
Artem Serov0fb37192016-12-06 18:13:40 +00001410 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1411 2 * kMaxInstructionSizeInBytes,
1412 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001413
1414 __ it(cc);
1415 __ mov(cc, out, 0);
1416 }
1417
1418 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1419 codegen->GetAssembler()->UnpoisonHeapReference(expected);
1420 if (value.Is(expected)) {
1421 // Do not unpoison `value`, as it is the same register as
1422 // `expected`, which has just been unpoisoned.
1423 } else {
1424 codegen->GetAssembler()->UnpoisonHeapReference(value);
1425 }
1426 }
1427}
1428
1429void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
1430 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimInt);
1431}
1432void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1433 // The only read barrier implementation supporting the
1434 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1435 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1436 return;
1437 }
1438
1439 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimNot);
1440}
1441void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
1442 GenCas(invoke, Primitive::kPrimInt, codegen_);
1443}
1444void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1445 // The only read barrier implementation supporting the
1446 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1447 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1448
1449 GenCas(invoke, Primitive::kPrimNot, codegen_);
1450}
1451
1452void IntrinsicLocationsBuilderARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1453 // The inputs plus one temp.
1454 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1455 invoke->InputAt(1)->CanBeNull()
1456 ? LocationSummary::kCallOnSlowPath
1457 : LocationSummary::kNoCall,
1458 kIntrinsified);
1459 locations->SetInAt(0, Location::RequiresRegister());
1460 locations->SetInAt(1, Location::RequiresRegister());
1461 locations->AddTemp(Location::RequiresRegister());
1462 locations->AddTemp(Location::RequiresRegister());
1463 locations->AddTemp(Location::RequiresRegister());
1464 // Need temporary registers for String compression's feature.
1465 if (mirror::kUseStringCompression) {
1466 locations->AddTemp(Location::RequiresRegister());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001467 }
1468 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1469}
1470
1471void IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1472 ArmVIXLAssembler* assembler = GetAssembler();
1473 LocationSummary* locations = invoke->GetLocations();
1474
1475 vixl32::Register str = InputRegisterAt(invoke, 0);
1476 vixl32::Register arg = InputRegisterAt(invoke, 1);
1477 vixl32::Register out = OutputRegister(invoke);
1478
1479 vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1480 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1481 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001482 vixl32::Register temp3;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001483 if (mirror::kUseStringCompression) {
1484 temp3 = RegisterFrom(locations->GetTemp(3));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001485 }
1486
1487 vixl32::Label loop;
1488 vixl32::Label find_char_diff;
1489 vixl32::Label end;
1490 vixl32::Label different_compression;
1491
1492 // Get offsets of count and value fields within a string object.
1493 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1494 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1495
1496 // Note that the null check must have been done earlier.
1497 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1498
1499 // Take slow path and throw if input can be and is null.
1500 SlowPathCodeARMVIXL* slow_path = nullptr;
1501 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1502 if (can_slow_path) {
1503 slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
1504 codegen_->AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001505 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001506 }
1507
1508 // Reference equality check, return 0 if same reference.
1509 __ Subs(out, str, arg);
1510 __ B(eq, &end);
1511
Anton Kirilov5ec62182016-10-13 20:16:02 +01001512 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001513 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001514 __ Ldr(temp3, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001515 __ Ldr(temp2, MemOperand(arg, count_offset));
1516 // Extract lengths from the `count` fields.
1517 __ Lsr(temp0, temp3, 1u);
1518 __ Lsr(temp1, temp2, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001519 } else {
1520 // Load lengths of this and argument strings.
1521 __ Ldr(temp0, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001522 __ Ldr(temp1, MemOperand(arg, count_offset));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001523 }
1524 // out = length diff.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001525 __ Subs(out, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001526 // temp0 = min(len(str), len(arg)).
1527
1528 {
Artem Serov0fb37192016-12-06 18:13:40 +00001529 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1530 2 * kMaxInstructionSizeInBytes,
1531 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001532
1533 __ it(gt);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001534 __ mov(gt, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001535 }
1536
Anton Kirilov5ec62182016-10-13 20:16:02 +01001537 // Shorter string is empty?
xueliang.zhongf51bc622016-11-04 09:23:32 +00001538 // Note that mirror::kUseStringCompression==true introduces lots of instructions,
1539 // which makes &end label far away from this branch and makes it not 'CBZ-encodable'.
1540 __ CompareAndBranchIfZero(temp0, &end, mirror::kUseStringCompression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001541
1542 if (mirror::kUseStringCompression) {
1543 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001544 __ Eors(temp2, temp2, temp3);
1545 __ Lsrs(temp2, temp2, 1u);
1546 __ B(cs, &different_compression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001547 // For string compression, calculate the number of bytes to compare (not chars).
1548 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001549 __ Lsls(temp3, temp3, 31u); // Extract purely the compression flag.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001550
Artem Serov0fb37192016-12-06 18:13:40 +00001551 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1552 2 * kMaxInstructionSizeInBytes,
1553 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001554
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001555 __ it(ne);
1556 __ add(ne, temp0, temp0, temp0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001557 }
1558
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001559 // Store offset of string value in preparation for comparison loop.
1560 __ Mov(temp1, value_offset);
1561
Anton Kirilov5ec62182016-10-13 20:16:02 +01001562 // Assertions that must hold in order to compare multiple characters at a time.
1563 CHECK_ALIGNED(value_offset, 8);
1564 static_assert(IsAligned<8>(kObjectAlignment),
1565 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1566
Scott Wakelingb77051e2016-11-21 19:46:00 +00001567 const unsigned char_size = Primitive::ComponentSize(Primitive::kPrimChar);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001568 DCHECK_EQ(char_size, 2u);
1569
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001570 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1571
Anton Kirilov5ec62182016-10-13 20:16:02 +01001572 vixl32::Label find_char_diff_2nd_cmp;
1573 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1574 __ Bind(&loop);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001575 vixl32::Register temp_reg = temps.Acquire();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001576 __ Ldr(temp_reg, MemOperand(str, temp1));
1577 __ Ldr(temp2, MemOperand(arg, temp1));
1578 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001579 __ B(ne, &find_char_diff, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001580 __ Add(temp1, temp1, char_size * 2);
1581
1582 __ Ldr(temp_reg, MemOperand(str, temp1));
1583 __ Ldr(temp2, MemOperand(arg, temp1));
1584 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001585 __ B(ne, &find_char_diff_2nd_cmp, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001586 __ Add(temp1, temp1, char_size * 2);
1587 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1588 __ Subs(temp0, temp0, (mirror::kUseStringCompression ? 8 : 4));
Artem Serov517d9f62016-12-12 15:51:15 +00001589 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001590 __ B(&end);
1591
1592 __ Bind(&find_char_diff_2nd_cmp);
1593 if (mirror::kUseStringCompression) {
1594 __ Subs(temp0, temp0, 4); // 4 bytes previously compared.
Artem Serov517d9f62016-12-12 15:51:15 +00001595 __ B(ls, &end, /* far_target */ false); // Was the second comparison fully beyond the end?
Anton Kirilov5ec62182016-10-13 20:16:02 +01001596 } else {
1597 // Without string compression, we can start treating temp0 as signed
1598 // and rely on the signed comparison below.
1599 __ Sub(temp0, temp0, 2);
1600 }
1601
1602 // Find the single character difference.
1603 __ Bind(&find_char_diff);
1604 // Get the bit position of the first character that differs.
1605 __ Eor(temp1, temp2, temp_reg);
1606 __ Rbit(temp1, temp1);
1607 __ Clz(temp1, temp1);
1608
1609 // temp0 = number of characters remaining to compare.
1610 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1611 // in the comparison loop, and after the end of the shorter string data).
1612
1613 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1614 // two words compared, in the interval [0,1].
1615 // (0 for low half-word different, 1 for high half-word different).
1616 // With string compression, (temp1 << 3) = byte where the difference occurs,
1617 // in the interval [0,3].
1618
1619 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1620 // the remaining string data, so just return length diff (out).
1621 // The comparison is unsigned for string compression, otherwise signed.
1622 __ Cmp(temp0, Operand(temp1, vixl32::LSR, (mirror::kUseStringCompression ? 3 : 4)));
Artem Serov517d9f62016-12-12 15:51:15 +00001623 __ B((mirror::kUseStringCompression ? ls : le), &end, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001624
Anton Kirilov5ec62182016-10-13 20:16:02 +01001625 // Extract the characters and calculate the difference.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001626 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001627 // For compressed strings we need to clear 0x7 from temp1, for uncompressed we need to clear
1628 // 0xf. We also need to prepare the character extraction mask `uncompressed ? 0xffffu : 0xffu`.
1629 // The compression flag is now in the highest bit of temp3, so let's play some tricks.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001630 __ Orr(temp3, temp3, 0xffu << 23); // uncompressed ? 0xff800000u : 0x7ff80000u
1631 __ Bic(temp1, temp1, Operand(temp3, vixl32::LSR, 31 - 3)); // &= ~(uncompressed ? 0xfu : 0x7u)
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001632 __ Asr(temp3, temp3, 7u); // uncompressed ? 0xffff0000u : 0xff0000u.
1633 __ Lsr(temp2, temp2, temp1); // Extract second character.
1634 __ Lsr(temp3, temp3, 16u); // uncompressed ? 0xffffu : 0xffu
1635 __ Lsr(out, temp_reg, temp1); // Extract first character.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001636 __ And(temp2, temp2, temp3);
1637 __ And(out, out, temp3);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001638 } else {
Anton Kirilovb88c4842016-11-14 14:37:00 +00001639 __ Bic(temp1, temp1, 0xf);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001640 __ Lsr(temp2, temp2, temp1);
1641 __ Lsr(out, temp_reg, temp1);
Anton Kirilovb88c4842016-11-14 14:37:00 +00001642 __ Movt(temp2, 0);
1643 __ Movt(out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001644 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001645
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001646 __ Sub(out, out, temp2);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001647 temps.Release(temp_reg);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001648
1649 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001650 __ B(&end);
1651 __ Bind(&different_compression);
1652
1653 // Comparison for different compression style.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001654 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1655 DCHECK_EQ(c_char_size, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001656
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001657 // We want to free up the temp3, currently holding `str.count`, for comparison.
1658 // So, we move it to the bottom bit of the iteration count `temp0` which we tnen
1659 // need to treat as unsigned. Start by freeing the bit with an ADD and continue
1660 // further down by a LSRS+SBC which will flip the meaning of the flag but allow
1661 // `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001662 __ Add(temp0, temp0, temp0); // Unlike LSL, this ADD is always 16-bit.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001663 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001664 __ Mov(temp1, str);
1665 __ Mov(temp2, arg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001666 __ Lsrs(temp3, temp3, 1u); // Continue the move of the compression flag.
1667 {
Artem Serov0fb37192016-12-06 18:13:40 +00001668 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1669 3 * kMaxInstructionSizeInBytes,
1670 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001671 __ itt(cs); // Interleave with selection of temp1 and temp2.
1672 __ mov(cs, temp1, arg); // Preserves flags.
1673 __ mov(cs, temp2, str); // Preserves flags.
1674 }
Anton Kirilovb88c4842016-11-14 14:37:00 +00001675 __ Sbc(temp0, temp0, 0); // Complete the move of the compression flag.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001676
1677 // Adjust temp1 and temp2 from string pointers to data pointers.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001678 __ Add(temp1, temp1, value_offset);
1679 __ Add(temp2, temp2, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001680
1681 vixl32::Label different_compression_loop;
1682 vixl32::Label different_compression_diff;
1683
1684 // Main loop for different compression.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001685 temp_reg = temps.Acquire();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001686 __ Bind(&different_compression_loop);
1687 __ Ldrb(temp_reg, MemOperand(temp1, c_char_size, PostIndex));
1688 __ Ldrh(temp3, MemOperand(temp2, char_size, PostIndex));
Anton Kirilovb88c4842016-11-14 14:37:00 +00001689 __ Cmp(temp_reg, temp3);
Artem Serov517d9f62016-12-12 15:51:15 +00001690 __ B(ne, &different_compression_diff, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001691 __ Subs(temp0, temp0, 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001692 __ B(hi, &different_compression_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001693 __ B(&end);
1694
1695 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001696 __ Bind(&different_compression_diff);
1697 __ Sub(out, temp_reg, temp3);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001698 temps.Release(temp_reg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001699 // Flip the difference if the `arg` is compressed.
1700 // `temp0` contains inverted `str` compression flag, i.e the same as `arg` compression flag.
1701 __ Lsrs(temp0, temp0, 1u);
1702 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1703 "Expecting 0=compressed, 1=uncompressed");
1704
Artem Serov0fb37192016-12-06 18:13:40 +00001705 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1706 2 * kMaxInstructionSizeInBytes,
1707 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001708 __ it(cc);
1709 __ rsb(cc, out, out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001710 }
1711
1712 __ Bind(&end);
1713
1714 if (can_slow_path) {
1715 __ Bind(slow_path->GetExitLabel());
1716 }
1717}
1718
1719void IntrinsicLocationsBuilderARMVIXL::VisitStringEquals(HInvoke* invoke) {
1720 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1721 LocationSummary::kNoCall,
1722 kIntrinsified);
1723 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1724 locations->SetInAt(0, Location::RequiresRegister());
1725 locations->SetInAt(1, Location::RequiresRegister());
1726 // Temporary registers to store lengths of strings and for calculations.
1727 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1728 locations->AddTemp(LocationFrom(r0));
1729 locations->AddTemp(Location::RequiresRegister());
1730 locations->AddTemp(Location::RequiresRegister());
1731
1732 locations->SetOut(Location::RequiresRegister());
1733}
1734
1735void IntrinsicCodeGeneratorARMVIXL::VisitStringEquals(HInvoke* invoke) {
1736 ArmVIXLAssembler* assembler = GetAssembler();
1737 LocationSummary* locations = invoke->GetLocations();
1738
1739 vixl32::Register str = InputRegisterAt(invoke, 0);
1740 vixl32::Register arg = InputRegisterAt(invoke, 1);
1741 vixl32::Register out = OutputRegister(invoke);
1742
1743 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1744 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1745 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
1746
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001747 vixl32::Label loop;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001748 vixl32::Label end;
1749 vixl32::Label return_true;
1750 vixl32::Label return_false;
Anton Kirilov6f644202017-02-27 18:29:45 +00001751 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001752
1753 // Get offsets of count, value, and class fields within a string object.
1754 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1755 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1756 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1757
1758 // Note that the null check must have been done earlier.
1759 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1760
1761 StringEqualsOptimizations optimizations(invoke);
1762 if (!optimizations.GetArgumentNotNull()) {
1763 // Check if input is null, return false if it is.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001764 __ CompareAndBranchIfZero(arg, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001765 }
1766
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001767 // Reference equality check, return true if same reference.
1768 __ Cmp(str, arg);
Artem Serov517d9f62016-12-12 15:51:15 +00001769 __ B(eq, &return_true, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001770
Anton Kirilov5ec62182016-10-13 20:16:02 +01001771 if (!optimizations.GetArgumentIsString()) {
1772 // Instanceof check for the argument by comparing class fields.
1773 // All string objects must have the same type since String cannot be subclassed.
1774 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1775 // If the argument is a string object, its class field must be equal to receiver's class field.
1776 __ Ldr(temp, MemOperand(str, class_offset));
1777 __ Ldr(temp1, MemOperand(arg, class_offset));
1778 __ Cmp(temp, temp1);
Artem Serov517d9f62016-12-12 15:51:15 +00001779 __ B(ne, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001780 }
1781
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001782 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001783 __ Ldr(temp, MemOperand(str, count_offset));
1784 __ Ldr(temp1, MemOperand(arg, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001785 // Check if `count` fields are equal, return false if they're not.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001786 // Also compares the compression style, if differs return false.
1787 __ Cmp(temp, temp1);
Artem Serov517d9f62016-12-12 15:51:15 +00001788 __ B(ne, &return_false, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001789 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1790 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1791 "Expecting 0=compressed, 1=uncompressed");
xueliang.zhongf51bc622016-11-04 09:23:32 +00001792 __ CompareAndBranchIfZero(temp, &return_true, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001793
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001794 // Assertions that must hold in order to compare strings 4 bytes at a time.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001795 DCHECK_ALIGNED(value_offset, 4);
1796 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
1797
1798 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001799 // For string compression, calculate the number of bytes to compare (not chars).
1800 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1801 __ Lsrs(temp, temp, 1u); // Extract length and check compression flag.
Artem Serov0fb37192016-12-06 18:13:40 +00001802 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1803 2 * kMaxInstructionSizeInBytes,
1804 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001805 __ it(cs); // If uncompressed,
1806 __ add(cs, temp, temp, temp); // double the byte count.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001807 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001808
1809 // Store offset of string value in preparation for comparison loop.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001810 __ Mov(temp1, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001811
1812 // Loop to compare strings 4 bytes at a time starting at the front of the string.
1813 // Ok to do this because strings are zero-padded to kObjectAlignment.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001814 __ Bind(&loop);
1815 __ Ldr(out, MemOperand(str, temp1));
1816 __ Ldr(temp2, MemOperand(arg, temp1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00001817 __ Add(temp1, temp1, Operand::From(sizeof(uint32_t)));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001818 __ Cmp(out, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001819 __ B(ne, &return_false, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001820 // With string compression, we have compared 4 bytes, otherwise 2 chars.
1821 __ Subs(temp, temp, mirror::kUseStringCompression ? 4 : 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001822 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001823
1824 // Return true and exit the function.
1825 // If loop does not result in returning false, we return true.
1826 __ Bind(&return_true);
1827 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00001828 __ B(final_label);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001829
1830 // Return false and exit the function.
1831 __ Bind(&return_false);
1832 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00001833
1834 if (end.IsReferenced()) {
1835 __ Bind(&end);
1836 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001837}
1838
1839static void GenerateVisitStringIndexOf(HInvoke* invoke,
1840 ArmVIXLAssembler* assembler,
1841 CodeGeneratorARMVIXL* codegen,
1842 ArenaAllocator* allocator,
1843 bool start_at_zero) {
1844 LocationSummary* locations = invoke->GetLocations();
1845
1846 // Note that the null check must have been done earlier.
1847 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1848
1849 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1850 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
1851 SlowPathCodeARMVIXL* slow_path = nullptr;
1852 HInstruction* code_point = invoke->InputAt(1);
1853 if (code_point->IsIntConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001854 if (static_cast<uint32_t>(Int32ConstantFrom(code_point)) >
Anton Kirilov5ec62182016-10-13 20:16:02 +01001855 std::numeric_limits<uint16_t>::max()) {
1856 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1857 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1858 slow_path = new (allocator) IntrinsicSlowPathARMVIXL(invoke);
1859 codegen->AddSlowPath(slow_path);
1860 __ B(slow_path->GetEntryLabel());
1861 __ Bind(slow_path->GetExitLabel());
1862 return;
1863 }
1864 } else if (code_point->GetType() != Primitive::kPrimChar) {
1865 vixl32::Register char_reg = InputRegisterAt(invoke, 1);
1866 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1867 __ Cmp(char_reg, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1);
1868 slow_path = new (allocator) IntrinsicSlowPathARMVIXL(invoke);
1869 codegen->AddSlowPath(slow_path);
1870 __ B(hs, slow_path->GetEntryLabel());
1871 }
1872
1873 if (start_at_zero) {
1874 vixl32::Register tmp_reg = RegisterFrom(locations->GetTemp(0));
1875 DCHECK(tmp_reg.Is(r2));
1876 // Start-index = 0.
1877 __ Mov(tmp_reg, 0);
1878 }
1879
1880 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
1881 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
1882
1883 if (slow_path != nullptr) {
1884 __ Bind(slow_path->GetExitLabel());
1885 }
1886}
1887
1888void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
1889 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1890 LocationSummary::kCallOnMainAndSlowPath,
1891 kIntrinsified);
1892 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1893 // best to align the inputs accordingly.
1894 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1895 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1896 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1897 locations->SetOut(LocationFrom(r0));
1898
1899 // Need to send start-index=0.
1900 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1901}
1902
1903void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
1904 GenerateVisitStringIndexOf(
1905 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
1906}
1907
1908void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
1909 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1910 LocationSummary::kCallOnMainAndSlowPath,
1911 kIntrinsified);
1912 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1913 // best to align the inputs accordingly.
1914 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1915 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1916 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1917 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1918 locations->SetOut(LocationFrom(r0));
1919}
1920
1921void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
1922 GenerateVisitStringIndexOf(
1923 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
1924}
1925
1926void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
1927 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1928 LocationSummary::kCallOnMainAndSlowPath,
1929 kIntrinsified);
1930 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1931 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1932 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1933 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1934 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1935 locations->SetOut(LocationFrom(r0));
1936}
1937
1938void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
1939 ArmVIXLAssembler* assembler = GetAssembler();
1940 vixl32::Register byte_array = InputRegisterAt(invoke, 0);
1941 __ Cmp(byte_array, 0);
1942 SlowPathCodeARMVIXL* slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
1943 codegen_->AddSlowPath(slow_path);
1944 __ B(eq, slow_path->GetEntryLabel());
1945
1946 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
1947 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
1948 __ Bind(slow_path->GetExitLabel());
1949}
1950
1951void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
1952 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1953 LocationSummary::kCallOnMainOnly,
1954 kIntrinsified);
1955 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1956 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1957 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1958 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1959 locations->SetOut(LocationFrom(r0));
1960}
1961
1962void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
1963 // No need to emit code checking whether `locations->InAt(2)` is a null
1964 // pointer, as callers of the native method
1965 //
1966 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1967 //
1968 // all include a null check on `data` before calling that method.
1969 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
1970 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
1971}
1972
1973void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
1974 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1975 LocationSummary::kCallOnMainAndSlowPath,
1976 kIntrinsified);
1977 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1978 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1979 locations->SetOut(LocationFrom(r0));
1980}
1981
1982void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
1983 ArmVIXLAssembler* assembler = GetAssembler();
1984 vixl32::Register string_to_copy = InputRegisterAt(invoke, 0);
1985 __ Cmp(string_to_copy, 0);
1986 SlowPathCodeARMVIXL* slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
1987 codegen_->AddSlowPath(slow_path);
1988 __ B(eq, slow_path->GetEntryLabel());
1989
1990 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
1991 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
1992
1993 __ Bind(slow_path->GetExitLabel());
1994}
1995
1996void IntrinsicLocationsBuilderARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1997 // The only read barrier implementation supporting the
1998 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1999 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
2000 return;
2001 }
2002
2003 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2004 LocationSummary* locations = invoke->GetLocations();
2005 if (locations == nullptr) {
2006 return;
2007 }
2008
2009 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2010 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2011 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2012
2013 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
2014 locations->SetInAt(1, Location::RequiresRegister());
2015 }
2016 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
2017 locations->SetInAt(3, Location::RequiresRegister());
2018 }
2019 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
2020 locations->SetInAt(4, Location::RequiresRegister());
2021 }
2022 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2023 // Temporary register IP cannot be used in
2024 // ReadBarrierSystemArrayCopySlowPathARM (because that register
2025 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
2026 // temporary register from the register allocator.
2027 locations->AddTemp(Location::RequiresRegister());
2028 }
2029}
2030
2031static void CheckPosition(ArmVIXLAssembler* assembler,
2032 Location pos,
2033 vixl32::Register input,
2034 Location length,
2035 SlowPathCodeARMVIXL* slow_path,
2036 vixl32::Register temp,
2037 bool length_is_input_length = false) {
2038 // Where is the length in the Array?
2039 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
2040
2041 if (pos.IsConstant()) {
2042 int32_t pos_const = Int32ConstantFrom(pos);
2043 if (pos_const == 0) {
2044 if (!length_is_input_length) {
2045 // Check that length(input) >= length.
2046 __ Ldr(temp, MemOperand(input, length_offset));
2047 if (length.IsConstant()) {
2048 __ Cmp(temp, Int32ConstantFrom(length));
2049 } else {
2050 __ Cmp(temp, RegisterFrom(length));
2051 }
2052 __ B(lt, slow_path->GetEntryLabel());
2053 }
2054 } else {
2055 // Check that length(input) >= pos.
2056 __ Ldr(temp, MemOperand(input, length_offset));
2057 __ Subs(temp, temp, pos_const);
2058 __ B(lt, slow_path->GetEntryLabel());
2059
2060 // Check that (length(input) - pos) >= length.
2061 if (length.IsConstant()) {
2062 __ Cmp(temp, Int32ConstantFrom(length));
2063 } else {
2064 __ Cmp(temp, RegisterFrom(length));
2065 }
2066 __ B(lt, slow_path->GetEntryLabel());
2067 }
2068 } else if (length_is_input_length) {
2069 // The only way the copy can succeed is if pos is zero.
2070 vixl32::Register pos_reg = RegisterFrom(pos);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002071 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002072 } else {
2073 // Check that pos >= 0.
2074 vixl32::Register pos_reg = RegisterFrom(pos);
2075 __ Cmp(pos_reg, 0);
2076 __ B(lt, slow_path->GetEntryLabel());
2077
2078 // Check that pos <= length(input).
2079 __ Ldr(temp, MemOperand(input, length_offset));
2080 __ Subs(temp, temp, pos_reg);
2081 __ B(lt, slow_path->GetEntryLabel());
2082
2083 // Check that (length(input) - pos) >= length.
2084 if (length.IsConstant()) {
2085 __ Cmp(temp, Int32ConstantFrom(length));
2086 } else {
2087 __ Cmp(temp, RegisterFrom(length));
2088 }
2089 __ B(lt, slow_path->GetEntryLabel());
2090 }
2091}
2092
2093void IntrinsicCodeGeneratorARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
2094 // The only read barrier implementation supporting the
2095 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2096 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2097
2098 ArmVIXLAssembler* assembler = GetAssembler();
2099 LocationSummary* locations = invoke->GetLocations();
2100
2101 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2102 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2103 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2104 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2105 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
2106
2107 vixl32::Register src = InputRegisterAt(invoke, 0);
2108 Location src_pos = locations->InAt(1);
2109 vixl32::Register dest = InputRegisterAt(invoke, 2);
2110 Location dest_pos = locations->InAt(3);
2111 Location length = locations->InAt(4);
2112 Location temp1_loc = locations->GetTemp(0);
2113 vixl32::Register temp1 = RegisterFrom(temp1_loc);
2114 Location temp2_loc = locations->GetTemp(1);
2115 vixl32::Register temp2 = RegisterFrom(temp2_loc);
2116 Location temp3_loc = locations->GetTemp(2);
2117 vixl32::Register temp3 = RegisterFrom(temp3_loc);
2118
2119 SlowPathCodeARMVIXL* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
2120 codegen_->AddSlowPath(intrinsic_slow_path);
2121
2122 vixl32::Label conditions_on_positions_validated;
2123 SystemArrayCopyOptimizations optimizations(invoke);
2124
2125 // If source and destination are the same, we go to slow path if we need to do
2126 // forward copying.
2127 if (src_pos.IsConstant()) {
2128 int32_t src_pos_constant = Int32ConstantFrom(src_pos);
2129 if (dest_pos.IsConstant()) {
2130 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
2131 if (optimizations.GetDestinationIsSource()) {
2132 // Checked when building locations.
2133 DCHECK_GE(src_pos_constant, dest_pos_constant);
2134 } else if (src_pos_constant < dest_pos_constant) {
2135 __ Cmp(src, dest);
2136 __ B(eq, intrinsic_slow_path->GetEntryLabel());
2137 }
2138
2139 // Checked when building locations.
2140 DCHECK(!optimizations.GetDestinationIsSource()
2141 || (src_pos_constant >= Int32ConstantFrom(dest_pos)));
2142 } else {
2143 if (!optimizations.GetDestinationIsSource()) {
2144 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00002145 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002146 }
2147 __ Cmp(RegisterFrom(dest_pos), src_pos_constant);
2148 __ B(gt, intrinsic_slow_path->GetEntryLabel());
2149 }
2150 } else {
2151 if (!optimizations.GetDestinationIsSource()) {
2152 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00002153 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002154 }
2155 if (dest_pos.IsConstant()) {
2156 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
2157 __ Cmp(RegisterFrom(src_pos), dest_pos_constant);
2158 } else {
2159 __ Cmp(RegisterFrom(src_pos), RegisterFrom(dest_pos));
2160 }
2161 __ B(lt, intrinsic_slow_path->GetEntryLabel());
2162 }
2163
2164 __ Bind(&conditions_on_positions_validated);
2165
2166 if (!optimizations.GetSourceIsNotNull()) {
2167 // Bail out if the source is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002168 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002169 }
2170
2171 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2172 // Bail out if the destination is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002173 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002174 }
2175
2176 // If the length is negative, bail out.
2177 // We have already checked in the LocationsBuilder for the constant case.
2178 if (!length.IsConstant() &&
2179 !optimizations.GetCountIsSourceLength() &&
2180 !optimizations.GetCountIsDestinationLength()) {
2181 __ Cmp(RegisterFrom(length), 0);
2182 __ B(lt, intrinsic_slow_path->GetEntryLabel());
2183 }
2184
2185 // Validity checks: source.
2186 CheckPosition(assembler,
2187 src_pos,
2188 src,
2189 length,
2190 intrinsic_slow_path,
2191 temp1,
2192 optimizations.GetCountIsSourceLength());
2193
2194 // Validity checks: dest.
2195 CheckPosition(assembler,
2196 dest_pos,
2197 dest,
2198 length,
2199 intrinsic_slow_path,
2200 temp1,
2201 optimizations.GetCountIsDestinationLength());
2202
2203 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2204 // Check whether all elements of the source array are assignable to the component
2205 // type of the destination array. We do two checks: the classes are the same,
2206 // or the destination is Object[]. If none of these checks succeed, we go to the
2207 // slow path.
2208
2209 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2210 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2211 // /* HeapReference<Class> */ temp1 = src->klass_
2212 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2213 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2214 // Bail out if the source is not a non primitive array.
2215 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2216 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2217 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002218 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002219 // If heap poisoning is enabled, `temp1` has been unpoisoned
2220 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2221 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2222 __ Ldrh(temp1, MemOperand(temp1, primitive_offset));
2223 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002224 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002225 }
2226
2227 // /* HeapReference<Class> */ temp1 = dest->klass_
2228 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2229 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
2230
2231 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2232 // Bail out if the destination is not a non primitive array.
2233 //
2234 // Register `temp1` is not trashed by the read barrier emitted
2235 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2236 // method produces a call to a ReadBarrierMarkRegX entry point,
2237 // which saves all potentially live registers, including
2238 // temporaries such a `temp1`.
2239 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2240 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2241 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002242 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002243 // If heap poisoning is enabled, `temp2` has been unpoisoned
2244 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2245 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2246 __ Ldrh(temp2, MemOperand(temp2, primitive_offset));
2247 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002248 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002249 }
2250
2251 // For the same reason given earlier, `temp1` is not trashed by the
2252 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2253 // /* HeapReference<Class> */ temp2 = src->klass_
2254 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2255 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
2256 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2257 __ Cmp(temp1, temp2);
2258
2259 if (optimizations.GetDestinationIsTypedObjectArray()) {
2260 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002261 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002262 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2263 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2264 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
2265 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2266 // We do not need to emit a read barrier for the following
2267 // heap reference load, as `temp1` is only used in a
2268 // comparison with null below, and this reference is not
2269 // kept afterwards.
2270 __ Ldr(temp1, MemOperand(temp1, super_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002271 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002272 __ Bind(&do_copy);
2273 } else {
2274 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2275 }
2276 } else {
2277 // Non read barrier code.
2278
2279 // /* HeapReference<Class> */ temp1 = dest->klass_
2280 __ Ldr(temp1, MemOperand(dest, class_offset));
2281 // /* HeapReference<Class> */ temp2 = src->klass_
2282 __ Ldr(temp2, MemOperand(src, class_offset));
2283 bool did_unpoison = false;
2284 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2285 !optimizations.GetSourceIsNonPrimitiveArray()) {
2286 // One or two of the references need to be unpoisoned. Unpoison them
2287 // both to make the identity check valid.
2288 assembler->MaybeUnpoisonHeapReference(temp1);
2289 assembler->MaybeUnpoisonHeapReference(temp2);
2290 did_unpoison = true;
2291 }
2292
2293 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2294 // Bail out if the destination is not a non primitive array.
2295 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2296 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002297 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002298 assembler->MaybeUnpoisonHeapReference(temp3);
2299 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2300 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2301 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002302 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002303 }
2304
2305 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2306 // Bail out if the source is not a non primitive array.
2307 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2308 __ Ldr(temp3, MemOperand(temp2, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002309 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002310 assembler->MaybeUnpoisonHeapReference(temp3);
2311 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2312 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2313 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002314 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002315 }
2316
2317 __ Cmp(temp1, temp2);
2318
2319 if (optimizations.GetDestinationIsTypedObjectArray()) {
2320 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002321 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002322 if (!did_unpoison) {
2323 assembler->MaybeUnpoisonHeapReference(temp1);
2324 }
2325 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2326 __ Ldr(temp1, MemOperand(temp1, component_offset));
2327 assembler->MaybeUnpoisonHeapReference(temp1);
2328 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2329 __ Ldr(temp1, MemOperand(temp1, super_offset));
2330 // No need to unpoison the result, we're comparing against null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002331 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002332 __ Bind(&do_copy);
2333 } else {
2334 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2335 }
2336 }
2337 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2338 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2339 // Bail out if the source is not a non primitive array.
2340 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2341 // /* HeapReference<Class> */ temp1 = src->klass_
2342 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2343 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2344 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2345 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2346 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002347 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002348 // If heap poisoning is enabled, `temp3` has been unpoisoned
2349 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2350 } else {
2351 // /* HeapReference<Class> */ temp1 = src->klass_
2352 __ Ldr(temp1, MemOperand(src, class_offset));
2353 assembler->MaybeUnpoisonHeapReference(temp1);
2354 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2355 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002356 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002357 assembler->MaybeUnpoisonHeapReference(temp3);
2358 }
2359 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2360 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2361 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002362 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002363 }
2364
Roland Levillain1663d162017-03-17 15:15:21 +00002365 if (length.IsConstant() && Int32ConstantFrom(length) == 0) {
2366 // Null constant length: not need to emit the loop code at all.
Anton Kirilov5ec62182016-10-13 20:16:02 +01002367 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002368 vixl32::Label done;
2369 const Primitive::Type type = Primitive::kPrimNot;
2370 const int32_t element_size = Primitive::ComponentSize(type);
2371
2372 if (length.IsRegister()) {
2373 // Don't enter the copy loop if the length is null.
2374 __ CompareAndBranchIfZero(RegisterFrom(length), &done, /* is_far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002375 }
Roland Levillain1663d162017-03-17 15:15:21 +00002376
2377 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2378 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2379
2380 // SystemArrayCopy implementation for Baker read barriers (see
2381 // also CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier):
2382 //
2383 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2384 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2385 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2386 // if (is_gray) {
2387 // // Slow-path copy.
2388 // do {
2389 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2390 // } while (src_ptr != end_ptr)
2391 // } else {
2392 // // Fast-path copy.
2393 // do {
2394 // *dest_ptr++ = *src_ptr++;
2395 // } while (src_ptr != end_ptr)
2396 // }
2397
2398 // /* int32_t */ monitor = src->monitor_
2399 __ Ldr(temp2, MemOperand(src, monitor_offset));
2400 // /* LockWord */ lock_word = LockWord(monitor)
2401 static_assert(sizeof(LockWord) == sizeof(int32_t),
2402 "art::LockWord and int32_t have different sizes.");
2403
2404 // Introduce a dependency on the lock_word including the rb_state,
2405 // which shall prevent load-load reordering without using
2406 // a memory barrier (which would be more expensive).
2407 // `src` is unchanged by this operation, but its value now depends
2408 // on `temp2`.
2409 __ Add(src, src, Operand(temp2, vixl32::LSR, 32));
2410
2411 // Compute the base source address in `temp1`.
2412 // Note that `temp1` (the base source address) is computed from
2413 // `src` (and `src_pos`) here, and thus honors the artificial
2414 // dependency of `src` on `temp2`.
2415 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2416 // Compute the end source address in `temp3`.
2417 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2418 // The base destination address is computed later, as `temp2` is
2419 // used for intermediate computations.
2420
2421 // Slow path used to copy array when `src` is gray.
2422 // Note that the base destination address is computed in `temp2`
2423 // by the slow path code.
2424 SlowPathCodeARMVIXL* read_barrier_slow_path =
2425 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARMVIXL(invoke);
2426 codegen_->AddSlowPath(read_barrier_slow_path);
2427
2428 // Given the numeric representation, it's enough to check the low bit of the
2429 // rb_state. We do that by shifting the bit out of the lock word with LSRS
2430 // which can be a 16-bit instruction unlike the TST immediate.
2431 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2432 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2433 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
2434 // Carry flag is the last bit shifted out by LSRS.
2435 __ B(cs, read_barrier_slow_path->GetEntryLabel());
2436
2437 // Fast-path copy.
2438 // Compute the base destination address in `temp2`.
2439 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2440 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2441 // poison/unpoison.
2442 vixl32::Label loop;
2443 __ Bind(&loop);
2444 {
2445 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2446 const vixl32::Register temp_reg = temps.Acquire();
2447 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2448 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2449 }
2450 __ Cmp(temp1, temp3);
2451 __ B(ne, &loop, /* far_target */ false);
2452
2453 __ Bind(read_barrier_slow_path->GetExitLabel());
2454 } else {
2455 // Non read barrier code.
2456 // Compute the base source address in `temp1`.
2457 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2458 // Compute the base destination address in `temp2`.
2459 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2460 // Compute the end source address in `temp3`.
2461 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2462 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2463 // poison/unpoison.
2464 vixl32::Label loop;
2465 __ Bind(&loop);
2466 {
2467 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2468 const vixl32::Register temp_reg = temps.Acquire();
2469 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2470 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2471 }
2472 __ Cmp(temp1, temp3);
2473 __ B(ne, &loop, /* far_target */ false);
2474 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002475 __ Bind(&done);
2476 }
2477
2478 // We only need one card marking on the destination array.
2479 codegen_->MarkGCCard(temp1, temp2, dest, NoReg, /* value_can_be_null */ false);
2480
2481 __ Bind(intrinsic_slow_path->GetExitLabel());
2482}
2483
2484static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2485 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2486 // the code generator. Furthermore, the register allocator creates fixed live intervals
2487 // for all caller-saved registers because we are doing a function call. As a result, if
2488 // the input and output locations are unallocated, the register allocator runs out of
2489 // registers and fails; however, a debuggable graph is not the common case.
2490 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2491 return;
2492 }
2493
2494 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2495 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2496 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2497
2498 LocationSummary* const locations = new (arena) LocationSummary(invoke,
2499 LocationSummary::kCallOnMainOnly,
2500 kIntrinsified);
2501 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2502
2503 locations->SetInAt(0, Location::RequiresFpuRegister());
2504 locations->SetOut(Location::RequiresFpuRegister());
2505 // Native code uses the soft float ABI.
2506 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2507 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2508}
2509
2510static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2511 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2512 // the code generator. Furthermore, the register allocator creates fixed live intervals
2513 // for all caller-saved registers because we are doing a function call. As a result, if
2514 // the input and output locations are unallocated, the register allocator runs out of
2515 // registers and fails; however, a debuggable graph is not the common case.
2516 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2517 return;
2518 }
2519
2520 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2521 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2522 DCHECK_EQ(invoke->InputAt(1)->GetType(), Primitive::kPrimDouble);
2523 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2524
2525 LocationSummary* const locations = new (arena) LocationSummary(invoke,
2526 LocationSummary::kCallOnMainOnly,
2527 kIntrinsified);
2528 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2529
2530 locations->SetInAt(0, Location::RequiresFpuRegister());
2531 locations->SetInAt(1, Location::RequiresFpuRegister());
2532 locations->SetOut(Location::RequiresFpuRegister());
2533 // Native code uses the soft float ABI.
2534 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2535 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2536 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
2537 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(3)));
2538}
2539
2540static void GenFPToFPCall(HInvoke* invoke,
2541 ArmVIXLAssembler* assembler,
2542 CodeGeneratorARMVIXL* codegen,
2543 QuickEntrypointEnum entry) {
2544 LocationSummary* const locations = invoke->GetLocations();
2545
2546 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2547 DCHECK(locations->WillCall() && locations->Intrinsified());
2548
2549 // Native code uses the soft float ABI.
2550 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2551 RegisterFrom(locations->GetTemp(1)),
2552 InputDRegisterAt(invoke, 0));
2553 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2554 __ Vmov(OutputDRegister(invoke),
2555 RegisterFrom(locations->GetTemp(0)),
2556 RegisterFrom(locations->GetTemp(1)));
2557}
2558
2559static void GenFPFPToFPCall(HInvoke* invoke,
2560 ArmVIXLAssembler* assembler,
2561 CodeGeneratorARMVIXL* codegen,
2562 QuickEntrypointEnum entry) {
2563 LocationSummary* const locations = invoke->GetLocations();
2564
2565 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2566 DCHECK(locations->WillCall() && locations->Intrinsified());
2567
2568 // Native code uses the soft float ABI.
2569 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2570 RegisterFrom(locations->GetTemp(1)),
2571 InputDRegisterAt(invoke, 0));
2572 __ Vmov(RegisterFrom(locations->GetTemp(2)),
2573 RegisterFrom(locations->GetTemp(3)),
2574 InputDRegisterAt(invoke, 1));
2575 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2576 __ Vmov(OutputDRegister(invoke),
2577 RegisterFrom(locations->GetTemp(0)),
2578 RegisterFrom(locations->GetTemp(1)));
2579}
2580
2581void IntrinsicLocationsBuilderARMVIXL::VisitMathCos(HInvoke* invoke) {
2582 CreateFPToFPCallLocations(arena_, invoke);
2583}
2584
2585void IntrinsicCodeGeneratorARMVIXL::VisitMathCos(HInvoke* invoke) {
2586 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2587}
2588
2589void IntrinsicLocationsBuilderARMVIXL::VisitMathSin(HInvoke* invoke) {
2590 CreateFPToFPCallLocations(arena_, invoke);
2591}
2592
2593void IntrinsicCodeGeneratorARMVIXL::VisitMathSin(HInvoke* invoke) {
2594 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2595}
2596
2597void IntrinsicLocationsBuilderARMVIXL::VisitMathAcos(HInvoke* invoke) {
2598 CreateFPToFPCallLocations(arena_, invoke);
2599}
2600
2601void IntrinsicCodeGeneratorARMVIXL::VisitMathAcos(HInvoke* invoke) {
2602 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2603}
2604
2605void IntrinsicLocationsBuilderARMVIXL::VisitMathAsin(HInvoke* invoke) {
2606 CreateFPToFPCallLocations(arena_, invoke);
2607}
2608
2609void IntrinsicCodeGeneratorARMVIXL::VisitMathAsin(HInvoke* invoke) {
2610 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2611}
2612
2613void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan(HInvoke* invoke) {
2614 CreateFPToFPCallLocations(arena_, invoke);
2615}
2616
2617void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan(HInvoke* invoke) {
2618 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2619}
2620
2621void IntrinsicLocationsBuilderARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2622 CreateFPToFPCallLocations(arena_, invoke);
2623}
2624
2625void IntrinsicCodeGeneratorARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2626 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2627}
2628
2629void IntrinsicLocationsBuilderARMVIXL::VisitMathCosh(HInvoke* invoke) {
2630 CreateFPToFPCallLocations(arena_, invoke);
2631}
2632
2633void IntrinsicCodeGeneratorARMVIXL::VisitMathCosh(HInvoke* invoke) {
2634 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2635}
2636
2637void IntrinsicLocationsBuilderARMVIXL::VisitMathExp(HInvoke* invoke) {
2638 CreateFPToFPCallLocations(arena_, invoke);
2639}
2640
2641void IntrinsicCodeGeneratorARMVIXL::VisitMathExp(HInvoke* invoke) {
2642 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2643}
2644
2645void IntrinsicLocationsBuilderARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2646 CreateFPToFPCallLocations(arena_, invoke);
2647}
2648
2649void IntrinsicCodeGeneratorARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2650 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2651}
2652
2653void IntrinsicLocationsBuilderARMVIXL::VisitMathLog(HInvoke* invoke) {
2654 CreateFPToFPCallLocations(arena_, invoke);
2655}
2656
2657void IntrinsicCodeGeneratorARMVIXL::VisitMathLog(HInvoke* invoke) {
2658 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2659}
2660
2661void IntrinsicLocationsBuilderARMVIXL::VisitMathLog10(HInvoke* invoke) {
2662 CreateFPToFPCallLocations(arena_, invoke);
2663}
2664
2665void IntrinsicCodeGeneratorARMVIXL::VisitMathLog10(HInvoke* invoke) {
2666 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2667}
2668
2669void IntrinsicLocationsBuilderARMVIXL::VisitMathSinh(HInvoke* invoke) {
2670 CreateFPToFPCallLocations(arena_, invoke);
2671}
2672
2673void IntrinsicCodeGeneratorARMVIXL::VisitMathSinh(HInvoke* invoke) {
2674 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2675}
2676
2677void IntrinsicLocationsBuilderARMVIXL::VisitMathTan(HInvoke* invoke) {
2678 CreateFPToFPCallLocations(arena_, invoke);
2679}
2680
2681void IntrinsicCodeGeneratorARMVIXL::VisitMathTan(HInvoke* invoke) {
2682 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2683}
2684
2685void IntrinsicLocationsBuilderARMVIXL::VisitMathTanh(HInvoke* invoke) {
2686 CreateFPToFPCallLocations(arena_, invoke);
2687}
2688
2689void IntrinsicCodeGeneratorARMVIXL::VisitMathTanh(HInvoke* invoke) {
2690 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2691}
2692
2693void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2694 CreateFPFPToFPCallLocations(arena_, invoke);
2695}
2696
2697void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2698 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2699}
2700
2701void IntrinsicLocationsBuilderARMVIXL::VisitMathHypot(HInvoke* invoke) {
2702 CreateFPFPToFPCallLocations(arena_, invoke);
2703}
2704
2705void IntrinsicCodeGeneratorARMVIXL::VisitMathHypot(HInvoke* invoke) {
2706 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2707}
2708
2709void IntrinsicLocationsBuilderARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2710 CreateFPFPToFPCallLocations(arena_, invoke);
2711}
2712
2713void IntrinsicCodeGeneratorARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2714 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2715}
2716
2717void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2718 CreateIntToIntLocations(arena_, invoke);
2719}
2720
2721void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2722 ArmVIXLAssembler* assembler = GetAssembler();
2723 __ Rbit(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2724}
2725
2726void IntrinsicLocationsBuilderARMVIXL::VisitLongReverse(HInvoke* invoke) {
2727 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2728 LocationSummary::kNoCall,
2729 kIntrinsified);
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2732}
2733
2734void IntrinsicCodeGeneratorARMVIXL::VisitLongReverse(HInvoke* invoke) {
2735 ArmVIXLAssembler* assembler = GetAssembler();
2736 LocationSummary* locations = invoke->GetLocations();
2737
2738 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2739 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2740 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2741 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2742
2743 __ Rbit(out_reg_lo, in_reg_hi);
2744 __ Rbit(out_reg_hi, in_reg_lo);
2745}
2746
2747void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2748 CreateIntToIntLocations(arena_, invoke);
2749}
2750
2751void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2752 ArmVIXLAssembler* assembler = GetAssembler();
2753 __ Rev(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2754}
2755
2756void IntrinsicLocationsBuilderARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2757 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2758 LocationSummary::kNoCall,
2759 kIntrinsified);
2760 locations->SetInAt(0, Location::RequiresRegister());
2761 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2762}
2763
2764void IntrinsicCodeGeneratorARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2765 ArmVIXLAssembler* assembler = GetAssembler();
2766 LocationSummary* locations = invoke->GetLocations();
2767
2768 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2769 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2770 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2771 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2772
2773 __ Rev(out_reg_lo, in_reg_hi);
2774 __ Rev(out_reg_hi, in_reg_lo);
2775}
2776
2777void IntrinsicLocationsBuilderARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2778 CreateIntToIntLocations(arena_, invoke);
2779}
2780
2781void IntrinsicCodeGeneratorARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2782 ArmVIXLAssembler* assembler = GetAssembler();
2783 __ Revsh(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2784}
2785
2786static void GenBitCount(HInvoke* instr, Primitive::Type type, ArmVIXLAssembler* assembler) {
2787 DCHECK(Primitive::IsIntOrLongType(type)) << type;
2788 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
2789 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
2790
2791 bool is_long = type == Primitive::kPrimLong;
2792 LocationSummary* locations = instr->GetLocations();
2793 Location in = locations->InAt(0);
2794 vixl32::Register src_0 = is_long ? LowRegisterFrom(in) : RegisterFrom(in);
2795 vixl32::Register src_1 = is_long ? HighRegisterFrom(in) : src_0;
2796 vixl32::SRegister tmp_s = LowSRegisterFrom(locations->GetTemp(0));
2797 vixl32::DRegister tmp_d = DRegisterFrom(locations->GetTemp(0));
2798 vixl32::Register out_r = OutputRegister(instr);
2799
2800 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2801 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2802 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2803 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2804 __ Vmov(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2805 __ Vcnt(Untyped8, tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2806 __ Vpaddl(U8, tmp_d, tmp_d); // Temp DReg |--c|--c|--c|--c|
2807 __ Vpaddl(U16, tmp_d, tmp_d); // Temp DReg |------c|------c|
2808 if (is_long) {
2809 __ Vpaddl(U32, tmp_d, tmp_d); // Temp DReg |--------------c|
2810 }
2811 __ Vmov(out_r, tmp_s);
2812}
2813
2814void IntrinsicLocationsBuilderARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
2815 CreateIntToIntLocations(arena_, invoke);
2816 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2817}
2818
2819void IntrinsicCodeGeneratorARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
2820 GenBitCount(invoke, Primitive::kPrimInt, GetAssembler());
2821}
2822
2823void IntrinsicLocationsBuilderARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2824 VisitIntegerBitCount(invoke);
2825}
2826
2827void IntrinsicCodeGeneratorARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2828 GenBitCount(invoke, Primitive::kPrimLong, GetAssembler());
2829}
2830
2831void IntrinsicLocationsBuilderARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2832 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2833 LocationSummary::kNoCall,
2834 kIntrinsified);
2835 locations->SetInAt(0, Location::RequiresRegister());
2836 locations->SetInAt(1, Location::RequiresRegister());
2837 locations->SetInAt(2, Location::RequiresRegister());
2838 locations->SetInAt(3, Location::RequiresRegister());
2839 locations->SetInAt(4, Location::RequiresRegister());
2840
2841 // Temporary registers to store lengths of strings and for calculations.
2842 locations->AddTemp(Location::RequiresRegister());
2843 locations->AddTemp(Location::RequiresRegister());
2844 locations->AddTemp(Location::RequiresRegister());
2845}
2846
2847void IntrinsicCodeGeneratorARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2848 ArmVIXLAssembler* assembler = GetAssembler();
2849 LocationSummary* locations = invoke->GetLocations();
2850
2851 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2852 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2853 DCHECK_EQ(char_size, 2u);
2854
2855 // Location of data in char array buffer.
2856 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2857
2858 // Location of char array data in string.
2859 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2860
2861 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2862 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2863 vixl32::Register srcObj = InputRegisterAt(invoke, 0);
2864 vixl32::Register srcBegin = InputRegisterAt(invoke, 1);
2865 vixl32::Register srcEnd = InputRegisterAt(invoke, 2);
2866 vixl32::Register dstObj = InputRegisterAt(invoke, 3);
2867 vixl32::Register dstBegin = InputRegisterAt(invoke, 4);
2868
2869 vixl32::Register num_chr = RegisterFrom(locations->GetTemp(0));
2870 vixl32::Register src_ptr = RegisterFrom(locations->GetTemp(1));
2871 vixl32::Register dst_ptr = RegisterFrom(locations->GetTemp(2));
2872
2873 vixl32::Label done, compressed_string_loop;
Anton Kirilov6f644202017-02-27 18:29:45 +00002874 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002875 // dst to be copied.
2876 __ Add(dst_ptr, dstObj, data_offset);
2877 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, vixl32::LSL, 1));
2878
2879 __ Subs(num_chr, srcEnd, srcBegin);
2880 // Early out for valid zero-length retrievals.
Anton Kirilov6f644202017-02-27 18:29:45 +00002881 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002882
2883 // src range to copy.
2884 __ Add(src_ptr, srcObj, value_offset);
2885
2886 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2887 vixl32::Register temp;
2888 vixl32::Label compressed_string_preloop;
2889 if (mirror::kUseStringCompression) {
2890 // Location of count in string.
2891 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2892 temp = temps.Acquire();
2893 // String's length.
2894 __ Ldr(temp, MemOperand(srcObj, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002895 __ Tst(temp, 1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002896 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002897 __ B(eq, &compressed_string_preloop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002898 }
2899 __ Add(src_ptr, src_ptr, Operand(srcBegin, vixl32::LSL, 1));
2900
2901 // Do the copy.
2902 vixl32::Label loop, remainder;
2903
2904 temp = temps.Acquire();
2905 // Save repairing the value of num_chr on the < 4 character path.
2906 __ Subs(temp, num_chr, 4);
Artem Serov517d9f62016-12-12 15:51:15 +00002907 __ B(lt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002908
2909 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2910 __ Mov(num_chr, temp);
2911
2912 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
2913 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
2914 // to rectify these everywhere this intrinsic applies.)
2915 __ Bind(&loop);
2916 __ Ldr(temp, MemOperand(src_ptr, char_size * 2));
2917 __ Subs(num_chr, num_chr, 4);
2918 __ Str(temp, MemOperand(dst_ptr, char_size * 2));
2919 __ Ldr(temp, MemOperand(src_ptr, char_size * 4, PostIndex));
2920 __ Str(temp, MemOperand(dst_ptr, char_size * 4, PostIndex));
2921 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002922 __ B(ge, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002923
2924 __ Adds(num_chr, num_chr, 4);
Anton Kirilov6f644202017-02-27 18:29:45 +00002925 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002926
2927 // Main loop for < 4 character case and remainder handling. Loads and stores one
2928 // 16-bit Java character at a time.
2929 __ Bind(&remainder);
2930 temp = temps.Acquire();
2931 __ Ldrh(temp, MemOperand(src_ptr, char_size, PostIndex));
2932 __ Subs(num_chr, num_chr, 1);
2933 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2934 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002935 __ B(gt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002936
2937 if (mirror::kUseStringCompression) {
Anton Kirilov6f644202017-02-27 18:29:45 +00002938 __ B(final_label);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002939
Anton Kirilov5ec62182016-10-13 20:16:02 +01002940 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
2941 DCHECK_EQ(c_char_size, 1u);
2942 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2943 __ Bind(&compressed_string_preloop);
2944 __ Add(src_ptr, src_ptr, srcBegin);
2945 __ Bind(&compressed_string_loop);
2946 temp = temps.Acquire();
2947 __ Ldrb(temp, MemOperand(src_ptr, c_char_size, PostIndex));
2948 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2949 temps.Release(temp);
2950 __ Subs(num_chr, num_chr, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00002951 __ B(gt, &compressed_string_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002952 }
2953
Anton Kirilov6f644202017-02-27 18:29:45 +00002954 if (done.IsReferenced()) {
2955 __ Bind(&done);
2956 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002957}
2958
2959void IntrinsicLocationsBuilderARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
2960 CreateFPToIntLocations(arena_, invoke);
2961}
2962
2963void IntrinsicCodeGeneratorARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
2964 ArmVIXLAssembler* const assembler = GetAssembler();
2965 const vixl32::Register out = OutputRegister(invoke);
2966 // Shifting left by 1 bit makes the value encodable as an immediate operand;
2967 // we don't care about the sign bit anyway.
2968 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
2969
2970 __ Vmov(out, InputSRegisterAt(invoke, 0));
2971 // We don't care about the sign bit, so shift left.
2972 __ Lsl(out, out, 1);
2973 __ Eor(out, out, infinity);
2974 // If the result is 0, then it has 32 leading zeros, and less than that otherwise.
2975 __ Clz(out, out);
2976 // Any number less than 32 logically shifted right by 5 bits results in 0;
2977 // the same operation on 32 yields 1.
2978 __ Lsr(out, out, 5);
2979}
2980
2981void IntrinsicLocationsBuilderARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
2982 CreateFPToIntLocations(arena_, invoke);
2983}
2984
2985void IntrinsicCodeGeneratorARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
2986 ArmVIXLAssembler* const assembler = GetAssembler();
2987 const vixl32::Register out = OutputRegister(invoke);
2988 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2989 const vixl32::Register temp = temps.Acquire();
2990 // The highest 32 bits of double precision positive infinity separated into
2991 // two constants encodable as immediate operands.
2992 constexpr uint32_t infinity_high = 0x7f000000U;
2993 constexpr uint32_t infinity_high2 = 0x00f00000U;
2994
2995 static_assert((infinity_high | infinity_high2) ==
2996 static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
2997 "The constants do not add up to the high 32 bits of double "
2998 "precision positive infinity.");
2999 __ Vmov(temp, out, InputDRegisterAt(invoke, 0));
3000 __ Eor(out, out, infinity_high);
3001 __ Eor(out, out, infinity_high2);
3002 // We don't care about the sign bit, so shift left.
3003 __ Orr(out, temp, Operand(out, vixl32::LSL, 1));
3004 // If the result is 0, then it has 32 leading zeros, and less than that otherwise.
3005 __ Clz(out, out);
3006 // Any number less than 32 logically shifted right by 5 bits results in 0;
3007 // the same operation on 32 yields 1.
3008 __ Lsr(out, out, 5);
3009}
3010
TatWai Chongd8c052a2016-11-02 16:12:48 +08003011void IntrinsicLocationsBuilderARMVIXL::VisitReferenceGetReferent(HInvoke* invoke) {
3012 if (kEmitCompilerReadBarrier) {
3013 // Do not intrinsify this call with the read barrier configuration.
3014 return;
3015 }
3016 LocationSummary* locations = new (arena_) LocationSummary(invoke,
3017 LocationSummary::kCallOnSlowPath,
3018 kIntrinsified);
3019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetOut(Location::SameAsFirstInput());
3021 locations->AddTemp(Location::RequiresRegister());
3022}
3023
3024void IntrinsicCodeGeneratorARMVIXL::VisitReferenceGetReferent(HInvoke* invoke) {
3025 DCHECK(!kEmitCompilerReadBarrier);
3026 ArmVIXLAssembler* assembler = GetAssembler();
3027 LocationSummary* locations = invoke->GetLocations();
3028
3029 vixl32::Register obj = InputRegisterAt(invoke, 0);
3030 vixl32::Register out = OutputRegister(invoke);
3031
3032 SlowPathCodeARMVIXL* slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
3033 codegen_->AddSlowPath(slow_path);
3034
3035 // Load ArtMethod first.
3036 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
3037 DCHECK(invoke_direct != nullptr);
3038 vixl32::Register temp0 = RegisterFrom(codegen_->GenerateCalleeMethodStaticOrDirectCall(
3039 invoke_direct, locations->GetTemp(0)));
3040
3041 // Now get declaring class.
3042 __ Ldr(temp0, MemOperand(temp0, ArtMethod::DeclaringClassOffset().Int32Value()));
3043
3044 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
3045 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
3046 DCHECK_NE(slow_path_flag_offset, 0u);
3047 DCHECK_NE(disable_flag_offset, 0u);
3048 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
3049
3050 // Check static flags that prevent using intrinsic.
3051 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3052 vixl32::Register temp1 = temps.Acquire();
3053 __ Ldr(temp1, MemOperand(temp0, disable_flag_offset));
3054 __ Ldr(temp0, MemOperand(temp0, slow_path_flag_offset));
3055 __ Orr(temp0, temp1, temp0);
3056 __ CompareAndBranchIfNonZero(temp0, slow_path->GetEntryLabel());
3057
3058 // Fast path.
3059 __ Ldr(out, MemOperand(obj, mirror::Reference::ReferentOffset().Int32Value()));
3060 codegen_->MaybeRecordImplicitNullCheck(invoke);
3061 assembler->MaybeUnpoisonHeapReference(out);
3062 __ Bind(slow_path->GetExitLabel());
3063}
3064
Artem Serov9aee2d42017-01-06 15:58:31 +00003065void IntrinsicLocationsBuilderARMVIXL::VisitMathCeil(HInvoke* invoke) {
3066 if (features_.HasARMv8AInstructions()) {
3067 CreateFPToFPLocations(arena_, invoke);
3068 }
3069}
3070
3071void IntrinsicCodeGeneratorARMVIXL::VisitMathCeil(HInvoke* invoke) {
3072 ArmVIXLAssembler* assembler = GetAssembler();
3073 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
3074 __ Vrintp(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
3075}
3076
3077void IntrinsicLocationsBuilderARMVIXL::VisitMathFloor(HInvoke* invoke) {
3078 if (features_.HasARMv8AInstructions()) {
3079 CreateFPToFPLocations(arena_, invoke);
3080 }
3081}
3082
3083void IntrinsicCodeGeneratorARMVIXL::VisitMathFloor(HInvoke* invoke) {
3084 ArmVIXLAssembler* assembler = GetAssembler();
3085 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
3086 __ Vrintm(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
3087}
3088
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003089void IntrinsicLocationsBuilderARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
3090 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3091 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3092 invoke,
3093 codegen_,
3094 LocationFrom(r0),
3095 LocationFrom(calling_convention.GetRegisterAt(0)));
3096}
3097
3098void IntrinsicCodeGeneratorARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
3099 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3100 LocationSummary* locations = invoke->GetLocations();
3101 ArmVIXLAssembler* const assembler = GetAssembler();
3102
3103 vixl32::Register out = RegisterFrom(locations->Out());
3104 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3105 vixl32::Register temp = temps.Acquire();
3106 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3107 vixl32::Register argument = calling_convention.GetRegisterAt(0);
3108 if (invoke->InputAt(0)->IsConstant()) {
3109 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3110 if (value >= info.low && value <= info.high) {
3111 // Just embed the j.l.Integer in the code.
3112 ScopedObjectAccess soa(Thread::Current());
3113 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3114 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3115 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3116 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
3117 } else {
3118 // Allocate and initialize a new j.l.Integer.
3119 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3120 // JIT object table.
3121 uint32_t address =
3122 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3123 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
3124 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3125 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3126 __ Mov(temp, value);
3127 assembler->StoreToOffset(kStoreWord, temp, out, info.value_offset);
3128 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3129 // one.
3130 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3131 }
3132 } else {
3133 vixl32::Register in = RegisterFrom(locations->InAt(0));
3134 // Check bounds of our cache.
3135 __ Add(out, in, -info.low);
3136 __ Cmp(out, info.high - info.low + 1);
3137 vixl32::Label allocate, done;
3138 __ B(hs, &allocate);
3139 // If the value is within the bounds, load the j.l.Integer directly from the array.
3140 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3141 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3142 __ Ldr(temp, codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
3143 codegen_->LoadFromShiftedRegOffset(Primitive::kPrimNot, locations->Out(), temp, out);
3144 assembler->MaybeUnpoisonHeapReference(out);
3145 __ B(&done);
3146 __ Bind(&allocate);
3147 // Otherwise allocate and initialize a new j.l.Integer.
3148 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3149 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
3150 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3151 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3152 assembler->StoreToOffset(kStoreWord, in, out, info.value_offset);
3153 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3154 // one.
3155 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3156 __ Bind(&done);
3157 }
3158}
3159
Anton Kirilov5ec62182016-10-13 20:16:02 +01003160UNIMPLEMENTED_INTRINSIC(ARMVIXL, MathRoundDouble) // Could be done by changing rounding mode, maybe?
Anton Kirilov5ec62182016-10-13 20:16:02 +01003161UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeCASLong) // High register pressure.
3162UNIMPLEMENTED_INTRINSIC(ARMVIXL, SystemArrayCopyChar)
Anton Kirilov5ec62182016-10-13 20:16:02 +01003163UNIMPLEMENTED_INTRINSIC(ARMVIXL, IntegerHighestOneBit)
3164UNIMPLEMENTED_INTRINSIC(ARMVIXL, LongHighestOneBit)
3165UNIMPLEMENTED_INTRINSIC(ARMVIXL, IntegerLowestOneBit)
3166UNIMPLEMENTED_INTRINSIC(ARMVIXL, LongLowestOneBit)
3167
Aart Bikff7d89c2016-11-07 08:49:28 -08003168UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOf);
3169UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003170UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferAppend);
3171UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferLength);
3172UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferToString);
3173UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderAppend);
3174UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderLength);
3175UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003176
Anton Kirilov5ec62182016-10-13 20:16:02 +01003177// 1.8.
3178UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddInt)
3179UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddLong)
3180UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetInt)
3181UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetLong)
3182UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetObject)
3183
3184UNREACHABLE_INTRINSICS(ARMVIXL)
3185
3186#undef __
3187
3188} // namespace arm
3189} // namespace art