1 /*
   2  * Copyright (c) 2002, 2009, 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 "incls/_precompiled.incl"
  26 #include "incls/_buildOopMap.cpp.incl"
  27 
  28 // The functions in this file builds OopMaps after all scheduling is done.
  29 //
  30 // OopMaps contain a list of all registers and stack-slots containing oops (so
  31 // they can be updated by GC).  OopMaps also contain a list of derived-pointer
  32 // base-pointer pairs.  When the base is moved, the derived pointer moves to
  33 // follow it.  Finally, any registers holding callee-save values are also
  34 // recorded.  These might contain oops, but only the caller knows.
  35 //
  36 // BuildOopMaps implements a simple forward reaching-defs solution.  At each
  37 // GC point we'll have the reaching-def Nodes.  If the reaching Nodes are
  38 // typed as pointers (no offset), then they are oops.  Pointers+offsets are
  39 // derived pointers, and bases can be found from them.  Finally, we'll also
  40 // track reaching callee-save values.  Note that a copy of a callee-save value
  41 // "kills" it's source, so that only 1 copy of a callee-save value is alive at
  42 // a time.
  43 //
  44 // We run a simple bitvector liveness pass to help trim out dead oops.  Due to
  45 // irreducible loops, we can have a reaching def of an oop that only reaches
  46 // along one path and no way to know if it's valid or not on the other path.
  47 // The bitvectors are quite dense and the liveness pass is fast.
  48 //
  49 // At GC points, we consult this information to build OopMaps.  All reaching
  50 // defs typed as oops are added to the OopMap.  Only 1 instance of a
  51 // callee-save register can be recorded.  For derived pointers, we'll have to
  52 // find and record the register holding the base.
  53 //
  54 // The reaching def's is a simple 1-pass worklist approach.  I tried a clever
  55 // breadth-first approach but it was worse (showed O(n^2) in the
  56 // pick-next-block code).
  57 //
  58 // The relevant data is kept in a struct of arrays (it could just as well be
  59 // an array of structs, but the struct-of-arrays is generally a little more
  60 // efficient).  The arrays are indexed by register number (including
  61 // stack-slots as registers) and so is bounded by 200 to 300 elements in
  62 // practice.  One array will map to a reaching def Node (or NULL for
  63 // conflict/dead).  The other array will map to a callee-saved register or
  64 // OptoReg::Bad for not-callee-saved.
  65 
  66 
  67 //------------------------------OopFlow----------------------------------------
  68 // Structure to pass around
  69 struct OopFlow : public ResourceObj {
  70   short *_callees;              // Array mapping register to callee-saved
  71   Node **_defs;                 // array mapping register to reaching def
  72                                 // or NULL if dead/conflict
  73   // OopFlow structs, when not being actively modified, describe the _end_ of
  74   // this block.
  75   Block *_b;                    // Block for this struct
  76   OopFlow *_next;               // Next free OopFlow
  77                                 // or NULL if dead/conflict
  78   Compile* C;
  79 
  80   OopFlow( short *callees, Node **defs, Compile* c ) : _callees(callees), _defs(defs),
  81     _b(NULL), _next(NULL), C(c) { }
  82 
  83   // Given reaching-defs for this block start, compute it for this block end
  84   void compute_reach( PhaseRegAlloc *regalloc, int max_reg, Dict *safehash );
  85 
  86   // Merge these two OopFlows into the 'this' pointer.
  87   void merge( OopFlow *flow, int max_reg );
  88 
  89   // Copy a 'flow' over an existing flow
  90   void clone( OopFlow *flow, int max_size);
  91 
  92   // Make a new OopFlow from scratch
  93   static OopFlow *make( Arena *A, int max_size, Compile* C );
  94 
  95   // Build an oopmap from the current flow info
  96   OopMap *build_oop_map( Node *n, int max_reg, PhaseRegAlloc *regalloc, int* live );
  97 };
  98 
  99 //------------------------------compute_reach----------------------------------
 100 // Given reaching-defs for this block start, compute it for this block end
 101 void OopFlow::compute_reach( PhaseRegAlloc *regalloc, int max_reg, Dict *safehash ) {
 102 
 103   for( uint i=0; i<_b->_nodes.size(); i++ ) {
 104     Node *n = _b->_nodes[i];
 105 
 106     if( n->jvms() ) {           // Build an OopMap here?
 107       JVMState *jvms = n->jvms();
 108       // no map needed for leaf calls
 109       if( n->is_MachSafePoint() && !n->is_MachCallLeaf() ) {
 110         int *live = (int*) (*safehash)[n];
 111         assert( live, "must find live" );
 112         n->as_MachSafePoint()->set_oop_map( build_oop_map(n,max_reg,regalloc, live) );
 113       }
 114     }
 115 
 116     // Assign new reaching def's.
 117     // Note that I padded the _defs and _callees arrays so it's legal
 118     // to index at _defs[OptoReg::Bad].
 119     OptoReg::Name first = regalloc->get_reg_first(n);
 120     OptoReg::Name second = regalloc->get_reg_second(n);
 121     _defs[first] = n;
 122     _defs[second] = n;
 123 
 124     // Pass callee-save info around copies
 125     int idx = n->is_Copy();
 126     if( idx ) {                 // Copies move callee-save info
 127       OptoReg::Name old_first = regalloc->get_reg_first(n->in(idx));
 128       OptoReg::Name old_second = regalloc->get_reg_second(n->in(idx));
 129       int tmp_first = _callees[old_first];
 130       int tmp_second = _callees[old_second];
 131       _callees[old_first] = OptoReg::Bad; // callee-save is moved, dead in old location
 132       _callees[old_second] = OptoReg::Bad;
 133       _callees[first] = tmp_first;
 134       _callees[second] = tmp_second;
 135     } else if( n->is_Phi() ) {  // Phis do not mod callee-saves
 136       assert( _callees[first] == _callees[regalloc->get_reg_first(n->in(1))], "" );
 137       assert( _callees[second] == _callees[regalloc->get_reg_second(n->in(1))], "" );
 138       assert( _callees[first] == _callees[regalloc->get_reg_first(n->in(n->req()-1))], "" );
 139       assert( _callees[second] == _callees[regalloc->get_reg_second(n->in(n->req()-1))], "" );
 140     } else {
 141       _callees[first] = OptoReg::Bad; // No longer holding a callee-save value
 142       _callees[second] = OptoReg::Bad;
 143 
 144       // Find base case for callee saves
 145       if( n->is_Proj() && n->in(0)->is_Start() ) {
 146         if( OptoReg::is_reg(first) &&
 147             regalloc->_matcher.is_save_on_entry(first) )
 148           _callees[first] = first;
 149         if( OptoReg::is_reg(second) &&
 150             regalloc->_matcher.is_save_on_entry(second) )
 151           _callees[second] = second;
 152       }
 153     }
 154   }
 155 }
 156 
 157 //------------------------------merge------------------------------------------
 158 // Merge the given flow into the 'this' flow
 159 void OopFlow::merge( OopFlow *flow, int max_reg ) {
 160   assert( _b == NULL, "merging into a happy flow" );
 161   assert( flow->_b, "this flow is still alive" );
 162   assert( flow != this, "no self flow" );
 163 
 164   // Do the merge.  If there are any differences, drop to 'bottom' which
 165   // is OptoReg::Bad or NULL depending.
 166   for( int i=0; i<max_reg; i++ ) {
 167     // Merge the callee-save's
 168     if( _callees[i] != flow->_callees[i] )
 169       _callees[i] = OptoReg::Bad;
 170     // Merge the reaching defs
 171     if( _defs[i] != flow->_defs[i] )
 172       _defs[i] = NULL;
 173   }
 174 
 175 }
 176 
 177 //------------------------------clone------------------------------------------
 178 void OopFlow::clone( OopFlow *flow, int max_size ) {
 179   _b = flow->_b;
 180   memcpy( _callees, flow->_callees, sizeof(short)*max_size);
 181   memcpy( _defs   , flow->_defs   , sizeof(Node*)*max_size);
 182 }
 183 
 184 //------------------------------make-------------------------------------------
 185 OopFlow *OopFlow::make( Arena *A, int max_size, Compile* C ) {
 186   short *callees = NEW_ARENA_ARRAY(A,short,max_size+1);
 187   Node **defs    = NEW_ARENA_ARRAY(A,Node*,max_size+1);
 188   debug_only( memset(defs,0,(max_size+1)*sizeof(Node*)) );
 189   OopFlow *flow = new (A) OopFlow(callees+1, defs+1, C);
 190   assert( &flow->_callees[OptoReg::Bad] == callees, "Ok to index at OptoReg::Bad" );
 191   assert( &flow->_defs   [OptoReg::Bad] == defs   , "Ok to index at OptoReg::Bad" );
 192   return flow;
 193 }
 194 
 195 //------------------------------bit twiddlers----------------------------------
 196 static int get_live_bit( int *live, int reg ) {
 197   return live[reg>>LogBitsPerInt] &   (1<<(reg&(BitsPerInt-1))); }
 198 static void set_live_bit( int *live, int reg ) {
 199          live[reg>>LogBitsPerInt] |=  (1<<(reg&(BitsPerInt-1))); }
 200 static void clr_live_bit( int *live, int reg ) {
 201          live[reg>>LogBitsPerInt] &= ~(1<<(reg&(BitsPerInt-1))); }
 202 
 203 //------------------------------build_oop_map----------------------------------
 204 // Build an oopmap from the current flow info
 205 OopMap *OopFlow::build_oop_map( Node *n, int max_reg, PhaseRegAlloc *regalloc, int* live ) {
 206   int framesize = regalloc->_framesize;
 207   int max_inarg_slot = OptoReg::reg2stack(regalloc->_matcher._new_SP);
 208   debug_only( char *dup_check = NEW_RESOURCE_ARRAY(char,OptoReg::stack0());
 209               memset(dup_check,0,OptoReg::stack0()) );
 210 
 211   OopMap *omap = new OopMap( framesize,  max_inarg_slot );
 212   MachCallNode *mcall = n->is_MachCall() ? n->as_MachCall() : NULL;
 213   JVMState* jvms = n->jvms();
 214 
 215   // For all registers do...
 216   for( int reg=0; reg<max_reg; reg++ ) {
 217     if( get_live_bit(live,reg) == 0 )
 218       continue;                 // Ignore if not live
 219 
 220     // %%% C2 can use 2 OptoRegs when the physical register is only one 64bit
 221     // register in that case we'll get an non-concrete register for the second
 222     // half. We only need to tell the map the register once!
 223     //
 224     // However for the moment we disable this change and leave things as they
 225     // were.
 226 
 227     VMReg r = OptoReg::as_VMReg(OptoReg::Name(reg), framesize, max_inarg_slot);
 228 
 229     if (false && r->is_reg() && !r->is_concrete()) {
 230       continue;
 231     }
 232 
 233     // See if dead (no reaching def).
 234     Node *def = _defs[reg];     // Get reaching def
 235     assert( def, "since live better have reaching def" );
 236 
 237     // Classify the reaching def as oop, derived, callee-save, dead, or other
 238     const Type *t = def->bottom_type();
 239     if( t->isa_oop_ptr() ) {    // Oop or derived?
 240       assert( !OptoReg::is_valid(_callees[reg]), "oop can't be callee save" );
 241 #ifdef _LP64
 242       // 64-bit pointers record oop-ishness on 2 aligned adjacent registers.
 243       // Make sure both are record from the same reaching def, but do not
 244       // put both into the oopmap.
 245       if( (reg&1) == 1 ) {      // High half of oop-pair?
 246         assert( _defs[reg-1] == _defs[reg], "both halves from same reaching def" );
 247         continue;               // Do not record high parts in oopmap
 248       }
 249 #endif
 250 
 251       // Check for a legal reg name in the oopMap and bailout if it is not.
 252       if (!omap->legal_vm_reg_name(r)) {
 253         regalloc->C->record_method_not_compilable("illegal oopMap register name");
 254         continue;
 255       }
 256       if( t->is_ptr()->_offset == 0 ) { // Not derived?
 257         if( mcall ) {
 258           // Outgoing argument GC mask responsibility belongs to the callee,
 259           // not the caller.  Inspect the inputs to the call, to see if
 260           // this live-range is one of them.
 261           uint cnt = mcall->tf()->domain()->cnt();
 262           uint j;
 263           for( j = TypeFunc::Parms; j < cnt; j++)
 264             if( mcall->in(j) == def )
 265               break;            // reaching def is an argument oop
 266           if( j < cnt )         // arg oops dont go in GC map
 267             continue;           // Continue on to the next register
 268         }
 269         omap->set_oop(r);
 270       } else {                  // Else it's derived.
 271         // Find the base of the derived value.
 272         uint i;
 273         // Fast, common case, scan
 274         for( i = jvms->oopoff(); i < n->req(); i+=2 )
 275           if( n->in(i) == def ) break; // Common case
 276         if( i == n->req() ) {   // Missed, try a more generous scan
 277           // Scan again, but this time peek through copies
 278           for( i = jvms->oopoff(); i < n->req(); i+=2 ) {
 279             Node *m = n->in(i); // Get initial derived value
 280             while( 1 ) {
 281               Node *d = def;    // Get initial reaching def
 282               while( 1 ) {      // Follow copies of reaching def to end
 283                 if( m == d ) goto found; // breaks 3 loops
 284                 int idx = d->is_Copy();
 285                 if( !idx ) break;
 286                 d = d->in(idx);     // Link through copy
 287               }
 288               int idx = m->is_Copy();
 289               if( !idx ) break;
 290               m = m->in(idx);
 291             }
 292           }
 293           guarantee( 0, "must find derived/base pair" );
 294         }
 295       found: ;
 296         Node *base = n->in(i+1); // Base is other half of pair
 297         int breg = regalloc->get_reg_first(base);
 298         VMReg b = OptoReg::as_VMReg(OptoReg::Name(breg), framesize, max_inarg_slot);
 299 
 300         // I record liveness at safepoints BEFORE I make the inputs
 301         // live.  This is because argument oops are NOT live at a
 302         // safepoint (or at least they cannot appear in the oopmap).
 303         // Thus bases of base/derived pairs might not be in the
 304         // liveness data but they need to appear in the oopmap.
 305         if( get_live_bit(live,breg) == 0 ) {// Not live?
 306           // Flag it, so next derived pointer won't re-insert into oopmap
 307           set_live_bit(live,breg);
 308           // Already missed our turn?
 309           if( breg < reg ) {
 310             if (b->is_stack() || b->is_concrete() || true ) {
 311               omap->set_oop( b);
 312             }
 313           }
 314         }
 315         if (b->is_stack() || b->is_concrete() || true ) {
 316           omap->set_derived_oop( r, b);
 317         }
 318       }
 319 
 320     } else if( t->isa_narrowoop() ) {
 321       assert( !OptoReg::is_valid(_callees[reg]), "oop can't be callee save" );
 322       // Check for a legal reg name in the oopMap and bailout if it is not.
 323       if (!omap->legal_vm_reg_name(r)) {
 324         regalloc->C->record_method_not_compilable("illegal oopMap register name");
 325         continue;
 326       }
 327       if( mcall ) {
 328           // Outgoing argument GC mask responsibility belongs to the callee,
 329           // not the caller.  Inspect the inputs to the call, to see if
 330           // this live-range is one of them.
 331         uint cnt = mcall->tf()->domain()->cnt();
 332         uint j;
 333         for( j = TypeFunc::Parms; j < cnt; j++)
 334           if( mcall->in(j) == def )
 335             break;            // reaching def is an argument oop
 336         if( j < cnt )         // arg oops dont go in GC map
 337           continue;           // Continue on to the next register
 338       }
 339       omap->set_narrowoop(r);
 340     } else if( OptoReg::is_valid(_callees[reg])) { // callee-save?
 341       // It's a callee-save value
 342       assert( dup_check[_callees[reg]]==0, "trying to callee save same reg twice" );
 343       debug_only( dup_check[_callees[reg]]=1; )
 344       VMReg callee = OptoReg::as_VMReg(OptoReg::Name(_callees[reg]));
 345       if ( callee->is_concrete() || true ) {
 346         omap->set_callee_saved( r, callee);
 347       }
 348 
 349     } else {
 350       // Other - some reaching non-oop value
 351       omap->set_value( r);
 352 #ifdef ASSERT
 353       if( t->isa_rawptr() && C->cfg()->_raw_oops.member(def) ) {
 354         def->dump();
 355         n->dump();
 356         assert(false, "there should be a oop in OopMap instead of a live raw oop at safepoint");
 357       }
 358 #endif
 359     }
 360 
 361   }
 362 
 363 #ifdef ASSERT
 364   /* Nice, Intel-only assert
 365   int cnt_callee_saves=0;
 366   int reg2 = 0;
 367   while (OptoReg::is_reg(reg2)) {
 368     if( dup_check[reg2] != 0) cnt_callee_saves++;
 369     assert( cnt_callee_saves==3 || cnt_callee_saves==5, "missed some callee-save" );
 370     reg2++;
 371   }
 372   */
 373 #endif
 374 
 375 #ifdef ASSERT
 376   for( OopMapStream oms1(omap, OopMapValue::derived_oop_value); !oms1.is_done(); oms1.next()) {
 377     OopMapValue omv1 = oms1.current();
 378     bool found = false;
 379     for( OopMapStream oms2(omap,OopMapValue::oop_value); !oms2.is_done(); oms2.next()) {
 380       if( omv1.content_reg() == oms2.current().reg() ) {
 381         found = true;
 382         break;
 383       }
 384     }
 385     assert( found, "derived with no base in oopmap" );
 386   }
 387 #endif
 388 
 389   return omap;
 390 }
 391 
 392 //------------------------------do_liveness------------------------------------
 393 // Compute backwards liveness on registers
 394 static void do_liveness( PhaseRegAlloc *regalloc, PhaseCFG *cfg, Block_List *worklist, int max_reg_ints, Arena *A, Dict *safehash ) {
 395   int *live = NEW_ARENA_ARRAY(A, int, (cfg->_num_blocks+1) * max_reg_ints);
 396   int *tmp_live = &live[cfg->_num_blocks * max_reg_ints];
 397   Node *root = cfg->C->root();
 398   // On CISC platforms, get the node representing the stack pointer  that regalloc
 399   // used for spills
 400   Node *fp = NodeSentinel;
 401   if (UseCISCSpill && root->req() > 1) {
 402     fp = root->in(1)->in(TypeFunc::FramePtr);
 403   }
 404   memset( live, 0, cfg->_num_blocks * (max_reg_ints<<LogBytesPerInt) );
 405   // Push preds onto worklist
 406   for( uint i=1; i<root->req(); i++ )
 407     worklist->push(cfg->_bbs[root->in(i)->_idx]);
 408 
 409   // ZKM.jar includes tiny infinite loops which are unreached from below.
 410   // If we missed any blocks, we'll retry here after pushing all missed
 411   // blocks on the worklist.  Normally this outer loop never trips more
 412   // than once.
 413   while( 1 ) {
 414 
 415     while( worklist->size() ) { // Standard worklist algorithm
 416       Block *b = worklist->rpop();
 417 
 418       // Copy first successor into my tmp_live space
 419       int s0num = b->_succs[0]->_pre_order;
 420       int *t = &live[s0num*max_reg_ints];
 421       for( int i=0; i<max_reg_ints; i++ )
 422         tmp_live[i] = t[i];
 423 
 424       // OR in the remaining live registers
 425       for( uint j=1; j<b->_num_succs; j++ ) {
 426         uint sjnum = b->_succs[j]->_pre_order;
 427         int *t = &live[sjnum*max_reg_ints];
 428         for( int i=0; i<max_reg_ints; i++ )
 429           tmp_live[i] |= t[i];
 430       }
 431 
 432       // Now walk tmp_live up the block backwards, computing live
 433       for( int k=b->_nodes.size()-1; k>=0; k-- ) {
 434         Node *n = b->_nodes[k];
 435         // KILL def'd bits
 436         int first = regalloc->get_reg_first(n);
 437         int second = regalloc->get_reg_second(n);
 438         if( OptoReg::is_valid(first) ) clr_live_bit(tmp_live,first);
 439         if( OptoReg::is_valid(second) ) clr_live_bit(tmp_live,second);
 440 
 441         MachNode *m = n->is_Mach() ? n->as_Mach() : NULL;
 442 
 443         // Check if m is potentially a CISC alternate instruction (i.e, possibly
 444         // synthesized by RegAlloc from a conventional instruction and a
 445         // spilled input)
 446         bool is_cisc_alternate = false;
 447         if (UseCISCSpill && m) {
 448           is_cisc_alternate = m->is_cisc_alternate();
 449         }
 450 
 451         // GEN use'd bits
 452         for( uint l=1; l<n->req(); l++ ) {
 453           Node *def = n->in(l);
 454           assert(def != 0, "input edge required");
 455           int first = regalloc->get_reg_first(def);
 456           int second = regalloc->get_reg_second(def);
 457           if( OptoReg::is_valid(first) ) set_live_bit(tmp_live,first);
 458           if( OptoReg::is_valid(second) ) set_live_bit(tmp_live,second);
 459           // If we use the stack pointer in a cisc-alternative instruction,
 460           // check for use as a memory operand.  Then reconstruct the RegName
 461           // for this stack location, and set the appropriate bit in the
 462           // live vector 4987749.
 463           if (is_cisc_alternate && def == fp) {
 464             const TypePtr *adr_type = NULL;
 465             intptr_t offset;
 466             const Node* base = m->get_base_and_disp(offset, adr_type);
 467             if (base == NodeSentinel) {
 468               // Machnode has multiple memory inputs. We are unable to reason
 469               // with these, but are presuming (with trepidation) that not any of
 470               // them are oops. This can be fixed by making get_base_and_disp()
 471               // look at a specific input instead of all inputs.
 472               assert(!def->bottom_type()->isa_oop_ptr(), "expecting non-oop mem input");
 473             } else if (base != fp || offset == Type::OffsetBot) {
 474               // Do nothing: the fp operand is either not from a memory use
 475               // (base == NULL) OR the fp is used in a non-memory context
 476               // (base is some other register) OR the offset is not constant,
 477               // so it is not a stack slot.
 478             } else {
 479               assert(offset >= 0, "unexpected negative offset");
 480               offset -= (offset % jintSize);  // count the whole word
 481               int stack_reg = regalloc->offset2reg(offset);
 482               if (OptoReg::is_stack(stack_reg)) {
 483                 set_live_bit(tmp_live, stack_reg);
 484               } else {
 485                 assert(false, "stack_reg not on stack?");
 486               }
 487             }
 488           }
 489         }
 490 
 491         if( n->jvms() ) {       // Record liveness at safepoint
 492 
 493           // This placement of this stanza means inputs to calls are
 494           // considered live at the callsite's OopMap.  Argument oops are
 495           // hence live, but NOT included in the oopmap.  See cutout in
 496           // build_oop_map.  Debug oops are live (and in OopMap).
 497           int *n_live = NEW_ARENA_ARRAY(A, int, max_reg_ints);
 498           for( int l=0; l<max_reg_ints; l++ )
 499             n_live[l] = tmp_live[l];
 500           safehash->Insert(n,n_live);
 501         }
 502 
 503       }
 504 
 505       // Now at block top, see if we have any changes.  If so, propagate
 506       // to prior blocks.
 507       int *old_live = &live[b->_pre_order*max_reg_ints];
 508       int l;
 509       for( l=0; l<max_reg_ints; l++ )
 510         if( tmp_live[l] != old_live[l] )
 511           break;
 512       if( l<max_reg_ints ) {     // Change!
 513         // Copy in new value
 514         for( l=0; l<max_reg_ints; l++ )
 515           old_live[l] = tmp_live[l];
 516         // Push preds onto worklist
 517         for( l=1; l<(int)b->num_preds(); l++ )
 518           worklist->push(cfg->_bbs[b->pred(l)->_idx]);
 519       }
 520     }
 521 
 522     // Scan for any missing safepoints.  Happens to infinite loops
 523     // ala ZKM.jar
 524     uint i;
 525     for( i=1; i<cfg->_num_blocks; i++ ) {
 526       Block *b = cfg->_blocks[i];
 527       uint j;
 528       for( j=1; j<b->_nodes.size(); j++ )
 529         if( b->_nodes[j]->jvms() &&
 530             (*safehash)[b->_nodes[j]] == NULL )
 531            break;
 532       if( j<b->_nodes.size() ) break;
 533     }
 534     if( i == cfg->_num_blocks )
 535       break;                    // Got 'em all
 536 #ifndef PRODUCT
 537     if( PrintOpto && Verbose )
 538       tty->print_cr("retripping live calc");
 539 #endif
 540     // Force the issue (expensively): recheck everybody
 541     for( i=1; i<cfg->_num_blocks; i++ )
 542       worklist->push(cfg->_blocks[i]);
 543   }
 544 
 545 }
 546 
 547 //------------------------------BuildOopMaps-----------------------------------
 548 // Collect GC mask info - where are all the OOPs?
 549 void Compile::BuildOopMaps() {
 550   NOT_PRODUCT( TracePhase t3("bldOopMaps", &_t_buildOopMaps, TimeCompiler); )
 551   // Can't resource-mark because I need to leave all those OopMaps around,
 552   // or else I need to resource-mark some arena other than the default.
 553   // ResourceMark rm;              // Reclaim all OopFlows when done
 554   int max_reg = _regalloc->_max_reg; // Current array extent
 555 
 556   Arena *A = Thread::current()->resource_area();
 557   Block_List worklist;          // Worklist of pending blocks
 558 
 559   int max_reg_ints = round_to(max_reg, BitsPerInt)>>LogBitsPerInt;
 560   Dict *safehash = NULL;        // Used for assert only
 561   // Compute a backwards liveness per register.  Needs a bitarray of
 562   // #blocks x (#registers, rounded up to ints)
 563   safehash = new Dict(cmpkey,hashkey,A);
 564   do_liveness( _regalloc, _cfg, &worklist, max_reg_ints, A, safehash );
 565   OopFlow *free_list = NULL;    // Free, unused
 566 
 567   // Array mapping blocks to completed oopflows
 568   OopFlow **flows = NEW_ARENA_ARRAY(A, OopFlow*, _cfg->_num_blocks);
 569   memset( flows, 0, _cfg->_num_blocks*sizeof(OopFlow*) );
 570 
 571 
 572   // Do the first block 'by hand' to prime the worklist
 573   Block *entry = _cfg->_blocks[1];
 574   OopFlow *rootflow = OopFlow::make(A,max_reg,this);
 575   // Initialize to 'bottom' (not 'top')
 576   memset( rootflow->_callees, OptoReg::Bad, max_reg*sizeof(short) );
 577   memset( rootflow->_defs   ,            0, max_reg*sizeof(Node*) );
 578   flows[entry->_pre_order] = rootflow;
 579 
 580   // Do the first block 'by hand' to prime the worklist
 581   rootflow->_b = entry;
 582   rootflow->compute_reach( _regalloc, max_reg, safehash );
 583   for( uint i=0; i<entry->_num_succs; i++ )
 584     worklist.push(entry->_succs[i]);
 585 
 586   // Now worklist contains blocks which have some, but perhaps not all,
 587   // predecessors visited.
 588   while( worklist.size() ) {
 589     // Scan for a block with all predecessors visited, or any randoms slob
 590     // otherwise.  All-preds-visited order allows me to recycle OopFlow
 591     // structures rapidly and cut down on the memory footprint.
 592     // Note: not all predecessors might be visited yet (must happen for
 593     // irreducible loops).  This is OK, since every live value must have the
 594     // SAME reaching def for the block, so any reaching def is OK.
 595     uint i;
 596 
 597     Block *b = worklist.pop();
 598     // Ignore root block
 599     if( b == _cfg->_broot ) continue;
 600     // Block is already done?  Happens if block has several predecessors,
 601     // he can get on the worklist more than once.
 602     if( flows[b->_pre_order] ) continue;
 603 
 604     // If this block has a visited predecessor AND that predecessor has this
 605     // last block as his only undone child, we can move the OopFlow from the
 606     // pred to this block.  Otherwise we have to grab a new OopFlow.
 607     OopFlow *flow = NULL;       // Flag for finding optimized flow
 608     Block *pred = (Block*)0xdeadbeef;
 609     uint j;
 610     // Scan this block's preds to find a done predecessor
 611     for( j=1; j<b->num_preds(); j++ ) {
 612       Block *p = _cfg->_bbs[b->pred(j)->_idx];
 613       OopFlow *p_flow = flows[p->_pre_order];
 614       if( p_flow ) {            // Predecessor is done
 615         assert( p_flow->_b == p, "cross check" );
 616         pred = p;               // Record some predecessor
 617         // If all successors of p are done except for 'b', then we can carry
 618         // p_flow forward to 'b' without copying, otherwise we have to draw
 619         // from the free_list and clone data.
 620         uint k;
 621         for( k=0; k<p->_num_succs; k++ )
 622           if( !flows[p->_succs[k]->_pre_order] &&
 623               p->_succs[k] != b )
 624             break;
 625 
 626         // Either carry-forward the now-unused OopFlow for b's use
 627         // or draw a new one from the free list
 628         if( k==p->_num_succs ) {
 629           flow = p_flow;
 630           break;                // Found an ideal pred, use him
 631         }
 632       }
 633     }
 634 
 635     if( flow ) {
 636       // We have an OopFlow that's the last-use of a predecessor.
 637       // Carry it forward.
 638     } else {                    // Draw a new OopFlow from the freelist
 639       if( !free_list )
 640         free_list = OopFlow::make(A,max_reg,C);
 641       flow = free_list;
 642       assert( flow->_b == NULL, "oopFlow is not free" );
 643       free_list = flow->_next;
 644       flow->_next = NULL;
 645 
 646       // Copy/clone over the data
 647       flow->clone(flows[pred->_pre_order], max_reg);
 648     }
 649 
 650     // Mark flow for block.  Blocks can only be flowed over once,
 651     // because after the first time they are guarded from entering
 652     // this code again.
 653     assert( flow->_b == pred, "have some prior flow" );
 654     flow->_b = NULL;
 655 
 656     // Now push flow forward
 657     flows[b->_pre_order] = flow;// Mark flow for this block
 658     flow->_b = b;
 659     flow->compute_reach( _regalloc, max_reg, safehash );
 660 
 661     // Now push children onto worklist
 662     for( i=0; i<b->_num_succs; i++ )
 663       worklist.push(b->_succs[i]);
 664 
 665   }
 666 }