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 (ShenandoahConditionalSATBBarrier) {
1654     LIR_Opr gc_state_addr = new_pointer_register();
1655     __ move(LIR_OprFact::intptrConst((intptr_t) ShenandoahHeap::gc_state_addr()), gc_state_addr);
1656     LIR_Opr gc_state = new_register(T_INT);
1657     __ move(new LIR_Address(gc_state_addr, T_BYTE), gc_state);
1658     __ logical_and(gc_state, LIR_OprFact::intConst(ShenandoahHeap::MARKING), gc_state);
1659     __ cmp(lir_cond_equal, gc_state, LIR_OprFact::intConst(0));
1660 
1661     LIR_PatchCode pre_val_patch_code = lir_patch_none;
1662 
1663     CodeStub* slow;
1664 
1665     if (do_load) {
1666       assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
1667       assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
1668 
1669       if (patch)
1670         pre_val_patch_code = lir_patch_normal;
1671 
1672       pre_val = new_register(T_OBJECT);
1673 
1674       if (!addr_opr->is_address()) {
1675         assert(addr_opr->is_register(), "must be");
1676         addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
1677       }
1678       slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info);
1679     } else {
1680       assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
1681       assert(pre_val->is_register(), "must be");
1682       assert(pre_val->type() == T_OBJECT, "must be an object");
1683       assert(info == NULL, "sanity");
1684 
1685       slow = new G1PreBarrierStub(pre_val);
1686     }
1687 
1688     __ branch(lir_cond_notEqual, T_CHAR, slow);
1689     __ branch_destination(slow->continuation());
1690   }
1691   if (ShenandoahSATBBarrier) {
1692     G1BarrierSet_pre_barrier(addr_opr, pre_val, do_load, patch, info);
1693   }
1694 }
1695 
1696 void LIRGenerator::Shenandoah_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1697   if (! UseShenandoahMatrix) {
1698     // No need for that barrier if not using matrix.
1699     return;
1700   }
1701 
1702   // If the "new_val" is a constant NULL, no barrier is necessary.
1703   if (new_val->is_constant() &&
1704       new_val->as_constant_ptr()->as_jobject() == NULL) return;
1705 
1706   if (!new_val->is_register()) {
1707     LIR_Opr new_val_reg = new_register(T_OBJECT);
1708     if (new_val->is_constant()) {
1709       __ move(new_val, new_val_reg);
1710     } else {
1711       __ leal(new_val, new_val_reg);
1712     }
1713     new_val = new_val_reg;
1714   }
1715   assert(new_val->is_register(), "must be a register at this point");
1716 
1717   if (addr->is_address()) {
1718     LIR_Address* address = addr->as_address_ptr();
1719     LIR_Opr ptr = new_pointer_register();
1720     if (!address->index()->is_valid() && address->disp() == 0) {
1721       __ move(address->base(), ptr);
1722     } else {
1723       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1724       __ leal(addr, ptr);
1725     }
1726     addr = ptr;
1727   }
1728   assert(addr->is_register(), "must be a register at this point");
1729 
1730   LabelObj* L_done = new LabelObj();
1731   __ cmp(lir_cond_equal, new_val, LIR_OprFact::oopConst(NULL_WORD));
1732   __ branch(lir_cond_equal, T_OBJECT, L_done->label());
1733 
1734   ShenandoahConnectionMatrix* matrix = ShenandoahHeap::heap()->connection_matrix();
1735 
1736   LIR_Opr heap_base = new_pointer_register();
1737   __ move(LIR_OprFact::intptrConst(ShenandoahHeap::heap()->base()), heap_base);
1738 
1739   LIR_Opr tmp1 = new_pointer_register();
1740   __ move(new_val, tmp1);
1741   __ sub(tmp1, heap_base, tmp1);
1742   __ unsigned_shift_right(tmp1, LIR_OprFact::intConst(ShenandoahHeapRegion::region_size_bytes_shift_jint()), tmp1, LIR_OprDesc::illegalOpr());
1743 
1744   LIR_Opr tmp2 = new_pointer_register();
1745   __ move(addr, tmp2);
1746   __ sub(tmp2, heap_base, tmp2);
1747   __ unsigned_shift_right(tmp2, LIR_OprFact::intConst(ShenandoahHeapRegion::region_size_bytes_shift_jint()), tmp2, LIR_OprDesc::illegalOpr());
1748 
1749   LIR_Opr tmp3 = new_pointer_register();
1750   __ move(LIR_OprFact::longConst(matrix->stride_jint()), tmp3);
1751   __ mul(tmp1, tmp3, tmp1);
1752   __ add(tmp1, tmp2, tmp1);
1753 
1754   LIR_Opr tmp4 = new_pointer_register();
1755   __ move(LIR_OprFact::intptrConst((intptr_t) matrix->matrix_addr()), tmp4);
1756   LIR_Address* matrix_elem_addr = new LIR_Address(tmp4, tmp1, T_BYTE);
1757 
1758   LIR_Opr tmp5 = new_register(T_INT);
1759   __ move(matrix_elem_addr, tmp5);
1760   __ cmp(lir_cond_notEqual, tmp5, LIR_OprFact::intConst(0));
1761   __ branch(lir_cond_notEqual, T_BYTE, L_done->label());
1762 
1763   // Aarch64 cannot move constant 1. Load it into a register.
1764   LIR_Opr one = new_register(T_INT);
1765   __ move(LIR_OprFact::intConst(1), one);
1766   __ move(one, matrix_elem_addr);
1767 
1768   __ branch_destination(L_done->label());
1769 }
1770 
1771 #endif // INCLUDE_ALL_GCS
1772 ////////////////////////////////////////////////////////////////////////
1773 
1774 void LIRGenerator::CardTableBarrierSet_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1775   LIR_Const* card_table_base = new LIR_Const(ci_card_table_address());
1776   if (addr->is_address()) {
1777     LIR_Address* address = addr->as_address_ptr();
1778     // ptr cannot be an object because we use this barrier for array card marks
1779     // and addr can point in the middle of an array.
1780     LIR_Opr ptr = new_pointer_register();
1781     if (!address->index()->is_valid() && address->disp() == 0) {
1782       __ move(address->base(), ptr);
1783     } else {
1784       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1785       __ leal(addr, ptr);
1786     }
1787     addr = ptr;
1788   }
1789   assert(addr->is_register(), "must be a register at this point");
1790 
1791 #ifdef CARDTABLEBARRIERSET_POST_BARRIER_HELPER
1792   CardTableBarrierSet_post_barrier_helper(addr, card_table_base);
1793 #else
1794   LIR_Opr tmp = new_pointer_register();
1795   if (TwoOperandLIRForm) {
1796     __ move(addr, tmp);
1797     __ unsigned_shift_right(tmp, CardTable::card_shift, tmp);
1798   } else {
1799     __ unsigned_shift_right(addr, CardTable::card_shift, tmp);
1800   }
1801 
1802   LIR_Address* card_addr;
1803   if (can_inline_as_constant(card_table_base)) {
1804     card_addr = new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE);
1805   } else {
1806     card_addr = new LIR_Address(tmp, load_constant(card_table_base), T_BYTE);
1807   }
1808 
1809   LIR_Opr dirty = LIR_OprFact::intConst(CardTable::dirty_card_val());
1810   if (UseCondCardMark) {
1811     LIR_Opr cur_value = new_register(T_INT);
1812     if (UseConcMarkSweepGC) {
1813       __ membar_storeload();
1814     }
1815     __ move(card_addr, cur_value);
1816 
1817     LabelObj* L_already_dirty = new LabelObj();
1818     __ cmp(lir_cond_equal, cur_value, dirty);
1819     __ branch(lir_cond_equal, T_BYTE, L_already_dirty->label());
1820     __ move(dirty, card_addr);
1821     __ branch_destination(L_already_dirty->label());
1822   } else {
1823 #if INCLUDE_ALL_GCS
1824     if (UseConcMarkSweepGC && CMSPrecleaningEnabled) {
1825       __ membar_storestore();
1826     }
1827 #endif
1828     __ move(dirty, card_addr);
1829   }
1830 #endif
1831 }
1832 
1833 
1834 //------------------------field access--------------------------------------
1835 
1836 // Comment copied form templateTable_i486.cpp
1837 // ----------------------------------------------------------------------------
1838 // Volatile variables demand their effects be made known to all CPU's in
1839 // order.  Store buffers on most chips allow reads & writes to reorder; the
1840 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1841 // memory barrier (i.e., it's not sufficient that the interpreter does not
1842 // reorder volatile references, the hardware also must not reorder them).
1843 //
1844 // According to the new Java Memory Model (JMM):
1845 // (1) All volatiles are serialized wrt to each other.
1846 // ALSO reads & writes act as aquire & release, so:
1847 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1848 // the read float up to before the read.  It's OK for non-volatile memory refs
1849 // that happen before the volatile read to float down below it.
1850 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1851 // that happen BEFORE the write float down to after the write.  It's OK for
1852 // non-volatile memory refs that happen after the volatile write to float up
1853 // before it.
1854 //
1855 // We only put in barriers around volatile refs (they are expensive), not
1856 // _between_ memory refs (that would require us to track the flavor of the
1857 // previous memory refs).  Requirements (2) and (3) require some barriers
1858 // before volatile stores and after volatile loads.  These nearly cover
1859 // requirement (1) but miss the volatile-store-volatile-load case.  This final
1860 // case is placed after volatile-stores although it could just as well go
1861 // before volatile-loads.
1862 
1863 
1864 void LIRGenerator::do_StoreField(StoreField* x) {
1865   bool needs_patching = x->needs_patching();
1866   bool is_volatile = x->field()->is_volatile();
1867   BasicType field_type = x->field_type();
1868   bool is_oop = (field_type == T_ARRAY || field_type == T_OBJECT);
1869 
1870   CodeEmitInfo* info = NULL;
1871   if (needs_patching) {
1872     assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1873     info = state_for(x, x->state_before());
1874   } else if (x->needs_null_check()) {
1875     NullCheck* nc = x->explicit_null_check();
1876     if (nc == NULL) {
1877       info = state_for(x);
1878     } else {
1879       info = state_for(nc);
1880     }
1881   }
1882 
1883 
1884   LIRItem object(x->obj(), this);
1885   LIRItem value(x->value(),  this);
1886 
1887   object.load_item();
1888 
1889   if (is_volatile || needs_patching) {
1890     // load item if field is volatile (fewer special cases for volatiles)
1891     // load item if field not initialized
1892     // load item if field not constant
1893     // because of code patching we cannot inline constants
1894     if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1895       value.load_byte_item();
1896     } else  {
1897       value.load_item();
1898     }
1899   } else {
1900     value.load_for_store(field_type);
1901   }
1902 
1903   set_no_result(x);
1904 
1905 #ifndef PRODUCT
1906   if (PrintNotLoaded && needs_patching) {
1907     tty->print_cr("   ###class not loaded at store_%s bci %d",
1908                   x->is_static() ?  "static" : "field", x->printable_bci());
1909   }
1910 #endif
1911 
1912   LIR_Opr obj = object.result();
1913 
1914   if (x->needs_null_check() &&
1915       (needs_patching ||
1916        MacroAssembler::needs_explicit_null_check(x->offset()))) {
1917     // Emit an explicit null check because the offset is too large.
1918     // If the class is not loaded and the object is NULL, we need to deoptimize to throw a
1919     // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
1920     __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);
1921   }
1922 
1923   obj = shenandoah_write_barrier(obj, info, x->needs_null_check());
1924   LIR_Opr val = value.result();
1925   if (is_oop && UseShenandoahGC) {
1926     val = shenandoah_storeval_barrier(val, NULL, true);
1927   }
1928 
1929   LIR_Address* address;
1930   if (needs_patching) {
1931     // we need to patch the offset in the instruction so don't allow
1932     // generate_address to try to be smart about emitting the -1.
1933     // Otherwise the patching code won't know how to find the
1934     // instruction to patch.
1935     address = new LIR_Address(obj, PATCHED_ADDR, field_type);
1936   } else {
1937     address = generate_address(obj, x->offset(), field_type);
1938   }
1939 
1940   if (is_volatile && os::is_MP()) {
1941     __ membar_release();
1942   }
1943 
1944   if (is_oop) {
1945     // Do the pre-write barrier, if any.
1946     pre_barrier(LIR_OprFact::address(address),
1947                 LIR_OprFact::illegalOpr /* pre_val */,
1948                 true /* do_load*/,
1949                 needs_patching,
1950                 (info ? new CodeEmitInfo(info) : NULL));
1951   }
1952 
1953   bool needs_atomic_access = is_volatile || AlwaysAtomicAccesses;
1954   if (needs_atomic_access && !needs_patching) {
1955     volatile_field_store(val, address, info);
1956   } else {
1957     LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1958     __ store(val, address, info, patch_code);
1959   }
1960 
1961   if (is_oop) {
1962     // Store to object so mark the card of the header
1963     post_barrier(obj, val);
1964   }
1965 
1966   if (!support_IRIW_for_not_multiple_copy_atomic_cpu && is_volatile && os::is_MP()) {
1967     __ membar();
1968   }
1969 }
1970 
1971 
1972 void LIRGenerator::do_LoadField(LoadField* x) {
1973   bool needs_patching = x->needs_patching();
1974   bool is_volatile = x->field()->is_volatile();
1975   BasicType field_type = x->field_type();
1976 
1977   CodeEmitInfo* info = NULL;
1978   if (needs_patching) {
1979     assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1980     info = state_for(x, x->state_before());
1981   } else if (x->needs_null_check()) {
1982     NullCheck* nc = x->explicit_null_check();
1983     if (nc == NULL) {
1984       info = state_for(x);
1985     } else {
1986       info = state_for(nc);
1987     }
1988   }
1989 
1990   LIRItem object(x->obj(), this);
1991 
1992   object.load_item();
1993 
1994 #ifndef PRODUCT
1995   if (PrintNotLoaded && needs_patching) {
1996     tty->print_cr("   ###class not loaded at load_%s bci %d",
1997                   x->is_static() ?  "static" : "field", x->printable_bci());
1998   }
1999 #endif
2000 
2001   LIR_Opr obj = object.result();
2002   bool stress_deopt = StressLoopInvariantCodeMotion && info && info->deoptimize_on_exception();
2003   if (x->needs_null_check() &&
2004       (needs_patching ||
2005        MacroAssembler::needs_explicit_null_check(x->offset()) ||
2006        stress_deopt)) {
2007     if (stress_deopt) {
2008       obj = new_register(T_OBJECT);
2009       __ move(LIR_OprFact::oopConst(NULL), obj);
2010     }
2011     // Emit an explicit null check because the offset is too large.
2012     // If the class is not loaded and the object is NULL, we need to deoptimize to throw a
2013     // NoClassDefFoundError in the interpreter instead of an implicit NPE from compiled code.
2014     __ null_check(obj, new CodeEmitInfo(info), /* deoptimize */ needs_patching);
2015   }
2016 
2017   obj = shenandoah_read_barrier(obj, info, x->needs_null_check() && x->explicit_null_check() != NULL);
2018   LIR_Opr reg = rlock_result(x, field_type);
2019   LIR_Address* address;
2020   if (needs_patching) {
2021     // we need to patch the offset in the instruction so don't allow
2022     // generate_address to try to be smart about emitting the -1.
2023     // Otherwise the patching code won't know how to find the
2024     // instruction to patch.
2025     address = new LIR_Address(obj, PATCHED_ADDR, field_type);
2026   } else {
2027     address = generate_address(obj, x->offset(), field_type);
2028   }
2029 
2030   if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_volatile && os::is_MP()) {
2031     __ membar();
2032   }
2033 
2034   bool needs_atomic_access = is_volatile || AlwaysAtomicAccesses;
2035   if (needs_atomic_access && !needs_patching) {
2036     volatile_field_load(address, reg, info);
2037   } else {
2038     LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
2039     __ load(address, reg, info, patch_code);
2040   }
2041 
2042   if (is_volatile && os::is_MP()) {
2043     __ membar_acquire();
2044   }
2045 }
2046 
2047 LIR_Opr LIRGenerator::shenandoah_read_barrier(LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
2048   if (UseShenandoahGC && ShenandoahReadBarrier) {
2049     return shenandoah_read_barrier_impl(obj, info, need_null_check);
2050   } else {
2051     return obj;
2052   }
2053 }
2054 
2055 LIR_Opr LIRGenerator::shenandoah_read_barrier_impl(LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
2056   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "Should be enabled");
2057   LabelObj* done = new LabelObj();
2058   LIR_Opr result = new_register(T_OBJECT);
2059   __ move(obj, result);
2060   if (need_null_check) {
2061     __ cmp(lir_cond_equal, result, LIR_OprFact::oopConst(NULL));
2062     __ branch(lir_cond_equal, T_LONG, done->label());
2063   }
2064   LIR_Address* brooks_ptr_address = generate_address(result, BrooksPointer::byte_offset(), T_ADDRESS);
2065   __ load(brooks_ptr_address, result, info ? new CodeEmitInfo(info) : NULL, lir_patch_none);
2066 
2067   __ branch_destination(done->label());
2068   return result;
2069 }
2070 
2071 LIR_Opr LIRGenerator::shenandoah_write_barrier(LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
2072   if (UseShenandoahGC && ShenandoahWriteBarrier) {
2073     return shenandoah_write_barrier_impl(obj, info, need_null_check);
2074   } else {
2075     return obj;
2076   }
2077 }
2078 
2079 LIR_Opr LIRGenerator::shenandoah_write_barrier_impl(LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
2080   assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValWriteBarrier || ShenandoahStoreValEnqueueBarrier), "Should be enabled");
2081   LIR_Opr result = new_register(T_OBJECT);
2082   __ shenandoah_wb(obj, result, info ? new CodeEmitInfo(info) : NULL, need_null_check);
2083   return result;
2084 }
2085 
2086 LIR_Opr LIRGenerator::shenandoah_storeval_barrier(LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
2087   if (UseShenandoahGC) {
2088     if (ShenandoahStoreValWriteBarrier || ShenandoahStoreValEnqueueBarrier) {
2089       // TODO: Maybe we can simply avoid this stuff on constants?
2090       if (! obj->is_register()) {
2091         LIR_Opr result = new_register(T_OBJECT);
2092         __ move(obj, result);
2093         obj = result;
2094       }
2095       obj = shenandoah_write_barrier_impl(obj, info, need_null_check);
2096     }
2097     if (ShenandoahStoreValEnqueueBarrier) {
2098       G1BarrierSet_pre_barrier(LIR_OprFact::illegalOpr, obj, false, false, NULL);
2099     }
2100     if (ShenandoahStoreValReadBarrier) {
2101       obj = shenandoah_read_barrier_impl(obj, info, need_null_check);
2102     }
2103   }
2104   return obj;
2105 }
2106 //------------------------java.nio.Buffer.checkIndex------------------------
2107 
2108 // int java.nio.Buffer.checkIndex(int)
2109 void LIRGenerator::do_NIOCheckIndex(Intrinsic* x) {
2110   // NOTE: by the time we are in checkIndex() we are guaranteed that
2111   // the buffer is non-null (because checkIndex is package-private and
2112   // only called from within other methods in the buffer).
2113   assert(x->number_of_arguments() == 2, "wrong type");
2114   LIRItem buf  (x->argument_at(0), this);
2115   LIRItem index(x->argument_at(1), this);
2116   buf.load_item();
2117   index.load_item();
2118 
2119   LIR_Opr result = rlock_result(x);
2120   if (GenerateRangeChecks) {
2121     CodeEmitInfo* info = state_for(x);
2122     CodeStub* stub = new RangeCheckStub(info, index.result(), true);
2123     LIR_Opr buf_obj = shenandoah_read_barrier(buf.result(), info, false);
2124     if (index.result()->is_constant()) {
2125       cmp_mem_int(lir_cond_belowEqual, buf_obj, java_nio_Buffer::limit_offset(), index.result()->as_jint(), info);
2126       __ branch(lir_cond_belowEqual, T_INT, stub);
2127     } else {
2128       cmp_reg_mem(lir_cond_aboveEqual, index.result(), buf_obj,
2129                   java_nio_Buffer::limit_offset(), T_INT, info);
2130       __ branch(lir_cond_aboveEqual, T_INT, stub);
2131     }
2132     __ move(index.result(), result);
2133   } else {
2134     // Just load the index into the result register
2135     __ move(index.result(), result);
2136   }
2137 }
2138 
2139 
2140 //------------------------array access--------------------------------------
2141 
2142 
2143 void LIRGenerator::do_ArrayLength(ArrayLength* x) {
2144   LIRItem array(x->array(), this);
2145   array.load_item();
2146   LIR_Opr reg = rlock_result(x);
2147 
2148   CodeEmitInfo* info = NULL;
2149   if (x->needs_null_check()) {
2150     NullCheck* nc = x->explicit_null_check();
2151     if (nc == NULL) {
2152       info = state_for(x);
2153     } else {
2154       info = state_for(nc);
2155     }
2156     if (StressLoopInvariantCodeMotion && info->deoptimize_on_exception()) {
2157       LIR_Opr obj = new_register(T_OBJECT);
2158       __ move(LIR_OprFact::oopConst(NULL), obj);
2159       __ null_check(obj, new CodeEmitInfo(info));
2160     }
2161   }
2162   __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);
2163 }
2164 
2165 
2166 void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {
2167   bool use_length = x->length() != NULL;
2168   LIRItem array(x->array(), this);
2169   LIRItem index(x->index(), this);
2170   LIRItem length(this);
2171   bool needs_range_check = x->compute_needs_range_check();
2172 
2173   if (use_length && needs_range_check) {
2174     length.set_instruction(x->length());
2175     length.load_item();
2176   }
2177 
2178   array.load_item();
2179   if (index.is_constant() && can_inline_as_constant(x->index())) {
2180     // let it be a constant
2181     index.dont_load_item();
2182   } else {
2183     index.load_item();
2184   }
2185 
2186   CodeEmitInfo* range_check_info = state_for(x);
2187   CodeEmitInfo* null_check_info = NULL;
2188   if (x->needs_null_check()) {
2189     NullCheck* nc = x->explicit_null_check();
2190     if (nc != NULL) {
2191       null_check_info = state_for(nc);
2192     } else {
2193       null_check_info = range_check_info;
2194     }
2195     if (StressLoopInvariantCodeMotion && null_check_info->deoptimize_on_exception()) {
2196       LIR_Opr obj = new_register(T_OBJECT);
2197       __ move(LIR_OprFact::oopConst(NULL), obj);
2198       __ null_check(obj, new CodeEmitInfo(null_check_info));
2199     }
2200   }
2201 
2202   LIR_Opr ary = array.result();
2203   ary = shenandoah_read_barrier(ary, null_check_info, null_check_info != NULL);
2204 
2205   // emit array address setup early so it schedules better
2206   LIR_Address* array_addr = emit_array_address(ary, index.result(), x->elt_type(), false);
2207 
2208   if (GenerateRangeChecks && needs_range_check) {
2209     if (StressLoopInvariantCodeMotion && range_check_info->deoptimize_on_exception()) {
2210       __ branch(lir_cond_always, T_ILLEGAL, new RangeCheckStub(range_check_info, index.result()));
2211     } else if (use_length) {
2212       // TODO: use a (modified) version of array_range_check that does not require a
2213       //       constant length to be loaded to a register
2214       __ cmp(lir_cond_belowEqual, length.result(), index.result());
2215       __ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));
2216     } else {
2217       array_range_check(ary, index.result(), null_check_info, range_check_info);
2218       // The range check performs the null check, so clear it out for the load
2219       null_check_info = NULL;
2220     }
2221   }
2222 
2223   __ move(array_addr, rlock_result(x, x->elt_type()), null_check_info);
2224 }
2225 
2226 
2227 void LIRGenerator::do_NullCheck(NullCheck* x) {
2228   if (x->can_trap()) {
2229     LIRItem value(x->obj(), this);
2230     value.load_item();
2231     CodeEmitInfo* info = state_for(x);
2232     __ null_check(value.result(), info);
2233   }
2234 }
2235 
2236 
2237 void LIRGenerator::do_TypeCast(TypeCast* x) {
2238   LIRItem value(x->obj(), this);
2239   value.load_item();
2240   // the result is the same as from the node we are casting
2241   set_result(x, value.result());
2242 }
2243 
2244 
2245 void LIRGenerator::do_Throw(Throw* x) {
2246   LIRItem exception(x->exception(), this);
2247   exception.load_item();
2248   set_no_result(x);
2249   LIR_Opr exception_opr = exception.result();
2250   CodeEmitInfo* info = state_for(x, x->state());
2251 
2252 #ifndef PRODUCT
2253   if (PrintC1Statistics) {
2254     increment_counter(Runtime1::throw_count_address(), T_INT);
2255   }
2256 #endif
2257 
2258   // check if the instruction has an xhandler in any of the nested scopes
2259   bool unwind = false;
2260   if (info->exception_handlers()->length() == 0) {
2261     // this throw is not inside an xhandler
2262     unwind = true;
2263   } else {
2264     // get some idea of the throw type
2265     bool type_is_exact = true;
2266     ciType* throw_type = x->exception()->exact_type();
2267     if (throw_type == NULL) {
2268       type_is_exact = false;
2269       throw_type = x->exception()->declared_type();
2270     }
2271     if (throw_type != NULL && throw_type->is_instance_klass()) {
2272       ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;
2273       unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);
2274     }
2275   }
2276 
2277   // do null check before moving exception oop into fixed register
2278   // to avoid a fixed interval with an oop during the null check.
2279   // Use a copy of the CodeEmitInfo because debug information is
2280   // different for null_check and throw.
2281   if (x->exception()->as_NewInstance() == NULL && x->exception()->as_ExceptionObject() == NULL) {
2282     // if the exception object wasn't created using new then it might be null.
2283     __ null_check(exception_opr, new CodeEmitInfo(info, x->state()->copy(ValueStack::ExceptionState, x->state()->bci())));
2284   }
2285 
2286   if (compilation()->env()->jvmti_can_post_on_exceptions()) {
2287     // we need to go through the exception lookup path to get JVMTI
2288     // notification done
2289     unwind = false;
2290   }
2291 
2292   // move exception oop into fixed register
2293   __ move(exception_opr, exceptionOopOpr());
2294 
2295   if (unwind) {
2296     __ unwind_exception(exceptionOopOpr());
2297   } else {
2298     __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);
2299   }
2300 }
2301 
2302 
2303 void LIRGenerator::do_RoundFP(RoundFP* x) {
2304   LIRItem input(x->input(), this);
2305   input.load_item();
2306   LIR_Opr input_opr = input.result();
2307   assert(input_opr->is_register(), "why round if value is not in a register?");
2308   assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value");
2309   if (input_opr->is_single_fpu()) {
2310     set_result(x, round_item(input_opr)); // This code path not currently taken
2311   } else {
2312     LIR_Opr result = new_register(T_DOUBLE);
2313     set_vreg_flag(result, must_start_in_memory);
2314     __ roundfp(input_opr, LIR_OprFact::illegalOpr, result);
2315     set_result(x, result);
2316   }
2317 }
2318 
2319 // Here UnsafeGetRaw may have x->base() and x->index() be int or long
2320 // on both 64 and 32 bits. Expecting x->base() to be always long on 64bit.
2321 void LIRGenerator::do_UnsafeGetRaw(UnsafeGetRaw* x) {
2322   LIRItem base(x->base(), this);
2323   LIRItem idx(this);
2324 
2325   base.load_item();
2326   if (x->has_index()) {
2327     idx.set_instruction(x->index());
2328     idx.load_nonconstant();
2329   }
2330 
2331   LIR_Opr reg = rlock_result(x, x->basic_type());
2332 
2333   int   log2_scale = 0;
2334   if (x->has_index()) {
2335     log2_scale = x->log2_scale();
2336   }
2337 
2338   assert(!x->has_index() || idx.value() == x->index(), "should match");
2339 
2340   LIR_Opr base_op = base.result();
2341   LIR_Opr index_op = idx.result();
2342 #ifndef _LP64
2343   if (base_op->type() == T_LONG) {
2344     base_op = new_register(T_INT);
2345     __ convert(Bytecodes::_l2i, base.result(), base_op);
2346   }
2347   if (x->has_index()) {
2348     if (index_op->type() == T_LONG) {
2349       LIR_Opr long_index_op = index_op;
2350       if (index_op->is_constant()) {
2351         long_index_op = new_register(T_LONG);
2352         __ move(index_op, long_index_op);
2353       }
2354       index_op = new_register(T_INT);
2355       __ convert(Bytecodes::_l2i, long_index_op, index_op);
2356     } else {
2357       assert(x->index()->type()->tag() == intTag, "must be");
2358     }
2359   }
2360   // At this point base and index should be all ints.
2361   assert(base_op->type() == T_INT && !base_op->is_constant(), "base should be an non-constant int");
2362   assert(!x->has_index() || index_op->type() == T_INT, "index should be an int");
2363 #else
2364   if (x->has_index()) {
2365     if (index_op->type() == T_INT) {
2366       if (!index_op->is_constant()) {
2367         index_op = new_register(T_LONG);
2368         __ convert(Bytecodes::_i2l, idx.result(), index_op);
2369       }
2370     } else {
2371       assert(index_op->type() == T_LONG, "must be");
2372       if (index_op->is_constant()) {
2373         index_op = new_register(T_LONG);
2374         __ move(idx.result(), index_op);
2375       }
2376     }
2377   }
2378   // At this point base is a long non-constant
2379   // Index is a long register or a int constant.
2380   // We allow the constant to stay an int because that would allow us a more compact encoding by
2381   // embedding an immediate offset in the address expression. If we have a long constant, we have to
2382   // move it into a register first.
2383   assert(base_op->type() == T_LONG && !base_op->is_constant(), "base must be a long non-constant");
2384   assert(!x->has_index() || (index_op->type() == T_INT && index_op->is_constant()) ||
2385                             (index_op->type() == T_LONG && !index_op->is_constant()), "unexpected index type");
2386 #endif
2387 
2388   BasicType dst_type = x->basic_type();
2389 
2390   LIR_Address* addr;
2391   if (index_op->is_constant()) {
2392     assert(log2_scale == 0, "must not have a scale");
2393     assert(index_op->type() == T_INT, "only int constants supported");
2394     addr = new LIR_Address(base_op, index_op->as_jint(), dst_type);
2395   } else {
2396 #ifdef X86
2397     addr = new LIR_Address(base_op, index_op, LIR_Address::Scale(log2_scale), 0, dst_type);
2398 #elif defined(GENERATE_ADDRESS_IS_PREFERRED)
2399     addr = generate_address(base_op, index_op, log2_scale, 0, dst_type);
2400 #else
2401     if (index_op->is_illegal() || log2_scale == 0) {
2402       addr = new LIR_Address(base_op, index_op, dst_type);
2403     } else {
2404       LIR_Opr tmp = new_pointer_register();
2405       __ shift_left(index_op, log2_scale, tmp);
2406       addr = new LIR_Address(base_op, tmp, dst_type);
2407     }
2408 #endif
2409   }
2410 
2411   if (x->may_be_unaligned() && (dst_type == T_LONG || dst_type == T_DOUBLE)) {
2412     __ unaligned_move(addr, reg);
2413   } else {
2414     if (dst_type == T_OBJECT && x->is_wide()) {
2415       __ move_wide(addr, reg);
2416     } else {
2417       __ move(addr, reg);
2418     }
2419   }
2420 }
2421 
2422 
2423 void LIRGenerator::do_UnsafePutRaw(UnsafePutRaw* x) {
2424   int  log2_scale = 0;
2425   BasicType type = x->basic_type();
2426 
2427   if (x->has_index()) {
2428     log2_scale = x->log2_scale();
2429   }
2430 
2431   LIRItem base(x->base(), this);
2432   LIRItem value(x->value(), this);
2433   LIRItem idx(this);
2434 
2435   base.load_item();
2436   if (x->has_index()) {
2437     idx.set_instruction(x->index());
2438     idx.load_item();
2439   }
2440 
2441   if (type == T_BYTE || type == T_BOOLEAN) {
2442     value.load_byte_item();
2443   } else {
2444     value.load_item();
2445   }
2446 
2447   set_no_result(x);
2448 
2449   LIR_Opr base_op = base.result();
2450   LIR_Opr index_op = idx.result();
2451 
2452 #ifdef GENERATE_ADDRESS_IS_PREFERRED
2453   LIR_Address* addr = generate_address(base_op, index_op, log2_scale, 0, x->basic_type());
2454 #else
2455 #ifndef _LP64
2456   if (base_op->type() == T_LONG) {
2457     base_op = new_register(T_INT);
2458     __ convert(Bytecodes::_l2i, base.result(), base_op);
2459   }
2460   if (x->has_index()) {
2461     if (index_op->type() == T_LONG) {
2462       index_op = new_register(T_INT);
2463       __ convert(Bytecodes::_l2i, idx.result(), index_op);
2464     }
2465   }
2466   // At this point base and index should be all ints and not constants
2467   assert(base_op->type() == T_INT && !base_op->is_constant(), "base should be an non-constant int");
2468   assert(!x->has_index() || (index_op->type() == T_INT && !index_op->is_constant()), "index should be an non-constant int");
2469 #else
2470   if (x->has_index()) {
2471     if (index_op->type() == T_INT) {
2472       index_op = new_register(T_LONG);
2473       __ convert(Bytecodes::_i2l, idx.result(), index_op);
2474     }
2475   }
2476   // At this point base and index are long and non-constant
2477   assert(base_op->type() == T_LONG && !base_op->is_constant(), "base must be a non-constant long");
2478   assert(!x->has_index() || (index_op->type() == T_LONG && !index_op->is_constant()), "index must be a non-constant long");
2479 #endif
2480 
2481   if (log2_scale != 0) {
2482     // temporary fix (platform dependent code without shift on Intel would be better)
2483     // TODO: ARM also allows embedded shift in the address
2484     LIR_Opr tmp = new_pointer_register();
2485     if (TwoOperandLIRForm) {
2486       __ move(index_op, tmp);
2487       index_op = tmp;
2488     }
2489     __ shift_left(index_op, log2_scale, tmp);
2490     if (!TwoOperandLIRForm) {
2491       index_op = tmp;
2492     }
2493   }
2494 
2495   LIR_Address* addr = new LIR_Address(base_op, index_op, x->basic_type());
2496 #endif // !GENERATE_ADDRESS_IS_PREFERRED
2497   __ move(value.result(), addr);
2498 }
2499 
2500 
2501 void LIRGenerator::do_UnsafeGetObject(UnsafeGetObject* x) {
2502   BasicType type = x->basic_type();
2503   LIRItem src(x->object(), this);
2504   LIRItem off(x->offset(), this);
2505 
2506   off.load_item();
2507   src.load_item();
2508 
2509   LIR_Opr value = rlock_result(x, x->basic_type());
2510 
2511   if (support_IRIW_for_not_multiple_copy_atomic_cpu && x->is_volatile() && os::is_MP()) {
2512     __ membar();
2513   }
2514 
2515   get_Object_unsafe(value, src.result(), off.result(), type, x->is_volatile());
2516 
2517 #if INCLUDE_ALL_GCS
2518   // We might be reading the value of the referent field of a
2519   // Reference object in order to attach it back to the live
2520   // object graph. If G1 is enabled then we need to record
2521   // the value that is being returned in an SATB log buffer.
2522   //
2523   // We need to generate code similar to the following...
2524   //
2525   // if (offset == java_lang_ref_Reference::referent_offset) {
2526   //   if (src != NULL) {
2527   //     if (klass(src)->reference_type() != REF_NONE) {
2528   //       pre_barrier(..., value, ...);
2529   //     }
2530   //   }
2531   // }
2532 
2533   if ((UseShenandoahGC || UseG1GC) && type == T_OBJECT) {
2534     bool gen_pre_barrier = true;     // Assume we need to generate pre_barrier.
2535     bool gen_offset_check = true;    // Assume we need to generate the offset guard.
2536     bool gen_source_check = true;    // Assume we need to check the src object for null.
2537     bool gen_type_check = true;      // Assume we need to check the reference_type.
2538 
2539     if (off.is_constant()) {
2540       jlong off_con = (off.type()->is_int() ?
2541                         (jlong) off.get_jint_constant() :
2542                         off.get_jlong_constant());
2543 
2544 
2545       if (off_con != (jlong) java_lang_ref_Reference::referent_offset) {
2546         // The constant offset is something other than referent_offset.
2547         // We can skip generating/checking the remaining guards and
2548         // skip generation of the code stub.
2549         gen_pre_barrier = false;
2550       } else {
2551         // The constant offset is the same as referent_offset -
2552         // we do not need to generate a runtime offset check.
2553         gen_offset_check = false;
2554       }
2555     }
2556 
2557     // We don't need to generate stub if the source object is an array
2558     if (gen_pre_barrier && src.type()->is_array()) {
2559       gen_pre_barrier = false;
2560     }
2561 
2562     if (gen_pre_barrier) {
2563       // We still need to continue with the checks.
2564       if (src.is_constant()) {
2565         ciObject* src_con = src.get_jobject_constant();
2566         guarantee(src_con != NULL, "no source constant");
2567 
2568         if (src_con->is_null_object()) {
2569           // The constant src object is null - We can skip
2570           // generating the code stub.
2571           gen_pre_barrier = false;
2572         } else {
2573           // Non-null constant source object. We still have to generate
2574           // the slow stub - but we don't need to generate the runtime
2575           // null object check.
2576           gen_source_check = false;
2577         }
2578       }
2579     }
2580     if (gen_pre_barrier && !PatchALot) {
2581       // Can the klass of object be statically determined to be
2582       // a sub-class of Reference?
2583       ciType* type = src.value()->declared_type();
2584       if ((type != NULL) && type->is_loaded()) {
2585         if (type->is_subtype_of(compilation()->env()->Reference_klass())) {
2586           gen_type_check = false;
2587         } else if (type->is_klass() &&
2588                    !compilation()->env()->Object_klass()->is_subtype_of(type->as_klass())) {
2589           // Not Reference and not Object klass.
2590           gen_pre_barrier = false;
2591         }
2592       }
2593     }
2594 
2595     if (gen_pre_barrier) {
2596       LabelObj* Lcont = new LabelObj();
2597 
2598       // We can have generate one runtime check here. Let's start with
2599       // the offset check.
2600       if (gen_offset_check) {
2601         // if (offset != referent_offset) -> continue
2602         // If offset is an int then we can do the comparison with the
2603         // referent_offset constant; otherwise we need to move
2604         // referent_offset into a temporary register and generate
2605         // a reg-reg compare.
2606 
2607         LIR_Opr referent_off;
2608 
2609         if (off.type()->is_int()) {
2610           referent_off = LIR_OprFact::intConst(java_lang_ref_Reference::referent_offset);
2611         } else {
2612           assert(off.type()->is_long(), "what else?");
2613           referent_off = new_register(T_LONG);
2614           __ move(LIR_OprFact::longConst(java_lang_ref_Reference::referent_offset), referent_off);
2615         }
2616         __ cmp(lir_cond_notEqual, off.result(), referent_off);
2617         __ branch(lir_cond_notEqual, as_BasicType(off.type()), Lcont->label());
2618       }
2619       if (gen_source_check) {
2620         // offset is a const and equals referent offset
2621         // if (source == null) -> continue
2622         __ cmp(lir_cond_equal, src.result(), LIR_OprFact::oopConst(NULL));
2623         __ branch(lir_cond_equal, T_OBJECT, Lcont->label());
2624       }
2625       LIR_Opr src_klass = new_register(T_OBJECT);
2626       if (gen_type_check) {
2627         // We have determined that offset == referent_offset && src != null.
2628         // if (src->_klass->_reference_type == REF_NONE) -> continue
2629         __ move(new LIR_Address(src.result(), oopDesc::klass_offset_in_bytes(), T_ADDRESS), src_klass);
2630         LIR_Address* reference_type_addr = new LIR_Address(src_klass, in_bytes(InstanceKlass::reference_type_offset()), T_BYTE);
2631         LIR_Opr reference_type = new_register(T_INT);
2632         __ move(reference_type_addr, reference_type);
2633         __ cmp(lir_cond_equal, reference_type, LIR_OprFact::intConst(REF_NONE));
2634         __ branch(lir_cond_equal, T_INT, Lcont->label());
2635       }
2636       {
2637         // We have determined that src->_klass->_reference_type != REF_NONE
2638         // so register the value in the referent field with the pre-barrier.
2639         pre_barrier(LIR_OprFact::illegalOpr /* addr_opr */,
2640                     value  /* pre_val */,
2641                     false  /* do_load */,
2642                     false  /* patch */,
2643                     NULL   /* info */);
2644       }
2645       __ branch_destination(Lcont->label());
2646     }
2647   }
2648 #endif // INCLUDE_ALL_GCS
2649 
2650   if (x->is_volatile() && os::is_MP()) __ membar_acquire();
2651 
2652   /* Normalize boolean value returned by unsafe operation, i.e., value  != 0 ? value = true : value false. */
2653   if (type == T_BOOLEAN) {
2654     LabelObj* equalZeroLabel = new LabelObj();
2655     __ cmp(lir_cond_equal, value, 0);
2656     __ branch(lir_cond_equal, T_BOOLEAN, equalZeroLabel->label());
2657     __ move(LIR_OprFact::intConst(1), value);
2658     __ branch_destination(equalZeroLabel->label());
2659   }
2660 }
2661 
2662 
2663 void LIRGenerator::do_UnsafePutObject(UnsafePutObject* x) {
2664   BasicType type = x->basic_type();
2665   LIRItem src(x->object(), this);
2666   LIRItem off(x->offset(), this);
2667   LIRItem data(x->value(), this);
2668 
2669   src.load_item();
2670   if (type == T_BOOLEAN || type == T_BYTE) {
2671     data.load_byte_item();
2672   } else {
2673     data.load_item();
2674   }
2675   off.load_item();
2676 
2677   set_no_result(x);
2678 
2679   if (x->is_volatile() && os::is_MP()) __ membar_release();
2680   put_Object_unsafe(src.result(), off.result(), data.result(), type, x->is_volatile());
2681   if (!support_IRIW_for_not_multiple_copy_atomic_cpu && x->is_volatile() && os::is_MP()) __ membar();
2682 }
2683 
2684 
2685 void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {
2686   int lng = x->length();
2687 
2688   for (int i = 0; i < lng; i++) {
2689     SwitchRange* one_range = x->at(i);
2690     int low_key = one_range->low_key();
2691     int high_key = one_range->high_key();
2692     BlockBegin* dest = one_range->sux();
2693     if (low_key == high_key) {
2694       __ cmp(lir_cond_equal, value, low_key);
2695       __ branch(lir_cond_equal, T_INT, dest);
2696     } else if (high_key - low_key == 1) {
2697       __ cmp(lir_cond_equal, value, low_key);
2698       __ branch(lir_cond_equal, T_INT, dest);
2699       __ cmp(lir_cond_equal, value, high_key);
2700       __ branch(lir_cond_equal, T_INT, dest);
2701     } else {
2702       LabelObj* L = new LabelObj();
2703       __ cmp(lir_cond_less, value, low_key);
2704       __ branch(lir_cond_less, T_INT, L->label());
2705       __ cmp(lir_cond_lessEqual, value, high_key);
2706       __ branch(lir_cond_lessEqual, T_INT, dest);
2707       __ branch_destination(L->label());
2708     }
2709   }
2710   __ jump(default_sux);
2711 }
2712 
2713 
2714 SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {
2715   SwitchRangeList* res = new SwitchRangeList();
2716   int len = x->length();
2717   if (len > 0) {
2718     BlockBegin* sux = x->sux_at(0);
2719     int key = x->lo_key();
2720     BlockBegin* default_sux = x->default_sux();
2721     SwitchRange* range = new SwitchRange(key, sux);
2722     for (int i = 0; i < len; i++, key++) {
2723       BlockBegin* new_sux = x->sux_at(i);
2724       if (sux == new_sux) {
2725         // still in same range
2726         range->set_high_key(key);
2727       } else {
2728         // skip tests which explicitly dispatch to the default
2729         if (sux != default_sux) {
2730           res->append(range);
2731         }
2732         range = new SwitchRange(key, new_sux);
2733       }
2734       sux = new_sux;
2735     }
2736     if (res->length() == 0 || res->last() != range)  res->append(range);
2737   }
2738   return res;
2739 }
2740 
2741 
2742 // we expect the keys to be sorted by increasing value
2743 SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {
2744   SwitchRangeList* res = new SwitchRangeList();
2745   int len = x->length();
2746   if (len > 0) {
2747     BlockBegin* default_sux = x->default_sux();
2748     int key = x->key_at(0);
2749     BlockBegin* sux = x->sux_at(0);
2750     SwitchRange* range = new SwitchRange(key, sux);
2751     for (int i = 1; i < len; i++) {
2752       int new_key = x->key_at(i);
2753       BlockBegin* new_sux = x->sux_at(i);
2754       if (key+1 == new_key && sux == new_sux) {
2755         // still in same range
2756         range->set_high_key(new_key);
2757       } else {
2758         // skip tests which explicitly dispatch to the default
2759         if (range->sux() != default_sux) {
2760           res->append(range);
2761         }
2762         range = new SwitchRange(new_key, new_sux);
2763       }
2764       key = new_key;
2765       sux = new_sux;
2766     }
2767     if (res->length() == 0 || res->last() != range)  res->append(range);
2768   }
2769   return res;
2770 }
2771 
2772 
2773 void LIRGenerator::do_TableSwitch(TableSwitch* x) {
2774   LIRItem tag(x->tag(), this);
2775   tag.load_item();
2776   set_no_result(x);
2777 
2778   if (x->is_safepoint()) {
2779     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2780   }
2781 
2782   // move values into phi locations
2783   move_to_phi(x->state());
2784 
2785   int lo_key = x->lo_key();
2786   int hi_key = x->hi_key();
2787   int len = x->length();
2788   LIR_Opr value = tag.result();
2789 
2790   if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) {
2791     ciMethod* method = x->state()->scope()->method();
2792     ciMethodData* md = method->method_data_or_null();
2793     ciProfileData* data = md->bci_to_data(x->state()->bci());
2794     assert(data->is_MultiBranchData(), "bad profile data?");
2795     int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset());
2796     LIR_Opr md_reg = new_register(T_METADATA);
2797     __ metadata2reg(md->constant_encoding(), md_reg);
2798     LIR_Opr data_offset_reg = new_pointer_register();
2799     LIR_Opr tmp_reg = new_pointer_register();
2800 
2801     __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg);
2802     for (int i = 0; i < len; i++) {
2803       int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i));
2804       __ cmp(lir_cond_equal, value, i + lo_key);
2805       __ move(data_offset_reg, tmp_reg);
2806       __ cmove(lir_cond_equal,
2807                LIR_OprFact::intptrConst(count_offset),
2808                tmp_reg,
2809                data_offset_reg, T_INT);
2810     }
2811 
2812     LIR_Opr data_reg = new_pointer_register();
2813     LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
2814     __ move(data_addr, data_reg);
2815     __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg);
2816     __ move(data_reg, data_addr);
2817   }
2818 
2819   if (UseTableRanges) {
2820     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2821   } else {
2822     for (int i = 0; i < len; i++) {
2823       __ cmp(lir_cond_equal, value, i + lo_key);
2824       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2825     }
2826     __ jump(x->default_sux());
2827   }
2828 }
2829 
2830 
2831 void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {
2832   LIRItem tag(x->tag(), this);
2833   tag.load_item();
2834   set_no_result(x);
2835 
2836   if (x->is_safepoint()) {
2837     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2838   }
2839 
2840   // move values into phi locations
2841   move_to_phi(x->state());
2842 
2843   LIR_Opr value = tag.result();
2844   int len = x->length();
2845 
2846   if (compilation()->env()->comp_level() == CompLevel_full_profile && UseSwitchProfiling) {
2847     ciMethod* method = x->state()->scope()->method();
2848     ciMethodData* md = method->method_data_or_null();
2849     ciProfileData* data = md->bci_to_data(x->state()->bci());
2850     assert(data->is_MultiBranchData(), "bad profile data?");
2851     int default_count_offset = md->byte_offset_of_slot(data, MultiBranchData::default_count_offset());
2852     LIR_Opr md_reg = new_register(T_METADATA);
2853     __ metadata2reg(md->constant_encoding(), md_reg);
2854     LIR_Opr data_offset_reg = new_pointer_register();
2855     LIR_Opr tmp_reg = new_pointer_register();
2856 
2857     __ move(LIR_OprFact::intptrConst(default_count_offset), data_offset_reg);
2858     for (int i = 0; i < len; i++) {
2859       int count_offset = md->byte_offset_of_slot(data, MultiBranchData::case_count_offset(i));
2860       __ cmp(lir_cond_equal, value, x->key_at(i));
2861       __ move(data_offset_reg, tmp_reg);
2862       __ cmove(lir_cond_equal,
2863                LIR_OprFact::intptrConst(count_offset),
2864                tmp_reg,
2865                data_offset_reg, T_INT);
2866     }
2867 
2868     LIR_Opr data_reg = new_pointer_register();
2869     LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
2870     __ move(data_addr, data_reg);
2871     __ add(data_reg, LIR_OprFact::intptrConst(1), data_reg);
2872     __ move(data_reg, data_addr);
2873   }
2874 
2875   if (UseTableRanges) {
2876     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2877   } else {
2878     int len = x->length();
2879     for (int i = 0; i < len; i++) {
2880       __ cmp(lir_cond_equal, value, x->key_at(i));
2881       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2882     }
2883     __ jump(x->default_sux());
2884   }
2885 }
2886 
2887 
2888 void LIRGenerator::do_Goto(Goto* x) {
2889   set_no_result(x);
2890 
2891   if (block()->next()->as_OsrEntry()) {
2892     // need to free up storage used for OSR entry point
2893     LIR_Opr osrBuffer = block()->next()->operand();
2894     BasicTypeList signature;
2895     signature.append(NOT_LP64(T_INT) LP64_ONLY(T_LONG)); // pass a pointer to osrBuffer
2896     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2897     __ move(osrBuffer, cc->args()->at(0));
2898     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
2899                          getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());
2900   }
2901 
2902   if (x->is_safepoint()) {
2903     ValueStack* state = x->state_before() ? x->state_before() : x->state();
2904 
2905     // increment backedge counter if needed
2906     CodeEmitInfo* info = state_for(x, state);
2907     increment_backedge_counter(info, x->profiled_bci());
2908     CodeEmitInfo* safepoint_info = state_for(x, state);
2909     __ safepoint(safepoint_poll_register(), safepoint_info);
2910   }
2911 
2912   // Gotos can be folded Ifs, handle this case.
2913   if (x->should_profile()) {
2914     ciMethod* method = x->profiled_method();
2915     assert(method != NULL, "method should be set if branch is profiled");
2916     ciMethodData* md = method->method_data_or_null();
2917     assert(md != NULL, "Sanity");
2918     ciProfileData* data = md->bci_to_data(x->profiled_bci());
2919     assert(data != NULL, "must have profiling data");
2920     int offset;
2921     if (x->direction() == Goto::taken) {
2922       assert(data->is_BranchData(), "need BranchData for two-way branches");
2923       offset = md->byte_offset_of_slot(data, BranchData::taken_offset());
2924     } else if (x->direction() == Goto::not_taken) {
2925       assert(data->is_BranchData(), "need BranchData for two-way branches");
2926       offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
2927     } else {
2928       assert(data->is_JumpData(), "need JumpData for branches");
2929       offset = md->byte_offset_of_slot(data, JumpData::taken_offset());
2930     }
2931     LIR_Opr md_reg = new_register(T_METADATA);
2932     __ metadata2reg(md->constant_encoding(), md_reg);
2933 
2934     increment_counter(new LIR_Address(md_reg, offset,
2935                                       NOT_LP64(T_INT) LP64_ONLY(T_LONG)), DataLayout::counter_increment);
2936   }
2937 
2938   // emit phi-instruction move after safepoint since this simplifies
2939   // describing the state as the safepoint.
2940   move_to_phi(x->state());
2941 
2942   __ jump(x->default_sux());
2943 }
2944 
2945 /**
2946  * Emit profiling code if needed for arguments, parameters, return value types
2947  *
2948  * @param md                    MDO the code will update at runtime
2949  * @param md_base_offset        common offset in the MDO for this profile and subsequent ones
2950  * @param md_offset             offset in the MDO (on top of md_base_offset) for this profile
2951  * @param profiled_k            current profile
2952  * @param obj                   IR node for the object to be profiled
2953  * @param mdp                   register to hold the pointer inside the MDO (md + md_base_offset).
2954  *                              Set once we find an update to make and use for next ones.
2955  * @param not_null              true if we know obj cannot be null
2956  * @param signature_at_call_k   signature at call for obj
2957  * @param callee_signature_k    signature of callee for obj
2958  *                              at call and callee signatures differ at method handle call
2959  * @return                      the only klass we know will ever be seen at this profile point
2960  */
2961 ciKlass* LIRGenerator::profile_type(ciMethodData* md, int md_base_offset, int md_offset, intptr_t profiled_k,
2962                                     Value obj, LIR_Opr& mdp, bool not_null, ciKlass* signature_at_call_k,
2963                                     ciKlass* callee_signature_k) {
2964   ciKlass* result = NULL;
2965   bool do_null = !not_null && !TypeEntries::was_null_seen(profiled_k);
2966   bool do_update = !TypeEntries::is_type_unknown(profiled_k);
2967   // known not to be null or null bit already set and already set to
2968   // unknown: nothing we can do to improve profiling
2969   if (!do_null && !do_update) {
2970     return result;
2971   }
2972 
2973   ciKlass* exact_klass = NULL;
2974   Compilation* comp = Compilation::current();
2975   if (do_update) {
2976     // try to find exact type, using CHA if possible, so that loading
2977     // the klass from the object can be avoided
2978     ciType* type = obj->exact_type();
2979     if (type == NULL) {
2980       type = obj->declared_type();
2981       type = comp->cha_exact_type(type);
2982     }
2983     assert(type == NULL || type->is_klass(), "type should be class");
2984     exact_klass = (type != NULL && type->is_loaded()) ? (ciKlass*)type : NULL;
2985 
2986     do_update = exact_klass == NULL || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
2987   }
2988 
2989   if (!do_null && !do_update) {
2990     return result;
2991   }
2992 
2993   ciKlass* exact_signature_k = NULL;
2994   if (do_update) {
2995     // Is the type from the signature exact (the only one possible)?
2996     exact_signature_k = signature_at_call_k->exact_klass();
2997     if (exact_signature_k == NULL) {
2998       exact_signature_k = comp->cha_exact_type(signature_at_call_k);
2999     } else {
3000       result = exact_signature_k;
3001       // Known statically. No need to emit any code: prevent
3002       // LIR_Assembler::emit_profile_type() from emitting useless code
3003       profiled_k = ciTypeEntries::with_status(result, profiled_k);
3004     }
3005     // exact_klass and exact_signature_k can be both non NULL but
3006     // different if exact_klass is loaded after the ciObject for
3007     // exact_signature_k is created.
3008     if (exact_klass == NULL && exact_signature_k != NULL && exact_klass != exact_signature_k) {
3009       // sometimes the type of the signature is better than the best type
3010       // the compiler has
3011       exact_klass = exact_signature_k;
3012     }
3013     if (callee_signature_k != NULL &&
3014         callee_signature_k != signature_at_call_k) {
3015       ciKlass* improved_klass = callee_signature_k->exact_klass();
3016       if (improved_klass == NULL) {
3017         improved_klass = comp->cha_exact_type(callee_signature_k);
3018       }
3019       if (exact_klass == NULL && improved_klass != NULL && exact_klass != improved_klass) {
3020         exact_klass = exact_signature_k;
3021       }
3022     }
3023     do_update = exact_klass == NULL || ciTypeEntries::valid_ciklass(profiled_k) != exact_klass;
3024   }
3025 
3026   if (!do_null && !do_update) {
3027     return result;
3028   }
3029 
3030   if (mdp == LIR_OprFact::illegalOpr) {
3031     mdp = new_register(T_METADATA);
3032     __ metadata2reg(md->constant_encoding(), mdp);
3033     if (md_base_offset != 0) {
3034       LIR_Address* base_type_address = new LIR_Address(mdp, md_base_offset, T_ADDRESS);
3035       mdp = new_pointer_register();
3036       __ leal(LIR_OprFact::address(base_type_address), mdp);
3037     }
3038   }
3039   LIRItem value(obj, this);
3040   value.load_item();
3041   __ profile_type(new LIR_Address(mdp, md_offset, T_METADATA),
3042                   value.result(), exact_klass, profiled_k, new_pointer_register(), not_null, exact_signature_k != NULL);
3043   return result;
3044 }
3045 
3046 // profile parameters on entry to the root of the compilation
3047 void LIRGenerator::profile_parameters(Base* x) {
3048   if (compilation()->profile_parameters()) {
3049     CallingConvention* args = compilation()->frame_map()->incoming_arguments();
3050     ciMethodData* md = scope()->method()->method_data_or_null();
3051     assert(md != NULL, "Sanity");
3052 
3053     if (md->parameters_type_data() != NULL) {
3054       ciParametersTypeData* parameters_type_data = md->parameters_type_data();
3055       ciTypeStackSlotEntries* parameters =  parameters_type_data->parameters();
3056       LIR_Opr mdp = LIR_OprFact::illegalOpr;
3057       for (int java_index = 0, i = 0, j = 0; j < parameters_type_data->number_of_parameters(); i++) {
3058         LIR_Opr src = args->at(i);
3059         assert(!src->is_illegal(), "check");
3060         BasicType t = src->type();
3061         if (t == T_OBJECT || t == T_ARRAY) {
3062           intptr_t profiled_k = parameters->type(j);
3063           Local* local = x->state()->local_at(java_index)->as_Local();
3064           ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
3065                                         in_bytes(ParametersTypeData::type_offset(j)) - in_bytes(ParametersTypeData::type_offset(0)),
3066                                         profiled_k, local, mdp, false, local->declared_type()->as_klass(), NULL);
3067           // If the profile is known statically set it once for all and do not emit any code
3068           if (exact != NULL) {
3069             md->set_parameter_type(j, exact);
3070           }
3071           j++;
3072         }
3073         java_index += type2size[t];
3074       }
3075     }
3076   }
3077 }
3078 
3079 void LIRGenerator::do_Base(Base* x) {
3080   __ std_entry(LIR_OprFact::illegalOpr);
3081   // Emit moves from physical registers / stack slots to virtual registers
3082   CallingConvention* args = compilation()->frame_map()->incoming_arguments();
3083   IRScope* irScope = compilation()->hir()->top_scope();
3084   int java_index = 0;
3085   for (int i = 0; i < args->length(); i++) {
3086     LIR_Opr src = args->at(i);
3087     assert(!src->is_illegal(), "check");
3088     BasicType t = src->type();
3089 
3090     // Types which are smaller than int are passed as int, so
3091     // correct the type which passed.
3092     switch (t) {
3093     case T_BYTE:
3094     case T_BOOLEAN:
3095     case T_SHORT:
3096     case T_CHAR:
3097       t = T_INT;
3098       break;
3099     default:
3100       break;
3101     }
3102 
3103     LIR_Opr dest = new_register(t);
3104     __ move(src, dest);
3105 
3106     // Assign new location to Local instruction for this local
3107     Local* local = x->state()->local_at(java_index)->as_Local();
3108     assert(local != NULL, "Locals for incoming arguments must have been created");
3109 #ifndef __SOFTFP__
3110     // The java calling convention passes double as long and float as int.
3111     assert(as_ValueType(t)->tag() == local->type()->tag(), "check");
3112 #endif // __SOFTFP__
3113     local->set_operand(dest);
3114     _instruction_for_operand.at_put_grow(dest->vreg_number(), local, NULL);
3115     java_index += type2size[t];
3116   }
3117 
3118   if (compilation()->env()->dtrace_method_probes()) {
3119     BasicTypeList signature;
3120     signature.append(LP64_ONLY(T_LONG) NOT_LP64(T_INT));    // thread
3121     signature.append(T_METADATA); // Method*
3122     LIR_OprList* args = new LIR_OprList();
3123     args->append(getThreadPointer());
3124     LIR_Opr meth = new_register(T_METADATA);
3125     __ metadata2reg(method()->constant_encoding(), meth);
3126     args->append(meth);
3127     call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, NULL);
3128   }
3129 
3130   if (method()->is_synchronized()) {
3131     LIR_Opr obj;
3132     if (method()->is_static()) {
3133       obj = new_register(T_OBJECT);
3134       __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);
3135     } else {
3136       Local* receiver = x->state()->local_at(0)->as_Local();
3137       assert(receiver != NULL, "must already exist");
3138       obj = receiver->operand();
3139     }
3140     assert(obj->is_valid(), "must be valid");
3141 
3142     if (method()->is_synchronized() && GenerateSynchronizationCode) {
3143       LIR_Opr lock = syncLockOpr();
3144       __ load_stack_address_monitor(0, lock);
3145 
3146       CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL, x->check_flag(Instruction::DeoptimizeOnException));
3147       obj = shenandoah_write_barrier(obj, info, false);
3148       CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
3149 
3150       // receiver is guaranteed non-NULL so don't need CodeEmitInfo
3151       __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, NULL);
3152     }
3153   }
3154   if (compilation()->age_code()) {
3155     CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, 0), NULL, false);
3156     decrement_age(info);
3157   }
3158   // increment invocation counters if needed
3159   if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.
3160     profile_parameters(x);
3161     CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL, false);
3162     increment_invocation_counter(info);
3163   }
3164 
3165   // all blocks with a successor must end with an unconditional jump
3166   // to the successor even if they are consecutive
3167   __ jump(x->default_sux());
3168 }
3169 
3170 
3171 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
3172   // construct our frame and model the production of incoming pointer
3173   // to the OSR buffer.
3174   __ osr_entry(LIR_Assembler::osrBufferPointer());
3175   LIR_Opr result = rlock_result(x);
3176   __ move(LIR_Assembler::osrBufferPointer(), result);
3177 }
3178 
3179 
3180 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
3181   assert(args->length() == arg_list->length(),
3182          "args=%d, arg_list=%d", args->length(), arg_list->length());
3183   for (int i = x->has_receiver() ? 1 : 0; i < args->length(); i++) {
3184     LIRItem* param = args->at(i);
3185     LIR_Opr loc = arg_list->at(i);
3186     if (loc->is_register()) {
3187       param->load_item_force(loc);
3188     } else {
3189       LIR_Address* addr = loc->as_address_ptr();
3190       param->load_for_store(addr->type());
3191       if (addr->type() == T_OBJECT) {
3192         __ move_wide(param->result(), addr);
3193       } else
3194         if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
3195           __ unaligned_move(param->result(), addr);
3196         } else {
3197           __ move(param->result(), addr);
3198         }
3199     }
3200   }
3201 
3202   if (x->has_receiver()) {
3203     LIRItem* receiver = args->at(0);
3204     LIR_Opr loc = arg_list->at(0);
3205     if (loc->is_register()) {
3206       receiver->load_item_force(loc);
3207     } else {
3208       assert(loc->is_address(), "just checking");
3209       receiver->load_for_store(T_OBJECT);
3210       __ move_wide(receiver->result(), loc->as_address_ptr());
3211     }
3212   }
3213 }
3214 
3215 
3216 // Visits all arguments, returns appropriate items without loading them
3217 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
3218   LIRItemList* argument_items = new LIRItemList();
3219   if (x->has_receiver()) {
3220     LIRItem* receiver = new LIRItem(x->receiver(), this);
3221     argument_items->append(receiver);
3222   }
3223   for (int i = 0; i < x->number_of_arguments(); i++) {
3224     LIRItem* param = new LIRItem(x->argument_at(i), this);
3225     argument_items->append(param);
3226   }
3227   return argument_items;
3228 }
3229 
3230 
3231 // The invoke with receiver has following phases:
3232 //   a) traverse and load/lock receiver;
3233 //   b) traverse all arguments -> item-array (invoke_visit_argument)
3234 //   c) push receiver on stack
3235 //   d) load each of the items and push on stack
3236 //   e) unlock receiver
3237 //   f) move receiver into receiver-register %o0
3238 //   g) lock result registers and emit call operation
3239 //
3240 // Before issuing a call, we must spill-save all values on stack
3241 // that are in caller-save register. "spill-save" moves those registers
3242 // either in a free callee-save register or spills them if no free
3243 // callee save register is available.
3244 //
3245 // The problem is where to invoke spill-save.
3246 // - if invoked between e) and f), we may lock callee save
3247 //   register in "spill-save" that destroys the receiver register
3248 //   before f) is executed
3249 // - if we rearrange f) to be earlier (by loading %o0) it
3250 //   may destroy a value on the stack that is currently in %o0
3251 //   and is waiting to be spilled
3252 // - if we keep the receiver locked while doing spill-save,
3253 //   we cannot spill it as it is spill-locked
3254 //
3255 void LIRGenerator::do_Invoke(Invoke* x) {
3256   CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);
3257 
3258   LIR_OprList* arg_list = cc->args();
3259   LIRItemList* args = invoke_visit_arguments(x);
3260   LIR_Opr receiver = LIR_OprFact::illegalOpr;
3261 
3262   // setup result register
3263   LIR_Opr result_register = LIR_OprFact::illegalOpr;
3264   if (x->type() != voidType) {
3265     result_register = result_register_for(x->type());
3266   }
3267 
3268   CodeEmitInfo* info = state_for(x, x->state());
3269 
3270   invoke_load_arguments(x, args, arg_list);
3271 
3272   if (x->has_receiver()) {
3273     args->at(0)->load_item_force(LIR_Assembler::receiverOpr());
3274     receiver = args->at(0)->result();
3275   }
3276 
3277   // emit invoke code
3278   assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");
3279 
3280   // JSR 292
3281   // Preserve the SP over MethodHandle call sites, if needed.
3282   ciMethod* target = x->target();
3283   bool is_method_handle_invoke = (// %%% FIXME: Are both of these relevant?
3284                                   target->is_method_handle_intrinsic() ||
3285                                   target->is_compiled_lambda_form());
3286   if (is_method_handle_invoke) {
3287     info->set_is_method_handle_invoke(true);
3288     if(FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) {
3289         __ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr());
3290     }
3291   }
3292 
3293   switch (x->code()) {
3294     case Bytecodes::_invokestatic:
3295       __ call_static(target, result_register,
3296                      SharedRuntime::get_resolve_static_call_stub(),
3297                      arg_list, info);
3298       break;
3299     case Bytecodes::_invokespecial:
3300     case Bytecodes::_invokevirtual:
3301     case Bytecodes::_invokeinterface:
3302       // for loaded and final (method or class) target we still produce an inline cache,
3303       // in order to be able to call mixed mode
3304       if (x->code() == Bytecodes::_invokespecial || x->target_is_final()) {
3305         __ call_opt_virtual(target, receiver, result_register,
3306                             SharedRuntime::get_resolve_opt_virtual_call_stub(),
3307                             arg_list, info);
3308       } else if (x->vtable_index() < 0) {
3309         __ call_icvirtual(target, receiver, result_register,
3310                           SharedRuntime::get_resolve_virtual_call_stub(),
3311                           arg_list, info);
3312       } else {
3313         int entry_offset = in_bytes(Klass::vtable_start_offset()) + x->vtable_index() * vtableEntry::size_in_bytes();
3314         int vtable_offset = entry_offset + vtableEntry::method_offset_in_bytes();
3315         __ call_virtual(target, receiver, result_register, vtable_offset, arg_list, info);
3316       }
3317       break;
3318     case Bytecodes::_invokedynamic: {
3319       __ call_dynamic(target, receiver, result_register,
3320                       SharedRuntime::get_resolve_static_call_stub(),
3321                       arg_list, info);
3322       break;
3323     }
3324     default:
3325       fatal("unexpected bytecode: %s", Bytecodes::name(x->code()));
3326       break;
3327   }
3328 
3329   // JSR 292
3330   // Restore the SP after MethodHandle call sites, if needed.
3331   if (is_method_handle_invoke
3332       && FrameMap::method_handle_invoke_SP_save_opr() != LIR_OprFact::illegalOpr) {
3333     __ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer());
3334   }
3335 
3336   if (x->type()->is_float() || x->type()->is_double()) {
3337     // Force rounding of results from non-strictfp when in strictfp
3338     // scope (or when we don't know the strictness of the callee, to
3339     // be safe.)
3340     if (method()->is_strict()) {
3341       if (!x->target_is_loaded() || !x->target_is_strictfp()) {
3342         result_register = round_item(result_register);
3343       }
3344     }
3345   }
3346 
3347   if (result_register->is_valid()) {
3348     LIR_Opr result = rlock_result(x);
3349     __ move(result_register, result);
3350   }
3351 }
3352 
3353 
3354 void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {
3355   assert(x->number_of_arguments() == 1, "wrong type");
3356   LIRItem value       (x->argument_at(0), this);
3357   LIR_Opr reg = rlock_result(x);
3358   value.load_item();
3359   LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));
3360   __ move(tmp, reg);
3361 }
3362 
3363 
3364 
3365 // Code for  :  x->x() {x->cond()} x->y() ? x->tval() : x->fval()
3366 void LIRGenerator::do_IfOp(IfOp* x) {
3367 #ifdef ASSERT
3368   {
3369     ValueTag xtag = x->x()->type()->tag();
3370     ValueTag ttag = x->tval()->type()->tag();
3371     assert(xtag == intTag || xtag == objectTag, "cannot handle others");
3372     assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
3373     assert(ttag == x->fval()->type()->tag(), "cannot handle others");
3374   }
3375 #endif
3376 
3377   LIRItem left(x->x(), this);
3378   LIRItem right(x->y(), this);
3379   left.load_item();
3380   if (can_inline_as_constant(right.value())) {
3381     right.dont_load_item();
3382   } else {
3383     right.load_item();
3384   }
3385 
3386   LIRItem t_val(x->tval(), this);
3387   LIRItem f_val(x->fval(), this);
3388   t_val.dont_load_item();
3389   f_val.dont_load_item();
3390   LIR_Opr reg = rlock_result(x);
3391 
3392   __ cmp(lir_cond(x->cond()), left.result(), right.result());
3393   __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg, as_BasicType(x->x()->type()));
3394 }
3395 
3396 #ifdef TRACE_HAVE_INTRINSICS
3397 void LIRGenerator::do_ClassIDIntrinsic(Intrinsic* x) {
3398   CodeEmitInfo* info = state_for(x);
3399   CodeEmitInfo* info2 = new CodeEmitInfo(info); // Clone for the second null check
3400 
3401   assert(info != NULL, "must have info");
3402   LIRItem arg(x->argument_at(0), this);
3403 
3404   arg.load_item();
3405   LIR_Opr klass = new_register(T_METADATA);
3406   __ move(new LIR_Address(arg.result(), java_lang_Class::klass_offset_in_bytes(), T_ADDRESS), klass, info);
3407   LIR_Opr id = new_register(T_LONG);
3408   ByteSize offset = TRACE_KLASS_TRACE_ID_OFFSET;
3409   LIR_Address* trace_id_addr = new LIR_Address(klass, in_bytes(offset), T_LONG);
3410 
3411   __ move(trace_id_addr, id);
3412   __ logical_or(id, LIR_OprFact::longConst(0x01l), id);
3413   __ store(id, trace_id_addr);
3414 
3415 #ifdef TRACE_ID_META_BITS
3416   __ logical_and(id, LIR_OprFact::longConst(~TRACE_ID_META_BITS), id);
3417 #endif
3418 #ifdef TRACE_ID_CLASS_SHIFT
3419   __ unsigned_shift_right(id, TRACE_ID_CLASS_SHIFT, id);
3420 #endif
3421 
3422   __ move(id, rlock_result(x));
3423 }
3424 
3425 void LIRGenerator::do_getBufferWriter(Intrinsic* x) {
3426   LabelObj* L_end = new LabelObj();
3427 
3428   LIR_Address* jobj_addr = new LIR_Address(getThreadPointer(),
3429                                            in_bytes(TRACE_THREAD_DATA_WRITER_OFFSET),
3430                                            T_OBJECT);
3431   LIR_Opr result = rlock_result(x);
3432   __ move_wide(jobj_addr, result);
3433   __ cmp(lir_cond_equal, result, LIR_OprFact::oopConst(NULL));
3434   __ branch(lir_cond_equal, T_OBJECT, L_end->label());
3435   __ move_wide(new LIR_Address(result, T_OBJECT), result);
3436 
3437   __ branch_destination(L_end->label());
3438 }
3439 
3440 #endif
3441 
3442 
3443 void LIRGenerator::do_RuntimeCall(address routine, Intrinsic* x) {
3444   assert(x->number_of_arguments() == 0, "wrong type");
3445   // Enforce computation of _reserved_argument_area_size which is required on some platforms.
3446   BasicTypeList signature;
3447   CallingConvention* cc = frame_map()->c_calling_convention(&signature);
3448   LIR_Opr reg = result_register_for(x->type());
3449   __ call_runtime_leaf(routine, getThreadTemp(),
3450                        reg, new LIR_OprList());
3451   LIR_Opr result = rlock_result(x);
3452   __ move(reg, result);
3453 }
3454 
3455 
3456 
3457 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
3458   switch (x->id()) {
3459   case vmIntrinsics::_intBitsToFloat      :
3460   case vmIntrinsics::_doubleToRawLongBits :
3461   case vmIntrinsics::_longBitsToDouble    :
3462   case vmIntrinsics::_floatToRawIntBits   : {
3463     do_FPIntrinsics(x);
3464     break;
3465   }
3466 
3467 #ifdef TRACE_HAVE_INTRINSICS
3468   case vmIntrinsics::_getClassId:
3469     do_ClassIDIntrinsic(x);
3470     break;
3471   case vmIntrinsics::_getBufferWriter:
3472     do_getBufferWriter(x);
3473     break;
3474   case vmIntrinsics::_counterTime:
3475     do_RuntimeCall(CAST_FROM_FN_PTR(address, TRACE_TIME_METHOD), x);
3476     break;
3477 #endif
3478 
3479   case vmIntrinsics::_currentTimeMillis:
3480     do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeMillis), x);
3481     break;
3482 
3483   case vmIntrinsics::_nanoTime:
3484     do_RuntimeCall(CAST_FROM_FN_PTR(address, os::javaTimeNanos), x);
3485     break;
3486 
3487   case vmIntrinsics::_Object_init:    do_RegisterFinalizer(x); break;
3488   case vmIntrinsics::_isInstance:     do_isInstance(x);    break;
3489   case vmIntrinsics::_isPrimitive:    do_isPrimitive(x);   break;
3490   case vmIntrinsics::_getClass:       do_getClass(x);      break;
3491   case vmIntrinsics::_currentThread:  do_currentThread(x); break;
3492 
3493   case vmIntrinsics::_dlog:           // fall through
3494   case vmIntrinsics::_dlog10:         // fall through
3495   case vmIntrinsics::_dabs:           // fall through
3496   case vmIntrinsics::_dsqrt:          // fall through
3497   case vmIntrinsics::_dtan:           // fall through
3498   case vmIntrinsics::_dsin :          // fall through
3499   case vmIntrinsics::_dcos :          // fall through
3500   case vmIntrinsics::_dexp :          // fall through
3501   case vmIntrinsics::_dpow :          do_MathIntrinsic(x); break;
3502   case vmIntrinsics::_arraycopy:      do_ArrayCopy(x);     break;
3503 
3504   case vmIntrinsics::_fmaD:           do_FmaIntrinsic(x); break;
3505   case vmIntrinsics::_fmaF:           do_FmaIntrinsic(x); break;
3506 
3507   // java.nio.Buffer.checkIndex
3508   case vmIntrinsics::_checkIndex:     do_NIOCheckIndex(x); break;
3509 
3510   case vmIntrinsics::_compareAndSetObject:
3511     do_CompareAndSwap(x, objectType);
3512     break;
3513   case vmIntrinsics::_compareAndSetInt:
3514     do_CompareAndSwap(x, intType);
3515     break;
3516   case vmIntrinsics::_compareAndSetLong:
3517     do_CompareAndSwap(x, longType);
3518     break;
3519 
3520   case vmIntrinsics::_loadFence :
3521     if (os::is_MP()) __ membar_acquire();
3522     break;
3523   case vmIntrinsics::_storeFence:
3524     if (os::is_MP()) __ membar_release();
3525     break;
3526   case vmIntrinsics::_fullFence :
3527     if (os::is_MP()) __ membar();
3528     break;
3529   case vmIntrinsics::_onSpinWait:
3530     __ on_spin_wait();
3531     break;
3532   case vmIntrinsics::_Reference_get:
3533     do_Reference_get(x);
3534     break;
3535 
3536   case vmIntrinsics::_updateCRC32:
3537   case vmIntrinsics::_updateBytesCRC32:
3538   case vmIntrinsics::_updateByteBufferCRC32:
3539     do_update_CRC32(x);
3540     break;
3541 
3542   case vmIntrinsics::_updateBytesCRC32C:
3543   case vmIntrinsics::_updateDirectByteBufferCRC32C:
3544     do_update_CRC32C(x);
3545     break;
3546 
3547   case vmIntrinsics::_vectorizedMismatch:
3548     do_vectorizedMismatch(x);
3549     break;
3550 
3551   default: ShouldNotReachHere(); break;
3552   }
3553 }
3554 
3555 void LIRGenerator::profile_arguments(ProfileCall* x) {
3556   if (compilation()->profile_arguments()) {
3557     int bci = x->bci_of_invoke();
3558     ciMethodData* md = x->method()->method_data_or_null();
3559     ciProfileData* data = md->bci_to_data(bci);
3560     if (data != NULL) {
3561       if ((data->is_CallTypeData() && data->as_CallTypeData()->has_arguments()) ||
3562           (data->is_VirtualCallTypeData() && data->as_VirtualCallTypeData()->has_arguments())) {
3563         ByteSize extra = data->is_CallTypeData() ? CallTypeData::args_data_offset() : VirtualCallTypeData::args_data_offset();
3564         int base_offset = md->byte_offset_of_slot(data, extra);
3565         LIR_Opr mdp = LIR_OprFact::illegalOpr;
3566         ciTypeStackSlotEntries* args = data->is_CallTypeData() ? ((ciCallTypeData*)data)->args() : ((ciVirtualCallTypeData*)data)->args();
3567 
3568         Bytecodes::Code bc = x->method()->java_code_at_bci(bci);
3569         int start = 0;
3570         int stop = data->is_CallTypeData() ? ((ciCallTypeData*)data)->number_of_arguments() : ((ciVirtualCallTypeData*)data)->number_of_arguments();
3571         if (x->callee()->is_loaded() && x->callee()->is_static() && Bytecodes::has_receiver(bc)) {
3572           // first argument is not profiled at call (method handle invoke)
3573           assert(x->method()->raw_code_at_bci(bci) == Bytecodes::_invokehandle, "invokehandle expected");
3574           start = 1;
3575         }
3576         ciSignature* callee_signature = x->callee()->signature();
3577         // method handle call to virtual method
3578         bool has_receiver = x->callee()->is_loaded() && !x->callee()->is_static() && !Bytecodes::has_receiver(bc);
3579         ciSignatureStream callee_signature_stream(callee_signature, has_receiver ? x->callee()->holder() : NULL);
3580 
3581         bool ignored_will_link;
3582         ciSignature* signature_at_call = NULL;
3583         x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3584         ciSignatureStream signature_at_call_stream(signature_at_call);
3585 
3586         // if called through method handle invoke, some arguments may have been popped
3587         for (int i = 0; i < stop && i+start < x->nb_profiled_args(); i++) {
3588           int off = in_bytes(TypeEntriesAtCall::argument_type_offset(i)) - in_bytes(TypeEntriesAtCall::args_data_offset());
3589           ciKlass* exact = profile_type(md, base_offset, off,
3590               args->type(i), x->profiled_arg_at(i+start), mdp,
3591               !x->arg_needs_null_check(i+start),
3592               signature_at_call_stream.next_klass(), callee_signature_stream.next_klass());
3593           if (exact != NULL) {
3594             md->set_argument_type(bci, i, exact);
3595           }
3596         }
3597       } else {
3598 #ifdef ASSERT
3599         Bytecodes::Code code = x->method()->raw_code_at_bci(x->bci_of_invoke());
3600         int n = x->nb_profiled_args();
3601         assert(MethodData::profile_parameters() && (MethodData::profile_arguments_jsr292_only() ||
3602             (x->inlined() && ((code == Bytecodes::_invokedynamic && n <= 1) || (code == Bytecodes::_invokehandle && n <= 2)))),
3603             "only at JSR292 bytecodes");
3604 #endif
3605       }
3606     }
3607   }
3608 }
3609 
3610 // profile parameters on entry to an inlined method
3611 void LIRGenerator::profile_parameters_at_call(ProfileCall* x) {
3612   if (compilation()->profile_parameters() && x->inlined()) {
3613     ciMethodData* md = x->callee()->method_data_or_null();
3614     if (md != NULL) {
3615       ciParametersTypeData* parameters_type_data = md->parameters_type_data();
3616       if (parameters_type_data != NULL) {
3617         ciTypeStackSlotEntries* parameters =  parameters_type_data->parameters();
3618         LIR_Opr mdp = LIR_OprFact::illegalOpr;
3619         bool has_receiver = !x->callee()->is_static();
3620         ciSignature* sig = x->callee()->signature();
3621         ciSignatureStream sig_stream(sig, has_receiver ? x->callee()->holder() : NULL);
3622         int i = 0; // to iterate on the Instructions
3623         Value arg = x->recv();
3624         bool not_null = false;
3625         int bci = x->bci_of_invoke();
3626         Bytecodes::Code bc = x->method()->java_code_at_bci(bci);
3627         // The first parameter is the receiver so that's what we start
3628         // with if it exists. One exception is method handle call to
3629         // virtual method: the receiver is in the args list
3630         if (arg == NULL || !Bytecodes::has_receiver(bc)) {
3631           i = 1;
3632           arg = x->profiled_arg_at(0);
3633           not_null = !x->arg_needs_null_check(0);
3634         }
3635         int k = 0; // to iterate on the profile data
3636         for (;;) {
3637           intptr_t profiled_k = parameters->type(k);
3638           ciKlass* exact = profile_type(md, md->byte_offset_of_slot(parameters_type_data, ParametersTypeData::type_offset(0)),
3639                                         in_bytes(ParametersTypeData::type_offset(k)) - in_bytes(ParametersTypeData::type_offset(0)),
3640                                         profiled_k, arg, mdp, not_null, sig_stream.next_klass(), NULL);
3641           // If the profile is known statically set it once for all and do not emit any code
3642           if (exact != NULL) {
3643             md->set_parameter_type(k, exact);
3644           }
3645           k++;
3646           if (k >= parameters_type_data->number_of_parameters()) {
3647 #ifdef ASSERT
3648             int extra = 0;
3649             if (MethodData::profile_arguments() && TypeProfileParmsLimit != -1 &&
3650                 x->nb_profiled_args() >= TypeProfileParmsLimit &&
3651                 x->recv() != NULL && Bytecodes::has_receiver(bc)) {
3652               extra += 1;
3653             }
3654             assert(i == x->nb_profiled_args() - extra || (TypeProfileParmsLimit != -1 && TypeProfileArgsLimit > TypeProfileParmsLimit), "unused parameters?");
3655 #endif
3656             break;
3657           }
3658           arg = x->profiled_arg_at(i);
3659           not_null = !x->arg_needs_null_check(i);
3660           i++;
3661         }
3662       }
3663     }
3664   }
3665 }
3666 
3667 void LIRGenerator::do_ProfileCall(ProfileCall* x) {
3668   // Need recv in a temporary register so it interferes with the other temporaries
3669   LIR_Opr recv = LIR_OprFact::illegalOpr;
3670   LIR_Opr mdo = new_register(T_OBJECT);
3671   // tmp is used to hold the counters on SPARC
3672   LIR_Opr tmp = new_pointer_register();
3673 
3674   if (x->nb_profiled_args() > 0) {
3675     profile_arguments(x);
3676   }
3677 
3678   // profile parameters on inlined method entry including receiver
3679   if (x->recv() != NULL || x->nb_profiled_args() > 0) {
3680     profile_parameters_at_call(x);
3681   }
3682 
3683   if (x->recv() != NULL) {
3684     LIRItem value(x->recv(), this);
3685     value.load_item();
3686     recv = new_register(T_OBJECT);
3687     __ move(value.result(), recv);
3688   }
3689   __ profile_call(x->method(), x->bci_of_invoke(), x->callee(), mdo, recv, tmp, x->known_holder());
3690 }
3691 
3692 void LIRGenerator::do_ProfileReturnType(ProfileReturnType* x) {
3693   int bci = x->bci_of_invoke();
3694   ciMethodData* md = x->method()->method_data_or_null();
3695   ciProfileData* data = md->bci_to_data(bci);
3696   if (data != NULL) {
3697     assert(data->is_CallTypeData() || data->is_VirtualCallTypeData(), "wrong profile data type");
3698     ciReturnTypeEntry* ret = data->is_CallTypeData() ? ((ciCallTypeData*)data)->ret() : ((ciVirtualCallTypeData*)data)->ret();
3699     LIR_Opr mdp = LIR_OprFact::illegalOpr;
3700 
3701     bool ignored_will_link;
3702     ciSignature* signature_at_call = NULL;
3703     x->method()->get_method_at_bci(bci, ignored_will_link, &signature_at_call);
3704 
3705     // The offset within the MDO of the entry to update may be too large
3706     // to be used in load/store instructions on some platforms. So have
3707     // profile_type() compute the address of the profile in a register.
3708     ciKlass* exact = profile_type(md, md->byte_offset_of_slot(data, ret->type_offset()), 0,
3709         ret->type(), x->ret(), mdp,
3710         !x->needs_null_check(),
3711         signature_at_call->return_type()->as_klass(),
3712         x->callee()->signature()->return_type()->as_klass());
3713     if (exact != NULL) {
3714       md->set_return_type(bci, exact);
3715     }
3716   }
3717 }
3718 
3719 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {
3720   // We can safely ignore accessors here, since c2 will inline them anyway,
3721   // accessors are also always mature.
3722   if (!x->inlinee()->is_accessor()) {
3723     CodeEmitInfo* info = state_for(x, x->state(), true);
3724     // Notify the runtime very infrequently only to take care of counter overflows
3725     int freq_log = Tier23InlineeNotifyFreqLog;
3726     double scale;
3727     if (_method->has_option_value("CompileThresholdScaling", scale)) {
3728       freq_log = Arguments::scaled_freq_log(freq_log, scale);
3729     }
3730     increment_event_counter_impl(info, x->inlinee(), right_n_bits(freq_log), InvocationEntryBci, false, true);
3731   }
3732 }
3733 
3734 void LIRGenerator::increment_event_counter(CodeEmitInfo* info, int bci, bool backedge) {
3735   int freq_log = 0;
3736   int level = compilation()->env()->comp_level();
3737   if (level == CompLevel_limited_profile) {
3738     freq_log = (backedge ? Tier2BackedgeNotifyFreqLog : Tier2InvokeNotifyFreqLog);
3739   } else if (level == CompLevel_full_profile) {
3740     freq_log = (backedge ? Tier3BackedgeNotifyFreqLog : Tier3InvokeNotifyFreqLog);
3741   } else {
3742     ShouldNotReachHere();
3743   }
3744   // Increment the appropriate invocation/backedge counter and notify the runtime.
3745   double scale;
3746   if (_method->has_option_value("CompileThresholdScaling", scale)) {
3747     freq_log = Arguments::scaled_freq_log(freq_log, scale);
3748   }
3749   increment_event_counter_impl(info, info->scope()->method(), right_n_bits(freq_log), bci, backedge, true);
3750 }
3751 
3752 void LIRGenerator::decrement_age(CodeEmitInfo* info) {
3753   ciMethod* method = info->scope()->method();
3754   MethodCounters* mc_adr = method->ensure_method_counters();
3755   if (mc_adr != NULL) {
3756     LIR_Opr mc = new_pointer_register();
3757     __ move(LIR_OprFact::intptrConst(mc_adr), mc);
3758     int offset = in_bytes(MethodCounters::nmethod_age_offset());
3759     LIR_Address* counter = new LIR_Address(mc, offset, T_INT);
3760     LIR_Opr result = new_register(T_INT);
3761     __ load(counter, result);
3762     __ sub(result, LIR_OprFact::intConst(1), result);
3763     __ store(result, counter);
3764     // DeoptimizeStub will reexecute from the current state in code info.
3765     CodeStub* deopt = new DeoptimizeStub(info, Deoptimization::Reason_tenured,
3766                                          Deoptimization::Action_make_not_entrant);
3767     __ cmp(lir_cond_lessEqual, result, LIR_OprFact::intConst(0));
3768     __ branch(lir_cond_lessEqual, T_INT, deopt);
3769   }
3770 }
3771 
3772 
3773 void LIRGenerator::increment_event_counter_impl(CodeEmitInfo* info,
3774                                                 ciMethod *method, int frequency,
3775                                                 int bci, bool backedge, bool notify) {
3776   assert(frequency == 0 || is_power_of_2(frequency + 1), "Frequency must be x^2 - 1 or 0");
3777   int level = _compilation->env()->comp_level();
3778   assert(level > CompLevel_simple, "Shouldn't be here");
3779 
3780   int offset = -1;
3781   LIR_Opr counter_holder = NULL;
3782   if (level == CompLevel_limited_profile) {
3783     MethodCounters* counters_adr = method->ensure_method_counters();
3784     if (counters_adr == NULL) {
3785       bailout("method counters allocation failed");
3786       return;
3787     }
3788     counter_holder = new_pointer_register();
3789     __ move(LIR_OprFact::intptrConst(counters_adr), counter_holder);
3790     offset = in_bytes(backedge ? MethodCounters::backedge_counter_offset() :
3791                                  MethodCounters::invocation_counter_offset());
3792   } else if (level == CompLevel_full_profile) {
3793     counter_holder = new_register(T_METADATA);
3794     offset = in_bytes(backedge ? MethodData::backedge_counter_offset() :
3795                                  MethodData::invocation_counter_offset());
3796     ciMethodData* md = method->method_data_or_null();
3797     assert(md != NULL, "Sanity");
3798     __ metadata2reg(md->constant_encoding(), counter_holder);
3799   } else {
3800     ShouldNotReachHere();
3801   }
3802   LIR_Address* counter = new LIR_Address(counter_holder, offset, T_INT);
3803   LIR_Opr result = new_register(T_INT);
3804   __ load(counter, result);
3805   __ add(result, LIR_OprFact::intConst(InvocationCounter::count_increment), result);
3806   __ store(result, counter);
3807   if (notify && (!backedge || UseOnStackReplacement)) {
3808     LIR_Opr meth = LIR_OprFact::metadataConst(method->constant_encoding());
3809     // The bci for info can point to cmp for if's we want the if bci
3810     CodeStub* overflow = new CounterOverflowStub(info, bci, meth);
3811     int freq = frequency << InvocationCounter::count_shift;
3812     if (freq == 0) {
3813       __ branch(lir_cond_always, T_ILLEGAL, overflow);
3814     } else {
3815       LIR_Opr mask = load_immediate(freq, T_INT);
3816       __ logical_and(result, mask, result);
3817       __ cmp(lir_cond_equal, result, LIR_OprFact::intConst(0));
3818       __ branch(lir_cond_equal, T_INT, overflow);
3819     }
3820     __ branch_destination(overflow->continuation());
3821   }
3822 }
3823 
3824 void LIRGenerator::do_RuntimeCall(RuntimeCall* x) {
3825   LIR_OprList* args = new LIR_OprList(x->number_of_arguments());
3826   BasicTypeList* signature = new BasicTypeList(x->number_of_arguments());
3827 
3828   if (x->pass_thread()) {
3829     signature->append(LP64_ONLY(T_LONG) NOT_LP64(T_INT));    // thread
3830     args->append(getThreadPointer());
3831   }
3832 
3833   for (int i = 0; i < x->number_of_arguments(); i++) {
3834     Value a = x->argument_at(i);
3835     LIRItem* item = new LIRItem(a, this);
3836     item->load_item();
3837     args->append(item->result());
3838     signature->append(as_BasicType(a->type()));
3839   }
3840 
3841   LIR_Opr result = call_runtime(signature, args, x->entry(), x->type(), NULL);
3842   if (x->type() == voidType) {
3843     set_no_result(x);
3844   } else {
3845     __ move(result, rlock_result(x));
3846   }
3847 }
3848 
3849 #ifdef ASSERT
3850 void LIRGenerator::do_Assert(Assert *x) {
3851   ValueTag tag = x->x()->type()->tag();
3852   If::Condition cond = x->cond();
3853 
3854   LIRItem xitem(x->x(), this);
3855   LIRItem yitem(x->y(), this);
3856   LIRItem* xin = &xitem;
3857   LIRItem* yin = &yitem;
3858 
3859   assert(tag == intTag, "Only integer assertions are valid!");
3860 
3861   xin->load_item();
3862   yin->dont_load_item();
3863 
3864   set_no_result(x);
3865 
3866   LIR_Opr left = xin->result();
3867   LIR_Opr right = yin->result();
3868 
3869   __ lir_assert(lir_cond(x->cond()), left, right, x->message(), true);
3870 }
3871 #endif
3872 
3873 void LIRGenerator::do_RangeCheckPredicate(RangeCheckPredicate *x) {
3874 
3875 
3876   Instruction *a = x->x();
3877   Instruction *b = x->y();
3878   if (!a || StressRangeCheckElimination) {
3879     assert(!b || StressRangeCheckElimination, "B must also be null");
3880 
3881     CodeEmitInfo *info = state_for(x, x->state());
3882     CodeStub* stub = new PredicateFailedStub(info);
3883 
3884     __ jump(stub);
3885   } else if (a->type()->as_IntConstant() && b->type()->as_IntConstant()) {
3886     int a_int = a->type()->as_IntConstant()->value();
3887     int b_int = b->type()->as_IntConstant()->value();
3888 
3889     bool ok = false;
3890 
3891     switch(x->cond()) {
3892       case Instruction::eql: ok = (a_int == b_int); break;
3893       case Instruction::neq: ok = (a_int != b_int); break;
3894       case Instruction::lss: ok = (a_int < b_int); break;
3895       case Instruction::leq: ok = (a_int <= b_int); break;
3896       case Instruction::gtr: ok = (a_int > b_int); break;
3897       case Instruction::geq: ok = (a_int >= b_int); break;
3898       case Instruction::aeq: ok = ((unsigned int)a_int >= (unsigned int)b_int); break;
3899       case Instruction::beq: ok = ((unsigned int)a_int <= (unsigned int)b_int); break;
3900       default: ShouldNotReachHere();
3901     }
3902 
3903     if (ok) {
3904 
3905       CodeEmitInfo *info = state_for(x, x->state());
3906       CodeStub* stub = new PredicateFailedStub(info);
3907 
3908       __ jump(stub);
3909     }
3910   } else {
3911 
3912     ValueTag tag = x->x()->type()->tag();
3913     If::Condition cond = x->cond();
3914     LIRItem xitem(x->x(), this);
3915     LIRItem yitem(x->y(), this);
3916     LIRItem* xin = &xitem;
3917     LIRItem* yin = &yitem;
3918 
3919     assert(tag == intTag, "Only integer deoptimizations are valid!");
3920 
3921     xin->load_item();
3922     yin->dont_load_item();
3923     set_no_result(x);
3924 
3925     LIR_Opr left = xin->result();
3926     LIR_Opr right = yin->result();
3927 
3928     CodeEmitInfo *info = state_for(x, x->state());
3929     CodeStub* stub = new PredicateFailedStub(info);
3930 
3931     __ cmp(lir_cond(cond), left, right);
3932     __ branch(lir_cond(cond), right->type(), stub);
3933   }
3934 }
3935 
3936 
3937 LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
3938   LIRItemList args(1);
3939   LIRItem value(arg1, this);
3940   args.append(&value);
3941   BasicTypeList signature;
3942   signature.append(as_BasicType(arg1->type()));
3943 
3944   return call_runtime(&signature, &args, entry, result_type, info);
3945 }
3946 
3947 
3948 LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {
3949   LIRItemList args(2);
3950   LIRItem value1(arg1, this);
3951   LIRItem value2(arg2, this);
3952   args.append(&value1);
3953   args.append(&value2);
3954   BasicTypeList signature;
3955   signature.append(as_BasicType(arg1->type()));
3956   signature.append(as_BasicType(arg2->type()));
3957 
3958   return call_runtime(&signature, &args, entry, result_type, info);
3959 }
3960 
3961 
3962 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,
3963                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
3964   // get a result register
3965   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
3966   LIR_Opr result = LIR_OprFact::illegalOpr;
3967   if (result_type->tag() != voidTag) {
3968     result = new_register(result_type);
3969     phys_reg = result_register_for(result_type);
3970   }
3971 
3972   // move the arguments into the correct location
3973   CallingConvention* cc = frame_map()->c_calling_convention(signature);
3974   assert(cc->length() == args->length(), "argument mismatch");
3975   for (int i = 0; i < args->length(); i++) {
3976     LIR_Opr arg = args->at(i);
3977     LIR_Opr loc = cc->at(i);
3978     if (loc->is_register()) {
3979       __ move(arg, loc);
3980     } else {
3981       LIR_Address* addr = loc->as_address_ptr();
3982 //           if (!can_store_as_constant(arg)) {
3983 //             LIR_Opr tmp = new_register(arg->type());
3984 //             __ move(arg, tmp);
3985 //             arg = tmp;
3986 //           }
3987       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
3988         __ unaligned_move(arg, addr);
3989       } else {
3990         __ move(arg, addr);
3991       }
3992     }
3993   }
3994 
3995   if (info) {
3996     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
3997   } else {
3998     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
3999   }
4000   if (result->is_valid()) {
4001     __ move(phys_reg, result);
4002   }
4003   return result;
4004 }
4005 
4006 
4007 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,
4008                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
4009   // get a result register
4010   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
4011   LIR_Opr result = LIR_OprFact::illegalOpr;
4012   if (result_type->tag() != voidTag) {
4013     result = new_register(result_type);
4014     phys_reg = result_register_for(result_type);
4015   }
4016 
4017   // move the arguments into the correct location
4018   CallingConvention* cc = frame_map()->c_calling_convention(signature);
4019 
4020   assert(cc->length() == args->length(), "argument mismatch");
4021   for (int i = 0; i < args->length(); i++) {
4022     LIRItem* arg = args->at(i);
4023     LIR_Opr loc = cc->at(i);
4024     if (loc->is_register()) {
4025       arg->load_item_force(loc);
4026     } else {
4027       LIR_Address* addr = loc->as_address_ptr();
4028       arg->load_for_store(addr->type());
4029       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
4030         __ unaligned_move(arg->result(), addr);
4031       } else {
4032         __ move(arg->result(), addr);
4033       }
4034     }
4035   }
4036 
4037   if (info) {
4038     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
4039   } else {
4040     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
4041   }
4042   if (result->is_valid()) {
4043     __ move(phys_reg, result);
4044   }
4045   return result;
4046 }
4047 
4048 void LIRGenerator::do_MemBar(MemBar* x) {
4049   if (os::is_MP()) {
4050     LIR_Code code = x->code();
4051     switch(code) {
4052       case lir_membar_acquire   : __ membar_acquire(); break;
4053       case lir_membar_release   : __ membar_release(); break;
4054       case lir_membar           : __ membar(); break;
4055       case lir_membar_loadload  : __ membar_loadload(); break;
4056       case lir_membar_storestore: __ membar_storestore(); break;
4057       case lir_membar_loadstore : __ membar_loadstore(); break;
4058       case lir_membar_storeload : __ membar_storeload(); break;
4059       default                   : ShouldNotReachHere(); break;
4060     }
4061   }
4062 }
4063 
4064 LIR_Opr LIRGenerator::maybe_mask_boolean(StoreIndexed* x, LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info) {
4065   if (x->check_boolean()) {
4066     LIR_Opr value_fixed = rlock_byte(T_BYTE);
4067     if (TwoOperandLIRForm) {
4068       __ move(value, value_fixed);
4069       __ logical_and(value_fixed, LIR_OprFact::intConst(1), value_fixed);
4070     } else {
4071       __ logical_and(value, LIR_OprFact::intConst(1), value_fixed);
4072     }
4073     LIR_Opr klass = new_register(T_METADATA);
4074     __ move(new LIR_Address(array, oopDesc::klass_offset_in_bytes(), T_ADDRESS), klass, null_check_info);
4075     null_check_info = NULL;
4076     LIR_Opr layout = new_register(T_INT);
4077     __ move(new LIR_Address(klass, in_bytes(Klass::layout_helper_offset()), T_INT), layout);
4078     int diffbit = Klass::layout_helper_boolean_diffbit();
4079     __ logical_and(layout, LIR_OprFact::intConst(diffbit), layout);
4080     __ cmp(lir_cond_notEqual, layout, LIR_OprFact::intConst(0));
4081     __ cmove(lir_cond_notEqual, value_fixed, value, value_fixed, T_BYTE);
4082     value = value_fixed;
4083   }
4084   return value;
4085 }