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_Defs.hpp"
27 #include "c1/c1_FrameMap.hpp"
28 #include "c1/c1_Instruction.hpp"
29 #include "c1/c1_LIRAssembler.hpp"
30 #include "c1/c1_LIRGenerator.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciArrayKlass.hpp"
33 #include "ci/ciFlatArrayKlass.hpp"
34 #include "ci/ciInlineKlass.hpp"
35 #include "ci/ciInstance.hpp"
36 #include "ci/ciObjArray.hpp"
37 #include "ci/ciObjArrayKlass.hpp"
38 #include "ci/ciUtilities.hpp"
39 #include "compiler/compilerDefinitions.inline.hpp"
40 #include "compiler/compilerOracle.hpp"
41 #include "gc/shared/barrierSet.hpp"
42 #include "gc/shared/c1/barrierSetC1.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/methodCounters.hpp"
45 #include "runtime/arguments.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/stubRoutines.hpp"
48 #include "runtime/vm_version.hpp"
49 #include "utilities/bitMap.inline.hpp"
50 #include "utilities/macros.hpp"
51 #include "utilities/powerOfTwo.hpp"
52
53 #ifdef ASSERT
54 #define __ gen()->lir(__FILE__, __LINE__)->
55 #else
56 #define __ gen()->lir()->
57 #endif
58
59 #ifndef PATCHED_ADDR
60 #define PATCHED_ADDR (max_jint)
61 #endif
62
63 void PhiResolverState::reset() {
64 _virtual_operands.clear();
65 _other_operands.clear();
66 _vreg_table.clear();
67 }
68
69
70 //--------------------------------------------------------------
71 // PhiResolver
72
73 // Resolves cycles:
74 //
75 // r1 := r2 becomes temp := r1
76 // r2 := r1 r1 := r2
77 // r2 := temp
78 // and orders moves:
79 //
80 // r2 := r3 becomes r1 := r2
81 // r1 := r2 r2 := r3
82
83 PhiResolver::PhiResolver(LIRGenerator* gen)
84 : _gen(gen)
85 , _state(gen->resolver_state())
86 , _loop(nullptr)
87 , _temp(LIR_OprFact::illegalOpr)
88 {
89 // reinitialize the shared state arrays
90 _state.reset();
91 }
92
93
94 void PhiResolver::emit_move(LIR_Opr src, LIR_Opr dest) {
95 assert(src->is_valid(), "");
96 assert(dest->is_valid(), "");
97 __ move(src, dest);
98 }
99
100
101 void PhiResolver::move_temp_to(LIR_Opr dest) {
102 assert(_temp->is_valid(), "");
103 emit_move(_temp, dest);
104 NOT_PRODUCT(_temp = LIR_OprFact::illegalOpr);
105 }
106
107
108 void PhiResolver::move_to_temp(LIR_Opr src) {
109 assert(_temp->is_illegal(), "");
110 _temp = _gen->new_register(src->type());
111 emit_move(src, _temp);
112 }
113
114
115 // Traverse assignment graph in depth first order and generate moves in post order
116 // ie. two assignments: b := c, a := b start with node c:
117 // Call graph: move(null, c) -> move(c, b) -> move(b, a)
118 // Generates moves in this order: move b to a and move c to b
119 // ie. cycle a := b, b := a start with node a
120 // Call graph: move(null, a) -> move(a, b) -> move(b, a)
121 // Generates moves in this order: move b to temp, move a to b, move temp to a
122 void PhiResolver::move(ResolveNode* src, ResolveNode* dest) {
123 if (!dest->visited()) {
124 dest->set_visited();
125 for (int i = dest->no_of_destinations()-1; i >= 0; i --) {
126 move(dest, dest->destination_at(i));
127 }
128 } else if (!dest->start_node()) {
129 // cylce in graph detected
130 assert(_loop == nullptr, "only one loop valid!");
131 _loop = dest;
132 move_to_temp(src->operand());
133 return;
134 } // else dest is a start node
135
136 if (!dest->assigned()) {
137 if (_loop == dest) {
138 move_temp_to(dest->operand());
139 dest->set_assigned();
140 } else if (src != nullptr) {
141 emit_move(src->operand(), dest->operand());
142 dest->set_assigned();
143 }
144 }
145 }
146
147
148 PhiResolver::~PhiResolver() {
149 int i;
150 // resolve any cycles in moves from and to virtual registers
151 for (i = virtual_operands().length() - 1; i >= 0; i --) {
152 ResolveNode* node = virtual_operands().at(i);
153 if (!node->visited()) {
154 _loop = nullptr;
155 move(nullptr, node);
156 node->set_start_node();
157 assert(_temp->is_illegal(), "move_temp_to() call missing");
158 }
159 }
160
161 // generate move for move from non virtual register to abitrary destination
162 for (i = other_operands().length() - 1; i >= 0; i --) {
163 ResolveNode* node = other_operands().at(i);
164 for (int j = node->no_of_destinations() - 1; j >= 0; j --) {
165 emit_move(node->operand(), node->destination_at(j)->operand());
166 }
167 }
168 }
169
170
171 ResolveNode* PhiResolver::create_node(LIR_Opr opr, bool source) {
172 ResolveNode* node;
173 if (opr->is_virtual()) {
174 int vreg_num = opr->vreg_number();
175 node = vreg_table().at_grow(vreg_num, nullptr);
176 assert(node == nullptr || node->operand() == opr, "");
177 if (node == nullptr) {
178 node = new ResolveNode(opr);
179 vreg_table().at_put(vreg_num, node);
180 }
181 // Make sure that all virtual operands show up in the list when
182 // they are used as the source of a move.
183 if (source && !virtual_operands().contains(node)) {
184 virtual_operands().append(node);
185 }
186 } else {
187 assert(source, "");
188 node = new ResolveNode(opr);
189 other_operands().append(node);
190 }
191 return node;
192 }
193
194
195 void PhiResolver::move(LIR_Opr src, LIR_Opr dest) {
196 assert(dest->is_virtual(), "");
197 // tty->print("move "); src->print(); tty->print(" to "); dest->print(); tty->cr();
198 assert(src->is_valid(), "");
199 assert(dest->is_valid(), "");
200 ResolveNode* source = source_node(src);
201 source->append(destination_node(dest));
202 }
203
204
205 //--------------------------------------------------------------
206 // LIRItem
207
208 void LIRItem::set_result(LIR_Opr opr) {
209 assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change");
210 value()->set_operand(opr);
211
212 #ifdef ASSERT
213 if (opr->is_virtual()) {
214 _gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), nullptr);
215 }
216 #endif
217
218 _result = opr;
219 }
220
221 void LIRItem::load_item() {
222 assert(!_gen->in_conditional_code(), "LIRItem cannot be loaded in conditional code");
223
224 if (result()->is_illegal()) {
225 // update the items result
226 _result = value()->operand();
227 }
228 if (!result()->is_register()) {
229 LIR_Opr reg = _gen->new_register(value()->type());
230 __ move(result(), reg);
231 if (result()->is_constant()) {
232 _result = reg;
233 } else {
234 set_result(reg);
235 }
236 }
237 }
238
239
240 void LIRItem::load_for_store(BasicType type) {
241 if (_gen->can_store_as_constant(value(), type)) {
242 _result = value()->operand();
243 if (!_result->is_constant()) {
244 _result = LIR_OprFact::value_type(value()->type());
245 }
246 } else if (type == T_BYTE || type == T_BOOLEAN) {
247 load_byte_item();
248 } else {
249 load_item();
250 }
251 }
252
253 void LIRItem::load_item_force(LIR_Opr reg) {
254 LIR_Opr r = result();
255 if (r != reg) {
256 #if !defined(ARM) && !defined(E500V2)
257 if (r->type() != reg->type()) {
258 // moves between different types need an intervening spill slot
259 r = _gen->force_to_spill(r, reg->type());
260 }
261 #endif
262 __ move(r, reg);
263 _result = reg;
264 }
265 }
266
267 ciObject* LIRItem::get_jobject_constant() const {
268 ObjectType* oc = type()->as_ObjectType();
269 if (oc) {
270 return oc->constant_value();
271 }
272 return nullptr;
273 }
274
275
276 jint LIRItem::get_jint_constant() const {
277 assert(is_constant() && value() != nullptr, "");
278 assert(type()->as_IntConstant() != nullptr, "type check");
279 return type()->as_IntConstant()->value();
280 }
281
282
283 jint LIRItem::get_address_constant() const {
284 assert(is_constant() && value() != nullptr, "");
285 assert(type()->as_AddressConstant() != nullptr, "type check");
286 return type()->as_AddressConstant()->value();
287 }
288
289
290 jfloat LIRItem::get_jfloat_constant() const {
291 assert(is_constant() && value() != nullptr, "");
292 assert(type()->as_FloatConstant() != nullptr, "type check");
293 return type()->as_FloatConstant()->value();
294 }
295
296
297 jdouble LIRItem::get_jdouble_constant() const {
298 assert(is_constant() && value() != nullptr, "");
299 assert(type()->as_DoubleConstant() != nullptr, "type check");
300 return type()->as_DoubleConstant()->value();
301 }
302
303
304 jlong LIRItem::get_jlong_constant() const {
305 assert(is_constant() && value() != nullptr, "");
306 assert(type()->as_LongConstant() != nullptr, "type check");
307 return type()->as_LongConstant()->value();
308 }
309
310
311
312 //--------------------------------------------------------------
313
314
315 void LIRGenerator::block_do_prolog(BlockBegin* block) {
316 #ifndef PRODUCT
317 if (PrintIRWithLIR) {
318 block->print();
319 }
320 #endif
321
322 // set up the list of LIR instructions
323 assert(block->lir() == nullptr, "LIR list already computed for this block");
324 _lir = new LIR_List(compilation(), block);
325 block->set_lir(_lir);
326
327 __ branch_destination(block->label());
328
329 if (LIRTraceExecution &&
330 Compilation::current()->hir()->start()->block_id() != block->block_id() &&
331 !block->is_set(BlockBegin::exception_entry_flag)) {
332 assert(block->lir()->instructions_list()->length() == 1, "should come right after br_dst");
333 trace_block_entry(block);
334 }
335 }
336
337
338 void LIRGenerator::block_do_epilog(BlockBegin* block) {
339 #ifndef PRODUCT
340 if (PrintIRWithLIR) {
341 tty->cr();
342 }
343 #endif
344
345 // LIR_Opr for unpinned constants shouldn't be referenced by other
346 // blocks so clear them out after processing the block.
347 for (int i = 0; i < _unpinned_constants.length(); i++) {
348 _unpinned_constants.at(i)->clear_operand();
349 }
350 _unpinned_constants.trunc_to(0);
351
352 // clear our any registers for other local constants
353 _constants.trunc_to(0);
354 _reg_for_constants.trunc_to(0);
355 }
356
357
358 void LIRGenerator::block_do(BlockBegin* block) {
359 CHECK_BAILOUT();
360
361 block_do_prolog(block);
362 set_block(block);
363
364 for (Instruction* instr = block; instr != nullptr; instr = instr->next()) {
365 if (instr->is_pinned()) do_root(instr);
366 }
367
368 set_block(nullptr);
369 block_do_epilog(block);
370 }
371
372
373 //-------------------------LIRGenerator-----------------------------
374
375 // This is where the tree-walk starts; instr must be root;
376 void LIRGenerator::do_root(Value instr) {
377 CHECK_BAILOUT();
378
379 InstructionMark im(compilation(), instr);
380
381 assert(instr->is_pinned(), "use only with roots");
382 assert(instr->subst() == instr, "shouldn't have missed substitution");
383
384 instr->visit(this);
385
386 assert(!instr->has_uses() || instr->operand()->is_valid() ||
387 instr->as_Constant() != nullptr || bailed_out(), "invalid item set");
388 }
389
390
391 // This is called for each node in tree; the walk stops if a root is reached
392 void LIRGenerator::walk(Value instr) {
393 InstructionMark im(compilation(), instr);
394 //stop walk when encounter a root
395 if ((instr->is_pinned() && instr->as_Phi() == nullptr) || instr->operand()->is_valid()) {
396 assert(instr->operand() != LIR_OprFact::illegalOpr || instr->as_Constant() != nullptr, "this root has not yet been visited");
397 } else {
398 assert(instr->subst() == instr, "shouldn't have missed substitution");
399 instr->visit(this);
400 // assert(instr->use_count() > 0 || instr->as_Phi() != nullptr, "leaf instruction must have a use");
401 }
402 }
403
404
405 CodeEmitInfo* LIRGenerator::state_for(Instruction* x, ValueStack* state, bool ignore_xhandler) {
406 assert(state != nullptr, "state must be defined");
407
408 #ifndef PRODUCT
409 state->verify();
410 #endif
411
412 ValueStack* s = state;
413 for_each_state(s) {
414 if (s->kind() == ValueStack::EmptyExceptionState ||
415 s->kind() == ValueStack::CallerEmptyExceptionState)
416 {
417 #ifdef ASSERT
418 int index;
419 Value value;
420 for_each_stack_value(s, index, value) {
421 fatal("state must be empty");
422 }
423 for_each_local_value(s, index, value) {
424 fatal("state must be empty");
425 }
426 #endif
427 assert(s->locks_size() == 0 || s->locks_size() == 1, "state must be empty");
428 continue;
429 }
430
431 int index;
432 Value value;
433 for_each_stack_value(s, index, value) {
434 assert(value->subst() == value, "missed substitution");
435 if (!value->is_pinned() && value->as_Constant() == nullptr && value->as_Local() == nullptr) {
436 walk(value);
437 assert(value->operand()->is_valid(), "must be evaluated now");
438 }
439 }
440
441 int bci = s->bci();
442 IRScope* scope = s->scope();
443 ciMethod* method = scope->method();
444
445 MethodLivenessResult liveness = method->liveness_at_bci(bci);
446 if (bci == SynchronizationEntryBCI) {
447 if (x->as_ExceptionObject() || x->as_Throw()) {
448 // all locals are dead on exit from the synthetic unlocker
449 liveness.clear();
450 } else {
451 assert(x->as_MonitorEnter() || x->as_ProfileInvoke(), "only other cases are MonitorEnter and ProfileInvoke");
452 }
453 }
454 if (!liveness.is_valid()) {
455 // Degenerate or breakpointed method.
456 bailout("Degenerate or breakpointed method");
457 } else {
458 assert((int)liveness.size() == s->locals_size(), "error in use of liveness");
459 for_each_local_value(s, index, value) {
460 assert(value->subst() == value, "missed substitution");
461 if (liveness.at(index) && !value->type()->is_illegal()) {
462 if (!value->is_pinned() && value->as_Constant() == nullptr && value->as_Local() == nullptr) {
463 walk(value);
464 assert(value->operand()->is_valid(), "must be evaluated now");
465 }
466 } else {
467 // null out this local so that linear scan can assume that all non-null values are live.
468 s->invalidate_local(index);
469 }
470 }
471 }
472 }
473
474 return new CodeEmitInfo(state, ignore_xhandler ? nullptr : x->exception_handlers(), x->check_flag(Instruction::DeoptimizeOnException));
475 }
476
477
478 CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {
479 return state_for(x, x->exception_state());
480 }
481
482
483 void LIRGenerator::klass2reg_with_patching(LIR_Opr r, ciMetadata* obj, CodeEmitInfo* info, bool need_resolve) {
484 /* C2 relies on constant pool entries being resolved (ciTypeFlow), so if tiered compilation
485 * is active and the class hasn't yet been resolved we need to emit a patch that resolves
486 * the class. */
487 if ((!CompilerConfig::is_c1_only() && need_resolve) || !obj->is_loaded() || PatchALot) {
488 assert(info != nullptr, "info must be set if class is not loaded");
489 __ klass2reg_patch(nullptr, r, info);
490 } else {
491 // no patching needed
492 __ metadata2reg(obj->constant_encoding(), r);
493 }
494 }
495
496
497 void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index,
498 CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) {
499 CodeStub* stub = new RangeCheckStub(range_check_info, index, array);
500 if (index->is_constant()) {
501 cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(),
502 index->as_jint(), null_check_info);
503 __ branch(lir_cond_belowEqual, stub); // forward branch
504 } else {
505 cmp_reg_mem(lir_cond_aboveEqual, index, array,
506 arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info);
507 __ branch(lir_cond_aboveEqual, stub); // forward branch
508 }
509 }
510
511 void LIRGenerator::arithmetic_op(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp_op, CodeEmitInfo* info) {
512 LIR_Opr result_op = result;
513 LIR_Opr left_op = left;
514 LIR_Opr right_op = right;
515
516 if (two_operand_lir_form && left_op != result_op) {
517 assert(right_op != result_op, "malformed");
518 __ move(left_op, result_op);
519 left_op = result_op;
520 }
521
522 switch(code) {
523 case Bytecodes::_dadd:
524 case Bytecodes::_fadd:
525 case Bytecodes::_ladd:
526 case Bytecodes::_iadd: __ add(left_op, right_op, result_op); break;
527 case Bytecodes::_fmul:
528 case Bytecodes::_lmul: __ mul(left_op, right_op, result_op); break;
529
530 case Bytecodes::_dmul: __ mul(left_op, right_op, result_op, tmp_op); break;
531
532 case Bytecodes::_imul:
533 {
534 bool did_strength_reduce = false;
535
536 if (right->is_constant()) {
537 jint c = right->as_jint();
538 if (c > 0 && is_power_of_2(c)) {
539 // do not need tmp here
540 __ shift_left(left_op, exact_log2(c), result_op);
541 did_strength_reduce = true;
542 } else {
543 did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op);
544 }
545 }
546 // we couldn't strength reduce so just emit the multiply
547 if (!did_strength_reduce) {
548 __ mul(left_op, right_op, result_op);
549 }
550 }
551 break;
552
553 case Bytecodes::_dsub:
554 case Bytecodes::_fsub:
555 case Bytecodes::_lsub:
556 case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break;
557
558 case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break;
559 // ldiv and lrem are implemented with a direct runtime call
560
561 case Bytecodes::_ddiv: __ div(left_op, right_op, result_op, tmp_op); break;
562
563 case Bytecodes::_drem:
564 case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break;
565
566 default: ShouldNotReachHere();
567 }
568 }
569
570
571 void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {
572 arithmetic_op(code, result, left, right, tmp);
573 }
574
575
576 void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
577 arithmetic_op(code, result, left, right, LIR_OprFact::illegalOpr, info);
578 }
579
580
581 void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {
582 arithmetic_op(code, result, left, right, tmp);
583 }
584
585
586 void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) {
587
588 if (two_operand_lir_form && value != result_op
589 // Only 32bit right shifts require two operand form on S390.
590 S390_ONLY(&& (code == Bytecodes::_ishr || code == Bytecodes::_iushr))) {
591 assert(count != result_op, "malformed");
592 __ move(value, result_op);
593 value = result_op;
594 }
595
596 assert(count->is_constant() || count->is_register(), "must be");
597 switch(code) {
598 case Bytecodes::_ishl:
599 case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break;
600 case Bytecodes::_ishr:
601 case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break;
602 case Bytecodes::_iushr:
603 case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break;
604 default: ShouldNotReachHere();
605 }
606 }
607
608
609 void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) {
610 if (two_operand_lir_form && left_op != result_op) {
611 assert(right_op != result_op, "malformed");
612 __ move(left_op, result_op);
613 left_op = result_op;
614 }
615
616 switch(code) {
617 case Bytecodes::_iand:
618 case Bytecodes::_land: __ logical_and(left_op, right_op, result_op); break;
619
620 case Bytecodes::_ior:
621 case Bytecodes::_lor: __ logical_or(left_op, right_op, result_op); break;
622
623 case Bytecodes::_ixor:
624 case Bytecodes::_lxor: __ logical_xor(left_op, right_op, result_op); break;
625
626 default: ShouldNotReachHere();
627 }
628 }
629
630
631 void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no,
632 CodeEmitInfo* info_for_exception, CodeEmitInfo* info, CodeStub* throw_ie_stub) {
633 // for slow path, use debug info for state after successful locking
634 CodeStub* slow_path = new MonitorEnterStub(object, lock, info, throw_ie_stub, scratch);
635 __ load_stack_address_monitor(monitor_no, lock);
636 // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
637 __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception, throw_ie_stub);
638 }
639
640
641 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
642 // setup registers
643 LIR_Opr hdr = lock;
644 lock = new_hdr;
645 CodeStub* slow_path = new MonitorExitStub(lock, monitor_no);
646 __ load_stack_address_monitor(monitor_no, lock);
647 __ unlock_object(hdr, object, lock, scratch, slow_path);
648 }
649
650 #ifndef PRODUCT
651 void LIRGenerator::print_if_not_loaded(const NewInstance* new_instance) {
652 if (PrintNotLoaded && !new_instance->klass()->is_loaded()) {
653 tty->print_cr(" ###class not loaded at new bci %d", new_instance->printable_bci());
654 } else if (PrintNotLoaded && (!CompilerConfig::is_c1_only() && new_instance->is_unresolved())) {
655 tty->print_cr(" ###class not resolved at new bci %d", new_instance->printable_bci());
656 }
657 }
658 #endif
659
660 void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, bool is_unresolved, bool allow_inline, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) {
661 if (allow_inline) {
662 assert(!is_unresolved && klass->is_loaded(), "inline type klass should be resolved");
663 __ metadata2reg(klass->constant_encoding(), klass_reg);
664 } else {
665 klass2reg_with_patching(klass_reg, klass, info, is_unresolved);
666 }
667 // If klass is not loaded we do not know if the klass has finalizers or is an unexpected inline klass
668 if (UseFastNewInstance && klass->is_loaded() && (allow_inline || !klass->is_inlinetype())
669 && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
670
671 StubId stub_id = klass->is_initialized() ? StubId::c1_fast_new_instance_id : StubId::c1_fast_new_instance_init_check_id;
672
673 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
674
675 assert(klass->is_loaded(), "must be loaded");
676 // allocate space for instance
677 assert(klass->size_helper() > 0, "illegal instance size");
678 const int instance_size = align_object_size(klass->size_helper());
679 __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
680 oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
681 } else {
682 CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, StubId::c1_new_instance_id);
683 __ jump(slow_path);
684 __ branch_destination(slow_path->continuation());
685 }
686 }
687
688
689 static bool is_constant_zero(Instruction* inst) {
690 IntConstant* c = inst->type()->as_IntConstant();
691 if (c) {
692 return (c->value() == 0);
693 }
694 return false;
695 }
696
697
698 static bool positive_constant(Instruction* inst) {
699 IntConstant* c = inst->type()->as_IntConstant();
700 if (c) {
701 return (c->value() >= 0);
702 }
703 return false;
704 }
705
706
707 static ciArrayKlass* as_array_klass(ciType* type) {
708 if (type != nullptr && type->is_array_klass() && type->is_loaded()) {
709 return (ciArrayKlass*)type;
710 } else {
711 return nullptr;
712 }
713 }
714
715 static ciType* phi_declared_type(Phi* phi) {
716 ciType* t = phi->operand_at(0)->declared_type();
717 if (t == nullptr) {
718 return nullptr;
719 }
720 for(int i = 1; i < phi->operand_count(); i++) {
721 if (t != phi->operand_at(i)->declared_type()) {
722 return nullptr;
723 }
724 }
725 return t;
726 }
727
728 void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) {
729 Instruction* src = x->argument_at(0);
730 Instruction* src_pos = x->argument_at(1);
731 Instruction* dst = x->argument_at(2);
732 Instruction* dst_pos = x->argument_at(3);
733 Instruction* length = x->argument_at(4);
734
735 // first try to identify the likely type of the arrays involved
736 ciArrayKlass* expected_type = nullptr;
737 bool is_exact = false, src_objarray = false, dst_objarray = false;
738 {
739 ciArrayKlass* src_exact_type = as_array_klass(src->exact_type());
740 ciArrayKlass* src_declared_type = as_array_klass(src->declared_type());
741 Phi* phi;
742 if (src_declared_type == nullptr && (phi = src->as_Phi()) != nullptr) {
743 src_declared_type = as_array_klass(phi_declared_type(phi));
744 }
745 ciArrayKlass* dst_exact_type = as_array_klass(dst->exact_type());
746 ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type());
747 if (dst_declared_type == nullptr && (phi = dst->as_Phi()) != nullptr) {
748 dst_declared_type = as_array_klass(phi_declared_type(phi));
749 }
750
751 if (src_exact_type != nullptr && src_exact_type == dst_exact_type) {
752 // the types exactly match so the type is fully known
753 is_exact = true;
754 expected_type = src_exact_type;
755 } else if (dst_exact_type != nullptr && dst_exact_type->is_obj_array_klass()) {
756 ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type;
757 ciArrayKlass* src_type = nullptr;
758 if (src_exact_type != nullptr && src_exact_type->is_obj_array_klass()) {
759 src_type = (ciArrayKlass*) src_exact_type;
760 } else if (src_declared_type != nullptr && src_declared_type->is_obj_array_klass()) {
761 src_type = (ciArrayKlass*) src_declared_type;
762 }
763 if (src_type != nullptr) {
764 if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
765 is_exact = true;
766 expected_type = dst_type;
767 }
768 }
769 }
770 // at least pass along a good guess
771 if (expected_type == nullptr) expected_type = dst_exact_type;
772 if (expected_type == nullptr) expected_type = src_declared_type;
773 if (expected_type == nullptr) expected_type = dst_declared_type;
774
775 if (expected_type != nullptr && expected_type->is_obj_array_klass()) {
776 // For a direct pointer comparison, we need the refined array klass pointer
777 expected_type = ciObjArrayKlass::make(expected_type->as_array_klass()->element_klass());
778 }
779
780 src_objarray = (src_exact_type && src_exact_type->is_obj_array_klass()) || (src_declared_type && src_declared_type->is_obj_array_klass());
781 dst_objarray = (dst_exact_type && dst_exact_type->is_obj_array_klass()) || (dst_declared_type && dst_declared_type->is_obj_array_klass());
782 }
783
784 // if a probable array type has been identified, figure out if any
785 // of the required checks for a fast case can be elided.
786 int flags = LIR_OpArrayCopy::all_flags;
787
788 // TODO 8251971 Compare ArrayKlass::properties() of source and destination
789 // array here instead, see also LIR_Assembler::arraycopy_inlinetype_check
790 if (!src->is_loaded_flat_array() && !dst->is_loaded_flat_array()) {
791 flags &= ~LIR_OpArrayCopy::always_slow_path;
792 }
793 if (!src->maybe_flat_array()) {
794 flags &= ~LIR_OpArrayCopy::src_inlinetype_check;
795 }
796 if (!dst->maybe_flat_array() && !dst->maybe_null_free_array()) {
797 flags &= ~LIR_OpArrayCopy::dst_inlinetype_check;
798 }
799
800 if (!src_objarray)
801 flags &= ~LIR_OpArrayCopy::src_objarray;
802 if (!dst_objarray)
803 flags &= ~LIR_OpArrayCopy::dst_objarray;
804
805 if (!x->arg_needs_null_check(0))
806 flags &= ~LIR_OpArrayCopy::src_null_check;
807 if (!x->arg_needs_null_check(2))
808 flags &= ~LIR_OpArrayCopy::dst_null_check;
809
810
811 if (expected_type != nullptr) {
812 Value length_limit = nullptr;
813
814 IfOp* ifop = length->as_IfOp();
815 if (ifop != nullptr) {
816 // look for expressions like min(v, a.length) which ends up as
817 // x > y ? y : x or x >= y ? y : x
818 if ((ifop->cond() == If::gtr || ifop->cond() == If::geq) &&
819 ifop->x() == ifop->fval() &&
820 ifop->y() == ifop->tval()) {
821 length_limit = ifop->y();
822 }
823 }
824
825 // try to skip null checks and range checks
826 NewArray* src_array = src->as_NewArray();
827 if (src_array != nullptr) {
828 flags &= ~LIR_OpArrayCopy::src_null_check;
829 if (length_limit != nullptr &&
830 src_array->length() == length_limit &&
831 is_constant_zero(src_pos)) {
832 flags &= ~LIR_OpArrayCopy::src_range_check;
833 }
834 }
835
836 NewArray* dst_array = dst->as_NewArray();
837 if (dst_array != nullptr) {
838 flags &= ~LIR_OpArrayCopy::dst_null_check;
839 if (length_limit != nullptr &&
840 dst_array->length() == length_limit &&
841 is_constant_zero(dst_pos)) {
842 flags &= ~LIR_OpArrayCopy::dst_range_check;
843 }
844 }
845
846 // check from incoming constant values
847 if (positive_constant(src_pos))
848 flags &= ~LIR_OpArrayCopy::src_pos_positive_check;
849 if (positive_constant(dst_pos))
850 flags &= ~LIR_OpArrayCopy::dst_pos_positive_check;
851 if (positive_constant(length))
852 flags &= ~LIR_OpArrayCopy::length_positive_check;
853
854 // see if the range check can be elided, which might also imply
855 // that src or dst is non-null.
856 ArrayLength* al = length->as_ArrayLength();
857 if (al != nullptr) {
858 if (al->array() == src) {
859 // it's the length of the source array
860 flags &= ~LIR_OpArrayCopy::length_positive_check;
861 flags &= ~LIR_OpArrayCopy::src_null_check;
862 if (is_constant_zero(src_pos))
863 flags &= ~LIR_OpArrayCopy::src_range_check;
864 }
865 if (al->array() == dst) {
866 // it's the length of the destination array
867 flags &= ~LIR_OpArrayCopy::length_positive_check;
868 flags &= ~LIR_OpArrayCopy::dst_null_check;
869 if (is_constant_zero(dst_pos))
870 flags &= ~LIR_OpArrayCopy::dst_range_check;
871 }
872 }
873 if (is_exact) {
874 flags &= ~LIR_OpArrayCopy::type_check;
875 }
876 }
877
878 IntConstant* src_int = src_pos->type()->as_IntConstant();
879 IntConstant* dst_int = dst_pos->type()->as_IntConstant();
880 if (src_int && dst_int) {
881 int s_offs = src_int->value();
882 int d_offs = dst_int->value();
883 if (src_int->value() >= dst_int->value()) {
884 flags &= ~LIR_OpArrayCopy::overlapping;
885 }
886 if (expected_type != nullptr) {
887 BasicType t = expected_type->element_type()->basic_type();
888 int element_size = type2aelembytes(t);
889 if (((arrayOopDesc::base_offset_in_bytes(t) + (uint)s_offs * element_size) % HeapWordSize == 0) &&
890 ((arrayOopDesc::base_offset_in_bytes(t) + (uint)d_offs * element_size) % HeapWordSize == 0)) {
891 flags &= ~LIR_OpArrayCopy::unaligned;
892 }
893 }
894 } else if (src_pos == dst_pos || is_constant_zero(dst_pos)) {
895 // src and dest positions are the same, or dst is zero so assume
896 // nonoverlapping copy.
897 flags &= ~LIR_OpArrayCopy::overlapping;
898 }
899
900 if (src == dst) {
901 // moving within a single array so no type checks are needed
902 if (flags & LIR_OpArrayCopy::type_check) {
903 flags &= ~LIR_OpArrayCopy::type_check;
904 }
905 }
906 *flagsp = flags;
907 *expected_typep = (ciArrayKlass*)expected_type;
908 }
909
910
911 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
912 assert(type2size[t] == type2size[value->type()],
913 "size mismatch: t=%s, value->type()=%s", type2name(t), type2name(value->type()));
914 if (!value->is_register()) {
915 // force into a register
916 LIR_Opr r = new_register(value->type());
917 __ move(value, r);
918 value = r;
919 }
920
921 // create a spill location
922 LIR_Opr tmp = new_register(t);
923 set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
924
925 // move from register to spill
926 __ move(value, tmp);
927 return tmp;
928 }
929
930 void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) {
931 if (if_instr->should_profile()) {
932 ciMethod* method = if_instr->profiled_method();
933 assert(method != nullptr, "method should be set if branch is profiled");
934 ciMethodData* md = method->method_data_or_null();
935 assert(md != nullptr, "Sanity");
936 ciProfileData* data = md->bci_to_data(if_instr->profiled_bci());
937 assert(data != nullptr, "must have profiling data");
938 assert(data->is_BranchData(), "need BranchData for two-way branches");
939 int taken_count_offset = md->byte_offset_of_slot(data, BranchData::taken_offset());
940 int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
941 if (if_instr->is_swapped()) {
942 int t = taken_count_offset;
943 taken_count_offset = not_taken_count_offset;
944 not_taken_count_offset = t;
945 }
946
947 LIR_Opr md_reg = new_register(T_METADATA);
948 __ metadata2reg(md->constant_encoding(), md_reg);
949
950 LIR_Opr data_offset_reg = new_pointer_register();
951 __ cmove(lir_cond(cond),
952 LIR_OprFact::intptrConst(taken_count_offset),
953 LIR_OprFact::intptrConst(not_taken_count_offset),
954 data_offset_reg, as_BasicType(if_instr->x()->type()));
955
956 // MDO cells are intptr_t, so the data_reg width is arch-dependent.
957 LIR_Opr data_reg = new_pointer_register();
958 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
959 __ move(data_addr, data_reg);
960 // Use leal instead of add to avoid destroying condition codes on x86
961 LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT);
962 __ leal(LIR_OprFact::address(fake_incr_value), data_reg);
963 __ move(data_reg, data_addr);
964 }
965 }
966
967 // Phi technique:
968 // This is about passing live values from one basic block to the other.
969 // In code generated with Java it is rather rare that more than one
970 // value is on the stack from one basic block to the other.
971 // We optimize our technique for efficient passing of one value
972 // (of type long, int, double..) but it can be extended.
973 // When entering or leaving a basic block, all registers and all spill
974 // slots are release and empty. We use the released registers
975 // and spill slots to pass the live values from one block
976 // to the other. The topmost value, i.e., the value on TOS of expression
977 // stack is passed in registers. All other values are stored in spilling
978 // area. Every Phi has an index which designates its spill slot
979 // At exit of a basic block, we fill the register(s) and spill slots.
980 // At entry of a basic block, the block_prolog sets up the content of phi nodes
981 // and locks necessary registers and spilling slots.
982
983
984 // move current value to referenced phi function
985 void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) {
986 Phi* phi = sux_val->as_Phi();
987 // cur_val can be null without phi being null in conjunction with inlining
988 if (phi != nullptr && cur_val != nullptr && cur_val != phi && !phi->is_illegal()) {
989 if (phi->is_local()) {
990 for (int i = 0; i < phi->operand_count(); i++) {
991 Value op = phi->operand_at(i);
992 if (op != nullptr && op->type()->is_illegal()) {
993 bailout("illegal phi operand");
994 }
995 }
996 }
997 Phi* cur_phi = cur_val->as_Phi();
998 if (cur_phi != nullptr && cur_phi->is_illegal()) {
999 // Phi and local would need to get invalidated
1000 // (which is unexpected for Linear Scan).
1001 // But this case is very rare so we simply bail out.
1002 bailout("propagation of illegal phi");
1003 return;
1004 }
1005 LIR_Opr operand = cur_val->operand();
1006 if (operand->is_illegal()) {
1007 assert(cur_val->as_Constant() != nullptr || cur_val->as_Local() != nullptr,
1008 "these can be produced lazily");
1009 operand = operand_for_instruction(cur_val);
1010 }
1011 resolver->move(operand, operand_for_instruction(phi));
1012 }
1013 }
1014
1015
1016 // Moves all stack values into their PHI position
1017 void LIRGenerator::move_to_phi(ValueStack* cur_state) {
1018 BlockBegin* bb = block();
1019 if (bb->number_of_sux() == 1) {
1020 BlockBegin* sux = bb->sux_at(0);
1021 assert(sux->number_of_preds() > 0, "invalid CFG");
1022
1023 // a block with only one predecessor never has phi functions
1024 if (sux->number_of_preds() > 1) {
1025 PhiResolver resolver(this);
1026
1027 ValueStack* sux_state = sux->state();
1028 Value sux_value;
1029 int index;
1030
1031 assert(cur_state->scope() == sux_state->scope(), "not matching");
1032 assert(cur_state->locals_size() == sux_state->locals_size(), "not matching");
1033 assert(cur_state->stack_size() == sux_state->stack_size(), "not matching");
1034
1035 for_each_stack_value(sux_state, index, sux_value) {
1036 move_to_phi(&resolver, cur_state->stack_at(index), sux_value);
1037 }
1038
1039 for_each_local_value(sux_state, index, sux_value) {
1040 move_to_phi(&resolver, cur_state->local_at(index), sux_value);
1041 }
1042
1043 assert(cur_state->caller_state() == sux_state->caller_state(), "caller states must be equal");
1044 }
1045 }
1046 }
1047
1048
1049 LIR_Opr LIRGenerator::new_register(BasicType type) {
1050 int vreg_num = _virtual_register_number;
1051 // Add a little fudge factor for the bailout since the bailout is only checked periodically. This allows us to hand out
1052 // a few extra registers before we really run out which helps to avoid to trip over assertions.
1053 if (vreg_num + 20 >= LIR_Opr::vreg_max) {
1054 bailout("out of virtual registers in LIR generator");
1055 if (vreg_num + 2 >= LIR_Opr::vreg_max) {
1056 // Wrap it around and continue until bailout really happens to avoid hitting assertions.
1057 _virtual_register_number = LIR_Opr::vreg_base;
1058 vreg_num = LIR_Opr::vreg_base;
1059 }
1060 }
1061 _virtual_register_number += 1;
1062 LIR_Opr vreg = LIR_OprFact::virtual_register(vreg_num, type);
1063 assert(vreg != LIR_OprFact::illegal(), "ran out of virtual registers");
1064 return vreg;
1065 }
1066
1067
1068 // Try to lock using register in hint
1069 LIR_Opr LIRGenerator::rlock(Value instr) {
1070 return new_register(instr->type());
1071 }
1072
1073
1074 // does an rlock and sets result
1075 LIR_Opr LIRGenerator::rlock_result(Value x) {
1076 LIR_Opr reg = rlock(x);
1077 set_result(x, reg);
1078 return reg;
1079 }
1080
1081
1082 // does an rlock and sets result
1083 LIR_Opr LIRGenerator::rlock_result(Value x, BasicType type) {
1084 LIR_Opr reg;
1085 switch (type) {
1086 case T_BYTE:
1087 case T_BOOLEAN:
1088 reg = rlock_byte(type);
1089 break;
1090 default:
1091 reg = rlock(x);
1092 break;
1093 }
1094
1095 set_result(x, reg);
1096 return reg;
1097 }
1098
1099
1100 //---------------------------------------------------------------------
1101 ciObject* LIRGenerator::get_jobject_constant(Value value) {
1102 ObjectType* oc = value->type()->as_ObjectType();
1103 if (oc) {
1104 return oc->constant_value();
1105 }
1106 return nullptr;
1107 }
1108
1109
1110 void LIRGenerator::do_ExceptionObject(ExceptionObject* x) {
1111 assert(block()->is_set(BlockBegin::exception_entry_flag), "ExceptionObject only allowed in exception handler block");
1112 assert(block()->next() == x, "ExceptionObject must be first instruction of block");
1113
1114 // no moves are created for phi functions at the begin of exception
1115 // handlers, so assign operands manually here
1116 for_each_phi_fun(block(), phi,
1117 if (!phi->is_illegal()) { operand_for_instruction(phi); });
1118
1119 LIR_Opr thread_reg = getThreadPointer();
1120 __ move_wide(new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT),
1121 exceptionOopOpr());
1122 __ move_wide(LIR_OprFact::oopConst(nullptr),
1123 new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT));
1124 __ move_wide(LIR_OprFact::oopConst(nullptr),
1125 new LIR_Address(thread_reg, in_bytes(JavaThread::exception_pc_offset()), T_OBJECT));
1126
1127 LIR_Opr result = new_register(T_OBJECT);
1128 __ move(exceptionOopOpr(), result);
1129 set_result(x, result);
1130 }
1131
1132
1133 //----------------------------------------------------------------------
1134 //----------------------------------------------------------------------
1135 //----------------------------------------------------------------------
1136 //----------------------------------------------------------------------
1137 // visitor functions
1138 //----------------------------------------------------------------------
1139 //----------------------------------------------------------------------
1140 //----------------------------------------------------------------------
1141 //----------------------------------------------------------------------
1142
1143 void LIRGenerator::do_Phi(Phi* x) {
1144 // phi functions are never visited directly
1145 ShouldNotReachHere();
1146 }
1147
1148
1149 // Code for a constant is generated lazily unless the constant is frequently used and can't be inlined.
1150 void LIRGenerator::do_Constant(Constant* x) {
1151 if (x->state_before() != nullptr) {
1152 // Any constant with a ValueStack requires patching so emit the patch here
1153 LIR_Opr reg = rlock_result(x);
1154 CodeEmitInfo* info = state_for(x, x->state_before());
1155 __ oop2reg_patch(nullptr, reg, info);
1156 } else if (x->use_count() > 1 && !can_inline_as_constant(x)) {
1157 if (!x->is_pinned()) {
1158 // unpinned constants are handled specially so that they can be
1159 // put into registers when they are used multiple times within a
1160 // block. After the block completes their operand will be
1161 // cleared so that other blocks can't refer to that register.
1162 set_result(x, load_constant(x));
1163 } else {
1164 LIR_Opr res = x->operand();
1165 if (!res->is_valid()) {
1166 res = LIR_OprFact::value_type(x->type());
1167 }
1168 if (res->is_constant()) {
1169 LIR_Opr reg = rlock_result(x);
1170 __ move(res, reg);
1171 } else {
1172 set_result(x, res);
1173 }
1174 }
1175 } else {
1176 set_result(x, LIR_OprFact::value_type(x->type()));
1177 }
1178 }
1179
1180
1181 void LIRGenerator::do_Local(Local* x) {
1182 // operand_for_instruction has the side effect of setting the result
1183 // so there's no need to do it here.
1184 operand_for_instruction(x);
1185 }
1186
1187
1188 void LIRGenerator::do_Return(Return* x) {
1189 if (compilation()->env()->dtrace_method_probes()) {
1190 BasicTypeList signature;
1191 signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread
1192 signature.append(T_METADATA); // Method*
1193 LIR_OprList* args = new LIR_OprList();
1194 args->append(getThreadPointer());
1195 LIR_Opr meth = new_register(T_METADATA);
1196 __ metadata2reg(method()->constant_encoding(), meth);
1197 args->append(meth);
1198 call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), voidType, nullptr);
1199 }
1200
1201 if (x->type()->is_void()) {
1202 __ return_op(LIR_OprFact::illegalOpr);
1203 } else {
1204 LIR_Opr reg = result_register_for(x->type(), /*callee=*/true);
1205 LIRItem result(x->result(), this);
1206
1207 result.load_item_force(reg);
1208 __ return_op(result.result());
1209 }
1210 set_no_result(x);
1211 }
1212
1213 // Example: ref.get()
1214 // Combination of LoadField and g1 pre-write barrier
1215 void LIRGenerator::do_Reference_get0(Intrinsic* x) {
1216
1217 const int referent_offset = java_lang_ref_Reference::referent_offset();
1218
1219 assert(x->number_of_arguments() == 1, "wrong type");
1220
1221 LIRItem reference(x->argument_at(0), this);
1222 reference.load_item();
1223
1224 // need to perform the null check on the reference object
1225 CodeEmitInfo* info = nullptr;
1226 if (x->needs_null_check()) {
1227 info = state_for(x);
1228 }
1229
1230 LIR_Opr result = rlock_result(x, T_OBJECT);
1231 access_load_at(IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT,
1232 reference, LIR_OprFact::intConst(referent_offset), result,
1233 nullptr, info);
1234 }
1235
1236 // Example: clazz.isInstance(object)
1237 void LIRGenerator::do_isInstance(Intrinsic* x) {
1238 assert(x->number_of_arguments() == 2, "wrong type");
1239
1240 LIRItem clazz(x->argument_at(0), this);
1241 LIRItem object(x->argument_at(1), this);
1242 clazz.load_item();
1243 object.load_item();
1244 LIR_Opr result = rlock_result(x);
1245
1246 // need to perform null check on clazz
1247 if (x->needs_null_check()) {
1248 CodeEmitInfo* info = state_for(x);
1249 __ null_check(clazz.result(), info);
1250 }
1251
1252 address pd_instanceof_fn = isInstance_entry();
1253 LIR_Opr call_result = call_runtime(clazz.value(), object.value(),
1254 pd_instanceof_fn,
1255 x->type(),
1256 nullptr); // null CodeEmitInfo results in a leaf call
1257 __ move(call_result, result);
1258 }
1259
1260 void LIRGenerator::load_klass(LIR_Opr obj, LIR_Opr klass, CodeEmitInfo* null_check_info) {
1261 __ load_klass(obj, klass, null_check_info);
1262 }
1263
1264 // Example: object.getClass ()
1265 void LIRGenerator::do_getClass(Intrinsic* x) {
1266 assert(x->number_of_arguments() == 1, "wrong type");
1267
1268 LIRItem rcvr(x->argument_at(0), this);
1269 rcvr.load_item();
1270 LIR_Opr temp = new_register(T_ADDRESS);
1271 LIR_Opr result = rlock_result(x);
1272
1273 // need to perform the null check on the rcvr
1274 CodeEmitInfo* info = nullptr;
1275 if (x->needs_null_check()) {
1276 info = state_for(x);
1277 }
1278
1279 LIR_Opr klass = new_register(T_METADATA);
1280 load_klass(rcvr.result(), klass, info);
1281 __ move_wide(new LIR_Address(klass, in_bytes(Klass::java_mirror_offset()), T_ADDRESS), temp);
1282 // mirror = ((OopHandle)mirror)->resolve();
1283 access_load(IN_NATIVE, T_OBJECT,
1284 LIR_OprFact::address(new LIR_Address(temp, T_OBJECT)), result);
1285 }
1286
1287 void LIRGenerator::do_getObjectSize(Intrinsic* x) {
1288 assert(x->number_of_arguments() == 3, "wrong type");
1289 LIR_Opr result_reg = rlock_result(x);
1290
1291 LIRItem value(x->argument_at(2), this);
1292 value.load_item();
1293
1294 LIR_Opr klass = new_register(T_METADATA);
1295 load_klass(value.result(), klass, nullptr);
1296 LIR_Opr layout = new_register(T_INT);
1297 __ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout);
1298
1299 LabelObj* L_done = new LabelObj();
1300 LabelObj* L_array = new LabelObj();
1301
1302 __ cmp(lir_cond_lessEqual, layout, 0);
1303 __ branch(lir_cond_lessEqual, L_array->label());
1304
1305 // Instance case: the layout helper gives us instance size almost directly,
1306 // but we need to mask out the _lh_instance_slow_path_bit.
1307
1308 assert((int) Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit");
1309
1310 LIR_Opr mask = load_immediate(~(jint) right_n_bits(LogBytesPerLong), T_INT);
1311 __ logical_and(layout, mask, layout);
1312 __ convert(Bytecodes::_i2l, layout, result_reg);
1313
1314 __ branch(lir_cond_always, L_done->label());
1315
1316 // Array case: size is round(header + element_size*arraylength).
1317 // Since arraylength is different for every array instance, we have to
1318 // compute the whole thing at runtime.
1319
1320 __ branch_destination(L_array->label());
1321
1322 int round_mask = MinObjAlignmentInBytes - 1;
1323
1324 // Figure out header sizes first.
1325 LIR_Opr hss = load_immediate(Klass::_lh_header_size_shift, T_INT);
1326 LIR_Opr hsm = load_immediate(Klass::_lh_header_size_mask, T_INT);
1327
1328 LIR_Opr header_size = new_register(T_INT);
1329 __ move(layout, header_size);
1330 LIR_Opr tmp = new_register(T_INT);
1331 __ unsigned_shift_right(header_size, hss, header_size, tmp);
1332 __ logical_and(header_size, hsm, header_size);
1333 __ add(header_size, LIR_OprFact::intConst(round_mask), header_size);
1334
1335 // Figure out the array length in bytes
1336 assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place");
1337 LIR_Opr l2esm = load_immediate(Klass::_lh_log2_element_size_mask, T_INT);
1338 __ logical_and(layout, l2esm, layout);
1339
1340 LIR_Opr length_int = new_register(T_INT);
1341 __ move(new LIR_Address(value.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), length_int);
1342
1343 #ifdef _LP64
1344 LIR_Opr length = new_register(T_LONG);
1345 __ convert(Bytecodes::_i2l, length_int, length);
1346 #endif
1347
1348 // Shift-left awkwardness. Normally it is just:
1349 // __ shift_left(length, layout, length);
1350 // But C1 cannot perform shift_left with non-constant count, so we end up
1351 // doing the per-bit loop dance here. x86_32 also does not know how to shift
1352 // longs, so we have to act on ints.
1353 LabelObj* L_shift_loop = new LabelObj();
1354 LabelObj* L_shift_exit = new LabelObj();
1355
1356 __ branch_destination(L_shift_loop->label());
1357 __ cmp(lir_cond_equal, layout, 0);
1358 __ branch(lir_cond_equal, L_shift_exit->label());
1359
1360 #ifdef _LP64
1361 __ shift_left(length, 1, length);
1362 #else
1363 __ shift_left(length_int, 1, length_int);
1364 #endif
1365
1366 __ sub(layout, LIR_OprFact::intConst(1), layout);
1367
1368 __ branch(lir_cond_always, L_shift_loop->label());
1369 __ branch_destination(L_shift_exit->label());
1370
1371 // Mix all up, round, and push to the result.
1372 #ifdef _LP64
1373 LIR_Opr header_size_long = new_register(T_LONG);
1374 __ convert(Bytecodes::_i2l, header_size, header_size_long);
1375 __ add(length, header_size_long, length);
1376 if (round_mask != 0) {
1377 LIR_Opr round_mask_opr = load_immediate(~(jlong)round_mask, T_LONG);
1378 __ logical_and(length, round_mask_opr, length);
1379 }
1380 __ move(length, result_reg);
1381 #else
1382 __ add(length_int, header_size, length_int);
1383 if (round_mask != 0) {
1384 LIR_Opr round_mask_opr = load_immediate(~round_mask, T_INT);
1385 __ logical_and(length_int, round_mask_opr, length_int);
1386 }
1387 __ convert(Bytecodes::_i2l, length_int, result_reg);
1388 #endif
1389
1390 __ branch_destination(L_done->label());
1391 }
1392
1393 void LIRGenerator::do_scopedValueCache(Intrinsic* x) {
1394 do_JavaThreadField(x, JavaThread::scopedValueCache_offset());
1395 }
1396
1397 // Example: Thread.currentCarrierThread()
1398 void LIRGenerator::do_currentCarrierThread(Intrinsic* x) {
1399 do_JavaThreadField(x, JavaThread::threadObj_offset());
1400 }
1401
1402 void LIRGenerator::do_vthread(Intrinsic* x) {
1403 do_JavaThreadField(x, JavaThread::vthread_offset());
1404 }
1405
1406 void LIRGenerator::do_JavaThreadField(Intrinsic* x, ByteSize offset) {
1407 assert(x->number_of_arguments() == 0, "wrong type");
1408 LIR_Opr temp = new_register(T_ADDRESS);
1409 LIR_Opr reg = rlock_result(x);
1410 __ move(new LIR_Address(getThreadPointer(), in_bytes(offset), T_ADDRESS), temp);
1411 access_load(IN_NATIVE, T_OBJECT,
1412 LIR_OprFact::address(new LIR_Address(temp, T_OBJECT)), reg);
1413 }
1414
1415 void LIRGenerator::do_RegisterFinalizer(Intrinsic* x) {
1416 assert(x->number_of_arguments() == 1, "wrong type");
1417 LIRItem receiver(x->argument_at(0), this);
1418
1419 receiver.load_item();
1420 BasicTypeList signature;
1421 signature.append(T_OBJECT); // receiver
1422 LIR_OprList* args = new LIR_OprList();
1423 args->append(receiver.result());
1424 CodeEmitInfo* info = state_for(x, x->state());
1425 call_runtime(&signature, args,
1426 CAST_FROM_FN_PTR(address, Runtime1::entry_for(StubId::c1_register_finalizer_id)),
1427 voidType, info);
1428
1429 set_no_result(x);
1430 }
1431
1432
1433 //------------------------local access--------------------------------------
1434
1435 LIR_Opr LIRGenerator::operand_for_instruction(Instruction* x) {
1436 if (x->operand()->is_illegal()) {
1437 Constant* c = x->as_Constant();
1438 if (c != nullptr) {
1439 x->set_operand(LIR_OprFact::value_type(c->type()));
1440 } else {
1441 assert(x->as_Phi() || x->as_Local() != nullptr, "only for Phi and Local");
1442 // allocate a virtual register for this local or phi
1443 x->set_operand(rlock(x));
1444 #ifdef ASSERT
1445 _instruction_for_operand.at_put_grow(x->operand()->vreg_number(), x, nullptr);
1446 #endif
1447 }
1448 }
1449 return x->operand();
1450 }
1451
1452 #ifdef ASSERT
1453 Instruction* LIRGenerator::instruction_for_vreg(int reg_num) {
1454 if (reg_num < _instruction_for_operand.length()) {
1455 return _instruction_for_operand.at(reg_num);
1456 }
1457 return nullptr;
1458 }
1459 #endif
1460
1461 void LIRGenerator::set_vreg_flag(int vreg_num, VregFlag f) {
1462 if (_vreg_flags.size_in_bits() == 0) {
1463 BitMap2D temp(100, num_vreg_flags);
1464 _vreg_flags = temp;
1465 }
1466 _vreg_flags.at_put_grow(vreg_num, f, true);
1467 }
1468
1469 bool LIRGenerator::is_vreg_flag_set(int vreg_num, VregFlag f) {
1470 if (!_vreg_flags.is_valid_index(vreg_num, f)) {
1471 return false;
1472 }
1473 return _vreg_flags.at(vreg_num, f);
1474 }
1475
1476
1477 // Block local constant handling. This code is useful for keeping
1478 // unpinned constants and constants which aren't exposed in the IR in
1479 // registers. Unpinned Constant instructions have their operands
1480 // cleared when the block is finished so that other blocks can't end
1481 // up referring to their registers.
1482
1483 LIR_Opr LIRGenerator::load_constant(Constant* x) {
1484 assert(!x->is_pinned(), "only for unpinned constants");
1485 _unpinned_constants.append(x);
1486 return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());
1487 }
1488
1489
1490 LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {
1491 BasicType t = c->type();
1492 for (int i = 0; i < _constants.length() && !in_conditional_code(); i++) {
1493 LIR_Const* other = _constants.at(i);
1494 if (t == other->type()) {
1495 switch (t) {
1496 case T_INT:
1497 case T_FLOAT:
1498 if (c->as_jint_bits() != other->as_jint_bits()) continue;
1499 break;
1500 case T_LONG:
1501 case T_DOUBLE:
1502 if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;
1503 if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;
1504 break;
1505 case T_OBJECT:
1506 if (c->as_jobject() != other->as_jobject()) continue;
1507 break;
1508 default:
1509 break;
1510 }
1511 return _reg_for_constants.at(i);
1512 }
1513 }
1514
1515 LIR_Opr result = new_register(t);
1516 __ move((LIR_Opr)c, result);
1517 if (!in_conditional_code()) {
1518 _constants.append(c);
1519 _reg_for_constants.append(result);
1520 }
1521 return result;
1522 }
1523
1524 void LIRGenerator::set_in_conditional_code(bool v) {
1525 assert(v != _in_conditional_code, "must change state");
1526 _in_conditional_code = v;
1527 }
1528
1529
1530 //------------------------field access--------------------------------------
1531
1532 void LIRGenerator::do_CompareAndSwap(Intrinsic* x, ValueType* type) {
1533 assert(x->number_of_arguments() == 4, "wrong type");
1534 LIRItem obj (x->argument_at(0), this); // object
1535 LIRItem offset(x->argument_at(1), this); // offset of field
1536 LIRItem cmp (x->argument_at(2), this); // value to compare with field
1537 LIRItem val (x->argument_at(3), this); // replace field with val if matches cmp
1538 assert(obj.type()->tag() == objectTag, "invalid type");
1539 assert(cmp.type()->tag() == type->tag(), "invalid type");
1540 assert(val.type()->tag() == type->tag(), "invalid type");
1541
1542 LIR_Opr result = access_atomic_cmpxchg_at(IN_HEAP, as_BasicType(type),
1543 obj, offset, cmp, val);
1544 set_result(x, result);
1545 }
1546
1547 // Returns an int/long value with the null marker bit set.
1548 static LIR_Opr null_marker_mask(BasicType bt, int nm_offset) {
1549 assert(nm_offset >= 0, "field does not have null marker");
1550 jlong null_marker = 1ULL << (nm_offset << LogBitsPerByte);
1551 return (bt == T_LONG) ? LIR_OprFact::longConst(null_marker) : LIR_OprFact::intConst(null_marker);
1552 }
1553
1554 static LIR_Opr null_marker_mask(BasicType bt, ciField* field) {
1555 assert(field->null_marker_offset() != -1, "field does not have null marker");
1556 return null_marker_mask(bt, field->null_marker_offset() - field->offset_in_bytes());
1557 }
1558
1559 // Comment copied form templateTable_i486.cpp
1560 // ----------------------------------------------------------------------------
1561 // Volatile variables demand their effects be made known to all CPU's in
1562 // order. Store buffers on most chips allow reads & writes to reorder; the
1563 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1564 // memory barrier (i.e., it's not sufficient that the interpreter does not
1565 // reorder volatile references, the hardware also must not reorder them).
1566 //
1567 // According to the new Java Memory Model (JMM):
1568 // (1) All volatiles are serialized wrt to each other.
1569 // ALSO reads & writes act as acquire & release, so:
1570 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1571 // the read float up to before the read. It's OK for non-volatile memory refs
1572 // that happen before the volatile read to float down below it.
1573 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1574 // that happen BEFORE the write float down to after the write. It's OK for
1575 // non-volatile memory refs that happen after the volatile write to float up
1576 // before it.
1577 //
1578 // We only put in barriers around volatile refs (they are expensive), not
1579 // _between_ memory refs (that would require us to track the flavor of the
1580 // previous memory refs). Requirements (2) and (3) require some barriers
1581 // before volatile stores and after volatile loads. These nearly cover
1582 // requirement (1) but miss the volatile-store-volatile-load case. This final
1583 // case is placed after volatile-stores although it could just as well go
1584 // before volatile-loads.
1585
1586
1587 void LIRGenerator::do_StoreField(StoreField* x) {
1588 ciField* field = x->field();
1589 bool needs_patching = x->needs_patching();
1590 bool is_volatile = field->is_volatile();
1591 BasicType field_type = x->field_type();
1592
1593 CodeEmitInfo* info = nullptr;
1594 if (needs_patching) {
1595 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
1596 info = state_for(x, x->state_before());
1597 } else if (x->needs_null_check()) {
1598 NullCheck* nc = x->explicit_null_check();
1599 if (nc == nullptr) {
1600 info = state_for(x);
1601 } else {
1602 info = state_for(nc);
1603 }
1604 }
1605
1606 LIRItem object(x->obj(), this);
1607 LIRItem value(x->value(), this);
1608
1609 object.load_item();
1610
1611 if (field->is_flat()) {
1612 value.load_item();
1613 } else {
1614 if (is_volatile || needs_patching) {
1615 // load item if field is volatile (fewer special cases for volatiles)
1616 // load item if field not initialized
1617 // load item if field not constant
1618 // because of code patching we cannot inline constants
1619 if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1620 value.load_byte_item();
1621 } else {
1622 value.load_item();
1623 }
1624 } else {
1625 value.load_for_store(field_type);
1626 }
1627 }
1628
1629 set_no_result(x);
1630
1631 #ifndef PRODUCT
1632 if (PrintNotLoaded && needs_patching) {
1633 tty->print_cr(" ###class not loaded at store_%s bci %d",
1634 x->is_static() ? "static" : "field", x->printable_bci());
1635 }
1636 #endif
1637
1638 if (x->needs_null_check() &&
1639 (needs_patching ||
1640 MacroAssembler::needs_explicit_null_check(x->offset()))) {
1641 // Emit an explicit null check because the offset is too large.
1642 // If the class is not loaded and the object is null, we need to deoptimize to throw a
1643 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
1644 __ null_check(object.result(), new CodeEmitInfo(info), /* deoptimize */ needs_patching);
1645 }
1646
1647 DecoratorSet decorators = IN_HEAP;
1648 if (is_volatile) {
1649 decorators |= MO_SEQ_CST;
1650 }
1651 if (needs_patching) {
1652 decorators |= C1_NEEDS_PATCHING;
1653 }
1654
1655 if (field->is_flat()) {
1656 ciInlineKlass* vk = field->type()->as_inline_klass();
1657
1658 #ifdef ASSERT
1659 assert(field->is_atomic(), "No atomic access required %s.%s", field->holder()->name()->as_utf8(), field->name()->as_utf8());
1660 // ZGC does not support compressed oops, so only one oop can be in the payload which is written by a "normal" oop store.
1661 assert(!vk->contains_oops() || !UseZGC, "ZGC does not support embedded oops in flat fields");
1662 #endif
1663
1664 // Zero the payload
1665 BasicType bt = vk->atomic_size_to_basic_type(field->is_null_free());
1666 LIR_Opr payload = new_register((bt == T_LONG) ? bt : T_INT);
1667 LIR_Opr zero = (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0);
1668 __ move(zero, payload);
1669
1670 bool is_constant_null = value.is_constant() && value.value()->is_null_obj();
1671 if (!is_constant_null) {
1672 LabelObj* L_isNull = new LabelObj();
1673 bool needs_null_check = !value.is_constant();
1674 if (needs_null_check) {
1675 __ cmp(lir_cond_equal, value.result(), LIR_OprFact::oopConst(nullptr));
1676 __ branch(lir_cond_equal, L_isNull->label());
1677 }
1678 // Load payload (if not empty) and set null marker (if not null-free)
1679 if (!vk->is_empty()) {
1680 access_load_at(decorators, bt, value, LIR_OprFact::intConst(vk->payload_offset()), payload);
1681 }
1682 if (!field->is_null_free()) {
1683 __ logical_or(payload, null_marker_mask(bt, field), payload);
1684 }
1685 if (needs_null_check) {
1686 __ branch_destination(L_isNull->label());
1687 }
1688 }
1689 access_store_at(decorators, bt, object, LIR_OprFact::intConst(x->offset()), payload,
1690 // Make sure to emit an implicit null check and pass the information
1691 // that this is a flat store that might require gc barriers for oop fields.
1692 info != nullptr ? new CodeEmitInfo(info) : nullptr, info, vk);
1693 return;
1694 }
1695
1696 access_store_at(decorators, field_type, object, LIR_OprFact::intConst(x->offset()),
1697 value.result(), info != nullptr ? new CodeEmitInfo(info) : nullptr, info);
1698 }
1699
1700 // Wrap an already computed address register as a C1 Instruction so it
1701 // can be passed as LIRItem into access_load_at() / access_store_at().
1702 class ComputedAddressValue: public Instruction {
1703 public:
1704 ComputedAddressValue(ValueType* type, LIR_Opr addr) : Instruction(type) {
1705 set_operand(addr);
1706 }
1707 virtual void input_values_do(ValueVisitor*) {}
1708 virtual void visit(InstructionVisitor* v) {}
1709 virtual const char* name() const { return "ComputedAddressValue"; }
1710 };
1711
1712 LIR_Opr LIRGenerator::get_and_load_element_address(LIRItem& array, LIRItem& index) {
1713 #ifndef _LP64
1714 // We need to be careful with overflows in 32-bit arithmetic
1715 Unimplemented();
1716 #endif
1717 ciType* array_type = array.value()->declared_type();
1718 ciFlatArrayKlass* flat_array_klass = array_type->as_flat_array_klass();
1719 assert(flat_array_klass->is_loaded(), "must be");
1720
1721 int array_header_size = flat_array_klass->array_header_in_bytes();
1722 int shift = flat_array_klass->log2_element_size();
1723
1724 LIR_Opr index_op = new_register(T_LONG);
1725 if (index.result()->is_constant()) {
1726 jint const_index = index.result()->as_jint();
1727 __ move(LIR_OprFact::longConst(static_cast<jlong>(const_index) << shift), index_op);
1728 } else {
1729 __ convert(Bytecodes::_i2l, index.result(), index_op);
1730 // Need to shift manually, as LIR_Address can scale only up to 3.
1731 __ shift_left(index_op, shift, index_op);
1732 }
1733
1734 LIR_Opr elm_op = new_pointer_register();
1735 LIR_Address* elm_address = generate_address(array.result(), index_op, 0, array_header_size, T_ADDRESS);
1736 __ leal(LIR_OprFact::address(elm_address), elm_op);
1737 return elm_op;
1738 }
1739
1740 void LIRGenerator::access_sub_element(LIRItem& array, LIRItem& index, LIR_Opr& result, ciField* field, size_t sub_offset) {
1741 assert(field != nullptr, "Need a subelement type specified");
1742
1743 // Find the starting address of the source (inside the array)
1744 LIR_Opr elm_op = get_and_load_element_address(array, index);
1745
1746 BasicType subelt_type = field->type()->basic_type();
1747 ComputedAddressValue* elm_resolved_addr = new ComputedAddressValue(as_ValueType(subelt_type), elm_op);
1748 LIRItem elm_item(elm_resolved_addr, this);
1749
1750 DecoratorSet decorators = IN_HEAP;
1751 access_load_at(decorators, subelt_type,
1752 elm_item, LIR_OprFact::longConst(sub_offset), result,
1753 nullptr, nullptr);
1754 }
1755
1756 LIR_Opr LIRGenerator::access_flat_array(bool is_load, LIRItem& array, LIRItem& index, LIRItem& obj_item,
1757 ciField* field, size_t sub_offset) {
1758 assert(sub_offset == 0 || field != nullptr, "Sanity check");
1759
1760 // Find the starting address of the source (inside the array)
1761 LIR_Opr elm_op = get_and_load_element_address(array, index);
1762
1763 ciFlatArrayKlass* array_klass = array.value()->declared_type()->as_flat_array_klass();
1764 ciInlineKlass* elem_klass = nullptr;
1765 if (field != nullptr) {
1766 elem_klass = field->type()->as_inline_klass();
1767 } else {
1768 elem_klass = array_klass->element_klass()->as_inline_klass();
1769 }
1770
1771 bool null_free = array_klass->is_elem_null_free();
1772 bool atomic = array_klass->is_elem_atomic();
1773 assert(null_free || atomic, "nullable flat arrays must use an atomic layout");
1774 if (atomic) {
1775 assert(field == nullptr && sub_offset == 0, "delayed sub-element access is only supported for non-atomic arrays");
1776 BasicType bt = elem_klass->atomic_size_to_basic_type(null_free);
1777 LIR_Opr payload = new_register((bt == T_LONG) ? bt : T_INT);
1778 ComputedAddressValue* elm_resolved_addr = new ComputedAddressValue(as_ValueType(bt), elm_op);
1779 LIRItem elm_item(elm_resolved_addr, this);
1780 DecoratorSet decorators = IN_HEAP;
1781 if (is_load) {
1782 access_load_at(decorators, bt, elm_item, LIR_OprFact::intConst(0), payload, nullptr, nullptr);
1783 access_store_at(decorators, bt, obj_item, LIR_OprFact::intConst(elem_klass->payload_offset()), payload,
1784 nullptr, nullptr, elem_klass);
1785 // Null check is performed in the caller
1786 } else {
1787 // Zero the payload
1788 LIR_Opr zero = (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0);
1789 __ move(zero, payload);
1790
1791 if (null_free) {
1792 if (!elem_klass->is_empty()) {
1793 access_load_at(decorators, bt, obj_item, LIR_OprFact::intConst(elem_klass->payload_offset()), payload);
1794 }
1795 } else {
1796 bool is_constant_null = obj_item.is_constant() && obj_item.value()->is_null_obj();
1797 if (!is_constant_null) {
1798 LabelObj* L_isNull = new LabelObj();
1799 bool needs_null_check = !obj_item.is_constant();
1800 if (needs_null_check) {
1801 __ cmp(lir_cond_equal, obj_item.result(), LIR_OprFact::oopConst(nullptr));
1802 __ branch(lir_cond_equal, L_isNull->label());
1803 }
1804 // Load payload (if not empty) and set null marker.
1805 if (!elem_klass->is_empty()) {
1806 access_load_at(decorators, bt, obj_item, LIR_OprFact::intConst(elem_klass->payload_offset()), payload);
1807 }
1808 __ logical_or(payload, null_marker_mask(bt, elem_klass->null_marker_offset_in_payload()), payload);
1809 if (needs_null_check) {
1810 __ branch_destination(L_isNull->label());
1811 }
1812 }
1813 }
1814 access_store_at(decorators, bt, elm_item, LIR_OprFact::intConst(0), payload, nullptr, nullptr, elem_klass);
1815 }
1816 return payload;
1817 }
1818
1819 for (int i = 0; i < elem_klass->nof_nonstatic_fields(); i++) {
1820 ciField* inner_field = elem_klass->nonstatic_field_at(i);
1821 assert(!inner_field->is_flat(), "flat fields must have been expanded");
1822 int obj_offset = inner_field->offset_in_bytes();
1823 size_t elm_offset = obj_offset - elem_klass->payload_offset() + sub_offset; // object header is not stored in array.
1824 BasicType field_type = inner_field->type()->basic_type();
1825
1826 // Types which are smaller than int are still passed in an int register.
1827 BasicType reg_type = field_type;
1828 switch (reg_type) {
1829 case T_BYTE:
1830 case T_BOOLEAN:
1831 case T_SHORT:
1832 case T_CHAR:
1833 reg_type = T_INT;
1834 break;
1835 default:
1836 break;
1837 }
1838
1839 LIR_Opr temp = new_register(reg_type);
1840 ComputedAddressValue* elm_resolved_addr = new ComputedAddressValue(as_ValueType(field_type), elm_op);
1841 LIRItem elm_item(elm_resolved_addr, this);
1842
1843 DecoratorSet decorators = IN_HEAP;
1844 if (is_load) {
1845 access_load_at(decorators, field_type,
1846 elm_item, LIR_OprFact::longConst(elm_offset), temp,
1847 nullptr, nullptr);
1848 access_store_at(decorators, field_type,
1849 obj_item, LIR_OprFact::intConst(obj_offset), temp,
1850 nullptr, nullptr);
1851 } else {
1852 access_load_at(decorators, field_type,
1853 obj_item, LIR_OprFact::intConst(obj_offset), temp,
1854 nullptr, nullptr);
1855 access_store_at(decorators, field_type,
1856 elm_item, LIR_OprFact::longConst(elm_offset), temp,
1857 nullptr, nullptr);
1858 }
1859 }
1860 return LIR_OprFact::illegalOpr;
1861 }
1862
1863 void LIRGenerator::check_flat_array(LIR_Opr array, CodeStub* slow_path) {
1864 LIR_Opr tmp = new_register(T_METADATA);
1865 __ check_flat_array(array, tmp, slow_path);
1866 }
1867
1868 void LIRGenerator::check_null_free_array(LIRItem& array, LIRItem& value, CodeEmitInfo* info) {
1869 LabelObj* L_end = new LabelObj();
1870 LIR_Opr tmp = new_register(T_METADATA);
1871 __ check_null_free_array(array.result(), tmp);
1872 #ifdef RISCV
1873 // tmp is used to hold the result of null free array check on riscv
1874 // See LIR_Assembler::emit_opNullFreeArrayCheck
1875 __ cmp(lir_cond_equal, tmp, LIR_OprFact::metadataConst(nullptr));
1876 #endif
1877 __ branch(lir_cond_equal, L_end->label());
1878 __ null_check(value.result(), info);
1879 __ branch_destination(L_end->label());
1880 }
1881
1882 bool LIRGenerator::needs_flat_array_store_check(StoreIndexed* x) {
1883 if (x->elt_type() == T_OBJECT && x->array()->maybe_flat_array()) {
1884 ciType* type = x->value()->declared_type();
1885 if (type != nullptr && type->is_klass()) {
1886 ciKlass* klass = type->as_klass();
1887 if (!klass->can_be_inline_klass() || (klass->is_inlinetype() && !klass->as_inline_klass()->maybe_flat_in_array())) {
1888 // This is known to be a non-flat object. If the array is a flat array,
1889 // it will be caught by the code generated by array_store_check().
1890 return false;
1891 }
1892 }
1893 // We're not 100% sure, so let's do the flat_array_store_check.
1894 return true;
1895 }
1896 return false;
1897 }
1898
1899 bool LIRGenerator::needs_null_free_array_store_check(StoreIndexed* x) {
1900 return x->elt_type() == T_OBJECT && x->array()->maybe_null_free_array();
1901 }
1902
1903 void LIRGenerator::do_StoreIndexed(StoreIndexed* x) {
1904 assert(x->is_pinned(),"");
1905 assert(x->elt_type() != T_ARRAY, "never used");
1906 bool is_loaded_flat_array = x->array()->is_loaded_flat_array();
1907 bool needs_range_check = x->compute_needs_range_check();
1908 bool use_length = x->length() != nullptr;
1909 bool obj_store = is_reference_type(x->elt_type());
1910 bool needs_store_check = obj_store && !(is_loaded_flat_array && x->is_exact_flat_array_store()) &&
1911 (x->value()->as_Constant() == nullptr ||
1912 !get_jobject_constant(x->value())->is_null_object());
1913
1914 LIRItem array(x->array(), this);
1915 LIRItem index(x->index(), this);
1916 LIRItem value(x->value(), this);
1917 LIRItem length(this);
1918
1919 array.load_item();
1920 index.load_nonconstant();
1921
1922 if (use_length && needs_range_check) {
1923 length.set_instruction(x->length());
1924 length.load_item();
1925 }
1926
1927 if (needs_store_check || x->check_boolean()
1928 || is_loaded_flat_array || needs_flat_array_store_check(x) || needs_null_free_array_store_check(x)) {
1929 value.load_item();
1930 } else {
1931 value.load_for_store(x->elt_type());
1932 }
1933
1934 set_no_result(x);
1935
1936 // the CodeEmitInfo must be duplicated for each different
1937 // LIR-instruction because spilling can occur anywhere between two
1938 // instructions and so the debug information must be different
1939 CodeEmitInfo* range_check_info = state_for(x);
1940 CodeEmitInfo* null_check_info = nullptr;
1941 if (x->needs_null_check()) {
1942 null_check_info = new CodeEmitInfo(range_check_info);
1943 }
1944
1945 if (needs_range_check) {
1946 if (use_length) {
1947 __ cmp(lir_cond_belowEqual, length.result(), index.result());
1948 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
1949 } else {
1950 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1951 // range_check also does the null check
1952 null_check_info = nullptr;
1953 }
1954 }
1955
1956 if (GenerateArrayStoreCheck && needs_store_check) {
1957 CodeEmitInfo* store_check_info = new CodeEmitInfo(range_check_info);
1958 array_store_check(value.result(), array.result(), store_check_info, x->profiled_method(), x->profiled_bci());
1959 }
1960
1961 if (x->should_profile()) {
1962 if (is_loaded_flat_array) {
1963 // No need to profile a store to a flat array of known type. This can happen if
1964 // the type only became known after optimizations (for example, after the PhiSimplifier).
1965 x->set_should_profile(false);
1966 } else {
1967 int bci = x->profiled_bci();
1968 ciMethodData* md = x->profiled_method()->method_data();
1969 assert(md != nullptr, "Sanity");
1970 ciProfileData* data = md->bci_to_data(bci);
1971 assert(data != nullptr && data->is_ArrayStoreData(), "incorrect profiling entry");
1972 ciArrayStoreData* store_data = (ciArrayStoreData*)data;
1973 profile_array_type(x, md, store_data);
1974 assert(store_data->is_ArrayStoreData(), "incorrect profiling entry");
1975 if (x->array()->maybe_null_free_array()) {
1976 profile_null_free_array(array, md, data);
1977 }
1978 }
1979 }
1980
1981 if (is_loaded_flat_array) {
1982 ciFlatArrayKlass* array_klass = x->array()->declared_type()->as_flat_array_klass();
1983 ciInlineKlass* elem_klass = array_klass->element_klass()->as_inline_klass();
1984 bool null_free = array_klass->is_elem_null_free();
1985 if (null_free && !x->value()->is_null_free()) {
1986 __ null_check(value.result(), new CodeEmitInfo(range_check_info));
1987 }
1988 // If array element is an empty null-free inline type, no need to copy anything.
1989 // Nullable empty arrays still need their null marker updated.
1990 if (!elem_klass->is_empty() || !null_free) {
1991 access_flat_array(false, array, index, value);
1992 }
1993 } else {
1994 StoreFlattenedArrayStub* slow_path = nullptr;
1995
1996 if (needs_flat_array_store_check(x)) {
1997 // Check if we indeed have a flat array
1998 index.load_item();
1999 slow_path = new StoreFlattenedArrayStub(array.result(), index.result(), value.result(), state_for(x, x->state_before()));
2000 check_flat_array(array.result(), slow_path);
2001 set_in_conditional_code(true);
2002 }
2003
2004 if (needs_null_free_array_store_check(x)) {
2005 CodeEmitInfo* info = new CodeEmitInfo(range_check_info);
2006 check_null_free_array(array, value, info);
2007 }
2008
2009 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
2010 if (x->check_boolean()) {
2011 decorators |= C1_MASK_BOOLEAN;
2012 }
2013
2014 access_store_at(decorators, x->elt_type(), array, index.result(), value.result(), nullptr, null_check_info);
2015 if (slow_path != nullptr) {
2016 __ branch_destination(slow_path->continuation());
2017 set_in_conditional_code(false);
2018 }
2019 }
2020 }
2021
2022 void LIRGenerator::access_load_at(DecoratorSet decorators, BasicType type,
2023 LIRItem& base, LIR_Opr offset, LIR_Opr result,
2024 CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) {
2025 decorators |= ACCESS_READ;
2026 LIRAccess access(this, decorators, base, offset, type, patch_info, load_emit_info);
2027 if (access.is_raw()) {
2028 _barrier_set->BarrierSetC1::load_at(access, result);
2029 } else {
2030 _barrier_set->load_at(access, result);
2031 }
2032 }
2033
2034 void LIRGenerator::access_load(DecoratorSet decorators, BasicType type,
2035 LIR_Opr addr, LIR_Opr result) {
2036 decorators |= ACCESS_READ;
2037 LIRAccess access(this, decorators, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, type);
2038 access.set_resolved_addr(addr);
2039 if (access.is_raw()) {
2040 _barrier_set->BarrierSetC1::load(access, result);
2041 } else {
2042 _barrier_set->load(access, result);
2043 }
2044 }
2045
2046 void LIRGenerator::access_store_at(DecoratorSet decorators, BasicType type,
2047 LIRItem& base, LIR_Opr offset, LIR_Opr value,
2048 CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info,
2049 ciInlineKlass* vk) {
2050 decorators |= ACCESS_WRITE;
2051 LIRAccess access(this, decorators, base, offset, type, patch_info, store_emit_info, vk);
2052 if (access.is_raw()) {
2053 _barrier_set->BarrierSetC1::store_at(access, value);
2054 } else {
2055 _barrier_set->store_at(access, value);
2056 }
2057 }
2058
2059 LIR_Opr LIRGenerator::access_atomic_cmpxchg_at(DecoratorSet decorators, BasicType type,
2060 LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) {
2061 decorators |= ACCESS_READ;
2062 decorators |= ACCESS_WRITE;
2063 // Atomic operations are SEQ_CST by default
2064 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
2065 LIRAccess access(this, decorators, base, offset, type);
2066 if (access.is_raw()) {
2067 return _barrier_set->BarrierSetC1::atomic_cmpxchg_at(access, cmp_value, new_value);
2068 } else {
2069 return _barrier_set->atomic_cmpxchg_at(access, cmp_value, new_value);
2070 }
2071 }
2072
2073 LIR_Opr LIRGenerator::access_atomic_xchg_at(DecoratorSet decorators, BasicType type,
2074 LIRItem& base, LIRItem& offset, LIRItem& value) {
2075 decorators |= ACCESS_READ;
2076 decorators |= ACCESS_WRITE;
2077 // Atomic operations are SEQ_CST by default
2078 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
2079 LIRAccess access(this, decorators, base, offset, type);
2080 if (access.is_raw()) {
2081 return _barrier_set->BarrierSetC1::atomic_xchg_at(access, value);
2082 } else {
2083 return _barrier_set->atomic_xchg_at(access, value);
2084 }
2085 }
2086
2087 LIR_Opr LIRGenerator::access_atomic_add_at(DecoratorSet decorators, BasicType type,
2088 LIRItem& base, LIRItem& offset, LIRItem& value) {
2089 decorators |= ACCESS_READ;
2090 decorators |= ACCESS_WRITE;
2091 // Atomic operations are SEQ_CST by default
2092 decorators |= ((decorators & MO_DECORATOR_MASK) == 0) ? MO_SEQ_CST : 0;
2093 LIRAccess access(this, decorators, base, offset, type);
2094 if (access.is_raw()) {
2095 return _barrier_set->BarrierSetC1::atomic_add_at(access, value);
2096 } else {
2097 return _barrier_set->atomic_add_at(access, value);
2098 }
2099 }
2100
2101 void LIRGenerator::do_LoadField(LoadField* x) {
2102 ciField* field = x->field();
2103 bool needs_patching = x->needs_patching();
2104 bool is_volatile = field->is_volatile();
2105 BasicType field_type = x->field_type();
2106
2107 CodeEmitInfo* info = nullptr;
2108 if (needs_patching) {
2109 assert(x->explicit_null_check() == nullptr, "can't fold null check into patching field access");
2110 info = state_for(x, x->state_before());
2111 } else if (x->needs_null_check()) {
2112 NullCheck* nc = x->explicit_null_check();
2113 if (nc == nullptr) {
2114 info = state_for(x);
2115 } else {
2116 info = state_for(nc);
2117 }
2118 }
2119
2120 LIRItem object(x->obj(), this);
2121
2122 object.load_item();
2123
2124 #ifndef PRODUCT
2125 if (PrintNotLoaded && needs_patching) {
2126 tty->print_cr(" ###class not loaded at load_%s bci %d",
2127 x->is_static() ? "static" : "field", x->printable_bci());
2128 }
2129 #endif
2130
2131 bool stress_deopt = StressLoopInvariantCodeMotion && info && info->deoptimize_on_exception();
2132 if (x->needs_null_check() &&
2133 (needs_patching ||
2134 MacroAssembler::needs_explicit_null_check(x->offset()) ||
2135 stress_deopt)) {
2136 LIR_Opr obj = object.result();
2137 if (stress_deopt) {
2138 obj = new_register(T_OBJECT);
2139 __ move(LIR_OprFact::oopConst(nullptr), obj);
2140 }
2141 // Emit an explicit null check because the offset is too large.
2142 // If the class is not loaded and the object is null, we need to deoptimize to throw a
2143 // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
2144 __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);
2145 }
2146
2147 DecoratorSet decorators = IN_HEAP;
2148 if (is_volatile) {
2149 decorators |= MO_SEQ_CST;
2150 }
2151 if (needs_patching) {
2152 decorators |= C1_NEEDS_PATCHING;
2153 }
2154
2155 if (field->is_flat()) {
2156 ciInlineKlass* vk = field->type()->as_inline_klass();
2157 #ifdef ASSERT
2158 assert(field->is_atomic(), "No atomic access required");
2159 assert(x->state_before() != nullptr, "Needs state before");
2160 #endif
2161
2162 // Allocate buffer (we can't easily do this conditionally on the null check below
2163 // because branches added in the LIR are opaque to the register allocator).
2164 NewInstance* buffer = new NewInstance(vk, x->state_before(), false, true);
2165 do_NewInstance(buffer);
2166 LIRItem dest(buffer, this);
2167
2168 // Copy the payload to the buffer
2169 BasicType bt = vk->atomic_size_to_basic_type(field->is_null_free());
2170 LIR_Opr payload = new_register((bt == T_LONG) ? bt : T_INT);
2171 access_load_at(decorators, bt, object, LIR_OprFact::intConst(field->offset_in_bytes()), payload,
2172 // Make sure to emit an implicit null check
2173 info ? new CodeEmitInfo(info) : nullptr, info);
2174 access_store_at(decorators, bt, dest, LIR_OprFact::intConst(vk->payload_offset()), payload);
2175
2176 if (field->is_null_free()) {
2177 set_result(x, buffer->operand());
2178 } else {
2179 // Check the null marker and set result to null if it's not set
2180 __ logical_and(payload, null_marker_mask(bt, field), payload);
2181 __ cmp(lir_cond_equal, payload, (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0));
2182 __ cmove(lir_cond_equal, LIR_OprFact::oopConst(nullptr), buffer->operand(), rlock_result(x), T_OBJECT);
2183 }
2184
2185 // Ensure the copy is visible before any subsequent store that publishes the buffer.
2186 __ membar_storestore();
2187 return;
2188 }
2189
2190 LIR_Opr result = rlock_result(x, field_type);
2191 access_load_at(decorators, field_type,
2192 object, LIR_OprFact::intConst(x->offset()), result,
2193 info ? new CodeEmitInfo(info) : nullptr, info);
2194 }
2195
2196 // int/long jdk.internal.util.Preconditions.checkIndex
2197 void LIRGenerator::do_PreconditionsCheckIndex(Intrinsic* x, BasicType type) {
2198 assert(x->number_of_arguments() == 3, "wrong type");
2199 LIRItem index(x->argument_at(0), this);
2200 LIRItem length(x->argument_at(1), this);
2201 LIRItem oobef(x->argument_at(2), this);
2202
2203 index.load_item();
2204 length.load_item();
2205 oobef.load_item();
2206
2207 LIR_Opr result = rlock_result(x);
2208 // x->state() is created from copy_state_for_exception, it does not contains arguments
2209 // we should prepare them before entering into interpreter mode due to deoptimization.
2210 ValueStack* state = x->state();
2211 for (int i = 0; i < x->number_of_arguments(); i++) {
2212 Value arg = x->argument_at(i);
2213 state->push(arg->type(), arg);
2214 }
2215 CodeEmitInfo* info = state_for(x, state);
2216
2217 LIR_Opr len = length.result();
2218 LIR_Opr zero;
2219 if (type == T_INT) {
2220 zero = LIR_OprFact::intConst(0);
2221 if (length.result()->is_constant()){
2222 len = LIR_OprFact::intConst(length.result()->as_jint());
2223 }
2224 } else {
2225 assert(type == T_LONG, "sanity check");
2226 zero = LIR_OprFact::longConst(0);
2227 if (length.result()->is_constant()){
2228 len = LIR_OprFact::longConst(length.result()->as_jlong());
2229 }
2230 }
2231 // C1 can not handle the case that comparing index with constant value while condition
2232 // is neither lir_cond_equal nor lir_cond_notEqual, see LIR_Assembler::comp_op.
2233 LIR_Opr zero_reg = new_register(type);
2234 __ move(zero, zero_reg);
2235 #if defined(X86) && !defined(_LP64)
2236 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy.
2237 LIR_Opr index_copy = new_register(index.type());
2238 // index >= 0
2239 __ move(index.result(), index_copy);
2240 __ cmp(lir_cond_less, index_copy, zero_reg);
2241 __ branch(lir_cond_less, new DeoptimizeStub(info, Deoptimization::Reason_range_check,
2242 Deoptimization::Action_make_not_entrant));
2243 // index < length
2244 __ move(index.result(), index_copy);
2245 __ cmp(lir_cond_greaterEqual, index_copy, len);
2246 __ branch(lir_cond_greaterEqual, new DeoptimizeStub(info, Deoptimization::Reason_range_check,
2247 Deoptimization::Action_make_not_entrant));
2248 #else
2249 // index >= 0
2250 __ cmp(lir_cond_less, index.result(), zero_reg);
2251 __ branch(lir_cond_less, new DeoptimizeStub(info, Deoptimization::Reason_range_check,
2252 Deoptimization::Action_make_not_entrant));
2253 // index < length
2254 __ cmp(lir_cond_greaterEqual, index.result(), len);
2255 __ branch(lir_cond_greaterEqual, new DeoptimizeStub(info, Deoptimization::Reason_range_check,
2256 Deoptimization::Action_make_not_entrant));
2257 #endif
2258 __ move(index.result(), result);
2259 }
2260
2261 //------------------------array access--------------------------------------
2262
2263
2264 void LIRGenerator::do_ArrayLength(ArrayLength* x) {
2265 LIRItem array(x->array(), this);
2266 array.load_item();
2267 LIR_Opr reg = rlock_result(x);
2268
2269 CodeEmitInfo* info = nullptr;
2270 if (x->needs_null_check()) {
2271 NullCheck* nc = x->explicit_null_check();
2272 if (nc == nullptr) {
2273 info = state_for(x);
2274 } else {
2275 info = state_for(nc);
2276 }
2277 if (StressLoopInvariantCodeMotion && info->deoptimize_on_exception()) {
2278 LIR_Opr obj = new_register(T_OBJECT);
2279 __ move(LIR_OprFact::oopConst(nullptr), obj);
2280 __ null_check(obj, new CodeEmitInfo(info));
2281 }
2282 }
2283 __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);
2284 }
2285
2286
2287 void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {
2288 bool use_length = x->length() != nullptr;
2289 LIRItem array(x->array(), this);
2290 LIRItem index(x->index(), this);
2291 LIRItem length(this);
2292 bool needs_range_check = x->compute_needs_range_check();
2293
2294 if (use_length && needs_range_check) {
2295 length.set_instruction(x->length());
2296 length.load_item();
2297 }
2298
2299 array.load_item();
2300 if (index.is_constant() && can_inline_as_constant(x->index())) {
2301 // let it be a constant
2302 index.dont_load_item();
2303 } else {
2304 index.load_item();
2305 }
2306
2307 CodeEmitInfo* range_check_info = state_for(x);
2308 CodeEmitInfo* null_check_info = nullptr;
2309 if (x->needs_null_check()) {
2310 NullCheck* nc = x->explicit_null_check();
2311 if (nc != nullptr) {
2312 null_check_info = state_for(nc);
2313 } else {
2314 null_check_info = range_check_info;
2315 }
2316 if (StressLoopInvariantCodeMotion && null_check_info->deoptimize_on_exception()) {
2317 LIR_Opr obj = new_register(T_OBJECT);
2318 __ move(LIR_OprFact::oopConst(nullptr), obj);
2319 __ null_check(obj, new CodeEmitInfo(null_check_info));
2320 }
2321 }
2322
2323 if (needs_range_check) {
2324 if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) {
2325 __ branch(lir_cond_always, new RangeCheckStub(range_check_info, index.result(), array.result()));
2326 } else if (use_length) {
2327 // TODO: use a (modified) version of array_range_check that does not require a
2328 // constant length to be loaded to a register
2329 __ cmp(lir_cond_belowEqual, length.result(), index.result());
2330 __ branch(lir_cond_belowEqual, new RangeCheckStub(range_check_info, index.result(), array.result()));
2331 } else {
2332 array_range_check(array.result(), index.result(), null_check_info, range_check_info);
2333 // The range check performs the null check, so clear it out for the load
2334 null_check_info = nullptr;
2335 }
2336 }
2337
2338 ciMethodData* md = nullptr;
2339 ciProfileData* data = nullptr;
2340 if (x->should_profile()) {
2341 if (x->array()->is_loaded_flat_array()) {
2342 // No need to profile a load from a flat array of known type. This can happen if
2343 // the type only became known after optimizations (for example, after the PhiSimplifier).
2344 x->set_should_profile(false);
2345 } else {
2346 int bci = x->profiled_bci();
2347 md = x->profiled_method()->method_data();
2348 assert(md != nullptr, "Sanity");
2349 data = md->bci_to_data(bci);
2350 assert(data != nullptr && data->is_ArrayLoadData(), "incorrect profiling entry");
2351 ciArrayLoadData* load_data = (ciArrayLoadData*)data;
2352 profile_array_type(x, md, load_data);
2353 }
2354 }
2355
2356 Value element = nullptr;
2357 if (x->buffer() != nullptr) {
2358 assert(x->array()->is_loaded_flat_array(), "must be");
2359 // Find the destination address (of the NewInlineTypeInstance).
2360 LIRItem buffer(x->buffer(), this);
2361 LIR_Opr payload = access_flat_array(true, array, index, buffer,
2362 x->delayed() == nullptr ? nullptr : x->delayed()->field(),
2363 x->delayed() == nullptr ? 0 : x->delayed()->offset());
2364 ciFlatArrayKlass* array_klass = x->array()->declared_type()->as_flat_array_klass();
2365 if (array_klass->is_elem_null_free()) {
2366 set_result(x, x->buffer()->operand());
2367 } else {
2368 // Check the null marker and set result to null if it's not set
2369 ciInlineKlass* elem_klass = array_klass->element_klass()->as_inline_klass();
2370 BasicType bt = elem_klass->atomic_size_to_basic_type(false);
2371 assert(payload->is_valid(), "nullable flat array load must return the atomic payload");
2372 __ logical_and(payload, null_marker_mask(bt, elem_klass->null_marker_offset_in_payload()), payload);
2373 __ cmp(lir_cond_equal, payload, (bt == T_LONG) ? LIR_OprFact::longConst(0) : LIR_OprFact::intConst(0));
2374 __ cmove(lir_cond_equal, LIR_OprFact::oopConst(nullptr), buffer.result(), rlock_result(x), T_OBJECT);
2375 }
2376 } else if (x->delayed() != nullptr) {
2377 assert(x->array()->is_loaded_flat_array(), "must be");
2378 LIR_Opr result = rlock_result(x, x->delayed()->field()->type()->basic_type());
2379 access_sub_element(array, index, result, x->delayed()->field(), x->delayed()->offset());
2380 } else {
2381 LIR_Opr result = rlock_result(x, x->elt_type());
2382 LoadFlattenedArrayStub* slow_path = nullptr;
2383
2384 if (x->should_profile() && x->array()->maybe_null_free_array()) {
2385 profile_null_free_array(array, md, data);
2386 }
2387
2388 if (x->elt_type() == T_OBJECT && x->array()->maybe_flat_array()) {
2389 assert(x->delayed() == nullptr, "Delayed LoadIndexed only apply to loaded_flat_arrays");
2390 index.load_item();
2391 // if we are loading from a flat array, load it using a runtime call
2392 slow_path = new LoadFlattenedArrayStub(array.result(), index.result(), result, state_for(x, x->state_before()));
2393 check_flat_array(array.result(), slow_path);
2394 set_in_conditional_code(true);
2395 }
2396
2397 DecoratorSet decorators = IN_HEAP | IS_ARRAY;
2398 access_load_at(decorators, x->elt_type(),
2399 array, index.result(), result,
2400 nullptr, null_check_info);
2401
2402 if (slow_path != nullptr) {
2403 __ branch_destination(slow_path->continuation());
2404 set_in_conditional_code(false);
2405 }
2406
2407 element = x;
2408 }
2409
2410 if (x->should_profile()) {
2411 profile_element_type(element, md, (ciArrayLoadData*)data);
2412 }
2413 }
2414
2415
2416 void LIRGenerator::do_NullCheck(NullCheck* x) {
2417 if (x->can_trap()) {
2418 LIRItem value(x->obj(), this);
2419 value.load_item();
2420 CodeEmitInfo* info = state_for(x);
2421 __ null_check(value.result(), info);
2422 }
2423 }
2424
2425
2426 void LIRGenerator::do_TypeCast(TypeCast* x) {
2427 LIRItem value(x->obj(), this);
2428 value.load_item();
2429 // the result is the same as from the node we are casting
2430 set_result(x, value.result());
2431 }
2432
2433
2434 void LIRGenerator::do_Throw(Throw* x) {
2435 LIRItem exception(x->exception(), this);
2436 exception.load_item();
2437 set_no_result(x);
2438 LIR_Opr exception_opr = exception.result();
2439 CodeEmitInfo* info = state_for(x, x->state());
2440
2441 #ifndef PRODUCT
2442 if (PrintC1Statistics) {
2443 increment_counter(Runtime1::throw_count_address(), T_INT);
2444 }
2445 #endif
2446
2447 // check if the instruction has an xhandler in any of the nested scopes
2448 bool unwind = false;
2449 if (info->exception_handlers()->length() == 0) {
2450 // this throw is not inside an xhandler
2451 unwind = true;
2452 } else {
2453 // get some idea of the throw type
2454 bool type_is_exact = true;
2455 ciType* throw_type = x->exception()->exact_type();
2456 if (throw_type == nullptr) {
2457 type_is_exact = false;
2458 throw_type = x->exception()->declared_type();
2459 }
2460 if (throw_type != nullptr && throw_type->is_instance_klass()) {
2461 ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;
2462 unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);
2463 }
2464 }
2465
2466 // do null check before moving exception oop into fixed register
2467 // to avoid a fixed interval with an oop during the null check.
2468 // Use a copy of the CodeEmitInfo because debug information is
2469 // different for null_check and throw.
2470 if (x->exception()->as_NewInstance() == nullptr && x->exception()->as_ExceptionObject() == nullptr) {
2471 // if the exception object wasn't created using new then it might be null.
2472 __ null_check(exception_opr, new CodeEmitInfo(info, x->state()->copy(ValueStack::ExceptionState, x->state()->bci())));
2473 }
2474
2475 if (compilation()->env()->jvmti_can_post_on_exceptions()) {
2476 // we need to go through the exception lookup path to get JVMTI
2477 // notification done
2478 unwind = false;
2479 }
2480
2481 // move exception oop into fixed register
2482 __ move(exception_opr, exceptionOopOpr());
2483
2484 if (unwind) {
2485 __ unwind_exception(exceptionOopOpr());
2486 } else {
2487 __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);
2488 }
2489 }
2490
2491
2492 void LIRGenerator::do_UnsafeGet(UnsafeGet* x) {
2493 BasicType type = x->basic_type();
2494 LIRItem src(x->object(), this);
2495 LIRItem off(x->offset(), this);
2496
2497 off.load_item();
2498 src.load_item();
2499
2500 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS;
2501
2502 if (x->is_volatile()) {
2503 decorators |= MO_SEQ_CST;
2504 }
2505 if (type == T_BOOLEAN) {
2506 decorators |= C1_MASK_BOOLEAN;
2507 }
2508 if (is_reference_type(type)) {
2509 decorators |= ON_UNKNOWN_OOP_REF;
2510 }
2511
2512 LIR_Opr result = rlock_result(x, type);
2513 if (!x->is_raw()) {
2514 access_load_at(decorators, type, src, off.result(), result);
2515 } else {
2516 // Currently it is only used in GraphBuilder::setup_osr_entry_block.
2517 // It reads the value from [src + offset] directly.
2518 #ifdef _LP64
2519 LIR_Opr offset = new_register(T_LONG);
2520 __ convert(Bytecodes::_i2l, off.result(), offset);
2521 #else
2522 LIR_Opr offset = off.result();
2523 #endif
2524 LIR_Address* addr = new LIR_Address(src.result(), offset, type);
2525 if (is_reference_type(type)) {
2526 __ move_wide(addr, result);
2527 } else {
2528 __ move(addr, result);
2529 }
2530 }
2531 }
2532
2533
2534 void LIRGenerator::do_UnsafePut(UnsafePut* x) {
2535 BasicType type = x->basic_type();
2536 LIRItem src(x->object(), this);
2537 LIRItem off(x->offset(), this);
2538 LIRItem data(x->value(), this);
2539
2540 src.load_item();
2541 if (type == T_BOOLEAN || type == T_BYTE) {
2542 data.load_byte_item();
2543 } else {
2544 data.load_item();
2545 }
2546 off.load_item();
2547
2548 set_no_result(x);
2549
2550 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS;
2551 if (is_reference_type(type)) {
2552 decorators |= ON_UNKNOWN_OOP_REF;
2553 }
2554 if (x->is_volatile()) {
2555 decorators |= MO_SEQ_CST;
2556 }
2557 access_store_at(decorators, type, src, off.result(), data.result());
2558 }
2559
2560 void LIRGenerator::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {
2561 BasicType type = x->basic_type();
2562 LIRItem src(x->object(), this);
2563 LIRItem off(x->offset(), this);
2564 LIRItem value(x->value(), this);
2565
2566 DecoratorSet decorators = IN_HEAP | C1_UNSAFE_ACCESS | MO_SEQ_CST;
2567
2568 if (is_reference_type(type)) {
2569 decorators |= ON_UNKNOWN_OOP_REF;
2570 }
2571
2572 LIR_Opr result;
2573 if (x->is_add()) {
2574 result = access_atomic_add_at(decorators, type, src, off, value);
2575 } else {
2576 result = access_atomic_xchg_at(decorators, type, src, off, value);
2577 }
2578 set_result(x, result);
2579 }
2580
2581 void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {
2582 int lng = x->length();
2583
2584 for (int i = 0; i < lng; i++) {
2585 C1SwitchRange* one_range = x->at(i);
2586 int low_key = one_range->low_key();
2587 int high_key = one_range->high_key();
2588 BlockBegin* dest = one_range->sux();
2589 if (low_key == high_key) {
2590 __ cmp(lir_cond_equal, value, low_key);
2591 __ branch(lir_cond_equal, dest);
2592 } else if (high_key - low_key == 1) {
2593 __ cmp(lir_cond_equal, value, low_key);
2594 __ branch(lir_cond_equal, dest);
2595 __ cmp(lir_cond_equal, value, high_key);
2596 __ branch(lir_cond_equal, dest);
2597 } else {
2598 LabelObj* L = new LabelObj();
2599 __ cmp(lir_cond_less, value, low_key);
2600 __ branch(lir_cond_less, L->label());
2601 __ cmp(lir_cond_lessEqual, value, high_key);
2602 __ branch(lir_cond_lessEqual, dest);
2603 __ branch_destination(L->label());
2604 }
2605 }
2606 __ jump(default_sux);
2607 }
2608
2609
2610 SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {
2611 SwitchRangeList* res = new SwitchRangeList();
2612 int len = x->length();
2613 if (len > 0) {
2614 BlockBegin* sux = x->sux_at(0);
2615 int low = x->lo_key();
2616 BlockBegin* default_sux = x->default_sux();
2617 C1SwitchRange* range = new C1SwitchRange(low, sux);
2618 for (int i = 0; i < len; i++) {
2619 int key = low + i;
2620 BlockBegin* new_sux = x->sux_at(i);
2621 if (sux == new_sux) {
2622 // still in same range
2623 range->set_high_key(key);
2624 } else {
2625 // skip tests which explicitly dispatch to the default
2626 if (sux != default_sux) {
2627 res->append(range);
2628 }
2629 range = new C1SwitchRange(key, new_sux);
2630 }
2631 sux = new_sux;
2632 }
2633 if (res->length() == 0 || res->last() != range) res->append(range);
2634 }
2635 return res;
2636 }
2637
2638
2639 // we expect the keys to be sorted by increasing value
2640 SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {
2641 SwitchRangeList* res = new SwitchRangeList();
2642 int len = x->length();
2643 if (len > 0) {
2644 BlockBegin* default_sux = x->default_sux();
2645 int key = x->key_at(0);
2646 BlockBegin* sux = x->sux_at(0);
2647 C1SwitchRange* range = new C1SwitchRange(key, sux);
2648 for (int i = 1; i < len; i++) {
2649 int new_key = x->key_at(i);
2650 BlockBegin* new_sux = x->sux_at(i);
2651 if (key+1 == new_key && sux == new_sux) {
2652 // still in same range
2653 range->set_high_key(new_key);
2654 } else {
2655 // skip tests which explicitly dispatch to the default
2656 if (range->sux() != default_sux) {
2657 res->append(range);
2658 }
2659 range = new C1SwitchRange(new_key, new_sux);
2660 }
2661 key = new_key;
2662 sux = new_sux;
2663 }
2664 if (res->length() == 0 || res->last() != range) res->append(range);
2665 }
2666 return res;
2667 }
2668
2669
2670 void LIRGenerator::do_TableSwitch(TableSwitch* x) {
2671 LIRItem tag(x->tag(), this);
2672 tag.load_item();
2673 set_no_result(x);
2674
2675 if (x->is_safepoint()) {
2676 __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2677 }
2678
2679 // move values into phi locations
2680 move_to_phi(x->state());
2681
2682 int lo_key = x->lo_key();
2683 int len = x->length();
2684 assert(lo_key <= (lo_key + (len - 1)), "integer overflow");
2685 LIR_Opr value = tag.result();
2686
2687 if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) {
2688 ciMethod* method = x->state()->scope()->method();
2689 ciMethodData* md = method->method_data_or_null();
2690 assert(md != nullptr, "Sanity");
2691 ciProfileData* data = md->bci_to_data(x->state()->bci());
2692 assert(data != nullptr, "must have profiling data");
2693 assert(data->is_MultiBranchData(), "bad profile data?");
2694 int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset());
2695 LIR_Opr md_reg = new_register(T_METADATA);
2696 __ metadata2reg(md->constant_encoding(), md_reg);
2697 LIR_Opr data_offset_reg = new_pointer_register();
2698 LIR_Opr tmp_reg = new_pointer_register();
2699
2700 __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg);
2701 for (int i = 0; i < len; i++) {
2702 int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i));
2703 __ cmp(lir_cond_equal, value, i + lo_key);
2704 __ move(data_offset_reg, tmp_reg);
2705 __ cmove(lir_cond_equal,
2706 LIR_OprFact::intptrConst(count_offset),
2707 tmp_reg,
2708 data_offset_reg, T_INT);
2709 }
2710
2711 LIR_Opr data_reg = new_pointer_register();
2712 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
2713 __ move(data_addr, data_reg);
2714 __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg);
2715 __ move(data_reg, data_addr);
2716 }
2717
2718 if (UseTableRanges) {
2719 do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2720 } else {
2721 for (int i = 0; i < len; i++) {
2722 __ cmp(lir_cond_equal, value, i + lo_key);
2723 __ branch(lir_cond_equal, x->sux_at(i));
2724 }
2725 __ jump(x->default_sux());
2726 }
2727 }
2728
2729
2730 void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {
2731 LIRItem tag(x->tag(), this);
2732 tag.load_item();
2733 set_no_result(x);
2734
2735 if (x->is_safepoint()) {
2736 __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2737 }
2738
2739 // move values into phi locations
2740 move_to_phi(x->state());
2741
2742 LIR_Opr value = tag.result();
2743 int len = x->length();
2744
2745 if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) {
2746 ciMethod* method = x->state()->scope()->method();
2747 ciMethodData* md = method->method_data_or_null();
2748 assert(md != nullptr, "Sanity");
2749 ciProfileData* data = md->bci_to_data(x->state()->bci());
2750 assert(data != nullptr, "must have profiling data");
2751 assert(data->is_MultiBranchData(), "bad profile data?");
2752 int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset());
2753 LIR_Opr md_reg = new_register(T_METADATA);
2754 __ metadata2reg(md->constant_encoding(), md_reg);
2755 LIR_Opr data_offset_reg = new_pointer_register();
2756 LIR_Opr tmp_reg = new_pointer_register();
2757
2758 __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg);
2759 for (int i = 0; i < len; i++) {
2760 int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i));
2761 __ cmp(lir_cond_equal, value, x->key_at(i));
2762 __ move(data_offset_reg, tmp_reg);
2763 __ cmove(lir_cond_equal,
2764 LIR_OprFact::intptrConst(count_offset),
2765 tmp_reg,
2766 data_offset_reg, T_INT);
2767 }
2768
2769 LIR_Opr data_reg = new_pointer_register();
2770 LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
2771 __ move(data_addr, data_reg);
2772 __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg);
2773 __ move(data_reg, data_addr);
2774 }
2775
2776 if (UseTableRanges) {
2777 do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2778 } else {
2779 int len = x->length();
2780 for (int i = 0; i < len; i++) {
2781 __ cmp(lir_cond_equal, value, x->key_at(i));
2782 __ branch(lir_cond_equal, x->sux_at(i));
2783 }
2784 __ jump(x->default_sux());
2785 }
2786 }
2787
2788
2789 void LIRGenerator::do_Goto(Goto* x) {
2790 set_no_result(x);
2791
2792 if (block()->next()->as_OsrEntry()) {
2793 // need to free up storage used for OSR entry point
2794 LIR_Opr osrBuffer = block()->next()->operand();
2795 BasicTypeList signature;
2796 signature.append(NOT_LP64(T_INT) LP64_ONLY(T_LONG)); // pass a pointer to osrBuffer
2797 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2798 __ move(osrBuffer, cc->args()->at(0));
2799 __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
2800 getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());
2801 }
2802
2803 if (x->is_safepoint()) {
2804 ValueStack* state = x->state_before() ? x->state_before() : x->state();
2805
2806 // increment backedge counter if needed
2807 CodeEmitInfo* info = state_for(x, state);
2808 increment_backedge_counter(info, x->profiled_bci());
2809 CodeEmitInfo* safepoint_info = state_for(x, state);
2810 __ safepoint(safepoint_poll_register(), safepoint_info);
2811 }
2812
2813 // Gotos can be folded Ifs, handle this case.
2814 if (x->should_profile()) {
2815 ciMethod* method = x->profiled_method();
2816 assert(method != nullptr, "method should be set if branch is profiled");
2817 ciMethodData* md = method->method_data_or_null();
2818 assert(md != nullptr, "Sanity");
2819 ciProfileData* data = md->bci_to_data(x->profiled_bci());
2820 assert(data != nullptr, "must have profiling data");
2821 int offset;
2822 if (x->direction() == Goto::taken) {
2823 assert(data->is_BranchData(), "need BranchData for two-way branches");
2824 offset = md->byte_offset_of_slot(data, BranchData::taken_offset());
2825 } else if (x->direction() == Goto::not_taken) {
2826 assert(data->is_BranchData(), "need BranchData for two-way branches");
2827 offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
2828 } else {
2829 assert(data->is_JumpData(), "need JumpData for branches");
2830 offset = md->byte_offset_of_slot(data, JumpData::taken_offset());
2831 }
2832 LIR_Opr md_reg = new_register(T_METADATA);
2833 __ metadata2reg(md->constant_encoding(), md_reg);
2834
2835 increment_counter(new LIR_Address(md_reg, offset,
2836 NOT_LP64(T_INT) LP64_ONLY(T_LONG)), DataLayout::counter_increment);
2837 }
2838
2839 // emit phi-instruction move after safepoint since this simplifies
2840 // describing the state as the safepoint.
2841 move_to_phi(x->state());
2842
2843 __ jump(x->default_sux());
2844 }
2845
2846 /**
2847 * Emit profiling code if needed for arguments, parameters, return value types
2848 *
2849 * @param md MDO the code will update at runtime
2850 * @param md_base_offset common offset in the MDO for this profile and subsequent ones
2851 * @param md_offset offset in the MDO (on top of md_base_offset) for this profile
2852 * @param profiled_k current profile
2853 * @param obj IR node for the object to be profiled
2854 * @param mdp register to hold the pointer inside the MDO (md + md_base_offset).
2855 * Set once we find an update to make and use for next ones.
2856 * @param not_null true if we know obj cannot be null
2857 * @param signature_at_call_k signature at call for obj
2858 * @param callee_signature_k signature of callee for obj
2859 * at call and callee signatures differ at method handle call
2860 * @return the only klass we know will ever be seen at this profile point
2861 */
2862 ciKlass* LIRGenerator::profile_type(ciMethodData* md, int md_base_offset, int md_offset, intptr_t profiled_k,
2863 Value obj, LIR_Opr& mdp, bool not_null, ciKlass* signature_at_call_k,
2864 ciKlass* callee_signature_k) {
2865 ciKlass* result = nullptr;
2866 bool do_null = !not_null && !TypeEntries::was_null_seen(profiled_k);
2867 bool do_update = !TypeEntries::is_type_unknown(profiled_k);
2868 // known not to be null or null bit already set and already set to
2869 // unknown: nothing we can do to improve profiling
2870 if (!do_null && !do_update) {
2871 return result;
2872 }
2873
2874 ciKlass* exact_klass = nullptr;
2875 Compilation* comp = Compilation::current();
2876 if (do_update) {
2877 // try to find exact type, using CHA if possible, so that loading
2878 // the klass from the object can be avoided
2879 ciType* type = obj->exact_type();
2880 if (type == nullptr) {
2881 type = obj->declared_type();
2882 type = comp->cha_exact_type(type);
2883 }
2884 assert(type == nullptr || type->is_klass(), "type should be class");
2885 exact_klass = (type != nullptr && type->is_loaded()) ? (ciKlass*)type : nullptr;
2886
2887 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2888 }
2889
2890 if (!do_null && !do_update) {
2891 return result;
2892 }
2893
2894 ciKlass* exact_signature_k = nullptr;
2895 if (do_update && signature_at_call_k != nullptr) {
2896 // Is the type from the signature exact (the only one possible)?
2897 exact_signature_k = signature_at_call_k->exact_klass();
2898 if (exact_signature_k == nullptr) {
2899 exact_signature_k = comp->cha_exact_type(signature_at_call_k);
2900 } else {
2901 result = exact_signature_k;
2902 // Known statically. No need to emit any code: prevent
2903 // LIR_Assembler::emit_profile_type() from emitting useless code
2904 profiled_k = ciTypeEntries::with_status(result, profiled_k);
2905 }
2906 // exact_klass and exact_signature_k can be both non null but
2907 // different if exact_klass is loaded after the ciObject for
2908 // exact_signature_k is created.
2909 if (exact_klass == nullptr && exact_signature_k != nullptr && exact_klass != exact_signature_k) {
2910 // sometimes the type of the signature is better than the best type
2911 // the compiler has
2912 exact_klass = exact_signature_k;
2913 }
2914 if (callee_signature_k != nullptr &&
2915 callee_signature_k != signature_at_call_k) {
2916 ciKlass* improved_klass = callee_signature_k->exact_klass();
2917 if (improved_klass == nullptr) {
2918 improved_klass = comp->cha_exact_type(callee_signature_k);
2919 }
2920 if (exact_klass == nullptr && improved_klass != nullptr && exact_klass != improved_klass) {
2921 exact_klass = exact_signature_k;
2922 }
2923 }
2924 do_update = exact_klass == nullptr || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2925 }
2926
2927 if (exact_klass != nullptr && exact_klass->is_obj_array_klass()) {
2928 ciArrayKlass* exact_array_klass = exact_klass->as_array_klass();
2929 if (exact_array_klass->is_refined()) {
2930 do_update = ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2931 } else if (exact_klass->can_be_inline_array_klass()) {
2932 // Inline type arrays can have additional properties. Load the klass unless
2933 // the C1 type already carries refined array properties.
2934 exact_klass = nullptr;
2935 do_update = true;
2936 } else {
2937 // For a direct pointer comparison, we need the refined array klass pointer
2938 exact_klass = ciObjArrayKlass::make(exact_array_klass->element_klass());
2939 do_update = ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2940 }
2941 }
2942 if (!do_null && !do_update) {
2943 return result;
2944 }
2945
2946 if (mdp == LIR_OprFact::illegalOpr) {
2947 mdp = new_register(T_METADATA);
2948 __ metadata2reg(md->constant_encoding(), mdp);
2949 if (md_base_offset != 0) {
2950 LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS);
2951 mdp = new_pointer_register();
2952 __ leal(LIR_OprFact::address(base_type_address), mdp);
2953 }
2954 }
2955 LIRItem value(obj, this);
2956 value.load_item();
2957 __ profile_type(new LIR_Address(mdp, md_offset, T_METADATA),
2958 value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != nullptr);
2959 return result;
2960 }
2961
2962 // profile parameters on entry to the root of the compilation
2963 void LIRGenerator::profile_parameters(Base* x) {
2964 if (compilation()->profile_parameters()) {
2965 CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2966 ciMethodData* md = scope()->method()->method_data_or_null();
2967 assert(md != nullptr, "Sanity");
2968
2969 if (md->parameters_type_data() != nullptr) {
2970 ciParametersTypeData* parameters_type_data = md->parameters_type_data();
2971 ciTypeStackSlotEntries* parameters = parameters_type_data->parameters();
2972 LIR_Opr mdp = LIR_OprFact::illegalOpr;
2973 for (int java_index = 0, i = 0, j = 0; j < parameters_type_data->number_of_parameters(); i++) {
2974 LIR_Opr src = args->at(i);
2975 assert(!src->is_illegal(), "check");
2976 BasicType t = src->type();
2977 if (is_reference_type(t)) {
2978 intptr_t profiled_k = parameters->type(j);
2979 Local* local = x->state()->local_at(java_index)->as_Local();
2980 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
2981 in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)),
2982 profiled_k, local, mdp, false, local->declared_type()->as_klass(), nullptr);
2983 // If the profile is known statically set it once for all and do not emit any code
2984 if (exact != nullptr) {
2985 md->set_parameter_type(j, exact);
2986 }
2987 j++;
2988 }
2989 java_index += type2size[t];
2990 }
2991 }
2992 }
2993 }
2994
2995 void LIRGenerator::profile_flags(ciMethodData* md, ciProfileData* data, int flag, LIR_Condition condition) {
2996 assert(md != nullptr && data != nullptr, "should have been initialized");
2997 LIR_Opr mdp = new_register(T_METADATA);
2998 __ metadata2reg(md->constant_encoding(), mdp);
2999 LIR_Address* addr = new LIR_Address(mdp, md->byte_offset_of_slot(data, DataLayout::flags_offset()), T_BYTE);
3000 LIR_Opr flags = new_register(T_INT);
3001 __ move(addr, flags);
3002 LIR_Opr update;
3003 if (condition != lir_cond_always) {
3004 update = new_register(T_INT);
3005 __ cmove(condition, LIR_OprFact::intConst(0), LIR_OprFact::intConst(flag), update, T_INT);
3006 } else {
3007 update = LIR_OprFact::intConst(flag);
3008 }
3009 __ logical_or(flags, update, flags);
3010 __ store(flags, addr);
3011 }
3012
3013 void LIRGenerator::profile_null_free_array(LIRItem array, ciMethodData* md, ciProfileData* data) {
3014 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
3015 LabelObj* L_end = new LabelObj();
3016 LIR_Opr tmp = new_register(T_METADATA);
3017 __ check_null_free_array(array.result(), tmp);
3018 #ifdef RISCV
3019 // tmp is used to hold the result of null free array check on riscv
3020 // See LIR_Assembler::emit_opNullFreeArrayCheck
3021 __ cmp(lir_cond_equal, tmp, LIR_OprFact::metadataConst(nullptr));
3022 #endif
3023 profile_flags(md, data, ArrayStoreData::null_free_array_byte_constant(), lir_cond_equal);
3024 }
3025
3026 template <class ArrayData> void LIRGenerator::profile_array_type(AccessIndexed* x, ciMethodData*& md, ArrayData*& load_store) {
3027 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
3028 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3029 profile_type(md, md->byte_offset_of_slot(load_store, ArrayData::array_offset()), 0,
3030 load_store->array()->type(), x->array(), mdp, true, nullptr, nullptr);
3031 }
3032
3033 void LIRGenerator::profile_element_type(Value element, ciMethodData* md, ciArrayLoadData* load_data) {
3034 assert(compilation()->profile_array_accesses(), "array access profiling is disabled");
3035 assert(md != nullptr && load_data != nullptr, "should have been initialized");
3036 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3037 profile_type(md, md->byte_offset_of_slot(load_data, ArrayLoadData::element_offset()), 0,
3038 load_data->element()->type(), element, mdp, false, nullptr, nullptr);
3039 }
3040
3041 void LIRGenerator::do_Base(Base* x) {
3042 __ std_entry(LIR_OprFact::illegalOpr);
3043 // Emit moves from physical registers / stack slots to virtual registers
3044 CallingConvention* args = compilation()->frame_map()->incoming_arguments();
3045 IRScope* irScope = compilation()->hir()->top_scope();
3046 int java_index = 0;
3047 for (int i = 0; i < args->length(); i++) {
3048 LIR_Opr src = args->at(i);
3049 assert(!src->is_illegal(), "check");
3050 BasicType t = src->type();
3051
3052 // Types which are smaller than int are passed as int, so
3053 // correct the type which passed.
3054 switch (t) {
3055 case T_BYTE:
3056 case T_BOOLEAN:
3057 case T_SHORT:
3058 case T_CHAR:
3059 t = T_INT;
3060 break;
3061 default:
3062 break;
3063 }
3064
3065 LIR_Opr dest = new_register(t);
3066 __ move(src, dest);
3067
3068 // Assign new location to Local instruction for this local
3069 Local* local = x->state()->local_at(java_index)->as_Local();
3070 assert(local != nullptr, "Locals for incoming arguments must have been created");
3071 #ifndef __SOFTFP__
3072 // The java calling convention passes double as long and float as int.
3073 assert(as_ValueType(t)->tag() == local->type()->tag(), "check");
3074 #endif // __SOFTFP__
3075 local->set_operand(dest);
3076 #ifdef ASSERT
3077 _instruction_for_operand.at_put_grow(dest->vreg_number(), local, nullptr);
3078 #endif
3079 java_index += type2size[t];
3080 }
3081
3082 // Check if we need a membar at the beginning of the java.lang.Object
3083 // constructor to satisfy the memory model for strict fields.
3084 if (Arguments::is_valhalla_enabled() && method()->intrinsic_id() == vmIntrinsics::_Object_init) {
3085 __ membar_storestore();
3086 }
3087
3088 if (compilation()->env()->dtrace_method_probes()) {
3089 BasicTypeList signature;
3090 signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread
3091 signature.append(T_METADATA); // Method*
3092 LIR_OprList* args = new LIR_OprList();
3093 args->append(getThreadPointer());
3094 LIR_Opr meth = new_register(T_METADATA);
3095 __ metadata2reg(method()->constant_encoding(), meth);
3096 args->append(meth);
3097 call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, nullptr);
3098 }
3099
3100 if (method()->is_synchronized()) {
3101 LIR_Opr obj;
3102 if (method()->is_static()) {
3103 obj = new_register(T_OBJECT);
3104 __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);
3105 } else {
3106 Local* receiver = x->state()->local_at(0)->as_Local();
3107 assert(receiver != nullptr, "must already exist");
3108 obj = receiver->operand();
3109 }
3110 assert(obj->is_valid(), "must be valid");
3111
3112 if (method()->is_synchronized()) {
3113 LIR_Opr lock = syncLockOpr();
3114 __ load_stack_address_monitor(0, lock);
3115
3116 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, x->check_flag(Instruction::DeoptimizeOnException));
3117 CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
3118
3119 // receiver is guaranteed non-null so don't need CodeEmitInfo
3120 __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, nullptr);
3121 }
3122 }
3123 // increment invocation counters if needed
3124 if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.
3125 profile_parameters(x);
3126 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), nullptr, false);
3127 increment_invocation_counter(info);
3128 }
3129 if (method()->has_scalarized_args()) {
3130 // Check if deoptimization was triggered (i.e. orig_pc was set) while buffering scalarized inline type arguments
3131 // in the entry point (see comments in frame::deoptimize). If so, deoptimize only now that we have the right state.
3132 CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, 0), nullptr, false);
3133 CodeStub* deopt_stub = new DeoptimizeStub(info, Deoptimization::Reason_none, Deoptimization::Action_none);
3134 __ append(new LIR_Op0(lir_check_orig_pc));
3135 __ branch(lir_cond_notEqual, deopt_stub);
3136 }
3137
3138 // all blocks with a successor must end with an unconditional jump
3139 // to the successor even if they are consecutive
3140 __ jump(x->default_sux());
3141 }
3142
3143
3144 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
3145 // construct our frame and model the production of incoming pointer
3146 // to the OSR buffer.
3147 __ osr_entry(LIR_Assembler::osrBufferPointer());
3148 LIR_Opr result = rlock_result(x);
3149 __ move(LIR_Assembler::osrBufferPointer(), result);
3150 }
3151
3152 void LIRGenerator::invoke_load_one_argument(LIRItem* param, LIR_Opr loc) {
3153 if (loc->is_register()) {
3154 param->load_item_force(loc);
3155 } else {
3156 LIR_Address* addr = loc->as_address_ptr();
3157 param->load_for_store(addr->type());
3158 if (addr->type() == T_OBJECT) {
3159 __ move_wide(param->result(), addr);
3160 } else {
3161 __ move(param->result(), addr);
3162 }
3163 }
3164 }
3165
3166 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
3167 assert(args->length() == arg_list->length(),
3168 "args=%d, arg_list=%d", args->length(), arg_list->length());
3169 for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) {
3170 LIRItem* param = args->at(i);
3171 LIR_Opr loc = arg_list->at(i);
3172 invoke_load_one_argument(param, loc);
3173 }
3174
3175 if (x->has_receiver()) {
3176 LIRItem* receiver = args->at(0);
3177 LIR_Opr loc = arg_list->at(0);
3178 if (loc->is_register()) {
3179 receiver->load_item_force(loc);
3180 } else {
3181 assert(loc->is_address(), "just checking");
3182 receiver->load_for_store(T_OBJECT);
3183 __ move_wide(receiver->result(), loc->as_address_ptr());
3184 }
3185 }
3186 }
3187
3188
3189 // Visits all arguments, returns appropriate items without loading them
3190 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
3191 LIRItemList* argument_items = new LIRItemList();
3192 if (x->has_receiver()) {
3193 LIRItem* receiver = new LIRItem(x->receiver(), this);
3194 argument_items->append(receiver);
3195 }
3196 for (int i = 0; i < x->number_of_arguments(); i++) {
3197 LIRItem* param = new LIRItem(x->argument_at(i), this);
3198 argument_items->append(param);
3199 }
3200 return argument_items;
3201 }
3202
3203
3204 // The invoke with receiver has following phases:
3205 // a) traverse and load/lock receiver;
3206 // b) traverse all arguments -> item-array (invoke_visit_argument)
3207 // c) push receiver on stack
3208 // d) load each of the items and push on stack
3209 // e) unlock receiver
3210 // f) move receiver into receiver-register %o0
3211 // g) lock result registers and emit call operation
3212 //
3213 // Before issuing a call, we must spill-save all values on stack
3214 // that are in caller-save register. "spill-save" moves those registers
3215 // either in a free callee-save register or spills them if no free
3216 // callee save register is available.
3217 //
3218 // The problem is where to invoke spill-save.
3219 // - if invoked between e) and f), we may lock callee save
3220 // register in "spill-save" that destroys the receiver register
3221 // before f) is executed
3222 // - if we rearrange f) to be earlier (by loading %o0) it
3223 // may destroy a value on the stack that is currently in %o0
3224 // and is waiting to be spilled
3225 // - if we keep the receiver locked while doing spill-save,
3226 // we cannot spill it as it is spill-locked
3227 //
3228 void LIRGenerator::do_Invoke(Invoke* x) {
3229 CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);
3230
3231 LIR_OprList* arg_list = cc->args();
3232 LIRItemList* args = invoke_visit_arguments(x);
3233 LIR_Opr receiver = LIR_OprFact::illegalOpr;
3234
3235 // setup result register
3236 LIR_Opr result_register = LIR_OprFact::illegalOpr;
3237 if (x->type() != voidType) {
3238 result_register = result_register_for(x->type());
3239 }
3240
3241 CodeEmitInfo* info = state_for(x, x->state());
3242
3243 invoke_load_arguments(x, args, arg_list);
3244
3245 if (x->has_receiver()) {
3246 args->at(0)->load_item_force(LIR_Assembler::receiverOpr());
3247 receiver = args->at(0)->result();
3248 }
3249
3250 // emit invoke code
3251 assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");
3252
3253 ciMethod* target = x->target();
3254 switch (x->code()) {
3255 case Bytecodes::_invokestatic:
3256 __ call_static(target, result_register,
3257 SharedRuntime::get_resolve_static_call_stub(),
3258 arg_list, info);
3259 break;
3260 case Bytecodes::_invokespecial:
3261 case Bytecodes::_invokevirtual:
3262 case Bytecodes::_invokeinterface:
3263 // for loaded and final (method or class) target we still produce an inline cache,
3264 // in order to be able to call mixed mode
3265 if (x->code() == Bytecodes::_invokespecial || x->target_is_final()) {
3266 __ call_opt_virtual(target, receiver, result_register,
3267 SharedRuntime::get_resolve_opt_virtual_call_stub(),
3268 arg_list, info);
3269 } else {
3270 __ call_icvirtual(target, receiver, result_register,
3271 SharedRuntime::get_resolve_virtual_call_stub(),
3272 arg_list, info);
3273 }
3274 break;
3275 case Bytecodes::_invokedynamic: {
3276 __ call_dynamic(target, receiver, result_register,
3277 SharedRuntime::get_resolve_static_call_stub(),
3278 arg_list, info);
3279 break;
3280 }
3281 default:
3282 fatal("unexpected bytecode: %s", Bytecodes::name(x->code()));
3283 break;
3284 }
3285
3286 if (result_register->is_valid()) {
3287 LIR_Opr result = rlock_result(x);
3288 __ move(result_register, result);
3289 }
3290 }
3291
3292
3293 void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {
3294 assert(x->number_of_arguments() == 1, "wrong type");
3295 LIRItem value (x->argument_at(0), this);
3296 LIR_Opr reg = rlock_result(x);
3297 value.load_item();
3298 LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));
3299 __ move(tmp, reg);
3300 }
3301
3302
3303
3304 // Code for : x->x() {x->cond()} x->y() ? x->tval() : x->fval()
3305 void LIRGenerator::do_IfOp(IfOp* x) {
3306 #ifdef ASSERT
3307 {
3308 ValueTag xtag = x->x()->type()->tag();
3309 ValueTag ttag = x->tval()->type()->tag();
3310 assert(xtag == intTag || xtag == objectTag, "cannot handle others");
3311 assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
3312 assert(ttag == x->fval()->type()->tag(), "cannot handle others");
3313 }
3314 #endif
3315
3316 LIRItem left(x->x(), this);
3317 LIRItem right(x->y(), this);
3318 left.load_item();
3319 if (can_inline_as_constant(right.value()) && !x->substitutability_check()) {
3320 right.dont_load_item();
3321 } else {
3322 // substitutability_check() needs to use right as a base register.
3323 right.load_item();
3324 }
3325
3326 LIRItem t_val(x->tval(), this);
3327 LIRItem f_val(x->fval(), this);
3328 t_val.dont_load_item();
3329 f_val.dont_load_item();
3330
3331 if (x->substitutability_check()) {
3332 substitutability_check(x, left, right, t_val, f_val);
3333 } else {
3334 LIR_Opr reg = rlock_result(x);
3335 __ cmp(lir_cond(x->cond()), left.result(), right.result());
3336 __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type()));
3337 }
3338 }
3339
3340 void LIRGenerator::substitutability_check(IfOp* x, LIRItem& left, LIRItem& right, LIRItem& t_val, LIRItem& f_val) {
3341 assert(x->cond() == If::eql || x->cond() == If::neq, "must be");
3342 bool is_acmpeq = (x->cond() == If::eql);
3343 LIR_Opr equal_result = is_acmpeq ? t_val.result() : f_val.result();
3344 LIR_Opr not_equal_result = is_acmpeq ? f_val.result() : t_val.result();
3345 LIR_Opr result = rlock_result(x);
3346 CodeEmitInfo* info = state_for(x, x->state_before());
3347
3348 substitutability_check_common(x->x(), x->y(), left, right, equal_result, not_equal_result, result, info);
3349 }
3350
3351 void LIRGenerator::substitutability_check(If* x, LIRItem& left, LIRItem& right) {
3352 LIR_Opr equal_result = LIR_OprFact::intConst(1);
3353 LIR_Opr not_equal_result = LIR_OprFact::intConst(0);
3354 LIR_Opr result = new_register(T_INT);
3355 CodeEmitInfo* info = state_for(x, x->state_before());
3356
3357 substitutability_check_common(x->x(), x->y(), left, right, equal_result, not_equal_result, result, info);
3358
3359 assert(x->cond() == If::eql || x->cond() == If::neq, "must be");
3360 __ cmp(lir_cond(x->cond()), result, equal_result);
3361 }
3362
3363 void LIRGenerator::substitutability_check_common(Value left_val, Value right_val, LIRItem& left, LIRItem& right,
3364 LIR_Opr equal_result, LIR_Opr not_equal_result, LIR_Opr result,
3365 CodeEmitInfo* info) {
3366 LIR_Opr tmp1 = LIR_OprFact::illegalOpr;
3367 LIR_Opr tmp2 = LIR_OprFact::illegalOpr;
3368
3369 ciKlass* left_klass = left_val->as_loaded_klass_or_null();
3370 ciKlass* right_klass = right_val->as_loaded_klass_or_null();
3371 if (left_klass != nullptr && left_klass->is_inlinetype() && left_klass == right_klass) {
3372 // No need to load klass -- the operands are statically known to be the same inline klass.
3373 } else {
3374 BasicType t_klass = UseCompressedOops ? T_INT : T_METADATA;
3375 tmp1 = new_register(t_klass);
3376 tmp2 = new_register(t_klass);
3377 }
3378
3379 CodeStub* slow_path = new SubstitutabilityCheckStub(left.result(), right.result(), info);
3380 __ substitutability_check(result, left.result(), right.result(), equal_result, not_equal_result,
3381 left_klass, right_klass, tmp1, tmp2, info, slow_path);
3382 }
3383
3384 void LIRGenerator::do_RuntimeCall(address routine, Intrinsic* x) {
3385 assert(x->number_of_arguments() == 0, "wrong type");
3386 // Enforce computation of _reserved_argument_area_size which is required on some platforms.
3387 BasicTypeList signature;
3388 CallingConvention* cc = frame_map()->c_calling_convention(&signature);
3389 LIR_Opr reg = result_register_for(x->type());
3390 __ call_runtime_leaf(routine, getThreadTemp(),
3391 reg, new LIR_OprList());
3392 LIR_Opr result = rlock_result(x);
3393 __ move(reg, result);
3394 }
3395
3396
3397
3398 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
3399 switch (x->id()) {
3400 case vmIntrinsics::_intBitsToFloat :
3401 case vmIntrinsics::_doubleToRawLongBits :
3402 case vmIntrinsics::_longBitsToDouble :
3403 case vmIntrinsics::_floatToRawIntBits : {
3404 do_FPIntrinsics(x);
3405 break;
3406 }
3407
3408 #ifdef JFR_HAVE_INTRINSICS
3409 case vmIntrinsics::_counterTime:
3410 do_RuntimeCall(CAST_FROM_FN_PTR(address, JfrTime::time_function()), x);
3411 break;
3412 #endif
3413
3414 case vmIntrinsics::_currentTimeMillis:
3415 do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeMillis), x);
3416 break;
3417
3418 case vmIntrinsics::_nanoTime:
3419 do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeNanos), x);
3420 break;
3421
3422 case vmIntrinsics::_Object_init: do_RegisterFinalizer(x); break;
3423 case vmIntrinsics::_isInstance: do_isInstance(x); break;
3424 case vmIntrinsics::_getClass: do_getClass(x); break;
3425 case vmIntrinsics::_getObjectSize: do_getObjectSize(x); break;
3426 case vmIntrinsics::_currentCarrierThread: do_currentCarrierThread(x); break;
3427 case vmIntrinsics::_currentThread: do_vthread(x); break;
3428 case vmIntrinsics::_scopedValueCache: do_scopedValueCache(x); break;
3429
3430 case vmIntrinsics::_dlog: // fall through
3431 case vmIntrinsics::_dlog10: // fall through
3432 case vmIntrinsics::_dabs: // fall through
3433 case vmIntrinsics::_dsqrt: // fall through
3434 case vmIntrinsics::_dsqrt_strict: // fall through
3435 case vmIntrinsics::_dtan: // fall through
3436 case vmIntrinsics::_dsinh: // fall through
3437 case vmIntrinsics::_dtanh: // fall through
3438 case vmIntrinsics::_dsin : // fall through
3439 case vmIntrinsics::_dcos : // fall through
3440 case vmIntrinsics::_dcbrt : // fall through
3441 case vmIntrinsics::_dexp : // fall through
3442 case vmIntrinsics::_dpow : do_MathIntrinsic(x); break;
3443 case vmIntrinsics::_arraycopy: do_ArrayCopy(x); break;
3444
3445 case vmIntrinsics::_fmaD: do_FmaIntrinsic(x); break;
3446 case vmIntrinsics::_fmaF: do_FmaIntrinsic(x); break;
3447
3448 // Use java.lang.Math intrinsics code since it works for these intrinsics too.
3449 case vmIntrinsics::_floatToFloat16: // fall through
3450 case vmIntrinsics::_float16ToFloat: do_MathIntrinsic(x); break;
3451
3452 case vmIntrinsics::_Preconditions_checkIndex:
3453 do_PreconditionsCheckIndex(x, T_INT);
3454 break;
3455 case vmIntrinsics::_Preconditions_checkLongIndex:
3456 do_PreconditionsCheckIndex(x, T_LONG);
3457 break;
3458
3459 case vmIntrinsics::_compareAndSetReference:
3460 do_CompareAndSwap(x, objectType);
3461 break;
3462 case vmIntrinsics::_compareAndSetInt:
3463 do_CompareAndSwap(x, intType);
3464 break;
3465 case vmIntrinsics::_compareAndSetLong:
3466 do_CompareAndSwap(x, longType);
3467 break;
3468
3469 case vmIntrinsics::_loadFence :
3470 __ membar_acquire();
3471 break;
3472 case vmIntrinsics::_storeFence:
3473 __ membar_release();
3474 break;
3475 case vmIntrinsics::_storeStoreFence:
3476 __ membar_storestore();
3477 break;
3478 case vmIntrinsics::_fullFence :
3479 __ membar();
3480 break;
3481 case vmIntrinsics::_onSpinWait:
3482 __ on_spin_wait();
3483 break;
3484 case vmIntrinsics::_Reference_get0:
3485 do_Reference_get0(x);
3486 break;
3487
3488 case vmIntrinsics::_updateCRC32:
3489 case vmIntrinsics::_updateBytesCRC32:
3490 case vmIntrinsics::_updateByteBufferCRC32:
3491 do_update_CRC32(x);
3492 break;
3493
3494 case vmIntrinsics::_updateBytesCRC32C:
3495 case vmIntrinsics::_updateDirectByteBufferCRC32C:
3496 do_update_CRC32C(x);
3497 break;
3498
3499 case vmIntrinsics::_vectorizedMismatch:
3500 do_vectorizedMismatch(x);
3501 break;
3502
3503 case vmIntrinsics::_blackhole:
3504 do_blackhole(x);
3505 break;
3506
3507 default: ShouldNotReachHere(); break;
3508 }
3509 }
3510
3511 void LIRGenerator::profile_arguments(ProfileCall* x) {
3512 if (compilation()->profile_arguments()) {
3513 int bci = x->bci_of_invoke();
3514 ciMethodData* md = x->method()->method_data_or_null();
3515 assert(md != nullptr, "Sanity");
3516 ciProfileData* data = md->bci_to_data(bci);
3517 if (data != nullptr) {
3518 if ((data->is_CallTypeData() && data->as_CallTypeData()->has_arguments()) ||
3519 (data->is_VirtualCallTypeData() && data->as_VirtualCallTypeData()->has_arguments())) {
3520 ByteSize extra = data->is_CallTypeData() ? CallTypeData::args_data_offset() : VirtualCallTypeData::args_data_offset();
3521 int base_offset = md->byte_offset_of_slot(data, extra);
3522 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3523 ciTypeStackSlotEntries* args = data->is_CallTypeData() ? ((ciCallTypeData*)data)->args() : ((ciVirtualCallTypeData*)data)->args();
3524
3525 Bytecodes::Code bc = x->method()->java_code_at_bci(bci);
3526 int start = 0;
3527 int stop = data->is_CallTypeData() ? ((ciCallTypeData*)data)->number_of_arguments() : ((ciVirtualCallTypeData*)data)->number_of_arguments();
3528 if (x->callee()->is_loaded() && x->callee()->is_static() && Bytecodes::has_receiver(bc)) {
3529 // first argument is not profiled at call (method handle invoke)
3530 assert(x->method()->raw_code_at_bci(bci) == Bytecodes::_invokehandle, "invokehandle expected");
3531 start = 1;
3532 }
3533 ciSignature* callee_signature = x->callee()->signature();
3534 // method handle call to virtual method
3535 bool has_receiver = x->callee()->is_loaded() && !x->callee()->is_static() && !Bytecodes::has_receiver(bc);
3536 ciSignatureStream callee_signature_stream(callee_signature, has_receiver ? x->callee()->holder() : nullptr);
3537
3538 bool ignored_will_link;
3539 ciSignature* signature_at_call = nullptr;
3540 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3541 ciSignatureStream signature_at_call_stream(signature_at_call);
3542
3543 // if called through method handle invoke, some arguments may have been popped
3544 for (int i = 0; i < stop && i+start < x->nb_profiled_args(); i++) {
3545 int off = in_bytes(TypeEntriesAtCall::argument_type_offset(i)) - in_bytes(TypeEntriesAtCall::args_data_offset());
3546 ciKlass* exact = profile_type(md, base_offset, off,
3547 args->type(i), x->profiled_arg_at(i+start), mdp,
3548 !x->arg_needs_null_check(i+start),
3549 signature_at_call_stream.next_klass(), callee_signature_stream.next_klass());
3550 if (exact != nullptr) {
3551 md->set_argument_type(bci, i, exact);
3552 }
3553 }
3554 } else {
3555 #ifdef ASSERT
3556 Bytecodes::Code code = x->method()->raw_code_at_bci(x->bci_of_invoke());
3557 int n = x->nb_profiled_args();
3558 assert(MethodData::profile_parameters() && (MethodData::profile_arguments_jsr292_only() ||
3559 (x->inlined() && ((code == Bytecodes::_invokedynamic && n <= 1) || (code == Bytecodes::_invokehandle && n <= 2)))),
3560 "only at JSR292 bytecodes");
3561 #endif
3562 }
3563 }
3564 }
3565 }
3566
3567 // profile parameters on entry to an inlined method
3568 void LIRGenerator::profile_parameters_at_call(ProfileCall* x) {
3569 if (compilation()->profile_parameters() && x->inlined()) {
3570 ciMethodData* md = x->callee()->method_data_or_null();
3571 if (md != nullptr) {
3572 ciParametersTypeData* parameters_type_data = md->parameters_type_data();
3573 if (parameters_type_data != nullptr) {
3574 ciTypeStackSlotEntries* parameters = parameters_type_data->parameters();
3575 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3576 bool has_receiver = !x->callee()->is_static();
3577 ciSignature* sig = x->callee()->signature();
3578 ciSignatureStream sig_stream(sig, has_receiver ? x->callee()->holder() : nullptr);
3579 int i = 0; // to iterate on the Instructions
3580 Value arg = x->recv();
3581 bool not_null = false;
3582 int bci = x->bci_of_invoke();
3583 Bytecodes::Code bc = x->method()->java_code_at_bci(bci);
3584 // The first parameter is the receiver so that's what we start
3585 // with if it exists. One exception is method handle call to
3586 // virtual method: the receiver is in the args list
3587 if (arg == nullptr || !Bytecodes::has_receiver(bc)) {
3588 i = 1;
3589 arg = x->profiled_arg_at(0);
3590 not_null = !x->arg_needs_null_check(0);
3591 }
3592 int k = 0; // to iterate on the profile data
3593 for (;;) {
3594 intptr_t profiled_k = parameters->type(k);
3595 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
3596 in_bytes(ParametersTypeData::type_offset(k)) - in_bytes(ParametersTypeData::type_offset(0)),
3597 profiled_k, arg, mdp, not_null, sig_stream.next_klass(), nullptr);
3598 // If the profile is known statically set it once for all and do not emit any code
3599 if (exact != nullptr) {
3600 md->set_parameter_type(k, exact);
3601 }
3602 k++;
3603 if (k >= parameters_type_data->number_of_parameters()) {
3604 #ifdef ASSERT
3605 int extra = 0;
3606 if (MethodData::profile_arguments() && TypeProfileParmsLimit != -1 &&
3607 x->nb_profiled_args() >= TypeProfileParmsLimit &&
3608 x->recv() != nullptr && Bytecodes::has_receiver(bc)) {
3609 extra += 1;
3610 }
3611 assert(i == x->nb_profiled_args() - extra || (TypeProfileParmsLimit != -1 && TypeProfileArgsLimit > TypeProfileParmsLimit), "unused parameters?");
3612 #endif
3613 break;
3614 }
3615 arg = x->profiled_arg_at(i);
3616 not_null = !x->arg_needs_null_check(i);
3617 i++;
3618 }
3619 }
3620 }
3621 }
3622 }
3623
3624 void LIRGenerator::do_ProfileCall(ProfileCall* x) {
3625 // Need recv in a temporary register so it interferes with the other temporaries
3626 LIR_Opr recv = LIR_OprFact::illegalOpr;
3627 LIR_Opr mdo = new_register(T_METADATA);
3628 // tmp is used to hold the counters on SPARC
3629 LIR_Opr tmp = new_pointer_register();
3630
3631 if (x->nb_profiled_args() > 0) {
3632 profile_arguments(x);
3633 }
3634
3635 // profile parameters on inlined method entry including receiver
3636 if (x->recv() != nullptr || x->nb_profiled_args() > 0) {
3637 profile_parameters_at_call(x);
3638 }
3639
3640 if (x->recv() != nullptr) {
3641 LIRItem value(x->recv(), this);
3642 value.load_item();
3643 recv = new_register(T_OBJECT);
3644 __ move(value.result(), recv);
3645 }
3646 __ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder());
3647 }
3648
3649 void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) {
3650 int bci = x->bci_of_invoke();
3651 ciMethodData* md = x->method()->method_data_or_null();
3652 assert(md != nullptr, "Sanity");
3653 ciProfileData* data = md->bci_to_data(bci);
3654 if (data != nullptr) {
3655 assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type");
3656 ciSingleTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret();
3657 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3658
3659 bool ignored_will_link;
3660 ciSignature* signature_at_call = nullptr;
3661 x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3662
3663 // The offset within the MDO of the entry to update may be too large
3664 // to be used in load/store instructions on some platforms. So have
3665 // profile_type() compute the address of the profile in a register.
3666 ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0,
3667 ret->type(), x->ret(), mdp,
3668 !x->needs_null_check(),
3669 signature_at_call->return_type()->as_klass(),
3670 x->callee()->signature()->return_type()->as_klass());
3671 if (exact != nullptr) {
3672 md->set_return_type(bci, exact);
3673 }
3674 }
3675 }
3676
3677 bool LIRGenerator::profile_inline_klass(ciMethodData* md, ciProfileData* data, Value value, int flag) {
3678 ciKlass* klass = value->as_loaded_klass_or_null();
3679 if (klass != nullptr) {
3680 if (klass->is_inlinetype()) {
3681 profile_flags(md, data, flag, lir_cond_always);
3682 } else if (klass->can_be_inline_klass()) {
3683 return false;
3684 }
3685 } else {
3686 return false;
3687 }
3688 return true;
3689 }
3690
3691 void LIRGenerator::do_ProfileACmpTypes(ProfileACmpTypes* x) {
3692 ciMethod* method = x->method();
3693 assert(method != nullptr, "method should be set if branch is profiled");
3694 ciMethodData* md = method->method_data_or_null();
3695 assert(md != nullptr, "Sanity");
3696 ciProfileData* data = md->bci_to_data(x->bci());
3697 assert(data != nullptr, "must have profiling data");
3698 assert(data->is_ACmpData(), "need BranchData for two-way branches");
3699 ciACmpData* acmp = (ciACmpData*)data;
3700 LIR_Opr mdp = LIR_OprFact::illegalOpr;
3701 profile_type(md, md->byte_offset_of_slot(acmp, ACmpData::left_offset()), 0,
3702 acmp->left()->type(), x->left(), mdp, !x->left_maybe_null(), nullptr, nullptr);
3703 int flags_offset = md->byte_offset_of_slot(data, DataLayout::flags_offset());
3704 if (!profile_inline_klass(md, acmp, x->left(), ACmpData::left_inline_type_byte_constant())) {
3705 LIR_Opr mdp = new_register(T_METADATA);
3706 __ metadata2reg(md->constant_encoding(), mdp);
3707 LIRItem value(x->left(), this);
3708 value.load_item();
3709 __ profile_inline_type(new LIR_Address(mdp, flags_offset, T_INT), value.result(), ACmpData::left_inline_type_byte_constant(), new_register(T_INT), !x->left_maybe_null());
3710 }
3711 profile_type(md, md->byte_offset_of_slot(acmp, ACmpData::left_offset()),
3712 in_bytes(ACmpData::right_offset()) - in_bytes(ACmpData::left_offset()),
3713 acmp->right()->type(), x->right(), mdp, !x->right_maybe_null(), nullptr, nullptr);
3714 if (!profile_inline_klass(md, acmp, x->right(), ACmpData::right_inline_type_byte_constant())) {
3715 LIR_Opr mdp = new_register(T_METADATA);
3716 __ metadata2reg(md->constant_encoding(), mdp);
3717 LIRItem value(x->right(), this);
3718 value.load_item();
3719 __ profile_inline_type(new LIR_Address(mdp, flags_offset, T_INT), value.result(), ACmpData::right_inline_type_byte_constant(), new_register(T_INT), !x->left_maybe_null());
3720 }
3721 }
3722
3723 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {
3724 // We can safely ignore accessors here, since c2 will inline them anyway,
3725 // accessors are also always mature.
3726 if (!x->inlinee()->is_accessor()) {
3727 CodeEmitInfo* info = state_for(x, x->state(), true);
3728 // Notify the runtime very infrequently only to take care of counter overflows
3729 int freq_log = Tier23InlineeNotifyFreqLog;
3730 double scale;
3731 if (_method->has_option_value(CompileCommandEnum::CompileThresholdScaling, scale)) {
3732 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale);
3733 }
3734 increment_event_counter_impl(info, x->inlinee(), LIR_OprFact::intConst(InvocationCounter::count_increment), right_n_bits(freq_log), InvocationEntryBci, false, true);
3735 }
3736 }
3737
3738 void LIRGenerator::increment_backedge_counter_conditionally(LIR_Condition cond, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info, int left_bci, int right_bci, int bci) {
3739 if (compilation()->is_profiling()) {
3740 #if defined(X86) && !defined(_LP64)
3741 // BEWARE! On 32-bit x86 cmp clobbers its left argument so we need a temp copy.
3742 LIR_Opr left_copy = new_register(left->type());
3743 __ move(left, left_copy);
3744 __ cmp(cond, left_copy, right);
3745 #else
3746 __ cmp(cond, left, right);
3747 #endif
3748 LIR_Opr step = new_register(T_INT);
3749 LIR_Opr plus_one = LIR_OprFact::intConst(InvocationCounter::count_increment);
3750 LIR_Opr zero = LIR_OprFact::intConst(0);
3751 __ cmove(cond,
3752 (left_bci < bci) ? plus_one : zero,
3753 (right_bci < bci) ? plus_one : zero,
3754 step, left->type());
3755 increment_backedge_counter(info, step, bci);
3756 }
3757 }
3758
3759
3760 void LIRGenerator::increment_event_counter(CodeEmitInfo* info, LIR_Opr step, int bci, bool backedge) {
3761 int freq_log = 0;
3762 int level = compilation()->env()->comp_level();
3763 if (level == CompLevel_limited_profile) {
3764 freq_log = (backedge ? Tier2BackedgeNotifyFreqLog : Tier2InvokeNotifyFreqLog);
3765 } else if (level == CompLevel_full_profile) {
3766 freq_log = (backedge ? Tier3BackedgeNotifyFreqLog : Tier3InvokeNotifyFreqLog);
3767 } else {
3768 ShouldNotReachHere();
3769 }
3770 // Increment the appropriate invocation/backedge counter and notify the runtime.
3771 double scale;
3772 if (_method->has_option_value(CompileCommandEnum::CompileThresholdScaling, scale)) {
3773 freq_log = CompilerConfig::scaled_freq_log(freq_log, scale);
3774 }
3775 increment_event_counter_impl(info, info->scope()->method(), step, right_n_bits(freq_log), bci, backedge, true);
3776 }
3777
3778 void LIRGenerator::increment_event_counter_impl(CodeEmitInfo* info,
3779 ciMethod *method, LIR_Opr step, int frequency,
3780 int bci, bool backedge, bool notify) {
3781 assert(frequency == 0 || is_power_of_2(frequency + 1), "Frequency must be x^2 - 1 or 0");
3782 int level = _compilation->env()->comp_level();
3783 assert(level > CompLevel_simple, "Shouldn't be here");
3784
3785 int offset = -1;
3786 LIR_Opr counter_holder;
3787 if (level == CompLevel_limited_profile) {
3788 MethodCounters* counters_adr = method->ensure_method_counters();
3789 if (counters_adr == nullptr) {
3790 bailout("method counters allocation failed");
3791 return;
3792 }
3793 counter_holder = new_pointer_register();
3794 __ move(LIR_OprFact::intptrConst(counters_adr), counter_holder);
3795 offset = in_bytes(backedge ? MethodCounters::backedge_counter_offset() :
3796 MethodCounters::invocation_counter_offset());
3797 } else if (level == CompLevel_full_profile) {
3798 counter_holder = new_register(T_METADATA);
3799 offset = in_bytes(backedge ? MethodData::backedge_counter_offset() :
3800 MethodData::invocation_counter_offset());
3801 ciMethodData* md = method->method_data_or_null();
3802 assert(md != nullptr, "Sanity");
3803 __ metadata2reg(md->constant_encoding(), counter_holder);
3804 } else {
3805 ShouldNotReachHere();
3806 }
3807 LIR_Address* counter = new LIR_Address(counter_holder, offset, T_INT);
3808 LIR_Opr result = new_register(T_INT);
3809 __ load(counter, result);
3810 __ add(result, step, result);
3811 __ store(result, counter);
3812 if (notify && (!backedge || UseOnStackReplacement)) {
3813 LIR_Opr meth = LIR_OprFact::metadataConst(method->constant_encoding());
3814 // The bci for info can point to cmp for if's we want the if bci
3815 CodeStub* overflow = new CounterOverflowStub(info, bci, meth);
3816 int freq = frequency << InvocationCounter::count_shift;
3817 if (freq == 0) {
3818 if (!step->is_constant()) {
3819 __ cmp(lir_cond_notEqual, step, LIR_OprFact::intConst(0));
3820 __ branch(lir_cond_notEqual, overflow);
3821 } else {
3822 __ branch(lir_cond_always, overflow);
3823 }
3824 } else {
3825 LIR_Opr mask = load_immediate(freq, T_INT);
3826 if (!step->is_constant()) {
3827 // If step is 0, make sure the overflow check below always fails
3828 __ cmp(lir_cond_notEqual, step, LIR_OprFact::intConst(0));
3829 __ cmove(lir_cond_notEqual, result, LIR_OprFact::intConst(InvocationCounter::count_increment), result, T_INT);
3830 }
3831 __ logical_and(result, mask, result);
3832 __ cmp(lir_cond_equal, result, LIR_OprFact::intConst(0));
3833 __ branch(lir_cond_equal, overflow);
3834 }
3835 __ branch_destination(overflow->continuation());
3836 }
3837 }
3838
3839 void LIRGenerator::do_RuntimeCall(RuntimeCall* x) {
3840 LIR_OprList* args = new LIR_OprList(x->number_of_arguments());
3841 BasicTypeList* signature = new BasicTypeList(x->number_of_arguments());
3842
3843 if (x->pass_thread()) {
3844 signature->append(LP64_ONLY(T_LONG) NOT_LP64(T_INT)); // thread
3845 args->append(getThreadPointer());
3846 }
3847
3848 for (int i = 0; i < x->number_of_arguments(); i++) {
3849 Value a = x->argument_at(i);
3850 LIRItem* item = new LIRItem(a, this);
3851 item->load_item();
3852 args->append(item->result());
3853 signature->append(as_BasicType(a->type()));
3854 }
3855
3856 LIR_Opr result = call_runtime(signature, args, x->entry(), x->type(), nullptr);
3857 if (x->type() == voidType) {
3858 set_no_result(x);
3859 } else {
3860 __ move(result, rlock_result(x));
3861 }
3862 }
3863
3864 #ifdef ASSERT
3865 void LIRGenerator::do_Assert(Assert *x) {
3866 ValueTag tag = x->x()->type()->tag();
3867 If::Condition cond = x->cond();
3868
3869 LIRItem xitem(x->x(), this);
3870 LIRItem yitem(x->y(), this);
3871 LIRItem* xin = &xitem;
3872 LIRItem* yin = &yitem;
3873
3874 assert(tag == intTag, "Only integer assertions are valid!");
3875
3876 xin->load_item();
3877 yin->dont_load_item();
3878
3879 set_no_result(x);
3880
3881 LIR_Opr left = xin->result();
3882 LIR_Opr right = yin->result();
3883
3884 __ lir_assert(lir_cond(x->cond()), left, right, x->message(), true);
3885 }
3886 #endif
3887
3888 void LIRGenerator::do_RangeCheckPredicate(RangeCheckPredicate *x) {
3889
3890
3891 Instruction *a = x->x();
3892 Instruction *b = x->y();
3893 if (!a || StressRangeCheckElimination) {
3894 assert(!b || StressRangeCheckElimination, "B must also be null");
3895
3896 CodeEmitInfo *info = state_for(x, x->state());
3897 CodeStub* stub = new PredicateFailedStub(info);
3898
3899 __ jump(stub);
3900 } else if (a->type()->as_IntConstant() && b->type()->as_IntConstant()) {
3901 int a_int = a->type()->as_IntConstant()->value();
3902 int b_int = b->type()->as_IntConstant()->value();
3903
3904 bool ok = false;
3905
3906 switch(x->cond()) {
3907 case Instruction::eql: ok = (a_int == b_int); break;
3908 case Instruction::neq: ok = (a_int != b_int); break;
3909 case Instruction::lss: ok = (a_int < b_int); break;
3910 case Instruction::leq: ok = (a_int <= b_int); break;
3911 case Instruction::gtr: ok = (a_int > b_int); break;
3912 case Instruction::geq: ok = (a_int >= b_int); break;
3913 case Instruction::aeq: ok = ((unsigned int)a_int >= (unsigned int)b_int); break;
3914 case Instruction::beq: ok = ((unsigned int)a_int <= (unsigned int)b_int); break;
3915 default: ShouldNotReachHere();
3916 }
3917
3918 if (ok) {
3919
3920 CodeEmitInfo *info = state_for(x, x->state());
3921 CodeStub* stub = new PredicateFailedStub(info);
3922
3923 __ jump(stub);
3924 }
3925 } else {
3926
3927 ValueTag tag = x->x()->type()->tag();
3928 If::Condition cond = x->cond();
3929 LIRItem xitem(x->x(), this);
3930 LIRItem yitem(x->y(), this);
3931 LIRItem* xin = &xitem;
3932 LIRItem* yin = &yitem;
3933
3934 assert(tag == intTag, "Only integer deoptimizations are valid!");
3935
3936 xin->load_item();
3937 yin->dont_load_item();
3938 set_no_result(x);
3939
3940 LIR_Opr left = xin->result();
3941 LIR_Opr right = yin->result();
3942
3943 CodeEmitInfo *info = state_for(x, x->state());
3944 CodeStub* stub = new PredicateFailedStub(info);
3945
3946 __ cmp(lir_cond(cond), left, right);
3947 __ branch(lir_cond(cond), stub);
3948 }
3949 }
3950
3951 void LIRGenerator::do_blackhole(Intrinsic *x) {
3952 assert(!x->has_receiver(), "Should have been checked before: only static methods here");
3953 for (int c = 0; c < x->number_of_arguments(); c++) {
3954 // Load the argument
3955 LIRItem vitem(x->argument_at(c), this);
3956 vitem.load_item();
3957 // ...and leave it unused.
3958 }
3959 }
3960
3961 LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
3962 LIRItemList args(1);
3963 LIRItem value(arg1, this);
3964 args.append(&value);
3965 BasicTypeList signature;
3966 signature.append(as_BasicType(arg1->type()));
3967
3968 return call_runtime(&signature, &args, entry, result_type, info);
3969 }
3970
3971
3972 LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {
3973 LIRItemList args(2);
3974 LIRItem value1(arg1, this);
3975 LIRItem value2(arg2, this);
3976 args.append(&value1);
3977 args.append(&value2);
3978 BasicTypeList signature;
3979 signature.append(as_BasicType(arg1->type()));
3980 signature.append(as_BasicType(arg2->type()));
3981
3982 return call_runtime(&signature, &args, entry, result_type, info);
3983 }
3984
3985
3986 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,
3987 address entry, ValueType* result_type, CodeEmitInfo* info) {
3988 // get a result register
3989 LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
3990 LIR_Opr result = LIR_OprFact::illegalOpr;
3991 if (result_type->tag() != voidTag) {
3992 result = new_register(result_type);
3993 phys_reg = result_register_for(result_type);
3994 }
3995
3996 // move the arguments into the correct location
3997 CallingConvention* cc = frame_map()->c_calling_convention(signature);
3998 assert(cc->length() == args->length(), "argument mismatch");
3999 for (int i = 0; i < args->length(); i++) {
4000 LIR_Opr arg = args->at(i);
4001 LIR_Opr loc = cc->at(i);
4002 if (loc->is_register()) {
4003 __ move(arg, loc);
4004 } else {
4005 LIR_Address* addr = loc->as_address_ptr();
4006 // if (!can_store_as_constant(arg)) {
4007 // LIR_Opr tmp = new_register(arg->type());
4008 // __ move(arg, tmp);
4009 // arg = tmp;
4010 // }
4011 __ move(arg, addr);
4012 }
4013 }
4014
4015 if (info) {
4016 __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
4017 } else {
4018 __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
4019 }
4020 if (result->is_valid()) {
4021 __ move(phys_reg, result);
4022 }
4023 return result;
4024 }
4025
4026
4027 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,
4028 address entry, ValueType* result_type, CodeEmitInfo* info) {
4029 // get a result register
4030 LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
4031 LIR_Opr result = LIR_OprFact::illegalOpr;
4032 if (result_type->tag() != voidTag) {
4033 result = new_register(result_type);
4034 phys_reg = result_register_for(result_type);
4035 }
4036
4037 // move the arguments into the correct location
4038 CallingConvention* cc = frame_map()->c_calling_convention(signature);
4039
4040 assert(cc->length() == args->length(), "argument mismatch");
4041 for (int i = 0; i < args->length(); i++) {
4042 LIRItem* arg = args->at(i);
4043 LIR_Opr loc = cc->at(i);
4044 if (loc->is_register()) {
4045 arg->load_item_force(loc);
4046 } else {
4047 LIR_Address* addr = loc->as_address_ptr();
4048 arg->load_for_store(addr->type());
4049 __ move(arg->result(), addr);
4050 }
4051 }
4052
4053 if (info) {
4054 __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
4055 } else {
4056 __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
4057 }
4058 if (result->is_valid()) {
4059 __ move(phys_reg, result);
4060 }
4061 return result;
4062 }
4063
4064 void LIRGenerator::do_MemBar(MemBar* x) {
4065 LIR_Code code = x->code();
4066 switch(code) {
4067 case lir_membar_acquire : __ membar_acquire(); break;
4068 case lir_membar_release : __ membar_release(); break;
4069 case lir_membar : __ membar(); break;
4070 case lir_membar_loadload : __ membar_loadload(); break;
4071 case lir_membar_storestore: __ membar_storestore(); break;
4072 case lir_membar_loadstore : __ membar_loadstore(); break;
4073 case lir_membar_storeload : __ membar_storeload(); break;
4074 default : ShouldNotReachHere(); break;
4075 }
4076 }
4077
4078 LIR_Opr LIRGenerator::mask_boolean(LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info) {
4079 LIR_Opr value_fixed = rlock_byte(T_BYTE);
4080 if (two_operand_lir_form) {
4081 __ move(value, value_fixed);
4082 __ logical_and(value_fixed, LIR_OprFact::intConst(1), value_fixed);
4083 } else {
4084 __ logical_and(value, LIR_OprFact::intConst(1), value_fixed);
4085 }
4086 LIR_Opr klass = new_register(T_METADATA);
4087 load_klass(array, klass, null_check_info);
4088 null_check_info = nullptr;
4089 LIR_Opr layout = new_register(T_INT);
4090 __ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout);
4091 int diffbit = Klass::layout_helper_boolean_diffbit();
4092 __ logical_and(layout, LIR_OprFact::intConst(diffbit), layout);
4093 __ cmp(lir_cond_notEqual, layout, LIR_OprFact::intConst(0));
4094 __ cmove(lir_cond_notEqual, value_fixed, value, value_fixed, T_BYTE);
4095 value = value_fixed;
4096 return value;
4097 }