c1_LIRGenerator.cpp revision 1601:126ea7725993
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
41void 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
66PhiResolver::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
76void 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
83void 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
90void 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
104void 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
130PhiResolver::~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
153ResolveNode* 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
177void 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
190void 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
201void 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
218void 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
231void 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
245ciObject* 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
254jint 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
261jint 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
268jfloat 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
275jdouble 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
282jlong 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
293void LIRGenerator::init() {
294  _bs = Universe::heap()->barrier_set();
295}
296
297
298void 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
321void 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
341void 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;
359void 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
375void 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
388CodeEmitInfo* 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
438CodeEmitInfo* LIRGenerator::state_for(Instruction* x) {
439  return state_for(x, x->lock_stack());
440}
441
442
443void 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
454void 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
469void 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
484LIR_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
494void 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
570void 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
575void 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
580void 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
585void 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
605void 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
627void 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
637void 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
648void 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
672static 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
681static 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
690static 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
698void 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
791LIR_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
806LIR_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
825void 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
875void 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
891void 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
928LIR_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  if (type == T_ADDRESS) type = T_INT;
943  return LIR_OprFact::virtual_register(vreg, type);
944}
945
946
947// Try to lock using register in hint
948LIR_Opr LIRGenerator::rlock(Value instr) {
949  return new_register(instr->type());
950}
951
952
953// does an rlock and sets result
954LIR_Opr LIRGenerator::rlock_result(Value x) {
955  LIR_Opr reg = rlock(x);
956  set_result(x, reg);
957  return reg;
958}
959
960
961// does an rlock and sets result
962LIR_Opr LIRGenerator::rlock_result(Value x, BasicType type) {
963  LIR_Opr reg;
964  switch (type) {
965  case T_BYTE:
966  case T_BOOLEAN:
967    reg = rlock_byte(type);
968    break;
969  default:
970    reg = rlock(x);
971    break;
972  }
973
974  set_result(x, reg);
975  return reg;
976}
977
978
979//---------------------------------------------------------------------
980ciObject* LIRGenerator::get_jobject_constant(Value value) {
981  ObjectType* oc = value->type()->as_ObjectType();
982  if (oc) {
983    return oc->constant_value();
984  }
985  return NULL;
986}
987
988
989void LIRGenerator::do_ExceptionObject(ExceptionObject* x) {
990  assert(block()->is_set(BlockBegin::exception_entry_flag), "ExceptionObject only allowed in exception handler block");
991  assert(block()->next() == x, "ExceptionObject must be first instruction of block");
992
993  // no moves are created for phi functions at the begin of exception
994  // handlers, so assign operands manually here
995  for_each_phi_fun(block(), phi,
996                   operand_for_instruction(phi));
997
998  LIR_Opr thread_reg = getThreadPointer();
999  __ move(new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT),
1000          exceptionOopOpr());
1001  __ move(LIR_OprFact::oopConst(NULL),
1002          new LIR_Address(thread_reg, in_bytes(JavaThread::exception_oop_offset()), T_OBJECT));
1003  __ move(LIR_OprFact::oopConst(NULL),
1004          new LIR_Address(thread_reg, in_bytes(JavaThread::exception_pc_offset()), T_OBJECT));
1005
1006  LIR_Opr result = new_register(T_OBJECT);
1007  __ move(exceptionOopOpr(), result);
1008  set_result(x, result);
1009}
1010
1011
1012//----------------------------------------------------------------------
1013//----------------------------------------------------------------------
1014//----------------------------------------------------------------------
1015//----------------------------------------------------------------------
1016//                        visitor functions
1017//----------------------------------------------------------------------
1018//----------------------------------------------------------------------
1019//----------------------------------------------------------------------
1020//----------------------------------------------------------------------
1021
1022void LIRGenerator::do_Phi(Phi* x) {
1023  // phi functions are never visited directly
1024  ShouldNotReachHere();
1025}
1026
1027
1028// Code for a constant is generated lazily unless the constant is frequently used and can't be inlined.
1029void LIRGenerator::do_Constant(Constant* x) {
1030  if (x->state() != NULL) {
1031    // Any constant with a ValueStack requires patching so emit the patch here
1032    LIR_Opr reg = rlock_result(x);
1033    CodeEmitInfo* info = state_for(x, x->state());
1034    __ oop2reg_patch(NULL, reg, info);
1035  } else if (x->use_count() > 1 && !can_inline_as_constant(x)) {
1036    if (!x->is_pinned()) {
1037      // unpinned constants are handled specially so that they can be
1038      // put into registers when they are used multiple times within a
1039      // block.  After the block completes their operand will be
1040      // cleared so that other blocks can't refer to that register.
1041      set_result(x, load_constant(x));
1042    } else {
1043      LIR_Opr res = x->operand();
1044      if (!res->is_valid()) {
1045        res = LIR_OprFact::value_type(x->type());
1046      }
1047      if (res->is_constant()) {
1048        LIR_Opr reg = rlock_result(x);
1049        __ move(res, reg);
1050      } else {
1051        set_result(x, res);
1052      }
1053    }
1054  } else {
1055    set_result(x, LIR_OprFact::value_type(x->type()));
1056  }
1057}
1058
1059
1060void LIRGenerator::do_Local(Local* x) {
1061  // operand_for_instruction has the side effect of setting the result
1062  // so there's no need to do it here.
1063  operand_for_instruction(x);
1064}
1065
1066
1067void LIRGenerator::do_IfInstanceOf(IfInstanceOf* x) {
1068  Unimplemented();
1069}
1070
1071
1072void LIRGenerator::do_Return(Return* x) {
1073  if (compilation()->env()->dtrace_method_probes()) {
1074    BasicTypeList signature;
1075    signature.append(T_INT);    // thread
1076    signature.append(T_OBJECT); // methodOop
1077    LIR_OprList* args = new LIR_OprList();
1078    args->append(getThreadPointer());
1079    LIR_Opr meth = new_register(T_OBJECT);
1080    __ oop2reg(method()->constant_encoding(), meth);
1081    args->append(meth);
1082    call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), voidType, NULL);
1083  }
1084
1085  if (x->type()->is_void()) {
1086    __ return_op(LIR_OprFact::illegalOpr);
1087  } else {
1088    LIR_Opr reg = result_register_for(x->type(), /*callee=*/true);
1089    LIRItem result(x->result(), this);
1090
1091    result.load_item_force(reg);
1092    __ return_op(result.result());
1093  }
1094  set_no_result(x);
1095}
1096
1097
1098// Example: object.getClass ()
1099void LIRGenerator::do_getClass(Intrinsic* x) {
1100  assert(x->number_of_arguments() == 1, "wrong type");
1101
1102  LIRItem rcvr(x->argument_at(0), this);
1103  rcvr.load_item();
1104  LIR_Opr result = rlock_result(x);
1105
1106  // need to perform the null check on the rcvr
1107  CodeEmitInfo* info = NULL;
1108  if (x->needs_null_check()) {
1109    info = state_for(x, x->state()->copy_locks());
1110  }
1111  __ move(new LIR_Address(rcvr.result(), oopDesc::klass_offset_in_bytes(), T_OBJECT), result, info);
1112  __ move(new LIR_Address(result, Klass::java_mirror_offset_in_bytes() +
1113                          klassOopDesc::klass_part_offset_in_bytes(), T_OBJECT), result);
1114}
1115
1116
1117// Example: Thread.currentThread()
1118void LIRGenerator::do_currentThread(Intrinsic* x) {
1119  assert(x->number_of_arguments() == 0, "wrong type");
1120  LIR_Opr reg = rlock_result(x);
1121  __ load(new LIR_Address(getThreadPointer(), in_bytes(JavaThread::threadObj_offset()), T_OBJECT), reg);
1122}
1123
1124
1125void LIRGenerator::do_RegisterFinalizer(Intrinsic* x) {
1126  assert(x->number_of_arguments() == 1, "wrong type");
1127  LIRItem receiver(x->argument_at(0), this);
1128
1129  receiver.load_item();
1130  BasicTypeList signature;
1131  signature.append(T_OBJECT); // receiver
1132  LIR_OprList* args = new LIR_OprList();
1133  args->append(receiver.result());
1134  CodeEmitInfo* info = state_for(x, x->state());
1135  call_runtime(&signature, args,
1136               CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::register_finalizer_id)),
1137               voidType, info);
1138
1139  set_no_result(x);
1140}
1141
1142
1143//------------------------local access--------------------------------------
1144
1145LIR_Opr LIRGenerator::operand_for_instruction(Instruction* x) {
1146  if (x->operand()->is_illegal()) {
1147    Constant* c = x->as_Constant();
1148    if (c != NULL) {
1149      x->set_operand(LIR_OprFact::value_type(c->type()));
1150    } else {
1151      assert(x->as_Phi() || x->as_Local() != NULL, "only for Phi and Local");
1152      // allocate a virtual register for this local or phi
1153      x->set_operand(rlock(x));
1154      _instruction_for_operand.at_put_grow(x->operand()->vreg_number(), x, NULL);
1155    }
1156  }
1157  return x->operand();
1158}
1159
1160
1161Instruction* LIRGenerator::instruction_for_opr(LIR_Opr opr) {
1162  if (opr->is_virtual()) {
1163    return instruction_for_vreg(opr->vreg_number());
1164  }
1165  return NULL;
1166}
1167
1168
1169Instruction* LIRGenerator::instruction_for_vreg(int reg_num) {
1170  if (reg_num < _instruction_for_operand.length()) {
1171    return _instruction_for_operand.at(reg_num);
1172  }
1173  return NULL;
1174}
1175
1176
1177void LIRGenerator::set_vreg_flag(int vreg_num, VregFlag f) {
1178  if (_vreg_flags.size_in_bits() == 0) {
1179    BitMap2D temp(100, num_vreg_flags);
1180    temp.clear();
1181    _vreg_flags = temp;
1182  }
1183  _vreg_flags.at_put_grow(vreg_num, f, true);
1184}
1185
1186bool LIRGenerator::is_vreg_flag_set(int vreg_num, VregFlag f) {
1187  if (!_vreg_flags.is_valid_index(vreg_num, f)) {
1188    return false;
1189  }
1190  return _vreg_flags.at(vreg_num, f);
1191}
1192
1193
1194// Block local constant handling.  This code is useful for keeping
1195// unpinned constants and constants which aren't exposed in the IR in
1196// registers.  Unpinned Constant instructions have their operands
1197// cleared when the block is finished so that other blocks can't end
1198// up referring to their registers.
1199
1200LIR_Opr LIRGenerator::load_constant(Constant* x) {
1201  assert(!x->is_pinned(), "only for unpinned constants");
1202  _unpinned_constants.append(x);
1203  return load_constant(LIR_OprFact::value_type(x->type())->as_constant_ptr());
1204}
1205
1206
1207LIR_Opr LIRGenerator::load_constant(LIR_Const* c) {
1208  BasicType t = c->type();
1209  for (int i = 0; i < _constants.length(); i++) {
1210    LIR_Const* other = _constants.at(i);
1211    if (t == other->type()) {
1212      switch (t) {
1213      case T_INT:
1214      case T_FLOAT:
1215        if (c->as_jint_bits() != other->as_jint_bits()) continue;
1216        break;
1217      case T_LONG:
1218      case T_DOUBLE:
1219        if (c->as_jint_hi_bits() != other->as_jint_hi_bits()) continue;
1220        if (c->as_jint_lo_bits() != other->as_jint_lo_bits()) continue;
1221        break;
1222      case T_OBJECT:
1223        if (c->as_jobject() != other->as_jobject()) continue;
1224        break;
1225      }
1226      return _reg_for_constants.at(i);
1227    }
1228  }
1229
1230  LIR_Opr result = new_register(t);
1231  __ move((LIR_Opr)c, result);
1232  _constants.append(c);
1233  _reg_for_constants.append(result);
1234  return result;
1235}
1236
1237// Various barriers
1238
1239void LIRGenerator::pre_barrier(LIR_Opr addr_opr, bool patch,  CodeEmitInfo* info) {
1240  // Do the pre-write barrier, if any.
1241  switch (_bs->kind()) {
1242#ifndef SERIALGC
1243    case BarrierSet::G1SATBCT:
1244    case BarrierSet::G1SATBCTLogging:
1245      G1SATBCardTableModRef_pre_barrier(addr_opr, patch, info);
1246      break;
1247#endif // SERIALGC
1248    case BarrierSet::CardTableModRef:
1249    case BarrierSet::CardTableExtension:
1250      // No pre barriers
1251      break;
1252    case BarrierSet::ModRef:
1253    case BarrierSet::Other:
1254      // No pre barriers
1255      break;
1256    default      :
1257      ShouldNotReachHere();
1258
1259  }
1260}
1261
1262void LIRGenerator::post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1263  switch (_bs->kind()) {
1264#ifndef SERIALGC
1265    case BarrierSet::G1SATBCT:
1266    case BarrierSet::G1SATBCTLogging:
1267      G1SATBCardTableModRef_post_barrier(addr,  new_val);
1268      break;
1269#endif // SERIALGC
1270    case BarrierSet::CardTableModRef:
1271    case BarrierSet::CardTableExtension:
1272      CardTableModRef_post_barrier(addr,  new_val);
1273      break;
1274    case BarrierSet::ModRef:
1275    case BarrierSet::Other:
1276      // No post barriers
1277      break;
1278    default      :
1279      ShouldNotReachHere();
1280    }
1281}
1282
1283////////////////////////////////////////////////////////////////////////
1284#ifndef SERIALGC
1285
1286void LIRGenerator::G1SATBCardTableModRef_pre_barrier(LIR_Opr addr_opr, bool patch,  CodeEmitInfo* info) {
1287  if (G1DisablePreBarrier) return;
1288
1289  // First we test whether marking is in progress.
1290  BasicType flag_type;
1291  if (in_bytes(PtrQueue::byte_width_of_active()) == 4) {
1292    flag_type = T_INT;
1293  } else {
1294    guarantee(in_bytes(PtrQueue::byte_width_of_active()) == 1,
1295              "Assumption");
1296    flag_type = T_BYTE;
1297  }
1298  LIR_Opr thrd = getThreadPointer();
1299  LIR_Address* mark_active_flag_addr =
1300    new LIR_Address(thrd,
1301                    in_bytes(JavaThread::satb_mark_queue_offset() +
1302                             PtrQueue::byte_offset_of_active()),
1303                    flag_type);
1304  // Read the marking-in-progress flag.
1305  LIR_Opr flag_val = new_register(T_INT);
1306  __ load(mark_active_flag_addr, flag_val);
1307
1308  LabelObj* start_store = new LabelObj();
1309
1310  LIR_PatchCode pre_val_patch_code =
1311    patch ? lir_patch_normal : lir_patch_none;
1312
1313  LIR_Opr pre_val = new_register(T_OBJECT);
1314
1315  __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
1316  if (!addr_opr->is_address()) {
1317    assert(addr_opr->is_register(), "must be");
1318    addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
1319  }
1320  CodeStub* slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code,
1321                                        info);
1322  __ branch(lir_cond_notEqual, T_INT, slow);
1323  __ branch_destination(slow->continuation());
1324}
1325
1326void LIRGenerator::G1SATBCardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1327  if (G1DisablePostBarrier) return;
1328
1329  // If the "new_val" is a constant NULL, no barrier is necessary.
1330  if (new_val->is_constant() &&
1331      new_val->as_constant_ptr()->as_jobject() == NULL) return;
1332
1333  if (!new_val->is_register()) {
1334    LIR_Opr new_val_reg = new_register(T_OBJECT);
1335    if (new_val->is_constant()) {
1336      __ move(new_val, new_val_reg);
1337    } else {
1338      __ leal(new_val, new_val_reg);
1339    }
1340    new_val = new_val_reg;
1341  }
1342  assert(new_val->is_register(), "must be a register at this point");
1343
1344  if (addr->is_address()) {
1345    LIR_Address* address = addr->as_address_ptr();
1346    LIR_Opr ptr = new_register(T_OBJECT);
1347    if (!address->index()->is_valid() && address->disp() == 0) {
1348      __ move(address->base(), ptr);
1349    } else {
1350      assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1351      __ leal(addr, ptr);
1352    }
1353    addr = ptr;
1354  }
1355  assert(addr->is_register(), "must be a register at this point");
1356
1357  LIR_Opr xor_res = new_pointer_register();
1358  LIR_Opr xor_shift_res = new_pointer_register();
1359  if (TwoOperandLIRForm ) {
1360    __ move(addr, xor_res);
1361    __ logical_xor(xor_res, new_val, xor_res);
1362    __ move(xor_res, xor_shift_res);
1363    __ unsigned_shift_right(xor_shift_res,
1364                            LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1365                            xor_shift_res,
1366                            LIR_OprDesc::illegalOpr());
1367  } else {
1368    __ logical_xor(addr, new_val, xor_res);
1369    __ unsigned_shift_right(xor_res,
1370                            LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
1371                            xor_shift_res,
1372                            LIR_OprDesc::illegalOpr());
1373  }
1374
1375  if (!new_val->is_register()) {
1376    LIR_Opr new_val_reg = new_register(T_OBJECT);
1377    __ leal(new_val, new_val_reg);
1378    new_val = new_val_reg;
1379  }
1380  assert(new_val->is_register(), "must be a register at this point");
1381
1382  __ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));
1383
1384  CodeStub* slow = new G1PostBarrierStub(addr, new_val);
1385  __ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);
1386  __ branch_destination(slow->continuation());
1387}
1388
1389#endif // SERIALGC
1390////////////////////////////////////////////////////////////////////////
1391
1392void LIRGenerator::CardTableModRef_post_barrier(LIR_OprDesc* addr, LIR_OprDesc* new_val) {
1393
1394  assert(sizeof(*((CardTableModRefBS*)_bs)->byte_map_base) == sizeof(jbyte), "adjust this code");
1395  LIR_Const* card_table_base = new LIR_Const(((CardTableModRefBS*)_bs)->byte_map_base);
1396  if (addr->is_address()) {
1397    LIR_Address* address = addr->as_address_ptr();
1398    LIR_Opr ptr = new_register(T_OBJECT);
1399    if (!address->index()->is_valid() && address->disp() == 0) {
1400      __ move(address->base(), ptr);
1401    } else {
1402      assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
1403      __ leal(addr, ptr);
1404    }
1405    addr = ptr;
1406  }
1407  assert(addr->is_register(), "must be a register at this point");
1408
1409#ifdef ARM
1410  // TODO: ARM - move to platform-dependent code
1411  LIR_Opr tmp = FrameMap::R14_opr;
1412  if (VM_Version::supports_movw()) {
1413    __ move((LIR_Opr)card_table_base, tmp);
1414  } else {
1415    __ move(new LIR_Address(FrameMap::Rthread_opr, in_bytes(JavaThread::card_table_base_offset()), T_ADDRESS), tmp);
1416  }
1417
1418  CardTableModRefBS* ct = (CardTableModRefBS*)_bs;
1419  LIR_Address *card_addr = new LIR_Address(tmp, addr, (LIR_Address::Scale) -CardTableModRefBS::card_shift, 0, T_BYTE);
1420  if(((int)ct->byte_map_base & 0xff) == 0) {
1421    __ move(tmp, card_addr);
1422  } else {
1423    LIR_Opr tmp_zero = new_register(T_INT);
1424    __ move(LIR_OprFact::intConst(0), tmp_zero);
1425    __ move(tmp_zero, card_addr);
1426  }
1427#else // ARM
1428  LIR_Opr tmp = new_pointer_register();
1429  if (TwoOperandLIRForm) {
1430    __ move(addr, tmp);
1431    __ unsigned_shift_right(tmp, CardTableModRefBS::card_shift, tmp);
1432  } else {
1433    __ unsigned_shift_right(addr, CardTableModRefBS::card_shift, tmp);
1434  }
1435  if (can_inline_as_constant(card_table_base)) {
1436    __ move(LIR_OprFact::intConst(0),
1437              new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE));
1438  } else {
1439    __ move(LIR_OprFact::intConst(0),
1440              new LIR_Address(tmp, load_constant(card_table_base),
1441                              T_BYTE));
1442  }
1443#endif // ARM
1444}
1445
1446
1447//------------------------field access--------------------------------------
1448
1449// Comment copied form templateTable_i486.cpp
1450// ----------------------------------------------------------------------------
1451// Volatile variables demand their effects be made known to all CPU's in
1452// order.  Store buffers on most chips allow reads & writes to reorder; the
1453// JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
1454// memory barrier (i.e., it's not sufficient that the interpreter does not
1455// reorder volatile references, the hardware also must not reorder them).
1456//
1457// According to the new Java Memory Model (JMM):
1458// (1) All volatiles are serialized wrt to each other.
1459// ALSO reads & writes act as aquire & release, so:
1460// (2) A read cannot let unrelated NON-volatile memory refs that happen after
1461// the read float up to before the read.  It's OK for non-volatile memory refs
1462// that happen before the volatile read to float down below it.
1463// (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
1464// that happen BEFORE the write float down to after the write.  It's OK for
1465// non-volatile memory refs that happen after the volatile write to float up
1466// before it.
1467//
1468// We only put in barriers around volatile refs (they are expensive), not
1469// _between_ memory refs (that would require us to track the flavor of the
1470// previous memory refs).  Requirements (2) and (3) require some barriers
1471// before volatile stores and after volatile loads.  These nearly cover
1472// requirement (1) but miss the volatile-store-volatile-load case.  This final
1473// case is placed after volatile-stores although it could just as well go
1474// before volatile-loads.
1475
1476
1477void LIRGenerator::do_StoreField(StoreField* x) {
1478  bool needs_patching = x->needs_patching();
1479  bool is_volatile = x->field()->is_volatile();
1480  BasicType field_type = x->field_type();
1481  bool is_oop = (field_type == T_ARRAY || field_type == T_OBJECT);
1482
1483  CodeEmitInfo* info = NULL;
1484  if (needs_patching) {
1485    assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1486    info = state_for(x, x->state_before());
1487  } else if (x->needs_null_check()) {
1488    NullCheck* nc = x->explicit_null_check();
1489    if (nc == NULL) {
1490      info = state_for(x, x->lock_stack());
1491    } else {
1492      info = state_for(nc);
1493    }
1494  }
1495
1496
1497  LIRItem object(x->obj(), this);
1498  LIRItem value(x->value(),  this);
1499
1500  object.load_item();
1501
1502  if (is_volatile || needs_patching) {
1503    // load item if field is volatile (fewer special cases for volatiles)
1504    // load item if field not initialized
1505    // load item if field not constant
1506    // because of code patching we cannot inline constants
1507    if (field_type == T_BYTE || field_type == T_BOOLEAN) {
1508      value.load_byte_item();
1509    } else  {
1510      value.load_item();
1511    }
1512  } else {
1513    value.load_for_store(field_type);
1514  }
1515
1516  set_no_result(x);
1517
1518  if (PrintNotLoaded && needs_patching) {
1519    tty->print_cr("   ###class not loaded at store_%s bci %d",
1520                  x->is_static() ?  "static" : "field", x->bci());
1521  }
1522
1523  if (x->needs_null_check() &&
1524      (needs_patching ||
1525       MacroAssembler::needs_explicit_null_check(x->offset()))) {
1526    // emit an explicit null check because the offset is too large
1527    __ null_check(object.result(), new CodeEmitInfo(info));
1528  }
1529
1530  LIR_Address* address;
1531  if (needs_patching) {
1532    // we need to patch the offset in the instruction so don't allow
1533    // generate_address to try to be smart about emitting the -1.
1534    // Otherwise the patching code won't know how to find the
1535    // instruction to patch.
1536    address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);
1537  } else {
1538    address = generate_address(object.result(), x->offset(), field_type);
1539  }
1540
1541  if (is_volatile && os::is_MP()) {
1542    __ membar_release();
1543  }
1544
1545  if (is_oop) {
1546    // Do the pre-write barrier, if any.
1547    pre_barrier(LIR_OprFact::address(address),
1548                needs_patching,
1549                (info ? new CodeEmitInfo(info) : NULL));
1550  }
1551
1552  if (is_volatile) {
1553    assert(!needs_patching && x->is_loaded(),
1554           "how do we know it's volatile if it's not loaded");
1555    volatile_field_store(value.result(), address, info);
1556  } else {
1557    LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1558    __ store(value.result(), address, info, patch_code);
1559  }
1560
1561  if (is_oop) {
1562    // Store to object so mark the card of the header
1563    post_barrier(object.result(), value.result());
1564  }
1565
1566  if (is_volatile && os::is_MP()) {
1567    __ membar();
1568  }
1569}
1570
1571
1572void LIRGenerator::do_LoadField(LoadField* x) {
1573  bool needs_patching = x->needs_patching();
1574  bool is_volatile = x->field()->is_volatile();
1575  BasicType field_type = x->field_type();
1576
1577  CodeEmitInfo* info = NULL;
1578  if (needs_patching) {
1579    assert(x->explicit_null_check() == NULL, "can't fold null check into patching field access");
1580    info = state_for(x, x->state_before());
1581  } else if (x->needs_null_check()) {
1582    NullCheck* nc = x->explicit_null_check();
1583    if (nc == NULL) {
1584      info = state_for(x, x->lock_stack());
1585    } else {
1586      info = state_for(nc);
1587    }
1588  }
1589
1590  LIRItem object(x->obj(), this);
1591
1592  object.load_item();
1593
1594  if (PrintNotLoaded && needs_patching) {
1595    tty->print_cr("   ###class not loaded at load_%s bci %d",
1596                  x->is_static() ?  "static" : "field", x->bci());
1597  }
1598
1599  if (x->needs_null_check() &&
1600      (needs_patching ||
1601       MacroAssembler::needs_explicit_null_check(x->offset()))) {
1602    // emit an explicit null check because the offset is too large
1603    __ null_check(object.result(), new CodeEmitInfo(info));
1604  }
1605
1606  LIR_Opr reg = rlock_result(x, field_type);
1607  LIR_Address* address;
1608  if (needs_patching) {
1609    // we need to patch the offset in the instruction so don't allow
1610    // generate_address to try to be smart about emitting the -1.
1611    // Otherwise the patching code won't know how to find the
1612    // instruction to patch.
1613    address = new LIR_Address(object.result(), PATCHED_ADDR, field_type);
1614  } else {
1615    address = generate_address(object.result(), x->offset(), field_type);
1616  }
1617
1618  if (is_volatile) {
1619    assert(!needs_patching && x->is_loaded(),
1620           "how do we know it's volatile if it's not loaded");
1621    volatile_field_load(address, reg, info);
1622  } else {
1623    LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;
1624    __ load(address, reg, info, patch_code);
1625  }
1626
1627  if (is_volatile && os::is_MP()) {
1628    __ membar_acquire();
1629  }
1630}
1631
1632
1633//------------------------java.nio.Buffer.checkIndex------------------------
1634
1635// int java.nio.Buffer.checkIndex(int)
1636void LIRGenerator::do_NIOCheckIndex(Intrinsic* x) {
1637  // NOTE: by the time we are in checkIndex() we are guaranteed that
1638  // the buffer is non-null (because checkIndex is package-private and
1639  // only called from within other methods in the buffer).
1640  assert(x->number_of_arguments() == 2, "wrong type");
1641  LIRItem buf  (x->argument_at(0), this);
1642  LIRItem index(x->argument_at(1), this);
1643  buf.load_item();
1644  index.load_item();
1645
1646  LIR_Opr result = rlock_result(x);
1647  if (GenerateRangeChecks) {
1648    CodeEmitInfo* info = state_for(x);
1649    CodeStub* stub = new RangeCheckStub(info, index.result(), true);
1650    if (index.result()->is_constant()) {
1651      cmp_mem_int(lir_cond_belowEqual, buf.result(), java_nio_Buffer::limit_offset(), index.result()->as_jint(), info);
1652      __ branch(lir_cond_belowEqual, T_INT, stub);
1653    } else {
1654      cmp_reg_mem(lir_cond_aboveEqual, index.result(), buf.result(),
1655                  java_nio_Buffer::limit_offset(), T_INT, info);
1656      __ branch(lir_cond_aboveEqual, T_INT, stub);
1657    }
1658    __ move(index.result(), result);
1659  } else {
1660    // Just load the index into the result register
1661    __ move(index.result(), result);
1662  }
1663}
1664
1665
1666//------------------------array access--------------------------------------
1667
1668
1669void LIRGenerator::do_ArrayLength(ArrayLength* x) {
1670  LIRItem array(x->array(), this);
1671  array.load_item();
1672  LIR_Opr reg = rlock_result(x);
1673
1674  CodeEmitInfo* info = NULL;
1675  if (x->needs_null_check()) {
1676    NullCheck* nc = x->explicit_null_check();
1677    if (nc == NULL) {
1678      info = state_for(x);
1679    } else {
1680      info = state_for(nc);
1681    }
1682  }
1683  __ load(new LIR_Address(array.result(), arrayOopDesc::length_offset_in_bytes(), T_INT), reg, info, lir_patch_none);
1684}
1685
1686
1687void LIRGenerator::do_LoadIndexed(LoadIndexed* x) {
1688  bool use_length = x->length() != NULL;
1689  LIRItem array(x->array(), this);
1690  LIRItem index(x->index(), this);
1691  LIRItem length(this);
1692  bool needs_range_check = true;
1693
1694  if (use_length) {
1695    needs_range_check = x->compute_needs_range_check();
1696    if (needs_range_check) {
1697      length.set_instruction(x->length());
1698      length.load_item();
1699    }
1700  }
1701
1702  array.load_item();
1703  if (index.is_constant() && can_inline_as_constant(x->index())) {
1704    // let it be a constant
1705    index.dont_load_item();
1706  } else {
1707    index.load_item();
1708  }
1709
1710  CodeEmitInfo* range_check_info = state_for(x);
1711  CodeEmitInfo* null_check_info = NULL;
1712  if (x->needs_null_check()) {
1713    NullCheck* nc = x->explicit_null_check();
1714    if (nc != NULL) {
1715      null_check_info = state_for(nc);
1716    } else {
1717      null_check_info = range_check_info;
1718    }
1719  }
1720
1721  // emit array address setup early so it schedules better
1722  LIR_Address* array_addr = emit_array_address(array.result(), index.result(), x->elt_type(), false);
1723
1724  if (GenerateRangeChecks && needs_range_check) {
1725    if (use_length) {
1726      // TODO: use a (modified) version of array_range_check that does not require a
1727      //       constant length to be loaded to a register
1728      __ cmp(lir_cond_belowEqual, length.result(), index.result());
1729      __ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));
1730    } else {
1731      array_range_check(array.result(), index.result(), null_check_info, range_check_info);
1732      // The range check performs the null check, so clear it out for the load
1733      null_check_info = NULL;
1734    }
1735  }
1736
1737  __ move(array_addr, rlock_result(x, x->elt_type()), null_check_info);
1738}
1739
1740
1741void LIRGenerator::do_NullCheck(NullCheck* x) {
1742  if (x->can_trap()) {
1743    LIRItem value(x->obj(), this);
1744    value.load_item();
1745    CodeEmitInfo* info = state_for(x);
1746    __ null_check(value.result(), info);
1747  }
1748}
1749
1750
1751void LIRGenerator::do_Throw(Throw* x) {
1752  LIRItem exception(x->exception(), this);
1753  exception.load_item();
1754  set_no_result(x);
1755  LIR_Opr exception_opr = exception.result();
1756  CodeEmitInfo* info = state_for(x, x->state());
1757
1758#ifndef PRODUCT
1759  if (PrintC1Statistics) {
1760    increment_counter(Runtime1::throw_count_address());
1761  }
1762#endif
1763
1764  // check if the instruction has an xhandler in any of the nested scopes
1765  bool unwind = false;
1766  if (info->exception_handlers()->length() == 0) {
1767    // this throw is not inside an xhandler
1768    unwind = true;
1769  } else {
1770    // get some idea of the throw type
1771    bool type_is_exact = true;
1772    ciType* throw_type = x->exception()->exact_type();
1773    if (throw_type == NULL) {
1774      type_is_exact = false;
1775      throw_type = x->exception()->declared_type();
1776    }
1777    if (throw_type != NULL && throw_type->is_instance_klass()) {
1778      ciInstanceKlass* throw_klass = (ciInstanceKlass*)throw_type;
1779      unwind = !x->exception_handlers()->could_catch(throw_klass, type_is_exact);
1780    }
1781  }
1782
1783  // do null check before moving exception oop into fixed register
1784  // to avoid a fixed interval with an oop during the null check.
1785  // Use a copy of the CodeEmitInfo because debug information is
1786  // different for null_check and throw.
1787  if (GenerateCompilerNullChecks &&
1788      (x->exception()->as_NewInstance() == NULL && x->exception()->as_ExceptionObject() == NULL)) {
1789    // if the exception object wasn't created using new then it might be null.
1790    __ null_check(exception_opr, new CodeEmitInfo(info, true));
1791  }
1792
1793  if (compilation()->env()->jvmti_can_post_on_exceptions()) {
1794    // we need to go through the exception lookup path to get JVMTI
1795    // notification done
1796    unwind = false;
1797  }
1798
1799  // move exception oop into fixed register
1800  __ move(exception_opr, exceptionOopOpr());
1801
1802  if (unwind) {
1803    __ unwind_exception(exceptionOopOpr());
1804  } else {
1805    __ throw_exception(exceptionPcOpr(), exceptionOopOpr(), info);
1806  }
1807}
1808
1809
1810void LIRGenerator::do_RoundFP(RoundFP* x) {
1811  LIRItem input(x->input(), this);
1812  input.load_item();
1813  LIR_Opr input_opr = input.result();
1814  assert(input_opr->is_register(), "why round if value is not in a register?");
1815  assert(input_opr->is_single_fpu() || input_opr->is_double_fpu(), "input should be floating-point value");
1816  if (input_opr->is_single_fpu()) {
1817    set_result(x, round_item(input_opr)); // This code path not currently taken
1818  } else {
1819    LIR_Opr result = new_register(T_DOUBLE);
1820    set_vreg_flag(result, must_start_in_memory);
1821    __ roundfp(input_opr, LIR_OprFact::illegalOpr, result);
1822    set_result(x, result);
1823  }
1824}
1825
1826void LIRGenerator::do_UnsafeGetRaw(UnsafeGetRaw* x) {
1827  LIRItem base(x->base(), this);
1828  LIRItem idx(this);
1829
1830  base.load_item();
1831  if (x->has_index()) {
1832    idx.set_instruction(x->index());
1833    idx.load_nonconstant();
1834  }
1835
1836  LIR_Opr reg = rlock_result(x, x->basic_type());
1837
1838  int   log2_scale = 0;
1839  if (x->has_index()) {
1840    assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1841    log2_scale = x->log2_scale();
1842  }
1843
1844  assert(!x->has_index() || idx.value() == x->index(), "should match");
1845
1846  LIR_Opr base_op = base.result();
1847#ifndef _LP64
1848  if (x->base()->type()->tag() == longTag) {
1849    base_op = new_register(T_INT);
1850    __ convert(Bytecodes::_l2i, base.result(), base_op);
1851  } else {
1852    assert(x->base()->type()->tag() == intTag, "must be");
1853  }
1854#endif
1855
1856  BasicType dst_type = x->basic_type();
1857  LIR_Opr index_op = idx.result();
1858
1859  LIR_Address* addr;
1860  if (index_op->is_constant()) {
1861    assert(log2_scale == 0, "must not have a scale");
1862    addr = new LIR_Address(base_op, index_op->as_jint(), dst_type);
1863  } else {
1864#ifdef X86
1865#ifdef _LP64
1866    if (!index_op->is_illegal() && index_op->type() == T_INT) {
1867      LIR_Opr tmp = new_pointer_register();
1868      __ convert(Bytecodes::_i2l, index_op, tmp);
1869      index_op = tmp;
1870    }
1871#endif
1872    addr = new LIR_Address(base_op, index_op, LIR_Address::Scale(log2_scale), 0, dst_type);
1873#elif defined(ARM)
1874    addr = generate_address(base_op, index_op, log2_scale, 0, dst_type);
1875#else
1876    if (index_op->is_illegal() || log2_scale == 0) {
1877#ifdef _LP64
1878      if (!index_op->is_illegal() && index_op->type() == T_INT) {
1879        LIR_Opr tmp = new_pointer_register();
1880        __ convert(Bytecodes::_i2l, index_op, tmp);
1881        index_op = tmp;
1882      }
1883#endif
1884      addr = new LIR_Address(base_op, index_op, dst_type);
1885    } else {
1886      LIR_Opr tmp = new_pointer_register();
1887      __ shift_left(index_op, log2_scale, tmp);
1888      addr = new LIR_Address(base_op, tmp, dst_type);
1889    }
1890#endif
1891  }
1892
1893  if (x->may_be_unaligned() && (dst_type == T_LONG || dst_type == T_DOUBLE)) {
1894    __ unaligned_move(addr, reg);
1895  } else {
1896    __ move(addr, reg);
1897  }
1898}
1899
1900
1901void LIRGenerator::do_UnsafePutRaw(UnsafePutRaw* x) {
1902  int  log2_scale = 0;
1903  BasicType type = x->basic_type();
1904
1905  if (x->has_index()) {
1906    assert(x->index()->type()->tag() == intTag, "should not find non-int index");
1907    log2_scale = x->log2_scale();
1908  }
1909
1910  LIRItem base(x->base(), this);
1911  LIRItem value(x->value(), this);
1912  LIRItem idx(this);
1913
1914  base.load_item();
1915  if (x->has_index()) {
1916    idx.set_instruction(x->index());
1917    idx.load_item();
1918  }
1919
1920  if (type == T_BYTE || type == T_BOOLEAN) {
1921    value.load_byte_item();
1922  } else {
1923    value.load_item();
1924  }
1925
1926  set_no_result(x);
1927
1928  LIR_Opr base_op = base.result();
1929#ifndef _LP64
1930  if (x->base()->type()->tag() == longTag) {
1931    base_op = new_register(T_INT);
1932    __ convert(Bytecodes::_l2i, base.result(), base_op);
1933  } else {
1934    assert(x->base()->type()->tag() == intTag, "must be");
1935  }
1936#endif
1937
1938  LIR_Opr index_op = idx.result();
1939  if (log2_scale != 0) {
1940    // temporary fix (platform dependent code without shift on Intel would be better)
1941    index_op = new_pointer_register();
1942#ifdef _LP64
1943    if(idx.result()->type() == T_INT) {
1944      __ convert(Bytecodes::_i2l, idx.result(), index_op);
1945    } else {
1946#endif
1947      // TODO: ARM also allows embedded shift in the address
1948      __ move(idx.result(), index_op);
1949#ifdef _LP64
1950    }
1951#endif
1952    __ shift_left(index_op, log2_scale, index_op);
1953  }
1954#ifdef _LP64
1955  else if(!index_op->is_illegal() && index_op->type() == T_INT) {
1956    LIR_Opr tmp = new_pointer_register();
1957    __ convert(Bytecodes::_i2l, index_op, tmp);
1958    index_op = tmp;
1959  }
1960#endif
1961
1962  LIR_Address* addr = new LIR_Address(base_op, index_op, x->basic_type());
1963  __ move(value.result(), addr);
1964}
1965
1966
1967void LIRGenerator::do_UnsafeGetObject(UnsafeGetObject* x) {
1968  BasicType type = x->basic_type();
1969  LIRItem src(x->object(), this);
1970  LIRItem off(x->offset(), this);
1971
1972  off.load_item();
1973  src.load_item();
1974
1975  LIR_Opr reg = reg = rlock_result(x, x->basic_type());
1976
1977  if (x->is_volatile() && os::is_MP()) __ membar_acquire();
1978  get_Object_unsafe(reg, src.result(), off.result(), type, x->is_volatile());
1979  if (x->is_volatile() && os::is_MP()) __ membar();
1980}
1981
1982
1983void LIRGenerator::do_UnsafePutObject(UnsafePutObject* x) {
1984  BasicType type = x->basic_type();
1985  LIRItem src(x->object(), this);
1986  LIRItem off(x->offset(), this);
1987  LIRItem data(x->value(), this);
1988
1989  src.load_item();
1990  if (type == T_BOOLEAN || type == T_BYTE) {
1991    data.load_byte_item();
1992  } else {
1993    data.load_item();
1994  }
1995  off.load_item();
1996
1997  set_no_result(x);
1998
1999  if (x->is_volatile() && os::is_MP()) __ membar_release();
2000  put_Object_unsafe(src.result(), off.result(), data.result(), type, x->is_volatile());
2001}
2002
2003
2004void LIRGenerator::do_UnsafePrefetch(UnsafePrefetch* x, bool is_store) {
2005  LIRItem src(x->object(), this);
2006  LIRItem off(x->offset(), this);
2007
2008  src.load_item();
2009  if (off.is_constant() && can_inline_as_constant(x->offset())) {
2010    // let it be a constant
2011    off.dont_load_item();
2012  } else {
2013    off.load_item();
2014  }
2015
2016  set_no_result(x);
2017
2018  LIR_Address* addr = generate_address(src.result(), off.result(), 0, 0, T_BYTE);
2019  __ prefetch(addr, is_store);
2020}
2021
2022
2023void LIRGenerator::do_UnsafePrefetchRead(UnsafePrefetchRead* x) {
2024  do_UnsafePrefetch(x, false);
2025}
2026
2027
2028void LIRGenerator::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {
2029  do_UnsafePrefetch(x, true);
2030}
2031
2032
2033void LIRGenerator::do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux) {
2034  int lng = x->length();
2035
2036  for (int i = 0; i < lng; i++) {
2037    SwitchRange* one_range = x->at(i);
2038    int low_key = one_range->low_key();
2039    int high_key = one_range->high_key();
2040    BlockBegin* dest = one_range->sux();
2041    if (low_key == high_key) {
2042      __ cmp(lir_cond_equal, value, low_key);
2043      __ branch(lir_cond_equal, T_INT, dest);
2044    } else if (high_key - low_key == 1) {
2045      __ cmp(lir_cond_equal, value, low_key);
2046      __ branch(lir_cond_equal, T_INT, dest);
2047      __ cmp(lir_cond_equal, value, high_key);
2048      __ branch(lir_cond_equal, T_INT, dest);
2049    } else {
2050      LabelObj* L = new LabelObj();
2051      __ cmp(lir_cond_less, value, low_key);
2052      __ branch(lir_cond_less, L->label());
2053      __ cmp(lir_cond_lessEqual, value, high_key);
2054      __ branch(lir_cond_lessEqual, T_INT, dest);
2055      __ branch_destination(L->label());
2056    }
2057  }
2058  __ jump(default_sux);
2059}
2060
2061
2062SwitchRangeArray* LIRGenerator::create_lookup_ranges(TableSwitch* x) {
2063  SwitchRangeList* res = new SwitchRangeList();
2064  int len = x->length();
2065  if (len > 0) {
2066    BlockBegin* sux = x->sux_at(0);
2067    int key = x->lo_key();
2068    BlockBegin* default_sux = x->default_sux();
2069    SwitchRange* range = new SwitchRange(key, sux);
2070    for (int i = 0; i < len; i++, key++) {
2071      BlockBegin* new_sux = x->sux_at(i);
2072      if (sux == new_sux) {
2073        // still in same range
2074        range->set_high_key(key);
2075      } else {
2076        // skip tests which explicitly dispatch to the default
2077        if (sux != default_sux) {
2078          res->append(range);
2079        }
2080        range = new SwitchRange(key, new_sux);
2081      }
2082      sux = new_sux;
2083    }
2084    if (res->length() == 0 || res->last() != range)  res->append(range);
2085  }
2086  return res;
2087}
2088
2089
2090// we expect the keys to be sorted by increasing value
2091SwitchRangeArray* LIRGenerator::create_lookup_ranges(LookupSwitch* x) {
2092  SwitchRangeList* res = new SwitchRangeList();
2093  int len = x->length();
2094  if (len > 0) {
2095    BlockBegin* default_sux = x->default_sux();
2096    int key = x->key_at(0);
2097    BlockBegin* sux = x->sux_at(0);
2098    SwitchRange* range = new SwitchRange(key, sux);
2099    for (int i = 1; i < len; i++) {
2100      int new_key = x->key_at(i);
2101      BlockBegin* new_sux = x->sux_at(i);
2102      if (key+1 == new_key && sux == new_sux) {
2103        // still in same range
2104        range->set_high_key(new_key);
2105      } else {
2106        // skip tests which explicitly dispatch to the default
2107        if (range->sux() != default_sux) {
2108          res->append(range);
2109        }
2110        range = new SwitchRange(new_key, new_sux);
2111      }
2112      key = new_key;
2113      sux = new_sux;
2114    }
2115    if (res->length() == 0 || res->last() != range)  res->append(range);
2116  }
2117  return res;
2118}
2119
2120
2121void LIRGenerator::do_TableSwitch(TableSwitch* x) {
2122  LIRItem tag(x->tag(), this);
2123  tag.load_item();
2124  set_no_result(x);
2125
2126  if (x->is_safepoint()) {
2127    __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2128  }
2129
2130  // move values into phi locations
2131  move_to_phi(x->state());
2132
2133  int lo_key = x->lo_key();
2134  int hi_key = x->hi_key();
2135  int len = x->length();
2136  CodeEmitInfo* info = state_for(x, x->state());
2137  LIR_Opr value = tag.result();
2138  if (UseTableRanges) {
2139    do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2140  } else {
2141    for (int i = 0; i < len; i++) {
2142      __ cmp(lir_cond_equal, value, i + lo_key);
2143      __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2144    }
2145    __ jump(x->default_sux());
2146  }
2147}
2148
2149
2150void LIRGenerator::do_LookupSwitch(LookupSwitch* x) {
2151  LIRItem tag(x->tag(), this);
2152  tag.load_item();
2153  set_no_result(x);
2154
2155  if (x->is_safepoint()) {
2156    __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
2157  }
2158
2159  // move values into phi locations
2160  move_to_phi(x->state());
2161
2162  LIR_Opr value = tag.result();
2163  if (UseTableRanges) {
2164    do_SwitchRanges(create_lookup_ranges(x), value, x->default_sux());
2165  } else {
2166    int len = x->length();
2167    for (int i = 0; i < len; i++) {
2168      __ cmp(lir_cond_equal, value, x->key_at(i));
2169      __ branch(lir_cond_equal, T_INT, x->sux_at(i));
2170    }
2171    __ jump(x->default_sux());
2172  }
2173}
2174
2175
2176void LIRGenerator::do_Goto(Goto* x) {
2177  set_no_result(x);
2178
2179  if (block()->next()->as_OsrEntry()) {
2180    // need to free up storage used for OSR entry point
2181    LIR_Opr osrBuffer = block()->next()->operand();
2182    BasicTypeList signature;
2183    signature.append(T_INT);
2184    CallingConvention* cc = frame_map()->c_calling_convention(&signature);
2185    __ move(osrBuffer, cc->args()->at(0));
2186    __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
2187                         getThreadTemp(), LIR_OprFact::illegalOpr, cc->args());
2188  }
2189
2190  if (x->is_safepoint()) {
2191    ValueStack* state = x->state_before() ? x->state_before() : x->state();
2192
2193    // increment backedge counter if needed
2194    increment_backedge_counter(state_for(x, state));
2195
2196    CodeEmitInfo* safepoint_info = state_for(x, state);
2197    __ safepoint(safepoint_poll_register(), safepoint_info);
2198  }
2199
2200  // emit phi-instruction move after safepoint since this simplifies
2201  // describing the state as the safepoint.
2202  move_to_phi(x->state());
2203
2204  __ jump(x->default_sux());
2205}
2206
2207
2208void LIRGenerator::do_Base(Base* x) {
2209  __ std_entry(LIR_OprFact::illegalOpr);
2210  // Emit moves from physical registers / stack slots to virtual registers
2211  CallingConvention* args = compilation()->frame_map()->incoming_arguments();
2212  IRScope* irScope = compilation()->hir()->top_scope();
2213  int java_index = 0;
2214  for (int i = 0; i < args->length(); i++) {
2215    LIR_Opr src = args->at(i);
2216    assert(!src->is_illegal(), "check");
2217    BasicType t = src->type();
2218
2219    // Types which are smaller than int are passed as int, so
2220    // correct the type which passed.
2221    switch (t) {
2222    case T_BYTE:
2223    case T_BOOLEAN:
2224    case T_SHORT:
2225    case T_CHAR:
2226      t = T_INT;
2227      break;
2228    }
2229
2230    LIR_Opr dest = new_register(t);
2231    __ move(src, dest);
2232
2233    // Assign new location to Local instruction for this local
2234    Local* local = x->state()->local_at(java_index)->as_Local();
2235    assert(local != NULL, "Locals for incoming arguments must have been created");
2236#ifndef __SOFTFP__
2237    // The java calling convention passes double as long and float as int.
2238    assert(as_ValueType(t)->tag() == local->type()->tag(), "check");
2239#endif // __SOFTFP__
2240    local->set_operand(dest);
2241    _instruction_for_operand.at_put_grow(dest->vreg_number(), local, NULL);
2242    java_index += type2size[t];
2243  }
2244
2245  if (compilation()->env()->dtrace_method_probes()) {
2246    BasicTypeList signature;
2247    signature.append(T_INT);    // thread
2248    signature.append(T_OBJECT); // methodOop
2249    LIR_OprList* args = new LIR_OprList();
2250    args->append(getThreadPointer());
2251    LIR_Opr meth = new_register(T_OBJECT);
2252    __ oop2reg(method()->constant_encoding(), meth);
2253    args->append(meth);
2254    call_runtime(&signature, args, CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), voidType, NULL);
2255  }
2256
2257  if (method()->is_synchronized()) {
2258    LIR_Opr obj;
2259    if (method()->is_static()) {
2260      obj = new_register(T_OBJECT);
2261      __ oop2reg(method()->holder()->java_mirror()->constant_encoding(), obj);
2262    } else {
2263      Local* receiver = x->state()->local_at(0)->as_Local();
2264      assert(receiver != NULL, "must already exist");
2265      obj = receiver->operand();
2266    }
2267    assert(obj->is_valid(), "must be valid");
2268
2269    if (method()->is_synchronized() && GenerateSynchronizationCode) {
2270      LIR_Opr lock = new_register(T_INT);
2271      __ load_stack_address_monitor(0, lock);
2272
2273      CodeEmitInfo* info = new CodeEmitInfo(SynchronizationEntryBCI, scope()->start()->state(), NULL);
2274      CodeStub* slow_path = new MonitorEnterStub(obj, lock, info);
2275
2276      // receiver is guaranteed non-NULL so don't need CodeEmitInfo
2277      __ lock_object(syncTempOpr(), obj, lock, new_register(T_OBJECT), slow_path, NULL);
2278    }
2279  }
2280
2281  // increment invocation counters if needed
2282  increment_invocation_counter(new CodeEmitInfo(0, scope()->start()->state(), NULL));
2283
2284  // all blocks with a successor must end with an unconditional jump
2285  // to the successor even if they are consecutive
2286  __ jump(x->default_sux());
2287}
2288
2289
2290void LIRGenerator::do_OsrEntry(OsrEntry* x) {
2291  // construct our frame and model the production of incoming pointer
2292  // to the OSR buffer.
2293  __ osr_entry(LIR_Assembler::osrBufferPointer());
2294  LIR_Opr result = rlock_result(x);
2295  __ move(LIR_Assembler::osrBufferPointer(), result);
2296}
2297
2298
2299void LIRGenerator::invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list) {
2300  int i = (x->has_receiver() || x->is_invokedynamic()) ? 1 : 0;
2301  for (; i < args->length(); i++) {
2302    LIRItem* param = args->at(i);
2303    LIR_Opr loc = arg_list->at(i);
2304    if (loc->is_register()) {
2305      param->load_item_force(loc);
2306    } else {
2307      LIR_Address* addr = loc->as_address_ptr();
2308      param->load_for_store(addr->type());
2309      if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2310        __ unaligned_move(param->result(), addr);
2311      } else {
2312        __ move(param->result(), addr);
2313      }
2314    }
2315  }
2316
2317  if (x->has_receiver()) {
2318    LIRItem* receiver = args->at(0);
2319    LIR_Opr loc = arg_list->at(0);
2320    if (loc->is_register()) {
2321      receiver->load_item_force(loc);
2322    } else {
2323      assert(loc->is_address(), "just checking");
2324      receiver->load_for_store(T_OBJECT);
2325      __ move(receiver->result(), loc);
2326    }
2327  }
2328}
2329
2330
2331// Visits all arguments, returns appropriate items without loading them
2332LIRItemList* LIRGenerator::invoke_visit_arguments(Invoke* x) {
2333  LIRItemList* argument_items = new LIRItemList();
2334  if (x->has_receiver()) {
2335    LIRItem* receiver = new LIRItem(x->receiver(), this);
2336    argument_items->append(receiver);
2337  }
2338  if (x->is_invokedynamic()) {
2339    // Insert a dummy for the synthetic MethodHandle argument.
2340    argument_items->append(NULL);
2341  }
2342  int idx = x->has_receiver() ? 1 : 0;
2343  for (int i = 0; i < x->number_of_arguments(); i++) {
2344    LIRItem* param = new LIRItem(x->argument_at(i), this);
2345    argument_items->append(param);
2346    idx += (param->type()->is_double_word() ? 2 : 1);
2347  }
2348  return argument_items;
2349}
2350
2351
2352// The invoke with receiver has following phases:
2353//   a) traverse and load/lock receiver;
2354//   b) traverse all arguments -> item-array (invoke_visit_argument)
2355//   c) push receiver on stack
2356//   d) load each of the items and push on stack
2357//   e) unlock receiver
2358//   f) move receiver into receiver-register %o0
2359//   g) lock result registers and emit call operation
2360//
2361// Before issuing a call, we must spill-save all values on stack
2362// that are in caller-save register. "spill-save" moves thos registers
2363// either in a free callee-save register or spills them if no free
2364// callee save register is available.
2365//
2366// The problem is where to invoke spill-save.
2367// - if invoked between e) and f), we may lock callee save
2368//   register in "spill-save" that destroys the receiver register
2369//   before f) is executed
2370// - if we rearange the f) to be earlier, by loading %o0, it
2371//   may destroy a value on the stack that is currently in %o0
2372//   and is waiting to be spilled
2373// - if we keep the receiver locked while doing spill-save,
2374//   we cannot spill it as it is spill-locked
2375//
2376void LIRGenerator::do_Invoke(Invoke* x) {
2377  CallingConvention* cc = frame_map()->java_calling_convention(x->signature(), true);
2378
2379  LIR_OprList* arg_list = cc->args();
2380  LIRItemList* args = invoke_visit_arguments(x);
2381  LIR_Opr receiver = LIR_OprFact::illegalOpr;
2382
2383  // setup result register
2384  LIR_Opr result_register = LIR_OprFact::illegalOpr;
2385  if (x->type() != voidType) {
2386    result_register = result_register_for(x->type());
2387  }
2388
2389  CodeEmitInfo* info = state_for(x, x->state());
2390
2391  // invokedynamics can deoptimize.
2392  CodeEmitInfo* deopt_info = x->is_invokedynamic() ? state_for(x, x->state_before()) : NULL;
2393
2394  invoke_load_arguments(x, args, arg_list);
2395
2396  if (x->has_receiver()) {
2397    args->at(0)->load_item_force(LIR_Assembler::receiverOpr());
2398    receiver = args->at(0)->result();
2399  }
2400
2401  // emit invoke code
2402  bool optimized = x->target_is_loaded() && x->target_is_final();
2403  assert(receiver->is_illegal() || receiver->is_equal(LIR_Assembler::receiverOpr()), "must match");
2404
2405  // JSR 292
2406  // Preserve the SP over MethodHandle call sites.
2407  ciMethod* target = x->target();
2408  if (target->is_method_handle_invoke()) {
2409    info->set_is_method_handle_invoke(true);
2410    __ move(FrameMap::stack_pointer(), FrameMap::method_handle_invoke_SP_save_opr());
2411  }
2412
2413  switch (x->code()) {
2414    case Bytecodes::_invokestatic:
2415      __ call_static(target, result_register,
2416                     SharedRuntime::get_resolve_static_call_stub(),
2417                     arg_list, info);
2418      break;
2419    case Bytecodes::_invokespecial:
2420    case Bytecodes::_invokevirtual:
2421    case Bytecodes::_invokeinterface:
2422      // for final target we still produce an inline cache, in order
2423      // to be able to call mixed mode
2424      if (x->code() == Bytecodes::_invokespecial || optimized) {
2425        __ call_opt_virtual(target, receiver, result_register,
2426                            SharedRuntime::get_resolve_opt_virtual_call_stub(),
2427                            arg_list, info);
2428      } else if (x->vtable_index() < 0) {
2429        __ call_icvirtual(target, receiver, result_register,
2430                          SharedRuntime::get_resolve_virtual_call_stub(),
2431                          arg_list, info);
2432      } else {
2433        int entry_offset = instanceKlass::vtable_start_offset() + x->vtable_index() * vtableEntry::size();
2434        int vtable_offset = entry_offset * wordSize + vtableEntry::method_offset_in_bytes();
2435        __ call_virtual(target, receiver, result_register, vtable_offset, arg_list, info);
2436      }
2437      break;
2438    case Bytecodes::_invokedynamic: {
2439      ciBytecodeStream bcs(x->scope()->method());
2440      bcs.force_bci(x->bci());
2441      assert(bcs.cur_bc() == Bytecodes::_invokedynamic, "wrong stream");
2442      ciCPCache* cpcache = bcs.get_cpcache();
2443
2444      // Get CallSite offset from constant pool cache pointer.
2445      int index = bcs.get_method_index();
2446      size_t call_site_offset = cpcache->get_f1_offset(index);
2447
2448      // If this invokedynamic call site hasn't been executed yet in
2449      // the interpreter, the CallSite object in the constant pool
2450      // cache is still null and we need to deoptimize.
2451      if (cpcache->is_f1_null_at(index)) {
2452        // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
2453        // clone all handlers.  This is handled transparently in other
2454        // places by the CodeEmitInfo cloning logic but is handled
2455        // specially here because a stub isn't being used.
2456        x->set_exception_handlers(new XHandlers(x->exception_handlers()));
2457
2458        DeoptimizeStub* deopt_stub = new DeoptimizeStub(deopt_info);
2459        __ jump(deopt_stub);
2460      }
2461
2462      // Use the receiver register for the synthetic MethodHandle
2463      // argument.
2464      receiver = LIR_Assembler::receiverOpr();
2465      LIR_Opr tmp = new_register(objectType);
2466
2467      // Load CallSite object from constant pool cache.
2468      __ oop2reg(cpcache->constant_encoding(), tmp);
2469      __ load(new LIR_Address(tmp, call_site_offset, T_OBJECT), tmp);
2470
2471      // Load target MethodHandle from CallSite object.
2472      __ load(new LIR_Address(tmp, java_dyn_CallSite::target_offset_in_bytes(), T_OBJECT), receiver);
2473
2474      __ call_dynamic(target, receiver, result_register,
2475                      SharedRuntime::get_resolve_opt_virtual_call_stub(),
2476                      arg_list, info);
2477      break;
2478    }
2479    default:
2480      ShouldNotReachHere();
2481      break;
2482  }
2483
2484  // JSR 292
2485  // Restore the SP after MethodHandle call sites.
2486  if (target->is_method_handle_invoke()) {
2487    __ move(FrameMap::method_handle_invoke_SP_save_opr(), FrameMap::stack_pointer());
2488  }
2489
2490  if (x->type()->is_float() || x->type()->is_double()) {
2491    // Force rounding of results from non-strictfp when in strictfp
2492    // scope (or when we don't know the strictness of the callee, to
2493    // be safe.)
2494    if (method()->is_strict()) {
2495      if (!x->target_is_loaded() || !x->target_is_strictfp()) {
2496        result_register = round_item(result_register);
2497      }
2498    }
2499  }
2500
2501  if (result_register->is_valid()) {
2502    LIR_Opr result = rlock_result(x);
2503    __ move(result_register, result);
2504  }
2505}
2506
2507
2508void LIRGenerator::do_FPIntrinsics(Intrinsic* x) {
2509  assert(x->number_of_arguments() == 1, "wrong type");
2510  LIRItem value       (x->argument_at(0), this);
2511  LIR_Opr reg = rlock_result(x);
2512  value.load_item();
2513  LIR_Opr tmp = force_to_spill(value.result(), as_BasicType(x->type()));
2514  __ move(tmp, reg);
2515}
2516
2517
2518
2519// Code for  :  x->x() {x->cond()} x->y() ? x->tval() : x->fval()
2520void LIRGenerator::do_IfOp(IfOp* x) {
2521#ifdef ASSERT
2522  {
2523    ValueTag xtag = x->x()->type()->tag();
2524    ValueTag ttag = x->tval()->type()->tag();
2525    assert(xtag == intTag || xtag == objectTag, "cannot handle others");
2526    assert(ttag == addressTag || ttag == intTag || ttag == objectTag || ttag == longTag, "cannot handle others");
2527    assert(ttag == x->fval()->type()->tag(), "cannot handle others");
2528  }
2529#endif
2530
2531  LIRItem left(x->x(), this);
2532  LIRItem right(x->y(), this);
2533  left.load_item();
2534  if (can_inline_as_constant(right.value())) {
2535    right.dont_load_item();
2536  } else {
2537    right.load_item();
2538  }
2539
2540  LIRItem t_val(x->tval(), this);
2541  LIRItem f_val(x->fval(), this);
2542  t_val.dont_load_item();
2543  f_val.dont_load_item();
2544  LIR_Opr reg = rlock_result(x);
2545
2546  __ cmp(lir_cond(x->cond()), left.result(), right.result());
2547  __ cmove(lir_cond(x->cond()), t_val.result(), f_val.result(), reg);
2548}
2549
2550
2551void LIRGenerator::do_Intrinsic(Intrinsic* x) {
2552  switch (x->id()) {
2553  case vmIntrinsics::_intBitsToFloat      :
2554  case vmIntrinsics::_doubleToRawLongBits :
2555  case vmIntrinsics::_longBitsToDouble    :
2556  case vmIntrinsics::_floatToRawIntBits   : {
2557    do_FPIntrinsics(x);
2558    break;
2559  }
2560
2561  case vmIntrinsics::_currentTimeMillis: {
2562    assert(x->number_of_arguments() == 0, "wrong type");
2563    LIR_Opr reg = result_register_for(x->type());
2564    __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeMillis), getThreadTemp(),
2565                         reg, new LIR_OprList());
2566    LIR_Opr result = rlock_result(x);
2567    __ move(reg, result);
2568    break;
2569  }
2570
2571  case vmIntrinsics::_nanoTime: {
2572    assert(x->number_of_arguments() == 0, "wrong type");
2573    LIR_Opr reg = result_register_for(x->type());
2574    __ call_runtime_leaf(CAST_FROM_FN_PTR(address, os::javaTimeNanos), getThreadTemp(),
2575                         reg, new LIR_OprList());
2576    LIR_Opr result = rlock_result(x);
2577    __ move(reg, result);
2578    break;
2579  }
2580
2581  case vmIntrinsics::_Object_init:    do_RegisterFinalizer(x); break;
2582  case vmIntrinsics::_getClass:       do_getClass(x);      break;
2583  case vmIntrinsics::_currentThread:  do_currentThread(x); break;
2584
2585  case vmIntrinsics::_dlog:           // fall through
2586  case vmIntrinsics::_dlog10:         // fall through
2587  case vmIntrinsics::_dabs:           // fall through
2588  case vmIntrinsics::_dsqrt:          // fall through
2589  case vmIntrinsics::_dtan:           // fall through
2590  case vmIntrinsics::_dsin :          // fall through
2591  case vmIntrinsics::_dcos :          do_MathIntrinsic(x); break;
2592  case vmIntrinsics::_arraycopy:      do_ArrayCopy(x);     break;
2593
2594  // java.nio.Buffer.checkIndex
2595  case vmIntrinsics::_checkIndex:     do_NIOCheckIndex(x); break;
2596
2597  case vmIntrinsics::_compareAndSwapObject:
2598    do_CompareAndSwap(x, objectType);
2599    break;
2600  case vmIntrinsics::_compareAndSwapInt:
2601    do_CompareAndSwap(x, intType);
2602    break;
2603  case vmIntrinsics::_compareAndSwapLong:
2604    do_CompareAndSwap(x, longType);
2605    break;
2606
2607    // sun.misc.AtomicLongCSImpl.attemptUpdate
2608  case vmIntrinsics::_attemptUpdate:
2609    do_AttemptUpdate(x);
2610    break;
2611
2612  default: ShouldNotReachHere(); break;
2613  }
2614}
2615
2616
2617void LIRGenerator::do_ProfileCall(ProfileCall* x) {
2618  // Need recv in a temporary register so it interferes with the other temporaries
2619  LIR_Opr recv = LIR_OprFact::illegalOpr;
2620  LIR_Opr mdo = new_register(T_OBJECT);
2621  LIR_Opr tmp = new_register(T_INT);
2622  if (x->recv() != NULL) {
2623    LIRItem value(x->recv(), this);
2624    value.load_item();
2625    recv = new_register(T_OBJECT);
2626    __ move(value.result(), recv);
2627  }
2628  __ profile_call(x->method(), x->bci_of_invoke(), mdo, recv, tmp, x->known_holder());
2629}
2630
2631
2632void LIRGenerator::do_ProfileCounter(ProfileCounter* x) {
2633  LIRItem mdo(x->mdo(), this);
2634  mdo.load_item();
2635
2636  increment_counter(new LIR_Address(mdo.result(), x->offset(), T_INT), x->increment());
2637}
2638
2639
2640LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
2641  LIRItemList args(1);
2642  LIRItem value(arg1, this);
2643  args.append(&value);
2644  BasicTypeList signature;
2645  signature.append(as_BasicType(arg1->type()));
2646
2647  return call_runtime(&signature, &args, entry, result_type, info);
2648}
2649
2650
2651LIR_Opr LIRGenerator::call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info) {
2652  LIRItemList args(2);
2653  LIRItem value1(arg1, this);
2654  LIRItem value2(arg2, this);
2655  args.append(&value1);
2656  args.append(&value2);
2657  BasicTypeList signature;
2658  signature.append(as_BasicType(arg1->type()));
2659  signature.append(as_BasicType(arg2->type()));
2660
2661  return call_runtime(&signature, &args, entry, result_type, info);
2662}
2663
2664
2665LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIR_OprList* args,
2666                                   address entry, ValueType* result_type, CodeEmitInfo* info) {
2667  // get a result register
2668  LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2669  LIR_Opr result = LIR_OprFact::illegalOpr;
2670  if (result_type->tag() != voidTag) {
2671    result = new_register(result_type);
2672    phys_reg = result_register_for(result_type);
2673  }
2674
2675  // move the arguments into the correct location
2676  CallingConvention* cc = frame_map()->c_calling_convention(signature);
2677  assert(cc->length() == args->length(), "argument mismatch");
2678  for (int i = 0; i < args->length(); i++) {
2679    LIR_Opr arg = args->at(i);
2680    LIR_Opr loc = cc->at(i);
2681    if (loc->is_register()) {
2682      __ move(arg, loc);
2683    } else {
2684      LIR_Address* addr = loc->as_address_ptr();
2685//           if (!can_store_as_constant(arg)) {
2686//             LIR_Opr tmp = new_register(arg->type());
2687//             __ move(arg, tmp);
2688//             arg = tmp;
2689//           }
2690      if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2691        __ unaligned_move(arg, addr);
2692      } else {
2693        __ move(arg, addr);
2694      }
2695    }
2696  }
2697
2698  if (info) {
2699    __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2700  } else {
2701    __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2702  }
2703  if (result->is_valid()) {
2704    __ move(phys_reg, result);
2705  }
2706  return result;
2707}
2708
2709
2710LIR_Opr LIRGenerator::call_runtime(BasicTypeArray* signature, LIRItemList* args,
2711                                   address entry, ValueType* result_type, CodeEmitInfo* info) {
2712  // get a result register
2713  LIR_Opr phys_reg = LIR_OprFact::illegalOpr;
2714  LIR_Opr result = LIR_OprFact::illegalOpr;
2715  if (result_type->tag() != voidTag) {
2716    result = new_register(result_type);
2717    phys_reg = result_register_for(result_type);
2718  }
2719
2720  // move the arguments into the correct location
2721  CallingConvention* cc = frame_map()->c_calling_convention(signature);
2722
2723  assert(cc->length() == args->length(), "argument mismatch");
2724  for (int i = 0; i < args->length(); i++) {
2725    LIRItem* arg = args->at(i);
2726    LIR_Opr loc = cc->at(i);
2727    if (loc->is_register()) {
2728      arg->load_item_force(loc);
2729    } else {
2730      LIR_Address* addr = loc->as_address_ptr();
2731      arg->load_for_store(addr->type());
2732      if (addr->type() == T_LONG || addr->type() == T_DOUBLE) {
2733        __ unaligned_move(arg->result(), addr);
2734      } else {
2735        __ move(arg->result(), addr);
2736      }
2737    }
2738  }
2739
2740  if (info) {
2741    __ call_runtime(entry, getThreadTemp(), phys_reg, cc->args(), info);
2742  } else {
2743    __ call_runtime_leaf(entry, getThreadTemp(), phys_reg, cc->args());
2744  }
2745  if (result->is_valid()) {
2746    __ move(phys_reg, result);
2747  }
2748  return result;
2749}
2750
2751
2752
2753void LIRGenerator::increment_invocation_counter(CodeEmitInfo* info, bool backedge) {
2754#ifdef TIERED
2755  if (_compilation->env()->comp_level() == CompLevel_fast_compile &&
2756      (method()->code_size() >= Tier1BytecodeLimit || backedge)) {
2757    int limit = InvocationCounter::Tier1InvocationLimit;
2758    int offset = in_bytes(methodOopDesc::invocation_counter_offset() +
2759                          InvocationCounter::counter_offset());
2760    if (backedge) {
2761      limit = InvocationCounter::Tier1BackEdgeLimit;
2762      offset = in_bytes(methodOopDesc::backedge_counter_offset() +
2763                        InvocationCounter::counter_offset());
2764    }
2765
2766    LIR_Opr meth = new_register(T_OBJECT);
2767    __ oop2reg(method()->constant_encoding(), meth);
2768    LIR_Opr result = increment_and_return_counter(meth, offset, InvocationCounter::count_increment);
2769    __ cmp(lir_cond_aboveEqual, result, LIR_OprFact::intConst(limit));
2770    CodeStub* overflow = new CounterOverflowStub(info, info->bci());
2771    __ branch(lir_cond_aboveEqual, T_INT, overflow);
2772    __ branch_destination(overflow->continuation());
2773  }
2774#endif
2775}
2776