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