Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/opto/ifnode.cpp
83404 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "opto/addnode.hpp"27#include "opto/cfgnode.hpp"28#include "opto/connode.hpp"29#include "opto/loopnode.hpp"30#include "opto/phaseX.hpp"31#include "opto/runtime.hpp"32#include "opto/subnode.hpp"3334// Portions of code courtesy of Clifford Click3536// Optimization - Graph Style373839extern int explicit_null_checks_elided;4041//=============================================================================42//------------------------------Value------------------------------------------43// Return a tuple for whichever arm of the IF is reachable44const Type *IfNode::Value( PhaseTransform *phase ) const {45if( !in(0) ) return Type::TOP;46if( phase->type(in(0)) == Type::TOP )47return Type::TOP;48const Type *t = phase->type(in(1));49if( t == Type::TOP ) // data is undefined50return TypeTuple::IFNEITHER; // unreachable altogether51if( t == TypeInt::ZERO ) // zero, or false52return TypeTuple::IFFALSE; // only false branch is reachable53if( t == TypeInt::ONE ) // 1, or true54return TypeTuple::IFTRUE; // only true branch is reachable55assert( t == TypeInt::BOOL, "expected boolean type" );5657return TypeTuple::IFBOTH; // No progress58}5960const RegMask &IfNode::out_RegMask() const {61return RegMask::Empty;62}6364//------------------------------split_if---------------------------------------65// Look for places where we merge constants, then test on the merged value.66// If the IF test will be constant folded on the path with the constant, we67// win by splitting the IF to before the merge point.68static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {69// I could be a lot more general here, but I'm trying to squeeze this70// in before the Christmas '98 break so I'm gonna be kinda restrictive71// on the patterns I accept. CNC7273// Look for a compare of a constant and a merged value74Node *i1 = iff->in(1);75if( !i1->is_Bool() ) return NULL;76BoolNode *b = i1->as_Bool();77Node *cmp = b->in(1);78if( !cmp->is_Cmp() ) return NULL;79i1 = cmp->in(1);80if( i1 == NULL || !i1->is_Phi() ) return NULL;81PhiNode *phi = i1->as_Phi();82if( phi->is_copy() ) return NULL;83Node *con2 = cmp->in(2);84if( !con2->is_Con() ) return NULL;85// See that the merge point contains some constants86Node *con1=NULL;87uint i4;88for( i4 = 1; i4 < phi->req(); i4++ ) {89con1 = phi->in(i4);90if( !con1 ) return NULL; // Do not optimize partially collapsed merges91if( con1->is_Con() ) break; // Found a constant92// Also allow null-vs-not-null checks93const TypePtr *tp = igvn->type(con1)->isa_ptr();94if( tp && tp->_ptr == TypePtr::NotNull )95break;96}97if( i4 >= phi->req() ) return NULL; // Found no constants9899igvn->C->set_has_split_ifs(true); // Has chance for split-if100101// Make sure that the compare can be constant folded away102Node *cmp2 = cmp->clone();103cmp2->set_req(1,con1);104cmp2->set_req(2,con2);105const Type *t = cmp2->Value(igvn);106// This compare is dead, so whack it!107igvn->remove_dead_node(cmp2);108if( !t->singleton() ) return NULL;109110// No intervening control, like a simple Call111Node *r = iff->in(0);112if( !r->is_Region() ) return NULL;113if( phi->region() != r ) return NULL;114// No other users of the cmp/bool115if (b->outcnt() != 1 || cmp->outcnt() != 1) {116//tty->print_cr("many users of cmp/bool");117return NULL;118}119120// Make sure we can determine where all the uses of merged values go121for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {122Node* u = r->fast_out(j);123if( u == r ) continue;124if( u == iff ) continue;125if( u->outcnt() == 0 ) continue; // use is dead & ignorable126if( !u->is_Phi() ) {127/*128if( u->is_Start() ) {129tty->print_cr("Region has inlined start use");130} else {131tty->print_cr("Region has odd use");132u->dump(2);133}*/134return NULL;135}136if( u != phi ) {137// CNC - do not allow any other merged value138//tty->print_cr("Merging another value");139//u->dump(2);140return NULL;141}142// Make sure we can account for all Phi uses143for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {144Node* v = u->fast_out(k); // User of the phi145// CNC - Allow only really simple patterns.146// In particular I disallow AddP of the Phi, a fairly common pattern147if( v == cmp ) continue; // The compare is OK148if( (v->is_ConstraintCast()) &&149v->in(0)->in(0) == iff )150continue; // CastPP/II of the IfNode is OK151// Disabled following code because I cannot tell if exactly one152// path dominates without a real dominator check. CNC 9/9/1999153//uint vop = v->Opcode();154//if( vop == Op_Phi ) { // Phi from another merge point might be OK155// Node *r = v->in(0); // Get controlling point156// if( !r ) return NULL; // Degraded to a copy157// // Find exactly one path in (either True or False doms, but not IFF)158// int cnt = 0;159// for( uint i = 1; i < r->req(); i++ )160// if( r->in(i) && r->in(i)->in(0) == iff )161// cnt++;162// if( cnt == 1 ) continue; // Exactly one of True or False guards Phi163//}164if( !v->is_Call() ) {165/*166if( v->Opcode() == Op_AddP ) {167tty->print_cr("Phi has AddP use");168} else if( v->Opcode() == Op_CastPP ) {169tty->print_cr("Phi has CastPP use");170} else if( v->Opcode() == Op_CastII ) {171tty->print_cr("Phi has CastII use");172} else {173tty->print_cr("Phi has use I cant be bothered with");174}175*/176}177return NULL;178179/* CNC - Cut out all the fancy acceptance tests180// Can we clone this use when doing the transformation?181// If all uses are from Phis at this merge or constants, then YES.182if( !v->in(0) && v != cmp ) {183tty->print_cr("Phi has free-floating use");184v->dump(2);185return NULL;186}187for( uint l = 1; l < v->req(); l++ ) {188if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&189!v->in(l)->is_Con() ) {190tty->print_cr("Phi has use");191v->dump(2);192return NULL;193} // End of if Phi-use input is neither Phi nor Constant194} // End of for all inputs to Phi-use195*/196} // End of for all uses of Phi197} // End of for all uses of Region198199// Only do this if the IF node is in a sane state200if (iff->outcnt() != 2)201return NULL;202203// Got a hit! Do the Mondo Hack!204//205//ABC a1c def ghi B 1 e h A C a c d f g i206// R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi207// cmp - 2 cmp - 2 cmp - 2208// bool bool_c bool_x209// if if_c if_x210// T F T F T F211// ..s.. ..t .. ..s.. ..t.. ..s.. ..t..212//213// Split the paths coming into the merge point into 2 separate groups of214// merges. On the left will be all the paths feeding constants into the215// Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi216// will fold up into a constant; this will let the Cmp fold up as well as217// all the control flow. Below the original IF we have 2 control218// dependent regions, 's' and 't'. Now we will merge the two paths219// just prior to 's' and 't' from the two IFs. At least 1 path (and quite220// likely 2 or more) will promptly constant fold away.221PhaseGVN *phase = igvn;222223// Make a region merging constants and a region merging the rest224uint req_c = 0;225Node* predicate_proj = NULL;226for (uint ii = 1; ii < r->req(); ii++) {227if (phi->in(ii) == con1) {228req_c++;229}230Node* proj = PhaseIdealLoop::find_predicate(r->in(ii));231if (proj != NULL) {232assert(predicate_proj == NULL, "only one predicate entry expected");233predicate_proj = proj;234}235}236237// If all the defs of the phi are the same constant, we already have the desired end state.238// Skip the split that would create empty phi and region nodes.239if((r->req() - req_c) == 1) {240return NULL;241}242243Node* predicate_c = NULL;244Node* predicate_x = NULL;245bool counted_loop = r->is_CountedLoop();246247Node *region_c = new (igvn->C) RegionNode(req_c + 1);248Node *phi_c = con1;249uint len = r->req();250Node *region_x = new (igvn->C) RegionNode(len - req_c);251Node *phi_x = PhiNode::make_blank(region_x, phi);252for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {253if (phi->in(i) == con1) {254region_c->init_req( i_c++, r ->in(i) );255if (r->in(i) == predicate_proj)256predicate_c = predicate_proj;257} else {258region_x->init_req( i_x, r ->in(i) );259phi_x ->init_req( i_x++, phi->in(i) );260if (r->in(i) == predicate_proj)261predicate_x = predicate_proj;262}263}264if (predicate_c != NULL && (req_c > 1)) {265assert(predicate_x == NULL, "only one predicate entry expected");266predicate_c = NULL; // Do not clone predicate below merge point267}268if (predicate_x != NULL && ((len - req_c) > 2)) {269assert(predicate_c == NULL, "only one predicate entry expected");270predicate_x = NULL; // Do not clone predicate below merge point271}272273// Register the new RegionNodes but do not transform them. Cannot274// transform until the entire Region/Phi conglomerate has been hacked275// as a single huge transform.276igvn->register_new_node_with_optimizer( region_c );277igvn->register_new_node_with_optimizer( region_x );278// Prevent the untimely death of phi_x. Currently he has no uses. He is279// about to get one. If this only use goes away, then phi_x will look dead.280// However, he will be picking up some more uses down below.281Node *hook = new (igvn->C) Node(4);282hook->init_req(0, phi_x);283hook->init_req(1, phi_c);284phi_x = phase->transform( phi_x );285286// Make the compare287Node *cmp_c = phase->makecon(t);288Node *cmp_x = cmp->clone();289cmp_x->set_req(1,phi_x);290cmp_x->set_req(2,con2);291cmp_x = phase->transform(cmp_x);292// Make the bool293Node *b_c = phase->transform(new (igvn->C) BoolNode(cmp_c,b->_test._test));294Node *b_x = phase->transform(new (igvn->C) BoolNode(cmp_x,b->_test._test));295// Make the IfNode296IfNode *iff_c = new (igvn->C) IfNode(region_c,b_c,iff->_prob,iff->_fcnt);297igvn->set_type_bottom(iff_c);298igvn->_worklist.push(iff_c);299hook->init_req(2, iff_c);300301IfNode *iff_x = new (igvn->C) IfNode(region_x,b_x,iff->_prob, iff->_fcnt);302igvn->set_type_bottom(iff_x);303igvn->_worklist.push(iff_x);304hook->init_req(3, iff_x);305306// Make the true/false arms307Node *iff_c_t = phase->transform(new (igvn->C) IfTrueNode (iff_c));308Node *iff_c_f = phase->transform(new (igvn->C) IfFalseNode(iff_c));309if (predicate_c != NULL) {310assert(predicate_x == NULL, "only one predicate entry expected");311// Clone loop predicates to each path312iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);313iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);314}315Node *iff_x_t = phase->transform(new (igvn->C) IfTrueNode (iff_x));316Node *iff_x_f = phase->transform(new (igvn->C) IfFalseNode(iff_x));317if (predicate_x != NULL) {318assert(predicate_c == NULL, "only one predicate entry expected");319// Clone loop predicates to each path320iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);321iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);322}323324// Merge the TRUE paths325Node *region_s = new (igvn->C) RegionNode(3);326igvn->_worklist.push(region_s);327region_s->init_req(1, iff_c_t);328region_s->init_req(2, iff_x_t);329igvn->register_new_node_with_optimizer( region_s );330331// Merge the FALSE paths332Node *region_f = new (igvn->C) RegionNode(3);333igvn->_worklist.push(region_f);334region_f->init_req(1, iff_c_f);335region_f->init_req(2, iff_x_f);336igvn->register_new_node_with_optimizer( region_f );337338igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.339cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead340cmp->set_req(2,NULL);341// Check for all uses of the Phi and give them a new home.342// The 'cmp' got cloned, but CastPP/IIs need to be moved.343Node *phi_s = NULL; // do not construct unless needed344Node *phi_f = NULL; // do not construct unless needed345for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {346Node* v = phi->last_out(i2);// User of the phi347igvn->rehash_node_delayed(v); // Have to fixup other Phi users348uint vop = v->Opcode();349Node *proj = NULL;350if( vop == Op_Phi ) { // Remote merge point351Node *r = v->in(0);352for (uint i3 = 1; i3 < r->req(); i3++)353if (r->in(i3) && r->in(i3)->in(0) == iff) {354proj = r->in(i3);355break;356}357} else if( v->is_ConstraintCast() ) {358proj = v->in(0); // Controlling projection359} else {360assert( 0, "do not know how to handle this guy" );361}362363Node *proj_path_data, *proj_path_ctrl;364if( proj->Opcode() == Op_IfTrue ) {365if( phi_s == NULL ) {366// Only construct phi_s if needed, otherwise provides367// interfering use.368phi_s = PhiNode::make_blank(region_s,phi);369phi_s->init_req( 1, phi_c );370phi_s->init_req( 2, phi_x );371hook->add_req(phi_s);372phi_s = phase->transform(phi_s);373}374proj_path_data = phi_s;375proj_path_ctrl = region_s;376} else {377if( phi_f == NULL ) {378// Only construct phi_f if needed, otherwise provides379// interfering use.380phi_f = PhiNode::make_blank(region_f,phi);381phi_f->init_req( 1, phi_c );382phi_f->init_req( 2, phi_x );383hook->add_req(phi_f);384phi_f = phase->transform(phi_f);385}386proj_path_data = phi_f;387proj_path_ctrl = region_f;388}389390// Fixup 'v' for for the split391if( vop == Op_Phi ) { // Remote merge point392uint i;393for( i = 1; i < v->req(); i++ )394if( v->in(i) == phi )395break;396v->set_req(i, proj_path_data );397} else if( v->is_ConstraintCast() ) {398v->set_req(0, proj_path_ctrl );399v->set_req(1, proj_path_data );400} else401ShouldNotReachHere();402}403404// Now replace the original iff's True/False with region_s/region_t.405// This makes the original iff go dead.406for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) {407Node* p = iff->last_out(i3);408assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" );409Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f;410// Replace p with u411igvn->add_users_to_worklist(p);412for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) {413Node* x = p->last_out(l);414igvn->hash_delete(x);415uint uses_found = 0;416for( uint j = 0; j < x->req(); j++ ) {417if( x->in(j) == p ) {418x->set_req(j, u);419uses_found++;420}421}422l -= uses_found; // we deleted 1 or more copies of this edge423}424igvn->remove_dead_node(p);425}426427// Force the original merge dead428igvn->hash_delete(r);429// First, remove region's dead users.430for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) {431Node* u = r->last_out(l);432if( u == r ) {433r->set_req(0, NULL);434} else {435assert(u->outcnt() == 0, "only dead users");436igvn->remove_dead_node(u);437}438l -= 1;439}440igvn->remove_dead_node(r);441442// Now remove the bogus extra edges used to keep things alive443igvn->remove_dead_node( hook );444445// Must return either the original node (now dead) or a new node446// (Do not return a top here, since that would break the uniqueness of top.)447return new (igvn->C) ConINode(TypeInt::ZERO);448}449450//------------------------------is_range_check---------------------------------451// Return 0 if not a range check. Return 1 if a range check and set index and452// offset. Return 2 if we had to negate the test. Index is NULL if the check453// is versus a constant.454int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {455if (outcnt() != 2) {456return 0;457}458Node* b = in(1);459if (b == NULL || !b->is_Bool()) return 0;460BoolNode* bn = b->as_Bool();461Node* cmp = bn->in(1);462if (cmp == NULL) return 0;463if (cmp->Opcode() != Op_CmpU) return 0;464465Node* l = cmp->in(1);466Node* r = cmp->in(2);467int flip_test = 1;468if (bn->_test._test == BoolTest::le) {469l = cmp->in(2);470r = cmp->in(1);471flip_test = 2;472} else if (bn->_test._test != BoolTest::lt) {473return 0;474}475if (l->is_top()) return 0; // Top input means dead test476if (r->Opcode() != Op_LoadRange) return 0;477478// We have recognized one of these forms:479// Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...480// Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...481482// Make sure it's a real range check by requiring an uncommon trap483// along the OOB path. Otherwise, it's possible that the user wrote484// something which optimized to look like a range check but behaves485// in some other way.486Node* iftrap = proj_out(flip_test == 2 ? true : false);487bool found_trap = false;488if (iftrap != NULL) {489Node* u = iftrap->unique_ctrl_out();490if (u != NULL) {491// It could be a merge point (Region) for uncommon trap.492if (u->is_Region()) {493Node* c = u->unique_ctrl_out();494if (c != NULL) {495iftrap = u;496u = c;497}498}499if (u->in(0) == iftrap && u->is_CallStaticJava()) {500int req = u->as_CallStaticJava()->uncommon_trap_request();501if (Deoptimization::trap_request_reason(req) ==502Deoptimization::Reason_range_check) {503found_trap = true;504}505}506}507}508if (!found_trap) return 0; // sorry, no cigar509510// Look for index+offset form511Node* ind = l;512jint off = 0;513if (l->is_top()) {514return 0;515} else if (l->Opcode() == Op_AddI) {516if ((off = l->in(1)->find_int_con(0)) != 0) {517ind = l->in(2);518} else if ((off = l->in(2)->find_int_con(0)) != 0) {519ind = l->in(1);520}521} else if ((off = l->find_int_con(-1)) >= 0) {522// constant offset with no variable index523ind = NULL;524} else {525// variable index with no constant offset (or dead negative index)526off = 0;527}528529// Return all the values:530index = ind;531offset = off;532range = r;533return flip_test;534}535536//------------------------------adjust_check-----------------------------------537// Adjust (widen) a prior range check538static void adjust_check(Node* proj, Node* range, Node* index,539int flip, jint off_lo, PhaseIterGVN* igvn) {540PhaseGVN *gvn = igvn;541// Break apart the old check542Node *iff = proj->in(0);543Node *bol = iff->in(1);544if( bol->is_top() ) return; // In case a partially dead range check appears545// bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode546DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )547if( !bol->is_Bool() ) return;548549Node *cmp = bol->in(1);550// Compute a new check551Node *new_add = gvn->intcon(off_lo);552if( index ) {553new_add = off_lo ? gvn->transform(new (gvn->C) AddINode( index, new_add )) : index;554}555Node *new_cmp = (flip == 1)556? new (gvn->C) CmpUNode( new_add, range )557: new (gvn->C) CmpUNode( range, new_add );558new_cmp = gvn->transform(new_cmp);559// See if no need to adjust the existing check560if( new_cmp == cmp ) return;561// Else, adjust existing check562Node *new_bol = gvn->transform( new (gvn->C) BoolNode( new_cmp, bol->as_Bool()->_test._test ) );563igvn->rehash_node_delayed( iff );564iff->set_req_X( 1, new_bol, igvn );565}566567//------------------------------up_one_dom-------------------------------------568// Walk up the dominator tree one step. Return NULL at root or true569// complex merges. Skips through small diamonds.570Node* IfNode::up_one_dom(Node *curr, bool linear_only) {571Node *dom = curr->in(0);572if( !dom ) // Found a Region degraded to a copy?573return curr->nonnull_req(); // Skip thru it574575if( curr != dom ) // Normal walk up one step?576return dom;577578// Use linear_only if we are still parsing, since we cannot579// trust the regions to be fully filled in.580if (linear_only)581return NULL;582583if( dom->is_Root() )584return NULL;585586// Else hit a Region. Check for a loop header587if( dom->is_Loop() )588return dom->in(1); // Skip up thru loops589590// Check for small diamonds591Node *din1, *din2, *din3, *din4;592if( dom->req() == 3 && // 2-path merge point593(din1 = dom ->in(1)) && // Left path exists594(din2 = dom ->in(2)) && // Right path exists595(din3 = din1->in(0)) && // Left path up one596(din4 = din2->in(0)) ) { // Right path up one597if( din3->is_Call() && // Handle a slow-path call on either arm598(din3 = din3->in(0)) )599din3 = din3->in(0);600if( din4->is_Call() && // Handle a slow-path call on either arm601(din4 = din4->in(0)) )602din4 = din4->in(0);603if (din3 != NULL && din3 == din4 && din3->is_If()) // Regions not degraded to a copy604return din3; // Skip around diamonds605}606607// Give up the search at true merges608return NULL; // Dead loop? Or hit root?609}610611612//------------------------------filtered_int_type--------------------------------613// Return a possibly more restrictive type for val based on condition control flow for an if614const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) {615assert(if_proj &&616(if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");617if (if_proj->in(0) && if_proj->in(0)->is_If()) {618IfNode* iff = if_proj->in(0)->as_If();619if (iff->in(1) && iff->in(1)->is_Bool()) {620BoolNode* bol = iff->in(1)->as_Bool();621if (bol->in(1) && bol->in(1)->is_Cmp()) {622const CmpNode* cmp = bol->in(1)->as_Cmp();623if (cmp->in(1) == val) {624const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();625if (cmp2_t != NULL) {626jint lo = cmp2_t->_lo;627jint hi = cmp2_t->_hi;628BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();629switch (msk) {630case BoolTest::ne:631// Can't refine type632return NULL;633case BoolTest::eq:634return cmp2_t;635case BoolTest::lt:636lo = TypeInt::INT->_lo;637if (hi - 1 < hi) {638hi = hi - 1;639}640break;641case BoolTest::le:642lo = TypeInt::INT->_lo;643break;644case BoolTest::gt:645if (lo + 1 > lo) {646lo = lo + 1;647}648hi = TypeInt::INT->_hi;649break;650case BoolTest::ge:651// lo unchanged652hi = TypeInt::INT->_hi;653break;654}655const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);656return rtn_t;657}658}659}660}661}662return NULL;663}664665//------------------------------fold_compares----------------------------666// See if a pair of CmpIs can be converted into a CmpU. In some cases667// the direction of this if is determined by the preceding if so it668// can be eliminate entirely. Given an if testing (CmpI n c) check669// for an immediately control dependent if that is testing (CmpI n c2)670// and has one projection leading to this if and the other projection671// leading to a region that merges one of this ifs control672// projections.673//674// If675// / |676// / |677// / |678// If |679// /\ |680// / \ |681// / \ |682// / Region683//684Node* IfNode::fold_compares(PhaseGVN* phase) {685if (Opcode() != Op_If) return NULL;686687Node* this_cmp = in(1)->in(1);688if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI &&689this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) {690Node* ctrl = in(0);691BoolNode* this_bool = in(1)->as_Bool();692Node* n = this_cmp->in(1);693int hi = this_cmp->in(2)->get_int();694if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 &&695ctrl->in(0)->is_If() &&696ctrl->in(0)->outcnt() == 2 &&697ctrl->in(0)->in(1)->is_Bool() &&698ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI &&699ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() &&700ctrl->in(0)->in(1)->in(1)->in(2) != phase->C->top() &&701ctrl->in(0)->in(1)->in(1)->in(1) == n) {702IfNode* dom_iff = ctrl->in(0)->as_If();703Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con);704if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() &&705this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) {706// Identify which proj goes to the region and which continues on707RegionNode* region = otherproj->unique_out()->as_Region();708Node* success = NULL;709Node* fail = NULL;710for (int i = 0; i < 2; i++) {711Node* proj = proj_out(i);712if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {713success = proj;714} else if (fail == NULL) {715fail = proj;716} else {717success = fail = NULL;718}719}720if (success != NULL && fail != NULL && !region->has_phi()) {721int lo = dom_iff->in(1)->in(1)->in(2)->get_int();722BoolNode* dom_bool = dom_iff->in(1)->as_Bool();723Node* dom_cmp = dom_bool->in(1);724const TypeInt* failtype = filtered_int_type(phase, n, ctrl);725if (failtype != NULL) {726const TypeInt* type2 = filtered_int_type(phase, n, fail);727if (type2 != NULL) {728failtype = failtype->join(type2)->is_int();729} else {730failtype = NULL;731}732}733734if (failtype != NULL &&735dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) {736int bound = failtype->_hi - failtype->_lo + 1;737if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) {738// Merge the two compares into a single unsigned compare by building (CmpU (n - lo) hi)739BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge;740Node* adjusted = phase->transform(new (phase->C) SubINode(n, phase->intcon(failtype->_lo)));741Node* newcmp = phase->transform(new (phase->C) CmpUNode(adjusted, phase->intcon(bound)));742Node* newbool = phase->transform(new (phase->C) BoolNode(newcmp, cond));743phase->is_IterGVN()->replace_input_of(dom_iff, 1, phase->intcon(ctrl->as_Proj()->_con));744phase->hash_delete(this);745set_req(1, newbool);746return this;747}748if (failtype->_lo > failtype->_hi) {749// previous if determines the result of this if so750// replace Bool with constant751phase->hash_delete(this);752set_req(1, phase->intcon(success->as_Proj()->_con));753return this;754}755}756}757}758}759}760return NULL;761}762763//------------------------------remove_useless_bool----------------------------764// Check for people making a useless boolean: things like765// if( (x < y ? true : false) ) { ... }766// Replace with if( x < y ) { ... }767static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {768Node *i1 = iff->in(1);769if( !i1->is_Bool() ) return NULL;770BoolNode *bol = i1->as_Bool();771772Node *cmp = bol->in(1);773if( cmp->Opcode() != Op_CmpI ) return NULL;774775// Must be comparing against a bool776const Type *cmp2_t = phase->type( cmp->in(2) );777if( cmp2_t != TypeInt::ZERO &&778cmp2_t != TypeInt::ONE )779return NULL;780781// Find a prior merge point merging the boolean782i1 = cmp->in(1);783if( !i1->is_Phi() ) return NULL;784PhiNode *phi = i1->as_Phi();785if( phase->type( phi ) != TypeInt::BOOL )786return NULL;787788// Check for diamond pattern789int true_path = phi->is_diamond_phi();790if( true_path == 0 ) return NULL;791792// Make sure that iff and the control of the phi are different. This793// should really only happen for dead control flow since it requires794// an illegal cycle.795if (phi->in(0)->in(1)->in(0) == iff) return NULL;796797// phi->region->if_proj->ifnode->bool->cmp798BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();799800// Now get the 'sense' of the test correct so we can plug in801// either iff2->in(1) or its complement.802int flip = 0;803if( bol->_test._test == BoolTest::ne ) flip = 1-flip;804else if( bol->_test._test != BoolTest::eq ) return NULL;805if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;806807const Type *phi1_t = phase->type( phi->in(1) );808const Type *phi2_t = phase->type( phi->in(2) );809// Check for Phi(0,1) and flip810if( phi1_t == TypeInt::ZERO ) {811if( phi2_t != TypeInt::ONE ) return NULL;812flip = 1-flip;813} else {814// Check for Phi(1,0)815if( phi1_t != TypeInt::ONE ) return NULL;816if( phi2_t != TypeInt::ZERO ) return NULL;817}818if( true_path == 2 ) {819flip = 1-flip;820}821822Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);823assert(new_bol != iff->in(1), "must make progress");824iff->set_req(1, new_bol);825// Intervening diamond probably goes dead826phase->C->set_major_progress();827return iff;828}829830static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);831832struct RangeCheck {833Node* ctl;834jint off;835};836837//------------------------------Ideal------------------------------------------838// Return a node which is more "ideal" than the current node. Strip out839// control copies840Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {841if (remove_dead_region(phase, can_reshape)) return this;842// No Def-Use info?843if (!can_reshape) return NULL;844PhaseIterGVN *igvn = phase->is_IterGVN();845846// Don't bother trying to transform a dead if847if (in(0)->is_top()) return NULL;848// Don't bother trying to transform an if with a dead test849if (in(1)->is_top()) return NULL;850// Another variation of a dead test851if (in(1)->is_Con()) return NULL;852// Another variation of a dead if853if (outcnt() < 2) return NULL;854855// Canonicalize the test.856Node* idt_if = idealize_test(phase, this);857if (idt_if != NULL) return idt_if;858859// Try to split the IF860Node *s = split_if(this, igvn);861if (s != NULL) return s;862863// Check for people making a useless boolean: things like864// if( (x < y ? true : false) ) { ... }865// Replace with if( x < y ) { ... }866Node *bol2 = remove_useless_bool(this, phase);867if( bol2 ) return bol2;868869// Setup to scan up the CFG looking for a dominating test870Node *dom = in(0);871Node *prev_dom = this;872873// Check for range-check vs other kinds of tests874Node *index1, *range1;875jint offset1;876int flip1 = is_range_check(range1, index1, offset1);877if( flip1 ) {878// Try to remove extra range checks. All 'up_one_dom' gives up at merges879// so all checks we inspect post-dominate the top-most check we find.880// If we are going to fail the current check and we reach the top check881// then we are guaranteed to fail, so just start interpreting there.882// We 'expand' the top 3 range checks to include all post-dominating883// checks.884885// The top 3 range checks seen886const int NRC =3;887RangeCheck prev_checks[NRC];888int nb_checks = 0;889890// Low and high offsets seen so far891jint off_lo = offset1;892jint off_hi = offset1;893894bool found_immediate_dominator = false;895896// Scan for the top checks and collect range of offsets897for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit898if (dom->Opcode() == Op_If && // Not same opcode?899prev_dom->in(0) == dom) { // One path of test does dominate?900if (dom == this) return NULL; // dead loop901// See if this is a range check902Node *index2, *range2;903jint offset2;904int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);905// See if this is a _matching_ range check, checking against906// the same array bounds.907if (flip2 == flip1 && range2 == range1 && index2 == index1 &&908dom->outcnt() == 2) {909if (nb_checks == 0 && dom->in(1) == in(1)) {910// Found an immediately dominating test at the same offset.911// This kind of back-to-back test can be eliminated locally,912// and there is no need to search further for dominating tests.913assert(offset2 == offset1, "Same test but different offsets");914found_immediate_dominator = true;915break;916}917// Gather expanded bounds918off_lo = MIN2(off_lo,offset2);919off_hi = MAX2(off_hi,offset2);920// Record top NRC range checks921prev_checks[nb_checks%NRC].ctl = prev_dom;922prev_checks[nb_checks%NRC].off = offset2;923nb_checks++;924}925}926prev_dom = dom;927dom = up_one_dom(dom);928if (!dom) break;929}930931if (!found_immediate_dominator) {932// Attempt to widen the dominating range check to cover some later933// ones. Since range checks "fail" by uncommon-trapping to the934// interpreter, widening a check can make us speculatively enter935// the interpreter. If we see range-check deopt's, do not widen!936if (!phase->C->allow_range_check_smearing()) return NULL;937938// Didn't find prior covering check, so cannot remove anything.939if (nb_checks == 0) {940return NULL;941}942// Constant indices only need to check the upper bound.943// Non-constant indices must check both low and high.944int chk0 = (nb_checks - 1) % NRC;945if (index1) {946if (nb_checks == 1) {947return NULL;948} else {949// If the top range check's constant is the min or max of950// all constants we widen the next one to cover the whole951// range of constants.952RangeCheck rc0 = prev_checks[chk0];953int chk1 = (nb_checks - 2) % NRC;954RangeCheck rc1 = prev_checks[chk1];955if (rc0.off == off_lo) {956adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);957prev_dom = rc1.ctl;958} else if (rc0.off == off_hi) {959adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);960prev_dom = rc1.ctl;961} else {962// If the top test's constant is not the min or max of all963// constants, we need 3 range checks. We must leave the964// top test unchanged because widening it would allow the965// accesses it protects to successfully read/write out of966// bounds.967if (nb_checks == 2) {968return NULL;969}970int chk2 = (nb_checks - 3) % NRC;971RangeCheck rc2 = prev_checks[chk2];972// The top range check a+i covers interval: -a <= i < length-a973// The second range check b+i covers interval: -b <= i < length-b974if (rc1.off <= rc0.off) {975// if b <= a, we change the second range check to:976// -min_of_all_constants <= i < length-min_of_all_constants977// Together top and second range checks now cover:978// -min_of_all_constants <= i < length-a979// which is more restrictive than -b <= i < length-b:980// -b <= -min_of_all_constants <= i < length-a <= length-b981// The third check is then changed to:982// -max_of_all_constants <= i < length-max_of_all_constants983// so 2nd and 3rd checks restrict allowed values of i to:984// -min_of_all_constants <= i < length-max_of_all_constants985adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);986adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);987} else {988// if b > a, we change the second range check to:989// -max_of_all_constants <= i < length-max_of_all_constants990// Together top and second range checks now cover:991// -a <= i < length-max_of_all_constants992// which is more restrictive than -b <= i < length-b:993// -b < -a <= i < length-max_of_all_constants <= length-b994// The third check is then changed to:995// -max_of_all_constants <= i < length-max_of_all_constants996// so 2nd and 3rd checks restrict allowed values of i to:997// -min_of_all_constants <= i < length-max_of_all_constants998adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);999adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);1000}1001prev_dom = rc2.ctl;1002}1003}1004} else {1005RangeCheck rc0 = prev_checks[chk0];1006// 'Widen' the offset of the 1st and only covering check1007adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);1008// Test is now covered by prior checks, dominate it out1009prev_dom = rc0.ctl;1010}1011}10121013} else { // Scan for an equivalent test10141015Node *cmp;1016int dist = 0; // Cutoff limit for search1017int op = Opcode();1018if( op == Op_If &&1019(cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {1020if( cmp->in(2) != NULL && // make sure cmp is not already dead1021cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {1022dist = 64; // Limit for null-pointer scans1023} else {1024dist = 4; // Do not bother for random pointer tests1025}1026} else {1027dist = 4; // Limit for random junky scans1028}10291030// Normal equivalent-test check.1031if( !dom ) return NULL; // Dead loop?10321033Node* result = fold_compares(phase);1034if (result != NULL) {1035return result;1036}10371038// Search up the dominator tree for an If with an identical test1039while( dom->Opcode() != op || // Not same opcode?1040dom->in(1) != in(1) || // Not same input 1?1041(req() == 3 && dom->in(2) != in(2)) || // Not same input 2?1042prev_dom->in(0) != dom ) { // One path of test does not dominate?1043if( dist < 0 ) return NULL;10441045dist--;1046prev_dom = dom;1047dom = up_one_dom( dom );1048if( !dom ) return NULL;1049}10501051// Check that we did not follow a loop back to ourselves1052if( this == dom )1053return NULL;10541055if( dist > 2 ) // Add to count of NULL checks elided1056explicit_null_checks_elided++;10571058} // End of Else scan for an equivalent test10591060// Hit! Remove this IF1061#ifndef PRODUCT1062if( TraceIterativeGVN ) {1063tty->print(" Removing IfNode: "); this->dump();1064}1065if( VerifyOpto && !phase->allow_progress() ) {1066// Found an equivalent dominating test,1067// we can not guarantee reaching a fix-point for these during iterativeGVN1068// since intervening nodes may not change.1069return NULL;1070}1071#endif10721073// Replace dominated IfNode1074dominated_by( prev_dom, igvn );10751076// Must return either the original node (now dead) or a new node1077// (Do not return a top here, since that would break the uniqueness of top.)1078return new (phase->C) ConINode(TypeInt::ZERO);1079}10801081//------------------------------dominated_by-----------------------------------1082void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {1083igvn->hash_delete(this); // Remove self to prevent spurious V-N1084Node *idom = in(0);1085// Need opcode to decide which way 'this' test goes1086int prev_op = prev_dom->Opcode();1087Node *top = igvn->C->top(); // Shortcut to top10881089// Loop predicates may have depending checks which should not1090// be skipped. For example, range check predicate has two checks1091// for lower and upper bounds.1092ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();1093if ((unc_proj != NULL) && (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate))) {1094prev_dom = idom;1095}10961097// Now walk the current IfNode's projections.1098// Loop ends when 'this' has no more uses.1099for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {1100Node *ifp = last_out(i); // Get IfTrue/IfFalse1101igvn->add_users_to_worklist(ifp);1102// Check which projection it is and set target.1103// Data-target is either the dominating projection of the same type1104// or TOP if the dominating projection is of opposite type.1105// Data-target will be used as the new control edge for the non-CFG1106// nodes like Casts and Loads.1107Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;1108// Control-target is just the If's immediate dominator or TOP.1109Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top;11101111// For each child of an IfTrue/IfFalse projection, reroute.1112// Loop ends when projection has no more uses.1113for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {1114Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse1115if( !s->depends_only_on_test() ) {1116// Find the control input matching this def-use edge.1117// For Regions it may not be in slot 0.1118uint l;1119for( l = 0; s->in(l) != ifp; l++ ) { }1120igvn->replace_input_of(s, l, ctrl_target);1121} else { // Else, for control producers,1122igvn->replace_input_of(s, 0, data_target); // Move child to data-target1123}1124} // End for each child of a projection11251126igvn->remove_dead_node(ifp);1127} // End for each IfTrue/IfFalse child of If11281129// Kill the IfNode1130igvn->remove_dead_node(this);1131}11321133//------------------------------Identity---------------------------------------1134// If the test is constant & we match, then we are the input Control1135Node *IfTrueNode::Identity( PhaseTransform *phase ) {1136// Can only optimize if cannot go the other way1137const TypeTuple *t = phase->type(in(0))->is_tuple();1138return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE )1139? in(0)->in(0) // IfNode control1140: this; // no progress1141}11421143//------------------------------dump_spec--------------------------------------1144#ifndef PRODUCT1145void IfNode::dump_spec(outputStream *st) const {1146st->print("P=%f, C=%f",_prob,_fcnt);1147}1148#endif11491150//------------------------------idealize_test----------------------------------1151// Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and1152// come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms1153// converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as1154// needed.1155static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {1156assert(iff->in(0) != NULL, "If must be live");11571158if (iff->outcnt() != 2) return NULL; // Malformed projections.1159Node* old_if_f = iff->proj_out(false);1160Node* old_if_t = iff->proj_out(true);11611162// CountedLoopEnds want the back-control test to be TRUE, irregardless of1163// whether they are testing a 'gt' or 'lt' condition. The 'gt' condition1164// happens in count-down loops1165if (iff->is_CountedLoopEnd()) return NULL;1166if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests1167BoolNode *b = iff->in(1)->as_Bool();1168BoolTest bt = b->_test;1169// Test already in good order?1170if( bt.is_canonical() )1171return NULL;11721173// Flip test to be canonical. Requires flipping the IfFalse/IfTrue and1174// cloning the IfNode.1175Node* new_b = phase->transform( new (phase->C) BoolNode(b->in(1), bt.negate()) );1176if( !new_b->is_Bool() ) return NULL;1177b = new_b->as_Bool();11781179PhaseIterGVN *igvn = phase->is_IterGVN();1180assert( igvn, "Test is not canonical in parser?" );11811182// The IF node never really changes, but it needs to be cloned1183iff = new (phase->C) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);11841185Node *prior = igvn->hash_find_insert(iff);1186if( prior ) {1187igvn->remove_dead_node(iff);1188iff = (IfNode*)prior;1189} else {1190// Cannot call transform on it just yet1191igvn->set_type_bottom(iff);1192}1193igvn->_worklist.push(iff);11941195// Now handle projections. Cloning not required.1196Node* new_if_f = (Node*)(new (phase->C) IfFalseNode( iff ));1197Node* new_if_t = (Node*)(new (phase->C) IfTrueNode ( iff ));11981199igvn->register_new_node_with_optimizer(new_if_f);1200igvn->register_new_node_with_optimizer(new_if_t);1201// Flip test, so flip trailing control1202igvn->replace_node(old_if_f, new_if_t);1203igvn->replace_node(old_if_t, new_if_f);12041205// Progress1206return iff;1207}12081209//------------------------------Identity---------------------------------------1210// If the test is constant & we match, then we are the input Control1211Node *IfFalseNode::Identity( PhaseTransform *phase ) {1212// Can only optimize if cannot go the other way1213const TypeTuple *t = phase->type(in(0))->is_tuple();1214return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE )1215? in(0)->in(0) // IfNode control1216: this; // no progress1217}121812191220