1 /*
2 * Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4 * Copyright 2026 Arm Limited and/or its affiliates.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "asm/macroAssembler.inline.hpp"
28 #include "asm/assembler.hpp"
29 #include "c1/c1_CodeStubs.hpp"
30 #include "c1/c1_Compilation.hpp"
31 #include "c1/c1_LIRAssembler.hpp"
32 #include "c1/c1_MacroAssembler.hpp"
33 #include "c1/c1_Runtime1.hpp"
34 #include "c1/c1_ValueStack.hpp"
35 #include "ci/ciArrayKlass.hpp"
36 #include "ci/ciInlineKlass.hpp"
37 #include "ci/ciInstance.hpp"
38 #include "ci/ciObjArrayKlass.hpp"
39 #include "code/aotCodeCache.hpp"
40 #include "code/compiledIC.hpp"
41 #include "gc/shared/collectedHeap.hpp"
42 #include "gc/shared/gc_globals.hpp"
43 #include "nativeInst_aarch64.hpp"
44 #include "oops/objArrayKlass.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "runtime/frame.inline.hpp"
47 #include "runtime/sharedRuntime.hpp"
48 #include "runtime/stubRoutines.hpp"
49 #include "runtime/threadIdentifier.hpp"
50 #include "utilities/powerOfTwo.hpp"
51 #include "vmreg_aarch64.inline.hpp"
52
53
54 #ifndef PRODUCT
55 #define COMMENT(x) do { __ block_comment(x); } while (0)
56 #else
57 #define COMMENT(x)
58 #endif
59
60 NEEDS_CLEANUP // remove this definitions ?
61 const Register SYNC_header = r0; // synchronization header
62 const Register SHIFT_count = r0; // where count for shift operations must be
63
64 #define __ _masm->
65
66
67 static void select_different_registers(Register preserve,
68 Register extra,
69 Register &tmp1,
70 Register &tmp2,
71 Register &tmp3) {
72 if (tmp1 == preserve) {
73 assert_different_registers(tmp1, tmp2, tmp3, extra);
74 tmp1 = extra;
75 } else if (tmp2 == preserve) {
76 assert_different_registers(tmp1, tmp2, tmp3, extra);
77 tmp2 = extra;
78 } else if (tmp3 == preserve) {
79 assert_different_registers(tmp1, tmp2, tmp3, extra);
80 tmp3 = extra;
81 }
82 assert_different_registers(preserve, tmp1, tmp2, tmp3);
83 }
84
85
86 bool LIR_Assembler::is_small_constant(LIR_Opr opr) { Unimplemented(); return false; }
87
88
89 LIR_Opr LIR_Assembler::receiverOpr() {
90 return FrameMap::receiver_opr;
91 }
92
93 LIR_Opr LIR_Assembler::osrBufferPointer() {
94 return FrameMap::as_pointer_opr(receiverOpr()->as_register());
95 }
96
97 //--------------fpu register translations-----------------------
98
99
100 address LIR_Assembler::float_constant(float f) {
101 address const_addr = __ float_constant(f);
102 if (const_addr == nullptr) {
103 bailout("const section overflow");
104 return __ code()->consts()->start();
105 } else {
106 return const_addr;
107 }
108 }
109
110
111 address LIR_Assembler::double_constant(double d) {
112 address const_addr = __ double_constant(d);
113 if (const_addr == nullptr) {
114 bailout("const section overflow");
115 return __ code()->consts()->start();
116 } else {
117 return const_addr;
118 }
119 }
120
121 address LIR_Assembler::int_constant(jlong n) {
122 address const_addr = __ long_constant(n);
123 if (const_addr == nullptr) {
124 bailout("const section overflow");
125 return __ code()->consts()->start();
126 } else {
127 return const_addr;
128 }
129 }
130
131 void LIR_Assembler::breakpoint() { Unimplemented(); }
132
133 void LIR_Assembler::push(LIR_Opr opr) { Unimplemented(); }
134
135 void LIR_Assembler::pop(LIR_Opr opr) { Unimplemented(); }
136
137 bool LIR_Assembler::is_literal_address(LIR_Address* addr) { Unimplemented(); return false; }
138 //-------------------------------------------
139
140 static Register as_reg(LIR_Opr op) {
141 return op->is_double_cpu() ? op->as_register_lo() : op->as_register();
142 }
143
144 static jlong as_long(LIR_Opr data) {
145 jlong result;
146 switch (data->type()) {
147 case T_INT:
148 result = (data->as_jint());
149 break;
150 case T_LONG:
151 result = (data->as_jlong());
152 break;
153 default:
154 ShouldNotReachHere();
155 result = 0; // unreachable
156 }
157 return result;
158 }
159
160 Address LIR_Assembler::as_Address(LIR_Address* addr, Register tmp) {
161 Register base = addr->base()->as_pointer_register();
162 LIR_Opr opr = addr->index();
163 if (opr->is_cpu_register()) {
164 Register index;
165 if (opr->is_single_cpu())
166 index = opr->as_register();
167 else
168 index = opr->as_register_lo();
169 assert(addr->disp() == 0, "must be");
170 switch(opr->type()) {
171 case T_INT:
172 return Address(base, index, Address::sxtw(addr->scale()));
173 case T_LONG:
174 return Address(base, index, Address::lsl(addr->scale()));
175 default:
176 ShouldNotReachHere();
177 }
178 } else {
179 assert(addr->scale() == 0,
180 "expected for immediate operand, was: %d", addr->scale());
181 ptrdiff_t offset = ptrdiff_t(addr->disp());
182 // NOTE: Does not handle any 16 byte vector access.
183 const uint type_size = type2aelembytes(addr->type(), true);
184 return __ legitimize_address(Address(base, offset), type_size, tmp);
185 }
186 return Address();
187 }
188
189 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
190 ShouldNotReachHere();
191 return Address();
192 }
193
194 Address LIR_Assembler::as_Address(LIR_Address* addr) {
195 return as_Address(addr, rscratch1);
196 }
197
198 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
199 return as_Address(addr, rscratch1); // Ouch
200 // FIXME: This needs to be much more clever. See x86.
201 }
202
203 // Ensure a valid Address (base + offset) to a stack-slot. If stack access is
204 // not encodable as a base + (immediate) offset, generate an explicit address
205 // calculation to hold the address in a temporary register.
206 Address LIR_Assembler::stack_slot_address(int index, uint size, Register tmp, int adjust) {
207 precond(size == 4 || size == 8);
208 Address addr = frame_map()->address_for_slot(index, adjust);
209 precond(addr.getMode() == Address::base_plus_offset);
210 precond(addr.base() == sp);
211 precond(addr.offset() > 0);
212 uint mask = size - 1;
213 assert((addr.offset() & mask) == 0, "scaled offsets only");
214 return __ legitimize_address(addr, size, tmp);
215 }
216
217 void LIR_Assembler::osr_entry() {
218 offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
219 BlockBegin* osr_entry = compilation()->hir()->osr_entry();
220 ValueStack* entry_state = osr_entry->state();
221 int number_of_locks = entry_state->locks_size();
222
223 // we jump here if osr happens with the interpreter
224 // state set up to continue at the beginning of the
225 // loop that triggered osr - in particular, we have
226 // the following registers setup:
227 //
228 // r2: osr buffer
229 //
230
231 // build frame
232 ciMethod* m = compilation()->method();
233 __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
234
235 // OSR buffer is
236 //
237 // locals[nlocals-1..0]
238 // monitors[0..number_of_locks]
239 //
240 // locals is a direct copy of the interpreter frame so in the osr buffer
241 // so first slot in the local array is the last local from the interpreter
242 // and last slot is local[0] (receiver) from the interpreter
243 //
244 // Similarly with locks. The first lock slot in the osr buffer is the nth lock
245 // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
246 // in the interpreter frame (the method lock if a sync method)
247
248 // Initialize monitors in the compiled activation.
249 // r2: pointer to osr buffer
250 //
251 // All other registers are dead at this point and the locals will be
252 // copied into place by code emitted in the IR.
253
254 Register OSR_buf = osrBufferPointer()->as_pointer_register();
255 { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
256 int monitor_offset = BytesPerWord * method()->max_locals() +
257 (2 * BytesPerWord) * (number_of_locks - 1);
258 // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
259 // the OSR buffer using 2 word entries: first the lock and then
260 // the oop.
261 for (int i = 0; i < number_of_locks; i++) {
262 int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
263 #ifdef ASSERT
264 // verify the interpreter's monitor has a non-null object
265 {
266 Label L;
267 __ ldr(rscratch1, __ form_address(rscratch1, OSR_buf, slot_offset + 1*BytesPerWord, 0));
268 __ cbnz(rscratch1, L);
269 __ stop("locked object is null");
270 __ bind(L);
271 }
272 #endif
273 __ ldr(r19, __ form_address(rscratch1, OSR_buf, slot_offset, 0));
274 __ ldr(r20, __ form_address(rscratch1, OSR_buf, slot_offset + BytesPerWord, 0));
275 __ str(r19, frame_map()->address_for_monitor_lock(i));
276 __ str(r20, frame_map()->address_for_monitor_object(i));
277 }
278 }
279 }
280
281
282 // inline cache check; done before the frame is built.
283 int LIR_Assembler::check_icache() {
284 return __ ic_check(CodeEntryAlignment);
285 }
286
287 void LIR_Assembler::clinit_barrier(ciMethod* method) {
288 assert(VM_Version::supports_fast_class_init_checks(), "sanity");
289 assert(!method->holder()->is_not_initialized(), "initialization should have been started");
290
291 Label L_skip_barrier;
292
293 __ mov_metadata(rscratch2, method->holder()->constant_encoding());
294 __ clinit_barrier(rscratch2, rscratch1, &L_skip_barrier /*L_fast_path*/);
295 __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
296 __ bind(L_skip_barrier);
297 }
298
299 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
300 if (o == nullptr) {
301 __ mov(reg, zr);
302 } else {
303 __ movoop(reg, o);
304 }
305 }
306
307 void LIR_Assembler::deoptimize_trap(CodeEmitInfo *info) {
308 address target = nullptr;
309 relocInfo::relocType reloc_type = relocInfo::none;
310
311 switch (patching_id(info)) {
312 case PatchingStub::access_field_id:
313 target = Runtime1::entry_for(StubId::c1_access_field_patching_id);
314 reloc_type = relocInfo::section_word_type;
315 break;
316 case PatchingStub::load_klass_id:
317 target = Runtime1::entry_for(StubId::c1_load_klass_patching_id);
318 reloc_type = relocInfo::metadata_type;
319 break;
320 case PatchingStub::load_mirror_id:
321 target = Runtime1::entry_for(StubId::c1_load_mirror_patching_id);
322 reloc_type = relocInfo::oop_type;
323 break;
324 case PatchingStub::load_appendix_id:
325 target = Runtime1::entry_for(StubId::c1_load_appendix_patching_id);
326 reloc_type = relocInfo::oop_type;
327 break;
328 default: ShouldNotReachHere();
329 }
330
331 __ far_call(RuntimeAddress(target));
332 add_call_info_here(info);
333 }
334
335 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
336 deoptimize_trap(info);
337 }
338
339
340 // This specifies the rsp decrement needed to build the frame
341 int LIR_Assembler::initial_frame_size_in_bytes() const {
342 // if rounding, must let FrameMap know!
343
344 return in_bytes(frame_map()->framesize_in_bytes());
345 }
346
347
348 int LIR_Assembler::emit_exception_handler() {
349 // generate code for exception handler
350 address handler_base = __ start_a_stub(exception_handler_size());
351 if (handler_base == nullptr) {
352 // not enough space left for the handler
353 bailout("exception handler overflow");
354 return -1;
355 }
356
357 int offset = code_offset();
358
359 // the exception oop and pc are in r0, and r3
360 // no other registers need to be preserved, so invalidate them
361 __ invalidate_registers(false, true, true, false, true, true);
362
363 // check that there is really an exception
364 __ verify_not_null_oop(r0);
365
366 // search an exception handler (r0: exception oop, r3: throwing pc)
367 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_handle_exception_from_callee_id)));
368 __ should_not_reach_here();
369 guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
370 __ end_a_stub();
371
372 return offset;
373 }
374
375
376 // Emit the code to remove the frame from the stack in the exception
377 // unwind path.
378 int LIR_Assembler::emit_unwind_handler() {
379 #ifndef PRODUCT
380 if (CommentedAssembly) {
381 _masm->block_comment("Unwind handler");
382 }
383 #endif
384
385 int offset = code_offset();
386
387 // Fetch the exception from TLS and clear out exception related thread state
388 __ ldr(r0, Address(rthread, JavaThread::exception_oop_offset()));
389 __ str(zr, Address(rthread, JavaThread::exception_oop_offset()));
390 __ str(zr, Address(rthread, JavaThread::exception_pc_offset()));
391
392 __ bind(_unwind_handler_entry);
393 __ verify_not_null_oop(r0);
394 if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
395 __ mov(r19, r0); // Preserve the exception
396 }
397
398 // Perform needed unlocking
399 MonitorExitStub* stub = nullptr;
400 if (method()->is_synchronized()) {
401 monitor_address(0, FrameMap::r0_opr);
402 stub = new MonitorExitStub(FrameMap::r0_opr, 0);
403 __ unlock_object(r5, r4, r0, r6, *stub->entry());
404 __ bind(*stub->continuation());
405 }
406
407 if (compilation()->env()->dtrace_method_probes()) {
408 __ mov(c_rarg0, rthread);
409 __ mov_metadata(c_rarg1, method()->constant_encoding());
410 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), c_rarg0, c_rarg1);
411 }
412
413 if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
414 __ mov(r0, r19); // Restore the exception
415 }
416
417 // remove the activation and dispatch to the unwind handler
418 __ block_comment("remove_frame and dispatch to the unwind handler");
419 __ remove_frame(initial_frame_size_in_bytes(), needs_stack_repair());
420 __ far_jump(RuntimeAddress(Runtime1::entry_for(StubId::c1_unwind_exception_id)));
421
422 // Emit the slow path assembly
423 if (stub != nullptr) {
424 stub->emit_code(this);
425 }
426
427 return offset;
428 }
429
430
431 int LIR_Assembler::emit_deopt_handler() {
432 // generate code for exception handler
433 address handler_base = __ start_a_stub(deopt_handler_size());
434 if (handler_base == nullptr) {
435 // not enough space left for the handler
436 bailout("deopt handler overflow");
437 return -1;
438 }
439
440 int offset = code_offset();
441
442 Label start;
443 __ bind(start);
444
445 __ far_call(RuntimeAddress(SharedRuntime::deopt_blob()->unpack()));
446
447 int entry_offset = __ offset();
448 __ b(start);
449
450 guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
451 assert(code_offset() - entry_offset >= NativePostCallNop::first_check_size,
452 "out of bounds read in post-call NOP check");
453 __ end_a_stub();
454
455 return entry_offset;
456 }
457
458 void LIR_Assembler::add_debug_info_for_branch(address adr, CodeEmitInfo* info) {
459 _masm->code_section()->relocate(adr, relocInfo::poll_type);
460 int pc_offset = code_offset();
461 flush_debug_info(pc_offset);
462 info->record_debug_info(compilation()->debug_info_recorder(), pc_offset);
463 if (info->exception_handlers() != nullptr) {
464 compilation()->add_exception_handlers_for_pco(pc_offset, info->exception_handlers());
465 }
466 }
467
468 void LIR_Assembler::return_op(LIR_Opr result, C1SafepointPollStub* code_stub) {
469 assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == r0, "word returns are in r0,");
470
471 if (InlineTypeReturnedAsFields) {
472 // Check if we are returning a non-null inline type and load its fields into registers
473 ciType* return_type = compilation()->method()->return_type();
474 if (return_type->is_inlinetype()) {
475 ciInlineKlass* vk = return_type->as_inline_klass();
476 if (vk->can_be_returned_as_fields()) {
477 address unpack_handler = vk->unpack_handler();
478 assert(unpack_handler != nullptr, "must be");
479 __ far_call(RuntimeAddress(unpack_handler));
480 }
481 } else if (return_type->is_instance_klass() && (!return_type->is_loaded() || StressCallingConvention)) {
482 Label skip;
483 Label not_null;
484 __ cbnz(r0, not_null);
485 // Returned value is null, zero all return registers because they may belong to oop fields
486 __ mov(j_rarg1, zr);
487 __ mov(j_rarg2, zr);
488 __ mov(j_rarg3, zr);
489 __ mov(j_rarg4, zr);
490 __ mov(j_rarg5, zr);
491 __ mov(j_rarg6, zr);
492 __ mov(j_rarg7, zr);
493 __ b(skip);
494 __ bind(not_null);
495
496 // Check if we are returning a non-null inline type and load its fields into registers
497 __ test_oop_is_not_inline_type(r0, rscratch2, skip, /* can_be_null= */ false);
498
499 // Load fields from a buffered value with an inline class specific handler
500 __ load_klass(rscratch1 /*dst*/, r0 /*src*/);
501 __ ldr(rscratch1, Address(rscratch1, InlineKlass::adr_members_offset()));
502 __ ldr(rscratch1, Address(rscratch1, InlineKlass::unpack_handler_offset()));
503 // Unpack handler can be null if inline type is not scalarizable in returns
504 __ cbz(rscratch1, skip);
505 __ blr(rscratch1);
506
507 __ bind(skip);
508 }
509 // At this point, r0 points to the value object (for interpreter or C1 caller).
510 // The fields of the object are copied into registers (for C2 caller).
511 }
512
513 // Pop the stack before the safepoint code
514 __ remove_frame(initial_frame_size_in_bytes(), needs_stack_repair());
515
516 if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
517 __ reserved_stack_check();
518 }
519
520 code_stub->set_safepoint_offset(__ offset());
521 __ relocate(relocInfo::poll_return_type);
522 __ safepoint_poll(*code_stub->entry(), true /* at_return */, true /* in_nmethod */);
523 __ ret(lr);
524 }
525
526 int LIR_Assembler::store_inline_type_fields_to_buf(ciInlineKlass* vk) {
527 return (__ store_inline_type_fields_to_buf(vk, false));
528 }
529
530 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
531 guarantee(info != nullptr, "Shouldn't be null");
532 __ get_polling_page(rscratch1, relocInfo::poll_type);
533 add_debug_info_for_branch(info); // This isn't just debug info:
534 // it's the oop map
535 __ read_polling_page(rscratch1, relocInfo::poll_type);
536 return __ offset();
537 }
538
539
540 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
541 if (from_reg == r31_sp)
542 from_reg = sp;
543 if (to_reg == r31_sp)
544 to_reg = sp;
545 __ mov(to_reg, from_reg);
546 }
547
548 void LIR_Assembler::swap_reg(Register a, Register b) { Unimplemented(); }
549
550
551 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
552 assert(src->is_constant(), "should not call otherwise");
553 assert(dest->is_register(), "should not call otherwise");
554 LIR_Const* c = src->as_constant_ptr();
555
556 switch (c->type()) {
557 case T_INT: {
558 assert(patch_code == lir_patch_none, "no patching handled here");
559 __ movw(dest->as_register(), c->as_jint());
560 break;
561 }
562
563 case T_ADDRESS: {
564 assert(patch_code == lir_patch_none, "no patching handled here");
565 __ mov(dest->as_register(), c->as_jint());
566 break;
567 }
568
569 case T_LONG: {
570 assert(patch_code == lir_patch_none, "no patching handled here");
571 #if INCLUDE_CDS
572 if (AOTCodeCache::is_on_for_dump()) {
573 address b = c->as_pointer();
574 if (b == (address)ThreadIdentifier::unsafe_offset()) {
575 __ lea(dest->as_register_lo(), ExternalAddress(b));
576 break;
577 }
578 if (AOTRuntimeConstants::contains(b)) {
579 __ load_aotrc_address(dest->as_register_lo(), b);
580 break;
581 }
582 }
583 #endif
584 __ mov(dest->as_register_lo(), (intptr_t)c->as_jlong());
585 break;
586 }
587
588 case T_OBJECT: {
589 if (patch_code != lir_patch_none) {
590 jobject2reg_with_patching(dest->as_register(), info);
591 } else {
592 jobject2reg(c->as_jobject(), dest->as_register());
593 }
594 break;
595 }
596
597 case T_METADATA: {
598 if (patch_code != lir_patch_none) {
599 klass2reg_with_patching(dest->as_register(), info);
600 } else {
601 __ mov_metadata(dest->as_register(), c->as_metadata());
602 }
603 break;
604 }
605
606 case T_FLOAT: {
607 if (__ operand_valid_for_float_immediate(c->as_jfloat())) {
608 __ fmovs(dest->as_float_reg(), (c->as_jfloat()));
609 } else {
610 __ adr(rscratch1, InternalAddress(float_constant(c->as_jfloat())));
611 __ ldrs(dest->as_float_reg(), Address(rscratch1));
612 }
613 break;
614 }
615
616 case T_DOUBLE: {
617 if (__ operand_valid_for_float_immediate(c->as_jdouble())) {
618 __ fmovd(dest->as_double_reg(), (c->as_jdouble()));
619 } else {
620 __ adr(rscratch1, InternalAddress(double_constant(c->as_jdouble())));
621 __ ldrd(dest->as_double_reg(), Address(rscratch1));
622 }
623 break;
624 }
625
626 default:
627 ShouldNotReachHere();
628 }
629 }
630
631 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
632 LIR_Const* c = src->as_constant_ptr();
633 switch (c->type()) {
634 case T_OBJECT:
635 {
636 if (! c->as_jobject())
637 __ str(zr, frame_map()->address_for_slot(dest->single_stack_ix()));
638 else {
639 const2reg(src, FrameMap::rscratch1_opr, lir_patch_none, nullptr);
640 reg2stack(FrameMap::rscratch1_opr, dest, c->type());
641 }
642 }
643 break;
644 case T_ADDRESS:
645 {
646 const2reg(src, FrameMap::rscratch1_opr, lir_patch_none, nullptr);
647 reg2stack(FrameMap::rscratch1_opr, dest, c->type());
648 }
649 case T_INT:
650 case T_FLOAT:
651 {
652 Register reg = zr;
653 if (c->as_jint_bits() == 0)
654 __ strw(zr, frame_map()->address_for_slot(dest->single_stack_ix()));
655 else {
656 __ movw(rscratch1, c->as_jint_bits());
657 __ strw(rscratch1, frame_map()->address_for_slot(dest->single_stack_ix()));
658 }
659 }
660 break;
661 case T_LONG:
662 case T_DOUBLE:
663 {
664 Register reg = zr;
665 if (c->as_jlong_bits() == 0)
666 __ str(zr, frame_map()->address_for_slot(dest->double_stack_ix(),
667 lo_word_offset_in_bytes));
668 else {
669 __ mov(rscratch1, (intptr_t)c->as_jlong_bits());
670 __ str(rscratch1, frame_map()->address_for_slot(dest->double_stack_ix(),
671 lo_word_offset_in_bytes));
672 }
673 }
674 break;
675 default:
676 ShouldNotReachHere();
677 }
678 }
679
680 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
681 assert(src->is_constant(), "should not call otherwise");
682 LIR_Const* c = src->as_constant_ptr();
683 LIR_Address* to_addr = dest->as_address_ptr();
684
685 void (Assembler::* insn)(Register Rt, const Address &adr);
686
687 switch (type) {
688 case T_ADDRESS:
689 assert(c->as_jint() == 0, "should be");
690 insn = &Assembler::str;
691 break;
692 case T_LONG:
693 assert(c->as_jlong() == 0, "should be");
694 insn = &Assembler::str;
695 break;
696 case T_INT:
697 assert(c->as_jint() == 0, "should be");
698 insn = &Assembler::strw;
699 break;
700 case T_OBJECT:
701 case T_ARRAY:
702 assert(c->as_jobject() == nullptr, "should be");
703 if (UseCompressedOops && !wide) {
704 insn = &Assembler::strw;
705 } else {
706 insn = &Assembler::str;
707 }
708 break;
709 case T_CHAR:
710 case T_SHORT:
711 assert(c->as_jint() == 0, "should be");
712 insn = &Assembler::strh;
713 break;
714 case T_BOOLEAN:
715 case T_BYTE:
716 assert(c->as_jint() == 0, "should be");
717 insn = &Assembler::strb;
718 break;
719 default:
720 ShouldNotReachHere();
721 insn = &Assembler::str; // unreachable
722 }
723
724 if (info) add_debug_info_for_null_check_here(info);
725 (_masm->*insn)(zr, as_Address(to_addr, rscratch1));
726 }
727
728 void LIR_Assembler::reg2reg(LIR_Opr src, LIR_Opr dest) {
729 assert(src->is_register(), "should not call otherwise");
730 assert(dest->is_register(), "should not call otherwise");
731
732 // move between cpu-registers
733 if (dest->is_single_cpu()) {
734 if (src->type() == T_LONG) {
735 // Can do LONG -> OBJECT
736 move_regs(src->as_register_lo(), dest->as_register());
737 return;
738 }
739 assert(src->is_single_cpu(), "must match");
740 if (src->type() == T_OBJECT) {
741 __ verify_oop(src->as_register());
742 }
743 move_regs(src->as_register(), dest->as_register());
744
745 } else if (dest->is_double_cpu()) {
746 if (is_reference_type(src->type())) {
747 // Surprising to me but we can see move of a long to t_object
748 __ verify_oop(src->as_register());
749 move_regs(src->as_register(), dest->as_register_lo());
750 return;
751 }
752 assert(src->is_double_cpu(), "must match");
753 Register f_lo = src->as_register_lo();
754 Register f_hi = src->as_register_hi();
755 Register t_lo = dest->as_register_lo();
756 Register t_hi = dest->as_register_hi();
757 assert(f_hi == f_lo, "must be same");
758 assert(t_hi == t_lo, "must be same");
759 move_regs(f_lo, t_lo);
760
761 } else if (dest->is_single_fpu()) {
762 __ fmovs(dest->as_float_reg(), src->as_float_reg());
763
764 } else if (dest->is_double_fpu()) {
765 __ fmovd(dest->as_double_reg(), src->as_double_reg());
766
767 } else {
768 ShouldNotReachHere();
769 }
770 }
771
772 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
773 precond(src->is_register() && dest->is_stack());
774
775 uint const c_sz32 = sizeof(uint32_t);
776 uint const c_sz64 = sizeof(uint64_t);
777
778 if (src->is_single_cpu()) {
779 int index = dest->single_stack_ix();
780 if (is_reference_type(type)) {
781 __ str(src->as_register(), stack_slot_address(index, c_sz64, rscratch1));
782 __ verify_oop(src->as_register());
783 } else if (type == T_METADATA || type == T_DOUBLE || type == T_ADDRESS) {
784 __ str(src->as_register(), stack_slot_address(index, c_sz64, rscratch1));
785 } else {
786 __ strw(src->as_register(), stack_slot_address(index, c_sz32, rscratch1));
787 }
788
789 } else if (src->is_double_cpu()) {
790 int index = dest->double_stack_ix();
791 Address dest_addr_LO = stack_slot_address(index, c_sz64, rscratch1, lo_word_offset_in_bytes);
792 __ str(src->as_register_lo(), dest_addr_LO);
793
794 } else if (src->is_single_fpu()) {
795 int index = dest->single_stack_ix();
796 __ strs(src->as_float_reg(), stack_slot_address(index, c_sz32, rscratch1));
797
798 } else if (src->is_double_fpu()) {
799 int index = dest->double_stack_ix();
800 __ strd(src->as_double_reg(), stack_slot_address(index, c_sz64, rscratch1));
801
802 } else {
803 ShouldNotReachHere();
804 }
805 }
806
807
808 void LIR_Assembler::reg2mem(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide) {
809 LIR_Address* to_addr = dest->as_address_ptr();
810 PatchingStub* patch = nullptr;
811 Register compressed_src = rscratch1;
812
813 if (patch_code != lir_patch_none) {
814 deoptimize_trap(info);
815 return;
816 }
817
818 if (is_reference_type(type)) {
819 __ verify_oop(src->as_register());
820
821 if (UseCompressedOops && !wide) {
822 __ encode_heap_oop(compressed_src, src->as_register());
823 } else {
824 compressed_src = src->as_register();
825 }
826 }
827
828 int null_check_here = code_offset();
829 switch (type) {
830 case T_FLOAT: {
831 __ strs(src->as_float_reg(), as_Address(to_addr));
832 break;
833 }
834
835 case T_DOUBLE: {
836 __ strd(src->as_double_reg(), as_Address(to_addr));
837 break;
838 }
839
840 case T_ARRAY: // fall through
841 case T_OBJECT: // fall through
842 if (UseCompressedOops && !wide) {
843 __ strw(compressed_src, as_Address(to_addr, rscratch2));
844 } else {
845 __ str(compressed_src, as_Address(to_addr));
846 }
847 break;
848 case T_METADATA:
849 // We get here to store a method pointer to the stack to pass to
850 // a dtrace runtime call. This can't work on 64 bit with
851 // compressed klass ptrs: T_METADATA can be a compressed klass
852 // ptr or a 64 bit method pointer.
853 ShouldNotReachHere();
854 __ str(src->as_register(), as_Address(to_addr));
855 break;
856 case T_ADDRESS:
857 __ str(src->as_register(), as_Address(to_addr));
858 break;
859 case T_INT:
860 __ strw(src->as_register(), as_Address(to_addr));
861 break;
862
863 case T_LONG: {
864 __ str(src->as_register_lo(), as_Address_lo(to_addr));
865 break;
866 }
867
868 case T_BYTE: // fall through
869 case T_BOOLEAN: {
870 __ strb(src->as_register(), as_Address(to_addr));
871 break;
872 }
873
874 case T_CHAR: // fall through
875 case T_SHORT:
876 __ strh(src->as_register(), as_Address(to_addr));
877 break;
878
879 default:
880 ShouldNotReachHere();
881 }
882 if (info != nullptr) {
883 add_debug_info_for_null_check(null_check_here, info);
884 }
885 }
886
887
888 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
889 precond(src->is_stack() && dest->is_register());
890
891 uint const c_sz32 = sizeof(uint32_t);
892 uint const c_sz64 = sizeof(uint64_t);
893
894 if (dest->is_single_cpu()) {
895 int index = src->single_stack_ix();
896 if (is_reference_type(type)) {
897 __ ldr(dest->as_register(), stack_slot_address(index, c_sz64, rscratch1));
898 __ verify_oop(dest->as_register());
899 } else if (type == T_METADATA || type == T_ADDRESS) {
900 __ ldr(dest->as_register(), stack_slot_address(index, c_sz64, rscratch1));
901 } else {
902 __ ldrw(dest->as_register(), stack_slot_address(index, c_sz32, rscratch1));
903 }
904
905 } else if (dest->is_double_cpu()) {
906 int index = src->double_stack_ix();
907 Address src_addr_LO = stack_slot_address(index, c_sz64, rscratch1, lo_word_offset_in_bytes);
908 __ ldr(dest->as_register_lo(), src_addr_LO);
909
910 } else if (dest->is_single_fpu()) {
911 int index = src->single_stack_ix();
912 __ ldrs(dest->as_float_reg(), stack_slot_address(index, c_sz32, rscratch1));
913
914 } else if (dest->is_double_fpu()) {
915 int index = src->double_stack_ix();
916 __ ldrd(dest->as_double_reg(), stack_slot_address(index, c_sz64, rscratch1));
917
918 } else {
919 ShouldNotReachHere();
920 }
921 }
922
923
924 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo* info) {
925 address target = nullptr;
926 relocInfo::relocType reloc_type = relocInfo::none;
927
928 switch (patching_id(info)) {
929 case PatchingStub::access_field_id:
930 target = Runtime1::entry_for(StubId::c1_access_field_patching_id);
931 reloc_type = relocInfo::section_word_type;
932 break;
933 case PatchingStub::load_klass_id:
934 target = Runtime1::entry_for(StubId::c1_load_klass_patching_id);
935 reloc_type = relocInfo::metadata_type;
936 break;
937 case PatchingStub::load_mirror_id:
938 target = Runtime1::entry_for(StubId::c1_load_mirror_patching_id);
939 reloc_type = relocInfo::oop_type;
940 break;
941 case PatchingStub::load_appendix_id:
942 target = Runtime1::entry_for(StubId::c1_load_appendix_patching_id);
943 reloc_type = relocInfo::oop_type;
944 break;
945 default: ShouldNotReachHere();
946 }
947
948 __ far_call(RuntimeAddress(target));
949 add_call_info_here(info);
950 }
951
952 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
953
954 LIR_Opr temp;
955 if (type == T_LONG || type == T_DOUBLE)
956 temp = FrameMap::rscratch1_long_opr;
957 else
958 temp = FrameMap::rscratch1_opr;
959
960 stack2reg(src, temp, src->type());
961 reg2stack(temp, dest, dest->type());
962 }
963
964 void LIR_Assembler::mem2reg(LIR_Opr src, LIR_Opr dest, BasicType type,
965 LIR_PatchCode patch_code, CodeEmitInfo* info,
966 bool wide) {
967 mem2reg(src, dest, type, patch_code, info, wide, false);
968 }
969
970 void LIR_Assembler::mem2reg(LIR_Opr src, LIR_Opr dest, BasicType type,
971 LIR_PatchCode patch_code, CodeEmitInfo* info,
972 bool wide, bool is_volatile) {
973 LIR_Address* addr = src->as_address_ptr();
974 LIR_Address* from_addr = src->as_address_ptr();
975
976 if (addr->base()->type() == T_OBJECT) {
977 __ verify_oop(addr->base()->as_pointer_register());
978 }
979
980 if (patch_code != lir_patch_none) {
981 deoptimize_trap(info);
982 return;
983 }
984
985 if (is_volatile) {
986 load_volatile(from_addr, dest, type, info);
987 } else {
988 load_unordered(from_addr, dest, type, wide, info);
989 }
990
991 if (is_reference_type(type)) {
992 if (UseCompressedOops && !wide) {
993 __ decode_heap_oop(dest->as_register());
994 }
995
996 __ verify_oop(dest->as_register());
997 }
998 }
999
1000 void LIR_Assembler::load_unordered(LIR_Address *from_addr, LIR_Opr dest,
1001 BasicType type, bool wide, CodeEmitInfo* info) {
1002 if (info != nullptr) {
1003 add_debug_info_for_null_check_here(info);
1004 }
1005
1006 switch (type) {
1007 case T_FLOAT: {
1008 __ ldrs(dest->as_float_reg(), as_Address(from_addr));
1009 break;
1010 }
1011
1012 case T_DOUBLE: {
1013 __ ldrd(dest->as_double_reg(), as_Address(from_addr));
1014 break;
1015 }
1016
1017 case T_ARRAY: // fall through
1018 case T_OBJECT: // fall through
1019 if (UseCompressedOops && !wide) {
1020 __ ldrw(dest->as_register(), as_Address(from_addr));
1021 } else {
1022 __ ldr(dest->as_register(), as_Address(from_addr));
1023 }
1024 break;
1025 case T_METADATA:
1026 // We get here to store a method pointer to the stack to pass to
1027 // a dtrace runtime call. This can't work on 64 bit with
1028 // compressed klass ptrs: T_METADATA can be a compressed klass
1029 // ptr or a 64 bit method pointer.
1030 ShouldNotReachHere();
1031 __ ldr(dest->as_register(), as_Address(from_addr));
1032 break;
1033 case T_ADDRESS:
1034 __ ldr(dest->as_register(), as_Address(from_addr));
1035 break;
1036 case T_INT:
1037 __ ldrw(dest->as_register(), as_Address(from_addr));
1038 break;
1039
1040 case T_LONG: {
1041 __ ldr(dest->as_register_lo(), as_Address_lo(from_addr));
1042 break;
1043 }
1044
1045 case T_BYTE:
1046 __ ldrsb(dest->as_register(), as_Address(from_addr));
1047 break;
1048 case T_BOOLEAN: {
1049 __ ldrb(dest->as_register(), as_Address(from_addr));
1050 break;
1051 }
1052
1053 case T_CHAR:
1054 __ ldrh(dest->as_register(), as_Address(from_addr));
1055 break;
1056 case T_SHORT:
1057 __ ldrsh(dest->as_register(), as_Address(from_addr));
1058 break;
1059
1060 default:
1061 ShouldNotReachHere();
1062 }
1063 }
1064
1065 void LIR_Assembler::move(LIR_Opr src, LIR_Opr dst) {
1066 assert(dst->is_cpu_register(), "must be");
1067 assert(dst->type() == src->type(), "must be");
1068
1069 if (src->is_cpu_register()) {
1070 reg2reg(src, dst);
1071 } else if (src->is_stack()) {
1072 stack2reg(src, dst, dst->type());
1073 } else if (src->is_constant()) {
1074 const2reg(src, dst, lir_patch_none, nullptr);
1075 } else {
1076 ShouldNotReachHere();
1077 }
1078 }
1079
1080 void LIR_Assembler::load_volatile(LIR_Address *from_addr, LIR_Opr dest,
1081 BasicType type, CodeEmitInfo* info) {
1082 __ lea(rscratch1, as_Address(from_addr));
1083
1084 Register dest_reg = rscratch2;
1085 if (!is_floating_point_type(type)) {
1086 dest_reg = (dest->is_single_cpu()
1087 ? dest->as_register() : dest->as_register_lo());
1088 }
1089
1090 if (info != nullptr) {
1091 add_debug_info_for_null_check_here(info);
1092 }
1093
1094 // Uses LDAR to ensure memory ordering.
1095 __ load_store_volatile(dest_reg, type, rscratch1, /*is_load*/true);
1096
1097 switch (type) {
1098 // LDAR is unsigned so need to sign-extend for byte and short
1099 case T_BYTE:
1100 __ sxtb(dest_reg, dest_reg);
1101 break;
1102 case T_SHORT:
1103 __ sxth(dest_reg, dest_reg);
1104 break;
1105 // need to move from GPR to FPR after LDAR with FMOV for floating types
1106 case T_FLOAT:
1107 __ fmovs(dest->as_float_reg(), dest_reg);
1108 break;
1109 case T_DOUBLE:
1110 __ fmovd(dest->as_double_reg(), dest_reg);
1111 break;
1112 default:
1113 break;
1114 }
1115 }
1116
1117 int LIR_Assembler::array_element_size(BasicType type) const {
1118 int elem_size = type2aelembytes(type);
1119 return exact_log2(elem_size);
1120 }
1121
1122
1123 void LIR_Assembler::emit_op3(LIR_Op3* op) {
1124 switch (op->code()) {
1125 case lir_idiv:
1126 case lir_irem:
1127 arithmetic_idiv(op->code(),
1128 op->in_opr1(),
1129 op->in_opr2(),
1130 op->in_opr3(),
1131 op->result_opr(),
1132 op->info());
1133 break;
1134 case lir_fmad:
1135 __ fmaddd(op->result_opr()->as_double_reg(),
1136 op->in_opr1()->as_double_reg(),
1137 op->in_opr2()->as_double_reg(),
1138 op->in_opr3()->as_double_reg());
1139 break;
1140 case lir_fmaf:
1141 __ fmadds(op->result_opr()->as_float_reg(),
1142 op->in_opr1()->as_float_reg(),
1143 op->in_opr2()->as_float_reg(),
1144 op->in_opr3()->as_float_reg());
1145 break;
1146 default: ShouldNotReachHere(); break;
1147 }
1148 }
1149
1150 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
1151 #ifdef ASSERT
1152 assert(op->block() == nullptr || op->block()->label() == op->label(), "wrong label");
1153 if (op->block() != nullptr) _branch_target_blocks.append(op->block());
1154 if (op->ublock() != nullptr) _branch_target_blocks.append(op->ublock());
1155 #endif
1156
1157 if (op->cond() == lir_cond_always) {
1158 if (op->info() != nullptr) add_debug_info_for_branch(op->info());
1159 __ b(*(op->label()));
1160 } else {
1161 Assembler::Condition acond;
1162 if (op->code() == lir_cond_float_branch) {
1163 bool is_unordered = (op->ublock() == op->block());
1164 // Assembler::EQ does not permit unordered branches, so we add
1165 // another branch here. Likewise, Assembler::NE does not permit
1166 // ordered branches.
1167 if ((is_unordered && op->cond() == lir_cond_equal)
1168 || (!is_unordered && op->cond() == lir_cond_notEqual))
1169 __ br(Assembler::VS, *(op->ublock()->label()));
1170 switch(op->cond()) {
1171 case lir_cond_equal: acond = Assembler::EQ; break;
1172 case lir_cond_notEqual: acond = Assembler::NE; break;
1173 case lir_cond_less: acond = (is_unordered ? Assembler::LT : Assembler::LO); break;
1174 case lir_cond_lessEqual: acond = (is_unordered ? Assembler::LE : Assembler::LS); break;
1175 case lir_cond_greaterEqual: acond = (is_unordered ? Assembler::HS : Assembler::GE); break;
1176 case lir_cond_greater: acond = (is_unordered ? Assembler::HI : Assembler::GT); break;
1177 default: ShouldNotReachHere();
1178 acond = Assembler::EQ; // unreachable
1179 }
1180 } else {
1181 switch (op->cond()) {
1182 case lir_cond_equal: acond = Assembler::EQ; break;
1183 case lir_cond_notEqual: acond = Assembler::NE; break;
1184 case lir_cond_less: acond = Assembler::LT; break;
1185 case lir_cond_lessEqual: acond = Assembler::LE; break;
1186 case lir_cond_greaterEqual: acond = Assembler::GE; break;
1187 case lir_cond_greater: acond = Assembler::GT; break;
1188 case lir_cond_belowEqual: acond = Assembler::LS; break;
1189 case lir_cond_aboveEqual: acond = Assembler::HS; break;
1190 default: ShouldNotReachHere();
1191 acond = Assembler::EQ; // unreachable
1192 }
1193 }
1194 __ br(acond,*(op->label()));
1195 }
1196 }
1197
1198
1199
1200 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
1201 LIR_Opr src = op->in_opr();
1202 LIR_Opr dest = op->result_opr();
1203
1204 switch (op->bytecode()) {
1205 case Bytecodes::_i2f:
1206 {
1207 __ scvtfws(dest->as_float_reg(), src->as_register());
1208 break;
1209 }
1210 case Bytecodes::_i2d:
1211 {
1212 __ scvtfwd(dest->as_double_reg(), src->as_register());
1213 break;
1214 }
1215 case Bytecodes::_l2d:
1216 {
1217 __ scvtfd(dest->as_double_reg(), src->as_register_lo());
1218 break;
1219 }
1220 case Bytecodes::_l2f:
1221 {
1222 __ scvtfs(dest->as_float_reg(), src->as_register_lo());
1223 break;
1224 }
1225 case Bytecodes::_f2d:
1226 {
1227 __ fcvts(dest->as_double_reg(), src->as_float_reg());
1228 break;
1229 }
1230 case Bytecodes::_d2f:
1231 {
1232 __ fcvtd(dest->as_float_reg(), src->as_double_reg());
1233 break;
1234 }
1235 case Bytecodes::_i2c:
1236 {
1237 __ ubfx(dest->as_register(), src->as_register(), 0, 16);
1238 break;
1239 }
1240 case Bytecodes::_i2l:
1241 {
1242 __ sxtw(dest->as_register_lo(), src->as_register());
1243 break;
1244 }
1245 case Bytecodes::_i2s:
1246 {
1247 __ sxth(dest->as_register(), src->as_register());
1248 break;
1249 }
1250 case Bytecodes::_i2b:
1251 {
1252 __ sxtb(dest->as_register(), src->as_register());
1253 break;
1254 }
1255 case Bytecodes::_l2i:
1256 {
1257 _masm->block_comment("FIXME: This could be a no-op");
1258 __ uxtw(dest->as_register(), src->as_register_lo());
1259 break;
1260 }
1261 case Bytecodes::_d2l:
1262 {
1263 __ fcvtzd(dest->as_register_lo(), src->as_double_reg());
1264 break;
1265 }
1266 case Bytecodes::_f2i:
1267 {
1268 __ fcvtzsw(dest->as_register(), src->as_float_reg());
1269 break;
1270 }
1271 case Bytecodes::_f2l:
1272 {
1273 __ fcvtzs(dest->as_register_lo(), src->as_float_reg());
1274 break;
1275 }
1276 case Bytecodes::_d2i:
1277 {
1278 __ fcvtzdw(dest->as_register(), src->as_double_reg());
1279 break;
1280 }
1281 default: ShouldNotReachHere();
1282 }
1283 }
1284
1285 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
1286 if (op->init_check()) {
1287 __ lea(rscratch1, Address(op->klass()->as_register(), InstanceKlass::init_state_offset()));
1288 __ ldarb(rscratch1, rscratch1);
1289 __ cmpw(rscratch1, InstanceKlass::fully_initialized);
1290 add_debug_info_for_null_check_here(op->stub()->info());
1291 __ br(Assembler::NE, *op->stub()->entry());
1292 }
1293 __ allocate_object(op->obj()->as_register(),
1294 op->tmp1()->as_register(),
1295 op->tmp2()->as_register(),
1296 op->header_size(),
1297 op->object_size(),
1298 op->klass()->as_register(),
1299 *op->stub()->entry());
1300 __ bind(*op->stub()->continuation());
1301 }
1302
1303 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
1304 Register len = op->len()->as_register();
1305 __ uxtw(len, len);
1306
1307 if (UseSlowPath || op->always_slow_path() ||
1308 (!UseFastNewObjectArray && is_reference_type(op->type())) ||
1309 (!UseFastNewTypeArray && !is_reference_type(op->type()))) {
1310 __ b(*op->stub()->entry());
1311 } else {
1312 Register tmp1 = op->tmp1()->as_register();
1313 Register tmp2 = op->tmp2()->as_register();
1314 Register tmp3 = op->tmp3()->as_register();
1315 if (len == tmp1) {
1316 tmp1 = tmp3;
1317 } else if (len == tmp2) {
1318 tmp2 = tmp3;
1319 } else if (len == tmp3) {
1320 // everything is ok
1321 } else {
1322 __ mov(tmp3, len);
1323 }
1324 __ allocate_array(op->obj()->as_register(),
1325 len,
1326 tmp1,
1327 tmp2,
1328 arrayOopDesc::base_offset_in_bytes(op->type()),
1329 array_element_size(op->type()),
1330 op->klass()->as_register(),
1331 *op->stub()->entry(),
1332 op->zero_array());
1333 }
1334 __ bind(*op->stub()->continuation());
1335 }
1336
1337 void LIR_Assembler::type_profile_helper(Register mdo, ciMethodData *md,
1338 ciProfileData *data, Register recv) {
1339
1340 int mdp_offset = md->byte_offset_of_slot(data, in_ByteSize(0));
1341 __ profile_receiver_type(recv, mdo, mdp_offset);
1342 }
1343
1344 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
1345 // we always need a stub for the failure case.
1346 CodeStub* stub = op->stub();
1347 Register obj = op->object()->as_register();
1348 Register k_RInfo = op->tmp1()->as_register();
1349 Register klass_RInfo = op->tmp2()->as_register();
1350 Register dst = op->result_opr()->as_register();
1351 ciKlass* k = op->klass();
1352 Register Rtmp1 = noreg;
1353
1354 // check if it needs to be profiled
1355 ciMethodData* md;
1356 ciProfileData* data;
1357
1358 const bool should_profile = op->should_profile();
1359
1360 if (should_profile) {
1361 ciMethod* method = op->profiled_method();
1362 assert(method != nullptr, "Should have method");
1363 int bci = op->profiled_bci();
1364 md = method->method_data_or_null();
1365 assert(md != nullptr, "Sanity");
1366 data = md->bci_to_data(bci);
1367 assert(data != nullptr, "need data for type check");
1368 assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1369 }
1370 Label* success_target = success;
1371 Label* failure_target = failure;
1372
1373 if (obj == k_RInfo) {
1374 k_RInfo = dst;
1375 } else if (obj == klass_RInfo) {
1376 klass_RInfo = dst;
1377 }
1378
1379 Rtmp1 = op->tmp3()->as_register();
1380 select_different_registers(obj, dst, k_RInfo, klass_RInfo, Rtmp1);
1381
1382 assert_different_registers(obj, k_RInfo, klass_RInfo);
1383
1384 if (op->need_null_check()) {
1385 if (should_profile) {
1386 Register mdo = klass_RInfo;
1387 __ mov_metadata(mdo, md->constant_encoding());
1388 Label not_null;
1389 __ cbnz(obj, not_null);
1390 // Object is null; update MDO and exit
1391 Address data_addr
1392 = __ form_address(rscratch2, mdo,
1393 md->byte_offset_of_slot(data, DataLayout::flags_offset()),
1394 0);
1395 __ ldrb(rscratch1, data_addr);
1396 __ orr(rscratch1, rscratch1, BitData::null_seen_byte_constant());
1397 __ strb(rscratch1, data_addr);
1398 __ b(*obj_is_null);
1399 __ bind(not_null);
1400
1401 Register recv = k_RInfo;
1402 __ load_klass(recv, obj);
1403 type_profile_helper(mdo, md, data, recv);
1404 } else {
1405 __ cbz(obj, *obj_is_null);
1406 }
1407 }
1408
1409 if (!k->is_loaded()) {
1410 klass2reg_with_patching(k_RInfo, op->info_for_patch());
1411 } else {
1412 __ mov_metadata(k_RInfo, k->constant_encoding());
1413 }
1414 __ verify_oop(obj);
1415
1416 if (op->fast_check()) {
1417 assert(!k->is_loaded() || !k->is_obj_array_klass(), "Use refined array for a direct pointer comparison");
1418 // get object class
1419 // not a safepoint as obj null check happens earlier
1420 __ load_klass(rscratch1, obj);
1421 __ cmp( rscratch1, k_RInfo);
1422
1423 __ br(Assembler::NE, *failure_target);
1424 // successful cast, fall through to profile or jump
1425 } else {
1426 // get object class
1427 // not a safepoint as obj null check happens earlier
1428 __ load_klass(klass_RInfo, obj);
1429 if (k->is_loaded()) {
1430 // See if we get an immediate positive hit
1431 __ ldr(rscratch1, Address(klass_RInfo, int64_t(k->super_check_offset())));
1432 __ cmp(k_RInfo, rscratch1);
1433 if ((juint)in_bytes(Klass::secondary_super_cache_offset()) != k->super_check_offset()) {
1434 __ br(Assembler::NE, *failure_target);
1435 // successful cast, fall through to profile or jump
1436 } else {
1437 // See if we get an immediate positive hit
1438 __ br(Assembler::EQ, *success_target);
1439 // check for self
1440 if (k->is_loaded() && k->is_obj_array_klass()) {
1441 // For a direct pointer comparison, we need the refined array klass pointer
1442 ciKlass* k_refined = ciObjArrayKlass::make(k->as_obj_array_klass()->element_klass());
1443 if (!k_refined->is_loaded()) {
1444 bailout("encountered unloaded_ciobjarrayklass due to out of memory error");
1445 return;
1446 }
1447 __ mov_metadata(rscratch1, k_refined->constant_encoding());
1448 __ cmp(klass_RInfo, rscratch1);
1449 } else {
1450 __ cmp(klass_RInfo, k_RInfo);
1451 }
1452 __ br(Assembler::EQ, *success_target);
1453
1454 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1455 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1456 __ ldr(klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1457 // result is a boolean
1458 __ cbzw(klass_RInfo, *failure_target);
1459 // successful cast, fall through to profile or jump
1460 }
1461 } else {
1462 // perform the fast part of the checking logic
1463 __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1464 // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1465 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1466 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1467 __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1468 // result is a boolean
1469 __ cbz(k_RInfo, *failure_target);
1470 // successful cast, fall through to profile or jump
1471 }
1472 }
1473 __ b(*success);
1474 }
1475
1476
1477 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
1478 const bool should_profile = op->should_profile();
1479
1480 LIR_Code code = op->code();
1481 if (code == lir_store_check) {
1482 Register value = op->object()->as_register();
1483 Register array = op->array()->as_register();
1484 Register k_RInfo = op->tmp1()->as_register();
1485 Register klass_RInfo = op->tmp2()->as_register();
1486 Register Rtmp1 = op->tmp3()->as_register();
1487
1488 CodeStub* stub = op->stub();
1489
1490 // check if it needs to be profiled
1491 ciMethodData* md;
1492 ciProfileData* data;
1493
1494 if (should_profile) {
1495 ciMethod* method = op->profiled_method();
1496 assert(method != nullptr, "Should have method");
1497 int bci = op->profiled_bci();
1498 md = method->method_data_or_null();
1499 assert(md != nullptr, "Sanity");
1500 data = md->bci_to_data(bci);
1501 assert(data != nullptr, "need data for type check");
1502 assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1503 }
1504 Label done;
1505 Label* success_target = &done;
1506 Label* failure_target = stub->entry();
1507
1508 if (should_profile) {
1509 Label not_null;
1510 Register mdo = klass_RInfo;
1511 __ mov_metadata(mdo, md->constant_encoding());
1512 __ cbnz(value, not_null);
1513 // Object is null; update MDO and exit
1514 Address data_addr
1515 = __ form_address(rscratch2, mdo,
1516 md->byte_offset_of_slot(data, DataLayout::flags_offset()), 0);
1517 __ ldrb(rscratch1, data_addr);
1518 __ orr(rscratch1, rscratch1, BitData::null_seen_byte_constant());
1519 __ strb(rscratch1, data_addr);
1520 __ b(done);
1521 __ bind(not_null);
1522
1523 Register recv = k_RInfo;
1524 __ load_klass(recv, value);
1525 type_profile_helper(mdo, md, data, recv);
1526 } else {
1527 __ cbz(value, done);
1528 }
1529
1530 add_debug_info_for_null_check_here(op->info_for_exception());
1531 __ load_klass(k_RInfo, array);
1532 __ load_klass(klass_RInfo, value);
1533
1534 // get instance klass (it's already uncompressed)
1535 __ ldr(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
1536 // perform the fast part of the checking logic
1537 __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1538 // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1539 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1540 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1541 __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1542 // result is a boolean
1543 __ cbzw(k_RInfo, *failure_target);
1544 // fall through to the success case
1545
1546 __ bind(done);
1547 } else if (code == lir_checkcast) {
1548 Register obj = op->object()->as_register();
1549 Register dst = op->result_opr()->as_register();
1550 Label success;
1551 emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
1552 __ bind(success);
1553 if (dst != obj) {
1554 __ mov(dst, obj);
1555 }
1556 } else if (code == lir_instanceof) {
1557 Register obj = op->object()->as_register();
1558 Register dst = op->result_opr()->as_register();
1559 Label success, failure, done;
1560 emit_typecheck_helper(op, &success, &failure, &failure);
1561 __ bind(failure);
1562 __ mov(dst, zr);
1563 __ b(done);
1564 __ bind(success);
1565 __ mov(dst, 1);
1566 __ bind(done);
1567 } else {
1568 ShouldNotReachHere();
1569 }
1570 }
1571
1572 void LIR_Assembler::emit_opFlattenedArrayCheck(LIR_OpFlattenedArrayCheck* op) {
1573 // We are loading/storing from/to an array that *may* be a flat array (the
1574 // declared type is Object[], abstract[], interface[] or VT.ref[]).
1575 // If this array is a flat array, take the slow path.
1576 __ test_flat_array_oop(op->array()->as_register(), op->tmp()->as_register(), *op->stub()->entry());
1577 }
1578
1579 void LIR_Assembler::emit_opNullFreeArrayCheck(LIR_OpNullFreeArrayCheck* op) {
1580 // We are storing into an array that *may* be null-free (the declared type is
1581 // Object[], abstract[], interface[] or VT.ref[]).
1582 Label test_mark_word;
1583 Register tmp = op->tmp()->as_register();
1584 __ ldr(tmp, Address(op->array()->as_register(), oopDesc::mark_offset_in_bytes()));
1585 __ tst(tmp, markWord::unlocked_value);
1586 __ br(Assembler::NE, test_mark_word);
1587 __ load_prototype_header(tmp, op->array()->as_register());
1588 __ bind(test_mark_word);
1589 __ tst(tmp, markWord::null_free_array_bit_in_place);
1590 }
1591
1592 void LIR_Assembler::emit_opSubstitutabilityCheck(LIR_OpSubstitutabilityCheck* op) {
1593 Label L_oops_equal;
1594 Label L_oops_not_equal;
1595 Label L_end;
1596
1597 Register left = op->left()->as_register();
1598 Register right = op->right()->as_register();
1599
1600 __ cmp(left, right);
1601 __ br(Assembler::EQ, L_oops_equal);
1602
1603 // (1) Null check -- if one of the operands is null, the other must not be null (because
1604 // the two references are not equal), so they are not substitutable,
1605 __ cbz(left, L_oops_not_equal);
1606 __ cbz(right, L_oops_not_equal);
1607
1608 ciKlass* left_klass = op->left_klass();
1609 ciKlass* right_klass = op->right_klass();
1610
1611 // (2) Inline type check -- if either of the operands is not an inline type,
1612 // they are not substitutable. We do this only if we are not sure that the
1613 // operands are inline type
1614 if ((left_klass == nullptr || right_klass == nullptr) ||// The klass is still unloaded, or came from a Phi node.
1615 !left_klass->is_inlinetype() || !right_klass->is_inlinetype()) {
1616 Register tmp1 = op->tmp1()->as_register();
1617 Register tmp2 = op->tmp2()->as_register();
1618 __ mov(tmp1, markWord::inline_type_pattern);
1619 __ ldr(tmp2, Address(left, oopDesc::mark_offset_in_bytes()));
1620 __ andr(tmp1, tmp1, tmp2);
1621 __ ldr(tmp2, Address(right, oopDesc::mark_offset_in_bytes()));
1622 __ andr(tmp1, tmp1, tmp2);
1623 __ cmp(tmp1, (u1)markWord::inline_type_pattern);
1624 __ br(Assembler::NE, L_oops_not_equal);
1625 }
1626
1627 // (3) Same klass check: if the operands are of different klasses, they are not substitutable.
1628 if (left_klass != nullptr && left_klass->is_inlinetype() && left_klass == right_klass) {
1629 // No need to load klass -- the operands are statically known to be the same inline klass.
1630 __ b(*op->stub()->entry());
1631 } else {
1632 Register tmp1 = op->tmp1()->as_register();
1633 Register tmp2 = op->tmp2()->as_register();
1634 __ cmp_klasses_from_objects(left, right, tmp1, tmp2);
1635 __ br(Assembler::EQ, *op->stub()->entry()); // same klass -> do slow check
1636 // fall through to L_oops_not_equal
1637 }
1638
1639 __ bind(L_oops_not_equal);
1640 move(op->not_equal_result(), op->result_opr());
1641 __ b(L_end);
1642
1643 __ bind(L_oops_equal);
1644 move(op->equal_result(), op->result_opr());
1645 __ b(L_end);
1646
1647 // We've returned from the stub. R0 contains 0x0 IFF the two
1648 // operands are not substitutable. (Don't compare against 0x1 in case the
1649 // C compiler is naughty)
1650 __ bind(*op->stub()->continuation());
1651 __ cbz(r0, L_oops_not_equal); // (call_stub() == 0x0) -> not_equal
1652 move(op->equal_result(), op->result_opr()); // (call_stub() != 0x0) -> equal
1653 // fall-through
1654 __ bind(L_end);
1655 }
1656
1657
1658 void LIR_Assembler::casw(Register addr, Register newval, Register cmpval) {
1659 __ cmpxchg(addr, cmpval, newval, Assembler::word, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1660 __ cset(rscratch1, Assembler::NE);
1661 }
1662
1663 void LIR_Assembler::casl(Register addr, Register newval, Register cmpval) {
1664 __ cmpxchg(addr, cmpval, newval, Assembler::xword, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1665 __ cset(rscratch1, Assembler::NE);
1666 }
1667
1668
1669 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
1670 Register addr;
1671 if (op->addr()->is_register()) {
1672 addr = as_reg(op->addr());
1673 } else {
1674 assert(op->addr()->is_address(), "what else?");
1675 LIR_Address* addr_ptr = op->addr()->as_address_ptr();
1676 assert(addr_ptr->disp() == 0, "need 0 disp");
1677 assert(addr_ptr->index() == LIR_Opr::illegalOpr(), "need 0 index");
1678 addr = as_reg(addr_ptr->base());
1679 }
1680 Register newval = as_reg(op->new_value());
1681 Register cmpval = as_reg(op->cmp_value());
1682
1683 if (op->code() == lir_cas_obj) {
1684 if (UseCompressedOops) {
1685 Register t1 = op->tmp1()->as_register();
1686 assert(op->tmp1()->is_valid(), "must be");
1687 __ encode_heap_oop(t1, cmpval);
1688 cmpval = t1;
1689 __ encode_heap_oop(rscratch2, newval);
1690 newval = rscratch2;
1691 casw(addr, newval, cmpval);
1692 } else {
1693 casl(addr, newval, cmpval);
1694 }
1695 } else if (op->code() == lir_cas_int) {
1696 casw(addr, newval, cmpval);
1697 } else {
1698 casl(addr, newval, cmpval);
1699 }
1700 }
1701
1702
1703 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type,
1704 LIR_Opr cmp_opr1, LIR_Opr cmp_opr2) {
1705 assert(cmp_opr1 == LIR_OprFact::illegalOpr && cmp_opr2 == LIR_OprFact::illegalOpr, "unnecessary cmp oprs on aarch64");
1706
1707 Assembler::Condition acond, ncond;
1708 switch (condition) {
1709 case lir_cond_equal: acond = Assembler::EQ; ncond = Assembler::NE; break;
1710 case lir_cond_notEqual: acond = Assembler::NE; ncond = Assembler::EQ; break;
1711 case lir_cond_less: acond = Assembler::LT; ncond = Assembler::GE; break;
1712 case lir_cond_lessEqual: acond = Assembler::LE; ncond = Assembler::GT; break;
1713 case lir_cond_greaterEqual: acond = Assembler::GE; ncond = Assembler::LT; break;
1714 case lir_cond_greater: acond = Assembler::GT; ncond = Assembler::LE; break;
1715 case lir_cond_belowEqual:
1716 case lir_cond_aboveEqual:
1717 default: ShouldNotReachHere();
1718 acond = Assembler::EQ; ncond = Assembler::NE; // unreachable
1719 }
1720
1721 assert(result->is_single_cpu() || result->is_double_cpu(),
1722 "expect single register for result");
1723 if (opr1->is_constant() && opr2->is_constant()
1724 && opr1->type() == T_INT && opr2->type() == T_INT) {
1725 jint val1 = opr1->as_jint();
1726 jint val2 = opr2->as_jint();
1727 if (val1 == 0 && val2 == 1) {
1728 __ cset(result->as_register(), ncond);
1729 return;
1730 } else if (val1 == 1 && val2 == 0) {
1731 __ cset(result->as_register(), acond);
1732 return;
1733 }
1734 }
1735
1736 if (opr1->is_constant() && opr2->is_constant()
1737 && opr1->type() == T_LONG && opr2->type() == T_LONG) {
1738 jlong val1 = opr1->as_jlong();
1739 jlong val2 = opr2->as_jlong();
1740 if (val1 == 0 && val2 == 1) {
1741 __ cset(result->as_register_lo(), ncond);
1742 return;
1743 } else if (val1 == 1 && val2 == 0) {
1744 __ cset(result->as_register_lo(), acond);
1745 return;
1746 }
1747 }
1748
1749 if (opr1->is_stack()) {
1750 stack2reg(opr1, FrameMap::rscratch1_opr, result->type());
1751 opr1 = FrameMap::rscratch1_opr;
1752 } else if (opr1->is_constant()) {
1753 LIR_Opr tmp
1754 = opr1->type() == T_LONG ? FrameMap::rscratch1_long_opr : FrameMap::rscratch1_opr;
1755 const2reg(opr1, tmp, lir_patch_none, nullptr);
1756 opr1 = tmp;
1757 }
1758
1759 if (opr2->is_stack()) {
1760 stack2reg(opr2, FrameMap::rscratch2_opr, result->type());
1761 opr2 = FrameMap::rscratch2_opr;
1762 } else if (opr2->is_constant()) {
1763 LIR_Opr tmp
1764 = opr2->type() == T_LONG ? FrameMap::rscratch2_long_opr : FrameMap::rscratch2_opr;
1765 const2reg(opr2, tmp, lir_patch_none, nullptr);
1766 opr2 = tmp;
1767 }
1768
1769 if (result->type() == T_LONG)
1770 __ csel(result->as_register_lo(), opr1->as_register_lo(), opr2->as_register_lo(), acond);
1771 else
1772 __ csel(result->as_register(), opr1->as_register(), opr2->as_register(), acond);
1773 }
1774
1775 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info) {
1776 assert(info == nullptr, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1777
1778 if (left->is_single_cpu()) {
1779 Register lreg = left->as_register();
1780 Register dreg = as_reg(dest);
1781
1782 if (right->is_single_cpu()) {
1783 // cpu register - cpu register
1784
1785 assert(left->type() == T_INT && right->type() == T_INT && dest->type() == T_INT,
1786 "should be");
1787 Register rreg = right->as_register();
1788 switch (code) {
1789 case lir_add: __ addw (dest->as_register(), lreg, rreg); break;
1790 case lir_sub: __ subw (dest->as_register(), lreg, rreg); break;
1791 case lir_mul: __ mulw (dest->as_register(), lreg, rreg); break;
1792 default: ShouldNotReachHere();
1793 }
1794
1795 } else if (right->is_double_cpu()) {
1796 Register rreg = right->as_register_lo();
1797 // single_cpu + double_cpu: can happen with obj+long
1798 assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1799 switch (code) {
1800 case lir_add: __ add(dreg, lreg, rreg); break;
1801 case lir_sub: __ sub(dreg, lreg, rreg); break;
1802 default: ShouldNotReachHere();
1803 }
1804 } else if (right->is_constant()) {
1805 // cpu register - constant
1806 jlong c;
1807
1808 // FIXME. This is fugly: we really need to factor all this logic.
1809 switch(right->type()) {
1810 case T_LONG:
1811 c = right->as_constant_ptr()->as_jlong();
1812 break;
1813 case T_INT:
1814 case T_ADDRESS:
1815 c = right->as_constant_ptr()->as_jint();
1816 break;
1817 default:
1818 ShouldNotReachHere();
1819 c = 0; // unreachable
1820 break;
1821 }
1822
1823 assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1824 if (c == 0 && dreg == lreg) {
1825 COMMENT("effective nop elided");
1826 return;
1827 }
1828 switch(left->type()) {
1829 case T_INT:
1830 switch (code) {
1831 case lir_add: __ addw(dreg, lreg, c); break;
1832 case lir_sub: __ subw(dreg, lreg, c); break;
1833 default: ShouldNotReachHere();
1834 }
1835 break;
1836 case T_OBJECT:
1837 case T_ADDRESS:
1838 switch (code) {
1839 case lir_add: __ add(dreg, lreg, c); break;
1840 case lir_sub: __ sub(dreg, lreg, c); break;
1841 default: ShouldNotReachHere();
1842 }
1843 break;
1844 default:
1845 ShouldNotReachHere();
1846 }
1847 } else {
1848 ShouldNotReachHere();
1849 }
1850
1851 } else if (left->is_double_cpu()) {
1852 Register lreg_lo = left->as_register_lo();
1853
1854 if (right->is_double_cpu()) {
1855 // cpu register - cpu register
1856 Register rreg_lo = right->as_register_lo();
1857 switch (code) {
1858 case lir_add: __ add (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1859 case lir_sub: __ sub (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1860 case lir_mul: __ mul (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1861 case lir_div: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, false, rscratch1); break;
1862 case lir_rem: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, true, rscratch1); break;
1863 default:
1864 ShouldNotReachHere();
1865 }
1866
1867 } else if (right->is_constant()) {
1868 jlong c = right->as_constant_ptr()->as_jlong();
1869 Register dreg = as_reg(dest);
1870 switch (code) {
1871 case lir_add:
1872 case lir_sub:
1873 if (c == 0 && dreg == lreg_lo) {
1874 COMMENT("effective nop elided");
1875 return;
1876 }
1877 code == lir_add ? __ add(dreg, lreg_lo, c) : __ sub(dreg, lreg_lo, c);
1878 break;
1879 case lir_div:
1880 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1881 if (c == 1) {
1882 // move lreg_lo to dreg if divisor is 1
1883 __ mov(dreg, lreg_lo);
1884 } else {
1885 unsigned int shift = log2i_exact(c);
1886 // use rscratch1 as intermediate result register
1887 __ asr(rscratch1, lreg_lo, 63);
1888 __ add(rscratch1, lreg_lo, rscratch1, Assembler::LSR, 64 - shift);
1889 __ asr(dreg, rscratch1, shift);
1890 }
1891 break;
1892 case lir_rem:
1893 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1894 if (c == 1) {
1895 // move 0 to dreg if divisor is 1
1896 __ mov(dreg, zr);
1897 } else {
1898 // use rscratch1 as intermediate result register
1899 __ negs(rscratch1, lreg_lo);
1900 __ andr(dreg, lreg_lo, c - 1);
1901 __ andr(rscratch1, rscratch1, c - 1);
1902 __ csneg(dreg, dreg, rscratch1, Assembler::MI);
1903 }
1904 break;
1905 default:
1906 ShouldNotReachHere();
1907 }
1908 } else {
1909 ShouldNotReachHere();
1910 }
1911 } else if (left->is_single_fpu()) {
1912 assert(right->is_single_fpu(), "right hand side of float arithmetics needs to be float register");
1913 switch (code) {
1914 case lir_add: __ fadds (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1915 case lir_sub: __ fsubs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1916 case lir_mul: __ fmuls (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1917 case lir_div: __ fdivs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1918 default:
1919 ShouldNotReachHere();
1920 }
1921 } else if (left->is_double_fpu()) {
1922 if (right->is_double_fpu()) {
1923 // fpu register - fpu register
1924 switch (code) {
1925 case lir_add: __ faddd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1926 case lir_sub: __ fsubd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1927 case lir_mul: __ fmuld (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1928 case lir_div: __ fdivd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1929 default:
1930 ShouldNotReachHere();
1931 }
1932 } else {
1933 if (right->is_constant()) {
1934 ShouldNotReachHere();
1935 }
1936 ShouldNotReachHere();
1937 }
1938 } else if (left->is_single_stack() || left->is_address()) {
1939 assert(left == dest, "left and dest must be equal");
1940 ShouldNotReachHere();
1941 } else {
1942 ShouldNotReachHere();
1943 }
1944 }
1945
1946 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr tmp, LIR_Opr dest, LIR_Op* op) {
1947 switch(code) {
1948 case lir_abs : __ fabsd(dest->as_double_reg(), value->as_double_reg()); break;
1949 case lir_sqrt: __ fsqrtd(dest->as_double_reg(), value->as_double_reg()); break;
1950 case lir_f2hf: __ flt_to_flt16(dest->as_register(), value->as_float_reg(), tmp->as_float_reg()); break;
1951 case lir_hf2f: __ flt16_to_flt(dest->as_float_reg(), value->as_register(), tmp->as_float_reg()); break;
1952 default : ShouldNotReachHere();
1953 }
1954 }
1955
1956 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
1957
1958 assert(left->is_single_cpu() || left->is_double_cpu(), "expect single or double register");
1959 Register Rleft = left->is_single_cpu() ? left->as_register() :
1960 left->as_register_lo();
1961 if (dst->is_single_cpu()) {
1962 Register Rdst = dst->as_register();
1963 if (right->is_constant()) {
1964 switch (code) {
1965 case lir_logic_and: __ andw (Rdst, Rleft, right->as_jint()); break;
1966 case lir_logic_or: __ orrw (Rdst, Rleft, right->as_jint()); break;
1967 case lir_logic_xor: __ eorw (Rdst, Rleft, right->as_jint()); break;
1968 default: ShouldNotReachHere(); break;
1969 }
1970 } else {
1971 Register Rright = right->is_single_cpu() ? right->as_register() :
1972 right->as_register_lo();
1973 switch (code) {
1974 case lir_logic_and: __ andw (Rdst, Rleft, Rright); break;
1975 case lir_logic_or: __ orrw (Rdst, Rleft, Rright); break;
1976 case lir_logic_xor: __ eorw (Rdst, Rleft, Rright); break;
1977 default: ShouldNotReachHere(); break;
1978 }
1979 }
1980 } else {
1981 Register Rdst = dst->as_register_lo();
1982 if (right->is_constant()) {
1983 switch (code) {
1984 case lir_logic_and: __ andr (Rdst, Rleft, right->as_jlong()); break;
1985 case lir_logic_or: __ orr (Rdst, Rleft, right->as_jlong()); break;
1986 case lir_logic_xor: __ eor (Rdst, Rleft, right->as_jlong()); break;
1987 default: ShouldNotReachHere(); break;
1988 }
1989 } else {
1990 Register Rright = right->is_single_cpu() ? right->as_register() :
1991 right->as_register_lo();
1992 switch (code) {
1993 case lir_logic_and: __ andr (Rdst, Rleft, Rright); break;
1994 case lir_logic_or: __ orr (Rdst, Rleft, Rright); break;
1995 case lir_logic_xor: __ eor (Rdst, Rleft, Rright); break;
1996 default: ShouldNotReachHere(); break;
1997 }
1998 }
1999 }
2000 }
2001
2002
2003
2004 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr illegal, LIR_Opr result, CodeEmitInfo* info) {
2005
2006 // opcode check
2007 assert((code == lir_idiv) || (code == lir_irem), "opcode must be idiv or irem");
2008 bool is_irem = (code == lir_irem);
2009
2010 // operand check
2011 assert(left->is_single_cpu(), "left must be register");
2012 assert(right->is_single_cpu() || right->is_constant(), "right must be register or constant");
2013 assert(result->is_single_cpu(), "result must be register");
2014 Register lreg = left->as_register();
2015 Register dreg = result->as_register();
2016
2017 // power-of-2 constant check and codegen
2018 if (right->is_constant()) {
2019 int c = right->as_constant_ptr()->as_jint();
2020 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
2021 if (is_irem) {
2022 if (c == 1) {
2023 // move 0 to dreg if divisor is 1
2024 __ movw(dreg, zr);
2025 } else {
2026 // use rscratch1 as intermediate result register
2027 __ negsw(rscratch1, lreg);
2028 __ andw(dreg, lreg, c - 1);
2029 __ andw(rscratch1, rscratch1, c - 1);
2030 __ csnegw(dreg, dreg, rscratch1, Assembler::MI);
2031 }
2032 } else {
2033 if (c == 1) {
2034 // move lreg to dreg if divisor is 1
2035 __ movw(dreg, lreg);
2036 } else {
2037 unsigned int shift = exact_log2(c);
2038 // use rscratch1 as intermediate result register
2039 __ asrw(rscratch1, lreg, 31);
2040 __ addw(rscratch1, lreg, rscratch1, Assembler::LSR, 32 - shift);
2041 __ asrw(dreg, rscratch1, shift);
2042 }
2043 }
2044 } else {
2045 Register rreg = right->as_register();
2046 __ corrected_idivl(dreg, lreg, rreg, is_irem, rscratch1);
2047 }
2048 }
2049
2050
2051 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
2052 if (opr1->is_constant() && opr2->is_single_cpu()) {
2053 // tableswitch
2054 Register reg = as_reg(opr2);
2055 struct tableswitch &table = switches[opr1->as_constant_ptr()->as_jint()];
2056 __ tableswitch(reg, table._first_key, table._last_key, table._branches, table._after);
2057 } else if (opr1->is_single_cpu() || opr1->is_double_cpu()) {
2058 Register reg1 = as_reg(opr1);
2059 if (opr2->is_single_cpu()) {
2060 // cpu register - cpu register
2061 Register reg2 = opr2->as_register();
2062 if (is_reference_type(opr1->type())) {
2063 __ cmpoop(reg1, reg2);
2064 } else {
2065 assert(!is_reference_type(opr2->type()), "cmp int, oop?");
2066 __ cmpw(reg1, reg2);
2067 }
2068 return;
2069 }
2070 if (opr2->is_double_cpu()) {
2071 // cpu register - cpu register
2072 Register reg2 = opr2->as_register_lo();
2073 __ cmp(reg1, reg2);
2074 return;
2075 }
2076
2077 if (opr2->is_constant()) {
2078 bool is_32bit = false; // width of register operand
2079 jlong imm;
2080
2081 switch(opr2->type()) {
2082 case T_INT:
2083 imm = opr2->as_constant_ptr()->as_jint();
2084 is_32bit = true;
2085 break;
2086 case T_LONG:
2087 imm = opr2->as_constant_ptr()->as_jlong();
2088 break;
2089 case T_ADDRESS:
2090 imm = opr2->as_constant_ptr()->as_jint();
2091 break;
2092 case T_METADATA:
2093 imm = (intptr_t)(opr2->as_constant_ptr()->as_metadata());
2094 break;
2095 case T_OBJECT:
2096 case T_ARRAY:
2097 jobject2reg(opr2->as_constant_ptr()->as_jobject(), rscratch1);
2098 __ cmpoop(reg1, rscratch1);
2099 return;
2100 default:
2101 ShouldNotReachHere();
2102 imm = 0; // unreachable
2103 break;
2104 }
2105
2106 if (Assembler::operand_valid_for_add_sub_immediate(imm)) {
2107 if (is_32bit)
2108 __ cmpw(reg1, imm);
2109 else
2110 __ subs(zr, reg1, imm);
2111 return;
2112 } else {
2113 __ mov(rscratch1, imm);
2114 if (is_32bit)
2115 __ cmpw(reg1, rscratch1);
2116 else
2117 __ cmp(reg1, rscratch1);
2118 return;
2119 }
2120 } else
2121 ShouldNotReachHere();
2122 } else if (opr1->is_single_fpu()) {
2123 FloatRegister reg1 = opr1->as_float_reg();
2124 assert(opr2->is_single_fpu(), "expect single float register");
2125 FloatRegister reg2 = opr2->as_float_reg();
2126 __ fcmps(reg1, reg2);
2127 } else if (opr1->is_double_fpu()) {
2128 FloatRegister reg1 = opr1->as_double_reg();
2129 assert(opr2->is_double_fpu(), "expect double float register");
2130 FloatRegister reg2 = opr2->as_double_reg();
2131 __ fcmpd(reg1, reg2);
2132 } else {
2133 ShouldNotReachHere();
2134 }
2135 }
2136
2137 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op){
2138 if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
2139 bool is_unordered_less = (code == lir_ucmp_fd2i);
2140 if (left->is_single_fpu()) {
2141 __ float_cmp(true, is_unordered_less ? -1 : 1, left->as_float_reg(), right->as_float_reg(), dst->as_register());
2142 } else if (left->is_double_fpu()) {
2143 __ float_cmp(false, is_unordered_less ? -1 : 1, left->as_double_reg(), right->as_double_reg(), dst->as_register());
2144 } else {
2145 ShouldNotReachHere();
2146 }
2147 } else if (code == lir_cmp_l2i) {
2148 Label done;
2149 __ cmp(left->as_register_lo(), right->as_register_lo());
2150 __ mov(dst->as_register(), (uint64_t)-1L);
2151 __ br(Assembler::LT, done);
2152 __ csinc(dst->as_register(), zr, zr, Assembler::EQ);
2153 __ bind(done);
2154 } else {
2155 ShouldNotReachHere();
2156 }
2157 }
2158
2159
2160 void LIR_Assembler::align_call(LIR_Code code) { }
2161
2162
2163 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
2164 address call = __ trampoline_call(Address(op->addr(), rtype));
2165 if (call == nullptr) {
2166 bailout("trampoline stub overflow");
2167 return;
2168 }
2169 add_call_info(code_offset(), op->info(), op->maybe_return_as_fields());
2170 __ post_call_nop();
2171 }
2172
2173
2174 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
2175 address call = __ ic_call(op->addr());
2176 if (call == nullptr) {
2177 bailout("trampoline stub overflow");
2178 return;
2179 }
2180 add_call_info(code_offset(), op->info(), op->maybe_return_as_fields());
2181 __ post_call_nop();
2182 }
2183
2184 void LIR_Assembler::emit_static_call_stub() {
2185 address call_pc = __ pc();
2186 address stub = __ start_a_stub(call_stub_size());
2187 if (stub == nullptr) {
2188 bailout("static call stub overflow");
2189 return;
2190 }
2191
2192 int start = __ offset();
2193
2194 __ relocate(static_stub_Relocation::spec(call_pc));
2195 __ emit_static_call_stub();
2196
2197 assert(__ offset() - start + CompiledDirectCall::to_trampoline_stub_size()
2198 <= call_stub_size(), "stub too big");
2199 __ end_a_stub();
2200 }
2201
2202
2203 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
2204 assert(exceptionOop->as_register() == r0, "must match");
2205 assert(exceptionPC->as_register() == r3, "must match");
2206
2207 // exception object is not added to oop map by LinearScan
2208 // (LinearScan assumes that no oops are in fixed registers)
2209 info->add_register_oop(exceptionOop);
2210 StubId unwind_id;
2211
2212 // get current pc information
2213 // pc is only needed if the method has an exception handler, the unwind code does not need it.
2214 if (compilation()->debug_info_recorder()->last_pc_offset() == __ offset()) {
2215 // As no instructions have been generated yet for this LIR node it's
2216 // possible that an oop map already exists for the current offset.
2217 // In that case insert an dummy NOP here to ensure all oop map PCs
2218 // are unique. See JDK-8237483.
2219 __ nop();
2220 }
2221 int pc_for_athrow_offset = __ offset();
2222 InternalAddress pc_for_athrow(__ pc());
2223 __ adr(exceptionPC->as_register(), pc_for_athrow);
2224 add_call_info(pc_for_athrow_offset, info); // for exception handler
2225
2226 __ verify_not_null_oop(r0);
2227 // search an exception handler (r0: exception oop, r3: throwing pc)
2228 if (compilation()->has_fpu_code()) {
2229 unwind_id = StubId::c1_handle_exception_id;
2230 } else {
2231 unwind_id = StubId::c1_handle_exception_nofpu_id;
2232 }
2233 __ far_call(RuntimeAddress(Runtime1::entry_for(unwind_id)));
2234
2235 // FIXME: enough room for two byte trap ????
2236 __ nop();
2237 }
2238
2239
2240 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
2241 assert(exceptionOop->as_register() == r0, "must match");
2242
2243 __ b(_unwind_handler_entry);
2244 }
2245
2246
2247 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2248 Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2249 Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2250
2251 switch (left->type()) {
2252 case T_INT: {
2253 switch (code) {
2254 case lir_shl: __ lslvw (dreg, lreg, count->as_register()); break;
2255 case lir_shr: __ asrvw (dreg, lreg, count->as_register()); break;
2256 case lir_ushr: __ lsrvw (dreg, lreg, count->as_register()); break;
2257 default:
2258 ShouldNotReachHere();
2259 break;
2260 }
2261 break;
2262 case T_LONG:
2263 case T_ADDRESS:
2264 case T_OBJECT:
2265 switch (code) {
2266 case lir_shl: __ lslv (dreg, lreg, count->as_register()); break;
2267 case lir_shr: __ asrv (dreg, lreg, count->as_register()); break;
2268 case lir_ushr: __ lsrv (dreg, lreg, count->as_register()); break;
2269 default:
2270 ShouldNotReachHere();
2271 break;
2272 }
2273 break;
2274 default:
2275 ShouldNotReachHere();
2276 break;
2277 }
2278 }
2279 }
2280
2281
2282 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2283 Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2284 Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2285
2286 switch (left->type()) {
2287 case T_INT: {
2288 switch (code) {
2289 case lir_shl: __ lslw (dreg, lreg, count); break;
2290 case lir_shr: __ asrw (dreg, lreg, count); break;
2291 case lir_ushr: __ lsrw (dreg, lreg, count); break;
2292 default:
2293 ShouldNotReachHere();
2294 break;
2295 }
2296 break;
2297 case T_LONG:
2298 case T_ADDRESS:
2299 case T_OBJECT:
2300 switch (code) {
2301 case lir_shl: __ lsl (dreg, lreg, count); break;
2302 case lir_shr: __ asr (dreg, lreg, count); break;
2303 case lir_ushr: __ lsr (dreg, lreg, count); break;
2304 default:
2305 ShouldNotReachHere();
2306 break;
2307 }
2308 break;
2309 default:
2310 ShouldNotReachHere();
2311 break;
2312 }
2313 }
2314 }
2315
2316
2317 void LIR_Assembler::store_parameter(Register r, int offset_from_rsp_in_words) {
2318 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2319 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2320 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2321 __ str (r, Address(sp, offset_from_rsp_in_bytes));
2322 }
2323
2324
2325 void LIR_Assembler::store_parameter(jint c, int offset_from_rsp_in_words) {
2326 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2327 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2328 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2329 __ mov (rscratch1, c);
2330 __ str (rscratch1, Address(sp, offset_from_rsp_in_bytes));
2331 }
2332
2333
2334 void LIR_Assembler::store_parameter(jobject o, int offset_from_rsp_in_words) {
2335 ShouldNotReachHere();
2336 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2337 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2338 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2339 __ lea(rscratch1, __ constant_oop_address(o));
2340 __ str(rscratch1, Address(sp, offset_from_rsp_in_bytes));
2341 }
2342
2343 void LIR_Assembler::arraycopy_inlinetype_check(Register obj, Register tmp, CodeStub* slow_path, bool is_dest, bool null_check) {
2344 if (null_check) {
2345 __ cbz(obj, *slow_path->entry());
2346 }
2347 if (is_dest) {
2348 __ test_null_free_array_oop(obj, tmp, *slow_path->entry());
2349 __ test_flat_array_oop(obj, tmp, *slow_path->entry());
2350 } else {
2351 __ test_flat_array_oop(obj, tmp, *slow_path->entry());
2352 }
2353 }
2354
2355 // This code replaces a call to arraycopy; no exception may
2356 // be thrown in this code, they must be thrown in the System.arraycopy
2357 // activation frame; we could save some checks if this would not be the case
2358 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
2359 ciArrayKlass* default_type = op->expected_type();
2360 Register src = op->src()->as_register();
2361 Register dst = op->dst()->as_register();
2362 Register src_pos = op->src_pos()->as_register();
2363 Register dst_pos = op->dst_pos()->as_register();
2364 Register length = op->length()->as_register();
2365 Register tmp = op->tmp()->as_register();
2366
2367 CodeStub* stub = op->stub();
2368 int flags = op->flags();
2369 BasicType basic_type = default_type != nullptr ? default_type->element_type()->basic_type() : T_ILLEGAL;
2370 if (is_reference_type(basic_type)) basic_type = T_OBJECT;
2371
2372 if (flags & LIR_OpArrayCopy::always_slow_path) {
2373 __ b(*stub->entry());
2374 __ bind(*stub->continuation());
2375 return;
2376 }
2377
2378 // if we don't know anything, just go through the generic arraycopy
2379 if (default_type == nullptr // || basic_type == T_OBJECT
2380 ) {
2381 Label done;
2382 assert(src == r1 && src_pos == r2, "mismatch in calling convention");
2383
2384 // Save the arguments in case the generic arraycopy fails and we
2385 // have to fall back to the JNI stub
2386 __ stp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2387 __ stp(length, src_pos, Address(sp, 2*BytesPerWord));
2388 __ str(src, Address(sp, 4*BytesPerWord));
2389
2390 address copyfunc_addr = StubRoutines::generic_arraycopy();
2391 assert(copyfunc_addr != nullptr, "generic arraycopy stub required");
2392
2393 // The arguments are in java calling convention so we shift them
2394 // to C convention
2395 assert_different_registers(c_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4);
2396 __ mov(c_rarg0, j_rarg0);
2397 assert_different_registers(c_rarg1, j_rarg2, j_rarg3, j_rarg4);
2398 __ mov(c_rarg1, j_rarg1);
2399 assert_different_registers(c_rarg2, j_rarg3, j_rarg4);
2400 __ mov(c_rarg2, j_rarg2);
2401 assert_different_registers(c_rarg3, j_rarg4);
2402 __ mov(c_rarg3, j_rarg3);
2403 __ mov(c_rarg4, j_rarg4);
2404 #ifndef PRODUCT
2405 if (PrintC1Statistics) {
2406 __ incrementw(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
2407 }
2408 #endif
2409 __ far_call(RuntimeAddress(copyfunc_addr));
2410
2411 __ cbz(r0, *stub->continuation());
2412
2413 // Reload values from the stack so they are where the stub
2414 // expects them.
2415 __ ldp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2416 __ ldp(length, src_pos, Address(sp, 2*BytesPerWord));
2417 __ ldr(src, Address(sp, 4*BytesPerWord));
2418
2419 // r0 is -1^K where K == partial copied count
2420 __ eonw(rscratch1, r0, zr);
2421 // adjust length down and src/end pos up by partial copied count
2422 __ subw(length, length, rscratch1);
2423 __ addw(src_pos, src_pos, rscratch1);
2424 __ addw(dst_pos, dst_pos, rscratch1);
2425 __ b(*stub->entry());
2426
2427 __ bind(*stub->continuation());
2428 return;
2429 }
2430
2431 // Handle inline type arrays
2432 if (flags & LIR_OpArrayCopy::src_inlinetype_check) {
2433 arraycopy_inlinetype_check(src, tmp, stub, false, (flags & LIR_OpArrayCopy::src_null_check));
2434 }
2435 if (flags & LIR_OpArrayCopy::dst_inlinetype_check) {
2436 arraycopy_inlinetype_check(dst, tmp, stub, true, (flags & LIR_OpArrayCopy::dst_null_check));
2437 }
2438
2439 assert(default_type != nullptr && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
2440
2441 int elem_size = type2aelembytes(basic_type);
2442 int scale = exact_log2(elem_size);
2443
2444 Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
2445 Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
2446
2447 // test for null
2448 if (flags & LIR_OpArrayCopy::src_null_check) {
2449 __ cbz(src, *stub->entry());
2450 }
2451 if (flags & LIR_OpArrayCopy::dst_null_check) {
2452 __ cbz(dst, *stub->entry());
2453 }
2454
2455 // If the compiler was not able to prove that exact type of the source or the destination
2456 // of the arraycopy is an array type, check at runtime if the source or the destination is
2457 // an instance type.
2458 if (flags & LIR_OpArrayCopy::type_check) {
2459 if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::dst_objarray)) {
2460 __ load_klass(tmp, dst);
2461 __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2462 __ cmpw(rscratch1, Klass::_lh_neutral_value);
2463 __ br(Assembler::GE, *stub->entry());
2464 }
2465
2466 if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::src_objarray)) {
2467 __ load_klass(tmp, src);
2468 __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2469 __ cmpw(rscratch1, Klass::_lh_neutral_value);
2470 __ br(Assembler::GE, *stub->entry());
2471 }
2472 }
2473
2474 // check if negative
2475 if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2476 __ cmpw(src_pos, 0);
2477 __ br(Assembler::LT, *stub->entry());
2478 }
2479 if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2480 __ cmpw(dst_pos, 0);
2481 __ br(Assembler::LT, *stub->entry());
2482 }
2483
2484 if (flags & LIR_OpArrayCopy::length_positive_check) {
2485 __ cmpw(length, 0);
2486 __ br(Assembler::LT, *stub->entry());
2487 }
2488
2489 if (flags & LIR_OpArrayCopy::src_range_check) {
2490 __ addw(tmp, src_pos, length);
2491 __ ldrw(rscratch1, src_length_addr);
2492 __ cmpw(tmp, rscratch1);
2493 __ br(Assembler::HI, *stub->entry());
2494 }
2495 if (flags & LIR_OpArrayCopy::dst_range_check) {
2496 __ addw(tmp, dst_pos, length);
2497 __ ldrw(rscratch1, dst_length_addr);
2498 __ cmpw(tmp, rscratch1);
2499 __ br(Assembler::HI, *stub->entry());
2500 }
2501
2502 if (flags & LIR_OpArrayCopy::type_check) {
2503 // We don't know the array types are compatible
2504 if (basic_type != T_OBJECT) {
2505 // Simple test for basic type arrays
2506 __ cmp_klasses_from_objects(src, dst, tmp, rscratch1);
2507 __ br(Assembler::NE, *stub->entry());
2508 } else {
2509 // For object arrays, if src is a sub class of dst then we can
2510 // safely do the copy.
2511 Label cont, slow;
2512
2513 #define PUSH(r1, r2) \
2514 stp(r1, r2, __ pre(sp, -2 * wordSize));
2515
2516 #define POP(r1, r2) \
2517 ldp(r1, r2, __ post(sp, 2 * wordSize));
2518
2519 __ PUSH(src, dst);
2520
2521 __ load_klass(src, src);
2522 __ load_klass(dst, dst);
2523
2524 __ check_klass_subtype_fast_path(src, dst, tmp, &cont, &slow, nullptr);
2525
2526 __ PUSH(src, dst);
2527 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
2528 __ POP(src, dst);
2529
2530 __ cbnz(src, cont);
2531
2532 __ bind(slow);
2533 __ POP(src, dst);
2534
2535 address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2536 if (copyfunc_addr != nullptr) { // use stub if available
2537 // src is not a sub class of dst so we have to do a
2538 // per-element check.
2539
2540 int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2541 if ((flags & mask) != mask) {
2542 // Check that at least both of them object arrays.
2543 assert(flags & mask, "one of the two should be known to be an object array");
2544
2545 if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2546 __ load_klass(tmp, src);
2547 } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2548 __ load_klass(tmp, dst);
2549 }
2550 int lh_offset = in_bytes(Klass::layout_helper_offset());
2551 Address klass_lh_addr(tmp, lh_offset);
2552 jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2553 __ ldrw(rscratch1, klass_lh_addr);
2554 __ mov(rscratch2, objArray_lh);
2555 __ eorw(rscratch1, rscratch1, rscratch2);
2556 __ cbnzw(rscratch1, *stub->entry());
2557 }
2558
2559 // Spill because stubs can use any register they like and it's
2560 // easier to restore just those that we care about.
2561 __ stp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2562 __ stp(length, src_pos, Address(sp, 2*BytesPerWord));
2563 __ str(src, Address(sp, 4*BytesPerWord));
2564
2565 __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2566 __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2567 assert_different_registers(c_rarg0, dst, dst_pos, length);
2568 __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2569 __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2570 assert_different_registers(c_rarg1, dst, length);
2571 __ uxtw(c_rarg2, length);
2572 assert_different_registers(c_rarg2, dst);
2573
2574 __ load_klass(c_rarg4, dst);
2575 __ ldr(c_rarg4, Address(c_rarg4, ObjArrayKlass::element_klass_offset()));
2576 __ ldrw(c_rarg3, Address(c_rarg4, Klass::super_check_offset_offset()));
2577 __ far_call(RuntimeAddress(copyfunc_addr));
2578
2579 #ifndef PRODUCT
2580 if (PrintC1Statistics) {
2581 Label failed;
2582 __ cbnz(r0, failed);
2583 __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_cnt));
2584 __ bind(failed);
2585 }
2586 #endif
2587
2588 __ cbz(r0, *stub->continuation());
2589
2590 #ifndef PRODUCT
2591 if (PrintC1Statistics) {
2592 __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_attempt_cnt));
2593 }
2594 #endif
2595 assert_different_registers(dst, dst_pos, length, src_pos, src, r0, rscratch1);
2596
2597 // Restore previously spilled arguments
2598 __ ldp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2599 __ ldp(length, src_pos, Address(sp, 2*BytesPerWord));
2600 __ ldr(src, Address(sp, 4*BytesPerWord));
2601
2602 // return value is -1^K where K is partial copied count
2603 __ eonw(rscratch1, r0, zr);
2604 // adjust length down and src/end pos up by partial copied count
2605 __ subw(length, length, rscratch1);
2606 __ addw(src_pos, src_pos, rscratch1);
2607 __ addw(dst_pos, dst_pos, rscratch1);
2608 }
2609
2610 __ b(*stub->entry());
2611
2612 __ bind(cont);
2613 __ POP(src, dst);
2614 }
2615 }
2616
2617 #ifdef ASSERT
2618 if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2619 // Sanity check the known type with the incoming class. For the
2620 // primitive case the types must match exactly with src.klass and
2621 // dst.klass each exactly matching the default type. For the
2622 // object array case, if no type check is needed then either the
2623 // dst type is exactly the expected type and the src type is a
2624 // subtype which we can't check or src is the same array as dst
2625 // but not necessarily exactly of type default_type.
2626 Label known_ok, halt;
2627 __ mov_metadata(tmp, default_type->constant_encoding());
2628
2629 if (basic_type != T_OBJECT) {
2630 __ cmp_klass(dst, tmp, rscratch1);
2631 __ br(Assembler::NE, halt);
2632 __ cmp_klass(src, tmp, rscratch1);
2633 __ br(Assembler::EQ, known_ok);
2634 } else {
2635 __ cmp_klass(dst, tmp, rscratch1);
2636 __ br(Assembler::EQ, known_ok);
2637 __ cmp(src, dst);
2638 __ br(Assembler::EQ, known_ok);
2639 }
2640 __ bind(halt);
2641 __ stop("incorrect type information in arraycopy");
2642 __ bind(known_ok);
2643 }
2644 #endif
2645
2646 #ifndef PRODUCT
2647 if (PrintC1Statistics) {
2648 __ incrementw(ExternalAddress(Runtime1::arraycopy_count_address(basic_type)));
2649 }
2650 #endif
2651
2652 __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2653 __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2654 assert_different_registers(c_rarg0, dst, dst_pos, length);
2655 __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2656 __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2657 assert_different_registers(c_rarg1, dst, length);
2658 __ uxtw(c_rarg2, length);
2659 assert_different_registers(c_rarg2, dst);
2660
2661 bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2662 bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2663 const char *name;
2664 address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2665
2666 CodeBlob *cb = CodeCache::find_blob(entry);
2667 if (cb) {
2668 __ far_call(RuntimeAddress(entry));
2669 } else {
2670 __ call_VM_leaf(entry, 3);
2671 }
2672
2673 if (stub != nullptr) {
2674 __ bind(*stub->continuation());
2675 }
2676 }
2677
2678
2679
2680
2681 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2682 Register obj = op->obj_opr()->as_register(); // may not be an oop
2683 Register hdr = op->hdr_opr()->as_register();
2684 Register lock = op->lock_opr()->as_register();
2685 Register temp = op->scratch_opr()->as_register();
2686 if (op->code() == lir_lock) {
2687 // add debug info for NullPointerException only if one is possible
2688 int null_check_offset = __ lock_object(hdr, obj, lock, temp, *op->stub()->entry());
2689 if (op->info() != nullptr) {
2690 add_debug_info_for_null_check(null_check_offset, op->info());
2691 }
2692 // done
2693 } else if (op->code() == lir_unlock) {
2694 __ unlock_object(hdr, obj, lock, temp, *op->stub()->entry());
2695 } else {
2696 Unimplemented();
2697 }
2698 __ bind(*op->stub()->continuation());
2699 }
2700
2701 void LIR_Assembler::emit_load_klass(LIR_OpLoadKlass* op) {
2702 Register obj = op->obj()->as_pointer_register();
2703 Register result = op->result_opr()->as_pointer_register();
2704
2705 CodeEmitInfo* info = op->info();
2706 if (info != nullptr) {
2707 add_debug_info_for_null_check_here(info);
2708 }
2709
2710 __ load_klass(result, obj);
2711 }
2712
2713 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2714 ciMethod* method = op->profiled_method();
2715 int bci = op->profiled_bci();
2716 ciMethod* callee = op->profiled_callee();
2717
2718 // Update counter for all call types
2719 ciMethodData* md = method->method_data_or_null();
2720 assert(md != nullptr, "Sanity");
2721 ciProfileData* data = md->bci_to_data(bci);
2722 assert(data != nullptr && data->is_CounterData(), "need CounterData for calls");
2723 assert(op->mdo()->is_single_cpu(), "mdo must be allocated");
2724 Register mdo = op->mdo()->as_register();
2725 __ mov_metadata(mdo, md->constant_encoding());
2726 Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
2727 // Perform additional virtual call profiling for invokevirtual and
2728 // invokeinterface bytecodes
2729 if (op->should_profile_receiver_type()) {
2730 assert(op->recv()->is_single_cpu(), "recv must be allocated");
2731 Register recv = op->recv()->as_register();
2732 assert_different_registers(mdo, recv);
2733 assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2734 ciKlass* known_klass = op->known_holder();
2735 if (C1OptimizeVirtualCallProfiling && known_klass != nullptr) {
2736 // We know the type that will be seen at this call site; we can
2737 // statically update the MethodData* rather than needing to do
2738 // dynamic tests on the receiver type.
2739 ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2740 for (uint i = 0; i < VirtualCallData::row_limit(); i++) {
2741 ciKlass* receiver = vc_data->receiver(i);
2742 if (known_klass->equals(receiver)) {
2743 Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2744 __ addptr(data_addr, DataLayout::counter_increment);
2745 return;
2746 }
2747 }
2748 // Receiver type is not found in profile data.
2749 // Fall back to runtime helper to handle the rest at runtime.
2750 __ mov_metadata(recv, known_klass->constant_encoding());
2751 } else {
2752 __ load_klass(recv, recv);
2753 }
2754 type_profile_helper(mdo, md, data, recv);
2755 } else {
2756 // Static call
2757 __ addptr(counter_addr, DataLayout::counter_increment);
2758 }
2759 }
2760
2761
2762 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst) {
2763 __ lea(dst->as_register(), frame_map()->address_for_monitor_lock(monitor_no));
2764 }
2765
2766 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
2767 assert(op->crc()->is_single_cpu(), "crc must be register");
2768 assert(op->val()->is_single_cpu(), "byte value must be register");
2769 assert(op->result_opr()->is_single_cpu(), "result must be register");
2770 Register crc = op->crc()->as_register();
2771 Register val = op->val()->as_register();
2772 Register res = op->result_opr()->as_register();
2773
2774 assert_different_registers(val, crc, res);
2775 uint64_t offset;
2776 __ adrp(res, ExternalAddress(StubRoutines::crc_table_addr()), offset);
2777 __ add(res, res, offset);
2778
2779 __ mvnw(crc, crc); // ~crc
2780 __ update_byte_crc32(crc, val, res);
2781 __ mvnw(res, crc); // ~crc
2782 }
2783
2784 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2785 COMMENT("emit_profile_type {");
2786 Register obj = op->obj()->as_register();
2787 Register tmp = op->tmp()->as_pointer_register();
2788 Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2789 ciKlass* exact_klass = op->exact_klass();
2790 intptr_t current_klass = op->current_klass();
2791 bool not_null = op->not_null();
2792 bool no_conflict = op->no_conflict();
2793
2794 Label update, next, none;
2795
2796 bool do_null = !not_null;
2797 bool exact_klass_set = exact_klass != nullptr && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2798 bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
2799
2800 assert(do_null || do_update, "why are we here?");
2801 assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
2802 assert(mdo_addr.base() != rscratch1, "wrong register");
2803
2804 __ verify_oop(obj);
2805
2806 if (tmp != obj) {
2807 assert_different_registers(obj, tmp, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2808 __ mov(tmp, obj);
2809 } else {
2810 assert_different_registers(obj, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2811 }
2812 if (do_null) {
2813 __ cbnz(tmp, update);
2814 if (!TypeEntries::was_null_seen(current_klass)) {
2815 __ ldr(rscratch2, mdo_addr);
2816 __ orr(rscratch2, rscratch2, TypeEntries::null_seen);
2817 __ str(rscratch2, mdo_addr);
2818 }
2819 if (do_update) {
2820 #ifndef ASSERT
2821 __ b(next);
2822 }
2823 #else
2824 __ b(next);
2825 }
2826 } else {
2827 __ cbnz(tmp, update);
2828 __ stop("unexpected null obj");
2829 #endif
2830 }
2831
2832 __ bind(update);
2833
2834 if (do_update) {
2835 #ifdef ASSERT
2836 if (exact_klass != nullptr) {
2837 Label ok;
2838 __ load_klass(tmp, tmp);
2839 __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2840 __ eor(rscratch1, tmp, rscratch1);
2841 __ cbz(rscratch1, ok);
2842 __ stop("exact klass and actual klass differ");
2843 __ bind(ok);
2844 }
2845 #endif
2846 if (!no_conflict) {
2847 if (exact_klass == nullptr || TypeEntries::is_type_none(current_klass)) {
2848 if (exact_klass != nullptr) {
2849 __ mov_metadata(tmp, exact_klass->constant_encoding());
2850 } else {
2851 __ load_klass(tmp, tmp);
2852 }
2853
2854 __ ldr(rscratch2, mdo_addr);
2855 __ eor(tmp, tmp, rscratch2);
2856 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2857 // klass seen before, nothing to do. The unknown bit may have been
2858 // set already but no need to check.
2859 __ cbz(rscratch1, next);
2860
2861 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2862
2863 if (TypeEntries::is_type_none(current_klass)) {
2864 __ cbz(rscratch2, none);
2865 __ cmp(rscratch2, (u1)TypeEntries::null_seen);
2866 __ br(Assembler::EQ, none);
2867 // There is a chance that the checks above
2868 // fail if another thread has just set the
2869 // profiling to this obj's klass
2870 __ dmb(Assembler::ISHLD);
2871 __ eor(tmp, tmp, rscratch2); // get back original value before XOR
2872 __ ldr(rscratch2, mdo_addr);
2873 __ eor(tmp, tmp, rscratch2);
2874 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2875 __ cbz(rscratch1, next);
2876 }
2877 } else {
2878 assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2879 ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
2880
2881 __ ldr(tmp, mdo_addr);
2882 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2883 }
2884
2885 // different than before. Cannot keep accurate profile.
2886 __ ldr(rscratch2, mdo_addr);
2887 __ orr(rscratch2, rscratch2, TypeEntries::type_unknown);
2888 __ str(rscratch2, mdo_addr);
2889
2890 if (TypeEntries::is_type_none(current_klass)) {
2891 __ b(next);
2892
2893 __ bind(none);
2894 // first time here. Set profile type.
2895 __ str(tmp, mdo_addr);
2896 #ifdef ASSERT
2897 __ andr(tmp, tmp, TypeEntries::type_mask);
2898 __ verify_klass_ptr(tmp);
2899 #endif
2900 }
2901 } else {
2902 // There's a single possible klass at this profile point
2903 assert(exact_klass != nullptr, "should be");
2904 if (TypeEntries::is_type_none(current_klass)) {
2905 __ mov_metadata(tmp, exact_klass->constant_encoding());
2906 __ ldr(rscratch2, mdo_addr);
2907 __ eor(tmp, tmp, rscratch2);
2908 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2909 __ cbz(rscratch1, next);
2910 #ifdef ASSERT
2911 {
2912 Label ok;
2913 __ ldr(rscratch1, mdo_addr);
2914 __ cbz(rscratch1, ok);
2915 __ cmp(rscratch1, (u1)TypeEntries::null_seen);
2916 __ br(Assembler::EQ, ok);
2917 // may have been set by another thread
2918 __ dmb(Assembler::ISHLD);
2919 __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2920 __ ldr(rscratch2, mdo_addr);
2921 __ eor(rscratch2, rscratch1, rscratch2);
2922 __ andr(rscratch2, rscratch2, TypeEntries::type_mask);
2923 __ cbz(rscratch2, ok);
2924
2925 __ stop("unexpected profiling mismatch");
2926 __ bind(ok);
2927 }
2928 #endif
2929 // first time here. Set profile type.
2930 __ str(tmp, mdo_addr);
2931 #ifdef ASSERT
2932 __ andr(tmp, tmp, TypeEntries::type_mask);
2933 __ verify_klass_ptr(tmp);
2934 #endif
2935 } else {
2936 assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2937 ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
2938
2939 __ ldr(tmp, mdo_addr);
2940 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2941
2942 __ orr(tmp, tmp, TypeEntries::type_unknown);
2943 __ str(tmp, mdo_addr);
2944 // FIXME: Write barrier needed here?
2945 }
2946 }
2947
2948 __ bind(next);
2949 }
2950 COMMENT("} emit_profile_type");
2951 }
2952
2953 void LIR_Assembler::emit_profile_inline_type(LIR_OpProfileInlineType* op) {
2954 Register obj = op->obj()->as_register();
2955 Register tmp = op->tmp()->as_pointer_register();
2956 bool not_null = op->not_null();
2957 int flag = op->flag();
2958
2959 Label not_inline_type;
2960 if (!not_null) {
2961 __ cbz(obj, not_inline_type);
2962 }
2963
2964 __ test_oop_is_not_inline_type(obj, tmp, not_inline_type);
2965
2966 Address mdo_addr = as_Address(op->mdp()->as_address_ptr(), rscratch2);
2967 __ ldrb(rscratch1, mdo_addr);
2968 __ orr(rscratch1, rscratch1, flag);
2969 __ strb(rscratch1, mdo_addr);
2970
2971 __ bind(not_inline_type);
2972 }
2973
2974 void LIR_Assembler::align_backward_branch_target() {
2975 }
2976
2977
2978 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
2979 // tmp must be unused
2980 assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2981
2982 if (left->is_single_cpu()) {
2983 assert(dest->is_single_cpu(), "expect single result reg");
2984 __ negw(dest->as_register(), left->as_register());
2985 } else if (left->is_double_cpu()) {
2986 assert(dest->is_double_cpu(), "expect double result reg");
2987 __ neg(dest->as_register_lo(), left->as_register_lo());
2988 } else if (left->is_single_fpu()) {
2989 assert(dest->is_single_fpu(), "expect single float result reg");
2990 __ fnegs(dest->as_float_reg(), left->as_float_reg());
2991 } else {
2992 assert(left->is_double_fpu(), "expect double float operand reg");
2993 assert(dest->is_double_fpu(), "expect double float result reg");
2994 __ fnegd(dest->as_double_reg(), left->as_double_reg());
2995 }
2996 }
2997
2998
2999 void LIR_Assembler::leal(LIR_Opr addr, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
3000 if (patch_code != lir_patch_none) {
3001 deoptimize_trap(info);
3002 return;
3003 }
3004
3005 __ lea(dest->as_pointer_register(), as_Address(addr->as_address_ptr()));
3006 }
3007
3008
3009 void LIR_Assembler::rt_call(LIR_Opr result, address dest, const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
3010 assert(!tmp->is_valid(), "don't need temporary");
3011
3012 CodeBlob *cb = CodeCache::find_blob(dest);
3013 if (cb) {
3014 __ far_call(RuntimeAddress(dest));
3015 } else {
3016 __ mov(rscratch1, RuntimeAddress(dest));
3017 __ blr(rscratch1);
3018 }
3019
3020 if (info != nullptr) {
3021 add_call_info_here(info);
3022 }
3023 __ post_call_nop();
3024 }
3025
3026 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
3027 if (src->is_address()) {
3028 mem2reg(src, dest, type, lir_patch_none, info, /*wide*/false, /*is_volatile*/true);
3029 } else if (dest->is_address()) {
3030 move_op(src, dest, type, lir_patch_none, info, /*wide*/false);
3031 } else {
3032 ShouldNotReachHere();
3033 }
3034 }
3035
3036 #ifdef ASSERT
3037 // emit run-time assertion
3038 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
3039 assert(op->code() == lir_assert, "must be");
3040
3041 if (op->in_opr1()->is_valid()) {
3042 assert(op->in_opr2()->is_valid(), "both operands must be valid");
3043 comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
3044 } else {
3045 assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
3046 assert(op->condition() == lir_cond_always, "no other conditions allowed");
3047 }
3048
3049 Label ok;
3050 if (op->condition() != lir_cond_always) {
3051 Assembler::Condition acond = Assembler::AL;
3052 switch (op->condition()) {
3053 case lir_cond_equal: acond = Assembler::EQ; break;
3054 case lir_cond_notEqual: acond = Assembler::NE; break;
3055 case lir_cond_less: acond = Assembler::LT; break;
3056 case lir_cond_lessEqual: acond = Assembler::LE; break;
3057 case lir_cond_greaterEqual: acond = Assembler::GE; break;
3058 case lir_cond_greater: acond = Assembler::GT; break;
3059 case lir_cond_belowEqual: acond = Assembler::LS; break;
3060 case lir_cond_aboveEqual: acond = Assembler::HS; break;
3061 default: ShouldNotReachHere();
3062 }
3063 __ br(acond, ok);
3064 }
3065 if (op->halt()) {
3066 const char* str = __ code_string(op->msg());
3067 __ stop(str);
3068 } else {
3069 breakpoint();
3070 }
3071 __ bind(ok);
3072 }
3073 #endif
3074
3075 #ifndef PRODUCT
3076 #define COMMENT(x) do { __ block_comment(x); } while (0)
3077 #else
3078 #define COMMENT(x)
3079 #endif
3080
3081 void LIR_Assembler::membar() {
3082 COMMENT("membar");
3083 __ membar(MacroAssembler::AnyAny);
3084 }
3085
3086 void LIR_Assembler::membar_acquire() {
3087 __ membar(Assembler::LoadLoad|Assembler::LoadStore);
3088 }
3089
3090 void LIR_Assembler::membar_release() {
3091 __ membar(Assembler::LoadStore|Assembler::StoreStore);
3092 }
3093
3094 void LIR_Assembler::membar_loadload() {
3095 __ membar(Assembler::LoadLoad);
3096 }
3097
3098 void LIR_Assembler::membar_storestore() {
3099 __ membar(MacroAssembler::StoreStore);
3100 }
3101
3102 void LIR_Assembler::membar_loadstore() { __ membar(MacroAssembler::LoadStore); }
3103
3104 void LIR_Assembler::membar_storeload() { __ membar(MacroAssembler::StoreLoad); }
3105
3106 void LIR_Assembler::on_spin_wait() {
3107 __ spin_wait();
3108 }
3109
3110 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
3111 __ mov(result_reg->as_register(), rthread);
3112 }
3113
3114 void LIR_Assembler::check_orig_pc() {
3115 __ ldr(rscratch2, frame_map()->address_for_orig_pc_addr());
3116 __ cmp(rscratch2, (u1)NULL_WORD);
3117 }
3118
3119 void LIR_Assembler::peephole(LIR_List *lir) {
3120 #if 0
3121 if (tableswitch_count >= max_tableswitches)
3122 return;
3123
3124 /*
3125 This finite-state automaton recognizes sequences of compare-and-
3126 branch instructions. We will turn them into a tableswitch. You
3127 could argue that C1 really shouldn't be doing this sort of
3128 optimization, but without it the code is really horrible.
3129 */
3130
3131 enum { start_s, cmp1_s, beq_s, cmp_s } state;
3132 int first_key, last_key = -2147483648;
3133 int next_key = 0;
3134 int start_insn = -1;
3135 int last_insn = -1;
3136 Register reg = noreg;
3137 LIR_Opr reg_opr;
3138 state = start_s;
3139
3140 LIR_OpList* inst = lir->instructions_list();
3141 for (int i = 0; i < inst->length(); i++) {
3142 LIR_Op* op = inst->at(i);
3143 switch (state) {
3144 case start_s:
3145 first_key = -1;
3146 start_insn = i;
3147 switch (op->code()) {
3148 case lir_cmp:
3149 LIR_Opr opr1 = op->as_Op2()->in_opr1();
3150 LIR_Opr opr2 = op->as_Op2()->in_opr2();
3151 if (opr1->is_cpu_register() && opr1->is_single_cpu()
3152 && opr2->is_constant()
3153 && opr2->type() == T_INT) {
3154 reg_opr = opr1;
3155 reg = opr1->as_register();
3156 first_key = opr2->as_constant_ptr()->as_jint();
3157 next_key = first_key + 1;
3158 state = cmp_s;
3159 goto next_state;
3160 }
3161 break;
3162 }
3163 break;
3164 case cmp_s:
3165 switch (op->code()) {
3166 case lir_branch:
3167 if (op->as_OpBranch()->cond() == lir_cond_equal) {
3168 state = beq_s;
3169 last_insn = i;
3170 goto next_state;
3171 }
3172 }
3173 state = start_s;
3174 break;
3175 case beq_s:
3176 switch (op->code()) {
3177 case lir_cmp: {
3178 LIR_Opr opr1 = op->as_Op2()->in_opr1();
3179 LIR_Opr opr2 = op->as_Op2()->in_opr2();
3180 if (opr1->is_cpu_register() && opr1->is_single_cpu()
3181 && opr1->as_register() == reg
3182 && opr2->is_constant()
3183 && opr2->type() == T_INT
3184 && opr2->as_constant_ptr()->as_jint() == next_key) {
3185 last_key = next_key;
3186 next_key++;
3187 state = cmp_s;
3188 goto next_state;
3189 }
3190 }
3191 }
3192 last_key = next_key;
3193 state = start_s;
3194 break;
3195 default:
3196 assert(false, "impossible state");
3197 }
3198 if (state == start_s) {
3199 if (first_key < last_key - 5L && reg != noreg) {
3200 {
3201 // printf("found run register %d starting at insn %d low value %d high value %d\n",
3202 // reg->encoding(),
3203 // start_insn, first_key, last_key);
3204 // for (int i = 0; i < inst->length(); i++) {
3205 // inst->at(i)->print();
3206 // tty->print("\n");
3207 // }
3208 // tty->print("\n");
3209 }
3210
3211 struct tableswitch *sw = &switches[tableswitch_count];
3212 sw->_insn_index = start_insn, sw->_first_key = first_key,
3213 sw->_last_key = last_key, sw->_reg = reg;
3214 inst->insert_before(last_insn + 1, new LIR_OpLabel(&sw->_after));
3215 {
3216 // Insert the new table of branches
3217 int offset = last_insn;
3218 for (int n = first_key; n < last_key; n++) {
3219 inst->insert_before
3220 (last_insn + 1,
3221 new LIR_OpBranch(lir_cond_always, T_ILLEGAL,
3222 inst->at(offset)->as_OpBranch()->label()));
3223 offset -= 2, i++;
3224 }
3225 }
3226 // Delete all the old compare-and-branch instructions
3227 for (int n = first_key; n < last_key; n++) {
3228 inst->remove_at(start_insn);
3229 inst->remove_at(start_insn);
3230 }
3231 // Insert the tableswitch instruction
3232 inst->insert_before(start_insn,
3233 new LIR_Op2(lir_cmp, lir_cond_always,
3234 LIR_OprFact::intConst(tableswitch_count),
3235 reg_opr));
3236 inst->insert_before(start_insn + 1, new LIR_OpLabel(&sw->_branches));
3237 tableswitch_count++;
3238 }
3239 reg = noreg;
3240 last_key = -2147483648;
3241 }
3242 next_state:
3243 ;
3244 }
3245 #endif
3246 }
3247
3248 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp_op) {
3249 Address addr = as_Address(src->as_address_ptr());
3250 BasicType type = src->type();
3251 bool is_oop = is_reference_type(type);
3252
3253 void (MacroAssembler::* add)(Register prev, RegisterOrConstant incr, Register addr);
3254 void (MacroAssembler::* xchg)(Register prev, Register newv, Register addr);
3255
3256 switch(type) {
3257 case T_INT:
3258 xchg = &MacroAssembler::atomic_xchgalw;
3259 add = &MacroAssembler::atomic_addalw;
3260 break;
3261 case T_LONG:
3262 xchg = &MacroAssembler::atomic_xchgal;
3263 add = &MacroAssembler::atomic_addal;
3264 break;
3265 case T_OBJECT:
3266 case T_ARRAY:
3267 if (UseCompressedOops) {
3268 xchg = &MacroAssembler::atomic_xchgalw;
3269 add = &MacroAssembler::atomic_addalw;
3270 } else {
3271 xchg = &MacroAssembler::atomic_xchgal;
3272 add = &MacroAssembler::atomic_addal;
3273 }
3274 break;
3275 default:
3276 ShouldNotReachHere();
3277 xchg = &MacroAssembler::atomic_xchgal;
3278 add = &MacroAssembler::atomic_addal; // unreachable
3279 }
3280
3281 switch (code) {
3282 case lir_xadd:
3283 {
3284 RegisterOrConstant inc;
3285 Register tmp = as_reg(tmp_op);
3286 Register dst = as_reg(dest);
3287 if (data->is_constant()) {
3288 inc = RegisterOrConstant(as_long(data));
3289 assert_different_registers(dst, addr.base(), tmp,
3290 rscratch1, rscratch2);
3291 } else {
3292 inc = RegisterOrConstant(as_reg(data));
3293 assert_different_registers(inc.as_register(), dst, addr.base(), tmp,
3294 rscratch1, rscratch2);
3295 }
3296 __ lea(tmp, addr);
3297 (_masm->*add)(dst, inc, tmp);
3298 break;
3299 }
3300 case lir_xchg:
3301 {
3302 Register tmp = tmp_op->as_register();
3303 Register obj = as_reg(data);
3304 Register dst = as_reg(dest);
3305 if (is_oop && UseCompressedOops) {
3306 __ encode_heap_oop(rscratch2, obj);
3307 obj = rscratch2;
3308 }
3309 assert_different_registers(obj, addr.base(), tmp, rscratch1);
3310 assert_different_registers(dst, addr.base(), tmp, rscratch1);
3311 __ lea(tmp, addr);
3312 (_masm->*xchg)(dst, obj, tmp);
3313 if (is_oop && UseCompressedOops) {
3314 __ decode_heap_oop(dst);
3315 }
3316 }
3317 break;
3318 default:
3319 ShouldNotReachHere();
3320 }
3321 }
3322
3323 #undef __