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