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 an 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 an 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 __ mov_metadata(rscratch1, k_refined->constant_encoding());
1444 __ cmp(klass_RInfo, rscratch1);
1445 } else {
1446 __ cmp(klass_RInfo, k_RInfo);
1447 }
1448 __ br(Assembler::EQ, *success_target);
1449
1450 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1451 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1452 __ ldr(klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1453 // result is a boolean
1454 __ cbzw(klass_RInfo, *failure_target);
1455 // successful cast, fall through to profile or jump
1456 }
1457 } else {
1458 // perform the fast part of the checking logic
1459 __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1460 // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1461 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1462 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1463 __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1464 // result is a boolean
1465 __ cbz(k_RInfo, *failure_target);
1466 // successful cast, fall through to profile or jump
1467 }
1468 }
1469 __ b(*success);
1470 }
1471
1472
1473 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
1474 const bool should_profile = op->should_profile();
1475
1476 LIR_Code code = op->code();
1477 if (code == lir_store_check) {
1478 Register value = op->object()->as_register();
1479 Register array = op->array()->as_register();
1480 Register k_RInfo = op->tmp1()->as_register();
1481 Register klass_RInfo = op->tmp2()->as_register();
1482 Register Rtmp1 = op->tmp3()->as_register();
1483
1484 CodeStub* stub = op->stub();
1485
1486 // check if it needs to be profiled
1487 ciMethodData* md;
1488 ciProfileData* data;
1489
1490 if (should_profile) {
1491 ciMethod* method = op->profiled_method();
1492 assert(method != nullptr, "Should have method");
1493 int bci = op->profiled_bci();
1494 md = method->method_data_or_null();
1495 assert(md != nullptr, "Sanity");
1496 data = md->bci_to_data(bci);
1497 assert(data != nullptr, "need data for type check");
1498 assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1499 }
1500 Label done;
1501 Label* success_target = &done;
1502 Label* failure_target = stub->entry();
1503
1504 if (should_profile) {
1505 Label not_null;
1506 Register mdo = klass_RInfo;
1507 __ mov_metadata(mdo, md->constant_encoding());
1508 __ cbnz(value, not_null);
1509 // Object is null; update MDO and exit
1510 Address data_addr
1511 = __ form_address(rscratch2, mdo,
1512 md->byte_offset_of_slot(data, DataLayout::flags_offset()), 0);
1513 __ ldrb(rscratch1, data_addr);
1514 __ orr(rscratch1, rscratch1, BitData::null_seen_byte_constant());
1515 __ strb(rscratch1, data_addr);
1516 __ b(done);
1517 __ bind(not_null);
1518
1519 Register recv = k_RInfo;
1520 __ load_klass(recv, value);
1521 type_profile_helper(mdo, md, data, recv);
1522 } else {
1523 __ cbz(value, done);
1524 }
1525
1526 add_debug_info_for_null_check_here(op->info_for_exception());
1527 __ load_klass(k_RInfo, array);
1528 __ load_klass(klass_RInfo, value);
1529
1530 // get instance klass (it's already uncompressed)
1531 __ ldr(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
1532 // perform the fast part of the checking logic
1533 __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1534 // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1535 __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1536 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
1537 __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1538 // result is a boolean
1539 __ cbzw(k_RInfo, *failure_target);
1540 // fall through to the success case
1541
1542 __ bind(done);
1543 } else if (code == lir_checkcast) {
1544 Register obj = op->object()->as_register();
1545 Register dst = op->result_opr()->as_register();
1546 Label success;
1547 emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
1548 __ bind(success);
1549 if (dst != obj) {
1550 __ mov(dst, obj);
1551 }
1552 } else if (code == lir_instanceof) {
1553 Register obj = op->object()->as_register();
1554 Register dst = op->result_opr()->as_register();
1555 Label success, failure, done;
1556 emit_typecheck_helper(op, &success, &failure, &failure);
1557 __ bind(failure);
1558 __ mov(dst, zr);
1559 __ b(done);
1560 __ bind(success);
1561 __ mov(dst, 1);
1562 __ bind(done);
1563 } else {
1564 ShouldNotReachHere();
1565 }
1566 }
1567
1568 void LIR_Assembler::emit_opFlattenedArrayCheck(LIR_OpFlattenedArrayCheck* op) {
1569 // We are loading/storing from/to an array that *may* be a flat array (the
1570 // declared type is Object[], abstract[], interface[] or VT.ref[]).
1571 // If this array is a flat array, take the slow path.
1572 __ test_flat_array_oop(op->array()->as_register(), op->tmp()->as_register(), *op->stub()->entry());
1573 }
1574
1575 void LIR_Assembler::emit_opNullFreeArrayCheck(LIR_OpNullFreeArrayCheck* op) {
1576 // We are storing into an array that *may* be null-free (the declared type is
1577 // Object[], abstract[], interface[] or VT.ref[]).
1578 Label test_mark_word;
1579 Register tmp = op->tmp()->as_register();
1580 __ ldr(tmp, Address(op->array()->as_register(), oopDesc::mark_offset_in_bytes()));
1581 __ tst(tmp, markWord::unlocked_value);
1582 __ br(Assembler::NE, test_mark_word);
1583 __ load_prototype_header(tmp, op->array()->as_register());
1584 __ bind(test_mark_word);
1585 __ tst(tmp, markWord::null_free_array_bit_in_place);
1586 }
1587
1588 void LIR_Assembler::emit_opSubstitutabilityCheck(LIR_OpSubstitutabilityCheck* op) {
1589 Label L_oops_equal;
1590 Label L_oops_not_equal;
1591 Label L_end;
1592
1593 Register left = op->left()->as_register();
1594 Register right = op->right()->as_register();
1595
1596 __ cmp(left, right);
1597 __ br(Assembler::EQ, L_oops_equal);
1598
1599 // (1) Null check -- if one of the operands is null, the other must not be null (because
1600 // the two references are not equal), so they are not substitutable,
1601 __ cbz(left, L_oops_not_equal);
1602 __ cbz(right, L_oops_not_equal);
1603
1604 ciKlass* left_klass = op->left_klass();
1605 ciKlass* right_klass = op->right_klass();
1606
1607 // (2) Inline type check -- if either of the operands is not a inline type,
1608 // they are not substitutable. We do this only if we are not sure that the
1609 // operands are inline type
1610 if ((left_klass == nullptr || right_klass == nullptr) ||// The klass is still unloaded, or came from a Phi node.
1611 !left_klass->is_inlinetype() || !right_klass->is_inlinetype()) {
1612 Register tmp1 = op->tmp1()->as_register();
1613 Register tmp2 = op->tmp2()->as_register();
1614 __ mov(tmp1, markWord::inline_type_pattern);
1615 __ ldr(tmp2, Address(left, oopDesc::mark_offset_in_bytes()));
1616 __ andr(tmp1, tmp1, tmp2);
1617 __ ldr(tmp2, Address(right, oopDesc::mark_offset_in_bytes()));
1618 __ andr(tmp1, tmp1, tmp2);
1619 __ cmp(tmp1, (u1)markWord::inline_type_pattern);
1620 __ br(Assembler::NE, L_oops_not_equal);
1621 }
1622
1623 // (3) Same klass check: if the operands are of different klasses, they are not substitutable.
1624 if (left_klass != nullptr && left_klass->is_inlinetype() && left_klass == right_klass) {
1625 // No need to load klass -- the operands are statically known to be the same inline klass.
1626 __ b(*op->stub()->entry());
1627 } else {
1628 Register tmp1 = op->tmp1()->as_register();
1629 Register tmp2 = op->tmp2()->as_register();
1630 __ cmp_klasses_from_objects(left, right, tmp1, tmp2);
1631 __ br(Assembler::EQ, *op->stub()->entry()); // same klass -> do slow check
1632 // fall through to L_oops_not_equal
1633 }
1634
1635 __ bind(L_oops_not_equal);
1636 move(op->not_equal_result(), op->result_opr());
1637 __ b(L_end);
1638
1639 __ bind(L_oops_equal);
1640 move(op->equal_result(), op->result_opr());
1641 __ b(L_end);
1642
1643 // We've returned from the stub. R0 contains 0x0 IFF the two
1644 // operands are not substitutable. (Don't compare against 0x1 in case the
1645 // C compiler is naughty)
1646 __ bind(*op->stub()->continuation());
1647 __ cbz(r0, L_oops_not_equal); // (call_stub() == 0x0) -> not_equal
1648 move(op->equal_result(), op->result_opr()); // (call_stub() != 0x0) -> equal
1649 // fall-through
1650 __ bind(L_end);
1651 }
1652
1653
1654 void LIR_Assembler::casw(Register addr, Register newval, Register cmpval) {
1655 __ cmpxchg(addr, cmpval, newval, Assembler::word, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1656 __ cset(rscratch1, Assembler::NE);
1657 __ membar(__ AnyAny);
1658 }
1659
1660 void LIR_Assembler::casl(Register addr, Register newval, Register cmpval) {
1661 __ cmpxchg(addr, cmpval, newval, Assembler::xword, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1662 __ cset(rscratch1, Assembler::NE);
1663 __ membar(__ AnyAny);
1664 }
1665
1666
1667 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
1668 Register addr;
1669 if (op->addr()->is_register()) {
1670 addr = as_reg(op->addr());
1671 } else {
1672 assert(op->addr()->is_address(), "what else?");
1673 LIR_Address* addr_ptr = op->addr()->as_address_ptr();
1674 assert(addr_ptr->disp() == 0, "need 0 disp");
1675 assert(addr_ptr->index() == LIR_Opr::illegalOpr(), "need 0 index");
1676 addr = as_reg(addr_ptr->base());
1677 }
1678 Register newval = as_reg(op->new_value());
1679 Register cmpval = as_reg(op->cmp_value());
1680
1681 if (op->code() == lir_cas_obj) {
1682 if (UseCompressedOops) {
1683 Register t1 = op->tmp1()->as_register();
1684 assert(op->tmp1()->is_valid(), "must be");
1685 __ encode_heap_oop(t1, cmpval);
1686 cmpval = t1;
1687 __ encode_heap_oop(rscratch2, newval);
1688 newval = rscratch2;
1689 casw(addr, newval, cmpval);
1690 } else {
1691 casl(addr, newval, cmpval);
1692 }
1693 } else if (op->code() == lir_cas_int) {
1694 casw(addr, newval, cmpval);
1695 } else {
1696 casl(addr, newval, cmpval);
1697 }
1698 }
1699
1700
1701 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type,
1702 LIR_Opr cmp_opr1, LIR_Opr cmp_opr2) {
1703 assert(cmp_opr1 == LIR_OprFact::illegalOpr && cmp_opr2 == LIR_OprFact::illegalOpr, "unnecessary cmp oprs on aarch64");
1704
1705 Assembler::Condition acond, ncond;
1706 switch (condition) {
1707 case lir_cond_equal: acond = Assembler::EQ; ncond = Assembler::NE; break;
1708 case lir_cond_notEqual: acond = Assembler::NE; ncond = Assembler::EQ; break;
1709 case lir_cond_less: acond = Assembler::LT; ncond = Assembler::GE; break;
1710 case lir_cond_lessEqual: acond = Assembler::LE; ncond = Assembler::GT; break;
1711 case lir_cond_greaterEqual: acond = Assembler::GE; ncond = Assembler::LT; break;
1712 case lir_cond_greater: acond = Assembler::GT; ncond = Assembler::LE; break;
1713 case lir_cond_belowEqual:
1714 case lir_cond_aboveEqual:
1715 default: ShouldNotReachHere();
1716 acond = Assembler::EQ; ncond = Assembler::NE; // unreachable
1717 }
1718
1719 assert(result->is_single_cpu() || result->is_double_cpu(),
1720 "expect single register for result");
1721 if (opr1->is_constant() && opr2->is_constant()
1722 && opr1->type() == T_INT && opr2->type() == T_INT) {
1723 jint val1 = opr1->as_jint();
1724 jint val2 = opr2->as_jint();
1725 if (val1 == 0 && val2 == 1) {
1726 __ cset(result->as_register(), ncond);
1727 return;
1728 } else if (val1 == 1 && val2 == 0) {
1729 __ cset(result->as_register(), acond);
1730 return;
1731 }
1732 }
1733
1734 if (opr1->is_constant() && opr2->is_constant()
1735 && opr1->type() == T_LONG && opr2->type() == T_LONG) {
1736 jlong val1 = opr1->as_jlong();
1737 jlong val2 = opr2->as_jlong();
1738 if (val1 == 0 && val2 == 1) {
1739 __ cset(result->as_register_lo(), ncond);
1740 return;
1741 } else if (val1 == 1 && val2 == 0) {
1742 __ cset(result->as_register_lo(), acond);
1743 return;
1744 }
1745 }
1746
1747 if (opr1->is_stack()) {
1748 stack2reg(opr1, FrameMap::rscratch1_opr, result->type());
1749 opr1 = FrameMap::rscratch1_opr;
1750 } else if (opr1->is_constant()) {
1751 LIR_Opr tmp
1752 = opr1->type() == T_LONG ? FrameMap::rscratch1_long_opr : FrameMap::rscratch1_opr;
1753 const2reg(opr1, tmp, lir_patch_none, nullptr);
1754 opr1 = tmp;
1755 }
1756
1757 if (opr2->is_stack()) {
1758 stack2reg(opr2, FrameMap::rscratch2_opr, result->type());
1759 opr2 = FrameMap::rscratch2_opr;
1760 } else if (opr2->is_constant()) {
1761 LIR_Opr tmp
1762 = opr2->type() == T_LONG ? FrameMap::rscratch2_long_opr : FrameMap::rscratch2_opr;
1763 const2reg(opr2, tmp, lir_patch_none, nullptr);
1764 opr2 = tmp;
1765 }
1766
1767 if (result->type() == T_LONG)
1768 __ csel(result->as_register_lo(), opr1->as_register_lo(), opr2->as_register_lo(), acond);
1769 else
1770 __ csel(result->as_register(), opr1->as_register(), opr2->as_register(), acond);
1771 }
1772
1773 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info) {
1774 assert(info == nullptr, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1775
1776 if (left->is_single_cpu()) {
1777 Register lreg = left->as_register();
1778 Register dreg = as_reg(dest);
1779
1780 if (right->is_single_cpu()) {
1781 // cpu register - cpu register
1782
1783 assert(left->type() == T_INT && right->type() == T_INT && dest->type() == T_INT,
1784 "should be");
1785 Register rreg = right->as_register();
1786 switch (code) {
1787 case lir_add: __ addw (dest->as_register(), lreg, rreg); break;
1788 case lir_sub: __ subw (dest->as_register(), lreg, rreg); break;
1789 case lir_mul: __ mulw (dest->as_register(), lreg, rreg); break;
1790 default: ShouldNotReachHere();
1791 }
1792
1793 } else if (right->is_double_cpu()) {
1794 Register rreg = right->as_register_lo();
1795 // single_cpu + double_cpu: can happen with obj+long
1796 assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1797 switch (code) {
1798 case lir_add: __ add(dreg, lreg, rreg); break;
1799 case lir_sub: __ sub(dreg, lreg, rreg); break;
1800 default: ShouldNotReachHere();
1801 }
1802 } else if (right->is_constant()) {
1803 // cpu register - constant
1804 jlong c;
1805
1806 // FIXME. This is fugly: we really need to factor all this logic.
1807 switch(right->type()) {
1808 case T_LONG:
1809 c = right->as_constant_ptr()->as_jlong();
1810 break;
1811 case T_INT:
1812 case T_ADDRESS:
1813 c = right->as_constant_ptr()->as_jint();
1814 break;
1815 default:
1816 ShouldNotReachHere();
1817 c = 0; // unreachable
1818 break;
1819 }
1820
1821 assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1822 if (c == 0 && dreg == lreg) {
1823 COMMENT("effective nop elided");
1824 return;
1825 }
1826 switch(left->type()) {
1827 case T_INT:
1828 switch (code) {
1829 case lir_add: __ addw(dreg, lreg, c); break;
1830 case lir_sub: __ subw(dreg, lreg, c); break;
1831 default: ShouldNotReachHere();
1832 }
1833 break;
1834 case T_OBJECT:
1835 case T_ADDRESS:
1836 switch (code) {
1837 case lir_add: __ add(dreg, lreg, c); break;
1838 case lir_sub: __ sub(dreg, lreg, c); break;
1839 default: ShouldNotReachHere();
1840 }
1841 break;
1842 default:
1843 ShouldNotReachHere();
1844 }
1845 } else {
1846 ShouldNotReachHere();
1847 }
1848
1849 } else if (left->is_double_cpu()) {
1850 Register lreg_lo = left->as_register_lo();
1851
1852 if (right->is_double_cpu()) {
1853 // cpu register - cpu register
1854 Register rreg_lo = right->as_register_lo();
1855 switch (code) {
1856 case lir_add: __ add (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1857 case lir_sub: __ sub (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1858 case lir_mul: __ mul (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1859 case lir_div: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, false, rscratch1); break;
1860 case lir_rem: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, true, rscratch1); break;
1861 default:
1862 ShouldNotReachHere();
1863 }
1864
1865 } else if (right->is_constant()) {
1866 jlong c = right->as_constant_ptr()->as_jlong();
1867 Register dreg = as_reg(dest);
1868 switch (code) {
1869 case lir_add:
1870 case lir_sub:
1871 if (c == 0 && dreg == lreg_lo) {
1872 COMMENT("effective nop elided");
1873 return;
1874 }
1875 code == lir_add ? __ add(dreg, lreg_lo, c) : __ sub(dreg, lreg_lo, c);
1876 break;
1877 case lir_div:
1878 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1879 if (c == 1) {
1880 // move lreg_lo to dreg if divisor is 1
1881 __ mov(dreg, lreg_lo);
1882 } else {
1883 unsigned int shift = log2i_exact(c);
1884 // use rscratch1 as intermediate result register
1885 __ asr(rscratch1, lreg_lo, 63);
1886 __ add(rscratch1, lreg_lo, rscratch1, Assembler::LSR, 64 - shift);
1887 __ asr(dreg, rscratch1, shift);
1888 }
1889 break;
1890 case lir_rem:
1891 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1892 if (c == 1) {
1893 // move 0 to dreg if divisor is 1
1894 __ mov(dreg, zr);
1895 } else {
1896 // use rscratch1 as intermediate result register
1897 __ negs(rscratch1, lreg_lo);
1898 __ andr(dreg, lreg_lo, c - 1);
1899 __ andr(rscratch1, rscratch1, c - 1);
1900 __ csneg(dreg, dreg, rscratch1, Assembler::MI);
1901 }
1902 break;
1903 default:
1904 ShouldNotReachHere();
1905 }
1906 } else {
1907 ShouldNotReachHere();
1908 }
1909 } else if (left->is_single_fpu()) {
1910 assert(right->is_single_fpu(), "right hand side of float arithmetics needs to be float register");
1911 switch (code) {
1912 case lir_add: __ fadds (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1913 case lir_sub: __ fsubs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1914 case lir_mul: __ fmuls (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1915 case lir_div: __ fdivs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1916 default:
1917 ShouldNotReachHere();
1918 }
1919 } else if (left->is_double_fpu()) {
1920 if (right->is_double_fpu()) {
1921 // fpu register - fpu register
1922 switch (code) {
1923 case lir_add: __ faddd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1924 case lir_sub: __ fsubd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1925 case lir_mul: __ fmuld (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1926 case lir_div: __ fdivd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1927 default:
1928 ShouldNotReachHere();
1929 }
1930 } else {
1931 if (right->is_constant()) {
1932 ShouldNotReachHere();
1933 }
1934 ShouldNotReachHere();
1935 }
1936 } else if (left->is_single_stack() || left->is_address()) {
1937 assert(left == dest, "left and dest must be equal");
1938 ShouldNotReachHere();
1939 } else {
1940 ShouldNotReachHere();
1941 }
1942 }
1943
1944 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr tmp, LIR_Opr dest, LIR_Op* op) {
1945 switch(code) {
1946 case lir_abs : __ fabsd(dest->as_double_reg(), value->as_double_reg()); break;
1947 case lir_sqrt: __ fsqrtd(dest->as_double_reg(), value->as_double_reg()); break;
1948 case lir_f2hf: __ flt_to_flt16(dest->as_register(), value->as_float_reg(), tmp->as_float_reg()); break;
1949 case lir_hf2f: __ flt16_to_flt(dest->as_float_reg(), value->as_register(), tmp->as_float_reg()); break;
1950 default : ShouldNotReachHere();
1951 }
1952 }
1953
1954 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
1955
1956 assert(left->is_single_cpu() || left->is_double_cpu(), "expect single or double register");
1957 Register Rleft = left->is_single_cpu() ? left->as_register() :
1958 left->as_register_lo();
1959 if (dst->is_single_cpu()) {
1960 Register Rdst = dst->as_register();
1961 if (right->is_constant()) {
1962 switch (code) {
1963 case lir_logic_and: __ andw (Rdst, Rleft, right->as_jint()); break;
1964 case lir_logic_or: __ orrw (Rdst, Rleft, right->as_jint()); break;
1965 case lir_logic_xor: __ eorw (Rdst, Rleft, right->as_jint()); break;
1966 default: ShouldNotReachHere(); break;
1967 }
1968 } else {
1969 Register Rright = right->is_single_cpu() ? right->as_register() :
1970 right->as_register_lo();
1971 switch (code) {
1972 case lir_logic_and: __ andw (Rdst, Rleft, Rright); break;
1973 case lir_logic_or: __ orrw (Rdst, Rleft, Rright); break;
1974 case lir_logic_xor: __ eorw (Rdst, Rleft, Rright); break;
1975 default: ShouldNotReachHere(); break;
1976 }
1977 }
1978 } else {
1979 Register Rdst = dst->as_register_lo();
1980 if (right->is_constant()) {
1981 switch (code) {
1982 case lir_logic_and: __ andr (Rdst, Rleft, right->as_jlong()); break;
1983 case lir_logic_or: __ orr (Rdst, Rleft, right->as_jlong()); break;
1984 case lir_logic_xor: __ eor (Rdst, Rleft, right->as_jlong()); break;
1985 default: ShouldNotReachHere(); break;
1986 }
1987 } else {
1988 Register Rright = right->is_single_cpu() ? right->as_register() :
1989 right->as_register_lo();
1990 switch (code) {
1991 case lir_logic_and: __ andr (Rdst, Rleft, Rright); break;
1992 case lir_logic_or: __ orr (Rdst, Rleft, Rright); break;
1993 case lir_logic_xor: __ eor (Rdst, Rleft, Rright); break;
1994 default: ShouldNotReachHere(); break;
1995 }
1996 }
1997 }
1998 }
1999
2000
2001
2002 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr illegal, LIR_Opr result, CodeEmitInfo* info) {
2003
2004 // opcode check
2005 assert((code == lir_idiv) || (code == lir_irem), "opcode must be idiv or irem");
2006 bool is_irem = (code == lir_irem);
2007
2008 // operand check
2009 assert(left->is_single_cpu(), "left must be register");
2010 assert(right->is_single_cpu() || right->is_constant(), "right must be register or constant");
2011 assert(result->is_single_cpu(), "result must be register");
2012 Register lreg = left->as_register();
2013 Register dreg = result->as_register();
2014
2015 // power-of-2 constant check and codegen
2016 if (right->is_constant()) {
2017 int c = right->as_constant_ptr()->as_jint();
2018 assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
2019 if (is_irem) {
2020 if (c == 1) {
2021 // move 0 to dreg if divisor is 1
2022 __ movw(dreg, zr);
2023 } else {
2024 // use rscratch1 as intermediate result register
2025 __ negsw(rscratch1, lreg);
2026 __ andw(dreg, lreg, c - 1);
2027 __ andw(rscratch1, rscratch1, c - 1);
2028 __ csnegw(dreg, dreg, rscratch1, Assembler::MI);
2029 }
2030 } else {
2031 if (c == 1) {
2032 // move lreg to dreg if divisor is 1
2033 __ movw(dreg, lreg);
2034 } else {
2035 unsigned int shift = exact_log2(c);
2036 // use rscratch1 as intermediate result register
2037 __ asrw(rscratch1, lreg, 31);
2038 __ addw(rscratch1, lreg, rscratch1, Assembler::LSR, 32 - shift);
2039 __ asrw(dreg, rscratch1, shift);
2040 }
2041 }
2042 } else {
2043 Register rreg = right->as_register();
2044 __ corrected_idivl(dreg, lreg, rreg, is_irem, rscratch1);
2045 }
2046 }
2047
2048
2049 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
2050 if (opr1->is_constant() && opr2->is_single_cpu()) {
2051 // tableswitch
2052 Register reg = as_reg(opr2);
2053 struct tableswitch &table = switches[opr1->as_constant_ptr()->as_jint()];
2054 __ tableswitch(reg, table._first_key, table._last_key, table._branches, table._after);
2055 } else if (opr1->is_single_cpu() || opr1->is_double_cpu()) {
2056 Register reg1 = as_reg(opr1);
2057 if (opr2->is_single_cpu()) {
2058 // cpu register - cpu register
2059 Register reg2 = opr2->as_register();
2060 if (is_reference_type(opr1->type())) {
2061 __ cmpoop(reg1, reg2);
2062 } else {
2063 assert(!is_reference_type(opr2->type()), "cmp int, oop?");
2064 __ cmpw(reg1, reg2);
2065 }
2066 return;
2067 }
2068 if (opr2->is_double_cpu()) {
2069 // cpu register - cpu register
2070 Register reg2 = opr2->as_register_lo();
2071 __ cmp(reg1, reg2);
2072 return;
2073 }
2074
2075 if (opr2->is_constant()) {
2076 bool is_32bit = false; // width of register operand
2077 jlong imm;
2078
2079 switch(opr2->type()) {
2080 case T_INT:
2081 imm = opr2->as_constant_ptr()->as_jint();
2082 is_32bit = true;
2083 break;
2084 case T_LONG:
2085 imm = opr2->as_constant_ptr()->as_jlong();
2086 break;
2087 case T_ADDRESS:
2088 imm = opr2->as_constant_ptr()->as_jint();
2089 break;
2090 case T_METADATA:
2091 imm = (intptr_t)(opr2->as_constant_ptr()->as_metadata());
2092 break;
2093 case T_OBJECT:
2094 case T_ARRAY:
2095 jobject2reg(opr2->as_constant_ptr()->as_jobject(), rscratch1);
2096 __ cmpoop(reg1, rscratch1);
2097 return;
2098 default:
2099 ShouldNotReachHere();
2100 imm = 0; // unreachable
2101 break;
2102 }
2103
2104 if (Assembler::operand_valid_for_add_sub_immediate(imm)) {
2105 if (is_32bit)
2106 __ cmpw(reg1, imm);
2107 else
2108 __ subs(zr, reg1, imm);
2109 return;
2110 } else {
2111 __ mov(rscratch1, imm);
2112 if (is_32bit)
2113 __ cmpw(reg1, rscratch1);
2114 else
2115 __ cmp(reg1, rscratch1);
2116 return;
2117 }
2118 } else
2119 ShouldNotReachHere();
2120 } else if (opr1->is_single_fpu()) {
2121 FloatRegister reg1 = opr1->as_float_reg();
2122 assert(opr2->is_single_fpu(), "expect single float register");
2123 FloatRegister reg2 = opr2->as_float_reg();
2124 __ fcmps(reg1, reg2);
2125 } else if (opr1->is_double_fpu()) {
2126 FloatRegister reg1 = opr1->as_double_reg();
2127 assert(opr2->is_double_fpu(), "expect double float register");
2128 FloatRegister reg2 = opr2->as_double_reg();
2129 __ fcmpd(reg1, reg2);
2130 } else {
2131 ShouldNotReachHere();
2132 }
2133 }
2134
2135 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op){
2136 if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
2137 bool is_unordered_less = (code == lir_ucmp_fd2i);
2138 if (left->is_single_fpu()) {
2139 __ float_cmp(true, is_unordered_less ? -1 : 1, left->as_float_reg(), right->as_float_reg(), dst->as_register());
2140 } else if (left->is_double_fpu()) {
2141 __ float_cmp(false, is_unordered_less ? -1 : 1, left->as_double_reg(), right->as_double_reg(), dst->as_register());
2142 } else {
2143 ShouldNotReachHere();
2144 }
2145 } else if (code == lir_cmp_l2i) {
2146 Label done;
2147 __ cmp(left->as_register_lo(), right->as_register_lo());
2148 __ mov(dst->as_register(), (uint64_t)-1L);
2149 __ br(Assembler::LT, done);
2150 __ csinc(dst->as_register(), zr, zr, Assembler::EQ);
2151 __ bind(done);
2152 } else {
2153 ShouldNotReachHere();
2154 }
2155 }
2156
2157
2158 void LIR_Assembler::align_call(LIR_Code code) { }
2159
2160
2161 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
2162 address call = __ trampoline_call(Address(op->addr(), rtype));
2163 if (call == nullptr) {
2164 bailout("trampoline stub overflow");
2165 return;
2166 }
2167 add_call_info(code_offset(), op->info(), op->maybe_return_as_fields());
2168 __ post_call_nop();
2169 }
2170
2171
2172 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
2173 address call = __ ic_call(op->addr());
2174 if (call == nullptr) {
2175 bailout("trampoline stub overflow");
2176 return;
2177 }
2178 add_call_info(code_offset(), op->info(), op->maybe_return_as_fields());
2179 __ post_call_nop();
2180 }
2181
2182 void LIR_Assembler::emit_static_call_stub() {
2183 address call_pc = __ pc();
2184 address stub = __ start_a_stub(call_stub_size());
2185 if (stub == nullptr) {
2186 bailout("static call stub overflow");
2187 return;
2188 }
2189
2190 int start = __ offset();
2191
2192 __ relocate(static_stub_Relocation::spec(call_pc));
2193 __ emit_static_call_stub();
2194
2195 assert(__ offset() - start + CompiledDirectCall::to_trampoline_stub_size()
2196 <= call_stub_size(), "stub too big");
2197 __ end_a_stub();
2198 }
2199
2200
2201 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
2202 assert(exceptionOop->as_register() == r0, "must match");
2203 assert(exceptionPC->as_register() == r3, "must match");
2204
2205 // exception object is not added to oop map by LinearScan
2206 // (LinearScan assumes that no oops are in fixed registers)
2207 info->add_register_oop(exceptionOop);
2208 StubId unwind_id;
2209
2210 // get current pc information
2211 // pc is only needed if the method has an exception handler, the unwind code does not need it.
2212 if (compilation()->debug_info_recorder()->last_pc_offset() == __ offset()) {
2213 // As no instructions have been generated yet for this LIR node it's
2214 // possible that an oop map already exists for the current offset.
2215 // In that case insert an dummy NOP here to ensure all oop map PCs
2216 // are unique. See JDK-8237483.
2217 __ nop();
2218 }
2219 int pc_for_athrow_offset = __ offset();
2220 InternalAddress pc_for_athrow(__ pc());
2221 __ adr(exceptionPC->as_register(), pc_for_athrow);
2222 add_call_info(pc_for_athrow_offset, info); // for exception handler
2223
2224 __ verify_not_null_oop(r0);
2225 // search an exception handler (r0: exception oop, r3: throwing pc)
2226 if (compilation()->has_fpu_code()) {
2227 unwind_id = StubId::c1_handle_exception_id;
2228 } else {
2229 unwind_id = StubId::c1_handle_exception_nofpu_id;
2230 }
2231 __ far_call(RuntimeAddress(Runtime1::entry_for(unwind_id)));
2232
2233 // FIXME: enough room for two byte trap ????
2234 __ nop();
2235 }
2236
2237
2238 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
2239 assert(exceptionOop->as_register() == r0, "must match");
2240
2241 __ b(_unwind_handler_entry);
2242 }
2243
2244
2245 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2246 Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2247 Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2248
2249 switch (left->type()) {
2250 case T_INT: {
2251 switch (code) {
2252 case lir_shl: __ lslvw (dreg, lreg, count->as_register()); break;
2253 case lir_shr: __ asrvw (dreg, lreg, count->as_register()); break;
2254 case lir_ushr: __ lsrvw (dreg, lreg, count->as_register()); break;
2255 default:
2256 ShouldNotReachHere();
2257 break;
2258 }
2259 break;
2260 case T_LONG:
2261 case T_ADDRESS:
2262 case T_OBJECT:
2263 switch (code) {
2264 case lir_shl: __ lslv (dreg, lreg, count->as_register()); break;
2265 case lir_shr: __ asrv (dreg, lreg, count->as_register()); break;
2266 case lir_ushr: __ lsrv (dreg, lreg, count->as_register()); break;
2267 default:
2268 ShouldNotReachHere();
2269 break;
2270 }
2271 break;
2272 default:
2273 ShouldNotReachHere();
2274 break;
2275 }
2276 }
2277 }
2278
2279
2280 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2281 Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2282 Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2283
2284 switch (left->type()) {
2285 case T_INT: {
2286 switch (code) {
2287 case lir_shl: __ lslw (dreg, lreg, count); break;
2288 case lir_shr: __ asrw (dreg, lreg, count); break;
2289 case lir_ushr: __ lsrw (dreg, lreg, count); break;
2290 default:
2291 ShouldNotReachHere();
2292 break;
2293 }
2294 break;
2295 case T_LONG:
2296 case T_ADDRESS:
2297 case T_OBJECT:
2298 switch (code) {
2299 case lir_shl: __ lsl (dreg, lreg, count); break;
2300 case lir_shr: __ asr (dreg, lreg, count); break;
2301 case lir_ushr: __ lsr (dreg, lreg, count); break;
2302 default:
2303 ShouldNotReachHere();
2304 break;
2305 }
2306 break;
2307 default:
2308 ShouldNotReachHere();
2309 break;
2310 }
2311 }
2312 }
2313
2314
2315 void LIR_Assembler::store_parameter(Register r, int offset_from_rsp_in_words) {
2316 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2317 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2318 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2319 __ str (r, Address(sp, offset_from_rsp_in_bytes));
2320 }
2321
2322
2323 void LIR_Assembler::store_parameter(jint c, int offset_from_rsp_in_words) {
2324 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2325 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2326 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2327 __ mov (rscratch1, c);
2328 __ str (rscratch1, Address(sp, offset_from_rsp_in_bytes));
2329 }
2330
2331
2332 void LIR_Assembler::store_parameter(jobject o, int offset_from_rsp_in_words) {
2333 ShouldNotReachHere();
2334 assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2335 int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2336 assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2337 __ lea(rscratch1, __ constant_oop_address(o));
2338 __ str(rscratch1, Address(sp, offset_from_rsp_in_bytes));
2339 }
2340
2341 void LIR_Assembler::arraycopy_inlinetype_check(Register obj, Register tmp, CodeStub* slow_path, bool is_dest, bool null_check) {
2342 if (null_check) {
2343 __ cbz(obj, *slow_path->entry());
2344 }
2345 if (is_dest) {
2346 __ test_null_free_array_oop(obj, tmp, *slow_path->entry());
2347 __ test_flat_array_oop(obj, tmp, *slow_path->entry());
2348 } else {
2349 __ test_flat_array_oop(obj, tmp, *slow_path->entry());
2350 }
2351 }
2352
2353 // This code replaces a call to arraycopy; no exception may
2354 // be thrown in this code, they must be thrown in the System.arraycopy
2355 // activation frame; we could save some checks if this would not be the case
2356 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
2357 ciArrayKlass* default_type = op->expected_type();
2358 Register src = op->src()->as_register();
2359 Register dst = op->dst()->as_register();
2360 Register src_pos = op->src_pos()->as_register();
2361 Register dst_pos = op->dst_pos()->as_register();
2362 Register length = op->length()->as_register();
2363 Register tmp = op->tmp()->as_register();
2364
2365 CodeStub* stub = op->stub();
2366 int flags = op->flags();
2367 BasicType basic_type = default_type != nullptr ? default_type->element_type()->basic_type() : T_ILLEGAL;
2368 if (is_reference_type(basic_type)) basic_type = T_OBJECT;
2369
2370 if (flags & LIR_OpArrayCopy::always_slow_path) {
2371 __ b(*stub->entry());
2372 __ bind(*stub->continuation());
2373 return;
2374 }
2375
2376 // if we don't know anything, just go through the generic arraycopy
2377 if (default_type == nullptr // || basic_type == T_OBJECT
2378 ) {
2379 Label done;
2380 assert(src == r1 && src_pos == r2, "mismatch in calling convention");
2381
2382 // Save the arguments in case the generic arraycopy fails and we
2383 // have to fall back to the JNI stub
2384 __ stp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2385 __ stp(length, src_pos, Address(sp, 2*BytesPerWord));
2386 __ str(src, Address(sp, 4*BytesPerWord));
2387
2388 address copyfunc_addr = StubRoutines::generic_arraycopy();
2389 assert(copyfunc_addr != nullptr, "generic arraycopy stub required");
2390
2391 // The arguments are in java calling convention so we shift them
2392 // to C convention
2393 assert_different_registers(c_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4);
2394 __ mov(c_rarg0, j_rarg0);
2395 assert_different_registers(c_rarg1, j_rarg2, j_rarg3, j_rarg4);
2396 __ mov(c_rarg1, j_rarg1);
2397 assert_different_registers(c_rarg2, j_rarg3, j_rarg4);
2398 __ mov(c_rarg2, j_rarg2);
2399 assert_different_registers(c_rarg3, j_rarg4);
2400 __ mov(c_rarg3, j_rarg3);
2401 __ mov(c_rarg4, j_rarg4);
2402 #ifndef PRODUCT
2403 if (PrintC1Statistics) {
2404 __ incrementw(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
2405 }
2406 #endif
2407 __ far_call(RuntimeAddress(copyfunc_addr));
2408
2409 __ cbz(r0, *stub->continuation());
2410
2411 // Reload values from the stack so they are where the stub
2412 // expects them.
2413 __ ldp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2414 __ ldp(length, src_pos, Address(sp, 2*BytesPerWord));
2415 __ ldr(src, Address(sp, 4*BytesPerWord));
2416
2417 // r0 is -1^K where K == partial copied count
2418 __ eonw(rscratch1, r0, zr);
2419 // adjust length down and src/end pos up by partial copied count
2420 __ subw(length, length, rscratch1);
2421 __ addw(src_pos, src_pos, rscratch1);
2422 __ addw(dst_pos, dst_pos, rscratch1);
2423 __ b(*stub->entry());
2424
2425 __ bind(*stub->continuation());
2426 return;
2427 }
2428
2429 // Handle inline type arrays
2430 if (flags & LIR_OpArrayCopy::src_inlinetype_check) {
2431 arraycopy_inlinetype_check(src, tmp, stub, false, (flags & LIR_OpArrayCopy::src_null_check));
2432 }
2433 if (flags & LIR_OpArrayCopy::dst_inlinetype_check) {
2434 arraycopy_inlinetype_check(dst, tmp, stub, true, (flags & LIR_OpArrayCopy::dst_null_check));
2435 }
2436
2437 assert(default_type != nullptr && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
2438
2439 int elem_size = type2aelembytes(basic_type);
2440 int scale = exact_log2(elem_size);
2441
2442 Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
2443 Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
2444
2445 // test for null
2446 if (flags & LIR_OpArrayCopy::src_null_check) {
2447 __ cbz(src, *stub->entry());
2448 }
2449 if (flags & LIR_OpArrayCopy::dst_null_check) {
2450 __ cbz(dst, *stub->entry());
2451 }
2452
2453 // If the compiler was not able to prove that exact type of the source or the destination
2454 // of the arraycopy is an array type, check at runtime if the source or the destination is
2455 // an instance type.
2456 if (flags & LIR_OpArrayCopy::type_check) {
2457 if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::dst_objarray)) {
2458 __ load_klass(tmp, dst);
2459 __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2460 __ cmpw(rscratch1, Klass::_lh_neutral_value);
2461 __ br(Assembler::GE, *stub->entry());
2462 }
2463
2464 if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::src_objarray)) {
2465 __ load_klass(tmp, src);
2466 __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2467 __ cmpw(rscratch1, Klass::_lh_neutral_value);
2468 __ br(Assembler::GE, *stub->entry());
2469 }
2470 }
2471
2472 // check if negative
2473 if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2474 __ cmpw(src_pos, 0);
2475 __ br(Assembler::LT, *stub->entry());
2476 }
2477 if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2478 __ cmpw(dst_pos, 0);
2479 __ br(Assembler::LT, *stub->entry());
2480 }
2481
2482 if (flags & LIR_OpArrayCopy::length_positive_check) {
2483 __ cmpw(length, 0);
2484 __ br(Assembler::LT, *stub->entry());
2485 }
2486
2487 if (flags & LIR_OpArrayCopy::src_range_check) {
2488 __ addw(tmp, src_pos, length);
2489 __ ldrw(rscratch1, src_length_addr);
2490 __ cmpw(tmp, rscratch1);
2491 __ br(Assembler::HI, *stub->entry());
2492 }
2493 if (flags & LIR_OpArrayCopy::dst_range_check) {
2494 __ addw(tmp, dst_pos, length);
2495 __ ldrw(rscratch1, dst_length_addr);
2496 __ cmpw(tmp, rscratch1);
2497 __ br(Assembler::HI, *stub->entry());
2498 }
2499
2500 if (flags & LIR_OpArrayCopy::type_check) {
2501 // We don't know the array types are compatible
2502 if (basic_type != T_OBJECT) {
2503 // Simple test for basic type arrays
2504 __ cmp_klasses_from_objects(src, dst, tmp, rscratch1);
2505 __ br(Assembler::NE, *stub->entry());
2506 } else {
2507 // For object arrays, if src is a sub class of dst then we can
2508 // safely do the copy.
2509 Label cont, slow;
2510
2511 #define PUSH(r1, r2) \
2512 stp(r1, r2, __ pre(sp, -2 * wordSize));
2513
2514 #define POP(r1, r2) \
2515 ldp(r1, r2, __ post(sp, 2 * wordSize));
2516
2517 __ PUSH(src, dst);
2518
2519 __ load_klass(src, src);
2520 __ load_klass(dst, dst);
2521
2522 __ check_klass_subtype_fast_path(src, dst, tmp, &cont, &slow, nullptr);
2523
2524 __ PUSH(src, dst);
2525 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_slow_subtype_check_id)));
2526 __ POP(src, dst);
2527
2528 __ cbnz(src, cont);
2529
2530 __ bind(slow);
2531 __ POP(src, dst);
2532
2533 address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2534 if (copyfunc_addr != nullptr) { // use stub if available
2535 // src is not a sub class of dst so we have to do a
2536 // per-element check.
2537
2538 int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2539 if ((flags & mask) != mask) {
2540 // Check that at least both of them object arrays.
2541 assert(flags & mask, "one of the two should be known to be an object array");
2542
2543 if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2544 __ load_klass(tmp, src);
2545 } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2546 __ load_klass(tmp, dst);
2547 }
2548 int lh_offset = in_bytes(Klass::layout_helper_offset());
2549 Address klass_lh_addr(tmp, lh_offset);
2550 jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2551 __ ldrw(rscratch1, klass_lh_addr);
2552 __ mov(rscratch2, objArray_lh);
2553 __ eorw(rscratch1, rscratch1, rscratch2);
2554 __ cbnzw(rscratch1, *stub->entry());
2555 }
2556
2557 // Spill because stubs can use any register they like and it's
2558 // easier to restore just those that we care about.
2559 __ stp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2560 __ stp(length, src_pos, Address(sp, 2*BytesPerWord));
2561 __ str(src, Address(sp, 4*BytesPerWord));
2562
2563 __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2564 __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2565 assert_different_registers(c_rarg0, dst, dst_pos, length);
2566 __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2567 __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2568 assert_different_registers(c_rarg1, dst, length);
2569 __ uxtw(c_rarg2, length);
2570 assert_different_registers(c_rarg2, dst);
2571
2572 __ load_klass(c_rarg4, dst);
2573 __ ldr(c_rarg4, Address(c_rarg4, ObjArrayKlass::element_klass_offset()));
2574 __ ldrw(c_rarg3, Address(c_rarg4, Klass::super_check_offset_offset()));
2575 __ far_call(RuntimeAddress(copyfunc_addr));
2576
2577 #ifndef PRODUCT
2578 if (PrintC1Statistics) {
2579 Label failed;
2580 __ cbnz(r0, failed);
2581 __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_cnt));
2582 __ bind(failed);
2583 }
2584 #endif
2585
2586 __ cbz(r0, *stub->continuation());
2587
2588 #ifndef PRODUCT
2589 if (PrintC1Statistics) {
2590 __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_attempt_cnt));
2591 }
2592 #endif
2593 assert_different_registers(dst, dst_pos, length, src_pos, src, r0, rscratch1);
2594
2595 // Restore previously spilled arguments
2596 __ ldp(dst, dst_pos, Address(sp, 0*BytesPerWord));
2597 __ ldp(length, src_pos, Address(sp, 2*BytesPerWord));
2598 __ ldr(src, Address(sp, 4*BytesPerWord));
2599
2600 // return value is -1^K where K is partial copied count
2601 __ eonw(rscratch1, r0, zr);
2602 // adjust length down and src/end pos up by partial copied count
2603 __ subw(length, length, rscratch1);
2604 __ addw(src_pos, src_pos, rscratch1);
2605 __ addw(dst_pos, dst_pos, rscratch1);
2606 }
2607
2608 __ b(*stub->entry());
2609
2610 __ bind(cont);
2611 __ POP(src, dst);
2612 }
2613 }
2614
2615 #ifdef ASSERT
2616 if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2617 // Sanity check the known type with the incoming class. For the
2618 // primitive case the types must match exactly with src.klass and
2619 // dst.klass each exactly matching the default type. For the
2620 // object array case, if no type check is needed then either the
2621 // dst type is exactly the expected type and the src type is a
2622 // subtype which we can't check or src is the same array as dst
2623 // but not necessarily exactly of type default_type.
2624 Label known_ok, halt;
2625 __ mov_metadata(tmp, default_type->constant_encoding());
2626
2627 if (basic_type != T_OBJECT) {
2628 __ cmp_klass(dst, tmp, rscratch1);
2629 __ br(Assembler::NE, halt);
2630 __ cmp_klass(src, tmp, rscratch1);
2631 __ br(Assembler::EQ, known_ok);
2632 } else {
2633 __ cmp_klass(dst, tmp, rscratch1);
2634 __ br(Assembler::EQ, known_ok);
2635 __ cmp(src, dst);
2636 __ br(Assembler::EQ, known_ok);
2637 }
2638 __ bind(halt);
2639 __ stop("incorrect type information in arraycopy");
2640 __ bind(known_ok);
2641 }
2642 #endif
2643
2644 #ifndef PRODUCT
2645 if (PrintC1Statistics) {
2646 __ incrementw(ExternalAddress(Runtime1::arraycopy_count_address(basic_type)));
2647 }
2648 #endif
2649
2650 __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2651 __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2652 assert_different_registers(c_rarg0, dst, dst_pos, length);
2653 __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2654 __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2655 assert_different_registers(c_rarg1, dst, length);
2656 __ uxtw(c_rarg2, length);
2657 assert_different_registers(c_rarg2, dst);
2658
2659 bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2660 bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2661 const char *name;
2662 address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2663
2664 CodeBlob *cb = CodeCache::find_blob(entry);
2665 if (cb) {
2666 __ far_call(RuntimeAddress(entry));
2667 } else {
2668 __ call_VM_leaf(entry, 3);
2669 }
2670
2671 if (stub != nullptr) {
2672 __ bind(*stub->continuation());
2673 }
2674 }
2675
2676
2677
2678
2679 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2680 Register obj = op->obj_opr()->as_register(); // may not be an oop
2681 Register hdr = op->hdr_opr()->as_register();
2682 Register lock = op->lock_opr()->as_register();
2683 Register temp = op->scratch_opr()->as_register();
2684 if (op->code() == lir_lock) {
2685 // add debug info for NullPointerException only if one is possible
2686 int null_check_offset = __ lock_object(hdr, obj, lock, temp, *op->stub()->entry());
2687 if (op->info() != nullptr) {
2688 add_debug_info_for_null_check(null_check_offset, op->info());
2689 }
2690 // done
2691 } else if (op->code() == lir_unlock) {
2692 __ unlock_object(hdr, obj, lock, temp, *op->stub()->entry());
2693 } else {
2694 Unimplemented();
2695 }
2696 __ bind(*op->stub()->continuation());
2697 }
2698
2699 void LIR_Assembler::emit_load_klass(LIR_OpLoadKlass* op) {
2700 Register obj = op->obj()->as_pointer_register();
2701 Register result = op->result_opr()->as_pointer_register();
2702
2703 CodeEmitInfo* info = op->info();
2704 if (info != nullptr) {
2705 add_debug_info_for_null_check_here(info);
2706 }
2707
2708 __ load_klass(result, obj);
2709 }
2710
2711 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2712 ciMethod* method = op->profiled_method();
2713 int bci = op->profiled_bci();
2714 ciMethod* callee = op->profiled_callee();
2715
2716 // Update counter for all call types
2717 ciMethodData* md = method->method_data_or_null();
2718 assert(md != nullptr, "Sanity");
2719 ciProfileData* data = md->bci_to_data(bci);
2720 assert(data != nullptr && data->is_CounterData(), "need CounterData for calls");
2721 assert(op->mdo()->is_single_cpu(), "mdo must be allocated");
2722 Register mdo = op->mdo()->as_register();
2723 __ mov_metadata(mdo, md->constant_encoding());
2724 Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
2725 // Perform additional virtual call profiling for invokevirtual and
2726 // invokeinterface bytecodes
2727 if (op->should_profile_receiver_type()) {
2728 assert(op->recv()->is_single_cpu(), "recv must be allocated");
2729 Register recv = op->recv()->as_register();
2730 assert_different_registers(mdo, recv);
2731 assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2732 ciKlass* known_klass = op->known_holder();
2733 if (C1OptimizeVirtualCallProfiling && known_klass != nullptr) {
2734 // We know the type that will be seen at this call site; we can
2735 // statically update the MethodData* rather than needing to do
2736 // dynamic tests on the receiver type.
2737 ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2738 for (uint i = 0; i < VirtualCallData::row_limit(); i++) {
2739 ciKlass* receiver = vc_data->receiver(i);
2740 if (known_klass->equals(receiver)) {
2741 Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2742 __ addptr(data_addr, DataLayout::counter_increment);
2743 return;
2744 }
2745 }
2746 // Receiver type is not found in profile data.
2747 // Fall back to runtime helper to handle the rest at runtime.
2748 __ mov_metadata(recv, known_klass->constant_encoding());
2749 } else {
2750 __ load_klass(recv, recv);
2751 }
2752 type_profile_helper(mdo, md, data, recv);
2753 } else {
2754 // Static call
2755 __ addptr(counter_addr, DataLayout::counter_increment);
2756 }
2757 }
2758
2759
2760 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst) {
2761 __ lea(dst->as_register(), frame_map()->address_for_monitor_lock(monitor_no));
2762 }
2763
2764 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
2765 assert(op->crc()->is_single_cpu(), "crc must be register");
2766 assert(op->val()->is_single_cpu(), "byte value must be register");
2767 assert(op->result_opr()->is_single_cpu(), "result must be register");
2768 Register crc = op->crc()->as_register();
2769 Register val = op->val()->as_register();
2770 Register res = op->result_opr()->as_register();
2771
2772 assert_different_registers(val, crc, res);
2773 uint64_t offset;
2774 __ adrp(res, ExternalAddress(StubRoutines::crc_table_addr()), offset);
2775 __ add(res, res, offset);
2776
2777 __ mvnw(crc, crc); // ~crc
2778 __ update_byte_crc32(crc, val, res);
2779 __ mvnw(res, crc); // ~crc
2780 }
2781
2782 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2783 COMMENT("emit_profile_type {");
2784 Register obj = op->obj()->as_register();
2785 Register tmp = op->tmp()->as_pointer_register();
2786 Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2787 ciKlass* exact_klass = op->exact_klass();
2788 intptr_t current_klass = op->current_klass();
2789 bool not_null = op->not_null();
2790 bool no_conflict = op->no_conflict();
2791
2792 Label update, next, none;
2793
2794 bool do_null = !not_null;
2795 bool exact_klass_set = exact_klass != nullptr && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2796 bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
2797
2798 assert(do_null || do_update, "why are we here?");
2799 assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
2800 assert(mdo_addr.base() != rscratch1, "wrong register");
2801
2802 __ verify_oop(obj);
2803
2804 if (tmp != obj) {
2805 assert_different_registers(obj, tmp, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2806 __ mov(tmp, obj);
2807 } else {
2808 assert_different_registers(obj, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2809 }
2810 if (do_null) {
2811 __ cbnz(tmp, update);
2812 if (!TypeEntries::was_null_seen(current_klass)) {
2813 __ ldr(rscratch2, mdo_addr);
2814 __ orr(rscratch2, rscratch2, TypeEntries::null_seen);
2815 __ str(rscratch2, mdo_addr);
2816 }
2817 if (do_update) {
2818 #ifndef ASSERT
2819 __ b(next);
2820 }
2821 #else
2822 __ b(next);
2823 }
2824 } else {
2825 __ cbnz(tmp, update);
2826 __ stop("unexpected null obj");
2827 #endif
2828 }
2829
2830 __ bind(update);
2831
2832 if (do_update) {
2833 #ifdef ASSERT
2834 if (exact_klass != nullptr) {
2835 Label ok;
2836 __ load_klass(tmp, tmp);
2837 __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2838 __ eor(rscratch1, tmp, rscratch1);
2839 __ cbz(rscratch1, ok);
2840 __ stop("exact klass and actual klass differ");
2841 __ bind(ok);
2842 }
2843 #endif
2844 if (!no_conflict) {
2845 if (exact_klass == nullptr || TypeEntries::is_type_none(current_klass)) {
2846 if (exact_klass != nullptr) {
2847 __ mov_metadata(tmp, exact_klass->constant_encoding());
2848 } else {
2849 __ load_klass(tmp, tmp);
2850 }
2851
2852 __ ldr(rscratch2, mdo_addr);
2853 __ eor(tmp, tmp, rscratch2);
2854 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2855 // klass seen before, nothing to do. The unknown bit may have been
2856 // set already but no need to check.
2857 __ cbz(rscratch1, next);
2858
2859 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2860
2861 if (TypeEntries::is_type_none(current_klass)) {
2862 __ cbz(rscratch2, none);
2863 __ cmp(rscratch2, (u1)TypeEntries::null_seen);
2864 __ br(Assembler::EQ, none);
2865 // There is a chance that the checks above
2866 // fail if another thread has just set the
2867 // profiling to this obj's klass
2868 __ dmb(Assembler::ISHLD);
2869 __ eor(tmp, tmp, rscratch2); // get back original value before XOR
2870 __ ldr(rscratch2, mdo_addr);
2871 __ eor(tmp, tmp, rscratch2);
2872 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2873 __ cbz(rscratch1, next);
2874 }
2875 } else {
2876 assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2877 ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
2878
2879 __ ldr(tmp, mdo_addr);
2880 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2881 }
2882
2883 // different than before. Cannot keep accurate profile.
2884 __ ldr(rscratch2, mdo_addr);
2885 __ orr(rscratch2, rscratch2, TypeEntries::type_unknown);
2886 __ str(rscratch2, mdo_addr);
2887
2888 if (TypeEntries::is_type_none(current_klass)) {
2889 __ b(next);
2890
2891 __ bind(none);
2892 // first time here. Set profile type.
2893 __ str(tmp, mdo_addr);
2894 #ifdef ASSERT
2895 __ andr(tmp, tmp, TypeEntries::type_mask);
2896 __ verify_klass_ptr(tmp);
2897 #endif
2898 }
2899 } else {
2900 // There's a single possible klass at this profile point
2901 assert(exact_klass != nullptr, "should be");
2902 if (TypeEntries::is_type_none(current_klass)) {
2903 __ mov_metadata(tmp, exact_klass->constant_encoding());
2904 __ ldr(rscratch2, mdo_addr);
2905 __ eor(tmp, tmp, rscratch2);
2906 __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2907 __ cbz(rscratch1, next);
2908 #ifdef ASSERT
2909 {
2910 Label ok;
2911 __ ldr(rscratch1, mdo_addr);
2912 __ cbz(rscratch1, ok);
2913 __ cmp(rscratch1, (u1)TypeEntries::null_seen);
2914 __ br(Assembler::EQ, ok);
2915 // may have been set by another thread
2916 __ dmb(Assembler::ISHLD);
2917 __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2918 __ ldr(rscratch2, mdo_addr);
2919 __ eor(rscratch2, rscratch1, rscratch2);
2920 __ andr(rscratch2, rscratch2, TypeEntries::type_mask);
2921 __ cbz(rscratch2, ok);
2922
2923 __ stop("unexpected profiling mismatch");
2924 __ bind(ok);
2925 }
2926 #endif
2927 // first time here. Set profile type.
2928 __ str(tmp, mdo_addr);
2929 #ifdef ASSERT
2930 __ andr(tmp, tmp, TypeEntries::type_mask);
2931 __ verify_klass_ptr(tmp);
2932 #endif
2933 } else {
2934 assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2935 ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
2936
2937 __ ldr(tmp, mdo_addr);
2938 __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2939
2940 __ orr(tmp, tmp, TypeEntries::type_unknown);
2941 __ str(tmp, mdo_addr);
2942 // FIXME: Write barrier needed here?
2943 }
2944 }
2945
2946 __ bind(next);
2947 }
2948 COMMENT("} emit_profile_type");
2949 }
2950
2951 void LIR_Assembler::emit_profile_inline_type(LIR_OpProfileInlineType* op) {
2952 Register obj = op->obj()->as_register();
2953 Register tmp = op->tmp()->as_pointer_register();
2954 bool not_null = op->not_null();
2955 int flag = op->flag();
2956
2957 Label not_inline_type;
2958 if (!not_null) {
2959 __ cbz(obj, not_inline_type);
2960 }
2961
2962 __ test_oop_is_not_inline_type(obj, tmp, not_inline_type);
2963
2964 Address mdo_addr = as_Address(op->mdp()->as_address_ptr(), rscratch2);
2965 __ ldrb(rscratch1, mdo_addr);
2966 __ orr(rscratch1, rscratch1, flag);
2967 __ strb(rscratch1, mdo_addr);
2968
2969 __ bind(not_inline_type);
2970 }
2971
2972 void LIR_Assembler::align_backward_branch_target() {
2973 }
2974
2975
2976 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
2977 // tmp must be unused
2978 assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2979
2980 if (left->is_single_cpu()) {
2981 assert(dest->is_single_cpu(), "expect single result reg");
2982 __ negw(dest->as_register(), left->as_register());
2983 } else if (left->is_double_cpu()) {
2984 assert(dest->is_double_cpu(), "expect double result reg");
2985 __ neg(dest->as_register_lo(), left->as_register_lo());
2986 } else if (left->is_single_fpu()) {
2987 assert(dest->is_single_fpu(), "expect single float result reg");
2988 __ fnegs(dest->as_float_reg(), left->as_float_reg());
2989 } else {
2990 assert(left->is_double_fpu(), "expect double float operand reg");
2991 assert(dest->is_double_fpu(), "expect double float result reg");
2992 __ fnegd(dest->as_double_reg(), left->as_double_reg());
2993 }
2994 }
2995
2996
2997 void LIR_Assembler::leal(LIR_Opr addr, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
2998 if (patch_code != lir_patch_none) {
2999 deoptimize_trap(info);
3000 return;
3001 }
3002
3003 __ lea(dest->as_pointer_register(), as_Address(addr->as_address_ptr()));
3004 }
3005
3006
3007 void LIR_Assembler::rt_call(LIR_Opr result, address dest, const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
3008 assert(!tmp->is_valid(), "don't need temporary");
3009
3010 CodeBlob *cb = CodeCache::find_blob(dest);
3011 if (cb) {
3012 __ far_call(RuntimeAddress(dest));
3013 } else {
3014 __ mov(rscratch1, RuntimeAddress(dest));
3015 __ blr(rscratch1);
3016 }
3017
3018 if (info != nullptr) {
3019 add_call_info_here(info);
3020 }
3021 __ post_call_nop();
3022 }
3023
3024 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
3025 if (src->is_address()) {
3026 mem2reg(src, dest, type, lir_patch_none, info, /*wide*/false, /*is_volatile*/true);
3027 } else if (dest->is_address()) {
3028 move_op(src, dest, type, lir_patch_none, info, /*wide*/false);
3029 } else {
3030 ShouldNotReachHere();
3031 }
3032 }
3033
3034 #ifdef ASSERT
3035 // emit run-time assertion
3036 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
3037 assert(op->code() == lir_assert, "must be");
3038
3039 if (op->in_opr1()->is_valid()) {
3040 assert(op->in_opr2()->is_valid(), "both operands must be valid");
3041 comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
3042 } else {
3043 assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
3044 assert(op->condition() == lir_cond_always, "no other conditions allowed");
3045 }
3046
3047 Label ok;
3048 if (op->condition() != lir_cond_always) {
3049 Assembler::Condition acond = Assembler::AL;
3050 switch (op->condition()) {
3051 case lir_cond_equal: acond = Assembler::EQ; break;
3052 case lir_cond_notEqual: acond = Assembler::NE; break;
3053 case lir_cond_less: acond = Assembler::LT; break;
3054 case lir_cond_lessEqual: acond = Assembler::LE; break;
3055 case lir_cond_greaterEqual: acond = Assembler::GE; break;
3056 case lir_cond_greater: acond = Assembler::GT; break;
3057 case lir_cond_belowEqual: acond = Assembler::LS; break;
3058 case lir_cond_aboveEqual: acond = Assembler::HS; break;
3059 default: ShouldNotReachHere();
3060 }
3061 __ br(acond, ok);
3062 }
3063 if (op->halt()) {
3064 const char* str = __ code_string(op->msg());
3065 __ stop(str);
3066 } else {
3067 breakpoint();
3068 }
3069 __ bind(ok);
3070 }
3071 #endif
3072
3073 #ifndef PRODUCT
3074 #define COMMENT(x) do { __ block_comment(x); } while (0)
3075 #else
3076 #define COMMENT(x)
3077 #endif
3078
3079 void LIR_Assembler::membar() {
3080 COMMENT("membar");
3081 __ membar(MacroAssembler::AnyAny);
3082 }
3083
3084 void LIR_Assembler::membar_acquire() {
3085 __ membar(Assembler::LoadLoad|Assembler::LoadStore);
3086 }
3087
3088 void LIR_Assembler::membar_release() {
3089 __ membar(Assembler::LoadStore|Assembler::StoreStore);
3090 }
3091
3092 void LIR_Assembler::membar_loadload() {
3093 __ membar(Assembler::LoadLoad);
3094 }
3095
3096 void LIR_Assembler::membar_storestore() {
3097 __ membar(MacroAssembler::StoreStore);
3098 }
3099
3100 void LIR_Assembler::membar_loadstore() { __ membar(MacroAssembler::LoadStore); }
3101
3102 void LIR_Assembler::membar_storeload() { __ membar(MacroAssembler::StoreLoad); }
3103
3104 void LIR_Assembler::on_spin_wait() {
3105 __ spin_wait();
3106 }
3107
3108 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
3109 __ mov(result_reg->as_register(), rthread);
3110 }
3111
3112 void LIR_Assembler::check_orig_pc() {
3113 __ ldr(rscratch2, frame_map()->address_for_orig_pc_addr());
3114 __ cmp(rscratch2, (u1)NULL_WORD);
3115 }
3116
3117 void LIR_Assembler::peephole(LIR_List *lir) {
3118 #if 0
3119 if (tableswitch_count >= max_tableswitches)
3120 return;
3121
3122 /*
3123 This finite-state automaton recognizes sequences of compare-and-
3124 branch instructions. We will turn them into a tableswitch. You
3125 could argue that C1 really shouldn't be doing this sort of
3126 optimization, but without it the code is really horrible.
3127 */
3128
3129 enum { start_s, cmp1_s, beq_s, cmp_s } state;
3130 int first_key, last_key = -2147483648;
3131 int next_key = 0;
3132 int start_insn = -1;
3133 int last_insn = -1;
3134 Register reg = noreg;
3135 LIR_Opr reg_opr;
3136 state = start_s;
3137
3138 LIR_OpList* inst = lir->instructions_list();
3139 for (int i = 0; i < inst->length(); i++) {
3140 LIR_Op* op = inst->at(i);
3141 switch (state) {
3142 case start_s:
3143 first_key = -1;
3144 start_insn = i;
3145 switch (op->code()) {
3146 case lir_cmp:
3147 LIR_Opr opr1 = op->as_Op2()->in_opr1();
3148 LIR_Opr opr2 = op->as_Op2()->in_opr2();
3149 if (opr1->is_cpu_register() && opr1->is_single_cpu()
3150 && opr2->is_constant()
3151 && opr2->type() == T_INT) {
3152 reg_opr = opr1;
3153 reg = opr1->as_register();
3154 first_key = opr2->as_constant_ptr()->as_jint();
3155 next_key = first_key + 1;
3156 state = cmp_s;
3157 goto next_state;
3158 }
3159 break;
3160 }
3161 break;
3162 case cmp_s:
3163 switch (op->code()) {
3164 case lir_branch:
3165 if (op->as_OpBranch()->cond() == lir_cond_equal) {
3166 state = beq_s;
3167 last_insn = i;
3168 goto next_state;
3169 }
3170 }
3171 state = start_s;
3172 break;
3173 case beq_s:
3174 switch (op->code()) {
3175 case lir_cmp: {
3176 LIR_Opr opr1 = op->as_Op2()->in_opr1();
3177 LIR_Opr opr2 = op->as_Op2()->in_opr2();
3178 if (opr1->is_cpu_register() && opr1->is_single_cpu()
3179 && opr1->as_register() == reg
3180 && opr2->is_constant()
3181 && opr2->type() == T_INT
3182 && opr2->as_constant_ptr()->as_jint() == next_key) {
3183 last_key = next_key;
3184 next_key++;
3185 state = cmp_s;
3186 goto next_state;
3187 }
3188 }
3189 }
3190 last_key = next_key;
3191 state = start_s;
3192 break;
3193 default:
3194 assert(false, "impossible state");
3195 }
3196 if (state == start_s) {
3197 if (first_key < last_key - 5L && reg != noreg) {
3198 {
3199 // printf("found run register %d starting at insn %d low value %d high value %d\n",
3200 // reg->encoding(),
3201 // start_insn, first_key, last_key);
3202 // for (int i = 0; i < inst->length(); i++) {
3203 // inst->at(i)->print();
3204 // tty->print("\n");
3205 // }
3206 // tty->print("\n");
3207 }
3208
3209 struct tableswitch *sw = &switches[tableswitch_count];
3210 sw->_insn_index = start_insn, sw->_first_key = first_key,
3211 sw->_last_key = last_key, sw->_reg = reg;
3212 inst->insert_before(last_insn + 1, new LIR_OpLabel(&sw->_after));
3213 {
3214 // Insert the new table of branches
3215 int offset = last_insn;
3216 for (int n = first_key; n < last_key; n++) {
3217 inst->insert_before
3218 (last_insn + 1,
3219 new LIR_OpBranch(lir_cond_always, T_ILLEGAL,
3220 inst->at(offset)->as_OpBranch()->label()));
3221 offset -= 2, i++;
3222 }
3223 }
3224 // Delete all the old compare-and-branch instructions
3225 for (int n = first_key; n < last_key; n++) {
3226 inst->remove_at(start_insn);
3227 inst->remove_at(start_insn);
3228 }
3229 // Insert the tableswitch instruction
3230 inst->insert_before(start_insn,
3231 new LIR_Op2(lir_cmp, lir_cond_always,
3232 LIR_OprFact::intConst(tableswitch_count),
3233 reg_opr));
3234 inst->insert_before(start_insn + 1, new LIR_OpLabel(&sw->_branches));
3235 tableswitch_count++;
3236 }
3237 reg = noreg;
3238 last_key = -2147483648;
3239 }
3240 next_state:
3241 ;
3242 }
3243 #endif
3244 }
3245
3246 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp_op) {
3247 Address addr = as_Address(src->as_address_ptr());
3248 BasicType type = src->type();
3249 bool is_oop = is_reference_type(type);
3250
3251 void (MacroAssembler::* add)(Register prev, RegisterOrConstant incr, Register addr);
3252 void (MacroAssembler::* xchg)(Register prev, Register newv, Register addr);
3253
3254 switch(type) {
3255 case T_INT:
3256 xchg = &MacroAssembler::atomic_xchgalw;
3257 add = &MacroAssembler::atomic_addalw;
3258 break;
3259 case T_LONG:
3260 xchg = &MacroAssembler::atomic_xchgal;
3261 add = &MacroAssembler::atomic_addal;
3262 break;
3263 case T_OBJECT:
3264 case T_ARRAY:
3265 if (UseCompressedOops) {
3266 xchg = &MacroAssembler::atomic_xchgalw;
3267 add = &MacroAssembler::atomic_addalw;
3268 } else {
3269 xchg = &MacroAssembler::atomic_xchgal;
3270 add = &MacroAssembler::atomic_addal;
3271 }
3272 break;
3273 default:
3274 ShouldNotReachHere();
3275 xchg = &MacroAssembler::atomic_xchgal;
3276 add = &MacroAssembler::atomic_addal; // unreachable
3277 }
3278
3279 switch (code) {
3280 case lir_xadd:
3281 {
3282 RegisterOrConstant inc;
3283 Register tmp = as_reg(tmp_op);
3284 Register dst = as_reg(dest);
3285 if (data->is_constant()) {
3286 inc = RegisterOrConstant(as_long(data));
3287 assert_different_registers(dst, addr.base(), tmp,
3288 rscratch1, rscratch2);
3289 } else {
3290 inc = RegisterOrConstant(as_reg(data));
3291 assert_different_registers(inc.as_register(), dst, addr.base(), tmp,
3292 rscratch1, rscratch2);
3293 }
3294 __ lea(tmp, addr);
3295 (_masm->*add)(dst, inc, tmp);
3296 break;
3297 }
3298 case lir_xchg:
3299 {
3300 Register tmp = tmp_op->as_register();
3301 Register obj = as_reg(data);
3302 Register dst = as_reg(dest);
3303 if (is_oop && UseCompressedOops) {
3304 __ encode_heap_oop(rscratch2, obj);
3305 obj = rscratch2;
3306 }
3307 assert_different_registers(obj, addr.base(), tmp, rscratch1);
3308 assert_different_registers(dst, addr.base(), tmp, rscratch1);
3309 __ lea(tmp, addr);
3310 (_masm->*xchg)(dst, obj, tmp);
3311 if (is_oop && UseCompressedOops) {
3312 __ decode_heap_oop(dst);
3313 }
3314 }
3315 break;
3316 default:
3317 ShouldNotReachHere();
3318 }
3319 if(!UseLSE) {
3320 __ membar(__ AnyAny);
3321 }
3322 }
3323
3324 #undef __