1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "opto/callnode.hpp"
  28 #include "opto/chaitin.hpp"
  29 #include "opto/live.hpp"
  30 #include "opto/machnode.hpp"
  31 
  32 
  33 
  34 //=============================================================================
  35 //------------------------------PhaseLive--------------------------------------
  36 // Compute live-in/live-out.  We use a totally incremental algorithm.  The LIVE
  37 // problem is monotonic.  The steady-state solution looks like this: pull a
  38 // block from the worklist.  It has a set of delta's - values which are newly
  39 // live-in from the block.  Push these to the live-out sets of all predecessor
  40 // blocks.  At each predecessor, the new live-out values are ANDed with what is
  41 // already live-out (extra stuff is added to the live-out sets).  Then the
  42 // remaining new live-out values are ANDed with what is locally defined.
  43 // Leftover bits become the new live-in for the predecessor block, and the pred
  44 // block is put on the worklist.
  45 //   The locally live-in stuff is computed once and added to predecessor
  46 // live-out sets.  This separate compilation is done in the outer loop below.
  47 PhaseLive::PhaseLive( const PhaseCFG &cfg, const LRG_List &names, Arena *arena ) : Phase(LIVE), _cfg(cfg), _names(names), _arena(arena), _live(0) {
  48 }
  49 
  50 void PhaseLive::compute(uint maxlrg) {
  51   _maxlrg   = maxlrg;
  52   _worklist = new (_arena) Block_List();
  53 
  54   // Init the sparse live arrays.  This data is live on exit from here!
  55   // The _live info is the live-out info.
  56   _live = (IndexSet*)_arena->Amalloc(sizeof(IndexSet)*_cfg._num_blocks);
  57   uint i;
  58   for( i=0; i<_cfg._num_blocks; i++ ) {
  59     _live[i].initialize(_maxlrg);
  60   }
  61 
  62   // Init the sparse arrays for delta-sets.
  63   ResourceMark rm;              // Nuke temp storage on exit
  64 
  65   // Does the memory used by _defs and _deltas get reclaimed?  Does it matter?  TT
  66 
  67   // Array of values defined locally in blocks
  68   _defs = NEW_RESOURCE_ARRAY(IndexSet,_cfg._num_blocks);
  69   for( i=0; i<_cfg._num_blocks; i++ ) {
  70     _defs[i].initialize(_maxlrg);
  71   }
  72 
  73   // Array of delta-set pointers, indexed by block pre_order-1.
  74   _deltas = NEW_RESOURCE_ARRAY(IndexSet*,_cfg._num_blocks);
  75   memset( _deltas, 0, sizeof(IndexSet*)* _cfg._num_blocks);
  76 
  77   _free_IndexSet = NULL;
  78 
  79   // Blocks having done pass-1
  80   VectorSet first_pass(Thread::current()->resource_area());
  81 
  82   // Outer loop: must compute local live-in sets and push into predecessors.
  83   uint iters = _cfg._num_blocks;        // stat counters
  84   for( uint j=_cfg._num_blocks; j>0; j-- ) {
  85     Block *b = _cfg._blocks[j-1];
  86 
  87     // Compute the local live-in set.  Start with any new live-out bits.
  88     IndexSet *use = getset( b );
  89     IndexSet *def = &_defs[b->_pre_order-1];
  90     DEBUG_ONLY(IndexSet *def_outside = getfreeset();)
  91     uint i;
  92     for( i=b->_nodes.size(); i>1; i-- ) {
  93       Node *n = b->_nodes[i-1];
  94       if( n->is_Phi() ) break;
  95 
  96       uint r = _names[n->_idx];
  97       assert(!def_outside->member(r), "Use of external LRG overlaps the same LRG defined in this block");
  98       def->insert( r );
  99       use->remove( r );
 100       uint cnt = n->req();
 101       for( uint k=1; k<cnt; k++ ) {
 102         Node *nk = n->in(k);
 103         uint nkidx = nk->_idx;
 104         if (_cfg.get_block_for_node(nk) != b) {
 105           uint u = _names[nkidx];
 106           use->insert( u );
 107           DEBUG_ONLY(def_outside->insert( u );)
 108         }
 109       }
 110     }
 111 #ifdef ASSERT
 112     def_outside->set_next(_free_IndexSet);
 113     _free_IndexSet = def_outside;     // Drop onto free list
 114 #endif
 115     // Remove anything defined by Phis and the block start instruction
 116     for( uint k=i; k>0; k-- ) {
 117       uint r = _names[b->_nodes[k-1]->_idx];
 118       def->insert( r );
 119       use->remove( r );
 120     }
 121 
 122     // Push these live-in things to predecessors
 123     for( uint l=1; l<b->num_preds(); l++ ) {
 124       Block *p = _cfg.get_block_for_node(b->pred(l));
 125       add_liveout( p, use, first_pass );
 126 
 127       // PhiNode uses go in the live-out set of prior blocks.
 128       for( uint k=i; k>0; k-- )
 129         add_liveout( p, _names[b->_nodes[k-1]->in(l)->_idx], first_pass );
 130     }
 131     freeset( b );
 132     first_pass.set(b->_pre_order);
 133 
 134     // Inner loop: blocks that picked up new live-out values to be propagated
 135     while( _worklist->size() ) {
 136         // !!!!!
 137 // #ifdef ASSERT
 138       iters++;
 139 // #endif
 140       Block *b = _worklist->pop();
 141       IndexSet *delta = getset(b);
 142       assert( delta->count(), "missing delta set" );
 143 
 144       // Add new-live-in to predecessors live-out sets
 145       for (uint l = 1; l < b->num_preds(); l++) {
 146         Block* block = _cfg.get_block_for_node(b->pred(l));
 147         add_liveout(block, delta, first_pass);
 148       }
 149 
 150       freeset(b);
 151     } // End of while-worklist-not-empty
 152 
 153   } // End of for-all-blocks-outer-loop
 154 
 155   // We explicitly clear all of the IndexSets which we are about to release.
 156   // This allows us to recycle their internal memory into IndexSet's free list.
 157 
 158   for( i=0; i<_cfg._num_blocks; i++ ) {
 159     _defs[i].clear();
 160     if (_deltas[i]) {
 161       // Is this always true?
 162       _deltas[i]->clear();
 163     }
 164   }
 165   IndexSet *free = _free_IndexSet;
 166   while (free != NULL) {
 167     IndexSet *temp = free;
 168     free = free->next();
 169     temp->clear();
 170   }
 171 
 172 }
 173 
 174 //------------------------------stats------------------------------------------
 175 #ifndef PRODUCT
 176 void PhaseLive::stats(uint iters) const {
 177 }
 178 #endif
 179 
 180 //------------------------------getset-----------------------------------------
 181 // Get an IndexSet for a block.  Return existing one, if any.  Make a new
 182 // empty one if a prior one does not exist.
 183 IndexSet *PhaseLive::getset( Block *p ) {
 184   IndexSet *delta = _deltas[p->_pre_order-1];
 185   if( !delta )                  // Not on worklist?
 186     // Get a free set; flag as being on worklist
 187     delta = _deltas[p->_pre_order-1] = getfreeset();
 188   return delta;                 // Return set of new live-out items
 189 }
 190 
 191 //------------------------------getfreeset-------------------------------------
 192 // Pull from free list, or allocate.  Internal allocation on the returned set
 193 // is always from thread local storage.
 194 IndexSet *PhaseLive::getfreeset( ) {
 195   IndexSet *f = _free_IndexSet;
 196   if( !f ) {
 197     f = new IndexSet;
 198 //    f->set_arena(Thread::current()->resource_area());
 199     f->initialize(_maxlrg, Thread::current()->resource_area());
 200   } else {
 201     // Pull from free list
 202     _free_IndexSet = f->next();
 203   //f->_cnt = 0;                        // Reset to empty
 204 //    f->set_arena(Thread::current()->resource_area());
 205     f->initialize(_maxlrg, Thread::current()->resource_area());
 206   }
 207   return f;
 208 }
 209 
 210 //------------------------------freeset----------------------------------------
 211 // Free an IndexSet from a block.
 212 void PhaseLive::freeset( const Block *p ) {
 213   IndexSet *f = _deltas[p->_pre_order-1];
 214   f->set_next(_free_IndexSet);
 215   _free_IndexSet = f;           // Drop onto free list
 216   _deltas[p->_pre_order-1] = NULL;
 217 }
 218 
 219 //------------------------------add_liveout------------------------------------
 220 // Add a live-out value to a given blocks live-out set.  If it is new, then
 221 // also add it to the delta set and stick the block on the worklist.
 222 void PhaseLive::add_liveout( Block *p, uint r, VectorSet &first_pass ) {
 223   IndexSet *live = &_live[p->_pre_order-1];
 224   if( live->insert(r) ) {       // If actually inserted...
 225     // We extended the live-out set.  See if the value is generated locally.
 226     // If it is not, then we must extend the live-in set.
 227     if( !_defs[p->_pre_order-1].member( r ) ) {
 228       if( !_deltas[p->_pre_order-1] && // Not on worklist?
 229           first_pass.test(p->_pre_order) )
 230         _worklist->push(p);     // Actually go on worklist if already 1st pass
 231       getset(p)->insert(r);
 232     }
 233   }
 234 }
 235 
 236 
 237 //------------------------------add_liveout------------------------------------
 238 // Add a vector of live-out values to a given blocks live-out set.
 239 void PhaseLive::add_liveout( Block *p, IndexSet *lo, VectorSet &first_pass ) {
 240   IndexSet *live = &_live[p->_pre_order-1];
 241   IndexSet *defs = &_defs[p->_pre_order-1];
 242   IndexSet *on_worklist = _deltas[p->_pre_order-1];
 243   IndexSet *delta = on_worklist ? on_worklist : getfreeset();
 244 
 245   IndexSetIterator elements(lo);
 246   uint r;
 247   while ((r = elements.next()) != 0) {
 248     if( live->insert(r) &&      // If actually inserted...
 249         !defs->member( r ) )    // and not defined locally
 250       delta->insert(r);         // Then add to live-in set
 251   }
 252 
 253   if( delta->count() ) {                // If actually added things
 254     _deltas[p->_pre_order-1] = delta; // Flag as on worklist now
 255     if( !on_worklist &&         // Not on worklist?
 256         first_pass.test(p->_pre_order) )
 257       _worklist->push(p);       // Actually go on worklist if already 1st pass
 258   } else {                      // Nothing there; just free it
 259     delta->set_next(_free_IndexSet);
 260     _free_IndexSet = delta;     // Drop onto free list
 261   }
 262 }
 263 
 264 #ifndef PRODUCT
 265 //------------------------------dump-------------------------------------------
 266 // Dump the live-out set for a block
 267 void PhaseLive::dump( const Block *b ) const {
 268   tty->print("Block %d: ",b->_pre_order);
 269   tty->print("LiveOut: ");  _live[b->_pre_order-1].dump();
 270   uint cnt = b->_nodes.size();
 271   for( uint i=0; i<cnt; i++ ) {
 272     tty->print("L%d/", _names[b->_nodes[i]->_idx] );
 273     b->_nodes[i]->dump();
 274   }
 275   tty->print("\n");
 276 }
 277 
 278 //------------------------------verify_base_ptrs-------------------------------
 279 // Verify that base pointers and derived pointers are still sane.
 280 void PhaseChaitin::verify_base_ptrs( ResourceArea *a ) const {
 281 #ifdef ASSERT
 282   Unique_Node_List worklist(a);
 283   for( uint i = 0; i < _cfg._num_blocks; i++ ) {
 284     Block *b = _cfg._blocks[i];
 285     for( uint j = b->end_idx() + 1; j > 1; j-- ) {
 286       Node *n = b->_nodes[j-1];
 287       if( n->is_Phi() ) break;
 288       // Found a safepoint?
 289       if( n->is_MachSafePoint() ) {
 290         MachSafePointNode *sfpt = n->as_MachSafePoint();
 291         JVMState* jvms = sfpt->jvms();
 292         if (jvms != NULL) {
 293           // Now scan for a live derived pointer
 294           if (jvms->oopoff() < sfpt->req()) {
 295             // Check each derived/base pair
 296             for (uint idx = jvms->oopoff(); idx < sfpt->req(); idx++) {
 297               Node *check = sfpt->in(idx);
 298               bool is_derived = ((idx - jvms->oopoff()) & 1) == 0;
 299               // search upwards through spills and spill phis for AddP
 300               worklist.clear();
 301               worklist.push(check);
 302               uint k = 0;
 303               while( k < worklist.size() ) {
 304                 check = worklist.at(k);
 305                 assert(check,"Bad base or derived pointer");
 306                 // See PhaseChaitin::find_base_for_derived() for all cases.
 307                 int isc = check->is_Copy();
 308                 if( isc ) {
 309                   worklist.push(check->in(isc));
 310                 } else if( check->is_Phi() ) {
 311                   for (uint m = 1; m < check->req(); m++)
 312                     worklist.push(check->in(m));
 313                 } else if( check->is_Con() ) {
 314                   if (is_derived) {
 315                     // Derived is NULL+offset
 316                     assert(!is_derived || check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad derived pointer");
 317                   } else {
 318                     assert(check->bottom_type()->is_ptr()->_offset == 0,"Bad base pointer");
 319                     // Base either ConP(NULL) or loadConP
 320                     if (check->is_Mach()) {
 321                       assert(check->as_Mach()->ideal_Opcode() == Op_ConP,"Bad base pointer");
 322                     } else {
 323                       assert(check->Opcode() == Op_ConP &&
 324                              check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad base pointer");
 325                     }
 326                   }
 327                 } else if( check->bottom_type()->is_ptr()->_offset == 0 ) {
 328                   if(check->is_Proj() || check->is_Mach() &&
 329                      (check->as_Mach()->ideal_Opcode() == Op_CreateEx ||
 330                       check->as_Mach()->ideal_Opcode() == Op_ThreadLocal ||
 331                       check->as_Mach()->ideal_Opcode() == Op_CMoveP ||
 332                       check->as_Mach()->ideal_Opcode() == Op_CheckCastPP ||
 333 #ifdef _LP64
 334                       UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_CastPP ||
 335                       UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_DecodeN ||
 336                       UseCompressedKlassPointers && check->as_Mach()->ideal_Opcode() == Op_DecodeNKlass ||
 337 #endif
 338                       check->as_Mach()->ideal_Opcode() == Op_LoadP ||
 339                       check->as_Mach()->ideal_Opcode() == Op_LoadKlass)) {
 340                     // Valid nodes
 341                   } else {
 342                     check->dump();
 343                     assert(false,"Bad base or derived pointer");
 344                   }
 345                 } else {
 346                   assert(is_derived,"Bad base pointer");
 347                   assert(check->is_Mach() && check->as_Mach()->ideal_Opcode() == Op_AddP,"Bad derived pointer");
 348                 }
 349                 k++;
 350                 assert(k < 100000,"Derived pointer checking in infinite loop");
 351               } // End while
 352             }
 353           } // End of check for derived pointers
 354         } // End of Kcheck for debug info
 355       } // End of if found a safepoint
 356     } // End of forall instructions in block
 357   } // End of forall blocks
 358 #endif
 359 }
 360 
 361 //------------------------------verify-------------------------------------
 362 // Verify that graphs and base pointers are still sane.
 363 void PhaseChaitin::verify( ResourceArea *a, bool verify_ifg ) const {
 364 #ifdef ASSERT
 365   if( VerifyOpto || VerifyRegisterAllocator ) {
 366     _cfg.verify();
 367     verify_base_ptrs(a);
 368     if(verify_ifg)
 369       _ifg->verify(this);
 370   }
 371 #endif
 372 }
 373 
 374 #endif