annotate src/share/vm/opto/block.cpp @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents a61af66fc99e
children 756b58154237
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // Optimization - Graph Style
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include "incls/_block.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 //-----------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
32 void Block_Array::grow( uint i ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 assert(i >= Max(), "must be an overflow");
a61af66fc99e Initial load
duke
parents:
diff changeset
34 debug_only(_limit = i+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
35 if( i < _size ) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 if( !_size ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 _size = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 _blocks = (Block**)_arena->Amalloc( _size * sizeof(Block*) );
a61af66fc99e Initial load
duke
parents:
diff changeset
39 _blocks[0] = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 }
a61af66fc99e Initial load
duke
parents:
diff changeset
41 uint old = _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 while( i >= _size ) _size <<= 1; // Double to fit
a61af66fc99e Initial load
duke
parents:
diff changeset
43 _blocks = (Block**)_arena->Arealloc( _blocks, old*sizeof(Block*),_size*sizeof(Block*));
a61af66fc99e Initial load
duke
parents:
diff changeset
44 Copy::zero_to_bytes( &_blocks[old], (_size-old)*sizeof(Block*) );
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
48 void Block_List::remove(uint i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 assert(i < _cnt, "index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
50 Copy::conjoint_words_to_lower((HeapWord*)&_blocks[i+1], (HeapWord*)&_blocks[i], ((_cnt-i-1)*sizeof(Block*)));
a61af66fc99e Initial load
duke
parents:
diff changeset
51 pop(); // shrink list by one block
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 void Block_List::insert(uint i, Block *b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 push(b); // grow list by one block
a61af66fc99e Initial load
duke
parents:
diff changeset
56 Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*)));
a61af66fc99e Initial load
duke
parents:
diff changeset
57 _blocks[i] = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 uint Block::code_alignment() {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // Check for Root block
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if( _pre_order == 0 ) return CodeEntryAlignment;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // Check for Start block
a61af66fc99e Initial load
duke
parents:
diff changeset
67 if( _pre_order == 1 ) return InteriorEntryAlignment;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // Check for loop alignment
a61af66fc99e Initial load
duke
parents:
diff changeset
69 Node *h = head();
a61af66fc99e Initial load
duke
parents:
diff changeset
70 if( h->is_Loop() && h->as_Loop()->is_inner_loop() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 // Pre- and post-loops have low trip count so do not bother with
a61af66fc99e Initial load
duke
parents:
diff changeset
72 // NOPs for align loop head. The constants are hidden from tuning
a61af66fc99e Initial load
duke
parents:
diff changeset
73 // but only because my "divide by 4" heuristic surely gets nearly
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // all possible gain (a "do not align at all" heuristic has a
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // chance of getting a really tiny gain).
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if( h->is_CountedLoop() && (h->as_CountedLoop()->is_pre_loop() ||
a61af66fc99e Initial load
duke
parents:
diff changeset
77 h->as_CountedLoop()->is_post_loop()) )
a61af66fc99e Initial load
duke
parents:
diff changeset
78 return (OptoLoopAlignment > 4) ? (OptoLoopAlignment>>2) : 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 // Loops with low backedge frequency should not be aligned.
a61af66fc99e Initial load
duke
parents:
diff changeset
80 Node *n = h->in(LoopNode::LoopBackControl)->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if( n->is_MachIf() && n->as_MachIf()->_prob < 0.01 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 return 1; // Loop does not loop, more often than not!
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84 return OptoLoopAlignment; // Otherwise align loop head
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 return 1; // no particular alignment
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 //-----------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // Compute the size of first 'inst_cnt' instructions in this block.
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // Return the number of instructions left to compute if the block has
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // less then 'inst_cnt' instructions.
a61af66fc99e Initial load
duke
parents:
diff changeset
93 uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt,
a61af66fc99e Initial load
duke
parents:
diff changeset
94 PhaseRegAlloc* ra) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 uint last_inst = _nodes.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
96 for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 uint inst_size = _nodes[j]->size(ra);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 if( inst_size > 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 inst_cnt--;
a61af66fc99e Initial load
duke
parents:
diff changeset
100 uint sz = sum_size + inst_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if( sz <= (uint)OptoLoopAlignment ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // Compute size of instructions which fit into fetch buffer only
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // since all inst_cnt instructions will not fit even if we align them.
a61af66fc99e Initial load
duke
parents:
diff changeset
104 sum_size = sz;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 return inst_cnt;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 //-----------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
114 uint Block::find_node( const Node *n ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 for( uint i = 0; i < _nodes.size(); i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 if( _nodes[i] == n )
a61af66fc99e Initial load
duke
parents:
diff changeset
117 return i;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 }
a61af66fc99e Initial load
duke
parents:
diff changeset
119 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // Find and remove n from block list
a61af66fc99e Initial load
duke
parents:
diff changeset
124 void Block::find_remove( const Node *n ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 _nodes.remove(find_node(n));
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127
a61af66fc99e Initial load
duke
parents:
diff changeset
128 //------------------------------is_Empty---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // Return empty status of a block. Empty blocks contain only the head, other
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // ideal nodes, and an optional trailing goto.
a61af66fc99e Initial load
duke
parents:
diff changeset
131 int Block::is_Empty() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 // Root or start block is not considered empty
a61af66fc99e Initial load
duke
parents:
diff changeset
134 if (head()->is_Root() || head()->is_Start()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 return not_empty;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 int success_result = completely_empty;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 int end_idx = _nodes.size()-1;
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // Check for ending goto
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if ((end_idx > 0) && (_nodes[end_idx]->is_Goto())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 success_result = empty_with_goto;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 end_idx--;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // Unreachable blocks are considered empty
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if (num_preds() <= 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 return success_result;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // Ideal nodes are allowable in empty blocks: skip them Only MachNodes
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // turn directly into code, because only MachNodes have non-trivial
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // emit() functions.
a61af66fc99e Initial load
duke
parents:
diff changeset
155 while ((end_idx > 0) && !_nodes[end_idx]->is_Mach()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 end_idx--;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // No room for any interesting instructions?
a61af66fc99e Initial load
duke
parents:
diff changeset
160 if (end_idx == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 return success_result;
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 return not_empty;
a61af66fc99e Initial load
duke
parents:
diff changeset
165 }
a61af66fc99e Initial load
duke
parents:
diff changeset
166
a61af66fc99e Initial load
duke
parents:
diff changeset
167 //------------------------------has_uncommon_code------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // Return true if the block's code implies that it is not likely to be
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // executed infrequently. Check to see if the block ends in a Halt or
a61af66fc99e Initial load
duke
parents:
diff changeset
170 // a low probability call.
a61af66fc99e Initial load
duke
parents:
diff changeset
171 bool Block::has_uncommon_code() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 Node* en = end();
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 if (en->is_Goto())
a61af66fc99e Initial load
duke
parents:
diff changeset
175 en = en->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (en->is_Catch())
a61af66fc99e Initial load
duke
parents:
diff changeset
177 en = en->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
178 if (en->is_Proj() && en->in(0)->is_MachCall()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 MachCallNode* call = en->in(0)->as_MachCall();
a61af66fc99e Initial load
duke
parents:
diff changeset
180 if (call->cnt() != COUNT_UNKNOWN && call->cnt() <= PROB_UNLIKELY_MAG(4)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 // This is true for slow-path stubs like new_{instance,array},
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // slow_arraycopy, complete_monitor_locking, uncommon_trap.
a61af66fc99e Initial load
duke
parents:
diff changeset
183 // The magic number corresponds to the probability of an uncommon_trap,
a61af66fc99e Initial load
duke
parents:
diff changeset
184 // even though it is a count not a probability.
a61af66fc99e Initial load
duke
parents:
diff changeset
185 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 int op = en->is_Mach() ? en->as_Mach()->ideal_Opcode() : en->Opcode();
a61af66fc99e Initial load
duke
parents:
diff changeset
190 return op == Op_Halt;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 //------------------------------is_uncommon------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // True if block is low enough frequency or guarded by a test which
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // mostly does not go here.
a61af66fc99e Initial load
duke
parents:
diff changeset
196 bool Block::is_uncommon( Block_Array &bbs ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // Initial blocks must never be moved, so are never uncommon.
a61af66fc99e Initial load
duke
parents:
diff changeset
198 if (head()->is_Root() || head()->is_Start()) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
199
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // Check for way-low freq
a61af66fc99e Initial load
duke
parents:
diff changeset
201 if( _freq < BLOCK_FREQUENCY(0.00001f) ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // Look for code shape indicating uncommon_trap or slow path
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (has_uncommon_code()) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
205
a61af66fc99e Initial load
duke
parents:
diff changeset
206 const float epsilon = 0.05f;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 const float guard_factor = PROB_UNLIKELY_MAG(4) / (1.f - epsilon);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 uint uncommon_preds = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
209 uint freq_preds = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 uint uncommon_for_freq_preds = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 for( uint i=1; i<num_preds(); i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 Block* guard = bbs[pred(i)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
214 // Check to see if this block follows its guard 1 time out of 10000
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // or less.
a61af66fc99e Initial load
duke
parents:
diff changeset
216 //
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // See list of magnitude-4 unlikely probabilities in cfgnode.hpp which
a61af66fc99e Initial load
duke
parents:
diff changeset
218 // we intend to be "uncommon", such as slow-path TLE allocation,
a61af66fc99e Initial load
duke
parents:
diff changeset
219 // predicted call failure, and uncommon trap triggers.
a61af66fc99e Initial load
duke
parents:
diff changeset
220 //
a61af66fc99e Initial load
duke
parents:
diff changeset
221 // Use an epsilon value of 5% to allow for variability in frequency
a61af66fc99e Initial load
duke
parents:
diff changeset
222 // predictions and floating point calculations. The net effect is
a61af66fc99e Initial load
duke
parents:
diff changeset
223 // that guard_factor is set to 9500.
a61af66fc99e Initial load
duke
parents:
diff changeset
224 //
a61af66fc99e Initial load
duke
parents:
diff changeset
225 // Ignore low-frequency blocks.
a61af66fc99e Initial load
duke
parents:
diff changeset
226 // The next check is (guard->_freq < 1.e-5 * 9500.).
a61af66fc99e Initial load
duke
parents:
diff changeset
227 if(guard->_freq*BLOCK_FREQUENCY(guard_factor) < BLOCK_FREQUENCY(0.00001f)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 uncommon_preds++;
a61af66fc99e Initial load
duke
parents:
diff changeset
229 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
230 freq_preds++;
a61af66fc99e Initial load
duke
parents:
diff changeset
231 if( _freq < guard->_freq * guard_factor ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 uncommon_for_freq_preds++;
a61af66fc99e Initial load
duke
parents:
diff changeset
233 }
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236 if( num_preds() > 1 &&
a61af66fc99e Initial load
duke
parents:
diff changeset
237 // The block is uncommon if all preds are uncommon or
a61af66fc99e Initial load
duke
parents:
diff changeset
238 (uncommon_preds == (num_preds()-1) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
239 // it is uncommon for all frequent preds.
a61af66fc99e Initial load
duke
parents:
diff changeset
240 uncommon_for_freq_preds == freq_preds) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
241 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
244 }
a61af66fc99e Initial load
duke
parents:
diff changeset
245
a61af66fc99e Initial load
duke
parents:
diff changeset
246 //------------------------------dump-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
247 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
248 void Block::dump_bidx(const Block* orig) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
249 if (_pre_order) tty->print("B%d",_pre_order);
a61af66fc99e Initial load
duke
parents:
diff changeset
250 else tty->print("N%d", head()->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252 if (Verbose && orig != this) {
a61af66fc99e Initial load
duke
parents:
diff changeset
253 // Dump the original block's idx
a61af66fc99e Initial load
duke
parents:
diff changeset
254 tty->print(" (");
a61af66fc99e Initial load
duke
parents:
diff changeset
255 orig->dump_bidx(orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
256 tty->print(")");
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259
a61af66fc99e Initial load
duke
parents:
diff changeset
260 void Block::dump_pred(const Block_Array *bbs, Block* orig) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
261 if (is_connector()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 for (uint i=1; i<num_preds(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 Block *p = ((*bbs)[pred(i)->_idx]);
a61af66fc99e Initial load
duke
parents:
diff changeset
264 p->dump_pred(bbs, orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 dump_bidx(orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
268 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
269 }
a61af66fc99e Initial load
duke
parents:
diff changeset
270 }
a61af66fc99e Initial load
duke
parents:
diff changeset
271
a61af66fc99e Initial load
duke
parents:
diff changeset
272 void Block::dump_head( const Block_Array *bbs ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 // Print the basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
274 dump_bidx(this);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 tty->print(": #\t");
a61af66fc99e Initial load
duke
parents:
diff changeset
276
a61af66fc99e Initial load
duke
parents:
diff changeset
277 // Print the incoming CFG edges and the outgoing CFG edges
a61af66fc99e Initial load
duke
parents:
diff changeset
278 for( uint i=0; i<_num_succs; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
279 non_connector_successor(i)->dump_bidx(_succs[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
280 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
281 }
a61af66fc99e Initial load
duke
parents:
diff changeset
282 tty->print("<- ");
a61af66fc99e Initial load
duke
parents:
diff changeset
283 if( head()->is_block_start() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
284 for (uint i=1; i<num_preds(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 Node *s = pred(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
286 if (bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 Block *p = (*bbs)[s->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
288 p->dump_pred(bbs, p);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
290 while (!s->is_block_start())
a61af66fc99e Initial load
duke
parents:
diff changeset
291 s = s->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
292 tty->print("N%d ", s->_idx );
a61af66fc99e Initial load
duke
parents:
diff changeset
293 }
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
296 tty->print("BLOCK HEAD IS JUNK ");
a61af66fc99e Initial load
duke
parents:
diff changeset
297
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // Print loop, if any
a61af66fc99e Initial load
duke
parents:
diff changeset
299 const Block *bhead = this; // Head of self-loop
a61af66fc99e Initial load
duke
parents:
diff changeset
300 Node *bh = bhead->head();
a61af66fc99e Initial load
duke
parents:
diff changeset
301 if( bbs && bh->is_Loop() && !head()->is_Root() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 LoopNode *loop = bh->as_Loop();
a61af66fc99e Initial load
duke
parents:
diff changeset
303 const Block *bx = (*bbs)[loop->in(LoopNode::LoopBackControl)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
304 while (bx->is_connector()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
305 bx = (*bbs)[bx->pred(1)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 tty->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order);
a61af66fc99e Initial load
duke
parents:
diff changeset
308 // Dump any loop-specific bits, especially for CountedLoops.
a61af66fc99e Initial load
duke
parents:
diff changeset
309 loop->dump_spec(tty);
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311 tty->print(" Freq: %g",_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
312 if( Verbose || WizardMode ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 tty->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth);
a61af66fc99e Initial load
duke
parents:
diff changeset
314 tty->print(" RegPressure: %d",_reg_pressure);
a61af66fc99e Initial load
duke
parents:
diff changeset
315 tty->print(" IHRP Index: %d",_ihrp_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
316 tty->print(" FRegPressure: %d",_freg_pressure);
a61af66fc99e Initial load
duke
parents:
diff changeset
317 tty->print(" FHRP Index: %d",_fhrp_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319 tty->print_cr("");
a61af66fc99e Initial load
duke
parents:
diff changeset
320 }
a61af66fc99e Initial load
duke
parents:
diff changeset
321
a61af66fc99e Initial load
duke
parents:
diff changeset
322 void Block::dump() const { dump(0); }
a61af66fc99e Initial load
duke
parents:
diff changeset
323
a61af66fc99e Initial load
duke
parents:
diff changeset
324 void Block::dump( const Block_Array *bbs ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 dump_head(bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
326 uint cnt = _nodes.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
327 for( uint i=0; i<cnt; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
328 _nodes[i]->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
329 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
332
a61af66fc99e Initial load
duke
parents:
diff changeset
333 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
334 //------------------------------PhaseCFG---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
335 PhaseCFG::PhaseCFG( Arena *a, RootNode *r, Matcher &m ) :
a61af66fc99e Initial load
duke
parents:
diff changeset
336 Phase(CFG),
a61af66fc99e Initial load
duke
parents:
diff changeset
337 _bbs(a),
a61af66fc99e Initial load
duke
parents:
diff changeset
338 _root(r)
a61af66fc99e Initial load
duke
parents:
diff changeset
339 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
340 , _trace_opto_pipelining(TraceOptoPipelining || C->method_has_option("TraceOptoPipelining"))
a61af66fc99e Initial load
duke
parents:
diff changeset
341 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
342 {
a61af66fc99e Initial load
duke
parents:
diff changeset
343 ResourceMark rm;
a61af66fc99e Initial load
duke
parents:
diff changeset
344 // I'll need a few machine-specific GotoNodes. Make an Ideal GotoNode,
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // then Match it into a machine-specific Node. Then clone the machine
a61af66fc99e Initial load
duke
parents:
diff changeset
346 // Node on demand.
a61af66fc99e Initial load
duke
parents:
diff changeset
347 Node *x = new (C, 1) GotoNode(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
348 x->init_req(0, x);
a61af66fc99e Initial load
duke
parents:
diff changeset
349 _goto = m.match_tree(x);
a61af66fc99e Initial load
duke
parents:
diff changeset
350 assert(_goto != NULL, "");
a61af66fc99e Initial load
duke
parents:
diff changeset
351 _goto->set_req(0,_goto);
a61af66fc99e Initial load
duke
parents:
diff changeset
352
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // Build the CFG in Reverse Post Order
a61af66fc99e Initial load
duke
parents:
diff changeset
354 _num_blocks = build_cfg();
a61af66fc99e Initial load
duke
parents:
diff changeset
355 _broot = _bbs[_root->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357
a61af66fc99e Initial load
duke
parents:
diff changeset
358 //------------------------------build_cfg--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // Build a proper looking CFG. Make every block begin with either a StartNode
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // or a RegionNode. Make every block end with either a Goto, If or Return.
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // The RootNode both starts and ends it's own block. Do this with a recursive
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // backwards walk over the control edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 uint PhaseCFG::build_cfg() {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 Arena *a = Thread::current()->resource_area();
a61af66fc99e Initial load
duke
parents:
diff changeset
365 VectorSet visited(a);
a61af66fc99e Initial load
duke
parents:
diff changeset
366
a61af66fc99e Initial load
duke
parents:
diff changeset
367 // Allocate stack with enough space to avoid frequent realloc
a61af66fc99e Initial load
duke
parents:
diff changeset
368 Node_Stack nstack(a, C->unique() >> 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
369 nstack.push(_root, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
370 uint sum = 0; // Counter for blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
371
a61af66fc99e Initial load
duke
parents:
diff changeset
372 while (nstack.is_nonempty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // node and in's index from stack's top
a61af66fc99e Initial load
duke
parents:
diff changeset
374 // 'np' is _root (see above) or RegionNode, StartNode: we push on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
375 // only nodes which point to the start of basic block (see below).
a61af66fc99e Initial load
duke
parents:
diff changeset
376 Node *np = nstack.node();
a61af66fc99e Initial load
duke
parents:
diff changeset
377 // idx > 0, except for the first node (_root) pushed on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // at the beginning when idx == 0.
a61af66fc99e Initial load
duke
parents:
diff changeset
379 // We will use the condition (idx == 0) later to end the build.
a61af66fc99e Initial load
duke
parents:
diff changeset
380 uint idx = nstack.index();
a61af66fc99e Initial load
duke
parents:
diff changeset
381 Node *proj = np->in(idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
382 const Node *x = proj->is_block_proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
383 // Does the block end with a proper block-ending Node? One of Return,
a61af66fc99e Initial load
duke
parents:
diff changeset
384 // If or Goto? (This check should be done for visited nodes also).
a61af66fc99e Initial load
duke
parents:
diff changeset
385 if (x == NULL) { // Does not end right...
a61af66fc99e Initial load
duke
parents:
diff changeset
386 Node *g = _goto->clone(); // Force it to end in a Goto
a61af66fc99e Initial load
duke
parents:
diff changeset
387 g->set_req(0, proj);
a61af66fc99e Initial load
duke
parents:
diff changeset
388 np->set_req(idx, g);
a61af66fc99e Initial load
duke
parents:
diff changeset
389 x = proj = g;
a61af66fc99e Initial load
duke
parents:
diff changeset
390 }
a61af66fc99e Initial load
duke
parents:
diff changeset
391 if (!visited.test_set(x->_idx)) { // Visit this block once
a61af66fc99e Initial load
duke
parents:
diff changeset
392 // Skip any control-pinned middle'in stuff
a61af66fc99e Initial load
duke
parents:
diff changeset
393 Node *p = proj;
a61af66fc99e Initial load
duke
parents:
diff changeset
394 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 proj = p; // Update pointer to last Control
a61af66fc99e Initial load
duke
parents:
diff changeset
396 p = p->in(0); // Move control forward
a61af66fc99e Initial load
duke
parents:
diff changeset
397 } while( !p->is_block_proj() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
398 !p->is_block_start() );
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // Make the block begin with one of Region or StartNode.
a61af66fc99e Initial load
duke
parents:
diff changeset
400 if( !p->is_block_start() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
401 RegionNode *r = new (C, 2) RegionNode( 2 );
a61af66fc99e Initial load
duke
parents:
diff changeset
402 r->init_req(1, p); // Insert RegionNode in the way
a61af66fc99e Initial load
duke
parents:
diff changeset
403 proj->set_req(0, r); // Insert RegionNode in the way
a61af66fc99e Initial load
duke
parents:
diff changeset
404 p = r;
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // 'p' now points to the start of this basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
407
a61af66fc99e Initial load
duke
parents:
diff changeset
408 // Put self in array of basic blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
409 Block *bb = new (_bbs._arena) Block(_bbs._arena,p);
a61af66fc99e Initial load
duke
parents:
diff changeset
410 _bbs.map(p->_idx,bb);
a61af66fc99e Initial load
duke
parents:
diff changeset
411 _bbs.map(x->_idx,bb);
a61af66fc99e Initial load
duke
parents:
diff changeset
412 if( x != p ) // Only for root is x == p
a61af66fc99e Initial load
duke
parents:
diff changeset
413 bb->_nodes.push((Node*)x);
a61af66fc99e Initial load
duke
parents:
diff changeset
414
a61af66fc99e Initial load
duke
parents:
diff changeset
415 // Now handle predecessors
a61af66fc99e Initial load
duke
parents:
diff changeset
416 ++sum; // Count 1 for self block
a61af66fc99e Initial load
duke
parents:
diff changeset
417 uint cnt = bb->num_preds();
a61af66fc99e Initial load
duke
parents:
diff changeset
418 for (int i = (cnt - 1); i > 0; i-- ) { // For all predecessors
a61af66fc99e Initial load
duke
parents:
diff changeset
419 Node *prevproj = p->in(i); // Get prior input
a61af66fc99e Initial load
duke
parents:
diff changeset
420 assert( !prevproj->is_Con(), "dead input not removed" );
a61af66fc99e Initial load
duke
parents:
diff changeset
421 // Check to see if p->in(i) is a "control-dependent" CFG edge -
a61af66fc99e Initial load
duke
parents:
diff changeset
422 // i.e., it splits at the source (via an IF or SWITCH) and merges
a61af66fc99e Initial load
duke
parents:
diff changeset
423 // at the destination (via a many-input Region).
a61af66fc99e Initial load
duke
parents:
diff changeset
424 // This breaks critical edges. The RegionNode to start the block
a61af66fc99e Initial load
duke
parents:
diff changeset
425 // will be added when <p,i> is pulled off the node stack
a61af66fc99e Initial load
duke
parents:
diff changeset
426 if ( cnt > 2 ) { // Merging many things?
a61af66fc99e Initial load
duke
parents:
diff changeset
427 assert( prevproj== bb->pred(i),"");
a61af66fc99e Initial load
duke
parents:
diff changeset
428 if(prevproj->is_block_proj() != prevproj) { // Control-dependent edge?
a61af66fc99e Initial load
duke
parents:
diff changeset
429 // Force a block on the control-dependent edge
a61af66fc99e Initial load
duke
parents:
diff changeset
430 Node *g = _goto->clone(); // Force it to end in a Goto
a61af66fc99e Initial load
duke
parents:
diff changeset
431 g->set_req(0,prevproj);
a61af66fc99e Initial load
duke
parents:
diff changeset
432 p->set_req(i,g);
a61af66fc99e Initial load
duke
parents:
diff changeset
433 }
a61af66fc99e Initial load
duke
parents:
diff changeset
434 }
a61af66fc99e Initial load
duke
parents:
diff changeset
435 nstack.push(p, i); // 'p' is RegionNode or StartNode
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
a61af66fc99e Initial load
duke
parents:
diff changeset
437 } else { // Post-processing visited nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
438 nstack.pop(); // remove node from stack
a61af66fc99e Initial load
duke
parents:
diff changeset
439 // Check if it the fist node pushed on stack at the beginning.
a61af66fc99e Initial load
duke
parents:
diff changeset
440 if (idx == 0) break; // end of the build
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // Find predecessor basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
442 Block *pb = _bbs[x->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
443 // Insert into nodes array, if not already there
a61af66fc99e Initial load
duke
parents:
diff changeset
444 if( !_bbs.lookup(proj->_idx) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
445 assert( x != proj, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
446 // Map basic block of projection
a61af66fc99e Initial load
duke
parents:
diff changeset
447 _bbs.map(proj->_idx,pb);
a61af66fc99e Initial load
duke
parents:
diff changeset
448 pb->_nodes.push(proj);
a61af66fc99e Initial load
duke
parents:
diff changeset
449 }
a61af66fc99e Initial load
duke
parents:
diff changeset
450 // Insert self as a child of my predecessor block
a61af66fc99e Initial load
duke
parents:
diff changeset
451 pb->_succs.map(pb->_num_succs++, _bbs[np->_idx]);
a61af66fc99e Initial load
duke
parents:
diff changeset
452 assert( pb->_nodes[ pb->_nodes.size() - pb->_num_succs ]->is_block_proj(),
a61af66fc99e Initial load
duke
parents:
diff changeset
453 "too many control users, not a CFG?" );
a61af66fc99e Initial load
duke
parents:
diff changeset
454 }
a61af66fc99e Initial load
duke
parents:
diff changeset
455 }
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // Return number of basic blocks for all children and self
a61af66fc99e Initial load
duke
parents:
diff changeset
457 return sum;
a61af66fc99e Initial load
duke
parents:
diff changeset
458 }
a61af66fc99e Initial load
duke
parents:
diff changeset
459
a61af66fc99e Initial load
duke
parents:
diff changeset
460 //------------------------------insert_goto_at---------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // Inserts a goto & corresponding basic block between
a61af66fc99e Initial load
duke
parents:
diff changeset
462 // block[block_no] and its succ_no'th successor block
a61af66fc99e Initial load
duke
parents:
diff changeset
463 void PhaseCFG::insert_goto_at(uint block_no, uint succ_no) {
a61af66fc99e Initial load
duke
parents:
diff changeset
464 // get block with block_no
a61af66fc99e Initial load
duke
parents:
diff changeset
465 assert(block_no < _num_blocks, "illegal block number");
a61af66fc99e Initial load
duke
parents:
diff changeset
466 Block* in = _blocks[block_no];
a61af66fc99e Initial load
duke
parents:
diff changeset
467 // get successor block succ_no
a61af66fc99e Initial load
duke
parents:
diff changeset
468 assert(succ_no < in->_num_succs, "illegal successor number");
a61af66fc99e Initial load
duke
parents:
diff changeset
469 Block* out = in->_succs[succ_no];
a61af66fc99e Initial load
duke
parents:
diff changeset
470 // get ProjNode corresponding to the succ_no'th successor of the in block
a61af66fc99e Initial load
duke
parents:
diff changeset
471 ProjNode* proj = in->_nodes[in->_nodes.size() - in->_num_succs + succ_no]->as_Proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // create region for basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
473 RegionNode* region = new (C, 2) RegionNode(2);
a61af66fc99e Initial load
duke
parents:
diff changeset
474 region->init_req(1, proj);
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // setup corresponding basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
476 Block* block = new (_bbs._arena) Block(_bbs._arena, region);
a61af66fc99e Initial load
duke
parents:
diff changeset
477 _bbs.map(region->_idx, block);
a61af66fc99e Initial load
duke
parents:
diff changeset
478 C->regalloc()->set_bad(region->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
479 // add a goto node
a61af66fc99e Initial load
duke
parents:
diff changeset
480 Node* gto = _goto->clone(); // get a new goto node
a61af66fc99e Initial load
duke
parents:
diff changeset
481 gto->set_req(0, region);
a61af66fc99e Initial load
duke
parents:
diff changeset
482 // add it to the basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
483 block->_nodes.push(gto);
a61af66fc99e Initial load
duke
parents:
diff changeset
484 _bbs.map(gto->_idx, block);
a61af66fc99e Initial load
duke
parents:
diff changeset
485 C->regalloc()->set_bad(gto->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
486 // hook up successor block
a61af66fc99e Initial load
duke
parents:
diff changeset
487 block->_succs.map(block->_num_succs++, out);
a61af66fc99e Initial load
duke
parents:
diff changeset
488 // remap successor's predecessors if necessary
a61af66fc99e Initial load
duke
parents:
diff changeset
489 for (uint i = 1; i < out->num_preds(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
490 if (out->pred(i) == proj) out->head()->set_req(i, gto);
a61af66fc99e Initial load
duke
parents:
diff changeset
491 }
a61af66fc99e Initial load
duke
parents:
diff changeset
492 // remap predecessor's successor to new block
a61af66fc99e Initial load
duke
parents:
diff changeset
493 in->_succs.map(succ_no, block);
a61af66fc99e Initial load
duke
parents:
diff changeset
494 // add new basic block to basic block list
a61af66fc99e Initial load
duke
parents:
diff changeset
495 _blocks.insert(block_no + 1, block);
a61af66fc99e Initial load
duke
parents:
diff changeset
496 _num_blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
497 }
a61af66fc99e Initial load
duke
parents:
diff changeset
498
a61af66fc99e Initial load
duke
parents:
diff changeset
499 //------------------------------no_flip_branch---------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
500 // Does this block end in a multiway branch that cannot have the default case
a61af66fc99e Initial load
duke
parents:
diff changeset
501 // flipped for another case?
a61af66fc99e Initial load
duke
parents:
diff changeset
502 static bool no_flip_branch( Block *b ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
503 int branch_idx = b->_nodes.size() - b->_num_succs-1;
a61af66fc99e Initial load
duke
parents:
diff changeset
504 if( branch_idx < 1 ) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
505 Node *bra = b->_nodes[branch_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
506 if( bra->is_Catch() ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
507 if( bra->is_Mach() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
508 if( bra->is_MachNullCheck() ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
509 int iop = bra->as_Mach()->ideal_Opcode();
a61af66fc99e Initial load
duke
parents:
diff changeset
510 if( iop == Op_FastLock || iop == Op_FastUnlock )
a61af66fc99e Initial load
duke
parents:
diff changeset
511 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
512 }
a61af66fc99e Initial load
duke
parents:
diff changeset
513 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
514 }
a61af66fc99e Initial load
duke
parents:
diff changeset
515
a61af66fc99e Initial load
duke
parents:
diff changeset
516 //------------------------------convert_NeverBranch_to_Goto--------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
517 // Check for NeverBranch at block end. This needs to become a GOTO to the
a61af66fc99e Initial load
duke
parents:
diff changeset
518 // true target. NeverBranch are treated as a conditional branch that always
a61af66fc99e Initial load
duke
parents:
diff changeset
519 // goes the same direction for most of the optimizer and are used to give a
a61af66fc99e Initial load
duke
parents:
diff changeset
520 // fake exit path to infinite loops. At this late stage they need to turn
a61af66fc99e Initial load
duke
parents:
diff changeset
521 // into Goto's so that when you enter the infinite loop you indeed hang.
a61af66fc99e Initial load
duke
parents:
diff changeset
522 void PhaseCFG::convert_NeverBranch_to_Goto(Block *b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
523 // Find true target
a61af66fc99e Initial load
duke
parents:
diff changeset
524 int end_idx = b->end_idx();
a61af66fc99e Initial load
duke
parents:
diff changeset
525 int idx = b->_nodes[end_idx+1]->as_Proj()->_con;
a61af66fc99e Initial load
duke
parents:
diff changeset
526 Block *succ = b->_succs[idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
527 Node* gto = _goto->clone(); // get a new goto node
a61af66fc99e Initial load
duke
parents:
diff changeset
528 gto->set_req(0, b->head());
a61af66fc99e Initial load
duke
parents:
diff changeset
529 Node *bp = b->_nodes[end_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
530 b->_nodes.map(end_idx,gto); // Slam over NeverBranch
a61af66fc99e Initial load
duke
parents:
diff changeset
531 _bbs.map(gto->_idx, b);
a61af66fc99e Initial load
duke
parents:
diff changeset
532 C->regalloc()->set_bad(gto->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
533 b->_nodes.pop(); // Yank projections
a61af66fc99e Initial load
duke
parents:
diff changeset
534 b->_nodes.pop(); // Yank projections
a61af66fc99e Initial load
duke
parents:
diff changeset
535 b->_succs.map(0,succ); // Map only successor
a61af66fc99e Initial load
duke
parents:
diff changeset
536 b->_num_succs = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
537 // remap successor's predecessors if necessary
a61af66fc99e Initial load
duke
parents:
diff changeset
538 uint j;
a61af66fc99e Initial load
duke
parents:
diff changeset
539 for( j = 1; j < succ->num_preds(); j++)
a61af66fc99e Initial load
duke
parents:
diff changeset
540 if( succ->pred(j)->in(0) == bp )
a61af66fc99e Initial load
duke
parents:
diff changeset
541 succ->head()->set_req(j, gto);
a61af66fc99e Initial load
duke
parents:
diff changeset
542 // Kill alternate exit path
a61af66fc99e Initial load
duke
parents:
diff changeset
543 Block *dead = b->_succs[1-idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
544 for( j = 1; j < dead->num_preds(); j++)
a61af66fc99e Initial load
duke
parents:
diff changeset
545 if( dead->pred(j)->in(0) == bp )
a61af66fc99e Initial load
duke
parents:
diff changeset
546 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // Scan through block, yanking dead path from
a61af66fc99e Initial load
duke
parents:
diff changeset
548 // all regions and phis.
a61af66fc99e Initial load
duke
parents:
diff changeset
549 dead->head()->del_req(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
550 for( int k = 1; dead->_nodes[k]->is_Phi(); k++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
551 dead->_nodes[k]->del_req(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
552 }
a61af66fc99e Initial load
duke
parents:
diff changeset
553
a61af66fc99e Initial load
duke
parents:
diff changeset
554 //------------------------------MoveToNext-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
555 // Helper function to move block bx to the slot following b_index. Return
a61af66fc99e Initial load
duke
parents:
diff changeset
556 // true if the move is successful, otherwise false
a61af66fc99e Initial load
duke
parents:
diff changeset
557 bool PhaseCFG::MoveToNext(Block* bx, uint b_index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 if (bx == NULL) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
559
a61af66fc99e Initial load
duke
parents:
diff changeset
560 // Return false if bx is already scheduled.
a61af66fc99e Initial load
duke
parents:
diff changeset
561 uint bx_index = bx->_pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
562 if ((bx_index <= b_index) && (_blocks[bx_index] == bx)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
564 }
a61af66fc99e Initial load
duke
parents:
diff changeset
565
a61af66fc99e Initial load
duke
parents:
diff changeset
566 // Find the current index of block bx on the block list
a61af66fc99e Initial load
duke
parents:
diff changeset
567 bx_index = b_index + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
568 while( bx_index < _num_blocks && _blocks[bx_index] != bx ) bx_index++;
a61af66fc99e Initial load
duke
parents:
diff changeset
569 assert(_blocks[bx_index] == bx, "block not found");
a61af66fc99e Initial load
duke
parents:
diff changeset
570
a61af66fc99e Initial load
duke
parents:
diff changeset
571 // If the previous block conditionally falls into bx, return false,
a61af66fc99e Initial load
duke
parents:
diff changeset
572 // because moving bx will create an extra jump.
a61af66fc99e Initial load
duke
parents:
diff changeset
573 for(uint k = 1; k < bx->num_preds(); k++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
574 Block* pred = _bbs[bx->pred(k)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
575 if (pred == _blocks[bx_index-1]) {
a61af66fc99e Initial load
duke
parents:
diff changeset
576 if (pred->_num_succs != 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
577 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
578 }
a61af66fc99e Initial load
duke
parents:
diff changeset
579 }
a61af66fc99e Initial load
duke
parents:
diff changeset
580 }
a61af66fc99e Initial load
duke
parents:
diff changeset
581
a61af66fc99e Initial load
duke
parents:
diff changeset
582 // Reinsert bx just past block 'b'
a61af66fc99e Initial load
duke
parents:
diff changeset
583 _blocks.remove(bx_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
584 _blocks.insert(b_index + 1, bx);
a61af66fc99e Initial load
duke
parents:
diff changeset
585 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
586 }
a61af66fc99e Initial load
duke
parents:
diff changeset
587
a61af66fc99e Initial load
duke
parents:
diff changeset
588 //------------------------------MoveToEnd--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
589 // Move empty and uncommon blocks to the end.
a61af66fc99e Initial load
duke
parents:
diff changeset
590 void PhaseCFG::MoveToEnd(Block *b, uint i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
591 int e = b->is_Empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
592 if (e != Block::not_empty) {
a61af66fc99e Initial load
duke
parents:
diff changeset
593 if (e == Block::empty_with_goto) {
a61af66fc99e Initial load
duke
parents:
diff changeset
594 // Remove the goto, but leave the block.
a61af66fc99e Initial load
duke
parents:
diff changeset
595 b->_nodes.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
596 }
a61af66fc99e Initial load
duke
parents:
diff changeset
597 // Mark this block as a connector block, which will cause it to be
a61af66fc99e Initial load
duke
parents:
diff changeset
598 // ignored in certain functions such as non_connector_successor().
a61af66fc99e Initial load
duke
parents:
diff changeset
599 b->set_connector();
a61af66fc99e Initial load
duke
parents:
diff changeset
600 }
a61af66fc99e Initial load
duke
parents:
diff changeset
601 // Move the empty block to the end, and don't recheck.
a61af66fc99e Initial load
duke
parents:
diff changeset
602 _blocks.remove(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
603 _blocks.push(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
604 }
a61af66fc99e Initial load
duke
parents:
diff changeset
605
a61af66fc99e Initial load
duke
parents:
diff changeset
606 //------------------------------RemoveEmpty------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
607 // Remove empty basic blocks and useless branches.
a61af66fc99e Initial load
duke
parents:
diff changeset
608 void PhaseCFG::RemoveEmpty() {
a61af66fc99e Initial load
duke
parents:
diff changeset
609 // Move uncommon blocks to the end
a61af66fc99e Initial load
duke
parents:
diff changeset
610 uint last = _num_blocks;
a61af66fc99e Initial load
duke
parents:
diff changeset
611 uint i;
a61af66fc99e Initial load
duke
parents:
diff changeset
612 assert( _blocks[0] == _broot, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
613 for( i = 1; i < last; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
614 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
615
a61af66fc99e Initial load
duke
parents:
diff changeset
616 // Check for NeverBranch at block end. This needs to become a GOTO to the
a61af66fc99e Initial load
duke
parents:
diff changeset
617 // true target. NeverBranch are treated as a conditional branch that
a61af66fc99e Initial load
duke
parents:
diff changeset
618 // always goes the same direction for most of the optimizer and are used
a61af66fc99e Initial load
duke
parents:
diff changeset
619 // to give a fake exit path to infinite loops. At this late stage they
a61af66fc99e Initial load
duke
parents:
diff changeset
620 // need to turn into Goto's so that when you enter the infinite loop you
a61af66fc99e Initial load
duke
parents:
diff changeset
621 // indeed hang.
a61af66fc99e Initial load
duke
parents:
diff changeset
622 if( b->_nodes[b->end_idx()]->Opcode() == Op_NeverBranch )
a61af66fc99e Initial load
duke
parents:
diff changeset
623 convert_NeverBranch_to_Goto(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
624
a61af66fc99e Initial load
duke
parents:
diff changeset
625 // Look for uncommon blocks and move to end.
a61af66fc99e Initial load
duke
parents:
diff changeset
626 if( b->is_uncommon(_bbs) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
627 MoveToEnd(b, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
628 last--; // No longer check for being uncommon!
a61af66fc99e Initial load
duke
parents:
diff changeset
629 if( no_flip_branch(b) ) { // Fall-thru case must follow?
a61af66fc99e Initial load
duke
parents:
diff changeset
630 b = _blocks[i]; // Find the fall-thru block
a61af66fc99e Initial load
duke
parents:
diff changeset
631 MoveToEnd(b, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
632 last--;
a61af66fc99e Initial load
duke
parents:
diff changeset
633 }
a61af66fc99e Initial load
duke
parents:
diff changeset
634 i--; // backup block counter post-increment
a61af66fc99e Initial load
duke
parents:
diff changeset
635 }
a61af66fc99e Initial load
duke
parents:
diff changeset
636 }
a61af66fc99e Initial load
duke
parents:
diff changeset
637
a61af66fc99e Initial load
duke
parents:
diff changeset
638 // Remove empty blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
639 uint j1;
a61af66fc99e Initial load
duke
parents:
diff changeset
640 last = _num_blocks;
a61af66fc99e Initial load
duke
parents:
diff changeset
641 for( i=0; i < last; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
642 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
643 if (i > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
644 if (b->is_Empty() != Block::not_empty) {
a61af66fc99e Initial load
duke
parents:
diff changeset
645 MoveToEnd(b, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
646 last--;
a61af66fc99e Initial load
duke
parents:
diff changeset
647 i--;
a61af66fc99e Initial load
duke
parents:
diff changeset
648 }
a61af66fc99e Initial load
duke
parents:
diff changeset
649 }
a61af66fc99e Initial load
duke
parents:
diff changeset
650 } // End of for all blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
651
a61af66fc99e Initial load
duke
parents:
diff changeset
652 // Fixup final control flow for the blocks. Remove jump-to-next
a61af66fc99e Initial load
duke
parents:
diff changeset
653 // block. If neither arm of a IF follows the conditional branch, we
a61af66fc99e Initial load
duke
parents:
diff changeset
654 // have to add a second jump after the conditional. We place the
a61af66fc99e Initial load
duke
parents:
diff changeset
655 // TRUE branch target in succs[0] for both GOTOs and IFs.
a61af66fc99e Initial load
duke
parents:
diff changeset
656 for( i=0; i < _num_blocks; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
657 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
658 b->_pre_order = i; // turn pre-order into block-index
a61af66fc99e Initial load
duke
parents:
diff changeset
659
a61af66fc99e Initial load
duke
parents:
diff changeset
660 // Connector blocks need no further processing.
a61af66fc99e Initial load
duke
parents:
diff changeset
661 if (b->is_connector()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
662 assert((i+1) == _num_blocks || _blocks[i+1]->is_connector(),
a61af66fc99e Initial load
duke
parents:
diff changeset
663 "All connector blocks should sink to the end");
a61af66fc99e Initial load
duke
parents:
diff changeset
664 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
665 }
a61af66fc99e Initial load
duke
parents:
diff changeset
666 assert(b->is_Empty() != Block::completely_empty,
a61af66fc99e Initial load
duke
parents:
diff changeset
667 "Empty blocks should be connectors");
a61af66fc99e Initial load
duke
parents:
diff changeset
668
a61af66fc99e Initial load
duke
parents:
diff changeset
669 Block *bnext = (i < _num_blocks-1) ? _blocks[i+1] : NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
670 Block *bs0 = b->non_connector_successor(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
671
a61af66fc99e Initial load
duke
parents:
diff changeset
672 // Check for multi-way branches where I cannot negate the test to
a61af66fc99e Initial load
duke
parents:
diff changeset
673 // exchange the true and false targets.
a61af66fc99e Initial load
duke
parents:
diff changeset
674 if( no_flip_branch( b ) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
675 // Find fall through case - if must fall into its target
a61af66fc99e Initial load
duke
parents:
diff changeset
676 int branch_idx = b->_nodes.size() - b->_num_succs;
a61af66fc99e Initial load
duke
parents:
diff changeset
677 for (uint j2 = 0; j2 < b->_num_succs; j2++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
678 const ProjNode* p = b->_nodes[branch_idx + j2]->as_Proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
679 if (p->_con == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
680 // successor j2 is fall through case
a61af66fc99e Initial load
duke
parents:
diff changeset
681 if (b->non_connector_successor(j2) != bnext) {
a61af66fc99e Initial load
duke
parents:
diff changeset
682 // but it is not the next block => insert a goto
a61af66fc99e Initial load
duke
parents:
diff changeset
683 insert_goto_at(i, j2);
a61af66fc99e Initial load
duke
parents:
diff changeset
684 }
a61af66fc99e Initial load
duke
parents:
diff changeset
685 // Put taken branch in slot 0
a61af66fc99e Initial load
duke
parents:
diff changeset
686 if( j2 == 0 && b->_num_succs == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
687 // Flip targets in succs map
a61af66fc99e Initial load
duke
parents:
diff changeset
688 Block *tbs0 = b->_succs[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
689 Block *tbs1 = b->_succs[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
690 b->_succs.map( 0, tbs1 );
a61af66fc99e Initial load
duke
parents:
diff changeset
691 b->_succs.map( 1, tbs0 );
a61af66fc99e Initial load
duke
parents:
diff changeset
692 }
a61af66fc99e Initial load
duke
parents:
diff changeset
693 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
694 }
a61af66fc99e Initial load
duke
parents:
diff changeset
695 }
a61af66fc99e Initial load
duke
parents:
diff changeset
696 // Remove all CatchProjs
a61af66fc99e Initial load
duke
parents:
diff changeset
697 for (j1 = 0; j1 < b->_num_succs; j1++) b->_nodes.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
698
a61af66fc99e Initial load
duke
parents:
diff changeset
699 } else if (b->_num_succs == 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
700 // Block ends in a Goto?
a61af66fc99e Initial load
duke
parents:
diff changeset
701 if (bnext == bs0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
702 // We fall into next block; remove the Goto
a61af66fc99e Initial load
duke
parents:
diff changeset
703 b->_nodes.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
704 }
a61af66fc99e Initial load
duke
parents:
diff changeset
705
a61af66fc99e Initial load
duke
parents:
diff changeset
706 } else if( b->_num_succs == 2 ) { // Block ends in a If?
a61af66fc99e Initial load
duke
parents:
diff changeset
707 // Get opcode of 1st projection (matches _succs[0])
a61af66fc99e Initial load
duke
parents:
diff changeset
708 // Note: Since this basic block has 2 exits, the last 2 nodes must
a61af66fc99e Initial load
duke
parents:
diff changeset
709 // be projections (in any order), the 3rd last node must be
a61af66fc99e Initial load
duke
parents:
diff changeset
710 // the IfNode (we have excluded other 2-way exits such as
a61af66fc99e Initial load
duke
parents:
diff changeset
711 // CatchNodes already).
a61af66fc99e Initial load
duke
parents:
diff changeset
712 MachNode *iff = b->_nodes[b->_nodes.size()-3]->as_Mach();
a61af66fc99e Initial load
duke
parents:
diff changeset
713 ProjNode *proj0 = b->_nodes[b->_nodes.size()-2]->as_Proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
714 ProjNode *proj1 = b->_nodes[b->_nodes.size()-1]->as_Proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
715
a61af66fc99e Initial load
duke
parents:
diff changeset
716 // Assert that proj0 and succs[0] match up. Similarly for proj1 and succs[1].
a61af66fc99e Initial load
duke
parents:
diff changeset
717 assert(proj0->raw_out(0) == b->_succs[0]->head(), "Mismatch successor 0");
a61af66fc99e Initial load
duke
parents:
diff changeset
718 assert(proj1->raw_out(0) == b->_succs[1]->head(), "Mismatch successor 1");
a61af66fc99e Initial load
duke
parents:
diff changeset
719
a61af66fc99e Initial load
duke
parents:
diff changeset
720 Block *bs1 = b->non_connector_successor(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
721
a61af66fc99e Initial load
duke
parents:
diff changeset
722 // Check for neither successor block following the current
a61af66fc99e Initial load
duke
parents:
diff changeset
723 // block ending in a conditional. If so, move one of the
a61af66fc99e Initial load
duke
parents:
diff changeset
724 // successors after the current one, provided that the
a61af66fc99e Initial load
duke
parents:
diff changeset
725 // successor was previously unscheduled, but moveable
a61af66fc99e Initial load
duke
parents:
diff changeset
726 // (i.e., all paths to it involve a branch).
a61af66fc99e Initial load
duke
parents:
diff changeset
727 if( bnext != bs0 && bnext != bs1 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
728
a61af66fc99e Initial load
duke
parents:
diff changeset
729 // Choose the more common successor based on the probability
a61af66fc99e Initial load
duke
parents:
diff changeset
730 // of the conditional branch.
a61af66fc99e Initial load
duke
parents:
diff changeset
731 Block *bx = bs0;
a61af66fc99e Initial load
duke
parents:
diff changeset
732 Block *by = bs1;
a61af66fc99e Initial load
duke
parents:
diff changeset
733
a61af66fc99e Initial load
duke
parents:
diff changeset
734 // _prob is the probability of taking the true path. Make
a61af66fc99e Initial load
duke
parents:
diff changeset
735 // p the probability of taking successor #1.
a61af66fc99e Initial load
duke
parents:
diff changeset
736 float p = iff->as_MachIf()->_prob;
a61af66fc99e Initial load
duke
parents:
diff changeset
737 if( proj0->Opcode() == Op_IfTrue ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
738 p = 1.0 - p;
a61af66fc99e Initial load
duke
parents:
diff changeset
739 }
a61af66fc99e Initial load
duke
parents:
diff changeset
740
a61af66fc99e Initial load
duke
parents:
diff changeset
741 // Prefer successor #1 if p > 0.5
a61af66fc99e Initial load
duke
parents:
diff changeset
742 if (p > PROB_FAIR) {
a61af66fc99e Initial load
duke
parents:
diff changeset
743 bx = bs1;
a61af66fc99e Initial load
duke
parents:
diff changeset
744 by = bs0;
a61af66fc99e Initial load
duke
parents:
diff changeset
745 }
a61af66fc99e Initial load
duke
parents:
diff changeset
746
a61af66fc99e Initial load
duke
parents:
diff changeset
747 // Attempt the more common successor first
a61af66fc99e Initial load
duke
parents:
diff changeset
748 if (MoveToNext(bx, i)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
749 bnext = bx;
a61af66fc99e Initial load
duke
parents:
diff changeset
750 } else if (MoveToNext(by, i)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
751 bnext = by;
a61af66fc99e Initial load
duke
parents:
diff changeset
752 }
a61af66fc99e Initial load
duke
parents:
diff changeset
753 }
a61af66fc99e Initial load
duke
parents:
diff changeset
754
a61af66fc99e Initial load
duke
parents:
diff changeset
755 // Check for conditional branching the wrong way. Negate
a61af66fc99e Initial load
duke
parents:
diff changeset
756 // conditional, if needed, so it falls into the following block
a61af66fc99e Initial load
duke
parents:
diff changeset
757 // and branches to the not-following block.
a61af66fc99e Initial load
duke
parents:
diff changeset
758
a61af66fc99e Initial load
duke
parents:
diff changeset
759 // Check for the next block being in succs[0]. We are going to branch
a61af66fc99e Initial load
duke
parents:
diff changeset
760 // to succs[0], so we want the fall-thru case as the next block in
a61af66fc99e Initial load
duke
parents:
diff changeset
761 // succs[1].
a61af66fc99e Initial load
duke
parents:
diff changeset
762 if (bnext == bs0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
763 // Fall-thru case in succs[0], so flip targets in succs map
a61af66fc99e Initial load
duke
parents:
diff changeset
764 Block *tbs0 = b->_succs[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
765 Block *tbs1 = b->_succs[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
766 b->_succs.map( 0, tbs1 );
a61af66fc99e Initial load
duke
parents:
diff changeset
767 b->_succs.map( 1, tbs0 );
a61af66fc99e Initial load
duke
parents:
diff changeset
768 // Flip projection for each target
a61af66fc99e Initial load
duke
parents:
diff changeset
769 { ProjNode *tmp = proj0; proj0 = proj1; proj1 = tmp; }
a61af66fc99e Initial load
duke
parents:
diff changeset
770
a61af66fc99e Initial load
duke
parents:
diff changeset
771 } else if( bnext == bs1 ) { // Fall-thru is already in succs[1]
a61af66fc99e Initial load
duke
parents:
diff changeset
772
a61af66fc99e Initial load
duke
parents:
diff changeset
773 } else { // Else need a double-branch
a61af66fc99e Initial load
duke
parents:
diff changeset
774
a61af66fc99e Initial load
duke
parents:
diff changeset
775 // The existing conditional branch need not change.
a61af66fc99e Initial load
duke
parents:
diff changeset
776 // Add a unconditional branch to the false target.
a61af66fc99e Initial load
duke
parents:
diff changeset
777 // Alas, it must appear in its own block and adding a
a61af66fc99e Initial load
duke
parents:
diff changeset
778 // block this late in the game is complicated. Sigh.
a61af66fc99e Initial load
duke
parents:
diff changeset
779 insert_goto_at(i, 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
780 }
a61af66fc99e Initial load
duke
parents:
diff changeset
781
a61af66fc99e Initial load
duke
parents:
diff changeset
782 // Make sure we TRUE branch to the target
a61af66fc99e Initial load
duke
parents:
diff changeset
783 if( proj0->Opcode() == Op_IfFalse )
a61af66fc99e Initial load
duke
parents:
diff changeset
784 iff->negate();
a61af66fc99e Initial load
duke
parents:
diff changeset
785
a61af66fc99e Initial load
duke
parents:
diff changeset
786 b->_nodes.pop(); // Remove IfFalse & IfTrue projections
a61af66fc99e Initial load
duke
parents:
diff changeset
787 b->_nodes.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
788
a61af66fc99e Initial load
duke
parents:
diff changeset
789 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
790 // Multi-exit block, e.g. a switch statement
a61af66fc99e Initial load
duke
parents:
diff changeset
791 // But we don't need to do anything here
a61af66fc99e Initial load
duke
parents:
diff changeset
792 }
a61af66fc99e Initial load
duke
parents:
diff changeset
793
a61af66fc99e Initial load
duke
parents:
diff changeset
794 } // End of for all blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
795
a61af66fc99e Initial load
duke
parents:
diff changeset
796 }
a61af66fc99e Initial load
duke
parents:
diff changeset
797
a61af66fc99e Initial load
duke
parents:
diff changeset
798
a61af66fc99e Initial load
duke
parents:
diff changeset
799 //------------------------------dump-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
800 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
801 void PhaseCFG::_dump_cfg( const Node *end, VectorSet &visited ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
802 const Node *x = end->is_block_proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
803 assert( x, "not a CFG" );
a61af66fc99e Initial load
duke
parents:
diff changeset
804
a61af66fc99e Initial load
duke
parents:
diff changeset
805 // Do not visit this block again
a61af66fc99e Initial load
duke
parents:
diff changeset
806 if( visited.test_set(x->_idx) ) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
807
a61af66fc99e Initial load
duke
parents:
diff changeset
808 // Skip through this block
a61af66fc99e Initial load
duke
parents:
diff changeset
809 const Node *p = x;
a61af66fc99e Initial load
duke
parents:
diff changeset
810 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
811 p = p->in(0); // Move control forward
a61af66fc99e Initial load
duke
parents:
diff changeset
812 assert( !p->is_block_proj() || p->is_Root(), "not a CFG" );
a61af66fc99e Initial load
duke
parents:
diff changeset
813 } while( !p->is_block_start() );
a61af66fc99e Initial load
duke
parents:
diff changeset
814
a61af66fc99e Initial load
duke
parents:
diff changeset
815 // Recursively visit
a61af66fc99e Initial load
duke
parents:
diff changeset
816 for( uint i=1; i<p->req(); i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
817 _dump_cfg(p->in(i),visited);
a61af66fc99e Initial load
duke
parents:
diff changeset
818
a61af66fc99e Initial load
duke
parents:
diff changeset
819 // Dump the block
a61af66fc99e Initial load
duke
parents:
diff changeset
820 _bbs[p->_idx]->dump(&_bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
821 }
a61af66fc99e Initial load
duke
parents:
diff changeset
822
a61af66fc99e Initial load
duke
parents:
diff changeset
823 void PhaseCFG::dump( ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
824 tty->print("\n--- CFG --- %d BBs\n",_num_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
825 if( _blocks.size() ) { // Did we do basic-block layout?
a61af66fc99e Initial load
duke
parents:
diff changeset
826 for( uint i=0; i<_num_blocks; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
827 _blocks[i]->dump(&_bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
828 } else { // Else do it with a DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
829 VectorSet visited(_bbs._arena);
a61af66fc99e Initial load
duke
parents:
diff changeset
830 _dump_cfg(_root,visited);
a61af66fc99e Initial load
duke
parents:
diff changeset
831 }
a61af66fc99e Initial load
duke
parents:
diff changeset
832 }
a61af66fc99e Initial load
duke
parents:
diff changeset
833
a61af66fc99e Initial load
duke
parents:
diff changeset
834 void PhaseCFG::dump_headers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
835 for( uint i = 0; i < _num_blocks; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
836 if( _blocks[i] == NULL ) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
837 _blocks[i]->dump_head(&_bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
838 }
a61af66fc99e Initial load
duke
parents:
diff changeset
839 }
a61af66fc99e Initial load
duke
parents:
diff changeset
840
a61af66fc99e Initial load
duke
parents:
diff changeset
841 void PhaseCFG::verify( ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
842 // Verify sane CFG
a61af66fc99e Initial load
duke
parents:
diff changeset
843 for( uint i = 0; i < _num_blocks; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
844 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
845 uint cnt = b->_nodes.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
846 uint j;
a61af66fc99e Initial load
duke
parents:
diff changeset
847 for( j = 0; j < cnt; j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
848 Node *n = b->_nodes[j];
a61af66fc99e Initial load
duke
parents:
diff changeset
849 assert( _bbs[n->_idx] == b, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
850 if( j >= 1 && n->is_Mach() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
851 n->as_Mach()->ideal_Opcode() == Op_CreateEx ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
852 assert( j == 1 || b->_nodes[j-1]->is_Phi(),
a61af66fc99e Initial load
duke
parents:
diff changeset
853 "CreateEx must be first instruction in block" );
a61af66fc99e Initial load
duke
parents:
diff changeset
854 }
a61af66fc99e Initial load
duke
parents:
diff changeset
855 for( uint k = 0; k < n->req(); k++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
856 Node *use = n->in(k);
a61af66fc99e Initial load
duke
parents:
diff changeset
857 if( use && use != n ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
858 assert( _bbs[use->_idx] || use->is_Con(),
a61af66fc99e Initial load
duke
parents:
diff changeset
859 "must have block; constants for debug info ok" );
a61af66fc99e Initial load
duke
parents:
diff changeset
860 }
a61af66fc99e Initial load
duke
parents:
diff changeset
861 }
a61af66fc99e Initial load
duke
parents:
diff changeset
862 }
a61af66fc99e Initial load
duke
parents:
diff changeset
863
a61af66fc99e Initial load
duke
parents:
diff changeset
864 j = b->end_idx();
a61af66fc99e Initial load
duke
parents:
diff changeset
865 Node *bp = (Node*)b->_nodes[b->_nodes.size()-1]->is_block_proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
866 assert( bp, "last instruction must be a block proj" );
a61af66fc99e Initial load
duke
parents:
diff changeset
867 assert( bp == b->_nodes[j], "wrong number of successors for this block" );
a61af66fc99e Initial load
duke
parents:
diff changeset
868 if( bp->is_Catch() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
869 while( b->_nodes[--j]->Opcode() == Op_MachProj ) ;
a61af66fc99e Initial load
duke
parents:
diff changeset
870 assert( b->_nodes[j]->is_Call(), "CatchProj must follow call" );
a61af66fc99e Initial load
duke
parents:
diff changeset
871 }
a61af66fc99e Initial load
duke
parents:
diff changeset
872 else if( bp->is_Mach() && bp->as_Mach()->ideal_Opcode() == Op_If ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
873 assert( b->_num_succs == 2, "Conditional branch must have two targets");
a61af66fc99e Initial load
duke
parents:
diff changeset
874 }
a61af66fc99e Initial load
duke
parents:
diff changeset
875 }
a61af66fc99e Initial load
duke
parents:
diff changeset
876 }
a61af66fc99e Initial load
duke
parents:
diff changeset
877 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
878
a61af66fc99e Initial load
duke
parents:
diff changeset
879 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
880 //------------------------------UnionFind--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
881 UnionFind::UnionFind( uint max ) : _cnt(max), _max(max), _indices(NEW_RESOURCE_ARRAY(uint,max)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
882 Copy::zero_to_bytes( _indices, sizeof(uint)*max );
a61af66fc99e Initial load
duke
parents:
diff changeset
883 }
a61af66fc99e Initial load
duke
parents:
diff changeset
884
a61af66fc99e Initial load
duke
parents:
diff changeset
885 void UnionFind::extend( uint from_idx, uint to_idx ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
886 _nesting.check();
a61af66fc99e Initial load
duke
parents:
diff changeset
887 if( from_idx >= _max ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
888 uint size = 16;
a61af66fc99e Initial load
duke
parents:
diff changeset
889 while( size <= from_idx ) size <<=1;
a61af66fc99e Initial load
duke
parents:
diff changeset
890 _indices = REALLOC_RESOURCE_ARRAY( uint, _indices, _max, size );
a61af66fc99e Initial load
duke
parents:
diff changeset
891 _max = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
892 }
a61af66fc99e Initial load
duke
parents:
diff changeset
893 while( _cnt <= from_idx ) _indices[_cnt++] = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
894 _indices[from_idx] = to_idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
895 }
a61af66fc99e Initial load
duke
parents:
diff changeset
896
a61af66fc99e Initial load
duke
parents:
diff changeset
897 void UnionFind::reset( uint max ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
898 assert( max <= max_uint, "Must fit within uint" );
a61af66fc99e Initial load
duke
parents:
diff changeset
899 // Force the Union-Find mapping to be at least this large
a61af66fc99e Initial load
duke
parents:
diff changeset
900 extend(max,0);
a61af66fc99e Initial load
duke
parents:
diff changeset
901 // Initialize to be the ID mapping.
a61af66fc99e Initial load
duke
parents:
diff changeset
902 for( uint i=0; i<_max; i++ ) map(i,i);
a61af66fc99e Initial load
duke
parents:
diff changeset
903 }
a61af66fc99e Initial load
duke
parents:
diff changeset
904
a61af66fc99e Initial load
duke
parents:
diff changeset
905 //------------------------------Find_compress----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
906 // Straight out of Tarjan's union-find algorithm
a61af66fc99e Initial load
duke
parents:
diff changeset
907 uint UnionFind::Find_compress( uint idx ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
908 uint cur = idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
909 uint next = lookup(cur);
a61af66fc99e Initial load
duke
parents:
diff changeset
910 while( next != cur ) { // Scan chain of equivalences
a61af66fc99e Initial load
duke
parents:
diff changeset
911 assert( next < cur, "always union smaller" );
a61af66fc99e Initial load
duke
parents:
diff changeset
912 cur = next; // until find a fixed-point
a61af66fc99e Initial load
duke
parents:
diff changeset
913 next = lookup(cur);
a61af66fc99e Initial load
duke
parents:
diff changeset
914 }
a61af66fc99e Initial load
duke
parents:
diff changeset
915 // Core of union-find algorithm: update chain of
a61af66fc99e Initial load
duke
parents:
diff changeset
916 // equivalences to be equal to the root.
a61af66fc99e Initial load
duke
parents:
diff changeset
917 while( idx != next ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
918 uint tmp = lookup(idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
919 map(idx, next);
a61af66fc99e Initial load
duke
parents:
diff changeset
920 idx = tmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
921 }
a61af66fc99e Initial load
duke
parents:
diff changeset
922 return idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
923 }
a61af66fc99e Initial load
duke
parents:
diff changeset
924
a61af66fc99e Initial load
duke
parents:
diff changeset
925 //------------------------------Find_const-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
926 // Like Find above, but no path compress, so bad asymptotic behavior
a61af66fc99e Initial load
duke
parents:
diff changeset
927 uint UnionFind::Find_const( uint idx ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
928 if( idx == 0 ) return idx; // Ignore the zero idx
a61af66fc99e Initial load
duke
parents:
diff changeset
929 // Off the end? This can happen during debugging dumps
a61af66fc99e Initial load
duke
parents:
diff changeset
930 // when data structures have not finished being updated.
a61af66fc99e Initial load
duke
parents:
diff changeset
931 if( idx >= _max ) return idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
932 uint next = lookup(idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
933 while( next != idx ) { // Scan chain of equivalences
a61af66fc99e Initial load
duke
parents:
diff changeset
934 assert( next < idx, "always union smaller" );
a61af66fc99e Initial load
duke
parents:
diff changeset
935 idx = next; // until find a fixed-point
a61af66fc99e Initial load
duke
parents:
diff changeset
936 next = lookup(idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
937 }
a61af66fc99e Initial load
duke
parents:
diff changeset
938 return next;
a61af66fc99e Initial load
duke
parents:
diff changeset
939 }
a61af66fc99e Initial load
duke
parents:
diff changeset
940
a61af66fc99e Initial load
duke
parents:
diff changeset
941 //------------------------------Union------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
942 // union 2 sets together.
a61af66fc99e Initial load
duke
parents:
diff changeset
943 void UnionFind::Union( uint idx1, uint idx2 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
944 uint src = Find(idx1);
a61af66fc99e Initial load
duke
parents:
diff changeset
945 uint dst = Find(idx2);
a61af66fc99e Initial load
duke
parents:
diff changeset
946 assert( src, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
947 assert( dst, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
948 assert( src < _max, "oob" );
a61af66fc99e Initial load
duke
parents:
diff changeset
949 assert( dst < _max, "oob" );
a61af66fc99e Initial load
duke
parents:
diff changeset
950 assert( src < dst, "always union smaller" );
a61af66fc99e Initial load
duke
parents:
diff changeset
951 map(dst,src);
a61af66fc99e Initial load
duke
parents:
diff changeset
952 }