1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "compiler/compileLog.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "opto/addnode.hpp" 29 #include "opto/callnode.hpp" 30 #include "opto/cfgnode.hpp" 31 #include "opto/constnode.hpp" 32 #include "opto/loopnode.hpp" 33 #include "opto/matcher.hpp" 34 #include "opto/movenode.hpp" 35 #include "opto/mulnode.hpp" 36 #include "opto/opcodes.hpp" 37 #include "opto/phaseX.hpp" 38 #include "opto/subnode.hpp" 39 #include "runtime/sharedRuntime.hpp" 40 41 // Portions of code courtesy of Clifford Click 42 43 // Optimization - Graph Style 44 45 #include "math.h" 46 47 //============================================================================= 48 //------------------------------Identity--------------------------------------- 49 // If right input is a constant 0, return the left input. 50 Node *SubNode::Identity( PhaseTransform *phase ) { 51 assert(in(1) != this, "Must already have called Value"); 52 assert(in(2) != this, "Must already have called Value"); 53 54 // Remove double negation 55 const Type *zero = add_id(); 56 if( phase->type( in(1) )->higher_equal( zero ) && 57 in(2)->Opcode() == Opcode() && 58 phase->type( in(2)->in(1) )->higher_equal( zero ) ) { 59 return in(2)->in(2); 60 } 61 62 // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y 63 if( in(1)->Opcode() == Op_AddI ) { 64 if( phase->eqv(in(1)->in(2),in(2)) ) 65 return in(1)->in(1); 66 if (phase->eqv(in(1)->in(1),in(2))) 67 return in(1)->in(2); 68 69 // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying 70 // trip counter and X is likely to be loop-invariant (that's how O2 Nodes 71 // are originally used, although the optimizer sometimes jiggers things). 72 // This folding through an O2 removes a loop-exit use of a loop-varying 73 // value and generally lowers register pressure in and around the loop. 74 if( in(1)->in(2)->Opcode() == Op_Opaque2 && 75 phase->eqv(in(1)->in(2)->in(1),in(2)) ) 76 return in(1)->in(1); 77 } 78 79 return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this; 80 } 81 82 //------------------------------Value------------------------------------------ 83 // A subtract node differences it's two inputs. 84 const Type *SubNode::Value( PhaseTransform *phase ) const { 85 const Node* in1 = in(1); 86 const Node* in2 = in(2); 87 // Either input is TOP ==> the result is TOP 88 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); 89 if( t1 == Type::TOP ) return Type::TOP; 90 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); 91 if( t2 == Type::TOP ) return Type::TOP; 92 93 // Not correct for SubFnode and AddFNode (must check for infinity) 94 // Equal? Subtract is zero 95 if (in1->eqv_uncast(in2)) return add_id(); 96 97 // Either input is BOTTOM ==> the result is the local BOTTOM 98 if( t1 == Type::BOTTOM || t2 == Type::BOTTOM ) 99 return bottom_type(); 100 101 return sub(t1,t2); // Local flavor of type subtraction 102 103 } 104 105 //============================================================================= 106 107 //------------------------------Helper function-------------------------------- 108 static bool ok_to_convert(Node* inc, Node* iv) { 109 // Do not collapse (x+c0)-y if "+" is a loop increment, because the 110 // "-" is loop invariant and collapsing extends the live-range of "x" 111 // to overlap with the "+", forcing another register to be used in 112 // the loop. 113 // This test will be clearer with '&&' (apply DeMorgan's rule) 114 // but I like the early cutouts that happen here. 115 const PhiNode *phi; 116 if( ( !inc->in(1)->is_Phi() || 117 !(phi=inc->in(1)->as_Phi()) || 118 phi->is_copy() || 119 !phi->region()->is_CountedLoop() || 120 inc != phi->region()->as_CountedLoop()->incr() ) 121 && 122 // Do not collapse (x+c0)-iv if "iv" is a loop induction variable, 123 // because "x" maybe invariant. 124 ( !iv->is_loop_iv() ) 125 ) { 126 return true; 127 } else { 128 return false; 129 } 130 } 131 //------------------------------Ideal------------------------------------------ 132 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){ 133 Node *in1 = in(1); 134 Node *in2 = in(2); 135 uint op1 = in1->Opcode(); 136 uint op2 = in2->Opcode(); 137 138 #ifdef ASSERT 139 // Check for dead loop 140 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || 141 ( op1 == Op_AddI || op1 == Op_SubI ) && 142 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || 143 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) 144 assert(false, "dead loop in SubINode::Ideal"); 145 #endif 146 147 const Type *t2 = phase->type( in2 ); 148 if( t2 == Type::TOP ) return NULL; 149 // Convert "x-c0" into "x+ -c0". 150 if( t2->base() == Type::Int ){ // Might be bottom or top... 151 const TypeInt *i = t2->is_int(); 152 if( i->is_con() ) 153 return new (phase->C) AddINode(in1, phase->intcon(-i->get_con())); 154 } 155 156 // Convert "(x+c0) - y" into (x-y) + c0" 157 // Do not collapse (x+c0)-y if "+" is a loop increment or 158 // if "y" is a loop induction variable. 159 if( op1 == Op_AddI && ok_to_convert(in1, in2) ) { 160 const Type *tadd = phase->type( in1->in(2) ); 161 if( tadd->singleton() && tadd != Type::TOP ) { 162 Node *sub2 = phase->transform( new (phase->C) SubINode( in1->in(1), in2 )); 163 return new (phase->C) AddINode( sub2, in1->in(2) ); 164 } 165 } 166 167 168 // Convert "x - (y+c0)" into "(x-y) - c0" 169 // Need the same check as in above optimization but reversed. 170 if (op2 == Op_AddI && ok_to_convert(in2, in1)) { 171 Node* in21 = in2->in(1); 172 Node* in22 = in2->in(2); 173 const TypeInt* tcon = phase->type(in22)->isa_int(); 174 if (tcon != NULL && tcon->is_con()) { 175 Node* sub2 = phase->transform( new (phase->C) SubINode(in1, in21) ); 176 Node* neg_c0 = phase->intcon(- tcon->get_con()); 177 return new (phase->C) AddINode(sub2, neg_c0); 178 } 179 } 180 181 const Type *t1 = phase->type( in1 ); 182 if( t1 == Type::TOP ) return NULL; 183 184 #ifdef ASSERT 185 // Check for dead loop 186 if( ( op2 == Op_AddI || op2 == Op_SubI ) && 187 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || 188 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) 189 assert(false, "dead loop in SubINode::Ideal"); 190 #endif 191 192 // Convert "x - (x+y)" into "-y" 193 if( op2 == Op_AddI && 194 phase->eqv( in1, in2->in(1) ) ) 195 return new (phase->C) SubINode( phase->intcon(0),in2->in(2)); 196 // Convert "(x-y) - x" into "-y" 197 if( op1 == Op_SubI && 198 phase->eqv( in1->in(1), in2 ) ) 199 return new (phase->C) SubINode( phase->intcon(0),in1->in(2)); 200 // Convert "x - (y+x)" into "-y" 201 if( op2 == Op_AddI && 202 phase->eqv( in1, in2->in(2) ) ) 203 return new (phase->C) SubINode( phase->intcon(0),in2->in(1)); 204 205 // Convert "0 - (x-y)" into "y-x" 206 if( t1 == TypeInt::ZERO && op2 == Op_SubI ) 207 return new (phase->C) SubINode( in2->in(2), in2->in(1) ); 208 209 // Convert "0 - (x+con)" into "-con-x" 210 jint con; 211 if( t1 == TypeInt::ZERO && op2 == Op_AddI && 212 (con = in2->in(2)->find_int_con(0)) != 0 ) 213 return new (phase->C) SubINode( phase->intcon(-con), in2->in(1) ); 214 215 // Convert "(X+A) - (X+B)" into "A - B" 216 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) ) 217 return new (phase->C) SubINode( in1->in(2), in2->in(2) ); 218 219 // Convert "(A+X) - (B+X)" into "A - B" 220 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) ) 221 return new (phase->C) SubINode( in1->in(1), in2->in(1) ); 222 223 // Convert "(A+X) - (X+B)" into "A - B" 224 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) ) 225 return new (phase->C) SubINode( in1->in(1), in2->in(2) ); 226 227 // Convert "(X+A) - (B+X)" into "A - B" 228 if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) ) 229 return new (phase->C) SubINode( in1->in(2), in2->in(1) ); 230 231 // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally 232 // nicer to optimize than subtract. 233 if( op2 == Op_SubI && in2->outcnt() == 1) { 234 Node *add1 = phase->transform( new (phase->C) AddINode( in1, in2->in(2) ) ); 235 return new (phase->C) SubINode( add1, in2->in(1) ); 236 } 237 238 return NULL; 239 } 240 241 //------------------------------sub-------------------------------------------- 242 // A subtract node differences it's two inputs. 243 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const { 244 const TypeInt *r0 = t1->is_int(); // Handy access 245 const TypeInt *r1 = t2->is_int(); 246 int32 lo = r0->_lo - r1->_hi; 247 int32 hi = r0->_hi - r1->_lo; 248 249 // We next check for 32-bit overflow. 250 // If that happens, we just assume all integers are possible. 251 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR 252 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND 253 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR 254 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs 255 return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen)); 256 else // Overflow; assume all integers 257 return TypeInt::INT; 258 } 259 260 //============================================================================= 261 //------------------------------Ideal------------------------------------------ 262 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 263 Node *in1 = in(1); 264 Node *in2 = in(2); 265 uint op1 = in1->Opcode(); 266 uint op2 = in2->Opcode(); 267 268 #ifdef ASSERT 269 // Check for dead loop 270 if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || 271 ( op1 == Op_AddL || op1 == Op_SubL ) && 272 ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || 273 phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) 274 assert(false, "dead loop in SubLNode::Ideal"); 275 #endif 276 277 if( phase->type( in2 ) == Type::TOP ) return NULL; 278 const TypeLong *i = phase->type( in2 )->isa_long(); 279 // Convert "x-c0" into "x+ -c0". 280 if( i && // Might be bottom or top... 281 i->is_con() ) 282 return new (phase->C) AddLNode(in1, phase->longcon(-i->get_con())); 283 284 // Convert "(x+c0) - y" into (x-y) + c0" 285 // Do not collapse (x+c0)-y if "+" is a loop increment or 286 // if "y" is a loop induction variable. 287 if( op1 == Op_AddL && ok_to_convert(in1, in2) ) { 288 Node *in11 = in1->in(1); 289 const Type *tadd = phase->type( in1->in(2) ); 290 if( tadd->singleton() && tadd != Type::TOP ) { 291 Node *sub2 = phase->transform( new (phase->C) SubLNode( in11, in2 )); 292 return new (phase->C) AddLNode( sub2, in1->in(2) ); 293 } 294 } 295 296 // Convert "x - (y+c0)" into "(x-y) - c0" 297 // Need the same check as in above optimization but reversed. 298 if (op2 == Op_AddL && ok_to_convert(in2, in1)) { 299 Node* in21 = in2->in(1); 300 Node* in22 = in2->in(2); 301 const TypeLong* tcon = phase->type(in22)->isa_long(); 302 if (tcon != NULL && tcon->is_con()) { 303 Node* sub2 = phase->transform( new (phase->C) SubLNode(in1, in21) ); 304 Node* neg_c0 = phase->longcon(- tcon->get_con()); 305 return new (phase->C) AddLNode(sub2, neg_c0); 306 } 307 } 308 309 const Type *t1 = phase->type( in1 ); 310 if( t1 == Type::TOP ) return NULL; 311 312 #ifdef ASSERT 313 // Check for dead loop 314 if( ( op2 == Op_AddL || op2 == Op_SubL ) && 315 ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || 316 phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) 317 assert(false, "dead loop in SubLNode::Ideal"); 318 #endif 319 320 // Convert "x - (x+y)" into "-y" 321 if( op2 == Op_AddL && 322 phase->eqv( in1, in2->in(1) ) ) 323 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2)); 324 // Convert "x - (y+x)" into "-y" 325 if( op2 == Op_AddL && 326 phase->eqv( in1, in2->in(2) ) ) 327 return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1)); 328 329 // Convert "0 - (x-y)" into "y-x" 330 if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL ) 331 return new (phase->C) SubLNode( in2->in(2), in2->in(1) ); 332 333 // Convert "(X+A) - (X+B)" into "A - B" 334 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) ) 335 return new (phase->C) SubLNode( in1->in(2), in2->in(2) ); 336 337 // Convert "(A+X) - (B+X)" into "A - B" 338 if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) ) 339 return new (phase->C) SubLNode( in1->in(1), in2->in(1) ); 340 341 // Convert "A-(B-C)" into (A+C)-B" 342 if( op2 == Op_SubL && in2->outcnt() == 1) { 343 Node *add1 = phase->transform( new (phase->C) AddLNode( in1, in2->in(2) ) ); 344 return new (phase->C) SubLNode( add1, in2->in(1) ); 345 } 346 347 return NULL; 348 } 349 350 //------------------------------sub-------------------------------------------- 351 // A subtract node differences it's two inputs. 352 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const { 353 const TypeLong *r0 = t1->is_long(); // Handy access 354 const TypeLong *r1 = t2->is_long(); 355 jlong lo = r0->_lo - r1->_hi; 356 jlong hi = r0->_hi - r1->_lo; 357 358 // We next check for 32-bit overflow. 359 // If that happens, we just assume all integers are possible. 360 if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR 361 ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND 362 (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR 363 ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs 364 return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen)); 365 else // Overflow; assume all integers 366 return TypeLong::LONG; 367 } 368 369 //============================================================================= 370 //------------------------------Value------------------------------------------ 371 // A subtract node differences its two inputs. 372 const Type *SubFPNode::Value( PhaseTransform *phase ) const { 373 const Node* in1 = in(1); 374 const Node* in2 = in(2); 375 // Either input is TOP ==> the result is TOP 376 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); 377 if( t1 == Type::TOP ) return Type::TOP; 378 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); 379 if( t2 == Type::TOP ) return Type::TOP; 380 381 // if both operands are infinity of same sign, the result is NaN; do 382 // not replace with zero 383 if( (t1->is_finite() && t2->is_finite()) ) { 384 if( phase->eqv(in1, in2) ) return add_id(); 385 } 386 387 // Either input is BOTTOM ==> the result is the local BOTTOM 388 const Type *bot = bottom_type(); 389 if( (t1 == bot) || (t2 == bot) || 390 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 391 return bot; 392 393 return sub(t1,t2); // Local flavor of type subtraction 394 } 395 396 397 //============================================================================= 398 //------------------------------Ideal------------------------------------------ 399 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 400 const Type *t2 = phase->type( in(2) ); 401 // Convert "x-c0" into "x+ -c0". 402 if( t2->base() == Type::FloatCon ) { // Might be bottom or top... 403 // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) ); 404 } 405 406 // Not associative because of boundary conditions (infinity) 407 if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 408 // Convert "x - (x+y)" into "-y" 409 if( in(2)->is_Add() && 410 phase->eqv(in(1),in(2)->in(1) ) ) 411 return new (phase->C) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2)); 412 } 413 414 // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes 415 // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0. 416 //if( phase->type(in(1)) == TypeF::ZERO ) 417 //return new (phase->C, 2) NegFNode(in(2)); 418 419 return NULL; 420 } 421 422 //------------------------------sub-------------------------------------------- 423 // A subtract node differences its two inputs. 424 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const { 425 // no folding if one of operands is infinity or NaN, do not do constant folding 426 if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) { 427 return TypeF::make( t1->getf() - t2->getf() ); 428 } 429 else if( g_isnan(t1->getf()) ) { 430 return t1; 431 } 432 else if( g_isnan(t2->getf()) ) { 433 return t2; 434 } 435 else { 436 return Type::FLOAT; 437 } 438 } 439 440 //============================================================================= 441 //------------------------------Ideal------------------------------------------ 442 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){ 443 const Type *t2 = phase->type( in(2) ); 444 // Convert "x-c0" into "x+ -c0". 445 if( t2->base() == Type::DoubleCon ) { // Might be bottom or top... 446 // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) ); 447 } 448 449 // Not associative because of boundary conditions (infinity) 450 if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 451 // Convert "x - (x+y)" into "-y" 452 if( in(2)->is_Add() && 453 phase->eqv(in(1),in(2)->in(1) ) ) 454 return new (phase->C) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2)); 455 } 456 457 // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes 458 // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0. 459 //if( phase->type(in(1)) == TypeD::ZERO ) 460 //return new (phase->C, 2) NegDNode(in(2)); 461 462 return NULL; 463 } 464 465 //------------------------------sub-------------------------------------------- 466 // A subtract node differences its two inputs. 467 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const { 468 // no folding if one of operands is infinity or NaN, do not do constant folding 469 if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) { 470 return TypeD::make( t1->getd() - t2->getd() ); 471 } 472 else if( g_isnan(t1->getd()) ) { 473 return t1; 474 } 475 else if( g_isnan(t2->getd()) ) { 476 return t2; 477 } 478 else { 479 return Type::DOUBLE; 480 } 481 } 482 483 //============================================================================= 484 //------------------------------Idealize--------------------------------------- 485 // Unlike SubNodes, compare must still flatten return value to the 486 // range -1, 0, 1. 487 // And optimizations like those for (X + Y) - X fail if overflow happens. 488 Node *CmpNode::Identity( PhaseTransform *phase ) { 489 return this; 490 } 491 492 //============================================================================= 493 //------------------------------cmp-------------------------------------------- 494 // Simplify a CmpI (compare 2 integers) node, based on local information. 495 // If both inputs are constants, compare them. 496 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const { 497 const TypeInt *r0 = t1->is_int(); // Handy access 498 const TypeInt *r1 = t2->is_int(); 499 500 if( r0->_hi < r1->_lo ) // Range is always low? 501 return TypeInt::CC_LT; 502 else if( r0->_lo > r1->_hi ) // Range is always high? 503 return TypeInt::CC_GT; 504 505 else if( r0->is_con() && r1->is_con() ) { // comparing constants? 506 assert(r0->get_con() == r1->get_con(), "must be equal"); 507 return TypeInt::CC_EQ; // Equal results. 508 } else if( r0->_hi == r1->_lo ) // Range is never high? 509 return TypeInt::CC_LE; 510 else if( r0->_lo == r1->_hi ) // Range is never low? 511 return TypeInt::CC_GE; 512 return TypeInt::CC; // else use worst case results 513 } 514 515 // Simplify a CmpU (compare 2 integers) node, based on local information. 516 // If both inputs are constants, compare them. 517 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const { 518 assert(!t1->isa_ptr(), "obsolete usage of CmpU"); 519 520 // comparing two unsigned ints 521 const TypeInt *r0 = t1->is_int(); // Handy access 522 const TypeInt *r1 = t2->is_int(); 523 524 // Current installed version 525 // Compare ranges for non-overlap 526 juint lo0 = r0->_lo; 527 juint hi0 = r0->_hi; 528 juint lo1 = r1->_lo; 529 juint hi1 = r1->_hi; 530 531 // If either one has both negative and positive values, 532 // it therefore contains both 0 and -1, and since [0..-1] is the 533 // full unsigned range, the type must act as an unsigned bottom. 534 bool bot0 = ((jint)(lo0 ^ hi0) < 0); 535 bool bot1 = ((jint)(lo1 ^ hi1) < 0); 536 537 if (bot0 || bot1) { 538 // All unsigned values are LE -1 and GE 0. 539 if (lo0 == 0 && hi0 == 0) { 540 return TypeInt::CC_LE; // 0 <= bot 541 } else if (lo1 == 0 && hi1 == 0) { 542 return TypeInt::CC_GE; // bot >= 0 543 } 544 } else { 545 // We can use ranges of the form [lo..hi] if signs are the same. 546 assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid"); 547 // results are reversed, '-' > '+' for unsigned compare 548 if (hi0 < lo1) { 549 return TypeInt::CC_LT; // smaller 550 } else if (lo0 > hi1) { 551 return TypeInt::CC_GT; // greater 552 } else if (hi0 == lo1 && lo0 == hi1) { 553 return TypeInt::CC_EQ; // Equal results 554 } else if (lo0 >= hi1) { 555 return TypeInt::CC_GE; 556 } else if (hi0 <= lo1) { 557 // Check for special case in Hashtable::get. (See below.) 558 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check()) 559 return TypeInt::CC_LT; 560 return TypeInt::CC_LE; 561 } 562 } 563 // Check for special case in Hashtable::get - the hash index is 564 // mod'ed to the table size so the following range check is useless. 565 // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have 566 // to be positive. 567 // (This is a gross hack, since the sub method never 568 // looks at the structure of the node in any other case.) 569 if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check()) 570 return TypeInt::CC_LT; 571 return TypeInt::CC; // else use worst case results 572 } 573 574 bool CmpUNode::is_index_range_check() const { 575 // Check for the "(X ModI Y) CmpU Y" shape 576 return (in(1)->Opcode() == Op_ModI && 577 in(1)->in(2)->eqv_uncast(in(2))); 578 } 579 580 //------------------------------Idealize--------------------------------------- 581 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) { 582 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) { 583 switch (in(1)->Opcode()) { 584 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL 585 return new (phase->C) CmpLNode(in(1)->in(1),in(1)->in(2)); 586 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF 587 return new (phase->C) CmpFNode(in(1)->in(1),in(1)->in(2)); 588 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD 589 return new (phase->C) CmpDNode(in(1)->in(1),in(1)->in(2)); 590 //case Op_SubI: 591 // If (x - y) cannot overflow, then ((x - y) <?> 0) 592 // can be turned into (x <?> y). 593 // This is handled (with more general cases) by Ideal_sub_algebra. 594 } 595 } 596 return NULL; // No change 597 } 598 599 600 //============================================================================= 601 // Simplify a CmpL (compare 2 longs ) node, based on local information. 602 // If both inputs are constants, compare them. 603 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const { 604 const TypeLong *r0 = t1->is_long(); // Handy access 605 const TypeLong *r1 = t2->is_long(); 606 607 if( r0->_hi < r1->_lo ) // Range is always low? 608 return TypeInt::CC_LT; 609 else if( r0->_lo > r1->_hi ) // Range is always high? 610 return TypeInt::CC_GT; 611 612 else if( r0->is_con() && r1->is_con() ) { // comparing constants? 613 assert(r0->get_con() == r1->get_con(), "must be equal"); 614 return TypeInt::CC_EQ; // Equal results. 615 } else if( r0->_hi == r1->_lo ) // Range is never high? 616 return TypeInt::CC_LE; 617 else if( r0->_lo == r1->_hi ) // Range is never low? 618 return TypeInt::CC_GE; 619 return TypeInt::CC; // else use worst case results 620 } 621 622 //============================================================================= 623 //------------------------------sub-------------------------------------------- 624 // Simplify an CmpP (compare 2 pointers) node, based on local information. 625 // If both inputs are constants, compare them. 626 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const { 627 const TypePtr *r0 = t1->is_ptr(); // Handy access 628 const TypePtr *r1 = t2->is_ptr(); 629 630 // Undefined inputs makes for an undefined result 631 if( TypePtr::above_centerline(r0->_ptr) || 632 TypePtr::above_centerline(r1->_ptr) ) 633 return Type::TOP; 634 635 if (r0 == r1 && r0->singleton()) { 636 // Equal pointer constants (klasses, nulls, etc.) 637 return TypeInt::CC_EQ; 638 } 639 640 // See if it is 2 unrelated classes. 641 const TypeOopPtr* p0 = r0->isa_oopptr(); 642 const TypeOopPtr* p1 = r1->isa_oopptr(); 643 if (p0 && p1) { 644 Node* in1 = in(1)->uncast(); 645 Node* in2 = in(2)->uncast(); 646 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL); 647 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL); 648 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) { 649 return TypeInt::CC_GT; // different pointers 650 } 651 ciKlass* klass0 = p0->klass(); 652 bool xklass0 = p0->klass_is_exact(); 653 ciKlass* klass1 = p1->klass(); 654 bool xklass1 = p1->klass_is_exact(); 655 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0); 656 if (klass0 && klass1 && 657 kps != 1 && // both or neither are klass pointers 658 klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces 659 klass1->is_loaded() && !klass1->is_interface() && 660 (!klass0->is_obj_array_klass() || 661 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) && 662 (!klass1->is_obj_array_klass() || 663 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) { 664 bool unrelated_classes = false; 665 // See if neither subclasses the other, or if the class on top 666 // is precise. In either of these cases, the compare is known 667 // to fail if at least one of the pointers is provably not null. 668 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal 669 // Do nothing; we know nothing for imprecise types 670 } else if (klass0->is_subtype_of(klass1)) { 671 // If klass1's type is PRECISE, then classes are unrelated. 672 unrelated_classes = xklass1; 673 } else if (klass1->is_subtype_of(klass0)) { 674 // If klass0's type is PRECISE, then classes are unrelated. 675 unrelated_classes = xklass0; 676 } else { // Neither subtypes the other 677 unrelated_classes = true; 678 } 679 if (unrelated_classes) { 680 // The oops classes are known to be unrelated. If the joined PTRs of 681 // two oops is not Null and not Bottom, then we are sure that one 682 // of the two oops is non-null, and the comparison will always fail. 683 TypePtr::PTR jp = r0->join_ptr(r1->_ptr); 684 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { 685 return TypeInt::CC_GT; 686 } 687 } 688 } 689 } 690 691 // Known constants can be compared exactly 692 // Null can be distinguished from any NotNull pointers 693 // Unknown inputs makes an unknown result 694 if( r0->singleton() ) { 695 intptr_t bits0 = r0->get_con(); 696 if( r1->singleton() ) 697 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT; 698 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC; 699 } else if( r1->singleton() ) { 700 intptr_t bits1 = r1->get_con(); 701 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC; 702 } else 703 return TypeInt::CC; 704 } 705 706 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) { 707 // Return the klass node for 708 // LoadP(AddP(foo:Klass, #java_mirror)) 709 // or NULL if not matching. 710 if (n->Opcode() != Op_LoadP) return NULL; 711 712 const TypeInstPtr* tp = phase->type(n)->isa_instptr(); 713 if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL; 714 715 Node* adr = n->in(MemNode::Address); 716 intptr_t off = 0; 717 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off); 718 if (k == NULL) return NULL; 719 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr(); 720 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL; 721 722 // We've found the klass node of a Java mirror load. 723 return k; 724 } 725 726 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) { 727 // for ConP(Foo.class) return ConP(Foo.klass) 728 // otherwise return NULL 729 if (!n->is_Con()) return NULL; 730 731 const TypeInstPtr* tp = phase->type(n)->isa_instptr(); 732 if (!tp) return NULL; 733 734 ciType* mirror_type = tp->java_mirror_type(); 735 // TypeInstPtr::java_mirror_type() returns non-NULL for compile- 736 // time Class constants only. 737 if (!mirror_type) return NULL; 738 739 // x.getClass() == int.class can never be true (for all primitive types) 740 // Return a ConP(NULL) node for this case. 741 if (mirror_type->is_classless()) { 742 return phase->makecon(TypePtr::NULL_PTR); 743 } 744 745 // return the ConP(Foo.klass) 746 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*"); 747 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass())); 748 } 749 750 //------------------------------Ideal------------------------------------------ 751 // Normalize comparisons between Java mirror loads to compare the klass instead. 752 // 753 // Also check for the case of comparing an unknown klass loaded from the primary 754 // super-type array vs a known klass with no subtypes. This amounts to 755 // checking to see an unknown klass subtypes a known klass with no subtypes; 756 // this only happens on an exact match. We can shorten this test by 1 load. 757 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) { 758 // Normalize comparisons between Java mirrors into comparisons of the low- 759 // level klass, where a dependent load could be shortened. 760 // 761 // The new pattern has a nice effect of matching the same pattern used in the 762 // fast path of instanceof/checkcast/Class.isInstance(), which allows 763 // redundant exact type check be optimized away by GVN. 764 // For example, in 765 // if (x.getClass() == Foo.class) { 766 // Foo foo = (Foo) x; 767 // // ... use a ... 768 // } 769 // a CmpPNode could be shared between if_acmpne and checkcast 770 { 771 Node* k1 = isa_java_mirror_load(phase, in(1)); 772 Node* k2 = isa_java_mirror_load(phase, in(2)); 773 Node* conk2 = isa_const_java_mirror(phase, in(2)); 774 775 if (k1 && (k2 || conk2)) { 776 Node* lhs = k1; 777 Node* rhs = (k2 != NULL) ? k2 : conk2; 778 this->set_req(1, lhs); 779 this->set_req(2, rhs); 780 return this; 781 } 782 } 783 784 // Constant pointer on right? 785 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr(); 786 if (t2 == NULL || !t2->klass_is_exact()) 787 return NULL; 788 // Get the constant klass we are comparing to. 789 ciKlass* superklass = t2->klass(); 790 791 // Now check for LoadKlass on left. 792 Node* ldk1 = in(1); 793 if (ldk1->is_DecodeNKlass()) { 794 ldk1 = ldk1->in(1); 795 if (ldk1->Opcode() != Op_LoadNKlass ) 796 return NULL; 797 } else if (ldk1->Opcode() != Op_LoadKlass ) 798 return NULL; 799 // Take apart the address of the LoadKlass: 800 Node* adr1 = ldk1->in(MemNode::Address); 801 intptr_t con2 = 0; 802 Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2); 803 if (ldk2 == NULL) 804 return NULL; 805 if (con2 == oopDesc::klass_offset_in_bytes()) { 806 // We are inspecting an object's concrete class. 807 // Short-circuit the check if the query is abstract. 808 if (superklass->is_interface() || 809 superklass->is_abstract()) { 810 // Make it come out always false: 811 this->set_req(2, phase->makecon(TypePtr::NULL_PTR)); 812 return this; 813 } 814 } 815 816 // Check for a LoadKlass from primary supertype array. 817 // Any nested loadklass from loadklass+con must be from the p.s. array. 818 if (ldk2->is_DecodeNKlass()) { 819 // Keep ldk2 as DecodeN since it could be used in CmpP below. 820 if (ldk2->in(1)->Opcode() != Op_LoadNKlass ) 821 return NULL; 822 } else if (ldk2->Opcode() != Op_LoadKlass) 823 return NULL; 824 825 // Verify that we understand the situation 826 if (con2 != (intptr_t) superklass->super_check_offset()) 827 return NULL; // Might be element-klass loading from array klass 828 829 // If 'superklass' has no subklasses and is not an interface, then we are 830 // assured that the only input which will pass the type check is 831 // 'superklass' itself. 832 // 833 // We could be more liberal here, and allow the optimization on interfaces 834 // which have a single implementor. This would require us to increase the 835 // expressiveness of the add_dependency() mechanism. 836 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now. 837 838 // Object arrays must have their base element have no subtypes 839 while (superklass->is_obj_array_klass()) { 840 ciType* elem = superklass->as_obj_array_klass()->element_type(); 841 superklass = elem->as_klass(); 842 } 843 if (superklass->is_instance_klass()) { 844 ciInstanceKlass* ik = superklass->as_instance_klass(); 845 if (ik->has_subklass() || ik->is_interface()) return NULL; 846 // Add a dependency if there is a chance that a subclass will be added later. 847 if (!ik->is_final()) { 848 phase->C->dependencies()->assert_leaf_type(ik); 849 } 850 } 851 852 // Bypass the dependent load, and compare directly 853 this->set_req(1,ldk2); 854 855 return this; 856 } 857 858 //============================================================================= 859 //------------------------------sub-------------------------------------------- 860 // Simplify an CmpN (compare 2 pointers) node, based on local information. 861 // If both inputs are constants, compare them. 862 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const { 863 const TypePtr *r0 = t1->make_ptr(); // Handy access 864 const TypePtr *r1 = t2->make_ptr(); 865 866 // Undefined inputs makes for an undefined result 867 if ((r0 == NULL) || (r1 == NULL) || 868 TypePtr::above_centerline(r0->_ptr) || 869 TypePtr::above_centerline(r1->_ptr)) { 870 return Type::TOP; 871 } 872 if (r0 == r1 && r0->singleton()) { 873 // Equal pointer constants (klasses, nulls, etc.) 874 return TypeInt::CC_EQ; 875 } 876 877 // See if it is 2 unrelated classes. 878 const TypeOopPtr* p0 = r0->isa_oopptr(); 879 const TypeOopPtr* p1 = r1->isa_oopptr(); 880 if (p0 && p1) { 881 ciKlass* klass0 = p0->klass(); 882 bool xklass0 = p0->klass_is_exact(); 883 ciKlass* klass1 = p1->klass(); 884 bool xklass1 = p1->klass_is_exact(); 885 int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0); 886 if (klass0 && klass1 && 887 kps != 1 && // both or neither are klass pointers 888 !klass0->is_interface() && // do not trust interfaces 889 !klass1->is_interface()) { 890 bool unrelated_classes = false; 891 // See if neither subclasses the other, or if the class on top 892 // is precise. In either of these cases, the compare is known 893 // to fail if at least one of the pointers is provably not null. 894 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal 895 // Do nothing; we know nothing for imprecise types 896 } else if (klass0->is_subtype_of(klass1)) { 897 // If klass1's type is PRECISE, then classes are unrelated. 898 unrelated_classes = xklass1; 899 } else if (klass1->is_subtype_of(klass0)) { 900 // If klass0's type is PRECISE, then classes are unrelated. 901 unrelated_classes = xklass0; 902 } else { // Neither subtypes the other 903 unrelated_classes = true; 904 } 905 if (unrelated_classes) { 906 // The oops classes are known to be unrelated. If the joined PTRs of 907 // two oops is not Null and not Bottom, then we are sure that one 908 // of the two oops is non-null, and the comparison will always fail. 909 TypePtr::PTR jp = r0->join_ptr(r1->_ptr); 910 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { 911 return TypeInt::CC_GT; 912 } 913 } 914 } 915 } 916 917 // Known constants can be compared exactly 918 // Null can be distinguished from any NotNull pointers 919 // Unknown inputs makes an unknown result 920 if( r0->singleton() ) { 921 intptr_t bits0 = r0->get_con(); 922 if( r1->singleton() ) 923 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT; 924 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC; 925 } else if( r1->singleton() ) { 926 intptr_t bits1 = r1->get_con(); 927 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC; 928 } else 929 return TypeInt::CC; 930 } 931 932 //------------------------------Ideal------------------------------------------ 933 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) { 934 return NULL; 935 } 936 937 //============================================================================= 938 //------------------------------Value------------------------------------------ 939 // Simplify an CmpF (compare 2 floats ) node, based on local information. 940 // If both inputs are constants, compare them. 941 const Type *CmpFNode::Value( PhaseTransform *phase ) const { 942 const Node* in1 = in(1); 943 const Node* in2 = in(2); 944 // Either input is TOP ==> the result is TOP 945 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); 946 if( t1 == Type::TOP ) return Type::TOP; 947 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); 948 if( t2 == Type::TOP ) return Type::TOP; 949 950 // Not constants? Don't know squat - even if they are the same 951 // value! If they are NaN's they compare to LT instead of EQ. 952 const TypeF *tf1 = t1->isa_float_constant(); 953 const TypeF *tf2 = t2->isa_float_constant(); 954 if( !tf1 || !tf2 ) return TypeInt::CC; 955 956 // This implements the Java bytecode fcmpl, so unordered returns -1. 957 if( tf1->is_nan() || tf2->is_nan() ) 958 return TypeInt::CC_LT; 959 960 if( tf1->_f < tf2->_f ) return TypeInt::CC_LT; 961 if( tf1->_f > tf2->_f ) return TypeInt::CC_GT; 962 assert( tf1->_f == tf2->_f, "do not understand FP behavior" ); 963 return TypeInt::CC_EQ; 964 } 965 966 967 //============================================================================= 968 //------------------------------Value------------------------------------------ 969 // Simplify an CmpD (compare 2 doubles ) node, based on local information. 970 // If both inputs are constants, compare them. 971 const Type *CmpDNode::Value( PhaseTransform *phase ) const { 972 const Node* in1 = in(1); 973 const Node* in2 = in(2); 974 // Either input is TOP ==> the result is TOP 975 const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); 976 if( t1 == Type::TOP ) return Type::TOP; 977 const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); 978 if( t2 == Type::TOP ) return Type::TOP; 979 980 // Not constants? Don't know squat - even if they are the same 981 // value! If they are NaN's they compare to LT instead of EQ. 982 const TypeD *td1 = t1->isa_double_constant(); 983 const TypeD *td2 = t2->isa_double_constant(); 984 if( !td1 || !td2 ) return TypeInt::CC; 985 986 // This implements the Java bytecode dcmpl, so unordered returns -1. 987 if( td1->is_nan() || td2->is_nan() ) 988 return TypeInt::CC_LT; 989 990 if( td1->_d < td2->_d ) return TypeInt::CC_LT; 991 if( td1->_d > td2->_d ) return TypeInt::CC_GT; 992 assert( td1->_d == td2->_d, "do not understand FP behavior" ); 993 return TypeInt::CC_EQ; 994 } 995 996 //------------------------------Ideal------------------------------------------ 997 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){ 998 // Check if we can change this to a CmpF and remove a ConvD2F operation. 999 // Change (CMPD (F2D (float)) (ConD value)) 1000 // To (CMPF (float) (ConF value)) 1001 // Valid when 'value' does not lose precision as a float. 1002 // Benefits: eliminates conversion, does not require 24-bit mode 1003 1004 // NaNs prevent commuting operands. This transform works regardless of the 1005 // order of ConD and ConvF2D inputs by preserving the original order. 1006 int idx_f2d = 1; // ConvF2D on left side? 1007 if( in(idx_f2d)->Opcode() != Op_ConvF2D ) 1008 idx_f2d = 2; // No, swap to check for reversed args 1009 int idx_con = 3-idx_f2d; // Check for the constant on other input 1010 1011 if( ConvertCmpD2CmpF && 1012 in(idx_f2d)->Opcode() == Op_ConvF2D && 1013 in(idx_con)->Opcode() == Op_ConD ) { 1014 const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant(); 1015 double t2_value_as_double = t2->_d; 1016 float t2_value_as_float = (float)t2_value_as_double; 1017 if( t2_value_as_double == (double)t2_value_as_float ) { 1018 // Test value can be represented as a float 1019 // Eliminate the conversion to double and create new comparison 1020 Node *new_in1 = in(idx_f2d)->in(1); 1021 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) ); 1022 if( idx_f2d != 1 ) { // Must flip args to match original order 1023 Node *tmp = new_in1; 1024 new_in1 = new_in2; 1025 new_in2 = tmp; 1026 } 1027 CmpFNode *new_cmp = (Opcode() == Op_CmpD3) 1028 ? new (phase->C) CmpF3Node( new_in1, new_in2 ) 1029 : new (phase->C) CmpFNode ( new_in1, new_in2 ) ; 1030 return new_cmp; // Changed to CmpFNode 1031 } 1032 // Testing value required the precision of a double 1033 } 1034 return NULL; // No change 1035 } 1036 1037 1038 //============================================================================= 1039 //------------------------------cc2logical------------------------------------- 1040 // Convert a condition code type to a logical type 1041 const Type *BoolTest::cc2logical( const Type *CC ) const { 1042 if( CC == Type::TOP ) return Type::TOP; 1043 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse 1044 const TypeInt *ti = CC->is_int(); 1045 if( ti->is_con() ) { // Only 1 kind of condition codes set? 1046 // Match low order 2 bits 1047 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0; 1048 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result 1049 return TypeInt::make(tmp); // Boolean result 1050 } 1051 1052 if( CC == TypeInt::CC_GE ) { 1053 if( _test == ge ) return TypeInt::ONE; 1054 if( _test == lt ) return TypeInt::ZERO; 1055 } 1056 if( CC == TypeInt::CC_LE ) { 1057 if( _test == le ) return TypeInt::ONE; 1058 if( _test == gt ) return TypeInt::ZERO; 1059 } 1060 1061 return TypeInt::BOOL; 1062 } 1063 1064 //------------------------------dump_spec------------------------------------- 1065 // Print special per-node info 1066 #ifndef PRODUCT 1067 void BoolTest::dump_on(outputStream *st) const { 1068 const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"}; 1069 st->print(msg[_test]); 1070 } 1071 #endif 1072 1073 //============================================================================= 1074 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); } 1075 uint BoolNode::size_of() const { return sizeof(BoolNode); } 1076 1077 //------------------------------operator==------------------------------------- 1078 uint BoolNode::cmp( const Node &n ) const { 1079 const BoolNode *b = (const BoolNode *)&n; // Cast up 1080 return (_test._test == b->_test._test); 1081 } 1082 1083 //-------------------------------make_predicate-------------------------------- 1084 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) { 1085 if (test_value->is_Con()) return test_value; 1086 if (test_value->is_Bool()) return test_value; 1087 Compile* C = phase->C; 1088 if (test_value->is_CMove() && 1089 test_value->in(CMoveNode::Condition)->is_Bool()) { 1090 BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool(); 1091 const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse)); 1092 const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue)); 1093 if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) { 1094 return bol; 1095 } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) { 1096 return phase->transform( bol->negate(phase) ); 1097 } 1098 // Else fall through. The CMove gets in the way of the test. 1099 // It should be the case that make_predicate(bol->as_int_value()) == bol. 1100 } 1101 Node* cmp = new (C) CmpINode(test_value, phase->intcon(0)); 1102 cmp = phase->transform(cmp); 1103 Node* bol = new (C) BoolNode(cmp, BoolTest::ne); 1104 return phase->transform(bol); 1105 } 1106 1107 //--------------------------------as_int_value--------------------------------- 1108 Node* BoolNode::as_int_value(PhaseGVN* phase) { 1109 // Inverse to make_predicate. The CMove probably boils down to a Conv2B. 1110 Node* cmov = CMoveNode::make(phase->C, NULL, this, 1111 phase->intcon(0), phase->intcon(1), 1112 TypeInt::BOOL); 1113 return phase->transform(cmov); 1114 } 1115 1116 //----------------------------------negate------------------------------------- 1117 BoolNode* BoolNode::negate(PhaseGVN* phase) { 1118 Compile* C = phase->C; 1119 return new (C) BoolNode(in(1), _test.negate()); 1120 } 1121 1122 1123 //------------------------------Ideal------------------------------------------ 1124 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1125 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)". 1126 // This moves the constant to the right. Helps value-numbering. 1127 Node *cmp = in(1); 1128 if( !cmp->is_Sub() ) return NULL; 1129 int cop = cmp->Opcode(); 1130 if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL; 1131 Node *cmp1 = cmp->in(1); 1132 Node *cmp2 = cmp->in(2); 1133 if( !cmp1 ) return NULL; 1134 1135 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) { 1136 return NULL; 1137 } 1138 1139 // Constant on left? 1140 Node *con = cmp1; 1141 uint op2 = cmp2->Opcode(); 1142 // Move constants to the right of compare's to canonicalize. 1143 // Do not muck with Opaque1 nodes, as this indicates a loop 1144 // guard that cannot change shape. 1145 if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 && 1146 // Because of NaN's, CmpD and CmpF are not commutative 1147 cop != Op_CmpD && cop != Op_CmpF && 1148 // Protect against swapping inputs to a compare when it is used by a 1149 // counted loop exit, which requires maintaining the loop-limit as in(2) 1150 !is_counted_loop_exit_test() ) { 1151 // Ok, commute the constant to the right of the cmp node. 1152 // Clone the Node, getting a new Node of the same class 1153 cmp = cmp->clone(); 1154 // Swap inputs to the clone 1155 cmp->swap_edges(1, 2); 1156 cmp = phase->transform( cmp ); 1157 return new (phase->C) BoolNode( cmp, _test.commute() ); 1158 } 1159 1160 // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)". 1161 // The XOR-1 is an idiom used to flip the sense of a bool. We flip the 1162 // test instead. 1163 int cmp1_op = cmp1->Opcode(); 1164 const TypeInt* cmp2_type = phase->type(cmp2)->isa_int(); 1165 if (cmp2_type == NULL) return NULL; 1166 Node* j_xor = cmp1; 1167 if( cmp2_type == TypeInt::ZERO && 1168 cmp1_op == Op_XorI && 1169 j_xor->in(1) != j_xor && // An xor of itself is dead 1170 phase->type( j_xor->in(1) ) == TypeInt::BOOL && 1171 phase->type( j_xor->in(2) ) == TypeInt::ONE && 1172 (_test._test == BoolTest::eq || 1173 _test._test == BoolTest::ne) ) { 1174 Node *ncmp = phase->transform(new (phase->C) CmpINode(j_xor->in(1),cmp2)); 1175 return new (phase->C) BoolNode( ncmp, _test.negate() ); 1176 } 1177 1178 // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)". 1179 // This is a standard idiom for branching on a boolean value. 1180 Node *c2b = cmp1; 1181 if( cmp2_type == TypeInt::ZERO && 1182 cmp1_op == Op_Conv2B && 1183 (_test._test == BoolTest::eq || 1184 _test._test == BoolTest::ne) ) { 1185 Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int() 1186 ? (Node*)new (phase->C) CmpINode(c2b->in(1),cmp2) 1187 : (Node*)new (phase->C) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR)) 1188 ); 1189 return new (phase->C) BoolNode( ncmp, _test._test ); 1190 } 1191 1192 // Comparing a SubI against a zero is equal to comparing the SubI 1193 // arguments directly. This only works for eq and ne comparisons 1194 // due to possible integer overflow. 1195 if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) && 1196 (cop == Op_CmpI) && 1197 (cmp1->Opcode() == Op_SubI) && 1198 ( cmp2_type == TypeInt::ZERO ) ) { 1199 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(1),cmp1->in(2))); 1200 return new (phase->C) BoolNode( ncmp, _test._test ); 1201 } 1202 1203 // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the 1204 // most general case because negating 0x80000000 does nothing. Needed for 1205 // the CmpF3/SubI/CmpI idiom. 1206 if( cop == Op_CmpI && 1207 cmp1->Opcode() == Op_SubI && 1208 cmp2_type == TypeInt::ZERO && 1209 phase->type( cmp1->in(1) ) == TypeInt::ZERO && 1210 phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) { 1211 Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2)); 1212 return new (phase->C) BoolNode( ncmp, _test.commute() ); 1213 } 1214 1215 // The transformation below is not valid for either signed or unsigned 1216 // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE. 1217 // This transformation can be resurrected when we are able to 1218 // make inferences about the range of values being subtracted from 1219 // (or added to) relative to the wraparound point. 1220 // 1221 // // Remove +/-1's if possible. 1222 // // "X <= Y-1" becomes "X < Y" 1223 // // "X+1 <= Y" becomes "X < Y" 1224 // // "X < Y+1" becomes "X <= Y" 1225 // // "X-1 < Y" becomes "X <= Y" 1226 // // Do not this to compares off of the counted-loop-end. These guys are 1227 // // checking the trip counter and they want to use the post-incremented 1228 // // counter. If they use the PRE-incremented counter, then the counter has 1229 // // to be incremented in a private block on a loop backedge. 1230 // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd ) 1231 // return NULL; 1232 // #ifndef PRODUCT 1233 // // Do not do this in a wash GVN pass during verification. 1234 // // Gets triggered by too many simple optimizations to be bothered with 1235 // // re-trying it again and again. 1236 // if( !phase->allow_progress() ) return NULL; 1237 // #endif 1238 // // Not valid for unsigned compare because of corner cases in involving zero. 1239 // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an 1240 // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but 1241 // // "0 <=u Y" is always true). 1242 // if( cmp->Opcode() == Op_CmpU ) return NULL; 1243 // int cmp2_op = cmp2->Opcode(); 1244 // if( _test._test == BoolTest::le ) { 1245 // if( cmp1_op == Op_AddI && 1246 // phase->type( cmp1->in(2) ) == TypeInt::ONE ) 1247 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt ); 1248 // else if( cmp2_op == Op_AddI && 1249 // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 ) 1250 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt ); 1251 // } else if( _test._test == BoolTest::lt ) { 1252 // if( cmp1_op == Op_AddI && 1253 // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 ) 1254 // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le ); 1255 // else if( cmp2_op == Op_AddI && 1256 // phase->type( cmp2->in(2) ) == TypeInt::ONE ) 1257 // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le ); 1258 // } 1259 1260 return NULL; 1261 } 1262 1263 //------------------------------Value------------------------------------------ 1264 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node, 1265 // based on local information. If the input is constant, do it. 1266 const Type *BoolNode::Value( PhaseTransform *phase ) const { 1267 return _test.cc2logical( phase->type( in(1) ) ); 1268 } 1269 1270 //------------------------------dump_spec-------------------------------------- 1271 // Dump special per-node info 1272 #ifndef PRODUCT 1273 void BoolNode::dump_spec(outputStream *st) const { 1274 st->print("["); 1275 _test.dump_on(st); 1276 st->print("]"); 1277 } 1278 #endif 1279 1280 //------------------------------is_counted_loop_exit_test-------------------------------------- 1281 // Returns true if node is used by a counted loop node. 1282 bool BoolNode::is_counted_loop_exit_test() { 1283 for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) { 1284 Node* use = fast_out(i); 1285 if (use->is_CountedLoopEnd()) { 1286 return true; 1287 } 1288 } 1289 return false; 1290 } 1291 1292 //============================================================================= 1293 //------------------------------Value------------------------------------------ 1294 // Compute sqrt 1295 const Type *SqrtDNode::Value( PhaseTransform *phase ) const { 1296 const Type *t1 = phase->type( in(1) ); 1297 if( t1 == Type::TOP ) return Type::TOP; 1298 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1299 double d = t1->getd(); 1300 if( d < 0.0 ) return Type::DOUBLE; 1301 return TypeD::make( sqrt( d ) ); 1302 } 1303 1304 //============================================================================= 1305 //------------------------------Value------------------------------------------ 1306 // Compute cos 1307 const Type *CosDNode::Value( PhaseTransform *phase ) const { 1308 const Type *t1 = phase->type( in(1) ); 1309 if( t1 == Type::TOP ) return Type::TOP; 1310 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1311 double d = t1->getd(); 1312 return TypeD::make( StubRoutines::intrinsic_cos( d ) ); 1313 } 1314 1315 //============================================================================= 1316 //------------------------------Value------------------------------------------ 1317 // Compute sin 1318 const Type *SinDNode::Value( PhaseTransform *phase ) const { 1319 const Type *t1 = phase->type( in(1) ); 1320 if( t1 == Type::TOP ) return Type::TOP; 1321 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1322 double d = t1->getd(); 1323 return TypeD::make( StubRoutines::intrinsic_sin( d ) ); 1324 } 1325 1326 //============================================================================= 1327 //------------------------------Value------------------------------------------ 1328 // Compute tan 1329 const Type *TanDNode::Value( PhaseTransform *phase ) const { 1330 const Type *t1 = phase->type( in(1) ); 1331 if( t1 == Type::TOP ) return Type::TOP; 1332 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1333 double d = t1->getd(); 1334 return TypeD::make( StubRoutines::intrinsic_tan( d ) ); 1335 } 1336 1337 //============================================================================= 1338 //------------------------------Value------------------------------------------ 1339 // Compute log 1340 const Type *LogDNode::Value( PhaseTransform *phase ) const { 1341 const Type *t1 = phase->type( in(1) ); 1342 if( t1 == Type::TOP ) return Type::TOP; 1343 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1344 double d = t1->getd(); 1345 return TypeD::make( StubRoutines::intrinsic_log( d ) ); 1346 } 1347 1348 //============================================================================= 1349 //------------------------------Value------------------------------------------ 1350 // Compute log10 1351 const Type *Log10DNode::Value( PhaseTransform *phase ) const { 1352 const Type *t1 = phase->type( in(1) ); 1353 if( t1 == Type::TOP ) return Type::TOP; 1354 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1355 double d = t1->getd(); 1356 return TypeD::make( StubRoutines::intrinsic_log10( d ) ); 1357 } 1358 1359 //============================================================================= 1360 //------------------------------Value------------------------------------------ 1361 // Compute exp 1362 const Type *ExpDNode::Value( PhaseTransform *phase ) const { 1363 const Type *t1 = phase->type( in(1) ); 1364 if( t1 == Type::TOP ) return Type::TOP; 1365 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1366 double d = t1->getd(); 1367 return TypeD::make( StubRoutines::intrinsic_exp( d ) ); 1368 } 1369 1370 1371 //============================================================================= 1372 //------------------------------Value------------------------------------------ 1373 // Compute pow 1374 const Type *PowDNode::Value( PhaseTransform *phase ) const { 1375 const Type *t1 = phase->type( in(1) ); 1376 if( t1 == Type::TOP ) return Type::TOP; 1377 if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; 1378 const Type *t2 = phase->type( in(2) ); 1379 if( t2 == Type::TOP ) return Type::TOP; 1380 if( t2->base() != Type::DoubleCon ) return Type::DOUBLE; 1381 double d1 = t1->getd(); 1382 double d2 = t2->getd(); 1383 return TypeD::make( StubRoutines::intrinsic_pow( d1, d2 ) ); 1384 }