Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/ifnode.cpp
32285 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 "gc_implementation/shenandoah/shenandoahHeap.hpp"26#include "memory/allocation.inline.hpp"27#include "opto/addnode.hpp"28#include "opto/cfgnode.hpp"29#include "opto/connode.hpp"30#include "opto/loopnode.hpp"31#include "opto/phaseX.hpp"32#include "opto/runtime.hpp"33#include "opto/subnode.hpp"3435// Portions of code courtesy of Clifford Click3637// Optimization - Graph Style383940extern int explicit_null_checks_elided;4142//=============================================================================43//------------------------------Value------------------------------------------44// Return a tuple for whichever arm of the IF is reachable45const Type *IfNode::Value( PhaseTransform *phase ) const {46if( !in(0) ) return Type::TOP;47if( phase->type(in(0)) == Type::TOP )48return Type::TOP;49const Type *t = phase->type(in(1));50if( t == Type::TOP ) // data is undefined51return TypeTuple::IFNEITHER; // unreachable altogether52if( t == TypeInt::ZERO ) // zero, or false53return TypeTuple::IFFALSE; // only false branch is reachable54if( t == TypeInt::ONE ) // 1, or true55return TypeTuple::IFTRUE; // only true branch is reachable56assert( t == TypeInt::BOOL, "expected boolean type" );5758return TypeTuple::IFBOTH; // No progress59}6061const RegMask &IfNode::out_RegMask() const {62return RegMask::Empty;63}6465//------------------------------split_if---------------------------------------66// Look for places where we merge constants, then test on the merged value.67// If the IF test will be constant folded on the path with the constant, we68// win by splitting the IF to before the merge point.69static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {70// I could be a lot more general here, but I'm trying to squeeze this71// in before the Christmas '98 break so I'm gonna be kinda restrictive72// on the patterns I accept. CNC7374// Look for a compare of a constant and a merged value75Node *i1 = iff->in(1);76if( !i1->is_Bool() ) return NULL;77BoolNode *b = i1->as_Bool();78Node *cmp = b->in(1);79if( !cmp->is_Cmp() ) return NULL;80i1 = cmp->in(1);81if( i1 == NULL || !i1->is_Phi() ) return NULL;82PhiNode *phi = i1->as_Phi();83if( phi->is_copy() ) return NULL;84Node *con2 = cmp->in(2);85if( !con2->is_Con() ) return NULL;86// See that the merge point contains some constants87Node *con1=NULL;88uint i4;89for( i4 = 1; i4 < phi->req(); i4++ ) {90con1 = phi->in(i4);91if( !con1 ) return NULL; // Do not optimize partially collapsed merges92if( con1->is_Con() ) break; // Found a constant93// Also allow null-vs-not-null checks94const TypePtr *tp = igvn->type(con1)->isa_ptr();95if( tp && tp->_ptr == TypePtr::NotNull )96break;97}98if( i4 >= phi->req() ) return NULL; // Found no constants99100igvn->C->set_has_split_ifs(true); // Has chance for split-if101102// Make sure that the compare can be constant folded away103Node *cmp2 = cmp->clone();104cmp2->set_req(1,con1);105cmp2->set_req(2,con2);106const Type *t = cmp2->Value(igvn);107// This compare is dead, so whack it!108igvn->remove_dead_node(cmp2);109if( !t->singleton() ) return NULL;110111// No intervening control, like a simple Call112Node *r = iff->in(0);113if( !r->is_Region() ) return NULL;114if( phi->region() != r ) return NULL;115// No other users of the cmp/bool116if (b->outcnt() != 1 || cmp->outcnt() != 1) {117//tty->print_cr("many users of cmp/bool");118return NULL;119}120121// Make sure we can determine where all the uses of merged values go122for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {123Node* u = r->fast_out(j);124if( u == r ) continue;125if( u == iff ) continue;126if( u->outcnt() == 0 ) continue; // use is dead & ignorable127if( !u->is_Phi() ) {128/*129if( u->is_Start() ) {130tty->print_cr("Region has inlined start use");131} else {132tty->print_cr("Region has odd use");133u->dump(2);134}*/135return NULL;136}137if( u != phi ) {138// CNC - do not allow any other merged value139//tty->print_cr("Merging another value");140//u->dump(2);141return NULL;142}143// Make sure we can account for all Phi uses144for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {145Node* v = u->fast_out(k); // User of the phi146// CNC - Allow only really simple patterns.147// In particular I disallow AddP of the Phi, a fairly common pattern148if( v == cmp ) continue; // The compare is OK149if( (v->is_ConstraintCast()) &&150v->in(0)->in(0) == iff )151continue; // CastPP/II of the IfNode is OK152// Disabled following code because I cannot tell if exactly one153// path dominates without a real dominator check. CNC 9/9/1999154//uint vop = v->Opcode();155//if( vop == Op_Phi ) { // Phi from another merge point might be OK156// Node *r = v->in(0); // Get controlling point157// if( !r ) return NULL; // Degraded to a copy158// // Find exactly one path in (either True or False doms, but not IFF)159// int cnt = 0;160// for( uint i = 1; i < r->req(); i++ )161// if( r->in(i) && r->in(i)->in(0) == iff )162// cnt++;163// if( cnt == 1 ) continue; // Exactly one of True or False guards Phi164//}165if( !v->is_Call() ) {166/*167if( v->Opcode() == Op_AddP ) {168tty->print_cr("Phi has AddP use");169} else if( v->Opcode() == Op_CastPP ) {170tty->print_cr("Phi has CastPP use");171} else if( v->Opcode() == Op_CastII ) {172tty->print_cr("Phi has CastII use");173} else {174tty->print_cr("Phi has use I cant be bothered with");175}176*/177}178return NULL;179180/* CNC - Cut out all the fancy acceptance tests181// Can we clone this use when doing the transformation?182// If all uses are from Phis at this merge or constants, then YES.183if( !v->in(0) && v != cmp ) {184tty->print_cr("Phi has free-floating use");185v->dump(2);186return NULL;187}188for( uint l = 1; l < v->req(); l++ ) {189if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&190!v->in(l)->is_Con() ) {191tty->print_cr("Phi has use");192v->dump(2);193return NULL;194} // End of if Phi-use input is neither Phi nor Constant195} // End of for all inputs to Phi-use196*/197} // End of for all uses of Phi198} // End of for all uses of Region199200// Only do this if the IF node is in a sane state201if (iff->outcnt() != 2)202return NULL;203204// Got a hit! Do the Mondo Hack!205//206//ABC a1c def ghi B 1 e h A C a c d f g i207// R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi208// cmp - 2 cmp - 2 cmp - 2209// bool bool_c bool_x210// if if_c if_x211// T F T F T F212// ..s.. ..t .. ..s.. ..t.. ..s.. ..t..213//214// Split the paths coming into the merge point into 2 separate groups of215// merges. On the left will be all the paths feeding constants into the216// Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi217// will fold up into a constant; this will let the Cmp fold up as well as218// all the control flow. Below the original IF we have 2 control219// dependent regions, 's' and 't'. Now we will merge the two paths220// just prior to 's' and 't' from the two IFs. At least 1 path (and quite221// likely 2 or more) will promptly constant fold away.222PhaseGVN *phase = igvn;223224// Make a region merging constants and a region merging the rest225uint req_c = 0;226Node* predicate_proj = NULL;227for (uint ii = 1; ii < r->req(); ii++) {228if (phi->in(ii) == con1) {229req_c++;230}231Node* proj = PhaseIdealLoop::find_predicate(r->in(ii));232if (proj != NULL) {233assert(predicate_proj == NULL, "only one predicate entry expected");234predicate_proj = proj;235}236}237238// If all the defs of the phi are the same constant, we already have the desired end state.239// Skip the split that would create empty phi and region nodes.240if((r->req() - req_c) == 1) {241return NULL;242}243244Node* predicate_c = NULL;245Node* predicate_x = NULL;246bool counted_loop = r->is_CountedLoop();247248Node *region_c = new (igvn->C) RegionNode(req_c + 1);249Node *phi_c = con1;250uint len = r->req();251Node *region_x = new (igvn->C) RegionNode(len - req_c);252Node *phi_x = PhiNode::make_blank(region_x, phi);253for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {254if (phi->in(i) == con1) {255region_c->init_req( i_c++, r ->in(i) );256if (r->in(i) == predicate_proj)257predicate_c = predicate_proj;258} else {259region_x->init_req( i_x, r ->in(i) );260phi_x ->init_req( i_x++, phi->in(i) );261if (r->in(i) == predicate_proj)262predicate_x = predicate_proj;263}264}265if (predicate_c != NULL && (req_c > 1)) {266assert(predicate_x == NULL, "only one predicate entry expected");267predicate_c = NULL; // Do not clone predicate below merge point268}269if (predicate_x != NULL && ((len - req_c) > 2)) {270assert(predicate_c == NULL, "only one predicate entry expected");271predicate_x = NULL; // Do not clone predicate below merge point272}273274// Register the new RegionNodes but do not transform them. Cannot275// transform until the entire Region/Phi conglomerate has been hacked276// as a single huge transform.277igvn->register_new_node_with_optimizer( region_c );278igvn->register_new_node_with_optimizer( region_x );279// Prevent the untimely death of phi_x. Currently he has no uses. He is280// about to get one. If this only use goes away, then phi_x will look dead.281// However, he will be picking up some more uses down below.282Node *hook = new (igvn->C) Node(4);283hook->init_req(0, phi_x);284hook->init_req(1, phi_c);285phi_x = phase->transform( phi_x );286287// Make the compare288Node *cmp_c = phase->makecon(t);289Node *cmp_x = cmp->clone();290cmp_x->set_req(1,phi_x);291cmp_x->set_req(2,con2);292cmp_x = phase->transform(cmp_x);293// Make the bool294Node *b_c = phase->transform(new (igvn->C) BoolNode(cmp_c,b->_test._test));295Node *b_x = phase->transform(new (igvn->C) BoolNode(cmp_x,b->_test._test));296// Make the IfNode297IfNode *iff_c = new (igvn->C) IfNode(region_c,b_c,iff->_prob,iff->_fcnt);298igvn->set_type_bottom(iff_c);299igvn->_worklist.push(iff_c);300hook->init_req(2, iff_c);301302IfNode *iff_x = new (igvn->C) IfNode(region_x,b_x,iff->_prob, iff->_fcnt);303igvn->set_type_bottom(iff_x);304igvn->_worklist.push(iff_x);305hook->init_req(3, iff_x);306307// Make the true/false arms308Node *iff_c_t = phase->transform(new (igvn->C) IfTrueNode (iff_c));309Node *iff_c_f = phase->transform(new (igvn->C) IfFalseNode(iff_c));310if (predicate_c != NULL) {311assert(predicate_x == NULL, "only one predicate entry expected");312// Clone loop predicates to each path313iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);314iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);315}316Node *iff_x_t = phase->transform(new (igvn->C) IfTrueNode (iff_x));317Node *iff_x_f = phase->transform(new (igvn->C) IfFalseNode(iff_x));318if (predicate_x != NULL) {319assert(predicate_c == NULL, "only one predicate entry expected");320// Clone loop predicates to each path321iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);322iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);323}324325// Merge the TRUE paths326Node *region_s = new (igvn->C) RegionNode(3);327igvn->_worklist.push(region_s);328region_s->init_req(1, iff_c_t);329region_s->init_req(2, iff_x_t);330igvn->register_new_node_with_optimizer( region_s );331332// Merge the FALSE paths333Node *region_f = new (igvn->C) RegionNode(3);334igvn->_worklist.push(region_f);335region_f->init_req(1, iff_c_f);336region_f->init_req(2, iff_x_f);337igvn->register_new_node_with_optimizer( region_f );338339igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.340cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead341cmp->set_req(2,NULL);342// Check for all uses of the Phi and give them a new home.343// The 'cmp' got cloned, but CastPP/IIs need to be moved.344Node *phi_s = NULL; // do not construct unless needed345Node *phi_f = NULL; // do not construct unless needed346for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {347Node* v = phi->last_out(i2);// User of the phi348igvn->rehash_node_delayed(v); // Have to fixup other Phi users349uint vop = v->Opcode();350Node *proj = NULL;351if( vop == Op_Phi ) { // Remote merge point352Node *r = v->in(0);353for (uint i3 = 1; i3 < r->req(); i3++)354if (r->in(i3) && r->in(i3)->in(0) == iff) {355proj = r->in(i3);356break;357}358} else if( v->is_ConstraintCast() ) {359proj = v->in(0); // Controlling projection360} else {361assert( 0, "do not know how to handle this guy" );362}363364Node *proj_path_data, *proj_path_ctrl;365if( proj->Opcode() == Op_IfTrue ) {366if( phi_s == NULL ) {367// Only construct phi_s if needed, otherwise provides368// interfering use.369phi_s = PhiNode::make_blank(region_s,phi);370phi_s->init_req( 1, phi_c );371phi_s->init_req( 2, phi_x );372hook->add_req(phi_s);373phi_s = phase->transform(phi_s);374}375proj_path_data = phi_s;376proj_path_ctrl = region_s;377} else {378if( phi_f == NULL ) {379// Only construct phi_f if needed, otherwise provides380// interfering use.381phi_f = PhiNode::make_blank(region_f,phi);382phi_f->init_req( 1, phi_c );383phi_f->init_req( 2, phi_x );384hook->add_req(phi_f);385phi_f = phase->transform(phi_f);386}387proj_path_data = phi_f;388proj_path_ctrl = region_f;389}390391// Fixup 'v' for for the split392if( vop == Op_Phi ) { // Remote merge point393uint i;394for( i = 1; i < v->req(); i++ )395if( v->in(i) == phi )396break;397v->set_req(i, proj_path_data );398} else if( v->is_ConstraintCast() ) {399v->set_req(0, proj_path_ctrl );400v->set_req(1, proj_path_data );401} else402ShouldNotReachHere();403}404405// Now replace the original iff's True/False with region_s/region_t.406// This makes the original iff go dead.407for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) {408Node* p = iff->last_out(i3);409assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" );410Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f;411// Replace p with u412igvn->add_users_to_worklist(p);413for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) {414Node* x = p->last_out(l);415igvn->hash_delete(x);416uint uses_found = 0;417for( uint j = 0; j < x->req(); j++ ) {418if( x->in(j) == p ) {419x->set_req(j, u);420uses_found++;421}422}423l -= uses_found; // we deleted 1 or more copies of this edge424}425igvn->remove_dead_node(p);426}427428// Force the original merge dead429igvn->hash_delete(r);430// First, remove region's dead users.431for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) {432Node* u = r->last_out(l);433if( u == r ) {434r->set_req(0, NULL);435} else {436assert(u->outcnt() == 0, "only dead users");437igvn->remove_dead_node(u);438}439l -= 1;440}441igvn->remove_dead_node(r);442443// Now remove the bogus extra edges used to keep things alive444igvn->remove_dead_node( hook );445446// Must return either the original node (now dead) or a new node447// (Do not return a top here, since that would break the uniqueness of top.)448return new (igvn->C) ConINode(TypeInt::ZERO);449}450451//------------------------------is_range_check---------------------------------452// Return 0 if not a range check. Return 1 if a range check and set index and453// offset. Return 2 if we had to negate the test. Index is NULL if the check454// is versus a constant.455int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {456if (outcnt() != 2) {457return 0;458}459Node* b = in(1);460if (b == NULL || !b->is_Bool()) return 0;461BoolNode* bn = b->as_Bool();462Node* cmp = bn->in(1);463if (cmp == NULL) return 0;464if (cmp->Opcode() != Op_CmpU) return 0;465466Node* l = cmp->in(1);467Node* r = cmp->in(2);468int flip_test = 1;469if (bn->_test._test == BoolTest::le) {470l = cmp->in(2);471r = cmp->in(1);472flip_test = 2;473} else if (bn->_test._test != BoolTest::lt) {474return 0;475}476if (l->is_top()) return 0; // Top input means dead test477if (r->Opcode() != Op_LoadRange) return 0;478479// We have recognized one of these forms:480// Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...481// Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...482483// Make sure it's a real range check by requiring an uncommon trap484// along the OOB path. Otherwise, it's possible that the user wrote485// something which optimized to look like a range check but behaves486// in some other way.487Node* iftrap = proj_out(flip_test == 2 ? true : false);488bool found_trap = false;489if (iftrap != NULL) {490Node* u = iftrap->unique_ctrl_out();491if (u != NULL) {492// It could be a merge point (Region) for uncommon trap.493if (u->is_Region()) {494Node* c = u->unique_ctrl_out();495if (c != NULL) {496iftrap = u;497u = c;498}499}500if (u->in(0) == iftrap && u->is_CallStaticJava()) {501int req = u->as_CallStaticJava()->uncommon_trap_request();502if (Deoptimization::trap_request_reason(req) ==503Deoptimization::Reason_range_check) {504found_trap = true;505}506}507}508}509if (!found_trap) return 0; // sorry, no cigar510511// Look for index+offset form512Node* ind = l;513jint off = 0;514if (l->is_top()) {515return 0;516} else if (l->Opcode() == Op_AddI) {517if ((off = l->in(1)->find_int_con(0)) != 0) {518ind = l->in(2);519} else if ((off = l->in(2)->find_int_con(0)) != 0) {520ind = l->in(1);521}522} else if ((off = l->find_int_con(-1)) >= 0) {523// constant offset with no variable index524ind = NULL;525} else {526// variable index with no constant offset (or dead negative index)527off = 0;528}529530// Return all the values:531index = ind;532offset = off;533range = r;534return flip_test;535}536537//------------------------------adjust_check-----------------------------------538// Adjust (widen) a prior range check539static void adjust_check(Node* proj, Node* range, Node* index,540int flip, jint off_lo, PhaseIterGVN* igvn) {541PhaseGVN *gvn = igvn;542// Break apart the old check543Node *iff = proj->in(0);544Node *bol = iff->in(1);545if( bol->is_top() ) return; // In case a partially dead range check appears546// bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode547DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )548if( !bol->is_Bool() ) return;549550Node *cmp = bol->in(1);551// Compute a new check552Node *new_add = gvn->intcon(off_lo);553if( index ) {554new_add = off_lo ? gvn->transform(new (gvn->C) AddINode( index, new_add )) : index;555}556Node *new_cmp = (flip == 1)557? new (gvn->C) CmpUNode( new_add, range )558: new (gvn->C) CmpUNode( range, new_add );559new_cmp = gvn->transform(new_cmp);560// See if no need to adjust the existing check561if( new_cmp == cmp ) return;562// Else, adjust existing check563Node *new_bol = gvn->transform( new (gvn->C) BoolNode( new_cmp, bol->as_Bool()->_test._test ) );564igvn->rehash_node_delayed( iff );565iff->set_req_X( 1, new_bol, igvn );566}567568//------------------------------up_one_dom-------------------------------------569// Walk up the dominator tree one step. Return NULL at root or true570// complex merges. Skips through small diamonds.571Node* IfNode::up_one_dom(Node *curr, bool linear_only) {572Node *dom = curr->in(0);573if( !dom ) // Found a Region degraded to a copy?574return curr->nonnull_req(); // Skip thru it575576if( curr != dom ) // Normal walk up one step?577return dom;578579// Use linear_only if we are still parsing, since we cannot580// trust the regions to be fully filled in.581if (linear_only)582return NULL;583584if( dom->is_Root() )585return NULL;586587// Else hit a Region. Check for a loop header588if( dom->is_Loop() )589return dom->in(1); // Skip up thru loops590591// Check for small diamonds592Node *din1, *din2, *din3, *din4;593if( dom->req() == 3 && // 2-path merge point594(din1 = dom ->in(1)) && // Left path exists595(din2 = dom ->in(2)) && // Right path exists596(din3 = din1->in(0)) && // Left path up one597(din4 = din2->in(0)) ) { // Right path up one598if( din3->is_Call() && // Handle a slow-path call on either arm599(din3 = din3->in(0)) )600din3 = din3->in(0);601if( din4->is_Call() && // Handle a slow-path call on either arm602(din4 = din4->in(0)) )603din4 = din4->in(0);604if (din3 != NULL && din3 == din4 && din3->is_If()) // Regions not degraded to a copy605return din3; // Skip around diamonds606}607608// Give up the search at true merges609return NULL; // Dead loop? Or hit root?610}611612bool IfNode::is_shenandoah_marking_if(PhaseTransform *phase) const {613if (!UseShenandoahGC) {614return false;615}616617if (Opcode() != Op_If) {618return false;619}620621Node* bol = in(1);622assert(bol->is_Bool(), "");623Node* cmpx = bol->in(1);624if (bol->as_Bool()->_test._test == BoolTest::ne &&625cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) &&626cmpx->in(1)->in(1)->is_shenandoah_state_load() &&627cmpx->in(1)->in(2)->is_Con() &&628cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) {629return true;630}631632return false;633}634635636//------------------------------filtered_int_type--------------------------------637// Return a possibly more restrictive type for val based on condition control flow for an if638const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) {639assert(if_proj &&640(if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");641if (if_proj->in(0) && if_proj->in(0)->is_If()) {642IfNode* iff = if_proj->in(0)->as_If();643if (iff->in(1) && iff->in(1)->is_Bool()) {644BoolNode* bol = iff->in(1)->as_Bool();645if (bol->in(1) && bol->in(1)->is_Cmp()) {646const CmpNode* cmp = bol->in(1)->as_Cmp();647if (cmp->in(1) == val) {648const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();649if (cmp2_t != NULL) {650jint lo = cmp2_t->_lo;651jint hi = cmp2_t->_hi;652BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();653switch (msk) {654case BoolTest::ne:655// Can't refine type656return NULL;657case BoolTest::eq:658return cmp2_t;659case BoolTest::lt:660lo = TypeInt::INT->_lo;661if (hi - 1 < hi) {662hi = hi - 1;663}664break;665case BoolTest::le:666lo = TypeInt::INT->_lo;667break;668case BoolTest::gt:669if (lo + 1 > lo) {670lo = lo + 1;671}672hi = TypeInt::INT->_hi;673break;674case BoolTest::ge:675// lo unchanged676hi = TypeInt::INT->_hi;677break;678}679const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);680return rtn_t;681}682}683}684}685}686return NULL;687}688689//------------------------------fold_compares----------------------------690// See if a pair of CmpIs can be converted into a CmpU. In some cases691// the direction of this if is determined by the preceding if so it692// can be eliminate entirely. Given an if testing (CmpI n c) check693// for an immediately control dependent if that is testing (CmpI n c2)694// and has one projection leading to this if and the other projection695// leading to a region that merges one of this ifs control696// projections.697//698// If699// / |700// / |701// / |702// If |703// /\ |704// / \ |705// / \ |706// / Region707//708Node* IfNode::fold_compares(PhaseGVN* phase) {709if (Opcode() != Op_If) return NULL;710711Node* this_cmp = in(1)->in(1);712if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI &&713this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) {714Node* ctrl = in(0);715BoolNode* this_bool = in(1)->as_Bool();716Node* n = this_cmp->in(1);717int hi = this_cmp->in(2)->get_int();718if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 &&719ctrl->in(0)->is_If() &&720ctrl->in(0)->outcnt() == 2 &&721ctrl->in(0)->in(1)->is_Bool() &&722ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI &&723ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() &&724ctrl->in(0)->in(1)->in(1)->in(2) != phase->C->top() &&725ctrl->in(0)->in(1)->in(1)->in(1) == n) {726IfNode* dom_iff = ctrl->in(0)->as_If();727Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con);728if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() &&729this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) {730// Identify which proj goes to the region and which continues on731RegionNode* region = otherproj->unique_out()->as_Region();732Node* success = NULL;733Node* fail = NULL;734for (int i = 0; i < 2; i++) {735Node* proj = proj_out(i);736if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {737success = proj;738} else if (fail == NULL) {739fail = proj;740} else {741success = fail = NULL;742}743}744if (success != NULL && fail != NULL && !region->has_phi()) {745int lo = dom_iff->in(1)->in(1)->in(2)->get_int();746BoolNode* dom_bool = dom_iff->in(1)->as_Bool();747Node* dom_cmp = dom_bool->in(1);748const TypeInt* failtype = filtered_int_type(phase, n, ctrl);749if (failtype != NULL) {750const TypeInt* type2 = filtered_int_type(phase, n, fail);751if (type2 != NULL) {752failtype = failtype->join(type2)->is_int();753} else {754failtype = NULL;755}756}757758if (failtype != NULL &&759dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) {760int bound = failtype->_hi - failtype->_lo + 1;761if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) {762// Merge the two compares into a single unsigned compare by building (CmpU (n - lo) hi)763BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge;764Node* adjusted = phase->transform(new (phase->C) SubINode(n, phase->intcon(failtype->_lo)));765Node* newcmp = phase->transform(new (phase->C) CmpUNode(adjusted, phase->intcon(bound)));766Node* newbool = phase->transform(new (phase->C) BoolNode(newcmp, cond));767phase->is_IterGVN()->replace_input_of(dom_iff, 1, phase->intcon(ctrl->as_Proj()->_con));768phase->hash_delete(this);769set_req(1, newbool);770return this;771}772if (failtype->_lo > failtype->_hi) {773// previous if determines the result of this if so774// replace Bool with constant775phase->hash_delete(this);776set_req(1, phase->intcon(success->as_Proj()->_con));777return this;778}779}780}781}782}783}784return NULL;785}786787//------------------------------remove_useless_bool----------------------------788// Check for people making a useless boolean: things like789// if( (x < y ? true : false) ) { ... }790// Replace with if( x < y ) { ... }791static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {792Node *i1 = iff->in(1);793if( !i1->is_Bool() ) return NULL;794BoolNode *bol = i1->as_Bool();795796Node *cmp = bol->in(1);797if( cmp->Opcode() != Op_CmpI ) return NULL;798799// Must be comparing against a bool800const Type *cmp2_t = phase->type( cmp->in(2) );801if( cmp2_t != TypeInt::ZERO &&802cmp2_t != TypeInt::ONE )803return NULL;804805// Find a prior merge point merging the boolean806i1 = cmp->in(1);807if( !i1->is_Phi() ) return NULL;808PhiNode *phi = i1->as_Phi();809if( phase->type( phi ) != TypeInt::BOOL )810return NULL;811812// Check for diamond pattern813int true_path = phi->is_diamond_phi();814if( true_path == 0 ) return NULL;815816// Make sure that iff and the control of the phi are different. This817// should really only happen for dead control flow since it requires818// an illegal cycle.819if (phi->in(0)->in(1)->in(0) == iff) return NULL;820821// phi->region->if_proj->ifnode->bool->cmp822BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();823824// Now get the 'sense' of the test correct so we can plug in825// either iff2->in(1) or its complement.826int flip = 0;827if( bol->_test._test == BoolTest::ne ) flip = 1-flip;828else if( bol->_test._test != BoolTest::eq ) return NULL;829if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;830831const Type *phi1_t = phase->type( phi->in(1) );832const Type *phi2_t = phase->type( phi->in(2) );833// Check for Phi(0,1) and flip834if( phi1_t == TypeInt::ZERO ) {835if( phi2_t != TypeInt::ONE ) return NULL;836flip = 1-flip;837} else {838// Check for Phi(1,0)839if( phi1_t != TypeInt::ONE ) return NULL;840if( phi2_t != TypeInt::ZERO ) return NULL;841}842if( true_path == 2 ) {843flip = 1-flip;844}845846Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);847assert(new_bol != iff->in(1), "must make progress");848iff->set_req(1, new_bol);849// Intervening diamond probably goes dead850phase->C->set_major_progress();851return iff;852}853854static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);855856struct RangeCheck {857Node* ctl;858jint off;859};860861//------------------------------Ideal------------------------------------------862// Return a node which is more "ideal" than the current node. Strip out863// control copies864Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {865if (remove_dead_region(phase, can_reshape)) return this;866// No Def-Use info?867if (!can_reshape) return NULL;868PhaseIterGVN *igvn = phase->is_IterGVN();869870// Don't bother trying to transform a dead if871if (in(0)->is_top()) return NULL;872// Don't bother trying to transform an if with a dead test873if (in(1)->is_top()) return NULL;874// Another variation of a dead test875if (in(1)->is_Con()) return NULL;876// Another variation of a dead if877if (outcnt() < 2) return NULL;878879// Canonicalize the test.880Node* idt_if = idealize_test(phase, this);881if (idt_if != NULL) return idt_if;882883// Try to split the IF884Node *s = split_if(this, igvn);885if (s != NULL) return s;886887// Check for people making a useless boolean: things like888// if( (x < y ? true : false) ) { ... }889// Replace with if( x < y ) { ... }890Node *bol2 = remove_useless_bool(this, phase);891if( bol2 ) return bol2;892893// Setup to scan up the CFG looking for a dominating test894Node *dom = in(0);895Node *prev_dom = this;896897// Check for range-check vs other kinds of tests898Node *index1, *range1;899jint offset1;900int flip1 = is_range_check(range1, index1, offset1);901if( flip1 ) {902// Try to remove extra range checks. All 'up_one_dom' gives up at merges903// so all checks we inspect post-dominate the top-most check we find.904// If we are going to fail the current check and we reach the top check905// then we are guaranteed to fail, so just start interpreting there.906// We 'expand' the top 3 range checks to include all post-dominating907// checks.908909// The top 3 range checks seen910const int NRC =3;911RangeCheck prev_checks[NRC];912int nb_checks = 0;913914// Low and high offsets seen so far915jint off_lo = offset1;916jint off_hi = offset1;917918bool found_immediate_dominator = false;919920// Scan for the top checks and collect range of offsets921for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit922if (dom->Opcode() == Op_If && // Not same opcode?923prev_dom->in(0) == dom) { // One path of test does dominate?924if (dom == this) return NULL; // dead loop925// See if this is a range check926Node *index2, *range2;927jint offset2;928int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);929// See if this is a _matching_ range check, checking against930// the same array bounds.931if (flip2 == flip1 && range2 == range1 && index2 == index1 &&932dom->outcnt() == 2) {933if (nb_checks == 0 && dom->in(1) == in(1)) {934// Found an immediately dominating test at the same offset.935// This kind of back-to-back test can be eliminated locally,936// and there is no need to search further for dominating tests.937assert(offset2 == offset1, "Same test but different offsets");938found_immediate_dominator = true;939break;940}941// Gather expanded bounds942off_lo = MIN2(off_lo,offset2);943off_hi = MAX2(off_hi,offset2);944// Record top NRC range checks945prev_checks[nb_checks%NRC].ctl = prev_dom;946prev_checks[nb_checks%NRC].off = offset2;947nb_checks++;948}949}950prev_dom = dom;951dom = up_one_dom(dom);952if (!dom) break;953}954955if (!found_immediate_dominator) {956// Attempt to widen the dominating range check to cover some later957// ones. Since range checks "fail" by uncommon-trapping to the958// interpreter, widening a check can make us speculatively enter959// the interpreter. If we see range-check deopt's, do not widen!960if (!phase->C->allow_range_check_smearing()) return NULL;961962// Didn't find prior covering check, so cannot remove anything.963if (nb_checks == 0) {964return NULL;965}966// Constant indices only need to check the upper bound.967// Non-constant indices must check both low and high.968int chk0 = (nb_checks - 1) % NRC;969if (index1) {970if (nb_checks == 1) {971return NULL;972} else {973// If the top range check's constant is the min or max of974// all constants we widen the next one to cover the whole975// range of constants.976RangeCheck rc0 = prev_checks[chk0];977int chk1 = (nb_checks - 2) % NRC;978RangeCheck rc1 = prev_checks[chk1];979if (rc0.off == off_lo) {980adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);981prev_dom = rc1.ctl;982} else if (rc0.off == off_hi) {983adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);984prev_dom = rc1.ctl;985} else {986// If the top test's constant is not the min or max of all987// constants, we need 3 range checks. We must leave the988// top test unchanged because widening it would allow the989// accesses it protects to successfully read/write out of990// bounds.991if (nb_checks == 2) {992return NULL;993}994int chk2 = (nb_checks - 3) % NRC;995RangeCheck rc2 = prev_checks[chk2];996// The top range check a+i covers interval: -a <= i < length-a997// The second range check b+i covers interval: -b <= i < length-b998if (rc1.off <= rc0.off) {999// if b <= a, we change the second range check to:1000// -min_of_all_constants <= i < length-min_of_all_constants1001// Together top and second range checks now cover:1002// -min_of_all_constants <= i < length-a1003// which is more restrictive than -b <= i < length-b:1004// -b <= -min_of_all_constants <= i < length-a <= length-b1005// The third check is then changed to:1006// -max_of_all_constants <= i < length-max_of_all_constants1007// so 2nd and 3rd checks restrict allowed values of i to:1008// -min_of_all_constants <= i < length-max_of_all_constants1009adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);1010adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);1011} else {1012// if b > a, we change the second range check to:1013// -max_of_all_constants <= i < length-max_of_all_constants1014// Together top and second range checks now cover:1015// -a <= i < length-max_of_all_constants1016// which is more restrictive than -b <= i < length-b:1017// -b < -a <= i < length-max_of_all_constants <= length-b1018// The third check is then changed to:1019// -max_of_all_constants <= i < length-max_of_all_constants1020// so 2nd and 3rd checks restrict allowed values of i to:1021// -min_of_all_constants <= i < length-max_of_all_constants1022adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);1023adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);1024}1025prev_dom = rc2.ctl;1026}1027}1028} else {1029RangeCheck rc0 = prev_checks[chk0];1030// 'Widen' the offset of the 1st and only covering check1031adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);1032// Test is now covered by prior checks, dominate it out1033prev_dom = rc0.ctl;1034}1035}10361037} else { // Scan for an equivalent test10381039Node *cmp;1040int dist = 0; // Cutoff limit for search1041int op = Opcode();1042if( op == Op_If &&1043(cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {1044if( cmp->in(2) != NULL && // make sure cmp is not already dead1045cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {1046dist = 64; // Limit for null-pointer scans1047} else {1048dist = 4; // Do not bother for random pointer tests1049}1050} else {1051dist = 4; // Limit for random junky scans1052}10531054// Normal equivalent-test check.1055if( !dom ) return NULL; // Dead loop?10561057Node* result = fold_compares(phase);1058if (result != NULL) {1059return result;1060}10611062// Search up the dominator tree for an If with an identical test1063while( dom->Opcode() != op || // Not same opcode?1064dom->in(1) != in(1) || // Not same input 1?1065(req() == 3 && dom->in(2) != in(2)) || // Not same input 2?1066prev_dom->in(0) != dom ) { // One path of test does not dominate?1067if( dist < 0 ) return NULL;10681069dist--;1070prev_dom = dom;1071dom = up_one_dom( dom );1072if( !dom ) return NULL;1073}10741075// Check that we did not follow a loop back to ourselves1076if( this == dom )1077return NULL;10781079if( dist > 2 ) // Add to count of NULL checks elided1080explicit_null_checks_elided++;10811082} // End of Else scan for an equivalent test10831084// Hit! Remove this IF1085#ifndef PRODUCT1086if( TraceIterativeGVN ) {1087tty->print(" Removing IfNode: "); this->dump();1088}1089if( VerifyOpto && !phase->allow_progress() ) {1090// Found an equivalent dominating test,1091// we can not guarantee reaching a fix-point for these during iterativeGVN1092// since intervening nodes may not change.1093return NULL;1094}1095#endif10961097// Replace dominated IfNode1098dominated_by( prev_dom, igvn );10991100// Must return either the original node (now dead) or a new node1101// (Do not return a top here, since that would break the uniqueness of top.)1102return new (phase->C) ConINode(TypeInt::ZERO);1103}11041105//------------------------------dominated_by-----------------------------------1106void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {1107igvn->hash_delete(this); // Remove self to prevent spurious V-N1108Node *idom = in(0);1109// Need opcode to decide which way 'this' test goes1110int prev_op = prev_dom->Opcode();1111Node *top = igvn->C->top(); // Shortcut to top11121113// Loop predicates may have depending checks which should not1114// be skipped. For example, range check predicate has two checks1115// for lower and upper bounds.1116ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();1117if ((unc_proj != NULL) && (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate))) {1118prev_dom = idom;1119}11201121// Now walk the current IfNode's projections.1122// Loop ends when 'this' has no more uses.1123for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {1124Node *ifp = last_out(i); // Get IfTrue/IfFalse1125igvn->add_users_to_worklist(ifp);1126// Check which projection it is and set target.1127// Data-target is either the dominating projection of the same type1128// or TOP if the dominating projection is of opposite type.1129// Data-target will be used as the new control edge for the non-CFG1130// nodes like Casts and Loads.1131Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;1132// Control-target is just the If's immediate dominator or TOP.1133Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top;11341135// For each child of an IfTrue/IfFalse projection, reroute.1136// Loop ends when projection has no more uses.1137for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {1138Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse1139if( !s->depends_only_on_test() ) {1140// Find the control input matching this def-use edge.1141// For Regions it may not be in slot 0.1142uint l;1143for( l = 0; s->in(l) != ifp; l++ ) { }1144igvn->replace_input_of(s, l, ctrl_target);1145} else { // Else, for control producers,1146igvn->replace_input_of(s, 0, data_target); // Move child to data-target1147}1148} // End for each child of a projection11491150igvn->remove_dead_node(ifp);1151} // End for each IfTrue/IfFalse child of If11521153// Kill the IfNode1154igvn->remove_dead_node(this);1155}11561157//------------------------------Identity---------------------------------------1158// If the test is constant & we match, then we are the input Control1159Node *IfTrueNode::Identity( PhaseTransform *phase ) {1160// Can only optimize if cannot go the other way1161const TypeTuple *t = phase->type(in(0))->is_tuple();1162return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE )1163? in(0)->in(0) // IfNode control1164: this; // no progress1165}11661167//------------------------------dump_spec--------------------------------------1168#ifndef PRODUCT1169void IfNode::dump_spec(outputStream *st) const {1170st->print("P=%f, C=%f",_prob,_fcnt);1171}1172#endif11731174//------------------------------idealize_test----------------------------------1175// Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and1176// come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms1177// converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as1178// needed.1179static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {1180assert(iff->in(0) != NULL, "If must be live");11811182if (iff->outcnt() != 2) return NULL; // Malformed projections.1183Node* old_if_f = iff->proj_out(false);1184Node* old_if_t = iff->proj_out(true);11851186// CountedLoopEnds want the back-control test to be TRUE, irregardless of1187// whether they are testing a 'gt' or 'lt' condition. The 'gt' condition1188// happens in count-down loops1189if (iff->is_CountedLoopEnd()) return NULL;1190if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests1191BoolNode *b = iff->in(1)->as_Bool();1192BoolTest bt = b->_test;1193// Test already in good order?1194if( bt.is_canonical() )1195return NULL;11961197// Flip test to be canonical. Requires flipping the IfFalse/IfTrue and1198// cloning the IfNode.1199Node* new_b = phase->transform( new (phase->C) BoolNode(b->in(1), bt.negate()) );1200if( !new_b->is_Bool() ) return NULL;1201b = new_b->as_Bool();12021203PhaseIterGVN *igvn = phase->is_IterGVN();1204assert( igvn, "Test is not canonical in parser?" );12051206// The IF node never really changes, but it needs to be cloned1207iff = new (phase->C) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);12081209Node *prior = igvn->hash_find_insert(iff);1210if( prior ) {1211igvn->remove_dead_node(iff);1212iff = (IfNode*)prior;1213} else {1214// Cannot call transform on it just yet1215igvn->set_type_bottom(iff);1216}1217igvn->_worklist.push(iff);12181219// Now handle projections. Cloning not required.1220Node* new_if_f = (Node*)(new (phase->C) IfFalseNode( iff ));1221Node* new_if_t = (Node*)(new (phase->C) IfTrueNode ( iff ));12221223igvn->register_new_node_with_optimizer(new_if_f);1224igvn->register_new_node_with_optimizer(new_if_t);1225// Flip test, so flip trailing control1226igvn->replace_node(old_if_f, new_if_t);1227igvn->replace_node(old_if_t, new_if_f);12281229// Progress1230return iff;1231}12321233//------------------------------Identity---------------------------------------1234// If the test is constant & we match, then we are the input Control1235Node *IfFalseNode::Identity( PhaseTransform *phase ) {1236// Can only optimize if cannot go the other way1237const TypeTuple *t = phase->type(in(0))->is_tuple();1238return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE )1239? in(0)->in(0) // IfNode control1240: this; // no progress1241}124212431244