c1_LIRGenerator_sparc.cpp revision 342:37f87013dfd8
1/*
2 * Copyright 2005-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_c1_LIRGenerator_sparc.cpp.incl"
27
28#ifdef ASSERT
29#define __ gen()->lir(__FILE__, __LINE__)->
30#else
31#define __ gen()->lir()->
32#endif
33
34void LIRItem::load_byte_item() {
35  // byte loads use same registers as other loads
36  load_item();
37}
38
39
40void LIRItem::load_nonconstant() {
41  LIR_Opr r = value()->operand();
42  if (_gen->can_inline_as_constant(value())) {
43    if (!r->is_constant()) {
44      r = LIR_OprFact::value_type(value()->type());
45    }
46    _result = r;
47  } else {
48    load_item();
49  }
50}
51
52
53//--------------------------------------------------------------
54//               LIRGenerator
55//--------------------------------------------------------------
56
57LIR_Opr LIRGenerator::exceptionOopOpr()              { return FrameMap::Oexception_opr;  }
58LIR_Opr LIRGenerator::exceptionPcOpr()               { return FrameMap::Oissuing_pc_opr; }
59LIR_Opr LIRGenerator::syncTempOpr()                  { return new_register(T_OBJECT); }
60LIR_Opr LIRGenerator::getThreadTemp()                { return rlock_callee_saved(T_INT); }
61
62LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
63  LIR_Opr opr;
64  switch (type->tag()) {
65  case intTag:     opr = callee ? FrameMap::I0_opr      : FrameMap::O0_opr;       break;
66  case objectTag:  opr = callee ? FrameMap::I0_oop_opr  : FrameMap::O0_oop_opr;   break;
67  case longTag:    opr = callee ? FrameMap::in_long_opr : FrameMap::out_long_opr; break;
68  case floatTag:   opr = FrameMap::F0_opr;                                        break;
69  case doubleTag:  opr = FrameMap::F0_double_opr;                                 break;
70
71  case addressTag:
72  default: ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
73  }
74
75  assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
76  return opr;
77}
78
79LIR_Opr LIRGenerator::rlock_callee_saved(BasicType type) {
80  LIR_Opr reg = new_register(type);
81  set_vreg_flag(reg, callee_saved);
82  return reg;
83}
84
85
86LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
87  return new_register(T_INT);
88}
89
90
91
92
93
94//--------- loading items into registers --------------------------------
95
96// SPARC cannot inline all constants
97bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
98  if (v->type()->as_IntConstant() != NULL) {
99    return v->type()->as_IntConstant()->value() == 0;
100  } else if (v->type()->as_LongConstant() != NULL) {
101    return v->type()->as_LongConstant()->value() == 0L;
102  } else if (v->type()->as_ObjectConstant() != NULL) {
103    return v->type()->as_ObjectConstant()->value()->is_null_object();
104  } else {
105    return false;
106  }
107}
108
109
110// only simm13 constants can be inlined
111bool LIRGenerator:: can_inline_as_constant(Value i) const {
112  if (i->type()->as_IntConstant() != NULL) {
113    return Assembler::is_simm13(i->type()->as_IntConstant()->value());
114  } else {
115    return can_store_as_constant(i, as_BasicType(i->type()));
116  }
117}
118
119
120bool LIRGenerator:: can_inline_as_constant(LIR_Const* c) const {
121  if (c->type() == T_INT) {
122    return Assembler::is_simm13(c->as_jint());
123  }
124  return false;
125}
126
127
128LIR_Opr LIRGenerator::safepoint_poll_register() {
129  return new_register(T_INT);
130}
131
132
133
134LIR_Address* LIRGenerator::generate_address(LIR_Opr base, LIR_Opr index,
135                                            int shift, int disp, BasicType type) {
136  assert(base->is_register(), "must be");
137
138  // accumulate fixed displacements
139  if (index->is_constant()) {
140    disp += index->as_constant_ptr()->as_jint() << shift;
141    index = LIR_OprFact::illegalOpr;
142  }
143
144  if (index->is_register()) {
145    // apply the shift and accumulate the displacement
146    if (shift > 0) {
147      LIR_Opr tmp = new_register(T_INT);
148      __ shift_left(index, shift, tmp);
149      index = tmp;
150    }
151    if (disp != 0) {
152      LIR_Opr tmp = new_register(T_INT);
153      if (Assembler::is_simm13(disp)) {
154        __ add(tmp, LIR_OprFact::intConst(disp), tmp);
155        index = tmp;
156      } else {
157        __ move(LIR_OprFact::intConst(disp), tmp);
158        __ add(tmp, index, tmp);
159        index = tmp;
160      }
161      disp = 0;
162    }
163  } else if (disp != 0 && !Assembler::is_simm13(disp)) {
164    // index is illegal so replace it with the displacement loaded into a register
165    index = new_register(T_INT);
166    __ move(LIR_OprFact::intConst(disp), index);
167    disp = 0;
168  }
169
170  // at this point we either have base + index or base + displacement
171  if (disp == 0) {
172    return new LIR_Address(base, index, type);
173  } else {
174    assert(Assembler::is_simm13(disp), "must be");
175    return new LIR_Address(base, disp, type);
176  }
177}
178
179
180LIR_Address* LIRGenerator::emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr,
181                                              BasicType type, bool needs_card_mark) {
182  int elem_size = type2aelembytes(type);
183  int shift = exact_log2(elem_size);
184
185  LIR_Opr base_opr;
186  int offset = arrayOopDesc::base_offset_in_bytes(type);
187
188  if (index_opr->is_constant()) {
189    int i = index_opr->as_constant_ptr()->as_jint();
190    int array_offset = i * elem_size;
191    if (Assembler::is_simm13(array_offset + offset)) {
192      base_opr = array_opr;
193      offset = array_offset + offset;
194    } else {
195      base_opr = new_pointer_register();
196      if (Assembler::is_simm13(array_offset)) {
197        __ add(array_opr, LIR_OprFact::intptrConst(array_offset), base_opr);
198      } else {
199        __ move(LIR_OprFact::intptrConst(array_offset), base_opr);
200        __ add(base_opr, array_opr, base_opr);
201      }
202    }
203  } else {
204#ifdef _LP64
205    if (index_opr->type() == T_INT) {
206      LIR_Opr tmp = new_register(T_LONG);
207      __ convert(Bytecodes::_i2l, index_opr, tmp);
208      index_opr = tmp;
209    }
210#endif
211
212    base_opr = new_pointer_register();
213    assert (index_opr->is_register(), "Must be register");
214    if (shift > 0) {
215      __ shift_left(index_opr, shift, base_opr);
216      __ add(base_opr, array_opr, base_opr);
217    } else {
218      __ add(index_opr, array_opr, base_opr);
219    }
220  }
221  if (needs_card_mark) {
222    LIR_Opr ptr = new_pointer_register();
223    __ add(base_opr, LIR_OprFact::intptrConst(offset), ptr);
224    return new LIR_Address(ptr, 0, type);
225  } else {
226    return new LIR_Address(base_opr, offset, type);
227  }
228}
229
230
231void LIRGenerator::increment_counter(address counter, int step) {
232  LIR_Opr pointer = new_pointer_register();
233  __ move(LIR_OprFact::intptrConst(counter), pointer);
234  LIR_Address* addr = new LIR_Address(pointer, 0, T_INT);
235  increment_counter(addr, step);
236}
237
238void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
239  LIR_Opr temp = new_register(T_INT);
240  __ move(addr, temp);
241  LIR_Opr c = LIR_OprFact::intConst(step);
242  if (Assembler::is_simm13(step)) {
243    __ add(temp, c, temp);
244  } else {
245    LIR_Opr temp2 = new_register(T_INT);
246    __ move(c, temp2);
247    __ add(temp, temp2, temp);
248  }
249  __ move(temp, addr);
250}
251
252
253void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
254  LIR_Opr o7opr = FrameMap::O7_opr;
255  __ load(new LIR_Address(base, disp, T_INT), o7opr, info);
256  __ cmp(condition, o7opr, c);
257}
258
259
260void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info) {
261  LIR_Opr o7opr = FrameMap::O7_opr;
262  __ load(new LIR_Address(base, disp, type), o7opr, info);
263  __ cmp(condition, reg, o7opr);
264}
265
266
267void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, LIR_Opr disp, BasicType type, CodeEmitInfo* info) {
268  LIR_Opr o7opr = FrameMap::O7_opr;
269  __ load(new LIR_Address(base, disp, type), o7opr, info);
270  __ cmp(condition, reg, o7opr);
271}
272
273
274bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, int c, LIR_Opr result, LIR_Opr tmp) {
275  assert(left != result, "should be different registers");
276  if (is_power_of_2(c + 1)) {
277    __ shift_left(left, log2_intptr(c + 1), result);
278    __ sub(result, left, result);
279    return true;
280  } else if (is_power_of_2(c - 1)) {
281    __ shift_left(left, log2_intptr(c - 1), result);
282    __ add(result, left, result);
283    return true;
284  }
285  return false;
286}
287
288
289void LIRGenerator::store_stack_parameter (LIR_Opr item, ByteSize offset_from_sp) {
290  BasicType t = item->type();
291  LIR_Opr sp_opr = FrameMap::SP_opr;
292  if ((t == T_LONG || t == T_DOUBLE) &&
293      ((in_bytes(offset_from_sp) - STACK_BIAS) % 8 != 0)) {
294    __ unaligned_move(item, new LIR_Address(sp_opr, in_bytes(offset_from_sp), t));
295  } else {
296    __ move(item, new LIR_Address(sp_opr, in_bytes(offset_from_sp), t));
297  }
298}
299
300//----------------------------------------------------------------------
301//             visitor functions
302//----------------------------------------------------------------------
303
304
305void LIRGenerator::do_StoreIndexed(StoreIndexed* x) {
306  assert(x->is_root(),"");
307  bool needs_range_check = true;
308  bool use_length = x->length() != NULL;
309  bool obj_store = x->elt_type() == T_ARRAY || x->elt_type() == T_OBJECT;
310  bool needs_store_check = obj_store && (x->value()->as_Constant() == NULL ||
311                                         !get_jobject_constant(x->value())->is_null_object());
312
313  LIRItem array(x->array(), this);
314  LIRItem index(x->index(), this);
315  LIRItem value(x->value(), this);
316  LIRItem length(this);
317
318  array.load_item();
319  index.load_nonconstant();
320
321  if (use_length) {
322    needs_range_check = x->compute_needs_range_check();
323    if (needs_range_check) {
324      length.set_instruction(x->length());
325      length.load_item();
326    }
327  }
328  if (needs_store_check) {
329    value.load_item();
330  } else {
331    value.load_for_store(x->elt_type());
332  }
333
334  set_no_result(x);
335
336  // the CodeEmitInfo must be duplicated for each different
337  // LIR-instruction because spilling can occur anywhere between two
338  // instructions and so the debug information must be different
339  CodeEmitInfo* range_check_info = state_for(x);
340  CodeEmitInfo* null_check_info = NULL;
341  if (x->needs_null_check()) {
342    null_check_info = new CodeEmitInfo(range_check_info);
343  }
344
345  // emit array address setup early so it schedules better
346  LIR_Address* array_addr = emit_array_address(array.result(), index.result(), x->elt_type(), obj_store);
347
348  if (GenerateRangeChecks && needs_range_check) {
349    if (use_length) {
350      __ cmp(lir_cond_belowEqual, length.result(), index.result());
351      __ branch(lir_cond_belowEqual, T_INT, new RangeCheckStub(range_check_info, index.result()));
352    } else {
353      array_range_check(array.result(), index.result(), null_check_info, range_check_info);
354      // range_check also does the null check
355      null_check_info = NULL;
356    }
357  }
358
359  if (GenerateArrayStoreCheck && needs_store_check) {
360    LIR_Opr tmp1 = FrameMap::G1_opr;
361    LIR_Opr tmp2 = FrameMap::G3_opr;
362    LIR_Opr tmp3 = FrameMap::G5_opr;
363
364    CodeEmitInfo* store_check_info = new CodeEmitInfo(range_check_info);
365    __ store_check(value.result(), array.result(), tmp1, tmp2, tmp3, store_check_info);
366  }
367
368  if (obj_store) {
369    // Needs GC write barriers.
370    pre_barrier(LIR_OprFact::address(array_addr), false, NULL);
371  }
372  __ move(value.result(), array_addr, null_check_info);
373  if (obj_store) {
374    // Is this precise?
375    post_barrier(LIR_OprFact::address(array_addr), value.result());
376  }
377}
378
379
380void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
381  assert(x->is_root(),"");
382  LIRItem obj(x->obj(), this);
383  obj.load_item();
384
385  set_no_result(x);
386
387  LIR_Opr lock    = FrameMap::G1_opr;
388  LIR_Opr scratch = FrameMap::G3_opr;
389  LIR_Opr hdr     = FrameMap::G4_opr;
390
391  CodeEmitInfo* info_for_exception = NULL;
392  if (x->needs_null_check()) {
393    info_for_exception = state_for(x, x->lock_stack_before());
394  }
395
396  // this CodeEmitInfo must not have the xhandlers because here the
397  // object is already locked (xhandlers expects object to be unlocked)
398  CodeEmitInfo* info = state_for(x, x->state(), true);
399  monitor_enter(obj.result(), lock, hdr, scratch, x->monitor_no(), info_for_exception, info);
400}
401
402
403void LIRGenerator::do_MonitorExit(MonitorExit* x) {
404  assert(x->is_root(),"");
405  LIRItem obj(x->obj(), this);
406  obj.dont_load_item();
407
408  set_no_result(x);
409  LIR_Opr lock      = FrameMap::G1_opr;
410  LIR_Opr hdr       = FrameMap::G3_opr;
411  LIR_Opr obj_temp  = FrameMap::G4_opr;
412  monitor_exit(obj_temp, lock, hdr, x->monitor_no());
413}
414
415
416// _ineg, _lneg, _fneg, _dneg
417void LIRGenerator::do_NegateOp(NegateOp* x) {
418  LIRItem value(x->x(), this);
419  value.load_item();
420  LIR_Opr reg = rlock_result(x);
421  __ negate(value.result(), reg);
422}
423
424
425
426// for  _fadd, _fmul, _fsub, _fdiv, _frem
427//      _dadd, _dmul, _dsub, _ddiv, _drem
428void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
429  switch (x->op()) {
430  case Bytecodes::_fadd:
431  case Bytecodes::_fmul:
432  case Bytecodes::_fsub:
433  case Bytecodes::_fdiv:
434  case Bytecodes::_dadd:
435  case Bytecodes::_dmul:
436  case Bytecodes::_dsub:
437  case Bytecodes::_ddiv: {
438    LIRItem left(x->x(), this);
439    LIRItem right(x->y(), this);
440    left.load_item();
441    right.load_item();
442    rlock_result(x);
443    arithmetic_op_fpu(x->op(), x->operand(), left.result(), right.result(), x->is_strictfp());
444  }
445  break;
446
447  case Bytecodes::_frem:
448  case Bytecodes::_drem: {
449    address entry;
450    switch (x->op()) {
451    case Bytecodes::_frem:
452      entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
453      break;
454    case Bytecodes::_drem:
455      entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
456      break;
457    default:
458      ShouldNotReachHere();
459    }
460    LIR_Opr result = call_runtime(x->x(), x->y(), entry, x->type(), NULL);
461    set_result(x, result);
462  }
463  break;
464
465  default: ShouldNotReachHere();
466  }
467}
468
469
470// for  _ladd, _lmul, _lsub, _ldiv, _lrem
471void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
472  switch (x->op()) {
473  case Bytecodes::_lrem:
474  case Bytecodes::_lmul:
475  case Bytecodes::_ldiv: {
476
477    if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem) {
478      LIRItem right(x->y(), this);
479      right.load_item();
480
481      CodeEmitInfo* info = state_for(x);
482      LIR_Opr item = right.result();
483      assert(item->is_register(), "must be");
484      __ cmp(lir_cond_equal, item, LIR_OprFact::longConst(0));
485      __ branch(lir_cond_equal, T_LONG, new DivByZeroStub(info));
486    }
487
488    address entry;
489    switch (x->op()) {
490    case Bytecodes::_lrem:
491      entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
492      break; // check if dividend is 0 is done elsewhere
493    case Bytecodes::_ldiv:
494      entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
495      break; // check if dividend is 0 is done elsewhere
496    case Bytecodes::_lmul:
497      entry = CAST_FROM_FN_PTR(address, SharedRuntime::lmul);
498      break;
499    default:
500      ShouldNotReachHere();
501    }
502
503    // order of arguments to runtime call is reversed.
504    LIR_Opr result = call_runtime(x->y(), x->x(), entry, x->type(), NULL);
505    set_result(x, result);
506    break;
507  }
508  case Bytecodes::_ladd:
509  case Bytecodes::_lsub: {
510    LIRItem left(x->x(), this);
511    LIRItem right(x->y(), this);
512    left.load_item();
513    right.load_item();
514    rlock_result(x);
515
516    arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), NULL);
517    break;
518  }
519  default: ShouldNotReachHere();
520  }
521}
522
523
524// Returns if item is an int constant that can be represented by a simm13
525static bool is_simm13(LIR_Opr item) {
526  if (item->is_constant() && item->type() == T_INT) {
527    return Assembler::is_simm13(item->as_constant_ptr()->as_jint());
528  } else {
529    return false;
530  }
531}
532
533
534// for: _iadd, _imul, _isub, _idiv, _irem
535void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
536  bool is_div_rem = x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem;
537  LIRItem left(x->x(), this);
538  LIRItem right(x->y(), this);
539  // missing test if instr is commutative and if we should swap
540  right.load_nonconstant();
541  assert(right.is_constant() || right.is_register(), "wrong state of right");
542  left.load_item();
543  rlock_result(x);
544  if (is_div_rem) {
545    CodeEmitInfo* info = state_for(x);
546    LIR_Opr tmp = FrameMap::G1_opr;
547    if (x->op() == Bytecodes::_irem) {
548      __ irem(left.result(), right.result(), x->operand(), tmp, info);
549    } else if (x->op() == Bytecodes::_idiv) {
550      __ idiv(left.result(), right.result(), x->operand(), tmp, info);
551    }
552  } else {
553    arithmetic_op_int(x->op(), x->operand(), left.result(), right.result(), FrameMap::G1_opr);
554  }
555}
556
557
558void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
559  ValueTag tag = x->type()->tag();
560  assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
561  switch (tag) {
562    case floatTag:
563    case doubleTag:  do_ArithmeticOp_FPU(x);  return;
564    case longTag:    do_ArithmeticOp_Long(x); return;
565    case intTag:     do_ArithmeticOp_Int(x);  return;
566  }
567  ShouldNotReachHere();
568}
569
570
571// _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
572void LIRGenerator::do_ShiftOp(ShiftOp* x) {
573  LIRItem value(x->x(), this);
574  LIRItem count(x->y(), this);
575  // Long shift destroys count register
576  if (value.type()->is_long()) {
577    count.set_destroys_register();
578  }
579  value.load_item();
580  // the old backend doesn't support this
581  if (count.is_constant() && count.type()->as_IntConstant() != NULL && value.type()->is_int()) {
582    jint c = count.get_jint_constant() & 0x1f;
583    assert(c >= 0 && c < 32, "should be small");
584    count.dont_load_item();
585  } else {
586    count.load_item();
587  }
588  LIR_Opr reg = rlock_result(x);
589  shift_op(x->op(), reg, value.result(), count.result(), LIR_OprFact::illegalOpr);
590}
591
592
593// _iand, _land, _ior, _lor, _ixor, _lxor
594void LIRGenerator::do_LogicOp(LogicOp* x) {
595  LIRItem left(x->x(), this);
596  LIRItem right(x->y(), this);
597
598  left.load_item();
599  right.load_nonconstant();
600  LIR_Opr reg = rlock_result(x);
601
602  logic_op(x->op(), reg, left.result(), right.result());
603}
604
605
606
607// _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
608void LIRGenerator::do_CompareOp(CompareOp* x) {
609  LIRItem left(x->x(), this);
610  LIRItem right(x->y(), this);
611  left.load_item();
612  right.load_item();
613  LIR_Opr reg = rlock_result(x);
614
615  if (x->x()->type()->is_float_kind()) {
616    Bytecodes::Code code = x->op();
617    __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
618  } else if (x->x()->type()->tag() == longTag) {
619    __ lcmp2int(left.result(), right.result(), reg);
620  } else {
621    Unimplemented();
622  }
623}
624
625
626void LIRGenerator::do_AttemptUpdate(Intrinsic* x) {
627  assert(x->number_of_arguments() == 3, "wrong type");
628  LIRItem obj       (x->argument_at(0), this);  // AtomicLong object
629  LIRItem cmp_value (x->argument_at(1), this);  // value to compare with field
630  LIRItem new_value (x->argument_at(2), this);  // replace field with new_value if it matches cmp_value
631
632  obj.load_item();
633  cmp_value.load_item();
634  new_value.load_item();
635
636  // generate compare-and-swap and produce zero condition if swap occurs
637  int value_offset = sun_misc_AtomicLongCSImpl::value_offset();
638  LIR_Opr addr = FrameMap::O7_opr;
639  __ add(obj.result(), LIR_OprFact::intConst(value_offset), addr);
640  LIR_Opr t1 = FrameMap::G1_opr;  // temp for 64-bit value
641  LIR_Opr t2 = FrameMap::G3_opr;  // temp for 64-bit value
642  __ cas_long(addr, cmp_value.result(), new_value.result(), t1, t2);
643
644  // generate conditional move of boolean result
645  LIR_Opr result = rlock_result(x);
646  __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0), result);
647}
648
649
650void LIRGenerator::do_CompareAndSwap(Intrinsic* x, ValueType* type) {
651  assert(x->number_of_arguments() == 4, "wrong type");
652  LIRItem obj   (x->argument_at(0), this);  // object
653  LIRItem offset(x->argument_at(1), this);  // offset of field
654  LIRItem cmp   (x->argument_at(2), this);  // value to compare with field
655  LIRItem val   (x->argument_at(3), this);  // replace field with val if matches cmp
656
657  // Use temps to avoid kills
658  LIR_Opr t1 = FrameMap::G1_opr;
659  LIR_Opr t2 = FrameMap::G3_opr;
660  LIR_Opr addr = new_pointer_register();
661
662  // get address of field
663  obj.load_item();
664  offset.load_item();
665  cmp.load_item();
666  val.load_item();
667
668  __ add(obj.result(), offset.result(), addr);
669
670  if (type == objectType) {  // Write-barrier needed for Object fields.
671    pre_barrier(obj.result(), false, NULL);
672  }
673
674  if (type == objectType)
675    __ cas_obj(addr, cmp.result(), val.result(), t1, t2);
676  else if (type == intType)
677    __ cas_int(addr, cmp.result(), val.result(), t1, t2);
678  else if (type == longType)
679    __ cas_long(addr, cmp.result(), val.result(), t1, t2);
680  else {
681    ShouldNotReachHere();
682  }
683
684  // generate conditional move of boolean result
685  LIR_Opr result = rlock_result(x);
686  __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0), result);
687  if (type == objectType) {  // Write-barrier needed for Object fields.
688#ifdef PRECISE_CARDMARK
689    post_barrier(addr, val.result());
690#else
691    post_barrier(obj.result(), val.result());
692#endif // PRECISE_CARDMARK
693  }
694}
695
696
697void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
698  switch (x->id()) {
699    case vmIntrinsics::_dabs:
700    case vmIntrinsics::_dsqrt: {
701      assert(x->number_of_arguments() == 1, "wrong type");
702      LIRItem value(x->argument_at(0), this);
703      value.load_item();
704      LIR_Opr dst = rlock_result(x);
705
706      switch (x->id()) {
707      case vmIntrinsics::_dsqrt: {
708        __ sqrt(value.result(), dst, LIR_OprFact::illegalOpr);
709        break;
710      }
711      case vmIntrinsics::_dabs: {
712        __ abs(value.result(), dst, LIR_OprFact::illegalOpr);
713        break;
714      }
715      }
716      break;
717    }
718    case vmIntrinsics::_dlog10: // fall through
719    case vmIntrinsics::_dlog: // fall through
720    case vmIntrinsics::_dsin: // fall through
721    case vmIntrinsics::_dtan: // fall through
722    case vmIntrinsics::_dcos: {
723      assert(x->number_of_arguments() == 1, "wrong type");
724
725      address runtime_entry = NULL;
726      switch (x->id()) {
727      case vmIntrinsics::_dsin:
728        runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
729        break;
730      case vmIntrinsics::_dcos:
731        runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
732        break;
733      case vmIntrinsics::_dtan:
734        runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
735        break;
736      case vmIntrinsics::_dlog:
737        runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
738        break;
739      case vmIntrinsics::_dlog10:
740        runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
741        break;
742      default:
743        ShouldNotReachHere();
744      }
745
746      LIR_Opr result = call_runtime(x->argument_at(0), runtime_entry, x->type(), NULL);
747      set_result(x, result);
748    }
749  }
750}
751
752
753void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
754  assert(x->number_of_arguments() == 5, "wrong type");
755  // Note: spill caller save before setting the item
756  LIRItem src     (x->argument_at(0), this);
757  LIRItem src_pos (x->argument_at(1), this);
758  LIRItem dst     (x->argument_at(2), this);
759  LIRItem dst_pos (x->argument_at(3), this);
760  LIRItem length  (x->argument_at(4), this);
761  // load all values in callee_save_registers, as this makes the
762  // parameter passing to the fast case simpler
763  src.load_item_force     (rlock_callee_saved(T_OBJECT));
764  src_pos.load_item_force (rlock_callee_saved(T_INT));
765  dst.load_item_force     (rlock_callee_saved(T_OBJECT));
766  dst_pos.load_item_force (rlock_callee_saved(T_INT));
767  length.load_item_force  (rlock_callee_saved(T_INT));
768
769  int flags;
770  ciArrayKlass* expected_type;
771  arraycopy_helper(x, &flags, &expected_type);
772
773  CodeEmitInfo* info = state_for(x, x->state());
774  __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(),
775               length.result(), rlock_callee_saved(T_INT),
776               expected_type, flags, info);
777  set_no_result(x);
778}
779
780// _i2l, _i2f, _i2d, _l2i, _l2f, _l2d, _f2i, _f2l, _f2d, _d2i, _d2l, _d2f
781// _i2b, _i2c, _i2s
782void LIRGenerator::do_Convert(Convert* x) {
783
784  switch (x->op()) {
785    case Bytecodes::_f2l:
786    case Bytecodes::_d2l:
787    case Bytecodes::_d2i:
788    case Bytecodes::_l2f:
789    case Bytecodes::_l2d: {
790
791      address entry;
792      switch (x->op()) {
793      case Bytecodes::_l2f:
794        entry = CAST_FROM_FN_PTR(address, SharedRuntime::l2f);
795        break;
796      case Bytecodes::_l2d:
797        entry = CAST_FROM_FN_PTR(address, SharedRuntime::l2d);
798        break;
799      case Bytecodes::_f2l:
800        entry = CAST_FROM_FN_PTR(address, SharedRuntime::f2l);
801        break;
802      case Bytecodes::_d2l:
803        entry = CAST_FROM_FN_PTR(address, SharedRuntime::d2l);
804        break;
805      case Bytecodes::_d2i:
806        entry = CAST_FROM_FN_PTR(address, SharedRuntime::d2i);
807        break;
808      default:
809        ShouldNotReachHere();
810      }
811      LIR_Opr result = call_runtime(x->value(), entry, x->type(), NULL);
812      set_result(x, result);
813      break;
814    }
815
816    case Bytecodes::_i2f:
817    case Bytecodes::_i2d: {
818      LIRItem value(x->value(), this);
819
820      LIR_Opr reg = rlock_result(x);
821      // To convert an int to double, we need to load the 32-bit int
822      // from memory into a single precision floating point register
823      // (even numbered). Then the sparc fitod instruction takes care
824      // of the conversion. This is a bit ugly, but is the best way to
825      // get the int value in a single precision floating point register
826      value.load_item();
827      LIR_Opr tmp = force_to_spill(value.result(), T_FLOAT);
828      __ convert(x->op(), tmp, reg);
829      break;
830    }
831    break;
832
833    case Bytecodes::_i2l:
834    case Bytecodes::_i2b:
835    case Bytecodes::_i2c:
836    case Bytecodes::_i2s:
837    case Bytecodes::_l2i:
838    case Bytecodes::_f2d:
839    case Bytecodes::_d2f: { // inline code
840      LIRItem value(x->value(), this);
841
842      value.load_item();
843      LIR_Opr reg = rlock_result(x);
844      __ convert(x->op(), value.result(), reg, false);
845    }
846    break;
847
848    case Bytecodes::_f2i: {
849      LIRItem value (x->value(), this);
850      value.set_destroys_register();
851      value.load_item();
852      LIR_Opr reg = rlock_result(x);
853      set_vreg_flag(reg, must_start_in_memory);
854      __ convert(x->op(), value.result(), reg, false);
855    }
856    break;
857
858    default: ShouldNotReachHere();
859  }
860}
861
862
863void LIRGenerator::do_NewInstance(NewInstance* x) {
864  // This instruction can be deoptimized in the slow path : use
865  // O0 as result register.
866  const LIR_Opr reg = result_register_for(x->type());
867
868  if (PrintNotLoaded && !x->klass()->is_loaded()) {
869    tty->print_cr("   ###class not loaded at new bci %d", x->bci());
870  }
871  CodeEmitInfo* info = state_for(x, x->state());
872  LIR_Opr tmp1 = FrameMap::G1_oop_opr;
873  LIR_Opr tmp2 = FrameMap::G3_oop_opr;
874  LIR_Opr tmp3 = FrameMap::G4_oop_opr;
875  LIR_Opr tmp4 = FrameMap::O1_oop_opr;
876  LIR_Opr klass_reg = FrameMap::G5_oop_opr;
877  new_instance(reg, x->klass(), tmp1, tmp2, tmp3, tmp4, klass_reg, info);
878  LIR_Opr result = rlock_result(x);
879  __ move(reg, result);
880}
881
882
883void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
884  LIRItem length(x->length(), this);
885  length.load_item();
886
887  LIR_Opr reg = result_register_for(x->type());
888  LIR_Opr tmp1 = FrameMap::G1_oop_opr;
889  LIR_Opr tmp2 = FrameMap::G3_oop_opr;
890  LIR_Opr tmp3 = FrameMap::G4_oop_opr;
891  LIR_Opr tmp4 = FrameMap::O1_oop_opr;
892  LIR_Opr klass_reg = FrameMap::G5_oop_opr;
893  LIR_Opr len = length.result();
894  BasicType elem_type = x->elt_type();
895
896  __ oop2reg(ciTypeArrayKlass::make(elem_type)->encoding(), klass_reg);
897
898  CodeEmitInfo* info = state_for(x, x->state());
899  CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
900  __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path);
901
902  LIR_Opr result = rlock_result(x);
903  __ move(reg, result);
904}
905
906
907void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
908  LIRItem length(x->length(), this);
909  // in case of patching (i.e., object class is not yet loaded), we need to reexecute the instruction
910  // and therefore provide the state before the parameters have been consumed
911  CodeEmitInfo* patching_info = NULL;
912  if (!x->klass()->is_loaded() || PatchALot) {
913    patching_info = state_for(x, x->state_before());
914  }
915
916  length.load_item();
917
918  const LIR_Opr reg = result_register_for(x->type());
919  LIR_Opr tmp1 = FrameMap::G1_oop_opr;
920  LIR_Opr tmp2 = FrameMap::G3_oop_opr;
921  LIR_Opr tmp3 = FrameMap::G4_oop_opr;
922  LIR_Opr tmp4 = FrameMap::O1_oop_opr;
923  LIR_Opr klass_reg = FrameMap::G5_oop_opr;
924  LIR_Opr len = length.result();
925  CodeEmitInfo* info = state_for(x, x->state());
926
927  CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info);
928  ciObject* obj = (ciObject*) ciObjArrayKlass::make(x->klass());
929  if (obj == ciEnv::unloaded_ciobjarrayklass()) {
930    BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
931  }
932  jobject2reg_with_patching(klass_reg, obj, patching_info);
933  __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path);
934
935  LIR_Opr result = rlock_result(x);
936  __ move(reg, result);
937}
938
939
940void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
941  Values* dims = x->dims();
942  int i = dims->length();
943  LIRItemList* items = new LIRItemList(dims->length(), NULL);
944  while (i-- > 0) {
945    LIRItem* size = new LIRItem(dims->at(i), this);
946    items->at_put(i, size);
947  }
948
949  // need to get the info before, as the items may become invalid through item_free
950  CodeEmitInfo* patching_info = NULL;
951  if (!x->klass()->is_loaded() || PatchALot) {
952    patching_info = state_for(x, x->state_before());
953
954    // cannot re-use same xhandlers for multiple CodeEmitInfos, so
955    // clone all handlers
956    x->set_exception_handlers(new XHandlers(x->exception_handlers()));
957  }
958
959  i = dims->length();
960  while (i-- > 0) {
961    LIRItem* size = items->at(i);
962    // if a patching_info was generated above then debug information for the state before
963    // the call is going to be emitted.  The LIRGenerator calls above may have left some values
964    // in registers and that's been recorded in the CodeEmitInfo.  In that case the items
965    // for those values can't simply be freed if they are registers because the values
966    // might be destroyed by store_stack_parameter.  So in the case of patching, delay the
967    // freeing of the items that already were in registers
968    size->load_item();
969    store_stack_parameter (size->result(),
970                           in_ByteSize(STACK_BIAS +
971                                       (i + frame::memory_parameter_word_sp_offset) * wordSize));
972  }
973
974  // This instruction can be deoptimized in the slow path : use
975  // O0 as result register.
976  const LIR_Opr reg = result_register_for(x->type());
977  CodeEmitInfo* info = state_for(x, x->state());
978
979  jobject2reg_with_patching(reg, x->klass(), patching_info);
980  LIR_Opr rank = FrameMap::O1_opr;
981  __ move(LIR_OprFact::intConst(x->rank()), rank);
982  LIR_Opr varargs = FrameMap::as_pointer_opr(O2);
983  int offset_from_sp = (frame::memory_parameter_word_sp_offset * wordSize) + STACK_BIAS;
984  __ add(FrameMap::SP_opr,
985         LIR_OprFact::intptrConst(offset_from_sp),
986         varargs);
987  LIR_OprList* args = new LIR_OprList(3);
988  args->append(reg);
989  args->append(rank);
990  args->append(varargs);
991  __ call_runtime(Runtime1::entry_for(Runtime1::new_multi_array_id),
992                  LIR_OprFact::illegalOpr,
993                  reg, args, info);
994
995  LIR_Opr result = rlock_result(x);
996  __ move(reg, result);
997}
998
999
1000void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1001}
1002
1003
1004void LIRGenerator::do_CheckCast(CheckCast* x) {
1005  LIRItem obj(x->obj(), this);
1006  CodeEmitInfo* patching_info = NULL;
1007  if (!x->klass()->is_loaded() || (PatchALot && !x->is_incompatible_class_change_check())) {
1008    // must do this before locking the destination register as an oop register,
1009    // and before the obj is loaded (so x->obj()->item() is valid for creating a debug info location)
1010    patching_info = state_for(x, x->state_before());
1011  }
1012  obj.load_item();
1013  LIR_Opr out_reg = rlock_result(x);
1014  CodeStub* stub;
1015  CodeEmitInfo* info_for_exception = state_for(x, x->state()->copy_locks());
1016
1017  if (x->is_incompatible_class_change_check()) {
1018    assert(patching_info == NULL, "can't patch this");
1019    stub = new SimpleExceptionStub(Runtime1::throw_incompatible_class_change_error_id, LIR_OprFact::illegalOpr, info_for_exception);
1020  } else {
1021    stub = new SimpleExceptionStub(Runtime1::throw_class_cast_exception_id, obj.result(), info_for_exception);
1022  }
1023  LIR_Opr tmp1 = FrameMap::G1_oop_opr;
1024  LIR_Opr tmp2 = FrameMap::G3_oop_opr;
1025  LIR_Opr tmp3 = FrameMap::G4_oop_opr;
1026  __ checkcast(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,
1027               x->direct_compare(), info_for_exception, patching_info, stub,
1028               x->profiled_method(), x->profiled_bci());
1029}
1030
1031
1032void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1033  LIRItem obj(x->obj(), this);
1034  CodeEmitInfo* patching_info = NULL;
1035  if (!x->klass()->is_loaded() || PatchALot) {
1036    patching_info = state_for(x, x->state_before());
1037  }
1038  // ensure the result register is not the input register because the result is initialized before the patching safepoint
1039  obj.load_item();
1040  LIR_Opr out_reg = rlock_result(x);
1041  LIR_Opr tmp1 = FrameMap::G1_oop_opr;
1042  LIR_Opr tmp2 = FrameMap::G3_oop_opr;
1043  LIR_Opr tmp3 = FrameMap::G4_oop_opr;
1044  __ instanceof(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,  x->direct_compare(), patching_info);
1045}
1046
1047
1048void LIRGenerator::do_If(If* x) {
1049  assert(x->number_of_sux() == 2, "inconsistency");
1050  ValueTag tag = x->x()->type()->tag();
1051  LIRItem xitem(x->x(), this);
1052  LIRItem yitem(x->y(), this);
1053  LIRItem* xin = &xitem;
1054  LIRItem* yin = &yitem;
1055  If::Condition cond = x->cond();
1056
1057  if (tag == longTag) {
1058    // for longs, only conditions "eql", "neq", "lss", "geq" are valid;
1059    // mirror for other conditions
1060    if (cond == If::gtr || cond == If::leq) {
1061      // swap inputs
1062      cond = Instruction::mirror(cond);
1063      xin = &yitem;
1064      yin = &xitem;
1065    }
1066    xin->set_destroys_register();
1067  }
1068
1069  LIR_Opr left = LIR_OprFact::illegalOpr;
1070  LIR_Opr right = LIR_OprFact::illegalOpr;
1071
1072  xin->load_item();
1073  left = xin->result();
1074
1075  if (is_simm13(yin->result())) {
1076    // inline int constants which are small enough to be immediate operands
1077    right = LIR_OprFact::value_type(yin->value()->type());
1078  } else if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 &&
1079             (cond == If::eql || cond == If::neq)) {
1080    // inline long zero
1081    right = LIR_OprFact::value_type(yin->value()->type());
1082  } else if (tag == objectTag && yin->is_constant() && (yin->get_jobject_constant()->is_null_object())) {
1083    right = LIR_OprFact::value_type(yin->value()->type());
1084  } else {
1085    yin->load_item();
1086    right = yin->result();
1087  }
1088  set_no_result(x);
1089
1090  // add safepoint before generating condition code so it can be recomputed
1091  if (x->is_safepoint()) {
1092    // increment backedge counter if needed
1093    increment_backedge_counter(state_for(x, x->state_before()));
1094
1095    __ safepoint(new_register(T_INT), state_for(x, x->state_before()));
1096  }
1097
1098  __ cmp(lir_cond(cond), left, right);
1099  profile_branch(x, cond);
1100  move_to_phi(x->state());
1101  if (x->x()->type()->is_float_kind()) {
1102    __ branch(lir_cond(cond), right->type(), x->tsux(), x->usux());
1103  } else {
1104    __ branch(lir_cond(cond), right->type(), x->tsux());
1105  }
1106  assert(x->default_sux() == x->fsux(), "wrong destination above");
1107  __ jump(x->default_sux());
1108}
1109
1110
1111LIR_Opr LIRGenerator::getThreadPointer() {
1112  return FrameMap::as_pointer_opr(G2);
1113}
1114
1115
1116void LIRGenerator::trace_block_entry(BlockBegin* block) {
1117  __ move(LIR_OprFact::intConst(block->block_id()), FrameMap::O0_opr);
1118  LIR_OprList* args = new LIR_OprList(1);
1119  args->append(FrameMap::O0_opr);
1120  address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1121  __ call_runtime_leaf(func, rlock_callee_saved(T_INT), LIR_OprFact::illegalOpr, args);
1122}
1123
1124
1125void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1126                                        CodeEmitInfo* info) {
1127#ifdef _LP64
1128  __ store(value, address, info);
1129#else
1130  __ volatile_store_mem_reg(value, address, info);
1131#endif
1132}
1133
1134void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1135                                       CodeEmitInfo* info) {
1136#ifdef _LP64
1137  __ load(address, result, info);
1138#else
1139  __ volatile_load_mem_reg(address, result, info);
1140#endif
1141}
1142
1143
1144void LIRGenerator::put_Object_unsafe(LIR_Opr src, LIR_Opr offset, LIR_Opr data,
1145                                     BasicType type, bool is_volatile) {
1146  LIR_Opr base_op = src;
1147  LIR_Opr index_op = offset;
1148
1149  bool is_obj = (type == T_ARRAY || type == T_OBJECT);
1150#ifndef _LP64
1151  if (is_volatile && type == T_LONG) {
1152    __ volatile_store_unsafe_reg(data, src, offset, type, NULL, lir_patch_none);
1153  } else
1154#endif
1155    {
1156      if (type == T_BOOLEAN) {
1157        type = T_BYTE;
1158      }
1159      LIR_Address* addr;
1160      if (type == T_ARRAY || type == T_OBJECT) {
1161        LIR_Opr tmp = new_pointer_register();
1162        __ add(base_op, index_op, tmp);
1163        addr = new LIR_Address(tmp, 0, type);
1164      } else {
1165        addr = new LIR_Address(base_op, index_op, type);
1166      }
1167
1168      if (is_obj) {
1169        pre_barrier(LIR_OprFact::address(addr), false, NULL);
1170        // _bs->c1_write_barrier_pre(this, LIR_OprFact::address(addr));
1171      }
1172      __ move(data, addr);
1173      if (is_obj) {
1174        // This address is precise
1175        post_barrier(LIR_OprFact::address(addr), data);
1176      }
1177    }
1178}
1179
1180
1181void LIRGenerator::get_Object_unsafe(LIR_Opr dst, LIR_Opr src, LIR_Opr offset,
1182                                     BasicType type, bool is_volatile) {
1183#ifndef _LP64
1184  if (is_volatile && type == T_LONG) {
1185    __ volatile_load_unsafe_reg(src, offset, dst, type, NULL, lir_patch_none);
1186  } else
1187#endif
1188    {
1189    LIR_Address* addr = new LIR_Address(src, offset, type);
1190    __ load(addr, dst);
1191  }
1192}
1193