duke@435: /* mikael@6198: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "opto/callnode.hpp" stefank@2314: #include "opto/chaitin.hpp" stefank@2314: #include "opto/live.hpp" stefank@2314: #include "opto/machnode.hpp" duke@435: duke@435: duke@435: // Compute live-in/live-out. We use a totally incremental algorithm. The LIVE duke@435: // problem is monotonic. The steady-state solution looks like this: pull a duke@435: // block from the worklist. It has a set of delta's - values which are newly duke@435: // live-in from the block. Push these to the live-out sets of all predecessor duke@435: // blocks. At each predecessor, the new live-out values are ANDed with what is duke@435: // already live-out (extra stuff is added to the live-out sets). Then the duke@435: // remaining new live-out values are ANDed with what is locally defined. duke@435: // Leftover bits become the new live-in for the predecessor block, and the pred duke@435: // block is put on the worklist. duke@435: // The locally live-in stuff is computed once and added to predecessor twisti@1040: // live-out sets. This separate compilation is done in the outer loop below. neliasso@4949: PhaseLive::PhaseLive( const PhaseCFG &cfg, const LRG_List &names, Arena *arena ) : Phase(LIVE), _cfg(cfg), _names(names), _arena(arena), _live(0) { duke@435: } duke@435: duke@435: void PhaseLive::compute(uint maxlrg) { duke@435: _maxlrg = maxlrg; duke@435: _worklist = new (_arena) Block_List(); duke@435: duke@435: // Init the sparse live arrays. This data is live on exit from here! duke@435: // The _live info is the live-out info. adlertz@5539: _live = (IndexSet*)_arena->Amalloc(sizeof(IndexSet) * _cfg.number_of_blocks()); duke@435: uint i; adlertz@5539: for (i = 0; i < _cfg.number_of_blocks(); i++) { duke@435: _live[i].initialize(_maxlrg); duke@435: } duke@435: duke@435: // Init the sparse arrays for delta-sets. duke@435: ResourceMark rm; // Nuke temp storage on exit duke@435: duke@435: // Does the memory used by _defs and _deltas get reclaimed? Does it matter? TT duke@435: duke@435: // Array of values defined locally in blocks adlertz@5539: _defs = NEW_RESOURCE_ARRAY(IndexSet,_cfg.number_of_blocks()); adlertz@5539: for (i = 0; i < _cfg.number_of_blocks(); i++) { duke@435: _defs[i].initialize(_maxlrg); duke@435: } duke@435: duke@435: // Array of delta-set pointers, indexed by block pre_order-1. adlertz@5539: _deltas = NEW_RESOURCE_ARRAY(IndexSet*,_cfg.number_of_blocks()); adlertz@5539: memset( _deltas, 0, sizeof(IndexSet*)* _cfg.number_of_blocks()); duke@435: duke@435: _free_IndexSet = NULL; duke@435: duke@435: // Blocks having done pass-1 duke@435: VectorSet first_pass(Thread::current()->resource_area()); duke@435: duke@435: // Outer loop: must compute local live-in sets and push into predecessors. adlertz@5539: for (uint j = _cfg.number_of_blocks(); j > 0; j--) { adlertz@5539: Block* block = _cfg.get_block(j - 1); duke@435: duke@435: // Compute the local live-in set. Start with any new live-out bits. adlertz@5539: IndexSet* use = getset(block); adlertz@5539: IndexSet* def = &_defs[block->_pre_order-1]; duke@435: DEBUG_ONLY(IndexSet *def_outside = getfreeset();) duke@435: uint i; adlertz@5635: for (i = block->number_of_nodes(); i > 1; i--) { adlertz@5635: Node* n = block->get_node(i-1); adlertz@5539: if (n->is_Phi()) { adlertz@5539: break; adlertz@5539: } duke@435: adlertz@5722: uint r = _names.at(n->_idx); duke@435: assert(!def_outside->member(r), "Use of external LRG overlaps the same LRG defined in this block"); duke@435: def->insert( r ); duke@435: use->remove( r ); duke@435: uint cnt = n->req(); adlertz@5539: for (uint k = 1; k < cnt; k++) { duke@435: Node *nk = n->in(k); duke@435: uint nkidx = nk->_idx; adlertz@5539: if (_cfg.get_block_for_node(nk) != block) { adlertz@5722: uint u = _names.at(nkidx); adlertz@5539: use->insert(u); adlertz@5539: DEBUG_ONLY(def_outside->insert(u);) duke@435: } duke@435: } duke@435: } duke@435: #ifdef ASSERT duke@435: def_outside->set_next(_free_IndexSet); duke@435: _free_IndexSet = def_outside; // Drop onto free list duke@435: #endif duke@435: // Remove anything defined by Phis and the block start instruction adlertz@5539: for (uint k = i; k > 0; k--) { adlertz@5722: uint r = _names.at(block->get_node(k - 1)->_idx); adlertz@5539: def->insert(r); adlertz@5539: use->remove(r); duke@435: } duke@435: duke@435: // Push these live-in things to predecessors adlertz@5539: for (uint l = 1; l < block->num_preds(); l++) { adlertz@5539: Block* p = _cfg.get_block_for_node(block->pred(l)); adlertz@5539: add_liveout(p, use, first_pass); duke@435: duke@435: // PhiNode uses go in the live-out set of prior blocks. adlertz@5539: for (uint k = i; k > 0; k--) { adlertz@5722: add_liveout(p, _names.at(block->get_node(k-1)->in(l)->_idx), first_pass); adlertz@5539: } duke@435: } adlertz@5539: freeset(block); adlertz@5539: first_pass.set(block->_pre_order); duke@435: duke@435: // Inner loop: blocks that picked up new live-out values to be propagated adlertz@5539: while (_worklist->size()) { adlertz@5539: Block* block = _worklist->pop(); adlertz@5539: IndexSet *delta = getset(block); duke@435: assert( delta->count(), "missing delta set" ); duke@435: duke@435: // Add new-live-in to predecessors live-out sets adlertz@5539: for (uint l = 1; l < block->num_preds(); l++) { adlertz@5539: Block* predecessor = _cfg.get_block_for_node(block->pred(l)); adlertz@5539: add_liveout(predecessor, delta, first_pass); adlertz@5509: } duke@435: adlertz@5539: freeset(block); duke@435: } // End of while-worklist-not-empty duke@435: duke@435: } // End of for-all-blocks-outer-loop duke@435: duke@435: // We explicitly clear all of the IndexSets which we are about to release. duke@435: // This allows us to recycle their internal memory into IndexSet's free list. duke@435: adlertz@5539: for (i = 0; i < _cfg.number_of_blocks(); i++) { duke@435: _defs[i].clear(); duke@435: if (_deltas[i]) { duke@435: // Is this always true? duke@435: _deltas[i]->clear(); duke@435: } duke@435: } duke@435: IndexSet *free = _free_IndexSet; duke@435: while (free != NULL) { duke@435: IndexSet *temp = free; duke@435: free = free->next(); duke@435: temp->clear(); duke@435: } duke@435: duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void PhaseLive::stats(uint iters) const { duke@435: } duke@435: #endif duke@435: duke@435: // Get an IndexSet for a block. Return existing one, if any. Make a new duke@435: // empty one if a prior one does not exist. duke@435: IndexSet *PhaseLive::getset( Block *p ) { duke@435: IndexSet *delta = _deltas[p->_pre_order-1]; duke@435: if( !delta ) // Not on worklist? duke@435: // Get a free set; flag as being on worklist duke@435: delta = _deltas[p->_pre_order-1] = getfreeset(); duke@435: return delta; // Return set of new live-out items duke@435: } duke@435: duke@435: // Pull from free list, or allocate. Internal allocation on the returned set duke@435: // is always from thread local storage. duke@435: IndexSet *PhaseLive::getfreeset( ) { duke@435: IndexSet *f = _free_IndexSet; duke@435: if( !f ) { duke@435: f = new IndexSet; duke@435: // f->set_arena(Thread::current()->resource_area()); duke@435: f->initialize(_maxlrg, Thread::current()->resource_area()); duke@435: } else { duke@435: // Pull from free list duke@435: _free_IndexSet = f->next(); duke@435: //f->_cnt = 0; // Reset to empty duke@435: // f->set_arena(Thread::current()->resource_area()); duke@435: f->initialize(_maxlrg, Thread::current()->resource_area()); duke@435: } duke@435: return f; duke@435: } duke@435: duke@435: // Free an IndexSet from a block. duke@435: void PhaseLive::freeset( const Block *p ) { duke@435: IndexSet *f = _deltas[p->_pre_order-1]; duke@435: f->set_next(_free_IndexSet); duke@435: _free_IndexSet = f; // Drop onto free list duke@435: _deltas[p->_pre_order-1] = NULL; duke@435: } duke@435: duke@435: // Add a live-out value to a given blocks live-out set. If it is new, then duke@435: // also add it to the delta set and stick the block on the worklist. duke@435: void PhaseLive::add_liveout( Block *p, uint r, VectorSet &first_pass ) { duke@435: IndexSet *live = &_live[p->_pre_order-1]; duke@435: if( live->insert(r) ) { // If actually inserted... duke@435: // We extended the live-out set. See if the value is generated locally. duke@435: // If it is not, then we must extend the live-in set. duke@435: if( !_defs[p->_pre_order-1].member( r ) ) { duke@435: if( !_deltas[p->_pre_order-1] && // Not on worklist? duke@435: first_pass.test(p->_pre_order) ) duke@435: _worklist->push(p); // Actually go on worklist if already 1st pass duke@435: getset(p)->insert(r); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Add a vector of live-out values to a given blocks live-out set. duke@435: void PhaseLive::add_liveout( Block *p, IndexSet *lo, VectorSet &first_pass ) { duke@435: IndexSet *live = &_live[p->_pre_order-1]; duke@435: IndexSet *defs = &_defs[p->_pre_order-1]; duke@435: IndexSet *on_worklist = _deltas[p->_pre_order-1]; duke@435: IndexSet *delta = on_worklist ? on_worklist : getfreeset(); duke@435: duke@435: IndexSetIterator elements(lo); duke@435: uint r; duke@435: while ((r = elements.next()) != 0) { duke@435: if( live->insert(r) && // If actually inserted... duke@435: !defs->member( r ) ) // and not defined locally duke@435: delta->insert(r); // Then add to live-in set duke@435: } duke@435: duke@435: if( delta->count() ) { // If actually added things duke@435: _deltas[p->_pre_order-1] = delta; // Flag as on worklist now duke@435: if( !on_worklist && // Not on worklist? duke@435: first_pass.test(p->_pre_order) ) duke@435: _worklist->push(p); // Actually go on worklist if already 1st pass duke@435: } else { // Nothing there; just free it duke@435: delta->set_next(_free_IndexSet); duke@435: _free_IndexSet = delta; // Drop onto free list duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: // Dump the live-out set for a block duke@435: void PhaseLive::dump( const Block *b ) const { duke@435: tty->print("Block %d: ",b->_pre_order); duke@435: tty->print("LiveOut: "); _live[b->_pre_order-1].dump(); adlertz@5635: uint cnt = b->number_of_nodes(); duke@435: for( uint i=0; iprint("L%d/", _names.at(b->get_node(i)->_idx)); adlertz@5635: b->get_node(i)->dump(); duke@435: } duke@435: tty->print("\n"); duke@435: } duke@435: duke@435: // Verify that base pointers and derived pointers are still sane. duke@435: void PhaseChaitin::verify_base_ptrs( ResourceArea *a ) const { kvn@985: #ifdef ASSERT kvn@985: Unique_Node_List worklist(a); adlertz@5539: for (uint i = 0; i < _cfg.number_of_blocks(); i++) { adlertz@5539: Block* block = _cfg.get_block(i); adlertz@5539: for (uint j = block->end_idx() + 1; j > 1; j--) { adlertz@5635: Node* n = block->get_node(j-1); adlertz@5539: if (n->is_Phi()) { adlertz@5539: break; adlertz@5539: } duke@435: // Found a safepoint? adlertz@5539: if (n->is_MachSafePoint()) { duke@435: MachSafePointNode *sfpt = n->as_MachSafePoint(); duke@435: JVMState* jvms = sfpt->jvms(); duke@435: if (jvms != NULL) { duke@435: // Now scan for a live derived pointer duke@435: if (jvms->oopoff() < sfpt->req()) { duke@435: // Check each derived/base pair kvn@985: for (uint idx = jvms->oopoff(); idx < sfpt->req(); idx++) { duke@435: Node *check = sfpt->in(idx); kvn@985: bool is_derived = ((idx - jvms->oopoff()) & 1) == 0; duke@435: // search upwards through spills and spill phis for AddP kvn@985: worklist.clear(); kvn@985: worklist.push(check); kvn@985: uint k = 0; kvn@985: while( k < worklist.size() ) { kvn@985: check = worklist.at(k); kvn@985: assert(check,"Bad base or derived pointer"); kvn@985: // See PhaseChaitin::find_base_for_derived() for all cases. kvn@985: int isc = check->is_Copy(); kvn@985: if( isc ) { kvn@985: worklist.push(check->in(isc)); kvn@985: } else if( check->is_Phi() ) { kvn@985: for (uint m = 1; m < check->req(); m++) kvn@985: worklist.push(check->in(m)); kvn@985: } else if( check->is_Con() ) { kvn@985: if (is_derived) { kvn@985: // Derived is NULL+offset kvn@985: assert(!is_derived || check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad derived pointer"); kvn@985: } else { kvn@985: assert(check->bottom_type()->is_ptr()->_offset == 0,"Bad base pointer"); kvn@985: // Base either ConP(NULL) or loadConP kvn@985: if (check->is_Mach()) { kvn@985: assert(check->as_Mach()->ideal_Opcode() == Op_ConP,"Bad base pointer"); kvn@985: } else { kvn@985: assert(check->Opcode() == Op_ConP && kvn@985: check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad base pointer"); kvn@985: } kvn@985: } kvn@985: } else if( check->bottom_type()->is_ptr()->_offset == 0 ) { kvn@985: if(check->is_Proj() || check->is_Mach() && kvn@985: (check->as_Mach()->ideal_Opcode() == Op_CreateEx || kvn@985: check->as_Mach()->ideal_Opcode() == Op_ThreadLocal || kvn@985: check->as_Mach()->ideal_Opcode() == Op_CMoveP || kvn@985: check->as_Mach()->ideal_Opcode() == Op_CheckCastPP || kvn@985: #ifdef _LP64 kvn@985: UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_CastPP || kvn@985: UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_DecodeN || ehelin@5694: UseCompressedClassPointers && check->as_Mach()->ideal_Opcode() == Op_DecodeNKlass || kvn@985: #endif kvn@985: check->as_Mach()->ideal_Opcode() == Op_LoadP || kvn@1001: check->as_Mach()->ideal_Opcode() == Op_LoadKlass)) { kvn@1001: // Valid nodes kvn@1001: } else { kvn@1001: check->dump(); kvn@985: assert(false,"Bad base or derived pointer"); kvn@1001: } kvn@985: } else { kvn@985: assert(is_derived,"Bad base pointer"); kvn@985: assert(check->is_Mach() && check->as_Mach()->ideal_Opcode() == Op_AddP,"Bad derived pointer"); kvn@985: } kvn@985: k++; kvn@985: assert(k < 100000,"Derived pointer checking in infinite loop"); duke@435: } // End while duke@435: } duke@435: } // End of check for derived pointers duke@435: } // End of Kcheck for debug info duke@435: } // End of if found a safepoint duke@435: } // End of forall instructions in block duke@435: } // End of forall blocks kvn@985: #endif duke@435: } kvn@1001: kvn@1001: // Verify that graphs and base pointers are still sane. kvn@1001: void PhaseChaitin::verify( ResourceArea *a, bool verify_ifg ) const { kvn@1001: #ifdef ASSERT kvn@1001: if( VerifyOpto || VerifyRegisterAllocator ) { kvn@1001: _cfg.verify(); kvn@1001: verify_base_ptrs(a); kvn@1001: if(verify_ifg) kvn@1001: _ifg->verify(this); kvn@1001: } duke@435: #endif kvn@1001: } kvn@1001: kvn@1001: #endif