blob: d2017da221d3a04117b406d7292b98d2c47791dc [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
44bool IntrinsicLocationsBuilderARM::TryDispatch(HInvoke* invoke) {
45 Dispatch(invoke);
46 LocationSummary* res = invoke->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +000047 if (res == nullptr) {
48 return false;
49 }
50 if (kEmitCompilerReadBarrier && res->CanCall()) {
51 // Generating an intrinsic for this HInvoke may produce an
52 // IntrinsicSlowPathARM slow path. Currently this approach
53 // does not work when using read barriers, as the emitted
54 // calling sequence will make use of another slow path
55 // (ReadBarrierForRootSlowPathARM for HInvokeStaticOrDirect,
56 // ReadBarrierSlowPathARM for HInvokeVirtual). So we bail
57 // out in this case.
58 //
59 // TODO: Find a way to have intrinsics work with read barriers.
60 invoke->SetLocations(nullptr);
61 return false;
62 }
63 return res->Intrinsified();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080064}
65
66#define __ assembler->
67
68static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
69 LocationSummary* locations = new (arena) LocationSummary(invoke,
70 LocationSummary::kNoCall,
71 kIntrinsified);
72 locations->SetInAt(0, Location::RequiresFpuRegister());
73 locations->SetOut(Location::RequiresRegister());
74}
75
76static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
77 LocationSummary* locations = new (arena) LocationSummary(invoke,
78 LocationSummary::kNoCall,
79 kIntrinsified);
80 locations->SetInAt(0, Location::RequiresRegister());
81 locations->SetOut(Location::RequiresFpuRegister());
82}
83
84static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
85 Location input = locations->InAt(0);
86 Location output = locations->Out();
87 if (is64bit) {
88 __ vmovrrd(output.AsRegisterPairLow<Register>(),
89 output.AsRegisterPairHigh<Register>(),
90 FromLowSToD(input.AsFpuRegisterPairLow<SRegister>()));
91 } else {
92 __ vmovrs(output.AsRegister<Register>(), input.AsFpuRegister<SRegister>());
93 }
94}
95
96static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
97 Location input = locations->InAt(0);
98 Location output = locations->Out();
99 if (is64bit) {
100 __ vmovdrr(FromLowSToD(output.AsFpuRegisterPairLow<SRegister>()),
101 input.AsRegisterPairLow<Register>(),
102 input.AsRegisterPairHigh<Register>());
103 } else {
104 __ vmovsr(output.AsFpuRegister<SRegister>(), input.AsRegister<Register>());
105 }
106}
107
108void IntrinsicLocationsBuilderARM::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
109 CreateFPToIntLocations(arena_, invoke);
110}
111void IntrinsicLocationsBuilderARM::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
112 CreateIntToFPLocations(arena_, invoke);
113}
114
115void IntrinsicCodeGeneratorARM::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
116 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
117}
118void IntrinsicCodeGeneratorARM::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
119 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
120}
121
122void IntrinsicLocationsBuilderARM::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
123 CreateFPToIntLocations(arena_, invoke);
124}
125void IntrinsicLocationsBuilderARM::VisitFloatIntBitsToFloat(HInvoke* invoke) {
126 CreateIntToFPLocations(arena_, invoke);
127}
128
129void IntrinsicCodeGeneratorARM::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
130 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
131}
132void IntrinsicCodeGeneratorARM::VisitFloatIntBitsToFloat(HInvoke* invoke) {
133 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
134}
135
136static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
137 LocationSummary* locations = new (arena) LocationSummary(invoke,
138 LocationSummary::kNoCall,
139 kIntrinsified);
140 locations->SetInAt(0, Location::RequiresRegister());
141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
142}
143
144static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
145 LocationSummary* locations = new (arena) LocationSummary(invoke,
146 LocationSummary::kNoCall,
147 kIntrinsified);
148 locations->SetInAt(0, Location::RequiresFpuRegister());
149 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
150}
151
Scott Wakeling611d3392015-07-10 11:42:06 +0100152static void GenNumberOfLeadingZeros(LocationSummary* locations,
153 Primitive::Type type,
154 ArmAssembler* assembler) {
155 Location in = locations->InAt(0);
156 Register out = locations->Out().AsRegister<Register>();
157
158 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
159
160 if (type == Primitive::kPrimLong) {
161 Register in_reg_lo = in.AsRegisterPairLow<Register>();
162 Register in_reg_hi = in.AsRegisterPairHigh<Register>();
163 Label end;
164 __ clz(out, in_reg_hi);
165 __ CompareAndBranchIfNonZero(in_reg_hi, &end);
166 __ clz(out, in_reg_lo);
167 __ AddConstant(out, 32);
168 __ Bind(&end);
169 } else {
170 __ clz(out, in.AsRegister<Register>());
171 }
172}
173
174void IntrinsicLocationsBuilderARM::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
175 CreateIntToIntLocations(arena_, invoke);
176}
177
178void IntrinsicCodeGeneratorARM::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
179 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
180}
181
182void IntrinsicLocationsBuilderARM::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
183 LocationSummary* locations = new (arena_) LocationSummary(invoke,
184 LocationSummary::kNoCall,
185 kIntrinsified);
186 locations->SetInAt(0, Location::RequiresRegister());
187 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
188}
189
190void IntrinsicCodeGeneratorARM::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
191 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
192}
193
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100194static void GenNumberOfTrailingZeros(LocationSummary* locations,
195 Primitive::Type type,
196 ArmAssembler* assembler) {
197 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
198
199 Register out = locations->Out().AsRegister<Register>();
200
201 if (type == Primitive::kPrimLong) {
202 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
203 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
204 Label end;
205 __ rbit(out, in_reg_lo);
206 __ clz(out, out);
207 __ CompareAndBranchIfNonZero(in_reg_lo, &end);
208 __ rbit(out, in_reg_hi);
209 __ clz(out, out);
210 __ AddConstant(out, 32);
211 __ Bind(&end);
212 } else {
213 Register in = locations->InAt(0).AsRegister<Register>();
214 __ rbit(out, in);
215 __ clz(out, out);
216 }
217}
218
219void IntrinsicLocationsBuilderARM::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
220 LocationSummary* locations = new (arena_) LocationSummary(invoke,
221 LocationSummary::kNoCall,
222 kIntrinsified);
223 locations->SetInAt(0, Location::RequiresRegister());
224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
225}
226
227void IntrinsicCodeGeneratorARM::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
228 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
229}
230
231void IntrinsicLocationsBuilderARM::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
232 LocationSummary* locations = new (arena_) LocationSummary(invoke,
233 LocationSummary::kNoCall,
234 kIntrinsified);
235 locations->SetInAt(0, Location::RequiresRegister());
236 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
237}
238
239void IntrinsicCodeGeneratorARM::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
240 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
241}
242
243static void GenIntegerRotate(LocationSummary* locations,
244 ArmAssembler* assembler,
245 bool is_left) {
246 Register in = locations->InAt(0).AsRegister<Register>();
247 Location rhs = locations->InAt(1);
248 Register out = locations->Out().AsRegister<Register>();
249
250 if (rhs.IsConstant()) {
251 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
252 // so map all rotations to a +ve. equivalent in that range.
253 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
254 uint32_t rot = rhs.GetConstant()->AsIntConstant()->GetValue() & 0x1F;
255 if (rot) {
256 // Rotate, mapping left rotations to right equivalents if necessary.
257 // (e.g. left by 2 bits == right by 30.)
258 __ Ror(out, in, is_left ? (0x20 - rot) : rot);
259 } else if (out != in) {
260 __ Mov(out, in);
261 }
262 } else {
263 if (is_left) {
264 __ rsb(out, rhs.AsRegister<Register>(), ShifterOperand(0));
265 __ Ror(out, in, out);
266 } else {
267 __ Ror(out, in, rhs.AsRegister<Register>());
268 }
269 }
270}
271
272// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
273// rotates by swapping input regs (effectively rotating by the first 32-bits of
274// a larger rotation) or flipping direction (thus treating larger right/left
275// rotations as sub-word sized rotations in the other direction) as appropriate.
276static void GenLongRotate(LocationSummary* locations,
277 ArmAssembler* assembler,
278 bool is_left) {
279 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
280 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
281 Location rhs = locations->InAt(1);
282 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
283 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
284
285 if (rhs.IsConstant()) {
286 uint32_t rot = rhs.GetConstant()->AsIntConstant()->GetValue();
287 // Map all left rotations to right equivalents.
288 if (is_left) {
289 rot = 0x40 - rot;
290 }
291 // Map all rotations to +ve. equivalents on the interval [0,63].
292 rot &= 0x3F;
293 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
294 // logic below to a simple pair of binary orr.
295 // (e.g. 34 bits == in_reg swap + 2 bits right.)
296 if (rot >= 0x20) {
297 rot -= 0x20;
298 std::swap(in_reg_hi, in_reg_lo);
299 }
300 // Rotate, or mov to out for zero or word size rotations.
301 if (rot) {
302 __ Lsr(out_reg_hi, in_reg_hi, rot);
303 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, 0x20 - rot));
304 __ Lsr(out_reg_lo, in_reg_lo, rot);
305 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, 0x20 - rot));
306 } else {
307 __ Mov(out_reg_lo, in_reg_lo);
308 __ Mov(out_reg_hi, in_reg_hi);
309 }
310 } else {
311 Register shift_left = locations->GetTemp(0).AsRegister<Register>();
312 Register shift_right = locations->GetTemp(1).AsRegister<Register>();
313 Label end;
314 Label right;
315
316 __ and_(shift_left, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
317 __ Lsrs(shift_right, rhs.AsRegister<Register>(), 6);
318 __ rsb(shift_right, shift_left, ShifterOperand(0x20), AL, kCcKeep);
319
320 if (is_left) {
321 __ b(&right, CS);
322 } else {
323 __ b(&right, CC);
324 std::swap(shift_left, shift_right);
325 }
326
327 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
328 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
329 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
330 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
331 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
332 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
333 __ Lsr(shift_left, in_reg_hi, shift_right);
334 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
335 __ b(&end);
336
337 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
338 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
339 __ Bind(&right);
340 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
341 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
342 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
343 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
344 __ Lsl(shift_right, in_reg_hi, shift_left);
345 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
346
347 __ Bind(&end);
348 }
349}
350
351void IntrinsicLocationsBuilderARM::VisitIntegerRotateRight(HInvoke* invoke) {
352 LocationSummary* locations = new (arena_) LocationSummary(invoke,
353 LocationSummary::kNoCall,
354 kIntrinsified);
355 locations->SetInAt(0, Location::RequiresRegister());
356 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
358}
359
360void IntrinsicCodeGeneratorARM::VisitIntegerRotateRight(HInvoke* invoke) {
361 GenIntegerRotate(invoke->GetLocations(), GetAssembler(), false /* is_left */);
362}
363
364void IntrinsicLocationsBuilderARM::VisitLongRotateRight(HInvoke* invoke) {
365 LocationSummary* locations = new (arena_) LocationSummary(invoke,
366 LocationSummary::kNoCall,
367 kIntrinsified);
368 locations->SetInAt(0, Location::RequiresRegister());
369 if (invoke->InputAt(1)->IsConstant()) {
370 locations->SetInAt(1, Location::ConstantLocation(invoke->InputAt(1)->AsConstant()));
371 } else {
372 locations->SetInAt(1, Location::RequiresRegister());
373 locations->AddTemp(Location::RequiresRegister());
374 locations->AddTemp(Location::RequiresRegister());
375 }
376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
377}
378
379void IntrinsicCodeGeneratorARM::VisitLongRotateRight(HInvoke* invoke) {
380 GenLongRotate(invoke->GetLocations(), GetAssembler(), false /* is_left */);
381}
382
383void IntrinsicLocationsBuilderARM::VisitIntegerRotateLeft(HInvoke* invoke) {
384 LocationSummary* locations = new (arena_) LocationSummary(invoke,
385 LocationSummary::kNoCall,
386 kIntrinsified);
387 locations->SetInAt(0, Location::RequiresRegister());
388 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
389 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
390}
391
392void IntrinsicCodeGeneratorARM::VisitIntegerRotateLeft(HInvoke* invoke) {
393 GenIntegerRotate(invoke->GetLocations(), GetAssembler(), true /* is_left */);
394}
395
396void IntrinsicLocationsBuilderARM::VisitLongRotateLeft(HInvoke* invoke) {
397 LocationSummary* locations = new (arena_) LocationSummary(invoke,
398 LocationSummary::kNoCall,
399 kIntrinsified);
400 locations->SetInAt(0, Location::RequiresRegister());
401 if (invoke->InputAt(1)->IsConstant()) {
402 locations->SetInAt(1, Location::ConstantLocation(invoke->InputAt(1)->AsConstant()));
403 } else {
404 locations->SetInAt(1, Location::RequiresRegister());
405 locations->AddTemp(Location::RequiresRegister());
406 locations->AddTemp(Location::RequiresRegister());
407 }
408 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
409}
410
411void IntrinsicCodeGeneratorARM::VisitLongRotateLeft(HInvoke* invoke) {
412 GenLongRotate(invoke->GetLocations(), GetAssembler(), true /* is_left */);
413}
414
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800415static void MathAbsFP(LocationSummary* locations, bool is64bit, ArmAssembler* assembler) {
416 Location in = locations->InAt(0);
417 Location out = locations->Out();
418
419 if (is64bit) {
420 __ vabsd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
421 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
422 } else {
423 __ vabss(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
424 }
425}
426
427void IntrinsicLocationsBuilderARM::VisitMathAbsDouble(HInvoke* invoke) {
428 CreateFPToFPLocations(arena_, invoke);
429}
430
431void IntrinsicCodeGeneratorARM::VisitMathAbsDouble(HInvoke* invoke) {
432 MathAbsFP(invoke->GetLocations(), true, GetAssembler());
433}
434
435void IntrinsicLocationsBuilderARM::VisitMathAbsFloat(HInvoke* invoke) {
436 CreateFPToFPLocations(arena_, invoke);
437}
438
439void IntrinsicCodeGeneratorARM::VisitMathAbsFloat(HInvoke* invoke) {
440 MathAbsFP(invoke->GetLocations(), false, GetAssembler());
441}
442
443static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
444 LocationSummary* locations = new (arena) LocationSummary(invoke,
445 LocationSummary::kNoCall,
446 kIntrinsified);
447 locations->SetInAt(0, Location::RequiresRegister());
448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
449
450 locations->AddTemp(Location::RequiresRegister());
451}
452
453static void GenAbsInteger(LocationSummary* locations,
454 bool is64bit,
455 ArmAssembler* assembler) {
456 Location in = locations->InAt(0);
457 Location output = locations->Out();
458
459 Register mask = locations->GetTemp(0).AsRegister<Register>();
460
461 if (is64bit) {
462 Register in_reg_lo = in.AsRegisterPairLow<Register>();
463 Register in_reg_hi = in.AsRegisterPairHigh<Register>();
464 Register out_reg_lo = output.AsRegisterPairLow<Register>();
465 Register out_reg_hi = output.AsRegisterPairHigh<Register>();
466
467 DCHECK_NE(out_reg_lo, in_reg_hi) << "Diagonal overlap unexpected.";
468
469 __ Asr(mask, in_reg_hi, 31);
470 __ adds(out_reg_lo, in_reg_lo, ShifterOperand(mask));
471 __ adc(out_reg_hi, in_reg_hi, ShifterOperand(mask));
472 __ eor(out_reg_lo, mask, ShifterOperand(out_reg_lo));
473 __ eor(out_reg_hi, mask, ShifterOperand(out_reg_hi));
474 } else {
475 Register in_reg = in.AsRegister<Register>();
476 Register out_reg = output.AsRegister<Register>();
477
478 __ Asr(mask, in_reg, 31);
479 __ add(out_reg, in_reg, ShifterOperand(mask));
480 __ eor(out_reg, mask, ShifterOperand(out_reg));
481 }
482}
483
484void IntrinsicLocationsBuilderARM::VisitMathAbsInt(HInvoke* invoke) {
485 CreateIntToIntPlusTemp(arena_, invoke);
486}
487
488void IntrinsicCodeGeneratorARM::VisitMathAbsInt(HInvoke* invoke) {
489 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
490}
491
492
493void IntrinsicLocationsBuilderARM::VisitMathAbsLong(HInvoke* invoke) {
494 CreateIntToIntPlusTemp(arena_, invoke);
495}
496
497void IntrinsicCodeGeneratorARM::VisitMathAbsLong(HInvoke* invoke) {
498 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
499}
500
501static void GenMinMax(LocationSummary* locations,
502 bool is_min,
503 ArmAssembler* assembler) {
504 Register op1 = locations->InAt(0).AsRegister<Register>();
505 Register op2 = locations->InAt(1).AsRegister<Register>();
506 Register out = locations->Out().AsRegister<Register>();
507
508 __ cmp(op1, ShifterOperand(op2));
509
510 __ it((is_min) ? Condition::LT : Condition::GT, kItElse);
511 __ mov(out, ShifterOperand(op1), is_min ? Condition::LT : Condition::GT);
512 __ mov(out, ShifterOperand(op2), is_min ? Condition::GE : Condition::LE);
513}
514
515static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
516 LocationSummary* locations = new (arena) LocationSummary(invoke,
517 LocationSummary::kNoCall,
518 kIntrinsified);
519 locations->SetInAt(0, Location::RequiresRegister());
520 locations->SetInAt(1, Location::RequiresRegister());
521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
522}
523
524void IntrinsicLocationsBuilderARM::VisitMathMinIntInt(HInvoke* invoke) {
525 CreateIntIntToIntLocations(arena_, invoke);
526}
527
528void IntrinsicCodeGeneratorARM::VisitMathMinIntInt(HInvoke* invoke) {
529 GenMinMax(invoke->GetLocations(), true, GetAssembler());
530}
531
532void IntrinsicLocationsBuilderARM::VisitMathMaxIntInt(HInvoke* invoke) {
533 CreateIntIntToIntLocations(arena_, invoke);
534}
535
536void IntrinsicCodeGeneratorARM::VisitMathMaxIntInt(HInvoke* invoke) {
537 GenMinMax(invoke->GetLocations(), false, GetAssembler());
538}
539
540void IntrinsicLocationsBuilderARM::VisitMathSqrt(HInvoke* invoke) {
541 CreateFPToFPLocations(arena_, invoke);
542}
543
544void IntrinsicCodeGeneratorARM::VisitMathSqrt(HInvoke* invoke) {
545 LocationSummary* locations = invoke->GetLocations();
546 ArmAssembler* assembler = GetAssembler();
547 __ vsqrtd(FromLowSToD(locations->Out().AsFpuRegisterPairLow<SRegister>()),
548 FromLowSToD(locations->InAt(0).AsFpuRegisterPairLow<SRegister>()));
549}
550
551void IntrinsicLocationsBuilderARM::VisitMemoryPeekByte(HInvoke* invoke) {
552 CreateIntToIntLocations(arena_, invoke);
553}
554
555void IntrinsicCodeGeneratorARM::VisitMemoryPeekByte(HInvoke* invoke) {
556 ArmAssembler* assembler = GetAssembler();
557 // Ignore upper 4B of long address.
558 __ ldrsb(invoke->GetLocations()->Out().AsRegister<Register>(),
559 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
560}
561
562void IntrinsicLocationsBuilderARM::VisitMemoryPeekIntNative(HInvoke* invoke) {
563 CreateIntToIntLocations(arena_, invoke);
564}
565
566void IntrinsicCodeGeneratorARM::VisitMemoryPeekIntNative(HInvoke* invoke) {
567 ArmAssembler* assembler = GetAssembler();
568 // Ignore upper 4B of long address.
569 __ ldr(invoke->GetLocations()->Out().AsRegister<Register>(),
570 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
571}
572
573void IntrinsicLocationsBuilderARM::VisitMemoryPeekLongNative(HInvoke* invoke) {
574 CreateIntToIntLocations(arena_, invoke);
575}
576
577void IntrinsicCodeGeneratorARM::VisitMemoryPeekLongNative(HInvoke* invoke) {
578 ArmAssembler* assembler = GetAssembler();
579 // Ignore upper 4B of long address.
580 Register addr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
581 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
582 // exception. So we can't use ldrd as addr may be unaligned.
583 Register lo = invoke->GetLocations()->Out().AsRegisterPairLow<Register>();
584 Register hi = invoke->GetLocations()->Out().AsRegisterPairHigh<Register>();
585 if (addr == lo) {
586 __ ldr(hi, Address(addr, 4));
587 __ ldr(lo, Address(addr, 0));
588 } else {
589 __ ldr(lo, Address(addr, 0));
590 __ ldr(hi, Address(addr, 4));
591 }
592}
593
594void IntrinsicLocationsBuilderARM::VisitMemoryPeekShortNative(HInvoke* invoke) {
595 CreateIntToIntLocations(arena_, invoke);
596}
597
598void IntrinsicCodeGeneratorARM::VisitMemoryPeekShortNative(HInvoke* invoke) {
599 ArmAssembler* assembler = GetAssembler();
600 // Ignore upper 4B of long address.
601 __ ldrsh(invoke->GetLocations()->Out().AsRegister<Register>(),
602 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
603}
604
605static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
606 LocationSummary* locations = new (arena) LocationSummary(invoke,
607 LocationSummary::kNoCall,
608 kIntrinsified);
609 locations->SetInAt(0, Location::RequiresRegister());
610 locations->SetInAt(1, Location::RequiresRegister());
611}
612
613void IntrinsicLocationsBuilderARM::VisitMemoryPokeByte(HInvoke* invoke) {
614 CreateIntIntToVoidLocations(arena_, invoke);
615}
616
617void IntrinsicCodeGeneratorARM::VisitMemoryPokeByte(HInvoke* invoke) {
618 ArmAssembler* assembler = GetAssembler();
619 __ strb(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
620 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
621}
622
623void IntrinsicLocationsBuilderARM::VisitMemoryPokeIntNative(HInvoke* invoke) {
624 CreateIntIntToVoidLocations(arena_, invoke);
625}
626
627void IntrinsicCodeGeneratorARM::VisitMemoryPokeIntNative(HInvoke* invoke) {
628 ArmAssembler* assembler = GetAssembler();
629 __ str(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
630 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
631}
632
633void IntrinsicLocationsBuilderARM::VisitMemoryPokeLongNative(HInvoke* invoke) {
634 CreateIntIntToVoidLocations(arena_, invoke);
635}
636
637void IntrinsicCodeGeneratorARM::VisitMemoryPokeLongNative(HInvoke* invoke) {
638 ArmAssembler* assembler = GetAssembler();
639 // Ignore upper 4B of long address.
640 Register addr = invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>();
641 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
642 // exception. So we can't use ldrd as addr may be unaligned.
643 __ str(invoke->GetLocations()->InAt(1).AsRegisterPairLow<Register>(), Address(addr, 0));
644 __ str(invoke->GetLocations()->InAt(1).AsRegisterPairHigh<Register>(), Address(addr, 4));
645}
646
647void IntrinsicLocationsBuilderARM::VisitMemoryPokeShortNative(HInvoke* invoke) {
648 CreateIntIntToVoidLocations(arena_, invoke);
649}
650
651void IntrinsicCodeGeneratorARM::VisitMemoryPokeShortNative(HInvoke* invoke) {
652 ArmAssembler* assembler = GetAssembler();
653 __ strh(invoke->GetLocations()->InAt(1).AsRegister<Register>(),
654 Address(invoke->GetLocations()->InAt(0).AsRegisterPairLow<Register>()));
655}
656
657void IntrinsicLocationsBuilderARM::VisitThreadCurrentThread(HInvoke* invoke) {
658 LocationSummary* locations = new (arena_) LocationSummary(invoke,
659 LocationSummary::kNoCall,
660 kIntrinsified);
661 locations->SetOut(Location::RequiresRegister());
662}
663
664void IntrinsicCodeGeneratorARM::VisitThreadCurrentThread(HInvoke* invoke) {
665 ArmAssembler* assembler = GetAssembler();
666 __ LoadFromOffset(kLoadWord,
667 invoke->GetLocations()->Out().AsRegister<Register>(),
668 TR,
669 Thread::PeerOffset<kArmPointerSize>().Int32Value());
670}
671
672static void GenUnsafeGet(HInvoke* invoke,
673 Primitive::Type type,
674 bool is_volatile,
675 CodeGeneratorARM* codegen) {
676 LocationSummary* locations = invoke->GetLocations();
677 DCHECK((type == Primitive::kPrimInt) ||
678 (type == Primitive::kPrimLong) ||
679 (type == Primitive::kPrimNot));
680 ArmAssembler* assembler = codegen->GetAssembler();
Roland Levillain3b359c72015-11-17 19:35:12 +0000681 Location base_loc = locations->InAt(1);
682 Register base = base_loc.AsRegister<Register>(); // Object pointer.
683 Location offset_loc = locations->InAt(2);
684 Register offset = offset_loc.AsRegisterPairLow<Register>(); // Long offset, lo part only.
685 Location trg_loc = locations->Out();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800686
687 if (type == Primitive::kPrimLong) {
Roland Levillain3b359c72015-11-17 19:35:12 +0000688 Register trg_lo = trg_loc.AsRegisterPairLow<Register>();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800689 __ add(IP, base, ShifterOperand(offset));
690 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
Roland Levillain3b359c72015-11-17 19:35:12 +0000691 Register trg_hi = trg_loc.AsRegisterPairHigh<Register>();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800692 __ ldrexd(trg_lo, trg_hi, IP);
693 } else {
694 __ ldrd(trg_lo, Address(IP));
695 }
696 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +0000697 Register trg = trg_loc.AsRegister<Register>();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800698 __ ldr(trg, Address(base, offset));
699 }
700
701 if (is_volatile) {
702 __ dmb(ISH);
703 }
Roland Levillain4d027112015-07-01 15:41:14 +0100704
705 if (type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +0000706 codegen->MaybeGenerateReadBarrier(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
Roland Levillain4d027112015-07-01 15:41:14 +0100707 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800708}
709
710static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain3b359c72015-11-17 19:35:12 +0000711 bool can_call = kEmitCompilerReadBarrier &&
712 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
713 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800714 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain3b359c72015-11-17 19:35:12 +0000715 can_call ?
716 LocationSummary::kCallOnSlowPath :
717 LocationSummary::kNoCall,
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800718 kIntrinsified);
719 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
720 locations->SetInAt(1, Location::RequiresRegister());
721 locations->SetInAt(2, Location::RequiresRegister());
722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
723}
724
725void IntrinsicLocationsBuilderARM::VisitUnsafeGet(HInvoke* invoke) {
726 CreateIntIntIntToIntLocations(arena_, invoke);
727}
728void IntrinsicLocationsBuilderARM::VisitUnsafeGetVolatile(HInvoke* invoke) {
729 CreateIntIntIntToIntLocations(arena_, invoke);
730}
731void IntrinsicLocationsBuilderARM::VisitUnsafeGetLong(HInvoke* invoke) {
732 CreateIntIntIntToIntLocations(arena_, invoke);
733}
734void IntrinsicLocationsBuilderARM::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
735 CreateIntIntIntToIntLocations(arena_, invoke);
736}
737void IntrinsicLocationsBuilderARM::VisitUnsafeGetObject(HInvoke* invoke) {
738 CreateIntIntIntToIntLocations(arena_, invoke);
739}
740void IntrinsicLocationsBuilderARM::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
741 CreateIntIntIntToIntLocations(arena_, invoke);
742}
743
744void IntrinsicCodeGeneratorARM::VisitUnsafeGet(HInvoke* invoke) {
745 GenUnsafeGet(invoke, Primitive::kPrimInt, false, codegen_);
746}
747void IntrinsicCodeGeneratorARM::VisitUnsafeGetVolatile(HInvoke* invoke) {
748 GenUnsafeGet(invoke, Primitive::kPrimInt, true, codegen_);
749}
750void IntrinsicCodeGeneratorARM::VisitUnsafeGetLong(HInvoke* invoke) {
751 GenUnsafeGet(invoke, Primitive::kPrimLong, false, codegen_);
752}
753void IntrinsicCodeGeneratorARM::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
754 GenUnsafeGet(invoke, Primitive::kPrimLong, true, codegen_);
755}
756void IntrinsicCodeGeneratorARM::VisitUnsafeGetObject(HInvoke* invoke) {
757 GenUnsafeGet(invoke, Primitive::kPrimNot, false, codegen_);
758}
759void IntrinsicCodeGeneratorARM::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
760 GenUnsafeGet(invoke, Primitive::kPrimNot, true, codegen_);
761}
762
763static void CreateIntIntIntIntToVoid(ArenaAllocator* arena,
764 const ArmInstructionSetFeatures& features,
765 Primitive::Type type,
766 bool is_volatile,
767 HInvoke* invoke) {
768 LocationSummary* locations = new (arena) LocationSummary(invoke,
769 LocationSummary::kNoCall,
770 kIntrinsified);
771 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
772 locations->SetInAt(1, Location::RequiresRegister());
773 locations->SetInAt(2, Location::RequiresRegister());
774 locations->SetInAt(3, Location::RequiresRegister());
775
776 if (type == Primitive::kPrimLong) {
777 // Potentially need temps for ldrexd-strexd loop.
778 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
779 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
780 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
781 }
782 } else if (type == Primitive::kPrimNot) {
783 // Temps for card-marking.
784 locations->AddTemp(Location::RequiresRegister()); // Temp.
785 locations->AddTemp(Location::RequiresRegister()); // Card.
786 }
787}
788
789void IntrinsicLocationsBuilderARM::VisitUnsafePut(HInvoke* invoke) {
790 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, false, invoke);
791}
792void IntrinsicLocationsBuilderARM::VisitUnsafePutOrdered(HInvoke* invoke) {
793 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, false, invoke);
794}
795void IntrinsicLocationsBuilderARM::VisitUnsafePutVolatile(HInvoke* invoke) {
796 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, true, invoke);
797}
798void IntrinsicLocationsBuilderARM::VisitUnsafePutObject(HInvoke* invoke) {
799 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, false, invoke);
800}
801void IntrinsicLocationsBuilderARM::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
802 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, false, invoke);
803}
804void IntrinsicLocationsBuilderARM::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
805 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, true, invoke);
806}
807void IntrinsicLocationsBuilderARM::VisitUnsafePutLong(HInvoke* invoke) {
808 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimLong, false, invoke);
809}
810void IntrinsicLocationsBuilderARM::VisitUnsafePutLongOrdered(HInvoke* invoke) {
811 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimLong, false, invoke);
812}
813void IntrinsicLocationsBuilderARM::VisitUnsafePutLongVolatile(HInvoke* invoke) {
814 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimLong, true, invoke);
815}
816
817static void GenUnsafePut(LocationSummary* locations,
818 Primitive::Type type,
819 bool is_volatile,
820 bool is_ordered,
821 CodeGeneratorARM* codegen) {
822 ArmAssembler* assembler = codegen->GetAssembler();
823
824 Register base = locations->InAt(1).AsRegister<Register>(); // Object pointer.
825 Register offset = locations->InAt(2).AsRegisterPairLow<Register>(); // Long offset, lo part only.
826 Register value;
827
828 if (is_volatile || is_ordered) {
829 __ dmb(ISH);
830 }
831
832 if (type == Primitive::kPrimLong) {
833 Register value_lo = locations->InAt(3).AsRegisterPairLow<Register>();
834 value = value_lo;
835 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
836 Register temp_lo = locations->GetTemp(0).AsRegister<Register>();
837 Register temp_hi = locations->GetTemp(1).AsRegister<Register>();
838 Register value_hi = locations->InAt(3).AsRegisterPairHigh<Register>();
839
840 __ add(IP, base, ShifterOperand(offset));
841 Label loop_head;
842 __ Bind(&loop_head);
843 __ ldrexd(temp_lo, temp_hi, IP);
844 __ strexd(temp_lo, value_lo, value_hi, IP);
845 __ cmp(temp_lo, ShifterOperand(0));
846 __ b(&loop_head, NE);
847 } else {
848 __ add(IP, base, ShifterOperand(offset));
849 __ strd(value_lo, Address(IP));
850 }
851 } else {
Roland Levillain4d027112015-07-01 15:41:14 +0100852 value = locations->InAt(3).AsRegister<Register>();
853 Register source = value;
854 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
855 Register temp = locations->GetTemp(0).AsRegister<Register>();
856 __ Mov(temp, value);
857 __ PoisonHeapReference(temp);
858 source = temp;
859 }
860 __ str(source, Address(base, offset));
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800861 }
862
863 if (is_volatile) {
864 __ dmb(ISH);
865 }
866
867 if (type == Primitive::kPrimNot) {
868 Register temp = locations->GetTemp(0).AsRegister<Register>();
869 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100870 bool value_can_be_null = true; // TODO: Worth finding out this information?
871 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800872 }
873}
874
875void IntrinsicCodeGeneratorARM::VisitUnsafePut(HInvoke* invoke) {
876 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, false, codegen_);
877}
878void IntrinsicCodeGeneratorARM::VisitUnsafePutOrdered(HInvoke* invoke) {
879 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, true, codegen_);
880}
881void IntrinsicCodeGeneratorARM::VisitUnsafePutVolatile(HInvoke* invoke) {
882 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, false, codegen_);
883}
884void IntrinsicCodeGeneratorARM::VisitUnsafePutObject(HInvoke* invoke) {
885 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, false, codegen_);
886}
887void IntrinsicCodeGeneratorARM::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
888 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, true, codegen_);
889}
890void IntrinsicCodeGeneratorARM::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
891 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, false, codegen_);
892}
893void IntrinsicCodeGeneratorARM::VisitUnsafePutLong(HInvoke* invoke) {
894 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, false, codegen_);
895}
896void IntrinsicCodeGeneratorARM::VisitUnsafePutLongOrdered(HInvoke* invoke) {
897 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, true, codegen_);
898}
899void IntrinsicCodeGeneratorARM::VisitUnsafePutLongVolatile(HInvoke* invoke) {
900 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, false, codegen_);
901}
902
903static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena,
904 HInvoke* invoke) {
905 LocationSummary* locations = new (arena) LocationSummary(invoke,
906 LocationSummary::kNoCall,
907 kIntrinsified);
908 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
909 locations->SetInAt(1, Location::RequiresRegister());
910 locations->SetInAt(2, Location::RequiresRegister());
911 locations->SetInAt(3, Location::RequiresRegister());
912 locations->SetInAt(4, Location::RequiresRegister());
913
914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
915
916 locations->AddTemp(Location::RequiresRegister()); // Pointer.
917 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
918 locations->AddTemp(Location::RequiresRegister()); // Temp 2.
919}
920
921static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM* codegen) {
922 DCHECK_NE(type, Primitive::kPrimLong);
923
924 ArmAssembler* assembler = codegen->GetAssembler();
925
926 Register out = locations->Out().AsRegister<Register>(); // Boolean result.
927
928 Register base = locations->InAt(1).AsRegister<Register>(); // Object pointer.
929 Register offset = locations->InAt(2).AsRegisterPairLow<Register>(); // Offset (discard high 4B).
930 Register expected_lo = locations->InAt(3).AsRegister<Register>(); // Expected.
931 Register value_lo = locations->InAt(4).AsRegister<Register>(); // Value.
932
933 Register tmp_ptr = locations->GetTemp(0).AsRegister<Register>(); // Pointer to actual memory.
934 Register tmp_lo = locations->GetTemp(1).AsRegister<Register>(); // Value in memory.
935
936 if (type == Primitive::kPrimNot) {
937 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
938 // object and scan the receiver at the next GC for nothing.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100939 bool value_can_be_null = true; // TODO: Worth finding out this information?
940 codegen->MarkGCCard(tmp_ptr, tmp_lo, base, value_lo, value_can_be_null);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800941 }
942
943 // Prevent reordering with prior memory operations.
944 __ dmb(ISH);
945
946 __ add(tmp_ptr, base, ShifterOperand(offset));
947
Roland Levillain4d027112015-07-01 15:41:14 +0100948 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
949 codegen->GetAssembler()->PoisonHeapReference(expected_lo);
950 codegen->GetAssembler()->PoisonHeapReference(value_lo);
951 }
952
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800953 // do {
954 // tmp = [r_ptr] - expected;
955 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
956 // result = tmp != 0;
957
958 Label loop_head;
959 __ Bind(&loop_head);
960
961 __ ldrex(tmp_lo, tmp_ptr);
Roland Levillain3b359c72015-11-17 19:35:12 +0000962 // TODO: Do we need a read barrier here when `type == Primitive::kPrimNot`?
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800963
964 __ subs(tmp_lo, tmp_lo, ShifterOperand(expected_lo));
965
966 __ it(EQ, ItState::kItT);
967 __ strex(tmp_lo, value_lo, tmp_ptr, EQ);
968 __ cmp(tmp_lo, ShifterOperand(1), EQ);
969
970 __ b(&loop_head, EQ);
971
972 __ dmb(ISH);
973
974 __ rsbs(out, tmp_lo, ShifterOperand(1));
975 __ it(CC);
976 __ mov(out, ShifterOperand(0), CC);
Roland Levillain4d027112015-07-01 15:41:14 +0100977
978 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
979 codegen->GetAssembler()->UnpoisonHeapReference(value_lo);
980 codegen->GetAssembler()->UnpoisonHeapReference(expected_lo);
981 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800982}
983
Andreas Gampeca714582015-04-03 19:41:34 -0700984void IntrinsicLocationsBuilderARM::VisitUnsafeCASInt(HInvoke* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800985 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
986}
Andreas Gampeca714582015-04-03 19:41:34 -0700987void IntrinsicLocationsBuilderARM::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain985ff702015-10-23 13:25:35 +0100988 // The UnsafeCASObject intrinsic does not always work when heap
989 // poisoning is enabled (it breaks run-test 004-UnsafeTest); turn it
990 // off temporarily as a quick fix.
Roland Levillain3b359c72015-11-17 19:35:12 +0000991 //
Roland Levillain985ff702015-10-23 13:25:35 +0100992 // TODO(rpl): Fix it and turn it back on.
Roland Levillain3b359c72015-11-17 19:35:12 +0000993 //
994 // TODO(rpl): Also, we should investigate whether we need a read
995 // barrier in the generated code.
Roland Levillain985ff702015-10-23 13:25:35 +0100996 if (kPoisonHeapReferences) {
997 return;
998 }
999
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001000 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke);
1001}
1002void IntrinsicCodeGeneratorARM::VisitUnsafeCASInt(HInvoke* invoke) {
1003 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1004}
1005void IntrinsicCodeGeneratorARM::VisitUnsafeCASObject(HInvoke* invoke) {
1006 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1007}
1008
1009void IntrinsicLocationsBuilderARM::VisitStringCharAt(HInvoke* invoke) {
1010 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1011 LocationSummary::kCallOnSlowPath,
1012 kIntrinsified);
1013 locations->SetInAt(0, Location::RequiresRegister());
1014 locations->SetInAt(1, Location::RequiresRegister());
1015 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1016
1017 locations->AddTemp(Location::RequiresRegister());
1018 locations->AddTemp(Location::RequiresRegister());
1019}
1020
1021void IntrinsicCodeGeneratorARM::VisitStringCharAt(HInvoke* invoke) {
1022 ArmAssembler* assembler = GetAssembler();
1023 LocationSummary* locations = invoke->GetLocations();
1024
1025 // Location of reference to data array
1026 const MemberOffset value_offset = mirror::String::ValueOffset();
1027 // Location of count
1028 const MemberOffset count_offset = mirror::String::CountOffset();
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001029
1030 Register obj = locations->InAt(0).AsRegister<Register>(); // String object pointer.
1031 Register idx = locations->InAt(1).AsRegister<Register>(); // Index of character.
1032 Register out = locations->Out().AsRegister<Register>(); // Result character.
1033
1034 Register temp = locations->GetTemp(0).AsRegister<Register>();
1035 Register array_temp = locations->GetTemp(1).AsRegister<Register>();
1036
1037 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
1038 // the cost.
1039 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
1040 // we will not optimize the code for constants (which would save a register).
1041
Andreas Gampe85b62f22015-09-09 13:15:38 -07001042 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001043 codegen_->AddSlowPath(slow_path);
1044
1045 __ ldr(temp, Address(obj, count_offset.Int32Value())); // temp = str.length.
1046 codegen_->MaybeRecordImplicitNullCheck(invoke);
1047 __ cmp(idx, ShifterOperand(temp));
1048 __ b(slow_path->GetEntryLabel(), CS);
1049
Jeff Hao848f70a2014-01-15 13:49:50 -08001050 __ add(array_temp, obj, ShifterOperand(value_offset.Int32Value())); // array_temp := str.value.
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001051
1052 // Load the value.
Jeff Hao848f70a2014-01-15 13:49:50 -08001053 __ ldrh(out, Address(array_temp, idx, LSL, 1)); // out := array_temp[idx].
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001054
1055 __ Bind(slow_path->GetExitLabel());
1056}
1057
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001058void IntrinsicLocationsBuilderARM::VisitStringCompareTo(HInvoke* invoke) {
1059 // The inputs plus one temp.
1060 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1061 LocationSummary::kCall,
1062 kIntrinsified);
1063 InvokeRuntimeCallingConvention calling_convention;
1064 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1065 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1066 locations->SetOut(Location::RegisterLocation(R0));
1067}
1068
1069void IntrinsicCodeGeneratorARM::VisitStringCompareTo(HInvoke* invoke) {
1070 ArmAssembler* assembler = GetAssembler();
1071 LocationSummary* locations = invoke->GetLocations();
1072
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001073 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001074 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001075
1076 Register argument = locations->InAt(1).AsRegister<Register>();
1077 __ cmp(argument, ShifterOperand(0));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001078 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001079 codegen_->AddSlowPath(slow_path);
1080 __ b(slow_path->GetEntryLabel(), EQ);
1081
1082 __ LoadFromOffset(
1083 kLoadWord, LR, TR, QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pStringCompareTo).Int32Value());
1084 __ blx(LR);
1085 __ Bind(slow_path->GetExitLabel());
1086}
1087
Agi Csaki289cd552015-08-18 17:10:38 -07001088void IntrinsicLocationsBuilderARM::VisitStringEquals(HInvoke* invoke) {
1089 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1090 LocationSummary::kNoCall,
1091 kIntrinsified);
1092 InvokeRuntimeCallingConvention calling_convention;
1093 locations->SetInAt(0, Location::RequiresRegister());
1094 locations->SetInAt(1, Location::RequiresRegister());
1095 // Temporary registers to store lengths of strings and for calculations.
1096 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1097 locations->AddTemp(Location::RegisterLocation(R0));
1098 locations->AddTemp(Location::RequiresRegister());
1099 locations->AddTemp(Location::RequiresRegister());
1100
1101 locations->SetOut(Location::RequiresRegister());
1102}
1103
1104void IntrinsicCodeGeneratorARM::VisitStringEquals(HInvoke* invoke) {
1105 ArmAssembler* assembler = GetAssembler();
1106 LocationSummary* locations = invoke->GetLocations();
1107
1108 Register str = locations->InAt(0).AsRegister<Register>();
1109 Register arg = locations->InAt(1).AsRegister<Register>();
1110 Register out = locations->Out().AsRegister<Register>();
1111
1112 Register temp = locations->GetTemp(0).AsRegister<Register>();
1113 Register temp1 = locations->GetTemp(1).AsRegister<Register>();
1114 Register temp2 = locations->GetTemp(2).AsRegister<Register>();
1115
1116 Label loop;
1117 Label end;
1118 Label return_true;
1119 Label return_false;
1120
1121 // Get offsets of count, value, and class fields within a string object.
1122 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1123 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1124 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1125
1126 // Note that the null check must have been done earlier.
1127 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1128
1129 // Check if input is null, return false if it is.
1130 __ CompareAndBranchIfZero(arg, &return_false);
1131
1132 // Instanceof check for the argument by comparing class fields.
1133 // All string objects must have the same type since String cannot be subclassed.
1134 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1135 // If the argument is a string object, its class field must be equal to receiver's class field.
1136 __ ldr(temp, Address(str, class_offset));
1137 __ ldr(temp1, Address(arg, class_offset));
1138 __ cmp(temp, ShifterOperand(temp1));
1139 __ b(&return_false, NE);
1140
1141 // Load lengths of this and argument strings.
1142 __ ldr(temp, Address(str, count_offset));
1143 __ ldr(temp1, Address(arg, count_offset));
1144 // Check if lengths are equal, return false if they're not.
1145 __ cmp(temp, ShifterOperand(temp1));
1146 __ b(&return_false, NE);
1147 // Return true if both strings are empty.
1148 __ cbz(temp, &return_true);
1149
1150 // Reference equality check, return true if same reference.
1151 __ cmp(str, ShifterOperand(arg));
1152 __ b(&return_true, EQ);
1153
1154 // Assertions that must hold in order to compare strings 2 characters at a time.
1155 DCHECK_ALIGNED(value_offset, 4);
1156 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1157
Agi Csaki289cd552015-08-18 17:10:38 -07001158 __ LoadImmediate(temp1, value_offset);
Agi Csaki289cd552015-08-18 17:10:38 -07001159
1160 // Loop to compare strings 2 characters at a time starting at the front of the string.
1161 // Ok to do this because strings with an odd length are zero-padded.
1162 __ Bind(&loop);
1163 __ ldr(out, Address(str, temp1));
1164 __ ldr(temp2, Address(arg, temp1));
1165 __ cmp(out, ShifterOperand(temp2));
1166 __ b(&return_false, NE);
1167 __ add(temp1, temp1, ShifterOperand(sizeof(uint32_t)));
Vladimir Markoa63f0d42015-09-01 13:36:35 +01001168 __ subs(temp, temp, ShifterOperand(sizeof(uint32_t) / sizeof(uint16_t)));
1169 __ b(&loop, GT);
Agi Csaki289cd552015-08-18 17:10:38 -07001170
1171 // Return true and exit the function.
1172 // If loop does not result in returning false, we return true.
1173 __ Bind(&return_true);
1174 __ LoadImmediate(out, 1);
1175 __ b(&end);
1176
1177 // Return false and exit the function.
1178 __ Bind(&return_false);
1179 __ LoadImmediate(out, 0);
1180 __ Bind(&end);
1181}
1182
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001183static void GenerateVisitStringIndexOf(HInvoke* invoke,
1184 ArmAssembler* assembler,
1185 CodeGeneratorARM* codegen,
1186 ArenaAllocator* allocator,
1187 bool start_at_zero) {
1188 LocationSummary* locations = invoke->GetLocations();
1189 Register tmp_reg = locations->GetTemp(0).AsRegister<Register>();
1190
1191 // Note that the null check must have been done earlier.
1192 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1193
1194 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1195 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001196 SlowPathCode* slow_path = nullptr;
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001197 if (invoke->InputAt(1)->IsIntConstant()) {
1198 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1199 std::numeric_limits<uint16_t>::max()) {
1200 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1201 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1202 slow_path = new (allocator) IntrinsicSlowPathARM(invoke);
1203 codegen->AddSlowPath(slow_path);
1204 __ b(slow_path->GetEntryLabel());
1205 __ Bind(slow_path->GetExitLabel());
1206 return;
1207 }
1208 } else {
1209 Register char_reg = locations->InAt(1).AsRegister<Register>();
1210 __ LoadImmediate(tmp_reg, std::numeric_limits<uint16_t>::max());
1211 __ cmp(char_reg, ShifterOperand(tmp_reg));
1212 slow_path = new (allocator) IntrinsicSlowPathARM(invoke);
1213 codegen->AddSlowPath(slow_path);
1214 __ b(slow_path->GetEntryLabel(), HI);
1215 }
1216
1217 if (start_at_zero) {
1218 DCHECK_EQ(tmp_reg, R2);
1219 // Start-index = 0.
1220 __ LoadImmediate(tmp_reg, 0);
1221 }
1222
1223 __ LoadFromOffset(kLoadWord, LR, TR,
1224 QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pIndexOf).Int32Value());
1225 __ blx(LR);
1226
1227 if (slow_path != nullptr) {
1228 __ Bind(slow_path->GetExitLabel());
1229 }
1230}
1231
1232void IntrinsicLocationsBuilderARM::VisitStringIndexOf(HInvoke* invoke) {
1233 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1234 LocationSummary::kCall,
1235 kIntrinsified);
1236 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1237 // best to align the inputs accordingly.
1238 InvokeRuntimeCallingConvention calling_convention;
1239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1240 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1241 locations->SetOut(Location::RegisterLocation(R0));
1242
1243 // Need a temp for slow-path codepoint compare, and need to send start-index=0.
1244 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1245}
1246
1247void IntrinsicCodeGeneratorARM::VisitStringIndexOf(HInvoke* invoke) {
1248 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1249}
1250
1251void IntrinsicLocationsBuilderARM::VisitStringIndexOfAfter(HInvoke* invoke) {
1252 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1253 LocationSummary::kCall,
1254 kIntrinsified);
1255 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1256 // best to align the inputs accordingly.
1257 InvokeRuntimeCallingConvention calling_convention;
1258 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1259 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1260 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1261 locations->SetOut(Location::RegisterLocation(R0));
1262
1263 // Need a temp for slow-path codepoint compare.
1264 locations->AddTemp(Location::RequiresRegister());
1265}
1266
1267void IntrinsicCodeGeneratorARM::VisitStringIndexOfAfter(HInvoke* invoke) {
1268 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1269}
1270
Jeff Hao848f70a2014-01-15 13:49:50 -08001271void IntrinsicLocationsBuilderARM::VisitStringNewStringFromBytes(HInvoke* invoke) {
1272 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1273 LocationSummary::kCall,
1274 kIntrinsified);
1275 InvokeRuntimeCallingConvention calling_convention;
1276 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1277 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1278 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1279 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1280 locations->SetOut(Location::RegisterLocation(R0));
1281}
1282
1283void IntrinsicCodeGeneratorARM::VisitStringNewStringFromBytes(HInvoke* invoke) {
1284 ArmAssembler* assembler = GetAssembler();
1285 LocationSummary* locations = invoke->GetLocations();
1286
1287 Register byte_array = locations->InAt(0).AsRegister<Register>();
1288 __ cmp(byte_array, ShifterOperand(0));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001289 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001290 codegen_->AddSlowPath(slow_path);
1291 __ b(slow_path->GetEntryLabel(), EQ);
1292
1293 __ LoadFromOffset(
1294 kLoadWord, LR, TR, QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pAllocStringFromBytes).Int32Value());
1295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1296 __ blx(LR);
1297 __ Bind(slow_path->GetExitLabel());
1298}
1299
1300void IntrinsicLocationsBuilderARM::VisitStringNewStringFromChars(HInvoke* invoke) {
1301 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1302 LocationSummary::kCall,
1303 kIntrinsified);
1304 InvokeRuntimeCallingConvention calling_convention;
1305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1306 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1307 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1308 locations->SetOut(Location::RegisterLocation(R0));
1309}
1310
1311void IntrinsicCodeGeneratorARM::VisitStringNewStringFromChars(HInvoke* invoke) {
1312 ArmAssembler* assembler = GetAssembler();
1313
1314 __ LoadFromOffset(
1315 kLoadWord, LR, TR, QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pAllocStringFromChars).Int32Value());
1316 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1317 __ blx(LR);
1318}
1319
1320void IntrinsicLocationsBuilderARM::VisitStringNewStringFromString(HInvoke* invoke) {
1321 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1322 LocationSummary::kCall,
1323 kIntrinsified);
1324 InvokeRuntimeCallingConvention calling_convention;
1325 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1326 locations->SetOut(Location::RegisterLocation(R0));
1327}
1328
1329void IntrinsicCodeGeneratorARM::VisitStringNewStringFromString(HInvoke* invoke) {
1330 ArmAssembler* assembler = GetAssembler();
1331 LocationSummary* locations = invoke->GetLocations();
1332
1333 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1334 __ cmp(string_to_copy, ShifterOperand(0));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001335 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001336 codegen_->AddSlowPath(slow_path);
1337 __ b(slow_path->GetEntryLabel(), EQ);
1338
1339 __ LoadFromOffset(kLoadWord,
1340 LR, TR, QUICK_ENTRYPOINT_OFFSET(kArmWordSize, pAllocStringFromString).Int32Value());
1341 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1342 __ blx(LR);
1343 __ Bind(slow_path->GetExitLabel());
1344}
1345
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001346void IntrinsicLocationsBuilderARM::VisitSystemArrayCopy(HInvoke* invoke) {
1347 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
1348 LocationSummary* locations = invoke->GetLocations();
1349 if (locations == nullptr) {
1350 return;
1351 }
1352
1353 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1354 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1355 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1356
1357 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
1358 locations->SetInAt(1, Location::RequiresRegister());
1359 }
1360 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
1361 locations->SetInAt(3, Location::RequiresRegister());
1362 }
1363 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
1364 locations->SetInAt(4, Location::RequiresRegister());
1365 }
1366}
1367
1368static void CheckPosition(ArmAssembler* assembler,
1369 Location pos,
1370 Register input,
1371 Location length,
1372 SlowPathCode* slow_path,
1373 Register input_len,
1374 Register temp,
1375 bool length_is_input_length = false) {
1376 // Where is the length in the Array?
1377 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1378
1379 if (pos.IsConstant()) {
1380 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1381 if (pos_const == 0) {
1382 if (!length_is_input_length) {
1383 // Check that length(input) >= length.
1384 __ LoadFromOffset(kLoadWord, temp, input, length_offset);
1385 if (length.IsConstant()) {
1386 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1387 } else {
1388 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1389 }
1390 __ b(slow_path->GetEntryLabel(), LT);
1391 }
1392 } else {
1393 // Check that length(input) >= pos.
1394 __ LoadFromOffset(kLoadWord, input_len, input, length_offset);
1395 __ subs(temp, input_len, ShifterOperand(pos_const));
1396 __ b(slow_path->GetEntryLabel(), LT);
1397
1398 // Check that (length(input) - pos) >= length.
1399 if (length.IsConstant()) {
1400 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1401 } else {
1402 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1403 }
1404 __ b(slow_path->GetEntryLabel(), LT);
1405 }
1406 } else if (length_is_input_length) {
1407 // The only way the copy can succeed is if pos is zero.
1408 Register pos_reg = pos.AsRegister<Register>();
1409 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
1410 } else {
1411 // Check that pos >= 0.
1412 Register pos_reg = pos.AsRegister<Register>();
1413 __ cmp(pos_reg, ShifterOperand(0));
1414 __ b(slow_path->GetEntryLabel(), LT);
1415
1416 // Check that pos <= length(input).
1417 __ LoadFromOffset(kLoadWord, temp, input, length_offset);
1418 __ subs(temp, temp, ShifterOperand(pos_reg));
1419 __ b(slow_path->GetEntryLabel(), LT);
1420
1421 // Check that (length(input) - pos) >= length.
1422 if (length.IsConstant()) {
1423 __ cmp(temp, ShifterOperand(length.GetConstant()->AsIntConstant()->GetValue()));
1424 } else {
1425 __ cmp(temp, ShifterOperand(length.AsRegister<Register>()));
1426 }
1427 __ b(slow_path->GetEntryLabel(), LT);
1428 }
1429}
1430
Roland Levillain3b359c72015-11-17 19:35:12 +00001431// TODO: Implement read barriers in the SystemArrayCopy intrinsic.
1432// Note that this code path is not used (yet) because we do not
1433// intrinsify methods that can go into the IntrinsicSlowPathARM
1434// slow path.
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001435void IntrinsicCodeGeneratorARM::VisitSystemArrayCopy(HInvoke* invoke) {
1436 ArmAssembler* assembler = GetAssembler();
1437 LocationSummary* locations = invoke->GetLocations();
1438
1439 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1440 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1441 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1442 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1443
1444 Register src = locations->InAt(0).AsRegister<Register>();
1445 Location src_pos = locations->InAt(1);
1446 Register dest = locations->InAt(2).AsRegister<Register>();
1447 Location dest_pos = locations->InAt(3);
1448 Location length = locations->InAt(4);
1449 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
1450 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
1451 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
1452
1453 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathARM(invoke);
1454 codegen_->AddSlowPath(slow_path);
1455
1456 Label ok;
1457 SystemArrayCopyOptimizations optimizations(invoke);
1458
1459 if (!optimizations.GetDestinationIsSource()) {
1460 if (!src_pos.IsConstant() || !dest_pos.IsConstant()) {
1461 __ cmp(src, ShifterOperand(dest));
1462 }
1463 }
1464
1465 // If source and destination are the same, we go to slow path if we need to do
1466 // forward copying.
1467 if (src_pos.IsConstant()) {
1468 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1469 if (dest_pos.IsConstant()) {
1470 // Checked when building locations.
1471 DCHECK(!optimizations.GetDestinationIsSource()
1472 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1473 } else {
1474 if (!optimizations.GetDestinationIsSource()) {
1475 __ b(&ok, NE);
1476 }
1477 __ cmp(dest_pos.AsRegister<Register>(), ShifterOperand(src_pos_constant));
1478 __ b(slow_path->GetEntryLabel(), GT);
1479 }
1480 } else {
1481 if (!optimizations.GetDestinationIsSource()) {
1482 __ b(&ok, NE);
1483 }
1484 if (dest_pos.IsConstant()) {
1485 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1486 __ cmp(src_pos.AsRegister<Register>(), ShifterOperand(dest_pos_constant));
1487 } else {
1488 __ cmp(src_pos.AsRegister<Register>(), ShifterOperand(dest_pos.AsRegister<Register>()));
1489 }
1490 __ b(slow_path->GetEntryLabel(), LT);
1491 }
1492
1493 __ Bind(&ok);
1494
1495 if (!optimizations.GetSourceIsNotNull()) {
1496 // Bail out if the source is null.
1497 __ CompareAndBranchIfZero(src, slow_path->GetEntryLabel());
1498 }
1499
1500 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1501 // Bail out if the destination is null.
1502 __ CompareAndBranchIfZero(dest, slow_path->GetEntryLabel());
1503 }
1504
1505 // If the length is negative, bail out.
1506 // We have already checked in the LocationsBuilder for the constant case.
1507 if (!length.IsConstant() &&
1508 !optimizations.GetCountIsSourceLength() &&
1509 !optimizations.GetCountIsDestinationLength()) {
1510 __ cmp(length.AsRegister<Register>(), ShifterOperand(0));
1511 __ b(slow_path->GetEntryLabel(), LT);
1512 }
1513
1514 // Validity checks: source.
1515 CheckPosition(assembler,
1516 src_pos,
1517 src,
1518 length,
1519 slow_path,
1520 temp1,
1521 temp2,
1522 optimizations.GetCountIsSourceLength());
1523
1524 // Validity checks: dest.
1525 CheckPosition(assembler,
1526 dest_pos,
1527 dest,
1528 length,
1529 slow_path,
1530 temp1,
1531 temp2,
1532 optimizations.GetCountIsDestinationLength());
1533
1534 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1535 // Check whether all elements of the source array are assignable to the component
1536 // type of the destination array. We do two checks: the classes are the same,
1537 // or the destination is Object[]. If none of these checks succeed, we go to the
1538 // slow path.
1539 __ LoadFromOffset(kLoadWord, temp1, dest, class_offset);
1540 __ LoadFromOffset(kLoadWord, temp2, src, class_offset);
1541 bool did_unpoison = false;
1542 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1543 !optimizations.GetSourceIsNonPrimitiveArray()) {
1544 // One or two of the references need to be unpoisoned. Unpoisoned them
1545 // both to make the identity check valid.
1546 __ MaybeUnpoisonHeapReference(temp1);
1547 __ MaybeUnpoisonHeapReference(temp2);
1548 did_unpoison = true;
1549 }
1550
1551 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1552 // Bail out if the destination is not a non primitive array.
1553 __ LoadFromOffset(kLoadWord, temp3, temp1, component_offset);
1554 __ CompareAndBranchIfZero(temp3, slow_path->GetEntryLabel());
1555 __ MaybeUnpoisonHeapReference(temp3);
1556 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1557 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1558 __ CompareAndBranchIfNonZero(temp3, slow_path->GetEntryLabel());
1559 }
1560
1561 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1562 // Bail out if the source is not a non primitive array.
1563 // Bail out if the destination is not a non primitive array.
1564 __ LoadFromOffset(kLoadWord, temp3, temp2, component_offset);
1565 __ CompareAndBranchIfZero(temp3, slow_path->GetEntryLabel());
1566 __ MaybeUnpoisonHeapReference(temp3);
1567 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1568 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1569 __ CompareAndBranchIfNonZero(temp3, slow_path->GetEntryLabel());
1570 }
1571
1572 __ cmp(temp1, ShifterOperand(temp2));
1573
1574 if (optimizations.GetDestinationIsTypedObjectArray()) {
1575 Label do_copy;
1576 __ b(&do_copy, EQ);
1577 if (!did_unpoison) {
1578 __ MaybeUnpoisonHeapReference(temp1);
1579 }
1580 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
1581 __ MaybeUnpoisonHeapReference(temp1);
1582 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
1583 // No need to unpoison the result, we're comparing against null.
1584 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
1585 __ Bind(&do_copy);
1586 } else {
1587 __ b(slow_path->GetEntryLabel(), NE);
1588 }
1589 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1590 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1591 // Bail out if the source is not a non primitive array.
1592 __ LoadFromOffset(kLoadWord, temp1, src, class_offset);
1593 __ MaybeUnpoisonHeapReference(temp1);
1594 __ LoadFromOffset(kLoadWord, temp3, temp1, component_offset);
1595 __ CompareAndBranchIfZero(temp3, slow_path->GetEntryLabel());
1596 __ MaybeUnpoisonHeapReference(temp3);
1597 __ LoadFromOffset(kLoadUnsignedHalfword, temp3, temp3, primitive_offset);
1598 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
1599 __ CompareAndBranchIfNonZero(temp3, slow_path->GetEntryLabel());
1600 }
1601
1602 // Compute base source address, base destination address, and end source address.
1603
1604 uint32_t element_size = sizeof(int32_t);
1605 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1606 if (src_pos.IsConstant()) {
1607 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1608 __ AddConstant(temp1, src, element_size * constant + offset);
1609 } else {
1610 __ add(temp1, src, ShifterOperand(src_pos.AsRegister<Register>(), LSL, 2));
1611 __ AddConstant(temp1, offset);
1612 }
1613
1614 if (dest_pos.IsConstant()) {
1615 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1616 __ AddConstant(temp2, dest, element_size * constant + offset);
1617 } else {
1618 __ add(temp2, dest, ShifterOperand(dest_pos.AsRegister<Register>(), LSL, 2));
1619 __ AddConstant(temp2, offset);
1620 }
1621
1622 if (length.IsConstant()) {
1623 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1624 __ AddConstant(temp3, temp1, element_size * constant);
1625 } else {
1626 __ add(temp3, temp1, ShifterOperand(length.AsRegister<Register>(), LSL, 2));
1627 }
1628
1629 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1630 // poison/unpoison, nor do any read barrier as the next uses of the destination
1631 // array will do it.
1632 Label loop, done;
1633 __ cmp(temp1, ShifterOperand(temp3));
1634 __ b(&done, EQ);
1635 __ Bind(&loop);
1636 __ ldr(IP, Address(temp1, element_size, Address::PostIndex));
1637 __ str(IP, Address(temp2, element_size, Address::PostIndex));
1638 __ cmp(temp1, ShifterOperand(temp3));
1639 __ b(&loop, NE);
1640 __ Bind(&done);
1641
1642 // We only need one card marking on the destination array.
1643 codegen_->MarkGCCard(temp1,
1644 temp2,
1645 dest,
1646 Register(kNoRegister),
1647 false);
1648
1649 __ Bind(slow_path->GetExitLabel());
1650}
1651
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001652// Unimplemented intrinsics.
1653
1654#define UNIMPLEMENTED_INTRINSIC(Name) \
1655void IntrinsicLocationsBuilderARM::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1656} \
1657void IntrinsicCodeGeneratorARM::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1658}
1659
1660UNIMPLEMENTED_INTRINSIC(IntegerReverse)
1661UNIMPLEMENTED_INTRINSIC(IntegerReverseBytes)
1662UNIMPLEMENTED_INTRINSIC(LongReverse)
1663UNIMPLEMENTED_INTRINSIC(LongReverseBytes)
1664UNIMPLEMENTED_INTRINSIC(ShortReverseBytes)
1665UNIMPLEMENTED_INTRINSIC(MathMinDoubleDouble)
1666UNIMPLEMENTED_INTRINSIC(MathMinFloatFloat)
1667UNIMPLEMENTED_INTRINSIC(MathMaxDoubleDouble)
1668UNIMPLEMENTED_INTRINSIC(MathMaxFloatFloat)
1669UNIMPLEMENTED_INTRINSIC(MathMinLongLong)
1670UNIMPLEMENTED_INTRINSIC(MathMaxLongLong)
1671UNIMPLEMENTED_INTRINSIC(MathCeil) // Could be done by changing rounding mode, maybe?
1672UNIMPLEMENTED_INTRINSIC(MathFloor) // Could be done by changing rounding mode, maybe?
1673UNIMPLEMENTED_INTRINSIC(MathRint)
1674UNIMPLEMENTED_INTRINSIC(MathRoundDouble) // Could be done by changing rounding mode, maybe?
1675UNIMPLEMENTED_INTRINSIC(MathRoundFloat) // Could be done by changing rounding mode, maybe?
1676UNIMPLEMENTED_INTRINSIC(UnsafeCASLong) // High register pressure.
1677UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001678UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Jeff Hao848f70a2014-01-15 13:49:50 -08001679UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001680
Roland Levillain4d027112015-07-01 15:41:14 +01001681#undef UNIMPLEMENTED_INTRINSIC
1682
1683#undef __
1684
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001685} // namespace arm
1686} // namespace art