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