annotate src/share/vm/opto/buildOopMap.cpp @ 729:04fa5affa478

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