1 /*
   2  * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "c1/c1_CodeStubs.hpp"
  26 #include "c1/c1_InstructionPrinter.hpp"
  27 #include "c1/c1_LIR.hpp"
  28 #include "c1/c1_LIRAssembler.hpp"
  29 #include "c1/c1_ValueStack.hpp"
  30 #include "ci/ciInstance.hpp"
  31 #include "runtime/safepointMechanism.inline.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "runtime/vm_version.hpp"
  34 
  35 Register LIR_Opr::as_register() const {
  36   return FrameMap::cpu_rnr2reg(cpu_regnr());
  37 }
  38 
  39 Register LIR_Opr::as_register_lo() const {
  40   return FrameMap::cpu_rnr2reg(cpu_regnrLo());
  41 }
  42 
  43 Register LIR_Opr::as_register_hi() const {
  44   return FrameMap::cpu_rnr2reg(cpu_regnrHi());
  45 }
  46 
  47 LIR_Opr LIR_OprFact::illegalOpr = LIR_OprFact::illegal();
  48 LIR_Opr LIR_OprFact::nullOpr = LIR_Opr();
  49 
  50 LIR_Opr LIR_OprFact::value_type(ValueType* type) {
  51   ValueTag tag = type->tag();
  52   switch (tag) {
  53   case metaDataTag : {
  54     ClassConstant* c = type->as_ClassConstant();
  55     if (c != nullptr && !c->value()->is_loaded()) {
  56       return LIR_OprFact::metadataConst(nullptr);
  57     } else if (c != nullptr) {
  58       return LIR_OprFact::metadataConst(c->value()->constant_encoding());
  59     } else {
  60       MethodConstant* m = type->as_MethodConstant();
  61       assert (m != nullptr, "not a class or a method?");
  62       return LIR_OprFact::metadataConst(m->value()->constant_encoding());
  63     }
  64   }
  65   case objectTag : {
  66       return LIR_OprFact::oopConst(type->as_ObjectType()->encoding());
  67     }
  68   case addressTag: return LIR_OprFact::addressConst(type->as_AddressConstant()->value());
  69   case intTag    : return LIR_OprFact::intConst(type->as_IntConstant()->value());
  70   case floatTag  : return LIR_OprFact::floatConst(type->as_FloatConstant()->value());
  71   case longTag   : return LIR_OprFact::longConst(type->as_LongConstant()->value());
  72   case doubleTag : return LIR_OprFact::doubleConst(type->as_DoubleConstant()->value());
  73   default: ShouldNotReachHere(); return LIR_OprFact::intConst(-1);
  74   }
  75 }
  76 
  77 
  78 //---------------------------------------------------
  79 
  80 
  81 LIR_Address::Scale LIR_Address::scale(BasicType type) {
  82   int elem_size = type2aelembytes(type);
  83   switch (elem_size) {
  84   case 1: return LIR_Address::times_1;
  85   case 2: return LIR_Address::times_2;
  86   case 4: return LIR_Address::times_4;
  87   case 8: return LIR_Address::times_8;
  88   }
  89   ShouldNotReachHere();
  90   return LIR_Address::times_1;
  91 }
  92 
  93 //---------------------------------------------------
  94 
  95 char LIR_Opr::type_char(BasicType t) {
  96   switch (t) {
  97     case T_ARRAY:
  98       t = T_OBJECT;
  99     case T_BOOLEAN:
 100     case T_CHAR:
 101     case T_FLOAT:
 102     case T_DOUBLE:
 103     case T_BYTE:
 104     case T_SHORT:
 105     case T_INT:
 106     case T_LONG:
 107     case T_OBJECT:
 108     case T_ADDRESS:
 109     case T_VOID:
 110       return ::type2char(t);
 111     case T_METADATA:
 112       return 'M';
 113     case T_ILLEGAL:
 114       return '?';
 115 
 116     default:
 117       ShouldNotReachHere();
 118       return '?';
 119   }
 120 }
 121 
 122 #ifndef PRODUCT
 123 void LIR_Opr::validate_type() const {
 124 
 125 #ifdef ASSERT
 126   if (!is_pointer() && !is_illegal()) {
 127     OprKind kindfield = kind_field(); // Factored out because of compiler bug, see 8002160
 128     switch (as_BasicType(type_field())) {
 129     case T_LONG:
 130       assert((kindfield == cpu_register || kindfield == stack_value) &&
 131              size_field() == double_size, "must match");
 132       break;
 133     case T_FLOAT:
 134       // FP return values can be also in CPU registers on ARM (softfp ABI)
 135       assert((kindfield == fpu_register || kindfield == stack_value
 136              ARM_ONLY(|| kindfield == cpu_register) ) &&
 137              size_field() == single_size, "must match");
 138       break;
 139     case T_DOUBLE:
 140       // FP return values can be also in CPU registers on ARM (softfp ABI)
 141       assert((kindfield == fpu_register || kindfield == stack_value
 142              ARM_ONLY(|| kindfield == cpu_register) ) &&
 143              size_field() == double_size, "must match");
 144       break;
 145     case T_BOOLEAN:
 146     case T_CHAR:
 147     case T_BYTE:
 148     case T_SHORT:
 149     case T_INT:
 150     case T_ADDRESS:
 151     case T_OBJECT:
 152     case T_METADATA:
 153     case T_ARRAY:
 154       assert((kindfield == cpu_register || kindfield == stack_value) &&
 155              size_field() == single_size, "must match");
 156       break;
 157 
 158     case T_ILLEGAL:
 159       // XXX TKR also means unknown right now
 160       // assert(is_illegal(), "must match");
 161       break;
 162 
 163     default:
 164       ShouldNotReachHere();
 165     }
 166   }
 167 #endif
 168 
 169 }
 170 #endif // PRODUCT
 171 
 172 
 173 bool LIR_Opr::is_oop() const {
 174   if (is_pointer()) {
 175     return pointer()->is_oop_pointer();
 176   } else {
 177     OprType t= type_field();
 178     assert(t != unknown_type, "not set");
 179     return t == object_type;
 180   }
 181 }
 182 
 183 
 184 
 185 void LIR_Op2::verify() const {
 186 #ifdef ASSERT
 187   switch (code()) {
 188     case lir_xchg:
 189       break;
 190 
 191     default:
 192       assert(!result_opr()->is_register() || !result_opr()->is_oop_register(),
 193              "can't produce oops from arith");
 194   }
 195 
 196   if (two_operand_lir_form) {
 197 
 198     bool threeOperandForm = false;
 199 #ifdef S390
 200     // There are 3 operand shifts on S390 (see LIR_Assembler::shift_op()).
 201     threeOperandForm =
 202       code() == lir_shl ||
 203       ((code() == lir_shr || code() == lir_ushr) && (result_opr()->is_double_cpu() || in_opr1()->type() == T_OBJECT));
 204 #endif
 205 
 206     switch (code()) {
 207     case lir_add:
 208     case lir_sub:
 209     case lir_mul:
 210     case lir_div:
 211     case lir_rem:
 212     case lir_logic_and:
 213     case lir_logic_or:
 214     case lir_logic_xor:
 215     case lir_shl:
 216     case lir_shr:
 217       assert(in_opr1() == result_opr() || threeOperandForm, "opr1 and result must match");
 218       assert(in_opr1()->is_valid() && in_opr2()->is_valid(), "must be valid");
 219       break;
 220 
 221     // special handling for lir_ushr because of write barriers
 222     case lir_ushr:
 223       assert(in_opr1() == result_opr() || in_opr2()->is_constant() || threeOperandForm, "opr1 and result must match or shift count is constant");
 224       assert(in_opr1()->is_valid() && in_opr2()->is_valid(), "must be valid");
 225       break;
 226 
 227     default:
 228       break;
 229     }
 230   }
 231 #endif
 232 }
 233 
 234 
 235 LIR_OpBranch::LIR_OpBranch(LIR_Condition cond, BlockBegin* block)
 236   : LIR_Op2(lir_branch, cond, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, (CodeEmitInfo*)nullptr)
 237   , _label(block->label())
 238   , _block(block)
 239   , _ublock(nullptr)
 240   , _stub(nullptr) {
 241 }
 242 
 243 LIR_OpBranch::LIR_OpBranch(LIR_Condition cond, CodeStub* stub) :
 244   LIR_Op2(lir_branch, cond, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, (CodeEmitInfo*)nullptr)
 245   , _label(stub->entry())
 246   , _block(nullptr)
 247   , _ublock(nullptr)
 248   , _stub(stub) {
 249 }
 250 
 251 LIR_OpBranch::LIR_OpBranch(LIR_Condition cond, BlockBegin* block, BlockBegin* ublock)
 252   : LIR_Op2(lir_cond_float_branch, cond, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, (CodeEmitInfo*)nullptr)
 253   , _label(block->label())
 254   , _block(block)
 255   , _ublock(ublock)
 256   , _stub(nullptr)
 257 {
 258 }
 259 
 260 void LIR_OpBranch::change_block(BlockBegin* b) {
 261   assert(_block != nullptr, "must have old block");
 262   assert(_block->label() == label(), "must be equal");
 263 
 264   _block = b;
 265   _label = b->label();
 266 }
 267 
 268 void LIR_OpBranch::change_ublock(BlockBegin* b) {
 269   assert(_ublock != nullptr, "must have old block");
 270   _ublock = b;
 271 }
 272 
 273 void LIR_OpBranch::negate_cond() {
 274   switch (cond()) {
 275     case lir_cond_equal:        set_cond(lir_cond_notEqual);     break;
 276     case lir_cond_notEqual:     set_cond(lir_cond_equal);        break;
 277     case lir_cond_less:         set_cond(lir_cond_greaterEqual); break;
 278     case lir_cond_lessEqual:    set_cond(lir_cond_greater);      break;
 279     case lir_cond_greaterEqual: set_cond(lir_cond_less);         break;
 280     case lir_cond_greater:      set_cond(lir_cond_lessEqual);    break;
 281     default: ShouldNotReachHere();
 282   }
 283 }
 284 
 285 
 286 LIR_OpTypeCheck::LIR_OpTypeCheck(LIR_Code code, LIR_Opr result, LIR_Opr object, ciKlass* klass,
 287                                  LIR_Opr tmp1, LIR_Opr tmp2, LIR_Opr tmp3,
 288                                  bool fast_check, CodeEmitInfo* info_for_exception, CodeEmitInfo* info_for_patch,
 289                                  CodeStub* stub)
 290 
 291   : LIR_Op(code, result, nullptr)
 292   , _object(object)
 293   , _array(LIR_OprFact::illegalOpr)
 294   , _klass(klass)
 295   , _tmp1(tmp1)
 296   , _tmp2(tmp2)
 297   , _tmp3(tmp3)
 298   , _info_for_patch(info_for_patch)
 299   , _info_for_exception(info_for_exception)
 300   , _stub(stub)
 301   , _profiled_method(nullptr)
 302   , _profiled_bci(-1)
 303   , _should_profile(false)
 304   , _fast_check(fast_check)
 305 {
 306   if (code == lir_checkcast) {
 307     assert(info_for_exception != nullptr, "checkcast throws exceptions");
 308   } else if (code == lir_instanceof) {
 309     assert(info_for_exception == nullptr, "instanceof throws no exceptions");
 310   } else {
 311     ShouldNotReachHere();
 312   }
 313 }
 314 
 315 
 316 
 317 LIR_OpTypeCheck::LIR_OpTypeCheck(LIR_Code code, LIR_Opr object, LIR_Opr array, LIR_Opr tmp1, LIR_Opr tmp2, LIR_Opr tmp3, CodeEmitInfo* info_for_exception)
 318   : LIR_Op(code, LIR_OprFact::illegalOpr, nullptr)
 319   , _object(object)
 320   , _array(array)
 321   , _klass(nullptr)
 322   , _tmp1(tmp1)
 323   , _tmp2(tmp2)
 324   , _tmp3(tmp3)
 325   , _info_for_patch(nullptr)
 326   , _info_for_exception(info_for_exception)
 327   , _stub(nullptr)
 328   , _profiled_method(nullptr)
 329   , _profiled_bci(-1)
 330   , _should_profile(false)
 331   , _fast_check(false)
 332 {
 333   if (code == lir_store_check) {
 334     _stub = new ArrayStoreExceptionStub(object, info_for_exception);
 335     assert(info_for_exception != nullptr, "store_check throws exceptions");
 336   } else {
 337     ShouldNotReachHere();
 338   }
 339 }
 340 
 341 
 342 LIR_OpArrayCopy::LIR_OpArrayCopy(LIR_Opr src, LIR_Opr src_pos, LIR_Opr dst, LIR_Opr dst_pos, LIR_Opr length,
 343                                  LIR_Opr tmp, ciArrayKlass* expected_type, int flags, CodeEmitInfo* info)
 344   : LIR_Op(lir_arraycopy, LIR_OprFact::illegalOpr, info)
 345   , _src(src)
 346   , _src_pos(src_pos)
 347   , _dst(dst)
 348   , _dst_pos(dst_pos)
 349   , _length(length)
 350   , _tmp(tmp)
 351   , _expected_type(expected_type)
 352   , _flags(flags) {
 353 #if defined(X86) || defined(AARCH64) || defined(S390) || defined(RISCV64) || defined(PPC64)
 354   if (expected_type != nullptr &&
 355       ((flags & ~LIR_OpArrayCopy::get_initial_copy_flags()) == 0)) {
 356     _stub = nullptr;
 357   } else {
 358     _stub = new ArrayCopyStub(this);
 359   }
 360 #else
 361   _stub = new ArrayCopyStub(this);
 362 #endif
 363 }
 364 
 365 LIR_OpUpdateCRC32::LIR_OpUpdateCRC32(LIR_Opr crc, LIR_Opr val, LIR_Opr res)
 366   : LIR_Op(lir_updatecrc32, res, nullptr)
 367   , _crc(crc)
 368   , _val(val) {
 369 }
 370 
 371 //-------------------verify--------------------------
 372 
 373 void LIR_Op1::verify() const {
 374   switch(code()) {
 375   case lir_move:
 376     assert(in_opr()->is_valid() && result_opr()->is_valid(), "must be");
 377     break;
 378   case lir_null_check:
 379     assert(in_opr()->is_register(), "must be");
 380     break;
 381   case lir_return:
 382     assert(in_opr()->is_register() || in_opr()->is_illegal(), "must be");
 383     break;
 384   default:
 385     break;
 386   }
 387 }
 388 
 389 void LIR_OpRTCall::verify() const {
 390   assert(strcmp(Runtime1::name_for_address(addr()), "<unknown function>") != 0, "unknown function");
 391 }
 392 
 393 //-------------------visits--------------------------
 394 
 395 // complete rework of LIR instruction visitor.
 396 // The virtual call for each instruction type is replaced by a big
 397 // switch that adds the operands for each instruction
 398 
 399 void LIR_OpVisitState::visit(LIR_Op* op) {
 400   // copy information from the LIR_Op
 401   reset();
 402   set_op(op);
 403 
 404   switch (op->code()) {
 405 
 406 // LIR_Op0
 407     case lir_breakpoint:               // result and info always invalid
 408     case lir_membar:                   // result and info always invalid
 409     case lir_membar_acquire:           // result and info always invalid
 410     case lir_membar_release:           // result and info always invalid
 411     case lir_membar_loadload:          // result and info always invalid
 412     case lir_membar_storestore:        // result and info always invalid
 413     case lir_membar_loadstore:         // result and info always invalid
 414     case lir_membar_storeload:         // result and info always invalid
 415     case lir_on_spin_wait:
 416     {
 417       assert(op->as_Op0() != nullptr, "must be");
 418       assert(op->_info == nullptr, "info not used by this instruction");
 419       assert(op->_result->is_illegal(), "not used");
 420       break;
 421     }
 422 
 423     case lir_nop:                      // may have info, result always invalid
 424     case lir_std_entry:                // may have result, info always invalid
 425     case lir_osr_entry:                // may have result, info always invalid
 426     case lir_get_thread:               // may have result, info always invalid
 427     {
 428       assert(op->as_Op0() != nullptr, "must be");
 429       if (op->_info != nullptr)           do_info(op->_info);
 430       if (op->_result->is_valid())     do_output(op->_result);
 431       break;
 432     }
 433 
 434 
 435 // LIR_OpLabel
 436     case lir_label:                    // result and info always invalid
 437     {
 438       assert(op->as_OpLabel() != nullptr, "must be");
 439       assert(op->_info == nullptr, "info not used by this instruction");
 440       assert(op->_result->is_illegal(), "not used");
 441       break;
 442     }
 443 
 444 
 445 // LIR_Op1
 446     case lir_push:           // input always valid, result and info always invalid
 447     case lir_pop:            // input always valid, result and info always invalid
 448     case lir_leal:           // input and result always valid, info always invalid
 449     case lir_monaddr:        // input and result always valid, info always invalid
 450     case lir_null_check:     // input and info always valid, result always invalid
 451     case lir_move:           // input and result always valid, may have info
 452     case lir_sqrt:           // FP Ops have no info, but input and result
 453     case lir_abs:
 454     case lir_neg:
 455     case lir_f2hf:
 456     case lir_hf2f:
 457     {
 458       assert(op->as_Op1() != nullptr, "must be");
 459       LIR_Op1* op1 = (LIR_Op1*)op;
 460 
 461       if (op1->_info)                  do_info(op1->_info);
 462       if (op1->_opr->is_valid())       do_input(op1->_opr);
 463       if (op1->_tmp->is_valid())       do_temp(op1->_tmp);
 464       if (op1->_result->is_valid())    do_output(op1->_result);
 465 
 466       break;
 467     }
 468 
 469     case lir_return:
 470     {
 471       assert(op->as_OpReturn() != nullptr, "must be");
 472       LIR_OpReturn* op_ret = (LIR_OpReturn*)op;
 473 
 474       if (op_ret->_info)               do_info(op_ret->_info);
 475       if (op_ret->_opr->is_valid())    do_input(op_ret->_opr);
 476       if (op_ret->_result->is_valid()) do_output(op_ret->_result);
 477       if (op_ret->stub() != nullptr)      do_stub(op_ret->stub());
 478 
 479       break;
 480     }
 481 
 482     case lir_safepoint:
 483     {
 484       assert(op->as_Op1() != nullptr, "must be");
 485       LIR_Op1* op1 = (LIR_Op1*)op;
 486 
 487       assert(op1->_info != nullptr, "");  do_info(op1->_info);
 488       if (op1->_opr->is_valid())       do_temp(op1->_opr); // safepoints on SPARC need temporary register
 489       assert(op1->_tmp->is_illegal(), "not used");
 490       assert(op1->_result->is_illegal(), "safepoint does not produce value");
 491 
 492       break;
 493     }
 494 
 495 // LIR_OpConvert;
 496     case lir_convert:        // input and result always valid, info always invalid
 497     {
 498       assert(op->as_OpConvert() != nullptr, "must be");
 499       LIR_OpConvert* opConvert = (LIR_OpConvert*)op;
 500 
 501       assert(opConvert->_info == nullptr, "must be");
 502       if (opConvert->_opr->is_valid())       do_input(opConvert->_opr);
 503       if (opConvert->_result->is_valid())    do_output(opConvert->_result);
 504       do_stub(opConvert->_stub);
 505 
 506       break;
 507     }
 508 
 509 // LIR_OpBranch;
 510     case lir_branch:                   // may have info, input and result register always invalid
 511     case lir_cond_float_branch:        // may have info, input and result register always invalid
 512     {
 513       assert(op->as_OpBranch() != nullptr, "must be");
 514       LIR_OpBranch* opBranch = (LIR_OpBranch*)op;
 515 
 516       assert(opBranch->_tmp1->is_illegal() && opBranch->_tmp2->is_illegal() &&
 517              opBranch->_tmp3->is_illegal() && opBranch->_tmp4->is_illegal() &&
 518              opBranch->_tmp5->is_illegal(), "not used");
 519 
 520       if (opBranch->_opr1->is_valid()) do_input(opBranch->_opr1);
 521       if (opBranch->_opr2->is_valid()) do_input(opBranch->_opr2);
 522 
 523       if (opBranch->_info != nullptr)  do_info(opBranch->_info);
 524       assert(opBranch->_result->is_illegal(), "not used");
 525       if (opBranch->_stub != nullptr)  opBranch->stub()->visit(this);
 526 
 527       break;
 528     }
 529 
 530 
 531 // LIR_OpAllocObj
 532     case lir_alloc_object:
 533     {
 534       assert(op->as_OpAllocObj() != nullptr, "must be");
 535       LIR_OpAllocObj* opAllocObj = (LIR_OpAllocObj*)op;
 536 
 537       if (opAllocObj->_info)                     do_info(opAllocObj->_info);
 538       if (opAllocObj->_opr->is_valid()) {        do_input(opAllocObj->_opr);
 539                                                  do_temp(opAllocObj->_opr);
 540                                         }
 541       if (opAllocObj->_tmp1->is_valid())         do_temp(opAllocObj->_tmp1);
 542       if (opAllocObj->_tmp2->is_valid())         do_temp(opAllocObj->_tmp2);
 543       if (opAllocObj->_tmp3->is_valid())         do_temp(opAllocObj->_tmp3);
 544       if (opAllocObj->_tmp4->is_valid())         do_temp(opAllocObj->_tmp4);
 545       if (opAllocObj->_result->is_valid())       do_output(opAllocObj->_result);
 546       if (opAllocObj->_stub != nullptr)          do_stub(opAllocObj->_stub);
 547       break;
 548     }
 549 
 550 
 551 // LIR_Op2
 552     case lir_cmp:
 553     case lir_cmp_l2i:
 554     case lir_ucmp_fd2i:
 555     case lir_cmp_fd2i:
 556     case lir_add:
 557     case lir_sub:
 558     case lir_rem:
 559     case lir_logic_and:
 560     case lir_logic_or:
 561     case lir_logic_xor:
 562     case lir_shl:
 563     case lir_shr:
 564     case lir_ushr:
 565     case lir_xadd:
 566     case lir_xchg:
 567     case lir_assert:
 568     {
 569       assert(op->as_Op2() != nullptr, "must be");
 570       LIR_Op2* op2 = (LIR_Op2*)op;
 571       assert(op2->_tmp2->is_illegal() && op2->_tmp3->is_illegal() &&
 572              op2->_tmp4->is_illegal() && op2->_tmp5->is_illegal(), "not used");
 573 
 574       if (op2->_info)                     do_info(op2->_info);
 575       if (op2->_opr1->is_valid())         do_input(op2->_opr1);
 576       if (op2->_opr2->is_valid())         do_input(op2->_opr2);
 577       if (op2->_tmp1->is_valid())         do_temp(op2->_tmp1);
 578       if (op2->_result->is_valid())       do_output(op2->_result);
 579       if (op->code() == lir_xchg || op->code() == lir_xadd) {
 580         // on ARM and PPC, return value is loaded first so could
 581         // destroy inputs. On other platforms that implement those
 582         // (x86, sparc), the extra constrainsts are harmless.
 583         if (op2->_opr1->is_valid())       do_temp(op2->_opr1);
 584         if (op2->_opr2->is_valid())       do_temp(op2->_opr2);
 585       }
 586 
 587       break;
 588     }
 589 
 590     // special handling for cmove: right input operand must not be equal
 591     // to the result operand, otherwise the backend fails
 592     case lir_cmove:
 593     {
 594       assert(op->as_Op4() != nullptr, "must be");
 595       LIR_Op4* op4 = (LIR_Op4*)op;
 596 
 597       assert(op4->_info == nullptr && op4->_tmp1->is_illegal() && op4->_tmp2->is_illegal() &&
 598              op4->_tmp3->is_illegal() && op4->_tmp4->is_illegal() && op4->_tmp5->is_illegal(), "not used");
 599       assert(op4->_opr1->is_valid() && op4->_opr2->is_valid() && op4->_result->is_valid(), "used");
 600 
 601       do_input(op4->_opr1);
 602       do_input(op4->_opr2);
 603       if (op4->_opr3->is_valid()) do_input(op4->_opr3);
 604       if (op4->_opr4->is_valid()) do_input(op4->_opr4);
 605       do_temp(op4->_opr2);
 606       do_output(op4->_result);
 607 
 608       break;
 609     }
 610 
 611     // vspecial handling for strict operations: register input operands
 612     // as temp to guarantee that they do not overlap with other
 613     // registers
 614     case lir_mul:
 615     case lir_div:
 616     {
 617       assert(op->as_Op2() != nullptr, "must be");
 618       LIR_Op2* op2 = (LIR_Op2*)op;
 619 
 620       assert(op2->_info == nullptr, "not used");
 621       assert(op2->_opr1->is_valid(), "used");
 622       assert(op2->_opr2->is_valid(), "used");
 623       assert(op2->_result->is_valid(), "used");
 624       assert(op2->_tmp2->is_illegal() && op2->_tmp3->is_illegal() &&
 625              op2->_tmp4->is_illegal() && op2->_tmp5->is_illegal(), "not used");
 626 
 627       do_input(op2->_opr1); do_temp(op2->_opr1);
 628       do_input(op2->_opr2); do_temp(op2->_opr2);
 629       if (op2->_tmp1->is_valid()) do_temp(op2->_tmp1);
 630       do_output(op2->_result);
 631 
 632       break;
 633     }
 634 
 635     case lir_throw: {
 636       assert(op->as_Op2() != nullptr, "must be");
 637       LIR_Op2* op2 = (LIR_Op2*)op;
 638 
 639       if (op2->_info)                     do_info(op2->_info);
 640       if (op2->_opr1->is_valid())         do_temp(op2->_opr1);
 641       if (op2->_opr2->is_valid())         do_input(op2->_opr2); // exception object is input parameter
 642       assert(op2->_result->is_illegal(), "no result");
 643       assert(op2->_tmp2->is_illegal() && op2->_tmp3->is_illegal() &&
 644              op2->_tmp4->is_illegal() && op2->_tmp5->is_illegal(), "not used");
 645 
 646       break;
 647     }
 648 
 649     case lir_unwind: {
 650       assert(op->as_Op1() != nullptr, "must be");
 651       LIR_Op1* op1 = (LIR_Op1*)op;
 652 
 653       assert(op1->_info == nullptr, "no info");
 654       assert(op1->_opr->is_valid(), "exception oop");         do_input(op1->_opr);
 655       assert(op1->_tmp->is_illegal(), "not used");
 656       assert(op1->_result->is_illegal(), "no result");
 657 
 658       break;
 659     }
 660 
 661 // LIR_Op3
 662     case lir_idiv:
 663     case lir_irem: {
 664       assert(op->as_Op3() != nullptr, "must be");
 665       LIR_Op3* op3= (LIR_Op3*)op;
 666 
 667       if (op3->_info)                     do_info(op3->_info);
 668       if (op3->_opr1->is_valid())         do_input(op3->_opr1);
 669 
 670       // second operand is input and temp, so ensure that second operand
 671       // and third operand get not the same register
 672       if (op3->_opr2->is_valid())         do_input(op3->_opr2);
 673       if (op3->_opr2->is_valid())         do_temp(op3->_opr2);
 674       if (op3->_opr3->is_valid())         do_temp(op3->_opr3);
 675 
 676       if (op3->_result->is_valid())       do_output(op3->_result);
 677 
 678       break;
 679     }
 680 
 681     case lir_fmad:
 682     case lir_fmaf: {
 683       assert(op->as_Op3() != nullptr, "must be");
 684       LIR_Op3* op3= (LIR_Op3*)op;
 685       assert(op3->_info == nullptr, "no info");
 686       do_input(op3->_opr1);
 687       do_input(op3->_opr2);
 688       do_input(op3->_opr3);
 689       do_output(op3->_result);
 690       break;
 691     }
 692 
 693 // LIR_OpJavaCall
 694     case lir_static_call:
 695     case lir_optvirtual_call:
 696     case lir_icvirtual_call:
 697     case lir_dynamic_call: {
 698       LIR_OpJavaCall* opJavaCall = op->as_OpJavaCall();
 699       assert(opJavaCall != nullptr, "must be");
 700 
 701       if (opJavaCall->_receiver->is_valid())     do_input(opJavaCall->_receiver);
 702 
 703       // only visit register parameters
 704       int n = opJavaCall->_arguments->length();
 705       for (int i = opJavaCall->_receiver->is_valid() ? 1 : 0; i < n; i++) {
 706         if (!opJavaCall->_arguments->at(i)->is_pointer()) {
 707           do_input(*opJavaCall->_arguments->adr_at(i));
 708         }
 709       }
 710 
 711       if (opJavaCall->_info)                     do_info(opJavaCall->_info);
 712       if (FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr &&
 713           opJavaCall->is_method_handle_invoke()) {
 714         opJavaCall->_method_handle_invoke_SP_save_opr = FrameMap::method_handle_invoke_SP_save_opr();
 715         do_temp(opJavaCall->_method_handle_invoke_SP_save_opr);
 716       }
 717       do_call();
 718       if (opJavaCall->_result->is_valid())       do_output(opJavaCall->_result);
 719 
 720       break;
 721     }
 722 
 723 
 724 // LIR_OpRTCall
 725     case lir_rtcall: {
 726       assert(op->as_OpRTCall() != nullptr, "must be");
 727       LIR_OpRTCall* opRTCall = (LIR_OpRTCall*)op;
 728 
 729       // only visit register parameters
 730       int n = opRTCall->_arguments->length();
 731       for (int i = 0; i < n; i++) {
 732         if (!opRTCall->_arguments->at(i)->is_pointer()) {
 733           do_input(*opRTCall->_arguments->adr_at(i));
 734         }
 735       }
 736       if (opRTCall->_info)                     do_info(opRTCall->_info);
 737       if (opRTCall->_tmp->is_valid())          do_temp(opRTCall->_tmp);
 738       do_call();
 739       if (opRTCall->_result->is_valid())       do_output(opRTCall->_result);
 740 
 741       break;
 742     }
 743 
 744 
 745 // LIR_OpArrayCopy
 746     case lir_arraycopy: {
 747       assert(op->as_OpArrayCopy() != nullptr, "must be");
 748       LIR_OpArrayCopy* opArrayCopy = (LIR_OpArrayCopy*)op;
 749 
 750       assert(opArrayCopy->_result->is_illegal(), "unused");
 751       assert(opArrayCopy->_src->is_valid(), "used");          do_input(opArrayCopy->_src);     do_temp(opArrayCopy->_src);
 752       assert(opArrayCopy->_src_pos->is_valid(), "used");      do_input(opArrayCopy->_src_pos); do_temp(opArrayCopy->_src_pos);
 753       assert(opArrayCopy->_dst->is_valid(), "used");          do_input(opArrayCopy->_dst);     do_temp(opArrayCopy->_dst);
 754       assert(opArrayCopy->_dst_pos->is_valid(), "used");      do_input(opArrayCopy->_dst_pos); do_temp(opArrayCopy->_dst_pos);
 755       assert(opArrayCopy->_length->is_valid(), "used");       do_input(opArrayCopy->_length);  do_temp(opArrayCopy->_length);
 756       assert(opArrayCopy->_tmp->is_valid(), "used");          do_temp(opArrayCopy->_tmp);
 757       if (opArrayCopy->_info)                     do_info(opArrayCopy->_info);
 758 
 759       // the implementation of arraycopy always has a call into the runtime
 760       do_call();
 761 
 762       break;
 763     }
 764 
 765 
 766 // LIR_OpUpdateCRC32
 767     case lir_updatecrc32: {
 768       assert(op->as_OpUpdateCRC32() != nullptr, "must be");
 769       LIR_OpUpdateCRC32* opUp = (LIR_OpUpdateCRC32*)op;
 770 
 771       assert(opUp->_crc->is_valid(), "used");          do_input(opUp->_crc);     do_temp(opUp->_crc);
 772       assert(opUp->_val->is_valid(), "used");          do_input(opUp->_val);     do_temp(opUp->_val);
 773       assert(opUp->_result->is_valid(), "used");       do_output(opUp->_result);
 774       assert(opUp->_info == nullptr, "no info for LIR_OpUpdateCRC32");
 775 
 776       break;
 777     }
 778 
 779 
 780 // LIR_OpLock
 781     case lir_lock:
 782     case lir_unlock: {
 783       assert(op->as_OpLock() != nullptr, "must be");
 784       LIR_OpLock* opLock = (LIR_OpLock*)op;
 785 
 786       if (opLock->_info)                          do_info(opLock->_info);
 787 
 788       // TODO: check if these operands really have to be temp
 789       // (or if input is sufficient). This may have influence on the oop map!
 790       assert(opLock->_lock->is_valid(), "used");  do_temp(opLock->_lock);
 791       assert(opLock->_hdr->is_valid(),  "used");  do_temp(opLock->_hdr);
 792       assert(opLock->_obj->is_valid(),  "used");  do_temp(opLock->_obj);
 793 
 794       if (opLock->_scratch->is_valid())           do_temp(opLock->_scratch);
 795       assert(opLock->_result->is_illegal(), "unused");
 796 
 797       do_stub(opLock->_stub);
 798 
 799       break;
 800     }
 801 
 802 
 803 // LIR_OpDelay
 804     case lir_delay_slot: {
 805       assert(op->as_OpDelay() != nullptr, "must be");
 806       LIR_OpDelay* opDelay = (LIR_OpDelay*)op;
 807 
 808       visit(opDelay->delay_op());
 809       break;
 810     }
 811 
 812 // LIR_OpTypeCheck
 813     case lir_instanceof:
 814     case lir_checkcast:
 815     case lir_store_check: {
 816       assert(op->as_OpTypeCheck() != nullptr, "must be");
 817       LIR_OpTypeCheck* opTypeCheck = (LIR_OpTypeCheck*)op;
 818 
 819       if (opTypeCheck->_info_for_exception)       do_info(opTypeCheck->_info_for_exception);
 820       if (opTypeCheck->_info_for_patch)           do_info(opTypeCheck->_info_for_patch);
 821       if (opTypeCheck->_object->is_valid())       do_input(opTypeCheck->_object);
 822       if (op->code() == lir_store_check && opTypeCheck->_object->is_valid()) {
 823         do_temp(opTypeCheck->_object);
 824       }
 825       if (opTypeCheck->_array->is_valid())        do_input(opTypeCheck->_array);
 826       if (opTypeCheck->_tmp1->is_valid())         do_temp(opTypeCheck->_tmp1);
 827       if (opTypeCheck->_tmp2->is_valid())         do_temp(opTypeCheck->_tmp2);
 828       if (opTypeCheck->_tmp3->is_valid())         do_temp(opTypeCheck->_tmp3);
 829       if (opTypeCheck->_result->is_valid())       do_output(opTypeCheck->_result);
 830       if (opTypeCheck->_stub != nullptr)          do_stub(opTypeCheck->_stub);
 831       break;
 832     }
 833 
 834 // LIR_OpCompareAndSwap
 835     case lir_cas_long:
 836     case lir_cas_obj:
 837     case lir_cas_int: {
 838       assert(op->as_OpCompareAndSwap() != nullptr, "must be");
 839       LIR_OpCompareAndSwap* opCmpAndSwap = (LIR_OpCompareAndSwap*)op;
 840 
 841       if (opCmpAndSwap->_info)                              do_info(opCmpAndSwap->_info);
 842       assert(opCmpAndSwap->_addr->is_valid(), "used");      do_input(opCmpAndSwap->_addr);
 843                                                             do_temp(opCmpAndSwap->_addr);
 844       assert(opCmpAndSwap->_cmp_value->is_valid(), "used"); do_input(opCmpAndSwap->_cmp_value);
 845                                                             do_temp(opCmpAndSwap->_cmp_value);
 846       assert(opCmpAndSwap->_new_value->is_valid(), "used"); do_input(opCmpAndSwap->_new_value);
 847                                                             do_temp(opCmpAndSwap->_new_value);
 848       if (opCmpAndSwap->_tmp1->is_valid())                  do_temp(opCmpAndSwap->_tmp1);
 849       if (opCmpAndSwap->_tmp2->is_valid())                  do_temp(opCmpAndSwap->_tmp2);
 850       if (opCmpAndSwap->_result->is_valid())                do_output(opCmpAndSwap->_result);
 851 
 852       break;
 853     }
 854 
 855 
 856 // LIR_OpAllocArray;
 857     case lir_alloc_array: {
 858       assert(op->as_OpAllocArray() != nullptr, "must be");
 859       LIR_OpAllocArray* opAllocArray = (LIR_OpAllocArray*)op;
 860 
 861       if (opAllocArray->_info)                        do_info(opAllocArray->_info);
 862       if (opAllocArray->_klass->is_valid()) {         do_input(opAllocArray->_klass);
 863                                                       do_temp(opAllocArray->_klass);
 864                                             }
 865       if (opAllocArray->_len->is_valid())   {         do_input(opAllocArray->_len);
 866                                                       do_temp(opAllocArray->_len);
 867                                             }
 868       if (opAllocArray->_tmp1->is_valid())            do_temp(opAllocArray->_tmp1);
 869       if (opAllocArray->_tmp2->is_valid())            do_temp(opAllocArray->_tmp2);
 870       if (opAllocArray->_tmp3->is_valid())            do_temp(opAllocArray->_tmp3);
 871       if (opAllocArray->_tmp4->is_valid())            do_temp(opAllocArray->_tmp4);
 872       if (opAllocArray->_result->is_valid())          do_output(opAllocArray->_result);
 873       if (opAllocArray->_stub != nullptr)             do_stub(opAllocArray->_stub);
 874       break;
 875     }
 876 
 877 // LIR_OpLoadKlass
 878     case lir_load_klass:
 879     {
 880       LIR_OpLoadKlass* opLoadKlass = op->as_OpLoadKlass();
 881       assert(opLoadKlass != nullptr, "must be");
 882 
 883       do_input(opLoadKlass->_obj);
 884       do_output(opLoadKlass->_result);
 885       if (opLoadKlass->_info) do_info(opLoadKlass->_info);
 886       break;
 887     }
 888 
 889 
 890 // LIR_OpProfileCall:
 891     case lir_profile_call: {
 892       assert(op->as_OpProfileCall() != nullptr, "must be");
 893       LIR_OpProfileCall* opProfileCall = (LIR_OpProfileCall*)op;
 894 
 895       if (opProfileCall->_recv->is_valid())              do_temp(opProfileCall->_recv);
 896       assert(opProfileCall->_mdo->is_valid(), "used");   do_temp(opProfileCall->_mdo);
 897       assert(opProfileCall->_tmp1->is_valid(), "used");  do_temp(opProfileCall->_tmp1);
 898       break;
 899     }
 900 
 901 // LIR_OpProfileType:
 902     case lir_profile_type: {
 903       assert(op->as_OpProfileType() != nullptr, "must be");
 904       LIR_OpProfileType* opProfileType = (LIR_OpProfileType*)op;
 905 
 906       do_input(opProfileType->_mdp); do_temp(opProfileType->_mdp);
 907       do_input(opProfileType->_obj);
 908       do_temp(opProfileType->_tmp);
 909       break;
 910     }
 911   default:
 912     op->visit(this);
 913   }
 914 }
 915 
 916 void LIR_Op::visit(LIR_OpVisitState* state) {
 917   ShouldNotReachHere();
 918 }
 919 
 920 void LIR_OpVisitState::do_stub(CodeStub* stub) {
 921   if (stub != nullptr) {
 922     stub->visit(this);
 923   }
 924 }
 925 
 926 XHandlers* LIR_OpVisitState::all_xhandler() {
 927   XHandlers* result = nullptr;
 928 
 929   int i;
 930   for (i = 0; i < info_count(); i++) {
 931     if (info_at(i)->exception_handlers() != nullptr) {
 932       result = info_at(i)->exception_handlers();
 933       break;
 934     }
 935   }
 936 
 937 #ifdef ASSERT
 938   for (i = 0; i < info_count(); i++) {
 939     assert(info_at(i)->exception_handlers() == nullptr ||
 940            info_at(i)->exception_handlers() == result,
 941            "only one xhandler list allowed per LIR-operation");
 942   }
 943 #endif
 944 
 945   if (result != nullptr) {
 946     return result;
 947   } else {
 948     return new XHandlers();
 949   }
 950 
 951   return result;
 952 }
 953 
 954 
 955 #ifdef ASSERT
 956 bool LIR_OpVisitState::no_operands(LIR_Op* op) {
 957   visit(op);
 958 
 959   return opr_count(inputMode) == 0 &&
 960          opr_count(outputMode) == 0 &&
 961          opr_count(tempMode) == 0 &&
 962          info_count() == 0 &&
 963          !has_call() &&
 964          !has_slow_case();
 965 }
 966 #endif
 967 
 968 // LIR_OpReturn
 969 LIR_OpReturn::LIR_OpReturn(LIR_Opr opr) :
 970     LIR_Op1(lir_return, opr, (CodeEmitInfo*)nullptr /* info */),
 971     _stub(nullptr) {
 972   if (VM_Version::supports_stack_watermark_barrier()) {
 973     _stub = new C1SafepointPollStub();
 974   }
 975 }
 976 
 977 //---------------------------------------------------
 978 
 979 
 980 void LIR_OpJavaCall::emit_code(LIR_Assembler* masm) {
 981   masm->emit_call(this);
 982 }
 983 
 984 void LIR_OpRTCall::emit_code(LIR_Assembler* masm) {
 985   masm->emit_rtcall(this);
 986 }
 987 
 988 void LIR_OpLabel::emit_code(LIR_Assembler* masm) {
 989   masm->emit_opLabel(this);
 990 }
 991 
 992 void LIR_OpArrayCopy::emit_code(LIR_Assembler* masm) {
 993   masm->emit_arraycopy(this);
 994   ArrayCopyStub* code_stub = stub();
 995   if (code_stub != nullptr) {
 996     masm->append_code_stub(code_stub);
 997   }
 998 }
 999 
