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
286 CodeEmitInfo* info_for_exception = nullptr;
287 if (x->needs_null_check()) {
288 info_for_exception = state_for(x);
289 }
290
291 CodeStub* throw_ie_stub = x->maybe_inlinetype() ?
292 new SimpleExceptionStub(StubId::c1_throw_identity_exception_id,
293 obj.result(), state_for(x))
294 : nullptr;
295
296 // this CodeEmitInfo must not have the xhandlers because here the
297 // object is already locked (xhandlers expect object to be unlocked)
298 CodeEmitInfo* info = state_for(x, x->state(), true);
299 LIR_Opr tmp = new_register(T_ADDRESS);
300 monitor_enter(obj.result(), lock, syncTempOpr(), tmp,
301 x->monitor_no(), info_for_exception, info, throw_ie_stub);
302 }
303
304
305 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
306 assert(x->is_pinned(),"");
307
308 LIRItem obj(x->obj(), this);
309 obj.dont_load_item();
310
311 LIR_Opr lock = new_register(T_INT);
312 LIR_Opr obj_temp = new_register(T_INT);
313 set_no_result(x);
314 monitor_exit(obj_temp, lock, syncTempOpr(), LIR_OprFact::illegalOpr, x->monitor_no());
315 }
316
317 // _ineg, _lneg, _fneg, _dneg
318 void LIRGenerator::do_NegateOp(NegateOp* x) {
319 LIRItem value(x->x(), this);
320 value.set_destroys_register();
321 value.load_item();
322 LIR_Opr reg = rlock(x);
323
324 __ negate(value.result(), reg);
325
326 set_result(x, reg);
327 }
328
329 // for _fadd, _fmul, _fsub, _fdiv, _frem
330 // _dadd, _dmul, _dsub, _ddiv, _drem
331 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
332 LIRItem left(x->x(), this);
333 LIRItem right(x->y(), this);
334 LIRItem* left_arg = &left;
335 LIRItem* right_arg = &right;
336 assert(!left.is_stack() || !right.is_stack(), "can't both be memory operands");
337 bool must_load_both = (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem);
338 if (left.is_register() || x->x()->type()->is_constant() || must_load_both) {
339 left.load_item();
340 } else {
341 left.dont_load_item();
342 }
343
344 if (must_load_both) {
345 // frem and drem destroy also right operand, so move it to a new register
346 right.set_destroys_register();
347 right.load_item();
348 } else if (right.is_register()) {
349 right.load_item();
350 } else {
351 right.dont_load_item();
352 }
353 LIR_Opr reg = rlock(x);
354 LIR_Opr tmp = LIR_OprFact::illegalOpr;
355 if (x->op() == Bytecodes::_dmul || x->op() == Bytecodes::_ddiv) {
356 tmp = new_register(T_DOUBLE);
357 }
358
359 if (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem) {
360 // frem and drem are implemented as a direct call into the runtime.
361 LIRItem left(x->x(), this);
362 LIRItem right(x->y(), this);
363
364 BasicType bt = as_BasicType(x->type());
365 BasicTypeList signature(2);
366 signature.append(bt);
367 signature.append(bt);
368 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
369
370 const LIR_Opr result_reg = result_register_for(x->type());
371 left.load_item_force(cc->at(0));
372 right.load_item_force(cc->at(1));
373
374 address entry = nullptr;
375 switch (x->op()) {
376 case Bytecodes::_frem:
377 entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
378 break;
379 case Bytecodes::_drem:
380 entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
381 break;
382 default:
383 ShouldNotReachHere();
384 }
385
386 LIR_Opr result = rlock_result(x);
387 __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
388 __ move(result_reg, result);
389 } else {
390 arithmetic_op_fpu(x->op(), reg, left.result(), right.result(), tmp);
391 set_result(x, reg);
392 }
393 }
394
395
396 // for _ladd, _lmul, _lsub, _ldiv, _lrem
397 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
398 if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem ) {
399 // long division is implemented as a direct call into the runtime
400 LIRItem left(x->x(), this);
401 LIRItem right(x->y(), this);
402
403 // the check for division by zero destroys the right operand
404 right.set_destroys_register();
405
406 BasicTypeList signature(2);
407 signature.append(T_LONG);
408 signature.append(T_LONG);
409 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
410
411 // check for division by zero (destroys registers of right operand!)
412 CodeEmitInfo* info = state_for(x);
413
414 const LIR_Opr result_reg = result_register_for(x->type());
415 left.load_item_force(cc->at(1));
416 right.load_item();
417
418 __ move(right.result(), cc->at(0));
419
420 __ cmp(lir_cond_equal, right.result(), LIR_OprFact::longConst(0));
421 __ branch(lir_cond_equal, new DivByZeroStub(info));
422
423 address entry = nullptr;
424 switch (x->op()) {
425 case Bytecodes::_lrem:
426 entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
427 break; // check if dividend is 0 is done elsewhere
428 case Bytecodes::_ldiv:
429 entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
430 break; // check if dividend is 0 is done elsewhere
431 default:
432 ShouldNotReachHere();
433 }
434
435 LIR_Opr result = rlock_result(x);
436 __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
437 __ move(result_reg, result);
438 } else if (x->op() == Bytecodes::_lmul) {
439 // missing test if instr is commutative and if we should swap
440 LIRItem left(x->x(), this);
441 LIRItem right(x->y(), this);
442
443 // right register is destroyed by the long mul, so it must be
444 // copied to a new register.
445 right.set_destroys_register();
446
447 left.load_item();
448 right.load_item();
449
450 LIR_Opr reg = FrameMap::long0_opr;
451 arithmetic_op_long(x->op(), reg, left.result(), right.result(), nullptr);
452 LIR_Opr result = rlock_result(x);
453 __ move(reg, result);
454 } else {
455 // missing test if instr is commutative and if we should swap
456 LIRItem left(x->x(), this);
457 LIRItem right(x->y(), this);
458
459 left.load_item();
460 // don't load constants to save register
461 right.load_nonconstant();
462 rlock_result(x);
463 arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), nullptr);
464 }
465 }
466
467
468
469 // for: _iadd, _imul, _isub, _idiv, _irem
470 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
471 if (x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem) {
472 // The requirements for division and modulo
473 // input : rax,: dividend min_int
474 // reg: divisor (may not be rax,/rdx) -1
475 //
476 // output: rax,: quotient (= rax, idiv reg) min_int
477 // rdx: remainder (= rax, irem reg) 0
478
479 // rax, and rdx will be destroyed
480
481 // Note: does this invalidate the spec ???
482 LIRItem right(x->y(), this);
483 LIRItem left(x->x() , this); // visit left second, so that the is_register test is valid
484
485 // call state_for before load_item_force because state_for may
486 // force the evaluation of other instructions that are needed for
487 // correct debug info. Otherwise the live range of the fix
488 // register might be too long.
489 CodeEmitInfo* info = state_for(x);
490
491 left.load_item_force(divInOpr());
492
493 right.load_item();
494
495 LIR_Opr result = rlock_result(x);
496 LIR_Opr result_reg;
497 if (x->op() == Bytecodes::_idiv) {
498 result_reg = divOutOpr();
499 } else {
500 result_reg = remOutOpr();
501 }
502
503 if (!ImplicitDiv0Checks) {
504 __ cmp(lir_cond_equal, right.result(), LIR_OprFact::intConst(0));
505 __ branch(lir_cond_equal, new DivByZeroStub(info));
506 // Idiv/irem cannot trap (passing info would generate an assertion).
507 info = nullptr;
508 }
509 LIR_Opr tmp = FrameMap::rdx_opr; // idiv and irem use rdx in their implementation
510 if (x->op() == Bytecodes::_irem) {
511 __ irem(left.result(), right.result(), result_reg, tmp, info);
512 } else if (x->op() == Bytecodes::_idiv) {
513 __ idiv(left.result(), right.result(), result_reg, tmp, info);
514 } else {
515 ShouldNotReachHere();
516 }
517
518 __ move(result_reg, result);
519 } else {
520 // missing test if instr is commutative and if we should swap
521 LIRItem left(x->x(), this);
522 LIRItem right(x->y(), this);
523 LIRItem* left_arg = &left;
524 LIRItem* right_arg = &right;
525 if (x->is_commutative() && left.is_stack() && right.is_register()) {
526 // swap them if left is real stack (or cached) and right is real register(not cached)
527 left_arg = &right;
528 right_arg = &left;
529 }
530
531 left_arg->load_item();
532
533 // do not need to load right, as we can handle stack and constants
534 if (x->op() == Bytecodes::_imul ) {
535 // check if we can use shift instead
536 bool use_constant = false;
537 bool use_tmp = false;
538 if (right_arg->is_constant()) {
539 jint iconst = right_arg->get_jint_constant();
540 if (iconst > 0 && iconst < max_jint) {
541 if (is_power_of_2(iconst)) {
542 use_constant = true;
543 } else if (is_power_of_2(iconst - 1) || is_power_of_2(iconst + 1)) {
544 use_constant = true;
545 use_tmp = true;
546 }
547 }
548 }
549 if (use_constant) {
550 right_arg->dont_load_item();
551 } else {
552 right_arg->load_item();
553 }
554 LIR_Opr tmp = LIR_OprFact::illegalOpr;
555 if (use_tmp) {
556 tmp = new_register(T_INT);
557 }
558 rlock_result(x);
559
560 arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
561 } else {
562 right_arg->dont_load_item();
563 rlock_result(x);
564 LIR_Opr tmp = LIR_OprFact::illegalOpr;
565 arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
566 }
567 }
568 }
569
570
571 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
572 // when an operand with use count 1 is the left operand, then it is
573 // likely that no move for 2-operand-LIR-form is necessary
574 if (x->is_commutative() && x->y()->as_Constant() == nullptr && x->x()->use_count() > x->y()->use_count()) {
575 x->swap_operands();
576 }
577
578 ValueTag tag = x->type()->tag();
579 assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
580 switch (tag) {
581 case floatTag:
582 case doubleTag: do_ArithmeticOp_FPU(x); return;
583 case longTag: do_ArithmeticOp_Long(x); return;
584 case intTag: do_ArithmeticOp_Int(x); return;
585 default: ShouldNotReachHere(); return;
586 }
587 }
588
589
590 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
591 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
592 // count must always be in rcx
593 LIRItem value(x->x(), this);
594 LIRItem count(x->y(), this);
595
596 ValueTag elemType = x->type()->tag();
597 bool must_load_count = !count.is_constant() || elemType == longTag;
598 if (must_load_count) {
599 // count for long must be in register
600 count.load_item_force(shiftCountOpr());
601 } else {
602 count.dont_load_item();
603 }
604 value.load_item();
605 LIR_Opr reg = rlock_result(x);
606
607 shift_op(x->op(), reg, value.result(), count.result(), LIR_OprFact::illegalOpr);
608 }
609
610
611 // _iand, _land, _ior, _lor, _ixor, _lxor
612 void LIRGenerator::do_LogicOp(LogicOp* x) {
613 // when an operand with use count 1 is the left operand, then it is
614 // likely that no move for 2-operand-LIR-form is necessary
615 if (x->is_commutative() && x->y()->as_Constant() == nullptr && x->x()->use_count() > x->y()->use_count()) {
616 x->swap_operands();
617 }
618
619 LIRItem left(x->x(), this);
620 LIRItem right(x->y(), this);
621
622 left.load_item();
623 right.load_nonconstant();
624 LIR_Opr reg = rlock_result(x);
625
626 logic_op(x->op(), reg, left.result(), right.result());
627 }
628
629
630
631 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
632 void LIRGenerator::do_CompareOp(CompareOp* x) {
633 LIRItem left(x->x(), this);
634 LIRItem right(x->y(), this);
635 ValueTag tag = x->x()->type()->tag();
636 if (tag == longTag) {
637 left.set_destroys_register();
638 }
639 left.load_item();
640 right.load_item();
641 LIR_Opr reg = rlock_result(x);
642
643 if (x->x()->type()->is_float_kind()) {
644 Bytecodes::Code code = x->op();
645 __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
646 } else if (x->x()->type()->tag() == longTag) {
647 __ lcmp2int(left.result(), right.result(), reg);
648 } else {
649 Unimplemented();
650 }
651 }
652
653 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
654 LIR_Opr ill = LIR_OprFact::illegalOpr; // for convenience
655 if (is_reference_type(type)) {
656 cmp_value.load_item_force(FrameMap::rax_oop_opr);
657 new_value.load_item();
658 __ cas_obj(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
659 } else if (type == T_INT) {
660 cmp_value.load_item_force(FrameMap::rax_opr);
661 new_value.load_item();
662 __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
663 } else if (type == T_LONG) {
664 cmp_value.load_item_force(FrameMap::long0_opr);
665 new_value.load_item_force(FrameMap::long1_opr);
666 __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
667 } else {
668 Unimplemented();
669 }
670 LIR_Opr result = new_register(T_INT);
671 __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0),
672 result, T_INT);
673 return result;
674 }
675
676 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
677 bool is_oop = is_reference_type(type);
678 LIR_Opr result = new_register(type);
679 value.load_item();
680 // Because we want a 2-arg form of xchg and xadd
681 __ move(value.result(), result);
682 assert(type == T_INT || is_oop || type == T_LONG, "unexpected type");
683 __ xchg(addr, result, result, LIR_OprFact::illegalOpr);
684 return result;
685 }
686
687 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
688 LIR_Opr result = new_register(type);
689 value.load_item();
690 // Because we want a 2-arg form of xchg and xadd
691 __ move(value.result(), result);
692 assert(type == T_INT || type == T_LONG, "unexpected type");
693 __ xadd(addr, result, result, LIR_OprFact::illegalOpr);
694 return result;
695 }
696
697 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
698 assert(x->number_of_arguments() == 3, "wrong type");
699 assert(UseFMA, "Needs FMA instructions support.");
700 LIRItem value(x->argument_at(0), this);
701 LIRItem value1(x->argument_at(1), this);
702 LIRItem value2(x->argument_at(2), this);
703
704 value2.set_destroys_register();
705
706 value.load_item();
707 value1.load_item();
708 value2.load_item();
709
710 LIR_Opr calc_input = value.result();
711 LIR_Opr calc_input1 = value1.result();
712 LIR_Opr calc_input2 = value2.result();
713 LIR_Opr calc_result = rlock_result(x);
714
715 switch (x->id()) {
716 case vmIntrinsics::_fmaD: __ fmad(calc_input, calc_input1, calc_input2, calc_result); break;
717 case vmIntrinsics::_fmaF: __ fmaf(calc_input, calc_input1, calc_input2, calc_result); break;
718 default: ShouldNotReachHere();
719 }
720
721 }
722
723
724 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
725 assert(x->number_of_arguments() == 1 || (x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow), "wrong type");
726
727 if (x->id() == vmIntrinsics::_dexp || x->id() == vmIntrinsics::_dlog ||
728 x->id() == vmIntrinsics::_dpow || x->id() == vmIntrinsics::_dcos ||
729 x->id() == vmIntrinsics::_dsin || x->id() == vmIntrinsics::_dtan ||
730 x->id() == vmIntrinsics::_dlog10 || x->id() == vmIntrinsics::_dsinh ||
731 x->id() == vmIntrinsics::_dtanh || x->id() == vmIntrinsics::_dcbrt
732 ) {
733 do_LibmIntrinsic(x);
734 return;
735 }
736
737 LIRItem value(x->argument_at(0), this);
738
739 value.load_item();
740
741 LIR_Opr calc_input = value.result();
742 LIR_Opr calc_result = rlock_result(x);
743
744 LIR_Opr tmp = LIR_OprFact::illegalOpr;
745 if (x->id() == vmIntrinsics::_floatToFloat16) {
746 tmp = new_register(T_FLOAT);
747 }
748
749 switch(x->id()) {
750 case vmIntrinsics::_dabs:
751 __ abs(calc_input, calc_result, tmp);
752 break;
753 case vmIntrinsics::_dsqrt:
754 case vmIntrinsics::_dsqrt_strict:
755 __ sqrt(calc_input, calc_result, LIR_OprFact::illegalOpr);
756 break;
757 case vmIntrinsics::_floatToFloat16:
758 __ f2hf(calc_input, calc_result, tmp);
759 break;
760 case vmIntrinsics::_float16ToFloat:
761 __ hf2f(calc_input, calc_result, LIR_OprFact::illegalOpr);
762 break;
763 default:
764 ShouldNotReachHere();
765 }
766 }
767
768 void LIRGenerator::do_LibmIntrinsic(Intrinsic* x) {
769 LIRItem value(x->argument_at(0), this);
770 value.set_destroys_register();
771
772 LIR_Opr calc_result = rlock_result(x);
773 LIR_Opr result_reg = result_register_for(x->type());
774
775 CallingConvention* cc = nullptr;
776
777 if (x->id() == vmIntrinsics::_dpow) {
778 LIRItem value1(x->argument_at(1), this);
779
780 value1.set_destroys_register();
781
782 BasicTypeList signature(2);
783 signature.append(T_DOUBLE);
784 signature.append(T_DOUBLE);
785 cc = frame_map()->c_calling_convention(&signature);
786 value.load_item_force(cc->at(0));
787 value1.load_item_force(cc->at(1));
788 } else {
789 BasicTypeList signature(1);
790 signature.append(T_DOUBLE);
791 cc = frame_map()->c_calling_convention(&signature);
792 value.load_item_force(cc->at(0));
793 }
794
795 switch (x->id()) {
796 case vmIntrinsics::_dexp:
797 if (StubRoutines::dexp() != nullptr) {
798 __ call_runtime_leaf(StubRoutines::dexp(), getThreadTemp(), result_reg, cc->args());
799 } else {
800 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dexp), getThreadTemp(), result_reg, cc->args());
801 }
802 break;
803 case vmIntrinsics::_dlog:
804 if (StubRoutines::dlog() != nullptr) {
805 __ call_runtime_leaf(StubRoutines::dlog(), getThreadTemp(), result_reg, cc->args());
806 } else {
807 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog), getThreadTemp(), result_reg, cc->args());
808 }
809 break;
810 case vmIntrinsics::_dlog10:
811 if (StubRoutines::dlog10() != nullptr) {
812 __ call_runtime_leaf(StubRoutines::dlog10(), getThreadTemp(), result_reg, cc->args());
813 } else {
814 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog10), getThreadTemp(), result_reg, cc->args());
815 }
816 break;
817 case vmIntrinsics::_dpow:
818 if (StubRoutines::dpow() != nullptr) {
819 __ call_runtime_leaf(StubRoutines::dpow(), getThreadTemp(), result_reg, cc->args());
820 } else {
821 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), getThreadTemp(), result_reg, cc->args());
822 }
823 break;
824 case vmIntrinsics::_dsin:
825 if (StubRoutines::dsin() != nullptr) {
826 __ call_runtime_leaf(StubRoutines::dsin(), getThreadTemp(), result_reg, cc->args());
827 } else {
828 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dsin), getThreadTemp(), result_reg, cc->args());
829 }
830 break;
831 case vmIntrinsics::_dcos:
832 if (StubRoutines::dcos() != nullptr) {
833 __ call_runtime_leaf(StubRoutines::dcos(), getThreadTemp(), result_reg, cc->args());
834 } else {
835 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dcos), getThreadTemp(), result_reg, cc->args());
836 }
837 break;
838 case vmIntrinsics::_dtan:
839 if (StubRoutines::dtan() != nullptr) {
840 __ call_runtime_leaf(StubRoutines::dtan(), getThreadTemp(), result_reg, cc->args());
841 } else {
842 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtan), getThreadTemp(), result_reg, cc->args());
843 }
844 break;
845 case vmIntrinsics::_dsinh:
846 assert(StubRoutines::dsinh() != nullptr, "sinh intrinsic not found");
847 if (StubRoutines::dsinh() != nullptr) {
848 __ call_runtime_leaf(StubRoutines::dsinh(), getThreadTemp(), result_reg, cc->args());
849 }
850 break;
851 case vmIntrinsics::_dtanh:
852 assert(StubRoutines::dtanh() != nullptr, "tanh intrinsic not found");
853 if (StubRoutines::dtanh() != nullptr) {
854 __ call_runtime_leaf(StubRoutines::dtanh(), getThreadTemp(), result_reg, cc->args());
855 }
856 break;
857 case vmIntrinsics::_dcbrt:
858 assert(StubRoutines::dcbrt() != nullptr, "cbrt intrinsic not found");
859 if (StubRoutines::dcbrt() != nullptr) {
860 __ call_runtime_leaf(StubRoutines::dcbrt(), getThreadTemp(), result_reg, cc->args());
861 }
862 break;
863 default: ShouldNotReachHere();
864 }
865
866 __ move(result_reg, calc_result);
867 }
868
869 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
870 assert(x->number_of_arguments() == 5, "wrong type");
871
872 // Make all state_for calls early since they can emit code
873 CodeEmitInfo* info = nullptr;
874 if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
875 info = state_for(x, x->state_before());
876 info->set_force_reexecute();
877 } else {
878 info = state_for(x, x->state());
879 }
880
881 LIRItem src(x->argument_at(0), this);
882 LIRItem src_pos(x->argument_at(1), this);
883 LIRItem dst(x->argument_at(2), this);
884 LIRItem dst_pos(x->argument_at(3), this);
885 LIRItem length(x->argument_at(4), this);
886
887 // operands for arraycopy must use fixed registers, otherwise
888 // LinearScan will fail allocation (because arraycopy always needs a
889 // call)
890
891 int flags;
892 ciArrayKlass* expected_type;
893 arraycopy_helper(x, &flags, &expected_type);
894 if (x->check_flag(Instruction::OmitChecksFlag)) {
895 flags = 0;
896 }
897
898 // The java calling convention will give us enough registers
899 // so that on the stub side the args will be perfect already.
900 // On the other slow/special case side we call C and the arg
901 // positions are not similar enough to pick one as the best.
902 // Also because the java calling convention is a "shifted" version
903 // of the C convention we can process the java args trivially into C
904 // args without worry of overwriting during the xfer
905
906 src.load_item_force (FrameMap::as_oop_opr(j_rarg0));
907 src_pos.load_item_force (FrameMap::as_opr(j_rarg1));
908 dst.load_item_force (FrameMap::as_oop_opr(j_rarg2));
909 dst_pos.load_item_force (FrameMap::as_opr(j_rarg3));
910 length.load_item_force (FrameMap::as_opr(j_rarg4));
911
912 LIR_Opr tmp = FrameMap::as_opr(j_rarg5);
913
914 set_no_result(x);
915
916 __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(), tmp, expected_type, flags, info); // does add_safepoint
917 }
918
919 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
920 assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions support");
921 // Make all state_for calls early since they can emit code
922 LIR_Opr result = rlock_result(x);
923 int flags = 0;
924 switch (x->id()) {
925 case vmIntrinsics::_updateCRC32: {
926 LIRItem crc(x->argument_at(0), this);
927 LIRItem val(x->argument_at(1), this);
928 // val is destroyed by update_crc32
929 val.set_destroys_register();
930 crc.load_item();
931 val.load_item();
932 __ update_crc32(crc.result(), val.result(), result);
933 break;
934 }
935 case vmIntrinsics::_updateBytesCRC32:
936 case vmIntrinsics::_updateByteBufferCRC32: {
937 bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32);
938
939 LIRItem crc(x->argument_at(0), this);
940 LIRItem buf(x->argument_at(1), this);
941 LIRItem off(x->argument_at(2), this);
942 LIRItem len(x->argument_at(3), this);
943 buf.load_item();
944 off.load_nonconstant();
945
946 LIR_Opr index = off.result();
947 int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
948 if(off.result()->is_constant()) {
949 index = LIR_OprFact::illegalOpr;
950 offset += off.result()->as_jint();
951 }
952 LIR_Opr base_op = buf.result();
953
954 if (index->is_valid()) {
955 LIR_Opr tmp = new_register(T_LONG);
956 __ convert(Bytecodes::_i2l, index, tmp);
957 index = tmp;
958 }
959
960 LIR_Address* a = new LIR_Address(base_op,
961 index,
962 offset,
963 T_BYTE);
964 BasicTypeList signature(3);
965 signature.append(T_INT);
966 signature.append(T_ADDRESS);
967 signature.append(T_INT);
968 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
969 const LIR_Opr result_reg = result_register_for(x->type());
970
971 LIR_Opr addr = new_register(T_ADDRESS);
972 __ leal(LIR_OprFact::address(a), addr);
973
974 crc.load_item_force(cc->at(0));
975 __ move(addr, cc->at(1));
976 len.load_item_force(cc->at(2));
977
978 __ call_runtime_leaf(StubRoutines::updateBytesCRC32(), getThreadTemp(), result_reg, cc->args());
979 __ move(result_reg, result);
980
981 break;
982 }
983 default: {
984 ShouldNotReachHere();
985 }
986 }
987 }
988
989 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
990 assert(UseCRC32CIntrinsics, "need AVX and CLMUL instructions support");
991 LIR_Opr result = rlock_result(x);
992
993 switch (x->id()) {
994 case vmIntrinsics::_updateBytesCRC32C:
995 case vmIntrinsics::_updateDirectByteBufferCRC32C: {
996 bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32C);
997
998 LIRItem crc(x->argument_at(0), this);
999 LIRItem buf(x->argument_at(1), this);
1000 LIRItem off(x->argument_at(2), this);
1001 LIRItem end(x->argument_at(3), this);
1002 buf.load_item();
1003 off.load_nonconstant();
1004 end.load_nonconstant();
1005
1006 // len = end - off
1007 LIR_Opr len = end.result();
1008 LIR_Opr tmpA = new_register(T_INT);
1009 LIR_Opr tmpB = new_register(T_INT);
1010 __ move(end.result(), tmpA);
1011 __ move(off.result(), tmpB);
1012 __ sub(tmpA, tmpB, tmpA);
1013 len = tmpA;
1014
1015 LIR_Opr index = off.result();
1016 int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
1017 if (off.result()->is_constant()) {
1018 index = LIR_OprFact::illegalOpr;
1019 offset += off.result()->as_jint();
1020 }
1021 LIR_Opr base_op = buf.result();
1022 LIR_Address* a = nullptr;
1023
1024 if (index->is_valid()) {
1025 LIR_Opr tmp = new_register(T_LONG);
1026 __ convert(Bytecodes::_i2l, index, tmp);
1027 index = tmp;
1028 a = new LIR_Address(base_op, index, offset, T_BYTE);
1029 } else {
1030 a = new LIR_Address(base_op, offset, T_BYTE);
1031 }
1032
1033 BasicTypeList signature(3);
1034 signature.append(T_INT);
1035 signature.append(T_ADDRESS);
1036 signature.append(T_INT);
1037 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1038 const LIR_Opr result_reg = result_register_for(x->type());
1039
1040 LIR_Opr arg1 = cc->at(0),
1041 arg2 = cc->at(1),
1042 arg3 = cc->at(2);
1043
1044 crc.load_item_force(arg1);
1045 __ leal(LIR_OprFact::address(a), arg2);
1046 __ move(len, arg3);
1047
1048 __ call_runtime_leaf(StubRoutines::updateBytesCRC32C(), getThreadTemp(), result_reg, cc->args());
1049 __ move(result_reg, result);
1050 break;
1051 }
1052 default: {
1053 ShouldNotReachHere();
1054 }
1055 }
1056 }
1057
1058 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
1059 assert(UseVectorizedMismatchIntrinsic, "need AVX instruction support");
1060
1061 // Make all state_for calls early since they can emit code
1062 LIR_Opr result = rlock_result(x);
1063
1064 LIRItem a(x->argument_at(0), this); // Object
1065 LIRItem aOffset(x->argument_at(1), this); // long
1066 LIRItem b(x->argument_at(2), this); // Object
1067 LIRItem bOffset(x->argument_at(3), this); // long
1068 LIRItem length(x->argument_at(4), this); // int
1069 LIRItem log2ArrayIndexScale(x->argument_at(5), this); // int
1070
1071 a.load_item();
1072 aOffset.load_nonconstant();
1073 b.load_item();
1074 bOffset.load_nonconstant();
1075
1076 long constant_aOffset = 0;
1077 LIR_Opr result_aOffset = aOffset.result();
1078 if (result_aOffset->is_constant()) {
1079 constant_aOffset = result_aOffset->as_jlong();
1080 result_aOffset = LIR_OprFact::illegalOpr;
1081 }
1082 LIR_Opr result_a = a.result();
1083
1084 long constant_bOffset = 0;
1085 LIR_Opr result_bOffset = bOffset.result();
1086 if (result_bOffset->is_constant()) {
1087 constant_bOffset = result_bOffset->as_jlong();
1088 result_bOffset = LIR_OprFact::illegalOpr;
1089 }
1090 LIR_Opr result_b = b.result();
1091
1092 LIR_Address* addr_a = new LIR_Address(result_a,
1093 result_aOffset,
1094 constant_aOffset,
1095 T_BYTE);
1096
1097 LIR_Address* addr_b = new LIR_Address(result_b,
1098 result_bOffset,
1099 constant_bOffset,
1100 T_BYTE);
1101
1102 BasicTypeList signature(4);
1103 signature.append(T_ADDRESS);
1104 signature.append(T_ADDRESS);
1105 signature.append(T_INT);
1106 signature.append(T_INT);
1107 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1108 const LIR_Opr result_reg = result_register_for(x->type());
1109
1110 LIR_Opr ptr_addr_a = new_register(T_ADDRESS);
1111 __ leal(LIR_OprFact::address(addr_a), ptr_addr_a);
1112
1113 LIR_Opr ptr_addr_b = new_register(T_ADDRESS);
1114 __ leal(LIR_OprFact::address(addr_b), ptr_addr_b);
1115
1116 __ move(ptr_addr_a, cc->at(0));
1117 __ move(ptr_addr_b, cc->at(1));
1118 length.load_item_force(cc->at(2));
1119 log2ArrayIndexScale.load_item_force(cc->at(3));
1120
1121 __ call_runtime_leaf(StubRoutines::vectorizedMismatch(), getThreadTemp(), result_reg, cc->args());
1122 __ move(result_reg, result);
1123 }
1124
1125 void LIRGenerator::do_Convert(Convert* x) {
1126 LIRItem value(x->value(), this);
1127 value.load_item();
1128 LIR_Opr input = value.result();
1129 LIR_Opr result = rlock(x);
1130 __ convert(x->op(), input, result);
1131 assert(result->is_virtual(), "result must be virtual register");
1132 set_result(x, result);
1133 }
1134
1135
1136 void LIRGenerator::do_NewInstance(NewInstance* x) {
1137 print_if_not_loaded(x);
1138
1139 CodeEmitInfo* info = state_for(x, x->needs_state_before() ? x->state_before() : x->state());
1140 LIR_Opr reg = result_register_for(x->type());
1141 new_instance(reg, x->klass(), x->is_unresolved(),
1142 !x->is_unresolved() && x->klass()->is_inlinetype(),
1143 FrameMap::rcx_oop_opr,
1144 FrameMap::rdi_oop_opr,
1145 FrameMap::rsi_oop_opr,
1146 LIR_OprFact::illegalOpr,
1147 FrameMap::rdx_metadata_opr, info);
1148 LIR_Opr result = rlock_result(x);
1149 __ move(reg, result);
1150 }
1151
1152 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
1153 CodeEmitInfo* info = nullptr;
1154 if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
1155 info = state_for(x, x->state_before());
1156 info->set_force_reexecute();
1157 } else {
1158 info = state_for(x, x->state());
1159 }
1160
1161 LIRItem length(x->length(), this);
1162 length.load_item_force(FrameMap::rbx_opr);
1163
1164 LIR_Opr reg = result_register_for(x->type());
1165 LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1166 LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1167 LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1168 LIR_Opr tmp4 = reg;
1169 LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1170 LIR_Opr len = length.result();
1171 BasicType elem_type = x->elt_type();
1172
1173 __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
1174
1175 CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
1176 __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path, x->zero_array());
1177
1178 LIR_Opr result = rlock_result(x);
1179 __ move(reg, result);
1180 }
1181
1182
1183 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
1184 LIRItem length(x->length(), this);
1185 // in case of patching (i.e., object class is not yet loaded), we need to reexecute the instruction
1186 // and therefore provide the state before the parameters have been consumed
1187 CodeEmitInfo* patching_info = nullptr;
1188 if (!x->klass()->is_loaded() || PatchALot) {
1189 patching_info = state_for(x, x->state_before());
1190 }
1191
1192 CodeEmitInfo* info = state_for(x, x->state());
1193
1194 const LIR_Opr reg = result_register_for(x->type());
1195 LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1196 LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1197 LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1198 LIR_Opr tmp4 = reg;
1199 LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1200
1201 length.load_item_force(FrameMap::rbx_opr);
1202 LIR_Opr len = length.result();
1203
1204 ciKlass* obj = ciObjArrayKlass::make(x->klass());
1205
1206 // TODO 8265122 Implement a fast path for this
1207 bool is_flat = obj->is_loaded() && obj->is_flat_array_klass();
1208 bool is_null_free = obj->is_loaded() && obj->as_array_klass()->is_elem_null_free();
1209
1210 CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info, is_null_free);
1211 if (obj == ciEnv::unloaded_ciobjarrayklass()) {
1212 BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
1213 }
1214 klass2reg_with_patching(klass_reg, obj, patching_info);
1215 bool always_slow_path = is_null_free || is_flat;
1216 __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path, true /*zero_array*/, always_slow_path);
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 __ membar_acquire();
1453 }