Mercurial > hg > truffle
comparison src/share/vm/opto/block.cpp @ 418:72c5366e5d86
6743900: frequency based block layout
Summary: post-register allocation pass that drives block layout by edge frequencies
Reviewed-by: never, kvn
author | rasbold |
---|---|
date | Thu, 06 Nov 2008 14:59:10 -0800 |
parents | 9ee9cf798b59 |
children | 91263420e1c6 |
comparison
equal
deleted
inserted
replaced
417:f4fe12e429a4 | 418:72c5366e5d86 |
---|---|
55 push(b); // grow list by one block | 55 push(b); // grow list by one block |
56 Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*))); | 56 Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*))); |
57 _blocks[i] = b; | 57 _blocks[i] = b; |
58 } | 58 } |
59 | 59 |
60 #ifndef PRODUCT | |
61 void Block_List::print() { | |
62 for (uint i=0; i < size(); i++) { | |
63 tty->print("B%d ", _blocks[i]->_pre_order); | |
64 } | |
65 tty->print("size = %d\n", size()); | |
66 } | |
67 #endif | |
60 | 68 |
61 //============================================================================= | 69 //============================================================================= |
62 | 70 |
63 uint Block::code_alignment() { | 71 uint Block::code_alignment() { |
64 // Check for Root block | 72 // Check for Root block |
65 if( _pre_order == 0 ) return CodeEntryAlignment; | 73 if( _pre_order == 0 ) return CodeEntryAlignment; |
66 // Check for Start block | 74 // Check for Start block |
67 if( _pre_order == 1 ) return InteriorEntryAlignment; | 75 if( _pre_order == 1 ) return InteriorEntryAlignment; |
68 // Check for loop alignment | 76 // Check for loop alignment |
77 if (has_loop_alignment()) return loop_alignment(); | |
78 | |
79 return 1; // no particular alignment | |
80 } | |
81 | |
82 uint Block::compute_loop_alignment() { | |
69 Node *h = head(); | 83 Node *h = head(); |
70 if( h->is_Loop() && h->as_Loop()->is_inner_loop() ) { | 84 if( h->is_Loop() && h->as_Loop()->is_inner_loop() ) { |
71 // Pre- and post-loops have low trip count so do not bother with | 85 // Pre- and post-loops have low trip count so do not bother with |
72 // NOPs for align loop head. The constants are hidden from tuning | 86 // NOPs for align loop head. The constants are hidden from tuning |
73 // but only because my "divide by 4" heuristic surely gets nearly | 87 // but only because my "divide by 4" heuristic surely gets nearly |
81 if( n->is_MachIf() && n->as_MachIf()->_prob < 0.01 ) { | 95 if( n->is_MachIf() && n->as_MachIf()->_prob < 0.01 ) { |
82 return 1; // Loop does not loop, more often than not! | 96 return 1; // Loop does not loop, more often than not! |
83 } | 97 } |
84 return OptoLoopAlignment; // Otherwise align loop head | 98 return OptoLoopAlignment; // Otherwise align loop head |
85 } | 99 } |
100 | |
86 return 1; // no particular alignment | 101 return 1; // no particular alignment |
87 } | 102 } |
88 | 103 |
89 //----------------------------------------------------------------------------- | 104 //----------------------------------------------------------------------------- |
90 // Compute the size of first 'inst_cnt' instructions in this block. | 105 // Compute the size of first 'inst_cnt' instructions in this block. |
91 // Return the number of instructions left to compute if the block has | 106 // Return the number of instructions left to compute if the block has |
92 // less then 'inst_cnt' instructions. | 107 // less then 'inst_cnt' instructions. Stop, and return 0 if sum_size |
108 // exceeds OptoLoopAlignment. | |
93 uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt, | 109 uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt, |
94 PhaseRegAlloc* ra) { | 110 PhaseRegAlloc* ra) { |
95 uint last_inst = _nodes.size(); | 111 uint last_inst = _nodes.size(); |
96 for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) { | 112 for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) { |
97 uint inst_size = _nodes[j]->size(ra); | 113 uint inst_size = _nodes[j]->size(ra); |
305 bx = (*bbs)[bx->pred(1)->_idx]; | 321 bx = (*bbs)[bx->pred(1)->_idx]; |
306 } | 322 } |
307 tty->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order); | 323 tty->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order); |
308 // Dump any loop-specific bits, especially for CountedLoops. | 324 // Dump any loop-specific bits, especially for CountedLoops. |
309 loop->dump_spec(tty); | 325 loop->dump_spec(tty); |
326 } else if (has_loop_alignment()) { | |
327 tty->print(" top-of-loop"); | |
310 } | 328 } |
311 tty->print(" Freq: %g",_freq); | 329 tty->print(" Freq: %g",_freq); |
312 if( Verbose || WizardMode ) { | 330 if( Verbose || WizardMode ) { |
313 tty->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth); | 331 tty->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth); |
314 tty->print(" RegPressure: %d",_reg_pressure); | 332 tty->print(" RegPressure: %d",_reg_pressure); |
507 // flipped for another case? | 525 // flipped for another case? |
508 static bool no_flip_branch( Block *b ) { | 526 static bool no_flip_branch( Block *b ) { |
509 int branch_idx = b->_nodes.size() - b->_num_succs-1; | 527 int branch_idx = b->_nodes.size() - b->_num_succs-1; |
510 if( branch_idx < 1 ) return false; | 528 if( branch_idx < 1 ) return false; |
511 Node *bra = b->_nodes[branch_idx]; | 529 Node *bra = b->_nodes[branch_idx]; |
512 if( bra->is_Catch() ) return true; | 530 if( bra->is_Catch() ) |
531 return true; | |
513 if( bra->is_Mach() ) { | 532 if( bra->is_Mach() ) { |
514 if( bra->is_MachNullCheck() ) return true; | 533 if( bra->is_MachNullCheck() ) |
534 return true; | |
515 int iop = bra->as_Mach()->ideal_Opcode(); | 535 int iop = bra->as_Mach()->ideal_Opcode(); |
516 if( iop == Op_FastLock || iop == Op_FastUnlock ) | 536 if( iop == Op_FastLock || iop == Op_FastUnlock ) |
517 return true; | 537 return true; |
518 } | 538 } |
519 return false; | 539 return false; |
555 dead->head()->del_req(j); | 575 dead->head()->del_req(j); |
556 for( int k = 1; dead->_nodes[k]->is_Phi(); k++ ) | 576 for( int k = 1; dead->_nodes[k]->is_Phi(); k++ ) |
557 dead->_nodes[k]->del_req(j); | 577 dead->_nodes[k]->del_req(j); |
558 } | 578 } |
559 | 579 |
560 //------------------------------MoveToNext------------------------------------- | 580 //------------------------------move_to_next----------------------------------- |
561 // Helper function to move block bx to the slot following b_index. Return | 581 // Helper function to move block bx to the slot following b_index. Return |
562 // true if the move is successful, otherwise false | 582 // true if the move is successful, otherwise false |
563 bool PhaseCFG::MoveToNext(Block* bx, uint b_index) { | 583 bool PhaseCFG::move_to_next(Block* bx, uint b_index) { |
564 if (bx == NULL) return false; | 584 if (bx == NULL) return false; |
565 | 585 |
566 // Return false if bx is already scheduled. | 586 // Return false if bx is already scheduled. |
567 uint bx_index = bx->_pre_order; | 587 uint bx_index = bx->_pre_order; |
568 if ((bx_index <= b_index) && (_blocks[bx_index] == bx)) { | 588 if ((bx_index <= b_index) && (_blocks[bx_index] == bx)) { |
589 _blocks.remove(bx_index); | 609 _blocks.remove(bx_index); |
590 _blocks.insert(b_index + 1, bx); | 610 _blocks.insert(b_index + 1, bx); |
591 return true; | 611 return true; |
592 } | 612 } |
593 | 613 |
594 //------------------------------MoveToEnd-------------------------------------- | 614 //------------------------------move_to_end------------------------------------ |
595 // Move empty and uncommon blocks to the end. | 615 // Move empty and uncommon blocks to the end. |
596 void PhaseCFG::MoveToEnd(Block *b, uint i) { | 616 void PhaseCFG::move_to_end(Block *b, uint i) { |
597 int e = b->is_Empty(); | 617 int e = b->is_Empty(); |
598 if (e != Block::not_empty) { | 618 if (e != Block::not_empty) { |
599 if (e == Block::empty_with_goto) { | 619 if (e == Block::empty_with_goto) { |
600 // Remove the goto, but leave the block. | 620 // Remove the goto, but leave the block. |
601 b->_nodes.pop(); | 621 b->_nodes.pop(); |
607 // Move the empty block to the end, and don't recheck. | 627 // Move the empty block to the end, and don't recheck. |
608 _blocks.remove(i); | 628 _blocks.remove(i); |
609 _blocks.push(b); | 629 _blocks.push(b); |
610 } | 630 } |
611 | 631 |
612 //------------------------------RemoveEmpty------------------------------------ | 632 //---------------------------set_loop_alignment-------------------------------- |
613 // Remove empty basic blocks and useless branches. | 633 // Set loop alignment for every block |
614 void PhaseCFG::RemoveEmpty() { | 634 void PhaseCFG::set_loop_alignment() { |
635 uint last = _num_blocks; | |
636 assert( _blocks[0] == _broot, "" ); | |
637 | |
638 for (uint i = 1; i < last; i++ ) { | |
639 Block *b = _blocks[i]; | |
640 if (b->head()->is_Loop()) { | |
641 b->set_loop_alignment(b); | |
642 } | |
643 } | |
644 } | |
645 | |
646 //-----------------------------remove_empty------------------------------------ | |
647 // Make empty basic blocks to be "connector" blocks, Move uncommon blocks | |
648 // to the end. | |
649 void PhaseCFG::remove_empty() { | |
615 // Move uncommon blocks to the end | 650 // Move uncommon blocks to the end |
616 uint last = _num_blocks; | 651 uint last = _num_blocks; |
617 uint i; | |
618 assert( _blocks[0] == _broot, "" ); | 652 assert( _blocks[0] == _broot, "" ); |
619 for( i = 1; i < last; i++ ) { | 653 |
654 for (uint i = 1; i < last; i++) { | |
620 Block *b = _blocks[i]; | 655 Block *b = _blocks[i]; |
656 if (b->is_connector()) break; | |
621 | 657 |
622 // Check for NeverBranch at block end. This needs to become a GOTO to the | 658 // Check for NeverBranch at block end. This needs to become a GOTO to the |
623 // true target. NeverBranch are treated as a conditional branch that | 659 // true target. NeverBranch are treated as a conditional branch that |
624 // always goes the same direction for most of the optimizer and are used | 660 // always goes the same direction for most of the optimizer and are used |
625 // to give a fake exit path to infinite loops. At this late stage they | 661 // to give a fake exit path to infinite loops. At this late stage they |
627 // indeed hang. | 663 // indeed hang. |
628 if( b->_nodes[b->end_idx()]->Opcode() == Op_NeverBranch ) | 664 if( b->_nodes[b->end_idx()]->Opcode() == Op_NeverBranch ) |
629 convert_NeverBranch_to_Goto(b); | 665 convert_NeverBranch_to_Goto(b); |
630 | 666 |
631 // Look for uncommon blocks and move to end. | 667 // Look for uncommon blocks and move to end. |
632 if( b->is_uncommon(_bbs) ) { | 668 if (!C->do_freq_based_layout()) { |
633 MoveToEnd(b, i); | 669 if( b->is_uncommon(_bbs) ) { |
634 last--; // No longer check for being uncommon! | 670 move_to_end(b, i); |
635 if( no_flip_branch(b) ) { // Fall-thru case must follow? | 671 last--; // No longer check for being uncommon! |
636 b = _blocks[i]; // Find the fall-thru block | 672 if( no_flip_branch(b) ) { // Fall-thru case must follow? |
637 MoveToEnd(b, i); | 673 b = _blocks[i]; // Find the fall-thru block |
638 last--; | 674 move_to_end(b, i); |
639 } | 675 last--; |
640 i--; // backup block counter post-increment | 676 } |
641 } | 677 i--; // backup block counter post-increment |
642 } | 678 } |
643 | 679 } |
644 // Remove empty blocks | 680 } |
645 uint j1; | 681 |
682 // Move empty blocks to the end | |
646 last = _num_blocks; | 683 last = _num_blocks; |
647 for( i=0; i < last; i++ ) { | 684 for (uint i = 1; i < last; i++) { |
648 Block *b = _blocks[i]; | 685 Block *b = _blocks[i]; |
649 if (i > 0) { | 686 if (b->is_Empty() != Block::not_empty) { |
650 if (b->is_Empty() != Block::not_empty) { | 687 move_to_end(b, i); |
651 MoveToEnd(b, i); | 688 last--; |
652 last--; | 689 i--; |
653 i--; | |
654 } | |
655 } | 690 } |
656 } // End of for all blocks | 691 } // End of for all blocks |
657 | 692 } |
693 | |
694 //-----------------------------fixup_flow-------------------------------------- | |
695 // Fix up the final control flow for basic blocks. | |
696 void PhaseCFG::fixup_flow() { | |
658 // Fixup final control flow for the blocks. Remove jump-to-next | 697 // Fixup final control flow for the blocks. Remove jump-to-next |
659 // block. If neither arm of a IF follows the conditional branch, we | 698 // block. If neither arm of a IF follows the conditional branch, we |
660 // have to add a second jump after the conditional. We place the | 699 // have to add a second jump after the conditional. We place the |
661 // TRUE branch target in succs[0] for both GOTOs and IFs. | 700 // TRUE branch target in succs[0] for both GOTOs and IFs. |
662 for( i=0; i < _num_blocks; i++ ) { | 701 for (uint i=0; i < _num_blocks; i++) { |
663 Block *b = _blocks[i]; | 702 Block *b = _blocks[i]; |
664 b->_pre_order = i; // turn pre-order into block-index | 703 b->_pre_order = i; // turn pre-order into block-index |
665 | 704 |
666 // Connector blocks need no further processing. | 705 // Connector blocks need no further processing. |
667 if (b->is_connector()) { | 706 if (b->is_connector()) { |
698 } | 737 } |
699 break; | 738 break; |
700 } | 739 } |
701 } | 740 } |
702 // Remove all CatchProjs | 741 // Remove all CatchProjs |
703 for (j1 = 0; j1 < b->_num_succs; j1++) b->_nodes.pop(); | 742 for (uint j1 = 0; j1 < b->_num_succs; j1++) b->_nodes.pop(); |
704 | 743 |
705 } else if (b->_num_succs == 1) { | 744 } else if (b->_num_succs == 1) { |
706 // Block ends in a Goto? | 745 // Block ends in a Goto? |
707 if (bnext == bs0) { | 746 if (bnext == bs0) { |
708 // We fall into next block; remove the Goto | 747 // We fall into next block; remove the Goto |
728 // Check for neither successor block following the current | 767 // Check for neither successor block following the current |
729 // block ending in a conditional. If so, move one of the | 768 // block ending in a conditional. If so, move one of the |
730 // successors after the current one, provided that the | 769 // successors after the current one, provided that the |
731 // successor was previously unscheduled, but moveable | 770 // successor was previously unscheduled, but moveable |
732 // (i.e., all paths to it involve a branch). | 771 // (i.e., all paths to it involve a branch). |
733 if( bnext != bs0 && bnext != bs1 ) { | 772 if( !C->do_freq_based_layout() && bnext != bs0 && bnext != bs1 ) { |
734 | |
735 // Choose the more common successor based on the probability | 773 // Choose the more common successor based on the probability |
736 // of the conditional branch. | 774 // of the conditional branch. |
737 Block *bx = bs0; | 775 Block *bx = bs0; |
738 Block *by = bs1; | 776 Block *by = bs1; |
739 | 777 |
749 bx = bs1; | 787 bx = bs1; |
750 by = bs0; | 788 by = bs0; |
751 } | 789 } |
752 | 790 |
753 // Attempt the more common successor first | 791 // Attempt the more common successor first |
754 if (MoveToNext(bx, i)) { | 792 if (move_to_next(bx, i)) { |
755 bnext = bx; | 793 bnext = bx; |
756 } else if (MoveToNext(by, i)) { | 794 } else if (move_to_next(by, i)) { |
757 bnext = by; | 795 bnext = by; |
758 } | 796 } |
759 } | 797 } |
760 | 798 |
761 // Check for conditional branching the wrong way. Negate | 799 // Check for conditional branching the wrong way. Negate |
772 b->_succs.map( 0, tbs1 ); | 810 b->_succs.map( 0, tbs1 ); |
773 b->_succs.map( 1, tbs0 ); | 811 b->_succs.map( 1, tbs0 ); |
774 // Flip projection for each target | 812 // Flip projection for each target |
775 { ProjNode *tmp = proj0; proj0 = proj1; proj1 = tmp; } | 813 { ProjNode *tmp = proj0; proj0 = proj1; proj1 = tmp; } |
776 | 814 |
777 } else if( bnext == bs1 ) { // Fall-thru is already in succs[1] | 815 } else if( bnext != bs1 ) { |
778 | 816 // Need a double-branch |
779 } else { // Else need a double-branch | |
780 | |
781 // The existing conditional branch need not change. | 817 // The existing conditional branch need not change. |
782 // Add a unconditional branch to the false target. | 818 // Add a unconditional branch to the false target. |
783 // Alas, it must appear in its own block and adding a | 819 // Alas, it must appear in its own block and adding a |
784 // block this late in the game is complicated. Sigh. | 820 // block this late in the game is complicated. Sigh. |
785 insert_goto_at(i, 1); | 821 insert_goto_at(i, 1); |
786 } | 822 } |
787 | 823 |
788 // Make sure we TRUE branch to the target | 824 // Make sure we TRUE branch to the target |
789 if( proj0->Opcode() == Op_IfFalse ) | 825 if( proj0->Opcode() == Op_IfFalse ) { |
790 iff->negate(); | 826 iff->negate(); |
827 } | |
791 | 828 |
792 b->_nodes.pop(); // Remove IfFalse & IfTrue projections | 829 b->_nodes.pop(); // Remove IfFalse & IfTrue projections |
793 b->_nodes.pop(); | 830 b->_nodes.pop(); |
794 | 831 |
795 } else { | 832 } else { |
796 // Multi-exit block, e.g. a switch statement | 833 // Multi-exit block, e.g. a switch statement |
797 // But we don't need to do anything here | 834 // But we don't need to do anything here |
798 } | 835 } |
799 | |
800 } // End of for all blocks | 836 } // End of for all blocks |
801 | |
802 } | 837 } |
803 | 838 |
804 | 839 |
805 //------------------------------dump------------------------------------------- | 840 //------------------------------dump------------------------------------------- |
806 #ifndef PRODUCT | 841 #ifndef PRODUCT |
903 void UnionFind::reset( uint max ) { | 938 void UnionFind::reset( uint max ) { |
904 assert( max <= max_uint, "Must fit within uint" ); | 939 assert( max <= max_uint, "Must fit within uint" ); |
905 // Force the Union-Find mapping to be at least this large | 940 // Force the Union-Find mapping to be at least this large |
906 extend(max,0); | 941 extend(max,0); |
907 // Initialize to be the ID mapping. | 942 // Initialize to be the ID mapping. |
908 for( uint i=0; i<_max; i++ ) map(i,i); | 943 for( uint i=0; i<max; i++ ) map(i,i); |
909 } | 944 } |
910 | 945 |
911 //------------------------------Find_compress---------------------------------- | 946 //------------------------------Find_compress---------------------------------- |
912 // Straight out of Tarjan's union-find algorithm | 947 // Straight out of Tarjan's union-find algorithm |
913 uint UnionFind::Find_compress( uint idx ) { | 948 uint UnionFind::Find_compress( uint idx ) { |
935 // Off the end? This can happen during debugging dumps | 970 // Off the end? This can happen during debugging dumps |
936 // when data structures have not finished being updated. | 971 // when data structures have not finished being updated. |
937 if( idx >= _max ) return idx; | 972 if( idx >= _max ) return idx; |
938 uint next = lookup(idx); | 973 uint next = lookup(idx); |
939 while( next != idx ) { // Scan chain of equivalences | 974 while( next != idx ) { // Scan chain of equivalences |
940 assert( next < idx, "always union smaller" ); | |
941 idx = next; // until find a fixed-point | 975 idx = next; // until find a fixed-point |
942 next = lookup(idx); | 976 next = lookup(idx); |
943 } | 977 } |
944 return next; | 978 return next; |
945 } | 979 } |
954 assert( src < _max, "oob" ); | 988 assert( src < _max, "oob" ); |
955 assert( dst < _max, "oob" ); | 989 assert( dst < _max, "oob" ); |
956 assert( src < dst, "always union smaller" ); | 990 assert( src < dst, "always union smaller" ); |
957 map(dst,src); | 991 map(dst,src); |
958 } | 992 } |
993 | |
994 #ifndef PRODUCT | |
995 static void edge_dump(GrowableArray<CFGEdge *> *edges) { | |
996 tty->print_cr("---- Edges ----"); | |
997 for (int i = 0; i < edges->length(); i++) { | |
998 CFGEdge *e = edges->at(i); | |
999 if (e != NULL) { | |
1000 edges->at(i)->dump(); | |
1001 } | |
1002 } | |
1003 } | |
1004 | |
1005 static void trace_dump(Trace *traces[], int count) { | |
1006 tty->print_cr("---- Traces ----"); | |
1007 for (int i = 0; i < count; i++) { | |
1008 Trace *tr = traces[i]; | |
1009 if (tr != NULL) { | |
1010 tr->dump(); | |
1011 } | |
1012 } | |
1013 } | |
1014 | |
1015 void Trace::dump( ) const { | |
1016 tty->print_cr("Trace (freq %f)", first_block()->_freq); | |
1017 for (Block *b = first_block(); b != NULL; b = next(b)) { | |
1018 tty->print(" B%d", b->_pre_order); | |
1019 if (b->head()->is_Loop()) { | |
1020 tty->print(" (L%d)", b->compute_loop_alignment()); | |
1021 } | |
1022 if (b->has_loop_alignment()) { | |
1023 tty->print(" (T%d)", b->code_alignment()); | |
1024 } | |
1025 } | |
1026 tty->cr(); | |
1027 } | |
1028 | |
1029 void CFGEdge::dump( ) const { | |
1030 tty->print(" B%d --> B%d Freq: %f out:%3d%% in:%3d%% State: ", | |
1031 from()->_pre_order, to()->_pre_order, freq(), _from_pct, _to_pct); | |
1032 switch(state()) { | |
1033 case connected: | |
1034 tty->print("connected"); | |
1035 break; | |
1036 case open: | |
1037 tty->print("open"); | |
1038 break; | |
1039 case interior: | |
1040 tty->print("interior"); | |
1041 break; | |
1042 } | |
1043 if (infrequent()) { | |
1044 tty->print(" infrequent"); | |
1045 } | |
1046 tty->cr(); | |
1047 } | |
1048 #endif | |
1049 | |
1050 //============================================================================= | |
1051 | |
1052 //------------------------------edge_order------------------------------------- | |
1053 // Comparison function for edges | |
1054 static int edge_order(CFGEdge **e0, CFGEdge **e1) { | |
1055 float freq0 = (*e0)->freq(); | |
1056 float freq1 = (*e1)->freq(); | |
1057 if (freq0 != freq1) { | |
1058 return freq0 > freq1 ? -1 : 1; | |
1059 } | |
1060 | |
1061 int dist0 = (*e0)->to()->_rpo - (*e0)->from()->_rpo; | |
1062 int dist1 = (*e1)->to()->_rpo - (*e1)->from()->_rpo; | |
1063 | |
1064 return dist1 - dist0; | |
1065 } | |
1066 | |
1067 //------------------------------trace_frequency_order-------------------------- | |
1068 // Comparison function for edges | |
1069 static int trace_frequency_order(const void *p0, const void *p1) { | |
1070 Trace *tr0 = *(Trace **) p0; | |
1071 Trace *tr1 = *(Trace **) p1; | |
1072 Block *b0 = tr0->first_block(); | |
1073 Block *b1 = tr1->first_block(); | |
1074 | |
1075 // The trace of connector blocks goes at the end; | |
1076 // we only expect one such trace | |
1077 if (b0->is_connector() != b1->is_connector()) { | |
1078 return b1->is_connector() ? -1 : 1; | |
1079 } | |
1080 | |
1081 // Pull more frequently executed blocks to the beginning | |
1082 float freq0 = b0->_freq; | |
1083 float freq1 = b1->_freq; | |
1084 if (freq0 != freq1) { | |
1085 return freq0 > freq1 ? -1 : 1; | |
1086 } | |
1087 | |
1088 int diff = tr0->first_block()->_rpo - tr1->first_block()->_rpo; | |
1089 | |
1090 return diff; | |
1091 } | |
1092 | |
1093 //------------------------------find_edges------------------------------------- | |
1094 // Find edges of interest, i.e, those which can fall through. Presumes that | |
1095 // edges which don't fall through are of low frequency and can be generally | |
1096 // ignored. Initialize the list of traces. | |
1097 void PhaseBlockLayout::find_edges() | |
1098 { | |
1099 // Walk the blocks, creating edges and Traces | |
1100 uint i; | |
1101 Trace *tr = NULL; | |
1102 for (i = 0; i < _cfg._num_blocks; i++) { | |
1103 Block *b = _cfg._blocks[i]; | |
1104 tr = new Trace(b, next, prev); | |
1105 traces[tr->id()] = tr; | |
1106 | |
1107 // All connector blocks should be at the end of the list | |
1108 if (b->is_connector()) break; | |
1109 | |
1110 // If this block and the next one have a one-to-one successor | |
1111 // predecessor relationship, simply append the next block | |
1112 int nfallthru = b->num_fall_throughs(); | |
1113 while (nfallthru == 1 && | |
1114 b->succ_fall_through(0)) { | |
1115 Block *n = b->_succs[0]; | |
1116 | |
1117 // Skip over single-entry connector blocks, we don't want to | |
1118 // add them to the trace. | |
1119 while (n->is_connector() && n->num_preds() == 1) { | |
1120 n = n->_succs[0]; | |
1121 } | |
1122 | |
1123 // We see a merge point, so stop search for the next block | |
1124 if (n->num_preds() != 1) break; | |
1125 | |
1126 i++; | |
1127 assert(n = _cfg._blocks[i], "expecting next block"); | |
1128 tr->append(n); | |
1129 uf->map(n->_pre_order, tr->id()); | |
1130 traces[n->_pre_order] = NULL; | |
1131 nfallthru = b->num_fall_throughs(); | |
1132 b = n; | |
1133 } | |
1134 | |
1135 if (nfallthru > 0) { | |
1136 // Create a CFGEdge for each outgoing | |
1137 // edge that could be a fall-through. | |
1138 for (uint j = 0; j < b->_num_succs; j++ ) { | |
1139 if (b->succ_fall_through(j)) { | |
1140 Block *target = b->non_connector_successor(j); | |
1141 float freq = b->_freq * b->succ_prob(j); | |
1142 int from_pct = (int) ((100 * freq) / b->_freq); | |
1143 int to_pct = (int) ((100 * freq) / target->_freq); | |
1144 edges->append(new CFGEdge(b, target, freq, from_pct, to_pct)); | |
1145 } | |
1146 } | |
1147 } | |
1148 } | |
1149 | |
1150 // Group connector blocks into one trace | |
1151 for (i++; i < _cfg._num_blocks; i++) { | |
1152 Block *b = _cfg._blocks[i]; | |
1153 assert(b->is_connector(), "connector blocks at the end"); | |
1154 tr->append(b); | |
1155 uf->map(b->_pre_order, tr->id()); | |
1156 traces[b->_pre_order] = NULL; | |
1157 } | |
1158 } | |
1159 | |
1160 //------------------------------union_traces---------------------------------- | |
1161 // Union two traces together in uf, and null out the trace in the list | |
1162 void PhaseBlockLayout::union_traces(Trace* updated_trace, Trace* old_trace) | |
1163 { | |
1164 uint old_id = old_trace->id(); | |
1165 uint updated_id = updated_trace->id(); | |
1166 | |
1167 uint lo_id = updated_id; | |
1168 uint hi_id = old_id; | |
1169 | |
1170 // If from is greater than to, swap values to meet | |
1171 // UnionFind guarantee. | |
1172 if (updated_id > old_id) { | |
1173 lo_id = old_id; | |
1174 hi_id = updated_id; | |
1175 | |
1176 // Fix up the trace ids | |
1177 traces[lo_id] = traces[updated_id]; | |
1178 updated_trace->set_id(lo_id); | |
1179 } | |
1180 | |
1181 // Union the lower with the higher and remove the pointer | |
1182 // to the higher. | |
1183 uf->Union(lo_id, hi_id); | |
1184 traces[hi_id] = NULL; | |
1185 } | |
1186 | |
1187 //------------------------------grow_traces------------------------------------- | |
1188 // Append traces together via the most frequently executed edges | |
1189 void PhaseBlockLayout::grow_traces() | |
1190 { | |
1191 // Order the edges, and drive the growth of Traces via the most | |
1192 // frequently executed edges. | |
1193 edges->sort(edge_order); | |
1194 for (int i = 0; i < edges->length(); i++) { | |
1195 CFGEdge *e = edges->at(i); | |
1196 | |
1197 if (e->state() != CFGEdge::open) continue; | |
1198 | |
1199 Block *src_block = e->from(); | |
1200 Block *targ_block = e->to(); | |
1201 | |
1202 // Don't grow traces along backedges? | |
1203 if (!BlockLayoutRotateLoops) { | |
1204 if (targ_block->_rpo <= src_block->_rpo) { | |
1205 targ_block->set_loop_alignment(targ_block); | |
1206 continue; | |
1207 } | |
1208 } | |
1209 | |
1210 Trace *src_trace = trace(src_block); | |
1211 Trace *targ_trace = trace(targ_block); | |
1212 | |
1213 // If the edge in question can join two traces at their ends, | |
1214 // append one trace to the other. | |
1215 if (src_trace->last_block() == src_block) { | |
1216 if (src_trace == targ_trace) { | |
1217 e->set_state(CFGEdge::interior); | |
1218 if (targ_trace->backedge(e)) { | |
1219 // Reset i to catch any newly eligible edge | |
1220 // (Or we could remember the first "open" edge, and reset there) | |
1221 i = 0; | |
1222 } | |
1223 } else if (targ_trace->first_block() == targ_block) { | |
1224 e->set_state(CFGEdge::connected); | |
1225 src_trace->append(targ_trace); | |
1226 union_traces(src_trace, targ_trace); | |
1227 } | |
1228 } | |
1229 } | |
1230 } | |
1231 | |
1232 //------------------------------merge_traces----------------------------------- | |
1233 // Embed one trace into another, if the fork or join points are sufficiently | |
1234 // balanced. | |
1235 void PhaseBlockLayout::merge_traces(bool fall_thru_only) | |
1236 { | |
1237 // Walk the edge list a another time, looking at unprocessed edges. | |
1238 // Fold in diamonds | |
1239 for (int i = 0; i < edges->length(); i++) { | |
1240 CFGEdge *e = edges->at(i); | |
1241 | |
1242 if (e->state() != CFGEdge::open) continue; | |
1243 if (fall_thru_only) { | |
1244 if (e->infrequent()) continue; | |
1245 } | |
1246 | |
1247 Block *src_block = e->from(); | |
1248 Trace *src_trace = trace(src_block); | |
1249 bool src_at_tail = src_trace->last_block() == src_block; | |
1250 | |
1251 Block *targ_block = e->to(); | |
1252 Trace *targ_trace = trace(targ_block); | |
1253 bool targ_at_start = targ_trace->first_block() == targ_block; | |
1254 | |
1255 if (src_trace == targ_trace) { | |
1256 // This may be a loop, but we can't do much about it. | |
1257 e->set_state(CFGEdge::interior); | |
1258 continue; | |
1259 } | |
1260 | |
1261 if (fall_thru_only) { | |
1262 // If the edge links the middle of two traces, we can't do anything. | |
1263 // Mark the edge and continue. | |
1264 if (!src_at_tail & !targ_at_start) { | |
1265 continue; | |
1266 } | |
1267 | |
1268 // Don't grow traces along backedges? | |
1269 if (!BlockLayoutRotateLoops && (targ_block->_rpo <= src_block->_rpo)) { | |
1270 continue; | |
1271 } | |
1272 | |
1273 // If both ends of the edge are available, why didn't we handle it earlier? | |
1274 assert(src_at_tail ^ targ_at_start, "Should have caught this edge earlier."); | |
1275 | |
1276 if (targ_at_start) { | |
1277 // Insert the "targ" trace in the "src" trace if the insertion point | |
1278 // is a two way branch. | |
1279 // Better profitability check possible, but may not be worth it. | |
1280 // Someday, see if the this "fork" has an associated "join"; | |
1281 // then make a policy on merging this trace at the fork or join. | |
1282 // For example, other things being equal, it may be better to place this | |
1283 // trace at the join point if the "src" trace ends in a two-way, but | |
1284 // the insertion point is one-way. | |
1285 assert(src_block->num_fall_throughs() == 2, "unexpected diamond"); | |
1286 e->set_state(CFGEdge::connected); | |
1287 src_trace->insert_after(src_block, targ_trace); | |
1288 union_traces(src_trace, targ_trace); | |
1289 } else if (src_at_tail) { | |
1290 if (src_trace != trace(_cfg._broot)) { | |
1291 e->set_state(CFGEdge::connected); | |
1292 targ_trace->insert_before(targ_block, src_trace); | |
1293 union_traces(targ_trace, src_trace); | |
1294 } | |
1295 } | |
1296 } else if (e->state() == CFGEdge::open) { | |
1297 // Append traces, even without a fall-thru connection. | |
1298 // But leave root entry at the begining of the block list. | |
1299 if (targ_trace != trace(_cfg._broot)) { | |
1300 e->set_state(CFGEdge::connected); | |
1301 src_trace->append(targ_trace); | |
1302 union_traces(src_trace, targ_trace); | |
1303 } | |
1304 } | |
1305 } | |
1306 } | |
1307 | |
1308 //----------------------------reorder_traces----------------------------------- | |
1309 // Order the sequence of the traces in some desirable way, and fixup the | |
1310 // jumps at the end of each block. | |
1311 void PhaseBlockLayout::reorder_traces(int count) | |
1312 { | |
1313 ResourceArea *area = Thread::current()->resource_area(); | |
1314 Trace ** new_traces = NEW_ARENA_ARRAY(area, Trace *, count); | |
1315 Block_List worklist; | |
1316 int new_count = 0; | |
1317 | |
1318 // Compact the traces. | |
1319 for (int i = 0; i < count; i++) { | |
1320 Trace *tr = traces[i]; | |
1321 if (tr != NULL) { | |
1322 new_traces[new_count++] = tr; | |
1323 } | |
1324 } | |
1325 | |
1326 // The entry block should be first on the new trace list. | |
1327 Trace *tr = trace(_cfg._broot); | |
1328 assert(tr == new_traces[0], "entry trace misplaced"); | |
1329 | |
1330 // Sort the new trace list by frequency | |
1331 qsort(new_traces + 1, new_count - 1, sizeof(new_traces[0]), trace_frequency_order); | |
1332 | |
1333 // Patch up the successor blocks | |
1334 _cfg._blocks.reset(); | |
1335 _cfg._num_blocks = 0; | |
1336 for (int i = 0; i < new_count; i++) { | |
1337 Trace *tr = new_traces[i]; | |
1338 if (tr != NULL) { | |
1339 tr->fixup_blocks(_cfg); | |
1340 } | |
1341 } | |
1342 } | |
1343 | |
1344 //------------------------------PhaseBlockLayout------------------------------- | |
1345 // Order basic blocks based on frequency | |
1346 PhaseBlockLayout::PhaseBlockLayout(PhaseCFG &cfg) : | |
1347 Phase(BlockLayout), | |
1348 _cfg(cfg) | |
1349 { | |
1350 ResourceMark rm; | |
1351 ResourceArea *area = Thread::current()->resource_area(); | |
1352 | |
1353 // List of traces | |
1354 int size = _cfg._num_blocks + 1; | |
1355 traces = NEW_ARENA_ARRAY(area, Trace *, size); | |
1356 memset(traces, 0, size*sizeof(Trace*)); | |
1357 next = NEW_ARENA_ARRAY(area, Block *, size); | |
1358 memset(next, 0, size*sizeof(Block *)); | |
1359 prev = NEW_ARENA_ARRAY(area, Block *, size); | |
1360 memset(prev , 0, size*sizeof(Block *)); | |
1361 | |
1362 // List of edges | |
1363 edges = new GrowableArray<CFGEdge*>; | |
1364 | |
1365 // Mapping block index --> block_trace | |
1366 uf = new UnionFind(size); | |
1367 uf->reset(size); | |
1368 | |
1369 // Find edges and create traces. | |
1370 find_edges(); | |
1371 | |
1372 // Grow traces at their ends via most frequent edges. | |
1373 grow_traces(); | |
1374 | |
1375 // Merge one trace into another, but only at fall-through points. | |
1376 // This may make diamonds and other related shapes in a trace. | |
1377 merge_traces(true); | |
1378 | |
1379 // Run merge again, allowing two traces to be catenated, even if | |
1380 // one does not fall through into the other. This appends loosely | |
1381 // related traces to be near each other. | |
1382 merge_traces(false); | |
1383 | |
1384 // Re-order all the remaining traces by frequency | |
1385 reorder_traces(size); | |
1386 | |
1387 assert(_cfg._num_blocks >= (uint) (size - 1), "number of blocks can not shrink"); | |
1388 } | |
1389 | |
1390 | |
1391 //------------------------------backedge--------------------------------------- | |
1392 // Edge e completes a loop in a trace. If the target block is head of the | |
1393 // loop, rotate the loop block so that the loop ends in a conditional branch. | |
1394 bool Trace::backedge(CFGEdge *e) { | |
1395 bool loop_rotated = false; | |
1396 Block *src_block = e->from(); | |
1397 Block *targ_block = e->to(); | |
1398 | |
1399 assert(last_block() == src_block, "loop discovery at back branch"); | |
1400 if (first_block() == targ_block) { | |
1401 if (BlockLayoutRotateLoops && last_block()->num_fall_throughs() < 2) { | |
1402 // Find the last block in the trace that has a conditional | |
1403 // branch. | |
1404 Block *b; | |
1405 for (b = last_block(); b != NULL; b = prev(b)) { | |
1406 if (b->num_fall_throughs() == 2) { | |
1407 break; | |
1408 } | |
1409 } | |
1410 | |
1411 if (b != last_block() && b != NULL) { | |
1412 loop_rotated = true; | |
1413 | |
1414 // Rotate the loop by doing two-part linked-list surgery. | |
1415 append(first_block()); | |
1416 break_loop_after(b); | |
1417 } | |
1418 } | |
1419 | |
1420 // Backbranch to the top of a trace | |
1421 // Scroll foward through the trace from the targ_block. If we find | |
1422 // a loop head before another loop top, use the the loop head alignment. | |
1423 for (Block *b = targ_block; b != NULL; b = next(b)) { | |
1424 if (b->has_loop_alignment()) { | |
1425 break; | |
1426 } | |
1427 if (b->head()->is_Loop()) { | |
1428 targ_block = b; | |
1429 break; | |
1430 } | |
1431 } | |
1432 | |
1433 first_block()->set_loop_alignment(targ_block); | |
1434 | |
1435 } else { | |
1436 // Backbranch into the middle of a trace | |
1437 targ_block->set_loop_alignment(targ_block); | |
1438 } | |
1439 | |
1440 return loop_rotated; | |
1441 } | |
1442 | |
1443 //------------------------------fixup_blocks----------------------------------- | |
1444 // push blocks onto the CFG list | |
1445 // ensure that blocks have the correct two-way branch sense | |
1446 void Trace::fixup_blocks(PhaseCFG &cfg) { | |
1447 Block *last = last_block(); | |
1448 for (Block *b = first_block(); b != NULL; b = next(b)) { | |
1449 cfg._blocks.push(b); | |
1450 cfg._num_blocks++; | |
1451 if (!b->is_connector()) { | |
1452 int nfallthru = b->num_fall_throughs(); | |
1453 if (b != last) { | |
1454 if (nfallthru == 2) { | |
1455 // Ensure that the sense of the branch is correct | |
1456 Block *bnext = next(b); | |
1457 Block *bs0 = b->non_connector_successor(0); | |
1458 | |
1459 MachNode *iff = b->_nodes[b->_nodes.size()-3]->as_Mach(); | |
1460 ProjNode *proj0 = b->_nodes[b->_nodes.size()-2]->as_Proj(); | |
1461 ProjNode *proj1 = b->_nodes[b->_nodes.size()-1]->as_Proj(); | |
1462 | |
1463 if (bnext == bs0) { | |
1464 // Fall-thru case in succs[0], should be in succs[1] | |
1465 | |
1466 // Flip targets in _succs map | |
1467 Block *tbs0 = b->_succs[0]; | |
1468 Block *tbs1 = b->_succs[1]; | |
1469 b->_succs.map( 0, tbs1 ); | |
1470 b->_succs.map( 1, tbs0 ); | |
1471 | |
1472 // Flip projections to match targets | |
1473 b->_nodes.map(b->_nodes.size()-2, proj1); | |
1474 b->_nodes.map(b->_nodes.size()-1, proj0); | |
1475 } | |
1476 } | |
1477 } | |
1478 } | |
1479 } | |
1480 } |