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