blob: 93a2340a326a6d63604ae6107d5265c45a90655a [file] [log] [blame]
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_arm.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080021#include "code_generator_arm.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070024#include "intrinsics_utils.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "mirror/array-inl.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080026#include "mirror/string.h"
27#include "thread.h"
28#include "utils/arm/assembler_arm.h"
29
30namespace art {
31
32namespace arm {
33
34ArmAssembler* IntrinsicCodeGeneratorARM::GetAssembler() {
35 return codegen_->GetAssembler();
36}
37
38ArenaAllocator* IntrinsicCodeGeneratorARM::GetAllocator() {
39 return codegen_->GetGraph()->GetArena();
40}
41
Andreas Gampe85b62f22015-09-09 13:15:38 -070042using IntrinsicSlowPathARM = IntrinsicSlowPath<InvokeDexCallingConventionVisitorARM>;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080043
Roland Levillain0b671c02016-08-19 12:02:34 +010044// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
45#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
46
47// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
48class ReadBarrierSystemArrayCopySlowPathARM : public SlowPathCode {
49 public:
50 explicit ReadBarrierSystemArrayCopySlowPathARM(HInstruction* instruction)
51 : SlowPathCode(instruction) {
52 DCHECK(kEmitCompilerReadBarrier);
53 DCHECK(kUseBakerReadBarrier);
54 }
55
56 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
57 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
58 LocationSummary* locations = instruction_->GetLocations();
59 DCHECK(locations->CanCall());
60 DCHECK(instruction_->IsInvokeStaticOrDirect())
61 << "Unexpected instruction in read barrier arraycopy slow path: "
62 << instruction_->DebugName();
63 DCHECK(instruction_->GetLocations()->Intrinsified());
64 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
65
66 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
67 uint32_t element_size_shift = Primitive::ComponentSizeShift(Primitive::kPrimNot);
68 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
69
70 Register dest = locations->InAt(2).AsRegister<Register>();
71 Location dest_pos = locations->InAt(3);
72 Register src_curr_addr = locations->GetTemp(0).AsRegister<Register>();
73 Register dst_curr_addr = locations->GetTemp(1).AsRegister<Register>();
74 Register src_stop_addr = locations->GetTemp(2).AsRegister<Register>();
75 Register tmp = locations->GetTemp(3).AsRegister<Register>();
76
77 __ Bind(GetEntryLabel());
78 // Compute the base destination address in `dst_curr_addr`.
79 if (dest_pos.IsConstant()) {
80 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
81 __ AddConstant(dst_curr_addr, dest, element_size * constant + offset);
82 } else {
83 __ add(dst_curr_addr,
84 dest,
85 ShifterOperand(dest_pos.AsRegister<Register>(), LSL, element_size_shift));
86 __ AddConstant(dst_curr_addr, offset);
87 }
88
89 Label loop;
90 __ Bind(&loop);
91 __ ldr(tmp, Address(src_curr_addr, element_size, Address::PostIndex));
92 __ MaybeUnpoisonHeapReference(tmp);
93 // TODO: Inline the mark bit check before calling the runtime?
94 // tmp = ReadBarrier::Mark(tmp);
95 // No need to save live registers; it's taken care of by the
96 // entrypoint. Also, there is no need to update the stack mask,
97 // as this runtime call will not trigger a garbage collection.
98 // (See ReadBarrierMarkSlowPathARM::EmitNativeCode for more
99 // explanations.)
100 DCHECK_NE(tmp, SP);
101 DCHECK_NE(tmp, LR);
102 DCHECK_NE(tmp, PC);
103 // IP is used internally by the ReadBarrierMarkRegX entry point
104 // as a temporary (and not preserved). It thus cannot be used by
105 // any live register in this slow path.
106 DCHECK_NE(src_curr_addr, IP);
107 DCHECK_NE(dst_curr_addr, IP);
108 DCHECK_NE(src_stop_addr, IP);
109 DCHECK_NE(tmp, IP);
110 DCHECK(0 <= tmp && tmp < kNumberOfCoreRegisters) << tmp;
111 int32_t entry_point_offset =
112 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(tmp);
113 // This runtime call does not require a stack map.
114 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
115 __ MaybePoisonHeapReference(tmp);
116 __ str(tmp, Address(dst_curr_addr, element_size, Address::PostIndex));
117 __ cmp(src_curr_addr, ShifterOperand(src_stop_addr));
118 __ b(&loop, NE);
119 __ b(GetExitLabel());
120 }
121
122 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathARM"; }
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARM);
126};
127
128#undef __
129
Vladimir Marko68c981f2016-08-26 13:13:33 +0100130IntrinsicLocationsBuilderARM::IntrinsicLocationsBuilderARM(CodeGeneratorARM* codegen)
131 : arena_(codegen->GetGraph()->GetArena()),
132 assembler_(codegen->GetAssembler()),
133 features_(codegen->GetInstructionSetFeatures()) {}
134
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800135bool IntrinsicLocationsBuilderARM::TryDispatch(HInvoke* invoke) {
136 Dispatch(invoke);
137 LocationSummary* res = invoke->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +0000138 if (res == nullptr) {
139 return false;
140 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000141 return res->Intrinsified();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800142}
143
144#define __ assembler->
145
146static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
147 LocationSummary* locations = new (arena) LocationSummary(invoke,
148 LocationSummary::kNoCall,
149 kIntrinsified);
150 locations->SetInAt(0, Location::RequiresFpuRegister());
151 locations->SetOut(Location::RequiresRegister());
152}
153
154static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
155 LocationSummary* locations = new (arena) LocationSummary(invoke,
156 LocationSummary::kNoCall,
157 kIntrinsified);
158 locations->SetInAt(0, Location::RequiresRegister());
159 locations->SetOut(Location::RequiresFpuRegister());
160}
161
162static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
163 Location input = locations->InAt(0);
164 Location output = locations->Out();
165 if (is64bit) {
166 __ vmovrrd(output.AsRegisterPairLow<Register>(),
167 output.AsRegisterPairHigh<Register>(),
168 FromLowSToD(input.AsFpuRegisterPairLow<SRegister>()));
169 } else {
170 __ vmovrs(output.AsRegister<Register>(), input.AsFpuRegister<SRegister>());
171 }
172}
173
174static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
175 Location input = locations->InAt(0);
176 Location output = locations->Out();
177 if (is64bit) {
178 __ vmovdrr(FromLowSToD(output.AsFpuRegisterPairLow<SRegister>()),
179 input.AsRegisterPairLow<Register>(),
180 input.AsRegisterPairHigh<Register>());
181 } else {
182 __ vmovsr(output.AsFpuRegister<SRegister>(), input.AsRegister<Register>());
183 }
184}
185
186void IntrinsicLocationsBuilderARM::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
187 CreateFPToIntLocations(arena_, invoke);
188}
189void IntrinsicLocationsBuilderARM::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
190 CreateIntToFPLocations(arena_, invoke);
191}
192
193void IntrinsicCodeGeneratorARM::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000194 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800195}
196void IntrinsicCodeGeneratorARM::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000197 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800198}
199
200void IntrinsicLocationsBuilderARM::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
201 CreateFPToIntLocations(arena_, invoke);
202}
203void IntrinsicLocationsBuilderARM::VisitFloatIntBitsToFloat(HInvoke* invoke) {
204 CreateIntToFPLocations(arena_, invoke);
205}
206
207void IntrinsicCodeGeneratorARM::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000208 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800209}
210void IntrinsicCodeGeneratorARM::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000211 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800212}
213
214static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
215 LocationSummary* locations = new (arena) LocationSummary(invoke,
216 LocationSummary::kNoCall,
217 kIntrinsified);
218 locations->SetInAt(0, Location::RequiresRegister());
219 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
220}
221
222static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
223 LocationSummary* locations = new (arena) LocationSummary(invoke,
224 LocationSummary::kNoCall,
225 kIntrinsified);
226 locations->SetInAt(0, Location::RequiresFpuRegister());
227 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
228}
229
Scott Wakeling611d3392015-07-10 11:42:06 +0100230static void GenNumberOfLeadingZeros(LocationSummary* locations,
231 Primitive::Type type,
232 ArmAssembler* assembler) {
233 Location in = locations->InAt(0);
234 Register out = locations->Out().AsRegister<Register>();
235
236 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
237
238 if (type == Primitive::kPrimLong) {
239 Register in_reg_lo = in.AsRegisterPairLow<Register>();
240 Register in_reg_hi = in.AsRegisterPairHigh<Register>();
241 Label end;
242 __ clz(out, in_reg_hi);
243 __ CompareAndBranchIfNonZero(in_reg_hi, &end);
244 __ clz(out, in_reg_lo);
245 __ AddConstant(out, 32);
246 __ Bind(&end);
247 } else {
248 __ clz(out, in.AsRegister<Register>());
249 }
250}
251
252void IntrinsicLocationsBuilderARM::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
253 CreateIntToIntLocations(arena_, invoke);
254}
255
256void IntrinsicCodeGeneratorARM::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
257 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
258}
259
260void IntrinsicLocationsBuilderARM::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
261 LocationSummary* locations = new (arena_) LocationSummary(invoke,
262 LocationSummary::kNoCall,
263 kIntrinsified);
264 locations->SetInAt(0, Location::RequiresRegister());
265 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
266}
267
268void IntrinsicCodeGeneratorARM::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
269 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
270}
271
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100272static void GenNumberOfTrailingZeros(LocationSummary* locations,
273 Primitive::Type type,
274 ArmAssembler* assembler) {
275 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
276
277 Register out = locations->Out().AsRegister<Register>();
278
279 if (type == Primitive::kPrimLong) {
280 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
281 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
282 Label end;
283 __ rbit(out, in_reg_lo);
284 __ clz(out, out);
285 __ CompareAndBranchIfNonZero(in_reg_lo, &end);
286 __ rbit(out, in_reg_hi);
287 __ clz(out, out);
288 __ AddConstant(out, 32);
289 __ Bind(&end);
290 } else {
291 Register in = locations->InAt(0).AsRegister<Register>();
292 __ rbit(out, in);
293 __ clz(out, out);
294 }
295}
296
297void IntrinsicLocationsBuilderARM::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
298 LocationSummary* locations = new (arena_) LocationSummary(invoke,
299 LocationSummary::kNoCall,
300 kIntrinsified);
301 locations->SetInAt(0, Location::RequiresRegister());
302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
303}
304
305void IntrinsicCodeGeneratorARM::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
306 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
307}
308
309void IntrinsicLocationsBuilderARM::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
310 LocationSummary* locations = new (arena_) LocationSummary(invoke,
311 LocationSummary::kNoCall,
312 kIntrinsified);
313 locations->SetInAt(0, Location::RequiresRegister());
314 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
315}
316
317void IntrinsicCodeGeneratorARM::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
318 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
319}
320
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800321static void MathAbsFP(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
322 Location in = locations->InAt(0);
323 Location out = locations->Out();
324
325 if (is64bit) {
326 __ vabsd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
327 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
328 } else {
329 __ vabss(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
330 }
331}
332
333void IntrinsicLocationsBuilderARM::VisitMathAbsDouble(HInvoke* invoke) {
334 CreateFPToFPLocations(arena_, invoke);
335}
336
337void IntrinsicCodeGeneratorARM::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000338 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800339}
340
341void IntrinsicLocationsBuilderARM::VisitMathAbsFloat(HInvoke* invoke) {
342 CreateFPToFPLocations(arena_, invoke);
343}
344
345void IntrinsicCodeGeneratorARM::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000346 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800347}
348
349static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
350 LocationSummary* locations = new (arena) LocationSummary(invoke,
351 LocationSummary::kNoCall,
352 kIntrinsified);
353 locations->SetInAt(0, Location::RequiresRegister());
354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
355
356 locations->AddTemp(Location::RequiresRegister());
357}
358
359static void GenAbsInteger(LocationSummary* locations,
360 bool is64bit,
361 ArmAssembler* assembler) {
362 Location in = locations->InAt(0);
363 Location output = locations->Out();
364
365 Register mask = locations->GetTemp(0).AsRegister<Register>();
366
367 if (is64bit) {
368 Register in_reg_lo = in.AsRegisterPairLow<Register>();
369 Register in_reg_hi = in.AsRegisterPairHigh<Register>();
370 Register out_reg_lo = output.AsRegisterPairLow<Register>();
371 Register out_reg_hi = output.AsRegisterPairHigh<Register>();
372
373 DCHECK_NE(out_reg_lo, in_reg_hi) << "Diagonal overlap unexpected.";
374
375 __ Asr(mask, in_reg_hi, 31);
376 __ adds(out_reg_lo, in_reg_lo, ShifterOperand(mask));
377 __ adc(out_reg_hi, in_reg_hi, ShifterOperand(mask));
378 __ eor(out_reg_lo, mask, ShifterOperand(out_reg_lo));
379 __ eor(out_reg_hi, mask, ShifterOperand(out_reg_hi));
380 } else {
381 Register in_reg = in.AsRegister<Register>();
382 Register out_reg = output.AsRegister<Register>();
383
384 __ Asr(mask, in_reg, 31);
385 __ add(out_reg, in_reg, ShifterOperand(mask));
386 __ eor(out_reg, mask, ShifterOperand(out_reg));
387 }
388}
389
390void IntrinsicLocationsBuilderARM::VisitMathAbsInt(HInvoke* invoke) {
391 CreateIntToIntPlusTemp(arena_, invoke);
392}
393
394void IntrinsicCodeGeneratorARM::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000395 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800396}
397
398
399void IntrinsicLocationsBuilderARM::VisitMathAbsLong(HInvoke* invoke) {
400 CreateIntToIntPlusTemp(arena_, invoke);
401}
402
403void IntrinsicCodeGeneratorARM::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000404 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800405}
406
407static void GenMinMax(LocationSummary* locations,
408 bool is_min,
409 ArmAssembler* assembler) {
410 Register op1 = locations->InAt(0).AsRegister<Register>();
411 Register op2 = locations->InAt(1).AsRegister<Register>();
412 Register out = locations->Out().AsRegister<Register>();
413
414 __ cmp(op1, ShifterOperand(op2));
415
416 __ it((is_min) ? Condition::LT : Condition::GT, kItElse);
417 __ mov(out, ShifterOperand(op1), is_min ? Condition::LT : Condition::GT);
418 __ mov(out, ShifterOperand(op2), is_min ? Condition::GE : Condition::LE);
419}
420
421static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
422 LocationSummary* locations = new (arena) LocationSummary(invoke,
423 LocationSummary::kNoCall,
424 kIntrinsified);
425 locations->SetInAt(0, Location::RequiresRegister());
426 locations->SetInAt(1, Location::RequiresRegister());
427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
428}
429
430void IntrinsicLocationsBuilderARM::VisitMathMinIntInt(HInvoke* invoke) {
431 CreateIntIntToIntLocations(arena_, invoke);
432}
433
434void IntrinsicCodeGeneratorARM::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000435 GenMinMax(invoke->GetLocations(), /* is_min */ true, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800436}
437
438void IntrinsicLocationsBuilderARM::VisitMathMaxIntInt(HInvoke* invoke) {
439 CreateIntIntToIntLocations(arena_, invoke);
440}
441
442void IntrinsicCodeGeneratorARM::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000443 GenMinMax(invoke->GetLocations(), /* is_min */ false, GetAssembler());
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800444}
445
446void IntrinsicLocationsBuilderARM::VisitMathSqrt(HInvoke* invoke) {
447 CreateFPToFPLocations(arena_, invoke);
448}
449
450void IntrinsicCodeGeneratorARM::VisitMathSqrt(HInvoke* invoke) {
451 LocationSummary* locations = invoke->GetLocations();
452 ArmAssembler* assembler = GetAssembler();
453 __ vsqrtd(FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>()),
454 FromLowSToD(locations->InAt(0).AsFpuRegisterPairLow<SRegister>()));
455}
456
457void IntrinsicLocationsBuilderARM::VisitMemoryPeekByte(HInvoke* invoke) {
458 CreateIntToIntLocations(arena_, invoke);
459}
460
461void IntrinsicCodeGeneratorARM::VisitMemoryPeekByte(HInvoke* invoke) {
462 ArmAssembler* assembler = GetAssembler();
463 // Ignore upper 4B of long address.
464 __ ldrsb(invoke->GetLocations()->Out().AsRegister<Register>(),
465 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
466}
467
468void IntrinsicLocationsBuilderARM::VisitMemoryPeekIntNative(HInvoke* invoke) {
469 CreateIntToIntLocations(arena_, invoke);
470}
471
472void IntrinsicCodeGeneratorARM::VisitMemoryPeekIntNative(HInvoke* invoke) {
473 ArmAssembler* assembler = GetAssembler();
474 // Ignore upper 4B of long address.
475 __ ldr(invoke->GetLocations()->Out().AsRegister<Register>(),
476 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
477}
478
479void IntrinsicLocationsBuilderARM::VisitMemoryPeekLongNative(HInvoke* invoke) {
480 CreateIntToIntLocations(arena_, invoke);
481}
482
483void IntrinsicCodeGeneratorARM::VisitMemoryPeekLongNative(HInvoke* invoke) {
484 ArmAssembler* assembler = GetAssembler();
485 // Ignore upper 4B of long address.
486 Register addr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
487 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
488 // exception. So we can't use ldrd as addr may be unaligned.
489 Register lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>();
490 Register hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>();
491 if (addr == lo) {
492 __ ldr(hi, Address(addr, 4));
493 __ ldr(lo, Address(addr, 0));
494 } else {
495 __ ldr(lo, Address(addr, 0));
496 __ ldr(hi, Address(addr, 4));
497 }
498}
499
500void IntrinsicLocationsBuilderARM::VisitMemoryPeekShortNative(HInvoke* invoke) {
501 CreateIntToIntLocations(arena_, invoke);
502}
503
504void IntrinsicCodeGeneratorARM::VisitMemoryPeekShortNative(HInvoke* invoke) {
505 ArmAssembler* assembler = GetAssembler();
506 // Ignore upper 4B of long address.
507 __ ldrsh(invoke->GetLocations()->Out().AsRegister<Register>(),
508 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
509}
510
511static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
512 LocationSummary* locations = new (arena) LocationSummary(invoke,
513 LocationSummary::kNoCall,
514 kIntrinsified);
515 locations->SetInAt(0, Location::RequiresRegister());
516 locations->SetInAt(1, Location::RequiresRegister());
517}
518
519void IntrinsicLocationsBuilderARM::VisitMemoryPokeByte(HInvoke* invoke) {
520 CreateIntIntToVoidLocations(arena_, invoke);
521}
522
523void IntrinsicCodeGeneratorARM::VisitMemoryPokeByte(HInvoke* invoke) {
524 ArmAssembler* assembler = GetAssembler();
525 __ strb(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
526 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
527}
528
529void IntrinsicLocationsBuilderARM::VisitMemoryPokeIntNative(HInvoke* invoke) {
530 CreateIntIntToVoidLocations(arena_, invoke);
531}
532
533void IntrinsicCodeGeneratorARM::VisitMemoryPokeIntNative(HInvoke* invoke) {
534 ArmAssembler* assembler = GetAssembler();
535 __ str(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
536 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
537}
538
539void IntrinsicLocationsBuilderARM::VisitMemoryPokeLongNative(HInvoke* invoke) {
540 CreateIntIntToVoidLocations(arena_, invoke);
541}
542
543void IntrinsicCodeGeneratorARM::VisitMemoryPokeLongNative(HInvoke* invoke) {
544 ArmAssembler* assembler = GetAssembler();
545 // Ignore upper 4B of long address.
546 Register addr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
547 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
548 // exception. So we can't use ldrd as addr may be unaligned.
549 __ str(invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>(), Address(addr, 0));
550 __ str(invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>(), Address(addr, 4));
551}
552
553void IntrinsicLocationsBuilderARM::VisitMemoryPokeShortNative(HInvoke* invoke) {
554 CreateIntIntToVoidLocations(arena_, invoke);
555}
556
557void IntrinsicCodeGeneratorARM::VisitMemoryPokeShortNative(HInvoke* invoke) {
558 ArmAssembler* assembler = GetAssembler();
559 __ strh(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
560 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
561}
562
563void IntrinsicLocationsBuilderARM::VisitThreadCurrentThread(HInvoke* invoke) {
564 LocationSummary* locations = new (arena_) LocationSummary(invoke,
565 LocationSummary::kNoCall,
566 kIntrinsified);
567 locations->SetOut(Location::RequiresRegister());
568}
569
570void IntrinsicCodeGeneratorARM::VisitThreadCurrentThread(HInvoke* invoke) {
571 ArmAssembler* assembler = GetAssembler();
572 __ LoadFromOffset(kLoadWord,
573 invoke->GetLocations()->Out().AsRegister<Register>(),
574 TR,
575 Thread::PeerOffset<kArmPointerSize>().Int32Value());
576}
577
578static void GenUnsafeGet(HInvoke* invoke,
579 Primitive::Type type,
580 bool is_volatile,
581 CodeGeneratorARM* codegen) {
582 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800583 ArmAssembler* assembler = codegen->GetAssembler();
Roland Levillain3b359c72015-11-17 19:35:12 +0000584 Location base_loc = locations->InAt(1);
585 Register base = base_loc.AsRegister<Register>(); // Object pointer.
586 Location offset_loc = locations->InAt(2);
587 Register offset = offset_loc.AsRegisterPairLow<Register>(); // Long offset, lo part only.
588 Location trg_loc = locations->Out();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800589
Roland Levillainc9285912015-12-18 10:38:42 +0000590 switch (type) {
591 case Primitive::kPrimInt: {
592 Register trg = trg_loc.AsRegister<Register>();
593 __ ldr(trg, Address(base, offset));
594 if (is_volatile) {
595 __ dmb(ISH);
596 }
597 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800598 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800599
Roland Levillainc9285912015-12-18 10:38:42 +0000600 case Primitive::kPrimNot: {
601 Register trg = trg_loc.AsRegister<Register>();
602 if (kEmitCompilerReadBarrier) {
603 if (kUseBakerReadBarrier) {
604 Location temp = locations->GetTemp(0);
Roland Levillainbfea3352016-06-23 13:48:47 +0100605 codegen->GenerateReferenceLoadWithBakerReadBarrier(
606 invoke, trg_loc, base, 0U, offset_loc, TIMES_1, temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +0000607 if (is_volatile) {
608 __ dmb(ISH);
609 }
610 } else {
611 __ ldr(trg, Address(base, offset));
612 if (is_volatile) {
613 __ dmb(ISH);
614 }
615 codegen->GenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
616 }
617 } else {
618 __ ldr(trg, Address(base, offset));
619 if (is_volatile) {
620 __ dmb(ISH);
621 }
622 __ MaybeUnpoisonHeapReference(trg);
623 }
624 break;
625 }
Roland Levillain4d027112015-07-01 15:41:14 +0100626
Roland Levillainc9285912015-12-18 10:38:42 +0000627 case Primitive::kPrimLong: {
628 Register trg_lo = trg_loc.AsRegisterPairLow<Register>();
629 __ add(IP, base, ShifterOperand(offset));
630 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
631 Register trg_hi = trg_loc.AsRegisterPairHigh<Register>();
632 __ ldrexd(trg_lo, trg_hi, IP);
633 } else {
634 __ ldrd(trg_lo, Address(IP));
635 }
636 if (is_volatile) {
637 __ dmb(ISH);
638 }
639 break;
640 }
641
642 default:
643 LOG(FATAL) << "Unexpected type " << type;
644 UNREACHABLE();
Roland Levillain4d027112015-07-01 15:41:14 +0100645 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800646}
647
Roland Levillainc9285912015-12-18 10:38:42 +0000648static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
649 HInvoke* invoke,
650 Primitive::Type type) {
Roland Levillain3b359c72015-11-17 19:35:12 +0000651 bool can_call = kEmitCompilerReadBarrier &&
652 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
653 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800654 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100655 (can_call
656 ? LocationSummary::kCallOnSlowPath
657 : LocationSummary::kNoCall),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800658 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +0100659 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100660 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +0100661 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800662 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
663 locations->SetInAt(1, Location::RequiresRegister());
664 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100665 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Roland Levillainc9285912015-12-18 10:38:42 +0000667 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
668 // We need a temporary register for the read barrier marking slow
Roland Levillainbfea3352016-06-23 13:48:47 +0100669 // path in InstructionCodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier.
Roland Levillainc9285912015-12-18 10:38:42 +0000670 locations->AddTemp(Location::RequiresRegister());
671 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800672}
673
674void IntrinsicLocationsBuilderARM::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000675 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800676}
677void IntrinsicLocationsBuilderARM::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000678 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800679}
680void IntrinsicLocationsBuilderARM::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000681 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800682}
683void IntrinsicLocationsBuilderARM::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000684 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800685}
686void IntrinsicLocationsBuilderARM::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000687 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800688}
689void IntrinsicLocationsBuilderARM::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainc9285912015-12-18 10:38:42 +0000690 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800691}
692
693void IntrinsicCodeGeneratorARM::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000694 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800695}
696void IntrinsicCodeGeneratorARM::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000697 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800698}
699void IntrinsicCodeGeneratorARM::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000700 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800701}
702void IntrinsicCodeGeneratorARM::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000703 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800704}
705void IntrinsicCodeGeneratorARM::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000706 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800707}
708void IntrinsicCodeGeneratorARM::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000709 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800710}
711
712static void CreateIntIntIntIntToVoid(ArenaAllocator* arena,
713 const ArmInstructionSetFeatures& features,
714 Primitive::Type type,
715 bool is_volatile,
716 HInvoke* invoke) {
717 LocationSummary* locations = new (arena) LocationSummary(invoke,
718 LocationSummary::kNoCall,
719 kIntrinsified);
720 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
721 locations->SetInAt(1, Location::RequiresRegister());
722 locations->SetInAt(2, Location::RequiresRegister());
723 locations->SetInAt(3, Location::RequiresRegister());
724
725 if (type == Primitive::kPrimLong) {
726 // Potentially need temps for ldrexd-strexd loop.
727 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
728 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
729 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
730 }
731 } else if (type == Primitive::kPrimNot) {
732 // Temps for card-marking.
733 locations->AddTemp(Location::RequiresRegister()); // Temp.
734 locations->AddTemp(Location::RequiresRegister()); // Card.
735 }
736}
737
738void IntrinsicLocationsBuilderARM::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000739 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800740}
741void IntrinsicLocationsBuilderARM::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000742 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800743}
744void IntrinsicLocationsBuilderARM::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000745 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ true, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800746}
747void IntrinsicLocationsBuilderARM::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000748 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800749}
750void IntrinsicLocationsBuilderARM::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000751 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800752}
753void IntrinsicLocationsBuilderARM::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000754 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ true, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800755}
756void IntrinsicLocationsBuilderARM::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000757 CreateIntIntIntIntToVoid(
758 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800759}
760void IntrinsicLocationsBuilderARM::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000761 CreateIntIntIntIntToVoid(
762 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800763}
764void IntrinsicLocationsBuilderARM::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000765 CreateIntIntIntIntToVoid(
766 arena_, features_, Primitive::kPrimLong, /* is_volatile */ true, invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800767}
768
769static void GenUnsafePut(LocationSummary* locations,
770 Primitive::Type type,
771 bool is_volatile,
772 bool is_ordered,
773 CodeGeneratorARM* codegen) {
774 ArmAssembler* assembler = codegen->GetAssembler();
775
776 Register base = locations->InAt(1).AsRegister<Register>(); // Object pointer.
777 Register offset = locations->InAt(2).AsRegisterPairLow<Register>(); // Long offset, lo part only.
778 Register value;
779
780 if (is_volatile || is_ordered) {
781 __ dmb(ISH);
782 }
783
784 if (type == Primitive::kPrimLong) {
785 Register value_lo = locations->InAt(3).AsRegisterPairLow<Register>();
786 value = value_lo;
787 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
788 Register temp_lo = locations->GetTemp(0).AsRegister<Register>();
789 Register temp_hi = locations->GetTemp(1).AsRegister<Register>();
790 Register value_hi = locations->InAt(3).AsRegisterPairHigh<Register>();
791
792 __ add(IP, base, ShifterOperand(offset));
793 Label loop_head;
794 __ Bind(&loop_head);
795 __ ldrexd(temp_lo, temp_hi, IP);
796 __ strexd(temp_lo, value_lo, value_hi, IP);
797 __ cmp(temp_lo, ShifterOperand(0));
798 __ b(&loop_head, NE);
799 } else {
800 __ add(IP, base, ShifterOperand(offset));
801 __ strd(value_lo, Address(IP));
802 }
803 } else {
Roland Levillain4d027112015-07-01 15:41:14 +0100804 value = locations->InAt(3).AsRegister<Register>();
805 Register source = value;
806 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
807 Register temp = locations->GetTemp(0).AsRegister<Register>();
808 __ Mov(temp, value);
809 __ PoisonHeapReference(temp);
810 source = temp;
811 }
812 __ str(source, Address(base, offset));
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800813 }
814
815 if (is_volatile) {
816 __ dmb(ISH);
817 }
818
819 if (type == Primitive::kPrimNot) {
820 Register temp = locations->GetTemp(0).AsRegister<Register>();
821 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100822 bool value_can_be_null = true; // TODO: Worth finding out this information?
823 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800824 }
825}
826
827void IntrinsicCodeGeneratorARM::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000828 GenUnsafePut(invoke->GetLocations(),
829 Primitive::kPrimInt,
830 /* is_volatile */ false,
831 /* is_ordered */ false,
832 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800833}
834void IntrinsicCodeGeneratorARM::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000835 GenUnsafePut(invoke->GetLocations(),
836 Primitive::kPrimInt,
837 /* is_volatile */ false,
838 /* is_ordered */ true,
839 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800840}
841void IntrinsicCodeGeneratorARM::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000842 GenUnsafePut(invoke->GetLocations(),
843 Primitive::kPrimInt,
844 /* is_volatile */ true,
845 /* is_ordered */ false,
846 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800847}
848void IntrinsicCodeGeneratorARM::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000849 GenUnsafePut(invoke->GetLocations(),
850 Primitive::kPrimNot,
851 /* is_volatile */ false,
852 /* is_ordered */ false,
853 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800854}
855void IntrinsicCodeGeneratorARM::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000856 GenUnsafePut(invoke->GetLocations(),
857 Primitive::kPrimNot,
858 /* is_volatile */ false,
859 /* is_ordered */ true,
860 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800861}
862void IntrinsicCodeGeneratorARM::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000863 GenUnsafePut(invoke->GetLocations(),
864 Primitive::kPrimNot,
865 /* is_volatile */ true,
866 /* is_ordered */ false,
867 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800868}
869void IntrinsicCodeGeneratorARM::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000870 GenUnsafePut(invoke->GetLocations(),
871 Primitive::kPrimLong,
872 /* is_volatile */ false,
873 /* is_ordered */ false,
874 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800875}
876void IntrinsicCodeGeneratorARM::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000877 GenUnsafePut(invoke->GetLocations(),
878 Primitive::kPrimLong,
879 /* is_volatile */ false,
880 /* is_ordered */ true,
881 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800882}
883void IntrinsicCodeGeneratorARM::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000884 GenUnsafePut(invoke->GetLocations(),
885 Primitive::kPrimLong,
886 /* is_volatile */ true,
887 /* is_ordered */ false,
888 codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800889}
890
891static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena,
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000892 HInvoke* invoke,
893 Primitive::Type type) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100894 bool can_call = kEmitCompilerReadBarrier &&
895 kUseBakerReadBarrier &&
896 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800897 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100898 (can_call
899 ? LocationSummary::kCallOnSlowPath
900 : LocationSummary::kNoCall),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800901 kIntrinsified);
902 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
903 locations->SetInAt(1, Location::RequiresRegister());
904 locations->SetInAt(2, Location::RequiresRegister());
905 locations->SetInAt(3, Location::RequiresRegister());
906 locations->SetInAt(4, Location::RequiresRegister());
907
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000908 // If heap poisoning is enabled, we don't want the unpoisoning
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100909 // operations to potentially clobber the output. Likewise when
910 // emitting a (Baker) read barrier, which may call.
911 Location::OutputOverlap overlaps =
912 ((kPoisonHeapReferences && type == Primitive::kPrimNot) || can_call)
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000913 ? Location::kOutputOverlap
914 : Location::kNoOutputOverlap;
915 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800916
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100917 // Temporary registers used in CAS. In the object case
918 // (UnsafeCASObject intrinsic), these are also used for
919 // card-marking, and possibly for (Baker) read barrier.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800920 locations->AddTemp(Location::RequiresRegister()); // Pointer.
921 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800922}
923
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100924static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorARM* codegen) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800925 DCHECK_NE(type, Primitive::kPrimLong);
926
927 ArmAssembler* assembler = codegen->GetAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100928 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800929
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100930 Location out_loc = locations->Out();
931 Register out = out_loc.AsRegister<Register>(); // Boolean result.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800932
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100933 Register base = locations->InAt(1).AsRegister<Register>(); // Object pointer.
934 Location offset_loc = locations->InAt(2);
935 Register offset = offset_loc.AsRegisterPairLow<Register>(); // Offset (discard high 4B).
936 Register expected = locations->InAt(3).AsRegister<Register>(); // Expected.
937 Register value = locations->InAt(4).AsRegister<Register>(); // Value.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800938
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100939 Location tmp_ptr_loc = locations->GetTemp(0);
940 Register tmp_ptr = tmp_ptr_loc.AsRegister<Register>(); // Pointer to actual memory.
941 Register tmp = locations->GetTemp(1).AsRegister<Register>(); // Value in memory.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800942
943 if (type == Primitive::kPrimNot) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100944 // The only read barrier implementation supporting the
945 // UnsafeCASObject intrinsic is the Baker-style read barriers.
946 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
947
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800948 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
949 // object and scan the receiver at the next GC for nothing.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100950 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100951 codegen->MarkGCCard(tmp_ptr, tmp, base, value, value_can_be_null);
952
953 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
954 // Need to make sure the reference stored in the field is a to-space
955 // one before attempting the CAS or the CAS could fail incorrectly.
956 codegen->GenerateReferenceLoadWithBakerReadBarrier(
957 invoke,
958 out_loc, // Unused, used only as a "temporary" within the read barrier.
959 base,
960 /* offset */ 0u,
961 /* index */ offset_loc,
962 ScaleFactor::TIMES_1,
963 tmp_ptr_loc,
964 /* needs_null_check */ false,
965 /* always_update_field */ true,
966 &tmp);
967 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800968 }
969
970 // Prevent reordering with prior memory operations.
Roland Levillain4bedb382016-01-12 12:01:04 +0000971 // Emit a DMB ISH instruction instead of an DMB ISHST one, as the
972 // latter allows a preceding load to be delayed past the STXR
973 // instruction below.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800974 __ dmb(ISH);
975
976 __ add(tmp_ptr, base, ShifterOperand(offset));
977
Roland Levillain4d027112015-07-01 15:41:14 +0100978 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100979 __ PoisonHeapReference(expected);
980 if (value == expected) {
981 // Do not poison `value`, as it is the same register as
982 // `expected`, which has just been poisoned.
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000983 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100984 __ PoisonHeapReference(value);
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000985 }
Roland Levillain4d027112015-07-01 15:41:14 +0100986 }
987
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800988 // do {
989 // tmp = [r_ptr] - expected;
990 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
991 // result = tmp != 0;
992
993 Label loop_head;
994 __ Bind(&loop_head);
995
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100996 __ ldrex(tmp, tmp_ptr);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800997
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100998 __ subs(tmp, tmp, ShifterOperand(expected));
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800999
1000 __ it(EQ, ItState::kItT);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001001 __ strex(tmp, value, tmp_ptr, EQ);
1002 __ cmp(tmp, ShifterOperand(1), EQ);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001003
1004 __ b(&loop_head, EQ);
1005
1006 __ dmb(ISH);
1007
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001008 __ rsbs(out, tmp, ShifterOperand(1));
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001009 __ it(CC);
1010 __ mov(out, ShifterOperand(0), CC);
Roland Levillain4d027112015-07-01 15:41:14 +01001011
1012 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001013 __ UnpoisonHeapReference(expected);
1014 if (value == expected) {
1015 // Do not unpoison `value`, as it is the same register as
1016 // `expected`, which has just been unpoisoned.
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001017 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001018 __ UnpoisonHeapReference(value);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001019 }
Roland Levillain4d027112015-07-01 15:41:14 +01001020 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001021}
1022
Andreas Gampeca714582015-04-03 19:41:34 -07001023void IntrinsicLocationsBuilderARM::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001024 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001025}
Andreas Gampeca714582015-04-03 19:41:34 -07001026void IntrinsicLocationsBuilderARM::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001027 // The only read barrier implementation supporting the
1028 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1029 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001030 return;
1031 }
1032
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001033 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001034}
1035void IntrinsicCodeGeneratorARM::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001036 GenCas(invoke, Primitive::kPrimInt, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001037}
1038void IntrinsicCodeGeneratorARM::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001039 // The only read barrier implementation supporting the
1040 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1041 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001042
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001043 GenCas(invoke, Primitive::kPrimNot, codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001044}
1045
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001046void IntrinsicLocationsBuilderARM::VisitStringCompareTo(HInvoke* invoke) {
1047 // The inputs plus one temp.
1048 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001049 invoke->InputAt(1)->CanBeNull()
1050 ? LocationSummary::kCallOnSlowPath
1051 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001052 kIntrinsified);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001053 locations->SetInAt(0, Location::RequiresRegister());
1054 locations->SetInAt(1, Location::RequiresRegister());
1055 locations->AddTemp(Location::RequiresRegister());
1056 locations->AddTemp(Location::RequiresRegister());
1057 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001058 // Need temporary registers for String compression's feature.
1059 if (mirror::kUseStringCompression) {
1060 locations->AddTemp(Location::RequiresRegister());
1061 locations->AddTemp(Location::RequiresRegister());
1062 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001063 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001064}
1065
1066void IntrinsicCodeGeneratorARM::VisitStringCompareTo(HInvoke* invoke) {
1067 ArmAssembler* assembler = GetAssembler();
1068 LocationSummary* locations = invoke->GetLocations();
1069
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001070 Register str = locations->InAt(0).AsRegister<Register>();
1071 Register arg = locations->InAt(1).AsRegister<Register>();
1072 Register out = locations->Out().AsRegister<Register>();
1073
1074 Register temp0 = locations->GetTemp(0).AsRegister<Register>();
1075 Register temp1 = locations->GetTemp(1).AsRegister<Register>();
1076 Register temp2 = locations->GetTemp(2).AsRegister<Register>();
jessicahandojo05765752016-09-09 19:01:32 -07001077 Register temp3, temp4;
1078 if (mirror::kUseStringCompression) {
1079 temp3 = locations->GetTemp(3).AsRegister<Register>();
1080 temp4 = locations->GetTemp(4).AsRegister<Register>();
1081 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001082
1083 Label loop;
1084 Label find_char_diff;
1085 Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001086 Label different_compression;
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001087
1088 // Get offsets of count and value fields within a string object.
1089 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1090 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1091
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001092 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001093 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001094
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001095 // Take slow path and throw if input can be and is null.
1096 SlowPathCode* slow_path = nullptr;
1097 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1098 if (can_slow_path) {
1099 slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
1100 codegen_->AddSlowPath(slow_path);
1101 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
1102 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001103
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001104 // Reference equality check, return 0 if same reference.
1105 __ subs(out, str, ShifterOperand(arg));
1106 __ b(&end, EQ);
jessicahandojo05765752016-09-09 19:01:32 -07001107 if (mirror::kUseStringCompression) {
1108 // Load lengths of this and argument strings.
1109 __ ldr(temp3, Address(str, count_offset));
1110 __ ldr(temp4, Address(arg, count_offset));
1111 // Clean out compression flag from lengths.
1112 __ bic(temp0, temp3, ShifterOperand(0x80000000));
1113 __ bic(IP, temp4, ShifterOperand(0x80000000));
1114 } else {
1115 // Load lengths of this and argument strings.
1116 __ ldr(temp0, Address(str, count_offset));
1117 __ ldr(IP, Address(arg, count_offset));
1118 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001119 // out = length diff.
jessicahandojo05765752016-09-09 19:01:32 -07001120 __ subs(out, temp0, ShifterOperand(IP));
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001121 // temp0 = min(len(str), len(arg)).
jessicahandojo05765752016-09-09 19:01:32 -07001122 __ it(GT);
1123 __ mov(temp0, ShifterOperand(IP), GT);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001124 // Shorter string is empty?
1125 __ CompareAndBranchIfZero(temp0, &end);
1126
jessicahandojo05765752016-09-09 19:01:32 -07001127 if (mirror::kUseStringCompression) {
1128 // Check if both strings using same compression style to use this comparison loop.
1129 __ eors(temp3, temp3, ShifterOperand(temp4));
1130 __ b(&different_compression, MI);
1131 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001132 // Store offset of string value in preparation for comparison loop.
1133 __ mov(temp1, ShifterOperand(value_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001134 if (mirror::kUseStringCompression) {
1135 // For string compression, calculate the number of bytes to compare (not chars).
1136 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1137 __ cmp(temp4, ShifterOperand(0));
1138 __ it(GE);
1139 __ add(temp0, temp0, ShifterOperand(temp0), GE);
1140 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001141
1142 // Assertions that must hold in order to compare multiple characters at a time.
1143 CHECK_ALIGNED(value_offset, 8);
1144 static_assert(IsAligned<8>(kObjectAlignment),
1145 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1146
1147 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1148 DCHECK_EQ(char_size, 2u);
1149
jessicahandojo05765752016-09-09 19:01:32 -07001150 Label find_char_diff_2nd_cmp;
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001151 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1152 __ Bind(&loop);
1153 __ ldr(IP, Address(str, temp1));
1154 __ ldr(temp2, Address(arg, temp1));
1155 __ cmp(IP, ShifterOperand(temp2));
1156 __ b(&find_char_diff, NE);
1157 __ add(temp1, temp1, ShifterOperand(char_size * 2));
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001158
1159 __ ldr(IP, Address(str, temp1));
1160 __ ldr(temp2, Address(arg, temp1));
1161 __ cmp(IP, ShifterOperand(temp2));
jessicahandojo05765752016-09-09 19:01:32 -07001162 __ b(&find_char_diff_2nd_cmp, NE);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001163 __ add(temp1, temp1, ShifterOperand(char_size * 2));
jessicahandojo05765752016-09-09 19:01:32 -07001164 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1165 __ subs(temp0, temp0, ShifterOperand(mirror::kUseStringCompression ? 8 : 4));
1166 __ b(&loop, HI);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001167 __ b(&end);
1168
jessicahandojo05765752016-09-09 19:01:32 -07001169 __ Bind(&find_char_diff_2nd_cmp);
1170 if (mirror::kUseStringCompression) {
1171 __ subs(temp0, temp0, ShifterOperand(4)); // 4 bytes previously compared.
1172 __ b(&end, LS); // Was the second comparison fully beyond the end?
1173 } else {
1174 // Without string compression, we can start treating temp0 as signed
1175 // and rely on the signed comparison below.
1176 __ sub(temp0, temp0, ShifterOperand(2));
1177 }
1178
1179 // Find the single character difference.
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001180 __ Bind(&find_char_diff);
1181 // Get the bit position of the first character that differs.
1182 __ eor(temp1, temp2, ShifterOperand(IP));
1183 __ rbit(temp1, temp1);
1184 __ clz(temp1, temp1);
1185
jessicahandojo05765752016-09-09 19:01:32 -07001186 // temp0 = number of characters remaining to compare.
1187 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1188 // in the comparison loop, and after the end of the shorter string data).
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001189
jessicahandojo05765752016-09-09 19:01:32 -07001190 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1191 // two words compared, in the interval [0,1].
1192 // (0 for low half-word different, 1 for high half-word different).
1193 // With string compression, (temp1 << 3) = byte where the difference occurs,
1194 // in the interval [0,3].
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001195
jessicahandojo05765752016-09-09 19:01:32 -07001196 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1197 // the remaining string data, so just return length diff (out).
1198 // The comparison is unsigned for string compression, otherwise signed.
1199 __ cmp(temp0, ShifterOperand(temp1, LSR, mirror::kUseStringCompression ? 3 : 4));
1200 __ b(&end, mirror::kUseStringCompression ? LS : LE);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001201 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001202 Label uncompressed_string, continue_process;
1203 if (mirror::kUseStringCompression) {
1204 __ cmp(temp4, ShifterOperand(0));
1205 __ b(&uncompressed_string, GE);
1206 __ bic(temp1, temp1, ShifterOperand(0x7));
1207 __ b(&continue_process);
1208 }
1209 __ Bind(&uncompressed_string);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001210 __ bic(temp1, temp1, ShifterOperand(0xf));
jessicahandojo05765752016-09-09 19:01:32 -07001211 __ Bind(&continue_process);
1212
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001213 __ Lsr(temp2, temp2, temp1);
1214 __ Lsr(IP, IP, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001215 Label calculate_difference, uncompressed_string_extract_chars;
1216 if (mirror::kUseStringCompression) {
1217 __ cmp(temp4, ShifterOperand(0));
1218 __ b(&uncompressed_string_extract_chars, GE);
1219 __ ubfx(temp2, temp2, 0, 8);
1220 __ ubfx(IP, IP, 0, 8);
1221 __ b(&calculate_difference);
1222 }
1223 __ Bind(&uncompressed_string_extract_chars);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001224 __ movt(temp2, 0);
1225 __ movt(IP, 0);
jessicahandojo05765752016-09-09 19:01:32 -07001226 __ Bind(&calculate_difference);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001227 __ sub(out, IP, ShifterOperand(temp2));
jessicahandojo05765752016-09-09 19:01:32 -07001228 __ b(&end);
1229
1230 if (mirror::kUseStringCompression) {
1231 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1232 DCHECK_EQ(c_char_size, 1u);
1233 Label loop_arg_compressed, loop_this_compressed, find_diff;
1234 // Comparison for different compression style.
1235 // This part is when THIS is compressed and ARG is not.
1236 __ Bind(&different_compression);
1237 __ add(temp2, str, ShifterOperand(value_offset));
1238 __ add(temp3, arg, ShifterOperand(value_offset));
1239 __ cmp(temp4, ShifterOperand(0));
1240 __ b(&loop_arg_compressed, LT);
1241
1242 __ Bind(&loop_this_compressed);
1243 __ ldrb(IP, Address(temp2, c_char_size, Address::PostIndex));
1244 __ ldrh(temp4, Address(temp3, char_size, Address::PostIndex));
1245 __ cmp(IP, ShifterOperand(temp4));
1246 __ b(&find_diff, NE);
1247 __ subs(temp0, temp0, ShifterOperand(1));
1248 __ b(&loop_this_compressed, GT);
1249 __ b(&end);
1250
1251 // This part is when THIS is not compressed and ARG is.
1252 __ Bind(&loop_arg_compressed);
1253 __ ldrh(IP, Address(temp2, char_size, Address::PostIndex));
1254 __ ldrb(temp4, Address(temp3, c_char_size, Address::PostIndex));
1255 __ cmp(IP, ShifterOperand(temp4));
1256 __ b(&find_diff, NE);
1257 __ subs(temp0, temp0, ShifterOperand(1));
1258 __ b(&loop_arg_compressed, GT);
1259 __ b(&end);
1260
1261 // Calculate the difference.
1262 __ Bind(&find_diff);
1263 __ sub(out, IP, ShifterOperand(temp4));
1264 }
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001265
1266 __ Bind(&end);
1267
1268 if (can_slow_path) {
1269 __ Bind(slow_path->GetExitLabel());
1270 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001271}
1272
Agi Csaki289cd552015-08-18 17:10:38 -07001273void IntrinsicLocationsBuilderARM::VisitStringEquals(HInvoke* invoke) {
1274 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1275 LocationSummary::kNoCall,
1276 kIntrinsified);
1277 InvokeRuntimeCallingConvention calling_convention;
1278 locations->SetInAt(0, Location::RequiresRegister());
1279 locations->SetInAt(1, Location::RequiresRegister());
1280 // Temporary registers to store lengths of strings and for calculations.
1281 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1282 locations->AddTemp(Location::RegisterLocation(R0));
1283 locations->AddTemp(Location::RequiresRegister());
1284 locations->AddTemp(Location::RequiresRegister());
1285
1286 locations->SetOut(Location::RequiresRegister());
1287}
1288
1289void IntrinsicCodeGeneratorARM::VisitStringEquals(HInvoke* invoke) {
1290 ArmAssembler* assembler = GetAssembler();
1291 LocationSummary* locations = invoke->GetLocations();
1292
1293 Register str = locations->InAt(0).AsRegister<Register>();
1294 Register arg = locations->InAt(1).AsRegister<Register>();
1295 Register out = locations->Out().AsRegister<Register>();
1296
1297 Register temp = locations->GetTemp(0).AsRegister<Register>();
1298 Register temp1 = locations->GetTemp(1).AsRegister<Register>();
1299 Register temp2 = locations->GetTemp(2).AsRegister<Register>();
1300
jessicahandojo05765752016-09-09 19:01:32 -07001301 Label loop, preloop;
Agi Csaki289cd552015-08-18 17:10:38 -07001302 Label end;
1303 Label return_true;
1304 Label return_false;
1305
1306 // Get offsets of count, value, and class fields within a string object.
1307 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1308 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1309 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1310
1311 // Note that the null check must have been done earlier.
1312 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1313
Vladimir Marko53b52002016-05-24 19:30:45 +01001314 StringEqualsOptimizations optimizations(invoke);
1315 if (!optimizations.GetArgumentNotNull()) {
1316 // Check if input is null, return false if it is.
1317 __ CompareAndBranchIfZero(arg, &return_false);
1318 }
Agi Csaki289cd552015-08-18 17:10:38 -07001319
Vladimir Marko53b52002016-05-24 19:30:45 +01001320 if (!optimizations.GetArgumentIsString()) {
1321 // Instanceof check for the argument by comparing class fields.
1322 // All string objects must have the same type since String cannot be subclassed.
1323 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1324 // If the argument is a string object, its class field must be equal to receiver's class field.
1325 __ ldr(temp, Address(str, class_offset));
1326 __ ldr(temp1, Address(arg, class_offset));
1327 __ cmp(temp, ShifterOperand(temp1));
1328 __ b(&return_false, NE);
1329 }
Agi Csaki289cd552015-08-18 17:10:38 -07001330
1331 // Load lengths of this and argument strings.
1332 __ ldr(temp, Address(str, count_offset));
1333 __ ldr(temp1, Address(arg, count_offset));
1334 // Check if lengths are equal, return false if they're not.
jessicahandojo05765752016-09-09 19:01:32 -07001335 // Also compares the compression style, if differs return false.
Agi Csaki289cd552015-08-18 17:10:38 -07001336 __ cmp(temp, ShifterOperand(temp1));
1337 __ b(&return_false, NE);
1338 // Return true if both strings are empty.
jessicahandojo05765752016-09-09 19:01:32 -07001339 if (mirror::kUseStringCompression) {
1340 // Length needs to be masked out first because 0 is treated as compressed.
1341 __ bic(temp, temp, ShifterOperand(0x80000000));
1342 }
Agi Csaki289cd552015-08-18 17:10:38 -07001343 __ cbz(temp, &return_true);
Agi Csaki289cd552015-08-18 17:10:38 -07001344 // Reference equality check, return true if same reference.
1345 __ cmp(str, ShifterOperand(arg));
1346 __ b(&return_true, EQ);
1347
1348 // Assertions that must hold in order to compare strings 2 characters at a time.
1349 DCHECK_ALIGNED(value_offset, 4);
Scott Wakelingc25cbf12016-04-18 09:00:11 +01001350 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
Agi Csaki289cd552015-08-18 17:10:38 -07001351
jessicahandojo05765752016-09-09 19:01:32 -07001352 if (mirror::kUseStringCompression) {
1353 // If not compressed, directly to fast compare. Else do preprocess on length.
1354 __ cmp(temp1, ShifterOperand(0));
1355 __ b(&preloop, GT);
1356 // Mask out compression flag and adjust length for compressed string (8-bit)
1357 // as if it is a 16-bit data, new_length = (length + 1) / 2.
1358 __ add(temp, temp, ShifterOperand(1));
1359 __ Lsr(temp, temp, 1);
1360 __ Bind(&preloop);
1361 }
Agi Csaki289cd552015-08-18 17:10:38 -07001362 // Loop to compare strings 2 characters at a time starting at the front of the string.
1363 // Ok to do this because strings with an odd length are zero-padded.
jessicahandojo05765752016-09-09 19:01:32 -07001364 __ LoadImmediate(temp1, value_offset);
Agi Csaki289cd552015-08-18 17:10:38 -07001365 __ Bind(&loop);
1366 __ ldr(out, Address(str, temp1));
1367 __ ldr(temp2, Address(arg, temp1));
1368 __ cmp(out, ShifterOperand(temp2));
1369 __ b(&return_false, NE);
1370 __ add(temp1, temp1, ShifterOperand(sizeof(uint32_t)));
Vladimir Markoa63f0d42015-09-01 13:36:35 +01001371 __ subs(temp, temp, ShifterOperand(sizeof(uint32_t) / sizeof(uint16_t)));
1372 __ b(&loop, GT);
Agi Csaki289cd552015-08-18 17:10:38 -07001373
1374 // Return true and exit the function.
1375 // If loop does not result in returning false, we return true.
1376 __ Bind(&return_true);
1377 __ LoadImmediate(out, 1);
1378 __ b(&end);
1379
1380 // Return false and exit the function.
1381 __ Bind(&return_false);
1382 __ LoadImmediate(out, 0);
1383 __ Bind(&end);
1384}
1385
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001386static void GenerateVisitStringIndexOf(HInvoke* invoke,
1387 ArmAssembler* assembler,
1388 CodeGeneratorARM* codegen,
1389 ArenaAllocator* allocator,
1390 bool start_at_zero) {
1391 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001392
1393 // Note that the null check must have been done earlier.
1394 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1395
1396 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001397 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001398 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001399 HInstruction* code_point = invoke->InputAt(1);
1400 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001401 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001402 std::numeric_limits<uint16_t>::max()) {
1403 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1404 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1405 slow_path = new (allocator) IntrinsicSlowPathARM(invoke);
1406 codegen->AddSlowPath(slow_path);
1407 __ b(slow_path->GetEntryLabel());
1408 __ Bind(slow_path->GetExitLabel());
1409 return;
1410 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001411 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001412 Register char_reg = locations->InAt(1).AsRegister<Register>();
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001413 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1414 __ cmp(char_reg,
1415 ShifterOperand(static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001416 slow_path = new (allocator) IntrinsicSlowPathARM(invoke);
1417 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001418 __ b(slow_path->GetEntryLabel(), HS);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001419 }
1420
1421 if (start_at_zero) {
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001422 Register tmp_reg = locations->GetTemp(0).AsRegister<Register>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001423 DCHECK_EQ(tmp_reg, R2);
1424 // Start-index = 0.
1425 __ LoadImmediate(tmp_reg, 0);
1426 }
1427
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001428 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001429 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001430
1431 if (slow_path != nullptr) {
1432 __ Bind(slow_path->GetExitLabel());
1433 }
1434}
1435
1436void IntrinsicLocationsBuilderARM::VisitStringIndexOf(HInvoke* invoke) {
1437 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001438 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001439 kIntrinsified);
1440 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1441 // best to align the inputs accordingly.
1442 InvokeRuntimeCallingConvention calling_convention;
1443 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1444 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1445 locations->SetOut(Location::RegisterLocation(R0));
1446
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001447 // Need to send start-index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001448 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1449}
1450
1451void IntrinsicCodeGeneratorARM::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001452 GenerateVisitStringIndexOf(
1453 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001454}
1455
1456void IntrinsicLocationsBuilderARM::VisitStringIndexOfAfter(HInvoke* invoke) {
1457 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001458 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001459 kIntrinsified);
1460 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1461 // best to align the inputs accordingly.
1462 InvokeRuntimeCallingConvention calling_convention;
1463 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1464 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1465 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1466 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001467}
1468
1469void IntrinsicCodeGeneratorARM::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001470 GenerateVisitStringIndexOf(
1471 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001472}
1473
Jeff Hao848f70a2014-01-15 13:49:50 -08001474void IntrinsicLocationsBuilderARM::VisitStringNewStringFromBytes(HInvoke* invoke) {
1475 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001476 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001477 kIntrinsified);
1478 InvokeRuntimeCallingConvention calling_convention;
1479 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1480 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1481 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1482 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1483 locations->SetOut(Location::RegisterLocation(R0));
1484}
1485
1486void IntrinsicCodeGeneratorARM::VisitStringNewStringFromBytes(HInvoke* invoke) {
1487 ArmAssembler* assembler = GetAssembler();
1488 LocationSummary* locations = invoke->GetLocations();
1489
1490 Register byte_array = locations->InAt(0).AsRegister<Register>();
1491 __ cmp(byte_array, ShifterOperand(0));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001492 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001493 codegen_->AddSlowPath(slow_path);
1494 __ b(slow_path->GetEntryLabel(), EQ);
1495
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001496 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001497 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001498 __ Bind(slow_path->GetExitLabel());
1499}
1500
1501void IntrinsicLocationsBuilderARM::VisitStringNewStringFromChars(HInvoke* invoke) {
1502 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001503 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001504 kIntrinsified);
1505 InvokeRuntimeCallingConvention calling_convention;
1506 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1507 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1508 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1509 locations->SetOut(Location::RegisterLocation(R0));
1510}
1511
1512void IntrinsicCodeGeneratorARM::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001513 // No need to emit code checking whether `locations->InAt(2)` is a null
1514 // pointer, as callers of the native method
1515 //
1516 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1517 //
1518 // all include a null check on `data` before calling that method.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001519 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001520 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001521}
1522
1523void IntrinsicLocationsBuilderARM::VisitStringNewStringFromString(HInvoke* invoke) {
1524 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001525 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001526 kIntrinsified);
1527 InvokeRuntimeCallingConvention calling_convention;
1528 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1529 locations->SetOut(Location::RegisterLocation(R0));
1530}
1531
1532void IntrinsicCodeGeneratorARM::VisitStringNewStringFromString(HInvoke* invoke) {
1533 ArmAssembler* assembler = GetAssembler();
1534 LocationSummary* locations = invoke->GetLocations();
1535
1536 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1537 __ cmp(string_to_copy, ShifterOperand(0));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001538 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001539 codegen_->AddSlowPath(slow_path);
1540 __ b(slow_path->GetEntryLabel(), EQ);
1541
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001542 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001543 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001544
Jeff Hao848f70a2014-01-15 13:49:50 -08001545 __ Bind(slow_path->GetExitLabel());
1546}
1547
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001548void IntrinsicLocationsBuilderARM::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001549 // The only read barrier implementation supporting the
1550 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1551 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001552 return;
1553 }
1554
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001555 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
1556 LocationSummary* locations = invoke->GetLocations();
1557 if (locations == nullptr) {
1558 return;
1559 }
1560
1561 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1562 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1563 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1564
1565 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
1566 locations->SetInAt(1, Location::RequiresRegister());
1567 }
1568 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
1569 locations->SetInAt(3, Location::RequiresRegister());
1570 }
1571 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
1572 locations->SetInAt(4, Location::RequiresRegister());
1573 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001574 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1575 // Temporary register IP cannot be used in
Roland Levillain16d9f942016-08-25 17:27:56 +01001576 // ReadBarrierSystemArrayCopySlowPathARM (because that register
Roland Levillain0b671c02016-08-19 12:02:34 +01001577 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
1578 // temporary register from the register allocator.
1579 locations->AddTemp(Location::RequiresRegister());
1580 }
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001581}
1582
1583static void CheckPosition(ArmAssembler* assembler,
1584 Location pos,
1585 Register input,
1586 Location length,
1587 SlowPathCode* slow_path,
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001588 Register temp,
1589 bool length_is_input_length = false) {
1590 // Where is the length in the Array?
1591 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1592
1593 if (pos.IsConstant()) {
1594 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1595 if (pos_const == 0) {
1596 if (!length_is_input_length) {
1597 // Check that length(input) >= length.
1598 __ LoadFromOffset(kLoadWord, temp, input, length_offset);
1599 if (length.IsConstant()) {
1600 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1601 } else {
1602 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1603 }
1604 __ b(slow_path->GetEntryLabel(), LT);
1605 }
1606 } else {
1607 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001608 __ LoadFromOffset(kLoadWord, temp, input, length_offset);
1609 __ subs(temp, temp, ShifterOperand(pos_const));
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001610 __ b(slow_path->GetEntryLabel(), LT);
1611
1612 // Check that (length(input) - pos) >= length.
1613 if (length.IsConstant()) {
1614 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1615 } else {
1616 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1617 }
1618 __ b(slow_path->GetEntryLabel(), LT);
1619 }
1620 } else if (length_is_input_length) {
1621 // The only way the copy can succeed is if pos is zero.
1622 Register pos_reg = pos.AsRegister<Register>();
1623 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
1624 } else {
1625 // Check that pos >= 0.
1626 Register pos_reg = pos.AsRegister<Register>();
1627 __ cmp(pos_reg, ShifterOperand(0));
1628 __ b(slow_path->GetEntryLabel(), LT);
1629
1630 // Check that pos <= length(input).
1631 __ LoadFromOffset(kLoadWord, temp, input, length_offset);
1632 __ subs(temp, temp, ShifterOperand(pos_reg));
1633 __ b(slow_path->GetEntryLabel(), LT);
1634
1635 // Check that (length(input) - pos) >= length.
1636 if (length.IsConstant()) {
1637 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1638 } else {
1639 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1640 }
1641 __ b(slow_path->GetEntryLabel(), LT);
1642 }
1643}
1644
1645void IntrinsicCodeGeneratorARM::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001646 // The only read barrier implementation supporting the
1647 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1648 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001649
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001650 ArmAssembler* assembler = GetAssembler();
1651 LocationSummary* locations = invoke->GetLocations();
1652
1653 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1654 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1655 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1656 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001657 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001658
1659 Register src = locations->InAt(0).AsRegister<Register>();
1660 Location src_pos = locations->InAt(1);
1661 Register dest = locations->InAt(2).AsRegister<Register>();
1662 Location dest_pos = locations->InAt(3);
1663 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001664 Location temp1_loc = locations->GetTemp(0);
1665 Register temp1 = temp1_loc.AsRegister<Register>();
1666 Location temp2_loc = locations->GetTemp(1);
1667 Register temp2 = temp2_loc.AsRegister<Register>();
1668 Location temp3_loc = locations->GetTemp(2);
1669 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001670
Roland Levillain0b671c02016-08-19 12:02:34 +01001671 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
1672 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001673
Roland Levillainebea3d22016-04-12 15:42:57 +01001674 Label conditions_on_positions_validated;
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001675 SystemArrayCopyOptimizations optimizations(invoke);
1676
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001677 // If source and destination are the same, we go to slow path if we need to do
1678 // forward copying.
1679 if (src_pos.IsConstant()) {
1680 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1681 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001682 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1683 if (optimizations.GetDestinationIsSource()) {
1684 // Checked when building locations.
1685 DCHECK_GE(src_pos_constant, dest_pos_constant);
1686 } else if (src_pos_constant < dest_pos_constant) {
1687 __ cmp(src, ShifterOperand(dest));
Roland Levillain0b671c02016-08-19 12:02:34 +01001688 __ b(intrinsic_slow_path->GetEntryLabel(), EQ);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001689 }
1690
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001691 // Checked when building locations.
1692 DCHECK(!optimizations.GetDestinationIsSource()
1693 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1694 } else {
1695 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001696 __ cmp(src, ShifterOperand(dest));
Roland Levillainebea3d22016-04-12 15:42:57 +01001697 __ b(&conditions_on_positions_validated, NE);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001698 }
1699 __ cmp(dest_pos.AsRegister<Register>(), ShifterOperand(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001700 __ b(intrinsic_slow_path->GetEntryLabel(), GT);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001701 }
1702 } else {
1703 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001704 __ cmp(src, ShifterOperand(dest));
Roland Levillainebea3d22016-04-12 15:42:57 +01001705 __ b(&conditions_on_positions_validated, NE);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001706 }
1707 if (dest_pos.IsConstant()) {
1708 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1709 __ cmp(src_pos.AsRegister<Register>(), ShifterOperand(dest_pos_constant));
1710 } else {
1711 __ cmp(src_pos.AsRegister<Register>(), ShifterOperand(dest_pos.AsRegister<Register>()));
1712 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001713 __ b(intrinsic_slow_path->GetEntryLabel(), LT);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001714 }
1715
Roland Levillainebea3d22016-04-12 15:42:57 +01001716 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001717
1718 if (!optimizations.GetSourceIsNotNull()) {
1719 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01001720 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001721 }
1722
1723 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1724 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01001725 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001726 }
1727
1728 // If the length is negative, bail out.
1729 // We have already checked in the LocationsBuilder for the constant case.
1730 if (!length.IsConstant() &&
1731 !optimizations.GetCountIsSourceLength() &&
1732 !optimizations.GetCountIsDestinationLength()) {
1733 __ cmp(length.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01001734 __ b(intrinsic_slow_path->GetEntryLabel(), LT);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001735 }
1736
1737 // Validity checks: source.
1738 CheckPosition(assembler,
1739 src_pos,
1740 src,
1741 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001742 intrinsic_slow_path,
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001743 temp1,
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001744 optimizations.GetCountIsSourceLength());
1745
1746 // Validity checks: dest.
1747 CheckPosition(assembler,
1748 dest_pos,
1749 dest,
1750 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001751 intrinsic_slow_path,
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001752 temp1,
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001753 optimizations.GetCountIsDestinationLength());
1754
1755 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1756 // Check whether all elements of the source array are assignable to the component
1757 // type of the destination array. We do two checks: the classes are the same,
1758 // or the destination is Object[]. If none of these checks succeed, we go to the
1759 // slow path.
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001760
Roland Levillain0b671c02016-08-19 12:02:34 +01001761 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1762 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1763 // /* HeapReference<Class> */ temp1 = src->klass_
1764 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1765 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
1766 // Bail out if the source is not a non primitive array.
1767 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1768 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1769 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
1770 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
1771 // If heap poisoning is enabled, `temp1` has been unpoisoned
1772 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1773 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
1774 __ LoadFromOffset(kLoadUnsignedHalfword, temp1, temp1, primitive_offset);
1775 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1776 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001777 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001778
1779 // /* HeapReference<Class> */ temp1 = dest->klass_
1780 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1781 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
1782
1783 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1784 // Bail out if the destination is not a non primitive array.
1785 //
1786 // Register `temp1` is not trashed by the read barrier emitted
1787 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1788 // method produces a call to a ReadBarrierMarkRegX entry point,
1789 // which saves all potentially live registers, including
1790 // temporaries such a `temp1`.
1791 // /* HeapReference<Class> */ temp2 = temp1->component_type_
1792 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1793 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
1794 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
1795 // If heap poisoning is enabled, `temp2` has been unpoisoned
1796 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1797 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
1798 __ LoadFromOffset(kLoadUnsignedHalfword, temp2, temp2, primitive_offset);
1799 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1800 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
1801 }
1802
1803 // For the same reason given earlier, `temp1` is not trashed by the
1804 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1805 // /* HeapReference<Class> */ temp2 = src->klass_
1806 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1807 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
1808 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
1809 __ cmp(temp1, ShifterOperand(temp2));
1810
1811 if (optimizations.GetDestinationIsTypedObjectArray()) {
1812 Label do_copy;
1813 __ b(&do_copy, EQ);
1814 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1815 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1816 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
1817 // /* HeapReference<Class> */ temp1 = temp1->super_class_
1818 // We do not need to emit a read barrier for the following
1819 // heap reference load, as `temp1` is only used in a
1820 // comparison with null below, and this reference is not
1821 // kept afterwards.
1822 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
1823 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
1824 __ Bind(&do_copy);
1825 } else {
1826 __ b(intrinsic_slow_path->GetEntryLabel(), NE);
1827 }
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001828 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001829 // Non read barrier code.
1830
1831 // /* HeapReference<Class> */ temp1 = dest->klass_
1832 __ LoadFromOffset(kLoadWord, temp1, dest, class_offset);
1833 // /* HeapReference<Class> */ temp2 = src->klass_
1834 __ LoadFromOffset(kLoadWord, temp2, src, class_offset);
1835 bool did_unpoison = false;
1836 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1837 !optimizations.GetSourceIsNonPrimitiveArray()) {
1838 // One or two of the references need to be unpoisoned. Unpoison them
1839 // both to make the identity check valid.
1840 __ MaybeUnpoisonHeapReference(temp1);
1841 __ MaybeUnpoisonHeapReference(temp2);
1842 did_unpoison = true;
1843 }
1844
1845 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1846 // Bail out if the destination is not a non primitive array.
1847 // /* HeapReference<Class> */ temp3 = temp1->component_type_
1848 __ LoadFromOffset(kLoadWord, temp3, temp1, component_offset);
1849 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
1850 __ MaybeUnpoisonHeapReference(temp3);
1851 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
1852 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1853 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1854 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
1855 }
1856
1857 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1858 // Bail out if the source is not a non primitive array.
1859 // /* HeapReference<Class> */ temp3 = temp2->component_type_
1860 __ LoadFromOffset(kLoadWord, temp3, temp2, component_offset);
1861 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
1862 __ MaybeUnpoisonHeapReference(temp3);
1863 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
1864 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1865 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1866 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
1867 }
1868
1869 __ cmp(temp1, ShifterOperand(temp2));
1870
1871 if (optimizations.GetDestinationIsTypedObjectArray()) {
1872 Label do_copy;
1873 __ b(&do_copy, EQ);
1874 if (!did_unpoison) {
1875 __ MaybeUnpoisonHeapReference(temp1);
1876 }
1877 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1878 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
1879 __ MaybeUnpoisonHeapReference(temp1);
1880 // /* HeapReference<Class> */ temp1 = temp1->super_class_
1881 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
1882 // No need to unpoison the result, we're comparing against null.
1883 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
1884 __ Bind(&do_copy);
1885 } else {
1886 __ b(intrinsic_slow_path->GetEntryLabel(), NE);
1887 }
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001888 }
1889 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1890 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1891 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001892 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1893 // /* HeapReference<Class> */ temp1 = src->klass_
1894 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1895 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
1896 // /* HeapReference<Class> */ temp3 = temp1->component_type_
1897 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1898 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
1899 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
1900 // If heap poisoning is enabled, `temp3` has been unpoisoned
1901 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1902 } else {
1903 // /* HeapReference<Class> */ temp1 = src->klass_
1904 __ LoadFromOffset(kLoadWord, temp1, src, class_offset);
1905 __ MaybeUnpoisonHeapReference(temp1);
1906 // /* HeapReference<Class> */ temp3 = temp1->component_type_
1907 __ LoadFromOffset(kLoadWord, temp3, temp1, component_offset);
1908 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
1909 __ MaybeUnpoisonHeapReference(temp3);
1910 }
1911 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001912 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1913 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01001914 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001915 }
1916
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001917 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Roland Levillain0b671c02016-08-19 12:02:34 +01001918 uint32_t element_size_shift = Primitive::ComponentSizeShift(Primitive::kPrimNot);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001919 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001920
1921 // Compute the base source address in `temp1`.
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001922 if (src_pos.IsConstant()) {
1923 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1924 __ AddConstant(temp1, src, element_size * constant + offset);
1925 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001926 __ add(temp1, src, ShifterOperand(src_pos.AsRegister<Register>(), LSL, element_size_shift));
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001927 __ AddConstant(temp1, offset);
1928 }
1929
Roland Levillain0b671c02016-08-19 12:02:34 +01001930 // Compute the end source address in `temp3`.
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001931 if (length.IsConstant()) {
1932 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1933 __ AddConstant(temp3, temp1, element_size * constant);
1934 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001935 __ add(temp3, temp1, ShifterOperand(length.AsRegister<Register>(), LSL, element_size_shift));
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001936 }
1937
Roland Levillain0b671c02016-08-19 12:02:34 +01001938 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1939 // The base destination address is computed later, as `temp2` is
1940 // used for intermediate computations.
1941
1942 // SystemArrayCopy implementation for Baker read barriers (see
1943 // also CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier):
1944 //
1945 // if (src_ptr != end_ptr) {
1946 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1947 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001948 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001949 // if (is_gray) {
1950 // // Slow-path copy.
1951 // do {
1952 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1953 // } while (src_ptr != end_ptr)
1954 // } else {
1955 // // Fast-path copy.
1956 // do {
1957 // *dest_ptr++ = *src_ptr++;
1958 // } while (src_ptr != end_ptr)
1959 // }
1960 // }
1961
1962 Label loop, done;
1963
1964 // Don't enter copy loop if `length == 0`.
1965 __ cmp(temp1, ShifterOperand(temp3));
1966 __ b(&done, EQ);
1967
1968 // /* int32_t */ monitor = src->monitor_
1969 __ LoadFromOffset(kLoadWord, temp2, src, monitor_offset);
1970 // /* LockWord */ lock_word = LockWord(monitor)
1971 static_assert(sizeof(LockWord) == sizeof(int32_t),
1972 "art::LockWord and int32_t have different sizes.");
1973
1974 // Introduce a dependency on the lock_word including the rb_state,
1975 // which shall prevent load-load reordering without using
1976 // a memory barrier (which would be more expensive).
1977 // `src` is unchanged by this operation, but its value now depends
1978 // on `temp2`.
1979 __ add(src, src, ShifterOperand(temp2, LSR, 32));
1980
1981 // Slow path used to copy array when `src` is gray.
1982 SlowPathCode* read_barrier_slow_path =
1983 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARM(invoke);
1984 codegen_->AddSlowPath(read_barrier_slow_path);
1985
1986 // Given the numeric representation, it's enough to check the low bit of the
1987 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1988 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001989 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1990 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Roland Levillain0b671c02016-08-19 12:02:34 +01001991 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
1992 // Carry flag is the last bit shifted out by LSRS.
1993 __ b(read_barrier_slow_path->GetEntryLabel(), CS);
1994
1995 // Fast-path copy.
1996
1997 // Compute the base destination address in `temp2`.
1998 if (dest_pos.IsConstant()) {
1999 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2000 __ AddConstant(temp2, dest, element_size * constant + offset);
2001 } else {
2002 __ add(temp2, dest, ShifterOperand(dest_pos.AsRegister<Register>(), LSL, element_size_shift));
2003 __ AddConstant(temp2, offset);
2004 }
2005
2006 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2007 // poison/unpoison.
2008 __ Bind(&loop);
2009 __ ldr(IP, Address(temp1, element_size, Address::PostIndex));
2010 __ str(IP, Address(temp2, element_size, Address::PostIndex));
2011 __ cmp(temp1, ShifterOperand(temp3));
2012 __ b(&loop, NE);
2013
2014 __ Bind(read_barrier_slow_path->GetExitLabel());
2015 __ Bind(&done);
2016 } else {
2017 // Non read barrier code.
2018
2019 // Compute the base destination address in `temp2`.
2020 if (dest_pos.IsConstant()) {
2021 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2022 __ AddConstant(temp2, dest, element_size * constant + offset);
2023 } else {
2024 __ add(temp2, dest, ShifterOperand(dest_pos.AsRegister<Register>(), LSL, element_size_shift));
2025 __ AddConstant(temp2, offset);
2026 }
2027
2028 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2029 // poison/unpoison.
2030 Label loop, done;
2031 __ cmp(temp1, ShifterOperand(temp3));
2032 __ b(&done, EQ);
2033 __ Bind(&loop);
2034 __ ldr(IP, Address(temp1, element_size, Address::PostIndex));
2035 __ str(IP, Address(temp2, element_size, Address::PostIndex));
2036 __ cmp(temp1, ShifterOperand(temp3));
2037 __ b(&loop, NE);
2038 __ Bind(&done);
2039 }
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002040
2041 // We only need one card marking on the destination array.
2042 codegen_->MarkGCCard(temp1,
2043 temp2,
2044 dest,
2045 Register(kNoRegister),
Roland Levillainebea3d22016-04-12 15:42:57 +01002046 /* value_can_be_null */ false);
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002047
Roland Levillain0b671c02016-08-19 12:02:34 +01002048 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002049}
2050
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002051static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2052 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2053 // the code generator. Furthermore, the register allocator creates fixed live intervals
2054 // for all caller-saved registers because we are doing a function call. As a result, if
2055 // the input and output locations are unallocated, the register allocator runs out of
2056 // registers and fails; however, a debuggable graph is not the common case.
2057 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2058 return;
2059 }
2060
2061 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2062 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2063 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2064
2065 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002066 LocationSummary::kCallOnMainOnly,
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002067 kIntrinsified);
2068 const InvokeRuntimeCallingConvention calling_convention;
2069
2070 locations->SetInAt(0, Location::RequiresFpuRegister());
2071 locations->SetOut(Location::RequiresFpuRegister());
2072 // Native code uses the soft float ABI.
2073 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2074 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2075}
2076
2077static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2078 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2079 // the code generator. Furthermore, the register allocator creates fixed live intervals
2080 // for all caller-saved registers because we are doing a function call. As a result, if
2081 // the input and output locations are unallocated, the register allocator runs out of
2082 // registers and fails; however, a debuggable graph is not the common case.
2083 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2084 return;
2085 }
2086
2087 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2088 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2089 DCHECK_EQ(invoke->InputAt(1)->GetType(), Primitive::kPrimDouble);
2090 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2091
2092 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002093 LocationSummary::kCallOnMainOnly,
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002094 kIntrinsified);
2095 const InvokeRuntimeCallingConvention calling_convention;
2096
2097 locations->SetInAt(0, Location::RequiresFpuRegister());
2098 locations->SetInAt(1, Location::RequiresFpuRegister());
2099 locations->SetOut(Location::RequiresFpuRegister());
2100 // Native code uses the soft float ABI.
2101 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2102 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2103 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2104 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
2105}
2106
2107static void GenFPToFPCall(HInvoke* invoke,
2108 ArmAssembler* assembler,
2109 CodeGeneratorARM* codegen,
2110 QuickEntrypointEnum entry) {
2111 LocationSummary* const locations = invoke->GetLocations();
2112 const InvokeRuntimeCallingConvention calling_convention;
2113
2114 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2115 DCHECK(locations->WillCall() && locations->Intrinsified());
2116 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(0)));
2117 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(1)));
2118
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002119 // Native code uses the soft float ABI.
2120 __ vmovrrd(calling_convention.GetRegisterAt(0),
2121 calling_convention.GetRegisterAt(1),
2122 FromLowSToD(locations->InAt(0).AsFpuRegisterPairLow<SRegister>()));
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002123 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002124 __ vmovdrr(FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>()),
2125 calling_convention.GetRegisterAt(0),
2126 calling_convention.GetRegisterAt(1));
2127}
2128
2129static void GenFPFPToFPCall(HInvoke* invoke,
2130 ArmAssembler* assembler,
2131 CodeGeneratorARM* codegen,
2132 QuickEntrypointEnum entry) {
2133 LocationSummary* const locations = invoke->GetLocations();
2134 const InvokeRuntimeCallingConvention calling_convention;
2135
2136 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2137 DCHECK(locations->WillCall() && locations->Intrinsified());
2138 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(0)));
2139 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(1)));
2140 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(2)));
2141 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(calling_convention.GetRegisterAt(3)));
2142
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002143 // Native code uses the soft float ABI.
2144 __ vmovrrd(calling_convention.GetRegisterAt(0),
2145 calling_convention.GetRegisterAt(1),
2146 FromLowSToD(locations->InAt(0).AsFpuRegisterPairLow<SRegister>()));
2147 __ vmovrrd(calling_convention.GetRegisterAt(2),
2148 calling_convention.GetRegisterAt(3),
2149 FromLowSToD(locations->InAt(1).AsFpuRegisterPairLow<SRegister>()));
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002150 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilovd70dc9d2016-02-04 14:59:04 +00002151 __ vmovdrr(FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>()),
2152 calling_convention.GetRegisterAt(0),
2153 calling_convention.GetRegisterAt(1));
2154}
2155
2156void IntrinsicLocationsBuilderARM::VisitMathCos(HInvoke* invoke) {
2157 CreateFPToFPCallLocations(arena_, invoke);
2158}
2159
2160void IntrinsicCodeGeneratorARM::VisitMathCos(HInvoke* invoke) {
2161 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2162}
2163
2164void IntrinsicLocationsBuilderARM::VisitMathSin(HInvoke* invoke) {
2165 CreateFPToFPCallLocations(arena_, invoke);
2166}
2167
2168void IntrinsicCodeGeneratorARM::VisitMathSin(HInvoke* invoke) {
2169 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2170}
2171
2172void IntrinsicLocationsBuilderARM::VisitMathAcos(HInvoke* invoke) {
2173 CreateFPToFPCallLocations(arena_, invoke);
2174}
2175
2176void IntrinsicCodeGeneratorARM::VisitMathAcos(HInvoke* invoke) {
2177 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2178}
2179
2180void IntrinsicLocationsBuilderARM::VisitMathAsin(HInvoke* invoke) {
2181 CreateFPToFPCallLocations(arena_, invoke);
2182}
2183
2184void IntrinsicCodeGeneratorARM::VisitMathAsin(HInvoke* invoke) {
2185 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2186}
2187
2188void IntrinsicLocationsBuilderARM::VisitMathAtan(HInvoke* invoke) {
2189 CreateFPToFPCallLocations(arena_, invoke);
2190}
2191
2192void IntrinsicCodeGeneratorARM::VisitMathAtan(HInvoke* invoke) {
2193 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2194}
2195
2196void IntrinsicLocationsBuilderARM::VisitMathCbrt(HInvoke* invoke) {
2197 CreateFPToFPCallLocations(arena_, invoke);
2198}
2199
2200void IntrinsicCodeGeneratorARM::VisitMathCbrt(HInvoke* invoke) {
2201 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2202}
2203
2204void IntrinsicLocationsBuilderARM::VisitMathCosh(HInvoke* invoke) {
2205 CreateFPToFPCallLocations(arena_, invoke);
2206}
2207
2208void IntrinsicCodeGeneratorARM::VisitMathCosh(HInvoke* invoke) {
2209 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2210}
2211
2212void IntrinsicLocationsBuilderARM::VisitMathExp(HInvoke* invoke) {
2213 CreateFPToFPCallLocations(arena_, invoke);
2214}
2215
2216void IntrinsicCodeGeneratorARM::VisitMathExp(HInvoke* invoke) {
2217 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2218}
2219
2220void IntrinsicLocationsBuilderARM::VisitMathExpm1(HInvoke* invoke) {
2221 CreateFPToFPCallLocations(arena_, invoke);
2222}
2223
2224void IntrinsicCodeGeneratorARM::VisitMathExpm1(HInvoke* invoke) {
2225 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2226}
2227
2228void IntrinsicLocationsBuilderARM::VisitMathLog(HInvoke* invoke) {
2229 CreateFPToFPCallLocations(arena_, invoke);
2230}
2231
2232void IntrinsicCodeGeneratorARM::VisitMathLog(HInvoke* invoke) {
2233 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2234}
2235
2236void IntrinsicLocationsBuilderARM::VisitMathLog10(HInvoke* invoke) {
2237 CreateFPToFPCallLocations(arena_, invoke);
2238}
2239
2240void IntrinsicCodeGeneratorARM::VisitMathLog10(HInvoke* invoke) {
2241 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2242}
2243
2244void IntrinsicLocationsBuilderARM::VisitMathSinh(HInvoke* invoke) {
2245 CreateFPToFPCallLocations(arena_, invoke);
2246}
2247
2248void IntrinsicCodeGeneratorARM::VisitMathSinh(HInvoke* invoke) {
2249 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2250}
2251
2252void IntrinsicLocationsBuilderARM::VisitMathTan(HInvoke* invoke) {
2253 CreateFPToFPCallLocations(arena_, invoke);
2254}
2255
2256void IntrinsicCodeGeneratorARM::VisitMathTan(HInvoke* invoke) {
2257 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2258}
2259
2260void IntrinsicLocationsBuilderARM::VisitMathTanh(HInvoke* invoke) {
2261 CreateFPToFPCallLocations(arena_, invoke);
2262}
2263
2264void IntrinsicCodeGeneratorARM::VisitMathTanh(HInvoke* invoke) {
2265 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2266}
2267
2268void IntrinsicLocationsBuilderARM::VisitMathAtan2(HInvoke* invoke) {
2269 CreateFPFPToFPCallLocations(arena_, invoke);
2270}
2271
2272void IntrinsicCodeGeneratorARM::VisitMathAtan2(HInvoke* invoke) {
2273 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2274}
2275
2276void IntrinsicLocationsBuilderARM::VisitMathHypot(HInvoke* invoke) {
2277 CreateFPFPToFPCallLocations(arena_, invoke);
2278}
2279
2280void IntrinsicCodeGeneratorARM::VisitMathHypot(HInvoke* invoke) {
2281 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2282}
2283
2284void IntrinsicLocationsBuilderARM::VisitMathNextAfter(HInvoke* invoke) {
2285 CreateFPFPToFPCallLocations(arena_, invoke);
2286}
2287
2288void IntrinsicCodeGeneratorARM::VisitMathNextAfter(HInvoke* invoke) {
2289 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2290}
2291
Artem Serovc257da72016-02-02 13:49:43 +00002292void IntrinsicLocationsBuilderARM::VisitIntegerReverse(HInvoke* invoke) {
2293 CreateIntToIntLocations(arena_, invoke);
2294}
2295
2296void IntrinsicCodeGeneratorARM::VisitIntegerReverse(HInvoke* invoke) {
2297 ArmAssembler* assembler = GetAssembler();
2298 LocationSummary* locations = invoke->GetLocations();
2299
2300 Register out = locations->Out().AsRegister<Register>();
2301 Register in = locations->InAt(0).AsRegister<Register>();
2302
2303 __ rbit(out, in);
2304}
2305
2306void IntrinsicLocationsBuilderARM::VisitLongReverse(HInvoke* invoke) {
2307 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2308 LocationSummary::kNoCall,
2309 kIntrinsified);
2310 locations->SetInAt(0, Location::RequiresRegister());
2311 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2312}
2313
2314void IntrinsicCodeGeneratorARM::VisitLongReverse(HInvoke* invoke) {
2315 ArmAssembler* assembler = GetAssembler();
2316 LocationSummary* locations = invoke->GetLocations();
2317
2318 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2319 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2320 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
2321 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
2322
2323 __ rbit(out_reg_lo, in_reg_hi);
2324 __ rbit(out_reg_hi, in_reg_lo);
2325}
2326
2327void IntrinsicLocationsBuilderARM::VisitIntegerReverseBytes(HInvoke* invoke) {
2328 CreateIntToIntLocations(arena_, invoke);
2329}
2330
2331void IntrinsicCodeGeneratorARM::VisitIntegerReverseBytes(HInvoke* invoke) {
2332 ArmAssembler* assembler = GetAssembler();
2333 LocationSummary* locations = invoke->GetLocations();
2334
2335 Register out = locations->Out().AsRegister<Register>();
2336 Register in = locations->InAt(0).AsRegister<Register>();
2337
2338 __ rev(out, in);
2339}
2340
2341void IntrinsicLocationsBuilderARM::VisitLongReverseBytes(HInvoke* invoke) {
2342 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2343 LocationSummary::kNoCall,
2344 kIntrinsified);
2345 locations->SetInAt(0, Location::RequiresRegister());
2346 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2347}
2348
2349void IntrinsicCodeGeneratorARM::VisitLongReverseBytes(HInvoke* invoke) {
2350 ArmAssembler* assembler = GetAssembler();
2351 LocationSummary* locations = invoke->GetLocations();
2352
2353 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
2354 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
2355 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
2356 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
2357
2358 __ rev(out_reg_lo, in_reg_hi);
2359 __ rev(out_reg_hi, in_reg_lo);
2360}
2361
2362void IntrinsicLocationsBuilderARM::VisitShortReverseBytes(HInvoke* invoke) {
2363 CreateIntToIntLocations(arena_, invoke);
2364}
2365
2366void IntrinsicCodeGeneratorARM::VisitShortReverseBytes(HInvoke* invoke) {
2367 ArmAssembler* assembler = GetAssembler();
2368 LocationSummary* locations = invoke->GetLocations();
2369
2370 Register out = locations->Out().AsRegister<Register>();
2371 Register in = locations->InAt(0).AsRegister<Register>();
2372
2373 __ revsh(out, in);
2374}
2375
xueliang.zhongf1073c82016-07-05 15:28:19 +01002376static void GenBitCount(HInvoke* instr, Primitive::Type type, ArmAssembler* assembler) {
2377 DCHECK(Primitive::IsIntOrLongType(type)) << type;
2378 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
2379 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
2380
2381 bool is_long = type == Primitive::kPrimLong;
2382 LocationSummary* locations = instr->GetLocations();
2383 Location in = locations->InAt(0);
2384 Register src_0 = is_long ? in.AsRegisterPairLow<Register>() : in.AsRegister<Register>();
2385 Register src_1 = is_long ? in.AsRegisterPairHigh<Register>() : src_0;
2386 SRegister tmp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2387 DRegister tmp_d = FromLowSToD(tmp_s);
2388 Register out_r = locations->Out().AsRegister<Register>();
2389
2390 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2391 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2392 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2393 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2394 __ vmovdrr(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2395 __ vcntd(tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2396 __ vpaddld(tmp_d, tmp_d, 8, /* is_unsigned */ true); // Temp DReg |--c|--c|--c|--c|
2397 __ vpaddld(tmp_d, tmp_d, 16, /* is_unsigned */ true); // Temp DReg |------c|------c|
2398 if (is_long) {
2399 __ vpaddld(tmp_d, tmp_d, 32, /* is_unsigned */ true); // Temp DReg |--------------c|
2400 }
2401 __ vmovrs(out_r, tmp_s);
2402}
2403
2404void IntrinsicLocationsBuilderARM::VisitIntegerBitCount(HInvoke* invoke) {
2405 CreateIntToIntLocations(arena_, invoke);
2406 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2407}
2408
2409void IntrinsicCodeGeneratorARM::VisitIntegerBitCount(HInvoke* invoke) {
2410 GenBitCount(invoke, Primitive::kPrimInt, GetAssembler());
2411}
2412
2413void IntrinsicLocationsBuilderARM::VisitLongBitCount(HInvoke* invoke) {
2414 VisitIntegerBitCount(invoke);
2415}
2416
2417void IntrinsicCodeGeneratorARM::VisitLongBitCount(HInvoke* invoke) {
2418 GenBitCount(invoke, Primitive::kPrimLong, GetAssembler());
2419}
2420
Tim Zhang25abd6c2016-01-19 23:39:24 +08002421void IntrinsicLocationsBuilderARM::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2422 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2423 LocationSummary::kNoCall,
2424 kIntrinsified);
2425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetInAt(1, Location::RequiresRegister());
2427 locations->SetInAt(2, Location::RequiresRegister());
2428 locations->SetInAt(3, Location::RequiresRegister());
2429 locations->SetInAt(4, Location::RequiresRegister());
2430
Scott Wakeling3fdab772016-04-25 11:32:37 +01002431 // Temporary registers to store lengths of strings and for calculations.
Tim Zhang25abd6c2016-01-19 23:39:24 +08002432 locations->AddTemp(Location::RequiresRegister());
2433 locations->AddTemp(Location::RequiresRegister());
2434 locations->AddTemp(Location::RequiresRegister());
2435}
2436
2437void IntrinsicCodeGeneratorARM::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2438 ArmAssembler* assembler = GetAssembler();
2439 LocationSummary* locations = invoke->GetLocations();
2440
2441 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2442 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2443 DCHECK_EQ(char_size, 2u);
2444
2445 // Location of data in char array buffer.
2446 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2447
2448 // Location of char array data in string.
2449 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2450
2451 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2452 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2453 Register srcObj = locations->InAt(0).AsRegister<Register>();
2454 Register srcBegin = locations->InAt(1).AsRegister<Register>();
2455 Register srcEnd = locations->InAt(2).AsRegister<Register>();
2456 Register dstObj = locations->InAt(3).AsRegister<Register>();
2457 Register dstBegin = locations->InAt(4).AsRegister<Register>();
2458
Scott Wakeling3fdab772016-04-25 11:32:37 +01002459 Register num_chr = locations->GetTemp(0).AsRegister<Register>();
2460 Register src_ptr = locations->GetTemp(1).AsRegister<Register>();
Tim Zhang25abd6c2016-01-19 23:39:24 +08002461 Register dst_ptr = locations->GetTemp(2).AsRegister<Register>();
Tim Zhang25abd6c2016-01-19 23:39:24 +08002462
jessicahandojo05765752016-09-09 19:01:32 -07002463 Label done, compressed_string_loop;
Tim Zhang25abd6c2016-01-19 23:39:24 +08002464 // dst to be copied.
2465 __ add(dst_ptr, dstObj, ShifterOperand(data_offset));
2466 __ add(dst_ptr, dst_ptr, ShifterOperand(dstBegin, LSL, 1));
2467
Scott Wakeling3fdab772016-04-25 11:32:37 +01002468 __ subs(num_chr, srcEnd, ShifterOperand(srcBegin));
Scott Wakeling3fdab772016-04-25 11:32:37 +01002469 // Early out for valid zero-length retrievals.
Tim Zhang25abd6c2016-01-19 23:39:24 +08002470 __ b(&done, EQ);
Scott Wakeling3fdab772016-04-25 11:32:37 +01002471
jessicahandojo05765752016-09-09 19:01:32 -07002472 // src range to copy.
2473 __ add(src_ptr, srcObj, ShifterOperand(value_offset));
2474 Label compressed_string_preloop;
2475 if (mirror::kUseStringCompression) {
2476 // Location of count in string.
2477 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2478 // String's length.
2479 __ ldr(IP, Address(srcObj, count_offset));
2480 __ cmp(IP, ShifterOperand(0));
2481 __ b(&compressed_string_preloop, LT);
2482 }
2483 __ add(src_ptr, src_ptr, ShifterOperand(srcBegin, LSL, 1));
2484
2485 // Do the copy.
2486 Label loop, remainder;
2487
Scott Wakeling3fdab772016-04-25 11:32:37 +01002488 // Save repairing the value of num_chr on the < 4 character path.
2489 __ subs(IP, num_chr, ShifterOperand(4));
2490 __ b(&remainder, LT);
2491
2492 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2493 __ mov(num_chr, ShifterOperand(IP));
2494
2495 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
2496 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
2497 // to rectify these everywhere this intrinsic applies.)
2498 __ Bind(&loop);
2499 __ ldr(IP, Address(src_ptr, char_size * 2));
2500 __ subs(num_chr, num_chr, ShifterOperand(4));
2501 __ str(IP, Address(dst_ptr, char_size * 2));
2502 __ ldr(IP, Address(src_ptr, char_size * 4, Address::PostIndex));
2503 __ str(IP, Address(dst_ptr, char_size * 4, Address::PostIndex));
2504 __ b(&loop, GE);
2505
2506 __ adds(num_chr, num_chr, ShifterOperand(4));
2507 __ b(&done, EQ);
2508
2509 // Main loop for < 4 character case and remainder handling. Loads and stores one
2510 // 16-bit Java character at a time.
2511 __ Bind(&remainder);
2512 __ ldrh(IP, Address(src_ptr, char_size, Address::PostIndex));
2513 __ subs(num_chr, num_chr, ShifterOperand(1));
2514 __ strh(IP, Address(dst_ptr, char_size, Address::PostIndex));
2515 __ b(&remainder, GT);
jessicahandojo05765752016-09-09 19:01:32 -07002516 __ b(&done);
2517
2518 if (mirror::kUseStringCompression) {
2519 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
2520 DCHECK_EQ(c_char_size, 1u);
2521 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2522 __ Bind(&compressed_string_preloop);
2523 __ add(src_ptr, src_ptr, ShifterOperand(srcBegin));
2524 __ Bind(&compressed_string_loop);
2525 __ ldrb(IP, Address(src_ptr, c_char_size, Address::PostIndex));
2526 __ strh(IP, Address(dst_ptr, char_size, Address::PostIndex));
2527 __ subs(num_chr, num_chr, ShifterOperand(1));
2528 __ b(&compressed_string_loop, GT);
2529 }
Scott Wakeling3fdab772016-04-25 11:32:37 +01002530
Tim Zhang25abd6c2016-01-19 23:39:24 +08002531 __ Bind(&done);
2532}
2533
Anton Kirilova3ffea22016-04-07 17:02:37 +01002534void IntrinsicLocationsBuilderARM::VisitFloatIsInfinite(HInvoke* invoke) {
2535 CreateFPToIntLocations(arena_, invoke);
2536}
2537
2538void IntrinsicCodeGeneratorARM::VisitFloatIsInfinite(HInvoke* invoke) {
2539 ArmAssembler* const assembler = GetAssembler();
2540 LocationSummary* const locations = invoke->GetLocations();
2541 const Register out = locations->Out().AsRegister<Register>();
2542 // Shifting left by 1 bit makes the value encodable as an immediate operand;
2543 // we don't care about the sign bit anyway.
2544 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
2545
2546 __ vmovrs(out, locations->InAt(0).AsFpuRegister<SRegister>());
2547 // We don't care about the sign bit, so shift left.
2548 __ Lsl(out, out, 1);
2549 __ eor(out, out, ShifterOperand(infinity));
2550 // If the result is 0, then it has 32 leading zeros, and less than that otherwise.
2551 __ clz(out, out);
2552 // Any number less than 32 logically shifted right by 5 bits results in 0;
2553 // the same operation on 32 yields 1.
2554 __ Lsr(out, out, 5);
2555}
2556
2557void IntrinsicLocationsBuilderARM::VisitDoubleIsInfinite(HInvoke* invoke) {
2558 CreateFPToIntLocations(arena_, invoke);
2559}
2560
2561void IntrinsicCodeGeneratorARM::VisitDoubleIsInfinite(HInvoke* invoke) {
2562 ArmAssembler* const assembler = GetAssembler();
2563 LocationSummary* const locations = invoke->GetLocations();
2564 const Register out = locations->Out().AsRegister<Register>();
2565 // The highest 32 bits of double precision positive infinity separated into
2566 // two constants encodable as immediate operands.
2567 constexpr uint32_t infinity_high = 0x7f000000U;
2568 constexpr uint32_t infinity_high2 = 0x00f00000U;
2569
2570 static_assert((infinity_high | infinity_high2) == static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
2571 "The constants do not add up to the high 32 bits of double precision positive infinity.");
2572 __ vmovrrd(IP, out, FromLowSToD(locations->InAt(0).AsFpuRegisterPairLow<SRegister>()));
2573 __ eor(out, out, ShifterOperand(infinity_high));
2574 __ eor(out, out, ShifterOperand(infinity_high2));
2575 // We don't care about the sign bit, so shift left.
2576 __ orr(out, IP, ShifterOperand(out, LSL, 1));
2577 // If the result is 0, then it has 32 leading zeros, and less than that otherwise.
2578 __ clz(out, out);
2579 // Any number less than 32 logically shifted right by 5 bits results in 0;
2580 // the same operation on 32 yields 1.
2581 __ Lsr(out, out, 5);
2582}
2583
Aart Bik2f9fcc92016-03-01 15:16:54 -08002584UNIMPLEMENTED_INTRINSIC(ARM, MathMinDoubleDouble)
2585UNIMPLEMENTED_INTRINSIC(ARM, MathMinFloatFloat)
2586UNIMPLEMENTED_INTRINSIC(ARM, MathMaxDoubleDouble)
2587UNIMPLEMENTED_INTRINSIC(ARM, MathMaxFloatFloat)
2588UNIMPLEMENTED_INTRINSIC(ARM, MathMinLongLong)
2589UNIMPLEMENTED_INTRINSIC(ARM, MathMaxLongLong)
2590UNIMPLEMENTED_INTRINSIC(ARM, MathCeil) // Could be done by changing rounding mode, maybe?
2591UNIMPLEMENTED_INTRINSIC(ARM, MathFloor) // Could be done by changing rounding mode, maybe?
2592UNIMPLEMENTED_INTRINSIC(ARM, MathRint)
2593UNIMPLEMENTED_INTRINSIC(ARM, MathRoundDouble) // Could be done by changing rounding mode, maybe?
2594UNIMPLEMENTED_INTRINSIC(ARM, MathRoundFloat) // Could be done by changing rounding mode, maybe?
2595UNIMPLEMENTED_INTRINSIC(ARM, UnsafeCASLong) // High register pressure.
2596UNIMPLEMENTED_INTRINSIC(ARM, SystemArrayCopyChar)
2597UNIMPLEMENTED_INTRINSIC(ARM, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002598UNIMPLEMENTED_INTRINSIC(ARM, IntegerHighestOneBit)
2599UNIMPLEMENTED_INTRINSIC(ARM, LongHighestOneBit)
2600UNIMPLEMENTED_INTRINSIC(ARM, IntegerLowestOneBit)
2601UNIMPLEMENTED_INTRINSIC(ARM, LongLowestOneBit)
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002602
Aart Bik0e54c012016-03-04 12:08:31 -08002603// 1.8.
2604UNIMPLEMENTED_INTRINSIC(ARM, UnsafeGetAndAddInt)
2605UNIMPLEMENTED_INTRINSIC(ARM, UnsafeGetAndAddLong)
2606UNIMPLEMENTED_INTRINSIC(ARM, UnsafeGetAndSetInt)
2607UNIMPLEMENTED_INTRINSIC(ARM, UnsafeGetAndSetLong)
2608UNIMPLEMENTED_INTRINSIC(ARM, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002609
Aart Bik2f9fcc92016-03-01 15:16:54 -08002610UNREACHABLE_INTRINSICS(ARM)
Roland Levillain4d027112015-07-01 15:41:14 +01002611
2612#undef __
2613
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002614} // namespace arm
2615} // namespace art