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