Path: blob/master/src/hotspot/share/opto/cfgnode.cpp
64441 views
/*1* Copyright (c) 1997, 2021, 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 "gc/shared/barrierSet.hpp"26#include "gc/shared/c2/barrierSetC2.hpp"27#include "memory/allocation.inline.hpp"28#include "memory/resourceArea.hpp"29#include "oops/objArrayKlass.hpp"30#include "opto/addnode.hpp"31#include "opto/castnode.hpp"32#include "opto/cfgnode.hpp"33#include "opto/connode.hpp"34#include "opto/convertnode.hpp"35#include "opto/loopnode.hpp"36#include "opto/machnode.hpp"37#include "opto/movenode.hpp"38#include "opto/narrowptrnode.hpp"39#include "opto/mulnode.hpp"40#include "opto/phaseX.hpp"41#include "opto/regmask.hpp"42#include "opto/runtime.hpp"43#include "opto/subnode.hpp"44#include "opto/vectornode.hpp"45#include "utilities/vmError.hpp"4647// Portions of code courtesy of Clifford Click4849// Optimization - Graph Style5051//=============================================================================52//------------------------------Value------------------------------------------53// Compute the type of the RegionNode.54const Type* RegionNode::Value(PhaseGVN* phase) const {55for( uint i=1; i<req(); ++i ) { // For all paths in56Node *n = in(i); // Get Control source57if( !n ) continue; // Missing inputs are TOP58if( phase->type(n) == Type::CONTROL )59return Type::CONTROL;60}61return Type::TOP; // All paths dead? Then so are we62}6364//------------------------------Identity---------------------------------------65// Check for Region being Identity.66Node* RegionNode::Identity(PhaseGVN* phase) {67// Cannot have Region be an identity, even if it has only 1 input.68// Phi users cannot have their Region input folded away for them,69// since they need to select the proper data input70return this;71}7273//------------------------------merge_region-----------------------------------74// If a Region flows into a Region, merge into one big happy merge. This is75// hard to do if there is stuff that has to happen76static Node *merge_region(RegionNode *region, PhaseGVN *phase) {77if( region->Opcode() != Op_Region ) // Do not do to LoopNodes78return NULL;79Node *progress = NULL; // Progress flag80PhaseIterGVN *igvn = phase->is_IterGVN();8182uint rreq = region->req();83for( uint i = 1; i < rreq; i++ ) {84Node *r = region->in(i);85if( r && r->Opcode() == Op_Region && // Found a region?86r->in(0) == r && // Not already collapsed?87r != region && // Avoid stupid situations88r->outcnt() == 2 ) { // Self user and 'region' user only?89assert(!r->as_Region()->has_phi(), "no phi users");90if( !progress ) { // No progress91if (region->has_phi()) {92return NULL; // Only flatten if no Phi users93// igvn->hash_delete( phi );94}95igvn->hash_delete( region );96progress = region; // Making progress97}98igvn->hash_delete( r );99100// Append inputs to 'r' onto 'region'101for( uint j = 1; j < r->req(); j++ ) {102// Move an input from 'r' to 'region'103region->add_req(r->in(j));104r->set_req(j, phase->C->top());105// Update phis of 'region'106//for( uint k = 0; k < max; k++ ) {107// Node *phi = region->out(k);108// if( phi->is_Phi() ) {109// phi->add_req(phi->in(i));110// }111//}112113rreq++; // One more input to Region114} // Found a region to merge into Region115igvn->_worklist.push(r);116// Clobber pointer to the now dead 'r'117region->set_req(i, phase->C->top());118}119}120121return progress;122}123124125126//--------------------------------has_phi--------------------------------------127// Helper function: Return any PhiNode that uses this region or NULL128PhiNode* RegionNode::has_phi() const {129for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {130Node* phi = fast_out(i);131if (phi->is_Phi()) { // Check for Phi users132assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");133return phi->as_Phi(); // this one is good enough134}135}136137return NULL;138}139140141//-----------------------------has_unique_phi----------------------------------142// Helper function: Return the only PhiNode that uses this region or NULL143PhiNode* RegionNode::has_unique_phi() const {144// Check that only one use is a Phi145PhiNode* only_phi = NULL;146for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {147Node* phi = fast_out(i);148if (phi->is_Phi()) { // Check for Phi users149assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");150if (only_phi == NULL) {151only_phi = phi->as_Phi();152} else {153return NULL; // multiple phis154}155}156}157158return only_phi;159}160161162//------------------------------check_phi_clipping-----------------------------163// Helper function for RegionNode's identification of FP clipping164// Check inputs to the Phi165static bool check_phi_clipping( PhiNode *phi, ConNode * &min, uint &min_idx, ConNode * &max, uint &max_idx, Node * &val, uint &val_idx ) {166min = NULL;167max = NULL;168val = NULL;169min_idx = 0;170max_idx = 0;171val_idx = 0;172uint phi_max = phi->req();173if( phi_max == 4 ) {174for( uint j = 1; j < phi_max; ++j ) {175Node *n = phi->in(j);176int opcode = n->Opcode();177switch( opcode ) {178case Op_ConI:179{180if( min == NULL ) {181min = n->Opcode() == Op_ConI ? (ConNode*)n : NULL;182min_idx = j;183} else {184max = n->Opcode() == Op_ConI ? (ConNode*)n : NULL;185max_idx = j;186if( min->get_int() > max->get_int() ) {187// Swap min and max188ConNode *temp;189uint temp_idx;190temp = min; min = max; max = temp;191temp_idx = min_idx; min_idx = max_idx; max_idx = temp_idx;192}193}194}195break;196default:197{198val = n;199val_idx = j;200}201break;202}203}204}205return ( min && max && val && (min->get_int() <= 0) && (max->get_int() >=0) );206}207208209//------------------------------check_if_clipping------------------------------210// Helper function for RegionNode's identification of FP clipping211// Check that inputs to Region come from two IfNodes,212//213// If214// False True215// If |216// False True |217// | | |218// RegionNode_inputs219//220static bool check_if_clipping( const RegionNode *region, IfNode * &bot_if, IfNode * &top_if ) {221top_if = NULL;222bot_if = NULL;223224// Check control structure above RegionNode for (if ( if ) )225Node *in1 = region->in(1);226Node *in2 = region->in(2);227Node *in3 = region->in(3);228// Check that all inputs are projections229if( in1->is_Proj() && in2->is_Proj() && in3->is_Proj() ) {230Node *in10 = in1->in(0);231Node *in20 = in2->in(0);232Node *in30 = in3->in(0);233// Check that #1 and #2 are ifTrue and ifFalse from same If234if( in10 != NULL && in10->is_If() &&235in20 != NULL && in20->is_If() &&236in30 != NULL && in30->is_If() && in10 == in20 &&237(in1->Opcode() != in2->Opcode()) ) {238Node *in100 = in10->in(0);239Node *in1000 = (in100 != NULL && in100->is_Proj()) ? in100->in(0) : NULL;240// Check that control for in10 comes from other branch of IF from in3241if( in1000 != NULL && in1000->is_If() &&242in30 == in1000 && (in3->Opcode() != in100->Opcode()) ) {243// Control pattern checks244top_if = (IfNode*)in1000;245bot_if = (IfNode*)in10;246}247}248}249250return (top_if != NULL);251}252253254//------------------------------check_convf2i_clipping-------------------------255// Helper function for RegionNode's identification of FP clipping256// Verify that the value input to the phi comes from "ConvF2I; LShift; RShift"257static bool check_convf2i_clipping( PhiNode *phi, uint idx, ConvF2INode * &convf2i, Node *min, Node *max) {258convf2i = NULL;259260// Check for the RShiftNode261Node *rshift = phi->in(idx);262assert( rshift, "Previous checks ensure phi input is present");263if( rshift->Opcode() != Op_RShiftI ) { return false; }264265// Check for the LShiftNode266Node *lshift = rshift->in(1);267assert( lshift, "Previous checks ensure phi input is present");268if( lshift->Opcode() != Op_LShiftI ) { return false; }269270// Check for the ConvF2INode271Node *conv = lshift->in(1);272if( conv->Opcode() != Op_ConvF2I ) { return false; }273274// Check that shift amounts are only to get sign bits set after F2I275jint max_cutoff = max->get_int();276jint min_cutoff = min->get_int();277jint left_shift = lshift->in(2)->get_int();278jint right_shift = rshift->in(2)->get_int();279jint max_post_shift = nth_bit(BitsPerJavaInteger - left_shift - 1);280if( left_shift != right_shift ||2810 > left_shift || left_shift >= BitsPerJavaInteger ||282max_post_shift < max_cutoff ||283max_post_shift < -min_cutoff ) {284// Shifts are necessary but current transformation eliminates them285return false;286}287288// OK to return the result of ConvF2I without shifting289convf2i = (ConvF2INode*)conv;290return true;291}292293294//------------------------------check_compare_clipping-------------------------295// Helper function for RegionNode's identification of FP clipping296static bool check_compare_clipping( bool less_than, IfNode *iff, ConNode *limit, Node * & input ) {297Node *i1 = iff->in(1);298if ( !i1->is_Bool() ) { return false; }299BoolNode *bool1 = i1->as_Bool();300if( less_than && bool1->_test._test != BoolTest::le ) { return false; }301else if( !less_than && bool1->_test._test != BoolTest::lt ) { return false; }302const Node *cmpF = bool1->in(1);303if( cmpF->Opcode() != Op_CmpF ) { return false; }304// Test that the float value being compared against305// is equivalent to the int value used as a limit306Node *nodef = cmpF->in(2);307if( nodef->Opcode() != Op_ConF ) { return false; }308jfloat conf = nodef->getf();309jint coni = limit->get_int();310if( ((int)conf) != coni ) { return false; }311input = cmpF->in(1);312return true;313}314315//------------------------------is_unreachable_region--------------------------316// Check if the RegionNode is part of an unsafe loop and unreachable from root.317bool RegionNode::is_unreachable_region(const PhaseGVN* phase) {318Node* top = phase->C->top();319assert(req() == 2 || (req() == 3 && in(1) != NULL && in(2) == top), "sanity check arguments");320if (_is_unreachable_region) {321// Return cached result from previous evaluation which should still be valid322assert(is_unreachable_from_root(phase), "walk the graph again and check if its indeed unreachable");323return true;324}325326// First, cut the simple case of fallthrough region when NONE of327// region's phis references itself directly or through a data node.328if (is_possible_unsafe_loop(phase)) {329// If we have a possible unsafe loop, check if the region node is actually unreachable from root.330if (is_unreachable_from_root(phase)) {331_is_unreachable_region = true;332return true;333}334}335return false;336}337338bool RegionNode::is_possible_unsafe_loop(const PhaseGVN* phase) const {339uint max = outcnt();340uint i;341for (i = 0; i < max; i++) {342Node* n = raw_out(i);343if (n != NULL && n->is_Phi()) {344PhiNode* phi = n->as_Phi();345assert(phi->in(0) == this, "sanity check phi");346if (phi->outcnt() == 0) {347continue; // Safe case - no loops348}349if (phi->outcnt() == 1) {350Node* u = phi->raw_out(0);351// Skip if only one use is an other Phi or Call or Uncommon trap.352// It is safe to consider this case as fallthrough.353if (u != NULL && (u->is_Phi() || u->is_CFG())) {354continue;355}356}357// Check when phi references itself directly or through an other node.358if (phi->as_Phi()->simple_data_loop_check(phi->in(1)) >= PhiNode::Unsafe) {359break; // Found possible unsafe data loop.360}361}362}363if (i >= max) {364return false; // An unsafe case was NOT found - don't need graph walk.365}366return true;367}368369bool RegionNode::is_unreachable_from_root(const PhaseGVN* phase) const {370ResourceMark rm;371Node_List nstack;372VectorSet visited;373374// Mark all control nodes reachable from root outputs375Node* n = (Node*)phase->C->root();376nstack.push(n);377visited.set(n->_idx);378while (nstack.size() != 0) {379n = nstack.pop();380uint max = n->outcnt();381for (uint i = 0; i < max; i++) {382Node* m = n->raw_out(i);383if (m != NULL && m->is_CFG()) {384if (m == this) {385return false; // We reached the Region node - it is not dead.386}387if (!visited.test_set(m->_idx))388nstack.push(m);389}390}391}392return true; // The Region node is unreachable - it is dead.393}394395bool RegionNode::try_clean_mem_phi(PhaseGVN *phase) {396// Incremental inlining + PhaseStringOpts sometimes produce:397//398// cmpP with 1 top input399// |400// If401// / \402// IfFalse IfTrue /- Some Node403// \ / / /404// Region / /-MergeMem405// \---Phi406//407//408// It's expected by PhaseStringOpts that the Region goes away and is409// replaced by If's control input but because there's still a Phi,410// the Region stays in the graph. The top input from the cmpP is411// propagated forward and a subgraph that is useful goes away. The412// code below replaces the Phi with the MergeMem so that the Region413// is simplified.414415PhiNode* phi = has_unique_phi();416if (phi && phi->type() == Type::MEMORY && req() == 3 && phi->is_diamond_phi(true)) {417MergeMemNode* m = NULL;418assert(phi->req() == 3, "same as region");419for (uint i = 1; i < 3; ++i) {420Node *mem = phi->in(i);421if (mem && mem->is_MergeMem() && in(i)->outcnt() == 1) {422// Nothing is control-dependent on path #i except the region itself.423m = mem->as_MergeMem();424uint j = 3 - i;425Node* other = phi->in(j);426if (other && other == m->base_memory()) {427// m is a successor memory to other, and is not pinned inside the diamond, so push it out.428// This will allow the diamond to collapse completely.429phase->is_IterGVN()->replace_node(phi, m);430return true;431}432}433}434}435return false;436}437438//------------------------------Ideal------------------------------------------439// Return a node which is more "ideal" than the current node. Must preserve440// the CFG, but we can still strip out dead paths.441Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {442if( !can_reshape && !in(0) ) return NULL; // Already degraded to a Copy443assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");444445// Check for RegionNode with no Phi users and both inputs come from either446// arm of the same IF. If found, then the control-flow split is useless.447bool has_phis = false;448if (can_reshape) { // Need DU info to check for Phi users449has_phis = (has_phi() != NULL); // Cache result450if (has_phis && try_clean_mem_phi(phase)) {451has_phis = false;452}453454if (!has_phis) { // No Phi users? Nothing merging?455for (uint i = 1; i < req()-1; i++) {456Node *if1 = in(i);457if( !if1 ) continue;458Node *iff = if1->in(0);459if( !iff || !iff->is_If() ) continue;460for( uint j=i+1; j<req(); j++ ) {461if( in(j) && in(j)->in(0) == iff &&462if1->Opcode() != in(j)->Opcode() ) {463// Add the IF Projections to the worklist. They (and the IF itself)464// will be eliminated if dead.465phase->is_IterGVN()->add_users_to_worklist(iff);466set_req(i, iff->in(0));// Skip around the useless IF diamond467set_req(j, NULL);468return this; // Record progress469}470}471}472}473}474475// Remove TOP or NULL input paths. If only 1 input path remains, this Region476// degrades to a copy.477bool add_to_worklist = true;478bool modified = false;479int cnt = 0; // Count of values merging480DEBUG_ONLY( int cnt_orig = req(); ) // Save original inputs count481int del_it = 0; // The last input path we delete482// For all inputs...483for( uint i=1; i<req(); ++i ){// For all paths in484Node *n = in(i); // Get the input485if( n != NULL ) {486// Remove useless control copy inputs487if( n->is_Region() && n->as_Region()->is_copy() ) {488set_req(i, n->nonnull_req());489modified = true;490i--;491continue;492}493if( n->is_Proj() ) { // Remove useless rethrows494Node *call = n->in(0);495if (call->is_Call() && call->as_Call()->entry_point() == OptoRuntime::rethrow_stub()) {496set_req(i, call->in(0));497modified = true;498i--;499continue;500}501}502if( phase->type(n) == Type::TOP ) {503set_req_X(i, NULL, phase); // Ignore TOP inputs504modified = true;505i--;506continue;507}508cnt++; // One more value merging509510} else if (can_reshape) { // Else found dead path with DU info511PhaseIterGVN *igvn = phase->is_IterGVN();512del_req(i); // Yank path from self513del_it = i;514uint max = outcnt();515DUIterator j;516bool progress = true;517while(progress) { // Need to establish property over all users518progress = false;519for (j = outs(); has_out(j); j++) {520Node *n = out(j);521if( n->req() != req() && n->is_Phi() ) {522assert( n->in(0) == this, "" );523igvn->hash_delete(n); // Yank from hash before hacking edges524n->set_req_X(i,NULL,igvn);// Correct DU info525n->del_req(i); // Yank path from Phis526if( max != outcnt() ) {527progress = true;528j = refresh_out_pos(j);529max = outcnt();530}531}532}533}534add_to_worklist = false;535phase->is_IterGVN()->add_users_to_worklist(this);536i--;537}538}539540if (can_reshape && cnt == 1) {541// Is it dead loop?542// If it is LoopNopde it had 2 (+1 itself) inputs and543// one of them was cut. The loop is dead if it was EntryContol.544// Loop node may have only one input because entry path545// is removed in PhaseIdealLoop::Dominators().546assert(!this->is_Loop() || cnt_orig <= 3, "Loop node should have 3 or less inputs");547if ((this->is_Loop() && (del_it == LoopNode::EntryControl ||548(del_it == 0 && is_unreachable_region(phase)))) ||549(!this->is_Loop() && has_phis && is_unreachable_region(phase))) {550// This region and therefore all nodes on the input control path(s) are unreachable551// from root. To avoid incomplete removal of unreachable subgraphs, walk up the CFG552// and aggressively replace all nodes by top.553PhaseIterGVN* igvn = phase->is_IterGVN();554Node* top = phase->C->top();555ResourceMark rm;556Node_List nstack;557VectorSet visited;558nstack.push(this);559visited.set(_idx);560while (nstack.size() != 0) {561Node* n = nstack.pop();562for (uint i = 0; i < n->req(); ++i) {563Node* m = n->in(i);564assert(m != (Node*)phase->C->root(), "Should be unreachable from root");565if (m != NULL && m->is_CFG() && !visited.test_set(m->_idx)) {566nstack.push(m);567}568}569if (n->is_Region()) {570// Eagerly replace phis with top to avoid regionless phis.571n->set_req(0, NULL);572bool progress = true;573uint max = n->outcnt();574DUIterator j;575while (progress) {576progress = false;577for (j = n->outs(); n->has_out(j); j++) {578Node* u = n->out(j);579if (u->is_Phi()) {580igvn->replace_node(u, top);581if (max != n->outcnt()) {582progress = true;583j = n->refresh_out_pos(j);584max = n->outcnt();585}586}587}588}589}590igvn->replace_node(n, top);591}592return NULL;593}594}595596if( cnt <= 1 ) { // Only 1 path in?597set_req(0, NULL); // Null control input for region copy598if( cnt == 0 && !can_reshape) { // Parse phase - leave the node as it is.599// No inputs or all inputs are NULL.600return NULL;601} else if (can_reshape) { // Optimization phase - remove the node602PhaseIterGVN *igvn = phase->is_IterGVN();603// Strip mined (inner) loop is going away, remove outer loop.604if (is_CountedLoop() &&605as_Loop()->is_strip_mined()) {606Node* outer_sfpt = as_CountedLoop()->outer_safepoint();607Node* outer_out = as_CountedLoop()->outer_loop_exit();608if (outer_sfpt != NULL && outer_out != NULL) {609Node* in = outer_sfpt->in(0);610igvn->replace_node(outer_out, in);611LoopNode* outer = as_CountedLoop()->outer_loop();612igvn->replace_input_of(outer, LoopNode::LoopBackControl, igvn->C->top());613}614}615if (is_CountedLoop()) {616Node* opaq = as_CountedLoop()->is_canonical_loop_entry();617if (opaq != NULL) {618// This is not a loop anymore. No need to keep the Opaque1 node on the test that guards the loop as it won't be619// subject to further loop opts.620assert(opaq->Opcode() == Op_Opaque1, "");621igvn->replace_node(opaq, opaq->in(1));622}623}624Node *parent_ctrl;625if( cnt == 0 ) {626assert( req() == 1, "no inputs expected" );627// During IGVN phase such region will be subsumed by TOP node628// so region's phis will have TOP as control node.629// Kill phis here to avoid it.630// Also set other user's input to top.631parent_ctrl = phase->C->top();632} else {633// The fallthrough case since we already checked dead loops above.634parent_ctrl = in(1);635assert(parent_ctrl != NULL, "Region is a copy of some non-null control");636assert(parent_ctrl != this, "Close dead loop");637}638if (add_to_worklist) {639igvn->add_users_to_worklist(this); // Check for further allowed opts640}641for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {642Node* n = last_out(i);643igvn->hash_delete(n); // Remove from worklist before modifying edges644if (n->outcnt() == 0) {645int uses_found = n->replace_edge(this, phase->C->top(), igvn);646if (uses_found > 1) { // (--i) done at the end of the loop.647i -= (uses_found - 1);648}649continue;650}651if( n->is_Phi() ) { // Collapse all Phis652// Eagerly replace phis to avoid regionless phis.653Node* in;654if( cnt == 0 ) {655assert( n->req() == 1, "No data inputs expected" );656in = parent_ctrl; // replaced by top657} else {658assert( n->req() == 2 && n->in(1) != NULL, "Only one data input expected" );659in = n->in(1); // replaced by unique input660if( n->as_Phi()->is_unsafe_data_reference(in) )661in = phase->C->top(); // replaced by top662}663igvn->replace_node(n, in);664}665else if( n->is_Region() ) { // Update all incoming edges666assert(n != this, "Must be removed from DefUse edges");667int uses_found = n->replace_edge(this, parent_ctrl, igvn);668if (uses_found > 1) { // (--i) done at the end of the loop.669i -= (uses_found - 1);670}671}672else {673assert(n->in(0) == this, "Expect RegionNode to be control parent");674n->set_req(0, parent_ctrl);675}676#ifdef ASSERT677for( uint k=0; k < n->req(); k++ ) {678assert(n->in(k) != this, "All uses of RegionNode should be gone");679}680#endif681}682// Remove the RegionNode itself from DefUse info683igvn->remove_dead_node(this);684return NULL;685}686return this; // Record progress687}688689690// If a Region flows into a Region, merge into one big happy merge.691if (can_reshape) {692Node *m = merge_region(this, phase);693if (m != NULL) return m;694}695696// Check if this region is the root of a clipping idiom on floats697if( ConvertFloat2IntClipping && can_reshape && req() == 4 ) {698// Check that only one use is a Phi and that it simplifies to two constants +699PhiNode* phi = has_unique_phi();700if (phi != NULL) { // One Phi user701// Check inputs to the Phi702ConNode *min;703ConNode *max;704Node *val;705uint min_idx;706uint max_idx;707uint val_idx;708if( check_phi_clipping( phi, min, min_idx, max, max_idx, val, val_idx ) ) {709IfNode *top_if;710IfNode *bot_if;711if( check_if_clipping( this, bot_if, top_if ) ) {712// Control pattern checks, now verify compares713Node *top_in = NULL; // value being compared against714Node *bot_in = NULL;715if( check_compare_clipping( true, bot_if, min, bot_in ) &&716check_compare_clipping( false, top_if, max, top_in ) ) {717if( bot_in == top_in ) {718PhaseIterGVN *gvn = phase->is_IterGVN();719assert( gvn != NULL, "Only had DefUse info in IterGVN");720// Only remaining check is that bot_in == top_in == (Phi's val + mods)721722// Check for the ConvF2INode723ConvF2INode *convf2i;724if( check_convf2i_clipping( phi, val_idx, convf2i, min, max ) &&725convf2i->in(1) == bot_in ) {726// Matched pattern, including LShiftI; RShiftI, replace with integer compares727// max test728Node *cmp = gvn->register_new_node_with_optimizer(new CmpINode( convf2i, min ));729Node *boo = gvn->register_new_node_with_optimizer(new BoolNode( cmp, BoolTest::lt ));730IfNode *iff = (IfNode*)gvn->register_new_node_with_optimizer(new IfNode( top_if->in(0), boo, PROB_UNLIKELY_MAG(5), top_if->_fcnt ));731Node *if_min= gvn->register_new_node_with_optimizer(new IfTrueNode (iff));732Node *ifF = gvn->register_new_node_with_optimizer(new IfFalseNode(iff));733// min test734cmp = gvn->register_new_node_with_optimizer(new CmpINode( convf2i, max ));735boo = gvn->register_new_node_with_optimizer(new BoolNode( cmp, BoolTest::gt ));736iff = (IfNode*)gvn->register_new_node_with_optimizer(new IfNode( ifF, boo, PROB_UNLIKELY_MAG(5), bot_if->_fcnt ));737Node *if_max= gvn->register_new_node_with_optimizer(new IfTrueNode (iff));738ifF = gvn->register_new_node_with_optimizer(new IfFalseNode(iff));739// update input edges to region node740set_req_X( min_idx, if_min, gvn );741set_req_X( max_idx, if_max, gvn );742set_req_X( val_idx, ifF, gvn );743// remove unnecessary 'LShiftI; RShiftI' idiom744gvn->hash_delete(phi);745phi->set_req_X( val_idx, convf2i, gvn );746gvn->hash_find_insert(phi);747// Return transformed region node748return this;749}750}751}752}753}754}755}756757if (can_reshape) {758modified |= optimize_trichotomy(phase->is_IterGVN());759}760761return modified ? this : NULL;762}763764//------------------------------optimize_trichotomy--------------------------765// Optimize nested comparisons of the following kind:766//767// int compare(int a, int b) {768// return (a < b) ? -1 : (a == b) ? 0 : 1;769// }770//771// Shape 1:772// if (compare(a, b) == 1) { ... } -> if (a > b) { ... }773//774// Shape 2:775// if (compare(a, b) == 0) { ... } -> if (a == b) { ... }776//777// Above code leads to the following IR shapes where both Ifs compare the778// same value and two out of three region inputs idx1 and idx2 map to779// the same value and control flow.780//781// (1) If (2) If782// / \ / \783// Proj Proj Proj Proj784// | \ | \785// | If | If If786// | / \ | / \ / \787// | Proj Proj | Proj Proj ==> Proj Proj788// | / / \ | / | /789// Region / \ | / | /790// \ / \ | / | /791// Region Region Region792//793// The method returns true if 'this' is modified and false otherwise.794bool RegionNode::optimize_trichotomy(PhaseIterGVN* igvn) {795int idx1 = 1, idx2 = 2;796Node* region = NULL;797if (req() == 3 && in(1) != NULL && in(2) != NULL) {798// Shape 1: Check if one of the inputs is a region that merges two control799// inputs and has no other users (especially no Phi users).800region = in(1)->isa_Region() ? in(1) : in(2)->isa_Region();801if (region == NULL || region->outcnt() != 2 || region->req() != 3) {802return false; // No suitable region input found803}804} else if (req() == 4) {805// Shape 2: Check if two control inputs map to the same value of the unique phi806// user and treat these as if they would come from another region (shape (1)).807PhiNode* phi = has_unique_phi();808if (phi == NULL) {809return false; // No unique phi user810}811if (phi->in(idx1) != phi->in(idx2)) {812idx2 = 3;813if (phi->in(idx1) != phi->in(idx2)) {814idx1 = 2;815if (phi->in(idx1) != phi->in(idx2)) {816return false; // No equal phi inputs found817}818}819}820assert(phi->in(idx1) == phi->in(idx2), "must be"); // Region is merging same value821region = this;822}823if (region == NULL || region->in(idx1) == NULL || region->in(idx2) == NULL) {824return false; // Region does not merge two control inputs825}826// At this point we know that region->in(idx1) and region->(idx2) map to the same827// value and control flow. Now search for ifs that feed into these region inputs.828ProjNode* proj1 = region->in(idx1)->isa_Proj();829ProjNode* proj2 = region->in(idx2)->isa_Proj();830if (proj1 == NULL || proj1->outcnt() != 1 ||831proj2 == NULL || proj2->outcnt() != 1) {832return false; // No projection inputs with region as unique user found833}834assert(proj1 != proj2, "should be different projections");835IfNode* iff1 = proj1->in(0)->isa_If();836IfNode* iff2 = proj2->in(0)->isa_If();837if (iff1 == NULL || iff1->outcnt() != 2 ||838iff2 == NULL || iff2->outcnt() != 2) {839return false; // No ifs found840}841if (iff1 == iff2) {842igvn->add_users_to_worklist(iff1); // Make sure dead if is eliminated843igvn->replace_input_of(region, idx1, iff1->in(0));844igvn->replace_input_of(region, idx2, igvn->C->top());845return (region == this); // Remove useless if (both projections map to the same control/value)846}847BoolNode* bol1 = iff1->in(1)->isa_Bool();848BoolNode* bol2 = iff2->in(1)->isa_Bool();849if (bol1 == NULL || bol2 == NULL) {850return false; // No bool inputs found851}852Node* cmp1 = bol1->in(1);853Node* cmp2 = bol2->in(1);854bool commute = false;855if (!cmp1->is_Cmp() || !cmp2->is_Cmp()) {856return false; // No comparison857} else if (cmp1->Opcode() == Op_CmpF || cmp1->Opcode() == Op_CmpD ||858cmp2->Opcode() == Op_CmpF || cmp2->Opcode() == Op_CmpD ||859cmp1->Opcode() == Op_CmpP || cmp1->Opcode() == Op_CmpN ||860cmp2->Opcode() == Op_CmpP || cmp2->Opcode() == Op_CmpN ||861cmp1->is_SubTypeCheck() || cmp2->is_SubTypeCheck()) {862// Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.863// SubTypeCheck is not commutative864return false;865} else if (cmp1 != cmp2) {866if (cmp1->in(1) == cmp2->in(2) &&867cmp1->in(2) == cmp2->in(1)) {868commute = true; // Same but swapped inputs, commute the test869} else {870return false; // Ifs are not comparing the same values871}872}873proj1 = proj1->other_if_proj();874proj2 = proj2->other_if_proj();875if (!((proj1->unique_ctrl_out() == iff2 &&876proj2->unique_ctrl_out() == this) ||877(proj2->unique_ctrl_out() == iff1 &&878proj1->unique_ctrl_out() == this))) {879return false; // Ifs are not connected through other projs880}881// Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged882// through 'region' and map to the same value. Merge the boolean tests and replace883// the ifs by a single comparison.884BoolTest test1 = (proj1->_con == 1) ? bol1->_test : bol1->_test.negate();885BoolTest test2 = (proj2->_con == 1) ? bol2->_test : bol2->_test.negate();886test1 = commute ? test1.commute() : test1;887// After possibly commuting test1, if we can merge test1 & test2, then proj2/iff2/bol2 are the nodes to refine.888BoolTest::mask res = test1.merge(test2);889if (res == BoolTest::illegal) {890return false; // Unable to merge tests891}892// Adjust iff1 to always pass (only iff2 will remain)893igvn->replace_input_of(iff1, 1, igvn->intcon(proj1->_con));894if (res == BoolTest::never) {895// Merged test is always false, adjust iff2 to always fail896igvn->replace_input_of(iff2, 1, igvn->intcon(1 - proj2->_con));897} else {898// Replace bool input of iff2 with merged test899BoolNode* new_bol = new BoolNode(bol2->in(1), res);900igvn->replace_input_of(iff2, 1, igvn->transform((proj2->_con == 1) ? new_bol : new_bol->negate(igvn)));901if (new_bol->outcnt() == 0) {902igvn->remove_dead_node(new_bol);903}904}905return false;906}907908const RegMask &RegionNode::out_RegMask() const {909return RegMask::Empty;910}911912// Find the one non-null required input. RegionNode only913Node *Node::nonnull_req() const {914assert( is_Region(), "" );915for( uint i = 1; i < _cnt; i++ )916if( in(i) )917return in(i);918ShouldNotReachHere();919return NULL;920}921922923//=============================================================================924// note that these functions assume that the _adr_type field is flattened925uint PhiNode::hash() const {926const Type* at = _adr_type;927return TypeNode::hash() + (at ? at->hash() : 0);928}929bool PhiNode::cmp( const Node &n ) const {930return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;931}932static inline933const TypePtr* flatten_phi_adr_type(const TypePtr* at) {934if (at == NULL || at == TypePtr::BOTTOM) return at;935return Compile::current()->alias_type(at)->adr_type();936}937938//----------------------------make---------------------------------------------939// create a new phi with edges matching r and set (initially) to x940PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {941uint preds = r->req(); // Number of predecessor paths942assert(t != Type::MEMORY || at == flatten_phi_adr_type(at), "flatten at");943PhiNode* p = new PhiNode(r, t, at);944for (uint j = 1; j < preds; j++) {945// Fill in all inputs, except those which the region does not yet have946if (r->in(j) != NULL)947p->init_req(j, x);948}949return p;950}951PhiNode* PhiNode::make(Node* r, Node* x) {952const Type* t = x->bottom_type();953const TypePtr* at = NULL;954if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());955return make(r, x, t, at);956}957PhiNode* PhiNode::make_blank(Node* r, Node* x) {958const Type* t = x->bottom_type();959const TypePtr* at = NULL;960if (t == Type::MEMORY) at = flatten_phi_adr_type(x->adr_type());961return new PhiNode(r, t, at);962}963964965//------------------------slice_memory-----------------------------------------966// create a new phi with narrowed memory type967PhiNode* PhiNode::slice_memory(const TypePtr* adr_type) const {968PhiNode* mem = (PhiNode*) clone();969*(const TypePtr**)&mem->_adr_type = adr_type;970// convert self-loops, or else we get a bad graph971for (uint i = 1; i < req(); i++) {972if ((const Node*)in(i) == this) mem->set_req(i, mem);973}974mem->verify_adr_type();975return mem;976}977978//------------------------split_out_instance-----------------------------------979// Split out an instance type from a bottom phi.980PhiNode* PhiNode::split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const {981const TypeOopPtr *t_oop = at->isa_oopptr();982assert(t_oop != NULL && t_oop->is_known_instance(), "expecting instance oopptr");983const TypePtr *t = adr_type();984assert(type() == Type::MEMORY &&985(t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||986t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&987t->is_oopptr()->cast_to_exactness(true)988->is_oopptr()->cast_to_ptr_type(t_oop->ptr())989->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop),990"bottom or raw memory required");991992// Check if an appropriate node already exists.993Node *region = in(0);994for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {995Node* use = region->fast_out(k);996if( use->is_Phi()) {997PhiNode *phi2 = use->as_Phi();998if (phi2->type() == Type::MEMORY && phi2->adr_type() == at) {999return phi2;1000}1001}1002}1003Compile *C = igvn->C;1004Arena *a = Thread::current()->resource_area();1005Node_Array node_map = new Node_Array(a);1006Node_Stack stack(a, C->live_nodes() >> 4);1007PhiNode *nphi = slice_memory(at);1008igvn->register_new_node_with_optimizer( nphi );1009node_map.map(_idx, nphi);1010stack.push((Node *)this, 1);1011while(!stack.is_empty()) {1012PhiNode *ophi = stack.node()->as_Phi();1013uint i = stack.index();1014assert(i >= 1, "not control edge");1015stack.pop();1016nphi = node_map[ophi->_idx]->as_Phi();1017for (; i < ophi->req(); i++) {1018Node *in = ophi->in(i);1019if (in == NULL || igvn->type(in) == Type::TOP)1020continue;1021Node *opt = MemNode::optimize_simple_memory_chain(in, t_oop, NULL, igvn);1022PhiNode *optphi = opt->is_Phi() ? opt->as_Phi() : NULL;1023if (optphi != NULL && optphi->adr_type() == TypePtr::BOTTOM) {1024opt = node_map[optphi->_idx];1025if (opt == NULL) {1026stack.push(ophi, i);1027nphi = optphi->slice_memory(at);1028igvn->register_new_node_with_optimizer( nphi );1029node_map.map(optphi->_idx, nphi);1030ophi = optphi;1031i = 0; // will get incremented at top of loop1032continue;1033}1034}1035nphi->set_req(i, opt);1036}1037}1038return nphi;1039}10401041//------------------------verify_adr_type--------------------------------------1042#ifdef ASSERT1043void PhiNode::verify_adr_type(VectorSet& visited, const TypePtr* at) const {1044if (visited.test_set(_idx)) return; //already visited10451046// recheck constructor invariants:1047verify_adr_type(false);10481049// recheck local phi/phi consistency:1050assert(_adr_type == at || _adr_type == TypePtr::BOTTOM,1051"adr_type must be consistent across phi nest");10521053// walk around1054for (uint i = 1; i < req(); i++) {1055Node* n = in(i);1056if (n == NULL) continue;1057const Node* np = in(i);1058if (np->is_Phi()) {1059np->as_Phi()->verify_adr_type(visited, at);1060} else if (n->bottom_type() == Type::TOP1061|| (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {1062// ignore top inputs1063} else {1064const TypePtr* nat = flatten_phi_adr_type(n->adr_type());1065// recheck phi/non-phi consistency at leaves:1066assert((nat != NULL) == (at != NULL), "");1067assert(nat == at || nat == TypePtr::BOTTOM,1068"adr_type must be consistent at leaves of phi nest");1069}1070}1071}10721073// Verify a whole nest of phis rooted at this one.1074void PhiNode::verify_adr_type(bool recursive) const {1075if (VMError::is_error_reported()) return; // muzzle asserts when debugging an error1076if (Node::in_dump()) return; // muzzle asserts when printing10771078assert((_type == Type::MEMORY) == (_adr_type != NULL), "adr_type for memory phis only");10791080if (!VerifyAliases) return; // verify thoroughly only if requested10811082assert(_adr_type == flatten_phi_adr_type(_adr_type),1083"Phi::adr_type must be pre-normalized");10841085if (recursive) {1086VectorSet visited;1087verify_adr_type(visited, _adr_type);1088}1089}1090#endif109110921093//------------------------------Value------------------------------------------1094// Compute the type of the PhiNode1095const Type* PhiNode::Value(PhaseGVN* phase) const {1096Node *r = in(0); // RegionNode1097if( !r ) // Copy or dead1098return in(1) ? phase->type(in(1)) : Type::TOP;10991100// Note: During parsing, phis are often transformed before their regions.1101// This means we have to use type_or_null to defend against untyped regions.1102if( phase->type_or_null(r) == Type::TOP ) // Dead code?1103return Type::TOP;11041105// Check for trip-counted loop. If so, be smarter.1106BaseCountedLoopNode* l = r->is_BaseCountedLoop() ? r->as_BaseCountedLoop() : NULL;1107if (l && ((const Node*)l->phi() == this)) { // Trip counted loop!1108// protect against init_trip() or limit() returning NULL1109if (l->can_be_counted_loop(phase)) {1110const Node* init = l->init_trip();1111const Node* limit = l->limit();1112const Node* stride = l->stride();1113if (init != NULL && limit != NULL && stride != NULL) {1114const TypeInteger* lo = phase->type(init)->isa_integer(l->bt());1115const TypeInteger* hi = phase->type(limit)->isa_integer(l->bt());1116const TypeInteger* stride_t = phase->type(stride)->isa_integer(l->bt());1117if (lo != NULL && hi != NULL && stride_t != NULL) { // Dying loops might have TOP here1118assert(stride_t->hi_as_long() >= stride_t->lo_as_long(), "bad stride type");1119BoolTest::mask bt = l->loopexit()->test_trip();1120// If the loop exit condition is "not equal", the condition1121// would not trigger if init > limit (if stride > 0) or if1122// init < limit if (stride > 0) so we can't deduce bounds1123// for the iv from the exit condition.1124if (bt != BoolTest::ne) {1125if (stride_t->hi_as_long() < 0) { // Down-counter loop1126swap(lo, hi);1127return TypeInteger::make(MIN2(lo->lo_as_long(), hi->lo_as_long()), hi->hi_as_long(), 3, l->bt())->filter_speculative(_type);1128} else if (stride_t->lo_as_long() >= 0) {1129return TypeInteger::make(lo->lo_as_long(), MAX2(lo->hi_as_long(), hi->hi_as_long()), 3, l->bt())->filter_speculative(_type);1130}1131}1132}1133}1134} else if (l->in(LoopNode::LoopBackControl) != NULL &&1135in(LoopNode::EntryControl) != NULL &&1136phase->type(l->in(LoopNode::LoopBackControl)) == Type::TOP) {1137// During CCP, if we saturate the type of a counted loop's Phi1138// before the special code for counted loop above has a chance1139// to run (that is as long as the type of the backedge's control1140// is top), we might end up with non monotonic types1141return phase->type(in(LoopNode::EntryControl))->filter_speculative(_type);1142}1143}11441145// Until we have harmony between classes and interfaces in the type1146// lattice, we must tread carefully around phis which implicitly1147// convert the one to the other.1148const TypePtr* ttp = _type->make_ptr();1149const TypeInstPtr* ttip = (ttp != NULL) ? ttp->isa_instptr() : NULL;1150const TypeKlassPtr* ttkp = (ttp != NULL) ? ttp->isa_klassptr() : NULL;1151bool is_intf = false;1152if (ttip != NULL) {1153ciKlass* k = ttip->klass();1154if (k->is_loaded() && k->is_interface())1155is_intf = true;1156}1157if (ttkp != NULL) {1158ciKlass* k = ttkp->klass();1159if (k->is_loaded() && k->is_interface())1160is_intf = true;1161}11621163// Default case: merge all inputs1164const Type *t = Type::TOP; // Merged type starting value1165for (uint i = 1; i < req(); ++i) {// For all paths in1166// Reachable control path?1167if (r->in(i) && phase->type(r->in(i)) == Type::CONTROL) {1168const Type* ti = phase->type(in(i));1169// We assume that each input of an interface-valued Phi is a true1170// subtype of that interface. This might not be true of the meet1171// of all the input types. The lattice is not distributive in1172// such cases. Ward off asserts in type.cpp by refusing to do1173// meets between interfaces and proper classes.1174const TypePtr* tip = ti->make_ptr();1175const TypeInstPtr* tiip = (tip != NULL) ? tip->isa_instptr() : NULL;1176if (tiip) {1177bool ti_is_intf = false;1178ciKlass* k = tiip->klass();1179if (k->is_loaded() && k->is_interface())1180ti_is_intf = true;1181if (is_intf != ti_is_intf)1182{ t = _type; break; }1183}1184t = t->meet_speculative(ti);1185}1186}11871188// The worst-case type (from ciTypeFlow) should be consistent with "t".1189// That is, we expect that "t->higher_equal(_type)" holds true.1190// There are various exceptions:1191// - Inputs which are phis might in fact be widened unnecessarily.1192// For example, an input might be a widened int while the phi is a short.1193// - Inputs might be BotPtrs but this phi is dependent on a null check,1194// and postCCP has removed the cast which encodes the result of the check.1195// - The type of this phi is an interface, and the inputs are classes.1196// - Value calls on inputs might produce fuzzy results.1197// (Occurrences of this case suggest improvements to Value methods.)1198//1199// It is not possible to see Type::BOTTOM values as phi inputs,1200// because the ciTypeFlow pre-pass produces verifier-quality types.1201const Type* ft = t->filter_speculative(_type); // Worst case type12021203#ifdef ASSERT1204// The following logic has been moved into TypeOopPtr::filter.1205const Type* jt = t->join_speculative(_type);1206if (jt->empty()) { // Emptied out???12071208// Check for evil case of 't' being a class and '_type' expecting an1209// interface. This can happen because the bytecodes do not contain1210// enough type info to distinguish a Java-level interface variable1211// from a Java-level object variable. If we meet 2 classes which1212// both implement interface I, but their meet is at 'j/l/O' which1213// doesn't implement I, we have no way to tell if the result should1214// be 'I' or 'j/l/O'. Thus we'll pick 'j/l/O'. If this then flows1215// into a Phi which "knows" it's an Interface type we'll have to1216// uplift the type.1217if (!t->empty() && ttip && ttip->is_loaded() && ttip->klass()->is_interface()) {1218assert(ft == _type, ""); // Uplift to interface1219} else if (!t->empty() && ttkp && ttkp->is_loaded() && ttkp->klass()->is_interface()) {1220assert(ft == _type, ""); // Uplift to interface1221} else {1222// We also have to handle 'evil cases' of interface- vs. class-arrays1223Type::get_arrays_base_elements(jt, _type, NULL, &ttip);1224if (!t->empty() && ttip != NULL && ttip->is_loaded() && ttip->klass()->is_interface()) {1225assert(ft == _type, ""); // Uplift to array of interface1226} else {1227// Otherwise it's something stupid like non-overlapping int ranges1228// found on dying counted loops.1229assert(ft == Type::TOP, ""); // Canonical empty value1230}1231}1232}12331234else {12351236// If we have an interface-typed Phi and we narrow to a class type, the join1237// should report back the class. However, if we have a J/L/Object1238// class-typed Phi and an interface flows in, it's possible that the meet &1239// join report an interface back out. This isn't possible but happens1240// because the type system doesn't interact well with interfaces.1241const TypePtr *jtp = jt->make_ptr();1242const TypeInstPtr *jtip = (jtp != NULL) ? jtp->isa_instptr() : NULL;1243const TypeKlassPtr *jtkp = (jtp != NULL) ? jtp->isa_klassptr() : NULL;1244if( jtip && ttip ) {1245if( jtip->is_loaded() && jtip->klass()->is_interface() &&1246ttip->is_loaded() && !ttip->klass()->is_interface() ) {1247assert(ft == ttip->cast_to_ptr_type(jtip->ptr()) ||1248ft->isa_narrowoop() && ft->make_ptr() == ttip->cast_to_ptr_type(jtip->ptr()), "");1249jt = ft;1250}1251}1252if( jtkp && ttkp ) {1253if( jtkp->is_loaded() && jtkp->klass()->is_interface() &&1254!jtkp->klass_is_exact() && // Keep exact interface klass (6894807)1255ttkp->is_loaded() && !ttkp->klass()->is_interface() ) {1256assert(ft == ttkp->cast_to_ptr_type(jtkp->ptr()) ||1257ft->isa_narrowklass() && ft->make_ptr() == ttkp->cast_to_ptr_type(jtkp->ptr()), "");1258jt = ft;1259}1260}1261if (jt != ft && jt->base() == ft->base()) {1262if (jt->isa_int() &&1263jt->is_int()->_lo == ft->is_int()->_lo &&1264jt->is_int()->_hi == ft->is_int()->_hi)1265jt = ft;1266if (jt->isa_long() &&1267jt->is_long()->_lo == ft->is_long()->_lo &&1268jt->is_long()->_hi == ft->is_long()->_hi)1269jt = ft;1270}1271if (jt != ft) {1272tty->print("merge type: "); t->dump(); tty->cr();1273tty->print("kill type: "); _type->dump(); tty->cr();1274tty->print("join type: "); jt->dump(); tty->cr();1275tty->print("filter type: "); ft->dump(); tty->cr();1276}1277assert(jt == ft, "");1278}1279#endif //ASSERT12801281// Deal with conversion problems found in data loops.1282ft = phase->saturate(ft, phase->type_or_null(this), _type);12831284return ft;1285}128612871288//------------------------------is_diamond_phi---------------------------------1289// Does this Phi represent a simple well-shaped diamond merge? Return the1290// index of the true path or 0 otherwise.1291// If check_control_only is true, do not inspect the If node at the1292// top, and return -1 (not an edge number) on success.1293int PhiNode::is_diamond_phi(bool check_control_only) const {1294// Check for a 2-path merge1295Node *region = in(0);1296if( !region ) return 0;1297if( region->req() != 3 ) return 0;1298if( req() != 3 ) return 0;1299// Check that both paths come from the same If1300Node *ifp1 = region->in(1);1301Node *ifp2 = region->in(2);1302if( !ifp1 || !ifp2 ) return 0;1303Node *iff = ifp1->in(0);1304if( !iff || !iff->is_If() ) return 0;1305if( iff != ifp2->in(0) ) return 0;1306if (check_control_only) return -1;1307// Check for a proper bool/cmp1308const Node *b = iff->in(1);1309if( !b->is_Bool() ) return 0;1310const Node *cmp = b->in(1);1311if( !cmp->is_Cmp() ) return 0;13121313// Check for branching opposite expected1314if( ifp2->Opcode() == Op_IfTrue ) {1315assert( ifp1->Opcode() == Op_IfFalse, "" );1316return 2;1317} else {1318assert( ifp1->Opcode() == Op_IfTrue, "" );1319return 1;1320}1321}13221323//----------------------------check_cmove_id-----------------------------------1324// Check for CMove'ing a constant after comparing against the constant.1325// Happens all the time now, since if we compare equality vs a constant in1326// the parser, we "know" the variable is constant on one path and we force1327// it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a1328// conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more1329// general in that we don't need constants. Since CMove's are only inserted1330// in very special circumstances, we do it here on generic Phi's.1331Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {1332assert(true_path !=0, "only diamond shape graph expected");13331334// is_diamond_phi() has guaranteed the correctness of the nodes sequence:1335// phi->region->if_proj->ifnode->bool->cmp1336Node* region = in(0);1337Node* iff = region->in(1)->in(0);1338BoolNode* b = iff->in(1)->as_Bool();1339Node* cmp = b->in(1);1340Node* tval = in(true_path);1341Node* fval = in(3-true_path);1342Node* id = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);1343if (id == NULL)1344return NULL;13451346// Either value might be a cast that depends on a branch of 'iff'.1347// Since the 'id' value will float free of the diamond, either1348// decast or return failure.1349Node* ctl = id->in(0);1350if (ctl != NULL && ctl->in(0) == iff) {1351if (id->is_ConstraintCast()) {1352return id->in(1);1353} else {1354// Don't know how to disentangle this value.1355return NULL;1356}1357}13581359return id;1360}13611362//------------------------------Identity---------------------------------------1363// Check for Region being Identity.1364Node* PhiNode::Identity(PhaseGVN* phase) {1365// Check for no merging going on1366// (There used to be special-case code here when this->region->is_Loop.1367// It would check for a tributary phi on the backedge that the main phi1368// trivially, perhaps with a single cast. The unique_input method1369// does all this and more, by reducing such tributaries to 'this'.)1370Node* uin = unique_input(phase, false);1371if (uin != NULL) {1372return uin;1373}13741375int true_path = is_diamond_phi();1376// Delay CMove'ing identity if Ideal has not had the chance to handle unsafe cases, yet.1377if (true_path != 0 && !(phase->is_IterGVN() && wait_for_region_igvn(phase))) {1378Node* id = is_cmove_id(phase, true_path);1379if (id != NULL) {1380return id;1381}1382}13831384// Looking for phis with identical inputs. If we find one that has1385// type TypePtr::BOTTOM, replace the current phi with the bottom phi.1386if (phase->is_IterGVN() && type() == Type::MEMORY && adr_type() !=1387TypePtr::BOTTOM && !adr_type()->is_known_instance()) {1388uint phi_len = req();1389Node* phi_reg = region();1390for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {1391Node* u = phi_reg->fast_out(i);1392if (u->is_Phi() && u->as_Phi()->type() == Type::MEMORY &&1393u->adr_type() == TypePtr::BOTTOM && u->in(0) == phi_reg &&1394u->req() == phi_len) {1395for (uint j = 1; j < phi_len; j++) {1396if (in(j) != u->in(j)) {1397u = NULL;1398break;1399}1400}1401if (u != NULL) {1402return u;1403}1404}1405}1406}14071408return this; // No identity1409}14101411//-----------------------------unique_input------------------------------------1412// Find the unique value, discounting top, self-loops, and casts.1413// Return top if there are no inputs, and self if there are multiple.1414Node* PhiNode::unique_input(PhaseTransform* phase, bool uncast) {1415// 1) One unique direct input,1416// or if uncast is true:1417// 2) some of the inputs have an intervening ConstraintCast1418// 3) an input is a self loop1419//1420// 1) input or 2) input or 3) input __1421// / \ / \ \ / \1422// \ / | cast phi cast1423// phi \ / / \ /1424// phi / --14251426Node* r = in(0); // RegionNode1427Node* input = NULL; // The unique direct input (maybe uncasted = ConstraintCasts removed)14281429for (uint i = 1, cnt = req(); i < cnt; ++i) {1430Node* rc = r->in(i);1431if (rc == NULL || phase->type(rc) == Type::TOP)1432continue; // ignore unreachable control path1433Node* n = in(i);1434if (n == NULL)1435continue;1436Node* un = n;1437if (uncast) {1438#ifdef ASSERT1439Node* m = un->uncast();1440#endif1441while (un != NULL && un->req() == 2 && un->is_ConstraintCast()) {1442Node* next = un->in(1);1443if (phase->type(next)->isa_rawptr() && phase->type(un)->isa_oopptr()) {1444// risk exposing raw ptr at safepoint1445break;1446}1447un = next;1448}1449assert(m == un || un->in(1) == m, "Only expected at CheckCastPP from allocation");1450}1451if (un == NULL || un == this || phase->type(un) == Type::TOP) {1452continue; // ignore if top, or in(i) and "this" are in a data cycle1453}1454// Check for a unique input (maybe uncasted)1455if (input == NULL) {1456input = un;1457} else if (input != un) {1458input = NodeSentinel; // no unique input1459}1460}1461if (input == NULL) {1462return phase->C->top(); // no inputs1463}14641465if (input != NodeSentinel) {1466return input; // one unique direct input1467}14681469// Nothing.1470return NULL;1471}14721473//------------------------------is_x2logic-------------------------------------1474// Check for simple convert-to-boolean pattern1475// If:(C Bool) Region:(IfF IfT) Phi:(Region 0 1)1476// Convert Phi to an ConvIB.1477static Node *is_x2logic( PhaseGVN *phase, PhiNode *phi, int true_path ) {1478assert(true_path !=0, "only diamond shape graph expected");1479// Convert the true/false index into an expected 0/1 return.1480// Map 2->0 and 1->1.1481int flipped = 2-true_path;14821483// is_diamond_phi() has guaranteed the correctness of the nodes sequence:1484// phi->region->if_proj->ifnode->bool->cmp1485Node *region = phi->in(0);1486Node *iff = region->in(1)->in(0);1487BoolNode *b = (BoolNode*)iff->in(1);1488const CmpNode *cmp = (CmpNode*)b->in(1);14891490Node *zero = phi->in(1);1491Node *one = phi->in(2);1492const Type *tzero = phase->type( zero );1493const Type *tone = phase->type( one );14941495// Check for compare vs 01496const Type *tcmp = phase->type(cmp->in(2));1497if( tcmp != TypeInt::ZERO && tcmp != TypePtr::NULL_PTR ) {1498// Allow cmp-vs-1 if the other input is bounded by 0-11499if( !(tcmp == TypeInt::ONE && phase->type(cmp->in(1)) == TypeInt::BOOL) )1500return NULL;1501flipped = 1-flipped; // Test is vs 1 instead of 0!1502}15031504// Check for setting zero/one opposite expected1505if( tzero == TypeInt::ZERO ) {1506if( tone == TypeInt::ONE ) {1507} else return NULL;1508} else if( tzero == TypeInt::ONE ) {1509if( tone == TypeInt::ZERO ) {1510flipped = 1-flipped;1511} else return NULL;1512} else return NULL;15131514// Check for boolean test backwards1515if( b->_test._test == BoolTest::ne ) {1516} else if( b->_test._test == BoolTest::eq ) {1517flipped = 1-flipped;1518} else return NULL;15191520// Build int->bool conversion1521Node *n = new Conv2BNode(cmp->in(1));1522if( flipped )1523n = new XorINode( phase->transform(n), phase->intcon(1) );15241525return n;1526}15271528//------------------------------is_cond_add------------------------------------1529// Check for simple conditional add pattern: "(P < Q) ? X+Y : X;"1530// To be profitable the control flow has to disappear; there can be no other1531// values merging here. We replace the test-and-branch with:1532// "(sgn(P-Q))&Y) + X". Basically, convert "(P < Q)" into 0 or -1 by1533// moving the carry bit from (P-Q) into a register with 'sbb EAX,EAX'.1534// Then convert Y to 0-or-Y and finally add.1535// This is a key transform for SpecJava _201_compress.1536static Node* is_cond_add(PhaseGVN *phase, PhiNode *phi, int true_path) {1537assert(true_path !=0, "only diamond shape graph expected");15381539// is_diamond_phi() has guaranteed the correctness of the nodes sequence:1540// phi->region->if_proj->ifnode->bool->cmp1541RegionNode *region = (RegionNode*)phi->in(0);1542Node *iff = region->in(1)->in(0);1543BoolNode* b = iff->in(1)->as_Bool();1544const CmpNode *cmp = (CmpNode*)b->in(1);15451546// Make sure only merging this one phi here1547if (region->has_unique_phi() != phi) return NULL;15481549// Make sure each arm of the diamond has exactly one output, which we assume1550// is the region. Otherwise, the control flow won't disappear.1551if (region->in(1)->outcnt() != 1) return NULL;1552if (region->in(2)->outcnt() != 1) return NULL;15531554// Check for "(P < Q)" of type signed int1555if (b->_test._test != BoolTest::lt) return NULL;1556if (cmp->Opcode() != Op_CmpI) return NULL;15571558Node *p = cmp->in(1);1559Node *q = cmp->in(2);1560Node *n1 = phi->in( true_path);1561Node *n2 = phi->in(3-true_path);15621563int op = n1->Opcode();1564if( op != Op_AddI // Need zero as additive identity1565/*&&op != Op_SubI &&1566op != Op_AddP &&1567op != Op_XorI &&1568op != Op_OrI*/ )1569return NULL;15701571Node *x = n2;1572Node *y = NULL;1573if( x == n1->in(1) ) {1574y = n1->in(2);1575} else if( x == n1->in(2) ) {1576y = n1->in(1);1577} else return NULL;15781579// Not so profitable if compare and add are constants1580if( q->is_Con() && phase->type(q) != TypeInt::ZERO && y->is_Con() )1581return NULL;15821583Node *cmplt = phase->transform( new CmpLTMaskNode(p,q) );1584Node *j_and = phase->transform( new AndINode(cmplt,y) );1585return new AddINode(j_and,x);1586}15871588//------------------------------is_absolute------------------------------------1589// Check for absolute value.1590static Node* is_absolute( PhaseGVN *phase, PhiNode *phi_root, int true_path) {1591assert(true_path !=0, "only diamond shape graph expected");15921593int cmp_zero_idx = 0; // Index of compare input where to look for zero1594int phi_x_idx = 0; // Index of phi input where to find naked x15951596// ABS ends with the merge of 2 control flow paths.1597// Find the false path from the true path. With only 2 inputs, 3 - x works nicely.1598int false_path = 3 - true_path;15991600// is_diamond_phi() has guaranteed the correctness of the nodes sequence:1601// phi->region->if_proj->ifnode->bool->cmp1602BoolNode *bol = phi_root->in(0)->in(1)->in(0)->in(1)->as_Bool();1603Node *cmp = bol->in(1);16041605// Check bool sense1606if (cmp->Opcode() == Op_CmpF || cmp->Opcode() == Op_CmpD) {1607switch (bol->_test._test) {1608case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = true_path; break;1609case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = false_path; break;1610case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = true_path; break;1611case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = false_path; break;1612default: return NULL; break;1613}1614} else if (cmp->Opcode() == Op_CmpI || cmp->Opcode() == Op_CmpL) {1615switch (bol->_test._test) {1616case BoolTest::lt:1617case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = false_path; break;1618case BoolTest::gt:1619case BoolTest::ge: cmp_zero_idx = 2; phi_x_idx = true_path; break;1620default: return NULL; break;1621}1622}16231624// Test is next1625const Type *tzero = NULL;1626switch (cmp->Opcode()) {1627case Op_CmpI: tzero = TypeInt::ZERO; break; // Integer ABS1628case Op_CmpL: tzero = TypeLong::ZERO; break; // Long ABS1629case Op_CmpF: tzero = TypeF::ZERO; break; // Float ABS1630case Op_CmpD: tzero = TypeD::ZERO; break; // Double ABS1631default: return NULL;1632}16331634// Find zero input of compare; the other input is being abs'd1635Node *x = NULL;1636bool flip = false;1637if( phase->type(cmp->in(cmp_zero_idx)) == tzero ) {1638x = cmp->in(3 - cmp_zero_idx);1639} else if( phase->type(cmp->in(3 - cmp_zero_idx)) == tzero ) {1640// The test is inverted, we should invert the result...1641x = cmp->in(cmp_zero_idx);1642flip = true;1643} else {1644return NULL;1645}16461647// Next get the 2 pieces being selected, one is the original value1648// and the other is the negated value.1649if( phi_root->in(phi_x_idx) != x ) return NULL;16501651// Check other phi input for subtract node1652Node *sub = phi_root->in(3 - phi_x_idx);16531654bool is_sub = sub->Opcode() == Op_SubF || sub->Opcode() == Op_SubD ||1655sub->Opcode() == Op_SubI || sub->Opcode() == Op_SubL;16561657// Allow only Sub(0,X) and fail out for all others; Neg is not OK1658if (!is_sub || phase->type(sub->in(1)) != tzero || sub->in(2) != x) return NULL;16591660if (tzero == TypeF::ZERO) {1661x = new AbsFNode(x);1662if (flip) {1663x = new SubFNode(sub->in(1), phase->transform(x));1664}1665} else if (tzero == TypeD::ZERO) {1666x = new AbsDNode(x);1667if (flip) {1668x = new SubDNode(sub->in(1), phase->transform(x));1669}1670} else if (tzero == TypeInt::ZERO && Matcher::match_rule_supported(Op_AbsI)) {1671x = new AbsINode(x);1672if (flip) {1673x = new SubINode(sub->in(1), phase->transform(x));1674}1675} else if (tzero == TypeLong::ZERO && Matcher::match_rule_supported(Op_AbsL)) {1676x = new AbsLNode(x);1677if (flip) {1678x = new SubLNode(sub->in(1), phase->transform(x));1679}1680} else return NULL;16811682return x;1683}16841685//------------------------------split_once-------------------------------------1686// Helper for split_flow_path1687static void split_once(PhaseIterGVN *igvn, Node *phi, Node *val, Node *n, Node *newn) {1688igvn->hash_delete(n); // Remove from hash before hacking edges16891690uint j = 1;1691for (uint i = phi->req()-1; i > 0; i--) {1692if (phi->in(i) == val) { // Found a path with val?1693// Add to NEW Region/Phi, no DU info1694newn->set_req( j++, n->in(i) );1695// Remove from OLD Region/Phi1696n->del_req(i);1697}1698}16991700// Register the new node but do not transform it. Cannot transform until the1701// entire Region/Phi conglomerate has been hacked as a single huge transform.1702igvn->register_new_node_with_optimizer( newn );17031704// Now I can point to the new node.1705n->add_req(newn);1706igvn->_worklist.push(n);1707}17081709//------------------------------split_flow_path--------------------------------1710// Check for merging identical values and split flow paths1711static Node* split_flow_path(PhaseGVN *phase, PhiNode *phi) {1712BasicType bt = phi->type()->basic_type();1713if( bt == T_ILLEGAL || type2size[bt] <= 0 )1714return NULL; // Bail out on funny non-value stuff1715if( phi->req() <= 3 ) // Need at least 2 matched inputs and a1716return NULL; // third unequal input to be worth doing17171718// Scan for a constant1719uint i;1720for( i = 1; i < phi->req()-1; i++ ) {1721Node *n = phi->in(i);1722if( !n ) return NULL;1723if( phase->type(n) == Type::TOP ) return NULL;1724if( n->Opcode() == Op_ConP || n->Opcode() == Op_ConN || n->Opcode() == Op_ConNKlass )1725break;1726}1727if( i >= phi->req() ) // Only split for constants1728return NULL;17291730Node *val = phi->in(i); // Constant to split for1731uint hit = 0; // Number of times it occurs1732Node *r = phi->region();17331734for( ; i < phi->req(); i++ ){ // Count occurrences of constant1735Node *n = phi->in(i);1736if( !n ) return NULL;1737if( phase->type(n) == Type::TOP ) return NULL;1738if( phi->in(i) == val ) {1739hit++;1740if (PhaseIdealLoop::find_predicate(r->in(i)) != NULL) {1741return NULL; // don't split loop entry path1742}1743}1744}17451746if( hit <= 1 || // Make sure we find 2 or more1747hit == phi->req()-1 ) // and not ALL the same value1748return NULL;17491750// Now start splitting out the flow paths that merge the same value.1751// Split first the RegionNode.1752PhaseIterGVN *igvn = phase->is_IterGVN();1753RegionNode *newr = new RegionNode(hit+1);1754split_once(igvn, phi, val, r, newr);17551756// Now split all other Phis than this one1757for (DUIterator_Fast kmax, k = r->fast_outs(kmax); k < kmax; k++) {1758Node* phi2 = r->fast_out(k);1759if( phi2->is_Phi() && phi2->as_Phi() != phi ) {1760PhiNode *newphi = PhiNode::make_blank(newr, phi2);1761split_once(igvn, phi, val, phi2, newphi);1762}1763}17641765// Clean up this guy1766igvn->hash_delete(phi);1767for( i = phi->req()-1; i > 0; i-- ) {1768if( phi->in(i) == val ) {1769phi->del_req(i);1770}1771}1772phi->add_req(val);17731774return phi;1775}17761777//=============================================================================1778//------------------------------simple_data_loop_check-------------------------1779// Try to determining if the phi node in a simple safe/unsafe data loop.1780// Returns:1781// enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop };1782// Safe - safe case when the phi and it's inputs reference only safe data1783// nodes;1784// Unsafe - the phi and it's inputs reference unsafe data nodes but there1785// is no reference back to the phi - need a graph walk1786// to determine if it is in a loop;1787// UnsafeLoop - unsafe case when the phi references itself directly or through1788// unsafe data node.1789// Note: a safe data node is a node which could/never reference itself during1790// GVN transformations. For now it is Con, Proj, Phi, CastPP, CheckCastPP.1791// I mark Phi nodes as safe node not only because they can reference itself1792// but also to prevent mistaking the fallthrough case inside an outer loop1793// as dead loop when the phi references itselfs through an other phi.1794PhiNode::LoopSafety PhiNode::simple_data_loop_check(Node *in) const {1795// It is unsafe loop if the phi node references itself directly.1796if (in == (Node*)this)1797return UnsafeLoop; // Unsafe loop1798// Unsafe loop if the phi node references itself through an unsafe data node.1799// Exclude cases with null inputs or data nodes which could reference1800// itself (safe for dead loops).1801if (in != NULL && !in->is_dead_loop_safe()) {1802// Check inputs of phi's inputs also.1803// It is much less expensive then full graph walk.1804uint cnt = in->req();1805uint i = (in->is_Proj() && !in->is_CFG()) ? 0 : 1;1806for (; i < cnt; ++i) {1807Node* m = in->in(i);1808if (m == (Node*)this)1809return UnsafeLoop; // Unsafe loop1810if (m != NULL && !m->is_dead_loop_safe()) {1811// Check the most common case (about 30% of all cases):1812// phi->Load/Store->AddP->(ConP ConP Con)/(Parm Parm Con).1813Node *m1 = (m->is_AddP() && m->req() > 3) ? m->in(1) : NULL;1814if (m1 == (Node*)this)1815return UnsafeLoop; // Unsafe loop1816if (m1 != NULL && m1 == m->in(2) &&1817m1->is_dead_loop_safe() && m->in(3)->is_Con()) {1818continue; // Safe case1819}1820// The phi references an unsafe node - need full analysis.1821return Unsafe;1822}1823}1824}1825return Safe; // Safe case - we can optimize the phi node.1826}18271828//------------------------------is_unsafe_data_reference-----------------------1829// If phi can be reached through the data input - it is data loop.1830bool PhiNode::is_unsafe_data_reference(Node *in) const {1831assert(req() > 1, "");1832// First, check simple cases when phi references itself directly or1833// through an other node.1834LoopSafety safety = simple_data_loop_check(in);1835if (safety == UnsafeLoop)1836return true; // phi references itself - unsafe loop1837else if (safety == Safe)1838return false; // Safe case - phi could be replaced with the unique input.18391840// Unsafe case when we should go through data graph to determine1841// if the phi references itself.18421843ResourceMark rm;18441845Node_List nstack;1846VectorSet visited;18471848nstack.push(in); // Start with unique input.1849visited.set(in->_idx);1850while (nstack.size() != 0) {1851Node* n = nstack.pop();1852uint cnt = n->req();1853uint i = (n->is_Proj() && !n->is_CFG()) ? 0 : 1;1854for (; i < cnt; i++) {1855Node* m = n->in(i);1856if (m == (Node*)this) {1857return true; // Data loop1858}1859if (m != NULL && !m->is_dead_loop_safe()) { // Only look for unsafe cases.1860if (!visited.test_set(m->_idx))1861nstack.push(m);1862}1863}1864}1865return false; // The phi is not reachable from its inputs1866}18671868// Is this Phi's region or some inputs to the region enqueued for IGVN1869// and so could cause the region to be optimized out?1870bool PhiNode::wait_for_region_igvn(PhaseGVN* phase) {1871PhaseIterGVN* igvn = phase->is_IterGVN();1872Unique_Node_List& worklist = igvn->_worklist;1873bool delay = false;1874Node* r = in(0);1875for (uint j = 1; j < req(); j++) {1876Node* rc = r->in(j);1877Node* n = in(j);1878if (rc != NULL &&1879rc->is_Proj()) {1880if (worklist.member(rc)) {1881delay = true;1882} else if (rc->in(0) != NULL &&1883rc->in(0)->is_If()) {1884if (worklist.member(rc->in(0))) {1885delay = true;1886} else if (rc->in(0)->in(1) != NULL &&1887rc->in(0)->in(1)->is_Bool()) {1888if (worklist.member(rc->in(0)->in(1))) {1889delay = true;1890} else if (rc->in(0)->in(1)->in(1) != NULL &&1891rc->in(0)->in(1)->in(1)->is_Cmp()) {1892if (worklist.member(rc->in(0)->in(1)->in(1))) {1893delay = true;1894}1895}1896}1897}1898}1899}1900if (delay) {1901worklist.push(this);1902}1903return delay;1904}19051906//------------------------------Ideal------------------------------------------1907// Return a node which is more "ideal" than the current node. Must preserve1908// the CFG, but we can still strip out dead paths.1909Node *PhiNode::Ideal(PhaseGVN *phase, bool can_reshape) {1910Node *r = in(0); // RegionNode1911assert(r != NULL && r->is_Region(), "this phi must have a region");1912assert(r->in(0) == NULL || !r->in(0)->is_Root(), "not a specially hidden merge");19131914// Note: During parsing, phis are often transformed before their regions.1915// This means we have to use type_or_null to defend against untyped regions.1916if( phase->type_or_null(r) == Type::TOP ) // Dead code?1917return NULL; // No change19181919Node *top = phase->C->top();1920bool new_phi = (outcnt() == 0); // transforming new Phi1921// No change for igvn if new phi is not hooked1922if (new_phi && can_reshape)1923return NULL;19241925// The are 2 situations when only one valid phi's input is left1926// (in addition to Region input).1927// One: region is not loop - replace phi with this input.1928// Two: region is loop - replace phi with top since this data path is dead1929// and we need to break the dead data loop.1930Node* progress = NULL; // Record if any progress made1931for( uint j = 1; j < req(); ++j ){ // For all paths in1932// Check unreachable control paths1933Node* rc = r->in(j);1934Node* n = in(j); // Get the input1935if (rc == NULL || phase->type(rc) == Type::TOP) {1936if (n != top) { // Not already top?1937PhaseIterGVN *igvn = phase->is_IterGVN();1938if (can_reshape && igvn != NULL) {1939igvn->_worklist.push(r);1940}1941// Nuke it down1942set_req_X(j, top, phase);1943progress = this; // Record progress1944}1945}1946}19471948if (can_reshape && outcnt() == 0) {1949// set_req() above may kill outputs if Phi is referenced1950// only by itself on the dead (top) control path.1951return top;1952}19531954bool uncasted = false;1955Node* uin = unique_input(phase, false);1956if (uin == NULL && can_reshape &&1957// If there is a chance that the region can be optimized out do1958// not add a cast node that we can't remove yet.1959!wait_for_region_igvn(phase)) {1960uncasted = true;1961uin = unique_input(phase, true);1962}1963if (uin == top) { // Simplest case: no alive inputs.1964if (can_reshape) // IGVN transformation1965return top;1966else1967return NULL; // Identity will return TOP1968} else if (uin != NULL) {1969// Only one not-NULL unique input path is left.1970// Determine if this input is backedge of a loop.1971// (Skip new phis which have no uses and dead regions).1972if (outcnt() > 0 && r->in(0) != NULL) {1973if (is_data_loop(r->as_Region(), uin, phase)) {1974// Break this data loop to avoid creation of a dead loop.1975if (can_reshape) {1976return top;1977} else {1978// We can't return top if we are in Parse phase - cut inputs only1979// let Identity to handle the case.1980replace_edge(uin, top, phase);1981return NULL;1982}1983}1984}19851986if (uncasted) {1987// Add cast nodes between the phi to be removed and its unique input.1988// Wait until after parsing for the type information to propagate from the casts.1989assert(can_reshape, "Invalid during parsing");1990const Type* phi_type = bottom_type();1991// Add casts to carry the control dependency of the Phi that is1992// going away1993Node* cast = NULL;1994if (phi_type->isa_ptr()) {1995const Type* uin_type = phase->type(uin);1996if (!phi_type->isa_oopptr() && !uin_type->isa_oopptr()) {1997cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, phi_type, ConstraintCastNode::StrongDependency);1998} else {1999// Use a CastPP for a cast to not null and a CheckCastPP for2000// a cast to a new klass (and both if both null-ness and2001// klass change).20022003// If the type of phi is not null but the type of uin may be2004// null, uin's type must be casted to not null2005if (phi_type->join(TypePtr::NOTNULL) == phi_type->remove_speculative() &&2006uin_type->join(TypePtr::NOTNULL) != uin_type->remove_speculative()) {2007cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, TypePtr::NOTNULL, ConstraintCastNode::StrongDependency);2008}20092010// If the type of phi and uin, both casted to not null,2011// differ the klass of uin must be (check)cast'ed to match2012// that of phi2013if (phi_type->join_speculative(TypePtr::NOTNULL) != uin_type->join_speculative(TypePtr::NOTNULL)) {2014Node* n = uin;2015if (cast != NULL) {2016cast = phase->transform(cast);2017n = cast;2018}2019cast = ConstraintCastNode::make_cast(Op_CheckCastPP, r, n, phi_type, ConstraintCastNode::StrongDependency);2020}2021if (cast == NULL) {2022cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, phi_type, ConstraintCastNode::StrongDependency);2023}2024}2025} else {2026cast = ConstraintCastNode::make_cast_for_type(r, uin, phi_type, ConstraintCastNode::StrongDependency);2027}2028assert(cast != NULL, "cast should be set");2029cast = phase->transform(cast);2030// set all inputs to the new cast(s) so the Phi is removed by Identity2031PhaseIterGVN* igvn = phase->is_IterGVN();2032for (uint i = 1; i < req(); i++) {2033set_req_X(i, cast, igvn);2034}2035uin = cast;2036}20372038// One unique input.2039debug_only(Node* ident = Identity(phase));2040// The unique input must eventually be detected by the Identity call.2041#ifdef ASSERT2042if (ident != uin && !ident->is_top()) {2043// print this output before failing assert2044r->dump(3);2045this->dump(3);2046ident->dump();2047uin->dump();2048}2049#endif2050assert(ident == uin || ident->is_top(), "Identity must clean this up");2051return NULL;2052}20532054Node* opt = NULL;2055int true_path = is_diamond_phi();2056if (true_path != 0 &&2057// If one of the diamond's branch is in the process of dying then, the Phi's input for that branch might transform2058// to top. If that happens replacing the Phi with an operation that consumes the Phi's inputs will cause the Phi2059// to be replaced by top. To prevent that, delay the transformation until the branch has a chance to be removed.2060!(can_reshape && wait_for_region_igvn(phase))) {2061// Check for CMove'ing identity. If it would be unsafe,2062// handle it here. In the safe case, let Identity handle it.2063Node* unsafe_id = is_cmove_id(phase, true_path);2064if( unsafe_id != NULL && is_unsafe_data_reference(unsafe_id) )2065opt = unsafe_id;20662067// Check for simple convert-to-boolean pattern2068if( opt == NULL )2069opt = is_x2logic(phase, this, true_path);20702071// Check for absolute value2072if( opt == NULL )2073opt = is_absolute(phase, this, true_path);20742075// Check for conditional add2076if( opt == NULL && can_reshape )2077opt = is_cond_add(phase, this, true_path);20782079// These 4 optimizations could subsume the phi:2080// have to check for a dead data loop creation.2081if( opt != NULL ) {2082if( opt == unsafe_id || is_unsafe_data_reference(opt) ) {2083// Found dead loop.2084if( can_reshape )2085return top;2086// We can't return top if we are in Parse phase - cut inputs only2087// to stop further optimizations for this phi. Identity will return TOP.2088assert(req() == 3, "only diamond merge phi here");2089set_req(1, top);2090set_req(2, top);2091return NULL;2092} else {2093return opt;2094}2095}2096}20972098// Check for merging identical values and split flow paths2099if (can_reshape) {2100opt = split_flow_path(phase, this);2101// This optimization only modifies phi - don't need to check for dead loop.2102assert(opt == NULL || opt == this, "do not elide phi");2103if (opt != NULL) return opt;2104}21052106if (in(1) != NULL && in(1)->Opcode() == Op_AddP && can_reshape) {2107// Try to undo Phi of AddP:2108// (Phi (AddP base address offset) (AddP base2 address2 offset2))2109// becomes:2110// newbase := (Phi base base2)2111// newaddress := (Phi address address2)2112// newoffset := (Phi offset offset2)2113// (AddP newbase newaddress newoffset)2114//2115// This occurs as a result of unsuccessful split_thru_phi and2116// interferes with taking advantage of addressing modes. See the2117// clone_shift_expressions code in matcher.cpp2118Node* addp = in(1);2119Node* base = addp->in(AddPNode::Base);2120Node* address = addp->in(AddPNode::Address);2121Node* offset = addp->in(AddPNode::Offset);2122if (base != NULL && address != NULL && offset != NULL &&2123!base->is_top() && !address->is_top() && !offset->is_top()) {2124const Type* base_type = base->bottom_type();2125const Type* address_type = address->bottom_type();2126// make sure that all the inputs are similar to the first one,2127// i.e. AddP with base == address and same offset as first AddP2128bool doit = true;2129for (uint i = 2; i < req(); i++) {2130if (in(i) == NULL ||2131in(i)->Opcode() != Op_AddP ||2132in(i)->in(AddPNode::Base) == NULL ||2133in(i)->in(AddPNode::Address) == NULL ||2134in(i)->in(AddPNode::Offset) == NULL ||2135in(i)->in(AddPNode::Base)->is_top() ||2136in(i)->in(AddPNode::Address)->is_top() ||2137in(i)->in(AddPNode::Offset)->is_top()) {2138doit = false;2139break;2140}2141if (in(i)->in(AddPNode::Base) != base) {2142base = NULL;2143}2144if (in(i)->in(AddPNode::Offset) != offset) {2145offset = NULL;2146}2147if (in(i)->in(AddPNode::Address) != address) {2148address = NULL;2149}2150// Accumulate type for resulting Phi2151base_type = base_type->meet_speculative(in(i)->in(AddPNode::Base)->bottom_type());2152address_type = address_type->meet_speculative(in(i)->in(AddPNode::Address)->bottom_type());2153}2154if (doit && base == NULL) {2155// Check for neighboring AddP nodes in a tree.2156// If they have a base, use that it.2157for (DUIterator_Fast kmax, k = this->fast_outs(kmax); k < kmax; k++) {2158Node* u = this->fast_out(k);2159if (u->is_AddP()) {2160Node* base2 = u->in(AddPNode::Base);2161if (base2 != NULL && !base2->is_top()) {2162if (base == NULL)2163base = base2;2164else if (base != base2)2165{ doit = false; break; }2166}2167}2168}2169}2170if (doit) {2171if (base == NULL) {2172base = new PhiNode(in(0), base_type, NULL);2173for (uint i = 1; i < req(); i++) {2174base->init_req(i, in(i)->in(AddPNode::Base));2175}2176phase->is_IterGVN()->register_new_node_with_optimizer(base);2177}2178if (address == NULL) {2179address = new PhiNode(in(0), address_type, NULL);2180for (uint i = 1; i < req(); i++) {2181address->init_req(i, in(i)->in(AddPNode::Address));2182}2183phase->is_IterGVN()->register_new_node_with_optimizer(address);2184}2185if (offset == NULL) {2186offset = new PhiNode(in(0), TypeX_X, NULL);2187for (uint i = 1; i < req(); i++) {2188offset->init_req(i, in(i)->in(AddPNode::Offset));2189}2190phase->is_IterGVN()->register_new_node_with_optimizer(offset);2191}2192return new AddPNode(base, address, offset);2193}2194}2195}21962197// Split phis through memory merges, so that the memory merges will go away.2198// Piggy-back this transformation on the search for a unique input....2199// It will be as if the merged memory is the unique value of the phi.2200// (Do not attempt this optimization unless parsing is complete.2201// It would make the parser's memory-merge logic sick.)2202// (MergeMemNode is not dead_loop_safe - need to check for dead loop.)2203if (progress == NULL && can_reshape && type() == Type::MEMORY) {2204// see if this phi should be sliced2205uint merge_width = 0;2206bool saw_self = false;2207for( uint i=1; i<req(); ++i ) {// For all paths in2208Node *ii = in(i);2209// TOP inputs should not be counted as safe inputs because if the2210// Phi references itself through all other inputs then splitting the2211// Phi through memory merges would create dead loop at later stage.2212if (ii == top) {2213return NULL; // Delay optimization until graph is cleaned.2214}2215if (ii->is_MergeMem()) {2216MergeMemNode* n = ii->as_MergeMem();2217merge_width = MAX2(merge_width, n->req());2218saw_self = saw_self || (n->base_memory() == this);2219}2220}22212222// This restriction is temporarily necessary to ensure termination:2223if (!saw_self && adr_type() == TypePtr::BOTTOM) merge_width = 0;22242225if (merge_width > Compile::AliasIdxRaw) {2226// found at least one non-empty MergeMem2227const TypePtr* at = adr_type();2228if (at != TypePtr::BOTTOM) {2229// Patch the existing phi to select an input from the merge:2230// Phi:AT1(...MergeMem(m0, m1, m2)...) into2231// Phi:AT1(...m1...)2232int alias_idx = phase->C->get_alias_index(at);2233for (uint i=1; i<req(); ++i) {2234Node *ii = in(i);2235if (ii->is_MergeMem()) {2236MergeMemNode* n = ii->as_MergeMem();2237// compress paths and change unreachable cycles to TOP2238// If not, we can update the input infinitely along a MergeMem cycle2239// Equivalent code is in MemNode::Ideal_common2240Node *m = phase->transform(n);2241if (outcnt() == 0) { // Above transform() may kill us!2242return top;2243}2244// If transformed to a MergeMem, get the desired slice2245// Otherwise the returned node represents memory for every slice2246Node *new_mem = (m->is_MergeMem()) ?2247m->as_MergeMem()->memory_at(alias_idx) : m;2248// Update input if it is progress over what we have now2249if (new_mem != ii) {2250set_req_X(i, new_mem, phase->is_IterGVN());2251progress = this;2252}2253}2254}2255} else {2256// We know that at least one MergeMem->base_memory() == this2257// (saw_self == true). If all other inputs also references this phi2258// (directly or through data nodes) - it is a dead loop.2259bool saw_safe_input = false;2260for (uint j = 1; j < req(); ++j) {2261Node* n = in(j);2262if (n->is_MergeMem()) {2263MergeMemNode* mm = n->as_MergeMem();2264if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {2265// Skip this input if it references back to this phi or if the memory path is dead2266continue;2267}2268}2269if (!is_unsafe_data_reference(n)) {2270saw_safe_input = true; // found safe input2271break;2272}2273}2274if (!saw_safe_input) {2275// There is a dead loop: All inputs are either dead or reference back to this phi2276return top;2277}22782279// Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into2280// MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))2281PhaseIterGVN* igvn = phase->is_IterGVN();2282assert(igvn != NULL, "sanity check");2283Node* hook = new Node(1);2284PhiNode* new_base = (PhiNode*) clone();2285// Must eagerly register phis, since they participate in loops.2286igvn->register_new_node_with_optimizer(new_base);2287hook->add_req(new_base);22882289MergeMemNode* result = MergeMemNode::make(new_base);2290for (uint i = 1; i < req(); ++i) {2291Node *ii = in(i);2292if (ii->is_MergeMem()) {2293MergeMemNode* n = ii->as_MergeMem();2294for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {2295// If we have not seen this slice yet, make a phi for it.2296bool made_new_phi = false;2297if (mms.is_empty()) {2298Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));2299made_new_phi = true;2300igvn->register_new_node_with_optimizer(new_phi);2301hook->add_req(new_phi);2302mms.set_memory(new_phi);2303}2304Node* phi = mms.memory();2305assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");2306phi->set_req(i, mms.memory2());2307}2308}2309}2310// Distribute all self-loops.2311{ // (Extra braces to hide mms.)2312for (MergeMemStream mms(result); mms.next_non_empty(); ) {2313Node* phi = mms.memory();2314for (uint i = 1; i < req(); ++i) {2315if (phi->in(i) == this) phi->set_req(i, phi);2316}2317}2318}2319// Already replace this phi node to cut it off from the graph to not interfere in dead loop checks during the2320// transformations of the new phi nodes below. Otherwise, we could wrongly conclude that there is no dead loop2321// because we are finding this phi node again. Also set the type of the new MergeMem node in case we are also2322// visiting it in the transformations below.2323igvn->replace_node(this, result);2324igvn->set_type(result, result->bottom_type());23252326// now transform the new nodes, and return the mergemem2327for (MergeMemStream mms(result); mms.next_non_empty(); ) {2328Node* phi = mms.memory();2329mms.set_memory(phase->transform(phi));2330}2331hook->destruct(igvn);2332// Replace self with the result.2333return result;2334}2335}2336//2337// Other optimizations on the memory chain2338//2339const TypePtr* at = adr_type();2340for( uint i=1; i<req(); ++i ) {// For all paths in2341Node *ii = in(i);2342Node *new_in = MemNode::optimize_memory_chain(ii, at, NULL, phase);2343if (ii != new_in ) {2344set_req(i, new_in);2345progress = this;2346}2347}2348}23492350#ifdef _LP642351// Push DecodeN/DecodeNKlass down through phi.2352// The rest of phi graph will transform by split EncodeP node though phis up.2353if ((UseCompressedOops || UseCompressedClassPointers) && can_reshape && progress == NULL) {2354bool may_push = true;2355bool has_decodeN = false;2356bool is_decodeN = false;2357for (uint i=1; i<req(); ++i) {// For all paths in2358Node *ii = in(i);2359if (ii->is_DecodeNarrowPtr() && ii->bottom_type() == bottom_type()) {2360// Do optimization if a non dead path exist.2361if (ii->in(1)->bottom_type() != Type::TOP) {2362has_decodeN = true;2363is_decodeN = ii->is_DecodeN();2364}2365} else if (!ii->is_Phi()) {2366may_push = false;2367}2368}23692370if (has_decodeN && may_push) {2371PhaseIterGVN *igvn = phase->is_IterGVN();2372// Make narrow type for new phi.2373const Type* narrow_t;2374if (is_decodeN) {2375narrow_t = TypeNarrowOop::make(this->bottom_type()->is_ptr());2376} else {2377narrow_t = TypeNarrowKlass::make(this->bottom_type()->is_ptr());2378}2379PhiNode* new_phi = new PhiNode(r, narrow_t);2380uint orig_cnt = req();2381for (uint i=1; i<req(); ++i) {// For all paths in2382Node *ii = in(i);2383Node* new_ii = NULL;2384if (ii->is_DecodeNarrowPtr()) {2385assert(ii->bottom_type() == bottom_type(), "sanity");2386new_ii = ii->in(1);2387} else {2388assert(ii->is_Phi(), "sanity");2389if (ii->as_Phi() == this) {2390new_ii = new_phi;2391} else {2392if (is_decodeN) {2393new_ii = new EncodePNode(ii, narrow_t);2394} else {2395new_ii = new EncodePKlassNode(ii, narrow_t);2396}2397igvn->register_new_node_with_optimizer(new_ii);2398}2399}2400new_phi->set_req(i, new_ii);2401}2402igvn->register_new_node_with_optimizer(new_phi, this);2403if (is_decodeN) {2404progress = new DecodeNNode(new_phi, bottom_type());2405} else {2406progress = new DecodeNKlassNode(new_phi, bottom_type());2407}2408}2409}2410#endif24112412// Phi (VB ... VB) => VB (Phi ...) (Phi ...)2413if (EnableVectorReboxing && can_reshape && progress == NULL) {2414PhaseIterGVN* igvn = phase->is_IterGVN();24152416bool all_inputs_are_equiv_vboxes = true;2417for (uint i = 1; i < req(); ++i) {2418Node* n = in(i);2419if (in(i)->Opcode() != Op_VectorBox) {2420all_inputs_are_equiv_vboxes = false;2421break;2422}2423// Check that vector type of vboxes is equivalent2424if (i != 1) {2425if (Type::cmp(in(i-0)->in(VectorBoxNode::Value)->bottom_type(),2426in(i-1)->in(VectorBoxNode::Value)->bottom_type()) != 0) {2427all_inputs_are_equiv_vboxes = false;2428break;2429}2430if (Type::cmp(in(i-0)->in(VectorBoxNode::Box)->bottom_type(),2431in(i-1)->in(VectorBoxNode::Box)->bottom_type()) != 0) {2432all_inputs_are_equiv_vboxes = false;2433break;2434}2435}2436}24372438if (all_inputs_are_equiv_vboxes) {2439VectorBoxNode* vbox = static_cast<VectorBoxNode*>(in(1));2440PhiNode* new_vbox_phi = new PhiNode(r, vbox->box_type());2441PhiNode* new_vect_phi = new PhiNode(r, vbox->vec_type());2442for (uint i = 1; i < req(); ++i) {2443VectorBoxNode* old_vbox = static_cast<VectorBoxNode*>(in(i));2444new_vbox_phi->set_req(i, old_vbox->in(VectorBoxNode::Box));2445new_vect_phi->set_req(i, old_vbox->in(VectorBoxNode::Value));2446}2447igvn->register_new_node_with_optimizer(new_vbox_phi, this);2448igvn->register_new_node_with_optimizer(new_vect_phi, this);2449progress = new VectorBoxNode(igvn->C, new_vbox_phi, new_vect_phi, vbox->box_type(), vbox->vec_type());2450}2451}24522453return progress; // Return any progress2454}24552456bool PhiNode::is_data_loop(RegionNode* r, Node* uin, const PhaseGVN* phase) {2457// First, take the short cut when we know it is a loop and the EntryControl data path is dead.2458// The loop node may only have one input because the entry path was removed in PhaseIdealLoop::Dominators().2459// Then, check if there is a data loop when the phi references itself directly or through other data nodes.2460assert(!r->is_Loop() || r->req() <= 3, "Loop node should have 3 or less inputs");2461const bool is_loop = (r->is_Loop() && r->req() == 3);2462const Node* top = phase->C->top();2463if (is_loop) {2464return !uin->eqv_uncast(in(LoopNode::EntryControl));2465} else {2466// We have a data loop either with an unsafe data reference or if a region is unreachable.2467return is_unsafe_data_reference(uin)2468|| (r->req() == 3 && (r->in(1) != top && r->in(2) == top && r->is_unreachable_region(phase)));2469}2470}24712472//------------------------------is_tripcount-----------------------------------2473bool PhiNode::is_tripcount(BasicType bt) const {2474return (in(0) != NULL && in(0)->is_BaseCountedLoop() &&2475in(0)->as_BaseCountedLoop()->operates_on(bt, true) &&2476in(0)->as_BaseCountedLoop()->phi() == this);2477}24782479//------------------------------out_RegMask------------------------------------2480const RegMask &PhiNode::in_RegMask(uint i) const {2481return i ? out_RegMask() : RegMask::Empty;2482}24832484const RegMask &PhiNode::out_RegMask() const {2485uint ideal_reg = _type->ideal_reg();2486assert( ideal_reg != Node::NotAMachineReg, "invalid type at Phi" );2487if( ideal_reg == 0 ) return RegMask::Empty;2488assert(ideal_reg != Op_RegFlags, "flags register is not spillable");2489return *(Compile::current()->matcher()->idealreg2spillmask[ideal_reg]);2490}24912492#ifndef PRODUCT2493void PhiNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {2494// For a PhiNode, the set of related nodes includes all inputs till level 2,2495// and all outputs till level 1. In compact mode, inputs till level 1 are2496// collected.2497this->collect_nodes(in_rel, compact ? 1 : 2, false, false);2498this->collect_nodes(out_rel, -1, false, false);2499}25002501void PhiNode::dump_spec(outputStream *st) const {2502TypeNode::dump_spec(st);2503if (is_tripcount(T_INT) || is_tripcount(T_LONG)) {2504st->print(" #tripcount");2505}2506}2507#endif250825092510//=============================================================================2511const Type* GotoNode::Value(PhaseGVN* phase) const {2512// If the input is reachable, then we are executed.2513// If the input is not reachable, then we are not executed.2514return phase->type(in(0));2515}25162517Node* GotoNode::Identity(PhaseGVN* phase) {2518return in(0); // Simple copy of incoming control2519}25202521const RegMask &GotoNode::out_RegMask() const {2522return RegMask::Empty;2523}25242525#ifndef PRODUCT2526//-----------------------------related-----------------------------------------2527// The related nodes of a GotoNode are all inputs at level 1, as well as the2528// outputs at level 1. This is regardless of compact mode.2529void GotoNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {2530this->collect_nodes(in_rel, 1, false, false);2531this->collect_nodes(out_rel, -1, false, false);2532}2533#endif253425352536//=============================================================================2537const RegMask &JumpNode::out_RegMask() const {2538return RegMask::Empty;2539}25402541#ifndef PRODUCT2542//-----------------------------related-----------------------------------------2543// The related nodes of a JumpNode are all inputs at level 1, as well as the2544// outputs at level 2 (to include actual jump targets beyond projection nodes).2545// This is regardless of compact mode.2546void JumpNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {2547this->collect_nodes(in_rel, 1, false, false);2548this->collect_nodes(out_rel, -2, false, false);2549}2550#endif25512552//=============================================================================2553const RegMask &JProjNode::out_RegMask() const {2554return RegMask::Empty;2555}25562557//=============================================================================2558const RegMask &CProjNode::out_RegMask() const {2559return RegMask::Empty;2560}2561256225632564//=============================================================================25652566uint PCTableNode::hash() const { return Node::hash() + _size; }2567bool PCTableNode::cmp( const Node &n ) const2568{ return _size == ((PCTableNode&)n)._size; }25692570const Type *PCTableNode::bottom_type() const {2571const Type** f = TypeTuple::fields(_size);2572for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;2573return TypeTuple::make(_size, f);2574}25752576//------------------------------Value------------------------------------------2577// Compute the type of the PCTableNode. If reachable it is a tuple of2578// Control, otherwise the table targets are not reachable2579const Type* PCTableNode::Value(PhaseGVN* phase) const {2580if( phase->type(in(0)) == Type::CONTROL )2581return bottom_type();2582return Type::TOP; // All paths dead? Then so are we2583}25842585//------------------------------Ideal------------------------------------------2586// Return a node which is more "ideal" than the current node. Strip out2587// control copies2588Node *PCTableNode::Ideal(PhaseGVN *phase, bool can_reshape) {2589return remove_dead_region(phase, can_reshape) ? this : NULL;2590}25912592//=============================================================================2593uint JumpProjNode::hash() const {2594return Node::hash() + _dest_bci;2595}25962597bool JumpProjNode::cmp( const Node &n ) const {2598return ProjNode::cmp(n) &&2599_dest_bci == ((JumpProjNode&)n)._dest_bci;2600}26012602#ifndef PRODUCT2603void JumpProjNode::dump_spec(outputStream *st) const {2604ProjNode::dump_spec(st);2605st->print("@bci %d ",_dest_bci);2606}26072608void JumpProjNode::dump_compact_spec(outputStream *st) const {2609ProjNode::dump_compact_spec(st);2610st->print("(%d)%d@%d", _switch_val, _proj_no, _dest_bci);2611}26122613void JumpProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {2614// The related nodes of a JumpProjNode are its inputs and outputs at level 1.2615this->collect_nodes(in_rel, 1, false, false);2616this->collect_nodes(out_rel, -1, false, false);2617}2618#endif26192620//=============================================================================2621//------------------------------Value------------------------------------------2622// Check for being unreachable, or for coming from a Rethrow. Rethrow's cannot2623// have the default "fall_through_index" path.2624const Type* CatchNode::Value(PhaseGVN* phase) const {2625// Unreachable? Then so are all paths from here.2626if( phase->type(in(0)) == Type::TOP ) return Type::TOP;2627// First assume all paths are reachable2628const Type** f = TypeTuple::fields(_size);2629for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;2630// Identify cases that will always throw an exception2631// () rethrow call2632// () virtual or interface call with NULL receiver2633// () call is a check cast with incompatible arguments2634if( in(1)->is_Proj() ) {2635Node *i10 = in(1)->in(0);2636if( i10->is_Call() ) {2637CallNode *call = i10->as_Call();2638// Rethrows always throw exceptions, never return2639if (call->entry_point() == OptoRuntime::rethrow_stub()) {2640f[CatchProjNode::fall_through_index] = Type::TOP;2641} else if (call->is_AllocateArray()) {2642Node* klass_node = call->in(AllocateNode::KlassNode);2643Node* length = call->in(AllocateNode::ALength);2644const Type* length_type = phase->type(length);2645const Type* klass_type = phase->type(klass_node);2646Node* valid_length_test = call->in(AllocateNode::ValidLengthTest);2647const Type* valid_length_test_t = phase->type(valid_length_test);2648if (length_type == Type::TOP || klass_type == Type::TOP || valid_length_test_t == Type::TOP ||2649valid_length_test_t->is_int()->is_con(0)) {2650f[CatchProjNode::fall_through_index] = Type::TOP;2651}2652} else if( call->req() > TypeFunc::Parms ) {2653const Type *arg0 = phase->type( call->in(TypeFunc::Parms) );2654// Check for null receiver to virtual or interface calls2655if( call->is_CallDynamicJava() &&2656arg0->higher_equal(TypePtr::NULL_PTR) ) {2657f[CatchProjNode::fall_through_index] = Type::TOP;2658}2659} // End of if not a runtime stub2660} // End of if have call above me2661} // End of slot 1 is not a projection2662return TypeTuple::make(_size, f);2663}26642665//=============================================================================2666uint CatchProjNode::hash() const {2667return Node::hash() + _handler_bci;2668}266926702671bool CatchProjNode::cmp( const Node &n ) const {2672return ProjNode::cmp(n) &&2673_handler_bci == ((CatchProjNode&)n)._handler_bci;2674}267526762677//------------------------------Identity---------------------------------------2678// If only 1 target is possible, choose it if it is the main control2679Node* CatchProjNode::Identity(PhaseGVN* phase) {2680// If my value is control and no other value is, then treat as ID2681const TypeTuple *t = phase->type(in(0))->is_tuple();2682if (t->field_at(_con) != Type::CONTROL) return this;2683// If we remove the last CatchProj and elide the Catch/CatchProj, then we2684// also remove any exception table entry. Thus we must know the call2685// feeding the Catch will not really throw an exception. This is ok for2686// the main fall-thru control (happens when we know a call can never throw2687// an exception) or for "rethrow", because a further optimization will2688// yank the rethrow (happens when we inline a function that can throw an2689// exception and the caller has no handler). Not legal, e.g., for passing2690// a NULL receiver to a v-call, or passing bad types to a slow-check-cast.2691// These cases MUST throw an exception via the runtime system, so the VM2692// will be looking for a table entry.2693Node *proj = in(0)->in(1); // Expect a proj feeding CatchNode2694CallNode *call;2695if (_con != TypeFunc::Control && // Bail out if not the main control.2696!(proj->is_Proj() && // AND NOT a rethrow2697proj->in(0)->is_Call() &&2698(call = proj->in(0)->as_Call()) &&2699call->entry_point() == OptoRuntime::rethrow_stub()))2700return this;27012702// Search for any other path being control2703for (uint i = 0; i < t->cnt(); i++) {2704if (i != _con && t->field_at(i) == Type::CONTROL)2705return this;2706}2707// Only my path is possible; I am identity on control to the jump2708return in(0)->in(0);2709}271027112712#ifndef PRODUCT2713void CatchProjNode::dump_spec(outputStream *st) const {2714ProjNode::dump_spec(st);2715st->print("@bci %d ",_handler_bci);2716}2717#endif27182719//=============================================================================2720//------------------------------Identity---------------------------------------2721// Check for CreateEx being Identity.2722Node* CreateExNode::Identity(PhaseGVN* phase) {2723if( phase->type(in(1)) == Type::TOP ) return in(1);2724if( phase->type(in(0)) == Type::TOP ) return in(0);2725// We only come from CatchProj, unless the CatchProj goes away.2726// If the CatchProj is optimized away, then we just carry the2727// exception oop through.2728CallNode *call = in(1)->in(0)->as_Call();27292730return ( in(0)->is_CatchProj() && in(0)->in(0)->in(1) == in(1) )2731? this2732: call->in(TypeFunc::Parms);2733}27342735//=============================================================================2736//------------------------------Value------------------------------------------2737// Check for being unreachable.2738const Type* NeverBranchNode::Value(PhaseGVN* phase) const {2739if (!in(0) || in(0)->is_top()) return Type::TOP;2740return bottom_type();2741}27422743//------------------------------Ideal------------------------------------------2744// Check for no longer being part of a loop2745Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {2746if (can_reshape && !in(0)->is_Region()) {2747// Dead code elimination can sometimes delete this projection so2748// if it's not there, there's nothing to do.2749Node* fallthru = proj_out_or_null(0);2750if (fallthru != NULL) {2751phase->is_IterGVN()->replace_node(fallthru, in(0));2752}2753return phase->C->top();2754}2755return NULL;2756}27572758#ifndef PRODUCT2759void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {2760st->print("%s", Name());2761}2762#endif276327642765