live.cpp revision 0:a61af66fc99e
138494Sobrien/* 238494Sobrien * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. 338494Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 438494Sobrien * 5119679Smbr * This code is free software; you can redistribute it and/or modify it 638494Sobrien * under the terms of the GNU General Public License version 2 only, as 7119679Smbr * published by the Free Software Foundation. 8119679Smbr * 9119679Smbr * This code is distributed in the hope that it will be useful, but WITHOUT 10119679Smbr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11119679Smbr * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12119679Smbr * version 2 for more details (a copy is included in the LICENSE file that 13119679Smbr * accompanied this code). 14119679Smbr * 15119679Smbr * You should have received a copy of the GNU General Public License version 16119679Smbr * 2 along with this work; if not, write to the Free Software Foundation, 17119679Smbr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18119679Smbr * 19119679Smbr * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20119679Smbr * CA 95054 USA or visit www.sun.com if you need additional information or 21119679Smbr * have any questions. 22119679Smbr * 23119679Smbr */ 24119679Smbr 25119679Smbr#include "incls/_precompiled.incl" 26119679Smbr#include "incls/_live.cpp.incl" 27119679Smbr 28119679Smbr 29119679Smbr 30119679Smbr//============================================================================= 31119679Smbr//------------------------------PhaseLive-------------------------------------- 32119679Smbr// Compute live-in/live-out. We use a totally incremental algorithm. The LIVE 33119679Smbr// problem is monotonic. The steady-state solution looks like this: pull a 34119679Smbr// block from the worklist. It has a set of delta's - values which are newly 35119679Smbr// live-in from the block. Push these to the live-out sets of all predecessor 36119679Smbr// blocks. At each predecessor, the new live-out values are ANDed with what is 37119679Smbr// already live-out (extra stuff is added to the live-out sets). Then the 38119679Smbr// remaining new live-out values are ANDed with what is locally defined. 39119679Smbr// Leftover bits become the new live-in for the predecessor block, and the pred 40119679Smbr// block is put on the worklist. 41119679Smbr// The locally live-in stuff is computed once and added to predecessor 42119679Smbr// live-out sets. This seperate compilation is done in the outer loop below. 43119679SmbrPhaseLive::PhaseLive( const PhaseCFG &cfg, LRG_List &names, Arena *arena ) : Phase(LIVE), _cfg(cfg), _names(names), _arena(arena), _live(0) { 44119679Smbr} 45119679Smbr 46119679Smbrvoid PhaseLive::compute(uint maxlrg) { 47119679Smbr _maxlrg = maxlrg; 48119679Smbr _worklist = new (_arena) Block_List(); 49119679Smbr 50119679Smbr // Init the sparse live arrays. This data is live on exit from here! 51119679Smbr // The _live info is the live-out info. 52119679Smbr _live = (IndexSet*)_arena->Amalloc(sizeof(IndexSet)*_cfg._num_blocks); 53119679Smbr uint i; 54119679Smbr for( i=0; i<_cfg._num_blocks; i++ ) { 55119679Smbr _live[i].initialize(_maxlrg); 56119679Smbr } 57119679Smbr 58119679Smbr // Init the sparse arrays for delta-sets. 59119679Smbr ResourceMark rm; // Nuke temp storage on exit 60119679Smbr 61119679Smbr // Does the memory used by _defs and _deltas get reclaimed? Does it matter? TT 62119679Smbr 63119679Smbr // Array of values defined locally in blocks 64119679Smbr _defs = NEW_RESOURCE_ARRAY(IndexSet,_cfg._num_blocks); 65119679Smbr for( i=0; i<_cfg._num_blocks; i++ ) { 66119679Smbr _defs[i].initialize(_maxlrg); 67119679Smbr } 68119679Smbr 69119679Smbr // Array of delta-set pointers, indexed by block pre_order-1. 70119679Smbr _deltas = NEW_RESOURCE_ARRAY(IndexSet*,_cfg._num_blocks); 71119679Smbr memset( _deltas, 0, sizeof(IndexSet*)* _cfg._num_blocks); 72119679Smbr 73119679Smbr _free_IndexSet = NULL; 74119679Smbr 75119679Smbr // Blocks having done pass-1 76119679Smbr VectorSet first_pass(Thread::current()->resource_area()); 77119679Smbr 78119679Smbr // Outer loop: must compute local live-in sets and push into predecessors. 79119679Smbr uint iters = _cfg._num_blocks; // stat counters 80119679Smbr for( uint j=_cfg._num_blocks; j>0; j-- ) { 81119679Smbr Block *b = _cfg._blocks[j-1]; 82119679Smbr 83119679Smbr // Compute the local live-in set. Start with any new live-out bits. 84119679Smbr IndexSet *use = getset( b ); 85119679Smbr IndexSet *def = &_defs[b->_pre_order-1]; 86119679Smbr DEBUG_ONLY(IndexSet *def_outside = getfreeset();) 87119679Smbr uint i; 88119679Smbr for( i=b->_nodes.size(); i>1; i-- ) { 89119679Smbr Node *n = b->_nodes[i-1]; 90119679Smbr if( n->is_Phi() ) break; 91119679Smbr 92119679Smbr uint r = _names[n->_idx]; 93119679Smbr assert(!def_outside->member(r), "Use of external LRG overlaps the same LRG defined in this block"); 94119679Smbr def->insert( r ); 95119679Smbr use->remove( r ); 96119679Smbr uint cnt = n->req(); 97119679Smbr for( uint k=1; k<cnt; k++ ) { 98119679Smbr Node *nk = n->in(k); 99119679Smbr uint nkidx = nk->_idx; 100119679Smbr if( _cfg._bbs[nkidx] != b ) { 101119679Smbr uint u = _names[nkidx]; 102119679Smbr use->insert( u ); 103119679Smbr DEBUG_ONLY(def_outside->insert( u );) 104119679Smbr } 105119679Smbr } 106119679Smbr } 107119679Smbr#ifdef ASSERT 108119679Smbr def_outside->set_next(_free_IndexSet); 109119679Smbr _free_IndexSet = def_outside; // Drop onto free list 110119679Smbr#endif 11138494Sobrien // Remove anything defined by Phis and the block start instruction 11238494Sobrien for( uint k=i; k>0; k-- ) { 11338494Sobrien uint r = _names[b->_nodes[k-1]->_idx]; 114131702Smbr def->insert( r ); 11538494Sobrien use->remove( r ); 11638494Sobrien } 11742629Sobrien 118119679Smbr // Push these live-in things to predecessors 11942629Sobrien for( uint l=1; l<b->num_preds(); l++ ) { 12038494Sobrien Block *p = _cfg._bbs[b->pred(l)->_idx]; 12142629Sobrien add_liveout( p, use, first_pass ); 12238494Sobrien 12338494Sobrien // PhiNode uses go in the live-out set of prior blocks. 12438494Sobrien for( uint k=i; k>0; k-- ) 12538494Sobrien add_liveout( p, _names[b->_nodes[k-1]->in(l)->_idx], first_pass ); 12638494Sobrien } 12738494Sobrien freeset( b ); 128119679Smbr first_pass.set(b->_pre_order); 129119679Smbr 130119679Smbr // Inner loop: blocks that picked up new live-out values to be propagated 13138494Sobrien while( _worklist->size() ) { 13238494Sobrien // !!!!! 13338494Sobrien// #ifdef ASSERT 13438494Sobrien iters++; 13538494Sobrien// #endif 13638494Sobrien Block *b = _worklist->pop(); 13738494Sobrien IndexSet *delta = getset(b); 13838494Sobrien assert( delta->count(), "missing delta set" ); 13938494Sobrien 14038494Sobrien // Add new-live-in to predecessors live-out sets 14138494Sobrien for( uint l=1; l<b->num_preds(); l++ ) 14238494Sobrien add_liveout( _cfg._bbs[b->pred(l)->_idx], delta, first_pass ); 14338494Sobrien 14438494Sobrien freeset(b); 145119679Smbr } // End of while-worklist-not-empty 14638494Sobrien 14738494Sobrien } // End of for-all-blocks-outer-loop 14838494Sobrien 14938494Sobrien // We explicitly clear all of the IndexSets which we are about to release. 15038494Sobrien // This allows us to recycle their internal memory into IndexSet's free list. 15138494Sobrien 15238494Sobrien for( i=0; i<_cfg._num_blocks; i++ ) { 15338494Sobrien _defs[i].clear(); 15438494Sobrien if (_deltas[i]) { 15538494Sobrien // Is this always true? 15638494Sobrien _deltas[i]->clear(); 15738494Sobrien } 15838494Sobrien } 15938494Sobrien IndexSet *free = _free_IndexSet; 16038494Sobrien while (free != NULL) { 16138494Sobrien IndexSet *temp = free; 16238494Sobrien free = free->next(); 16338494Sobrien temp->clear(); 164 } 165 166} 167 168//------------------------------stats------------------------------------------ 169#ifndef PRODUCT 170void PhaseLive::stats(uint iters) const { 171} 172#endif 173 174//------------------------------getset----------------------------------------- 175// Get an IndexSet for a block. Return existing one, if any. Make a new 176// empty one if a prior one does not exist. 177IndexSet *PhaseLive::getset( Block *p ) { 178 IndexSet *delta = _deltas[p->_pre_order-1]; 179 if( !delta ) // Not on worklist? 180 // Get a free set; flag as being on worklist 181 delta = _deltas[p->_pre_order-1] = getfreeset(); 182 return delta; // Return set of new live-out items 183} 184 185//------------------------------getfreeset------------------------------------- 186// Pull from free list, or allocate. Internal allocation on the returned set 187// is always from thread local storage. 188IndexSet *PhaseLive::getfreeset( ) { 189 IndexSet *f = _free_IndexSet; 190 if( !f ) { 191 f = new IndexSet; 192// f->set_arena(Thread::current()->resource_area()); 193 f->initialize(_maxlrg, Thread::current()->resource_area()); 194 } else { 195 // Pull from free list 196 _free_IndexSet = f->next(); 197 //f->_cnt = 0; // Reset to empty 198// f->set_arena(Thread::current()->resource_area()); 199 f->initialize(_maxlrg, Thread::current()->resource_area()); 200 } 201 return f; 202} 203 204//------------------------------freeset---------------------------------------- 205// Free an IndexSet from a block. 206void PhaseLive::freeset( const Block *p ) { 207 IndexSet *f = _deltas[p->_pre_order-1]; 208 f->set_next(_free_IndexSet); 209 _free_IndexSet = f; // Drop onto free list 210 _deltas[p->_pre_order-1] = NULL; 211} 212 213//------------------------------add_liveout------------------------------------ 214// Add a live-out value to a given blocks live-out set. If it is new, then 215// also add it to the delta set and stick the block on the worklist. 216void PhaseLive::add_liveout( Block *p, uint r, VectorSet &first_pass ) { 217 IndexSet *live = &_live[p->_pre_order-1]; 218 if( live->insert(r) ) { // If actually inserted... 219 // We extended the live-out set. See if the value is generated locally. 220 // If it is not, then we must extend the live-in set. 221 if( !_defs[p->_pre_order-1].member( r ) ) { 222 if( !_deltas[p->_pre_order-1] && // Not on worklist? 223 first_pass.test(p->_pre_order) ) 224 _worklist->push(p); // Actually go on worklist if already 1st pass 225 getset(p)->insert(r); 226 } 227 } 228} 229 230 231//------------------------------add_liveout------------------------------------ 232// Add a vector of live-out values to a given blocks live-out set. 233void PhaseLive::add_liveout( Block *p, IndexSet *lo, VectorSet &first_pass ) { 234 IndexSet *live = &_live[p->_pre_order-1]; 235 IndexSet *defs = &_defs[p->_pre_order-1]; 236 IndexSet *on_worklist = _deltas[p->_pre_order-1]; 237 IndexSet *delta = on_worklist ? on_worklist : getfreeset(); 238 239 IndexSetIterator elements(lo); 240 uint r; 241 while ((r = elements.next()) != 0) { 242 if( live->insert(r) && // If actually inserted... 243 !defs->member( r ) ) // and not defined locally 244 delta->insert(r); // Then add to live-in set 245 } 246 247 if( delta->count() ) { // If actually added things 248 _deltas[p->_pre_order-1] = delta; // Flag as on worklist now 249 if( !on_worklist && // Not on worklist? 250 first_pass.test(p->_pre_order) ) 251 _worklist->push(p); // Actually go on worklist if already 1st pass 252 } else { // Nothing there; just free it 253 delta->set_next(_free_IndexSet); 254 _free_IndexSet = delta; // Drop onto free list 255 } 256} 257 258#ifndef PRODUCT 259//------------------------------dump------------------------------------------- 260// Dump the live-out set for a block 261void PhaseLive::dump( const Block *b ) const { 262 tty->print("Block %d: ",b->_pre_order); 263 tty->print("LiveOut: "); _live[b->_pre_order-1].dump(); 264 uint cnt = b->_nodes.size(); 265 for( uint i=0; i<cnt; i++ ) { 266 tty->print("L%d/", _names[b->_nodes[i]->_idx] ); 267 b->_nodes[i]->dump(); 268 } 269 tty->print("\n"); 270} 271 272//------------------------------verify_base_ptrs------------------------------- 273// Verify that base pointers and derived pointers are still sane. 274// Basically, if a derived pointer is live at a safepoint, then its 275// base pointer must be live also. 276void PhaseChaitin::verify_base_ptrs( ResourceArea *a ) const { 277 for( uint i = 0; i < _cfg._num_blocks; i++ ) { 278 Block *b = _cfg._blocks[i]; 279 for( uint j = b->end_idx() + 1; j > 1; j-- ) { 280 Node *n = b->_nodes[j-1]; 281 if( n->is_Phi() ) break; 282 // Found a safepoint? 283 if( n->is_MachSafePoint() ) { 284 MachSafePointNode *sfpt = n->as_MachSafePoint(); 285 JVMState* jvms = sfpt->jvms(); 286 if (jvms != NULL) { 287 // Now scan for a live derived pointer 288 if (jvms->oopoff() < sfpt->req()) { 289 // Check each derived/base pair 290 for (uint idx = jvms->oopoff(); idx < sfpt->req(); idx += 2) { 291 Node *check = sfpt->in(idx); 292 uint j = 0; 293 // search upwards through spills and spill phis for AddP 294 while(true) { 295 if( !check ) break; 296 int idx = check->is_Copy(); 297 if( idx ) { 298 check = check->in(idx); 299 } else if( check->is_Phi() && check->_idx >= _oldphi ) { 300 check = check->in(1); 301 } else 302 break; 303 j++; 304 assert(j < 100000,"Derived pointer checking in infinite loop"); 305 } // End while 306 assert(check->is_Mach() && check->as_Mach()->ideal_Opcode() == Op_AddP,"Bad derived pointer") 307 } 308 } // End of check for derived pointers 309 } // End of Kcheck for debug info 310 } // End of if found a safepoint 311 } // End of forall instructions in block 312 } // End of forall blocks 313} 314#endif 315