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