1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
28 #include "opto/multnode.hpp"
29 #include "opto/node.hpp"
30 #include "opto/phase.hpp"
31
32 // Optimization - Graph Style
33
34 class Block;
35 class CFGLoop;
36 class MachCallNode;
37 class Matcher;
38 class RootNode;
39 class VectorSet;
40 struct Tarjan;
41
42 //------------------------------Block_Array------------------------------------
43 // Map dense integer indices to Blocks. Uses classic doubling-array trick.
44 // Abstractly provides an infinite array of Block*'s, initialized to NULL.
45 // Note that the constructor just zeros things, and since I use Arena
46 // allocation I do not need a destructor to reclaim storage.
47 class Block_Array : public ResourceObj {
48 uint _size; // allocated size, as opposed to formal limit
49 debug_only(uint _limit;) // limit to formal domain
50 protected:
51 Block **_blocks;
52 void grow( uint i ); // Grow array node to fit
53
54 public:
55 Arena *_arena; // Arena to allocate in
56
57 Block_Array(Arena *a) : _arena(a), _size(OptoBlockListSize) {
58 debug_only(_limit=0);
59 _blocks = NEW_ARENA_ARRAY( a, Block *, OptoBlockListSize );
60 for( int i = 0; i < OptoBlockListSize; i++ ) {
61 _blocks[i] = NULL;
62 }
63 }
64 Block *lookup( uint i ) const // Lookup, or NULL for not mapped
65 { return (i<Max()) ? _blocks[i] : (Block*)NULL; }
66 Block *operator[] ( uint i ) const // Lookup, or assert for not mapped
67 { assert( i < Max(), "oob" ); return _blocks[i]; }
68 // Extend the mapping: index i maps to Block *n.
69 void map( uint i, Block *n ) { if( i>=Max() ) grow(i); _blocks[i] = n; }
70 uint Max() const { debug_only(return _limit); return _size; }
71 };
72
73
74 class Block_List : public Block_Array {
75 public:
76 uint _cnt;
77 Block_List() : Block_Array(Thread::current()->resource_area()), _cnt(0) {}
78 void push( Block *b ) { map(_cnt++,b); }
79 Block *pop() { return _blocks[--_cnt]; }
80 Block *rpop() { Block *b = _blocks[0]; _blocks[0]=_blocks[--_cnt]; return b;}
81 void remove( uint i );
82 void insert( uint i, Block *n );
83 uint size() const { return _cnt; }
84 void reset() { _cnt = 0; }
85 void print();
86 };
87
88
89 class CFGElement : public ResourceObj {
90 public:
91 float _freq; // Execution frequency (estimate)
92
93 CFGElement() : _freq(0.0f) {}
94 virtual bool is_block() { return false; }
95 virtual bool is_loop() { return false; }
96 Block* as_Block() { assert(is_block(), "must be block"); return (Block*)this; }
97 CFGLoop* as_CFGLoop() { assert(is_loop(), "must be loop"); return (CFGLoop*)this; }
98 };
99
100 //------------------------------Block------------------------------------------
101 // This class defines a Basic Block.
102 // Basic blocks are used during the output routines, and are not used during
103 // any optimization pass. They are created late in the game.
104 class Block : public CFGElement {
105 public:
106 // Nodes in this block, in order
107 Node_List _nodes;
108
109 // Basic blocks have a Node which defines Control for all Nodes pinned in
110 // this block. This Node is a RegionNode. Exception-causing Nodes
111 // (division, subroutines) and Phi functions are always pinned. Later,
112 // every Node will get pinned to some block.
113 Node *head() const { return _nodes[0]; }
114
115 // CAUTION: num_preds() is ONE based, so that predecessor numbers match
116 // input edges to Regions and Phis.
117 uint num_preds() const { return head()->req(); }
118 Node *pred(uint i) const { return head()->in(i); }
119
120 // Array of successor blocks, same size as projs array
121 Block_Array _succs;
122
123 // Basic blocks have some number of Nodes which split control to all
124 // following blocks. These Nodes are always Projections. The field in
324 bool has_uncommon_code() const;
325
326 // Use frequency calculations and code shape to predict if the block
327 // is uncommon.
328 bool is_uncommon( Block_Array &bbs ) const;
329
330 #ifndef PRODUCT
331 // Debugging print of basic block
332 void dump_bidx(const Block* orig, outputStream* st = tty) const;
333 void dump_pred(const Block_Array *bbs, Block* orig, outputStream* st = tty) const;
334 void dump_head( const Block_Array *bbs, outputStream* st = tty ) const;
335 void dump() const;
336 void dump( const Block_Array *bbs ) const;
337 #endif
338 };
339
340
341 //------------------------------PhaseCFG---------------------------------------
342 // Build an array of Basic Block pointers, one per Node.
343 class PhaseCFG : public Phase {
344 private:
345 // Build a proper looking cfg. Return count of basic blocks
346 uint build_cfg();
347
348 // Perform DFS search.
349 // Setup 'vertex' as DFS to vertex mapping.
350 // Setup 'semi' as vertex to DFS mapping.
351 // Set 'parent' to DFS parent.
352 uint DFS( Tarjan *tarjan );
353
354 // Helper function to insert a node into a block
355 void schedule_node_into_block( Node *n, Block *b );
356
357 void replace_block_proj_ctrl( Node *n );
358
359 // Set the basic block for pinned Nodes
360 void schedule_pinned_nodes( VectorSet &visited );
361
362 // I'll need a few machine-specific GotoNodes. Clone from this one.
363 MachNode *_goto;
498 void Union( uint idx1, uint idx2 );
499
500 };
501
502 //----------------------------BlockProbPair---------------------------
503 // Ordered pair of Node*.
504 class BlockProbPair VALUE_OBJ_CLASS_SPEC {
505 protected:
506 Block* _target; // block target
507 float _prob; // probability of edge to block
508 public:
509 BlockProbPair() : _target(NULL), _prob(0.0) {}
510 BlockProbPair(Block* b, float p) : _target(b), _prob(p) {}
511
512 Block* get_target() const { return _target; }
513 float get_prob() const { return _prob; }
514 };
515
516 //------------------------------CFGLoop-------------------------------------------
517 class CFGLoop : public CFGElement {
518 int _id;
519 int _depth;
520 CFGLoop *_parent; // root of loop tree is the method level "pseudo" loop, it's parent is null
521 CFGLoop *_sibling; // null terminated list
522 CFGLoop *_child; // first child, use child's sibling to visit all immediately nested loops
523 GrowableArray<CFGElement*> _members; // list of members of loop
524 GrowableArray<BlockProbPair> _exits; // list of successor blocks and their probabilities
525 float _exit_prob; // probability any loop exit is taken on a single loop iteration
526 void update_succ_freq(Block* b, float freq);
527
528 public:
529 CFGLoop(int id) :
530 CFGElement(),
531 _id(id),
532 _depth(0),
533 _parent(NULL),
534 _sibling(NULL),
535 _child(NULL),
536 _exit_prob(1.0f) {}
537 CFGLoop* parent() { return _parent; }
549 void compute_loop_depth(int depth);
550 void compute_freq(); // compute frequency with loop assuming head freq 1.0f
551 void scale_freq(); // scale frequency by loop trip count (including outer loops)
552 float outer_loop_freq() const; // frequency of outer loop
553 bool in_loop_nest(Block* b);
554 float trip_count() const { return 1.0f / _exit_prob; }
555 virtual bool is_loop() { return true; }
556 int id() { return _id; }
557
558 #ifndef PRODUCT
559 void dump( ) const;
560 void dump_tree() const;
561 #endif
562 };
563
564
565 //----------------------------------CFGEdge------------------------------------
566 // A edge between two basic blocks that will be embodied by a branch or a
567 // fall-through.
568 class CFGEdge : public ResourceObj {
569 private:
570 Block * _from; // Source basic block
571 Block * _to; // Destination basic block
572 float _freq; // Execution frequency (estimate)
573 int _state;
574 bool _infrequent;
575 int _from_pct;
576 int _to_pct;
577
578 // Private accessors
579 int from_pct() const { return _from_pct; }
580 int to_pct() const { return _to_pct; }
581 int from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; }
582 int to_infrequent() const { return to_pct() < BlockLayoutMinDiamondPercentage; }
583
584 public:
585 enum {
586 open, // initial edge state; unprocessed
587 connected, // edge used to connect two traces together
588 interior // edge is interior to trace (could be backedge)
685
686 // Append a block at the end of this trace
687 void append(Block *b) {
688 set_next(_last, b);
689 set_prev(b, _last);
690 _last = b;
691 }
692
693 // Adjust the the blocks in this trace
694 void fixup_blocks(PhaseCFG &cfg);
695 bool backedge(CFGEdge *e);
696
697 #ifndef PRODUCT
698 void dump( ) const;
699 #endif
700 };
701
702 //------------------------------PhaseBlockLayout-------------------------------
703 // Rearrange blocks into some canonical order, based on edges and their frequencies
704 class PhaseBlockLayout : public Phase {
705 PhaseCFG &_cfg; // Control flow graph
706
707 GrowableArray<CFGEdge *> *edges;
708 Trace **traces;
709 Block **next;
710 Block **prev;
711 UnionFind *uf;
712
713 // Given a block, find its encompassing Trace
714 Trace * trace(Block *b) {
715 return traces[uf->Find_compress(b->_pre_order)];
716 }
717 public:
718 PhaseBlockLayout(PhaseCFG &cfg);
719
720 void find_edges();
721 void grow_traces();
722 void merge_traces(bool loose_connections);
723 void reorder_traces(int count);
724 void union_traces(Trace* from, Trace* to);
|
1 /*
2 * Copyright (c) 1997, 2011, 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 *
28 #include "opto/multnode.hpp"
29 #include "opto/node.hpp"
30 #include "opto/phase.hpp"
31
32 // Optimization - Graph Style
33
34 class Block;
35 class CFGLoop;
36 class MachCallNode;
37 class Matcher;
38 class RootNode;
39 class VectorSet;
40 struct Tarjan;
41
42 //------------------------------Block_Array------------------------------------
43 // Map dense integer indices to Blocks. Uses classic doubling-array trick.
44 // Abstractly provides an infinite array of Block*'s, initialized to NULL.
45 // Note that the constructor just zeros things, and since I use Arena
46 // allocation I do not need a destructor to reclaim storage.
47 class Block_Array : public ResourceObj {
48 friend class VMStructs;
49 uint _size; // allocated size, as opposed to formal limit
50 debug_only(uint _limit;) // limit to formal domain
51 protected:
52 Block **_blocks;
53 void grow( uint i ); // Grow array node to fit
54
55 public:
56 Arena *_arena; // Arena to allocate in
57
58 Block_Array(Arena *a) : _arena(a), _size(OptoBlockListSize) {
59 debug_only(_limit=0);
60 _blocks = NEW_ARENA_ARRAY( a, Block *, OptoBlockListSize );
61 for( int i = 0; i < OptoBlockListSize; i++ ) {
62 _blocks[i] = NULL;
63 }
64 }
65 Block *lookup( uint i ) const // Lookup, or NULL for not mapped
66 { return (i<Max()) ? _blocks[i] : (Block*)NULL; }
67 Block *operator[] ( uint i ) const // Lookup, or assert for not mapped
68 { assert( i < Max(), "oob" ); return _blocks[i]; }
69 // Extend the mapping: index i maps to Block *n.
70 void map( uint i, Block *n ) { if( i>=Max() ) grow(i); _blocks[i] = n; }
71 uint Max() const { debug_only(return _limit); return _size; }
72 };
73
74
75 class Block_List : public Block_Array {
76 friend class VMStructs;
77 public:
78 uint _cnt;
79 Block_List() : Block_Array(Thread::current()->resource_area()), _cnt(0) {}
80 void push( Block *b ) { map(_cnt++,b); }
81 Block *pop() { return _blocks[--_cnt]; }
82 Block *rpop() { Block *b = _blocks[0]; _blocks[0]=_blocks[--_cnt]; return b;}
83 void remove( uint i );
84 void insert( uint i, Block *n );
85 uint size() const { return _cnt; }
86 void reset() { _cnt = 0; }
87 void print();
88 };
89
90
91 class CFGElement : public ResourceObj {
92 friend class VMStructs;
93 public:
94 float _freq; // Execution frequency (estimate)
95
96 CFGElement() : _freq(0.0f) {}
97 virtual bool is_block() { return false; }
98 virtual bool is_loop() { return false; }
99 Block* as_Block() { assert(is_block(), "must be block"); return (Block*)this; }
100 CFGLoop* as_CFGLoop() { assert(is_loop(), "must be loop"); return (CFGLoop*)this; }
101 };
102
103 //------------------------------Block------------------------------------------
104 // This class defines a Basic Block.
105 // Basic blocks are used during the output routines, and are not used during
106 // any optimization pass. They are created late in the game.
107 class Block : public CFGElement {
108 friend class VMStructs;
109 public:
110 // Nodes in this block, in order
111 Node_List _nodes;
112
113 // Basic blocks have a Node which defines Control for all Nodes pinned in
114 // this block. This Node is a RegionNode. Exception-causing Nodes
115 // (division, subroutines) and Phi functions are always pinned. Later,
116 // every Node will get pinned to some block.
117 Node *head() const { return _nodes[0]; }
118
119 // CAUTION: num_preds() is ONE based, so that predecessor numbers match
120 // input edges to Regions and Phis.
121 uint num_preds() const { return head()->req(); }
122 Node *pred(uint i) const { return head()->in(i); }
123
124 // Array of successor blocks, same size as projs array
125 Block_Array _succs;
126
127 // Basic blocks have some number of Nodes which split control to all
128 // following blocks. These Nodes are always Projections. The field in
328 bool has_uncommon_code() const;
329
330 // Use frequency calculations and code shape to predict if the block
331 // is uncommon.
332 bool is_uncommon( Block_Array &bbs ) const;
333
334 #ifndef PRODUCT
335 // Debugging print of basic block
336 void dump_bidx(const Block* orig, outputStream* st = tty) const;
337 void dump_pred(const Block_Array *bbs, Block* orig, outputStream* st = tty) const;
338 void dump_head( const Block_Array *bbs, outputStream* st = tty ) const;
339 void dump() const;
340 void dump( const Block_Array *bbs ) const;
341 #endif
342 };
343
344
345 //------------------------------PhaseCFG---------------------------------------
346 // Build an array of Basic Block pointers, one per Node.
347 class PhaseCFG : public Phase {
348 friend class VMStructs;
349 private:
350 // Build a proper looking cfg. Return count of basic blocks
351 uint build_cfg();
352
353 // Perform DFS search.
354 // Setup 'vertex' as DFS to vertex mapping.
355 // Setup 'semi' as vertex to DFS mapping.
356 // Set 'parent' to DFS parent.
357 uint DFS( Tarjan *tarjan );
358
359 // Helper function to insert a node into a block
360 void schedule_node_into_block( Node *n, Block *b );
361
362 void replace_block_proj_ctrl( Node *n );
363
364 // Set the basic block for pinned Nodes
365 void schedule_pinned_nodes( VectorSet &visited );
366
367 // I'll need a few machine-specific GotoNodes. Clone from this one.
368 MachNode *_goto;
503 void Union( uint idx1, uint idx2 );
504
505 };
506
507 //----------------------------BlockProbPair---------------------------
508 // Ordered pair of Node*.
509 class BlockProbPair VALUE_OBJ_CLASS_SPEC {
510 protected:
511 Block* _target; // block target
512 float _prob; // probability of edge to block
513 public:
514 BlockProbPair() : _target(NULL), _prob(0.0) {}
515 BlockProbPair(Block* b, float p) : _target(b), _prob(p) {}
516
517 Block* get_target() const { return _target; }
518 float get_prob() const { return _prob; }
519 };
520
521 //------------------------------CFGLoop-------------------------------------------
522 class CFGLoop : public CFGElement {
523 friend class VMStructs;
524 int _id;
525 int _depth;
526 CFGLoop *_parent; // root of loop tree is the method level "pseudo" loop, it's parent is null
527 CFGLoop *_sibling; // null terminated list
528 CFGLoop *_child; // first child, use child's sibling to visit all immediately nested loops
529 GrowableArray<CFGElement*> _members; // list of members of loop
530 GrowableArray<BlockProbPair> _exits; // list of successor blocks and their probabilities
531 float _exit_prob; // probability any loop exit is taken on a single loop iteration
532 void update_succ_freq(Block* b, float freq);
533
534 public:
535 CFGLoop(int id) :
536 CFGElement(),
537 _id(id),
538 _depth(0),
539 _parent(NULL),
540 _sibling(NULL),
541 _child(NULL),
542 _exit_prob(1.0f) {}
543 CFGLoop* parent() { return _parent; }
555 void compute_loop_depth(int depth);
556 void compute_freq(); // compute frequency with loop assuming head freq 1.0f
557 void scale_freq(); // scale frequency by loop trip count (including outer loops)
558 float outer_loop_freq() const; // frequency of outer loop
559 bool in_loop_nest(Block* b);
560 float trip_count() const { return 1.0f / _exit_prob; }
561 virtual bool is_loop() { return true; }
562 int id() { return _id; }
563
564 #ifndef PRODUCT
565 void dump( ) const;
566 void dump_tree() const;
567 #endif
568 };
569
570
571 //----------------------------------CFGEdge------------------------------------
572 // A edge between two basic blocks that will be embodied by a branch or a
573 // fall-through.
574 class CFGEdge : public ResourceObj {
575 friend class VMStructs;
576 private:
577 Block * _from; // Source basic block
578 Block * _to; // Destination basic block
579 float _freq; // Execution frequency (estimate)
580 int _state;
581 bool _infrequent;
582 int _from_pct;
583 int _to_pct;
584
585 // Private accessors
586 int from_pct() const { return _from_pct; }
587 int to_pct() const { return _to_pct; }
588 int from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; }
589 int to_infrequent() const { return to_pct() < BlockLayoutMinDiamondPercentage; }
590
591 public:
592 enum {
593 open, // initial edge state; unprocessed
594 connected, // edge used to connect two traces together
595 interior // edge is interior to trace (could be backedge)
692
693 // Append a block at the end of this trace
694 void append(Block *b) {
695 set_next(_last, b);
696 set_prev(b, _last);
697 _last = b;
698 }
699
700 // Adjust the the blocks in this trace
701 void fixup_blocks(PhaseCFG &cfg);
702 bool backedge(CFGEdge *e);
703
704 #ifndef PRODUCT
705 void dump( ) const;
706 #endif
707 };
708
709 //------------------------------PhaseBlockLayout-------------------------------
710 // Rearrange blocks into some canonical order, based on edges and their frequencies
711 class PhaseBlockLayout : public Phase {
712 friend class VMStructs;
713 PhaseCFG &_cfg; // Control flow graph
714
715 GrowableArray<CFGEdge *> *edges;
716 Trace **traces;
717 Block **next;
718 Block **prev;
719 UnionFind *uf;
720
721 // Given a block, find its encompassing Trace
722 Trace * trace(Block *b) {
723 return traces[uf->Find_compress(b->_pre_order)];
724 }
725 public:
726 PhaseBlockLayout(PhaseCFG &cfg);
727
728 void find_edges();
729 void grow_traces();
730 void merge_traces(bool loose_connections);
731 void reorder_traces(int count);
732 void union_traces(Trace* from, Trace* to);
|