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