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