420 void do_Return (Return* x);
421 void do_Throw (Throw* x);
422 void do_Base (Base* x);
423 void do_OsrEntry (OsrEntry* x);
424 void do_ExceptionObject(ExceptionObject* x);
425 void do_RoundFP (RoundFP* x);
426 void do_UnsafeGetRaw (UnsafeGetRaw* x);
427 void do_UnsafePutRaw (UnsafePutRaw* x);
428 void do_UnsafeGetObject(UnsafeGetObject* x);
429 void do_UnsafePutObject(UnsafePutObject* x);
430 void do_UnsafePrefetchRead (UnsafePrefetchRead* x);
431 void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x);
432 void do_ProfileCall (ProfileCall* x);
433 void do_ProfileCounter (ProfileCounter* x);
434 };
435
436
437 // Because of a static contained within (for the purpose of iteration
438 // over instructions), it is only valid to have one of these active at
439 // a time
440 class NullCheckEliminator {
441 private:
442 static NullCheckEliminator* _static_nce;
443 static void do_value(Value* vp);
444
445 Optimizer* _opt;
446
447 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
448 BlockList* _work_list; // Basic blocks to visit
449
450 bool visitable(Value x) {
451 assert(_visitable_instructions != NULL, "check");
452 return _visitable_instructions->contains(x);
453 }
454 void mark_visited(Value x) {
455 assert(_visitable_instructions != NULL, "check");
456 _visitable_instructions->remove(x);
457 }
458 void mark_visitable(Value x) {
459 assert(_visitable_instructions != NULL, "check");
460 _visitable_instructions->put(x);
461 }
462 void clear_visitable_state() {
463 assert(_visitable_instructions != NULL, "check");
464 _visitable_instructions->clear();
487 ValueSet* incoming_state);
488
489 public:
490 // constructor
491 NullCheckEliminator(Optimizer* opt)
492 : _opt(opt)
493 , _set(new ValueSet())
494 , _last_explicit_null_check(NULL)
495 , _block_states(BlockBegin::number_of_blocks(), NULL)
496 , _work_list(new BlockList()) {
497 _visitable_instructions = new ValueSet();
498 _visitor.set_eliminator(this);
499 }
500
501 Optimizer* opt() { return _opt; }
502 IR* ir () { return opt()->ir(); }
503
504 // Process a graph
505 void iterate(BlockBegin* root);
506
507 // In some situations (like NullCheck(x); getfield(x)) the debug
508 // information from the explicit NullCheck can be used to populate
509 // the getfield, even if the two instructions are in different
510 // scopes; this allows implicit null checks to be used but the
511 // correct exception information to be generated. We must clear the
512 // last-traversed NullCheck when we reach a potentially-exception-
513 // throwing instruction, as well as in some other cases.
514 void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; }
515 NullCheck* last_explicit_null_check() { return _last_explicit_null_check; }
516 Value last_explicit_null_check_obj() { return (_last_explicit_null_check
517 ? _last_explicit_null_check->obj()
518 : NULL); }
519 NullCheck* consume_last_explicit_null_check() {
520 _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
521 _last_explicit_null_check->set_can_trap(false);
522 return _last_explicit_null_check;
523 }
524 void clear_last_explicit_null_check() { _last_explicit_null_check = NULL; }
525
526 // Handlers for relevant instructions
585 void NullCheckVisitor::do_If (If* x) {}
586 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {}
587 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
588 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
589 void NullCheckVisitor::do_Return (Return* x) {}
590 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
591 void NullCheckVisitor::do_Base (Base* x) {}
592 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
593 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
594 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
595 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {}
596 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {}
597 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
598 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
599 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
600 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
601 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check(); }
602 void NullCheckVisitor::do_ProfileCounter (ProfileCounter* x) {}
603
604
605 NullCheckEliminator* NullCheckEliminator::_static_nce = NULL;
606
607
608 void NullCheckEliminator::do_value(Value* p) {
609 assert(*p != NULL, "should not find NULL instructions");
610 if (_static_nce->visitable(*p)) {
611 _static_nce->mark_visited(*p);
612 (*p)->visit(&_static_nce->_visitor);
613 }
614 }
615
616 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
617 ValueSet* state = state_for(block);
618 if (state == NULL) {
619 state = incoming_state->copy();
620 set_state_for(block, state);
621 return true;
622 } else {
623 bool changed = state->set_intersect(incoming_state);
624 if (PrintNullCheckElimination && changed) {
625 tty->print_cr("Block %d's null check state changed", block->block_id());
626 }
627 return changed;
628 }
629 }
630
631
632 void NullCheckEliminator::iterate_all() {
633 while (work_list()->length() > 0) {
634 iterate_one(work_list()->pop());
635 }
636 }
637
638
639 void NullCheckEliminator::iterate_one(BlockBegin* block) {
640 _static_nce = this;
641 clear_visitable_state();
642 // clear out an old explicit null checks
643 set_last_explicit_null_check(NULL);
644
645 if (PrintNullCheckElimination) {
646 tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s",
647 block->block_id(),
648 ir()->method()->holder()->name()->as_utf8(),
649 ir()->method()->name()->as_utf8(),
650 ir()->method()->signature()->as_symbol()->as_utf8());
651 }
652
653 // Create new state if none present (only happens at root)
654 if (state_for(block) == NULL) {
655 ValueSet* tmp_state = new ValueSet();
656 set_state_for(block, tmp_state);
657 // Initial state is that local 0 (receiver) is non-null for
658 // non-static methods
659 ValueStack* stack = block->state();
660 IRScope* scope = stack->scope();
695 // them on first seems to help reduce the amount of iteration to
696 // reach a fixed point.
697 for (i = 0; i < block->number_of_exception_handlers(); i++) {
698 BlockBegin* next = block->exception_handler_at(i);
699 if (merge_state_for(next, state())) {
700 if (!work_list()->contains(next)) {
701 work_list()->push(next);
702 }
703 }
704 }
705
706 // Iterate through block, updating state.
707 for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
708 // Mark instructions in this block as visitable as they are seen
709 // in the instruction list. This keeps the iteration from
710 // visiting instructions which are references in other blocks or
711 // visiting instructions more than once.
712 mark_visitable(instr);
713 if (instr->is_root() || instr->can_trap() || (instr->as_NullCheck() != NULL)) {
714 mark_visited(instr);
715 instr->input_values_do(&NullCheckEliminator::do_value);
716 instr->visit(&_visitor);
717 }
718 }
719
720 // Propagate state to successors if necessary
721 for (i = 0; i < e->number_of_sux(); i++) {
722 BlockBegin* next = e->sux_at(i);
723 if (merge_state_for(next, state())) {
724 if (!work_list()->contains(next)) {
725 work_list()->push(next);
726 }
727 }
728 }
729 }
730
731
732 void NullCheckEliminator::iterate(BlockBegin* block) {
733 work_list()->push(block);
734 iterate_all();
735 }
|
420 void do_Return (Return* x);
421 void do_Throw (Throw* x);
422 void do_Base (Base* x);
423 void do_OsrEntry (OsrEntry* x);
424 void do_ExceptionObject(ExceptionObject* x);
425 void do_RoundFP (RoundFP* x);
426 void do_UnsafeGetRaw (UnsafeGetRaw* x);
427 void do_UnsafePutRaw (UnsafePutRaw* x);
428 void do_UnsafeGetObject(UnsafeGetObject* x);
429 void do_UnsafePutObject(UnsafePutObject* x);
430 void do_UnsafePrefetchRead (UnsafePrefetchRead* x);
431 void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x);
432 void do_ProfileCall (ProfileCall* x);
433 void do_ProfileCounter (ProfileCounter* x);
434 };
435
436
437 // Because of a static contained within (for the purpose of iteration
438 // over instructions), it is only valid to have one of these active at
439 // a time
440 class NullCheckEliminator: public ValueVisitor {
441 private:
442 Optimizer* _opt;
443
444 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
445 BlockList* _work_list; // Basic blocks to visit
446
447 bool visitable(Value x) {
448 assert(_visitable_instructions != NULL, "check");
449 return _visitable_instructions->contains(x);
450 }
451 void mark_visited(Value x) {
452 assert(_visitable_instructions != NULL, "check");
453 _visitable_instructions->remove(x);
454 }
455 void mark_visitable(Value x) {
456 assert(_visitable_instructions != NULL, "check");
457 _visitable_instructions->put(x);
458 }
459 void clear_visitable_state() {
460 assert(_visitable_instructions != NULL, "check");
461 _visitable_instructions->clear();
484 ValueSet* incoming_state);
485
486 public:
487 // constructor
488 NullCheckEliminator(Optimizer* opt)
489 : _opt(opt)
490 , _set(new ValueSet())
491 , _last_explicit_null_check(NULL)
492 , _block_states(BlockBegin::number_of_blocks(), NULL)
493 , _work_list(new BlockList()) {
494 _visitable_instructions = new ValueSet();
495 _visitor.set_eliminator(this);
496 }
497
498 Optimizer* opt() { return _opt; }
499 IR* ir () { return opt()->ir(); }
500
501 // Process a graph
502 void iterate(BlockBegin* root);
503
504 void visit(Value* f);
505
506 // In some situations (like NullCheck(x); getfield(x)) the debug
507 // information from the explicit NullCheck can be used to populate
508 // the getfield, even if the two instructions are in different
509 // scopes; this allows implicit null checks to be used but the
510 // correct exception information to be generated. We must clear the
511 // last-traversed NullCheck when we reach a potentially-exception-
512 // throwing instruction, as well as in some other cases.
513 void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; }
514 NullCheck* last_explicit_null_check() { return _last_explicit_null_check; }
515 Value last_explicit_null_check_obj() { return (_last_explicit_null_check
516 ? _last_explicit_null_check->obj()
517 : NULL); }
518 NullCheck* consume_last_explicit_null_check() {
519 _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
520 _last_explicit_null_check->set_can_trap(false);
521 return _last_explicit_null_check;
522 }
523 void clear_last_explicit_null_check() { _last_explicit_null_check = NULL; }
524
525 // Handlers for relevant instructions
584 void NullCheckVisitor::do_If (If* x) {}
585 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {}
586 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
587 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
588 void NullCheckVisitor::do_Return (Return* x) {}
589 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
590 void NullCheckVisitor::do_Base (Base* x) {}
591 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
592 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
593 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
594 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {}
595 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {}
596 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
597 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
598 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
599 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
600 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check(); }
601 void NullCheckVisitor::do_ProfileCounter (ProfileCounter* x) {}
602
603
604 void NullCheckEliminator::visit(Value* p) {
605 assert(*p != NULL, "should not find NULL instructions");
606 if (visitable(*p)) {
607 mark_visited(*p);
608 (*p)->visit(&_visitor);
609 }
610 }
611
612 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
613 ValueSet* state = state_for(block);
614 if (state == NULL) {
615 state = incoming_state->copy();
616 set_state_for(block, state);
617 return true;
618 } else {
619 bool changed = state->set_intersect(incoming_state);
620 if (PrintNullCheckElimination && changed) {
621 tty->print_cr("Block %d's null check state changed", block->block_id());
622 }
623 return changed;
624 }
625 }
626
627
628 void NullCheckEliminator::iterate_all() {
629 while (work_list()->length() > 0) {
630 iterate_one(work_list()->pop());
631 }
632 }
633
634
635 void NullCheckEliminator::iterate_one(BlockBegin* block) {
636 clear_visitable_state();
637 // clear out an old explicit null checks
638 set_last_explicit_null_check(NULL);
639
640 if (PrintNullCheckElimination) {
641 tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s",
642 block->block_id(),
643 ir()->method()->holder()->name()->as_utf8(),
644 ir()->method()->name()->as_utf8(),
645 ir()->method()->signature()->as_symbol()->as_utf8());
646 }
647
648 // Create new state if none present (only happens at root)
649 if (state_for(block) == NULL) {
650 ValueSet* tmp_state = new ValueSet();
651 set_state_for(block, tmp_state);
652 // Initial state is that local 0 (receiver) is non-null for
653 // non-static methods
654 ValueStack* stack = block->state();
655 IRScope* scope = stack->scope();
690 // them on first seems to help reduce the amount of iteration to
691 // reach a fixed point.
692 for (i = 0; i < block->number_of_exception_handlers(); i++) {
693 BlockBegin* next = block->exception_handler_at(i);
694 if (merge_state_for(next, state())) {
695 if (!work_list()->contains(next)) {
696 work_list()->push(next);
697 }
698 }
699 }
700
701 // Iterate through block, updating state.
702 for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
703 // Mark instructions in this block as visitable as they are seen
704 // in the instruction list. This keeps the iteration from
705 // visiting instructions which are references in other blocks or
706 // visiting instructions more than once.
707 mark_visitable(instr);
708 if (instr->is_root() || instr->can_trap() || (instr->as_NullCheck() != NULL)) {
709 mark_visited(instr);
710 instr->input_values_do(this);
711 instr->visit(&_visitor);
712 }
713 }
714
715 // Propagate state to successors if necessary
716 for (i = 0; i < e->number_of_sux(); i++) {
717 BlockBegin* next = e->sux_at(i);
718 if (merge_state_for(next, state())) {
719 if (!work_list()->contains(next)) {
720 work_list()->push(next);
721 }
722 }
723 }
724 }
725
726
727 void NullCheckEliminator::iterate(BlockBegin* block) {
728 work_list()->push(block);
729 iterate_all();
730 }
|