src/share/vm/opto/live.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 6876
710a3c8b516e
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial