src/share/vm/opto/ifnode.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "opto/addnode.hpp"
aoqi@0 28 #include "opto/cfgnode.hpp"
aoqi@0 29 #include "opto/connode.hpp"
aoqi@0 30 #include "opto/loopnode.hpp"
aoqi@0 31 #include "opto/phaseX.hpp"
aoqi@0 32 #include "opto/runtime.hpp"
aoqi@0 33 #include "opto/subnode.hpp"
aoqi@0 34
aoqi@0 35 // Portions of code courtesy of Clifford Click
aoqi@0 36
aoqi@0 37 // Optimization - Graph Style
aoqi@0 38
aoqi@0 39
aoqi@0 40 extern int explicit_null_checks_elided;
aoqi@0 41
aoqi@0 42 //=============================================================================
aoqi@0 43 //------------------------------Value------------------------------------------
aoqi@0 44 // Return a tuple for whichever arm of the IF is reachable
aoqi@0 45 const Type *IfNode::Value( PhaseTransform *phase ) const {
aoqi@0 46 if( !in(0) ) return Type::TOP;
aoqi@0 47 if( phase->type(in(0)) == Type::TOP )
aoqi@0 48 return Type::TOP;
aoqi@0 49 const Type *t = phase->type(in(1));
aoqi@0 50 if( t == Type::TOP ) // data is undefined
aoqi@0 51 return TypeTuple::IFNEITHER; // unreachable altogether
aoqi@0 52 if( t == TypeInt::ZERO ) // zero, or false
aoqi@0 53 return TypeTuple::IFFALSE; // only false branch is reachable
aoqi@0 54 if( t == TypeInt::ONE ) // 1, or true
aoqi@0 55 return TypeTuple::IFTRUE; // only true branch is reachable
aoqi@0 56 assert( t == TypeInt::BOOL, "expected boolean type" );
aoqi@0 57
aoqi@0 58 return TypeTuple::IFBOTH; // No progress
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 const RegMask &IfNode::out_RegMask() const {
aoqi@0 62 return RegMask::Empty;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 //------------------------------split_if---------------------------------------
aoqi@0 66 // Look for places where we merge constants, then test on the merged value.
aoqi@0 67 // If the IF test will be constant folded on the path with the constant, we
aoqi@0 68 // win by splitting the IF to before the merge point.
aoqi@0 69 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {
aoqi@0 70 // I could be a lot more general here, but I'm trying to squeeze this
aoqi@0 71 // in before the Christmas '98 break so I'm gonna be kinda restrictive
aoqi@0 72 // on the patterns I accept. CNC
aoqi@0 73
aoqi@0 74 // Look for a compare of a constant and a merged value
aoqi@0 75 Node *i1 = iff->in(1);
aoqi@0 76 if( !i1->is_Bool() ) return NULL;
aoqi@0 77 BoolNode *b = i1->as_Bool();
aoqi@0 78 Node *cmp = b->in(1);
aoqi@0 79 if( !cmp->is_Cmp() ) return NULL;
aoqi@0 80 i1 = cmp->in(1);
aoqi@0 81 if( i1 == NULL || !i1->is_Phi() ) return NULL;
aoqi@0 82 PhiNode *phi = i1->as_Phi();
aoqi@0 83 if( phi->is_copy() ) return NULL;
aoqi@0 84 Node *con2 = cmp->in(2);
aoqi@0 85 if( !con2->is_Con() ) return NULL;
aoqi@0 86 // See that the merge point contains some constants
aoqi@0 87 Node *con1=NULL;
aoqi@0 88 uint i4;
aoqi@0 89 for( i4 = 1; i4 < phi->req(); i4++ ) {
aoqi@0 90 con1 = phi->in(i4);
aoqi@0 91 if( !con1 ) return NULL; // Do not optimize partially collapsed merges
aoqi@0 92 if( con1->is_Con() ) break; // Found a constant
aoqi@0 93 // Also allow null-vs-not-null checks
aoqi@0 94 const TypePtr *tp = igvn->type(con1)->isa_ptr();
aoqi@0 95 if( tp && tp->_ptr == TypePtr::NotNull )
aoqi@0 96 break;
aoqi@0 97 }
aoqi@0 98 if( i4 >= phi->req() ) return NULL; // Found no constants
aoqi@0 99
aoqi@0 100 igvn->C->set_has_split_ifs(true); // Has chance for split-if
aoqi@0 101
aoqi@0 102 // Make sure that the compare can be constant folded away
aoqi@0 103 Node *cmp2 = cmp->clone();
aoqi@0 104 cmp2->set_req(1,con1);
aoqi@0 105 cmp2->set_req(2,con2);
aoqi@0 106 const Type *t = cmp2->Value(igvn);
aoqi@0 107 // This compare is dead, so whack it!
aoqi@0 108 igvn->remove_dead_node(cmp2);
aoqi@0 109 if( !t->singleton() ) return NULL;
aoqi@0 110
aoqi@0 111 // No intervening control, like a simple Call
aoqi@0 112 Node *r = iff->in(0);
aoqi@0 113 if( !r->is_Region() ) return NULL;
aoqi@0 114 if( phi->region() != r ) return NULL;
aoqi@0 115 // No other users of the cmp/bool
aoqi@0 116 if (b->outcnt() != 1 || cmp->outcnt() != 1) {
aoqi@0 117 //tty->print_cr("many users of cmp/bool");
aoqi@0 118 return NULL;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 // Make sure we can determine where all the uses of merged values go
aoqi@0 122 for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {
aoqi@0 123 Node* u = r->fast_out(j);
aoqi@0 124 if( u == r ) continue;
aoqi@0 125 if( u == iff ) continue;
aoqi@0 126 if( u->outcnt() == 0 ) continue; // use is dead & ignorable
aoqi@0 127 if( !u->is_Phi() ) {
aoqi@0 128 /*
aoqi@0 129 if( u->is_Start() ) {
aoqi@0 130 tty->print_cr("Region has inlined start use");
aoqi@0 131 } else {
aoqi@0 132 tty->print_cr("Region has odd use");
aoqi@0 133 u->dump(2);
aoqi@0 134 }*/
aoqi@0 135 return NULL;
aoqi@0 136 }
aoqi@0 137 if( u != phi ) {
aoqi@0 138 // CNC - do not allow any other merged value
aoqi@0 139 //tty->print_cr("Merging another value");
aoqi@0 140 //u->dump(2);
aoqi@0 141 return NULL;
aoqi@0 142 }
aoqi@0 143 // Make sure we can account for all Phi uses
aoqi@0 144 for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {
aoqi@0 145 Node* v = u->fast_out(k); // User of the phi
aoqi@0 146 // CNC - Allow only really simple patterns.
aoqi@0 147 // In particular I disallow AddP of the Phi, a fairly common pattern
aoqi@0 148 if( v == cmp ) continue; // The compare is OK
aoqi@0 149 if( (v->is_ConstraintCast()) &&
aoqi@0 150 v->in(0)->in(0) == iff )
aoqi@0 151 continue; // CastPP/II of the IfNode is OK
aoqi@0 152 // Disabled following code because I cannot tell if exactly one
aoqi@0 153 // path dominates without a real dominator check. CNC 9/9/1999
aoqi@0 154 //uint vop = v->Opcode();
aoqi@0 155 //if( vop == Op_Phi ) { // Phi from another merge point might be OK
aoqi@0 156 // Node *r = v->in(0); // Get controlling point
aoqi@0 157 // if( !r ) return NULL; // Degraded to a copy
aoqi@0 158 // // Find exactly one path in (either True or False doms, but not IFF)
aoqi@0 159 // int cnt = 0;
aoqi@0 160 // for( uint i = 1; i < r->req(); i++ )
aoqi@0 161 // if( r->in(i) && r->in(i)->in(0) == iff )
aoqi@0 162 // cnt++;
aoqi@0 163 // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi
aoqi@0 164 //}
aoqi@0 165 if( !v->is_Call() ) {
aoqi@0 166 /*
aoqi@0 167 if( v->Opcode() == Op_AddP ) {
aoqi@0 168 tty->print_cr("Phi has AddP use");
aoqi@0 169 } else if( v->Opcode() == Op_CastPP ) {
aoqi@0 170 tty->print_cr("Phi has CastPP use");
aoqi@0 171 } else if( v->Opcode() == Op_CastII ) {
aoqi@0 172 tty->print_cr("Phi has CastII use");
aoqi@0 173 } else {
aoqi@0 174 tty->print_cr("Phi has use I cant be bothered with");
aoqi@0 175 }
aoqi@0 176 */
aoqi@0 177 }
aoqi@0 178 return NULL;
aoqi@0 179
aoqi@0 180 /* CNC - Cut out all the fancy acceptance tests
aoqi@0 181 // Can we clone this use when doing the transformation?
aoqi@0 182 // If all uses are from Phis at this merge or constants, then YES.
aoqi@0 183 if( !v->in(0) && v != cmp ) {
aoqi@0 184 tty->print_cr("Phi has free-floating use");
aoqi@0 185 v->dump(2);
aoqi@0 186 return NULL;
aoqi@0 187 }
aoqi@0 188 for( uint l = 1; l < v->req(); l++ ) {
aoqi@0 189 if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&
aoqi@0 190 !v->in(l)->is_Con() ) {
aoqi@0 191 tty->print_cr("Phi has use");
aoqi@0 192 v->dump(2);
aoqi@0 193 return NULL;
aoqi@0 194 } // End of if Phi-use input is neither Phi nor Constant
aoqi@0 195 } // End of for all inputs to Phi-use
aoqi@0 196 */
aoqi@0 197 } // End of for all uses of Phi
aoqi@0 198 } // End of for all uses of Region
aoqi@0 199
aoqi@0 200 // Only do this if the IF node is in a sane state
aoqi@0 201 if (iff->outcnt() != 2)
aoqi@0 202 return NULL;
aoqi@0 203
aoqi@0 204 // Got a hit! Do the Mondo Hack!
aoqi@0 205 //
aoqi@0 206 //ABC a1c def ghi B 1 e h A C a c d f g i
aoqi@0 207 // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi
aoqi@0 208 // cmp - 2 cmp - 2 cmp - 2
aoqi@0 209 // bool bool_c bool_x
aoqi@0 210 // if if_c if_x
aoqi@0 211 // T F T F T F
aoqi@0 212 // ..s.. ..t .. ..s.. ..t.. ..s.. ..t..
aoqi@0 213 //
aoqi@0 214 // Split the paths coming into the merge point into 2 separate groups of
aoqi@0 215 // merges. On the left will be all the paths feeding constants into the
aoqi@0 216 // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi
aoqi@0 217 // will fold up into a constant; this will let the Cmp fold up as well as
aoqi@0 218 // all the control flow. Below the original IF we have 2 control
aoqi@0 219 // dependent regions, 's' and 't'. Now we will merge the two paths
aoqi@0 220 // just prior to 's' and 't' from the two IFs. At least 1 path (and quite
aoqi@0 221 // likely 2 or more) will promptly constant fold away.
aoqi@0 222 PhaseGVN *phase = igvn;
aoqi@0 223
aoqi@0 224 // Make a region merging constants and a region merging the rest
aoqi@0 225 uint req_c = 0;
aoqi@0 226 Node* predicate_proj = NULL;
aoqi@0 227 for (uint ii = 1; ii < r->req(); ii++) {
aoqi@0 228 if (phi->in(ii) == con1) {
aoqi@0 229 req_c++;
aoqi@0 230 }
aoqi@0 231 Node* proj = PhaseIdealLoop::find_predicate(r->in(ii));
aoqi@0 232 if (proj != NULL) {
aoqi@0 233 assert(predicate_proj == NULL, "only one predicate entry expected");
aoqi@0 234 predicate_proj = proj;
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237 Node* predicate_c = NULL;
aoqi@0 238 Node* predicate_x = NULL;
aoqi@0 239 bool counted_loop = r->is_CountedLoop();
aoqi@0 240
aoqi@0 241 Node *region_c = new (igvn->C) RegionNode(req_c + 1);
aoqi@0 242 Node *phi_c = con1;
aoqi@0 243 uint len = r->req();
aoqi@0 244 Node *region_x = new (igvn->C) RegionNode(len - req_c);
aoqi@0 245 Node *phi_x = PhiNode::make_blank(region_x, phi);
aoqi@0 246 for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {
aoqi@0 247 if (phi->in(i) == con1) {
aoqi@0 248 region_c->init_req( i_c++, r ->in(i) );
aoqi@0 249 if (r->in(i) == predicate_proj)
aoqi@0 250 predicate_c = predicate_proj;
aoqi@0 251 } else {
aoqi@0 252 region_x->init_req( i_x, r ->in(i) );
aoqi@0 253 phi_x ->init_req( i_x++, phi->in(i) );
aoqi@0 254 if (r->in(i) == predicate_proj)
aoqi@0 255 predicate_x = predicate_proj;
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258 if (predicate_c != NULL && (req_c > 1)) {
aoqi@0 259 assert(predicate_x == NULL, "only one predicate entry expected");
aoqi@0 260 predicate_c = NULL; // Do not clone predicate below merge point
aoqi@0 261 }
aoqi@0 262 if (predicate_x != NULL && ((len - req_c) > 2)) {
aoqi@0 263 assert(predicate_c == NULL, "only one predicate entry expected");
aoqi@0 264 predicate_x = NULL; // Do not clone predicate below merge point
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 // Register the new RegionNodes but do not transform them. Cannot
aoqi@0 268 // transform until the entire Region/Phi conglomerate has been hacked
aoqi@0 269 // as a single huge transform.
aoqi@0 270 igvn->register_new_node_with_optimizer( region_c );
aoqi@0 271 igvn->register_new_node_with_optimizer( region_x );
aoqi@0 272 // Prevent the untimely death of phi_x. Currently he has no uses. He is
aoqi@0 273 // about to get one. If this only use goes away, then phi_x will look dead.
aoqi@0 274 // However, he will be picking up some more uses down below.
aoqi@0 275 Node *hook = new (igvn->C) Node(4);
aoqi@0 276 hook->init_req(0, phi_x);
aoqi@0 277 hook->init_req(1, phi_c);
aoqi@0 278 phi_x = phase->transform( phi_x );
aoqi@0 279
aoqi@0 280 // Make the compare
aoqi@0 281 Node *cmp_c = phase->makecon(t);
aoqi@0 282 Node *cmp_x = cmp->clone();
aoqi@0 283 cmp_x->set_req(1,phi_x);
aoqi@0 284 cmp_x->set_req(2,con2);
aoqi@0 285 cmp_x = phase->transform(cmp_x);
aoqi@0 286 // Make the bool
aoqi@0 287 Node *b_c = phase->transform(new (igvn->C) BoolNode(cmp_c,b->_test._test));
aoqi@0 288 Node *b_x = phase->transform(new (igvn->C) BoolNode(cmp_x,b->_test._test));
aoqi@0 289 // Make the IfNode
aoqi@0 290 IfNode *iff_c = new (igvn->C) IfNode(region_c,b_c,iff->_prob,iff->_fcnt);
aoqi@0 291 igvn->set_type_bottom(iff_c);
aoqi@0 292 igvn->_worklist.push(iff_c);
aoqi@0 293 hook->init_req(2, iff_c);
aoqi@0 294
aoqi@0 295 IfNode *iff_x = new (igvn->C) IfNode(region_x,b_x,iff->_prob, iff->_fcnt);
aoqi@0 296 igvn->set_type_bottom(iff_x);
aoqi@0 297 igvn->_worklist.push(iff_x);
aoqi@0 298 hook->init_req(3, iff_x);
aoqi@0 299
aoqi@0 300 // Make the true/false arms
aoqi@0 301 Node *iff_c_t = phase->transform(new (igvn->C) IfTrueNode (iff_c));
aoqi@0 302 Node *iff_c_f = phase->transform(new (igvn->C) IfFalseNode(iff_c));
aoqi@0 303 if (predicate_c != NULL) {
aoqi@0 304 assert(predicate_x == NULL, "only one predicate entry expected");
aoqi@0 305 // Clone loop predicates to each path
aoqi@0 306 iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
aoqi@0 307 iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
aoqi@0 308 }
aoqi@0 309 Node *iff_x_t = phase->transform(new (igvn->C) IfTrueNode (iff_x));
aoqi@0 310 Node *iff_x_f = phase->transform(new (igvn->C) IfFalseNode(iff_x));
aoqi@0 311 if (predicate_x != NULL) {
aoqi@0 312 assert(predicate_c == NULL, "only one predicate entry expected");
aoqi@0 313 // Clone loop predicates to each path
aoqi@0 314 iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
aoqi@0 315 iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 // Merge the TRUE paths
aoqi@0 319 Node *region_s = new (igvn->C) RegionNode(3);
aoqi@0 320 igvn->_worklist.push(region_s);
aoqi@0 321 region_s->init_req(1, iff_c_t);
aoqi@0 322 region_s->init_req(2, iff_x_t);
aoqi@0 323 igvn->register_new_node_with_optimizer( region_s );
aoqi@0 324
aoqi@0 325 // Merge the FALSE paths
aoqi@0 326 Node *region_f = new (igvn->C) RegionNode(3);
aoqi@0 327 igvn->_worklist.push(region_f);
aoqi@0 328 region_f->init_req(1, iff_c_f);
aoqi@0 329 region_f->init_req(2, iff_x_f);
aoqi@0 330 igvn->register_new_node_with_optimizer( region_f );
aoqi@0 331
aoqi@0 332 igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.
aoqi@0 333 cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead
aoqi@0 334 cmp->set_req(2,NULL);
aoqi@0 335 // Check for all uses of the Phi and give them a new home.
aoqi@0 336 // The 'cmp' got cloned, but CastPP/IIs need to be moved.
aoqi@0 337 Node *phi_s = NULL; // do not construct unless needed
aoqi@0 338 Node *phi_f = NULL; // do not construct unless needed
aoqi@0 339 for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {
aoqi@0 340 Node* v = phi->last_out(i2);// User of the phi
aoqi@0 341 igvn->rehash_node_delayed(v); // Have to fixup other Phi users
aoqi@0 342 uint vop = v->Opcode();
aoqi@0 343 Node *proj = NULL;
aoqi@0 344 if( vop == Op_Phi ) { // Remote merge point
aoqi@0 345 Node *r = v->in(0);
aoqi@0 346 for (uint i3 = 1; i3 < r->req(); i3++)
aoqi@0 347 if (r->in(i3) && r->in(i3)->in(0) == iff) {
aoqi@0 348 proj = r->in(i3);
aoqi@0 349 break;
aoqi@0 350 }
aoqi@0 351 } else if( v->is_ConstraintCast() ) {
aoqi@0 352 proj = v->in(0); // Controlling projection
aoqi@0 353 } else {
aoqi@0 354 assert( 0, "do not know how to handle this guy" );
aoqi@0 355 }
aoqi@0 356
aoqi@0 357 Node *proj_path_data, *proj_path_ctrl;
aoqi@0 358 if( proj->Opcode() == Op_IfTrue ) {
aoqi@0 359 if( phi_s == NULL ) {
aoqi@0 360 // Only construct phi_s if needed, otherwise provides
aoqi@0 361 // interfering use.
aoqi@0 362 phi_s = PhiNode::make_blank(region_s,phi);
aoqi@0 363 phi_s->init_req( 1, phi_c );
aoqi@0 364 phi_s->init_req( 2, phi_x );
aoqi@0 365 hook->add_req(phi_s);
aoqi@0 366 phi_s = phase->transform(phi_s);
aoqi@0 367 }
aoqi@0 368 proj_path_data = phi_s;
aoqi@0 369 proj_path_ctrl = region_s;
aoqi@0 370 } else {
aoqi@0 371 if( phi_f == NULL ) {
aoqi@0 372 // Only construct phi_f if needed, otherwise provides
aoqi@0 373 // interfering use.
aoqi@0 374 phi_f = PhiNode::make_blank(region_f,phi);
aoqi@0 375 phi_f->init_req( 1, phi_c );
aoqi@0 376 phi_f->init_req( 2, phi_x );
aoqi@0 377 hook->add_req(phi_f);
aoqi@0 378 phi_f = phase->transform(phi_f);
aoqi@0 379 }
aoqi@0 380 proj_path_data = phi_f;
aoqi@0 381 proj_path_ctrl = region_f;
aoqi@0 382 }
aoqi@0 383
aoqi@0 384 // Fixup 'v' for for the split
aoqi@0 385 if( vop == Op_Phi ) { // Remote merge point
aoqi@0 386 uint i;
aoqi@0 387 for( i = 1; i < v->req(); i++ )
aoqi@0 388 if( v->in(i) == phi )
aoqi@0 389 break;
aoqi@0 390 v->set_req(i, proj_path_data );
aoqi@0 391 } else if( v->is_ConstraintCast() ) {
aoqi@0 392 v->set_req(0, proj_path_ctrl );
aoqi@0 393 v->set_req(1, proj_path_data );
aoqi@0 394 } else
aoqi@0 395 ShouldNotReachHere();
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 // Now replace the original iff's True/False with region_s/region_t.
aoqi@0 399 // This makes the original iff go dead.
aoqi@0 400 for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) {
aoqi@0 401 Node* p = iff->last_out(i3);
aoqi@0 402 assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" );
aoqi@0 403 Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f;
aoqi@0 404 // Replace p with u
aoqi@0 405 igvn->add_users_to_worklist(p);
aoqi@0 406 for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) {
aoqi@0 407 Node* x = p->last_out(l);
aoqi@0 408 igvn->hash_delete(x);
aoqi@0 409 uint uses_found = 0;
aoqi@0 410 for( uint j = 0; j < x->req(); j++ ) {
aoqi@0 411 if( x->in(j) == p ) {
aoqi@0 412 x->set_req(j, u);
aoqi@0 413 uses_found++;
aoqi@0 414 }
aoqi@0 415 }
aoqi@0 416 l -= uses_found; // we deleted 1 or more copies of this edge
aoqi@0 417 }
aoqi@0 418 igvn->remove_dead_node(p);
aoqi@0 419 }
aoqi@0 420
aoqi@0 421 // Force the original merge dead
aoqi@0 422 igvn->hash_delete(r);
aoqi@0 423 // First, remove region's dead users.
aoqi@0 424 for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) {
aoqi@0 425 Node* u = r->last_out(l);
aoqi@0 426 if( u == r ) {
aoqi@0 427 r->set_req(0, NULL);
aoqi@0 428 } else {
aoqi@0 429 assert(u->outcnt() == 0, "only dead users");
aoqi@0 430 igvn->remove_dead_node(u);
aoqi@0 431 }
aoqi@0 432 l -= 1;
aoqi@0 433 }
aoqi@0 434 igvn->remove_dead_node(r);
aoqi@0 435
aoqi@0 436 // Now remove the bogus extra edges used to keep things alive
aoqi@0 437 igvn->remove_dead_node( hook );
aoqi@0 438
aoqi@0 439 // Must return either the original node (now dead) or a new node
aoqi@0 440 // (Do not return a top here, since that would break the uniqueness of top.)
aoqi@0 441 return new (igvn->C) ConINode(TypeInt::ZERO);
aoqi@0 442 }
aoqi@0 443
aoqi@0 444 //------------------------------is_range_check---------------------------------
aoqi@0 445 // Return 0 if not a range check. Return 1 if a range check and set index and
aoqi@0 446 // offset. Return 2 if we had to negate the test. Index is NULL if the check
aoqi@0 447 // is versus a constant.
aoqi@0 448 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {
aoqi@0 449 Node* b = in(1);
aoqi@0 450 if (b == NULL || !b->is_Bool()) return 0;
aoqi@0 451 BoolNode* bn = b->as_Bool();
aoqi@0 452 Node* cmp = bn->in(1);
aoqi@0 453 if (cmp == NULL) return 0;
aoqi@0 454 if (cmp->Opcode() != Op_CmpU) return 0;
aoqi@0 455
aoqi@0 456 Node* l = cmp->in(1);
aoqi@0 457 Node* r = cmp->in(2);
aoqi@0 458 int flip_test = 1;
aoqi@0 459 if (bn->_test._test == BoolTest::le) {
aoqi@0 460 l = cmp->in(2);
aoqi@0 461 r = cmp->in(1);
aoqi@0 462 flip_test = 2;
aoqi@0 463 } else if (bn->_test._test != BoolTest::lt) {
aoqi@0 464 return 0;
aoqi@0 465 }
aoqi@0 466 if (l->is_top()) return 0; // Top input means dead test
aoqi@0 467 if (r->Opcode() != Op_LoadRange) return 0;
aoqi@0 468
aoqi@0 469 // We have recognized one of these forms:
aoqi@0 470 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...
aoqi@0 471 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...
aoqi@0 472
aoqi@0 473 // Make sure it's a real range check by requiring an uncommon trap
aoqi@0 474 // along the OOB path. Otherwise, it's possible that the user wrote
aoqi@0 475 // something which optimized to look like a range check but behaves
aoqi@0 476 // in some other way.
aoqi@0 477 Node* iftrap = proj_out(flip_test == 2 ? true : false);
aoqi@0 478 bool found_trap = false;
aoqi@0 479 if (iftrap != NULL) {
aoqi@0 480 Node* u = iftrap->unique_ctrl_out();
aoqi@0 481 if (u != NULL) {
aoqi@0 482 // It could be a merge point (Region) for uncommon trap.
aoqi@0 483 if (u->is_Region()) {
aoqi@0 484 Node* c = u->unique_ctrl_out();
aoqi@0 485 if (c != NULL) {
aoqi@0 486 iftrap = u;
aoqi@0 487 u = c;
aoqi@0 488 }
aoqi@0 489 }
aoqi@0 490 if (u->in(0) == iftrap && u->is_CallStaticJava()) {
aoqi@0 491 int req = u->as_CallStaticJava()->uncommon_trap_request();
aoqi@0 492 if (Deoptimization::trap_request_reason(req) ==
aoqi@0 493 Deoptimization::Reason_range_check) {
aoqi@0 494 found_trap = true;
aoqi@0 495 }
aoqi@0 496 }
aoqi@0 497 }
aoqi@0 498 }
aoqi@0 499 if (!found_trap) return 0; // sorry, no cigar
aoqi@0 500
aoqi@0 501 // Look for index+offset form
aoqi@0 502 Node* ind = l;
aoqi@0 503 jint off = 0;
aoqi@0 504 if (l->is_top()) {
aoqi@0 505 return 0;
aoqi@0 506 } else if (l->is_Add()) {
aoqi@0 507 if ((off = l->in(1)->find_int_con(0)) != 0) {
aoqi@0 508 ind = l->in(2);
aoqi@0 509 } else if ((off = l->in(2)->find_int_con(0)) != 0) {
aoqi@0 510 ind = l->in(1);
aoqi@0 511 }
aoqi@0 512 } else if ((off = l->find_int_con(-1)) >= 0) {
aoqi@0 513 // constant offset with no variable index
aoqi@0 514 ind = NULL;
aoqi@0 515 } else {
aoqi@0 516 // variable index with no constant offset (or dead negative index)
aoqi@0 517 off = 0;
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 // Return all the values:
aoqi@0 521 index = ind;
aoqi@0 522 offset = off;
aoqi@0 523 range = r;
aoqi@0 524 return flip_test;
aoqi@0 525 }
aoqi@0 526
aoqi@0 527 //------------------------------adjust_check-----------------------------------
aoqi@0 528 // Adjust (widen) a prior range check
aoqi@0 529 static void adjust_check(Node* proj, Node* range, Node* index,
aoqi@0 530 int flip, jint off_lo, PhaseIterGVN* igvn) {
aoqi@0 531 PhaseGVN *gvn = igvn;
aoqi@0 532 // Break apart the old check
aoqi@0 533 Node *iff = proj->in(0);
aoqi@0 534 Node *bol = iff->in(1);
aoqi@0 535 if( bol->is_top() ) return; // In case a partially dead range check appears
aoqi@0 536 // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode
aoqi@0 537 DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )
aoqi@0 538 if( !bol->is_Bool() ) return;
aoqi@0 539
aoqi@0 540 Node *cmp = bol->in(1);
aoqi@0 541 // Compute a new check
aoqi@0 542 Node *new_add = gvn->intcon(off_lo);
aoqi@0 543 if( index ) {
aoqi@0 544 new_add = off_lo ? gvn->transform(new (gvn->C) AddINode( index, new_add )) : index;
aoqi@0 545 }
aoqi@0 546 Node *new_cmp = (flip == 1)
aoqi@0 547 ? new (gvn->C) CmpUNode( new_add, range )
aoqi@0 548 : new (gvn->C) CmpUNode( range, new_add );
aoqi@0 549 new_cmp = gvn->transform(new_cmp);
aoqi@0 550 // See if no need to adjust the existing check
aoqi@0 551 if( new_cmp == cmp ) return;
aoqi@0 552 // Else, adjust existing check
aoqi@0 553 Node *new_bol = gvn->transform( new (gvn->C) BoolNode( new_cmp, bol->as_Bool()->_test._test ) );
aoqi@0 554 igvn->rehash_node_delayed( iff );
aoqi@0 555 iff->set_req_X( 1, new_bol, igvn );
aoqi@0 556 }
aoqi@0 557
aoqi@0 558 //------------------------------up_one_dom-------------------------------------
aoqi@0 559 // Walk up the dominator tree one step. Return NULL at root or true
aoqi@0 560 // complex merges. Skips through small diamonds.
aoqi@0 561 Node* IfNode::up_one_dom(Node *curr, bool linear_only) {
aoqi@0 562 Node *dom = curr->in(0);
aoqi@0 563 if( !dom ) // Found a Region degraded to a copy?
aoqi@0 564 return curr->nonnull_req(); // Skip thru it
aoqi@0 565
aoqi@0 566 if( curr != dom ) // Normal walk up one step?
aoqi@0 567 return dom;
aoqi@0 568
aoqi@0 569 // Use linear_only if we are still parsing, since we cannot
aoqi@0 570 // trust the regions to be fully filled in.
aoqi@0 571 if (linear_only)
aoqi@0 572 return NULL;
aoqi@0 573
aoqi@0 574 if( dom->is_Root() )
aoqi@0 575 return NULL;
aoqi@0 576
aoqi@0 577 // Else hit a Region. Check for a loop header
aoqi@0 578 if( dom->is_Loop() )
aoqi@0 579 return dom->in(1); // Skip up thru loops
aoqi@0 580
aoqi@0 581 // Check for small diamonds
aoqi@0 582 Node *din1, *din2, *din3, *din4;
aoqi@0 583 if( dom->req() == 3 && // 2-path merge point
aoqi@0 584 (din1 = dom ->in(1)) && // Left path exists
aoqi@0 585 (din2 = dom ->in(2)) && // Right path exists
aoqi@0 586 (din3 = din1->in(0)) && // Left path up one
aoqi@0 587 (din4 = din2->in(0)) ) { // Right path up one
aoqi@0 588 if( din3->is_Call() && // Handle a slow-path call on either arm
aoqi@0 589 (din3 = din3->in(0)) )
aoqi@0 590 din3 = din3->in(0);
aoqi@0 591 if( din4->is_Call() && // Handle a slow-path call on either arm
aoqi@0 592 (din4 = din4->in(0)) )
aoqi@0 593 din4 = din4->in(0);
aoqi@0 594 if( din3 == din4 && din3->is_If() )
aoqi@0 595 return din3; // Skip around diamonds
aoqi@0 596 }
aoqi@0 597
aoqi@0 598 // Give up the search at true merges
aoqi@0 599 return NULL; // Dead loop? Or hit root?
aoqi@0 600 }
aoqi@0 601
aoqi@0 602
aoqi@0 603 //------------------------------filtered_int_type--------------------------------
aoqi@0 604 // Return a possibly more restrictive type for val based on condition control flow for an if
aoqi@0 605 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) {
aoqi@0 606 assert(if_proj &&
aoqi@0 607 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
aoqi@0 608 if (if_proj->in(0) && if_proj->in(0)->is_If()) {
aoqi@0 609 IfNode* iff = if_proj->in(0)->as_If();
aoqi@0 610 if (iff->in(1) && iff->in(1)->is_Bool()) {
aoqi@0 611 BoolNode* bol = iff->in(1)->as_Bool();
aoqi@0 612 if (bol->in(1) && bol->in(1)->is_Cmp()) {
aoqi@0 613 const CmpNode* cmp = bol->in(1)->as_Cmp();
aoqi@0 614 if (cmp->in(1) == val) {
aoqi@0 615 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
aoqi@0 616 if (cmp2_t != NULL) {
aoqi@0 617 jint lo = cmp2_t->_lo;
aoqi@0 618 jint hi = cmp2_t->_hi;
aoqi@0 619 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
aoqi@0 620 switch (msk) {
aoqi@0 621 case BoolTest::ne:
aoqi@0 622 // Can't refine type
aoqi@0 623 return NULL;
aoqi@0 624 case BoolTest::eq:
aoqi@0 625 return cmp2_t;
aoqi@0 626 case BoolTest::lt:
aoqi@0 627 lo = TypeInt::INT->_lo;
aoqi@0 628 if (hi - 1 < hi) {
aoqi@0 629 hi = hi - 1;
aoqi@0 630 }
aoqi@0 631 break;
aoqi@0 632 case BoolTest::le:
aoqi@0 633 lo = TypeInt::INT->_lo;
aoqi@0 634 break;
aoqi@0 635 case BoolTest::gt:
aoqi@0 636 if (lo + 1 > lo) {
aoqi@0 637 lo = lo + 1;
aoqi@0 638 }
aoqi@0 639 hi = TypeInt::INT->_hi;
aoqi@0 640 break;
aoqi@0 641 case BoolTest::ge:
aoqi@0 642 // lo unchanged
aoqi@0 643 hi = TypeInt::INT->_hi;
aoqi@0 644 break;
aoqi@0 645 }
aoqi@0 646 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
aoqi@0 647 return rtn_t;
aoqi@0 648 }
aoqi@0 649 }
aoqi@0 650 }
aoqi@0 651 }
aoqi@0 652 }
aoqi@0 653 return NULL;
aoqi@0 654 }
aoqi@0 655
aoqi@0 656 //------------------------------fold_compares----------------------------
aoqi@0 657 // See if a pair of CmpIs can be converted into a CmpU. In some cases
aoqi@0 658 // the direction of this if is determined by the preceding if so it
aoqi@0 659 // can be eliminate entirely. Given an if testing (CmpI n c) check
aoqi@0 660 // for an immediately control dependent if that is testing (CmpI n c2)
aoqi@0 661 // and has one projection leading to this if and the other projection
aoqi@0 662 // leading to a region that merges one of this ifs control
aoqi@0 663 // projections.
aoqi@0 664 //
aoqi@0 665 // If
aoqi@0 666 // / |
aoqi@0 667 // / |
aoqi@0 668 // / |
aoqi@0 669 // If |
aoqi@0 670 // /\ |
aoqi@0 671 // / \ |
aoqi@0 672 // / \ |
aoqi@0 673 // / Region
aoqi@0 674 //
aoqi@0 675 Node* IfNode::fold_compares(PhaseGVN* phase) {
aoqi@0 676 if (Opcode() != Op_If) return NULL;
aoqi@0 677
aoqi@0 678 Node* this_cmp = in(1)->in(1);
aoqi@0 679 if (this_cmp != NULL && this_cmp->Opcode() == Op_CmpI &&
aoqi@0 680 this_cmp->in(2)->is_Con() && this_cmp->in(2) != phase->C->top()) {
aoqi@0 681 Node* ctrl = in(0);
aoqi@0 682 BoolNode* this_bool = in(1)->as_Bool();
aoqi@0 683 Node* n = this_cmp->in(1);
aoqi@0 684 int hi = this_cmp->in(2)->get_int();
aoqi@0 685 if (ctrl != NULL && ctrl->is_Proj() && ctrl->outcnt() == 1 &&
aoqi@0 686 ctrl->in(0)->is_If() &&
aoqi@0 687 ctrl->in(0)->outcnt() == 2 &&
aoqi@0 688 ctrl->in(0)->in(1)->is_Bool() &&
aoqi@0 689 ctrl->in(0)->in(1)->in(1)->Opcode() == Op_CmpI &&
aoqi@0 690 ctrl->in(0)->in(1)->in(1)->in(2)->is_Con() &&
aoqi@0 691 ctrl->in(0)->in(1)->in(1)->in(2) != phase->C->top() &&
aoqi@0 692 ctrl->in(0)->in(1)->in(1)->in(1) == n) {
aoqi@0 693 IfNode* dom_iff = ctrl->in(0)->as_If();
aoqi@0 694 Node* otherproj = dom_iff->proj_out(!ctrl->as_Proj()->_con);
aoqi@0 695 if (otherproj->outcnt() == 1 && otherproj->unique_out()->is_Region() &&
aoqi@0 696 this_bool->_test._test != BoolTest::ne && this_bool->_test._test != BoolTest::eq) {
aoqi@0 697 // Identify which proj goes to the region and which continues on
aoqi@0 698 RegionNode* region = otherproj->unique_out()->as_Region();
aoqi@0 699 Node* success = NULL;
aoqi@0 700 Node* fail = NULL;
aoqi@0 701 for (int i = 0; i < 2; i++) {
aoqi@0 702 Node* proj = proj_out(i);
aoqi@0 703 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
aoqi@0 704 success = proj;
aoqi@0 705 } else if (fail == NULL) {
aoqi@0 706 fail = proj;
aoqi@0 707 } else {
aoqi@0 708 success = fail = NULL;
aoqi@0 709 }
aoqi@0 710 }
aoqi@0 711 if (success != NULL && fail != NULL && !region->has_phi()) {
aoqi@0 712 int lo = dom_iff->in(1)->in(1)->in(2)->get_int();
aoqi@0 713 BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
aoqi@0 714 Node* dom_cmp = dom_bool->in(1);
aoqi@0 715 const TypeInt* failtype = filtered_int_type(phase, n, ctrl);
aoqi@0 716 if (failtype != NULL) {
aoqi@0 717 const TypeInt* type2 = filtered_int_type(phase, n, fail);
aoqi@0 718 if (type2 != NULL) {
aoqi@0 719 failtype = failtype->join(type2)->is_int();
aoqi@0 720 } else {
aoqi@0 721 failtype = NULL;
aoqi@0 722 }
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 if (failtype != NULL &&
aoqi@0 726 dom_bool->_test._test != BoolTest::ne && dom_bool->_test._test != BoolTest::eq) {
aoqi@0 727 int bound = failtype->_hi - failtype->_lo + 1;
aoqi@0 728 if (failtype->_hi != max_jint && failtype->_lo != min_jint && bound > 1) {
aoqi@0 729 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) hi)
aoqi@0 730 BoolTest::mask cond = fail->as_Proj()->_con ? BoolTest::lt : BoolTest::ge;
aoqi@0 731 Node* adjusted = phase->transform(new (phase->C) SubINode(n, phase->intcon(failtype->_lo)));
aoqi@0 732 Node* newcmp = phase->transform(new (phase->C) CmpUNode(adjusted, phase->intcon(bound)));
aoqi@0 733 Node* newbool = phase->transform(new (phase->C) BoolNode(newcmp, cond));
aoqi@0 734 phase->is_IterGVN()->replace_input_of(dom_iff, 1, phase->intcon(ctrl->as_Proj()->_con));
aoqi@0 735 phase->hash_delete(this);
aoqi@0 736 set_req(1, newbool);
aoqi@0 737 return this;
aoqi@0 738 }
aoqi@0 739 if (failtype->_lo > failtype->_hi) {
aoqi@0 740 // previous if determines the result of this if so
aoqi@0 741 // replace Bool with constant
aoqi@0 742 phase->hash_delete(this);
aoqi@0 743 set_req(1, phase->intcon(success->as_Proj()->_con));
aoqi@0 744 return this;
aoqi@0 745 }
aoqi@0 746 }
aoqi@0 747 }
aoqi@0 748 }
aoqi@0 749 }
aoqi@0 750 }
aoqi@0 751 return NULL;
aoqi@0 752 }
aoqi@0 753
aoqi@0 754 //------------------------------remove_useless_bool----------------------------
aoqi@0 755 // Check for people making a useless boolean: things like
aoqi@0 756 // if( (x < y ? true : false) ) { ... }
aoqi@0 757 // Replace with if( x < y ) { ... }
aoqi@0 758 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
aoqi@0 759 Node *i1 = iff->in(1);
aoqi@0 760 if( !i1->is_Bool() ) return NULL;
aoqi@0 761 BoolNode *bol = i1->as_Bool();
aoqi@0 762
aoqi@0 763 Node *cmp = bol->in(1);
aoqi@0 764 if( cmp->Opcode() != Op_CmpI ) return NULL;
aoqi@0 765
aoqi@0 766 // Must be comparing against a bool
aoqi@0 767 const Type *cmp2_t = phase->type( cmp->in(2) );
aoqi@0 768 if( cmp2_t != TypeInt::ZERO &&
aoqi@0 769 cmp2_t != TypeInt::ONE )
aoqi@0 770 return NULL;
aoqi@0 771
aoqi@0 772 // Find a prior merge point merging the boolean
aoqi@0 773 i1 = cmp->in(1);
aoqi@0 774 if( !i1->is_Phi() ) return NULL;
aoqi@0 775 PhiNode *phi = i1->as_Phi();
aoqi@0 776 if( phase->type( phi ) != TypeInt::BOOL )
aoqi@0 777 return NULL;
aoqi@0 778
aoqi@0 779 // Check for diamond pattern
aoqi@0 780 int true_path = phi->is_diamond_phi();
aoqi@0 781 if( true_path == 0 ) return NULL;
aoqi@0 782
aoqi@0 783 // Make sure that iff and the control of the phi are different. This
aoqi@0 784 // should really only happen for dead control flow since it requires
aoqi@0 785 // an illegal cycle.
aoqi@0 786 if (phi->in(0)->in(1)->in(0) == iff) return NULL;
aoqi@0 787
aoqi@0 788 // phi->region->if_proj->ifnode->bool->cmp
aoqi@0 789 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
aoqi@0 790
aoqi@0 791 // Now get the 'sense' of the test correct so we can plug in
aoqi@0 792 // either iff2->in(1) or its complement.
aoqi@0 793 int flip = 0;
aoqi@0 794 if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
aoqi@0 795 else if( bol->_test._test != BoolTest::eq ) return NULL;
aoqi@0 796 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
aoqi@0 797
aoqi@0 798 const Type *phi1_t = phase->type( phi->in(1) );
aoqi@0 799 const Type *phi2_t = phase->type( phi->in(2) );
aoqi@0 800 // Check for Phi(0,1) and flip
aoqi@0 801 if( phi1_t == TypeInt::ZERO ) {
aoqi@0 802 if( phi2_t != TypeInt::ONE ) return NULL;
aoqi@0 803 flip = 1-flip;
aoqi@0 804 } else {
aoqi@0 805 // Check for Phi(1,0)
aoqi@0 806 if( phi1_t != TypeInt::ONE ) return NULL;
aoqi@0 807 if( phi2_t != TypeInt::ZERO ) return NULL;
aoqi@0 808 }
aoqi@0 809 if( true_path == 2 ) {
aoqi@0 810 flip = 1-flip;
aoqi@0 811 }
aoqi@0 812
aoqi@0 813 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
aoqi@0 814 assert(new_bol != iff->in(1), "must make progress");
aoqi@0 815 iff->set_req(1, new_bol);
aoqi@0 816 // Intervening diamond probably goes dead
aoqi@0 817 phase->C->set_major_progress();
aoqi@0 818 return iff;
aoqi@0 819 }
aoqi@0 820
aoqi@0 821 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
aoqi@0 822
aoqi@0 823 //------------------------------Ideal------------------------------------------
aoqi@0 824 // Return a node which is more "ideal" than the current node. Strip out
aoqi@0 825 // control copies
aoqi@0 826 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 827 if (remove_dead_region(phase, can_reshape)) return this;
aoqi@0 828 // No Def-Use info?
aoqi@0 829 if (!can_reshape) return NULL;
aoqi@0 830 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 831
aoqi@0 832 // Don't bother trying to transform a dead if
aoqi@0 833 if (in(0)->is_top()) return NULL;
aoqi@0 834 // Don't bother trying to transform an if with a dead test
aoqi@0 835 if (in(1)->is_top()) return NULL;
aoqi@0 836 // Another variation of a dead test
aoqi@0 837 if (in(1)->is_Con()) return NULL;
aoqi@0 838 // Another variation of a dead if
aoqi@0 839 if (outcnt() < 2) return NULL;
aoqi@0 840
aoqi@0 841 // Canonicalize the test.
aoqi@0 842 Node* idt_if = idealize_test(phase, this);
aoqi@0 843 if (idt_if != NULL) return idt_if;
aoqi@0 844
aoqi@0 845 // Try to split the IF
aoqi@0 846 Node *s = split_if(this, igvn);
aoqi@0 847 if (s != NULL) return s;
aoqi@0 848
aoqi@0 849 // Check for people making a useless boolean: things like
aoqi@0 850 // if( (x < y ? true : false) ) { ... }
aoqi@0 851 // Replace with if( x < y ) { ... }
aoqi@0 852 Node *bol2 = remove_useless_bool(this, phase);
aoqi@0 853 if( bol2 ) return bol2;
aoqi@0 854
aoqi@0 855 // Setup to scan up the CFG looking for a dominating test
aoqi@0 856 Node *dom = in(0);
aoqi@0 857 Node *prev_dom = this;
aoqi@0 858
aoqi@0 859 // Check for range-check vs other kinds of tests
aoqi@0 860 Node *index1, *range1;
aoqi@0 861 jint offset1;
aoqi@0 862 int flip1 = is_range_check(range1, index1, offset1);
aoqi@0 863 if( flip1 ) {
aoqi@0 864 Node *first_prev_dom = NULL;
aoqi@0 865
aoqi@0 866 // Try to remove extra range checks. All 'up_one_dom' gives up at merges
aoqi@0 867 // so all checks we inspect post-dominate the top-most check we find.
aoqi@0 868 // If we are going to fail the current check and we reach the top check
aoqi@0 869 // then we are guaranteed to fail, so just start interpreting there.
aoqi@0 870 // We 'expand' the top 2 range checks to include all post-dominating
aoqi@0 871 // checks.
aoqi@0 872
aoqi@0 873 // The top 2 range checks seen
aoqi@0 874 Node *prev_chk1 = NULL;
aoqi@0 875 Node *prev_chk2 = NULL;
aoqi@0 876 // Low and high offsets seen so far
aoqi@0 877 jint off_lo = offset1;
aoqi@0 878 jint off_hi = offset1;
aoqi@0 879
aoqi@0 880 // Scan for the top 2 checks and collect range of offsets
aoqi@0 881 for( int dist = 0; dist < 999; dist++ ) { // Range-Check scan limit
aoqi@0 882 if( dom->Opcode() == Op_If && // Not same opcode?
aoqi@0 883 prev_dom->in(0) == dom ) { // One path of test does dominate?
aoqi@0 884 if( dom == this ) return NULL; // dead loop
aoqi@0 885 // See if this is a range check
aoqi@0 886 Node *index2, *range2;
aoqi@0 887 jint offset2;
aoqi@0 888 int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
aoqi@0 889 // See if this is a _matching_ range check, checking against
aoqi@0 890 // the same array bounds.
aoqi@0 891 if( flip2 == flip1 && range2 == range1 && index2 == index1 &&
aoqi@0 892 dom->outcnt() == 2 ) {
aoqi@0 893 // Gather expanded bounds
aoqi@0 894 off_lo = MIN2(off_lo,offset2);
aoqi@0 895 off_hi = MAX2(off_hi,offset2);
aoqi@0 896 // Record top 2 range checks
aoqi@0 897 prev_chk2 = prev_chk1;
aoqi@0 898 prev_chk1 = prev_dom;
aoqi@0 899 // If we match the test exactly, then the top test covers
aoqi@0 900 // both our lower and upper bounds.
aoqi@0 901 if( dom->in(1) == in(1) )
aoqi@0 902 prev_chk2 = prev_chk1;
aoqi@0 903 }
aoqi@0 904 }
aoqi@0 905 prev_dom = dom;
aoqi@0 906 dom = up_one_dom( dom );
aoqi@0 907 if( !dom ) break;
aoqi@0 908 }
aoqi@0 909
aoqi@0 910
aoqi@0 911 // Attempt to widen the dominating range check to cover some later
aoqi@0 912 // ones. Since range checks "fail" by uncommon-trapping to the
aoqi@0 913 // interpreter, widening a check can make us speculative enter the
aoqi@0 914 // interpreter. If we see range-check deopt's, do not widen!
aoqi@0 915 if (!phase->C->allow_range_check_smearing()) return NULL;
aoqi@0 916
aoqi@0 917 // Constant indices only need to check the upper bound.
aoqi@0 918 // Non-constance indices must check both low and high.
aoqi@0 919 if( index1 ) {
aoqi@0 920 // Didn't find 2 prior covering checks, so cannot remove anything.
aoqi@0 921 if( !prev_chk2 ) return NULL;
aoqi@0 922 // 'Widen' the offsets of the 1st and 2nd covering check
aoqi@0 923 adjust_check( prev_chk1, range1, index1, flip1, off_lo, igvn );
aoqi@0 924 // Do not call adjust_check twice on the same projection
aoqi@0 925 // as the first call may have transformed the BoolNode to a ConI
aoqi@0 926 if( prev_chk1 != prev_chk2 ) {
aoqi@0 927 adjust_check( prev_chk2, range1, index1, flip1, off_hi, igvn );
aoqi@0 928 }
aoqi@0 929 // Test is now covered by prior checks, dominate it out
aoqi@0 930 prev_dom = prev_chk2;
aoqi@0 931 } else {
aoqi@0 932 // Didn't find prior covering check, so cannot remove anything.
aoqi@0 933 if( !prev_chk1 ) return NULL;
aoqi@0 934 // 'Widen' the offset of the 1st and only covering check
aoqi@0 935 adjust_check( prev_chk1, range1, index1, flip1, off_hi, igvn );
aoqi@0 936 // Test is now covered by prior checks, dominate it out
aoqi@0 937 prev_dom = prev_chk1;
aoqi@0 938 }
aoqi@0 939
aoqi@0 940
aoqi@0 941 } else { // Scan for an equivalent test
aoqi@0 942
aoqi@0 943 Node *cmp;
aoqi@0 944 int dist = 0; // Cutoff limit for search
aoqi@0 945 int op = Opcode();
aoqi@0 946 if( op == Op_If &&
aoqi@0 947 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
aoqi@0 948 if( cmp->in(2) != NULL && // make sure cmp is not already dead
aoqi@0 949 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
aoqi@0 950 dist = 64; // Limit for null-pointer scans
aoqi@0 951 } else {
aoqi@0 952 dist = 4; // Do not bother for random pointer tests
aoqi@0 953 }
aoqi@0 954 } else {
aoqi@0 955 dist = 4; // Limit for random junky scans
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 // Normal equivalent-test check.
aoqi@0 959 if( !dom ) return NULL; // Dead loop?
aoqi@0 960
aoqi@0 961 Node* result = fold_compares(phase);
aoqi@0 962 if (result != NULL) {
aoqi@0 963 return result;
aoqi@0 964 }
aoqi@0 965
aoqi@0 966 // Search up the dominator tree for an If with an identical test
aoqi@0 967 while( dom->Opcode() != op || // Not same opcode?
aoqi@0 968 dom->in(1) != in(1) || // Not same input 1?
aoqi@0 969 (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
aoqi@0 970 prev_dom->in(0) != dom ) { // One path of test does not dominate?
aoqi@0 971 if( dist < 0 ) return NULL;
aoqi@0 972
aoqi@0 973 dist--;
aoqi@0 974 prev_dom = dom;
aoqi@0 975 dom = up_one_dom( dom );
aoqi@0 976 if( !dom ) return NULL;
aoqi@0 977 }
aoqi@0 978
aoqi@0 979 // Check that we did not follow a loop back to ourselves
aoqi@0 980 if( this == dom )
aoqi@0 981 return NULL;
aoqi@0 982
aoqi@0 983 if( dist > 2 ) // Add to count of NULL checks elided
aoqi@0 984 explicit_null_checks_elided++;
aoqi@0 985
aoqi@0 986 } // End of Else scan for an equivalent test
aoqi@0 987
aoqi@0 988 // Hit! Remove this IF
aoqi@0 989 #ifndef PRODUCT
aoqi@0 990 if( TraceIterativeGVN ) {
aoqi@0 991 tty->print(" Removing IfNode: "); this->dump();
aoqi@0 992 }
aoqi@0 993 if( VerifyOpto && !phase->allow_progress() ) {
aoqi@0 994 // Found an equivalent dominating test,
aoqi@0 995 // we can not guarantee reaching a fix-point for these during iterativeGVN
aoqi@0 996 // since intervening nodes may not change.
aoqi@0 997 return NULL;
aoqi@0 998 }
aoqi@0 999 #endif
aoqi@0 1000
aoqi@0 1001 // Replace dominated IfNode
aoqi@0 1002 dominated_by( prev_dom, igvn );
aoqi@0 1003
aoqi@0 1004 // Must return either the original node (now dead) or a new node
aoqi@0 1005 // (Do not return a top here, since that would break the uniqueness of top.)
aoqi@0 1006 return new (phase->C) ConINode(TypeInt::ZERO);
aoqi@0 1007 }
aoqi@0 1008
aoqi@0 1009 //------------------------------dominated_by-----------------------------------
aoqi@0 1010 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
aoqi@0 1011 igvn->hash_delete(this); // Remove self to prevent spurious V-N
aoqi@0 1012 Node *idom = in(0);
aoqi@0 1013 // Need opcode to decide which way 'this' test goes
aoqi@0 1014 int prev_op = prev_dom->Opcode();
aoqi@0 1015 Node *top = igvn->C->top(); // Shortcut to top
aoqi@0 1016
aoqi@0 1017 // Loop predicates may have depending checks which should not
aoqi@0 1018 // be skipped. For example, range check predicate has two checks
aoqi@0 1019 // for lower and upper bounds.
aoqi@0 1020 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
aoqi@0 1021 if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate))
aoqi@0 1022 prev_dom = idom;
aoqi@0 1023
aoqi@0 1024 // Now walk the current IfNode's projections.
aoqi@0 1025 // Loop ends when 'this' has no more uses.
aoqi@0 1026 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
aoqi@0 1027 Node *ifp = last_out(i); // Get IfTrue/IfFalse
aoqi@0 1028 igvn->add_users_to_worklist(ifp);
aoqi@0 1029 // Check which projection it is and set target.
aoqi@0 1030 // Data-target is either the dominating projection of the same type
aoqi@0 1031 // or TOP if the dominating projection is of opposite type.
aoqi@0 1032 // Data-target will be used as the new control edge for the non-CFG
aoqi@0 1033 // nodes like Casts and Loads.
aoqi@0 1034 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
aoqi@0 1035 // Control-target is just the If's immediate dominator or TOP.
aoqi@0 1036 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top;
aoqi@0 1037
aoqi@0 1038 // For each child of an IfTrue/IfFalse projection, reroute.
aoqi@0 1039 // Loop ends when projection has no more uses.
aoqi@0 1040 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
aoqi@0 1041 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
aoqi@0 1042 if( !s->depends_only_on_test() ) {
aoqi@0 1043 // Find the control input matching this def-use edge.
aoqi@0 1044 // For Regions it may not be in slot 0.
aoqi@0 1045 uint l;
aoqi@0 1046 for( l = 0; s->in(l) != ifp; l++ ) { }
aoqi@0 1047 igvn->replace_input_of(s, l, ctrl_target);
aoqi@0 1048 } else { // Else, for control producers,
aoqi@0 1049 igvn->replace_input_of(s, 0, data_target); // Move child to data-target
aoqi@0 1050 }
aoqi@0 1051 } // End for each child of a projection
aoqi@0 1052
aoqi@0 1053 igvn->remove_dead_node(ifp);
aoqi@0 1054 } // End for each IfTrue/IfFalse child of If
aoqi@0 1055
aoqi@0 1056 // Kill the IfNode
aoqi@0 1057 igvn->remove_dead_node(this);
aoqi@0 1058 }
aoqi@0 1059
aoqi@0 1060 //------------------------------Identity---------------------------------------
aoqi@0 1061 // If the test is constant & we match, then we are the input Control
aoqi@0 1062 Node *IfTrueNode::Identity( PhaseTransform *phase ) {
aoqi@0 1063 // Can only optimize if cannot go the other way
aoqi@0 1064 const TypeTuple *t = phase->type(in(0))->is_tuple();
aoqi@0 1065 return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFTRUE )
aoqi@0 1066 ? in(0)->in(0) // IfNode control
aoqi@0 1067 : this; // no progress
aoqi@0 1068 }
aoqi@0 1069
aoqi@0 1070 //------------------------------dump_spec--------------------------------------
aoqi@0 1071 #ifndef PRODUCT
aoqi@0 1072 void IfNode::dump_spec(outputStream *st) const {
aoqi@0 1073 st->print("P=%f, C=%f",_prob,_fcnt);
aoqi@0 1074 }
aoqi@0 1075 #endif
aoqi@0 1076
aoqi@0 1077 //------------------------------idealize_test----------------------------------
aoqi@0 1078 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and
aoqi@0 1079 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms
aoqi@0 1080 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as
aoqi@0 1081 // needed.
aoqi@0 1082 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
aoqi@0 1083 assert(iff->in(0) != NULL, "If must be live");
aoqi@0 1084
aoqi@0 1085 if (iff->outcnt() != 2) return NULL; // Malformed projections.
aoqi@0 1086 Node* old_if_f = iff->proj_out(false);
aoqi@0 1087 Node* old_if_t = iff->proj_out(true);
aoqi@0 1088
aoqi@0 1089 // CountedLoopEnds want the back-control test to be TRUE, irregardless of
aoqi@0 1090 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition
aoqi@0 1091 // happens in count-down loops
aoqi@0 1092 if (iff->is_CountedLoopEnd()) return NULL;
aoqi@0 1093 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests
aoqi@0 1094 BoolNode *b = iff->in(1)->as_Bool();
aoqi@0 1095 BoolTest bt = b->_test;
aoqi@0 1096 // Test already in good order?
aoqi@0 1097 if( bt.is_canonical() )
aoqi@0 1098 return NULL;
aoqi@0 1099
aoqi@0 1100 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and
aoqi@0 1101 // cloning the IfNode.
aoqi@0 1102 Node* new_b = phase->transform( new (phase->C) BoolNode(b->in(1), bt.negate()) );
aoqi@0 1103 if( !new_b->is_Bool() ) return NULL;
aoqi@0 1104 b = new_b->as_Bool();
aoqi@0 1105
aoqi@0 1106 PhaseIterGVN *igvn = phase->is_IterGVN();
aoqi@0 1107 assert( igvn, "Test is not canonical in parser?" );
aoqi@0 1108
aoqi@0 1109 // The IF node never really changes, but it needs to be cloned
aoqi@0 1110 iff = new (phase->C) IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);
aoqi@0 1111
aoqi@0 1112 Node *prior = igvn->hash_find_insert(iff);
aoqi@0 1113 if( prior ) {
aoqi@0 1114 igvn->remove_dead_node(iff);
aoqi@0 1115 iff = (IfNode*)prior;
aoqi@0 1116 } else {
aoqi@0 1117 // Cannot call transform on it just yet
aoqi@0 1118 igvn->set_type_bottom(iff);
aoqi@0 1119 }
aoqi@0 1120 igvn->_worklist.push(iff);
aoqi@0 1121
aoqi@0 1122 // Now handle projections. Cloning not required.
aoqi@0 1123 Node* new_if_f = (Node*)(new (phase->C) IfFalseNode( iff ));
aoqi@0 1124 Node* new_if_t = (Node*)(new (phase->C) IfTrueNode ( iff ));
aoqi@0 1125
aoqi@0 1126 igvn->register_new_node_with_optimizer(new_if_f);
aoqi@0 1127 igvn->register_new_node_with_optimizer(new_if_t);
aoqi@0 1128 // Flip test, so flip trailing control
aoqi@0 1129 igvn->replace_node(old_if_f, new_if_t);
aoqi@0 1130 igvn->replace_node(old_if_t, new_if_f);
aoqi@0 1131
aoqi@0 1132 // Progress
aoqi@0 1133 return iff;
aoqi@0 1134 }
aoqi@0 1135
aoqi@0 1136 //------------------------------Identity---------------------------------------
aoqi@0 1137 // If the test is constant & we match, then we are the input Control
aoqi@0 1138 Node *IfFalseNode::Identity( PhaseTransform *phase ) {
aoqi@0 1139 // Can only optimize if cannot go the other way
aoqi@0 1140 const TypeTuple *t = phase->type(in(0))->is_tuple();
aoqi@0 1141 return ( t == TypeTuple::IFNEITHER || t == TypeTuple::IFFALSE )
aoqi@0 1142 ? in(0)->in(0) // IfNode control
aoqi@0 1143 : this; // no progress
aoqi@0 1144 }

mercurial