1 /*
2 * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "c1/c1_Compilation.hpp"
26 #include "c1/c1_FrameMap.hpp"
27 #include "c1/c1_Instruction.hpp"
28 #include "c1/c1_LIRAssembler.hpp"
29 #include "c1/c1_LIRGenerator.hpp"
30 #include "c1/c1_Runtime1.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciArray.hpp"
33 #include "ci/ciInlineKlass.hpp"
34 #include "ci/ciObjArrayKlass.hpp"
35 #include "ci/ciTypeArrayKlass.hpp"
36 #include "gc/shared/c1/barrierSetC1.hpp"
37 #include "runtime/sharedRuntime.hpp"
38 #include "runtime/stubRoutines.hpp"
39 #include "utilities/powerOfTwo.hpp"
40 #include "vmreg_x86.inline.hpp"
41
42 #ifdef ASSERT
43 #define __ gen()->lir(__FILE__, __LINE__)->
44 #else
45 #define __ gen()->lir()->
46 #endif
47
48 // Item will be loaded into a byte register; Intel only
49 void LIRItem::load_byte_item() {
50 load_item();
51 LIR_Opr res = result();
52
53 if (!res->is_virtual() || !_gen->is_vreg_flag_set(res, LIRGenerator::byte_reg)) {
54 // make sure that it is a byte register
55 assert(!value()->type()->is_float() && !value()->type()->is_double(),
56 "can't load floats in byte register");
57 LIR_Opr reg = _gen->rlock_byte(T_BYTE);
58 __ move(res, reg);
59
60 _result = reg;
61 }
62 }
63
64
65 void LIRItem::load_nonconstant() {
66 LIR_Opr r = value()->operand();
67 if (r->is_constant()) {
68 _result = r;
69 } else {
70 load_item();
71 }
72 }
73
74 //--------------------------------------------------------------
75 // LIRGenerator
76 //--------------------------------------------------------------
77
78
79 LIR_Opr LIRGenerator::exceptionOopOpr() { return FrameMap::rax_oop_opr; }
80 LIR_Opr LIRGenerator::exceptionPcOpr() { return FrameMap::rdx_opr; }
81 LIR_Opr LIRGenerator::divInOpr() { return FrameMap::rax_opr; }
82 LIR_Opr LIRGenerator::divOutOpr() { return FrameMap::rax_opr; }
83 LIR_Opr LIRGenerator::remOutOpr() { return FrameMap::rdx_opr; }
84 LIR_Opr LIRGenerator::shiftCountOpr() { return FrameMap::rcx_opr; }
85 LIR_Opr LIRGenerator::syncLockOpr() { return new_register(T_INT); }
86 LIR_Opr LIRGenerator::syncTempOpr() { return FrameMap::rax_opr; }
87 LIR_Opr LIRGenerator::getThreadTemp() { return LIR_OprFact::illegalOpr; }
88
89
90 LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
91 LIR_Opr opr;
92 switch (type->tag()) {
93 case intTag: opr = FrameMap::rax_opr; break;
94 case objectTag: opr = FrameMap::rax_oop_opr; break;
95 case longTag: opr = FrameMap::long0_opr; break;
96 case floatTag: opr = FrameMap::xmm0_float_opr; break;
97 case doubleTag: opr = FrameMap::xmm0_double_opr; break;
98 case addressTag:
99 default: ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
100 }
101
102 assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
103 return opr;
104 }
105
106
107 LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
108 LIR_Opr reg = new_register(T_INT);
109 set_vreg_flag(reg, LIRGenerator::byte_reg);
110 return reg;
111 }
112
113
114 //--------- loading items into registers --------------------------------
115
116
117 // i486 instructions can inline constants
118 bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
119 if (type == T_SHORT || type == T_CHAR) {
120 return false;
121 }
122 Constant* c = v->as_Constant();
123 if (c && c->state_before() == nullptr) {
124 // constants of any type can be stored directly, except for
125 // unloaded object constants.
126 return true;
127 }
128 return false;
129 }
130
131
132 bool LIRGenerator::can_inline_as_constant(Value v) const {
133 if (v->type()->tag() == longTag) return false;
134 return v->type()->tag() != objectTag ||
135 (v->type()->is_constant() && v->type()->as_ObjectType()->constant_value()->is_null_object());
136 }
137
138
139 bool LIRGenerator::can_inline_as_constant(LIR_Const* c) const {
140 if (c->type() == T_LONG) return false;
141 return c->type() != T_OBJECT || c->as_jobject() == nullptr;
142 }
143
144
145 LIR_Opr LIRGenerator::safepoint_poll_register() {
146 return LIR_OprFact::illegalOpr;
147 }
148
149
150 LIR_Address* LIRGenerator::generate_address(LIR_Opr base, LIR_Opr index,
151 int shift, int disp, BasicType type) {
152 assert(base->is_register(), "must be");
153 if (index->is_constant()) {
154 LIR_Const *constant = index->as_constant_ptr();
155 jlong c;
156 if (constant->type() == T_INT) {
157 c = (jlong(index->as_jint()) << shift) + disp;
158 } else {
159 assert(constant->type() == T_LONG, "should be");
160 c = (index->as_jlong() << shift) + disp;
161 }
162 if ((jlong)((jint)c) == c) {
163 return new LIR_Address(base, (jint)c, type);
164 } else {
165 LIR_Opr tmp = new_register(T_LONG);
166 __ move(index, tmp);
167 return new LIR_Address(base, tmp, type);
168 }
169 } else {
170 return new LIR_Address(base, index, (LIR_Address::Scale)shift, disp, type);
171 }
172 }
173
174
175 LIR_Address* LIRGenerator::emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr,
176 BasicType type) {
177 int offset_in_bytes = arrayOopDesc::base_offset_in_bytes(type);
178
179 LIR_Address* addr;
180 if (index_opr->is_constant()) {
181 int elem_size = type2aelembytes(type);
182 jint index = index_opr->as_jint();
183 jlong disp = offset_in_bytes + (jlong)(index) * elem_size;
184 if (disp > max_jint) {
185 // Displacement overflow. Cannot directly use instruction with 32-bit displacement for 64-bit addresses.
186 // Convert array index to long to do array offset computation with 64-bit values.
187 index_opr = new_register(T_LONG);
188 __ move(LIR_OprFact::longConst(index), index_opr);
189 addr = new LIR_Address(array_opr, index_opr, LIR_Address::scale(type), offset_in_bytes, type);
190 } else {
191 addr = new LIR_Address(array_opr, (intx)disp, type);
192 }
193 } else {
194 if (index_opr->type() == T_INT) {
195 LIR_Opr tmp = new_register(T_LONG);
196 __ convert(Bytecodes::_i2l, index_opr, tmp);
197 index_opr = tmp;
198 }
199 addr = new LIR_Address(array_opr,
200 index_opr,
201 LIR_Address::scale(type),
202 offset_in_bytes, type);
203 }
204 return addr;
205 }
206
207
208 LIR_Opr LIRGenerator::load_immediate(jlong x, BasicType type) {
209 LIR_Opr r;
210 if (type == T_LONG) {
211 r = LIR_OprFact::longConst(x);
212 } else if (type == T_INT) {
213 r = LIR_OprFact::intConst(checked_cast<jint>(x));
214 } else {
215 ShouldNotReachHere();
216 }
217 return r;
218 }
219
220 void LIRGenerator::increment_counter(address counter, BasicType type, int step) {
221 LIR_Opr pointer = new_pointer_register();
222 __ move(LIR_OprFact::intptrConst(counter), pointer);
223 LIR_Address* addr = new LIR_Address(pointer, type);
224 increment_counter(addr, step);
225 }
226
227
228 void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
229 __ add((LIR_Opr)addr, LIR_OprFact::intConst(step), (LIR_Opr)addr);
230 }
231
232 void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
233 __ cmp_mem_int(condition, base, disp, c, info);
234 }
235
236
237 void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info) {
238 __ cmp_reg_mem(condition, reg, new LIR_Address(base, disp, type), info);
239 }
240
241
242 bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, jint c, LIR_Opr result, LIR_Opr tmp) {
243 if (tmp->is_valid() && c > 0 && c < max_jint) {
244 if (is_power_of_2(c + 1)) {
245 __ move(left, tmp);
246 __ shift_left(left, log2i_exact(c + 1), left);
247 __ sub(left, tmp, result);
248 return true;
249 } else if (is_power_of_2(c - 1)) {
250 __ move(left, tmp);
251 __ shift_left(left, log2i_exact(c - 1), left);
252 __ add(left, tmp, result);
253 return true;
254 }
255 }
256 return false;
257 }
258
259
260 void LIRGenerator::store_stack_parameter (LIR_Opr item, ByteSize offset_from_sp) {
261 BasicType type = item->type();
262 __ store(item, new LIR_Address(FrameMap::rsp_opr, in_bytes(offset_from_sp), type));
263 }
264
265 void LIRGenerator::array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info, ciMethod* profiled_method, int profiled_bci) {
266 LIR_Opr tmp1 = new_register(objectType);
267 LIR_Opr tmp2 = new_register(objectType);
268 LIR_Opr tmp3 = new_register(objectType);
269 __ store_check(value, array, tmp1, tmp2, tmp3, store_check_info, profiled_method, profiled_bci);
270 }
271
272 //----------------------------------------------------------------------
273 // visitor functions
274 //----------------------------------------------------------------------
275
276 void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
277 assert(x->is_pinned(),"");
278 LIRItem obj(x->obj(), this);
279 obj.load_item();
280
281 set_no_result(x);
282
283 // "lock" stores the address of the monitor stack slot, so this is not an oop
284 LIR_Opr lock = new_register(T_INT);
285 // Need a scratch register for inline types on x86
286 LIR_Opr scratch = new_register(T_ADDRESS);
287
288 CodeEmitInfo* info_for_exception = nullptr;
289 if (x->needs_null_check()) {
290 info_for_exception = state_for(x);
291 }
292
293 CodeStub* throw_ie_stub = x->maybe_inlinetype() ?
294 new SimpleExceptionStub(StubId::c1_throw_identity_exception_id,
295 obj.result(), state_for(x))
296 : nullptr;
297
298 // this CodeEmitInfo must not have the xhandlers because here the
299 // object is already locked (xhandlers expect object to be unlocked)
300 CodeEmitInfo* info = state_for(x, x->state(), true);
301 monitor_enter(obj.result(), lock, syncTempOpr(), scratch,
302 x->monitor_no(), info_for_exception, info, throw_ie_stub);
303 }
304
305
306 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
307 assert(x->is_pinned(),"");
308
309 LIRItem obj(x->obj(), this);
310 obj.dont_load_item();
311
312 LIR_Opr lock = new_register(T_INT);
313 LIR_Opr obj_temp = new_register(T_INT);
314 set_no_result(x);
315 monitor_exit(obj_temp, lock, syncTempOpr(), LIR_OprFact::illegalOpr, x->monitor_no());
316 }
317
318 // _ineg, _lneg, _fneg, _dneg
319 void LIRGenerator::do_NegateOp(NegateOp* x) {
320 LIRItem value(x->x(), this);
321 value.set_destroys_register();
322 value.load_item();
323 LIR_Opr reg = rlock(x);
324
325 __ negate(value.result(), reg);
326
327 set_result(x, reg);
328 }
329
330 // for _fadd, _fmul, _fsub, _fdiv, _frem
331 // _dadd, _dmul, _dsub, _ddiv, _drem
332 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
333 LIRItem left(x->x(), this);
334 LIRItem right(x->y(), this);
335 LIRItem* left_arg = &left;
336 LIRItem* right_arg = &right;
337 assert(!left.is_stack() || !right.is_stack(), "can't both be memory operands");
338 bool must_load_both = (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem);
339 if (left.is_register() || x->x()->type()->is_constant() || must_load_both) {
340 left.load_item();
341 } else {
342 left.dont_load_item();
343 }
344
345 if (must_load_both) {
346 // frem and drem destroy also right operand, so move it to a new register
347 right.set_destroys_register();
348 right.load_item();
349 } else if (right.is_register()) {
350 right.load_item();
351 } else {
352 right.dont_load_item();
353 }
354 LIR_Opr reg = rlock(x);
355 LIR_Opr tmp = LIR_OprFact::illegalOpr;
356 if (x->op() == Bytecodes::_dmul || x->op() == Bytecodes::_ddiv) {
357 tmp = new_register(T_DOUBLE);
358 }
359
360 if (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem) {
361 // frem and drem are implemented as a direct call into the runtime.
362 LIRItem left(x->x(), this);
363 LIRItem right(x->y(), this);
364
365 BasicType bt = as_BasicType(x->type());
366 BasicTypeList signature(2);
367 signature.append(bt);
368 signature.append(bt);
369 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
370
371 const LIR_Opr result_reg = result_register_for(x->type());
372 left.load_item_force(cc->at(0));
373 right.load_item_force(cc->at(1));
374
375 address entry = nullptr;
376 switch (x->op()) {
377 case Bytecodes::_frem:
378 entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
379 break;
380 case Bytecodes::_drem:
381 entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
382 break;
383 default:
384 ShouldNotReachHere();
385 }
386
387 LIR_Opr result = rlock_result(x);
388 __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
389 __ move(result_reg, result);
390 } else {
391 arithmetic_op_fpu(x->op(), reg, left.result(), right.result(), tmp);
392 set_result(x, reg);
393 }
394 }
395
396
397 // for _ladd, _lmul, _lsub, _ldiv, _lrem
398 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
399 if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem ) {
400 // long division is implemented as a direct call into the runtime
401 LIRItem left(x->x(), this);
402 LIRItem right(x->y(), this);
403
404 // the check for division by zero destroys the right operand
405 right.set_destroys_register();
406
407 BasicTypeList signature(2);
408 signature.append(T_LONG);
409 signature.append(T_LONG);
410 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
411
412 // check for division by zero (destroys registers of right operand!)
413 CodeEmitInfo* info = state_for(x);
414
415 const LIR_Opr result_reg = result_register_for(x->type());
416 left.load_item_force(cc->at(1));
417 right.load_item();
418
419 __ move(right.result(), cc->at(0));
420
421 __ cmp(lir_cond_equal, right.result(), LIR_OprFact::longConst(0));
422 __ branch(lir_cond_equal, new DivByZeroStub(info));
423
424 address entry = nullptr;
425 switch (x->op()) {
426 case Bytecodes::_lrem:
427 entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
428 break; // check if dividend is 0 is done elsewhere
429 case Bytecodes::_ldiv:
430 entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
431 break; // check if dividend is 0 is done elsewhere
432 default:
433 ShouldNotReachHere();
434 }
435
436 LIR_Opr result = rlock_result(x);
437 __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
438 __ move(result_reg, result);
439 } else if (x->op() == Bytecodes::_lmul) {
440 // missing test if instr is commutative and if we should swap
441 LIRItem left(x->x(), this);
442 LIRItem right(x->y(), this);
443
444 // right register is destroyed by the long mul, so it must be
445 // copied to a new register.
446 right.set_destroys_register();
447
448 left.load_item();
449 right.load_item();
450
451 LIR_Opr reg = FrameMap::long0_opr;
452 arithmetic_op_long(x->op(), reg, left.result(), right.result(), nullptr);
453 LIR_Opr result = rlock_result(x);
454 __ move(reg, result);
455 } else {
456 // missing test if instr is commutative and if we should swap
457 LIRItem left(x->x(), this);
458 LIRItem right(x->y(), this);
459
460 left.load_item();
461 // don't load constants to save register
462 right.load_nonconstant();
463 rlock_result(x);
464 arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), nullptr);
465 }
466 }
467
468
469
470 // for: _iadd, _imul, _isub, _idiv, _irem
471 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
472 if (x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem) {
473 // The requirements for division and modulo
474 // input : rax,: dividend min_int
475 // reg: divisor (may not be rax,/rdx) -1
476 //
477 // output: rax,: quotient (= rax, idiv reg) min_int
478 // rdx: remainder (= rax, irem reg) 0
479
480 // rax, and rdx will be destroyed
481
482 // Note: does this invalidate the spec ???
483 LIRItem right(x->y(), this);
484 LIRItem left(x->x() , this); // visit left second, so that the is_register test is valid
485
486 // call state_for before load_item_force because state_for may
487 // force the evaluation of other instructions that are needed for
488 // correct debug info. Otherwise the live range of the fix
489 // register might be too long.
490 CodeEmitInfo* info = state_for(x);
491
492 left.load_item_force(divInOpr());
493
494 right.load_item();
495
496 LIR_Opr result = rlock_result(x);
497 LIR_Opr result_reg;
498 if (x->op() == Bytecodes::_idiv) {
499 result_reg = divOutOpr();
500 } else {
501 result_reg = remOutOpr();
502 }
503
504 if (!ImplicitDiv0Checks) {
505 __ cmp(lir_cond_equal, right.result(), LIR_OprFact::intConst(0));
506 __ branch(lir_cond_equal, new DivByZeroStub(info));
507 // Idiv/irem cannot trap (passing info would generate an assertion).
508 info = nullptr;
509 }
510 LIR_Opr tmp = FrameMap::rdx_opr; // idiv and irem use rdx in their implementation
511 if (x->op() == Bytecodes::_irem) {
512 __ irem(left.result(), right.result(), result_reg, tmp, info);
513 } else if (x->op() == Bytecodes::_idiv) {
514 __ idiv(left.result(), right.result(), result_reg, tmp, info);
515 } else {
516 ShouldNotReachHere();
517 }
518
519 __ move(result_reg, result);
520 } else {
521 // missing test if instr is commutative and if we should swap
522 LIRItem left(x->x(), this);
523 LIRItem right(x->y(), this);
524 LIRItem* left_arg = &left;
525 LIRItem* right_arg = &right;
526 if (x->is_commutative() && left.is_stack() && right.is_register()) {
527 // swap them if left is real stack (or cached) and right is real register(not cached)
528 left_arg = &right;
529 right_arg = &left;
530 }
531
532 left_arg->load_item();
533
534 // do not need to load right, as we can handle stack and constants
535 if (x->op() == Bytecodes::_imul ) {
536 // check if we can use shift instead
537 bool use_constant = false;
538 bool use_tmp = false;
539 if (right_arg->is_constant()) {
540 jint iconst = right_arg->get_jint_constant();
541 if (iconst > 0 && iconst < max_jint) {
542 if (is_power_of_2(iconst)) {
543 use_constant = true;
544 } else if (is_power_of_2(iconst - 1) || is_power_of_2(iconst + 1)) {
545 use_constant = true;
546 use_tmp = true;
547 }
548 }
549 }
550 if (use_constant) {
551 right_arg->dont_load_item();
552 } else {
553 right_arg->load_item();
554 }
555 LIR_Opr tmp = LIR_OprFact::illegalOpr;
556 if (use_tmp) {
557 tmp = new_register(T_INT);
558 }
559 rlock_result(x);
560
561 arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
562 } else {
563 right_arg->dont_load_item();
564 rlock_result(x);
565 LIR_Opr tmp = LIR_OprFact::illegalOpr;
566 arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
567 }
568 }
569 }
570
571
572 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
573 // when an operand with use count 1 is the left operand, then it is
574 // likely that no move for 2-operand-LIR-form is necessary
575 if (x->is_commutative() && x->y()->as_Constant() == nullptr && x->x()->use_count() > x->y()->use_count()) {
576 x->swap_operands();
577 }
578
579 ValueTag tag = x->type()->tag();
580 assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
581 switch (tag) {
582 case floatTag:
583 case doubleTag: do_ArithmeticOp_FPU(x); return;
584 case longTag: do_ArithmeticOp_Long(x); return;
585 case intTag: do_ArithmeticOp_Int(x); return;
586 default: ShouldNotReachHere(); return;
587 }
588 }
589
590
591 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
592 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
593 // count must always be in rcx
594 LIRItem value(x->x(), this);
595 LIRItem count(x->y(), this);
596
597 ValueTag elemType = x->type()->tag();
598 bool must_load_count = !count.is_constant() || elemType == longTag;
599 if (must_load_count) {
600 // count for long must be in register
601 count.load_item_force(shiftCountOpr());
602 } else {
603 count.dont_load_item();
604 }
605 value.load_item();
606 LIR_Opr reg = rlock_result(x);
607
608 shift_op(x->op(), reg, value.result(), count.result(), LIR_OprFact::illegalOpr);
609 }
610
611
612 // _iand, _land, _ior, _lor, _ixor, _lxor
613 void LIRGenerator::do_LogicOp(LogicOp* x) {
614 // when an operand with use count 1 is the left operand, then it is
615 // likely that no move for 2-operand-LIR-form is necessary
616 if (x->is_commutative() && x->y()->as_Constant() == nullptr && x->x()->use_count() > x->y()->use_count()) {
617 x->swap_operands();
618 }
619
620 LIRItem left(x->x(), this);
621 LIRItem right(x->y(), this);
622
623 left.load_item();
624 right.load_nonconstant();
625 LIR_Opr reg = rlock_result(x);
626
627 logic_op(x->op(), reg, left.result(), right.result());
628 }
629
630
631
632 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
633 void LIRGenerator::do_CompareOp(CompareOp* x) {
634 LIRItem left(x->x(), this);
635 LIRItem right(x->y(), this);
636 ValueTag tag = x->x()->type()->tag();
637 if (tag == longTag) {
638 left.set_destroys_register();
639 }
640 left.load_item();
641 right.load_item();
642 LIR_Opr reg = rlock_result(x);
643
644 if (x->x()->type()->is_float_kind()) {
645 Bytecodes::Code code = x->op();
646 __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
647 } else if (x->x()->type()->tag() == longTag) {
648 __ lcmp2int(left.result(), right.result(), reg);
649 } else {
650 Unimplemented();
651 }
652 }
653
654 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
655 LIR_Opr ill = LIR_OprFact::illegalOpr; // for convenience
656 if (is_reference_type(type)) {
657 cmp_value.load_item_force(FrameMap::rax_oop_opr);
658 new_value.load_item();
659 __ cas_obj(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
660 } else if (type == T_INT) {
661 cmp_value.load_item_force(FrameMap::rax_opr);
662 new_value.load_item();
663 __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
664 } else if (type == T_LONG) {
665 cmp_value.load_item_force(FrameMap::long0_opr);
666 new_value.load_item_force(FrameMap::long1_opr);
667 __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
668 } else {
669 Unimplemented();
670 }
671 LIR_Opr result = new_register(T_INT);
672 __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0),
673 result, T_INT);
674 return result;
675 }
676
677 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
678 bool is_oop = is_reference_type(type);
679 LIR_Opr result = new_register(type);
680 value.load_item();
681 // Because we want a 2-arg form of xchg and xadd
682 __ move(value.result(), result);
683 assert(type == T_INT || is_oop || type == T_LONG, "unexpected type");
684 __ xchg(addr, result, result, LIR_OprFact::illegalOpr);
685 return result;
686 }
687
688 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
689 LIR_Opr result = new_register(type);
690 value.load_item();
691 // Because we want a 2-arg form of xchg and xadd
692 __ move(value.result(), result);
693 assert(type == T_INT || type == T_LONG, "unexpected type");
694 __ xadd(addr, result, result, LIR_OprFact::illegalOpr);
695 return result;
696 }
697
698 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
699 assert(x->number_of_arguments() == 3, "wrong type");
700 assert(UseFMA, "Needs FMA instructions support.");
701 LIRItem value(x->argument_at(0), this);
702 LIRItem value1(x->argument_at(1), this);
703 LIRItem value2(x->argument_at(2), this);
704
705 value2.set_destroys_register();
706
707 value.load_item();
708 value1.load_item();
709 value2.load_item();
710
711 LIR_Opr calc_input = value.result();
712 LIR_Opr calc_input1 = value1.result();
713 LIR_Opr calc_input2 = value2.result();
714 LIR_Opr calc_result = rlock_result(x);
715
716 switch (x->id()) {
717 case vmIntrinsics::_fmaD: __ fmad(calc_input, calc_input1, calc_input2, calc_result); break;
718 case vmIntrinsics::_fmaF: __ fmaf(calc_input, calc_input1, calc_input2, calc_result); break;
719 default: ShouldNotReachHere();
720 }
721
722 }
723
724
725 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
726 assert(x->number_of_arguments() == 1 || (x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow), "wrong type");
727
728 if (x->id() == vmIntrinsics::_dexp || x->id() == vmIntrinsics::_dlog ||
729 x->id() == vmIntrinsics::_dpow || x->id() == vmIntrinsics::_dcos ||
730 x->id() == vmIntrinsics::_dsin || x->id() == vmIntrinsics::_dtan ||
731 x->id() == vmIntrinsics::_dlog10 || x->id() == vmIntrinsics::_dsinh ||
732 x->id() == vmIntrinsics::_dtanh || x->id() == vmIntrinsics::_dcbrt
733 ) {
734 do_LibmIntrinsic(x);
735 return;
736 }
737
738 LIRItem value(x->argument_at(0), this);
739
740 value.load_item();
741
742 LIR_Opr calc_input = value.result();
743 LIR_Opr calc_result = rlock_result(x);
744
745 LIR_Opr tmp = LIR_OprFact::illegalOpr;
746 if (x->id() == vmIntrinsics::_floatToFloat16) {
747 tmp = new_register(T_FLOAT);
748 }
749
750 switch(x->id()) {
751 case vmIntrinsics::_dabs:
752 __ abs(calc_input, calc_result, tmp);
753 break;
754 case vmIntrinsics::_dsqrt:
755 case vmIntrinsics::_dsqrt_strict:
756 __ sqrt(calc_input, calc_result, LIR_OprFact::illegalOpr);
757 break;
758 case vmIntrinsics::_floatToFloat16:
759 __ f2hf(calc_input, calc_result, tmp);
760 break;
761 case vmIntrinsics::_float16ToFloat:
762 __ hf2f(calc_input, calc_result, LIR_OprFact::illegalOpr);
763 break;
764 default:
765 ShouldNotReachHere();
766 }
767 }
768
769 void LIRGenerator::do_LibmIntrinsic(Intrinsic* x) {
770 LIRItem value(x->argument_at(0), this);
771 value.set_destroys_register();
772
773 LIR_Opr calc_result = rlock_result(x);
774 LIR_Opr result_reg = result_register_for(x->type());
775
776 CallingConvention* cc = nullptr;
777
778 if (x->id() == vmIntrinsics::_dpow) {
779 LIRItem value1(x->argument_at(1), this);
780
781 value1.set_destroys_register();
782
783 BasicTypeList signature(2);
784 signature.append(T_DOUBLE);
785 signature.append(T_DOUBLE);
786 cc = frame_map()->c_calling_convention(&signature);
787 value.load_item_force(cc->at(0));
788 value1.load_item_force(cc->at(1));
789 } else {
790 BasicTypeList signature(1);
791 signature.append(T_DOUBLE);
792 cc = frame_map()->c_calling_convention(&signature);
793 value.load_item_force(cc->at(0));
794 }
795
796 switch (x->id()) {
797 case vmIntrinsics::_dexp:
798 if (StubRoutines::dexp() != nullptr) {
799 __ call_runtime_leaf(StubRoutines::dexp(), getThreadTemp(), result_reg, cc->args());
800 } else {
801 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dexp), getThreadTemp(), result_reg, cc->args());
802 }
803 break;
804 case vmIntrinsics::_dlog:
805 if (StubRoutines::dlog() != nullptr) {
806 __ call_runtime_leaf(StubRoutines::dlog(), getThreadTemp(), result_reg, cc->args());
807 } else {
808 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog), getThreadTemp(), result_reg, cc->args());
809 }
810 break;
811 case vmIntrinsics::_dlog10:
812 if (StubRoutines::dlog10() != nullptr) {
813 __ call_runtime_leaf(StubRoutines::dlog10(), getThreadTemp(), result_reg, cc->args());
814 } else {
815 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog10), getThreadTemp(), result_reg, cc->args());
816 }
817 break;
818 case vmIntrinsics::_dpow:
819 if (StubRoutines::dpow() != nullptr) {
820 __ call_runtime_leaf(StubRoutines::dpow(), getThreadTemp(), result_reg, cc->args());
821 } else {
822 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), getThreadTemp(), result_reg, cc->args());
823 }
824 break;
825 case vmIntrinsics::_dsin:
826 if (StubRoutines::dsin() != nullptr) {
827 __ call_runtime_leaf(StubRoutines::dsin(), getThreadTemp(), result_reg, cc->args());
828 } else {
829 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dsin), getThreadTemp(), result_reg, cc->args());
830 }
831 break;
832 case vmIntrinsics::_dcos:
833 if (StubRoutines::dcos() != nullptr) {
834 __ call_runtime_leaf(StubRoutines::dcos(), getThreadTemp(), result_reg, cc->args());
835 } else {
836 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dcos), getThreadTemp(), result_reg, cc->args());
837 }
838 break;
839 case vmIntrinsics::_dtan:
840 if (StubRoutines::dtan() != nullptr) {
841 __ call_runtime_leaf(StubRoutines::dtan(), getThreadTemp(), result_reg, cc->args());
842 } else {
843 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtan), getThreadTemp(), result_reg, cc->args());
844 }
845 break;
846 case vmIntrinsics::_dsinh:
847 assert(StubRoutines::dsinh() != nullptr, "sinh intrinsic not found");
848 if (StubRoutines::dsinh() != nullptr) {
849 __ call_runtime_leaf(StubRoutines::dsinh(), getThreadTemp(), result_reg, cc->args());
850 }
851 break;
852 case vmIntrinsics::_dtanh:
853 assert(StubRoutines::dtanh() != nullptr, "tanh intrinsic not found");
854 if (StubRoutines::dtanh() != nullptr) {
855 __ call_runtime_leaf(StubRoutines::dtanh(), getThreadTemp(), result_reg, cc->args());
856 }
857 break;
858 case vmIntrinsics::_dcbrt:
859 assert(StubRoutines::dcbrt() != nullptr, "cbrt intrinsic not found");
860 if (StubRoutines::dcbrt() != nullptr) {
861 __ call_runtime_leaf(StubRoutines::dcbrt(), getThreadTemp(), result_reg, cc->args());
862 }
863 break;
864 default: ShouldNotReachHere();
865 }
866
867 __ move(result_reg, calc_result);
868 }
869
870 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
871 assert(x->number_of_arguments() == 5, "wrong type");
872
873 // Make all state_for calls early since they can emit code
874 CodeEmitInfo* info = nullptr;
875 if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
876 info = state_for(x, x->state_before());
877 info->set_force_reexecute();
878 } else {
879 info = state_for(x, x->state());
880 }
881
882 LIRItem src(x->argument_at(0), this);
883 LIRItem src_pos(x->argument_at(1), this);
884 LIRItem dst(x->argument_at(2), this);
885 LIRItem dst_pos(x->argument_at(3), this);
886 LIRItem length(x->argument_at(4), this);
887
888 // operands for arraycopy must use fixed registers, otherwise
889 // LinearScan will fail allocation (because arraycopy always needs a
890 // call)
891
892 int flags;
893 ciArrayKlass* expected_type;
894 arraycopy_helper(x, &flags, &expected_type);
895 if (x->check_flag(Instruction::OmitChecksFlag)) {
896 flags = 0;
897 }
898
899 // The java calling convention will give us enough registers
900 // so that on the stub side the args will be perfect already.
901 // On the other slow/special case side we call C and the arg
902 // positions are not similar enough to pick one as the best.
903 // Also because the java calling convention is a "shifted" version
904 // of the C convention we can process the java args trivially into C
905 // args without worry of overwriting during the xfer
906
907 src.load_item_force (FrameMap::as_oop_opr(j_rarg0));
908 src_pos.load_item_force (FrameMap::as_opr(j_rarg1));
909 dst.load_item_force (FrameMap::as_oop_opr(j_rarg2));
910 dst_pos.load_item_force (FrameMap::as_opr(j_rarg3));
911 length.load_item_force (FrameMap::as_opr(j_rarg4));
912
913 LIR_Opr tmp = FrameMap::as_opr(j_rarg5);
914
915 set_no_result(x);
916
917 __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(), tmp, expected_type, flags, info); // does add_safepoint
918 }
919
920 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
921 assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions support");
922 // Make all state_for calls early since they can emit code
923 LIR_Opr result = rlock_result(x);
924 int flags = 0;
925 switch (x->id()) {
926 case vmIntrinsics::_updateCRC32: {
927 LIRItem crc(x->argument_at(0), this);
928 LIRItem val(x->argument_at(1), this);
929 // val is destroyed by update_crc32
930 val.set_destroys_register();
931 crc.load_item();
932 val.load_item();
933 __ update_crc32(crc.result(), val.result(), result);
934 break;
935 }
936 case vmIntrinsics::_updateBytesCRC32:
937 case vmIntrinsics::_updateByteBufferCRC32: {
938 bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32);
939
940 LIRItem crc(x->argument_at(0), this);
941 LIRItem buf(x->argument_at(1), this);
942 LIRItem off(x->argument_at(2), this);
943 LIRItem len(x->argument_at(3), this);
944 buf.load_item();
945 off.load_nonconstant();
946
947 LIR_Opr index = off.result();
948 int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
949 if(off.result()->is_constant()) {
950 index = LIR_OprFact::illegalOpr;
951 offset += off.result()->as_jint();
952 }
953 LIR_Opr base_op = buf.result();
954
955 if (index->is_valid()) {
956 LIR_Opr tmp = new_register(T_LONG);
957 __ convert(Bytecodes::_i2l, index, tmp);
958 index = tmp;
959 }
960
961 LIR_Address* a = new LIR_Address(base_op,
962 index,
963 offset,
964 T_BYTE);
965 BasicTypeList signature(3);
966 signature.append(T_INT);
967 signature.append(T_ADDRESS);
968 signature.append(T_INT);
969 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
970 const LIR_Opr result_reg = result_register_for(x->type());
971
972 LIR_Opr addr = new_register(T_ADDRESS);
973 __ leal(LIR_OprFact::address(a), addr);
974
975 crc.load_item_force(cc->at(0));
976 __ move(addr, cc->at(1));
977 len.load_item_force(cc->at(2));
978
979 __ call_runtime_leaf(StubRoutines::updateBytesCRC32(), getThreadTemp(), result_reg, cc->args());
980 __ move(result_reg, result);
981
982 break;
983 }
984 default: {
985 ShouldNotReachHere();
986 }
987 }
988 }
989
990 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
991 assert(UseCRC32CIntrinsics, "need AVX and CLMUL instructions support");
992 LIR_Opr result = rlock_result(x);
993
994 switch (x->id()) {
995 case vmIntrinsics::_updateBytesCRC32C:
996 case vmIntrinsics::_updateDirectByteBufferCRC32C: {
997 bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32C);
998
999 LIRItem crc(x->argument_at(0), this);
1000 LIRItem buf(x->argument_at(1), this);
1001 LIRItem off(x->argument_at(2), this);
1002 LIRItem end(x->argument_at(3), this);
1003 buf.load_item();
1004 off.load_nonconstant();
1005 end.load_nonconstant();
1006
1007 // len = end - off
1008 LIR_Opr len = end.result();
1009 LIR_Opr tmpA = new_register(T_INT);
1010 LIR_Opr tmpB = new_register(T_INT);
1011 __ move(end.result(), tmpA);
1012 __ move(off.result(), tmpB);
1013 __ sub(tmpA, tmpB, tmpA);
1014 len = tmpA;
1015
1016 LIR_Opr index = off.result();
1017 int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
1018 if (off.result()->is_constant()) {
1019 index = LIR_OprFact::illegalOpr;
1020 offset += off.result()->as_jint();
1021 }
1022 LIR_Opr base_op = buf.result();
1023 LIR_Address* a = nullptr;
1024
1025 if (index->is_valid()) {
1026 LIR_Opr tmp = new_register(T_LONG);
1027 __ convert(Bytecodes::_i2l, index, tmp);
1028 index = tmp;
1029 a = new LIR_Address(base_op, index, offset, T_BYTE);
1030 } else {
1031 a = new LIR_Address(base_op, offset, T_BYTE);
1032 }
1033
1034 BasicTypeList signature(3);
1035 signature.append(T_INT);
1036 signature.append(T_ADDRESS);
1037 signature.append(T_INT);
1038 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1039 const LIR_Opr result_reg = result_register_for(x->type());
1040
1041 LIR_Opr arg1 = cc->at(0),
1042 arg2 = cc->at(1),
1043 arg3 = cc->at(2);
1044
1045 crc.load_item_force(arg1);
1046 __ leal(LIR_OprFact::address(a), arg2);
1047 __ move(len, arg3);
1048
1049 __ call_runtime_leaf(StubRoutines::updateBytesCRC32C(), getThreadTemp(), result_reg, cc->args());
1050 __ move(result_reg, result);
1051 break;
1052 }
1053 default: {
1054 ShouldNotReachHere();
1055 }
1056 }
1057 }
1058
1059 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
1060 assert(UseVectorizedMismatchIntrinsic, "need AVX instruction support");
1061
1062 // Make all state_for calls early since they can emit code
1063 LIR_Opr result = rlock_result(x);
1064
1065 LIRItem a(x->argument_at(0), this); // Object
1066 LIRItem aOffset(x->argument_at(1), this); // long
1067 LIRItem b(x->argument_at(2), this); // Object
1068 LIRItem bOffset(x->argument_at(3), this); // long
1069 LIRItem length(x->argument_at(4), this); // int
1070 LIRItem log2ArrayIndexScale(x->argument_at(5), this); // int
1071
1072 a.load_item();
1073 aOffset.load_nonconstant();
1074 b.load_item();
1075 bOffset.load_nonconstant();
1076
1077 long constant_aOffset = 0;
1078 LIR_Opr result_aOffset = aOffset.result();
1079 if (result_aOffset->is_constant()) {
1080 constant_aOffset = result_aOffset->as_jlong();
1081 result_aOffset = LIR_OprFact::illegalOpr;
1082 }
1083 LIR_Opr result_a = a.result();
1084
1085 long constant_bOffset = 0;
1086 LIR_Opr result_bOffset = bOffset.result();
1087 if (result_bOffset->is_constant()) {
1088 constant_bOffset = result_bOffset->as_jlong();
1089 result_bOffset = LIR_OprFact::illegalOpr;
1090 }
1091 LIR_Opr result_b = b.result();
1092
1093 LIR_Address* addr_a = new LIR_Address(result_a,
1094 result_aOffset,
1095 constant_aOffset,
1096 T_BYTE);
1097
1098 LIR_Address* addr_b = new LIR_Address(result_b,
1099 result_bOffset,
1100 constant_bOffset,
1101 T_BYTE);
1102
1103 BasicTypeList signature(4);
1104 signature.append(T_ADDRESS);
1105 signature.append(T_ADDRESS);
1106 signature.append(T_INT);
1107 signature.append(T_INT);
1108 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1109 const LIR_Opr result_reg = result_register_for(x->type());
1110
1111 LIR_Opr ptr_addr_a = new_register(T_ADDRESS);
1112 __ leal(LIR_OprFact::address(addr_a), ptr_addr_a);
1113
1114 LIR_Opr ptr_addr_b = new_register(T_ADDRESS);
1115 __ leal(LIR_OprFact::address(addr_b), ptr_addr_b);
1116
1117 __ move(ptr_addr_a, cc->at(0));
1118 __ move(ptr_addr_b, cc->at(1));
1119 length.load_item_force(cc->at(2));
1120 log2ArrayIndexScale.load_item_force(cc->at(3));
1121
1122 __ call_runtime_leaf(StubRoutines::vectorizedMismatch(), getThreadTemp(), result_reg, cc->args());
1123 __ move(result_reg, result);
1124 }
1125
1126 void LIRGenerator::do_Convert(Convert* x) {
1127 LIRItem value(x->value(), this);
1128 value.load_item();
1129 LIR_Opr input = value.result();
1130 LIR_Opr result = rlock(x);
1131 __ convert(x->op(), input, result);
1132 assert(result->is_virtual(), "result must be virtual register");
1133 set_result(x, result);
1134 }
1135
1136
1137 void LIRGenerator::do_NewInstance(NewInstance* x) {
1138 print_if_not_loaded(x);
1139
1140 CodeEmitInfo* info = state_for(x, x->needs_state_before() ? x->state_before() : x->state());
1141 LIR_Opr reg = result_register_for(x->type());
1142 new_instance(reg, x->klass(), x->is_unresolved(),
1143 !x->is_unresolved() && x->klass()->is_inlinetype(),
1144 FrameMap::rcx_oop_opr,
1145 FrameMap::rdi_oop_opr,
1146 FrameMap::rsi_oop_opr,
1147 LIR_OprFact::illegalOpr,
1148 FrameMap::rdx_metadata_opr, info);
1149 LIR_Opr result = rlock_result(x);
1150 __ move(reg, result);
1151 }
1152
1153 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
1154 CodeEmitInfo* info = nullptr;
1155 if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
1156 info = state_for(x, x->state_before());
1157 info->set_force_reexecute();
1158 } else {
1159 info = state_for(x, x->state());
1160 }
1161
1162 LIRItem length(x->length(), this);
1163 length.load_item_force(FrameMap::rbx_opr);
1164
1165 LIR_Opr reg = result_register_for(x->type());
1166 LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1167 LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1168 LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1169 LIR_Opr tmp4 = reg;
1170 LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1171 LIR_Opr len = length.result();
1172 BasicType elem_type = x->elt_type();
1173
1174 __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
1175
1176 CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
1177 __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path, x->zero_array());
1178
1179 LIR_Opr result = rlock_result(x);
1180 __ move(reg, result);
1181 }
1182
1183
1184 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
1185 LIRItem length(x->length(), this);
1186 // in case of patching (i.e., object class is not yet loaded), we need to reexecute the instruction
1187 // and therefore provide the state before the parameters have been consumed
1188 CodeEmitInfo* patching_info = nullptr;
1189 if (!x->klass()->is_loaded() || PatchALot) {
1190 patching_info = state_for(x, x->state_before());
1191 }
1192
1193 CodeEmitInfo* info = state_for(x, x->state());
1194
1195 const LIR_Opr reg = result_register_for(x->type());
1196 LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1197 LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1198 LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1199 LIR_Opr tmp4 = reg;
1200 LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1201
1202 length.load_item_force(FrameMap::rbx_opr);
1203 LIR_Opr len = length.result();
1204
1205 ciKlass* obj = ciObjArrayKlass::make(x->klass());
1206
1207 // TODO 8265122 Implement a fast path for this
1208 bool is_flat = obj->is_loaded() && obj->is_flat_array_klass();
1209 bool is_null_free = obj->is_loaded() && obj->as_array_klass()->is_elem_null_free();
1210
1211 CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info, is_null_free);
1212 if (obj == ciEnv::unloaded_ciobjarrayklass()) {
1213 BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
1214 }
1215 klass2reg_with_patching(klass_reg, obj, patching_info);
1216 __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path, true, is_null_free || is_flat);
1217
1218 LIR_Opr result = rlock_result(x);
1219 __ move(reg, result);
1220 }
1221
1222
1223 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1224 Values* dims = x->dims();
1225 int i = dims->length();
1226 LIRItemList* items = new LIRItemList(i, i, nullptr);
1227 while (i-- > 0) {
1228 LIRItem* size = new LIRItem(dims->at(i), this);
1229 items->at_put(i, size);
1230 }
1231
1232 // Evaluate state_for early since it may emit code.
1233 CodeEmitInfo* patching_info = nullptr;
1234 if (!x->klass()->is_loaded() || PatchALot) {
1235 patching_info = state_for(x, x->state_before());
1236
1237 // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1238 // clone all handlers (NOTE: Usually this is handled transparently
1239 // by the CodeEmitInfo cloning logic in CodeStub constructors but
1240 // is done explicitly here because a stub isn't being used).
1241 x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1242 }
1243 CodeEmitInfo* info = state_for(x, x->state());
1244
1245 i = dims->length();
1246 while (i-- > 0) {
1247 LIRItem* size = items->at(i);
1248 size->load_nonconstant();
1249
1250 store_stack_parameter(size->result(), in_ByteSize(i*4));
1251 }
1252
1253 LIR_Opr klass_reg = FrameMap::rax_metadata_opr;
1254 klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1255
1256 LIR_Opr rank = FrameMap::rbx_opr;
1257 __ move(LIR_OprFact::intConst(x->rank()), rank);
1258 LIR_Opr varargs = FrameMap::rcx_opr;
1259 __ move(FrameMap::rsp_opr, varargs);
1260 LIR_OprList* args = new LIR_OprList(3);
1261 args->append(klass_reg);
1262 args->append(rank);
1263 args->append(varargs);
1264 LIR_Opr reg = result_register_for(x->type());
1265 __ call_runtime(Runtime1::entry_for(StubId::c1_new_multi_array_id),
1266 LIR_OprFact::illegalOpr,
1267 reg, args, info);
1268
1269 LIR_Opr result = rlock_result(x);
1270 __ move(reg, result);
1271 }
1272
1273
1274 void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1275 // nothing to do for now
1276 }
1277
1278
1279 void LIRGenerator::do_CheckCast(CheckCast* x) {
1280 LIRItem obj(x->obj(), this);
1281
1282 CodeEmitInfo* patching_info = nullptr;
1283 if (!x->klass()->is_loaded() || (PatchALot && !x->is_incompatible_class_change_check() && !x->is_invokespecial_receiver_check())) {
1284 // must do this before locking the destination register as an oop register,
1285 // and before the obj is loaded (the latter is for deoptimization)
1286 patching_info = state_for(x, x->state_before());
1287 }
1288 obj.load_item();
1289
1290 // info for exceptions
1291 CodeEmitInfo* info_for_exception =
1292 (x->needs_exception_state() ? state_for(x) :
1293 state_for(x, x->state_before(), true /*ignore_xhandler*/));
1294
1295 CodeStub* stub;
1296 if (x->is_incompatible_class_change_check()) {
1297 assert(patching_info == nullptr, "can't patch this");
1298 stub = new SimpleExceptionStub(StubId::c1_throw_incompatible_class_change_error_id, LIR_OprFact::illegalOpr, info_for_exception);
1299 } else if (x->is_invokespecial_receiver_check()) {
1300 assert(patching_info == nullptr, "can't patch this");
1301 stub = new DeoptimizeStub(info_for_exception, Deoptimization::Reason_class_check, Deoptimization::Action_none);
1302 } else {
1303 stub = new SimpleExceptionStub(StubId::c1_throw_class_cast_exception_id, obj.result(), info_for_exception);
1304 }
1305 LIR_Opr reg = rlock_result(x);
1306 LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1307 tmp3 = new_register(objectType);
1308 __ checkcast(reg, obj.result(), x->klass(),
1309 new_register(objectType), new_register(objectType), tmp3,
1310 x->direct_compare(), info_for_exception, patching_info, stub,
1311 x->profiled_method(), x->profiled_bci(), x->is_null_free());
1312 }
1313
1314
1315 void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1316 LIRItem obj(x->obj(), this);
1317
1318 // result and test object may not be in same register
1319 LIR_Opr reg = rlock_result(x);
1320 CodeEmitInfo* patching_info = nullptr;
1321 if ((!x->klass()->is_loaded() || PatchALot)) {
1322 // must do this before locking the destination register as an oop register
1323 patching_info = state_for(x, x->state_before());
1324 }
1325 obj.load_item();
1326 LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1327 tmp3 = new_register(objectType);
1328 __ instanceof(reg, obj.result(), x->klass(),
1329 new_register(objectType), new_register(objectType), tmp3,
1330 x->direct_compare(), patching_info, x->profiled_method(), x->profiled_bci());
1331 }
1332
1333 // Intrinsic for Class::isInstance
1334 address LIRGenerator::isInstance_entry() {
1335 return Runtime1::entry_for(StubId::c1_is_instance_of_id);
1336 }
1337
1338
1339 void LIRGenerator::do_If(If* x) {
1340 assert(x->number_of_sux() == 2, "inconsistency");
1341 ValueTag tag = x->x()->type()->tag();
1342 bool is_safepoint = x->is_safepoint();
1343
1344 If::Condition cond = x->cond();
1345
1346 LIRItem xitem(x->x(), this);
1347 LIRItem yitem(x->y(), this);
1348 LIRItem* xin = &xitem;
1349 LIRItem* yin = &yitem;
1350
1351 if (tag == longTag) {
1352 // for longs, only conditions "eql", "neq", "lss", "geq" are valid;
1353 // mirror for other conditions
1354 if (cond == If::gtr || cond == If::leq) {
1355 cond = Instruction::mirror(cond);
1356 xin = &yitem;
1357 yin = &xitem;
1358 }
1359 xin->set_destroys_register();
1360 }
1361 xin->load_item();
1362 if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 && (cond == If::eql || cond == If::neq)) {
1363 // inline long zero
1364 yin->dont_load_item();
1365 } else if (tag == longTag || tag == floatTag || tag == doubleTag || x->substitutability_check()) {
1366 // longs cannot handle constants at right side
1367 yin->load_item();
1368 } else {
1369 yin->dont_load_item();
1370 }
1371
1372 LIR_Opr left = xin->result();
1373 LIR_Opr right = yin->result();
1374
1375 set_no_result(x);
1376
1377 // add safepoint before generating condition code so it can be recomputed
1378 if (x->is_safepoint()) {
1379 // increment backedge counter if needed
1380 increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1381 x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1382 __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
1383 }
1384
1385 if (x->substitutability_check()) {
1386 substitutability_check(x, *xin, *yin);
1387 } else {
1388 __ cmp(lir_cond(cond), left, right);
1389 }
1390 // Generate branch profiling. Profiling code doesn't kill flags.
1391 profile_branch(x, cond);
1392 move_to_phi(x->state());
1393 if (x->x()->type()->is_float_kind()) {
1394 __ branch(lir_cond(cond), x->tsux(), x->usux());
1395 } else {
1396 __ branch(lir_cond(cond), x->tsux());
1397 }
1398 assert(x->default_sux() == x->fsux(), "wrong destination above");
1399 __ jump(x->default_sux());
1400 }
1401
1402
1403 LIR_Opr LIRGenerator::getThreadPointer() {
1404 return FrameMap::as_pointer_opr(r15_thread);
1405 }
1406
1407 void LIRGenerator::trace_block_entry(BlockBegin* block) {
1408 store_stack_parameter(LIR_OprFact::intConst(block->block_id()), in_ByteSize(0));
1409 LIR_OprList* args = new LIR_OprList();
1410 address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1411 __ call_runtime_leaf(func, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, args);
1412 }
1413
1414
1415 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1416 CodeEmitInfo* info) {
1417 if (address->type() == T_LONG) {
1418 address = new LIR_Address(address->base(),
1419 address->index(), address->scale(),
1420 address->disp(), T_DOUBLE);
1421 // Transfer the value atomically by using FP moves. This means
1422 // the value has to be moved between CPU and FPU registers. It
1423 // always has to be moved through spill slot since there's no
1424 // quick way to pack the value into an SSE register.
1425 LIR_Opr temp_double = new_register(T_DOUBLE);
1426 LIR_Opr spill = new_register(T_LONG);
1427 set_vreg_flag(spill, must_start_in_memory);
1428 __ move(value, spill);
1429 __ volatile_move(spill, temp_double, T_LONG);
1430 __ volatile_move(temp_double, LIR_OprFact::address(address), T_LONG, info);
1431 } else {
1432 __ store(value, address, info);
1433 }
1434 }
1435
1436 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1437 CodeEmitInfo* info) {
1438 if (address->type() == T_LONG) {
1439 address = new LIR_Address(address->base(),
1440 address->index(), address->scale(),
1441 address->disp(), T_DOUBLE);
1442 // Transfer the value atomically by using FP moves. This means
1443 // the value has to be moved between CPU and FPU registers. In
1444 // SSE0 and SSE1 mode it has to be moved through spill slot but in
1445 // SSE2+ mode it can be moved directly.
1446 LIR_Opr temp_double = new_register(T_DOUBLE);
1447 __ volatile_move(LIR_OprFact::address(address), temp_double, T_LONG, info);
1448 __ volatile_move(temp_double, result, T_LONG);
1449 } else {
1450 __ load(address, result, info);
1451 }
1452 }