1000 void LIR_OpUpdateCRC32::emit_code(LIR_Assembler* masm) {
1001   masm->emit_updatecrc32(this);
1002 }
1003 
1004 void LIR_Op0::emit_code(LIR_Assembler* masm) {
1005   masm->emit_op0(this);
1006 }
1007 
1008 void LIR_Op1::emit_code(LIR_Assembler* masm) {
1009   masm->emit_op1(this);
1010 }
1011 
1012 void LIR_OpAllocObj::emit_code(LIR_Assembler* masm) {
1013   masm->emit_alloc_obj(this);
1014   masm->append_code_stub(stub());
1015 }
1016 
1017 void LIR_OpBranch::emit_code(LIR_Assembler* masm) {
1018   masm->emit_opBranch(this);
1019   if (stub()) {
1020     masm->append_code_stub(stub());
1021   }
1022 }
1023 
1024 void LIR_OpConvert::emit_code(LIR_Assembler* masm) {
1025   masm->emit_opConvert(this);
1026   if (stub() != nullptr) {
1027     masm->append_code_stub(stub());
1028   }
1029 }
1030 
1031 void LIR_Op2::emit_code(LIR_Assembler* masm) {
1032   masm->emit_op2(this);
1033 }
1034 
1035 void LIR_OpAllocArray::emit_code(LIR_Assembler* masm) {
1036   masm->emit_alloc_array(this);
1037   masm->append_code_stub(stub());
1038 }
1039 
1040 void LIR_OpTypeCheck::emit_code(LIR_Assembler* masm) {
1041   masm->emit_opTypeCheck(this);
1042   if (stub()) {
1043     masm->append_code_stub(stub());
1044   }
1045 }
1046 
1047 void LIR_OpCompareAndSwap::emit_code(LIR_Assembler* masm) {
1048   masm->emit_compare_and_swap(this);
1049 }
1050 
1051 void LIR_Op3::emit_code(LIR_Assembler* masm) {
1052   masm->emit_op3(this);
1053 }
1054 
1055 void LIR_Op4::emit_code(LIR_Assembler* masm) {
1056   masm->emit_op4(this);
1057 }
1058 
1059 void LIR_OpLock::emit_code(LIR_Assembler* masm) {
1060   masm->emit_lock(this);
1061   if (stub()) {
1062     masm->append_code_stub(stub());
1063   }
1064 }
1065 
1066 void LIR_OpLoadKlass::emit_code(LIR_Assembler* masm) {
1067   masm->emit_load_klass(this);
1068 }
1069 
1070 #ifdef ASSERT
1071 void LIR_OpAssert::emit_code(LIR_Assembler* masm) {
1072   masm->emit_assert(this);
1073 }
1074 #endif
1075 
1076 void LIR_OpDelay::emit_code(LIR_Assembler* masm) {
1077   masm->emit_delay(this);
1078 }
1079 
1080 void LIR_OpProfileCall::emit_code(LIR_Assembler* masm) {
1081   masm->emit_profile_call(this);
1082 }
1083 
1084 void LIR_OpProfileType::emit_code(LIR_Assembler* masm) {
1085   masm->emit_profile_type(this);
1086 }
1087 
1088 // LIR_List
1089 LIR_List::LIR_List(Compilation* compilation, BlockBegin* block)
1090   : _operations(8)
1091   , _compilation(compilation)
1092 #ifndef PRODUCT
1093   , _block(block)
1094 #endif
1095 #ifdef ASSERT
1096   , _file(nullptr)
1097   , _line(0)
1098 #endif
1099 #ifdef RISCV
1100   , _cmp_opr1(LIR_OprFact::illegalOpr)
1101   , _cmp_opr2(LIR_OprFact::illegalOpr)
1102 #endif
1103 { }
1104 
1105 
1106 #ifdef ASSERT
1107 void LIR_List::set_file_and_line(const char * file, int line) {
1108   const char * f = strrchr(file, '/');
1109   if (f == nullptr) f = strrchr(file, '\\');
1110   if (f == nullptr) {
1111     f = file;
1112   } else {
1113     f++;
1114   }
1115   _file = f;
1116   _line = line;
1117 }
1118 #endif
1119 
1120 #ifdef RISCV
1121 void LIR_List::set_cmp_oprs(LIR_Op* op) {
1122   switch (op->code()) {
1123     case lir_cmp:
1124       _cmp_opr1 = op->as_Op2()->in_opr1();
1125       _cmp_opr2 = op->as_Op2()->in_opr2();
1126       break;
1127     case lir_branch: // fall through
1128     case lir_cond_float_branch:
1129       assert(op->as_OpBranch()->cond() == lir_cond_always ||
1130             (_cmp_opr1 != LIR_OprFact::illegalOpr && _cmp_opr2 != LIR_OprFact::illegalOpr),
1131             "conditional branches must have legal operands");
1132       if (op->as_OpBranch()->cond() != lir_cond_always) {
1133         op->as_Op2()->set_in_opr1(_cmp_opr1);
1134         op->as_Op2()->set_in_opr2(_cmp_opr2);
1135       }
1136       break;
1137     case lir_cmove:
1138       op->as_Op4()->set_in_opr3(_cmp_opr1);
1139       op->as_Op4()->set_in_opr4(_cmp_opr2);
1140       break;
1141     case lir_cas_long:
1142     case lir_cas_obj:
1143     case lir_cas_int:
1144       _cmp_opr1 = op->as_OpCompareAndSwap()->result_opr();
1145       _cmp_opr2 = LIR_OprFact::intConst(0);
1146       break;
1147 #if INCLUDE_ZGC
1148     case lir_xloadbarrier_test:
1149       _cmp_opr1 = FrameMap::as_opr(t1);
1150       _cmp_opr2 = LIR_OprFact::intConst(0);
1151       break;
1152 #endif
1153     default:
1154       break;
1155   }
1156 }
1157 #endif
1158 
1159 void LIR_List::append(LIR_InsertionBuffer* buffer) {
1160   assert(this == buffer->lir_list(), "wrong lir list");
1161   const int n = _operations.length();
1162 
1163   if (buffer->number_of_ops() > 0) {
1164     // increase size of instructions list
1165     _operations.at_grow(n + buffer->number_of_ops() - 1, nullptr);
1166     // insert ops from buffer into instructions list
1167     int op_index = buffer->number_of_ops() - 1;
1168     int ip_index = buffer->number_of_insertion_points() - 1;
1169     int from_index = n - 1;
1170     int to_index = _operations.length() - 1;
1171     for (; ip_index >= 0; ip_index --) {
1172       int index = buffer->index_at(ip_index);
1173       // make room after insertion point
1174       while (index < from_index) {
1175         _operations.at_put(to_index --, _operations.at(from_index --));
1176       }
1177       // insert ops from buffer
1178       for (int i = buffer->count_at(ip_index); i > 0; i --) {
1179         _operations.at_put(to_index --, buffer->op_at(op_index --));
1180       }
1181     }
1182   }
1183 
1184   buffer->finish();
1185 }
1186 
1187 
1188 void LIR_List::oop2reg_patch(jobject o, LIR_Opr reg, CodeEmitInfo* info) {
1189   assert(reg->type() == T_OBJECT, "bad reg");
1190   append(new LIR_Op1(lir_move, LIR_OprFact::oopConst(o),  reg, T_OBJECT, lir_patch_normal, info));
1191 }
1192 
1193 void LIR_List::klass2reg_patch(Metadata* o, LIR_Opr reg, CodeEmitInfo* info) {
1194   assert(reg->type() == T_METADATA, "bad reg");
1195   append(new LIR_Op1(lir_move, LIR_OprFact::metadataConst(o), reg, T_METADATA, lir_patch_normal, info));
1196 }
1197 
1198 void LIR_List::load(LIR_Address* addr, LIR_Opr src, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1199   append(new LIR_Op1(
1200             lir_move,
1201             LIR_OprFact::address(addr),
1202             src,
1203             addr->type(),
1204             patch_code,
1205             info));
1206 }
1207 
1208 
1209 void LIR_List::volatile_load_mem_reg(LIR_Address* address, LIR_Opr dst, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1210   append(new LIR_Op1(
1211             lir_move,
1212             LIR_OprFact::address(address),
1213             dst,
1214             address->type(),
1215             patch_code,
1216             info, lir_move_volatile));
1217 }
1218 
1219 void LIR_List::volatile_load_unsafe_reg(LIR_Opr base, LIR_Opr offset, LIR_Opr dst, BasicType type, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1220   append(new LIR_Op1(
1221             lir_move,
1222             LIR_OprFact::address(new LIR_Address(base, offset, type)),
1223             dst,
1224             type,
1225             patch_code,
1226             info, lir_move_volatile));
1227 }
1228 
1229 
1230 void LIR_List::store_mem_int(jint v, LIR_Opr base, int offset_in_bytes, BasicType type, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1231   append(new LIR_Op1(
1232             lir_move,
1233             LIR_OprFact::intConst(v),
1234             LIR_OprFact::address(new LIR_Address(base, offset_in_bytes, type)),
1235             type,
1236             patch_code,
1237             info));
1238 }
1239 
1240 
1241 void LIR_List::store_mem_oop(jobject o, LIR_Opr base, int offset_in_bytes, BasicType type, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1242   append(new LIR_Op1(
1243             lir_move,
1244             LIR_OprFact::oopConst(o),
1245             LIR_OprFact::address(new LIR_Address(base, offset_in_bytes, type)),
1246             type,
1247             patch_code,
1248             info));
1249 }
1250 
1251 
1252 void LIR_List::store(LIR_Opr src, LIR_Address* addr, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1253   append(new LIR_Op1(
1254             lir_move,
1255             src,
1256             LIR_OprFact::address(addr),
1257             addr->type(),
1258             patch_code,
1259             info));
1260 }
1261 
1262 
1263 void LIR_List::volatile_store_mem_reg(LIR_Opr src, LIR_Address* addr, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1264   append(new LIR_Op1(
1265             lir_move,
1266             src,
1267             LIR_OprFact::address(addr),
1268             addr->type(),
1269             patch_code,
1270             info,
1271             lir_move_volatile));
1272 }
1273 
1274 void LIR_List::volatile_store_unsafe_reg(LIR_Opr src, LIR_Opr base, LIR_Opr offset, BasicType type, CodeEmitInfo* info, LIR_PatchCode patch_code) {
1275   append(new LIR_Op1(
1276             lir_move,
1277             src,
1278             LIR_OprFact::address(new LIR_Address(base, offset, type)),
1279             type,
1280             patch_code,
1281             info, lir_move_volatile));
1282 }
1283 
1284 
1285 void LIR_List::idiv(LIR_Opr left, LIR_Opr right, LIR_Opr res, LIR_Opr tmp, CodeEmitInfo* info) {
1286   append(new LIR_Op3(
1287                     lir_idiv,
1288                     left,
1289                     right,
1290                     tmp,
1291                     res,
1292                     info));
1293 }
1294 
1295 
1296 void LIR_List::idiv(LIR_Opr left, int right, LIR_Opr res, LIR_Opr tmp, CodeEmitInfo* info) {
1297   append(new LIR_Op3(
1298                     lir_idiv,
1299                     left,
1300                     LIR_OprFact::intConst(right),
1301                     tmp,
1302                     res,
1303                     info));
1304 }
1305 
1306 
1307 void LIR_List::irem(LIR_Opr left, LIR_Opr right, LIR_Opr res, LIR_Opr tmp, CodeEmitInfo* info) {
1308   append(new LIR_Op3(
1309                     lir_irem,
1310                     left,
1311                     right,
1312                     tmp,
1313                     res,
1314                     info));
1315 }
1316 
1317 
1318 void LIR_List::irem(LIR_Opr left, int right, LIR_Opr res, LIR_Opr tmp, CodeEmitInfo* info) {
1319   append(new LIR_Op3(
1320                     lir_irem,
1321                     left,
1322                     LIR_OprFact::intConst(right),
1323                     tmp,
1324                     res,
1325                     info));
1326 }
1327 
1328 
1329 void LIR_List::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
1330   append(new LIR_Op2(
1331                     lir_cmp,
1332                     condition,
1333                     LIR_OprFact::address(new LIR_Address(base, disp, T_INT)),
1334                     LIR_OprFact::intConst(c),
1335                     info));
1336 }
1337 
1338 
1339 void LIR_List::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Address* addr, CodeEmitInfo* info) {
1340   append(new LIR_Op2(
1341                     lir_cmp,
1342                     condition,
1343                     reg,
1344                     LIR_OprFact::address(addr),
1345                     info));
1346 }
1347 
1348 void LIR_List::allocate_object(LIR_Opr dst, LIR_Opr t1, LIR_Opr t2, LIR_Opr t3, LIR_Opr t4,
1349                                int header_size, int object_size, LIR_Opr klass, bool init_check, CodeStub* stub) {
1350   append(new LIR_OpAllocObj(
1351                            klass,
1352                            dst,
1353                            t1,
1354                            t2,
1355                            t3,
1356                            t4,
1357                            header_size,
1358                            object_size,
1359                            init_check,
1360                            stub));
1361 }
1362 
1363 void LIR_List::allocate_array(LIR_Opr dst, LIR_Opr len, LIR_Opr t1,LIR_Opr t2, LIR_Opr t3,LIR_Opr t4, BasicType type, LIR_Opr klass, CodeStub* stub, bool zero_array) {
1364   append(new LIR_OpAllocArray(
1365                            klass,
1366                            len,
1367                            dst,
1368                            t1,
1369                            t2,
1370                            t3,
1371                            t4,
1372                            type,
1373                            stub,
1374                            zero_array));
1375 }
1376 
1377 void LIR_List::shift_left(LIR_Opr value, LIR_Opr count, LIR_Opr dst, LIR_Opr tmp) {
1378  append(new LIR_Op2(
1379                     lir_shl,
1380                     value,
1381                     count,
1382                     dst,
1383                     tmp));
1384 }
1385 
1386 void LIR_List::shift_right(LIR_Opr value, LIR_Opr count, LIR_Opr dst, LIR_Opr tmp) {
1387  append(new LIR_Op2(
1388                     lir_shr,
1389                     value,
1390                     count,
1391                     dst,
1392                     tmp));
1393 }
1394 
1395 
1396 void LIR_List::unsigned_shift_right(LIR_Opr value, LIR_Opr count, LIR_Opr dst, LIR_Opr tmp) {
1397  append(new LIR_Op2(
1398                     lir_ushr,
1399                     value,
1400                     count,
1401                     dst,
1402                     tmp));
1403 }
1404 
1405 void LIR_List::fcmp2int(LIR_Opr left, LIR_Opr right, LIR_Opr dst, bool is_unordered_less) {
1406   append(new LIR_Op2(is_unordered_less ? lir_ucmp_fd2i : lir_cmp_fd2i,
1407                      left,
1408                      right,
1409                      dst));
1410 }
1411 
1412 void LIR_List::lock_object(LIR_Opr hdr, LIR_Opr obj, LIR_Opr lock, LIR_Opr scratch, CodeStub* stub, CodeEmitInfo* info) {
1413   append(new LIR_OpLock(
1414                     lir_lock,
1415                     hdr,
1416                     obj,
1417                     lock,
1418                     scratch,
1419                     stub,
1420                     info));
1421 }
1422 
1423 void LIR_List::unlock_object(LIR_Opr hdr, LIR_Opr obj, LIR_Opr lock, LIR_Opr scratch, CodeStub* stub) {
1424   append(new LIR_OpLock(
1425                     lir_unlock,
1426                     hdr,
1427                     obj,
1428                     lock,
1429                     scratch,
1430                     stub,
1431                     nullptr));
1432 }
1433 
1434 
1435 void check_LIR() {
1436   // cannot do the proper checking as PRODUCT and other modes return different results
1437   // guarantee(sizeof(LIR_Opr) == wordSize, "may not have a v-table");
1438 }
1439 
1440 
1441 
1442 void LIR_List::checkcast (LIR_Opr result, LIR_Opr object, ciKlass* klass,
1443                           LIR_Opr tmp1, LIR_Opr tmp2, LIR_Opr tmp3, bool fast_check,
1444                           CodeEmitInfo* info_for_exception, CodeEmitInfo* info_for_patch, CodeStub* stub,
1445                           ciMethod* profiled_method, int profiled_bci) {
1446   LIR_OpTypeCheck* c = new LIR_OpTypeCheck(lir_checkcast, result, object, klass,
1447                                            tmp1, tmp2, tmp3, fast_check, info_for_exception, info_for_patch, stub);
1448   if (profiled_method != nullptr && TypeProfileCasts) {
1449     c->set_profiled_method(profiled_method);
1450     c->set_profiled_bci(profiled_bci);
1451     c->set_should_profile(true);
1452   }
1453   append(c);
1454 }
1455 
1456 void LIR_List::instanceof(LIR_Opr result, LIR_Opr object, ciKlass* klass, LIR_Opr tmp1, LIR_Opr tmp2, LIR_Opr tmp3, bool fast_check, CodeEmitInfo* info_for_patch, ciMethod* profiled_method, int profiled_bci) {
1457   LIR_OpTypeCheck* c = new LIR_OpTypeCheck(lir_instanceof, result, object, klass, tmp1, tmp2, tmp3, fast_check, nullptr, info_for_patch, nullptr);
1458   if (profiled_method != nullptr && TypeProfileCasts) {
1459     c->set_profiled_method(profiled_method);
1460     c->set_profiled_bci(profiled_bci);
1461     c->set_should_profile(true);
1462   }
1463   append(c);
1464 }
1465 
1466 
1467 void LIR_List::store_check(LIR_Opr object, LIR_Opr array, LIR_Opr tmp1, LIR_Opr tmp2, LIR_Opr tmp3,
1468                            CodeEmitInfo* info_for_exception, ciMethod* profiled_method, int profiled_bci) {
1469   LIR_OpTypeCheck* c = new LIR_OpTypeCheck(lir_store_check, object, array, tmp1, tmp2, tmp3, info_for_exception);
1470   if (profiled_method != nullptr && TypeProfileCasts) {
1471     c->set_profiled_method(profiled_method);
1472     c->set_profiled_bci(profiled_bci);
1473     c->set_should_profile(true);
1474   }
1475   append(c);
1476 }
1477 
1478 void LIR_List::null_check(LIR_Opr opr, CodeEmitInfo* info, bool deoptimize_on_null) {
1479   if (deoptimize_on_null) {
1480     // Emit an explicit null check and deoptimize if opr is null
1481     CodeStub* deopt = new DeoptimizeStub(info, Deoptimization::Reason_null_check, Deoptimization::Action_none);
1482     cmp(lir_cond_equal, opr, LIR_OprFact::oopConst(nullptr));
1483     branch(lir_cond_equal, deopt);
1484   } else {
1485     // Emit an implicit null check
1486     append(new LIR_Op1(lir_null_check, opr, info));
1487   }
1488 }
1489 
1490 void LIR_List::cas_long(LIR_Opr addr, LIR_Opr cmp_value, LIR_Opr new_value,
1491                         LIR_Opr t1, LIR_Opr t2, LIR_Opr result) {
1492   append(new LIR_OpCompareAndSwap(lir_cas_long, addr, cmp_value, new_value, t1, t2, result));
1493 }
1494 
1495 void LIR_List::cas_obj(LIR_Opr addr, LIR_Opr cmp_value, LIR_Opr new_value,
1496                        LIR_Opr t1, LIR_Opr t2, LIR_Opr result) {
1497   append(new LIR_OpCompareAndSwap(lir_cas_obj, addr, cmp_value, new_value, t1, t2, result));
1498 }
1499 
1500 void LIR_List::cas_int(LIR_Opr addr, LIR_Opr cmp_value, LIR_Opr new_value,
1501                        LIR_Opr t1, LIR_Opr t2, LIR_Opr result) {
1502   append(new LIR_OpCompareAndSwap(lir_cas_int, addr, cmp_value, new_value, t1, t2, result));
1503 }
1504 
1505 
1506 #ifdef PRODUCT
1507 
1508 void print_LIR(BlockList* blocks) {
1509 }
1510 
1511 #else
1512 // LIR_Opr
1513 void LIR_Opr::print() const {
1514   print(tty);
1515 }
1516 
1517 void LIR_Opr::print(outputStream* out) const {
1518   if (is_illegal()) {
1519     return;
1520   }
1521 
1522   out->print("[");
1523   if (is_pointer()) {
1524     pointer()->print_value_on(out);
1525   } else if (is_single_stack()) {
1526     out->print("stack:%d", single_stack_ix());
1527   } else if (is_double_stack()) {
1528     out->print("dbl_stack:%d",double_stack_ix());
1529   } else if (is_virtual()) {
1530     out->print("R%d", vreg_number());
1531   } else if (is_single_cpu()) {
1532     out->print("%s", as_register()->name());
1533   } else if (is_double_cpu()) {
1534     out->print("%s", as_register_hi()->name());
1535     out->print("%s", as_register_lo()->name());
1536 #if defined(X86)
1537   } else if (is_single_xmm()) {
1538     out->print("%s", as_xmm_float_reg()->name());
1539   } else if (is_double_xmm()) {
1540     out->print("%s", as_xmm_double_reg()->name());
1541   } else if (is_single_fpu()) {
1542     out->print("fpu%d", fpu_regnr());
1543   } else if (is_double_fpu()) {
1544     out->print("fpu%d", fpu_regnrLo());
1545 #elif defined(AARCH64)
1546   } else if (is_single_fpu()) {
1547     out->print("fpu%d", fpu_regnr());
1548   } else if (is_double_fpu()) {
1549     out->print("fpu%d", fpu_regnrLo());
1550 #elif defined(ARM)
1551   } else if (is_single_fpu()) {
1552     out->print("s%d", fpu_regnr());
1553   } else if (is_double_fpu()) {
1554     out->print("d%d", fpu_regnrLo() >> 1);
1555 #else
1556   } else if (is_single_fpu()) {
1557     out->print("%s", as_float_reg()->name());
1558   } else if (is_double_fpu()) {
1559     out->print("%s", as_double_reg()->name());
1560 #endif
1561 
1562   } else if (is_illegal()) {
1563     out->print("-");
1564   } else {
1565     out->print("Unknown Operand");
1566   }
1567   if (!is_illegal()) {
1568     out->print("|%c", type_char());
1569   }
1570   if (is_register() && is_last_use()) {
1571     out->print("(last_use)");
1572   }
1573   out->print("]");
1574 }
1575 
1576 
1577 // LIR_Address
1578 void LIR_Const::print_value_on(outputStream* out) const {
1579   switch (type()) {
1580     case T_ADDRESS:out->print("address:%d",as_jint());          break;
1581     case T_INT:    out->print("int:%d",   as_jint());           break;
1582     case T_LONG:   out->print("lng:" JLONG_FORMAT, as_jlong()); break;
1583     case T_FLOAT:  out->print("flt:%f",   as_jfloat());         break;
1584     case T_DOUBLE: out->print("dbl:%f",   as_jdouble());        break;
1585     case T_OBJECT: out->print("obj:" INTPTR_FORMAT, p2i(as_jobject()));        break;
1586     case T_METADATA: out->print("metadata:" INTPTR_FORMAT, p2i(as_metadata()));break;
1587     default:       out->print("%3d:" UINT64_FORMAT_X, type(), (uint64_t)as_jlong()); break;
1588   }
1589 }
1590 
1591 // LIR_Address
1592 void LIR_Address::print_value_on(outputStream* out) const {
1593   out->print("Base:"); _base->print(out);
1594   if (!_index->is_illegal()) {
1595     out->print(" Index:"); _index->print(out);
1596     switch (scale()) {
1597     case times_1: break;
1598     case times_2: out->print(" * 2"); break;
1599     case times_4: out->print(" * 4"); break;
1600     case times_8: out->print(" * 8"); break;
1601     }
1602   }
1603   out->print(" Disp: %zd", _disp);
1604 }
1605 
1606 // debug output of block header without InstructionPrinter
1607 //       (because phi functions are not necessary for LIR)
1608 static void print_block(BlockBegin* x) {
1609   // print block id
1610   BlockEnd* end = x->end();
1611   tty->print("B%d ", x->block_id());
1612 
1613   // print flags
1614   if (x->is_set(BlockBegin::std_entry_flag))               tty->print("std ");
1615   if (x->is_set(BlockBegin::osr_entry_flag))               tty->print("osr ");
1616   if (x->is_set(BlockBegin::exception_entry_flag))         tty->print("ex ");
1617   if (x->is_set(BlockBegin::subroutine_entry_flag))        tty->print("jsr ");
1618   if (x->is_set(BlockBegin::backward_branch_target_flag))  tty->print("bb ");
1619   if (x->is_set(BlockBegin::linear_scan_loop_header_flag)) tty->print("lh ");
1620   if (x->is_set(BlockBegin::linear_scan_loop_end_flag))    tty->print("le ");
1621 
1622   // print block bci range
1623   tty->print("[%d, %d] ", x->bci(), (end == nullptr ? -1 : end->printable_bci()));
1624 
1625   // print predecessors and successors
1626   if (x->number_of_preds() > 0) {
1627     tty->print("preds: ");
1628     for (int i = 0; i < x->number_of_preds(); i ++) {
1629       tty->print("B%d ", x->pred_at(i)->block_id());
1630     }
1631   }
1632 
1633   if (end != nullptr && x->number_of_sux() > 0) {
1634     tty->print("sux: ");
1635     for (int i = 0; i < x->number_of_sux(); i ++) {
1636       tty->print("B%d ", x->sux_at(i)->block_id());
1637     }
1638   }
1639 
1640   // print exception handlers
1641   if (x->number_of_exception_handlers() > 0) {
1642     tty->print("xhandler: ");
1643     for (int i = 0; i < x->number_of_exception_handlers();  i++) {
1644       tty->print("B%d ", x->exception_handler_at(i)->block_id());
1645     }
1646   }
1647 
1648   tty->cr();
1649 }
1650 
1651 void print_LIR(BlockList* blocks) {
1652   tty->print_cr("LIR:");
1653   int i;
1654   for (i = 0; i < blocks->length(); i++) {
1655     BlockBegin* bb = blocks->at(i);
1656     print_block(bb);
1657     tty->print("__id_Instruction___________________________________________"); tty->cr();
1658     bb->lir()->print_instructions();
1659   }
1660 }
1661 
1662 void LIR_List::print_instructions() {
1663   for (int i = 0; i < _operations.length(); i++) {
1664     _operations.at(i)->print(); tty->cr();
1665   }
1666   tty->cr();
1667 }
1668 
1669 // LIR_Ops printing routines
1670 // LIR_Op
1671 void LIR_Op::print_on(outputStream* out) const {
1672   if (id() != -1 || PrintCFGToFile) {
1673     out->print("%4d ", id());
1674   } else {
1675     out->print("     ");
1676   }
1677   out->print("%s ", name());
1678   print_instr(out);
1679   if (info() != nullptr) out->print(" [bci:%d]", info()->stack()->bci());
1680 #ifdef ASSERT
1681   if (Verbose && _file != nullptr) {
1682     out->print(" (%s:%d)", _file, _line);
1683   }
1684 #endif
1685 }
1686 
1687 const char * LIR_Op::name() const {
1688   const char* s = nullptr;
1689   switch(code()) {
1690      // LIR_Op0
1691      case lir_membar:                s = "membar";        break;
1692      case lir_membar_acquire:        s = "membar_acquire"; break;
1693      case lir_membar_release:        s = "membar_release"; break;
1694      case lir_membar_loadload:       s = "membar_loadload";   break;
1695      case lir_membar_storestore:     s = "membar_storestore"; break;
1696      case lir_membar_loadstore:      s = "membar_loadstore";  break;
1697      case lir_membar_storeload:      s = "membar_storeload";  break;
1698      case lir_label:                 s = "label";         break;
1699      case lir_nop:                   s = "nop";           break;
1700      case lir_on_spin_wait:          s = "on_spin_wait";  break;
1701      case lir_std_entry:             s = "std_entry";     break;
1702      case lir_osr_entry:             s = "osr_entry";     break;
1703      case lir_breakpoint:            s = "breakpoint";    break;
1704      case lir_get_thread:            s = "get_thread";    break;
1705      // LIR_Op1
1706      case lir_push:                  s = "push";          break;
1707      case lir_pop:                   s = "pop";           break;
1708      case lir_null_check:            s = "null_check";    break;
1709      case lir_return:                s = "return";        break;
1710      case lir_safepoint:             s = "safepoint";     break;
1711      case lir_leal:                  s = "leal";          break;
1712      case lir_branch:                s = "branch";        break;
1713      case lir_cond_float_branch:     s = "flt_cond_br";   break;
1714      case lir_move:                  s = "move";          break;
1715      case lir_abs:                   s = "abs";           break;
1716      case lir_neg:                   s = "neg";           break;
1717      case lir_sqrt:                  s = "sqrt";          break;
1718      case lir_f2hf:                  s = "f2hf";          break;
1719      case lir_hf2f:                  s = "hf2f";          break;
1720      case lir_rtcall:                s = "rtcall";        break;
1721      case lir_throw:                 s = "throw";         break;
1722      case lir_unwind:                s = "unwind";        break;
1723      case lir_convert:               s = "convert";       break;
1724      case lir_alloc_object:          s = "alloc_obj";     break;
1725      case lir_monaddr:               s = "mon_addr";      break;
1726      // LIR_Op2
1727      case lir_cmp:                   s = "cmp";           break;
1728      case lir_cmp_l2i:               s = "cmp_l2i";       break;
1729      case lir_ucmp_fd2i:             s = "ucomp_fd2i";    break;
1730      case lir_cmp_fd2i:              s = "comp_fd2i";     break;
1731      case lir_add:                   s = "add";           break;
1732      case lir_sub:                   s = "sub";           break;
1733      case lir_mul:                   s = "mul";           break;
1734      case lir_div:                   s = "div";           break;
1735      case lir_rem:                   s = "rem";           break;
1736      case lir_logic_and:             s = "logic_and";     break;
1737      case lir_logic_or:              s = "logic_or";      break;
1738      case lir_logic_xor:             s = "logic_xor";     break;
1739      case lir_shl:                   s = "shift_left";    break;
1740      case lir_shr:                   s = "shift_right";   break;
1741      case lir_ushr:                  s = "ushift_right";  break;
1742      case lir_alloc_array:           s = "alloc_array";   break;
1743      case lir_xadd:                  s = "xadd";          break;
1744      case lir_xchg:                  s = "xchg";          break;
1745      // LIR_Op3
1746      case lir_idiv:                  s = "idiv";          break;
1747      case lir_irem:                  s = "irem";          break;
1748      case lir_fmad:                  s = "fmad";          break;
1749      case lir_fmaf:                  s = "fmaf";          break;
1750      // LIR_Op4
1751      case lir_cmove:                 s = "cmove";         break;
1752      // LIR_OpJavaCall
1753      case lir_static_call:           s = "static";        break;
1754      case lir_optvirtual_call:       s = "optvirtual";    break;
1755      case lir_icvirtual_call:        s = "icvirtual";     break;
1756      case lir_dynamic_call:          s = "dynamic";       break;
1757      // LIR_OpArrayCopy
1758      case lir_arraycopy:             s = "arraycopy";     break;
1759      // LIR_OpUpdateCRC32
1760      case lir_updatecrc32:           s = "updatecrc32";   break;
1761      // LIR_OpLock
1762      case lir_lock:                  s = "lock";          break;
1763      case lir_unlock:                s = "unlock";        break;
1764      // LIR_OpDelay
1765      case lir_delay_slot:            s = "delay";         break;
1766      // LIR_OpTypeCheck
1767      case lir_instanceof:            s = "instanceof";    break;
1768      case lir_checkcast:             s = "checkcast";     break;
1769      case lir_store_check:           s = "store_check";   break;
1770      // LIR_OpCompareAndSwap
1771      case lir_cas_long:              s = "cas_long";      break;
1772      case lir_cas_obj:               s = "cas_obj";      break;
1773      case lir_cas_int:               s = "cas_int";      break;
1774      // LIR_OpProfileCall
1775      case lir_profile_call:          s = "profile_call";  break;
1776      // LIR_OpProfileType
1777      case lir_profile_type:          s = "profile_type";  break;
1778      // LIR_OpAssert
1779 #ifdef ASSERT
1780      case lir_assert:                s = "assert";        break;
1781 #endif
1782      case lir_none:                  ShouldNotReachHere();break;
1783     default:                         s = "illegal_op";    break;
1784   }
1785   return s;
1786 }
1787 
1788 // LIR_OpJavaCall
1789 void LIR_OpJavaCall::print_instr(outputStream* out) const {
1790   out->print("call: ");
1791   out->print("[addr: " INTPTR_FORMAT "]", p2i(address()));
1792   if (receiver()->is_valid()) {
1793     out->print(" [recv: ");   receiver()->print(out);   out->print("]");
1794   }
1795   if (result_opr()->is_valid()) {
1796     out->print(" [result: "); result_opr()->print(out); out->print("]");
1797   }
1798 }
1799 
1800 // LIR_OpLabel
1801 void LIR_OpLabel::print_instr(outputStream* out) const {
1802   out->print("[label:" INTPTR_FORMAT "]", p2i(_label));
1803 }
1804 
1805 // LIR_OpArrayCopy
1806 void LIR_OpArrayCopy::print_instr(outputStream* out) const {
1807   src()->print(out);     out->print(" ");
1808   src_pos()->print(out); out->print(" ");
1809   dst()->print(out);     out->print(" ");
1810   dst_pos()->print(out); out->print(" ");
1811   length()->print(out);  out->print(" ");
1812   tmp()->print(out);     out->print(" ");
1813 }
1814 
1815 // LIR_OpUpdateCRC32
1816 void LIR_OpUpdateCRC32::print_instr(outputStream* out) const {
1817   crc()->print(out);     out->print(" ");
1818   val()->print(out);     out->print(" ");
1819   result_opr()->print(out); out->print(" ");
1820 }
1821 
1822 // LIR_OpCompareAndSwap
1823 void LIR_OpCompareAndSwap::print_instr(outputStream* out) const {
1824   addr()->print(out);      out->print(" ");
1825   cmp_value()->print(out); out->print(" ");
1826   new_value()->print(out); out->print(" ");
1827   tmp1()->print(out);      out->print(" ");
1828   tmp2()->print(out);      out->print(" ");
1829 
1830 }
1831 
1832 // LIR_Op0
1833 void LIR_Op0::print_instr(outputStream* out) const {
1834   result_opr()->print(out);
1835 }
1836 
1837 // LIR_Op1
1838 const char * LIR_Op1::name() const {
1839   if (code() == lir_move) {
1840     switch (move_kind()) {
1841     case lir_move_normal:
1842       return "move";
1843     case lir_move_volatile:
1844       return "volatile_move";
1845     case lir_move_wide:
1846       return "wide_move";
1847     default:
1848       ShouldNotReachHere();
1849     return "illegal_op";
1850     }
1851   } else {
1852     return LIR_Op::name();
1853   }
1854 }
1855 
1856 
1857 void LIR_Op1::print_instr(outputStream* out) const {
1858   _opr->print(out);         out->print(" ");
1859   result_opr()->print(out); out->print(" ");
1860   print_patch_code(out, patch_code());
1861 }
1862 
1863 
1864 // LIR_Op1
1865 void LIR_OpRTCall::print_instr(outputStream* out) const {
1866   intx a = (intx)addr();
1867   out->print("%s", Runtime1::name_for_address(addr()));
1868   out->print(" ");
1869   tmp()->print(out);
1870 }
1871 
1872 void LIR_Op1::print_patch_code(outputStream* out, LIR_PatchCode code) {
1873   switch(code) {
1874     case lir_patch_none:                                 break;
1875     case lir_patch_low:    out->print("[patch_low]");    break;
1876     case lir_patch_high:   out->print("[patch_high]");   break;
1877     case lir_patch_normal: out->print("[patch_normal]"); break;
1878     default: ShouldNotReachHere();
1879   }
1880 }
1881 
1882 // LIR_OpBranch
1883 void LIR_OpBranch::print_instr(outputStream* out) const {
1884   print_condition(out, cond());             out->print(" ");
1885   in_opr1()->print(out); out->print(" ");
1886   in_opr2()->print(out); out->print(" ");
1887   if (block() != nullptr) {
1888     out->print("[B%d] ", block()->block_id());
1889   } else if (stub() != nullptr) {
1890     out->print("[");
1891     stub()->print_name(out);
1892     out->print(": " INTPTR_FORMAT "]", p2i(stub()));
1893     if (stub()->info() != nullptr) out->print(" [bci:%d]", stub()->info()->stack()->bci());
1894   } else {
1895     out->print("[label:" INTPTR_FORMAT "] ", p2i(label()));
1896   }
1897   if (ublock() != nullptr) {
1898     out->print("unordered: [B%d] ", ublock()->block_id());
1899   }
1900 }
1901 
1902 void LIR_Op::print_condition(outputStream* out, LIR_Condition cond) {
1903   switch(cond) {
1904     case lir_cond_equal:           out->print("[EQ]");      break;
1905     case lir_cond_notEqual:        out->print("[NE]");      break;
1906     case lir_cond_less:            out->print("[LT]");      break;
1907     case lir_cond_lessEqual:       out->print("[LE]");      break;
1908     case lir_cond_greaterEqual:    out->print("[GE]");      break;
1909     case lir_cond_greater:         out->print("[GT]");      break;
1910     case lir_cond_belowEqual:      out->print("[BE]");      break;
1911     case lir_cond_aboveEqual:      out->print("[AE]");      break;
1912     case lir_cond_always:          out->print("[AL]");      break;
1913     default:                       out->print("[%d]",cond); break;
1914   }
1915 }
1916 
1917 // LIR_OpConvert
1918 void LIR_OpConvert::print_instr(outputStream* out) const {
1919   print_bytecode(out, bytecode());
1920   in_opr()->print(out);                  out->print(" ");
1921   result_opr()->print(out);              out->print(" ");
1922 }
1923 
1924 void LIR_OpConvert::print_bytecode(outputStream* out, Bytecodes::Code code) {
1925   switch(code) {
1926     case Bytecodes::_d2f: out->print("[d2f] "); break;
1927     case Bytecodes::_d2i: out->print("[d2i] "); break;
1928     case Bytecodes::_d2l: out->print("[d2l] "); break;
1929     case Bytecodes::_f2d: out->print("[f2d] "); break;
1930     case Bytecodes::_f2i: out->print("[f2i] "); break;
1931     case Bytecodes::_f2l: out->print("[f2l] "); break;
1932     case Bytecodes::_i2b: out->print("[i2b] "); break;
1933     case Bytecodes::_i2c: out->print("[i2c] "); break;
1934     case Bytecodes::_i2d: out->print("[i2d] "); break;
1935     case Bytecodes::_i2f: out->print("[i2f] "); break;
1936     case Bytecodes::_i2l: out->print("[i2l] "); break;
1937     case Bytecodes::_i2s: out->print("[i2s] "); break;
1938     case Bytecodes::_l2i: out->print("[l2i] "); break;
1939     case Bytecodes::_l2f: out->print("[l2f] "); break;
1940     case Bytecodes::_l2d: out->print("[l2d] "); break;
1941     default:
1942       out->print("[?%d]",code);
1943     break;
1944   }
1945 }
1946 
1947 void LIR_OpAllocObj::print_instr(outputStream* out) const {
1948   klass()->print(out);                      out->print(" ");
1949   obj()->print(out);                        out->print(" ");
1950   tmp1()->print(out);                       out->print(" ");
1951   tmp2()->print(out);                       out->print(" ");
1952   tmp3()->print(out);                       out->print(" ");
1953   tmp4()->print(out);                       out->print(" ");
1954   out->print("[hdr:%d]", header_size()); out->print(" ");
1955   out->print("[obj:%d]", object_size()); out->print(" ");
1956   out->print("[lbl:" INTPTR_FORMAT "]", p2i(stub()->entry()));
1957 }
1958 
1959 // LIR_Op2
1960 void LIR_Op2::print_instr(outputStream* out) const {
1961   if (code() == lir_cmp || code() == lir_branch || code() == lir_cond_float_branch) {
1962     print_condition(out, condition());         out->print(" ");
1963   }
1964   in_opr1()->print(out);    out->print(" ");
1965   in_opr2()->print(out);    out->print(" ");
1966   if (tmp1_opr()->is_valid()) { tmp1_opr()->print(out);    out->print(" "); }
1967   if (tmp2_opr()->is_valid()) { tmp2_opr()->print(out);    out->print(" "); }
1968   if (tmp3_opr()->is_valid()) { tmp3_opr()->print(out);    out->print(" "); }
1969   if (tmp4_opr()->is_valid()) { tmp4_opr()->print(out);    out->print(" "); }
1970   if (tmp5_opr()->is_valid()) { tmp5_opr()->print(out);    out->print(" "); }
1971   result_opr()->print(out);
1972 }
1973 
1974 void LIR_OpAllocArray::print_instr(outputStream* out) const {
1975   klass()->print(out);                   out->print(" ");
1976   len()->print(out);                     out->print(" ");
1977   obj()->print(out);                     out->print(" ");
1978   tmp1()->print(out);                    out->print(" ");
1979   tmp2()->print(out);                    out->print(" ");
1980   tmp3()->print(out);                    out->print(" ");
1981   tmp4()->print(out);                    out->print(" ");
1982   out->print("[type:0x%x]", type());     out->print(" ");
1983   out->print("[label:" INTPTR_FORMAT "]", p2i(stub()->entry()));
1984 }
1985 
1986 
1987 void LIR_OpTypeCheck::print_instr(outputStream* out) const {
1988   object()->print(out);                  out->print(" ");
1989   if (code() == lir_store_check) {
1990     array()->print(out);                 out->print(" ");
1991   }
1992   if (code() != lir_store_check) {
1993     klass()->print_name_on(out);         out->print(" ");
1994     if (fast_check())                 out->print("fast_check ");
1995   }
1996   tmp1()->print(out);                    out->print(" ");
1997   tmp2()->print(out);                    out->print(" ");
1998   tmp3()->print(out);                    out->print(" ");
1999   result_opr()->print(out);              out->print(" ");
2000   if (info_for_exception() != nullptr) out->print(" [bci:%d]", info_for_exception()->stack()->bci());
2001 }
2002 
2003 
2004 // LIR_Op3
2005 void LIR_Op3::print_instr(outputStream* out) const {
2006   in_opr1()->print(out);    out->print(" ");
2007   in_opr2()->print(out);    out->print(" ");
2008   in_opr3()->print(out);    out->print(" ");
2009   result_opr()->print(out);
2010 }
2011 
2012 // LIR_Op4
2013 void LIR_Op4::print_instr(outputStream* out) const {
2014   print_condition(out, condition()); out->print(" ");
2015   in_opr1()->print(out);             out->print(" ");
2016   in_opr2()->print(out);             out->print(" ");
2017   in_opr3()->print(out);             out->print(" ");
2018   in_opr4()->print(out);             out->print(" ");
2019   result_opr()->print(out);
2020 }
2021 
2022 void LIR_OpLock::print_instr(outputStream* out) const {
2023   hdr_opr()->print(out);   out->print(" ");
2024   obj_opr()->print(out);   out->print(" ");
2025   lock_opr()->print(out);  out->print(" ");
2026   if (_scratch->is_valid()) {
2027     _scratch->print(out);  out->print(" ");
2028   }
2029   out->print("[lbl:" INTPTR_FORMAT "]", p2i(stub()->entry()));
2030 }
2031 
2032 void LIR_OpLoadKlass::print_instr(outputStream* out) const {
2033   obj()->print(out);        out->print(" ");
2034   result_opr()->print(out); out->print(" ");
2035 }
2036 
2037 #ifdef ASSERT
2038 void LIR_OpAssert::print_instr(outputStream* out) const {
2039   print_condition(out, condition()); out->print(" ");
2040   in_opr1()->print(out);             out->print(" ");
2041   in_opr2()->print(out);             out->print(", \"");
2042   out->print("%s", msg());          out->print("\"");
2043 }
2044 #endif
2045 
2046 
2047 void LIR_OpDelay::print_instr(outputStream* out) const {
2048   _op->print_on(out);
2049 }
2050 
2051 
2052 // LIR_OpProfileCall
2053 void LIR_OpProfileCall::print_instr(outputStream* out) const {
2054   profiled_method()->name()->print_symbol_on(out);
2055   out->print(".");
2056   profiled_method()->holder()->name()->print_symbol_on(out);
2057   out->print(" @ %d ", profiled_bci());
2058   mdo()->print(out);           out->print(" ");
2059   recv()->print(out);          out->print(" ");
2060   tmp1()->print(out);          out->print(" ");
2061 }
2062 
2063 // LIR_OpProfileType
2064 void LIR_OpProfileType::print_instr(outputStream* out) const {
2065   out->print("exact = ");
2066   if (exact_klass() == nullptr) {
2067     out->print("unknown");
2068   } else {
2069     exact_klass()->print_name_on(out);
2070   }
2071   out->print(" current = "); ciTypeEntries::print_ciklass(out, current_klass());
2072   out->print(" ");
2073   mdp()->print(out);          out->print(" ");
2074   obj()->print(out);          out->print(" ");
2075   tmp()->print(out);          out->print(" ");
2076 }
2077 
2078 #endif // PRODUCT
2079 
2080 // Implementation of LIR_InsertionBuffer
2081 
2082 void LIR_InsertionBuffer::append(int index, LIR_Op* op) {
2083   assert(_index_and_count.length() % 2 == 0, "must have a count for each index");
2084 
2085   int i = number_of_insertion_points() - 1;
2086   if (i < 0 || index_at(i) < index) {
2087     append_new(index, 1);
2088   } else {
2089     assert(index_at(i) == index, "can append LIR_Ops in ascending order only");
2090     assert(count_at(i) > 0, "check");
2091     set_count_at(i, count_at(i) + 1);
2092   }
2093   _ops.push(op);
2094 
2095   DEBUG_ONLY(verify());
2096 }
2097 
2098 #ifdef ASSERT
2099 void LIR_InsertionBuffer::verify() {
2100   int sum = 0;
2101   int prev_idx = -1;
2102 
2103   for (int i = 0; i < number_of_insertion_points(); i++) {
2104     assert(prev_idx < index_at(i), "index must be ordered ascending");
2105     sum += count_at(i);
2106   }
2107   assert(sum == number_of_ops(), "wrong total sum");
2108 }
2109 #endif