live.cpp revision 579:0fbdb4381b99
1/*
2 * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_live.cpp.incl"
27
28
29
30//=============================================================================
31//------------------------------PhaseLive--------------------------------------
32// Compute live-in/live-out.  We use a totally incremental algorithm.  The LIVE
33// problem is monotonic.  The steady-state solution looks like this: pull a
34// block from the worklist.  It has a set of delta's - values which are newly
35// live-in from the block.  Push these to the live-out sets of all predecessor
36// blocks.  At each predecessor, the new live-out values are ANDed with what is
37// already live-out (extra stuff is added to the live-out sets).  Then the
38// remaining new live-out values are ANDed with what is locally defined.
39// Leftover bits become the new live-in for the predecessor block, and the pred
40// block is put on the worklist.
41//   The locally live-in stuff is computed once and added to predecessor
42// live-out sets.  This seperate compilation is done in the outer loop below.
43PhaseLive::PhaseLive( const PhaseCFG &cfg, LRG_List &names, Arena *arena ) : Phase(LIVE), _cfg(cfg), _names(names), _arena(arena), _live(0) {
44}
45
46void PhaseLive::compute(uint maxlrg) {
47  _maxlrg   = maxlrg;
48  _worklist = new (_arena) Block_List();
49
50  // Init the sparse live arrays.  This data is live on exit from here!
51  // The _live info is the live-out info.
52  _live = (IndexSet*)_arena->Amalloc(sizeof(IndexSet)*_cfg._num_blocks);
53  uint i;
54  for( i=0; i<_cfg._num_blocks; i++ ) {
55    _live[i].initialize(_maxlrg);
56  }
57
58  // Init the sparse arrays for delta-sets.
59  ResourceMark rm;              // Nuke temp storage on exit
60
61  // Does the memory used by _defs and _deltas get reclaimed?  Does it matter?  TT
62
63  // Array of values defined locally in blocks
64  _defs = NEW_RESOURCE_ARRAY(IndexSet,_cfg._num_blocks);
65  for( i=0; i<_cfg._num_blocks; i++ ) {
66    _defs[i].initialize(_maxlrg);
67  }
68
69  // Array of delta-set pointers, indexed by block pre_order-1.
70  _deltas = NEW_RESOURCE_ARRAY(IndexSet*,_cfg._num_blocks);
71  memset( _deltas, 0, sizeof(IndexSet*)* _cfg._num_blocks);
72
73  _free_IndexSet = NULL;
74
75  // Blocks having done pass-1
76  VectorSet first_pass(Thread::current()->resource_area());
77
78  // Outer loop: must compute local live-in sets and push into predecessors.
79  uint iters = _cfg._num_blocks;        // stat counters
80  for( uint j=_cfg._num_blocks; j>0; j-- ) {
81    Block *b = _cfg._blocks[j-1];
82
83    // Compute the local live-in set.  Start with any new live-out bits.
84    IndexSet *use = getset( b );
85    IndexSet *def = &_defs[b->_pre_order-1];
86    DEBUG_ONLY(IndexSet *def_outside = getfreeset();)
87    uint i;
88    for( i=b->_nodes.size(); i>1; i-- ) {
89      Node *n = b->_nodes[i-1];
90      if( n->is_Phi() ) break;
91
92      uint r = _names[n->_idx];
93      assert(!def_outside->member(r), "Use of external LRG overlaps the same LRG defined in this block");
94      def->insert( r );
95      use->remove( r );
96      uint cnt = n->req();
97      for( uint k=1; k<cnt; k++ ) {
98        Node *nk = n->in(k);
99        uint nkidx = nk->_idx;
100        if( _cfg._bbs[nkidx] != b ) {
101          uint u = _names[nkidx];
102          use->insert( u );
103          DEBUG_ONLY(def_outside->insert( u );)
104        }
105      }
106    }
107#ifdef ASSERT
108    def_outside->set_next(_free_IndexSet);
109    _free_IndexSet = def_outside;     // Drop onto free list
110#endif
111    // Remove anything defined by Phis and the block start instruction
112    for( uint k=i; k>0; k-- ) {
113      uint r = _names[b->_nodes[k-1]->_idx];
114      def->insert( r );
115      use->remove( r );
116    }
117
118    // Push these live-in things to predecessors
119    for( uint l=1; l<b->num_preds(); l++ ) {
120      Block *p = _cfg._bbs[b->pred(l)->_idx];
121      add_liveout( p, use, first_pass );
122
123      // PhiNode uses go in the live-out set of prior blocks.
124      for( uint k=i; k>0; k-- )
125        add_liveout( p, _names[b->_nodes[k-1]->in(l)->_idx], first_pass );
126    }
127    freeset( b );
128    first_pass.set(b->_pre_order);
129
130    // Inner loop: blocks that picked up new live-out values to be propagated
131    while( _worklist->size() ) {
132        // !!!!!
133// #ifdef ASSERT
134      iters++;
135// #endif
136      Block *b = _worklist->pop();
137      IndexSet *delta = getset(b);
138      assert( delta->count(), "missing delta set" );
139
140      // Add new-live-in to predecessors live-out sets
141      for( uint l=1; l<b->num_preds(); l++ )
142        add_liveout( _cfg._bbs[b->pred(l)->_idx], delta, first_pass );
143
144      freeset(b);
145    } // End of while-worklist-not-empty
146
147  } // End of for-all-blocks-outer-loop
148
149  // We explicitly clear all of the IndexSets which we are about to release.
150  // This allows us to recycle their internal memory into IndexSet's free list.
151
152  for( i=0; i<_cfg._num_blocks; i++ ) {
153    _defs[i].clear();
154    if (_deltas[i]) {
155      // Is this always true?
156      _deltas[i]->clear();
157    }
158  }
159  IndexSet *free = _free_IndexSet;
160  while (free != NULL) {
161    IndexSet *temp = free;
162    free = free->next();
163    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.
274void PhaseChaitin::verify_base_ptrs( ResourceArea *a ) const {
275#ifdef ASSERT
276  Unique_Node_List worklist(a);
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++) {
291              Node *check = sfpt->in(idx);
292              bool is_derived = ((idx - jvms->oopoff()) & 1) == 0;
293              // search upwards through spills and spill phis for AddP
294              worklist.clear();
295              worklist.push(check);
296              uint k = 0;
297              while( k < worklist.size() ) {
298                check = worklist.at(k);
299                assert(check,"Bad base or derived pointer");
300                // See PhaseChaitin::find_base_for_derived() for all cases.
301                int isc = check->is_Copy();
302                if( isc ) {
303                  worklist.push(check->in(isc));
304                } else if( check->is_Phi() ) {
305                  for (uint m = 1; m < check->req(); m++)
306                    worklist.push(check->in(m));
307                } else if( check->is_Con() ) {
308                  if (is_derived) {
309                    // Derived is NULL+offset
310                    assert(!is_derived || check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad derived pointer");
311                  } else {
312                    assert(check->bottom_type()->is_ptr()->_offset == 0,"Bad base pointer");
313                    // Base either ConP(NULL) or loadConP
314                    if (check->is_Mach()) {
315                      assert(check->as_Mach()->ideal_Opcode() == Op_ConP,"Bad base pointer");
316                    } else {
317                      assert(check->Opcode() == Op_ConP &&
318                             check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad base pointer");
319                    }
320                  }
321                } else if( check->bottom_type()->is_ptr()->_offset == 0 ) {
322                  if(check->is_Proj() || check->is_Mach() &&
323                     (check->as_Mach()->ideal_Opcode() == Op_CreateEx ||
324                      check->as_Mach()->ideal_Opcode() == Op_ThreadLocal ||
325                      check->as_Mach()->ideal_Opcode() == Op_CMoveP ||
326                      check->as_Mach()->ideal_Opcode() == Op_CheckCastPP ||
327#ifdef _LP64
328                      UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_CastPP ||
329                      UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_DecodeN ||
330#endif
331                      check->as_Mach()->ideal_Opcode() == Op_LoadP ||
332                      check->as_Mach()->ideal_Opcode() == Op_LoadKlass)) {
333                    // Valid nodes
334                  } else {
335                    check->dump();
336                    assert(false,"Bad base or derived pointer");
337                  }
338                } else {
339                  assert(is_derived,"Bad base pointer");
340                  assert(check->is_Mach() && check->as_Mach()->ideal_Opcode() == Op_AddP,"Bad derived pointer");
341                }
342                k++;
343                assert(k < 100000,"Derived pointer checking in infinite loop");
344              } // End while
345            }
346          } // End of check for derived pointers
347        } // End of Kcheck for debug info
348      } // End of if found a safepoint
349    } // End of forall instructions in block
350  } // End of forall blocks
351#endif
352}
353
354//------------------------------verify-------------------------------------
355// Verify that graphs and base pointers are still sane.
356void PhaseChaitin::verify( ResourceArea *a, bool verify_ifg ) const {
357#ifdef ASSERT
358  if( VerifyOpto || VerifyRegisterAllocator ) {
359    _cfg.verify();
360    verify_base_ptrs(a);
361    if(verify_ifg)
362      _ifg->verify(this);
363  }
364#endif
365}
366
367#endif
368