aoqi@0: /* aoqi@0: * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "opto/addnode.hpp" aoqi@0: #include "opto/cfgnode.hpp" aoqi@0: #include "opto/connode.hpp" aoqi@0: #include "opto/loopnode.hpp" aoqi@0: #include "opto/phaseX.hpp" aoqi@0: #include "opto/runtime.hpp" aoqi@0: #include "opto/subnode.hpp" aoqi@0: aoqi@0: // Portions of code courtesy of Clifford Click aoqi@0: aoqi@0: // Optimization - Graph Style aoqi@0: aoqi@0: aoqi@0: extern int explicit_null_checks_elided; aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: // Return a tuple for whichever arm of the IF is reachable aoqi@0: const Type *IfNode::Value( PhaseTransform *phase ) const { aoqi@0: if( !in(0) ) return Type::TOP; aoqi@0: if( phase->type(in(0)) == Type::TOP ) aoqi@0: return Type::TOP; aoqi@0: const Type *t = phase->type(in(1)); aoqi@0: if( t == Type::TOP ) // data is undefined aoqi@0: return TypeTuple::IFNEITHER; // unreachable altogether aoqi@0: if( t == TypeInt::ZERO ) // zero, or false aoqi@0: return TypeTuple::IFFALSE; // only false branch is reachable aoqi@0: if( t == TypeInt::ONE ) // 1, or true aoqi@0: return TypeTuple::IFTRUE; // only true branch is reachable aoqi@0: assert( t == TypeInt::BOOL, "expected boolean type" ); aoqi@0: aoqi@0: return TypeTuple::IFBOTH; // No progress aoqi@0: } aoqi@0: aoqi@0: const RegMask &IfNode::out_RegMask() const { aoqi@0: return RegMask::Empty; aoqi@0: } aoqi@0: aoqi@0: //------------------------------split_if--------------------------------------- aoqi@0: // Look for places where we merge constants, then test on the merged value. aoqi@0: // If the IF test will be constant folded on the path with the constant, we aoqi@0: // win by splitting the IF to before the merge point. aoqi@0: static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) { aoqi@0: // I could be a lot more general here, but I'm trying to squeeze this aoqi@0: // in before the Christmas '98 break so I'm gonna be kinda restrictive aoqi@0: // on the patterns I accept. CNC aoqi@0: aoqi@0: // Look for a compare of a constant and a merged value aoqi@0: Node *i1 = iff->in(1); aoqi@0: if( !i1->is_Bool() ) return NULL; aoqi@0: BoolNode *b = i1->as_Bool(); aoqi@0: Node *cmp = b->in(1); aoqi@0: if( !cmp->is_Cmp() ) return NULL; aoqi@0: i1 = cmp->in(1); aoqi@0: if( i1 == NULL || !i1->is_Phi() ) return NULL; aoqi@0: PhiNode *phi = i1->as_Phi(); aoqi@0: if( phi->is_copy() ) return NULL; aoqi@0: Node *con2 = cmp->in(2); aoqi@0: if( !con2->is_Con() ) return NULL; aoqi@0: // See that the merge point contains some constants aoqi@0: Node *con1=NULL; aoqi@0: uint i4; aoqi@0: for( i4 = 1; i4 < phi->req(); i4++ ) { aoqi@0: con1 = phi->in(i4); aoqi@0: if( !con1 ) return NULL; // Do not optimize partially collapsed merges aoqi@0: if( con1->is_Con() ) break; // Found a constant aoqi@0: // Also allow null-vs-not-null checks aoqi@0: const TypePtr *tp = igvn->type(con1)->isa_ptr(); aoqi@0: if( tp && tp->_ptr == TypePtr::NotNull ) aoqi@0: break; aoqi@0: } aoqi@0: if( i4 >= phi->req() ) return NULL; // Found no constants aoqi@0: aoqi@0: igvn->C->set_has_split_ifs(true); // Has chance for split-if aoqi@0: aoqi@0: // Make sure that the compare can be constant folded away aoqi@0: Node *cmp2 = cmp->clone(); aoqi@0: cmp2->set_req(1,con1); aoqi@0: cmp2->set_req(2,con2); aoqi@0: const Type *t = cmp2->Value(igvn); aoqi@0: // This compare is dead, so whack it! aoqi@0: igvn->remove_dead_node(cmp2); aoqi@0: if( !t->singleton() ) return NULL; aoqi@0: aoqi@0: // No intervening control, like a simple Call aoqi@0: Node *r = iff->in(0); aoqi@0: if( !r->is_Region() ) return NULL; aoqi@0: if( phi->region() != r ) return NULL; aoqi@0: // No other users of the cmp/bool aoqi@0: if (b->outcnt() != 1 || cmp->outcnt() != 1) { aoqi@0: //tty->print_cr("many users of cmp/bool"); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Make sure we can determine where all the uses of merged values go aoqi@0: for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) { aoqi@0: Node* u = r->fast_out(j); aoqi@0: if( u == r ) continue; aoqi@0: if( u == iff ) continue; aoqi@0: if( u->outcnt() == 0 ) continue; // use is dead & ignorable aoqi@0: if( !u->is_Phi() ) { aoqi@0: /* aoqi@0: if( u->is_Start() ) { aoqi@0: tty->print_cr("Region has inlined start use"); aoqi@0: } else { aoqi@0: tty->print_cr("Region has odd use"); aoqi@0: u->dump(2); aoqi@0: }*/ aoqi@0: return NULL; aoqi@0: } aoqi@0: if( u != phi ) { aoqi@0: // CNC - do not allow any other merged value aoqi@0: //tty->print_cr("Merging another value"); aoqi@0: //u->dump(2); aoqi@0: return NULL; aoqi@0: } aoqi@0: // Make sure we can account for all Phi uses aoqi@0: for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) { aoqi@0: Node* v = u->fast_out(k); // User of the phi aoqi@0: // CNC - Allow only really simple patterns. aoqi@0: // In particular I disallow AddP of the Phi, a fairly common pattern aoqi@0: if( v == cmp ) continue; // The compare is OK aoqi@0: if( (v->is_ConstraintCast()) && aoqi@0: v->in(0)->in(0) == iff ) aoqi@0: continue; // CastPP/II of the IfNode is OK aoqi@0: // Disabled following code because I cannot tell if exactly one aoqi@0: // path dominates without a real dominator check. CNC 9/9/1999 aoqi@0: //uint vop = v->Opcode(); aoqi@0: //if( vop == Op_Phi ) { // Phi from another merge point might be OK aoqi@0: // Node *r = v->in(0); // Get controlling point aoqi@0: // if( !r ) return NULL; // Degraded to a copy aoqi@0: // // Find exactly one path in (either True or False doms, but not IFF) aoqi@0: // int cnt = 0; aoqi@0: // for( uint i = 1; i < r->req(); i++ ) aoqi@0: // if( r->in(i) && r->in(i)->in(0) == iff ) aoqi@0: // cnt++; aoqi@0: // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi aoqi@0: //} aoqi@0: if( !v->is_Call() ) { aoqi@0: /* aoqi@0: if( v->Opcode() == Op_AddP ) { aoqi@0: tty->print_cr("Phi has AddP use"); aoqi@0: } else if( v->Opcode() == Op_CastPP ) { aoqi@0: tty->print_cr("Phi has CastPP use"); aoqi@0: } else if( v->Opcode() == Op_CastII ) { aoqi@0: tty->print_cr("Phi has CastII use"); aoqi@0: } else { aoqi@0: tty->print_cr("Phi has use I cant be bothered with"); aoqi@0: } aoqi@0: */ aoqi@0: } aoqi@0: return NULL; aoqi@0: aoqi@0: /* CNC - Cut out all the fancy acceptance tests aoqi@0: // Can we clone this use when doing the transformation? aoqi@0: // If all uses are from Phis at this merge or constants, then YES. aoqi@0: if( !v->in(0) && v != cmp ) { aoqi@0: tty->print_cr("Phi has free-floating use"); aoqi@0: v->dump(2); aoqi@0: return NULL; aoqi@0: } aoqi@0: for( uint l = 1; l < v->req(); l++ ) { aoqi@0: if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) && aoqi@0: !v->in(l)->is_Con() ) { aoqi@0: tty->print_cr("Phi has use"); aoqi@0: v->dump(2); aoqi@0: return NULL; aoqi@0: } // End of if Phi-use input is neither Phi nor Constant aoqi@0: } // End of for all inputs to Phi-use aoqi@0: */ aoqi@0: } // End of for all uses of Phi aoqi@0: } // End of for all uses of Region aoqi@0: aoqi@0: // Only do this if the IF node is in a sane state aoqi@0: if (iff->outcnt() != 2) aoqi@0: return NULL; aoqi@0: aoqi@0: // Got a hit! Do the Mondo Hack! aoqi@0: // aoqi@0: //ABC a1c def ghi B 1 e h A C a c d f g i aoqi@0: // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi aoqi@0: // cmp - 2 cmp - 2 cmp - 2 aoqi@0: // bool bool_c bool_x aoqi@0: // if if_c if_x aoqi@0: // T F T F T F aoqi@0: // ..s.. ..t .. ..s.. ..t.. ..s.. ..t.. aoqi@0: // aoqi@0: // Split the paths coming into the merge point into 2 separate groups of aoqi@0: // merges. On the left will be all the paths feeding constants into the aoqi@0: // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi aoqi@0: // will fold up into a constant; this will let the Cmp fold up as well as aoqi@0: // all the control flow. Below the original IF we have 2 control aoqi@0: // dependent regions, 's' and 't'. Now we will merge the two paths aoqi@0: // just prior to 's' and 't' from the two IFs. At least 1 path (and quite aoqi@0: // likely 2 or more) will promptly constant fold away. aoqi@0: PhaseGVN *phase = igvn; aoqi@0: aoqi@0: // Make a region merging constants and a region merging the rest aoqi@0: uint req_c = 0; aoqi@0: Node* predicate_proj = NULL; aoqi@0: for (uint ii = 1; ii < r->req(); ii++) { aoqi@0: if (phi->in(ii) == con1) { aoqi@0: req_c++; aoqi@0: } aoqi@0: Node* proj = PhaseIdealLoop::find_predicate(r->in(ii)); aoqi@0: if (proj != NULL) { aoqi@0: assert(predicate_proj == NULL, "only one predicate entry expected"); aoqi@0: predicate_proj = proj; aoqi@0: } aoqi@0: } aoqi@0: Node* predicate_c = NULL; aoqi@0: Node* predicate_x = NULL; aoqi@0: bool counted_loop = r->is_CountedLoop(); aoqi@0: aoqi@0: Node *region_c = new (igvn->C) RegionNode(req_c + 1); aoqi@0: Node *phi_c = con1; aoqi@0: uint len = r->req(); aoqi@0: Node *region_x = new (igvn->C) RegionNode(len - req_c); aoqi@0: Node *phi_x = PhiNode::make_blank(region_x, phi); aoqi@0: for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) { aoqi@0: if (phi->in(i) == con1) { aoqi@0: region_c->init_req( i_c++, r ->in(i) ); aoqi@0: if (r->in(i) == predicate_proj) aoqi@0: predicate_c = predicate_proj; aoqi@0: } else { aoqi@0: region_x->init_req( i_x, r ->in(i) ); aoqi@0: phi_x ->init_req( i_x++, phi->in(i) ); aoqi@0: if (r->in(i) == predicate_proj) aoqi@0: predicate_x = predicate_proj; aoqi@0: } aoqi@0: } aoqi@0: if (predicate_c != NULL && (req_c > 1)) { aoqi@0: assert(predicate_x == NULL, "only one predicate entry expected"); aoqi@0: predicate_c = NULL; // Do not clone predicate below merge point aoqi@0: } aoqi@0: if (predicate_x != NULL && ((len - req_c) > 2)) { aoqi@0: assert(predicate_c == NULL, "only one predicate entry expected"); aoqi@0: predicate_x = NULL; // Do not clone predicate below merge point aoqi@0: } aoqi@0: aoqi@0: // Register the new RegionNodes but do not transform them. Cannot aoqi@0: // transform until the entire Region/Phi conglomerate has been hacked aoqi@0: // as a single huge transform. aoqi@0: igvn->register_new_node_with_optimizer( region_c ); aoqi@0: igvn->register_new_node_with_optimizer( region_x ); aoqi@0: // Prevent the untimely death of phi_x. Currently he has no uses. He is aoqi@0: // about to get one. If this only use goes away, then phi_x will look dead. aoqi@0: // However, he will be picking up some more uses down below. aoqi@0: Node *hook = new (igvn->C) Node(4); aoqi@0: hook->init_req(0, phi_x); aoqi@0: hook->init_req(1, phi_c); aoqi@0: phi_x = phase->transform( phi_x ); aoqi@0: aoqi@0: // Make the compare aoqi@0: Node *cmp_c = phase->makecon(t); aoqi@0: Node *cmp_x = cmp->clone(); aoqi@0: cmp_x->set_req(1,phi_x); aoqi@0: cmp_x->set_req(2,con2); aoqi@0: cmp_x = phase->transform(cmp_x); aoqi@0: // Make the bool aoqi@0: Node *b_c = phase->transform(new (igvn->C) BoolNode(cmp_c,b->_test._test)); aoqi@0: Node *b_x = phase->transform(new (igvn->C) BoolNode(cmp_x,b->_test._test)); aoqi@0: // Make the IfNode aoqi@0: IfNode *iff_c = new (igvn->C) IfNode(region_c,b_c,iff->_prob,iff->_fcnt); aoqi@0: igvn->set_type_bottom(iff_c); aoqi@0: igvn->_worklist.push(iff_c); aoqi@0: hook->init_req(2, iff_c); aoqi@0: aoqi@0: IfNode *iff_x = new (igvn->C) IfNode(region_x,b_x,iff->_prob, iff->_fcnt); aoqi@0: igvn->set_type_bottom(iff_x); aoqi@0: igvn->_worklist.push(iff_x); aoqi@0: hook->init_req(3, iff_x); aoqi@0: aoqi@0: // Make the true/false arms aoqi@0: Node *iff_c_t = phase->transform(new (igvn->C) IfTrueNode (iff_c)); aoqi@0: Node *iff_c_f = phase->transform(new (igvn->C) IfFalseNode(iff_c)); aoqi@0: if (predicate_c != NULL) { aoqi@0: assert(predicate_x == NULL, "only one predicate entry expected"); aoqi@0: // Clone loop predicates to each path aoqi@0: iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop); aoqi@0: iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop); aoqi@0: } aoqi@0: Node *iff_x_t = phase->transform(new (igvn->C) IfTrueNode (iff_x)); aoqi@0: Node *iff_x_f = phase->transform(new (igvn->C) IfFalseNode(iff_x)); aoqi@0: if (predicate_x != NULL) { aoqi@0: assert(predicate_c == NULL, "only one predicate entry expected"); aoqi@0: // Clone loop predicates to each path aoqi@0: iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop); aoqi@0: iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop); aoqi@0: } aoqi@0: aoqi@0: // Merge the TRUE paths aoqi@0: Node *region_s = new (igvn->C) RegionNode(3); aoqi@0: igvn->_worklist.push(region_s); aoqi@0: region_s->init_req(1, iff_c_t); aoqi@0: region_s->init_req(2, iff_x_t); aoqi@0: igvn->register_new_node_with_optimizer( region_s ); aoqi@0: aoqi@0: // Merge the FALSE paths aoqi@0: Node *region_f = new (igvn->C) RegionNode(3); aoqi@0: igvn->_worklist.push(region_f); aoqi@0: region_f->init_req(1, iff_c_f); aoqi@0: region_f->init_req(2, iff_x_f); aoqi@0: igvn->register_new_node_with_optimizer( region_f ); aoqi@0: aoqi@0: igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table. aoqi@0: cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead aoqi@0: cmp->set_req(2,NULL); aoqi@0: // Check for all uses of the Phi and give them a new home. aoqi@0: // The 'cmp' got cloned, but CastPP/IIs need to be moved. aoqi@0: Node *phi_s = NULL; // do not construct unless needed aoqi@0: Node *phi_f = NULL; // do not construct unless needed aoqi@0: for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) { aoqi@0: Node* v = phi->last_out(i2);// User of the phi aoqi@0: igvn->rehash_node_delayed(v); // Have to fixup other Phi users aoqi@0: uint vop = v->Opcode(); aoqi@0: Node *proj = NULL; aoqi@0: if( vop == Op_Phi ) { // Remote merge point aoqi@0: Node *r = v->in(0); aoqi@0: for (uint i3 = 1; i3 < r->req(); i3++) aoqi@0: if (r->in(i3) && r->in(i3)->in(0) == iff) { aoqi@0: proj = r->in(i3); aoqi@0: break; aoqi@0: } aoqi@0: } else if( v->is_ConstraintCast() ) { aoqi@0: proj = v->in(0); // Controlling projection aoqi@0: } else { aoqi@0: assert( 0, "do not know how to handle this guy" ); aoqi@0: } aoqi@0: aoqi@0: Node *proj_path_data, *proj_path_ctrl; aoqi@0: if( proj->Opcode() == Op_IfTrue ) { aoqi@0: if( phi_s == NULL ) { aoqi@0: // Only construct phi_s if needed, otherwise provides aoqi@0: // interfering use. aoqi@0: phi_s = PhiNode::make_blank(region_s,phi); aoqi@0: phi_s->init_req( 1, phi_c ); aoqi@0: phi_s->init_req( 2, phi_x ); aoqi@0: hook->add_req(phi_s); aoqi@0: phi_s = phase->transform(phi_s); aoqi@0: } aoqi@0: proj_path_data = phi_s; aoqi@0: proj_path_ctrl = region_s; aoqi@0: } else { aoqi@0: if( phi_f == NULL ) { aoqi@0: // Only construct phi_f if needed, otherwise provides aoqi@0: // interfering use. aoqi@0: phi_f = PhiNode::make_blank(region_f,phi); aoqi@0: phi_f->init_req( 1, phi_c ); aoqi@0: phi_f->init_req( 2, phi_x ); aoqi@0: hook->add_req(phi_f); aoqi@0: phi_f = phase->transform(phi_f); aoqi@0: } aoqi@0: proj_path_data = phi_f; aoqi@0: proj_path_ctrl = region_f; aoqi@0: } aoqi@0: aoqi@0: // Fixup 'v' for for the split aoqi@0: if( vop == Op_Phi ) { // Remote merge point aoqi@0: uint i; aoqi@0: for( i = 1; i < v->req(); i++ ) aoqi@0: if( v->in(i) == phi ) aoqi@0: break; aoqi@0: v->set_req(i, proj_path_data ); aoqi@0: } else if( v->is_ConstraintCast() ) { aoqi@0: v->set_req(0, proj_path_ctrl ); aoqi@0: v->set_req(1, proj_path_data ); aoqi@0: } else aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: // Now replace the original iff's True/False with region_s/region_t. aoqi@0: // This makes the original iff go dead. aoqi@0: for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) { aoqi@0: Node* p = iff->last_out(i3); aoqi@0: assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" ); aoqi@0: Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f; aoqi@0: // Replace p with u aoqi@0: igvn->add_users_to_worklist(p); aoqi@0: for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) { aoqi@0: Node* x = p->last_out(l); aoqi@0: igvn->hash_delete(x); aoqi@0: uint uses_found = 0; aoqi@0: for( uint j = 0; j < x->req(); j++ ) { aoqi@0: if( x->in(j) == p ) { aoqi@0: x->set_req(j, u); aoqi@0: uses_found++; aoqi@0: } aoqi@0: } aoqi@0: l -= uses_found; // we deleted 1 or more copies of this edge aoqi@0: } aoqi@0: igvn->remove_dead_node(p); aoqi@0: } aoqi@0: aoqi@0: // Force the original merge dead aoqi@0: igvn->hash_delete(r); aoqi@0: // First, remove region's dead users. aoqi@0: for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) { aoqi@0: Node* u = r->last_out(l); aoqi@0: if( u == r ) { aoqi@0: r->set_req(0, NULL); aoqi@0: } else { aoqi@0: assert(u->outcnt() == 0, "only dead users"); aoqi@0: igvn->remove_dead_node(u); aoqi@0: } aoqi@0: l -= 1; aoqi@0: } aoqi@0: igvn->remove_dead_node(r); aoqi@0: aoqi@0: // Now remove the bogus extra edges used to keep things alive aoqi@0: igvn->remove_dead_node( hook ); aoqi@0: aoqi@0: // Must return either the original node (now dead) or a new node aoqi@0: // (Do not return a top here, since that would break the uniqueness of top.) aoqi@0: return new (igvn->C) ConINode(TypeInt::ZERO); aoqi@0: } aoqi@0: aoqi@0: //------------------------------is_range_check--------------------------------- aoqi@0: // Return 0 if not a range check. Return 1 if a range check and set index and aoqi@0: // offset. Return 2 if we had to negate the test. Index is NULL if the check aoqi@0: // is versus a constant. aoqi@0: int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) { aoqi@0: Node* b = in(1); aoqi@0: if (b == NULL || !b->is_Bool()) return 0; aoqi@0: BoolNode* bn = b->as_Bool(); aoqi@0: Node* cmp = bn->in(1); aoqi@0: if (cmp == NULL) return 0; aoqi@0: if (cmp->Opcode() != Op_CmpU) return 0; aoqi@0: aoqi@0: Node* l = cmp->in(1); aoqi@0: Node* r = cmp->in(2); aoqi@0: int flip_test = 1; aoqi@0: if (bn->_test._test == BoolTest::le) { aoqi@0: l = cmp->in(2); aoqi@0: r = cmp->in(1); aoqi@0: flip_test = 2; aoqi@0: } else if (bn->_test._test != BoolTest::lt) { aoqi@0: return 0; aoqi@0: } aoqi@0: if (l->is_top()) return 0; // Top input means dead test aoqi@0: if (r->Opcode() != Op_LoadRange) return 0; aoqi@0: aoqi@0: // We have recognized one of these forms: aoqi@0: // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ... aoqi@0: // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ... aoqi@0: aoqi@0: // Make sure it's a real range check by requiring an uncommon trap aoqi@0: // along the OOB path. Otherwise, it's possible that the user wrote aoqi@0: // something which optimized to look like a range check but behaves aoqi@0: // in some other way. aoqi@0: Node* iftrap = proj_out(flip_test == 2 ? true : false); aoqi@0: bool found_trap = false; aoqi@0: if (iftrap != NULL) { aoqi@0: Node* u = iftrap->unique_ctrl_out(); aoqi@0: if (u != NULL) { aoqi@0: // It could be a merge point (Region) for uncommon trap. aoqi@0: if (u->is_Region()) { aoqi@0: Node* c = u->unique_ctrl_out(); aoqi@0: if (c != NULL) { aoqi@0: iftrap = u; aoqi@0: u = c; aoqi@0: } aoqi@0: } aoqi@0: if (u->in(0) == iftrap && u->is_CallStaticJava()) { aoqi@0: int req = u->as_CallStaticJava()->uncommon_trap_request(); aoqi@0: if (Deoptimization::trap_request_reason(req) == aoqi@0: Deoptimization::Reason_range_check) { aoqi@0: found_trap = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (!found_trap) return 0; // sorry, no cigar aoqi@0: aoqi@0: // Look for index+offset form aoqi@0: Node* ind = l; aoqi@0: jint off = 0; aoqi@0: if (l->is_top()) { aoqi@0: return 0; aoqi@0: } else if (l->is_Add()) { aoqi@0: if ((off = l->in(1)->find_int_con(0)) != 0) { aoqi@0: ind = l->in(2); aoqi@0: } else if ((off = l->in(2)->find_int_con(0)) != 0) { aoqi@0: ind = l->in(1); aoqi@0: } aoqi@0: } else if ((off = l->find_int_con(-1)) >= 0) { aoqi@0: // constant offset with no variable index aoqi@0: ind = NULL; aoqi@0: } else { aoqi@0: // variable index with no constant offset (or dead negative index) aoqi@0: off = 0; aoqi@0: } aoqi@0: aoqi@0: // Return all the values: aoqi@0: index = ind; aoqi@0: offset = off; aoqi@0: range = r; aoqi@0: return flip_test; aoqi@0: } aoqi@0: aoqi@0: //------------------------------adjust_check----------------------------------- aoqi@0: // Adjust (widen) a prior range check aoqi@0: static void adjust_check(Node* proj, Node* range, Node* index, aoqi@0: int flip, jint off_lo, PhaseIterGVN* igvn) { aoqi@0: PhaseGVN *gvn = igvn; aoqi@0: // Break apart the old check aoqi@0: Node *iff = proj->in(0); aoqi@0: Node *bol = iff->in(1); aoqi@0: if( bol->is_top() ) return; // In case a partially dead range check appears aoqi@0: // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode aoqi@0: DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } ) aoqi@0: if( !bol->is_Bool() ) return; aoqi@0: aoqi@0: Node *cmp = bol->in(1); aoqi@0: // Compute a new check aoqi@0: Node *new_add = gvn->intcon(off_lo); aoqi@0: if( index ) { aoqi@0: new_add = off_lo ? gvn->transform(new (gvn->C) AddINode( index, new_add )) : index; aoqi@0: } aoqi@0: Node *new_cmp = (flip == 1) aoqi@0: ? new (gvn->C) CmpUNode( new_add, range ) aoqi@0: : new (gvn->C) CmpUNode( range, new_add ); aoqi@0: new_cmp = gvn->transform(new_cmp); aoqi@0: // See if no need to adjust the existing check aoqi@0: if( new_cmp == cmp ) return; aoqi@0: // Else, adjust existing check aoqi@0: Node *new_bol = gvn->transform( new (gvn->C) BoolNode( new_cmp, bol->as_Bool()->_test._test ) ); aoqi@0: igvn->rehash_node_delayed( iff ); aoqi@0: iff->set_req_X( 1, new_bol, igvn ); aoqi@0: } aoqi@0: aoqi@0: //------------------------------up_one_dom------------------------------------- aoqi@0: // Walk up the dominator tree one step. Return NULL at root or true aoqi@0: // complex merges. Skips through small diamonds. aoqi@0: Node* IfNode::up_one_dom(Node *curr, bool linear_only) { aoqi@0: Node *dom = curr->in(0); aoqi@0: if( !dom ) // Found a Region degraded to a copy? aoqi@0: return curr->nonnull_req(); // Skip thru it aoqi@0: aoqi@0: if( curr != dom ) // Normal walk up one step? aoqi@0: return dom; aoqi@0: aoqi@0: // Use linear_only if we are still parsing, since we cannot aoqi@0: // trust the regions to be fully filled in. aoqi@0: if (linear_only) aoqi@0: return NULL; aoqi@0: aoqi@0: if( dom->is_Root() ) aoqi@0: return NULL; aoqi@0: aoqi@0: // Else hit a Region. Check for a loop header aoqi@0: if( dom->is_Loop() ) aoqi@0: return dom->in(1); // Skip up thru loops aoqi@0: aoqi@0: // Check for small diamonds aoqi@0: Node *din1, *din2, *din3, *din4; aoqi@0: if( dom->req() == 3 && // 2-path merge point aoqi@0: (din1 = dom ->in(1)) && // Left path exists aoqi@0: (din2 = dom ->in(2)) && // Right path exists aoqi@0: (din3 = din1->in(0)) && // Left path up one aoqi@0: (din4 = din2->in(0)) ) { // Right path up one aoqi@0: if( din3->is_Call() && // Handle a slow-path call on either arm aoqi@0: (din3 = din3->in(0)) ) aoqi@0: din3 = din3->in(0); aoqi@0: if( din4->is_Call() && // Handle a slow-path call on either arm aoqi@0: (din4 = din4->in(0)) ) aoqi@0: din4 = din4->in(0); aoqi@0: if( din3 == din4 && din3->is_If() ) aoqi@0: return din3; // Skip around diamonds aoqi@0: } aoqi@0: aoqi@0: // Give up the search at true merges aoqi@0: return NULL; // Dead loop? Or hit root? aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //------------------------------filtered_int_type-------------------------------- aoqi@0: // Return a possibly more restrictive type for val based on condition control flow for an if aoqi@0: const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) { aoqi@0: assert(if_proj && aoqi@0: (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection"); aoqi@0: if (if_proj->in(0) && if_proj->in(0)->is_If()) { aoqi@0: IfNode* iff = if_proj->in(0)->as_If(); aoqi@0: if (iff->in(1) && iff->in(1)->is_Bool()) { aoqi@0: BoolNode* bol = iff->in(1)->as_Bool(); aoqi@0: if (bol->in(1) && bol->in(1)->is_Cmp()) { aoqi@0: const CmpNode* cmp = bol->in(1)->as_Cmp(); aoqi@0: if (cmp->in(1) == val) { aoqi@0: const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int(); aoqi@0: if (cmp2_t != NULL) { aoqi@0: jint lo = cmp2_t->_lo; aoqi@0: jint hi = cmp2_t->_hi; aoqi@0: BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate(); aoqi@0: switch (msk) { aoqi@0: case BoolTest::ne: aoqi@0: // Can't refine type aoqi@0: return NULL; aoqi@0: case BoolTest::eq: aoqi@0: return cmp2_t; aoqi@0: case BoolTest::lt: aoqi@0: lo = TypeInt::INT->_lo; aoqi@0: if (hi - 1 < hi) { aoqi@0: hi = hi - 1; aoqi@0: } aoqi@0: break; aoqi@0: case BoolTest::le: aoqi@0: lo = TypeInt::INT->_lo; aoqi@0: break; aoqi@0: case BoolTest::gt: aoqi@0: if (lo + 1 > lo) { aoqi@0: lo = lo + 1; aoqi@0: } aoqi@0: hi = TypeInt::INT->_hi; aoqi@0: break; aoqi@0: case BoolTest::ge: aoqi@0: // lo unchanged aoqi@0: hi = TypeInt::INT->_hi; aoqi@0: break; aoqi@0: } aoqi@0: const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen); aoqi@0: return rtn_t; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------fold_compares---------------------------- aoqi@0: // See if a pair of CmpIs can be converted into a CmpU. In some cases aoqi@0: // the direction of this if is determined by the preceding if so it aoqi@0: // can be eliminate entirely. Given an if testing (CmpI n c) check aoqi@0: // for an immediately control dependent if that is testing (CmpI n c2) aoqi@0: // and has one projection leading to this if and the other projection aoqi@0: // leading to a region that merges one of this ifs control aoqi@0: // projections. aoqi@0: // aoqi@0: // If aoqi@0: // / | aoqi@0: // / | aoqi@0: // / | aoqi@0: // If | aoqi@0: // /\ | aoqi@0: // / \ | aoqi@0: // / \ | aoqi@0: // / Region aoqi@0: // aoqi@0: Node* IfNode::fold_compares(PhaseGVN* phase) { aoqi@0: if (Opcode() != Op_If) return NULL; aoqi@0: aoqi@0: Node* this_cmp = in(1)->in(1); aoqi@0: if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI && aoqi@0: this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) { aoqi@0: Node* ctrl = in(0); aoqi@0: BoolNode* this_bool = in(1)->as_Bool(); aoqi@0: Node* n = this_cmp->in(1); aoqi@0: int hi = this_cmp->in(2)->get_int(); aoqi@0: if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 && aoqi@0: ctrl->in(0)->is_If() && aoqi@0: ctrl->in(0)->outcnt() == 2 && aoqi@0: ctrl->in(0)->in(1)->is_Bool() && aoqi@0: ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI && aoqi@0: ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() && aoqi@0: ctrl->in(0)->in(1)->in(1)->in(2) != phase->C->top() && aoqi@0: ctrl->in(0)->in(1)->in(1)->in(1) == n) { aoqi@0: IfNode* dom_iff = ctrl->in(0)->as_If(); aoqi@0: Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con); aoqi@0: if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() && aoqi@0: this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) { aoqi@0: // Identify which proj goes to the region and which continues on aoqi@0: RegionNode* region = otherproj->unique_out()->as_Region(); aoqi@0: Node* success = NULL; aoqi@0: Node* fail = NULL; aoqi@0: for (int i = 0; i < 2; i++) { aoqi@0: Node* proj = proj_out(i); aoqi@0: if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) { aoqi@0: success = proj; aoqi@0: } else if (fail == NULL) { aoqi@0: fail = proj; aoqi@0: } else { aoqi@0: success = fail = NULL; aoqi@0: } aoqi@0: } aoqi@0: if (success != NULL && fail != NULL && !region->has_phi()) { aoqi@0: int lo = dom_iff->in(1)->in(1)->in(2)->get_int(); aoqi@0: BoolNode* dom_bool = dom_iff->in(1)->as_Bool(); aoqi@0: Node* dom_cmp = dom_bool->in(1); aoqi@0: const TypeInt* failtype = filtered_int_type(phase, n, ctrl); aoqi@0: if (failtype != NULL) { aoqi@0: const TypeInt* type2 = filtered_int_type(phase, n, fail); aoqi@0: if (type2 != NULL) { aoqi@0: failtype = failtype->join(type2)->is_int(); aoqi@0: } else { aoqi@0: failtype = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (failtype != NULL && aoqi@0: dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) { aoqi@0: int bound = failtype->_hi - failtype->_lo + 1; aoqi@0: if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) { aoqi@0: // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) hi) aoqi@0: BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge; aoqi@0: Node* adjusted = phase->transform(new (phase->C) SubINode(n, phase->intcon(failtype->_lo))); aoqi@0: Node* newcmp = phase->transform(new (phase->C) CmpUNode(adjusted, phase->intcon(bound))); aoqi@0: Node* newbool = phase->transform(new (phase->C) BoolNode(newcmp, cond)); aoqi@0: phase->is_IterGVN()->replace_input_of(dom_iff, 1, phase->intcon(ctrl->as_Proj()->_con)); aoqi@0: phase->hash_delete(this); aoqi@0: set_req(1, newbool); aoqi@0: return this; aoqi@0: } aoqi@0: if (failtype->_lo > failtype->_hi) { aoqi@0: // previous if determines the result of this if so aoqi@0: // replace Bool with constant aoqi@0: phase->hash_delete(this); aoqi@0: set_req(1, phase->intcon(success->as_Proj()->_con)); aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------remove_useless_bool---------------------------- aoqi@0: // Check for people making a useless boolean: things like aoqi@0: // if( (x < y ? true : false) ) { ... } aoqi@0: // Replace with if( x < y ) { ... } aoqi@0: static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) { aoqi@0: Node *i1 = iff->in(1); aoqi@0: if( !i1->is_Bool() ) return NULL; aoqi@0: BoolNode *bol = i1->as_Bool(); aoqi@0: aoqi@0: Node *cmp = bol->in(1); aoqi@0: if( cmp->Opcode() != Op_CmpI ) return NULL; aoqi@0: aoqi@0: // Must be comparing against a bool aoqi@0: const Type *cmp2_t = phase->type( cmp->in(2) ); aoqi@0: if( cmp2_t != TypeInt::ZERO && aoqi@0: cmp2_t != TypeInt::ONE ) aoqi@0: return NULL; aoqi@0: aoqi@0: // Find a prior merge point merging the boolean aoqi@0: i1 = cmp->in(1); aoqi@0: if( !i1->is_Phi() ) return NULL; aoqi@0: PhiNode *phi = i1->as_Phi(); aoqi@0: if( phase->type( phi ) != TypeInt::BOOL ) aoqi@0: return NULL; aoqi@0: aoqi@0: // Check for diamond pattern aoqi@0: int true_path = phi->is_diamond_phi(); aoqi@0: if( true_path == 0 ) return NULL; aoqi@0: aoqi@0: // Make sure that iff and the control of the phi are different. This aoqi@0: // should really only happen for dead control flow since it requires aoqi@0: // an illegal cycle. aoqi@0: if (phi->in(0)->in(1)->in(0) == iff) return NULL; aoqi@0: aoqi@0: // phi->region->if_proj->ifnode->bool->cmp aoqi@0: BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool(); aoqi@0: aoqi@0: // Now get the 'sense' of the test correct so we can plug in aoqi@0: // either iff2->in(1) or its complement. aoqi@0: int flip = 0; aoqi@0: if( bol->_test._test == BoolTest::ne ) flip = 1-flip; aoqi@0: else if( bol->_test._test != BoolTest::eq ) return NULL; aoqi@0: if( cmp2_t == TypeInt::ZERO ) flip = 1-flip; aoqi@0: aoqi@0: const Type *phi1_t = phase->type( phi->in(1) ); aoqi@0: const Type *phi2_t = phase->type( phi->in(2) ); aoqi@0: // Check for Phi(0,1) and flip aoqi@0: if( phi1_t == TypeInt::ZERO ) { aoqi@0: if( phi2_t != TypeInt::ONE ) return NULL; aoqi@0: flip = 1-flip; aoqi@0: } else { aoqi@0: // Check for Phi(1,0) aoqi@0: if( phi1_t != TypeInt::ONE ) return NULL; aoqi@0: if( phi2_t != TypeInt::ZERO ) return NULL; aoqi@0: } aoqi@0: if( true_path == 2 ) { aoqi@0: flip = 1-flip; aoqi@0: } aoqi@0: aoqi@0: Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2); aoqi@0: assert(new_bol != iff->in(1), "must make progress"); aoqi@0: iff->set_req(1, new_bol); aoqi@0: // Intervening diamond probably goes dead aoqi@0: phase->C->set_major_progress(); aoqi@0: return iff; aoqi@0: } aoqi@0: aoqi@0: static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff); aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // Return a node which is more "ideal" than the current node. Strip out aoqi@0: // control copies aoqi@0: Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: if (remove_dead_region(phase, can_reshape)) return this; aoqi@0: // No Def-Use info? aoqi@0: if (!can_reshape) return NULL; aoqi@0: PhaseIterGVN *igvn = phase->is_IterGVN(); aoqi@0: aoqi@0: // Don't bother trying to transform a dead if aoqi@0: if (in(0)->is_top()) return NULL; aoqi@0: // Don't bother trying to transform an if with a dead test aoqi@0: if (in(1)->is_top()) return NULL; aoqi@0: // Another variation of a dead test aoqi@0: if (in(1)->is_Con()) return NULL; aoqi@0: // Another variation of a dead if aoqi@0: if (outcnt() < 2) return NULL; aoqi@0: aoqi@0: // Canonicalize the test. aoqi@0: Node* idt_if = idealize_test(phase, this); aoqi@0: if (idt_if != NULL) return idt_if; aoqi@0: aoqi@0: // Try to split the IF aoqi@0: Node *s = split_if(this, igvn); aoqi@0: if (s != NULL) return s; aoqi@0: aoqi@0: // Check for people making a useless boolean: things like aoqi@0: // if( (x < y ? true : false) ) { ... } aoqi@0: // Replace with if( x < y ) { ... } aoqi@0: Node *bol2 = remove_useless_bool(this, phase); aoqi@0: if( bol2 ) return bol2; aoqi@0: aoqi@0: // Setup to scan up the CFG looking for a dominating test aoqi@0: Node *dom = in(0); aoqi@0: Node *prev_dom = this; aoqi@0: aoqi@0: // Check for range-check vs other kinds of tests aoqi@0: Node *index1, *range1; aoqi@0: jint offset1; aoqi@0: int flip1 = is_range_check(range1, index1, offset1); aoqi@0: if( flip1 ) { aoqi@0: Node *first_prev_dom = NULL; aoqi@0: aoqi@0: // Try to remove extra range checks. All 'up_one_dom' gives up at merges aoqi@0: // so all checks we inspect post-dominate the top-most check we find. aoqi@0: // If we are going to fail the current check and we reach the top check aoqi@0: // then we are guaranteed to fail, so just start interpreting there. aoqi@0: // We 'expand' the top 2 range checks to include all post-dominating aoqi@0: // checks. aoqi@0: aoqi@0: // The top 2 range checks seen aoqi@0: Node *prev_chk1 = NULL; aoqi@0: Node *prev_chk2 = NULL; aoqi@0: // Low and high offsets seen so far aoqi@0: jint off_lo = offset1; aoqi@0: jint off_hi = offset1; aoqi@0: aoqi@0: // Scan for the top 2 checks and collect range of offsets aoqi@0: for( int dist = 0; dist < 999; dist++ ) { // Range-Check scan limit aoqi@0: if( dom->Opcode() == Op_If && // Not same opcode? aoqi@0: prev_dom->in(0) == dom ) { // One path of test does dominate? aoqi@0: if( dom == this ) return NULL; // dead loop aoqi@0: // See if this is a range check aoqi@0: Node *index2, *range2; aoqi@0: jint offset2; aoqi@0: int flip2 = dom->as_If()->is_range_check(range2, index2, offset2); aoqi@0: // See if this is a _matching_ range check, checking against aoqi@0: // the same array bounds. aoqi@0: if( flip2 == flip1 && range2 == range1 && index2 == index1 && aoqi@0: dom->outcnt() == 2 ) { aoqi@0: // Gather expanded bounds aoqi@0: off_lo = MIN2(off_lo,offset2); aoqi@0: off_hi = MAX2(off_hi,offset2); aoqi@0: // Record top 2 range checks aoqi@0: prev_chk2 = prev_chk1; aoqi@0: prev_chk1 = prev_dom; aoqi@0: // If we match the test exactly, then the top test covers aoqi@0: // both our lower and upper bounds. aoqi@0: if( dom->in(1) == in(1) ) aoqi@0: prev_chk2 = prev_chk1; aoqi@0: } aoqi@0: } aoqi@0: prev_dom = dom; aoqi@0: dom = up_one_dom( dom ); aoqi@0: if( !dom ) break; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Attempt to widen the dominating range check to cover some later aoqi@0: // ones. Since range checks "fail" by uncommon-trapping to the aoqi@0: // interpreter, widening a check can make us speculative enter the aoqi@0: // interpreter. If we see range-check deopt's, do not widen! aoqi@0: if (!phase->C->allow_range_check_smearing()) return NULL; aoqi@0: aoqi@0: // Constant indices only need to check the upper bound. aoqi@0: // Non-constance indices must check both low and high. aoqi@0: if( index1 ) { aoqi@0: // Didn't find 2 prior covering checks, so cannot remove anything. aoqi@0: if( !prev_chk2 ) return NULL; aoqi@0: // 'Widen' the offsets of the 1st and 2nd covering check aoqi@0: adjust_check( prev_chk1, range1, index1, flip1, off_lo, igvn ); aoqi@0: // Do not call adjust_check twice on the same projection aoqi@0: // as the first call may have transformed the BoolNode to a ConI aoqi@0: if( prev_chk1 != prev_chk2 ) { aoqi@0: adjust_check( prev_chk2, range1, index1, flip1, off_hi, igvn ); aoqi@0: } aoqi@0: // Test is now covered by prior checks, dominate it out aoqi@0: prev_dom = prev_chk2; aoqi@0: } else { aoqi@0: // Didn't find prior covering check, so cannot remove anything. aoqi@0: if( !prev_chk1 ) return NULL; aoqi@0: // 'Widen' the offset of the 1st and only covering check aoqi@0: adjust_check( prev_chk1, range1, index1, flip1, off_hi, igvn ); aoqi@0: // Test is now covered by prior checks, dominate it out aoqi@0: prev_dom = prev_chk1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: } else { // Scan for an equivalent test aoqi@0: aoqi@0: Node *cmp; aoqi@0: int dist = 0; // Cutoff limit for search aoqi@0: int op = Opcode(); aoqi@0: if( op == Op_If && aoqi@0: (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) { aoqi@0: if( cmp->in(2) != NULL && // make sure cmp is not already dead aoqi@0: cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) { aoqi@0: dist = 64; // Limit for null-pointer scans aoqi@0: } else { aoqi@0: dist = 4; // Do not bother for random pointer tests aoqi@0: } aoqi@0: } else { aoqi@0: dist = 4; // Limit for random junky scans aoqi@0: } aoqi@0: aoqi@0: // Normal equivalent-test check. aoqi@0: if( !dom ) return NULL; // Dead loop? aoqi@0: aoqi@0: Node* result = fold_compares(phase); aoqi@0: if (result != NULL) { aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // Search up the dominator tree for an If with an identical test aoqi@0: while( dom->Opcode() != op || // Not same opcode? aoqi@0: dom->in(1) != in(1) || // Not same input 1? aoqi@0: (req() == 3 && dom->in(2) != in(2)) || // Not same input 2? aoqi@0: prev_dom->in(0) != dom ) { // One path of test does not dominate? aoqi@0: if( dist < 0 ) return NULL; aoqi@0: aoqi@0: dist--; aoqi@0: prev_dom = dom; aoqi@0: dom = up_one_dom( dom ); aoqi@0: if( !dom ) return NULL; aoqi@0: } aoqi@0: aoqi@0: // Check that we did not follow a loop back to ourselves aoqi@0: if( this == dom ) aoqi@0: return NULL; aoqi@0: aoqi@0: if( dist > 2 ) // Add to count of NULL checks elided aoqi@0: explicit_null_checks_elided++; aoqi@0: aoqi@0: } // End of Else scan for an equivalent test aoqi@0: aoqi@0: // Hit! Remove this IF aoqi@0: #ifndef PRODUCT aoqi@0: if( TraceIterativeGVN ) { aoqi@0: tty->print(" Removing IfNode: "); this->dump(); aoqi@0: } aoqi@0: if( VerifyOpto && !phase->allow_progress() ) { aoqi@0: // Found an equivalent dominating test, aoqi@0: // we can not guarantee reaching a fix-point for these during iterativeGVN aoqi@0: // since intervening nodes may not change. aoqi@0: return NULL; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // Replace dominated IfNode aoqi@0: dominated_by( prev_dom, igvn ); aoqi@0: aoqi@0: // Must return either the original node (now dead) or a new node aoqi@0: // (Do not return a top here, since that would break the uniqueness of top.) aoqi@0: return new (phase->C) ConINode(TypeInt::ZERO); aoqi@0: } aoqi@0: aoqi@0: //------------------------------dominated_by----------------------------------- aoqi@0: void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) { aoqi@0: igvn->hash_delete(this); // Remove self to prevent spurious V-N aoqi@0: Node *idom = in(0); aoqi@0: // Need opcode to decide which way 'this' test goes aoqi@0: int prev_op = prev_dom->Opcode(); aoqi@0: Node *top = igvn->C->top(); // Shortcut to top aoqi@0: aoqi@0: // Loop predicates may have depending checks which should not aoqi@0: // be skipped. For example, range check predicate has two checks aoqi@0: // for lower and upper bounds. aoqi@0: ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj(); aoqi@0: if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate)) aoqi@0: prev_dom = idom; aoqi@0: aoqi@0: // Now walk the current IfNode's projections. aoqi@0: // Loop ends when 'this' has no more uses. aoqi@0: for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) { aoqi@0: Node *ifp = last_out(i); // Get IfTrue/IfFalse aoqi@0: igvn->add_users_to_worklist(ifp); aoqi@0: // Check which projection it is and set target. aoqi@0: // Data-target is either the dominating projection of the same type aoqi@0: // or TOP if the dominating projection is of opposite type. aoqi@0: // Data-target will be used as the new control edge for the non-CFG aoqi@0: // nodes like Casts and Loads. aoqi@0: Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top; aoqi@0: // Control-target is just the If's immediate dominator or TOP. aoqi@0: Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top; aoqi@0: aoqi@0: // For each child of an IfTrue/IfFalse projection, reroute. aoqi@0: // Loop ends when projection has no more uses. aoqi@0: for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) { aoqi@0: Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse aoqi@0: if( !s->depends_only_on_test() ) { aoqi@0: // Find the control input matching this def-use edge. aoqi@0: // For Regions it may not be in slot 0. aoqi@0: uint l; aoqi@0: for( l = 0; s->in(l) != ifp; l++ ) { } aoqi@0: igvn->replace_input_of(s, l, ctrl_target); aoqi@0: } else { // Else, for control producers, aoqi@0: igvn->replace_input_of(s, 0, data_target); // Move child to data-target aoqi@0: } aoqi@0: } // End for each child of a projection aoqi@0: aoqi@0: igvn->remove_dead_node(ifp); aoqi@0: } // End for each IfTrue/IfFalse child of If aoqi@0: aoqi@0: // Kill the IfNode aoqi@0: igvn->remove_dead_node(this); aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // If the test is constant & we match, then we are the input Control aoqi@0: Node *IfTrueNode::Identity( PhaseTransform *phase ) { aoqi@0: // Can only optimize if cannot go the other way aoqi@0: const TypeTuple *t = phase->type(in(0))->is_tuple(); aoqi@0: return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE ) aoqi@0: ? in(0)->in(0) // IfNode control aoqi@0: : this; // no progress aoqi@0: } aoqi@0: aoqi@0: //------------------------------dump_spec-------------------------------------- aoqi@0: #ifndef PRODUCT aoqi@0: void IfNode::dump_spec(outputStream *st) const { aoqi@0: st->print("P=%f, C=%f",_prob,_fcnt); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: //------------------------------idealize_test---------------------------------- aoqi@0: // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and aoqi@0: // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms aoqi@0: // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as aoqi@0: // needed. aoqi@0: static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) { aoqi@0: assert(iff->in(0) != NULL, "If must be live"); aoqi@0: aoqi@0: if (iff->outcnt() != 2) return NULL; // Malformed projections. aoqi@0: Node* old_if_f = iff->proj_out(false); aoqi@0: Node* old_if_t = iff->proj_out(true); aoqi@0: aoqi@0: // CountedLoopEnds want the back-control test to be TRUE, irregardless of aoqi@0: // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition aoqi@0: // happens in count-down loops aoqi@0: if (iff->is_CountedLoopEnd()) return NULL; aoqi@0: if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests aoqi@0: BoolNode *b = iff->in(1)->as_Bool(); aoqi@0: BoolTest bt = b->_test; aoqi@0: // Test already in good order? aoqi@0: if( bt.is_canonical() ) aoqi@0: return NULL; aoqi@0: aoqi@0: // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and aoqi@0: // cloning the IfNode. aoqi@0: Node* new_b = phase->transform( new (phase->C) BoolNode(b->in(1), bt.negate()) ); aoqi@0: if( !new_b->is_Bool() ) return NULL; aoqi@0: b = new_b->as_Bool(); aoqi@0: aoqi@0: PhaseIterGVN *igvn = phase->is_IterGVN(); aoqi@0: assert( igvn, "Test is not canonical in parser?" ); aoqi@0: aoqi@0: // The IF node never really changes, but it needs to be cloned aoqi@0: iff = new (phase->C) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt); aoqi@0: aoqi@0: Node *prior = igvn->hash_find_insert(iff); aoqi@0: if( prior ) { aoqi@0: igvn->remove_dead_node(iff); aoqi@0: iff = (IfNode*)prior; aoqi@0: } else { aoqi@0: // Cannot call transform on it just yet aoqi@0: igvn->set_type_bottom(iff); aoqi@0: } aoqi@0: igvn->_worklist.push(iff); aoqi@0: aoqi@0: // Now handle projections. Cloning not required. aoqi@0: Node* new_if_f = (Node*)(new (phase->C) IfFalseNode( iff )); aoqi@0: Node* new_if_t = (Node*)(new (phase->C) IfTrueNode ( iff )); aoqi@0: aoqi@0: igvn->register_new_node_with_optimizer(new_if_f); aoqi@0: igvn->register_new_node_with_optimizer(new_if_t); aoqi@0: // Flip test, so flip trailing control aoqi@0: igvn->replace_node(old_if_f, new_if_t); aoqi@0: igvn->replace_node(old_if_t, new_if_f); aoqi@0: aoqi@0: // Progress aoqi@0: return iff; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // If the test is constant & we match, then we are the input Control aoqi@0: Node *IfFalseNode::Identity( PhaseTransform *phase ) { aoqi@0: // Can only optimize if cannot go the other way aoqi@0: const TypeTuple *t = phase->type(in(0))->is_tuple(); aoqi@0: return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE ) aoqi@0: ? in(0)->in(0) // IfNode control aoqi@0: : this; // no progress aoqi@0: }