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