Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/live.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "opto/callnode.hpp"27#include "opto/chaitin.hpp"28#include "opto/live.hpp"29#include "opto/machnode.hpp"303132// Compute live-in/live-out. We use a totally incremental algorithm. The LIVE33// problem is monotonic. The steady-state solution looks like this: pull a34// block from the worklist. It has a set of delta's - values which are newly35// live-in from the block. Push these to the live-out sets of all predecessor36// blocks. At each predecessor, the new live-out values are ANDed with what is37// already live-out (extra stuff is added to the live-out sets). Then the38// remaining new live-out values are ANDed with what is locally defined.39// Leftover bits become the new live-in for the predecessor block, and the pred40// block is put on the worklist.41// The locally live-in stuff is computed once and added to predecessor42// live-out sets. This separate compilation is done in the outer loop below.43PhaseLive::PhaseLive( const PhaseCFG &cfg, const LRG_List &names, Arena *arena ) : Phase(LIVE), _cfg(cfg), _names(names), _arena(arena), _live(0) {44}4546void PhaseLive::compute(uint maxlrg) {47_maxlrg = maxlrg;48_worklist = new (_arena) Block_List();4950// Init the sparse live arrays. This data is live on exit from here!51// The _live info is the live-out info.52_live = (IndexSet*)_arena->Amalloc(sizeof(IndexSet) * _cfg.number_of_blocks());53uint i;54for (i = 0; i < _cfg.number_of_blocks(); i++) {55_live[i].initialize(_maxlrg);56}5758// Init the sparse arrays for delta-sets.59ResourceMark rm; // Nuke temp storage on exit6061// Does the memory used by _defs and _deltas get reclaimed? Does it matter? TT6263// Array of values defined locally in blocks64_defs = NEW_RESOURCE_ARRAY(IndexSet,_cfg.number_of_blocks());65for (i = 0; i < _cfg.number_of_blocks(); i++) {66_defs[i].initialize(_maxlrg);67}6869// Array of delta-set pointers, indexed by block pre_order-1.70_deltas = NEW_RESOURCE_ARRAY(IndexSet*,_cfg.number_of_blocks());71memset( _deltas, 0, sizeof(IndexSet*)* _cfg.number_of_blocks());7273_free_IndexSet = NULL;7475// Blocks having done pass-176VectorSet first_pass(Thread::current()->resource_area());7778// Outer loop: must compute local live-in sets and push into predecessors.79for (uint j = _cfg.number_of_blocks(); j > 0; j--) {80Block* block = _cfg.get_block(j - 1);8182// Compute the local live-in set. Start with any new live-out bits.83IndexSet* use = getset(block);84IndexSet* def = &_defs[block->_pre_order-1];85DEBUG_ONLY(IndexSet *def_outside = getfreeset();)86uint i;87for (i = block->number_of_nodes(); i > 1; i--) {88Node* n = block->get_node(i-1);89if (n->is_Phi()) {90break;91}9293uint r = _names.at(n->_idx);94assert(!def_outside->member(r), "Use of external LRG overlaps the same LRG defined in this block");95def->insert( r );96use->remove( r );97uint cnt = n->req();98for (uint k = 1; k < cnt; k++) {99Node *nk = n->in(k);100uint nkidx = nk->_idx;101if (_cfg.get_block_for_node(nk) != block) {102uint u = _names.at(nkidx);103use->insert(u);104DEBUG_ONLY(def_outside->insert(u);)105}106}107}108#ifdef ASSERT109def_outside->set_next(_free_IndexSet);110_free_IndexSet = def_outside; // Drop onto free list111#endif112// Remove anything defined by Phis and the block start instruction113for (uint k = i; k > 0; k--) {114uint r = _names.at(block->get_node(k - 1)->_idx);115def->insert(r);116use->remove(r);117}118119// Push these live-in things to predecessors120for (uint l = 1; l < block->num_preds(); l++) {121Block* p = _cfg.get_block_for_node(block->pred(l));122add_liveout(p, use, first_pass);123124// PhiNode uses go in the live-out set of prior blocks.125for (uint k = i; k > 0; k--) {126add_liveout(p, _names.at(block->get_node(k-1)->in(l)->_idx), first_pass);127}128}129freeset(block);130first_pass.set(block->_pre_order);131132// Inner loop: blocks that picked up new live-out values to be propagated133while (_worklist->size()) {134Block* block = _worklist->pop();135IndexSet *delta = getset(block);136assert( delta->count(), "missing delta set" );137138// Add new-live-in to predecessors live-out sets139for (uint l = 1; l < block->num_preds(); l++) {140Block* predecessor = _cfg.get_block_for_node(block->pred(l));141add_liveout(predecessor, delta, first_pass);142}143144freeset(block);145} // End of while-worklist-not-empty146147} // End of for-all-blocks-outer-loop148149// We explicitly clear all of the IndexSets which we are about to release.150// This allows us to recycle their internal memory into IndexSet's free list.151152for (i = 0; i < _cfg.number_of_blocks(); i++) {153_defs[i].clear();154if (_deltas[i]) {155// Is this always true?156_deltas[i]->clear();157}158}159IndexSet *free = _free_IndexSet;160while (free != NULL) {161IndexSet *temp = free;162free = free->next();163temp->clear();164}165166}167168#ifndef PRODUCT169void PhaseLive::stats(uint iters) const {170}171#endif172173// Get an IndexSet for a block. Return existing one, if any. Make a new174// empty one if a prior one does not exist.175IndexSet *PhaseLive::getset( Block *p ) {176IndexSet *delta = _deltas[p->_pre_order-1];177if( !delta ) // Not on worklist?178// Get a free set; flag as being on worklist179delta = _deltas[p->_pre_order-1] = getfreeset();180return delta; // Return set of new live-out items181}182183// Pull from free list, or allocate. Internal allocation on the returned set184// is always from thread local storage.185IndexSet *PhaseLive::getfreeset( ) {186IndexSet *f = _free_IndexSet;187if( !f ) {188f = new IndexSet;189// f->set_arena(Thread::current()->resource_area());190f->initialize(_maxlrg, Thread::current()->resource_area());191} else {192// Pull from free list193_free_IndexSet = f->next();194//f->_cnt = 0; // Reset to empty195// f->set_arena(Thread::current()->resource_area());196f->initialize(_maxlrg, Thread::current()->resource_area());197}198return f;199}200201// Free an IndexSet from a block.202void PhaseLive::freeset( const Block *p ) {203IndexSet *f = _deltas[p->_pre_order-1];204f->set_next(_free_IndexSet);205_free_IndexSet = f; // Drop onto free list206_deltas[p->_pre_order-1] = NULL;207}208209// Add a live-out value to a given blocks live-out set. If it is new, then210// also add it to the delta set and stick the block on the worklist.211void PhaseLive::add_liveout( Block *p, uint r, VectorSet &first_pass ) {212IndexSet *live = &_live[p->_pre_order-1];213if( live->insert(r) ) { // If actually inserted...214// We extended the live-out set. See if the value is generated locally.215// If it is not, then we must extend the live-in set.216if( !_defs[p->_pre_order-1].member( r ) ) {217if( !_deltas[p->_pre_order-1] && // Not on worklist?218first_pass.test(p->_pre_order) )219_worklist->push(p); // Actually go on worklist if already 1st pass220getset(p)->insert(r);221}222}223}224225// Add a vector of live-out values to a given blocks live-out set.226void PhaseLive::add_liveout( Block *p, IndexSet *lo, VectorSet &first_pass ) {227IndexSet *live = &_live[p->_pre_order-1];228IndexSet *defs = &_defs[p->_pre_order-1];229IndexSet *on_worklist = _deltas[p->_pre_order-1];230IndexSet *delta = on_worklist ? on_worklist : getfreeset();231232IndexSetIterator elements(lo);233uint r;234while ((r = elements.next()) != 0) {235if( live->insert(r) && // If actually inserted...236!defs->member( r ) ) // and not defined locally237delta->insert(r); // Then add to live-in set238}239240if( delta->count() ) { // If actually added things241_deltas[p->_pre_order-1] = delta; // Flag as on worklist now242if( !on_worklist && // Not on worklist?243first_pass.test(p->_pre_order) )244_worklist->push(p); // Actually go on worklist if already 1st pass245} else { // Nothing there; just free it246delta->set_next(_free_IndexSet);247_free_IndexSet = delta; // Drop onto free list248}249}250251#ifndef PRODUCT252// Dump the live-out set for a block253void PhaseLive::dump( const Block *b ) const {254tty->print("Block %d: ",b->_pre_order);255tty->print("LiveOut: "); _live[b->_pre_order-1].dump();256uint cnt = b->number_of_nodes();257for( uint i=0; i<cnt; i++ ) {258tty->print("L%d/", _names.at(b->get_node(i)->_idx));259b->get_node(i)->dump();260}261tty->print("\n");262}263264// Verify that base pointers and derived pointers are still sane.265void PhaseChaitin::verify_base_ptrs( ResourceArea *a ) const {266#ifdef ASSERT267Unique_Node_List worklist(a);268for (uint i = 0; i < _cfg.number_of_blocks(); i++) {269Block* block = _cfg.get_block(i);270for (uint j = block->end_idx() + 1; j > 1; j--) {271Node* n = block->get_node(j-1);272if (n->is_Phi()) {273break;274}275// Found a safepoint?276if (n->is_MachSafePoint()) {277MachSafePointNode *sfpt = n->as_MachSafePoint();278JVMState* jvms = sfpt->jvms();279if (jvms != NULL) {280// Now scan for a live derived pointer281if (jvms->oopoff() < sfpt->req()) {282// Check each derived/base pair283for (uint idx = jvms->oopoff(); idx < sfpt->req(); idx++) {284Node *check = sfpt->in(idx);285bool is_derived = ((idx - jvms->oopoff()) & 1) == 0;286// search upwards through spills and spill phis for AddP287worklist.clear();288worklist.push(check);289uint k = 0;290while( k < worklist.size() ) {291check = worklist.at(k);292assert(check,"Bad base or derived pointer");293// See PhaseChaitin::find_base_for_derived() for all cases.294int isc = check->is_Copy();295if( isc ) {296worklist.push(check->in(isc));297} else if( check->is_Phi() ) {298for (uint m = 1; m < check->req(); m++)299worklist.push(check->in(m));300} else if( check->is_Con() ) {301if (is_derived) {302// Derived is NULL+offset303assert(!is_derived || check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad derived pointer");304} else {305assert(check->bottom_type()->is_ptr()->_offset == 0,"Bad base pointer");306// Base either ConP(NULL) or loadConP307if (check->is_Mach()) {308assert(check->as_Mach()->ideal_Opcode() == Op_ConP,"Bad base pointer");309} else {310assert(check->Opcode() == Op_ConP &&311check->bottom_type()->is_ptr()->ptr() == TypePtr::Null,"Bad base pointer");312}313}314} else if( check->bottom_type()->is_ptr()->_offset == 0 ) {315if(check->is_Proj() || check->is_Mach() &&316(check->as_Mach()->ideal_Opcode() == Op_CreateEx ||317check->as_Mach()->ideal_Opcode() == Op_ThreadLocal ||318check->as_Mach()->ideal_Opcode() == Op_CMoveP ||319check->as_Mach()->ideal_Opcode() == Op_CheckCastPP ||320#ifdef _LP64321UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_CastPP ||322UseCompressedOops && check->as_Mach()->ideal_Opcode() == Op_DecodeN ||323UseCompressedClassPointers && check->as_Mach()->ideal_Opcode() == Op_DecodeNKlass ||324#endif325check->as_Mach()->ideal_Opcode() == Op_LoadP ||326check->as_Mach()->ideal_Opcode() == Op_LoadKlass)) {327// Valid nodes328} else {329check->dump();330assert(false,"Bad base or derived pointer");331}332} else {333assert(is_derived,"Bad base pointer");334assert(check->is_Mach() && check->as_Mach()->ideal_Opcode() == Op_AddP,"Bad derived pointer");335}336k++;337assert(k < 100000,"Derived pointer checking in infinite loop");338} // End while339}340} // End of check for derived pointers341} // End of Kcheck for debug info342} // End of if found a safepoint343} // End of forall instructions in block344} // End of forall blocks345#endif346}347348// Verify that graphs and base pointers are still sane.349void PhaseChaitin::verify( ResourceArea *a, bool verify_ifg ) const {350#ifdef ASSERT351if( VerifyOpto || VerifyRegisterAllocator ) {352_cfg.verify();353verify_base_ptrs(a);354if(verify_ifg)355_ifg->verify(this);356}357#endif358}359360#endif361362363