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   int index;
 390   Value value;
 391   for_each_stack_value(state, index, value) {
 392     assert(value->subst() == value, "missed substition");
 393     if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
 394       walk(value);
 395       assert(value->operand()->is_valid(), "must be evaluated now");
 396     }
 397   }
 398   ValueStack* s = state;
 399   int bci = x->bci();
 400   for_each_state(s) {
 401     IRScope* scope = s->scope();
 402     ciMethod* method = scope->method();
 403 
 404     MethodLivenessResult liveness = method->liveness_at_bci(bci);
 405     if (bci == SynchronizationEntryBCI) {
 406       if (x->as_ExceptionObject() || x->as_Throw()) {
 407         // all locals are dead on exit from the synthetic unlocker
 408         liveness.clear();
 409       } else {
 410         assert(x->as_MonitorEnter(), "only other case is MonitorEnter");
 411       }
 412     }
 413     if (!liveness.is_valid()) {
 414       // Degenerate or breakpointed method.
 415       bailout("Degenerate or breakpointed method");
 416     } else {
 417       assert((int)liveness.size() == s->locals_size(), "error in use of liveness");
 418       for_each_local_value(s, index, value) {
 419         assert(value->subst() == value, "missed substition");
 420         if (liveness.at(index) && !value->type()->is_illegal()) {
 421           if (!value->is_pinned() && value->as_Constant() == NULL && value->as_Local() == NULL) {
 422             walk(value);
 423             assert(value->operand()->is_valid(), "must be evaluated now");
 424           }
 425         } else {
 426           // NULL out this local so that linear scan can assume that all non-NULL values are live.
 427           s->invalidate_local(index);
 428         }
 429       }
 430     }
 431     bci = scope->caller_bci();
 432   }
 433 
 434   return new CodeEmitInfo(x->bci(), state, ignore_xhandler ? NULL : x->exception_handlers());
 435 }
 436 
 437 
 438 CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {
 439   return state_for(x, x->lock_stack());
 440 }
 441 
 442 
 443 void LIRGenerator::jobject2reg_with_patching(LIR_Opr r, ciObject* obj, CodeEmitInfo* info) {
 444   if (!obj->is_loaded() || PatchALot) {
 445     assert(info != NULL, "info must be set if class is not loaded");
 446     __ oop2reg_patch(NULL, r, info);
 447   } else {
 448     // no patching needed
 449     __ oop2reg(obj->constant_encoding(), r);
 450   }
 451 }
 452 
 453 
 454 void LIRGenerator::array_range_check(LIR_Opr array, LIR_Opr index,
 455                                     CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info) {
 456   CodeStub* stub = new RangeCheckStub(range_check_info, index);
 457   if (index->is_constant()) {
 458     cmp_mem_int(lir_cond_belowEqual, array, arrayOopDesc::length_offset_in_bytes(),
 459                 index->as_jint(), null_check_info);
 460     __ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
 461   } else {
 462     cmp_reg_mem(lir_cond_aboveEqual, index, array,
 463                 arrayOopDesc::length_offset_in_bytes(), T_INT, null_check_info);
 464     __ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
 465   }
 466 }
 467 
 468 
 469 void LIRGenerator::nio_range_check(LIR_Opr buffer, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
 470   CodeStub* stub = new RangeCheckStub(info, index, true);
 471   if (index->is_constant()) {
 472     cmp_mem_int(lir_cond_belowEqual, buffer, java_nio_Buffer::limit_offset(), index->as_jint(), info);
 473     __ branch(lir_cond_belowEqual, T_INT, stub); // forward branch
 474   } else {
 475     cmp_reg_mem(lir_cond_aboveEqual, index, buffer,
 476                 java_nio_Buffer::limit_offset(), T_INT, info);
 477     __ branch(lir_cond_aboveEqual, T_INT, stub); // forward branch
 478   }
 479   __ move(index, result);
 480 }
 481 
 482 
 483 // increment a counter returning the incremented value
 484 LIR_Opr LIRGenerator::increment_and_return_counter(LIR_Opr base, int offset, int increment) {
 485   LIR_Address* counter = new LIR_Address(base, offset, T_INT);
 486   LIR_Opr result = new_register(T_INT);
 487   __ load(counter, result);
 488   __ add(result, LIR_OprFact::intConst(increment), result);
 489   __ store(result, counter);
 490   return result;
 491 }
 492 
 493 
 494 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) {
 495   LIR_Opr result_op = result;
 496   LIR_Opr left_op   = left;
 497   LIR_Opr right_op  = right;
 498 
 499   if (TwoOperandLIRForm && left_op != result_op) {
 500     assert(right_op != result_op, "malformed");
 501     __ move(left_op, result_op);
 502     left_op = result_op;
 503   }
 504 
 505   switch(code) {
 506     case Bytecodes::_dadd:
 507     case Bytecodes::_fadd:
 508     case Bytecodes::_ladd:
 509     case Bytecodes::_iadd:  __ add(left_op, right_op, result_op); break;
 510     case Bytecodes::_fmul:
 511     case Bytecodes::_lmul:  __ mul(left_op, right_op, result_op); break;
 512 
 513     case Bytecodes::_dmul:
 514       {
 515         if (is_strictfp) {
 516           __ mul_strictfp(left_op, right_op, result_op, tmp_op); break;
 517         } else {
 518           __ mul(left_op, right_op, result_op); break;
 519         }
 520       }
 521       break;
 522 
 523     case Bytecodes::_imul:
 524       {
 525         bool    did_strength_reduce = false;
 526 
 527         if (right->is_constant()) {
 528           int c = right->as_jint();
 529           if (is_power_of_2(c)) {
 530             // do not need tmp here
 531             __ shift_left(left_op, exact_log2(c), result_op);
 532             did_strength_reduce = true;
 533           } else {
 534             did_strength_reduce = strength_reduce_multiply(left_op, c, result_op, tmp_op);
 535           }
 536         }
 537         // we couldn't strength reduce so just emit the multiply
 538         if (!did_strength_reduce) {
 539           __ mul(left_op, right_op, result_op);
 540         }
 541       }
 542       break;
 543 
 544     case Bytecodes::_dsub:
 545     case Bytecodes::_fsub:
 546     case Bytecodes::_lsub:
 547     case Bytecodes::_isub: __ sub(left_op, right_op, result_op); break;
 548 
 549     case Bytecodes::_fdiv: __ div (left_op, right_op, result_op); break;
 550     // ldiv and lrem are implemented with a direct runtime call
 551 
 552     case Bytecodes::_ddiv:
 553       {
 554         if (is_strictfp) {
 555           __ div_strictfp (left_op, right_op, result_op, tmp_op); break;
 556         } else {
 557           __ div (left_op, right_op, result_op); break;
 558         }
 559       }
 560       break;
 561 
 562     case Bytecodes::_drem:
 563     case Bytecodes::_frem: __ rem (left_op, right_op, result_op); break;
 564 
 565     default: ShouldNotReachHere();
 566   }
 567 }
 568 
 569 
 570 void LIRGenerator::arithmetic_op_int(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp) {
 571   arithmetic_op(code, result, left, right, false, tmp);
 572 }
 573 
 574 
 575 void LIRGenerator::arithmetic_op_long(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
 576   arithmetic_op(code, result, left, right, false, LIR_OprFact::illegalOpr, info);
 577 }
 578 
 579 
 580 void LIRGenerator::arithmetic_op_fpu(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp) {
 581   arithmetic_op(code, result, left, right, is_strictfp, tmp);
 582 }
 583 
 584 
 585 void LIRGenerator::shift_op(Bytecodes::Code code, LIR_Opr result_op, LIR_Opr value, LIR_Opr count, LIR_Opr tmp) {
 586   if (TwoOperandLIRForm && value != result_op) {
 587     assert(count != result_op, "malformed");
 588     __ move(value, result_op);
 589     value = result_op;
 590   }
 591 
 592   assert(count->is_constant() || count->is_register(), "must be");
 593   switch(code) {
 594   case Bytecodes::_ishl:
 595   case Bytecodes::_lshl: __ shift_left(value, count, result_op, tmp); break;
 596   case Bytecodes::_ishr:
 597   case Bytecodes::_lshr: __ shift_right(value, count, result_op, tmp); break;
 598   case Bytecodes::_iushr:
 599   case Bytecodes::_lushr: __ unsigned_shift_right(value, count, result_op, tmp); break;
 600   default: ShouldNotReachHere();
 601   }
 602 }
 603 
 604 
 605 void LIRGenerator::logic_op (Bytecodes::Code code, LIR_Opr result_op, LIR_Opr left_op, LIR_Opr right_op) {
 606   if (TwoOperandLIRForm && left_op != result_op) {
 607     assert(right_op != result_op, "malformed");
 608     __ move(left_op, result_op);
 609     left_op = result_op;
 610   }
 611 
 612   switch(code) {
 613     case Bytecodes::_iand:
 614     case Bytecodes::_land:  __ logical_and(left_op, right_op, result_op); break;
 615 
 616     case Bytecodes::_ior:
 617     case Bytecodes::_lor:   __ logical_or(left_op, right_op, result_op);  break;
 618 
 619     case Bytecodes::_ixor:
 620     case Bytecodes::_lxor:  __ logical_xor(left_op, right_op, result_op); break;
 621 
 622     default: ShouldNotReachHere();
 623   }
 624 }
 625 
 626 
 627 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) {
 628   if (!GenerateSynchronizationCode) return;
 629   // for slow path, use debug info for state after successful locking
 630   CodeStub* slow_path = new MonitorEnterStub(object, lock, info);
 631   __ load_stack_address_monitor(monitor_no, lock);
 632   // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
 633   __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);
 634 }
 635 
 636 
 637 void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
 638   if (!GenerateSynchronizationCode) return;
 639   // setup registers
 640   LIR_Opr hdr = lock;
 641   lock = new_hdr;
 642   CodeStub* slow_path = new MonitorExitStub(lock, UseFastLocking, monitor_no);
 643   __ load_stack_address_monitor(monitor_no, lock);
 644   __ unlock_object(hdr, object, lock, scratch, slow_path);
 645 }
 646 
 647 
 648 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) {
 649   jobject2reg_with_patching(klass_reg, klass, info);
 650   // If klass is not loaded we do not know if the klass has finalizers:
 651   if (UseFastNewInstance && klass->is_loaded()
 652       && !Klass::layout_helper_needs_slow_path(klass->layout_helper())) {
 653 
 654     Runtime1::StubID stub_id = klass->is_initialized() ? Runtime1::fast_new_instance_id : Runtime1::fast_new_instance_init_check_id;
 655 
 656     CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, stub_id);
 657 
 658     assert(klass->is_loaded(), "must be loaded");
 659     // allocate space for instance
 660     assert(klass->size_helper() >= 0, "illegal instance size");
 661     const int instance_size = align_object_size(klass->size_helper());
 662     __ allocate_object(dst, scratch1, scratch2, scratch3, scratch4,
 663                        oopDesc::header_size(), instance_size, klass_reg, !klass->is_initialized(), slow_path);
 664   } else {
 665     CodeStub* slow_path = new NewInstanceStub(klass_reg, dst, klass, info, Runtime1::new_instance_id);
 666     __ branch(lir_cond_always, T_ILLEGAL, slow_path);
 667     __ branch_destination(slow_path->continuation());
 668   }
 669 }
 670 
 671 
 672 static bool is_constant_zero(Instruction* inst) {
 673   IntConstant* c = inst->type()->as_IntConstant();
 674   if (c) {
 675     return (c->value() == 0);
 676   }
 677   return false;
 678 }
 679 
 680 
 681 static bool positive_constant(Instruction* inst) {
 682   IntConstant* c = inst->type()->as_IntConstant();
 683   if (c) {
 684     return (c->value() >= 0);
 685   }
 686   return false;
 687 }
 688 
 689 
 690 static ciArrayKlass* as_array_klass(ciType* type) {
 691   if (type != NULL && type->is_array_klass() && type->is_loaded()) {
 692     return (ciArrayKlass*)type;
 693   } else {
 694     return NULL;
 695   }
 696 }
 697 
 698 void LIRGenerator::arraycopy_helper(Intrinsic* x, int* flagsp, ciArrayKlass** expected_typep) {
 699   Instruction* src     = x->argument_at(0);
 700   Instruction* src_pos = x->argument_at(1);
 701   Instruction* dst     = x->argument_at(2);
 702   Instruction* dst_pos = x->argument_at(3);
 703   Instruction* length  = x->argument_at(4);
 704 
 705   // first try to identify the likely type of the arrays involved
 706   ciArrayKlass* expected_type = NULL;
 707   bool is_exact = false;
 708   {
 709     ciArrayKlass* src_exact_type    = as_array_klass(src->exact_type());
 710     ciArrayKlass* src_declared_type = as_array_klass(src->declared_type());
 711     ciArrayKlass* dst_exact_type    = as_array_klass(dst->exact_type());
 712     ciArrayKlass* dst_declared_type = as_array_klass(dst->declared_type());
 713     if (src_exact_type != NULL && src_exact_type == dst_exact_type) {
 714       // the types exactly match so the type is fully known
 715       is_exact = true;
 716       expected_type = src_exact_type;
 717     } else if (dst_exact_type != NULL && dst_exact_type->is_obj_array_klass()) {
 718       ciArrayKlass* dst_type = (ciArrayKlass*) dst_exact_type;
 719       ciArrayKlass* src_type = NULL;
 720       if (src_exact_type != NULL && src_exact_type->is_obj_array_klass()) {
 721         src_type = (ciArrayKlass*) src_exact_type;
 722       } else if (src_declared_type != NULL && src_declared_type->is_obj_array_klass()) {
 723         src_type = (ciArrayKlass*) src_declared_type;
 724       }
 725       if (src_type != NULL) {
 726         if (src_type->element_type()->is_subtype_of(dst_type->element_type())) {
 727           is_exact = true;
 728           expected_type = dst_type;
 729         }
 730       }
 731     }
 732     // at least pass along a good guess
 733     if (expected_type == NULL) expected_type = dst_exact_type;
 734     if (expected_type == NULL) expected_type = src_declared_type;
 735     if (expected_type == NULL) expected_type = dst_declared_type;
 736   }
 737 
 738   // if a probable array type has been identified, figure out if any
 739   // of the required checks for a fast case can be elided.
 740   int flags = LIR_OpArrayCopy::all_flags;
 741   if (expected_type != NULL) {
 742     // try to skip null checks
 743     if (src->as_NewArray() != NULL)
 744       flags &= ~LIR_OpArrayCopy::src_null_check;
 745     if (dst->as_NewArray() != NULL)
 746       flags &= ~LIR_OpArrayCopy::dst_null_check;
 747 
 748     // check from incoming constant values
 749     if (positive_constant(src_pos))
 750       flags &= ~LIR_OpArrayCopy::src_pos_positive_check;
 751     if (positive_constant(dst_pos))
 752       flags &= ~LIR_OpArrayCopy::dst_pos_positive_check;
 753     if (positive_constant(length))
 754       flags &= ~LIR_OpArrayCopy::length_positive_check;
 755 
 756     // see if the range check can be elided, which might also imply
 757     // that src or dst is non-null.
 758     ArrayLength* al = length->as_ArrayLength();
 759     if (al != NULL) {
 760       if (al->array() == src) {
 761         // it's the length of the source array
 762         flags &= ~LIR_OpArrayCopy::length_positive_check;
 763         flags &= ~LIR_OpArrayCopy::src_null_check;
 764         if (is_constant_zero(src_pos))
 765           flags &= ~LIR_OpArrayCopy::src_range_check;
 766       }
 767       if (al->array() == dst) {
 768         // it's the length of the destination array
 769         flags &= ~LIR_OpArrayCopy::length_positive_check;
 770         flags &= ~LIR_OpArrayCopy::dst_null_check;
 771         if (is_constant_zero(dst_pos))
 772           flags &= ~LIR_OpArrayCopy::dst_range_check;
 773       }
 774     }
 775     if (is_exact) {
 776       flags &= ~LIR_OpArrayCopy::type_check;
 777     }
 778   }
 779 
 780   if (src == dst) {
 781     // moving within a single array so no type checks are needed
 782     if (flags & LIR_OpArrayCopy::type_check) {
 783       flags &= ~LIR_OpArrayCopy::type_check;
 784     }
 785   }
 786   *flagsp = flags;
 787   *expected_typep = (ciArrayKlass*)expected_type;
 788 }
 789 
 790 
 791 LIR_Opr LIRGenerator::round_item(LIR_Opr opr) {
 792   assert(opr->is_register(), "why spill if item is not register?");
 793 
 794   if (RoundFPResults && UseSSE < 1 && opr->is_single_fpu()) {
 795     LIR_Opr result = new_register(T_FLOAT);
 796     set_vreg_flag(result, must_start_in_memory);
 797     assert(opr->is_register(), "only a register can be spilled");
 798     assert(opr->value_type()->is_float(), "rounding only for floats available");
 799     __ roundfp(opr, LIR_OprFact::illegalOpr, result);
 800     return result;
 801   }
 802   return opr;
 803 }
 804 
 805 
 806 LIR_Opr LIRGenerator::force_to_spill(LIR_Opr value, BasicType t) {
 807   assert(type2size[t] == type2size[value->type()], "size mismatch");
 808   if (!value->is_register()) {
 809     // force into a register
 810     LIR_Opr r = new_register(value->type());
 811     __ move(value, r);
 812     value = r;
 813   }
 814 
 815   // create a spill location
 816   LIR_Opr tmp = new_register(t);
 817   set_vreg_flag(tmp, LIRGenerator::must_start_in_memory);
 818 
 819   // move from register to spill
 820   __ move(value, tmp);
 821   return tmp;
 822 }
 823 
 824 
 825 void LIRGenerator::profile_branch(If* if_instr, If::Condition cond) {
 826   if (if_instr->should_profile()) {
 827     ciMethod* method = if_instr->profiled_method();
 828     assert(method != NULL, "method should be set if branch is profiled");
 829     ciMethodData* md = method->method_data();
 830     if (md == NULL) {
 831       bailout("out of memory building methodDataOop");
 832       return;
 833     }
 834     ciProfileData* data = md->bci_to_data(if_instr->profiled_bci());
 835     assert(data != NULL, "must have profiling data");
 836     assert(data->is_BranchData(), "need BranchData for two-way branches");
 837     int taken_count_offset     = md->byte_offset_of_slot(data, BranchData::taken_offset());
 838     int not_taken_count_offset = md->byte_offset_of_slot(data, BranchData::not_taken_offset());
 839     LIR_Opr md_reg = new_register(T_OBJECT);
 840     __ move(LIR_OprFact::oopConst(md->constant_encoding()), md_reg);
 841     LIR_Opr data_offset_reg = new_register(T_INT);
 842     __ cmove(lir_cond(cond),
 843              LIR_OprFact::intConst(taken_count_offset),
 844              LIR_OprFact::intConst(not_taken_count_offset),
 845              data_offset_reg);
 846     LIR_Opr data_reg = new_register(T_INT);
 847     LIR_Address* data_addr = new LIR_Address(md_reg, data_offset_reg, T_INT);
 848     __ move(LIR_OprFact::address(data_addr), data_reg);
 849     LIR_Address* fake_incr_value = new LIR_Address(data_reg, DataLayout::counter_increment, T_INT);
 850     // Use leal instead of add to avoid destroying condition codes on x86
 851     __ leal(LIR_OprFact::address(fake_incr_value), data_reg);
 852     __ move(data_reg, LIR_OprFact::address(data_addr));
 853   }
 854 }
 855 
 856 
 857 // Phi technique:
 858 // This is about passing live values from one basic block to the other.
 859 // In code generated with Java it is rather rare that more than one
 860 // value is on the stack from one basic block to the other.
 861 // We optimize our technique for efficient passing of one value
 862 // (of type long, int, double..) but it can be extended.
 863 // When entering or leaving a basic block, all registers and all spill
 864 // slots are release and empty. We use the released registers
 865 // and spill slots to pass the live values from one block
 866 // to the other. The topmost value, i.e., the value on TOS of expression
 867 // stack is passed in registers. All other values are stored in spilling
 868 // area. Every Phi has an index which designates its spill slot
 869 // At exit of a basic block, we fill the register(s) and spill slots.
 870 // At entry of a basic block, the block_prolog sets up the content of phi nodes
 871 // and locks necessary registers and spilling slots.
 872 
 873 
 874 // move current value to referenced phi function
 875 void LIRGenerator::move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val) {
 876   Phi* phi = sux_val->as_Phi();
 877   // cur_val can be null without phi being null in conjunction with inlining
 878   if (phi != NULL && cur_val != NULL && cur_val != phi && !phi->is_illegal()) {
 879     LIR_Opr operand = cur_val->operand();
 880     if (cur_val->operand()->is_illegal()) {
 881       assert(cur_val->as_Constant() != NULL || cur_val->as_Local() != NULL,
 882              "these can be produced lazily");
 883       operand = operand_for_instruction(cur_val);
 884     }
 885     resolver->move(operand, operand_for_instruction(phi));
 886   }
 887 }
 888 
 889 
 890 // Moves all stack values into their PHI position
 891 void LIRGenerator::move_to_phi(ValueStack* cur_state) {
 892   BlockBegin* bb = block();
 893   if (bb->number_of_sux() == 1) {
 894     BlockBegin* sux = bb->sux_at(0);
 895     assert(sux->number_of_preds() > 0, "invalid CFG");
 896 
 897     // a block with only one predecessor never has phi functions
 898     if (sux->number_of_preds() > 1) {
 899       int max_phis = cur_state->stack_size() + cur_state->locals_size();
 900       PhiResolver resolver(this, _virtual_register_number + max_phis * 2);
 901 
 902       ValueStack* sux_state = sux->state();
 903       Value sux_value;
 904       int index;
 905 
 906       for_each_stack_value(sux_state, index, sux_value) {
 907         move_to_phi(&resolver, cur_state->stack_at(index), sux_value);
 908       }
 909 
 910       // Inlining may cause the local state not to match up, so walk up
 911       // the caller state until we get to the same scope as the
 912       // successor and then start processing from there.
 913       while (cur_state->scope() != sux_state->scope()) {
 914         cur_state = cur_state->caller_state();
 915         assert(cur_state != NULL, "scopes don't match up");
 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() != 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());
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, x->state()->copy_locks());
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   LabelObj* start_store = new LabelObj();
1308 
1309   LIR_PatchCode pre_val_patch_code =
1310     patch ? lir_patch_normal : lir_patch_none;
1311 
1312   LIR_Opr pre_val = new_register(T_OBJECT);
1313 
1314   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
1315   if (!addr_opr->is_address()) {
1316     assert(addr_opr->is_register(), "must be");
1317     addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
1318   }
1319   CodeStub* slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code,
1320                                         info);
1321   __ branch(lir_cond_notEqual, T_INT, slow);
1322   __ branch_destination(slow->continuation());
1323 }
1324 
1325 void LIRGenerator::G1SATBCardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1326   if (G1DisablePostBarrier) return;
1327 
1328   // If the "new_val" is a constant NULL, no barrier is necessary.
1329   if (new_val->is_constant() &&
1330       new_val->as_constant_ptr()->as_jobject() == NULL) return;
1331 
1332   if (!new_val->is_register()) {
1333     LIR_Opr new_val_reg = new_register(T_OBJECT);
1334     if (new_val->is_constant()) {
1335       __ move(new_val, new_val_reg);
1336     } else {
1337       __ leal(new_val, new_val_reg);
1338     }
1339     new_val = new_val_reg;
1340   }
1341   assert(new_val->is_register(), "must be a register at this point");
1342 
1343   if (addr->is_address()) {
1344     LIR_Address* address = addr->as_address_ptr();
1345     LIR_Opr ptr = new_register(T_OBJECT);
1346     if (!address->index()->is_valid() && address->disp() == 0) {
1347       __ move(address->base(), ptr);
1348     } else {
1349       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1350       __ leal(addr, ptr);
1351     }
1352     addr = ptr;
1353   }
1354   assert(addr->is_register(), "must be a register at this point");
1355 
1356   LIR_Opr xor_res = new_pointer_register();
1357   LIR_Opr xor_shift_res = new_pointer_register();
1358   if (TwoOperandLIRForm ) {
1359     __ move(addr, xor_res);
1360     __ logical_xor(xor_res, new_val, xor_res);
1361     __ move(xor_res, xor_shift_res);
1362     __ unsigned_shift_right(xor_shift_res,
1363                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1364                             xor_shift_res,
1365                             LIR_OprDesc::illegalOpr());
1366   } else {
1367     __ logical_xor(addr, new_val, xor_res);
1368     __ unsigned_shift_right(xor_res,
1369                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1370                             xor_shift_res,
1371                             LIR_OprDesc::illegalOpr());
1372   }
1373 
1374   if (!new_val->is_register()) {
1375     LIR_Opr new_val_reg = new_register(T_OBJECT);
1376     __ leal(new_val, new_val_reg);
1377     new_val = new_val_reg;
1378   }
1379   assert(new_val->is_register(), "must be a register at this point");
1380 
1381   __ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));
1382 
1383   CodeStub* slow = new G1PostBarrierStub(addr, new_val);
1384   __ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);
1385   __ branch_destination(slow->continuation());
1386 }
1387 
1388 #endif // SERIALGC
1389 ////////////////////////////////////////////////////////////////////////
1390 
1391 void LIRGenerator::CardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1392 
1393   assert(sizeof(*((CardTableModRefBS*)_bs)->byte_map_base) == sizeof(jbyte), "adjust this code");
1394   LIR_Const* card_table_base = new LIR_Const(((CardTableModRefBS*)_bs)->byte_map_base);
1395   if (addr->is_address()) {
1396     LIR_Address* address = addr->as_address_ptr();
1397     LIR_Opr ptr = new_register(T_OBJECT);
1398     if (!address->index()->is_valid() && address->disp() == 0) {
1399       __ move(address->base(), ptr);
1400     } else {
1401       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1402       __ leal(addr, ptr);
1403     }
1404     addr = ptr;
1405   }
1406   assert(addr->is_register(), "must be a register at this point");
1407 
1408 #ifdef ARM
1409   // TODO: ARM - move to platform-dependent code
1410   LIR_Opr tmp = FrameMap::R14_opr;
1411   if (VM_Version::supports_movw()) {
1412     __ move((LIR_Opr)card_table_base, tmp);
1413   } else {
1414     __ move(new LIR_Address(FrameMap::Rthread_opr, in_bytes(JavaThread::card_table_base_offset()), T_ADDRESS), tmp);
1415   }
1416 
1417   CardTableModRefBS* ct = (CardTableModRefBS*)_bs;
1418   LIR_Address *card_addr = new LIR_Address(tmp, addr, (LIR_Address::Scale) -CardTableModRefBS::card_shift, 0, T_BYTE);
1419   if(((int)ct->byte_map_base & 0xff) == 0) {
1420     __ move(tmp, card_addr);
1421   } else {
1422     LIR_Opr tmp_zero = new_register(T_INT);
1423     __ move(LIR_OprFact::intConst(0), tmp_zero);
1424     __ move(tmp_zero, card_addr);
1425   }
1426 #else // ARM
1427   LIR_Opr tmp = new_pointer_register();
1428   if (TwoOperandLIRForm) {
1429     __ move(addr, tmp);
1430     __ unsigned_shift_right(tmp, CardTableModRefBS::card_shift, tmp);
1431   } else {
1432     __ unsigned_shift_right(addr, CardTableModRefBS::card_shift, tmp);
1433   }
1434   if (can_inline_as_constant(card_table_base)) {
1435     __ move(LIR_OprFact::intConst(0),
1436               new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE));
1437   } else {
1438     __ move(LIR_OprFact::intConst(0),
1439               new LIR_Address(tmp, load_constant(card_table_base),
1440                               T_BYTE));
1441   }
1442 #endif // ARM
1443 }
1444 
1445 
1446 //------------------------field access--------------------------------------
1447 
1448 // Comment copied form templateTable_i486.cpp
1449 // ----------------------------------------------------------------------------
1450 // Volatile variables demand their effects be made known to all CPU's in
1451 // order.  Store buffers on most chips allow reads & writes to reorder; the
1452 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1453 // memory barrier (i.e., it's not sufficient that the interpreter does not
1454 // reorder volatile references, the hardware also must not reorder them).
1455 //
1456 // According to the new Java Memory Model (JMM):
1457 // (1) All volatiles are serialized wrt to each other.
1458 // ALSO reads & writes act as aquire & release, so:
1459 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
1460 // the read float up to before the read.  It's OK for non-volatile memory refs
1461 // that happen before the volatile read to float down below it.
1462 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1463 // that happen BEFORE the write float down to after the write.  It's OK for
1464 // non-volatile memory refs that happen after the volatile write to float up
1465 // before it.
1466 //
1467 // We only put in barriers around volatile refs (they are expensive), not
1468 // _between_ memory refs (that would require us to track the flavor of the
1469 // previous memory refs).  Requirements (2) and (3) require some barriers
1470 // before volatile stores and after volatile loads.  These nearly cover
1471 // requirement (1) but miss the volatile-store-volatile-load case.  This final
1472 // case is placed after volatile-stores although it could just as well go
1473 // before volatile-loads.
1474 
1475 
1476 void LIRGenerator::do_StoreField(StoreField* x) {
1477   bool needs_patching = x->needs_patching();
1478   bool is_volatile = x->field()->is_volatile();
1479   BasicType field_type = x->field_type();
1480   bool is_oop = (field_type == T_ARRAY || field_type == T_OBJECT);
1481 
1482   CodeEmitInfo* info = NULL;
1483   if (needs_patching) {
1484     assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1485     info = state_for(x, x->state_before());
1486   } else if (x->needs_null_check()) {
1487     NullCheck* nc = x->explicit_null_check();
1488     if (nc == NULL) {
1489       info = state_for(x, x->lock_stack());
1490     } else {
1491       info = state_for(nc);
1492     }
1493   }
1494 
1495 
1496   LIRItem object(x->obj(), this);
1497   LIRItem value(x->value(),  this);
1498 
1499   object.load_item();
1500 
1501   if (is_volatile || needs_patching) {
1502     // load item if field is volatile (fewer special cases for volatiles)
1503     // load item if field not initialized
1504     // load item if field not constant
1505     // because of code patching we cannot inline constants
1506     if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1507       value.load_byte_item();
1508     } else  {
1509       value.load_item();
1510     }
1511   } else {
1512     value.load_for_store(field_type);
1513   }
1514 
1515   set_no_result(x);
1516 
1517   if (PrintNotLoaded && needs_patching) {
1518     tty->print_cr("   ###class not loaded at store_%s bci %d",
1519                   x->is_static() ?  "static" : "field", x->bci());
1520   }
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, x->lock_stack());
1584     } else {
1585       info = state_for(nc);
1586     }
1587   }
1588 
1589   LIRItem object(x->obj(), this);
1590 
1591   object.load_item();
1592 
1593   if (PrintNotLoaded && needs_patching) {
1594     tty->print_cr("   ###class not loaded at load_%s bci %d",
1595                   x->is_static() ?  "static" : "field", x->bci());
1596   }
1597 
1598   if (x->needs_null_check() &&
1599       (needs_patching ||
1600        MacroAssembler::needs_explicit_null_check(x->offset()))) {
1601     // emit an explicit null check because the offset is too large
1602     __ null_check(object.result(), new CodeEmitInfo(info));
1603   }
1604 
1605   LIR_Opr reg = rlock_result(x, field_type);
1606   LIR_Address* address;
1607   if (needs_patching) {
1608     // we need to patch the offset in the instruction so don't allow
1609     // generate_address to try to be smart about emitting the -1.
1610     // Otherwise the patching code won't know how to find the
1611     // instruction to patch.
1612     address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);
1613   } else {
1614     address = generate_address(object.result(), x->offset(), field_type);
1615   }
1616 
1617   if (is_volatile) {
1618     assert(!needs_patching && x->is_loaded(),
1619            "how do we know it's volatile if it's not loaded");
1620     volatile_field_load(address, reg, info);
1621   } else {
1622     LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1623     __ load(address, reg, info, patch_code);
1624   }
1625 
1626   if (is_volatile && os::is_MP()) {
1627     __ membar_acquire();
1628   }
1629 }
1630 
1631 
1632 //------------------------java.nio.Buffer.checkIndex------------------------
1633 
1634 // int java.nio.Buffer.checkIndex(int)
1635 void LIRGenerator::do_NIOCheckIndex(Intrinsic* x) {
1636   // NOTE: by the time we are in checkIndex() we are guaranteed that
1637   // the buffer is non-null (because checkIndex is package-private and
1638   // only called from within other methods in the buffer).
1639   assert(x->number_of_arguments() == 2, "wrong type");
1640   LIRItem buf  (x->argument_at(0), this);
1641   LIRItem index(x->argument_at(1), this);
1642   buf.load_item();
1643   index.load_item();
1644 
1645   LIR_Opr result = rlock_result(x);
1646   if (GenerateRangeChecks) {
1647     CodeEmitInfo* info = state_for(x);
1648     CodeStub* stub = new RangeCheckStub(info, index.result(), true);
1649     if (index.result()->is_constant()) {
1650       cmp_mem_int(lir_cond_belowEqual, buf.result(), java_nio_Buffer::limit_offset(), index.result()->as_jint(), info);
1651       __ branch(lir_cond_belowEqual, T_INT, stub);
1652     } else {
1653       cmp_reg_mem(lir_cond_aboveEqual, index.result(), buf.result(),
1654                   java_nio_Buffer::limit_offset(), T_INT, info);
1655       __ branch(lir_cond_aboveEqual, T_INT, stub);
1656     }
1657     __ move(index.result(), result);
1658   } else {
1659     // Just load the index into the result register
1660     __ move(index.result(), result);
1661   }
1662 }
1663 
1664 
1665 //------------------------array access--------------------------------------
1666 
1667 
1668 void LIRGenerator::do_ArrayLength(ArrayLength* x) {
1669   LIRItem array(x->array(), this);
1670   array.load_item();
1671   LIR_Opr reg = rlock_result(x);
1672 
1673   CodeEmitInfo* info = NULL;
1674   if (x->needs_null_check()) {
1675     NullCheck* nc = x->explicit_null_check();
1676     if (nc == NULL) {
1677       info = state_for(x);
1678     } else {
1679       info = state_for(nc);
1680     }
1681   }
1682   __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);
1683 }
1684 
1685 
1686 void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {
1687   bool use_length = x->length() != NULL;
1688   LIRItem array(x->array(), this);
1689   LIRItem index(x->index(), this);
1690   LIRItem length(this);
1691   bool needs_range_check = true;
1692 
1693   if (use_length) {
1694     needs_range_check = x->compute_needs_range_check();
1695     if (needs_range_check) {
1696       length.set_instruction(x->length());
1697       length.load_item();
1698     }
1699   }
1700 
1701   array.load_item();
1702   if (index.is_constant() && can_inline_as_constant(x->index())) {
1703     // let it be a constant
1704     index.dont_load_item();
1705   } else {
1706     index.load_item();
1707   }
1708 
1709   CodeEmitInfo* range_check_info = state_for(x);
1710   CodeEmitInfo* null_check_info = NULL;
1711   if (x->needs_null_check()) {
1712     NullCheck* nc = x->explicit_null_check();
1713     if (nc != NULL) {
1714       null_check_info = state_for(nc);
1715     } else {
1716       null_check_info = range_check_info;
1717     }
1718   }
1719 
1720   // emit array address setup early so it schedules better
1721   LIR_Address* array_addr = emit_array_address(array.result(), index.result(), x->elt_type(), false);
1722 
1723   if (GenerateRangeChecks && needs_range_check) {
1724     if (use_length) {
1725       // TODO: use a (modified) version of array_range_check that does not require a
1726       //       constant length to be loaded to a register
1727       __ cmp(lir_cond_belowEqual, length.result(), index.result());
1728       __ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));
1729     } else {
1730       array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1731       // The range check performs the null check, so clear it out for the load
1732       null_check_info = NULL;
1733     }
1734   }
1735 
1736   __ move(array_addr, rlock_result(x, x->elt_type()), null_check_info);
1737 }
1738 
1739 
1740 void LIRGenerator::do_NullCheck(NullCheck* x) {
1741   if (x->can_trap()) {
1742     LIRItem value(x->obj(), this);
1743     value.load_item();
1744     CodeEmitInfo* info = state_for(x);
1745     __ null_check(value.result(), info);
1746   }
1747 }
1748 
1749 
1750 void LIRGenerator::do_Throw(Throw* x) {
1751   LIRItem exception(x->exception(), this);
1752   exception.load_item();
1753   set_no_result(x);
1754   LIR_Opr exception_opr = exception.result();
1755   CodeEmitInfo* info = state_for(x, x->state());
1756 
1757 #ifndef PRODUCT
1758   if (PrintC1Statistics) {
1759     increment_counter(Runtime1::throw_count_address());
1760   }
1761 #endif
1762 
1763   // check if the instruction has an xhandler in any of the nested scopes
1764   bool unwind = false;
1765   if (info->exception_handlers()->length() == 0) {
1766     // this throw is not inside an xhandler
1767     unwind = true;
1768   } else {
1769     // get some idea of the throw type
1770     bool type_is_exact = true;
1771     ciType* throw_type = x->exception()->exact_type();
1772     if (throw_type == NULL) {
1773       type_is_exact = false;
1774       throw_type = x->exception()->declared_type();
1775     }
1776     if (throw_type != NULL && throw_type->is_instance_klass()) {
1777       ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;
1778       unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);
1779     }
1780   }
1781 
1782   // do null check before moving exception oop into fixed register
1783   // to avoid a fixed interval with an oop during the null check.
1784   // Use a copy of the CodeEmitInfo because debug information is
1785   // different for null_check and throw.
1786   if (GenerateCompilerNullChecks &&
1787       (x->exception()->as_NewInstance() == NULL && x->exception()->as_ExceptionObject() == NULL)) {
1788     // if the exception object wasn't created using new then it might be null.
1789     __ null_check(exception_opr, new CodeEmitInfo(info, true));
1790   }
1791 
1792   if (compilation()->env()->jvmti_can_post_on_exceptions()) {
1793     // we need to go through the exception lookup path to get JVMTI
1794     // notification done
1795     unwind = false;
1796   }
1797 
1798   // move exception oop into fixed register
1799   __ move(exception_opr, exceptionOopOpr());
1800 
1801   if (unwind) {
1802     __ unwind_exception(exceptionOopOpr());
1803   } else {
1804     __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);
1805   }
1806 }
1807 
1808 
1809 void LIRGenerator::do_RoundFP(RoundFP* x) {
1810   LIRItem input(x->input(), this);
1811   input.load_item();
1812   LIR_Opr input_opr = input.result();
1813   assert(input_opr->is_register(), "why round if value is not in a register?");
1814   assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value");
1815   if (input_opr->is_single_fpu()) {
1816     set_result(x, round_item(input_opr)); // This code path not currently taken
1817   } else {
1818     LIR_Opr result = new_register(T_DOUBLE);
1819     set_vreg_flag(result, must_start_in_memory);
1820     __ roundfp(input_opr, LIR_OprFact::illegalOpr, result);
1821     set_result(x, result);
1822   }
1823 }
1824 
1825 void LIRGenerator::do_UnsafeGetRaw(UnsafeGetRaw* x) {
1826   LIRItem base(x->base(), this);
1827   LIRItem idx(this);
1828 
1829   base.load_item();
1830   if (x->has_index()) {
1831     idx.set_instruction(x->index());
1832     idx.load_nonconstant();
1833   }
1834 
1835   LIR_Opr reg = rlock_result(x, x->basic_type());
1836 
1837   int   log2_scale = 0;
1838   if (x->has_index()) {
1839     assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1840     log2_scale = x->log2_scale();
1841   }
1842 
1843   assert(!x->has_index() || idx.value() == x->index(), "should match");
1844 
1845   LIR_Opr base_op = base.result();
1846 #ifndef _LP64
1847   if (x->base()->type()->tag() == longTag) {
1848     base_op = new_register(T_INT);
1849     __ convert(Bytecodes::_l2i, base.result(), base_op);
1850   } else {
1851     assert(x->base()->type()->tag() == intTag, "must be");
1852   }
1853 #endif
1854 
1855   BasicType dst_type = x->basic_type();
1856   LIR_Opr index_op = idx.result();
1857 
1858   LIR_Address* addr;
1859   if (index_op->is_constant()) {
1860     assert(log2_scale == 0, "must not have a scale");
1861     addr = new LIR_Address(base_op, index_op->as_jint(), dst_type);
1862   } else {
1863 #ifdef X86
1864 #ifdef _LP64
1865     if (!index_op->is_illegal() && index_op->type() == T_INT) {
1866       LIR_Opr tmp = new_pointer_register();
1867       __ convert(Bytecodes::_i2l, index_op, tmp);
1868       index_op = tmp;
1869     }
1870 #endif
1871     addr = new LIR_Address(base_op, index_op, LIR_Address::Scale(log2_scale), 0, dst_type);
1872 #elif defined(ARM)
1873     addr = generate_address(base_op, index_op, log2_scale, 0, dst_type);
1874 #else
1875     if (index_op->is_illegal() || log2_scale == 0) {
1876 #ifdef _LP64
1877       if (!index_op->is_illegal() && index_op->type() == T_INT) {
1878         LIR_Opr tmp = new_pointer_register();
1879         __ convert(Bytecodes::_i2l, index_op, tmp);
1880         index_op = tmp;
1881       }
1882 #endif
1883       addr = new LIR_Address(base_op, index_op, dst_type);
1884     } else {
1885       LIR_Opr tmp = new_pointer_register();
1886       __ shift_left(index_op, log2_scale, tmp);
1887       addr = new LIR_Address(base_op, tmp, dst_type);
1888     }
1889 #endif
1890   }
1891 
1892   if (x->may_be_unaligned() && (dst_type == T_LONG || dst_type == T_DOUBLE)) {
1893     __ unaligned_move(addr, reg);
1894   } else {
1895     __ move(addr, reg);
1896   }
1897 }
1898 
1899 
1900 void LIRGenerator::do_UnsafePutRaw(UnsafePutRaw* x) {
1901   int  log2_scale = 0;
1902   BasicType type = x->basic_type();
1903 
1904   if (x->has_index()) {
1905     assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1906     log2_scale = x->log2_scale();
1907   }
1908 
1909   LIRItem base(x->base(), this);
1910   LIRItem value(x->value(), this);
1911   LIRItem idx(this);
1912 
1913   base.load_item();
1914   if (x->has_index()) {
1915     idx.set_instruction(x->index());
1916     idx.load_item();
1917   }
1918 
1919   if (type == T_BYTE || type == T_BOOLEAN) {
1920     value.load_byte_item();
1921   } else {
1922     value.load_item();
1923   }
1924 
1925   set_no_result(x);
1926 
1927   LIR_Opr base_op = base.result();
1928 #ifndef _LP64
1929   if (x->base()->type()->tag() == longTag) {
1930     base_op = new_register(T_INT);
1931     __ convert(Bytecodes::_l2i, base.result(), base_op);
1932   } else {
1933     assert(x->base()->type()->tag() == intTag, "must be");
1934   }
1935 #endif
1936 
1937   LIR_Opr index_op = idx.result();
1938   if (log2_scale != 0) {
1939     // temporary fix (platform dependent code without shift on Intel would be better)
1940     index_op = new_pointer_register();
1941 #ifdef _LP64
1942     if(idx.result()->type() == T_INT) {
1943       __ convert(Bytecodes::_i2l, idx.result(), index_op);
1944     } else {
1945 #endif
1946       // TODO: ARM also allows embedded shift in the address
1947       __ move(idx.result(), index_op);
1948 #ifdef _LP64
1949     }
1950 #endif
1951     __ shift_left(index_op, log2_scale, index_op);
1952   }
1953 #ifdef _LP64
1954   else if(!index_op->is_illegal() && index_op->type() == T_INT) {
1955     LIR_Opr tmp = new_pointer_register();
1956     __ convert(Bytecodes::_i2l, index_op, tmp);
1957     index_op = tmp;
1958   }
1959 #endif
1960 
1961   LIR_Address* addr = new LIR_Address(base_op, index_op, x->basic_type());
1962   __ move(value.result(), addr);
1963 }
1964 
1965 
1966 void LIRGenerator::do_UnsafeGetObject(UnsafeGetObject* x) {
1967   BasicType type = x->basic_type();
1968   LIRItem src(x->object(), this);
1969   LIRItem off(x->offset(), this);
1970 
1971   off.load_item();
1972   src.load_item();
1973 
1974   LIR_Opr reg = reg = rlock_result(x, x->basic_type());
1975 
1976   if (x->is_volatile() && os::is_MP()) __ membar_acquire();
1977   get_Object_unsafe(reg, src.result(), off.result(), type, x->is_volatile());
1978   if (x->is_volatile() && os::is_MP()) __ membar();
1979 }
1980 
1981 
1982 void LIRGenerator::do_UnsafePutObject(UnsafePutObject* x) {
1983   BasicType type = x->basic_type();
1984   LIRItem src(x->object(), this);
1985   LIRItem off(x->offset(), this);
1986   LIRItem data(x->value(), this);
1987 
1988   src.load_item();
1989   if (type == T_BOOLEAN || type == T_BYTE) {
1990     data.load_byte_item();
1991   } else {
1992     data.load_item();
1993   }
1994   off.load_item();
1995 
1996   set_no_result(x);
1997 
1998   if (x->is_volatile() && os::is_MP()) __ membar_release();
1999   put_Object_unsafe(src.result(), off.result(), data.result(), type, x->is_volatile());
2000 }
2001 
2002 
2003 void LIRGenerator::do_UnsafePrefetch(UnsafePrefetch* x, bool is_store) {
2004   LIRItem src(x->object(), this);
2005   LIRItem off(x->offset(), this);
2006 
2007   src.load_item();
2008   if (off.is_constant() && can_inline_as_constant(x->offset())) {
2009     // let it be a constant
2010     off.dont_load_item();
2011   } else {
2012     off.load_item();
2013   }
2014 
2015   set_no_result(x);
2016 
2017   LIR_Address* addr = generate_address(src.result(), off.result(), 0, 0, T_BYTE);
2018   __ prefetch(addr, is_store);
2019 }
2020 
2021 
2022 void LIRGenerator::do_UnsafePrefetchRead(UnsafePrefetchRead* x) {
2023   do_UnsafePrefetch(x, false);
2024 }
2025 
2026 
2027 void LIRGenerator::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {
2028   do_UnsafePrefetch(x, true);
2029 }
2030 
2031 
2032 void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {
2033   int lng = x->length();
2034 
2035   for (int i = 0; i < lng; i++) {
2036     SwitchRange* one_range = x->at(i);
2037     int low_key = one_range->low_key();
2038     int high_key = one_range->high_key();
2039     BlockBegin* dest = one_range->sux();
2040     if (low_key == high_key) {
2041       __ cmp(lir_cond_equal, value, low_key);
2042       __ branch(lir_cond_equal, T_INT, dest);
2043     } else if (high_key - low_key == 1) {
2044       __ cmp(lir_cond_equal, value, low_key);
2045       __ branch(lir_cond_equal, T_INT, dest);
2046       __ cmp(lir_cond_equal, value, high_key);
2047       __ branch(lir_cond_equal, T_INT, dest);
2048     } else {
2049       LabelObj* L = new LabelObj();
2050       __ cmp(lir_cond_less, value, low_key);
2051       __ branch(lir_cond_less, L->label());
2052       __ cmp(lir_cond_lessEqual, value, high_key);
2053       __ branch(lir_cond_lessEqual, T_INT, dest);
2054       __ branch_destination(L->label());
2055     }
2056   }
2057   __ jump(default_sux);
2058 }
2059 
2060 
2061 SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {
2062   SwitchRangeList* res = new SwitchRangeList();
2063   int len = x->length();
2064   if (len > 0) {
2065     BlockBegin* sux = x->sux_at(0);
2066     int key = x->lo_key();
2067     BlockBegin* default_sux = x->default_sux();
2068     SwitchRange* range = new SwitchRange(key, sux);
2069     for (int i = 0; i < len; i++, key++) {
2070       BlockBegin* new_sux = x->sux_at(i);
2071       if (sux == new_sux) {
2072         // still in same range
2073         range->set_high_key(key);
2074       } else {
2075         // skip tests which explicitly dispatch to the default
2076         if (sux != default_sux) {
2077           res->append(range);
2078         }
2079         range = new SwitchRange(key, new_sux);
2080       }
2081       sux = new_sux;
2082     }
2083     if (res->length() == 0 || res->last() != range)  res->append(range);
2084   }
2085   return res;
2086 }
2087 
2088 
2089 // we expect the keys to be sorted by increasing value
2090 SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {
2091   SwitchRangeList* res = new SwitchRangeList();
2092   int len = x->length();
2093   if (len > 0) {
2094     BlockBegin* default_sux = x->default_sux();
2095     int key = x->key_at(0);
2096     BlockBegin* sux = x->sux_at(0);
2097     SwitchRange* range = new SwitchRange(key, sux);
2098     for (int i = 1; i < len; i++) {
2099       int new_key = x->key_at(i);
2100       BlockBegin* new_sux = x->sux_at(i);
2101       if (key+1 == new_key && sux == new_sux) {
2102         // still in same range
2103         range->set_high_key(new_key);
2104       } else {
2105         // skip tests which explicitly dispatch to the default
2106         if (range->sux() != default_sux) {
2107           res->append(range);
2108         }
2109         range = new SwitchRange(new_key, new_sux);
2110       }
2111       key = new_key;
2112       sux = new_sux;
2113     }
2114     if (res->length() == 0 || res->last() != range)  res->append(range);
2115   }
2116   return res;
2117 }
2118 
2119 
2120 void LIRGenerator::do_TableSwitch(TableSwitch* x) {
2121   LIRItem tag(x->tag(), this);
2122   tag.load_item();
2123   set_no_result(x);
2124 
2125   if (x->is_safepoint()) {
2126     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2127   }
2128 
2129   // move values into phi locations
2130   move_to_phi(x->state());
2131 
2132   int lo_key = x->lo_key();
2133   int hi_key = x->hi_key();
2134   int len = x->length();
2135   CodeEmitInfo* info = state_for(x, x->state());
2136   LIR_Opr value = tag.result();
2137   if (UseTableRanges) {
2138     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2139   } else {
2140     for (int i = 0; i < len; i++) {
2141       __ cmp(lir_cond_equal, value, i + lo_key);
2142       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2143     }
2144     __ jump(x->default_sux());
2145   }
2146 }
2147 
2148 
2149 void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {
2150   LIRItem tag(x->tag(), this);
2151   tag.load_item();
2152   set_no_result(x);
2153 
2154   if (x->is_safepoint()) {
2155     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2156   }
2157 
2158   // move values into phi locations
2159   move_to_phi(x->state());
2160 
2161   LIR_Opr value = tag.result();
2162   if (UseTableRanges) {
2163     do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2164   } else {
2165     int len = x->length();
2166     for (int i = 0; i < len; i++) {
2167       __ cmp(lir_cond_equal, value, x->key_at(i));
2168       __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2169     }
2170     __ jump(x->default_sux());
2171   }
2172 }
2173 
2174 
2175 void LIRGenerator::do_Goto(Goto* x) {
2176   set_no_result(x);
2177 
2178   if (block()->next()->as_OsrEntry()) {
2179     // need to free up storage used for OSR entry point
2180     LIR_Opr osrBuffer = block()->next()->operand();
2181     BasicTypeList signature;
2182     signature.append(T_INT);
2183     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2184     __ move(osrBuffer, cc->args()->at(0));
2185     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
2186                          getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());
2187   }
2188 
2189   if (x->is_safepoint()) {
2190     ValueStack* state = x->state_before() ? x->state_before() : x->state();
2191 
2192     // increment backedge counter if needed
2193     increment_backedge_counter(state_for(x, state));
2194 
2195     CodeEmitInfo* safepoint_info = state_for(x, state);
2196     __ safepoint(safepoint_poll_register(), safepoint_info);
2197   }
2198 
2199   // emit phi-instruction move after safepoint since this simplifies
2200   // describing the state as the safepoint.
2201   move_to_phi(x->state());
2202 
2203   __ jump(x->default_sux());
2204 }
2205 
2206 
2207 void LIRGenerator::do_Base(Base* x) {
2208   __ std_entry(LIR_OprFact::illegalOpr);
2209   // Emit moves from physical registers / stack slots to virtual registers
2210   CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2211   IRScope* irScope = compilation()->hir()->top_scope();
2212   int java_index = 0;
2213   for (int i = 0; i < args->length(); i++) {
2214     LIR_Opr src = args->at(i);
2215     assert(!src->is_illegal(), "check");
2216     BasicType t = src->type();
2217 
2218     // Types which are smaller than int are passed as int, so
2219     // correct the type which passed.
2220     switch (t) {
2221     case T_BYTE:
2222     case T_BOOLEAN:
2223     case T_SHORT:
2224     case T_CHAR:
2225       t = T_INT;
2226       break;
2227     }
2228 
2229     LIR_Opr dest = new_register(t);
2230     __ move(src, dest);
2231 
2232     // Assign new location to Local instruction for this local
2233     Local* local = x->state()->local_at(java_index)->as_Local();
2234     assert(local != NULL, "Locals for incoming arguments must have been created");
2235 #ifndef __SOFTFP__
2236     // The java calling convention passes double as long and float as int.
2237     assert(as_ValueType(t)->tag() == local->type()->tag(), "check");
2238 #endif // __SOFTFP__
2239     local->set_operand(dest);
2240     _instruction_for_operand.at_put_grow(dest->vreg_number(), local, NULL);
2241     java_index += type2size[t];
2242   }
2243 
2244   if (compilation()->env()->dtrace_method_probes()) {
2245     BasicTypeList signature;
2246     signature.append(T_INT);    // thread
2247     signature.append(T_OBJECT); // methodOop
2248     LIR_OprList* args = new LIR_OprList();
2249     args->append(getThreadPointer());
2250     LIR_Opr meth = new_register(T_OBJECT);
2251     __ oop2reg(method()->constant_encoding(), meth);
2252     args->append(meth);
2253     call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, NULL);
2254   }
2255 
2256   if (method()->is_synchronized()) {
2257     LIR_Opr obj;
2258     if (method()->is_static()) {
2259       obj = new_register(T_OBJECT);
2260       __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);
2261     } else {
2262       Local* receiver = x->state()->local_at(0)->as_Local();
2263       assert(receiver != NULL, "must already exist");
2264       obj = receiver->operand();
2265     }
2266     assert(obj->is_valid(), "must be valid");
2267 
2268     if (method()->is_synchronized() && GenerateSynchronizationCode) {
2269       LIR_Opr lock = new_register(T_INT);
2270       __ load_stack_address_monitor(0, lock);
2271 
2272       CodeEmitInfo* info = new CodeEmitInfo(SynchronizationEntryBCI, scope()->start()->state(), NULL);
2273       CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
2274 
2275       // receiver is guaranteed non-NULL so don't need CodeEmitInfo
2276       __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, NULL);
2277     }
2278   }
2279 
2280   // increment invocation counters if needed
2281   increment_invocation_counter(new CodeEmitInfo(0, scope()->start()->state(), NULL));
2282 
2283   // all blocks with a successor must end with an unconditional jump
2284   // to the successor even if they are consecutive
2285   __ jump(x->default_sux());
2286 }
2287 
2288 
2289 void LIRGenerator::do_OsrEntry(OsrEntry* x) {
2290   // construct our frame and model the production of incoming pointer
2291   // to the OSR buffer.
2292   __ osr_entry(LIR_Assembler::osrBufferPointer());
2293   LIR_Opr result = rlock_result(x);
2294   __ move(LIR_Assembler::osrBufferPointer(), result);
2295 }
2296 
2297 
2298 void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
2299   int i = (x->has_receiver() || x->is_invokedynamic()) ? 1 : 0;
2300   for (; i < args->length(); i++) {
2301     LIRItem* param = args->at(i);
2302     LIR_Opr loc = arg_list->at(i);
2303     if (loc->is_register()) {
2304       param->load_item_force(loc);
2305     } else {
2306       LIR_Address* addr = loc->as_address_ptr();
2307       param->load_for_store(addr->type());
2308       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2309         __ unaligned_move(param->result(), addr);
2310       } else {
2311         __ move(param->result(), addr);
2312       }
2313     }
2314   }
2315 
2316   if (x->has_receiver()) {
2317     LIRItem* receiver = args->at(0);
2318     LIR_Opr loc = arg_list->at(0);
2319     if (loc->is_register()) {
2320       receiver->load_item_force(loc);
2321     } else {
2322       assert(loc->is_address(), "just checking");
2323       receiver->load_for_store(T_OBJECT);
2324       __ move(receiver->result(), loc);
2325     }
2326   }
2327 }
2328 
2329 
2330 // Visits all arguments, returns appropriate items without loading them
2331 LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
2332   LIRItemList* argument_items = new LIRItemList();
2333   if (x->has_receiver()) {
2334     LIRItem* receiver = new LIRItem(x->receiver(), this);
2335     argument_items->append(receiver);
2336   }
2337   if (x->is_invokedynamic()) {
2338     // Insert a dummy for the synthetic MethodHandle argument.
2339     argument_items->append(NULL);
2340   }
2341   int idx = x->has_receiver() ? 1 : 0;
2342   for (int i = 0; i < x->number_of_arguments(); i++) {
2343     LIRItem* param = new LIRItem(x->argument_at(i), this);
2344     argument_items->append(param);
2345     idx += (param->type()->is_double_word() ? 2 : 1);
2346   }
2347   return argument_items;
2348 }
2349 
2350 
2351 // The invoke with receiver has following phases:
2352 //   a) traverse and load/lock receiver;
2353 //   b) traverse all arguments -> item-array (invoke_visit_argument)
2354 //   c) push receiver on stack
2355 //   d) load each of the items and push on stack
2356 //   e) unlock receiver
2357 //   f) move receiver into receiver-register %o0
2358 //   g) lock result registers and emit call operation
2359 //
2360 // Before issuing a call, we must spill-save all values on stack
2361 // that are in caller-save register. "spill-save" moves thos registers
2362 // either in a free callee-save register or spills them if no free
2363 // callee save register is available.
2364 //
2365 // The problem is where to invoke spill-save.
2366 // - if invoked between e) and f), we may lock callee save
2367 //   register in "spill-save" that destroys the receiver register
2368 //   before f) is executed
2369 // - if we rearange the f) to be earlier, by loading %o0, it
2370 //   may destroy a value on the stack that is currently in %o0
2371 //   and is waiting to be spilled
2372 // - if we keep the receiver locked while doing spill-save,
2373 //   we cannot spill it as it is spill-locked
2374 //
2375 void LIRGenerator::do_Invoke(Invoke* x) {
2376   CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);
2377 
2378   LIR_OprList* arg_list = cc->args();
2379   LIRItemList* args = invoke_visit_arguments(x);
2380   LIR_Opr receiver = LIR_OprFact::illegalOpr;
2381 
2382   // setup result register
2383   LIR_Opr result_register = LIR_OprFact::illegalOpr;
2384   if (x->type() != voidType) {
2385     result_register = result_register_for(x->type());
2386   }
2387 
2388   CodeEmitInfo* info = state_for(x, x->state());
2389 
2390   // invokedynamics can deoptimize.
2391   CodeEmitInfo* deopt_info = x->is_invokedynamic() ? state_for(x, x->state_before()) : NULL;
2392 
2393   invoke_load_arguments(x, args, arg_list);
2394 
2395   if (x->has_receiver()) {
2396     args->at(0)->load_item_force(LIR_Assembler::receiverOpr());
2397     receiver = args->at(0)->result();
2398   }
2399 
2400   // emit invoke code
2401   bool optimized = x->target_is_loaded() && x->target_is_final();
2402   assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");
2403 
2404   // JSR 292
2405   // Preserve the SP over MethodHandle call sites.
2406   ciMethod* target = x->target();
2407   if (target->is_method_handle_invoke()) {
2408     info->set_is_method_handle_invoke(true);
2409     __ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr());
2410   }
2411 
2412   switch (x->code()) {
2413     case Bytecodes::_invokestatic:
2414       __ call_static(target, result_register,
2415                      SharedRuntime::get_resolve_static_call_stub(),
2416                      arg_list, info);
2417       break;
2418     case Bytecodes::_invokespecial:
2419     case Bytecodes::_invokevirtual:
2420     case Bytecodes::_invokeinterface:
2421       // for final target we still produce an inline cache, in order
2422       // to be able to call mixed mode
2423       if (x->code() == Bytecodes::_invokespecial || optimized) {
2424         __ call_opt_virtual(target, receiver, result_register,
2425                             SharedRuntime::get_resolve_opt_virtual_call_stub(),
2426                             arg_list, info);
2427       } else if (x->vtable_index() < 0) {
2428         __ call_icvirtual(target, receiver, result_register,
2429                           SharedRuntime::get_resolve_virtual_call_stub(),
2430                           arg_list, info);
2431       } else {
2432         int entry_offset = instanceKlass::vtable_start_offset() + x->vtable_index() * vtableEntry::size();
2433         int vtable_offset = entry_offset * wordSize + vtableEntry::method_offset_in_bytes();
2434         __ call_virtual(target, receiver, result_register, vtable_offset, arg_list, info);
2435       }
2436       break;
2437     case Bytecodes::_invokedynamic: {
2438       ciBytecodeStream bcs(x->scope()->method());
2439       bcs.force_bci(x->bci());
2440       assert(bcs.cur_bc() == Bytecodes::_invokedynamic, "wrong stream");
2441       ciCPCache* cpcache = bcs.get_cpcache();
2442 
2443       // Get CallSite offset from constant pool cache pointer.
2444       int index = bcs.get_method_index();
2445       size_t call_site_offset = cpcache->get_f1_offset(index);
2446 
2447       // If this invokedynamic call site hasn't been executed yet in
2448       // the interpreter, the CallSite object in the constant pool
2449       // cache is still null and we need to deoptimize.
2450       if (cpcache->is_f1_null_at(index)) {
2451         // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
2452         // clone all handlers.  This is handled transparently in other
2453         // places by the CodeEmitInfo cloning logic but is handled
2454         // specially here because a stub isn't being used.
2455         x->set_exception_handlers(new XHandlers(x->exception_handlers()));
2456 
2457         DeoptimizeStub* deopt_stub = new DeoptimizeStub(deopt_info);
2458         __ jump(deopt_stub);
2459       }
2460 
2461       // Use the receiver register for the synthetic MethodHandle
2462       // argument.
2463       receiver = LIR_Assembler::receiverOpr();
2464       LIR_Opr tmp = new_register(objectType);
2465 
2466       // Load CallSite object from constant pool cache.
2467       __ oop2reg(cpcache->constant_encoding(), tmp);
2468       __ load(new LIR_Address(tmp, call_site_offset, T_OBJECT), tmp);
2469 
2470       // Load target MethodHandle from CallSite object.
2471       __ load(new LIR_Address(tmp, java_dyn_CallSite::target_offset_in_bytes(), T_OBJECT), receiver);
2472 
2473       __ call_dynamic(target, receiver, result_register,
2474                       SharedRuntime::get_resolve_opt_virtual_call_stub(),
2475                       arg_list, info);
2476       break;
2477     }
2478     default:
2479       ShouldNotReachHere();
2480       break;
2481   }
2482 
2483   // JSR 292
2484   // Restore the SP after MethodHandle call sites.
2485   if (target->is_method_handle_invoke()) {
2486     __ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer());
2487   }
2488 
2489   if (x->type()->is_float() || x->type()->is_double()) {
2490     // Force rounding of results from non-strictfp when in strictfp
2491     // scope (or when we don't know the strictness of the callee, to
2492     // be safe.)
2493     if (method()->is_strict()) {
2494       if (!x->target_is_loaded() || !x->target_is_strictfp()) {
2495         result_register = round_item(result_register);
2496       }
2497     }
2498   }
2499 
2500   if (result_register->is_valid()) {
2501     LIR_Opr result = rlock_result(x);
2502     __ move(result_register, result);
2503   }
2504 }
2505 
2506 
2507 void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {
2508   assert(x->number_of_arguments() == 1, "wrong type");
2509   LIRItem value       (x->argument_at(0), this);
2510   LIR_Opr reg = rlock_result(x);
2511   value.load_item();
2512   LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));
2513   __ move(tmp, reg);
2514 }
2515 
2516 
2517 
2518 // Code for  :  x->x() {x->cond()} x->y() ? x->tval() : x->fval()
2519 void LIRGenerator::do_IfOp(IfOp* x) {
2520 #ifdef ASSERT
2521   {
2522     ValueTag xtag = x->x()->type()->tag();
2523     ValueTag ttag = x->tval()->type()->tag();
2524     assert(xtag == intTag || xtag == objectTag, "cannot handle others");
2525     assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
2526     assert(ttag == x->fval()->type()->tag(), "cannot handle others");
2527   }
2528 #endif
2529 
2530   LIRItem left(x->x(), this);
2531   LIRItem right(x->y(), this);
2532   left.load_item();
2533   if (can_inline_as_constant(right.value())) {
2534     right.dont_load_item();
2535   } else {
2536     right.load_item();
2537   }
2538 
2539   LIRItem t_val(x->tval(), this);
2540   LIRItem f_val(x->fval(), this);
2541   t_val.dont_load_item();
2542   f_val.dont_load_item();
2543   LIR_Opr reg = rlock_result(x);
2544 
2545   __ cmp(lir_cond(x->cond()), left.result(), right.result());
2546   __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg);
2547 }
2548 
2549 
2550 void LIRGenerator::do_Intrinsic(Intrinsic* x) {
2551   switch (x->id()) {
2552   case vmIntrinsics::_intBitsToFloat      :
2553   case vmIntrinsics::_doubleToRawLongBits :
2554   case vmIntrinsics::_longBitsToDouble    :
2555   case vmIntrinsics::_floatToRawIntBits   : {
2556     do_FPIntrinsics(x);
2557     break;
2558   }
2559 
2560   case vmIntrinsics::_currentTimeMillis: {
2561     assert(x->number_of_arguments() == 0, "wrong type");
2562     LIR_Opr reg = result_register_for(x->type());
2563     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeMillis), getThreadTemp(),
2564                          reg, new LIR_OprList());
2565     LIR_Opr result = rlock_result(x);
2566     __ move(reg, result);
2567     break;
2568   }
2569 
2570   case vmIntrinsics::_nanoTime: {
2571     assert(x->number_of_arguments() == 0, "wrong type");
2572     LIR_Opr reg = result_register_for(x->type());
2573     __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeNanos), getThreadTemp(),
2574                          reg, new LIR_OprList());
2575     LIR_Opr result = rlock_result(x);
2576     __ move(reg, result);
2577     break;
2578   }
2579 
2580   case vmIntrinsics::_Object_init:    do_RegisterFinalizer(x); break;
2581   case vmIntrinsics::_getClass:       do_getClass(x);      break;
2582   case vmIntrinsics::_currentThread:  do_currentThread(x); break;
2583 
2584   case vmIntrinsics::_dlog:           // fall through
2585   case vmIntrinsics::_dlog10:         // fall through
2586   case vmIntrinsics::_dabs:           // fall through
2587   case vmIntrinsics::_dsqrt:          // fall through
2588   case vmIntrinsics::_dtan:           // fall through
2589   case vmIntrinsics::_dsin :          // fall through
2590   case vmIntrinsics::_dcos :          do_MathIntrinsic(x); break;
2591   case vmIntrinsics::_arraycopy:      do_ArrayCopy(x);     break;
2592 
2593   // java.nio.Buffer.checkIndex
2594   case vmIntrinsics::_checkIndex:     do_NIOCheckIndex(x); break;
2595 
2596   case vmIntrinsics::_compareAndSwapObject:
2597     do_CompareAndSwap(x, objectType);
2598     break;
2599   case vmIntrinsics::_compareAndSwapInt:
2600     do_CompareAndSwap(x, intType);
2601     break;
2602   case vmIntrinsics::_compareAndSwapLong:
2603     do_CompareAndSwap(x, longType);
2604     break;
2605 
2606     // sun.misc.AtomicLongCSImpl.attemptUpdate
2607   case vmIntrinsics::_attemptUpdate:
2608     do_AttemptUpdate(x);
2609     break;
2610 
2611   default: ShouldNotReachHere(); break;
2612   }
2613 }
2614 
2615 
2616 void LIRGenerator::do_ProfileCall(ProfileCall* x) {
2617   // Need recv in a temporary register so it interferes with the other temporaries
2618   LIR_Opr recv = LIR_OprFact::illegalOpr;
2619   LIR_Opr mdo = new_register(T_OBJECT);
2620   LIR_Opr tmp = new_register(T_INT);
2621   if (x->recv() != NULL) {
2622     LIRItem value(x->recv(), this);
2623     value.load_item();
2624     recv = new_register(T_OBJECT);
2625     __ move(value.result(), recv);
2626   }
2627   __ profile_call(x->method(), x->bci_of_invoke(), mdo, recv, tmp, x->known_holder());
2628 }
2629 
2630 
2631 void LIRGenerator::do_ProfileCounter(ProfileCounter* x) {
2632   LIRItem mdo(x->mdo(), this);
2633   mdo.load_item();
2634 
2635   increment_counter(new LIR_Address(mdo.result(), x->offset(), T_INT), x->increment());
2636 }
2637 
2638 
2639 LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
2640   LIRItemList args(1);
2641   LIRItem value(arg1, this);
2642   args.append(&value);
2643   BasicTypeList signature;
2644   signature.append(as_BasicType(arg1->type()));
2645 
2646   return call_runtime(&signature, &args, entry, result_type, info);
2647 }
2648 
2649 
2650 LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {
2651   LIRItemList args(2);
2652   LIRItem value1(arg1, this);
2653   LIRItem value2(arg2, this);
2654   args.append(&value1);
2655   args.append(&value2);
2656   BasicTypeList signature;
2657   signature.append(as_BasicType(arg1->type()));
2658   signature.append(as_BasicType(arg2->type()));
2659 
2660   return call_runtime(&signature, &args, entry, result_type, info);
2661 }
2662 
2663 
2664 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,
2665                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
2666   // get a result register
2667   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2668   LIR_Opr result = LIR_OprFact::illegalOpr;
2669   if (result_type->tag() != voidTag) {
2670     result = new_register(result_type);
2671     phys_reg = result_register_for(result_type);
2672   }
2673 
2674   // move the arguments into the correct location
2675   CallingConvention* cc = frame_map()->c_calling_convention(signature);
2676   assert(cc->length() == args->length(), "argument mismatch");
2677   for (int i = 0; i < args->length(); i++) {
2678     LIR_Opr arg = args->at(i);
2679     LIR_Opr loc = cc->at(i);
2680     if (loc->is_register()) {
2681       __ move(arg, loc);
2682     } else {
2683       LIR_Address* addr = loc->as_address_ptr();
2684 //           if (!can_store_as_constant(arg)) {
2685 //             LIR_Opr tmp = new_register(arg->type());
2686 //             __ move(arg, tmp);
2687 //             arg = tmp;
2688 //           }
2689       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2690         __ unaligned_move(arg, addr);
2691       } else {
2692         __ move(arg, addr);
2693       }
2694     }
2695   }
2696 
2697   if (info) {
2698     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2699   } else {
2700     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2701   }
2702   if (result->is_valid()) {
2703     __ move(phys_reg, result);
2704   }
2705   return result;
2706 }
2707 
2708 
2709 LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,
2710                                    address entry, ValueType* result_type, CodeEmitInfo* info) {
2711   // get a result register
2712   LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2713   LIR_Opr result = LIR_OprFact::illegalOpr;
2714   if (result_type->tag() != voidTag) {
2715     result = new_register(result_type);
2716     phys_reg = result_register_for(result_type);
2717   }
2718 
2719   // move the arguments into the correct location
2720   CallingConvention* cc = frame_map()->c_calling_convention(signature);
2721 
2722   assert(cc->length() == args->length(), "argument mismatch");
2723   for (int i = 0; i < args->length(); i++) {
2724     LIRItem* arg = args->at(i);
2725     LIR_Opr loc = cc->at(i);
2726     if (loc->is_register()) {
2727       arg->load_item_force(loc);
2728     } else {
2729       LIR_Address* addr = loc->as_address_ptr();
2730       arg->load_for_store(addr->type());
2731       if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2732         __ unaligned_move(arg->result(), addr);
2733       } else {
2734         __ move(arg->result(), addr);
2735       }
2736     }
2737   }
2738 
2739   if (info) {
2740     __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2741   } else {
2742     __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2743   }
2744   if (result->is_valid()) {
2745     __ move(phys_reg, result);
2746   }
2747   return result;
2748 }
2749 
2750 
2751 
2752 void LIRGenerator::increment_invocation_counter(CodeEmitInfo* info, bool backedge) {
2753 #ifdef TIERED
2754   if (_compilation->env()->comp_level() == CompLevel_fast_compile &&
2755       (method()->code_size() >= Tier1BytecodeLimit || backedge)) {
2756     int limit = InvocationCounter::Tier1InvocationLimit;
2757     int offset = in_bytes(methodOopDesc::invocation_counter_offset() +
2758                           InvocationCounter::counter_offset());
2759     if (backedge) {
2760       limit = InvocationCounter::Tier1BackEdgeLimit;
2761       offset = in_bytes(methodOopDesc::backedge_counter_offset() +
2762                         InvocationCounter::counter_offset());
2763     }
2764 
2765     LIR_Opr meth = new_register(T_OBJECT);
2766     __ oop2reg(method()->constant_encoding(), meth);
2767     LIR_Opr result = increment_and_return_counter(meth, offset, InvocationCounter::count_increment);
2768     __ cmp(lir_cond_aboveEqual, result, LIR_OprFact::intConst(limit));
2769     CodeStub* overflow = new CounterOverflowStub(info, info->bci());
2770     __ branch(lir_cond_aboveEqual, T_INT, overflow);
2771     __ branch_destination(overflow->continuation());
2772   }
2773 #endif
2774 }