1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_c1_LIRGenerator.cpp.incl"
  27 
  28 #ifdef ASSERT
  29 #define __ gen()->lir(__FILE__, __LINE__)->
  30 #else
  31 #define __ gen()->lir()->
  32 #endif
  33 
  34 // TODO: ARM - Use some recognizable constant which still fits architectural constraints
  35 #ifdef ARM
  36 #define PATCHED_ADDR  (204)
  37 #else
  38 #define PATCHED_ADDR  (max_jint)
  39 #endif
  40 
  41 void PhiResolverState::reset(int max_vregs) {
  42   // Initialize array sizes
  43   _virtual_operands.at_put_grow(max_vregs - 1, NULL, NULL);
  44   _virtual_operands.trunc_to(0);
  45   _other_operands.at_put_grow(max_vregs - 1, NULL, NULL);
  46   _other_operands.trunc_to(0);
  47   _vreg_table.at_put_grow(max_vregs - 1, NULL, NULL);
  48   _vreg_table.trunc_to(0);
  49 }
  50 
  51 
  52 
  53 //--------------------------------------------------------------
  54 // PhiResolver
  55 
  56 // Resolves cycles:
  57 //
  58 //  r1 := r2  becomes  temp := r1
  59 //  r2 := r1           r1 := r2
  60 //                     r2 := temp
  61 // and orders moves:
  62 //
  63 //  r2 := r3  becomes  r1 := r2
  64 //  r1 := r2           r2 := r3
  65 
  66 PhiResolver::PhiResolver(LIRGenerator* gen, int max_vregs)
  67  : _gen(gen)
  68  , _state(gen->resolver_state())
  69  , _temp(LIR_OprFact::illegalOpr)
  70 {
  71   // reinitialize the shared state arrays
  72   _state.reset(max_vregs);
  73 }
  74 
  75 
  76 void PhiResolver::emit_move(LIR_Opr src, LIR_Opr dest) {
  77   assert(src->is_valid(), "");
  78   assert(dest->is_valid(), "");
  79   __ move(src, dest);
  80 }
  81 
  82 
  83 void PhiResolver::move_temp_to(LIR_Opr dest) {
  84   assert(_temp->is_valid(), "");
  85   emit_move(_temp, dest);
  86   NOT_PRODUCT(_temp = LIR_OprFact::illegalOpr);
  87 }
  88 
  89 
  90 void PhiResolver::move_to_temp(LIR_Opr src) {
  91   assert(_temp->is_illegal(), "");
  92   _temp = _gen->new_register(src->type());
  93   emit_move(src, _temp);
  94 }
  95 
  96 
  97 // Traverse assignment graph in depth first order and generate moves in post order
  98 // ie. two assignments: b := c, a := b start with node c:
  99 // Call graph: move(NULL, c) -> move(c, b) -> move(b, a)
 100 // Generates moves in this order: move b to a and move c to b
 101 // ie. cycle a := b, b := a start with node a
 102 // Call graph: move(NULL, a) -> move(a, b) -> move(b, a)
 103 // Generates moves in this order: move b to temp, move a to b, move temp to a
 104 void PhiResolver::move(ResolveNode* src, ResolveNode* dest) {
 105   if (!dest->visited()) {
 106     dest->set_visited();
 107     for (int i = dest->no_of_destinations()-1; i >= 0; i --) {
 108       move(dest, dest->destination_at(i));
 109     }
 110   } else if (!dest->start_node()) {
 111     // cylce in graph detected
 112     assert(_loop == NULL, "only one loop valid!");
 113     _loop = dest;
 114     move_to_temp(src->operand());
 115     return;
 116   } // else dest is a start node
 117 
 118   if (!dest->assigned()) {
 119     if (_loop == dest) {
 120       move_temp_to(dest->operand());
 121       dest->set_assigned();
 122     } else if (src != NULL) {
 123       emit_move(src->operand(), dest->operand());
 124       dest->set_assigned();
 125     }
 126   }
 127 }
 128 
 129 
 130 PhiResolver::~PhiResolver() {
 131   int i;
 132   // resolve any cycles in moves from and to virtual registers
 133   for (i = virtual_operands().length() - 1; i >= 0; i --) {
 134     ResolveNode* node = virtual_operands()[i];
 135     if (!node->visited()) {
 136       _loop = NULL;
 137       move(NULL, node);
 138       node->set_start_node();
 139       assert(_temp->is_illegal(), "move_temp_to() call missing");
 140     }
 141   }
 142 
 143   // generate move for move from non virtual register to abitrary destination
 144   for (i = other_operands().length() - 1; i >= 0; i --) {
 145     ResolveNode* node = other_operands()[i];
 146     for (int j = node->no_of_destinations() - 1; j >= 0; j --) {
 147       emit_move(node->operand(), node->destination_at(j)->operand());
 148     }
 149   }
 150 }
 151 
 152 
 153 ResolveNode* PhiResolver::create_node(LIR_Opr opr, bool source) {
 154   ResolveNode* node;
 155   if (opr->is_virtual()) {
 156     int vreg_num = opr->vreg_number();
 157     node = vreg_table().at_grow(vreg_num, NULL);
 158     assert(node == NULL || node->operand() == opr, "");
 159     if (node == NULL) {
 160       node = new ResolveNode(opr);
 161       vreg_table()[vreg_num] = node;
 162     }
 163     // Make sure that all virtual operands show up in the list when
 164     // they are used as the source of a move.
 165     if (source && !virtual_operands().contains(node)) {
 166       virtual_operands().append(node);
 167     }
 168   } else {
 169     assert(source, "");
 170     node = new ResolveNode(opr);
 171     other_operands().append(node);
 172   }
 173   return node;
 174 }
 175 
 176 
 177 void PhiResolver::move(LIR_Opr src, LIR_Opr dest) {
 178   assert(dest->is_virtual(), "");
 179   // tty->print("move "); src->print(); tty->print(" to "); dest->print(); tty->cr();
 180   assert(src->is_valid(), "");
 181   assert(dest->is_valid(), "");
 182   ResolveNode* source = source_node(src);
 183   source->append(destination_node(dest));
 184 }
 185 
 186 
 187 //--------------------------------------------------------------
 188 // LIRItem
 189 
 190 void LIRItem::set_result(LIR_Opr opr) {
 191   assert(value()->operand()->is_illegal() || value()->operand()->is_constant(), "operand should never change");
 192   value()->set_operand(opr);
 193 
 194   if (opr->is_virtual()) {
 195     _gen->_instruction_for_operand.at_put_grow(opr->vreg_number(), value(), NULL);
 196   }
 197 
 198   _result = opr;
 199 }
 200 
 201 void LIRItem::load_item() {
 202   if (result()->is_illegal()) {
 203     // update the items result
 204     _result = value()->operand();
 205   }
 206   if (!result()->is_register()) {
 207     LIR_Opr reg = _gen->new_register(value()->type());
 208     __ move(result(), reg);
 209     if (result()->is_constant()) {
 210       _result = reg;
 211     } else {
 212       set_result(reg);
 213     }
 214   }
 215 }
 216 
 217 
 218 void LIRItem::load_for_store(BasicType type) {
 219   if (_gen->can_store_as_constant(value(), type)) {
 220     _result = value()->operand();
 221     if (!_result->is_constant()) {
 222       _result = LIR_OprFact::value_type(value()->type());
 223     }
 224   } else if (type == T_BYTE || type == T_BOOLEAN) {
 225     load_byte_item();
 226   } else {
 227     load_item();
 228   }
 229 }
 230 
 231 void LIRItem::load_item_force(LIR_Opr reg) {
 232   LIR_Opr r = result();
 233   if (r != reg) {
 234 #if !defined(ARM) && !defined(E500V2)
 235     if (r->type() != reg->type()) {
 236       // moves between different types need an intervening spill slot
 237       r = _gen->force_to_spill(r, reg->type());
 238     }
 239 #endif
 240     __ move(r, reg);
 241     _result = reg;
 242   }
 243 }
 244 
 245 ciObject* LIRItem::get_jobject_constant() const {
 246   ObjectType* oc = type()->as_ObjectType();
 247   if (oc) {
 248     return oc->constant_value();
 249   }
 250   return NULL;
 251 }
 252 
 253 
 254 jint LIRItem::get_jint_constant() const {
 255   assert(is_constant() && value() != NULL, "");
 256   assert(type()->as_IntConstant() != NULL, "type check");
 257   return type()->as_IntConstant()->value();
 258 }
 259 
 260 
 261 jint LIRItem::get_address_constant() const {
 262   assert(is_constant() && value() != NULL, "");
 263   assert(type()->as_AddressConstant() != NULL, "type check");
 264   return type()->as_AddressConstant()->value();
 265 }
 266 
 267 
 268 jfloat LIRItem::get_jfloat_constant() const {
 269   assert(is_constant() && value() != NULL, "");
 270   assert(type()->as_FloatConstant() != NULL, "type check");
 271   return type()->as_FloatConstant()->value();
 272 }
 273 
 274 
 275 jdouble LIRItem::get_jdouble_constant() const {
 276   assert(is_constant() && value() != NULL, "");
 277   assert(type()->as_DoubleConstant() != NULL, "type check");
 278   return type()->as_DoubleConstant()->value();
 279 }
 280 
 281 
 282 jlong LIRItem::get_jlong_constant() const {
 283   assert(is_constant() && value() != NULL, "");
 284   assert(type()->as_LongConstant() != NULL, "type check");
 285   return type()->as_LongConstant()->value();
 286 }
 287 
 288 
 289 
 290 //--------------------------------------------------------------
 291 
 292 
 293 void LIRGenerator::init() {
 294   _bs = Universe::heap()->barrier_set();
 295 }
 296 
 297 
 298 void LIRGenerator::block_do_prolog(BlockBegin* block) {
 299 #ifndef PRODUCT
 300   if (PrintIRWithLIR) {
 301     block->print();
 302   }
 303 #endif
 304 
 305   // set up the list of LIR instructions
 306   assert(block->lir() == NULL, "LIR list already computed for this block");
 307   _lir = new LIR_List(compilation(), block);
 308   block->set_lir(_lir);
 309 
 310   __ branch_destination(block->label());
 311 
 312   if (LIRTraceExecution &&
 313       Compilation::current()->hir()->start()->block_id() != block->block_id() &&
 314       !block->is_set(BlockBegin::exception_entry_flag)) {
 315     assert(block->lir()->instructions_list()->length() == 1, "should come right after br_dst");
 316     trace_block_entry(block);
 317   }
 318 }
 319 
 320 
 321 void LIRGenerator::block_do_epilog(BlockBegin* block) {
 322 #ifndef PRODUCT
 323   if (PrintIRWithLIR) {
 324     tty->cr();
 325   }
 326 #endif
 327 
 328   // LIR_Opr for unpinned constants shouldn't be referenced by other
 329   // blocks so clear them out after processing the block.
 330   for (int i = 0; i < _unpinned_constants.length(); i++) {
 331     _unpinned_constants.at(i)->clear_operand();
 332   }
 333   _unpinned_constants.trunc_to(0);
 334 
 335   // clear our any registers for other local constants
 336   _constants.trunc_to(0);
 337   _reg_for_constants.trunc_to(0);
 338 }
 339 
 340 
 341 void LIRGenerator::block_do(BlockBegin* block) {
 342   CHECK_BAILOUT();
 343 
 344   block_do_prolog(block);
 345   set_block(block);
 346 
 347   for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
 348     if (instr->is_pinned()) do_root(instr);
 349   }
 350 
 351   set_block(NULL);
 352   block_do_epilog(block);
 353 }
 354 
 355 
 356 //-------------------------LIRGenerator-----------------------------
 357 
 358 // This is where the tree-walk starts; instr must be root;
 359 void LIRGenerator::do_root(Value instr) {
 360   CHECK_BAILOUT();
 361 
 362   InstructionMark im(compilation(), instr);
 363 
 364   assert(instr->is_pinned(), "use only with roots");
 365   assert(instr->subst() == instr, "shouldn't have missed substitution");
 366 
 367   instr->visit(this);
 368 
 369   assert(!instr->has_uses() || instr->operand()->is_valid() ||
 370          instr->as_Constant() != NULL || bailed_out(), "invalid item set");
 371 }
 372 
 373 
 374 // This is called for each node in tree; the walk stops if a root is reached
 375 void LIRGenerator::walk(Value instr) {
 376   InstructionMark im(compilation(), instr);
 377   //stop walk when encounter a root
 378   if (instr->is_pinned() && instr->as_Phi() == NULL || instr->operand()->is_valid()) {
 379     assert(instr->operand() != LIR_OprFact::illegalOpr || instr->as_Constant() != NULL, "this root has not yet been visited");
 380   } else {
 381     assert(instr->subst() == instr, "shouldn't have missed substitution");
 382     instr->visit(this);
 383     // assert(instr->use_count() > 0 || instr->as_Phi() != NULL, "leaf instruction must have a use");
 384   }
 385 }
 386 
 387 
 388 CodeEmitInfo* LIRGenerator::state_for(Instruction* x, ValueStack* state, bool ignore_xhandler) {
 389   assert(state != NULL, "state must be defined");
 390 
 391   ValueStack* s = state;
 392   for_each_state(s) {
 393     if (s->kind() == ValueStack::EmptyExceptionState) {
 394       assert(s->stack_size() == 0 && s->locals_size() == 0 && (s->locks_size() == 0 || s->locks_size() == 1), "state must be empty");
 395       continue;
 396     }
 397 
 398     int index;
 399     Value value;
 400     for_each_stack_value(s, index, value) {
 401       assert(value->subst() == value, "missed substitution");
 402       if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
 403         walk(value);
 404         assert(value->operand()->is_valid(), "must be evaluated now");
 405       }
 406     }
 407 
 408     int bci = s->bci();
 409     IRScope* scope = s->scope();
 410     ciMethod* method = scope->method();
 411 
 412     MethodLivenessResult liveness = method->liveness_at_bci(bci);
 413     if (bci == SynchronizationEntryBCI) {
 414       if (x->as_ExceptionObject() || x->as_Throw()) {
 415         // all locals are dead on exit from the synthetic unlocker
 416         liveness.clear();
 417       } else {
 418         assert(x->as_MonitorEnter(), "only other case is MonitorEnter");
 419       }
 420     }
 421     if (!liveness.is_valid()) {
 422       // Degenerate or breakpointed method.
 423       bailout("Degenerate or breakpointed method");
 424     } else {
 425       assert((int)liveness.size() == s->locals_size(), "error in use of liveness");
 426       for_each_local_value(s, index, value) {
 427         assert(value->subst() == value, "missed substition");
 428         if (liveness.at(index) && !value->type()->is_illegal()) {
 429           if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
 430             walk(value);
 431             assert(value->operand()->is_valid(), "must be evaluated now");
 432           }
 433         } else {
 434           // NULL out this local so that linear scan can assume that all non-NULL values are live.
 435           s->invalidate_local(index);
 436         }
 437       }
 438     }
 439   }
 440 
 441   return new CodeEmitInfo(state, ignore_xhandler ? NULL : x->exception_handlers());
 442 }
 443 
 444 
 445 CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {
 446   return state_for(x, x->exception_state());
 447 }
 448 
 449 
 450 void LIRGenerator::jobject2reg_with_patching(LIR_Opr r, ciObject* obj, CodeEmitInfo* info) {
 451   if (!obj->is_loaded() || PatchALot) {
 452     assert(info != NULL, "info must be set if class is not loaded");
 453     __ oop2reg_patch(NULL, r, info);
 454   } else {
 455     // no patching needed
 456     __ oop2reg(obj->constant_encoding(), r);
 457   }
 458 }
 459 
 460 
 461 void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index,
 462                                     CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) {
 463   CodeStub* stub = new RangeCheckStub(range_check_info, index);
 464   if (index->is_constant()) {
 465     cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(),
 466                 index->as_jint(), null_check_info);
 467     __ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
 468   } else {
 469     cmp_reg_mem(lir_cond_aboveEqual, index, array,
 470                 arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info);
 471     __ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
 472   }
 473 }
 474 
 475 
 476 void LIRGenerator::nio_range_check(LIR_Opr buffer, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
 477   CodeStub* stub = new RangeCheckStub(info, index, true);
 478   if (index->is_constant()) {
 479     cmp_mem_int(lir_cond_belowEqual, buffer, java_nio_Buffer::limit_offset(), index->as_jint(), info);
 480     __ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
 481   } else {
 482     cmp_reg_mem(lir_cond_aboveEqual, index, buffer,
 483                 java_nio_Buffer::limit_offset(), T_INT, info);
 484     __ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
 485   }
 486   __ move(index, result);
 487 }
 488 
 489 
 490 
 491 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) {
 492   LIR_Opr result_op = result;
 493   LIR_Opr left_op   = left;
 494   LIR_Opr right_op  = right;
 495 
 496   if (TwoOperandLIRForm && left_op != result_op) {
 497     assert(right_op != result_op, "malformed");
 498     __ move(left_op, result_op);
 499     left_op = result_op;
 500   }
 501 
 502   switch(code) {
 503     case Bytecodes::_dadd:
 504     case Bytecodes::_fadd:
 505     case Bytecodes::_ladd:
 506     case Bytecodes::_iadd:  __ add(left_op, right_op, result_op); break;
 507     case Bytecodes::_fmul:
 508     case Bytecodes::_lmul:  __ mul(left_op, right_op, result_op); break;
 509 
 510     case Bytecodes::_dmul:
 511       {
 512         if (is_strictfp) {
 513           __ mul_strictfp(left_op, right_op, result_op, tmp_op); break;
 514         } else {
 515           __ mul(left_op, right_op, result_op); break;
 516         }
 517       }
 518       break;
 519 
 520     case Bytecodes::_imul:
 521       {
 522         bool    did_strength_reduce = false;
 523 
 524         if (right->is_constant()) {
 525           int c = right->as_jint();
 526           if (is_power_of_2(c)) {
 527             // do not need tmp here
 528             __ shift_left(left_op, exact_log2(c), result_op);
 529             did_strength_reduce = true;
 530           } else {
 531             did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op);
 532           }
 533         }
 534         // we couldn't strength reduce so just emit the multiply
 535         if (!did_strength_reduce) {
 536           __ mul(left_op, right_op, result_op);
 537         }
 538       }
 539       break;
 540 
 541     case Bytecodes::_dsub:
 542     case Bytecodes::_fsub:
 543     case Bytecodes::_lsub:
 544     case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break;
 545 
 546     case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break;
 547     // ldiv and lrem are implemented with a direct runtime call
 548 
 549     case Bytecodes::_ddiv:
 550       {
 551         if (is_strictfp) {
 552           __ div_strictfp (left_op, right_op, result_op, tmp_op); break;
 553         } else {
 554           __ div (left_op, right_op, result_op); break;
 555         }
 556       }
 557       break;
 558 
 559     case Bytecodes::_drem:
 560     case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break;
 561 
 562     default: ShouldNotReachHere();
 563   }
 564 }
 565 
 566 
 567 void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {
 568   arithmetic_op(code, result, left, right, false, tmp);
 569 }
 570 
 571 
 572 void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
 573   arithmetic_op(code, result, left, right, false, LIR_OprFact::illegalOpr, info);
 574 }
 575 
 576 
 577 void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp) {
 578   arithmetic_op(code, result, left, right, is_strictfp, tmp);
 579 }
 580 
 581 
 582 void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) {
 583   if (TwoOperandLIRForm && value != result_op) {
 584     assert(count != result_op, "malformed");
 585     __ move(value, result_op);
 586     value = result_op;
 587   }
 588 
 589   assert(count->is_constant() || count->is_register(), "must be");
 590   switch(code) {
 591   case Bytecodes::_ishl:
 592   case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break;
 593   case Bytecodes::_ishr:
 594   case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break;
 595   case Bytecodes::_iushr:
 596   case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break;
 597   default: ShouldNotReachHere();
 598   }
 599 }
 600 
 601 
 602 void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) {
 603   if (TwoOperandLIRForm && left_op != result_op) {
 604     assert(right_op != result_op, "malformed");
 605     __ move(left_op, result_op);
 606     left_op = result_op;
 607   }
 608 
 609   switch(code) {
 610     case Bytecodes::_iand:
 611     case Bytecodes::_land:  __ logical_and(left_op, right_op, result_op); break;
 612 
 613     case Bytecodes::_ior:
 614     case Bytecodes::_lor:   __ logical_or(left_op, right_op, result_op);  break;
 615 
 616     case Bytecodes::_ixor:
 617     case Bytecodes::_lxor:  __ logical_xor(left_op, right_op, result_op); break;
 618 
 619     default: ShouldNotReachHere();
 620   }
 621 }
 622 
 623 
 624 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) {
 625   if (!GenerateSynchronizationCode) return;
 626   // for slow path, use debug info for state after successful locking
 627   CodeStub* slow_path = new MonitorEnterStub(object, lock, info);
 628   __ load_stack_address_monitor(monitor_no, lock);
 629   // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
 630   __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);
 631 }
 632 
 633 
 634 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
 635   if (!GenerateSynchronizationCode) return;
 636   // setup registers
 637   LIR_Opr hdr = lock;
 638   lock = new_hdr;
 639   CodeStub* slow_path = new MonitorExitStub(lock, UseFastLocking, monitor_no);
 640   __ load_stack_address_monitor(monitor_no, lock);
 641   __ unlock_object(hdr, object, lock, scratch, slow_path);
 642 }
 643 
 644 
 645 void LIRGenerator::new_instance(LIR_Opr dst, ciInstanceKlass* klass, LIR_Opr scratch1, LIR_Opr scratch2, LIR_Opr scratch3, LIR_Opr scratch4, LIR_Opr klass_reg, CodeEmitInfo* info) {
 646   jobject2reg_with_patching(klass_reg, klass, info);
 647   // If klass is not loaded we do not know if the klass has finalizers:
 648   if (UseFastNewInstance && klass->is_loaded()
 649       && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
 650 
 651     Runtime1::StubID stub_id = klass->is_initialized() ? Runtime1::fast_new_instance_id : Runtime1::fast_new_instance_init_check_id;
 652 
 653     CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
 654 
 655     assert(klass->is_loaded(), "must be loaded");
 656     // allocate space for instance
 657     assert(klass->size_helper() >= 0, "illegal instance size");
 658     const int instance_size = align_object_size(klass->size_helper());
 659     __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
 660                        oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
 661   } else {
 662     CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, Runtime1::new_instance_id);
 663     __ branch(lir_cond_always, T_ILLEGAL, slow_path);
 664     __ branch_destination(slow_path->continuation());
 665   }
 666 }
 667 
 668 
 669 static bool is_constant_zero(Instruction* inst) {
 670   IntConstant* c = inst->type()->as_IntConstant();
 671   if (c) {
 672     return (c->value() == 0);
 673   }
 674   return false;
 675 }
 676 
 677 
 678 static bool positive_constant(Instruction* inst) {
 679   IntConstant* c = inst->type()->as_IntConstant();
 680   if (c) {
 681     return (c->value() >= 0);
 682   }
 683   return false;
 684 }
 685 
 686 
 687 static ciArrayKlass* as_array_klass(ciType* type) {
 688   if (type != NULL && type->is_array_klass() && type->is_loaded()) {
 689     return (ciArrayKlass*)type;
 690   } else {
 691     return NULL;
 692   }
 693 }
 694 
 695 void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) {
 696   Instruction* src     = x->argument_at(0);
 697   Instruction* src_pos = x->argument_at(1);
 698   Instruction* dst     = x->argument_at(2);
 699   Instruction* dst_pos = x->argument_at(3);
 700   Instruction* length  = x->argument_at(4);
 701 
 702   // first try to identify the likely type of the arrays involved
 703   ciArrayKlass* expected_type = NULL;
 704   bool is_exact = false;
 705   {
 706     ciArrayKlass* src_exact_type    = as_array_klass(src->exact_type());
 707     ciArrayKlass* src_declared_type = as_array_klass(src->declared_type());
 708     ciArrayKlass* dst_exact_type    = as_array_klass(dst->exact_type());
 709     ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type());
 710     if (src_exact_type != NULL && src_exact_type == dst_exact_type) {
 711       // the types exactly match so the type is fully known
 712       is_exact = true;
 713       expected_type = src_exact_type;
 714     } else if (dst_exact_type != NULL && dst_exact_type->is_obj_array_klass()) {
 715       ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type;
 716       ciArrayKlass* src_type = NULL;
 717       if (src_exact_type != NULL && src_exact_type->is_obj_array_klass()) {
 718         src_type = (ciArrayKlass*) src_exact_type;
 719       } else if (src_declared_type != NULL && src_declared_type->is_obj_array_klass()) {
 720         src_type = (ciArrayKlass*) src_declared_type;
 721       }
 722       if (src_type != NULL) {
 723         if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
 724           is_exact = true;
 725           expected_type = dst_type;
 726         }
 727       }
 728     }
 729     // at least pass along a good guess
 730     if (expected_type == NULL) expected_type = dst_exact_type;
 731     if (expected_type == NULL) expected_type = src_declared_type;
 732     if (expected_type == NULL) expected_type = dst_declared_type;
 733   }
 734 
 735   // if a probable array type has been identified, figure out if any
 736   // of the required checks for a fast case can be elided.
 737   int flags = LIR_OpArrayCopy::all_flags;
 738   if (expected_type != NULL) {
 739     // try to skip null checks
 740     if (src->as_NewArray() != NULL)
 741       flags &= ~LIR_OpArrayCopy::src_null_check;
 742     if (dst->as_NewArray() != NULL)
 743       flags &= ~LIR_OpArrayCopy::dst_null_check;
 744 
 745     // check from incoming constant values
 746     if (positive_constant(src_pos))
 747       flags &= ~LIR_OpArrayCopy::src_pos_positive_check;
 748     if (positive_constant(dst_pos))
 749       flags &= ~LIR_OpArrayCopy::dst_pos_positive_check;
 750     if (positive_constant(length))
 751       flags &= ~LIR_OpArrayCopy::length_positive_check;
 752 
 753     // see if the range check can be elided, which might also imply
 754     // that src or dst is non-null.
 755     ArrayLength* al = length->as_ArrayLength();
 756     if (al != NULL) {
 757       if (al->array() == src) {
 758         // it's the length of the source array
 759         flags &= ~LIR_OpArrayCopy::length_positive_check;
 760         flags &= ~LIR_OpArrayCopy::src_null_check;
 761         if (is_constant_zero(src_pos))
 762           flags &= ~LIR_OpArrayCopy::src_range_check;
 763       }
 764       if (al->array() == dst) {
 765         // it's the length of the destination array
 766         flags &= ~LIR_OpArrayCopy::length_positive_check;
 767         flags &= ~LIR_OpArrayCopy::dst_null_check;
 768         if (is_constant_zero(dst_pos))
 769           flags &= ~LIR_OpArrayCopy::dst_range_check;
 770       }
 771     }
 772     if (is_exact) {
 773       flags &= ~LIR_OpArrayCopy::type_check;
 774     }
 775   }
 776 
 777   if (src == dst) {
 778     // moving within a single array so no type checks are needed
 779     if (flags & LIR_OpArrayCopy::type_check) {
 780       flags &= ~LIR_OpArrayCopy::type_check;
 781     }
 782   }
 783   *flagsp = flags;
 784   *expected_typep = (ciArrayKlass*)expected_type;
 785 }
 786 
 787 
 788 LIR_Opr LIRGenerator::round_item(LIR_Opr opr) {
 789   assert(opr->is_register(), "why spill if item is not register?");
 790 
 791   if (RoundFPResults && UseSSE < 1 && opr->is_single_fpu()) {
 792     LIR_Opr result = new_register(T_FLOAT);
 793     set_vreg_flag(result, must_start_in_memory);
 794     assert(opr->is_register(), "only a register can be spilled");
 795     assert(opr->value_type()->is_float(), "rounding only for floats available");
 796     __ roundfp(opr, LIR_OprFact::illegalOpr, result);
 797     return result;
 798   }
 799   return opr;
 800 }
 801 
 802 
 803 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
 804   assert(type2size[t] == type2size[value->type()], "size mismatch");
 805   if (!value->is_register()) {
 806     // force into a register
 807     LIR_Opr r = new_register(value->type());
 808     __ move(value, r);
 809     value = r;
 810   }
 811 
 812   // create a spill location
 813   LIR_Opr tmp = new_register(t);
 814   set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
 815 
 816   // move from register to spill
 817   __ move(value, tmp);
 818   return tmp;
 819 }
 820 
 821 void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) {
 822   if (if_instr->should_profile()) {
 823     ciMethod* method = if_instr->profiled_method();
 824     assert(method != NULL, "method should be set if branch is profiled");
 825     ciMethodData* md = method->method_data();
 826     if (md == NULL) {
 827       bailout("out of memory building methodDataOop");
 828       return;
 829     }
 830     ciProfileData* data = md->bci_to_data(if_instr->profiled_bci());
 831     assert(data != NULL, "must have profiling data");
 832     assert(data->is_BranchData(), "need BranchData for two-way branches");
 833     int taken_count_offset     = md->byte_offset_of_slot(data, BranchData::taken_offset());
 834     int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
 835     if (if_instr->is_swapped()) {
 836       int t = taken_count_offset;
 837       taken_count_offset = not_taken_count_offset;
 838       not_taken_count_offset = t;
 839     }
 840 
 841     LIR_Opr md_reg = new_register(T_OBJECT);
 842     __ oop2reg(md->constant_encoding(), md_reg);
 843 
 844     LIR_Opr data_offset_reg = new_pointer_register();
 845     __ cmove(lir_cond(cond),
 846              LIR_OprFact::intptrConst(taken_count_offset),
 847              LIR_OprFact::intptrConst(not_taken_count_offset),
 848              data_offset_reg);
 849 
 850     // MDO cells are intptr_t, so the data_reg width is arch-dependent.
 851     LIR_Opr data_reg = new_pointer_register();
 852     LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, data_reg->type());
 853     __ move(LIR_OprFact::address(data_addr), data_reg);
 854     // Use leal instead of add to avoid destroying condition codes on x86
 855     LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT);
 856     __ leal(LIR_OprFact::address(fake_incr_value), data_reg);
 857     __ move(data_reg, LIR_OprFact::address(data_addr));
 858   }
 859 }
 860 
 861 // Phi technique:
 862 // This is about passing live values from one basic block to the other.
 863 // In code generated with Java it is rather rare that more than one
 864 // value is on the stack from one basic block to the other.
 865 // We optimize our technique for efficient passing of one value
 866 // (of type long, int, double..) but it can be extended.
 867 // When entering or leaving a basic block, all registers and all spill
 868 // slots are release and empty. We use the released registers
 869 // and spill slots to pass the live values from one block
 870 // to the other. The topmost value, i.e., the value on TOS of expression
 871 // stack is passed in registers. All other values are stored in spilling
 872 // area. Every Phi has an index which designates its spill slot
 873 // At exit of a basic block, we fill the register(s) and spill slots.
 874 // At entry of a basic block, the block_prolog sets up the content of phi nodes
 875 // and locks necessary registers and spilling slots.
 876 
 877 
 878 // move current value to referenced phi function
 879 void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) {
 880   Phi* phi = sux_val->as_Phi();
 881   // cur_val can be null without phi being null in conjunction with inlining
 882   if (phi != NULL && cur_val != NULL && cur_val != phi && !phi->is_illegal()) {
 883     LIR_Opr operand = cur_val->operand();
 884     if (cur_val->operand()->is_illegal()) {
 885       assert(cur_val->as_Constant() != NULL || cur_val->as_Local() != NULL,
 886              "these can be produced lazily");
 887       operand = operand_for_instruction(cur_val);
 888     }
 889     resolver->move(operand, operand_for_instruction(phi));
 890   }
 891 }
 892 
 893 
 894 // Moves all stack values into their PHI position
 895 void LIRGenerator::move_to_phi(ValueStack* cur_state) {
 896   BlockBegin* bb = block();
 897   if (bb->number_of_sux() == 1) {
 898     BlockBegin* sux = bb->sux_at(0);
 899     assert(sux->number_of_preds() > 0, "invalid CFG");
 900 
 901     // a block with only one predecessor never has phi functions
 902     if (sux->number_of_preds() > 1) {
 903       int max_phis = cur_state->stack_size() + cur_state->locals_size();
 904       PhiResolver resolver(this, _virtual_register_number + max_phis * 2);
 905 
 906       ValueStack* sux_state = sux->state();
 907       Value sux_value;
 908       int index;
 909 
 910       assert(cur_state->scope() == sux_state->scope(), "not matching");
 911       assert(cur_state->locals_size() == sux_state->locals_size(), "not matching");
 912       assert(cur_state->stack_size() == sux_state->stack_size(), "not matching");
 913 
 914       for_each_stack_value(sux_state, index, sux_value) {
 915         move_to_phi(&resolver, cur_state->stack_at(index), sux_value);
 916       }
 917 
 918       for_each_local_value(sux_state, index, sux_value) {
 919         move_to_phi(&resolver, cur_state->local_at(index), sux_value);
 920       }
 921 
 922       assert(cur_state->caller_state() == sux_state->caller_state(), "caller states must be equal");
 923     }
 924   }
 925 }
 926 
 927 
 928 LIR_Opr LIRGenerator::new_register(BasicType type) {
 929   int vreg = _virtual_register_number;
 930   // add a little fudge factor for the bailout, since the bailout is
 931   // only checked periodically.  This gives a few extra registers to
 932   // hand out before we really run out, which helps us keep from
 933   // tripping over assertions.
 934   if (vreg + 20 >= LIR_OprDesc::vreg_max) {
 935     bailout("out of virtual registers");
 936     if (vreg + 2 >= LIR_OprDesc::vreg_max) {
 937       // wrap it around
 938       _virtual_register_number = LIR_OprDesc::vreg_base;
 939     }
 940   }
 941   _virtual_register_number += 1;
 942   return LIR_OprFact::virtual_register(vreg, type);
 943 }
 944 
 945 
 946 // Try to lock using register in hint
 947 LIR_Opr LIRGenerator::rlock(Value instr) {
 948   return new_register(instr->type());
 949 }
 950 
 951 
 952 // does an rlock and sets result
 953 LIR_Opr LIRGenerator::rlock_result(Value x) {
 954   LIR_Opr reg = rlock(x);
 955   set_result(x, reg);
 956   return reg;
 957 }
 958 
 959 
 960 // does an rlock and sets result
 961 LIR_Opr LIRGenerator::rlock_result(Value x, BasicType type) {
 962   LIR_Opr reg;
 963   switch (type) {
 964   case T_BYTE:
 965   case T_BOOLEAN:
 966     reg = rlock_byte(type);
 967     break;
 968   default:
 969     reg = rlock(x);
 970     break;
 971   }
 972 
 973   set_result(x, reg);
 974   return reg;
 975 }
 976 
 977 
 978 //---------------------------------------------------------------------
 979 ciObject* LIRGenerator::get_jobject_constant(Value value) {
 980   ObjectType* oc = value->type()->as_ObjectType();
 981   if (oc) {
 982     return oc->constant_value();
 983   }
 984   return NULL;
 985 }
 986 
 987 
 988 void LIRGenerator::do_ExceptionObject(ExceptionObject* x) {
 989   assert(block()->is_set(BlockBegin::exception_entry_flag), "ExceptionObject only allowed in exception handler block");
 990   assert(block()->next() == x, "ExceptionObject must be first instruction of block");
 991 
 992   // no moves are created for phi functions at the begin of exception
 993   // handlers, so assign operands manually here
 994   for_each_phi_fun(block(), phi,
 995                    operand_for_instruction(phi));
 996 
 997   LIR_Opr thread_reg = getThreadPointer();
 998   __ move(new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT),
 999           exceptionOopOpr());
1000   __ move(LIR_OprFact::oopConst(NULL),
1001           new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT));
1002   __ move(LIR_OprFact::oopConst(NULL),
1003           new LIR_Address(thread_reg, in_bytes(JavaThread::exception_pc_offset()), T_OBJECT));
1004 
1005   LIR_Opr result = new_register(T_OBJECT);
1006   __ move(exceptionOopOpr(), result);
1007   set_result(x, result);
1008 }
1009 
1010 
1011 //----------------------------------------------------------------------
1012 //----------------------------------------------------------------------
1013 //----------------------------------------------------------------------
1014 //----------------------------------------------------------------------
1015 //                        visitor functions
1016 //----------------------------------------------------------------------
1017 //----------------------------------------------------------------------
1018 //----------------------------------------------------------------------
1019 //----------------------------------------------------------------------
1020 
1021 void LIRGenerator::do_Phi(Phi* x) {
1022   // phi functions are never visited directly
1023   ShouldNotReachHere();
1024 }
1025 
1026 
1027 // Code for a constant is generated lazily unless the constant is frequently used and can't be inlined.
1028 void LIRGenerator::do_Constant(Constant* x) {
1029   if (x->state_before() != NULL) {
1030     // Any constant with a ValueStack requires patching so emit the patch here
1031     LIR_Opr reg = rlock_result(x);
1032     CodeEmitInfo* info = state_for(x, x->state_before());
1033     __ oop2reg_patch(NULL, reg, info);
1034   } else if (x->use_count() > 1 && !can_inline_as_constant(x)) {
1035     if (!x->is_pinned()) {
1036       // unpinned constants are handled specially so that they can be
1037       // put into registers when they are used multiple times within a
1038       // block.  After the block completes their operand will be
1039       // cleared so that other blocks can't refer to that register.
1040       set_result(x, load_constant(x));
1041     } else {
1042       LIR_Opr res = x->operand();
1043       if (!res->is_valid()) {
1044         res = LIR_OprFact::value_type(x->type());
1045       }
1046       if (res->is_constant()) {
1047         LIR_Opr reg = rlock_result(x);
1048         __ move(res, reg);
1049       } else {
1050         set_result(x, res);
1051       }
1052     }
1053   } else {
1054     set_result(x, LIR_OprFact::value_type(x->type()));
1055   }
1056 }
1057 
1058 
1059 void LIRGenerator::do_Local(Local* x) {
1060   // operand_for_instruction has the side effect of setting the result
1061   // so there's no need to do it here.
1062   operand_for_instruction(x);
1063 }
1064 
1065 
1066 void LIRGenerator::do_IfInstanceOf(IfInstanceOf* x) {
1067   Unimplemented();
1068 }
1069 
1070 
1071 void LIRGenerator::do_Return(Return* x) {
1072   if (compilation()->env()->dtrace_method_probes()) {
1073     BasicTypeList signature;
1074     signature.append(T_INT);    // thread
1075     signature.append(T_OBJECT); // methodOop
1076     LIR_OprList* args = new LIR_OprList();
1077     args->append(getThreadPointer());
1078     LIR_Opr meth = new_register(T_OBJECT);
1079     __ oop2reg(method()->constant_encoding(), meth);
1080     args->append(meth);
1081     call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), voidType, NULL);
1082   }
1083 
1084   if (x->type()->is_void()) {
1085     __ return_op(LIR_OprFact::illegalOpr);
1086   } else {
1087     LIR_Opr reg = result_register_for(x->type(), /*callee=*/true);
1088     LIRItem result(x->result(), this);
1089 
1090     result.load_item_force(reg);
1091     __ return_op(result.result());
1092   }
1093   set_no_result(x);
1094 }
1095 
1096 
1097 // Example: object.getClass ()
1098 void LIRGenerator::do_getClass(Intrinsic* x) {
1099   assert(x->number_of_arguments() == 1, "wrong type");
1100 
1101   LIRItem rcvr(x->argument_at(0), this);
1102   rcvr.load_item();
1103   LIR_Opr result = rlock_result(x);
1104 
1105   // need to perform the null check on the rcvr
1106   CodeEmitInfo* info = NULL;
1107   if (x->needs_null_check()) {
1108     info = state_for(x);
1109   }
1110   __ move(new LIR_Address(rcvr.result(), oopDesc::klass_offset_in_bytes(), T_OBJECT), result, info);
1111   __ move(new LIR_Address(result, Klass::java_mirror_offset_in_bytes() +
1112                           klassOopDesc::klass_part_offset_in_bytes(), T_OBJECT), result);
1113 }
1114 
1115 
1116 // Example: Thread.currentThread()
1117 void LIRGenerator::do_currentThread(Intrinsic* x) {
1118   assert(x->number_of_arguments() == 0, "wrong type");
1119   LIR_Opr reg = rlock_result(x);
1120   __ load(new LIR_Address(getThreadPointer(), in_bytes(JavaThread::threadObj_offset()), T_OBJECT), reg);
1121 }
1122 
1123 
1124 void LIRGenerator::do_RegisterFinalizer(Intrinsic* x) {
1125   assert(x->number_of_arguments() == 1, "wrong type");
1126   LIRItem receiver(x->argument_at(0), this);
1127 
1128   receiver.load_item();
1129   BasicTypeList signature;
1130   signature.append(T_OBJECT); // receiver
1131   LIR_OprList* args = new LIR_OprList();
1132   args->append(receiver.result());
1133   CodeEmitInfo* info = state_for(x, x->state());
1134   call_runtime(&signature, args,
1135                CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::register_finalizer_id)),
1136                voidType, info);
1137 
1138   set_no_result(x);
1139 }
1140 
1141 
1142 //------------------------local access--------------------------------------
1143 
1144 LIR_Opr LIRGenerator::operand_for_instruction(Instruction* x) {
1145   if (x->operand()->is_illegal()) {
1146     Constant* c = x->as_Constant();
1147     if (c != NULL) {
1148       x->set_operand(LIR_OprFact::value_type(c->type()));
1149     } else {
1150       assert(x->as_Phi() || x->as_Local() != NULL, "only for Phi and Local");
1151       // allocate a virtual register for this local or phi
1152       x->set_operand(rlock(x));
1153       _instruction_for_operand.at_put_grow(x->operand()->vreg_number(), x, NULL);
1154     }
1155   }
1156   return x->operand();
1157 }
1158 
1159 
1160 Instruction* LIRGenerator::instruction_for_opr(LIR_Opr opr) {
1161   if (opr->is_virtual()) {
1162     return instruction_for_vreg(opr->vreg_number());
1163   }
1164   return NULL;
1165 }
1166 
1167 
1168 Instruction* LIRGenerator::instruction_for_vreg(int reg_num) {
1169   if (reg_num < _instruction_for_operand.length()) {
1170     return _instruction_for_operand.at(reg_num);
1171   }
1172   return NULL;
1173 }
1174 
1175 
1176 void LIRGenerator::set_vreg_flag(int vreg_num, VregFlag f) {
1177   if (_vreg_flags.size_in_bits() == 0) {
1178     BitMap2D temp(100, num_vreg_flags);
1179     temp.clear();
1180     _vreg_flags = temp;
1181   }
1182   _vreg_flags.at_put_grow(vreg_num, f, true);
1183 }
1184 
1185 bool LIRGenerator::is_vreg_flag_set(int vreg_num, VregFlag f) {
1186   if (!_vreg_flags.is_valid_index(vreg_num, f)) {
1187     return false;
1188   }
1189   return _vreg_flags.at(vreg_num, f);
1190 }
1191 
1192 
1193 // Block local constant handling.  This code is useful for keeping
1194 // unpinned constants and constants which aren't exposed in the IR in
1195 // registers.  Unpinned Constant instructions have their operands
1196 // cleared when the block is finished so that other blocks can't end
1197 // up referring to their registers.
1198 
1199 LIR_Opr LIRGenerator::load_constant(Constant* x) {
1200   assert(!x->is_pinned(), "only for unpinned constants");
1201   _unpinned_constants.append(x);
1202   return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());
1203 }
1204 
1205 
1206 LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {
1207   BasicType t = c->type();
1208   for (int i = 0; i < _constants.length(); i++) {
1209     LIR_Const* other = _constants.at(i);
1210     if (t == other->type()) {
1211       switch (t) {
1212       case T_INT:
1213       case T_FLOAT:
1214         if (c->as_jint_bits() != other->as_jint_bits()) continue;
1215         break;
1216       case T_LONG:
1217       case T_DOUBLE:
1218         if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;
1219         if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;
1220         break;
1221       case T_OBJECT:
1222         if (c->as_jobject() != other->as_jobject()) continue;
1223         break;
1224       }
1225       return _reg_for_constants.at(i);
1226     }
1227   }
1228 
1229   LIR_Opr result = new_register(t);
1230   __ move((LIR_Opr)c, result);
1231   _constants.append(c);
1232   _reg_for_constants.append(result);
1233   return result;
1234 }
1235 
1236 // Various barriers
1237 
1238 void LIRGenerator::pre_barrier(LIR_Opr addr_opr, bool patch,  CodeEmitInfo* info) {
1239   // Do the pre-write barrier, if any.
1240   switch (_bs->kind()) {
1241 #ifndef SERIALGC
1242     case BarrierSet::G1SATBCT:
1243     case BarrierSet::G1SATBCTLogging:
1244       G1SATBCardTableModRef_pre_barrier(addr_opr, patch, info);
1245       break;
1246 #endif // SERIALGC
1247     case BarrierSet::CardTableModRef:
1248     case BarrierSet::CardTableExtension:
1249       // No pre barriers
1250       break;
1251     case BarrierSet::ModRef:
1252     case BarrierSet::Other:
1253       // No pre barriers
1254       break;
1255     default      :
1256       ShouldNotReachHere();
1257 
1258   }
1259 }
1260 
1261 void LIRGenerator::post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1262   switch (_bs->kind()) {
1263 #ifndef SERIALGC
1264     case BarrierSet::G1SATBCT:
1265     case BarrierSet::G1SATBCTLogging:
1266       G1SATBCardTableModRef_post_barrier(addr,  new_val);
1267       break;
1268 #endif // SERIALGC
1269     case BarrierSet::CardTableModRef:
1270     case BarrierSet::CardTableExtension:
1271       CardTableModRef_post_barrier(addr,  new_val);
1272       break;
1273     case BarrierSet::ModRef:
1274     case BarrierSet::Other:
1275       // No post barriers
1276       break;
1277     default      :
1278       ShouldNotReachHere();
1279     }
1280 }
1281 
1282 ////////////////////////////////////////////////////////////////////////
1283 #ifndef SERIALGC
1284 
1285 void LIRGenerator::G1SATBCardTableModRef_pre_barrier(LIR_Opr addr_opr, bool patch,  CodeEmitInfo* info) {
1286   if (G1DisablePreBarrier) return;
1287 
1288   // First we test whether marking is in progress.
1289   BasicType flag_type;
1290   if (in_bytes(PtrQueue::byte_width_of_active()) == 4) {
1291     flag_type = T_INT;
1292   } else {
1293     guarantee(in_bytes(PtrQueue::byte_width_of_active()) == 1,
1294               "Assumption");
1295     flag_type = T_BYTE;
1296   }
1297   LIR_Opr thrd = getThreadPointer();
1298   LIR_Address* mark_active_flag_addr =
1299     new LIR_Address(thrd,
1300                     in_bytes(JavaThread::satb_mark_queue_offset() +
1301                              PtrQueue::byte_offset_of_active()),
1302                     flag_type);
1303   // Read the marking-in-progress flag.
1304   LIR_Opr flag_val = new_register(T_INT);
1305   __ load(mark_active_flag_addr, flag_val);
1306 
1307   LIR_PatchCode pre_val_patch_code =
1308     patch ? lir_patch_normal : lir_patch_none;
1309 
1310   LIR_Opr pre_val = new_register(T_OBJECT);
1311 
1312   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
1313   if (!addr_opr->is_address()) {
1314     assert(addr_opr->is_register(), "must be");
1315     addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
1316   }
1317   CodeStub* slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code,
1318                                         info);
1319   __ branch(lir_cond_notEqual, T_INT, slow);
1320   __ branch_destination(slow->continuation());
1321 }
1322 
1323 void LIRGenerator::G1SATBCardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1324   if (G1DisablePostBarrier) return;
1325 
1326   // If the "new_val" is a constant NULL, no barrier is necessary.
1327   if (new_val->is_constant() &&
1328       new_val->as_constant_ptr()->as_jobject() == NULL) return;
1329 
1330   if (!new_val->is_register()) {
1331     LIR_Opr new_val_reg = new_register(T_OBJECT);
1332     if (new_val->is_constant()) {
1333       __ move(new_val, new_val_reg);
1334     } else {
1335       __ leal(new_val, new_val_reg);
1336     }
1337     new_val = new_val_reg;
1338   }
1339   assert(new_val->is_register(), "must be a register at this point");
1340 
1341   if (addr->is_address()) {
1342     LIR_Address* address = addr->as_address_ptr();
1343     LIR_Opr ptr = new_register(T_OBJECT);
1344     if (!address->index()->is_valid() && address->disp() == 0) {
1345       __ move(address->base(), ptr);
1346     } else {
1347       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1348       __ leal(addr, ptr);
1349     }
1350     addr = ptr;
1351   }
1352   assert(addr->is_register(), "must be a register at this point");
1353 
1354   LIR_Opr xor_res = new_pointer_register();
1355   LIR_Opr xor_shift_res = new_pointer_register();
1356   if (TwoOperandLIRForm ) {
1357     __ move(addr, xor_res);
1358     __ logical_xor(xor_res, new_val, xor_res);
1359     __ move(xor_res, xor_shift_res);
1360     __ unsigned_shift_right(xor_shift_res,
1361                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1362                             xor_shift_res,
1363                             LIR_OprDesc::illegalOpr());
1364   } else {
1365     __ logical_xor(addr, new_val, xor_res);
1366     __ unsigned_shift_right(xor_res,
1367                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1368                             xor_shift_res,
1369                             LIR_OprDesc::illegalOpr());
1370   }
1371 
1372   if (!new_val->is_register()) {
1373     LIR_Opr new_val_reg = new_register(T_OBJECT);
1374     __ leal(new_val, new_val_reg);
1375     new_val = new_val_reg;
1376   }
1377   assert(new_val->is_register(), "must be a register at this point");
1378 
1379   __ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));
1380 
1381   CodeStub* slow = new G1PostBarrierStub(addr, new_val);
1382   __ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);
1383   __ branch_destination(slow->continuation());
1384 }
1385 
1386 #endif // SERIALGC
1387 ////////////////////////////////////////////////////////////////////////
1388 
1389 void LIRGenerator::CardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1390 
1391   assert(sizeof(*((CardTableModRefBS*)_bs)->byte_map_base) == sizeof(jbyte), "adjust this code");
1392   LIR_Const* card_table_base = new LIR_Const(((CardTableModRefBS*)_bs)->byte_map_base);
1393   if (addr->is_address()) {
1394     LIR_Address* address = addr->as_address_ptr();
1395     LIR_Opr ptr = new_register(T_OBJECT);
1396     if (!address->index()->is_valid() && address->disp() == 0) {
1397       __ move(address->base(), ptr);
1398     } else {
1399       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1400       __ leal(addr, ptr);
1401     }
1402     addr = ptr;
1403   }
1404   assert(addr->is_register(), "must be a register at this point");
1405 
1406 #ifdef ARM
1407   // TODO: ARM - move to platform-dependent code
1408   LIR_Opr tmp = FrameMap::R14_opr;
1409   if (VM_Version::supports_movw()) {
1410     __ move((LIR_Opr)card_table_base, tmp);
1411   } else {
1412     __ move(new LIR_Address(FrameMap::Rthread_opr, in_bytes(JavaThread::card_table_base_offset()), T_ADDRESS), tmp);
1413   }
1414 
1415   CardTableModRefBS* ct = (CardTableModRefBS*)_bs;
1416   LIR_Address *card_addr = new LIR_Address(tmp, addr, (LIR_Address::Scale) -CardTableModRefBS::card_shift, 0, T_BYTE);
1417   if(((int)ct->byte_map_base & 0xff) == 0) {
1418     __ move(tmp, card_addr);
1419   } else {
1420     LIR_Opr tmp_zero = new_register(T_INT);
1421     __ move(LIR_OprFact::intConst(0), tmp_zero);
1422     __ move(tmp_zero, card_addr);
1423   }
1424 #else // ARM
1425   LIR_Opr tmp = new_pointer_register();
1426   if (TwoOperandLIRForm) {
1427     __ move(addr, tmp);
1428     __ unsigned_shift_right(tmp, CardTableModRefBS::card_shift, tmp);
1429   } else {
1430     __ unsigned_shift_right(addr, CardTableModRefBS::card_shift, tmp);
1431   }
1432   if (can_inline_as_constant(card_table_base)) {
1433     __ move(LIR_OprFact::intConst(0),
1434               new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE));
1435   } else {
1436     __ move(LIR_OprFact::intConst(0),
1437               new LIR_Address(tmp, load_constant(card_table_base),
1438                               T_BYTE));
1439   }
1440 #endif // ARM
1441 }
1442 
1443 
1444 //------------------------field access--------------------------------------
1445 
1446 // Comment copied form templateTable_i486.cpp
1447 // ----------------------------------------------------------------------------
1448 // Volatile variables demand their effects be made known to all CPU's in
1449 // order.  Store buffers on most chips allow reads & writes to reorder; the
1450 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1451 // memory barrier (i.e., it's not sufficient that the interpreter does not
1452 // reorder volatile references, the hardware also must not reorder them).
1453 //
1454 // According to the new Java Memory Model (JMM):
1455 // (1) All volatiles are serialized wrt to each other.
1456 // ALSO reads & writes act as aquire & release, so:
1457 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1458 // the read float up to before the read.  It's OK for non-volatile memory refs
1459 // that happen before the volatile read to float down below it.
1460 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1461 // that happen BEFORE the write float down to after the write.  It's OK for
1462 // non-volatile memory refs that happen after the volatile write to float up
1463 // before it.
1464 //
1465 // We only put in barriers around volatile refs (they are expensive), not
1466 // _between_ memory refs (that would require us to track the flavor of the
1467 // previous memory refs).  Requirements (2) and (3) require some barriers
1468 // before volatile stores and after volatile loads.  These nearly cover
1469 // requirement (1) but miss the volatile-store-volatile-load case.  This final
1470 // case is placed after volatile-stores although it could just as well go
1471 // before volatile-loads.
1472 
1473 
1474 void LIRGenerator::do_StoreField(StoreField* x) {
1475   bool needs_patching = x->needs_patching();
1476   bool is_volatile = x->field()->is_volatile();
1477   BasicType field_type = x->field_type();
1478   bool is_oop = (field_type == T_ARRAY || field_type == T_OBJECT);
1479 
1480   CodeEmitInfo* info = NULL;
1481   if (needs_patching) {
1482     assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1483     info = state_for(x, x->state_before());
1484   } else if (x->needs_null_check()) {
1485     NullCheck* nc = x->explicit_null_check();
1486     if (nc == NULL) {
1487       info = state_for(x);
1488     } else {
1489       info = state_for(nc);
1490     }
1491   }
1492 
1493 
1494   LIRItem object(x->obj(), this);
1495   LIRItem value(x->value(),  this);
1496 
1497   object.load_item();
1498 
1499   if (is_volatile || needs_patching) {
1500     // load item if field is volatile (fewer special cases for volatiles)
1501     // load item if field not initialized
1502     // load item if field not constant
1503     // because of code patching we cannot inline constants
1504     if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1505       value.load_byte_item();
1506     } else  {
1507       value.load_item();
1508     }
1509   } else {
1510     value.load_for_store(field_type);
1511   }
1512 
1513   set_no_result(x);
1514 
1515 #ifndef PRODUCT
1516   if (PrintNotLoaded && needs_patching) {
1517     tty->print_cr("   ###class not loaded at store_%s bci %d",
1518                   x->is_static() ?  "static" : "field", x->printable_bci());
1519   }
1520 #endif
1521 
1522   if (x->needs_null_check() &&
1523       (needs_patching ||
1524        MacroAssembler::needs_explicit_null_check(x->offset()))) {
1525     // emit an explicit null check because the offset is too large
1526     __ null_check(object.result(), new CodeEmitInfo(info));
1527   }
1528 
1529   LIR_Address* address;
1530   if (needs_patching) {
1531     // we need to patch the offset in the instruction so don't allow
1532     // generate_address to try to be smart about emitting the -1.
1533     // Otherwise the patching code won't know how to find the
1534     // instruction to patch.
1535     address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);
1536   } else {
1537     address = generate_address(object.result(), x->offset(), field_type);
1538   }
1539 
1540   if (is_volatile && os::is_MP()) {
1541     __ membar_release();
1542   }
1543 
1544   if (is_oop) {
1545     // Do the pre-write barrier, if any.
1546     pre_barrier(LIR_OprFact::address(address),
1547                 needs_patching,
1548                 (info ? new CodeEmitInfo(info) : NULL));
1549   }
1550 
1551   if (is_volatile) {
1552     assert(!needs_patching && x->is_loaded(),
1553            "how do we know it's volatile if it's not loaded");
1554     volatile_field_store(value.result(), address, info);
1555   } else {
1556     LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1557     __ store(value.result(), address, info, patch_code);
1558   }
1559 
1560   if (is_oop) {
1561     // Store to object so mark the card of the header
1562     post_barrier(object.result(), value.result());
1563   }
1564 
1565   if (is_volatile && os::is_MP()) {
1566     __ membar();
1567   }
1568 }
1569 
1570 
1571 void LIRGenerator::do_LoadField(LoadField* x) {
1572   bool needs_patching = x->needs_patching();
1573   bool is_volatile = x->field()->is_volatile();
1574   BasicType field_type = x->field_type();
1575 
1576   CodeEmitInfo* info = NULL;
1577   if (needs_patching) {
1578     assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1579     info = state_for(x, x->state_before());
1580   } else if (x->needs_null_check()) {
1581     NullCheck* nc = x->explicit_null_check();
1582     if (nc == NULL) {
1583       info = state_for(x);
1584     } else {
1585       info = state_for(nc);
1586     }
1587   }
1588 
1589   LIRItem object(x->obj(), this);
1590 
1591   object.load_item();
1592 
1593 #ifndef PRODUCT
1594   if (PrintNotLoaded && needs_patching) {
1595     tty->print_cr("   ###class not loaded at load_%s bci %d",
1596                   x->is_static() ?  "static" : "field", x->printable_bci());
1597   }
1598 #endif
1599 
1600   if (x->needs_null_check() &&
1601       (needs_patching ||
1602        MacroAssembler::needs_explicit_null_check(x->offset()))) {
1603     // emit an explicit null check because the offset is too large
1604     __ null_check(object.result(), new CodeEmitInfo(info));
1605   }
1606 
1607   LIR_Opr reg = rlock_result(x, field_type);
1608   LIR_Address* address;
1609   if (needs_patching) {
1610     // we need to patch the offset in the instruction so don't allow
1611     // generate_address to try to be smart about emitting the -1.
1612     // Otherwise the patching code won't know how to find the
1613     // instruction to patch.
1614     address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);
1615   } else {
1616     address = generate_address(object.result(), x->offset(), field_type);
1617   }
1618 
1619   if (is_volatile) {
1620     assert(!needs_patching && x->is_loaded(),
1621            "how do we know it's volatile if it's not loaded");
1622     volatile_field_load(address, reg, info);
1623   } else {
1624     LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1625     __ load(address, reg, info, patch_code);
1626   }
1627 
1628   if (is_volatile && os::is_MP()) {
1629     __ membar_acquire();
1630   }
1631 }
1632 
1633 
1634 //------------------------java.nio.Buffer.checkIndex------------------------
1635 
1636 // int java.nio.Buffer.checkIndex(int)
1637 void LIRGenerator::do_NIOCheckIndex(Intrinsic* x) {
1638   // NOTE: by the time we are in checkIndex() we are guaranteed that
1639   // the buffer is non-null (because checkIndex is package-private and
1640   // only called from within other methods in the buffer).
1641   assert(x->number_of_arguments() == 2, "wrong type");
1642   LIRItem buf  (x->argument_at(0), this);
1643   LIRItem index(x->argument_at(1), this);
1644   buf.load_item();
1645   index.load_item();
1646 
1647   LIR_Opr result = rlock_result(x);
1648   if (GenerateRangeChecks) {
1649     CodeEmitInfo* info = state_for(x);
1650     CodeStub* stub = new RangeCheckStub(info, index.result(), true);
1651     if (index.result()->is_constant()) {
1652       cmp_mem_int(lir_cond_belowEqual, buf.result(), java_nio_Buffer::limit_offset(), index.result()->as_jint(), info);
1653       __ branch(lir_cond_belowEqual, T_INT, stub);
1654     } else {
1655       cmp_reg_mem(lir_cond_aboveEqual, index.result(), buf.result(),
1656                   java_nio_Buffer::limit_offset(), T_INT, info);
1657       __ branch(lir_cond_aboveEqual, T_INT, stub);
1658     }
1659     __ move(index.result(), result);
1660   } else {
1661     // Just load the index into the result register
1662     __ move(index.result(), result);
1663   }
1664 }
1665 
1666 
1667 //------------------------array access--------------------------------------
1668 
1669 
1670 void LIRGenerator::do_ArrayLength(ArrayLength* x) {
1671   LIRItem array(x->array(), this);
1672   array.load_item();
1673   LIR_Opr reg = rlock_result(x);
1674 
1675   CodeEmitInfo* info = NULL;
1676   if (x->needs_null_check()) {
1677     NullCheck* nc = x->explicit_null_check();
1678     if (nc == NULL) {
1679       info = state_for(x);
1680     } else {
1681       info = state_for(nc);
1682     }
1683   }
1684   __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);
1685 }
1686 
1687 
1688 void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {
1689   bool use_length = x->length() != NULL;
1690   LIRItem array(x->array(), this);
1691   LIRItem index(x->index(), this);
1692   LIRItem length(this);
1693   bool needs_range_check = true;
1694 
1695   if (use_length) {
1696     needs_range_check = x->compute_needs_range_check();
1697     if (needs_range_check) {
1698       length.set_instruction(x->length());
1699       length.load_item();
1700     }
1701   }
1702 
1703   array.load_item();
1704   if (index.is_constant() && can_inline_as_constant(x->index())) {
1705     // let it be a constant
1706     index.dont_load_item();
1707   } else {
1708     index.load_item();
1709   }
1710 
1711   CodeEmitInfo* range_check_info = state_for(x);
1712   CodeEmitInfo* null_check_info = NULL;
1713   if (x->needs_null_check()) {
1714     NullCheck* nc = x->explicit_null_check();
1715     if (nc != NULL) {
1716       null_check_info = state_for(nc);
1717     } else {
1718       null_check_info = range_check_info;
1719     }
1720   }
1721 
1722   // emit array address setup early so it schedules better
1723   LIR_Address* array_addr = emit_array_address(array.result(), index.result(), x->elt_type(), false);
1724 
1725   if (GenerateRangeChecks && needs_range_check) {
1726     if (use_length) {
1727       // TODO: use a (modified) version of array_range_check that does not require a
1728       //       constant length to be loaded to a register
1729       __ cmp(lir_cond_belowEqual, length.result(), index.result());
1730       __ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));
1731     } else {
1732       array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1733       // The range check performs the null check, so clear it out for the load
1734       null_check_info = NULL;
1735     }
1736   }
1737 
1738   __ move(array_addr, rlock_result(x, x->elt_type()), null_check_info);
1739 }
1740 
1741 
1742 void LIRGenerator::do_NullCheck(NullCheck* x) {
1743   if (x->can_trap()) {
1744     LIRItem value(x->obj(), this);
1745     value.load_item();
1746     CodeEmitInfo* info = state_for(x);
1747     __ null_check(value.result(), info);
1748   }
1749 }
1750 
1751 
1752 void LIRGenerator::do_Throw(Throw* x) {
1753   LIRItem exception(x->exception(), this);
1754   exception.load_item();
1755   set_no_result(x);
1756   LIR_Opr exception_opr = exception.result();
1757   CodeEmitInfo* info = state_for(x, x->state());
1758 
1759 #ifndef PRODUCT
1760   if (PrintC1Statistics) {
1761     increment_counter(Runtime1::throw_count_address(), T_INT);
1762   }
1763 #endif
1764 
1765   // check if the instruction has an xhandler in any of the nested scopes
1766   bool unwind = false;
1767   if (info->exception_handlers()->length() == 0) {
1768     // this throw is not inside an xhandler
1769     unwind = true;
1770   } else {
1771     // get some idea of the throw type
1772     bool type_is_exact = true;
1773     ciType* throw_type = x->exception()->exact_type();
1774     if (throw_type == NULL) {
1775       type_is_exact = false;
1776       throw_type = x->exception()->declared_type();
1777     }
1778     if (throw_type != NULL && throw_type->is_instance_klass()) {
1779       ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;
1780       unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);
1781     }
1782   }
1783 
1784   // do null check before moving exception oop into fixed register
1785   // to avoid a fixed interval with an oop during the null check.
1786   // Use a copy of the CodeEmitInfo because debug information is
1787   // different for null_check and throw.
1788   if (GenerateCompilerNullChecks &&
1789       (x->exception()->as_NewInstance() == NULL && x->exception()->as_ExceptionObject() == NULL)) {
1790     // if the exception object wasn't created using new then it might be null.
1791     __ null_check(exception_opr, new CodeEmitInfo(info, x->state()->copy(ValueStack::ExceptionState, x->state()->bci())));
1792   }
1793 
1794   if (compilation()->env()->jvmti_can_post_on_exceptions()) {
1795     // we need to go through the exception lookup path to get JVMTI
1796     // notification done
1797     unwind = false;
1798   }
1799 
1800   // move exception oop into fixed register
1801   __ move(exception_opr, exceptionOopOpr());
1802 
1803   if (unwind) {
1804     __ unwind_exception(exceptionOopOpr());
1805   } else {
1806     __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);
1807   }
1808 }
1809 
1810 
1811 void LIRGenerator::do_RoundFP(RoundFP* x) {
1812   LIRItem input(x->input(), this);
1813   input.load_item();
1814   LIR_Opr input_opr = input.result();
1815   assert(input_opr->is_register(), "why round if value is not in a register?");
1816   assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value");
1817   if (input_opr->is_single_fpu()) {
1818     set_result(x, round_item(input_opr)); // This code path not currently taken
1819   } else {
1820     LIR_Opr result = new_register(T_DOUBLE);
1821     set_vreg_flag(result, must_start_in_memory);
1822     __ roundfp(input_opr, LIR_OprFact::illegalOpr, result);
1823     set_result(x, result);
1824   }
1825 }
1826 
1827 void LIRGenerator::do_UnsafeGetRaw(UnsafeGetRaw* x) {
1828   LIRItem base(x->base(), this);
1829   LIRItem idx(this);
1830 
1831   base.load_item();
1832   if (x->has_index()) {
1833     idx.set_instruction(x->index());
1834     idx.load_nonconstant();
1835   }
1836 
1837   LIR_Opr reg = rlock_result(x, x->basic_type());
1838 
1839   int   log2_scale = 0;
1840   if (x->has_index()) {
1841     assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1842     log2_scale = x->log2_scale();
1843   }
1844 
1845   assert(!x->has_index() || idx.value() == x->index(), "should match");
1846 
1847   LIR_Opr base_op = base.result();
1848 #ifndef _LP64
1849   if (x->base()->type()->tag() == longTag) {
1850     base_op = new_register(T_INT);
1851     __ convert(Bytecodes::_l2i, base.result(), base_op);
1852   } else {
1853     assert(x->base()->type()->tag() == intTag, "must be");
1854   }
1855 #endif
1856 
1857   BasicType dst_type = x->basic_type();
1858   LIR_Opr index_op = idx.result();
1859 
1860   LIR_Address* addr;
1861   if (index_op->is_constant()) {
1862     assert(log2_scale == 0, "must not have a scale");
1863     addr = new LIR_Address(base_op, index_op->as_jint(), dst_type);
1864   } else {
1865 #ifdef X86
1866 #ifdef _LP64
1867     if (!index_op->is_illegal() && index_op->type() == T_INT) {
1868       LIR_Opr tmp = new_pointer_register();
1869       __ convert(Bytecodes::_i2l, index_op, tmp);
1870       index_op = tmp;
1871     }
1872 #endif
1873     addr = new LIR_Address(base_op, index_op, LIR_Address::Scale(log2_scale), 0, dst_type);
1874 #elif defined(ARM)
1875     addr = generate_address(base_op, index_op, log2_scale, 0, dst_type);
1876 #else
1877     if (index_op->is_illegal() || log2_scale == 0) {
1878 #ifdef _LP64
1879       if (!index_op->is_illegal() && index_op->type() == T_INT) {
1880         LIR_Opr tmp = new_pointer_register();
1881         __ convert(Bytecodes::_i2l, index_op, tmp);
1882         index_op = tmp;
1883       }
1884 #endif
1885       addr = new LIR_Address(base_op, index_op, dst_type);
1886     } else {
1887       LIR_Opr tmp = new_pointer_register();
1888       __ shift_left(index_op, log2_scale, tmp);
1889       addr = new LIR_Address(base_op, tmp, dst_type);
1890     }
1891 #endif
1892   }
1893 
1894   if (x->may_be_unaligned() && (dst_type == T_LONG || dst_type == T_DOUBLE)) {
1895     __ unaligned_move(addr, reg);
1896   } else {
1897     __ move(addr, reg);
1898   }
1899 }
1900 
1901 
1902 void LIRGenerator::do_UnsafePutRaw(UnsafePutRaw* x) {
1903   int  log2_scale = 0;
1904   BasicType type = x->basic_type();
1905 
1906   if (x->has_index()) {
1907     assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1908     log2_scale = x->log2_scale();
1909   }
1910 
1911   LIRItem base(x->base(), this);
1912   LIRItem value(x->value(), this);
1913   LIRItem idx(this);
1914 
1915   base.load_item();
1916   if (x->has_index()) {
1917     idx.set_instruction(x->index());
1918     idx.load_item();
1919   }
1920 
1921   if (type == T_BYTE || type == T_BOOLEAN) {
1922     value.load_byte_item();
1923   } else {
1924     value.load_item();
1925   }
1926 
1927   set_no_result(x);
1928 
1929   LIR_Opr base_op = base.result();
1930 #ifndef _LP64
1931   if (x->base()->type()->tag() == longTag) {
1932     base_op = new_register(T_INT);
1933     __ convert(Bytecodes::_l2i, base.result(), base_op);
1934   } else {
1935     assert(x->base()->type()->tag() == intTag, "must be");
1936   }
1937 #endif
1938 
1939   LIR_Opr index_op = idx.result();
1940   if (log2_scale != 0) {
1941     // temporary fix (platform dependent code without shift on Intel would be better)
1942     index_op = new_pointer_register();
1943 #ifdef _LP64
1944     if(idx.result()->type() == T_INT) {
1945       __ convert(Bytecodes::_i2l, idx.result(), index_op);
1946     } else {
1947 #endif
1948       // TODO: ARM also allows embedded shift in the address
1949       __ move(idx.result(), index_op);
1950 #ifdef _LP64
1951     }
1952 #endif
1953     __ shift_left(index_op, log2_scale, index_op);
1954   }
1955 #ifdef _LP64
1956   else if(!index_op->is_illegal() && index_op->type() == T_INT) {
1957     LIR_Opr tmp = new_pointer_register();
1958     __ convert(Bytecodes::_i2l, index_op, tmp);
1959     index_op = tmp;
1960   }
1961 #endif
1962 
1963   LIR_Address* addr = new LIR_Address(base_op, index_op, x->basic_type());
1964   __ move(value.result(), addr);
1965 }
1966 
1967 
1968 void LIRGenerator::do_UnsafeGetObject(UnsafeGetObject* x) {
1969   BasicType type = x->basic_type();
1970   LIRItem src(x->object(), this);
1971   LIRItem off(x->offset(), this);
1972 
1973   off.load_item();
1974   src.load_item();
1975 
1976   LIR_Opr reg = reg = rlock_result(x, x->basic_type());
1977 
1978   if (x->is_volatile() && os::is_MP()) __ membar_acquire();
1979   get_Object_unsafe(reg, src.result(), off.result(), type, x->is_volatile());
1980   if (x->is_volatile() && os::is_MP()) __ membar();
1981 }
1982 
1983 
1984 void LIRGenerator::do_UnsafePutObject(UnsafePutObject* x) {
1985   BasicType type = x->basic_type();
1986   LIRItem src(x->object(), this);
1987   LIRItem off(x->offset(), this);
1988   LIRItem data(x->value(), this);
1989 
1990   src.load_item();
1991   if (type == T_BOOLEAN || type == T_BYTE) {
1992     data.load_byte_item();
1993   } else {
1994     data.load_item();
1995   }
1996   off.load_item();
1997 
1998   set_no_result(x);
1999 
2000   if (x->is_volatile() && os::is_MP()) __ membar_release();
2001   put_Object_unsafe(src.result(), off.result(), data.result(), type, x->is_volatile());
2002 }
2003 
2004 
2005 void LIRGenerator::do_UnsafePrefetch(UnsafePrefetch* x, bool is_store) {
2006   LIRItem src(x->object(), this);
2007   LIRItem off(x->offset(), this);
2008 
2009   src.load_item();
2010   if (off.is_constant() && can_inline_as_constant(x->offset())) {
2011     // let it be a constant
2012     off.dont_load_item();
2013   } else {
2014     off.load_item();
2015   }
2016 
2017   set_no_result(x);
2018 
2019   LIR_Address* addr = generate_address(src.result(), off.result(), 0, 0, T_BYTE);
2020   __ prefetch(addr, is_store);
2021 }
2022 
2023 
2024 void LIRGenerator::do_UnsafePrefetchRead(UnsafePrefetchRead* x) {
2025   do_UnsafePrefetch(x, false);
2026 }
2027 
2028 
2029 void LIRGenerator::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {
2030   do_UnsafePrefetch(x, true);
2031 }
2032 
2033 
2034 void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {
2035   int lng = x->length();
2036 
2037   for (int i = 0; i < lng; i++) {
2038     SwitchRange* one_range = x->at(i);
2039     int low_key = one_range->low_key();
2040     int high_key = one_range->high_key();
2041     BlockBegin* dest = one_range->sux();
2042     if (low_key == high_key) {
2043       __ cmp(lir_cond_equal, value, low_key);
2044       __ branch(lir_cond_equal, T_INT, dest);
2045     } else if (high_key - low_key == 1) {
2046       __ cmp(lir_cond_equal, value, low_key);
2047       __ branch(lir_cond_equal, T_INT, dest);
2048       __ cmp(lir_cond_equal, value, high_key);
2049       __ branch(lir_cond_equal, T_INT, dest);
2050     } else {
2051       LabelObj* L = new LabelObj();
2052       __ cmp(lir_cond_less, value, low_key);
2053       __ branch(lir_cond_less, L->label());
2054       __ cmp(lir_cond_lessEqual, value, high_key);
2055       __ branch(lir_cond_lessEqual, T_INT, dest);
2056       __ branch_destination(L->label());
2057     }
2058   }
2059   __ jump(default_sux);
2060 }
2061 
2062 
2063 SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {
2064   SwitchRangeList* res = new SwitchRangeList();
2065   int len = x->length();
2066   if (len > 0) {
2067     BlockBegin* sux = x->sux_at(0);
2068     int key = x->lo_key();
2069     BlockBegin* default_sux = x->default_sux();
2070     SwitchRange* range = new SwitchRange(key, sux);
2071     for (int i = 0; i < len; i++, key++) {
2072       BlockBegin* new_sux = x->sux_at(i);
2073       if (sux == new_sux) {
2074         // still in same range
2075         range->set_high_key(key);
2076       } else {
2077         // skip tests which explicitly dispatch to the default
2078         if (sux != default_sux) {
2079           res->append(range);
2080         }
2081         range = new SwitchRange(key, new_sux);
2082       }
2083       sux = new_sux;
2084     }
2085     if (res->length() == 0 || res->last() != range)  res->append(range);
2086   }
2087   return res;
2088 }
2089 
2090 
2091 // we expect the keys to be sorted by increasing value
2092 SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {
2093   SwitchRangeList* res = new SwitchRangeList();
2094   int len = x->length();
2095   if (len > 0) {
2096     BlockBegin* default_sux = x->default_sux();
2097     int key = x->key_at(0);
2098     BlockBegin* sux = x->sux_at(0);
2099     SwitchRange* range = new SwitchRange(key, sux);
2100     for (int i = 1; i < len; i++) {
2101       int new_key = x->key_at(i);
2102       BlockBegin* new_sux = x->sux_at(i);
2103       if (key+1 == new_key && sux == new_sux) {
2104         // still in same range
2105         range->set_high_key(new_key);
2106       } else {
2107         // skip tests which explicitly dispatch to the default
2108         if (range->sux() != default_sux) {
2109           res->append(range);
2110         }
2111         range = new SwitchRange(new_key, new_sux);
2112       }
2113       key = new_key;
2114       sux = new_sux;
2115     }
2116     if (res->length() == 0 || res->last() != range)  res->append(range);
2117   }
2118   return res;
2119 }
2120 
2121 
2122 void LIRGenerator::do_TableSwitch(TableSwitch* x) {
2123   LIRItem tag(x->tag(), this);
2124   tag.load_item();
2125   set_no_result(x);
2126 
2127   if (x->is_safepoint()) {
2128     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2129   }
2130 
2131   // move values into phi locations
2132   move_to_phi(x->state());
2133 
2134   int lo_key = x->lo_key();
2135   int hi_key = x->hi_key();
2136   int len = x->length();
2137   LIR_Opr value = tag.result();
2138   if (UseTableRanges) {
2139     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2140   } else {
2141     for (int i = 0; i < len; i++) {
2142       __ cmp(lir_cond_equal, value, i + lo_key);
2143       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2144     }
2145     __ jump(x->default_sux());
2146   }
2147 }
2148 
2149 
2150 void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {
2151   LIRItem tag(x->tag(), this);
2152   tag.load_item();
2153   set_no_result(x);
2154 
2155   if (x->is_safepoint()) {
2156     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2157   }
2158 
2159   // move values into phi locations
2160   move_to_phi(x->state());
2161 
2162   LIR_Opr value = tag.result();
2163   if (UseTableRanges) {
2164     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2165   } else {
2166     int len = x->length();
2167     for (int i = 0; i < len; i++) {
2168       __ cmp(lir_cond_equal, value, x->key_at(i));
2169       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2170     }
2171     __ jump(x->default_sux());
2172   }
2173 }
2174 
2175 
2176 void LIRGenerator::do_Goto(Goto* x) {
2177   set_no_result(x);
2178 
2179   if (block()->next()->as_OsrEntry()) {
2180     // need to free up storage used for OSR entry point
2181     LIR_Opr osrBuffer = block()->next()->operand();
2182     BasicTypeList signature;
2183     signature.append(T_INT);
2184     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2185     __ move(osrBuffer, cc->args()->at(0));
2186     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
2187                          getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());
2188   }
2189 
2190   if (x->is_safepoint()) {
2191     ValueStack* state = x->state_before() ? x->state_before() : x->state();
2192 
2193     // increment backedge counter if needed
2194     CodeEmitInfo* info = state_for(x, state);
2195     increment_backedge_counter(info, info->stack()->bci());
2196     CodeEmitInfo* safepoint_info = state_for(x, state);
2197     __ safepoint(safepoint_poll_register(), safepoint_info);
2198   }
2199 
2200   // Gotos can be folded Ifs, handle this case.
2201   if (x->should_profile()) {
2202     ciMethod* method = x->profiled_method();
2203     assert(method != NULL, "method should be set if branch is profiled");
2204     ciMethodData* md = method->method_data();
2205     if (md == NULL) {
2206       bailout("out of memory building methodDataOop");
2207       return;
2208     }
2209     ciProfileData* data = md->bci_to_data(x->profiled_bci());
2210     assert(data != NULL, "must have profiling data");
2211     int offset;
2212     if (x->direction() == Goto::taken) {
2213       assert(data->is_BranchData(), "need BranchData for two-way branches");
2214       offset = md->byte_offset_of_slot(data, BranchData::taken_offset());
2215     } else if (x->direction() == Goto::not_taken) {
2216       assert(data->is_BranchData(), "need BranchData for two-way branches");
2217       offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
2218     } else {
2219       assert(data->is_JumpData(), "need JumpData for branches");
2220       offset = md->byte_offset_of_slot(data, JumpData::taken_offset());
2221     }
2222     LIR_Opr md_reg = new_register(T_OBJECT);
2223     __ oop2reg(md->constant_encoding(), md_reg);
2224 
2225     increment_counter(new LIR_Address(md_reg, offset,
2226                                       NOT_LP64(T_INT) LP64_ONLY(T_LONG)), DataLayout::counter_increment);
2227   }
2228 
2229   // emit phi-instruction move after safepoint since this simplifies
2230   // describing the state as the safepoint.
2231   move_to_phi(x->state());
2232 
2233   __ jump(x->default_sux());
2234 }
2235 
2236 
2237 void LIRGenerator::do_Base(Base* x) {
2238   __ std_entry(LIR_OprFact::illegalOpr);
2239   // Emit moves from physical registers / stack slots to virtual registers
2240   CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2241   IRScope* irScope = compilation()->hir()->top_scope();
2242   int java_index = 0;
2243   for (int i = 0; i < args->length(); i++) {
2244     LIR_Opr src = args->at(i);
2245     assert(!src->is_illegal(), "check");
2246     BasicType t = src->type();
2247 
2248     // Types which are smaller than int are passed as int, so
2249     // correct the type which passed.
2250     switch (t) {
2251     case T_BYTE:
2252     case T_BOOLEAN:
2253     case T_SHORT:
2254     case T_CHAR:
2255       t = T_INT;
2256       break;
2257     }
2258 
2259     LIR_Opr dest = new_register(t);
2260     __ move(src, dest);
2261 
2262     // Assign new location to Local instruction for this local
2263     Local* local = x->state()->local_at(java_index)->as_Local();
2264     assert(local != NULL, "Locals for incoming arguments must have been created");
2265 #ifndef __SOFTFP__
2266     // The java calling convention passes double as long and float as int.
2267     assert(as_ValueType(t)->tag() == local->type()->tag(), "check");
2268 #endif // __SOFTFP__
2269     local->set_operand(dest);
2270     _instruction_for_operand.at_put_grow(dest->vreg_number(), local, NULL);
2271     java_index += type2size[t];
2272   }
2273 
2274   if (compilation()->env()->dtrace_method_probes()) {
2275     BasicTypeList signature;
2276     signature.append(T_INT);    // thread
2277     signature.append(T_OBJECT); // methodOop
2278     LIR_OprList* args = new LIR_OprList();
2279     args->append(getThreadPointer());
2280     LIR_Opr meth = new_register(T_OBJECT);
2281     __ oop2reg(method()->constant_encoding(), meth);
2282     args->append(meth);
2283     call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, NULL);
2284   }
2285 
2286   if (method()->is_synchronized()) {
2287     LIR_Opr obj;
2288     if (method()->is_static()) {
2289       obj = new_register(T_OBJECT);
2290       __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);
2291     } else {
2292       Local* receiver = x->state()->local_at(0)->as_Local();
2293       assert(receiver != NULL, "must already exist");
2294       obj = receiver->operand();
2295     }
2296     assert(obj->is_valid(), "must be valid");
2297 
2298     if (method()->is_synchronized() && GenerateSynchronizationCode) {
2299       LIR_Opr lock = new_register(T_INT);
2300       __ load_stack_address_monitor(0, lock);
2301 
2302       CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL);
2303       CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
2304 
2305       // receiver is guaranteed non-NULL so don't need CodeEmitInfo
2306       __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, NULL);
2307     }
2308   }
2309 
2310   // increment invocation counters if needed
2311   if (!method()->is_accessor()) { // Accessors do not have MDOs, so no counting.
2312     CodeEmitInfo* info = new CodeEmitInfo(scope()->start()->state()->copy(ValueStack::StateBefore, SynchronizationEntryBCI), NULL);
2313     increment_invocation_counter(info);
2314   }
2315 
2316   // all blocks with a successor must end with an unconditional jump
2317   // to the successor even if they are consecutive
2318   __ jump(x->default_sux());
2319 }
2320 
2321 
2322 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
2323   // construct our frame and model the production of incoming pointer
2324   // to the OSR buffer.
2325   __ osr_entry(LIR_Assembler::osrBufferPointer());
2326   LIR_Opr result = rlock_result(x);
2327   __ move(LIR_Assembler::osrBufferPointer(), result);
2328 }
2329 
2330 
2331 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
2332   int i = (x->has_receiver() || x->is_invokedynamic()) ? 1 : 0;
2333   for (; i < args->length(); i++) {
2334     LIRItem* param = args->at(i);
2335     LIR_Opr loc = arg_list->at(i);
2336     if (loc->is_register()) {
2337       param->load_item_force(loc);
2338     } else {
2339       LIR_Address* addr = loc->as_address_ptr();
2340       param->load_for_store(addr->type());
2341       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2342         __ unaligned_move(param->result(), addr);
2343       } else {
2344         __ move(param->result(), addr);
2345       }
2346     }
2347   }
2348 
2349   if (x->has_receiver()) {
2350     LIRItem* receiver = args->at(0);
2351     LIR_Opr loc = arg_list->at(0);
2352     if (loc->is_register()) {
2353       receiver->load_item_force(loc);
2354     } else {
2355       assert(loc->is_address(), "just checking");
2356       receiver->load_for_store(T_OBJECT);
2357       __ move(receiver->result(), loc);
2358     }
2359   }
2360 }
2361 
2362 
2363 // Visits all arguments, returns appropriate items without loading them
2364 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
2365   LIRItemList* argument_items = new LIRItemList();
2366   if (x->has_receiver()) {
2367     LIRItem* receiver = new LIRItem(x->receiver(), this);
2368     argument_items->append(receiver);
2369   }
2370   if (x->is_invokedynamic()) {
2371     // Insert a dummy for the synthetic MethodHandle argument.
2372     argument_items->append(NULL);
2373   }
2374   int idx = x->has_receiver() ? 1 : 0;
2375   for (int i = 0; i < x->number_of_arguments(); i++) {
2376     LIRItem* param = new LIRItem(x->argument_at(i), this);
2377     argument_items->append(param);
2378     idx += (param->type()->is_double_word() ? 2 : 1);
2379   }
2380   return argument_items;
2381 }
2382 
2383 
2384 // The invoke with receiver has following phases:
2385 //   a) traverse and load/lock receiver;
2386 //   b) traverse all arguments -> item-array (invoke_visit_argument)
2387 //   c) push receiver on stack
2388 //   d) load each of the items and push on stack
2389 //   e) unlock receiver
2390 //   f) move receiver into receiver-register %o0
2391 //   g) lock result registers and emit call operation
2392 //
2393 // Before issuing a call, we must spill-save all values on stack
2394 // that are in caller-save register. "spill-save" moves thos registers
2395 // either in a free callee-save register or spills them if no free
2396 // callee save register is available.
2397 //
2398 // The problem is where to invoke spill-save.
2399 // - if invoked between e) and f), we may lock callee save
2400 //   register in "spill-save" that destroys the receiver register
2401 //   before f) is executed
2402 // - if we rearange the f) to be earlier, by loading %o0, it
2403 //   may destroy a value on the stack that is currently in %o0
2404 //   and is waiting to be spilled
2405 // - if we keep the receiver locked while doing spill-save,
2406 //   we cannot spill it as it is spill-locked
2407 //
2408 void LIRGenerator::do_Invoke(Invoke* x) {
2409   CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);
2410 
2411   LIR_OprList* arg_list = cc->args();
2412   LIRItemList* args = invoke_visit_arguments(x);
2413   LIR_Opr receiver = LIR_OprFact::illegalOpr;
2414 
2415   // setup result register
2416   LIR_Opr result_register = LIR_OprFact::illegalOpr;
2417   if (x->type() != voidType) {
2418     result_register = result_register_for(x->type());
2419   }
2420 
2421   CodeEmitInfo* info = state_for(x, x->state());
2422 
2423   // invokedynamics can deoptimize.
2424   CodeEmitInfo* deopt_info = x->is_invokedynamic() ? state_for(x, x->state_before()) : NULL;
2425 
2426   invoke_load_arguments(x, args, arg_list);
2427 
2428   if (x->has_receiver()) {
2429     args->at(0)->load_item_force(LIR_Assembler::receiverOpr());
2430     receiver = args->at(0)->result();
2431   }
2432 
2433   // emit invoke code
2434   bool optimized = x->target_is_loaded() && x->target_is_final();
2435   assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");
2436 
2437   // JSR 292
2438   // Preserve the SP over MethodHandle call sites.
2439   ciMethod* target = x->target();
2440   if (target->is_method_handle_invoke()) {
2441     info->set_is_method_handle_invoke(true);
2442     __ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr());
2443   }
2444 
2445   switch (x->code()) {
2446     case Bytecodes::_invokestatic:
2447       __ call_static(target, result_register,
2448                      SharedRuntime::get_resolve_static_call_stub(),
2449                      arg_list, info);
2450       break;
2451     case Bytecodes::_invokespecial:
2452     case Bytecodes::_invokevirtual:
2453     case Bytecodes::_invokeinterface:
2454       // for final target we still produce an inline cache, in order
2455       // to be able to call mixed mode
2456       if (x->code() == Bytecodes::_invokespecial || optimized) {
2457         __ call_opt_virtual(target, receiver, result_register,
2458                             SharedRuntime::get_resolve_opt_virtual_call_stub(),
2459                             arg_list, info);
2460       } else if (x->vtable_index() < 0) {
2461         __ call_icvirtual(target, receiver, result_register,
2462                           SharedRuntime::get_resolve_virtual_call_stub(),
2463                           arg_list, info);
2464       } else {
2465         int entry_offset = instanceKlass::vtable_start_offset() + x->vtable_index() * vtableEntry::size();
2466         int vtable_offset = entry_offset * wordSize + vtableEntry::method_offset_in_bytes();
2467         __ call_virtual(target, receiver, result_register, vtable_offset, arg_list, info);
2468       }
2469       break;
2470     case Bytecodes::_invokedynamic: {
2471       ciBytecodeStream bcs(x->scope()->method());
2472       bcs.force_bci(x->state()->bci());
2473       assert(bcs.cur_bc() == Bytecodes::_invokedynamic, "wrong stream");
2474       ciCPCache* cpcache = bcs.get_cpcache();
2475 
2476       // Get CallSite offset from constant pool cache pointer.
2477       int index = bcs.get_method_index();
2478       size_t call_site_offset = cpcache->get_f1_offset(index);
2479 
2480       // If this invokedynamic call site hasn't been executed yet in
2481       // the interpreter, the CallSite object in the constant pool
2482       // cache is still null and we need to deoptimize.
2483       if (cpcache->is_f1_null_at(index)) {
2484         // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
2485         // clone all handlers.  This is handled transparently in other
2486         // places by the CodeEmitInfo cloning logic but is handled
2487         // specially here because a stub isn't being used.
2488         x->set_exception_handlers(new XHandlers(x->exception_handlers()));
2489 
2490         DeoptimizeStub* deopt_stub = new DeoptimizeStub(deopt_info);
2491         __ jump(deopt_stub);
2492       }
2493 
2494       // Use the receiver register for the synthetic MethodHandle
2495       // argument.
2496       receiver = LIR_Assembler::receiverOpr();
2497       LIR_Opr tmp = new_register(objectType);
2498 
2499       // Load CallSite object from constant pool cache.
2500       __ oop2reg(cpcache->constant_encoding(), tmp);
2501       __ load(new LIR_Address(tmp, call_site_offset, T_OBJECT), tmp);
2502 
2503       // Load target MethodHandle from CallSite object.
2504       __ load(new LIR_Address(tmp, java_dyn_CallSite::target_offset_in_bytes(), T_OBJECT), receiver);
2505 
2506       __ call_dynamic(target, receiver, result_register,
2507                       SharedRuntime::get_resolve_opt_virtual_call_stub(),
2508                       arg_list, info);
2509       break;
2510     }
2511     default:
2512       ShouldNotReachHere();
2513       break;
2514   }
2515 
2516   // JSR 292
2517   // Restore the SP after MethodHandle call sites.
2518   if (target->is_method_handle_invoke()) {
2519     __ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer());
2520   }
2521 
2522   if (x->type()->is_float() || x->type()->is_double()) {
2523     // Force rounding of results from non-strictfp when in strictfp
2524     // scope (or when we don't know the strictness of the callee, to
2525     // be safe.)
2526     if (method()->is_strict()) {
2527       if (!x->target_is_loaded() || !x->target_is_strictfp()) {
2528         result_register = round_item(result_register);
2529       }
2530     }
2531   }
2532 
2533   if (result_register->is_valid()) {
2534     LIR_Opr result = rlock_result(x);
2535     __ move(result_register, result);
2536   }
2537 }
2538 
2539 
2540 void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {
2541   assert(x->number_of_arguments() == 1, "wrong type");
2542   LIRItem value       (x->argument_at(0), this);
2543   LIR_Opr reg = rlock_result(x);
2544   value.load_item();
2545   LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));
2546   __ move(tmp, reg);
2547 }
2548 
2549 
2550 
2551 // Code for  :  x->x() {x->cond()} x->y() ? x->tval() : x->fval()
2552 void LIRGenerator::do_IfOp(IfOp* x) {
2553 #ifdef ASSERT
2554   {
2555     ValueTag xtag = x->x()->type()->tag();
2556     ValueTag ttag = x->tval()->type()->tag();
2557     assert(xtag == intTag || xtag == objectTag, "cannot handle others");
2558     assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
2559     assert(ttag == x->fval()->type()->tag(), "cannot handle others");
2560   }
2561 #endif
2562 
2563   LIRItem left(x->x(), this);
2564   LIRItem right(x->y(), this);
2565   left.load_item();
2566   if (can_inline_as_constant(right.value())) {
2567     right.dont_load_item();
2568   } else {
2569     right.load_item();
2570   }
2571 
2572   LIRItem t_val(x->tval(), this);
2573   LIRItem f_val(x->fval(), this);
2574   t_val.dont_load_item();
2575   f_val.dont_load_item();
2576   LIR_Opr reg = rlock_result(x);
2577 
2578   __ cmp(lir_cond(x->cond()), left.result(), right.result());
2579   __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg);
2580 }
2581 
2582 
2583 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
2584   switch (x->id()) {
2585   case vmIntrinsics::_intBitsToFloat      :
2586   case vmIntrinsics::_doubleToRawLongBits :
2587   case vmIntrinsics::_longBitsToDouble    :
2588   case vmIntrinsics::_floatToRawIntBits   : {
2589     do_FPIntrinsics(x);
2590     break;
2591   }
2592 
2593   case vmIntrinsics::_currentTimeMillis: {
2594     assert(x->number_of_arguments() == 0, "wrong type");
2595     LIR_Opr reg = result_register_for(x->type());
2596     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeMillis), getThreadTemp(),
2597                          reg, new LIR_OprList());
2598     LIR_Opr result = rlock_result(x);
2599     __ move(reg, result);
2600     break;
2601   }
2602 
2603   case vmIntrinsics::_nanoTime: {
2604     assert(x->number_of_arguments() == 0, "wrong type");
2605     LIR_Opr reg = result_register_for(x->type());
2606     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeNanos), getThreadTemp(),
2607                          reg, new LIR_OprList());
2608     LIR_Opr result = rlock_result(x);
2609     __ move(reg, result);
2610     break;
2611   }
2612 
2613   case vmIntrinsics::_Object_init:    do_RegisterFinalizer(x); break;
2614   case vmIntrinsics::_getClass:       do_getClass(x);      break;
2615   case vmIntrinsics::_currentThread:  do_currentThread(x); break;
2616 
2617   case vmIntrinsics::_dlog:           // fall through
2618   case vmIntrinsics::_dlog10:         // fall through
2619   case vmIntrinsics::_dabs:           // fall through
2620   case vmIntrinsics::_dsqrt:          // fall through
2621   case vmIntrinsics::_dtan:           // fall through
2622   case vmIntrinsics::_dsin :          // fall through
2623   case vmIntrinsics::_dcos :          do_MathIntrinsic(x); break;
2624   case vmIntrinsics::_arraycopy:      do_ArrayCopy(x);     break;
2625 
2626   // java.nio.Buffer.checkIndex
2627   case vmIntrinsics::_checkIndex:     do_NIOCheckIndex(x); break;
2628 
2629   case vmIntrinsics::_compareAndSwapObject:
2630     do_CompareAndSwap(x, objectType);
2631     break;
2632   case vmIntrinsics::_compareAndSwapInt:
2633     do_CompareAndSwap(x, intType);
2634     break;
2635   case vmIntrinsics::_compareAndSwapLong:
2636     do_CompareAndSwap(x, longType);
2637     break;
2638 
2639     // sun.misc.AtomicLongCSImpl.attemptUpdate
2640   case vmIntrinsics::_attemptUpdate:
2641     do_AttemptUpdate(x);
2642     break;
2643 
2644   default: ShouldNotReachHere(); break;
2645   }
2646 }
2647 
2648 void LIRGenerator::do_ProfileCall(ProfileCall* x) {
2649   // Need recv in a temporary register so it interferes with the other temporaries
2650   LIR_Opr recv = LIR_OprFact::illegalOpr;
2651   LIR_Opr mdo = new_register(T_OBJECT);
2652   // tmp is used to hold the counters on SPARC
2653   LIR_Opr tmp = new_pointer_register();
2654   if (x->recv() != NULL) {
2655     LIRItem value(x->recv(), this);
2656     value.load_item();
2657     recv = new_register(T_OBJECT);
2658     __ move(value.result(), recv);
2659   }
2660   __ profile_call(x->method(), x->bci_of_invoke(), mdo, recv, tmp, x->known_holder());
2661 }
2662 
2663 void LIRGenerator::do_ProfileInvoke(ProfileInvoke* x) {
2664   // We can safely ignore accessors here, since c2 will inline them anyway,
2665   // accessors are also always mature.
2666   if (!x->inlinee()->is_accessor()) {
2667     CodeEmitInfo* info = state_for(x, x->state(), true);
2668     // Increment invocation counter, don't notify the runtime, because we don't inline loops,
2669     increment_event_counter_impl(info, x->inlinee(), 0, InvocationEntryBci, false, false);
2670   }
2671 }
2672 
2673 void LIRGenerator::increment_event_counter(CodeEmitInfo* info, int bci, bool backedge) {
2674   int freq_log;
2675   int level = compilation()->env()->comp_level();
2676   if (level == CompLevel_limited_profile) {
2677     freq_log = (backedge ? Tier2BackedgeNotifyFreqLog : Tier2InvokeNotifyFreqLog);
2678   } else if (level == CompLevel_full_profile) {
2679     freq_log = (backedge ? Tier3BackedgeNotifyFreqLog : Tier3InvokeNotifyFreqLog);
2680   } else {
2681     ShouldNotReachHere();
2682   }
2683   // Increment the appropriate invocation/backedge counter and notify the runtime.
2684   increment_event_counter_impl(info, info->scope()->method(), (1 << freq_log) - 1, bci, backedge, true);
2685 }
2686 
2687 void LIRGenerator::increment_event_counter_impl(CodeEmitInfo* info,
2688                                                 ciMethod *method, int frequency,
2689                                                 int bci, bool backedge, bool notify) {
2690   assert(frequency == 0 || is_power_of_2(frequency + 1), "Frequency must be x^2 - 1 or 0");
2691   int level = _compilation->env()->comp_level();
2692   assert(level > CompLevel_simple, "Shouldn't be here");
2693 
2694   int offset = -1;
2695   LIR_Opr counter_holder = new_register(T_OBJECT);
2696   LIR_Opr meth;
2697   if (level == CompLevel_limited_profile) {
2698     offset = in_bytes(backedge ? methodOopDesc::backedge_counter_offset() :
2699                                  methodOopDesc::invocation_counter_offset());
2700     __ oop2reg(method->constant_encoding(), counter_holder);
2701     meth = counter_holder;
2702   } else if (level == CompLevel_full_profile) {
2703     offset = in_bytes(backedge ? methodDataOopDesc::backedge_counter_offset() :
2704                                  methodDataOopDesc::invocation_counter_offset());
2705     __ oop2reg(method->method_data()->constant_encoding(), counter_holder);
2706     meth = new_register(T_OBJECT);
2707     __ oop2reg(method->constant_encoding(), meth);
2708   } else {
2709     ShouldNotReachHere();
2710   }
2711   LIR_Address* counter = new LIR_Address(counter_holder, offset, T_INT);
2712   LIR_Opr result = new_register(T_INT);
2713   __ load(counter, result);
2714   __ add(result, LIR_OprFact::intConst(InvocationCounter::count_increment), result);
2715   __ store(result, counter);
2716   if (notify) {
2717     LIR_Opr mask = load_immediate(frequency << InvocationCounter::count_shift, T_INT);
2718     __ logical_and(result, mask, result);
2719     __ cmp(lir_cond_equal, result, LIR_OprFact::intConst(0));
2720     // The bci for info can point to cmp for if's we want the if bci
2721     CodeStub* overflow = new CounterOverflowStub(info, bci, meth);
2722     __ branch(lir_cond_equal, T_INT, overflow);
2723     __ branch_destination(overflow->continuation());
2724   }
2725 }
2726 
2727 LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
2728   LIRItemList args(1);
2729   LIRItem value(arg1, this);
2730   args.append(&value);
2731   BasicTypeList signature;
2732   signature.append(as_BasicType(arg1->type()));
2733 
2734   return call_runtime(&signature, &args, entry, result_type, info);
2735 }
2736 
2737 
2738 LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {
2739   LIRItemList args(2);
2740   LIRItem value1(arg1, this);
2741   LIRItem value2(arg2, this);
2742   args.append(&value1);
2743   args.append(&value2);
2744   BasicTypeList signature;
2745   signature.append(as_BasicType(arg1->type()));
2746   signature.append(as_BasicType(arg2->type()));
2747 
2748   return call_runtime(&signature, &args, entry, result_type, info);
2749 }
2750 
2751 
2752 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,
2753                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
2754   // get a result register
2755   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2756   LIR_Opr result = LIR_OprFact::illegalOpr;
2757   if (result_type->tag() != voidTag) {
2758     result = new_register(result_type);
2759     phys_reg = result_register_for(result_type);
2760   }
2761 
2762   // move the arguments into the correct location
2763   CallingConvention* cc = frame_map()->c_calling_convention(signature);
2764   assert(cc->length() == args->length(), "argument mismatch");
2765   for (int i = 0; i < args->length(); i++) {
2766     LIR_Opr arg = args->at(i);
2767     LIR_Opr loc = cc->at(i);
2768     if (loc->is_register()) {
2769       __ move(arg, loc);
2770     } else {
2771       LIR_Address* addr = loc->as_address_ptr();
2772 //           if (!can_store_as_constant(arg)) {
2773 //             LIR_Opr tmp = new_register(arg->type());
2774 //             __ move(arg, tmp);
2775 //             arg = tmp;
2776 //           }
2777       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2778         __ unaligned_move(arg, addr);
2779       } else {
2780         __ move(arg, addr);
2781       }
2782     }
2783   }
2784 
2785   if (info) {
2786     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2787   } else {
2788     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2789   }
2790   if (result->is_valid()) {
2791     __ move(phys_reg, result);
2792   }
2793   return result;
2794 }
2795 
2796 
2797 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,
2798                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
2799   // get a result register
2800   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2801   LIR_Opr result = LIR_OprFact::illegalOpr;
2802   if (result_type->tag() != voidTag) {
2803     result = new_register(result_type);
2804     phys_reg = result_register_for(result_type);
2805   }
2806 
2807   // move the arguments into the correct location
2808   CallingConvention* cc = frame_map()->c_calling_convention(signature);
2809 
2810   assert(cc->length() == args->length(), "argument mismatch");
2811   for (int i = 0; i < args->length(); i++) {
2812     LIRItem* arg = args->at(i);
2813     LIR_Opr loc = cc->at(i);
2814     if (loc->is_register()) {
2815       arg->load_item_force(loc);
2816     } else {
2817       LIR_Address* addr = loc->as_address_ptr();
2818       arg->load_for_store(addr->type());
2819       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2820         __ unaligned_move(arg->result(), addr);
2821       } else {
2822         __ move(arg->result(), addr);
2823       }
2824     }
2825   }
2826 
2827   if (info) {
2828     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2829   } else {
2830     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2831   }
2832   if (result->is_valid()) {
2833     __ move(phys_reg, result);
2834   }
2835   return result;
2836 }